xref: /openbmc/linux/drivers/net/ethernet/ni/nixge.c (revision 3d40aed862874db14e1dd41fd6f12636dcfdcc3e)
1492caffaSMoritz Fischer // SPDX-License-Identifier: GPL-2.0
2492caffaSMoritz Fischer /* Copyright (c) 2016-2017, National Instruments Corp.
3492caffaSMoritz Fischer  *
4492caffaSMoritz Fischer  * Author: Moritz Fischer <mdf@kernel.org>
5492caffaSMoritz Fischer  */
6492caffaSMoritz Fischer 
7492caffaSMoritz Fischer #include <linux/etherdevice.h>
8492caffaSMoritz Fischer #include <linux/module.h>
9492caffaSMoritz Fischer #include <linux/netdevice.h>
10*3d40aed8SRob Herring #include <linux/of.h>
11492caffaSMoritz Fischer #include <linux/of_mdio.h>
12492caffaSMoritz Fischer #include <linux/of_net.h>
13*3d40aed8SRob Herring #include <linux/platform_device.h>
14492caffaSMoritz Fischer #include <linux/skbuff.h>
15492caffaSMoritz Fischer #include <linux/phy.h>
16492caffaSMoritz Fischer #include <linux/mii.h>
17492caffaSMoritz Fischer #include <linux/nvmem-consumer.h>
18492caffaSMoritz Fischer #include <linux/ethtool.h>
19492caffaSMoritz Fischer #include <linux/iopoll.h>
20492caffaSMoritz Fischer 
21492caffaSMoritz Fischer #define TX_BD_NUM		64
22492caffaSMoritz Fischer #define RX_BD_NUM		128
23492caffaSMoritz Fischer 
24492caffaSMoritz Fischer /* Axi DMA Register definitions */
25492caffaSMoritz Fischer #define XAXIDMA_TX_CR_OFFSET	0x00 /* Channel control */
26492caffaSMoritz Fischer #define XAXIDMA_TX_SR_OFFSET	0x04 /* Status */
27492caffaSMoritz Fischer #define XAXIDMA_TX_CDESC_OFFSET	0x08 /* Current descriptor pointer */
28492caffaSMoritz Fischer #define XAXIDMA_TX_TDESC_OFFSET	0x10 /* Tail descriptor pointer */
29492caffaSMoritz Fischer 
30492caffaSMoritz Fischer #define XAXIDMA_RX_CR_OFFSET	0x30 /* Channel control */
31492caffaSMoritz Fischer #define XAXIDMA_RX_SR_OFFSET	0x34 /* Status */
32492caffaSMoritz Fischer #define XAXIDMA_RX_CDESC_OFFSET	0x38 /* Current descriptor pointer */
33492caffaSMoritz Fischer #define XAXIDMA_RX_TDESC_OFFSET	0x40 /* Tail descriptor pointer */
34492caffaSMoritz Fischer 
35492caffaSMoritz Fischer #define XAXIDMA_CR_RUNSTOP_MASK	0x1 /* Start/stop DMA channel */
36492caffaSMoritz Fischer #define XAXIDMA_CR_RESET_MASK	0x4 /* Reset DMA engine */
37492caffaSMoritz Fischer 
38492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_LENGTH_MASK	0x007FFFFF /* Requested len */
39492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_TXSOF_MASK	0x08000000 /* First tx packet */
40492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_TXEOF_MASK	0x04000000 /* Last tx packet */
41492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_ALL_MASK	0x0C000000 /* All control bits */
42492caffaSMoritz Fischer 
43492caffaSMoritz Fischer #define XAXIDMA_DELAY_MASK		0xFF000000 /* Delay timeout counter */
44492caffaSMoritz Fischer #define XAXIDMA_COALESCE_MASK		0x00FF0000 /* Coalesce counter */
45492caffaSMoritz Fischer 
46492caffaSMoritz Fischer #define XAXIDMA_DELAY_SHIFT		24
47492caffaSMoritz Fischer #define XAXIDMA_COALESCE_SHIFT		16
48492caffaSMoritz Fischer 
49492caffaSMoritz Fischer #define XAXIDMA_IRQ_IOC_MASK		0x00001000 /* Completion intr */
50492caffaSMoritz Fischer #define XAXIDMA_IRQ_DELAY_MASK		0x00002000 /* Delay interrupt */
51492caffaSMoritz Fischer #define XAXIDMA_IRQ_ERROR_MASK		0x00004000 /* Error interrupt */
52492caffaSMoritz Fischer #define XAXIDMA_IRQ_ALL_MASK		0x00007000 /* All interrupts */
53492caffaSMoritz Fischer 
54492caffaSMoritz Fischer /* Default TX/RX Threshold and waitbound values for SGDMA mode */
55492caffaSMoritz Fischer #define XAXIDMA_DFT_TX_THRESHOLD	24
56492caffaSMoritz Fischer #define XAXIDMA_DFT_TX_WAITBOUND	254
57492caffaSMoritz Fischer #define XAXIDMA_DFT_RX_THRESHOLD	24
58492caffaSMoritz Fischer #define XAXIDMA_DFT_RX_WAITBOUND	254
59492caffaSMoritz Fischer 
60492caffaSMoritz Fischer #define XAXIDMA_BD_STS_ACTUAL_LEN_MASK	0x007FFFFF /* Actual len */
61492caffaSMoritz Fischer #define XAXIDMA_BD_STS_COMPLETE_MASK	0x80000000 /* Completed */
62492caffaSMoritz Fischer #define XAXIDMA_BD_STS_DEC_ERR_MASK	0x40000000 /* Decode error */
63492caffaSMoritz Fischer #define XAXIDMA_BD_STS_SLV_ERR_MASK	0x20000000 /* Slave error */
64492caffaSMoritz Fischer #define XAXIDMA_BD_STS_INT_ERR_MASK	0x10000000 /* Internal err */
65492caffaSMoritz Fischer #define XAXIDMA_BD_STS_ALL_ERR_MASK	0x70000000 /* All errors */
66492caffaSMoritz Fischer #define XAXIDMA_BD_STS_RXSOF_MASK	0x08000000 /* First rx pkt */
67492caffaSMoritz Fischer #define XAXIDMA_BD_STS_RXEOF_MASK	0x04000000 /* Last rx pkt */
68492caffaSMoritz Fischer #define XAXIDMA_BD_STS_ALL_MASK		0xFC000000 /* All status bits */
69492caffaSMoritz Fischer 
70492caffaSMoritz Fischer #define NIXGE_REG_CTRL_OFFSET	0x4000
71492caffaSMoritz Fischer #define NIXGE_REG_INFO		0x00
72492caffaSMoritz Fischer #define NIXGE_REG_MAC_CTL	0x04
73492caffaSMoritz Fischer #define NIXGE_REG_PHY_CTL	0x08
74492caffaSMoritz Fischer #define NIXGE_REG_LED_CTL	0x0c
75492caffaSMoritz Fischer #define NIXGE_REG_MDIO_DATA	0x10
76492caffaSMoritz Fischer #define NIXGE_REG_MDIO_ADDR	0x14
77492caffaSMoritz Fischer #define NIXGE_REG_MDIO_OP	0x18
78492caffaSMoritz Fischer #define NIXGE_REG_MDIO_CTRL	0x1c
79492caffaSMoritz Fischer 
80492caffaSMoritz Fischer #define NIXGE_ID_LED_CTL_EN	BIT(0)
81492caffaSMoritz Fischer #define NIXGE_ID_LED_CTL_VAL	BIT(1)
82492caffaSMoritz Fischer 
83492caffaSMoritz Fischer #define NIXGE_MDIO_CLAUSE45	BIT(12)
84492caffaSMoritz Fischer #define NIXGE_MDIO_CLAUSE22	0
85492caffaSMoritz Fischer #define NIXGE_MDIO_OP(n)     (((n) & 0x3) << 10)
86492caffaSMoritz Fischer #define NIXGE_MDIO_OP_ADDRESS	0
87492caffaSMoritz Fischer #define NIXGE_MDIO_C45_WRITE	BIT(0)
88492caffaSMoritz Fischer #define NIXGE_MDIO_C45_READ	(BIT(1) | BIT(0))
89492caffaSMoritz Fischer #define NIXGE_MDIO_C22_WRITE	BIT(0)
90492caffaSMoritz Fischer #define NIXGE_MDIO_C22_READ	BIT(1)
91492caffaSMoritz Fischer #define NIXGE_MDIO_ADDR(n)   (((n) & 0x1f) << 5)
92492caffaSMoritz Fischer #define NIXGE_MDIO_MMD(n)    (((n) & 0x1f) << 0)
93492caffaSMoritz Fischer 
94492caffaSMoritz Fischer #define NIXGE_REG_MAC_LSB	0x1000
95492caffaSMoritz Fischer #define NIXGE_REG_MAC_MSB	0x1004
96492caffaSMoritz Fischer 
97492caffaSMoritz Fischer /* Packet size info */
98492caffaSMoritz Fischer #define NIXGE_HDR_SIZE		14 /* Size of Ethernet header */
99492caffaSMoritz Fischer #define NIXGE_TRL_SIZE		4 /* Size of Ethernet trailer (FCS) */
100492caffaSMoritz Fischer #define NIXGE_MTU		1500 /* Max MTU of an Ethernet frame */
101492caffaSMoritz Fischer #define NIXGE_JUMBO_MTU		9000 /* Max MTU of a jumbo Eth. frame */
102492caffaSMoritz Fischer 
103492caffaSMoritz Fischer #define NIXGE_MAX_FRAME_SIZE	 (NIXGE_MTU + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE)
104492caffaSMoritz Fischer #define NIXGE_MAX_JUMBO_FRAME_SIZE \
105492caffaSMoritz Fischer 	(NIXGE_JUMBO_MTU + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE)
106492caffaSMoritz Fischer 
10787ab2079SAlex Williams enum nixge_version {
10887ab2079SAlex Williams 	NIXGE_V2,
10987ab2079SAlex Williams 	NIXGE_V3,
11087ab2079SAlex Williams 	NIXGE_VERSION_COUNT
11187ab2079SAlex Williams };
11287ab2079SAlex Williams 
113492caffaSMoritz Fischer struct nixge_hw_dma_bd {
1147e8d5755SMoritz Fischer 	u32 next_lo;
1157e8d5755SMoritz Fischer 	u32 next_hi;
1167e8d5755SMoritz Fischer 	u32 phys_lo;
1177e8d5755SMoritz Fischer 	u32 phys_hi;
118492caffaSMoritz Fischer 	u32 reserved3;
119492caffaSMoritz Fischer 	u32 reserved4;
120492caffaSMoritz Fischer 	u32 cntrl;
121492caffaSMoritz Fischer 	u32 status;
122492caffaSMoritz Fischer 	u32 app0;
123492caffaSMoritz Fischer 	u32 app1;
124492caffaSMoritz Fischer 	u32 app2;
125492caffaSMoritz Fischer 	u32 app3;
126492caffaSMoritz Fischer 	u32 app4;
1277e8d5755SMoritz Fischer 	u32 sw_id_offset_lo;
1287e8d5755SMoritz Fischer 	u32 sw_id_offset_hi;
129492caffaSMoritz Fischer 	u32 reserved6;
130492caffaSMoritz Fischer };
131492caffaSMoritz Fischer 
1327e8d5755SMoritz Fischer #ifdef CONFIG_PHYS_ADDR_T_64BIT
1337e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_addr(bd, field, addr) \
1347e8d5755SMoritz Fischer 	do { \
135ea43a590SMoritz Fischer 		(bd)->field##_lo = lower_32_bits((addr)); \
136ea43a590SMoritz Fischer 		(bd)->field##_hi = upper_32_bits((addr)); \
1377e8d5755SMoritz Fischer 	} while (0)
1387e8d5755SMoritz Fischer #else
1397e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_addr(bd, field, addr) \
1407e8d5755SMoritz Fischer 	((bd)->field##_lo = lower_32_bits((addr)))
1417e8d5755SMoritz Fischer #endif
1427e8d5755SMoritz Fischer 
1437e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_phys(bd, addr) \
1447e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_addr((bd), phys, (addr))
1457e8d5755SMoritz Fischer 
1467e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_next(bd, addr) \
1477e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_addr((bd), next, (addr))
1487e8d5755SMoritz Fischer 
1497e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_offset(bd, addr) \
1507e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_addr((bd), sw_id_offset, (addr))
1517e8d5755SMoritz Fischer 
1527e8d5755SMoritz Fischer #ifdef CONFIG_PHYS_ADDR_T_64BIT
1537e8d5755SMoritz Fischer #define nixge_hw_dma_bd_get_addr(bd, field) \
1547e8d5755SMoritz Fischer 	(dma_addr_t)((((u64)(bd)->field##_hi) << 32) | ((bd)->field##_lo))
1557e8d5755SMoritz Fischer #else
1567e8d5755SMoritz Fischer #define nixge_hw_dma_bd_get_addr(bd, field) \
1577e8d5755SMoritz Fischer 	(dma_addr_t)((bd)->field##_lo)
1587e8d5755SMoritz Fischer #endif
1597e8d5755SMoritz Fischer 
160492caffaSMoritz Fischer struct nixge_tx_skb {
161492caffaSMoritz Fischer 	struct sk_buff *skb;
162492caffaSMoritz Fischer 	dma_addr_t mapping;
163492caffaSMoritz Fischer 	size_t size;
164492caffaSMoritz Fischer 	bool mapped_as_page;
165492caffaSMoritz Fischer };
166492caffaSMoritz Fischer 
167492caffaSMoritz Fischer struct nixge_priv {
168492caffaSMoritz Fischer 	struct net_device *ndev;
169492caffaSMoritz Fischer 	struct napi_struct napi;
170492caffaSMoritz Fischer 	struct device *dev;
171492caffaSMoritz Fischer 
172492caffaSMoritz Fischer 	/* Connection to PHY device */
173492caffaSMoritz Fischer 	struct device_node *phy_node;
174492caffaSMoritz Fischer 	phy_interface_t		phy_mode;
175492caffaSMoritz Fischer 
176492caffaSMoritz Fischer 	int link;
177492caffaSMoritz Fischer 	unsigned int speed;
178492caffaSMoritz Fischer 	unsigned int duplex;
179492caffaSMoritz Fischer 
180492caffaSMoritz Fischer 	/* MDIO bus data */
181492caffaSMoritz Fischer 	struct mii_bus *mii_bus;	/* MII bus reference */
182492caffaSMoritz Fischer 
183492caffaSMoritz Fischer 	/* IO registers, dma functions and IRQs */
184492caffaSMoritz Fischer 	void __iomem *ctrl_regs;
185492caffaSMoritz Fischer 	void __iomem *dma_regs;
186492caffaSMoritz Fischer 
187492caffaSMoritz Fischer 	struct tasklet_struct dma_err_tasklet;
188492caffaSMoritz Fischer 
189492caffaSMoritz Fischer 	int tx_irq;
190492caffaSMoritz Fischer 	int rx_irq;
191492caffaSMoritz Fischer 
192492caffaSMoritz Fischer 	/* Buffer descriptors */
193492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *tx_bd_v;
194492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
195492caffaSMoritz Fischer 	dma_addr_t tx_bd_p;
196492caffaSMoritz Fischer 
197492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *rx_bd_v;
198492caffaSMoritz Fischer 	dma_addr_t rx_bd_p;
199492caffaSMoritz Fischer 	u32 tx_bd_ci;
200492caffaSMoritz Fischer 	u32 tx_bd_tail;
201492caffaSMoritz Fischer 	u32 rx_bd_ci;
202492caffaSMoritz Fischer 
203492caffaSMoritz Fischer 	u32 coalesce_count_rx;
204492caffaSMoritz Fischer 	u32 coalesce_count_tx;
205492caffaSMoritz Fischer };
206492caffaSMoritz Fischer 
207492caffaSMoritz Fischer static void nixge_dma_write_reg(struct nixge_priv *priv, off_t offset, u32 val)
208492caffaSMoritz Fischer {
209492caffaSMoritz Fischer 	writel(val, priv->dma_regs + offset);
210492caffaSMoritz Fischer }
211492caffaSMoritz Fischer 
2127e8d5755SMoritz Fischer static void nixge_dma_write_desc_reg(struct nixge_priv *priv, off_t offset,
2137e8d5755SMoritz Fischer 				     dma_addr_t addr)
2147e8d5755SMoritz Fischer {
2157e8d5755SMoritz Fischer 	writel(lower_32_bits(addr), priv->dma_regs + offset);
2167e8d5755SMoritz Fischer #ifdef CONFIG_PHYS_ADDR_T_64BIT
2177e8d5755SMoritz Fischer 	writel(upper_32_bits(addr), priv->dma_regs + offset + 4);
2187e8d5755SMoritz Fischer #endif
2197e8d5755SMoritz Fischer }
2207e8d5755SMoritz Fischer 
221492caffaSMoritz Fischer static u32 nixge_dma_read_reg(const struct nixge_priv *priv, off_t offset)
222492caffaSMoritz Fischer {
223492caffaSMoritz Fischer 	return readl(priv->dma_regs + offset);
224492caffaSMoritz Fischer }
225492caffaSMoritz Fischer 
226492caffaSMoritz Fischer static void nixge_ctrl_write_reg(struct nixge_priv *priv, off_t offset, u32 val)
227492caffaSMoritz Fischer {
228492caffaSMoritz Fischer 	writel(val, priv->ctrl_regs + offset);
229492caffaSMoritz Fischer }
230492caffaSMoritz Fischer 
231492caffaSMoritz Fischer static u32 nixge_ctrl_read_reg(struct nixge_priv *priv, off_t offset)
232492caffaSMoritz Fischer {
233492caffaSMoritz Fischer 	return readl(priv->ctrl_regs + offset);
234492caffaSMoritz Fischer }
235492caffaSMoritz Fischer 
236492caffaSMoritz Fischer #define nixge_ctrl_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
237492caffaSMoritz Fischer 	readl_poll_timeout((priv)->ctrl_regs + (addr), (val), (cond), \
238492caffaSMoritz Fischer 			   (sleep_us), (timeout_us))
239492caffaSMoritz Fischer 
240492caffaSMoritz Fischer #define nixge_dma_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
241492caffaSMoritz Fischer 	readl_poll_timeout((priv)->dma_regs + (addr), (val), (cond), \
242492caffaSMoritz Fischer 			   (sleep_us), (timeout_us))
243492caffaSMoritz Fischer 
244492caffaSMoritz Fischer static void nixge_hw_dma_bd_release(struct net_device *ndev)
245492caffaSMoritz Fischer {
246492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
2477e8d5755SMoritz Fischer 	dma_addr_t phys_addr;
2487e8d5755SMoritz Fischer 	struct sk_buff *skb;
249492caffaSMoritz Fischer 	int i;
250492caffaSMoritz Fischer 
2519256db4eSYuri Karpov 	if (priv->rx_bd_v) {
252492caffaSMoritz Fischer 		for (i = 0; i < RX_BD_NUM; i++) {
2537e8d5755SMoritz Fischer 			phys_addr = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
2547e8d5755SMoritz Fischer 							     phys);
2557e8d5755SMoritz Fischer 
2567e8d5755SMoritz Fischer 			dma_unmap_single(ndev->dev.parent, phys_addr,
2577e8d5755SMoritz Fischer 					 NIXGE_MAX_JUMBO_FRAME_SIZE,
2587e8d5755SMoritz Fischer 					 DMA_FROM_DEVICE);
2597e8d5755SMoritz Fischer 
260ea43a590SMoritz Fischer 			skb = (struct sk_buff *)(uintptr_t)
2617e8d5755SMoritz Fischer 				nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
2627e8d5755SMoritz Fischer 							 sw_id_offset);
2637e8d5755SMoritz Fischer 			dev_kfree_skb(skb);
264492caffaSMoritz Fischer 		}
265492caffaSMoritz Fischer 
266492caffaSMoritz Fischer 		dma_free_coherent(ndev->dev.parent,
267492caffaSMoritz Fischer 				  sizeof(*priv->rx_bd_v) * RX_BD_NUM,
268492caffaSMoritz Fischer 				  priv->rx_bd_v,
269492caffaSMoritz Fischer 				  priv->rx_bd_p);
2709256db4eSYuri Karpov 	}
271492caffaSMoritz Fischer 
272492caffaSMoritz Fischer 	if (priv->tx_skb)
273492caffaSMoritz Fischer 		devm_kfree(ndev->dev.parent, priv->tx_skb);
274492caffaSMoritz Fischer 
275492caffaSMoritz Fischer 	if (priv->tx_bd_v)
276492caffaSMoritz Fischer 		dma_free_coherent(ndev->dev.parent,
277492caffaSMoritz Fischer 				  sizeof(*priv->tx_bd_v) * TX_BD_NUM,
278492caffaSMoritz Fischer 				  priv->tx_bd_v,
279492caffaSMoritz Fischer 				  priv->tx_bd_p);
280492caffaSMoritz Fischer }
281492caffaSMoritz Fischer 
282492caffaSMoritz Fischer static int nixge_hw_dma_bd_init(struct net_device *ndev)
283492caffaSMoritz Fischer {
284492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
285492caffaSMoritz Fischer 	struct sk_buff *skb;
2867e8d5755SMoritz Fischer 	dma_addr_t phys;
287492caffaSMoritz Fischer 	u32 cr;
288492caffaSMoritz Fischer 	int i;
289492caffaSMoritz Fischer 
290492caffaSMoritz Fischer 	/* Reset the indexes which are used for accessing the BDs */
291492caffaSMoritz Fischer 	priv->tx_bd_ci = 0;
292492caffaSMoritz Fischer 	priv->tx_bd_tail = 0;
293492caffaSMoritz Fischer 	priv->rx_bd_ci = 0;
294492caffaSMoritz Fischer 
295492caffaSMoritz Fischer 	/* Allocate the Tx and Rx buffer descriptors. */
296750afb08SLuis Chamberlain 	priv->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
297492caffaSMoritz Fischer 					   sizeof(*priv->tx_bd_v) * TX_BD_NUM,
298492caffaSMoritz Fischer 					   &priv->tx_bd_p, GFP_KERNEL);
299492caffaSMoritz Fischer 	if (!priv->tx_bd_v)
300492caffaSMoritz Fischer 		goto out;
301492caffaSMoritz Fischer 
302a86854d0SKees Cook 	priv->tx_skb = devm_kcalloc(ndev->dev.parent,
303a86854d0SKees Cook 				    TX_BD_NUM, sizeof(*priv->tx_skb),
304492caffaSMoritz Fischer 				    GFP_KERNEL);
305492caffaSMoritz Fischer 	if (!priv->tx_skb)
306492caffaSMoritz Fischer 		goto out;
307492caffaSMoritz Fischer 
308750afb08SLuis Chamberlain 	priv->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
309492caffaSMoritz Fischer 					   sizeof(*priv->rx_bd_v) * RX_BD_NUM,
310492caffaSMoritz Fischer 					   &priv->rx_bd_p, GFP_KERNEL);
311492caffaSMoritz Fischer 	if (!priv->rx_bd_v)
312492caffaSMoritz Fischer 		goto out;
313492caffaSMoritz Fischer 
314492caffaSMoritz Fischer 	for (i = 0; i < TX_BD_NUM; i++) {
3157e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_next(&priv->tx_bd_v[i],
3167e8d5755SMoritz Fischer 					 priv->tx_bd_p +
317492caffaSMoritz Fischer 					 sizeof(*priv->tx_bd_v) *
3187e8d5755SMoritz Fischer 					 ((i + 1) % TX_BD_NUM));
319492caffaSMoritz Fischer 	}
320492caffaSMoritz Fischer 
321492caffaSMoritz Fischer 	for (i = 0; i < RX_BD_NUM; i++) {
3227e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_next(&priv->rx_bd_v[i],
3237e8d5755SMoritz Fischer 					 priv->rx_bd_p
3247e8d5755SMoritz Fischer 					 + sizeof(*priv->rx_bd_v) *
3257e8d5755SMoritz Fischer 					 ((i + 1) % RX_BD_NUM));
326492caffaSMoritz Fischer 
3276b48beceSChristophe JAILLET 		skb = __netdev_alloc_skb_ip_align(ndev,
3286b48beceSChristophe JAILLET 						  NIXGE_MAX_JUMBO_FRAME_SIZE,
3296b48beceSChristophe JAILLET 						  GFP_KERNEL);
330492caffaSMoritz Fischer 		if (!skb)
331492caffaSMoritz Fischer 			goto out;
332492caffaSMoritz Fischer 
333ea43a590SMoritz Fischer 		nixge_hw_dma_bd_set_offset(&priv->rx_bd_v[i], (uintptr_t)skb);
3347e8d5755SMoritz Fischer 		phys = dma_map_single(ndev->dev.parent, skb->data,
335492caffaSMoritz Fischer 				      NIXGE_MAX_JUMBO_FRAME_SIZE,
336492caffaSMoritz Fischer 				      DMA_FROM_DEVICE);
3377e8d5755SMoritz Fischer 
3387e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(&priv->rx_bd_v[i], phys);
3397e8d5755SMoritz Fischer 
340492caffaSMoritz Fischer 		priv->rx_bd_v[i].cntrl = NIXGE_MAX_JUMBO_FRAME_SIZE;
341492caffaSMoritz Fischer 	}
342492caffaSMoritz Fischer 
343492caffaSMoritz Fischer 	/* Start updating the Rx channel control register */
344492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
345492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
346492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
347492caffaSMoritz Fischer 	      ((priv->coalesce_count_rx) << XAXIDMA_COALESCE_SHIFT));
348492caffaSMoritz Fischer 	/* Update the delay timer count */
349492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_DELAY_MASK) |
350492caffaSMoritz Fischer 	      (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
351492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
352492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
353492caffaSMoritz Fischer 	/* Write to the Rx channel control register */
354492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
355492caffaSMoritz Fischer 
356492caffaSMoritz Fischer 	/* Start updating the Tx channel control register */
357492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
358492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
359492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
360492caffaSMoritz Fischer 	      ((priv->coalesce_count_tx) << XAXIDMA_COALESCE_SHIFT));
361492caffaSMoritz Fischer 	/* Update the delay timer count */
362492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
363492caffaSMoritz Fischer 	      (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
364492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
365492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
366492caffaSMoritz Fischer 	/* Write to the Tx channel control register */
367492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
368492caffaSMoritz Fischer 
369492caffaSMoritz Fischer 	/* Populate the tail pointer and bring the Rx Axi DMA engine out of
370492caffaSMoritz Fischer 	 * halted state. This will make the Rx side ready for reception.
371492caffaSMoritz Fischer 	 */
3727e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_RX_CDESC_OFFSET, priv->rx_bd_p);
373492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
374492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET,
375492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
3767e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_RX_TDESC_OFFSET, priv->rx_bd_p +
377492caffaSMoritz Fischer 			    (sizeof(*priv->rx_bd_v) * (RX_BD_NUM - 1)));
378492caffaSMoritz Fischer 
379492caffaSMoritz Fischer 	/* Write to the RS (Run-stop) bit in the Tx channel control register.
380492caffaSMoritz Fischer 	 * Tx channel is now ready to run. But only after we write to the
381492caffaSMoritz Fischer 	 * tail pointer register that the Tx channel will start transmitting.
382492caffaSMoritz Fischer 	 */
3837e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_TX_CDESC_OFFSET, priv->tx_bd_p);
384492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
385492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET,
386492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
387492caffaSMoritz Fischer 
388492caffaSMoritz Fischer 	return 0;
389492caffaSMoritz Fischer out:
390492caffaSMoritz Fischer 	nixge_hw_dma_bd_release(ndev);
391492caffaSMoritz Fischer 	return -ENOMEM;
392492caffaSMoritz Fischer }
393492caffaSMoritz Fischer 
394492caffaSMoritz Fischer static void __nixge_device_reset(struct nixge_priv *priv, off_t offset)
395492caffaSMoritz Fischer {
396492caffaSMoritz Fischer 	u32 status;
397492caffaSMoritz Fischer 	int err;
398492caffaSMoritz Fischer 
399492caffaSMoritz Fischer 	/* Reset Axi DMA. This would reset NIXGE Ethernet core as well.
400492caffaSMoritz Fischer 	 * The reset process of Axi DMA takes a while to complete as all
401492caffaSMoritz Fischer 	 * pending commands/transfers will be flushed or completed during
402492caffaSMoritz Fischer 	 * this reset process.
403492caffaSMoritz Fischer 	 */
404492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, offset, XAXIDMA_CR_RESET_MASK);
405492caffaSMoritz Fischer 	err = nixge_dma_poll_timeout(priv, offset, status,
406492caffaSMoritz Fischer 				     !(status & XAXIDMA_CR_RESET_MASK), 10,
407492caffaSMoritz Fischer 				     1000);
408492caffaSMoritz Fischer 	if (err)
409492caffaSMoritz Fischer 		netdev_err(priv->ndev, "%s: DMA reset timeout!\n", __func__);
410492caffaSMoritz Fischer }
411492caffaSMoritz Fischer 
412492caffaSMoritz Fischer static void nixge_device_reset(struct net_device *ndev)
413492caffaSMoritz Fischer {
414492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
415492caffaSMoritz Fischer 
416492caffaSMoritz Fischer 	__nixge_device_reset(priv, XAXIDMA_TX_CR_OFFSET);
417492caffaSMoritz Fischer 	__nixge_device_reset(priv, XAXIDMA_RX_CR_OFFSET);
418492caffaSMoritz Fischer 
419492caffaSMoritz Fischer 	if (nixge_hw_dma_bd_init(ndev))
420492caffaSMoritz Fischer 		netdev_err(ndev, "%s: descriptor allocation failed\n",
421492caffaSMoritz Fischer 			   __func__);
422492caffaSMoritz Fischer 
423492caffaSMoritz Fischer 	netif_trans_update(ndev);
424492caffaSMoritz Fischer }
425492caffaSMoritz Fischer 
426492caffaSMoritz Fischer static void nixge_handle_link_change(struct net_device *ndev)
427492caffaSMoritz Fischer {
428492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
429492caffaSMoritz Fischer 	struct phy_device *phydev = ndev->phydev;
430492caffaSMoritz Fischer 
431492caffaSMoritz Fischer 	if (phydev->link != priv->link || phydev->speed != priv->speed ||
432492caffaSMoritz Fischer 	    phydev->duplex != priv->duplex) {
433492caffaSMoritz Fischer 		priv->link = phydev->link;
434492caffaSMoritz Fischer 		priv->speed = phydev->speed;
435492caffaSMoritz Fischer 		priv->duplex = phydev->duplex;
436492caffaSMoritz Fischer 		phy_print_status(phydev);
437492caffaSMoritz Fischer 	}
438492caffaSMoritz Fischer }
439492caffaSMoritz Fischer 
440492caffaSMoritz Fischer static void nixge_tx_skb_unmap(struct nixge_priv *priv,
441492caffaSMoritz Fischer 			       struct nixge_tx_skb *tx_skb)
442492caffaSMoritz Fischer {
443492caffaSMoritz Fischer 	if (tx_skb->mapping) {
444492caffaSMoritz Fischer 		if (tx_skb->mapped_as_page)
445492caffaSMoritz Fischer 			dma_unmap_page(priv->ndev->dev.parent, tx_skb->mapping,
446492caffaSMoritz Fischer 				       tx_skb->size, DMA_TO_DEVICE);
447492caffaSMoritz Fischer 		else
448492caffaSMoritz Fischer 			dma_unmap_single(priv->ndev->dev.parent,
449492caffaSMoritz Fischer 					 tx_skb->mapping,
450492caffaSMoritz Fischer 					 tx_skb->size, DMA_TO_DEVICE);
451492caffaSMoritz Fischer 		tx_skb->mapping = 0;
452492caffaSMoritz Fischer 	}
453492caffaSMoritz Fischer 
454492caffaSMoritz Fischer 	if (tx_skb->skb) {
455492caffaSMoritz Fischer 		dev_kfree_skb_any(tx_skb->skb);
456492caffaSMoritz Fischer 		tx_skb->skb = NULL;
457492caffaSMoritz Fischer 	}
458492caffaSMoritz Fischer }
459492caffaSMoritz Fischer 
460492caffaSMoritz Fischer static void nixge_start_xmit_done(struct net_device *ndev)
461492caffaSMoritz Fischer {
462492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
463492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
464492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
465492caffaSMoritz Fischer 	unsigned int status = 0;
466492caffaSMoritz Fischer 	u32 packets = 0;
467492caffaSMoritz Fischer 	u32 size = 0;
468492caffaSMoritz Fischer 
469492caffaSMoritz Fischer 	cur_p = &priv->tx_bd_v[priv->tx_bd_ci];
470492caffaSMoritz Fischer 	tx_skb = &priv->tx_skb[priv->tx_bd_ci];
471492caffaSMoritz Fischer 
472492caffaSMoritz Fischer 	status = cur_p->status;
473492caffaSMoritz Fischer 
474492caffaSMoritz Fischer 	while (status & XAXIDMA_BD_STS_COMPLETE_MASK) {
475492caffaSMoritz Fischer 		nixge_tx_skb_unmap(priv, tx_skb);
476492caffaSMoritz Fischer 		cur_p->status = 0;
477492caffaSMoritz Fischer 
478492caffaSMoritz Fischer 		size += status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
479492caffaSMoritz Fischer 		packets++;
480492caffaSMoritz Fischer 
481492caffaSMoritz Fischer 		++priv->tx_bd_ci;
482492caffaSMoritz Fischer 		priv->tx_bd_ci %= TX_BD_NUM;
483492caffaSMoritz Fischer 		cur_p = &priv->tx_bd_v[priv->tx_bd_ci];
484492caffaSMoritz Fischer 		tx_skb = &priv->tx_skb[priv->tx_bd_ci];
485492caffaSMoritz Fischer 		status = cur_p->status;
486492caffaSMoritz Fischer 	}
487492caffaSMoritz Fischer 
488492caffaSMoritz Fischer 	ndev->stats.tx_packets += packets;
489492caffaSMoritz Fischer 	ndev->stats.tx_bytes += size;
490492caffaSMoritz Fischer 
491492caffaSMoritz Fischer 	if (packets)
492492caffaSMoritz Fischer 		netif_wake_queue(ndev);
493492caffaSMoritz Fischer }
494492caffaSMoritz Fischer 
495492caffaSMoritz Fischer static int nixge_check_tx_bd_space(struct nixge_priv *priv,
496492caffaSMoritz Fischer 				   int num_frag)
497492caffaSMoritz Fischer {
498492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
499492caffaSMoritz Fischer 
500492caffaSMoritz Fischer 	cur_p = &priv->tx_bd_v[(priv->tx_bd_tail + num_frag) % TX_BD_NUM];
501492caffaSMoritz Fischer 	if (cur_p->status & XAXIDMA_BD_STS_ALL_MASK)
502492caffaSMoritz Fischer 		return NETDEV_TX_BUSY;
503492caffaSMoritz Fischer 	return 0;
504492caffaSMoritz Fischer }
505492caffaSMoritz Fischer 
506015cba7eSYunjian Wang static netdev_tx_t nixge_start_xmit(struct sk_buff *skb,
507015cba7eSYunjian Wang 				    struct net_device *ndev)
508492caffaSMoritz Fischer {
509492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
510492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
511492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
5127e8d5755SMoritz Fischer 	dma_addr_t tail_p, cur_phys;
513492caffaSMoritz Fischer 	skb_frag_t *frag;
514492caffaSMoritz Fischer 	u32 num_frag;
515492caffaSMoritz Fischer 	u32 ii;
516492caffaSMoritz Fischer 
517492caffaSMoritz Fischer 	num_frag = skb_shinfo(skb)->nr_frags;
518492caffaSMoritz Fischer 	cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
519492caffaSMoritz Fischer 	tx_skb = &priv->tx_skb[priv->tx_bd_tail];
520492caffaSMoritz Fischer 
521492caffaSMoritz Fischer 	if (nixge_check_tx_bd_space(priv, num_frag)) {
522492caffaSMoritz Fischer 		if (!netif_queue_stopped(ndev))
523492caffaSMoritz Fischer 			netif_stop_queue(ndev);
524492caffaSMoritz Fischer 		return NETDEV_TX_OK;
525492caffaSMoritz Fischer 	}
526492caffaSMoritz Fischer 
5277e8d5755SMoritz Fischer 	cur_phys = dma_map_single(ndev->dev.parent, skb->data,
528492caffaSMoritz Fischer 				  skb_headlen(skb), DMA_TO_DEVICE);
5297e8d5755SMoritz Fischer 	if (dma_mapping_error(ndev->dev.parent, cur_phys))
530492caffaSMoritz Fischer 		goto drop;
5317e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
532492caffaSMoritz Fischer 
533492caffaSMoritz Fischer 	cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;
534492caffaSMoritz Fischer 
535492caffaSMoritz Fischer 	tx_skb->skb = NULL;
5367e8d5755SMoritz Fischer 	tx_skb->mapping = cur_phys;
537492caffaSMoritz Fischer 	tx_skb->size = skb_headlen(skb);
538492caffaSMoritz Fischer 	tx_skb->mapped_as_page = false;
539492caffaSMoritz Fischer 
540492caffaSMoritz Fischer 	for (ii = 0; ii < num_frag; ii++) {
541492caffaSMoritz Fischer 		++priv->tx_bd_tail;
542492caffaSMoritz Fischer 		priv->tx_bd_tail %= TX_BD_NUM;
543492caffaSMoritz Fischer 		cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
544492caffaSMoritz Fischer 		tx_skb = &priv->tx_skb[priv->tx_bd_tail];
545492caffaSMoritz Fischer 		frag = &skb_shinfo(skb)->frags[ii];
546492caffaSMoritz Fischer 
5477e8d5755SMoritz Fischer 		cur_phys = skb_frag_dma_map(ndev->dev.parent, frag, 0,
548492caffaSMoritz Fischer 					    skb_frag_size(frag),
549492caffaSMoritz Fischer 					    DMA_TO_DEVICE);
5507e8d5755SMoritz Fischer 		if (dma_mapping_error(ndev->dev.parent, cur_phys))
551492caffaSMoritz Fischer 			goto frag_err;
5527e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
553492caffaSMoritz Fischer 
554492caffaSMoritz Fischer 		cur_p->cntrl = skb_frag_size(frag);
555492caffaSMoritz Fischer 
556492caffaSMoritz Fischer 		tx_skb->skb = NULL;
5577e8d5755SMoritz Fischer 		tx_skb->mapping = cur_phys;
558492caffaSMoritz Fischer 		tx_skb->size = skb_frag_size(frag);
559492caffaSMoritz Fischer 		tx_skb->mapped_as_page = true;
560492caffaSMoritz Fischer 	}
561492caffaSMoritz Fischer 
562492caffaSMoritz Fischer 	/* last buffer of the frame */
563492caffaSMoritz Fischer 	tx_skb->skb = skb;
564492caffaSMoritz Fischer 
565492caffaSMoritz Fischer 	cur_p->cntrl |= XAXIDMA_BD_CTRL_TXEOF_MASK;
566492caffaSMoritz Fischer 
567492caffaSMoritz Fischer 	tail_p = priv->tx_bd_p + sizeof(*priv->tx_bd_v) * priv->tx_bd_tail;
568492caffaSMoritz Fischer 	/* Start the transfer */
5697e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_TX_TDESC_OFFSET, tail_p);
570492caffaSMoritz Fischer 	++priv->tx_bd_tail;
571492caffaSMoritz Fischer 	priv->tx_bd_tail %= TX_BD_NUM;
572492caffaSMoritz Fischer 
573492caffaSMoritz Fischer 	return NETDEV_TX_OK;
574492caffaSMoritz Fischer frag_err:
575492caffaSMoritz Fischer 	for (; ii > 0; ii--) {
576492caffaSMoritz Fischer 		if (priv->tx_bd_tail)
577492caffaSMoritz Fischer 			priv->tx_bd_tail--;
578492caffaSMoritz Fischer 		else
579492caffaSMoritz Fischer 			priv->tx_bd_tail = TX_BD_NUM - 1;
580492caffaSMoritz Fischer 
581492caffaSMoritz Fischer 		tx_skb = &priv->tx_skb[priv->tx_bd_tail];
582492caffaSMoritz Fischer 		nixge_tx_skb_unmap(priv, tx_skb);
583492caffaSMoritz Fischer 
584492caffaSMoritz Fischer 		cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
585492caffaSMoritz Fischer 		cur_p->status = 0;
586492caffaSMoritz Fischer 	}
587492caffaSMoritz Fischer 	dma_unmap_single(priv->ndev->dev.parent,
588492caffaSMoritz Fischer 			 tx_skb->mapping,
589492caffaSMoritz Fischer 			 tx_skb->size, DMA_TO_DEVICE);
590492caffaSMoritz Fischer drop:
591492caffaSMoritz Fischer 	ndev->stats.tx_dropped++;
592492caffaSMoritz Fischer 	return NETDEV_TX_OK;
593492caffaSMoritz Fischer }
594492caffaSMoritz Fischer 
595492caffaSMoritz Fischer static int nixge_recv(struct net_device *ndev, int budget)
596492caffaSMoritz Fischer {
597492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
598492caffaSMoritz Fischer 	struct sk_buff *skb, *new_skb;
599492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
6007e8d5755SMoritz Fischer 	dma_addr_t tail_p = 0, cur_phys = 0;
601492caffaSMoritz Fischer 	u32 packets = 0;
602492caffaSMoritz Fischer 	u32 length = 0;
603492caffaSMoritz Fischer 	u32 size = 0;
604492caffaSMoritz Fischer 
605492caffaSMoritz Fischer 	cur_p = &priv->rx_bd_v[priv->rx_bd_ci];
606492caffaSMoritz Fischer 
607492caffaSMoritz Fischer 	while ((cur_p->status & XAXIDMA_BD_STS_COMPLETE_MASK &&
608492caffaSMoritz Fischer 		budget > packets)) {
609492caffaSMoritz Fischer 		tail_p = priv->rx_bd_p + sizeof(*priv->rx_bd_v) *
610492caffaSMoritz Fischer 			 priv->rx_bd_ci;
611492caffaSMoritz Fischer 
612ea43a590SMoritz Fischer 		skb = (struct sk_buff *)(uintptr_t)
613ea43a590SMoritz Fischer 			nixge_hw_dma_bd_get_addr(cur_p, sw_id_offset);
614492caffaSMoritz Fischer 
615492caffaSMoritz Fischer 		length = cur_p->status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
616492caffaSMoritz Fischer 		if (length > NIXGE_MAX_JUMBO_FRAME_SIZE)
617492caffaSMoritz Fischer 			length = NIXGE_MAX_JUMBO_FRAME_SIZE;
618492caffaSMoritz Fischer 
6197e8d5755SMoritz Fischer 		dma_unmap_single(ndev->dev.parent,
6207e8d5755SMoritz Fischer 				 nixge_hw_dma_bd_get_addr(cur_p, phys),
621492caffaSMoritz Fischer 				 NIXGE_MAX_JUMBO_FRAME_SIZE,
622492caffaSMoritz Fischer 				 DMA_FROM_DEVICE);
623492caffaSMoritz Fischer 
624492caffaSMoritz Fischer 		skb_put(skb, length);
625492caffaSMoritz Fischer 
626492caffaSMoritz Fischer 		skb->protocol = eth_type_trans(skb, ndev);
627492caffaSMoritz Fischer 		skb_checksum_none_assert(skb);
628492caffaSMoritz Fischer 
629492caffaSMoritz Fischer 		/* For now mark them as CHECKSUM_NONE since
630492caffaSMoritz Fischer 		 * we don't have offload capabilities
631492caffaSMoritz Fischer 		 */
632492caffaSMoritz Fischer 		skb->ip_summed = CHECKSUM_NONE;
633492caffaSMoritz Fischer 
634492caffaSMoritz Fischer 		napi_gro_receive(&priv->napi, skb);
635492caffaSMoritz Fischer 
636492caffaSMoritz Fischer 		size += length;
637492caffaSMoritz Fischer 		packets++;
638492caffaSMoritz Fischer 
639492caffaSMoritz Fischer 		new_skb = netdev_alloc_skb_ip_align(ndev,
640492caffaSMoritz Fischer 						    NIXGE_MAX_JUMBO_FRAME_SIZE);
641492caffaSMoritz Fischer 		if (!new_skb)
642492caffaSMoritz Fischer 			return packets;
643492caffaSMoritz Fischer 
6447e8d5755SMoritz Fischer 		cur_phys = dma_map_single(ndev->dev.parent, new_skb->data,
645492caffaSMoritz Fischer 					  NIXGE_MAX_JUMBO_FRAME_SIZE,
646492caffaSMoritz Fischer 					  DMA_FROM_DEVICE);
6477e8d5755SMoritz Fischer 		if (dma_mapping_error(ndev->dev.parent, cur_phys)) {
648492caffaSMoritz Fischer 			/* FIXME: bail out and clean up */
649492caffaSMoritz Fischer 			netdev_err(ndev, "Failed to map ...\n");
650492caffaSMoritz Fischer 		}
6517e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
652492caffaSMoritz Fischer 		cur_p->cntrl = NIXGE_MAX_JUMBO_FRAME_SIZE;
653492caffaSMoritz Fischer 		cur_p->status = 0;
654ea43a590SMoritz Fischer 		nixge_hw_dma_bd_set_offset(cur_p, (uintptr_t)new_skb);
655492caffaSMoritz Fischer 
656492caffaSMoritz Fischer 		++priv->rx_bd_ci;
657492caffaSMoritz Fischer 		priv->rx_bd_ci %= RX_BD_NUM;
658492caffaSMoritz Fischer 		cur_p = &priv->rx_bd_v[priv->rx_bd_ci];
659492caffaSMoritz Fischer 	}
660492caffaSMoritz Fischer 
661492caffaSMoritz Fischer 	ndev->stats.rx_packets += packets;
662492caffaSMoritz Fischer 	ndev->stats.rx_bytes += size;
663492caffaSMoritz Fischer 
664492caffaSMoritz Fischer 	if (tail_p)
6657e8d5755SMoritz Fischer 		nixge_dma_write_desc_reg(priv, XAXIDMA_RX_TDESC_OFFSET, tail_p);
666492caffaSMoritz Fischer 
667492caffaSMoritz Fischer 	return packets;
668492caffaSMoritz Fischer }
669492caffaSMoritz Fischer 
670492caffaSMoritz Fischer static int nixge_poll(struct napi_struct *napi, int budget)
671492caffaSMoritz Fischer {
672492caffaSMoritz Fischer 	struct nixge_priv *priv = container_of(napi, struct nixge_priv, napi);
673492caffaSMoritz Fischer 	int work_done;
674492caffaSMoritz Fischer 	u32 status, cr;
675492caffaSMoritz Fischer 
676492caffaSMoritz Fischer 	work_done = 0;
677492caffaSMoritz Fischer 
678492caffaSMoritz Fischer 	work_done = nixge_recv(priv->ndev, budget);
679492caffaSMoritz Fischer 	if (work_done < budget) {
680492caffaSMoritz Fischer 		napi_complete_done(napi, work_done);
681492caffaSMoritz Fischer 		status = nixge_dma_read_reg(priv, XAXIDMA_RX_SR_OFFSET);
682492caffaSMoritz Fischer 
683492caffaSMoritz Fischer 		if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
684492caffaSMoritz Fischer 			/* If there's more, reschedule, but clear */
685492caffaSMoritz Fischer 			nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
686492caffaSMoritz Fischer 			napi_reschedule(napi);
687492caffaSMoritz Fischer 		} else {
688492caffaSMoritz Fischer 			/* if not, turn on RX IRQs again ... */
689492caffaSMoritz Fischer 			cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
690492caffaSMoritz Fischer 			cr |= (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
691492caffaSMoritz Fischer 			nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
692492caffaSMoritz Fischer 		}
693492caffaSMoritz Fischer 	}
694492caffaSMoritz Fischer 
695492caffaSMoritz Fischer 	return work_done;
696492caffaSMoritz Fischer }
697492caffaSMoritz Fischer 
698492caffaSMoritz Fischer static irqreturn_t nixge_tx_irq(int irq, void *_ndev)
699492caffaSMoritz Fischer {
700492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(_ndev);
701492caffaSMoritz Fischer 	struct net_device *ndev = _ndev;
702492caffaSMoritz Fischer 	unsigned int status;
7037e8d5755SMoritz Fischer 	dma_addr_t phys;
704492caffaSMoritz Fischer 	u32 cr;
705492caffaSMoritz Fischer 
706492caffaSMoritz Fischer 	status = nixge_dma_read_reg(priv, XAXIDMA_TX_SR_OFFSET);
707492caffaSMoritz Fischer 	if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
708492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_SR_OFFSET, status);
709492caffaSMoritz Fischer 		nixge_start_xmit_done(priv->ndev);
710492caffaSMoritz Fischer 		goto out;
711492caffaSMoritz Fischer 	}
712492caffaSMoritz Fischer 	if (!(status & XAXIDMA_IRQ_ALL_MASK)) {
713492caffaSMoritz Fischer 		netdev_err(ndev, "No interrupts asserted in Tx path\n");
714492caffaSMoritz Fischer 		return IRQ_NONE;
715492caffaSMoritz Fischer 	}
716492caffaSMoritz Fischer 	if (status & XAXIDMA_IRQ_ERROR_MASK) {
7177e8d5755SMoritz Fischer 		phys = nixge_hw_dma_bd_get_addr(&priv->tx_bd_v[priv->tx_bd_ci],
7187e8d5755SMoritz Fischer 						phys);
7197e8d5755SMoritz Fischer 
720492caffaSMoritz Fischer 		netdev_err(ndev, "DMA Tx error 0x%x\n", status);
7217e8d5755SMoritz Fischer 		netdev_err(ndev, "Current BD is at: 0x%llx\n", (u64)phys);
722492caffaSMoritz Fischer 
723492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
724492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
725492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
726492caffaSMoritz Fischer 		/* Write to the Tx channel control register */
727492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
728492caffaSMoritz Fischer 
729492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
730492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
731492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
732492caffaSMoritz Fischer 		/* Write to the Rx channel control register */
733492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
734492caffaSMoritz Fischer 
735492caffaSMoritz Fischer 		tasklet_schedule(&priv->dma_err_tasklet);
736492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_SR_OFFSET, status);
737492caffaSMoritz Fischer 	}
738492caffaSMoritz Fischer out:
739492caffaSMoritz Fischer 	return IRQ_HANDLED;
740492caffaSMoritz Fischer }
741492caffaSMoritz Fischer 
742492caffaSMoritz Fischer static irqreturn_t nixge_rx_irq(int irq, void *_ndev)
743492caffaSMoritz Fischer {
744492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(_ndev);
745492caffaSMoritz Fischer 	struct net_device *ndev = _ndev;
746492caffaSMoritz Fischer 	unsigned int status;
7477e8d5755SMoritz Fischer 	dma_addr_t phys;
748492caffaSMoritz Fischer 	u32 cr;
749492caffaSMoritz Fischer 
750492caffaSMoritz Fischer 	status = nixge_dma_read_reg(priv, XAXIDMA_RX_SR_OFFSET);
751492caffaSMoritz Fischer 	if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
752492caffaSMoritz Fischer 		/* Turn of IRQs because NAPI */
753492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
754492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
755492caffaSMoritz Fischer 		cr &= ~(XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
756492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
757492caffaSMoritz Fischer 
758492caffaSMoritz Fischer 		if (napi_schedule_prep(&priv->napi))
759492caffaSMoritz Fischer 			__napi_schedule(&priv->napi);
760492caffaSMoritz Fischer 		goto out;
761492caffaSMoritz Fischer 	}
762492caffaSMoritz Fischer 	if (!(status & XAXIDMA_IRQ_ALL_MASK)) {
763492caffaSMoritz Fischer 		netdev_err(ndev, "No interrupts asserted in Rx path\n");
764492caffaSMoritz Fischer 		return IRQ_NONE;
765492caffaSMoritz Fischer 	}
766492caffaSMoritz Fischer 	if (status & XAXIDMA_IRQ_ERROR_MASK) {
7677e8d5755SMoritz Fischer 		phys = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[priv->rx_bd_ci],
7687e8d5755SMoritz Fischer 						phys);
769492caffaSMoritz Fischer 		netdev_err(ndev, "DMA Rx error 0x%x\n", status);
7707e8d5755SMoritz Fischer 		netdev_err(ndev, "Current BD is at: 0x%llx\n", (u64)phys);
771492caffaSMoritz Fischer 
772492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
773492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
774492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
775492caffaSMoritz Fischer 		/* Finally write to the Tx channel control register */
776492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
777492caffaSMoritz Fischer 
778492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
779492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
780492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
781492caffaSMoritz Fischer 		/* write to the Rx channel control register */
782492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
783492caffaSMoritz Fischer 
784492caffaSMoritz Fischer 		tasklet_schedule(&priv->dma_err_tasklet);
785492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
786492caffaSMoritz Fischer 	}
787492caffaSMoritz Fischer out:
788492caffaSMoritz Fischer 	return IRQ_HANDLED;
789492caffaSMoritz Fischer }
790492caffaSMoritz Fischer 
791f246d129SAllen Pais static void nixge_dma_err_handler(struct tasklet_struct *t)
792492caffaSMoritz Fischer {
793f246d129SAllen Pais 	struct nixge_priv *lp = from_tasklet(lp, t, dma_err_tasklet);
794492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
795492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
796492caffaSMoritz Fischer 	u32 cr, i;
797492caffaSMoritz Fischer 
798492caffaSMoritz Fischer 	__nixge_device_reset(lp, XAXIDMA_TX_CR_OFFSET);
799492caffaSMoritz Fischer 	__nixge_device_reset(lp, XAXIDMA_RX_CR_OFFSET);
800492caffaSMoritz Fischer 
801492caffaSMoritz Fischer 	for (i = 0; i < TX_BD_NUM; i++) {
802492caffaSMoritz Fischer 		cur_p = &lp->tx_bd_v[i];
803492caffaSMoritz Fischer 		tx_skb = &lp->tx_skb[i];
804492caffaSMoritz Fischer 		nixge_tx_skb_unmap(lp, tx_skb);
805492caffaSMoritz Fischer 
8067e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(cur_p, 0);
807492caffaSMoritz Fischer 		cur_p->cntrl = 0;
808492caffaSMoritz Fischer 		cur_p->status = 0;
8097e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_offset(cur_p, 0);
810492caffaSMoritz Fischer 	}
811492caffaSMoritz Fischer 
812492caffaSMoritz Fischer 	for (i = 0; i < RX_BD_NUM; i++) {
813492caffaSMoritz Fischer 		cur_p = &lp->rx_bd_v[i];
814492caffaSMoritz Fischer 		cur_p->status = 0;
815492caffaSMoritz Fischer 	}
816492caffaSMoritz Fischer 
817492caffaSMoritz Fischer 	lp->tx_bd_ci = 0;
818492caffaSMoritz Fischer 	lp->tx_bd_tail = 0;
819492caffaSMoritz Fischer 	lp->rx_bd_ci = 0;
820492caffaSMoritz Fischer 
821492caffaSMoritz Fischer 	/* Start updating the Rx channel control register */
822492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_RX_CR_OFFSET);
823492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
824492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
825492caffaSMoritz Fischer 	      (XAXIDMA_DFT_RX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
826492caffaSMoritz Fischer 	/* Update the delay timer count */
827492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_DELAY_MASK) |
828492caffaSMoritz Fischer 	      (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
829492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
830492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
831492caffaSMoritz Fischer 	/* Finally write to the Rx channel control register */
832492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_RX_CR_OFFSET, cr);
833492caffaSMoritz Fischer 
834492caffaSMoritz Fischer 	/* Start updating the Tx channel control register */
835492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_TX_CR_OFFSET);
836492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
837492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
838492caffaSMoritz Fischer 	      (XAXIDMA_DFT_TX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
839492caffaSMoritz Fischer 	/* Update the delay timer count */
840492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
841492caffaSMoritz Fischer 	      (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
842492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
843492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
844492caffaSMoritz Fischer 	/* Finally write to the Tx channel control register */
845492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_TX_CR_OFFSET, cr);
846492caffaSMoritz Fischer 
847492caffaSMoritz Fischer 	/* Populate the tail pointer and bring the Rx Axi DMA engine out of
848492caffaSMoritz Fischer 	 * halted state. This will make the Rx side ready for reception.
849492caffaSMoritz Fischer 	 */
8507e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
851492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_RX_CR_OFFSET);
852492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_RX_CR_OFFSET,
853492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
8547e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
855492caffaSMoritz Fischer 			    (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
856492caffaSMoritz Fischer 
857492caffaSMoritz Fischer 	/* Write to the RS (Run-stop) bit in the Tx channel control register.
858492caffaSMoritz Fischer 	 * Tx channel is now ready to run. But only after we write to the
859492caffaSMoritz Fischer 	 * tail pointer register that the Tx channel will start transmitting
860492caffaSMoritz Fischer 	 */
8617e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
862492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_TX_CR_OFFSET);
863492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_TX_CR_OFFSET,
864492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
865492caffaSMoritz Fischer }
866492caffaSMoritz Fischer 
867492caffaSMoritz Fischer static int nixge_open(struct net_device *ndev)
868492caffaSMoritz Fischer {
869492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
870492caffaSMoritz Fischer 	struct phy_device *phy;
871492caffaSMoritz Fischer 	int ret;
872492caffaSMoritz Fischer 
873492caffaSMoritz Fischer 	nixge_device_reset(ndev);
874492caffaSMoritz Fischer 
875492caffaSMoritz Fischer 	phy = of_phy_connect(ndev, priv->phy_node,
876492caffaSMoritz Fischer 			     &nixge_handle_link_change, 0, priv->phy_mode);
877492caffaSMoritz Fischer 	if (!phy)
878492caffaSMoritz Fischer 		return -ENODEV;
879492caffaSMoritz Fischer 
880492caffaSMoritz Fischer 	phy_start(phy);
881492caffaSMoritz Fischer 
882492caffaSMoritz Fischer 	/* Enable tasklets for Axi DMA error handling */
883f246d129SAllen Pais 	tasklet_setup(&priv->dma_err_tasklet, nixge_dma_err_handler);
884492caffaSMoritz Fischer 
885492caffaSMoritz Fischer 	napi_enable(&priv->napi);
886492caffaSMoritz Fischer 
887492caffaSMoritz Fischer 	/* Enable interrupts for Axi DMA Tx */
888492caffaSMoritz Fischer 	ret = request_irq(priv->tx_irq, nixge_tx_irq, 0, ndev->name, ndev);
889492caffaSMoritz Fischer 	if (ret)
890492caffaSMoritz Fischer 		goto err_tx_irq;
891492caffaSMoritz Fischer 	/* Enable interrupts for Axi DMA Rx */
892492caffaSMoritz Fischer 	ret = request_irq(priv->rx_irq, nixge_rx_irq, 0, ndev->name, ndev);
893492caffaSMoritz Fischer 	if (ret)
894492caffaSMoritz Fischer 		goto err_rx_irq;
895492caffaSMoritz Fischer 
896492caffaSMoritz Fischer 	netif_start_queue(ndev);
897492caffaSMoritz Fischer 
898492caffaSMoritz Fischer 	return 0;
899492caffaSMoritz Fischer 
900492caffaSMoritz Fischer err_rx_irq:
901492caffaSMoritz Fischer 	free_irq(priv->tx_irq, ndev);
902492caffaSMoritz Fischer err_tx_irq:
903b0633491SZhengchao Shao 	napi_disable(&priv->napi);
904492caffaSMoritz Fischer 	phy_stop(phy);
905492caffaSMoritz Fischer 	phy_disconnect(phy);
906492caffaSMoritz Fischer 	tasklet_kill(&priv->dma_err_tasklet);
907492caffaSMoritz Fischer 	netdev_err(ndev, "request_irq() failed\n");
908492caffaSMoritz Fischer 	return ret;
909492caffaSMoritz Fischer }
910492caffaSMoritz Fischer 
911492caffaSMoritz Fischer static int nixge_stop(struct net_device *ndev)
912492caffaSMoritz Fischer {
913492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
914492caffaSMoritz Fischer 	u32 cr;
915492caffaSMoritz Fischer 
916492caffaSMoritz Fischer 	netif_stop_queue(ndev);
917492caffaSMoritz Fischer 	napi_disable(&priv->napi);
918492caffaSMoritz Fischer 
919492caffaSMoritz Fischer 	if (ndev->phydev) {
920492caffaSMoritz Fischer 		phy_stop(ndev->phydev);
921492caffaSMoritz Fischer 		phy_disconnect(ndev->phydev);
922492caffaSMoritz Fischer 	}
923492caffaSMoritz Fischer 
924492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
925492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET,
926492caffaSMoritz Fischer 			    cr & (~XAXIDMA_CR_RUNSTOP_MASK));
927492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
928492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET,
929492caffaSMoritz Fischer 			    cr & (~XAXIDMA_CR_RUNSTOP_MASK));
930492caffaSMoritz Fischer 
931492caffaSMoritz Fischer 	tasklet_kill(&priv->dma_err_tasklet);
932492caffaSMoritz Fischer 
933492caffaSMoritz Fischer 	free_irq(priv->tx_irq, ndev);
934492caffaSMoritz Fischer 	free_irq(priv->rx_irq, ndev);
935492caffaSMoritz Fischer 
936492caffaSMoritz Fischer 	nixge_hw_dma_bd_release(ndev);
937492caffaSMoritz Fischer 
938492caffaSMoritz Fischer 	return 0;
939492caffaSMoritz Fischer }
940492caffaSMoritz Fischer 
941492caffaSMoritz Fischer static int nixge_change_mtu(struct net_device *ndev, int new_mtu)
942492caffaSMoritz Fischer {
943492caffaSMoritz Fischer 	if (netif_running(ndev))
944492caffaSMoritz Fischer 		return -EBUSY;
945492caffaSMoritz Fischer 
946492caffaSMoritz Fischer 	if ((new_mtu + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE) >
947492caffaSMoritz Fischer 	     NIXGE_MAX_JUMBO_FRAME_SIZE)
948492caffaSMoritz Fischer 		return -EINVAL;
949492caffaSMoritz Fischer 
950492caffaSMoritz Fischer 	ndev->mtu = new_mtu;
951492caffaSMoritz Fischer 
952492caffaSMoritz Fischer 	return 0;
953492caffaSMoritz Fischer }
954492caffaSMoritz Fischer 
955492caffaSMoritz Fischer static s32 __nixge_hw_set_mac_address(struct net_device *ndev)
956492caffaSMoritz Fischer {
957492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
958492caffaSMoritz Fischer 
959492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MAC_LSB,
960492caffaSMoritz Fischer 			     (ndev->dev_addr[2]) << 24 |
961492caffaSMoritz Fischer 			     (ndev->dev_addr[3] << 16) |
962492caffaSMoritz Fischer 			     (ndev->dev_addr[4] << 8) |
963492caffaSMoritz Fischer 			     (ndev->dev_addr[5] << 0));
964492caffaSMoritz Fischer 
965492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MAC_MSB,
966492caffaSMoritz Fischer 			     (ndev->dev_addr[1] | (ndev->dev_addr[0] << 8)));
967492caffaSMoritz Fischer 
968492caffaSMoritz Fischer 	return 0;
969492caffaSMoritz Fischer }
970492caffaSMoritz Fischer 
971492caffaSMoritz Fischer static int nixge_net_set_mac_address(struct net_device *ndev, void *p)
972492caffaSMoritz Fischer {
973492caffaSMoritz Fischer 	int err;
974492caffaSMoritz Fischer 
975492caffaSMoritz Fischer 	err = eth_mac_addr(ndev, p);
976492caffaSMoritz Fischer 	if (!err)
977492caffaSMoritz Fischer 		__nixge_hw_set_mac_address(ndev);
978492caffaSMoritz Fischer 
979492caffaSMoritz Fischer 	return err;
980492caffaSMoritz Fischer }
981492caffaSMoritz Fischer 
982492caffaSMoritz Fischer static const struct net_device_ops nixge_netdev_ops = {
983492caffaSMoritz Fischer 	.ndo_open = nixge_open,
984492caffaSMoritz Fischer 	.ndo_stop = nixge_stop,
985492caffaSMoritz Fischer 	.ndo_start_xmit = nixge_start_xmit,
986492caffaSMoritz Fischer 	.ndo_change_mtu	= nixge_change_mtu,
987492caffaSMoritz Fischer 	.ndo_set_mac_address = nixge_net_set_mac_address,
988492caffaSMoritz Fischer 	.ndo_validate_addr = eth_validate_addr,
989492caffaSMoritz Fischer };
990492caffaSMoritz Fischer 
991492caffaSMoritz Fischer static void nixge_ethtools_get_drvinfo(struct net_device *ndev,
992492caffaSMoritz Fischer 				       struct ethtool_drvinfo *ed)
993492caffaSMoritz Fischer {
994f029c781SWolfram Sang 	strscpy(ed->driver, "nixge", sizeof(ed->driver));
995f029c781SWolfram Sang 	strscpy(ed->bus_info, "platform", sizeof(ed->bus_info));
996492caffaSMoritz Fischer }
997492caffaSMoritz Fischer 
998f3ccfda1SYufeng Mo static int
999f3ccfda1SYufeng Mo nixge_ethtools_get_coalesce(struct net_device *ndev,
1000f3ccfda1SYufeng Mo 			    struct ethtool_coalesce *ecoalesce,
1001f3ccfda1SYufeng Mo 			    struct kernel_ethtool_coalesce *kernel_coal,
1002f3ccfda1SYufeng Mo 			    struct netlink_ext_ack *extack)
1003492caffaSMoritz Fischer {
1004492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1005492caffaSMoritz Fischer 	u32 regval = 0;
1006492caffaSMoritz Fischer 
1007492caffaSMoritz Fischer 	regval = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
1008492caffaSMoritz Fischer 	ecoalesce->rx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
1009492caffaSMoritz Fischer 					     >> XAXIDMA_COALESCE_SHIFT;
1010492caffaSMoritz Fischer 	regval = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
1011492caffaSMoritz Fischer 	ecoalesce->tx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
1012492caffaSMoritz Fischer 					     >> XAXIDMA_COALESCE_SHIFT;
1013492caffaSMoritz Fischer 	return 0;
1014492caffaSMoritz Fischer }
1015492caffaSMoritz Fischer 
1016f3ccfda1SYufeng Mo static int
1017f3ccfda1SYufeng Mo nixge_ethtools_set_coalesce(struct net_device *ndev,
1018f3ccfda1SYufeng Mo 			    struct ethtool_coalesce *ecoalesce,
1019f3ccfda1SYufeng Mo 			    struct kernel_ethtool_coalesce *kernel_coal,
1020f3ccfda1SYufeng Mo 			    struct netlink_ext_ack *extack)
1021492caffaSMoritz Fischer {
1022492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1023492caffaSMoritz Fischer 
1024492caffaSMoritz Fischer 	if (netif_running(ndev)) {
1025492caffaSMoritz Fischer 		netdev_err(ndev,
1026492caffaSMoritz Fischer 			   "Please stop netif before applying configuration\n");
1027492caffaSMoritz Fischer 		return -EBUSY;
1028492caffaSMoritz Fischer 	}
1029492caffaSMoritz Fischer 
1030492caffaSMoritz Fischer 	if (ecoalesce->rx_max_coalesced_frames)
1031492caffaSMoritz Fischer 		priv->coalesce_count_rx = ecoalesce->rx_max_coalesced_frames;
1032492caffaSMoritz Fischer 	if (ecoalesce->tx_max_coalesced_frames)
1033492caffaSMoritz Fischer 		priv->coalesce_count_tx = ecoalesce->tx_max_coalesced_frames;
1034492caffaSMoritz Fischer 
1035492caffaSMoritz Fischer 	return 0;
1036492caffaSMoritz Fischer }
1037492caffaSMoritz Fischer 
1038492caffaSMoritz Fischer static int nixge_ethtools_set_phys_id(struct net_device *ndev,
1039492caffaSMoritz Fischer 				      enum ethtool_phys_id_state state)
1040492caffaSMoritz Fischer {
1041492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1042492caffaSMoritz Fischer 	u32 ctrl;
1043492caffaSMoritz Fischer 
1044492caffaSMoritz Fischer 	ctrl = nixge_ctrl_read_reg(priv, NIXGE_REG_LED_CTL);
1045492caffaSMoritz Fischer 	switch (state) {
1046492caffaSMoritz Fischer 	case ETHTOOL_ID_ACTIVE:
1047492caffaSMoritz Fischer 		ctrl |= NIXGE_ID_LED_CTL_EN;
1048492caffaSMoritz Fischer 		/* Enable identification LED override*/
1049492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1050492caffaSMoritz Fischer 		return 2;
1051492caffaSMoritz Fischer 
1052492caffaSMoritz Fischer 	case ETHTOOL_ID_ON:
1053492caffaSMoritz Fischer 		ctrl |= NIXGE_ID_LED_CTL_VAL;
1054492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1055492caffaSMoritz Fischer 		break;
1056492caffaSMoritz Fischer 
1057492caffaSMoritz Fischer 	case ETHTOOL_ID_OFF:
1058492caffaSMoritz Fischer 		ctrl &= ~NIXGE_ID_LED_CTL_VAL;
1059492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1060492caffaSMoritz Fischer 		break;
1061492caffaSMoritz Fischer 
1062492caffaSMoritz Fischer 	case ETHTOOL_ID_INACTIVE:
1063492caffaSMoritz Fischer 		/* Restore LED settings */
1064492caffaSMoritz Fischer 		ctrl &= ~NIXGE_ID_LED_CTL_EN;
1065492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1066492caffaSMoritz Fischer 		break;
1067492caffaSMoritz Fischer 	}
1068492caffaSMoritz Fischer 
1069492caffaSMoritz Fischer 	return 0;
1070492caffaSMoritz Fischer }
1071492caffaSMoritz Fischer 
1072492caffaSMoritz Fischer static const struct ethtool_ops nixge_ethtool_ops = {
10738078f028SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
1074492caffaSMoritz Fischer 	.get_drvinfo    = nixge_ethtools_get_drvinfo,
1075492caffaSMoritz Fischer 	.get_coalesce   = nixge_ethtools_get_coalesce,
1076492caffaSMoritz Fischer 	.set_coalesce   = nixge_ethtools_set_coalesce,
1077492caffaSMoritz Fischer 	.set_phys_id    = nixge_ethtools_set_phys_id,
1078492caffaSMoritz Fischer 	.get_link_ksettings     = phy_ethtool_get_link_ksettings,
1079492caffaSMoritz Fischer 	.set_link_ksettings     = phy_ethtool_set_link_ksettings,
1080492caffaSMoritz Fischer 	.get_link		= ethtool_op_get_link,
1081492caffaSMoritz Fischer };
1082492caffaSMoritz Fischer 
1083064a6a88SAndrew Lunn static int nixge_mdio_read_c22(struct mii_bus *bus, int phy_id, int reg)
1084492caffaSMoritz Fischer {
1085492caffaSMoritz Fischer 	struct nixge_priv *priv = bus->priv;
1086492caffaSMoritz Fischer 	u32 status, tmp;
1087492caffaSMoritz Fischer 	int err;
1088492caffaSMoritz Fischer 	u16 device;
1089492caffaSMoritz Fischer 
1090492caffaSMoritz Fischer 	device = reg & 0x1f;
1091492caffaSMoritz Fischer 
1092492caffaSMoritz Fischer 	tmp = NIXGE_MDIO_CLAUSE22 | NIXGE_MDIO_OP(NIXGE_MDIO_C22_READ) |
1093492caffaSMoritz Fischer 	      NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1094492caffaSMoritz Fischer 
1095492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1096492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1097492caffaSMoritz Fischer 
1098492caffaSMoritz Fischer 	err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1099492caffaSMoritz Fischer 				      !status, 10, 1000);
1100492caffaSMoritz Fischer 	if (err) {
1101492caffaSMoritz Fischer 		dev_err(priv->dev, "timeout setting read command");
1102492caffaSMoritz Fischer 		return err;
1103492caffaSMoritz Fischer 	}
1104492caffaSMoritz Fischer 
1105492caffaSMoritz Fischer 	status = nixge_ctrl_read_reg(priv, NIXGE_REG_MDIO_DATA);
1106492caffaSMoritz Fischer 
1107492caffaSMoritz Fischer 	return status;
1108492caffaSMoritz Fischer }
1109492caffaSMoritz Fischer 
1110064a6a88SAndrew Lunn static int nixge_mdio_read_c45(struct mii_bus *bus, int phy_id, int device,
1111064a6a88SAndrew Lunn 			       int reg)
1112492caffaSMoritz Fischer {
1113492caffaSMoritz Fischer 	struct nixge_priv *priv = bus->priv;
1114492caffaSMoritz Fischer 	u32 status, tmp;
1115492caffaSMoritz Fischer 	int err;
1116492caffaSMoritz Fischer 
1117492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_ADDR, reg & 0xffff);
1118492caffaSMoritz Fischer 
1119064a6a88SAndrew Lunn 	tmp = NIXGE_MDIO_CLAUSE45 |
1120064a6a88SAndrew Lunn 	      NIXGE_MDIO_OP(NIXGE_MDIO_OP_ADDRESS) |
1121064a6a88SAndrew Lunn 	      NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1122492caffaSMoritz Fischer 
1123492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1124492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1125492caffaSMoritz Fischer 
1126492caffaSMoritz Fischer 	err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1127492caffaSMoritz Fischer 				      !status, 10, 1000);
1128492caffaSMoritz Fischer 	if (err) {
1129492caffaSMoritz Fischer 		dev_err(priv->dev, "timeout setting address");
1130492caffaSMoritz Fischer 		return err;
1131492caffaSMoritz Fischer 	}
1132492caffaSMoritz Fischer 
1133064a6a88SAndrew Lunn 	tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_C45_READ) |
1134064a6a88SAndrew Lunn 	      NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1135492caffaSMoritz Fischer 
1136492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1137064a6a88SAndrew Lunn 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1138064a6a88SAndrew Lunn 
1139492caffaSMoritz Fischer 	err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1140492caffaSMoritz Fischer 				      !status, 10, 1000);
1141064a6a88SAndrew Lunn 	if (err) {
1142064a6a88SAndrew Lunn 		dev_err(priv->dev, "timeout setting read command");
1143064a6a88SAndrew Lunn 		return err;
1144064a6a88SAndrew Lunn 	}
1145064a6a88SAndrew Lunn 
1146064a6a88SAndrew Lunn 	status = nixge_ctrl_read_reg(priv, NIXGE_REG_MDIO_DATA);
1147064a6a88SAndrew Lunn 
1148064a6a88SAndrew Lunn 	return status;
1149064a6a88SAndrew Lunn }
1150064a6a88SAndrew Lunn 
1151064a6a88SAndrew Lunn static int nixge_mdio_write_c22(struct mii_bus *bus, int phy_id, int reg,
1152064a6a88SAndrew Lunn 				u16 val)
1153064a6a88SAndrew Lunn {
1154064a6a88SAndrew Lunn 	struct nixge_priv *priv = bus->priv;
1155064a6a88SAndrew Lunn 	u32 status, tmp;
1156064a6a88SAndrew Lunn 	u16 device;
1157064a6a88SAndrew Lunn 	int err;
1158064a6a88SAndrew Lunn 
1159492caffaSMoritz Fischer 	device = reg & 0x1f;
1160492caffaSMoritz Fischer 
1161064a6a88SAndrew Lunn 	tmp = NIXGE_MDIO_CLAUSE22 | NIXGE_MDIO_OP(NIXGE_MDIO_C22_WRITE) |
1162492caffaSMoritz Fischer 	      NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1163492caffaSMoritz Fischer 
1164492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_DATA, val);
1165492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1166492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1167492caffaSMoritz Fischer 
1168492caffaSMoritz Fischer 	err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1169492caffaSMoritz Fischer 				      !status, 10, 1000);
1170492caffaSMoritz Fischer 	if (err)
1171492caffaSMoritz Fischer 		dev_err(priv->dev, "timeout setting write command");
1172064a6a88SAndrew Lunn 
1173064a6a88SAndrew Lunn 	return err;
1174492caffaSMoritz Fischer }
1175492caffaSMoritz Fischer 
1176064a6a88SAndrew Lunn static int nixge_mdio_write_c45(struct mii_bus *bus, int phy_id,
1177064a6a88SAndrew Lunn 				int device, int reg, u16 val)
1178064a6a88SAndrew Lunn {
1179064a6a88SAndrew Lunn 	struct nixge_priv *priv = bus->priv;
1180064a6a88SAndrew Lunn 	u32 status, tmp;
1181064a6a88SAndrew Lunn 	int err;
1182064a6a88SAndrew Lunn 
1183064a6a88SAndrew Lunn 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_ADDR, reg & 0xffff);
1184064a6a88SAndrew Lunn 
1185064a6a88SAndrew Lunn 	tmp = NIXGE_MDIO_CLAUSE45 |
1186064a6a88SAndrew Lunn 	      NIXGE_MDIO_OP(NIXGE_MDIO_OP_ADDRESS) |
1187064a6a88SAndrew Lunn 	      NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1188064a6a88SAndrew Lunn 
1189064a6a88SAndrew Lunn 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1190064a6a88SAndrew Lunn 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1191064a6a88SAndrew Lunn 
1192064a6a88SAndrew Lunn 	err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1193064a6a88SAndrew Lunn 				      !status, 10, 1000);
1194064a6a88SAndrew Lunn 	if (err) {
1195064a6a88SAndrew Lunn 		dev_err(priv->dev, "timeout setting address");
1196064a6a88SAndrew Lunn 		return err;
1197064a6a88SAndrew Lunn 	}
1198064a6a88SAndrew Lunn 
1199064a6a88SAndrew Lunn 	tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_C45_WRITE) |
1200064a6a88SAndrew Lunn 	      NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1201064a6a88SAndrew Lunn 
1202064a6a88SAndrew Lunn 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_DATA, val);
1203064a6a88SAndrew Lunn 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1204064a6a88SAndrew Lunn 
1205064a6a88SAndrew Lunn 	err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1206064a6a88SAndrew Lunn 				      !status, 10, 1000);
1207064a6a88SAndrew Lunn 	if (err)
1208064a6a88SAndrew Lunn 		dev_err(priv->dev, "timeout setting write command");
1209064a6a88SAndrew Lunn 
1210492caffaSMoritz Fischer 	return err;
1211492caffaSMoritz Fischer }
1212492caffaSMoritz Fischer 
1213492caffaSMoritz Fischer static int nixge_mdio_setup(struct nixge_priv *priv, struct device_node *np)
1214492caffaSMoritz Fischer {
1215492caffaSMoritz Fischer 	struct mii_bus *bus;
1216492caffaSMoritz Fischer 
1217492caffaSMoritz Fischer 	bus = devm_mdiobus_alloc(priv->dev);
1218492caffaSMoritz Fischer 	if (!bus)
1219492caffaSMoritz Fischer 		return -ENOMEM;
1220492caffaSMoritz Fischer 
1221492caffaSMoritz Fischer 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(priv->dev));
1222492caffaSMoritz Fischer 	bus->priv = priv;
1223492caffaSMoritz Fischer 	bus->name = "nixge_mii_bus";
1224064a6a88SAndrew Lunn 	bus->read = nixge_mdio_read_c22;
1225064a6a88SAndrew Lunn 	bus->write = nixge_mdio_write_c22;
1226064a6a88SAndrew Lunn 	bus->read_c45 = nixge_mdio_read_c45;
1227064a6a88SAndrew Lunn 	bus->write_c45 = nixge_mdio_write_c45;
1228492caffaSMoritz Fischer 	bus->parent = priv->dev;
1229492caffaSMoritz Fischer 
1230492caffaSMoritz Fischer 	priv->mii_bus = bus;
1231492caffaSMoritz Fischer 
1232492caffaSMoritz Fischer 	return of_mdiobus_register(bus, np);
1233492caffaSMoritz Fischer }
1234492caffaSMoritz Fischer 
1235492caffaSMoritz Fischer static void *nixge_get_nvmem_address(struct device *dev)
1236492caffaSMoritz Fischer {
1237492caffaSMoritz Fischer 	struct nvmem_cell *cell;
1238492caffaSMoritz Fischer 	size_t cell_size;
1239492caffaSMoritz Fischer 	char *mac;
1240492caffaSMoritz Fischer 
1241492caffaSMoritz Fischer 	cell = nvmem_cell_get(dev, "address");
1242492caffaSMoritz Fischer 	if (IS_ERR(cell))
1243a68229caSArnd Bergmann 		return cell;
1244492caffaSMoritz Fischer 
1245492caffaSMoritz Fischer 	mac = nvmem_cell_read(cell, &cell_size);
1246492caffaSMoritz Fischer 	nvmem_cell_put(cell);
1247492caffaSMoritz Fischer 
1248492caffaSMoritz Fischer 	return mac;
1249492caffaSMoritz Fischer }
1250492caffaSMoritz Fischer 
125187ab2079SAlex Williams /* Match table for of_platform binding */
125287ab2079SAlex Williams static const struct of_device_id nixge_dt_ids[] = {
125387ab2079SAlex Williams 	{ .compatible = "ni,xge-enet-2.00", .data = (void *)NIXGE_V2 },
125487ab2079SAlex Williams 	{ .compatible = "ni,xge-enet-3.00", .data = (void *)NIXGE_V3 },
125587ab2079SAlex Williams 	{},
125687ab2079SAlex Williams };
125787ab2079SAlex Williams MODULE_DEVICE_TABLE(of, nixge_dt_ids);
125887ab2079SAlex Williams 
125987ab2079SAlex Williams static int nixge_of_get_resources(struct platform_device *pdev)
126087ab2079SAlex Williams {
126187ab2079SAlex Williams 	const struct of_device_id *of_id;
126287ab2079SAlex Williams 	enum nixge_version version;
126387ab2079SAlex Williams 	struct net_device *ndev;
126487ab2079SAlex Williams 	struct nixge_priv *priv;
126587ab2079SAlex Williams 
126687ab2079SAlex Williams 	ndev = platform_get_drvdata(pdev);
126787ab2079SAlex Williams 	priv = netdev_priv(ndev);
126887ab2079SAlex Williams 	of_id = of_match_node(nixge_dt_ids, pdev->dev.of_node);
126987ab2079SAlex Williams 	if (!of_id)
127087ab2079SAlex Williams 		return -ENODEV;
127187ab2079SAlex Williams 
127287ab2079SAlex Williams 	version = (enum nixge_version)of_id->data;
127387ab2079SAlex Williams 	if (version <= NIXGE_V2)
12745b38b97fSYang Yingliang 		priv->dma_regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
127587ab2079SAlex Williams 	else
12765b38b97fSYang Yingliang 		priv->dma_regs = devm_platform_ioremap_resource_byname(pdev, "dma");
127787ab2079SAlex Williams 	if (IS_ERR(priv->dma_regs)) {
127887ab2079SAlex Williams 		netdev_err(ndev, "failed to map dma regs\n");
127987ab2079SAlex Williams 		return PTR_ERR(priv->dma_regs);
128087ab2079SAlex Williams 	}
1281464a5728SCai Huoqing 	if (version <= NIXGE_V2)
128287ab2079SAlex Williams 		priv->ctrl_regs = priv->dma_regs + NIXGE_REG_CTRL_OFFSET;
1283464a5728SCai Huoqing 	else
1284464a5728SCai Huoqing 		priv->ctrl_regs = devm_platform_ioremap_resource_byname(pdev, "ctrl");
128587ab2079SAlex Williams 	if (IS_ERR(priv->ctrl_regs)) {
128687ab2079SAlex Williams 		netdev_err(ndev, "failed to map ctrl regs\n");
128787ab2079SAlex Williams 		return PTR_ERR(priv->ctrl_regs);
128887ab2079SAlex Williams 	}
128987ab2079SAlex Williams 	return 0;
129087ab2079SAlex Williams }
129187ab2079SAlex Williams 
1292492caffaSMoritz Fischer static int nixge_probe(struct platform_device *pdev)
1293492caffaSMoritz Fischer {
12948dc0ae90SMoritz Fischer 	struct device_node *mn, *phy_node;
1295492caffaSMoritz Fischer 	struct nixge_priv *priv;
1296492caffaSMoritz Fischer 	struct net_device *ndev;
1297a86b74d3SMoritz Fischer 	const u8 *mac_addr;
1298492caffaSMoritz Fischer 	int err;
1299492caffaSMoritz Fischer 
1300492caffaSMoritz Fischer 	ndev = alloc_etherdev(sizeof(*priv));
1301492caffaSMoritz Fischer 	if (!ndev)
1302492caffaSMoritz Fischer 		return -ENOMEM;
1303492caffaSMoritz Fischer 
1304492caffaSMoritz Fischer 	platform_set_drvdata(pdev, ndev);
1305492caffaSMoritz Fischer 	SET_NETDEV_DEV(ndev, &pdev->dev);
1306492caffaSMoritz Fischer 
1307492caffaSMoritz Fischer 	ndev->features = NETIF_F_SG;
1308492caffaSMoritz Fischer 	ndev->netdev_ops = &nixge_netdev_ops;
1309492caffaSMoritz Fischer 	ndev->ethtool_ops = &nixge_ethtool_ops;
1310492caffaSMoritz Fischer 
1311492caffaSMoritz Fischer 	/* MTU range: 64 - 9000 */
1312492caffaSMoritz Fischer 	ndev->min_mtu = 64;
1313492caffaSMoritz Fischer 	ndev->max_mtu = NIXGE_JUMBO_MTU;
1314492caffaSMoritz Fischer 
1315492caffaSMoritz Fischer 	mac_addr = nixge_get_nvmem_address(&pdev->dev);
1316a68229caSArnd Bergmann 	if (!IS_ERR(mac_addr) && is_valid_ether_addr(mac_addr)) {
1317f3956ebbSJakub Kicinski 		eth_hw_addr_set(ndev, mac_addr);
1318abcd3d6fSMoritz Fischer 		kfree(mac_addr);
1319abcd3d6fSMoritz Fischer 	} else {
1320492caffaSMoritz Fischer 		eth_hw_addr_random(ndev);
1321abcd3d6fSMoritz Fischer 	}
1322492caffaSMoritz Fischer 
1323492caffaSMoritz Fischer 	priv = netdev_priv(ndev);
1324492caffaSMoritz Fischer 	priv->ndev = ndev;
1325492caffaSMoritz Fischer 	priv->dev = &pdev->dev;
1326492caffaSMoritz Fischer 
1327b48b89f9SJakub Kicinski 	netif_napi_add(ndev, &priv->napi, nixge_poll);
132887ab2079SAlex Williams 	err = nixge_of_get_resources(pdev);
132987ab2079SAlex Williams 	if (err)
1330366228edSLu Wei 		goto free_netdev;
1331492caffaSMoritz Fischer 	__nixge_hw_set_mac_address(ndev);
1332492caffaSMoritz Fischer 
1333492caffaSMoritz Fischer 	priv->tx_irq = platform_get_irq_byname(pdev, "tx");
1334492caffaSMoritz Fischer 	if (priv->tx_irq < 0) {
1335492caffaSMoritz Fischer 		netdev_err(ndev, "could not find 'tx' irq");
1336366228edSLu Wei 		err = priv->tx_irq;
1337366228edSLu Wei 		goto free_netdev;
1338492caffaSMoritz Fischer 	}
1339492caffaSMoritz Fischer 
1340492caffaSMoritz Fischer 	priv->rx_irq = platform_get_irq_byname(pdev, "rx");
1341492caffaSMoritz Fischer 	if (priv->rx_irq < 0) {
1342492caffaSMoritz Fischer 		netdev_err(ndev, "could not find 'rx' irq");
1343366228edSLu Wei 		err = priv->rx_irq;
1344366228edSLu Wei 		goto free_netdev;
1345492caffaSMoritz Fischer 	}
1346492caffaSMoritz Fischer 
1347492caffaSMoritz Fischer 	priv->coalesce_count_rx = XAXIDMA_DFT_RX_THRESHOLD;
1348492caffaSMoritz Fischer 	priv->coalesce_count_tx = XAXIDMA_DFT_TX_THRESHOLD;
1349492caffaSMoritz Fischer 
1350dd648818SMoritz Fischer 	mn = of_get_child_by_name(pdev->dev.of_node, "mdio");
1351dd648818SMoritz Fischer 	if (mn) {
1352dd648818SMoritz Fischer 		err = nixge_mdio_setup(priv, mn);
1353dd648818SMoritz Fischer 		of_node_put(mn);
1354492caffaSMoritz Fischer 		if (err) {
1355492caffaSMoritz Fischer 			netdev_err(ndev, "error registering mdio bus");
1356492caffaSMoritz Fischer 			goto free_netdev;
1357492caffaSMoritz Fischer 		}
1358dd648818SMoritz Fischer 	}
1359492caffaSMoritz Fischer 
13600c65b2b9SAndrew Lunn 	err = of_get_phy_mode(pdev->dev.of_node, &priv->phy_mode);
13610c65b2b9SAndrew Lunn 	if (err) {
1362492caffaSMoritz Fischer 		netdev_err(ndev, "not find \"phy-mode\" property\n");
1363492caffaSMoritz Fischer 		goto unregister_mdio;
1364492caffaSMoritz Fischer 	}
1365492caffaSMoritz Fischer 
13668dc0ae90SMoritz Fischer 	phy_node = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
13678dc0ae90SMoritz Fischer 	if (!phy_node && of_phy_is_fixed_link(pdev->dev.of_node)) {
13688dc0ae90SMoritz Fischer 		err = of_phy_register_fixed_link(pdev->dev.of_node);
13698dc0ae90SMoritz Fischer 		if (err < 0) {
13708dc0ae90SMoritz Fischer 			netdev_err(ndev, "broken fixed-link specification\n");
1371492caffaSMoritz Fischer 			goto unregister_mdio;
1372492caffaSMoritz Fischer 		}
13738dc0ae90SMoritz Fischer 		phy_node = of_node_get(pdev->dev.of_node);
13748dc0ae90SMoritz Fischer 	}
13758dc0ae90SMoritz Fischer 	priv->phy_node = phy_node;
1376492caffaSMoritz Fischer 
1377492caffaSMoritz Fischer 	err = register_netdev(priv->ndev);
1378492caffaSMoritz Fischer 	if (err) {
1379492caffaSMoritz Fischer 		netdev_err(ndev, "register_netdev() error (%i)\n", err);
13808dc0ae90SMoritz Fischer 		goto free_phy;
1381492caffaSMoritz Fischer 	}
1382492caffaSMoritz Fischer 
1383492caffaSMoritz Fischer 	return 0;
1384492caffaSMoritz Fischer 
13858dc0ae90SMoritz Fischer free_phy:
13868dc0ae90SMoritz Fischer 	if (of_phy_is_fixed_link(pdev->dev.of_node))
13878dc0ae90SMoritz Fischer 		of_phy_deregister_fixed_link(pdev->dev.of_node);
13888dc0ae90SMoritz Fischer 	of_node_put(phy_node);
13898dc0ae90SMoritz Fischer 
1390492caffaSMoritz Fischer unregister_mdio:
1391dd648818SMoritz Fischer 	if (priv->mii_bus)
1392492caffaSMoritz Fischer 		mdiobus_unregister(priv->mii_bus);
1393492caffaSMoritz Fischer 
1394492caffaSMoritz Fischer free_netdev:
1395492caffaSMoritz Fischer 	free_netdev(ndev);
1396492caffaSMoritz Fischer 
1397492caffaSMoritz Fischer 	return err;
1398492caffaSMoritz Fischer }
1399492caffaSMoritz Fischer 
1400492caffaSMoritz Fischer static int nixge_remove(struct platform_device *pdev)
1401492caffaSMoritz Fischer {
1402492caffaSMoritz Fischer 	struct net_device *ndev = platform_get_drvdata(pdev);
1403492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1404492caffaSMoritz Fischer 
1405492caffaSMoritz Fischer 	unregister_netdev(ndev);
1406492caffaSMoritz Fischer 
14078dc0ae90SMoritz Fischer 	if (of_phy_is_fixed_link(pdev->dev.of_node))
14088dc0ae90SMoritz Fischer 		of_phy_deregister_fixed_link(pdev->dev.of_node);
14098dc0ae90SMoritz Fischer 	of_node_put(priv->phy_node);
14108dc0ae90SMoritz Fischer 
1411dd648818SMoritz Fischer 	if (priv->mii_bus)
1412492caffaSMoritz Fischer 		mdiobus_unregister(priv->mii_bus);
1413492caffaSMoritz Fischer 
1414492caffaSMoritz Fischer 	free_netdev(ndev);
1415492caffaSMoritz Fischer 
1416492caffaSMoritz Fischer 	return 0;
1417492caffaSMoritz Fischer }
1418492caffaSMoritz Fischer 
1419492caffaSMoritz Fischer static struct platform_driver nixge_driver = {
1420492caffaSMoritz Fischer 	.probe		= nixge_probe,
1421492caffaSMoritz Fischer 	.remove		= nixge_remove,
1422492caffaSMoritz Fischer 	.driver		= {
1423492caffaSMoritz Fischer 		.name		= "nixge",
14247e9aa8caSKrzysztof Kozlowski 		.of_match_table	= nixge_dt_ids,
1425492caffaSMoritz Fischer 	},
1426492caffaSMoritz Fischer };
1427492caffaSMoritz Fischer module_platform_driver(nixge_driver);
1428492caffaSMoritz Fischer 
1429492caffaSMoritz Fischer MODULE_LICENSE("GPL v2");
1430492caffaSMoritz Fischer MODULE_DESCRIPTION("National Instruments XGE Management MAC");
1431492caffaSMoritz Fischer MODULE_AUTHOR("Moritz Fischer <mdf@kernel.org>");
1432