xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision 43b5169d)
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) {
395*43b5169dSLorenzo Bianconi 		xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
396*43b5169dSLorenzo Bianconi 
3979ed4050cSIvan Khoronzhuk 		if (status & CPDMA_RX_VLAN_ENCAP) {
3989ed4050cSIvan Khoronzhuk 			xdp.data = pa + CPSW_HEADROOM +
3999ed4050cSIvan Khoronzhuk 				   CPSW_RX_VLAN_ENCAP_HDR_SIZE;
4009ed4050cSIvan Khoronzhuk 			xdp.data_end = xdp.data + len -
4019ed4050cSIvan Khoronzhuk 				       CPSW_RX_VLAN_ENCAP_HDR_SIZE;
4029ed4050cSIvan Khoronzhuk 		} else {
4039ed4050cSIvan Khoronzhuk 			xdp.data = pa + CPSW_HEADROOM;
4049ed4050cSIvan Khoronzhuk 			xdp.data_end = xdp.data + len;
4059ed4050cSIvan Khoronzhuk 		}
4069ed4050cSIvan Khoronzhuk 
4079ed4050cSIvan Khoronzhuk 		xdp_set_data_meta_invalid(&xdp);
4089ed4050cSIvan Khoronzhuk 
4099ed4050cSIvan Khoronzhuk 		xdp.data_hard_start = pa;
4109ed4050cSIvan Khoronzhuk 
411c5013ac1SGrygorii Strashko 		port = priv->emac_port + cpsw->data.dual_emac;
412c5013ac1SGrygorii Strashko 		ret = cpsw_run_xdp(priv, ch, &xdp, page, port);
4139ed4050cSIvan Khoronzhuk 		if (ret != CPSW_XDP_PASS)
4149ed4050cSIvan Khoronzhuk 			goto requeue;
4159ed4050cSIvan Khoronzhuk 
4169ed4050cSIvan Khoronzhuk 		/* XDP prog might have changed packet data and boundaries */
4179ed4050cSIvan Khoronzhuk 		len = xdp.data_end - xdp.data;
4189ed4050cSIvan Khoronzhuk 		headroom = xdp.data - xdp.data_hard_start;
4199ed4050cSIvan Khoronzhuk 
4209ed4050cSIvan Khoronzhuk 		/* XDP prog can modify vlan tag, so can't use encap header */
4219ed4050cSIvan Khoronzhuk 		status &= ~CPDMA_RX_VLAN_ENCAP;
4229ed4050cSIvan Khoronzhuk 	}
4239ed4050cSIvan Khoronzhuk 
4249ed4050cSIvan Khoronzhuk 	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
4259ed4050cSIvan Khoronzhuk 	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
4269ed4050cSIvan Khoronzhuk 	if (!skb) {
4279ed4050cSIvan Khoronzhuk 		ndev->stats.rx_dropped++;
4289ed4050cSIvan Khoronzhuk 		page_pool_recycle_direct(pool, page);
4299ed4050cSIvan Khoronzhuk 		goto requeue;
4309ed4050cSIvan Khoronzhuk 	}
4319ed4050cSIvan Khoronzhuk 
4329ed4050cSIvan Khoronzhuk 	skb_reserve(skb, headroom);
433df828598SMugunthan V N 	skb_put(skb, len);
4349ed4050cSIvan Khoronzhuk 	skb->dev = ndev;
435a3a41d2fSGrygorii Strashko 	if (status & CPDMA_RX_VLAN_ENCAP)
436a3a41d2fSGrygorii Strashko 		cpsw_rx_vlan_encap(skb);
437a9423120SIvan Khoronzhuk 	if (priv->rx_ts_enabled)
4382a05a622SIvan Khoronzhuk 		cpts_rx_timestamp(cpsw->cpts, skb);
439df828598SMugunthan V N 	skb->protocol = eth_type_trans(skb, ndev);
4409ed4050cSIvan Khoronzhuk 
4419ed4050cSIvan Khoronzhuk 	/* unmap page as no netstack skb page recycling */
4429ed4050cSIvan Khoronzhuk 	page_pool_release_page(pool, page);
443df828598SMugunthan V N 	netif_receive_skb(skb);
4449ed4050cSIvan Khoronzhuk 
4458dc43ddcSTobias Klauser 	ndev->stats.rx_bytes += len;
4468dc43ddcSTobias Klauser 	ndev->stats.rx_packets++;
447df828598SMugunthan V N 
448a0e2c822SMugunthan V N requeue:
4499ed4050cSIvan Khoronzhuk 	xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
4509ed4050cSIvan Khoronzhuk 	xmeta->ndev = ndev;
4519ed4050cSIvan Khoronzhuk 	xmeta->ch = ch;
4529ed4050cSIvan Khoronzhuk 
4539ed4050cSIvan Khoronzhuk 	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
4549ed4050cSIvan Khoronzhuk 	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
4559ed4050cSIvan Khoronzhuk 				       pkt_size, 0);
456871e8465SIvan Khoronzhuk 	if (ret < 0) {
457871e8465SIvan Khoronzhuk 		WARN_ON(ret == -ENOMEM);
4589ed4050cSIvan Khoronzhuk 		page_pool_recycle_direct(pool, new_page);
459df828598SMugunthan V N 	}
460871e8465SIvan Khoronzhuk }
461df828598SMugunthan V N 
462df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
463df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
464df828598SMugunthan V N {
465df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
466df828598SMugunthan V N 	u32			mac_control = 0;
467df828598SMugunthan V N 	u32			slave_port;
468606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
469df828598SMugunthan V N 
470df828598SMugunthan V N 	if (!phy)
471df828598SMugunthan V N 		return;
472df828598SMugunthan V N 
4736f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
474df828598SMugunthan V N 
475df828598SMugunthan V N 	if (phy->link) {
476cfc08345SGrygorii Strashko 		mac_control = CPSW_SL_CTL_GMII_EN;
477cfc08345SGrygorii Strashko 
478cfc08345SGrygorii Strashko 		if (phy->speed == 1000)
479cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_GIG;
480cfc08345SGrygorii Strashko 		if (phy->duplex)
481cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_FULLDUPLEX;
482cfc08345SGrygorii Strashko 
483cfc08345SGrygorii Strashko 		/* set speed_in input in case RMII mode is used in 100Mbps */
484cfc08345SGrygorii Strashko 		if (phy->speed == 100)
485cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_IFCTL_A;
486cfc08345SGrygorii Strashko 		/* in band mode only works in 10Mbps RGMII mode */
487cfc08345SGrygorii Strashko 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
488cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
489cfc08345SGrygorii Strashko 
490cfc08345SGrygorii Strashko 		if (priv->rx_pause)
491cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
492cfc08345SGrygorii Strashko 
493cfc08345SGrygorii Strashko 		if (priv->tx_pause)
494cfc08345SGrygorii Strashko 			mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
495cfc08345SGrygorii Strashko 
496cfc08345SGrygorii Strashko 		if (mac_control != slave->mac_control)
497cfc08345SGrygorii Strashko 			cpsw_sl_ctl_set(slave->mac_sl, mac_control);
498df828598SMugunthan V N 
499df828598SMugunthan V N 		/* enable forwarding */
5002a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
501df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
502df828598SMugunthan V N 
503df828598SMugunthan V N 		*link = true;
50457d90148SIvan Khoronzhuk 
50557d90148SIvan Khoronzhuk 		if (priv->shp_cfg_speed &&
50657d90148SIvan Khoronzhuk 		    priv->shp_cfg_speed != slave->phy->speed &&
50757d90148SIvan Khoronzhuk 		    !cpsw_shp_is_off(priv))
50857d90148SIvan Khoronzhuk 			dev_warn(priv->dev,
50957d90148SIvan Khoronzhuk 				 "Speed was changed, CBS shaper speeds are changed!");
510df828598SMugunthan V N 	} else {
511df828598SMugunthan V N 		mac_control = 0;
512df828598SMugunthan V N 		/* disable forwarding */
5132a05a622SIvan Khoronzhuk 		cpsw_ale_control_set(cpsw->ale, slave_port,
514df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
515cfc08345SGrygorii Strashko 
516cfc08345SGrygorii Strashko 		cpsw_sl_wait_for_idle(slave->mac_sl, 100);
517cfc08345SGrygorii Strashko 
518cfc08345SGrygorii Strashko 		cpsw_sl_ctl_reset(slave->mac_sl);
519df828598SMugunthan V N 	}
520df828598SMugunthan V N 
521cfc08345SGrygorii Strashko 	if (mac_control != slave->mac_control)
522df828598SMugunthan V N 		phy_print_status(phy);
523df828598SMugunthan V N 
524df828598SMugunthan V N 	slave->mac_control = mac_control;
525df828598SMugunthan V N }
526df828598SMugunthan V N 
527df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
528df828598SMugunthan V N {
529df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
5300be01b8eSIvan Khoronzhuk 	struct cpsw_common	*cpsw = priv->cpsw;
531df828598SMugunthan V N 	bool			link = false;
532df828598SMugunthan V N 
533df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
534df828598SMugunthan V N 
535df828598SMugunthan V N 	if (link) {
5360be01b8eSIvan Khoronzhuk 		if (cpsw_need_resplit(cpsw))
5379763a891SGrygorii Strashko 			cpsw_split_res(cpsw);
5380be01b8eSIvan Khoronzhuk 
539df828598SMugunthan V N 		netif_carrier_on(ndev);
540df828598SMugunthan V N 		if (netif_running(ndev))
541e05107e6SIvan Khoronzhuk 			netif_tx_wake_all_queues(ndev);
542df828598SMugunthan V N 	} else {
543df828598SMugunthan V N 		netif_carrier_off(ndev);
544e05107e6SIvan Khoronzhuk 		netif_tx_stop_all_queues(ndev);
545df828598SMugunthan V N 	}
546df828598SMugunthan V N }
547df828598SMugunthan V N 
548d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries(
549d9ba8f9eSMugunthan V N 		struct cpsw_priv *priv, struct cpsw_slave *slave,
550d9ba8f9eSMugunthan V N 		u32 slave_port)
551d9ba8f9eSMugunthan V N {
5522a05a622SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
55371a2cbb7SGrygorii Strashko 	u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
554d9ba8f9eSMugunthan V N 
5552a05a622SIvan Khoronzhuk 	if (cpsw->version == CPSW_VERSION_1)
556d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
557d9ba8f9eSMugunthan V N 	else
558d9ba8f9eSMugunthan V N 		slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
5592a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
560d9ba8f9eSMugunthan V N 			  port_mask, port_mask, 0);
5612a05a622SIvan Khoronzhuk 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
5625b3a5a14SIvan Khoronzhuk 			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0);
5632a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
5642a05a622SIvan Khoronzhuk 			   HOST_PORT_NUM, ALE_VLAN |
5652a05a622SIvan Khoronzhuk 			   ALE_SECURE, slave->port_vlan);
5665e5add17SGrygorii Strashko 	cpsw_ale_control_set(cpsw->ale, slave_port,
5675e5add17SGrygorii Strashko 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
568d9ba8f9eSMugunthan V N }
569d9ba8f9eSMugunthan V N 
5701e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
5711e7a2e21SDaniel Mack {
572df828598SMugunthan V N 	u32 slave_port;
57330c57f07SSekhar Nori 	struct phy_device *phy;
574649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
575df828598SMugunthan V N 
576cfc08345SGrygorii Strashko 	cpsw_sl_reset(slave->mac_sl, 100);
577cfc08345SGrygorii Strashko 	cpsw_sl_ctl_reset(slave->mac_sl);
578df828598SMugunthan V N 
579df828598SMugunthan V N 	/* setup priority mapping */
580cfc08345SGrygorii Strashko 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
581cfc08345SGrygorii Strashko 			  RX_PRIORITY_MAPPING);
5829750a3adSRichard Cochran 
5832a05a622SIvan Khoronzhuk 	switch (cpsw->version) {
5849750a3adSRichard Cochran 	case CPSW_VERSION_1:
5859750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
58648f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
58748f5bcccSGrygorii Strashko 		 * flow control mode
58848f5bcccSGrygorii Strashko 		 */
58948f5bcccSGrygorii Strashko 		slave_write(slave,
59048f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
59148f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
5929750a3adSRichard Cochran 		break;
5939750a3adSRichard Cochran 	case CPSW_VERSION_2:
594c193f365SMugunthan V N 	case CPSW_VERSION_3:
595926489beSMugunthan V N 	case CPSW_VERSION_4:
5969750a3adSRichard Cochran 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
59748f5bcccSGrygorii Strashko 		/* Increase RX FIFO size to 5 for supporting fullduplex
59848f5bcccSGrygorii Strashko 		 * flow control mode
59948f5bcccSGrygorii Strashko 		 */
60048f5bcccSGrygorii Strashko 		slave_write(slave,
60148f5bcccSGrygorii Strashko 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
60248f5bcccSGrygorii Strashko 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
6039750a3adSRichard Cochran 		break;
6049750a3adSRichard Cochran 	}
605df828598SMugunthan V N 
606df828598SMugunthan V N 	/* setup max packet size, and mac address */
607cfc08345SGrygorii Strashko 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
608cfc08345SGrygorii Strashko 			  cpsw->rx_packet_max);
609df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
610df828598SMugunthan V N 
611df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
612df828598SMugunthan V N 
6136f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
614df828598SMugunthan V N 
615606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac)
616d9ba8f9eSMugunthan V N 		cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
617d9ba8f9eSMugunthan V N 	else
6182a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
619e11b220fSMugunthan V N 				   1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
620df828598SMugunthan V N 
621d733f754SDavid Rivshin 	if (slave->data->phy_node) {
62230c57f07SSekhar Nori 		phy = of_phy_connect(priv->ndev, slave->data->phy_node,
6239e42f715SHeiko Schocher 				 &cpsw_adjust_link, 0, slave->data->phy_if);
62430c57f07SSekhar Nori 		if (!phy) {
625f7ce9103SRob Herring 			dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
626f7ce9103SRob Herring 				slave->data->phy_node,
627d733f754SDavid Rivshin 				slave->slave_num);
628d733f754SDavid Rivshin 			return;
629d733f754SDavid Rivshin 		}
630d733f754SDavid Rivshin 	} else {
63130c57f07SSekhar Nori 		phy = phy_connect(priv->ndev, slave->data->phy_id,
632f9a8f83bSFlorian Fainelli 				 &cpsw_adjust_link, slave->data->phy_if);
63330c57f07SSekhar Nori 		if (IS_ERR(phy)) {
634d733f754SDavid Rivshin 			dev_err(priv->dev,
635d733f754SDavid Rivshin 				"phy \"%s\" not found on slave %d, err %ld\n",
636d733f754SDavid Rivshin 				slave->data->phy_id, slave->slave_num,
63730c57f07SSekhar Nori 				PTR_ERR(phy));
638d733f754SDavid Rivshin 			return;
639d733f754SDavid Rivshin 		}
640d733f754SDavid Rivshin 	}
641d733f754SDavid Rivshin 
64230c57f07SSekhar Nori 	slave->phy = phy;
64330c57f07SSekhar Nori 
6442220943aSAndrew Lunn 	phy_attached_info(slave->phy);
6452220943aSAndrew Lunn 
646df828598SMugunthan V N 	phy_start(slave->phy);
647388367a5SMugunthan V N 
648388367a5SMugunthan V N 	/* Configure GMII_SEL register */
6493ff18849SGrygorii Strashko 	if (!IS_ERR(slave->data->ifphy))
6503ff18849SGrygorii Strashko 		phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
6513ff18849SGrygorii Strashko 				 slave->data->phy_if);
6523ff18849SGrygorii Strashko 	else
6533ff18849SGrygorii Strashko 		cpsw_phy_sel(cpsw->dev, slave->phy->interface,
6543ff18849SGrygorii Strashko 			     slave->slave_num);
655df828598SMugunthan V N }
656df828598SMugunthan V N 
6573b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
6583b72c2feSMugunthan V N {
659606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
660606f3993SIvan Khoronzhuk 	const int vlan = cpsw->data.default_vlan;
6613b72c2feSMugunthan V N 	u32 reg;
6623b72c2feSMugunthan V N 	int i;
6631e5c4bc4SLennart Sorensen 	int unreg_mcast_mask;
6643b72c2feSMugunthan V N 
6652a05a622SIvan Khoronzhuk 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
6663b72c2feSMugunthan V N 	       CPSW2_PORT_VLAN;
6673b72c2feSMugunthan V N 
6685d8d0d4dSIvan Khoronzhuk 	writel(vlan, &cpsw->host_port_regs->port_vlan);
6693b72c2feSMugunthan V N 
670606f3993SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
671606f3993SIvan Khoronzhuk 		slave_write(cpsw->slaves + i, vlan, reg);
6723b72c2feSMugunthan V N 
6731e5c4bc4SLennart Sorensen 	if (priv->ndev->flags & IFF_ALLMULTI)
6741e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_ALL_PORTS;
6751e5c4bc4SLennart Sorensen 	else
6761e5c4bc4SLennart Sorensen 		unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
6771e5c4bc4SLennart Sorensen 
6782a05a622SIvan Khoronzhuk 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
67961f1cef9SGrygorii Strashko 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
68061f1cef9SGrygorii Strashko 			  unreg_mcast_mask);
6813b72c2feSMugunthan V N }
6823b72c2feSMugunthan V N 
683df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
684df828598SMugunthan V N {
685d9ba8f9eSMugunthan V N 	u32 fifo_mode;
6865d8d0d4dSIvan Khoronzhuk 	u32 control_reg;
6875d8d0d4dSIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
6883b72c2feSMugunthan V N 
689df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
6905d8d0d4dSIvan Khoronzhuk 	soft_reset("cpsw", &cpsw->regs->soft_reset);
6912a05a622SIvan Khoronzhuk 	cpsw_ale_start(cpsw->ale);
692df828598SMugunthan V N 
693df828598SMugunthan V N 	/* switch to vlan unaware mode */
6942a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
6953b72c2feSMugunthan V N 			     CPSW_ALE_VLAN_AWARE);
6965d8d0d4dSIvan Khoronzhuk 	control_reg = readl(&cpsw->regs->control);
697a3a41d2fSGrygorii Strashko 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
6985d8d0d4dSIvan Khoronzhuk 	writel(control_reg, &cpsw->regs->control);
699606f3993SIvan Khoronzhuk 	fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
700d9ba8f9eSMugunthan V N 		     CPSW_FIFO_NORMAL_MODE;
7015d8d0d4dSIvan Khoronzhuk 	writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
702df828598SMugunthan V N 
703df828598SMugunthan V N 	/* setup host port priority mapping */
704dda5f5feSGrygorii Strashko 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
7055d8d0d4dSIvan Khoronzhuk 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
706dda5f5feSGrygorii Strashko 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
707df828598SMugunthan V N 
7082a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
709df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
710df828598SMugunthan V N 
711606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac) {
7122a05a622SIvan Khoronzhuk 		cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
713d9ba8f9eSMugunthan V N 				   0, 0);
7142a05a622SIvan Khoronzhuk 		cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
71571a2cbb7SGrygorii Strashko 				   ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
716df828598SMugunthan V N 	}
717d9ba8f9eSMugunthan V N }
718df828598SMugunthan V N 
7192a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
720aacebbf8SSebastian Siewior {
7213995d265SSchuyler Patton 	u32 slave_port;
7223995d265SSchuyler Patton 
7236f1f5836SIvan Khoronzhuk 	slave_port = cpsw_get_slave_port(slave->slave_num);
7243995d265SSchuyler Patton 
725aacebbf8SSebastian Siewior 	if (!slave->phy)
726aacebbf8SSebastian Siewior 		return;
727aacebbf8SSebastian Siewior 	phy_stop(slave->phy);
728aacebbf8SSebastian Siewior 	phy_disconnect(slave->phy);
729aacebbf8SSebastian Siewior 	slave->phy = NULL;
7302a05a622SIvan Khoronzhuk 	cpsw_ale_control_set(cpsw->ale, slave_port,
7313995d265SSchuyler Patton 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
732cfc08345SGrygorii Strashko 	cpsw_sl_reset(slave->mac_sl, 100);
733cfc08345SGrygorii Strashko 	cpsw_sl_ctl_reset(slave->mac_sl);
734aacebbf8SSebastian Siewior }
735aacebbf8SSebastian Siewior 
73600fe4712SIvan Khoronzhuk static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
73700fe4712SIvan Khoronzhuk {
73800fe4712SIvan Khoronzhuk 	struct cpsw_priv *priv = arg;
73900fe4712SIvan Khoronzhuk 
74000fe4712SIvan Khoronzhuk 	if (!vdev)
74100fe4712SIvan Khoronzhuk 		return 0;
74200fe4712SIvan Khoronzhuk 
74300fe4712SIvan Khoronzhuk 	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
74400fe4712SIvan Khoronzhuk 	return 0;
74500fe4712SIvan Khoronzhuk }
74600fe4712SIvan Khoronzhuk 
7474b4255edSIvan Khoronzhuk /* restore resources after port reset */
7484b4255edSIvan Khoronzhuk static void cpsw_restore(struct cpsw_priv *priv)
7494b4255edSIvan Khoronzhuk {
75000fe4712SIvan Khoronzhuk 	/* restore vlan configurations */
75100fe4712SIvan Khoronzhuk 	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
75200fe4712SIvan Khoronzhuk 
7534b4255edSIvan Khoronzhuk 	/* restore MQPRIO offload */
7544b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_mqprio_resume, priv);
7554b4255edSIvan Khoronzhuk 
7564b4255edSIvan Khoronzhuk 	/* restore CBS offload */
7574b4255edSIvan Khoronzhuk 	for_each_slave(priv, cpsw_cbs_resume, priv);
7584b4255edSIvan Khoronzhuk }
7594b4255edSIvan Khoronzhuk 
760df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
761df828598SMugunthan V N {
762df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
763649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
7643802dce1SIvan Khoronzhuk 	int ret;
765df828598SMugunthan V N 	u32 reg;
766df828598SMugunthan V N 
76756e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
768108a6537SGrygorii Strashko 	if (ret < 0) {
76956e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
770108a6537SGrygorii Strashko 		return ret;
771108a6537SGrygorii Strashko 	}
7723fa88c51SGrygorii Strashko 
773df828598SMugunthan V N 	netif_carrier_off(ndev);
774df828598SMugunthan V N 
775e05107e6SIvan Khoronzhuk 	/* Notify the stack of the actual queue counts. */
776e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
777e05107e6SIvan Khoronzhuk 	if (ret) {
778e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of tx queues\n");
779e05107e6SIvan Khoronzhuk 		goto err_cleanup;
780e05107e6SIvan Khoronzhuk 	}
781e05107e6SIvan Khoronzhuk 
782e05107e6SIvan Khoronzhuk 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
783e05107e6SIvan Khoronzhuk 	if (ret) {
784e05107e6SIvan Khoronzhuk 		dev_err(priv->dev, "cannot set real number of rx queues\n");
785e05107e6SIvan Khoronzhuk 		goto err_cleanup;
786e05107e6SIvan Khoronzhuk 	}
787e05107e6SIvan Khoronzhuk 
7882a05a622SIvan Khoronzhuk 	reg = cpsw->version;
789df828598SMugunthan V N 
790df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
791df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
792df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
793df828598SMugunthan V N 
794d5bc1613SIvan Khoronzhuk 	/* Initialize host and slave ports */
795d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count)
796df828598SMugunthan V N 		cpsw_init_host_port(priv);
797df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
798df828598SMugunthan V N 
7993b72c2feSMugunthan V N 	/* Add default VLAN */
800606f3993SIvan Khoronzhuk 	if (!cpsw->data.dual_emac)
8013b72c2feSMugunthan V N 		cpsw_add_default_vlan(priv);
802e6afea0bSMugunthan V N 	else
8032a05a622SIvan Khoronzhuk 		cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
80461f1cef9SGrygorii Strashko 				  ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
8053b72c2feSMugunthan V N 
806d5bc1613SIvan Khoronzhuk 	/* initialize shared resources for every ndev */
807d5bc1613SIvan Khoronzhuk 	if (!cpsw->usage_count) {
808d9ba8f9eSMugunthan V N 		/* disable priority elevation */
809dda5f5feSGrygorii Strashko 		writel_relaxed(0, &cpsw->regs->ptype);
810df828598SMugunthan V N 
811d9ba8f9eSMugunthan V N 		/* enable statistics collection only on all ports */
812dda5f5feSGrygorii Strashko 		writel_relaxed(0x7, &cpsw->regs->stat_port_en);
813df828598SMugunthan V N 
8141923d6e4SMugunthan V N 		/* Enable internal fifo flow control */
8155d8d0d4dSIvan Khoronzhuk 		writel(0x7, &cpsw->regs->flow_control);
8161923d6e4SMugunthan V N 
817dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_rx);
818dbc4ec52SIvan Khoronzhuk 		napi_enable(&cpsw->napi_tx);
819d354eb85SMugunthan V N 
820e38b5a3dSIvan Khoronzhuk 		if (cpsw->tx_irq_disabled) {
821e38b5a3dSIvan Khoronzhuk 			cpsw->tx_irq_disabled = false;
822e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[1]);
8237da11600SMugunthan V N 		}
8247da11600SMugunthan V N 
825e38b5a3dSIvan Khoronzhuk 		if (cpsw->rx_irq_disabled) {
826e38b5a3dSIvan Khoronzhuk 			cpsw->rx_irq_disabled = false;
827e38b5a3dSIvan Khoronzhuk 			enable_irq(cpsw->irqs_table[0]);
8287da11600SMugunthan V N 		}
8297da11600SMugunthan V N 
8309ed4050cSIvan Khoronzhuk 		/* create rxqs for both infs in dual mac as they use same pool
8319ed4050cSIvan Khoronzhuk 		 * and must be destroyed together when no users.
8329ed4050cSIvan Khoronzhuk 		 */
8339ed4050cSIvan Khoronzhuk 		ret = cpsw_create_xdp_rxqs(cpsw);
8349ed4050cSIvan Khoronzhuk 		if (ret < 0)
8359ed4050cSIvan Khoronzhuk 			goto err_cleanup;
8369ed4050cSIvan Khoronzhuk 
8373802dce1SIvan Khoronzhuk 		ret = cpsw_fill_rx_channels(priv);
8383802dce1SIvan Khoronzhuk 		if (ret < 0)
839aacebbf8SSebastian Siewior 			goto err_cleanup;
840f280e89aSMugunthan V N 
8412b566873SGrygorii Strashko 		if (cpsw->cpts) {
8428a2c9a5aSGrygorii Strashko 			if (cpts_register(cpsw->cpts))
843f280e89aSMugunthan V N 				dev_err(priv->dev, "error registering cpts device\n");
8442b566873SGrygorii Strashko 			else
8452b566873SGrygorii Strashko 				writel(0x10, &cpsw->wr_regs->misc_en);
8462b566873SGrygorii Strashko 		}
847d9ba8f9eSMugunthan V N 	}
848df828598SMugunthan V N 
8494b4255edSIvan Khoronzhuk 	cpsw_restore(priv);
8504b4255edSIvan Khoronzhuk 
851ff5b8ef2SMugunthan V N 	/* Enable Interrupt pacing if configured */
8522a05a622SIvan Khoronzhuk 	if (cpsw->coal_intvl != 0) {
853ff5b8ef2SMugunthan V N 		struct ethtool_coalesce coal;
854ff5b8ef2SMugunthan V N 
8552a05a622SIvan Khoronzhuk 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
856ff5b8ef2SMugunthan V N 		cpsw_set_coalesce(ndev, &coal);
857ff5b8ef2SMugunthan V N 	}
858ff5b8ef2SMugunthan V N 
8592c836bd9SIvan Khoronzhuk 	cpdma_ctlr_start(cpsw->dma);
8602c836bd9SIvan Khoronzhuk 	cpsw_intr_enable(cpsw);
861d5bc1613SIvan Khoronzhuk 	cpsw->usage_count++;
862f63a975eSMugunthan V N 
863df828598SMugunthan V N 	return 0;
864df828598SMugunthan V N 
865aacebbf8SSebastian Siewior err_cleanup:
86602cacedeSIvan Khoronzhuk 	if (!cpsw->usage_count) {
8672c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
8689ed4050cSIvan Khoronzhuk 		cpsw_destroy_xdp_rxqs(cpsw);
86902cacedeSIvan Khoronzhuk 	}
87002cacedeSIvan Khoronzhuk 
8719ed4050cSIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
87256e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
873aacebbf8SSebastian Siewior 	netif_carrier_off(priv->ndev);
874aacebbf8SSebastian Siewior 	return ret;
875df828598SMugunthan V N }
876df828598SMugunthan V N 
877df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
878df828598SMugunthan V N {
879df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
880649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
881df828598SMugunthan V N 
882df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
88315180ecaSIvan Khoronzhuk 	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
884e05107e6SIvan Khoronzhuk 	netif_tx_stop_all_queues(priv->ndev);
885df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
886d9ba8f9eSMugunthan V N 
887d5bc1613SIvan Khoronzhuk 	if (cpsw->usage_count <= 1) {
888dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_rx);
889dbc4ec52SIvan Khoronzhuk 		napi_disable(&cpsw->napi_tx);
8902a05a622SIvan Khoronzhuk 		cpts_unregister(cpsw->cpts);
8912c836bd9SIvan Khoronzhuk 		cpsw_intr_disable(cpsw);
8922c836bd9SIvan Khoronzhuk 		cpdma_ctlr_stop(cpsw->dma);
8932a05a622SIvan Khoronzhuk 		cpsw_ale_stop(cpsw->ale);
8949ed4050cSIvan Khoronzhuk 		cpsw_destroy_xdp_rxqs(cpsw);
895d9ba8f9eSMugunthan V N 	}
8962a05a622SIvan Khoronzhuk 	for_each_slave(priv, cpsw_slave_stop, cpsw);
8970be01b8eSIvan Khoronzhuk 
8980be01b8eSIvan Khoronzhuk 	if (cpsw_need_resplit(cpsw))
8999763a891SGrygorii Strashko 		cpsw_split_res(cpsw);
9000be01b8eSIvan Khoronzhuk 
901d5bc1613SIvan Khoronzhuk 	cpsw->usage_count--;
90256e31bd8SIvan Khoronzhuk 	pm_runtime_put_sync(cpsw->dev);
903df828598SMugunthan V N 	return 0;
904df828598SMugunthan V N }
905df828598SMugunthan V N 
906df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
907df828598SMugunthan V N 				       struct net_device *ndev)
908df828598SMugunthan V N {
909df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
9102c836bd9SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
911f44f8417SIvan Khoronzhuk 	struct cpts *cpts = cpsw->cpts;
912e05107e6SIvan Khoronzhuk 	struct netdev_queue *txq;
913e05107e6SIvan Khoronzhuk 	struct cpdma_chan *txch;
914e05107e6SIvan Khoronzhuk 	int ret, q_idx;
915df828598SMugunthan V N 
916df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
917df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
9188dc43ddcSTobias Klauser 		ndev->stats.tx_dropped++;
9191bf96050SIvan Khoronzhuk 		return NET_XMIT_DROP;
920df828598SMugunthan V N 	}
921df828598SMugunthan V N 
9229232b16dSMugunthan V N 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
923a9423120SIvan Khoronzhuk 	    priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
9242e5b38abSRichard Cochran 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
9252e5b38abSRichard Cochran 
926e05107e6SIvan Khoronzhuk 	q_idx = skb_get_queue_mapping(skb);
927e05107e6SIvan Khoronzhuk 	if (q_idx >= cpsw->tx_ch_num)
928e05107e6SIvan Khoronzhuk 		q_idx = q_idx % cpsw->tx_ch_num;
929e05107e6SIvan Khoronzhuk 
9308feb0a19SIvan Khoronzhuk 	txch = cpsw->txv[q_idx].ch;
93162f94c21SGrygorii Strashko 	txq = netdev_get_tx_queue(ndev, q_idx);
93210ae8054SGrygorii Strashko 	skb_tx_timestamp(skb);
93310ae8054SGrygorii Strashko 	ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
93410ae8054SGrygorii Strashko 				priv->emac_port + cpsw->data.dual_emac);
935df828598SMugunthan V N 	if (unlikely(ret != 0)) {
936df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
937df828598SMugunthan V N 		goto fail;
938df828598SMugunthan V N 	}
939df828598SMugunthan V N 
940fae50823SMugunthan V N 	/* If there is no more tx desc left free then we need to
941fae50823SMugunthan V N 	 * tell the kernel to stop sending us tx frames.
942fae50823SMugunthan V N 	 */
943e05107e6SIvan Khoronzhuk 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
944e05107e6SIvan Khoronzhuk 		netif_tx_stop_queue(txq);
94562f94c21SGrygorii Strashko 
94662f94c21SGrygorii Strashko 		/* Barrier, so that stop_queue visible to other cpus */
94762f94c21SGrygorii Strashko 		smp_mb__after_atomic();
94862f94c21SGrygorii Strashko 
94962f94c21SGrygorii Strashko 		if (cpdma_check_free_tx_desc(txch))
95062f94c21SGrygorii Strashko 			netif_tx_wake_queue(txq);
951e05107e6SIvan Khoronzhuk 	}
952fae50823SMugunthan V N 
953df828598SMugunthan V N 	return NETDEV_TX_OK;
954df828598SMugunthan V N fail:
9558dc43ddcSTobias Klauser 	ndev->stats.tx_dropped++;
956e05107e6SIvan Khoronzhuk 	netif_tx_stop_queue(txq);
95762f94c21SGrygorii Strashko 
95862f94c21SGrygorii Strashko 	/* Barrier, so that stop_queue visible to other cpus */
95962f94c21SGrygorii Strashko 	smp_mb__after_atomic();
96062f94c21SGrygorii Strashko 
96162f94c21SGrygorii Strashko 	if (cpdma_check_free_tx_desc(txch))
96262f94c21SGrygorii Strashko 		netif_tx_wake_queue(txq);
96362f94c21SGrygorii Strashko 
964df828598SMugunthan V N 	return NETDEV_TX_BUSY;
965df828598SMugunthan V N }
966df828598SMugunthan V N 
967dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
968dcfd8d58SMugunthan V N {
969dcfd8d58SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
970dcfd8d58SMugunthan V N 	struct sockaddr *addr = (struct sockaddr *)p;
971649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
972dcfd8d58SMugunthan V N 	int flags = 0;
973dcfd8d58SMugunthan V N 	u16 vid = 0;
974a6c5d14fSGrygorii Strashko 	int ret;
975dcfd8d58SMugunthan V N 
976dcfd8d58SMugunthan V N 	if (!is_valid_ether_addr(addr->sa_data))
977dcfd8d58SMugunthan V N 		return -EADDRNOTAVAIL;
978dcfd8d58SMugunthan V N 
97956e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
980a6c5d14fSGrygorii Strashko 	if (ret < 0) {
98156e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
982a6c5d14fSGrygorii Strashko 		return ret;
983a6c5d14fSGrygorii Strashko 	}
984a6c5d14fSGrygorii Strashko 
985606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
986606f3993SIvan Khoronzhuk 		vid = cpsw->slaves[priv->emac_port].port_vlan;
987dcfd8d58SMugunthan V N 		flags = ALE_VLAN;
988dcfd8d58SMugunthan V N 	}
989dcfd8d58SMugunthan V N 
9902a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
991dcfd8d58SMugunthan V N 			   flags, vid);
9922a05a622SIvan Khoronzhuk 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
993dcfd8d58SMugunthan V N 			   flags, vid);
994dcfd8d58SMugunthan V N 
995dcfd8d58SMugunthan V N 	memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
996dcfd8d58SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
997dcfd8d58SMugunthan V N 	for_each_slave(priv, cpsw_set_slave_mac, priv);
998dcfd8d58SMugunthan V N 
99956e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
1000a6c5d14fSGrygorii Strashko 
1001dcfd8d58SMugunthan V N 	return 0;
1002dcfd8d58SMugunthan V N }
1003dcfd8d58SMugunthan V N 
10043b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
10053b72c2feSMugunthan V N 				unsigned short vid)
10063b72c2feSMugunthan V N {
10073b72c2feSMugunthan V N 	int ret;
10089f6bd8faSMugunthan V N 	int unreg_mcast_mask = 0;
10095b3a5a14SIvan Khoronzhuk 	int mcast_mask;
10109f6bd8faSMugunthan V N 	u32 port_mask;
1011606f3993SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
10129f6bd8faSMugunthan V N 
1013606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
10149f6bd8faSMugunthan V N 		port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
10159f6bd8faSMugunthan V N 
10165b3a5a14SIvan Khoronzhuk 		mcast_mask = ALE_PORT_HOST;
10179f6bd8faSMugunthan V N 		if (priv->ndev->flags & IFF_ALLMULTI)
10185b3a5a14SIvan Khoronzhuk 			unreg_mcast_mask = mcast_mask;
10199f6bd8faSMugunthan V N 	} else {
10209f6bd8faSMugunthan V N 		port_mask = ALE_ALL_PORTS;
10215b3a5a14SIvan Khoronzhuk 		mcast_mask = port_mask;
10221e5c4bc4SLennart Sorensen 
10231e5c4bc4SLennart Sorensen 		if (priv->ndev->flags & IFF_ALLMULTI)
10241e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_ALL_PORTS;
10251e5c4bc4SLennart Sorensen 		else
10261e5c4bc4SLennart Sorensen 			unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
10279f6bd8faSMugunthan V N 	}
10283b72c2feSMugunthan V N 
10292a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
103061f1cef9SGrygorii Strashko 				unreg_mcast_mask);
10313b72c2feSMugunthan V N 	if (ret != 0)
10323b72c2feSMugunthan V N 		return ret;
10333b72c2feSMugunthan V N 
10342a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
103571a2cbb7SGrygorii Strashko 				 HOST_PORT_NUM, ALE_VLAN, vid);
10363b72c2feSMugunthan V N 	if (ret != 0)
10373b72c2feSMugunthan V N 		goto clean_vid;
10383b72c2feSMugunthan V N 
10392a05a622SIvan Khoronzhuk 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
10405b3a5a14SIvan Khoronzhuk 				 mcast_mask, ALE_VLAN, vid, 0);
10413b72c2feSMugunthan V N 	if (ret != 0)
10423b72c2feSMugunthan V N 		goto clean_vlan_ucast;
10433b72c2feSMugunthan V N 	return 0;
10443b72c2feSMugunthan V N 
10453b72c2feSMugunthan V N clean_vlan_ucast:
10462a05a622SIvan Khoronzhuk 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
104771a2cbb7SGrygorii Strashko 			   HOST_PORT_NUM, ALE_VLAN, vid);
10483b72c2feSMugunthan V N clean_vid:
10492a05a622SIvan Khoronzhuk 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
10503b72c2feSMugunthan V N 	return ret;
10513b72c2feSMugunthan V N }
10523b72c2feSMugunthan V N 
10533b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
105480d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
10553b72c2feSMugunthan V N {
10563b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1057649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
1058a6c5d14fSGrygorii Strashko 	int ret;
10593b72c2feSMugunthan V N 
1060606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
10613b72c2feSMugunthan V N 		return 0;
10623b72c2feSMugunthan V N 
106356e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1064a6c5d14fSGrygorii Strashko 	if (ret < 0) {
106556e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1066a6c5d14fSGrygorii Strashko 		return ret;
1067a6c5d14fSGrygorii Strashko 	}
1068a6c5d14fSGrygorii Strashko 
1069606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
107002a54164SMugunthan V N 		/* In dual EMAC, reserved VLAN id should not be used for
107102a54164SMugunthan V N 		 * creating VLAN interfaces as this can break the dual
107202a54164SMugunthan V N 		 * EMAC port separation
107302a54164SMugunthan V N 		 */
107402a54164SMugunthan V N 		int i;
107502a54164SMugunthan V N 
1076606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
1077803c4f64SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan) {
1078803c4f64SIvan Khoronzhuk 				ret = -EINVAL;
1079803c4f64SIvan Khoronzhuk 				goto err;
1080803c4f64SIvan Khoronzhuk 			}
108102a54164SMugunthan V N 		}
108202a54164SMugunthan V N 	}
108302a54164SMugunthan V N 
10843b72c2feSMugunthan V N 	dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
1085a6c5d14fSGrygorii Strashko 	ret = cpsw_add_vlan_ale_entry(priv, vid);
1086803c4f64SIvan Khoronzhuk err:
108756e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
1088a6c5d14fSGrygorii Strashko 	return ret;
10893b72c2feSMugunthan V N }
10903b72c2feSMugunthan V N 
10913b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
109280d5c368SPatrick McHardy 				     __be16 proto, u16 vid)
10933b72c2feSMugunthan V N {
10943b72c2feSMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
1095649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = priv->cpsw;
10963b72c2feSMugunthan V N 	int ret;
10973b72c2feSMugunthan V N 
1098606f3993SIvan Khoronzhuk 	if (vid == cpsw->data.default_vlan)
10993b72c2feSMugunthan V N 		return 0;
11003b72c2feSMugunthan V N 
110156e31bd8SIvan Khoronzhuk 	ret = pm_runtime_get_sync(cpsw->dev);
1102a6c5d14fSGrygorii Strashko 	if (ret < 0) {
110356e31bd8SIvan Khoronzhuk 		pm_runtime_put_noidle(cpsw->dev);
1104a6c5d14fSGrygorii Strashko 		return ret;
1105a6c5d14fSGrygorii Strashko 	}
1106a6c5d14fSGrygorii Strashko 
1107606f3993SIvan Khoronzhuk 	if (cpsw->data.dual_emac) {
110802a54164SMugunthan V N 		int i;
110902a54164SMugunthan V N 
1110606f3993SIvan Khoronzhuk 		for (i = 0; i < cpsw->data.slaves; i++) {
1111606f3993SIvan Khoronzhuk 			if (vid == cpsw->slaves[i].port_vlan)
1112803c4f64SIvan Khoronzhuk 				goto err;
111302a54164SMugunthan V N 		}
111402a54164SMugunthan V N 	}
111502a54164SMugunthan V N 
11163b72c2feSMugunthan V N 	dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
11172a05a622SIvan Khoronzhuk 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1118be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
111961f1cef9SGrygorii Strashko 				  HOST_PORT_NUM, ALE_VLAN, vid);
1120be35b982SIvan Khoronzhuk 	ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
11213b72c2feSMugunthan V N 				  0, ALE_VLAN, vid);
112299d469fcSMurali Karicheri 	ret |= cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
1123803c4f64SIvan Khoronzhuk err:
112456e31bd8SIvan Khoronzhuk 	pm_runtime_put(cpsw->dev);
1125a6c5d14fSGrygorii Strashko 	return ret;
11263b72c2feSMugunthan V N }
11273b72c2feSMugunthan V N 
11289ed4050cSIvan Khoronzhuk static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
11299ed4050cSIvan Khoronzhuk 			     struct xdp_frame **frames, u32 flags)
11309ed4050cSIvan Khoronzhuk {
11319ed4050cSIvan Khoronzhuk 	struct cpsw_priv *priv = netdev_priv(ndev);
1132c5013ac1SGrygorii Strashko 	struct cpsw_common *cpsw = priv->cpsw;
11339ed4050cSIvan Khoronzhuk 	struct xdp_frame *xdpf;
1134c5013ac1SGrygorii Strashko 	int i, drops = 0, port;
11359ed4050cSIvan Khoronzhuk 
11369ed4050cSIvan Khoronzhuk 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
11379ed4050cSIvan Khoronzhuk 		return -EINVAL;
11389ed4050cSIvan Khoronzhuk 
11399ed4050cSIvan Khoronzhuk 	for (i = 0; i < n; i++) {
11409ed4050cSIvan Khoronzhuk 		xdpf = frames[i];
11419ed4050cSIvan Khoronzhuk 		if (xdpf->len < CPSW_MIN_PACKET_SIZE) {
11429ed4050cSIvan Khoronzhuk 			xdp_return_frame_rx_napi(xdpf);
11439ed4050cSIvan Khoronzhuk 			drops++;
11449ed4050cSIvan Khoronzhuk 			continue;
11459ed4050cSIvan Khoronzhuk 		}
11469ed4050cSIvan Khoronzhuk 
1147c5013ac1SGrygorii Strashko 		port = priv->emac_port + cpsw->data.dual_emac;
1148c5013ac1SGrygorii Strashko 		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, port))
11499ed4050cSIvan Khoronzhuk 			drops++;
11509ed4050cSIvan Khoronzhuk 	}
11519ed4050cSIvan Khoronzhuk 
11529ed4050cSIvan Khoronzhuk 	return n - drops;
11539ed4050cSIvan Khoronzhuk }
11549ed4050cSIvan Khoronzhuk 
1155026cc9c3SDavid S. Miller #ifdef CONFIG_NET_POLL_CONTROLLER
1156026cc9c3SDavid S. Miller static void cpsw_ndo_poll_controller(struct net_device *ndev)
1157026cc9c3SDavid S. Miller {
1158026cc9c3SDavid S. Miller 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1159026cc9c3SDavid S. Miller 
1160026cc9c3SDavid S. Miller 	cpsw_intr_disable(cpsw);
1161026cc9c3SDavid S. Miller 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1162026cc9c3SDavid S. Miller 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1163026cc9c3SDavid S. Miller 	cpsw_intr_enable(cpsw);
1164026cc9c3SDavid S. Miller }
1165026cc9c3SDavid S. Miller #endif
1166026cc9c3SDavid S. Miller 
1167df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
1168df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
1169df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
1170df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
1171dcfd8d58SMugunthan V N 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
11722e5b38abSRichard Cochran 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
1173df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
1174df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
11755c50a856SMugunthan V N 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
117683fcad0cSIvan Khoronzhuk 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
1177df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
1178df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
1179df828598SMugunthan V N #endif
11803b72c2feSMugunthan V N 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
11813b72c2feSMugunthan V N 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
11827929a668SIvan Khoronzhuk 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
11839ed4050cSIvan Khoronzhuk 	.ndo_bpf		= cpsw_ndo_bpf,
11849ed4050cSIvan Khoronzhuk 	.ndo_xdp_xmit		= cpsw_ndo_xdp_xmit,
1185df828598SMugunthan V N };
1186df828598SMugunthan V N 
1187df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
1188df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
1189df828598SMugunthan V N {
1190649a1688SIvan Khoronzhuk 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
119156e31bd8SIvan Khoronzhuk 	struct platform_device	*pdev = to_platform_device(cpsw->dev);
11927826d43fSJiri Pirko 
119352c4f0ecSMugunthan V N 	strlcpy(info->driver, "cpsw", sizeof(info->driver));
11947826d43fSJiri Pirko 	strlcpy(info->version, "1.0", sizeof(info->version));
119556e31bd8SIvan Khoronzhuk 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1196df828598SMugunthan V N }
1197df828598SMugunthan V N 
11981923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev,
11991923d6e4SMugunthan V N 			       struct ethtool_pauseparam *pause)
12001923d6e4SMugunthan V N {
12011923d6e4SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
12021923d6e4SMugunthan V N 	bool link;
12031923d6e4SMugunthan V N 
12041923d6e4SMugunthan V N 	priv->rx_pause = pause->rx_pause ? true : false;
12051923d6e4SMugunthan V N 	priv->tx_pause = pause->tx_pause ? true : false;
12061923d6e4SMugunthan V N 
12071923d6e4SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
12081923d6e4SMugunthan V N 	return 0;
12091923d6e4SMugunthan V N }
12101923d6e4SMugunthan V N 
1211022d7ad7SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev,
1212022d7ad7SIvan Khoronzhuk 			     struct ethtool_channels *chs)
1213022d7ad7SIvan Khoronzhuk {
1214c24eef28SGrygorii Strashko 	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1215be034fc1SGrygorii Strashko }
1216be034fc1SGrygorii Strashko 
1217df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
12183b6e1a4eSJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1219df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
1220df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
1221df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
1222df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
12232e5b38abSRichard Cochran 	.get_ts_info	= cpsw_get_ts_info,
1224ff5b8ef2SMugunthan V N 	.get_coalesce	= cpsw_get_coalesce,
1225ff5b8ef2SMugunthan V N 	.set_coalesce	= cpsw_set_coalesce,
1226d9718546SMugunthan V N 	.get_sset_count		= cpsw_get_sset_count,
1227d9718546SMugunthan V N 	.get_strings		= cpsw_get_strings,
1228d9718546SMugunthan V N 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
12291923d6e4SMugunthan V N 	.get_pauseparam		= cpsw_get_pauseparam,
12301923d6e4SMugunthan V N 	.set_pauseparam		= cpsw_set_pauseparam,
1231d8a64420SMatus Ujhelyi 	.get_wol	= cpsw_get_wol,
1232d8a64420SMatus Ujhelyi 	.set_wol	= cpsw_set_wol,
123352c4f0ecSMugunthan V N 	.get_regs_len	= cpsw_get_regs_len,
123452c4f0ecSMugunthan V N 	.get_regs	= cpsw_get_regs,
12357898b1daSGrygorii Strashko 	.begin		= cpsw_ethtool_op_begin,
12367898b1daSGrygorii Strashko 	.complete	= cpsw_ethtool_op_complete,
1237ce52c744SIvan Khoronzhuk 	.get_channels	= cpsw_get_channels,
1238ce52c744SIvan Khoronzhuk 	.set_channels	= cpsw_set_channels,
12392479876dSPhilippe Reynes 	.get_link_ksettings	= cpsw_get_link_ksettings,
12402479876dSPhilippe Reynes 	.set_link_ksettings	= cpsw_set_link_ksettings,
1241a0909949SYegor Yefremov 	.get_eee	= cpsw_get_eee,
1242a0909949SYegor Yefremov 	.set_eee	= cpsw_set_eee,
12436bb10c2bSYegor Yefremov 	.nway_reset	= cpsw_nway_reset,
1244be034fc1SGrygorii Strashko 	.get_ringparam = cpsw_get_ringparam,
1245be034fc1SGrygorii Strashko 	.set_ringparam = cpsw_set_ringparam,
1246df828598SMugunthan V N };
1247df828598SMugunthan V N 
1248552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data,
12492eb32b0aSMugunthan V N 			 struct platform_device *pdev)
12502eb32b0aSMugunthan V N {
12512eb32b0aSMugunthan V N 	struct device_node *node = pdev->dev.of_node;
12522eb32b0aSMugunthan V N 	struct device_node *slave_node;
12532eb32b0aSMugunthan V N 	int i = 0, ret;
12542eb32b0aSMugunthan V N 	u32 prop;
12552eb32b0aSMugunthan V N 
12562eb32b0aSMugunthan V N 	if (!node)
12572eb32b0aSMugunthan V N 		return -EINVAL;
12582eb32b0aSMugunthan V N 
12592eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "slaves", &prop)) {
126088c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
12612eb32b0aSMugunthan V N 		return -EINVAL;
12622eb32b0aSMugunthan V N 	}
12632eb32b0aSMugunthan V N 	data->slaves = prop;
12642eb32b0aSMugunthan V N 
1265e86ac13bSMugunthan V N 	if (of_property_read_u32(node, "active_slave", &prop)) {
126688c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
1267aa1a15e2SDaniel Mack 		return -EINVAL;
126878ca0b28SRichard Cochran 	}
1269e86ac13bSMugunthan V N 	data->active_slave = prop;
127078ca0b28SRichard Cochran 
1271a86854d0SKees Cook 	data->slave_data = devm_kcalloc(&pdev->dev,
1272a86854d0SKees Cook 					data->slaves,
1273a86854d0SKees Cook 					sizeof(struct cpsw_slave_data),
1274b2adaca9SJoe Perches 					GFP_KERNEL);
1275b2adaca9SJoe Perches 	if (!data->slave_data)
1276aa1a15e2SDaniel Mack 		return -ENOMEM;
12772eb32b0aSMugunthan V N 
12782eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "cpdma_channels", &prop)) {
127988c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
1280aa1a15e2SDaniel Mack 		return -EINVAL;
12812eb32b0aSMugunthan V N 	}
12822eb32b0aSMugunthan V N 	data->channels = prop;
12832eb32b0aSMugunthan V N 
12842eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "bd_ram_size", &prop)) {
128588c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
1286aa1a15e2SDaniel Mack 		return -EINVAL;
12872eb32b0aSMugunthan V N 	}
12882eb32b0aSMugunthan V N 	data->bd_ram_size = prop;
12892eb32b0aSMugunthan V N 
12902eb32b0aSMugunthan V N 	if (of_property_read_u32(node, "mac_control", &prop)) {
129188c99ff6SGeorge Cherian 		dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
1292aa1a15e2SDaniel Mack 		return -EINVAL;
12932eb32b0aSMugunthan V N 	}
12942eb32b0aSMugunthan V N 	data->mac_control = prop;
12952eb32b0aSMugunthan V N 
1296281abd96SMarkus Pargmann 	if (of_property_read_bool(node, "dual_emac"))
1297a78766d9SJason Yan 		data->dual_emac = true;
1298d9ba8f9eSMugunthan V N 
12991fb19aa7SVaibhav Hiremath 	/*
13001fb19aa7SVaibhav Hiremath 	 * Populate all the child nodes here...
13011fb19aa7SVaibhav Hiremath 	 */
13021fb19aa7SVaibhav Hiremath 	ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
13031fb19aa7SVaibhav Hiremath 	/* We do not want to force this, as in some cases may not have child */
13041fb19aa7SVaibhav Hiremath 	if (ret)
130588c99ff6SGeorge Cherian 		dev_warn(&pdev->dev, "Doesn't have any child node\n");
13061fb19aa7SVaibhav Hiremath 
13078658aaf2SBen Hutchings 	for_each_available_child_of_node(node, slave_node) {
1308549985eeSRichard Cochran 		struct cpsw_slave_data *slave_data = data->slave_data + i;
1309549985eeSRichard Cochran 		const void *mac_addr = NULL;
1310549985eeSRichard Cochran 		int lenp;
1311549985eeSRichard Cochran 		const __be32 *parp;
1312549985eeSRichard Cochran 
1313f468b10eSMarkus Pargmann 		/* This is no slave child node, continue */
1314bf5849f1SRob Herring 		if (!of_node_name_eq(slave_node, "slave"))
1315f468b10eSMarkus Pargmann 			continue;
1316f468b10eSMarkus Pargmann 
13173ff18849SGrygorii Strashko 		slave_data->ifphy = devm_of_phy_get(&pdev->dev, slave_node,
13183ff18849SGrygorii Strashko 						    NULL);
13193ff18849SGrygorii Strashko 		if (!IS_ENABLED(CONFIG_TI_CPSW_PHY_SEL) &&
13203ff18849SGrygorii Strashko 		    IS_ERR(slave_data->ifphy)) {
13213ff18849SGrygorii Strashko 			ret = PTR_ERR(slave_data->ifphy);
13223ff18849SGrygorii Strashko 			dev_err(&pdev->dev,
13233ff18849SGrygorii Strashko 				"%d: Error retrieving port phy: %d\n", i, ret);
13243cd6e20fSNishka Dasgupta 			goto err_node_put;
13253ff18849SGrygorii Strashko 		}
13263ff18849SGrygorii Strashko 
1327337d1727SMarek Vasut 		slave_data->slave_node = slave_node;
1328552165bcSDavid Rivshin 		slave_data->phy_node = of_parse_phandle(slave_node,
1329552165bcSDavid Rivshin 							"phy-handle", 0);
1330f1eea5c1SDavid Rivshin 		parp = of_get_property(slave_node, "phy_id", &lenp);
1331ae092b5bSDavid Rivshin 		if (slave_data->phy_node) {
1332ae092b5bSDavid Rivshin 			dev_dbg(&pdev->dev,
1333f7ce9103SRob Herring 				"slave[%d] using phy-handle=\"%pOF\"\n",
1334f7ce9103SRob Herring 				i, slave_data->phy_node);
1335ae092b5bSDavid Rivshin 		} else if (of_phy_is_fixed_link(slave_node)) {
1336dfc0a6d3SDavid Rivshin 			/* In the case of a fixed PHY, the DT node associated
1337dfc0a6d3SDavid Rivshin 			 * to the PHY is the Ethernet MAC DT node.
1338dfc0a6d3SDavid Rivshin 			 */
13391f71e8c9SMarkus Brunner 			ret = of_phy_register_fixed_link(slave_node);
134023a09873SJohan Hovold 			if (ret) {
134123a09873SJohan Hovold 				if (ret != -EPROBE_DEFER)
134223a09873SJohan Hovold 					dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
13433cd6e20fSNishka Dasgupta 				goto err_node_put;
134423a09873SJohan Hovold 			}
134506cd6d6eSDavid Rivshin 			slave_data->phy_node = of_node_get(slave_node);
1346f1eea5c1SDavid Rivshin 		} else if (parp) {
1347f1eea5c1SDavid Rivshin 			u32 phyid;
1348f1eea5c1SDavid Rivshin 			struct device_node *mdio_node;
1349f1eea5c1SDavid Rivshin 			struct platform_device *mdio;
1350f1eea5c1SDavid Rivshin 
1351f1eea5c1SDavid Rivshin 			if (lenp != (sizeof(__be32) * 2)) {
1352f1eea5c1SDavid Rivshin 				dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
135347276fccSMugunthan V N 				goto no_phy_slave;
1354549985eeSRichard Cochran 			}
1355549985eeSRichard Cochran 			mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
1356549985eeSRichard Cochran 			phyid = be32_to_cpup(parp+1);
1357549985eeSRichard Cochran 			mdio = of_find_device_by_node(mdio_node);
135860e71ab5SJohan Hovold 			of_node_put(mdio_node);
13596954cc1fSJohan Hovold 			if (!mdio) {
136056fdb2e0SMarkus Pargmann 				dev_err(&pdev->dev, "Missing mdio platform device\n");
13613cd6e20fSNishka Dasgupta 				ret = -EINVAL;
13623cd6e20fSNishka Dasgupta 				goto err_node_put;
13636954cc1fSJohan Hovold 			}
1364549985eeSRichard Cochran 			snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
1365549985eeSRichard Cochran 				 PHY_ID_FMT, mdio->name, phyid);
136686e1d5adSJohan Hovold 			put_device(&mdio->dev);
1367f1eea5c1SDavid Rivshin 		} else {
1368ae092b5bSDavid Rivshin 			dev_err(&pdev->dev,
1369ae092b5bSDavid Rivshin 				"No slave[%d] phy_id, phy-handle, or fixed-link property\n",
1370ae092b5bSDavid Rivshin 				i);
1371f1eea5c1SDavid Rivshin 			goto no_phy_slave;
1372f1eea5c1SDavid Rivshin 		}
13730c65b2b9SAndrew Lunn 		ret = of_get_phy_mode(slave_node, &slave_data->phy_if);
13740c65b2b9SAndrew Lunn 		if (ret) {
137547276fccSMugunthan V N 			dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
137647276fccSMugunthan V N 				i);
13773cd6e20fSNishka Dasgupta 			goto err_node_put;
137847276fccSMugunthan V N 		}
137947276fccSMugunthan V N 
138047276fccSMugunthan V N no_phy_slave:
1381549985eeSRichard Cochran 		mac_addr = of_get_mac_address(slave_node);
1382a51645f7SPetr Štetiar 		if (!IS_ERR(mac_addr)) {
13832d2924afSPetr Štetiar 			ether_addr_copy(slave_data->mac_addr, mac_addr);
13840ba517b1SMarkus Pargmann 		} else {
1385b6745f6eSMugunthan V N 			ret = ti_cm_get_macid(&pdev->dev, i,
13860ba517b1SMarkus Pargmann 					      slave_data->mac_addr);
13870ba517b1SMarkus Pargmann 			if (ret)
13883cd6e20fSNishka Dasgupta 				goto err_node_put;
13890ba517b1SMarkus Pargmann 		}
1390d9ba8f9eSMugunthan V N 		if (data->dual_emac) {
139191c4166cSMugunthan V N 			if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
1392d9ba8f9eSMugunthan V N 						 &prop)) {
139388c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
1394d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = i+1;
139588c99ff6SGeorge Cherian 				dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
1396d9ba8f9eSMugunthan V N 					slave_data->dual_emac_res_vlan, i);
1397d9ba8f9eSMugunthan V N 			} else {
1398d9ba8f9eSMugunthan V N 				slave_data->dual_emac_res_vlan = prop;
1399d9ba8f9eSMugunthan V N 			}
1400d9ba8f9eSMugunthan V N 		}
1401d9ba8f9eSMugunthan V N 
1402549985eeSRichard Cochran 		i++;
14033cd6e20fSNishka Dasgupta 		if (i == data->slaves) {
14043cd6e20fSNishka Dasgupta 			ret = 0;
14053cd6e20fSNishka Dasgupta 			goto err_node_put;
14063cd6e20fSNishka Dasgupta 		}
1407549985eeSRichard Cochran 	}
1408549985eeSRichard Cochran 
14092eb32b0aSMugunthan V N 	return 0;
14103cd6e20fSNishka Dasgupta 
14113cd6e20fSNishka Dasgupta err_node_put:
14123cd6e20fSNishka Dasgupta 	of_node_put(slave_node);
14133cd6e20fSNishka Dasgupta 	return ret;
14142eb32b0aSMugunthan V N }
14152eb32b0aSMugunthan V N 
1416a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev)
1417a4e32b0dSJohan Hovold {
1418bfe59032SIvan Khoronzhuk 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
14198cbcc466SJohan Hovold 	struct cpsw_platform_data *data = &cpsw->data;
14208cbcc466SJohan Hovold 	struct device_node *node = pdev->dev.of_node;
14218cbcc466SJohan Hovold 	struct device_node *slave_node;
14228cbcc466SJohan Hovold 	int i = 0;
14238cbcc466SJohan Hovold 
14248cbcc466SJohan Hovold 	for_each_available_child_of_node(node, slave_node) {
14258cbcc466SJohan Hovold 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
14268cbcc466SJohan Hovold 
1427bf5849f1SRob Herring 		if (!of_node_name_eq(slave_node, "slave"))
14288cbcc466SJohan Hovold 			continue;
14298cbcc466SJohan Hovold 
14303f65047cSJohan Hovold 		if (of_phy_is_fixed_link(slave_node))
14313f65047cSJohan Hovold 			of_phy_deregister_fixed_link(slave_node);
14328cbcc466SJohan Hovold 
14338cbcc466SJohan Hovold 		of_node_put(slave_data->phy_node);
14348cbcc466SJohan Hovold 
14358cbcc466SJohan Hovold 		i++;
14363cd6e20fSNishka Dasgupta 		if (i == data->slaves) {
14373cd6e20fSNishka Dasgupta 			of_node_put(slave_node);
14388cbcc466SJohan Hovold 			break;
14398cbcc466SJohan Hovold 		}
14403cd6e20fSNishka Dasgupta 	}
14418cbcc466SJohan Hovold 
1442a4e32b0dSJohan Hovold 	of_platform_depopulate(&pdev->dev);
1443a4e32b0dSJohan Hovold }
1444a4e32b0dSJohan Hovold 
144556e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
1446d9ba8f9eSMugunthan V N {
1447606f3993SIvan Khoronzhuk 	struct cpsw_common		*cpsw = priv->cpsw;
1448606f3993SIvan Khoronzhuk 	struct cpsw_platform_data	*data = &cpsw->data;
1449d9ba8f9eSMugunthan V N 	struct net_device		*ndev;
1450d9ba8f9eSMugunthan V N 	struct cpsw_priv		*priv_sl2;
1451e38b5a3dSIvan Khoronzhuk 	int ret = 0;
1452d9ba8f9eSMugunthan V N 
1453d183a942SGrygorii Strashko 	ndev = devm_alloc_etherdev_mqs(cpsw->dev, sizeof(struct cpsw_priv),
1454d183a942SGrygorii Strashko 				       CPSW_MAX_QUEUES, CPSW_MAX_QUEUES);
1455d9ba8f9eSMugunthan V N 	if (!ndev) {
145656e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
1457d9ba8f9eSMugunthan V N 		return -ENOMEM;
1458d9ba8f9eSMugunthan V N 	}
1459d9ba8f9eSMugunthan V N 
1460d9ba8f9eSMugunthan V N 	priv_sl2 = netdev_priv(ndev);
1461606f3993SIvan Khoronzhuk 	priv_sl2->cpsw = cpsw;
1462d9ba8f9eSMugunthan V N 	priv_sl2->ndev = ndev;
1463d9ba8f9eSMugunthan V N 	priv_sl2->dev  = &ndev->dev;
1464d9ba8f9eSMugunthan V N 	priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1465d9ba8f9eSMugunthan V N 
1466d9ba8f9eSMugunthan V N 	if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
1467d9ba8f9eSMugunthan V N 		memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
1468d9ba8f9eSMugunthan V N 			ETH_ALEN);
146956e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
147056e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
1471d9ba8f9eSMugunthan V N 	} else {
14726c1f0a1fSJoe Perches 		eth_random_addr(priv_sl2->mac_addr);
147356e31bd8SIvan Khoronzhuk 		dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
147456e31bd8SIvan Khoronzhuk 			 priv_sl2->mac_addr);
1475d9ba8f9eSMugunthan V N 	}
1476d9ba8f9eSMugunthan V N 	memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
1477d9ba8f9eSMugunthan V N 
1478d9ba8f9eSMugunthan V N 	priv_sl2->emac_port = 1;
1479606f3993SIvan Khoronzhuk 	cpsw->slaves[1].ndev = ndev;
1480193736c8SIvan Khoronzhuk 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
1481d9ba8f9eSMugunthan V N 
1482d9ba8f9eSMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
14837ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &cpsw_ethtool_ops;
1484d9ba8f9eSMugunthan V N 
1485d9ba8f9eSMugunthan V N 	/* register the network device */
148656e31bd8SIvan Khoronzhuk 	SET_NETDEV_DEV(ndev, cpsw->dev);
1487337d1727SMarek Vasut 	ndev->dev.of_node = cpsw->slaves[1].data->slave_node;
1488d9ba8f9eSMugunthan V N 	ret = register_netdev(ndev);
1489d183a942SGrygorii Strashko 	if (ret)
149056e31bd8SIvan Khoronzhuk 		dev_err(cpsw->dev, "cpsw: error registering net device\n");
1491d9ba8f9eSMugunthan V N 
1492d9ba8f9eSMugunthan V N 	return ret;
1493d9ba8f9eSMugunthan V N }
1494d9ba8f9eSMugunthan V N 
14957da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = {
14969611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,cpsw"},
14979611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am335x-cpsw"},
14989611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,am4372-cpsw"},
14999611d6d6SIvan Khoronzhuk 	{ .compatible = "ti,dra7-cpsw"},
15007da11600SMugunthan V N 	{ /* sentinel */ },
15017da11600SMugunthan V N };
15027da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
15037da11600SMugunthan V N 
15049611d6d6SIvan Khoronzhuk static const struct soc_device_attribute cpsw_soc_devices[] = {
15059611d6d6SIvan Khoronzhuk 	{ .family = "AM33xx", .revision = "ES1.0"},
15069611d6d6SIvan Khoronzhuk 	{ /* sentinel */ }
15079611d6d6SIvan Khoronzhuk };
15089611d6d6SIvan Khoronzhuk 
1509663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev)
1510df828598SMugunthan V N {
1511c8fb5668SGrygorii Strashko 	struct device			*dev = &pdev->dev;
1512ef4183a1SIvan Khoronzhuk 	struct clk			*clk;
1513d1bd9acfSSebastian Siewior 	struct cpsw_platform_data	*data;
1514df828598SMugunthan V N 	struct net_device		*ndev;
1515df828598SMugunthan V N 	struct cpsw_priv		*priv;
1516aa1a15e2SDaniel Mack 	void __iomem			*ss_regs;
1517c8ace62fSYueHaibing 	struct resource			*ss_res;
15181d147ccbSMugunthan V N 	struct gpio_descs		*mode;
15199611d6d6SIvan Khoronzhuk 	const struct soc_device_attribute *soc;
1520649a1688SIvan Khoronzhuk 	struct cpsw_common		*cpsw;
1521e6a84624SGrygorii Strashko 	int ret = 0, ch;
15225087b915SFelipe Balbi 	int irq;
1523df828598SMugunthan V N 
1524c8fb5668SGrygorii Strashko 	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
15253420ea88SJohan Hovold 	if (!cpsw)
15263420ea88SJohan Hovold 		return -ENOMEM;
15273420ea88SJohan Hovold 
15282d683eaaSAntoine Tenart 	platform_set_drvdata(pdev, cpsw);
152951a95337SGrygorii Strashko 	cpsw_slave_index = cpsw_slave_index_priv;
153051a95337SGrygorii Strashko 
1531c8fb5668SGrygorii Strashko 	cpsw->dev = dev;
1532649a1688SIvan Khoronzhuk 
1533c8fb5668SGrygorii Strashko 	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
15341d147ccbSMugunthan V N 	if (IS_ERR(mode)) {
15351d147ccbSMugunthan V N 		ret = PTR_ERR(mode);
1536c8fb5668SGrygorii Strashko 		dev_err(dev, "gpio request failed, ret %d\n", ret);
1537d183a942SGrygorii Strashko 		return ret;
15381d147ccbSMugunthan V N 	}
15391d147ccbSMugunthan V N 
154083a8471bSGrygorii Strashko 	clk = devm_clk_get(dev, "fck");
154183a8471bSGrygorii Strashko 	if (IS_ERR(clk)) {
1542ac97a359SYueHaibing 		ret = PTR_ERR(clk);
154383a8471bSGrygorii Strashko 		dev_err(dev, "fck is not found %d\n", ret);
154483a8471bSGrygorii Strashko 		return ret;
154583a8471bSGrygorii Strashko 	}
154683a8471bSGrygorii Strashko 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
154783a8471bSGrygorii Strashko 
154883a8471bSGrygorii Strashko 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
154983a8471bSGrygorii Strashko 	ss_regs = devm_ioremap_resource(dev, ss_res);
155083a8471bSGrygorii Strashko 	if (IS_ERR(ss_regs))
155183a8471bSGrygorii Strashko 		return PTR_ERR(ss_regs);
155283a8471bSGrygorii Strashko 	cpsw->regs = ss_regs;
155383a8471bSGrygorii Strashko 
1554c8ace62fSYueHaibing 	cpsw->wr_regs = devm_platform_ioremap_resource(pdev, 1);
155583a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->wr_regs))
155683a8471bSGrygorii Strashko 		return PTR_ERR(cpsw->wr_regs);
155783a8471bSGrygorii Strashko 
155883a8471bSGrygorii Strashko 	/* RX IRQ */
155983a8471bSGrygorii Strashko 	irq = platform_get_irq(pdev, 1);
156083a8471bSGrygorii Strashko 	if (irq < 0)
156183a8471bSGrygorii Strashko 		return irq;
156283a8471bSGrygorii Strashko 	cpsw->irqs_table[0] = irq;
156383a8471bSGrygorii Strashko 
156483a8471bSGrygorii Strashko 	/* TX IRQ */
156583a8471bSGrygorii Strashko 	irq = platform_get_irq(pdev, 2);
156683a8471bSGrygorii Strashko 	if (irq < 0)
156783a8471bSGrygorii Strashko 		return irq;
156883a8471bSGrygorii Strashko 	cpsw->irqs_table[1] = irq;
156983a8471bSGrygorii Strashko 
157084ea9c0aSGrygorii Strashko 	/* get misc irq*/
157184ea9c0aSGrygorii Strashko 	irq = platform_get_irq(pdev, 3);
157284ea9c0aSGrygorii Strashko 	if (irq <= 0)
157384ea9c0aSGrygorii Strashko 		return irq;
157484ea9c0aSGrygorii Strashko 	cpsw->misc_irq = irq;
157584ea9c0aSGrygorii Strashko 
15761fb19aa7SVaibhav Hiremath 	/*
15771fb19aa7SVaibhav Hiremath 	 * This may be required here for child devices.
15781fb19aa7SVaibhav Hiremath 	 */
1579c8fb5668SGrygorii Strashko 	pm_runtime_enable(dev);
15801fb19aa7SVaibhav Hiremath 
1581a4e32b0dSJohan Hovold 	/* Need to enable clocks with runtime PM api to access module
1582a4e32b0dSJohan Hovold 	 * registers
1583a4e32b0dSJohan Hovold 	 */
1584c8fb5668SGrygorii Strashko 	ret = pm_runtime_get_sync(dev);
1585a4e32b0dSJohan Hovold 	if (ret < 0) {
1586c8fb5668SGrygorii Strashko 		pm_runtime_put_noidle(dev);
1587aa1a15e2SDaniel Mack 		goto clean_runtime_disable_ret;
15882eb32b0aSMugunthan V N 	}
1589a4e32b0dSJohan Hovold 
159023a09873SJohan Hovold 	ret = cpsw_probe_dt(&cpsw->data, pdev);
159123a09873SJohan Hovold 	if (ret)
1592a4e32b0dSJohan Hovold 		goto clean_dt_ret;
159323a09873SJohan Hovold 
159483a8471bSGrygorii Strashko 	soc = soc_device_match(cpsw_soc_devices);
159583a8471bSGrygorii Strashko 	if (soc)
1596a78766d9SJason Yan 		cpsw->quirk_irq = true;
159783a8471bSGrygorii Strashko 
1598606f3993SIvan Khoronzhuk 	data = &cpsw->data;
1599c8fb5668SGrygorii Strashko 	cpsw->slaves = devm_kcalloc(dev,
1600a86854d0SKees Cook 				    data->slaves, sizeof(struct cpsw_slave),
1601df828598SMugunthan V N 				    GFP_KERNEL);
1602606f3993SIvan Khoronzhuk 	if (!cpsw->slaves) {
1603aa1a15e2SDaniel Mack 		ret = -ENOMEM;
1604a4e32b0dSJohan Hovold 		goto clean_dt_ret;
1605df828598SMugunthan V N 	}
1606df828598SMugunthan V N 
160783a8471bSGrygorii Strashko 	cpsw->rx_packet_max = max(rx_packet_max, CPSW_MAX_PACKET_SIZE);
1608c24eef28SGrygorii Strashko 	cpsw->descs_pool_size = descs_pool_size;
1609df828598SMugunthan V N 
1610e6a84624SGrygorii Strashko 	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1611e6a84624SGrygorii Strashko 			       ss_res->start + CPSW2_BD_OFFSET,
1612e6a84624SGrygorii Strashko 			       descs_pool_size);
1613e6a84624SGrygorii Strashko 	if (ret)
1614a4e32b0dSJohan Hovold 		goto clean_dt_ret;
16158a2c9a5aSGrygorii Strashko 
161683a8471bSGrygorii Strashko 	ch = cpsw->quirk_irq ? 0 : 7;
161783a8471bSGrygorii Strashko 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
161883a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->txv[0].ch)) {
161983a8471bSGrygorii Strashko 		dev_err(dev, "error initializing tx dma channel\n");
162083a8471bSGrygorii Strashko 		ret = PTR_ERR(cpsw->txv[0].ch);
162183a8471bSGrygorii Strashko 		goto clean_cpts;
1622df828598SMugunthan V N 	}
1623df828598SMugunthan V N 
162483a8471bSGrygorii Strashko 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
162583a8471bSGrygorii Strashko 	if (IS_ERR(cpsw->rxv[0].ch)) {
162683a8471bSGrygorii Strashko 		dev_err(dev, "error initializing rx dma channel\n");
162783a8471bSGrygorii Strashko 		ret = PTR_ERR(cpsw->rxv[0].ch);
162883a8471bSGrygorii Strashko 		goto clean_cpts;
162983a8471bSGrygorii Strashko 	}
163083a8471bSGrygorii Strashko 	cpsw_split_res(cpsw);
163183a8471bSGrygorii Strashko 
163283a8471bSGrygorii Strashko 	/* setup netdev */
163383a8471bSGrygorii Strashko 	ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
163483a8471bSGrygorii Strashko 				       CPSW_MAX_QUEUES, CPSW_MAX_QUEUES);
163583a8471bSGrygorii Strashko 	if (!ndev) {
163683a8471bSGrygorii Strashko 		dev_err(dev, "error allocating net_device\n");
163735f735c6SZhang Changzhong 		ret = -ENOMEM;
163883a8471bSGrygorii Strashko 		goto clean_cpts;
163983a8471bSGrygorii Strashko 	}
164083a8471bSGrygorii Strashko 
164183a8471bSGrygorii Strashko 	priv = netdev_priv(ndev);
164283a8471bSGrygorii Strashko 	priv->cpsw = cpsw;
164383a8471bSGrygorii Strashko 	priv->ndev = ndev;
164483a8471bSGrygorii Strashko 	priv->dev  = dev;
164583a8471bSGrygorii Strashko 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
164683a8471bSGrygorii Strashko 	priv->emac_port = 0;
164783a8471bSGrygorii Strashko 
164883a8471bSGrygorii Strashko 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
164983a8471bSGrygorii Strashko 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
165083a8471bSGrygorii Strashko 		dev_info(dev, "Detected MACID = %pM\n", priv->mac_addr);
165183a8471bSGrygorii Strashko 	} else {
165283a8471bSGrygorii Strashko 		eth_random_addr(priv->mac_addr);
165383a8471bSGrygorii Strashko 		dev_info(dev, "Random MACID = %pM\n", priv->mac_addr);
165483a8471bSGrygorii Strashko 	}
165583a8471bSGrygorii Strashko 
165683a8471bSGrygorii Strashko 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
165783a8471bSGrygorii Strashko 
165883a8471bSGrygorii Strashko 	cpsw->slaves[0].ndev = ndev;
165983a8471bSGrygorii Strashko 
1660a3a41d2fSGrygorii Strashko 	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
1661070f9c65SKeerthy 
1662070f9c65SKeerthy 	ndev->netdev_ops = &cpsw_netdev_ops;
1663070f9c65SKeerthy 	ndev->ethtool_ops = &cpsw_ethtool_ops;
16649611d6d6SIvan Khoronzhuk 	netif_napi_add(ndev, &cpsw->napi_rx,
16659611d6d6SIvan Khoronzhuk 		       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
16669611d6d6SIvan Khoronzhuk 		       CPSW_POLL_WEIGHT);
16679611d6d6SIvan Khoronzhuk 	netif_tx_napi_add(ndev, &cpsw->napi_tx,
16689611d6d6SIvan Khoronzhuk 			  cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
16699611d6d6SIvan Khoronzhuk 			  CPSW_POLL_WEIGHT);
1670070f9c65SKeerthy 
1671070f9c65SKeerthy 	/* register the network device */
1672c8fb5668SGrygorii Strashko 	SET_NETDEV_DEV(ndev, dev);
1673337d1727SMarek Vasut 	ndev->dev.of_node = cpsw->slaves[0].data->slave_node;
1674070f9c65SKeerthy 	ret = register_netdev(ndev);
1675070f9c65SKeerthy 	if (ret) {
1676c8fb5668SGrygorii Strashko 		dev_err(dev, "error registering net device\n");
1677070f9c65SKeerthy 		ret = -ENODEV;
167883a8471bSGrygorii Strashko 		goto clean_cpts;
1679070f9c65SKeerthy 	}
1680070f9c65SKeerthy 
1681070f9c65SKeerthy 	if (cpsw->data.dual_emac) {
1682070f9c65SKeerthy 		ret = cpsw_probe_dual_emac(priv);
1683070f9c65SKeerthy 		if (ret) {
1684070f9c65SKeerthy 			cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
1685070f9c65SKeerthy 			goto clean_unregister_netdev_ret;
1686070f9c65SKeerthy 		}
1687070f9c65SKeerthy 	}
1688070f9c65SKeerthy 
1689c03abd84SFelipe Balbi 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1690c03abd84SFelipe Balbi 	 * MISC IRQs which are always kept disabled with this driver so
1691c03abd84SFelipe Balbi 	 * we will not request them.
1692c03abd84SFelipe Balbi 	 *
1693c03abd84SFelipe Balbi 	 * If anyone wants to implement support for those, make sure to
1694c03abd84SFelipe Balbi 	 * first request and append them to irqs_table array.
1695c03abd84SFelipe Balbi 	 */
169683a8471bSGrygorii Strashko 	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1697c8fb5668SGrygorii Strashko 			       0, dev_name(dev), cpsw);
16985087b915SFelipe Balbi 	if (ret < 0) {
1699c8fb5668SGrygorii Strashko 		dev_err(dev, "error attaching irq (%d)\n", ret);
170083a8471bSGrygorii Strashko 		goto clean_unregister_netdev_ret;
1701df828598SMugunthan V N 	}
1702df828598SMugunthan V N 
17035087b915SFelipe Balbi 
170483a8471bSGrygorii Strashko 	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1705dbc4ec52SIvan Khoronzhuk 			       0, dev_name(&pdev->dev), cpsw);
17065087b915SFelipe Balbi 	if (ret < 0) {
1707c8fb5668SGrygorii Strashko 		dev_err(dev, "error attaching irq (%d)\n", ret);
170883a8471bSGrygorii Strashko 		goto clean_unregister_netdev_ret;
17095087b915SFelipe Balbi 	}
1710c2b32e58SDaniel Mack 
171184ea9c0aSGrygorii Strashko 	if (!cpsw->cpts)
171284ea9c0aSGrygorii Strashko 		goto skip_cpts;
171384ea9c0aSGrygorii Strashko 
171484ea9c0aSGrygorii Strashko 	ret = devm_request_irq(&pdev->dev, cpsw->misc_irq, cpsw_misc_interrupt,
171584ea9c0aSGrygorii Strashko 			       0, dev_name(&pdev->dev), cpsw);
171684ea9c0aSGrygorii Strashko 	if (ret < 0) {
171784ea9c0aSGrygorii Strashko 		dev_err(dev, "error attaching misc irq (%d)\n", ret);
171884ea9c0aSGrygorii Strashko 		goto clean_unregister_netdev_ret;
171984ea9c0aSGrygorii Strashko 	}
172084ea9c0aSGrygorii Strashko 
172184ea9c0aSGrygorii Strashko 	/* Enable misc CPTS evnt_pend IRQ */
172284ea9c0aSGrygorii Strashko 	cpts_set_irqpoll(cpsw->cpts, false);
172384ea9c0aSGrygorii Strashko 
172484ea9c0aSGrygorii Strashko skip_cpts:
172590225bf0SGrygorii Strashko 	cpsw_notice(priv, probe,
172690225bf0SGrygorii Strashko 		    "initialized device (regs %pa, irq %d, pool size %d)\n",
172783a8471bSGrygorii Strashko 		    &ss_res->start, cpsw->irqs_table[0], descs_pool_size);
1728d9ba8f9eSMugunthan V N 
1729c46ab7e0SJohan Hovold 	pm_runtime_put(&pdev->dev);
1730c46ab7e0SJohan Hovold 
1731df828598SMugunthan V N 	return 0;
1732df828598SMugunthan V N 
1733a7fe9d46SJohan Hovold clean_unregister_netdev_ret:
1734a7fe9d46SJohan Hovold 	unregister_netdev(ndev);
173583a8471bSGrygorii Strashko clean_cpts:
173683a8471bSGrygorii Strashko 	cpts_release(cpsw->cpts);
17372c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
1738a4e32b0dSJohan Hovold clean_dt_ret:
1739a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
1740c46ab7e0SJohan Hovold 	pm_runtime_put_sync(&pdev->dev);
1741aa1a15e2SDaniel Mack clean_runtime_disable_ret:
1742f150bd7fSMugunthan V N 	pm_runtime_disable(&pdev->dev);
1743df828598SMugunthan V N 	return ret;
1744df828598SMugunthan V N }
1745df828598SMugunthan V N 
1746663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev)
1747df828598SMugunthan V N {
1748bfe59032SIvan Khoronzhuk 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
1749bfe59032SIvan Khoronzhuk 	int i, ret;
17508a0b6dc9SGrygorii Strashko 
17518a0b6dc9SGrygorii Strashko 	ret = pm_runtime_get_sync(&pdev->dev);
17528a0b6dc9SGrygorii Strashko 	if (ret < 0) {
17538a0b6dc9SGrygorii Strashko 		pm_runtime_put_noidle(&pdev->dev);
17548a0b6dc9SGrygorii Strashko 		return ret;
17558a0b6dc9SGrygorii Strashko 	}
1756df828598SMugunthan V N 
1757bfe59032SIvan Khoronzhuk 	for (i = 0; i < cpsw->data.slaves; i++)
1758bfe59032SIvan Khoronzhuk 		if (cpsw->slaves[i].ndev)
1759bfe59032SIvan Khoronzhuk 			unregister_netdev(cpsw->slaves[i].ndev);
1760df828598SMugunthan V N 
17618a2c9a5aSGrygorii Strashko 	cpts_release(cpsw->cpts);
17622c836bd9SIvan Khoronzhuk 	cpdma_ctlr_destroy(cpsw->dma);
1763a4e32b0dSJohan Hovold 	cpsw_remove_dt(pdev);
17648a0b6dc9SGrygorii Strashko 	pm_runtime_put_sync(&pdev->dev);
17658a0b6dc9SGrygorii Strashko 	pm_runtime_disable(&pdev->dev);
1766df828598SMugunthan V N 	return 0;
1767df828598SMugunthan V N }
1768df828598SMugunthan V N 
17698963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP
1770df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
1771df828598SMugunthan V N {
17722f9b0d93SKeerthy 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
1773618073e3SMugunthan V N 	int i;
1774618073e3SMugunthan V N 
17754c64b83dSGrygorii Strashko 	rtnl_lock();
17764c64b83dSGrygorii Strashko 
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_stop(cpsw->slaves[i].ndev);
17811e7a2e21SDaniel Mack 
17824c64b83dSGrygorii Strashko 	rtnl_unlock();
17834c64b83dSGrygorii Strashko 
1784739683b4SMugunthan V N 	/* Select sleep pin state */
178556e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_sleep_state(dev);
1786739683b4SMugunthan V N 
1787df828598SMugunthan V N 	return 0;
1788df828598SMugunthan V N }
1789df828598SMugunthan V N 
1790df828598SMugunthan V N static int cpsw_resume(struct device *dev)
1791df828598SMugunthan V N {
17922f9b0d93SKeerthy 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
17932f9b0d93SKeerthy 	int i;
1794df828598SMugunthan V N 
1795739683b4SMugunthan V N 	/* Select default pin state */
179656e31bd8SIvan Khoronzhuk 	pinctrl_pm_select_default_state(dev);
1797739683b4SMugunthan V N 
17984ccfd638SGrygorii Strashko 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
17994ccfd638SGrygorii Strashko 	rtnl_lock();
1800618073e3SMugunthan V N 
18012f9b0d93SKeerthy 	for (i = 0; i < cpsw->data.slaves; i++)
18022f9b0d93SKeerthy 		if (cpsw->slaves[i].ndev)
1803606f3993SIvan Khoronzhuk 			if (netif_running(cpsw->slaves[i].ndev))
1804606f3993SIvan Khoronzhuk 				cpsw_ndo_open(cpsw->slaves[i].ndev);
18052f9b0d93SKeerthy 
18064ccfd638SGrygorii Strashko 	rtnl_unlock();
18074ccfd638SGrygorii Strashko 
1808df828598SMugunthan V N 	return 0;
1809df828598SMugunthan V N }
18108963a504SGrygorii Strashko #endif
1811df828598SMugunthan V N 
18128963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
1813df828598SMugunthan V N 
1814df828598SMugunthan V N static struct platform_driver cpsw_driver = {
1815df828598SMugunthan V N 	.driver = {
1816df828598SMugunthan V N 		.name	 = "cpsw",
1817df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
18181e5c76d4SSachin Kamat 		.of_match_table = cpsw_of_mtable,
1819df828598SMugunthan V N 	},
1820df828598SMugunthan V N 	.probe = cpsw_probe,
1821663e12e6SBill Pemberton 	.remove = cpsw_remove,
1822df828598SMugunthan V N };
1823df828598SMugunthan V N 
18246fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver);
1825df828598SMugunthan V N 
1826df828598SMugunthan V N MODULE_LICENSE("GPL");
1827df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1828df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1829df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
1830