xref: /openbmc/linux/net/dsa/slave.c (revision b2033a05a7197f5ddef617be8f510c9957a82553)
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 
2657569459aSVladimir Oltean /* Keep flooding enabled towards this port's CPU port as long as it serves at
2667569459aSVladimir Oltean  * least one port in the tree that requires it.
2677569459aSVladimir Oltean  */
2687569459aSVladimir Oltean static void dsa_port_manage_cpu_flood(struct dsa_port *dp)
2697569459aSVladimir Oltean {
2707569459aSVladimir Oltean 	struct switchdev_brport_flags flags = {
2717569459aSVladimir Oltean 		.mask = BR_FLOOD | BR_MCAST_FLOOD,
2727569459aSVladimir Oltean 	};
2737569459aSVladimir Oltean 	struct dsa_switch_tree *dst = dp->ds->dst;
2747569459aSVladimir Oltean 	struct dsa_port *cpu_dp = dp->cpu_dp;
2757569459aSVladimir Oltean 	struct dsa_port *other_dp;
2767569459aSVladimir Oltean 	int err;
2777569459aSVladimir Oltean 
2787569459aSVladimir Oltean 	list_for_each_entry(other_dp, &dst->ports, list) {
2797569459aSVladimir Oltean 		if (!dsa_port_is_user(other_dp))
2807569459aSVladimir Oltean 			continue;
2817569459aSVladimir Oltean 
2827569459aSVladimir Oltean 		if (other_dp->cpu_dp != cpu_dp)
2837569459aSVladimir Oltean 			continue;
2847569459aSVladimir Oltean 
2857569459aSVladimir Oltean 		if (other_dp->slave->flags & IFF_ALLMULTI)
2867569459aSVladimir Oltean 			flags.val |= BR_MCAST_FLOOD;
2877569459aSVladimir Oltean 		if (other_dp->slave->flags & IFF_PROMISC)
2887569459aSVladimir Oltean 			flags.val |= BR_FLOOD;
2897569459aSVladimir Oltean 	}
2907569459aSVladimir Oltean 
2917569459aSVladimir Oltean 	err = dsa_port_pre_bridge_flags(dp, flags, NULL);
2927569459aSVladimir Oltean 	if (err)
2937569459aSVladimir Oltean 		return;
2947569459aSVladimir Oltean 
2957569459aSVladimir Oltean 	dsa_port_bridge_flags(cpu_dp, flags, NULL);
2967569459aSVladimir Oltean }
2977569459aSVladimir Oltean 
29891da11f8SLennert Buytenhek static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
29991da11f8SLennert Buytenhek {
300d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
3017569459aSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
3027569459aSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
30335aae5abSVladimir Oltean 
30491da11f8SLennert Buytenhek 	if (change & IFF_ALLMULTI)
30517ab4f61SRundong Ge 		dev_set_allmulti(master,
30617ab4f61SRundong Ge 				 dev->flags & IFF_ALLMULTI ? 1 : -1);
30791da11f8SLennert Buytenhek 	if (change & IFF_PROMISC)
30817ab4f61SRundong Ge 		dev_set_promiscuity(master,
30917ab4f61SRundong Ge 				    dev->flags & IFF_PROMISC ? 1 : -1);
3107569459aSVladimir Oltean 
3117569459aSVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds) &&
3127569459aSVladimir Oltean 	    dsa_switch_supports_mc_filtering(ds))
3137569459aSVladimir Oltean 		dsa_port_manage_cpu_flood(dp);
31417ab4f61SRundong Ge }
31591da11f8SLennert Buytenhek 
31691da11f8SLennert Buytenhek static void dsa_slave_set_rx_mode(struct net_device *dev)
31791da11f8SLennert Buytenhek {
3185e8a1e03SVladimir Oltean 	__dev_mc_sync(dev, dsa_slave_sync_mc, dsa_slave_unsync_mc);
3195e8a1e03SVladimir Oltean 	__dev_uc_sync(dev, dsa_slave_sync_uc, dsa_slave_unsync_uc);
32091da11f8SLennert Buytenhek }
32191da11f8SLennert Buytenhek 
322df02c6ffSLennert Buytenhek static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
32391da11f8SLennert Buytenhek {
324d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
325499aa9e1SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
326499aa9e1SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
327df02c6ffSLennert Buytenhek 	struct sockaddr *addr = a;
328df02c6ffSLennert Buytenhek 	int err;
329df02c6ffSLennert Buytenhek 
330df02c6ffSLennert Buytenhek 	if (!is_valid_ether_addr(addr->sa_data))
331df02c6ffSLennert Buytenhek 		return -EADDRNOTAVAIL;
332df02c6ffSLennert Buytenhek 
333e2d0576fSVladimir Oltean 	/* If the port is down, the address isn't synced yet to hardware or
334e2d0576fSVladimir Oltean 	 * to the DSA master, so there is nothing to change.
335e2d0576fSVladimir Oltean 	 */
336e2d0576fSVladimir Oltean 	if (!(dev->flags & IFF_UP))
337e2d0576fSVladimir Oltean 		goto out_change_dev_addr;
338e2d0576fSVladimir Oltean 
339499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds)) {
340499aa9e1SVladimir Oltean 		err = dsa_port_standalone_host_fdb_add(dp, addr->sa_data, 0);
341499aa9e1SVladimir Oltean 		if (err)
342499aa9e1SVladimir Oltean 			return err;
343499aa9e1SVladimir Oltean 	}
344499aa9e1SVladimir Oltean 
3458feedbb4SJoe Perches 	if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
346a748ee24SJiri Pirko 		err = dev_uc_add(master, addr->sa_data);
347df02c6ffSLennert Buytenhek 		if (err < 0)
348499aa9e1SVladimir Oltean 			goto del_unicast;
349df02c6ffSLennert Buytenhek 	}
350df02c6ffSLennert Buytenhek 
3518feedbb4SJoe Perches 	if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
352a748ee24SJiri Pirko 		dev_uc_del(master, dev->dev_addr);
353df02c6ffSLennert Buytenhek 
354499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
355499aa9e1SVladimir Oltean 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
356499aa9e1SVladimir Oltean 
357e2d0576fSVladimir Oltean out_change_dev_addr:
358e35b8d7dSJakub Kicinski 	eth_hw_addr_set(dev, addr->sa_data);
35991da11f8SLennert Buytenhek 
36091da11f8SLennert Buytenhek 	return 0;
361499aa9e1SVladimir Oltean 
362499aa9e1SVladimir Oltean del_unicast:
363499aa9e1SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
364499aa9e1SVladimir Oltean 		dsa_port_standalone_host_fdb_del(dp, addr->sa_data, 0);
365499aa9e1SVladimir Oltean 
366499aa9e1SVladimir Oltean 	return err;
36791da11f8SLennert Buytenhek }
36891da11f8SLennert Buytenhek 
3692bedde1aSArkadi Sharshevsky struct dsa_slave_dump_ctx {
3702bedde1aSArkadi Sharshevsky 	struct net_device *dev;
3712bedde1aSArkadi Sharshevsky 	struct sk_buff *skb;
3722bedde1aSArkadi Sharshevsky 	struct netlink_callback *cb;
3732bedde1aSArkadi Sharshevsky 	int idx;
3742bedde1aSArkadi Sharshevsky };
3752bedde1aSArkadi Sharshevsky 
3762bedde1aSArkadi Sharshevsky static int
3772bedde1aSArkadi Sharshevsky dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
3782bedde1aSArkadi Sharshevsky 			   bool is_static, void *data)
3792bedde1aSArkadi Sharshevsky {
3802bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx *dump = data;
3812bedde1aSArkadi Sharshevsky 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
3822bedde1aSArkadi Sharshevsky 	u32 seq = dump->cb->nlh->nlmsg_seq;
3832bedde1aSArkadi Sharshevsky 	struct nlmsghdr *nlh;
3842bedde1aSArkadi Sharshevsky 	struct ndmsg *ndm;
3852bedde1aSArkadi Sharshevsky 
3862bedde1aSArkadi Sharshevsky 	if (dump->idx < dump->cb->args[2])
3872bedde1aSArkadi Sharshevsky 		goto skip;
3882bedde1aSArkadi Sharshevsky 
3892bedde1aSArkadi Sharshevsky 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
3902bedde1aSArkadi Sharshevsky 			sizeof(*ndm), NLM_F_MULTI);
3912bedde1aSArkadi Sharshevsky 	if (!nlh)
3922bedde1aSArkadi Sharshevsky 		return -EMSGSIZE;
3932bedde1aSArkadi Sharshevsky 
3942bedde1aSArkadi Sharshevsky 	ndm = nlmsg_data(nlh);
3952bedde1aSArkadi Sharshevsky 	ndm->ndm_family  = AF_BRIDGE;
3962bedde1aSArkadi Sharshevsky 	ndm->ndm_pad1    = 0;
3972bedde1aSArkadi Sharshevsky 	ndm->ndm_pad2    = 0;
3982bedde1aSArkadi Sharshevsky 	ndm->ndm_flags   = NTF_SELF;
3992bedde1aSArkadi Sharshevsky 	ndm->ndm_type    = 0;
4002bedde1aSArkadi Sharshevsky 	ndm->ndm_ifindex = dump->dev->ifindex;
4012bedde1aSArkadi Sharshevsky 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
4022bedde1aSArkadi Sharshevsky 
4032bedde1aSArkadi Sharshevsky 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
4042bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
4052bedde1aSArkadi Sharshevsky 
4062bedde1aSArkadi Sharshevsky 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
4072bedde1aSArkadi Sharshevsky 		goto nla_put_failure;
4082bedde1aSArkadi Sharshevsky 
4092bedde1aSArkadi Sharshevsky 	nlmsg_end(dump->skb, nlh);
4102bedde1aSArkadi Sharshevsky 
4112bedde1aSArkadi Sharshevsky skip:
4122bedde1aSArkadi Sharshevsky 	dump->idx++;
4132bedde1aSArkadi Sharshevsky 	return 0;
4142bedde1aSArkadi Sharshevsky 
4152bedde1aSArkadi Sharshevsky nla_put_failure:
4162bedde1aSArkadi Sharshevsky 	nlmsg_cancel(dump->skb, nlh);
4172bedde1aSArkadi Sharshevsky 	return -EMSGSIZE;
4182bedde1aSArkadi Sharshevsky }
4192bedde1aSArkadi Sharshevsky 
4202bedde1aSArkadi Sharshevsky static int
4212bedde1aSArkadi Sharshevsky dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
4222bedde1aSArkadi Sharshevsky 		   struct net_device *dev, struct net_device *filter_dev,
4232bedde1aSArkadi Sharshevsky 		   int *idx)
4242bedde1aSArkadi Sharshevsky {
425d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
4262bedde1aSArkadi Sharshevsky 	struct dsa_slave_dump_ctx dump = {
4272bedde1aSArkadi Sharshevsky 		.dev = dev,
4282bedde1aSArkadi Sharshevsky 		.skb = skb,
4292bedde1aSArkadi Sharshevsky 		.cb = cb,
4302bedde1aSArkadi Sharshevsky 		.idx = *idx,
4312bedde1aSArkadi Sharshevsky 	};
4322bedde1aSArkadi Sharshevsky 	int err;
4332bedde1aSArkadi Sharshevsky 
434de40fc5dSVivien Didelot 	err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
4352bedde1aSArkadi Sharshevsky 	*idx = dump.idx;
436de40fc5dSVivien Didelot 
4372bedde1aSArkadi Sharshevsky 	return err;
4382bedde1aSArkadi Sharshevsky }
4392bedde1aSArkadi Sharshevsky 
44091da11f8SLennert Buytenhek static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
44191da11f8SLennert Buytenhek {
4420336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
4430336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
4440336369dSBrandon Streiff 	int port = p->dp->index;
4450336369dSBrandon Streiff 
4460336369dSBrandon Streiff 	/* Pass through to switch driver if it supports timestamping */
4470336369dSBrandon Streiff 	switch (cmd) {
4480336369dSBrandon Streiff 	case SIOCGHWTSTAMP:
4490336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_get)
4500336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
4510336369dSBrandon Streiff 		break;
4520336369dSBrandon Streiff 	case SIOCSHWTSTAMP:
4530336369dSBrandon Streiff 		if (ds->ops->port_hwtstamp_set)
4540336369dSBrandon Streiff 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
4550336369dSBrandon Streiff 		break;
4560336369dSBrandon Streiff 	}
4570336369dSBrandon Streiff 
458aab9c406SFlorian Fainelli 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
45991da11f8SLennert Buytenhek }
46091da11f8SLennert Buytenhek 
46169bfac96SVladimir Oltean static int dsa_slave_port_attr_set(struct net_device *dev, const void *ctx,
4624c08c586SVladimir Oltean 				   const struct switchdev_attr *attr,
4634c08c586SVladimir Oltean 				   struct netlink_ext_ack *extack)
46435636062SScott Feldman {
465d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
466b8d866acSVivien Didelot 	int ret;
46735636062SScott Feldman 
4680d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
4690d2cfbd4SVladimir Oltean 		return 0;
4700d2cfbd4SVladimir Oltean 
47135636062SScott Feldman 	switch (attr->id) {
4721f868398SJiri Pirko 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
47303cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
47403cbb870SVladimir Oltean 			return -EOPNOTSUPP;
47503cbb870SVladimir Oltean 
47639f32101SVladimir Oltean 		ret = dsa_port_set_state(dp, attr->u.stp_state, true);
47735636062SScott Feldman 		break;
4787414af30STobias Waldekranz 	case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
4797414af30STobias Waldekranz 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
4807414af30STobias Waldekranz 			return -EOPNOTSUPP;
4817414af30STobias Waldekranz 
4827414af30STobias Waldekranz 		ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
4837414af30STobias Waldekranz 		break;
484fb2dabadSVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
485936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
48603cbb870SVladimir Oltean 			return -EOPNOTSUPP;
48703cbb870SVladimir Oltean 
48889153ed6SVladimir Oltean 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
48989153ed6SVladimir Oltean 					      extack);
490fb2dabadSVivien Didelot 		break;
49134a79f63SVivien Didelot 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
492936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
49303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
49403cbb870SVladimir Oltean 
495bae33f2bSVladimir Oltean 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
49634a79f63SVivien Didelot 		break;
497332afc4cSTobias Waldekranz 	case SWITCHDEV_ATTR_ID_BRIDGE_MST:
498332afc4cSTobias Waldekranz 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
499332afc4cSTobias Waldekranz 			return -EOPNOTSUPP;
500332afc4cSTobias Waldekranz 
501332afc4cSTobias Waldekranz 		ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
502332afc4cSTobias Waldekranz 		break;
503ea87005aSFlorian Fainelli 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
50403cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
50503cbb870SVladimir Oltean 			return -EOPNOTSUPP;
50603cbb870SVladimir Oltean 
507a8b659e7SVladimir Oltean 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
508a8b659e7SVladimir Oltean 						extack);
509ea87005aSFlorian Fainelli 		break;
51057652796SRussell King 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
51103cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
51203cbb870SVladimir Oltean 			return -EOPNOTSUPP;
51303cbb870SVladimir Oltean 
514a8b659e7SVladimir Oltean 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
51557652796SRussell King 		break;
5168e6598a7STobias Waldekranz 	case SWITCHDEV_ATTR_ID_VLAN_MSTI:
5178e6598a7STobias Waldekranz 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
5188e6598a7STobias Waldekranz 			return -EOPNOTSUPP;
5198e6598a7STobias Waldekranz 
5208e6598a7STobias Waldekranz 		ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
5218e6598a7STobias Waldekranz 		break;
52235636062SScott Feldman 	default:
52335636062SScott Feldman 		ret = -EOPNOTSUPP;
52435636062SScott Feldman 		break;
52535636062SScott Feldman 	}
52635636062SScott Feldman 
52735636062SScott Feldman 	return ret;
52835636062SScott Feldman }
52935636062SScott Feldman 
5301ce39f0eSVladimir Oltean /* Must be called under rcu_read_lock() */
5311ce39f0eSVladimir Oltean static int
5321ce39f0eSVladimir Oltean dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
5331ce39f0eSVladimir Oltean 				      const struct switchdev_obj_port_vlan *vlan)
5341ce39f0eSVladimir Oltean {
5351ce39f0eSVladimir Oltean 	struct net_device *upper_dev;
5361ce39f0eSVladimir Oltean 	struct list_head *iter;
5371ce39f0eSVladimir Oltean 
5381ce39f0eSVladimir Oltean 	netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
5391ce39f0eSVladimir Oltean 		u16 vid;
5401ce39f0eSVladimir Oltean 
5411ce39f0eSVladimir Oltean 		if (!is_vlan_dev(upper_dev))
5421ce39f0eSVladimir Oltean 			continue;
5431ce39f0eSVladimir Oltean 
5441ce39f0eSVladimir Oltean 		vid = vlan_dev_vlan_id(upper_dev);
545b7a9e0daSVladimir Oltean 		if (vid == vlan->vid)
5461ce39f0eSVladimir Oltean 			return -EBUSY;
5471ce39f0eSVladimir Oltean 	}
5481ce39f0eSVladimir Oltean 
5491ce39f0eSVladimir Oltean 	return 0;
5501ce39f0eSVladimir Oltean }
5511ce39f0eSVladimir Oltean 
552bdcff080SVivien Didelot static int dsa_slave_vlan_add(struct net_device *dev,
5530ee2af4eSVladimir Oltean 			      const struct switchdev_obj *obj,
5540ee2af4eSVladimir Oltean 			      struct netlink_ext_ack *extack)
555bdcff080SVivien Didelot {
556bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
557134ef238SVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
558b7a9e0daSVladimir Oltean 	int err;
559bdcff080SVivien Didelot 
5600ee2af4eSVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
5610ee2af4eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
562c5335d73SVivien Didelot 		return 0;
5630ee2af4eSVladimir Oltean 	}
564c5335d73SVivien Didelot 
565134ef238SVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
566bdcff080SVivien Didelot 
5671ce39f0eSVladimir Oltean 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
5681ce39f0eSVladimir Oltean 	 * the same VID.
5691ce39f0eSVladimir Oltean 	 */
57036cbf39bSVladimir Oltean 	if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
5711ce39f0eSVladimir Oltean 		rcu_read_lock();
572134ef238SVladimir Oltean 		err = dsa_slave_vlan_check_for_8021q_uppers(dev, vlan);
5731ce39f0eSVladimir Oltean 		rcu_read_unlock();
57431046a5fSVladimir Oltean 		if (err) {
57531046a5fSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
57631046a5fSVladimir Oltean 					   "Port already has a VLAN upper with this VID");
5771ce39f0eSVladimir Oltean 			return err;
5781ce39f0eSVladimir Oltean 		}
57931046a5fSVladimir Oltean 	}
5801ce39f0eSVladimir Oltean 
581134ef238SVladimir Oltean 	return dsa_port_vlan_add(dp, vlan, extack);
582134ef238SVladimir Oltean }
583bdcff080SVivien Didelot 
584164f861bSVladimir Oltean /* Offload a VLAN installed on the bridge or on a foreign interface by
585164f861bSVladimir Oltean  * installing it as a VLAN towards the CPU port.
586164f861bSVladimir Oltean  */
587134ef238SVladimir Oltean static int dsa_slave_host_vlan_add(struct net_device *dev,
588134ef238SVladimir Oltean 				   const struct switchdev_obj *obj,
589134ef238SVladimir Oltean 				   struct netlink_ext_ack *extack)
590134ef238SVladimir Oltean {
591134ef238SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
592134ef238SVladimir Oltean 	struct switchdev_obj_port_vlan vlan;
593134ef238SVladimir Oltean 
594164f861bSVladimir Oltean 	/* Do nothing if this is a software bridge */
595164f861bSVladimir Oltean 	if (!dp->bridge)
596164f861bSVladimir Oltean 		return -EOPNOTSUPP;
597164f861bSVladimir Oltean 
598134ef238SVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp)) {
599134ef238SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
600134ef238SVladimir Oltean 		return 0;
601134ef238SVladimir Oltean 	}
602134ef238SVladimir Oltean 
603134ef238SVladimir Oltean 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
604134ef238SVladimir Oltean 
605134ef238SVladimir Oltean 	/* Even though drivers often handle CPU membership in special ways,
606b9499904SVivien Didelot 	 * it doesn't make sense to program a PVID, so clear this flag.
607b9499904SVivien Didelot 	 */
608b9499904SVivien Didelot 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
609b9499904SVivien Didelot 
610134ef238SVladimir Oltean 	return dsa_port_host_vlan_add(dp, &vlan, extack);
611bdcff080SVivien Didelot }
612bdcff080SVivien Didelot 
61369bfac96SVladimir Oltean static int dsa_slave_port_obj_add(struct net_device *dev, const void *ctx,
614648b4a99SJiri Pirko 				  const struct switchdev_obj *obj,
61579b139f4SVivien Didelot 				  struct netlink_ext_ack *extack)
616ba14d9ebSVivien Didelot {
617d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
618ba14d9ebSVivien Didelot 	int err;
619ba14d9ebSVivien Didelot 
6200d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
6210d2cfbd4SVladimir Oltean 		return 0;
6220d2cfbd4SVladimir Oltean 
6239e8f4a54SJiri Pirko 	switch (obj->id) {
6248df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
62503cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
62679b139f4SVivien Didelot 			return -EOPNOTSUPP;
62703cbb870SVladimir Oltean 
628ffb68fc5SVladimir Oltean 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
6298df30255SVivien Didelot 		break;
6305f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
631936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
63203cbb870SVladimir Oltean 			return -EOPNOTSUPP;
63303cbb870SVladimir Oltean 
63468d6d71eSVladimir Oltean 		err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
6355f4dbc50SAndrew Lunn 		break;
63657d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
637164f861bSVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
6380ee2af4eSVladimir Oltean 			err = dsa_slave_vlan_add(dev, obj, extack);
639164f861bSVladimir Oltean 		else
640164f861bSVladimir Oltean 			err = dsa_slave_host_vlan_add(dev, obj, extack);
64111149536SVivien Didelot 		break;
642c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
643936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
644c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
64503cbb870SVladimir Oltean 
646c595c433SHoratiu Vultur 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
647c595c433SHoratiu Vultur 		break;
648c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
649936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
650c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
65103cbb870SVladimir Oltean 
652c595c433SHoratiu Vultur 		err = dsa_port_mrp_add_ring_role(dp,
653c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
654c595c433SHoratiu Vultur 		break;
655ba14d9ebSVivien Didelot 	default:
656ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
657ba14d9ebSVivien Didelot 		break;
658ba14d9ebSVivien Didelot 	}
659ba14d9ebSVivien Didelot 
660ba14d9ebSVivien Didelot 	return err;
661ba14d9ebSVivien Didelot }
662ba14d9ebSVivien Didelot 
663bdcff080SVivien Didelot static int dsa_slave_vlan_del(struct net_device *dev,
664bdcff080SVivien Didelot 			      const struct switchdev_obj *obj)
665bdcff080SVivien Didelot {
666bdcff080SVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
6672209158cSVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
668bdcff080SVivien Didelot 
66954a0ed0dSRussell King 	if (dsa_port_skip_vlan_configuration(dp))
670c5335d73SVivien Didelot 		return 0;
671c5335d73SVivien Didelot 
6722209158cSVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
6732209158cSVladimir Oltean 
674134ef238SVladimir Oltean 	return dsa_port_vlan_del(dp, vlan);
675134ef238SVladimir Oltean }
6762209158cSVladimir Oltean 
677134ef238SVladimir Oltean static int dsa_slave_host_vlan_del(struct net_device *dev,
678134ef238SVladimir Oltean 				   const struct switchdev_obj *obj)
679134ef238SVladimir Oltean {
680134ef238SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
681134ef238SVladimir Oltean 	struct switchdev_obj_port_vlan *vlan;
6822209158cSVladimir Oltean 
683164f861bSVladimir Oltean 	/* Do nothing if this is a software bridge */
684164f861bSVladimir Oltean 	if (!dp->bridge)
685164f861bSVladimir Oltean 		return -EOPNOTSUPP;
686164f861bSVladimir Oltean 
687134ef238SVladimir Oltean 	if (dsa_port_skip_vlan_configuration(dp))
6882209158cSVladimir Oltean 		return 0;
689134ef238SVladimir Oltean 
690134ef238SVladimir Oltean 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
691134ef238SVladimir Oltean 
692134ef238SVladimir Oltean 	return dsa_port_host_vlan_del(dp, vlan);
693bdcff080SVivien Didelot }
694bdcff080SVivien Didelot 
69569bfac96SVladimir Oltean static int dsa_slave_port_obj_del(struct net_device *dev, const void *ctx,
696648b4a99SJiri Pirko 				  const struct switchdev_obj *obj)
697ba14d9ebSVivien Didelot {
698d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
699ba14d9ebSVivien Didelot 	int err;
700ba14d9ebSVivien Didelot 
7010d2cfbd4SVladimir Oltean 	if (ctx && ctx != dp)
7020d2cfbd4SVladimir Oltean 		return 0;
7030d2cfbd4SVladimir Oltean 
7049e8f4a54SJiri Pirko 	switch (obj->id) {
7058df30255SVivien Didelot 	case SWITCHDEV_OBJ_ID_PORT_MDB:
70603cbb870SVladimir Oltean 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
70779b139f4SVivien Didelot 			return -EOPNOTSUPP;
70803cbb870SVladimir Oltean 
709bcebb976SVivien Didelot 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
7108df30255SVivien Didelot 		break;
7115f4dbc50SAndrew Lunn 	case SWITCHDEV_OBJ_ID_HOST_MDB:
712936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
71303cbb870SVladimir Oltean 			return -EOPNOTSUPP;
71403cbb870SVladimir Oltean 
71568d6d71eSVladimir Oltean 		err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
7165f4dbc50SAndrew Lunn 		break;
71757d80838SJiri Pirko 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
718164f861bSVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
719bdcff080SVivien Didelot 			err = dsa_slave_vlan_del(dev, obj);
720164f861bSVladimir Oltean 		else
721164f861bSVladimir Oltean 			err = dsa_slave_host_vlan_del(dev, obj);
72211149536SVivien Didelot 		break;
723c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_MRP:
724936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
725c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
72603cbb870SVladimir Oltean 
727c595c433SHoratiu Vultur 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
728c595c433SHoratiu Vultur 		break;
729c595c433SHoratiu Vultur 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
730936db8a2SVladimir Oltean 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
731c595c433SHoratiu Vultur 			return -EOPNOTSUPP;
73203cbb870SVladimir Oltean 
733c595c433SHoratiu Vultur 		err = dsa_port_mrp_del_ring_role(dp,
734c595c433SHoratiu Vultur 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
735c595c433SHoratiu Vultur 		break;
736ba14d9ebSVivien Didelot 	default:
737ba14d9ebSVivien Didelot 		err = -EOPNOTSUPP;
738ba14d9ebSVivien Didelot 		break;
739ba14d9ebSVivien Didelot 	}
740ba14d9ebSVivien Didelot 
741ba14d9ebSVivien Didelot 	return err;
742ba14d9ebSVivien Didelot }
743ba14d9ebSVivien Didelot 
7444fa7b718SVivien Didelot static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
74504ff53f9SFlorian Fainelli 						     struct sk_buff *skb)
74604ff53f9SFlorian Fainelli {
74704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
7484fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
7494fa7b718SVivien Didelot 
750f78ed220SEric Dumazet 	return netpoll_send_skb(p->netpoll, skb);
75104ff53f9SFlorian Fainelli #else
75204ff53f9SFlorian Fainelli 	BUG();
75304ff53f9SFlorian Fainelli 	return NETDEV_TX_OK;
754f78ed220SEric Dumazet #endif
75504ff53f9SFlorian Fainelli }
75604ff53f9SFlorian Fainelli 
75790af1059SBrandon Streiff static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
75890af1059SBrandon Streiff 				 struct sk_buff *skb)
75990af1059SBrandon Streiff {
76090af1059SBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
76190af1059SBrandon Streiff 
762cfd12c06SYangbo Lu 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
763cfd12c06SYangbo Lu 		return;
764cfd12c06SYangbo Lu 
76590af1059SBrandon Streiff 	if (!ds->ops->port_txtstamp)
76690af1059SBrandon Streiff 		return;
76790af1059SBrandon Streiff 
7685c5416f5SYangbo Lu 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
76990af1059SBrandon Streiff }
77090af1059SBrandon Streiff 
77197a69a0dSVladimir Oltean netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
77297a69a0dSVladimir Oltean {
77397a69a0dSVladimir Oltean 	/* SKB for netpoll still need to be mangled with the protocol-specific
77497a69a0dSVladimir Oltean 	 * tag to be successfully transmitted
77597a69a0dSVladimir Oltean 	 */
77697a69a0dSVladimir Oltean 	if (unlikely(netpoll_tx_running(dev)))
77797a69a0dSVladimir Oltean 		return dsa_slave_netpoll_send_skb(dev, skb);
77897a69a0dSVladimir Oltean 
77997a69a0dSVladimir Oltean 	/* Queue the SKB for transmission on the parent interface, but
78097a69a0dSVladimir Oltean 	 * do not modify its EtherType
78197a69a0dSVladimir Oltean 	 */
78297a69a0dSVladimir Oltean 	skb->dev = dsa_slave_to_master(dev);
78397a69a0dSVladimir Oltean 	dev_queue_xmit(skb);
78497a69a0dSVladimir Oltean 
78597a69a0dSVladimir Oltean 	return NETDEV_TX_OK;
78697a69a0dSVladimir Oltean }
78797a69a0dSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
78897a69a0dSVladimir Oltean 
789a3b0b647SVladimir Oltean static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
790a3b0b647SVladimir Oltean {
791a3b0b647SVladimir Oltean 	int needed_headroom = dev->needed_headroom;
792a3b0b647SVladimir Oltean 	int needed_tailroom = dev->needed_tailroom;
793a3b0b647SVladimir Oltean 
794a3b0b647SVladimir Oltean 	/* For tail taggers, we need to pad short frames ourselves, to ensure
795a3b0b647SVladimir Oltean 	 * that the tail tag does not fail at its role of being at the end of
796a3b0b647SVladimir Oltean 	 * the packet, once the master interface pads the frame. Account for
797a3b0b647SVladimir Oltean 	 * that pad length here, and pad later.
798a3b0b647SVladimir Oltean 	 */
799a3b0b647SVladimir Oltean 	if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
800a3b0b647SVladimir Oltean 		needed_tailroom += ETH_ZLEN - skb->len;
801a3b0b647SVladimir Oltean 	/* skb_headroom() returns unsigned int... */
802a3b0b647SVladimir Oltean 	needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
803a3b0b647SVladimir Oltean 	needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
804a3b0b647SVladimir Oltean 
805a3b0b647SVladimir Oltean 	if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
806a3b0b647SVladimir Oltean 		/* No reallocation needed, yay! */
807a3b0b647SVladimir Oltean 		return 0;
808a3b0b647SVladimir Oltean 
809a3b0b647SVladimir Oltean 	return pskb_expand_head(skb, needed_headroom, needed_tailroom,
810a3b0b647SVladimir Oltean 				GFP_ATOMIC);
811a3b0b647SVladimir Oltean }
812a3b0b647SVladimir Oltean 
8133e8a72d1SFlorian Fainelli static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
8143e8a72d1SFlorian Fainelli {
8153e8a72d1SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
8164ed70ce9SFlorian Fainelli 	struct sk_buff *nskb;
8173e8a72d1SFlorian Fainelli 
8186a900628SHeiner Kallweit 	dev_sw_netstats_tx_add(dev, 1, skb->len);
8193e8a72d1SFlorian Fainelli 
820c4b364ceSYangbo Lu 	memset(skb->cb, 0, sizeof(skb->cb));
82187671375SVladimir Oltean 
822cf536ea3SYangbo Lu 	/* Handle tx timestamp if any */
82390af1059SBrandon Streiff 	dsa_skb_tx_timestamp(p, skb);
82490af1059SBrandon Streiff 
825a3b0b647SVladimir Oltean 	if (dsa_realloc_skb(skb, dev)) {
826a3b0b647SVladimir Oltean 		dev_kfree_skb_any(skb);
827a3b0b647SVladimir Oltean 		return NETDEV_TX_OK;
828a3b0b647SVladimir Oltean 	}
829a3b0b647SVladimir Oltean 
830a3b0b647SVladimir Oltean 	/* needed_tailroom should still be 'warm' in the cache line from
831a3b0b647SVladimir Oltean 	 * dsa_realloc_skb(), which has also ensured that padding is safe.
832a3b0b647SVladimir Oltean 	 */
833a3b0b647SVladimir Oltean 	if (dev->needed_tailroom)
834a3b0b647SVladimir Oltean 		eth_skb_pad(skb);
835a3b0b647SVladimir Oltean 
836fe47d563SVivien Didelot 	/* Transmit function may have to reallocate the original SKB,
837fe47d563SVivien Didelot 	 * in which case it must have freed it. Only free it here on error.
838fe47d563SVivien Didelot 	 */
8394ed70ce9SFlorian Fainelli 	nskb = p->xmit(skb, dev);
840fe47d563SVivien Didelot 	if (!nskb) {
841fe47d563SVivien Didelot 		kfree_skb(skb);
8424ed70ce9SFlorian Fainelli 		return NETDEV_TX_OK;
843fe47d563SVivien Didelot 	}
8445aed85ceSFlorian Fainelli 
84597a69a0dSVladimir Oltean 	return dsa_enqueue_skb(nskb, dev);
84697a69a0dSVladimir Oltean }
84704ff53f9SFlorian Fainelli 
84891da11f8SLennert Buytenhek /* ethtool operations *******************************************************/
84991da11f8SLennert Buytenhek 
85091da11f8SLennert Buytenhek static void dsa_slave_get_drvinfo(struct net_device *dev,
85191da11f8SLennert Buytenhek 				  struct ethtool_drvinfo *drvinfo)
85291da11f8SLennert Buytenhek {
8537826d43fSJiri Pirko 	strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
8547826d43fSJiri Pirko 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
8557826d43fSJiri Pirko 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
85691da11f8SLennert Buytenhek }
85791da11f8SLennert Buytenhek 
8583d762a0fSGuenter Roeck static int dsa_slave_get_regs_len(struct net_device *dev)
8593d762a0fSGuenter Roeck {
860d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
861d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8623d762a0fSGuenter Roeck 
8639d490b4eSVivien Didelot 	if (ds->ops->get_regs_len)
864d945097bSVivien Didelot 		return ds->ops->get_regs_len(ds, dp->index);
8653d762a0fSGuenter Roeck 
8663d762a0fSGuenter Roeck 	return -EOPNOTSUPP;
8673d762a0fSGuenter Roeck }
8683d762a0fSGuenter Roeck 
8693d762a0fSGuenter Roeck static void
8703d762a0fSGuenter Roeck dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
8713d762a0fSGuenter Roeck {
872d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
873d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8743d762a0fSGuenter Roeck 
8759d490b4eSVivien Didelot 	if (ds->ops->get_regs)
876d945097bSVivien Didelot 		ds->ops->get_regs(ds, dp->index, regs, _p);
8773d762a0fSGuenter Roeck }
8783d762a0fSGuenter Roeck 
879aab9c406SFlorian Fainelli static int dsa_slave_nway_reset(struct net_device *dev)
880aab9c406SFlorian Fainelli {
881aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
882aab9c406SFlorian Fainelli 
883aab9c406SFlorian Fainelli 	return phylink_ethtool_nway_reset(dp->pl);
884aab9c406SFlorian Fainelli }
885aab9c406SFlorian Fainelli 
8866793abb4SGuenter Roeck static int dsa_slave_get_eeprom_len(struct net_device *dev)
8876793abb4SGuenter Roeck {
888d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
889d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
8906793abb4SGuenter Roeck 
8910e576044SAndrew Lunn 	if (ds->cd && ds->cd->eeprom_len)
892ff04955cSAndrew Lunn 		return ds->cd->eeprom_len;
8936793abb4SGuenter Roeck 
8949d490b4eSVivien Didelot 	if (ds->ops->get_eeprom_len)
8959d490b4eSVivien Didelot 		return ds->ops->get_eeprom_len(ds);
8966793abb4SGuenter Roeck 
8976793abb4SGuenter Roeck 	return 0;
8986793abb4SGuenter Roeck }
8996793abb4SGuenter Roeck 
9006793abb4SGuenter Roeck static int dsa_slave_get_eeprom(struct net_device *dev,
9016793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
9026793abb4SGuenter Roeck {
903d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
904d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9056793abb4SGuenter Roeck 
9069d490b4eSVivien Didelot 	if (ds->ops->get_eeprom)
9079d490b4eSVivien Didelot 		return ds->ops->get_eeprom(ds, eeprom, data);
9086793abb4SGuenter Roeck 
9096793abb4SGuenter Roeck 	return -EOPNOTSUPP;
9106793abb4SGuenter Roeck }
9116793abb4SGuenter Roeck 
9126793abb4SGuenter Roeck static int dsa_slave_set_eeprom(struct net_device *dev,
9136793abb4SGuenter Roeck 				struct ethtool_eeprom *eeprom, u8 *data)
9146793abb4SGuenter Roeck {
915d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
916d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9176793abb4SGuenter Roeck 
9189d490b4eSVivien Didelot 	if (ds->ops->set_eeprom)
9199d490b4eSVivien Didelot 		return ds->ops->set_eeprom(ds, eeprom, data);
9206793abb4SGuenter Roeck 
9216793abb4SGuenter Roeck 	return -EOPNOTSUPP;
9226793abb4SGuenter Roeck }
9236793abb4SGuenter Roeck 
92491da11f8SLennert Buytenhek static void dsa_slave_get_strings(struct net_device *dev,
92591da11f8SLennert Buytenhek 				  uint32_t stringset, uint8_t *data)
92691da11f8SLennert Buytenhek {
927d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
928d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
92991da11f8SLennert Buytenhek 
93091da11f8SLennert Buytenhek 	if (stringset == ETH_SS_STATS) {
93191da11f8SLennert Buytenhek 		int len = ETH_GSTRING_LEN;
93291da11f8SLennert Buytenhek 
93391da11f8SLennert Buytenhek 		strncpy(data, "tx_packets", len);
93491da11f8SLennert Buytenhek 		strncpy(data + len, "tx_bytes", len);
93591da11f8SLennert Buytenhek 		strncpy(data + 2 * len, "rx_packets", len);
93691da11f8SLennert Buytenhek 		strncpy(data + 3 * len, "rx_bytes", len);
9379d490b4eSVivien Didelot 		if (ds->ops->get_strings)
93889f09048SFlorian Fainelli 			ds->ops->get_strings(ds, dp->index, stringset,
93989f09048SFlorian Fainelli 					     data + 4 * len);
940a71acad9SOleksij Rempel 	} else if (stringset ==  ETH_SS_TEST) {
941a71acad9SOleksij Rempel 		net_selftest_get_strings(data);
94291da11f8SLennert Buytenhek 	}
943a71acad9SOleksij Rempel 
94491da11f8SLennert Buytenhek }
94591da11f8SLennert Buytenhek 
94691da11f8SLennert Buytenhek static void dsa_slave_get_ethtool_stats(struct net_device *dev,
94791da11f8SLennert Buytenhek 					struct ethtool_stats *stats,
94891da11f8SLennert Buytenhek 					uint64_t *data)
94991da11f8SLennert Buytenhek {
950d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
951d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
9525f6b4e14SFlorian Fainelli 	struct pcpu_sw_netstats *s;
953f613ed66SFlorian Fainelli 	unsigned int start;
9545f6b4e14SFlorian Fainelli 	int i;
95591da11f8SLennert Buytenhek 
9565f6b4e14SFlorian Fainelli 	for_each_possible_cpu(i) {
9575f6b4e14SFlorian Fainelli 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
9585f6b4e14SFlorian Fainelli 
9596a900628SHeiner Kallweit 		s = per_cpu_ptr(dev->tstats, i);
960f613ed66SFlorian Fainelli 		do {
9615f6b4e14SFlorian Fainelli 			start = u64_stats_fetch_begin_irq(&s->syncp);
9625f6b4e14SFlorian Fainelli 			tx_packets = s->tx_packets;
9635f6b4e14SFlorian Fainelli 			tx_bytes = s->tx_bytes;
9645f6b4e14SFlorian Fainelli 			rx_packets = s->rx_packets;
9655f6b4e14SFlorian Fainelli 			rx_bytes = s->rx_bytes;
9665f6b4e14SFlorian Fainelli 		} while (u64_stats_fetch_retry_irq(&s->syncp, start));
9675f6b4e14SFlorian Fainelli 		data[0] += tx_packets;
9685f6b4e14SFlorian Fainelli 		data[1] += tx_bytes;
9695f6b4e14SFlorian Fainelli 		data[2] += rx_packets;
9705f6b4e14SFlorian Fainelli 		data[3] += rx_bytes;
9715f6b4e14SFlorian Fainelli 	}
9729d490b4eSVivien Didelot 	if (ds->ops->get_ethtool_stats)
973d945097bSVivien Didelot 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
97491da11f8SLennert Buytenhek }
97591da11f8SLennert Buytenhek 
97691da11f8SLennert Buytenhek static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
97791da11f8SLennert Buytenhek {
978d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
979d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
98091da11f8SLennert Buytenhek 
98191da11f8SLennert Buytenhek 	if (sset == ETH_SS_STATS) {
982b94cbc90SVladimir Oltean 		int count = 0;
98391da11f8SLennert Buytenhek 
984b94cbc90SVladimir Oltean 		if (ds->ops->get_sset_count) {
985b94cbc90SVladimir Oltean 			count = ds->ops->get_sset_count(ds, dp->index, sset);
986b94cbc90SVladimir Oltean 			if (count < 0)
98791da11f8SLennert Buytenhek 				return count;
988b94cbc90SVladimir Oltean 		}
989b94cbc90SVladimir Oltean 
990b94cbc90SVladimir Oltean 		return count + 4;
991a71acad9SOleksij Rempel 	} else if (sset ==  ETH_SS_TEST) {
992a71acad9SOleksij Rempel 		return net_selftest_get_count();
99391da11f8SLennert Buytenhek 	}
99491da11f8SLennert Buytenhek 
99591da11f8SLennert Buytenhek 	return -EOPNOTSUPP;
99691da11f8SLennert Buytenhek }
99791da11f8SLennert Buytenhek 
998487d3855SAlvin Šipraga static void dsa_slave_get_eth_phy_stats(struct net_device *dev,
999487d3855SAlvin Šipraga 					struct ethtool_eth_phy_stats *phy_stats)
1000487d3855SAlvin Šipraga {
1001487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
1002487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
1003487d3855SAlvin Šipraga 
1004487d3855SAlvin Šipraga 	if (ds->ops->get_eth_phy_stats)
1005487d3855SAlvin Šipraga 		ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1006487d3855SAlvin Šipraga }
1007487d3855SAlvin Šipraga 
1008487d3855SAlvin Šipraga static void dsa_slave_get_eth_mac_stats(struct net_device *dev,
1009487d3855SAlvin Šipraga 					struct ethtool_eth_mac_stats *mac_stats)
1010487d3855SAlvin Šipraga {
1011487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
1012487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
1013487d3855SAlvin Šipraga 
1014487d3855SAlvin Šipraga 	if (ds->ops->get_eth_mac_stats)
1015487d3855SAlvin Šipraga 		ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1016487d3855SAlvin Šipraga }
1017487d3855SAlvin Šipraga 
1018487d3855SAlvin Šipraga static void
1019487d3855SAlvin Šipraga dsa_slave_get_eth_ctrl_stats(struct net_device *dev,
1020487d3855SAlvin Šipraga 			     struct ethtool_eth_ctrl_stats *ctrl_stats)
1021487d3855SAlvin Šipraga {
1022487d3855SAlvin Šipraga 	struct dsa_port *dp = dsa_slave_to_port(dev);
1023487d3855SAlvin Šipraga 	struct dsa_switch *ds = dp->ds;
1024487d3855SAlvin Šipraga 
1025487d3855SAlvin Šipraga 	if (ds->ops->get_eth_ctrl_stats)
1026487d3855SAlvin Šipraga 		ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1027487d3855SAlvin Šipraga }
1028487d3855SAlvin Šipraga 
1029a71acad9SOleksij Rempel static void dsa_slave_net_selftest(struct net_device *ndev,
1030a71acad9SOleksij Rempel 				   struct ethtool_test *etest, u64 *buf)
1031a71acad9SOleksij Rempel {
1032a71acad9SOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(ndev);
1033a71acad9SOleksij Rempel 	struct dsa_switch *ds = dp->ds;
1034a71acad9SOleksij Rempel 
1035a71acad9SOleksij Rempel 	if (ds->ops->self_test) {
1036a71acad9SOleksij Rempel 		ds->ops->self_test(ds, dp->index, etest, buf);
1037a71acad9SOleksij Rempel 		return;
1038a71acad9SOleksij Rempel 	}
1039a71acad9SOleksij Rempel 
1040a71acad9SOleksij Rempel 	net_selftest(ndev, etest, buf);
1041a71acad9SOleksij Rempel }
1042a71acad9SOleksij Rempel 
104319e57c4eSFlorian Fainelli static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
104419e57c4eSFlorian Fainelli {
1045d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1046d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
104719e57c4eSFlorian Fainelli 
1048aab9c406SFlorian Fainelli 	phylink_ethtool_get_wol(dp->pl, w);
1049aab9c406SFlorian Fainelli 
10509d490b4eSVivien Didelot 	if (ds->ops->get_wol)
1051d945097bSVivien Didelot 		ds->ops->get_wol(ds, dp->index, w);
105219e57c4eSFlorian Fainelli }
105319e57c4eSFlorian Fainelli 
105419e57c4eSFlorian Fainelli static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
105519e57c4eSFlorian Fainelli {
1056d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1057d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
105819e57c4eSFlorian Fainelli 	int ret = -EOPNOTSUPP;
105919e57c4eSFlorian Fainelli 
1060aab9c406SFlorian Fainelli 	phylink_ethtool_set_wol(dp->pl, w);
1061aab9c406SFlorian Fainelli 
10629d490b4eSVivien Didelot 	if (ds->ops->set_wol)
1063d945097bSVivien Didelot 		ret = ds->ops->set_wol(ds, dp->index, w);
106419e57c4eSFlorian Fainelli 
106519e57c4eSFlorian Fainelli 	return ret;
106619e57c4eSFlorian Fainelli }
106719e57c4eSFlorian Fainelli 
10687905288fSFlorian Fainelli static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
10697905288fSFlorian Fainelli {
1070d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1071d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
10727905288fSFlorian Fainelli 	int ret;
10737905288fSFlorian Fainelli 
10747b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
107500670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
10767b9cc738SVivien Didelot 		return -ENODEV;
10777b9cc738SVivien Didelot 
107808f50061SVivien Didelot 	if (!ds->ops->set_mac_eee)
10797905288fSFlorian Fainelli 		return -EOPNOTSUPP;
10807905288fSFlorian Fainelli 
1081d945097bSVivien Didelot 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
10827905288fSFlorian Fainelli 	if (ret)
10837905288fSFlorian Fainelli 		return ret;
10847905288fSFlorian Fainelli 
1085aab9c406SFlorian Fainelli 	return phylink_ethtool_set_eee(dp->pl, e);
10867905288fSFlorian Fainelli }
10877905288fSFlorian Fainelli 
10887905288fSFlorian Fainelli static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
10897905288fSFlorian Fainelli {
1090d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1091d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
10927905288fSFlorian Fainelli 	int ret;
10937905288fSFlorian Fainelli 
10947b9cc738SVivien Didelot 	/* Port's PHY and MAC both need to be EEE capable */
109500670cb8SDan Carpenter 	if (!dev->phydev || !dp->pl)
10967b9cc738SVivien Didelot 		return -ENODEV;
10977b9cc738SVivien Didelot 
109808f50061SVivien Didelot 	if (!ds->ops->get_mac_eee)
10997905288fSFlorian Fainelli 		return -EOPNOTSUPP;
11007905288fSFlorian Fainelli 
1101d945097bSVivien Didelot 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
11027905288fSFlorian Fainelli 	if (ret)
11037905288fSFlorian Fainelli 		return ret;
11047905288fSFlorian Fainelli 
1105aab9c406SFlorian Fainelli 	return phylink_ethtool_get_eee(dp->pl, e);
1106aab9c406SFlorian Fainelli }
1107aab9c406SFlorian Fainelli 
1108aab9c406SFlorian Fainelli static int dsa_slave_get_link_ksettings(struct net_device *dev,
1109aab9c406SFlorian Fainelli 					struct ethtool_link_ksettings *cmd)
1110aab9c406SFlorian Fainelli {
1111aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1112aab9c406SFlorian Fainelli 
1113aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
1114aab9c406SFlorian Fainelli }
1115aab9c406SFlorian Fainelli 
1116aab9c406SFlorian Fainelli static int dsa_slave_set_link_ksettings(struct net_device *dev,
1117aab9c406SFlorian Fainelli 					const struct ethtool_link_ksettings *cmd)
1118aab9c406SFlorian Fainelli {
1119aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
1120aab9c406SFlorian Fainelli 
1121aab9c406SFlorian Fainelli 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
11227905288fSFlorian Fainelli }
11237905288fSFlorian Fainelli 
1124a2a1a13bSHeiner Kallweit static void dsa_slave_get_pauseparam(struct net_device *dev,
1125a2a1a13bSHeiner Kallweit 				     struct ethtool_pauseparam *pause)
1126a2a1a13bSHeiner Kallweit {
1127a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
1128a2a1a13bSHeiner Kallweit 
1129a2a1a13bSHeiner Kallweit 	phylink_ethtool_get_pauseparam(dp->pl, pause);
1130a2a1a13bSHeiner Kallweit }
1131a2a1a13bSHeiner Kallweit 
1132a2a1a13bSHeiner Kallweit static int dsa_slave_set_pauseparam(struct net_device *dev,
1133a2a1a13bSHeiner Kallweit 				    struct ethtool_pauseparam *pause)
1134a2a1a13bSHeiner Kallweit {
1135a2a1a13bSHeiner Kallweit 	struct dsa_port *dp = dsa_slave_to_port(dev);
1136a2a1a13bSHeiner Kallweit 
1137a2a1a13bSHeiner Kallweit 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
1138a2a1a13bSHeiner Kallweit }
1139a2a1a13bSHeiner Kallweit 
114004ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
114104ff53f9SFlorian Fainelli static int dsa_slave_netpoll_setup(struct net_device *dev,
114204ff53f9SFlorian Fainelli 				   struct netpoll_info *ni)
114304ff53f9SFlorian Fainelli {
1144d0006b00SVivien Didelot 	struct net_device *master = dsa_slave_to_master(dev);
114504ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
114604ff53f9SFlorian Fainelli 	struct netpoll *netpoll;
114704ff53f9SFlorian Fainelli 	int err = 0;
114804ff53f9SFlorian Fainelli 
114904ff53f9SFlorian Fainelli 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
115004ff53f9SFlorian Fainelli 	if (!netpoll)
115104ff53f9SFlorian Fainelli 		return -ENOMEM;
115204ff53f9SFlorian Fainelli 
115304ff53f9SFlorian Fainelli 	err = __netpoll_setup(netpoll, master);
115404ff53f9SFlorian Fainelli 	if (err) {
115504ff53f9SFlorian Fainelli 		kfree(netpoll);
115604ff53f9SFlorian Fainelli 		goto out;
115704ff53f9SFlorian Fainelli 	}
115804ff53f9SFlorian Fainelli 
115904ff53f9SFlorian Fainelli 	p->netpoll = netpoll;
116004ff53f9SFlorian Fainelli out:
116104ff53f9SFlorian Fainelli 	return err;
116204ff53f9SFlorian Fainelli }
116304ff53f9SFlorian Fainelli 
116404ff53f9SFlorian Fainelli static void dsa_slave_netpoll_cleanup(struct net_device *dev)
116504ff53f9SFlorian Fainelli {
116604ff53f9SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
116704ff53f9SFlorian Fainelli 	struct netpoll *netpoll = p->netpoll;
116804ff53f9SFlorian Fainelli 
116904ff53f9SFlorian Fainelli 	if (!netpoll)
117004ff53f9SFlorian Fainelli 		return;
117104ff53f9SFlorian Fainelli 
117204ff53f9SFlorian Fainelli 	p->netpoll = NULL;
117304ff53f9SFlorian Fainelli 
1174c9fbd71fSDebabrata Banerjee 	__netpoll_free(netpoll);
117504ff53f9SFlorian Fainelli }
117604ff53f9SFlorian Fainelli 
117704ff53f9SFlorian Fainelli static void dsa_slave_poll_controller(struct net_device *dev)
117804ff53f9SFlorian Fainelli {
117904ff53f9SFlorian Fainelli }
118004ff53f9SFlorian Fainelli #endif
118104ff53f9SFlorian Fainelli 
1182f50f2127SFlorian Fainelli static struct dsa_mall_tc_entry *
11834fa7b718SVivien Didelot dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1184f50f2127SFlorian Fainelli {
11854fa7b718SVivien Didelot 	struct dsa_slave_priv *p = netdev_priv(dev);
1186f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1187f50f2127SFlorian Fainelli 
1188f50f2127SFlorian Fainelli 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1189f50f2127SFlorian Fainelli 		if (mall_tc_entry->cookie == cookie)
1190f50f2127SFlorian Fainelli 			return mall_tc_entry;
1191f50f2127SFlorian Fainelli 
1192f50f2127SFlorian Fainelli 	return NULL;
1193f50f2127SFlorian Fainelli }
1194f50f2127SFlorian Fainelli 
1195e13c2075SVladimir Oltean static int
1196e13c2075SVladimir Oltean dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
1197f50f2127SFlorian Fainelli 				  struct tc_cls_matchall_offload *cls,
1198f50f2127SFlorian Fainelli 				  bool ingress)
1199f50f2127SFlorian Fainelli {
12000148bb50SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
1201d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1202f50f2127SFlorian Fainelli 	struct dsa_slave_priv *p = netdev_priv(dev);
1203e13c2075SVladimir Oltean 	struct dsa_mall_mirror_tc_entry *mirror;
1204f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1205d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
12069681e8b3SPieter Jansen van Vuuren 	struct flow_action_entry *act;
1207d945097bSVivien Didelot 	struct dsa_port *to_dp;
1208e13c2075SVladimir Oltean 	int err;
1209e13c2075SVladimir Oltean 
1210f50f2127SFlorian Fainelli 	if (!ds->ops->port_mirror_add)
121134297176SVladimir Oltean 		return -EOPNOTSUPP;
1212f50f2127SFlorian Fainelli 
121353eca1f3SJakub Kicinski 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1214319a1d19SJiri Pirko 					      cls->common.extack))
121534297176SVladimir Oltean 		return -EOPNOTSUPP;
1216319a1d19SJiri Pirko 
12179681e8b3SPieter Jansen van Vuuren 	act = &cls->rule->action.entries[0];
1218f50f2127SFlorian Fainelli 
121965722159SVladimir Oltean 	if (!act->dev)
122065722159SVladimir Oltean 		return -EINVAL;
122165722159SVladimir Oltean 
12229681e8b3SPieter Jansen van Vuuren 	if (!dsa_slave_dev_check(act->dev))
1223f50f2127SFlorian Fainelli 		return -EOPNOTSUPP;
1224f50f2127SFlorian Fainelli 
1225f50f2127SFlorian Fainelli 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1226f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1227f50f2127SFlorian Fainelli 		return -ENOMEM;
1228f50f2127SFlorian Fainelli 
1229f50f2127SFlorian Fainelli 	mall_tc_entry->cookie = cls->cookie;
1230f50f2127SFlorian Fainelli 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1231f50f2127SFlorian Fainelli 	mirror = &mall_tc_entry->mirror;
1232f50f2127SFlorian Fainelli 
12339681e8b3SPieter Jansen van Vuuren 	to_dp = dsa_slave_to_port(act->dev);
1234f50f2127SFlorian Fainelli 
1235d945097bSVivien Didelot 	mirror->to_local_port = to_dp->index;
1236f50f2127SFlorian Fainelli 	mirror->ingress = ingress;
1237f50f2127SFlorian Fainelli 
12380148bb50SVladimir Oltean 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1239f50f2127SFlorian Fainelli 	if (err) {
1240f50f2127SFlorian Fainelli 		kfree(mall_tc_entry);
1241f50f2127SFlorian Fainelli 		return err;
1242f50f2127SFlorian Fainelli 	}
1243f50f2127SFlorian Fainelli 
1244f50f2127SFlorian Fainelli 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1245e13c2075SVladimir Oltean 
1246e13c2075SVladimir Oltean 	return err;
1247f50f2127SFlorian Fainelli }
1248f50f2127SFlorian Fainelli 
124934297176SVladimir Oltean static int
125034297176SVladimir Oltean dsa_slave_add_cls_matchall_police(struct net_device *dev,
125134297176SVladimir Oltean 				  struct tc_cls_matchall_offload *cls,
125234297176SVladimir Oltean 				  bool ingress)
125334297176SVladimir Oltean {
125434297176SVladimir Oltean 	struct netlink_ext_ack *extack = cls->common.extack;
125534297176SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
125634297176SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
125734297176SVladimir Oltean 	struct dsa_mall_policer_tc_entry *policer;
125834297176SVladimir Oltean 	struct dsa_mall_tc_entry *mall_tc_entry;
125934297176SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
126034297176SVladimir Oltean 	struct flow_action_entry *act;
126134297176SVladimir Oltean 	int err;
126234297176SVladimir Oltean 
126334297176SVladimir Oltean 	if (!ds->ops->port_policer_add) {
126434297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1265c75a33c8SJacob Keller 				   "Policing offload not implemented");
126634297176SVladimir Oltean 		return -EOPNOTSUPP;
126734297176SVladimir Oltean 	}
126834297176SVladimir Oltean 
126934297176SVladimir Oltean 	if (!ingress) {
127034297176SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1271c75a33c8SJacob Keller 				   "Only supported on ingress qdisc");
127234297176SVladimir Oltean 		return -EOPNOTSUPP;
127334297176SVladimir Oltean 	}
127434297176SVladimir Oltean 
127534297176SVladimir Oltean 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
127634297176SVladimir Oltean 					      cls->common.extack))
127734297176SVladimir Oltean 		return -EOPNOTSUPP;
127834297176SVladimir Oltean 
127934297176SVladimir Oltean 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
128034297176SVladimir Oltean 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
128134297176SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
1282c75a33c8SJacob Keller 					   "Only one port policer allowed");
128334297176SVladimir Oltean 			return -EEXIST;
128434297176SVladimir Oltean 		}
128534297176SVladimir Oltean 	}
128634297176SVladimir Oltean 
128734297176SVladimir Oltean 	act = &cls->rule->action.entries[0];
128834297176SVladimir Oltean 
128934297176SVladimir Oltean 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
129034297176SVladimir Oltean 	if (!mall_tc_entry)
129134297176SVladimir Oltean 		return -ENOMEM;
129234297176SVladimir Oltean 
129334297176SVladimir Oltean 	mall_tc_entry->cookie = cls->cookie;
129434297176SVladimir Oltean 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
129534297176SVladimir Oltean 	policer = &mall_tc_entry->policer;
129634297176SVladimir Oltean 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
129734297176SVladimir Oltean 	policer->burst = act->police.burst;
129834297176SVladimir Oltean 
129934297176SVladimir Oltean 	err = ds->ops->port_policer_add(ds, dp->index, policer);
130034297176SVladimir Oltean 	if (err) {
130134297176SVladimir Oltean 		kfree(mall_tc_entry);
130234297176SVladimir Oltean 		return err;
130334297176SVladimir Oltean 	}
130434297176SVladimir Oltean 
130534297176SVladimir Oltean 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
130634297176SVladimir Oltean 
130734297176SVladimir Oltean 	return err;
130834297176SVladimir Oltean }
130934297176SVladimir Oltean 
1310e13c2075SVladimir Oltean static int dsa_slave_add_cls_matchall(struct net_device *dev,
1311e13c2075SVladimir Oltean 				      struct tc_cls_matchall_offload *cls,
1312e13c2075SVladimir Oltean 				      bool ingress)
1313e13c2075SVladimir Oltean {
1314e13c2075SVladimir Oltean 	int err = -EOPNOTSUPP;
1315e13c2075SVladimir Oltean 
1316e13c2075SVladimir Oltean 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1317e13c2075SVladimir Oltean 	    flow_offload_has_one_action(&cls->rule->action) &&
1318e13c2075SVladimir Oltean 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1319e13c2075SVladimir Oltean 		err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
132034297176SVladimir Oltean 	else if (flow_offload_has_one_action(&cls->rule->action) &&
132134297176SVladimir Oltean 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
132234297176SVladimir Oltean 		err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
1323e13c2075SVladimir Oltean 
1324e13c2075SVladimir Oltean 	return err;
1325f50f2127SFlorian Fainelli }
1326f50f2127SFlorian Fainelli 
1327f50f2127SFlorian Fainelli static void dsa_slave_del_cls_matchall(struct net_device *dev,
1328f50f2127SFlorian Fainelli 				       struct tc_cls_matchall_offload *cls)
1329f50f2127SFlorian Fainelli {
1330d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1331f50f2127SFlorian Fainelli 	struct dsa_mall_tc_entry *mall_tc_entry;
1332d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1333f50f2127SFlorian Fainelli 
13344fa7b718SVivien Didelot 	mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
1335f50f2127SFlorian Fainelli 	if (!mall_tc_entry)
1336f50f2127SFlorian Fainelli 		return;
1337f50f2127SFlorian Fainelli 
1338f50f2127SFlorian Fainelli 	list_del(&mall_tc_entry->list);
1339f50f2127SFlorian Fainelli 
1340f50f2127SFlorian Fainelli 	switch (mall_tc_entry->type) {
1341f50f2127SFlorian Fainelli 	case DSA_PORT_MALL_MIRROR:
134234297176SVladimir Oltean 		if (ds->ops->port_mirror_del)
134334297176SVladimir Oltean 			ds->ops->port_mirror_del(ds, dp->index,
134434297176SVladimir Oltean 						 &mall_tc_entry->mirror);
134534297176SVladimir Oltean 		break;
134634297176SVladimir Oltean 	case DSA_PORT_MALL_POLICER:
134734297176SVladimir Oltean 		if (ds->ops->port_policer_del)
134834297176SVladimir Oltean 			ds->ops->port_policer_del(ds, dp->index);
1349f50f2127SFlorian Fainelli 		break;
1350f50f2127SFlorian Fainelli 	default:
1351f50f2127SFlorian Fainelli 		WARN_ON(1);
1352f50f2127SFlorian Fainelli 	}
1353f50f2127SFlorian Fainelli 
1354f50f2127SFlorian Fainelli 	kfree(mall_tc_entry);
1355f50f2127SFlorian Fainelli }
1356f50f2127SFlorian Fainelli 
13573fbae382SJiri Pirko static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
13586b3eb752SJiri Pirko 					   struct tc_cls_matchall_offload *cls,
13596b3eb752SJiri Pirko 					   bool ingress)
1360f50f2127SFlorian Fainelli {
13615fd9fc4eSJiri Pirko 	if (cls->common.chain_index)
1362a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
1363f50f2127SFlorian Fainelli 
13643fbae382SJiri Pirko 	switch (cls->command) {
13653fbae382SJiri Pirko 	case TC_CLSMATCHALL_REPLACE:
13665fd9fc4eSJiri Pirko 		return dsa_slave_add_cls_matchall(dev, cls, ingress);
13673fbae382SJiri Pirko 	case TC_CLSMATCHALL_DESTROY:
13683fbae382SJiri Pirko 		dsa_slave_del_cls_matchall(dev, cls);
13693fbae382SJiri Pirko 		return 0;
13703fbae382SJiri Pirko 	default:
13713fbae382SJiri Pirko 		return -EOPNOTSUPP;
13723fbae382SJiri Pirko 	}
13733fbae382SJiri Pirko }
13743fbae382SJiri Pirko 
1375ed11bb1fSVladimir Oltean static int dsa_slave_add_cls_flower(struct net_device *dev,
1376ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1377ed11bb1fSVladimir Oltean 				    bool ingress)
1378ed11bb1fSVladimir Oltean {
1379ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1380ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1381ed11bb1fSVladimir Oltean 	int port = dp->index;
1382ed11bb1fSVladimir Oltean 
1383ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_add)
1384ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1385ed11bb1fSVladimir Oltean 
1386ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1387ed11bb1fSVladimir Oltean }
1388ed11bb1fSVladimir Oltean 
1389ed11bb1fSVladimir Oltean static int dsa_slave_del_cls_flower(struct net_device *dev,
1390ed11bb1fSVladimir Oltean 				    struct flow_cls_offload *cls,
1391ed11bb1fSVladimir Oltean 				    bool ingress)
1392ed11bb1fSVladimir Oltean {
1393ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1394ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1395ed11bb1fSVladimir Oltean 	int port = dp->index;
1396ed11bb1fSVladimir Oltean 
1397ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_del)
1398ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1399ed11bb1fSVladimir Oltean 
1400ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1401ed11bb1fSVladimir Oltean }
1402ed11bb1fSVladimir Oltean 
1403ed11bb1fSVladimir Oltean static int dsa_slave_stats_cls_flower(struct net_device *dev,
1404ed11bb1fSVladimir Oltean 				      struct flow_cls_offload *cls,
1405ed11bb1fSVladimir Oltean 				      bool ingress)
1406ed11bb1fSVladimir Oltean {
1407ed11bb1fSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1408ed11bb1fSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1409ed11bb1fSVladimir Oltean 	int port = dp->index;
1410ed11bb1fSVladimir Oltean 
1411ed11bb1fSVladimir Oltean 	if (!ds->ops->cls_flower_stats)
1412ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1413ed11bb1fSVladimir Oltean 
1414ed11bb1fSVladimir Oltean 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1415ed11bb1fSVladimir Oltean }
1416ed11bb1fSVladimir Oltean 
1417ed11bb1fSVladimir Oltean static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1418ed11bb1fSVladimir Oltean 					 struct flow_cls_offload *cls,
1419ed11bb1fSVladimir Oltean 					 bool ingress)
1420ed11bb1fSVladimir Oltean {
1421ed11bb1fSVladimir Oltean 	switch (cls->command) {
1422ed11bb1fSVladimir Oltean 	case FLOW_CLS_REPLACE:
1423ed11bb1fSVladimir Oltean 		return dsa_slave_add_cls_flower(dev, cls, ingress);
1424ed11bb1fSVladimir Oltean 	case FLOW_CLS_DESTROY:
1425ed11bb1fSVladimir Oltean 		return dsa_slave_del_cls_flower(dev, cls, ingress);
1426ed11bb1fSVladimir Oltean 	case FLOW_CLS_STATS:
1427ed11bb1fSVladimir Oltean 		return dsa_slave_stats_cls_flower(dev, cls, ingress);
1428ed11bb1fSVladimir Oltean 	default:
1429ed11bb1fSVladimir Oltean 		return -EOPNOTSUPP;
1430ed11bb1fSVladimir Oltean 	}
1431ed11bb1fSVladimir Oltean }
1432ed11bb1fSVladimir Oltean 
14336b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14346b3eb752SJiri Pirko 				       void *cb_priv, bool ingress)
14356b3eb752SJiri Pirko {
14366b3eb752SJiri Pirko 	struct net_device *dev = cb_priv;
14376b3eb752SJiri Pirko 
143844ae12a7SJiri Pirko 	if (!tc_can_offload(dev))
143944ae12a7SJiri Pirko 		return -EOPNOTSUPP;
144044ae12a7SJiri Pirko 
14416b3eb752SJiri Pirko 	switch (type) {
14426b3eb752SJiri Pirko 	case TC_SETUP_CLSMATCHALL:
14436b3eb752SJiri Pirko 		return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
1444ed11bb1fSVladimir Oltean 	case TC_SETUP_CLSFLOWER:
1445ed11bb1fSVladimir Oltean 		return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
14466b3eb752SJiri Pirko 	default:
14476b3eb752SJiri Pirko 		return -EOPNOTSUPP;
14486b3eb752SJiri Pirko 	}
14496b3eb752SJiri Pirko }
14506b3eb752SJiri Pirko 
14516b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
14526b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
14536b3eb752SJiri Pirko {
14546b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
14556b3eb752SJiri Pirko }
14566b3eb752SJiri Pirko 
14576b3eb752SJiri Pirko static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
14586b3eb752SJiri Pirko 					  void *type_data, void *cb_priv)
14596b3eb752SJiri Pirko {
14606b3eb752SJiri Pirko 	return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
14616b3eb752SJiri Pirko }
14626b3eb752SJiri Pirko 
1463955bcb6eSPablo Neira Ayuso static LIST_HEAD(dsa_slave_block_cb_list);
1464955bcb6eSPablo Neira Ayuso 
14656b3eb752SJiri Pirko static int dsa_slave_setup_tc_block(struct net_device *dev,
1466955bcb6eSPablo Neira Ayuso 				    struct flow_block_offload *f)
14676b3eb752SJiri Pirko {
1468955bcb6eSPablo Neira Ayuso 	struct flow_block_cb *block_cb;
1469a7323311SPablo Neira Ayuso 	flow_setup_cb_t *cb;
14706b3eb752SJiri Pirko 
147132f8c409SPablo Neira Ayuso 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
14726b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_ig;
147332f8c409SPablo Neira Ayuso 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
14746b3eb752SJiri Pirko 		cb = dsa_slave_setup_tc_block_cb_eg;
14756b3eb752SJiri Pirko 	else
14766b3eb752SJiri Pirko 		return -EOPNOTSUPP;
14776b3eb752SJiri Pirko 
1478955bcb6eSPablo Neira Ayuso 	f->driver_block_list = &dsa_slave_block_cb_list;
1479955bcb6eSPablo Neira Ayuso 
14806b3eb752SJiri Pirko 	switch (f->command) {
14819c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_BIND:
14820d4fd02eSPablo Neira Ayuso 		if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
14830d4fd02eSPablo Neira Ayuso 			return -EBUSY;
14840d4fd02eSPablo Neira Ayuso 
14850c7294ddSPablo Neira Ayuso 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1486955bcb6eSPablo Neira Ayuso 		if (IS_ERR(block_cb))
1487955bcb6eSPablo Neira Ayuso 			return PTR_ERR(block_cb);
1488955bcb6eSPablo Neira Ayuso 
1489955bcb6eSPablo Neira Ayuso 		flow_block_cb_add(block_cb, f);
1490955bcb6eSPablo Neira Ayuso 		list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1491955bcb6eSPablo Neira Ayuso 		return 0;
14929c0e189eSPablo Neira Ayuso 	case FLOW_BLOCK_UNBIND:
149314bfb13fSPablo Neira Ayuso 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1494955bcb6eSPablo Neira Ayuso 		if (!block_cb)
1495955bcb6eSPablo Neira Ayuso 			return -ENOENT;
1496955bcb6eSPablo Neira Ayuso 
1497955bcb6eSPablo Neira Ayuso 		flow_block_cb_remove(block_cb, f);
1498955bcb6eSPablo Neira Ayuso 		list_del(&block_cb->driver_list);
14996b3eb752SJiri Pirko 		return 0;
15006b3eb752SJiri Pirko 	default:
15016b3eb752SJiri Pirko 		return -EOPNOTSUPP;
15026b3eb752SJiri Pirko 	}
15036b3eb752SJiri Pirko }
15046b3eb752SJiri Pirko 
15053fb24a43SPablo Neira Ayuso static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
15063fb24a43SPablo Neira Ayuso 				    void *type_data)
15073fb24a43SPablo Neira Ayuso {
15083fb24a43SPablo Neira Ayuso 	struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
15093fb24a43SPablo Neira Ayuso 	struct net_device *master = cpu_dp->master;
15103fb24a43SPablo Neira Ayuso 
15113fb24a43SPablo Neira Ayuso 	if (!master->netdev_ops->ndo_setup_tc)
15123fb24a43SPablo Neira Ayuso 		return -EOPNOTSUPP;
15133fb24a43SPablo Neira Ayuso 
15143fb24a43SPablo Neira Ayuso 	return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
15153fb24a43SPablo Neira Ayuso }
15163fb24a43SPablo Neira Ayuso 
15173fbae382SJiri Pirko static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
1518de4784caSJiri Pirko 			      void *type_data)
15193fbae382SJiri Pirko {
152047d23af2SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
152147d23af2SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
152247d23af2SVladimir Oltean 
15233fb24a43SPablo Neira Ayuso 	switch (type) {
15243fb24a43SPablo Neira Ayuso 	case TC_SETUP_BLOCK:
15256b3eb752SJiri Pirko 		return dsa_slave_setup_tc_block(dev, type_data);
15263fb24a43SPablo Neira Ayuso 	case TC_SETUP_FT:
15273fb24a43SPablo Neira Ayuso 		return dsa_slave_setup_ft_block(ds, dp->index, type_data);
15283fb24a43SPablo Neira Ayuso 	default:
15293fb24a43SPablo Neira Ayuso 		break;
15303fb24a43SPablo Neira Ayuso 	}
153147d23af2SVladimir Oltean 
153247d23af2SVladimir Oltean 	if (!ds->ops->port_setup_tc)
1533a5fcf8a6SJiri Pirko 		return -EOPNOTSUPP;
153447d23af2SVladimir Oltean 
153547d23af2SVladimir Oltean 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1536f50f2127SFlorian Fainelli }
1537f50f2127SFlorian Fainelli 
1538bf9f2648SFlorian Fainelli static int dsa_slave_get_rxnfc(struct net_device *dev,
1539bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc, u32 *rule_locs)
1540bf9f2648SFlorian Fainelli {
1541d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1542d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1543bf9f2648SFlorian Fainelli 
1544bf9f2648SFlorian Fainelli 	if (!ds->ops->get_rxnfc)
1545bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1546bf9f2648SFlorian Fainelli 
1547d945097bSVivien Didelot 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1548bf9f2648SFlorian Fainelli }
1549bf9f2648SFlorian Fainelli 
1550bf9f2648SFlorian Fainelli static int dsa_slave_set_rxnfc(struct net_device *dev,
1551bf9f2648SFlorian Fainelli 			       struct ethtool_rxnfc *nfc)
1552bf9f2648SFlorian Fainelli {
1553d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
1554d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
1555bf9f2648SFlorian Fainelli 
1556bf9f2648SFlorian Fainelli 	if (!ds->ops->set_rxnfc)
1557bf9f2648SFlorian Fainelli 		return -EOPNOTSUPP;
1558bf9f2648SFlorian Fainelli 
1559d945097bSVivien Didelot 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1560bf9f2648SFlorian Fainelli }
1561bf9f2648SFlorian Fainelli 
15620336369dSBrandon Streiff static int dsa_slave_get_ts_info(struct net_device *dev,
15630336369dSBrandon Streiff 				 struct ethtool_ts_info *ts)
15640336369dSBrandon Streiff {
15650336369dSBrandon Streiff 	struct dsa_slave_priv *p = netdev_priv(dev);
15660336369dSBrandon Streiff 	struct dsa_switch *ds = p->dp->ds;
15670336369dSBrandon Streiff 
15680336369dSBrandon Streiff 	if (!ds->ops->get_ts_info)
15690336369dSBrandon Streiff 		return -EOPNOTSUPP;
15700336369dSBrandon Streiff 
15710336369dSBrandon Streiff 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
15720336369dSBrandon Streiff }
15730336369dSBrandon Streiff 
1574061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1575061f6a50SFlorian Fainelli 				     u16 vid)
1576061f6a50SFlorian Fainelli {
1577061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
157888236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
157988236591SVladimir Oltean 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1580b7a9e0daSVladimir Oltean 		.vid = vid,
158188236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
158288236591SVladimir Oltean 		.flags = 0,
158388236591SVladimir Oltean 	};
158431046a5fSVladimir Oltean 	struct netlink_ext_ack extack = {0};
1585061f6a50SFlorian Fainelli 	int ret;
1586061f6a50SFlorian Fainelli 
158788236591SVladimir Oltean 	/* User port... */
158831046a5fSVladimir Oltean 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
158931046a5fSVladimir Oltean 	if (ret) {
159031046a5fSVladimir Oltean 		if (extack._msg)
159131046a5fSVladimir Oltean 			netdev_err(dev, "%s\n", extack._msg);
159288236591SVladimir Oltean 		return ret;
159331046a5fSVladimir Oltean 	}
159488236591SVladimir Oltean 
159588236591SVladimir Oltean 	/* And CPU port... */
1596134ef238SVladimir Oltean 	ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
159731046a5fSVladimir Oltean 	if (ret) {
159831046a5fSVladimir Oltean 		if (extack._msg)
159931046a5fSVladimir Oltean 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
160031046a5fSVladimir Oltean 				   extack._msg);
16017e1741b4SVivien Didelot 		return ret;
160231046a5fSVladimir Oltean 	}
16037e1741b4SVivien Didelot 
1604134ef238SVladimir Oltean 	return 0;
1605061f6a50SFlorian Fainelli }
1606061f6a50SFlorian Fainelli 
1607061f6a50SFlorian Fainelli static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1608061f6a50SFlorian Fainelli 				      u16 vid)
1609061f6a50SFlorian Fainelli {
1610061f6a50SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(dev);
161188236591SVladimir Oltean 	struct switchdev_obj_port_vlan vlan = {
1612b7a9e0daSVladimir Oltean 		.vid = vid,
161388236591SVladimir Oltean 		/* This API only allows programming tagged, non-PVID VIDs */
161488236591SVladimir Oltean 		.flags = 0,
161588236591SVladimir Oltean 	};
16162209158cSVladimir Oltean 	int err;
1617061f6a50SFlorian Fainelli 
16182209158cSVladimir Oltean 	err = dsa_port_vlan_del(dp, &vlan);
16192209158cSVladimir Oltean 	if (err)
16202209158cSVladimir Oltean 		return err;
16212209158cSVladimir Oltean 
1622134ef238SVladimir Oltean 	return dsa_port_host_vlan_del(dp, &vlan);
1623061f6a50SFlorian Fainelli }
1624061f6a50SFlorian Fainelli 
162506cfb2dfSVladimir Oltean static int dsa_slave_restore_vlan(struct net_device *vdev, int vid, void *arg)
162606cfb2dfSVladimir Oltean {
162706cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
162806cfb2dfSVladimir Oltean 
162906cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_add_vid(arg, proto, vid);
163006cfb2dfSVladimir Oltean }
163106cfb2dfSVladimir Oltean 
163206cfb2dfSVladimir Oltean static int dsa_slave_clear_vlan(struct net_device *vdev, int vid, void *arg)
163306cfb2dfSVladimir Oltean {
163406cfb2dfSVladimir Oltean 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
163506cfb2dfSVladimir Oltean 
163606cfb2dfSVladimir Oltean 	return dsa_slave_vlan_rx_kill_vid(arg, proto, vid);
163706cfb2dfSVladimir Oltean }
163806cfb2dfSVladimir Oltean 
163906cfb2dfSVladimir Oltean /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
164006cfb2dfSVladimir Oltean  * filtering is enabled. The baseline is that only ports that offload a
164106cfb2dfSVladimir Oltean  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
164206cfb2dfSVladimir Oltean  * but there are exceptions for quirky hardware.
164306cfb2dfSVladimir Oltean  *
164406cfb2dfSVladimir Oltean  * If ds->vlan_filtering_is_global = true, then standalone ports which share
164506cfb2dfSVladimir Oltean  * the same switch with other ports that offload a VLAN-aware bridge are also
164606cfb2dfSVladimir Oltean  * inevitably VLAN-aware.
164706cfb2dfSVladimir Oltean  *
164806cfb2dfSVladimir Oltean  * To summarize, a DSA switch port offloads:
164906cfb2dfSVladimir Oltean  *
165006cfb2dfSVladimir Oltean  * - If standalone (this includes software bridge, software LAG):
165158adf9dcSVladimir Oltean  *     - if ds->needs_standalone_vlan_filtering = true, OR if
165258adf9dcSVladimir Oltean  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
165358adf9dcSVladimir Oltean  *       this switch chip which have vlan_filtering=1)
165406cfb2dfSVladimir Oltean  *         - the 8021q upper VLANs
165558adf9dcSVladimir Oltean  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
165658adf9dcSVladimir Oltean  *       global, or it is, but no port is under a VLAN-aware bridge):
165706cfb2dfSVladimir Oltean  *         - no VLAN (any 8021q upper is a software VLAN)
165806cfb2dfSVladimir Oltean  *
165906cfb2dfSVladimir Oltean  * - If under a vlan_filtering=0 bridge which it offload:
166006cfb2dfSVladimir Oltean  *     - if ds->configure_vlan_while_not_filtering = true (default):
166106cfb2dfSVladimir Oltean  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
166206cfb2dfSVladimir Oltean  *     - else (deprecated):
166306cfb2dfSVladimir Oltean  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
166406cfb2dfSVladimir Oltean  *           enabled, so this behavior is broken and discouraged.
166506cfb2dfSVladimir Oltean  *
166606cfb2dfSVladimir Oltean  * - If under a vlan_filtering=1 bridge which it offload:
166706cfb2dfSVladimir Oltean  *     - the bridge VLANs
166806cfb2dfSVladimir Oltean  *     - the 8021q upper VLANs
166906cfb2dfSVladimir Oltean  */
167006cfb2dfSVladimir Oltean int dsa_slave_manage_vlan_filtering(struct net_device *slave,
167106cfb2dfSVladimir Oltean 				    bool vlan_filtering)
167206cfb2dfSVladimir Oltean {
167306cfb2dfSVladimir Oltean 	int err;
167406cfb2dfSVladimir Oltean 
167506cfb2dfSVladimir Oltean 	if (vlan_filtering) {
167606cfb2dfSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
167706cfb2dfSVladimir Oltean 
167806cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_restore_vlan, slave);
167906cfb2dfSVladimir Oltean 		if (err) {
168006cfb2dfSVladimir Oltean 			vlan_for_each(slave, dsa_slave_clear_vlan, slave);
168106cfb2dfSVladimir Oltean 			slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
168206cfb2dfSVladimir Oltean 			return err;
168306cfb2dfSVladimir Oltean 		}
168406cfb2dfSVladimir Oltean 	} else {
168506cfb2dfSVladimir Oltean 		err = vlan_for_each(slave, dsa_slave_clear_vlan, slave);
168606cfb2dfSVladimir Oltean 		if (err)
168706cfb2dfSVladimir Oltean 			return err;
168806cfb2dfSVladimir Oltean 
168906cfb2dfSVladimir Oltean 		slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
169006cfb2dfSVladimir Oltean 	}
169106cfb2dfSVladimir Oltean 
169206cfb2dfSVladimir Oltean 	return 0;
169306cfb2dfSVladimir Oltean }
169406cfb2dfSVladimir Oltean 
1695bff33f7eSVladimir Oltean struct dsa_hw_port {
1696bff33f7eSVladimir Oltean 	struct list_head list;
1697bff33f7eSVladimir Oltean 	struct net_device *dev;
1698bff33f7eSVladimir Oltean 	int old_mtu;
1699bff33f7eSVladimir Oltean };
1700bff33f7eSVladimir Oltean 
1701bff33f7eSVladimir Oltean static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1702bff33f7eSVladimir Oltean {
1703bff33f7eSVladimir Oltean 	const struct dsa_hw_port *p;
1704bff33f7eSVladimir Oltean 	int err;
1705bff33f7eSVladimir Oltean 
1706bff33f7eSVladimir Oltean 	list_for_each_entry(p, hw_port_list, list) {
1707bff33f7eSVladimir Oltean 		if (p->dev->mtu == mtu)
1708bff33f7eSVladimir Oltean 			continue;
1709bff33f7eSVladimir Oltean 
1710bff33f7eSVladimir Oltean 		err = dev_set_mtu(p->dev, mtu);
1711bff33f7eSVladimir Oltean 		if (err)
1712bff33f7eSVladimir Oltean 			goto rollback;
1713bff33f7eSVladimir Oltean 	}
1714bff33f7eSVladimir Oltean 
1715bff33f7eSVladimir Oltean 	return 0;
1716bff33f7eSVladimir Oltean 
1717bff33f7eSVladimir Oltean rollback:
1718bff33f7eSVladimir Oltean 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1719bff33f7eSVladimir Oltean 		if (p->dev->mtu == p->old_mtu)
1720bff33f7eSVladimir Oltean 			continue;
1721bff33f7eSVladimir Oltean 
1722bff33f7eSVladimir Oltean 		if (dev_set_mtu(p->dev, p->old_mtu))
1723bff33f7eSVladimir Oltean 			netdev_err(p->dev, "Failed to restore MTU\n");
1724bff33f7eSVladimir Oltean 	}
1725bff33f7eSVladimir Oltean 
1726bff33f7eSVladimir Oltean 	return err;
1727bff33f7eSVladimir Oltean }
1728bff33f7eSVladimir Oltean 
1729bff33f7eSVladimir Oltean static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1730bff33f7eSVladimir Oltean {
1731bff33f7eSVladimir Oltean 	struct dsa_hw_port *p, *n;
1732bff33f7eSVladimir Oltean 
1733bff33f7eSVladimir Oltean 	list_for_each_entry_safe(p, n, hw_port_list, list)
1734bff33f7eSVladimir Oltean 		kfree(p);
1735bff33f7eSVladimir Oltean }
1736bff33f7eSVladimir Oltean 
1737bff33f7eSVladimir Oltean /* Make the hardware datapath to/from @dev limited to a common MTU */
1738bf88dc32Skbuild test robot static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
1739bff33f7eSVladimir Oltean {
1740bff33f7eSVladimir Oltean 	struct list_head hw_port_list;
1741bff33f7eSVladimir Oltean 	struct dsa_switch_tree *dst;
1742bff33f7eSVladimir Oltean 	int min_mtu = ETH_MAX_MTU;
1743bff33f7eSVladimir Oltean 	struct dsa_port *other_dp;
1744bff33f7eSVladimir Oltean 	int err;
1745bff33f7eSVladimir Oltean 
1746bff33f7eSVladimir Oltean 	if (!dp->ds->mtu_enforcement_ingress)
1747bff33f7eSVladimir Oltean 		return;
1748bff33f7eSVladimir Oltean 
1749d3eed0e5SVladimir Oltean 	if (!dp->bridge)
1750bff33f7eSVladimir Oltean 		return;
1751bff33f7eSVladimir Oltean 
1752bff33f7eSVladimir Oltean 	INIT_LIST_HEAD(&hw_port_list);
1753bff33f7eSVladimir Oltean 
1754bff33f7eSVladimir Oltean 	/* Populate the list of ports that are part of the same bridge
1755bff33f7eSVladimir Oltean 	 * as the newly added/modified port
1756bff33f7eSVladimir Oltean 	 */
1757bff33f7eSVladimir Oltean 	list_for_each_entry(dst, &dsa_tree_list, list) {
1758bff33f7eSVladimir Oltean 		list_for_each_entry(other_dp, &dst->ports, list) {
1759bff33f7eSVladimir Oltean 			struct dsa_hw_port *hw_port;
1760bff33f7eSVladimir Oltean 			struct net_device *slave;
1761bff33f7eSVladimir Oltean 
1762bff33f7eSVladimir Oltean 			if (other_dp->type != DSA_PORT_TYPE_USER)
1763bff33f7eSVladimir Oltean 				continue;
1764bff33f7eSVladimir Oltean 
176536cbf39bSVladimir Oltean 			if (!dsa_port_bridge_same(dp, other_dp))
1766bff33f7eSVladimir Oltean 				continue;
1767bff33f7eSVladimir Oltean 
1768bff33f7eSVladimir Oltean 			if (!other_dp->ds->mtu_enforcement_ingress)
1769bff33f7eSVladimir Oltean 				continue;
1770bff33f7eSVladimir Oltean 
1771bff33f7eSVladimir Oltean 			slave = other_dp->slave;
1772bff33f7eSVladimir Oltean 
1773bff33f7eSVladimir Oltean 			if (min_mtu > slave->mtu)
1774bff33f7eSVladimir Oltean 				min_mtu = slave->mtu;
1775bff33f7eSVladimir Oltean 
1776bff33f7eSVladimir Oltean 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1777bff33f7eSVladimir Oltean 			if (!hw_port)
1778bff33f7eSVladimir Oltean 				goto out;
1779bff33f7eSVladimir Oltean 
1780bff33f7eSVladimir Oltean 			hw_port->dev = slave;
1781bff33f7eSVladimir Oltean 			hw_port->old_mtu = slave->mtu;
1782bff33f7eSVladimir Oltean 
1783bff33f7eSVladimir Oltean 			list_add(&hw_port->list, &hw_port_list);
1784bff33f7eSVladimir Oltean 		}
1785bff33f7eSVladimir Oltean 	}
1786bff33f7eSVladimir Oltean 
1787bff33f7eSVladimir Oltean 	/* Attempt to configure the entire hardware bridge to the newly added
1788bff33f7eSVladimir Oltean 	 * interface's MTU first, regardless of whether the intention of the
1789bff33f7eSVladimir Oltean 	 * user was to raise or lower it.
1790bff33f7eSVladimir Oltean 	 */
1791bff33f7eSVladimir Oltean 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1792bff33f7eSVladimir Oltean 	if (!err)
1793bff33f7eSVladimir Oltean 		goto out;
1794bff33f7eSVladimir Oltean 
1795bff33f7eSVladimir Oltean 	/* Clearly that didn't work out so well, so just set the minimum MTU on
1796bff33f7eSVladimir Oltean 	 * all hardware bridge ports now. If this fails too, then all ports will
1797bff33f7eSVladimir Oltean 	 * still have their old MTU rolled back anyway.
1798bff33f7eSVladimir Oltean 	 */
1799bff33f7eSVladimir Oltean 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1800bff33f7eSVladimir Oltean 
1801bff33f7eSVladimir Oltean out:
1802bff33f7eSVladimir Oltean 	dsa_hw_port_list_free(&hw_port_list);
1803bff33f7eSVladimir Oltean }
1804bff33f7eSVladimir Oltean 
180553da0ebaSVladimir Oltean int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1806bfcb8132SVladimir Oltean {
1807bfcb8132SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(dev);
1808bfcb8132SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1809bfcb8132SVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(dev);
1810bfcb8132SVladimir Oltean 	struct dsa_switch *ds = p->dp->ds;
1811*b2033a05SVladimir Oltean 	struct dsa_port *other_dp;
1812bfcb8132SVladimir Oltean 	struct dsa_port *cpu_dp;
1813bfcb8132SVladimir Oltean 	int port = p->dp->index;
1814bfcb8132SVladimir Oltean 	int largest_mtu = 0;
1815bfcb8132SVladimir Oltean 	int new_master_mtu;
1816bfcb8132SVladimir Oltean 	int old_master_mtu;
1817bfcb8132SVladimir Oltean 	int mtu_limit;
1818bfcb8132SVladimir Oltean 	int cpu_mtu;
18194e4ab795SVladimir Oltean 	int err;
1820bfcb8132SVladimir Oltean 
1821bfcb8132SVladimir Oltean 	if (!ds->ops->port_change_mtu)
1822bfcb8132SVladimir Oltean 		return -EOPNOTSUPP;
1823bfcb8132SVladimir Oltean 
1824*b2033a05SVladimir Oltean 	dsa_tree_for_each_user_port(other_dp, ds->dst) {
1825bfcb8132SVladimir Oltean 		int slave_mtu;
1826bfcb8132SVladimir Oltean 
1827bfcb8132SVladimir Oltean 		/* During probe, this function will be called for each slave
1828bfcb8132SVladimir Oltean 		 * device, while not all of them have been allocated. That's
1829bfcb8132SVladimir Oltean 		 * ok, it doesn't change what the maximum is, so ignore it.
1830bfcb8132SVladimir Oltean 		 */
1831*b2033a05SVladimir Oltean 		if (!other_dp->slave)
1832bfcb8132SVladimir Oltean 			continue;
1833bfcb8132SVladimir Oltean 
1834bfcb8132SVladimir Oltean 		/* Pretend that we already applied the setting, which we
1835bfcb8132SVladimir Oltean 		 * actually haven't (still haven't done all integrity checks)
1836bfcb8132SVladimir Oltean 		 */
1837*b2033a05SVladimir Oltean 		if (dp == other_dp)
1838bfcb8132SVladimir Oltean 			slave_mtu = new_mtu;
1839bfcb8132SVladimir Oltean 		else
1840*b2033a05SVladimir Oltean 			slave_mtu = other_dp->slave->mtu;
1841bfcb8132SVladimir Oltean 
1842bfcb8132SVladimir Oltean 		if (largest_mtu < slave_mtu)
1843bfcb8132SVladimir Oltean 			largest_mtu = slave_mtu;
1844bfcb8132SVladimir Oltean 	}
1845bfcb8132SVladimir Oltean 
1846bfcb8132SVladimir Oltean 	cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1847bfcb8132SVladimir Oltean 
1848bfcb8132SVladimir Oltean 	mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1849bfcb8132SVladimir Oltean 	old_master_mtu = master->mtu;
18504e500251SVladimir Oltean 	new_master_mtu = largest_mtu + dsa_tag_protocol_overhead(cpu_dp->tag_ops);
1851bfcb8132SVladimir Oltean 	if (new_master_mtu > mtu_limit)
1852bfcb8132SVladimir Oltean 		return -ERANGE;
1853bfcb8132SVladimir Oltean 
1854bfcb8132SVladimir Oltean 	/* If the master MTU isn't over limit, there's no need to check the CPU
1855bfcb8132SVladimir Oltean 	 * MTU, since that surely isn't either.
1856bfcb8132SVladimir Oltean 	 */
1857bfcb8132SVladimir Oltean 	cpu_mtu = largest_mtu;
1858bfcb8132SVladimir Oltean 
1859bfcb8132SVladimir Oltean 	/* Start applying stuff */
1860bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu) {
1861bfcb8132SVladimir Oltean 		err = dev_set_mtu(master, new_master_mtu);
1862bfcb8132SVladimir Oltean 		if (err < 0)
1863bfcb8132SVladimir Oltean 			goto out_master_failed;
1864bfcb8132SVladimir Oltean 
1865bfcb8132SVladimir Oltean 		/* We only need to propagate the MTU of the CPU port to
186688faba20SVladimir Oltean 		 * upstream switches, so create a non-targeted notifier which
186788faba20SVladimir Oltean 		 * updates all switches.
1868bfcb8132SVladimir Oltean 		 */
186988faba20SVladimir Oltean 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu, false);
1870bfcb8132SVladimir Oltean 		if (err)
1871bfcb8132SVladimir Oltean 			goto out_cpu_failed;
1872bfcb8132SVladimir Oltean 	}
1873bfcb8132SVladimir Oltean 
187488faba20SVladimir Oltean 	err = dsa_port_mtu_change(dp, new_mtu, true);
1875bfcb8132SVladimir Oltean 	if (err)
1876bfcb8132SVladimir Oltean 		goto out_port_failed;
1877bfcb8132SVladimir Oltean 
1878bfcb8132SVladimir Oltean 	dev->mtu = new_mtu;
1879bfcb8132SVladimir Oltean 
1880bff33f7eSVladimir Oltean 	dsa_bridge_mtu_normalization(dp);
1881bff33f7eSVladimir Oltean 
1882bfcb8132SVladimir Oltean 	return 0;
1883bfcb8132SVladimir Oltean 
1884bfcb8132SVladimir Oltean out_port_failed:
1885bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1886bfcb8132SVladimir Oltean 		dsa_port_mtu_change(cpu_dp, old_master_mtu -
18874e500251SVladimir Oltean 				    dsa_tag_protocol_overhead(cpu_dp->tag_ops),
188888faba20SVladimir Oltean 				    false);
1889bfcb8132SVladimir Oltean out_cpu_failed:
1890bfcb8132SVladimir Oltean 	if (new_master_mtu != old_master_mtu)
1891bfcb8132SVladimir Oltean 		dev_set_mtu(master, old_master_mtu);
1892bfcb8132SVladimir Oltean out_master_failed:
1893bfcb8132SVladimir Oltean 	return err;
1894bfcb8132SVladimir Oltean }
1895bfcb8132SVladimir Oltean 
1896d538eca8SVladimir Oltean static int __maybe_unused
1897d538eca8SVladimir Oltean dsa_slave_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
1898d538eca8SVladimir Oltean {
1899d538eca8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1900d538eca8SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1901d538eca8SVladimir Oltean 	unsigned long mask, new_prio;
1902d538eca8SVladimir Oltean 	int err, port = dp->index;
1903d538eca8SVladimir Oltean 
1904d538eca8SVladimir Oltean 	if (!ds->ops->port_set_default_prio)
1905d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
1906d538eca8SVladimir Oltean 
1907d538eca8SVladimir Oltean 	err = dcb_ieee_setapp(dev, app);
1908d538eca8SVladimir Oltean 	if (err)
1909d538eca8SVladimir Oltean 		return err;
1910d538eca8SVladimir Oltean 
1911d538eca8SVladimir Oltean 	mask = dcb_ieee_getapp_mask(dev, app);
1912d538eca8SVladimir Oltean 	new_prio = __fls(mask);
1913d538eca8SVladimir Oltean 
1914d538eca8SVladimir Oltean 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
1915d538eca8SVladimir Oltean 	if (err) {
1916d538eca8SVladimir Oltean 		dcb_ieee_delapp(dev, app);
1917d538eca8SVladimir Oltean 		return err;
1918d538eca8SVladimir Oltean 	}
1919d538eca8SVladimir Oltean 
1920d538eca8SVladimir Oltean 	return 0;
1921d538eca8SVladimir Oltean }
1922d538eca8SVladimir Oltean 
192347d75f78SVladimir Oltean static int __maybe_unused
192447d75f78SVladimir Oltean dsa_slave_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
192547d75f78SVladimir Oltean {
192647d75f78SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
192747d75f78SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
192847d75f78SVladimir Oltean 	unsigned long mask, new_prio;
192947d75f78SVladimir Oltean 	int err, port = dp->index;
193047d75f78SVladimir Oltean 	u8 dscp = app->protocol;
193147d75f78SVladimir Oltean 
193247d75f78SVladimir Oltean 	if (!ds->ops->port_add_dscp_prio)
193347d75f78SVladimir Oltean 		return -EOPNOTSUPP;
193447d75f78SVladimir Oltean 
193547d75f78SVladimir Oltean 	if (dscp >= 64) {
193647d75f78SVladimir Oltean 		netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
193747d75f78SVladimir Oltean 			   dscp);
193847d75f78SVladimir Oltean 		return -EINVAL;
193947d75f78SVladimir Oltean 	}
194047d75f78SVladimir Oltean 
194147d75f78SVladimir Oltean 	err = dcb_ieee_setapp(dev, app);
194247d75f78SVladimir Oltean 	if (err)
194347d75f78SVladimir Oltean 		return err;
194447d75f78SVladimir Oltean 
194547d75f78SVladimir Oltean 	mask = dcb_ieee_getapp_mask(dev, app);
194647d75f78SVladimir Oltean 	new_prio = __fls(mask);
194747d75f78SVladimir Oltean 
194847d75f78SVladimir Oltean 	err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
194947d75f78SVladimir Oltean 	if (err) {
195047d75f78SVladimir Oltean 		dcb_ieee_delapp(dev, app);
195147d75f78SVladimir Oltean 		return err;
195247d75f78SVladimir Oltean 	}
195347d75f78SVladimir Oltean 
195447d75f78SVladimir Oltean 	return 0;
195547d75f78SVladimir Oltean }
195647d75f78SVladimir Oltean 
1957d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_setapp(struct net_device *dev,
1958d538eca8SVladimir Oltean 						      struct dcb_app *app)
1959d538eca8SVladimir Oltean {
1960d538eca8SVladimir Oltean 	switch (app->selector) {
1961d538eca8SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
1962d538eca8SVladimir Oltean 		switch (app->protocol) {
1963d538eca8SVladimir Oltean 		case 0:
1964d538eca8SVladimir Oltean 			return dsa_slave_dcbnl_set_default_prio(dev, app);
1965d538eca8SVladimir Oltean 		default:
1966d538eca8SVladimir Oltean 			return -EOPNOTSUPP;
1967d538eca8SVladimir Oltean 		}
1968d538eca8SVladimir Oltean 		break;
196947d75f78SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_DSCP:
197047d75f78SVladimir Oltean 		return dsa_slave_dcbnl_add_dscp_prio(dev, app);
1971d538eca8SVladimir Oltean 	default:
1972d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
1973d538eca8SVladimir Oltean 	}
1974d538eca8SVladimir Oltean }
1975d538eca8SVladimir Oltean 
1976d538eca8SVladimir Oltean static int __maybe_unused
1977d538eca8SVladimir Oltean dsa_slave_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
1978d538eca8SVladimir Oltean {
1979d538eca8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
1980d538eca8SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
1981d538eca8SVladimir Oltean 	unsigned long mask, new_prio;
1982d538eca8SVladimir Oltean 	int err, port = dp->index;
1983d538eca8SVladimir Oltean 
1984d538eca8SVladimir Oltean 	if (!ds->ops->port_set_default_prio)
1985d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
1986d538eca8SVladimir Oltean 
1987d538eca8SVladimir Oltean 	err = dcb_ieee_delapp(dev, app);
1988d538eca8SVladimir Oltean 	if (err)
1989d538eca8SVladimir Oltean 		return err;
1990d538eca8SVladimir Oltean 
1991d538eca8SVladimir Oltean 	mask = dcb_ieee_getapp_mask(dev, app);
1992d538eca8SVladimir Oltean 	new_prio = mask ? __fls(mask) : 0;
1993d538eca8SVladimir Oltean 
1994d538eca8SVladimir Oltean 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
1995d538eca8SVladimir Oltean 	if (err) {
1996d538eca8SVladimir Oltean 		dcb_ieee_setapp(dev, app);
1997d538eca8SVladimir Oltean 		return err;
1998d538eca8SVladimir Oltean 	}
1999d538eca8SVladimir Oltean 
2000d538eca8SVladimir Oltean 	return 0;
2001d538eca8SVladimir Oltean }
2002d538eca8SVladimir Oltean 
200347d75f78SVladimir Oltean static int __maybe_unused
200447d75f78SVladimir Oltean dsa_slave_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
200547d75f78SVladimir Oltean {
200647d75f78SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
200747d75f78SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
200847d75f78SVladimir Oltean 	int err, port = dp->index;
200947d75f78SVladimir Oltean 	u8 dscp = app->protocol;
201047d75f78SVladimir Oltean 
201147d75f78SVladimir Oltean 	if (!ds->ops->port_del_dscp_prio)
201247d75f78SVladimir Oltean 		return -EOPNOTSUPP;
201347d75f78SVladimir Oltean 
201447d75f78SVladimir Oltean 	err = dcb_ieee_delapp(dev, app);
201547d75f78SVladimir Oltean 	if (err)
201647d75f78SVladimir Oltean 		return err;
201747d75f78SVladimir Oltean 
201847d75f78SVladimir Oltean 	err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
201947d75f78SVladimir Oltean 	if (err) {
202047d75f78SVladimir Oltean 		dcb_ieee_setapp(dev, app);
202147d75f78SVladimir Oltean 		return err;
202247d75f78SVladimir Oltean 	}
202347d75f78SVladimir Oltean 
202447d75f78SVladimir Oltean 	return 0;
202547d75f78SVladimir Oltean }
202647d75f78SVladimir Oltean 
2027d538eca8SVladimir Oltean static int __maybe_unused dsa_slave_dcbnl_ieee_delapp(struct net_device *dev,
2028d538eca8SVladimir Oltean 						      struct dcb_app *app)
2029d538eca8SVladimir Oltean {
2030d538eca8SVladimir Oltean 	switch (app->selector) {
2031d538eca8SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2032d538eca8SVladimir Oltean 		switch (app->protocol) {
2033d538eca8SVladimir Oltean 		case 0:
2034d538eca8SVladimir Oltean 			return dsa_slave_dcbnl_del_default_prio(dev, app);
2035d538eca8SVladimir Oltean 		default:
2036d538eca8SVladimir Oltean 			return -EOPNOTSUPP;
2037d538eca8SVladimir Oltean 		}
2038d538eca8SVladimir Oltean 		break;
203947d75f78SVladimir Oltean 	case IEEE_8021QAZ_APP_SEL_DSCP:
204047d75f78SVladimir Oltean 		return dsa_slave_dcbnl_del_dscp_prio(dev, app);
2041d538eca8SVladimir Oltean 	default:
2042d538eca8SVladimir Oltean 		return -EOPNOTSUPP;
2043d538eca8SVladimir Oltean 	}
2044d538eca8SVladimir Oltean }
2045d538eca8SVladimir Oltean 
2046d538eca8SVladimir Oltean /* Pre-populate the DCB application priority table with the priorities
2047d538eca8SVladimir Oltean  * configured during switch setup, which we read from hardware here.
2048d538eca8SVladimir Oltean  */
2049d538eca8SVladimir Oltean static int dsa_slave_dcbnl_init(struct net_device *dev)
2050d538eca8SVladimir Oltean {
2051d538eca8SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2052d538eca8SVladimir Oltean 	struct dsa_switch *ds = dp->ds;
2053d538eca8SVladimir Oltean 	int port = dp->index;
2054d538eca8SVladimir Oltean 	int err;
2055d538eca8SVladimir Oltean 
2056d538eca8SVladimir Oltean 	if (ds->ops->port_get_default_prio) {
2057d538eca8SVladimir Oltean 		int prio = ds->ops->port_get_default_prio(ds, port);
2058d538eca8SVladimir Oltean 		struct dcb_app app = {
2059d538eca8SVladimir Oltean 			.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2060d538eca8SVladimir Oltean 			.protocol = 0,
2061d538eca8SVladimir Oltean 			.priority = prio,
2062d538eca8SVladimir Oltean 		};
2063d538eca8SVladimir Oltean 
2064d538eca8SVladimir Oltean 		if (prio < 0)
2065d538eca8SVladimir Oltean 			return prio;
2066d538eca8SVladimir Oltean 
2067d538eca8SVladimir Oltean 		err = dcb_ieee_setapp(dev, &app);
2068d538eca8SVladimir Oltean 		if (err)
2069d538eca8SVladimir Oltean 			return err;
2070d538eca8SVladimir Oltean 	}
2071d538eca8SVladimir Oltean 
207247d75f78SVladimir Oltean 	if (ds->ops->port_get_dscp_prio) {
207347d75f78SVladimir Oltean 		int protocol;
207447d75f78SVladimir Oltean 
207547d75f78SVladimir Oltean 		for (protocol = 0; protocol < 64; protocol++) {
207647d75f78SVladimir Oltean 			struct dcb_app app = {
207747d75f78SVladimir Oltean 				.selector = IEEE_8021QAZ_APP_SEL_DSCP,
207847d75f78SVladimir Oltean 				.protocol = protocol,
207947d75f78SVladimir Oltean 			};
208047d75f78SVladimir Oltean 			int prio;
208147d75f78SVladimir Oltean 
208247d75f78SVladimir Oltean 			prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
208347d75f78SVladimir Oltean 			if (prio == -EOPNOTSUPP)
208447d75f78SVladimir Oltean 				continue;
208547d75f78SVladimir Oltean 			if (prio < 0)
208647d75f78SVladimir Oltean 				return prio;
208747d75f78SVladimir Oltean 
208847d75f78SVladimir Oltean 			app.priority = prio;
208947d75f78SVladimir Oltean 
209047d75f78SVladimir Oltean 			err = dcb_ieee_setapp(dev, &app);
209147d75f78SVladimir Oltean 			if (err)
209247d75f78SVladimir Oltean 				return err;
209347d75f78SVladimir Oltean 		}
209447d75f78SVladimir Oltean 	}
209547d75f78SVladimir Oltean 
2096d538eca8SVladimir Oltean 	return 0;
2097d538eca8SVladimir Oltean }
2098d538eca8SVladimir Oltean 
209991da11f8SLennert Buytenhek static const struct ethtool_ops dsa_slave_ethtool_ops = {
210091da11f8SLennert Buytenhek 	.get_drvinfo		= dsa_slave_get_drvinfo,
21013d762a0fSGuenter Roeck 	.get_regs_len		= dsa_slave_get_regs_len,
21023d762a0fSGuenter Roeck 	.get_regs		= dsa_slave_get_regs,
2103aab9c406SFlorian Fainelli 	.nway_reset		= dsa_slave_nway_reset,
2104c4aef9fcSFlorian Fainelli 	.get_link		= ethtool_op_get_link,
21056793abb4SGuenter Roeck 	.get_eeprom_len		= dsa_slave_get_eeprom_len,
21066793abb4SGuenter Roeck 	.get_eeprom		= dsa_slave_get_eeprom,
21076793abb4SGuenter Roeck 	.set_eeprom		= dsa_slave_set_eeprom,
210891da11f8SLennert Buytenhek 	.get_strings		= dsa_slave_get_strings,
210991da11f8SLennert Buytenhek 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
211091da11f8SLennert Buytenhek 	.get_sset_count		= dsa_slave_get_sset_count,
2111487d3855SAlvin Šipraga 	.get_eth_phy_stats	= dsa_slave_get_eth_phy_stats,
2112487d3855SAlvin Šipraga 	.get_eth_mac_stats	= dsa_slave_get_eth_mac_stats,
2113487d3855SAlvin Šipraga 	.get_eth_ctrl_stats	= dsa_slave_get_eth_ctrl_stats,
211419e57c4eSFlorian Fainelli 	.set_wol		= dsa_slave_set_wol,
211519e57c4eSFlorian Fainelli 	.get_wol		= dsa_slave_get_wol,
21167905288fSFlorian Fainelli 	.set_eee		= dsa_slave_set_eee,
21177905288fSFlorian Fainelli 	.get_eee		= dsa_slave_get_eee,
2118aab9c406SFlorian Fainelli 	.get_link_ksettings	= dsa_slave_get_link_ksettings,
2119aab9c406SFlorian Fainelli 	.set_link_ksettings	= dsa_slave_set_link_ksettings,
2120a2a1a13bSHeiner Kallweit 	.get_pauseparam		= dsa_slave_get_pauseparam,
2121a2a1a13bSHeiner Kallweit 	.set_pauseparam		= dsa_slave_set_pauseparam,
2122bf9f2648SFlorian Fainelli 	.get_rxnfc		= dsa_slave_get_rxnfc,
2123bf9f2648SFlorian Fainelli 	.set_rxnfc		= dsa_slave_set_rxnfc,
21240336369dSBrandon Streiff 	.get_ts_info		= dsa_slave_get_ts_info,
2125a71acad9SOleksij Rempel 	.self_test		= dsa_slave_net_selftest,
212691da11f8SLennert Buytenhek };
212791da11f8SLennert Buytenhek 
2128d538eca8SVladimir Oltean static const struct dcbnl_rtnl_ops __maybe_unused dsa_slave_dcbnl_ops = {
2129d538eca8SVladimir Oltean 	.ieee_setapp		= dsa_slave_dcbnl_ieee_setapp,
2130d538eca8SVladimir Oltean 	.ieee_delapp		= dsa_slave_dcbnl_ieee_delapp,
2131d538eca8SVladimir Oltean };
2132d538eca8SVladimir Oltean 
2133716efee2SJiri Pirko static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
2134716efee2SJiri Pirko {
2135716efee2SJiri Pirko 	struct dsa_port *dp = dsa_slave_to_port(dev);
2136716efee2SJiri Pirko 
213745b987d5SVladimir Oltean 	return &dp->devlink_port;
2138716efee2SJiri Pirko }
2139716efee2SJiri Pirko 
2140c2ec5f2eSOleksij Rempel static void dsa_slave_get_stats64(struct net_device *dev,
2141c2ec5f2eSOleksij Rempel 				  struct rtnl_link_stats64 *s)
2142c2ec5f2eSOleksij Rempel {
2143c2ec5f2eSOleksij Rempel 	struct dsa_port *dp = dsa_slave_to_port(dev);
2144c2ec5f2eSOleksij Rempel 	struct dsa_switch *ds = dp->ds;
2145c2ec5f2eSOleksij Rempel 
2146c2ec5f2eSOleksij Rempel 	if (ds->ops->get_stats64)
2147c2ec5f2eSOleksij Rempel 		ds->ops->get_stats64(ds, dp->index, s);
2148c2ec5f2eSOleksij Rempel 	else
2149c2ec5f2eSOleksij Rempel 		dev_get_tstats64(dev, s);
2150c2ec5f2eSOleksij Rempel }
2151c2ec5f2eSOleksij Rempel 
21520994d492SFelix Fietkau static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
21530994d492SFelix Fietkau 				       struct net_device_path *path)
21540994d492SFelix Fietkau {
21550994d492SFelix Fietkau 	struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
21560994d492SFelix Fietkau 	struct dsa_port *cpu_dp = dp->cpu_dp;
21570994d492SFelix Fietkau 
21580994d492SFelix Fietkau 	path->dev = ctx->dev;
21590994d492SFelix Fietkau 	path->type = DEV_PATH_DSA;
21600994d492SFelix Fietkau 	path->dsa.proto = cpu_dp->tag_ops->proto;
21610994d492SFelix Fietkau 	path->dsa.port = dp->index;
21620994d492SFelix Fietkau 	ctx->dev = cpu_dp->master;
21630994d492SFelix Fietkau 
21640994d492SFelix Fietkau 	return 0;
21650994d492SFelix Fietkau }
21660994d492SFelix Fietkau 
21673e8a72d1SFlorian Fainelli static const struct net_device_ops dsa_slave_netdev_ops = {
2168d442ad4aSStephen Hemminger 	.ndo_open	 	= dsa_slave_open,
2169d442ad4aSStephen Hemminger 	.ndo_stop		= dsa_slave_close,
21703e8a72d1SFlorian Fainelli 	.ndo_start_xmit		= dsa_slave_xmit,
2171d442ad4aSStephen Hemminger 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
2172d442ad4aSStephen Hemminger 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
2173d442ad4aSStephen Hemminger 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
21742bedde1aSArkadi Sharshevsky 	.ndo_fdb_dump		= dsa_slave_fdb_dump,
2175a7605370SArnd Bergmann 	.ndo_eth_ioctl		= dsa_slave_ioctl,
2176abd2be00SNicolas Dichtel 	.ndo_get_iflink		= dsa_slave_get_iflink,
217704ff53f9SFlorian Fainelli #ifdef CONFIG_NET_POLL_CONTROLLER
217804ff53f9SFlorian Fainelli 	.ndo_netpoll_setup	= dsa_slave_netpoll_setup,
217904ff53f9SFlorian Fainelli 	.ndo_netpoll_cleanup	= dsa_slave_netpoll_cleanup,
218004ff53f9SFlorian Fainelli 	.ndo_poll_controller	= dsa_slave_poll_controller,
218104ff53f9SFlorian Fainelli #endif
2182f50f2127SFlorian Fainelli 	.ndo_setup_tc		= dsa_slave_setup_tc,
2183c2ec5f2eSOleksij Rempel 	.ndo_get_stats64	= dsa_slave_get_stats64,
2184061f6a50SFlorian Fainelli 	.ndo_vlan_rx_add_vid	= dsa_slave_vlan_rx_add_vid,
2185061f6a50SFlorian Fainelli 	.ndo_vlan_rx_kill_vid	= dsa_slave_vlan_rx_kill_vid,
2186716efee2SJiri Pirko 	.ndo_get_devlink_port	= dsa_slave_get_devlink_port,
2187bfcb8132SVladimir Oltean 	.ndo_change_mtu		= dsa_slave_change_mtu,
21880994d492SFelix Fietkau 	.ndo_fill_forward_path	= dsa_slave_fill_forward_path,
218998237d43SScott Feldman };
219098237d43SScott Feldman 
2191f37db85dSFlorian Fainelli static struct device_type dsa_type = {
2192f37db85dSFlorian Fainelli 	.name	= "dsa",
2193f37db85dSFlorian Fainelli };
2194f37db85dSFlorian Fainelli 
2195aab9c406SFlorian Fainelli void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2196aab9c406SFlorian Fainelli {
2197aab9c406SFlorian Fainelli 	const struct dsa_port *dp = dsa_to_port(ds, port);
2198aab9c406SFlorian Fainelli 
2199765bda93SRussell King 	if (dp->pl)
2200aab9c406SFlorian Fainelli 		phylink_mac_change(dp->pl, up);
2201aab9c406SFlorian Fainelli }
2202aab9c406SFlorian Fainelli EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2203aab9c406SFlorian Fainelli 
22045c05c1dbSRussell King static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
2205aab9c406SFlorian Fainelli 					  struct phylink_link_state *state)
2206aab9c406SFlorian Fainelli {
22075c05c1dbSRussell King 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
2208aab9c406SFlorian Fainelli 	struct dsa_switch *ds = dp->ds;
2209aab9c406SFlorian Fainelli 
2210aab9c406SFlorian Fainelli 	/* No need to check that this operation is valid, the callback would
2211aab9c406SFlorian Fainelli 	 * not be called if it was not.
2212aab9c406SFlorian Fainelli 	 */
2213aab9c406SFlorian Fainelli 	ds->ops->phylink_fixed_state(ds, dp->index, state);
2214ce31b31cSFlorian Fainelli }
2215ce31b31cSFlorian Fainelli 
221691da11f8SLennert Buytenhek /* slave device setup *******************************************************/
2217c916e8e1SOleksij Rempel static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr,
2218c916e8e1SOleksij Rempel 				 u32 flags)
2219c305c165SFlorian Fainelli {
2220d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2221d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
2222c305c165SFlorian Fainelli 
22230115dcd1SVivien Didelot 	slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
22240115dcd1SVivien Didelot 	if (!slave_dev->phydev) {
2225d25b8e74SRussell King 		netdev_err(slave_dev, "no phy at %d\n", addr);
2226c305c165SFlorian Fainelli 		return -ENODEV;
2227d25b8e74SRussell King 	}
2228c305c165SFlorian Fainelli 
2229c916e8e1SOleksij Rempel 	slave_dev->phydev->dev_flags |= flags;
2230c916e8e1SOleksij Rempel 
2231aab9c406SFlorian Fainelli 	return phylink_connect_phy(dp->pl, slave_dev->phydev);
2232c305c165SFlorian Fainelli }
2233c305c165SFlorian Fainelli 
22344fa7b718SVivien Didelot static int dsa_slave_phy_setup(struct net_device *slave_dev)
22350d8bcdd3SFlorian Fainelli {
2236d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2237d945097bSVivien Didelot 	struct device_node *port_dn = dp->dn;
2238d945097bSVivien Didelot 	struct dsa_switch *ds = dp->ds;
22396819563eSFlorian Fainelli 	u32 phy_flags = 0;
22400c65b2b9SAndrew Lunn 	int ret;
22410d8bcdd3SFlorian Fainelli 
224244cc27e4SIoana Ciornei 	dp->pl_config.dev = &slave_dev->dev;
224344cc27e4SIoana Ciornei 	dp->pl_config.type = PHYLINK_NETDEV;
224444cc27e4SIoana Ciornei 
22455c05c1dbSRussell King 	/* The get_fixed_state callback takes precedence over polling the
22465c05c1dbSRussell King 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
22475c05c1dbSRussell King 	 * this if the switch provides such a callback.
22485c05c1dbSRussell King 	 */
22495c05c1dbSRussell King 	if (ds->ops->phylink_fixed_state) {
22505c05c1dbSRussell King 		dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
22515c05c1dbSRussell King 		dp->pl_config.poll_fixed_state = true;
22525c05c1dbSRussell King 	}
22535c05c1dbSRussell King 
225421bd64bdSRussell King (Oracle) 	ret = dsa_port_phylink_create(dp);
225521bd64bdSRussell King (Oracle) 	if (ret)
225621bd64bdSRussell King (Oracle) 		return ret;
2257aab9c406SFlorian Fainelli 
22589d490b4eSVivien Didelot 	if (ds->ops->get_phy_flags)
2259d945097bSVivien Didelot 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
22606819563eSFlorian Fainelli 
2261aab9c406SFlorian Fainelli 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
22626146dd45SVladimir Oltean 	if (ret == -ENODEV && ds->slave_mii_bus) {
22636146dd45SVladimir Oltean 		/* We could not connect to a designated PHY or SFP, so try to
22646146dd45SVladimir Oltean 		 * use the switch internal MDIO bus instead
22650d8bcdd3SFlorian Fainelli 		 */
2266c916e8e1SOleksij Rempel 		ret = dsa_slave_phy_connect(slave_dev, dp->index, phy_flags);
2267d25b8e74SRussell King 	}
22686a52e733SVladimir Oltean 	if (ret) {
22696a52e733SVladimir Oltean 		netdev_err(slave_dev, "failed to connect to PHY: %pe\n",
22706a52e733SVladimir Oltean 			   ERR_PTR(ret));
22716a52e733SVladimir Oltean 		phylink_destroy(dp->pl);
22720d8bcdd3SFlorian Fainelli 	}
22739697f1cdSFlorian Fainelli 
22746146dd45SVladimir Oltean 	return ret;
2275b31f65fbSAndrew Lunn }
22760d8bcdd3SFlorian Fainelli 
227753da0ebaSVladimir Oltean void dsa_slave_setup_tagger(struct net_device *slave)
227853da0ebaSVladimir Oltean {
227953da0ebaSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(slave);
228053da0ebaSVladimir Oltean 	struct dsa_slave_priv *p = netdev_priv(slave);
228153da0ebaSVladimir Oltean 	const struct dsa_port *cpu_dp = dp->cpu_dp;
228253da0ebaSVladimir Oltean 	struct net_device *master = cpu_dp->master;
228358adf9dcSVladimir Oltean 	const struct dsa_switch *ds = dp->ds;
228453da0ebaSVladimir Oltean 
22854e500251SVladimir Oltean 	slave->needed_headroom = cpu_dp->tag_ops->needed_headroom;
22864e500251SVladimir Oltean 	slave->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
228753da0ebaSVladimir Oltean 	/* Try to save one extra realloc later in the TX path (in the master)
228853da0ebaSVladimir Oltean 	 * by also inheriting the master's needed headroom and tailroom.
228953da0ebaSVladimir Oltean 	 * The 8021q driver also does this.
229053da0ebaSVladimir Oltean 	 */
229153da0ebaSVladimir Oltean 	slave->needed_headroom += master->needed_headroom;
229253da0ebaSVladimir Oltean 	slave->needed_tailroom += master->needed_tailroom;
229353da0ebaSVladimir Oltean 
229453da0ebaSVladimir Oltean 	p->xmit = cpu_dp->tag_ops->xmit;
229521cf377aSLino Sanfilippo 
229621cf377aSLino Sanfilippo 	slave->features = master->vlan_features | NETIF_F_HW_TC;
229721cf377aSLino Sanfilippo 	slave->hw_features |= NETIF_F_HW_TC;
229821cf377aSLino Sanfilippo 	slave->features |= NETIF_F_LLTX;
229921cf377aSLino Sanfilippo 	if (slave->needed_tailroom)
230021cf377aSLino Sanfilippo 		slave->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
230158adf9dcSVladimir Oltean 	if (ds->needs_standalone_vlan_filtering)
230258adf9dcSVladimir Oltean 		slave->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
230353da0ebaSVladimir Oltean }
230453da0ebaSVladimir Oltean 
230524462549SFlorian Fainelli int dsa_slave_suspend(struct net_device *slave_dev)
230624462549SFlorian Fainelli {
2307aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
230824462549SFlorian Fainelli 
2309a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
2310a94c689eSFlorian Fainelli 		return 0;
2311a94c689eSFlorian Fainelli 
2312f154be24SFlorian Fainelli 	netif_device_detach(slave_dev);
2313f154be24SFlorian Fainelli 
2314aab9c406SFlorian Fainelli 	rtnl_lock();
2315aab9c406SFlorian Fainelli 	phylink_stop(dp->pl);
2316aab9c406SFlorian Fainelli 	rtnl_unlock();
231724462549SFlorian Fainelli 
231824462549SFlorian Fainelli 	return 0;
231924462549SFlorian Fainelli }
232024462549SFlorian Fainelli 
232124462549SFlorian Fainelli int dsa_slave_resume(struct net_device *slave_dev)
232224462549SFlorian Fainelli {
2323aab9c406SFlorian Fainelli 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2324aab9c406SFlorian Fainelli 
2325a94c689eSFlorian Fainelli 	if (!netif_running(slave_dev))
2326a94c689eSFlorian Fainelli 		return 0;
2327a94c689eSFlorian Fainelli 
232824462549SFlorian Fainelli 	netif_device_attach(slave_dev);
232924462549SFlorian Fainelli 
2330aab9c406SFlorian Fainelli 	rtnl_lock();
2331aab9c406SFlorian Fainelli 	phylink_start(dp->pl);
2332aab9c406SFlorian Fainelli 	rtnl_unlock();
233324462549SFlorian Fainelli 
233424462549SFlorian Fainelli 	return 0;
233524462549SFlorian Fainelli }
233624462549SFlorian Fainelli 
2337951259aaSVivien Didelot int dsa_slave_create(struct dsa_port *port)
233891da11f8SLennert Buytenhek {
233924a9332aSVivien Didelot 	const struct dsa_port *cpu_dp = port->cpu_dp;
2340f8b8b1cdSVivien Didelot 	struct net_device *master = cpu_dp->master;
23414cfbf09cSVivien Didelot 	struct dsa_switch *ds = port->ds;
2342951259aaSVivien Didelot 	const char *name = port->name;
234391da11f8SLennert Buytenhek 	struct net_device *slave_dev;
234491da11f8SLennert Buytenhek 	struct dsa_slave_priv *p;
234591da11f8SLennert Buytenhek 	int ret;
234691da11f8SLennert Buytenhek 
234755199df6SFlorian Fainelli 	if (!ds->num_tx_queues)
234855199df6SFlorian Fainelli 		ds->num_tx_queues = 1;
234955199df6SFlorian Fainelli 
235055199df6SFlorian Fainelli 	slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
235155199df6SFlorian Fainelli 				     NET_NAME_UNKNOWN, ether_setup,
235255199df6SFlorian Fainelli 				     ds->num_tx_queues, 1);
235391da11f8SLennert Buytenhek 	if (slave_dev == NULL)
2354d87d6f44SGuenter Roeck 		return -ENOMEM;
235591da11f8SLennert Buytenhek 
23567ad24ea4SWilfried Klaebe 	slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
2357d538eca8SVladimir Oltean #if IS_ENABLED(CONFIG_DCB)
2358d538eca8SVladimir Oltean 	slave_dev->dcbnl_ops = &dsa_slave_dcbnl_ops;
2359d538eca8SVladimir Oltean #endif
236083216e39SMichael Walle 	if (!is_zero_ether_addr(port->mac))
2361e35b8d7dSJakub Kicinski 		eth_hw_addr_set(slave_dev, port->mac);
2362a2c7023fSXiaofei Shen 	else
23632fcc8005SBjørn Mork 		eth_hw_addr_inherit(slave_dev, master);
23640a5f107bSPhil Sutter 	slave_dev->priv_flags |= IFF_NO_QUEUE;
23655e8a1e03SVladimir Oltean 	if (dsa_switch_supports_uc_filtering(ds))
23665e8a1e03SVladimir Oltean 		slave_dev->priv_flags |= IFF_UNICAST_FLT;
23673e8a72d1SFlorian Fainelli 	slave_dev->netdev_ops = &dsa_slave_netdev_ops;
2368bfcb8132SVladimir Oltean 	if (ds->ops->port_max_mtu)
2369bfcb8132SVladimir Oltean 		slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2370f37db85dSFlorian Fainelli 	SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
2371d442ad4aSStephen Hemminger 
23724cfbf09cSVivien Didelot 	SET_NETDEV_DEV(slave_dev, port->ds->dev);
23734cfbf09cSVivien Didelot 	slave_dev->dev.of_node = port->dn;
237491da11f8SLennert Buytenhek 	slave_dev->vlan_features = master->vlan_features;
237591da11f8SLennert Buytenhek 
237691da11f8SLennert Buytenhek 	p = netdev_priv(slave_dev);
23776a900628SHeiner Kallweit 	slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
23786a900628SHeiner Kallweit 	if (!slave_dev->tstats) {
23795f6b4e14SFlorian Fainelli 		free_netdev(slave_dev);
23805f6b4e14SFlorian Fainelli 		return -ENOMEM;
23815f6b4e14SFlorian Fainelli 	}
2382e131a563SAlexander Lobakin 
2383e131a563SAlexander Lobakin 	ret = gro_cells_init(&p->gcells, slave_dev);
2384e131a563SAlexander Lobakin 	if (ret)
2385e131a563SAlexander Lobakin 		goto out_free;
2386e131a563SAlexander Lobakin 
23874cfbf09cSVivien Didelot 	p->dp = port;
2388f50f2127SFlorian Fainelli 	INIT_LIST_HEAD(&p->mall_tc_list);
2389f8b8b1cdSVivien Didelot 	port->slave = slave_dev;
239053da0ebaSVladimir Oltean 	dsa_slave_setup_tagger(slave_dev);
239191da11f8SLennert Buytenhek 
239291da11f8SLennert Buytenhek 	netif_carrier_off(slave_dev);
239391da11f8SLennert Buytenhek 
23944fa7b718SVivien Didelot 	ret = dsa_slave_phy_setup(slave_dev);
23950071f56eSAndrew Lunn 	if (ret) {
2396c9ebf126SVladimir Oltean 		netdev_err(slave_dev,
2397c9ebf126SVladimir Oltean 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2398c9ebf126SVladimir Oltean 			   ret, ds->dst->index, ds->index, port->index);
2399e131a563SAlexander Lobakin 		goto out_gcells;
2400e804441cSFlorian Fainelli 	}
2401e804441cSFlorian Fainelli 
24022f1e8ea7SVladimir Oltean 	rtnl_lock();
2403e31dbd3bSVladimir Oltean 
2404904e112aSVladimir Oltean 	ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
2405904e112aSVladimir Oltean 	if (ret && ret != -EOPNOTSUPP)
2406904e112aSVladimir Oltean 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2407904e112aSVladimir Oltean 			 ret, ETH_DATA_LEN, port->index);
2408904e112aSVladimir Oltean 
24092f1e8ea7SVladimir Oltean 	ret = register_netdevice(slave_dev);
2410e804441cSFlorian Fainelli 	if (ret) {
2411e804441cSFlorian Fainelli 		netdev_err(master, "error %d registering interface %s\n",
2412e804441cSFlorian Fainelli 			   ret, slave_dev->name);
24132f1e8ea7SVladimir Oltean 		rtnl_unlock();
2414e804441cSFlorian Fainelli 		goto out_phy;
24150071f56eSAndrew Lunn 	}
24160071f56eSAndrew Lunn 
2417d538eca8SVladimir Oltean 	if (IS_ENABLED(CONFIG_DCB)) {
2418d538eca8SVladimir Oltean 		ret = dsa_slave_dcbnl_init(slave_dev);
2419d538eca8SVladimir Oltean 		if (ret) {
2420d538eca8SVladimir Oltean 			netdev_err(slave_dev,
2421d538eca8SVladimir Oltean 				   "failed to initialize DCB: %pe\n",
2422d538eca8SVladimir Oltean 				   ERR_PTR(ret));
2423d538eca8SVladimir Oltean 			rtnl_unlock();
2424d538eca8SVladimir Oltean 			goto out_unregister;
2425d538eca8SVladimir Oltean 		}
2426d538eca8SVladimir Oltean 	}
2427d538eca8SVladimir Oltean 
24282f1e8ea7SVladimir Oltean 	ret = netdev_upper_dev_link(master, slave_dev, NULL);
24292f1e8ea7SVladimir Oltean 
24302f1e8ea7SVladimir Oltean 	rtnl_unlock();
24312f1e8ea7SVladimir Oltean 
24322f1e8ea7SVladimir Oltean 	if (ret)
24332f1e8ea7SVladimir Oltean 		goto out_unregister;
24342f1e8ea7SVladimir Oltean 
2435d87d6f44SGuenter Roeck 	return 0;
2436e804441cSFlorian Fainelli 
24372f1e8ea7SVladimir Oltean out_unregister:
24382f1e8ea7SVladimir Oltean 	unregister_netdev(slave_dev);
2439e804441cSFlorian Fainelli out_phy:
2440aab9c406SFlorian Fainelli 	rtnl_lock();
2441aab9c406SFlorian Fainelli 	phylink_disconnect_phy(p->dp->pl);
2442aab9c406SFlorian Fainelli 	rtnl_unlock();
2443aab9c406SFlorian Fainelli 	phylink_destroy(p->dp->pl);
2444e131a563SAlexander Lobakin out_gcells:
2445e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
2446e804441cSFlorian Fainelli out_free:
24476a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2448e804441cSFlorian Fainelli 	free_netdev(slave_dev);
2449f8b8b1cdSVivien Didelot 	port->slave = NULL;
2450e804441cSFlorian Fainelli 	return ret;
245191da11f8SLennert Buytenhek }
2452b73adef6SFlorian Fainelli 
2453cda5c15bSNeil Armstrong void dsa_slave_destroy(struct net_device *slave_dev)
2454cda5c15bSNeil Armstrong {
24552f1e8ea7SVladimir Oltean 	struct net_device *master = dsa_slave_to_master(slave_dev);
2456d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(slave_dev);
2457cda5c15bSNeil Armstrong 	struct dsa_slave_priv *p = netdev_priv(slave_dev);
2458cda5c15bSNeil Armstrong 
2459cda5c15bSNeil Armstrong 	netif_carrier_off(slave_dev);
2460aab9c406SFlorian Fainelli 	rtnl_lock();
24612f1e8ea7SVladimir Oltean 	netdev_upper_dev_unlink(master, slave_dev);
24622f1e8ea7SVladimir Oltean 	unregister_netdevice(slave_dev);
2463aab9c406SFlorian Fainelli 	phylink_disconnect_phy(dp->pl);
2464aab9c406SFlorian Fainelli 	rtnl_unlock();
2465881eadabSJohan Hovold 
2466aab9c406SFlorian Fainelli 	phylink_destroy(dp->pl);
2467e131a563SAlexander Lobakin 	gro_cells_destroy(&p->gcells);
24686a900628SHeiner Kallweit 	free_percpu(slave_dev->tstats);
2469cda5c15bSNeil Armstrong 	free_netdev(slave_dev);
2470cda5c15bSNeil Armstrong }
2471cda5c15bSNeil Armstrong 
24724d776482SFlorian Fainelli bool dsa_slave_dev_check(const struct net_device *dev)
2473b73adef6SFlorian Fainelli {
2474b73adef6SFlorian Fainelli 	return dev->netdev_ops == &dsa_slave_netdev_ops;
2475b73adef6SFlorian Fainelli }
2476a5e3c9baSVladimir Oltean EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
2477b73adef6SFlorian Fainelli 
24788e92ab3aSVivien Didelot static int dsa_slave_changeupper(struct net_device *dev,
24798e92ab3aSVivien Didelot 				 struct netdev_notifier_changeupper_info *info)
2480b73adef6SFlorian Fainelli {
2481d945097bSVivien Didelot 	struct dsa_port *dp = dsa_slave_to_port(dev);
24822afc526aSVladimir Oltean 	struct netlink_ext_ack *extack;
24838e92ab3aSVivien Didelot 	int err = NOTIFY_DONE;
2484b73adef6SFlorian Fainelli 
24852afc526aSVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
24862afc526aSVladimir Oltean 
24878e92ab3aSVivien Didelot 	if (netif_is_bridge_master(info->upper_dev)) {
24888e92ab3aSVivien Didelot 		if (info->linking) {
24892afc526aSVladimir Oltean 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2490bff33f7eSVladimir Oltean 			if (!err)
2491bff33f7eSVladimir Oltean 				dsa_bridge_mtu_normalization(dp);
249267b5fb5dSVladimir Oltean 			if (err == -EOPNOTSUPP) {
249367b5fb5dSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
249467b5fb5dSVladimir Oltean 						   "Offloading not supported");
249567b5fb5dSVladimir Oltean 				err = 0;
249667b5fb5dSVladimir Oltean 			}
24978e92ab3aSVivien Didelot 			err = notifier_from_errno(err);
24988e92ab3aSVivien Didelot 		} else {
249917d7802bSVivien Didelot 			dsa_port_bridge_leave(dp, info->upper_dev);
25008e92ab3aSVivien Didelot 			err = NOTIFY_OK;
25018e92ab3aSVivien Didelot 		}
2502058102a6STobias Waldekranz 	} else if (netif_is_lag_master(info->upper_dev)) {
2503058102a6STobias Waldekranz 		if (info->linking) {
2504058102a6STobias Waldekranz 			err = dsa_port_lag_join(dp, info->upper_dev,
25052afc526aSVladimir Oltean 						info->upper_info, extack);
2506058102a6STobias Waldekranz 			if (err == -EOPNOTSUPP) {
2507058102a6STobias Waldekranz 				NL_SET_ERR_MSG_MOD(info->info.extack,
2508058102a6STobias Waldekranz 						   "Offloading not supported");
2509058102a6STobias Waldekranz 				err = 0;
2510058102a6STobias Waldekranz 			}
2511058102a6STobias Waldekranz 			err = notifier_from_errno(err);
2512058102a6STobias Waldekranz 		} else {
2513058102a6STobias Waldekranz 			dsa_port_lag_leave(dp, info->upper_dev);
2514058102a6STobias Waldekranz 			err = NOTIFY_OK;
2515058102a6STobias Waldekranz 		}
251618596f50SGeorge McCollister 	} else if (is_hsr_master(info->upper_dev)) {
251718596f50SGeorge McCollister 		if (info->linking) {
251818596f50SGeorge McCollister 			err = dsa_port_hsr_join(dp, info->upper_dev);
251918596f50SGeorge McCollister 			if (err == -EOPNOTSUPP) {
252018596f50SGeorge McCollister 				NL_SET_ERR_MSG_MOD(info->info.extack,
252118596f50SGeorge McCollister 						   "Offloading not supported");
252218596f50SGeorge McCollister 				err = 0;
252318596f50SGeorge McCollister 			}
252418596f50SGeorge McCollister 			err = notifier_from_errno(err);
252518596f50SGeorge McCollister 		} else {
252618596f50SGeorge McCollister 			dsa_port_hsr_leave(dp, info->upper_dev);
252718596f50SGeorge McCollister 			err = NOTIFY_OK;
252818596f50SGeorge McCollister 		}
2529058102a6STobias Waldekranz 	}
2530058102a6STobias Waldekranz 
2531058102a6STobias Waldekranz 	return err;
2532058102a6STobias Waldekranz }
2533058102a6STobias Waldekranz 
253474918945SVladimir Oltean static int dsa_slave_prechangeupper(struct net_device *dev,
253574918945SVladimir Oltean 				    struct netdev_notifier_changeupper_info *info)
253674918945SVladimir Oltean {
253774918945SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
253874918945SVladimir Oltean 
253974918945SVladimir Oltean 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
25404e51bf44SVladimir Oltean 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
254174918945SVladimir Oltean 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
25424e51bf44SVladimir Oltean 		dsa_port_pre_lag_leave(dp, info->upper_dev);
254374918945SVladimir Oltean 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr cannot be
254474918945SVladimir Oltean 	 * meaningfully enslaved to a bridge yet
254574918945SVladimir Oltean 	 */
254674918945SVladimir Oltean 
25474e51bf44SVladimir Oltean 	return NOTIFY_DONE;
254874918945SVladimir Oltean }
254974918945SVladimir Oltean 
2550058102a6STobias Waldekranz static int
2551058102a6STobias Waldekranz dsa_slave_lag_changeupper(struct net_device *dev,
2552058102a6STobias Waldekranz 			  struct netdev_notifier_changeupper_info *info)
2553058102a6STobias Waldekranz {
2554058102a6STobias Waldekranz 	struct net_device *lower;
2555058102a6STobias Waldekranz 	struct list_head *iter;
2556058102a6STobias Waldekranz 	int err = NOTIFY_DONE;
2557058102a6STobias Waldekranz 	struct dsa_port *dp;
2558058102a6STobias Waldekranz 
2559058102a6STobias Waldekranz 	netdev_for_each_lower_dev(dev, lower, iter) {
2560058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(lower))
2561058102a6STobias Waldekranz 			continue;
2562058102a6STobias Waldekranz 
2563058102a6STobias Waldekranz 		dp = dsa_slave_to_port(lower);
2564dedd6a00SVladimir Oltean 		if (!dp->lag)
2565058102a6STobias Waldekranz 			/* Software LAG */
2566058102a6STobias Waldekranz 			continue;
2567058102a6STobias Waldekranz 
2568058102a6STobias Waldekranz 		err = dsa_slave_changeupper(lower, info);
2569058102a6STobias Waldekranz 		if (notifier_to_errno(err))
2570058102a6STobias Waldekranz 			break;
25716debb68aSVivien Didelot 	}
2572b73adef6SFlorian Fainelli 
25738e92ab3aSVivien Didelot 	return err;
2574b73adef6SFlorian Fainelli }
2575b73adef6SFlorian Fainelli 
257674918945SVladimir Oltean /* Same as dsa_slave_lag_changeupper() except that it calls
257774918945SVladimir Oltean  * dsa_slave_prechangeupper()
257874918945SVladimir Oltean  */
257974918945SVladimir Oltean static int
258074918945SVladimir Oltean dsa_slave_lag_prechangeupper(struct net_device *dev,
258174918945SVladimir Oltean 			     struct netdev_notifier_changeupper_info *info)
258274918945SVladimir Oltean {
258374918945SVladimir Oltean 	struct net_device *lower;
258474918945SVladimir Oltean 	struct list_head *iter;
258574918945SVladimir Oltean 	int err = NOTIFY_DONE;
258674918945SVladimir Oltean 	struct dsa_port *dp;
258774918945SVladimir Oltean 
258874918945SVladimir Oltean 	netdev_for_each_lower_dev(dev, lower, iter) {
258974918945SVladimir Oltean 		if (!dsa_slave_dev_check(lower))
259074918945SVladimir Oltean 			continue;
259174918945SVladimir Oltean 
259274918945SVladimir Oltean 		dp = dsa_slave_to_port(lower);
2593dedd6a00SVladimir Oltean 		if (!dp->lag)
259474918945SVladimir Oltean 			/* Software LAG */
259574918945SVladimir Oltean 			continue;
259674918945SVladimir Oltean 
259774918945SVladimir Oltean 		err = dsa_slave_prechangeupper(lower, info);
259874918945SVladimir Oltean 		if (notifier_to_errno(err))
259974918945SVladimir Oltean 			break;
260074918945SVladimir Oltean 	}
260174918945SVladimir Oltean 
260274918945SVladimir Oltean 	return err;
260374918945SVladimir Oltean }
260474918945SVladimir Oltean 
2605eb46e8daSVladimir Oltean static int
2606eb46e8daSVladimir Oltean dsa_prevent_bridging_8021q_upper(struct net_device *dev,
2607eb46e8daSVladimir Oltean 				 struct netdev_notifier_changeupper_info *info)
2608cc1d5bdaSFlorian Fainelli {
2609cc1d5bdaSFlorian Fainelli 	struct netlink_ext_ack *ext_ack;
261036cbf39bSVladimir Oltean 	struct net_device *slave, *br;
2611cc1d5bdaSFlorian Fainelli 	struct dsa_port *dp;
2612cc1d5bdaSFlorian Fainelli 
2613cc1d5bdaSFlorian Fainelli 	ext_ack = netdev_notifier_info_to_extack(&info->info);
2614cc1d5bdaSFlorian Fainelli 
2615cc1d5bdaSFlorian Fainelli 	if (!is_vlan_dev(dev))
2616cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2617cc1d5bdaSFlorian Fainelli 
2618cc1d5bdaSFlorian Fainelli 	slave = vlan_dev_real_dev(dev);
2619cc1d5bdaSFlorian Fainelli 	if (!dsa_slave_dev_check(slave))
2620cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2621cc1d5bdaSFlorian Fainelli 
2622cc1d5bdaSFlorian Fainelli 	dp = dsa_slave_to_port(slave);
262336cbf39bSVladimir Oltean 	br = dsa_port_bridge_dev_get(dp);
262436cbf39bSVladimir Oltean 	if (!br)
2625cc1d5bdaSFlorian Fainelli 		return NOTIFY_DONE;
2626cc1d5bdaSFlorian Fainelli 
2627cc1d5bdaSFlorian Fainelli 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
262836cbf39bSVladimir Oltean 	if (br_vlan_enabled(br) &&
2629cc1d5bdaSFlorian Fainelli 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
2630cc1d5bdaSFlorian Fainelli 		NL_SET_ERR_MSG_MOD(ext_ack,
2631cc1d5bdaSFlorian Fainelli 				   "Cannot enslave VLAN device into VLAN aware bridge");
2632cc1d5bdaSFlorian Fainelli 		return notifier_from_errno(-EINVAL);
2633cc1d5bdaSFlorian Fainelli 	}
2634cc1d5bdaSFlorian Fainelli 
2635cc1d5bdaSFlorian Fainelli 	return NOTIFY_DONE;
2636cc1d5bdaSFlorian Fainelli }
2637cc1d5bdaSFlorian Fainelli 
26382b138406SVladimir Oltean static int
26392b138406SVladimir Oltean dsa_slave_check_8021q_upper(struct net_device *dev,
26402b138406SVladimir Oltean 			    struct netdev_notifier_changeupper_info *info)
26412b138406SVladimir Oltean {
26422b138406SVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
264336cbf39bSVladimir Oltean 	struct net_device *br = dsa_port_bridge_dev_get(dp);
26442b138406SVladimir Oltean 	struct bridge_vlan_info br_info;
26452b138406SVladimir Oltean 	struct netlink_ext_ack *extack;
26462b138406SVladimir Oltean 	int err = NOTIFY_DONE;
26472b138406SVladimir Oltean 	u16 vid;
26482b138406SVladimir Oltean 
2649adb256ebSVladimir Oltean 	if (!br || !br_vlan_enabled(br))
26502b138406SVladimir Oltean 		return NOTIFY_DONE;
26512b138406SVladimir Oltean 
26522b138406SVladimir Oltean 	extack = netdev_notifier_info_to_extack(&info->info);
26532b138406SVladimir Oltean 	vid = vlan_dev_vlan_id(info->upper_dev);
26542b138406SVladimir Oltean 
26552b138406SVladimir Oltean 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
26562b138406SVladimir Oltean 	 * device, respectively the VID is not found, returning
26572b138406SVladimir Oltean 	 * 0 means success, which is a failure for us here.
26582b138406SVladimir Oltean 	 */
26592b138406SVladimir Oltean 	err = br_vlan_get_info(br, vid, &br_info);
26602b138406SVladimir Oltean 	if (err == 0) {
26612b138406SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
26622b138406SVladimir Oltean 				   "This VLAN is already configured by the bridge");
26632b138406SVladimir Oltean 		return notifier_from_errno(-EBUSY);
26642b138406SVladimir Oltean 	}
26652b138406SVladimir Oltean 
26662b138406SVladimir Oltean 	return NOTIFY_DONE;
26672b138406SVladimir Oltean }
26682b138406SVladimir Oltean 
26694ede74e7SVladimir Oltean static int
26704ede74e7SVladimir Oltean dsa_slave_prechangeupper_sanity_check(struct net_device *dev,
26714ede74e7SVladimir Oltean 				      struct netdev_notifier_changeupper_info *info)
2672b73adef6SFlorian Fainelli {
2673e358bef7SVladimir Oltean 	struct dsa_switch *ds;
2674e358bef7SVladimir Oltean 	struct dsa_port *dp;
2675e358bef7SVladimir Oltean 	int err;
26762b138406SVladimir Oltean 
267753bade8aSFlorian Fainelli 	if (!dsa_slave_dev_check(dev))
26784ede74e7SVladimir Oltean 		return dsa_prevent_bridging_8021q_upper(dev, info);
26792b138406SVladimir Oltean 
2680e358bef7SVladimir Oltean 	dp = dsa_slave_to_port(dev);
2681e358bef7SVladimir Oltean 	ds = dp->ds;
2682e358bef7SVladimir Oltean 
2683e358bef7SVladimir Oltean 	if (ds->ops->port_prechangeupper) {
2684e358bef7SVladimir Oltean 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
2685e358bef7SVladimir Oltean 		if (err)
2686e358bef7SVladimir Oltean 			return notifier_from_errno(err);
2687e358bef7SVladimir Oltean 	}
2688e358bef7SVladimir Oltean 
26892b138406SVladimir Oltean 	if (is_vlan_dev(info->upper_dev))
26904ede74e7SVladimir Oltean 		return dsa_slave_check_8021q_upper(dev, info);
26914ede74e7SVladimir Oltean 
26924ede74e7SVladimir Oltean 	return NOTIFY_DONE;
26934ede74e7SVladimir Oltean }
26944ede74e7SVladimir Oltean 
26954ede74e7SVladimir Oltean static int dsa_slave_netdevice_event(struct notifier_block *nb,
26964ede74e7SVladimir Oltean 				     unsigned long event, void *ptr)
26974ede74e7SVladimir Oltean {
26984ede74e7SVladimir Oltean 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
26994ede74e7SVladimir Oltean 
27004ede74e7SVladimir Oltean 	switch (event) {
27014ede74e7SVladimir Oltean 	case NETDEV_PRECHANGEUPPER: {
27024ede74e7SVladimir Oltean 		struct netdev_notifier_changeupper_info *info = ptr;
27034ede74e7SVladimir Oltean 		int err;
27044ede74e7SVladimir Oltean 
27054ede74e7SVladimir Oltean 		err = dsa_slave_prechangeupper_sanity_check(dev, info);
27064ede74e7SVladimir Oltean 		if (err != NOTIFY_DONE)
27074ede74e7SVladimir Oltean 			return err;
27084ede74e7SVladimir Oltean 
270974918945SVladimir Oltean 		if (dsa_slave_dev_check(dev))
271074918945SVladimir Oltean 			return dsa_slave_prechangeupper(dev, ptr);
271174918945SVladimir Oltean 
271274918945SVladimir Oltean 		if (netif_is_lag_master(dev))
271374918945SVladimir Oltean 			return dsa_slave_lag_prechangeupper(dev, ptr);
271474918945SVladimir Oltean 
271583501299SVladimir Oltean 		break;
27162b138406SVladimir Oltean 	}
271783501299SVladimir Oltean 	case NETDEV_CHANGEUPPER:
2718058102a6STobias Waldekranz 		if (dsa_slave_dev_check(dev))
27198e92ab3aSVivien Didelot 			return dsa_slave_changeupper(dev, ptr);
2720058102a6STobias Waldekranz 
2721058102a6STobias Waldekranz 		if (netif_is_lag_master(dev))
2722058102a6STobias Waldekranz 			return dsa_slave_lag_changeupper(dev, ptr);
2723058102a6STobias Waldekranz 
2724058102a6STobias Waldekranz 		break;
2725058102a6STobias Waldekranz 	case NETDEV_CHANGELOWERSTATE: {
2726058102a6STobias Waldekranz 		struct netdev_notifier_changelowerstate_info *info = ptr;
2727058102a6STobias Waldekranz 		struct dsa_port *dp;
2728058102a6STobias Waldekranz 		int err;
2729058102a6STobias Waldekranz 
2730058102a6STobias Waldekranz 		if (!dsa_slave_dev_check(dev))
2731058102a6STobias Waldekranz 			break;
2732058102a6STobias Waldekranz 
2733058102a6STobias Waldekranz 		dp = dsa_slave_to_port(dev);
2734058102a6STobias Waldekranz 
2735058102a6STobias Waldekranz 		err = dsa_port_lag_change(dp, info->lower_state_info);
2736058102a6STobias Waldekranz 		return notifier_from_errno(err);
2737058102a6STobias Waldekranz 	}
2738295ab96fSVladimir Oltean 	case NETDEV_CHANGE:
2739295ab96fSVladimir Oltean 	case NETDEV_UP: {
2740295ab96fSVladimir Oltean 		/* Track state of master port.
2741295ab96fSVladimir Oltean 		 * DSA driver may require the master port (and indirectly
2742295ab96fSVladimir Oltean 		 * the tagger) to be available for some special operation.
2743295ab96fSVladimir Oltean 		 */
2744295ab96fSVladimir Oltean 		if (netdev_uses_dsa(dev)) {
2745295ab96fSVladimir Oltean 			struct dsa_port *cpu_dp = dev->dsa_ptr;
2746295ab96fSVladimir Oltean 			struct dsa_switch_tree *dst = cpu_dp->ds->dst;
2747295ab96fSVladimir Oltean 
2748295ab96fSVladimir Oltean 			/* Track when the master port is UP */
2749295ab96fSVladimir Oltean 			dsa_tree_master_oper_state_change(dst, dev,
2750295ab96fSVladimir Oltean 							  netif_oper_up(dev));
2751295ab96fSVladimir Oltean 
2752295ab96fSVladimir Oltean 			/* Track when the master port is ready and can accept
2753295ab96fSVladimir Oltean 			 * packet.
2754295ab96fSVladimir Oltean 			 * NETDEV_UP event is not enough to flag a port as ready.
2755295ab96fSVladimir Oltean 			 * We also have to wait for linkwatch_do_dev to dev_activate
2756295ab96fSVladimir Oltean 			 * and emit a NETDEV_CHANGE event.
2757295ab96fSVladimir Oltean 			 * We check if a master port is ready by checking if the dev
2758295ab96fSVladimir Oltean 			 * have a qdisc assigned and is not noop.
2759295ab96fSVladimir Oltean 			 */
2760295ab96fSVladimir Oltean 			dsa_tree_master_admin_state_change(dst, dev,
2761295ab96fSVladimir Oltean 							   !qdisc_tx_is_noop(dev));
2762295ab96fSVladimir Oltean 
2763295ab96fSVladimir Oltean 			return NOTIFY_OK;
2764295ab96fSVladimir Oltean 		}
2765295ab96fSVladimir Oltean 
2766295ab96fSVladimir Oltean 		return NOTIFY_DONE;
2767295ab96fSVladimir Oltean 	}
2768c0a8a9c2SVladimir Oltean 	case NETDEV_GOING_DOWN: {
2769c0a8a9c2SVladimir Oltean 		struct dsa_port *dp, *cpu_dp;
2770c0a8a9c2SVladimir Oltean 		struct dsa_switch_tree *dst;
2771c0a8a9c2SVladimir Oltean 		LIST_HEAD(close_list);
2772c0a8a9c2SVladimir Oltean 
2773c0a8a9c2SVladimir Oltean 		if (!netdev_uses_dsa(dev))
2774c0a8a9c2SVladimir Oltean 			return NOTIFY_DONE;
2775c0a8a9c2SVladimir Oltean 
2776c0a8a9c2SVladimir Oltean 		cpu_dp = dev->dsa_ptr;
2777c0a8a9c2SVladimir Oltean 		dst = cpu_dp->ds->dst;
2778c0a8a9c2SVladimir Oltean 
2779295ab96fSVladimir Oltean 		dsa_tree_master_admin_state_change(dst, dev, false);
2780295ab96fSVladimir Oltean 
2781c0a8a9c2SVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
2782d0004a02SVladimir Oltean 			if (!dsa_port_is_user(dp))
2783c0a8a9c2SVladimir Oltean 				continue;
2784c0a8a9c2SVladimir Oltean 
2785c0a8a9c2SVladimir Oltean 			list_add(&dp->slave->close_list, &close_list);
2786c0a8a9c2SVladimir Oltean 		}
2787c0a8a9c2SVladimir Oltean 
2788c0a8a9c2SVladimir Oltean 		dev_close_many(&close_list, true);
2789c0a8a9c2SVladimir Oltean 
2790c0a8a9c2SVladimir Oltean 		return NOTIFY_OK;
2791c0a8a9c2SVladimir Oltean 	}
2792c0a8a9c2SVladimir Oltean 	default:
2793c0a8a9c2SVladimir Oltean 		break;
2794cc1d5bdaSFlorian Fainelli 	}
2795b73adef6SFlorian Fainelli 
2796b73adef6SFlorian Fainelli 	return NOTIFY_DONE;
2797b73adef6SFlorian Fainelli }
279888e4f0caSVivien Didelot 
2799c4bb76a9SVladimir Oltean static void
2800c4bb76a9SVladimir Oltean dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2801c4bb76a9SVladimir Oltean {
2802c35b57ceSVladimir Oltean 	struct switchdev_notifier_fdb_info info = {};
2803c4bb76a9SVladimir Oltean 
2804c4bb76a9SVladimir Oltean 	info.addr = switchdev_work->addr;
2805c4bb76a9SVladimir Oltean 	info.vid = switchdev_work->vid;
2806c4bb76a9SVladimir Oltean 	info.offloaded = true;
2807c4bb76a9SVladimir Oltean 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
280893c79823SVladimir Oltean 				 switchdev_work->orig_dev, &info.info, NULL);
2809c4bb76a9SVladimir Oltean }
2810c9eb3e0fSArkadi Sharshevsky 
2811c9eb3e0fSArkadi Sharshevsky static void dsa_slave_switchdev_event_work(struct work_struct *work)
2812c9eb3e0fSArkadi Sharshevsky {
2813c9eb3e0fSArkadi Sharshevsky 	struct dsa_switchdev_event_work *switchdev_work =
2814c9eb3e0fSArkadi Sharshevsky 		container_of(work, struct dsa_switchdev_event_work, work);
281568d6d71eSVladimir Oltean 	const unsigned char *addr = switchdev_work->addr;
2816e35f12e9SVladimir Oltean 	struct net_device *dev = switchdev_work->dev;
281768d6d71eSVladimir Oltean 	u16 vid = switchdev_work->vid;
2818e35f12e9SVladimir Oltean 	struct dsa_switch *ds;
2819c4bb76a9SVladimir Oltean 	struct dsa_port *dp;
2820c9eb3e0fSArkadi Sharshevsky 	int err;
2821c9eb3e0fSArkadi Sharshevsky 
2822e35f12e9SVladimir Oltean 	dp = dsa_slave_to_port(dev);
2823e35f12e9SVladimir Oltean 	ds = dp->ds;
2824c4bb76a9SVladimir Oltean 
2825c9eb3e0fSArkadi Sharshevsky 	switch (switchdev_work->event) {
2826c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
28273dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
282868d6d71eSVladimir Oltean 			err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
2829e212fa7cSVladimir Oltean 		else if (dp->lag)
283068d6d71eSVladimir Oltean 			err = dsa_port_lag_fdb_add(dp, addr, vid);
28313dc80afcSVladimir Oltean 		else
283268d6d71eSVladimir Oltean 			err = dsa_port_fdb_add(dp, addr, vid);
2833c9eb3e0fSArkadi Sharshevsky 		if (err) {
2834c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2835c4bb76a9SVladimir Oltean 				"port %d failed to add %pM vid %d to fdb: %d\n",
283668d6d71eSVladimir Oltean 				dp->index, addr, vid, err);
2837c9eb3e0fSArkadi Sharshevsky 			break;
2838c9eb3e0fSArkadi Sharshevsky 		}
2839c4bb76a9SVladimir Oltean 		dsa_fdb_offload_notify(switchdev_work);
2840c9eb3e0fSArkadi Sharshevsky 		break;
2841c9eb3e0fSArkadi Sharshevsky 
2842c9eb3e0fSArkadi Sharshevsky 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
28433dc80afcSVladimir Oltean 		if (switchdev_work->host_addr)
284468d6d71eSVladimir Oltean 			err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
2845e212fa7cSVladimir Oltean 		else if (dp->lag)
284668d6d71eSVladimir Oltean 			err = dsa_port_lag_fdb_del(dp, addr, vid);
28473dc80afcSVladimir Oltean 		else
284868d6d71eSVladimir Oltean 			err = dsa_port_fdb_del(dp, addr, vid);
2849c9eb3e0fSArkadi Sharshevsky 		if (err) {
2850c4bb76a9SVladimir Oltean 			dev_err(ds->dev,
2851c4bb76a9SVladimir Oltean 				"port %d failed to delete %pM vid %d from fdb: %d\n",
285268d6d71eSVladimir Oltean 				dp->index, addr, vid, err);
2853c9eb3e0fSArkadi Sharshevsky 		}
28542fd18650SVladimir Oltean 
2855c9eb3e0fSArkadi Sharshevsky 		break;
2856c9eb3e0fSArkadi Sharshevsky 	}
2857c9eb3e0fSArkadi Sharshevsky 
2858c9eb3e0fSArkadi Sharshevsky 	kfree(switchdev_work);
2859c9eb3e0fSArkadi Sharshevsky }
2860c9eb3e0fSArkadi Sharshevsky 
2861b94dc99cSVladimir Oltean static bool dsa_foreign_dev_check(const struct net_device *dev,
2862b94dc99cSVladimir Oltean 				  const struct net_device *foreign_dev)
2863d5f19486SVladimir Oltean {
2864b94dc99cSVladimir Oltean 	const struct dsa_port *dp = dsa_slave_to_port(dev);
2865b94dc99cSVladimir Oltean 	struct dsa_switch_tree *dst = dp->ds->dst;
2866b94dc99cSVladimir Oltean 
2867b94dc99cSVladimir Oltean 	if (netif_is_bridge_master(foreign_dev))
2868936db8a2SVladimir Oltean 		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
2869b94dc99cSVladimir Oltean 
2870b94dc99cSVladimir Oltean 	if (netif_is_bridge_port(foreign_dev))
2871b94dc99cSVladimir Oltean 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
2872b94dc99cSVladimir Oltean 
2873b94dc99cSVladimir Oltean 	/* Everything else is foreign */
2874b94dc99cSVladimir Oltean 	return true;
2875d5f19486SVladimir Oltean }
2876d5f19486SVladimir Oltean 
2877b94dc99cSVladimir Oltean static int dsa_slave_fdb_event(struct net_device *dev,
2878716a30a9SVladimir Oltean 			       struct net_device *orig_dev,
2879716a30a9SVladimir Oltean 			       unsigned long event, const void *ctx,
2880716a30a9SVladimir Oltean 			       const struct switchdev_notifier_fdb_info *fdb_info)
2881b94dc99cSVladimir Oltean {
2882b94dc99cSVladimir Oltean 	struct dsa_switchdev_event_work *switchdev_work;
2883b94dc99cSVladimir Oltean 	struct dsa_port *dp = dsa_slave_to_port(dev);
2884b94dc99cSVladimir Oltean 	bool host_addr = fdb_info->is_local;
2885b94dc99cSVladimir Oltean 	struct dsa_switch *ds = dp->ds;
2886b94dc99cSVladimir Oltean 
2887b94dc99cSVladimir Oltean 	if (ctx && ctx != dp)
2888b94dc99cSVladimir Oltean 		return 0;
2889b94dc99cSVladimir Oltean 
2890a860352eSTobias Waldekranz 	if (!dp->bridge)
2891a860352eSTobias Waldekranz 		return 0;
2892a860352eSTobias Waldekranz 
2893e212fa7cSVladimir Oltean 	if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
2894e212fa7cSVladimir Oltean 		if (dsa_port_offloads_bridge_port(dp, orig_dev))
2895b94dc99cSVladimir Oltean 			return 0;
2896b94dc99cSVladimir Oltean 
2897e212fa7cSVladimir Oltean 		/* FDB entries learned by the software bridge or by foreign
2898e212fa7cSVladimir Oltean 		 * bridge ports should be installed as host addresses only if
2899e212fa7cSVladimir Oltean 		 * the driver requests assisted learning.
2900b94dc99cSVladimir Oltean 		 */
2901e212fa7cSVladimir Oltean 		if (!ds->assisted_learning_on_cpu_port)
2902b94dc99cSVladimir Oltean 			return 0;
2903e212fa7cSVladimir Oltean 	}
2904b94dc99cSVladimir Oltean 
2905b94dc99cSVladimir Oltean 	/* Also treat FDB entries on foreign interfaces bridged with us as host
2906b94dc99cSVladimir Oltean 	 * addresses.
2907b94dc99cSVladimir Oltean 	 */
2908b94dc99cSVladimir Oltean 	if (dsa_foreign_dev_check(dev, orig_dev))
2909b94dc99cSVladimir Oltean 		host_addr = true;
2910b94dc99cSVladimir Oltean 
2911e212fa7cSVladimir Oltean 	/* Check early that we're not doing work in vain.
2912e212fa7cSVladimir Oltean 	 * Host addresses on LAG ports still require regular FDB ops,
2913e212fa7cSVladimir Oltean 	 * since the CPU port isn't in a LAG.
2914e212fa7cSVladimir Oltean 	 */
2915e212fa7cSVladimir Oltean 	if (dp->lag && !host_addr) {
2916e212fa7cSVladimir Oltean 		if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
2917e212fa7cSVladimir Oltean 			return -EOPNOTSUPP;
2918e212fa7cSVladimir Oltean 	} else {
2919e212fa7cSVladimir Oltean 		if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
2920e212fa7cSVladimir Oltean 			return -EOPNOTSUPP;
2921e212fa7cSVladimir Oltean 	}
2922e212fa7cSVladimir Oltean 
2923b94dc99cSVladimir Oltean 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2924b94dc99cSVladimir Oltean 	if (!switchdev_work)
2925b94dc99cSVladimir Oltean 		return -ENOMEM;
2926b94dc99cSVladimir Oltean 
2927b94dc99cSVladimir Oltean 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
2928b94dc99cSVladimir Oltean 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
2929b94dc99cSVladimir Oltean 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
2930b94dc99cSVladimir Oltean 		   host_addr ? " as host address" : "");
2931b94dc99cSVladimir Oltean 
2932b94dc99cSVladimir Oltean 	INIT_WORK(&switchdev_work->work, dsa_slave_switchdev_event_work);
2933b94dc99cSVladimir Oltean 	switchdev_work->event = event;
2934b94dc99cSVladimir Oltean 	switchdev_work->dev = dev;
293593c79823SVladimir Oltean 	switchdev_work->orig_dev = orig_dev;
2936b94dc99cSVladimir Oltean 
2937b94dc99cSVladimir Oltean 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
2938b94dc99cSVladimir Oltean 	switchdev_work->vid = fdb_info->vid;
2939b94dc99cSVladimir Oltean 	switchdev_work->host_addr = host_addr;
2940b94dc99cSVladimir Oltean 
2941b94dc99cSVladimir Oltean 	dsa_schedule_work(&switchdev_work->work);
2942b94dc99cSVladimir Oltean 
2943d5f19486SVladimir Oltean 	return 0;
2944d5f19486SVladimir Oltean }
2945d5f19486SVladimir Oltean 
2946c9eb3e0fSArkadi Sharshevsky /* Called under rcu_read_lock() */
2947c9eb3e0fSArkadi Sharshevsky static int dsa_slave_switchdev_event(struct notifier_block *unused,
2948c9eb3e0fSArkadi Sharshevsky 				     unsigned long event, void *ptr)
2949c9eb3e0fSArkadi Sharshevsky {
2950c9eb3e0fSArkadi Sharshevsky 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
295179b139f4SVivien Didelot 	int err;
295279b139f4SVivien Didelot 
2953447d290aSVladimir Oltean 	switch (event) {
2954447d290aSVladimir Oltean 	case SWITCHDEV_PORT_ATTR_SET:
295579b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
295679b139f4SVivien Didelot 						     dsa_slave_dev_check,
295779b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
295879b139f4SVivien Didelot 		return notifier_from_errno(err);
2959447d290aSVladimir Oltean 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2960447d290aSVladimir Oltean 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2961716a30a9SVladimir Oltean 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
2962b94dc99cSVladimir Oltean 							   dsa_slave_dev_check,
2963b94dc99cSVladimir Oltean 							   dsa_foreign_dev_check,
2964ec638740SVladimir Oltean 							   dsa_slave_fdb_event);
2965b94dc99cSVladimir Oltean 		return notifier_from_errno(err);
2966c9eb3e0fSArkadi Sharshevsky 	default:
2967c9eb3e0fSArkadi Sharshevsky 		return NOTIFY_DONE;
2968c9eb3e0fSArkadi Sharshevsky 	}
2969c9eb3e0fSArkadi Sharshevsky 
2970c9eb3e0fSArkadi Sharshevsky 	return NOTIFY_OK;
2971c9eb3e0fSArkadi Sharshevsky }
2972c9eb3e0fSArkadi Sharshevsky 
29732b239f67SPetr Machata static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
29742b239f67SPetr Machata 					      unsigned long event, void *ptr)
29752b239f67SPetr Machata {
29762b239f67SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
297779b139f4SVivien Didelot 	int err;
29782b239f67SPetr Machata 
29792b239f67SPetr Machata 	switch (event) {
298079b139f4SVivien Didelot 	case SWITCHDEV_PORT_OBJ_ADD:
2981164f861bSVladimir Oltean 		err = switchdev_handle_port_obj_add_foreign(dev, ptr,
298279b139f4SVivien Didelot 							    dsa_slave_dev_check,
2983164f861bSVladimir Oltean 							    dsa_foreign_dev_check,
298479b139f4SVivien Didelot 							    dsa_slave_port_obj_add);
298579b139f4SVivien Didelot 		return notifier_from_errno(err);
29862b239f67SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
2987164f861bSVladimir Oltean 		err = switchdev_handle_port_obj_del_foreign(dev, ptr,
298879b139f4SVivien Didelot 							    dsa_slave_dev_check,
2989164f861bSVladimir Oltean 							    dsa_foreign_dev_check,
299079b139f4SVivien Didelot 							    dsa_slave_port_obj_del);
299179b139f4SVivien Didelot 		return notifier_from_errno(err);
29929ed1ecedSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
299379b139f4SVivien Didelot 		err = switchdev_handle_port_attr_set(dev, ptr,
299479b139f4SVivien Didelot 						     dsa_slave_dev_check,
299579b139f4SVivien Didelot 						     dsa_slave_port_attr_set);
299679b139f4SVivien Didelot 		return notifier_from_errno(err);
29972b239f67SPetr Machata 	}
29982b239f67SPetr Machata 
29992b239f67SPetr Machata 	return NOTIFY_DONE;
30002b239f67SPetr Machata }
30012b239f67SPetr Machata 
300288e4f0caSVivien Didelot static struct notifier_block dsa_slave_nb __read_mostly = {
300388e4f0caSVivien Didelot 	.notifier_call  = dsa_slave_netdevice_event,
300488e4f0caSVivien Didelot };
300588e4f0caSVivien Didelot 
3006010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_notifier = {
3007c9eb3e0fSArkadi Sharshevsky 	.notifier_call = dsa_slave_switchdev_event,
3008c9eb3e0fSArkadi Sharshevsky };
3009c9eb3e0fSArkadi Sharshevsky 
3010010e269fSVladimir Oltean struct notifier_block dsa_slave_switchdev_blocking_notifier = {
30112b239f67SPetr Machata 	.notifier_call = dsa_slave_switchdev_blocking_event,
30122b239f67SPetr Machata };
30132b239f67SPetr Machata 
301488e4f0caSVivien Didelot int dsa_slave_register_notifier(void)
301588e4f0caSVivien Didelot {
30162b239f67SPetr Machata 	struct notifier_block *nb;
3017c9eb3e0fSArkadi Sharshevsky 	int err;
3018c9eb3e0fSArkadi Sharshevsky 
3019c9eb3e0fSArkadi Sharshevsky 	err = register_netdevice_notifier(&dsa_slave_nb);
3020c9eb3e0fSArkadi Sharshevsky 	if (err)
3021c9eb3e0fSArkadi Sharshevsky 		return err;
3022c9eb3e0fSArkadi Sharshevsky 
3023c9eb3e0fSArkadi Sharshevsky 	err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
3024c9eb3e0fSArkadi Sharshevsky 	if (err)
3025c9eb3e0fSArkadi Sharshevsky 		goto err_switchdev_nb;
3026c9eb3e0fSArkadi Sharshevsky 
30272b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
30282b239f67SPetr Machata 	err = register_switchdev_blocking_notifier(nb);
30292b239f67SPetr Machata 	if (err)
30302b239f67SPetr Machata 		goto err_switchdev_blocking_nb;
30312b239f67SPetr Machata 
3032c9eb3e0fSArkadi Sharshevsky 	return 0;
3033c9eb3e0fSArkadi Sharshevsky 
30342b239f67SPetr Machata err_switchdev_blocking_nb:
30352b239f67SPetr Machata 	unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3036c9eb3e0fSArkadi Sharshevsky err_switchdev_nb:
3037c9eb3e0fSArkadi Sharshevsky 	unregister_netdevice_notifier(&dsa_slave_nb);
3038c9eb3e0fSArkadi Sharshevsky 	return err;
303988e4f0caSVivien Didelot }
304088e4f0caSVivien Didelot 
304188e4f0caSVivien Didelot void dsa_slave_unregister_notifier(void)
304288e4f0caSVivien Didelot {
30432b239f67SPetr Machata 	struct notifier_block *nb;
304488e4f0caSVivien Didelot 	int err;
304588e4f0caSVivien Didelot 
30462b239f67SPetr Machata 	nb = &dsa_slave_switchdev_blocking_notifier;
30472b239f67SPetr Machata 	err = unregister_switchdev_blocking_notifier(nb);
30482b239f67SPetr Machata 	if (err)
30492b239f67SPetr Machata 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
30502b239f67SPetr Machata 
3051c9eb3e0fSArkadi Sharshevsky 	err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
3052c9eb3e0fSArkadi Sharshevsky 	if (err)
3053c9eb3e0fSArkadi Sharshevsky 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3054c9eb3e0fSArkadi Sharshevsky 
305588e4f0caSVivien Didelot 	err = unregister_netdevice_notifier(&dsa_slave_nb);
305688e4f0caSVivien Didelot 	if (err)
305788e4f0caSVivien Didelot 		pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
305888e4f0caSVivien Didelot }
3059