xref: /openbmc/linux/net/dsa/slave.c (revision a5e3c9ba9258dbe55cba0cc3804675c9f1eaa169)
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>
2004ff53f9SFlorian Fainelli #include <linux/netpoll.h>
2190af1059SBrandon Streiff #include <linux/ptp_classify.h>
22ea5dd34bSVivien Didelot 
2391da11f8SLennert Buytenhek #include "dsa_priv.h"
2491da11f8SLennert Buytenhek 
2591da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
2691da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2791da11f8SLennert Buytenhek {
2891da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
2991da11f8SLennert Buytenhek 
300d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
319d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3291da11f8SLennert Buytenhek 
3391da11f8SLennert Buytenhek 	return 0xffff;
3491da11f8SLennert Buytenhek }
3591da11f8SLennert Buytenhek 
3691da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
3791da11f8SLennert Buytenhek {
3891da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3991da11f8SLennert Buytenhek 
400d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
419d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4291da11f8SLennert Buytenhek 
4391da11f8SLennert Buytenhek 	return 0;
4491da11f8SLennert Buytenhek }
4591da11f8SLennert Buytenhek 
4691da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
4791da11f8SLennert Buytenhek {
4891da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
4991da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5091da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5191da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
520b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
5349463b7fSVivien Didelot 		 ds->dst->index, ds->index);
54c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
5524df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
5691da11f8SLennert Buytenhek }
5791da11f8SLennert Buytenhek 
5891da11f8SLennert Buytenhek 
5991da11f8SLennert Buytenhek /* slave device handling ****************************************************/
60abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
61c0840801SLennert Buytenhek {
62d0006b00SVivien Didelot 	return dsa_slave_to_master(dev)->ifindex;
63c0840801SLennert Buytenhek }
64c0840801SLennert Buytenhek 
6591da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
6691da11f8SLennert Buytenhek {
67d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
68d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
69df02c6ffSLennert Buytenhek 	int err;
70df02c6ffSLennert Buytenhek 
71df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
72df02c6ffSLennert Buytenhek 		return -ENETDOWN;
73df02c6ffSLennert Buytenhek 
748feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
75a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
76df02c6ffSLennert Buytenhek 		if (err < 0)
77df02c6ffSLennert Buytenhek 			goto out;
78df02c6ffSLennert Buytenhek 	}
79df02c6ffSLennert Buytenhek 
80df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
81df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
82df02c6ffSLennert Buytenhek 		if (err < 0)
83df02c6ffSLennert Buytenhek 			goto del_unicast;
84df02c6ffSLennert Buytenhek 	}
85df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
86df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
87df02c6ffSLennert Buytenhek 		if (err < 0)
88df02c6ffSLennert Buytenhek 			goto clear_allmulti;
89df02c6ffSLennert Buytenhek 	}
90df02c6ffSLennert Buytenhek 
918640f8dcSRussell King 	err = dsa_port_enable_rt(dp, dev->phydev);
92b2f2af21SFlorian Fainelli 	if (err)
93b2f2af21SFlorian Fainelli 		goto clear_promisc;
94b73adef6SFlorian Fainelli 
9591da11f8SLennert Buytenhek 	return 0;
96df02c6ffSLennert Buytenhek 
97b2f2af21SFlorian Fainelli clear_promisc:
98b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
994fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
100df02c6ffSLennert Buytenhek clear_allmulti:
101df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
102df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
103df02c6ffSLennert Buytenhek del_unicast:
1048feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
105a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
106df02c6ffSLennert Buytenhek out:
107df02c6ffSLennert Buytenhek 	return err;
10891da11f8SLennert Buytenhek }
10991da11f8SLennert Buytenhek 
11091da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
11191da11f8SLennert Buytenhek {
112d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
113d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
114df02c6ffSLennert Buytenhek 
1158640f8dcSRussell King 	dsa_port_disable_rt(dp);
1166457edfeSVivien Didelot 
117df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
118a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
119df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
120df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
121df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
122df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
123df02c6ffSLennert Buytenhek 
1248feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
125a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
126df02c6ffSLennert Buytenhek 
12791da11f8SLennert Buytenhek 	return 0;
12891da11f8SLennert Buytenhek }
12991da11f8SLennert Buytenhek 
13091da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
13191da11f8SLennert Buytenhek {
132d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
13317ab4f61SRundong Ge 	if (dev->flags & IFF_UP) {
13491da11f8SLennert Buytenhek 		if (change & IFF_ALLMULTI)
13517ab4f61SRundong Ge 			dev_set_allmulti(master,
13617ab4f61SRundong Ge 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
13791da11f8SLennert Buytenhek 		if (change & IFF_PROMISC)
13817ab4f61SRundong Ge 			dev_set_promiscuity(master,
13917ab4f61SRundong Ge 					    dev->flags & IFF_PROMISC ? 1 : -1);
14017ab4f61SRundong Ge 	}
14191da11f8SLennert Buytenhek }
14291da11f8SLennert Buytenhek 
14391da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
14491da11f8SLennert Buytenhek {
145d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
14691da11f8SLennert Buytenhek 
14791da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
148a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
14991da11f8SLennert Buytenhek }
15091da11f8SLennert Buytenhek 
151df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
15291da11f8SLennert Buytenhek {
153d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
154df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
155df02c6ffSLennert Buytenhek 	int err;
156df02c6ffSLennert Buytenhek 
157df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
158df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
159df02c6ffSLennert Buytenhek 
160df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
161df02c6ffSLennert Buytenhek 		goto out;
162df02c6ffSLennert Buytenhek 
1638feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
164a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
165df02c6ffSLennert Buytenhek 		if (err < 0)
166df02c6ffSLennert Buytenhek 			return err;
167df02c6ffSLennert Buytenhek 	}
168df02c6ffSLennert Buytenhek 
1698feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
170a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
171df02c6ffSLennert Buytenhek 
172df02c6ffSLennert Buytenhek out:
173d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
17491da11f8SLennert Buytenhek 
17591da11f8SLennert Buytenhek 	return 0;
17691da11f8SLennert Buytenhek }
17791da11f8SLennert Buytenhek 
1782bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1792bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1802bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1812bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1822bedde1aSArkadi Sharshevsky 	int idx;
1832bedde1aSArkadi Sharshevsky };
1842bedde1aSArkadi Sharshevsky 
1852bedde1aSArkadi Sharshevsky static int
1862bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1872bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
1882bedde1aSArkadi Sharshevsky {
1892bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
1902bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1912bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
1922bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
1932bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
1942bedde1aSArkadi Sharshevsky 
1952bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
1962bedde1aSArkadi Sharshevsky 		goto skip;
1972bedde1aSArkadi Sharshevsky 
1982bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1992bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2002bedde1aSArkadi Sharshevsky 	if (!nlh)
2012bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2022bedde1aSArkadi Sharshevsky 
2032bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2042bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2052bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2062bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2072bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2082bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2092bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2102bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2112bedde1aSArkadi Sharshevsky 
2122bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2132bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2142bedde1aSArkadi Sharshevsky 
2152bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2162bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2172bedde1aSArkadi Sharshevsky 
2182bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2192bedde1aSArkadi Sharshevsky 
2202bedde1aSArkadi Sharshevsky skip:
2212bedde1aSArkadi Sharshevsky 	dump->idx++;
2222bedde1aSArkadi Sharshevsky 	return 0;
2232bedde1aSArkadi Sharshevsky 
2242bedde1aSArkadi Sharshevsky nla_put_failure:
2252bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2262bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2272bedde1aSArkadi Sharshevsky }
2282bedde1aSArkadi Sharshevsky 
2292bedde1aSArkadi Sharshevsky static int
2302bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2312bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2322bedde1aSArkadi Sharshevsky 		   int *idx)
2332bedde1aSArkadi Sharshevsky {
234d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2352bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2362bedde1aSArkadi Sharshevsky 		.dev = dev,
2372bedde1aSArkadi Sharshevsky 		.skb = skb,
2382bedde1aSArkadi Sharshevsky 		.cb = cb,
2392bedde1aSArkadi Sharshevsky 		.idx = *idx,
2402bedde1aSArkadi Sharshevsky 	};
2412bedde1aSArkadi Sharshevsky 	int err;
2422bedde1aSArkadi Sharshevsky 
243de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2442bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
245de40fc5dSVivien Didelot 
2462bedde1aSArkadi Sharshevsky 	return err;
2472bedde1aSArkadi Sharshevsky }
2482bedde1aSArkadi Sharshevsky 
24991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
25091da11f8SLennert Buytenhek {
2510336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2520336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2530336369dSBrandon Streiff 	int port = p->dp->index;
2540336369dSBrandon Streiff 
2550336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2560336369dSBrandon Streiff 	switch (cmd) {
2570336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2580336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2590336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2600336369dSBrandon Streiff 		break;
2610336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2620336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2630336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2640336369dSBrandon Streiff 		break;
2650336369dSBrandon Streiff 	}
2660336369dSBrandon Streiff 
267aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
26891da11f8SLennert Buytenhek }
26991da11f8SLennert Buytenhek 
27035636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
271f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
2727ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
27335636062SScott Feldman {
274d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
275b8d866acSVivien Didelot 	int ret;
27635636062SScott Feldman 
27735636062SScott Feldman 	switch (attr->id) {
2781f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
279fd364541SVivien Didelot 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
28035636062SScott Feldman 		break;
281fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
282c02c4175SVivien Didelot 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
283c02c4175SVivien Didelot 					      trans);
284fb2dabadSVivien Didelot 		break;
28534a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
286072bb190SVivien Didelot 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
28734a79f63SVivien Didelot 		break;
288ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
289ea87005aSFlorian Fainelli 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
290ea87005aSFlorian Fainelli 						trans);
291ea87005aSFlorian Fainelli 		break;
29257652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
29357652796SRussell King 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
29457652796SRussell King 		break;
29508cc83ccSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
29608cc83ccSVivien Didelot 		ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter, trans);
29708cc83ccSVivien Didelot 		break;
29835636062SScott Feldman 	default:
29935636062SScott Feldman 		ret = -EOPNOTSUPP;
30035636062SScott Feldman 		break;
30135636062SScott Feldman 	}
30235636062SScott Feldman 
30335636062SScott Feldman 	return ret;
30435636062SScott Feldman }
30535636062SScott Feldman 
3061ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */
3071ce39f0eSVladimir Oltean static int
3081ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
3091ce39f0eSVladimir Oltean 				      const struct switchdev_obj_port_vlan *vlan)
3101ce39f0eSVladimir Oltean {
3111ce39f0eSVladimir Oltean 	struct net_device *upper_dev;
3121ce39f0eSVladimir Oltean 	struct list_head *iter;
3131ce39f0eSVladimir Oltean 
3141ce39f0eSVladimir Oltean 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
3151ce39f0eSVladimir Oltean 		u16 vid;
3161ce39f0eSVladimir Oltean 
3171ce39f0eSVladimir Oltean 		if (!is_vlan_dev(upper_dev))
3181ce39f0eSVladimir Oltean 			continue;
3191ce39f0eSVladimir Oltean 
3201ce39f0eSVladimir Oltean 		vid = vlan_dev_vlan_id(upper_dev);
3211ce39f0eSVladimir Oltean 		if (vid >= vlan->vid_begin && vid <= vlan->vid_end)
3221ce39f0eSVladimir Oltean 			return -EBUSY;
3231ce39f0eSVladimir Oltean 	}
3241ce39f0eSVladimir Oltean 
3251ce39f0eSVladimir Oltean 	return 0;
3261ce39f0eSVladimir Oltean }
3271ce39f0eSVladimir Oltean 
328bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
329bdcff080SVivien Didelot 			      const struct switchdev_obj *obj,
330bdcff080SVivien Didelot 			      struct switchdev_trans *trans)
331bdcff080SVivien Didelot {
3322209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
333bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
334bdcff080SVivien Didelot 	struct switchdev_obj_port_vlan vlan;
3352209158cSVladimir Oltean 	int vid, err;
336bdcff080SVivien Didelot 
337bdcff080SVivien Didelot 	if (obj->orig_dev != dev)
338bdcff080SVivien Didelot 		return -EOPNOTSUPP;
339bdcff080SVivien Didelot 
34054a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
341c5335d73SVivien Didelot 		return 0;
342c5335d73SVivien Didelot 
343bdcff080SVivien Didelot 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
344bdcff080SVivien Didelot 
3451ce39f0eSVladimir Oltean 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
3461ce39f0eSVladimir Oltean 	 * the same VID.
3471ce39f0eSVladimir Oltean 	 */
348adb256ebSVladimir Oltean 	if (trans->ph_prepare && br_vlan_enabled(dp->bridge_dev)) {
3491ce39f0eSVladimir Oltean 		rcu_read_lock();
3501ce39f0eSVladimir Oltean 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
3511ce39f0eSVladimir Oltean 		rcu_read_unlock();
3521ce39f0eSVladimir Oltean 		if (err)
3531ce39f0eSVladimir Oltean 			return err;
3541ce39f0eSVladimir Oltean 	}
3551ce39f0eSVladimir Oltean 
356bdcff080SVivien Didelot 	err = dsa_port_vlan_add(dp, &vlan, trans);
357bdcff080SVivien Didelot 	if (err)
358bdcff080SVivien Didelot 		return err;
359bdcff080SVivien Didelot 
360b9499904SVivien Didelot 	/* We need the dedicated CPU port to be a member of the VLAN as well.
361b9499904SVivien Didelot 	 * Even though drivers often handle CPU membership in special ways,
362b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
363b9499904SVivien Didelot 	 */
364b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
365b9499904SVivien Didelot 
3667e1741b4SVivien Didelot 	err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
3677e1741b4SVivien Didelot 	if (err)
3687e1741b4SVivien Didelot 		return err;
3697e1741b4SVivien Didelot 
3702209158cSVladimir Oltean 	for (vid = vlan.vid_begin; vid <= vlan.vid_end; vid++) {
3712209158cSVladimir Oltean 		err = vlan_vid_add(master, htons(ETH_P_8021Q), vid);
3722209158cSVladimir Oltean 		if (err)
3732209158cSVladimir Oltean 			return err;
3742209158cSVladimir Oltean 	}
3752209158cSVladimir Oltean 
376bdcff080SVivien Didelot 	return 0;
377bdcff080SVivien Didelot }
378bdcff080SVivien Didelot 
379ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
380648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
38179b139f4SVivien Didelot 				  struct switchdev_trans *trans,
38279b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
383ba14d9ebSVivien Didelot {
384d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
385ba14d9ebSVivien Didelot 	int err;
386ba14d9ebSVivien Didelot 
387ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
388ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
389ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
390ba14d9ebSVivien Didelot 	 */
391ba14d9ebSVivien Didelot 
3929e8f4a54SJiri Pirko 	switch (obj->id) {
3938df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
39479b139f4SVivien Didelot 		if (obj->orig_dev != dev)
39579b139f4SVivien Didelot 			return -EOPNOTSUPP;
396bcebb976SVivien Didelot 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
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 		 */
4025f4dbc50SAndrew Lunn 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
4035f4dbc50SAndrew Lunn 				       trans);
4045f4dbc50SAndrew Lunn 		break;
40557d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
406bdcff080SVivien Didelot 		err = dsa_slave_vlan_add(dev, obj, trans);
40711149536SVivien Didelot 		break;
408ba14d9ebSVivien Didelot 	default:
409ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
410ba14d9ebSVivien Didelot 		break;
411ba14d9ebSVivien Didelot 	}
412ba14d9ebSVivien Didelot 
413ba14d9ebSVivien Didelot 	return err;
414ba14d9ebSVivien Didelot }
415ba14d9ebSVivien Didelot 
416bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
417bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
418bdcff080SVivien Didelot {
4192209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
420bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
4212209158cSVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
4222209158cSVladimir Oltean 	int vid, err;
423bdcff080SVivien Didelot 
424bdcff080SVivien Didelot 	if (obj->orig_dev != dev)
425bdcff080SVivien Didelot 		return -EOPNOTSUPP;
426bdcff080SVivien Didelot 
42754a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
428c5335d73SVivien Didelot 		return 0;
429c5335d73SVivien Didelot 
4302209158cSVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
4312209158cSVladimir Oltean 
4327e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
4337e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
4347e1741b4SVivien Didelot 	 */
4352209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, vlan);
4362209158cSVladimir Oltean 	if (err)
4372209158cSVladimir Oltean 		return err;
4382209158cSVladimir Oltean 
4392209158cSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
4402209158cSVladimir Oltean 		vlan_vid_del(master, htons(ETH_P_8021Q), vid);
4412209158cSVladimir Oltean 
4422209158cSVladimir Oltean 	return 0;
443bdcff080SVivien Didelot }
444bdcff080SVivien Didelot 
445ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
446648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
447ba14d9ebSVivien Didelot {
448d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
449ba14d9ebSVivien Didelot 	int err;
450ba14d9ebSVivien Didelot 
4519e8f4a54SJiri Pirko 	switch (obj->id) {
4528df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
45379b139f4SVivien Didelot 		if (obj->orig_dev != dev)
45479b139f4SVivien Didelot 			return -EOPNOTSUPP;
455bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4568df30255SVivien Didelot 		break;
4575f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
4585f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
4595f4dbc50SAndrew Lunn 		 * but on the CPU port.
4605f4dbc50SAndrew Lunn 		 */
4615f4dbc50SAndrew Lunn 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
4625f4dbc50SAndrew Lunn 		break;
46357d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
464bdcff080SVivien Didelot 		err = dsa_slave_vlan_del(dev, obj);
46511149536SVivien Didelot 		break;
466ba14d9ebSVivien Didelot 	default:
467ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
468ba14d9ebSVivien Didelot 		break;
469ba14d9ebSVivien Didelot 	}
470ba14d9ebSVivien Didelot 
471ba14d9ebSVivien Didelot 	return err;
472ba14d9ebSVivien Didelot }
473ba14d9ebSVivien Didelot 
474929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
475929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
476b73adef6SFlorian Fainelli {
477d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
478d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
479a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
480b73adef6SFlorian Fainelli 
48115b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
48215b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
48315b04aceSJiri Pirko 	 * should be removed with legacy support.
48415b04aceSJiri Pirko 	 */
48515b04aceSJiri Pirko 	if (dp->ds->devlink)
48615b04aceSJiri Pirko 		return -EOPNOTSUPP;
48715b04aceSJiri Pirko 
488929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
489929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
490929d6c14SFlorian Fainelli 
491929d6c14SFlorian Fainelli 	return 0;
492929d6c14SFlorian Fainelli }
493929d6c14SFlorian Fainelli 
4944fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
49504ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
49604ff53f9SFlorian Fainelli {
49704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
4984fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
4994fa7b718SVivien Didelot 
500f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
50104ff53f9SFlorian Fainelli #else
50204ff53f9SFlorian Fainelli 	BUG();
50304ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
504f78ed220SEric Dumazet #endif
50504ff53f9SFlorian Fainelli }
50604ff53f9SFlorian Fainelli 
50790af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
50890af1059SBrandon Streiff 				 struct sk_buff *skb)
50990af1059SBrandon Streiff {
51090af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
51190af1059SBrandon Streiff 	struct sk_buff *clone;
51290af1059SBrandon Streiff 	unsigned int type;
51390af1059SBrandon Streiff 
51490af1059SBrandon Streiff 	type = ptp_classify_raw(skb);
51590af1059SBrandon Streiff 	if (type == PTP_CLASS_NONE)
51690af1059SBrandon Streiff 		return;
51790af1059SBrandon Streiff 
51890af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
51990af1059SBrandon Streiff 		return;
52090af1059SBrandon Streiff 
52190af1059SBrandon Streiff 	clone = skb_clone_sk(skb);
52290af1059SBrandon Streiff 	if (!clone)
52390af1059SBrandon Streiff 		return;
52490af1059SBrandon Streiff 
52530abc9cdSChristian Eggers 	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) {
526146d442cSVladimir Oltean 		DSA_SKB_CB(skb)->clone = clone;
52790af1059SBrandon Streiff 		return;
52830abc9cdSChristian Eggers 	}
52990af1059SBrandon Streiff 
53090af1059SBrandon Streiff 	kfree_skb(clone);
53190af1059SBrandon Streiff }
53290af1059SBrandon Streiff 
53397a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
53497a69a0dSVladimir Oltean {
53597a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
53697a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
53797a69a0dSVladimir Oltean 	 */
53897a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
53997a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
54097a69a0dSVladimir Oltean 
54197a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
54297a69a0dSVladimir Oltean 	 * do not modify its EtherType
54397a69a0dSVladimir Oltean 	 */
54497a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
54597a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
54697a69a0dSVladimir Oltean 
54797a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
54897a69a0dSVladimir Oltean }
54997a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
55097a69a0dSVladimir Oltean 
551a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
552a3b0b647SVladimir Oltean {
553a3b0b647SVladimir Oltean 	int needed_headroom = dev->needed_headroom;
554a3b0b647SVladimir Oltean 	int needed_tailroom = dev->needed_tailroom;
555a3b0b647SVladimir Oltean 
556a3b0b647SVladimir Oltean 	/* For tail taggers, we need to pad short frames ourselves, to ensure
557a3b0b647SVladimir Oltean 	 * that the tail tag does not fail at its role of being at the end of
558a3b0b647SVladimir Oltean 	 * the packet, once the master interface pads the frame. Account for
559a3b0b647SVladimir Oltean 	 * that pad length here, and pad later.
560a3b0b647SVladimir Oltean 	 */
561a3b0b647SVladimir Oltean 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
562a3b0b647SVladimir Oltean 		needed_tailroom += ETH_ZLEN - skb->len;
563a3b0b647SVladimir Oltean 	/* skb_headroom() returns unsigned int... */
564a3b0b647SVladimir Oltean 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
565a3b0b647SVladimir Oltean 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
566a3b0b647SVladimir Oltean 
567a3b0b647SVladimir Oltean 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
568a3b0b647SVladimir Oltean 		/* No reallocation needed, yay! */
569a3b0b647SVladimir Oltean 		return 0;
570a3b0b647SVladimir Oltean 
571a3b0b647SVladimir Oltean 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
572a3b0b647SVladimir Oltean 				GFP_ATOMIC);
573a3b0b647SVladimir Oltean }
574a3b0b647SVladimir Oltean 
5753e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
5763e8a72d1SFlorian Fainelli {
5773e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
5784ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
5793e8a72d1SFlorian Fainelli 
5806a900628SHeiner Kallweit 	dev_sw_netstats_tx_add(dev, 1, skb->len);
5813e8a72d1SFlorian Fainelli 
582146d442cSVladimir Oltean 	DSA_SKB_CB(skb)->clone = NULL;
58387671375SVladimir Oltean 
58490af1059SBrandon Streiff 	/* Identify PTP protocol packets, clone them, and pass them to the
58590af1059SBrandon Streiff 	 * switch driver
58690af1059SBrandon Streiff 	 */
58790af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
58890af1059SBrandon Streiff 
589a3b0b647SVladimir Oltean 	if (dsa_realloc_skb(skb, dev)) {
590a3b0b647SVladimir Oltean 		dev_kfree_skb_any(skb);
591a3b0b647SVladimir Oltean 		return NETDEV_TX_OK;
592a3b0b647SVladimir Oltean 	}
593a3b0b647SVladimir Oltean 
594a3b0b647SVladimir Oltean 	/* needed_tailroom should still be 'warm' in the cache line from
595a3b0b647SVladimir Oltean 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
596a3b0b647SVladimir Oltean 	 */
597a3b0b647SVladimir Oltean 	if (dev->needed_tailroom)
598a3b0b647SVladimir Oltean 		eth_skb_pad(skb);
599a3b0b647SVladimir Oltean 
600fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
601fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
602fe47d563SVivien Didelot 	 */
6034ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
604fe47d563SVivien Didelot 	if (!nskb) {
605fe47d563SVivien Didelot 		kfree_skb(skb);
6064ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
607fe47d563SVivien Didelot 	}
6085aed85ceSFlorian Fainelli 
60997a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
61097a69a0dSVladimir Oltean }
61104ff53f9SFlorian Fainelli 
61291da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
61391da11f8SLennert Buytenhek 
61491da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
61591da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
61691da11f8SLennert Buytenhek {
6177826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
6187826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
6197826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
62091da11f8SLennert Buytenhek }
62191da11f8SLennert Buytenhek 
6223d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
6233d762a0fSGuenter Roeck {
624d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
625d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6263d762a0fSGuenter Roeck 
6279d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
628d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
6293d762a0fSGuenter Roeck 
6303d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
6313d762a0fSGuenter Roeck }
6323d762a0fSGuenter Roeck 
6333d762a0fSGuenter Roeck static void
6343d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
6353d762a0fSGuenter Roeck {
636d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
637d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6383d762a0fSGuenter Roeck 
6399d490b4eSVivien Didelot 	if (ds->ops->get_regs)
640d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
6413d762a0fSGuenter Roeck }
6423d762a0fSGuenter Roeck 
643aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
644aab9c406SFlorian Fainelli {
645aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
646aab9c406SFlorian Fainelli 
647aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
648aab9c406SFlorian Fainelli }
649aab9c406SFlorian Fainelli 
6506793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
6516793abb4SGuenter Roeck {
652d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
653d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6546793abb4SGuenter Roeck 
6550e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
656ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
6576793abb4SGuenter Roeck 
6589d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
6599d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
6606793abb4SGuenter Roeck 
6616793abb4SGuenter Roeck 	return 0;
6626793abb4SGuenter Roeck }
6636793abb4SGuenter Roeck 
6646793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
6656793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6666793abb4SGuenter Roeck {
667d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
668d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6696793abb4SGuenter Roeck 
6709d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
6719d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
6726793abb4SGuenter Roeck 
6736793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6746793abb4SGuenter Roeck }
6756793abb4SGuenter Roeck 
6766793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
6776793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
6786793abb4SGuenter Roeck {
679d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
680d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6816793abb4SGuenter Roeck 
6829d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
6839d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
6846793abb4SGuenter Roeck 
6856793abb4SGuenter Roeck 	return -EOPNOTSUPP;
6866793abb4SGuenter Roeck }
6876793abb4SGuenter Roeck 
68891da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
68991da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
69091da11f8SLennert Buytenhek {
691d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
692d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
69391da11f8SLennert Buytenhek 
69491da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
69591da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
69691da11f8SLennert Buytenhek 
69791da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
69891da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
69991da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
70091da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
7019d490b4eSVivien Didelot 		if (ds->ops->get_strings)
70289f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
70389f09048SFlorian Fainelli 					     data + 4 * len);
70491da11f8SLennert Buytenhek 	}
70591da11f8SLennert Buytenhek }
70691da11f8SLennert Buytenhek 
70791da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
70891da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
70991da11f8SLennert Buytenhek 					uint64_t *data)
71091da11f8SLennert Buytenhek {
711d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
712d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7135f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
714f613ed66SFlorian Fainelli 	unsigned int start;
7155f6b4e14SFlorian Fainelli 	int i;
71691da11f8SLennert Buytenhek 
7175f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
7185f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
7195f6b4e14SFlorian Fainelli 
7206a900628SHeiner Kallweit 		s = per_cpu_ptr(dev->tstats, i);
721f613ed66SFlorian Fainelli 		do {
7225f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
7235f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
7245f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
7255f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
7265f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
7275f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
7285f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
7295f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
7305f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
7315f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
7325f6b4e14SFlorian Fainelli 	}
7339d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
734d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
73591da11f8SLennert Buytenhek }
73691da11f8SLennert Buytenhek 
73791da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
73891da11f8SLennert Buytenhek {
739d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
740d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
74191da11f8SLennert Buytenhek 
74291da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
74391da11f8SLennert Buytenhek 		int count;
74491da11f8SLennert Buytenhek 
74591da11f8SLennert Buytenhek 		count = 4;
7469d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
74789f09048SFlorian Fainelli 			count += ds->ops->get_sset_count(ds, dp->index, sset);
74891da11f8SLennert Buytenhek 
74991da11f8SLennert Buytenhek 		return count;
75091da11f8SLennert Buytenhek 	}
75191da11f8SLennert Buytenhek 
75291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
75391da11f8SLennert Buytenhek }
75491da11f8SLennert Buytenhek 
75519e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
75619e57c4eSFlorian Fainelli {
757d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
758d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
75919e57c4eSFlorian Fainelli 
760aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
761aab9c406SFlorian Fainelli 
7629d490b4eSVivien Didelot 	if (ds->ops->get_wol)
763d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
76419e57c4eSFlorian Fainelli }
76519e57c4eSFlorian Fainelli 
76619e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
76719e57c4eSFlorian Fainelli {
768d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
769d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
77019e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
77119e57c4eSFlorian Fainelli 
772aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
773aab9c406SFlorian Fainelli 
7749d490b4eSVivien Didelot 	if (ds->ops->set_wol)
775d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
77619e57c4eSFlorian Fainelli 
77719e57c4eSFlorian Fainelli 	return ret;
77819e57c4eSFlorian Fainelli }
77919e57c4eSFlorian Fainelli 
7807905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
7817905288fSFlorian Fainelli {
782d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
783d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7847905288fSFlorian Fainelli 	int ret;
7857905288fSFlorian Fainelli 
7867b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
78700670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
7887b9cc738SVivien Didelot 		return -ENODEV;
7897b9cc738SVivien Didelot 
79008f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
7917905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7927905288fSFlorian Fainelli 
793d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
7947905288fSFlorian Fainelli 	if (ret)
7957905288fSFlorian Fainelli 		return ret;
7967905288fSFlorian Fainelli 
797aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
7987905288fSFlorian Fainelli }
7997905288fSFlorian Fainelli 
8007905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
8017905288fSFlorian Fainelli {
802d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
803d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8047905288fSFlorian Fainelli 	int ret;
8057905288fSFlorian Fainelli 
8067b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
80700670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
8087b9cc738SVivien Didelot 		return -ENODEV;
8097b9cc738SVivien Didelot 
81008f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
8117905288fSFlorian Fainelli 		return -EOPNOTSUPP;
8127905288fSFlorian Fainelli 
813d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
8147905288fSFlorian Fainelli 	if (ret)
8157905288fSFlorian Fainelli 		return ret;
8167905288fSFlorian Fainelli 
817aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
818aab9c406SFlorian Fainelli }
819aab9c406SFlorian Fainelli 
820aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
821aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
822aab9c406SFlorian Fainelli {
823aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
824aab9c406SFlorian Fainelli 
825aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
826aab9c406SFlorian Fainelli }
827aab9c406SFlorian Fainelli 
828aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
829aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
830aab9c406SFlorian Fainelli {
831aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
832aab9c406SFlorian Fainelli 
833aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
8347905288fSFlorian Fainelli }
8357905288fSFlorian Fainelli 
836a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
837a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
838a2a1a13bSHeiner Kallweit {
839a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
840a2a1a13bSHeiner Kallweit 
841a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
842a2a1a13bSHeiner Kallweit }
843a2a1a13bSHeiner Kallweit 
844a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
845a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
846a2a1a13bSHeiner Kallweit {
847a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
848a2a1a13bSHeiner Kallweit 
849a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
850a2a1a13bSHeiner Kallweit }
851a2a1a13bSHeiner Kallweit 
85204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
85304ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
85404ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
85504ff53f9SFlorian Fainelli {
856d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
85704ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
85804ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
85904ff53f9SFlorian Fainelli 	int err = 0;
86004ff53f9SFlorian Fainelli 
86104ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
86204ff53f9SFlorian Fainelli 	if (!netpoll)
86304ff53f9SFlorian Fainelli 		return -ENOMEM;
86404ff53f9SFlorian Fainelli 
86504ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
86604ff53f9SFlorian Fainelli 	if (err) {
86704ff53f9SFlorian Fainelli 		kfree(netpoll);
86804ff53f9SFlorian Fainelli 		goto out;
86904ff53f9SFlorian Fainelli 	}
87004ff53f9SFlorian Fainelli 
87104ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
87204ff53f9SFlorian Fainelli out:
87304ff53f9SFlorian Fainelli 	return err;
87404ff53f9SFlorian Fainelli }
87504ff53f9SFlorian Fainelli 
87604ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
87704ff53f9SFlorian Fainelli {
87804ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
87904ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
88004ff53f9SFlorian Fainelli 
88104ff53f9SFlorian Fainelli 	if (!netpoll)
88204ff53f9SFlorian Fainelli 		return;
88304ff53f9SFlorian Fainelli 
88404ff53f9SFlorian Fainelli 	p->netpoll = NULL;
88504ff53f9SFlorian Fainelli 
886c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
88704ff53f9SFlorian Fainelli }
88804ff53f9SFlorian Fainelli 
88904ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
89004ff53f9SFlorian Fainelli {
89104ff53f9SFlorian Fainelli }
89204ff53f9SFlorian Fainelli #endif
89304ff53f9SFlorian Fainelli 
89444bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
89544bb765cSFlorian Fainelli 					char *name, size_t len)
89644bb765cSFlorian Fainelli {
897d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
89844bb765cSFlorian Fainelli 
899d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
900d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
901d484210bSJiri Pirko 	 * should be removed with legacy support.
902d484210bSJiri Pirko 	 */
903d484210bSJiri Pirko 	if (dp->ds->devlink)
904d484210bSJiri Pirko 		return -EOPNOTSUPP;
905d484210bSJiri Pirko 
906d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
90744bb765cSFlorian Fainelli 		return -EINVAL;
9083a543ef4SFlorian Fainelli 
9093a543ef4SFlorian Fainelli 	return 0;
9103a543ef4SFlorian Fainelli }
9113a543ef4SFlorian Fainelli 
912f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
9134fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
914f50f2127SFlorian Fainelli {
9154fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
916f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
917f50f2127SFlorian Fainelli 
918f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
919f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
920f50f2127SFlorian Fainelli 			return mall_tc_entry;
921f50f2127SFlorian Fainelli 
922f50f2127SFlorian Fainelli 	return NULL;
923f50f2127SFlorian Fainelli }
924f50f2127SFlorian Fainelli 
925e13c2075SVladimir Oltean static int
926e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
927f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
928f50f2127SFlorian Fainelli 				  bool ingress)
929f50f2127SFlorian Fainelli {
930d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
931f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
932e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
933f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
934d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9359681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
936d945097bSVivien Didelot 	struct dsa_port *to_dp;
937e13c2075SVladimir Oltean 	int err;
938e13c2075SVladimir Oltean 
939f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
94034297176SVladimir Oltean 		return -EOPNOTSUPP;
941f50f2127SFlorian Fainelli 
94253eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
943319a1d19SJiri Pirko 					      cls->common.extack))
94434297176SVladimir Oltean 		return -EOPNOTSUPP;
945319a1d19SJiri Pirko 
9469681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
947f50f2127SFlorian Fainelli 
94865722159SVladimir Oltean 	if (!act->dev)
94965722159SVladimir Oltean 		return -EINVAL;
95065722159SVladimir Oltean 
9519681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
952f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
953f50f2127SFlorian Fainelli 
954f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
955f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
956f50f2127SFlorian Fainelli 		return -ENOMEM;
957f50f2127SFlorian Fainelli 
958f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
959f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
960f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
961f50f2127SFlorian Fainelli 
9629681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
963f50f2127SFlorian Fainelli 
964d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
965f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
966f50f2127SFlorian Fainelli 
967d945097bSVivien Didelot 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
968f50f2127SFlorian Fainelli 	if (err) {
969f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
970f50f2127SFlorian Fainelli 		return err;
971f50f2127SFlorian Fainelli 	}
972f50f2127SFlorian Fainelli 
973f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
974e13c2075SVladimir Oltean 
975e13c2075SVladimir Oltean 	return err;
976f50f2127SFlorian Fainelli }
977f50f2127SFlorian Fainelli 
97834297176SVladimir Oltean static int
97934297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
98034297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
98134297176SVladimir Oltean 				  bool ingress)
98234297176SVladimir Oltean {
98334297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
98434297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
98534297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
98634297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
98734297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
98834297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
98934297176SVladimir Oltean 	struct flow_action_entry *act;
99034297176SVladimir Oltean 	int err;
99134297176SVladimir Oltean 
99234297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
99334297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
994c75a33c8SJacob Keller 				   "Policing offload not implemented");
99534297176SVladimir Oltean 		return -EOPNOTSUPP;
99634297176SVladimir Oltean 	}
99734297176SVladimir Oltean 
99834297176SVladimir Oltean 	if (!ingress) {
99934297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1000c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
100134297176SVladimir Oltean 		return -EOPNOTSUPP;
100234297176SVladimir Oltean 	}
100334297176SVladimir Oltean 
100434297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
100534297176SVladimir Oltean 					      cls->common.extack))
100634297176SVladimir Oltean 		return -EOPNOTSUPP;
100734297176SVladimir Oltean 
100834297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
100934297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
101034297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1011c75a33c8SJacob Keller 					   "Only one port policer allowed");
101234297176SVladimir Oltean 			return -EEXIST;
101334297176SVladimir Oltean 		}
101434297176SVladimir Oltean 	}
101534297176SVladimir Oltean 
101634297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
101734297176SVladimir Oltean 
101834297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
101934297176SVladimir Oltean 	if (!mall_tc_entry)
102034297176SVladimir Oltean 		return -ENOMEM;
102134297176SVladimir Oltean 
102234297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
102334297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
102434297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
102534297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
102634297176SVladimir Oltean 	policer->burst = act->police.burst;
102734297176SVladimir Oltean 
102834297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
102934297176SVladimir Oltean 	if (err) {
103034297176SVladimir Oltean 		kfree(mall_tc_entry);
103134297176SVladimir Oltean 		return err;
103234297176SVladimir Oltean 	}
103334297176SVladimir Oltean 
103434297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
103534297176SVladimir Oltean 
103634297176SVladimir Oltean 	return err;
103734297176SVladimir Oltean }
103834297176SVladimir Oltean 
1039e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1040e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1041e13c2075SVladimir Oltean 				      bool ingress)
1042e13c2075SVladimir Oltean {
1043e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1044e13c2075SVladimir Oltean 
1045e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1046e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1047e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1048e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
104934297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
105034297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
105134297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1052e13c2075SVladimir Oltean 
1053e13c2075SVladimir Oltean 	return err;
1054f50f2127SFlorian Fainelli }
1055f50f2127SFlorian Fainelli 
1056f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1057f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1058f50f2127SFlorian Fainelli {
1059d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1060f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1061d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1062f50f2127SFlorian Fainelli 
10634fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1064f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1065f50f2127SFlorian Fainelli 		return;
1066f50f2127SFlorian Fainelli 
1067f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1068f50f2127SFlorian Fainelli 
1069f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1070f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
107134297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
107234297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
107334297176SVladimir Oltean 						 &mall_tc_entry->mirror);
107434297176SVladimir Oltean 		break;
107534297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
107634297176SVladimir Oltean 		if (ds->ops->port_policer_del)
107734297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1078f50f2127SFlorian Fainelli 		break;
1079f50f2127SFlorian Fainelli 	default:
1080f50f2127SFlorian Fainelli 		WARN_ON(1);
1081f50f2127SFlorian Fainelli 	}
1082f50f2127SFlorian Fainelli 
1083f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1084f50f2127SFlorian Fainelli }
1085f50f2127SFlorian Fainelli 
10863fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
10876b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
10886b3eb752SJiri Pirko 					   bool ingress)
1089f50f2127SFlorian Fainelli {
10905fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1091a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1092f50f2127SFlorian Fainelli 
10933fbae382SJiri Pirko 	switch (cls->command) {
10943fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
10955fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
10963fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
10973fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
10983fbae382SJiri Pirko 		return 0;
10993fbae382SJiri Pirko 	default:
11003fbae382SJiri Pirko 		return -EOPNOTSUPP;
11013fbae382SJiri Pirko 	}
11023fbae382SJiri Pirko }
11033fbae382SJiri Pirko 
1104ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1105ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1106ed11bb1fSVladimir Oltean 				    bool ingress)
1107ed11bb1fSVladimir Oltean {
1108ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1109ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1110ed11bb1fSVladimir Oltean 	int port = dp->index;
1111ed11bb1fSVladimir Oltean 
1112ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1113ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1114ed11bb1fSVladimir Oltean 
1115ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1116ed11bb1fSVladimir Oltean }
1117ed11bb1fSVladimir Oltean 
1118ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1119ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1120ed11bb1fSVladimir Oltean 				    bool ingress)
1121ed11bb1fSVladimir Oltean {
1122ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1123ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1124ed11bb1fSVladimir Oltean 	int port = dp->index;
1125ed11bb1fSVladimir Oltean 
1126ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1127ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1128ed11bb1fSVladimir Oltean 
1129ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1130ed11bb1fSVladimir Oltean }
1131ed11bb1fSVladimir Oltean 
1132ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1133ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1134ed11bb1fSVladimir Oltean 				      bool ingress)
1135ed11bb1fSVladimir Oltean {
1136ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1137ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1138ed11bb1fSVladimir Oltean 	int port = dp->index;
1139ed11bb1fSVladimir Oltean 
1140ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1141ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1142ed11bb1fSVladimir Oltean 
1143ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1144ed11bb1fSVladimir Oltean }
1145ed11bb1fSVladimir Oltean 
1146ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1147ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1148ed11bb1fSVladimir Oltean 					 bool ingress)
1149ed11bb1fSVladimir Oltean {
1150ed11bb1fSVladimir Oltean 	switch (cls->command) {
1151ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1152ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1153ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1154ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1155ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1156ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1157ed11bb1fSVladimir Oltean 	default:
1158ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1159ed11bb1fSVladimir Oltean 	}
1160ed11bb1fSVladimir Oltean }
1161ed11bb1fSVladimir Oltean 
11626b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
11636b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
11646b3eb752SJiri Pirko {
11656b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
11666b3eb752SJiri Pirko 
116744ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
116844ae12a7SJiri Pirko 		return -EOPNOTSUPP;
116944ae12a7SJiri Pirko 
11706b3eb752SJiri Pirko 	switch (type) {
11716b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
11726b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1173ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1174ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
11756b3eb752SJiri Pirko 	default:
11766b3eb752SJiri Pirko 		return -EOPNOTSUPP;
11776b3eb752SJiri Pirko 	}
11786b3eb752SJiri Pirko }
11796b3eb752SJiri Pirko 
11806b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
11816b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
11826b3eb752SJiri Pirko {
11836b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
11846b3eb752SJiri Pirko }
11856b3eb752SJiri Pirko 
11866b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
11876b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
11886b3eb752SJiri Pirko {
11896b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
11906b3eb752SJiri Pirko }
11916b3eb752SJiri Pirko 
1192955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1193955bcb6eSPablo Neira Ayuso 
11946b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1195955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
11966b3eb752SJiri Pirko {
1197955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1198a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
11996b3eb752SJiri Pirko 
120032f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
12016b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
120232f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
12036b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
12046b3eb752SJiri Pirko 	else
12056b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12066b3eb752SJiri Pirko 
1207955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1208955bcb6eSPablo Neira Ayuso 
12096b3eb752SJiri Pirko 	switch (f->command) {
12109c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
12110d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
12120d4fd02eSPablo Neira Ayuso 			return -EBUSY;
12130d4fd02eSPablo Neira Ayuso 
12140c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1215955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1216955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1217955bcb6eSPablo Neira Ayuso 
1218955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1219955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1220955bcb6eSPablo Neira Ayuso 		return 0;
12219c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
122214bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1223955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1224955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1225955bcb6eSPablo Neira Ayuso 
1226955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1227955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
12286b3eb752SJiri Pirko 		return 0;
12296b3eb752SJiri Pirko 	default:
12306b3eb752SJiri Pirko 		return -EOPNOTSUPP;
12316b3eb752SJiri Pirko 	}
12326b3eb752SJiri Pirko }
12336b3eb752SJiri Pirko 
12343fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1235de4784caSJiri Pirko 			      void *type_data)
12363fbae382SJiri Pirko {
123747d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
123847d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
123947d23af2SVladimir Oltean 
124047d23af2SVladimir Oltean 	if (type == TC_SETUP_BLOCK)
12416b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
124247d23af2SVladimir Oltean 
124347d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1244a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
124547d23af2SVladimir Oltean 
124647d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1247f50f2127SFlorian Fainelli }
1248f50f2127SFlorian Fainelli 
1249bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1250bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1251bf9f2648SFlorian Fainelli {
1252d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1253d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1254bf9f2648SFlorian Fainelli 
1255bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1256bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1257bf9f2648SFlorian Fainelli 
1258d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1259bf9f2648SFlorian Fainelli }
1260bf9f2648SFlorian Fainelli 
1261bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1262bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1263bf9f2648SFlorian Fainelli {
1264d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1265d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1266bf9f2648SFlorian Fainelli 
1267bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1268bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1269bf9f2648SFlorian Fainelli 
1270d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1271bf9f2648SFlorian Fainelli }
1272bf9f2648SFlorian Fainelli 
12730336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
12740336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
12750336369dSBrandon Streiff {
12760336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
12770336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
12780336369dSBrandon Streiff 
12790336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
12800336369dSBrandon Streiff 		return -EOPNOTSUPP;
12810336369dSBrandon Streiff 
12820336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
12830336369dSBrandon Streiff }
12840336369dSBrandon Streiff 
1285061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1286061f6a50SFlorian Fainelli 				     u16 vid)
1287061f6a50SFlorian Fainelli {
12882209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1289061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
129088236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
129188236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
129288236591SVladimir Oltean 		.vid_begin = vid,
129388236591SVladimir Oltean 		.vid_end = vid,
129488236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
129588236591SVladimir Oltean 		.flags = 0,
129688236591SVladimir Oltean 	};
129788236591SVladimir Oltean 	struct switchdev_trans trans;
1298061f6a50SFlorian Fainelli 	int ret;
1299061f6a50SFlorian Fainelli 
130088236591SVladimir Oltean 	/* User port... */
130188236591SVladimir Oltean 	trans.ph_prepare = true;
130288236591SVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &trans);
13039b236d2aSVladimir Oltean 	if (ret)
1304cf360866SVivien Didelot 		return ret;
1305cf360866SVivien Didelot 
130688236591SVladimir Oltean 	trans.ph_prepare = false;
130788236591SVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &trans);
130888236591SVladimir Oltean 	if (ret)
130988236591SVladimir Oltean 		return ret;
131088236591SVladimir Oltean 
131188236591SVladimir Oltean 	/* And CPU port... */
131288236591SVladimir Oltean 	trans.ph_prepare = true;
131388236591SVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &trans);
131488236591SVladimir Oltean 	if (ret)
131588236591SVladimir Oltean 		return ret;
131688236591SVladimir Oltean 
131788236591SVladimir Oltean 	trans.ph_prepare = false;
131888236591SVladimir Oltean 	ret = dsa_port_vlan_add(dp->cpu_dp, &vlan, &trans);
13199b236d2aSVladimir Oltean 	if (ret)
13207e1741b4SVivien Didelot 		return ret;
13217e1741b4SVivien Didelot 
13222209158cSVladimir Oltean 	return vlan_vid_add(master, proto, vid);
1323061f6a50SFlorian Fainelli }
1324061f6a50SFlorian Fainelli 
1325061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1326061f6a50SFlorian Fainelli 				      u16 vid)
1327061f6a50SFlorian Fainelli {
13282209158cSVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1329061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
133088236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
133188236591SVladimir Oltean 		.vid_begin = vid,
133288236591SVladimir Oltean 		.vid_end = vid,
133388236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
133488236591SVladimir Oltean 		.flags = 0,
133588236591SVladimir Oltean 	};
13362209158cSVladimir Oltean 	int err;
1337061f6a50SFlorian Fainelli 
13387e1741b4SVivien Didelot 	/* Do not deprogram the CPU port as it may be shared with other user
13397e1741b4SVivien Didelot 	 * ports which can be members of this VLAN as well.
13407e1741b4SVivien Didelot 	 */
13412209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
13422209158cSVladimir Oltean 	if (err)
13432209158cSVladimir Oltean 		return err;
13442209158cSVladimir Oltean 
13452209158cSVladimir Oltean 	vlan_vid_del(master, proto, vid);
13462209158cSVladimir Oltean 
13472209158cSVladimir Oltean 	return 0;
1348061f6a50SFlorian Fainelli }
1349061f6a50SFlorian Fainelli 
1350bff33f7eSVladimir Oltean struct dsa_hw_port {
1351bff33f7eSVladimir Oltean 	struct list_head list;
1352bff33f7eSVladimir Oltean 	struct net_device *dev;
1353bff33f7eSVladimir Oltean 	int old_mtu;
1354bff33f7eSVladimir Oltean };
1355bff33f7eSVladimir Oltean 
1356bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1357bff33f7eSVladimir Oltean {
1358bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1359bff33f7eSVladimir Oltean 	int err;
1360bff33f7eSVladimir Oltean 
1361bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1362bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1363bff33f7eSVladimir Oltean 			continue;
1364bff33f7eSVladimir Oltean 
1365bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1366bff33f7eSVladimir Oltean 		if (err)
1367bff33f7eSVladimir Oltean 			goto rollback;
1368bff33f7eSVladimir Oltean 	}
1369bff33f7eSVladimir Oltean 
1370bff33f7eSVladimir Oltean 	return 0;
1371bff33f7eSVladimir Oltean 
1372bff33f7eSVladimir Oltean rollback:
1373bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1374bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1375bff33f7eSVladimir Oltean 			continue;
1376bff33f7eSVladimir Oltean 
1377bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1378bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1379bff33f7eSVladimir Oltean 	}
1380bff33f7eSVladimir Oltean 
1381bff33f7eSVladimir Oltean 	return err;
1382bff33f7eSVladimir Oltean }
1383bff33f7eSVladimir Oltean 
1384bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1385bff33f7eSVladimir Oltean {
1386bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1387bff33f7eSVladimir Oltean 
1388bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1389bff33f7eSVladimir Oltean 		kfree(p);
1390bff33f7eSVladimir Oltean }
1391bff33f7eSVladimir Oltean 
1392bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1393bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1394bff33f7eSVladimir Oltean {
1395bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1396bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1397bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1398bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1399bff33f7eSVladimir Oltean 	int err;
1400bff33f7eSVladimir Oltean 
1401bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1402bff33f7eSVladimir Oltean 		return;
1403bff33f7eSVladimir Oltean 
1404bff33f7eSVladimir Oltean 	if (!dp->bridge_dev)
1405bff33f7eSVladimir Oltean 		return;
1406bff33f7eSVladimir Oltean 
1407bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1408bff33f7eSVladimir Oltean 
1409bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1410bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1411bff33f7eSVladimir Oltean 	 */
1412bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1413bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1414bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1415bff33f7eSVladimir Oltean 			struct net_device *slave;
1416bff33f7eSVladimir Oltean 
1417bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1418bff33f7eSVladimir Oltean 				continue;
1419bff33f7eSVladimir Oltean 
1420bff33f7eSVladimir Oltean 			if (other_dp->bridge_dev != dp->bridge_dev)
1421bff33f7eSVladimir Oltean 				continue;
1422bff33f7eSVladimir Oltean 
1423bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1424bff33f7eSVladimir Oltean 				continue;
1425bff33f7eSVladimir Oltean 
1426bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1427bff33f7eSVladimir Oltean 
1428bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1429bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1430bff33f7eSVladimir Oltean 
1431bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1432bff33f7eSVladimir Oltean 			if (!hw_port)
1433bff33f7eSVladimir Oltean 				goto out;
1434bff33f7eSVladimir Oltean 
1435bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1436bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1437bff33f7eSVladimir Oltean 
1438bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1439bff33f7eSVladimir Oltean 		}
1440bff33f7eSVladimir Oltean 	}
1441bff33f7eSVladimir Oltean 
1442bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1443bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1444bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1445bff33f7eSVladimir Oltean 	 */
1446bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1447bff33f7eSVladimir Oltean 	if (!err)
1448bff33f7eSVladimir Oltean 		goto out;
1449bff33f7eSVladimir Oltean 
1450bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1451bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1452bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1453bff33f7eSVladimir Oltean 	 */
1454bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1455bff33f7eSVladimir Oltean 
1456bff33f7eSVladimir Oltean out:
1457bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1458bff33f7eSVladimir Oltean }
1459bff33f7eSVladimir Oltean 
1460bfcb8132SVladimir Oltean static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1461bfcb8132SVladimir Oltean {
1462bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1463bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1464bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1465bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
1466bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1467bfcb8132SVladimir Oltean 	int port = p->dp->index;
1468bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1469bfcb8132SVladimir Oltean 	int new_master_mtu;
1470bfcb8132SVladimir Oltean 	int old_master_mtu;
1471bfcb8132SVladimir Oltean 	int mtu_limit;
1472bfcb8132SVladimir Oltean 	int cpu_mtu;
1473bfcb8132SVladimir Oltean 	int err, i;
1474bfcb8132SVladimir Oltean 
1475bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1476bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1477bfcb8132SVladimir Oltean 
1478bfcb8132SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1479bfcb8132SVladimir Oltean 		int slave_mtu;
1480bfcb8132SVladimir Oltean 
1481bfcb8132SVladimir Oltean 		if (!dsa_is_user_port(ds, i))
1482bfcb8132SVladimir Oltean 			continue;
1483bfcb8132SVladimir Oltean 
1484bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1485bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1486bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1487bfcb8132SVladimir Oltean 		 */
1488bfcb8132SVladimir Oltean 		if (!dsa_to_port(ds, i)->slave)
1489bfcb8132SVladimir Oltean 			continue;
1490bfcb8132SVladimir Oltean 
1491bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1492bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1493bfcb8132SVladimir Oltean 		 */
1494bfcb8132SVladimir Oltean 		if (i == port)
1495bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1496bfcb8132SVladimir Oltean 		else
1497bfcb8132SVladimir Oltean 			slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1498bfcb8132SVladimir Oltean 
1499bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1500bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1501bfcb8132SVladimir Oltean 	}
1502bfcb8132SVladimir Oltean 
1503bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1504bfcb8132SVladimir Oltean 
1505bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1506bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
1507bfcb8132SVladimir Oltean 	new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1508bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1509bfcb8132SVladimir Oltean 		return -ERANGE;
1510bfcb8132SVladimir Oltean 
1511bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1512bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1513bfcb8132SVladimir Oltean 	 */
1514bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1515bfcb8132SVladimir Oltean 
1516bfcb8132SVladimir Oltean 	/* Start applying stuff */
1517bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1518bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1519bfcb8132SVladimir Oltean 		if (err < 0)
1520bfcb8132SVladimir Oltean 			goto out_master_failed;
1521bfcb8132SVladimir Oltean 
1522bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
1523bfcb8132SVladimir Oltean 		 * upstream switches.
1524bfcb8132SVladimir Oltean 		 */
1525bfcb8132SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1526bfcb8132SVladimir Oltean 		if (err)
1527bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1528bfcb8132SVladimir Oltean 	}
1529bfcb8132SVladimir Oltean 
1530bfcb8132SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, false);
1531bfcb8132SVladimir Oltean 	if (err)
1532bfcb8132SVladimir Oltean 		goto out_port_failed;
1533bfcb8132SVladimir Oltean 
1534bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1535bfcb8132SVladimir Oltean 
1536bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1537bff33f7eSVladimir Oltean 
1538bfcb8132SVladimir Oltean 	return 0;
1539bfcb8132SVladimir Oltean 
1540bfcb8132SVladimir Oltean out_port_failed:
1541bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1542bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1543bfcb8132SVladimir Oltean 				    cpu_dp->tag_ops->overhead,
1544bfcb8132SVladimir Oltean 				    true);
1545bfcb8132SVladimir Oltean out_cpu_failed:
1546bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1547bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1548bfcb8132SVladimir Oltean out_master_failed:
1549bfcb8132SVladimir Oltean 	return err;
1550bfcb8132SVladimir Oltean }
1551bfcb8132SVladimir Oltean 
155291da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
155391da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
15543d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
15553d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1556aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1557c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
15586793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
15596793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
15606793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
156191da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
156291da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
156391da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
156419e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
156519e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
15667905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
15677905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1568aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1569aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1570a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
1571a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
1572bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1573bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
15740336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
157591da11f8SLennert Buytenhek };
157691da11f8SLennert Buytenhek 
15772a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
15782a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
15792a93c1a3SFlorian Fainelli 		       struct net_device *dev,
15802a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid,
158187b0984eSPetr Machata 		       u16 flags,
158287b0984eSPetr Machata 		       struct netlink_ext_ack *extack)
15832a93c1a3SFlorian Fainelli {
15842a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
15852a93c1a3SFlorian Fainelli 
15862a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
15872a93c1a3SFlorian Fainelli }
15882a93c1a3SFlorian Fainelli 
15892a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
15902a93c1a3SFlorian Fainelli 		       struct net_device *dev,
15912a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid)
15922a93c1a3SFlorian Fainelli {
15932a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
15942a93c1a3SFlorian Fainelli 
15952a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
15962a93c1a3SFlorian Fainelli }
15972a93c1a3SFlorian Fainelli 
1598716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1599716efee2SJiri Pirko {
1600716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1601716efee2SJiri Pirko 
1602716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1603716efee2SJiri Pirko }
1604716efee2SJiri Pirko 
16053e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1606d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1607d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
16083e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1609d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1610d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1611d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
161237b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
161337b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
16142bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1615d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1616abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
161704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
161804ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
161904ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
162004ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
162104ff53f9SFlorian Fainelli #endif
162244bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1623f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
16246a900628SHeiner Kallweit 	.ndo_get_stats64	= dev_get_tstats64,
1625929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1626061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1627061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1628716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
1629bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
163098237d43SScott Feldman };
163198237d43SScott Feldman 
1632f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1633f37db85dSFlorian Fainelli 	.name	= "dsa",
1634f37db85dSFlorian Fainelli };
1635f37db85dSFlorian Fainelli 
1636aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1637aab9c406SFlorian Fainelli {
1638aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1639aab9c406SFlorian Fainelli 
1640765bda93SRussell King 	if (dp->pl)
1641aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
1642aab9c406SFlorian Fainelli }
1643aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1644aab9c406SFlorian Fainelli 
16455c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
1646aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1647aab9c406SFlorian Fainelli {
16485c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1649aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1650aab9c406SFlorian Fainelli 
1651aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1652aab9c406SFlorian Fainelli 	 * not be called if it was not.
1653aab9c406SFlorian Fainelli 	 */
1654aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1655ce31b31cSFlorian Fainelli }
1656ce31b31cSFlorian Fainelli 
165791da11f8SLennert Buytenhek /* slave device setup *******************************************************/
16584fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1659c305c165SFlorian Fainelli {
1660d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1661d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1662c305c165SFlorian Fainelli 
16630115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
16640115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1665d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1666c305c165SFlorian Fainelli 		return -ENODEV;
1667d25b8e74SRussell King 	}
1668c305c165SFlorian Fainelli 
1669aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1670c305c165SFlorian Fainelli }
1671c305c165SFlorian Fainelli 
16724fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
16730d8bcdd3SFlorian Fainelli {
1674d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1675d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1676d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
16770c65b2b9SAndrew Lunn 	phy_interface_t mode;
16786819563eSFlorian Fainelli 	u32 phy_flags = 0;
16790c65b2b9SAndrew Lunn 	int ret;
16800d8bcdd3SFlorian Fainelli 
16810c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(port_dn, &mode);
16820c65b2b9SAndrew Lunn 	if (ret)
168319334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
16840d8bcdd3SFlorian Fainelli 
168544cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
168644cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
168744cc27e4SIoana Ciornei 
16885c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
16895c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
16905c05c1dbSRussell King 	 * this if the switch provides such a callback.
16915c05c1dbSRussell King 	 */
16925c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
16935c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
16945c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
16955c05c1dbSRussell King 	}
16965c05c1dbSRussell King 
169744cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
169877373d49SIoana Ciornei 				&dsa_port_phylink_mac_ops);
1699aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1700aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1701aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1702aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1703aab9c406SFlorian Fainelli 	}
1704aab9c406SFlorian Fainelli 
17059d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1706d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
17076819563eSFlorian Fainelli 
1708aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
17096146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
17106146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
17116146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
17120d8bcdd3SFlorian Fainelli 		 */
1713d945097bSVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1714d25b8e74SRussell King 		if (ret) {
1715aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1716aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1717d945097bSVivien Didelot 				   dp->index, ret);
1718aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1719c305c165SFlorian Fainelli 			return ret;
1720d25b8e74SRussell King 		}
17210d8bcdd3SFlorian Fainelli 	}
17229697f1cdSFlorian Fainelli 
17236146dd45SVladimir Oltean 	return ret;
1724b31f65fbSAndrew Lunn }
17250d8bcdd3SFlorian Fainelli 
17261a33e10eSCong Wang static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
17271a33e10eSCong Wang static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
17281a33e10eSCong Wang 					    struct netdev_queue *txq,
17291a33e10eSCong Wang 					    void *_unused)
17301a33e10eSCong Wang {
17311a33e10eSCong Wang 	lockdep_set_class(&txq->_xmit_lock,
17321a33e10eSCong Wang 			  &dsa_slave_netdev_xmit_lock_key);
17331a33e10eSCong Wang }
17341a33e10eSCong Wang 
173524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
173624462549SFlorian Fainelli {
1737aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
173824462549SFlorian Fainelli 
1739a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1740a94c689eSFlorian Fainelli 		return 0;
1741a94c689eSFlorian Fainelli 
1742f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1743f154be24SFlorian Fainelli 
1744aab9c406SFlorian Fainelli 	rtnl_lock();
1745aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1746aab9c406SFlorian Fainelli 	rtnl_unlock();
174724462549SFlorian Fainelli 
174824462549SFlorian Fainelli 	return 0;
174924462549SFlorian Fainelli }
175024462549SFlorian Fainelli 
175124462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
175224462549SFlorian Fainelli {
1753aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1754aab9c406SFlorian Fainelli 
1755a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1756a94c689eSFlorian Fainelli 		return 0;
1757a94c689eSFlorian Fainelli 
175824462549SFlorian Fainelli 	netif_device_attach(slave_dev);
175924462549SFlorian Fainelli 
1760aab9c406SFlorian Fainelli 	rtnl_lock();
1761aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1762aab9c406SFlorian Fainelli 	rtnl_unlock();
176324462549SFlorian Fainelli 
176424462549SFlorian Fainelli 	return 0;
176524462549SFlorian Fainelli }
176624462549SFlorian Fainelli 
17676158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val)
17686158eaa7SVivien Didelot {
1769d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
1770d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
17716158eaa7SVivien Didelot 	struct dsa_notifier_register_info rinfo = {
17726158eaa7SVivien Didelot 		.switch_number = dp->ds->index,
17736158eaa7SVivien Didelot 		.port_number = dp->index,
17746158eaa7SVivien Didelot 		.master = master,
17756158eaa7SVivien Didelot 		.info.dev = dev,
17766158eaa7SVivien Didelot 	};
17776158eaa7SVivien Didelot 
17786158eaa7SVivien Didelot 	call_dsa_notifiers(val, dev, &rinfo.info);
17796158eaa7SVivien Didelot }
17806158eaa7SVivien Didelot 
1781951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
178291da11f8SLennert Buytenhek {
178324a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1784f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
17854cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1786951259aaSVivien Didelot 	const char *name = port->name;
178791da11f8SLennert Buytenhek 	struct net_device *slave_dev;
178891da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
178991da11f8SLennert Buytenhek 	int ret;
179091da11f8SLennert Buytenhek 
179155199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
179255199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
179355199df6SFlorian Fainelli 
179455199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
179555199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
179655199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
179791da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1798d87d6f44SGuenter Roeck 		return -ENOMEM;
179991da11f8SLennert Buytenhek 
18009b236d2aSVladimir Oltean 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
18019b236d2aSVladimir Oltean 	if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
18029b236d2aSVladimir Oltean 		slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1803f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
18042b86cb82SVladimir Oltean 	slave_dev->features |= NETIF_F_LLTX;
18057ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
18064974f9b7SPetr Štetiar 	if (!IS_ERR_OR_NULL(port->mac))
1807a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1808a2c7023fSXiaofei Shen 	else
18092fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
18100a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
18113e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
1812bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
1813bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
1814a3b0b647SVladimir Oltean 	if (cpu_dp->tag_ops->tail_tag)
1815a3b0b647SVladimir Oltean 		slave_dev->needed_tailroom = cpu_dp->tag_ops->overhead;
1816a3b0b647SVladimir Oltean 	else
1817a3b0b647SVladimir Oltean 		slave_dev->needed_headroom = cpu_dp->tag_ops->overhead;
1818a3b0b647SVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
1819a3b0b647SVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
1820a3b0b647SVladimir Oltean 	 * The 8021q driver also does this.
1821a3b0b647SVladimir Oltean 	 */
1822a3b0b647SVladimir Oltean 	slave_dev->needed_headroom += master->needed_headroom;
1823a3b0b647SVladimir Oltean 	slave_dev->needed_tailroom += master->needed_tailroom;
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);
184615240248SVivien Didelot 	p->xmit = cpu_dp->tag_ops->xmit;
1847f8b8b1cdSVivien Didelot 	port->slave = 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 
18666158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
186760724d4bSFlorian Fainelli 
18682f1e8ea7SVladimir Oltean 	rtnl_lock();
18692f1e8ea7SVladimir Oltean 
18702f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
1871e804441cSFlorian Fainelli 	if (ret) {
1872e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1873e804441cSFlorian Fainelli 			   ret, slave_dev->name);
18742f1e8ea7SVladimir Oltean 		rtnl_unlock();
1875e804441cSFlorian Fainelli 		goto out_phy;
18760071f56eSAndrew Lunn 	}
18770071f56eSAndrew Lunn 
18782f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
18792f1e8ea7SVladimir Oltean 
18802f1e8ea7SVladimir Oltean 	rtnl_unlock();
18812f1e8ea7SVladimir Oltean 
18822f1e8ea7SVladimir Oltean 	if (ret)
18832f1e8ea7SVladimir Oltean 		goto out_unregister;
18842f1e8ea7SVladimir Oltean 
1885d87d6f44SGuenter Roeck 	return 0;
1886e804441cSFlorian Fainelli 
18872f1e8ea7SVladimir Oltean out_unregister:
18882f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
1889e804441cSFlorian Fainelli out_phy:
1890aab9c406SFlorian Fainelli 	rtnl_lock();
1891aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1892aab9c406SFlorian Fainelli 	rtnl_unlock();
1893aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1894e131a563SAlexander Lobakin out_gcells:
1895e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
1896e804441cSFlorian Fainelli out_free:
18976a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
1898e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1899f8b8b1cdSVivien Didelot 	port->slave = NULL;
1900e804441cSFlorian Fainelli 	return ret;
190191da11f8SLennert Buytenhek }
1902b73adef6SFlorian Fainelli 
1903cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1904cda5c15bSNeil Armstrong {
19052f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
1906d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1907cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1908cda5c15bSNeil Armstrong 
1909cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1910aab9c406SFlorian Fainelli 	rtnl_lock();
19112f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
19122f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
1913aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
1914aab9c406SFlorian Fainelli 	rtnl_unlock();
1915881eadabSJohan Hovold 
19166158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1917aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
1918e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
19196a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
1920cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1921cda5c15bSNeil Armstrong }
1922cda5c15bSNeil Armstrong 
19234d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
1924b73adef6SFlorian Fainelli {
1925b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1926b73adef6SFlorian Fainelli }
1927*a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
1928b73adef6SFlorian Fainelli 
19298e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
19308e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1931b73adef6SFlorian Fainelli {
1932d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
19338e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1934b73adef6SFlorian Fainelli 
19358e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
19368e92ab3aSVivien Didelot 		if (info->linking) {
193717d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
1938bff33f7eSVladimir Oltean 			if (!err)
1939bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
19408e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
19418e92ab3aSVivien Didelot 		} else {
194217d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
19438e92ab3aSVivien Didelot 			err = NOTIFY_OK;
19448e92ab3aSVivien Didelot 		}
19456debb68aSVivien Didelot 	}
1946b73adef6SFlorian Fainelli 
19478e92ab3aSVivien Didelot 	return err;
1948b73adef6SFlorian Fainelli }
1949b73adef6SFlorian Fainelli 
1950eb46e8daSVladimir Oltean static int
1951eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
1952eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
1953cc1d5bdaSFlorian Fainelli {
1954cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
1955cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
1956cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
1957cc1d5bdaSFlorian Fainelli 
1958cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
1959cc1d5bdaSFlorian Fainelli 
1960cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
1961cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1962cc1d5bdaSFlorian Fainelli 
1963cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
1964cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
1965cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1966cc1d5bdaSFlorian Fainelli 
1967cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
1968cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
1969cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1970cc1d5bdaSFlorian Fainelli 
1971cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
1972cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
1973cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
1974cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
1975cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
1976cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
1977cc1d5bdaSFlorian Fainelli 	}
1978cc1d5bdaSFlorian Fainelli 
1979cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
1980cc1d5bdaSFlorian Fainelli }
1981cc1d5bdaSFlorian Fainelli 
19822b138406SVladimir Oltean static int
19832b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
19842b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
19852b138406SVladimir Oltean {
19862b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
19872b138406SVladimir Oltean 	struct net_device *br = dp->bridge_dev;
19882b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
19892b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
19902b138406SVladimir Oltean 	int err = NOTIFY_DONE;
19912b138406SVladimir Oltean 	u16 vid;
19922b138406SVladimir Oltean 
1993adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
19942b138406SVladimir Oltean 		return NOTIFY_DONE;
19952b138406SVladimir Oltean 
19962b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
19972b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
19982b138406SVladimir Oltean 
19992b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
20002b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
20012b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
20022b138406SVladimir Oltean 	 */
20032b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
20042b138406SVladimir Oltean 	if (err == 0) {
20052b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
20062b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
20072b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
20082b138406SVladimir Oltean 	}
20092b138406SVladimir Oltean 
20102b138406SVladimir Oltean 	return NOTIFY_DONE;
20112b138406SVladimir Oltean }
20122b138406SVladimir Oltean 
201388e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
2014b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
2015b73adef6SFlorian Fainelli {
20166debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2017b73adef6SFlorian Fainelli 
201883501299SVladimir Oltean 	switch (event) {
20192b138406SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
20202b138406SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
2021e358bef7SVladimir Oltean 		struct dsa_switch *ds;
2022e358bef7SVladimir Oltean 		struct dsa_port *dp;
2023e358bef7SVladimir Oltean 		int err;
20242b138406SVladimir Oltean 
202553bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
2026eb46e8daSVladimir Oltean 			return dsa_prevent_bridging_8021q_upper(dev, ptr);
20272b138406SVladimir Oltean 
2028e358bef7SVladimir Oltean 		dp = dsa_slave_to_port(dev);
2029e358bef7SVladimir Oltean 		ds = dp->ds;
2030e358bef7SVladimir Oltean 
2031e358bef7SVladimir Oltean 		if (ds->ops->port_prechangeupper) {
2032e358bef7SVladimir Oltean 			err = ds->ops->port_prechangeupper(ds, dp->index, info);
2033e358bef7SVladimir Oltean 			if (err)
2034e358bef7SVladimir Oltean 				return notifier_from_errno(err);
2035e358bef7SVladimir Oltean 		}
2036e358bef7SVladimir Oltean 
20372b138406SVladimir Oltean 		if (is_vlan_dev(info->upper_dev))
20382b138406SVladimir Oltean 			return dsa_slave_check_8021q_upper(dev, ptr);
203983501299SVladimir Oltean 		break;
20402b138406SVladimir Oltean 	}
204183501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
204283501299SVladimir Oltean 		if (!dsa_slave_dev_check(dev))
204383501299SVladimir Oltean 			return NOTIFY_DONE;
20448e92ab3aSVivien Didelot 
20458e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
2046cc1d5bdaSFlorian Fainelli 	}
2047b73adef6SFlorian Fainelli 
2048b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2049b73adef6SFlorian Fainelli }
205088e4f0caSVivien Didelot 
2051c4bb76a9SVladimir Oltean static void
2052c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2053c4bb76a9SVladimir Oltean {
2054c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2055c4bb76a9SVladimir Oltean 	struct switchdev_notifier_fdb_info info;
2056c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2057c4bb76a9SVladimir Oltean 
2058c4bb76a9SVladimir Oltean 	if (!dsa_is_user_port(ds, switchdev_work->port))
2059c4bb76a9SVladimir Oltean 		return;
2060c4bb76a9SVladimir Oltean 
2061c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2062c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2063c4bb76a9SVladimir Oltean 	info.offloaded = true;
2064c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2065c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2066c4bb76a9SVladimir Oltean 				 dp->slave, &info.info, NULL);
2067c4bb76a9SVladimir Oltean }
2068c9eb3e0fSArkadi Sharshevsky 
2069c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2070c9eb3e0fSArkadi Sharshevsky {
2071c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2072c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
2073c4bb76a9SVladimir Oltean 	struct dsa_switch *ds = switchdev_work->ds;
2074c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2075c9eb3e0fSArkadi Sharshevsky 	int err;
2076c9eb3e0fSArkadi Sharshevsky 
2077c4bb76a9SVladimir Oltean 	dp = dsa_to_port(ds, switchdev_work->port);
2078c4bb76a9SVladimir Oltean 
2079c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
2080c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2081c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2082c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_add(dp, switchdev_work->addr,
2083c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2084c9eb3e0fSArkadi Sharshevsky 		if (err) {
2085c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2086c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
2087c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2088c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2089c9eb3e0fSArkadi Sharshevsky 			break;
2090c9eb3e0fSArkadi Sharshevsky 		}
2091c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2092c9eb3e0fSArkadi Sharshevsky 		break;
2093c9eb3e0fSArkadi Sharshevsky 
2094c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2095c4bb76a9SVladimir Oltean 		err = dsa_port_fdb_del(dp, switchdev_work->addr,
2096c4bb76a9SVladimir Oltean 				       switchdev_work->vid);
2097c9eb3e0fSArkadi Sharshevsky 		if (err) {
2098c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2099c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
2100c4bb76a9SVladimir Oltean 				dp->index, switchdev_work->addr,
2101c4bb76a9SVladimir Oltean 				switchdev_work->vid, err);
2102c9eb3e0fSArkadi Sharshevsky 		}
21032fd18650SVladimir Oltean 
2104c9eb3e0fSArkadi Sharshevsky 		break;
2105c9eb3e0fSArkadi Sharshevsky 	}
2106c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
2107c9eb3e0fSArkadi Sharshevsky 
2108c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2109c4bb76a9SVladimir Oltean 	if (dsa_is_user_port(ds, dp->index))
2110c4bb76a9SVladimir Oltean 		dev_put(dp->slave);
2111c9eb3e0fSArkadi Sharshevsky }
2112c9eb3e0fSArkadi Sharshevsky 
2113d5f19486SVladimir Oltean static int dsa_lower_dev_walk(struct net_device *lower_dev,
2114d5f19486SVladimir Oltean 			      struct netdev_nested_priv *priv)
2115d5f19486SVladimir Oltean {
2116d5f19486SVladimir Oltean 	if (dsa_slave_dev_check(lower_dev)) {
2117d5f19486SVladimir Oltean 		priv->data = (void *)netdev_priv(lower_dev);
2118d5f19486SVladimir Oltean 		return 1;
2119d5f19486SVladimir Oltean 	}
2120d5f19486SVladimir Oltean 
2121d5f19486SVladimir Oltean 	return 0;
2122d5f19486SVladimir Oltean }
2123d5f19486SVladimir Oltean 
2124d5f19486SVladimir Oltean static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev)
2125d5f19486SVladimir Oltean {
2126d5f19486SVladimir Oltean 	struct netdev_nested_priv priv = {
2127d5f19486SVladimir Oltean 		.data = NULL,
2128d5f19486SVladimir Oltean 	};
2129d5f19486SVladimir Oltean 
2130d5f19486SVladimir Oltean 	netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv);
2131d5f19486SVladimir Oltean 
2132d5f19486SVladimir Oltean 	return (struct dsa_slave_priv *)priv.data;
2133d5f19486SVladimir Oltean }
2134d5f19486SVladimir Oltean 
2135c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2136c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2137c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2138c9eb3e0fSArkadi Sharshevsky {
2139c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2140c4bb76a9SVladimir Oltean 	const struct switchdev_notifier_fdb_info *fdb_info;
2141c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
2142c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
214379b139f4SVivien Didelot 	int err;
214479b139f4SVivien Didelot 
2145447d290aSVladimir Oltean 	switch (event) {
2146447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
214779b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
214879b139f4SVivien Didelot 						     dsa_slave_dev_check,
214979b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
215079b139f4SVivien Didelot 		return notifier_from_errno(err);
2151447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2152447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2153d5f19486SVladimir Oltean 		fdb_info = ptr;
2154d5f19486SVladimir Oltean 
2155d5f19486SVladimir Oltean 		if (dsa_slave_dev_check(dev)) {
2156d5f19486SVladimir Oltean 			if (!fdb_info->added_by_user)
2157d5f19486SVladimir Oltean 				return NOTIFY_OK;
2158c9eb3e0fSArkadi Sharshevsky 
2159c4bb76a9SVladimir Oltean 			dp = dsa_slave_to_port(dev);
2160d5f19486SVladimir Oltean 		} else {
2161d5f19486SVladimir Oltean 			/* Snoop addresses learnt on foreign interfaces
2162d5f19486SVladimir Oltean 			 * bridged with us, for switches that don't
2163d5f19486SVladimir Oltean 			 * automatically learn SA from CPU-injected traffic
2164d5f19486SVladimir Oltean 			 */
2165d5f19486SVladimir Oltean 			struct net_device *br_dev;
2166d5f19486SVladimir Oltean 			struct dsa_slave_priv *p;
2167d5f19486SVladimir Oltean 
2168d5f19486SVladimir Oltean 			br_dev = netdev_master_upper_dev_get_rcu(dev);
2169d5f19486SVladimir Oltean 			if (!br_dev)
2170d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2171d5f19486SVladimir Oltean 
2172d5f19486SVladimir Oltean 			if (!netif_is_bridge_master(br_dev))
2173d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2174d5f19486SVladimir Oltean 
2175d5f19486SVladimir Oltean 			p = dsa_slave_dev_lower_find(br_dev);
2176d5f19486SVladimir Oltean 			if (!p)
2177d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2178d5f19486SVladimir Oltean 
2179d5f19486SVladimir Oltean 			dp = p->dp->cpu_dp;
2180d5f19486SVladimir Oltean 
2181d5f19486SVladimir Oltean 			if (!dp->ds->assisted_learning_on_cpu_port)
2182d5f19486SVladimir Oltean 				return NOTIFY_DONE;
2183d5f19486SVladimir Oltean 		}
2184c4bb76a9SVladimir Oltean 
21855fb4a451SVladimir Oltean 		if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del)
21865fb4a451SVladimir Oltean 			return NOTIFY_DONE;
21875fb4a451SVladimir Oltean 
2188c9eb3e0fSArkadi Sharshevsky 		switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2189c9eb3e0fSArkadi Sharshevsky 		if (!switchdev_work)
2190c9eb3e0fSArkadi Sharshevsky 			return NOTIFY_BAD;
2191c9eb3e0fSArkadi Sharshevsky 
2192c9eb3e0fSArkadi Sharshevsky 		INIT_WORK(&switchdev_work->work,
2193c9eb3e0fSArkadi Sharshevsky 			  dsa_slave_switchdev_event_work);
2194c4bb76a9SVladimir Oltean 		switchdev_work->ds = dp->ds;
2195c4bb76a9SVladimir Oltean 		switchdev_work->port = dp->index;
2196c9eb3e0fSArkadi Sharshevsky 		switchdev_work->event = event;
2197c9eb3e0fSArkadi Sharshevsky 
2198c4bb76a9SVladimir Oltean 		ether_addr_copy(switchdev_work->addr,
2199c4bb76a9SVladimir Oltean 				fdb_info->addr);
2200c4bb76a9SVladimir Oltean 		switchdev_work->vid = fdb_info->vid;
2201c4bb76a9SVladimir Oltean 
2202d5f19486SVladimir Oltean 		/* Hold a reference on the slave for dsa_fdb_offload_notify */
2203d5f19486SVladimir Oltean 		if (dsa_is_user_port(dp->ds, dp->index))
2204c9eb3e0fSArkadi Sharshevsky 			dev_hold(dev);
2205447d290aSVladimir Oltean 		dsa_schedule_work(&switchdev_work->work);
2206c9eb3e0fSArkadi Sharshevsky 		break;
2207c9eb3e0fSArkadi Sharshevsky 	default:
2208c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2209c9eb3e0fSArkadi Sharshevsky 	}
2210c9eb3e0fSArkadi Sharshevsky 
2211c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2212c9eb3e0fSArkadi Sharshevsky }
2213c9eb3e0fSArkadi Sharshevsky 
22142b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
22152b239f67SPetr Machata 					      unsigned long event, void *ptr)
22162b239f67SPetr Machata {
22172b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
221879b139f4SVivien Didelot 	int err;
22192b239f67SPetr Machata 
22202b239f67SPetr Machata 	switch (event) {
222179b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
222279b139f4SVivien Didelot 		err = switchdev_handle_port_obj_add(dev, ptr,
222379b139f4SVivien Didelot 						    dsa_slave_dev_check,
222479b139f4SVivien Didelot 						    dsa_slave_port_obj_add);
222579b139f4SVivien Didelot 		return notifier_from_errno(err);
22262b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
222779b139f4SVivien Didelot 		err = switchdev_handle_port_obj_del(dev, ptr,
222879b139f4SVivien Didelot 						    dsa_slave_dev_check,
222979b139f4SVivien Didelot 						    dsa_slave_port_obj_del);
223079b139f4SVivien Didelot 		return notifier_from_errno(err);
22319ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
223279b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
223379b139f4SVivien Didelot 						     dsa_slave_dev_check,
223479b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
223579b139f4SVivien Didelot 		return notifier_from_errno(err);
22362b239f67SPetr Machata 	}
22372b239f67SPetr Machata 
22382b239f67SPetr Machata 	return NOTIFY_DONE;
22392b239f67SPetr Machata }
22402b239f67SPetr Machata 
224188e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
224288e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
224388e4f0caSVivien Didelot };
224488e4f0caSVivien Didelot 
2245c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
2246c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2247c9eb3e0fSArkadi Sharshevsky };
2248c9eb3e0fSArkadi Sharshevsky 
22492b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
22502b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
22512b239f67SPetr Machata };
22522b239f67SPetr Machata 
225388e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
225488e4f0caSVivien Didelot {
22552b239f67SPetr Machata 	struct notifier_block *nb;
2256c9eb3e0fSArkadi Sharshevsky 	int err;
2257c9eb3e0fSArkadi Sharshevsky 
2258c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2259c9eb3e0fSArkadi Sharshevsky 	if (err)
2260c9eb3e0fSArkadi Sharshevsky 		return err;
2261c9eb3e0fSArkadi Sharshevsky 
2262c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2263c9eb3e0fSArkadi Sharshevsky 	if (err)
2264c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2265c9eb3e0fSArkadi Sharshevsky 
22662b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
22672b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
22682b239f67SPetr Machata 	if (err)
22692b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
22702b239f67SPetr Machata 
2271c9eb3e0fSArkadi Sharshevsky 	return 0;
2272c9eb3e0fSArkadi Sharshevsky 
22732b239f67SPetr Machata err_switchdev_blocking_nb:
22742b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2275c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
2276c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
2277c9eb3e0fSArkadi Sharshevsky 	return err;
227888e4f0caSVivien Didelot }
227988e4f0caSVivien Didelot 
228088e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
228188e4f0caSVivien Didelot {
22822b239f67SPetr Machata 	struct notifier_block *nb;
228388e4f0caSVivien Didelot 	int err;
228488e4f0caSVivien Didelot 
22852b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
22862b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
22872b239f67SPetr Machata 	if (err)
22882b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
22892b239f67SPetr Machata 
2290c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2291c9eb3e0fSArkadi Sharshevsky 	if (err)
2292c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2293c9eb3e0fSArkadi Sharshevsky 
229488e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
229588e4f0caSVivien Didelot 	if (err)
229688e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
229788e4f0caSVivien Didelot }
2298