xref: /openbmc/linux/net/dsa/slave.c (revision 44cc27e43fa3b8977373915a8e7f515a9d263343)
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 
12397a69a0dSVladimir Oltean 	cancel_work_sync(&dp->xmit_work);
12497a69a0dSVladimir Oltean 	skb_queue_purge(&dp->xmit_queue);
12597a69a0dSVladimir Oltean 
126aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
127f7f1de51SFlorian Fainelli 
12875104db0SAndrew Lunn 	dsa_port_disable(dp);
1296457edfeSVivien Didelot 
130df02c6ffSLennert Buytenhek 	dev_mc_unsync(master, dev);
131a748ee24SJiri Pirko 	dev_uc_unsync(master, dev);
132df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_ALLMULTI)
133df02c6ffSLennert Buytenhek 		dev_set_allmulti(master, -1);
134df02c6ffSLennert Buytenhek 	if (dev->flags & IFF_PROMISC)
135df02c6ffSLennert Buytenhek 		dev_set_promiscuity(master, -1);
136df02c6ffSLennert Buytenhek 
1378feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
138a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
139df02c6ffSLennert Buytenhek 
14091da11f8SLennert Buytenhek 	return 0;
14191da11f8SLennert Buytenhek }
14291da11f8SLennert Buytenhek 
14391da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
14491da11f8SLennert Buytenhek {
145d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
14617ab4f61SRundong Ge 	if (dev->flags & IFF_UP) {
14791da11f8SLennert Buytenhek 		if (change & IFF_ALLMULTI)
14817ab4f61SRundong Ge 			dev_set_allmulti(master,
14917ab4f61SRundong Ge 					 dev->flags & IFF_ALLMULTI ? 1 : -1);
15091da11f8SLennert Buytenhek 		if (change & IFF_PROMISC)
15117ab4f61SRundong Ge 			dev_set_promiscuity(master,
15217ab4f61SRundong Ge 					    dev->flags & IFF_PROMISC ? 1 : -1);
15317ab4f61SRundong Ge 	}
15491da11f8SLennert Buytenhek }
15591da11f8SLennert Buytenhek 
15691da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
15791da11f8SLennert Buytenhek {
158d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
15991da11f8SLennert Buytenhek 
16091da11f8SLennert Buytenhek 	dev_mc_sync(master, dev);
161a748ee24SJiri Pirko 	dev_uc_sync(master, dev);
16291da11f8SLennert Buytenhek }
16391da11f8SLennert Buytenhek 
164df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
16591da11f8SLennert Buytenhek {
166d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
167df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
168df02c6ffSLennert Buytenhek 	int err;
169df02c6ffSLennert Buytenhek 
170df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
171df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
172df02c6ffSLennert Buytenhek 
173df02c6ffSLennert Buytenhek 	if (!(dev->flags & IFF_UP))
174df02c6ffSLennert Buytenhek 		goto out;
175df02c6ffSLennert Buytenhek 
1768feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
177a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
178df02c6ffSLennert Buytenhek 		if (err < 0)
179df02c6ffSLennert Buytenhek 			return err;
180df02c6ffSLennert Buytenhek 	}
181df02c6ffSLennert Buytenhek 
1828feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
183a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
184df02c6ffSLennert Buytenhek 
185df02c6ffSLennert Buytenhek out:
186d08f161aSJoe Perches 	ether_addr_copy(dev->dev_addr, addr->sa_data);
18791da11f8SLennert Buytenhek 
18891da11f8SLennert Buytenhek 	return 0;
18991da11f8SLennert Buytenhek }
19091da11f8SLennert Buytenhek 
1912bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
1922bedde1aSArkadi Sharshevsky 	struct net_device *dev;
1932bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
1942bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
1952bedde1aSArkadi Sharshevsky 	int idx;
1962bedde1aSArkadi Sharshevsky };
1972bedde1aSArkadi Sharshevsky 
1982bedde1aSArkadi Sharshevsky static int
1992bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
2002bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
2012bedde1aSArkadi Sharshevsky {
2022bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
2032bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
2042bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
2052bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
2062bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
2072bedde1aSArkadi Sharshevsky 
2082bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
2092bedde1aSArkadi Sharshevsky 		goto skip;
2102bedde1aSArkadi Sharshevsky 
2112bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
2122bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
2132bedde1aSArkadi Sharshevsky 	if (!nlh)
2142bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
2152bedde1aSArkadi Sharshevsky 
2162bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
2172bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
2182bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
2192bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
2202bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
2212bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
2222bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
2232bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
2242bedde1aSArkadi Sharshevsky 
2252bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
2262bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2272bedde1aSArkadi Sharshevsky 
2282bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
2292bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
2302bedde1aSArkadi Sharshevsky 
2312bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
2322bedde1aSArkadi Sharshevsky 
2332bedde1aSArkadi Sharshevsky skip:
2342bedde1aSArkadi Sharshevsky 	dump->idx++;
2352bedde1aSArkadi Sharshevsky 	return 0;
2362bedde1aSArkadi Sharshevsky 
2372bedde1aSArkadi Sharshevsky nla_put_failure:
2382bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
2392bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
2402bedde1aSArkadi Sharshevsky }
2412bedde1aSArkadi Sharshevsky 
2422bedde1aSArkadi Sharshevsky static int
2432bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
2442bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
2452bedde1aSArkadi Sharshevsky 		   int *idx)
2462bedde1aSArkadi Sharshevsky {
247d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
2482bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
2492bedde1aSArkadi Sharshevsky 		.dev = dev,
2502bedde1aSArkadi Sharshevsky 		.skb = skb,
2512bedde1aSArkadi Sharshevsky 		.cb = cb,
2522bedde1aSArkadi Sharshevsky 		.idx = *idx,
2532bedde1aSArkadi Sharshevsky 	};
2542bedde1aSArkadi Sharshevsky 	int err;
2552bedde1aSArkadi Sharshevsky 
256de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
2572bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
258de40fc5dSVivien Didelot 
2592bedde1aSArkadi Sharshevsky 	return err;
2602bedde1aSArkadi Sharshevsky }
2612bedde1aSArkadi Sharshevsky 
26291da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
26391da11f8SLennert Buytenhek {
2640336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
2650336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
2660336369dSBrandon Streiff 	int port = p->dp->index;
2670336369dSBrandon Streiff 
2680336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
2690336369dSBrandon Streiff 	switch (cmd) {
2700336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
2710336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
2720336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
2730336369dSBrandon Streiff 		break;
2740336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
2750336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
2760336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
2770336369dSBrandon Streiff 		break;
2780336369dSBrandon Streiff 	}
2790336369dSBrandon Streiff 
280aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
28191da11f8SLennert Buytenhek }
28291da11f8SLennert Buytenhek 
28335636062SScott Feldman static int dsa_slave_port_attr_set(struct net_device *dev,
284f7fadf30SJiri Pirko 				   const struct switchdev_attr *attr,
2857ea6eb3fSJiri Pirko 				   struct switchdev_trans *trans)
28635636062SScott Feldman {
287d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
288b8d866acSVivien Didelot 	int ret;
28935636062SScott Feldman 
29035636062SScott Feldman 	switch (attr->id) {
2911f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
292fd364541SVivien Didelot 		ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
29335636062SScott Feldman 		break;
294fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
295c02c4175SVivien Didelot 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
296c02c4175SVivien Didelot 					      trans);
297fb2dabadSVivien Didelot 		break;
29834a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
299072bb190SVivien Didelot 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
30034a79f63SVivien Didelot 		break;
301ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
302ea87005aSFlorian Fainelli 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
303ea87005aSFlorian Fainelli 						trans);
304ea87005aSFlorian Fainelli 		break;
30557652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
30657652796SRussell King 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, trans);
30757652796SRussell King 		break;
30835636062SScott Feldman 	default:
30935636062SScott Feldman 		ret = -EOPNOTSUPP;
31035636062SScott Feldman 		break;
31135636062SScott Feldman 	}
31235636062SScott Feldman 
31335636062SScott Feldman 	return ret;
31435636062SScott Feldman }
31535636062SScott Feldman 
316ba14d9ebSVivien Didelot static int dsa_slave_port_obj_add(struct net_device *dev,
317648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
3187ea6eb3fSJiri Pirko 				  struct switchdev_trans *trans)
319ba14d9ebSVivien Didelot {
320d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
321ba14d9ebSVivien Didelot 	int err;
322ba14d9ebSVivien Didelot 
323ba14d9ebSVivien Didelot 	/* For the prepare phase, ensure the full set of changes is feasable in
324ba14d9ebSVivien Didelot 	 * one go in order to signal a failure properly. If an operation is not
325ba14d9ebSVivien Didelot 	 * supported, return -EOPNOTSUPP.
326ba14d9ebSVivien Didelot 	 */
327ba14d9ebSVivien Didelot 
3289e8f4a54SJiri Pirko 	switch (obj->id) {
3298df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
330bcebb976SVivien Didelot 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
3318df30255SVivien Didelot 		break;
3325f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
3335f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
3345f4dbc50SAndrew Lunn 		 * but on the CPU port.
3355f4dbc50SAndrew Lunn 		 */
3365f4dbc50SAndrew Lunn 		err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
3375f4dbc50SAndrew Lunn 				       trans);
3385f4dbc50SAndrew Lunn 		break;
33957d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
34001676d12SVivien Didelot 		err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
341648b4a99SJiri Pirko 					trans);
34211149536SVivien Didelot 		break;
343ba14d9ebSVivien Didelot 	default:
344ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
345ba14d9ebSVivien Didelot 		break;
346ba14d9ebSVivien Didelot 	}
347ba14d9ebSVivien Didelot 
348ba14d9ebSVivien Didelot 	return err;
349ba14d9ebSVivien Didelot }
350ba14d9ebSVivien Didelot 
351ba14d9ebSVivien Didelot static int dsa_slave_port_obj_del(struct net_device *dev,
352648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
353ba14d9ebSVivien Didelot {
354d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
355ba14d9ebSVivien Didelot 	int err;
356ba14d9ebSVivien Didelot 
3579e8f4a54SJiri Pirko 	switch (obj->id) {
3588df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
359bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
3608df30255SVivien Didelot 		break;
3615f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
3625f4dbc50SAndrew Lunn 		/* DSA can directly translate this to a normal MDB add,
3635f4dbc50SAndrew Lunn 		 * but on the CPU port.
3645f4dbc50SAndrew Lunn 		 */
3655f4dbc50SAndrew Lunn 		err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
3665f4dbc50SAndrew Lunn 		break;
36757d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
36801676d12SVivien Didelot 		err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
36911149536SVivien Didelot 		break;
370ba14d9ebSVivien Didelot 	default:
371ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
372ba14d9ebSVivien Didelot 		break;
373ba14d9ebSVivien Didelot 	}
374ba14d9ebSVivien Didelot 
375ba14d9ebSVivien Didelot 	return err;
376ba14d9ebSVivien Didelot }
377ba14d9ebSVivien Didelot 
378929d6c14SFlorian Fainelli static int dsa_slave_get_port_parent_id(struct net_device *dev,
379929d6c14SFlorian Fainelli 					struct netdev_phys_item_id *ppid)
380b73adef6SFlorian Fainelli {
381d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
382d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
383a42c8e33SAndrew Lunn 	struct dsa_switch_tree *dst = ds->dst;
384b73adef6SFlorian Fainelli 
38515b04aceSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
38615b04aceSJiri Pirko 	 * care of the name generation. This ndo implementation
38715b04aceSJiri Pirko 	 * should be removed with legacy support.
38815b04aceSJiri Pirko 	 */
38915b04aceSJiri Pirko 	if (dp->ds->devlink)
39015b04aceSJiri Pirko 		return -EOPNOTSUPP;
39115b04aceSJiri Pirko 
392929d6c14SFlorian Fainelli 	ppid->id_len = sizeof(dst->index);
393929d6c14SFlorian Fainelli 	memcpy(&ppid->id, &dst->index, ppid->id_len);
394929d6c14SFlorian Fainelli 
395929d6c14SFlorian Fainelli 	return 0;
396929d6c14SFlorian Fainelli }
397929d6c14SFlorian Fainelli 
3984fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
39904ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
40004ff53f9SFlorian Fainelli {
40104ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
4024fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
4034fa7b718SVivien Didelot 
40404ff53f9SFlorian Fainelli 	if (p->netpoll)
40504ff53f9SFlorian Fainelli 		netpoll_send_skb(p->netpoll, skb);
40604ff53f9SFlorian Fainelli #else
40704ff53f9SFlorian Fainelli 	BUG();
40804ff53f9SFlorian Fainelli #endif
40904ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
41004ff53f9SFlorian Fainelli }
41104ff53f9SFlorian Fainelli 
41290af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
41390af1059SBrandon Streiff 				 struct sk_buff *skb)
41490af1059SBrandon Streiff {
41590af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
41690af1059SBrandon Streiff 	struct sk_buff *clone;
41790af1059SBrandon Streiff 	unsigned int type;
41890af1059SBrandon Streiff 
41990af1059SBrandon Streiff 	type = ptp_classify_raw(skb);
42090af1059SBrandon Streiff 	if (type == PTP_CLASS_NONE)
42190af1059SBrandon Streiff 		return;
42290af1059SBrandon Streiff 
42390af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
42490af1059SBrandon Streiff 		return;
42590af1059SBrandon Streiff 
42690af1059SBrandon Streiff 	clone = skb_clone_sk(skb);
42790af1059SBrandon Streiff 	if (!clone)
42890af1059SBrandon Streiff 		return;
42990af1059SBrandon Streiff 
43090af1059SBrandon Streiff 	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
43190af1059SBrandon Streiff 		return;
43290af1059SBrandon Streiff 
43390af1059SBrandon Streiff 	kfree_skb(clone);
43490af1059SBrandon Streiff }
43590af1059SBrandon Streiff 
43697a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
43797a69a0dSVladimir Oltean {
43897a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
43997a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
44097a69a0dSVladimir Oltean 	 */
44197a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
44297a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
44397a69a0dSVladimir Oltean 
44497a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
44597a69a0dSVladimir Oltean 	 * do not modify its EtherType
44697a69a0dSVladimir Oltean 	 */
44797a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
44897a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
44997a69a0dSVladimir Oltean 
45097a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
45197a69a0dSVladimir Oltean }
45297a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
45397a69a0dSVladimir Oltean 
4543e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
4553e8a72d1SFlorian Fainelli {
4563e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
4575f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
4584ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
4593e8a72d1SFlorian Fainelli 
4605f6b4e14SFlorian Fainelli 	s = this_cpu_ptr(p->stats64);
4615f6b4e14SFlorian Fainelli 	u64_stats_update_begin(&s->syncp);
4625f6b4e14SFlorian Fainelli 	s->tx_packets++;
4635f6b4e14SFlorian Fainelli 	s->tx_bytes += skb->len;
4645f6b4e14SFlorian Fainelli 	u64_stats_update_end(&s->syncp);
4653e8a72d1SFlorian Fainelli 
46687671375SVladimir Oltean 	DSA_SKB_CB(skb)->deferred_xmit = false;
46787671375SVladimir Oltean 
46890af1059SBrandon Streiff 	/* Identify PTP protocol packets, clone them, and pass them to the
46990af1059SBrandon Streiff 	 * switch driver
47090af1059SBrandon Streiff 	 */
47190af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
47290af1059SBrandon Streiff 
473fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
474fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
475fe47d563SVivien Didelot 	 */
4764ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
477fe47d563SVivien Didelot 	if (!nskb) {
47897a69a0dSVladimir Oltean 		if (!DSA_SKB_CB(skb)->deferred_xmit)
479fe47d563SVivien Didelot 			kfree_skb(skb);
4804ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
481fe47d563SVivien Didelot 	}
4825aed85ceSFlorian Fainelli 
48397a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
48497a69a0dSVladimir Oltean }
48504ff53f9SFlorian Fainelli 
48697a69a0dSVladimir Oltean void *dsa_defer_xmit(struct sk_buff *skb, struct net_device *dev)
48797a69a0dSVladimir Oltean {
48897a69a0dSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
4895aed85ceSFlorian Fainelli 
49097a69a0dSVladimir Oltean 	DSA_SKB_CB(skb)->deferred_xmit = true;
49197a69a0dSVladimir Oltean 
49297a69a0dSVladimir Oltean 	skb_queue_tail(&dp->xmit_queue, skb);
49397a69a0dSVladimir Oltean 	schedule_work(&dp->xmit_work);
49497a69a0dSVladimir Oltean 	return NULL;
49597a69a0dSVladimir Oltean }
49697a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_defer_xmit);
49797a69a0dSVladimir Oltean 
49897a69a0dSVladimir Oltean static void dsa_port_xmit_work(struct work_struct *work)
49997a69a0dSVladimir Oltean {
50097a69a0dSVladimir Oltean 	struct dsa_port *dp = container_of(work, struct dsa_port, xmit_work);
50197a69a0dSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
50297a69a0dSVladimir Oltean 	struct sk_buff *skb;
50397a69a0dSVladimir Oltean 
50497a69a0dSVladimir Oltean 	if (unlikely(!ds->ops->port_deferred_xmit))
50597a69a0dSVladimir Oltean 		return;
50697a69a0dSVladimir Oltean 
50797a69a0dSVladimir Oltean 	while ((skb = skb_dequeue(&dp->xmit_queue)) != NULL)
50897a69a0dSVladimir Oltean 		ds->ops->port_deferred_xmit(ds, dp->index, skb);
5095aed85ceSFlorian Fainelli }
5105aed85ceSFlorian Fainelli 
51191da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
51291da11f8SLennert Buytenhek 
51391da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
51491da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
51591da11f8SLennert Buytenhek {
5167826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
5177826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
5187826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
51991da11f8SLennert Buytenhek }
52091da11f8SLennert Buytenhek 
5213d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
5223d762a0fSGuenter Roeck {
523d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
524d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5253d762a0fSGuenter Roeck 
5269d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
527d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
5283d762a0fSGuenter Roeck 
5293d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
5303d762a0fSGuenter Roeck }
5313d762a0fSGuenter Roeck 
5323d762a0fSGuenter Roeck static void
5333d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
5343d762a0fSGuenter Roeck {
535d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
536d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5373d762a0fSGuenter Roeck 
5389d490b4eSVivien Didelot 	if (ds->ops->get_regs)
539d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
5403d762a0fSGuenter Roeck }
5413d762a0fSGuenter Roeck 
542aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
543aab9c406SFlorian Fainelli {
544aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
545aab9c406SFlorian Fainelli 
546aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
547aab9c406SFlorian Fainelli }
548aab9c406SFlorian Fainelli 
5496793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
5506793abb4SGuenter Roeck {
551d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
552d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5536793abb4SGuenter Roeck 
5540e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
555ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
5566793abb4SGuenter Roeck 
5579d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
5589d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
5596793abb4SGuenter Roeck 
5606793abb4SGuenter Roeck 	return 0;
5616793abb4SGuenter Roeck }
5626793abb4SGuenter Roeck 
5636793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
5646793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5656793abb4SGuenter Roeck {
566d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
567d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5686793abb4SGuenter Roeck 
5699d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
5709d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
5716793abb4SGuenter Roeck 
5726793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5736793abb4SGuenter Roeck }
5746793abb4SGuenter Roeck 
5756793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
5766793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
5776793abb4SGuenter Roeck {
578d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
579d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
5806793abb4SGuenter Roeck 
5819d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
5829d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
5836793abb4SGuenter Roeck 
5846793abb4SGuenter Roeck 	return -EOPNOTSUPP;
5856793abb4SGuenter Roeck }
5866793abb4SGuenter Roeck 
58791da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
58891da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
58991da11f8SLennert Buytenhek {
590d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
591d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
59291da11f8SLennert Buytenhek 
59391da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
59491da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
59591da11f8SLennert Buytenhek 
59691da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
59791da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
59891da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
59991da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
6009d490b4eSVivien Didelot 		if (ds->ops->get_strings)
60189f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
60289f09048SFlorian Fainelli 					     data + 4 * len);
60391da11f8SLennert Buytenhek 	}
60491da11f8SLennert Buytenhek }
60591da11f8SLennert Buytenhek 
60691da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
60791da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
60891da11f8SLennert Buytenhek 					uint64_t *data)
60991da11f8SLennert Buytenhek {
610d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
61191da11f8SLennert Buytenhek 	struct dsa_slave_priv *p = netdev_priv(dev);
612d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6135f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
614f613ed66SFlorian Fainelli 	unsigned int start;
6155f6b4e14SFlorian Fainelli 	int i;
61691da11f8SLennert Buytenhek 
6175f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
6185f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
6195f6b4e14SFlorian Fainelli 
6205f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
621f613ed66SFlorian Fainelli 		do {
6225f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
6235f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
6245f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
6255f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
6265f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
6275f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
6285f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
6295f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
6305f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
6315f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
6325f6b4e14SFlorian Fainelli 	}
6339d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
634d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
63591da11f8SLennert Buytenhek }
63691da11f8SLennert Buytenhek 
63791da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
63891da11f8SLennert Buytenhek {
639d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
640d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
64191da11f8SLennert Buytenhek 
64291da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
64391da11f8SLennert Buytenhek 		int count;
64491da11f8SLennert Buytenhek 
64591da11f8SLennert Buytenhek 		count = 4;
6469d490b4eSVivien Didelot 		if (ds->ops->get_sset_count)
64789f09048SFlorian Fainelli 			count += ds->ops->get_sset_count(ds, dp->index, sset);
64891da11f8SLennert Buytenhek 
64991da11f8SLennert Buytenhek 		return count;
65091da11f8SLennert Buytenhek 	}
65191da11f8SLennert Buytenhek 
65291da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
65391da11f8SLennert Buytenhek }
65491da11f8SLennert Buytenhek 
65519e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
65619e57c4eSFlorian Fainelli {
657d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
658d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
65919e57c4eSFlorian Fainelli 
660aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
661aab9c406SFlorian Fainelli 
6629d490b4eSVivien Didelot 	if (ds->ops->get_wol)
663d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
66419e57c4eSFlorian Fainelli }
66519e57c4eSFlorian Fainelli 
66619e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
66719e57c4eSFlorian Fainelli {
668d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
669d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
67019e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
67119e57c4eSFlorian Fainelli 
672aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
673aab9c406SFlorian Fainelli 
6749d490b4eSVivien Didelot 	if (ds->ops->set_wol)
675d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
67619e57c4eSFlorian Fainelli 
67719e57c4eSFlorian Fainelli 	return ret;
67819e57c4eSFlorian Fainelli }
67919e57c4eSFlorian Fainelli 
6807905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
6817905288fSFlorian Fainelli {
682d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
683d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
6847905288fSFlorian Fainelli 	int ret;
6857905288fSFlorian Fainelli 
6867b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
68700670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
6887b9cc738SVivien Didelot 		return -ENODEV;
6897b9cc738SVivien Didelot 
69008f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
6917905288fSFlorian Fainelli 		return -EOPNOTSUPP;
6927905288fSFlorian Fainelli 
693d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
6947905288fSFlorian Fainelli 	if (ret)
6957905288fSFlorian Fainelli 		return ret;
6967905288fSFlorian Fainelli 
697aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
6987905288fSFlorian Fainelli }
6997905288fSFlorian Fainelli 
7007905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
7017905288fSFlorian Fainelli {
702d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
703d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
7047905288fSFlorian Fainelli 	int ret;
7057905288fSFlorian Fainelli 
7067b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
70700670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
7087b9cc738SVivien Didelot 		return -ENODEV;
7097b9cc738SVivien Didelot 
71008f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
7117905288fSFlorian Fainelli 		return -EOPNOTSUPP;
7127905288fSFlorian Fainelli 
713d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
7147905288fSFlorian Fainelli 	if (ret)
7157905288fSFlorian Fainelli 		return ret;
7167905288fSFlorian Fainelli 
717aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
718aab9c406SFlorian Fainelli }
719aab9c406SFlorian Fainelli 
720aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
721aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
722aab9c406SFlorian Fainelli {
723aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
724aab9c406SFlorian Fainelli 
725aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
726aab9c406SFlorian Fainelli }
727aab9c406SFlorian Fainelli 
728aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
729aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
730aab9c406SFlorian Fainelli {
731aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
732aab9c406SFlorian Fainelli 
733aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
7347905288fSFlorian Fainelli }
7357905288fSFlorian Fainelli 
73604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
73704ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
73804ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
73904ff53f9SFlorian Fainelli {
740d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
74104ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
74204ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
74304ff53f9SFlorian Fainelli 	int err = 0;
74404ff53f9SFlorian Fainelli 
74504ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
74604ff53f9SFlorian Fainelli 	if (!netpoll)
74704ff53f9SFlorian Fainelli 		return -ENOMEM;
74804ff53f9SFlorian Fainelli 
74904ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
75004ff53f9SFlorian Fainelli 	if (err) {
75104ff53f9SFlorian Fainelli 		kfree(netpoll);
75204ff53f9SFlorian Fainelli 		goto out;
75304ff53f9SFlorian Fainelli 	}
75404ff53f9SFlorian Fainelli 
75504ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
75604ff53f9SFlorian Fainelli out:
75704ff53f9SFlorian Fainelli 	return err;
75804ff53f9SFlorian Fainelli }
75904ff53f9SFlorian Fainelli 
76004ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
76104ff53f9SFlorian Fainelli {
76204ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
76304ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
76404ff53f9SFlorian Fainelli 
76504ff53f9SFlorian Fainelli 	if (!netpoll)
76604ff53f9SFlorian Fainelli 		return;
76704ff53f9SFlorian Fainelli 
76804ff53f9SFlorian Fainelli 	p->netpoll = NULL;
76904ff53f9SFlorian Fainelli 
770c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
77104ff53f9SFlorian Fainelli }
77204ff53f9SFlorian Fainelli 
77304ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
77404ff53f9SFlorian Fainelli {
77504ff53f9SFlorian Fainelli }
77604ff53f9SFlorian Fainelli #endif
77704ff53f9SFlorian Fainelli 
77844bb765cSFlorian Fainelli static int dsa_slave_get_phys_port_name(struct net_device *dev,
77944bb765cSFlorian Fainelli 					char *name, size_t len)
78044bb765cSFlorian Fainelli {
781d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
78244bb765cSFlorian Fainelli 
783d484210bSJiri Pirko 	/* For non-legacy ports, devlink is used and it takes
784d484210bSJiri Pirko 	 * care of the name generation. This ndo implementation
785d484210bSJiri Pirko 	 * should be removed with legacy support.
786d484210bSJiri Pirko 	 */
787d484210bSJiri Pirko 	if (dp->ds->devlink)
788d484210bSJiri Pirko 		return -EOPNOTSUPP;
789d484210bSJiri Pirko 
790d945097bSVivien Didelot 	if (snprintf(name, len, "p%d", dp->index) >= len)
79144bb765cSFlorian Fainelli 		return -EINVAL;
7923a543ef4SFlorian Fainelli 
7933a543ef4SFlorian Fainelli 	return 0;
7943a543ef4SFlorian Fainelli }
7953a543ef4SFlorian Fainelli 
796f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
7974fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
798f50f2127SFlorian Fainelli {
7994fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
800f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
801f50f2127SFlorian Fainelli 
802f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
803f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
804f50f2127SFlorian Fainelli 			return mall_tc_entry;
805f50f2127SFlorian Fainelli 
806f50f2127SFlorian Fainelli 	return NULL;
807f50f2127SFlorian Fainelli }
808f50f2127SFlorian Fainelli 
809f50f2127SFlorian Fainelli static int dsa_slave_add_cls_matchall(struct net_device *dev,
810f50f2127SFlorian Fainelli 				      struct tc_cls_matchall_offload *cls,
811f50f2127SFlorian Fainelli 				      bool ingress)
812f50f2127SFlorian Fainelli {
813d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
814f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
815f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
8165fd9fc4eSJiri Pirko 	__be16 protocol = cls->common.protocol;
817d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8189681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
819d945097bSVivien Didelot 	struct dsa_port *to_dp;
820f50f2127SFlorian Fainelli 	int err = -EOPNOTSUPP;
821f50f2127SFlorian Fainelli 
822f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
823f50f2127SFlorian Fainelli 		return err;
824f50f2127SFlorian Fainelli 
8259681e8b3SPieter Jansen van Vuuren 	if (!flow_offload_has_one_action(&cls->rule->action))
826f50f2127SFlorian Fainelli 		return err;
827f50f2127SFlorian Fainelli 
8289681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
829f50f2127SFlorian Fainelli 
8309681e8b3SPieter Jansen van Vuuren 	if (act->id == FLOW_ACTION_MIRRED && protocol == htons(ETH_P_ALL)) {
831f50f2127SFlorian Fainelli 		struct dsa_mall_mirror_tc_entry *mirror;
832f50f2127SFlorian Fainelli 
8339681e8b3SPieter Jansen van Vuuren 		if (!act->dev)
834f50f2127SFlorian Fainelli 			return -EINVAL;
835f50f2127SFlorian Fainelli 
8369681e8b3SPieter Jansen van Vuuren 		if (!dsa_slave_dev_check(act->dev))
837f50f2127SFlorian Fainelli 			return -EOPNOTSUPP;
838f50f2127SFlorian Fainelli 
839f50f2127SFlorian Fainelli 		mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
840f50f2127SFlorian Fainelli 		if (!mall_tc_entry)
841f50f2127SFlorian Fainelli 			return -ENOMEM;
842f50f2127SFlorian Fainelli 
843f50f2127SFlorian Fainelli 		mall_tc_entry->cookie = cls->cookie;
844f50f2127SFlorian Fainelli 		mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
845f50f2127SFlorian Fainelli 		mirror = &mall_tc_entry->mirror;
846f50f2127SFlorian Fainelli 
8479681e8b3SPieter Jansen van Vuuren 		to_dp = dsa_slave_to_port(act->dev);
848f50f2127SFlorian Fainelli 
849d945097bSVivien Didelot 		mirror->to_local_port = to_dp->index;
850f50f2127SFlorian Fainelli 		mirror->ingress = ingress;
851f50f2127SFlorian Fainelli 
852d945097bSVivien Didelot 		err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
853f50f2127SFlorian Fainelli 		if (err) {
854f50f2127SFlorian Fainelli 			kfree(mall_tc_entry);
855f50f2127SFlorian Fainelli 			return err;
856f50f2127SFlorian Fainelli 		}
857f50f2127SFlorian Fainelli 
858f50f2127SFlorian Fainelli 		list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
859f50f2127SFlorian Fainelli 	}
860f50f2127SFlorian Fainelli 
861f50f2127SFlorian Fainelli 	return 0;
862f50f2127SFlorian Fainelli }
863f50f2127SFlorian Fainelli 
864f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
865f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
866f50f2127SFlorian Fainelli {
867d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
868f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
869d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
870f50f2127SFlorian Fainelli 
871f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_del)
872f50f2127SFlorian Fainelli 		return;
873f50f2127SFlorian Fainelli 
8744fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
875f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
876f50f2127SFlorian Fainelli 		return;
877f50f2127SFlorian Fainelli 
878f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
879f50f2127SFlorian Fainelli 
880f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
881f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
882d945097bSVivien Didelot 		ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
883f50f2127SFlorian Fainelli 		break;
884f50f2127SFlorian Fainelli 	default:
885f50f2127SFlorian Fainelli 		WARN_ON(1);
886f50f2127SFlorian Fainelli 	}
887f50f2127SFlorian Fainelli 
888f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
889f50f2127SFlorian Fainelli }
890f50f2127SFlorian Fainelli 
8913fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
8926b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
8936b3eb752SJiri Pirko 					   bool ingress)
894f50f2127SFlorian Fainelli {
8955fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
896a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
897f50f2127SFlorian Fainelli 
8983fbae382SJiri Pirko 	switch (cls->command) {
8993fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
9005fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
9013fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
9023fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
9033fbae382SJiri Pirko 		return 0;
9043fbae382SJiri Pirko 	default:
9053fbae382SJiri Pirko 		return -EOPNOTSUPP;
9063fbae382SJiri Pirko 	}
9073fbae382SJiri Pirko }
9083fbae382SJiri Pirko 
9096b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
9106b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
9116b3eb752SJiri Pirko {
9126b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
9136b3eb752SJiri Pirko 
91444ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
91544ae12a7SJiri Pirko 		return -EOPNOTSUPP;
91644ae12a7SJiri Pirko 
9176b3eb752SJiri Pirko 	switch (type) {
9186b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
9196b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
9206b3eb752SJiri Pirko 	default:
9216b3eb752SJiri Pirko 		return -EOPNOTSUPP;
9226b3eb752SJiri Pirko 	}
9236b3eb752SJiri Pirko }
9246b3eb752SJiri Pirko 
9256b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
9266b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
9276b3eb752SJiri Pirko {
9286b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
9296b3eb752SJiri Pirko }
9306b3eb752SJiri Pirko 
9316b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
9326b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
9336b3eb752SJiri Pirko {
9346b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
9356b3eb752SJiri Pirko }
9366b3eb752SJiri Pirko 
9376b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
9386b3eb752SJiri Pirko 				    struct tc_block_offload *f)
9396b3eb752SJiri Pirko {
9406b3eb752SJiri Pirko 	tc_setup_cb_t *cb;
9416b3eb752SJiri Pirko 
9426b3eb752SJiri Pirko 	if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9436b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
9446b3eb752SJiri Pirko 	else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
9456b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
9466b3eb752SJiri Pirko 	else
9476b3eb752SJiri Pirko 		return -EOPNOTSUPP;
9486b3eb752SJiri Pirko 
9496b3eb752SJiri Pirko 	switch (f->command) {
9506b3eb752SJiri Pirko 	case TC_BLOCK_BIND:
95160513bd8SJohn Hurley 		return tcf_block_cb_register(f->block, cb, dev, dev, f->extack);
9526b3eb752SJiri Pirko 	case TC_BLOCK_UNBIND:
9536b3eb752SJiri Pirko 		tcf_block_cb_unregister(f->block, cb, dev);
9546b3eb752SJiri Pirko 		return 0;
9556b3eb752SJiri Pirko 	default:
9566b3eb752SJiri Pirko 		return -EOPNOTSUPP;
9576b3eb752SJiri Pirko 	}
9586b3eb752SJiri Pirko }
9596b3eb752SJiri Pirko 
9603fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
961de4784caSJiri Pirko 			      void *type_data)
9623fbae382SJiri Pirko {
9632572ac53SJiri Pirko 	switch (type) {
9646b3eb752SJiri Pirko 	case TC_SETUP_BLOCK:
9656b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
966f50f2127SFlorian Fainelli 	default:
967a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
968f50f2127SFlorian Fainelli 	}
969f50f2127SFlorian Fainelli }
970f50f2127SFlorian Fainelli 
971f613ed66SFlorian Fainelli static void dsa_slave_get_stats64(struct net_device *dev,
972f613ed66SFlorian Fainelli 				  struct rtnl_link_stats64 *stats)
973f613ed66SFlorian Fainelli {
974f613ed66SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
9755f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
976f613ed66SFlorian Fainelli 	unsigned int start;
9775f6b4e14SFlorian Fainelli 	int i;
978f613ed66SFlorian Fainelli 
979f613ed66SFlorian Fainelli 	netdev_stats_to_stats64(stats, &dev->stats);
9805f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
9815f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
9825f6b4e14SFlorian Fainelli 
9835f6b4e14SFlorian Fainelli 		s = per_cpu_ptr(p->stats64, i);
984f613ed66SFlorian Fainelli 		do {
9855f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
9865f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
9875f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
9885f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
9895f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
9905f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
9915f6b4e14SFlorian Fainelli 
9925f6b4e14SFlorian Fainelli 		stats->tx_packets += tx_packets;
9935f6b4e14SFlorian Fainelli 		stats->tx_bytes += tx_bytes;
9945f6b4e14SFlorian Fainelli 		stats->rx_packets += rx_packets;
9955f6b4e14SFlorian Fainelli 		stats->rx_bytes += rx_bytes;
9965f6b4e14SFlorian Fainelli 	}
997f613ed66SFlorian Fainelli }
998f613ed66SFlorian Fainelli 
999bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1000bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1001bf9f2648SFlorian Fainelli {
1002d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1003d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1004bf9f2648SFlorian Fainelli 
1005bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1006bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1007bf9f2648SFlorian Fainelli 
1008d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1009bf9f2648SFlorian Fainelli }
1010bf9f2648SFlorian Fainelli 
1011bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1012bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1013bf9f2648SFlorian Fainelli {
1014d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1015d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1016bf9f2648SFlorian Fainelli 
1017bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1018bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1019bf9f2648SFlorian Fainelli 
1020d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1021bf9f2648SFlorian Fainelli }
1022bf9f2648SFlorian Fainelli 
10230336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
10240336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
10250336369dSBrandon Streiff {
10260336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
10270336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
10280336369dSBrandon Streiff 
10290336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
10300336369dSBrandon Streiff 		return -EOPNOTSUPP;
10310336369dSBrandon Streiff 
10320336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
10330336369dSBrandon Streiff }
10340336369dSBrandon Streiff 
1035061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1036061f6a50SFlorian Fainelli 				     u16 vid)
1037061f6a50SFlorian Fainelli {
1038061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1039061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1040061f6a50SFlorian Fainelli 	int ret;
1041061f6a50SFlorian Fainelli 
1042061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1043061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1044061f6a50SFlorian Fainelli 	 */
1045061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
1046061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1047061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1048061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1049061f6a50SFlorian Fainelli 		 */
1050061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1051061f6a50SFlorian Fainelli 		if (ret == 0)
1052061f6a50SFlorian Fainelli 			return -EBUSY;
1053061f6a50SFlorian Fainelli 	}
1054061f6a50SFlorian Fainelli 
1055314f76d7SVladimir Oltean 	/* This API only allows programming tagged, non-PVID VIDs */
1056314f76d7SVladimir Oltean 	return dsa_port_vid_add(dp, vid, 0);
1057061f6a50SFlorian Fainelli }
1058061f6a50SFlorian Fainelli 
1059061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1060061f6a50SFlorian Fainelli 				      u16 vid)
1061061f6a50SFlorian Fainelli {
1062061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1063061f6a50SFlorian Fainelli 	struct bridge_vlan_info info;
1064061f6a50SFlorian Fainelli 	int ret;
1065061f6a50SFlorian Fainelli 
1066061f6a50SFlorian Fainelli 	/* Check for a possible bridge VLAN entry now since there is no
1067061f6a50SFlorian Fainelli 	 * need to emulate the switchdev prepare + commit phase.
1068061f6a50SFlorian Fainelli 	 */
1069061f6a50SFlorian Fainelli 	if (dp->bridge_dev) {
1070061f6a50SFlorian Fainelli 		/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1071061f6a50SFlorian Fainelli 		 * device, respectively the VID is not found, returning
1072061f6a50SFlorian Fainelli 		 * 0 means success, which is a failure for us here.
1073061f6a50SFlorian Fainelli 		 */
1074061f6a50SFlorian Fainelli 		ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
1075061f6a50SFlorian Fainelli 		if (ret == 0)
1076061f6a50SFlorian Fainelli 			return -EBUSY;
1077061f6a50SFlorian Fainelli 	}
1078061f6a50SFlorian Fainelli 
1079314f76d7SVladimir Oltean 	ret = dsa_port_vid_del(dp, vid);
1080061f6a50SFlorian Fainelli 	if (ret == -EOPNOTSUPP)
1081061f6a50SFlorian Fainelli 		ret = 0;
1082061f6a50SFlorian Fainelli 
1083061f6a50SFlorian Fainelli 	return ret;
1084061f6a50SFlorian Fainelli }
1085061f6a50SFlorian Fainelli 
108691da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
108791da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
10883d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
10893d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
1090aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
1091c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
10926793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
10936793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
10946793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
109591da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
109691da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
109791da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
109819e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
109919e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
11007905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
11017905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
1102aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
1103aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
1104bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
1105bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
11060336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
110791da11f8SLennert Buytenhek };
110891da11f8SLennert Buytenhek 
11092a93c1a3SFlorian Fainelli /* legacy way, bypassing the bridge *****************************************/
11102a93c1a3SFlorian Fainelli int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11112a93c1a3SFlorian Fainelli 		       struct net_device *dev,
11122a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid,
111387b0984eSPetr Machata 		       u16 flags,
111487b0984eSPetr Machata 		       struct netlink_ext_ack *extack)
11152a93c1a3SFlorian Fainelli {
11162a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
11172a93c1a3SFlorian Fainelli 
11182a93c1a3SFlorian Fainelli 	return dsa_port_fdb_add(dp, addr, vid);
11192a93c1a3SFlorian Fainelli }
11202a93c1a3SFlorian Fainelli 
11212a93c1a3SFlorian Fainelli int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
11222a93c1a3SFlorian Fainelli 		       struct net_device *dev,
11232a93c1a3SFlorian Fainelli 		       const unsigned char *addr, u16 vid)
11242a93c1a3SFlorian Fainelli {
11252a93c1a3SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
11262a93c1a3SFlorian Fainelli 
11272a93c1a3SFlorian Fainelli 	return dsa_port_fdb_del(dp, addr, vid);
11282a93c1a3SFlorian Fainelli }
11292a93c1a3SFlorian Fainelli 
1130716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1131716efee2SJiri Pirko {
1132716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
1133716efee2SJiri Pirko 
1134716efee2SJiri Pirko 	return dp->ds->devlink ? &dp->devlink_port : NULL;
1135716efee2SJiri Pirko }
1136716efee2SJiri Pirko 
11373e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
1138d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
1139d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
11403e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
1141d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
1142d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
1143d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
114437b8da1aSArkadi Sharshevsky 	.ndo_fdb_add		= dsa_legacy_fdb_add,
114537b8da1aSArkadi Sharshevsky 	.ndo_fdb_del		= dsa_legacy_fdb_del,
11462bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
1147d442ad4aSStephen Hemminger 	.ndo_do_ioctl		= dsa_slave_ioctl,
1148abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
114904ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
115004ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
115104ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
115204ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
115304ff53f9SFlorian Fainelli #endif
115444bb765cSFlorian Fainelli 	.ndo_get_phys_port_name	= dsa_slave_get_phys_port_name,
1155f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
1156f613ed66SFlorian Fainelli 	.ndo_get_stats64	= dsa_slave_get_stats64,
1157929d6c14SFlorian Fainelli 	.ndo_get_port_parent_id	= dsa_slave_get_port_parent_id,
1158061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
1159061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
1160716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
116198237d43SScott Feldman };
116298237d43SScott Feldman 
1163f37db85dSFlorian Fainelli static struct device_type dsa_type = {
1164f37db85dSFlorian Fainelli 	.name	= "dsa",
1165f37db85dSFlorian Fainelli };
1166f37db85dSFlorian Fainelli 
1167*44cc27e4SIoana Ciornei static void dsa_slave_phylink_validate(struct phylink_config *config,
1168aab9c406SFlorian Fainelli 				       unsigned long *supported,
1169aab9c406SFlorian Fainelli 				       struct phylink_link_state *state)
11700d8bcdd3SFlorian Fainelli {
1171*44cc27e4SIoana Ciornei 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1172d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
11730d8bcdd3SFlorian Fainelli 
1174aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_validate)
1175aab9c406SFlorian Fainelli 		return;
1176aab9c406SFlorian Fainelli 
1177aab9c406SFlorian Fainelli 	ds->ops->phylink_validate(ds, dp->index, supported, state);
11780d8bcdd3SFlorian Fainelli }
11790d8bcdd3SFlorian Fainelli 
1180*44cc27e4SIoana Ciornei static int dsa_slave_phylink_mac_link_state(struct phylink_config *config,
1181aab9c406SFlorian Fainelli 					    struct phylink_link_state *state)
1182ce31b31cSFlorian Fainelli {
1183*44cc27e4SIoana Ciornei 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1184aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1185ce31b31cSFlorian Fainelli 
1186aab9c406SFlorian Fainelli 	/* Only called for SGMII and 802.3z */
1187aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_link_state)
1188aab9c406SFlorian Fainelli 		return -EOPNOTSUPP;
1189aab9c406SFlorian Fainelli 
1190aab9c406SFlorian Fainelli 	return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1191b71be352SAndrew Lunn }
1192ce31b31cSFlorian Fainelli 
1193*44cc27e4SIoana Ciornei static void dsa_slave_phylink_mac_config(struct phylink_config *config,
1194aab9c406SFlorian Fainelli 					 unsigned int mode,
1195aab9c406SFlorian Fainelli 					 const struct phylink_link_state *state)
1196aab9c406SFlorian Fainelli {
1197*44cc27e4SIoana Ciornei 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1198aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1199aab9c406SFlorian Fainelli 
1200aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_config)
1201aab9c406SFlorian Fainelli 		return;
1202aab9c406SFlorian Fainelli 
1203aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1204aab9c406SFlorian Fainelli }
1205aab9c406SFlorian Fainelli 
1206*44cc27e4SIoana Ciornei static void dsa_slave_phylink_mac_an_restart(struct phylink_config *config)
1207aab9c406SFlorian Fainelli {
1208*44cc27e4SIoana Ciornei 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1209aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1210aab9c406SFlorian Fainelli 
1211aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_an_restart)
1212aab9c406SFlorian Fainelli 		return;
1213aab9c406SFlorian Fainelli 
1214aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_an_restart(ds, dp->index);
1215aab9c406SFlorian Fainelli }
1216aab9c406SFlorian Fainelli 
1217*44cc27e4SIoana Ciornei static void dsa_slave_phylink_mac_link_down(struct phylink_config *config,
1218aab9c406SFlorian Fainelli 					    unsigned int mode,
1219aab9c406SFlorian Fainelli 					    phy_interface_t interface)
1220aab9c406SFlorian Fainelli {
1221*44cc27e4SIoana Ciornei 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1222*44cc27e4SIoana Ciornei 	struct net_device *dev = dp->slave;
1223aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1224aab9c406SFlorian Fainelli 
1225aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_link_down) {
1226aab9c406SFlorian Fainelli 		if (ds->ops->adjust_link && dev->phydev)
1227aab9c406SFlorian Fainelli 			ds->ops->adjust_link(ds, dp->index, dev->phydev);
1228aab9c406SFlorian Fainelli 		return;
1229aab9c406SFlorian Fainelli 	}
1230aab9c406SFlorian Fainelli 
1231aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1232aab9c406SFlorian Fainelli }
1233aab9c406SFlorian Fainelli 
1234*44cc27e4SIoana Ciornei static void dsa_slave_phylink_mac_link_up(struct phylink_config *config,
1235aab9c406SFlorian Fainelli 					  unsigned int mode,
1236aab9c406SFlorian Fainelli 					  phy_interface_t interface,
1237aab9c406SFlorian Fainelli 					  struct phy_device *phydev)
1238aab9c406SFlorian Fainelli {
1239*44cc27e4SIoana Ciornei 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1240*44cc27e4SIoana Ciornei 	struct net_device *dev = dp->slave;
1241aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1242aab9c406SFlorian Fainelli 
1243aab9c406SFlorian Fainelli 	if (!ds->ops->phylink_mac_link_up) {
1244aab9c406SFlorian Fainelli 		if (ds->ops->adjust_link && dev->phydev)
1245aab9c406SFlorian Fainelli 			ds->ops->adjust_link(ds, dp->index, dev->phydev);
1246aab9c406SFlorian Fainelli 		return;
1247aab9c406SFlorian Fainelli 	}
1248aab9c406SFlorian Fainelli 
1249aab9c406SFlorian Fainelli 	ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1250aab9c406SFlorian Fainelli }
1251aab9c406SFlorian Fainelli 
1252aab9c406SFlorian Fainelli static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1253aab9c406SFlorian Fainelli 	.validate = dsa_slave_phylink_validate,
1254aab9c406SFlorian Fainelli 	.mac_link_state = dsa_slave_phylink_mac_link_state,
1255aab9c406SFlorian Fainelli 	.mac_config = dsa_slave_phylink_mac_config,
1256aab9c406SFlorian Fainelli 	.mac_an_restart = dsa_slave_phylink_mac_an_restart,
1257aab9c406SFlorian Fainelli 	.mac_link_down = dsa_slave_phylink_mac_link_down,
1258aab9c406SFlorian Fainelli 	.mac_link_up = dsa_slave_phylink_mac_link_up,
1259aab9c406SFlorian Fainelli };
1260aab9c406SFlorian Fainelli 
1261aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1262aab9c406SFlorian Fainelli {
1263aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
1264aab9c406SFlorian Fainelli 
1265aab9c406SFlorian Fainelli 	phylink_mac_change(dp->pl, up);
1266aab9c406SFlorian Fainelli }
1267aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1268aab9c406SFlorian Fainelli 
1269aab9c406SFlorian Fainelli static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1270aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
1271aab9c406SFlorian Fainelli {
1272aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1273aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
1274aab9c406SFlorian Fainelli 
1275aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
1276aab9c406SFlorian Fainelli 	 * not be called if it was not.
1277aab9c406SFlorian Fainelli 	 */
1278aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
1279ce31b31cSFlorian Fainelli }
1280ce31b31cSFlorian Fainelli 
128191da11f8SLennert Buytenhek /* slave device setup *******************************************************/
12824fa7b718SVivien Didelot static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1283c305c165SFlorian Fainelli {
1284d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1285d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1286c305c165SFlorian Fainelli 
12870115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
12880115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
1289d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
1290c305c165SFlorian Fainelli 		return -ENODEV;
1291d25b8e74SRussell King 	}
1292c305c165SFlorian Fainelli 
1293aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
1294c305c165SFlorian Fainelli }
1295c305c165SFlorian Fainelli 
12964fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
12970d8bcdd3SFlorian Fainelli {
1298d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1299d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
1300d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
13016819563eSFlorian Fainelli 	u32 phy_flags = 0;
130219334920SGuenter Roeck 	int mode, ret;
13030d8bcdd3SFlorian Fainelli 
130419334920SGuenter Roeck 	mode = of_get_phy_mode(port_dn);
130519334920SGuenter Roeck 	if (mode < 0)
130619334920SGuenter Roeck 		mode = PHY_INTERFACE_MODE_NA;
13070d8bcdd3SFlorian Fainelli 
1308*44cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
1309*44cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
1310*44cc27e4SIoana Ciornei 
1311*44cc27e4SIoana Ciornei 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
1312aab9c406SFlorian Fainelli 				&dsa_slave_phylink_mac_ops);
1313aab9c406SFlorian Fainelli 	if (IS_ERR(dp->pl)) {
1314aab9c406SFlorian Fainelli 		netdev_err(slave_dev,
1315aab9c406SFlorian Fainelli 			   "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1316aab9c406SFlorian Fainelli 		return PTR_ERR(dp->pl);
1317aab9c406SFlorian Fainelli 	}
1318aab9c406SFlorian Fainelli 
1319aab9c406SFlorian Fainelli 	/* Register only if the switch provides such a callback, since this
1320aab9c406SFlorian Fainelli 	 * callback takes precedence over polling the link GPIO in PHYLINK
1321aab9c406SFlorian Fainelli 	 * (see phylink_get_fixed_state).
13220d8bcdd3SFlorian Fainelli 	 */
1323aab9c406SFlorian Fainelli 	if (ds->ops->phylink_fixed_state)
1324aab9c406SFlorian Fainelli 		phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
13250d8bcdd3SFlorian Fainelli 
13269d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
1327d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
13286819563eSFlorian Fainelli 
1329aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
13306146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
13316146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
13326146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
13330d8bcdd3SFlorian Fainelli 		 */
1334d945097bSVivien Didelot 		ret = dsa_slave_phy_connect(slave_dev, dp->index);
1335d25b8e74SRussell King 		if (ret) {
1336aab9c406SFlorian Fainelli 			netdev_err(slave_dev,
1337aab9c406SFlorian Fainelli 				   "failed to connect to port %d: %d\n",
1338d945097bSVivien Didelot 				   dp->index, ret);
1339aab9c406SFlorian Fainelli 			phylink_destroy(dp->pl);
1340c305c165SFlorian Fainelli 			return ret;
1341d25b8e74SRussell King 		}
13420d8bcdd3SFlorian Fainelli 	}
13439697f1cdSFlorian Fainelli 
13446146dd45SVladimir Oltean 	return ret;
1345b31f65fbSAndrew Lunn }
13460d8bcdd3SFlorian Fainelli 
1347448b4482SAndrew Lunn static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1348448b4482SAndrew Lunn static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1349448b4482SAndrew Lunn 					    struct netdev_queue *txq,
1350448b4482SAndrew Lunn 					    void *_unused)
1351448b4482SAndrew Lunn {
1352448b4482SAndrew Lunn 	lockdep_set_class(&txq->_xmit_lock,
1353448b4482SAndrew Lunn 			  &dsa_slave_netdev_xmit_lock_key);
1354448b4482SAndrew Lunn }
1355448b4482SAndrew Lunn 
135624462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
135724462549SFlorian Fainelli {
1358aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
135924462549SFlorian Fainelli 
1360a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1361a94c689eSFlorian Fainelli 		return 0;
1362a94c689eSFlorian Fainelli 
136397a69a0dSVladimir Oltean 	cancel_work_sync(&dp->xmit_work);
136497a69a0dSVladimir Oltean 	skb_queue_purge(&dp->xmit_queue);
136597a69a0dSVladimir Oltean 
1366f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
1367f154be24SFlorian Fainelli 
1368aab9c406SFlorian Fainelli 	rtnl_lock();
1369aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
1370aab9c406SFlorian Fainelli 	rtnl_unlock();
137124462549SFlorian Fainelli 
137224462549SFlorian Fainelli 	return 0;
137324462549SFlorian Fainelli }
137424462549SFlorian Fainelli 
137524462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
137624462549SFlorian Fainelli {
1377aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1378aab9c406SFlorian Fainelli 
1379a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
1380a94c689eSFlorian Fainelli 		return 0;
1381a94c689eSFlorian Fainelli 
138224462549SFlorian Fainelli 	netif_device_attach(slave_dev);
138324462549SFlorian Fainelli 
1384aab9c406SFlorian Fainelli 	rtnl_lock();
1385aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
1386aab9c406SFlorian Fainelli 	rtnl_unlock();
138724462549SFlorian Fainelli 
138824462549SFlorian Fainelli 	return 0;
138924462549SFlorian Fainelli }
139024462549SFlorian Fainelli 
13916158eaa7SVivien Didelot static void dsa_slave_notify(struct net_device *dev, unsigned long val)
13926158eaa7SVivien Didelot {
1393d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
1394d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
13956158eaa7SVivien Didelot 	struct dsa_notifier_register_info rinfo = {
13966158eaa7SVivien Didelot 		.switch_number = dp->ds->index,
13976158eaa7SVivien Didelot 		.port_number = dp->index,
13986158eaa7SVivien Didelot 		.master = master,
13996158eaa7SVivien Didelot 		.info.dev = dev,
14006158eaa7SVivien Didelot 	};
14016158eaa7SVivien Didelot 
14026158eaa7SVivien Didelot 	call_dsa_notifiers(val, dev, &rinfo.info);
14036158eaa7SVivien Didelot }
14046158eaa7SVivien Didelot 
1405951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
140691da11f8SLennert Buytenhek {
140724a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
1408f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
14094cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
1410951259aaSVivien Didelot 	const char *name = port->name;
141191da11f8SLennert Buytenhek 	struct net_device *slave_dev;
141291da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
141391da11f8SLennert Buytenhek 	int ret;
141491da11f8SLennert Buytenhek 
141555199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
141655199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
141755199df6SFlorian Fainelli 
141855199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
141955199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
142055199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
142191da11f8SLennert Buytenhek 	if (slave_dev == NULL)
1422d87d6f44SGuenter Roeck 		return -ENOMEM;
142391da11f8SLennert Buytenhek 
1424061f6a50SFlorian Fainelli 	slave_dev->features = master->vlan_features | NETIF_F_HW_TC |
1425061f6a50SFlorian Fainelli 				NETIF_F_HW_VLAN_CTAG_FILTER;
1426f50f2127SFlorian Fainelli 	slave_dev->hw_features |= NETIF_F_HW_TC;
14277ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
14284974f9b7SPetr Štetiar 	if (!IS_ERR_OR_NULL(port->mac))
1429a2c7023fSXiaofei Shen 		ether_addr_copy(slave_dev->dev_addr, port->mac);
1430a2c7023fSXiaofei Shen 	else
14312fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
14320a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
14333e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
14348b1efc0fSJarod Wilson 	slave_dev->min_mtu = 0;
14358b1efc0fSJarod Wilson 	slave_dev->max_mtu = ETH_MAX_MTU;
1436f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
1437d442ad4aSStephen Hemminger 
1438448b4482SAndrew Lunn 	netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1439448b4482SAndrew Lunn 				 NULL);
1440448b4482SAndrew Lunn 
14414cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
14424cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
144391da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
144491da11f8SLennert Buytenhek 
144591da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
14465f6b4e14SFlorian Fainelli 	p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
14475f6b4e14SFlorian Fainelli 	if (!p->stats64) {
14485f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
14495f6b4e14SFlorian Fainelli 		return -ENOMEM;
14505f6b4e14SFlorian Fainelli 	}
14514cfbf09cSVivien Didelot 	p->dp = port;
1452f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
145397a69a0dSVladimir Oltean 	INIT_WORK(&port->xmit_work, dsa_port_xmit_work);
145497a69a0dSVladimir Oltean 	skb_queue_head_init(&port->xmit_queue);
145515240248SVivien Didelot 	p->xmit = cpu_dp->tag_ops->xmit;
1456f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
145791da11f8SLennert Buytenhek 
145891da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
145991da11f8SLennert Buytenhek 
14604fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
14610071f56eSAndrew Lunn 	if (ret) {
14620071f56eSAndrew Lunn 		netdev_err(master, "error %d setting up slave phy\n", ret);
1463e804441cSFlorian Fainelli 		goto out_free;
1464e804441cSFlorian Fainelli 	}
1465e804441cSFlorian Fainelli 
14666158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
146760724d4bSFlorian Fainelli 
1468e804441cSFlorian Fainelli 	ret = register_netdev(slave_dev);
1469e804441cSFlorian Fainelli 	if (ret) {
1470e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
1471e804441cSFlorian Fainelli 			   ret, slave_dev->name);
1472e804441cSFlorian Fainelli 		goto out_phy;
14730071f56eSAndrew Lunn 	}
14740071f56eSAndrew Lunn 
1475d87d6f44SGuenter Roeck 	return 0;
1476e804441cSFlorian Fainelli 
1477e804441cSFlorian Fainelli out_phy:
1478aab9c406SFlorian Fainelli 	rtnl_lock();
1479aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
1480aab9c406SFlorian Fainelli 	rtnl_unlock();
1481aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
1482e804441cSFlorian Fainelli out_free:
1483e804441cSFlorian Fainelli 	free_percpu(p->stats64);
1484e804441cSFlorian Fainelli 	free_netdev(slave_dev);
1485f8b8b1cdSVivien Didelot 	port->slave = NULL;
1486e804441cSFlorian Fainelli 	return ret;
148791da11f8SLennert Buytenhek }
1488b73adef6SFlorian Fainelli 
1489cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
1490cda5c15bSNeil Armstrong {
1491d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1492cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
1493cda5c15bSNeil Armstrong 
1494cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
1495aab9c406SFlorian Fainelli 	rtnl_lock();
1496aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
1497aab9c406SFlorian Fainelli 	rtnl_unlock();
1498881eadabSJohan Hovold 
14996158eaa7SVivien Didelot 	dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1500cda5c15bSNeil Armstrong 	unregister_netdev(slave_dev);
1501aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
15025f6b4e14SFlorian Fainelli 	free_percpu(p->stats64);
1503cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
1504cda5c15bSNeil Armstrong }
1505cda5c15bSNeil Armstrong 
1506b73adef6SFlorian Fainelli static bool dsa_slave_dev_check(struct net_device *dev)
1507b73adef6SFlorian Fainelli {
1508b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
1509b73adef6SFlorian Fainelli }
1510b73adef6SFlorian Fainelli 
15118e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
15128e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
1513b73adef6SFlorian Fainelli {
1514d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
15158e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
1516b73adef6SFlorian Fainelli 
15178e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
15188e92ab3aSVivien Didelot 		if (info->linking) {
151917d7802bSVivien Didelot 			err = dsa_port_bridge_join(dp, info->upper_dev);
15208e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
15218e92ab3aSVivien Didelot 		} else {
152217d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
15238e92ab3aSVivien Didelot 			err = NOTIFY_OK;
15248e92ab3aSVivien Didelot 		}
15256debb68aSVivien Didelot 	}
1526b73adef6SFlorian Fainelli 
15278e92ab3aSVivien Didelot 	return err;
1528b73adef6SFlorian Fainelli }
1529b73adef6SFlorian Fainelli 
1530cc1d5bdaSFlorian Fainelli static int dsa_slave_upper_vlan_check(struct net_device *dev,
1531cc1d5bdaSFlorian Fainelli 				      struct netdev_notifier_changeupper_info *
1532cc1d5bdaSFlorian Fainelli 				      info)
1533cc1d5bdaSFlorian Fainelli {
1534cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
1535cc1d5bdaSFlorian Fainelli 	struct net_device *slave;
1536cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
1537cc1d5bdaSFlorian Fainelli 
1538cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
1539cc1d5bdaSFlorian Fainelli 
1540cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
1541cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1542cc1d5bdaSFlorian Fainelli 
1543cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
1544cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
1545cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1546cc1d5bdaSFlorian Fainelli 
1547cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
1548cc1d5bdaSFlorian Fainelli 	if (!dp->bridge_dev)
1549cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
1550cc1d5bdaSFlorian Fainelli 
1551cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
1552cc1d5bdaSFlorian Fainelli 	if (br_vlan_enabled(dp->bridge_dev) &&
1553cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
1554cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
1555cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
1556cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
1557cc1d5bdaSFlorian Fainelli 	}
1558cc1d5bdaSFlorian Fainelli 
1559cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
1560cc1d5bdaSFlorian Fainelli }
1561cc1d5bdaSFlorian Fainelli 
156288e4f0caSVivien Didelot static int dsa_slave_netdevice_event(struct notifier_block *nb,
1563b73adef6SFlorian Fainelli 				     unsigned long event, void *ptr)
1564b73adef6SFlorian Fainelli {
15656debb68aSVivien Didelot 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1566b73adef6SFlorian Fainelli 
1567cc1d5bdaSFlorian Fainelli 	if (event == NETDEV_CHANGEUPPER) {
156853bade8aSFlorian Fainelli 		if (!dsa_slave_dev_check(dev))
1569cc1d5bdaSFlorian Fainelli 			return dsa_slave_upper_vlan_check(dev, ptr);
15708e92ab3aSVivien Didelot 
15718e92ab3aSVivien Didelot 		return dsa_slave_changeupper(dev, ptr);
1572cc1d5bdaSFlorian Fainelli 	}
1573b73adef6SFlorian Fainelli 
1574b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
1575b73adef6SFlorian Fainelli }
157688e4f0caSVivien Didelot 
15779ed1ecedSFlorian Fainelli static int
15789ed1ecedSFlorian Fainelli dsa_slave_switchdev_port_attr_set_event(struct net_device *netdev,
15799ed1ecedSFlorian Fainelli 		struct switchdev_notifier_port_attr_info *port_attr_info)
15809ed1ecedSFlorian Fainelli {
15819ed1ecedSFlorian Fainelli 	int err;
15829ed1ecedSFlorian Fainelli 
15839ed1ecedSFlorian Fainelli 	err = dsa_slave_port_attr_set(netdev, port_attr_info->attr,
15849ed1ecedSFlorian Fainelli 				      port_attr_info->trans);
15859ed1ecedSFlorian Fainelli 
15869ed1ecedSFlorian Fainelli 	port_attr_info->handled = true;
15879ed1ecedSFlorian Fainelli 	return notifier_from_errno(err);
15889ed1ecedSFlorian Fainelli }
15899ed1ecedSFlorian Fainelli 
1590c9eb3e0fSArkadi Sharshevsky struct dsa_switchdev_event_work {
1591c9eb3e0fSArkadi Sharshevsky 	struct work_struct work;
1592c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info fdb_info;
1593c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev;
1594c9eb3e0fSArkadi Sharshevsky 	unsigned long event;
1595c9eb3e0fSArkadi Sharshevsky };
1596c9eb3e0fSArkadi Sharshevsky 
1597c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
1598c9eb3e0fSArkadi Sharshevsky {
1599c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
1600c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
1601c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_work->dev;
1602c9eb3e0fSArkadi Sharshevsky 	struct switchdev_notifier_fdb_info *fdb_info;
1603d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1604c9eb3e0fSArkadi Sharshevsky 	int err;
1605c9eb3e0fSArkadi Sharshevsky 
1606c9eb3e0fSArkadi Sharshevsky 	rtnl_lock();
1607c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
1608c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
1609c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1610a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1611a37fb855SVivien Didelot 			break;
1612a37fb855SVivien Didelot 
1613d945097bSVivien Didelot 		err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
1614c9eb3e0fSArkadi Sharshevsky 		if (err) {
1615c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb add failed err=%d\n", err);
1616c9eb3e0fSArkadi Sharshevsky 			break;
1617c9eb3e0fSArkadi Sharshevsky 		}
1618e9ba0fbcSIdo Schimmel 		fdb_info->offloaded = true;
1619c9eb3e0fSArkadi Sharshevsky 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
16206685987cSPetr Machata 					 &fdb_info->info, NULL);
1621c9eb3e0fSArkadi Sharshevsky 		break;
1622c9eb3e0fSArkadi Sharshevsky 
1623c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1624c9eb3e0fSArkadi Sharshevsky 		fdb_info = &switchdev_work->fdb_info;
1625a37fb855SVivien Didelot 		if (!fdb_info->added_by_user)
1626a37fb855SVivien Didelot 			break;
1627a37fb855SVivien Didelot 
1628d945097bSVivien Didelot 		err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
1629c9eb3e0fSArkadi Sharshevsky 		if (err) {
1630c9eb3e0fSArkadi Sharshevsky 			netdev_dbg(dev, "fdb del failed err=%d\n", err);
1631c9eb3e0fSArkadi Sharshevsky 			dev_close(dev);
1632c9eb3e0fSArkadi Sharshevsky 		}
1633c9eb3e0fSArkadi Sharshevsky 		break;
1634c9eb3e0fSArkadi Sharshevsky 	}
1635c9eb3e0fSArkadi Sharshevsky 	rtnl_unlock();
1636c9eb3e0fSArkadi Sharshevsky 
1637c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work->fdb_info.addr);
1638c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1639c9eb3e0fSArkadi Sharshevsky 	dev_put(dev);
1640c9eb3e0fSArkadi Sharshevsky }
1641c9eb3e0fSArkadi Sharshevsky 
1642c9eb3e0fSArkadi Sharshevsky static int
1643c9eb3e0fSArkadi Sharshevsky dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1644c9eb3e0fSArkadi Sharshevsky 				  switchdev_work,
1645c9eb3e0fSArkadi Sharshevsky 				  const struct switchdev_notifier_fdb_info *
1646c9eb3e0fSArkadi Sharshevsky 				  fdb_info)
1647c9eb3e0fSArkadi Sharshevsky {
1648c9eb3e0fSArkadi Sharshevsky 	memcpy(&switchdev_work->fdb_info, fdb_info,
1649c9eb3e0fSArkadi Sharshevsky 	       sizeof(switchdev_work->fdb_info));
1650c9eb3e0fSArkadi Sharshevsky 	switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1651c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work->fdb_info.addr)
1652c9eb3e0fSArkadi Sharshevsky 		return -ENOMEM;
1653c9eb3e0fSArkadi Sharshevsky 	ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1654c9eb3e0fSArkadi Sharshevsky 			fdb_info->addr);
1655c9eb3e0fSArkadi Sharshevsky 	return 0;
1656c9eb3e0fSArkadi Sharshevsky }
1657c9eb3e0fSArkadi Sharshevsky 
1658c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
1659c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
1660c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
1661c9eb3e0fSArkadi Sharshevsky {
1662c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1663c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work;
1664c9eb3e0fSArkadi Sharshevsky 
1665c9eb3e0fSArkadi Sharshevsky 	if (!dsa_slave_dev_check(dev))
1666c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1667c9eb3e0fSArkadi Sharshevsky 
16689ed1ecedSFlorian Fainelli 	if (event == SWITCHDEV_PORT_ATTR_SET)
16699ed1ecedSFlorian Fainelli 		return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
16709ed1ecedSFlorian Fainelli 
1671c9eb3e0fSArkadi Sharshevsky 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1672c9eb3e0fSArkadi Sharshevsky 	if (!switchdev_work)
1673c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_BAD;
1674c9eb3e0fSArkadi Sharshevsky 
1675c9eb3e0fSArkadi Sharshevsky 	INIT_WORK(&switchdev_work->work,
1676c9eb3e0fSArkadi Sharshevsky 		  dsa_slave_switchdev_event_work);
1677c9eb3e0fSArkadi Sharshevsky 	switchdev_work->dev = dev;
1678c9eb3e0fSArkadi Sharshevsky 	switchdev_work->event = event;
1679c9eb3e0fSArkadi Sharshevsky 
1680c9eb3e0fSArkadi Sharshevsky 	switch (event) {
1681c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1682c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
1683a37fb855SVivien Didelot 		if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
1684c9eb3e0fSArkadi Sharshevsky 			goto err_fdb_work_init;
1685c9eb3e0fSArkadi Sharshevsky 		dev_hold(dev);
1686c9eb3e0fSArkadi Sharshevsky 		break;
1687c9eb3e0fSArkadi Sharshevsky 	default:
1688c9eb3e0fSArkadi Sharshevsky 		kfree(switchdev_work);
1689c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
1690c9eb3e0fSArkadi Sharshevsky 	}
1691c9eb3e0fSArkadi Sharshevsky 
1692c9eb3e0fSArkadi Sharshevsky 	dsa_schedule_work(&switchdev_work->work);
1693c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
1694c9eb3e0fSArkadi Sharshevsky 
1695c9eb3e0fSArkadi Sharshevsky err_fdb_work_init:
1696c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
1697c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_BAD;
1698c9eb3e0fSArkadi Sharshevsky }
1699c9eb3e0fSArkadi Sharshevsky 
17002b239f67SPetr Machata static int
17012b239f67SPetr Machata dsa_slave_switchdev_port_obj_event(unsigned long event,
17022b239f67SPetr Machata 			struct net_device *netdev,
17032b239f67SPetr Machata 			struct switchdev_notifier_port_obj_info *port_obj_info)
17042b239f67SPetr Machata {
17052b239f67SPetr Machata 	int err = -EOPNOTSUPP;
17062b239f67SPetr Machata 
17072b239f67SPetr Machata 	switch (event) {
17082b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
17092b239f67SPetr Machata 		err = dsa_slave_port_obj_add(netdev, port_obj_info->obj,
17102b239f67SPetr Machata 					     port_obj_info->trans);
17112b239f67SPetr Machata 		break;
17122b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
17132b239f67SPetr Machata 		err = dsa_slave_port_obj_del(netdev, port_obj_info->obj);
17142b239f67SPetr Machata 		break;
17152b239f67SPetr Machata 	}
17162b239f67SPetr Machata 
17172b239f67SPetr Machata 	port_obj_info->handled = true;
17182b239f67SPetr Machata 	return notifier_from_errno(err);
17192b239f67SPetr Machata }
17202b239f67SPetr Machata 
17212b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
17222b239f67SPetr Machata 					      unsigned long event, void *ptr)
17232b239f67SPetr Machata {
17242b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
17252b239f67SPetr Machata 
17262b239f67SPetr Machata 	if (!dsa_slave_dev_check(dev))
17272b239f67SPetr Machata 		return NOTIFY_DONE;
17282b239f67SPetr Machata 
17292b239f67SPetr Machata 	switch (event) {
17302b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
17312b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
17322b239f67SPetr Machata 		return dsa_slave_switchdev_port_obj_event(event, dev, ptr);
17339ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
17349ed1ecedSFlorian Fainelli 		return dsa_slave_switchdev_port_attr_set_event(dev, ptr);
17352b239f67SPetr Machata 	}
17362b239f67SPetr Machata 
17372b239f67SPetr Machata 	return NOTIFY_DONE;
17382b239f67SPetr Machata }
17392b239f67SPetr Machata 
174088e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
174188e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
174288e4f0caSVivien Didelot };
174388e4f0caSVivien Didelot 
1744c9eb3e0fSArkadi Sharshevsky static struct notifier_block dsa_slave_switchdev_notifier = {
1745c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
1746c9eb3e0fSArkadi Sharshevsky };
1747c9eb3e0fSArkadi Sharshevsky 
17482b239f67SPetr Machata static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
17492b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
17502b239f67SPetr Machata };
17512b239f67SPetr Machata 
175288e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
175388e4f0caSVivien Didelot {
17542b239f67SPetr Machata 	struct notifier_block *nb;
1755c9eb3e0fSArkadi Sharshevsky 	int err;
1756c9eb3e0fSArkadi Sharshevsky 
1757c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
1758c9eb3e0fSArkadi Sharshevsky 	if (err)
1759c9eb3e0fSArkadi Sharshevsky 		return err;
1760c9eb3e0fSArkadi Sharshevsky 
1761c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1762c9eb3e0fSArkadi Sharshevsky 	if (err)
1763c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
1764c9eb3e0fSArkadi Sharshevsky 
17652b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
17662b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
17672b239f67SPetr Machata 	if (err)
17682b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
17692b239f67SPetr Machata 
1770c9eb3e0fSArkadi Sharshevsky 	return 0;
1771c9eb3e0fSArkadi Sharshevsky 
17722b239f67SPetr Machata err_switchdev_blocking_nb:
17732b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1774c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
1775c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
1776c9eb3e0fSArkadi Sharshevsky 	return err;
177788e4f0caSVivien Didelot }
177888e4f0caSVivien Didelot 
177988e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
178088e4f0caSVivien Didelot {
17812b239f67SPetr Machata 	struct notifier_block *nb;
178288e4f0caSVivien Didelot 	int err;
178388e4f0caSVivien Didelot 
17842b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
17852b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
17862b239f67SPetr Machata 	if (err)
17872b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
17882b239f67SPetr Machata 
1789c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1790c9eb3e0fSArkadi Sharshevsky 	if (err)
1791c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1792c9eb3e0fSArkadi Sharshevsky 
179388e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
179488e4f0caSVivien Didelot 	if (err)
179588e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
179688e4f0caSVivien Didelot }
1797