xref: /linux/drivers/net/ethernet/xilinx/xilinx_axienet_main.c (revision 8e8cd9a4c6dc006e9bd51b8ad0079fb4576b48d3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Xilinx Axi Ethernet device driver
4  *
5  * Copyright (c) 2008 Nissin Systems Co., Ltd.,  Yoshio Kashiwagi
6  * Copyright (c) 2005-2008 DLA Systems,  David H. Lynch Jr. <dhlii@dlasys.net>
7  * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
8  * Copyright (c) 2010 - 2011 Michal Simek <monstr@monstr.eu>
9  * Copyright (c) 2010 - 2011 PetaLogix
10  * Copyright (c) 2019 - 2022 Calian Advanced Technologies
11  * Copyright (c) 2010 - 2012 Xilinx, Inc. All rights reserved.
12  *
13  * This is a driver for the Xilinx Axi Ethernet which is used in the Virtex6
14  * and Spartan6.
15  *
16  * TODO:
17  *  - Add Axi Fifo support.
18  *  - Factor out Axi DMA code into separate driver.
19  *  - Test and fix basic multicast filtering.
20  *  - Add support for extended multicast filtering.
21  *  - Test basic VLAN support.
22  *  - Add support for extended VLAN support.
23  */
24 
25 #include <linux/clk.h>
26 #include <linux/delay.h>
27 #include <linux/etherdevice.h>
28 #include <linux/module.h>
29 #include <linux/netdevice.h>
30 #include <linux/of.h>
31 #include <linux/of_mdio.h>
32 #include <linux/of_net.h>
33 #include <linux/of_irq.h>
34 #include <linux/of_address.h>
35 #include <linux/platform_device.h>
36 #include <linux/skbuff.h>
37 #include <linux/math64.h>
38 #include <linux/phy.h>
39 #include <linux/mii.h>
40 #include <linux/ethtool.h>
41 #include <linux/dmaengine.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/dma/xilinx_dma.h>
44 #include <linux/circ_buf.h>
45 #include <net/netdev_queues.h>
46 
47 #include "xilinx_axienet.h"
48 
49 /* Descriptors defines for Tx and Rx DMA */
50 #define TX_BD_NUM_DEFAULT		128
51 #define RX_BD_NUM_DEFAULT		1024
52 #define TX_BD_NUM_MIN			(MAX_SKB_FRAGS + 1)
53 #define TX_BD_NUM_MAX			4096
54 #define RX_BD_NUM_MAX			4096
55 #define DMA_NUM_APP_WORDS		5
56 #define LEN_APP				4
57 #define RX_BUF_NUM_DEFAULT		128
58 
59 /* Must be shorter than length of ethtool_drvinfo.driver field to fit */
60 #define DRIVER_NAME		"xaxienet"
61 #define DRIVER_DESCRIPTION	"Xilinx Axi Ethernet driver"
62 #define DRIVER_VERSION		"1.00a"
63 
64 #define AXIENET_REGS_N		40
65 
66 static void axienet_rx_submit_desc(struct net_device *ndev);
67 
68 /* Match table for of_platform binding */
69 static const struct of_device_id axienet_of_match[] = {
70 	{ .compatible = "xlnx,axi-ethernet-1.00.a", },
71 	{ .compatible = "xlnx,axi-ethernet-1.01.a", },
72 	{ .compatible = "xlnx,axi-ethernet-2.01.a", },
73 	{},
74 };
75 
76 MODULE_DEVICE_TABLE(of, axienet_of_match);
77 
78 /* Option table for setting up Axi Ethernet hardware options */
79 static struct axienet_option axienet_options[] = {
80 	/* Turn on jumbo packet support for both Rx and Tx */
81 	{
82 		.opt = XAE_OPTION_JUMBO,
83 		.reg = XAE_TC_OFFSET,
84 		.m_or = XAE_TC_JUM_MASK,
85 	}, {
86 		.opt = XAE_OPTION_JUMBO,
87 		.reg = XAE_RCW1_OFFSET,
88 		.m_or = XAE_RCW1_JUM_MASK,
89 	}, { /* Turn on VLAN packet support for both Rx and Tx */
90 		.opt = XAE_OPTION_VLAN,
91 		.reg = XAE_TC_OFFSET,
92 		.m_or = XAE_TC_VLAN_MASK,
93 	}, {
94 		.opt = XAE_OPTION_VLAN,
95 		.reg = XAE_RCW1_OFFSET,
96 		.m_or = XAE_RCW1_VLAN_MASK,
97 	}, { /* Turn on FCS stripping on receive packets */
98 		.opt = XAE_OPTION_FCS_STRIP,
99 		.reg = XAE_RCW1_OFFSET,
100 		.m_or = XAE_RCW1_FCS_MASK,
101 	}, { /* Turn on FCS insertion on transmit packets */
102 		.opt = XAE_OPTION_FCS_INSERT,
103 		.reg = XAE_TC_OFFSET,
104 		.m_or = XAE_TC_FCS_MASK,
105 	}, { /* Turn off length/type field checking on receive packets */
106 		.opt = XAE_OPTION_LENTYPE_ERR,
107 		.reg = XAE_RCW1_OFFSET,
108 		.m_or = XAE_RCW1_LT_DIS_MASK,
109 	}, { /* Turn on Rx flow control */
110 		.opt = XAE_OPTION_FLOW_CONTROL,
111 		.reg = XAE_FCC_OFFSET,
112 		.m_or = XAE_FCC_FCRX_MASK,
113 	}, { /* Turn on Tx flow control */
114 		.opt = XAE_OPTION_FLOW_CONTROL,
115 		.reg = XAE_FCC_OFFSET,
116 		.m_or = XAE_FCC_FCTX_MASK,
117 	}, { /* Turn on promiscuous frame filtering */
118 		.opt = XAE_OPTION_PROMISC,
119 		.reg = XAE_FMI_OFFSET,
120 		.m_or = XAE_FMI_PM_MASK,
121 	}, { /* Enable transmitter */
122 		.opt = XAE_OPTION_TXEN,
123 		.reg = XAE_TC_OFFSET,
124 		.m_or = XAE_TC_TX_MASK,
125 	}, { /* Enable receiver */
126 		.opt = XAE_OPTION_RXEN,
127 		.reg = XAE_RCW1_OFFSET,
128 		.m_or = XAE_RCW1_RX_MASK,
129 	},
130 	{}
131 };
132 
axienet_get_rx_desc(struct axienet_local * lp,int i)133 static struct skbuf_dma_descriptor *axienet_get_rx_desc(struct axienet_local *lp, int i)
134 {
135 	return lp->rx_skb_ring[i & (RX_BUF_NUM_DEFAULT - 1)];
136 }
137 
axienet_get_tx_desc(struct axienet_local * lp,int i)138 static struct skbuf_dma_descriptor *axienet_get_tx_desc(struct axienet_local *lp, int i)
139 {
140 	return lp->tx_skb_ring[i & (TX_BD_NUM_MAX - 1)];
141 }
142 
143 /**
144  * axienet_dma_in32 - Memory mapped Axi DMA register read
145  * @lp:		Pointer to axienet local structure
146  * @reg:	Address offset from the base address of the Axi DMA core
147  *
148  * Return: The contents of the Axi DMA register
149  *
150  * This function returns the contents of the corresponding Axi DMA register.
151  */
axienet_dma_in32(struct axienet_local * lp,off_t reg)152 static inline u32 axienet_dma_in32(struct axienet_local *lp, off_t reg)
153 {
154 	return ioread32(lp->dma_regs + reg);
155 }
156 
desc_set_phys_addr(struct axienet_local * lp,dma_addr_t addr,struct axidma_bd * desc)157 static void desc_set_phys_addr(struct axienet_local *lp, dma_addr_t addr,
158 			       struct axidma_bd *desc)
159 {
160 	desc->phys = lower_32_bits(addr);
161 	if (lp->features & XAE_FEATURE_DMA_64BIT)
162 		desc->phys_msb = upper_32_bits(addr);
163 }
164 
desc_get_phys_addr(struct axienet_local * lp,struct axidma_bd * desc)165 static dma_addr_t desc_get_phys_addr(struct axienet_local *lp,
166 				     struct axidma_bd *desc)
167 {
168 	dma_addr_t ret = desc->phys;
169 
170 	if (lp->features & XAE_FEATURE_DMA_64BIT)
171 		ret |= ((dma_addr_t)desc->phys_msb << 16) << 16;
172 
173 	return ret;
174 }
175 
176 /**
177  * axienet_dma_bd_release - Release buffer descriptor rings
178  * @ndev:	Pointer to the net_device structure
179  *
180  * This function is used to release the descriptors allocated in
181  * axienet_dma_bd_init. axienet_dma_bd_release is called when Axi Ethernet
182  * driver stop api is called.
183  */
axienet_dma_bd_release(struct net_device * ndev)184 static void axienet_dma_bd_release(struct net_device *ndev)
185 {
186 	int i;
187 	struct axienet_local *lp = netdev_priv(ndev);
188 
189 	/* If we end up here, tx_bd_v must have been DMA allocated. */
190 	dma_free_coherent(lp->dev,
191 			  sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
192 			  lp->tx_bd_v,
193 			  lp->tx_bd_p);
194 
195 	if (!lp->rx_bd_v)
196 		return;
197 
198 	for (i = 0; i < lp->rx_bd_num; i++) {
199 		dma_addr_t phys;
200 
201 		/* A NULL skb means this descriptor has not been initialised
202 		 * at all.
203 		 */
204 		if (!lp->rx_bd_v[i].skb)
205 			break;
206 
207 		dev_kfree_skb(lp->rx_bd_v[i].skb);
208 
209 		/* For each descriptor, we programmed cntrl with the (non-zero)
210 		 * descriptor size, after it had been successfully allocated.
211 		 * So a non-zero value in there means we need to unmap it.
212 		 */
213 		if (lp->rx_bd_v[i].cntrl) {
214 			phys = desc_get_phys_addr(lp, &lp->rx_bd_v[i]);
215 			dma_unmap_single(lp->dev, phys,
216 					 lp->max_frm_size, DMA_FROM_DEVICE);
217 		}
218 	}
219 
220 	dma_free_coherent(lp->dev,
221 			  sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
222 			  lp->rx_bd_v,
223 			  lp->rx_bd_p);
224 }
225 
axienet_dma_rate(struct axienet_local * lp)226 static u64 axienet_dma_rate(struct axienet_local *lp)
227 {
228 	if (lp->axi_clk)
229 		return clk_get_rate(lp->axi_clk);
230 	return 125000000; /* arbitrary guess if no clock rate set */
231 }
232 
233 /**
234  * axienet_calc_cr() - Calculate control register value
235  * @lp: Device private data
236  * @count: Number of completions before an interrupt
237  * @usec: Microseconds after the last completion before an interrupt
238  *
239  * Calculate a control register value based on the coalescing settings. The
240  * run/stop bit is not set.
241  *
242  * Return: Control register value with coalescing settings configured.
243  */
axienet_calc_cr(struct axienet_local * lp,u32 count,u32 usec)244 static u32 axienet_calc_cr(struct axienet_local *lp, u32 count, u32 usec)
245 {
246 	u32 cr;
247 
248 	cr = FIELD_PREP(XAXIDMA_COALESCE_MASK, count) | XAXIDMA_IRQ_IOC_MASK |
249 	     XAXIDMA_IRQ_ERROR_MASK;
250 	/* Only set interrupt delay timer if not generating an interrupt on
251 	 * the first packet. Otherwise leave at 0 to disable delay interrupt.
252 	 */
253 	if (count > 1) {
254 		u64 clk_rate = axienet_dma_rate(lp);
255 		u32 timer;
256 
257 		/* 1 Timeout Interval = 125 * (clock period of SG clock) */
258 		timer = DIV64_U64_ROUND_CLOSEST((u64)usec * clk_rate,
259 						XAXIDMA_DELAY_SCALE);
260 
261 		timer = min(timer, FIELD_MAX(XAXIDMA_DELAY_MASK));
262 		cr |= FIELD_PREP(XAXIDMA_DELAY_MASK, timer) |
263 		      XAXIDMA_IRQ_DELAY_MASK;
264 	}
265 
266 	return cr;
267 }
268 
269 /**
270  * axienet_coalesce_params() - Extract coalesce parameters from the CR
271  * @lp: Device private data
272  * @cr: The control register to parse
273  * @count: Number of packets before an interrupt
274  * @usec: Idle time (in usec) before an interrupt
275  */
axienet_coalesce_params(struct axienet_local * lp,u32 cr,u32 * count,u32 * usec)276 static void axienet_coalesce_params(struct axienet_local *lp, u32 cr,
277 				    u32 *count, u32 *usec)
278 {
279 	u64 clk_rate = axienet_dma_rate(lp);
280 	u64 timer = FIELD_GET(XAXIDMA_DELAY_MASK, cr);
281 
282 	*count = FIELD_GET(XAXIDMA_COALESCE_MASK, cr);
283 	*usec = DIV64_U64_ROUND_CLOSEST(timer * XAXIDMA_DELAY_SCALE, clk_rate);
284 }
285 
286 /**
287  * axienet_dma_start - Set up DMA registers and start DMA operation
288  * @lp:		Pointer to the axienet_local structure
289  */
axienet_dma_start(struct axienet_local * lp)290 static void axienet_dma_start(struct axienet_local *lp)
291 {
292 	spin_lock_irq(&lp->rx_cr_lock);
293 
294 	/* Start updating the Rx channel control register */
295 	lp->rx_dma_cr &= ~XAXIDMA_CR_RUNSTOP_MASK;
296 	axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, lp->rx_dma_cr);
297 
298 	/* Populate the tail pointer and bring the Rx Axi DMA engine out of
299 	 * halted state. This will make the Rx side ready for reception.
300 	 */
301 	axienet_dma_out_addr(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
302 	lp->rx_dma_cr |= XAXIDMA_CR_RUNSTOP_MASK;
303 	axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, lp->rx_dma_cr);
304 	axienet_dma_out_addr(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
305 			     (sizeof(*lp->rx_bd_v) * (lp->rx_bd_num - 1)));
306 	lp->rx_dma_started = true;
307 
308 	spin_unlock_irq(&lp->rx_cr_lock);
309 	spin_lock_irq(&lp->tx_cr_lock);
310 
311 	/* Start updating the Tx channel control register */
312 	lp->tx_dma_cr &= ~XAXIDMA_CR_RUNSTOP_MASK;
313 	axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, lp->tx_dma_cr);
314 
315 	/* Write to the RS (Run-stop) bit in the Tx channel control register.
316 	 * Tx channel is now ready to run. But only after we write to the
317 	 * tail pointer register that the Tx channel will start transmitting.
318 	 */
319 	axienet_dma_out_addr(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
320 	lp->tx_dma_cr |= XAXIDMA_CR_RUNSTOP_MASK;
321 	axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, lp->tx_dma_cr);
322 	lp->tx_dma_started = true;
323 
324 	spin_unlock_irq(&lp->tx_cr_lock);
325 }
326 
327 /**
328  * axienet_dma_bd_init - Setup buffer descriptor rings for Axi DMA
329  * @ndev:	Pointer to the net_device structure
330  *
331  * Return: 0, on success -ENOMEM, on failure
332  *
333  * This function is called to initialize the Rx and Tx DMA descriptor
334  * rings. This initializes the descriptors with required default values
335  * and is called when Axi Ethernet driver reset is called.
336  */
axienet_dma_bd_init(struct net_device * ndev)337 static int axienet_dma_bd_init(struct net_device *ndev)
338 {
339 	int i;
340 	struct sk_buff *skb;
341 	struct axienet_local *lp = netdev_priv(ndev);
342 
343 	/* Reset the indexes which are used for accessing the BDs */
344 	lp->tx_bd_ci = 0;
345 	lp->tx_bd_tail = 0;
346 	lp->rx_bd_ci = 0;
347 
348 	/* Allocate the Tx and Rx buffer descriptors. */
349 	lp->tx_bd_v = dma_alloc_coherent(lp->dev,
350 					 sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
351 					 &lp->tx_bd_p, GFP_KERNEL);
352 	if (!lp->tx_bd_v)
353 		return -ENOMEM;
354 
355 	lp->rx_bd_v = dma_alloc_coherent(lp->dev,
356 					 sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
357 					 &lp->rx_bd_p, GFP_KERNEL);
358 	if (!lp->rx_bd_v)
359 		goto out;
360 
361 	for (i = 0; i < lp->tx_bd_num; i++) {
362 		dma_addr_t addr = lp->tx_bd_p +
363 				  sizeof(*lp->tx_bd_v) *
364 				  ((i + 1) % lp->tx_bd_num);
365 
366 		lp->tx_bd_v[i].next = lower_32_bits(addr);
367 		if (lp->features & XAE_FEATURE_DMA_64BIT)
368 			lp->tx_bd_v[i].next_msb = upper_32_bits(addr);
369 	}
370 
371 	for (i = 0; i < lp->rx_bd_num; i++) {
372 		dma_addr_t addr;
373 
374 		addr = lp->rx_bd_p + sizeof(*lp->rx_bd_v) *
375 			((i + 1) % lp->rx_bd_num);
376 		lp->rx_bd_v[i].next = lower_32_bits(addr);
377 		if (lp->features & XAE_FEATURE_DMA_64BIT)
378 			lp->rx_bd_v[i].next_msb = upper_32_bits(addr);
379 
380 		skb = netdev_alloc_skb_ip_align(ndev, lp->max_frm_size);
381 		if (!skb)
382 			goto out;
383 
384 		lp->rx_bd_v[i].skb = skb;
385 		addr = dma_map_single(lp->dev, skb->data,
386 				      lp->max_frm_size, DMA_FROM_DEVICE);
387 		if (dma_mapping_error(lp->dev, addr)) {
388 			netdev_err(ndev, "DMA mapping error\n");
389 			goto out;
390 		}
391 		desc_set_phys_addr(lp, addr, &lp->rx_bd_v[i]);
392 
393 		lp->rx_bd_v[i].cntrl = lp->max_frm_size;
394 	}
395 
396 	axienet_dma_start(lp);
397 
398 	return 0;
399 out:
400 	axienet_dma_bd_release(ndev);
401 	return -ENOMEM;
402 }
403 
404 /**
405  * axienet_set_mac_address - Write the MAC address
406  * @ndev:	Pointer to the net_device structure
407  * @address:	6 byte Address to be written as MAC address
408  *
409  * This function is called to initialize the MAC address of the Axi Ethernet
410  * core. It writes to the UAW0 and UAW1 registers of the core.
411  */
axienet_set_mac_address(struct net_device * ndev,const void * address)412 static void axienet_set_mac_address(struct net_device *ndev,
413 				    const void *address)
414 {
415 	struct axienet_local *lp = netdev_priv(ndev);
416 
417 	if (address)
418 		eth_hw_addr_set(ndev, address);
419 	if (!is_valid_ether_addr(ndev->dev_addr))
420 		eth_hw_addr_random(ndev);
421 
422 	/* Set up unicast MAC address filter set its mac address */
423 	axienet_iow(lp, XAE_UAW0_OFFSET,
424 		    (ndev->dev_addr[0]) |
425 		    (ndev->dev_addr[1] << 8) |
426 		    (ndev->dev_addr[2] << 16) |
427 		    (ndev->dev_addr[3] << 24));
428 	axienet_iow(lp, XAE_UAW1_OFFSET,
429 		    (((axienet_ior(lp, XAE_UAW1_OFFSET)) &
430 		      ~XAE_UAW1_UNICASTADDR_MASK) |
431 		     (ndev->dev_addr[4] |
432 		     (ndev->dev_addr[5] << 8))));
433 }
434 
435 /**
436  * netdev_set_mac_address - Write the MAC address (from outside the driver)
437  * @ndev:	Pointer to the net_device structure
438  * @p:		6 byte Address to be written as MAC address
439  *
440  * Return: 0 for all conditions. Presently, there is no failure case.
441  *
442  * This function is called to initialize the MAC address of the Axi Ethernet
443  * core. It calls the core specific axienet_set_mac_address. This is the
444  * function that goes into net_device_ops structure entry ndo_set_mac_address.
445  */
netdev_set_mac_address(struct net_device * ndev,void * p)446 static int netdev_set_mac_address(struct net_device *ndev, void *p)
447 {
448 	struct sockaddr *addr = p;
449 
450 	axienet_set_mac_address(ndev, addr->sa_data);
451 	return 0;
452 }
453 
454 /**
455  * axienet_set_multicast_list - Prepare the multicast table
456  * @ndev:	Pointer to the net_device structure
457  *
458  * This function is called to initialize the multicast table during
459  * initialization. The Axi Ethernet basic multicast support has a four-entry
460  * multicast table which is initialized here. Additionally this function
461  * goes into the net_device_ops structure entry ndo_set_multicast_list. This
462  * means whenever the multicast table entries need to be updated this
463  * function gets called.
464  */
axienet_set_multicast_list(struct net_device * ndev)465 static void axienet_set_multicast_list(struct net_device *ndev)
466 {
467 	int i = 0;
468 	u32 reg, af0reg, af1reg;
469 	struct axienet_local *lp = netdev_priv(ndev);
470 
471 	reg = axienet_ior(lp, XAE_FMI_OFFSET);
472 	reg &= ~XAE_FMI_PM_MASK;
473 	if (ndev->flags & IFF_PROMISC)
474 		reg |= XAE_FMI_PM_MASK;
475 	else
476 		reg &= ~XAE_FMI_PM_MASK;
477 	axienet_iow(lp, XAE_FMI_OFFSET, reg);
478 
479 	if (ndev->flags & IFF_ALLMULTI ||
480 	    netdev_mc_count(ndev) > XAE_MULTICAST_CAM_TABLE_NUM) {
481 		reg &= 0xFFFFFF00;
482 		axienet_iow(lp, XAE_FMI_OFFSET, reg);
483 		axienet_iow(lp, XAE_AF0_OFFSET, 1); /* Multicast bit */
484 		axienet_iow(lp, XAE_AF1_OFFSET, 0);
485 		axienet_iow(lp, XAE_AM0_OFFSET, 1); /* ditto */
486 		axienet_iow(lp, XAE_AM1_OFFSET, 0);
487 		axienet_iow(lp, XAE_FFE_OFFSET, 1);
488 		i = 1;
489 	} else if (!netdev_mc_empty(ndev)) {
490 		struct netdev_hw_addr *ha;
491 
492 		netdev_for_each_mc_addr(ha, ndev) {
493 			if (i >= XAE_MULTICAST_CAM_TABLE_NUM)
494 				break;
495 
496 			af0reg = (ha->addr[0]);
497 			af0reg |= (ha->addr[1] << 8);
498 			af0reg |= (ha->addr[2] << 16);
499 			af0reg |= (ha->addr[3] << 24);
500 
501 			af1reg = (ha->addr[4]);
502 			af1reg |= (ha->addr[5] << 8);
503 
504 			reg &= 0xFFFFFF00;
505 			reg |= i;
506 
507 			axienet_iow(lp, XAE_FMI_OFFSET, reg);
508 			axienet_iow(lp, XAE_AF0_OFFSET, af0reg);
509 			axienet_iow(lp, XAE_AF1_OFFSET, af1reg);
510 			axienet_iow(lp, XAE_AM0_OFFSET, 0xffffffff);
511 			axienet_iow(lp, XAE_AM1_OFFSET, 0x0000ffff);
512 			axienet_iow(lp, XAE_FFE_OFFSET, 1);
513 			i++;
514 		}
515 	}
516 
517 	for (; i < XAE_MULTICAST_CAM_TABLE_NUM; i++) {
518 		reg &= 0xFFFFFF00;
519 		reg |= i;
520 		axienet_iow(lp, XAE_FMI_OFFSET, reg);
521 		axienet_iow(lp, XAE_FFE_OFFSET, 0);
522 	}
523 }
524 
525 /**
526  * axienet_setoptions - Set an Axi Ethernet option
527  * @ndev:	Pointer to the net_device structure
528  * @options:	Option to be enabled/disabled
529  *
530  * The Axi Ethernet core has multiple features which can be selectively turned
531  * on or off. The typical options could be jumbo frame option, basic VLAN
532  * option, promiscuous mode option etc. This function is used to set or clear
533  * these options in the Axi Ethernet hardware. This is done through
534  * axienet_option structure .
535  */
axienet_setoptions(struct net_device * ndev,u32 options)536 static void axienet_setoptions(struct net_device *ndev, u32 options)
537 {
538 	int reg;
539 	struct axienet_local *lp = netdev_priv(ndev);
540 	struct axienet_option *tp = &axienet_options[0];
541 
542 	while (tp->opt) {
543 		reg = ((axienet_ior(lp, tp->reg)) & ~(tp->m_or));
544 		if (options & tp->opt)
545 			reg |= tp->m_or;
546 		axienet_iow(lp, tp->reg, reg);
547 		tp++;
548 	}
549 
550 	lp->options |= options;
551 }
552 
axienet_stat(struct axienet_local * lp,enum temac_stat stat)553 static u64 axienet_stat(struct axienet_local *lp, enum temac_stat stat)
554 {
555 	u32 counter;
556 
557 	if (lp->reset_in_progress)
558 		return lp->hw_stat_base[stat];
559 
560 	counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);
561 	return lp->hw_stat_base[stat] + (counter - lp->hw_last_counter[stat]);
562 }
563 
axienet_stats_update(struct axienet_local * lp,bool reset)564 static void axienet_stats_update(struct axienet_local *lp, bool reset)
565 {
566 	enum temac_stat stat;
567 
568 	write_seqcount_begin(&lp->hw_stats_seqcount);
569 	lp->reset_in_progress = reset;
570 	for (stat = 0; stat < STAT_COUNT; stat++) {
571 		u32 counter = axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);
572 
573 		lp->hw_stat_base[stat] += counter - lp->hw_last_counter[stat];
574 		lp->hw_last_counter[stat] = counter;
575 	}
576 	write_seqcount_end(&lp->hw_stats_seqcount);
577 }
578 
axienet_refresh_stats(struct work_struct * work)579 static void axienet_refresh_stats(struct work_struct *work)
580 {
581 	struct axienet_local *lp = container_of(work, struct axienet_local,
582 						stats_work.work);
583 
584 	mutex_lock(&lp->stats_lock);
585 	axienet_stats_update(lp, false);
586 	mutex_unlock(&lp->stats_lock);
587 
588 	/* Just less than 2^32 bytes at 2.5 GBit/s */
589 	schedule_delayed_work(&lp->stats_work, 13 * HZ);
590 }
591 
__axienet_device_reset(struct axienet_local * lp)592 static int __axienet_device_reset(struct axienet_local *lp)
593 {
594 	u32 value;
595 	int ret;
596 
597 	/* Save statistics counters in case they will be reset */
598 	mutex_lock(&lp->stats_lock);
599 	if (lp->features & XAE_FEATURE_STATS)
600 		axienet_stats_update(lp, true);
601 
602 	/* Reset Axi DMA. This would reset Axi Ethernet core as well. The reset
603 	 * process of Axi DMA takes a while to complete as all pending
604 	 * commands/transfers will be flushed or completed during this
605 	 * reset process.
606 	 * Note that even though both TX and RX have their own reset register,
607 	 * they both reset the entire DMA core, so only one needs to be used.
608 	 */
609 	axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, XAXIDMA_CR_RESET_MASK);
610 	ret = read_poll_timeout(axienet_dma_in32, value,
611 				!(value & XAXIDMA_CR_RESET_MASK),
612 				DELAY_OF_ONE_MILLISEC, 50000, false, lp,
613 				XAXIDMA_TX_CR_OFFSET);
614 	if (ret) {
615 		dev_err(lp->dev, "%s: DMA reset timeout!\n", __func__);
616 		goto out;
617 	}
618 
619 	/* Wait for PhyRstCmplt bit to be set, indicating the PHY reset has finished */
620 	ret = read_poll_timeout(axienet_ior, value,
621 				value & XAE_INT_PHYRSTCMPLT_MASK,
622 				DELAY_OF_ONE_MILLISEC, 50000, false, lp,
623 				XAE_IS_OFFSET);
624 	if (ret) {
625 		dev_err(lp->dev, "%s: timeout waiting for PhyRstCmplt\n", __func__);
626 		goto out;
627 	}
628 
629 	/* Update statistics counters with new values */
630 	if (lp->features & XAE_FEATURE_STATS) {
631 		enum temac_stat stat;
632 
633 		write_seqcount_begin(&lp->hw_stats_seqcount);
634 		lp->reset_in_progress = false;
635 		for (stat = 0; stat < STAT_COUNT; stat++) {
636 			u32 counter =
637 				axienet_ior(lp, XAE_STATS_OFFSET + stat * 8);
638 
639 			lp->hw_stat_base[stat] +=
640 				lp->hw_last_counter[stat] - counter;
641 			lp->hw_last_counter[stat] = counter;
642 		}
643 		write_seqcount_end(&lp->hw_stats_seqcount);
644 	}
645 
646 out:
647 	mutex_unlock(&lp->stats_lock);
648 	return ret;
649 }
650 
651 /**
652  * axienet_dma_stop - Stop DMA operation
653  * @lp:		Pointer to the axienet_local structure
654  */
axienet_dma_stop(struct axienet_local * lp)655 static void axienet_dma_stop(struct axienet_local *lp)
656 {
657 	int count;
658 	u32 cr, sr;
659 
660 	spin_lock_irq(&lp->rx_cr_lock);
661 
662 	cr = lp->rx_dma_cr & ~(XAXIDMA_CR_RUNSTOP_MASK | XAXIDMA_IRQ_ALL_MASK);
663 	axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
664 	lp->rx_dma_started = false;
665 
666 	spin_unlock_irq(&lp->rx_cr_lock);
667 	synchronize_irq(lp->rx_irq);
668 
669 	spin_lock_irq(&lp->tx_cr_lock);
670 
671 	cr = lp->tx_dma_cr & ~(XAXIDMA_CR_RUNSTOP_MASK | XAXIDMA_IRQ_ALL_MASK);
672 	axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
673 	lp->tx_dma_started = false;
674 
675 	spin_unlock_irq(&lp->tx_cr_lock);
676 	synchronize_irq(lp->tx_irq);
677 
678 	/* Give DMAs a chance to halt gracefully */
679 	sr = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
680 	for (count = 0; !(sr & XAXIDMA_SR_HALT_MASK) && count < 5; ++count) {
681 		msleep(20);
682 		sr = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
683 	}
684 
685 	sr = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
686 	for (count = 0; !(sr & XAXIDMA_SR_HALT_MASK) && count < 5; ++count) {
687 		msleep(20);
688 		sr = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
689 	}
690 
691 	/* Do a reset to ensure DMA is really stopped */
692 	axienet_lock_mii(lp);
693 	__axienet_device_reset(lp);
694 	axienet_unlock_mii(lp);
695 }
696 
697 /**
698  * axienet_device_reset - Reset and initialize the Axi Ethernet hardware.
699  * @ndev:	Pointer to the net_device structure
700  *
701  * This function is called to reset and initialize the Axi Ethernet core. This
702  * is typically called during initialization. It does a reset of the Axi DMA
703  * Rx/Tx channels and initializes the Axi DMA BDs. Since Axi DMA reset lines
704  * are connected to Axi Ethernet reset lines, this in turn resets the Axi
705  * Ethernet core. No separate hardware reset is done for the Axi Ethernet
706  * core.
707  *
708  * Return: 0 on success or a negative error number otherwise.
709  */
axienet_device_reset(struct net_device * ndev)710 static int axienet_device_reset(struct net_device *ndev)
711 {
712 	u32 axienet_status;
713 	struct axienet_local *lp = netdev_priv(ndev);
714 	int ret;
715 
716 	lp->max_frm_size = XAE_MAX_VLAN_FRAME_SIZE;
717 	lp->options |= XAE_OPTION_VLAN;
718 	lp->options &= (~XAE_OPTION_JUMBO);
719 
720 	if (ndev->mtu > XAE_MTU && ndev->mtu <= XAE_JUMBO_MTU) {
721 		lp->max_frm_size = ndev->mtu + VLAN_ETH_HLEN +
722 					XAE_TRL_SIZE;
723 
724 		if (lp->max_frm_size <= lp->rxmem)
725 			lp->options |= XAE_OPTION_JUMBO;
726 	}
727 
728 	if (!lp->use_dmaengine) {
729 		ret = __axienet_device_reset(lp);
730 		if (ret)
731 			return ret;
732 
733 		ret = axienet_dma_bd_init(ndev);
734 		if (ret) {
735 			netdev_err(ndev, "%s: descriptor allocation failed\n",
736 				   __func__);
737 			return ret;
738 		}
739 	}
740 
741 	axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
742 	axienet_status &= ~XAE_RCW1_RX_MASK;
743 	axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
744 
745 	axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
746 	if (axienet_status & XAE_INT_RXRJECT_MASK)
747 		axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
748 	axienet_iow(lp, XAE_IE_OFFSET, lp->eth_irq > 0 ?
749 		    XAE_INT_RECV_ERROR_MASK : 0);
750 
751 	axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
752 
753 	/* Sync default options with HW but leave receiver and
754 	 * transmitter disabled.
755 	 */
756 	axienet_setoptions(ndev, lp->options &
757 			   ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
758 	axienet_set_mac_address(ndev, NULL);
759 	axienet_set_multicast_list(ndev);
760 	axienet_setoptions(ndev, lp->options);
761 
762 	netif_trans_update(ndev);
763 
764 	return 0;
765 }
766 
767 /**
768  * axienet_free_tx_chain - Clean up a series of linked TX descriptors.
769  * @lp:		Pointer to the axienet_local structure
770  * @first_bd:	Index of first descriptor to clean up
771  * @nr_bds:	Max number of descriptors to clean up
772  * @force:	Whether to clean descriptors even if not complete
773  * @sizep:	Pointer to a u32 accumulating the total byte count of
774  *		completed packets (using skb->len). Ignored if NULL.
775  * @budget:	NAPI budget (use 0 when not called from NAPI poll)
776  *
777  * Would either be called after a successful transmit operation, or after
778  * there was an error when setting up the chain.
779  *
780  * Return: The number of packets handled.
781  */
axienet_free_tx_chain(struct axienet_local * lp,u32 first_bd,int nr_bds,bool force,u32 * sizep,int budget)782 static int axienet_free_tx_chain(struct axienet_local *lp, u32 first_bd,
783 				 int nr_bds, bool force, u32 *sizep, int budget)
784 {
785 	struct axidma_bd *cur_p;
786 	unsigned int status;
787 	int i, packets = 0;
788 	dma_addr_t phys;
789 
790 	for (i = 0; i < nr_bds; i++) {
791 		cur_p = &lp->tx_bd_v[(first_bd + i) % lp->tx_bd_num];
792 		status = cur_p->status;
793 
794 		/* If force is not specified, clean up only descriptors
795 		 * that have been completed by the MAC.
796 		 */
797 		if (!force && !(status & XAXIDMA_BD_STS_COMPLETE_MASK))
798 			break;
799 
800 		/* Ensure we see complete descriptor update */
801 		dma_rmb();
802 		phys = desc_get_phys_addr(lp, cur_p);
803 		dma_unmap_single(lp->dev, phys,
804 				 (cur_p->cntrl & XAXIDMA_BD_CTRL_LENGTH_MASK),
805 				 DMA_TO_DEVICE);
806 
807 		if (cur_p->skb && (status & XAXIDMA_BD_STS_COMPLETE_MASK)) {
808 			if (sizep)
809 				*sizep += cur_p->skb->len;
810 			napi_consume_skb(cur_p->skb, budget);
811 			packets++;
812 		}
813 
814 		cur_p->app0 = 0;
815 		cur_p->app1 = 0;
816 		cur_p->app2 = 0;
817 		cur_p->app4 = 0;
818 		cur_p->skb = NULL;
819 		/* ensure our transmit path and device don't prematurely see status cleared */
820 		wmb();
821 		cur_p->cntrl = 0;
822 		cur_p->status = 0;
823 	}
824 
825 	if (!force) {
826 		lp->tx_bd_ci += i;
827 		if (lp->tx_bd_ci >= lp->tx_bd_num)
828 			lp->tx_bd_ci %= lp->tx_bd_num;
829 	}
830 
831 	return packets;
832 }
833 
834 /**
835  * axienet_check_tx_bd_space - Checks if a BD/group of BDs are currently busy
836  * @lp:		Pointer to the axienet_local structure
837  * @num_frag:	The number of BDs to check for
838  *
839  * Return: 0, on success
840  *	    NETDEV_TX_BUSY, if any of the descriptors are not free
841  *
842  * This function is invoked before BDs are allocated and transmission starts.
843  * This function returns 0 if a BD or group of BDs can be allocated for
844  * transmission. If the BD or any of the BDs are not free the function
845  * returns a busy status.
846  */
axienet_check_tx_bd_space(struct axienet_local * lp,int num_frag)847 static inline int axienet_check_tx_bd_space(struct axienet_local *lp,
848 					    int num_frag)
849 {
850 	struct axidma_bd *cur_p;
851 
852 	/* Ensure we see all descriptor updates from device or TX polling */
853 	rmb();
854 	cur_p = &lp->tx_bd_v[(READ_ONCE(lp->tx_bd_tail) + num_frag) %
855 			     lp->tx_bd_num];
856 	if (cur_p->cntrl)
857 		return NETDEV_TX_BUSY;
858 	return 0;
859 }
860 
861 /**
862  * axienet_dma_tx_cb - DMA engine callback for TX channel.
863  * @data:       Pointer to the axienet_local structure.
864  * @result:     error reporting through dmaengine_result.
865  * This function is called by dmaengine driver for TX channel to notify
866  * that the transmit is done.
867  */
axienet_dma_tx_cb(void * data,const struct dmaengine_result * result)868 static void axienet_dma_tx_cb(void *data, const struct dmaengine_result *result)
869 {
870 	struct skbuf_dma_descriptor *skbuf_dma;
871 	struct axienet_local *lp = data;
872 	struct netdev_queue *txq;
873 	int len;
874 
875 	skbuf_dma = axienet_get_tx_desc(lp, lp->tx_ring_tail++);
876 	len = skbuf_dma->skb->len;
877 	txq = skb_get_tx_queue(lp->ndev, skbuf_dma->skb);
878 	u64_stats_update_begin(&lp->tx_stat_sync);
879 	u64_stats_add(&lp->tx_bytes, len);
880 	u64_stats_add(&lp->tx_packets, 1);
881 	u64_stats_update_end(&lp->tx_stat_sync);
882 	dma_unmap_sg(lp->dev, skbuf_dma->sgl, skbuf_dma->sg_len, DMA_TO_DEVICE);
883 	dev_consume_skb_any(skbuf_dma->skb);
884 	skbuf_dma->skb = NULL;
885 	netif_txq_completed_wake(txq, 1, len,
886 				 CIRC_SPACE(lp->tx_ring_head, lp->tx_ring_tail, TX_BD_NUM_MAX),
887 				 2);
888 }
889 
890 /**
891  * axienet_start_xmit_dmaengine - Starts the transmission.
892  * @skb:        sk_buff pointer that contains data to be Txed.
893  * @ndev:       Pointer to net_device structure.
894  *
895  * Return: NETDEV_TX_OK on success or any non space errors.
896  *         NETDEV_TX_BUSY when free element in TX skb ring buffer
897  *         is not available.
898  *
899  * This function is invoked to initiate transmission. The
900  * function sets the skbs, register dma callback API and submit
901  * the dma transaction.
902  * Additionally if checksum offloading is supported,
903  * it populates AXI Stream Control fields with appropriate values.
904  */
905 static netdev_tx_t
axienet_start_xmit_dmaengine(struct sk_buff * skb,struct net_device * ndev)906 axienet_start_xmit_dmaengine(struct sk_buff *skb, struct net_device *ndev)
907 {
908 	struct dma_async_tx_descriptor *dma_tx_desc = NULL;
909 	struct axienet_local *lp = netdev_priv(ndev);
910 	u32 app_metadata[DMA_NUM_APP_WORDS] = {0};
911 	struct skbuf_dma_descriptor *skbuf_dma;
912 	struct dma_device *dma_dev;
913 	struct netdev_queue *txq;
914 	u32 csum_start_off;
915 	u32 csum_index_off;
916 	int sg_len;
917 	int ret;
918 
919 	dma_dev = lp->tx_chan->device;
920 	sg_len = skb_shinfo(skb)->nr_frags + 1;
921 	if (CIRC_SPACE(lp->tx_ring_head, lp->tx_ring_tail, TX_BD_NUM_MAX) <= 1) {
922 		netif_stop_queue(ndev);
923 		if (net_ratelimit())
924 			netdev_warn(ndev, "TX ring unexpectedly full\n");
925 		return NETDEV_TX_BUSY;
926 	}
927 
928 	skbuf_dma = axienet_get_tx_desc(lp, lp->tx_ring_head);
929 	if (!skbuf_dma)
930 		goto xmit_error_drop_skb;
931 
932 	lp->tx_ring_head++;
933 	sg_init_table(skbuf_dma->sgl, sg_len);
934 	ret = skb_to_sgvec(skb, skbuf_dma->sgl, 0, skb->len);
935 	if (ret < 0)
936 		goto xmit_error_drop_skb;
937 
938 	ret = dma_map_sg(lp->dev, skbuf_dma->sgl, sg_len, DMA_TO_DEVICE);
939 	if (!ret)
940 		goto xmit_error_drop_skb;
941 
942 	/* Fill up app fields for checksum */
943 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
944 		if (lp->features & XAE_FEATURE_FULL_TX_CSUM) {
945 			/* Tx Full Checksum Offload Enabled */
946 			app_metadata[0] |= 2;
947 		} else if (lp->features & XAE_FEATURE_PARTIAL_TX_CSUM) {
948 			csum_start_off = skb_transport_offset(skb);
949 			csum_index_off = csum_start_off + skb->csum_offset;
950 			/* Tx Partial Checksum Offload Enabled */
951 			app_metadata[0] |= 1;
952 			app_metadata[1] = (csum_start_off << 16) | csum_index_off;
953 		}
954 	} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
955 		app_metadata[0] |= 2; /* Tx Full Checksum Offload Enabled */
956 	}
957 
958 	dma_tx_desc = dma_dev->device_prep_slave_sg(lp->tx_chan, skbuf_dma->sgl,
959 			sg_len, DMA_MEM_TO_DEV,
960 			DMA_PREP_INTERRUPT, (void *)app_metadata);
961 	if (!dma_tx_desc)
962 		goto xmit_error_unmap_sg;
963 
964 	skbuf_dma->skb = skb;
965 	skbuf_dma->sg_len = sg_len;
966 	dma_tx_desc->callback_param = lp;
967 	dma_tx_desc->callback_result = axienet_dma_tx_cb;
968 	txq = skb_get_tx_queue(lp->ndev, skb);
969 	netdev_tx_sent_queue(txq, skb->len);
970 	netif_txq_maybe_stop(txq, CIRC_SPACE(lp->tx_ring_head, lp->tx_ring_tail, TX_BD_NUM_MAX),
971 			     1, 2);
972 
973 	dmaengine_submit(dma_tx_desc);
974 	dma_async_issue_pending(lp->tx_chan);
975 	return NETDEV_TX_OK;
976 
977 xmit_error_unmap_sg:
978 	dma_unmap_sg(lp->dev, skbuf_dma->sgl, sg_len, DMA_TO_DEVICE);
979 xmit_error_drop_skb:
980 	dev_kfree_skb_any(skb);
981 	return NETDEV_TX_OK;
982 }
983 
984 /**
985  * axienet_tx_poll - Invoked once a transmit is completed by the
986  * Axi DMA Tx channel.
987  * @napi:	Pointer to NAPI structure.
988  * @budget:	Max number of TX packets to process.
989  *
990  * Return: Number of TX packets processed.
991  *
992  * This function is invoked from the NAPI processing to notify the completion
993  * of transmit operation. It clears fields in the corresponding Tx BDs and
994  * unmaps the corresponding buffer so that CPU can regain ownership of the
995  * buffer. It finally invokes "netif_wake_queue" to restart transmission if
996  * required.
997  */
axienet_tx_poll(struct napi_struct * napi,int budget)998 static int axienet_tx_poll(struct napi_struct *napi, int budget)
999 {
1000 	struct axienet_local *lp = container_of(napi, struct axienet_local, napi_tx);
1001 	struct net_device *ndev = lp->ndev;
1002 	u32 size = 0;
1003 	int packets;
1004 
1005 	packets = axienet_free_tx_chain(lp, lp->tx_bd_ci, lp->tx_bd_num, false,
1006 					&size, budget);
1007 
1008 	if (packets) {
1009 		netdev_completed_queue(ndev, packets, size);
1010 		u64_stats_update_begin(&lp->tx_stat_sync);
1011 		u64_stats_add(&lp->tx_packets, packets);
1012 		u64_stats_add(&lp->tx_bytes, size);
1013 		u64_stats_update_end(&lp->tx_stat_sync);
1014 
1015 		/* Matches barrier in axienet_start_xmit */
1016 		smp_mb();
1017 
1018 		if (!axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1))
1019 			netif_wake_queue(ndev);
1020 	}
1021 
1022 	if (packets < budget && napi_complete_done(napi, packets)) {
1023 		/* Re-enable TX completion interrupts. This should
1024 		 * cause an immediate interrupt if any TX packets are
1025 		 * already pending.
1026 		 */
1027 		spin_lock_irq(&lp->tx_cr_lock);
1028 		axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, lp->tx_dma_cr);
1029 		spin_unlock_irq(&lp->tx_cr_lock);
1030 	}
1031 	return packets;
1032 }
1033 
1034 /**
1035  * axienet_start_xmit - Starts the transmission.
1036  * @skb:	sk_buff pointer that contains data to be Txed.
1037  * @ndev:	Pointer to net_device structure.
1038  *
1039  * Return: NETDEV_TX_OK, on success
1040  *	    NETDEV_TX_BUSY, if any of the descriptors are not free
1041  *
1042  * This function is invoked from upper layers to initiate transmission. The
1043  * function uses the next available free BDs and populates their fields to
1044  * start the transmission. Additionally if checksum offloading is supported,
1045  * it populates AXI Stream Control fields with appropriate values.
1046  */
1047 static netdev_tx_t
axienet_start_xmit(struct sk_buff * skb,struct net_device * ndev)1048 axienet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1049 {
1050 	u32 ii;
1051 	u32 num_frag;
1052 	u32 csum_start_off;
1053 	u32 csum_index_off;
1054 	skb_frag_t *frag;
1055 	dma_addr_t tail_p, phys;
1056 	u32 orig_tail_ptr, new_tail_ptr;
1057 	struct axienet_local *lp = netdev_priv(ndev);
1058 	struct axidma_bd *cur_p;
1059 
1060 	orig_tail_ptr = lp->tx_bd_tail;
1061 	new_tail_ptr = orig_tail_ptr;
1062 
1063 	num_frag = skb_shinfo(skb)->nr_frags;
1064 	cur_p = &lp->tx_bd_v[orig_tail_ptr];
1065 
1066 	if (axienet_check_tx_bd_space(lp, num_frag + 1)) {
1067 		/* Should not happen as last start_xmit call should have
1068 		 * checked for sufficient space and queue should only be
1069 		 * woken when sufficient space is available.
1070 		 */
1071 		netif_stop_queue(ndev);
1072 		if (net_ratelimit())
1073 			netdev_warn(ndev, "TX ring unexpectedly full\n");
1074 		return NETDEV_TX_BUSY;
1075 	}
1076 
1077 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1078 		if (lp->features & XAE_FEATURE_FULL_TX_CSUM) {
1079 			/* Tx Full Checksum Offload Enabled */
1080 			cur_p->app0 |= 2;
1081 		} else if (lp->features & XAE_FEATURE_PARTIAL_TX_CSUM) {
1082 			csum_start_off = skb_transport_offset(skb);
1083 			csum_index_off = csum_start_off + skb->csum_offset;
1084 			/* Tx Partial Checksum Offload Enabled */
1085 			cur_p->app0 |= 1;
1086 			cur_p->app1 = (csum_start_off << 16) | csum_index_off;
1087 		}
1088 	} else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1089 		cur_p->app0 |= 2; /* Tx Full Checksum Offload Enabled */
1090 	}
1091 
1092 	phys = dma_map_single(lp->dev, skb->data,
1093 			      skb_headlen(skb), DMA_TO_DEVICE);
1094 	if (unlikely(dma_mapping_error(lp->dev, phys))) {
1095 		if (net_ratelimit())
1096 			netdev_err(ndev, "TX DMA mapping error\n");
1097 		ndev->stats.tx_dropped++;
1098 		dev_kfree_skb_any(skb);
1099 		return NETDEV_TX_OK;
1100 	}
1101 	desc_set_phys_addr(lp, phys, cur_p);
1102 	cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;
1103 
1104 	for (ii = 0; ii < num_frag; ii++) {
1105 		if (++new_tail_ptr >= lp->tx_bd_num)
1106 			new_tail_ptr = 0;
1107 		cur_p = &lp->tx_bd_v[new_tail_ptr];
1108 		frag = &skb_shinfo(skb)->frags[ii];
1109 		phys = dma_map_single(lp->dev,
1110 				      skb_frag_address(frag),
1111 				      skb_frag_size(frag),
1112 				      DMA_TO_DEVICE);
1113 		if (unlikely(dma_mapping_error(lp->dev, phys))) {
1114 			if (net_ratelimit())
1115 				netdev_err(ndev, "TX DMA mapping error\n");
1116 			ndev->stats.tx_dropped++;
1117 			axienet_free_tx_chain(lp, orig_tail_ptr, ii + 1,
1118 					      true, NULL, 0);
1119 			dev_kfree_skb_any(skb);
1120 			return NETDEV_TX_OK;
1121 		}
1122 		desc_set_phys_addr(lp, phys, cur_p);
1123 		cur_p->cntrl = skb_frag_size(frag);
1124 	}
1125 
1126 	cur_p->cntrl |= XAXIDMA_BD_CTRL_TXEOF_MASK;
1127 	cur_p->skb = skb;
1128 
1129 	tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * new_tail_ptr;
1130 	if (++new_tail_ptr >= lp->tx_bd_num)
1131 		new_tail_ptr = 0;
1132 	WRITE_ONCE(lp->tx_bd_tail, new_tail_ptr);
1133 	netdev_sent_queue(ndev, skb->len);
1134 
1135 	/* Start the transfer */
1136 	axienet_dma_out_addr(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
1137 
1138 	/* Stop queue if next transmit may not have space */
1139 	if (axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1)) {
1140 		netif_stop_queue(ndev);
1141 
1142 		/* Matches barrier in axienet_tx_poll */
1143 		smp_mb();
1144 
1145 		/* Space might have just been freed - check again */
1146 		if (!axienet_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1))
1147 			netif_wake_queue(ndev);
1148 	}
1149 
1150 	return NETDEV_TX_OK;
1151 }
1152 
1153 /**
1154  * axienet_dma_rx_cb - DMA engine callback for RX channel.
1155  * @data:       Pointer to the skbuf_dma_descriptor structure.
1156  * @result:     error reporting through dmaengine_result.
1157  * This function is called by dmaengine driver for RX channel to notify
1158  * that the packet is received.
1159  */
axienet_dma_rx_cb(void * data,const struct dmaengine_result * result)1160 static void axienet_dma_rx_cb(void *data, const struct dmaengine_result *result)
1161 {
1162 	struct skbuf_dma_descriptor *skbuf_dma;
1163 	size_t meta_len, meta_max_len, rx_len;
1164 	struct axienet_local *lp = data;
1165 	struct sk_buff *skb;
1166 	u32 *app_metadata;
1167 	int i;
1168 
1169 	skbuf_dma = axienet_get_rx_desc(lp, lp->rx_ring_tail++);
1170 	skb = skbuf_dma->skb;
1171 	app_metadata = dmaengine_desc_get_metadata_ptr(skbuf_dma->desc, &meta_len,
1172 						       &meta_max_len);
1173 	dma_unmap_single(lp->dev, skbuf_dma->dma_address, lp->max_frm_size,
1174 			 DMA_FROM_DEVICE);
1175 	skbuf_dma->skb = NULL;
1176 
1177 	if (IS_ERR(app_metadata)) {
1178 		if (net_ratelimit())
1179 			netdev_err(lp->ndev, "Failed to get RX metadata pointer\n");
1180 		dev_kfree_skb_any(skb);
1181 		lp->ndev->stats.rx_dropped++;
1182 		goto rx_submit;
1183 	}
1184 
1185 	/* TODO: Derive app word index programmatically */
1186 	rx_len = (app_metadata[LEN_APP] & 0xFFFF);
1187 	skb_put(skb, rx_len);
1188 	skb->protocol = eth_type_trans(skb, lp->ndev);
1189 	skb->ip_summed = CHECKSUM_NONE;
1190 
1191 	__netif_rx(skb);
1192 	u64_stats_update_begin(&lp->rx_stat_sync);
1193 	u64_stats_add(&lp->rx_packets, 1);
1194 	u64_stats_add(&lp->rx_bytes, rx_len);
1195 	u64_stats_update_end(&lp->rx_stat_sync);
1196 
1197 rx_submit:
1198 	spin_lock(&lp->rx_submit_lock);
1199 	if (lp->stopping) {
1200 		spin_unlock(&lp->rx_submit_lock);
1201 		return;
1202 	}
1203 
1204 	for (i = 0; i < CIRC_SPACE(lp->rx_ring_head, lp->rx_ring_tail,
1205 				   RX_BUF_NUM_DEFAULT); i++)
1206 		axienet_rx_submit_desc(lp->ndev);
1207 	dma_async_issue_pending(lp->rx_chan);
1208 	spin_unlock(&lp->rx_submit_lock);
1209 }
1210 
1211 /**
1212  * axienet_rx_poll - Triggered by RX ISR to complete the BD processing.
1213  * @napi:	Pointer to NAPI structure.
1214  * @budget:	Max number of RX packets to process.
1215  *
1216  * Return: Number of RX packets processed.
1217  */
axienet_rx_poll(struct napi_struct * napi,int budget)1218 static int axienet_rx_poll(struct napi_struct *napi, int budget)
1219 {
1220 	u32 length;
1221 	u32 csumstatus;
1222 	u32 size = 0;
1223 	int packets = 0;
1224 	dma_addr_t tail_p = 0;
1225 	struct axidma_bd *cur_p;
1226 	struct sk_buff *skb, *new_skb;
1227 	struct axienet_local *lp = container_of(napi, struct axienet_local, napi_rx);
1228 
1229 	cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
1230 
1231 	while (packets < budget && (cur_p->status & XAXIDMA_BD_STS_COMPLETE_MASK)) {
1232 		dma_addr_t phys;
1233 
1234 		/* Ensure we see complete descriptor update */
1235 		dma_rmb();
1236 
1237 		skb = cur_p->skb;
1238 		cur_p->skb = NULL;
1239 
1240 		/* skb could be NULL if a previous pass already received the
1241 		 * packet for this slot in the ring, but failed to refill it
1242 		 * with a newly allocated buffer. In this case, don't try to
1243 		 * receive it again.
1244 		 */
1245 		if (likely(skb)) {
1246 			length = cur_p->app4 & 0x0000FFFF;
1247 
1248 			phys = desc_get_phys_addr(lp, cur_p);
1249 			dma_unmap_single(lp->dev, phys, lp->max_frm_size,
1250 					 DMA_FROM_DEVICE);
1251 
1252 			skb_put(skb, length);
1253 			skb->protocol = eth_type_trans(skb, lp->ndev);
1254 			/*skb_checksum_none_assert(skb);*/
1255 			skb->ip_summed = CHECKSUM_NONE;
1256 
1257 			/* if we're doing Rx csum offload, set it up */
1258 			if (lp->features & XAE_FEATURE_FULL_RX_CSUM) {
1259 				csumstatus = (cur_p->app2 &
1260 					      XAE_FULL_CSUM_STATUS_MASK) >> 3;
1261 				if (csumstatus == XAE_IP_TCP_CSUM_VALIDATED ||
1262 				    csumstatus == XAE_IP_UDP_CSUM_VALIDATED) {
1263 					skb->ip_summed = CHECKSUM_UNNECESSARY;
1264 				}
1265 			} else if (lp->features & XAE_FEATURE_PARTIAL_RX_CSUM) {
1266 				skb->csum = be32_to_cpu(cur_p->app3 & 0xFFFF);
1267 				skb->ip_summed = CHECKSUM_COMPLETE;
1268 			}
1269 
1270 			napi_gro_receive(napi, skb);
1271 
1272 			size += length;
1273 			packets++;
1274 		}
1275 
1276 		new_skb = napi_alloc_skb(napi, lp->max_frm_size);
1277 		if (!new_skb)
1278 			break;
1279 
1280 		phys = dma_map_single(lp->dev, new_skb->data,
1281 				      lp->max_frm_size,
1282 				      DMA_FROM_DEVICE);
1283 		if (unlikely(dma_mapping_error(lp->dev, phys))) {
1284 			if (net_ratelimit())
1285 				netdev_err(lp->ndev, "RX DMA mapping error\n");
1286 			dev_kfree_skb(new_skb);
1287 			break;
1288 		}
1289 		desc_set_phys_addr(lp, phys, cur_p);
1290 
1291 		cur_p->cntrl = lp->max_frm_size;
1292 		cur_p->status = 0;
1293 		cur_p->skb = new_skb;
1294 
1295 		/* Only update tail_p to mark this slot as usable after it has
1296 		 * been successfully refilled.
1297 		 */
1298 		tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
1299 
1300 		if (++lp->rx_bd_ci >= lp->rx_bd_num)
1301 			lp->rx_bd_ci = 0;
1302 		cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
1303 	}
1304 
1305 	u64_stats_update_begin(&lp->rx_stat_sync);
1306 	u64_stats_add(&lp->rx_packets, packets);
1307 	u64_stats_add(&lp->rx_bytes, size);
1308 	u64_stats_update_end(&lp->rx_stat_sync);
1309 
1310 	if (tail_p)
1311 		axienet_dma_out_addr(lp, XAXIDMA_RX_TDESC_OFFSET, tail_p);
1312 
1313 	if (packets < budget && napi_complete_done(napi, packets)) {
1314 		if (READ_ONCE(lp->rx_dim_enabled)) {
1315 			struct dim_sample sample = {
1316 				.time = ktime_get(),
1317 				/* Safe because we are the only writer */
1318 				.pkt_ctr = u64_stats_read(&lp->rx_packets),
1319 				.byte_ctr = u64_stats_read(&lp->rx_bytes),
1320 				.event_ctr = READ_ONCE(lp->rx_irqs),
1321 			};
1322 
1323 			net_dim(&lp->rx_dim, &sample);
1324 		}
1325 
1326 		/* Re-enable RX completion interrupts. This should
1327 		 * cause an immediate interrupt if any RX packets are
1328 		 * already pending.
1329 		 */
1330 		spin_lock_irq(&lp->rx_cr_lock);
1331 		axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, lp->rx_dma_cr);
1332 		spin_unlock_irq(&lp->rx_cr_lock);
1333 	}
1334 	return packets;
1335 }
1336 
1337 /**
1338  * axienet_tx_irq - Tx Done Isr.
1339  * @irq:	irq number
1340  * @_ndev:	net_device pointer
1341  *
1342  * Return: IRQ_HANDLED if device generated a TX interrupt, IRQ_NONE otherwise.
1343  *
1344  * This is the Axi DMA Tx done Isr. It invokes NAPI polling to complete the
1345  * TX BD processing.
1346  */
axienet_tx_irq(int irq,void * _ndev)1347 static irqreturn_t axienet_tx_irq(int irq, void *_ndev)
1348 {
1349 	unsigned int status;
1350 	struct net_device *ndev = _ndev;
1351 	struct axienet_local *lp = netdev_priv(ndev);
1352 
1353 	status = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
1354 
1355 	if (!(status & XAXIDMA_IRQ_ALL_MASK))
1356 		return IRQ_NONE;
1357 
1358 	axienet_dma_out32(lp, XAXIDMA_TX_SR_OFFSET, status);
1359 
1360 	if (unlikely(status & XAXIDMA_IRQ_ERROR_MASK)) {
1361 		netdev_err(ndev, "DMA Tx error 0x%x\n", status);
1362 		netdev_err(ndev, "Current BD is at: 0x%x%08x\n",
1363 			   (lp->tx_bd_v[lp->tx_bd_ci]).phys_msb,
1364 			   (lp->tx_bd_v[lp->tx_bd_ci]).phys);
1365 		schedule_work(&lp->dma_err_task);
1366 	} else {
1367 		/* Disable further TX completion interrupts and schedule
1368 		 * NAPI to handle the completions.
1369 		 */
1370 		if (napi_schedule_prep(&lp->napi_tx)) {
1371 			u32 cr;
1372 
1373 			spin_lock(&lp->tx_cr_lock);
1374 			cr = lp->tx_dma_cr;
1375 			cr &= ~(XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
1376 			axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
1377 			spin_unlock(&lp->tx_cr_lock);
1378 			__napi_schedule(&lp->napi_tx);
1379 		}
1380 	}
1381 
1382 	return IRQ_HANDLED;
1383 }
1384 
1385 /**
1386  * axienet_rx_irq - Rx Isr.
1387  * @irq:	irq number
1388  * @_ndev:	net_device pointer
1389  *
1390  * Return: IRQ_HANDLED if device generated a RX interrupt, IRQ_NONE otherwise.
1391  *
1392  * This is the Axi DMA Rx Isr. It invokes NAPI polling to complete the RX BD
1393  * processing.
1394  */
axienet_rx_irq(int irq,void * _ndev)1395 static irqreturn_t axienet_rx_irq(int irq, void *_ndev)
1396 {
1397 	unsigned int status;
1398 	struct net_device *ndev = _ndev;
1399 	struct axienet_local *lp = netdev_priv(ndev);
1400 
1401 	status = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
1402 
1403 	if (!(status & XAXIDMA_IRQ_ALL_MASK))
1404 		return IRQ_NONE;
1405 
1406 	axienet_dma_out32(lp, XAXIDMA_RX_SR_OFFSET, status);
1407 
1408 	if (unlikely(status & XAXIDMA_IRQ_ERROR_MASK)) {
1409 		netdev_err(ndev, "DMA Rx error 0x%x\n", status);
1410 		netdev_err(ndev, "Current BD is at: 0x%x%08x\n",
1411 			   (lp->rx_bd_v[lp->rx_bd_ci]).phys_msb,
1412 			   (lp->rx_bd_v[lp->rx_bd_ci]).phys);
1413 		schedule_work(&lp->dma_err_task);
1414 	} else {
1415 		/* Disable further RX completion interrupts and schedule
1416 		 * NAPI receive.
1417 		 */
1418 		WRITE_ONCE(lp->rx_irqs, READ_ONCE(lp->rx_irqs) + 1);
1419 		if (napi_schedule_prep(&lp->napi_rx)) {
1420 			u32 cr;
1421 
1422 			spin_lock(&lp->rx_cr_lock);
1423 			cr = lp->rx_dma_cr;
1424 			cr &= ~(XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK);
1425 			axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
1426 			spin_unlock(&lp->rx_cr_lock);
1427 
1428 			__napi_schedule(&lp->napi_rx);
1429 		}
1430 	}
1431 
1432 	return IRQ_HANDLED;
1433 }
1434 
1435 /**
1436  * axienet_eth_irq - Ethernet core Isr.
1437  * @irq:	irq number
1438  * @_ndev:	net_device pointer
1439  *
1440  * Return: IRQ_HANDLED if device generated a core interrupt, IRQ_NONE otherwise.
1441  *
1442  * Handle miscellaneous conditions indicated by Ethernet core IRQ.
1443  */
axienet_eth_irq(int irq,void * _ndev)1444 static irqreturn_t axienet_eth_irq(int irq, void *_ndev)
1445 {
1446 	struct net_device *ndev = _ndev;
1447 	struct axienet_local *lp = netdev_priv(ndev);
1448 	unsigned int pending;
1449 
1450 	pending = axienet_ior(lp, XAE_IP_OFFSET);
1451 	if (!pending)
1452 		return IRQ_NONE;
1453 
1454 	if (pending & XAE_INT_RXFIFOOVR_MASK)
1455 		ndev->stats.rx_missed_errors++;
1456 
1457 	if (pending & XAE_INT_RXRJECT_MASK)
1458 		ndev->stats.rx_dropped++;
1459 
1460 	axienet_iow(lp, XAE_IS_OFFSET, pending);
1461 	return IRQ_HANDLED;
1462 }
1463 
1464 static void axienet_dma_err_handler(struct work_struct *work);
1465 
1466 /**
1467  * axienet_rx_submit_desc - Submit the rx descriptors to dmaengine.
1468  * allocate skbuff, map the scatterlist and obtain a descriptor
1469  * and then add the callback information and submit descriptor.
1470  *
1471  * @ndev:	net_device pointer
1472  *
1473  */
axienet_rx_submit_desc(struct net_device * ndev)1474 static void axienet_rx_submit_desc(struct net_device *ndev)
1475 {
1476 	struct dma_async_tx_descriptor *dma_rx_desc = NULL;
1477 	struct axienet_local *lp = netdev_priv(ndev);
1478 	struct skbuf_dma_descriptor *skbuf_dma;
1479 	struct sk_buff *skb;
1480 	dma_addr_t addr;
1481 
1482 	skbuf_dma = axienet_get_rx_desc(lp, lp->rx_ring_head);
1483 	if (!skbuf_dma)
1484 		return;
1485 
1486 	skb = netdev_alloc_skb(ndev, lp->max_frm_size);
1487 	if (!skb)
1488 		return;
1489 
1490 	sg_init_table(skbuf_dma->sgl, 1);
1491 	addr = dma_map_single(lp->dev, skb->data, lp->max_frm_size, DMA_FROM_DEVICE);
1492 	if (unlikely(dma_mapping_error(lp->dev, addr))) {
1493 		if (net_ratelimit())
1494 			netdev_err(ndev, "DMA mapping error\n");
1495 		goto rx_submit_err_free_skb;
1496 	}
1497 	sg_dma_address(skbuf_dma->sgl) = addr;
1498 	sg_dma_len(skbuf_dma->sgl) = lp->max_frm_size;
1499 	dma_rx_desc = dmaengine_prep_slave_sg(lp->rx_chan, skbuf_dma->sgl,
1500 					      1, DMA_DEV_TO_MEM,
1501 					      DMA_PREP_INTERRUPT);
1502 	if (!dma_rx_desc)
1503 		goto rx_submit_err_unmap_skb;
1504 
1505 	skbuf_dma->skb = skb;
1506 	skbuf_dma->dma_address = sg_dma_address(skbuf_dma->sgl);
1507 	skbuf_dma->desc = dma_rx_desc;
1508 	dma_rx_desc->callback_param = lp;
1509 	dma_rx_desc->callback_result = axienet_dma_rx_cb;
1510 	lp->rx_ring_head++;
1511 	dmaengine_submit(dma_rx_desc);
1512 
1513 	return;
1514 
1515 rx_submit_err_unmap_skb:
1516 	dma_unmap_single(lp->dev, addr, lp->max_frm_size, DMA_FROM_DEVICE);
1517 rx_submit_err_free_skb:
1518 	dev_kfree_skb(skb);
1519 }
1520 
1521 /**
1522  * axienet_init_dmaengine - init the dmaengine code.
1523  * @ndev:       Pointer to net_device structure
1524  *
1525  * Return: 0, on success.
1526  *          non-zero error value on failure
1527  *
1528  * This is the dmaengine initialization code.
1529  */
axienet_init_dmaengine(struct net_device * ndev)1530 static int axienet_init_dmaengine(struct net_device *ndev)
1531 {
1532 	struct axienet_local *lp = netdev_priv(ndev);
1533 	struct skbuf_dma_descriptor *skbuf_dma;
1534 	int i, ret;
1535 
1536 	lp->tx_chan = dma_request_chan(lp->dev, "tx_chan0");
1537 	if (IS_ERR(lp->tx_chan)) {
1538 		dev_err(lp->dev, "No Ethernet DMA (TX) channel found\n");
1539 		return PTR_ERR(lp->tx_chan);
1540 	}
1541 
1542 	lp->rx_chan = dma_request_chan(lp->dev, "rx_chan0");
1543 	if (IS_ERR(lp->rx_chan)) {
1544 		ret = PTR_ERR(lp->rx_chan);
1545 		dev_err(lp->dev, "No Ethernet DMA (RX) channel found\n");
1546 		goto err_dma_release_tx;
1547 	}
1548 
1549 	lp->tx_ring_tail = 0;
1550 	lp->tx_ring_head = 0;
1551 	lp->rx_ring_tail = 0;
1552 	lp->rx_ring_head = 0;
1553 	lp->stopping = false;
1554 	lp->tx_skb_ring = kzalloc_objs(*lp->tx_skb_ring, TX_BD_NUM_MAX);
1555 	if (!lp->tx_skb_ring) {
1556 		ret = -ENOMEM;
1557 		goto err_dma_release_rx;
1558 	}
1559 	for (i = 0; i < TX_BD_NUM_MAX; i++) {
1560 		skbuf_dma = kzalloc_obj(*skbuf_dma);
1561 		if (!skbuf_dma) {
1562 			ret = -ENOMEM;
1563 			goto err_free_tx_skb_ring;
1564 		}
1565 		lp->tx_skb_ring[i] = skbuf_dma;
1566 	}
1567 
1568 	lp->rx_skb_ring = kzalloc_objs(*lp->rx_skb_ring, RX_BUF_NUM_DEFAULT);
1569 	if (!lp->rx_skb_ring) {
1570 		ret = -ENOMEM;
1571 		goto err_free_tx_skb_ring;
1572 	}
1573 	for (i = 0; i < RX_BUF_NUM_DEFAULT; i++) {
1574 		skbuf_dma = kzalloc_obj(*skbuf_dma);
1575 		if (!skbuf_dma) {
1576 			ret = -ENOMEM;
1577 			goto err_free_rx_skb_ring;
1578 		}
1579 		lp->rx_skb_ring[i] = skbuf_dma;
1580 	}
1581 	/* TODO: Instead of BD_NUM_DEFAULT use runtime support */
1582 	for (i = 0; i < RX_BUF_NUM_DEFAULT; i++)
1583 		axienet_rx_submit_desc(ndev);
1584 	dma_async_issue_pending(lp->rx_chan);
1585 
1586 	return 0;
1587 
1588 err_free_rx_skb_ring:
1589 	for (i = 0; i < RX_BUF_NUM_DEFAULT; i++)
1590 		kfree(lp->rx_skb_ring[i]);
1591 	kfree(lp->rx_skb_ring);
1592 err_free_tx_skb_ring:
1593 	for (i = 0; i < TX_BD_NUM_MAX; i++)
1594 		kfree(lp->tx_skb_ring[i]);
1595 	kfree(lp->tx_skb_ring);
1596 err_dma_release_rx:
1597 	dma_release_channel(lp->rx_chan);
1598 err_dma_release_tx:
1599 	dma_release_channel(lp->tx_chan);
1600 	return ret;
1601 }
1602 
1603 /**
1604  * axienet_init_legacy_dma - init the dma legacy code.
1605  * @ndev:       Pointer to net_device structure
1606  *
1607  * Return: 0, on success.
1608  *          non-zero error value on failure
1609  *
1610  * This is the dma  initialization code. It also allocates interrupt
1611  * service routines, enables the interrupt lines and ISR handling.
1612  *
1613  */
axienet_init_legacy_dma(struct net_device * ndev)1614 static int axienet_init_legacy_dma(struct net_device *ndev)
1615 {
1616 	int ret;
1617 	struct axienet_local *lp = netdev_priv(ndev);
1618 
1619 	/* Enable worker thread for Axi DMA error handling */
1620 	lp->stopping = false;
1621 	INIT_WORK(&lp->dma_err_task, axienet_dma_err_handler);
1622 
1623 	napi_enable(&lp->napi_rx);
1624 	napi_enable(&lp->napi_tx);
1625 
1626 	/* Enable interrupts for Axi DMA Tx */
1627 	ret = request_irq(lp->tx_irq, axienet_tx_irq, IRQF_SHARED,
1628 			  ndev->name, ndev);
1629 	if (ret)
1630 		goto err_tx_irq;
1631 	/* Enable interrupts for Axi DMA Rx */
1632 	ret = request_irq(lp->rx_irq, axienet_rx_irq, IRQF_SHARED,
1633 			  ndev->name, ndev);
1634 	if (ret)
1635 		goto err_rx_irq;
1636 	/* Enable interrupts for Axi Ethernet core (if defined) */
1637 	if (lp->eth_irq > 0) {
1638 		ret = request_irq(lp->eth_irq, axienet_eth_irq, IRQF_SHARED,
1639 				  ndev->name, ndev);
1640 		if (ret)
1641 			goto err_eth_irq;
1642 	}
1643 
1644 	return 0;
1645 
1646 err_eth_irq:
1647 	free_irq(lp->rx_irq, ndev);
1648 err_rx_irq:
1649 	free_irq(lp->tx_irq, ndev);
1650 err_tx_irq:
1651 	napi_disable(&lp->napi_tx);
1652 	napi_disable(&lp->napi_rx);
1653 	cancel_work_sync(&lp->dma_err_task);
1654 	dev_err(lp->dev, "request_irq() failed\n");
1655 	return ret;
1656 }
1657 
1658 /**
1659  * axienet_open - Driver open routine.
1660  * @ndev:	Pointer to net_device structure
1661  *
1662  * Return: 0, on success.
1663  *	    non-zero error value on failure
1664  *
1665  * This is the driver open routine. It calls phylink_start to start the
1666  * PHY device.
1667  * It also allocates interrupt service routines, enables the interrupt lines
1668  * and ISR handling. Axi Ethernet core is reset through Axi DMA core. Buffer
1669  * descriptors are initialized.
1670  */
axienet_open(struct net_device * ndev)1671 static int axienet_open(struct net_device *ndev)
1672 {
1673 	int ret;
1674 	struct axienet_local *lp = netdev_priv(ndev);
1675 
1676 	/* When we do an Axi Ethernet reset, it resets the complete core
1677 	 * including the MDIO. MDIO must be disabled before resetting.
1678 	 * Hold MDIO bus lock to avoid MDIO accesses during the reset.
1679 	 */
1680 	axienet_lock_mii(lp);
1681 	ret = axienet_device_reset(ndev);
1682 	axienet_unlock_mii(lp);
1683 
1684 	ret = phylink_of_phy_connect(lp->phylink, lp->dev->of_node, 0);
1685 	if (ret) {
1686 		dev_err(lp->dev, "phylink_of_phy_connect() failed: %d\n", ret);
1687 		return ret;
1688 	}
1689 
1690 	phylink_start(lp->phylink);
1691 
1692 	/* Start the statistics refresh work */
1693 	schedule_delayed_work(&lp->stats_work, 0);
1694 
1695 	if (lp->use_dmaengine) {
1696 		/* Enable interrupts for Axi Ethernet core (if defined) */
1697 		if (lp->eth_irq > 0) {
1698 			ret = request_irq(lp->eth_irq, axienet_eth_irq, IRQF_SHARED,
1699 					  ndev->name, ndev);
1700 			if (ret)
1701 				goto err_phy;
1702 		}
1703 
1704 		ret = axienet_init_dmaengine(ndev);
1705 		if (ret < 0)
1706 			goto err_free_eth_irq;
1707 	} else {
1708 		ret = axienet_init_legacy_dma(ndev);
1709 		if (ret)
1710 			goto err_phy;
1711 	}
1712 
1713 	return 0;
1714 
1715 err_free_eth_irq:
1716 	if (lp->eth_irq > 0)
1717 		free_irq(lp->eth_irq, ndev);
1718 err_phy:
1719 	cancel_work_sync(&lp->rx_dim.work);
1720 	cancel_delayed_work_sync(&lp->stats_work);
1721 	phylink_stop(lp->phylink);
1722 	phylink_disconnect_phy(lp->phylink);
1723 	return ret;
1724 }
1725 
1726 /**
1727  * axienet_stop - Driver stop routine.
1728  * @ndev:	Pointer to net_device structure
1729  *
1730  * Return: 0, on success.
1731  *
1732  * This is the driver stop routine. It calls phylink_disconnect to stop the PHY
1733  * device. It also removes the interrupt handlers and disables the interrupts.
1734  * The Axi DMA Tx/Rx BDs are released.
1735  */
axienet_stop(struct net_device * ndev)1736 static int axienet_stop(struct net_device *ndev)
1737 {
1738 	struct axienet_local *lp = netdev_priv(ndev);
1739 	int i;
1740 
1741 	if (!lp->use_dmaengine) {
1742 		WRITE_ONCE(lp->stopping, true);
1743 		flush_work(&lp->dma_err_task);
1744 
1745 		napi_disable(&lp->napi_tx);
1746 		napi_disable(&lp->napi_rx);
1747 	}
1748 
1749 	cancel_work_sync(&lp->rx_dim.work);
1750 	cancel_delayed_work_sync(&lp->stats_work);
1751 
1752 	phylink_stop(lp->phylink);
1753 	phylink_disconnect_phy(lp->phylink);
1754 
1755 	axienet_setoptions(ndev, lp->options &
1756 			   ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
1757 
1758 	if (!lp->use_dmaengine) {
1759 		axienet_dma_stop(lp);
1760 		cancel_work_sync(&lp->dma_err_task);
1761 		free_irq(lp->tx_irq, ndev);
1762 		free_irq(lp->rx_irq, ndev);
1763 		axienet_dma_bd_release(ndev);
1764 	} else {
1765 		struct skbuf_dma_descriptor *skbuf_dma;
1766 
1767 		spin_lock_bh(&lp->rx_submit_lock);
1768 		lp->stopping = true;
1769 		spin_unlock_bh(&lp->rx_submit_lock);
1770 
1771 		dmaengine_terminate_sync(lp->tx_chan);
1772 		dmaengine_terminate_sync(lp->rx_chan);
1773 
1774 		dma_release_channel(lp->rx_chan);
1775 		dma_release_channel(lp->tx_chan);
1776 
1777 		/* Unmap and free any buffer the terminate did not reclaim, so it
1778 		 * is not leaked; a non-NULL skb marks such a slot.
1779 		 */
1780 		for (i = 0; i < TX_BD_NUM_MAX; i++) {
1781 			skbuf_dma = lp->tx_skb_ring[i];
1782 			if (skbuf_dma && skbuf_dma->skb) {
1783 				dma_unmap_sg(lp->dev, skbuf_dma->sgl,
1784 					     skbuf_dma->sg_len, DMA_TO_DEVICE);
1785 				dev_kfree_skb_any(skbuf_dma->skb);
1786 			}
1787 			kfree(skbuf_dma);
1788 		}
1789 		kfree(lp->tx_skb_ring);
1790 
1791 		for (i = 0; i < RX_BUF_NUM_DEFAULT; i++) {
1792 			skbuf_dma = lp->rx_skb_ring[i];
1793 			if (skbuf_dma && skbuf_dma->skb) {
1794 				dma_unmap_single(lp->dev, skbuf_dma->dma_address,
1795 						 lp->max_frm_size, DMA_FROM_DEVICE);
1796 				dev_kfree_skb_any(skbuf_dma->skb);
1797 			}
1798 			kfree(skbuf_dma);
1799 		}
1800 		kfree(lp->rx_skb_ring);
1801 	}
1802 
1803 	netdev_reset_queue(ndev);
1804 	axienet_iow(lp, XAE_IE_OFFSET, 0);
1805 
1806 	if (lp->eth_irq > 0)
1807 		free_irq(lp->eth_irq, ndev);
1808 	return 0;
1809 }
1810 
1811 /**
1812  * axienet_change_mtu - Driver change mtu routine.
1813  * @ndev:	Pointer to net_device structure
1814  * @new_mtu:	New mtu value to be applied
1815  *
1816  * Return: Always returns 0 (success).
1817  *
1818  * This is the change mtu driver routine. It checks if the Axi Ethernet
1819  * hardware supports jumbo frames before changing the mtu. This can be
1820  * called only when the device is not up.
1821  */
axienet_change_mtu(struct net_device * ndev,int new_mtu)1822 static int axienet_change_mtu(struct net_device *ndev, int new_mtu)
1823 {
1824 	struct axienet_local *lp = netdev_priv(ndev);
1825 
1826 	if (netif_running(ndev))
1827 		return -EBUSY;
1828 
1829 	if ((new_mtu + VLAN_ETH_HLEN +
1830 		XAE_TRL_SIZE) > lp->rxmem)
1831 		return -EINVAL;
1832 
1833 	WRITE_ONCE(ndev->mtu, new_mtu);
1834 
1835 	return 0;
1836 }
1837 
1838 #ifdef CONFIG_NET_POLL_CONTROLLER
1839 /**
1840  * axienet_poll_controller - Axi Ethernet poll mechanism.
1841  * @ndev:	Pointer to net_device structure
1842  *
1843  * This implements Rx/Tx ISR poll mechanisms. The interrupts are disabled prior
1844  * to polling the ISRs and are enabled back after the polling is done.
1845  */
axienet_poll_controller(struct net_device * ndev)1846 static void axienet_poll_controller(struct net_device *ndev)
1847 {
1848 	struct axienet_local *lp = netdev_priv(ndev);
1849 
1850 	disable_irq(lp->tx_irq);
1851 	disable_irq(lp->rx_irq);
1852 	axienet_rx_irq(lp->tx_irq, ndev);
1853 	axienet_tx_irq(lp->rx_irq, ndev);
1854 	enable_irq(lp->tx_irq);
1855 	enable_irq(lp->rx_irq);
1856 }
1857 #endif
1858 
axienet_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)1859 static int axienet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1860 {
1861 	struct axienet_local *lp = netdev_priv(dev);
1862 
1863 	if (!netif_running(dev))
1864 		return -EINVAL;
1865 
1866 	return phylink_mii_ioctl(lp->phylink, rq, cmd);
1867 }
1868 
1869 static void
axienet_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)1870 axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1871 {
1872 	struct axienet_local *lp = netdev_priv(dev);
1873 	unsigned int start;
1874 
1875 	netdev_stats_to_stats64(stats, &dev->stats);
1876 
1877 	do {
1878 		start = u64_stats_fetch_begin(&lp->rx_stat_sync);
1879 		stats->rx_packets = u64_stats_read(&lp->rx_packets);
1880 		stats->rx_bytes = u64_stats_read(&lp->rx_bytes);
1881 	} while (u64_stats_fetch_retry(&lp->rx_stat_sync, start));
1882 
1883 	do {
1884 		start = u64_stats_fetch_begin(&lp->tx_stat_sync);
1885 		stats->tx_packets = u64_stats_read(&lp->tx_packets);
1886 		stats->tx_bytes = u64_stats_read(&lp->tx_bytes);
1887 	} while (u64_stats_fetch_retry(&lp->tx_stat_sync, start));
1888 
1889 	if (!(lp->features & XAE_FEATURE_STATS))
1890 		return;
1891 
1892 	do {
1893 		start = read_seqcount_begin(&lp->hw_stats_seqcount);
1894 		stats->rx_length_errors =
1895 			axienet_stat(lp, STAT_RX_LENGTH_ERRORS);
1896 		stats->rx_crc_errors = axienet_stat(lp, STAT_RX_FCS_ERRORS);
1897 		stats->rx_frame_errors =
1898 			axienet_stat(lp, STAT_RX_ALIGNMENT_ERRORS);
1899 		stats->rx_errors = axienet_stat(lp, STAT_UNDERSIZE_FRAMES) +
1900 				   axienet_stat(lp, STAT_FRAGMENT_FRAMES) +
1901 				   stats->rx_length_errors +
1902 				   stats->rx_crc_errors +
1903 				   stats->rx_frame_errors;
1904 		stats->multicast = axienet_stat(lp, STAT_RX_MULTICAST_FRAMES);
1905 
1906 		stats->tx_aborted_errors =
1907 			axienet_stat(lp, STAT_TX_EXCESS_COLLISIONS);
1908 		stats->tx_fifo_errors =
1909 			axienet_stat(lp, STAT_TX_UNDERRUN_ERRORS);
1910 		stats->tx_window_errors =
1911 			axienet_stat(lp, STAT_TX_LATE_COLLISIONS);
1912 		stats->tx_errors = axienet_stat(lp, STAT_TX_EXCESS_DEFERRAL) +
1913 				   stats->tx_aborted_errors +
1914 				   stats->tx_fifo_errors +
1915 				   stats->tx_window_errors;
1916 	} while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
1917 }
1918 
1919 static const struct net_device_ops axienet_netdev_ops = {
1920 	.ndo_open = axienet_open,
1921 	.ndo_stop = axienet_stop,
1922 	.ndo_start_xmit = axienet_start_xmit,
1923 	.ndo_get_stats64 = axienet_get_stats64,
1924 	.ndo_change_mtu	= axienet_change_mtu,
1925 	.ndo_set_mac_address = netdev_set_mac_address,
1926 	.ndo_validate_addr = eth_validate_addr,
1927 	.ndo_eth_ioctl = axienet_ioctl,
1928 	.ndo_set_rx_mode = axienet_set_multicast_list,
1929 #ifdef CONFIG_NET_POLL_CONTROLLER
1930 	.ndo_poll_controller = axienet_poll_controller,
1931 #endif
1932 };
1933 
1934 static const struct net_device_ops axienet_netdev_dmaengine_ops = {
1935 	.ndo_open = axienet_open,
1936 	.ndo_stop = axienet_stop,
1937 	.ndo_start_xmit = axienet_start_xmit_dmaengine,
1938 	.ndo_get_stats64 = axienet_get_stats64,
1939 	.ndo_change_mtu	= axienet_change_mtu,
1940 	.ndo_set_mac_address = netdev_set_mac_address,
1941 	.ndo_validate_addr = eth_validate_addr,
1942 	.ndo_eth_ioctl = axienet_ioctl,
1943 	.ndo_set_rx_mode = axienet_set_multicast_list,
1944 };
1945 
1946 /**
1947  * axienet_ethtools_get_drvinfo - Get various Axi Ethernet driver information.
1948  * @ndev:	Pointer to net_device structure
1949  * @ed:		Pointer to ethtool_drvinfo structure
1950  *
1951  * This implements ethtool command for getting the driver information.
1952  * Issue "ethtool -i ethX" under linux prompt to execute this function.
1953  */
axienet_ethtools_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * ed)1954 static void axienet_ethtools_get_drvinfo(struct net_device *ndev,
1955 					 struct ethtool_drvinfo *ed)
1956 {
1957 	strscpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
1958 	strscpy(ed->version, DRIVER_VERSION, sizeof(ed->version));
1959 }
1960 
1961 /**
1962  * axienet_ethtools_get_regs_len - Get the total regs length present in the
1963  *				   AxiEthernet core.
1964  * @ndev:	Pointer to net_device structure
1965  *
1966  * This implements ethtool command for getting the total register length
1967  * information.
1968  *
1969  * Return: the total regs length
1970  */
axienet_ethtools_get_regs_len(struct net_device * ndev)1971 static int axienet_ethtools_get_regs_len(struct net_device *ndev)
1972 {
1973 	return sizeof(u32) * AXIENET_REGS_N;
1974 }
1975 
1976 /**
1977  * axienet_ethtools_get_regs - Dump the contents of all registers present
1978  *			       in AxiEthernet core.
1979  * @ndev:	Pointer to net_device structure
1980  * @regs:	Pointer to ethtool_regs structure
1981  * @ret:	Void pointer used to return the contents of the registers.
1982  *
1983  * This implements ethtool command for getting the Axi Ethernet register dump.
1984  * Issue "ethtool -d ethX" to execute this function.
1985  */
axienet_ethtools_get_regs(struct net_device * ndev,struct ethtool_regs * regs,void * ret)1986 static void axienet_ethtools_get_regs(struct net_device *ndev,
1987 				      struct ethtool_regs *regs, void *ret)
1988 {
1989 	u32 *data = (u32 *)ret;
1990 	size_t len = sizeof(u32) * AXIENET_REGS_N;
1991 	struct axienet_local *lp = netdev_priv(ndev);
1992 
1993 	regs->version = 0;
1994 	regs->len = len;
1995 
1996 	memset(data, 0, len);
1997 	data[0] = axienet_ior(lp, XAE_RAF_OFFSET);
1998 	data[1] = axienet_ior(lp, XAE_TPF_OFFSET);
1999 	data[2] = axienet_ior(lp, XAE_IFGP_OFFSET);
2000 	data[3] = axienet_ior(lp, XAE_IS_OFFSET);
2001 	data[4] = axienet_ior(lp, XAE_IP_OFFSET);
2002 	data[5] = axienet_ior(lp, XAE_IE_OFFSET);
2003 	data[6] = axienet_ior(lp, XAE_TTAG_OFFSET);
2004 	data[7] = axienet_ior(lp, XAE_RTAG_OFFSET);
2005 	data[8] = axienet_ior(lp, XAE_UAWL_OFFSET);
2006 	data[9] = axienet_ior(lp, XAE_UAWU_OFFSET);
2007 	data[10] = axienet_ior(lp, XAE_TPID0_OFFSET);
2008 	data[11] = axienet_ior(lp, XAE_TPID1_OFFSET);
2009 	data[12] = axienet_ior(lp, XAE_PPST_OFFSET);
2010 	data[13] = axienet_ior(lp, XAE_RCW0_OFFSET);
2011 	data[14] = axienet_ior(lp, XAE_RCW1_OFFSET);
2012 	data[15] = axienet_ior(lp, XAE_TC_OFFSET);
2013 	data[16] = axienet_ior(lp, XAE_FCC_OFFSET);
2014 	data[17] = axienet_ior(lp, XAE_EMMC_OFFSET);
2015 	data[18] = axienet_ior(lp, XAE_PHYC_OFFSET);
2016 	data[19] = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
2017 	data[20] = axienet_ior(lp, XAE_MDIO_MCR_OFFSET);
2018 	data[21] = axienet_ior(lp, XAE_MDIO_MWD_OFFSET);
2019 	data[22] = axienet_ior(lp, XAE_MDIO_MRD_OFFSET);
2020 	data[27] = axienet_ior(lp, XAE_UAW0_OFFSET);
2021 	data[28] = axienet_ior(lp, XAE_UAW1_OFFSET);
2022 	data[29] = axienet_ior(lp, XAE_FMI_OFFSET);
2023 	data[30] = axienet_ior(lp, XAE_AF0_OFFSET);
2024 	data[31] = axienet_ior(lp, XAE_AF1_OFFSET);
2025 	if (!lp->use_dmaengine) {
2026 		data[32] = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
2027 		data[33] = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
2028 		data[34] = axienet_dma_in32(lp, XAXIDMA_TX_CDESC_OFFSET);
2029 		data[35] = axienet_dma_in32(lp, XAXIDMA_TX_TDESC_OFFSET);
2030 		data[36] = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
2031 		data[37] = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
2032 		data[38] = axienet_dma_in32(lp, XAXIDMA_RX_CDESC_OFFSET);
2033 		data[39] = axienet_dma_in32(lp, XAXIDMA_RX_TDESC_OFFSET);
2034 	}
2035 }
2036 
2037 static void
axienet_ethtools_get_ringparam(struct net_device * ndev,struct ethtool_ringparam * ering,struct kernel_ethtool_ringparam * kernel_ering,struct netlink_ext_ack * extack)2038 axienet_ethtools_get_ringparam(struct net_device *ndev,
2039 			       struct ethtool_ringparam *ering,
2040 			       struct kernel_ethtool_ringparam *kernel_ering,
2041 			       struct netlink_ext_ack *extack)
2042 {
2043 	struct axienet_local *lp = netdev_priv(ndev);
2044 
2045 	ering->rx_max_pending = RX_BD_NUM_MAX;
2046 	ering->rx_mini_max_pending = 0;
2047 	ering->rx_jumbo_max_pending = 0;
2048 	ering->tx_max_pending = TX_BD_NUM_MAX;
2049 	ering->rx_pending = lp->rx_bd_num;
2050 	ering->rx_mini_pending = 0;
2051 	ering->rx_jumbo_pending = 0;
2052 	ering->tx_pending = lp->tx_bd_num;
2053 }
2054 
2055 static int
axienet_ethtools_set_ringparam(struct net_device * ndev,struct ethtool_ringparam * ering,struct kernel_ethtool_ringparam * kernel_ering,struct netlink_ext_ack * extack)2056 axienet_ethtools_set_ringparam(struct net_device *ndev,
2057 			       struct ethtool_ringparam *ering,
2058 			       struct kernel_ethtool_ringparam *kernel_ering,
2059 			       struct netlink_ext_ack *extack)
2060 {
2061 	struct axienet_local *lp = netdev_priv(ndev);
2062 
2063 	if (ering->rx_pending > RX_BD_NUM_MAX ||
2064 	    ering->rx_mini_pending ||
2065 	    ering->rx_jumbo_pending ||
2066 	    ering->tx_pending < TX_BD_NUM_MIN ||
2067 	    ering->tx_pending > TX_BD_NUM_MAX)
2068 		return -EINVAL;
2069 
2070 	if (netif_running(ndev))
2071 		return -EBUSY;
2072 
2073 	lp->rx_bd_num = ering->rx_pending;
2074 	lp->tx_bd_num = ering->tx_pending;
2075 	return 0;
2076 }
2077 
2078 /**
2079  * axienet_ethtools_get_pauseparam - Get the pause parameter setting for
2080  *				     Tx and Rx paths.
2081  * @ndev:	Pointer to net_device structure
2082  * @epauseparm:	Pointer to ethtool_pauseparam structure.
2083  *
2084  * This implements ethtool command for getting axi ethernet pause frame
2085  * setting. Issue "ethtool -a ethX" to execute this function.
2086  */
2087 static void
axienet_ethtools_get_pauseparam(struct net_device * ndev,struct ethtool_pauseparam * epauseparm)2088 axienet_ethtools_get_pauseparam(struct net_device *ndev,
2089 				struct ethtool_pauseparam *epauseparm)
2090 {
2091 	struct axienet_local *lp = netdev_priv(ndev);
2092 
2093 	phylink_ethtool_get_pauseparam(lp->phylink, epauseparm);
2094 }
2095 
2096 /**
2097  * axienet_ethtools_set_pauseparam - Set device pause parameter(flow control)
2098  *				     settings.
2099  * @ndev:	Pointer to net_device structure
2100  * @epauseparm:Pointer to ethtool_pauseparam structure
2101  *
2102  * This implements ethtool command for enabling flow control on Rx and Tx
2103  * paths. Issue "ethtool -A ethX tx on|off" under linux prompt to execute this
2104  * function.
2105  *
2106  * Return: 0 on success, -EFAULT if device is running
2107  */
2108 static int
axienet_ethtools_set_pauseparam(struct net_device * ndev,struct ethtool_pauseparam * epauseparm)2109 axienet_ethtools_set_pauseparam(struct net_device *ndev,
2110 				struct ethtool_pauseparam *epauseparm)
2111 {
2112 	struct axienet_local *lp = netdev_priv(ndev);
2113 
2114 	return phylink_ethtool_set_pauseparam(lp->phylink, epauseparm);
2115 }
2116 
2117 /**
2118  * axienet_update_coalesce_rx() - Set RX CR
2119  * @lp: Device private data
2120  * @cr: Value to write to the RX CR
2121  * @mask: Bits to set from @cr
2122  */
axienet_update_coalesce_rx(struct axienet_local * lp,u32 cr,u32 mask)2123 static void axienet_update_coalesce_rx(struct axienet_local *lp, u32 cr,
2124 				       u32 mask)
2125 {
2126 	spin_lock_irq(&lp->rx_cr_lock);
2127 	lp->rx_dma_cr &= ~mask;
2128 	lp->rx_dma_cr |= cr;
2129 	/* If DMA isn't started, then the settings will be applied the next
2130 	 * time dma_start() is called.
2131 	 */
2132 	if (lp->rx_dma_started) {
2133 		u32 reg = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
2134 
2135 		/* Don't enable IRQs if they are disabled by NAPI */
2136 		if (reg & XAXIDMA_IRQ_ALL_MASK)
2137 			cr = lp->rx_dma_cr;
2138 		else
2139 			cr = lp->rx_dma_cr & ~XAXIDMA_IRQ_ALL_MASK;
2140 		axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
2141 	}
2142 	spin_unlock_irq(&lp->rx_cr_lock);
2143 }
2144 
2145 /**
2146  * axienet_dim_coalesce_count_rx() - RX coalesce count for DIM
2147  * @lp: Device private data
2148  *
2149  * Return: RX coalescing frame count value for DIM.
2150  */
axienet_dim_coalesce_count_rx(struct axienet_local * lp)2151 static u32 axienet_dim_coalesce_count_rx(struct axienet_local *lp)
2152 {
2153 	return min(1 << (lp->rx_dim.profile_ix << 1), 255);
2154 }
2155 
2156 /**
2157  * axienet_rx_dim_work() - Adjust RX DIM settings
2158  * @work: The work struct
2159  */
axienet_rx_dim_work(struct work_struct * work)2160 static void axienet_rx_dim_work(struct work_struct *work)
2161 {
2162 	struct axienet_local *lp =
2163 		container_of(work, struct axienet_local, rx_dim.work);
2164 	u32 cr = axienet_calc_cr(lp, axienet_dim_coalesce_count_rx(lp), 0);
2165 	u32 mask = XAXIDMA_COALESCE_MASK | XAXIDMA_IRQ_IOC_MASK |
2166 		   XAXIDMA_IRQ_ERROR_MASK;
2167 
2168 	axienet_update_coalesce_rx(lp, cr, mask);
2169 	lp->rx_dim.state = DIM_START_MEASURE;
2170 }
2171 
2172 /**
2173  * axienet_update_coalesce_tx() - Set TX CR
2174  * @lp: Device private data
2175  * @cr: Value to write to the TX CR
2176  * @mask: Bits to set from @cr
2177  */
axienet_update_coalesce_tx(struct axienet_local * lp,u32 cr,u32 mask)2178 static void axienet_update_coalesce_tx(struct axienet_local *lp, u32 cr,
2179 				       u32 mask)
2180 {
2181 	spin_lock_irq(&lp->tx_cr_lock);
2182 	lp->tx_dma_cr &= ~mask;
2183 	lp->tx_dma_cr |= cr;
2184 	/* If DMA isn't started, then the settings will be applied the next
2185 	 * time dma_start() is called.
2186 	 */
2187 	if (lp->tx_dma_started) {
2188 		u32 reg = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
2189 
2190 		/* Don't enable IRQs if they are disabled by NAPI */
2191 		if (reg & XAXIDMA_IRQ_ALL_MASK)
2192 			cr = lp->tx_dma_cr;
2193 		else
2194 			cr = lp->tx_dma_cr & ~XAXIDMA_IRQ_ALL_MASK;
2195 		axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
2196 	}
2197 	spin_unlock_irq(&lp->tx_cr_lock);
2198 }
2199 
2200 /**
2201  * axienet_ethtools_get_coalesce - Get DMA interrupt coalescing count.
2202  * @ndev:	Pointer to net_device structure
2203  * @ecoalesce:	Pointer to ethtool_coalesce structure
2204  * @kernel_coal: ethtool CQE mode setting structure
2205  * @extack:	extack for reporting error messages
2206  *
2207  * This implements ethtool command for getting the DMA interrupt coalescing
2208  * count on Tx and Rx paths. Issue "ethtool -c ethX" under linux prompt to
2209  * execute this function.
2210  *
2211  * Return: 0 always
2212  */
2213 static int
axienet_ethtools_get_coalesce(struct net_device * ndev,struct ethtool_coalesce * ecoalesce,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)2214 axienet_ethtools_get_coalesce(struct net_device *ndev,
2215 			      struct ethtool_coalesce *ecoalesce,
2216 			      struct kernel_ethtool_coalesce *kernel_coal,
2217 			      struct netlink_ext_ack *extack)
2218 {
2219 	struct axienet_local *lp = netdev_priv(ndev);
2220 	u32 cr;
2221 
2222 	ecoalesce->use_adaptive_rx_coalesce = lp->rx_dim_enabled;
2223 
2224 	spin_lock_irq(&lp->rx_cr_lock);
2225 	cr = lp->rx_dma_cr;
2226 	spin_unlock_irq(&lp->rx_cr_lock);
2227 	axienet_coalesce_params(lp, cr,
2228 				&ecoalesce->rx_max_coalesced_frames,
2229 				&ecoalesce->rx_coalesce_usecs);
2230 
2231 	spin_lock_irq(&lp->tx_cr_lock);
2232 	cr = lp->tx_dma_cr;
2233 	spin_unlock_irq(&lp->tx_cr_lock);
2234 	axienet_coalesce_params(lp, cr,
2235 				&ecoalesce->tx_max_coalesced_frames,
2236 				&ecoalesce->tx_coalesce_usecs);
2237 	return 0;
2238 }
2239 
2240 /**
2241  * axienet_ethtools_set_coalesce - Set DMA interrupt coalescing count.
2242  * @ndev:	Pointer to net_device structure
2243  * @ecoalesce:	Pointer to ethtool_coalesce structure
2244  * @kernel_coal: ethtool CQE mode setting structure
2245  * @extack:	extack for reporting error messages
2246  *
2247  * This implements ethtool command for setting the DMA interrupt coalescing
2248  * count on Tx and Rx paths. Issue "ethtool -C ethX rx-frames 5" under linux
2249  * prompt to execute this function.
2250  *
2251  * Return: 0, on success, Non-zero error value on failure.
2252  */
2253 static int
axienet_ethtools_set_coalesce(struct net_device * ndev,struct ethtool_coalesce * ecoalesce,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)2254 axienet_ethtools_set_coalesce(struct net_device *ndev,
2255 			      struct ethtool_coalesce *ecoalesce,
2256 			      struct kernel_ethtool_coalesce *kernel_coal,
2257 			      struct netlink_ext_ack *extack)
2258 {
2259 	struct axienet_local *lp = netdev_priv(ndev);
2260 	bool new_dim = ecoalesce->use_adaptive_rx_coalesce;
2261 	bool old_dim = lp->rx_dim_enabled;
2262 	u32 cr, mask = ~XAXIDMA_CR_RUNSTOP_MASK;
2263 
2264 	if (ecoalesce->rx_max_coalesced_frames > 255 ||
2265 	    ecoalesce->tx_max_coalesced_frames > 255) {
2266 		NL_SET_ERR_MSG(extack, "frames must be less than 256");
2267 		return -EINVAL;
2268 	}
2269 
2270 	if (!ecoalesce->rx_max_coalesced_frames ||
2271 	    !ecoalesce->tx_max_coalesced_frames) {
2272 		NL_SET_ERR_MSG(extack, "frames must be non-zero");
2273 		return -EINVAL;
2274 	}
2275 
2276 	if (((ecoalesce->rx_max_coalesced_frames > 1 || new_dim) &&
2277 	     !ecoalesce->rx_coalesce_usecs) ||
2278 	    (ecoalesce->tx_max_coalesced_frames > 1 &&
2279 	     !ecoalesce->tx_coalesce_usecs)) {
2280 		NL_SET_ERR_MSG(extack,
2281 			       "usecs must be non-zero when frames is greater than one");
2282 		return -EINVAL;
2283 	}
2284 
2285 	if (new_dim && !old_dim) {
2286 		cr = axienet_calc_cr(lp, axienet_dim_coalesce_count_rx(lp),
2287 				     ecoalesce->rx_coalesce_usecs);
2288 	} else if (!new_dim) {
2289 		if (old_dim) {
2290 			WRITE_ONCE(lp->rx_dim_enabled, false);
2291 			napi_synchronize(&lp->napi_rx);
2292 			flush_work(&lp->rx_dim.work);
2293 		}
2294 
2295 		cr = axienet_calc_cr(lp, ecoalesce->rx_max_coalesced_frames,
2296 				     ecoalesce->rx_coalesce_usecs);
2297 	} else {
2298 		/* Dummy value for count just to calculate timer */
2299 		cr = axienet_calc_cr(lp, 2, ecoalesce->rx_coalesce_usecs);
2300 		mask = XAXIDMA_DELAY_MASK | XAXIDMA_IRQ_DELAY_MASK;
2301 	}
2302 
2303 	axienet_update_coalesce_rx(lp, cr, mask);
2304 	if (new_dim && !old_dim)
2305 		WRITE_ONCE(lp->rx_dim_enabled, true);
2306 
2307 	cr = axienet_calc_cr(lp, ecoalesce->tx_max_coalesced_frames,
2308 			     ecoalesce->tx_coalesce_usecs);
2309 	axienet_update_coalesce_tx(lp, cr, ~XAXIDMA_CR_RUNSTOP_MASK);
2310 	return 0;
2311 }
2312 
2313 static int
axienet_ethtools_get_link_ksettings(struct net_device * ndev,struct ethtool_link_ksettings * cmd)2314 axienet_ethtools_get_link_ksettings(struct net_device *ndev,
2315 				    struct ethtool_link_ksettings *cmd)
2316 {
2317 	struct axienet_local *lp = netdev_priv(ndev);
2318 
2319 	return phylink_ethtool_ksettings_get(lp->phylink, cmd);
2320 }
2321 
2322 static int
axienet_ethtools_set_link_ksettings(struct net_device * ndev,const struct ethtool_link_ksettings * cmd)2323 axienet_ethtools_set_link_ksettings(struct net_device *ndev,
2324 				    const struct ethtool_link_ksettings *cmd)
2325 {
2326 	struct axienet_local *lp = netdev_priv(ndev);
2327 
2328 	return phylink_ethtool_ksettings_set(lp->phylink, cmd);
2329 }
2330 
axienet_ethtools_nway_reset(struct net_device * dev)2331 static int axienet_ethtools_nway_reset(struct net_device *dev)
2332 {
2333 	struct axienet_local *lp = netdev_priv(dev);
2334 
2335 	return phylink_ethtool_nway_reset(lp->phylink);
2336 }
2337 
axienet_ethtools_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)2338 static void axienet_ethtools_get_ethtool_stats(struct net_device *dev,
2339 					       struct ethtool_stats *stats,
2340 					       u64 *data)
2341 {
2342 	struct axienet_local *lp = netdev_priv(dev);
2343 	unsigned int start;
2344 
2345 	do {
2346 		start = read_seqcount_begin(&lp->hw_stats_seqcount);
2347 		data[0] = axienet_stat(lp, STAT_RX_BYTES);
2348 		data[1] = axienet_stat(lp, STAT_TX_BYTES);
2349 		data[2] = axienet_stat(lp, STAT_RX_VLAN_FRAMES);
2350 		data[3] = axienet_stat(lp, STAT_TX_VLAN_FRAMES);
2351 		data[6] = axienet_stat(lp, STAT_TX_PFC_FRAMES);
2352 		data[7] = axienet_stat(lp, STAT_RX_PFC_FRAMES);
2353 		data[8] = axienet_stat(lp, STAT_USER_DEFINED0);
2354 		data[9] = axienet_stat(lp, STAT_USER_DEFINED1);
2355 		data[10] = axienet_stat(lp, STAT_USER_DEFINED2);
2356 	} while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
2357 }
2358 
2359 static const char axienet_ethtool_stats_strings[][ETH_GSTRING_LEN] = {
2360 	"Received bytes",
2361 	"Transmitted bytes",
2362 	"RX Good VLAN Tagged Frames",
2363 	"TX Good VLAN Tagged Frames",
2364 	"TX Good PFC Frames",
2365 	"RX Good PFC Frames",
2366 	"User Defined Counter 0",
2367 	"User Defined Counter 1",
2368 	"User Defined Counter 2",
2369 };
2370 
axienet_ethtools_get_strings(struct net_device * dev,u32 stringset,u8 * data)2371 static void axienet_ethtools_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2372 {
2373 	switch (stringset) {
2374 	case ETH_SS_STATS:
2375 		memcpy(data, axienet_ethtool_stats_strings,
2376 		       sizeof(axienet_ethtool_stats_strings));
2377 		break;
2378 	}
2379 }
2380 
axienet_ethtools_get_sset_count(struct net_device * dev,int sset)2381 static int axienet_ethtools_get_sset_count(struct net_device *dev, int sset)
2382 {
2383 	struct axienet_local *lp = netdev_priv(dev);
2384 
2385 	switch (sset) {
2386 	case ETH_SS_STATS:
2387 		if (lp->features & XAE_FEATURE_STATS)
2388 			return ARRAY_SIZE(axienet_ethtool_stats_strings);
2389 		fallthrough;
2390 	default:
2391 		return -EOPNOTSUPP;
2392 	}
2393 }
2394 
2395 static void
axienet_ethtools_get_pause_stats(struct net_device * dev,struct ethtool_pause_stats * pause_stats)2396 axienet_ethtools_get_pause_stats(struct net_device *dev,
2397 				 struct ethtool_pause_stats *pause_stats)
2398 {
2399 	struct axienet_local *lp = netdev_priv(dev);
2400 	unsigned int start;
2401 
2402 	if (!(lp->features & XAE_FEATURE_STATS))
2403 		return;
2404 
2405 	do {
2406 		start = read_seqcount_begin(&lp->hw_stats_seqcount);
2407 		pause_stats->tx_pause_frames =
2408 			axienet_stat(lp, STAT_TX_PAUSE_FRAMES);
2409 		pause_stats->rx_pause_frames =
2410 			axienet_stat(lp, STAT_RX_PAUSE_FRAMES);
2411 	} while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
2412 }
2413 
2414 static void
axienet_ethtool_get_eth_mac_stats(struct net_device * dev,struct ethtool_eth_mac_stats * mac_stats)2415 axienet_ethtool_get_eth_mac_stats(struct net_device *dev,
2416 				  struct ethtool_eth_mac_stats *mac_stats)
2417 {
2418 	struct axienet_local *lp = netdev_priv(dev);
2419 	unsigned int start;
2420 
2421 	if (!(lp->features & XAE_FEATURE_STATS))
2422 		return;
2423 
2424 	do {
2425 		start = read_seqcount_begin(&lp->hw_stats_seqcount);
2426 		mac_stats->FramesTransmittedOK =
2427 			axienet_stat(lp, STAT_TX_GOOD_FRAMES);
2428 		mac_stats->SingleCollisionFrames =
2429 			axienet_stat(lp, STAT_TX_SINGLE_COLLISION_FRAMES);
2430 		mac_stats->MultipleCollisionFrames =
2431 			axienet_stat(lp, STAT_TX_MULTIPLE_COLLISION_FRAMES);
2432 		mac_stats->FramesReceivedOK =
2433 			axienet_stat(lp, STAT_RX_GOOD_FRAMES);
2434 		mac_stats->FrameCheckSequenceErrors =
2435 			axienet_stat(lp, STAT_RX_FCS_ERRORS);
2436 		mac_stats->AlignmentErrors =
2437 			axienet_stat(lp, STAT_RX_ALIGNMENT_ERRORS);
2438 		mac_stats->FramesWithDeferredXmissions =
2439 			axienet_stat(lp, STAT_TX_DEFERRED_FRAMES);
2440 		mac_stats->LateCollisions =
2441 			axienet_stat(lp, STAT_TX_LATE_COLLISIONS);
2442 		mac_stats->FramesAbortedDueToXSColls =
2443 			axienet_stat(lp, STAT_TX_EXCESS_COLLISIONS);
2444 		mac_stats->MulticastFramesXmittedOK =
2445 			axienet_stat(lp, STAT_TX_MULTICAST_FRAMES);
2446 		mac_stats->BroadcastFramesXmittedOK =
2447 			axienet_stat(lp, STAT_TX_BROADCAST_FRAMES);
2448 		mac_stats->FramesWithExcessiveDeferral =
2449 			axienet_stat(lp, STAT_TX_EXCESS_DEFERRAL);
2450 		mac_stats->MulticastFramesReceivedOK =
2451 			axienet_stat(lp, STAT_RX_MULTICAST_FRAMES);
2452 		mac_stats->BroadcastFramesReceivedOK =
2453 			axienet_stat(lp, STAT_RX_BROADCAST_FRAMES);
2454 		mac_stats->InRangeLengthErrors =
2455 			axienet_stat(lp, STAT_RX_LENGTH_ERRORS);
2456 	} while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
2457 }
2458 
2459 static void
axienet_ethtool_get_eth_ctrl_stats(struct net_device * dev,struct ethtool_eth_ctrl_stats * ctrl_stats)2460 axienet_ethtool_get_eth_ctrl_stats(struct net_device *dev,
2461 				   struct ethtool_eth_ctrl_stats *ctrl_stats)
2462 {
2463 	struct axienet_local *lp = netdev_priv(dev);
2464 	unsigned int start;
2465 
2466 	if (!(lp->features & XAE_FEATURE_STATS))
2467 		return;
2468 
2469 	do {
2470 		start = read_seqcount_begin(&lp->hw_stats_seqcount);
2471 		ctrl_stats->MACControlFramesTransmitted =
2472 			axienet_stat(lp, STAT_TX_CONTROL_FRAMES);
2473 		ctrl_stats->MACControlFramesReceived =
2474 			axienet_stat(lp, STAT_RX_CONTROL_FRAMES);
2475 		ctrl_stats->UnsupportedOpcodesReceived =
2476 			axienet_stat(lp, STAT_RX_CONTROL_OPCODE_ERRORS);
2477 	} while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
2478 }
2479 
2480 static const struct ethtool_rmon_hist_range axienet_rmon_ranges[] = {
2481 	{   64,    64 },
2482 	{   65,   127 },
2483 	{  128,   255 },
2484 	{  256,   511 },
2485 	{  512,  1023 },
2486 	{ 1024,  1518 },
2487 	{ 1519, 16384 },
2488 	{ },
2489 };
2490 
2491 static void
axienet_ethtool_get_rmon_stats(struct net_device * dev,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)2492 axienet_ethtool_get_rmon_stats(struct net_device *dev,
2493 			       struct ethtool_rmon_stats *rmon_stats,
2494 			       const struct ethtool_rmon_hist_range **ranges)
2495 {
2496 	struct axienet_local *lp = netdev_priv(dev);
2497 	unsigned int start;
2498 
2499 	if (!(lp->features & XAE_FEATURE_STATS))
2500 		return;
2501 
2502 	do {
2503 		start = read_seqcount_begin(&lp->hw_stats_seqcount);
2504 		rmon_stats->undersize_pkts =
2505 			axienet_stat(lp, STAT_UNDERSIZE_FRAMES);
2506 		rmon_stats->oversize_pkts =
2507 			axienet_stat(lp, STAT_RX_OVERSIZE_FRAMES);
2508 		rmon_stats->fragments =
2509 			axienet_stat(lp, STAT_FRAGMENT_FRAMES);
2510 
2511 		rmon_stats->hist[0] =
2512 			axienet_stat(lp, STAT_RX_64_BYTE_FRAMES);
2513 		rmon_stats->hist[1] =
2514 			axienet_stat(lp, STAT_RX_65_127_BYTE_FRAMES);
2515 		rmon_stats->hist[2] =
2516 			axienet_stat(lp, STAT_RX_128_255_BYTE_FRAMES);
2517 		rmon_stats->hist[3] =
2518 			axienet_stat(lp, STAT_RX_256_511_BYTE_FRAMES);
2519 		rmon_stats->hist[4] =
2520 			axienet_stat(lp, STAT_RX_512_1023_BYTE_FRAMES);
2521 		rmon_stats->hist[5] =
2522 			axienet_stat(lp, STAT_RX_1024_MAX_BYTE_FRAMES);
2523 		rmon_stats->hist[6] =
2524 			rmon_stats->oversize_pkts;
2525 
2526 		rmon_stats->hist_tx[0] =
2527 			axienet_stat(lp, STAT_TX_64_BYTE_FRAMES);
2528 		rmon_stats->hist_tx[1] =
2529 			axienet_stat(lp, STAT_TX_65_127_BYTE_FRAMES);
2530 		rmon_stats->hist_tx[2] =
2531 			axienet_stat(lp, STAT_TX_128_255_BYTE_FRAMES);
2532 		rmon_stats->hist_tx[3] =
2533 			axienet_stat(lp, STAT_TX_256_511_BYTE_FRAMES);
2534 		rmon_stats->hist_tx[4] =
2535 			axienet_stat(lp, STAT_TX_512_1023_BYTE_FRAMES);
2536 		rmon_stats->hist_tx[5] =
2537 			axienet_stat(lp, STAT_TX_1024_MAX_BYTE_FRAMES);
2538 		rmon_stats->hist_tx[6] =
2539 			axienet_stat(lp, STAT_TX_OVERSIZE_FRAMES);
2540 	} while (read_seqcount_retry(&lp->hw_stats_seqcount, start));
2541 
2542 	*ranges = axienet_rmon_ranges;
2543 }
2544 
2545 static const struct ethtool_ops axienet_ethtool_ops = {
2546 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
2547 				     ETHTOOL_COALESCE_USECS |
2548 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
2549 	.get_drvinfo    = axienet_ethtools_get_drvinfo,
2550 	.get_regs_len   = axienet_ethtools_get_regs_len,
2551 	.get_regs       = axienet_ethtools_get_regs,
2552 	.get_link       = ethtool_op_get_link,
2553 	.get_ringparam	= axienet_ethtools_get_ringparam,
2554 	.set_ringparam	= axienet_ethtools_set_ringparam,
2555 	.get_pauseparam = axienet_ethtools_get_pauseparam,
2556 	.set_pauseparam = axienet_ethtools_set_pauseparam,
2557 	.get_coalesce   = axienet_ethtools_get_coalesce,
2558 	.set_coalesce   = axienet_ethtools_set_coalesce,
2559 	.get_link_ksettings = axienet_ethtools_get_link_ksettings,
2560 	.set_link_ksettings = axienet_ethtools_set_link_ksettings,
2561 	.nway_reset	= axienet_ethtools_nway_reset,
2562 	.get_ethtool_stats = axienet_ethtools_get_ethtool_stats,
2563 	.get_strings    = axienet_ethtools_get_strings,
2564 	.get_sset_count = axienet_ethtools_get_sset_count,
2565 	.get_pause_stats = axienet_ethtools_get_pause_stats,
2566 	.get_eth_mac_stats = axienet_ethtool_get_eth_mac_stats,
2567 	.get_eth_ctrl_stats = axienet_ethtool_get_eth_ctrl_stats,
2568 	.get_rmon_stats = axienet_ethtool_get_rmon_stats,
2569 };
2570 
2571 static const struct ethtool_ops axienet_ethtool_dmaengine_ops = {
2572 	.get_drvinfo    = axienet_ethtools_get_drvinfo,
2573 	.get_regs_len   = axienet_ethtools_get_regs_len,
2574 	.get_regs       = axienet_ethtools_get_regs,
2575 	.get_link       = ethtool_op_get_link,
2576 	.get_pauseparam = axienet_ethtools_get_pauseparam,
2577 	.set_pauseparam = axienet_ethtools_set_pauseparam,
2578 	.get_link_ksettings = axienet_ethtools_get_link_ksettings,
2579 	.set_link_ksettings = axienet_ethtools_set_link_ksettings,
2580 	.nway_reset	= axienet_ethtools_nway_reset,
2581 	.get_ethtool_stats = axienet_ethtools_get_ethtool_stats,
2582 	.get_strings    = axienet_ethtools_get_strings,
2583 	.get_sset_count = axienet_ethtools_get_sset_count,
2584 	.get_pause_stats = axienet_ethtools_get_pause_stats,
2585 	.get_eth_mac_stats = axienet_ethtool_get_eth_mac_stats,
2586 	.get_eth_ctrl_stats = axienet_ethtool_get_eth_ctrl_stats,
2587 	.get_rmon_stats = axienet_ethtool_get_rmon_stats,
2588 };
2589 
pcs_to_axienet_local(struct phylink_pcs * pcs)2590 static struct axienet_local *pcs_to_axienet_local(struct phylink_pcs *pcs)
2591 {
2592 	return container_of(pcs, struct axienet_local, pcs);
2593 }
2594 
axienet_pcs_get_state(struct phylink_pcs * pcs,unsigned int neg_mode,struct phylink_link_state * state)2595 static void axienet_pcs_get_state(struct phylink_pcs *pcs,
2596 				  unsigned int neg_mode,
2597 				  struct phylink_link_state *state)
2598 {
2599 	struct mdio_device *pcs_phy = pcs_to_axienet_local(pcs)->pcs_phy;
2600 
2601 	phylink_mii_c22_pcs_get_state(pcs_phy, neg_mode, state);
2602 }
2603 
axienet_pcs_an_restart(struct phylink_pcs * pcs)2604 static void axienet_pcs_an_restart(struct phylink_pcs *pcs)
2605 {
2606 	struct mdio_device *pcs_phy = pcs_to_axienet_local(pcs)->pcs_phy;
2607 
2608 	phylink_mii_c22_pcs_an_restart(pcs_phy);
2609 }
2610 
axienet_pcs_config(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)2611 static int axienet_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
2612 			      phy_interface_t interface,
2613 			      const unsigned long *advertising,
2614 			      bool permit_pause_to_mac)
2615 {
2616 	struct mdio_device *pcs_phy = pcs_to_axienet_local(pcs)->pcs_phy;
2617 	struct net_device *ndev = pcs_to_axienet_local(pcs)->ndev;
2618 	struct axienet_local *lp = netdev_priv(ndev);
2619 	int ret;
2620 
2621 	if (lp->switch_x_sgmii) {
2622 		ret = mdiodev_write(pcs_phy, XLNX_MII_STD_SELECT_REG,
2623 				    interface == PHY_INTERFACE_MODE_SGMII ?
2624 					XLNX_MII_STD_SELECT_SGMII : 0);
2625 		if (ret < 0) {
2626 			netdev_warn(ndev,
2627 				    "Failed to switch PHY interface: %d\n",
2628 				    ret);
2629 			return ret;
2630 		}
2631 	}
2632 
2633 	ret = phylink_mii_c22_pcs_config(pcs_phy, interface, advertising,
2634 					 neg_mode);
2635 	if (ret < 0)
2636 		netdev_warn(ndev, "Failed to configure PCS: %d\n", ret);
2637 
2638 	return ret;
2639 }
2640 
2641 static const struct phylink_pcs_ops axienet_pcs_ops = {
2642 	.pcs_get_state = axienet_pcs_get_state,
2643 	.pcs_config = axienet_pcs_config,
2644 	.pcs_an_restart = axienet_pcs_an_restart,
2645 };
2646 
axienet_mac_select_pcs(struct phylink_config * config,phy_interface_t interface)2647 static struct phylink_pcs *axienet_mac_select_pcs(struct phylink_config *config,
2648 						  phy_interface_t interface)
2649 {
2650 	struct net_device *ndev = to_net_dev(config->dev);
2651 	struct axienet_local *lp = netdev_priv(ndev);
2652 
2653 	if (interface == PHY_INTERFACE_MODE_1000BASEX ||
2654 	    interface ==  PHY_INTERFACE_MODE_SGMII)
2655 		return &lp->pcs;
2656 
2657 	return NULL;
2658 }
2659 
axienet_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)2660 static void axienet_mac_config(struct phylink_config *config, unsigned int mode,
2661 			       const struct phylink_link_state *state)
2662 {
2663 	/* nothing meaningful to do */
2664 }
2665 
axienet_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)2666 static void axienet_mac_link_down(struct phylink_config *config,
2667 				  unsigned int mode,
2668 				  phy_interface_t interface)
2669 {
2670 	/* nothing meaningful to do */
2671 }
2672 
axienet_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)2673 static void axienet_mac_link_up(struct phylink_config *config,
2674 				struct phy_device *phy,
2675 				unsigned int mode, phy_interface_t interface,
2676 				int speed, int duplex,
2677 				bool tx_pause, bool rx_pause)
2678 {
2679 	struct net_device *ndev = to_net_dev(config->dev);
2680 	struct axienet_local *lp = netdev_priv(ndev);
2681 	u32 emmc_reg, fcc_reg;
2682 
2683 	emmc_reg = axienet_ior(lp, XAE_EMMC_OFFSET);
2684 	emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
2685 
2686 	switch (speed) {
2687 	case SPEED_1000:
2688 		emmc_reg |= XAE_EMMC_LINKSPD_1000;
2689 		break;
2690 	case SPEED_100:
2691 		emmc_reg |= XAE_EMMC_LINKSPD_100;
2692 		break;
2693 	case SPEED_10:
2694 		emmc_reg |= XAE_EMMC_LINKSPD_10;
2695 		break;
2696 	default:
2697 		dev_err(&ndev->dev,
2698 			"Speed other than 10, 100 or 1Gbps is not supported\n");
2699 		break;
2700 	}
2701 
2702 	axienet_iow(lp, XAE_EMMC_OFFSET, emmc_reg);
2703 
2704 	fcc_reg = axienet_ior(lp, XAE_FCC_OFFSET);
2705 	if (tx_pause)
2706 		fcc_reg |= XAE_FCC_FCTX_MASK;
2707 	else
2708 		fcc_reg &= ~XAE_FCC_FCTX_MASK;
2709 	if (rx_pause)
2710 		fcc_reg |= XAE_FCC_FCRX_MASK;
2711 	else
2712 		fcc_reg &= ~XAE_FCC_FCRX_MASK;
2713 	axienet_iow(lp, XAE_FCC_OFFSET, fcc_reg);
2714 }
2715 
2716 static const struct phylink_mac_ops axienet_phylink_ops = {
2717 	.mac_select_pcs = axienet_mac_select_pcs,
2718 	.mac_config = axienet_mac_config,
2719 	.mac_link_down = axienet_mac_link_down,
2720 	.mac_link_up = axienet_mac_link_up,
2721 };
2722 
2723 /**
2724  * axienet_dma_err_handler - Work queue task for Axi DMA Error
2725  * @work:	pointer to work_struct
2726  *
2727  * Resets the Axi DMA and Axi Ethernet devices, and reconfigures the
2728  * Tx/Rx BDs.
2729  */
axienet_dma_err_handler(struct work_struct * work)2730 static void axienet_dma_err_handler(struct work_struct *work)
2731 {
2732 	u32 i;
2733 	u32 axienet_status;
2734 	struct axidma_bd *cur_p;
2735 	struct axienet_local *lp = container_of(work, struct axienet_local,
2736 						dma_err_task);
2737 	struct net_device *ndev = lp->ndev;
2738 
2739 	/* Don't bother if we are going to stop anyway */
2740 	if (READ_ONCE(lp->stopping))
2741 		return;
2742 
2743 	napi_disable(&lp->napi_tx);
2744 	napi_disable(&lp->napi_rx);
2745 
2746 	axienet_setoptions(ndev, lp->options &
2747 			   ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
2748 
2749 	axienet_dma_stop(lp);
2750 	netdev_reset_queue(ndev);
2751 
2752 	for (i = 0; i < lp->tx_bd_num; i++) {
2753 		cur_p = &lp->tx_bd_v[i];
2754 		if (cur_p->cntrl) {
2755 			dma_addr_t addr = desc_get_phys_addr(lp, cur_p);
2756 
2757 			dma_unmap_single(lp->dev, addr,
2758 					 (cur_p->cntrl &
2759 					  XAXIDMA_BD_CTRL_LENGTH_MASK),
2760 					 DMA_TO_DEVICE);
2761 		}
2762 		if (cur_p->skb)
2763 			dev_kfree_skb_irq(cur_p->skb);
2764 		cur_p->phys = 0;
2765 		cur_p->phys_msb = 0;
2766 		cur_p->cntrl = 0;
2767 		cur_p->status = 0;
2768 		cur_p->app0 = 0;
2769 		cur_p->app1 = 0;
2770 		cur_p->app2 = 0;
2771 		cur_p->app3 = 0;
2772 		cur_p->app4 = 0;
2773 		cur_p->skb = NULL;
2774 	}
2775 
2776 	for (i = 0; i < lp->rx_bd_num; i++) {
2777 		cur_p = &lp->rx_bd_v[i];
2778 		cur_p->status = 0;
2779 		cur_p->app0 = 0;
2780 		cur_p->app1 = 0;
2781 		cur_p->app2 = 0;
2782 		cur_p->app3 = 0;
2783 		cur_p->app4 = 0;
2784 	}
2785 
2786 	lp->tx_bd_ci = 0;
2787 	lp->tx_bd_tail = 0;
2788 	lp->rx_bd_ci = 0;
2789 
2790 	axienet_dma_start(lp);
2791 
2792 	axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
2793 	axienet_status &= ~XAE_RCW1_RX_MASK;
2794 	axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
2795 
2796 	axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
2797 	if (axienet_status & XAE_INT_RXRJECT_MASK)
2798 		axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
2799 	axienet_iow(lp, XAE_IE_OFFSET, lp->eth_irq > 0 ?
2800 		    XAE_INT_RECV_ERROR_MASK : 0);
2801 	axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
2802 
2803 	/* Sync default options with HW but leave receiver and
2804 	 * transmitter disabled.
2805 	 */
2806 	axienet_setoptions(ndev, lp->options &
2807 			   ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
2808 	axienet_set_mac_address(ndev, NULL);
2809 	axienet_set_multicast_list(ndev);
2810 	napi_enable(&lp->napi_rx);
2811 	napi_enable(&lp->napi_tx);
2812 	axienet_setoptions(ndev, lp->options);
2813 }
2814 
2815 /**
2816  * axienet_probe - Axi Ethernet probe function.
2817  * @pdev:	Pointer to platform device structure.
2818  *
2819  * Return: 0, on success
2820  *	    Non-zero error value on failure.
2821  *
2822  * This is the probe routine for Axi Ethernet driver. This is called before
2823  * any other driver routines are invoked. It allocates and sets up the Ethernet
2824  * device. Parses through device tree and populates fields of
2825  * axienet_local. It registers the Ethernet device.
2826  */
axienet_probe(struct platform_device * pdev)2827 static int axienet_probe(struct platform_device *pdev)
2828 {
2829 	int ret;
2830 	struct device_node *np;
2831 	struct axienet_local *lp;
2832 	struct net_device *ndev;
2833 	struct resource *ethres;
2834 	u8 mac_addr[ETH_ALEN];
2835 	int addr_width = 32;
2836 	u32 value;
2837 
2838 	ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*lp));
2839 	if (!ndev)
2840 		return -ENOMEM;
2841 
2842 	platform_set_drvdata(pdev, ndev);
2843 
2844 	SET_NETDEV_DEV(ndev, &pdev->dev);
2845 	ndev->features = NETIF_F_SG;
2846 
2847 	/* MTU range: 64 - 9000 */
2848 	ndev->min_mtu = 64;
2849 	ndev->max_mtu = XAE_JUMBO_MTU;
2850 
2851 	lp = netdev_priv(ndev);
2852 	lp->ndev = ndev;
2853 	lp->dev = &pdev->dev;
2854 	lp->options = XAE_OPTION_DEFAULTS;
2855 	lp->rx_bd_num = RX_BD_NUM_DEFAULT;
2856 	lp->tx_bd_num = TX_BD_NUM_DEFAULT;
2857 
2858 	u64_stats_init(&lp->rx_stat_sync);
2859 	u64_stats_init(&lp->tx_stat_sync);
2860 
2861 	mutex_init(&lp->stats_lock);
2862 	seqcount_mutex_init(&lp->hw_stats_seqcount, &lp->stats_lock);
2863 	INIT_DEFERRABLE_WORK(&lp->stats_work, axienet_refresh_stats);
2864 
2865 	lp->axi_clk = devm_clk_get_optional_enabled(&pdev->dev,
2866 						    "s_axi_lite_clk");
2867 	if (!lp->axi_clk) {
2868 		/* For backward compatibility, if named AXI clock is not present,
2869 		 * treat the first clock specified as the AXI clock.
2870 		 */
2871 		lp->axi_clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
2872 	}
2873 	if (IS_ERR(lp->axi_clk))
2874 		return dev_err_probe(&pdev->dev, PTR_ERR(lp->axi_clk),
2875 				     "could not get AXI clock\n");
2876 
2877 	lp->misc_clks[0].id = "axis_clk";
2878 	lp->misc_clks[1].id = "ref_clk";
2879 	lp->misc_clks[2].id = "mgt_clk";
2880 
2881 	ret = devm_clk_bulk_get_optional_enable(&pdev->dev, XAE_NUM_MISC_CLOCKS,
2882 						lp->misc_clks);
2883 	if (ret)
2884 		return dev_err_probe(&pdev->dev, ret,
2885 				     "could not get/enable misc. clocks\n");
2886 
2887 	/* Map device registers */
2888 	lp->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ethres);
2889 	if (IS_ERR(lp->regs))
2890 		return PTR_ERR(lp->regs);
2891 	lp->regs_start = ethres->start;
2892 
2893 	/* Setup checksum offload, but default to off if not specified */
2894 	lp->features = 0;
2895 
2896 	if (axienet_ior(lp, XAE_ABILITY_OFFSET) & XAE_ABILITY_STATS)
2897 		lp->features |= XAE_FEATURE_STATS;
2898 
2899 	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,txcsum", &value);
2900 	if (!ret) {
2901 		switch (value) {
2902 		case 1:
2903 			lp->features |= XAE_FEATURE_PARTIAL_TX_CSUM;
2904 			/* Can checksum any contiguous range */
2905 			ndev->features |= NETIF_F_HW_CSUM;
2906 			break;
2907 		case 2:
2908 			lp->features |= XAE_FEATURE_FULL_TX_CSUM;
2909 			/* Can checksum TCP/UDP over IPv4. */
2910 			ndev->features |= NETIF_F_IP_CSUM;
2911 			break;
2912 		}
2913 	}
2914 	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,rxcsum", &value);
2915 	if (!ret) {
2916 		switch (value) {
2917 		case 1:
2918 			lp->features |= XAE_FEATURE_PARTIAL_RX_CSUM;
2919 			ndev->features |= NETIF_F_RXCSUM;
2920 			break;
2921 		case 2:
2922 			lp->features |= XAE_FEATURE_FULL_RX_CSUM;
2923 			ndev->features |= NETIF_F_RXCSUM;
2924 			break;
2925 		}
2926 	}
2927 	/* For supporting jumbo frames, the Axi Ethernet hardware must have
2928 	 * a larger Rx/Tx Memory. Typically, the size must be large so that
2929 	 * we can enable jumbo option and start supporting jumbo frames.
2930 	 * Here we check for memory allocated for Rx/Tx in the hardware from
2931 	 * the device-tree and accordingly set flags.
2932 	 */
2933 	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,rxmem", &lp->rxmem);
2934 	if (ret)
2935 		return dev_err_probe(&pdev->dev, ret,
2936 				     "failed to read xlnx,rxmem property\n");
2937 
2938 	lp->switch_x_sgmii = of_property_read_bool(pdev->dev.of_node,
2939 						   "xlnx,switch-x-sgmii");
2940 
2941 	/* Start with the proprietary, and broken phy_type */
2942 	ret = of_property_read_u32(pdev->dev.of_node, "xlnx,phy-type", &value);
2943 	if (!ret) {
2944 		netdev_warn(ndev, "Please upgrade your device tree binary blob to use phy-mode");
2945 		switch (value) {
2946 		case XAE_PHY_TYPE_MII:
2947 			lp->phy_mode = PHY_INTERFACE_MODE_MII;
2948 			break;
2949 		case XAE_PHY_TYPE_GMII:
2950 			lp->phy_mode = PHY_INTERFACE_MODE_GMII;
2951 			break;
2952 		case XAE_PHY_TYPE_RGMII_2_0:
2953 			lp->phy_mode = PHY_INTERFACE_MODE_RGMII_ID;
2954 			break;
2955 		case XAE_PHY_TYPE_SGMII:
2956 			lp->phy_mode = PHY_INTERFACE_MODE_SGMII;
2957 			break;
2958 		case XAE_PHY_TYPE_1000BASE_X:
2959 			lp->phy_mode = PHY_INTERFACE_MODE_1000BASEX;
2960 			break;
2961 		default:
2962 			return -EINVAL;
2963 		}
2964 	} else {
2965 		ret = of_get_phy_mode(pdev->dev.of_node, &lp->phy_mode);
2966 		if (ret)
2967 			return ret;
2968 	}
2969 	if (lp->switch_x_sgmii && lp->phy_mode != PHY_INTERFACE_MODE_SGMII &&
2970 	    lp->phy_mode != PHY_INTERFACE_MODE_1000BASEX) {
2971 		dev_err(&pdev->dev, "xlnx,switch-x-sgmii only supported with SGMII or 1000BaseX\n");
2972 		return -EINVAL;
2973 	}
2974 
2975 	if (!of_property_present(pdev->dev.of_node, "dmas")) {
2976 		/* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
2977 		np = of_parse_phandle(pdev->dev.of_node, "axistream-connected", 0);
2978 
2979 		if (np) {
2980 			struct resource dmares;
2981 
2982 			ret = of_address_to_resource(np, 0, &dmares);
2983 			if (ret) {
2984 				dev_err(&pdev->dev,
2985 					"unable to get DMA resource\n");
2986 				of_node_put(np);
2987 				return ret;
2988 			}
2989 			lp->dma_regs = devm_ioremap_resource(&pdev->dev,
2990 							     &dmares);
2991 			lp->rx_irq = irq_of_parse_and_map(np, 1);
2992 			lp->tx_irq = irq_of_parse_and_map(np, 0);
2993 			of_node_put(np);
2994 			lp->eth_irq = platform_get_irq_optional(pdev, 0);
2995 		} else {
2996 			/* Check for these resources directly on the Ethernet node. */
2997 			lp->dma_regs = devm_platform_get_and_ioremap_resource(pdev, 1, NULL);
2998 			lp->rx_irq = platform_get_irq(pdev, 1);
2999 			lp->tx_irq = platform_get_irq(pdev, 0);
3000 			lp->eth_irq = platform_get_irq_optional(pdev, 2);
3001 		}
3002 		if (IS_ERR(lp->dma_regs)) {
3003 			dev_err(&pdev->dev, "could not map DMA regs\n");
3004 			return PTR_ERR(lp->dma_regs);
3005 		}
3006 		if (lp->rx_irq <= 0 || lp->tx_irq <= 0) {
3007 			dev_err(&pdev->dev, "could not determine irqs\n");
3008 			return -ENOMEM;
3009 		}
3010 
3011 		/* Reset core now that clocks are enabled, prior to accessing MDIO */
3012 		ret = __axienet_device_reset(lp);
3013 		if (ret)
3014 			return ret;
3015 
3016 		/* Autodetect the need for 64-bit DMA pointers.
3017 		 * When the IP is configured for a bus width bigger than 32 bits,
3018 		 * writing the MSB registers is mandatory, even if they are all 0.
3019 		 * We can detect this case by writing all 1's to one such register
3020 		 * and see if that sticks: when the IP is configured for 32 bits
3021 		 * only, those registers are RES0.
3022 		 * Those MSB registers were introduced in IP v7.1, which we check first.
3023 		 */
3024 		if ((axienet_ior(lp, XAE_ID_OFFSET) >> 24) >= 0x9) {
3025 			void __iomem *desc = lp->dma_regs + XAXIDMA_TX_CDESC_OFFSET + 4;
3026 
3027 			iowrite32(0x0, desc);
3028 			if (ioread32(desc) == 0) {	/* sanity check */
3029 				iowrite32(0xffffffff, desc);
3030 				if (ioread32(desc) > 0) {
3031 					lp->features |= XAE_FEATURE_DMA_64BIT;
3032 					addr_width = 64;
3033 					dev_info(&pdev->dev,
3034 						 "autodetected 64-bit DMA range\n");
3035 				}
3036 				iowrite32(0x0, desc);
3037 			}
3038 		}
3039 		if (!IS_ENABLED(CONFIG_64BIT) && lp->features & XAE_FEATURE_DMA_64BIT) {
3040 			dev_err(&pdev->dev, "64-bit addressable DMA is not compatible with 32-bit architecture\n");
3041 			return -EINVAL;
3042 		}
3043 
3044 		ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(addr_width));
3045 		if (ret) {
3046 			dev_err(&pdev->dev, "No suitable DMA available\n");
3047 			return ret;
3048 		}
3049 		netif_napi_add(ndev, &lp->napi_rx, axienet_rx_poll);
3050 		netif_napi_add(ndev, &lp->napi_tx, axienet_tx_poll);
3051 	} else {
3052 		struct xilinx_vdma_config cfg;
3053 		struct dma_chan *tx_chan;
3054 
3055 		lp->eth_irq = platform_get_irq_optional(pdev, 0);
3056 		if (lp->eth_irq < 0 && lp->eth_irq != -ENXIO) {
3057 			return lp->eth_irq;
3058 		}
3059 		tx_chan = dma_request_chan(lp->dev, "tx_chan0");
3060 		if (IS_ERR(tx_chan))
3061 			return dev_err_probe(lp->dev, PTR_ERR(tx_chan),
3062 					     "No Ethernet DMA (TX) channel found\n");
3063 
3064 		cfg.reset = 1;
3065 		/* As name says VDMA but it has support for DMA channel reset */
3066 		ret = xilinx_vdma_channel_set_config(tx_chan, &cfg);
3067 		if (ret < 0) {
3068 			dev_err(&pdev->dev, "Reset channel failed\n");
3069 			dma_release_channel(tx_chan);
3070 			return ret;
3071 		}
3072 
3073 		dma_release_channel(tx_chan);
3074 		lp->use_dmaengine = 1;
3075 	}
3076 
3077 	if (lp->use_dmaengine) {
3078 		ndev->netdev_ops = &axienet_netdev_dmaengine_ops;
3079 		ndev->ethtool_ops = &axienet_ethtool_dmaengine_ops;
3080 	} else {
3081 		ndev->netdev_ops = &axienet_netdev_ops;
3082 		ndev->ethtool_ops = &axienet_ethtool_ops;
3083 	}
3084 	/* Check for Ethernet core IRQ (optional) */
3085 	if (lp->eth_irq <= 0)
3086 		dev_info(&pdev->dev, "Ethernet core IRQ not defined\n");
3087 
3088 	/* Retrieve the MAC address */
3089 	ret = of_get_mac_address(pdev->dev.of_node, mac_addr);
3090 	if (!ret) {
3091 		axienet_set_mac_address(ndev, mac_addr);
3092 	} else {
3093 		dev_warn(&pdev->dev, "could not find MAC address property: %d\n",
3094 			 ret);
3095 		axienet_set_mac_address(ndev, NULL);
3096 	}
3097 
3098 	spin_lock_init(&lp->rx_cr_lock);
3099 	spin_lock_init(&lp->tx_cr_lock);
3100 	spin_lock_init(&lp->rx_submit_lock);
3101 	INIT_WORK(&lp->rx_dim.work, axienet_rx_dim_work);
3102 	lp->rx_dim_enabled = true;
3103 	lp->rx_dim.profile_ix = 1;
3104 	lp->rx_dma_cr = axienet_calc_cr(lp, axienet_dim_coalesce_count_rx(lp),
3105 					XAXIDMA_DFT_RX_USEC);
3106 	lp->tx_dma_cr = axienet_calc_cr(lp, XAXIDMA_DFT_TX_THRESHOLD,
3107 					XAXIDMA_DFT_TX_USEC);
3108 
3109 	ret = axienet_mdio_setup(lp);
3110 	if (ret)
3111 		dev_warn(&pdev->dev,
3112 			 "error registering MDIO bus: %d\n", ret);
3113 
3114 	if (lp->phy_mode == PHY_INTERFACE_MODE_SGMII ||
3115 	    lp->phy_mode == PHY_INTERFACE_MODE_1000BASEX) {
3116 		np = of_parse_phandle(pdev->dev.of_node, "pcs-handle", 0);
3117 		if (!np) {
3118 			/* Deprecated: Always use "pcs-handle" for pcs_phy.
3119 			 * Falling back to "phy-handle" here is only for
3120 			 * backward compatibility with old device trees.
3121 			 */
3122 			np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
3123 		}
3124 		if (!np) {
3125 			dev_err(&pdev->dev, "pcs-handle (preferred) or phy-handle required for 1000BaseX/SGMII\n");
3126 			ret = -EINVAL;
3127 			goto cleanup_mdio;
3128 		}
3129 		lp->pcs_phy = of_mdio_find_device(np);
3130 		if (!lp->pcs_phy) {
3131 			ret = -EPROBE_DEFER;
3132 			of_node_put(np);
3133 			goto cleanup_mdio;
3134 		}
3135 		of_node_put(np);
3136 		lp->pcs.ops = &axienet_pcs_ops;
3137 		lp->pcs.poll = true;
3138 	}
3139 
3140 	lp->phylink_config.dev = &ndev->dev;
3141 	lp->phylink_config.type = PHYLINK_NETDEV;
3142 	lp->phylink_config.mac_managed_pm = true;
3143 	lp->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE |
3144 		MAC_10FD | MAC_100FD | MAC_1000FD;
3145 
3146 	__set_bit(lp->phy_mode, lp->phylink_config.supported_interfaces);
3147 	if (lp->switch_x_sgmii) {
3148 		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
3149 			  lp->phylink_config.supported_interfaces);
3150 		__set_bit(PHY_INTERFACE_MODE_SGMII,
3151 			  lp->phylink_config.supported_interfaces);
3152 	}
3153 
3154 	lp->phylink = phylink_create(&lp->phylink_config, pdev->dev.fwnode,
3155 				     lp->phy_mode,
3156 				     &axienet_phylink_ops);
3157 	if (IS_ERR(lp->phylink)) {
3158 		ret = PTR_ERR(lp->phylink);
3159 		dev_err(&pdev->dev, "phylink_create error (%i)\n", ret);
3160 		goto cleanup_mdio;
3161 	}
3162 
3163 	ret = register_netdev(lp->ndev);
3164 	if (ret) {
3165 		dev_err(lp->dev, "register_netdev() error (%i)\n", ret);
3166 		goto cleanup_phylink;
3167 	}
3168 
3169 	return 0;
3170 
3171 cleanup_phylink:
3172 	phylink_destroy(lp->phylink);
3173 
3174 cleanup_mdio:
3175 	if (lp->pcs_phy)
3176 		put_device(&lp->pcs_phy->dev);
3177 	if (lp->mii_bus)
3178 		axienet_mdio_teardown(lp);
3179 	return ret;
3180 }
3181 
axienet_remove(struct platform_device * pdev)3182 static void axienet_remove(struct platform_device *pdev)
3183 {
3184 	struct net_device *ndev = platform_get_drvdata(pdev);
3185 	struct axienet_local *lp = netdev_priv(ndev);
3186 
3187 	unregister_netdev(ndev);
3188 
3189 	if (lp->phylink)
3190 		phylink_destroy(lp->phylink);
3191 
3192 	if (lp->pcs_phy)
3193 		put_device(&lp->pcs_phy->dev);
3194 
3195 	axienet_mdio_teardown(lp);
3196 }
3197 
axienet_shutdown(struct platform_device * pdev)3198 static void axienet_shutdown(struct platform_device *pdev)
3199 {
3200 	struct net_device *ndev = platform_get_drvdata(pdev);
3201 
3202 	rtnl_lock();
3203 	netif_device_detach(ndev);
3204 
3205 	if (netif_running(ndev))
3206 		dev_close(ndev);
3207 
3208 	rtnl_unlock();
3209 }
3210 
axienet_suspend(struct device * dev)3211 static int axienet_suspend(struct device *dev)
3212 {
3213 	struct net_device *ndev = dev_get_drvdata(dev);
3214 
3215 	if (!netif_running(ndev))
3216 		return 0;
3217 
3218 	netif_device_detach(ndev);
3219 
3220 	rtnl_lock();
3221 	axienet_stop(ndev);
3222 	rtnl_unlock();
3223 
3224 	return 0;
3225 }
3226 
axienet_resume(struct device * dev)3227 static int axienet_resume(struct device *dev)
3228 {
3229 	struct net_device *ndev = dev_get_drvdata(dev);
3230 
3231 	if (!netif_running(ndev))
3232 		return 0;
3233 
3234 	rtnl_lock();
3235 	axienet_open(ndev);
3236 	rtnl_unlock();
3237 
3238 	netif_device_attach(ndev);
3239 
3240 	return 0;
3241 }
3242 
3243 static DEFINE_SIMPLE_DEV_PM_OPS(axienet_pm_ops,
3244 				axienet_suspend, axienet_resume);
3245 
3246 static struct platform_driver axienet_driver = {
3247 	.probe = axienet_probe,
3248 	.remove = axienet_remove,
3249 	.shutdown = axienet_shutdown,
3250 	.driver = {
3251 		 .name = "xilinx_axienet",
3252 		 .pm = &axienet_pm_ops,
3253 		 .of_match_table = axienet_of_match,
3254 	},
3255 };
3256 
3257 module_platform_driver(axienet_driver);
3258 
3259 MODULE_DESCRIPTION("Xilinx Axi Ethernet driver");
3260 MODULE_AUTHOR("Xilinx");
3261 MODULE_LICENSE("GPL");
3262