xref: /openbmc/linux/net/dsa/slave.c (revision 89153ed6ebc14879b04686f0e3f3066b1b6bef05)
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>
18f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
19b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2018596f50SGeorge McCollister #include <linux/if_hsr.h>
2104ff53f9SFlorian Fainelli #include <linux/netpoll.h>
2290af1059SBrandon Streiff #include <linux/ptp_classify.h>
23ea5dd34bSVivien Didelot 
2491da11f8SLennert Buytenhek #include "dsa_priv.h"
2591da11f8SLennert Buytenhek 
2691da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2791da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2891da11f8SLennert Buytenhek {
2991da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3091da11f8SLennert Buytenhek 
310d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
329d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3391da11f8SLennert Buytenhek 
3491da11f8SLennert Buytenhek 	return 0xffff;
3591da11f8SLennert Buytenhek }
3691da11f8SLennert Buytenhek 
3791da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3891da11f8SLennert Buytenhek {
3991da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4091da11f8SLennert Buytenhek 
410d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
429d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4391da11f8SLennert Buytenhek 
4491da11f8SLennert Buytenhek 	return 0;
4591da11f8SLennert Buytenhek }
4691da11f8SLennert Buytenhek 
4791da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4891da11f8SLennert Buytenhek {
4991da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
5091da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5191da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5291da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
530b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
5449463b7fSVivien Didelot 		 ds->dst->index, ds->index);
55c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
5624df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
5791da11f8SLennert Buytenhek }
5891da11f8SLennert Buytenhek 
5991da11f8SLennert Buytenhek 
6091da11f8SLennert Buytenhek /* slave device handling ****************************************************/
61abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
62c0840801SLennert Buytenhek {
63d0006b00SVivien Didelot 	return dsa_slave_to_master(dev)->ifindex;
64c0840801SLennert Buytenhek }
65c0840801SLennert Buytenhek 
6691da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6791da11f8SLennert Buytenhek {
68d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
69d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
70df02c6ffSLennert Buytenhek 	int err;
71df02c6ffSLennert Buytenhek 
729d5ef190SVladimir Oltean 	err = dev_open(master, NULL);
739d5ef190SVladimir Oltean 	if (err < 0) {
749d5ef190SVladimir Oltean 		netdev_err(dev, "failed to open master %s\n", master->name);
759d5ef190SVladimir Oltean 		goto out;
769d5ef190SVladimir Oltean 	}
77df02c6ffSLennert Buytenhek 
788feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
79a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
80df02c6ffSLennert Buytenhek 		if (err < 0)
81df02c6ffSLennert Buytenhek 			goto out;
82df02c6ffSLennert Buytenhek 	}
83df02c6ffSLennert Buytenhek 
84df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
85df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
86df02c6ffSLennert Buytenhek 		if (err < 0)
87df02c6ffSLennert Buytenhek 			goto del_unicast;
88df02c6ffSLennert Buytenhek 	}
89df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
90df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
91df02c6ffSLennert Buytenhek 		if (err < 0)
92df02c6ffSLennert Buytenhek 			goto clear_allmulti;
93df02c6ffSLennert Buytenhek 	}
94df02c6ffSLennert Buytenhek 
958640f8dcSRussell King 	err = dsa_port_enable_rt(dp, dev->phydev);
96b2f2af21SFlorian Fainelli 	if (err)
97b2f2af21SFlorian Fainelli 		goto clear_promisc;
98b73adef6SFlorian Fainelli 
9991da11f8SLennert Buytenhek 	return 0;
100df02c6ffSLennert Buytenhek 
101b2f2af21SFlorian Fainelli clear_promisc:
102b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1034fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
104df02c6ffSLennert Buytenhek clear_allmulti:
105df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
106df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
107df02c6ffSLennert Buytenhek del_unicast:
1088feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
109a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
110df02c6ffSLennert Buytenhek out:
111df02c6ffSLennert Buytenhek 	return err;
11291da11f8SLennert Buytenhek }
11391da11f8SLennert Buytenhek 
11491da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
11591da11f8SLennert Buytenhek {
116d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
117d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
118df02c6ffSLennert Buytenhek 
1198640f8dcSRussell King 	dsa_port_disable_rt(dp);
1206457edfeSVivien Didelot 
121df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
122a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
123df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
124df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
125df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
126df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
127df02c6ffSLennert Buytenhek 
1288feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
129a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
130df02c6ffSLennert Buytenhek 
13191da11f8SLennert Buytenhek 	return 0;
13291da11f8SLennert Buytenhek }
13391da11f8SLennert Buytenhek 
13491da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
13591da11f8SLennert Buytenhek {
136d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
13717ab4f61SRundong Ge 	if (dev->flags & IFF_UP) {
13891da11f8SLennert Buytenhek 		if (change & IFF_ALLMULTI)
13917ab4f61SRundong Ge 			dev_set_allmulti(master,
14017ab4f61SRundong Ge 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
14191da11f8SLennert Buytenhek 		if (change & IFF_PROMISC)
14217ab4f61SRundong Ge 			dev_set_promiscuity(master,
14317ab4f61SRundong Ge 					    dev->flags & IFF_PROMISC ? 1 : -1);
14417ab4f61SRundong Ge 	}
14591da11f8SLennert Buytenhek }
14691da11f8SLennert Buytenhek 
14791da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
14891da11f8SLennert Buytenhek {
149d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
15091da11f8SLennert Buytenhek 
15191da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
152a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
15391da11f8SLennert Buytenhek }
15491da11f8SLennert Buytenhek 
155df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
15691da11f8SLennert Buytenhek {
157d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
158df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
159df02c6ffSLennert Buytenhek 	int err;
160df02c6ffSLennert Buytenhek 
161df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
162df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
163df02c6ffSLennert Buytenhek 
164df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
165df02c6ffSLennert Buytenhek 		goto out;
166df02c6ffSLennert Buytenhek 
1678feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
168a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
169df02c6ffSLennert Buytenhek 		if (err < 0)
170df02c6ffSLennert Buytenhek 			return err;
171df02c6ffSLennert Buytenhek 	}
172df02c6ffSLennert Buytenhek 
1738feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
174a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
175df02c6ffSLennert Buytenhek 
176df02c6ffSLennert Buytenhek out:
177d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
17891da11f8SLennert Buytenhek 
17991da11f8SLennert Buytenhek 	return 0;
18091da11f8SLennert Buytenhek }
18191da11f8SLennert Buytenhek 
1822bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1832bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1842bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1852bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1862bedde1aSArkadi Sharshevsky 	int idx;
1872bedde1aSArkadi Sharshevsky };
1882bedde1aSArkadi Sharshevsky 
1892bedde1aSArkadi Sharshevsky static int
1902bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1912bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
1922bedde1aSArkadi Sharshevsky {
1932bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
1942bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1952bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
1962bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
1972bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
1982bedde1aSArkadi Sharshevsky 
1992bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
2002bedde1aSArkadi Sharshevsky 		goto skip;
2012bedde1aSArkadi Sharshevsky 
2022bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
2032bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2042bedde1aSArkadi Sharshevsky 	if (!nlh)
2052bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2062bedde1aSArkadi Sharshevsky 
2072bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2082bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2092bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2102bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2112bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2122bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2132bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2142bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2152bedde1aSArkadi Sharshevsky 
2162bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2172bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2182bedde1aSArkadi Sharshevsky 
2192bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2202bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2212bedde1aSArkadi Sharshevsky 
2222bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2232bedde1aSArkadi Sharshevsky 
2242bedde1aSArkadi Sharshevsky skip:
2252bedde1aSArkadi Sharshevsky 	dump->idx++;
2262bedde1aSArkadi Sharshevsky 	return 0;
2272bedde1aSArkadi Sharshevsky 
2282bedde1aSArkadi Sharshevsky nla_put_failure:
2292bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2302bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2312bedde1aSArkadi Sharshevsky }
2322bedde1aSArkadi Sharshevsky 
2332bedde1aSArkadi Sharshevsky static int
2342bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2352bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2362bedde1aSArkadi Sharshevsky 		   int *idx)
2372bedde1aSArkadi Sharshevsky {
238d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2392bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2402bedde1aSArkadi Sharshevsky 		.dev = dev,
2412bedde1aSArkadi Sharshevsky 		.skb = skb,
2422bedde1aSArkadi Sharshevsky 		.cb = cb,
2432bedde1aSArkadi Sharshevsky 		.idx = *idx,
2442bedde1aSArkadi Sharshevsky 	};
2452bedde1aSArkadi Sharshevsky 	int err;
2462bedde1aSArkadi Sharshevsky 
247de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2482bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
249de40fc5dSVivien Didelot 
2502bedde1aSArkadi Sharshevsky 	return err;
2512bedde1aSArkadi Sharshevsky }
2522bedde1aSArkadi Sharshevsky 
25391da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
25491da11f8SLennert Buytenhek {
2550336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2560336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2570336369dSBrandon Streiff 	int port = p->dp->index;
2580336369dSBrandon Streiff 
2590336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2600336369dSBrandon Streiff 	switch (cmd) {
2610336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2620336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2630336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2640336369dSBrandon Streiff 		break;
2650336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2660336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2670336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2680336369dSBrandon Streiff 		break;
2690336369dSBrandon Streiff 	}
2700336369dSBrandon Streiff 
271aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
27291da11f8SLennert Buytenhek }
27391da11f8SLennert Buytenhek 
27435636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
2754c08c586SVladimir Oltean 				   const struct switchdev_attr *attr,
2764c08c586SVladimir Oltean 				   struct netlink_ext_ack *extack)
27735636062SScott Feldman {
278d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
279b8d866acSVivien Didelot 	int ret;
28035636062SScott Feldman 
281058102a6STobias Waldekranz 	if (!dsa_port_offloads_netdev(dp, attr->orig_dev))
2825696c8aeSTobias Waldekranz 		return -EOPNOTSUPP;
2835696c8aeSTobias Waldekranz 
28435636062SScott Feldman 	switch (attr->id) {
2851f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
286bae33f2bSVladimir Oltean 		ret = dsa_port_set_state(dp, attr->u.stp_state);
28735636062SScott Feldman 		break;
288fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
289*89153ed6SVladimir Oltean 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
290*89153ed6SVladimir Oltean 					      extack);
291fb2dabadSVivien Didelot 		break;
29234a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
293bae33f2bSVladimir Oltean 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
29434a79f63SVivien Didelot 		break;
295ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
296a8b659e7SVladimir Oltean 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
297a8b659e7SVladimir Oltean 						extack);
298ea87005aSFlorian Fainelli 		break;
29957652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
300a8b659e7SVladimir Oltean 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
30157652796SRussell King 		break;
30208cc83ccSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
303a8b659e7SVladimir Oltean 		ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, extack);
30408cc83ccSVivien Didelot 		break;
30535636062SScott Feldman 	default:
30635636062SScott Feldman 		ret = -EOPNOTSUPP;
30735636062SScott Feldman 		break;
30835636062SScott Feldman 	}
30935636062SScott Feldman 
31035636062SScott Feldman 	return ret;
31135636062SScott Feldman }
31235636062SScott Feldman 
3131ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */
3141ce39f0eSVladimir Oltean static int
3151ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
3161ce39f0eSVladimir Oltean 				      const struct switchdev_obj_port_vlan *vlan)
3171ce39f0eSVladimir Oltean {
3181ce39f0eSVladimir Oltean 	struct net_device *upper_dev;
3191ce39f0eSVladimir Oltean 	struct list_head *iter;
3201ce39f0eSVladimir Oltean 
3211ce39f0eSVladimir Oltean 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
3221ce39f0eSVladimir Oltean 		u16 vid;
3231ce39f0eSVladimir Oltean 
3241ce39f0eSVladimir Oltean 		if (!is_vlan_dev(upper_dev))
3251ce39f0eSVladimir Oltean 			continue;
3261ce39f0eSVladimir Oltean 
3271ce39f0eSVladimir Oltean 		vid = vlan_dev_vlan_id(upper_dev);
328b7a9e0daSVladimir Oltean 		if (vid == vlan->vid)
3291ce39f0eSVladimir Oltean 			return -EBUSY;
3301ce39f0eSVladimir Oltean 	}
3311ce39f0eSVladimir Oltean 
3321ce39f0eSVladimir Oltean 	return 0;
3331ce39f0eSVladimir Oltean }
3341ce39f0eSVladimir Oltean 
335bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
3360ee2af4eSVladimir Oltean 			      const struct switchdev_obj *obj,
3370ee2af4eSVladimir Oltean 			      struct netlink_ext_ack *extack)
338bdcff080SVivien Didelot {
3392209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
340bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
341bdcff080SVivien Didelot 	struct switchdev_obj_port_vlan vlan;
342b7a9e0daSVladimir Oltean 	int err;
343bdcff080SVivien Didelot 
344058102a6STobias Waldekranz 	if (!dsa_port_offloads_netdev(dp, obj->orig_dev))
345bdcff080SVivien Didelot 		return -EOPNOTSUPP;
346bdcff080SVivien Didelot 
3470ee2af4eSVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
3480ee2af4eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
349c5335d73SVivien Didelot 		return 0;
3500ee2af4eSVladimir Oltean 	}
351c5335d73SVivien Didelot 
352bdcff080SVivien Didelot 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
353bdcff080SVivien Didelot 
3541ce39f0eSVladimir Oltean 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
3551ce39f0eSVladimir Oltean 	 * the same VID.
3561ce39f0eSVladimir Oltean 	 */
357ffb68fc5SVladimir Oltean 	if (br_vlan_enabled(dp->bridge_dev)) {
3581ce39f0eSVladimir Oltean 		rcu_read_lock();
3591ce39f0eSVladimir Oltean 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
3601ce39f0eSVladimir Oltean 		rcu_read_unlock();
36131046a5fSVladimir Oltean 		if (err) {
36231046a5fSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
36331046a5fSVladimir Oltean 					   "Port already has a VLAN upper with this VID");
3641ce39f0eSVladimir Oltean 			return err;
3651ce39f0eSVladimir Oltean 		}
36631046a5fSVladimir Oltean 	}
3671ce39f0eSVladimir Oltean 
36831046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp, &vlan, extack);
369bdcff080SVivien Didelot 	if (err)
370bdcff080SVivien Didelot 		return err;
371bdcff080SVivien Didelot 
372b9499904SVivien Didelot 	/* We need the dedicated CPU port to be a member of the VLAN as well.
373b9499904SVivien Didelot 	 * Even though drivers often handle CPU membership in special ways,
374b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
375b9499904SVivien Didelot 	 */
376b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
377b9499904SVivien Didelot 
37831046a5fSVladimir Oltean 	err = dsa_port_vlan_add(dp->cpu_dp, &vlan, extack);
3797e1741b4SVivien Didelot 	if (err)
3807e1741b4SVivien Didelot 		return err;
3817e1741b4SVivien Didelot 
382b7a9e0daSVladimir Oltean 	return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid);
383bdcff080SVivien Didelot }
384bdcff080SVivien Didelot 
385ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
386648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
38779b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
388ba14d9ebSVivien Didelot {
389d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
390ba14d9ebSVivien Didelot 	int err;
391ba14d9ebSVivien Didelot 
3929e8f4a54SJiri Pirko 	switch (obj->id) {
3938df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
394058102a6STobias Waldekranz 		if (!dsa_port_offloads_netdev(dp, obj->orig_dev))
39579b139f4SVivien Didelot 			return -EOPNOTSUPP;
396ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
3978df30255SVivien Didelot 		break;
3985f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
3995f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4005f4dbc50SAndrew Lunn 		 * but on the CPU port.
4015f4dbc50SAndrew Lunn 		 */
402ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4035f4dbc50SAndrew Lunn 		break;
40457d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
4050ee2af4eSVladimir Oltean 		err = dsa_slave_vlan_add(dev, obj, extack);
40611149536SVivien Didelot 		break;
407ba14d9ebSVivien Didelot 	default:
408ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
409ba14d9ebSVivien Didelot 		break;
410ba14d9ebSVivien Didelot 	}
411ba14d9ebSVivien Didelot 
412ba14d9ebSVivien Didelot 	return err;
413ba14d9ebSVivien Didelot }
414ba14d9ebSVivien Didelot 
415bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
416bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
417bdcff080SVivien Didelot {
4182209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
419bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
4202209158cSVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
421b7a9e0daSVladimir Oltean 	int err;
422bdcff080SVivien Didelot 
423058102a6STobias Waldekranz 	if (!dsa_port_offloads_netdev(dp, obj->orig_dev))
424bdcff080SVivien Didelot 		return -EOPNOTSUPP;
425bdcff080SVivien Didelot 
42654a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
427c5335d73SVivien Didelot 		return 0;
428c5335d73SVivien Didelot 
4292209158cSVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
4302209158cSVladimir Oltean 
4317e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
4327e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
4337e1741b4SVivien Didelot 	 */
4342209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, vlan);
4352209158cSVladimir Oltean 	if (err)
4362209158cSVladimir Oltean 		return err;
4372209158cSVladimir Oltean 
438b7a9e0daSVladimir Oltean 	vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
4392209158cSVladimir Oltean 
4402209158cSVladimir Oltean 	return 0;
441bdcff080SVivien Didelot }
442bdcff080SVivien Didelot 
443ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
444648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
445ba14d9ebSVivien Didelot {
446d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
447ba14d9ebSVivien Didelot 	int err;
448ba14d9ebSVivien Didelot 
4499e8f4a54SJiri Pirko 	switch (obj->id) {
4508df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
451058102a6STobias Waldekranz 		if (!dsa_port_offloads_netdev(dp, obj->orig_dev))
45279b139f4SVivien Didelot 			return -EOPNOTSUPP;
453bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4548df30255SVivien Didelot 		break;
4555f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
4565f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4575f4dbc50SAndrew Lunn 		 * but on the CPU port.
4585f4dbc50SAndrew Lunn 		 */
4595f4dbc50SAndrew Lunn 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4605f4dbc50SAndrew Lunn 		break;
46157d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
462bdcff080SVivien Didelot 		err = dsa_slave_vlan_del(dev, obj);
46311149536SVivien Didelot 		break;
464ba14d9ebSVivien Didelot 	default:
465ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
466ba14d9ebSVivien Didelot 		break;
467ba14d9ebSVivien Didelot 	}
468ba14d9ebSVivien Didelot 
469ba14d9ebSVivien Didelot 	return err;
470ba14d9ebSVivien Didelot }
471ba14d9ebSVivien Didelot 
472929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
473929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
474b73adef6SFlorian Fainelli {
475d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
476d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
477a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
478b73adef6SFlorian Fainelli 
47915b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
48015b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
48115b04aceSJiri Pirko 	 * should be removed with legacy support.
48215b04aceSJiri Pirko 	 */
48315b04aceSJiri Pirko 	if (dp->ds->devlink)
48415b04aceSJiri Pirko 		return -EOPNOTSUPP;
48515b04aceSJiri Pirko 
486929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
487929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
488929d6c14SFlorian Fainelli 
489929d6c14SFlorian Fainelli 	return 0;
490929d6c14SFlorian Fainelli }
491929d6c14SFlorian Fainelli 
4924fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
49304ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
49404ff53f9SFlorian Fainelli {
49504ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
4964fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
4974fa7b718SVivien Didelot 
498f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
49904ff53f9SFlorian Fainelli #else
50004ff53f9SFlorian Fainelli 	BUG();
50104ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
502f78ed220SEric Dumazet #endif
50304ff53f9SFlorian Fainelli }
50404ff53f9SFlorian Fainelli 
50590af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
50690af1059SBrandon Streiff 				 struct sk_buff *skb)
50790af1059SBrandon Streiff {
50890af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
50990af1059SBrandon Streiff 	struct sk_buff *clone;
51090af1059SBrandon Streiff 	unsigned int type;
51190af1059SBrandon Streiff 
51290af1059SBrandon Streiff 	type = ptp_classify_raw(skb);
51390af1059SBrandon Streiff 	if (type == PTP_CLASS_NONE)
51490af1059SBrandon Streiff 		return;
51590af1059SBrandon Streiff 
51690af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
51790af1059SBrandon Streiff 		return;
51890af1059SBrandon Streiff 
51990af1059SBrandon Streiff 	clone = skb_clone_sk(skb);
52090af1059SBrandon Streiff 	if (!clone)
52190af1059SBrandon Streiff 		return;
52290af1059SBrandon Streiff 
52330abc9cdSChristian Eggers 	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) {
524146d442cSVladimir Oltean 		DSA_SKB_CB(skb)->clone = clone;
52590af1059SBrandon Streiff 		return;
52630abc9cdSChristian Eggers 	}
52790af1059SBrandon Streiff 
52890af1059SBrandon Streiff 	kfree_skb(clone);
52990af1059SBrandon Streiff }
53090af1059SBrandon Streiff 
53197a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
53297a69a0dSVladimir Oltean {
53397a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
53497a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
53597a69a0dSVladimir Oltean 	 */
53697a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
53797a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
53897a69a0dSVladimir Oltean 
53997a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
54097a69a0dSVladimir Oltean 	 * do not modify its EtherType
54197a69a0dSVladimir Oltean 	 */
54297a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
54397a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
54497a69a0dSVladimir Oltean 
54597a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
54697a69a0dSVladimir Oltean }
54797a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
54897a69a0dSVladimir Oltean 
549a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
550a3b0b647SVladimir Oltean {
551a3b0b647SVladimir Oltean 	int needed_headroom = dev->needed_headroom;
552a3b0b647SVladimir Oltean 	int needed_tailroom = dev->needed_tailroom;
553a3b0b647SVladimir Oltean 
554a3b0b647SVladimir Oltean 	/* For tail taggers, we need to pad short frames ourselves, to ensure
555a3b0b647SVladimir Oltean 	 * that the tail tag does not fail at its role of being at the end of
556a3b0b647SVladimir Oltean 	 * the packet, once the master interface pads the frame. Account for
557a3b0b647SVladimir Oltean 	 * that pad length here, and pad later.
558a3b0b647SVladimir Oltean 	 */
559a3b0b647SVladimir Oltean 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
560a3b0b647SVladimir Oltean 		needed_tailroom += ETH_ZLEN - skb->len;
561a3b0b647SVladimir Oltean 	/* skb_headroom() returns unsigned int... */
562a3b0b647SVladimir Oltean 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
563a3b0b647SVladimir Oltean 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
564a3b0b647SVladimir Oltean 
565a3b0b647SVladimir Oltean 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
566a3b0b647SVladimir Oltean 		/* No reallocation needed, yay! */
567a3b0b647SVladimir Oltean 		return 0;
568a3b0b647SVladimir Oltean 
569a3b0b647SVladimir Oltean 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
570a3b0b647SVladimir Oltean 				GFP_ATOMIC);
571a3b0b647SVladimir Oltean }
572a3b0b647SVladimir Oltean 
5733e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
5743e8a72d1SFlorian Fainelli {
5753e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
5764ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
5773e8a72d1SFlorian Fainelli 
5786a900628SHeiner Kallweit 	dev_sw_netstats_tx_add(dev, 1, skb->len);
5793e8a72d1SFlorian Fainelli 
580146d442cSVladimir Oltean 	DSA_SKB_CB(skb)->clone = NULL;
58187671375SVladimir Oltean 
58290af1059SBrandon Streiff 	/* Identify PTP protocol packets, clone them, and pass them to the
58390af1059SBrandon Streiff 	 * switch driver
58490af1059SBrandon Streiff 	 */
58590af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
58690af1059SBrandon Streiff 
587a3b0b647SVladimir Oltean 	if (dsa_realloc_skb(skb, dev)) {
588a3b0b647SVladimir Oltean 		dev_kfree_skb_any(skb);
589a3b0b647SVladimir Oltean 		return NETDEV_TX_OK;
590a3b0b647SVladimir Oltean 	}
591a3b0b647SVladimir Oltean 
592a3b0b647SVladimir Oltean 	/* needed_tailroom should still be 'warm' in the cache line from
593a3b0b647SVladimir Oltean 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
594a3b0b647SVladimir Oltean 	 */
595a3b0b647SVladimir Oltean 	if (dev->needed_tailroom)
596a3b0b647SVladimir Oltean 		eth_skb_pad(skb);
597a3b0b647SVladimir Oltean 
598fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
599fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
600fe47d563SVivien Didelot 	 */
6014ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
602fe47d563SVivien Didelot 	if (!nskb) {
603fe47d563SVivien Didelot 		kfree_skb(skb);
6044ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
605fe47d563SVivien Didelot 	}
6065aed85ceSFlorian Fainelli 
60797a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
60897a69a0dSVladimir Oltean }
60904ff53f9SFlorian Fainelli 
61091da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
61191da11f8SLennert Buytenhek 
61291da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
61391da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
61491da11f8SLennert Buytenhek {
6157826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
6167826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
6177826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
61891da11f8SLennert Buytenhek }
61991da11f8SLennert Buytenhek 
6203d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
6213d762a0fSGuenter Roeck {
622d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
623d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6243d762a0fSGuenter Roeck 
6259d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
626d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
6273d762a0fSGuenter Roeck 
6283d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
6293d762a0fSGuenter Roeck }
6303d762a0fSGuenter Roeck 
6313d762a0fSGuenter Roeck static void
6323d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
6333d762a0fSGuenter Roeck {
634d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
635d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6363d762a0fSGuenter Roeck 
6379d490b4eSVivien Didelot 	if (ds->ops->get_regs)
638d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
6393d762a0fSGuenter Roeck }
6403d762a0fSGuenter Roeck 
641aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
642aab9c406SFlorian Fainelli {
643aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
644aab9c406SFlorian Fainelli 
645aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
646aab9c406SFlorian Fainelli }
647aab9c406SFlorian Fainelli 
6486793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
6496793abb4SGuenter Roeck {
650d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
651d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6526793abb4SGuenter Roeck 
6530e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
654ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
6556793abb4SGuenter Roeck 
6569d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
6579d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
6586793abb4SGuenter Roeck 
6596793abb4SGuenter Roeck 	return 0;
6606793abb4SGuenter Roeck }
6616793abb4SGuenter Roeck 
6626793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
6636793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6646793abb4SGuenter Roeck {
665d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
666d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6676793abb4SGuenter Roeck 
6689d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
6699d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
6706793abb4SGuenter Roeck 
6716793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6726793abb4SGuenter Roeck }
6736793abb4SGuenter Roeck 
6746793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
6756793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6766793abb4SGuenter Roeck {
677d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
678d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6796793abb4SGuenter Roeck 
6809d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
6819d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
6826793abb4SGuenter Roeck 
6836793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6846793abb4SGuenter Roeck }
6856793abb4SGuenter Roeck 
68691da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
68791da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
68891da11f8SLennert Buytenhek {
689d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
690d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
69191da11f8SLennert Buytenhek 
69291da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
69391da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
69491da11f8SLennert Buytenhek 
69591da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
69691da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
69791da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
69891da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
6999d490b4eSVivien Didelot 		if (ds->ops->get_strings)
70089f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
70189f09048SFlorian Fainelli 					     data + 4 * len);
70291da11f8SLennert Buytenhek 	}
70391da11f8SLennert Buytenhek }
70491da11f8SLennert Buytenhek 
70591da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
70691da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
70791da11f8SLennert Buytenhek 					uint64_t *data)
70891da11f8SLennert Buytenhek {
709d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
710d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7115f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
712f613ed66SFlorian Fainelli 	unsigned int start;
7135f6b4e14SFlorian Fainelli 	int i;
71491da11f8SLennert Buytenhek 
7155f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
7165f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
7175f6b4e14SFlorian Fainelli 
7186a900628SHeiner Kallweit 		s = per_cpu_ptr(dev->tstats, i);
719f613ed66SFlorian Fainelli 		do {
7205f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
7215f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
7225f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
7235f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
7245f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
7255f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
7265f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
7275f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
7285f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
7295f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
7305f6b4e14SFlorian Fainelli 	}
7319d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
732d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
73391da11f8SLennert Buytenhek }
73491da11f8SLennert Buytenhek 
73591da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
73691da11f8SLennert Buytenhek {
737d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
738d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
73991da11f8SLennert Buytenhek 
74091da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
74191da11f8SLennert Buytenhek 		int count;
74291da11f8SLennert Buytenhek 
74391da11f8SLennert Buytenhek 		count = 4;
7449d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
74589f09048SFlorian Fainelli 			count += ds->ops->get_sset_count(ds, dp->index, sset);
74691da11f8SLennert Buytenhek 
74791da11f8SLennert Buytenhek 		return count;
74891da11f8SLennert Buytenhek 	}
74991da11f8SLennert Buytenhek 
75091da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
75191da11f8SLennert Buytenhek }
75291da11f8SLennert Buytenhek 
75319e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
75419e57c4eSFlorian Fainelli {
755d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
756d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
75719e57c4eSFlorian Fainelli 
758aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
759aab9c406SFlorian Fainelli 
7609d490b4eSVivien Didelot 	if (ds->ops->get_wol)
761d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
76219e57c4eSFlorian Fainelli }
76319e57c4eSFlorian Fainelli 
76419e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
76519e57c4eSFlorian Fainelli {
766d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
767d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
76819e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
76919e57c4eSFlorian Fainelli 
770aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
771aab9c406SFlorian Fainelli 
7729d490b4eSVivien Didelot 	if (ds->ops->set_wol)
773d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
77419e57c4eSFlorian Fainelli 
77519e57c4eSFlorian Fainelli 	return ret;
77619e57c4eSFlorian Fainelli }
77719e57c4eSFlorian Fainelli 
7787905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
7797905288fSFlorian Fainelli {
780d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
781d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7827905288fSFlorian Fainelli 	int ret;
7837905288fSFlorian Fainelli 
7847b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
78500670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
7867b9cc738SVivien Didelot 		return -ENODEV;
7877b9cc738SVivien Didelot 
78808f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
7897905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7907905288fSFlorian Fainelli 
791d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
7927905288fSFlorian Fainelli 	if (ret)
7937905288fSFlorian Fainelli 		return ret;
7947905288fSFlorian Fainelli 
795aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
7967905288fSFlorian Fainelli }
7977905288fSFlorian Fainelli 
7987905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
7997905288fSFlorian Fainelli {
800d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
801d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8027905288fSFlorian Fainelli 	int ret;
8037905288fSFlorian Fainelli 
8047b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
80500670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8067b9cc738SVivien Didelot 		return -ENODEV;
8077b9cc738SVivien Didelot 
80808f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
8097905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8107905288fSFlorian Fainelli 
811d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
8127905288fSFlorian Fainelli 	if (ret)
8137905288fSFlorian Fainelli 		return ret;
8147905288fSFlorian Fainelli 
815aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
816aab9c406SFlorian Fainelli }
817aab9c406SFlorian Fainelli 
818aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
819aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
820aab9c406SFlorian Fainelli {
821aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
822aab9c406SFlorian Fainelli 
823aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
824aab9c406SFlorian Fainelli }
825aab9c406SFlorian Fainelli 
826aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
827aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
828aab9c406SFlorian Fainelli {
829aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
830aab9c406SFlorian Fainelli 
831aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
8327905288fSFlorian Fainelli }
8337905288fSFlorian Fainelli 
834a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
835a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
836a2a1a13bSHeiner Kallweit {
837a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
838a2a1a13bSHeiner Kallweit 
839a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
840a2a1a13bSHeiner Kallweit }
841a2a1a13bSHeiner Kallweit 
842a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
843a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
844a2a1a13bSHeiner Kallweit {
845a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
846a2a1a13bSHeiner Kallweit 
847a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
848a2a1a13bSHeiner Kallweit }
849a2a1a13bSHeiner Kallweit 
85004ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
85104ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
85204ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
85304ff53f9SFlorian Fainelli {
854d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
85504ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
85604ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
85704ff53f9SFlorian Fainelli 	int err = 0;
85804ff53f9SFlorian Fainelli 
85904ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
86004ff53f9SFlorian Fainelli 	if (!netpoll)
86104ff53f9SFlorian Fainelli 		return -ENOMEM;
86204ff53f9SFlorian Fainelli 
86304ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
86404ff53f9SFlorian Fainelli 	if (err) {
86504ff53f9SFlorian Fainelli 		kfree(netpoll);
86604ff53f9SFlorian Fainelli 		goto out;
86704ff53f9SFlorian Fainelli 	}
86804ff53f9SFlorian Fainelli 
86904ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
87004ff53f9SFlorian Fainelli out:
87104ff53f9SFlorian Fainelli 	return err;
87204ff53f9SFlorian Fainelli }
87304ff53f9SFlorian Fainelli 
87404ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
87504ff53f9SFlorian Fainelli {
87604ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
87704ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
87804ff53f9SFlorian Fainelli 
87904ff53f9SFlorian Fainelli 	if (!netpoll)
88004ff53f9SFlorian Fainelli 		return;
88104ff53f9SFlorian Fainelli 
88204ff53f9SFlorian Fainelli 	p->netpoll = NULL;
88304ff53f9SFlorian Fainelli 
884c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
88504ff53f9SFlorian Fainelli }
88604ff53f9SFlorian Fainelli 
88704ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
88804ff53f9SFlorian Fainelli {
88904ff53f9SFlorian Fainelli }
89004ff53f9SFlorian Fainelli #endif
89104ff53f9SFlorian Fainelli 
89244bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
89344bb765cSFlorian Fainelli 					char *name, size_t len)
89444bb765cSFlorian Fainelli {
895d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
89644bb765cSFlorian Fainelli 
897d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
898d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
899d484210bSJiri Pirko 	 * should be removed with legacy support.
900d484210bSJiri Pirko 	 */
901d484210bSJiri Pirko 	if (dp->ds->devlink)
902d484210bSJiri Pirko 		return -EOPNOTSUPP;
903d484210bSJiri Pirko 
904d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
90544bb765cSFlorian Fainelli 		return -EINVAL;
9063a543ef4SFlorian Fainelli 
9073a543ef4SFlorian Fainelli 	return 0;
9083a543ef4SFlorian Fainelli }
9093a543ef4SFlorian Fainelli 
910f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
9114fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
912f50f2127SFlorian Fainelli {
9134fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
914f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
915f50f2127SFlorian Fainelli 
916f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
917f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
918f50f2127SFlorian Fainelli 			return mall_tc_entry;
919f50f2127SFlorian Fainelli 
920f50f2127SFlorian Fainelli 	return NULL;
921f50f2127SFlorian Fainelli }
922f50f2127SFlorian Fainelli 
923e13c2075SVladimir Oltean static int
924e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
925f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
926f50f2127SFlorian Fainelli 				  bool ingress)
927f50f2127SFlorian Fainelli {
928d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
929f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
930e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
931f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
932d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9339681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
934d945097bSVivien Didelot 	struct dsa_port *to_dp;
935e13c2075SVladimir Oltean 	int err;
936e13c2075SVladimir Oltean 
937f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
93834297176SVladimir Oltean 		return -EOPNOTSUPP;
939f50f2127SFlorian Fainelli 
94053eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
941319a1d19SJiri Pirko 					      cls->common.extack))
94234297176SVladimir Oltean 		return -EOPNOTSUPP;
943319a1d19SJiri Pirko 
9449681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
945f50f2127SFlorian Fainelli 
94665722159SVladimir Oltean 	if (!act->dev)
94765722159SVladimir Oltean 		return -EINVAL;
94865722159SVladimir Oltean 
9499681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
950f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
951f50f2127SFlorian Fainelli 
952f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
953f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
954f50f2127SFlorian Fainelli 		return -ENOMEM;
955f50f2127SFlorian Fainelli 
956f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
957f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
958f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
959f50f2127SFlorian Fainelli 
9609681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
961f50f2127SFlorian Fainelli 
962d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
963f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
964f50f2127SFlorian Fainelli 
965d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
966f50f2127SFlorian Fainelli 	if (err) {
967f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
968f50f2127SFlorian Fainelli 		return err;
969f50f2127SFlorian Fainelli 	}
970f50f2127SFlorian Fainelli 
971f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
972e13c2075SVladimir Oltean 
973e13c2075SVladimir Oltean 	return err;
974f50f2127SFlorian Fainelli }
975f50f2127SFlorian Fainelli 
97634297176SVladimir Oltean static int
97734297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
97834297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
97934297176SVladimir Oltean 				  bool ingress)
98034297176SVladimir Oltean {
98134297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
98234297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
98334297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
98434297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
98534297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
98634297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
98734297176SVladimir Oltean 	struct flow_action_entry *act;
98834297176SVladimir Oltean 	int err;
98934297176SVladimir Oltean 
99034297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
99134297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
992c75a33c8SJacob Keller 				   "Policing offload not implemented");
99334297176SVladimir Oltean 		return -EOPNOTSUPP;
99434297176SVladimir Oltean 	}
99534297176SVladimir Oltean 
99634297176SVladimir Oltean 	if (!ingress) {
99734297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
998c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
99934297176SVladimir Oltean 		return -EOPNOTSUPP;
100034297176SVladimir Oltean 	}
100134297176SVladimir Oltean 
100234297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
100334297176SVladimir Oltean 					      cls->common.extack))
100434297176SVladimir Oltean 		return -EOPNOTSUPP;
100534297176SVladimir Oltean 
100634297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
100734297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
100834297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1009c75a33c8SJacob Keller 					   "Only one port policer allowed");
101034297176SVladimir Oltean 			return -EEXIST;
101134297176SVladimir Oltean 		}
101234297176SVladimir Oltean 	}
101334297176SVladimir Oltean 
101434297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
101534297176SVladimir Oltean 
101634297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
101734297176SVladimir Oltean 	if (!mall_tc_entry)
101834297176SVladimir Oltean 		return -ENOMEM;
101934297176SVladimir Oltean 
102034297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
102134297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
102234297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
102334297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
102434297176SVladimir Oltean 	policer->burst = act->police.burst;
102534297176SVladimir Oltean 
102634297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
102734297176SVladimir Oltean 	if (err) {
102834297176SVladimir Oltean 		kfree(mall_tc_entry);
102934297176SVladimir Oltean 		return err;
103034297176SVladimir Oltean 	}
103134297176SVladimir Oltean 
103234297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
103334297176SVladimir Oltean 
103434297176SVladimir Oltean 	return err;
103534297176SVladimir Oltean }
103634297176SVladimir Oltean 
1037e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1038e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1039e13c2075SVladimir Oltean 				      bool ingress)
1040e13c2075SVladimir Oltean {
1041e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1042e13c2075SVladimir Oltean 
1043e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1044e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1045e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1046e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
104734297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
104834297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
104934297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1050e13c2075SVladimir Oltean 
1051e13c2075SVladimir Oltean 	return err;
1052f50f2127SFlorian Fainelli }
1053f50f2127SFlorian Fainelli 
1054f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1055f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1056f50f2127SFlorian Fainelli {
1057d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1058f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1059d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1060f50f2127SFlorian Fainelli 
10614fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1062f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1063f50f2127SFlorian Fainelli 		return;
1064f50f2127SFlorian Fainelli 
1065f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1066f50f2127SFlorian Fainelli 
1067f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1068f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
106934297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
107034297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
107134297176SVladimir Oltean 						 &mall_tc_entry->mirror);
107234297176SVladimir Oltean 		break;
107334297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
107434297176SVladimir Oltean 		if (ds->ops->port_policer_del)
107534297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1076f50f2127SFlorian Fainelli 		break;
1077f50f2127SFlorian Fainelli 	default:
1078f50f2127SFlorian Fainelli 		WARN_ON(1);
1079f50f2127SFlorian Fainelli 	}
1080f50f2127SFlorian Fainelli 
1081f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1082f50f2127SFlorian Fainelli }
1083f50f2127SFlorian Fainelli 
10843fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
10856b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
10866b3eb752SJiri Pirko 					   bool ingress)
1087f50f2127SFlorian Fainelli {
10885fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1089a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1090f50f2127SFlorian Fainelli 
10913fbae382SJiri Pirko 	switch (cls->command) {
10923fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
10935fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
10943fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
10953fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
10963fbae382SJiri Pirko 		return 0;
10973fbae382SJiri Pirko 	default:
10983fbae382SJiri Pirko 		return -EOPNOTSUPP;
10993fbae382SJiri Pirko 	}
11003fbae382SJiri Pirko }
11013fbae382SJiri Pirko 
1102ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1103ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1104ed11bb1fSVladimir Oltean 				    bool ingress)
1105ed11bb1fSVladimir Oltean {
1106ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1107ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1108ed11bb1fSVladimir Oltean 	int port = dp->index;
1109ed11bb1fSVladimir Oltean 
1110ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1111ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1112ed11bb1fSVladimir Oltean 
1113ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1114ed11bb1fSVladimir Oltean }
1115ed11bb1fSVladimir Oltean 
1116ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1117ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1118ed11bb1fSVladimir Oltean 				    bool ingress)
1119ed11bb1fSVladimir Oltean {
1120ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1121ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1122ed11bb1fSVladimir Oltean 	int port = dp->index;
1123ed11bb1fSVladimir Oltean 
1124ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1125ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1126ed11bb1fSVladimir Oltean 
1127ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1128ed11bb1fSVladimir Oltean }
1129ed11bb1fSVladimir Oltean 
1130ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1131ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1132ed11bb1fSVladimir Oltean 				      bool ingress)
1133ed11bb1fSVladimir Oltean {
1134ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1135ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1136ed11bb1fSVladimir Oltean 	int port = dp->index;
1137ed11bb1fSVladimir Oltean 
1138ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1139ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1140ed11bb1fSVladimir Oltean 
1141ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1142ed11bb1fSVladimir Oltean }
1143ed11bb1fSVladimir Oltean 
1144ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1145ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1146ed11bb1fSVladimir Oltean 					 bool ingress)
1147ed11bb1fSVladimir Oltean {
1148ed11bb1fSVladimir Oltean 	switch (cls->command) {
1149ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1150ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1151ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1152ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1153ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1154ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1155ed11bb1fSVladimir Oltean 	default:
1156ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1157ed11bb1fSVladimir Oltean 	}
1158ed11bb1fSVladimir Oltean }
1159ed11bb1fSVladimir Oltean 
11606b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
11616b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
11626b3eb752SJiri Pirko {
11636b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
11646b3eb752SJiri Pirko 
116544ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
116644ae12a7SJiri Pirko 		return -EOPNOTSUPP;
116744ae12a7SJiri Pirko 
11686b3eb752SJiri Pirko 	switch (type) {
11696b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
11706b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1171ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1172ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
11736b3eb752SJiri Pirko 	default:
11746b3eb752SJiri Pirko 		return -EOPNOTSUPP;
11756b3eb752SJiri Pirko 	}
11766b3eb752SJiri Pirko }
11776b3eb752SJiri Pirko 
11786b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
11796b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
11806b3eb752SJiri Pirko {
11816b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
11826b3eb752SJiri Pirko }
11836b3eb752SJiri Pirko 
11846b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
11856b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
11866b3eb752SJiri Pirko {
11876b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
11886b3eb752SJiri Pirko }
11896b3eb752SJiri Pirko 
1190955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1191955bcb6eSPablo Neira Ayuso 
11926b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1193955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
11946b3eb752SJiri Pirko {
1195955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1196a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
11976b3eb752SJiri Pirko 
119832f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
11996b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
120032f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
12016b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
12026b3eb752SJiri Pirko 	else
12036b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12046b3eb752SJiri Pirko 
1205955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1206955bcb6eSPablo Neira Ayuso 
12076b3eb752SJiri Pirko 	switch (f->command) {
12089c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
12090d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
12100d4fd02eSPablo Neira Ayuso 			return -EBUSY;
12110d4fd02eSPablo Neira Ayuso 
12120c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1213955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1214955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1215955bcb6eSPablo Neira Ayuso 
1216955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1217955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1218955bcb6eSPablo Neira Ayuso 		return 0;
12199c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
122014bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1221955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1222955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1223955bcb6eSPablo Neira Ayuso 
1224955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1225955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
12266b3eb752SJiri Pirko 		return 0;
12276b3eb752SJiri Pirko 	default:
12286b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12296b3eb752SJiri Pirko 	}
12306b3eb752SJiri Pirko }
12316b3eb752SJiri Pirko 
12323fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1233de4784caSJiri Pirko 			      void *type_data)
12343fbae382SJiri Pirko {
123547d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
123647d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
123747d23af2SVladimir Oltean 
123847d23af2SVladimir Oltean 	if (type == TC_SETUP_BLOCK)
12396b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
124047d23af2SVladimir Oltean 
124147d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1242a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
124347d23af2SVladimir Oltean 
124447d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1245f50f2127SFlorian Fainelli }
1246f50f2127SFlorian Fainelli 
1247bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1248bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1249bf9f2648SFlorian Fainelli {
1250d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1251d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1252bf9f2648SFlorian Fainelli 
1253bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1254bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1255bf9f2648SFlorian Fainelli 
1256d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1257bf9f2648SFlorian Fainelli }
1258bf9f2648SFlorian Fainelli 
1259bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1260bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1261bf9f2648SFlorian Fainelli {
1262d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1263d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1264bf9f2648SFlorian Fainelli 
1265bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1266bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1267bf9f2648SFlorian Fainelli 
1268d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1269bf9f2648SFlorian Fainelli }
1270bf9f2648SFlorian Fainelli 
12710336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
12720336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
12730336369dSBrandon Streiff {
12740336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
12750336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
12760336369dSBrandon Streiff 
12770336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
12780336369dSBrandon Streiff 		return -EOPNOTSUPP;
12790336369dSBrandon Streiff 
12800336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
12810336369dSBrandon Streiff }
12820336369dSBrandon Streiff 
1283061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1284061f6a50SFlorian Fainelli 				     u16 vid)
1285061f6a50SFlorian Fainelli {
12862209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1287061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
128888236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
128988236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1290b7a9e0daSVladimir Oltean 		.vid = vid,
129188236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
129288236591SVladimir Oltean 		.flags = 0,
129388236591SVladimir Oltean 	};
129431046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1295061f6a50SFlorian Fainelli 	int ret;
1296061f6a50SFlorian Fainelli 
129788236591SVladimir Oltean 	/* User port... */
129831046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
129931046a5fSVladimir Oltean 	if (ret) {
130031046a5fSVladimir Oltean 		if (extack._msg)
130131046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
130288236591SVladimir Oltean 		return ret;
130331046a5fSVladimir Oltean 	}
130488236591SVladimir Oltean 
130588236591SVladimir Oltean 	/* And CPU port... */
130631046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &extack);
130731046a5fSVladimir Oltean 	if (ret) {
130831046a5fSVladimir Oltean 		if (extack._msg)
130931046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
131031046a5fSVladimir Oltean 				   extack._msg);
13117e1741b4SVivien Didelot 		return ret;
131231046a5fSVladimir Oltean 	}
13137e1741b4SVivien Didelot 
13142209158cSVladimir Oltean 	return vlan_vid_add(master, proto, vid);
1315061f6a50SFlorian Fainelli }
1316061f6a50SFlorian Fainelli 
1317061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1318061f6a50SFlorian Fainelli 				      u16 vid)
1319061f6a50SFlorian Fainelli {
13202209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1321061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
132288236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1323b7a9e0daSVladimir Oltean 		.vid = vid,
132488236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
132588236591SVladimir Oltean 		.flags = 0,
132688236591SVladimir Oltean 	};
13272209158cSVladimir Oltean 	int err;
1328061f6a50SFlorian Fainelli 
13297e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
13307e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
13317e1741b4SVivien Didelot 	 */
13322209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
13332209158cSVladimir Oltean 	if (err)
13342209158cSVladimir Oltean 		return err;
13352209158cSVladimir Oltean 
13362209158cSVladimir Oltean 	vlan_vid_del(master, proto, vid);
13372209158cSVladimir Oltean 
13382209158cSVladimir Oltean 	return 0;
1339061f6a50SFlorian Fainelli }
1340061f6a50SFlorian Fainelli 
1341bff33f7eSVladimir Oltean struct dsa_hw_port {
1342bff33f7eSVladimir Oltean 	struct list_head list;
1343bff33f7eSVladimir Oltean 	struct net_device *dev;
1344bff33f7eSVladimir Oltean 	int old_mtu;
1345bff33f7eSVladimir Oltean };
1346bff33f7eSVladimir Oltean 
1347bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1348bff33f7eSVladimir Oltean {
1349bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1350bff33f7eSVladimir Oltean 	int err;
1351bff33f7eSVladimir Oltean 
1352bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1353bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1354bff33f7eSVladimir Oltean 			continue;
1355bff33f7eSVladimir Oltean 
1356bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1357bff33f7eSVladimir Oltean 		if (err)
1358bff33f7eSVladimir Oltean 			goto rollback;
1359bff33f7eSVladimir Oltean 	}
1360bff33f7eSVladimir Oltean 
1361bff33f7eSVladimir Oltean 	return 0;
1362bff33f7eSVladimir Oltean 
1363bff33f7eSVladimir Oltean rollback:
1364bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1365bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1366bff33f7eSVladimir Oltean 			continue;
1367bff33f7eSVladimir Oltean 
1368bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1369bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1370bff33f7eSVladimir Oltean 	}
1371bff33f7eSVladimir Oltean 
1372bff33f7eSVladimir Oltean 	return err;
1373bff33f7eSVladimir Oltean }
1374bff33f7eSVladimir Oltean 
1375bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1376bff33f7eSVladimir Oltean {
1377bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1378bff33f7eSVladimir Oltean 
1379bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1380bff33f7eSVladimir Oltean 		kfree(p);
1381bff33f7eSVladimir Oltean }
1382bff33f7eSVladimir Oltean 
1383bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1384bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1385bff33f7eSVladimir Oltean {
1386bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1387bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1388bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1389bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1390bff33f7eSVladimir Oltean 	int err;
1391bff33f7eSVladimir Oltean 
1392bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1393bff33f7eSVladimir Oltean 		return;
1394bff33f7eSVladimir Oltean 
1395bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1396bff33f7eSVladimir Oltean 		return;
1397bff33f7eSVladimir Oltean 
1398bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1399bff33f7eSVladimir Oltean 
1400bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1401bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1402bff33f7eSVladimir Oltean 	 */
1403bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1404bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1405bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1406bff33f7eSVladimir Oltean 			struct net_device *slave;
1407bff33f7eSVladimir Oltean 
1408bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1409bff33f7eSVladimir Oltean 				continue;
1410bff33f7eSVladimir Oltean 
1411bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1412bff33f7eSVladimir Oltean 				continue;
1413bff33f7eSVladimir Oltean 
1414bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1415bff33f7eSVladimir Oltean 				continue;
1416bff33f7eSVladimir Oltean 
1417bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1418bff33f7eSVladimir Oltean 
1419bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1420bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1421bff33f7eSVladimir Oltean 
1422bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1423bff33f7eSVladimir Oltean 			if (!hw_port)
1424bff33f7eSVladimir Oltean 				goto out;
1425bff33f7eSVladimir Oltean 
1426bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1427bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1428bff33f7eSVladimir Oltean 
1429bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1430bff33f7eSVladimir Oltean 		}
1431bff33f7eSVladimir Oltean 	}
1432bff33f7eSVladimir Oltean 
1433bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1434bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1435bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1436bff33f7eSVladimir Oltean 	 */
1437bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1438bff33f7eSVladimir Oltean 	if (!err)
1439bff33f7eSVladimir Oltean 		goto out;
1440bff33f7eSVladimir Oltean 
1441bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1442bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1443bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1444bff33f7eSVladimir Oltean 	 */
1445bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1446bff33f7eSVladimir Oltean 
1447bff33f7eSVladimir Oltean out:
1448bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1449bff33f7eSVladimir Oltean }
1450bff33f7eSVladimir Oltean 
145153da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1452bfcb8132SVladimir Oltean {
1453bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1454bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1455bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1456bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
1457bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1458bfcb8132SVladimir Oltean 	int port = p->dp->index;
1459bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1460bfcb8132SVladimir Oltean 	int new_master_mtu;
1461bfcb8132SVladimir Oltean 	int old_master_mtu;
1462bfcb8132SVladimir Oltean 	int mtu_limit;
1463bfcb8132SVladimir Oltean 	int cpu_mtu;
1464bfcb8132SVladimir Oltean 	int err, i;
1465bfcb8132SVladimir Oltean 
1466bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1467bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1468bfcb8132SVladimir Oltean 
1469bfcb8132SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1470bfcb8132SVladimir Oltean 		int slave_mtu;
1471bfcb8132SVladimir Oltean 
1472bfcb8132SVladimir Oltean 		if (!dsa_is_user_port(ds, i))
1473bfcb8132SVladimir Oltean 			continue;
1474bfcb8132SVladimir Oltean 
1475bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1476bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1477bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1478bfcb8132SVladimir Oltean 		 */
1479bfcb8132SVladimir Oltean 		if (!dsa_to_port(ds, i)->slave)
1480bfcb8132SVladimir Oltean 			continue;
1481bfcb8132SVladimir Oltean 
1482bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1483bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1484bfcb8132SVladimir Oltean 		 */
1485bfcb8132SVladimir Oltean 		if (i == port)
1486bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1487bfcb8132SVladimir Oltean 		else
1488bfcb8132SVladimir Oltean 			slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1489bfcb8132SVladimir Oltean 
1490bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1491bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1492bfcb8132SVladimir Oltean 	}
1493bfcb8132SVladimir Oltean 
1494bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1495bfcb8132SVladimir Oltean 
1496bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1497bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
1498bfcb8132SVladimir Oltean 	new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1499bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1500bfcb8132SVladimir Oltean 		return -ERANGE;
1501bfcb8132SVladimir Oltean 
1502bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1503bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1504bfcb8132SVladimir Oltean 	 */
1505bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1506bfcb8132SVladimir Oltean 
1507bfcb8132SVladimir Oltean 	/* Start applying stuff */
1508bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1509bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1510bfcb8132SVladimir Oltean 		if (err < 0)
1511bfcb8132SVladimir Oltean 			goto out_master_failed;
1512bfcb8132SVladimir Oltean 
1513bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
1514bfcb8132SVladimir Oltean 		 * upstream switches.
1515bfcb8132SVladimir Oltean 		 */
1516bfcb8132SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1517bfcb8132SVladimir Oltean 		if (err)
1518bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1519bfcb8132SVladimir Oltean 	}
1520bfcb8132SVladimir Oltean 
1521bfcb8132SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, false);
1522bfcb8132SVladimir Oltean 	if (err)
1523bfcb8132SVladimir Oltean 		goto out_port_failed;
1524bfcb8132SVladimir Oltean 
1525bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1526bfcb8132SVladimir Oltean 
1527bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1528bff33f7eSVladimir Oltean 
1529bfcb8132SVladimir Oltean 	return 0;
1530bfcb8132SVladimir Oltean 
1531bfcb8132SVladimir Oltean out_port_failed:
1532bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1533bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1534bfcb8132SVladimir Oltean 				    cpu_dp->tag_ops->overhead,
1535bfcb8132SVladimir Oltean 				    true);
1536bfcb8132SVladimir Oltean out_cpu_failed:
1537bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1538bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1539bfcb8132SVladimir Oltean out_master_failed:
1540bfcb8132SVladimir Oltean 	return err;
1541bfcb8132SVladimir Oltean }
1542bfcb8132SVladimir Oltean 
154391da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
154491da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
15453d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
15463d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1547aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1548c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
15496793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
15506793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
15516793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
155291da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
155391da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
155491da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
155519e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
155619e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
15577905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
15587905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1559aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1560aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1561a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1562a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1563bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1564bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
15650336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
156691da11f8SLennert Buytenhek };
156791da11f8SLennert Buytenhek 
15682a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
15694b9c9358SVladimir Oltean static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
15702a93c1a3SFlorian Fainelli 			      struct net_device *dev,
15712a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid,
157287b0984eSPetr Machata 			      u16 flags,
157387b0984eSPetr Machata 			      struct netlink_ext_ack *extack)
15742a93c1a3SFlorian Fainelli {
15752a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
15762a93c1a3SFlorian Fainelli 
15772a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
15782a93c1a3SFlorian Fainelli }
15792a93c1a3SFlorian Fainelli 
15804b9c9358SVladimir Oltean static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
15812a93c1a3SFlorian Fainelli 			      struct net_device *dev,
15822a93c1a3SFlorian Fainelli 			      const unsigned char *addr, u16 vid)
15832a93c1a3SFlorian Fainelli {
15842a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
15852a93c1a3SFlorian Fainelli 
15862a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
15872a93c1a3SFlorian Fainelli }
15882a93c1a3SFlorian Fainelli 
1589716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1590716efee2SJiri Pirko {
1591716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1592716efee2SJiri Pirko 
1593716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1594716efee2SJiri Pirko }
1595716efee2SJiri Pirko 
1596c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
1597c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
1598c2ec5f2eSOleksij Rempel {
1599c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
1600c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1601c2ec5f2eSOleksij Rempel 
1602c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
1603c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
1604c2ec5f2eSOleksij Rempel 	else
1605c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
1606c2ec5f2eSOleksij Rempel }
1607c2ec5f2eSOleksij Rempel 
16083e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1609d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1610d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
16113e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1612d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1613d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1614d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
161537b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
161637b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
16172bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1618d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1619abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
162004ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
162104ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
162204ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
162304ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
162404ff53f9SFlorian Fainelli #endif
162544bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1626f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1627c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
1628929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1629061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1630061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1631716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1632bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
163398237d43SScott Feldman };
163498237d43SScott Feldman 
1635f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1636f37db85dSFlorian Fainelli 	.name	= "dsa",
1637f37db85dSFlorian Fainelli };
1638f37db85dSFlorian Fainelli 
1639aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1640aab9c406SFlorian Fainelli {
1641aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1642aab9c406SFlorian Fainelli 
1643765bda93SRussell King 	if (dp->pl)
1644aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1645aab9c406SFlorian Fainelli }
1646aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1647aab9c406SFlorian Fainelli 
16485c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1649aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1650aab9c406SFlorian Fainelli {
16515c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1652aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1653aab9c406SFlorian Fainelli 
1654aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1655aab9c406SFlorian Fainelli 	 * not be called if it was not.
1656aab9c406SFlorian Fainelli 	 */
1657aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1658ce31b31cSFlorian Fainelli }
1659ce31b31cSFlorian Fainelli 
166091da11f8SLennert Buytenhek /* slave device setup *******************************************************/
16614fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1662c305c165SFlorian Fainelli {
1663d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1664d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1665c305c165SFlorian Fainelli 
16660115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
16670115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1668d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1669c305c165SFlorian Fainelli 		return -ENODEV;
1670d25b8e74SRussell King 	}
1671c305c165SFlorian Fainelli 
1672aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1673c305c165SFlorian Fainelli }
1674c305c165SFlorian Fainelli 
16754fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
16760d8bcdd3SFlorian Fainelli {
1677d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1678d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1679d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
16800c65b2b9SAndrew Lunn 	phy_interface_t mode;
16816819563eSFlorian Fainelli 	u32 phy_flags = 0;
16820c65b2b9SAndrew Lunn 	int ret;
16830d8bcdd3SFlorian Fainelli 
16840c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
16850c65b2b9SAndrew Lunn 	if (ret)
168619334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
16870d8bcdd3SFlorian Fainelli 
168844cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
168944cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
169044cc27e4SIoana Ciornei 
16915c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
16925c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
16935c05c1dbSRussell King 	 * this if the switch provides such a callback.
16945c05c1dbSRussell King 	 */
16955c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
16965c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
16975c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
16985c05c1dbSRussell King 	}
16995c05c1dbSRussell King 
170044cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
170177373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1702aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1703aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1704aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1705aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1706aab9c406SFlorian Fainelli 	}
1707aab9c406SFlorian Fainelli 
17089d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1709d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
17106819563eSFlorian Fainelli 
1711aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
17126146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
17136146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
17146146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
17150d8bcdd3SFlorian Fainelli 		 */
1716d945097bSVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1717d25b8e74SRussell King 		if (ret) {
1718aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1719aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1720d945097bSVivien Didelot 				   dp->index, ret);
1721aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1722c305c165SFlorian Fainelli 			return ret;
1723d25b8e74SRussell King 		}
17240d8bcdd3SFlorian Fainelli 	}
17259697f1cdSFlorian Fainelli 
17266146dd45SVladimir Oltean 	return ret;
1727b31f65fbSAndrew Lunn }
17280d8bcdd3SFlorian Fainelli 
172953da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
173053da0ebaSVladimir Oltean {
173153da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
173253da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
173353da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
173453da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
173553da0ebaSVladimir Oltean 
173653da0ebaSVladimir Oltean 	if (cpu_dp->tag_ops->tail_tag)
173753da0ebaSVladimir Oltean 		slave->needed_tailroom = cpu_dp->tag_ops->overhead;
173853da0ebaSVladimir Oltean 	else
173953da0ebaSVladimir Oltean 		slave->needed_headroom = cpu_dp->tag_ops->overhead;
174053da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
174153da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
174253da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
174353da0ebaSVladimir Oltean 	 */
174453da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
174553da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
174653da0ebaSVladimir Oltean 
174753da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
174853da0ebaSVladimir Oltean }
174953da0ebaSVladimir Oltean 
17501a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
17511a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
17521a33e10eSCong Wang 					    struct netdev_queue *txq,
17531a33e10eSCong Wang 					    void *_unused)
17541a33e10eSCong Wang {
17551a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
17561a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
17571a33e10eSCong Wang }
17581a33e10eSCong Wang 
175924462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
176024462549SFlorian Fainelli {
1761aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
176224462549SFlorian Fainelli 
1763a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1764a94c689eSFlorian Fainelli 		return 0;
1765a94c689eSFlorian Fainelli 
1766f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1767f154be24SFlorian Fainelli 
1768aab9c406SFlorian Fainelli 	rtnl_lock();
1769aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1770aab9c406SFlorian Fainelli 	rtnl_unlock();
177124462549SFlorian Fainelli 
177224462549SFlorian Fainelli 	return 0;
177324462549SFlorian Fainelli }
177424462549SFlorian Fainelli 
177524462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
177624462549SFlorian Fainelli {
1777aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1778aab9c406SFlorian Fainelli 
1779a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1780a94c689eSFlorian Fainelli 		return 0;
1781a94c689eSFlorian Fainelli 
178224462549SFlorian Fainelli 	netif_device_attach(slave_dev);
178324462549SFlorian Fainelli 
1784aab9c406SFlorian Fainelli 	rtnl_lock();
1785aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1786aab9c406SFlorian Fainelli 	rtnl_unlock();
178724462549SFlorian Fainelli 
178824462549SFlorian Fainelli 	return 0;
178924462549SFlorian Fainelli }
179024462549SFlorian Fainelli 
1791951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
179291da11f8SLennert Buytenhek {
179324a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1794f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
17954cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1796951259aaSVivien Didelot 	const char *name = port->name;
179791da11f8SLennert Buytenhek 	struct net_device *slave_dev;
179891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
179991da11f8SLennert Buytenhek 	int ret;
180091da11f8SLennert Buytenhek 
180155199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
180255199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
180355199df6SFlorian Fainelli 
180455199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
180555199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
180655199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
180791da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1808d87d6f44SGuenter Roeck 		return -ENOMEM;
180991da11f8SLennert Buytenhek 
18109b236d2aSVladimir Oltean 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
18119b236d2aSVladimir Oltean 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
18129b236d2aSVladimir Oltean 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1813f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
18142b86cb82SVladimir Oltean 	slave_dev->features |= NETIF_F_LLTX;
18157ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
18164974f9b7SPetr Štetiar 	if (!IS_ERR_OR_NULL(port->mac))
1817a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1818a2c7023fSXiaofei Shen 	else
18192fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
18200a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
18213e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1822bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1823bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1824f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1825d442ad4aSStephen Hemminger 
18261a33e10eSCong Wang 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
18271a33e10eSCong Wang 				 NULL);
18281a33e10eSCong Wang 
18294cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
18304cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
183191da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
183291da11f8SLennert Buytenhek 
183391da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
18346a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
18356a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
18365f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
18375f6b4e14SFlorian Fainelli 		return -ENOMEM;
18385f6b4e14SFlorian Fainelli 	}
1839e131a563SAlexander Lobakin 
1840e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
1841e131a563SAlexander Lobakin 	if (ret)
1842e131a563SAlexander Lobakin 		goto out_free;
1843e131a563SAlexander Lobakin 
18444cfbf09cSVivien Didelot 	p->dp = port;
1845f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
1846f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
184753da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
184891da11f8SLennert Buytenhek 
1849bfcb8132SVladimir Oltean 	rtnl_lock();
1850bfcb8132SVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1851bfcb8132SVladimir Oltean 	rtnl_unlock();
18524349abdbSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
1853bdc40a3fSRasmus Villemoes 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1854bdc40a3fSRasmus Villemoes 			 ret, ETH_DATA_LEN, port->index);
1855bfcb8132SVladimir Oltean 
185691da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
185791da11f8SLennert Buytenhek 
18584fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
18590071f56eSAndrew Lunn 	if (ret) {
1860c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
1861c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
1862c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
1863e131a563SAlexander Lobakin 		goto out_gcells;
1864e804441cSFlorian Fainelli 	}
1865e804441cSFlorian Fainelli 
18662f1e8ea7SVladimir Oltean 	rtnl_lock();
18672f1e8ea7SVladimir Oltean 
18682f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
1869e804441cSFlorian Fainelli 	if (ret) {
1870e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1871e804441cSFlorian Fainelli 			   ret, slave_dev->name);
18722f1e8ea7SVladimir Oltean 		rtnl_unlock();
1873e804441cSFlorian Fainelli 		goto out_phy;
18740071f56eSAndrew Lunn 	}
18750071f56eSAndrew Lunn 
18762f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
18772f1e8ea7SVladimir Oltean 
18782f1e8ea7SVladimir Oltean 	rtnl_unlock();
18792f1e8ea7SVladimir Oltean 
18802f1e8ea7SVladimir Oltean 	if (ret)
18812f1e8ea7SVladimir Oltean 		goto out_unregister;
18822f1e8ea7SVladimir Oltean 
1883d87d6f44SGuenter Roeck 	return 0;
1884e804441cSFlorian Fainelli 
18852f1e8ea7SVladimir Oltean out_unregister:
18862f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
1887e804441cSFlorian Fainelli out_phy:
1888aab9c406SFlorian Fainelli 	rtnl_lock();
1889aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1890aab9c406SFlorian Fainelli 	rtnl_unlock();
1891aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1892e131a563SAlexander Lobakin out_gcells:
1893e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
1894e804441cSFlorian Fainelli out_free:
18956a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
1896e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1897f8b8b1cdSVivien Didelot 	port->slave = NULL;
1898e804441cSFlorian Fainelli 	return ret;
189991da11f8SLennert Buytenhek }
1900b73adef6SFlorian Fainelli 
1901cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1902cda5c15bSNeil Armstrong {
19032f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
1904d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1905cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1906cda5c15bSNeil Armstrong 
1907cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1908aab9c406SFlorian Fainelli 	rtnl_lock();
19092f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
19102f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
1911aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
1912aab9c406SFlorian Fainelli 	rtnl_unlock();
1913881eadabSJohan Hovold 
1914aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
1915e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
19166a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
1917cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1918cda5c15bSNeil Armstrong }
1919cda5c15bSNeil Armstrong 
19204d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
1921b73adef6SFlorian Fainelli {
1922b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1923b73adef6SFlorian Fainelli }
1924a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
1925b73adef6SFlorian Fainelli 
19268e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
19278e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1928b73adef6SFlorian Fainelli {
1929d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
19308e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1931b73adef6SFlorian Fainelli 
19328e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
19338e92ab3aSVivien Didelot 		if (info->linking) {
193417d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
1935bff33f7eSVladimir Oltean 			if (!err)
1936bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
19378e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
19388e92ab3aSVivien Didelot 		} else {
193917d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
19408e92ab3aSVivien Didelot 			err = NOTIFY_OK;
19418e92ab3aSVivien Didelot 		}
1942058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
1943058102a6STobias Waldekranz 		if (info->linking) {
1944058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
1945058102a6STobias Waldekranz 						info->upper_info);
1946058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
1947058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
1948058102a6STobias Waldekranz 						   "Offloading not supported");
1949058102a6STobias Waldekranz 				err = 0;
1950058102a6STobias Waldekranz 			}
1951058102a6STobias Waldekranz 			err = notifier_from_errno(err);
1952058102a6STobias Waldekranz 		} else {
1953058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
1954058102a6STobias Waldekranz 			err = NOTIFY_OK;
1955058102a6STobias Waldekranz 		}
195618596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
195718596f50SGeorge McCollister 		if (info->linking) {
195818596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
195918596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
196018596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
196118596f50SGeorge McCollister 						   "Offloading not supported");
196218596f50SGeorge McCollister 				err = 0;
196318596f50SGeorge McCollister 			}
196418596f50SGeorge McCollister 			err = notifier_from_errno(err);
196518596f50SGeorge McCollister 		} else {
196618596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
196718596f50SGeorge McCollister 			err = NOTIFY_OK;
196818596f50SGeorge McCollister 		}
1969058102a6STobias Waldekranz 	}
1970058102a6STobias Waldekranz 
1971058102a6STobias Waldekranz 	return err;
1972058102a6STobias Waldekranz }
1973058102a6STobias Waldekranz 
1974058102a6STobias Waldekranz static int
1975058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
1976058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
1977058102a6STobias Waldekranz {
1978058102a6STobias Waldekranz 	struct net_device *lower;
1979058102a6STobias Waldekranz 	struct list_head *iter;
1980058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
1981058102a6STobias Waldekranz 	struct dsa_port *dp;
1982058102a6STobias Waldekranz 
1983058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
1984058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
1985058102a6STobias Waldekranz 			continue;
1986058102a6STobias Waldekranz 
1987058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
1988058102a6STobias Waldekranz 		if (!dp->lag_dev)
1989058102a6STobias Waldekranz 			/* Software LAG */
1990058102a6STobias Waldekranz 			continue;
1991058102a6STobias Waldekranz 
1992058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
1993058102a6STobias Waldekranz 		if (notifier_to_errno(err))
1994058102a6STobias Waldekranz 			break;
19956debb68aSVivien Didelot 	}
1996b73adef6SFlorian Fainelli 
19978e92ab3aSVivien Didelot 	return err;
1998b73adef6SFlorian Fainelli }
1999b73adef6SFlorian Fainelli 
2000eb46e8daSVladimir Oltean static int
2001eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2002eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2003cc1d5bdaSFlorian Fainelli {
2004cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
2005cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
2006cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2007cc1d5bdaSFlorian Fainelli 
2008cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2009cc1d5bdaSFlorian Fainelli 
2010cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2011cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2012cc1d5bdaSFlorian Fainelli 
2013cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2014cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2015cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2016cc1d5bdaSFlorian Fainelli 
2017cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
2018cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
2019cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2020cc1d5bdaSFlorian Fainelli 
2021cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
2022cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
2023cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2024cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2025cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2026cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2027cc1d5bdaSFlorian Fainelli 	}
2028cc1d5bdaSFlorian Fainelli 
2029cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2030cc1d5bdaSFlorian Fainelli }
2031cc1d5bdaSFlorian Fainelli 
20322b138406SVladimir Oltean static int
20332b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
20342b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
20352b138406SVladimir Oltean {
20362b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
20372b138406SVladimir Oltean 	struct net_device *br = dp->bridge_dev;
20382b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
20392b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
20402b138406SVladimir Oltean 	int err = NOTIFY_DONE;
20412b138406SVladimir Oltean 	u16 vid;
20422b138406SVladimir Oltean 
2043adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
20442b138406SVladimir Oltean 		return NOTIFY_DONE;
20452b138406SVladimir Oltean 
20462b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
20472b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
20482b138406SVladimir Oltean 
20492b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
20502b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
20512b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
20522b138406SVladimir Oltean 	 */
20532b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
20542b138406SVladimir Oltean 	if (err == 0) {
20552b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
20562b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
20572b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
20582b138406SVladimir Oltean 	}
20592b138406SVladimir Oltean 
20602b138406SVladimir Oltean 	return NOTIFY_DONE;
20612b138406SVladimir Oltean }
20622b138406SVladimir Oltean 
206388e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
2064b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
2065b73adef6SFlorian Fainelli {
20666debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2067b73adef6SFlorian Fainelli 
206883501299SVladimir Oltean 	switch (event) {
20692b138406SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
20702b138406SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
2071e358bef7SVladimir Oltean 		struct dsa_switch *ds;
2072e358bef7SVladimir Oltean 		struct dsa_port *dp;
2073e358bef7SVladimir Oltean 		int err;
20742b138406SVladimir Oltean 
207553bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
2076eb46e8daSVladimir Oltean 			return dsa_prevent_bridging_8021q_upper(dev, ptr);
20772b138406SVladimir Oltean 
2078e358bef7SVladimir Oltean 		dp = dsa_slave_to_port(dev);
2079e358bef7SVladimir Oltean 		ds = dp->ds;
2080e358bef7SVladimir Oltean 
2081e358bef7SVladimir Oltean 		if (ds->ops->port_prechangeupper) {
2082e358bef7SVladimir Oltean 			err = ds->ops->port_prechangeupper(ds, dp->index, info);
2083e358bef7SVladimir Oltean 			if (err)
2084e358bef7SVladimir Oltean 				return notifier_from_errno(err);
2085e358bef7SVladimir Oltean 		}
2086e358bef7SVladimir Oltean 
20872b138406SVladimir Oltean 		if (is_vlan_dev(info->upper_dev))
20882b138406SVladimir Oltean 			return dsa_slave_check_8021q_upper(dev, ptr);
208983501299SVladimir Oltean 		break;
20902b138406SVladimir Oltean 	}
209183501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2092058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
20938e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2094058102a6STobias Waldekranz 
2095058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2096058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2097058102a6STobias Waldekranz 
2098058102a6STobias Waldekranz 		break;
2099058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2100058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2101058102a6STobias Waldekranz 		struct dsa_port *dp;
2102058102a6STobias Waldekranz 		int err;
2103058102a6STobias Waldekranz 
2104058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2105058102a6STobias Waldekranz 			break;
2106058102a6STobias Waldekranz 
2107058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2108058102a6STobias Waldekranz 
2109058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2110058102a6STobias Waldekranz 		return notifier_from_errno(err);
2111058102a6STobias Waldekranz 	}
2112c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2113c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2114c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2115c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2116c0a8a9c2SVladimir Oltean 
2117c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2118c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2119c0a8a9c2SVladimir Oltean 
2120c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2121c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2122c0a8a9c2SVladimir Oltean 
2123c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2124c0a8a9c2SVladimir Oltean 			if (!dsa_is_user_port(dp->ds, dp->index))
2125c0a8a9c2SVladimir Oltean 				continue;
2126c0a8a9c2SVladimir Oltean 
2127c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2128c0a8a9c2SVladimir Oltean 		}
2129c0a8a9c2SVladimir Oltean 
2130c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2131c0a8a9c2SVladimir Oltean 
2132c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2133c0a8a9c2SVladimir Oltean 	}
2134c0a8a9c2SVladimir Oltean 	default:
2135c0a8a9c2SVladimir Oltean 		break;
2136cc1d5bdaSFlorian Fainelli 	}
2137b73adef6SFlorian Fainelli 
2138b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2139b73adef6SFlorian Fainelli }
214088e4f0caSVivien Didelot 
2141c4bb76a9SVladimir Oltean static void
2142c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2143c4bb76a9SVladimir Oltean {
2144c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2145c4bb76a9SVladimir Oltean 	struct switchdev_notifier_fdb_info info;
2146c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2147c4bb76a9SVladimir Oltean 
2148c4bb76a9SVladimir Oltean 	if (!dsa_is_user_port(ds, switchdev_work->port))
2149c4bb76a9SVladimir Oltean 		return;
2150c4bb76a9SVladimir Oltean 
2151c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2152c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2153c4bb76a9SVladimir Oltean 	info.offloaded = true;
2154c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2155c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2156c4bb76a9SVladimir Oltean 				 dp->slave, &info.info, NULL);
2157c4bb76a9SVladimir Oltean }
2158c9eb3e0fSArkadi Sharshevsky 
2159c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2160c9eb3e0fSArkadi Sharshevsky {
2161c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2162c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
2163c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2164c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2165c9eb3e0fSArkadi Sharshevsky 	int err;
2166c9eb3e0fSArkadi Sharshevsky 
2167c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2168c4bb76a9SVladimir Oltean 
2169c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
2170c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2171c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2172c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_add(dp, switchdev_work->addr,
2173c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2174c9eb3e0fSArkadi Sharshevsky 		if (err) {
2175c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2176c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
2177c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2178c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2179c9eb3e0fSArkadi Sharshevsky 			break;
2180c9eb3e0fSArkadi Sharshevsky 		}
2181c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2182c9eb3e0fSArkadi Sharshevsky 		break;
2183c9eb3e0fSArkadi Sharshevsky 
2184c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2185c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_del(dp, switchdev_work->addr,
2186c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2187c9eb3e0fSArkadi Sharshevsky 		if (err) {
2188c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2189c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2190c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2191c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2192c9eb3e0fSArkadi Sharshevsky 		}
21932fd18650SVladimir Oltean 
2194c9eb3e0fSArkadi Sharshevsky 		break;
2195c9eb3e0fSArkadi Sharshevsky 	}
2196c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
2197c9eb3e0fSArkadi Sharshevsky 
2198c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2199c4bb76a9SVladimir Oltean 	if (dsa_is_user_port(ds, dp->index))
2200c4bb76a9SVladimir Oltean 		dev_put(dp->slave);
2201c9eb3e0fSArkadi Sharshevsky }
2202c9eb3e0fSArkadi Sharshevsky 
2203d5f19486SVladimir Oltean static int dsa_lower_dev_walk(struct net_device *lower_dev,
2204d5f19486SVladimir Oltean 			      struct netdev_nested_priv *priv)
2205d5f19486SVladimir Oltean {
2206d5f19486SVladimir Oltean 	if (dsa_slave_dev_check(lower_dev)) {
2207d5f19486SVladimir Oltean 		priv->data = (void *)netdev_priv(lower_dev);
2208d5f19486SVladimir Oltean 		return 1;
2209d5f19486SVladimir Oltean 	}
2210d5f19486SVladimir Oltean 
2211d5f19486SVladimir Oltean 	return 0;
2212d5f19486SVladimir Oltean }
2213d5f19486SVladimir Oltean 
2214d5f19486SVladimir Oltean static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev)
2215d5f19486SVladimir Oltean {
2216d5f19486SVladimir Oltean 	struct netdev_nested_priv priv = {
2217d5f19486SVladimir Oltean 		.data = NULL,
2218d5f19486SVladimir Oltean 	};
2219d5f19486SVladimir Oltean 
2220d5f19486SVladimir Oltean 	netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv);
2221d5f19486SVladimir Oltean 
2222d5f19486SVladimir Oltean 	return (struct dsa_slave_priv *)priv.data;
2223d5f19486SVladimir Oltean }
2224d5f19486SVladimir Oltean 
2225c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2226c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2227c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2228c9eb3e0fSArkadi Sharshevsky {
2229c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2230c4bb76a9SVladimir Oltean 	const struct switchdev_notifier_fdb_info *fdb_info;
2231c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
2232c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
223379b139f4SVivien Didelot 	int err;
223479b139f4SVivien Didelot 
2235447d290aSVladimir Oltean 	switch (event) {
2236447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
223779b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
223879b139f4SVivien Didelot 						     dsa_slave_dev_check,
223979b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
224079b139f4SVivien Didelot 		return notifier_from_errno(err);
2241447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2242447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2243d5f19486SVladimir Oltean 		fdb_info = ptr;
2244d5f19486SVladimir Oltean 
2245d5f19486SVladimir Oltean 		if (dsa_slave_dev_check(dev)) {
2246d5f19486SVladimir Oltean 			if (!fdb_info->added_by_user)
2247d5f19486SVladimir Oltean 				return NOTIFY_OK;
2248c9eb3e0fSArkadi Sharshevsky 
2249c4bb76a9SVladimir Oltean 			dp = dsa_slave_to_port(dev);
2250d5f19486SVladimir Oltean 		} else {
2251d5f19486SVladimir Oltean 			/* Snoop addresses learnt on foreign interfaces
2252d5f19486SVladimir Oltean 			 * bridged with us, for switches that don't
2253d5f19486SVladimir Oltean 			 * automatically learn SA from CPU-injected traffic
2254d5f19486SVladimir Oltean 			 */
2255d5f19486SVladimir Oltean 			struct net_device *br_dev;
2256d5f19486SVladimir Oltean 			struct dsa_slave_priv *p;
2257d5f19486SVladimir Oltean 
2258d5f19486SVladimir Oltean 			br_dev = netdev_master_upper_dev_get_rcu(dev);
2259d5f19486SVladimir Oltean 			if (!br_dev)
2260d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2261d5f19486SVladimir Oltean 
2262d5f19486SVladimir Oltean 			if (!netif_is_bridge_master(br_dev))
2263d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2264d5f19486SVladimir Oltean 
2265d5f19486SVladimir Oltean 			p = dsa_slave_dev_lower_find(br_dev);
2266d5f19486SVladimir Oltean 			if (!p)
2267d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2268d5f19486SVladimir Oltean 
2269d5f19486SVladimir Oltean 			dp = p->dp->cpu_dp;
2270d5f19486SVladimir Oltean 
2271d5f19486SVladimir Oltean 			if (!dp->ds->assisted_learning_on_cpu_port)
2272d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2273a324d3d4SVladimir Oltean 
2274a324d3d4SVladimir Oltean 			/* When the bridge learns an address on an offloaded
2275a324d3d4SVladimir Oltean 			 * LAG we don't want to send traffic to the CPU, the
2276a324d3d4SVladimir Oltean 			 * other ports bridged with the LAG should be able to
2277a324d3d4SVladimir Oltean 			 * autonomously forward towards it.
2278a324d3d4SVladimir Oltean 			 */
2279a324d3d4SVladimir Oltean 			if (dsa_tree_offloads_netdev(dp->ds->dst, dev))
2280a324d3d4SVladimir Oltean 				return NOTIFY_DONE;
2281d5f19486SVladimir Oltean 		}
2282c4bb76a9SVladimir Oltean 
22835fb4a451SVladimir Oltean 		if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del)
22845fb4a451SVladimir Oltean 			return NOTIFY_DONE;
22855fb4a451SVladimir Oltean 
2286c9eb3e0fSArkadi Sharshevsky 		switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2287c9eb3e0fSArkadi Sharshevsky 		if (!switchdev_work)
2288c9eb3e0fSArkadi Sharshevsky 			return NOTIFY_BAD;
2289c9eb3e0fSArkadi Sharshevsky 
2290c9eb3e0fSArkadi Sharshevsky 		INIT_WORK(&switchdev_work->work,
2291c9eb3e0fSArkadi Sharshevsky 			  dsa_slave_switchdev_event_work);
2292c4bb76a9SVladimir Oltean 		switchdev_work->ds = dp->ds;
2293c4bb76a9SVladimir Oltean 		switchdev_work->port = dp->index;
2294c9eb3e0fSArkadi Sharshevsky 		switchdev_work->event = event;
2295c9eb3e0fSArkadi Sharshevsky 
2296c4bb76a9SVladimir Oltean 		ether_addr_copy(switchdev_work->addr,
2297c4bb76a9SVladimir Oltean 				fdb_info->addr);
2298c4bb76a9SVladimir Oltean 		switchdev_work->vid = fdb_info->vid;
2299c4bb76a9SVladimir Oltean 
2300d5f19486SVladimir Oltean 		/* Hold a reference on the slave for dsa_fdb_offload_notify */
2301d5f19486SVladimir Oltean 		if (dsa_is_user_port(dp->ds, dp->index))
2302c9eb3e0fSArkadi Sharshevsky 			dev_hold(dev);
2303447d290aSVladimir Oltean 		dsa_schedule_work(&switchdev_work->work);
2304c9eb3e0fSArkadi Sharshevsky 		break;
2305c9eb3e0fSArkadi Sharshevsky 	default:
2306c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2307c9eb3e0fSArkadi Sharshevsky 	}
2308c9eb3e0fSArkadi Sharshevsky 
2309c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2310c9eb3e0fSArkadi Sharshevsky }
2311c9eb3e0fSArkadi Sharshevsky 
23122b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
23132b239f67SPetr Machata 					      unsigned long event, void *ptr)
23142b239f67SPetr Machata {
23152b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
231679b139f4SVivien Didelot 	int err;
23172b239f67SPetr Machata 
23182b239f67SPetr Machata 	switch (event) {
231979b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
232079b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
232179b139f4SVivien Didelot 						    dsa_slave_dev_check,
232279b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
232379b139f4SVivien Didelot 		return notifier_from_errno(err);
23242b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
232579b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
232679b139f4SVivien Didelot 						    dsa_slave_dev_check,
232779b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
232879b139f4SVivien Didelot 		return notifier_from_errno(err);
23299ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
233079b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
233179b139f4SVivien Didelot 						     dsa_slave_dev_check,
233279b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
233379b139f4SVivien Didelot 		return notifier_from_errno(err);
23342b239f67SPetr Machata 	}
23352b239f67SPetr Machata 
23362b239f67SPetr Machata 	return NOTIFY_DONE;
23372b239f67SPetr Machata }
23382b239f67SPetr Machata 
233988e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
234088e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
234188e4f0caSVivien Didelot };
234288e4f0caSVivien Didelot 
2343c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
2344c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2345c9eb3e0fSArkadi Sharshevsky };
2346c9eb3e0fSArkadi Sharshevsky 
23472b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
23482b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
23492b239f67SPetr Machata };
23502b239f67SPetr Machata 
235188e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
235288e4f0caSVivien Didelot {
23532b239f67SPetr Machata 	struct notifier_block *nb;
2354c9eb3e0fSArkadi Sharshevsky 	int err;
2355c9eb3e0fSArkadi Sharshevsky 
2356c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2357c9eb3e0fSArkadi Sharshevsky 	if (err)
2358c9eb3e0fSArkadi Sharshevsky 		return err;
2359c9eb3e0fSArkadi Sharshevsky 
2360c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2361c9eb3e0fSArkadi Sharshevsky 	if (err)
2362c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2363c9eb3e0fSArkadi Sharshevsky 
23642b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
23652b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
23662b239f67SPetr Machata 	if (err)
23672b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
23682b239f67SPetr Machata 
2369c9eb3e0fSArkadi Sharshevsky 	return 0;
2370c9eb3e0fSArkadi Sharshevsky 
23712b239f67SPetr Machata err_switchdev_blocking_nb:
23722b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2373c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2374c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2375c9eb3e0fSArkadi Sharshevsky 	return err;
237688e4f0caSVivien Didelot }
237788e4f0caSVivien Didelot 
237888e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
237988e4f0caSVivien Didelot {
23802b239f67SPetr Machata 	struct notifier_block *nb;
238188e4f0caSVivien Didelot 	int err;
238288e4f0caSVivien Didelot 
23832b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
23842b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
23852b239f67SPetr Machata 	if (err)
23862b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
23872b239f67SPetr Machata 
2388c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2389c9eb3e0fSArkadi Sharshevsky 	if (err)
2390c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2391c9eb3e0fSArkadi Sharshevsky 
239288e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
239388e4f0caSVivien Didelot 	if (err)
239488e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
239588e4f0caSVivien Didelot }
2396