xref: /openbmc/linux/net/dsa/slave.c (revision 1c6e8088d9a74674b4017f8d2090ca9a314954b7)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
291da11f8SLennert Buytenhek /*
391da11f8SLennert Buytenhek  * net/dsa/slave.c - Slave device handling
4e84665c9SLennert Buytenhek  * Copyright (c) 2008-2009 Marvell Semiconductor
591da11f8SLennert Buytenhek  */
691da11f8SLennert Buytenhek 
791da11f8SLennert Buytenhek #include <linux/list.h>
8df02c6ffSLennert Buytenhek #include <linux/etherdevice.h>
9b73adef6SFlorian Fainelli #include <linux/netdevice.h>
1091da11f8SLennert Buytenhek #include <linux/phy.h>
11a2820543SFlorian Fainelli #include <linux/phy_fixed.h>
12aab9c406SFlorian Fainelli #include <linux/phylink.h>
130d8bcdd3SFlorian Fainelli #include <linux/of_net.h>
140d8bcdd3SFlorian Fainelli #include <linux/of_mdio.h>
157f854420SAndrew Lunn #include <linux/mdio.h>
16b73adef6SFlorian Fainelli #include <net/rtnetlink.h>
17f50f2127SFlorian Fainelli #include <net/pkt_cls.h>
18a71acad9SOleksij Rempel #include <net/selftests.h>
19f50f2127SFlorian Fainelli #include <net/tc_act/tc_mirred.h>
20b73adef6SFlorian Fainelli #include <linux/if_bridge.h>
2118596f50SGeorge McCollister #include <linux/if_hsr.h>
22d538eca8SVladimir Oltean #include <net/dcbnl.h>
2304ff53f9SFlorian Fainelli #include <linux/netpoll.h>
24ea5dd34bSVivien Didelot 
2591da11f8SLennert Buytenhek #include "dsa_priv.h"
2691da11f8SLennert Buytenhek 
275e8a1e03SVladimir Oltean static void dsa_slave_standalone_event_work(struct work_struct *work)
285e8a1e03SVladimir Oltean {
295e8a1e03SVladimir Oltean 	struct dsa_standalone_event_work *standalone_work =
305e8a1e03SVladimir Oltean 		container_of(work, struct dsa_standalone_event_work, work);
315e8a1e03SVladimir Oltean 	const unsigned char *addr = standalone_work->addr;
325e8a1e03SVladimir Oltean 	struct net_device *dev = standalone_work->dev;
335e8a1e03SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
345e8a1e03SVladimir Oltean 	struct switchdev_obj_port_mdb mdb;
355e8a1e03SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
365e8a1e03SVladimir Oltean 	u16 vid = standalone_work->vid;
375e8a1e03SVladimir Oltean 	int err;
385e8a1e03SVladimir Oltean 
395e8a1e03SVladimir Oltean 	switch (standalone_work->event) {
405e8a1e03SVladimir Oltean 	case DSA_UC_ADD:
415e8a1e03SVladimir Oltean 		err = dsa_port_standalone_host_fdb_add(dp, addr, vid);
425e8a1e03SVladimir Oltean 		if (err) {
435e8a1e03SVladimir Oltean 			dev_err(ds->dev,
445e8a1e03SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
455e8a1e03SVladimir Oltean 				dp->index, addr, vid, err);
465e8a1e03SVladimir Oltean 			break;
475e8a1e03SVladimir Oltean 		}
485e8a1e03SVladimir Oltean 		break;
495e8a1e03SVladimir Oltean 
505e8a1e03SVladimir Oltean 	case DSA_UC_DEL:
515e8a1e03SVladimir Oltean 		err = dsa_port_standalone_host_fdb_del(dp, addr, vid);
525e8a1e03SVladimir Oltean 		if (err) {
535e8a1e03SVladimir Oltean 			dev_err(ds->dev,
545e8a1e03SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
555e8a1e03SVladimir Oltean 				dp->index, addr, vid, err);
565e8a1e03SVladimir Oltean 		}
575e8a1e03SVladimir Oltean 
585e8a1e03SVladimir Oltean 		break;
595e8a1e03SVladimir Oltean 	case DSA_MC_ADD:
605e8a1e03SVladimir Oltean 		ether_addr_copy(mdb.addr, addr);
615e8a1e03SVladimir Oltean 		mdb.vid = vid;
625e8a1e03SVladimir Oltean 
635e8a1e03SVladimir Oltean 		err = dsa_port_standalone_host_mdb_add(dp, &mdb);
645e8a1e03SVladimir Oltean 		if (err) {
655e8a1e03SVladimir Oltean 			dev_err(ds->dev,
665e8a1e03SVladimir Oltean 				"port %d failed to add %pM vid %d to mdb: %d\n",
675e8a1e03SVladimir Oltean 				dp->index, addr, vid, err);
685e8a1e03SVladimir Oltean 			break;
695e8a1e03SVladimir Oltean 		}
705e8a1e03SVladimir Oltean 		break;
715e8a1e03SVladimir Oltean 	case DSA_MC_DEL:
725e8a1e03SVladimir Oltean 		ether_addr_copy(mdb.addr, addr);
735e8a1e03SVladimir Oltean 		mdb.vid = vid;
745e8a1e03SVladimir Oltean 
755e8a1e03SVladimir Oltean 		err = dsa_port_standalone_host_mdb_del(dp, &mdb);
765e8a1e03SVladimir Oltean 		if (err) {
775e8a1e03SVladimir Oltean 			dev_err(ds->dev,
785e8a1e03SVladimir Oltean 				"port %d failed to delete %pM vid %d from mdb: %d\n",
795e8a1e03SVladimir Oltean 				dp->index, addr, vid, err);
805e8a1e03SVladimir Oltean 		}
815e8a1e03SVladimir Oltean 
825e8a1e03SVladimir Oltean 		break;
835e8a1e03SVladimir Oltean 	}
845e8a1e03SVladimir Oltean 
855e8a1e03SVladimir Oltean 	kfree(standalone_work);
865e8a1e03SVladimir Oltean }
875e8a1e03SVladimir Oltean 
885e8a1e03SVladimir Oltean static int dsa_slave_schedule_standalone_work(struct net_device *dev,
895e8a1e03SVladimir Oltean 					      enum dsa_standalone_event event,
905e8a1e03SVladimir Oltean 					      const unsigned char *addr,
915e8a1e03SVladimir Oltean 					      u16 vid)
925e8a1e03SVladimir Oltean {
935e8a1e03SVladimir Oltean 	struct dsa_standalone_event_work *standalone_work;
945e8a1e03SVladimir Oltean 
955e8a1e03SVladimir Oltean 	standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC);
965e8a1e03SVladimir Oltean 	if (!standalone_work)
975e8a1e03SVladimir Oltean 		return -ENOMEM;
985e8a1e03SVladimir Oltean 
995e8a1e03SVladimir Oltean 	INIT_WORK(&standalone_work->work, dsa_slave_standalone_event_work);
1005e8a1e03SVladimir Oltean 	standalone_work->event = event;
1015e8a1e03SVladimir Oltean 	standalone_work->dev = dev;
1025e8a1e03SVladimir Oltean 
1035e8a1e03SVladimir Oltean 	ether_addr_copy(standalone_work->addr, addr);
1045e8a1e03SVladimir Oltean 	standalone_work->vid = vid;
1055e8a1e03SVladimir Oltean 
1065e8a1e03SVladimir Oltean 	dsa_schedule_work(&standalone_work->work);
1075e8a1e03SVladimir Oltean 
1085e8a1e03SVladimir Oltean 	return 0;
1095e8a1e03SVladimir Oltean }
1105e8a1e03SVladimir Oltean 
1115e8a1e03SVladimir Oltean static int dsa_slave_sync_uc(struct net_device *dev,
1125e8a1e03SVladimir Oltean 			     const unsigned char *addr)
1135e8a1e03SVladimir Oltean {
1145077e2c8SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1155077e2c8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1165077e2c8SVladimir Oltean 
1175077e2c8SVladimir Oltean 	dev_uc_add(master, addr);
1185077e2c8SVladimir Oltean 
1195077e2c8SVladimir Oltean 	if (!dsa_switch_supports_uc_filtering(dp->ds))
1205077e2c8SVladimir Oltean 		return 0;
1215077e2c8SVladimir Oltean 
1225e8a1e03SVladimir Oltean 	return dsa_slave_schedule_standalone_work(dev, DSA_UC_ADD, addr, 0);
1235e8a1e03SVladimir Oltean }
1245e8a1e03SVladimir Oltean 
1255e8a1e03SVladimir Oltean static int dsa_slave_unsync_uc(struct net_device *dev,
1265e8a1e03SVladimir Oltean 			       const unsigned char *addr)
1275e8a1e03SVladimir Oltean {
1285077e2c8SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1295077e2c8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1305077e2c8SVladimir Oltean 
1315077e2c8SVladimir Oltean 	dev_uc_del(master, addr);
1325077e2c8SVladimir Oltean 
1335077e2c8SVladimir Oltean 	if (!dsa_switch_supports_uc_filtering(dp->ds))
1345077e2c8SVladimir Oltean 		return 0;
1355077e2c8SVladimir Oltean 
1365e8a1e03SVladimir Oltean 	return dsa_slave_schedule_standalone_work(dev, DSA_UC_DEL, addr, 0);
1375e8a1e03SVladimir Oltean }
1385e8a1e03SVladimir Oltean 
1395e8a1e03SVladimir Oltean static int dsa_slave_sync_mc(struct net_device *dev,
1405e8a1e03SVladimir Oltean 			     const unsigned char *addr)
1415e8a1e03SVladimir Oltean {
1425077e2c8SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1435077e2c8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1445077e2c8SVladimir Oltean 
1455077e2c8SVladimir Oltean 	dev_mc_add(master, addr);
1465077e2c8SVladimir Oltean 
1475077e2c8SVladimir Oltean 	if (!dsa_switch_supports_mc_filtering(dp->ds))
1485077e2c8SVladimir Oltean 		return 0;
1495077e2c8SVladimir Oltean 
1505e8a1e03SVladimir Oltean 	return dsa_slave_schedule_standalone_work(dev, DSA_MC_ADD, addr, 0);
1515e8a1e03SVladimir Oltean }
1525e8a1e03SVladimir Oltean 
1535e8a1e03SVladimir Oltean static int dsa_slave_unsync_mc(struct net_device *dev,
1545e8a1e03SVladimir Oltean 			       const unsigned char *addr)
1555e8a1e03SVladimir Oltean {
1565077e2c8SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1575077e2c8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1585077e2c8SVladimir Oltean 
1595077e2c8SVladimir Oltean 	dev_mc_del(master, addr);
1605077e2c8SVladimir Oltean 
1615077e2c8SVladimir Oltean 	if (!dsa_switch_supports_mc_filtering(dp->ds))
1625077e2c8SVladimir Oltean 		return 0;
1635077e2c8SVladimir Oltean 
1645e8a1e03SVladimir Oltean 	return dsa_slave_schedule_standalone_work(dev, DSA_MC_DEL, addr, 0);
1655e8a1e03SVladimir Oltean }
1665e8a1e03SVladimir Oltean 
16791da11f8SLennert Buytenhek /* slave mii_bus handling ***************************************************/
16891da11f8SLennert Buytenhek static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
16991da11f8SLennert Buytenhek {
17091da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
17191da11f8SLennert Buytenhek 
1720d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
1739d490b4eSVivien Didelot 		return ds->ops->phy_read(ds, addr, reg);
17491da11f8SLennert Buytenhek 
17591da11f8SLennert Buytenhek 	return 0xffff;
17691da11f8SLennert Buytenhek }
17791da11f8SLennert Buytenhek 
17891da11f8SLennert Buytenhek static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
17991da11f8SLennert Buytenhek {
18091da11f8SLennert Buytenhek 	struct dsa_switch *ds = bus->priv;
18191da11f8SLennert Buytenhek 
1820d8bcdd3SFlorian Fainelli 	if (ds->phys_mii_mask & (1 << addr))
1839d490b4eSVivien Didelot 		return ds->ops->phy_write(ds, addr, reg, val);
18491da11f8SLennert Buytenhek 
18591da11f8SLennert Buytenhek 	return 0;
18691da11f8SLennert Buytenhek }
18791da11f8SLennert Buytenhek 
18891da11f8SLennert Buytenhek void dsa_slave_mii_bus_init(struct dsa_switch *ds)
18991da11f8SLennert Buytenhek {
19091da11f8SLennert Buytenhek 	ds->slave_mii_bus->priv = (void *)ds;
19191da11f8SLennert Buytenhek 	ds->slave_mii_bus->name = "dsa slave smi";
19291da11f8SLennert Buytenhek 	ds->slave_mii_bus->read = dsa_slave_phy_read;
19391da11f8SLennert Buytenhek 	ds->slave_mii_bus->write = dsa_slave_phy_write;
1940b7b498dSFlorian Fainelli 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
19549463b7fSVivien Didelot 		 ds->dst->index, ds->index);
196c33063d6SAndrew Lunn 	ds->slave_mii_bus->parent = ds->dev;
19724df8986SVivien Didelot 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
19891da11f8SLennert Buytenhek }
19991da11f8SLennert Buytenhek 
20091da11f8SLennert Buytenhek 
20191da11f8SLennert Buytenhek /* slave device handling ****************************************************/
202abd2be00SNicolas Dichtel static int dsa_slave_get_iflink(const struct net_device *dev)
203c0840801SLennert Buytenhek {
204d0006b00SVivien Didelot 	return dsa_slave_to_master(dev)->ifindex;
205c0840801SLennert Buytenhek }
206c0840801SLennert Buytenhek 
20791da11f8SLennert Buytenhek static int dsa_slave_open(struct net_device *dev)
20891da11f8SLennert Buytenhek {
209d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
210d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
211499aa9e1SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
212df02c6ffSLennert Buytenhek 	int err;
213df02c6ffSLennert Buytenhek 
2149d5ef190SVladimir Oltean 	err = dev_open(master, NULL);
2159d5ef190SVladimir Oltean 	if (err < 0) {
2169d5ef190SVladimir Oltean 		netdev_err(dev, "failed to open master %s\n", master->name);
2179d5ef190SVladimir Oltean 		goto out;
2189d5ef190SVladimir Oltean 	}
219df02c6ffSLennert Buytenhek 
220499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds)) {
221499aa9e1SVladimir Oltean 		err = dsa_port_standalone_host_fdb_add(dp, dev->dev_addr, 0);
222499aa9e1SVladimir Oltean 		if (err)
223499aa9e1SVladimir Oltean 			goto out;
224499aa9e1SVladimir Oltean 	}
225499aa9e1SVladimir Oltean 
2268feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
227a748ee24SJiri Pirko 		err = dev_uc_add(master, dev->dev_addr);
228df02c6ffSLennert Buytenhek 		if (err < 0)
229499aa9e1SVladimir Oltean 			goto del_host_addr;
230df02c6ffSLennert Buytenhek 	}
231df02c6ffSLennert Buytenhek 
2328640f8dcSRussell King 	err = dsa_port_enable_rt(dp, dev->phydev);
233b2f2af21SFlorian Fainelli 	if (err)
23435aae5abSVladimir Oltean 		goto del_unicast;
235b73adef6SFlorian Fainelli 
23691da11f8SLennert Buytenhek 	return 0;
237df02c6ffSLennert Buytenhek 
238df02c6ffSLennert Buytenhek del_unicast:
2398feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
240a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
241499aa9e1SVladimir Oltean del_host_addr:
242499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
243499aa9e1SVladimir Oltean 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
244df02c6ffSLennert Buytenhek out:
245df02c6ffSLennert Buytenhek 	return err;
24691da11f8SLennert Buytenhek }
24791da11f8SLennert Buytenhek 
24891da11f8SLennert Buytenhek static int dsa_slave_close(struct net_device *dev)
24991da11f8SLennert Buytenhek {
250d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
251d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
252499aa9e1SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
253df02c6ffSLennert Buytenhek 
2548640f8dcSRussell King 	dsa_port_disable_rt(dp);
2556457edfeSVivien Didelot 
2568feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
257a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
258df02c6ffSLennert Buytenhek 
259499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
260499aa9e1SVladimir Oltean 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
261499aa9e1SVladimir Oltean 
26291da11f8SLennert Buytenhek 	return 0;
26391da11f8SLennert Buytenhek }
26491da11f8SLennert Buytenhek 
26572c3b0c7SVladimir Oltean static void dsa_slave_manage_host_flood(struct net_device *dev)
2667569459aSVladimir Oltean {
26772c3b0c7SVladimir Oltean 	bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI);
26872c3b0c7SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
26972c3b0c7SVladimir Oltean 	bool uc = dev->flags & IFF_PROMISC;
2707569459aSVladimir Oltean 
27172c3b0c7SVladimir Oltean 	dsa_port_set_host_flood(dp, uc, mc);
2727569459aSVladimir Oltean }
2737569459aSVladimir Oltean 
27491da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
27591da11f8SLennert Buytenhek {
276d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
2777569459aSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2787569459aSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
27935aae5abSVladimir Oltean 
28091da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
28117ab4f61SRundong Ge 		dev_set_allmulti(master,
28217ab4f61SRundong Ge 				 dev->flags & IFF_ALLMULTI ? 1 : -1);
28391da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
28417ab4f61SRundong Ge 		dev_set_promiscuity(master,
28517ab4f61SRundong Ge 				    dev->flags & IFF_PROMISC ? 1 : -1);
2867569459aSVladimir Oltean 
2877569459aSVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds) &&
2887569459aSVladimir Oltean 	    dsa_switch_supports_mc_filtering(ds))
28972c3b0c7SVladimir Oltean 		dsa_slave_manage_host_flood(dev);
29017ab4f61SRundong Ge }
29191da11f8SLennert Buytenhek 
29291da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
29391da11f8SLennert Buytenhek {
2945e8a1e03SVladimir Oltean 	__dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc);
2955e8a1e03SVladimir Oltean 	__dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc);
29691da11f8SLennert Buytenhek }
29791da11f8SLennert Buytenhek 
298df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
29991da11f8SLennert Buytenhek {
300d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
301499aa9e1SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
302499aa9e1SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
303df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
304df02c6ffSLennert Buytenhek 	int err;
305df02c6ffSLennert Buytenhek 
306df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
307df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
308df02c6ffSLennert Buytenhek 
309e2d0576fSVladimir Oltean 	/* If the port is down, the address isn't synced yet to hardware or
310e2d0576fSVladimir Oltean 	 * to the DSA master, so there is nothing to change.
311e2d0576fSVladimir Oltean 	 */
312e2d0576fSVladimir Oltean 	if (!(dev->flags & IFF_UP))
313e2d0576fSVladimir Oltean 		goto out_change_dev_addr;
314e2d0576fSVladimir Oltean 
315499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds)) {
316499aa9e1SVladimir Oltean 		err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
317499aa9e1SVladimir Oltean 		if (err)
318499aa9e1SVladimir Oltean 			return err;
319499aa9e1SVladimir Oltean 	}
320499aa9e1SVladimir Oltean 
3218feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
322a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
323df02c6ffSLennert Buytenhek 		if (err < 0)
324499aa9e1SVladimir Oltean 			goto del_unicast;
325df02c6ffSLennert Buytenhek 	}
326df02c6ffSLennert Buytenhek 
3278feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
328a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
329df02c6ffSLennert Buytenhek 
330499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
331499aa9e1SVladimir Oltean 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
332499aa9e1SVladimir Oltean 
333e2d0576fSVladimir Oltean out_change_dev_addr:
334e35b8d7dSJakub Kicinski 	eth_hw_addr_set(dev, addr->sa_data);
33591da11f8SLennert Buytenhek 
33691da11f8SLennert Buytenhek 	return 0;
337499aa9e1SVladimir Oltean 
338499aa9e1SVladimir Oltean del_unicast:
339499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
340499aa9e1SVladimir Oltean 		dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
341499aa9e1SVladimir Oltean 
342499aa9e1SVladimir Oltean 	return err;
34391da11f8SLennert Buytenhek }
34491da11f8SLennert Buytenhek 
3452bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
3462bedde1aSArkadi Sharshevsky 	struct net_device *dev;
3472bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
3482bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
3492bedde1aSArkadi Sharshevsky 	int idx;
3502bedde1aSArkadi Sharshevsky };
3512bedde1aSArkadi Sharshevsky 
3522bedde1aSArkadi Sharshevsky static int
3532bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
3542bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
3552bedde1aSArkadi Sharshevsky {
3562bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
3572bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
3582bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
3592bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
3602bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
3612bedde1aSArkadi Sharshevsky 
3622bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
3632bedde1aSArkadi Sharshevsky 		goto skip;
3642bedde1aSArkadi Sharshevsky 
3652bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
3662bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
3672bedde1aSArkadi Sharshevsky 	if (!nlh)
3682bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
3692bedde1aSArkadi Sharshevsky 
3702bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
3712bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
3722bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
3732bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
3742bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
3752bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
3762bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
3772bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
3782bedde1aSArkadi Sharshevsky 
3792bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
3802bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
3812bedde1aSArkadi Sharshevsky 
3822bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
3832bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
3842bedde1aSArkadi Sharshevsky 
3852bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
3862bedde1aSArkadi Sharshevsky 
3872bedde1aSArkadi Sharshevsky skip:
3882bedde1aSArkadi Sharshevsky 	dump->idx++;
3892bedde1aSArkadi Sharshevsky 	return 0;
3902bedde1aSArkadi Sharshevsky 
3912bedde1aSArkadi Sharshevsky nla_put_failure:
3922bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
3932bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
3942bedde1aSArkadi Sharshevsky }
3952bedde1aSArkadi Sharshevsky 
3962bedde1aSArkadi Sharshevsky static int
3972bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
3982bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
3992bedde1aSArkadi Sharshevsky 		   int *idx)
4002bedde1aSArkadi Sharshevsky {
401d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
4022bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
4032bedde1aSArkadi Sharshevsky 		.dev = dev,
4042bedde1aSArkadi Sharshevsky 		.skb = skb,
4052bedde1aSArkadi Sharshevsky 		.cb = cb,
4062bedde1aSArkadi Sharshevsky 		.idx = *idx,
4072bedde1aSArkadi Sharshevsky 	};
4082bedde1aSArkadi Sharshevsky 	int err;
4092bedde1aSArkadi Sharshevsky 
410de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
4112bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
412de40fc5dSVivien Didelot 
4132bedde1aSArkadi Sharshevsky 	return err;
4142bedde1aSArkadi Sharshevsky }
4152bedde1aSArkadi Sharshevsky 
41691da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
41791da11f8SLennert Buytenhek {
4180336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
4190336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
4200336369dSBrandon Streiff 	int port = p->dp->index;
4210336369dSBrandon Streiff 
4220336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
4230336369dSBrandon Streiff 	switch (cmd) {
4240336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
4250336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
4260336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
4270336369dSBrandon Streiff 		break;
4280336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
4290336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
4300336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
4310336369dSBrandon Streiff 		break;
4320336369dSBrandon Streiff 	}
4330336369dSBrandon Streiff 
434aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
43591da11f8SLennert Buytenhek }
43691da11f8SLennert Buytenhek 
43769bfac96SVladimir Oltean static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
4384c08c586SVladimir Oltean 				   const struct switchdev_attr *attr,
4394c08c586SVladimir Oltean 				   struct netlink_ext_ack *extack)
44035636062SScott Feldman {
441d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
442b8d866acSVivien Didelot 	int ret;
44335636062SScott Feldman 
4440d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
4450d2cfbd4SVladimir Oltean 		return 0;
4460d2cfbd4SVladimir Oltean 
44735636062SScott Feldman 	switch (attr->id) {
4481f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
44903cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
45003cbb870SVladimir Oltean 			return -EOPNOTSUPP;
45103cbb870SVladimir Oltean 
45239f32101SVladimir Oltean 		ret = dsa_port_set_state(dp, attr->u.stp_state, true);
45335636062SScott Feldman 		break;
4547414af30STobias Waldekranz 	case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
4557414af30STobias Waldekranz 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
4567414af30STobias Waldekranz 			return -EOPNOTSUPP;
4577414af30STobias Waldekranz 
4587414af30STobias Waldekranz 		ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
4597414af30STobias Waldekranz 		break;
460fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
461936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
46203cbb870SVladimir Oltean 			return -EOPNOTSUPP;
46303cbb870SVladimir Oltean 
46489153ed6SVladimir Oltean 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
46589153ed6SVladimir Oltean 					      extack);
466fb2dabadSVivien Didelot 		break;
46734a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
468936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
46903cbb870SVladimir Oltean 			return -EOPNOTSUPP;
47003cbb870SVladimir Oltean 
471bae33f2bSVladimir Oltean 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
47234a79f63SVivien Didelot 		break;
473332afc4cSTobias Waldekranz 	case SWITCHDEV_ATTR_ID_BRIDGE_MST:
474332afc4cSTobias Waldekranz 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
475332afc4cSTobias Waldekranz 			return -EOPNOTSUPP;
476332afc4cSTobias Waldekranz 
477332afc4cSTobias Waldekranz 		ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
478332afc4cSTobias Waldekranz 		break;
479ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
48003cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
48103cbb870SVladimir Oltean 			return -EOPNOTSUPP;
48203cbb870SVladimir Oltean 
483a8b659e7SVladimir Oltean 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
484a8b659e7SVladimir Oltean 						extack);
485ea87005aSFlorian Fainelli 		break;
48657652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
48703cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
48803cbb870SVladimir Oltean 			return -EOPNOTSUPP;
48903cbb870SVladimir Oltean 
490a8b659e7SVladimir Oltean 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
49157652796SRussell King 		break;
4928e6598a7STobias Waldekranz 	case SWITCHDEV_ATTR_ID_VLAN_MSTI:
4938e6598a7STobias Waldekranz 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
4948e6598a7STobias Waldekranz 			return -EOPNOTSUPP;
4958e6598a7STobias Waldekranz 
4968e6598a7STobias Waldekranz 		ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
4978e6598a7STobias Waldekranz 		break;
49835636062SScott Feldman 	default:
49935636062SScott Feldman 		ret = -EOPNOTSUPP;
50035636062SScott Feldman 		break;
50135636062SScott Feldman 	}
50235636062SScott Feldman 
50335636062SScott Feldman 	return ret;
50435636062SScott Feldman }
50535636062SScott Feldman 
5061ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */
5071ce39f0eSVladimir Oltean static int
5081ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
5091ce39f0eSVladimir Oltean 				      const struct switchdev_obj_port_vlan *vlan)
5101ce39f0eSVladimir Oltean {
5111ce39f0eSVladimir Oltean 	struct net_device *upper_dev;
5121ce39f0eSVladimir Oltean 	struct list_head *iter;
5131ce39f0eSVladimir Oltean 
5141ce39f0eSVladimir Oltean 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
5151ce39f0eSVladimir Oltean 		u16 vid;
5161ce39f0eSVladimir Oltean 
5171ce39f0eSVladimir Oltean 		if (!is_vlan_dev(upper_dev))
5181ce39f0eSVladimir Oltean 			continue;
5191ce39f0eSVladimir Oltean 
5201ce39f0eSVladimir Oltean 		vid = vlan_dev_vlan_id(upper_dev);
521b7a9e0daSVladimir Oltean 		if (vid == vlan->vid)
5221ce39f0eSVladimir Oltean 			return -EBUSY;
5231ce39f0eSVladimir Oltean 	}
5241ce39f0eSVladimir Oltean 
5251ce39f0eSVladimir Oltean 	return 0;
5261ce39f0eSVladimir Oltean }
5271ce39f0eSVladimir Oltean 
528bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
5290ee2af4eSVladimir Oltean 			      const struct switchdev_obj *obj,
5300ee2af4eSVladimir Oltean 			      struct netlink_ext_ack *extack)
531bdcff080SVivien Didelot {
532bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
533134ef238SVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
534b7a9e0daSVladimir Oltean 	int err;
535bdcff080SVivien Didelot 
5360ee2af4eSVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
5370ee2af4eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
538c5335d73SVivien Didelot 		return 0;
5390ee2af4eSVladimir Oltean 	}
540c5335d73SVivien Didelot 
541134ef238SVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
542bdcff080SVivien Didelot 
5431ce39f0eSVladimir Oltean 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
5441ce39f0eSVladimir Oltean 	 * the same VID.
5451ce39f0eSVladimir Oltean 	 */
54636cbf39bSVladimir Oltean 	if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
5471ce39f0eSVladimir Oltean 		rcu_read_lock();
548134ef238SVladimir Oltean 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan);
5491ce39f0eSVladimir Oltean 		rcu_read_unlock();
55031046a5fSVladimir Oltean 		if (err) {
55131046a5fSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
55231046a5fSVladimir Oltean 					   "Port already has a VLAN upper with this VID");
5531ce39f0eSVladimir Oltean 			return err;
5541ce39f0eSVladimir Oltean 		}
55531046a5fSVladimir Oltean 	}
5561ce39f0eSVladimir Oltean 
557134ef238SVladimir Oltean 	return dsa_port_vlan_add(dp, vlan, extack);
558134ef238SVladimir Oltean }
559bdcff080SVivien Didelot 
560164f861bSVladimir Oltean /* Offload a VLAN installed on the bridge or on a foreign interface by
561164f861bSVladimir Oltean  * installing it as a VLAN towards the CPU port.
562164f861bSVladimir Oltean  */
563134ef238SVladimir Oltean static int dsa_slave_host_vlan_add(struct net_device *dev,
564134ef238SVladimir Oltean 				   const struct switchdev_obj *obj,
565134ef238SVladimir Oltean 				   struct netlink_ext_ack *extack)
566134ef238SVladimir Oltean {
567134ef238SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
568134ef238SVladimir Oltean 	struct switchdev_obj_port_vlan vlan;
569134ef238SVladimir Oltean 
570164f861bSVladimir Oltean 	/* Do nothing if this is a software bridge */
571164f861bSVladimir Oltean 	if (!dp->bridge)
572164f861bSVladimir Oltean 		return -EOPNOTSUPP;
573164f861bSVladimir Oltean 
574134ef238SVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
575134ef238SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
576134ef238SVladimir Oltean 		return 0;
577134ef238SVladimir Oltean 	}
578134ef238SVladimir Oltean 
579134ef238SVladimir Oltean 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
580134ef238SVladimir Oltean 
581134ef238SVladimir Oltean 	/* Even though drivers often handle CPU membership in special ways,
582b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
583b9499904SVivien Didelot 	 */
584b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
585b9499904SVivien Didelot 
586134ef238SVladimir Oltean 	return dsa_port_host_vlan_add(dp, &vlan, extack);
587bdcff080SVivien Didelot }
588bdcff080SVivien Didelot 
58969bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
590648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
59179b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
592ba14d9ebSVivien Didelot {
593d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
594ba14d9ebSVivien Didelot 	int err;
595ba14d9ebSVivien Didelot 
5960d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
5970d2cfbd4SVladimir Oltean 		return 0;
5980d2cfbd4SVladimir Oltean 
5999e8f4a54SJiri Pirko 	switch (obj->id) {
6008df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
60103cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
60279b139f4SVivien Didelot 			return -EOPNOTSUPP;
60303cbb870SVladimir Oltean 
604ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
6058df30255SVivien Didelot 		break;
6065f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
607936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
60803cbb870SVladimir Oltean 			return -EOPNOTSUPP;
60903cbb870SVladimir Oltean 
61068d6d71eSVladimir Oltean 		err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
6115f4dbc50SAndrew Lunn 		break;
61257d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
613164f861bSVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
6140ee2af4eSVladimir Oltean 			err = dsa_slave_vlan_add(dev, obj, extack);
615164f861bSVladimir Oltean 		else
616164f861bSVladimir Oltean 			err = dsa_slave_host_vlan_add(dev, obj, extack);
61711149536SVivien Didelot 		break;
618c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
619936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
620c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
62103cbb870SVladimir Oltean 
622c595c433SHoratiu Vultur 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
623c595c433SHoratiu Vultur 		break;
624c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
625936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
626c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
62703cbb870SVladimir Oltean 
628c595c433SHoratiu Vultur 		err = dsa_port_mrp_add_ring_role(dp,
629c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
630c595c433SHoratiu Vultur 		break;
631ba14d9ebSVivien Didelot 	default:
632ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
633ba14d9ebSVivien Didelot 		break;
634ba14d9ebSVivien Didelot 	}
635ba14d9ebSVivien Didelot 
636ba14d9ebSVivien Didelot 	return err;
637ba14d9ebSVivien Didelot }
638ba14d9ebSVivien Didelot 
639bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
640bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
641bdcff080SVivien Didelot {
642bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
6432209158cSVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
644bdcff080SVivien Didelot 
64554a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
646c5335d73SVivien Didelot 		return 0;
647c5335d73SVivien Didelot 
6482209158cSVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
6492209158cSVladimir Oltean 
650134ef238SVladimir Oltean 	return dsa_port_vlan_del(dp, vlan);
651134ef238SVladimir Oltean }
6522209158cSVladimir Oltean 
653134ef238SVladimir Oltean static int dsa_slave_host_vlan_del(struct net_device *dev,
654134ef238SVladimir Oltean 				   const struct switchdev_obj *obj)
655134ef238SVladimir Oltean {
656134ef238SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
657134ef238SVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
6582209158cSVladimir Oltean 
659164f861bSVladimir Oltean 	/* Do nothing if this is a software bridge */
660164f861bSVladimir Oltean 	if (!dp->bridge)
661164f861bSVladimir Oltean 		return -EOPNOTSUPP;
662164f861bSVladimir Oltean 
663134ef238SVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp))
6642209158cSVladimir Oltean 		return 0;
665134ef238SVladimir Oltean 
666134ef238SVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
667134ef238SVladimir Oltean 
668134ef238SVladimir Oltean 	return dsa_port_host_vlan_del(dp, vlan);
669bdcff080SVivien Didelot }
670bdcff080SVivien Didelot 
67169bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
672648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
673ba14d9ebSVivien Didelot {
674d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
675ba14d9ebSVivien Didelot 	int err;
676ba14d9ebSVivien Didelot 
6770d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
6780d2cfbd4SVladimir Oltean 		return 0;
6790d2cfbd4SVladimir Oltean 
6809e8f4a54SJiri Pirko 	switch (obj->id) {
6818df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
68203cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
68379b139f4SVivien Didelot 			return -EOPNOTSUPP;
68403cbb870SVladimir Oltean 
685bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
6868df30255SVivien Didelot 		break;
6875f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
688936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
68903cbb870SVladimir Oltean 			return -EOPNOTSUPP;
69003cbb870SVladimir Oltean 
69168d6d71eSVladimir Oltean 		err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
6925f4dbc50SAndrew Lunn 		break;
69357d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
694164f861bSVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
695bdcff080SVivien Didelot 			err = dsa_slave_vlan_del(dev, obj);
696164f861bSVladimir Oltean 		else
697164f861bSVladimir Oltean 			err = dsa_slave_host_vlan_del(dev, obj);
69811149536SVivien Didelot 		break;
699c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
700936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
701c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
70203cbb870SVladimir Oltean 
703c595c433SHoratiu Vultur 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
704c595c433SHoratiu Vultur 		break;
705c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
706936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
707c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
70803cbb870SVladimir Oltean 
709c595c433SHoratiu Vultur 		err = dsa_port_mrp_del_ring_role(dp,
710c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
711c595c433SHoratiu Vultur 		break;
712ba14d9ebSVivien Didelot 	default:
713ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
714ba14d9ebSVivien Didelot 		break;
715ba14d9ebSVivien Didelot 	}
716ba14d9ebSVivien Didelot 
717ba14d9ebSVivien Didelot 	return err;
718ba14d9ebSVivien Didelot }
719ba14d9ebSVivien Didelot 
7204fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
72104ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
72204ff53f9SFlorian Fainelli {
72304ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
7244fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
7254fa7b718SVivien Didelot 
726f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
72704ff53f9SFlorian Fainelli #else
72804ff53f9SFlorian Fainelli 	BUG();
72904ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
730f78ed220SEric Dumazet #endif
73104ff53f9SFlorian Fainelli }
73204ff53f9SFlorian Fainelli 
73390af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
73490af1059SBrandon Streiff 				 struct sk_buff *skb)
73590af1059SBrandon Streiff {
73690af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
73790af1059SBrandon Streiff 
738cfd12c06SYangbo Lu 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
739cfd12c06SYangbo Lu 		return;
740cfd12c06SYangbo Lu 
74190af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
74290af1059SBrandon Streiff 		return;
74390af1059SBrandon Streiff 
7445c5416f5SYangbo Lu 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
74590af1059SBrandon Streiff }
74690af1059SBrandon Streiff 
74797a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
74897a69a0dSVladimir Oltean {
74997a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
75097a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
75197a69a0dSVladimir Oltean 	 */
75297a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
75397a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
75497a69a0dSVladimir Oltean 
75597a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
75697a69a0dSVladimir Oltean 	 * do not modify its EtherType
75797a69a0dSVladimir Oltean 	 */
75897a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
75997a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
76097a69a0dSVladimir Oltean 
76197a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
76297a69a0dSVladimir Oltean }
76397a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
76497a69a0dSVladimir Oltean 
765a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
766a3b0b647SVladimir Oltean {
767a3b0b647SVladimir Oltean 	int needed_headroom = dev->needed_headroom;
768a3b0b647SVladimir Oltean 	int needed_tailroom = dev->needed_tailroom;
769a3b0b647SVladimir Oltean 
770a3b0b647SVladimir Oltean 	/* For tail taggers, we need to pad short frames ourselves, to ensure
771a3b0b647SVladimir Oltean 	 * that the tail tag does not fail at its role of being at the end of
772a3b0b647SVladimir Oltean 	 * the packet, once the master interface pads the frame. Account for
773a3b0b647SVladimir Oltean 	 * that pad length here, and pad later.
774a3b0b647SVladimir Oltean 	 */
775a3b0b647SVladimir Oltean 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
776a3b0b647SVladimir Oltean 		needed_tailroom += ETH_ZLEN - skb->len;
777a3b0b647SVladimir Oltean 	/* skb_headroom() returns unsigned int... */
778a3b0b647SVladimir Oltean 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
779a3b0b647SVladimir Oltean 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
780a3b0b647SVladimir Oltean 
781a3b0b647SVladimir Oltean 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
782a3b0b647SVladimir Oltean 		/* No reallocation needed, yay! */
783a3b0b647SVladimir Oltean 		return 0;
784a3b0b647SVladimir Oltean 
785a3b0b647SVladimir Oltean 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
786a3b0b647SVladimir Oltean 				GFP_ATOMIC);
787a3b0b647SVladimir Oltean }
788a3b0b647SVladimir Oltean 
7893e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
7903e8a72d1SFlorian Fainelli {
7913e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
7924ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
7933e8a72d1SFlorian Fainelli 
7946a900628SHeiner Kallweit 	dev_sw_netstats_tx_add(dev, 1, skb->len);
7953e8a72d1SFlorian Fainelli 
796c4b364ceSYangbo Lu 	memset(skb->cb, 0, sizeof(skb->cb));
79787671375SVladimir Oltean 
798cf536ea3SYangbo Lu 	/* Handle tx timestamp if any */
79990af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
80090af1059SBrandon Streiff 
801a3b0b647SVladimir Oltean 	if (dsa_realloc_skb(skb, dev)) {
802a3b0b647SVladimir Oltean 		dev_kfree_skb_any(skb);
803a3b0b647SVladimir Oltean 		return NETDEV_TX_OK;
804a3b0b647SVladimir Oltean 	}
805a3b0b647SVladimir Oltean 
806a3b0b647SVladimir Oltean 	/* needed_tailroom should still be 'warm' in the cache line from
807a3b0b647SVladimir Oltean 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
808a3b0b647SVladimir Oltean 	 */
809a3b0b647SVladimir Oltean 	if (dev->needed_tailroom)
810a3b0b647SVladimir Oltean 		eth_skb_pad(skb);
811a3b0b647SVladimir Oltean 
812fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
813fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
814fe47d563SVivien Didelot 	 */
8154ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
816fe47d563SVivien Didelot 	if (!nskb) {
817fe47d563SVivien Didelot 		kfree_skb(skb);
8184ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
819fe47d563SVivien Didelot 	}
8205aed85ceSFlorian Fainelli 
82197a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
82297a69a0dSVladimir Oltean }
82304ff53f9SFlorian Fainelli 
82491da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
82591da11f8SLennert Buytenhek 
82691da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
82791da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
82891da11f8SLennert Buytenhek {
8297826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
8307826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
8317826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
83291da11f8SLennert Buytenhek }
83391da11f8SLennert Buytenhek 
8343d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
8353d762a0fSGuenter Roeck {
836d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
837d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8383d762a0fSGuenter Roeck 
8399d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
840d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
8413d762a0fSGuenter Roeck 
8423d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
8433d762a0fSGuenter Roeck }
8443d762a0fSGuenter Roeck 
8453d762a0fSGuenter Roeck static void
8463d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
8473d762a0fSGuenter Roeck {
848d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
849d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8503d762a0fSGuenter Roeck 
8519d490b4eSVivien Didelot 	if (ds->ops->get_regs)
852d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
8533d762a0fSGuenter Roeck }
8543d762a0fSGuenter Roeck 
855aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
856aab9c406SFlorian Fainelli {
857aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
858aab9c406SFlorian Fainelli 
859aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
860aab9c406SFlorian Fainelli }
861aab9c406SFlorian Fainelli 
8626793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
8636793abb4SGuenter Roeck {
864d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
865d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8666793abb4SGuenter Roeck 
8670e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
868ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
8696793abb4SGuenter Roeck 
8709d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
8719d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
8726793abb4SGuenter Roeck 
8736793abb4SGuenter Roeck 	return 0;
8746793abb4SGuenter Roeck }
8756793abb4SGuenter Roeck 
8766793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
8776793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
8786793abb4SGuenter Roeck {
879d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
880d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8816793abb4SGuenter Roeck 
8829d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
8839d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
8846793abb4SGuenter Roeck 
8856793abb4SGuenter Roeck 	return -EOPNOTSUPP;
8866793abb4SGuenter Roeck }
8876793abb4SGuenter Roeck 
8886793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
8896793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
8906793abb4SGuenter Roeck {
891d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
892d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8936793abb4SGuenter Roeck 
8949d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
8959d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
8966793abb4SGuenter Roeck 
8976793abb4SGuenter Roeck 	return -EOPNOTSUPP;
8986793abb4SGuenter Roeck }
8996793abb4SGuenter Roeck 
90091da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
90191da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
90291da11f8SLennert Buytenhek {
903d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
904d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
90591da11f8SLennert Buytenhek 
90691da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
90791da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
90891da11f8SLennert Buytenhek 
90991da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
91091da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
91191da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
91291da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
9139d490b4eSVivien Didelot 		if (ds->ops->get_strings)
91489f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
91589f09048SFlorian Fainelli 					     data + 4 * len);
916a71acad9SOleksij Rempel 	} else if (stringset ==  ETH_SS_TEST) {
917a71acad9SOleksij Rempel 		net_selftest_get_strings(data);
91891da11f8SLennert Buytenhek 	}
919a71acad9SOleksij Rempel 
92091da11f8SLennert Buytenhek }
92191da11f8SLennert Buytenhek 
92291da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
92391da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
92491da11f8SLennert Buytenhek 					uint64_t *data)
92591da11f8SLennert Buytenhek {
926d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
927d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9285f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
929f613ed66SFlorian Fainelli 	unsigned int start;
9305f6b4e14SFlorian Fainelli 	int i;
93191da11f8SLennert Buytenhek 
9325f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
9335f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
9345f6b4e14SFlorian Fainelli 
9356a900628SHeiner Kallweit 		s = per_cpu_ptr(dev->tstats, i);
936f613ed66SFlorian Fainelli 		do {
9375f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
9389962acefSEric Dumazet 			tx_packets = u64_stats_read(&s->tx_packets);
9399962acefSEric Dumazet 			tx_bytes = u64_stats_read(&s->tx_bytes);
9409962acefSEric Dumazet 			rx_packets = u64_stats_read(&s->rx_packets);
9419962acefSEric Dumazet 			rx_bytes = u64_stats_read(&s->rx_bytes);
9425f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
9435f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
9445f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
9455f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
9465f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
9475f6b4e14SFlorian Fainelli 	}
9489d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
949d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
95091da11f8SLennert Buytenhek }
95191da11f8SLennert Buytenhek 
95291da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
95391da11f8SLennert Buytenhek {
954d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
955d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
95691da11f8SLennert Buytenhek 
95791da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
958b94cbc90SVladimir Oltean 		int count = 0;
95991da11f8SLennert Buytenhek 
960b94cbc90SVladimir Oltean 		if (ds->ops->get_sset_count) {
961b94cbc90SVladimir Oltean 			count = ds->ops->get_sset_count(ds, dp->index, sset);
962b94cbc90SVladimir Oltean 			if (count < 0)
96391da11f8SLennert Buytenhek 				return count;
964b94cbc90SVladimir Oltean 		}
965b94cbc90SVladimir Oltean 
966b94cbc90SVladimir Oltean 		return count + 4;
967a71acad9SOleksij Rempel 	} else if (sset ==  ETH_SS_TEST) {
968a71acad9SOleksij Rempel 		return net_selftest_get_count();
96991da11f8SLennert Buytenhek 	}
97091da11f8SLennert Buytenhek 
97191da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
97291da11f8SLennert Buytenhek }
97391da11f8SLennert Buytenhek 
974487d3855SAlvin Šipraga static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
975487d3855SAlvin Šipraga 					struct ethtool_eth_phy_stats *phy_stats)
976487d3855SAlvin Šipraga {
977487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
978487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
979487d3855SAlvin Šipraga 
980487d3855SAlvin Šipraga 	if (ds->ops->get_eth_phy_stats)
981487d3855SAlvin Šipraga 		ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
982487d3855SAlvin Šipraga }
983487d3855SAlvin Šipraga 
984487d3855SAlvin Šipraga static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
985487d3855SAlvin Šipraga 					struct ethtool_eth_mac_stats *mac_stats)
986487d3855SAlvin Šipraga {
987487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
988487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
989487d3855SAlvin Šipraga 
990487d3855SAlvin Šipraga 	if (ds->ops->get_eth_mac_stats)
991487d3855SAlvin Šipraga 		ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
992487d3855SAlvin Šipraga }
993487d3855SAlvin Šipraga 
994487d3855SAlvin Šipraga static void
995487d3855SAlvin Šipraga dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
996487d3855SAlvin Šipraga 			     struct ethtool_eth_ctrl_stats *ctrl_stats)
997487d3855SAlvin Šipraga {
998487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
999487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
1000487d3855SAlvin Šipraga 
1001487d3855SAlvin Šipraga 	if (ds->ops->get_eth_ctrl_stats)
1002487d3855SAlvin Šipraga 		ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1003487d3855SAlvin Šipraga }
1004487d3855SAlvin Šipraga 
1005a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev,
1006a71acad9SOleksij Rempel 				   struct ethtool_test *etest, u64 *buf)
1007a71acad9SOleksij Rempel {
1008a71acad9SOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(ndev);
1009a71acad9SOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1010a71acad9SOleksij Rempel 
1011a71acad9SOleksij Rempel 	if (ds->ops->self_test) {
1012a71acad9SOleksij Rempel 		ds->ops->self_test(ds, dp->index, etest, buf);
1013a71acad9SOleksij Rempel 		return;
1014a71acad9SOleksij Rempel 	}
1015a71acad9SOleksij Rempel 
1016a71acad9SOleksij Rempel 	net_selftest(ndev, etest, buf);
1017a71acad9SOleksij Rempel }
1018a71acad9SOleksij Rempel 
101919e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
102019e57c4eSFlorian Fainelli {
1021d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1022d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
102319e57c4eSFlorian Fainelli 
1024aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
1025aab9c406SFlorian Fainelli 
10269d490b4eSVivien Didelot 	if (ds->ops->get_wol)
1027d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
102819e57c4eSFlorian Fainelli }
102919e57c4eSFlorian Fainelli 
103019e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
103119e57c4eSFlorian Fainelli {
1032d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1033d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
103419e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
103519e57c4eSFlorian Fainelli 
1036aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
1037aab9c406SFlorian Fainelli 
10389d490b4eSVivien Didelot 	if (ds->ops->set_wol)
1039d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
104019e57c4eSFlorian Fainelli 
104119e57c4eSFlorian Fainelli 	return ret;
104219e57c4eSFlorian Fainelli }
104319e57c4eSFlorian Fainelli 
10447905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
10457905288fSFlorian Fainelli {
1046d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1047d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
10487905288fSFlorian Fainelli 	int ret;
10497905288fSFlorian Fainelli 
10507b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
105100670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
10527b9cc738SVivien Didelot 		return -ENODEV;
10537b9cc738SVivien Didelot 
105408f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
10557905288fSFlorian Fainelli 		return -EOPNOTSUPP;
10567905288fSFlorian Fainelli 
1057d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
10587905288fSFlorian Fainelli 	if (ret)
10597905288fSFlorian Fainelli 		return ret;
10607905288fSFlorian Fainelli 
1061aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
10627905288fSFlorian Fainelli }
10637905288fSFlorian Fainelli 
10647905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
10657905288fSFlorian Fainelli {
1066d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1067d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
10687905288fSFlorian Fainelli 	int ret;
10697905288fSFlorian Fainelli 
10707b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
107100670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
10727b9cc738SVivien Didelot 		return -ENODEV;
10737b9cc738SVivien Didelot 
107408f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
10757905288fSFlorian Fainelli 		return -EOPNOTSUPP;
10767905288fSFlorian Fainelli 
1077d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
10787905288fSFlorian Fainelli 	if (ret)
10797905288fSFlorian Fainelli 		return ret;
10807905288fSFlorian Fainelli 
1081aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
1082aab9c406SFlorian Fainelli }
1083aab9c406SFlorian Fainelli 
1084aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
1085aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
1086aab9c406SFlorian Fainelli {
1087aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1088aab9c406SFlorian Fainelli 
1089aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
1090aab9c406SFlorian Fainelli }
1091aab9c406SFlorian Fainelli 
1092aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
1093aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
1094aab9c406SFlorian Fainelli {
1095aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1096aab9c406SFlorian Fainelli 
1097aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
10987905288fSFlorian Fainelli }
10997905288fSFlorian Fainelli 
1100a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
1101a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
1102a2a1a13bSHeiner Kallweit {
1103a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
1104a2a1a13bSHeiner Kallweit 
1105a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
1106a2a1a13bSHeiner Kallweit }
1107a2a1a13bSHeiner Kallweit 
1108a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
1109a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
1110a2a1a13bSHeiner Kallweit {
1111a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
1112a2a1a13bSHeiner Kallweit 
1113a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
1114a2a1a13bSHeiner Kallweit }
1115a2a1a13bSHeiner Kallweit 
111604ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
111704ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
111804ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
111904ff53f9SFlorian Fainelli {
1120d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
112104ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
112204ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
112304ff53f9SFlorian Fainelli 	int err = 0;
112404ff53f9SFlorian Fainelli 
112504ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
112604ff53f9SFlorian Fainelli 	if (!netpoll)
112704ff53f9SFlorian Fainelli 		return -ENOMEM;
112804ff53f9SFlorian Fainelli 
112904ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
113004ff53f9SFlorian Fainelli 	if (err) {
113104ff53f9SFlorian Fainelli 		kfree(netpoll);
113204ff53f9SFlorian Fainelli 		goto out;
113304ff53f9SFlorian Fainelli 	}
113404ff53f9SFlorian Fainelli 
113504ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
113604ff53f9SFlorian Fainelli out:
113704ff53f9SFlorian Fainelli 	return err;
113804ff53f9SFlorian Fainelli }
113904ff53f9SFlorian Fainelli 
114004ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
114104ff53f9SFlorian Fainelli {
114204ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
114304ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
114404ff53f9SFlorian Fainelli 
114504ff53f9SFlorian Fainelli 	if (!netpoll)
114604ff53f9SFlorian Fainelli 		return;
114704ff53f9SFlorian Fainelli 
114804ff53f9SFlorian Fainelli 	p->netpoll = NULL;
114904ff53f9SFlorian Fainelli 
1150c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
115104ff53f9SFlorian Fainelli }
115204ff53f9SFlorian Fainelli 
115304ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
115404ff53f9SFlorian Fainelli {
115504ff53f9SFlorian Fainelli }
115604ff53f9SFlorian Fainelli #endif
115704ff53f9SFlorian Fainelli 
1158f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
11594fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1160f50f2127SFlorian Fainelli {
11614fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
1162f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1163f50f2127SFlorian Fainelli 
1164f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1165f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1166f50f2127SFlorian Fainelli 			return mall_tc_entry;
1167f50f2127SFlorian Fainelli 
1168f50f2127SFlorian Fainelli 	return NULL;
1169f50f2127SFlorian Fainelli }
1170f50f2127SFlorian Fainelli 
1171e13c2075SVladimir Oltean static int
1172e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1173f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
1174f50f2127SFlorian Fainelli 				  bool ingress)
1175f50f2127SFlorian Fainelli {
11760148bb50SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
1177d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1178f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1179e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
1180f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1181d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
11829681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
1183d945097bSVivien Didelot 	struct dsa_port *to_dp;
1184e13c2075SVladimir Oltean 	int err;
1185e13c2075SVladimir Oltean 
1186f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
118734297176SVladimir Oltean 		return -EOPNOTSUPP;
1188f50f2127SFlorian Fainelli 
118953eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1190319a1d19SJiri Pirko 					      cls->common.extack))
119134297176SVladimir Oltean 		return -EOPNOTSUPP;
1192319a1d19SJiri Pirko 
11939681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
1194f50f2127SFlorian Fainelli 
119565722159SVladimir Oltean 	if (!act->dev)
119665722159SVladimir Oltean 		return -EINVAL;
119765722159SVladimir Oltean 
11989681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
1199f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
1200f50f2127SFlorian Fainelli 
1201f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1202f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1203f50f2127SFlorian Fainelli 		return -ENOMEM;
1204f50f2127SFlorian Fainelli 
1205f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
1206f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1207f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
1208f50f2127SFlorian Fainelli 
12099681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
1210f50f2127SFlorian Fainelli 
1211d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
1212f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
1213f50f2127SFlorian Fainelli 
12140148bb50SVladimir Oltean 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1215f50f2127SFlorian Fainelli 	if (err) {
1216f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
1217f50f2127SFlorian Fainelli 		return err;
1218f50f2127SFlorian Fainelli 	}
1219f50f2127SFlorian Fainelli 
1220f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1221e13c2075SVladimir Oltean 
1222e13c2075SVladimir Oltean 	return err;
1223f50f2127SFlorian Fainelli }
1224f50f2127SFlorian Fainelli 
122534297176SVladimir Oltean static int
122634297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
122734297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
122834297176SVladimir Oltean 				  bool ingress)
122934297176SVladimir Oltean {
123034297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
123134297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
123234297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
123334297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
123434297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
123534297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
123634297176SVladimir Oltean 	struct flow_action_entry *act;
123734297176SVladimir Oltean 	int err;
123834297176SVladimir Oltean 
123934297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
124034297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1241c75a33c8SJacob Keller 				   "Policing offload not implemented");
124234297176SVladimir Oltean 		return -EOPNOTSUPP;
124334297176SVladimir Oltean 	}
124434297176SVladimir Oltean 
124534297176SVladimir Oltean 	if (!ingress) {
124634297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1247c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
124834297176SVladimir Oltean 		return -EOPNOTSUPP;
124934297176SVladimir Oltean 	}
125034297176SVladimir Oltean 
125134297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
125234297176SVladimir Oltean 					      cls->common.extack))
125334297176SVladimir Oltean 		return -EOPNOTSUPP;
125434297176SVladimir Oltean 
125534297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
125634297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
125734297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1258c75a33c8SJacob Keller 					   "Only one port policer allowed");
125934297176SVladimir Oltean 			return -EEXIST;
126034297176SVladimir Oltean 		}
126134297176SVladimir Oltean 	}
126234297176SVladimir Oltean 
126334297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
126434297176SVladimir Oltean 
126534297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
126634297176SVladimir Oltean 	if (!mall_tc_entry)
126734297176SVladimir Oltean 		return -ENOMEM;
126834297176SVladimir Oltean 
126934297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
127034297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
127134297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
127234297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
127334297176SVladimir Oltean 	policer->burst = act->police.burst;
127434297176SVladimir Oltean 
127534297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
127634297176SVladimir Oltean 	if (err) {
127734297176SVladimir Oltean 		kfree(mall_tc_entry);
127834297176SVladimir Oltean 		return err;
127934297176SVladimir Oltean 	}
128034297176SVladimir Oltean 
128134297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
128234297176SVladimir Oltean 
128334297176SVladimir Oltean 	return err;
128434297176SVladimir Oltean }
128534297176SVladimir Oltean 
1286e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1287e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1288e13c2075SVladimir Oltean 				      bool ingress)
1289e13c2075SVladimir Oltean {
1290e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1291e13c2075SVladimir Oltean 
1292e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1293e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1294e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1295e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
129634297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
129734297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
129834297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1299e13c2075SVladimir Oltean 
1300e13c2075SVladimir Oltean 	return err;
1301f50f2127SFlorian Fainelli }
1302f50f2127SFlorian Fainelli 
1303f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1304f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1305f50f2127SFlorian Fainelli {
1306d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1307f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1308d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1309f50f2127SFlorian Fainelli 
13104fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1311f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1312f50f2127SFlorian Fainelli 		return;
1313f50f2127SFlorian Fainelli 
1314f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1315f50f2127SFlorian Fainelli 
1316f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1317f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
131834297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
131934297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
132034297176SVladimir Oltean 						 &mall_tc_entry->mirror);
132134297176SVladimir Oltean 		break;
132234297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
132334297176SVladimir Oltean 		if (ds->ops->port_policer_del)
132434297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1325f50f2127SFlorian Fainelli 		break;
1326f50f2127SFlorian Fainelli 	default:
1327f50f2127SFlorian Fainelli 		WARN_ON(1);
1328f50f2127SFlorian Fainelli 	}
1329f50f2127SFlorian Fainelli 
1330f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1331f50f2127SFlorian Fainelli }
1332f50f2127SFlorian Fainelli 
13333fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
13346b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
13356b3eb752SJiri Pirko 					   bool ingress)
1336f50f2127SFlorian Fainelli {
13375fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1338a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1339f50f2127SFlorian Fainelli 
13403fbae382SJiri Pirko 	switch (cls->command) {
13413fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
13425fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
13433fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
13443fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
13453fbae382SJiri Pirko 		return 0;
13463fbae382SJiri Pirko 	default:
13473fbae382SJiri Pirko 		return -EOPNOTSUPP;
13483fbae382SJiri Pirko 	}
13493fbae382SJiri Pirko }
13503fbae382SJiri Pirko 
1351ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1352ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1353ed11bb1fSVladimir Oltean 				    bool ingress)
1354ed11bb1fSVladimir Oltean {
1355ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1356ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1357ed11bb1fSVladimir Oltean 	int port = dp->index;
1358ed11bb1fSVladimir Oltean 
1359ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1360ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1361ed11bb1fSVladimir Oltean 
1362ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1363ed11bb1fSVladimir Oltean }
1364ed11bb1fSVladimir Oltean 
1365ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1366ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1367ed11bb1fSVladimir Oltean 				    bool ingress)
1368ed11bb1fSVladimir Oltean {
1369ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1370ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1371ed11bb1fSVladimir Oltean 	int port = dp->index;
1372ed11bb1fSVladimir Oltean 
1373ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1374ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1375ed11bb1fSVladimir Oltean 
1376ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1377ed11bb1fSVladimir Oltean }
1378ed11bb1fSVladimir Oltean 
1379ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1380ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1381ed11bb1fSVladimir Oltean 				      bool ingress)
1382ed11bb1fSVladimir Oltean {
1383ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1384ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1385ed11bb1fSVladimir Oltean 	int port = dp->index;
1386ed11bb1fSVladimir Oltean 
1387ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1388ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1389ed11bb1fSVladimir Oltean 
1390ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1391ed11bb1fSVladimir Oltean }
1392ed11bb1fSVladimir Oltean 
1393ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1394ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1395ed11bb1fSVladimir Oltean 					 bool ingress)
1396ed11bb1fSVladimir Oltean {
1397ed11bb1fSVladimir Oltean 	switch (cls->command) {
1398ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1399ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1400ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1401ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1402ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1403ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1404ed11bb1fSVladimir Oltean 	default:
1405ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1406ed11bb1fSVladimir Oltean 	}
1407ed11bb1fSVladimir Oltean }
1408ed11bb1fSVladimir Oltean 
14096b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14106b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
14116b3eb752SJiri Pirko {
14126b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
14136b3eb752SJiri Pirko 
141444ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
141544ae12a7SJiri Pirko 		return -EOPNOTSUPP;
141644ae12a7SJiri Pirko 
14176b3eb752SJiri Pirko 	switch (type) {
14186b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
14196b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1420ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1421ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
14226b3eb752SJiri Pirko 	default:
14236b3eb752SJiri Pirko 		return -EOPNOTSUPP;
14246b3eb752SJiri Pirko 	}
14256b3eb752SJiri Pirko }
14266b3eb752SJiri Pirko 
14276b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
14286b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
14296b3eb752SJiri Pirko {
14306b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
14316b3eb752SJiri Pirko }
14326b3eb752SJiri Pirko 
14336b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
14346b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
14356b3eb752SJiri Pirko {
14366b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
14376b3eb752SJiri Pirko }
14386b3eb752SJiri Pirko 
1439955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1440955bcb6eSPablo Neira Ayuso 
14416b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1442955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
14436b3eb752SJiri Pirko {
1444955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1445a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
14466b3eb752SJiri Pirko 
144732f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
14486b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
144932f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
14506b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
14516b3eb752SJiri Pirko 	else
14526b3eb752SJiri Pirko 		return -EOPNOTSUPP;
14536b3eb752SJiri Pirko 
1454955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1455955bcb6eSPablo Neira Ayuso 
14566b3eb752SJiri Pirko 	switch (f->command) {
14579c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
14580d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
14590d4fd02eSPablo Neira Ayuso 			return -EBUSY;
14600d4fd02eSPablo Neira Ayuso 
14610c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1462955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1463955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1464955bcb6eSPablo Neira Ayuso 
1465955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1466955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1467955bcb6eSPablo Neira Ayuso 		return 0;
14689c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
146914bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1470955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1471955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1472955bcb6eSPablo Neira Ayuso 
1473955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1474955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
14756b3eb752SJiri Pirko 		return 0;
14766b3eb752SJiri Pirko 	default:
14776b3eb752SJiri Pirko 		return -EOPNOTSUPP;
14786b3eb752SJiri Pirko 	}
14796b3eb752SJiri Pirko }
14806b3eb752SJiri Pirko 
14813fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
14823fb24a43SPablo Neira Ayuso 				    void *type_data)
14833fb24a43SPablo Neira Ayuso {
14843fb24a43SPablo Neira Ayuso 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
14853fb24a43SPablo Neira Ayuso 	struct net_device *master = cpu_dp->master;
14863fb24a43SPablo Neira Ayuso 
14873fb24a43SPablo Neira Ayuso 	if (!master->netdev_ops->ndo_setup_tc)
14883fb24a43SPablo Neira Ayuso 		return -EOPNOTSUPP;
14893fb24a43SPablo Neira Ayuso 
14903fb24a43SPablo Neira Ayuso 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
14913fb24a43SPablo Neira Ayuso }
14923fb24a43SPablo Neira Ayuso 
14933fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1494de4784caSJiri Pirko 			      void *type_data)
14953fbae382SJiri Pirko {
149647d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
149747d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
149847d23af2SVladimir Oltean 
14993fb24a43SPablo Neira Ayuso 	switch (type) {
15003fb24a43SPablo Neira Ayuso 	case TC_SETUP_BLOCK:
15016b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
15023fb24a43SPablo Neira Ayuso 	case TC_SETUP_FT:
15033fb24a43SPablo Neira Ayuso 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
15043fb24a43SPablo Neira Ayuso 	default:
15053fb24a43SPablo Neira Ayuso 		break;
15063fb24a43SPablo Neira Ayuso 	}
150747d23af2SVladimir Oltean 
150847d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1509a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
151047d23af2SVladimir Oltean 
151147d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1512f50f2127SFlorian Fainelli }
1513f50f2127SFlorian Fainelli 
1514bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1515bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1516bf9f2648SFlorian Fainelli {
1517d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1518d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1519bf9f2648SFlorian Fainelli 
1520bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1521bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1522bf9f2648SFlorian Fainelli 
1523d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1524bf9f2648SFlorian Fainelli }
1525bf9f2648SFlorian Fainelli 
1526bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1527bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1528bf9f2648SFlorian Fainelli {
1529d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1530d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1531bf9f2648SFlorian Fainelli 
1532bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1533bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1534bf9f2648SFlorian Fainelli 
1535d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1536bf9f2648SFlorian Fainelli }
1537bf9f2648SFlorian Fainelli 
15380336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
15390336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
15400336369dSBrandon Streiff {
15410336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
15420336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
15430336369dSBrandon Streiff 
15440336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
15450336369dSBrandon Streiff 		return -EOPNOTSUPP;
15460336369dSBrandon Streiff 
15470336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
15480336369dSBrandon Streiff }
15490336369dSBrandon Streiff 
1550061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1551061f6a50SFlorian Fainelli 				     u16 vid)
1552061f6a50SFlorian Fainelli {
1553061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
155488236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
155588236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1556b7a9e0daSVladimir Oltean 		.vid = vid,
155788236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
155888236591SVladimir Oltean 		.flags = 0,
155988236591SVladimir Oltean 	};
156031046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1561061f6a50SFlorian Fainelli 	int ret;
1562061f6a50SFlorian Fainelli 
156388236591SVladimir Oltean 	/* User port... */
156431046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
156531046a5fSVladimir Oltean 	if (ret) {
156631046a5fSVladimir Oltean 		if (extack._msg)
156731046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
156888236591SVladimir Oltean 		return ret;
156931046a5fSVladimir Oltean 	}
157088236591SVladimir Oltean 
157188236591SVladimir Oltean 	/* And CPU port... */
1572134ef238SVladimir Oltean 	ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
157331046a5fSVladimir Oltean 	if (ret) {
157431046a5fSVladimir Oltean 		if (extack._msg)
157531046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
157631046a5fSVladimir Oltean 				   extack._msg);
15777e1741b4SVivien Didelot 		return ret;
157831046a5fSVladimir Oltean 	}
15797e1741b4SVivien Didelot 
1580134ef238SVladimir Oltean 	return 0;
1581061f6a50SFlorian Fainelli }
1582061f6a50SFlorian Fainelli 
1583061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1584061f6a50SFlorian Fainelli 				      u16 vid)
1585061f6a50SFlorian Fainelli {
1586061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
158788236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1588b7a9e0daSVladimir Oltean 		.vid = vid,
158988236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
159088236591SVladimir Oltean 		.flags = 0,
159188236591SVladimir Oltean 	};
15922209158cSVladimir Oltean 	int err;
1593061f6a50SFlorian Fainelli 
15942209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
15952209158cSVladimir Oltean 	if (err)
15962209158cSVladimir Oltean 		return err;
15972209158cSVladimir Oltean 
1598134ef238SVladimir Oltean 	return dsa_port_host_vlan_del(dp, &vlan);
1599061f6a50SFlorian Fainelli }
1600061f6a50SFlorian Fainelli 
160106cfb2dfSVladimir Oltean static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
160206cfb2dfSVladimir Oltean {
160306cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
160406cfb2dfSVladimir Oltean 
160506cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
160606cfb2dfSVladimir Oltean }
160706cfb2dfSVladimir Oltean 
160806cfb2dfSVladimir Oltean static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
160906cfb2dfSVladimir Oltean {
161006cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
161106cfb2dfSVladimir Oltean 
161206cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
161306cfb2dfSVladimir Oltean }
161406cfb2dfSVladimir Oltean 
161506cfb2dfSVladimir Oltean /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
161606cfb2dfSVladimir Oltean  * filtering is enabled. The baseline is that only ports that offload a
161706cfb2dfSVladimir Oltean  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
161806cfb2dfSVladimir Oltean  * but there are exceptions for quirky hardware.
161906cfb2dfSVladimir Oltean  *
162006cfb2dfSVladimir Oltean  * If ds->vlan_filtering_is_global = true, then standalone ports which share
162106cfb2dfSVladimir Oltean  * the same switch with other ports that offload a VLAN-aware bridge are also
162206cfb2dfSVladimir Oltean  * inevitably VLAN-aware.
162306cfb2dfSVladimir Oltean  *
162406cfb2dfSVladimir Oltean  * To summarize, a DSA switch port offloads:
162506cfb2dfSVladimir Oltean  *
162606cfb2dfSVladimir Oltean  * - If standalone (this includes software bridge, software LAG):
162758adf9dcSVladimir Oltean  *     - if ds->needs_standalone_vlan_filtering = true, OR if
162858adf9dcSVladimir Oltean  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
162958adf9dcSVladimir Oltean  *       this switch chip which have vlan_filtering=1)
163006cfb2dfSVladimir Oltean  *         - the 8021q upper VLANs
163158adf9dcSVladimir Oltean  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
163258adf9dcSVladimir Oltean  *       global, or it is, but no port is under a VLAN-aware bridge):
163306cfb2dfSVladimir Oltean  *         - no VLAN (any 8021q upper is a software VLAN)
163406cfb2dfSVladimir Oltean  *
163506cfb2dfSVladimir Oltean  * - If under a vlan_filtering=0 bridge which it offload:
163606cfb2dfSVladimir Oltean  *     - if ds->configure_vlan_while_not_filtering = true (default):
163706cfb2dfSVladimir Oltean  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
163806cfb2dfSVladimir Oltean  *     - else (deprecated):
163906cfb2dfSVladimir Oltean  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
164006cfb2dfSVladimir Oltean  *           enabled, so this behavior is broken and discouraged.
164106cfb2dfSVladimir Oltean  *
164206cfb2dfSVladimir Oltean  * - If under a vlan_filtering=1 bridge which it offload:
164306cfb2dfSVladimir Oltean  *     - the bridge VLANs
164406cfb2dfSVladimir Oltean  *     - the 8021q upper VLANs
164506cfb2dfSVladimir Oltean  */
164606cfb2dfSVladimir Oltean int dsa_slave_manage_vlan_filtering(struct net_device *slave,
164706cfb2dfSVladimir Oltean 				    bool vlan_filtering)
164806cfb2dfSVladimir Oltean {
164906cfb2dfSVladimir Oltean 	int err;
165006cfb2dfSVladimir Oltean 
165106cfb2dfSVladimir Oltean 	if (vlan_filtering) {
165206cfb2dfSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
165306cfb2dfSVladimir Oltean 
165406cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
165506cfb2dfSVladimir Oltean 		if (err) {
165606cfb2dfSVladimir Oltean 			vlan_for_each(slave, dsa_slave_clear_vlan, slave);
165706cfb2dfSVladimir Oltean 			slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
165806cfb2dfSVladimir Oltean 			return err;
165906cfb2dfSVladimir Oltean 		}
166006cfb2dfSVladimir Oltean 	} else {
166106cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
166206cfb2dfSVladimir Oltean 		if (err)
166306cfb2dfSVladimir Oltean 			return err;
166406cfb2dfSVladimir Oltean 
166506cfb2dfSVladimir Oltean 		slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
166606cfb2dfSVladimir Oltean 	}
166706cfb2dfSVladimir Oltean 
166806cfb2dfSVladimir Oltean 	return 0;
166906cfb2dfSVladimir Oltean }
167006cfb2dfSVladimir Oltean 
1671bff33f7eSVladimir Oltean struct dsa_hw_port {
1672bff33f7eSVladimir Oltean 	struct list_head list;
1673bff33f7eSVladimir Oltean 	struct net_device *dev;
1674bff33f7eSVladimir Oltean 	int old_mtu;
1675bff33f7eSVladimir Oltean };
1676bff33f7eSVladimir Oltean 
1677bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1678bff33f7eSVladimir Oltean {
1679bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1680bff33f7eSVladimir Oltean 	int err;
1681bff33f7eSVladimir Oltean 
1682bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1683bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1684bff33f7eSVladimir Oltean 			continue;
1685bff33f7eSVladimir Oltean 
1686bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1687bff33f7eSVladimir Oltean 		if (err)
1688bff33f7eSVladimir Oltean 			goto rollback;
1689bff33f7eSVladimir Oltean 	}
1690bff33f7eSVladimir Oltean 
1691bff33f7eSVladimir Oltean 	return 0;
1692bff33f7eSVladimir Oltean 
1693bff33f7eSVladimir Oltean rollback:
1694bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1695bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1696bff33f7eSVladimir Oltean 			continue;
1697bff33f7eSVladimir Oltean 
1698bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1699bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1700bff33f7eSVladimir Oltean 	}
1701bff33f7eSVladimir Oltean 
1702bff33f7eSVladimir Oltean 	return err;
1703bff33f7eSVladimir Oltean }
1704bff33f7eSVladimir Oltean 
1705bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1706bff33f7eSVladimir Oltean {
1707bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1708bff33f7eSVladimir Oltean 
1709bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1710bff33f7eSVladimir Oltean 		kfree(p);
1711bff33f7eSVladimir Oltean }
1712bff33f7eSVladimir Oltean 
1713bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1714bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1715bff33f7eSVladimir Oltean {
1716bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1717bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1718bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1719bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1720bff33f7eSVladimir Oltean 	int err;
1721bff33f7eSVladimir Oltean 
1722bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1723bff33f7eSVladimir Oltean 		return;
1724bff33f7eSVladimir Oltean 
1725d3eed0e5SVladimir Oltean 	if (!dp->bridge)
1726bff33f7eSVladimir Oltean 		return;
1727bff33f7eSVladimir Oltean 
1728bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1729bff33f7eSVladimir Oltean 
1730bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1731bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1732bff33f7eSVladimir Oltean 	 */
1733bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1734bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1735bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1736bff33f7eSVladimir Oltean 			struct net_device *slave;
1737bff33f7eSVladimir Oltean 
1738bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1739bff33f7eSVladimir Oltean 				continue;
1740bff33f7eSVladimir Oltean 
174136cbf39bSVladimir Oltean 			if (!dsa_port_bridge_same(dp, other_dp))
1742bff33f7eSVladimir Oltean 				continue;
1743bff33f7eSVladimir Oltean 
1744bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1745bff33f7eSVladimir Oltean 				continue;
1746bff33f7eSVladimir Oltean 
1747bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1748bff33f7eSVladimir Oltean 
1749bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1750bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1751bff33f7eSVladimir Oltean 
1752bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1753bff33f7eSVladimir Oltean 			if (!hw_port)
1754bff33f7eSVladimir Oltean 				goto out;
1755bff33f7eSVladimir Oltean 
1756bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1757bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1758bff33f7eSVladimir Oltean 
1759bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1760bff33f7eSVladimir Oltean 		}
1761bff33f7eSVladimir Oltean 	}
1762bff33f7eSVladimir Oltean 
1763bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1764bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1765bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1766bff33f7eSVladimir Oltean 	 */
1767bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1768bff33f7eSVladimir Oltean 	if (!err)
1769bff33f7eSVladimir Oltean 		goto out;
1770bff33f7eSVladimir Oltean 
1771bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1772bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1773bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1774bff33f7eSVladimir Oltean 	 */
1775bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1776bff33f7eSVladimir Oltean 
1777bff33f7eSVladimir Oltean out:
1778bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1779bff33f7eSVladimir Oltean }
1780bff33f7eSVladimir Oltean 
178153da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1782bfcb8132SVladimir Oltean {
1783bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1784bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1785cf1c39d3SVladimir Oltean 	struct dsa_port *cpu_dp = dp->cpu_dp;
17864715029fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1787b2033a05SVladimir Oltean 	struct dsa_port *other_dp;
1788bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1789bfcb8132SVladimir Oltean 	int new_master_mtu;
1790bfcb8132SVladimir Oltean 	int old_master_mtu;
1791bfcb8132SVladimir Oltean 	int mtu_limit;
1792bfcb8132SVladimir Oltean 	int cpu_mtu;
17934e4ab795SVladimir Oltean 	int err;
1794bfcb8132SVladimir Oltean 
1795bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1796bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1797bfcb8132SVladimir Oltean 
1798b2033a05SVladimir Oltean 	dsa_tree_for_each_user_port(other_dp, ds->dst) {
1799bfcb8132SVladimir Oltean 		int slave_mtu;
1800bfcb8132SVladimir Oltean 
1801bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1802bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1803bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1804bfcb8132SVladimir Oltean 		 */
1805b2033a05SVladimir Oltean 		if (!other_dp->slave)
1806bfcb8132SVladimir Oltean 			continue;
1807bfcb8132SVladimir Oltean 
1808bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1809bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1810bfcb8132SVladimir Oltean 		 */
1811b2033a05SVladimir Oltean 		if (dp == other_dp)
1812bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1813bfcb8132SVladimir Oltean 		else
1814b2033a05SVladimir Oltean 			slave_mtu = other_dp->slave->mtu;
1815bfcb8132SVladimir Oltean 
1816bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1817bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1818bfcb8132SVladimir Oltean 	}
1819bfcb8132SVladimir Oltean 
1820bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1821bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
18224e500251SVladimir Oltean 	new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1823bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1824bfcb8132SVladimir Oltean 		return -ERANGE;
1825bfcb8132SVladimir Oltean 
1826bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1827bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1828bfcb8132SVladimir Oltean 	 */
1829bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1830bfcb8132SVladimir Oltean 
1831bfcb8132SVladimir Oltean 	/* Start applying stuff */
1832bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1833bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1834bfcb8132SVladimir Oltean 		if (err < 0)
1835bfcb8132SVladimir Oltean 			goto out_master_failed;
1836bfcb8132SVladimir Oltean 
1837bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
1838be6ff966SVladimir Oltean 		 * upstream switches, so emit a notifier which updates them.
1839bfcb8132SVladimir Oltean 		 */
1840be6ff966SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
1841bfcb8132SVladimir Oltean 		if (err)
1842bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1843bfcb8132SVladimir Oltean 	}
1844bfcb8132SVladimir Oltean 
1845be6ff966SVladimir Oltean 	err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
1846bfcb8132SVladimir Oltean 	if (err)
1847bfcb8132SVladimir Oltean 		goto out_port_failed;
1848bfcb8132SVladimir Oltean 
1849bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1850bfcb8132SVladimir Oltean 
1851bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1852bff33f7eSVladimir Oltean 
1853bfcb8132SVladimir Oltean 	return 0;
1854bfcb8132SVladimir Oltean 
1855bfcb8132SVladimir Oltean out_port_failed:
1856bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1857bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
1858be6ff966SVladimir Oltean 				    dsa_tag_protocol_overhead(cpu_dp->tag_ops));
1859bfcb8132SVladimir Oltean out_cpu_failed:
1860bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1861bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1862bfcb8132SVladimir Oltean out_master_failed:
1863bfcb8132SVladimir Oltean 	return err;
1864bfcb8132SVladimir Oltean }
1865bfcb8132SVladimir Oltean 
1866d538eca8SVladimir Oltean static int __maybe_unused
1867d538eca8SVladimir Oltean dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
1868d538eca8SVladimir Oltean {
1869d538eca8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1870d538eca8SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1871d538eca8SVladimir Oltean 	unsigned long mask, new_prio;
1872d538eca8SVladimir Oltean 	int err, port = dp->index;
1873d538eca8SVladimir Oltean 
1874d538eca8SVladimir Oltean 	if (!ds->ops->port_set_default_prio)
1875d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
1876d538eca8SVladimir Oltean 
1877d538eca8SVladimir Oltean 	err = dcb_ieee_setapp(dev, app);
1878d538eca8SVladimir Oltean 	if (err)
1879d538eca8SVladimir Oltean 		return err;
1880d538eca8SVladimir Oltean 
1881d538eca8SVladimir Oltean 	mask = dcb_ieee_getapp_mask(dev, app);
1882d538eca8SVladimir Oltean 	new_prio = __fls(mask);
1883d538eca8SVladimir Oltean 
1884d538eca8SVladimir Oltean 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
1885d538eca8SVladimir Oltean 	if (err) {
1886d538eca8SVladimir Oltean 		dcb_ieee_delapp(dev, app);
1887d538eca8SVladimir Oltean 		return err;
1888d538eca8SVladimir Oltean 	}
1889d538eca8SVladimir Oltean 
1890d538eca8SVladimir Oltean 	return 0;
1891d538eca8SVladimir Oltean }
1892d538eca8SVladimir Oltean 
189347d75f78SVladimir Oltean static int __maybe_unused
189447d75f78SVladimir Oltean dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
189547d75f78SVladimir Oltean {
189647d75f78SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
189747d75f78SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
189847d75f78SVladimir Oltean 	unsigned long mask, new_prio;
189947d75f78SVladimir Oltean 	int err, port = dp->index;
190047d75f78SVladimir Oltean 	u8 dscp = app->protocol;
190147d75f78SVladimir Oltean 
190247d75f78SVladimir Oltean 	if (!ds->ops->port_add_dscp_prio)
190347d75f78SVladimir Oltean 		return -EOPNOTSUPP;
190447d75f78SVladimir Oltean 
190547d75f78SVladimir Oltean 	if (dscp >= 64) {
190647d75f78SVladimir Oltean 		netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
190747d75f78SVladimir Oltean 			   dscp);
190847d75f78SVladimir Oltean 		return -EINVAL;
190947d75f78SVladimir Oltean 	}
191047d75f78SVladimir Oltean 
191147d75f78SVladimir Oltean 	err = dcb_ieee_setapp(dev, app);
191247d75f78SVladimir Oltean 	if (err)
191347d75f78SVladimir Oltean 		return err;
191447d75f78SVladimir Oltean 
191547d75f78SVladimir Oltean 	mask = dcb_ieee_getapp_mask(dev, app);
191647d75f78SVladimir Oltean 	new_prio = __fls(mask);
191747d75f78SVladimir Oltean 
191847d75f78SVladimir Oltean 	err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
191947d75f78SVladimir Oltean 	if (err) {
192047d75f78SVladimir Oltean 		dcb_ieee_delapp(dev, app);
192147d75f78SVladimir Oltean 		return err;
192247d75f78SVladimir Oltean 	}
192347d75f78SVladimir Oltean 
192447d75f78SVladimir Oltean 	return 0;
192547d75f78SVladimir Oltean }
192647d75f78SVladimir Oltean 
1927d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev,
1928d538eca8SVladimir Oltean 						      struct dcb_app *app)
1929d538eca8SVladimir Oltean {
1930d538eca8SVladimir Oltean 	switch (app->selector) {
1931d538eca8SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
1932d538eca8SVladimir Oltean 		switch (app->protocol) {
1933d538eca8SVladimir Oltean 		case 0:
1934d538eca8SVladimir Oltean 			return dsa_slave_dcbnl_set_default_prio(dev, app);
1935d538eca8SVladimir Oltean 		default:
1936d538eca8SVladimir Oltean 			return -EOPNOTSUPP;
1937d538eca8SVladimir Oltean 		}
1938d538eca8SVladimir Oltean 		break;
193947d75f78SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_DSCP:
194047d75f78SVladimir Oltean 		return dsa_slave_dcbnl_add_dscp_prio(dev, app);
1941d538eca8SVladimir Oltean 	default:
1942d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
1943d538eca8SVladimir Oltean 	}
1944d538eca8SVladimir Oltean }
1945d538eca8SVladimir Oltean 
1946d538eca8SVladimir Oltean static int __maybe_unused
1947d538eca8SVladimir Oltean dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
1948d538eca8SVladimir Oltean {
1949d538eca8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1950d538eca8SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1951d538eca8SVladimir Oltean 	unsigned long mask, new_prio;
1952d538eca8SVladimir Oltean 	int err, port = dp->index;
1953d538eca8SVladimir Oltean 
1954d538eca8SVladimir Oltean 	if (!ds->ops->port_set_default_prio)
1955d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
1956d538eca8SVladimir Oltean 
1957d538eca8SVladimir Oltean 	err = dcb_ieee_delapp(dev, app);
1958d538eca8SVladimir Oltean 	if (err)
1959d538eca8SVladimir Oltean 		return err;
1960d538eca8SVladimir Oltean 
1961d538eca8SVladimir Oltean 	mask = dcb_ieee_getapp_mask(dev, app);
1962d538eca8SVladimir Oltean 	new_prio = mask ? __fls(mask) : 0;
1963d538eca8SVladimir Oltean 
1964d538eca8SVladimir Oltean 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
1965d538eca8SVladimir Oltean 	if (err) {
1966d538eca8SVladimir Oltean 		dcb_ieee_setapp(dev, app);
1967d538eca8SVladimir Oltean 		return err;
1968d538eca8SVladimir Oltean 	}
1969d538eca8SVladimir Oltean 
1970d538eca8SVladimir Oltean 	return 0;
1971d538eca8SVladimir Oltean }
1972d538eca8SVladimir Oltean 
197347d75f78SVladimir Oltean static int __maybe_unused
197447d75f78SVladimir Oltean dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
197547d75f78SVladimir Oltean {
197647d75f78SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
197747d75f78SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
197847d75f78SVladimir Oltean 	int err, port = dp->index;
197947d75f78SVladimir Oltean 	u8 dscp = app->protocol;
198047d75f78SVladimir Oltean 
198147d75f78SVladimir Oltean 	if (!ds->ops->port_del_dscp_prio)
198247d75f78SVladimir Oltean 		return -EOPNOTSUPP;
198347d75f78SVladimir Oltean 
198447d75f78SVladimir Oltean 	err = dcb_ieee_delapp(dev, app);
198547d75f78SVladimir Oltean 	if (err)
198647d75f78SVladimir Oltean 		return err;
198747d75f78SVladimir Oltean 
198847d75f78SVladimir Oltean 	err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
198947d75f78SVladimir Oltean 	if (err) {
199047d75f78SVladimir Oltean 		dcb_ieee_setapp(dev, app);
199147d75f78SVladimir Oltean 		return err;
199247d75f78SVladimir Oltean 	}
199347d75f78SVladimir Oltean 
199447d75f78SVladimir Oltean 	return 0;
199547d75f78SVladimir Oltean }
199647d75f78SVladimir Oltean 
1997d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev,
1998d538eca8SVladimir Oltean 						      struct dcb_app *app)
1999d538eca8SVladimir Oltean {
2000d538eca8SVladimir Oltean 	switch (app->selector) {
2001d538eca8SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2002d538eca8SVladimir Oltean 		switch (app->protocol) {
2003d538eca8SVladimir Oltean 		case 0:
2004d538eca8SVladimir Oltean 			return dsa_slave_dcbnl_del_default_prio(dev, app);
2005d538eca8SVladimir Oltean 		default:
2006d538eca8SVladimir Oltean 			return -EOPNOTSUPP;
2007d538eca8SVladimir Oltean 		}
2008d538eca8SVladimir Oltean 		break;
200947d75f78SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_DSCP:
201047d75f78SVladimir Oltean 		return dsa_slave_dcbnl_del_dscp_prio(dev, app);
2011d538eca8SVladimir Oltean 	default:
2012d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
2013d538eca8SVladimir Oltean 	}
2014d538eca8SVladimir Oltean }
2015d538eca8SVladimir Oltean 
2016d538eca8SVladimir Oltean /* Pre-populate the DCB application priority table with the priorities
2017d538eca8SVladimir Oltean  * configured during switch setup, which we read from hardware here.
2018d538eca8SVladimir Oltean  */
2019d538eca8SVladimir Oltean static int dsa_slave_dcbnl_init(struct net_device *dev)
2020d538eca8SVladimir Oltean {
2021d538eca8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2022d538eca8SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
2023d538eca8SVladimir Oltean 	int port = dp->index;
2024d538eca8SVladimir Oltean 	int err;
2025d538eca8SVladimir Oltean 
2026d538eca8SVladimir Oltean 	if (ds->ops->port_get_default_prio) {
2027d538eca8SVladimir Oltean 		int prio = ds->ops->port_get_default_prio(ds, port);
2028d538eca8SVladimir Oltean 		struct dcb_app app = {
2029d538eca8SVladimir Oltean 			.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2030d538eca8SVladimir Oltean 			.protocol = 0,
2031d538eca8SVladimir Oltean 			.priority = prio,
2032d538eca8SVladimir Oltean 		};
2033d538eca8SVladimir Oltean 
2034d538eca8SVladimir Oltean 		if (prio < 0)
2035d538eca8SVladimir Oltean 			return prio;
2036d538eca8SVladimir Oltean 
2037d538eca8SVladimir Oltean 		err = dcb_ieee_setapp(dev, &app);
2038d538eca8SVladimir Oltean 		if (err)
2039d538eca8SVladimir Oltean 			return err;
2040d538eca8SVladimir Oltean 	}
2041d538eca8SVladimir Oltean 
204247d75f78SVladimir Oltean 	if (ds->ops->port_get_dscp_prio) {
204347d75f78SVladimir Oltean 		int protocol;
204447d75f78SVladimir Oltean 
204547d75f78SVladimir Oltean 		for (protocol = 0; protocol < 64; protocol++) {
204647d75f78SVladimir Oltean 			struct dcb_app app = {
204747d75f78SVladimir Oltean 				.selector = IEEE_8021QAZ_APP_SEL_DSCP,
204847d75f78SVladimir Oltean 				.protocol = protocol,
204947d75f78SVladimir Oltean 			};
205047d75f78SVladimir Oltean 			int prio;
205147d75f78SVladimir Oltean 
205247d75f78SVladimir Oltean 			prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
205347d75f78SVladimir Oltean 			if (prio == -EOPNOTSUPP)
205447d75f78SVladimir Oltean 				continue;
205547d75f78SVladimir Oltean 			if (prio < 0)
205647d75f78SVladimir Oltean 				return prio;
205747d75f78SVladimir Oltean 
205847d75f78SVladimir Oltean 			app.priority = prio;
205947d75f78SVladimir Oltean 
206047d75f78SVladimir Oltean 			err = dcb_ieee_setapp(dev, &app);
206147d75f78SVladimir Oltean 			if (err)
206247d75f78SVladimir Oltean 				return err;
206347d75f78SVladimir Oltean 		}
206447d75f78SVladimir Oltean 	}
206547d75f78SVladimir Oltean 
2066d538eca8SVladimir Oltean 	return 0;
2067d538eca8SVladimir Oltean }
2068d538eca8SVladimir Oltean 
206991da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
207091da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
20713d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
20723d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
2073aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
2074c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
20756793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
20766793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
20776793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
207891da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
207991da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
208091da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
2081487d3855SAlvin Šipraga 	.get_eth_phy_stats	= dsa_slave_get_eth_phy_stats,
2082487d3855SAlvin Šipraga 	.get_eth_mac_stats	= dsa_slave_get_eth_mac_stats,
2083487d3855SAlvin Šipraga 	.get_eth_ctrl_stats	= dsa_slave_get_eth_ctrl_stats,
208419e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
208519e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
20867905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
20877905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
2088aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
2089aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
2090a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
2091a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
2092bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
2093bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
20940336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
2095a71acad9SOleksij Rempel 	.self_test		= dsa_slave_net_selftest,
209691da11f8SLennert Buytenhek };
209791da11f8SLennert Buytenhek 
2098d538eca8SVladimir Oltean static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = {
2099d538eca8SVladimir Oltean 	.ieee_setapp		= dsa_slave_dcbnl_ieee_setapp,
2100d538eca8SVladimir Oltean 	.ieee_delapp		= dsa_slave_dcbnl_ieee_delapp,
2101d538eca8SVladimir Oltean };
2102d538eca8SVladimir Oltean 
2103716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
2104716efee2SJiri Pirko {
2105716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
2106716efee2SJiri Pirko 
210745b987d5SVladimir Oltean 	return &dp->devlink_port;
2108716efee2SJiri Pirko }
2109716efee2SJiri Pirko 
2110c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
2111c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
2112c2ec5f2eSOleksij Rempel {
2113c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
2114c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
2115c2ec5f2eSOleksij Rempel 
2116c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
2117c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
2118c2ec5f2eSOleksij Rempel 	else
2119c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
2120c2ec5f2eSOleksij Rempel }
2121c2ec5f2eSOleksij Rempel 
21220994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
21230994d492SFelix Fietkau 				       struct net_device_path *path)
21240994d492SFelix Fietkau {
21250994d492SFelix Fietkau 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
21260994d492SFelix Fietkau 	struct dsa_port *cpu_dp = dp->cpu_dp;
21270994d492SFelix Fietkau 
21280994d492SFelix Fietkau 	path->dev = ctx->dev;
21290994d492SFelix Fietkau 	path->type = DEV_PATH_DSA;
21300994d492SFelix Fietkau 	path->dsa.proto = cpu_dp->tag_ops->proto;
21310994d492SFelix Fietkau 	path->dsa.port = dp->index;
21320994d492SFelix Fietkau 	ctx->dev = cpu_dp->master;
21330994d492SFelix Fietkau 
21340994d492SFelix Fietkau 	return 0;
21350994d492SFelix Fietkau }
21360994d492SFelix Fietkau 
21373e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
2138d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
2139d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
21403e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
2141d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
2142d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
2143d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
21442bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
2145a7605370SArnd Bergmann 	.ndo_eth_ioctl		= dsa_slave_ioctl,
2146abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
214704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
214804ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
214904ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
215004ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
215104ff53f9SFlorian Fainelli #endif
2152f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
2153c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
2154061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
2155061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
2156716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
2157bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
21580994d492SFelix Fietkau 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
215998237d43SScott Feldman };
216098237d43SScott Feldman 
2161f37db85dSFlorian Fainelli static struct device_type dsa_type = {
2162f37db85dSFlorian Fainelli 	.name	= "dsa",
2163f37db85dSFlorian Fainelli };
2164f37db85dSFlorian Fainelli 
2165aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2166aab9c406SFlorian Fainelli {
2167aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
2168aab9c406SFlorian Fainelli 
2169765bda93SRussell King 	if (dp->pl)
2170aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
2171aab9c406SFlorian Fainelli }
2172aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2173aab9c406SFlorian Fainelli 
21745c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
2175aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
2176aab9c406SFlorian Fainelli {
21775c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2178aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
2179aab9c406SFlorian Fainelli 
2180aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
2181aab9c406SFlorian Fainelli 	 * not be called if it was not.
2182aab9c406SFlorian Fainelli 	 */
2183aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
2184ce31b31cSFlorian Fainelli }
2185ce31b31cSFlorian Fainelli 
218691da11f8SLennert Buytenhek /* slave device setup *******************************************************/
2187c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
2188c916e8e1SOleksij Rempel 				 u32 flags)
2189c305c165SFlorian Fainelli {
2190d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2191d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
2192c305c165SFlorian Fainelli 
21930115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
21940115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
2195d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
2196c305c165SFlorian Fainelli 		return -ENODEV;
2197d25b8e74SRussell King 	}
2198c305c165SFlorian Fainelli 
2199c916e8e1SOleksij Rempel 	slave_dev->phydev->dev_flags |= flags;
2200c916e8e1SOleksij Rempel 
2201aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
2202c305c165SFlorian Fainelli }
2203c305c165SFlorian Fainelli 
22044fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
22050d8bcdd3SFlorian Fainelli {
2206d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2207d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
2208d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
22096819563eSFlorian Fainelli 	u32 phy_flags = 0;
22100c65b2b9SAndrew Lunn 	int ret;
22110d8bcdd3SFlorian Fainelli 
221244cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
221344cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
221444cc27e4SIoana Ciornei 
22155c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
22165c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
22175c05c1dbSRussell King 	 * this if the switch provides such a callback.
22185c05c1dbSRussell King 	 */
22195c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
22205c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
22215c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
22225c05c1dbSRussell King 	}
22235c05c1dbSRussell King 
222421bd64bdSRussell King (Oracle) 	ret = dsa_port_phylink_create(dp);
222521bd64bdSRussell King (Oracle) 	if (ret)
222621bd64bdSRussell King (Oracle) 		return ret;
2227aab9c406SFlorian Fainelli 
22289d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
2229d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
22306819563eSFlorian Fainelli 
2231aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
22326146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
22336146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
22346146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
22350d8bcdd3SFlorian Fainelli 		 */
2236c916e8e1SOleksij Rempel 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
2237d25b8e74SRussell King 	}
22386a52e733SVladimir Oltean 	if (ret) {
22396a52e733SVladimir Oltean 		netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
22406a52e733SVladimir Oltean 			   ERR_PTR(ret));
22416a52e733SVladimir Oltean 		phylink_destroy(dp->pl);
22420d8bcdd3SFlorian Fainelli 	}
22439697f1cdSFlorian Fainelli 
22446146dd45SVladimir Oltean 	return ret;
2245b31f65fbSAndrew Lunn }
22460d8bcdd3SFlorian Fainelli 
224753da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
224853da0ebaSVladimir Oltean {
224953da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
225053da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
225153da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
225253da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
225358adf9dcSVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
225453da0ebaSVladimir Oltean 
22554e500251SVladimir Oltean 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
22564e500251SVladimir Oltean 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
225753da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
225853da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
225953da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
226053da0ebaSVladimir Oltean 	 */
226153da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
226253da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
226353da0ebaSVladimir Oltean 
226453da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
226521cf377aSLino Sanfilippo 
226621cf377aSLino Sanfilippo 	slave->features = master->vlan_features | NETIF_F_HW_TC;
226721cf377aSLino Sanfilippo 	slave->hw_features |= NETIF_F_HW_TC;
226821cf377aSLino Sanfilippo 	slave->features |= NETIF_F_LLTX;
226921cf377aSLino Sanfilippo 	if (slave->needed_tailroom)
227021cf377aSLino Sanfilippo 		slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
227158adf9dcSVladimir Oltean 	if (ds->needs_standalone_vlan_filtering)
227258adf9dcSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
227353da0ebaSVladimir Oltean }
227453da0ebaSVladimir Oltean 
227524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
227624462549SFlorian Fainelli {
2277aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
227824462549SFlorian Fainelli 
2279a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
2280a94c689eSFlorian Fainelli 		return 0;
2281a94c689eSFlorian Fainelli 
2282f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
2283f154be24SFlorian Fainelli 
2284aab9c406SFlorian Fainelli 	rtnl_lock();
2285aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
2286aab9c406SFlorian Fainelli 	rtnl_unlock();
228724462549SFlorian Fainelli 
228824462549SFlorian Fainelli 	return 0;
228924462549SFlorian Fainelli }
229024462549SFlorian Fainelli 
229124462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
229224462549SFlorian Fainelli {
2293aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2294aab9c406SFlorian Fainelli 
2295a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
2296a94c689eSFlorian Fainelli 		return 0;
2297a94c689eSFlorian Fainelli 
229824462549SFlorian Fainelli 	netif_device_attach(slave_dev);
229924462549SFlorian Fainelli 
2300aab9c406SFlorian Fainelli 	rtnl_lock();
2301aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
2302aab9c406SFlorian Fainelli 	rtnl_unlock();
230324462549SFlorian Fainelli 
230424462549SFlorian Fainelli 	return 0;
230524462549SFlorian Fainelli }
230624462549SFlorian Fainelli 
2307951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
230891da11f8SLennert Buytenhek {
230924a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
2310f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
23114cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
2312951259aaSVivien Didelot 	const char *name = port->name;
231391da11f8SLennert Buytenhek 	struct net_device *slave_dev;
231491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
231591da11f8SLennert Buytenhek 	int ret;
231691da11f8SLennert Buytenhek 
231755199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
231855199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
231955199df6SFlorian Fainelli 
232055199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
232155199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
232255199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
232391da11f8SLennert Buytenhek 	if (slave_dev == NULL)
2324d87d6f44SGuenter Roeck 		return -ENOMEM;
232591da11f8SLennert Buytenhek 
23267ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2327d538eca8SVladimir Oltean #if IS_ENABLED(CONFIG_DCB)
2328d538eca8SVladimir Oltean 	slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops;
2329d538eca8SVladimir Oltean #endif
233083216e39SMichael Walle 	if (!is_zero_ether_addr(port->mac))
2331e35b8d7dSJakub Kicinski 		eth_hw_addr_set(slave_dev, port->mac);
2332a2c7023fSXiaofei Shen 	else
23332fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
23340a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
23355e8a1e03SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
23365e8a1e03SVladimir Oltean 		slave_dev->priv_flags |= IFF_UNICAST_FLT;
23373e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2338bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
2339bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2340f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2341d442ad4aSStephen Hemminger 
23424cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
23434cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
234491da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
234591da11f8SLennert Buytenhek 
234691da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
23476a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
23486a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
23495f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
23505f6b4e14SFlorian Fainelli 		return -ENOMEM;
23515f6b4e14SFlorian Fainelli 	}
2352e131a563SAlexander Lobakin 
2353e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
2354e131a563SAlexander Lobakin 	if (ret)
2355e131a563SAlexander Lobakin 		goto out_free;
2356e131a563SAlexander Lobakin 
23574cfbf09cSVivien Didelot 	p->dp = port;
2358f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
2359f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
236053da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
236191da11f8SLennert Buytenhek 
236291da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
236391da11f8SLennert Buytenhek 
23644fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
23650071f56eSAndrew Lunn 	if (ret) {
2366c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
2367c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2368c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
2369e131a563SAlexander Lobakin 		goto out_gcells;
2370e804441cSFlorian Fainelli 	}
2371e804441cSFlorian Fainelli 
23722f1e8ea7SVladimir Oltean 	rtnl_lock();
2373e31dbd3bSVladimir Oltean 
2374904e112aSVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2375904e112aSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
2376904e112aSVladimir Oltean 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2377904e112aSVladimir Oltean 			 ret, ETH_DATA_LEN, port->index);
2378904e112aSVladimir Oltean 
23792f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
2380e804441cSFlorian Fainelli 	if (ret) {
2381e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
2382e804441cSFlorian Fainelli 			   ret, slave_dev->name);
23832f1e8ea7SVladimir Oltean 		rtnl_unlock();
2384e804441cSFlorian Fainelli 		goto out_phy;
23850071f56eSAndrew Lunn 	}
23860071f56eSAndrew Lunn 
2387d538eca8SVladimir Oltean 	if (IS_ENABLED(CONFIG_DCB)) {
2388d538eca8SVladimir Oltean 		ret = dsa_slave_dcbnl_init(slave_dev);
2389d538eca8SVladimir Oltean 		if (ret) {
2390d538eca8SVladimir Oltean 			netdev_err(slave_dev,
2391d538eca8SVladimir Oltean 				   "failed to initialize DCB: %pe\n",
2392d538eca8SVladimir Oltean 				   ERR_PTR(ret));
2393d538eca8SVladimir Oltean 			rtnl_unlock();
2394d538eca8SVladimir Oltean 			goto out_unregister;
2395d538eca8SVladimir Oltean 		}
2396d538eca8SVladimir Oltean 	}
2397d538eca8SVladimir Oltean 
23982f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
23992f1e8ea7SVladimir Oltean 
24002f1e8ea7SVladimir Oltean 	rtnl_unlock();
24012f1e8ea7SVladimir Oltean 
24022f1e8ea7SVladimir Oltean 	if (ret)
24032f1e8ea7SVladimir Oltean 		goto out_unregister;
24042f1e8ea7SVladimir Oltean 
2405d87d6f44SGuenter Roeck 	return 0;
2406e804441cSFlorian Fainelli 
24072f1e8ea7SVladimir Oltean out_unregister:
24082f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
2409e804441cSFlorian Fainelli out_phy:
2410aab9c406SFlorian Fainelli 	rtnl_lock();
2411aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
2412aab9c406SFlorian Fainelli 	rtnl_unlock();
2413aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
2414e131a563SAlexander Lobakin out_gcells:
2415e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
2416e804441cSFlorian Fainelli out_free:
24176a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2418e804441cSFlorian Fainelli 	free_netdev(slave_dev);
2419f8b8b1cdSVivien Didelot 	port->slave = NULL;
2420e804441cSFlorian Fainelli 	return ret;
242191da11f8SLennert Buytenhek }
2422b73adef6SFlorian Fainelli 
2423cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
2424cda5c15bSNeil Armstrong {
24252f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
2426d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2427cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2428cda5c15bSNeil Armstrong 
2429cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
2430aab9c406SFlorian Fainelli 	rtnl_lock();
24312f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
24322f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
2433aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
2434aab9c406SFlorian Fainelli 	rtnl_unlock();
2435881eadabSJohan Hovold 
2436aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
2437e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
24386a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2439cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
2440cda5c15bSNeil Armstrong }
2441cda5c15bSNeil Armstrong 
24424d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
2443b73adef6SFlorian Fainelli {
2444b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2445b73adef6SFlorian Fainelli }
2446a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2447b73adef6SFlorian Fainelli 
24488e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
24498e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
2450b73adef6SFlorian Fainelli {
2451d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
24522afc526aSVladimir Oltean 	struct netlink_ext_ack *extack;
24538e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
2454b73adef6SFlorian Fainelli 
24552afc526aSVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
24562afc526aSVladimir Oltean 
24578e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
24588e92ab3aSVivien Didelot 		if (info->linking) {
24592afc526aSVladimir Oltean 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2460bff33f7eSVladimir Oltean 			if (!err)
2461bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
246267b5fb5dSVladimir Oltean 			if (err == -EOPNOTSUPP) {
2463*1c6e8088SClément Léger 				if (!extack->_msg)
246467b5fb5dSVladimir Oltean 					NL_SET_ERR_MSG_MOD(extack,
246567b5fb5dSVladimir Oltean 							   "Offloading not supported");
246667b5fb5dSVladimir Oltean 				err = 0;
246767b5fb5dSVladimir Oltean 			}
24688e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
24698e92ab3aSVivien Didelot 		} else {
247017d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
24718e92ab3aSVivien Didelot 			err = NOTIFY_OK;
24728e92ab3aSVivien Didelot 		}
2473058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
2474058102a6STobias Waldekranz 		if (info->linking) {
2475058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
24762afc526aSVladimir Oltean 						info->upper_info, extack);
2477058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
2478058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
2479058102a6STobias Waldekranz 						   "Offloading not supported");
2480058102a6STobias Waldekranz 				err = 0;
2481058102a6STobias Waldekranz 			}
2482058102a6STobias Waldekranz 			err = notifier_from_errno(err);
2483058102a6STobias Waldekranz 		} else {
2484058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
2485058102a6STobias Waldekranz 			err = NOTIFY_OK;
2486058102a6STobias Waldekranz 		}
248718596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
248818596f50SGeorge McCollister 		if (info->linking) {
248918596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
249018596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
249118596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
249218596f50SGeorge McCollister 						   "Offloading not supported");
249318596f50SGeorge McCollister 				err = 0;
249418596f50SGeorge McCollister 			}
249518596f50SGeorge McCollister 			err = notifier_from_errno(err);
249618596f50SGeorge McCollister 		} else {
249718596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
249818596f50SGeorge McCollister 			err = NOTIFY_OK;
249918596f50SGeorge McCollister 		}
2500058102a6STobias Waldekranz 	}
2501058102a6STobias Waldekranz 
2502058102a6STobias Waldekranz 	return err;
2503058102a6STobias Waldekranz }
2504058102a6STobias Waldekranz 
250574918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev,
250674918945SVladimir Oltean 				    struct netdev_notifier_changeupper_info *info)
250774918945SVladimir Oltean {
250874918945SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
250974918945SVladimir Oltean 
251074918945SVladimir Oltean 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
25114e51bf44SVladimir Oltean 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
251274918945SVladimir Oltean 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
25134e51bf44SVladimir Oltean 		dsa_port_pre_lag_leave(dp, info->upper_dev);
251474918945SVladimir Oltean 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
251574918945SVladimir Oltean 	 * meaningfully enslaved to a bridge yet
251674918945SVladimir Oltean 	 */
251774918945SVladimir Oltean 
25184e51bf44SVladimir Oltean 	return NOTIFY_DONE;
251974918945SVladimir Oltean }
252074918945SVladimir Oltean 
2521058102a6STobias Waldekranz static int
2522058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
2523058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
2524058102a6STobias Waldekranz {
2525058102a6STobias Waldekranz 	struct net_device *lower;
2526058102a6STobias Waldekranz 	struct list_head *iter;
2527058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
2528058102a6STobias Waldekranz 	struct dsa_port *dp;
2529058102a6STobias Waldekranz 
2530058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
2531058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
2532058102a6STobias Waldekranz 			continue;
2533058102a6STobias Waldekranz 
2534058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
2535dedd6a00SVladimir Oltean 		if (!dp->lag)
2536058102a6STobias Waldekranz 			/* Software LAG */
2537058102a6STobias Waldekranz 			continue;
2538058102a6STobias Waldekranz 
2539058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
2540058102a6STobias Waldekranz 		if (notifier_to_errno(err))
2541058102a6STobias Waldekranz 			break;
25426debb68aSVivien Didelot 	}
2543b73adef6SFlorian Fainelli 
25448e92ab3aSVivien Didelot 	return err;
2545b73adef6SFlorian Fainelli }
2546b73adef6SFlorian Fainelli 
254774918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls
254874918945SVladimir Oltean  * dsa_slave_prechangeupper()
254974918945SVladimir Oltean  */
255074918945SVladimir Oltean static int
255174918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev,
255274918945SVladimir Oltean 			     struct netdev_notifier_changeupper_info *info)
255374918945SVladimir Oltean {
255474918945SVladimir Oltean 	struct net_device *lower;
255574918945SVladimir Oltean 	struct list_head *iter;
255674918945SVladimir Oltean 	int err = NOTIFY_DONE;
255774918945SVladimir Oltean 	struct dsa_port *dp;
255874918945SVladimir Oltean 
255974918945SVladimir Oltean 	netdev_for_each_lower_dev(dev, lower, iter) {
256074918945SVladimir Oltean 		if (!dsa_slave_dev_check(lower))
256174918945SVladimir Oltean 			continue;
256274918945SVladimir Oltean 
256374918945SVladimir Oltean 		dp = dsa_slave_to_port(lower);
2564dedd6a00SVladimir Oltean 		if (!dp->lag)
256574918945SVladimir Oltean 			/* Software LAG */
256674918945SVladimir Oltean 			continue;
256774918945SVladimir Oltean 
256874918945SVladimir Oltean 		err = dsa_slave_prechangeupper(lower, info);
256974918945SVladimir Oltean 		if (notifier_to_errno(err))
257074918945SVladimir Oltean 			break;
257174918945SVladimir Oltean 	}
257274918945SVladimir Oltean 
257374918945SVladimir Oltean 	return err;
257474918945SVladimir Oltean }
257574918945SVladimir Oltean 
2576eb46e8daSVladimir Oltean static int
2577eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2578eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2579cc1d5bdaSFlorian Fainelli {
2580cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
258136cbf39bSVladimir Oltean 	struct net_device *slave, *br;
2582cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2583cc1d5bdaSFlorian Fainelli 
2584cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2585cc1d5bdaSFlorian Fainelli 
2586cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2587cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2588cc1d5bdaSFlorian Fainelli 
2589cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2590cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2591cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2592cc1d5bdaSFlorian Fainelli 
2593cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
259436cbf39bSVladimir Oltean 	br = dsa_port_bridge_dev_get(dp);
259536cbf39bSVladimir Oltean 	if (!br)
2596cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2597cc1d5bdaSFlorian Fainelli 
2598cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
259936cbf39bSVladimir Oltean 	if (br_vlan_enabled(br) &&
2600cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2601cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2602cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2603cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2604cc1d5bdaSFlorian Fainelli 	}
2605cc1d5bdaSFlorian Fainelli 
2606cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2607cc1d5bdaSFlorian Fainelli }
2608cc1d5bdaSFlorian Fainelli 
26092b138406SVladimir Oltean static int
26102b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
26112b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
26122b138406SVladimir Oltean {
26132b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
261436cbf39bSVladimir Oltean 	struct net_device *br = dsa_port_bridge_dev_get(dp);
26152b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
26162b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
26172b138406SVladimir Oltean 	int err = NOTIFY_DONE;
26182b138406SVladimir Oltean 	u16 vid;
26192b138406SVladimir Oltean 
2620adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
26212b138406SVladimir Oltean 		return NOTIFY_DONE;
26222b138406SVladimir Oltean 
26232b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
26242b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
26252b138406SVladimir Oltean 
26262b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
26272b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
26282b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
26292b138406SVladimir Oltean 	 */
26302b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
26312b138406SVladimir Oltean 	if (err == 0) {
26322b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
26332b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
26342b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
26352b138406SVladimir Oltean 	}
26362b138406SVladimir Oltean 
26372b138406SVladimir Oltean 	return NOTIFY_DONE;
26382b138406SVladimir Oltean }
26392b138406SVladimir Oltean 
26404ede74e7SVladimir Oltean static int
26414ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
26424ede74e7SVladimir Oltean 				      struct netdev_notifier_changeupper_info *info)
2643b73adef6SFlorian Fainelli {
2644e358bef7SVladimir Oltean 	struct dsa_switch *ds;
2645e358bef7SVladimir Oltean 	struct dsa_port *dp;
2646e358bef7SVladimir Oltean 	int err;
26472b138406SVladimir Oltean 
264853bade8aSFlorian Fainelli 	if (!dsa_slave_dev_check(dev))
26494ede74e7SVladimir Oltean 		return dsa_prevent_bridging_8021q_upper(dev, info);
26502b138406SVladimir Oltean 
2651e358bef7SVladimir Oltean 	dp = dsa_slave_to_port(dev);
2652e358bef7SVladimir Oltean 	ds = dp->ds;
2653e358bef7SVladimir Oltean 
2654e358bef7SVladimir Oltean 	if (ds->ops->port_prechangeupper) {
2655e358bef7SVladimir Oltean 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
2656e358bef7SVladimir Oltean 		if (err)
2657e358bef7SVladimir Oltean 			return notifier_from_errno(err);
2658e358bef7SVladimir Oltean 	}
2659e358bef7SVladimir Oltean 
26602b138406SVladimir Oltean 	if (is_vlan_dev(info->upper_dev))
26614ede74e7SVladimir Oltean 		return dsa_slave_check_8021q_upper(dev, info);
26624ede74e7SVladimir Oltean 
26634ede74e7SVladimir Oltean 	return NOTIFY_DONE;
26644ede74e7SVladimir Oltean }
26654ede74e7SVladimir Oltean 
26664ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb,
26674ede74e7SVladimir Oltean 				     unsigned long event, void *ptr)
26684ede74e7SVladimir Oltean {
26694ede74e7SVladimir Oltean 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
26704ede74e7SVladimir Oltean 
26714ede74e7SVladimir Oltean 	switch (event) {
26724ede74e7SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
26734ede74e7SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
26744ede74e7SVladimir Oltean 		int err;
26754ede74e7SVladimir Oltean 
26764ede74e7SVladimir Oltean 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
26774ede74e7SVladimir Oltean 		if (err != NOTIFY_DONE)
26784ede74e7SVladimir Oltean 			return err;
26794ede74e7SVladimir Oltean 
268074918945SVladimir Oltean 		if (dsa_slave_dev_check(dev))
268174918945SVladimir Oltean 			return dsa_slave_prechangeupper(dev, ptr);
268274918945SVladimir Oltean 
268374918945SVladimir Oltean 		if (netif_is_lag_master(dev))
268474918945SVladimir Oltean 			return dsa_slave_lag_prechangeupper(dev, ptr);
268574918945SVladimir Oltean 
268683501299SVladimir Oltean 		break;
26872b138406SVladimir Oltean 	}
268883501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2689058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
26908e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2691058102a6STobias Waldekranz 
2692058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2693058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2694058102a6STobias Waldekranz 
2695058102a6STobias Waldekranz 		break;
2696058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2697058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2698058102a6STobias Waldekranz 		struct dsa_port *dp;
2699058102a6STobias Waldekranz 		int err;
2700058102a6STobias Waldekranz 
2701058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2702058102a6STobias Waldekranz 			break;
2703058102a6STobias Waldekranz 
2704058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2705058102a6STobias Waldekranz 
2706058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2707058102a6STobias Waldekranz 		return notifier_from_errno(err);
2708058102a6STobias Waldekranz 	}
2709295ab96fSVladimir Oltean 	case NETDEV_CHANGE:
2710295ab96fSVladimir Oltean 	case NETDEV_UP: {
2711295ab96fSVladimir Oltean 		/* Track state of master port.
2712295ab96fSVladimir Oltean 		 * DSA driver may require the master port (and indirectly
2713295ab96fSVladimir Oltean 		 * the tagger) to be available for some special operation.
2714295ab96fSVladimir Oltean 		 */
2715295ab96fSVladimir Oltean 		if (netdev_uses_dsa(dev)) {
2716295ab96fSVladimir Oltean 			struct dsa_port *cpu_dp = dev->dsa_ptr;
2717295ab96fSVladimir Oltean 			struct dsa_switch_tree *dst = cpu_dp->ds->dst;
2718295ab96fSVladimir Oltean 
2719295ab96fSVladimir Oltean 			/* Track when the master port is UP */
2720295ab96fSVladimir Oltean 			dsa_tree_master_oper_state_change(dst, dev,
2721295ab96fSVladimir Oltean 							  netif_oper_up(dev));
2722295ab96fSVladimir Oltean 
2723295ab96fSVladimir Oltean 			/* Track when the master port is ready and can accept
2724295ab96fSVladimir Oltean 			 * packet.
2725295ab96fSVladimir Oltean 			 * NETDEV_UP event is not enough to flag a port as ready.
2726295ab96fSVladimir Oltean 			 * We also have to wait for linkwatch_do_dev to dev_activate
2727295ab96fSVladimir Oltean 			 * and emit a NETDEV_CHANGE event.
2728295ab96fSVladimir Oltean 			 * We check if a master port is ready by checking if the dev
2729295ab96fSVladimir Oltean 			 * have a qdisc assigned and is not noop.
2730295ab96fSVladimir Oltean 			 */
2731295ab96fSVladimir Oltean 			dsa_tree_master_admin_state_change(dst, dev,
2732295ab96fSVladimir Oltean 							   !qdisc_tx_is_noop(dev));
2733295ab96fSVladimir Oltean 
2734295ab96fSVladimir Oltean 			return NOTIFY_OK;
2735295ab96fSVladimir Oltean 		}
2736295ab96fSVladimir Oltean 
2737295ab96fSVladimir Oltean 		return NOTIFY_DONE;
2738295ab96fSVladimir Oltean 	}
2739c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2740c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2741c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2742c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2743c0a8a9c2SVladimir Oltean 
2744c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2745c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2746c0a8a9c2SVladimir Oltean 
2747c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2748c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2749c0a8a9c2SVladimir Oltean 
2750295ab96fSVladimir Oltean 		dsa_tree_master_admin_state_change(dst, dev, false);
2751295ab96fSVladimir Oltean 
2752c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2753d0004a02SVladimir Oltean 			if (!dsa_port_is_user(dp))
2754c0a8a9c2SVladimir Oltean 				continue;
2755c0a8a9c2SVladimir Oltean 
2756c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2757c0a8a9c2SVladimir Oltean 		}
2758c0a8a9c2SVladimir Oltean 
2759c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2760c0a8a9c2SVladimir Oltean 
2761c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2762c0a8a9c2SVladimir Oltean 	}
2763c0a8a9c2SVladimir Oltean 	default:
2764c0a8a9c2SVladimir Oltean 		break;
2765cc1d5bdaSFlorian Fainelli 	}
2766b73adef6SFlorian Fainelli 
2767b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2768b73adef6SFlorian Fainelli }
276988e4f0caSVivien Didelot 
2770c4bb76a9SVladimir Oltean static void
2771c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2772c4bb76a9SVladimir Oltean {
2773c35b57ceSVladimir Oltean 	struct switchdev_notifier_fdb_info info = {};
2774c4bb76a9SVladimir Oltean 
2775c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2776c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2777c4bb76a9SVladimir Oltean 	info.offloaded = true;
2778c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
277993c79823SVladimir Oltean 				 switchdev_work->orig_dev, &info.info, NULL);
2780c4bb76a9SVladimir Oltean }
2781c9eb3e0fSArkadi Sharshevsky 
2782c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2783c9eb3e0fSArkadi Sharshevsky {
2784c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2785c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
278668d6d71eSVladimir Oltean 	const unsigned char *addr = switchdev_work->addr;
2787e35f12e9SVladimir Oltean 	struct net_device *dev = switchdev_work->dev;
278868d6d71eSVladimir Oltean 	u16 vid = switchdev_work->vid;
2789e35f12e9SVladimir Oltean 	struct dsa_switch *ds;
2790c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2791c9eb3e0fSArkadi Sharshevsky 	int err;
2792c9eb3e0fSArkadi Sharshevsky 
2793e35f12e9SVladimir Oltean 	dp = dsa_slave_to_port(dev);
2794e35f12e9SVladimir Oltean 	ds = dp->ds;
2795c4bb76a9SVladimir Oltean 
2796c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2797c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
27983dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
279968d6d71eSVladimir Oltean 			err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
2800e212fa7cSVladimir Oltean 		else if (dp->lag)
280168d6d71eSVladimir Oltean 			err = dsa_port_lag_fdb_add(dp, addr, vid);
28023dc80afcSVladimir Oltean 		else
280368d6d71eSVladimir Oltean 			err = dsa_port_fdb_add(dp, addr, vid);
2804c9eb3e0fSArkadi Sharshevsky 		if (err) {
2805c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2806c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
280768d6d71eSVladimir Oltean 				dp->index, addr, vid, err);
2808c9eb3e0fSArkadi Sharshevsky 			break;
2809c9eb3e0fSArkadi Sharshevsky 		}
2810c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2811c9eb3e0fSArkadi Sharshevsky 		break;
2812c9eb3e0fSArkadi Sharshevsky 
2813c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
28143dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
281568d6d71eSVladimir Oltean 			err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
2816e212fa7cSVladimir Oltean 		else if (dp->lag)
281768d6d71eSVladimir Oltean 			err = dsa_port_lag_fdb_del(dp, addr, vid);
28183dc80afcSVladimir Oltean 		else
281968d6d71eSVladimir Oltean 			err = dsa_port_fdb_del(dp, addr, vid);
2820c9eb3e0fSArkadi Sharshevsky 		if (err) {
2821c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2822c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
282368d6d71eSVladimir Oltean 				dp->index, addr, vid, err);
2824c9eb3e0fSArkadi Sharshevsky 		}
28252fd18650SVladimir Oltean 
2826c9eb3e0fSArkadi Sharshevsky 		break;
2827c9eb3e0fSArkadi Sharshevsky 	}
2828c9eb3e0fSArkadi Sharshevsky 
2829c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2830c9eb3e0fSArkadi Sharshevsky }
2831c9eb3e0fSArkadi Sharshevsky 
2832b94dc99cSVladimir Oltean static bool dsa_foreign_dev_check(const struct net_device *dev,
2833b94dc99cSVladimir Oltean 				  const struct net_device *foreign_dev)
2834d5f19486SVladimir Oltean {
2835b94dc99cSVladimir Oltean 	const struct dsa_port *dp = dsa_slave_to_port(dev);
2836b94dc99cSVladimir Oltean 	struct dsa_switch_tree *dst = dp->ds->dst;
2837b94dc99cSVladimir Oltean 
2838b94dc99cSVladimir Oltean 	if (netif_is_bridge_master(foreign_dev))
2839936db8a2SVladimir Oltean 		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
2840b94dc99cSVladimir Oltean 
2841b94dc99cSVladimir Oltean 	if (netif_is_bridge_port(foreign_dev))
2842b94dc99cSVladimir Oltean 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2843b94dc99cSVladimir Oltean 
2844b94dc99cSVladimir Oltean 	/* Everything else is foreign */
2845b94dc99cSVladimir Oltean 	return true;
2846d5f19486SVladimir Oltean }
2847d5f19486SVladimir Oltean 
2848b94dc99cSVladimir Oltean static int dsa_slave_fdb_event(struct net_device *dev,
2849716a30a9SVladimir Oltean 			       struct net_device *orig_dev,
2850716a30a9SVladimir Oltean 			       unsigned long event, const void *ctx,
2851716a30a9SVladimir Oltean 			       const struct switchdev_notifier_fdb_info *fdb_info)
2852b94dc99cSVladimir Oltean {
2853b94dc99cSVladimir Oltean 	struct dsa_switchdev_event_work *switchdev_work;
2854b94dc99cSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2855b94dc99cSVladimir Oltean 	bool host_addr = fdb_info->is_local;
2856b94dc99cSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
2857b94dc99cSVladimir Oltean 
2858b94dc99cSVladimir Oltean 	if (ctx && ctx != dp)
2859b94dc99cSVladimir Oltean 		return 0;
2860b94dc99cSVladimir Oltean 
2861a860352eSTobias Waldekranz 	if (!dp->bridge)
2862a860352eSTobias Waldekranz 		return 0;
2863a860352eSTobias Waldekranz 
2864e212fa7cSVladimir Oltean 	if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
2865e212fa7cSVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, orig_dev))
2866b94dc99cSVladimir Oltean 			return 0;
2867b94dc99cSVladimir Oltean 
2868e212fa7cSVladimir Oltean 		/* FDB entries learned by the software bridge or by foreign
2869e212fa7cSVladimir Oltean 		 * bridge ports should be installed as host addresses only if
2870e212fa7cSVladimir Oltean 		 * the driver requests assisted learning.
2871b94dc99cSVladimir Oltean 		 */
2872e212fa7cSVladimir Oltean 		if (!ds->assisted_learning_on_cpu_port)
2873b94dc99cSVladimir Oltean 			return 0;
2874e212fa7cSVladimir Oltean 	}
2875b94dc99cSVladimir Oltean 
2876b94dc99cSVladimir Oltean 	/* Also treat FDB entries on foreign interfaces bridged with us as host
2877b94dc99cSVladimir Oltean 	 * addresses.
2878b94dc99cSVladimir Oltean 	 */
2879b94dc99cSVladimir Oltean 	if (dsa_foreign_dev_check(dev, orig_dev))
2880b94dc99cSVladimir Oltean 		host_addr = true;
2881b94dc99cSVladimir Oltean 
2882e212fa7cSVladimir Oltean 	/* Check early that we're not doing work in vain.
2883e212fa7cSVladimir Oltean 	 * Host addresses on LAG ports still require regular FDB ops,
2884e212fa7cSVladimir Oltean 	 * since the CPU port isn't in a LAG.
2885e212fa7cSVladimir Oltean 	 */
2886e212fa7cSVladimir Oltean 	if (dp->lag && !host_addr) {
2887e212fa7cSVladimir Oltean 		if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
2888e212fa7cSVladimir Oltean 			return -EOPNOTSUPP;
2889e212fa7cSVladimir Oltean 	} else {
2890e212fa7cSVladimir Oltean 		if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2891e212fa7cSVladimir Oltean 			return -EOPNOTSUPP;
2892e212fa7cSVladimir Oltean 	}
2893e212fa7cSVladimir Oltean 
2894b94dc99cSVladimir Oltean 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2895b94dc99cSVladimir Oltean 	if (!switchdev_work)
2896b94dc99cSVladimir Oltean 		return -ENOMEM;
2897b94dc99cSVladimir Oltean 
2898b94dc99cSVladimir Oltean 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2899b94dc99cSVladimir Oltean 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2900b94dc99cSVladimir Oltean 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
2901b94dc99cSVladimir Oltean 		   host_addr ? " as host address" : "");
2902b94dc99cSVladimir Oltean 
2903b94dc99cSVladimir Oltean 	INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2904b94dc99cSVladimir Oltean 	switchdev_work->event = event;
2905b94dc99cSVladimir Oltean 	switchdev_work->dev = dev;
290693c79823SVladimir Oltean 	switchdev_work->orig_dev = orig_dev;
2907b94dc99cSVladimir Oltean 
2908b94dc99cSVladimir Oltean 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2909b94dc99cSVladimir Oltean 	switchdev_work->vid = fdb_info->vid;
2910b94dc99cSVladimir Oltean 	switchdev_work->host_addr = host_addr;
2911b94dc99cSVladimir Oltean 
2912b94dc99cSVladimir Oltean 	dsa_schedule_work(&switchdev_work->work);
2913b94dc99cSVladimir Oltean 
2914d5f19486SVladimir Oltean 	return 0;
2915d5f19486SVladimir Oltean }
2916d5f19486SVladimir Oltean 
2917c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2918c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2919c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2920c9eb3e0fSArkadi Sharshevsky {
2921c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
292279b139f4SVivien Didelot 	int err;
292379b139f4SVivien Didelot 
2924447d290aSVladimir Oltean 	switch (event) {
2925447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
292679b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
292779b139f4SVivien Didelot 						     dsa_slave_dev_check,
292879b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
292979b139f4SVivien Didelot 		return notifier_from_errno(err);
2930447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2931447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2932716a30a9SVladimir Oltean 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
2933b94dc99cSVladimir Oltean 							   dsa_slave_dev_check,
2934b94dc99cSVladimir Oltean 							   dsa_foreign_dev_check,
2935ec638740SVladimir Oltean 							   dsa_slave_fdb_event);
2936b94dc99cSVladimir Oltean 		return notifier_from_errno(err);
2937c9eb3e0fSArkadi Sharshevsky 	default:
2938c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2939c9eb3e0fSArkadi Sharshevsky 	}
2940c9eb3e0fSArkadi Sharshevsky 
2941c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2942c9eb3e0fSArkadi Sharshevsky }
2943c9eb3e0fSArkadi Sharshevsky 
29442b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
29452b239f67SPetr Machata 					      unsigned long event, void *ptr)
29462b239f67SPetr Machata {
29472b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
294879b139f4SVivien Didelot 	int err;
29492b239f67SPetr Machata 
29502b239f67SPetr Machata 	switch (event) {
295179b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
2952164f861bSVladimir Oltean 		err = switchdev_handle_port_obj_add_foreign(dev, ptr,
295379b139f4SVivien Didelot 							    dsa_slave_dev_check,
2954164f861bSVladimir Oltean 							    dsa_foreign_dev_check,
295579b139f4SVivien Didelot 							    dsa_slave_port_obj_add);
295679b139f4SVivien Didelot 		return notifier_from_errno(err);
29572b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
2958164f861bSVladimir Oltean 		err = switchdev_handle_port_obj_del_foreign(dev, ptr,
295979b139f4SVivien Didelot 							    dsa_slave_dev_check,
2960164f861bSVladimir Oltean 							    dsa_foreign_dev_check,
296179b139f4SVivien Didelot 							    dsa_slave_port_obj_del);
296279b139f4SVivien Didelot 		return notifier_from_errno(err);
29639ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
296479b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
296579b139f4SVivien Didelot 						     dsa_slave_dev_check,
296679b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
296779b139f4SVivien Didelot 		return notifier_from_errno(err);
29682b239f67SPetr Machata 	}
29692b239f67SPetr Machata 
29702b239f67SPetr Machata 	return NOTIFY_DONE;
29712b239f67SPetr Machata }
29722b239f67SPetr Machata 
297388e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
297488e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
297588e4f0caSVivien Didelot };
297688e4f0caSVivien Didelot 
2977010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = {
2978c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
2979c9eb3e0fSArkadi Sharshevsky };
2980c9eb3e0fSArkadi Sharshevsky 
2981010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = {
29822b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
29832b239f67SPetr Machata };
29842b239f67SPetr Machata 
298588e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
298688e4f0caSVivien Didelot {
29872b239f67SPetr Machata 	struct notifier_block *nb;
2988c9eb3e0fSArkadi Sharshevsky 	int err;
2989c9eb3e0fSArkadi Sharshevsky 
2990c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
2991c9eb3e0fSArkadi Sharshevsky 	if (err)
2992c9eb3e0fSArkadi Sharshevsky 		return err;
2993c9eb3e0fSArkadi Sharshevsky 
2994c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2995c9eb3e0fSArkadi Sharshevsky 	if (err)
2996c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
2997c9eb3e0fSArkadi Sharshevsky 
29982b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
29992b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
30002b239f67SPetr Machata 	if (err)
30012b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
30022b239f67SPetr Machata 
3003c9eb3e0fSArkadi Sharshevsky 	return 0;
3004c9eb3e0fSArkadi Sharshevsky 
30052b239f67SPetr Machata err_switchdev_blocking_nb:
30062b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3007c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
3008c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
3009c9eb3e0fSArkadi Sharshevsky 	return err;
301088e4f0caSVivien Didelot }
301188e4f0caSVivien Didelot 
301288e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
301388e4f0caSVivien Didelot {
30142b239f67SPetr Machata 	struct notifier_block *nb;
301588e4f0caSVivien Didelot 	int err;
301688e4f0caSVivien Didelot 
30172b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
30182b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
30192b239f67SPetr Machata 	if (err)
30202b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
30212b239f67SPetr Machata 
3022c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3023c9eb3e0fSArkadi Sharshevsky 	if (err)
3024c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3025c9eb3e0fSArkadi Sharshevsky 
302688e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
302788e4f0caSVivien Didelot 	if (err)
302888e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
302988e4f0caSVivien Didelot }
3030