xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision 99d469fc)
168cf027fSGrygorii Strashko // SPDX-License-Identifier: GPL-2.0
2df828598SMugunthan V N /*
3df828598SMugunthan V N  * Texas Instruments Ethernet Switch Driver
4df828598SMugunthan V N  *
5df828598SMugunthan V N  * Copyright (C) 2012 Texas Instruments
6df828598SMugunthan V N  *
7df828598SMugunthan V N  */
8df828598SMugunthan V N 
9df828598SMugunthan V N #include <linux/kernel.h>
10df828598SMugunthan V N #include <linux/io.h>
11df828598SMugunthan V N #include <linux/clk.h>
12df828598SMugunthan V N #include <linux/timer.h>
13df828598SMugunthan V N #include <linux/module.h>
14df828598SMugunthan V N #include <linux/platform_device.h>
15df828598SMugunthan V N #include <linux/irqreturn.h>
16df828598SMugunthan V N #include <linux/interrupt.h>
17df828598SMugunthan V N #include <linux/if_ether.h>
18df828598SMugunthan V N #include <linux/etherdevice.h>
19df828598SMugunthan V N #include <linux/netdevice.h>
202e5b38abSRichard Cochran #include <linux/net_tstamp.h>
21df828598SMugunthan V N #include <linux/phy.h>
223ff18849SGrygorii Strashko #include <linux/phy/phy.h>
23df828598SMugunthan V N #include <linux/workqueue.h>
24df828598SMugunthan V N #include <linux/delay.h>
25f150bd7fSMugunthan V N #include <linux/pm_runtime.h>
26e2b3e493SArnd Bergmann #include <linux/gpio/consumer.h>
272eb32b0aSMugunthan V N #include <linux/of.h>
289e42f715SHeiko Schocher #include <linux/of_mdio.h>
292eb32b0aSMugunthan V N #include <linux/of_net.h>
302eb32b0aSMugunthan V N #include <linux/of_device.h>
313b72c2feSMugunthan V N #include <linux/if_vlan.h>
32514c6032SRandy Dunlap #include <linux/kmemleak.h>
339611d6d6SIvan Khoronzhuk #include <linux/sys_soc.h>
349ed4050cSIvan Khoronzhuk #include <net/page_pool.h>
359ed4050cSIvan Khoronzhuk #include <linux/bpf.h>
369ed4050cSIvan Khoronzhuk #include <linux/bpf_trace.h>
37df828598SMugunthan V N 
38739683b4SMugunthan V N #include <linux/pinctrl/consumer.h>
397929a668SIvan Khoronzhuk #include <net/pkt_cls.h>
40df828598SMugunthan V N 
41dbe34724SMugunthan V N #include "cpsw.h"
42df828598SMugunthan V N #include "cpsw_ale.h"
43814b4a67SGrygorii Strashko #include "cpsw_priv.h"
44cfc08345SGrygorii Strashko #include "cpsw_sl.h"
452e5b38abSRichard Cochran #include "cpts.h"
46df828598SMugunthan V N #include "davinci_cpdma.h"
47df828598SMugunthan V N 
4857d90148SIvan Khoronzhuk #include <net/pkt_sched.h>
4957d90148SIvan Khoronzhuk 
50df828598SMugunthan V N static int debug_level;
51df828598SMugunthan V N module_param(debug_level, int, 0);
52df828598SMugunthan V N MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
53df828598SMugunthan V N 
54df828598SMugunthan V N static int ale_ageout = 10;
55df828598SMugunthan V N module_param(ale_ageout, int, 0);
56df828598SMugunthan V N MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
57df828598SMugunthan V N 
58df828598SMugunthan V N static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
59df828598SMugunthan V N module_param(rx_packet_max, int, 0);
60df828598SMugunthan V N MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
61df828598SMugunthan V N 
6290225bf0SGrygorii Strashko static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
6390225bf0SGrygorii Strashko module_param(descs_pool_size, int, 0444);
6490225bf0SGrygorii Strashko MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
6590225bf0SGrygorii Strashko 
66df828598SMugunthan V N #define for_each_slave(priv, func, arg...)				\
67df828598SMugunthan V N 	do {								\
686e6ceaedSSebastian Siewior 		struct cpsw_slave *slave;				\
69606f3993SIvan Khoronzhuk 		struct cpsw_common *cpsw = (priv)->cpsw;		\
706e6ceaedSSebastian Siewior 		int n;							\
71606f3993SIvan Khoronzhuk 		if (cpsw->data.dual_emac)				\
72606f3993SIvan Khoronzhuk 			(func)((cpsw)->slaves + priv->emac_port, ##arg);\
73d9ba8f9eSMugunthan V N 		else							\
74606f3993SIvan Khoronzhuk 			for (n = cpsw->data.slaves,			\
75606f3993SIvan Khoronzhuk 					slave = cpsw->slaves;		\
766e6ceaedSSebastian Siewior 					n; n--)				\
776e6ceaedSSebastian Siewior 				(func)(slave++, ##arg);			\
78df828598SMugunthan V N 	} while (0)
79d9ba8f9eSMugunthan V N 
8051a95337SGrygorii Strashko static int cpsw_slave_index_priv(struct cpsw_common *cpsw,
8151a95337SGrygorii Strashko 				 struct cpsw_priv *priv)
8251a95337SGrygorii Strashko {
8351a95337SGrygorii Strashko 	return cpsw->data.dual_emac ? priv->emac_port : cpsw->data.active_slave;
8451a95337SGrygorii Strashko }
8551a95337SGrygorii Strashko 
8651a95337SGrygorii Strashko static int cpsw_get_slave_port(u32 slave_num)
8751a95337SGrygorii Strashko {
8851a95337SGrygorii Strashko 	return slave_num + 1;
8951a95337SGrygorii Strashko }
9051a95337SGrygorii Strashko 
9100fe4712SIvan Khoronzhuk static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
9200fe4712SIvan Khoronzhuk 				    __be16 proto, u16 vid);
9300fe4712SIvan Khoronzhuk 
940cd8f9ccSMugunthan V N static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
950cd8f9ccSMugunthan V N {
962a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
972a05a622SIvan Khoronzhuk 	struct cpsw_ale *ale = cpsw->ale;
980cd8f9ccSMugunthan V N 	int i;
990cd8f9ccSMugunthan V N 
100606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
1010cd8f9ccSMugunthan V N 		bool flag = false;
1020cd8f9ccSMugunthan V N 
1030cd8f9ccSMugunthan V N 		/* Enabling promiscuous mode for one interface will be
1040cd8f9ccSMugunthan V N 		 * common for both the interface as the interface shares
1050cd8f9ccSMugunthan V N 		 * the same hardware resource.
1060cd8f9ccSMugunthan V N 		 */
107606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++)
108606f3993SIvan Khoronzhuk 			if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
1090cd8f9ccSMugunthan V N 				flag = true;
1100cd8f9ccSMugunthan V N 
1110cd8f9ccSMugunthan V N 		if (!enable && flag) {
1120cd8f9ccSMugunthan V N 			enable = true;
1130cd8f9ccSMugunthan V N 			dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
1140cd8f9ccSMugunthan V N 		}
1150cd8f9ccSMugunthan V N 
1160cd8f9ccSMugunthan V N 		if (enable) {
1170cd8f9ccSMugunthan V N 			/* Enable Bypass */
1180cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
1190cd8f9ccSMugunthan V N 
1200cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
1210cd8f9ccSMugunthan V N 		} else {
1220cd8f9ccSMugunthan V N 			/* Disable Bypass */
1230cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
1240cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
1250cd8f9ccSMugunthan V N 		}
1260cd8f9ccSMugunthan V N 	} else {
1270cd8f9ccSMugunthan V N 		if (enable) {
1280cd8f9ccSMugunthan V N 			unsigned long timeout = jiffies + HZ;
1290cd8f9ccSMugunthan V N 
1306f979eb3SLennart Sorensen 			/* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
131606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
1320cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
1330cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 1);
1340cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
1350cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 1);
1360cd8f9ccSMugunthan V N 			}
1370cd8f9ccSMugunthan V N 
1380cd8f9ccSMugunthan V N 			/* Clear All Untouched entries */
1390cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
1400cd8f9ccSMugunthan V N 			do {
1410cd8f9ccSMugunthan V N 				cpu_relax();
1420cd8f9ccSMugunthan V N 				if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
1430cd8f9ccSMugunthan V N 					break;
1440cd8f9ccSMugunthan V N 			} while (time_after(timeout, jiffies));
1450cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
1460cd8f9ccSMugunthan V N 
1470cd8f9ccSMugunthan V N 			/* Clear all mcast from ALE */
14861f1cef9SGrygorii Strashko 			cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
14915180ecaSIvan Khoronzhuk 			__hw_addr_ref_unsync_dev(&ndev->mc, ndev, NULL);
1500cd8f9ccSMugunthan V N 
1510cd8f9ccSMugunthan V N 			/* Flood All Unicast Packets to Host port */
1520cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
1530cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity enabled\n");
1540cd8f9ccSMugunthan V N 		} else {
1556f979eb3SLennart Sorensen 			/* Don't Flood All Unicast Packets to Host port */
1560cd8f9ccSMugunthan V N 			cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
1570cd8f9ccSMugunthan V N 
1586f979eb3SLennart Sorensen 			/* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
159606f3993SIvan Khoronzhuk 			for (i = 0; i <= cpsw->data.slaves; i++) {
1600cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
1610cd8f9ccSMugunthan V N 						     ALE_PORT_NOLEARN, 0);
1620cd8f9ccSMugunthan V N 				cpsw_ale_control_set(ale, i,
1630cd8f9ccSMugunthan V N 						     ALE_PORT_NO_SA_UPDATE, 0);
1640cd8f9ccSMugunthan V N 			}
1650cd8f9ccSMugunthan V N 			dev_dbg(&ndev->dev, "promiscuity disabled\n");
1660cd8f9ccSMugunthan V N 		}
1670cd8f9ccSMugunthan V N 	}
1680cd8f9ccSMugunthan V N }
1690cd8f9ccSMugunthan V N 
17015180ecaSIvan Khoronzhuk /**
17115180ecaSIvan Khoronzhuk  * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes
17215180ecaSIvan Khoronzhuk  * if it's not deleted
17315180ecaSIvan Khoronzhuk  * @ndev: device to sync
17415180ecaSIvan Khoronzhuk  * @addr: address to be added or deleted
17515180ecaSIvan Khoronzhuk  * @vid: vlan id, if vid < 0 set/unset address for real device
17615180ecaSIvan Khoronzhuk  * @add: add address if the flag is set or remove otherwise
17715180ecaSIvan Khoronzhuk  */
17815180ecaSIvan Khoronzhuk static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
17915180ecaSIvan Khoronzhuk 		       int vid, int add)
1805c50a856SMugunthan V N {
1815c50a856SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
182606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
18315180ecaSIvan Khoronzhuk 	int mask, flags, ret;
18425906052SMugunthan V N 
18515180ecaSIvan Khoronzhuk 	if (vid < 0) {
18615180ecaSIvan Khoronzhuk 		if (cpsw->data.dual_emac)
187606f3993SIvan Khoronzhuk 			vid = cpsw->slaves[priv->emac_port].port_vlan;
18815180ecaSIvan Khoronzhuk 		else
1895da19489SIvan Khoronzhuk 			vid = 0;
1905da19489SIvan Khoronzhuk 	}
1915da19489SIvan Khoronzhuk 
19215180ecaSIvan Khoronzhuk 	mask = cpsw->data.dual_emac ? ALE_PORT_HOST : ALE_ALL_PORTS;
19315180ecaSIvan Khoronzhuk 	flags = vid ? ALE_VLAN : 0;
19415180ecaSIvan Khoronzhuk 
19515180ecaSIvan Khoronzhuk 	if (add)
19615180ecaSIvan Khoronzhuk 		ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
19715180ecaSIvan Khoronzhuk 	else
19815180ecaSIvan Khoronzhuk 		ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
19915180ecaSIvan Khoronzhuk 
20015180ecaSIvan Khoronzhuk 	return ret;
20115180ecaSIvan Khoronzhuk }
20215180ecaSIvan Khoronzhuk 
20315180ecaSIvan Khoronzhuk static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
20415180ecaSIvan Khoronzhuk {
20515180ecaSIvan Khoronzhuk 	struct addr_sync_ctx *sync_ctx = ctx;
20615180ecaSIvan Khoronzhuk 	struct netdev_hw_addr *ha;
20715180ecaSIvan Khoronzhuk 	int found = 0, ret = 0;
20815180ecaSIvan Khoronzhuk 
20915180ecaSIvan Khoronzhuk 	if (!vdev || !(vdev->flags & IFF_UP))
21015180ecaSIvan Khoronzhuk 		return 0;
21115180ecaSIvan Khoronzhuk 
21215180ecaSIvan Khoronzhuk 	/* vlan address is relevant if its sync_cnt != 0 */
21315180ecaSIvan Khoronzhuk 	netdev_for_each_mc_addr(ha, vdev) {
21415180ecaSIvan Khoronzhuk 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
21515180ecaSIvan Khoronzhuk 			found = ha->sync_cnt;
21615180ecaSIvan Khoronzhuk 			break;
21715180ecaSIvan Khoronzhuk 		}
21815180ecaSIvan Khoronzhuk 	}
21915180ecaSIvan Khoronzhuk 
22015180ecaSIvan Khoronzhuk 	if (found)
22115180ecaSIvan Khoronzhuk 		sync_ctx->consumed++;
22215180ecaSIvan Khoronzhuk 
22315180ecaSIvan Khoronzhuk 	if (sync_ctx->flush) {
22415180ecaSIvan Khoronzhuk 		if (!found)
22515180ecaSIvan Khoronzhuk 			cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
22615180ecaSIvan Khoronzhuk 		return 0;
22715180ecaSIvan Khoronzhuk 	}
22815180ecaSIvan Khoronzhuk 
22915180ecaSIvan Khoronzhuk 	if (found)
23015180ecaSIvan Khoronzhuk 		ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
23115180ecaSIvan Khoronzhuk 
23215180ecaSIvan Khoronzhuk 	return ret;
23315180ecaSIvan Khoronzhuk }
23415180ecaSIvan Khoronzhuk 
23515180ecaSIvan Khoronzhuk static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
23615180ecaSIvan Khoronzhuk {
23715180ecaSIvan Khoronzhuk 	struct addr_sync_ctx sync_ctx;
23815180ecaSIvan Khoronzhuk 	int ret;
23915180ecaSIvan Khoronzhuk 
24015180ecaSIvan Khoronzhuk 	sync_ctx.consumed = 0;
24115180ecaSIvan Khoronzhuk 	sync_ctx.addr = addr;
24215180ecaSIvan Khoronzhuk 	sync_ctx.ndev = ndev;
24315180ecaSIvan Khoronzhuk 	sync_ctx.flush = 0;
24415180ecaSIvan Khoronzhuk 
24515180ecaSIvan Khoronzhuk 	ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
24615180ecaSIvan Khoronzhuk 	if (sync_ctx.consumed < num && !ret)
24715180ecaSIvan Khoronzhuk 		ret = cpsw_set_mc(ndev, addr, -1, 1);
24815180ecaSIvan Khoronzhuk 
24915180ecaSIvan Khoronzhuk 	return ret;
25015180ecaSIvan Khoronzhuk }
25115180ecaSIvan Khoronzhuk 
25215180ecaSIvan Khoronzhuk static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
25315180ecaSIvan Khoronzhuk {
25415180ecaSIvan Khoronzhuk 	struct addr_sync_ctx sync_ctx;
25515180ecaSIvan Khoronzhuk 
25615180ecaSIvan Khoronzhuk 	sync_ctx.consumed = 0;
25715180ecaSIvan Khoronzhuk 	sync_ctx.addr = addr;
25815180ecaSIvan Khoronzhuk 	sync_ctx.ndev = ndev;
25915180ecaSIvan Khoronzhuk 	sync_ctx.flush = 1;
26015180ecaSIvan Khoronzhuk 
26115180ecaSIvan Khoronzhuk 	vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
26215180ecaSIvan Khoronzhuk 	if (sync_ctx.consumed == num)
26315180ecaSIvan Khoronzhuk 		cpsw_set_mc(ndev, addr, -1, 0);
26415180ecaSIvan Khoronzhuk 
26515180ecaSIvan Khoronzhuk 	return 0;
26615180ecaSIvan Khoronzhuk }
26715180ecaSIvan Khoronzhuk 
26815180ecaSIvan Khoronzhuk static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
26915180ecaSIvan Khoronzhuk {
27015180ecaSIvan Khoronzhuk 	struct addr_sync_ctx *sync_ctx = ctx;
27115180ecaSIvan Khoronzhuk 	struct netdev_hw_addr *ha;
27215180ecaSIvan Khoronzhuk 	int found = 0;
27315180ecaSIvan Khoronzhuk 
27415180ecaSIvan Khoronzhuk 	if (!vdev || !(vdev->flags & IFF_UP))
27515180ecaSIvan Khoronzhuk 		return 0;
27615180ecaSIvan Khoronzhuk 
27715180ecaSIvan Khoronzhuk 	/* vlan address is relevant if its sync_cnt != 0 */
27815180ecaSIvan Khoronzhuk 	netdev_for_each_mc_addr(ha, vdev) {
27915180ecaSIvan Khoronzhuk 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
28015180ecaSIvan Khoronzhuk 			found = ha->sync_cnt;
28115180ecaSIvan Khoronzhuk 			break;
28215180ecaSIvan Khoronzhuk 		}
28315180ecaSIvan Khoronzhuk 	}
28415180ecaSIvan Khoronzhuk 
28515180ecaSIvan Khoronzhuk 	if (!found)
28615180ecaSIvan Khoronzhuk 		return 0;
28715180ecaSIvan Khoronzhuk 
28815180ecaSIvan Khoronzhuk 	sync_ctx->consumed++;
28915180ecaSIvan Khoronzhuk 	cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
29015180ecaSIvan Khoronzhuk 	return 0;
29115180ecaSIvan Khoronzhuk }
29215180ecaSIvan Khoronzhuk 
29315180ecaSIvan Khoronzhuk static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
29415180ecaSIvan Khoronzhuk {
29515180ecaSIvan Khoronzhuk 	struct addr_sync_ctx sync_ctx;
29615180ecaSIvan Khoronzhuk 
29715180ecaSIvan Khoronzhuk 	sync_ctx.addr = addr;
29815180ecaSIvan Khoronzhuk 	sync_ctx.ndev = ndev;
29915180ecaSIvan Khoronzhuk 	sync_ctx.consumed = 0;
30015180ecaSIvan Khoronzhuk 
30115180ecaSIvan Khoronzhuk 	vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx);
30215180ecaSIvan Khoronzhuk 	if (sync_ctx.consumed < num)
30315180ecaSIvan Khoronzhuk 		cpsw_set_mc(ndev, addr, -1, 0);
30415180ecaSIvan Khoronzhuk 
3055da19489SIvan Khoronzhuk 	return 0;
3065da19489SIvan Khoronzhuk }
3075da19489SIvan Khoronzhuk 
3085da19489SIvan Khoronzhuk static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
3095da19489SIvan Khoronzhuk {
31006095f34SGrygorii Strashko 	struct cpsw_priv *priv = netdev_priv(ndev);
31106095f34SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
31206095f34SGrygorii Strashko 	int slave_port = -1;
31306095f34SGrygorii Strashko 
31406095f34SGrygorii Strashko 	if (cpsw->data.dual_emac)
31506095f34SGrygorii Strashko 		slave_port = priv->emac_port + 1;
3165c50a856SMugunthan V N 
3175c50a856SMugunthan V N 	if (ndev->flags & IFF_PROMISC) {
3185c50a856SMugunthan V N 		/* Enable promiscuous mode */
3190cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, true);
32006095f34SGrygorii Strashko 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, slave_port);
3215c50a856SMugunthan V N 		return;
3220cd8f9ccSMugunthan V N 	} else {
3230cd8f9ccSMugunthan V N 		/* Disable promiscuous mode */
3240cd8f9ccSMugunthan V N 		cpsw_set_promiscious(ndev, false);
3255c50a856SMugunthan V N 	}
3265c50a856SMugunthan V N 
3271e5c4bc4SLennart Sorensen 	/* Restore allmulti on vlans if necessary */
32806095f34SGrygorii Strashko 	cpsw_ale_set_allmulti(cpsw->ale,
32906095f34SGrygorii Strashko 			      ndev->flags & IFF_ALLMULTI, slave_port);
3301e5c4bc4SLennart Sorensen 
33115180ecaSIvan Khoronzhuk 	/* add/remove mcast address either for real netdev or for vlan */
33215180ecaSIvan Khoronzhuk 	__hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr,
33315180ecaSIvan Khoronzhuk 			       cpsw_del_mc_addr);
3345c50a856SMugunthan V N }
3355c50a856SMugunthan V N 
3369ed4050cSIvan Khoronzhuk static unsigned int cpsw_rxbuf_total_len(unsigned int len)
3379ed4050cSIvan Khoronzhuk {
3389ed4050cSIvan Khoronzhuk 	len += CPSW_HEADROOM;
3399ed4050cSIvan Khoronzhuk 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3409ed4050cSIvan Khoronzhuk 
3419ed4050cSIvan Khoronzhuk 	return SKB_DATA_ALIGN(len);
3429ed4050cSIvan Khoronzhuk }
3439ed4050cSIvan Khoronzhuk 
3441a3b5056SOlof Johansson static void cpsw_rx_handler(void *token, int len, int status)
345df828598SMugunthan V N {
3469ed4050cSIvan Khoronzhuk 	struct page		*new_page, *page = token;
3479ed4050cSIvan Khoronzhuk 	void			*pa = page_address(page);
3489ed4050cSIvan Khoronzhuk 	struct cpsw_meta_xdp	*xmeta = pa + CPSW_XMETA_OFFSET;
3499ed4050cSIvan Khoronzhuk 	struct cpsw_common	*cpsw = ndev_to_cpsw(xmeta->ndev);
3509ed4050cSIvan Khoronzhuk 	int			pkt_size = cpsw->rx_packet_max;
3519ed4050cSIvan Khoronzhuk 	int			ret = 0, port, ch = xmeta->ch;
3529ed4050cSIvan Khoronzhuk 	int			headroom = CPSW_HEADROOM;
3539ed4050cSIvan Khoronzhuk 	struct net_device	*ndev = xmeta->ndev;
354a9423120SIvan Khoronzhuk 	struct cpsw_priv	*priv;
3559ed4050cSIvan Khoronzhuk 	struct page_pool	*pool;
3569ed4050cSIvan Khoronzhuk 	struct sk_buff		*skb;
3579ed4050cSIvan Khoronzhuk 	struct xdp_buff		xdp;
3589ed4050cSIvan Khoronzhuk 	dma_addr_t		dma;
359df828598SMugunthan V N 
3609ed4050cSIvan Khoronzhuk 	if (cpsw->data.dual_emac && status >= 0) {
361fea49f60SIvan Khoronzhuk 		port = CPDMA_RX_SOURCE_PORT(status);
3629ed4050cSIvan Khoronzhuk 		if (port)
363fea49f60SIvan Khoronzhuk 			ndev = cpsw->slaves[--port].ndev;
364fea49f60SIvan Khoronzhuk 	}
365d9ba8f9eSMugunthan V N 
3669ed4050cSIvan Khoronzhuk 	priv = netdev_priv(ndev);
3679ed4050cSIvan Khoronzhuk 	pool = cpsw->page_pool[ch];
36816e5c57dSMugunthan V N 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
369a0e2c822SMugunthan V N 		/* In dual emac mode check for all interfaces */
370d5bc1613SIvan Khoronzhuk 		if (cpsw->data.dual_emac && cpsw->usage_count &&
371fe734d0aSIvan Khoronzhuk 		    (status >= 0)) {
372a0e2c822SMugunthan V N 			/* The packet received is for the interface which
373a0e2c822SMugunthan V N 			 * is already down and the other interface is up
374dbedd44eSJoe Perches 			 * and running, instead of freeing which results
375a0e2c822SMugunthan V N 			 * in reducing of the number of rx descriptor in
3769ed4050cSIvan Khoronzhuk 			 * DMA engine, requeue page back to cpdma.
377a0e2c822SMugunthan V N 			 */
3789ed4050cSIvan Khoronzhuk 			new_page = page;
379a0e2c822SMugunthan V N 			goto requeue;
380a0e2c822SMugunthan V N 		}
381a0e2c822SMugunthan V N 
3829ed4050cSIvan Khoronzhuk 		/* the interface is going down, pages are purged */
3839ed4050cSIvan Khoronzhuk 		page_pool_recycle_direct(pool, page);
384df828598SMugunthan V N 		return;
385df828598SMugunthan V N 	}
386b4727e69SSebastian Siewior 
3879ed4050cSIvan Khoronzhuk 	new_page = page_pool_dev_alloc_pages(pool);
3889ed4050cSIvan Khoronzhuk 	if (unlikely(!new_page)) {
3899ed4050cSIvan Khoronzhuk 		new_page = page;
3909ed4050cSIvan Khoronzhuk 		ndev->stats.rx_dropped++;
3919ed4050cSIvan Khoronzhuk 		goto requeue;
3929ed4050cSIvan Khoronzhuk 	}
3939ed4050cSIvan Khoronzhuk 
3949ed4050cSIvan Khoronzhuk 	if (priv->xdp_prog) {
3959ed4050cSIvan Khoronzhuk 		if (status & CPDMA_RX_VLAN_ENCAP) {
3969ed4050cSIvan Khoronzhuk 			xdp.data = pa + CPSW_HEADROOM +
3979ed4050cSIvan Khoronzhuk 				   CPSW_RX_VLAN_ENCAP_HDR_SIZE;
3989ed4050cSIvan Khoronzhuk 			xdp.data_end = xdp.data + len -
3999ed4050cSIvan Khoronzhuk 				       CPSW_RX_VLAN_ENCAP_HDR_SIZE;
4009ed4050cSIvan Khoronzhuk 		} else {
4019ed4050cSIvan Khoronzhuk 			xdp.data = pa + CPSW_HEADROOM;
4029ed4050cSIvan Khoronzhuk 			xdp.data_end = xdp.data + len;
4039ed4050cSIvan Khoronzhuk 		}
4049ed4050cSIvan Khoronzhuk 
4059ed4050cSIvan Khoronzhuk 		xdp_set_data_meta_invalid(&xdp);
4069ed4050cSIvan Khoronzhuk 
4079ed4050cSIvan Khoronzhuk 		xdp.data_hard_start = pa;
4089ed4050cSIvan Khoronzhuk 		xdp.rxq = &priv->xdp_rxq[ch];
409c88c3518SJesper Dangaard Brouer 		xdp.frame_sz = PAGE_SIZE;
4109ed4050cSIvan Khoronzhuk 
411c5013ac1SGrygorii Strashko 		port = priv->emac_port + cpsw->data.dual_emac;
412c5013ac1SGrygorii Strashko 		ret = cpsw_run_xdp(priv, ch, &xdp, page, port);
4139ed4050cSIvan Khoronzhuk 		if (ret != CPSW_XDP_PASS)
4149ed4050cSIvan Khoronzhuk 			goto requeue;
4159ed4050cSIvan Khoronzhuk 
4169ed4050cSIvan Khoronzhuk 		/* XDP prog might have changed packet data and boundaries */
4179ed4050cSIvan Khoronzhuk 		len = xdp.data_end - xdp.data;
4189ed4050cSIvan Khoronzhuk 		headroom = xdp.data - xdp.data_hard_start;
4199ed4050cSIvan Khoronzhuk 
4209ed4050cSIvan Khoronzhuk 		/* XDP prog can modify vlan tag, so can't use encap header */
4219ed4050cSIvan Khoronzhuk 		status &= ~CPDMA_RX_VLAN_ENCAP;
4229ed4050cSIvan Khoronzhuk 	}
4239ed4050cSIvan Khoronzhuk 
4249ed4050cSIvan Khoronzhuk 	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
4259ed4050cSIvan Khoronzhuk 	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
4269ed4050cSIvan Khoronzhuk 	if (!skb) {
4279ed4050cSIvan Khoronzhuk 		ndev->stats.rx_dropped++;
4289ed4050cSIvan Khoronzhuk 		page_pool_recycle_direct(pool, page);
4299ed4050cSIvan Khoronzhuk 		goto requeue;
4309ed4050cSIvan Khoronzhuk 	}
4319ed4050cSIvan Khoronzhuk 
4329ed4050cSIvan Khoronzhuk 	skb_reserve(skb, headroom);
433df828598SMugunthan V N 	skb_put(skb, len);
4349ed4050cSIvan Khoronzhuk 	skb->dev = ndev;
435a3a41d2fSGrygorii Strashko 	if (status & CPDMA_RX_VLAN_ENCAP)
436a3a41d2fSGrygorii Strashko 		cpsw_rx_vlan_encap(skb);
437a9423120SIvan Khoronzhuk 	if (priv->rx_ts_enabled)
4382a05a622SIvan Khoronzhuk 		cpts_rx_timestamp(cpsw->cpts, skb);
439df828598SMugunthan V N 	skb->protocol = eth_type_trans(skb, ndev);
4409ed4050cSIvan Khoronzhuk 
4419ed4050cSIvan Khoronzhuk 	/* unmap page as no netstack skb page recycling */
4429ed4050cSIvan Khoronzhuk 	page_pool_release_page(pool, page);
443df828598SMugunthan V N 	netif_receive_skb(skb);
4449ed4050cSIvan Khoronzhuk 
4458dc43ddcSTobias Klauser 	ndev->stats.rx_bytes += len;
4468dc43ddcSTobias Klauser 	ndev->stats.rx_packets++;
447df828598SMugunthan V N 
448a0e2c822SMugunthan V N requeue:
4499ed4050cSIvan Khoronzhuk 	xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
4509ed4050cSIvan Khoronzhuk 	xmeta->ndev = ndev;
4519ed4050cSIvan Khoronzhuk 	xmeta->ch = ch;
4529ed4050cSIvan Khoronzhuk 
4539ed4050cSIvan Khoronzhuk 	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
4549ed4050cSIvan Khoronzhuk 	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
4559ed4050cSIvan Khoronzhuk 				       pkt_size, 0);
456871e8465SIvan Khoronzhuk 	if (ret < 0) {
457871e8465SIvan Khoronzhuk 		WARN_ON(ret == -ENOMEM);
4589ed4050cSIvan Khoronzhuk 		page_pool_recycle_direct(pool, new_page);
459df828598SMugunthan V N 	}
460871e8465SIvan Khoronzhuk }
461df828598SMugunthan V N 
462df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
463df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
464df828598SMugunthan V N {
465df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
466df828598SMugunthan V N 	u32			mac_control = 0;
467df828598SMugunthan V N 	u32			slave_port;
468606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
469df828598SMugunthan V N 
470df828598SMugunthan V N 	if (!phy)
471df828598SMugunthan V N 		return;
472df828598SMugunthan V N 
4736f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
474df828598SMugunthan V N 
475df828598SMugunthan V N 	if (phy->link) {
476cfc08345SGrygorii Strashko 		mac_control = CPSW_SL_CTL_GMII_EN;
477cfc08345SGrygorii Strashko 
478cfc08345SGrygorii Strashko 		if (phy->speed == 1000)
479cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_GIG;
480cfc08345SGrygorii Strashko 		if (phy->duplex)
481cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_FULLDUPLEX;
482cfc08345SGrygorii Strashko 
483cfc08345SGrygorii Strashko 		/* set speed_in input in case RMII mode is used in 100Mbps */
484cfc08345SGrygorii Strashko 		if (phy->speed == 100)
485cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_IFCTL_A;
486cfc08345SGrygorii Strashko 		/* in band mode only works in 10Mbps RGMII mode */
487cfc08345SGrygorii Strashko 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
488cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
489cfc08345SGrygorii Strashko 
490cfc08345SGrygorii Strashko 		if (priv->rx_pause)
491cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
492cfc08345SGrygorii Strashko 
493cfc08345SGrygorii Strashko 		if (priv->tx_pause)
494cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
495cfc08345SGrygorii Strashko 
496cfc08345SGrygorii Strashko 		if (mac_control != slave->mac_control)
497cfc08345SGrygorii Strashko 			cpsw_sl_ctl_set(slave->mac_sl, mac_control);
498df828598SMugunthan V N 
499df828598SMugunthan V N 		/* enable forwarding */
5002a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
501df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
502df828598SMugunthan V N 
503df828598SMugunthan V N 		*link = true;
50457d90148SIvan Khoronzhuk 
50557d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
50657d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed &&
50757d90148SIvan Khoronzhuk 		    !cpsw_shp_is_off(priv))
50857d90148SIvan Khoronzhuk 			dev_warn(priv->dev,
50957d90148SIvan Khoronzhuk 				 "Speed was changed, CBS shaper speeds are changed!");
510df828598SMugunthan V N 	} else {
511df828598SMugunthan V N 		mac_control = 0;
512df828598SMugunthan V N 		/* disable forwarding */
5132a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
514df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
515cfc08345SGrygorii Strashko 
516cfc08345SGrygorii Strashko 		cpsw_sl_wait_for_idle(slave->mac_sl, 100);
517cfc08345SGrygorii Strashko 
518cfc08345SGrygorii Strashko 		cpsw_sl_ctl_reset(slave->mac_sl);
519df828598SMugunthan V N 	}
520df828598SMugunthan V N 
521cfc08345SGrygorii Strashko 	if (mac_control != slave->mac_control)
522df828598SMugunthan V N 		phy_print_status(phy);
523df828598SMugunthan V N 
524df828598SMugunthan V N 	slave->mac_control = mac_control;
525df828598SMugunthan V N }
526df828598SMugunthan V N 
527df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
528df828598SMugunthan V N {
529df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
5300be01b8eSIvan Khoronzhuk 	struct cpsw_common	*cpsw = priv->cpsw;
531df828598SMugunthan V N 	bool			link = false;
532df828598SMugunthan V N 
533df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
534df828598SMugunthan V N 
535df828598SMugunthan V N 	if (link) {
5360be01b8eSIvan Khoronzhuk 		if (cpsw_need_resplit(cpsw))
5379763a891SGrygorii Strashko 			cpsw_split_res(cpsw);
5380be01b8eSIvan Khoronzhuk 
539df828598SMugunthan V N 		netif_carrier_on(ndev);
540df828598SMugunthan V N 		if (netif_running(ndev))
541e05107e6SIvan Khoronzhuk 			netif_tx_wake_all_queues(ndev);
542df828598SMugunthan V N 	} else {
543df828598SMugunthan V N 		netif_carrier_off(ndev);
544e05107e6SIvan Khoronzhuk 		netif_tx_stop_all_queues(ndev);
545df828598SMugunthan V N 	}
546df828598SMugunthan V N }
547df828598SMugunthan V N 
548d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries(
549d9ba8f9eSMugunthan V N 		struct cpsw_priv *priv, struct cpsw_slave *slave,
550d9ba8f9eSMugunthan V N 		u32 slave_port)
551d9ba8f9eSMugunthan V N {
5522a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
55371a2cbb7SGrygorii Strashko 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
554d9ba8f9eSMugunthan V N 
5552a05a622SIvan Khoronzhuk 	if (cpsw->version == CPSW_VERSION_1)
556d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
557d9ba8f9eSMugunthan V N 	else
558d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
5592a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
560d9ba8f9eSMugunthan V N 			  port_mask, port_mask, 0);
5612a05a622SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
5625b3a5a14SIvan Khoronzhuk 			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0);
5632a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
5642a05a622SIvan Khoronzhuk 			   HOST_PORT_NUM, ALE_VLAN |
5652a05a622SIvan Khoronzhuk 			   ALE_SECURE, slave->port_vlan);
5665e5add17SGrygorii Strashko 	cpsw_ale_control_set(cpsw->ale, slave_port,
5675e5add17SGrygorii Strashko 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
568d9ba8f9eSMugunthan V N }
569d9ba8f9eSMugunthan V N 
5701e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
5711e7a2e21SDaniel Mack {
572df828598SMugunthan V N 	u32 slave_port;
57330c57f07SSekhar Nori 	struct phy_device *phy;
574649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
575df828598SMugunthan V N 
576cfc08345SGrygorii Strashko 	cpsw_sl_reset(slave->mac_sl, 100);
577cfc08345SGrygorii Strashko 	cpsw_sl_ctl_reset(slave->mac_sl);
578df828598SMugunthan V N 
579df828598SMugunthan V N 	/* setup priority mapping */
580cfc08345SGrygorii Strashko 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
581cfc08345SGrygorii Strashko 			  RX_PRIORITY_MAPPING);
5829750a3adSRichard Cochran 
5832a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
5849750a3adSRichard Cochran 	case CPSW_VERSION_1:
5859750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
58648f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
58748f5bcccSGrygorii Strashko 		 * flow control mode
58848f5bcccSGrygorii Strashko 		 */
58948f5bcccSGrygorii Strashko 		slave_write(slave,
59048f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
59148f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
5929750a3adSRichard Cochran 		break;
5939750a3adSRichard Cochran 	case CPSW_VERSION_2:
594c193f365SMugunthan V N 	case CPSW_VERSION_3:
595926489beSMugunthan V N 	case CPSW_VERSION_4:
5969750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
59748f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
59848f5bcccSGrygorii Strashko 		 * flow control mode
59948f5bcccSGrygorii Strashko 		 */
60048f5bcccSGrygorii Strashko 		slave_write(slave,
60148f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
60248f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
6039750a3adSRichard Cochran 		break;
6049750a3adSRichard Cochran 	}
605df828598SMugunthan V N 
606df828598SMugunthan V N 	/* setup max packet size, and mac address */
607cfc08345SGrygorii Strashko 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
608cfc08345SGrygorii Strashko 			  cpsw->rx_packet_max);
609df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
610df828598SMugunthan V N 
611df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
612df828598SMugunthan V N 
6136f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
614df828598SMugunthan V N 
615606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
616d9ba8f9eSMugunthan V N 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
617d9ba8f9eSMugunthan V N 	else
6182a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
619e11b220fSMugunthan V N 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
620df828598SMugunthan V N 
621d733f754SDavid Rivshin 	if (slave->data->phy_node) {
62230c57f07SSekhar Nori 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
6239e42f715SHeiko Schocher 				 &cpsw_adjust_link, 0, slave->data->phy_if);
62430c57f07SSekhar Nori 		if (!phy) {
625f7ce9103SRob Herring 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
626f7ce9103SRob Herring 				slave->data->phy_node,
627d733f754SDavid Rivshin 				slave->slave_num);
628d733f754SDavid Rivshin 			return;
629d733f754SDavid Rivshin 		}
630d733f754SDavid Rivshin 	} else {
63130c57f07SSekhar Nori 		phy = phy_connect(priv->ndev, slave->data->phy_id,
632f9a8f83bSFlorian Fainelli 				 &cpsw_adjust_link, slave->data->phy_if);
63330c57f07SSekhar Nori 		if (IS_ERR(phy)) {
634d733f754SDavid Rivshin 			dev_err(priv->dev,
635d733f754SDavid Rivshin 				"phy \"%s\" not found on slave %d, err %ld\n",
636d733f754SDavid Rivshin 				slave->data->phy_id, slave->slave_num,
63730c57f07SSekhar Nori 				PTR_ERR(phy));
638d733f754SDavid Rivshin 			return;
639d733f754SDavid Rivshin 		}
640d733f754SDavid Rivshin 	}
641d733f754SDavid Rivshin 
64230c57f07SSekhar Nori 	slave->phy = phy;
64330c57f07SSekhar Nori 
6442220943aSAndrew Lunn 	phy_attached_info(slave->phy);
6452220943aSAndrew Lunn 
646df828598SMugunthan V N 	phy_start(slave->phy);
647388367a5SMugunthan V N 
648388367a5SMugunthan V N 	/* Configure GMII_SEL register */
6493ff18849SGrygorii Strashko 	if (!IS_ERR(slave->data->ifphy))
6503ff18849SGrygorii Strashko 		phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
6513ff18849SGrygorii Strashko 				 slave->data->phy_if);
6523ff18849SGrygorii Strashko 	else
6533ff18849SGrygorii Strashko 		cpsw_phy_sel(cpsw->dev, slave->phy->interface,
6543ff18849SGrygorii Strashko 			     slave->slave_num);
655df828598SMugunthan V N }
656df828598SMugunthan V N 
6573b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
6583b72c2feSMugunthan V N {
659606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
660606f3993SIvan Khoronzhuk 	const int vlan = cpsw->data.default_vlan;
6613b72c2feSMugunthan V N 	u32 reg;
6623b72c2feSMugunthan V N 	int i;
6631e5c4bc4SLennart Sorensen 	int unreg_mcast_mask;
6643b72c2feSMugunthan V N 
6652a05a622SIvan Khoronzhuk 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
6663b72c2feSMugunthan V N 	       CPSW2_PORT_VLAN;
6673b72c2feSMugunthan V N 
6685d8d0d4dSIvan Khoronzhuk 	writel(vlan, &cpsw->host_port_regs->port_vlan);
6693b72c2feSMugunthan V N 
670606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
671606f3993SIvan Khoronzhuk 		slave_write(cpsw->slaves + i, vlan, reg);
6723b72c2feSMugunthan V N 
6731e5c4bc4SLennart Sorensen 	if (priv->ndev->flags & IFF_ALLMULTI)
6741e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_ALL_PORTS;
6751e5c4bc4SLennart Sorensen 	else
6761e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
6771e5c4bc4SLennart Sorensen 
6782a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
67961f1cef9SGrygorii Strashko 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
68061f1cef9SGrygorii Strashko 			  unreg_mcast_mask);
6813b72c2feSMugunthan V N }
6823b72c2feSMugunthan V N 
683df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
684df828598SMugunthan V N {
685d9ba8f9eSMugunthan V N 	u32 fifo_mode;
6865d8d0d4dSIvan Khoronzhuk 	u32 control_reg;
6875d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
6883b72c2feSMugunthan V N 
689df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
6905d8d0d4dSIvan Khoronzhuk 	soft_reset("cpsw", &cpsw->regs->soft_reset);
6912a05a622SIvan Khoronzhuk 	cpsw_ale_start(cpsw->ale);
692df828598SMugunthan V N 
693df828598SMugunthan V N 	/* switch to vlan unaware mode */
6942a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
6953b72c2feSMugunthan V N 			     CPSW_ALE_VLAN_AWARE);
6965d8d0d4dSIvan Khoronzhuk 	control_reg = readl(&cpsw->regs->control);
697a3a41d2fSGrygorii Strashko 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
6985d8d0d4dSIvan Khoronzhuk 	writel(control_reg, &cpsw->regs->control);
699606f3993SIvan Khoronzhuk 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
700d9ba8f9eSMugunthan V N 		     CPSW_FIFO_NORMAL_MODE;
7015d8d0d4dSIvan Khoronzhuk 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
702df828598SMugunthan V N 
703df828598SMugunthan V N 	/* setup host port priority mapping */
704dda5f5feSGrygorii Strashko 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
7055d8d0d4dSIvan Khoronzhuk 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
706dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
707df828598SMugunthan V N 
7082a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
709df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
710df828598SMugunthan V N 
711606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac) {
7122a05a622SIvan Khoronzhuk 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
713d9ba8f9eSMugunthan V N 				   0, 0);
7142a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
71571a2cbb7SGrygorii Strashko 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
716df828598SMugunthan V N 	}
717d9ba8f9eSMugunthan V N }
718df828598SMugunthan V N 
7192a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
720aacebbf8SSebastian Siewior {
7213995d265SSchuyler Patton 	u32 slave_port;
7223995d265SSchuyler Patton 
7236f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
7243995d265SSchuyler Patton 
725aacebbf8SSebastian Siewior 	if (!slave->phy)
726aacebbf8SSebastian Siewior 		return;
727aacebbf8SSebastian Siewior 	phy_stop(slave->phy);
728aacebbf8SSebastian Siewior 	phy_disconnect(slave->phy);
729aacebbf8SSebastian Siewior 	slave->phy = NULL;
7302a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, slave_port,
7313995d265SSchuyler Patton 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
732cfc08345SGrygorii Strashko 	cpsw_sl_reset(slave->mac_sl, 100);
733cfc08345SGrygorii Strashko 	cpsw_sl_ctl_reset(slave->mac_sl);
734aacebbf8SSebastian Siewior }
735aacebbf8SSebastian Siewior 
73600fe4712SIvan Khoronzhuk static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
73700fe4712SIvan Khoronzhuk {
73800fe4712SIvan Khoronzhuk 	struct cpsw_priv *priv = arg;
73900fe4712SIvan Khoronzhuk 
74000fe4712SIvan Khoronzhuk 	if (!vdev)
74100fe4712SIvan Khoronzhuk 		return 0;
74200fe4712SIvan Khoronzhuk 
74300fe4712SIvan Khoronzhuk 	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
74400fe4712SIvan Khoronzhuk 	return 0;
74500fe4712SIvan Khoronzhuk }
74600fe4712SIvan Khoronzhuk 
7474b4255edSIvan Khoronzhuk /* restore resources after port reset */
7484b4255edSIvan Khoronzhuk static void cpsw_restore(struct cpsw_priv *priv)
7494b4255edSIvan Khoronzhuk {
75000fe4712SIvan Khoronzhuk 	/* restore vlan configurations */
75100fe4712SIvan Khoronzhuk 	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
75200fe4712SIvan Khoronzhuk 
7534b4255edSIvan Khoronzhuk 	/* restore MQPRIO offload */
7544b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_mqprio_resume, priv);
7554b4255edSIvan Khoronzhuk 
7564b4255edSIvan Khoronzhuk 	/* restore CBS offload */
7574b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_cbs_resume, priv);
7584b4255edSIvan Khoronzhuk }
7594b4255edSIvan Khoronzhuk 
760df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
761df828598SMugunthan V N {
762df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
763649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
7643802dce1SIvan Khoronzhuk 	int ret;
765df828598SMugunthan V N 	u32 reg;
766df828598SMugunthan V N 
76756e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
768108a6537SGrygorii Strashko 	if (ret < 0) {
76956e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
770108a6537SGrygorii Strashko 		return ret;
771108a6537SGrygorii Strashko 	}
7723fa88c51SGrygorii Strashko 
773df828598SMugunthan V N 	netif_carrier_off(ndev);
774df828598SMugunthan V N 
775e05107e6SIvan Khoronzhuk 	/* Notify the stack of the actual queue counts. */
776e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
777e05107e6SIvan Khoronzhuk 	if (ret) {
778e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of tx queues\n");
779e05107e6SIvan Khoronzhuk 		goto err_cleanup;
780e05107e6SIvan Khoronzhuk 	}
781e05107e6SIvan Khoronzhuk 
782e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
783e05107e6SIvan Khoronzhuk 	if (ret) {
784e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of rx queues\n");
785e05107e6SIvan Khoronzhuk 		goto err_cleanup;
786e05107e6SIvan Khoronzhuk 	}
787e05107e6SIvan Khoronzhuk 
7882a05a622SIvan Khoronzhuk 	reg = cpsw->version;
789df828598SMugunthan V N 
790df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
791df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
792df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
793df828598SMugunthan V N 
794d5bc1613SIvan Khoronzhuk 	/* Initialize host and slave ports */
795d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count)
796df828598SMugunthan V N 		cpsw_init_host_port(priv);
797df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
798df828598SMugunthan V N 
7993b72c2feSMugunthan V N 	/* Add default VLAN */
800606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac)
8013b72c2feSMugunthan V N 		cpsw_add_default_vlan(priv);
802e6afea0bSMugunthan V N 	else
8032a05a622SIvan Khoronzhuk 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
80461f1cef9SGrygorii Strashko 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
8053b72c2feSMugunthan V N 
806d5bc1613SIvan Khoronzhuk 	/* initialize shared resources for every ndev */
807d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count) {
808d9ba8f9eSMugunthan V N 		/* disable priority elevation */
809dda5f5feSGrygorii Strashko 		writel_relaxed(0, &cpsw->regs->ptype);
810df828598SMugunthan V N 
811d9ba8f9eSMugunthan V N 		/* enable statistics collection only on all ports */
812dda5f5feSGrygorii Strashko 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
813df828598SMugunthan V N 
8141923d6e4SMugunthan V N 		/* Enable internal fifo flow control */
8155d8d0d4dSIvan Khoronzhuk 		writel(0x7, &cpsw->regs->flow_control);
8161923d6e4SMugunthan V N 
817dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_rx);
818dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_tx);
819d354eb85SMugunthan V N 
820e38b5a3dSIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
821e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
822e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
8237da11600SMugunthan V N 		}
8247da11600SMugunthan V N 
825e38b5a3dSIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
826e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
827e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
8287da11600SMugunthan V N 		}
8297da11600SMugunthan V N 
8309ed4050cSIvan Khoronzhuk 		/* create rxqs for both infs in dual mac as they use same pool
8319ed4050cSIvan Khoronzhuk 		 * and must be destroyed together when no users.
8329ed4050cSIvan Khoronzhuk 		 */
8339ed4050cSIvan Khoronzhuk 		ret = cpsw_create_xdp_rxqs(cpsw);
8349ed4050cSIvan Khoronzhuk 		if (ret < 0)
8359ed4050cSIvan Khoronzhuk 			goto err_cleanup;
8369ed4050cSIvan Khoronzhuk 
8373802dce1SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
8383802dce1SIvan Khoronzhuk 		if (ret < 0)
839aacebbf8SSebastian Siewior 			goto err_cleanup;
840f280e89aSMugunthan V N 
8418a2c9a5aSGrygorii Strashko 		if (cpts_register(cpsw->cpts))
842f280e89aSMugunthan V N 			dev_err(priv->dev, "error registering cpts device\n");
843f280e89aSMugunthan V N 
844d9ba8f9eSMugunthan V N 	}
845df828598SMugunthan V N 
8464b4255edSIvan Khoronzhuk 	cpsw_restore(priv);
8474b4255edSIvan Khoronzhuk 
848ff5b8ef2SMugunthan V N 	/* Enable Interrupt pacing if configured */
8492a05a622SIvan Khoronzhuk 	if (cpsw->coal_intvl != 0) {
850ff5b8ef2SMugunthan V N 		struct ethtool_coalesce coal;
851ff5b8ef2SMugunthan V N 
8522a05a622SIvan Khoronzhuk 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
853ff5b8ef2SMugunthan V N 		cpsw_set_coalesce(ndev, &coal);
854ff5b8ef2SMugunthan V N 	}
855ff5b8ef2SMugunthan V N 
8562c836bd9SIvan Khoronzhuk 	cpdma_ctlr_start(cpsw->dma);
8572c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
858d5bc1613SIvan Khoronzhuk 	cpsw->usage_count++;
859f63a975eSMugunthan V N 
860df828598SMugunthan V N 	return 0;
861df828598SMugunthan V N 
862aacebbf8SSebastian Siewior err_cleanup:
86302cacedeSIvan Khoronzhuk 	if (!cpsw->usage_count) {
8642c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
8659ed4050cSIvan Khoronzhuk 		cpsw_destroy_xdp_rxqs(cpsw);
86602cacedeSIvan Khoronzhuk 	}
86702cacedeSIvan Khoronzhuk 
8689ed4050cSIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
86956e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
870aacebbf8SSebastian Siewior 	netif_carrier_off(priv->ndev);
871aacebbf8SSebastian Siewior 	return ret;
872df828598SMugunthan V N }
873df828598SMugunthan V N 
874df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
875df828598SMugunthan V N {
876df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
877649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
878df828598SMugunthan V N 
879df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
88015180ecaSIvan Khoronzhuk 	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
881e05107e6SIvan Khoronzhuk 	netif_tx_stop_all_queues(priv->ndev);
882df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
883d9ba8f9eSMugunthan V N 
884d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count <= 1) {
885dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_rx);
886dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_tx);
8872a05a622SIvan Khoronzhuk 		cpts_unregister(cpsw->cpts);
8882c836bd9SIvan Khoronzhuk 		cpsw_intr_disable(cpsw);
8892c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
8902a05a622SIvan Khoronzhuk 		cpsw_ale_stop(cpsw->ale);
8919ed4050cSIvan Khoronzhuk 		cpsw_destroy_xdp_rxqs(cpsw);
892d9ba8f9eSMugunthan V N 	}
8932a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
8940be01b8eSIvan Khoronzhuk 
8950be01b8eSIvan Khoronzhuk 	if (cpsw_need_resplit(cpsw))
8969763a891SGrygorii Strashko 		cpsw_split_res(cpsw);
8970be01b8eSIvan Khoronzhuk 
898d5bc1613SIvan Khoronzhuk 	cpsw->usage_count--;
89956e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
900df828598SMugunthan V N 	return 0;
901df828598SMugunthan V N }
902df828598SMugunthan V N 
903df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
904df828598SMugunthan V N 				       struct net_device *ndev)
905df828598SMugunthan V N {
906df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
9072c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
908f44f8417SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
909e05107e6SIvan Khoronzhuk 	struct netdev_queue *txq;
910e05107e6SIvan Khoronzhuk 	struct cpdma_chan *txch;
911e05107e6SIvan Khoronzhuk 	int ret, q_idx;
912df828598SMugunthan V N 
913df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
914df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
9158dc43ddcSTobias Klauser 		ndev->stats.tx_dropped++;
9161bf96050SIvan Khoronzhuk 		return NET_XMIT_DROP;
917df828598SMugunthan V N 	}
918df828598SMugunthan V N 
9199232b16dSMugunthan V N 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
920a9423120SIvan Khoronzhuk 	    priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
9212e5b38abSRichard Cochran 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
9222e5b38abSRichard Cochran 
923e05107e6SIvan Khoronzhuk 	q_idx = skb_get_queue_mapping(skb);
924e05107e6SIvan Khoronzhuk 	if (q_idx >= cpsw->tx_ch_num)
925e05107e6SIvan Khoronzhuk 		q_idx = q_idx % cpsw->tx_ch_num;
926e05107e6SIvan Khoronzhuk 
9278feb0a19SIvan Khoronzhuk 	txch = cpsw->txv[q_idx].ch;
92862f94c21SGrygorii Strashko 	txq = netdev_get_tx_queue(ndev, q_idx);
92910ae8054SGrygorii Strashko 	skb_tx_timestamp(skb);
93010ae8054SGrygorii Strashko 	ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
93110ae8054SGrygorii Strashko 				priv->emac_port + cpsw->data.dual_emac);
932df828598SMugunthan V N 	if (unlikely(ret != 0)) {
933df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
934df828598SMugunthan V N 		goto fail;
935df828598SMugunthan V N 	}
936df828598SMugunthan V N 
937fae50823SMugunthan V N 	/* If there is no more tx desc left free then we need to
938fae50823SMugunthan V N 	 * tell the kernel to stop sending us tx frames.
939fae50823SMugunthan V N 	 */
940e05107e6SIvan Khoronzhuk 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
941e05107e6SIvan Khoronzhuk 		netif_tx_stop_queue(txq);
94262f94c21SGrygorii Strashko 
94362f94c21SGrygorii Strashko 		/* Barrier, so that stop_queue visible to other cpus */
94462f94c21SGrygorii Strashko 		smp_mb__after_atomic();
94562f94c21SGrygorii Strashko 
94662f94c21SGrygorii Strashko 		if (cpdma_check_free_tx_desc(txch))
94762f94c21SGrygorii Strashko 			netif_tx_wake_queue(txq);
948e05107e6SIvan Khoronzhuk 	}
949fae50823SMugunthan V N 
950df828598SMugunthan V N 	return NETDEV_TX_OK;
951df828598SMugunthan V N fail:
9528dc43ddcSTobias Klauser 	ndev->stats.tx_dropped++;
953e05107e6SIvan Khoronzhuk 	netif_tx_stop_queue(txq);
95462f94c21SGrygorii Strashko 
95562f94c21SGrygorii Strashko 	/* Barrier, so that stop_queue visible to other cpus */
95662f94c21SGrygorii Strashko 	smp_mb__after_atomic();
95762f94c21SGrygorii Strashko 
95862f94c21SGrygorii Strashko 	if (cpdma_check_free_tx_desc(txch))
95962f94c21SGrygorii Strashko 		netif_tx_wake_queue(txq);
96062f94c21SGrygorii Strashko 
961df828598SMugunthan V N 	return NETDEV_TX_BUSY;
962df828598SMugunthan V N }
963df828598SMugunthan V N 
964dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
965dcfd8d58SMugunthan V N {
966dcfd8d58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
967dcfd8d58SMugunthan V N 	struct sockaddr *addr = (struct sockaddr *)p;
968649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
969dcfd8d58SMugunthan V N 	int flags = 0;
970dcfd8d58SMugunthan V N 	u16 vid = 0;
971a6c5d14fSGrygorii Strashko 	int ret;
972dcfd8d58SMugunthan V N 
973dcfd8d58SMugunthan V N 	if (!is_valid_ether_addr(addr->sa_data))
974dcfd8d58SMugunthan V N 		return -EADDRNOTAVAIL;
975dcfd8d58SMugunthan V N 
97656e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
977a6c5d14fSGrygorii Strashko 	if (ret < 0) {
97856e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
979a6c5d14fSGrygorii Strashko 		return ret;
980a6c5d14fSGrygorii Strashko 	}
981a6c5d14fSGrygorii Strashko 
982606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
983606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
984dcfd8d58SMugunthan V N 		flags = ALE_VLAN;
985dcfd8d58SMugunthan V N 	}
986dcfd8d58SMugunthan V N 
9872a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
988dcfd8d58SMugunthan V N 			   flags, vid);
9892a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
990dcfd8d58SMugunthan V N 			   flags, vid);
991dcfd8d58SMugunthan V N 
992dcfd8d58SMugunthan V N 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
993dcfd8d58SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
994dcfd8d58SMugunthan V N 	for_each_slave(priv, cpsw_set_slave_mac, priv);
995dcfd8d58SMugunthan V N 
99656e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
997a6c5d14fSGrygorii Strashko 
998dcfd8d58SMugunthan V N 	return 0;
999dcfd8d58SMugunthan V N }
1000dcfd8d58SMugunthan V N 
10013b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
10023b72c2feSMugunthan V N 				unsigned short vid)
10033b72c2feSMugunthan V N {
10043b72c2feSMugunthan V N 	int ret;
10059f6bd8faSMugunthan V N 	int unreg_mcast_mask = 0;
10065b3a5a14SIvan Khoronzhuk 	int mcast_mask;
10079f6bd8faSMugunthan V N 	u32 port_mask;
1008606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
10099f6bd8faSMugunthan V N 
1010606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
10119f6bd8faSMugunthan V N 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
10129f6bd8faSMugunthan V N 
10135b3a5a14SIvan Khoronzhuk 		mcast_mask = ALE_PORT_HOST;
10149f6bd8faSMugunthan V N 		if (priv->ndev->flags & IFF_ALLMULTI)
10155b3a5a14SIvan Khoronzhuk 			unreg_mcast_mask = mcast_mask;
10169f6bd8faSMugunthan V N 	} else {
10179f6bd8faSMugunthan V N 		port_mask = ALE_ALL_PORTS;
10185b3a5a14SIvan Khoronzhuk 		mcast_mask = port_mask;
10191e5c4bc4SLennart Sorensen 
10201e5c4bc4SLennart Sorensen 		if (priv->ndev->flags & IFF_ALLMULTI)
10211e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_ALL_PORTS;
10221e5c4bc4SLennart Sorensen 		else
10231e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
10249f6bd8faSMugunthan V N 	}
10253b72c2feSMugunthan V N 
10262a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
102761f1cef9SGrygorii Strashko 				unreg_mcast_mask);
10283b72c2feSMugunthan V N 	if (ret != 0)
10293b72c2feSMugunthan V N 		return ret;
10303b72c2feSMugunthan V N 
10312a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
103271a2cbb7SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
10333b72c2feSMugunthan V N 	if (ret != 0)
10343b72c2feSMugunthan V N 		goto clean_vid;
10353b72c2feSMugunthan V N 
10362a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
10375b3a5a14SIvan Khoronzhuk 				 mcast_mask, ALE_VLAN, vid, 0);
10383b72c2feSMugunthan V N 	if (ret != 0)
10393b72c2feSMugunthan V N 		goto clean_vlan_ucast;
10403b72c2feSMugunthan V N 	return 0;
10413b72c2feSMugunthan V N 
10423b72c2feSMugunthan V N clean_vlan_ucast:
10432a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
104471a2cbb7SGrygorii Strashko 			   HOST_PORT_NUM, ALE_VLAN, vid);
10453b72c2feSMugunthan V N clean_vid:
10462a05a622SIvan Khoronzhuk 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
10473b72c2feSMugunthan V N 	return ret;
10483b72c2feSMugunthan V N }
10493b72c2feSMugunthan V N 
10503b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
105180d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
10523b72c2feSMugunthan V N {
10533b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1054649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1055a6c5d14fSGrygorii Strashko 	int ret;
10563b72c2feSMugunthan V N 
1057606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
10583b72c2feSMugunthan V N 		return 0;
10593b72c2feSMugunthan V N 
106056e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1061a6c5d14fSGrygorii Strashko 	if (ret < 0) {
106256e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1063a6c5d14fSGrygorii Strashko 		return ret;
1064a6c5d14fSGrygorii Strashko 	}
1065a6c5d14fSGrygorii Strashko 
1066606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
106702a54164SMugunthan V N 		/* In dual EMAC, reserved VLAN id should not be used for
106802a54164SMugunthan V N 		 * creating VLAN interfaces as this can break the dual
106902a54164SMugunthan V N 		 * EMAC port separation
107002a54164SMugunthan V N 		 */
107102a54164SMugunthan V N 		int i;
107202a54164SMugunthan V N 
1073606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
1074803c4f64SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan) {
1075803c4f64SIvan Khoronzhuk 				ret = -EINVAL;
1076803c4f64SIvan Khoronzhuk 				goto err;
1077803c4f64SIvan Khoronzhuk 			}
107802a54164SMugunthan V N 		}
107902a54164SMugunthan V N 	}
108002a54164SMugunthan V N 
10813b72c2feSMugunthan V N 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
1082a6c5d14fSGrygorii Strashko 	ret = cpsw_add_vlan_ale_entry(priv, vid);
1083803c4f64SIvan Khoronzhuk err:
108456e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
1085a6c5d14fSGrygorii Strashko 	return ret;
10863b72c2feSMugunthan V N }
10873b72c2feSMugunthan V N 
10883b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
108980d5c368SPatrick McHardy 				     __be16 proto, u16 vid)
10903b72c2feSMugunthan V N {
10913b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1092649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
10933b72c2feSMugunthan V N 	int ret;
10943b72c2feSMugunthan V N 
1095606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
10963b72c2feSMugunthan V N 		return 0;
10973b72c2feSMugunthan V N 
109856e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1099a6c5d14fSGrygorii Strashko 	if (ret < 0) {
110056e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1101a6c5d14fSGrygorii Strashko 		return ret;
1102a6c5d14fSGrygorii Strashko 	}
1103a6c5d14fSGrygorii Strashko 
1104606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
110502a54164SMugunthan V N 		int i;
110602a54164SMugunthan V N 
1107606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
1108606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
1109803c4f64SIvan Khoronzhuk 				goto err;
111002a54164SMugunthan V N 		}
111102a54164SMugunthan V N 	}
111202a54164SMugunthan V N 
11133b72c2feSMugunthan V N 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
11142a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1115be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
111661f1cef9SGrygorii Strashko 				  HOST_PORT_NUM, ALE_VLAN, vid);
1117be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
11183b72c2feSMugunthan V N 				  0, ALE_VLAN, vid);
111999d469fcSMurali Karicheri 	ret |= cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
1120803c4f64SIvan Khoronzhuk err:
112156e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
1122a6c5d14fSGrygorii Strashko 	return ret;
11233b72c2feSMugunthan V N }
11243b72c2feSMugunthan V N 
11259ed4050cSIvan Khoronzhuk static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
11269ed4050cSIvan Khoronzhuk 			     struct xdp_frame **frames, u32 flags)
11279ed4050cSIvan Khoronzhuk {
11289ed4050cSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
1129c5013ac1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
11309ed4050cSIvan Khoronzhuk 	struct xdp_frame *xdpf;
1131c5013ac1SGrygorii Strashko 	int i, drops = 0, port;
11329ed4050cSIvan Khoronzhuk 
11339ed4050cSIvan Khoronzhuk 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
11349ed4050cSIvan Khoronzhuk 		return -EINVAL;
11359ed4050cSIvan Khoronzhuk 
11369ed4050cSIvan Khoronzhuk 	for (i = 0; i < n; i++) {
11379ed4050cSIvan Khoronzhuk 		xdpf = frames[i];
11389ed4050cSIvan Khoronzhuk 		if (xdpf->len < CPSW_MIN_PACKET_SIZE) {
11399ed4050cSIvan Khoronzhuk 			xdp_return_frame_rx_napi(xdpf);
11409ed4050cSIvan Khoronzhuk 			drops++;
11419ed4050cSIvan Khoronzhuk 			continue;
11429ed4050cSIvan Khoronzhuk 		}
11439ed4050cSIvan Khoronzhuk 
1144c5013ac1SGrygorii Strashko 		port = priv->emac_port + cpsw->data.dual_emac;
1145c5013ac1SGrygorii Strashko 		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, port))
11469ed4050cSIvan Khoronzhuk 			drops++;
11479ed4050cSIvan Khoronzhuk 	}
11489ed4050cSIvan Khoronzhuk 
11499ed4050cSIvan Khoronzhuk 	return n - drops;
11509ed4050cSIvan Khoronzhuk }
11519ed4050cSIvan Khoronzhuk 
1152026cc9c3SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
1153026cc9c3SDavid S. Miller static void cpsw_ndo_poll_controller(struct net_device *ndev)
1154026cc9c3SDavid S. Miller {
1155026cc9c3SDavid S. Miller 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1156026cc9c3SDavid S. Miller 
1157026cc9c3SDavid S. Miller 	cpsw_intr_disable(cpsw);
1158026cc9c3SDavid S. Miller 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1159026cc9c3SDavid S. Miller 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1160026cc9c3SDavid S. Miller 	cpsw_intr_enable(cpsw);
1161026cc9c3SDavid S. Miller }
1162026cc9c3SDavid S. Miller #endif
1163026cc9c3SDavid S. Miller 
1164df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
1165df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
1166df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
1167df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
1168dcfd8d58SMugunthan V N 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
11692e5b38abSRichard Cochran 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
1170df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
1171df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
11725c50a856SMugunthan V N 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
117383fcad0cSIvan Khoronzhuk 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
1174df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
1175df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
1176df828598SMugunthan V N #endif
11773b72c2feSMugunthan V N 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
11783b72c2feSMugunthan V N 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
11797929a668SIvan Khoronzhuk 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
11809ed4050cSIvan Khoronzhuk 	.ndo_bpf		= cpsw_ndo_bpf,
11819ed4050cSIvan Khoronzhuk 	.ndo_xdp_xmit		= cpsw_ndo_xdp_xmit,
1182df828598SMugunthan V N };
1183df828598SMugunthan V N 
1184df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
1185df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
1186df828598SMugunthan V N {
1187649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
118856e31bd8SIvan Khoronzhuk 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
11897826d43fSJiri Pirko 
119052c4f0ecSMugunthan V N 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
11917826d43fSJiri Pirko 	strlcpy(info->version, "1.0", sizeof(info->version));
119256e31bd8SIvan Khoronzhuk 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1193df828598SMugunthan V N }
1194df828598SMugunthan V N 
11951923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev,
11961923d6e4SMugunthan V N 			       struct ethtool_pauseparam *pause)
11971923d6e4SMugunthan V N {
11981923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
11991923d6e4SMugunthan V N 	bool link;
12001923d6e4SMugunthan V N 
12011923d6e4SMugunthan V N 	priv->rx_pause = pause->rx_pause ? true : false;
12021923d6e4SMugunthan V N 	priv->tx_pause = pause->tx_pause ? true : false;
12031923d6e4SMugunthan V N 
12041923d6e4SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
12051923d6e4SMugunthan V N 	return 0;
12061923d6e4SMugunthan V N }
12071923d6e4SMugunthan V N 
1208022d7ad7SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev,
1209022d7ad7SIvan Khoronzhuk 			     struct ethtool_channels *chs)
1210022d7ad7SIvan Khoronzhuk {
1211c24eef28SGrygorii Strashko 	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1212be034fc1SGrygorii Strashko }
1213be034fc1SGrygorii Strashko 
1214df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
12153b6e1a4eSJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1216df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
1217df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
1218df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
1219df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
12202e5b38abSRichard Cochran 	.get_ts_info	= cpsw_get_ts_info,
1221ff5b8ef2SMugunthan V N 	.get_coalesce	= cpsw_get_coalesce,
1222ff5b8ef2SMugunthan V N 	.set_coalesce	= cpsw_set_coalesce,
1223d9718546SMugunthan V N 	.get_sset_count		= cpsw_get_sset_count,
1224d9718546SMugunthan V N 	.get_strings		= cpsw_get_strings,
1225d9718546SMugunthan V N 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
12261923d6e4SMugunthan V N 	.get_pauseparam		= cpsw_get_pauseparam,
12271923d6e4SMugunthan V N 	.set_pauseparam		= cpsw_set_pauseparam,
1228d8a64420SMatus Ujhelyi 	.get_wol	= cpsw_get_wol,
1229d8a64420SMatus Ujhelyi 	.set_wol	= cpsw_set_wol,
123052c4f0ecSMugunthan V N 	.get_regs_len	= cpsw_get_regs_len,
123152c4f0ecSMugunthan V N 	.get_regs	= cpsw_get_regs,
12327898b1daSGrygorii Strashko 	.begin		= cpsw_ethtool_op_begin,
12337898b1daSGrygorii Strashko 	.complete	= cpsw_ethtool_op_complete,
1234ce52c744SIvan Khoronzhuk 	.get_channels	= cpsw_get_channels,
1235ce52c744SIvan Khoronzhuk 	.set_channels	= cpsw_set_channels,
12362479876dSPhilippe Reynes 	.get_link_ksettings	= cpsw_get_link_ksettings,
12372479876dSPhilippe Reynes 	.set_link_ksettings	= cpsw_set_link_ksettings,
1238a0909949SYegor Yefremov 	.get_eee	= cpsw_get_eee,
1239a0909949SYegor Yefremov 	.set_eee	= cpsw_set_eee,
12406bb10c2bSYegor Yefremov 	.nway_reset	= cpsw_nway_reset,
1241be034fc1SGrygorii Strashko 	.get_ringparam = cpsw_get_ringparam,
1242be034fc1SGrygorii Strashko 	.set_ringparam = cpsw_set_ringparam,
1243df828598SMugunthan V N };
1244df828598SMugunthan V N 
1245552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data,
12462eb32b0aSMugunthan V N 			 struct platform_device *pdev)
12472eb32b0aSMugunthan V N {
12482eb32b0aSMugunthan V N 	struct device_node *node = pdev->dev.of_node;
12492eb32b0aSMugunthan V N 	struct device_node *slave_node;
12502eb32b0aSMugunthan V N 	int i = 0, ret;
12512eb32b0aSMugunthan V N 	u32 prop;
12522eb32b0aSMugunthan V N 
12532eb32b0aSMugunthan V N 	if (!node)
12542eb32b0aSMugunthan V N 		return -EINVAL;
12552eb32b0aSMugunthan V N 
12562eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "slaves", &prop)) {
125788c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
12582eb32b0aSMugunthan V N 		return -EINVAL;
12592eb32b0aSMugunthan V N 	}
12602eb32b0aSMugunthan V N 	data->slaves = prop;
12612eb32b0aSMugunthan V N 
1262e86ac13bSMugunthan V N 	if (of_property_read_u32(node, "active_slave", &prop)) {
126388c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
1264aa1a15e2SDaniel Mack 		return -EINVAL;
126578ca0b28SRichard Cochran 	}
1266e86ac13bSMugunthan V N 	data->active_slave = prop;
126778ca0b28SRichard Cochran 
1268a86854d0SKees Cook 	data->slave_data = devm_kcalloc(&pdev->dev,
1269a86854d0SKees Cook 					data->slaves,
1270a86854d0SKees Cook 					sizeof(struct cpsw_slave_data),
1271b2adaca9SJoe Perches 					GFP_KERNEL);
1272b2adaca9SJoe Perches 	if (!data->slave_data)
1273aa1a15e2SDaniel Mack 		return -ENOMEM;
12742eb32b0aSMugunthan V N 
12752eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
127688c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
1277aa1a15e2SDaniel Mack 		return -EINVAL;
12782eb32b0aSMugunthan V N 	}
12792eb32b0aSMugunthan V N 	data->channels = prop;
12802eb32b0aSMugunthan V N 
12812eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "ale_entries", &prop)) {
128288c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
1283aa1a15e2SDaniel Mack 		return -EINVAL;
12842eb32b0aSMugunthan V N 	}
12852eb32b0aSMugunthan V N 	data->ale_entries = prop;
12862eb32b0aSMugunthan V N 
12872eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
128888c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
1289aa1a15e2SDaniel Mack 		return -EINVAL;
12902eb32b0aSMugunthan V N 	}
12912eb32b0aSMugunthan V N 	data->bd_ram_size = prop;
12922eb32b0aSMugunthan V N 
12932eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "mac_control", &prop)) {
129488c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
1295aa1a15e2SDaniel Mack 		return -EINVAL;
12962eb32b0aSMugunthan V N 	}
12972eb32b0aSMugunthan V N 	data->mac_control = prop;
12982eb32b0aSMugunthan V N 
1299281abd96SMarkus Pargmann 	if (of_property_read_bool(node, "dual_emac"))
1300281abd96SMarkus Pargmann 		data->dual_emac = 1;
1301d9ba8f9eSMugunthan V N 
13021fb19aa7SVaibhav Hiremath 	/*
13031fb19aa7SVaibhav Hiremath 	 * Populate all the child nodes here...
13041fb19aa7SVaibhav Hiremath 	 */
13051fb19aa7SVaibhav Hiremath 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
13061fb19aa7SVaibhav Hiremath 	/* We do not want to force this, as in some cases may not have child */
13071fb19aa7SVaibhav Hiremath 	if (ret)
130888c99ff6SGeorge Cherian 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
13091fb19aa7SVaibhav Hiremath 
13108658aaf2SBen Hutchings 	for_each_available_child_of_node(node, slave_node) {
1311549985eeSRichard Cochran 		struct cpsw_slave_data *slave_data = data->slave_data + i;
1312549985eeSRichard Cochran 		const void *mac_addr = NULL;
1313549985eeSRichard Cochran 		int lenp;
1314549985eeSRichard Cochran 		const __be32 *parp;
1315549985eeSRichard Cochran 
1316f468b10eSMarkus Pargmann 		/* This is no slave child node, continue */
1317bf5849f1SRob Herring 		if (!of_node_name_eq(slave_node, "slave"))
1318f468b10eSMarkus Pargmann 			continue;
1319f468b10eSMarkus Pargmann 
13203ff18849SGrygorii Strashko 		slave_data->ifphy = devm_of_phy_get(&pdev->dev, slave_node,
13213ff18849SGrygorii Strashko 						    NULL);
13223ff18849SGrygorii Strashko 		if (!IS_ENABLED(CONFIG_TI_CPSW_PHY_SEL) &&
13233ff18849SGrygorii Strashko 		    IS_ERR(slave_data->ifphy)) {
13243ff18849SGrygorii Strashko 			ret = PTR_ERR(slave_data->ifphy);
13253ff18849SGrygorii Strashko 			dev_err(&pdev->dev,
13263ff18849SGrygorii Strashko 				"%d: Error retrieving port phy: %d\n", i, ret);
13273cd6e20fSNishka Dasgupta 			goto err_node_put;
13283ff18849SGrygorii Strashko 		}
13293ff18849SGrygorii Strashko 
1330337d1727SMarek Vasut 		slave_data->slave_node = slave_node;
1331552165bcSDavid Rivshin 		slave_data->phy_node = of_parse_phandle(slave_node,
1332552165bcSDavid Rivshin 							"phy-handle", 0);
1333f1eea5c1SDavid Rivshin 		parp = of_get_property(slave_node, "phy_id", &lenp);
1334ae092b5bSDavid Rivshin 		if (slave_data->phy_node) {
1335ae092b5bSDavid Rivshin 			dev_dbg(&pdev->dev,
1336f7ce9103SRob Herring 				"slave[%d] using phy-handle=\"%pOF\"\n",
1337f7ce9103SRob Herring 				i, slave_data->phy_node);
1338ae092b5bSDavid Rivshin 		} else if (of_phy_is_fixed_link(slave_node)) {
1339dfc0a6d3SDavid Rivshin 			/* In the case of a fixed PHY, the DT node associated
1340dfc0a6d3SDavid Rivshin 			 * to the PHY is the Ethernet MAC DT node.
1341dfc0a6d3SDavid Rivshin 			 */
13421f71e8c9SMarkus Brunner 			ret = of_phy_register_fixed_link(slave_node);
134323a09873SJohan Hovold 			if (ret) {
134423a09873SJohan Hovold 				if (ret != -EPROBE_DEFER)
134523a09873SJohan Hovold 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
13463cd6e20fSNishka Dasgupta 				goto err_node_put;
134723a09873SJohan Hovold 			}
134806cd6d6eSDavid Rivshin 			slave_data->phy_node = of_node_get(slave_node);
1349f1eea5c1SDavid Rivshin 		} else if (parp) {
1350f1eea5c1SDavid Rivshin 			u32 phyid;
1351f1eea5c1SDavid Rivshin 			struct device_node *mdio_node;
1352f1eea5c1SDavid Rivshin 			struct platform_device *mdio;
1353f1eea5c1SDavid Rivshin 
1354f1eea5c1SDavid Rivshin 			if (lenp != (sizeof(__be32) * 2)) {
1355f1eea5c1SDavid Rivshin 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
135647276fccSMugunthan V N 				goto no_phy_slave;
1357549985eeSRichard Cochran 			}
1358549985eeSRichard Cochran 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
1359549985eeSRichard Cochran 			phyid = be32_to_cpup(parp+1);
1360549985eeSRichard Cochran 			mdio = of_find_device_by_node(mdio_node);
136160e71ab5SJohan Hovold 			of_node_put(mdio_node);
13626954cc1fSJohan Hovold 			if (!mdio) {
136356fdb2e0SMarkus Pargmann 				dev_err(&pdev->dev, "Missing mdio platform device\n");
13643cd6e20fSNishka Dasgupta 				ret = -EINVAL;
13653cd6e20fSNishka Dasgupta 				goto err_node_put;
13666954cc1fSJohan Hovold 			}
1367549985eeSRichard Cochran 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
1368549985eeSRichard Cochran 				 PHY_ID_FMT, mdio->name, phyid);
136986e1d5adSJohan Hovold 			put_device(&mdio->dev);
1370f1eea5c1SDavid Rivshin 		} else {
1371ae092b5bSDavid Rivshin 			dev_err(&pdev->dev,
1372ae092b5bSDavid Rivshin 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
1373ae092b5bSDavid Rivshin 				i);
1374f1eea5c1SDavid Rivshin 			goto no_phy_slave;
1375f1eea5c1SDavid Rivshin 		}
13760c65b2b9SAndrew Lunn 		ret = of_get_phy_mode(slave_node, &slave_data->phy_if);
13770c65b2b9SAndrew Lunn 		if (ret) {
137847276fccSMugunthan V N 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
137947276fccSMugunthan V N 				i);
13803cd6e20fSNishka Dasgupta 			goto err_node_put;
138147276fccSMugunthan V N 		}
138247276fccSMugunthan V N 
138347276fccSMugunthan V N no_phy_slave:
1384549985eeSRichard Cochran 		mac_addr = of_get_mac_address(slave_node);
1385a51645f7SPetr Štetiar 		if (!IS_ERR(mac_addr)) {
13862d2924afSPetr Štetiar 			ether_addr_copy(slave_data->mac_addr, mac_addr);
13870ba517b1SMarkus Pargmann 		} else {
1388b6745f6eSMugunthan V N 			ret = ti_cm_get_macid(&pdev->dev, i,
13890ba517b1SMarkus Pargmann 					      slave_data->mac_addr);
13900ba517b1SMarkus Pargmann 			if (ret)
13913cd6e20fSNishka Dasgupta 				goto err_node_put;
13920ba517b1SMarkus Pargmann 		}
1393d9ba8f9eSMugunthan V N 		if (data->dual_emac) {
139491c4166cSMugunthan V N 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
1395d9ba8f9eSMugunthan V N 						 &prop)) {
139688c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
1397d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = i+1;
139888c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
1399d9ba8f9eSMugunthan V N 					slave_data->dual_emac_res_vlan, i);
1400d9ba8f9eSMugunthan V N 			} else {
1401d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = prop;
1402d9ba8f9eSMugunthan V N 			}
1403d9ba8f9eSMugunthan V N 		}
1404d9ba8f9eSMugunthan V N 
1405549985eeSRichard Cochran 		i++;
14063cd6e20fSNishka Dasgupta 		if (i == data->slaves) {
14073cd6e20fSNishka Dasgupta 			ret = 0;
14083cd6e20fSNishka Dasgupta 			goto err_node_put;
14093cd6e20fSNishka Dasgupta 		}
1410549985eeSRichard Cochran 	}
1411549985eeSRichard Cochran 
14122eb32b0aSMugunthan V N 	return 0;
14133cd6e20fSNishka Dasgupta 
14143cd6e20fSNishka Dasgupta err_node_put:
14153cd6e20fSNishka Dasgupta 	of_node_put(slave_node);
14163cd6e20fSNishka Dasgupta 	return ret;
14172eb32b0aSMugunthan V N }
14182eb32b0aSMugunthan V N 
1419a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev)
1420a4e32b0dSJohan Hovold {
1421bfe59032SIvan Khoronzhuk 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
14228cbcc466SJohan Hovold 	struct cpsw_platform_data *data = &cpsw->data;
14238cbcc466SJohan Hovold 	struct device_node *node = pdev->dev.of_node;
14248cbcc466SJohan Hovold 	struct device_node *slave_node;
14258cbcc466SJohan Hovold 	int i = 0;
14268cbcc466SJohan Hovold 
14278cbcc466SJohan Hovold 	for_each_available_child_of_node(node, slave_node) {
14288cbcc466SJohan Hovold 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
14298cbcc466SJohan Hovold 
1430bf5849f1SRob Herring 		if (!of_node_name_eq(slave_node, "slave"))
14318cbcc466SJohan Hovold 			continue;
14328cbcc466SJohan Hovold 
14333f65047cSJohan Hovold 		if (of_phy_is_fixed_link(slave_node))
14343f65047cSJohan Hovold 			of_phy_deregister_fixed_link(slave_node);
14358cbcc466SJohan Hovold 
14368cbcc466SJohan Hovold 		of_node_put(slave_data->phy_node);
14378cbcc466SJohan Hovold 
14388cbcc466SJohan Hovold 		i++;
14393cd6e20fSNishka Dasgupta 		if (i == data->slaves) {
14403cd6e20fSNishka Dasgupta 			of_node_put(slave_node);
14418cbcc466SJohan Hovold 			break;
14428cbcc466SJohan Hovold 		}
14433cd6e20fSNishka Dasgupta 	}
14448cbcc466SJohan Hovold 
1445a4e32b0dSJohan Hovold 	of_platform_depopulate(&pdev->dev);
1446a4e32b0dSJohan Hovold }
1447a4e32b0dSJohan Hovold 
144856e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
1449d9ba8f9eSMugunthan V N {
1450606f3993SIvan Khoronzhuk 	struct cpsw_common		*cpsw = priv->cpsw;
1451606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	*data = &cpsw->data;
1452d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
1453d9ba8f9eSMugunthan V N 	struct cpsw_priv		*priv_sl2;
1454e38b5a3dSIvan Khoronzhuk 	int ret = 0;
1455d9ba8f9eSMugunthan V N 
1456d183a942SGrygorii Strashko 	ndev = devm_alloc_etherdev_mqs(cpsw->dev, sizeof(struct cpsw_priv),
1457d183a942SGrygorii Strashko 				       CPSW_MAX_QUEUES, CPSW_MAX_QUEUES);
1458d9ba8f9eSMugunthan V N 	if (!ndev) {
145956e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
1460d9ba8f9eSMugunthan V N 		return -ENOMEM;
1461d9ba8f9eSMugunthan V N 	}
1462d9ba8f9eSMugunthan V N 
1463d9ba8f9eSMugunthan V N 	priv_sl2 = netdev_priv(ndev);
1464606f3993SIvan Khoronzhuk 	priv_sl2->cpsw = cpsw;
1465d9ba8f9eSMugunthan V N 	priv_sl2->ndev = ndev;
1466d9ba8f9eSMugunthan V N 	priv_sl2->dev  = &ndev->dev;
1467d9ba8f9eSMugunthan V N 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1468d9ba8f9eSMugunthan V N 
1469d9ba8f9eSMugunthan V N 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
1470d9ba8f9eSMugunthan V N 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
1471d9ba8f9eSMugunthan V N 			ETH_ALEN);
147256e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
147356e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
1474d9ba8f9eSMugunthan V N 	} else {
14756c1f0a1fSJoe Perches 		eth_random_addr(priv_sl2->mac_addr);
147656e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
147756e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
1478d9ba8f9eSMugunthan V N 	}
1479d9ba8f9eSMugunthan V N 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
1480d9ba8f9eSMugunthan V N 
1481d9ba8f9eSMugunthan V N 	priv_sl2->emac_port = 1;
1482606f3993SIvan Khoronzhuk 	cpsw->slaves[1].ndev = ndev;
1483193736c8SIvan Khoronzhuk 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
1484d9ba8f9eSMugunthan V N 
1485d9ba8f9eSMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
14867ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &cpsw_ethtool_ops;
1487d9ba8f9eSMugunthan V N 
1488d9ba8f9eSMugunthan V N 	/* register the network device */
148956e31bd8SIvan Khoronzhuk 	SET_NETDEV_DEV(ndev, cpsw->dev);
1490337d1727SMarek Vasut 	ndev->dev.of_node = cpsw->slaves[1].data->slave_node;
1491d9ba8f9eSMugunthan V N 	ret = register_netdev(ndev);
1492d183a942SGrygorii Strashko 	if (ret)
149356e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
1494d9ba8f9eSMugunthan V N 
1495d9ba8f9eSMugunthan V N 	return ret;
1496d9ba8f9eSMugunthan V N }
1497d9ba8f9eSMugunthan V N 
14987da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = {
14999611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,cpsw"},
15009611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am335x-cpsw"},
15019611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am4372-cpsw"},
15029611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,dra7-cpsw"},
15037da11600SMugunthan V N 	{ /* sentinel */ },
15047da11600SMugunthan V N };
15057da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
15067da11600SMugunthan V N 
15079611d6d6SIvan Khoronzhuk static const struct soc_device_attribute cpsw_soc_devices[] = {
15089611d6d6SIvan Khoronzhuk 	{ .family = "AM33xx", .revision = "ES1.0"},
15099611d6d6SIvan Khoronzhuk 	{ /* sentinel */ }
15109611d6d6SIvan Khoronzhuk };
15119611d6d6SIvan Khoronzhuk 
1512663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev)
1513df828598SMugunthan V N {
1514c8fb5668SGrygorii Strashko 	struct device			*dev = &pdev->dev;
1515ef4183a1SIvan Khoronzhuk 	struct clk			*clk;
1516d1bd9acfSSebastian Siewior 	struct cpsw_platform_data	*data;
1517df828598SMugunthan V N 	struct net_device		*ndev;
1518df828598SMugunthan V N 	struct cpsw_priv		*priv;
1519aa1a15e2SDaniel Mack 	void __iomem			*ss_regs;
1520c8ace62fSYueHaibing 	struct resource			*ss_res;
15211d147ccbSMugunthan V N 	struct gpio_descs		*mode;
15229611d6d6SIvan Khoronzhuk 	const struct soc_device_attribute *soc;
1523649a1688SIvan Khoronzhuk 	struct cpsw_common		*cpsw;
1524e6a84624SGrygorii Strashko 	int ret = 0, ch;
15255087b915SFelipe Balbi 	int irq;
1526df828598SMugunthan V N 
1527c8fb5668SGrygorii Strashko 	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
15283420ea88SJohan Hovold 	if (!cpsw)
15293420ea88SJohan Hovold 		return -ENOMEM;
15303420ea88SJohan Hovold 
15312d683eaaSAntoine Tenart 	platform_set_drvdata(pdev, cpsw);
153251a95337SGrygorii Strashko 	cpsw_slave_index = cpsw_slave_index_priv;
153351a95337SGrygorii Strashko 
1534c8fb5668SGrygorii Strashko 	cpsw->dev = dev;
1535649a1688SIvan Khoronzhuk 
1536c8fb5668SGrygorii Strashko 	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
15371d147ccbSMugunthan V N 	if (IS_ERR(mode)) {
15381d147ccbSMugunthan V N 		ret = PTR_ERR(mode);
1539c8fb5668SGrygorii Strashko 		dev_err(dev, "gpio request failed, ret %d\n", ret);
1540d183a942SGrygorii Strashko 		return ret;
15411d147ccbSMugunthan V N 	}
15421d147ccbSMugunthan V N 
154383a8471bSGrygorii Strashko 	clk = devm_clk_get(dev, "fck");
154483a8471bSGrygorii Strashko 	if (IS_ERR(clk)) {
1545ac97a359SYueHaibing 		ret = PTR_ERR(clk);
154683a8471bSGrygorii Strashko 		dev_err(dev, "fck is not found %d\n", ret);
154783a8471bSGrygorii Strashko 		return ret;
154883a8471bSGrygorii Strashko 	}
154983a8471bSGrygorii Strashko 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
155083a8471bSGrygorii Strashko 
155183a8471bSGrygorii Strashko 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
155283a8471bSGrygorii Strashko 	ss_regs = devm_ioremap_resource(dev, ss_res);
155383a8471bSGrygorii Strashko 	if (IS_ERR(ss_regs))
155483a8471bSGrygorii Strashko 		return PTR_ERR(ss_regs);
155583a8471bSGrygorii Strashko 	cpsw->regs = ss_regs;
155683a8471bSGrygorii Strashko 
1557c8ace62fSYueHaibing 	cpsw->wr_regs = devm_platform_ioremap_resource(pdev, 1);
155883a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->wr_regs))
155983a8471bSGrygorii Strashko 		return PTR_ERR(cpsw->wr_regs);
156083a8471bSGrygorii Strashko 
156183a8471bSGrygorii Strashko 	/* RX IRQ */
156283a8471bSGrygorii Strashko 	irq = platform_get_irq(pdev, 1);
156383a8471bSGrygorii Strashko 	if (irq < 0)
156483a8471bSGrygorii Strashko 		return irq;
156583a8471bSGrygorii Strashko 	cpsw->irqs_table[0] = irq;
156683a8471bSGrygorii Strashko 
156783a8471bSGrygorii Strashko 	/* TX IRQ */
156883a8471bSGrygorii Strashko 	irq = platform_get_irq(pdev, 2);
156983a8471bSGrygorii Strashko 	if (irq < 0)
157083a8471bSGrygorii Strashko 		return irq;
157183a8471bSGrygorii Strashko 	cpsw->irqs_table[1] = irq;
157283a8471bSGrygorii Strashko 
157384ea9c0aSGrygorii Strashko 	/* get misc irq*/
157484ea9c0aSGrygorii Strashko 	irq = platform_get_irq(pdev, 3);
157584ea9c0aSGrygorii Strashko 	if (irq <= 0)
157684ea9c0aSGrygorii Strashko 		return irq;
157784ea9c0aSGrygorii Strashko 	cpsw->misc_irq = irq;
157884ea9c0aSGrygorii Strashko 
15791fb19aa7SVaibhav Hiremath 	/*
15801fb19aa7SVaibhav Hiremath 	 * This may be required here for child devices.
15811fb19aa7SVaibhav Hiremath 	 */
1582c8fb5668SGrygorii Strashko 	pm_runtime_enable(dev);
15831fb19aa7SVaibhav Hiremath 
1584a4e32b0dSJohan Hovold 	/* Need to enable clocks with runtime PM api to access module
1585a4e32b0dSJohan Hovold 	 * registers
1586a4e32b0dSJohan Hovold 	 */
1587c8fb5668SGrygorii Strashko 	ret = pm_runtime_get_sync(dev);
1588a4e32b0dSJohan Hovold 	if (ret < 0) {
1589c8fb5668SGrygorii Strashko 		pm_runtime_put_noidle(dev);
1590aa1a15e2SDaniel Mack 		goto clean_runtime_disable_ret;
15912eb32b0aSMugunthan V N 	}
1592a4e32b0dSJohan Hovold 
159323a09873SJohan Hovold 	ret = cpsw_probe_dt(&cpsw->data, pdev);
159423a09873SJohan Hovold 	if (ret)
1595a4e32b0dSJohan Hovold 		goto clean_dt_ret;
159623a09873SJohan Hovold 
159783a8471bSGrygorii Strashko 	soc = soc_device_match(cpsw_soc_devices);
159883a8471bSGrygorii Strashko 	if (soc)
159983a8471bSGrygorii Strashko 		cpsw->quirk_irq = 1;
160083a8471bSGrygorii Strashko 
1601606f3993SIvan Khoronzhuk 	data = &cpsw->data;
1602c8fb5668SGrygorii Strashko 	cpsw->slaves = devm_kcalloc(dev,
1603a86854d0SKees Cook 				    data->slaves, sizeof(struct cpsw_slave),
1604df828598SMugunthan V N 				    GFP_KERNEL);
1605606f3993SIvan Khoronzhuk 	if (!cpsw->slaves) {
1606aa1a15e2SDaniel Mack 		ret = -ENOMEM;
1607a4e32b0dSJohan Hovold 		goto clean_dt_ret;
1608df828598SMugunthan V N 	}
1609df828598SMugunthan V N 
161083a8471bSGrygorii Strashko 	cpsw->rx_packet_max = max(rx_packet_max, CPSW_MAX_PACKET_SIZE);
1611c24eef28SGrygorii Strashko 	cpsw->descs_pool_size = descs_pool_size;
1612df828598SMugunthan V N 
1613e6a84624SGrygorii Strashko 	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1614e6a84624SGrygorii Strashko 			       ss_res->start + CPSW2_BD_OFFSET,
1615e6a84624SGrygorii Strashko 			       descs_pool_size);
1616e6a84624SGrygorii Strashko 	if (ret)
1617a4e32b0dSJohan Hovold 		goto clean_dt_ret;
16188a2c9a5aSGrygorii Strashko 
161983a8471bSGrygorii Strashko 	ch = cpsw->quirk_irq ? 0 : 7;
162083a8471bSGrygorii Strashko 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
162183a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->txv[0].ch)) {
162283a8471bSGrygorii Strashko 		dev_err(dev, "error initializing tx dma channel\n");
162383a8471bSGrygorii Strashko 		ret = PTR_ERR(cpsw->txv[0].ch);
162483a8471bSGrygorii Strashko 		goto clean_cpts;
1625df828598SMugunthan V N 	}
1626df828598SMugunthan V N 
162783a8471bSGrygorii Strashko 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
162883a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->rxv[0].ch)) {
162983a8471bSGrygorii Strashko 		dev_err(dev, "error initializing rx dma channel\n");
163083a8471bSGrygorii Strashko 		ret = PTR_ERR(cpsw->rxv[0].ch);
163183a8471bSGrygorii Strashko 		goto clean_cpts;
163283a8471bSGrygorii Strashko 	}
163383a8471bSGrygorii Strashko 	cpsw_split_res(cpsw);
163483a8471bSGrygorii Strashko 
163583a8471bSGrygorii Strashko 	/* setup netdev */
163683a8471bSGrygorii Strashko 	ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
163783a8471bSGrygorii Strashko 				       CPSW_MAX_QUEUES, CPSW_MAX_QUEUES);
163883a8471bSGrygorii Strashko 	if (!ndev) {
163983a8471bSGrygorii Strashko 		dev_err(dev, "error allocating net_device\n");
164083a8471bSGrygorii Strashko 		goto clean_cpts;
164183a8471bSGrygorii Strashko 	}
164283a8471bSGrygorii Strashko 
164383a8471bSGrygorii Strashko 	priv = netdev_priv(ndev);
164483a8471bSGrygorii Strashko 	priv->cpsw = cpsw;
164583a8471bSGrygorii Strashko 	priv->ndev = ndev;
164683a8471bSGrygorii Strashko 	priv->dev  = dev;
164783a8471bSGrygorii Strashko 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
164883a8471bSGrygorii Strashko 	priv->emac_port = 0;
164983a8471bSGrygorii Strashko 
165083a8471bSGrygorii Strashko 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
165183a8471bSGrygorii Strashko 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
165283a8471bSGrygorii Strashko 		dev_info(dev, "Detected MACID = %pM\n", priv->mac_addr);
165383a8471bSGrygorii Strashko 	} else {
165483a8471bSGrygorii Strashko 		eth_random_addr(priv->mac_addr);
165583a8471bSGrygorii Strashko 		dev_info(dev, "Random MACID = %pM\n", priv->mac_addr);
165683a8471bSGrygorii Strashko 	}
165783a8471bSGrygorii Strashko 
165883a8471bSGrygorii Strashko 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
165983a8471bSGrygorii Strashko 
166083a8471bSGrygorii Strashko 	cpsw->slaves[0].ndev = ndev;
166183a8471bSGrygorii Strashko 
1662a3a41d2fSGrygorii Strashko 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
1663070f9c65SKeerthy 
1664070f9c65SKeerthy 	ndev->netdev_ops = &cpsw_netdev_ops;
1665070f9c65SKeerthy 	ndev->ethtool_ops = &cpsw_ethtool_ops;
16669611d6d6SIvan Khoronzhuk 	netif_napi_add(ndev, &cpsw->napi_rx,
16679611d6d6SIvan Khoronzhuk 		       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
16689611d6d6SIvan Khoronzhuk 		       CPSW_POLL_WEIGHT);
16699611d6d6SIvan Khoronzhuk 	netif_tx_napi_add(ndev, &cpsw->napi_tx,
16709611d6d6SIvan Khoronzhuk 			  cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
16719611d6d6SIvan Khoronzhuk 			  CPSW_POLL_WEIGHT);
1672070f9c65SKeerthy 
1673070f9c65SKeerthy 	/* register the network device */
1674c8fb5668SGrygorii Strashko 	SET_NETDEV_DEV(ndev, dev);
1675337d1727SMarek Vasut 	ndev->dev.of_node = cpsw->slaves[0].data->slave_node;
1676070f9c65SKeerthy 	ret = register_netdev(ndev);
1677070f9c65SKeerthy 	if (ret) {
1678c8fb5668SGrygorii Strashko 		dev_err(dev, "error registering net device\n");
1679070f9c65SKeerthy 		ret = -ENODEV;
168083a8471bSGrygorii Strashko 		goto clean_cpts;
1681070f9c65SKeerthy 	}
1682070f9c65SKeerthy 
1683070f9c65SKeerthy 	if (cpsw->data.dual_emac) {
1684070f9c65SKeerthy 		ret = cpsw_probe_dual_emac(priv);
1685070f9c65SKeerthy 		if (ret) {
1686070f9c65SKeerthy 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
1687070f9c65SKeerthy 			goto clean_unregister_netdev_ret;
1688070f9c65SKeerthy 		}
1689070f9c65SKeerthy 	}
1690070f9c65SKeerthy 
1691c03abd84SFelipe Balbi 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1692c03abd84SFelipe Balbi 	 * MISC IRQs which are always kept disabled with this driver so
1693c03abd84SFelipe Balbi 	 * we will not request them.
1694c03abd84SFelipe Balbi 	 *
1695c03abd84SFelipe Balbi 	 * If anyone wants to implement support for those, make sure to
1696c03abd84SFelipe Balbi 	 * first request and append them to irqs_table array.
1697c03abd84SFelipe Balbi 	 */
169883a8471bSGrygorii Strashko 	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1699c8fb5668SGrygorii Strashko 			       0, dev_name(dev), cpsw);
17005087b915SFelipe Balbi 	if (ret < 0) {
1701c8fb5668SGrygorii Strashko 		dev_err(dev, "error attaching irq (%d)\n", ret);
170283a8471bSGrygorii Strashko 		goto clean_unregister_netdev_ret;
1703df828598SMugunthan V N 	}
1704df828598SMugunthan V N 
17055087b915SFelipe Balbi 
170683a8471bSGrygorii Strashko 	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1707dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
17085087b915SFelipe Balbi 	if (ret < 0) {
1709c8fb5668SGrygorii Strashko 		dev_err(dev, "error attaching irq (%d)\n", ret);
171083a8471bSGrygorii Strashko 		goto clean_unregister_netdev_ret;
17115087b915SFelipe Balbi 	}
1712c2b32e58SDaniel Mack 
171384ea9c0aSGrygorii Strashko 	if (!cpsw->cpts)
171484ea9c0aSGrygorii Strashko 		goto skip_cpts;
171584ea9c0aSGrygorii Strashko 
171684ea9c0aSGrygorii Strashko 	ret = devm_request_irq(&pdev->dev, cpsw->misc_irq, cpsw_misc_interrupt,
171784ea9c0aSGrygorii Strashko 			       0, dev_name(&pdev->dev), cpsw);
171884ea9c0aSGrygorii Strashko 	if (ret < 0) {
171984ea9c0aSGrygorii Strashko 		dev_err(dev, "error attaching misc irq (%d)\n", ret);
172084ea9c0aSGrygorii Strashko 		goto clean_unregister_netdev_ret;
172184ea9c0aSGrygorii Strashko 	}
172284ea9c0aSGrygorii Strashko 
172384ea9c0aSGrygorii Strashko 	/* Enable misc CPTS evnt_pend IRQ */
172484ea9c0aSGrygorii Strashko 	cpts_set_irqpoll(cpsw->cpts, false);
172584ea9c0aSGrygorii Strashko 	writel(0x10, &cpsw->wr_regs->misc_en);
172684ea9c0aSGrygorii Strashko 
172784ea9c0aSGrygorii Strashko skip_cpts:
172890225bf0SGrygorii Strashko 	cpsw_notice(priv, probe,
172990225bf0SGrygorii Strashko 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
173083a8471bSGrygorii Strashko 		    &ss_res->start, cpsw->irqs_table[0], descs_pool_size);
1731d9ba8f9eSMugunthan V N 
1732c46ab7e0SJohan Hovold 	pm_runtime_put(&pdev->dev);
1733c46ab7e0SJohan Hovold 
1734df828598SMugunthan V N 	return 0;
1735df828598SMugunthan V N 
1736a7fe9d46SJohan Hovold clean_unregister_netdev_ret:
1737a7fe9d46SJohan Hovold 	unregister_netdev(ndev);
173883a8471bSGrygorii Strashko clean_cpts:
173983a8471bSGrygorii Strashko 	cpts_release(cpsw->cpts);
17402c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
1741a4e32b0dSJohan Hovold clean_dt_ret:
1742a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
1743c46ab7e0SJohan Hovold 	pm_runtime_put_sync(&pdev->dev);
1744aa1a15e2SDaniel Mack clean_runtime_disable_ret:
1745f150bd7fSMugunthan V N 	pm_runtime_disable(&pdev->dev);
1746df828598SMugunthan V N 	return ret;
1747df828598SMugunthan V N }
1748df828598SMugunthan V N 
1749663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev)
1750df828598SMugunthan V N {
1751bfe59032SIvan Khoronzhuk 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
1752bfe59032SIvan Khoronzhuk 	int i, ret;
17538a0b6dc9SGrygorii Strashko 
17548a0b6dc9SGrygorii Strashko 	ret = pm_runtime_get_sync(&pdev->dev);
17558a0b6dc9SGrygorii Strashko 	if (ret < 0) {
17568a0b6dc9SGrygorii Strashko 		pm_runtime_put_noidle(&pdev->dev);
17578a0b6dc9SGrygorii Strashko 		return ret;
17588a0b6dc9SGrygorii Strashko 	}
1759df828598SMugunthan V N 
1760bfe59032SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
1761bfe59032SIvan Khoronzhuk 		if (cpsw->slaves[i].ndev)
1762bfe59032SIvan Khoronzhuk 			unregister_netdev(cpsw->slaves[i].ndev);
1763df828598SMugunthan V N 
17648a2c9a5aSGrygorii Strashko 	cpts_release(cpsw->cpts);
17652c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
1766a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
17678a0b6dc9SGrygorii Strashko 	pm_runtime_put_sync(&pdev->dev);
17688a0b6dc9SGrygorii Strashko 	pm_runtime_disable(&pdev->dev);
1769df828598SMugunthan V N 	return 0;
1770df828598SMugunthan V N }
1771df828598SMugunthan V N 
17728963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP
1773df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
1774df828598SMugunthan V N {
17752f9b0d93SKeerthy 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
1776618073e3SMugunthan V N 	int i;
1777618073e3SMugunthan V N 
17784c64b83dSGrygorii Strashko 	rtnl_lock();
17794c64b83dSGrygorii Strashko 
17802f9b0d93SKeerthy 	for (i = 0; i < cpsw->data.slaves; i++)
17812f9b0d93SKeerthy 		if (cpsw->slaves[i].ndev)
1782606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
1783606f3993SIvan Khoronzhuk 				cpsw_ndo_stop(cpsw->slaves[i].ndev);
17841e7a2e21SDaniel Mack 
17854c64b83dSGrygorii Strashko 	rtnl_unlock();
17864c64b83dSGrygorii Strashko 
1787739683b4SMugunthan V N 	/* Select sleep pin state */
178856e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_sleep_state(dev);
1789739683b4SMugunthan V N 
1790df828598SMugunthan V N 	return 0;
1791df828598SMugunthan V N }
1792df828598SMugunthan V N 
1793df828598SMugunthan V N static int cpsw_resume(struct device *dev)
1794df828598SMugunthan V N {
17952f9b0d93SKeerthy 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
17962f9b0d93SKeerthy 	int i;
1797df828598SMugunthan V N 
1798739683b4SMugunthan V N 	/* Select default pin state */
179956e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_default_state(dev);
1800739683b4SMugunthan V N 
18014ccfd638SGrygorii Strashko 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
18024ccfd638SGrygorii Strashko 	rtnl_lock();
1803618073e3SMugunthan V N 
18042f9b0d93SKeerthy 	for (i = 0; i < cpsw->data.slaves; i++)
18052f9b0d93SKeerthy 		if (cpsw->slaves[i].ndev)
1806606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
1807606f3993SIvan Khoronzhuk 				cpsw_ndo_open(cpsw->slaves[i].ndev);
18082f9b0d93SKeerthy 
18094ccfd638SGrygorii Strashko 	rtnl_unlock();
18104ccfd638SGrygorii Strashko 
1811df828598SMugunthan V N 	return 0;
1812df828598SMugunthan V N }
18138963a504SGrygorii Strashko #endif
1814df828598SMugunthan V N 
18158963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
1816df828598SMugunthan V N 
1817df828598SMugunthan V N static struct platform_driver cpsw_driver = {
1818df828598SMugunthan V N 	.driver = {
1819df828598SMugunthan V N 		.name	 = "cpsw",
1820df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
18211e5c76d4SSachin Kamat 		.of_match_table = cpsw_of_mtable,
1822df828598SMugunthan V N 	},
1823df828598SMugunthan V N 	.probe = cpsw_probe,
1824663e12e6SBill Pemberton 	.remove = cpsw_remove,
1825df828598SMugunthan V N };
1826df828598SMugunthan V N 
18276fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver);
1828df828598SMugunthan V N 
1829df828598SMugunthan V N MODULE_LICENSE("GPL");
1830df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1831df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1832df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
1833