xref: /openbmc/linux/net/dsa/slave.c (revision 15b04aceeb83086ea3109c331cb7d8c2767fa0c6)
191da11f8SLennert Buytenhek /*
291da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
3e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
491da11f8SLennert Buytenhek  *
591da11f8SLennert Buytenhek  * This program is free software; you can redistribute it and/or modify
691da11f8SLennert Buytenhek  * it under the terms of the GNU General Public License as published by
791da11f8SLennert Buytenhek  * the Free Software Foundation; either version 2 of the License, or
891da11f8SLennert Buytenhek  * (at your option) any later version.
991da11f8SLennert Buytenhek  */
1091da11f8SLennert Buytenhek 
1191da11f8SLennert Buytenhek #include <linux/list.h>
12df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
13b73adef6SFlorian Fainelli #include <linux/netdevice.h>
1491da11f8SLennert Buytenhek #include <linux/phy.h>
15a2820543SFlorian Fainelli #include <linux/phy_fixed.h>
16aab9c406SFlorian Fainelli #include <linux/phylink.h>
170d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
180d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
197f854420SAndrew Lunn #include <linux/mdio.h>
20b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
21f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
22f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
23b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2404ff53f9SFlorian Fainelli #include <linux/netpoll.h>
2590af1059SBrandon Streiff #include <linux/ptp_classify.h>
26ea5dd34bSVivien Didelot 
2791da11f8SLennert Buytenhek #include "dsa_priv.h"
2891da11f8SLennert Buytenhek 
29f50f2127SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev);
30f50f2127SFlorian Fainelli 
3191da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
3291da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
3391da11f8SLennert Buytenhek {
3491da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
3591da11f8SLennert Buytenhek 
360d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
379d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
3891da11f8SLennert Buytenhek 
3991da11f8SLennert Buytenhek 	return 0xffff;
4091da11f8SLennert Buytenhek }
4191da11f8SLennert Buytenhek 
4291da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
4391da11f8SLennert Buytenhek {
4491da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
4591da11f8SLennert Buytenhek 
460d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
479d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
4891da11f8SLennert Buytenhek 
4991da11f8SLennert Buytenhek 	return 0;
5091da11f8SLennert Buytenhek }
5191da11f8SLennert Buytenhek 
5291da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
5391da11f8SLennert Buytenhek {
5491da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
5591da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
5691da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
5791da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
580b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
5949463b7fSVivien Didelot 		 ds->dst->index, ds->index);
60c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
6124df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
6291da11f8SLennert Buytenhek }
6391da11f8SLennert Buytenhek 
6491da11f8SLennert Buytenhek 
6591da11f8SLennert Buytenhek /* slave device handling ****************************************************/
66abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
67c0840801SLennert Buytenhek {
68d0006b00SVivien Didelot 	return dsa_slave_to_master(dev)->ifindex;
69c0840801SLennert Buytenhek }
70c0840801SLennert Buytenhek 
7191da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
7291da11f8SLennert Buytenhek {
73d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
74d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
75df02c6ffSLennert Buytenhek 	int err;
76df02c6ffSLennert Buytenhek 
77df02c6ffSLennert Buytenhek 	if (!(master->flags & IFF_UP))
78df02c6ffSLennert Buytenhek 		return -ENETDOWN;
79df02c6ffSLennert Buytenhek 
808feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
81a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
82df02c6ffSLennert Buytenhek 		if (err < 0)
83df02c6ffSLennert Buytenhek 			goto out;
84df02c6ffSLennert Buytenhek 	}
85df02c6ffSLennert Buytenhek 
86df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI) {
87df02c6ffSLennert Buytenhek 		err = dev_set_allmulti(master, 1);
88df02c6ffSLennert Buytenhek 		if (err < 0)
89df02c6ffSLennert Buytenhek 			goto del_unicast;
90df02c6ffSLennert Buytenhek 	}
91df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC) {
92df02c6ffSLennert Buytenhek 		err = dev_set_promiscuity(master, 1);
93df02c6ffSLennert Buytenhek 		if (err < 0)
94df02c6ffSLennert Buytenhek 			goto clear_allmulti;
95df02c6ffSLennert Buytenhek 	}
96df02c6ffSLennert Buytenhek 
970115dcd1SVivien Didelot 	err = dsa_port_enable(dp, dev->phydev);
98b2f2af21SFlorian Fainelli 	if (err)
99b2f2af21SFlorian Fainelli 		goto clear_promisc;
100b73adef6SFlorian Fainelli 
101aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
102f7f1de51SFlorian Fainelli 
10391da11f8SLennert Buytenhek 	return 0;
104df02c6ffSLennert Buytenhek 
105b2f2af21SFlorian Fainelli clear_promisc:
106b2f2af21SFlorian Fainelli 	if (dev->flags & IFF_PROMISC)
1074fdeddfeSGilad Ben-Yossef 		dev_set_promiscuity(master, -1);
108df02c6ffSLennert Buytenhek clear_allmulti:
109df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
110df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
111df02c6ffSLennert Buytenhek del_unicast:
1128feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
113a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
114df02c6ffSLennert Buytenhek out:
115df02c6ffSLennert Buytenhek 	return err;
11691da11f8SLennert Buytenhek }
11791da11f8SLennert Buytenhek 
11891da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
11991da11f8SLennert Buytenhek {
120d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
121d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
122df02c6ffSLennert Buytenhek 
123aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
124f7f1de51SFlorian Fainelli 
12575104db0SAndrew Lunn 	dsa_port_disable(dp);
1266457edfeSVivien Didelot 
127df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
128a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
129df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
130df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
131df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
132df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
133df02c6ffSLennert Buytenhek 
1348feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
135a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
136df02c6ffSLennert Buytenhek 
13791da11f8SLennert Buytenhek 	return 0;
13891da11f8SLennert Buytenhek }
13991da11f8SLennert Buytenhek 
14091da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
14191da11f8SLennert Buytenhek {
142d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
14317ab4f61SRundong Ge 	if (dev->flags & IFF_UP) {
14491da11f8SLennert Buytenhek 		if (change & IFF_ALLMULTI)
14517ab4f61SRundong Ge 			dev_set_allmulti(master,
14617ab4f61SRundong Ge 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
14791da11f8SLennert Buytenhek 		if (change & IFF_PROMISC)
14817ab4f61SRundong Ge 			dev_set_promiscuity(master,
14917ab4f61SRundong Ge 					    dev->flags & IFF_PROMISC ? 1 : -1);
15017ab4f61SRundong Ge 	}
15191da11f8SLennert Buytenhek }
15291da11f8SLennert Buytenhek 
15391da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
15491da11f8SLennert Buytenhek {
155d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
15691da11f8SLennert Buytenhek 
15791da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
158a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
15991da11f8SLennert Buytenhek }
16091da11f8SLennert Buytenhek 
161df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
16291da11f8SLennert Buytenhek {
163d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
164df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
165df02c6ffSLennert Buytenhek 	int err;
166df02c6ffSLennert Buytenhek 
167df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
168df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
169df02c6ffSLennert Buytenhek 
170df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
171df02c6ffSLennert Buytenhek 		goto out;
172df02c6ffSLennert Buytenhek 
1738feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
174a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
175df02c6ffSLennert Buytenhek 		if (err < 0)
176df02c6ffSLennert Buytenhek 			return err;
177df02c6ffSLennert Buytenhek 	}
178df02c6ffSLennert Buytenhek 
1798feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
180a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
181df02c6ffSLennert Buytenhek 
182df02c6ffSLennert Buytenhek out:
183d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
18491da11f8SLennert Buytenhek 
18591da11f8SLennert Buytenhek 	return 0;
18691da11f8SLennert Buytenhek }
18791da11f8SLennert Buytenhek 
1882bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1892bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1902bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1912bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1922bedde1aSArkadi Sharshevsky 	int idx;
1932bedde1aSArkadi Sharshevsky };
1942bedde1aSArkadi Sharshevsky 
1952bedde1aSArkadi Sharshevsky static int
1962bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1972bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
1982bedde1aSArkadi Sharshevsky {
1992bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
2002bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
2012bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
2022bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
2032bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
2042bedde1aSArkadi Sharshevsky 
2052bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
2062bedde1aSArkadi Sharshevsky 		goto skip;
2072bedde1aSArkadi Sharshevsky 
2082bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
2092bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2102bedde1aSArkadi Sharshevsky 	if (!nlh)
2112bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2122bedde1aSArkadi Sharshevsky 
2132bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2142bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2152bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2162bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2172bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2182bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2192bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2202bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2212bedde1aSArkadi Sharshevsky 
2222bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2232bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2242bedde1aSArkadi Sharshevsky 
2252bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2262bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2272bedde1aSArkadi Sharshevsky 
2282bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2292bedde1aSArkadi Sharshevsky 
2302bedde1aSArkadi Sharshevsky skip:
2312bedde1aSArkadi Sharshevsky 	dump->idx++;
2322bedde1aSArkadi Sharshevsky 	return 0;
2332bedde1aSArkadi Sharshevsky 
2342bedde1aSArkadi Sharshevsky nla_put_failure:
2352bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2362bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2372bedde1aSArkadi Sharshevsky }
2382bedde1aSArkadi Sharshevsky 
2392bedde1aSArkadi Sharshevsky static int
2402bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2412bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2422bedde1aSArkadi Sharshevsky 		   int *idx)
2432bedde1aSArkadi Sharshevsky {
244d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2452bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2462bedde1aSArkadi Sharshevsky 		.dev = dev,
2472bedde1aSArkadi Sharshevsky 		.skb = skb,
2482bedde1aSArkadi Sharshevsky 		.cb = cb,
2492bedde1aSArkadi Sharshevsky 		.idx = *idx,
2502bedde1aSArkadi Sharshevsky 	};
2512bedde1aSArkadi Sharshevsky 	int err;
2522bedde1aSArkadi Sharshevsky 
253de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2542bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
255de40fc5dSVivien Didelot 
2562bedde1aSArkadi Sharshevsky 	return err;
2572bedde1aSArkadi Sharshevsky }
2582bedde1aSArkadi Sharshevsky 
25991da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
26091da11f8SLennert Buytenhek {
2610336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2620336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2630336369dSBrandon Streiff 	int port = p->dp->index;
2640336369dSBrandon Streiff 
2650336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2660336369dSBrandon Streiff 	switch (cmd) {
2670336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2680336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2690336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2700336369dSBrandon Streiff 		break;
2710336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2720336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2730336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2740336369dSBrandon Streiff 		break;
2750336369dSBrandon Streiff 	}
2760336369dSBrandon Streiff 
277aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
27891da11f8SLennert Buytenhek }
27991da11f8SLennert Buytenhek 
28035636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
281f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
2827ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
28335636062SScott Feldman {
284d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
285b8d866acSVivien Didelot 	int ret;
28635636062SScott Feldman 
28735636062SScott Feldman 	switch (attr->id) {
2881f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
289fd364541SVivien Didelot 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
29035636062SScott Feldman 		break;
291fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
292c02c4175SVivien Didelot 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
293c02c4175SVivien Didelot 					      trans);
294fb2dabadSVivien Didelot 		break;
29534a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
296072bb190SVivien Didelot 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
29734a79f63SVivien Didelot 		break;
298ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
299ea87005aSFlorian Fainelli 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
300ea87005aSFlorian Fainelli 						trans);
301ea87005aSFlorian Fainelli 		break;
30257652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
30357652796SRussell King 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
30457652796SRussell King 		break;
30535636062SScott Feldman 	default:
30635636062SScott Feldman 		ret = -EOPNOTSUPP;
30735636062SScott Feldman 		break;
30835636062SScott Feldman 	}
30935636062SScott Feldman 
31035636062SScott Feldman 	return ret;
31135636062SScott Feldman }
31235636062SScott Feldman 
313ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
314648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
3157ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
316ba14d9ebSVivien Didelot {
317d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
318ba14d9ebSVivien Didelot 	int err;
319ba14d9ebSVivien Didelot 
320ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
321ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
322ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
323ba14d9ebSVivien Didelot 	 */
324ba14d9ebSVivien Didelot 
3259e8f4a54SJiri Pirko 	switch (obj->id) {
3268df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
327bcebb976SVivien Didelot 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
3288df30255SVivien Didelot 		break;
3295f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
3305f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
3315f4dbc50SAndrew Lunn 		 * but on the CPU port.
3325f4dbc50SAndrew Lunn 		 */
3335f4dbc50SAndrew Lunn 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
3345f4dbc50SAndrew Lunn 				       trans);
3355f4dbc50SAndrew Lunn 		break;
33657d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
33701676d12SVivien Didelot 		err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
338648b4a99SJiri Pirko 					trans);
33911149536SVivien Didelot 		break;
340ba14d9ebSVivien Didelot 	default:
341ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
342ba14d9ebSVivien Didelot 		break;
343ba14d9ebSVivien Didelot 	}
344ba14d9ebSVivien Didelot 
345ba14d9ebSVivien Didelot 	return err;
346ba14d9ebSVivien Didelot }
347ba14d9ebSVivien Didelot 
348ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
349648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
350ba14d9ebSVivien Didelot {
351d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
352ba14d9ebSVivien Didelot 	int err;
353ba14d9ebSVivien Didelot 
3549e8f4a54SJiri Pirko 	switch (obj->id) {
3558df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
356bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
3578df30255SVivien Didelot 		break;
3585f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
3595f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
3605f4dbc50SAndrew Lunn 		 * but on the CPU port.
3615f4dbc50SAndrew Lunn 		 */
3625f4dbc50SAndrew Lunn 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
3635f4dbc50SAndrew Lunn 		break;
36457d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
36501676d12SVivien Didelot 		err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
36611149536SVivien Didelot 		break;
367ba14d9ebSVivien Didelot 	default:
368ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
369ba14d9ebSVivien Didelot 		break;
370ba14d9ebSVivien Didelot 	}
371ba14d9ebSVivien Didelot 
372ba14d9ebSVivien Didelot 	return err;
373ba14d9ebSVivien Didelot }
374ba14d9ebSVivien Didelot 
375929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
376929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
377b73adef6SFlorian Fainelli {
378d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
379d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
380a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
381b73adef6SFlorian Fainelli 
382*15b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
383*15b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
384*15b04aceSJiri Pirko 	 * should be removed with legacy support.
385*15b04aceSJiri Pirko 	 */
386*15b04aceSJiri Pirko 	if (dp->ds->devlink)
387*15b04aceSJiri Pirko 		return -EOPNOTSUPP;
388*15b04aceSJiri Pirko 
389929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
390929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
391929d6c14SFlorian Fainelli 
392929d6c14SFlorian Fainelli 	return 0;
393929d6c14SFlorian Fainelli }
394929d6c14SFlorian Fainelli 
3954fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
39604ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
39704ff53f9SFlorian Fainelli {
39804ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
3994fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
4004fa7b718SVivien Didelot 
40104ff53f9SFlorian Fainelli 	if (p->netpoll)
40204ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
40304ff53f9SFlorian Fainelli #else
40404ff53f9SFlorian Fainelli 	BUG();
40504ff53f9SFlorian Fainelli #endif
40604ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
40704ff53f9SFlorian Fainelli }
40804ff53f9SFlorian Fainelli 
40990af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
41090af1059SBrandon Streiff 				 struct sk_buff *skb)
41190af1059SBrandon Streiff {
41290af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
41390af1059SBrandon Streiff 	struct sk_buff *clone;
41490af1059SBrandon Streiff 	unsigned int type;
41590af1059SBrandon Streiff 
41690af1059SBrandon Streiff 	type = ptp_classify_raw(skb);
41790af1059SBrandon Streiff 	if (type == PTP_CLASS_NONE)
41890af1059SBrandon Streiff 		return;
41990af1059SBrandon Streiff 
42090af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
42190af1059SBrandon Streiff 		return;
42290af1059SBrandon Streiff 
42390af1059SBrandon Streiff 	clone = skb_clone_sk(skb);
42490af1059SBrandon Streiff 	if (!clone)
42590af1059SBrandon Streiff 		return;
42690af1059SBrandon Streiff 
42790af1059SBrandon Streiff 	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
42890af1059SBrandon Streiff 		return;
42990af1059SBrandon Streiff 
43090af1059SBrandon Streiff 	kfree_skb(clone);
43190af1059SBrandon Streiff }
43290af1059SBrandon Streiff 
4333e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
4343e8a72d1SFlorian Fainelli {
4353e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
4365f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
4374ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
4383e8a72d1SFlorian Fainelli 
4395f6b4e14SFlorian Fainelli 	s = this_cpu_ptr(p->stats64);
4405f6b4e14SFlorian Fainelli 	u64_stats_update_begin(&s->syncp);
4415f6b4e14SFlorian Fainelli 	s->tx_packets++;
4425f6b4e14SFlorian Fainelli 	s->tx_bytes += skb->len;
4435f6b4e14SFlorian Fainelli 	u64_stats_update_end(&s->syncp);
4443e8a72d1SFlorian Fainelli 
44590af1059SBrandon Streiff 	/* Identify PTP protocol packets, clone them, and pass them to the
44690af1059SBrandon Streiff 	 * switch driver
44790af1059SBrandon Streiff 	 */
44890af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
44990af1059SBrandon Streiff 
450fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
451fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
452fe47d563SVivien Didelot 	 */
4534ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
454fe47d563SVivien Didelot 	if (!nskb) {
455fe47d563SVivien Didelot 		kfree_skb(skb);
4564ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
457fe47d563SVivien Didelot 	}
4585aed85ceSFlorian Fainelli 
45904ff53f9SFlorian Fainelli 	/* SKB for netpoll still need to be mangled with the protocol-specific
46004ff53f9SFlorian Fainelli 	 * tag to be successfully transmitted
46104ff53f9SFlorian Fainelli 	 */
46204ff53f9SFlorian Fainelli 	if (unlikely(netpoll_tx_running(dev)))
4634fa7b718SVivien Didelot 		return dsa_slave_netpoll_send_skb(dev, nskb);
46404ff53f9SFlorian Fainelli 
4654ed70ce9SFlorian Fainelli 	/* Queue the SKB for transmission on the parent interface, but
4664ed70ce9SFlorian Fainelli 	 * do not modify its EtherType
4674ed70ce9SFlorian Fainelli 	 */
468d0006b00SVivien Didelot 	nskb->dev = dsa_slave_to_master(dev);
4694ed70ce9SFlorian Fainelli 	dev_queue_xmit(nskb);
4705aed85ceSFlorian Fainelli 
4715aed85ceSFlorian Fainelli 	return NETDEV_TX_OK;
4725aed85ceSFlorian Fainelli }
4735aed85ceSFlorian Fainelli 
47491da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
47591da11f8SLennert Buytenhek 
47691da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
47791da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
47891da11f8SLennert Buytenhek {
4797826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
4807826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
4817826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
48291da11f8SLennert Buytenhek }
48391da11f8SLennert Buytenhek 
4843d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
4853d762a0fSGuenter Roeck {
486d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
487d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
4883d762a0fSGuenter Roeck 
4899d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
490d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
4913d762a0fSGuenter Roeck 
4923d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
4933d762a0fSGuenter Roeck }
4943d762a0fSGuenter Roeck 
4953d762a0fSGuenter Roeck static void
4963d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
4973d762a0fSGuenter Roeck {
498d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
499d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5003d762a0fSGuenter Roeck 
5019d490b4eSVivien Didelot 	if (ds->ops->get_regs)
502d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
5033d762a0fSGuenter Roeck }
5043d762a0fSGuenter Roeck 
505aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
506aab9c406SFlorian Fainelli {
507aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
508aab9c406SFlorian Fainelli 
509aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
510aab9c406SFlorian Fainelli }
511aab9c406SFlorian Fainelli 
5126793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
5136793abb4SGuenter Roeck {
514d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
515d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5166793abb4SGuenter Roeck 
5170e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
518ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
5196793abb4SGuenter Roeck 
5209d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
5219d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
5226793abb4SGuenter Roeck 
5236793abb4SGuenter Roeck 	return 0;
5246793abb4SGuenter Roeck }
5256793abb4SGuenter Roeck 
5266793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
5276793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5286793abb4SGuenter Roeck {
529d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
530d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5316793abb4SGuenter Roeck 
5329d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
5339d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
5346793abb4SGuenter Roeck 
5356793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5366793abb4SGuenter Roeck }
5376793abb4SGuenter Roeck 
5386793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
5396793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5406793abb4SGuenter Roeck {
541d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
542d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5436793abb4SGuenter Roeck 
5449d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
5459d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
5466793abb4SGuenter Roeck 
5476793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5486793abb4SGuenter Roeck }
5496793abb4SGuenter Roeck 
55091da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
55191da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
55291da11f8SLennert Buytenhek {
553d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
554d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
55591da11f8SLennert Buytenhek 
55691da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
55791da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
55891da11f8SLennert Buytenhek 
55991da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
56091da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
56191da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
56291da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
5639d490b4eSVivien Didelot 		if (ds->ops->get_strings)
56489f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
56589f09048SFlorian Fainelli 					     data + 4 * len);
56691da11f8SLennert Buytenhek 	}
56791da11f8SLennert Buytenhek }
56891da11f8SLennert Buytenhek 
56991da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
57091da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
57191da11f8SLennert Buytenhek 					uint64_t *data)
57291da11f8SLennert Buytenhek {
573d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
57491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
575d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5765f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
577f613ed66SFlorian Fainelli 	unsigned int start;
5785f6b4e14SFlorian Fainelli 	int i;
57991da11f8SLennert Buytenhek 
5805f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
5815f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
5825f6b4e14SFlorian Fainelli 
5835f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
584f613ed66SFlorian Fainelli 		do {
5855f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
5865f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
5875f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
5885f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
5895f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
5905f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
5915f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
5925f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
5935f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
5945f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
5955f6b4e14SFlorian Fainelli 	}
5969d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
597d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
59891da11f8SLennert Buytenhek }
59991da11f8SLennert Buytenhek 
60091da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
60191da11f8SLennert Buytenhek {
602d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
603d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
60491da11f8SLennert Buytenhek 
60591da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
60691da11f8SLennert Buytenhek 		int count;
60791da11f8SLennert Buytenhek 
60891da11f8SLennert Buytenhek 		count = 4;
6099d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
61089f09048SFlorian Fainelli 			count += ds->ops->get_sset_count(ds, dp->index, sset);
61191da11f8SLennert Buytenhek 
61291da11f8SLennert Buytenhek 		return count;
61391da11f8SLennert Buytenhek 	}
61491da11f8SLennert Buytenhek 
61591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
61691da11f8SLennert Buytenhek }
61791da11f8SLennert Buytenhek 
61819e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
61919e57c4eSFlorian Fainelli {
620d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
621d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
62219e57c4eSFlorian Fainelli 
623aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
624aab9c406SFlorian Fainelli 
6259d490b4eSVivien Didelot 	if (ds->ops->get_wol)
626d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
62719e57c4eSFlorian Fainelli }
62819e57c4eSFlorian Fainelli 
62919e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
63019e57c4eSFlorian Fainelli {
631d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
632d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
63319e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
63419e57c4eSFlorian Fainelli 
635aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
636aab9c406SFlorian Fainelli 
6379d490b4eSVivien Didelot 	if (ds->ops->set_wol)
638d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
63919e57c4eSFlorian Fainelli 
64019e57c4eSFlorian Fainelli 	return ret;
64119e57c4eSFlorian Fainelli }
64219e57c4eSFlorian Fainelli 
6437905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
6447905288fSFlorian Fainelli {
645d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
646d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6477905288fSFlorian Fainelli 	int ret;
6487905288fSFlorian Fainelli 
6497b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
65000670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
6517b9cc738SVivien Didelot 		return -ENODEV;
6527b9cc738SVivien Didelot 
65308f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
6547905288fSFlorian Fainelli 		return -EOPNOTSUPP;
6557905288fSFlorian Fainelli 
656d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
6577905288fSFlorian Fainelli 	if (ret)
6587905288fSFlorian Fainelli 		return ret;
6597905288fSFlorian Fainelli 
660aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
6617905288fSFlorian Fainelli }
6627905288fSFlorian Fainelli 
6637905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
6647905288fSFlorian Fainelli {
665d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
666d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6677905288fSFlorian Fainelli 	int ret;
6687905288fSFlorian Fainelli 
6697b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
67000670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
6717b9cc738SVivien Didelot 		return -ENODEV;
6727b9cc738SVivien Didelot 
67308f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
6747905288fSFlorian Fainelli 		return -EOPNOTSUPP;
6757905288fSFlorian Fainelli 
676d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
6777905288fSFlorian Fainelli 	if (ret)
6787905288fSFlorian Fainelli 		return ret;
6797905288fSFlorian Fainelli 
680aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
681aab9c406SFlorian Fainelli }
682aab9c406SFlorian Fainelli 
683aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
684aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
685aab9c406SFlorian Fainelli {
686aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
687aab9c406SFlorian Fainelli 
688aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
689aab9c406SFlorian Fainelli }
690aab9c406SFlorian Fainelli 
691aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
692aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
693aab9c406SFlorian Fainelli {
694aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
695aab9c406SFlorian Fainelli 
696aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
6977905288fSFlorian Fainelli }
6987905288fSFlorian Fainelli 
69904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
70004ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
70104ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
70204ff53f9SFlorian Fainelli {
703d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
70404ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
70504ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
70604ff53f9SFlorian Fainelli 	int err = 0;
70704ff53f9SFlorian Fainelli 
70804ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
70904ff53f9SFlorian Fainelli 	if (!netpoll)
71004ff53f9SFlorian Fainelli 		return -ENOMEM;
71104ff53f9SFlorian Fainelli 
71204ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
71304ff53f9SFlorian Fainelli 	if (err) {
71404ff53f9SFlorian Fainelli 		kfree(netpoll);
71504ff53f9SFlorian Fainelli 		goto out;
71604ff53f9SFlorian Fainelli 	}
71704ff53f9SFlorian Fainelli 
71804ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
71904ff53f9SFlorian Fainelli out:
72004ff53f9SFlorian Fainelli 	return err;
72104ff53f9SFlorian Fainelli }
72204ff53f9SFlorian Fainelli 
72304ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
72404ff53f9SFlorian Fainelli {
72504ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
72604ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
72704ff53f9SFlorian Fainelli 
72804ff53f9SFlorian Fainelli 	if (!netpoll)
72904ff53f9SFlorian Fainelli 		return;
73004ff53f9SFlorian Fainelli 
73104ff53f9SFlorian Fainelli 	p->netpoll = NULL;
73204ff53f9SFlorian Fainelli 
733c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
73404ff53f9SFlorian Fainelli }
73504ff53f9SFlorian Fainelli 
73604ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
73704ff53f9SFlorian Fainelli {
73804ff53f9SFlorian Fainelli }
73904ff53f9SFlorian Fainelli #endif
74004ff53f9SFlorian Fainelli 
74144bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
74244bb765cSFlorian Fainelli 					char *name, size_t len)
74344bb765cSFlorian Fainelli {
744d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
74544bb765cSFlorian Fainelli 
746d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
747d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
748d484210bSJiri Pirko 	 * should be removed with legacy support.
749d484210bSJiri Pirko 	 */
750d484210bSJiri Pirko 	if (dp->ds->devlink)
751d484210bSJiri Pirko 		return -EOPNOTSUPP;
752d484210bSJiri Pirko 
753d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
75444bb765cSFlorian Fainelli 		return -EINVAL;
7553a543ef4SFlorian Fainelli 
7563a543ef4SFlorian Fainelli 	return 0;
7573a543ef4SFlorian Fainelli }
7583a543ef4SFlorian Fainelli 
759f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
7604fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
761f50f2127SFlorian Fainelli {
7624fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
763f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
764f50f2127SFlorian Fainelli 
765f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
766f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
767f50f2127SFlorian Fainelli 			return mall_tc_entry;
768f50f2127SFlorian Fainelli 
769f50f2127SFlorian Fainelli 	return NULL;
770f50f2127SFlorian Fainelli }
771f50f2127SFlorian Fainelli 
772f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
773f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
774f50f2127SFlorian Fainelli 				      bool ingress)
775f50f2127SFlorian Fainelli {
776d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
777f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
778f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
7795fd9fc4eSJiri Pirko 	__be16 protocol = cls->common.protocol;
780d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
781f50f2127SFlorian Fainelli 	struct net_device *to_dev;
782f50f2127SFlorian Fainelli 	const struct tc_action *a;
783d945097bSVivien Didelot 	struct dsa_port *to_dp;
784f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
785f50f2127SFlorian Fainelli 
786f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
787f50f2127SFlorian Fainelli 		return err;
788f50f2127SFlorian Fainelli 
7893bcc0cecSJiri Pirko 	if (!tcf_exts_has_one_action(cls->exts))
790f50f2127SFlorian Fainelli 		return err;
791f50f2127SFlorian Fainelli 
792244cd96aSCong Wang 	a = tcf_exts_first_action(cls->exts);
793f50f2127SFlorian Fainelli 
794f50f2127SFlorian Fainelli 	if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
795f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
796f50f2127SFlorian Fainelli 
7979f8a739eSCong Wang 		to_dev = tcf_mirred_dev(a);
798f50f2127SFlorian Fainelli 		if (!to_dev)
799f50f2127SFlorian Fainelli 			return -EINVAL;
800f50f2127SFlorian Fainelli 
801f50f2127SFlorian Fainelli 		if (!dsa_slave_dev_check(to_dev))
802f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
803f50f2127SFlorian Fainelli 
804f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
805f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
806f50f2127SFlorian Fainelli 			return -ENOMEM;
807f50f2127SFlorian Fainelli 
808f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
809f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
810f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
811f50f2127SFlorian Fainelli 
812d945097bSVivien Didelot 		to_dp = dsa_slave_to_port(to_dev);
813f50f2127SFlorian Fainelli 
814d945097bSVivien Didelot 		mirror->to_local_port = to_dp->index;
815f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
816f50f2127SFlorian Fainelli 
817d945097bSVivien Didelot 		err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
818f50f2127SFlorian Fainelli 		if (err) {
819f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
820f50f2127SFlorian Fainelli 			return err;
821f50f2127SFlorian Fainelli 		}
822f50f2127SFlorian Fainelli 
823f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
824f50f2127SFlorian Fainelli 	}
825f50f2127SFlorian Fainelli 
826f50f2127SFlorian Fainelli 	return 0;
827f50f2127SFlorian Fainelli }
828f50f2127SFlorian Fainelli 
829f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
830f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
831f50f2127SFlorian Fainelli {
832d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
833f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
834d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
835f50f2127SFlorian Fainelli 
836f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
837f50f2127SFlorian Fainelli 		return;
838f50f2127SFlorian Fainelli 
8394fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
840f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
841f50f2127SFlorian Fainelli 		return;
842f50f2127SFlorian Fainelli 
843f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
844f50f2127SFlorian Fainelli 
845f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
846f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
847d945097bSVivien Didelot 		ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
848f50f2127SFlorian Fainelli 		break;
849f50f2127SFlorian Fainelli 	default:
850f50f2127SFlorian Fainelli 		WARN_ON(1);
851f50f2127SFlorian Fainelli 	}
852f50f2127SFlorian Fainelli 
853f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
854f50f2127SFlorian Fainelli }
855f50f2127SFlorian Fainelli 
8563fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
8576b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
8586b3eb752SJiri Pirko 					   bool ingress)
859f50f2127SFlorian Fainelli {
8605fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
861a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
862f50f2127SFlorian Fainelli 
8633fbae382SJiri Pirko 	switch (cls->command) {
8643fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
8655fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
8663fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
8673fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
8683fbae382SJiri Pirko 		return 0;
8693fbae382SJiri Pirko 	default:
8703fbae382SJiri Pirko 		return -EOPNOTSUPP;
8713fbae382SJiri Pirko 	}
8723fbae382SJiri Pirko }
8733fbae382SJiri Pirko 
8746b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8756b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
8766b3eb752SJiri Pirko {
8776b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
8786b3eb752SJiri Pirko 
87944ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
88044ae12a7SJiri Pirko 		return -EOPNOTSUPP;
88144ae12a7SJiri Pirko 
8826b3eb752SJiri Pirko 	switch (type) {
8836b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
8846b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
8856b3eb752SJiri Pirko 	default:
8866b3eb752SJiri Pirko 		return -EOPNOTSUPP;
8876b3eb752SJiri Pirko 	}
8886b3eb752SJiri Pirko }
8896b3eb752SJiri Pirko 
8906b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
8916b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
8926b3eb752SJiri Pirko {
8936b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
8946b3eb752SJiri Pirko }
8956b3eb752SJiri Pirko 
8966b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
8976b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
8986b3eb752SJiri Pirko {
8996b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
9006b3eb752SJiri Pirko }
9016b3eb752SJiri Pirko 
9026b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
9036b3eb752SJiri Pirko 				    struct tc_block_offload *f)
9046b3eb752SJiri Pirko {
9056b3eb752SJiri Pirko 	tc_setup_cb_t *cb;
9066b3eb752SJiri Pirko 
9076b3eb752SJiri Pirko 	if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9086b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
9096b3eb752SJiri Pirko 	else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
9106b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
9116b3eb752SJiri Pirko 	else
9126b3eb752SJiri Pirko 		return -EOPNOTSUPP;
9136b3eb752SJiri Pirko 
9146b3eb752SJiri Pirko 	switch (f->command) {
9156b3eb752SJiri Pirko 	case TC_BLOCK_BIND:
91660513bd8SJohn Hurley 		return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
9176b3eb752SJiri Pirko 	case TC_BLOCK_UNBIND:
9186b3eb752SJiri Pirko 		tcf_block_cb_unregister(f->block, cb, dev);
9196b3eb752SJiri Pirko 		return 0;
9206b3eb752SJiri Pirko 	default:
9216b3eb752SJiri Pirko 		return -EOPNOTSUPP;
9226b3eb752SJiri Pirko 	}
9236b3eb752SJiri Pirko }
9246b3eb752SJiri Pirko 
9253fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
926de4784caSJiri Pirko 			      void *type_data)
9273fbae382SJiri Pirko {
9282572ac53SJiri Pirko 	switch (type) {
9296b3eb752SJiri Pirko 	case TC_SETUP_BLOCK:
9306b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
931f50f2127SFlorian Fainelli 	default:
932a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
933f50f2127SFlorian Fainelli 	}
934f50f2127SFlorian Fainelli }
935f50f2127SFlorian Fainelli 
936f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev,
937f613ed66SFlorian Fainelli 				  struct rtnl_link_stats64 *stats)
938f613ed66SFlorian Fainelli {
939f613ed66SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
9405f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
941f613ed66SFlorian Fainelli 	unsigned int start;
9425f6b4e14SFlorian Fainelli 	int i;
943f613ed66SFlorian Fainelli 
944f613ed66SFlorian Fainelli 	netdev_stats_to_stats64(stats, &dev->stats);
9455f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
9465f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
9475f6b4e14SFlorian Fainelli 
9485f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
949f613ed66SFlorian Fainelli 		do {
9505f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
9515f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
9525f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
9535f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
9545f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
9555f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
9565f6b4e14SFlorian Fainelli 
9575f6b4e14SFlorian Fainelli 		stats->tx_packets += tx_packets;
9585f6b4e14SFlorian Fainelli 		stats->tx_bytes += tx_bytes;
9595f6b4e14SFlorian Fainelli 		stats->rx_packets += rx_packets;
9605f6b4e14SFlorian Fainelli 		stats->rx_bytes += rx_bytes;
9615f6b4e14SFlorian Fainelli 	}
962f613ed66SFlorian Fainelli }
963f613ed66SFlorian Fainelli 
964bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
965bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
966bf9f2648SFlorian Fainelli {
967d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
968d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
969bf9f2648SFlorian Fainelli 
970bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
971bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
972bf9f2648SFlorian Fainelli 
973d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
974bf9f2648SFlorian Fainelli }
975bf9f2648SFlorian Fainelli 
976bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
977bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
978bf9f2648SFlorian Fainelli {
979d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
980d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
981bf9f2648SFlorian Fainelli 
982bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
983bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
984bf9f2648SFlorian Fainelli 
985d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
986bf9f2648SFlorian Fainelli }
987bf9f2648SFlorian Fainelli 
9880336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
9890336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
9900336369dSBrandon Streiff {
9910336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
9920336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
9930336369dSBrandon Streiff 
9940336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
9950336369dSBrandon Streiff 		return -EOPNOTSUPP;
9960336369dSBrandon Streiff 
9970336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
9980336369dSBrandon Streiff }
9990336369dSBrandon Streiff 
1000061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1001061f6a50SFlorian Fainelli 				     u16 vid)
1002061f6a50SFlorian Fainelli {
1003061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1004061f6a50SFlorian Fainelli 	struct switchdev_obj_port_vlan vlan = {
1005061f6a50SFlorian Fainelli 		.vid_begin = vid,
1006061f6a50SFlorian Fainelli 		.vid_end = vid,
1007061f6a50SFlorian Fainelli 		/* This API only allows programming tagged, non-PVID VIDs */
1008061f6a50SFlorian Fainelli 		.flags = 0,
1009061f6a50SFlorian Fainelli 	};
1010d6af21a4SFlorian Fainelli 	struct switchdev_trans trans;
1011061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1012061f6a50SFlorian Fainelli 	int ret;
1013061f6a50SFlorian Fainelli 
1014061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1015061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1016061f6a50SFlorian Fainelli 	 */
1017061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
1018061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1019061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1020061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1021061f6a50SFlorian Fainelli 		 */
1022061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1023061f6a50SFlorian Fainelli 		if (ret == 0)
1024061f6a50SFlorian Fainelli 			return -EBUSY;
1025061f6a50SFlorian Fainelli 	}
1026061f6a50SFlorian Fainelli 
1027d6af21a4SFlorian Fainelli 	trans.ph_prepare = true;
1028d6af21a4SFlorian Fainelli 	ret = dsa_port_vlan_add(dp, &vlan, &trans);
1029061f6a50SFlorian Fainelli 	if (ret == -EOPNOTSUPP)
1030d6af21a4SFlorian Fainelli 		return 0;
1031061f6a50SFlorian Fainelli 
1032d6af21a4SFlorian Fainelli 	trans.ph_prepare = false;
1033d6af21a4SFlorian Fainelli 	return dsa_port_vlan_add(dp, &vlan, &trans);
1034061f6a50SFlorian Fainelli }
1035061f6a50SFlorian Fainelli 
1036061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1037061f6a50SFlorian Fainelli 				      u16 vid)
1038061f6a50SFlorian Fainelli {
1039061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1040061f6a50SFlorian Fainelli 	struct switchdev_obj_port_vlan vlan = {
1041061f6a50SFlorian Fainelli 		.vid_begin = vid,
1042061f6a50SFlorian Fainelli 		.vid_end = vid,
1043061f6a50SFlorian Fainelli 		/* This API only allows programming tagged, non-PVID VIDs */
1044061f6a50SFlorian Fainelli 		.flags = 0,
1045061f6a50SFlorian Fainelli 	};
1046061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1047061f6a50SFlorian Fainelli 	int ret;
1048061f6a50SFlorian Fainelli 
1049061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1050061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1051061f6a50SFlorian Fainelli 	 */
1052061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
1053061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1054061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1055061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1056061f6a50SFlorian Fainelli 		 */
1057061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1058061f6a50SFlorian Fainelli 		if (ret == 0)
1059061f6a50SFlorian Fainelli 			return -EBUSY;
1060061f6a50SFlorian Fainelli 	}
1061061f6a50SFlorian Fainelli 
1062061f6a50SFlorian Fainelli 	ret = dsa_port_vlan_del(dp, &vlan);
1063061f6a50SFlorian Fainelli 	if (ret == -EOPNOTSUPP)
1064061f6a50SFlorian Fainelli 		ret = 0;
1065061f6a50SFlorian Fainelli 
1066061f6a50SFlorian Fainelli 	return ret;
1067061f6a50SFlorian Fainelli }
1068061f6a50SFlorian Fainelli 
106991da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
107091da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
10713d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
10723d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1073aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1074c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
10756793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
10766793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
10776793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
107891da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
107991da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
108091da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
108119e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
108219e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
10837905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
10847905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1085aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1086aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1087bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1088bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
10890336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
109091da11f8SLennert Buytenhek };
109191da11f8SLennert Buytenhek 
10922a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
10932a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
10942a93c1a3SFlorian Fainelli 		       struct net_device *dev,
10952a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid,
109687b0984eSPetr Machata 		       u16 flags,
109787b0984eSPetr Machata 		       struct netlink_ext_ack *extack)
10982a93c1a3SFlorian Fainelli {
10992a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
11002a93c1a3SFlorian Fainelli 
11012a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
11022a93c1a3SFlorian Fainelli }
11032a93c1a3SFlorian Fainelli 
11042a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
11052a93c1a3SFlorian Fainelli 		       struct net_device *dev,
11062a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid)
11072a93c1a3SFlorian Fainelli {
11082a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
11092a93c1a3SFlorian Fainelli 
11102a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
11112a93c1a3SFlorian Fainelli }
11122a93c1a3SFlorian Fainelli 
1113716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1114716efee2SJiri Pirko {
1115716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1116716efee2SJiri Pirko 
1117716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1118716efee2SJiri Pirko }
1119716efee2SJiri Pirko 
11203e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1121d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1122d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
11233e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1124d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1125d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1126d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
112737b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
112837b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
11292bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1130d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1131abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
113204ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
113304ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
113404ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
113504ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
113604ff53f9SFlorian Fainelli #endif
113744bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1138f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1139f613ed66SFlorian Fainelli 	.ndo_get_stats64	= dsa_slave_get_stats64,
1140929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1141061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1142061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1143716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
114498237d43SScott Feldman };
114598237d43SScott Feldman 
1146f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1147f37db85dSFlorian Fainelli 	.name	= "dsa",
1148f37db85dSFlorian Fainelli };
1149f37db85dSFlorian Fainelli 
1150aab9c406SFlorian Fainelli static void dsa_slave_phylink_validate(struct net_device *dev,
1151aab9c406SFlorian Fainelli 				       unsigned long *supported,
1152aab9c406SFlorian Fainelli 				       struct phylink_link_state *state)
11530d8bcdd3SFlorian Fainelli {
1154d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1155d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
11560d8bcdd3SFlorian Fainelli 
1157aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_validate)
1158aab9c406SFlorian Fainelli 		return;
1159aab9c406SFlorian Fainelli 
1160aab9c406SFlorian Fainelli 	ds->ops->phylink_validate(ds, dp->index, supported, state);
11610d8bcdd3SFlorian Fainelli }
11620d8bcdd3SFlorian Fainelli 
1163aab9c406SFlorian Fainelli static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1164aab9c406SFlorian Fainelli 					    struct phylink_link_state *state)
1165ce31b31cSFlorian Fainelli {
1166aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1167aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1168ce31b31cSFlorian Fainelli 
1169aab9c406SFlorian Fainelli 	/* Only called for SGMII and 802.3z */
1170aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_link_state)
1171aab9c406SFlorian Fainelli 		return -EOPNOTSUPP;
1172aab9c406SFlorian Fainelli 
1173aab9c406SFlorian Fainelli 	return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1174b71be352SAndrew Lunn }
1175ce31b31cSFlorian Fainelli 
1176aab9c406SFlorian Fainelli static void dsa_slave_phylink_mac_config(struct net_device *dev,
1177aab9c406SFlorian Fainelli 					 unsigned int mode,
1178aab9c406SFlorian Fainelli 					 const struct phylink_link_state *state)
1179aab9c406SFlorian Fainelli {
1180aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1181aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1182aab9c406SFlorian Fainelli 
1183aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_config)
1184aab9c406SFlorian Fainelli 		return;
1185aab9c406SFlorian Fainelli 
1186aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1187aab9c406SFlorian Fainelli }
1188aab9c406SFlorian Fainelli 
1189aab9c406SFlorian Fainelli static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1190aab9c406SFlorian Fainelli {
1191aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1192aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1193aab9c406SFlorian Fainelli 
1194aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_an_restart)
1195aab9c406SFlorian Fainelli 		return;
1196aab9c406SFlorian Fainelli 
1197aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_an_restart(ds, dp->index);
1198aab9c406SFlorian Fainelli }
1199aab9c406SFlorian Fainelli 
1200aab9c406SFlorian Fainelli static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1201aab9c406SFlorian Fainelli 					    unsigned int mode,
1202aab9c406SFlorian Fainelli 					    phy_interface_t interface)
1203aab9c406SFlorian Fainelli {
1204aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1205aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1206aab9c406SFlorian Fainelli 
1207aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_link_down) {
1208aab9c406SFlorian Fainelli 		if (ds->ops->adjust_link && dev->phydev)
1209aab9c406SFlorian Fainelli 			ds->ops->adjust_link(ds, dp->index, dev->phydev);
1210aab9c406SFlorian Fainelli 		return;
1211aab9c406SFlorian Fainelli 	}
1212aab9c406SFlorian Fainelli 
1213aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1214aab9c406SFlorian Fainelli }
1215aab9c406SFlorian Fainelli 
1216aab9c406SFlorian Fainelli static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1217aab9c406SFlorian Fainelli 					  unsigned int mode,
1218aab9c406SFlorian Fainelli 					  phy_interface_t interface,
1219aab9c406SFlorian Fainelli 					  struct phy_device *phydev)
1220aab9c406SFlorian Fainelli {
1221aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1222aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1223aab9c406SFlorian Fainelli 
1224aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_link_up) {
1225aab9c406SFlorian Fainelli 		if (ds->ops->adjust_link && dev->phydev)
1226aab9c406SFlorian Fainelli 			ds->ops->adjust_link(ds, dp->index, dev->phydev);
1227aab9c406SFlorian Fainelli 		return;
1228aab9c406SFlorian Fainelli 	}
1229aab9c406SFlorian Fainelli 
1230aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1231aab9c406SFlorian Fainelli }
1232aab9c406SFlorian Fainelli 
1233aab9c406SFlorian Fainelli static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1234aab9c406SFlorian Fainelli 	.validate = dsa_slave_phylink_validate,
1235aab9c406SFlorian Fainelli 	.mac_link_state = dsa_slave_phylink_mac_link_state,
1236aab9c406SFlorian Fainelli 	.mac_config = dsa_slave_phylink_mac_config,
1237aab9c406SFlorian Fainelli 	.mac_an_restart = dsa_slave_phylink_mac_an_restart,
1238aab9c406SFlorian Fainelli 	.mac_link_down = dsa_slave_phylink_mac_link_down,
1239aab9c406SFlorian Fainelli 	.mac_link_up = dsa_slave_phylink_mac_link_up,
1240aab9c406SFlorian Fainelli };
1241aab9c406SFlorian Fainelli 
1242aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1243aab9c406SFlorian Fainelli {
1244aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1245aab9c406SFlorian Fainelli 
1246aab9c406SFlorian Fainelli 	phylink_mac_change(dp->pl, up);
1247aab9c406SFlorian Fainelli }
1248aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1249aab9c406SFlorian Fainelli 
1250aab9c406SFlorian Fainelli static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1251aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1252aab9c406SFlorian Fainelli {
1253aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1254aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1255aab9c406SFlorian Fainelli 
1256aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1257aab9c406SFlorian Fainelli 	 * not be called if it was not.
1258aab9c406SFlorian Fainelli 	 */
1259aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1260ce31b31cSFlorian Fainelli }
1261ce31b31cSFlorian Fainelli 
126291da11f8SLennert Buytenhek /* slave device setup *******************************************************/
12634fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1264c305c165SFlorian Fainelli {
1265d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1266d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1267c305c165SFlorian Fainelli 
12680115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
12690115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1270d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1271c305c165SFlorian Fainelli 		return -ENODEV;
1272d25b8e74SRussell King 	}
1273c305c165SFlorian Fainelli 
1274aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1275c305c165SFlorian Fainelli }
1276c305c165SFlorian Fainelli 
12774fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
12780d8bcdd3SFlorian Fainelli {
1279d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1280d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1281d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
12826819563eSFlorian Fainelli 	u32 phy_flags = 0;
128319334920SGuenter Roeck 	int mode, ret;
12840d8bcdd3SFlorian Fainelli 
128519334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
128619334920SGuenter Roeck 	if (mode < 0)
128719334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
12880d8bcdd3SFlorian Fainelli 
1289aab9c406SFlorian Fainelli 	dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1290aab9c406SFlorian Fainelli 				&dsa_slave_phylink_mac_ops);
1291aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1292aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1293aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1294aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1295aab9c406SFlorian Fainelli 	}
1296aab9c406SFlorian Fainelli 
1297aab9c406SFlorian Fainelli 	/* Register only if the switch provides such a callback, since this
1298aab9c406SFlorian Fainelli 	 * callback takes precedence over polling the link GPIO in PHYLINK
1299aab9c406SFlorian Fainelli 	 * (see phylink_get_fixed_state).
13000d8bcdd3SFlorian Fainelli 	 */
1301aab9c406SFlorian Fainelli 	if (ds->ops->phylink_fixed_state)
1302aab9c406SFlorian Fainelli 		phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
13030d8bcdd3SFlorian Fainelli 
13049d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1305d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
13066819563eSFlorian Fainelli 
1307aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
13086146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
13096146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
13106146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
13110d8bcdd3SFlorian Fainelli 		 */
1312d945097bSVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1313d25b8e74SRussell King 		if (ret) {
1314aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1315aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1316d945097bSVivien Didelot 				   dp->index, ret);
1317aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1318c305c165SFlorian Fainelli 			return ret;
1319d25b8e74SRussell King 		}
13200d8bcdd3SFlorian Fainelli 	}
13219697f1cdSFlorian Fainelli 
13226146dd45SVladimir Oltean 	return ret;
1323b31f65fbSAndrew Lunn }
13240d8bcdd3SFlorian Fainelli 
1325448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1326448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1327448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1328448b4482SAndrew Lunn 					    void *_unused)
1329448b4482SAndrew Lunn {
1330448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1331448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1332448b4482SAndrew Lunn }
1333448b4482SAndrew Lunn 
133424462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
133524462549SFlorian Fainelli {
1336aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
133724462549SFlorian Fainelli 
1338a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1339a94c689eSFlorian Fainelli 		return 0;
1340a94c689eSFlorian Fainelli 
1341f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1342f154be24SFlorian Fainelli 
1343aab9c406SFlorian Fainelli 	rtnl_lock();
1344aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1345aab9c406SFlorian Fainelli 	rtnl_unlock();
134624462549SFlorian Fainelli 
134724462549SFlorian Fainelli 	return 0;
134824462549SFlorian Fainelli }
134924462549SFlorian Fainelli 
135024462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
135124462549SFlorian Fainelli {
1352aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1353aab9c406SFlorian Fainelli 
1354a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1355a94c689eSFlorian Fainelli 		return 0;
1356a94c689eSFlorian Fainelli 
135724462549SFlorian Fainelli 	netif_device_attach(slave_dev);
135824462549SFlorian Fainelli 
1359aab9c406SFlorian Fainelli 	rtnl_lock();
1360aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1361aab9c406SFlorian Fainelli 	rtnl_unlock();
136224462549SFlorian Fainelli 
136324462549SFlorian Fainelli 	return 0;
136424462549SFlorian Fainelli }
136524462549SFlorian Fainelli 
13666158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val)
13676158eaa7SVivien Didelot {
1368d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
1369d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
13706158eaa7SVivien Didelot 	struct dsa_notifier_register_info rinfo = {
13716158eaa7SVivien Didelot 		.switch_number = dp->ds->index,
13726158eaa7SVivien Didelot 		.port_number = dp->index,
13736158eaa7SVivien Didelot 		.master = master,
13746158eaa7SVivien Didelot 		.info.dev = dev,
13756158eaa7SVivien Didelot 	};
13766158eaa7SVivien Didelot 
13776158eaa7SVivien Didelot 	call_dsa_notifiers(val, dev, &rinfo.info);
13786158eaa7SVivien Didelot }
13796158eaa7SVivien Didelot 
1380951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
138191da11f8SLennert Buytenhek {
138224a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1383f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
13844cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1385951259aaSVivien Didelot 	const char *name = port->name;
138691da11f8SLennert Buytenhek 	struct net_device *slave_dev;
138791da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
138891da11f8SLennert Buytenhek 	int ret;
138991da11f8SLennert Buytenhek 
139055199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
139155199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
139255199df6SFlorian Fainelli 
139355199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
139455199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
139555199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
139691da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1397d87d6f44SGuenter Roeck 		return -ENOMEM;
139891da11f8SLennert Buytenhek 
1399061f6a50SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC |
1400061f6a50SFlorian Fainelli 				NETIF_F_HW_VLAN_CTAG_FILTER;
1401f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
14027ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
1403a2c7023fSXiaofei Shen 	if (port->mac && is_valid_ether_addr(port->mac))
1404a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1405a2c7023fSXiaofei Shen 	else
14062fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
14070a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
14083e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
14098b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
14108b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1411f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1412d442ad4aSStephen Hemminger 
1413448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1414448b4482SAndrew Lunn 				 NULL);
1415448b4482SAndrew Lunn 
14164cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
14174cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
141891da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
141991da11f8SLennert Buytenhek 
142091da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
14215f6b4e14SFlorian Fainelli 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
14225f6b4e14SFlorian Fainelli 	if (!p->stats64) {
14235f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
14245f6b4e14SFlorian Fainelli 		return -ENOMEM;
14255f6b4e14SFlorian Fainelli 	}
14264cfbf09cSVivien Didelot 	p->dp = port;
1427f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
142815240248SVivien Didelot 	p->xmit = cpu_dp->tag_ops->xmit;
1429f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
143091da11f8SLennert Buytenhek 
143191da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
143291da11f8SLennert Buytenhek 
14334fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
14340071f56eSAndrew Lunn 	if (ret) {
14350071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
1436e804441cSFlorian Fainelli 		goto out_free;
1437e804441cSFlorian Fainelli 	}
1438e804441cSFlorian Fainelli 
14396158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
144060724d4bSFlorian Fainelli 
1441e804441cSFlorian Fainelli 	ret = register_netdev(slave_dev);
1442e804441cSFlorian Fainelli 	if (ret) {
1443e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1444e804441cSFlorian Fainelli 			   ret, slave_dev->name);
1445e804441cSFlorian Fainelli 		goto out_phy;
14460071f56eSAndrew Lunn 	}
14470071f56eSAndrew Lunn 
1448d87d6f44SGuenter Roeck 	return 0;
1449e804441cSFlorian Fainelli 
1450e804441cSFlorian Fainelli out_phy:
1451aab9c406SFlorian Fainelli 	rtnl_lock();
1452aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1453aab9c406SFlorian Fainelli 	rtnl_unlock();
1454aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1455e804441cSFlorian Fainelli out_free:
1456e804441cSFlorian Fainelli 	free_percpu(p->stats64);
1457e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1458f8b8b1cdSVivien Didelot 	port->slave = NULL;
1459e804441cSFlorian Fainelli 	return ret;
146091da11f8SLennert Buytenhek }
1461b73adef6SFlorian Fainelli 
1462cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1463cda5c15bSNeil Armstrong {
1464d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1465cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1466cda5c15bSNeil Armstrong 
1467cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1468aab9c406SFlorian Fainelli 	rtnl_lock();
1469aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
1470aab9c406SFlorian Fainelli 	rtnl_unlock();
1471881eadabSJohan Hovold 
14726158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1473cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1474aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
14755f6b4e14SFlorian Fainelli 	free_percpu(p->stats64);
1476cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1477cda5c15bSNeil Armstrong }
1478cda5c15bSNeil Armstrong 
1479b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1480b73adef6SFlorian Fainelli {
1481b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1482b73adef6SFlorian Fainelli }
1483b73adef6SFlorian Fainelli 
14848e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
14858e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1486b73adef6SFlorian Fainelli {
1487d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
14888e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1489b73adef6SFlorian Fainelli 
14908e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
14918e92ab3aSVivien Didelot 		if (info->linking) {
149217d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
14938e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
14948e92ab3aSVivien Didelot 		} else {
149517d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
14968e92ab3aSVivien Didelot 			err = NOTIFY_OK;
14978e92ab3aSVivien Didelot 		}
14986debb68aSVivien Didelot 	}
1499b73adef6SFlorian Fainelli 
15008e92ab3aSVivien Didelot 	return err;
1501b73adef6SFlorian Fainelli }
1502b73adef6SFlorian Fainelli 
1503cc1d5bdaSFlorian Fainelli static int dsa_slave_upper_vlan_check(struct net_device *dev,
1504cc1d5bdaSFlorian Fainelli 				      struct netdev_notifier_changeupper_info *
1505cc1d5bdaSFlorian Fainelli 				      info)
1506cc1d5bdaSFlorian Fainelli {
1507cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
1508cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
1509cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
1510cc1d5bdaSFlorian Fainelli 
1511cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
1512cc1d5bdaSFlorian Fainelli 
1513cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
1514cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1515cc1d5bdaSFlorian Fainelli 
1516cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
1517cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
1518cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1519cc1d5bdaSFlorian Fainelli 
1520cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
1521cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
1522cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1523cc1d5bdaSFlorian Fainelli 
1524cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
1525cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
1526cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
1527cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
1528cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
1529cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
1530cc1d5bdaSFlorian Fainelli 	}
1531cc1d5bdaSFlorian Fainelli 
1532cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
1533cc1d5bdaSFlorian Fainelli }
1534cc1d5bdaSFlorian Fainelli 
153588e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1536b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1537b73adef6SFlorian Fainelli {
15386debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1539b73adef6SFlorian Fainelli 
1540cc1d5bdaSFlorian Fainelli 	if (event == NETDEV_CHANGEUPPER) {
154153bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
1542cc1d5bdaSFlorian Fainelli 			return dsa_slave_upper_vlan_check(dev, ptr);
15438e92ab3aSVivien Didelot 
15448e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1545cc1d5bdaSFlorian Fainelli 	}
1546b73adef6SFlorian Fainelli 
1547b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1548b73adef6SFlorian Fainelli }
154988e4f0caSVivien Didelot 
15509ed1ecedSFlorian Fainelli static int
15519ed1ecedSFlorian Fainelli dsa_slave_switchdev_port_attr_set_event(struct net_device *netdev,
15529ed1ecedSFlorian Fainelli 		struct switchdev_notifier_port_attr_info *port_attr_info)
15539ed1ecedSFlorian Fainelli {
15549ed1ecedSFlorian Fainelli 	int err;
15559ed1ecedSFlorian Fainelli 
15569ed1ecedSFlorian Fainelli 	err = dsa_slave_port_attr_set(netdev, port_attr_info->attr,
15579ed1ecedSFlorian Fainelli 				      port_attr_info->trans);
15589ed1ecedSFlorian Fainelli 
15599ed1ecedSFlorian Fainelli 	port_attr_info->handled = true;
15609ed1ecedSFlorian Fainelli 	return notifier_from_errno(err);
15619ed1ecedSFlorian Fainelli }
15629ed1ecedSFlorian Fainelli 
1563c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work {
1564c9eb3e0fSArkadi Sharshevsky 	struct work_struct work;
1565c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info fdb_info;
1566c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev;
1567c9eb3e0fSArkadi Sharshevsky 	unsigned long event;
1568c9eb3e0fSArkadi Sharshevsky };
1569c9eb3e0fSArkadi Sharshevsky 
1570c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
1571c9eb3e0fSArkadi Sharshevsky {
1572c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
1573c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
1574c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_work->dev;
1575c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info *fdb_info;
1576d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1577c9eb3e0fSArkadi Sharshevsky 	int err;
1578c9eb3e0fSArkadi Sharshevsky 
1579c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
1580c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
1581c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1582c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1583a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1584a37fb855SVivien Didelot 			break;
1585a37fb855SVivien Didelot 
1586d945097bSVivien Didelot 		err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1587c9eb3e0fSArkadi Sharshevsky 		if (err) {
1588c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1589c9eb3e0fSArkadi Sharshevsky 			break;
1590c9eb3e0fSArkadi Sharshevsky 		}
1591e9ba0fbcSIdo Schimmel 		fdb_info->offloaded = true;
1592c9eb3e0fSArkadi Sharshevsky 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
15936685987cSPetr Machata 					 &fdb_info->info, NULL);
1594c9eb3e0fSArkadi Sharshevsky 		break;
1595c9eb3e0fSArkadi Sharshevsky 
1596c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1597c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1598a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1599a37fb855SVivien Didelot 			break;
1600a37fb855SVivien Didelot 
1601d945097bSVivien Didelot 		err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1602c9eb3e0fSArkadi Sharshevsky 		if (err) {
1603c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1604c9eb3e0fSArkadi Sharshevsky 			dev_close(dev);
1605c9eb3e0fSArkadi Sharshevsky 		}
1606c9eb3e0fSArkadi Sharshevsky 		break;
1607c9eb3e0fSArkadi Sharshevsky 	}
1608c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
1609c9eb3e0fSArkadi Sharshevsky 
1610c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work->fdb_info.addr);
1611c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1612c9eb3e0fSArkadi Sharshevsky 	dev_put(dev);
1613c9eb3e0fSArkadi Sharshevsky }
1614c9eb3e0fSArkadi Sharshevsky 
1615c9eb3e0fSArkadi Sharshevsky static int
1616c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1617c9eb3e0fSArkadi Sharshevsky 				  switchdev_work,
1618c9eb3e0fSArkadi Sharshevsky 				  const struct switchdev_notifier_fdb_info *
1619c9eb3e0fSArkadi Sharshevsky 				  fdb_info)
1620c9eb3e0fSArkadi Sharshevsky {
1621c9eb3e0fSArkadi Sharshevsky 	memcpy(&switchdev_work->fdb_info, fdb_info,
1622c9eb3e0fSArkadi Sharshevsky 	       sizeof(switchdev_work->fdb_info));
1623c9eb3e0fSArkadi Sharshevsky 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1624c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work->fdb_info.addr)
1625c9eb3e0fSArkadi Sharshevsky 		return -ENOMEM;
1626c9eb3e0fSArkadi Sharshevsky 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1627c9eb3e0fSArkadi Sharshevsky 			fdb_info->addr);
1628c9eb3e0fSArkadi Sharshevsky 	return 0;
1629c9eb3e0fSArkadi Sharshevsky }
1630c9eb3e0fSArkadi Sharshevsky 
1631c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
1632c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
1633c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
1634c9eb3e0fSArkadi Sharshevsky {
1635c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1636c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
1637c9eb3e0fSArkadi Sharshevsky 
1638c9eb3e0fSArkadi Sharshevsky 	if (!dsa_slave_dev_check(dev))
1639c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1640c9eb3e0fSArkadi Sharshevsky 
16419ed1ecedSFlorian Fainelli 	if (event == SWITCHDEV_PORT_ATTR_SET)
16429ed1ecedSFlorian Fainelli 		return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
16439ed1ecedSFlorian Fainelli 
1644c9eb3e0fSArkadi Sharshevsky 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1645c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work)
1646c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_BAD;
1647c9eb3e0fSArkadi Sharshevsky 
1648c9eb3e0fSArkadi Sharshevsky 	INIT_WORK(&switchdev_work->work,
1649c9eb3e0fSArkadi Sharshevsky 		  dsa_slave_switchdev_event_work);
1650c9eb3e0fSArkadi Sharshevsky 	switchdev_work->dev = dev;
1651c9eb3e0fSArkadi Sharshevsky 	switchdev_work->event = event;
1652c9eb3e0fSArkadi Sharshevsky 
1653c9eb3e0fSArkadi Sharshevsky 	switch (event) {
1654c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1655c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1656a37fb855SVivien Didelot 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1657c9eb3e0fSArkadi Sharshevsky 			goto err_fdb_work_init;
1658c9eb3e0fSArkadi Sharshevsky 		dev_hold(dev);
1659c9eb3e0fSArkadi Sharshevsky 		break;
1660c9eb3e0fSArkadi Sharshevsky 	default:
1661c9eb3e0fSArkadi Sharshevsky 		kfree(switchdev_work);
1662c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1663c9eb3e0fSArkadi Sharshevsky 	}
1664c9eb3e0fSArkadi Sharshevsky 
1665c9eb3e0fSArkadi Sharshevsky 	dsa_schedule_work(&switchdev_work->work);
1666c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
1667c9eb3e0fSArkadi Sharshevsky 
1668c9eb3e0fSArkadi Sharshevsky err_fdb_work_init:
1669c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1670c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_BAD;
1671c9eb3e0fSArkadi Sharshevsky }
1672c9eb3e0fSArkadi Sharshevsky 
16732b239f67SPetr Machata static int
16742b239f67SPetr Machata dsa_slave_switchdev_port_obj_event(unsigned long event,
16752b239f67SPetr Machata 			struct net_device *netdev,
16762b239f67SPetr Machata 			struct switchdev_notifier_port_obj_info *port_obj_info)
16772b239f67SPetr Machata {
16782b239f67SPetr Machata 	int err = -EOPNOTSUPP;
16792b239f67SPetr Machata 
16802b239f67SPetr Machata 	switch (event) {
16812b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
16822b239f67SPetr Machata 		err = dsa_slave_port_obj_add(netdev, port_obj_info->obj,
16832b239f67SPetr Machata 					     port_obj_info->trans);
16842b239f67SPetr Machata 		break;
16852b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
16862b239f67SPetr Machata 		err = dsa_slave_port_obj_del(netdev, port_obj_info->obj);
16872b239f67SPetr Machata 		break;
16882b239f67SPetr Machata 	}
16892b239f67SPetr Machata 
16902b239f67SPetr Machata 	port_obj_info->handled = true;
16912b239f67SPetr Machata 	return notifier_from_errno(err);
16922b239f67SPetr Machata }
16932b239f67SPetr Machata 
16942b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
16952b239f67SPetr Machata 					      unsigned long event, void *ptr)
16962b239f67SPetr Machata {
16972b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
16982b239f67SPetr Machata 
16992b239f67SPetr Machata 	if (!dsa_slave_dev_check(dev))
17002b239f67SPetr Machata 		return NOTIFY_DONE;
17012b239f67SPetr Machata 
17022b239f67SPetr Machata 	switch (event) {
17032b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
17042b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
17052b239f67SPetr Machata 		return dsa_slave_switchdev_port_obj_event(event, dev, ptr);
17069ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
17079ed1ecedSFlorian Fainelli 		return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
17082b239f67SPetr Machata 	}
17092b239f67SPetr Machata 
17102b239f67SPetr Machata 	return NOTIFY_DONE;
17112b239f67SPetr Machata }
17122b239f67SPetr Machata 
171388e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
171488e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
171588e4f0caSVivien Didelot };
171688e4f0caSVivien Didelot 
1717c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
1718c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
1719c9eb3e0fSArkadi Sharshevsky };
1720c9eb3e0fSArkadi Sharshevsky 
17212b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
17222b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
17232b239f67SPetr Machata };
17242b239f67SPetr Machata 
172588e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
172688e4f0caSVivien Didelot {
17272b239f67SPetr Machata 	struct notifier_block *nb;
1728c9eb3e0fSArkadi Sharshevsky 	int err;
1729c9eb3e0fSArkadi Sharshevsky 
1730c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
1731c9eb3e0fSArkadi Sharshevsky 	if (err)
1732c9eb3e0fSArkadi Sharshevsky 		return err;
1733c9eb3e0fSArkadi Sharshevsky 
1734c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1735c9eb3e0fSArkadi Sharshevsky 	if (err)
1736c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
1737c9eb3e0fSArkadi Sharshevsky 
17382b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
17392b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
17402b239f67SPetr Machata 	if (err)
17412b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
17422b239f67SPetr Machata 
1743c9eb3e0fSArkadi Sharshevsky 	return 0;
1744c9eb3e0fSArkadi Sharshevsky 
17452b239f67SPetr Machata err_switchdev_blocking_nb:
17462b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1747c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
1748c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
1749c9eb3e0fSArkadi Sharshevsky 	return err;
175088e4f0caSVivien Didelot }
175188e4f0caSVivien Didelot 
175288e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
175388e4f0caSVivien Didelot {
17542b239f67SPetr Machata 	struct notifier_block *nb;
175588e4f0caSVivien Didelot 	int err;
175688e4f0caSVivien Didelot 
17572b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
17582b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
17592b239f67SPetr Machata 	if (err)
17602b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
17612b239f67SPetr Machata 
1762c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1763c9eb3e0fSArkadi Sharshevsky 	if (err)
1764c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1765c9eb3e0fSArkadi Sharshevsky 
176688e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
176788e4f0caSVivien Didelot 	if (err)
176888e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
176988e4f0caSVivien Didelot }
1770