xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision c5013ac1)
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];
4099ed4050cSIvan Khoronzhuk 
410c5013ac1SGrygorii Strashko 		port = priv->emac_port + cpsw->data.dual_emac;
411c5013ac1SGrygorii Strashko 		ret = cpsw_run_xdp(priv, ch, &xdp, page, port);
4129ed4050cSIvan Khoronzhuk 		if (ret != CPSW_XDP_PASS)
4139ed4050cSIvan Khoronzhuk 			goto requeue;
4149ed4050cSIvan Khoronzhuk 
4159ed4050cSIvan Khoronzhuk 		/* XDP prog might have changed packet data and boundaries */
4169ed4050cSIvan Khoronzhuk 		len = xdp.data_end - xdp.data;
4179ed4050cSIvan Khoronzhuk 		headroom = xdp.data - xdp.data_hard_start;
4189ed4050cSIvan Khoronzhuk 
4199ed4050cSIvan Khoronzhuk 		/* XDP prog can modify vlan tag, so can't use encap header */
4209ed4050cSIvan Khoronzhuk 		status &= ~CPDMA_RX_VLAN_ENCAP;
4219ed4050cSIvan Khoronzhuk 	}
4229ed4050cSIvan Khoronzhuk 
4239ed4050cSIvan Khoronzhuk 	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
4249ed4050cSIvan Khoronzhuk 	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
4259ed4050cSIvan Khoronzhuk 	if (!skb) {
4269ed4050cSIvan Khoronzhuk 		ndev->stats.rx_dropped++;
4279ed4050cSIvan Khoronzhuk 		page_pool_recycle_direct(pool, page);
4289ed4050cSIvan Khoronzhuk 		goto requeue;
4299ed4050cSIvan Khoronzhuk 	}
4309ed4050cSIvan Khoronzhuk 
4319ed4050cSIvan Khoronzhuk 	skb_reserve(skb, headroom);
432df828598SMugunthan V N 	skb_put(skb, len);
4339ed4050cSIvan Khoronzhuk 	skb->dev = ndev;
434a3a41d2fSGrygorii Strashko 	if (status & CPDMA_RX_VLAN_ENCAP)
435a3a41d2fSGrygorii Strashko 		cpsw_rx_vlan_encap(skb);
436a9423120SIvan Khoronzhuk 	if (priv->rx_ts_enabled)
4372a05a622SIvan Khoronzhuk 		cpts_rx_timestamp(cpsw->cpts, skb);
438df828598SMugunthan V N 	skb->protocol = eth_type_trans(skb, ndev);
4399ed4050cSIvan Khoronzhuk 
4409ed4050cSIvan Khoronzhuk 	/* unmap page as no netstack skb page recycling */
4419ed4050cSIvan Khoronzhuk 	page_pool_release_page(pool, page);
442df828598SMugunthan V N 	netif_receive_skb(skb);
4439ed4050cSIvan Khoronzhuk 
4448dc43ddcSTobias Klauser 	ndev->stats.rx_bytes += len;
4458dc43ddcSTobias Klauser 	ndev->stats.rx_packets++;
446df828598SMugunthan V N 
447a0e2c822SMugunthan V N requeue:
4489ed4050cSIvan Khoronzhuk 	xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
4499ed4050cSIvan Khoronzhuk 	xmeta->ndev = ndev;
4509ed4050cSIvan Khoronzhuk 	xmeta->ch = ch;
4519ed4050cSIvan Khoronzhuk 
4529ed4050cSIvan Khoronzhuk 	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
4539ed4050cSIvan Khoronzhuk 	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
4549ed4050cSIvan Khoronzhuk 				       pkt_size, 0);
455871e8465SIvan Khoronzhuk 	if (ret < 0) {
456871e8465SIvan Khoronzhuk 		WARN_ON(ret == -ENOMEM);
4579ed4050cSIvan Khoronzhuk 		page_pool_recycle_direct(pool, new_page);
458df828598SMugunthan V N 	}
459871e8465SIvan Khoronzhuk }
460df828598SMugunthan V N 
461df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
462df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
463df828598SMugunthan V N {
464df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
465df828598SMugunthan V N 	u32			mac_control = 0;
466df828598SMugunthan V N 	u32			slave_port;
467606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
468df828598SMugunthan V N 
469df828598SMugunthan V N 	if (!phy)
470df828598SMugunthan V N 		return;
471df828598SMugunthan V N 
4726f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
473df828598SMugunthan V N 
474df828598SMugunthan V N 	if (phy->link) {
475cfc08345SGrygorii Strashko 		mac_control = CPSW_SL_CTL_GMII_EN;
476cfc08345SGrygorii Strashko 
477cfc08345SGrygorii Strashko 		if (phy->speed == 1000)
478cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_GIG;
479cfc08345SGrygorii Strashko 		if (phy->duplex)
480cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_FULLDUPLEX;
481cfc08345SGrygorii Strashko 
482cfc08345SGrygorii Strashko 		/* set speed_in input in case RMII mode is used in 100Mbps */
483cfc08345SGrygorii Strashko 		if (phy->speed == 100)
484cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_IFCTL_A;
485cfc08345SGrygorii Strashko 		/* in band mode only works in 10Mbps RGMII mode */
486cfc08345SGrygorii Strashko 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
487cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
488cfc08345SGrygorii Strashko 
489cfc08345SGrygorii Strashko 		if (priv->rx_pause)
490cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
491cfc08345SGrygorii Strashko 
492cfc08345SGrygorii Strashko 		if (priv->tx_pause)
493cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
494cfc08345SGrygorii Strashko 
495cfc08345SGrygorii Strashko 		if (mac_control != slave->mac_control)
496cfc08345SGrygorii Strashko 			cpsw_sl_ctl_set(slave->mac_sl, mac_control);
497df828598SMugunthan V N 
498df828598SMugunthan V N 		/* enable forwarding */
4992a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
500df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
501df828598SMugunthan V N 
502df828598SMugunthan V N 		*link = true;
50357d90148SIvan Khoronzhuk 
50457d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
50557d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed &&
50657d90148SIvan Khoronzhuk 		    !cpsw_shp_is_off(priv))
50757d90148SIvan Khoronzhuk 			dev_warn(priv->dev,
50857d90148SIvan Khoronzhuk 				 "Speed was changed, CBS shaper speeds are changed!");
509df828598SMugunthan V N 	} else {
510df828598SMugunthan V N 		mac_control = 0;
511df828598SMugunthan V N 		/* disable forwarding */
5122a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
513df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
514cfc08345SGrygorii Strashko 
515cfc08345SGrygorii Strashko 		cpsw_sl_wait_for_idle(slave->mac_sl, 100);
516cfc08345SGrygorii Strashko 
517cfc08345SGrygorii Strashko 		cpsw_sl_ctl_reset(slave->mac_sl);
518df828598SMugunthan V N 	}
519df828598SMugunthan V N 
520cfc08345SGrygorii Strashko 	if (mac_control != slave->mac_control)
521df828598SMugunthan V N 		phy_print_status(phy);
522df828598SMugunthan V N 
523df828598SMugunthan V N 	slave->mac_control = mac_control;
524df828598SMugunthan V N }
525df828598SMugunthan V N 
526df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
527df828598SMugunthan V N {
528df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
5290be01b8eSIvan Khoronzhuk 	struct cpsw_common	*cpsw = priv->cpsw;
530df828598SMugunthan V N 	bool			link = false;
531df828598SMugunthan V N 
532df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
533df828598SMugunthan V N 
534df828598SMugunthan V N 	if (link) {
5350be01b8eSIvan Khoronzhuk 		if (cpsw_need_resplit(cpsw))
5369763a891SGrygorii Strashko 			cpsw_split_res(cpsw);
5370be01b8eSIvan Khoronzhuk 
538df828598SMugunthan V N 		netif_carrier_on(ndev);
539df828598SMugunthan V N 		if (netif_running(ndev))
540e05107e6SIvan Khoronzhuk 			netif_tx_wake_all_queues(ndev);
541df828598SMugunthan V N 	} else {
542df828598SMugunthan V N 		netif_carrier_off(ndev);
543e05107e6SIvan Khoronzhuk 		netif_tx_stop_all_queues(ndev);
544df828598SMugunthan V N 	}
545df828598SMugunthan V N }
546df828598SMugunthan V N 
547d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries(
548d9ba8f9eSMugunthan V N 		struct cpsw_priv *priv, struct cpsw_slave *slave,
549d9ba8f9eSMugunthan V N 		u32 slave_port)
550d9ba8f9eSMugunthan V N {
5512a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
55271a2cbb7SGrygorii Strashko 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
553d9ba8f9eSMugunthan V N 
5542a05a622SIvan Khoronzhuk 	if (cpsw->version == CPSW_VERSION_1)
555d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
556d9ba8f9eSMugunthan V N 	else
557d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
5582a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
559d9ba8f9eSMugunthan V N 			  port_mask, port_mask, 0);
5602a05a622SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
5615b3a5a14SIvan Khoronzhuk 			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0);
5622a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
5632a05a622SIvan Khoronzhuk 			   HOST_PORT_NUM, ALE_VLAN |
5642a05a622SIvan Khoronzhuk 			   ALE_SECURE, slave->port_vlan);
5655e5add17SGrygorii Strashko 	cpsw_ale_control_set(cpsw->ale, slave_port,
5665e5add17SGrygorii Strashko 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
567d9ba8f9eSMugunthan V N }
568d9ba8f9eSMugunthan V N 
5691e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
5701e7a2e21SDaniel Mack {
571df828598SMugunthan V N 	u32 slave_port;
57230c57f07SSekhar Nori 	struct phy_device *phy;
573649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
574df828598SMugunthan V N 
575cfc08345SGrygorii Strashko 	cpsw_sl_reset(slave->mac_sl, 100);
576cfc08345SGrygorii Strashko 	cpsw_sl_ctl_reset(slave->mac_sl);
577df828598SMugunthan V N 
578df828598SMugunthan V N 	/* setup priority mapping */
579cfc08345SGrygorii Strashko 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
580cfc08345SGrygorii Strashko 			  RX_PRIORITY_MAPPING);
5819750a3adSRichard Cochran 
5822a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
5839750a3adSRichard Cochran 	case CPSW_VERSION_1:
5849750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
58548f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
58648f5bcccSGrygorii Strashko 		 * flow control mode
58748f5bcccSGrygorii Strashko 		 */
58848f5bcccSGrygorii Strashko 		slave_write(slave,
58948f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
59048f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
5919750a3adSRichard Cochran 		break;
5929750a3adSRichard Cochran 	case CPSW_VERSION_2:
593c193f365SMugunthan V N 	case CPSW_VERSION_3:
594926489beSMugunthan V N 	case CPSW_VERSION_4:
5959750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
59648f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
59748f5bcccSGrygorii Strashko 		 * flow control mode
59848f5bcccSGrygorii Strashko 		 */
59948f5bcccSGrygorii Strashko 		slave_write(slave,
60048f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
60148f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
6029750a3adSRichard Cochran 		break;
6039750a3adSRichard Cochran 	}
604df828598SMugunthan V N 
605df828598SMugunthan V N 	/* setup max packet size, and mac address */
606cfc08345SGrygorii Strashko 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
607cfc08345SGrygorii Strashko 			  cpsw->rx_packet_max);
608df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
609df828598SMugunthan V N 
610df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
611df828598SMugunthan V N 
6126f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
613df828598SMugunthan V N 
614606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
615d9ba8f9eSMugunthan V N 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
616d9ba8f9eSMugunthan V N 	else
6172a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
618e11b220fSMugunthan V N 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
619df828598SMugunthan V N 
620d733f754SDavid Rivshin 	if (slave->data->phy_node) {
62130c57f07SSekhar Nori 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
6229e42f715SHeiko Schocher 				 &cpsw_adjust_link, 0, slave->data->phy_if);
62330c57f07SSekhar Nori 		if (!phy) {
624f7ce9103SRob Herring 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
625f7ce9103SRob Herring 				slave->data->phy_node,
626d733f754SDavid Rivshin 				slave->slave_num);
627d733f754SDavid Rivshin 			return;
628d733f754SDavid Rivshin 		}
629d733f754SDavid Rivshin 	} else {
63030c57f07SSekhar Nori 		phy = phy_connect(priv->ndev, slave->data->phy_id,
631f9a8f83bSFlorian Fainelli 				 &cpsw_adjust_link, slave->data->phy_if);
63230c57f07SSekhar Nori 		if (IS_ERR(phy)) {
633d733f754SDavid Rivshin 			dev_err(priv->dev,
634d733f754SDavid Rivshin 				"phy \"%s\" not found on slave %d, err %ld\n",
635d733f754SDavid Rivshin 				slave->data->phy_id, slave->slave_num,
63630c57f07SSekhar Nori 				PTR_ERR(phy));
637d733f754SDavid Rivshin 			return;
638d733f754SDavid Rivshin 		}
639d733f754SDavid Rivshin 	}
640d733f754SDavid Rivshin 
64130c57f07SSekhar Nori 	slave->phy = phy;
64230c57f07SSekhar Nori 
6432220943aSAndrew Lunn 	phy_attached_info(slave->phy);
6442220943aSAndrew Lunn 
645df828598SMugunthan V N 	phy_start(slave->phy);
646388367a5SMugunthan V N 
647388367a5SMugunthan V N 	/* Configure GMII_SEL register */
6483ff18849SGrygorii Strashko 	if (!IS_ERR(slave->data->ifphy))
6493ff18849SGrygorii Strashko 		phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
6503ff18849SGrygorii Strashko 				 slave->data->phy_if);
6513ff18849SGrygorii Strashko 	else
6523ff18849SGrygorii Strashko 		cpsw_phy_sel(cpsw->dev, slave->phy->interface,
6533ff18849SGrygorii Strashko 			     slave->slave_num);
654df828598SMugunthan V N }
655df828598SMugunthan V N 
6563b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
6573b72c2feSMugunthan V N {
658606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
659606f3993SIvan Khoronzhuk 	const int vlan = cpsw->data.default_vlan;
6603b72c2feSMugunthan V N 	u32 reg;
6613b72c2feSMugunthan V N 	int i;
6621e5c4bc4SLennart Sorensen 	int unreg_mcast_mask;
6633b72c2feSMugunthan V N 
6642a05a622SIvan Khoronzhuk 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
6653b72c2feSMugunthan V N 	       CPSW2_PORT_VLAN;
6663b72c2feSMugunthan V N 
6675d8d0d4dSIvan Khoronzhuk 	writel(vlan, &cpsw->host_port_regs->port_vlan);
6683b72c2feSMugunthan V N 
669606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
670606f3993SIvan Khoronzhuk 		slave_write(cpsw->slaves + i, vlan, reg);
6713b72c2feSMugunthan V N 
6721e5c4bc4SLennart Sorensen 	if (priv->ndev->flags & IFF_ALLMULTI)
6731e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_ALL_PORTS;
6741e5c4bc4SLennart Sorensen 	else
6751e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
6761e5c4bc4SLennart Sorensen 
6772a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
67861f1cef9SGrygorii Strashko 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
67961f1cef9SGrygorii Strashko 			  unreg_mcast_mask);
6803b72c2feSMugunthan V N }
6813b72c2feSMugunthan V N 
682df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
683df828598SMugunthan V N {
684d9ba8f9eSMugunthan V N 	u32 fifo_mode;
6855d8d0d4dSIvan Khoronzhuk 	u32 control_reg;
6865d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
6873b72c2feSMugunthan V N 
688df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
6895d8d0d4dSIvan Khoronzhuk 	soft_reset("cpsw", &cpsw->regs->soft_reset);
6902a05a622SIvan Khoronzhuk 	cpsw_ale_start(cpsw->ale);
691df828598SMugunthan V N 
692df828598SMugunthan V N 	/* switch to vlan unaware mode */
6932a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
6943b72c2feSMugunthan V N 			     CPSW_ALE_VLAN_AWARE);
6955d8d0d4dSIvan Khoronzhuk 	control_reg = readl(&cpsw->regs->control);
696a3a41d2fSGrygorii Strashko 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
6975d8d0d4dSIvan Khoronzhuk 	writel(control_reg, &cpsw->regs->control);
698606f3993SIvan Khoronzhuk 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
699d9ba8f9eSMugunthan V N 		     CPSW_FIFO_NORMAL_MODE;
7005d8d0d4dSIvan Khoronzhuk 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
701df828598SMugunthan V N 
702df828598SMugunthan V N 	/* setup host port priority mapping */
703dda5f5feSGrygorii Strashko 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
7045d8d0d4dSIvan Khoronzhuk 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
705dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
706df828598SMugunthan V N 
7072a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
708df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
709df828598SMugunthan V N 
710606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac) {
7112a05a622SIvan Khoronzhuk 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
712d9ba8f9eSMugunthan V N 				   0, 0);
7132a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
71471a2cbb7SGrygorii Strashko 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
715df828598SMugunthan V N 	}
716d9ba8f9eSMugunthan V N }
717df828598SMugunthan V N 
7182a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
719aacebbf8SSebastian Siewior {
7203995d265SSchuyler Patton 	u32 slave_port;
7213995d265SSchuyler Patton 
7226f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
7233995d265SSchuyler Patton 
724aacebbf8SSebastian Siewior 	if (!slave->phy)
725aacebbf8SSebastian Siewior 		return;
726aacebbf8SSebastian Siewior 	phy_stop(slave->phy);
727aacebbf8SSebastian Siewior 	phy_disconnect(slave->phy);
728aacebbf8SSebastian Siewior 	slave->phy = NULL;
7292a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, slave_port,
7303995d265SSchuyler Patton 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
731cfc08345SGrygorii Strashko 	cpsw_sl_reset(slave->mac_sl, 100);
732cfc08345SGrygorii Strashko 	cpsw_sl_ctl_reset(slave->mac_sl);
733aacebbf8SSebastian Siewior }
734aacebbf8SSebastian Siewior 
73500fe4712SIvan Khoronzhuk static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
73600fe4712SIvan Khoronzhuk {
73700fe4712SIvan Khoronzhuk 	struct cpsw_priv *priv = arg;
73800fe4712SIvan Khoronzhuk 
73900fe4712SIvan Khoronzhuk 	if (!vdev)
74000fe4712SIvan Khoronzhuk 		return 0;
74100fe4712SIvan Khoronzhuk 
74200fe4712SIvan Khoronzhuk 	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
74300fe4712SIvan Khoronzhuk 	return 0;
74400fe4712SIvan Khoronzhuk }
74500fe4712SIvan Khoronzhuk 
7464b4255edSIvan Khoronzhuk /* restore resources after port reset */
7474b4255edSIvan Khoronzhuk static void cpsw_restore(struct cpsw_priv *priv)
7484b4255edSIvan Khoronzhuk {
74900fe4712SIvan Khoronzhuk 	/* restore vlan configurations */
75000fe4712SIvan Khoronzhuk 	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
75100fe4712SIvan Khoronzhuk 
7524b4255edSIvan Khoronzhuk 	/* restore MQPRIO offload */
7534b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_mqprio_resume, priv);
7544b4255edSIvan Khoronzhuk 
7554b4255edSIvan Khoronzhuk 	/* restore CBS offload */
7564b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_cbs_resume, priv);
7574b4255edSIvan Khoronzhuk }
7584b4255edSIvan Khoronzhuk 
759df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
760df828598SMugunthan V N {
761df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
762649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
7633802dce1SIvan Khoronzhuk 	int ret;
764df828598SMugunthan V N 	u32 reg;
765df828598SMugunthan V N 
76656e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
767108a6537SGrygorii Strashko 	if (ret < 0) {
76856e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
769108a6537SGrygorii Strashko 		return ret;
770108a6537SGrygorii Strashko 	}
7713fa88c51SGrygorii Strashko 
772df828598SMugunthan V N 	netif_carrier_off(ndev);
773df828598SMugunthan V N 
774e05107e6SIvan Khoronzhuk 	/* Notify the stack of the actual queue counts. */
775e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
776e05107e6SIvan Khoronzhuk 	if (ret) {
777e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of tx queues\n");
778e05107e6SIvan Khoronzhuk 		goto err_cleanup;
779e05107e6SIvan Khoronzhuk 	}
780e05107e6SIvan Khoronzhuk 
781e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
782e05107e6SIvan Khoronzhuk 	if (ret) {
783e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of rx queues\n");
784e05107e6SIvan Khoronzhuk 		goto err_cleanup;
785e05107e6SIvan Khoronzhuk 	}
786e05107e6SIvan Khoronzhuk 
7872a05a622SIvan Khoronzhuk 	reg = cpsw->version;
788df828598SMugunthan V N 
789df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
790df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
791df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
792df828598SMugunthan V N 
793d5bc1613SIvan Khoronzhuk 	/* Initialize host and slave ports */
794d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count)
795df828598SMugunthan V N 		cpsw_init_host_port(priv);
796df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
797df828598SMugunthan V N 
7983b72c2feSMugunthan V N 	/* Add default VLAN */
799606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac)
8003b72c2feSMugunthan V N 		cpsw_add_default_vlan(priv);
801e6afea0bSMugunthan V N 	else
8022a05a622SIvan Khoronzhuk 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
80361f1cef9SGrygorii Strashko 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
8043b72c2feSMugunthan V N 
805d5bc1613SIvan Khoronzhuk 	/* initialize shared resources for every ndev */
806d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count) {
807d9ba8f9eSMugunthan V N 		/* disable priority elevation */
808dda5f5feSGrygorii Strashko 		writel_relaxed(0, &cpsw->regs->ptype);
809df828598SMugunthan V N 
810d9ba8f9eSMugunthan V N 		/* enable statistics collection only on all ports */
811dda5f5feSGrygorii Strashko 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
812df828598SMugunthan V N 
8131923d6e4SMugunthan V N 		/* Enable internal fifo flow control */
8145d8d0d4dSIvan Khoronzhuk 		writel(0x7, &cpsw->regs->flow_control);
8151923d6e4SMugunthan V N 
816dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_rx);
817dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_tx);
818d354eb85SMugunthan V N 
819e38b5a3dSIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
820e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
821e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
8227da11600SMugunthan V N 		}
8237da11600SMugunthan V N 
824e38b5a3dSIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
825e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
826e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
8277da11600SMugunthan V N 		}
8287da11600SMugunthan V N 
8299ed4050cSIvan Khoronzhuk 		/* create rxqs for both infs in dual mac as they use same pool
8309ed4050cSIvan Khoronzhuk 		 * and must be destroyed together when no users.
8319ed4050cSIvan Khoronzhuk 		 */
8329ed4050cSIvan Khoronzhuk 		ret = cpsw_create_xdp_rxqs(cpsw);
8339ed4050cSIvan Khoronzhuk 		if (ret < 0)
8349ed4050cSIvan Khoronzhuk 			goto err_cleanup;
8359ed4050cSIvan Khoronzhuk 
8363802dce1SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
8373802dce1SIvan Khoronzhuk 		if (ret < 0)
838aacebbf8SSebastian Siewior 			goto err_cleanup;
839f280e89aSMugunthan V N 
8408a2c9a5aSGrygorii Strashko 		if (cpts_register(cpsw->cpts))
841f280e89aSMugunthan V N 			dev_err(priv->dev, "error registering cpts device\n");
842f280e89aSMugunthan V N 
843d9ba8f9eSMugunthan V N 	}
844df828598SMugunthan V N 
8454b4255edSIvan Khoronzhuk 	cpsw_restore(priv);
8464b4255edSIvan Khoronzhuk 
847ff5b8ef2SMugunthan V N 	/* Enable Interrupt pacing if configured */
8482a05a622SIvan Khoronzhuk 	if (cpsw->coal_intvl != 0) {
849ff5b8ef2SMugunthan V N 		struct ethtool_coalesce coal;
850ff5b8ef2SMugunthan V N 
8512a05a622SIvan Khoronzhuk 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
852ff5b8ef2SMugunthan V N 		cpsw_set_coalesce(ndev, &coal);
853ff5b8ef2SMugunthan V N 	}
854ff5b8ef2SMugunthan V N 
8552c836bd9SIvan Khoronzhuk 	cpdma_ctlr_start(cpsw->dma);
8562c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
857d5bc1613SIvan Khoronzhuk 	cpsw->usage_count++;
858f63a975eSMugunthan V N 
859df828598SMugunthan V N 	return 0;
860df828598SMugunthan V N 
861aacebbf8SSebastian Siewior err_cleanup:
86202cacedeSIvan Khoronzhuk 	if (!cpsw->usage_count) {
8632c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
8649ed4050cSIvan Khoronzhuk 		cpsw_destroy_xdp_rxqs(cpsw);
86502cacedeSIvan Khoronzhuk 	}
86602cacedeSIvan Khoronzhuk 
8679ed4050cSIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
86856e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
869aacebbf8SSebastian Siewior 	netif_carrier_off(priv->ndev);
870aacebbf8SSebastian Siewior 	return ret;
871df828598SMugunthan V N }
872df828598SMugunthan V N 
873df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
874df828598SMugunthan V N {
875df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
876649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
877df828598SMugunthan V N 
878df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
87915180ecaSIvan Khoronzhuk 	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
880e05107e6SIvan Khoronzhuk 	netif_tx_stop_all_queues(priv->ndev);
881df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
882d9ba8f9eSMugunthan V N 
883d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count <= 1) {
884dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_rx);
885dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_tx);
8862a05a622SIvan Khoronzhuk 		cpts_unregister(cpsw->cpts);
8872c836bd9SIvan Khoronzhuk 		cpsw_intr_disable(cpsw);
8882c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
8892a05a622SIvan Khoronzhuk 		cpsw_ale_stop(cpsw->ale);
8909ed4050cSIvan Khoronzhuk 		cpsw_destroy_xdp_rxqs(cpsw);
891d9ba8f9eSMugunthan V N 	}
8922a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
8930be01b8eSIvan Khoronzhuk 
8940be01b8eSIvan Khoronzhuk 	if (cpsw_need_resplit(cpsw))
8959763a891SGrygorii Strashko 		cpsw_split_res(cpsw);
8960be01b8eSIvan Khoronzhuk 
897d5bc1613SIvan Khoronzhuk 	cpsw->usage_count--;
89856e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
899df828598SMugunthan V N 	return 0;
900df828598SMugunthan V N }
901df828598SMugunthan V N 
902df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
903df828598SMugunthan V N 				       struct net_device *ndev)
904df828598SMugunthan V N {
905df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
9062c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
907f44f8417SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
908e05107e6SIvan Khoronzhuk 	struct netdev_queue *txq;
909e05107e6SIvan Khoronzhuk 	struct cpdma_chan *txch;
910e05107e6SIvan Khoronzhuk 	int ret, q_idx;
911df828598SMugunthan V N 
912df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
913df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
9148dc43ddcSTobias Klauser 		ndev->stats.tx_dropped++;
9151bf96050SIvan Khoronzhuk 		return NET_XMIT_DROP;
916df828598SMugunthan V N 	}
917df828598SMugunthan V N 
9189232b16dSMugunthan V N 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
919a9423120SIvan Khoronzhuk 	    priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
9202e5b38abSRichard Cochran 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
9212e5b38abSRichard Cochran 
922e05107e6SIvan Khoronzhuk 	q_idx = skb_get_queue_mapping(skb);
923e05107e6SIvan Khoronzhuk 	if (q_idx >= cpsw->tx_ch_num)
924e05107e6SIvan Khoronzhuk 		q_idx = q_idx % cpsw->tx_ch_num;
925e05107e6SIvan Khoronzhuk 
9268feb0a19SIvan Khoronzhuk 	txch = cpsw->txv[q_idx].ch;
92762f94c21SGrygorii Strashko 	txq = netdev_get_tx_queue(ndev, q_idx);
92810ae8054SGrygorii Strashko 	skb_tx_timestamp(skb);
92910ae8054SGrygorii Strashko 	ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
93010ae8054SGrygorii Strashko 				priv->emac_port + cpsw->data.dual_emac);
931df828598SMugunthan V N 	if (unlikely(ret != 0)) {
932df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
933df828598SMugunthan V N 		goto fail;
934df828598SMugunthan V N 	}
935df828598SMugunthan V N 
936fae50823SMugunthan V N 	/* If there is no more tx desc left free then we need to
937fae50823SMugunthan V N 	 * tell the kernel to stop sending us tx frames.
938fae50823SMugunthan V N 	 */
939e05107e6SIvan Khoronzhuk 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
940e05107e6SIvan Khoronzhuk 		netif_tx_stop_queue(txq);
94162f94c21SGrygorii Strashko 
94262f94c21SGrygorii Strashko 		/* Barrier, so that stop_queue visible to other cpus */
94362f94c21SGrygorii Strashko 		smp_mb__after_atomic();
94462f94c21SGrygorii Strashko 
94562f94c21SGrygorii Strashko 		if (cpdma_check_free_tx_desc(txch))
94662f94c21SGrygorii Strashko 			netif_tx_wake_queue(txq);
947e05107e6SIvan Khoronzhuk 	}
948fae50823SMugunthan V N 
949df828598SMugunthan V N 	return NETDEV_TX_OK;
950df828598SMugunthan V N fail:
9518dc43ddcSTobias Klauser 	ndev->stats.tx_dropped++;
952e05107e6SIvan Khoronzhuk 	netif_tx_stop_queue(txq);
95362f94c21SGrygorii Strashko 
95462f94c21SGrygorii Strashko 	/* Barrier, so that stop_queue visible to other cpus */
95562f94c21SGrygorii Strashko 	smp_mb__after_atomic();
95662f94c21SGrygorii Strashko 
95762f94c21SGrygorii Strashko 	if (cpdma_check_free_tx_desc(txch))
95862f94c21SGrygorii Strashko 		netif_tx_wake_queue(txq);
95962f94c21SGrygorii Strashko 
960df828598SMugunthan V N 	return NETDEV_TX_BUSY;
961df828598SMugunthan V N }
962df828598SMugunthan V N 
963dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
964dcfd8d58SMugunthan V N {
965dcfd8d58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
966dcfd8d58SMugunthan V N 	struct sockaddr *addr = (struct sockaddr *)p;
967649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
968dcfd8d58SMugunthan V N 	int flags = 0;
969dcfd8d58SMugunthan V N 	u16 vid = 0;
970a6c5d14fSGrygorii Strashko 	int ret;
971dcfd8d58SMugunthan V N 
972dcfd8d58SMugunthan V N 	if (!is_valid_ether_addr(addr->sa_data))
973dcfd8d58SMugunthan V N 		return -EADDRNOTAVAIL;
974dcfd8d58SMugunthan V N 
97556e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
976a6c5d14fSGrygorii Strashko 	if (ret < 0) {
97756e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
978a6c5d14fSGrygorii Strashko 		return ret;
979a6c5d14fSGrygorii Strashko 	}
980a6c5d14fSGrygorii Strashko 
981606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
982606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
983dcfd8d58SMugunthan V N 		flags = ALE_VLAN;
984dcfd8d58SMugunthan V N 	}
985dcfd8d58SMugunthan V N 
9862a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
987dcfd8d58SMugunthan V N 			   flags, vid);
9882a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
989dcfd8d58SMugunthan V N 			   flags, vid);
990dcfd8d58SMugunthan V N 
991dcfd8d58SMugunthan V N 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
992dcfd8d58SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
993dcfd8d58SMugunthan V N 	for_each_slave(priv, cpsw_set_slave_mac, priv);
994dcfd8d58SMugunthan V N 
99556e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
996a6c5d14fSGrygorii Strashko 
997dcfd8d58SMugunthan V N 	return 0;
998dcfd8d58SMugunthan V N }
999dcfd8d58SMugunthan V N 
10003b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
10013b72c2feSMugunthan V N 				unsigned short vid)
10023b72c2feSMugunthan V N {
10033b72c2feSMugunthan V N 	int ret;
10049f6bd8faSMugunthan V N 	int unreg_mcast_mask = 0;
10055b3a5a14SIvan Khoronzhuk 	int mcast_mask;
10069f6bd8faSMugunthan V N 	u32 port_mask;
1007606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
10089f6bd8faSMugunthan V N 
1009606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
10109f6bd8faSMugunthan V N 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
10119f6bd8faSMugunthan V N 
10125b3a5a14SIvan Khoronzhuk 		mcast_mask = ALE_PORT_HOST;
10139f6bd8faSMugunthan V N 		if (priv->ndev->flags & IFF_ALLMULTI)
10145b3a5a14SIvan Khoronzhuk 			unreg_mcast_mask = mcast_mask;
10159f6bd8faSMugunthan V N 	} else {
10169f6bd8faSMugunthan V N 		port_mask = ALE_ALL_PORTS;
10175b3a5a14SIvan Khoronzhuk 		mcast_mask = port_mask;
10181e5c4bc4SLennart Sorensen 
10191e5c4bc4SLennart Sorensen 		if (priv->ndev->flags & IFF_ALLMULTI)
10201e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_ALL_PORTS;
10211e5c4bc4SLennart Sorensen 		else
10221e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
10239f6bd8faSMugunthan V N 	}
10243b72c2feSMugunthan V N 
10252a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
102661f1cef9SGrygorii Strashko 				unreg_mcast_mask);
10273b72c2feSMugunthan V N 	if (ret != 0)
10283b72c2feSMugunthan V N 		return ret;
10293b72c2feSMugunthan V N 
10302a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
103171a2cbb7SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
10323b72c2feSMugunthan V N 	if (ret != 0)
10333b72c2feSMugunthan V N 		goto clean_vid;
10343b72c2feSMugunthan V N 
10352a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
10365b3a5a14SIvan Khoronzhuk 				 mcast_mask, ALE_VLAN, vid, 0);
10373b72c2feSMugunthan V N 	if (ret != 0)
10383b72c2feSMugunthan V N 		goto clean_vlan_ucast;
10393b72c2feSMugunthan V N 	return 0;
10403b72c2feSMugunthan V N 
10413b72c2feSMugunthan V N clean_vlan_ucast:
10422a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
104371a2cbb7SGrygorii Strashko 			   HOST_PORT_NUM, ALE_VLAN, vid);
10443b72c2feSMugunthan V N clean_vid:
10452a05a622SIvan Khoronzhuk 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
10463b72c2feSMugunthan V N 	return ret;
10473b72c2feSMugunthan V N }
10483b72c2feSMugunthan V N 
10493b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
105080d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
10513b72c2feSMugunthan V N {
10523b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1053649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1054a6c5d14fSGrygorii Strashko 	int ret;
10553b72c2feSMugunthan V N 
1056606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
10573b72c2feSMugunthan V N 		return 0;
10583b72c2feSMugunthan V N 
105956e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1060a6c5d14fSGrygorii Strashko 	if (ret < 0) {
106156e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1062a6c5d14fSGrygorii Strashko 		return ret;
1063a6c5d14fSGrygorii Strashko 	}
1064a6c5d14fSGrygorii Strashko 
1065606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
106602a54164SMugunthan V N 		/* In dual EMAC, reserved VLAN id should not be used for
106702a54164SMugunthan V N 		 * creating VLAN interfaces as this can break the dual
106802a54164SMugunthan V N 		 * EMAC port separation
106902a54164SMugunthan V N 		 */
107002a54164SMugunthan V N 		int i;
107102a54164SMugunthan V N 
1072606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
1073803c4f64SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan) {
1074803c4f64SIvan Khoronzhuk 				ret = -EINVAL;
1075803c4f64SIvan Khoronzhuk 				goto err;
1076803c4f64SIvan Khoronzhuk 			}
107702a54164SMugunthan V N 		}
107802a54164SMugunthan V N 	}
107902a54164SMugunthan V N 
10803b72c2feSMugunthan V N 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
1081a6c5d14fSGrygorii Strashko 	ret = cpsw_add_vlan_ale_entry(priv, vid);
1082803c4f64SIvan Khoronzhuk err:
108356e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
1084a6c5d14fSGrygorii Strashko 	return ret;
10853b72c2feSMugunthan V N }
10863b72c2feSMugunthan V N 
10873b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
108880d5c368SPatrick McHardy 				     __be16 proto, u16 vid)
10893b72c2feSMugunthan V N {
10903b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1091649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
10923b72c2feSMugunthan V N 	int ret;
10933b72c2feSMugunthan V N 
1094606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
10953b72c2feSMugunthan V N 		return 0;
10963b72c2feSMugunthan V N 
109756e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1098a6c5d14fSGrygorii Strashko 	if (ret < 0) {
109956e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1100a6c5d14fSGrygorii Strashko 		return ret;
1101a6c5d14fSGrygorii Strashko 	}
1102a6c5d14fSGrygorii Strashko 
1103606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
110402a54164SMugunthan V N 		int i;
110502a54164SMugunthan V N 
1106606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
1107606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
1108803c4f64SIvan Khoronzhuk 				goto err;
110902a54164SMugunthan V N 		}
111002a54164SMugunthan V N 	}
111102a54164SMugunthan V N 
11123b72c2feSMugunthan V N 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
11132a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1114be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
111561f1cef9SGrygorii Strashko 				  HOST_PORT_NUM, ALE_VLAN, vid);
1116be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
11173b72c2feSMugunthan V N 				  0, ALE_VLAN, vid);
111815180ecaSIvan Khoronzhuk 	ret |= cpsw_ale_flush_multicast(cpsw->ale, 0, vid);
1119803c4f64SIvan Khoronzhuk err:
112056e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
1121a6c5d14fSGrygorii Strashko 	return ret;
11223b72c2feSMugunthan V N }
11233b72c2feSMugunthan V N 
11249ed4050cSIvan Khoronzhuk static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
11259ed4050cSIvan Khoronzhuk 			     struct xdp_frame **frames, u32 flags)
11269ed4050cSIvan Khoronzhuk {
11279ed4050cSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
1128c5013ac1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
11299ed4050cSIvan Khoronzhuk 	struct xdp_frame *xdpf;
1130c5013ac1SGrygorii Strashko 	int i, drops = 0, port;
11319ed4050cSIvan Khoronzhuk 
11329ed4050cSIvan Khoronzhuk 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
11339ed4050cSIvan Khoronzhuk 		return -EINVAL;
11349ed4050cSIvan Khoronzhuk 
11359ed4050cSIvan Khoronzhuk 	for (i = 0; i < n; i++) {
11369ed4050cSIvan Khoronzhuk 		xdpf = frames[i];
11379ed4050cSIvan Khoronzhuk 		if (xdpf->len < CPSW_MIN_PACKET_SIZE) {
11389ed4050cSIvan Khoronzhuk 			xdp_return_frame_rx_napi(xdpf);
11399ed4050cSIvan Khoronzhuk 			drops++;
11409ed4050cSIvan Khoronzhuk 			continue;
11419ed4050cSIvan Khoronzhuk 		}
11429ed4050cSIvan Khoronzhuk 
1143c5013ac1SGrygorii Strashko 		port = priv->emac_port + cpsw->data.dual_emac;
1144c5013ac1SGrygorii Strashko 		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, port))
11459ed4050cSIvan Khoronzhuk 			drops++;
11469ed4050cSIvan Khoronzhuk 	}
11479ed4050cSIvan Khoronzhuk 
11489ed4050cSIvan Khoronzhuk 	return n - drops;
11499ed4050cSIvan Khoronzhuk }
11509ed4050cSIvan Khoronzhuk 
1151026cc9c3SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
1152026cc9c3SDavid S. Miller static void cpsw_ndo_poll_controller(struct net_device *ndev)
1153026cc9c3SDavid S. Miller {
1154026cc9c3SDavid S. Miller 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1155026cc9c3SDavid S. Miller 
1156026cc9c3SDavid S. Miller 	cpsw_intr_disable(cpsw);
1157026cc9c3SDavid S. Miller 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1158026cc9c3SDavid S. Miller 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1159026cc9c3SDavid S. Miller 	cpsw_intr_enable(cpsw);
1160026cc9c3SDavid S. Miller }
1161026cc9c3SDavid S. Miller #endif
1162026cc9c3SDavid S. Miller 
1163df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
1164df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
1165df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
1166df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
1167dcfd8d58SMugunthan V N 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
11682e5b38abSRichard Cochran 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
1169df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
1170df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
11715c50a856SMugunthan V N 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
117283fcad0cSIvan Khoronzhuk 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
1173df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
1174df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
1175df828598SMugunthan V N #endif
11763b72c2feSMugunthan V N 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
11773b72c2feSMugunthan V N 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
11787929a668SIvan Khoronzhuk 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
11799ed4050cSIvan Khoronzhuk 	.ndo_bpf		= cpsw_ndo_bpf,
11809ed4050cSIvan Khoronzhuk 	.ndo_xdp_xmit		= cpsw_ndo_xdp_xmit,
1181df828598SMugunthan V N };
1182df828598SMugunthan V N 
1183df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
1184df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
1185df828598SMugunthan V N {
1186649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
118756e31bd8SIvan Khoronzhuk 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
11887826d43fSJiri Pirko 
118952c4f0ecSMugunthan V N 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
11907826d43fSJiri Pirko 	strlcpy(info->version, "1.0", sizeof(info->version));
119156e31bd8SIvan Khoronzhuk 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1192df828598SMugunthan V N }
1193df828598SMugunthan V N 
11941923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev,
11951923d6e4SMugunthan V N 			       struct ethtool_pauseparam *pause)
11961923d6e4SMugunthan V N {
11971923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
11981923d6e4SMugunthan V N 	bool link;
11991923d6e4SMugunthan V N 
12001923d6e4SMugunthan V N 	priv->rx_pause = pause->rx_pause ? true : false;
12011923d6e4SMugunthan V N 	priv->tx_pause = pause->tx_pause ? true : false;
12021923d6e4SMugunthan V N 
12031923d6e4SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
12041923d6e4SMugunthan V N 	return 0;
12051923d6e4SMugunthan V N }
12061923d6e4SMugunthan V N 
1207022d7ad7SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev,
1208022d7ad7SIvan Khoronzhuk 			     struct ethtool_channels *chs)
1209022d7ad7SIvan Khoronzhuk {
1210c24eef28SGrygorii Strashko 	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1211be034fc1SGrygorii Strashko }
1212be034fc1SGrygorii Strashko 
1213df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
1214df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
1215df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
1216df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
1217df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
12182e5b38abSRichard Cochran 	.get_ts_info	= cpsw_get_ts_info,
1219ff5b8ef2SMugunthan V N 	.get_coalesce	= cpsw_get_coalesce,
1220ff5b8ef2SMugunthan V N 	.set_coalesce	= cpsw_set_coalesce,
1221d9718546SMugunthan V N 	.get_sset_count		= cpsw_get_sset_count,
1222d9718546SMugunthan V N 	.get_strings		= cpsw_get_strings,
1223d9718546SMugunthan V N 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
12241923d6e4SMugunthan V N 	.get_pauseparam		= cpsw_get_pauseparam,
12251923d6e4SMugunthan V N 	.set_pauseparam		= cpsw_set_pauseparam,
1226d8a64420SMatus Ujhelyi 	.get_wol	= cpsw_get_wol,
1227d8a64420SMatus Ujhelyi 	.set_wol	= cpsw_set_wol,
122852c4f0ecSMugunthan V N 	.get_regs_len	= cpsw_get_regs_len,
122952c4f0ecSMugunthan V N 	.get_regs	= cpsw_get_regs,
12307898b1daSGrygorii Strashko 	.begin		= cpsw_ethtool_op_begin,
12317898b1daSGrygorii Strashko 	.complete	= cpsw_ethtool_op_complete,
1232ce52c744SIvan Khoronzhuk 	.get_channels	= cpsw_get_channels,
1233ce52c744SIvan Khoronzhuk 	.set_channels	= cpsw_set_channels,
12342479876dSPhilippe Reynes 	.get_link_ksettings	= cpsw_get_link_ksettings,
12352479876dSPhilippe Reynes 	.set_link_ksettings	= cpsw_set_link_ksettings,
1236a0909949SYegor Yefremov 	.get_eee	= cpsw_get_eee,
1237a0909949SYegor Yefremov 	.set_eee	= cpsw_set_eee,
12386bb10c2bSYegor Yefremov 	.nway_reset	= cpsw_nway_reset,
1239be034fc1SGrygorii Strashko 	.get_ringparam = cpsw_get_ringparam,
1240be034fc1SGrygorii Strashko 	.set_ringparam = cpsw_set_ringparam,
1241df828598SMugunthan V N };
1242df828598SMugunthan V N 
1243552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data,
12442eb32b0aSMugunthan V N 			 struct platform_device *pdev)
12452eb32b0aSMugunthan V N {
12462eb32b0aSMugunthan V N 	struct device_node *node = pdev->dev.of_node;
12472eb32b0aSMugunthan V N 	struct device_node *slave_node;
12482eb32b0aSMugunthan V N 	int i = 0, ret;
12492eb32b0aSMugunthan V N 	u32 prop;
12502eb32b0aSMugunthan V N 
12512eb32b0aSMugunthan V N 	if (!node)
12522eb32b0aSMugunthan V N 		return -EINVAL;
12532eb32b0aSMugunthan V N 
12542eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "slaves", &prop)) {
125588c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
12562eb32b0aSMugunthan V N 		return -EINVAL;
12572eb32b0aSMugunthan V N 	}
12582eb32b0aSMugunthan V N 	data->slaves = prop;
12592eb32b0aSMugunthan V N 
1260e86ac13bSMugunthan V N 	if (of_property_read_u32(node, "active_slave", &prop)) {
126188c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
1262aa1a15e2SDaniel Mack 		return -EINVAL;
126378ca0b28SRichard Cochran 	}
1264e86ac13bSMugunthan V N 	data->active_slave = prop;
126578ca0b28SRichard Cochran 
1266a86854d0SKees Cook 	data->slave_data = devm_kcalloc(&pdev->dev,
1267a86854d0SKees Cook 					data->slaves,
1268a86854d0SKees Cook 					sizeof(struct cpsw_slave_data),
1269b2adaca9SJoe Perches 					GFP_KERNEL);
1270b2adaca9SJoe Perches 	if (!data->slave_data)
1271aa1a15e2SDaniel Mack 		return -ENOMEM;
12722eb32b0aSMugunthan V N 
12732eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
127488c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
1275aa1a15e2SDaniel Mack 		return -EINVAL;
12762eb32b0aSMugunthan V N 	}
12772eb32b0aSMugunthan V N 	data->channels = prop;
12782eb32b0aSMugunthan V N 
12792eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "ale_entries", &prop)) {
128088c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
1281aa1a15e2SDaniel Mack 		return -EINVAL;
12822eb32b0aSMugunthan V N 	}
12832eb32b0aSMugunthan V N 	data->ale_entries = prop;
12842eb32b0aSMugunthan V N 
12852eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
128688c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
1287aa1a15e2SDaniel Mack 		return -EINVAL;
12882eb32b0aSMugunthan V N 	}
12892eb32b0aSMugunthan V N 	data->bd_ram_size = prop;
12902eb32b0aSMugunthan V N 
12912eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "mac_control", &prop)) {
129288c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
1293aa1a15e2SDaniel Mack 		return -EINVAL;
12942eb32b0aSMugunthan V N 	}
12952eb32b0aSMugunthan V N 	data->mac_control = prop;
12962eb32b0aSMugunthan V N 
1297281abd96SMarkus Pargmann 	if (of_property_read_bool(node, "dual_emac"))
1298281abd96SMarkus Pargmann 		data->dual_emac = 1;
1299d9ba8f9eSMugunthan V N 
13001fb19aa7SVaibhav Hiremath 	/*
13011fb19aa7SVaibhav Hiremath 	 * Populate all the child nodes here...
13021fb19aa7SVaibhav Hiremath 	 */
13031fb19aa7SVaibhav Hiremath 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
13041fb19aa7SVaibhav Hiremath 	/* We do not want to force this, as in some cases may not have child */
13051fb19aa7SVaibhav Hiremath 	if (ret)
130688c99ff6SGeorge Cherian 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
13071fb19aa7SVaibhav Hiremath 
13088658aaf2SBen Hutchings 	for_each_available_child_of_node(node, slave_node) {
1309549985eeSRichard Cochran 		struct cpsw_slave_data *slave_data = data->slave_data + i;
1310549985eeSRichard Cochran 		const void *mac_addr = NULL;
1311549985eeSRichard Cochran 		int lenp;
1312549985eeSRichard Cochran 		const __be32 *parp;
1313549985eeSRichard Cochran 
1314f468b10eSMarkus Pargmann 		/* This is no slave child node, continue */
1315bf5849f1SRob Herring 		if (!of_node_name_eq(slave_node, "slave"))
1316f468b10eSMarkus Pargmann 			continue;
1317f468b10eSMarkus Pargmann 
13183ff18849SGrygorii Strashko 		slave_data->ifphy = devm_of_phy_get(&pdev->dev, slave_node,
13193ff18849SGrygorii Strashko 						    NULL);
13203ff18849SGrygorii Strashko 		if (!IS_ENABLED(CONFIG_TI_CPSW_PHY_SEL) &&
13213ff18849SGrygorii Strashko 		    IS_ERR(slave_data->ifphy)) {
13223ff18849SGrygorii Strashko 			ret = PTR_ERR(slave_data->ifphy);
13233ff18849SGrygorii Strashko 			dev_err(&pdev->dev,
13243ff18849SGrygorii Strashko 				"%d: Error retrieving port phy: %d\n", i, ret);
13253cd6e20fSNishka Dasgupta 			goto err_node_put;
13263ff18849SGrygorii Strashko 		}
13273ff18849SGrygorii Strashko 
1328337d1727SMarek Vasut 		slave_data->slave_node = slave_node;
1329552165bcSDavid Rivshin 		slave_data->phy_node = of_parse_phandle(slave_node,
1330552165bcSDavid Rivshin 							"phy-handle", 0);
1331f1eea5c1SDavid Rivshin 		parp = of_get_property(slave_node, "phy_id", &lenp);
1332ae092b5bSDavid Rivshin 		if (slave_data->phy_node) {
1333ae092b5bSDavid Rivshin 			dev_dbg(&pdev->dev,
1334f7ce9103SRob Herring 				"slave[%d] using phy-handle=\"%pOF\"\n",
1335f7ce9103SRob Herring 				i, slave_data->phy_node);
1336ae092b5bSDavid Rivshin 		} else if (of_phy_is_fixed_link(slave_node)) {
1337dfc0a6d3SDavid Rivshin 			/* In the case of a fixed PHY, the DT node associated
1338dfc0a6d3SDavid Rivshin 			 * to the PHY is the Ethernet MAC DT node.
1339dfc0a6d3SDavid Rivshin 			 */
13401f71e8c9SMarkus Brunner 			ret = of_phy_register_fixed_link(slave_node);
134123a09873SJohan Hovold 			if (ret) {
134223a09873SJohan Hovold 				if (ret != -EPROBE_DEFER)
134323a09873SJohan Hovold 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
13443cd6e20fSNishka Dasgupta 				goto err_node_put;
134523a09873SJohan Hovold 			}
134606cd6d6eSDavid Rivshin 			slave_data->phy_node = of_node_get(slave_node);
1347f1eea5c1SDavid Rivshin 		} else if (parp) {
1348f1eea5c1SDavid Rivshin 			u32 phyid;
1349f1eea5c1SDavid Rivshin 			struct device_node *mdio_node;
1350f1eea5c1SDavid Rivshin 			struct platform_device *mdio;
1351f1eea5c1SDavid Rivshin 
1352f1eea5c1SDavid Rivshin 			if (lenp != (sizeof(__be32) * 2)) {
1353f1eea5c1SDavid Rivshin 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
135447276fccSMugunthan V N 				goto no_phy_slave;
1355549985eeSRichard Cochran 			}
1356549985eeSRichard Cochran 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
1357549985eeSRichard Cochran 			phyid = be32_to_cpup(parp+1);
1358549985eeSRichard Cochran 			mdio = of_find_device_by_node(mdio_node);
135960e71ab5SJohan Hovold 			of_node_put(mdio_node);
13606954cc1fSJohan Hovold 			if (!mdio) {
136156fdb2e0SMarkus Pargmann 				dev_err(&pdev->dev, "Missing mdio platform device\n");
13623cd6e20fSNishka Dasgupta 				ret = -EINVAL;
13633cd6e20fSNishka Dasgupta 				goto err_node_put;
13646954cc1fSJohan Hovold 			}
1365549985eeSRichard Cochran 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
1366549985eeSRichard Cochran 				 PHY_ID_FMT, mdio->name, phyid);
136786e1d5adSJohan Hovold 			put_device(&mdio->dev);
1368f1eea5c1SDavid Rivshin 		} else {
1369ae092b5bSDavid Rivshin 			dev_err(&pdev->dev,
1370ae092b5bSDavid Rivshin 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
1371ae092b5bSDavid Rivshin 				i);
1372f1eea5c1SDavid Rivshin 			goto no_phy_slave;
1373f1eea5c1SDavid Rivshin 		}
13740c65b2b9SAndrew Lunn 		ret = of_get_phy_mode(slave_node, &slave_data->phy_if);
13750c65b2b9SAndrew Lunn 		if (ret) {
137647276fccSMugunthan V N 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
137747276fccSMugunthan V N 				i);
13783cd6e20fSNishka Dasgupta 			goto err_node_put;
137947276fccSMugunthan V N 		}
138047276fccSMugunthan V N 
138147276fccSMugunthan V N no_phy_slave:
1382549985eeSRichard Cochran 		mac_addr = of_get_mac_address(slave_node);
1383a51645f7SPetr Štetiar 		if (!IS_ERR(mac_addr)) {
13842d2924afSPetr Štetiar 			ether_addr_copy(slave_data->mac_addr, mac_addr);
13850ba517b1SMarkus Pargmann 		} else {
1386b6745f6eSMugunthan V N 			ret = ti_cm_get_macid(&pdev->dev, i,
13870ba517b1SMarkus Pargmann 					      slave_data->mac_addr);
13880ba517b1SMarkus Pargmann 			if (ret)
13893cd6e20fSNishka Dasgupta 				goto err_node_put;
13900ba517b1SMarkus Pargmann 		}
1391d9ba8f9eSMugunthan V N 		if (data->dual_emac) {
139291c4166cSMugunthan V N 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
1393d9ba8f9eSMugunthan V N 						 &prop)) {
139488c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
1395d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = i+1;
139688c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
1397d9ba8f9eSMugunthan V N 					slave_data->dual_emac_res_vlan, i);
1398d9ba8f9eSMugunthan V N 			} else {
1399d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = prop;
1400d9ba8f9eSMugunthan V N 			}
1401d9ba8f9eSMugunthan V N 		}
1402d9ba8f9eSMugunthan V N 
1403549985eeSRichard Cochran 		i++;
14043cd6e20fSNishka Dasgupta 		if (i == data->slaves) {
14053cd6e20fSNishka Dasgupta 			ret = 0;
14063cd6e20fSNishka Dasgupta 			goto err_node_put;
14073cd6e20fSNishka Dasgupta 		}
1408549985eeSRichard Cochran 	}
1409549985eeSRichard Cochran 
14102eb32b0aSMugunthan V N 	return 0;
14113cd6e20fSNishka Dasgupta 
14123cd6e20fSNishka Dasgupta err_node_put:
14133cd6e20fSNishka Dasgupta 	of_node_put(slave_node);
14143cd6e20fSNishka Dasgupta 	return ret;
14152eb32b0aSMugunthan V N }
14162eb32b0aSMugunthan V N 
1417a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev)
1418a4e32b0dSJohan Hovold {
1419bfe59032SIvan Khoronzhuk 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
14208cbcc466SJohan Hovold 	struct cpsw_platform_data *data = &cpsw->data;
14218cbcc466SJohan Hovold 	struct device_node *node = pdev->dev.of_node;
14228cbcc466SJohan Hovold 	struct device_node *slave_node;
14238cbcc466SJohan Hovold 	int i = 0;
14248cbcc466SJohan Hovold 
14258cbcc466SJohan Hovold 	for_each_available_child_of_node(node, slave_node) {
14268cbcc466SJohan Hovold 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
14278cbcc466SJohan Hovold 
1428bf5849f1SRob Herring 		if (!of_node_name_eq(slave_node, "slave"))
14298cbcc466SJohan Hovold 			continue;
14308cbcc466SJohan Hovold 
14313f65047cSJohan Hovold 		if (of_phy_is_fixed_link(slave_node))
14323f65047cSJohan Hovold 			of_phy_deregister_fixed_link(slave_node);
14338cbcc466SJohan Hovold 
14348cbcc466SJohan Hovold 		of_node_put(slave_data->phy_node);
14358cbcc466SJohan Hovold 
14368cbcc466SJohan Hovold 		i++;
14373cd6e20fSNishka Dasgupta 		if (i == data->slaves) {
14383cd6e20fSNishka Dasgupta 			of_node_put(slave_node);
14398cbcc466SJohan Hovold 			break;
14408cbcc466SJohan Hovold 		}
14413cd6e20fSNishka Dasgupta 	}
14428cbcc466SJohan Hovold 
1443a4e32b0dSJohan Hovold 	of_platform_depopulate(&pdev->dev);
1444a4e32b0dSJohan Hovold }
1445a4e32b0dSJohan Hovold 
144656e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
1447d9ba8f9eSMugunthan V N {
1448606f3993SIvan Khoronzhuk 	struct cpsw_common		*cpsw = priv->cpsw;
1449606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	*data = &cpsw->data;
1450d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
1451d9ba8f9eSMugunthan V N 	struct cpsw_priv		*priv_sl2;
1452e38b5a3dSIvan Khoronzhuk 	int ret = 0;
1453d9ba8f9eSMugunthan V N 
1454d183a942SGrygorii Strashko 	ndev = devm_alloc_etherdev_mqs(cpsw->dev, sizeof(struct cpsw_priv),
1455d183a942SGrygorii Strashko 				       CPSW_MAX_QUEUES, CPSW_MAX_QUEUES);
1456d9ba8f9eSMugunthan V N 	if (!ndev) {
145756e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
1458d9ba8f9eSMugunthan V N 		return -ENOMEM;
1459d9ba8f9eSMugunthan V N 	}
1460d9ba8f9eSMugunthan V N 
1461d9ba8f9eSMugunthan V N 	priv_sl2 = netdev_priv(ndev);
1462606f3993SIvan Khoronzhuk 	priv_sl2->cpsw = cpsw;
1463d9ba8f9eSMugunthan V N 	priv_sl2->ndev = ndev;
1464d9ba8f9eSMugunthan V N 	priv_sl2->dev  = &ndev->dev;
1465d9ba8f9eSMugunthan V N 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1466d9ba8f9eSMugunthan V N 
1467d9ba8f9eSMugunthan V N 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
1468d9ba8f9eSMugunthan V N 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
1469d9ba8f9eSMugunthan V N 			ETH_ALEN);
147056e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
147156e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
1472d9ba8f9eSMugunthan V N 	} else {
14736c1f0a1fSJoe Perches 		eth_random_addr(priv_sl2->mac_addr);
147456e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
147556e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
1476d9ba8f9eSMugunthan V N 	}
1477d9ba8f9eSMugunthan V N 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
1478d9ba8f9eSMugunthan V N 
1479d9ba8f9eSMugunthan V N 	priv_sl2->emac_port = 1;
1480606f3993SIvan Khoronzhuk 	cpsw->slaves[1].ndev = ndev;
1481193736c8SIvan Khoronzhuk 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
1482d9ba8f9eSMugunthan V N 
1483d9ba8f9eSMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
14847ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &cpsw_ethtool_ops;
1485d9ba8f9eSMugunthan V N 
1486d9ba8f9eSMugunthan V N 	/* register the network device */
148756e31bd8SIvan Khoronzhuk 	SET_NETDEV_DEV(ndev, cpsw->dev);
1488337d1727SMarek Vasut 	ndev->dev.of_node = cpsw->slaves[1].data->slave_node;
1489d9ba8f9eSMugunthan V N 	ret = register_netdev(ndev);
1490d183a942SGrygorii Strashko 	if (ret)
149156e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
1492d9ba8f9eSMugunthan V N 
1493d9ba8f9eSMugunthan V N 	return ret;
1494d9ba8f9eSMugunthan V N }
1495d9ba8f9eSMugunthan V N 
14967da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = {
14979611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,cpsw"},
14989611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am335x-cpsw"},
14999611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am4372-cpsw"},
15009611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,dra7-cpsw"},
15017da11600SMugunthan V N 	{ /* sentinel */ },
15027da11600SMugunthan V N };
15037da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
15047da11600SMugunthan V N 
15059611d6d6SIvan Khoronzhuk static const struct soc_device_attribute cpsw_soc_devices[] = {
15069611d6d6SIvan Khoronzhuk 	{ .family = "AM33xx", .revision = "ES1.0"},
15079611d6d6SIvan Khoronzhuk 	{ /* sentinel */ }
15089611d6d6SIvan Khoronzhuk };
15099611d6d6SIvan Khoronzhuk 
1510663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev)
1511df828598SMugunthan V N {
1512c8fb5668SGrygorii Strashko 	struct device			*dev = &pdev->dev;
1513ef4183a1SIvan Khoronzhuk 	struct clk			*clk;
1514d1bd9acfSSebastian Siewior 	struct cpsw_platform_data	*data;
1515df828598SMugunthan V N 	struct net_device		*ndev;
1516df828598SMugunthan V N 	struct cpsw_priv		*priv;
1517aa1a15e2SDaniel Mack 	void __iomem			*ss_regs;
1518c8ace62fSYueHaibing 	struct resource			*ss_res;
15191d147ccbSMugunthan V N 	struct gpio_descs		*mode;
15209611d6d6SIvan Khoronzhuk 	const struct soc_device_attribute *soc;
1521649a1688SIvan Khoronzhuk 	struct cpsw_common		*cpsw;
1522e6a84624SGrygorii Strashko 	int ret = 0, ch;
15235087b915SFelipe Balbi 	int irq;
1524df828598SMugunthan V N 
1525c8fb5668SGrygorii Strashko 	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
15263420ea88SJohan Hovold 	if (!cpsw)
15273420ea88SJohan Hovold 		return -ENOMEM;
15283420ea88SJohan Hovold 
15292d683eaaSAntoine Tenart 	platform_set_drvdata(pdev, cpsw);
153051a95337SGrygorii Strashko 	cpsw_slave_index = cpsw_slave_index_priv;
153151a95337SGrygorii Strashko 
1532c8fb5668SGrygorii Strashko 	cpsw->dev = dev;
1533649a1688SIvan Khoronzhuk 
1534c8fb5668SGrygorii Strashko 	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
15351d147ccbSMugunthan V N 	if (IS_ERR(mode)) {
15361d147ccbSMugunthan V N 		ret = PTR_ERR(mode);
1537c8fb5668SGrygorii Strashko 		dev_err(dev, "gpio request failed, ret %d\n", ret);
1538d183a942SGrygorii Strashko 		return ret;
15391d147ccbSMugunthan V N 	}
15401d147ccbSMugunthan V N 
154183a8471bSGrygorii Strashko 	clk = devm_clk_get(dev, "fck");
154283a8471bSGrygorii Strashko 	if (IS_ERR(clk)) {
1543ac97a359SYueHaibing 		ret = PTR_ERR(clk);
154483a8471bSGrygorii Strashko 		dev_err(dev, "fck is not found %d\n", ret);
154583a8471bSGrygorii Strashko 		return ret;
154683a8471bSGrygorii Strashko 	}
154783a8471bSGrygorii Strashko 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
154883a8471bSGrygorii Strashko 
154983a8471bSGrygorii Strashko 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
155083a8471bSGrygorii Strashko 	ss_regs = devm_ioremap_resource(dev, ss_res);
155183a8471bSGrygorii Strashko 	if (IS_ERR(ss_regs))
155283a8471bSGrygorii Strashko 		return PTR_ERR(ss_regs);
155383a8471bSGrygorii Strashko 	cpsw->regs = ss_regs;
155483a8471bSGrygorii Strashko 
1555c8ace62fSYueHaibing 	cpsw->wr_regs = devm_platform_ioremap_resource(pdev, 1);
155683a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->wr_regs))
155783a8471bSGrygorii Strashko 		return PTR_ERR(cpsw->wr_regs);
155883a8471bSGrygorii Strashko 
155983a8471bSGrygorii Strashko 	/* RX IRQ */
156083a8471bSGrygorii Strashko 	irq = platform_get_irq(pdev, 1);
156183a8471bSGrygorii Strashko 	if (irq < 0)
156283a8471bSGrygorii Strashko 		return irq;
156383a8471bSGrygorii Strashko 	cpsw->irqs_table[0] = irq;
156483a8471bSGrygorii Strashko 
156583a8471bSGrygorii Strashko 	/* TX IRQ */
156683a8471bSGrygorii Strashko 	irq = platform_get_irq(pdev, 2);
156783a8471bSGrygorii Strashko 	if (irq < 0)
156883a8471bSGrygorii Strashko 		return irq;
156983a8471bSGrygorii Strashko 	cpsw->irqs_table[1] = irq;
157083a8471bSGrygorii Strashko 
15711fb19aa7SVaibhav Hiremath 	/*
15721fb19aa7SVaibhav Hiremath 	 * This may be required here for child devices.
15731fb19aa7SVaibhav Hiremath 	 */
1574c8fb5668SGrygorii Strashko 	pm_runtime_enable(dev);
15751fb19aa7SVaibhav Hiremath 
1576a4e32b0dSJohan Hovold 	/* Need to enable clocks with runtime PM api to access module
1577a4e32b0dSJohan Hovold 	 * registers
1578a4e32b0dSJohan Hovold 	 */
1579c8fb5668SGrygorii Strashko 	ret = pm_runtime_get_sync(dev);
1580a4e32b0dSJohan Hovold 	if (ret < 0) {
1581c8fb5668SGrygorii Strashko 		pm_runtime_put_noidle(dev);
1582aa1a15e2SDaniel Mack 		goto clean_runtime_disable_ret;
15832eb32b0aSMugunthan V N 	}
1584a4e32b0dSJohan Hovold 
158523a09873SJohan Hovold 	ret = cpsw_probe_dt(&cpsw->data, pdev);
158623a09873SJohan Hovold 	if (ret)
1587a4e32b0dSJohan Hovold 		goto clean_dt_ret;
158823a09873SJohan Hovold 
158983a8471bSGrygorii Strashko 	soc = soc_device_match(cpsw_soc_devices);
159083a8471bSGrygorii Strashko 	if (soc)
159183a8471bSGrygorii Strashko 		cpsw->quirk_irq = 1;
159283a8471bSGrygorii Strashko 
1593606f3993SIvan Khoronzhuk 	data = &cpsw->data;
1594c8fb5668SGrygorii Strashko 	cpsw->slaves = devm_kcalloc(dev,
1595a86854d0SKees Cook 				    data->slaves, sizeof(struct cpsw_slave),
1596df828598SMugunthan V N 				    GFP_KERNEL);
1597606f3993SIvan Khoronzhuk 	if (!cpsw->slaves) {
1598aa1a15e2SDaniel Mack 		ret = -ENOMEM;
1599a4e32b0dSJohan Hovold 		goto clean_dt_ret;
1600df828598SMugunthan V N 	}
1601df828598SMugunthan V N 
160283a8471bSGrygorii Strashko 	cpsw->rx_packet_max = max(rx_packet_max, CPSW_MAX_PACKET_SIZE);
1603c24eef28SGrygorii Strashko 	cpsw->descs_pool_size = descs_pool_size;
1604df828598SMugunthan V N 
1605e6a84624SGrygorii Strashko 	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1606e6a84624SGrygorii Strashko 			       ss_res->start + CPSW2_BD_OFFSET,
1607e6a84624SGrygorii Strashko 			       descs_pool_size);
1608e6a84624SGrygorii Strashko 	if (ret)
1609a4e32b0dSJohan Hovold 		goto clean_dt_ret;
16108a2c9a5aSGrygorii Strashko 
161183a8471bSGrygorii Strashko 	ch = cpsw->quirk_irq ? 0 : 7;
161283a8471bSGrygorii Strashko 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
161383a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->txv[0].ch)) {
161483a8471bSGrygorii Strashko 		dev_err(dev, "error initializing tx dma channel\n");
161583a8471bSGrygorii Strashko 		ret = PTR_ERR(cpsw->txv[0].ch);
161683a8471bSGrygorii Strashko 		goto clean_cpts;
1617df828598SMugunthan V N 	}
1618df828598SMugunthan V N 
161983a8471bSGrygorii Strashko 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
162083a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->rxv[0].ch)) {
162183a8471bSGrygorii Strashko 		dev_err(dev, "error initializing rx dma channel\n");
162283a8471bSGrygorii Strashko 		ret = PTR_ERR(cpsw->rxv[0].ch);
162383a8471bSGrygorii Strashko 		goto clean_cpts;
162483a8471bSGrygorii Strashko 	}
162583a8471bSGrygorii Strashko 	cpsw_split_res(cpsw);
162683a8471bSGrygorii Strashko 
162783a8471bSGrygorii Strashko 	/* setup netdev */
162883a8471bSGrygorii Strashko 	ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
162983a8471bSGrygorii Strashko 				       CPSW_MAX_QUEUES, CPSW_MAX_QUEUES);
163083a8471bSGrygorii Strashko 	if (!ndev) {
163183a8471bSGrygorii Strashko 		dev_err(dev, "error allocating net_device\n");
163283a8471bSGrygorii Strashko 		goto clean_cpts;
163383a8471bSGrygorii Strashko 	}
163483a8471bSGrygorii Strashko 
163583a8471bSGrygorii Strashko 	priv = netdev_priv(ndev);
163683a8471bSGrygorii Strashko 	priv->cpsw = cpsw;
163783a8471bSGrygorii Strashko 	priv->ndev = ndev;
163883a8471bSGrygorii Strashko 	priv->dev  = dev;
163983a8471bSGrygorii Strashko 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
164083a8471bSGrygorii Strashko 	priv->emac_port = 0;
164183a8471bSGrygorii Strashko 
164283a8471bSGrygorii Strashko 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
164383a8471bSGrygorii Strashko 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
164483a8471bSGrygorii Strashko 		dev_info(dev, "Detected MACID = %pM\n", priv->mac_addr);
164583a8471bSGrygorii Strashko 	} else {
164683a8471bSGrygorii Strashko 		eth_random_addr(priv->mac_addr);
164783a8471bSGrygorii Strashko 		dev_info(dev, "Random MACID = %pM\n", priv->mac_addr);
164883a8471bSGrygorii Strashko 	}
164983a8471bSGrygorii Strashko 
165083a8471bSGrygorii Strashko 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
165183a8471bSGrygorii Strashko 
165283a8471bSGrygorii Strashko 	cpsw->slaves[0].ndev = ndev;
165383a8471bSGrygorii Strashko 
1654a3a41d2fSGrygorii Strashko 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
1655070f9c65SKeerthy 
1656070f9c65SKeerthy 	ndev->netdev_ops = &cpsw_netdev_ops;
1657070f9c65SKeerthy 	ndev->ethtool_ops = &cpsw_ethtool_ops;
16589611d6d6SIvan Khoronzhuk 	netif_napi_add(ndev, &cpsw->napi_rx,
16599611d6d6SIvan Khoronzhuk 		       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
16609611d6d6SIvan Khoronzhuk 		       CPSW_POLL_WEIGHT);
16619611d6d6SIvan Khoronzhuk 	netif_tx_napi_add(ndev, &cpsw->napi_tx,
16629611d6d6SIvan Khoronzhuk 			  cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
16639611d6d6SIvan Khoronzhuk 			  CPSW_POLL_WEIGHT);
1664070f9c65SKeerthy 
1665070f9c65SKeerthy 	/* register the network device */
1666c8fb5668SGrygorii Strashko 	SET_NETDEV_DEV(ndev, dev);
1667337d1727SMarek Vasut 	ndev->dev.of_node = cpsw->slaves[0].data->slave_node;
1668070f9c65SKeerthy 	ret = register_netdev(ndev);
1669070f9c65SKeerthy 	if (ret) {
1670c8fb5668SGrygorii Strashko 		dev_err(dev, "error registering net device\n");
1671070f9c65SKeerthy 		ret = -ENODEV;
167283a8471bSGrygorii Strashko 		goto clean_cpts;
1673070f9c65SKeerthy 	}
1674070f9c65SKeerthy 
1675070f9c65SKeerthy 	if (cpsw->data.dual_emac) {
1676070f9c65SKeerthy 		ret = cpsw_probe_dual_emac(priv);
1677070f9c65SKeerthy 		if (ret) {
1678070f9c65SKeerthy 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
1679070f9c65SKeerthy 			goto clean_unregister_netdev_ret;
1680070f9c65SKeerthy 		}
1681070f9c65SKeerthy 	}
1682070f9c65SKeerthy 
1683c03abd84SFelipe Balbi 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1684c03abd84SFelipe Balbi 	 * MISC IRQs which are always kept disabled with this driver so
1685c03abd84SFelipe Balbi 	 * we will not request them.
1686c03abd84SFelipe Balbi 	 *
1687c03abd84SFelipe Balbi 	 * If anyone wants to implement support for those, make sure to
1688c03abd84SFelipe Balbi 	 * first request and append them to irqs_table array.
1689c03abd84SFelipe Balbi 	 */
169083a8471bSGrygorii Strashko 	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1691c8fb5668SGrygorii Strashko 			       0, dev_name(dev), cpsw);
16925087b915SFelipe Balbi 	if (ret < 0) {
1693c8fb5668SGrygorii Strashko 		dev_err(dev, "error attaching irq (%d)\n", ret);
169483a8471bSGrygorii Strashko 		goto clean_unregister_netdev_ret;
1695df828598SMugunthan V N 	}
1696df828598SMugunthan V N 
16975087b915SFelipe Balbi 
169883a8471bSGrygorii Strashko 	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1699dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->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;
17035087b915SFelipe Balbi 	}
1704c2b32e58SDaniel Mack 
170590225bf0SGrygorii Strashko 	cpsw_notice(priv, probe,
170690225bf0SGrygorii Strashko 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
170783a8471bSGrygorii Strashko 		    &ss_res->start, cpsw->irqs_table[0], descs_pool_size);
1708d9ba8f9eSMugunthan V N 
1709c46ab7e0SJohan Hovold 	pm_runtime_put(&pdev->dev);
1710c46ab7e0SJohan Hovold 
1711df828598SMugunthan V N 	return 0;
1712df828598SMugunthan V N 
1713a7fe9d46SJohan Hovold clean_unregister_netdev_ret:
1714a7fe9d46SJohan Hovold 	unregister_netdev(ndev);
171583a8471bSGrygorii Strashko clean_cpts:
171683a8471bSGrygorii Strashko 	cpts_release(cpsw->cpts);
17172c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
1718a4e32b0dSJohan Hovold clean_dt_ret:
1719a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
1720c46ab7e0SJohan Hovold 	pm_runtime_put_sync(&pdev->dev);
1721aa1a15e2SDaniel Mack clean_runtime_disable_ret:
1722f150bd7fSMugunthan V N 	pm_runtime_disable(&pdev->dev);
1723df828598SMugunthan V N 	return ret;
1724df828598SMugunthan V N }
1725df828598SMugunthan V N 
1726663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev)
1727df828598SMugunthan V N {
1728bfe59032SIvan Khoronzhuk 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
1729bfe59032SIvan Khoronzhuk 	int i, ret;
17308a0b6dc9SGrygorii Strashko 
17318a0b6dc9SGrygorii Strashko 	ret = pm_runtime_get_sync(&pdev->dev);
17328a0b6dc9SGrygorii Strashko 	if (ret < 0) {
17338a0b6dc9SGrygorii Strashko 		pm_runtime_put_noidle(&pdev->dev);
17348a0b6dc9SGrygorii Strashko 		return ret;
17358a0b6dc9SGrygorii Strashko 	}
1736df828598SMugunthan V N 
1737bfe59032SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
1738bfe59032SIvan Khoronzhuk 		if (cpsw->slaves[i].ndev)
1739bfe59032SIvan Khoronzhuk 			unregister_netdev(cpsw->slaves[i].ndev);
1740df828598SMugunthan V N 
17418a2c9a5aSGrygorii Strashko 	cpts_release(cpsw->cpts);
17422c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
1743a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
17448a0b6dc9SGrygorii Strashko 	pm_runtime_put_sync(&pdev->dev);
17458a0b6dc9SGrygorii Strashko 	pm_runtime_disable(&pdev->dev);
1746df828598SMugunthan V N 	return 0;
1747df828598SMugunthan V N }
1748df828598SMugunthan V N 
17498963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP
1750df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
1751df828598SMugunthan V N {
17522f9b0d93SKeerthy 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
1753618073e3SMugunthan V N 	int i;
1754618073e3SMugunthan V N 
17552f9b0d93SKeerthy 	for (i = 0; i < cpsw->data.slaves; i++)
17562f9b0d93SKeerthy 		if (cpsw->slaves[i].ndev)
1757606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
1758606f3993SIvan Khoronzhuk 				cpsw_ndo_stop(cpsw->slaves[i].ndev);
17591e7a2e21SDaniel Mack 
1760739683b4SMugunthan V N 	/* Select sleep pin state */
176156e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_sleep_state(dev);
1762739683b4SMugunthan V N 
1763df828598SMugunthan V N 	return 0;
1764df828598SMugunthan V N }
1765df828598SMugunthan V N 
1766df828598SMugunthan V N static int cpsw_resume(struct device *dev)
1767df828598SMugunthan V N {
17682f9b0d93SKeerthy 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
17692f9b0d93SKeerthy 	int i;
1770df828598SMugunthan V N 
1771739683b4SMugunthan V N 	/* Select default pin state */
177256e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_default_state(dev);
1773739683b4SMugunthan V N 
17744ccfd638SGrygorii Strashko 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
17754ccfd638SGrygorii Strashko 	rtnl_lock();
1776618073e3SMugunthan V N 
17772f9b0d93SKeerthy 	for (i = 0; i < cpsw->data.slaves; i++)
17782f9b0d93SKeerthy 		if (cpsw->slaves[i].ndev)
1779606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
1780606f3993SIvan Khoronzhuk 				cpsw_ndo_open(cpsw->slaves[i].ndev);
17812f9b0d93SKeerthy 
17824ccfd638SGrygorii Strashko 	rtnl_unlock();
17834ccfd638SGrygorii Strashko 
1784df828598SMugunthan V N 	return 0;
1785df828598SMugunthan V N }
17868963a504SGrygorii Strashko #endif
1787df828598SMugunthan V N 
17888963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
1789df828598SMugunthan V N 
1790df828598SMugunthan V N static struct platform_driver cpsw_driver = {
1791df828598SMugunthan V N 	.driver = {
1792df828598SMugunthan V N 		.name	 = "cpsw",
1793df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
17941e5c76d4SSachin Kamat 		.of_match_table = cpsw_of_mtable,
1795df828598SMugunthan V N 	},
1796df828598SMugunthan V N 	.probe = cpsw_probe,
1797663e12e6SBill Pemberton 	.remove = cpsw_remove,
1798df828598SMugunthan V N };
1799df828598SMugunthan V N 
18006fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver);
1801df828598SMugunthan V N 
1802df828598SMugunthan V N MODULE_LICENSE("GPL");
1803df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1804df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1805df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
1806