xref: /openbmc/linux/net/dsa/slave.c (revision 6a52e73368038f47f6618623d75061dc263b26ae)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
291da11f8SLennert Buytenhek /*
391da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
4e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
591da11f8SLennert Buytenhek  */
691da11f8SLennert Buytenhek 
791da11f8SLennert Buytenhek #include <linux/list.h>
8df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
9b73adef6SFlorian Fainelli #include <linux/netdevice.h>
1091da11f8SLennert Buytenhek #include <linux/phy.h>
11a2820543SFlorian Fainelli #include <linux/phy_fixed.h>
12aab9c406SFlorian Fainelli #include <linux/phylink.h>
130d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
140d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
157f854420SAndrew Lunn #include <linux/mdio.h>
16b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
17f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
18a71acad9SOleksij Rempel #include <net/selftests.h>
19f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
20b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2118596f50SGeorge McCollister #include <linux/if_hsr.h>
2204ff53f9SFlorian Fainelli #include <linux/netpoll.h>
23ea5dd34bSVivien Didelot 
2491da11f8SLennert Buytenhek #include "dsa_priv.h"
2591da11f8SLennert Buytenhek 
2691da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2791da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2891da11f8SLennert Buytenhek {
2991da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3091da11f8SLennert Buytenhek 
310d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
329d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3391da11f8SLennert Buytenhek 
3491da11f8SLennert Buytenhek 	return 0xffff;
3591da11f8SLennert Buytenhek }
3691da11f8SLennert Buytenhek 
3791da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3891da11f8SLennert Buytenhek {
3991da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4091da11f8SLennert Buytenhek 
410d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
429d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4391da11f8SLennert Buytenhek 
4491da11f8SLennert Buytenhek 	return 0;
4591da11f8SLennert Buytenhek }
4691da11f8SLennert Buytenhek 
4791da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4891da11f8SLennert Buytenhek {
4991da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
5091da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5191da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5291da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
530b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
5449463b7fSVivien Didelot 		 ds->dst->index, ds->index);
55c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
5624df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
5791da11f8SLennert Buytenhek }
5891da11f8SLennert Buytenhek 
5991da11f8SLennert Buytenhek 
6091da11f8SLennert Buytenhek /* slave device handling ****************************************************/
61abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
62c0840801SLennert Buytenhek {
63d0006b00SVivien Didelot 	return dsa_slave_to_master(dev)->ifindex;
64c0840801SLennert Buytenhek }
65c0840801SLennert Buytenhek 
6691da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6791da11f8SLennert Buytenhek {
68d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
69d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
70df02c6ffSLennert Buytenhek 	int err;
71df02c6ffSLennert Buytenhek 
729d5ef190SVladimir Oltean 	err = dev_open(master, NULL);
739d5ef190SVladimir Oltean 	if (err < 0) {
749d5ef190SVladimir Oltean 		netdev_err(dev, "failed to open master %s\n", master->name);
759d5ef190SVladimir Oltean 		goto out;
769d5ef190SVladimir Oltean 	}
77df02c6ffSLennert Buytenhek 
788feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
79a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
80df02c6ffSLennert Buytenhek 		if (err < 0)
81df02c6ffSLennert Buytenhek 			goto out;
82df02c6ffSLennert Buytenhek 	}
83df02c6ffSLennert Buytenhek 
84df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
85df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
86df02c6ffSLennert Buytenhek 		if (err < 0)
87df02c6ffSLennert Buytenhek 			goto del_unicast;
88df02c6ffSLennert Buytenhek 	}
89df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
90df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
91df02c6ffSLennert Buytenhek 		if (err < 0)
92df02c6ffSLennert Buytenhek 			goto clear_allmulti;
93df02c6ffSLennert Buytenhek 	}
94df02c6ffSLennert Buytenhek 
958640f8dcSRussell King 	err = dsa_port_enable_rt(dp, dev->phydev);
96b2f2af21SFlorian Fainelli 	if (err)
97b2f2af21SFlorian Fainelli 		goto clear_promisc;
98b73adef6SFlorian Fainelli 
9991da11f8SLennert Buytenhek 	return 0;
100df02c6ffSLennert Buytenhek 
101b2f2af21SFlorian Fainelli clear_promisc:
102b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1034fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
104df02c6ffSLennert Buytenhek clear_allmulti:
105df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
106df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
107df02c6ffSLennert Buytenhek del_unicast:
1088feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
109a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
110df02c6ffSLennert Buytenhek out:
111df02c6ffSLennert Buytenhek 	return err;
11291da11f8SLennert Buytenhek }
11391da11f8SLennert Buytenhek 
11491da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
11591da11f8SLennert Buytenhek {
116d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
117d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
118df02c6ffSLennert Buytenhek 
1198640f8dcSRussell King 	dsa_port_disable_rt(dp);
1206457edfeSVivien Didelot 
121df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
122a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
123df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
124df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
125df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
126df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
127df02c6ffSLennert Buytenhek 
1288feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
129a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
130df02c6ffSLennert Buytenhek 
13191da11f8SLennert Buytenhek 	return 0;
13291da11f8SLennert Buytenhek }
13391da11f8SLennert Buytenhek 
13491da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
13591da11f8SLennert Buytenhek {
136d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
13717ab4f61SRundong Ge 	if (dev->flags & IFF_UP) {
13891da11f8SLennert Buytenhek 		if (change & IFF_ALLMULTI)
13917ab4f61SRundong Ge 			dev_set_allmulti(master,
14017ab4f61SRundong Ge 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
14191da11f8SLennert Buytenhek 		if (change & IFF_PROMISC)
14217ab4f61SRundong Ge 			dev_set_promiscuity(master,
14317ab4f61SRundong Ge 					    dev->flags & IFF_PROMISC ? 1 : -1);
14417ab4f61SRundong Ge 	}
14591da11f8SLennert Buytenhek }
14691da11f8SLennert Buytenhek 
14791da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
14891da11f8SLennert Buytenhek {
149d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
15091da11f8SLennert Buytenhek 
15191da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
152a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
15391da11f8SLennert Buytenhek }
15491da11f8SLennert Buytenhek 
155df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
15691da11f8SLennert Buytenhek {
157d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
158df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
159df02c6ffSLennert Buytenhek 	int err;
160df02c6ffSLennert Buytenhek 
161df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
162df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
163df02c6ffSLennert Buytenhek 
164df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
165df02c6ffSLennert Buytenhek 		goto out;
166df02c6ffSLennert Buytenhek 
1678feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
168a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
169df02c6ffSLennert Buytenhek 		if (err < 0)
170df02c6ffSLennert Buytenhek 			return err;
171df02c6ffSLennert Buytenhek 	}
172df02c6ffSLennert Buytenhek 
1738feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
174a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
175df02c6ffSLennert Buytenhek 
176df02c6ffSLennert Buytenhek out:
177d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
17891da11f8SLennert Buytenhek 
17991da11f8SLennert Buytenhek 	return 0;
18091da11f8SLennert Buytenhek }
18191da11f8SLennert Buytenhek 
1822bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1832bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1842bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1852bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1862bedde1aSArkadi Sharshevsky 	int idx;
1872bedde1aSArkadi Sharshevsky };
1882bedde1aSArkadi Sharshevsky 
1892bedde1aSArkadi Sharshevsky static int
1902bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1912bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
1922bedde1aSArkadi Sharshevsky {
1932bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
1942bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1952bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
1962bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
1972bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
1982bedde1aSArkadi Sharshevsky 
1992bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
2002bedde1aSArkadi Sharshevsky 		goto skip;
2012bedde1aSArkadi Sharshevsky 
2022bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
2032bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2042bedde1aSArkadi Sharshevsky 	if (!nlh)
2052bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2062bedde1aSArkadi Sharshevsky 
2072bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2082bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2092bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2102bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2112bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2122bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2132bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2142bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2152bedde1aSArkadi Sharshevsky 
2162bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2172bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2182bedde1aSArkadi Sharshevsky 
2192bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2202bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2212bedde1aSArkadi Sharshevsky 
2222bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2232bedde1aSArkadi Sharshevsky 
2242bedde1aSArkadi Sharshevsky skip:
2252bedde1aSArkadi Sharshevsky 	dump->idx++;
2262bedde1aSArkadi Sharshevsky 	return 0;
2272bedde1aSArkadi Sharshevsky 
2282bedde1aSArkadi Sharshevsky nla_put_failure:
2292bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2302bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2312bedde1aSArkadi Sharshevsky }
2322bedde1aSArkadi Sharshevsky 
2332bedde1aSArkadi Sharshevsky static int
2342bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2352bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2362bedde1aSArkadi Sharshevsky 		   int *idx)
2372bedde1aSArkadi Sharshevsky {
238d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2392bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2402bedde1aSArkadi Sharshevsky 		.dev = dev,
2412bedde1aSArkadi Sharshevsky 		.skb = skb,
2422bedde1aSArkadi Sharshevsky 		.cb = cb,
2432bedde1aSArkadi Sharshevsky 		.idx = *idx,
2442bedde1aSArkadi Sharshevsky 	};
2452bedde1aSArkadi Sharshevsky 	int err;
2462bedde1aSArkadi Sharshevsky 
247de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2482bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
249de40fc5dSVivien Didelot 
2502bedde1aSArkadi Sharshevsky 	return err;
2512bedde1aSArkadi Sharshevsky }
2522bedde1aSArkadi Sharshevsky 
25391da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
25491da11f8SLennert Buytenhek {
2550336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2560336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2570336369dSBrandon Streiff 	int port = p->dp->index;
2580336369dSBrandon Streiff 
2590336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2600336369dSBrandon Streiff 	switch (cmd) {
2610336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2620336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2630336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2640336369dSBrandon Streiff 		break;
2650336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2660336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2670336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2680336369dSBrandon Streiff 		break;
2690336369dSBrandon Streiff 	}
2700336369dSBrandon Streiff 
271aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
27291da11f8SLennert Buytenhek }
27391da11f8SLennert Buytenhek 
27469bfac96SVladimir Oltean static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
2754c08c586SVladimir Oltean 				   const struct switchdev_attr *attr,
2764c08c586SVladimir Oltean 				   struct netlink_ext_ack *extack)
27735636062SScott Feldman {
278d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
279b8d866acSVivien Didelot 	int ret;
28035636062SScott Feldman 
2810d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
2820d2cfbd4SVladimir Oltean 		return 0;
2830d2cfbd4SVladimir Oltean 
28435636062SScott Feldman 	switch (attr->id) {
2851f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
28603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
28703cbb870SVladimir Oltean 			return -EOPNOTSUPP;
28803cbb870SVladimir Oltean 
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:
29203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
29303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
29403cbb870SVladimir Oltean 
29589153ed6SVladimir Oltean 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
29689153ed6SVladimir Oltean 					      extack);
297fb2dabadSVivien Didelot 		break;
29834a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
29903cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(dp, attr->orig_dev))
30003cbb870SVladimir Oltean 			return -EOPNOTSUPP;
30103cbb870SVladimir Oltean 
302bae33f2bSVladimir Oltean 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
30334a79f63SVivien Didelot 		break;
304ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
30503cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
30603cbb870SVladimir Oltean 			return -EOPNOTSUPP;
30703cbb870SVladimir Oltean 
308a8b659e7SVladimir Oltean 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
309a8b659e7SVladimir Oltean 						extack);
310ea87005aSFlorian Fainelli 		break;
31157652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
31203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
31303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
31403cbb870SVladimir Oltean 
315a8b659e7SVladimir Oltean 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
31657652796SRussell King 		break;
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 	 */
366ffb68fc5SVladimir Oltean 	if (br_vlan_enabled(dp->bridge_dev)) {
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:
41203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(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:
42403cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(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:
43003cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(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:
48603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(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:
49803cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(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:
50403cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge(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 
792a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev,
793a71acad9SOleksij Rempel 				   struct ethtool_test *etest, u64 *buf)
794a71acad9SOleksij Rempel {
795a71acad9SOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(ndev);
796a71acad9SOleksij Rempel 	struct dsa_switch *ds = dp->ds;
797a71acad9SOleksij Rempel 
798a71acad9SOleksij Rempel 	if (ds->ops->self_test) {
799a71acad9SOleksij Rempel 		ds->ops->self_test(ds, dp->index, etest, buf);
800a71acad9SOleksij Rempel 		return;
801a71acad9SOleksij Rempel 	}
802a71acad9SOleksij Rempel 
803a71acad9SOleksij Rempel 	net_selftest(ndev, etest, buf);
804a71acad9SOleksij Rempel }
805a71acad9SOleksij Rempel 
80619e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
80719e57c4eSFlorian Fainelli {
808d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
809d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
81019e57c4eSFlorian Fainelli 
811aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
812aab9c406SFlorian Fainelli 
8139d490b4eSVivien Didelot 	if (ds->ops->get_wol)
814d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
81519e57c4eSFlorian Fainelli }
81619e57c4eSFlorian Fainelli 
81719e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
81819e57c4eSFlorian Fainelli {
819d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
820d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
82119e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
82219e57c4eSFlorian Fainelli 
823aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
824aab9c406SFlorian Fainelli 
8259d490b4eSVivien Didelot 	if (ds->ops->set_wol)
826d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
82719e57c4eSFlorian Fainelli 
82819e57c4eSFlorian Fainelli 	return ret;
82919e57c4eSFlorian Fainelli }
83019e57c4eSFlorian Fainelli 
8317905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
8327905288fSFlorian Fainelli {
833d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
834d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8357905288fSFlorian Fainelli 	int ret;
8367905288fSFlorian Fainelli 
8377b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
83800670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8397b9cc738SVivien Didelot 		return -ENODEV;
8407b9cc738SVivien Didelot 
84108f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
8427905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8437905288fSFlorian Fainelli 
844d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
8457905288fSFlorian Fainelli 	if (ret)
8467905288fSFlorian Fainelli 		return ret;
8477905288fSFlorian Fainelli 
848aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
8497905288fSFlorian Fainelli }
8507905288fSFlorian Fainelli 
8517905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
8527905288fSFlorian Fainelli {
853d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
854d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8557905288fSFlorian Fainelli 	int ret;
8567905288fSFlorian Fainelli 
8577b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
85800670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8597b9cc738SVivien Didelot 		return -ENODEV;
8607b9cc738SVivien Didelot 
86108f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
8627905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8637905288fSFlorian Fainelli 
864d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
8657905288fSFlorian Fainelli 	if (ret)
8667905288fSFlorian Fainelli 		return ret;
8677905288fSFlorian Fainelli 
868aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
869aab9c406SFlorian Fainelli }
870aab9c406SFlorian Fainelli 
871aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
872aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
873aab9c406SFlorian Fainelli {
874aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
875aab9c406SFlorian Fainelli 
876aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
877aab9c406SFlorian Fainelli }
878aab9c406SFlorian Fainelli 
879aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
880aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
881aab9c406SFlorian Fainelli {
882aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
883aab9c406SFlorian Fainelli 
884aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
8857905288fSFlorian Fainelli }
8867905288fSFlorian Fainelli 
887a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
888a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
889a2a1a13bSHeiner Kallweit {
890a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
891a2a1a13bSHeiner Kallweit 
892a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
893a2a1a13bSHeiner Kallweit }
894a2a1a13bSHeiner Kallweit 
895a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
896a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
897a2a1a13bSHeiner Kallweit {
898a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
899a2a1a13bSHeiner Kallweit 
900a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
901a2a1a13bSHeiner Kallweit }
902a2a1a13bSHeiner Kallweit 
90304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
90404ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
90504ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
90604ff53f9SFlorian Fainelli {
907d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
90804ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
90904ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
91004ff53f9SFlorian Fainelli 	int err = 0;
91104ff53f9SFlorian Fainelli 
91204ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
91304ff53f9SFlorian Fainelli 	if (!netpoll)
91404ff53f9SFlorian Fainelli 		return -ENOMEM;
91504ff53f9SFlorian Fainelli 
91604ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
91704ff53f9SFlorian Fainelli 	if (err) {
91804ff53f9SFlorian Fainelli 		kfree(netpoll);
91904ff53f9SFlorian Fainelli 		goto out;
92004ff53f9SFlorian Fainelli 	}
92104ff53f9SFlorian Fainelli 
92204ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
92304ff53f9SFlorian Fainelli out:
92404ff53f9SFlorian Fainelli 	return err;
92504ff53f9SFlorian Fainelli }
92604ff53f9SFlorian Fainelli 
92704ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
92804ff53f9SFlorian Fainelli {
92904ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
93004ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
93104ff53f9SFlorian Fainelli 
93204ff53f9SFlorian Fainelli 	if (!netpoll)
93304ff53f9SFlorian Fainelli 		return;
93404ff53f9SFlorian Fainelli 
93504ff53f9SFlorian Fainelli 	p->netpoll = NULL;
93604ff53f9SFlorian Fainelli 
937c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
93804ff53f9SFlorian Fainelli }
93904ff53f9SFlorian Fainelli 
94004ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
94104ff53f9SFlorian Fainelli {
94204ff53f9SFlorian Fainelli }
94304ff53f9SFlorian Fainelli #endif
94404ff53f9SFlorian Fainelli 
94544bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
94644bb765cSFlorian Fainelli 					char *name, size_t len)
94744bb765cSFlorian Fainelli {
948d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
94944bb765cSFlorian Fainelli 
950d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
951d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
952d484210bSJiri Pirko 	 * should be removed with legacy support.
953d484210bSJiri Pirko 	 */
954d484210bSJiri Pirko 	if (dp->ds->devlink)
955d484210bSJiri Pirko 		return -EOPNOTSUPP;
956d484210bSJiri Pirko 
957d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
95844bb765cSFlorian Fainelli 		return -EINVAL;
9593a543ef4SFlorian Fainelli 
9603a543ef4SFlorian Fainelli 	return 0;
9613a543ef4SFlorian Fainelli }
9623a543ef4SFlorian Fainelli 
963f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
9644fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
965f50f2127SFlorian Fainelli {
9664fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
967f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
968f50f2127SFlorian Fainelli 
969f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
970f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
971f50f2127SFlorian Fainelli 			return mall_tc_entry;
972f50f2127SFlorian Fainelli 
973f50f2127SFlorian Fainelli 	return NULL;
974f50f2127SFlorian Fainelli }
975f50f2127SFlorian Fainelli 
976e13c2075SVladimir Oltean static int
977e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
978f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
979f50f2127SFlorian Fainelli 				  bool ingress)
980f50f2127SFlorian Fainelli {
981d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
982f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
983e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
984f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
985d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9869681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
987d945097bSVivien Didelot 	struct dsa_port *to_dp;
988e13c2075SVladimir Oltean 	int err;
989e13c2075SVladimir Oltean 
990f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
99134297176SVladimir Oltean 		return -EOPNOTSUPP;
992f50f2127SFlorian Fainelli 
99353eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
994319a1d19SJiri Pirko 					      cls->common.extack))
99534297176SVladimir Oltean 		return -EOPNOTSUPP;
996319a1d19SJiri Pirko 
9979681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
998f50f2127SFlorian Fainelli 
99965722159SVladimir Oltean 	if (!act->dev)
100065722159SVladimir Oltean 		return -EINVAL;
100165722159SVladimir Oltean 
10029681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
1003f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
1004f50f2127SFlorian Fainelli 
1005f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1006f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1007f50f2127SFlorian Fainelli 		return -ENOMEM;
1008f50f2127SFlorian Fainelli 
1009f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
1010f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1011f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
1012f50f2127SFlorian Fainelli 
10139681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
1014f50f2127SFlorian Fainelli 
1015d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
1016f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
1017f50f2127SFlorian Fainelli 
1018d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
1019f50f2127SFlorian Fainelli 	if (err) {
1020f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
1021f50f2127SFlorian Fainelli 		return err;
1022f50f2127SFlorian Fainelli 	}
1023f50f2127SFlorian Fainelli 
1024f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1025e13c2075SVladimir Oltean 
1026e13c2075SVladimir Oltean 	return err;
1027f50f2127SFlorian Fainelli }
1028f50f2127SFlorian Fainelli 
102934297176SVladimir Oltean static int
103034297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
103134297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
103234297176SVladimir Oltean 				  bool ingress)
103334297176SVladimir Oltean {
103434297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
103534297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
103634297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
103734297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
103834297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
103934297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
104034297176SVladimir Oltean 	struct flow_action_entry *act;
104134297176SVladimir Oltean 	int err;
104234297176SVladimir Oltean 
104334297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
104434297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1045c75a33c8SJacob Keller 				   "Policing offload not implemented");
104634297176SVladimir Oltean 		return -EOPNOTSUPP;
104734297176SVladimir Oltean 	}
104834297176SVladimir Oltean 
104934297176SVladimir Oltean 	if (!ingress) {
105034297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1051c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
105234297176SVladimir Oltean 		return -EOPNOTSUPP;
105334297176SVladimir Oltean 	}
105434297176SVladimir Oltean 
105534297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
105634297176SVladimir Oltean 					      cls->common.extack))
105734297176SVladimir Oltean 		return -EOPNOTSUPP;
105834297176SVladimir Oltean 
105934297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
106034297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
106134297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1062c75a33c8SJacob Keller 					   "Only one port policer allowed");
106334297176SVladimir Oltean 			return -EEXIST;
106434297176SVladimir Oltean 		}
106534297176SVladimir Oltean 	}
106634297176SVladimir Oltean 
106734297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
106834297176SVladimir Oltean 
106934297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
107034297176SVladimir Oltean 	if (!mall_tc_entry)
107134297176SVladimir Oltean 		return -ENOMEM;
107234297176SVladimir Oltean 
107334297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
107434297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
107534297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
107634297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
107734297176SVladimir Oltean 	policer->burst = act->police.burst;
107834297176SVladimir Oltean 
107934297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
108034297176SVladimir Oltean 	if (err) {
108134297176SVladimir Oltean 		kfree(mall_tc_entry);
108234297176SVladimir Oltean 		return err;
108334297176SVladimir Oltean 	}
108434297176SVladimir Oltean 
108534297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
108634297176SVladimir Oltean 
108734297176SVladimir Oltean 	return err;
108834297176SVladimir Oltean }
108934297176SVladimir Oltean 
1090e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1091e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1092e13c2075SVladimir Oltean 				      bool ingress)
1093e13c2075SVladimir Oltean {
1094e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1095e13c2075SVladimir Oltean 
1096e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1097e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1098e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1099e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
110034297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
110134297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
110234297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1103e13c2075SVladimir Oltean 
1104e13c2075SVladimir Oltean 	return err;
1105f50f2127SFlorian Fainelli }
1106f50f2127SFlorian Fainelli 
1107f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1108f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1109f50f2127SFlorian Fainelli {
1110d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1111f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1112d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1113f50f2127SFlorian Fainelli 
11144fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1115f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1116f50f2127SFlorian Fainelli 		return;
1117f50f2127SFlorian Fainelli 
1118f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1119f50f2127SFlorian Fainelli 
1120f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1121f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
112234297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
112334297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
112434297176SVladimir Oltean 						 &mall_tc_entry->mirror);
112534297176SVladimir Oltean 		break;
112634297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
112734297176SVladimir Oltean 		if (ds->ops->port_policer_del)
112834297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1129f50f2127SFlorian Fainelli 		break;
1130f50f2127SFlorian Fainelli 	default:
1131f50f2127SFlorian Fainelli 		WARN_ON(1);
1132f50f2127SFlorian Fainelli 	}
1133f50f2127SFlorian Fainelli 
1134f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1135f50f2127SFlorian Fainelli }
1136f50f2127SFlorian Fainelli 
11373fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
11386b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
11396b3eb752SJiri Pirko 					   bool ingress)
1140f50f2127SFlorian Fainelli {
11415fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1142a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1143f50f2127SFlorian Fainelli 
11443fbae382SJiri Pirko 	switch (cls->command) {
11453fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
11465fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
11473fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
11483fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
11493fbae382SJiri Pirko 		return 0;
11503fbae382SJiri Pirko 	default:
11513fbae382SJiri Pirko 		return -EOPNOTSUPP;
11523fbae382SJiri Pirko 	}
11533fbae382SJiri Pirko }
11543fbae382SJiri Pirko 
1155ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1156ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1157ed11bb1fSVladimir Oltean 				    bool ingress)
1158ed11bb1fSVladimir Oltean {
1159ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1160ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1161ed11bb1fSVladimir Oltean 	int port = dp->index;
1162ed11bb1fSVladimir Oltean 
1163ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1164ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1165ed11bb1fSVladimir Oltean 
1166ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1167ed11bb1fSVladimir Oltean }
1168ed11bb1fSVladimir Oltean 
1169ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1170ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1171ed11bb1fSVladimir Oltean 				    bool ingress)
1172ed11bb1fSVladimir Oltean {
1173ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1174ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1175ed11bb1fSVladimir Oltean 	int port = dp->index;
1176ed11bb1fSVladimir Oltean 
1177ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1178ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1179ed11bb1fSVladimir Oltean 
1180ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1181ed11bb1fSVladimir Oltean }
1182ed11bb1fSVladimir Oltean 
1183ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1184ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1185ed11bb1fSVladimir Oltean 				      bool ingress)
1186ed11bb1fSVladimir Oltean {
1187ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1188ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1189ed11bb1fSVladimir Oltean 	int port = dp->index;
1190ed11bb1fSVladimir Oltean 
1191ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1192ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1193ed11bb1fSVladimir Oltean 
1194ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1195ed11bb1fSVladimir Oltean }
1196ed11bb1fSVladimir Oltean 
1197ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1198ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1199ed11bb1fSVladimir Oltean 					 bool ingress)
1200ed11bb1fSVladimir Oltean {
1201ed11bb1fSVladimir Oltean 	switch (cls->command) {
1202ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1203ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1204ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1205ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1206ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1207ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1208ed11bb1fSVladimir Oltean 	default:
1209ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1210ed11bb1fSVladimir Oltean 	}
1211ed11bb1fSVladimir Oltean }
1212ed11bb1fSVladimir Oltean 
12136b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
12146b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
12156b3eb752SJiri Pirko {
12166b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
12176b3eb752SJiri Pirko 
121844ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
121944ae12a7SJiri Pirko 		return -EOPNOTSUPP;
122044ae12a7SJiri Pirko 
12216b3eb752SJiri Pirko 	switch (type) {
12226b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
12236b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1224ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1225ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
12266b3eb752SJiri Pirko 	default:
12276b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12286b3eb752SJiri Pirko 	}
12296b3eb752SJiri Pirko }
12306b3eb752SJiri Pirko 
12316b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
12326b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12336b3eb752SJiri Pirko {
12346b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
12356b3eb752SJiri Pirko }
12366b3eb752SJiri Pirko 
12376b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
12386b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
12396b3eb752SJiri Pirko {
12406b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
12416b3eb752SJiri Pirko }
12426b3eb752SJiri Pirko 
1243955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1244955bcb6eSPablo Neira Ayuso 
12456b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1246955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
12476b3eb752SJiri Pirko {
1248955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1249a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
12506b3eb752SJiri Pirko 
125132f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
12526b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
125332f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
12546b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
12556b3eb752SJiri Pirko 	else
12566b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12576b3eb752SJiri Pirko 
1258955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1259955bcb6eSPablo Neira Ayuso 
12606b3eb752SJiri Pirko 	switch (f->command) {
12619c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
12620d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
12630d4fd02eSPablo Neira Ayuso 			return -EBUSY;
12640d4fd02eSPablo Neira Ayuso 
12650c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1266955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1267955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1268955bcb6eSPablo Neira Ayuso 
1269955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1270955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1271955bcb6eSPablo Neira Ayuso 		return 0;
12729c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
127314bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1274955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1275955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1276955bcb6eSPablo Neira Ayuso 
1277955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1278955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
12796b3eb752SJiri Pirko 		return 0;
12806b3eb752SJiri Pirko 	default:
12816b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12826b3eb752SJiri Pirko 	}
12836b3eb752SJiri Pirko }
12846b3eb752SJiri Pirko 
12853fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
12863fb24a43SPablo Neira Ayuso 				    void *type_data)
12873fb24a43SPablo Neira Ayuso {
12883fb24a43SPablo Neira Ayuso 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
12893fb24a43SPablo Neira Ayuso 	struct net_device *master = cpu_dp->master;
12903fb24a43SPablo Neira Ayuso 
12913fb24a43SPablo Neira Ayuso 	if (!master->netdev_ops->ndo_setup_tc)
12923fb24a43SPablo Neira Ayuso 		return -EOPNOTSUPP;
12933fb24a43SPablo Neira Ayuso 
12943fb24a43SPablo Neira Ayuso 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
12953fb24a43SPablo Neira Ayuso }
12963fb24a43SPablo Neira Ayuso 
12973fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1298de4784caSJiri Pirko 			      void *type_data)
12993fbae382SJiri Pirko {
130047d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
130147d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
130247d23af2SVladimir Oltean 
13033fb24a43SPablo Neira Ayuso 	switch (type) {
13043fb24a43SPablo Neira Ayuso 	case TC_SETUP_BLOCK:
13056b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
13063fb24a43SPablo Neira Ayuso 	case TC_SETUP_FT:
13073fb24a43SPablo Neira Ayuso 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
13083fb24a43SPablo Neira Ayuso 	default:
13093fb24a43SPablo Neira Ayuso 		break;
13103fb24a43SPablo Neira Ayuso 	}
131147d23af2SVladimir Oltean 
131247d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1313a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
131447d23af2SVladimir Oltean 
131547d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1316f50f2127SFlorian Fainelli }
1317f50f2127SFlorian Fainelli 
1318bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1319bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1320bf9f2648SFlorian Fainelli {
1321d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1322d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1323bf9f2648SFlorian Fainelli 
1324bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1325bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1326bf9f2648SFlorian Fainelli 
1327d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1328bf9f2648SFlorian Fainelli }
1329bf9f2648SFlorian Fainelli 
1330bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1331bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1332bf9f2648SFlorian Fainelli {
1333d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1334d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1335bf9f2648SFlorian Fainelli 
1336bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1337bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1338bf9f2648SFlorian Fainelli 
1339d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1340bf9f2648SFlorian Fainelli }
1341bf9f2648SFlorian Fainelli 
13420336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
13430336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
13440336369dSBrandon Streiff {
13450336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
13460336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
13470336369dSBrandon Streiff 
13480336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
13490336369dSBrandon Streiff 		return -EOPNOTSUPP;
13500336369dSBrandon Streiff 
13510336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
13520336369dSBrandon Streiff }
13530336369dSBrandon Streiff 
1354061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1355061f6a50SFlorian Fainelli 				     u16 vid)
1356061f6a50SFlorian Fainelli {
13572209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1358061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
135988236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
136088236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1361b7a9e0daSVladimir Oltean 		.vid = vid,
136288236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
136388236591SVladimir Oltean 		.flags = 0,
136488236591SVladimir Oltean 	};
136531046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1366061f6a50SFlorian Fainelli 	int ret;
1367061f6a50SFlorian Fainelli 
136888236591SVladimir Oltean 	/* User port... */
136931046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
137031046a5fSVladimir Oltean 	if (ret) {
137131046a5fSVladimir Oltean 		if (extack._msg)
137231046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
137388236591SVladimir Oltean 		return ret;
137431046a5fSVladimir Oltean 	}
137588236591SVladimir Oltean 
137688236591SVladimir Oltean 	/* And CPU port... */
137731046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
137831046a5fSVladimir Oltean 	if (ret) {
137931046a5fSVladimir Oltean 		if (extack._msg)
138031046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
138131046a5fSVladimir Oltean 				   extack._msg);
13827e1741b4SVivien Didelot 		return ret;
138331046a5fSVladimir Oltean 	}
13847e1741b4SVivien Didelot 
13852209158cSVladimir Oltean 	return vlan_vid_add(master, proto, vid);
1386061f6a50SFlorian Fainelli }
1387061f6a50SFlorian Fainelli 
1388061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1389061f6a50SFlorian Fainelli 				      u16 vid)
1390061f6a50SFlorian Fainelli {
13912209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1392061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
139388236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1394b7a9e0daSVladimir Oltean 		.vid = vid,
139588236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
139688236591SVladimir Oltean 		.flags = 0,
139788236591SVladimir Oltean 	};
13982209158cSVladimir Oltean 	int err;
1399061f6a50SFlorian Fainelli 
14007e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
14017e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
14027e1741b4SVivien Didelot 	 */
14032209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
14042209158cSVladimir Oltean 	if (err)
14052209158cSVladimir Oltean 		return err;
14062209158cSVladimir Oltean 
14072209158cSVladimir Oltean 	vlan_vid_del(master, proto, vid);
14082209158cSVladimir Oltean 
14092209158cSVladimir Oltean 	return 0;
1410061f6a50SFlorian Fainelli }
1411061f6a50SFlorian Fainelli 
141206cfb2dfSVladimir Oltean static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
141306cfb2dfSVladimir Oltean {
141406cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
141506cfb2dfSVladimir Oltean 
141606cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
141706cfb2dfSVladimir Oltean }
141806cfb2dfSVladimir Oltean 
141906cfb2dfSVladimir Oltean static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
142006cfb2dfSVladimir Oltean {
142106cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
142206cfb2dfSVladimir Oltean 
142306cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
142406cfb2dfSVladimir Oltean }
142506cfb2dfSVladimir Oltean 
142606cfb2dfSVladimir Oltean /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
142706cfb2dfSVladimir Oltean  * filtering is enabled. The baseline is that only ports that offload a
142806cfb2dfSVladimir Oltean  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
142906cfb2dfSVladimir Oltean  * but there are exceptions for quirky hardware.
143006cfb2dfSVladimir Oltean  *
143106cfb2dfSVladimir Oltean  * If ds->vlan_filtering_is_global = true, then standalone ports which share
143206cfb2dfSVladimir Oltean  * the same switch with other ports that offload a VLAN-aware bridge are also
143306cfb2dfSVladimir Oltean  * inevitably VLAN-aware.
143406cfb2dfSVladimir Oltean  *
143506cfb2dfSVladimir Oltean  * To summarize, a DSA switch port offloads:
143606cfb2dfSVladimir Oltean  *
143706cfb2dfSVladimir Oltean  * - If standalone (this includes software bridge, software LAG):
143858adf9dcSVladimir Oltean  *     - if ds->needs_standalone_vlan_filtering = true, OR if
143958adf9dcSVladimir Oltean  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
144058adf9dcSVladimir Oltean  *       this switch chip which have vlan_filtering=1)
144106cfb2dfSVladimir Oltean  *         - the 8021q upper VLANs
144258adf9dcSVladimir Oltean  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
144358adf9dcSVladimir Oltean  *       global, or it is, but no port is under a VLAN-aware bridge):
144406cfb2dfSVladimir Oltean  *         - no VLAN (any 8021q upper is a software VLAN)
144506cfb2dfSVladimir Oltean  *
144606cfb2dfSVladimir Oltean  * - If under a vlan_filtering=0 bridge which it offload:
144706cfb2dfSVladimir Oltean  *     - if ds->configure_vlan_while_not_filtering = true (default):
144806cfb2dfSVladimir Oltean  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
144906cfb2dfSVladimir Oltean  *     - else (deprecated):
145006cfb2dfSVladimir Oltean  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
145106cfb2dfSVladimir Oltean  *           enabled, so this behavior is broken and discouraged.
145206cfb2dfSVladimir Oltean  *
145306cfb2dfSVladimir Oltean  * - If under a vlan_filtering=1 bridge which it offload:
145406cfb2dfSVladimir Oltean  *     - the bridge VLANs
145506cfb2dfSVladimir Oltean  *     - the 8021q upper VLANs
145606cfb2dfSVladimir Oltean  */
145706cfb2dfSVladimir Oltean int dsa_slave_manage_vlan_filtering(struct net_device *slave,
145806cfb2dfSVladimir Oltean 				    bool vlan_filtering)
145906cfb2dfSVladimir Oltean {
146006cfb2dfSVladimir Oltean 	int err;
146106cfb2dfSVladimir Oltean 
146206cfb2dfSVladimir Oltean 	if (vlan_filtering) {
146306cfb2dfSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
146406cfb2dfSVladimir Oltean 
146506cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
146606cfb2dfSVladimir Oltean 		if (err) {
146706cfb2dfSVladimir Oltean 			vlan_for_each(slave, dsa_slave_clear_vlan, slave);
146806cfb2dfSVladimir Oltean 			slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
146906cfb2dfSVladimir Oltean 			return err;
147006cfb2dfSVladimir Oltean 		}
147106cfb2dfSVladimir Oltean 	} else {
147206cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
147306cfb2dfSVladimir Oltean 		if (err)
147406cfb2dfSVladimir Oltean 			return err;
147506cfb2dfSVladimir Oltean 
147606cfb2dfSVladimir Oltean 		slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
147706cfb2dfSVladimir Oltean 	}
147806cfb2dfSVladimir Oltean 
147906cfb2dfSVladimir Oltean 	return 0;
148006cfb2dfSVladimir Oltean }
148106cfb2dfSVladimir Oltean 
1482bff33f7eSVladimir Oltean struct dsa_hw_port {
1483bff33f7eSVladimir Oltean 	struct list_head list;
1484bff33f7eSVladimir Oltean 	struct net_device *dev;
1485bff33f7eSVladimir Oltean 	int old_mtu;
1486bff33f7eSVladimir Oltean };
1487bff33f7eSVladimir Oltean 
1488bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1489bff33f7eSVladimir Oltean {
1490bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1491bff33f7eSVladimir Oltean 	int err;
1492bff33f7eSVladimir Oltean 
1493bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1494bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1495bff33f7eSVladimir Oltean 			continue;
1496bff33f7eSVladimir Oltean 
1497bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1498bff33f7eSVladimir Oltean 		if (err)
1499bff33f7eSVladimir Oltean 			goto rollback;
1500bff33f7eSVladimir Oltean 	}
1501bff33f7eSVladimir Oltean 
1502bff33f7eSVladimir Oltean 	return 0;
1503bff33f7eSVladimir Oltean 
1504bff33f7eSVladimir Oltean rollback:
1505bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1506bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1507bff33f7eSVladimir Oltean 			continue;
1508bff33f7eSVladimir Oltean 
1509bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1510bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1511bff33f7eSVladimir Oltean 	}
1512bff33f7eSVladimir Oltean 
1513bff33f7eSVladimir Oltean 	return err;
1514bff33f7eSVladimir Oltean }
1515bff33f7eSVladimir Oltean 
1516bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1517bff33f7eSVladimir Oltean {
1518bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1519bff33f7eSVladimir Oltean 
1520bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1521bff33f7eSVladimir Oltean 		kfree(p);
1522bff33f7eSVladimir Oltean }
1523bff33f7eSVladimir Oltean 
1524bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1525bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1526bff33f7eSVladimir Oltean {
1527bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1528bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1529bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1530bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1531bff33f7eSVladimir Oltean 	int err;
1532bff33f7eSVladimir Oltean 
1533bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1534bff33f7eSVladimir Oltean 		return;
1535bff33f7eSVladimir Oltean 
1536bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1537bff33f7eSVladimir Oltean 		return;
1538bff33f7eSVladimir Oltean 
1539bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1540bff33f7eSVladimir Oltean 
1541bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1542bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1543bff33f7eSVladimir Oltean 	 */
1544bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1545bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1546bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1547bff33f7eSVladimir Oltean 			struct net_device *slave;
1548bff33f7eSVladimir Oltean 
1549bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1550bff33f7eSVladimir Oltean 				continue;
1551bff33f7eSVladimir Oltean 
1552bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1553bff33f7eSVladimir Oltean 				continue;
1554bff33f7eSVladimir Oltean 
1555bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1556bff33f7eSVladimir Oltean 				continue;
1557bff33f7eSVladimir Oltean 
1558bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1559bff33f7eSVladimir Oltean 
1560bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1561bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1562bff33f7eSVladimir Oltean 
1563bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1564bff33f7eSVladimir Oltean 			if (!hw_port)
1565bff33f7eSVladimir Oltean 				goto out;
1566bff33f7eSVladimir Oltean 
1567bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1568bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1569bff33f7eSVladimir Oltean 
1570bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1571bff33f7eSVladimir Oltean 		}
1572bff33f7eSVladimir Oltean 	}
1573bff33f7eSVladimir Oltean 
1574bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1575bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1576bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1577bff33f7eSVladimir Oltean 	 */
1578bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1579bff33f7eSVladimir Oltean 	if (!err)
1580bff33f7eSVladimir Oltean 		goto out;
1581bff33f7eSVladimir Oltean 
1582bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1583bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1584bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1585bff33f7eSVladimir Oltean 	 */
1586bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1587bff33f7eSVladimir Oltean 
1588bff33f7eSVladimir Oltean out:
1589bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1590bff33f7eSVladimir Oltean }
1591bff33f7eSVladimir Oltean 
159253da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1593bfcb8132SVladimir Oltean {
1594bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1595bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1596bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1597bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
15984e4ab795SVladimir Oltean 	struct dsa_port *dp_iter;
1599bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1600bfcb8132SVladimir Oltean 	int port = p->dp->index;
1601bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1602bfcb8132SVladimir Oltean 	int new_master_mtu;
1603bfcb8132SVladimir Oltean 	int old_master_mtu;
1604bfcb8132SVladimir Oltean 	int mtu_limit;
1605bfcb8132SVladimir Oltean 	int cpu_mtu;
16064e4ab795SVladimir Oltean 	int err;
1607bfcb8132SVladimir Oltean 
1608bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1609bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1610bfcb8132SVladimir Oltean 
16114e4ab795SVladimir Oltean 	list_for_each_entry(dp_iter, &ds->dst->ports, list) {
1612bfcb8132SVladimir Oltean 		int slave_mtu;
1613bfcb8132SVladimir Oltean 
16144e4ab795SVladimir Oltean 		if (!dsa_port_is_user(dp_iter))
1615bfcb8132SVladimir Oltean 			continue;
1616bfcb8132SVladimir Oltean 
1617bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1618bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1619bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1620bfcb8132SVladimir Oltean 		 */
16214e4ab795SVladimir Oltean 		if (!dp_iter->slave)
1622bfcb8132SVladimir Oltean 			continue;
1623bfcb8132SVladimir Oltean 
1624bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1625bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1626bfcb8132SVladimir Oltean 		 */
16274e4ab795SVladimir Oltean 		if (dp_iter == dp)
1628bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1629bfcb8132SVladimir Oltean 		else
16304e4ab795SVladimir Oltean 			slave_mtu = dp_iter->slave->mtu;
1631bfcb8132SVladimir Oltean 
1632bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1633bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1634bfcb8132SVladimir Oltean 	}
1635bfcb8132SVladimir Oltean 
1636bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1637bfcb8132SVladimir Oltean 
1638bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1639bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
16404e500251SVladimir Oltean 	new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1641bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1642bfcb8132SVladimir Oltean 		return -ERANGE;
1643bfcb8132SVladimir Oltean 
1644bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1645bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1646bfcb8132SVladimir Oltean 	 */
1647bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1648bfcb8132SVladimir Oltean 
1649bfcb8132SVladimir Oltean 	/* Start applying stuff */
1650bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1651bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1652bfcb8132SVladimir Oltean 		if (err < 0)
1653bfcb8132SVladimir Oltean 			goto out_master_failed;
1654bfcb8132SVladimir Oltean 
1655bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
165688faba20SVladimir Oltean 		 * upstream switches, so create a non-targeted notifier which
165788faba20SVladimir Oltean 		 * updates all switches.
1658bfcb8132SVladimir Oltean 		 */
165988faba20SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1660bfcb8132SVladimir Oltean 		if (err)
1661bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1662bfcb8132SVladimir Oltean 	}
1663bfcb8132SVladimir Oltean 
166488faba20SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, true);
1665bfcb8132SVladimir Oltean 	if (err)
1666bfcb8132SVladimir Oltean 		goto out_port_failed;
1667bfcb8132SVladimir Oltean 
1668bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1669bfcb8132SVladimir Oltean 
1670bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1671bff33f7eSVladimir Oltean 
1672bfcb8132SVladimir Oltean 	return 0;
1673bfcb8132SVladimir Oltean 
1674bfcb8132SVladimir Oltean out_port_failed:
1675bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1676bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
16774e500251SVladimir Oltean 				    dsa_tag_protocol_overhead(cpu_dp->tag_ops),
167888faba20SVladimir Oltean 				    false);
1679bfcb8132SVladimir Oltean out_cpu_failed:
1680bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1681bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1682bfcb8132SVladimir Oltean out_master_failed:
1683bfcb8132SVladimir Oltean 	return err;
1684bfcb8132SVladimir Oltean }
1685bfcb8132SVladimir Oltean 
168691da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
168791da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
16883d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
16893d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1690aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1691c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
16926793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
16936793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
16946793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
169591da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
169691da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
169791da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
169819e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
169919e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
17007905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
17017905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1702aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1703aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1704a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1705a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1706bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1707bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
17080336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
1709a71acad9SOleksij Rempel 	.self_test		= dsa_slave_net_selftest,
171091da11f8SLennert Buytenhek };
171191da11f8SLennert Buytenhek 
1712716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1713716efee2SJiri Pirko {
1714716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1715716efee2SJiri Pirko 
1716716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1717716efee2SJiri Pirko }
1718716efee2SJiri Pirko 
1719c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
1720c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
1721c2ec5f2eSOleksij Rempel {
1722c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
1723c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1724c2ec5f2eSOleksij Rempel 
1725c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
1726c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
1727c2ec5f2eSOleksij Rempel 	else
1728c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
1729c2ec5f2eSOleksij Rempel }
1730c2ec5f2eSOleksij Rempel 
17310994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
17320994d492SFelix Fietkau 				       struct net_device_path *path)
17330994d492SFelix Fietkau {
17340994d492SFelix Fietkau 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
17350994d492SFelix Fietkau 	struct dsa_port *cpu_dp = dp->cpu_dp;
17360994d492SFelix Fietkau 
17370994d492SFelix Fietkau 	path->dev = ctx->dev;
17380994d492SFelix Fietkau 	path->type = DEV_PATH_DSA;
17390994d492SFelix Fietkau 	path->dsa.proto = cpu_dp->tag_ops->proto;
17400994d492SFelix Fietkau 	path->dsa.port = dp->index;
17410994d492SFelix Fietkau 	ctx->dev = cpu_dp->master;
17420994d492SFelix Fietkau 
17430994d492SFelix Fietkau 	return 0;
17440994d492SFelix Fietkau }
17450994d492SFelix Fietkau 
17463e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1747d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1748d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
17493e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1750d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1751d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1752d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
17532bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1754a7605370SArnd Bergmann 	.ndo_eth_ioctl		= dsa_slave_ioctl,
1755abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
175604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
175704ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
175804ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
175904ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
176004ff53f9SFlorian Fainelli #endif
176144bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1762f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1763c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
1764929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1765061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1766061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1767716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1768bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
17690994d492SFelix Fietkau 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
177098237d43SScott Feldman };
177198237d43SScott Feldman 
1772f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1773f37db85dSFlorian Fainelli 	.name	= "dsa",
1774f37db85dSFlorian Fainelli };
1775f37db85dSFlorian Fainelli 
1776aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1777aab9c406SFlorian Fainelli {
1778aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1779aab9c406SFlorian Fainelli 
1780765bda93SRussell King 	if (dp->pl)
1781aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1782aab9c406SFlorian Fainelli }
1783aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1784aab9c406SFlorian Fainelli 
17855c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1786aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1787aab9c406SFlorian Fainelli {
17885c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1789aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1790aab9c406SFlorian Fainelli 
1791aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1792aab9c406SFlorian Fainelli 	 * not be called if it was not.
1793aab9c406SFlorian Fainelli 	 */
1794aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1795ce31b31cSFlorian Fainelli }
1796ce31b31cSFlorian Fainelli 
179791da11f8SLennert Buytenhek /* slave device setup *******************************************************/
1798c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
1799c916e8e1SOleksij Rempel 				 u32 flags)
1800c305c165SFlorian Fainelli {
1801d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1802d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1803c305c165SFlorian Fainelli 
18040115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
18050115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1806d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1807c305c165SFlorian Fainelli 		return -ENODEV;
1808d25b8e74SRussell King 	}
1809c305c165SFlorian Fainelli 
1810c916e8e1SOleksij Rempel 	slave_dev->phydev->dev_flags |= flags;
1811c916e8e1SOleksij Rempel 
1812aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1813c305c165SFlorian Fainelli }
1814c305c165SFlorian Fainelli 
18154fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
18160d8bcdd3SFlorian Fainelli {
1817d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1818d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1819d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
18200c65b2b9SAndrew Lunn 	phy_interface_t mode;
18216819563eSFlorian Fainelli 	u32 phy_flags = 0;
18220c65b2b9SAndrew Lunn 	int ret;
18230d8bcdd3SFlorian Fainelli 
18240c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
18250c65b2b9SAndrew Lunn 	if (ret)
182619334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
18270d8bcdd3SFlorian Fainelli 
182844cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
182944cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
183044cc27e4SIoana Ciornei 
18315c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
18325c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
18335c05c1dbSRussell King 	 * this if the switch provides such a callback.
18345c05c1dbSRussell King 	 */
18355c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
18365c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
18375c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
18385c05c1dbSRussell King 	}
18395c05c1dbSRussell King 
184044cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
184177373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1842aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1843aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1844aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1845aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1846aab9c406SFlorian Fainelli 	}
1847aab9c406SFlorian Fainelli 
18489d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1849d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
18506819563eSFlorian Fainelli 
1851aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
18526146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
18536146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
18546146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
18550d8bcdd3SFlorian Fainelli 		 */
1856c916e8e1SOleksij Rempel 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
1857d25b8e74SRussell King 	}
1858*6a52e733SVladimir Oltean 	if (ret) {
1859*6a52e733SVladimir Oltean 		netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
1860*6a52e733SVladimir Oltean 			   ERR_PTR(ret));
1861*6a52e733SVladimir Oltean 		phylink_destroy(dp->pl);
18620d8bcdd3SFlorian Fainelli 	}
18639697f1cdSFlorian Fainelli 
18646146dd45SVladimir Oltean 	return ret;
1865b31f65fbSAndrew Lunn }
18660d8bcdd3SFlorian Fainelli 
186753da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
186853da0ebaSVladimir Oltean {
186953da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
187053da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
187153da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
187253da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
187358adf9dcSVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
187453da0ebaSVladimir Oltean 
18754e500251SVladimir Oltean 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
18764e500251SVladimir Oltean 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
187753da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
187853da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
187953da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
188053da0ebaSVladimir Oltean 	 */
188153da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
188253da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
188353da0ebaSVladimir Oltean 
188453da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
188521cf377aSLino Sanfilippo 
188621cf377aSLino Sanfilippo 	slave->features = master->vlan_features | NETIF_F_HW_TC;
188721cf377aSLino Sanfilippo 	slave->hw_features |= NETIF_F_HW_TC;
188821cf377aSLino Sanfilippo 	slave->features |= NETIF_F_LLTX;
188921cf377aSLino Sanfilippo 	if (slave->needed_tailroom)
189021cf377aSLino Sanfilippo 		slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
189158adf9dcSVladimir Oltean 	if (ds->needs_standalone_vlan_filtering)
189258adf9dcSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
189353da0ebaSVladimir Oltean }
189453da0ebaSVladimir Oltean 
18951a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
18961a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
18971a33e10eSCong Wang 					    struct netdev_queue *txq,
18981a33e10eSCong Wang 					    void *_unused)
18991a33e10eSCong Wang {
19001a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
19011a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
19021a33e10eSCong Wang }
19031a33e10eSCong Wang 
190424462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
190524462549SFlorian Fainelli {
1906aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
190724462549SFlorian Fainelli 
1908a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1909a94c689eSFlorian Fainelli 		return 0;
1910a94c689eSFlorian Fainelli 
1911f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1912f154be24SFlorian Fainelli 
1913aab9c406SFlorian Fainelli 	rtnl_lock();
1914aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1915aab9c406SFlorian Fainelli 	rtnl_unlock();
191624462549SFlorian Fainelli 
191724462549SFlorian Fainelli 	return 0;
191824462549SFlorian Fainelli }
191924462549SFlorian Fainelli 
192024462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
192124462549SFlorian Fainelli {
1922aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1923aab9c406SFlorian Fainelli 
1924a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1925a94c689eSFlorian Fainelli 		return 0;
1926a94c689eSFlorian Fainelli 
192724462549SFlorian Fainelli 	netif_device_attach(slave_dev);
192824462549SFlorian Fainelli 
1929aab9c406SFlorian Fainelli 	rtnl_lock();
1930aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1931aab9c406SFlorian Fainelli 	rtnl_unlock();
193224462549SFlorian Fainelli 
193324462549SFlorian Fainelli 	return 0;
193424462549SFlorian Fainelli }
193524462549SFlorian Fainelli 
1936951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
193791da11f8SLennert Buytenhek {
193824a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1939f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
19404cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1941951259aaSVivien Didelot 	const char *name = port->name;
194291da11f8SLennert Buytenhek 	struct net_device *slave_dev;
194391da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
194491da11f8SLennert Buytenhek 	int ret;
194591da11f8SLennert Buytenhek 
194655199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
194755199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
194855199df6SFlorian Fainelli 
194955199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
195055199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
195155199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
195291da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1953d87d6f44SGuenter Roeck 		return -ENOMEM;
195491da11f8SLennert Buytenhek 
19557ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
195683216e39SMichael Walle 	if (!is_zero_ether_addr(port->mac))
1957a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1958a2c7023fSXiaofei Shen 	else
19592fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
19600a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
19613e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1962bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1963bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1964f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1965d442ad4aSStephen Hemminger 
19661a33e10eSCong Wang 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
19671a33e10eSCong Wang 				 NULL);
19681a33e10eSCong Wang 
19694cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
19704cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
197191da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
197291da11f8SLennert Buytenhek 
197391da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
19746a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
19756a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
19765f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
19775f6b4e14SFlorian Fainelli 		return -ENOMEM;
19785f6b4e14SFlorian Fainelli 	}
1979e131a563SAlexander Lobakin 
1980e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
1981e131a563SAlexander Lobakin 	if (ret)
1982e131a563SAlexander Lobakin 		goto out_free;
1983e131a563SAlexander Lobakin 
19844cfbf09cSVivien Didelot 	p->dp = port;
1985f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
1986f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
198753da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
198891da11f8SLennert Buytenhek 
1989bfcb8132SVladimir Oltean 	rtnl_lock();
1990bfcb8132SVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1991bfcb8132SVladimir Oltean 	rtnl_unlock();
19924349abdbSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
1993bdc40a3fSRasmus Villemoes 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1994bdc40a3fSRasmus Villemoes 			 ret, ETH_DATA_LEN, port->index);
1995bfcb8132SVladimir Oltean 
199691da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
199791da11f8SLennert Buytenhek 
19984fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
19990071f56eSAndrew Lunn 	if (ret) {
2000c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
2001c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2002c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
2003e131a563SAlexander Lobakin 		goto out_gcells;
2004e804441cSFlorian Fainelli 	}
2005e804441cSFlorian Fainelli 
20062f1e8ea7SVladimir Oltean 	rtnl_lock();
20072f1e8ea7SVladimir Oltean 
20082f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
2009e804441cSFlorian Fainelli 	if (ret) {
2010e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
2011e804441cSFlorian Fainelli 			   ret, slave_dev->name);
20122f1e8ea7SVladimir Oltean 		rtnl_unlock();
2013e804441cSFlorian Fainelli 		goto out_phy;
20140071f56eSAndrew Lunn 	}
20150071f56eSAndrew Lunn 
20162f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
20172f1e8ea7SVladimir Oltean 
20182f1e8ea7SVladimir Oltean 	rtnl_unlock();
20192f1e8ea7SVladimir Oltean 
20202f1e8ea7SVladimir Oltean 	if (ret)
20212f1e8ea7SVladimir Oltean 		goto out_unregister;
20222f1e8ea7SVladimir Oltean 
2023d87d6f44SGuenter Roeck 	return 0;
2024e804441cSFlorian Fainelli 
20252f1e8ea7SVladimir Oltean out_unregister:
20262f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
2027e804441cSFlorian Fainelli out_phy:
2028aab9c406SFlorian Fainelli 	rtnl_lock();
2029aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
2030aab9c406SFlorian Fainelli 	rtnl_unlock();
2031aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
2032e131a563SAlexander Lobakin out_gcells:
2033e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
2034e804441cSFlorian Fainelli out_free:
20356a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2036e804441cSFlorian Fainelli 	free_netdev(slave_dev);
2037f8b8b1cdSVivien Didelot 	port->slave = NULL;
2038e804441cSFlorian Fainelli 	return ret;
203991da11f8SLennert Buytenhek }
2040b73adef6SFlorian Fainelli 
2041cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
2042cda5c15bSNeil Armstrong {
20432f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
2044d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2045cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2046cda5c15bSNeil Armstrong 
2047cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
2048aab9c406SFlorian Fainelli 	rtnl_lock();
20492f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
20502f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
2051aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
2052aab9c406SFlorian Fainelli 	rtnl_unlock();
2053881eadabSJohan Hovold 
2054aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
2055e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
20566a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2057cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
2058cda5c15bSNeil Armstrong }
2059cda5c15bSNeil Armstrong 
20604d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
2061b73adef6SFlorian Fainelli {
2062b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2063b73adef6SFlorian Fainelli }
2064a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2065b73adef6SFlorian Fainelli 
20668e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
20678e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
2068b73adef6SFlorian Fainelli {
2069d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
20702afc526aSVladimir Oltean 	struct netlink_ext_ack *extack;
20718e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
2072b73adef6SFlorian Fainelli 
20732afc526aSVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
20742afc526aSVladimir Oltean 
20758e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
20768e92ab3aSVivien Didelot 		if (info->linking) {
20772afc526aSVladimir Oltean 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2078bff33f7eSVladimir Oltean 			if (!err)
2079bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
208067b5fb5dSVladimir Oltean 			if (err == -EOPNOTSUPP) {
208167b5fb5dSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
208267b5fb5dSVladimir Oltean 						   "Offloading not supported");
208367b5fb5dSVladimir Oltean 				err = 0;
208467b5fb5dSVladimir Oltean 			}
20858e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
20868e92ab3aSVivien Didelot 		} else {
208717d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
20888e92ab3aSVivien Didelot 			err = NOTIFY_OK;
20898e92ab3aSVivien Didelot 		}
2090058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
2091058102a6STobias Waldekranz 		if (info->linking) {
2092058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
20932afc526aSVladimir Oltean 						info->upper_info, extack);
2094058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
2095058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
2096058102a6STobias Waldekranz 						   "Offloading not supported");
2097058102a6STobias Waldekranz 				err = 0;
2098058102a6STobias Waldekranz 			}
2099058102a6STobias Waldekranz 			err = notifier_from_errno(err);
2100058102a6STobias Waldekranz 		} else {
2101058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
2102058102a6STobias Waldekranz 			err = NOTIFY_OK;
2103058102a6STobias Waldekranz 		}
210418596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
210518596f50SGeorge McCollister 		if (info->linking) {
210618596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
210718596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
210818596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
210918596f50SGeorge McCollister 						   "Offloading not supported");
211018596f50SGeorge McCollister 				err = 0;
211118596f50SGeorge McCollister 			}
211218596f50SGeorge McCollister 			err = notifier_from_errno(err);
211318596f50SGeorge McCollister 		} else {
211418596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
211518596f50SGeorge McCollister 			err = NOTIFY_OK;
211618596f50SGeorge McCollister 		}
2117058102a6STobias Waldekranz 	}
2118058102a6STobias Waldekranz 
2119058102a6STobias Waldekranz 	return err;
2120058102a6STobias Waldekranz }
2121058102a6STobias Waldekranz 
212274918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev,
212374918945SVladimir Oltean 				    struct netdev_notifier_changeupper_info *info)
212474918945SVladimir Oltean {
212574918945SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
212674918945SVladimir Oltean 
212774918945SVladimir Oltean 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
21284e51bf44SVladimir Oltean 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
212974918945SVladimir Oltean 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
21304e51bf44SVladimir Oltean 		dsa_port_pre_lag_leave(dp, info->upper_dev);
213174918945SVladimir Oltean 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
213274918945SVladimir Oltean 	 * meaningfully enslaved to a bridge yet
213374918945SVladimir Oltean 	 */
213474918945SVladimir Oltean 
21354e51bf44SVladimir Oltean 	return NOTIFY_DONE;
213674918945SVladimir Oltean }
213774918945SVladimir Oltean 
2138058102a6STobias Waldekranz static int
2139058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
2140058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
2141058102a6STobias Waldekranz {
2142058102a6STobias Waldekranz 	struct net_device *lower;
2143058102a6STobias Waldekranz 	struct list_head *iter;
2144058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
2145058102a6STobias Waldekranz 	struct dsa_port *dp;
2146058102a6STobias Waldekranz 
2147058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
2148058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
2149058102a6STobias Waldekranz 			continue;
2150058102a6STobias Waldekranz 
2151058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
2152058102a6STobias Waldekranz 		if (!dp->lag_dev)
2153058102a6STobias Waldekranz 			/* Software LAG */
2154058102a6STobias Waldekranz 			continue;
2155058102a6STobias Waldekranz 
2156058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
2157058102a6STobias Waldekranz 		if (notifier_to_errno(err))
2158058102a6STobias Waldekranz 			break;
21596debb68aSVivien Didelot 	}
2160b73adef6SFlorian Fainelli 
21618e92ab3aSVivien Didelot 	return err;
2162b73adef6SFlorian Fainelli }
2163b73adef6SFlorian Fainelli 
216474918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls
216574918945SVladimir Oltean  * dsa_slave_prechangeupper()
216674918945SVladimir Oltean  */
216774918945SVladimir Oltean static int
216874918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev,
216974918945SVladimir Oltean 			     struct netdev_notifier_changeupper_info *info)
217074918945SVladimir Oltean {
217174918945SVladimir Oltean 	struct net_device *lower;
217274918945SVladimir Oltean 	struct list_head *iter;
217374918945SVladimir Oltean 	int err = NOTIFY_DONE;
217474918945SVladimir Oltean 	struct dsa_port *dp;
217574918945SVladimir Oltean 
217674918945SVladimir Oltean 	netdev_for_each_lower_dev(dev, lower, iter) {
217774918945SVladimir Oltean 		if (!dsa_slave_dev_check(lower))
217874918945SVladimir Oltean 			continue;
217974918945SVladimir Oltean 
218074918945SVladimir Oltean 		dp = dsa_slave_to_port(lower);
218174918945SVladimir Oltean 		if (!dp->lag_dev)
218274918945SVladimir Oltean 			/* Software LAG */
218374918945SVladimir Oltean 			continue;
218474918945SVladimir Oltean 
218574918945SVladimir Oltean 		err = dsa_slave_prechangeupper(lower, info);
218674918945SVladimir Oltean 		if (notifier_to_errno(err))
218774918945SVladimir Oltean 			break;
218874918945SVladimir Oltean 	}
218974918945SVladimir Oltean 
219074918945SVladimir Oltean 	return err;
219174918945SVladimir Oltean }
219274918945SVladimir Oltean 
2193eb46e8daSVladimir Oltean static int
2194eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2195eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2196cc1d5bdaSFlorian Fainelli {
2197cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
2198cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
2199cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2200cc1d5bdaSFlorian Fainelli 
2201cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2202cc1d5bdaSFlorian Fainelli 
2203cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2204cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2205cc1d5bdaSFlorian Fainelli 
2206cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2207cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2208cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2209cc1d5bdaSFlorian Fainelli 
2210cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
2211cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
2212cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2213cc1d5bdaSFlorian Fainelli 
2214cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2215cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
2216cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2217cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2218cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2219cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2220cc1d5bdaSFlorian Fainelli 	}
2221cc1d5bdaSFlorian Fainelli 
2222cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2223cc1d5bdaSFlorian Fainelli }
2224cc1d5bdaSFlorian Fainelli 
22252b138406SVladimir Oltean static int
22262b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
22272b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
22282b138406SVladimir Oltean {
22292b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
22302b138406SVladimir Oltean 	struct net_device *br = dp->bridge_dev;
22312b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
22322b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
22332b138406SVladimir Oltean 	int err = NOTIFY_DONE;
22342b138406SVladimir Oltean 	u16 vid;
22352b138406SVladimir Oltean 
2236adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
22372b138406SVladimir Oltean 		return NOTIFY_DONE;
22382b138406SVladimir Oltean 
22392b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
22402b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
22412b138406SVladimir Oltean 
22422b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
22432b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
22442b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
22452b138406SVladimir Oltean 	 */
22462b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
22472b138406SVladimir Oltean 	if (err == 0) {
22482b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
22492b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
22502b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
22512b138406SVladimir Oltean 	}
22522b138406SVladimir Oltean 
22532b138406SVladimir Oltean 	return NOTIFY_DONE;
22542b138406SVladimir Oltean }
22552b138406SVladimir Oltean 
22564ede74e7SVladimir Oltean static int
22574ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
22584ede74e7SVladimir Oltean 				      struct netdev_notifier_changeupper_info *info)
2259b73adef6SFlorian Fainelli {
2260e358bef7SVladimir Oltean 	struct dsa_switch *ds;
2261e358bef7SVladimir Oltean 	struct dsa_port *dp;
2262e358bef7SVladimir Oltean 	int err;
22632b138406SVladimir Oltean 
226453bade8aSFlorian Fainelli 	if (!dsa_slave_dev_check(dev))
22654ede74e7SVladimir Oltean 		return dsa_prevent_bridging_8021q_upper(dev, info);
22662b138406SVladimir Oltean 
2267e358bef7SVladimir Oltean 	dp = dsa_slave_to_port(dev);
2268e358bef7SVladimir Oltean 	ds = dp->ds;
2269e358bef7SVladimir Oltean 
2270e358bef7SVladimir Oltean 	if (ds->ops->port_prechangeupper) {
2271e358bef7SVladimir Oltean 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
2272e358bef7SVladimir Oltean 		if (err)
2273e358bef7SVladimir Oltean 			return notifier_from_errno(err);
2274e358bef7SVladimir Oltean 	}
2275e358bef7SVladimir Oltean 
22762b138406SVladimir Oltean 	if (is_vlan_dev(info->upper_dev))
22774ede74e7SVladimir Oltean 		return dsa_slave_check_8021q_upper(dev, info);
22784ede74e7SVladimir Oltean 
22794ede74e7SVladimir Oltean 	return NOTIFY_DONE;
22804ede74e7SVladimir Oltean }
22814ede74e7SVladimir Oltean 
22824ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb,
22834ede74e7SVladimir Oltean 				     unsigned long event, void *ptr)
22844ede74e7SVladimir Oltean {
22854ede74e7SVladimir Oltean 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
22864ede74e7SVladimir Oltean 
22874ede74e7SVladimir Oltean 	switch (event) {
22884ede74e7SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
22894ede74e7SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
22904ede74e7SVladimir Oltean 		int err;
22914ede74e7SVladimir Oltean 
22924ede74e7SVladimir Oltean 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
22934ede74e7SVladimir Oltean 		if (err != NOTIFY_DONE)
22944ede74e7SVladimir Oltean 			return err;
22954ede74e7SVladimir Oltean 
229674918945SVladimir Oltean 		if (dsa_slave_dev_check(dev))
229774918945SVladimir Oltean 			return dsa_slave_prechangeupper(dev, ptr);
229874918945SVladimir Oltean 
229974918945SVladimir Oltean 		if (netif_is_lag_master(dev))
230074918945SVladimir Oltean 			return dsa_slave_lag_prechangeupper(dev, ptr);
230174918945SVladimir Oltean 
230283501299SVladimir Oltean 		break;
23032b138406SVladimir Oltean 	}
230483501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2305058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
23068e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2307058102a6STobias Waldekranz 
2308058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2309058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2310058102a6STobias Waldekranz 
2311058102a6STobias Waldekranz 		break;
2312058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2313058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2314058102a6STobias Waldekranz 		struct dsa_port *dp;
2315058102a6STobias Waldekranz 		int err;
2316058102a6STobias Waldekranz 
2317058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2318058102a6STobias Waldekranz 			break;
2319058102a6STobias Waldekranz 
2320058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2321058102a6STobias Waldekranz 
2322058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2323058102a6STobias Waldekranz 		return notifier_from_errno(err);
2324058102a6STobias Waldekranz 	}
2325c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2326c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2327c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2328c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2329c0a8a9c2SVladimir Oltean 
2330c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2331c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2332c0a8a9c2SVladimir Oltean 
2333c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2334c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2335c0a8a9c2SVladimir Oltean 
2336c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2337c0a8a9c2SVladimir Oltean 			if (!dsa_is_user_port(dp->ds, dp->index))
2338c0a8a9c2SVladimir Oltean 				continue;
2339c0a8a9c2SVladimir Oltean 
2340c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2341c0a8a9c2SVladimir Oltean 		}
2342c0a8a9c2SVladimir Oltean 
2343c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2344c0a8a9c2SVladimir Oltean 
2345c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2346c0a8a9c2SVladimir Oltean 	}
2347c0a8a9c2SVladimir Oltean 	default:
2348c0a8a9c2SVladimir Oltean 		break;
2349cc1d5bdaSFlorian Fainelli 	}
2350b73adef6SFlorian Fainelli 
2351b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2352b73adef6SFlorian Fainelli }
235388e4f0caSVivien Didelot 
2354c4bb76a9SVladimir Oltean static void
2355c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2356c4bb76a9SVladimir Oltean {
2357c35b57ceSVladimir Oltean 	struct switchdev_notifier_fdb_info info = {};
2358c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2359c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2360c4bb76a9SVladimir Oltean 
2361c4bb76a9SVladimir Oltean 	if (!dsa_is_user_port(ds, switchdev_work->port))
2362c4bb76a9SVladimir Oltean 		return;
2363c4bb76a9SVladimir Oltean 
2364c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2365c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2366c4bb76a9SVladimir Oltean 	info.offloaded = true;
2367c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2368c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2369c4bb76a9SVladimir Oltean 				 dp->slave, &info.info, NULL);
2370c4bb76a9SVladimir Oltean }
2371c9eb3e0fSArkadi Sharshevsky 
2372c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2373c9eb3e0fSArkadi Sharshevsky {
2374c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2375c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
2376c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2377c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2378c9eb3e0fSArkadi Sharshevsky 	int err;
2379c9eb3e0fSArkadi Sharshevsky 
2380c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2381c4bb76a9SVladimir Oltean 
2382c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
2383c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2384c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
23853dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
23863dc80afcSVladimir Oltean 			err = dsa_port_host_fdb_add(dp, switchdev_work->addr,
23873dc80afcSVladimir Oltean 						    switchdev_work->vid);
23883dc80afcSVladimir Oltean 		else
2389c4bb76a9SVladimir Oltean 			err = dsa_port_fdb_add(dp, switchdev_work->addr,
2390c4bb76a9SVladimir Oltean 					       switchdev_work->vid);
2391c9eb3e0fSArkadi Sharshevsky 		if (err) {
2392c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2393c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
2394c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2395c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2396c9eb3e0fSArkadi Sharshevsky 			break;
2397c9eb3e0fSArkadi Sharshevsky 		}
2398c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2399c9eb3e0fSArkadi Sharshevsky 		break;
2400c9eb3e0fSArkadi Sharshevsky 
2401c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
24023dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
24033dc80afcSVladimir Oltean 			err = dsa_port_host_fdb_del(dp, switchdev_work->addr,
24043dc80afcSVladimir Oltean 						    switchdev_work->vid);
24053dc80afcSVladimir Oltean 		else
2406c4bb76a9SVladimir Oltean 			err = dsa_port_fdb_del(dp, switchdev_work->addr,
2407c4bb76a9SVladimir Oltean 					       switchdev_work->vid);
2408c9eb3e0fSArkadi Sharshevsky 		if (err) {
2409c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2410c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2411c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2412c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2413c9eb3e0fSArkadi Sharshevsky 		}
24142fd18650SVladimir Oltean 
2415c9eb3e0fSArkadi Sharshevsky 		break;
2416c9eb3e0fSArkadi Sharshevsky 	}
2417c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
2418c9eb3e0fSArkadi Sharshevsky 
24194bed397cSVladimir Oltean 	dev_put(switchdev_work->dev);
2420c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2421c9eb3e0fSArkadi Sharshevsky }
2422c9eb3e0fSArkadi Sharshevsky 
2423b94dc99cSVladimir Oltean static bool dsa_foreign_dev_check(const struct net_device *dev,
2424b94dc99cSVladimir Oltean 				  const struct net_device *foreign_dev)
2425d5f19486SVladimir Oltean {
2426b94dc99cSVladimir Oltean 	const struct dsa_port *dp = dsa_slave_to_port(dev);
2427b94dc99cSVladimir Oltean 	struct dsa_switch_tree *dst = dp->ds->dst;
2428b94dc99cSVladimir Oltean 
2429b94dc99cSVladimir Oltean 	if (netif_is_bridge_master(foreign_dev))
2430b94dc99cSVladimir Oltean 		return !dsa_tree_offloads_bridge(dst, foreign_dev);
2431b94dc99cSVladimir Oltean 
2432b94dc99cSVladimir Oltean 	if (netif_is_bridge_port(foreign_dev))
2433b94dc99cSVladimir Oltean 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2434b94dc99cSVladimir Oltean 
2435b94dc99cSVladimir Oltean 	/* Everything else is foreign */
2436b94dc99cSVladimir Oltean 	return true;
2437d5f19486SVladimir Oltean }
2438d5f19486SVladimir Oltean 
2439b94dc99cSVladimir Oltean static int dsa_slave_fdb_event(struct net_device *dev,
2440b94dc99cSVladimir Oltean 			       const struct net_device *orig_dev,
2441b94dc99cSVladimir Oltean 			       const void *ctx,
2442b94dc99cSVladimir Oltean 			       const struct switchdev_notifier_fdb_info *fdb_info,
2443b94dc99cSVladimir Oltean 			       unsigned long event)
2444b94dc99cSVladimir Oltean {
2445b94dc99cSVladimir Oltean 	struct dsa_switchdev_event_work *switchdev_work;
2446b94dc99cSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2447b94dc99cSVladimir Oltean 	bool host_addr = fdb_info->is_local;
2448b94dc99cSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
2449b94dc99cSVladimir Oltean 
2450b94dc99cSVladimir Oltean 	if (ctx && ctx != dp)
2451b94dc99cSVladimir Oltean 		return 0;
2452b94dc99cSVladimir Oltean 
2453b94dc99cSVladimir Oltean 	if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2454b94dc99cSVladimir Oltean 		return -EOPNOTSUPP;
2455b94dc99cSVladimir Oltean 
2456b94dc99cSVladimir Oltean 	if (dsa_slave_dev_check(orig_dev) &&
2457b94dc99cSVladimir Oltean 	    switchdev_fdb_is_dynamically_learned(fdb_info))
2458b94dc99cSVladimir Oltean 		return 0;
2459b94dc99cSVladimir Oltean 
2460b94dc99cSVladimir Oltean 	/* FDB entries learned by the software bridge should be installed as
2461b94dc99cSVladimir Oltean 	 * host addresses only if the driver requests assisted learning.
2462b94dc99cSVladimir Oltean 	 */
2463b94dc99cSVladimir Oltean 	if (switchdev_fdb_is_dynamically_learned(fdb_info) &&
2464b94dc99cSVladimir Oltean 	    !ds->assisted_learning_on_cpu_port)
2465b94dc99cSVladimir Oltean 		return 0;
2466b94dc99cSVladimir Oltean 
2467b94dc99cSVladimir Oltean 	/* Also treat FDB entries on foreign interfaces bridged with us as host
2468b94dc99cSVladimir Oltean 	 * addresses.
2469b94dc99cSVladimir Oltean 	 */
2470b94dc99cSVladimir Oltean 	if (dsa_foreign_dev_check(dev, orig_dev))
2471b94dc99cSVladimir Oltean 		host_addr = true;
2472b94dc99cSVladimir Oltean 
2473b94dc99cSVladimir Oltean 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2474b94dc99cSVladimir Oltean 	if (!switchdev_work)
2475b94dc99cSVladimir Oltean 		return -ENOMEM;
2476b94dc99cSVladimir Oltean 
2477b94dc99cSVladimir Oltean 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2478b94dc99cSVladimir Oltean 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2479b94dc99cSVladimir Oltean 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
2480b94dc99cSVladimir Oltean 		   host_addr ? " as host address" : "");
2481b94dc99cSVladimir Oltean 
2482b94dc99cSVladimir Oltean 	INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2483b94dc99cSVladimir Oltean 	switchdev_work->ds = ds;
2484b94dc99cSVladimir Oltean 	switchdev_work->port = dp->index;
2485b94dc99cSVladimir Oltean 	switchdev_work->event = event;
2486b94dc99cSVladimir Oltean 	switchdev_work->dev = dev;
2487b94dc99cSVladimir Oltean 
2488b94dc99cSVladimir Oltean 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2489b94dc99cSVladimir Oltean 	switchdev_work->vid = fdb_info->vid;
2490b94dc99cSVladimir Oltean 	switchdev_work->host_addr = host_addr;
2491b94dc99cSVladimir Oltean 
2492b94dc99cSVladimir Oltean 	/* Hold a reference for dsa_fdb_offload_notify */
2493b94dc99cSVladimir Oltean 	dev_hold(dev);
2494b94dc99cSVladimir Oltean 	dsa_schedule_work(&switchdev_work->work);
2495b94dc99cSVladimir Oltean 
2496d5f19486SVladimir Oltean 	return 0;
2497d5f19486SVladimir Oltean }
2498d5f19486SVladimir Oltean 
2499b94dc99cSVladimir Oltean static int
2500b94dc99cSVladimir Oltean dsa_slave_fdb_add_to_device(struct net_device *dev,
2501b94dc99cSVladimir Oltean 			    const struct net_device *orig_dev, const void *ctx,
2502b94dc99cSVladimir Oltean 			    const struct switchdev_notifier_fdb_info *fdb_info)
2503d5f19486SVladimir Oltean {
2504b94dc99cSVladimir Oltean 	return dsa_slave_fdb_event(dev, orig_dev, ctx, fdb_info,
2505b94dc99cSVladimir Oltean 				   SWITCHDEV_FDB_ADD_TO_DEVICE);
2506b94dc99cSVladimir Oltean }
2507d5f19486SVladimir Oltean 
2508b94dc99cSVladimir Oltean static int
2509b94dc99cSVladimir Oltean dsa_slave_fdb_del_to_device(struct net_device *dev,
2510b94dc99cSVladimir Oltean 			    const struct net_device *orig_dev, const void *ctx,
2511b94dc99cSVladimir Oltean 			    const struct switchdev_notifier_fdb_info *fdb_info)
2512b94dc99cSVladimir Oltean {
2513b94dc99cSVladimir Oltean 	return dsa_slave_fdb_event(dev, orig_dev, ctx, fdb_info,
2514b94dc99cSVladimir Oltean 				   SWITCHDEV_FDB_DEL_TO_DEVICE);
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:
2531b94dc99cSVladimir Oltean 		err = switchdev_handle_fdb_add_to_device(dev, ptr,
2532b94dc99cSVladimir Oltean 							 dsa_slave_dev_check,
2533b94dc99cSVladimir Oltean 							 dsa_foreign_dev_check,
2534b94dc99cSVladimir Oltean 							 dsa_slave_fdb_add_to_device,
2535b94dc99cSVladimir Oltean 							 NULL);
2536b94dc99cSVladimir Oltean 		return notifier_from_errno(err);
2537447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2538b94dc99cSVladimir Oltean 		err = switchdev_handle_fdb_del_to_device(dev, ptr,
2539b94dc99cSVladimir Oltean 							 dsa_slave_dev_check,
2540b94dc99cSVladimir Oltean 							 dsa_foreign_dev_check,
2541b94dc99cSVladimir Oltean 							 dsa_slave_fdb_del_to_device,
2542b94dc99cSVladimir Oltean 							 NULL);
2543b94dc99cSVladimir Oltean 		return notifier_from_errno(err);
2544c9eb3e0fSArkadi Sharshevsky 	default:
2545c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2546c9eb3e0fSArkadi Sharshevsky 	}
2547c9eb3e0fSArkadi Sharshevsky 
2548c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2549c9eb3e0fSArkadi Sharshevsky }
2550c9eb3e0fSArkadi Sharshevsky 
25512b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
25522b239f67SPetr Machata 					      unsigned long event, void *ptr)
25532b239f67SPetr Machata {
25542b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
255579b139f4SVivien Didelot 	int err;
25562b239f67SPetr Machata 
25572b239f67SPetr Machata 	switch (event) {
255879b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
255979b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
256079b139f4SVivien Didelot 						    dsa_slave_dev_check,
256179b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
256279b139f4SVivien Didelot 		return notifier_from_errno(err);
25632b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
256479b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
256579b139f4SVivien Didelot 						    dsa_slave_dev_check,
256679b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
256779b139f4SVivien Didelot 		return notifier_from_errno(err);
25689ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
256979b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
257079b139f4SVivien Didelot 						     dsa_slave_dev_check,
257179b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
257279b139f4SVivien Didelot 		return notifier_from_errno(err);
25732b239f67SPetr Machata 	}
25742b239f67SPetr Machata 
25752b239f67SPetr Machata 	return NOTIFY_DONE;
25762b239f67SPetr Machata }
25772b239f67SPetr Machata 
257888e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
257988e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
258088e4f0caSVivien Didelot };
258188e4f0caSVivien Didelot 
2582010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = {
2583c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2584c9eb3e0fSArkadi Sharshevsky };
2585c9eb3e0fSArkadi Sharshevsky 
2586010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = {
25872b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
25882b239f67SPetr Machata };
25892b239f67SPetr Machata 
259088e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
259188e4f0caSVivien Didelot {
25922b239f67SPetr Machata 	struct notifier_block *nb;
2593c9eb3e0fSArkadi Sharshevsky 	int err;
2594c9eb3e0fSArkadi Sharshevsky 
2595c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2596c9eb3e0fSArkadi Sharshevsky 	if (err)
2597c9eb3e0fSArkadi Sharshevsky 		return err;
2598c9eb3e0fSArkadi Sharshevsky 
2599c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2600c9eb3e0fSArkadi Sharshevsky 	if (err)
2601c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2602c9eb3e0fSArkadi Sharshevsky 
26032b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
26042b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
26052b239f67SPetr Machata 	if (err)
26062b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
26072b239f67SPetr Machata 
2608c9eb3e0fSArkadi Sharshevsky 	return 0;
2609c9eb3e0fSArkadi Sharshevsky 
26102b239f67SPetr Machata err_switchdev_blocking_nb:
26112b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2612c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2613c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2614c9eb3e0fSArkadi Sharshevsky 	return err;
261588e4f0caSVivien Didelot }
261688e4f0caSVivien Didelot 
261788e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
261888e4f0caSVivien Didelot {
26192b239f67SPetr Machata 	struct notifier_block *nb;
262088e4f0caSVivien Didelot 	int err;
262188e4f0caSVivien Didelot 
26222b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
26232b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
26242b239f67SPetr Machata 	if (err)
26252b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
26262b239f67SPetr Machata 
2627c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2628c9eb3e0fSArkadi Sharshevsky 	if (err)
2629c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2630c9eb3e0fSArkadi Sharshevsky 
263188e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
263288e4f0caSVivien Didelot 	if (err)
263388e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
263488e4f0caSVivien Didelot }
2635