xref: /openbmc/linux/drivers/net/ethernet/ni/nixge.c (revision a68229ca634066975fff6d4780155bd2eb14a82a)
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>
10492caffaSMoritz Fischer #include <linux/of_address.h>
11492caffaSMoritz Fischer #include <linux/of_mdio.h>
12492caffaSMoritz Fischer #include <linux/of_net.h>
13492caffaSMoritz Fischer #include <linux/of_platform.h>
14492caffaSMoritz Fischer #include <linux/of_irq.h>
15492caffaSMoritz Fischer #include <linux/skbuff.h>
16492caffaSMoritz Fischer #include <linux/phy.h>
17492caffaSMoritz Fischer #include <linux/mii.h>
18492caffaSMoritz Fischer #include <linux/nvmem-consumer.h>
19492caffaSMoritz Fischer #include <linux/ethtool.h>
20492caffaSMoritz Fischer #include <linux/iopoll.h>
21492caffaSMoritz Fischer 
22492caffaSMoritz Fischer #define TX_BD_NUM		64
23492caffaSMoritz Fischer #define RX_BD_NUM		128
24492caffaSMoritz Fischer 
25492caffaSMoritz Fischer /* Axi DMA Register definitions */
26492caffaSMoritz Fischer #define XAXIDMA_TX_CR_OFFSET	0x00 /* Channel control */
27492caffaSMoritz Fischer #define XAXIDMA_TX_SR_OFFSET	0x04 /* Status */
28492caffaSMoritz Fischer #define XAXIDMA_TX_CDESC_OFFSET	0x08 /* Current descriptor pointer */
29492caffaSMoritz Fischer #define XAXIDMA_TX_TDESC_OFFSET	0x10 /* Tail descriptor pointer */
30492caffaSMoritz Fischer 
31492caffaSMoritz Fischer #define XAXIDMA_RX_CR_OFFSET	0x30 /* Channel control */
32492caffaSMoritz Fischer #define XAXIDMA_RX_SR_OFFSET	0x34 /* Status */
33492caffaSMoritz Fischer #define XAXIDMA_RX_CDESC_OFFSET	0x38 /* Current descriptor pointer */
34492caffaSMoritz Fischer #define XAXIDMA_RX_TDESC_OFFSET	0x40 /* Tail descriptor pointer */
35492caffaSMoritz Fischer 
36492caffaSMoritz Fischer #define XAXIDMA_CR_RUNSTOP_MASK	0x1 /* Start/stop DMA channel */
37492caffaSMoritz Fischer #define XAXIDMA_CR_RESET_MASK	0x4 /* Reset DMA engine */
38492caffaSMoritz Fischer 
39492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_LENGTH_MASK	0x007FFFFF /* Requested len */
40492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_TXSOF_MASK	0x08000000 /* First tx packet */
41492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_TXEOF_MASK	0x04000000 /* Last tx packet */
42492caffaSMoritz Fischer #define XAXIDMA_BD_CTRL_ALL_MASK	0x0C000000 /* All control bits */
43492caffaSMoritz Fischer 
44492caffaSMoritz Fischer #define XAXIDMA_DELAY_MASK		0xFF000000 /* Delay timeout counter */
45492caffaSMoritz Fischer #define XAXIDMA_COALESCE_MASK		0x00FF0000 /* Coalesce counter */
46492caffaSMoritz Fischer 
47492caffaSMoritz Fischer #define XAXIDMA_DELAY_SHIFT		24
48492caffaSMoritz Fischer #define XAXIDMA_COALESCE_SHIFT		16
49492caffaSMoritz Fischer 
50492caffaSMoritz Fischer #define XAXIDMA_IRQ_IOC_MASK		0x00001000 /* Completion intr */
51492caffaSMoritz Fischer #define XAXIDMA_IRQ_DELAY_MASK		0x00002000 /* Delay interrupt */
52492caffaSMoritz Fischer #define XAXIDMA_IRQ_ERROR_MASK		0x00004000 /* Error interrupt */
53492caffaSMoritz Fischer #define XAXIDMA_IRQ_ALL_MASK		0x00007000 /* All interrupts */
54492caffaSMoritz Fischer 
55492caffaSMoritz Fischer /* Default TX/RX Threshold and waitbound values for SGDMA mode */
56492caffaSMoritz Fischer #define XAXIDMA_DFT_TX_THRESHOLD	24
57492caffaSMoritz Fischer #define XAXIDMA_DFT_TX_WAITBOUND	254
58492caffaSMoritz Fischer #define XAXIDMA_DFT_RX_THRESHOLD	24
59492caffaSMoritz Fischer #define XAXIDMA_DFT_RX_WAITBOUND	254
60492caffaSMoritz Fischer 
61492caffaSMoritz Fischer #define XAXIDMA_BD_STS_ACTUAL_LEN_MASK	0x007FFFFF /* Actual len */
62492caffaSMoritz Fischer #define XAXIDMA_BD_STS_COMPLETE_MASK	0x80000000 /* Completed */
63492caffaSMoritz Fischer #define XAXIDMA_BD_STS_DEC_ERR_MASK	0x40000000 /* Decode error */
64492caffaSMoritz Fischer #define XAXIDMA_BD_STS_SLV_ERR_MASK	0x20000000 /* Slave error */
65492caffaSMoritz Fischer #define XAXIDMA_BD_STS_INT_ERR_MASK	0x10000000 /* Internal err */
66492caffaSMoritz Fischer #define XAXIDMA_BD_STS_ALL_ERR_MASK	0x70000000 /* All errors */
67492caffaSMoritz Fischer #define XAXIDMA_BD_STS_RXSOF_MASK	0x08000000 /* First rx pkt */
68492caffaSMoritz Fischer #define XAXIDMA_BD_STS_RXEOF_MASK	0x04000000 /* Last rx pkt */
69492caffaSMoritz Fischer #define XAXIDMA_BD_STS_ALL_MASK		0xFC000000 /* All status bits */
70492caffaSMoritz Fischer 
71492caffaSMoritz Fischer #define NIXGE_REG_CTRL_OFFSET	0x4000
72492caffaSMoritz Fischer #define NIXGE_REG_INFO		0x00
73492caffaSMoritz Fischer #define NIXGE_REG_MAC_CTL	0x04
74492caffaSMoritz Fischer #define NIXGE_REG_PHY_CTL	0x08
75492caffaSMoritz Fischer #define NIXGE_REG_LED_CTL	0x0c
76492caffaSMoritz Fischer #define NIXGE_REG_MDIO_DATA	0x10
77492caffaSMoritz Fischer #define NIXGE_REG_MDIO_ADDR	0x14
78492caffaSMoritz Fischer #define NIXGE_REG_MDIO_OP	0x18
79492caffaSMoritz Fischer #define NIXGE_REG_MDIO_CTRL	0x1c
80492caffaSMoritz Fischer 
81492caffaSMoritz Fischer #define NIXGE_ID_LED_CTL_EN	BIT(0)
82492caffaSMoritz Fischer #define NIXGE_ID_LED_CTL_VAL	BIT(1)
83492caffaSMoritz Fischer 
84492caffaSMoritz Fischer #define NIXGE_MDIO_CLAUSE45	BIT(12)
85492caffaSMoritz Fischer #define NIXGE_MDIO_CLAUSE22	0
86492caffaSMoritz Fischer #define NIXGE_MDIO_OP(n)     (((n) & 0x3) << 10)
87492caffaSMoritz Fischer #define NIXGE_MDIO_OP_ADDRESS	0
88492caffaSMoritz Fischer #define NIXGE_MDIO_C45_WRITE	BIT(0)
89492caffaSMoritz Fischer #define NIXGE_MDIO_C45_READ	(BIT(1) | BIT(0))
90492caffaSMoritz Fischer #define NIXGE_MDIO_C22_WRITE	BIT(0)
91492caffaSMoritz Fischer #define NIXGE_MDIO_C22_READ	BIT(1)
92492caffaSMoritz Fischer #define NIXGE_MDIO_ADDR(n)   (((n) & 0x1f) << 5)
93492caffaSMoritz Fischer #define NIXGE_MDIO_MMD(n)    (((n) & 0x1f) << 0)
94492caffaSMoritz Fischer 
95492caffaSMoritz Fischer #define NIXGE_REG_MAC_LSB	0x1000
96492caffaSMoritz Fischer #define NIXGE_REG_MAC_MSB	0x1004
97492caffaSMoritz Fischer 
98492caffaSMoritz Fischer /* Packet size info */
99492caffaSMoritz Fischer #define NIXGE_HDR_SIZE		14 /* Size of Ethernet header */
100492caffaSMoritz Fischer #define NIXGE_TRL_SIZE		4 /* Size of Ethernet trailer (FCS) */
101492caffaSMoritz Fischer #define NIXGE_MTU		1500 /* Max MTU of an Ethernet frame */
102492caffaSMoritz Fischer #define NIXGE_JUMBO_MTU		9000 /* Max MTU of a jumbo Eth. frame */
103492caffaSMoritz Fischer 
104492caffaSMoritz Fischer #define NIXGE_MAX_FRAME_SIZE	 (NIXGE_MTU + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE)
105492caffaSMoritz Fischer #define NIXGE_MAX_JUMBO_FRAME_SIZE \
106492caffaSMoritz Fischer 	(NIXGE_JUMBO_MTU + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE)
107492caffaSMoritz Fischer 
10887ab2079SAlex Williams enum nixge_version {
10987ab2079SAlex Williams 	NIXGE_V2,
11087ab2079SAlex Williams 	NIXGE_V3,
11187ab2079SAlex Williams 	NIXGE_VERSION_COUNT
11287ab2079SAlex Williams };
11387ab2079SAlex Williams 
114492caffaSMoritz Fischer struct nixge_hw_dma_bd {
1157e8d5755SMoritz Fischer 	u32 next_lo;
1167e8d5755SMoritz Fischer 	u32 next_hi;
1177e8d5755SMoritz Fischer 	u32 phys_lo;
1187e8d5755SMoritz Fischer 	u32 phys_hi;
119492caffaSMoritz Fischer 	u32 reserved3;
120492caffaSMoritz Fischer 	u32 reserved4;
121492caffaSMoritz Fischer 	u32 cntrl;
122492caffaSMoritz Fischer 	u32 status;
123492caffaSMoritz Fischer 	u32 app0;
124492caffaSMoritz Fischer 	u32 app1;
125492caffaSMoritz Fischer 	u32 app2;
126492caffaSMoritz Fischer 	u32 app3;
127492caffaSMoritz Fischer 	u32 app4;
1287e8d5755SMoritz Fischer 	u32 sw_id_offset_lo;
1297e8d5755SMoritz Fischer 	u32 sw_id_offset_hi;
130492caffaSMoritz Fischer 	u32 reserved6;
131492caffaSMoritz Fischer };
132492caffaSMoritz Fischer 
1337e8d5755SMoritz Fischer #ifdef CONFIG_PHYS_ADDR_T_64BIT
1347e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_addr(bd, field, addr) \
1357e8d5755SMoritz Fischer 	do { \
136ea43a590SMoritz Fischer 		(bd)->field##_lo = lower_32_bits((addr)); \
137ea43a590SMoritz Fischer 		(bd)->field##_hi = upper_32_bits((addr)); \
1387e8d5755SMoritz Fischer 	} while (0)
1397e8d5755SMoritz Fischer #else
1407e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_addr(bd, field, addr) \
1417e8d5755SMoritz Fischer 	((bd)->field##_lo = lower_32_bits((addr)))
1427e8d5755SMoritz Fischer #endif
1437e8d5755SMoritz Fischer 
1447e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_phys(bd, addr) \
1457e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_addr((bd), phys, (addr))
1467e8d5755SMoritz Fischer 
1477e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_next(bd, addr) \
1487e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_addr((bd), next, (addr))
1497e8d5755SMoritz Fischer 
1507e8d5755SMoritz Fischer #define nixge_hw_dma_bd_set_offset(bd, addr) \
1517e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_addr((bd), sw_id_offset, (addr))
1527e8d5755SMoritz Fischer 
1537e8d5755SMoritz Fischer #ifdef CONFIG_PHYS_ADDR_T_64BIT
1547e8d5755SMoritz Fischer #define nixge_hw_dma_bd_get_addr(bd, field) \
1557e8d5755SMoritz Fischer 	(dma_addr_t)((((u64)(bd)->field##_hi) << 32) | ((bd)->field##_lo))
1567e8d5755SMoritz Fischer #else
1577e8d5755SMoritz Fischer #define nixge_hw_dma_bd_get_addr(bd, field) \
1587e8d5755SMoritz Fischer 	(dma_addr_t)((bd)->field##_lo)
1597e8d5755SMoritz Fischer #endif
1607e8d5755SMoritz Fischer 
161492caffaSMoritz Fischer struct nixge_tx_skb {
162492caffaSMoritz Fischer 	struct sk_buff *skb;
163492caffaSMoritz Fischer 	dma_addr_t mapping;
164492caffaSMoritz Fischer 	size_t size;
165492caffaSMoritz Fischer 	bool mapped_as_page;
166492caffaSMoritz Fischer };
167492caffaSMoritz Fischer 
168492caffaSMoritz Fischer struct nixge_priv {
169492caffaSMoritz Fischer 	struct net_device *ndev;
170492caffaSMoritz Fischer 	struct napi_struct napi;
171492caffaSMoritz Fischer 	struct device *dev;
172492caffaSMoritz Fischer 
173492caffaSMoritz Fischer 	/* Connection to PHY device */
174492caffaSMoritz Fischer 	struct device_node *phy_node;
175492caffaSMoritz Fischer 	phy_interface_t		phy_mode;
176492caffaSMoritz Fischer 
177492caffaSMoritz Fischer 	int link;
178492caffaSMoritz Fischer 	unsigned int speed;
179492caffaSMoritz Fischer 	unsigned int duplex;
180492caffaSMoritz Fischer 
181492caffaSMoritz Fischer 	/* MDIO bus data */
182492caffaSMoritz Fischer 	struct mii_bus *mii_bus;	/* MII bus reference */
183492caffaSMoritz Fischer 
184492caffaSMoritz Fischer 	/* IO registers, dma functions and IRQs */
185492caffaSMoritz Fischer 	void __iomem *ctrl_regs;
186492caffaSMoritz Fischer 	void __iomem *dma_regs;
187492caffaSMoritz Fischer 
188492caffaSMoritz Fischer 	struct tasklet_struct dma_err_tasklet;
189492caffaSMoritz Fischer 
190492caffaSMoritz Fischer 	int tx_irq;
191492caffaSMoritz Fischer 	int rx_irq;
192492caffaSMoritz Fischer 
193492caffaSMoritz Fischer 	/* Buffer descriptors */
194492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *tx_bd_v;
195492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
196492caffaSMoritz Fischer 	dma_addr_t tx_bd_p;
197492caffaSMoritz Fischer 
198492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *rx_bd_v;
199492caffaSMoritz Fischer 	dma_addr_t rx_bd_p;
200492caffaSMoritz Fischer 	u32 tx_bd_ci;
201492caffaSMoritz Fischer 	u32 tx_bd_tail;
202492caffaSMoritz Fischer 	u32 rx_bd_ci;
203492caffaSMoritz Fischer 
204492caffaSMoritz Fischer 	u32 coalesce_count_rx;
205492caffaSMoritz Fischer 	u32 coalesce_count_tx;
206492caffaSMoritz Fischer };
207492caffaSMoritz Fischer 
208492caffaSMoritz Fischer static void nixge_dma_write_reg(struct nixge_priv *priv, off_t offset, u32 val)
209492caffaSMoritz Fischer {
210492caffaSMoritz Fischer 	writel(val, priv->dma_regs + offset);
211492caffaSMoritz Fischer }
212492caffaSMoritz Fischer 
2137e8d5755SMoritz Fischer static void nixge_dma_write_desc_reg(struct nixge_priv *priv, off_t offset,
2147e8d5755SMoritz Fischer 				     dma_addr_t addr)
2157e8d5755SMoritz Fischer {
2167e8d5755SMoritz Fischer 	writel(lower_32_bits(addr), priv->dma_regs + offset);
2177e8d5755SMoritz Fischer #ifdef CONFIG_PHYS_ADDR_T_64BIT
2187e8d5755SMoritz Fischer 	writel(upper_32_bits(addr), priv->dma_regs + offset + 4);
2197e8d5755SMoritz Fischer #endif
2207e8d5755SMoritz Fischer }
2217e8d5755SMoritz Fischer 
222492caffaSMoritz Fischer static u32 nixge_dma_read_reg(const struct nixge_priv *priv, off_t offset)
223492caffaSMoritz Fischer {
224492caffaSMoritz Fischer 	return readl(priv->dma_regs + offset);
225492caffaSMoritz Fischer }
226492caffaSMoritz Fischer 
227492caffaSMoritz Fischer static void nixge_ctrl_write_reg(struct nixge_priv *priv, off_t offset, u32 val)
228492caffaSMoritz Fischer {
229492caffaSMoritz Fischer 	writel(val, priv->ctrl_regs + offset);
230492caffaSMoritz Fischer }
231492caffaSMoritz Fischer 
232492caffaSMoritz Fischer static u32 nixge_ctrl_read_reg(struct nixge_priv *priv, off_t offset)
233492caffaSMoritz Fischer {
234492caffaSMoritz Fischer 	return readl(priv->ctrl_regs + offset);
235492caffaSMoritz Fischer }
236492caffaSMoritz Fischer 
237492caffaSMoritz Fischer #define nixge_ctrl_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
238492caffaSMoritz Fischer 	readl_poll_timeout((priv)->ctrl_regs + (addr), (val), (cond), \
239492caffaSMoritz Fischer 			   (sleep_us), (timeout_us))
240492caffaSMoritz Fischer 
241492caffaSMoritz Fischer #define nixge_dma_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
242492caffaSMoritz Fischer 	readl_poll_timeout((priv)->dma_regs + (addr), (val), (cond), \
243492caffaSMoritz Fischer 			   (sleep_us), (timeout_us))
244492caffaSMoritz Fischer 
245492caffaSMoritz Fischer static void nixge_hw_dma_bd_release(struct net_device *ndev)
246492caffaSMoritz Fischer {
247492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
2487e8d5755SMoritz Fischer 	dma_addr_t phys_addr;
2497e8d5755SMoritz Fischer 	struct sk_buff *skb;
250492caffaSMoritz Fischer 	int i;
251492caffaSMoritz Fischer 
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 	if (priv->rx_bd_v)
267492caffaSMoritz Fischer 		dma_free_coherent(ndev->dev.parent,
268492caffaSMoritz Fischer 				  sizeof(*priv->rx_bd_v) * RX_BD_NUM,
269492caffaSMoritz Fischer 				  priv->rx_bd_v,
270492caffaSMoritz Fischer 				  priv->rx_bd_p);
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 
327492caffaSMoritz Fischer 		skb = netdev_alloc_skb_ip_align(ndev,
328492caffaSMoritz Fischer 						NIXGE_MAX_JUMBO_FRAME_SIZE);
329492caffaSMoritz Fischer 		if (!skb)
330492caffaSMoritz Fischer 			goto out;
331492caffaSMoritz Fischer 
332ea43a590SMoritz Fischer 		nixge_hw_dma_bd_set_offset(&priv->rx_bd_v[i], (uintptr_t)skb);
3337e8d5755SMoritz Fischer 		phys = dma_map_single(ndev->dev.parent, skb->data,
334492caffaSMoritz Fischer 				      NIXGE_MAX_JUMBO_FRAME_SIZE,
335492caffaSMoritz Fischer 				      DMA_FROM_DEVICE);
3367e8d5755SMoritz Fischer 
3377e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(&priv->rx_bd_v[i], phys);
3387e8d5755SMoritz Fischer 
339492caffaSMoritz Fischer 		priv->rx_bd_v[i].cntrl = NIXGE_MAX_JUMBO_FRAME_SIZE;
340492caffaSMoritz Fischer 	}
341492caffaSMoritz Fischer 
342492caffaSMoritz Fischer 	/* Start updating the Rx channel control register */
343492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
344492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
345492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
346492caffaSMoritz Fischer 	      ((priv->coalesce_count_rx) << XAXIDMA_COALESCE_SHIFT));
347492caffaSMoritz Fischer 	/* Update the delay timer count */
348492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_DELAY_MASK) |
349492caffaSMoritz Fischer 	      (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
350492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
351492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
352492caffaSMoritz Fischer 	/* Write to the Rx channel control register */
353492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
354492caffaSMoritz Fischer 
355492caffaSMoritz Fischer 	/* Start updating the Tx channel control register */
356492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
357492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
358492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
359492caffaSMoritz Fischer 	      ((priv->coalesce_count_tx) << XAXIDMA_COALESCE_SHIFT));
360492caffaSMoritz Fischer 	/* Update the delay timer count */
361492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
362492caffaSMoritz Fischer 	      (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
363492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
364492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
365492caffaSMoritz Fischer 	/* Write to the Tx channel control register */
366492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
367492caffaSMoritz Fischer 
368492caffaSMoritz Fischer 	/* Populate the tail pointer and bring the Rx Axi DMA engine out of
369492caffaSMoritz Fischer 	 * halted state. This will make the Rx side ready for reception.
370492caffaSMoritz Fischer 	 */
3717e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_RX_CDESC_OFFSET, priv->rx_bd_p);
372492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
373492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET,
374492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
3757e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_RX_TDESC_OFFSET, priv->rx_bd_p +
376492caffaSMoritz Fischer 			    (sizeof(*priv->rx_bd_v) * (RX_BD_NUM - 1)));
377492caffaSMoritz Fischer 
378492caffaSMoritz Fischer 	/* Write to the RS (Run-stop) bit in the Tx channel control register.
379492caffaSMoritz Fischer 	 * Tx channel is now ready to run. But only after we write to the
380492caffaSMoritz Fischer 	 * tail pointer register that the Tx channel will start transmitting.
381492caffaSMoritz Fischer 	 */
3827e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_TX_CDESC_OFFSET, priv->tx_bd_p);
383492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
384492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET,
385492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
386492caffaSMoritz Fischer 
387492caffaSMoritz Fischer 	return 0;
388492caffaSMoritz Fischer out:
389492caffaSMoritz Fischer 	nixge_hw_dma_bd_release(ndev);
390492caffaSMoritz Fischer 	return -ENOMEM;
391492caffaSMoritz Fischer }
392492caffaSMoritz Fischer 
393492caffaSMoritz Fischer static void __nixge_device_reset(struct nixge_priv *priv, off_t offset)
394492caffaSMoritz Fischer {
395492caffaSMoritz Fischer 	u32 status;
396492caffaSMoritz Fischer 	int err;
397492caffaSMoritz Fischer 
398492caffaSMoritz Fischer 	/* Reset Axi DMA. This would reset NIXGE Ethernet core as well.
399492caffaSMoritz Fischer 	 * The reset process of Axi DMA takes a while to complete as all
400492caffaSMoritz Fischer 	 * pending commands/transfers will be flushed or completed during
401492caffaSMoritz Fischer 	 * this reset process.
402492caffaSMoritz Fischer 	 */
403492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, offset, XAXIDMA_CR_RESET_MASK);
404492caffaSMoritz Fischer 	err = nixge_dma_poll_timeout(priv, offset, status,
405492caffaSMoritz Fischer 				     !(status & XAXIDMA_CR_RESET_MASK), 10,
406492caffaSMoritz Fischer 				     1000);
407492caffaSMoritz Fischer 	if (err)
408492caffaSMoritz Fischer 		netdev_err(priv->ndev, "%s: DMA reset timeout!\n", __func__);
409492caffaSMoritz Fischer }
410492caffaSMoritz Fischer 
411492caffaSMoritz Fischer static void nixge_device_reset(struct net_device *ndev)
412492caffaSMoritz Fischer {
413492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
414492caffaSMoritz Fischer 
415492caffaSMoritz Fischer 	__nixge_device_reset(priv, XAXIDMA_TX_CR_OFFSET);
416492caffaSMoritz Fischer 	__nixge_device_reset(priv, XAXIDMA_RX_CR_OFFSET);
417492caffaSMoritz Fischer 
418492caffaSMoritz Fischer 	if (nixge_hw_dma_bd_init(ndev))
419492caffaSMoritz Fischer 		netdev_err(ndev, "%s: descriptor allocation failed\n",
420492caffaSMoritz Fischer 			   __func__);
421492caffaSMoritz Fischer 
422492caffaSMoritz Fischer 	netif_trans_update(ndev);
423492caffaSMoritz Fischer }
424492caffaSMoritz Fischer 
425492caffaSMoritz Fischer static void nixge_handle_link_change(struct net_device *ndev)
426492caffaSMoritz Fischer {
427492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
428492caffaSMoritz Fischer 	struct phy_device *phydev = ndev->phydev;
429492caffaSMoritz Fischer 
430492caffaSMoritz Fischer 	if (phydev->link != priv->link || phydev->speed != priv->speed ||
431492caffaSMoritz Fischer 	    phydev->duplex != priv->duplex) {
432492caffaSMoritz Fischer 		priv->link = phydev->link;
433492caffaSMoritz Fischer 		priv->speed = phydev->speed;
434492caffaSMoritz Fischer 		priv->duplex = phydev->duplex;
435492caffaSMoritz Fischer 		phy_print_status(phydev);
436492caffaSMoritz Fischer 	}
437492caffaSMoritz Fischer }
438492caffaSMoritz Fischer 
439492caffaSMoritz Fischer static void nixge_tx_skb_unmap(struct nixge_priv *priv,
440492caffaSMoritz Fischer 			       struct nixge_tx_skb *tx_skb)
441492caffaSMoritz Fischer {
442492caffaSMoritz Fischer 	if (tx_skb->mapping) {
443492caffaSMoritz Fischer 		if (tx_skb->mapped_as_page)
444492caffaSMoritz Fischer 			dma_unmap_page(priv->ndev->dev.parent, tx_skb->mapping,
445492caffaSMoritz Fischer 				       tx_skb->size, DMA_TO_DEVICE);
446492caffaSMoritz Fischer 		else
447492caffaSMoritz Fischer 			dma_unmap_single(priv->ndev->dev.parent,
448492caffaSMoritz Fischer 					 tx_skb->mapping,
449492caffaSMoritz Fischer 					 tx_skb->size, DMA_TO_DEVICE);
450492caffaSMoritz Fischer 		tx_skb->mapping = 0;
451492caffaSMoritz Fischer 	}
452492caffaSMoritz Fischer 
453492caffaSMoritz Fischer 	if (tx_skb->skb) {
454492caffaSMoritz Fischer 		dev_kfree_skb_any(tx_skb->skb);
455492caffaSMoritz Fischer 		tx_skb->skb = NULL;
456492caffaSMoritz Fischer 	}
457492caffaSMoritz Fischer }
458492caffaSMoritz Fischer 
459492caffaSMoritz Fischer static void nixge_start_xmit_done(struct net_device *ndev)
460492caffaSMoritz Fischer {
461492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
462492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
463492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
464492caffaSMoritz Fischer 	unsigned int status = 0;
465492caffaSMoritz Fischer 	u32 packets = 0;
466492caffaSMoritz Fischer 	u32 size = 0;
467492caffaSMoritz Fischer 
468492caffaSMoritz Fischer 	cur_p = &priv->tx_bd_v[priv->tx_bd_ci];
469492caffaSMoritz Fischer 	tx_skb = &priv->tx_skb[priv->tx_bd_ci];
470492caffaSMoritz Fischer 
471492caffaSMoritz Fischer 	status = cur_p->status;
472492caffaSMoritz Fischer 
473492caffaSMoritz Fischer 	while (status & XAXIDMA_BD_STS_COMPLETE_MASK) {
474492caffaSMoritz Fischer 		nixge_tx_skb_unmap(priv, tx_skb);
475492caffaSMoritz Fischer 		cur_p->status = 0;
476492caffaSMoritz Fischer 
477492caffaSMoritz Fischer 		size += status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
478492caffaSMoritz Fischer 		packets++;
479492caffaSMoritz Fischer 
480492caffaSMoritz Fischer 		++priv->tx_bd_ci;
481492caffaSMoritz Fischer 		priv->tx_bd_ci %= TX_BD_NUM;
482492caffaSMoritz Fischer 		cur_p = &priv->tx_bd_v[priv->tx_bd_ci];
483492caffaSMoritz Fischer 		tx_skb = &priv->tx_skb[priv->tx_bd_ci];
484492caffaSMoritz Fischer 		status = cur_p->status;
485492caffaSMoritz Fischer 	}
486492caffaSMoritz Fischer 
487492caffaSMoritz Fischer 	ndev->stats.tx_packets += packets;
488492caffaSMoritz Fischer 	ndev->stats.tx_bytes += size;
489492caffaSMoritz Fischer 
490492caffaSMoritz Fischer 	if (packets)
491492caffaSMoritz Fischer 		netif_wake_queue(ndev);
492492caffaSMoritz Fischer }
493492caffaSMoritz Fischer 
494492caffaSMoritz Fischer static int nixge_check_tx_bd_space(struct nixge_priv *priv,
495492caffaSMoritz Fischer 				   int num_frag)
496492caffaSMoritz Fischer {
497492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
498492caffaSMoritz Fischer 
499492caffaSMoritz Fischer 	cur_p = &priv->tx_bd_v[(priv->tx_bd_tail + num_frag) % TX_BD_NUM];
500492caffaSMoritz Fischer 	if (cur_p->status & XAXIDMA_BD_STS_ALL_MASK)
501492caffaSMoritz Fischer 		return NETDEV_TX_BUSY;
502492caffaSMoritz Fischer 	return 0;
503492caffaSMoritz Fischer }
504492caffaSMoritz Fischer 
505015cba7eSYunjian Wang static netdev_tx_t nixge_start_xmit(struct sk_buff *skb,
506015cba7eSYunjian Wang 				    struct net_device *ndev)
507492caffaSMoritz Fischer {
508492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
509492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
510492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
5117e8d5755SMoritz Fischer 	dma_addr_t tail_p, cur_phys;
512492caffaSMoritz Fischer 	skb_frag_t *frag;
513492caffaSMoritz Fischer 	u32 num_frag;
514492caffaSMoritz Fischer 	u32 ii;
515492caffaSMoritz Fischer 
516492caffaSMoritz Fischer 	num_frag = skb_shinfo(skb)->nr_frags;
517492caffaSMoritz Fischer 	cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
518492caffaSMoritz Fischer 	tx_skb = &priv->tx_skb[priv->tx_bd_tail];
519492caffaSMoritz Fischer 
520492caffaSMoritz Fischer 	if (nixge_check_tx_bd_space(priv, num_frag)) {
521492caffaSMoritz Fischer 		if (!netif_queue_stopped(ndev))
522492caffaSMoritz Fischer 			netif_stop_queue(ndev);
523492caffaSMoritz Fischer 		return NETDEV_TX_OK;
524492caffaSMoritz Fischer 	}
525492caffaSMoritz Fischer 
5267e8d5755SMoritz Fischer 	cur_phys = dma_map_single(ndev->dev.parent, skb->data,
527492caffaSMoritz Fischer 				  skb_headlen(skb), DMA_TO_DEVICE);
5287e8d5755SMoritz Fischer 	if (dma_mapping_error(ndev->dev.parent, cur_phys))
529492caffaSMoritz Fischer 		goto drop;
5307e8d5755SMoritz Fischer 	nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
531492caffaSMoritz Fischer 
532492caffaSMoritz Fischer 	cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;
533492caffaSMoritz Fischer 
534492caffaSMoritz Fischer 	tx_skb->skb = NULL;
5357e8d5755SMoritz Fischer 	tx_skb->mapping = cur_phys;
536492caffaSMoritz Fischer 	tx_skb->size = skb_headlen(skb);
537492caffaSMoritz Fischer 	tx_skb->mapped_as_page = false;
538492caffaSMoritz Fischer 
539492caffaSMoritz Fischer 	for (ii = 0; ii < num_frag; ii++) {
540492caffaSMoritz Fischer 		++priv->tx_bd_tail;
541492caffaSMoritz Fischer 		priv->tx_bd_tail %= TX_BD_NUM;
542492caffaSMoritz Fischer 		cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
543492caffaSMoritz Fischer 		tx_skb = &priv->tx_skb[priv->tx_bd_tail];
544492caffaSMoritz Fischer 		frag = &skb_shinfo(skb)->frags[ii];
545492caffaSMoritz Fischer 
5467e8d5755SMoritz Fischer 		cur_phys = skb_frag_dma_map(ndev->dev.parent, frag, 0,
547492caffaSMoritz Fischer 					    skb_frag_size(frag),
548492caffaSMoritz Fischer 					    DMA_TO_DEVICE);
5497e8d5755SMoritz Fischer 		if (dma_mapping_error(ndev->dev.parent, cur_phys))
550492caffaSMoritz Fischer 			goto frag_err;
5517e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
552492caffaSMoritz Fischer 
553492caffaSMoritz Fischer 		cur_p->cntrl = skb_frag_size(frag);
554492caffaSMoritz Fischer 
555492caffaSMoritz Fischer 		tx_skb->skb = NULL;
5567e8d5755SMoritz Fischer 		tx_skb->mapping = cur_phys;
557492caffaSMoritz Fischer 		tx_skb->size = skb_frag_size(frag);
558492caffaSMoritz Fischer 		tx_skb->mapped_as_page = true;
559492caffaSMoritz Fischer 	}
560492caffaSMoritz Fischer 
561492caffaSMoritz Fischer 	/* last buffer of the frame */
562492caffaSMoritz Fischer 	tx_skb->skb = skb;
563492caffaSMoritz Fischer 
564492caffaSMoritz Fischer 	cur_p->cntrl |= XAXIDMA_BD_CTRL_TXEOF_MASK;
565492caffaSMoritz Fischer 
566492caffaSMoritz Fischer 	tail_p = priv->tx_bd_p + sizeof(*priv->tx_bd_v) * priv->tx_bd_tail;
567492caffaSMoritz Fischer 	/* Start the transfer */
5687e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(priv, XAXIDMA_TX_TDESC_OFFSET, tail_p);
569492caffaSMoritz Fischer 	++priv->tx_bd_tail;
570492caffaSMoritz Fischer 	priv->tx_bd_tail %= TX_BD_NUM;
571492caffaSMoritz Fischer 
572492caffaSMoritz Fischer 	return NETDEV_TX_OK;
573492caffaSMoritz Fischer frag_err:
574492caffaSMoritz Fischer 	for (; ii > 0; ii--) {
575492caffaSMoritz Fischer 		if (priv->tx_bd_tail)
576492caffaSMoritz Fischer 			priv->tx_bd_tail--;
577492caffaSMoritz Fischer 		else
578492caffaSMoritz Fischer 			priv->tx_bd_tail = TX_BD_NUM - 1;
579492caffaSMoritz Fischer 
580492caffaSMoritz Fischer 		tx_skb = &priv->tx_skb[priv->tx_bd_tail];
581492caffaSMoritz Fischer 		nixge_tx_skb_unmap(priv, tx_skb);
582492caffaSMoritz Fischer 
583492caffaSMoritz Fischer 		cur_p = &priv->tx_bd_v[priv->tx_bd_tail];
584492caffaSMoritz Fischer 		cur_p->status = 0;
585492caffaSMoritz Fischer 	}
586492caffaSMoritz Fischer 	dma_unmap_single(priv->ndev->dev.parent,
587492caffaSMoritz Fischer 			 tx_skb->mapping,
588492caffaSMoritz Fischer 			 tx_skb->size, DMA_TO_DEVICE);
589492caffaSMoritz Fischer drop:
590492caffaSMoritz Fischer 	ndev->stats.tx_dropped++;
591492caffaSMoritz Fischer 	return NETDEV_TX_OK;
592492caffaSMoritz Fischer }
593492caffaSMoritz Fischer 
594492caffaSMoritz Fischer static int nixge_recv(struct net_device *ndev, int budget)
595492caffaSMoritz Fischer {
596492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
597492caffaSMoritz Fischer 	struct sk_buff *skb, *new_skb;
598492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
5997e8d5755SMoritz Fischer 	dma_addr_t tail_p = 0, cur_phys = 0;
600492caffaSMoritz Fischer 	u32 packets = 0;
601492caffaSMoritz Fischer 	u32 length = 0;
602492caffaSMoritz Fischer 	u32 size = 0;
603492caffaSMoritz Fischer 
604492caffaSMoritz Fischer 	cur_p = &priv->rx_bd_v[priv->rx_bd_ci];
605492caffaSMoritz Fischer 
606492caffaSMoritz Fischer 	while ((cur_p->status & XAXIDMA_BD_STS_COMPLETE_MASK &&
607492caffaSMoritz Fischer 		budget > packets)) {
608492caffaSMoritz Fischer 		tail_p = priv->rx_bd_p + sizeof(*priv->rx_bd_v) *
609492caffaSMoritz Fischer 			 priv->rx_bd_ci;
610492caffaSMoritz Fischer 
611ea43a590SMoritz Fischer 		skb = (struct sk_buff *)(uintptr_t)
612ea43a590SMoritz Fischer 			nixge_hw_dma_bd_get_addr(cur_p, sw_id_offset);
613492caffaSMoritz Fischer 
614492caffaSMoritz Fischer 		length = cur_p->status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
615492caffaSMoritz Fischer 		if (length > NIXGE_MAX_JUMBO_FRAME_SIZE)
616492caffaSMoritz Fischer 			length = NIXGE_MAX_JUMBO_FRAME_SIZE;
617492caffaSMoritz Fischer 
6187e8d5755SMoritz Fischer 		dma_unmap_single(ndev->dev.parent,
6197e8d5755SMoritz Fischer 				 nixge_hw_dma_bd_get_addr(cur_p, phys),
620492caffaSMoritz Fischer 				 NIXGE_MAX_JUMBO_FRAME_SIZE,
621492caffaSMoritz Fischer 				 DMA_FROM_DEVICE);
622492caffaSMoritz Fischer 
623492caffaSMoritz Fischer 		skb_put(skb, length);
624492caffaSMoritz Fischer 
625492caffaSMoritz Fischer 		skb->protocol = eth_type_trans(skb, ndev);
626492caffaSMoritz Fischer 		skb_checksum_none_assert(skb);
627492caffaSMoritz Fischer 
628492caffaSMoritz Fischer 		/* For now mark them as CHECKSUM_NONE since
629492caffaSMoritz Fischer 		 * we don't have offload capabilities
630492caffaSMoritz Fischer 		 */
631492caffaSMoritz Fischer 		skb->ip_summed = CHECKSUM_NONE;
632492caffaSMoritz Fischer 
633492caffaSMoritz Fischer 		napi_gro_receive(&priv->napi, skb);
634492caffaSMoritz Fischer 
635492caffaSMoritz Fischer 		size += length;
636492caffaSMoritz Fischer 		packets++;
637492caffaSMoritz Fischer 
638492caffaSMoritz Fischer 		new_skb = netdev_alloc_skb_ip_align(ndev,
639492caffaSMoritz Fischer 						    NIXGE_MAX_JUMBO_FRAME_SIZE);
640492caffaSMoritz Fischer 		if (!new_skb)
641492caffaSMoritz Fischer 			return packets;
642492caffaSMoritz Fischer 
6437e8d5755SMoritz Fischer 		cur_phys = dma_map_single(ndev->dev.parent, new_skb->data,
644492caffaSMoritz Fischer 					  NIXGE_MAX_JUMBO_FRAME_SIZE,
645492caffaSMoritz Fischer 					  DMA_FROM_DEVICE);
6467e8d5755SMoritz Fischer 		if (dma_mapping_error(ndev->dev.parent, cur_phys)) {
647492caffaSMoritz Fischer 			/* FIXME: bail out and clean up */
648492caffaSMoritz Fischer 			netdev_err(ndev, "Failed to map ...\n");
649492caffaSMoritz Fischer 		}
6507e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(cur_p, cur_phys);
651492caffaSMoritz Fischer 		cur_p->cntrl = NIXGE_MAX_JUMBO_FRAME_SIZE;
652492caffaSMoritz Fischer 		cur_p->status = 0;
653ea43a590SMoritz Fischer 		nixge_hw_dma_bd_set_offset(cur_p, (uintptr_t)new_skb);
654492caffaSMoritz Fischer 
655492caffaSMoritz Fischer 		++priv->rx_bd_ci;
656492caffaSMoritz Fischer 		priv->rx_bd_ci %= RX_BD_NUM;
657492caffaSMoritz Fischer 		cur_p = &priv->rx_bd_v[priv->rx_bd_ci];
658492caffaSMoritz Fischer 	}
659492caffaSMoritz Fischer 
660492caffaSMoritz Fischer 	ndev->stats.rx_packets += packets;
661492caffaSMoritz Fischer 	ndev->stats.rx_bytes += size;
662492caffaSMoritz Fischer 
663492caffaSMoritz Fischer 	if (tail_p)
6647e8d5755SMoritz Fischer 		nixge_dma_write_desc_reg(priv, XAXIDMA_RX_TDESC_OFFSET, tail_p);
665492caffaSMoritz Fischer 
666492caffaSMoritz Fischer 	return packets;
667492caffaSMoritz Fischer }
668492caffaSMoritz Fischer 
669492caffaSMoritz Fischer static int nixge_poll(struct napi_struct *napi, int budget)
670492caffaSMoritz Fischer {
671492caffaSMoritz Fischer 	struct nixge_priv *priv = container_of(napi, struct nixge_priv, napi);
672492caffaSMoritz Fischer 	int work_done;
673492caffaSMoritz Fischer 	u32 status, cr;
674492caffaSMoritz Fischer 
675492caffaSMoritz Fischer 	work_done = 0;
676492caffaSMoritz Fischer 
677492caffaSMoritz Fischer 	work_done = nixge_recv(priv->ndev, budget);
678492caffaSMoritz Fischer 	if (work_done < budget) {
679492caffaSMoritz Fischer 		napi_complete_done(napi, work_done);
680492caffaSMoritz Fischer 		status = nixge_dma_read_reg(priv, XAXIDMA_RX_SR_OFFSET);
681492caffaSMoritz Fischer 
682492caffaSMoritz Fischer 		if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
683492caffaSMoritz Fischer 			/* If there's more, reschedule, but clear */
684492caffaSMoritz Fischer 			nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
685492caffaSMoritz Fischer 			napi_reschedule(napi);
686492caffaSMoritz Fischer 		} else {
687492caffaSMoritz Fischer 			/* if not, turn on RX IRQs again ... */
688492caffaSMoritz Fischer 			cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
689492caffaSMoritz Fischer 			cr |= (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
690492caffaSMoritz Fischer 			nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
691492caffaSMoritz Fischer 		}
692492caffaSMoritz Fischer 	}
693492caffaSMoritz Fischer 
694492caffaSMoritz Fischer 	return work_done;
695492caffaSMoritz Fischer }
696492caffaSMoritz Fischer 
697492caffaSMoritz Fischer static irqreturn_t nixge_tx_irq(int irq, void *_ndev)
698492caffaSMoritz Fischer {
699492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(_ndev);
700492caffaSMoritz Fischer 	struct net_device *ndev = _ndev;
701492caffaSMoritz Fischer 	unsigned int status;
7027e8d5755SMoritz Fischer 	dma_addr_t phys;
703492caffaSMoritz Fischer 	u32 cr;
704492caffaSMoritz Fischer 
705492caffaSMoritz Fischer 	status = nixge_dma_read_reg(priv, XAXIDMA_TX_SR_OFFSET);
706492caffaSMoritz Fischer 	if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
707492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_SR_OFFSET, status);
708492caffaSMoritz Fischer 		nixge_start_xmit_done(priv->ndev);
709492caffaSMoritz Fischer 		goto out;
710492caffaSMoritz Fischer 	}
711492caffaSMoritz Fischer 	if (!(status & XAXIDMA_IRQ_ALL_MASK)) {
712492caffaSMoritz Fischer 		netdev_err(ndev, "No interrupts asserted in Tx path\n");
713492caffaSMoritz Fischer 		return IRQ_NONE;
714492caffaSMoritz Fischer 	}
715492caffaSMoritz Fischer 	if (status & XAXIDMA_IRQ_ERROR_MASK) {
7167e8d5755SMoritz Fischer 		phys = nixge_hw_dma_bd_get_addr(&priv->tx_bd_v[priv->tx_bd_ci],
7177e8d5755SMoritz Fischer 						phys);
7187e8d5755SMoritz Fischer 
719492caffaSMoritz Fischer 		netdev_err(ndev, "DMA Tx error 0x%x\n", status);
7207e8d5755SMoritz Fischer 		netdev_err(ndev, "Current BD is at: 0x%llx\n", (u64)phys);
721492caffaSMoritz Fischer 
722492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
723492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
724492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
725492caffaSMoritz Fischer 		/* Write to the Tx channel control register */
726492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
727492caffaSMoritz Fischer 
728492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
729492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
730492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
731492caffaSMoritz Fischer 		/* Write to the Rx channel control register */
732492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
733492caffaSMoritz Fischer 
734492caffaSMoritz Fischer 		tasklet_schedule(&priv->dma_err_tasklet);
735492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_SR_OFFSET, status);
736492caffaSMoritz Fischer 	}
737492caffaSMoritz Fischer out:
738492caffaSMoritz Fischer 	return IRQ_HANDLED;
739492caffaSMoritz Fischer }
740492caffaSMoritz Fischer 
741492caffaSMoritz Fischer static irqreturn_t nixge_rx_irq(int irq, void *_ndev)
742492caffaSMoritz Fischer {
743492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(_ndev);
744492caffaSMoritz Fischer 	struct net_device *ndev = _ndev;
745492caffaSMoritz Fischer 	unsigned int status;
7467e8d5755SMoritz Fischer 	dma_addr_t phys;
747492caffaSMoritz Fischer 	u32 cr;
748492caffaSMoritz Fischer 
749492caffaSMoritz Fischer 	status = nixge_dma_read_reg(priv, XAXIDMA_RX_SR_OFFSET);
750492caffaSMoritz Fischer 	if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
751492caffaSMoritz Fischer 		/* Turn of IRQs because NAPI */
752492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
753492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
754492caffaSMoritz Fischer 		cr &= ~(XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
755492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
756492caffaSMoritz Fischer 
757492caffaSMoritz Fischer 		if (napi_schedule_prep(&priv->napi))
758492caffaSMoritz Fischer 			__napi_schedule(&priv->napi);
759492caffaSMoritz Fischer 		goto out;
760492caffaSMoritz Fischer 	}
761492caffaSMoritz Fischer 	if (!(status & XAXIDMA_IRQ_ALL_MASK)) {
762492caffaSMoritz Fischer 		netdev_err(ndev, "No interrupts asserted in Rx path\n");
763492caffaSMoritz Fischer 		return IRQ_NONE;
764492caffaSMoritz Fischer 	}
765492caffaSMoritz Fischer 	if (status & XAXIDMA_IRQ_ERROR_MASK) {
7667e8d5755SMoritz Fischer 		phys = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[priv->rx_bd_ci],
7677e8d5755SMoritz Fischer 						phys);
768492caffaSMoritz Fischer 		netdev_err(ndev, "DMA Rx error 0x%x\n", status);
7697e8d5755SMoritz Fischer 		netdev_err(ndev, "Current BD is at: 0x%llx\n", (u64)phys);
770492caffaSMoritz Fischer 
771492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
772492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
773492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
774492caffaSMoritz Fischer 		/* Finally write to the Tx channel control register */
775492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET, cr);
776492caffaSMoritz Fischer 
777492caffaSMoritz Fischer 		cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
778492caffaSMoritz Fischer 		/* Disable coalesce, delay timer and error interrupts */
779492caffaSMoritz Fischer 		cr &= (~XAXIDMA_IRQ_ALL_MASK);
780492caffaSMoritz Fischer 		/* write to the Rx channel control register */
781492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET, cr);
782492caffaSMoritz Fischer 
783492caffaSMoritz Fischer 		tasklet_schedule(&priv->dma_err_tasklet);
784492caffaSMoritz Fischer 		nixge_dma_write_reg(priv, XAXIDMA_RX_SR_OFFSET, status);
785492caffaSMoritz Fischer 	}
786492caffaSMoritz Fischer out:
787492caffaSMoritz Fischer 	return IRQ_HANDLED;
788492caffaSMoritz Fischer }
789492caffaSMoritz Fischer 
790f246d129SAllen Pais static void nixge_dma_err_handler(struct tasklet_struct *t)
791492caffaSMoritz Fischer {
792f246d129SAllen Pais 	struct nixge_priv *lp = from_tasklet(lp, t, dma_err_tasklet);
793492caffaSMoritz Fischer 	struct nixge_hw_dma_bd *cur_p;
794492caffaSMoritz Fischer 	struct nixge_tx_skb *tx_skb;
795492caffaSMoritz Fischer 	u32 cr, i;
796492caffaSMoritz Fischer 
797492caffaSMoritz Fischer 	__nixge_device_reset(lp, XAXIDMA_TX_CR_OFFSET);
798492caffaSMoritz Fischer 	__nixge_device_reset(lp, XAXIDMA_RX_CR_OFFSET);
799492caffaSMoritz Fischer 
800492caffaSMoritz Fischer 	for (i = 0; i < TX_BD_NUM; i++) {
801492caffaSMoritz Fischer 		cur_p = &lp->tx_bd_v[i];
802492caffaSMoritz Fischer 		tx_skb = &lp->tx_skb[i];
803492caffaSMoritz Fischer 		nixge_tx_skb_unmap(lp, tx_skb);
804492caffaSMoritz Fischer 
8057e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_phys(cur_p, 0);
806492caffaSMoritz Fischer 		cur_p->cntrl = 0;
807492caffaSMoritz Fischer 		cur_p->status = 0;
8087e8d5755SMoritz Fischer 		nixge_hw_dma_bd_set_offset(cur_p, 0);
809492caffaSMoritz Fischer 	}
810492caffaSMoritz Fischer 
811492caffaSMoritz Fischer 	for (i = 0; i < RX_BD_NUM; i++) {
812492caffaSMoritz Fischer 		cur_p = &lp->rx_bd_v[i];
813492caffaSMoritz Fischer 		cur_p->status = 0;
814492caffaSMoritz Fischer 	}
815492caffaSMoritz Fischer 
816492caffaSMoritz Fischer 	lp->tx_bd_ci = 0;
817492caffaSMoritz Fischer 	lp->tx_bd_tail = 0;
818492caffaSMoritz Fischer 	lp->rx_bd_ci = 0;
819492caffaSMoritz Fischer 
820492caffaSMoritz Fischer 	/* Start updating the Rx channel control register */
821492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_RX_CR_OFFSET);
822492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
823492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
824492caffaSMoritz Fischer 	      (XAXIDMA_DFT_RX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
825492caffaSMoritz Fischer 	/* Update the delay timer count */
826492caffaSMoritz Fischer 	cr = ((cr & ~XAXIDMA_DELAY_MASK) |
827492caffaSMoritz Fischer 	      (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
828492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
829492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
830492caffaSMoritz Fischer 	/* Finally write to the Rx channel control register */
831492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_RX_CR_OFFSET, cr);
832492caffaSMoritz Fischer 
833492caffaSMoritz Fischer 	/* Start updating the Tx channel control register */
834492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_TX_CR_OFFSET);
835492caffaSMoritz Fischer 	/* Update the interrupt coalesce count */
836492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
837492caffaSMoritz Fischer 	      (XAXIDMA_DFT_TX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
838492caffaSMoritz Fischer 	/* Update the delay timer count */
839492caffaSMoritz Fischer 	cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
840492caffaSMoritz Fischer 	      (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
841492caffaSMoritz Fischer 	/* Enable coalesce, delay timer and error interrupts */
842492caffaSMoritz Fischer 	cr |= XAXIDMA_IRQ_ALL_MASK;
843492caffaSMoritz Fischer 	/* Finally write to the Tx channel control register */
844492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_TX_CR_OFFSET, cr);
845492caffaSMoritz Fischer 
846492caffaSMoritz Fischer 	/* Populate the tail pointer and bring the Rx Axi DMA engine out of
847492caffaSMoritz Fischer 	 * halted state. This will make the Rx side ready for reception.
848492caffaSMoritz Fischer 	 */
8497e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
850492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_RX_CR_OFFSET);
851492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_RX_CR_OFFSET,
852492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
8537e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
854492caffaSMoritz Fischer 			    (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
855492caffaSMoritz Fischer 
856492caffaSMoritz Fischer 	/* Write to the RS (Run-stop) bit in the Tx channel control register.
857492caffaSMoritz Fischer 	 * Tx channel is now ready to run. But only after we write to the
858492caffaSMoritz Fischer 	 * tail pointer register that the Tx channel will start transmitting
859492caffaSMoritz Fischer 	 */
8607e8d5755SMoritz Fischer 	nixge_dma_write_desc_reg(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
861492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(lp, XAXIDMA_TX_CR_OFFSET);
862492caffaSMoritz Fischer 	nixge_dma_write_reg(lp, XAXIDMA_TX_CR_OFFSET,
863492caffaSMoritz Fischer 			    cr | XAXIDMA_CR_RUNSTOP_MASK);
864492caffaSMoritz Fischer }
865492caffaSMoritz Fischer 
866492caffaSMoritz Fischer static int nixge_open(struct net_device *ndev)
867492caffaSMoritz Fischer {
868492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
869492caffaSMoritz Fischer 	struct phy_device *phy;
870492caffaSMoritz Fischer 	int ret;
871492caffaSMoritz Fischer 
872492caffaSMoritz Fischer 	nixge_device_reset(ndev);
873492caffaSMoritz Fischer 
874492caffaSMoritz Fischer 	phy = of_phy_connect(ndev, priv->phy_node,
875492caffaSMoritz Fischer 			     &nixge_handle_link_change, 0, priv->phy_mode);
876492caffaSMoritz Fischer 	if (!phy)
877492caffaSMoritz Fischer 		return -ENODEV;
878492caffaSMoritz Fischer 
879492caffaSMoritz Fischer 	phy_start(phy);
880492caffaSMoritz Fischer 
881492caffaSMoritz Fischer 	/* Enable tasklets for Axi DMA error handling */
882f246d129SAllen Pais 	tasklet_setup(&priv->dma_err_tasklet, nixge_dma_err_handler);
883492caffaSMoritz Fischer 
884492caffaSMoritz Fischer 	napi_enable(&priv->napi);
885492caffaSMoritz Fischer 
886492caffaSMoritz Fischer 	/* Enable interrupts for Axi DMA Tx */
887492caffaSMoritz Fischer 	ret = request_irq(priv->tx_irq, nixge_tx_irq, 0, ndev->name, ndev);
888492caffaSMoritz Fischer 	if (ret)
889492caffaSMoritz Fischer 		goto err_tx_irq;
890492caffaSMoritz Fischer 	/* Enable interrupts for Axi DMA Rx */
891492caffaSMoritz Fischer 	ret = request_irq(priv->rx_irq, nixge_rx_irq, 0, ndev->name, ndev);
892492caffaSMoritz Fischer 	if (ret)
893492caffaSMoritz Fischer 		goto err_rx_irq;
894492caffaSMoritz Fischer 
895492caffaSMoritz Fischer 	netif_start_queue(ndev);
896492caffaSMoritz Fischer 
897492caffaSMoritz Fischer 	return 0;
898492caffaSMoritz Fischer 
899492caffaSMoritz Fischer err_rx_irq:
900492caffaSMoritz Fischer 	free_irq(priv->tx_irq, ndev);
901492caffaSMoritz Fischer err_tx_irq:
902492caffaSMoritz Fischer 	phy_stop(phy);
903492caffaSMoritz Fischer 	phy_disconnect(phy);
904492caffaSMoritz Fischer 	tasklet_kill(&priv->dma_err_tasklet);
905492caffaSMoritz Fischer 	netdev_err(ndev, "request_irq() failed\n");
906492caffaSMoritz Fischer 	return ret;
907492caffaSMoritz Fischer }
908492caffaSMoritz Fischer 
909492caffaSMoritz Fischer static int nixge_stop(struct net_device *ndev)
910492caffaSMoritz Fischer {
911492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
912492caffaSMoritz Fischer 	u32 cr;
913492caffaSMoritz Fischer 
914492caffaSMoritz Fischer 	netif_stop_queue(ndev);
915492caffaSMoritz Fischer 	napi_disable(&priv->napi);
916492caffaSMoritz Fischer 
917492caffaSMoritz Fischer 	if (ndev->phydev) {
918492caffaSMoritz Fischer 		phy_stop(ndev->phydev);
919492caffaSMoritz Fischer 		phy_disconnect(ndev->phydev);
920492caffaSMoritz Fischer 	}
921492caffaSMoritz Fischer 
922492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
923492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_RX_CR_OFFSET,
924492caffaSMoritz Fischer 			    cr & (~XAXIDMA_CR_RUNSTOP_MASK));
925492caffaSMoritz Fischer 	cr = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
926492caffaSMoritz Fischer 	nixge_dma_write_reg(priv, XAXIDMA_TX_CR_OFFSET,
927492caffaSMoritz Fischer 			    cr & (~XAXIDMA_CR_RUNSTOP_MASK));
928492caffaSMoritz Fischer 
929492caffaSMoritz Fischer 	tasklet_kill(&priv->dma_err_tasklet);
930492caffaSMoritz Fischer 
931492caffaSMoritz Fischer 	free_irq(priv->tx_irq, ndev);
932492caffaSMoritz Fischer 	free_irq(priv->rx_irq, ndev);
933492caffaSMoritz Fischer 
934492caffaSMoritz Fischer 	nixge_hw_dma_bd_release(ndev);
935492caffaSMoritz Fischer 
936492caffaSMoritz Fischer 	return 0;
937492caffaSMoritz Fischer }
938492caffaSMoritz Fischer 
939492caffaSMoritz Fischer static int nixge_change_mtu(struct net_device *ndev, int new_mtu)
940492caffaSMoritz Fischer {
941492caffaSMoritz Fischer 	if (netif_running(ndev))
942492caffaSMoritz Fischer 		return -EBUSY;
943492caffaSMoritz Fischer 
944492caffaSMoritz Fischer 	if ((new_mtu + NIXGE_HDR_SIZE + NIXGE_TRL_SIZE) >
945492caffaSMoritz Fischer 	     NIXGE_MAX_JUMBO_FRAME_SIZE)
946492caffaSMoritz Fischer 		return -EINVAL;
947492caffaSMoritz Fischer 
948492caffaSMoritz Fischer 	ndev->mtu = new_mtu;
949492caffaSMoritz Fischer 
950492caffaSMoritz Fischer 	return 0;
951492caffaSMoritz Fischer }
952492caffaSMoritz Fischer 
953492caffaSMoritz Fischer static s32 __nixge_hw_set_mac_address(struct net_device *ndev)
954492caffaSMoritz Fischer {
955492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
956492caffaSMoritz Fischer 
957492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MAC_LSB,
958492caffaSMoritz Fischer 			     (ndev->dev_addr[2]) << 24 |
959492caffaSMoritz Fischer 			     (ndev->dev_addr[3] << 16) |
960492caffaSMoritz Fischer 			     (ndev->dev_addr[4] << 8) |
961492caffaSMoritz Fischer 			     (ndev->dev_addr[5] << 0));
962492caffaSMoritz Fischer 
963492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MAC_MSB,
964492caffaSMoritz Fischer 			     (ndev->dev_addr[1] | (ndev->dev_addr[0] << 8)));
965492caffaSMoritz Fischer 
966492caffaSMoritz Fischer 	return 0;
967492caffaSMoritz Fischer }
968492caffaSMoritz Fischer 
969492caffaSMoritz Fischer static int nixge_net_set_mac_address(struct net_device *ndev, void *p)
970492caffaSMoritz Fischer {
971492caffaSMoritz Fischer 	int err;
972492caffaSMoritz Fischer 
973492caffaSMoritz Fischer 	err = eth_mac_addr(ndev, p);
974492caffaSMoritz Fischer 	if (!err)
975492caffaSMoritz Fischer 		__nixge_hw_set_mac_address(ndev);
976492caffaSMoritz Fischer 
977492caffaSMoritz Fischer 	return err;
978492caffaSMoritz Fischer }
979492caffaSMoritz Fischer 
980492caffaSMoritz Fischer static const struct net_device_ops nixge_netdev_ops = {
981492caffaSMoritz Fischer 	.ndo_open = nixge_open,
982492caffaSMoritz Fischer 	.ndo_stop = nixge_stop,
983492caffaSMoritz Fischer 	.ndo_start_xmit = nixge_start_xmit,
984492caffaSMoritz Fischer 	.ndo_change_mtu	= nixge_change_mtu,
985492caffaSMoritz Fischer 	.ndo_set_mac_address = nixge_net_set_mac_address,
986492caffaSMoritz Fischer 	.ndo_validate_addr = eth_validate_addr,
987492caffaSMoritz Fischer };
988492caffaSMoritz Fischer 
989492caffaSMoritz Fischer static void nixge_ethtools_get_drvinfo(struct net_device *ndev,
990492caffaSMoritz Fischer 				       struct ethtool_drvinfo *ed)
991492caffaSMoritz Fischer {
992492caffaSMoritz Fischer 	strlcpy(ed->driver, "nixge", sizeof(ed->driver));
9936b4ddf99SJoe Perches 	strlcpy(ed->bus_info, "platform", sizeof(ed->bus_info));
994492caffaSMoritz Fischer }
995492caffaSMoritz Fischer 
996f3ccfda1SYufeng Mo static int
997f3ccfda1SYufeng Mo nixge_ethtools_get_coalesce(struct net_device *ndev,
998f3ccfda1SYufeng Mo 			    struct ethtool_coalesce *ecoalesce,
999f3ccfda1SYufeng Mo 			    struct kernel_ethtool_coalesce *kernel_coal,
1000f3ccfda1SYufeng Mo 			    struct netlink_ext_ack *extack)
1001492caffaSMoritz Fischer {
1002492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1003492caffaSMoritz Fischer 	u32 regval = 0;
1004492caffaSMoritz Fischer 
1005492caffaSMoritz Fischer 	regval = nixge_dma_read_reg(priv, XAXIDMA_RX_CR_OFFSET);
1006492caffaSMoritz Fischer 	ecoalesce->rx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
1007492caffaSMoritz Fischer 					     >> XAXIDMA_COALESCE_SHIFT;
1008492caffaSMoritz Fischer 	regval = nixge_dma_read_reg(priv, XAXIDMA_TX_CR_OFFSET);
1009492caffaSMoritz Fischer 	ecoalesce->tx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
1010492caffaSMoritz Fischer 					     >> XAXIDMA_COALESCE_SHIFT;
1011492caffaSMoritz Fischer 	return 0;
1012492caffaSMoritz Fischer }
1013492caffaSMoritz Fischer 
1014f3ccfda1SYufeng Mo static int
1015f3ccfda1SYufeng Mo nixge_ethtools_set_coalesce(struct net_device *ndev,
1016f3ccfda1SYufeng Mo 			    struct ethtool_coalesce *ecoalesce,
1017f3ccfda1SYufeng Mo 			    struct kernel_ethtool_coalesce *kernel_coal,
1018f3ccfda1SYufeng Mo 			    struct netlink_ext_ack *extack)
1019492caffaSMoritz Fischer {
1020492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1021492caffaSMoritz Fischer 
1022492caffaSMoritz Fischer 	if (netif_running(ndev)) {
1023492caffaSMoritz Fischer 		netdev_err(ndev,
1024492caffaSMoritz Fischer 			   "Please stop netif before applying configuration\n");
1025492caffaSMoritz Fischer 		return -EBUSY;
1026492caffaSMoritz Fischer 	}
1027492caffaSMoritz Fischer 
1028492caffaSMoritz Fischer 	if (ecoalesce->rx_max_coalesced_frames)
1029492caffaSMoritz Fischer 		priv->coalesce_count_rx = ecoalesce->rx_max_coalesced_frames;
1030492caffaSMoritz Fischer 	if (ecoalesce->tx_max_coalesced_frames)
1031492caffaSMoritz Fischer 		priv->coalesce_count_tx = ecoalesce->tx_max_coalesced_frames;
1032492caffaSMoritz Fischer 
1033492caffaSMoritz Fischer 	return 0;
1034492caffaSMoritz Fischer }
1035492caffaSMoritz Fischer 
1036492caffaSMoritz Fischer static int nixge_ethtools_set_phys_id(struct net_device *ndev,
1037492caffaSMoritz Fischer 				      enum ethtool_phys_id_state state)
1038492caffaSMoritz Fischer {
1039492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1040492caffaSMoritz Fischer 	u32 ctrl;
1041492caffaSMoritz Fischer 
1042492caffaSMoritz Fischer 	ctrl = nixge_ctrl_read_reg(priv, NIXGE_REG_LED_CTL);
1043492caffaSMoritz Fischer 	switch (state) {
1044492caffaSMoritz Fischer 	case ETHTOOL_ID_ACTIVE:
1045492caffaSMoritz Fischer 		ctrl |= NIXGE_ID_LED_CTL_EN;
1046492caffaSMoritz Fischer 		/* Enable identification LED override*/
1047492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1048492caffaSMoritz Fischer 		return 2;
1049492caffaSMoritz Fischer 
1050492caffaSMoritz Fischer 	case ETHTOOL_ID_ON:
1051492caffaSMoritz Fischer 		ctrl |= NIXGE_ID_LED_CTL_VAL;
1052492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1053492caffaSMoritz Fischer 		break;
1054492caffaSMoritz Fischer 
1055492caffaSMoritz Fischer 	case ETHTOOL_ID_OFF:
1056492caffaSMoritz Fischer 		ctrl &= ~NIXGE_ID_LED_CTL_VAL;
1057492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1058492caffaSMoritz Fischer 		break;
1059492caffaSMoritz Fischer 
1060492caffaSMoritz Fischer 	case ETHTOOL_ID_INACTIVE:
1061492caffaSMoritz Fischer 		/* Restore LED settings */
1062492caffaSMoritz Fischer 		ctrl &= ~NIXGE_ID_LED_CTL_EN;
1063492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_LED_CTL, ctrl);
1064492caffaSMoritz Fischer 		break;
1065492caffaSMoritz Fischer 	}
1066492caffaSMoritz Fischer 
1067492caffaSMoritz Fischer 	return 0;
1068492caffaSMoritz Fischer }
1069492caffaSMoritz Fischer 
1070492caffaSMoritz Fischer static const struct ethtool_ops nixge_ethtool_ops = {
10718078f028SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES,
1072492caffaSMoritz Fischer 	.get_drvinfo    = nixge_ethtools_get_drvinfo,
1073492caffaSMoritz Fischer 	.get_coalesce   = nixge_ethtools_get_coalesce,
1074492caffaSMoritz Fischer 	.set_coalesce   = nixge_ethtools_set_coalesce,
1075492caffaSMoritz Fischer 	.set_phys_id    = nixge_ethtools_set_phys_id,
1076492caffaSMoritz Fischer 	.get_link_ksettings     = phy_ethtool_get_link_ksettings,
1077492caffaSMoritz Fischer 	.set_link_ksettings     = phy_ethtool_set_link_ksettings,
1078492caffaSMoritz Fischer 	.get_link		= ethtool_op_get_link,
1079492caffaSMoritz Fischer };
1080492caffaSMoritz Fischer 
1081492caffaSMoritz Fischer static int nixge_mdio_read(struct mii_bus *bus, int phy_id, int reg)
1082492caffaSMoritz Fischer {
1083492caffaSMoritz Fischer 	struct nixge_priv *priv = bus->priv;
1084492caffaSMoritz Fischer 	u32 status, tmp;
1085492caffaSMoritz Fischer 	int err;
1086492caffaSMoritz Fischer 	u16 device;
1087492caffaSMoritz Fischer 
1088492caffaSMoritz Fischer 	if (reg & MII_ADDR_C45) {
1089492caffaSMoritz Fischer 		device = (reg >> 16) & 0x1f;
1090492caffaSMoritz Fischer 
1091492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_ADDR, reg & 0xffff);
1092492caffaSMoritz Fischer 
1093492caffaSMoritz Fischer 		tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_OP_ADDRESS)
1094492caffaSMoritz Fischer 			| NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1095492caffaSMoritz Fischer 
1096492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1097492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1098492caffaSMoritz Fischer 
1099492caffaSMoritz Fischer 		err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1100492caffaSMoritz Fischer 					      !status, 10, 1000);
1101492caffaSMoritz Fischer 		if (err) {
1102492caffaSMoritz Fischer 			dev_err(priv->dev, "timeout setting address");
1103492caffaSMoritz Fischer 			return err;
1104492caffaSMoritz Fischer 		}
1105492caffaSMoritz Fischer 
1106492caffaSMoritz Fischer 		tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_C45_READ) |
1107492caffaSMoritz Fischer 			NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1108492caffaSMoritz Fischer 	} else {
1109492caffaSMoritz Fischer 		device = reg & 0x1f;
1110492caffaSMoritz Fischer 
1111492caffaSMoritz Fischer 		tmp = NIXGE_MDIO_CLAUSE22 | NIXGE_MDIO_OP(NIXGE_MDIO_C22_READ) |
1112492caffaSMoritz Fischer 			NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1113492caffaSMoritz Fischer 	}
1114492caffaSMoritz Fischer 
1115492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1116492caffaSMoritz Fischer 	nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1117492caffaSMoritz Fischer 
1118492caffaSMoritz Fischer 	err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1119492caffaSMoritz Fischer 				      !status, 10, 1000);
1120492caffaSMoritz Fischer 	if (err) {
1121492caffaSMoritz Fischer 		dev_err(priv->dev, "timeout setting read command");
1122492caffaSMoritz Fischer 		return err;
1123492caffaSMoritz Fischer 	}
1124492caffaSMoritz Fischer 
1125492caffaSMoritz Fischer 	status = nixge_ctrl_read_reg(priv, NIXGE_REG_MDIO_DATA);
1126492caffaSMoritz Fischer 
1127492caffaSMoritz Fischer 	return status;
1128492caffaSMoritz Fischer }
1129492caffaSMoritz Fischer 
1130492caffaSMoritz Fischer static int nixge_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
1131492caffaSMoritz Fischer {
1132492caffaSMoritz Fischer 	struct nixge_priv *priv = bus->priv;
1133492caffaSMoritz Fischer 	u32 status, tmp;
1134492caffaSMoritz Fischer 	u16 device;
1135492caffaSMoritz Fischer 	int err;
1136492caffaSMoritz Fischer 
1137492caffaSMoritz Fischer 	if (reg & MII_ADDR_C45) {
1138492caffaSMoritz Fischer 		device = (reg >> 16) & 0x1f;
1139492caffaSMoritz Fischer 
1140492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_ADDR, reg & 0xffff);
1141492caffaSMoritz Fischer 
1142492caffaSMoritz Fischer 		tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_OP_ADDRESS)
1143492caffaSMoritz Fischer 			| NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1144492caffaSMoritz Fischer 
1145492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1146492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1147492caffaSMoritz Fischer 
1148492caffaSMoritz Fischer 		err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1149492caffaSMoritz Fischer 					      !status, 10, 1000);
1150492caffaSMoritz Fischer 		if (err) {
1151492caffaSMoritz Fischer 			dev_err(priv->dev, "timeout setting address");
1152492caffaSMoritz Fischer 			return err;
1153492caffaSMoritz Fischer 		}
1154492caffaSMoritz Fischer 
1155492caffaSMoritz Fischer 		tmp = NIXGE_MDIO_CLAUSE45 | NIXGE_MDIO_OP(NIXGE_MDIO_C45_WRITE)
1156492caffaSMoritz Fischer 			| NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1157492caffaSMoritz Fischer 
1158492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_DATA, val);
1159492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1160492caffaSMoritz Fischer 		err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1161492caffaSMoritz Fischer 					      !status, 10, 1000);
1162492caffaSMoritz Fischer 		if (err)
1163492caffaSMoritz Fischer 			dev_err(priv->dev, "timeout setting write command");
1164492caffaSMoritz Fischer 	} else {
1165492caffaSMoritz Fischer 		device = reg & 0x1f;
1166492caffaSMoritz Fischer 
1167492caffaSMoritz Fischer 		tmp = NIXGE_MDIO_CLAUSE22 |
1168492caffaSMoritz Fischer 			NIXGE_MDIO_OP(NIXGE_MDIO_C22_WRITE) |
1169492caffaSMoritz Fischer 			NIXGE_MDIO_ADDR(phy_id) | NIXGE_MDIO_MMD(device);
1170492caffaSMoritz Fischer 
1171492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_DATA, val);
1172492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_OP, tmp);
1173492caffaSMoritz Fischer 		nixge_ctrl_write_reg(priv, NIXGE_REG_MDIO_CTRL, 1);
1174492caffaSMoritz Fischer 
1175492caffaSMoritz Fischer 		err = nixge_ctrl_poll_timeout(priv, NIXGE_REG_MDIO_CTRL, status,
1176492caffaSMoritz Fischer 					      !status, 10, 1000);
1177492caffaSMoritz Fischer 		if (err)
1178492caffaSMoritz Fischer 			dev_err(priv->dev, "timeout setting write command");
1179492caffaSMoritz Fischer 	}
1180492caffaSMoritz Fischer 
1181492caffaSMoritz Fischer 	return err;
1182492caffaSMoritz Fischer }
1183492caffaSMoritz Fischer 
1184492caffaSMoritz Fischer static int nixge_mdio_setup(struct nixge_priv *priv, struct device_node *np)
1185492caffaSMoritz Fischer {
1186492caffaSMoritz Fischer 	struct mii_bus *bus;
1187492caffaSMoritz Fischer 
1188492caffaSMoritz Fischer 	bus = devm_mdiobus_alloc(priv->dev);
1189492caffaSMoritz Fischer 	if (!bus)
1190492caffaSMoritz Fischer 		return -ENOMEM;
1191492caffaSMoritz Fischer 
1192492caffaSMoritz Fischer 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(priv->dev));
1193492caffaSMoritz Fischer 	bus->priv = priv;
1194492caffaSMoritz Fischer 	bus->name = "nixge_mii_bus";
1195492caffaSMoritz Fischer 	bus->read = nixge_mdio_read;
1196492caffaSMoritz Fischer 	bus->write = nixge_mdio_write;
1197492caffaSMoritz Fischer 	bus->parent = priv->dev;
1198492caffaSMoritz Fischer 
1199492caffaSMoritz Fischer 	priv->mii_bus = bus;
1200492caffaSMoritz Fischer 
1201492caffaSMoritz Fischer 	return of_mdiobus_register(bus, np);
1202492caffaSMoritz Fischer }
1203492caffaSMoritz Fischer 
1204492caffaSMoritz Fischer static void *nixge_get_nvmem_address(struct device *dev)
1205492caffaSMoritz Fischer {
1206492caffaSMoritz Fischer 	struct nvmem_cell *cell;
1207492caffaSMoritz Fischer 	size_t cell_size;
1208492caffaSMoritz Fischer 	char *mac;
1209492caffaSMoritz Fischer 
1210492caffaSMoritz Fischer 	cell = nvmem_cell_get(dev, "address");
1211492caffaSMoritz Fischer 	if (IS_ERR(cell))
1212*a68229caSArnd Bergmann 		return cell;
1213492caffaSMoritz Fischer 
1214492caffaSMoritz Fischer 	mac = nvmem_cell_read(cell, &cell_size);
1215492caffaSMoritz Fischer 	nvmem_cell_put(cell);
1216492caffaSMoritz Fischer 
1217492caffaSMoritz Fischer 	return mac;
1218492caffaSMoritz Fischer }
1219492caffaSMoritz Fischer 
122087ab2079SAlex Williams /* Match table for of_platform binding */
122187ab2079SAlex Williams static const struct of_device_id nixge_dt_ids[] = {
122287ab2079SAlex Williams 	{ .compatible = "ni,xge-enet-2.00", .data = (void *)NIXGE_V2 },
122387ab2079SAlex Williams 	{ .compatible = "ni,xge-enet-3.00", .data = (void *)NIXGE_V3 },
122487ab2079SAlex Williams 	{},
122587ab2079SAlex Williams };
122687ab2079SAlex Williams MODULE_DEVICE_TABLE(of, nixge_dt_ids);
122787ab2079SAlex Williams 
122887ab2079SAlex Williams static int nixge_of_get_resources(struct platform_device *pdev)
122987ab2079SAlex Williams {
123087ab2079SAlex Williams 	const struct of_device_id *of_id;
123187ab2079SAlex Williams 	enum nixge_version version;
123287ab2079SAlex Williams 	struct net_device *ndev;
123387ab2079SAlex Williams 	struct nixge_priv *priv;
123487ab2079SAlex Williams 
123587ab2079SAlex Williams 	ndev = platform_get_drvdata(pdev);
123687ab2079SAlex Williams 	priv = netdev_priv(ndev);
123787ab2079SAlex Williams 	of_id = of_match_node(nixge_dt_ids, pdev->dev.of_node);
123887ab2079SAlex Williams 	if (!of_id)
123987ab2079SAlex Williams 		return -ENODEV;
124087ab2079SAlex Williams 
124187ab2079SAlex Williams 	version = (enum nixge_version)of_id->data;
124287ab2079SAlex Williams 	if (version <= NIXGE_V2)
12435b38b97fSYang Yingliang 		priv->dma_regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
124487ab2079SAlex Williams 	else
12455b38b97fSYang Yingliang 		priv->dma_regs = devm_platform_ioremap_resource_byname(pdev, "dma");
124687ab2079SAlex Williams 	if (IS_ERR(priv->dma_regs)) {
124787ab2079SAlex Williams 		netdev_err(ndev, "failed to map dma regs\n");
124887ab2079SAlex Williams 		return PTR_ERR(priv->dma_regs);
124987ab2079SAlex Williams 	}
1250464a5728SCai Huoqing 	if (version <= NIXGE_V2)
125187ab2079SAlex Williams 		priv->ctrl_regs = priv->dma_regs + NIXGE_REG_CTRL_OFFSET;
1252464a5728SCai Huoqing 	else
1253464a5728SCai Huoqing 		priv->ctrl_regs = devm_platform_ioremap_resource_byname(pdev, "ctrl");
125487ab2079SAlex Williams 	if (IS_ERR(priv->ctrl_regs)) {
125587ab2079SAlex Williams 		netdev_err(ndev, "failed to map ctrl regs\n");
125687ab2079SAlex Williams 		return PTR_ERR(priv->ctrl_regs);
125787ab2079SAlex Williams 	}
125887ab2079SAlex Williams 	return 0;
125987ab2079SAlex Williams }
126087ab2079SAlex Williams 
1261492caffaSMoritz Fischer static int nixge_probe(struct platform_device *pdev)
1262492caffaSMoritz Fischer {
12638dc0ae90SMoritz Fischer 	struct device_node *mn, *phy_node;
1264492caffaSMoritz Fischer 	struct nixge_priv *priv;
1265492caffaSMoritz Fischer 	struct net_device *ndev;
1266a86b74d3SMoritz Fischer 	const u8 *mac_addr;
1267492caffaSMoritz Fischer 	int err;
1268492caffaSMoritz Fischer 
1269492caffaSMoritz Fischer 	ndev = alloc_etherdev(sizeof(*priv));
1270492caffaSMoritz Fischer 	if (!ndev)
1271492caffaSMoritz Fischer 		return -ENOMEM;
1272492caffaSMoritz Fischer 
1273492caffaSMoritz Fischer 	platform_set_drvdata(pdev, ndev);
1274492caffaSMoritz Fischer 	SET_NETDEV_DEV(ndev, &pdev->dev);
1275492caffaSMoritz Fischer 
1276492caffaSMoritz Fischer 	ndev->features = NETIF_F_SG;
1277492caffaSMoritz Fischer 	ndev->netdev_ops = &nixge_netdev_ops;
1278492caffaSMoritz Fischer 	ndev->ethtool_ops = &nixge_ethtool_ops;
1279492caffaSMoritz Fischer 
1280492caffaSMoritz Fischer 	/* MTU range: 64 - 9000 */
1281492caffaSMoritz Fischer 	ndev->min_mtu = 64;
1282492caffaSMoritz Fischer 	ndev->max_mtu = NIXGE_JUMBO_MTU;
1283492caffaSMoritz Fischer 
1284492caffaSMoritz Fischer 	mac_addr = nixge_get_nvmem_address(&pdev->dev);
1285*a68229caSArnd Bergmann 	if (!IS_ERR(mac_addr) && is_valid_ether_addr(mac_addr)) {
1286f3956ebbSJakub Kicinski 		eth_hw_addr_set(ndev, mac_addr);
1287abcd3d6fSMoritz Fischer 		kfree(mac_addr);
1288abcd3d6fSMoritz Fischer 	} else {
1289492caffaSMoritz Fischer 		eth_hw_addr_random(ndev);
1290abcd3d6fSMoritz Fischer 	}
1291492caffaSMoritz Fischer 
1292492caffaSMoritz Fischer 	priv = netdev_priv(ndev);
1293492caffaSMoritz Fischer 	priv->ndev = ndev;
1294492caffaSMoritz Fischer 	priv->dev = &pdev->dev;
1295492caffaSMoritz Fischer 
1296492caffaSMoritz Fischer 	netif_napi_add(ndev, &priv->napi, nixge_poll, NAPI_POLL_WEIGHT);
129787ab2079SAlex Williams 	err = nixge_of_get_resources(pdev);
129887ab2079SAlex Williams 	if (err)
1299366228edSLu Wei 		goto free_netdev;
1300492caffaSMoritz Fischer 	__nixge_hw_set_mac_address(ndev);
1301492caffaSMoritz Fischer 
1302492caffaSMoritz Fischer 	priv->tx_irq = platform_get_irq_byname(pdev, "tx");
1303492caffaSMoritz Fischer 	if (priv->tx_irq < 0) {
1304492caffaSMoritz Fischer 		netdev_err(ndev, "could not find 'tx' irq");
1305366228edSLu Wei 		err = priv->tx_irq;
1306366228edSLu Wei 		goto free_netdev;
1307492caffaSMoritz Fischer 	}
1308492caffaSMoritz Fischer 
1309492caffaSMoritz Fischer 	priv->rx_irq = platform_get_irq_byname(pdev, "rx");
1310492caffaSMoritz Fischer 	if (priv->rx_irq < 0) {
1311492caffaSMoritz Fischer 		netdev_err(ndev, "could not find 'rx' irq");
1312366228edSLu Wei 		err = priv->rx_irq;
1313366228edSLu Wei 		goto free_netdev;
1314492caffaSMoritz Fischer 	}
1315492caffaSMoritz Fischer 
1316492caffaSMoritz Fischer 	priv->coalesce_count_rx = XAXIDMA_DFT_RX_THRESHOLD;
1317492caffaSMoritz Fischer 	priv->coalesce_count_tx = XAXIDMA_DFT_TX_THRESHOLD;
1318492caffaSMoritz Fischer 
1319dd648818SMoritz Fischer 	mn = of_get_child_by_name(pdev->dev.of_node, "mdio");
1320dd648818SMoritz Fischer 	if (mn) {
1321dd648818SMoritz Fischer 		err = nixge_mdio_setup(priv, mn);
1322dd648818SMoritz Fischer 		of_node_put(mn);
1323492caffaSMoritz Fischer 		if (err) {
1324492caffaSMoritz Fischer 			netdev_err(ndev, "error registering mdio bus");
1325492caffaSMoritz Fischer 			goto free_netdev;
1326492caffaSMoritz Fischer 		}
1327dd648818SMoritz Fischer 	}
1328492caffaSMoritz Fischer 
13290c65b2b9SAndrew Lunn 	err = of_get_phy_mode(pdev->dev.of_node, &priv->phy_mode);
13300c65b2b9SAndrew Lunn 	if (err) {
1331492caffaSMoritz Fischer 		netdev_err(ndev, "not find \"phy-mode\" property\n");
1332492caffaSMoritz Fischer 		goto unregister_mdio;
1333492caffaSMoritz Fischer 	}
1334492caffaSMoritz Fischer 
13358dc0ae90SMoritz Fischer 	phy_node = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
13368dc0ae90SMoritz Fischer 	if (!phy_node && of_phy_is_fixed_link(pdev->dev.of_node)) {
13378dc0ae90SMoritz Fischer 		err = of_phy_register_fixed_link(pdev->dev.of_node);
13388dc0ae90SMoritz Fischer 		if (err < 0) {
13398dc0ae90SMoritz Fischer 			netdev_err(ndev, "broken fixed-link specification\n");
1340492caffaSMoritz Fischer 			goto unregister_mdio;
1341492caffaSMoritz Fischer 		}
13428dc0ae90SMoritz Fischer 		phy_node = of_node_get(pdev->dev.of_node);
13438dc0ae90SMoritz Fischer 	}
13448dc0ae90SMoritz Fischer 	priv->phy_node = phy_node;
1345492caffaSMoritz Fischer 
1346492caffaSMoritz Fischer 	err = register_netdev(priv->ndev);
1347492caffaSMoritz Fischer 	if (err) {
1348492caffaSMoritz Fischer 		netdev_err(ndev, "register_netdev() error (%i)\n", err);
13498dc0ae90SMoritz Fischer 		goto free_phy;
1350492caffaSMoritz Fischer 	}
1351492caffaSMoritz Fischer 
1352492caffaSMoritz Fischer 	return 0;
1353492caffaSMoritz Fischer 
13548dc0ae90SMoritz Fischer free_phy:
13558dc0ae90SMoritz Fischer 	if (of_phy_is_fixed_link(pdev->dev.of_node))
13568dc0ae90SMoritz Fischer 		of_phy_deregister_fixed_link(pdev->dev.of_node);
13578dc0ae90SMoritz Fischer 	of_node_put(phy_node);
13588dc0ae90SMoritz Fischer 
1359492caffaSMoritz Fischer unregister_mdio:
1360dd648818SMoritz Fischer 	if (priv->mii_bus)
1361492caffaSMoritz Fischer 		mdiobus_unregister(priv->mii_bus);
1362492caffaSMoritz Fischer 
1363492caffaSMoritz Fischer free_netdev:
1364492caffaSMoritz Fischer 	free_netdev(ndev);
1365492caffaSMoritz Fischer 
1366492caffaSMoritz Fischer 	return err;
1367492caffaSMoritz Fischer }
1368492caffaSMoritz Fischer 
1369492caffaSMoritz Fischer static int nixge_remove(struct platform_device *pdev)
1370492caffaSMoritz Fischer {
1371492caffaSMoritz Fischer 	struct net_device *ndev = platform_get_drvdata(pdev);
1372492caffaSMoritz Fischer 	struct nixge_priv *priv = netdev_priv(ndev);
1373492caffaSMoritz Fischer 
1374492caffaSMoritz Fischer 	unregister_netdev(ndev);
1375492caffaSMoritz Fischer 
13768dc0ae90SMoritz Fischer 	if (of_phy_is_fixed_link(pdev->dev.of_node))
13778dc0ae90SMoritz Fischer 		of_phy_deregister_fixed_link(pdev->dev.of_node);
13788dc0ae90SMoritz Fischer 	of_node_put(priv->phy_node);
13798dc0ae90SMoritz Fischer 
1380dd648818SMoritz Fischer 	if (priv->mii_bus)
1381492caffaSMoritz Fischer 		mdiobus_unregister(priv->mii_bus);
1382492caffaSMoritz Fischer 
1383492caffaSMoritz Fischer 	free_netdev(ndev);
1384492caffaSMoritz Fischer 
1385492caffaSMoritz Fischer 	return 0;
1386492caffaSMoritz Fischer }
1387492caffaSMoritz Fischer 
1388492caffaSMoritz Fischer static struct platform_driver nixge_driver = {
1389492caffaSMoritz Fischer 	.probe		= nixge_probe,
1390492caffaSMoritz Fischer 	.remove		= nixge_remove,
1391492caffaSMoritz Fischer 	.driver		= {
1392492caffaSMoritz Fischer 		.name		= "nixge",
1393492caffaSMoritz Fischer 		.of_match_table	= of_match_ptr(nixge_dt_ids),
1394492caffaSMoritz Fischer 	},
1395492caffaSMoritz Fischer };
1396492caffaSMoritz Fischer module_platform_driver(nixge_driver);
1397492caffaSMoritz Fischer 
1398492caffaSMoritz Fischer MODULE_LICENSE("GPL v2");
1399492caffaSMoritz Fischer MODULE_DESCRIPTION("National Instruments XGE Management MAC");
1400492caffaSMoritz Fischer MODULE_AUTHOR("Moritz Fischer <mdf@kernel.org>");
1401