xref: /openbmc/linux/net/dsa/slave.c (revision e31dbd3b6aba585231cd84a87adeb22e7c6a8c19)
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:
177e35b8d7dSJakub Kicinski 	eth_hw_addr_set(dev, addr->sa_data);
17891da11f8SLennert Buytenhek 
17991da11f8SLennert Buytenhek 	return 0;
18091da11f8SLennert Buytenhek }
18191da11f8SLennert Buytenhek 
1822bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1832bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1842bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1852bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1862bedde1aSArkadi Sharshevsky 	int idx;
1872bedde1aSArkadi Sharshevsky };
1882bedde1aSArkadi Sharshevsky 
1892bedde1aSArkadi Sharshevsky static int
1902bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1912bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
1922bedde1aSArkadi Sharshevsky {
1932bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
1942bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1952bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
1962bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
1972bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
1982bedde1aSArkadi Sharshevsky 
1992bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
2002bedde1aSArkadi Sharshevsky 		goto skip;
2012bedde1aSArkadi Sharshevsky 
2022bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
2032bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2042bedde1aSArkadi Sharshevsky 	if (!nlh)
2052bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2062bedde1aSArkadi Sharshevsky 
2072bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2082bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2092bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2102bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2112bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2122bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2132bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2142bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2152bedde1aSArkadi Sharshevsky 
2162bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2172bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2182bedde1aSArkadi Sharshevsky 
2192bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2202bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2212bedde1aSArkadi Sharshevsky 
2222bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2232bedde1aSArkadi Sharshevsky 
2242bedde1aSArkadi Sharshevsky skip:
2252bedde1aSArkadi Sharshevsky 	dump->idx++;
2262bedde1aSArkadi Sharshevsky 	return 0;
2272bedde1aSArkadi Sharshevsky 
2282bedde1aSArkadi Sharshevsky nla_put_failure:
2292bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2302bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2312bedde1aSArkadi Sharshevsky }
2322bedde1aSArkadi Sharshevsky 
2332bedde1aSArkadi Sharshevsky static int
2342bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2352bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2362bedde1aSArkadi Sharshevsky 		   int *idx)
2372bedde1aSArkadi Sharshevsky {
238d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2392bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2402bedde1aSArkadi Sharshevsky 		.dev = dev,
2412bedde1aSArkadi Sharshevsky 		.skb = skb,
2422bedde1aSArkadi Sharshevsky 		.cb = cb,
2432bedde1aSArkadi Sharshevsky 		.idx = *idx,
2442bedde1aSArkadi Sharshevsky 	};
2452bedde1aSArkadi Sharshevsky 	int err;
2462bedde1aSArkadi Sharshevsky 
247de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2482bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
249de40fc5dSVivien Didelot 
2502bedde1aSArkadi Sharshevsky 	return err;
2512bedde1aSArkadi Sharshevsky }
2522bedde1aSArkadi Sharshevsky 
25391da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
25491da11f8SLennert Buytenhek {
2550336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2560336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2570336369dSBrandon Streiff 	int port = p->dp->index;
2580336369dSBrandon Streiff 
2590336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2600336369dSBrandon Streiff 	switch (cmd) {
2610336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2620336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2630336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2640336369dSBrandon Streiff 		break;
2650336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2660336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2670336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2680336369dSBrandon Streiff 		break;
2690336369dSBrandon Streiff 	}
2700336369dSBrandon Streiff 
271aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
27291da11f8SLennert Buytenhek }
27391da11f8SLennert Buytenhek 
27469bfac96SVladimir Oltean static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
2754c08c586SVladimir Oltean 				   const struct switchdev_attr *attr,
2764c08c586SVladimir Oltean 				   struct netlink_ext_ack *extack)
27735636062SScott Feldman {
278d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
279b8d866acSVivien Didelot 	int ret;
28035636062SScott Feldman 
2810d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
2820d2cfbd4SVladimir Oltean 		return 0;
2830d2cfbd4SVladimir Oltean 
28435636062SScott Feldman 	switch (attr->id) {
2851f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
28603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
28703cbb870SVladimir Oltean 			return -EOPNOTSUPP;
28803cbb870SVladimir Oltean 
28939f32101SVladimir Oltean 		ret = dsa_port_set_state(dp, attr->u.stp_state, true);
29035636062SScott Feldman 		break;
291fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
292936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
29303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
29403cbb870SVladimir Oltean 
29589153ed6SVladimir Oltean 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
29689153ed6SVladimir Oltean 					      extack);
297fb2dabadSVivien Didelot 		break;
29834a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
299936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
30003cbb870SVladimir Oltean 			return -EOPNOTSUPP;
30103cbb870SVladimir Oltean 
302bae33f2bSVladimir Oltean 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
30334a79f63SVivien Didelot 		break;
304ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
30503cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
30603cbb870SVladimir Oltean 			return -EOPNOTSUPP;
30703cbb870SVladimir Oltean 
308a8b659e7SVladimir Oltean 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
309a8b659e7SVladimir Oltean 						extack);
310ea87005aSFlorian Fainelli 		break;
31157652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
31203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
31303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
31403cbb870SVladimir Oltean 
315a8b659e7SVladimir Oltean 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
31657652796SRussell King 		break;
31735636062SScott Feldman 	default:
31835636062SScott Feldman 		ret = -EOPNOTSUPP;
31935636062SScott Feldman 		break;
32035636062SScott Feldman 	}
32135636062SScott Feldman 
32235636062SScott Feldman 	return ret;
32335636062SScott Feldman }
32435636062SScott Feldman 
3251ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */
3261ce39f0eSVladimir Oltean static int
3271ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
3281ce39f0eSVladimir Oltean 				      const struct switchdev_obj_port_vlan *vlan)
3291ce39f0eSVladimir Oltean {
3301ce39f0eSVladimir Oltean 	struct net_device *upper_dev;
3311ce39f0eSVladimir Oltean 	struct list_head *iter;
3321ce39f0eSVladimir Oltean 
3331ce39f0eSVladimir Oltean 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
3341ce39f0eSVladimir Oltean 		u16 vid;
3351ce39f0eSVladimir Oltean 
3361ce39f0eSVladimir Oltean 		if (!is_vlan_dev(upper_dev))
3371ce39f0eSVladimir Oltean 			continue;
3381ce39f0eSVladimir Oltean 
3391ce39f0eSVladimir Oltean 		vid = vlan_dev_vlan_id(upper_dev);
340b7a9e0daSVladimir Oltean 		if (vid == vlan->vid)
3411ce39f0eSVladimir Oltean 			return -EBUSY;
3421ce39f0eSVladimir Oltean 	}
3431ce39f0eSVladimir Oltean 
3441ce39f0eSVladimir Oltean 	return 0;
3451ce39f0eSVladimir Oltean }
3461ce39f0eSVladimir Oltean 
347bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
3480ee2af4eSVladimir Oltean 			      const struct switchdev_obj *obj,
3490ee2af4eSVladimir Oltean 			      struct netlink_ext_ack *extack)
350bdcff080SVivien Didelot {
3512209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
352bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
353bdcff080SVivien Didelot 	struct switchdev_obj_port_vlan vlan;
354b7a9e0daSVladimir Oltean 	int err;
355bdcff080SVivien Didelot 
3560ee2af4eSVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
3570ee2af4eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
358c5335d73SVivien Didelot 		return 0;
3590ee2af4eSVladimir Oltean 	}
360c5335d73SVivien Didelot 
361bdcff080SVivien Didelot 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
362bdcff080SVivien Didelot 
3631ce39f0eSVladimir Oltean 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
3641ce39f0eSVladimir Oltean 	 * the same VID.
3651ce39f0eSVladimir Oltean 	 */
36636cbf39bSVladimir Oltean 	if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
3671ce39f0eSVladimir Oltean 		rcu_read_lock();
3681ce39f0eSVladimir Oltean 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
3691ce39f0eSVladimir Oltean 		rcu_read_unlock();
37031046a5fSVladimir Oltean 		if (err) {
37131046a5fSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
37231046a5fSVladimir Oltean 					   "Port already has a VLAN upper with this VID");
3731ce39f0eSVladimir Oltean 			return err;
3741ce39f0eSVladimir Oltean 		}
37531046a5fSVladimir Oltean 	}
3761ce39f0eSVladimir Oltean 
37731046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp, &vlan, extack);
378bdcff080SVivien Didelot 	if (err)
379bdcff080SVivien Didelot 		return err;
380bdcff080SVivien Didelot 
381b9499904SVivien Didelot 	/* We need the dedicated CPU port to be a member of the VLAN as well.
382b9499904SVivien Didelot 	 * Even though drivers often handle CPU membership in special ways,
383b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
384b9499904SVivien Didelot 	 */
385b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
386b9499904SVivien Didelot 
38731046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp->cpu_dp, &vlan, extack);
3887e1741b4SVivien Didelot 	if (err)
3897e1741b4SVivien Didelot 		return err;
3907e1741b4SVivien Didelot 
391b7a9e0daSVladimir Oltean 	return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid);
392bdcff080SVivien Didelot }
393bdcff080SVivien Didelot 
39469bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
395648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
39679b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
397ba14d9ebSVivien Didelot {
398d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
399ba14d9ebSVivien Didelot 	int err;
400ba14d9ebSVivien Didelot 
4010d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
4020d2cfbd4SVladimir Oltean 		return 0;
4030d2cfbd4SVladimir Oltean 
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:
412936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
41303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
41403cbb870SVladimir Oltean 
415b8e997c4SVladimir Oltean 		err = dsa_port_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4165f4dbc50SAndrew Lunn 		break;
41757d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
41803cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
41903cbb870SVladimir Oltean 			return -EOPNOTSUPP;
42003cbb870SVladimir Oltean 
4210ee2af4eSVladimir Oltean 		err = dsa_slave_vlan_add(dev, obj, extack);
42211149536SVivien Didelot 		break;
423c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
424936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
425c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
42603cbb870SVladimir Oltean 
427c595c433SHoratiu Vultur 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
428c595c433SHoratiu Vultur 		break;
429c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
430936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
431c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
43203cbb870SVladimir Oltean 
433c595c433SHoratiu Vultur 		err = dsa_port_mrp_add_ring_role(dp,
434c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
435c595c433SHoratiu Vultur 		break;
436ba14d9ebSVivien Didelot 	default:
437ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
438ba14d9ebSVivien Didelot 		break;
439ba14d9ebSVivien Didelot 	}
440ba14d9ebSVivien Didelot 
441ba14d9ebSVivien Didelot 	return err;
442ba14d9ebSVivien Didelot }
443ba14d9ebSVivien Didelot 
444bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
445bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
446bdcff080SVivien Didelot {
4472209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
448bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
4492209158cSVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
450b7a9e0daSVladimir Oltean 	int err;
451bdcff080SVivien Didelot 
45254a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
453c5335d73SVivien Didelot 		return 0;
454c5335d73SVivien Didelot 
4552209158cSVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
4562209158cSVladimir Oltean 
4577e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
4587e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
4597e1741b4SVivien Didelot 	 */
4602209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, vlan);
4612209158cSVladimir Oltean 	if (err)
4622209158cSVladimir Oltean 		return err;
4632209158cSVladimir Oltean 
464b7a9e0daSVladimir Oltean 	vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
4652209158cSVladimir Oltean 
4662209158cSVladimir Oltean 	return 0;
467bdcff080SVivien Didelot }
468bdcff080SVivien Didelot 
46969bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
470648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
471ba14d9ebSVivien Didelot {
472d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
473ba14d9ebSVivien Didelot 	int err;
474ba14d9ebSVivien Didelot 
4750d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
4760d2cfbd4SVladimir Oltean 		return 0;
4770d2cfbd4SVladimir Oltean 
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:
486936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
48703cbb870SVladimir Oltean 			return -EOPNOTSUPP;
48803cbb870SVladimir Oltean 
489b8e997c4SVladimir Oltean 		err = dsa_port_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4905f4dbc50SAndrew Lunn 		break;
49157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
49203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
49303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
49403cbb870SVladimir Oltean 
495bdcff080SVivien Didelot 		err = dsa_slave_vlan_del(dev, obj);
49611149536SVivien Didelot 		break;
497c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
498936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
499c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
50003cbb870SVladimir Oltean 
501c595c433SHoratiu Vultur 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
502c595c433SHoratiu Vultur 		break;
503c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
504936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
505c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
50603cbb870SVladimir Oltean 
507c595c433SHoratiu Vultur 		err = dsa_port_mrp_del_ring_role(dp,
508c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
509c595c433SHoratiu Vultur 		break;
510ba14d9ebSVivien Didelot 	default:
511ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
512ba14d9ebSVivien Didelot 		break;
513ba14d9ebSVivien Didelot 	}
514ba14d9ebSVivien Didelot 
515ba14d9ebSVivien Didelot 	return err;
516ba14d9ebSVivien Didelot }
517ba14d9ebSVivien Didelot 
518929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
519929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
520b73adef6SFlorian Fainelli {
521d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
522d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
523a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
524b73adef6SFlorian Fainelli 
52515b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
52615b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
52715b04aceSJiri Pirko 	 * should be removed with legacy support.
52815b04aceSJiri Pirko 	 */
52915b04aceSJiri Pirko 	if (dp->ds->devlink)
53015b04aceSJiri Pirko 		return -EOPNOTSUPP;
53115b04aceSJiri Pirko 
532929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
533929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
534929d6c14SFlorian Fainelli 
535929d6c14SFlorian Fainelli 	return 0;
536929d6c14SFlorian Fainelli }
537929d6c14SFlorian Fainelli 
5384fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
53904ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
54004ff53f9SFlorian Fainelli {
54104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
5424fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
5434fa7b718SVivien Didelot 
544f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
54504ff53f9SFlorian Fainelli #else
54604ff53f9SFlorian Fainelli 	BUG();
54704ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
548f78ed220SEric Dumazet #endif
54904ff53f9SFlorian Fainelli }
55004ff53f9SFlorian Fainelli 
55190af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
55290af1059SBrandon Streiff 				 struct sk_buff *skb)
55390af1059SBrandon Streiff {
55490af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
55590af1059SBrandon Streiff 
556cfd12c06SYangbo Lu 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
557cfd12c06SYangbo Lu 		return;
558cfd12c06SYangbo Lu 
55990af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
56090af1059SBrandon Streiff 		return;
56190af1059SBrandon Streiff 
5625c5416f5SYangbo Lu 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
56390af1059SBrandon Streiff }
56490af1059SBrandon Streiff 
56597a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
56697a69a0dSVladimir Oltean {
56797a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
56897a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
56997a69a0dSVladimir Oltean 	 */
57097a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
57197a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
57297a69a0dSVladimir Oltean 
57397a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
57497a69a0dSVladimir Oltean 	 * do not modify its EtherType
57597a69a0dSVladimir Oltean 	 */
57697a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
57797a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
57897a69a0dSVladimir Oltean 
57997a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
58097a69a0dSVladimir Oltean }
58197a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
58297a69a0dSVladimir Oltean 
583a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
584a3b0b647SVladimir Oltean {
585a3b0b647SVladimir Oltean 	int needed_headroom = dev->needed_headroom;
586a3b0b647SVladimir Oltean 	int needed_tailroom = dev->needed_tailroom;
587a3b0b647SVladimir Oltean 
588a3b0b647SVladimir Oltean 	/* For tail taggers, we need to pad short frames ourselves, to ensure
589a3b0b647SVladimir Oltean 	 * that the tail tag does not fail at its role of being at the end of
590a3b0b647SVladimir Oltean 	 * the packet, once the master interface pads the frame. Account for
591a3b0b647SVladimir Oltean 	 * that pad length here, and pad later.
592a3b0b647SVladimir Oltean 	 */
593a3b0b647SVladimir Oltean 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
594a3b0b647SVladimir Oltean 		needed_tailroom += ETH_ZLEN - skb->len;
595a3b0b647SVladimir Oltean 	/* skb_headroom() returns unsigned int... */
596a3b0b647SVladimir Oltean 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
597a3b0b647SVladimir Oltean 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
598a3b0b647SVladimir Oltean 
599a3b0b647SVladimir Oltean 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
600a3b0b647SVladimir Oltean 		/* No reallocation needed, yay! */
601a3b0b647SVladimir Oltean 		return 0;
602a3b0b647SVladimir Oltean 
603a3b0b647SVladimir Oltean 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
604a3b0b647SVladimir Oltean 				GFP_ATOMIC);
605a3b0b647SVladimir Oltean }
606a3b0b647SVladimir Oltean 
6073e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
6083e8a72d1SFlorian Fainelli {
6093e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
6104ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
6113e8a72d1SFlorian Fainelli 
6126a900628SHeiner Kallweit 	dev_sw_netstats_tx_add(dev, 1, skb->len);
6133e8a72d1SFlorian Fainelli 
614c4b364ceSYangbo Lu 	memset(skb->cb, 0, sizeof(skb->cb));
61587671375SVladimir Oltean 
616cf536ea3SYangbo Lu 	/* Handle tx timestamp if any */
61790af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
61890af1059SBrandon Streiff 
619a3b0b647SVladimir Oltean 	if (dsa_realloc_skb(skb, dev)) {
620a3b0b647SVladimir Oltean 		dev_kfree_skb_any(skb);
621a3b0b647SVladimir Oltean 		return NETDEV_TX_OK;
622a3b0b647SVladimir Oltean 	}
623a3b0b647SVladimir Oltean 
624a3b0b647SVladimir Oltean 	/* needed_tailroom should still be 'warm' in the cache line from
625a3b0b647SVladimir Oltean 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
626a3b0b647SVladimir Oltean 	 */
627a3b0b647SVladimir Oltean 	if (dev->needed_tailroom)
628a3b0b647SVladimir Oltean 		eth_skb_pad(skb);
629a3b0b647SVladimir Oltean 
630fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
631fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
632fe47d563SVivien Didelot 	 */
6334ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
634fe47d563SVivien Didelot 	if (!nskb) {
635fe47d563SVivien Didelot 		kfree_skb(skb);
6364ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
637fe47d563SVivien Didelot 	}
6385aed85ceSFlorian Fainelli 
63997a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
64097a69a0dSVladimir Oltean }
64104ff53f9SFlorian Fainelli 
64291da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
64391da11f8SLennert Buytenhek 
64491da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
64591da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
64691da11f8SLennert Buytenhek {
6477826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
6487826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
6497826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
65091da11f8SLennert Buytenhek }
65191da11f8SLennert Buytenhek 
6523d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
6533d762a0fSGuenter Roeck {
654d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
655d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6563d762a0fSGuenter Roeck 
6579d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
658d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
6593d762a0fSGuenter Roeck 
6603d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
6613d762a0fSGuenter Roeck }
6623d762a0fSGuenter Roeck 
6633d762a0fSGuenter Roeck static void
6643d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
6653d762a0fSGuenter Roeck {
666d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
667d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6683d762a0fSGuenter Roeck 
6699d490b4eSVivien Didelot 	if (ds->ops->get_regs)
670d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
6713d762a0fSGuenter Roeck }
6723d762a0fSGuenter Roeck 
673aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
674aab9c406SFlorian Fainelli {
675aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
676aab9c406SFlorian Fainelli 
677aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
678aab9c406SFlorian Fainelli }
679aab9c406SFlorian Fainelli 
6806793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
6816793abb4SGuenter Roeck {
682d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
683d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6846793abb4SGuenter Roeck 
6850e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
686ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
6876793abb4SGuenter Roeck 
6889d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
6899d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
6906793abb4SGuenter Roeck 
6916793abb4SGuenter Roeck 	return 0;
6926793abb4SGuenter Roeck }
6936793abb4SGuenter Roeck 
6946793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
6956793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6966793abb4SGuenter Roeck {
697d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
698d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6996793abb4SGuenter Roeck 
7009d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
7019d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
7026793abb4SGuenter Roeck 
7036793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7046793abb4SGuenter Roeck }
7056793abb4SGuenter Roeck 
7066793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
7076793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
7086793abb4SGuenter Roeck {
709d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
710d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7116793abb4SGuenter Roeck 
7129d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
7139d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
7146793abb4SGuenter Roeck 
7156793abb4SGuenter Roeck 	return -EOPNOTSUPP;
7166793abb4SGuenter Roeck }
7176793abb4SGuenter Roeck 
71891da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
71991da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
72091da11f8SLennert Buytenhek {
721d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
722d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
72391da11f8SLennert Buytenhek 
72491da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
72591da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
72691da11f8SLennert Buytenhek 
72791da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
72891da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
72991da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
73091da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
7319d490b4eSVivien Didelot 		if (ds->ops->get_strings)
73289f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
73389f09048SFlorian Fainelli 					     data + 4 * len);
734a71acad9SOleksij Rempel 	} else if (stringset ==  ETH_SS_TEST) {
735a71acad9SOleksij Rempel 		net_selftest_get_strings(data);
73691da11f8SLennert Buytenhek 	}
737a71acad9SOleksij Rempel 
73891da11f8SLennert Buytenhek }
73991da11f8SLennert Buytenhek 
74091da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
74191da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
74291da11f8SLennert Buytenhek 					uint64_t *data)
74391da11f8SLennert Buytenhek {
744d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
745d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7465f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
747f613ed66SFlorian Fainelli 	unsigned int start;
7485f6b4e14SFlorian Fainelli 	int i;
74991da11f8SLennert Buytenhek 
7505f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
7515f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
7525f6b4e14SFlorian Fainelli 
7536a900628SHeiner Kallweit 		s = per_cpu_ptr(dev->tstats, i);
754f613ed66SFlorian Fainelli 		do {
7555f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
7565f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
7575f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
7585f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
7595f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
7605f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
7615f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
7625f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
7635f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
7645f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
7655f6b4e14SFlorian Fainelli 	}
7669d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
767d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
76891da11f8SLennert Buytenhek }
76991da11f8SLennert Buytenhek 
77091da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
77191da11f8SLennert Buytenhek {
772d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
773d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
77491da11f8SLennert Buytenhek 
77591da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
776b94cbc90SVladimir Oltean 		int count = 0;
77791da11f8SLennert Buytenhek 
778b94cbc90SVladimir Oltean 		if (ds->ops->get_sset_count) {
779b94cbc90SVladimir Oltean 			count = ds->ops->get_sset_count(ds, dp->index, sset);
780b94cbc90SVladimir Oltean 			if (count < 0)
78191da11f8SLennert Buytenhek 				return count;
782b94cbc90SVladimir Oltean 		}
783b94cbc90SVladimir Oltean 
784b94cbc90SVladimir Oltean 		return count + 4;
785a71acad9SOleksij Rempel 	} else if (sset ==  ETH_SS_TEST) {
786a71acad9SOleksij Rempel 		return net_selftest_get_count();
78791da11f8SLennert Buytenhek 	}
78891da11f8SLennert Buytenhek 
78991da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
79091da11f8SLennert Buytenhek }
79191da11f8SLennert Buytenhek 
792487d3855SAlvin Šipraga static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
793487d3855SAlvin Šipraga 					struct ethtool_eth_phy_stats *phy_stats)
794487d3855SAlvin Šipraga {
795487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
796487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
797487d3855SAlvin Šipraga 
798487d3855SAlvin Šipraga 	if (ds->ops->get_eth_phy_stats)
799487d3855SAlvin Šipraga 		ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
800487d3855SAlvin Šipraga }
801487d3855SAlvin Šipraga 
802487d3855SAlvin Šipraga static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
803487d3855SAlvin Šipraga 					struct ethtool_eth_mac_stats *mac_stats)
804487d3855SAlvin Šipraga {
805487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
806487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
807487d3855SAlvin Šipraga 
808487d3855SAlvin Šipraga 	if (ds->ops->get_eth_mac_stats)
809487d3855SAlvin Šipraga 		ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
810487d3855SAlvin Šipraga }
811487d3855SAlvin Šipraga 
812487d3855SAlvin Šipraga static void
813487d3855SAlvin Šipraga dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
814487d3855SAlvin Šipraga 			     struct ethtool_eth_ctrl_stats *ctrl_stats)
815487d3855SAlvin Šipraga {
816487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
817487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
818487d3855SAlvin Šipraga 
819487d3855SAlvin Šipraga 	if (ds->ops->get_eth_ctrl_stats)
820487d3855SAlvin Šipraga 		ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
821487d3855SAlvin Šipraga }
822487d3855SAlvin Šipraga 
823a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev,
824a71acad9SOleksij Rempel 				   struct ethtool_test *etest, u64 *buf)
825a71acad9SOleksij Rempel {
826a71acad9SOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(ndev);
827a71acad9SOleksij Rempel 	struct dsa_switch *ds = dp->ds;
828a71acad9SOleksij Rempel 
829a71acad9SOleksij Rempel 	if (ds->ops->self_test) {
830a71acad9SOleksij Rempel 		ds->ops->self_test(ds, dp->index, etest, buf);
831a71acad9SOleksij Rempel 		return;
832a71acad9SOleksij Rempel 	}
833a71acad9SOleksij Rempel 
834a71acad9SOleksij Rempel 	net_selftest(ndev, etest, buf);
835a71acad9SOleksij Rempel }
836a71acad9SOleksij Rempel 
83719e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
83819e57c4eSFlorian Fainelli {
839d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
840d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
84119e57c4eSFlorian Fainelli 
842aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
843aab9c406SFlorian Fainelli 
8449d490b4eSVivien Didelot 	if (ds->ops->get_wol)
845d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
84619e57c4eSFlorian Fainelli }
84719e57c4eSFlorian Fainelli 
84819e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
84919e57c4eSFlorian Fainelli {
850d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
851d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
85219e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
85319e57c4eSFlorian Fainelli 
854aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
855aab9c406SFlorian Fainelli 
8569d490b4eSVivien Didelot 	if (ds->ops->set_wol)
857d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
85819e57c4eSFlorian Fainelli 
85919e57c4eSFlorian Fainelli 	return ret;
86019e57c4eSFlorian Fainelli }
86119e57c4eSFlorian Fainelli 
8627905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
8637905288fSFlorian Fainelli {
864d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
865d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8667905288fSFlorian Fainelli 	int ret;
8677905288fSFlorian Fainelli 
8687b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
86900670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8707b9cc738SVivien Didelot 		return -ENODEV;
8717b9cc738SVivien Didelot 
87208f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
8737905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8747905288fSFlorian Fainelli 
875d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
8767905288fSFlorian Fainelli 	if (ret)
8777905288fSFlorian Fainelli 		return ret;
8787905288fSFlorian Fainelli 
879aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
8807905288fSFlorian Fainelli }
8817905288fSFlorian Fainelli 
8827905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
8837905288fSFlorian Fainelli {
884d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
885d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8867905288fSFlorian Fainelli 	int ret;
8877905288fSFlorian Fainelli 
8887b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
88900670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8907b9cc738SVivien Didelot 		return -ENODEV;
8917b9cc738SVivien Didelot 
89208f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
8937905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8947905288fSFlorian Fainelli 
895d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
8967905288fSFlorian Fainelli 	if (ret)
8977905288fSFlorian Fainelli 		return ret;
8987905288fSFlorian Fainelli 
899aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
900aab9c406SFlorian Fainelli }
901aab9c406SFlorian Fainelli 
902aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
903aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
904aab9c406SFlorian Fainelli {
905aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
906aab9c406SFlorian Fainelli 
907aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
908aab9c406SFlorian Fainelli }
909aab9c406SFlorian Fainelli 
910aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
911aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
912aab9c406SFlorian Fainelli {
913aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
914aab9c406SFlorian Fainelli 
915aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
9167905288fSFlorian Fainelli }
9177905288fSFlorian Fainelli 
918a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
919a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
920a2a1a13bSHeiner Kallweit {
921a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
922a2a1a13bSHeiner Kallweit 
923a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
924a2a1a13bSHeiner Kallweit }
925a2a1a13bSHeiner Kallweit 
926a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
927a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
928a2a1a13bSHeiner Kallweit {
929a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
930a2a1a13bSHeiner Kallweit 
931a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
932a2a1a13bSHeiner Kallweit }
933a2a1a13bSHeiner Kallweit 
93404ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
93504ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
93604ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
93704ff53f9SFlorian Fainelli {
938d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
93904ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
94004ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
94104ff53f9SFlorian Fainelli 	int err = 0;
94204ff53f9SFlorian Fainelli 
94304ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
94404ff53f9SFlorian Fainelli 	if (!netpoll)
94504ff53f9SFlorian Fainelli 		return -ENOMEM;
94604ff53f9SFlorian Fainelli 
94704ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
94804ff53f9SFlorian Fainelli 	if (err) {
94904ff53f9SFlorian Fainelli 		kfree(netpoll);
95004ff53f9SFlorian Fainelli 		goto out;
95104ff53f9SFlorian Fainelli 	}
95204ff53f9SFlorian Fainelli 
95304ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
95404ff53f9SFlorian Fainelli out:
95504ff53f9SFlorian Fainelli 	return err;
95604ff53f9SFlorian Fainelli }
95704ff53f9SFlorian Fainelli 
95804ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
95904ff53f9SFlorian Fainelli {
96004ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
96104ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
96204ff53f9SFlorian Fainelli 
96304ff53f9SFlorian Fainelli 	if (!netpoll)
96404ff53f9SFlorian Fainelli 		return;
96504ff53f9SFlorian Fainelli 
96604ff53f9SFlorian Fainelli 	p->netpoll = NULL;
96704ff53f9SFlorian Fainelli 
968c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
96904ff53f9SFlorian Fainelli }
97004ff53f9SFlorian Fainelli 
97104ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
97204ff53f9SFlorian Fainelli {
97304ff53f9SFlorian Fainelli }
97404ff53f9SFlorian Fainelli #endif
97504ff53f9SFlorian Fainelli 
97644bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
97744bb765cSFlorian Fainelli 					char *name, size_t len)
97844bb765cSFlorian Fainelli {
979d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
98044bb765cSFlorian Fainelli 
981d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
982d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
983d484210bSJiri Pirko 	 * should be removed with legacy support.
984d484210bSJiri Pirko 	 */
985d484210bSJiri Pirko 	if (dp->ds->devlink)
986d484210bSJiri Pirko 		return -EOPNOTSUPP;
987d484210bSJiri Pirko 
988d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
98944bb765cSFlorian Fainelli 		return -EINVAL;
9903a543ef4SFlorian Fainelli 
9913a543ef4SFlorian Fainelli 	return 0;
9923a543ef4SFlorian Fainelli }
9933a543ef4SFlorian Fainelli 
994f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
9954fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
996f50f2127SFlorian Fainelli {
9974fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
998f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
999f50f2127SFlorian Fainelli 
1000f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1001f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1002f50f2127SFlorian Fainelli 			return mall_tc_entry;
1003f50f2127SFlorian Fainelli 
1004f50f2127SFlorian Fainelli 	return NULL;
1005f50f2127SFlorian Fainelli }
1006f50f2127SFlorian Fainelli 
1007e13c2075SVladimir Oltean static int
1008e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1009f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
1010f50f2127SFlorian Fainelli 				  bool ingress)
1011f50f2127SFlorian Fainelli {
1012d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1013f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1014e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
1015f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1016d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
10179681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
1018d945097bSVivien Didelot 	struct dsa_port *to_dp;
1019e13c2075SVladimir Oltean 	int err;
1020e13c2075SVladimir Oltean 
1021f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
102234297176SVladimir Oltean 		return -EOPNOTSUPP;
1023f50f2127SFlorian Fainelli 
102453eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1025319a1d19SJiri Pirko 					      cls->common.extack))
102634297176SVladimir Oltean 		return -EOPNOTSUPP;
1027319a1d19SJiri Pirko 
10289681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
1029f50f2127SFlorian Fainelli 
103065722159SVladimir Oltean 	if (!act->dev)
103165722159SVladimir Oltean 		return -EINVAL;
103265722159SVladimir Oltean 
10339681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
1034f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
1035f50f2127SFlorian Fainelli 
1036f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1037f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1038f50f2127SFlorian Fainelli 		return -ENOMEM;
1039f50f2127SFlorian Fainelli 
1040f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
1041f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1042f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
1043f50f2127SFlorian Fainelli 
10449681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
1045f50f2127SFlorian Fainelli 
1046d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
1047f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
1048f50f2127SFlorian Fainelli 
1049d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1050f50f2127SFlorian Fainelli 	if (err) {
1051f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
1052f50f2127SFlorian Fainelli 		return err;
1053f50f2127SFlorian Fainelli 	}
1054f50f2127SFlorian Fainelli 
1055f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1056e13c2075SVladimir Oltean 
1057e13c2075SVladimir Oltean 	return err;
1058f50f2127SFlorian Fainelli }
1059f50f2127SFlorian Fainelli 
106034297176SVladimir Oltean static int
106134297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
106234297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
106334297176SVladimir Oltean 				  bool ingress)
106434297176SVladimir Oltean {
106534297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
106634297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
106734297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
106834297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
106934297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
107034297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
107134297176SVladimir Oltean 	struct flow_action_entry *act;
107234297176SVladimir Oltean 	int err;
107334297176SVladimir Oltean 
107434297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
107534297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1076c75a33c8SJacob Keller 				   "Policing offload not implemented");
107734297176SVladimir Oltean 		return -EOPNOTSUPP;
107834297176SVladimir Oltean 	}
107934297176SVladimir Oltean 
108034297176SVladimir Oltean 	if (!ingress) {
108134297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1082c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
108334297176SVladimir Oltean 		return -EOPNOTSUPP;
108434297176SVladimir Oltean 	}
108534297176SVladimir Oltean 
108634297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
108734297176SVladimir Oltean 					      cls->common.extack))
108834297176SVladimir Oltean 		return -EOPNOTSUPP;
108934297176SVladimir Oltean 
109034297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
109134297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
109234297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1093c75a33c8SJacob Keller 					   "Only one port policer allowed");
109434297176SVladimir Oltean 			return -EEXIST;
109534297176SVladimir Oltean 		}
109634297176SVladimir Oltean 	}
109734297176SVladimir Oltean 
109834297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
109934297176SVladimir Oltean 
110034297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
110134297176SVladimir Oltean 	if (!mall_tc_entry)
110234297176SVladimir Oltean 		return -ENOMEM;
110334297176SVladimir Oltean 
110434297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
110534297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
110634297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
110734297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
110834297176SVladimir Oltean 	policer->burst = act->police.burst;
110934297176SVladimir Oltean 
111034297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
111134297176SVladimir Oltean 	if (err) {
111234297176SVladimir Oltean 		kfree(mall_tc_entry);
111334297176SVladimir Oltean 		return err;
111434297176SVladimir Oltean 	}
111534297176SVladimir Oltean 
111634297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
111734297176SVladimir Oltean 
111834297176SVladimir Oltean 	return err;
111934297176SVladimir Oltean }
112034297176SVladimir Oltean 
1121e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1122e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1123e13c2075SVladimir Oltean 				      bool ingress)
1124e13c2075SVladimir Oltean {
1125e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1126e13c2075SVladimir Oltean 
1127e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1128e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1129e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1130e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
113134297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
113234297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
113334297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1134e13c2075SVladimir Oltean 
1135e13c2075SVladimir Oltean 	return err;
1136f50f2127SFlorian Fainelli }
1137f50f2127SFlorian Fainelli 
1138f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1139f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1140f50f2127SFlorian Fainelli {
1141d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1142f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1143d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1144f50f2127SFlorian Fainelli 
11454fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1146f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1147f50f2127SFlorian Fainelli 		return;
1148f50f2127SFlorian Fainelli 
1149f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1150f50f2127SFlorian Fainelli 
1151f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1152f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
115334297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
115434297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
115534297176SVladimir Oltean 						 &mall_tc_entry->mirror);
115634297176SVladimir Oltean 		break;
115734297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
115834297176SVladimir Oltean 		if (ds->ops->port_policer_del)
115934297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1160f50f2127SFlorian Fainelli 		break;
1161f50f2127SFlorian Fainelli 	default:
1162f50f2127SFlorian Fainelli 		WARN_ON(1);
1163f50f2127SFlorian Fainelli 	}
1164f50f2127SFlorian Fainelli 
1165f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1166f50f2127SFlorian Fainelli }
1167f50f2127SFlorian Fainelli 
11683fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
11696b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
11706b3eb752SJiri Pirko 					   bool ingress)
1171f50f2127SFlorian Fainelli {
11725fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1173a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1174f50f2127SFlorian Fainelli 
11753fbae382SJiri Pirko 	switch (cls->command) {
11763fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
11775fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
11783fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
11793fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
11803fbae382SJiri Pirko 		return 0;
11813fbae382SJiri Pirko 	default:
11823fbae382SJiri Pirko 		return -EOPNOTSUPP;
11833fbae382SJiri Pirko 	}
11843fbae382SJiri Pirko }
11853fbae382SJiri Pirko 
1186ed11bb1fSVladimir Oltean static int dsa_slave_add_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_add)
1195ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1196ed11bb1fSVladimir Oltean 
1197ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1198ed11bb1fSVladimir Oltean }
1199ed11bb1fSVladimir Oltean 
1200ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1201ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1202ed11bb1fSVladimir Oltean 				    bool ingress)
1203ed11bb1fSVladimir Oltean {
1204ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1205ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1206ed11bb1fSVladimir Oltean 	int port = dp->index;
1207ed11bb1fSVladimir Oltean 
1208ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1209ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1210ed11bb1fSVladimir Oltean 
1211ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1212ed11bb1fSVladimir Oltean }
1213ed11bb1fSVladimir Oltean 
1214ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1215ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1216ed11bb1fSVladimir Oltean 				      bool ingress)
1217ed11bb1fSVladimir Oltean {
1218ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1219ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1220ed11bb1fSVladimir Oltean 	int port = dp->index;
1221ed11bb1fSVladimir Oltean 
1222ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1223ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1224ed11bb1fSVladimir Oltean 
1225ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1226ed11bb1fSVladimir Oltean }
1227ed11bb1fSVladimir Oltean 
1228ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1229ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1230ed11bb1fSVladimir Oltean 					 bool ingress)
1231ed11bb1fSVladimir Oltean {
1232ed11bb1fSVladimir Oltean 	switch (cls->command) {
1233ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1234ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1235ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1236ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1237ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1238ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1239ed11bb1fSVladimir Oltean 	default:
1240ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1241ed11bb1fSVladimir Oltean 	}
1242ed11bb1fSVladimir Oltean }
1243ed11bb1fSVladimir Oltean 
12446b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12456b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
12466b3eb752SJiri Pirko {
12476b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
12486b3eb752SJiri Pirko 
124944ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
125044ae12a7SJiri Pirko 		return -EOPNOTSUPP;
125144ae12a7SJiri Pirko 
12526b3eb752SJiri Pirko 	switch (type) {
12536b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
12546b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1255ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1256ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
12576b3eb752SJiri Pirko 	default:
12586b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12596b3eb752SJiri Pirko 	}
12606b3eb752SJiri Pirko }
12616b3eb752SJiri Pirko 
12626b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
12636b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12646b3eb752SJiri Pirko {
12656b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
12666b3eb752SJiri Pirko }
12676b3eb752SJiri Pirko 
12686b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
12696b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12706b3eb752SJiri Pirko {
12716b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
12726b3eb752SJiri Pirko }
12736b3eb752SJiri Pirko 
1274955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1275955bcb6eSPablo Neira Ayuso 
12766b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1277955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
12786b3eb752SJiri Pirko {
1279955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1280a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
12816b3eb752SJiri Pirko 
128232f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
12836b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
128432f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
12856b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
12866b3eb752SJiri Pirko 	else
12876b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12886b3eb752SJiri Pirko 
1289955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1290955bcb6eSPablo Neira Ayuso 
12916b3eb752SJiri Pirko 	switch (f->command) {
12929c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
12930d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
12940d4fd02eSPablo Neira Ayuso 			return -EBUSY;
12950d4fd02eSPablo Neira Ayuso 
12960c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1297955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1298955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1299955bcb6eSPablo Neira Ayuso 
1300955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1301955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1302955bcb6eSPablo Neira Ayuso 		return 0;
13039c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
130414bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1305955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1306955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1307955bcb6eSPablo Neira Ayuso 
1308955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1309955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
13106b3eb752SJiri Pirko 		return 0;
13116b3eb752SJiri Pirko 	default:
13126b3eb752SJiri Pirko 		return -EOPNOTSUPP;
13136b3eb752SJiri Pirko 	}
13146b3eb752SJiri Pirko }
13156b3eb752SJiri Pirko 
13163fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
13173fb24a43SPablo Neira Ayuso 				    void *type_data)
13183fb24a43SPablo Neira Ayuso {
13193fb24a43SPablo Neira Ayuso 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
13203fb24a43SPablo Neira Ayuso 	struct net_device *master = cpu_dp->master;
13213fb24a43SPablo Neira Ayuso 
13223fb24a43SPablo Neira Ayuso 	if (!master->netdev_ops->ndo_setup_tc)
13233fb24a43SPablo Neira Ayuso 		return -EOPNOTSUPP;
13243fb24a43SPablo Neira Ayuso 
13253fb24a43SPablo Neira Ayuso 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
13263fb24a43SPablo Neira Ayuso }
13273fb24a43SPablo Neira Ayuso 
13283fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1329de4784caSJiri Pirko 			      void *type_data)
13303fbae382SJiri Pirko {
133147d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
133247d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
133347d23af2SVladimir Oltean 
13343fb24a43SPablo Neira Ayuso 	switch (type) {
13353fb24a43SPablo Neira Ayuso 	case TC_SETUP_BLOCK:
13366b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
13373fb24a43SPablo Neira Ayuso 	case TC_SETUP_FT:
13383fb24a43SPablo Neira Ayuso 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
13393fb24a43SPablo Neira Ayuso 	default:
13403fb24a43SPablo Neira Ayuso 		break;
13413fb24a43SPablo Neira Ayuso 	}
134247d23af2SVladimir Oltean 
134347d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1344a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
134547d23af2SVladimir Oltean 
134647d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1347f50f2127SFlorian Fainelli }
1348f50f2127SFlorian Fainelli 
1349bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1350bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1351bf9f2648SFlorian Fainelli {
1352d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1353d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1354bf9f2648SFlorian Fainelli 
1355bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1356bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1357bf9f2648SFlorian Fainelli 
1358d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1359bf9f2648SFlorian Fainelli }
1360bf9f2648SFlorian Fainelli 
1361bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1362bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1363bf9f2648SFlorian Fainelli {
1364d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1365d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1366bf9f2648SFlorian Fainelli 
1367bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1368bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1369bf9f2648SFlorian Fainelli 
1370d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1371bf9f2648SFlorian Fainelli }
1372bf9f2648SFlorian Fainelli 
13730336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
13740336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
13750336369dSBrandon Streiff {
13760336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
13770336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
13780336369dSBrandon Streiff 
13790336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
13800336369dSBrandon Streiff 		return -EOPNOTSUPP;
13810336369dSBrandon Streiff 
13820336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
13830336369dSBrandon Streiff }
13840336369dSBrandon Streiff 
1385061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1386061f6a50SFlorian Fainelli 				     u16 vid)
1387061f6a50SFlorian Fainelli {
13882209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1389061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
139088236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
139188236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1392b7a9e0daSVladimir Oltean 		.vid = vid,
139388236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
139488236591SVladimir Oltean 		.flags = 0,
139588236591SVladimir Oltean 	};
139631046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1397061f6a50SFlorian Fainelli 	int ret;
1398061f6a50SFlorian Fainelli 
139988236591SVladimir Oltean 	/* User port... */
140031046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
140131046a5fSVladimir Oltean 	if (ret) {
140231046a5fSVladimir Oltean 		if (extack._msg)
140331046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
140488236591SVladimir Oltean 		return ret;
140531046a5fSVladimir Oltean 	}
140688236591SVladimir Oltean 
140788236591SVladimir Oltean 	/* And CPU port... */
140831046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
140931046a5fSVladimir Oltean 	if (ret) {
141031046a5fSVladimir Oltean 		if (extack._msg)
141131046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
141231046a5fSVladimir Oltean 				   extack._msg);
14137e1741b4SVivien Didelot 		return ret;
141431046a5fSVladimir Oltean 	}
14157e1741b4SVivien Didelot 
14162209158cSVladimir Oltean 	return vlan_vid_add(master, proto, vid);
1417061f6a50SFlorian Fainelli }
1418061f6a50SFlorian Fainelli 
1419061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1420061f6a50SFlorian Fainelli 				      u16 vid)
1421061f6a50SFlorian Fainelli {
14222209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1423061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
142488236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1425b7a9e0daSVladimir Oltean 		.vid = vid,
142688236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
142788236591SVladimir Oltean 		.flags = 0,
142888236591SVladimir Oltean 	};
14292209158cSVladimir Oltean 	int err;
1430061f6a50SFlorian Fainelli 
14317e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
14327e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
14337e1741b4SVivien Didelot 	 */
14342209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
14352209158cSVladimir Oltean 	if (err)
14362209158cSVladimir Oltean 		return err;
14372209158cSVladimir Oltean 
14382209158cSVladimir Oltean 	vlan_vid_del(master, proto, vid);
14392209158cSVladimir Oltean 
14402209158cSVladimir Oltean 	return 0;
1441061f6a50SFlorian Fainelli }
1442061f6a50SFlorian Fainelli 
144306cfb2dfSVladimir Oltean static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
144406cfb2dfSVladimir Oltean {
144506cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
144606cfb2dfSVladimir Oltean 
144706cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
144806cfb2dfSVladimir Oltean }
144906cfb2dfSVladimir Oltean 
145006cfb2dfSVladimir Oltean static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
145106cfb2dfSVladimir Oltean {
145206cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
145306cfb2dfSVladimir Oltean 
145406cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
145506cfb2dfSVladimir Oltean }
145606cfb2dfSVladimir Oltean 
145706cfb2dfSVladimir Oltean /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
145806cfb2dfSVladimir Oltean  * filtering is enabled. The baseline is that only ports that offload a
145906cfb2dfSVladimir Oltean  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
146006cfb2dfSVladimir Oltean  * but there are exceptions for quirky hardware.
146106cfb2dfSVladimir Oltean  *
146206cfb2dfSVladimir Oltean  * If ds->vlan_filtering_is_global = true, then standalone ports which share
146306cfb2dfSVladimir Oltean  * the same switch with other ports that offload a VLAN-aware bridge are also
146406cfb2dfSVladimir Oltean  * inevitably VLAN-aware.
146506cfb2dfSVladimir Oltean  *
146606cfb2dfSVladimir Oltean  * To summarize, a DSA switch port offloads:
146706cfb2dfSVladimir Oltean  *
146806cfb2dfSVladimir Oltean  * - If standalone (this includes software bridge, software LAG):
146958adf9dcSVladimir Oltean  *     - if ds->needs_standalone_vlan_filtering = true, OR if
147058adf9dcSVladimir Oltean  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
147158adf9dcSVladimir Oltean  *       this switch chip which have vlan_filtering=1)
147206cfb2dfSVladimir Oltean  *         - the 8021q upper VLANs
147358adf9dcSVladimir Oltean  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
147458adf9dcSVladimir Oltean  *       global, or it is, but no port is under a VLAN-aware bridge):
147506cfb2dfSVladimir Oltean  *         - no VLAN (any 8021q upper is a software VLAN)
147606cfb2dfSVladimir Oltean  *
147706cfb2dfSVladimir Oltean  * - If under a vlan_filtering=0 bridge which it offload:
147806cfb2dfSVladimir Oltean  *     - if ds->configure_vlan_while_not_filtering = true (default):
147906cfb2dfSVladimir Oltean  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
148006cfb2dfSVladimir Oltean  *     - else (deprecated):
148106cfb2dfSVladimir Oltean  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
148206cfb2dfSVladimir Oltean  *           enabled, so this behavior is broken and discouraged.
148306cfb2dfSVladimir Oltean  *
148406cfb2dfSVladimir Oltean  * - If under a vlan_filtering=1 bridge which it offload:
148506cfb2dfSVladimir Oltean  *     - the bridge VLANs
148606cfb2dfSVladimir Oltean  *     - the 8021q upper VLANs
148706cfb2dfSVladimir Oltean  */
148806cfb2dfSVladimir Oltean int dsa_slave_manage_vlan_filtering(struct net_device *slave,
148906cfb2dfSVladimir Oltean 				    bool vlan_filtering)
149006cfb2dfSVladimir Oltean {
149106cfb2dfSVladimir Oltean 	int err;
149206cfb2dfSVladimir Oltean 
149306cfb2dfSVladimir Oltean 	if (vlan_filtering) {
149406cfb2dfSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
149506cfb2dfSVladimir Oltean 
149606cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
149706cfb2dfSVladimir Oltean 		if (err) {
149806cfb2dfSVladimir Oltean 			vlan_for_each(slave, dsa_slave_clear_vlan, slave);
149906cfb2dfSVladimir Oltean 			slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
150006cfb2dfSVladimir Oltean 			return err;
150106cfb2dfSVladimir Oltean 		}
150206cfb2dfSVladimir Oltean 	} else {
150306cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
150406cfb2dfSVladimir Oltean 		if (err)
150506cfb2dfSVladimir Oltean 			return err;
150606cfb2dfSVladimir Oltean 
150706cfb2dfSVladimir Oltean 		slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
150806cfb2dfSVladimir Oltean 	}
150906cfb2dfSVladimir Oltean 
151006cfb2dfSVladimir Oltean 	return 0;
151106cfb2dfSVladimir Oltean }
151206cfb2dfSVladimir Oltean 
1513bff33f7eSVladimir Oltean struct dsa_hw_port {
1514bff33f7eSVladimir Oltean 	struct list_head list;
1515bff33f7eSVladimir Oltean 	struct net_device *dev;
1516bff33f7eSVladimir Oltean 	int old_mtu;
1517bff33f7eSVladimir Oltean };
1518bff33f7eSVladimir Oltean 
1519bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1520bff33f7eSVladimir Oltean {
1521bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1522bff33f7eSVladimir Oltean 	int err;
1523bff33f7eSVladimir Oltean 
1524bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1525bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1526bff33f7eSVladimir Oltean 			continue;
1527bff33f7eSVladimir Oltean 
1528bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1529bff33f7eSVladimir Oltean 		if (err)
1530bff33f7eSVladimir Oltean 			goto rollback;
1531bff33f7eSVladimir Oltean 	}
1532bff33f7eSVladimir Oltean 
1533bff33f7eSVladimir Oltean 	return 0;
1534bff33f7eSVladimir Oltean 
1535bff33f7eSVladimir Oltean rollback:
1536bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1537bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1538bff33f7eSVladimir Oltean 			continue;
1539bff33f7eSVladimir Oltean 
1540bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1541bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1542bff33f7eSVladimir Oltean 	}
1543bff33f7eSVladimir Oltean 
1544bff33f7eSVladimir Oltean 	return err;
1545bff33f7eSVladimir Oltean }
1546bff33f7eSVladimir Oltean 
1547bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1548bff33f7eSVladimir Oltean {
1549bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1550bff33f7eSVladimir Oltean 
1551bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1552bff33f7eSVladimir Oltean 		kfree(p);
1553bff33f7eSVladimir Oltean }
1554bff33f7eSVladimir Oltean 
1555bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1556bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1557bff33f7eSVladimir Oltean {
1558bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1559bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1560bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1561bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1562bff33f7eSVladimir Oltean 	int err;
1563bff33f7eSVladimir Oltean 
1564bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1565bff33f7eSVladimir Oltean 		return;
1566bff33f7eSVladimir Oltean 
1567d3eed0e5SVladimir Oltean 	if (!dp->bridge)
1568bff33f7eSVladimir Oltean 		return;
1569bff33f7eSVladimir Oltean 
1570bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1571bff33f7eSVladimir Oltean 
1572bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1573bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1574bff33f7eSVladimir Oltean 	 */
1575bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1576bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1577bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1578bff33f7eSVladimir Oltean 			struct net_device *slave;
1579bff33f7eSVladimir Oltean 
1580bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1581bff33f7eSVladimir Oltean 				continue;
1582bff33f7eSVladimir Oltean 
158336cbf39bSVladimir Oltean 			if (!dsa_port_bridge_same(dp, other_dp))
1584bff33f7eSVladimir Oltean 				continue;
1585bff33f7eSVladimir Oltean 
1586bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1587bff33f7eSVladimir Oltean 				continue;
1588bff33f7eSVladimir Oltean 
1589bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1590bff33f7eSVladimir Oltean 
1591bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1592bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1593bff33f7eSVladimir Oltean 
1594bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1595bff33f7eSVladimir Oltean 			if (!hw_port)
1596bff33f7eSVladimir Oltean 				goto out;
1597bff33f7eSVladimir Oltean 
1598bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1599bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1600bff33f7eSVladimir Oltean 
1601bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1602bff33f7eSVladimir Oltean 		}
1603bff33f7eSVladimir Oltean 	}
1604bff33f7eSVladimir Oltean 
1605bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1606bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1607bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1608bff33f7eSVladimir Oltean 	 */
1609bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1610bff33f7eSVladimir Oltean 	if (!err)
1611bff33f7eSVladimir Oltean 		goto out;
1612bff33f7eSVladimir Oltean 
1613bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1614bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1615bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1616bff33f7eSVladimir Oltean 	 */
1617bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1618bff33f7eSVladimir Oltean 
1619bff33f7eSVladimir Oltean out:
1620bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1621bff33f7eSVladimir Oltean }
1622bff33f7eSVladimir Oltean 
162353da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1624bfcb8132SVladimir Oltean {
1625bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1626bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1627bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1628bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
16294e4ab795SVladimir Oltean 	struct dsa_port *dp_iter;
1630bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1631bfcb8132SVladimir Oltean 	int port = p->dp->index;
1632bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1633bfcb8132SVladimir Oltean 	int new_master_mtu;
1634bfcb8132SVladimir Oltean 	int old_master_mtu;
1635bfcb8132SVladimir Oltean 	int mtu_limit;
1636bfcb8132SVladimir Oltean 	int cpu_mtu;
16374e4ab795SVladimir Oltean 	int err;
1638bfcb8132SVladimir Oltean 
1639bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1640bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1641bfcb8132SVladimir Oltean 
16424e4ab795SVladimir Oltean 	list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1643bfcb8132SVladimir Oltean 		int slave_mtu;
1644bfcb8132SVladimir Oltean 
16454e4ab795SVladimir Oltean 		if (!dsa_port_is_user(dp_iter))
1646bfcb8132SVladimir Oltean 			continue;
1647bfcb8132SVladimir Oltean 
1648bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1649bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1650bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1651bfcb8132SVladimir Oltean 		 */
16524e4ab795SVladimir Oltean 		if (!dp_iter->slave)
1653bfcb8132SVladimir Oltean 			continue;
1654bfcb8132SVladimir Oltean 
1655bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1656bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1657bfcb8132SVladimir Oltean 		 */
16584e4ab795SVladimir Oltean 		if (dp_iter == dp)
1659bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1660bfcb8132SVladimir Oltean 		else
16614e4ab795SVladimir Oltean 			slave_mtu = dp_iter->slave->mtu;
1662bfcb8132SVladimir Oltean 
1663bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1664bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1665bfcb8132SVladimir Oltean 	}
1666bfcb8132SVladimir Oltean 
1667bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1668bfcb8132SVladimir Oltean 
1669bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1670bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
16714e500251SVladimir Oltean 	new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1672bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1673bfcb8132SVladimir Oltean 		return -ERANGE;
1674bfcb8132SVladimir Oltean 
1675bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1676bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1677bfcb8132SVladimir Oltean 	 */
1678bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1679bfcb8132SVladimir Oltean 
1680bfcb8132SVladimir Oltean 	/* Start applying stuff */
1681bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1682bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1683bfcb8132SVladimir Oltean 		if (err < 0)
1684bfcb8132SVladimir Oltean 			goto out_master_failed;
1685bfcb8132SVladimir Oltean 
1686bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
168788faba20SVladimir Oltean 		 * upstream switches, so create a non-targeted notifier which
168888faba20SVladimir Oltean 		 * updates all switches.
1689bfcb8132SVladimir Oltean 		 */
169088faba20SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1691bfcb8132SVladimir Oltean 		if (err)
1692bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1693bfcb8132SVladimir Oltean 	}
1694bfcb8132SVladimir Oltean 
169588faba20SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, true);
1696bfcb8132SVladimir Oltean 	if (err)
1697bfcb8132SVladimir Oltean 		goto out_port_failed;
1698bfcb8132SVladimir Oltean 
1699bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1700bfcb8132SVladimir Oltean 
1701bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1702bff33f7eSVladimir Oltean 
1703bfcb8132SVladimir Oltean 	return 0;
1704bfcb8132SVladimir Oltean 
1705bfcb8132SVladimir Oltean out_port_failed:
1706bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1707bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
17084e500251SVladimir Oltean 				    dsa_tag_protocol_overhead(cpu_dp->tag_ops),
170988faba20SVladimir Oltean 				    false);
1710bfcb8132SVladimir Oltean out_cpu_failed:
1711bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1712bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1713bfcb8132SVladimir Oltean out_master_failed:
1714bfcb8132SVladimir Oltean 	return err;
1715bfcb8132SVladimir Oltean }
1716bfcb8132SVladimir Oltean 
171791da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
171891da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
17193d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
17203d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1721aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1722c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
17236793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
17246793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
17256793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
172691da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
172791da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
172891da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
1729487d3855SAlvin Šipraga 	.get_eth_phy_stats	= dsa_slave_get_eth_phy_stats,
1730487d3855SAlvin Šipraga 	.get_eth_mac_stats	= dsa_slave_get_eth_mac_stats,
1731487d3855SAlvin Šipraga 	.get_eth_ctrl_stats	= dsa_slave_get_eth_ctrl_stats,
173219e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
173319e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
17347905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
17357905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1736aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1737aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1738a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1739a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1740bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1741bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
17420336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
1743a71acad9SOleksij Rempel 	.self_test		= dsa_slave_net_selftest,
174491da11f8SLennert Buytenhek };
174591da11f8SLennert Buytenhek 
1746716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1747716efee2SJiri Pirko {
1748716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1749716efee2SJiri Pirko 
1750716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1751716efee2SJiri Pirko }
1752716efee2SJiri Pirko 
1753c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
1754c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
1755c2ec5f2eSOleksij Rempel {
1756c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
1757c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1758c2ec5f2eSOleksij Rempel 
1759c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
1760c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
1761c2ec5f2eSOleksij Rempel 	else
1762c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
1763c2ec5f2eSOleksij Rempel }
1764c2ec5f2eSOleksij Rempel 
17650994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
17660994d492SFelix Fietkau 				       struct net_device_path *path)
17670994d492SFelix Fietkau {
17680994d492SFelix Fietkau 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
17690994d492SFelix Fietkau 	struct dsa_port *cpu_dp = dp->cpu_dp;
17700994d492SFelix Fietkau 
17710994d492SFelix Fietkau 	path->dev = ctx->dev;
17720994d492SFelix Fietkau 	path->type = DEV_PATH_DSA;
17730994d492SFelix Fietkau 	path->dsa.proto = cpu_dp->tag_ops->proto;
17740994d492SFelix Fietkau 	path->dsa.port = dp->index;
17750994d492SFelix Fietkau 	ctx->dev = cpu_dp->master;
17760994d492SFelix Fietkau 
17770994d492SFelix Fietkau 	return 0;
17780994d492SFelix Fietkau }
17790994d492SFelix Fietkau 
17803e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1781d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1782d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
17833e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1784d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1785d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1786d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
17872bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1788a7605370SArnd Bergmann 	.ndo_eth_ioctl		= dsa_slave_ioctl,
1789abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
179004ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
179104ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
179204ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
179304ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
179404ff53f9SFlorian Fainelli #endif
179544bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1796f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1797c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
1798929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1799061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1800061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1801716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1802bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
18030994d492SFelix Fietkau 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
180498237d43SScott Feldman };
180598237d43SScott Feldman 
1806f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1807f37db85dSFlorian Fainelli 	.name	= "dsa",
1808f37db85dSFlorian Fainelli };
1809f37db85dSFlorian Fainelli 
1810aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1811aab9c406SFlorian Fainelli {
1812aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1813aab9c406SFlorian Fainelli 
1814765bda93SRussell King 	if (dp->pl)
1815aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1816aab9c406SFlorian Fainelli }
1817aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1818aab9c406SFlorian Fainelli 
18195c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1820aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1821aab9c406SFlorian Fainelli {
18225c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1823aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1824aab9c406SFlorian Fainelli 
1825aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1826aab9c406SFlorian Fainelli 	 * not be called if it was not.
1827aab9c406SFlorian Fainelli 	 */
1828aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1829ce31b31cSFlorian Fainelli }
1830ce31b31cSFlorian Fainelli 
183191da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1832c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
1833c916e8e1SOleksij Rempel 				 u32 flags)
1834c305c165SFlorian Fainelli {
1835d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1836d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1837c305c165SFlorian Fainelli 
18380115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
18390115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1840d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1841c305c165SFlorian Fainelli 		return -ENODEV;
1842d25b8e74SRussell King 	}
1843c305c165SFlorian Fainelli 
1844c916e8e1SOleksij Rempel 	slave_dev->phydev->dev_flags |= flags;
1845c916e8e1SOleksij Rempel 
1846aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1847c305c165SFlorian Fainelli }
1848c305c165SFlorian Fainelli 
18494fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
18500d8bcdd3SFlorian Fainelli {
1851d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1852d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1853d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
18546819563eSFlorian Fainelli 	u32 phy_flags = 0;
18550c65b2b9SAndrew Lunn 	int ret;
18560d8bcdd3SFlorian Fainelli 
185744cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
185844cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
185944cc27e4SIoana Ciornei 
18605c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
18615c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
18625c05c1dbSRussell King 	 * this if the switch provides such a callback.
18635c05c1dbSRussell King 	 */
18645c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
18655c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
18665c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
18675c05c1dbSRussell King 	}
18685c05c1dbSRussell King 
186921bd64bdSRussell King (Oracle) 	ret = dsa_port_phylink_create(dp);
187021bd64bdSRussell King (Oracle) 	if (ret)
187121bd64bdSRussell King (Oracle) 		return ret;
1872aab9c406SFlorian Fainelli 
18739d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1874d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
18756819563eSFlorian Fainelli 
1876aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
18776146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
18786146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
18796146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
18800d8bcdd3SFlorian Fainelli 		 */
1881c916e8e1SOleksij Rempel 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1882d25b8e74SRussell King 	}
18836a52e733SVladimir Oltean 	if (ret) {
18846a52e733SVladimir Oltean 		netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
18856a52e733SVladimir Oltean 			   ERR_PTR(ret));
18866a52e733SVladimir Oltean 		phylink_destroy(dp->pl);
18870d8bcdd3SFlorian Fainelli 	}
18889697f1cdSFlorian Fainelli 
18896146dd45SVladimir Oltean 	return ret;
1890b31f65fbSAndrew Lunn }
18910d8bcdd3SFlorian Fainelli 
189253da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
189353da0ebaSVladimir Oltean {
189453da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
189553da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
189653da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
189753da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
189858adf9dcSVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
189953da0ebaSVladimir Oltean 
19004e500251SVladimir Oltean 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
19014e500251SVladimir Oltean 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
190253da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
190353da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
190453da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
190553da0ebaSVladimir Oltean 	 */
190653da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
190753da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
190853da0ebaSVladimir Oltean 
190953da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
191021cf377aSLino Sanfilippo 
191121cf377aSLino Sanfilippo 	slave->features = master->vlan_features | NETIF_F_HW_TC;
191221cf377aSLino Sanfilippo 	slave->hw_features |= NETIF_F_HW_TC;
191321cf377aSLino Sanfilippo 	slave->features |= NETIF_F_LLTX;
191421cf377aSLino Sanfilippo 	if (slave->needed_tailroom)
191521cf377aSLino Sanfilippo 		slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
191658adf9dcSVladimir Oltean 	if (ds->needs_standalone_vlan_filtering)
191758adf9dcSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
191853da0ebaSVladimir Oltean }
191953da0ebaSVladimir Oltean 
19201a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
19211a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
19221a33e10eSCong Wang 					    struct netdev_queue *txq,
19231a33e10eSCong Wang 					    void *_unused)
19241a33e10eSCong Wang {
19251a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
19261a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
19271a33e10eSCong Wang }
19281a33e10eSCong Wang 
192924462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
193024462549SFlorian Fainelli {
1931aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
193224462549SFlorian Fainelli 
1933a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1934a94c689eSFlorian Fainelli 		return 0;
1935a94c689eSFlorian Fainelli 
1936f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1937f154be24SFlorian Fainelli 
1938aab9c406SFlorian Fainelli 	rtnl_lock();
1939aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1940aab9c406SFlorian Fainelli 	rtnl_unlock();
194124462549SFlorian Fainelli 
194224462549SFlorian Fainelli 	return 0;
194324462549SFlorian Fainelli }
194424462549SFlorian Fainelli 
194524462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
194624462549SFlorian Fainelli {
1947aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1948aab9c406SFlorian Fainelli 
1949a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1950a94c689eSFlorian Fainelli 		return 0;
1951a94c689eSFlorian Fainelli 
195224462549SFlorian Fainelli 	netif_device_attach(slave_dev);
195324462549SFlorian Fainelli 
1954aab9c406SFlorian Fainelli 	rtnl_lock();
1955aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1956aab9c406SFlorian Fainelli 	rtnl_unlock();
195724462549SFlorian Fainelli 
195824462549SFlorian Fainelli 	return 0;
195924462549SFlorian Fainelli }
196024462549SFlorian Fainelli 
1961951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
196291da11f8SLennert Buytenhek {
196324a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1964f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
19654cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1966951259aaSVivien Didelot 	const char *name = port->name;
196791da11f8SLennert Buytenhek 	struct net_device *slave_dev;
196891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
196991da11f8SLennert Buytenhek 	int ret;
197091da11f8SLennert Buytenhek 
197155199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
197255199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
197355199df6SFlorian Fainelli 
197455199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
197555199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
197655199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
197791da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1978d87d6f44SGuenter Roeck 		return -ENOMEM;
197991da11f8SLennert Buytenhek 
19807ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
198183216e39SMichael Walle 	if (!is_zero_ether_addr(port->mac))
1982e35b8d7dSJakub Kicinski 		eth_hw_addr_set(slave_dev, port->mac);
1983a2c7023fSXiaofei Shen 	else
19842fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
19850a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
19863e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1987bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1988bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1989f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1990d442ad4aSStephen Hemminger 
19911a33e10eSCong Wang 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
19921a33e10eSCong Wang 				 NULL);
19931a33e10eSCong Wang 
19944cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
19954cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
199691da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
199791da11f8SLennert Buytenhek 
199891da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
19996a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
20006a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
20015f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
20025f6b4e14SFlorian Fainelli 		return -ENOMEM;
20035f6b4e14SFlorian Fainelli 	}
2004e131a563SAlexander Lobakin 
2005e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
2006e131a563SAlexander Lobakin 	if (ret)
2007e131a563SAlexander Lobakin 		goto out_free;
2008e131a563SAlexander Lobakin 
20094cfbf09cSVivien Didelot 	p->dp = port;
2010f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
2011f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
201253da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
201391da11f8SLennert Buytenhek 
201491da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
201591da11f8SLennert Buytenhek 
20164fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
20170071f56eSAndrew Lunn 	if (ret) {
2018c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
2019c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2020c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
2021e131a563SAlexander Lobakin 		goto out_gcells;
2022e804441cSFlorian Fainelli 	}
2023e804441cSFlorian Fainelli 
20242f1e8ea7SVladimir Oltean 	rtnl_lock();
2025*e31dbd3bSVladimir Oltean 
2026904e112aSVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2027904e112aSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
2028904e112aSVladimir Oltean 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2029904e112aSVladimir Oltean 			 ret, ETH_DATA_LEN, port->index);
2030904e112aSVladimir Oltean 
20312f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
2032e804441cSFlorian Fainelli 	if (ret) {
2033e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
2034e804441cSFlorian Fainelli 			   ret, slave_dev->name);
20352f1e8ea7SVladimir Oltean 		rtnl_unlock();
2036e804441cSFlorian Fainelli 		goto out_phy;
20370071f56eSAndrew Lunn 	}
20380071f56eSAndrew Lunn 
20392f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
20402f1e8ea7SVladimir Oltean 
20412f1e8ea7SVladimir Oltean 	rtnl_unlock();
20422f1e8ea7SVladimir Oltean 
20432f1e8ea7SVladimir Oltean 	if (ret)
20442f1e8ea7SVladimir Oltean 		goto out_unregister;
20452f1e8ea7SVladimir Oltean 
2046d87d6f44SGuenter Roeck 	return 0;
2047e804441cSFlorian Fainelli 
20482f1e8ea7SVladimir Oltean out_unregister:
20492f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
2050e804441cSFlorian Fainelli out_phy:
2051aab9c406SFlorian Fainelli 	rtnl_lock();
2052aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
2053aab9c406SFlorian Fainelli 	rtnl_unlock();
2054aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
2055e131a563SAlexander Lobakin out_gcells:
2056e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
2057e804441cSFlorian Fainelli out_free:
20586a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2059e804441cSFlorian Fainelli 	free_netdev(slave_dev);
2060f8b8b1cdSVivien Didelot 	port->slave = NULL;
2061e804441cSFlorian Fainelli 	return ret;
206291da11f8SLennert Buytenhek }
2063b73adef6SFlorian Fainelli 
2064cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
2065cda5c15bSNeil Armstrong {
20662f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
2067d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2068cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2069cda5c15bSNeil Armstrong 
2070cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
2071aab9c406SFlorian Fainelli 	rtnl_lock();
20722f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
20732f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
2074aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
2075aab9c406SFlorian Fainelli 	rtnl_unlock();
2076881eadabSJohan Hovold 
2077aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
2078e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
20796a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2080cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
2081cda5c15bSNeil Armstrong }
2082cda5c15bSNeil Armstrong 
20834d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
2084b73adef6SFlorian Fainelli {
2085b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2086b73adef6SFlorian Fainelli }
2087a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2088b73adef6SFlorian Fainelli 
20898e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
20908e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
2091b73adef6SFlorian Fainelli {
2092d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
20932afc526aSVladimir Oltean 	struct netlink_ext_ack *extack;
20948e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
2095b73adef6SFlorian Fainelli 
20962afc526aSVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
20972afc526aSVladimir Oltean 
20988e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
20998e92ab3aSVivien Didelot 		if (info->linking) {
21002afc526aSVladimir Oltean 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2101bff33f7eSVladimir Oltean 			if (!err)
2102bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
210367b5fb5dSVladimir Oltean 			if (err == -EOPNOTSUPP) {
210467b5fb5dSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
210567b5fb5dSVladimir Oltean 						   "Offloading not supported");
210667b5fb5dSVladimir Oltean 				err = 0;
210767b5fb5dSVladimir Oltean 			}
21088e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
21098e92ab3aSVivien Didelot 		} else {
211017d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
21118e92ab3aSVivien Didelot 			err = NOTIFY_OK;
21128e92ab3aSVivien Didelot 		}
2113058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
2114058102a6STobias Waldekranz 		if (info->linking) {
2115058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
21162afc526aSVladimir Oltean 						info->upper_info, extack);
2117058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
2118058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
2119058102a6STobias Waldekranz 						   "Offloading not supported");
2120058102a6STobias Waldekranz 				err = 0;
2121058102a6STobias Waldekranz 			}
2122058102a6STobias Waldekranz 			err = notifier_from_errno(err);
2123058102a6STobias Waldekranz 		} else {
2124058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
2125058102a6STobias Waldekranz 			err = NOTIFY_OK;
2126058102a6STobias Waldekranz 		}
212718596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
212818596f50SGeorge McCollister 		if (info->linking) {
212918596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
213018596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
213118596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
213218596f50SGeorge McCollister 						   "Offloading not supported");
213318596f50SGeorge McCollister 				err = 0;
213418596f50SGeorge McCollister 			}
213518596f50SGeorge McCollister 			err = notifier_from_errno(err);
213618596f50SGeorge McCollister 		} else {
213718596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
213818596f50SGeorge McCollister 			err = NOTIFY_OK;
213918596f50SGeorge McCollister 		}
2140058102a6STobias Waldekranz 	}
2141058102a6STobias Waldekranz 
2142058102a6STobias Waldekranz 	return err;
2143058102a6STobias Waldekranz }
2144058102a6STobias Waldekranz 
214574918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev,
214674918945SVladimir Oltean 				    struct netdev_notifier_changeupper_info *info)
214774918945SVladimir Oltean {
214874918945SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
214974918945SVladimir Oltean 
215074918945SVladimir Oltean 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
21514e51bf44SVladimir Oltean 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
215274918945SVladimir Oltean 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
21534e51bf44SVladimir Oltean 		dsa_port_pre_lag_leave(dp, info->upper_dev);
215474918945SVladimir Oltean 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
215574918945SVladimir Oltean 	 * meaningfully enslaved to a bridge yet
215674918945SVladimir Oltean 	 */
215774918945SVladimir Oltean 
21584e51bf44SVladimir Oltean 	return NOTIFY_DONE;
215974918945SVladimir Oltean }
216074918945SVladimir Oltean 
2161058102a6STobias Waldekranz static int
2162058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
2163058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
2164058102a6STobias Waldekranz {
2165058102a6STobias Waldekranz 	struct net_device *lower;
2166058102a6STobias Waldekranz 	struct list_head *iter;
2167058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
2168058102a6STobias Waldekranz 	struct dsa_port *dp;
2169058102a6STobias Waldekranz 
2170058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
2171058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
2172058102a6STobias Waldekranz 			continue;
2173058102a6STobias Waldekranz 
2174058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
2175058102a6STobias Waldekranz 		if (!dp->lag_dev)
2176058102a6STobias Waldekranz 			/* Software LAG */
2177058102a6STobias Waldekranz 			continue;
2178058102a6STobias Waldekranz 
2179058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
2180058102a6STobias Waldekranz 		if (notifier_to_errno(err))
2181058102a6STobias Waldekranz 			break;
21826debb68aSVivien Didelot 	}
2183b73adef6SFlorian Fainelli 
21848e92ab3aSVivien Didelot 	return err;
2185b73adef6SFlorian Fainelli }
2186b73adef6SFlorian Fainelli 
218774918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls
218874918945SVladimir Oltean  * dsa_slave_prechangeupper()
218974918945SVladimir Oltean  */
219074918945SVladimir Oltean static int
219174918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev,
219274918945SVladimir Oltean 			     struct netdev_notifier_changeupper_info *info)
219374918945SVladimir Oltean {
219474918945SVladimir Oltean 	struct net_device *lower;
219574918945SVladimir Oltean 	struct list_head *iter;
219674918945SVladimir Oltean 	int err = NOTIFY_DONE;
219774918945SVladimir Oltean 	struct dsa_port *dp;
219874918945SVladimir Oltean 
219974918945SVladimir Oltean 	netdev_for_each_lower_dev(dev, lower, iter) {
220074918945SVladimir Oltean 		if (!dsa_slave_dev_check(lower))
220174918945SVladimir Oltean 			continue;
220274918945SVladimir Oltean 
220374918945SVladimir Oltean 		dp = dsa_slave_to_port(lower);
220474918945SVladimir Oltean 		if (!dp->lag_dev)
220574918945SVladimir Oltean 			/* Software LAG */
220674918945SVladimir Oltean 			continue;
220774918945SVladimir Oltean 
220874918945SVladimir Oltean 		err = dsa_slave_prechangeupper(lower, info);
220974918945SVladimir Oltean 		if (notifier_to_errno(err))
221074918945SVladimir Oltean 			break;
221174918945SVladimir Oltean 	}
221274918945SVladimir Oltean 
221374918945SVladimir Oltean 	return err;
221474918945SVladimir Oltean }
221574918945SVladimir Oltean 
2216eb46e8daSVladimir Oltean static int
2217eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2218eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2219cc1d5bdaSFlorian Fainelli {
2220cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
222136cbf39bSVladimir Oltean 	struct net_device *slave, *br;
2222cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2223cc1d5bdaSFlorian Fainelli 
2224cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2225cc1d5bdaSFlorian Fainelli 
2226cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2227cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2228cc1d5bdaSFlorian Fainelli 
2229cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2230cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2231cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2232cc1d5bdaSFlorian Fainelli 
2233cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
223436cbf39bSVladimir Oltean 	br = dsa_port_bridge_dev_get(dp);
223536cbf39bSVladimir Oltean 	if (!br)
2236cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2237cc1d5bdaSFlorian Fainelli 
2238cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
223936cbf39bSVladimir Oltean 	if (br_vlan_enabled(br) &&
2240cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2241cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2242cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2243cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2244cc1d5bdaSFlorian Fainelli 	}
2245cc1d5bdaSFlorian Fainelli 
2246cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2247cc1d5bdaSFlorian Fainelli }
2248cc1d5bdaSFlorian Fainelli 
22492b138406SVladimir Oltean static int
22502b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
22512b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
22522b138406SVladimir Oltean {
22532b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
225436cbf39bSVladimir Oltean 	struct net_device *br = dsa_port_bridge_dev_get(dp);
22552b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
22562b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
22572b138406SVladimir Oltean 	int err = NOTIFY_DONE;
22582b138406SVladimir Oltean 	u16 vid;
22592b138406SVladimir Oltean 
2260adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
22612b138406SVladimir Oltean 		return NOTIFY_DONE;
22622b138406SVladimir Oltean 
22632b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
22642b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
22652b138406SVladimir Oltean 
22662b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
22672b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
22682b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
22692b138406SVladimir Oltean 	 */
22702b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
22712b138406SVladimir Oltean 	if (err == 0) {
22722b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
22732b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
22742b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
22752b138406SVladimir Oltean 	}
22762b138406SVladimir Oltean 
22772b138406SVladimir Oltean 	return NOTIFY_DONE;
22782b138406SVladimir Oltean }
22792b138406SVladimir Oltean 
22804ede74e7SVladimir Oltean static int
22814ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
22824ede74e7SVladimir Oltean 				      struct netdev_notifier_changeupper_info *info)
2283b73adef6SFlorian Fainelli {
2284e358bef7SVladimir Oltean 	struct dsa_switch *ds;
2285e358bef7SVladimir Oltean 	struct dsa_port *dp;
2286e358bef7SVladimir Oltean 	int err;
22872b138406SVladimir Oltean 
228853bade8aSFlorian Fainelli 	if (!dsa_slave_dev_check(dev))
22894ede74e7SVladimir Oltean 		return dsa_prevent_bridging_8021q_upper(dev, info);
22902b138406SVladimir Oltean 
2291e358bef7SVladimir Oltean 	dp = dsa_slave_to_port(dev);
2292e358bef7SVladimir Oltean 	ds = dp->ds;
2293e358bef7SVladimir Oltean 
2294e358bef7SVladimir Oltean 	if (ds->ops->port_prechangeupper) {
2295e358bef7SVladimir Oltean 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
2296e358bef7SVladimir Oltean 		if (err)
2297e358bef7SVladimir Oltean 			return notifier_from_errno(err);
2298e358bef7SVladimir Oltean 	}
2299e358bef7SVladimir Oltean 
23002b138406SVladimir Oltean 	if (is_vlan_dev(info->upper_dev))
23014ede74e7SVladimir Oltean 		return dsa_slave_check_8021q_upper(dev, info);
23024ede74e7SVladimir Oltean 
23034ede74e7SVladimir Oltean 	return NOTIFY_DONE;
23044ede74e7SVladimir Oltean }
23054ede74e7SVladimir Oltean 
23064ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb,
23074ede74e7SVladimir Oltean 				     unsigned long event, void *ptr)
23084ede74e7SVladimir Oltean {
23094ede74e7SVladimir Oltean 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
23104ede74e7SVladimir Oltean 
23114ede74e7SVladimir Oltean 	switch (event) {
23124ede74e7SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
23134ede74e7SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
23144ede74e7SVladimir Oltean 		int err;
23154ede74e7SVladimir Oltean 
23164ede74e7SVladimir Oltean 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
23174ede74e7SVladimir Oltean 		if (err != NOTIFY_DONE)
23184ede74e7SVladimir Oltean 			return err;
23194ede74e7SVladimir Oltean 
232074918945SVladimir Oltean 		if (dsa_slave_dev_check(dev))
232174918945SVladimir Oltean 			return dsa_slave_prechangeupper(dev, ptr);
232274918945SVladimir Oltean 
232374918945SVladimir Oltean 		if (netif_is_lag_master(dev))
232474918945SVladimir Oltean 			return dsa_slave_lag_prechangeupper(dev, ptr);
232574918945SVladimir Oltean 
232683501299SVladimir Oltean 		break;
23272b138406SVladimir Oltean 	}
232883501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2329058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
23308e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2331058102a6STobias Waldekranz 
2332058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2333058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2334058102a6STobias Waldekranz 
2335058102a6STobias Waldekranz 		break;
2336058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2337058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2338058102a6STobias Waldekranz 		struct dsa_port *dp;
2339058102a6STobias Waldekranz 		int err;
2340058102a6STobias Waldekranz 
2341058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2342058102a6STobias Waldekranz 			break;
2343058102a6STobias Waldekranz 
2344058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2345058102a6STobias Waldekranz 
2346058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2347058102a6STobias Waldekranz 		return notifier_from_errno(err);
2348058102a6STobias Waldekranz 	}
2349c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2350c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2351c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2352c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2353c0a8a9c2SVladimir Oltean 
2354c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2355c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2356c0a8a9c2SVladimir Oltean 
2357c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2358c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2359c0a8a9c2SVladimir Oltean 
2360c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2361d0004a02SVladimir Oltean 			if (!dsa_port_is_user(dp))
2362c0a8a9c2SVladimir Oltean 				continue;
2363c0a8a9c2SVladimir Oltean 
2364c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2365c0a8a9c2SVladimir Oltean 		}
2366c0a8a9c2SVladimir Oltean 
2367c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2368c0a8a9c2SVladimir Oltean 
2369c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2370c0a8a9c2SVladimir Oltean 	}
2371c0a8a9c2SVladimir Oltean 	default:
2372c0a8a9c2SVladimir Oltean 		break;
2373cc1d5bdaSFlorian Fainelli 	}
2374b73adef6SFlorian Fainelli 
2375b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2376b73adef6SFlorian Fainelli }
237788e4f0caSVivien Didelot 
2378c4bb76a9SVladimir Oltean static void
2379c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2380c4bb76a9SVladimir Oltean {
2381c35b57ceSVladimir Oltean 	struct switchdev_notifier_fdb_info info = {};
2382c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2383c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2384c4bb76a9SVladimir Oltean 
2385c4bb76a9SVladimir Oltean 	if (!dsa_is_user_port(ds, switchdev_work->port))
2386c4bb76a9SVladimir Oltean 		return;
2387c4bb76a9SVladimir Oltean 
2388c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2389c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2390c4bb76a9SVladimir Oltean 	info.offloaded = true;
2391c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2392c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2393c4bb76a9SVladimir Oltean 				 dp->slave, &info.info, NULL);
2394c4bb76a9SVladimir Oltean }
2395c9eb3e0fSArkadi Sharshevsky 
2396c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2397c9eb3e0fSArkadi Sharshevsky {
2398c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2399c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
2400c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2401c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2402c9eb3e0fSArkadi Sharshevsky 	int err;
2403c9eb3e0fSArkadi Sharshevsky 
2404c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2405c4bb76a9SVladimir Oltean 
2406c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2407c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
24083dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
24093dc80afcSVladimir Oltean 			err = dsa_port_host_fdb_add(dp, switchdev_work->addr,
24103dc80afcSVladimir Oltean 						    switchdev_work->vid);
24113dc80afcSVladimir Oltean 		else
2412c4bb76a9SVladimir Oltean 			err = dsa_port_fdb_add(dp, switchdev_work->addr,
2413c4bb76a9SVladimir Oltean 					       switchdev_work->vid);
2414c9eb3e0fSArkadi Sharshevsky 		if (err) {
2415c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2416c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
2417c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2418c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2419c9eb3e0fSArkadi Sharshevsky 			break;
2420c9eb3e0fSArkadi Sharshevsky 		}
2421c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2422c9eb3e0fSArkadi Sharshevsky 		break;
2423c9eb3e0fSArkadi Sharshevsky 
2424c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
24253dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
24263dc80afcSVladimir Oltean 			err = dsa_port_host_fdb_del(dp, switchdev_work->addr,
24273dc80afcSVladimir Oltean 						    switchdev_work->vid);
24283dc80afcSVladimir Oltean 		else
2429c4bb76a9SVladimir Oltean 			err = dsa_port_fdb_del(dp, switchdev_work->addr,
2430c4bb76a9SVladimir Oltean 					       switchdev_work->vid);
2431c9eb3e0fSArkadi Sharshevsky 		if (err) {
2432c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2433c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2434c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2435c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2436c9eb3e0fSArkadi Sharshevsky 		}
24372fd18650SVladimir Oltean 
2438c9eb3e0fSArkadi Sharshevsky 		break;
2439c9eb3e0fSArkadi Sharshevsky 	}
2440c9eb3e0fSArkadi Sharshevsky 
2441c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2442c9eb3e0fSArkadi Sharshevsky }
2443c9eb3e0fSArkadi Sharshevsky 
2444b94dc99cSVladimir Oltean static bool dsa_foreign_dev_check(const struct net_device *dev,
2445b94dc99cSVladimir Oltean 				  const struct net_device *foreign_dev)
2446d5f19486SVladimir Oltean {
2447b94dc99cSVladimir Oltean 	const struct dsa_port *dp = dsa_slave_to_port(dev);
2448b94dc99cSVladimir Oltean 	struct dsa_switch_tree *dst = dp->ds->dst;
2449b94dc99cSVladimir Oltean 
2450b94dc99cSVladimir Oltean 	if (netif_is_bridge_master(foreign_dev))
2451936db8a2SVladimir Oltean 		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
2452b94dc99cSVladimir Oltean 
2453b94dc99cSVladimir Oltean 	if (netif_is_bridge_port(foreign_dev))
2454b94dc99cSVladimir Oltean 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2455b94dc99cSVladimir Oltean 
2456b94dc99cSVladimir Oltean 	/* Everything else is foreign */
2457b94dc99cSVladimir Oltean 	return true;
2458d5f19486SVladimir Oltean }
2459d5f19486SVladimir Oltean 
2460b94dc99cSVladimir Oltean static int dsa_slave_fdb_event(struct net_device *dev,
2461716a30a9SVladimir Oltean 			       struct net_device *orig_dev,
2462716a30a9SVladimir Oltean 			       unsigned long event, const void *ctx,
2463716a30a9SVladimir Oltean 			       const struct switchdev_notifier_fdb_info *fdb_info)
2464b94dc99cSVladimir Oltean {
2465b94dc99cSVladimir Oltean 	struct dsa_switchdev_event_work *switchdev_work;
2466b94dc99cSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2467b94dc99cSVladimir Oltean 	bool host_addr = fdb_info->is_local;
2468b94dc99cSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
2469b94dc99cSVladimir Oltean 
2470b94dc99cSVladimir Oltean 	if (ctx && ctx != dp)
2471b94dc99cSVladimir Oltean 		return 0;
2472b94dc99cSVladimir Oltean 
2473b94dc99cSVladimir Oltean 	if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2474b94dc99cSVladimir Oltean 		return -EOPNOTSUPP;
2475b94dc99cSVladimir Oltean 
2476b94dc99cSVladimir Oltean 	if (dsa_slave_dev_check(orig_dev) &&
2477b94dc99cSVladimir Oltean 	    switchdev_fdb_is_dynamically_learned(fdb_info))
2478b94dc99cSVladimir Oltean 		return 0;
2479b94dc99cSVladimir Oltean 
2480b94dc99cSVladimir Oltean 	/* FDB entries learned by the software bridge should be installed as
2481b94dc99cSVladimir Oltean 	 * host addresses only if the driver requests assisted learning.
2482b94dc99cSVladimir Oltean 	 */
2483b94dc99cSVladimir Oltean 	if (switchdev_fdb_is_dynamically_learned(fdb_info) &&
2484b94dc99cSVladimir Oltean 	    !ds->assisted_learning_on_cpu_port)
2485b94dc99cSVladimir Oltean 		return 0;
2486b94dc99cSVladimir Oltean 
2487b94dc99cSVladimir Oltean 	/* Also treat FDB entries on foreign interfaces bridged with us as host
2488b94dc99cSVladimir Oltean 	 * addresses.
2489b94dc99cSVladimir Oltean 	 */
2490b94dc99cSVladimir Oltean 	if (dsa_foreign_dev_check(dev, orig_dev))
2491b94dc99cSVladimir Oltean 		host_addr = true;
2492b94dc99cSVladimir Oltean 
2493b94dc99cSVladimir Oltean 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2494b94dc99cSVladimir Oltean 	if (!switchdev_work)
2495b94dc99cSVladimir Oltean 		return -ENOMEM;
2496b94dc99cSVladimir Oltean 
2497b94dc99cSVladimir Oltean 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2498b94dc99cSVladimir Oltean 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2499b94dc99cSVladimir Oltean 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
2500b94dc99cSVladimir Oltean 		   host_addr ? " as host address" : "");
2501b94dc99cSVladimir Oltean 
2502b94dc99cSVladimir Oltean 	INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2503b94dc99cSVladimir Oltean 	switchdev_work->ds = ds;
2504b94dc99cSVladimir Oltean 	switchdev_work->port = dp->index;
2505b94dc99cSVladimir Oltean 	switchdev_work->event = event;
2506b94dc99cSVladimir Oltean 	switchdev_work->dev = dev;
2507b94dc99cSVladimir Oltean 
2508b94dc99cSVladimir Oltean 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2509b94dc99cSVladimir Oltean 	switchdev_work->vid = fdb_info->vid;
2510b94dc99cSVladimir Oltean 	switchdev_work->host_addr = host_addr;
2511b94dc99cSVladimir Oltean 
2512b94dc99cSVladimir Oltean 	dsa_schedule_work(&switchdev_work->work);
2513b94dc99cSVladimir Oltean 
2514d5f19486SVladimir Oltean 	return 0;
2515d5f19486SVladimir Oltean }
2516d5f19486SVladimir Oltean 
2517c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2518c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2519c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2520c9eb3e0fSArkadi Sharshevsky {
2521c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
252279b139f4SVivien Didelot 	int err;
252379b139f4SVivien Didelot 
2524447d290aSVladimir Oltean 	switch (event) {
2525447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
252679b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
252779b139f4SVivien Didelot 						     dsa_slave_dev_check,
252879b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
252979b139f4SVivien Didelot 		return notifier_from_errno(err);
2530447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2531447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2532716a30a9SVladimir Oltean 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
2533b94dc99cSVladimir Oltean 							   dsa_slave_dev_check,
2534b94dc99cSVladimir Oltean 							   dsa_foreign_dev_check,
2535716a30a9SVladimir Oltean 							   dsa_slave_fdb_event,
2536b94dc99cSVladimir Oltean 							   NULL);
2537b94dc99cSVladimir Oltean 		return notifier_from_errno(err);
2538c9eb3e0fSArkadi Sharshevsky 	default:
2539c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2540c9eb3e0fSArkadi Sharshevsky 	}
2541c9eb3e0fSArkadi Sharshevsky 
2542c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2543c9eb3e0fSArkadi Sharshevsky }
2544c9eb3e0fSArkadi Sharshevsky 
25452b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
25462b239f67SPetr Machata 					      unsigned long event, void *ptr)
25472b239f67SPetr Machata {
25482b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
254979b139f4SVivien Didelot 	int err;
25502b239f67SPetr Machata 
25512b239f67SPetr Machata 	switch (event) {
255279b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
255379b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
255479b139f4SVivien Didelot 						    dsa_slave_dev_check,
255579b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
255679b139f4SVivien Didelot 		return notifier_from_errno(err);
25572b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
255879b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
255979b139f4SVivien Didelot 						    dsa_slave_dev_check,
256079b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
256179b139f4SVivien Didelot 		return notifier_from_errno(err);
25629ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
256379b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
256479b139f4SVivien Didelot 						     dsa_slave_dev_check,
256579b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
256679b139f4SVivien Didelot 		return notifier_from_errno(err);
25672b239f67SPetr Machata 	}
25682b239f67SPetr Machata 
25692b239f67SPetr Machata 	return NOTIFY_DONE;
25702b239f67SPetr Machata }
25712b239f67SPetr Machata 
257288e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
257388e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
257488e4f0caSVivien Didelot };
257588e4f0caSVivien Didelot 
2576010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = {
2577c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2578c9eb3e0fSArkadi Sharshevsky };
2579c9eb3e0fSArkadi Sharshevsky 
2580010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = {
25812b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
25822b239f67SPetr Machata };
25832b239f67SPetr Machata 
258488e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
258588e4f0caSVivien Didelot {
25862b239f67SPetr Machata 	struct notifier_block *nb;
2587c9eb3e0fSArkadi Sharshevsky 	int err;
2588c9eb3e0fSArkadi Sharshevsky 
2589c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2590c9eb3e0fSArkadi Sharshevsky 	if (err)
2591c9eb3e0fSArkadi Sharshevsky 		return err;
2592c9eb3e0fSArkadi Sharshevsky 
2593c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2594c9eb3e0fSArkadi Sharshevsky 	if (err)
2595c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2596c9eb3e0fSArkadi Sharshevsky 
25972b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
25982b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
25992b239f67SPetr Machata 	if (err)
26002b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
26012b239f67SPetr Machata 
2602c9eb3e0fSArkadi Sharshevsky 	return 0;
2603c9eb3e0fSArkadi Sharshevsky 
26042b239f67SPetr Machata err_switchdev_blocking_nb:
26052b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2606c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2607c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2608c9eb3e0fSArkadi Sharshevsky 	return err;
260988e4f0caSVivien Didelot }
261088e4f0caSVivien Didelot 
261188e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
261288e4f0caSVivien Didelot {
26132b239f67SPetr Machata 	struct notifier_block *nb;
261488e4f0caSVivien Didelot 	int err;
261588e4f0caSVivien Didelot 
26162b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
26172b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
26182b239f67SPetr Machata 	if (err)
26192b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
26202b239f67SPetr Machata 
2621c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2622c9eb3e0fSArkadi Sharshevsky 	if (err)
2623c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2624c9eb3e0fSArkadi Sharshevsky 
262588e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
262688e4f0caSVivien Didelot 	if (err)
262788e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
262888e4f0caSVivien Didelot }
2629