xref: /linux/drivers/net/ethernet/amd/xgbe/xgbe-drv.c (revision 515c0ead788f4118a91b3ae55fe51f95543553ec)
1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-3-Clause)
2 /*
3  * Copyright (c) 2014-2025, Advanced Micro Devices, Inc.
4  * Copyright (c) 2014, Synopsys, Inc.
5  * All rights reserved
6  */
7 
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
10 #include <linux/tcp.h>
11 #include <linux/if_vlan.h>
12 #include <linux/interrupt.h>
13 #include <linux/clk.h>
14 #include <linux/if_ether.h>
15 #include <linux/net_tstamp.h>
16 #include <linux/phy.h>
17 #include <net/vxlan.h>
18 
19 #include "xgbe.h"
20 #include "xgbe-common.h"
21 
22 static unsigned int ecc_sec_info_threshold = 10;
23 static unsigned int ecc_sec_warn_threshold = 10000;
24 static unsigned int ecc_sec_period = 600;
25 static unsigned int ecc_ded_threshold = 2;
26 static unsigned int ecc_ded_period = 600;
27 
28 #ifdef CONFIG_AMD_XGBE_HAVE_ECC
29 /* Only expose the ECC parameters if supported */
30 module_param(ecc_sec_info_threshold, uint, 0644);
31 MODULE_PARM_DESC(ecc_sec_info_threshold,
32 		 " ECC corrected error informational threshold setting");
33 
34 module_param(ecc_sec_warn_threshold, uint, 0644);
35 MODULE_PARM_DESC(ecc_sec_warn_threshold,
36 		 " ECC corrected error warning threshold setting");
37 
38 module_param(ecc_sec_period, uint, 0644);
39 MODULE_PARM_DESC(ecc_sec_period, " ECC corrected error period (in seconds)");
40 
41 module_param(ecc_ded_threshold, uint, 0644);
42 MODULE_PARM_DESC(ecc_ded_threshold, " ECC detected error threshold setting");
43 
44 module_param(ecc_ded_period, uint, 0644);
45 MODULE_PARM_DESC(ecc_ded_period, " ECC detected error period (in seconds)");
46 #endif
47 
48 static int xgbe_one_poll(struct napi_struct *, int);
49 static int xgbe_all_poll(struct napi_struct *, int);
50 static void xgbe_stop(struct xgbe_prv_data *);
51 
52 static void *xgbe_alloc_node(size_t size, int node)
53 {
54 	void *mem;
55 
56 	mem = kzalloc_node(size, GFP_KERNEL, node);
57 	if (!mem)
58 		mem = kzalloc(size, GFP_KERNEL);
59 
60 	return mem;
61 }
62 
63 static void xgbe_free_channels(struct xgbe_prv_data *pdata)
64 {
65 	unsigned int i;
66 
67 	for (i = 0; i < ARRAY_SIZE(pdata->channel); i++) {
68 		if (!pdata->channel[i])
69 			continue;
70 
71 		kfree(pdata->channel[i]->rx_ring);
72 		kfree(pdata->channel[i]->tx_ring);
73 		kfree(pdata->channel[i]);
74 
75 		pdata->channel[i] = NULL;
76 	}
77 
78 	pdata->channel_count = 0;
79 }
80 
81 static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
82 {
83 	struct xgbe_channel *channel;
84 	struct xgbe_ring *ring;
85 	unsigned int count, i;
86 	unsigned int cpu;
87 	int node;
88 
89 	count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
90 	for (i = 0; i < count; i++) {
91 		/* Attempt to use a CPU on the node the device is on */
92 		cpu = cpumask_local_spread(i, dev_to_node(pdata->dev));
93 
94 		/* Set the allocation node based on the returned CPU */
95 		node = cpu_to_node(cpu);
96 
97 		channel = xgbe_alloc_node(sizeof(*channel), node);
98 		if (!channel)
99 			goto err_mem;
100 		pdata->channel[i] = channel;
101 
102 		snprintf(channel->name, sizeof(channel->name), "channel-%u", i);
103 		channel->pdata = pdata;
104 		channel->queue_index = i;
105 		channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
106 				    (DMA_CH_INC * i);
107 		channel->node = node;
108 		cpumask_set_cpu(cpu, &channel->affinity_mask);
109 
110 		if (pdata->per_channel_irq)
111 			channel->dma_irq = pdata->channel_irq[i];
112 
113 		if (i < pdata->tx_ring_count) {
114 			ring = xgbe_alloc_node(sizeof(*ring), node);
115 			if (!ring)
116 				goto err_mem;
117 
118 			spin_lock_init(&ring->lock);
119 			ring->node = node;
120 
121 			channel->tx_ring = ring;
122 		}
123 
124 		if (i < pdata->rx_ring_count) {
125 			ring = xgbe_alloc_node(sizeof(*ring), node);
126 			if (!ring)
127 				goto err_mem;
128 
129 			spin_lock_init(&ring->lock);
130 			ring->node = node;
131 
132 			channel->rx_ring = ring;
133 		}
134 
135 		netif_dbg(pdata, drv, pdata->netdev,
136 			  "%s: cpu=%u, node=%d\n", channel->name, cpu, node);
137 
138 		netif_dbg(pdata, drv, pdata->netdev,
139 			  "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
140 			  channel->name, channel->dma_regs, channel->dma_irq,
141 			  channel->tx_ring, channel->rx_ring);
142 	}
143 
144 	pdata->channel_count = count;
145 
146 	return 0;
147 
148 err_mem:
149 	xgbe_free_channels(pdata);
150 
151 	return -ENOMEM;
152 }
153 
154 static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
155 {
156 	return (ring->rdesc_count - (ring->cur - ring->dirty));
157 }
158 
159 static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
160 {
161 	return (ring->cur - ring->dirty);
162 }
163 
164 static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
165 				    struct xgbe_ring *ring, unsigned int count)
166 {
167 	struct xgbe_prv_data *pdata = channel->pdata;
168 
169 	if (count > xgbe_tx_avail_desc(ring)) {
170 		netif_info(pdata, drv, pdata->netdev,
171 			   "Tx queue stopped, not enough descriptors available\n");
172 		netif_stop_subqueue(pdata->netdev, channel->queue_index);
173 		ring->tx.queue_stopped = 1;
174 
175 		/* If we haven't notified the hardware because of xmit_more
176 		 * support, tell it now
177 		 */
178 		if (ring->tx.xmit_more)
179 			pdata->hw_if.tx_start_xmit(channel, ring);
180 
181 		return NETDEV_TX_BUSY;
182 	}
183 
184 	return 0;
185 }
186 
187 static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
188 {
189 	unsigned int rx_buf_size;
190 
191 	rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
192 	rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
193 
194 	rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
195 		      ~(XGBE_RX_BUF_ALIGN - 1);
196 
197 	return rx_buf_size;
198 }
199 
200 static void xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata,
201 				  struct xgbe_channel *channel)
202 {
203 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
204 	enum xgbe_int int_id;
205 
206 	if (channel->tx_ring && channel->rx_ring)
207 		int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
208 	else if (channel->tx_ring)
209 		int_id = XGMAC_INT_DMA_CH_SR_TI;
210 	else if (channel->rx_ring)
211 		int_id = XGMAC_INT_DMA_CH_SR_RI;
212 	else
213 		return;
214 
215 	hw_if->enable_int(channel, int_id);
216 }
217 
218 static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
219 {
220 	unsigned int i;
221 
222 	for (i = 0; i < pdata->channel_count; i++)
223 		xgbe_enable_rx_tx_int(pdata, pdata->channel[i]);
224 }
225 
226 static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata,
227 				   struct xgbe_channel *channel)
228 {
229 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
230 	enum xgbe_int int_id;
231 
232 	if (channel->tx_ring && channel->rx_ring)
233 		int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
234 	else if (channel->tx_ring)
235 		int_id = XGMAC_INT_DMA_CH_SR_TI;
236 	else if (channel->rx_ring)
237 		int_id = XGMAC_INT_DMA_CH_SR_RI;
238 	else
239 		return;
240 
241 	hw_if->disable_int(channel, int_id);
242 }
243 
244 static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
245 {
246 	unsigned int i;
247 
248 	for (i = 0; i < pdata->channel_count; i++)
249 		xgbe_disable_rx_tx_int(pdata, pdata->channel[i]);
250 }
251 
252 static bool xgbe_ecc_sec(struct xgbe_prv_data *pdata, unsigned long *period,
253 			 unsigned int *count, const char *area)
254 {
255 	if (time_before(jiffies, *period)) {
256 		(*count)++;
257 	} else {
258 		*period = jiffies + (ecc_sec_period * HZ);
259 		*count = 1;
260 	}
261 
262 	if (*count > ecc_sec_info_threshold)
263 		dev_warn_once(pdata->dev,
264 			      "%s ECC corrected errors exceed informational threshold\n",
265 			      area);
266 
267 	if (*count > ecc_sec_warn_threshold) {
268 		dev_warn_once(pdata->dev,
269 			      "%s ECC corrected errors exceed warning threshold\n",
270 			      area);
271 		return true;
272 	}
273 
274 	return false;
275 }
276 
277 static bool xgbe_ecc_ded(struct xgbe_prv_data *pdata, unsigned long *period,
278 			 unsigned int *count, const char *area)
279 {
280 	if (time_before(jiffies, *period)) {
281 		(*count)++;
282 	} else {
283 		*period = jiffies + (ecc_ded_period * HZ);
284 		*count = 1;
285 	}
286 
287 	if (*count > ecc_ded_threshold) {
288 		netdev_alert(pdata->netdev,
289 			     "%s ECC detected errors exceed threshold\n",
290 			     area);
291 		return true;
292 	}
293 
294 	return false;
295 }
296 
297 static void xgbe_ecc_isr_bh_work(struct work_struct *work)
298 {
299 	struct xgbe_prv_data *pdata = from_work(pdata, work, ecc_bh_work);
300 	unsigned int ecc_isr;
301 	bool stop = false;
302 
303 	/* Mask status with only the interrupts we care about */
304 	ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
305 	ecc_isr &= XP_IOREAD(pdata, XP_ECC_IER);
306 	netif_dbg(pdata, intr, pdata->netdev, "ECC_ISR=%#010x\n", ecc_isr);
307 
308 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_DED)) {
309 		stop |= xgbe_ecc_ded(pdata, &pdata->tx_ded_period,
310 				     &pdata->tx_ded_count, "TX fifo");
311 	}
312 
313 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_DED)) {
314 		stop |= xgbe_ecc_ded(pdata, &pdata->rx_ded_period,
315 				     &pdata->rx_ded_count, "RX fifo");
316 	}
317 
318 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_DED)) {
319 		stop |= xgbe_ecc_ded(pdata, &pdata->desc_ded_period,
320 				     &pdata->desc_ded_count,
321 				     "descriptor cache");
322 	}
323 
324 	if (stop) {
325 		pdata->hw_if.disable_ecc_ded(pdata);
326 		schedule_work(&pdata->stopdev_work);
327 		goto out;
328 	}
329 
330 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_SEC)) {
331 		if (xgbe_ecc_sec(pdata, &pdata->tx_sec_period,
332 				 &pdata->tx_sec_count, "TX fifo"))
333 			pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_TX);
334 	}
335 
336 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_SEC))
337 		if (xgbe_ecc_sec(pdata, &pdata->rx_sec_period,
338 				 &pdata->rx_sec_count, "RX fifo"))
339 			pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_RX);
340 
341 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_SEC))
342 		if (xgbe_ecc_sec(pdata, &pdata->desc_sec_period,
343 				 &pdata->desc_sec_count, "descriptor cache"))
344 			pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_DESC);
345 
346 out:
347 	/* Clear all ECC interrupts */
348 	XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
349 
350 	/* Reissue interrupt if status is not clear */
351 	if (pdata->vdata->irq_reissue_support)
352 		XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 1);
353 }
354 
355 static irqreturn_t xgbe_ecc_isr(int irq, void *data)
356 {
357 	struct xgbe_prv_data *pdata = data;
358 
359 	if (pdata->isr_as_bh_work)
360 		queue_work(system_bh_wq, &pdata->ecc_bh_work);
361 	else
362 		xgbe_ecc_isr_bh_work(&pdata->ecc_bh_work);
363 
364 	return IRQ_HANDLED;
365 }
366 
367 static void xgbe_isr_bh_work(struct work_struct *work)
368 {
369 	struct xgbe_prv_data *pdata = from_work(pdata, work, dev_bh_work);
370 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
371 	struct xgbe_channel *channel;
372 	unsigned int dma_isr, dma_ch_isr;
373 	unsigned int mac_isr, mac_tssr, mac_mdioisr;
374 	unsigned int i;
375 
376 	/* The DMA interrupt status register also reports MAC and MTL
377 	 * interrupts. So for polling mode, we just need to check for
378 	 * this register to be non-zero
379 	 */
380 	dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
381 	if (!dma_isr)
382 		goto isr_done;
383 
384 	netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
385 
386 	for (i = 0; i < pdata->channel_count; i++) {
387 		if (!(dma_isr & (1 << i)))
388 			continue;
389 
390 		channel = pdata->channel[i];
391 
392 		dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
393 		netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
394 			  i, dma_ch_isr);
395 
396 		/* The TI or RI interrupt bits may still be set even if using
397 		 * per channel DMA interrupts. Check to be sure those are not
398 		 * enabled before using the private data napi structure.
399 		 */
400 		if (!pdata->per_channel_irq &&
401 		    (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
402 		     XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
403 			if (napi_schedule_prep(&pdata->napi)) {
404 				/* Disable Tx and Rx interrupts */
405 				xgbe_disable_rx_tx_ints(pdata);
406 
407 				/* Turn on polling */
408 				__napi_schedule(&pdata->napi);
409 			}
410 		} else {
411 			/* Don't clear Rx/Tx status if doing per channel DMA
412 			 * interrupts, these will be cleared by the ISR for
413 			 * per channel DMA interrupts.
414 			 */
415 			XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
416 			XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
417 		}
418 
419 		if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
420 			pdata->ext_stats.rx_buffer_unavailable++;
421 
422 		/* Restart the device on a Fatal Bus Error */
423 		if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
424 			schedule_work(&pdata->restart_work);
425 
426 		/* Clear interrupt signals */
427 		XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
428 	}
429 
430 	if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
431 		mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
432 
433 		netif_dbg(pdata, intr, pdata->netdev, "MAC_ISR=%#010x\n",
434 			  mac_isr);
435 
436 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
437 			hw_if->tx_mmc_int(pdata);
438 
439 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
440 			hw_if->rx_mmc_int(pdata);
441 
442 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
443 			mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
444 
445 			netif_dbg(pdata, intr, pdata->netdev,
446 				  "MAC_TSSR=%#010x\n", mac_tssr);
447 
448 			if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
449 				/* Read Tx Timestamp to clear interrupt */
450 				pdata->tx_tstamp =
451 					xgbe_get_tx_tstamp(pdata);
452 				queue_work(pdata->dev_workqueue,
453 					   &pdata->tx_tstamp_work);
454 			}
455 		}
456 
457 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
458 			mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
459 
460 			netif_dbg(pdata, intr, pdata->netdev,
461 				  "MAC_MDIOISR=%#010x\n", mac_mdioisr);
462 
463 			if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
464 					   SNGLCOMPINT))
465 				complete(&pdata->mdio_complete);
466 		}
467 	}
468 
469 isr_done:
470 	/* If there is not a separate AN irq, handle it here */
471 	if (pdata->dev_irq == pdata->an_irq)
472 		pdata->phy_if.an_isr(pdata);
473 
474 	/* If there is not a separate ECC irq, handle it here */
475 	if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
476 		xgbe_ecc_isr_bh_work(&pdata->ecc_bh_work);
477 
478 	/* If there is not a separate I2C irq, handle it here */
479 	if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
480 		pdata->i2c_if.i2c_isr(pdata);
481 
482 	/* Reissue interrupt if status is not clear */
483 	if (pdata->vdata->irq_reissue_support) {
484 		unsigned int reissue_mask;
485 
486 		reissue_mask = 1 << 0;
487 		if (!pdata->per_channel_irq)
488 			reissue_mask |= 0xffff << 4;
489 
490 		XP_IOWRITE(pdata, XP_INT_REISSUE_EN, reissue_mask);
491 	}
492 }
493 
494 static irqreturn_t xgbe_isr(int irq, void *data)
495 {
496 	struct xgbe_prv_data *pdata = data;
497 
498 	if (pdata->isr_as_bh_work)
499 		queue_work(system_bh_wq, &pdata->dev_bh_work);
500 	else
501 		xgbe_isr_bh_work(&pdata->dev_bh_work);
502 
503 	return IRQ_HANDLED;
504 }
505 
506 static irqreturn_t xgbe_dma_isr(int irq, void *data)
507 {
508 	struct xgbe_channel *channel = data;
509 	struct xgbe_prv_data *pdata = channel->pdata;
510 	unsigned int dma_status;
511 
512 	/* Per channel DMA interrupts are enabled, so we use the per
513 	 * channel napi structure and not the private data napi structure
514 	 */
515 	if (napi_schedule_prep(&channel->napi)) {
516 		/* Disable Tx and Rx interrupts */
517 		if (pdata->channel_irq_mode)
518 			xgbe_disable_rx_tx_int(pdata, channel);
519 		else
520 			disable_irq_nosync(channel->dma_irq);
521 
522 		/* Turn on polling */
523 		__napi_schedule_irqoff(&channel->napi);
524 	}
525 
526 	/* Clear Tx/Rx signals */
527 	dma_status = 0;
528 	XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
529 	XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
530 	XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
531 
532 	return IRQ_HANDLED;
533 }
534 
535 static void xgbe_tx_timer(struct timer_list *t)
536 {
537 	struct xgbe_channel *channel = timer_container_of(channel, t,
538 							  tx_timer);
539 	struct xgbe_prv_data *pdata = channel->pdata;
540 	struct napi_struct *napi;
541 
542 	DBGPR("-->xgbe_tx_timer\n");
543 
544 	napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
545 
546 	if (napi_schedule_prep(napi)) {
547 		/* Disable Tx and Rx interrupts */
548 		if (pdata->per_channel_irq)
549 			if (pdata->channel_irq_mode)
550 				xgbe_disable_rx_tx_int(pdata, channel);
551 			else
552 				disable_irq_nosync(channel->dma_irq);
553 		else
554 			xgbe_disable_rx_tx_ints(pdata);
555 
556 		/* Turn on polling */
557 		__napi_schedule(napi);
558 	}
559 
560 	channel->tx_timer_active = 0;
561 
562 	DBGPR("<--xgbe_tx_timer\n");
563 }
564 
565 static void xgbe_service(struct work_struct *work)
566 {
567 	struct xgbe_prv_data *pdata = container_of(work,
568 						   struct xgbe_prv_data,
569 						   service_work);
570 
571 	pdata->phy_if.phy_status(pdata);
572 }
573 
574 static void xgbe_service_timer(struct timer_list *t)
575 {
576 	struct xgbe_prv_data *pdata = timer_container_of(pdata, t,
577 							 service_timer);
578 	struct xgbe_channel *channel;
579 	unsigned int i;
580 
581 	queue_work(pdata->dev_workqueue, &pdata->service_work);
582 
583 	mod_timer(&pdata->service_timer, jiffies + HZ);
584 
585 	if (!pdata->tx_usecs)
586 		return;
587 
588 	for (i = 0; i < pdata->channel_count; i++) {
589 		channel = pdata->channel[i];
590 		if (!channel->tx_ring || channel->tx_timer_active)
591 			break;
592 		channel->tx_timer_active = 1;
593 		mod_timer(&channel->tx_timer,
594 			  jiffies + usecs_to_jiffies(pdata->tx_usecs));
595 	}
596 }
597 
598 static void xgbe_init_timers(struct xgbe_prv_data *pdata)
599 {
600 	struct xgbe_channel *channel;
601 	unsigned int i;
602 
603 	timer_setup(&pdata->service_timer, xgbe_service_timer, 0);
604 
605 	for (i = 0; i < pdata->channel_count; i++) {
606 		channel = pdata->channel[i];
607 		if (!channel->tx_ring)
608 			break;
609 
610 		timer_setup(&channel->tx_timer, xgbe_tx_timer, 0);
611 	}
612 }
613 
614 static void xgbe_start_timers(struct xgbe_prv_data *pdata)
615 {
616 	mod_timer(&pdata->service_timer, jiffies + HZ);
617 }
618 
619 static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
620 {
621 	struct xgbe_channel *channel;
622 	unsigned int i;
623 
624 	timer_delete_sync(&pdata->service_timer);
625 
626 	for (i = 0; i < pdata->channel_count; i++) {
627 		channel = pdata->channel[i];
628 		if (!channel->tx_ring)
629 			break;
630 
631 		/* Deactivate the Tx timer */
632 		timer_delete_sync(&channel->tx_timer);
633 		channel->tx_timer_active = 0;
634 	}
635 }
636 
637 void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
638 {
639 	unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
640 	struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
641 
642 	mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
643 	mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
644 	mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
645 
646 	memset(hw_feat, 0, sizeof(*hw_feat));
647 
648 	hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
649 
650 	/* Hardware feature register 0 */
651 	hw_feat->gmii        = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
652 	hw_feat->vlhash      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
653 	hw_feat->sma         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
654 	hw_feat->rwk         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
655 	hw_feat->mgk         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
656 	hw_feat->mmc         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
657 	hw_feat->aoe         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
658 	hw_feat->ts          = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
659 	hw_feat->eee         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
660 	hw_feat->tx_coe      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
661 	hw_feat->rx_coe      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
662 	hw_feat->addn_mac    = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
663 					      ADDMACADRSEL);
664 	hw_feat->ts_src      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
665 	hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
666 	hw_feat->vxn         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VXN);
667 
668 	/* Hardware feature register 1 */
669 	hw_feat->rx_fifo_size  = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
670 						RXFIFOSIZE);
671 	hw_feat->tx_fifo_size  = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
672 						TXFIFOSIZE);
673 	hw_feat->adv_ts_hi     = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
674 	hw_feat->dma_width     = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
675 	hw_feat->dcb           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
676 	hw_feat->sph           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
677 	hw_feat->tso           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
678 	hw_feat->dma_debug     = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
679 	hw_feat->rss           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
680 	hw_feat->tc_cnt	       = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
681 	hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
682 						  HASHTBLSZ);
683 	hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
684 						  L3L4FNUM);
685 
686 	/* Hardware feature register 2 */
687 	hw_feat->rx_q_cnt     = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
688 	hw_feat->tx_q_cnt     = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
689 	hw_feat->rx_ch_cnt    = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
690 	hw_feat->tx_ch_cnt    = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
691 	hw_feat->pps_out_num  = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
692 	hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
693 
694 	/* Sanity check and warn if hardware reports more than supported */
695 	if (hw_feat->pps_out_num > XGBE_MAX_PPS_OUT) {
696 		dev_warn(pdata->dev,
697 			 "Hardware reports %u PPS outputs, limiting to %u\n",
698 			 hw_feat->pps_out_num, XGBE_MAX_PPS_OUT);
699 		hw_feat->pps_out_num = XGBE_MAX_PPS_OUT;
700 	}
701 
702 	if (hw_feat->aux_snap_num > XGBE_MAX_AUX_SNAP) {
703 		dev_warn(pdata->dev,
704 			 "Hardware reports %u aux snapshot inputs, limiting to %u\n",
705 			 hw_feat->aux_snap_num, XGBE_MAX_AUX_SNAP);
706 		hw_feat->aux_snap_num = XGBE_MAX_AUX_SNAP;
707 	}
708 
709 	/* Translate the Hash Table size into actual number */
710 	switch (hw_feat->hash_table_size) {
711 	case 0:
712 		break;
713 	case 1:
714 		hw_feat->hash_table_size = 64;
715 		break;
716 	case 2:
717 		hw_feat->hash_table_size = 128;
718 		break;
719 	case 3:
720 		hw_feat->hash_table_size = 256;
721 		break;
722 	}
723 
724 	/* Translate the address width setting into actual number */
725 	switch (hw_feat->dma_width) {
726 	case 0:
727 		hw_feat->dma_width = 32;
728 		break;
729 	case 1:
730 		hw_feat->dma_width = 40;
731 		break;
732 	case 2:
733 		hw_feat->dma_width = 48;
734 		break;
735 	default:
736 		hw_feat->dma_width = 32;
737 	}
738 
739 	/* The Queue, Channel and TC counts are zero based so increment them
740 	 * to get the actual number
741 	 */
742 	hw_feat->rx_q_cnt++;
743 	hw_feat->tx_q_cnt++;
744 	hw_feat->rx_ch_cnt++;
745 	hw_feat->tx_ch_cnt++;
746 	hw_feat->tc_cnt++;
747 
748 	/* Translate the fifo sizes into actual numbers */
749 	hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
750 	hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
751 
752 	if (netif_msg_probe(pdata)) {
753 		dev_dbg(pdata->dev, "Hardware features:\n");
754 
755 		/* Hardware feature register 0 */
756 		dev_dbg(pdata->dev, "  1GbE support              : %s\n",
757 			hw_feat->gmii ? "yes" : "no");
758 		dev_dbg(pdata->dev, "  VLAN hash filter          : %s\n",
759 			hw_feat->vlhash ? "yes" : "no");
760 		dev_dbg(pdata->dev, "  MDIO interface            : %s\n",
761 			hw_feat->sma ? "yes" : "no");
762 		dev_dbg(pdata->dev, "  Wake-up packet support    : %s\n",
763 			hw_feat->rwk ? "yes" : "no");
764 		dev_dbg(pdata->dev, "  Magic packet support      : %s\n",
765 			hw_feat->mgk ? "yes" : "no");
766 		dev_dbg(pdata->dev, "  Management counters       : %s\n",
767 			hw_feat->mmc ? "yes" : "no");
768 		dev_dbg(pdata->dev, "  ARP offload               : %s\n",
769 			hw_feat->aoe ? "yes" : "no");
770 		dev_dbg(pdata->dev, "  IEEE 1588-2008 Timestamp  : %s\n",
771 			hw_feat->ts ? "yes" : "no");
772 		dev_dbg(pdata->dev, "  Energy Efficient Ethernet : %s\n",
773 			hw_feat->eee ? "yes" : "no");
774 		dev_dbg(pdata->dev, "  TX checksum offload       : %s\n",
775 			hw_feat->tx_coe ? "yes" : "no");
776 		dev_dbg(pdata->dev, "  RX checksum offload       : %s\n",
777 			hw_feat->rx_coe ? "yes" : "no");
778 		dev_dbg(pdata->dev, "  Additional MAC addresses  : %u\n",
779 			hw_feat->addn_mac);
780 		dev_dbg(pdata->dev, "  Timestamp source          : %s\n",
781 			(hw_feat->ts_src == 1) ? "internal" :
782 			(hw_feat->ts_src == 2) ? "external" :
783 			(hw_feat->ts_src == 3) ? "internal/external" : "n/a");
784 		dev_dbg(pdata->dev, "  SA/VLAN insertion         : %s\n",
785 			hw_feat->sa_vlan_ins ? "yes" : "no");
786 		dev_dbg(pdata->dev, "  VXLAN/NVGRE support       : %s\n",
787 			hw_feat->vxn ? "yes" : "no");
788 
789 		/* Hardware feature register 1 */
790 		dev_dbg(pdata->dev, "  RX fifo size              : %u\n",
791 			hw_feat->rx_fifo_size);
792 		dev_dbg(pdata->dev, "  TX fifo size              : %u\n",
793 			hw_feat->tx_fifo_size);
794 		dev_dbg(pdata->dev, "  IEEE 1588 high word       : %s\n",
795 			hw_feat->adv_ts_hi ? "yes" : "no");
796 		dev_dbg(pdata->dev, "  DMA width                 : %u\n",
797 			hw_feat->dma_width);
798 		dev_dbg(pdata->dev, "  Data Center Bridging      : %s\n",
799 			hw_feat->dcb ? "yes" : "no");
800 		dev_dbg(pdata->dev, "  Split header              : %s\n",
801 			hw_feat->sph ? "yes" : "no");
802 		dev_dbg(pdata->dev, "  TCP Segmentation Offload  : %s\n",
803 			hw_feat->tso ? "yes" : "no");
804 		dev_dbg(pdata->dev, "  Debug memory interface    : %s\n",
805 			hw_feat->dma_debug ? "yes" : "no");
806 		dev_dbg(pdata->dev, "  Receive Side Scaling      : %s\n",
807 			hw_feat->rss ? "yes" : "no");
808 		dev_dbg(pdata->dev, "  Traffic Class count       : %u\n",
809 			hw_feat->tc_cnt);
810 		dev_dbg(pdata->dev, "  Hash table size           : %u\n",
811 			hw_feat->hash_table_size);
812 		dev_dbg(pdata->dev, "  L3/L4 Filters             : %u\n",
813 			hw_feat->l3l4_filter_num);
814 
815 		/* Hardware feature register 2 */
816 		dev_dbg(pdata->dev, "  RX queue count            : %u\n",
817 			hw_feat->rx_q_cnt);
818 		dev_dbg(pdata->dev, "  TX queue count            : %u\n",
819 			hw_feat->tx_q_cnt);
820 		dev_dbg(pdata->dev, "  RX DMA channel count      : %u\n",
821 			hw_feat->rx_ch_cnt);
822 		dev_dbg(pdata->dev, "  TX DMA channel count      : %u\n",
823 			hw_feat->rx_ch_cnt);
824 		dev_dbg(pdata->dev, "  PPS outputs               : %u\n",
825 			hw_feat->pps_out_num);
826 		dev_dbg(pdata->dev, "  Auxiliary snapshot inputs : %u\n",
827 			hw_feat->aux_snap_num);
828 	}
829 }
830 
831 static int xgbe_vxlan_set_port(struct net_device *netdev, unsigned int table,
832 			       unsigned int entry, struct udp_tunnel_info *ti)
833 {
834 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
835 
836 	pdata->vxlan_port = be16_to_cpu(ti->port);
837 	pdata->hw_if.enable_vxlan(pdata);
838 
839 	return 0;
840 }
841 
842 static int xgbe_vxlan_unset_port(struct net_device *netdev, unsigned int table,
843 				 unsigned int entry, struct udp_tunnel_info *ti)
844 {
845 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
846 
847 	pdata->hw_if.disable_vxlan(pdata);
848 	pdata->vxlan_port = 0;
849 
850 	return 0;
851 }
852 
853 static const struct udp_tunnel_nic_info xgbe_udp_tunnels = {
854 	.set_port	= xgbe_vxlan_set_port,
855 	.unset_port	= xgbe_vxlan_unset_port,
856 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
857 	.tables		= {
858 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
859 	},
860 };
861 
862 const struct udp_tunnel_nic_info *xgbe_get_udp_tunnel_info(void)
863 {
864 	return &xgbe_udp_tunnels;
865 }
866 
867 static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
868 {
869 	struct xgbe_channel *channel;
870 	unsigned int i;
871 
872 	if (pdata->per_channel_irq) {
873 		for (i = 0; i < pdata->channel_count; i++) {
874 			channel = pdata->channel[i];
875 			if (add)
876 				netif_napi_add(pdata->netdev, &channel->napi,
877 					       xgbe_one_poll);
878 
879 			napi_enable(&channel->napi);
880 		}
881 	} else {
882 		if (add)
883 			netif_napi_add(pdata->netdev, &pdata->napi,
884 				       xgbe_all_poll);
885 
886 		napi_enable(&pdata->napi);
887 	}
888 }
889 
890 static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
891 {
892 	struct xgbe_channel *channel;
893 	unsigned int i;
894 
895 	if (pdata->per_channel_irq) {
896 		for (i = 0; i < pdata->channel_count; i++) {
897 			channel = pdata->channel[i];
898 			napi_disable(&channel->napi);
899 
900 			if (del)
901 				netif_napi_del(&channel->napi);
902 		}
903 	} else {
904 		napi_disable(&pdata->napi);
905 
906 		if (del)
907 			netif_napi_del(&pdata->napi);
908 	}
909 }
910 
911 static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
912 {
913 	struct xgbe_channel *channel;
914 	struct net_device *netdev = pdata->netdev;
915 	unsigned int i;
916 	int ret;
917 
918 	INIT_WORK(&pdata->dev_bh_work, xgbe_isr_bh_work);
919 	INIT_WORK(&pdata->ecc_bh_work, xgbe_ecc_isr_bh_work);
920 
921 	ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
922 			       netdev_name(netdev), pdata);
923 	if (ret) {
924 		netdev_alert(netdev, "error requesting irq %d\n",
925 			     pdata->dev_irq);
926 		return ret;
927 	}
928 
929 	if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
930 		ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
931 				       0, pdata->ecc_name, pdata);
932 		if (ret) {
933 			netdev_alert(netdev, "error requesting ecc irq %d\n",
934 				     pdata->ecc_irq);
935 			goto err_dev_irq;
936 		}
937 	}
938 
939 	if (!pdata->per_channel_irq)
940 		return 0;
941 
942 	for (i = 0; i < pdata->channel_count; i++) {
943 		channel = pdata->channel[i];
944 		snprintf(channel->dma_irq_name,
945 			 sizeof(channel->dma_irq_name) - 1,
946 			 "%s-TxRx-%u", netdev_name(netdev),
947 			 channel->queue_index);
948 
949 		ret = devm_request_irq(pdata->dev, channel->dma_irq,
950 				       xgbe_dma_isr, 0,
951 				       channel->dma_irq_name, channel);
952 		if (ret) {
953 			netdev_alert(netdev, "error requesting irq %d\n",
954 				     channel->dma_irq);
955 			goto err_dma_irq;
956 		}
957 
958 		irq_set_affinity_hint(channel->dma_irq,
959 				      &channel->affinity_mask);
960 	}
961 
962 	return 0;
963 
964 err_dma_irq:
965 	/* Using an unsigned int, 'i' will go to UINT_MAX and exit */
966 	for (i--; i < pdata->channel_count; i--) {
967 		channel = pdata->channel[i];
968 
969 		irq_set_affinity_hint(channel->dma_irq, NULL);
970 		devm_free_irq(pdata->dev, channel->dma_irq, channel);
971 	}
972 
973 	if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
974 		devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
975 
976 err_dev_irq:
977 	devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
978 
979 	return ret;
980 }
981 
982 static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
983 {
984 	struct xgbe_channel *channel;
985 	unsigned int i;
986 
987 	devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
988 
989 	cancel_work_sync(&pdata->dev_bh_work);
990 	cancel_work_sync(&pdata->ecc_bh_work);
991 
992 	if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
993 		devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
994 
995 	if (!pdata->per_channel_irq)
996 		return;
997 
998 	for (i = 0; i < pdata->channel_count; i++) {
999 		channel = pdata->channel[i];
1000 
1001 		irq_set_affinity_hint(channel->dma_irq, NULL);
1002 		devm_free_irq(pdata->dev, channel->dma_irq, channel);
1003 	}
1004 }
1005 
1006 void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
1007 {
1008 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1009 
1010 	DBGPR("-->xgbe_init_tx_coalesce\n");
1011 
1012 	pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
1013 	pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
1014 
1015 	hw_if->config_tx_coalesce(pdata);
1016 
1017 	DBGPR("<--xgbe_init_tx_coalesce\n");
1018 }
1019 
1020 void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
1021 {
1022 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1023 
1024 	DBGPR("-->xgbe_init_rx_coalesce\n");
1025 
1026 	pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
1027 	pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
1028 	pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
1029 
1030 	hw_if->config_rx_coalesce(pdata);
1031 
1032 	DBGPR("<--xgbe_init_rx_coalesce\n");
1033 }
1034 
1035 static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
1036 {
1037 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1038 	struct xgbe_ring *ring;
1039 	struct xgbe_ring_data *rdata;
1040 	unsigned int i, j;
1041 
1042 	DBGPR("-->xgbe_free_tx_data\n");
1043 
1044 	for (i = 0; i < pdata->channel_count; i++) {
1045 		ring = pdata->channel[i]->tx_ring;
1046 		if (!ring)
1047 			break;
1048 
1049 		for (j = 0; j < ring->rdesc_count; j++) {
1050 			rdata = XGBE_GET_DESC_DATA(ring, j);
1051 			desc_if->unmap_rdata(pdata, rdata);
1052 		}
1053 	}
1054 
1055 	DBGPR("<--xgbe_free_tx_data\n");
1056 }
1057 
1058 static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
1059 {
1060 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1061 	struct xgbe_ring *ring;
1062 	struct xgbe_ring_data *rdata;
1063 	unsigned int i, j;
1064 
1065 	DBGPR("-->xgbe_free_rx_data\n");
1066 
1067 	for (i = 0; i < pdata->channel_count; i++) {
1068 		ring = pdata->channel[i]->rx_ring;
1069 		if (!ring)
1070 			break;
1071 
1072 		for (j = 0; j < ring->rdesc_count; j++) {
1073 			rdata = XGBE_GET_DESC_DATA(ring, j);
1074 			desc_if->unmap_rdata(pdata, rdata);
1075 		}
1076 	}
1077 
1078 	DBGPR("<--xgbe_free_rx_data\n");
1079 }
1080 
1081 static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
1082 {
1083 	pdata->phy_link = -1;
1084 	pdata->phy_speed = SPEED_UNKNOWN;
1085 
1086 	return pdata->phy_if.phy_reset(pdata);
1087 }
1088 
1089 int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
1090 {
1091 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1092 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1093 	unsigned long flags;
1094 
1095 	DBGPR("-->xgbe_powerdown\n");
1096 
1097 	if (!netif_running(netdev) ||
1098 	    (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
1099 		netdev_alert(netdev, "Device is already powered down\n");
1100 		DBGPR("<--xgbe_powerdown\n");
1101 		return -EINVAL;
1102 	}
1103 
1104 	spin_lock_irqsave(&pdata->lock, flags);
1105 
1106 	if (caller == XGMAC_DRIVER_CONTEXT)
1107 		netif_device_detach(netdev);
1108 
1109 	netif_tx_stop_all_queues(netdev);
1110 
1111 	xgbe_stop_timers(pdata);
1112 	flush_workqueue(pdata->dev_workqueue);
1113 
1114 	hw_if->powerdown_tx(pdata);
1115 	hw_if->powerdown_rx(pdata);
1116 
1117 	xgbe_napi_disable(pdata, 0);
1118 
1119 	pdata->power_down = 1;
1120 
1121 	spin_unlock_irqrestore(&pdata->lock, flags);
1122 
1123 	DBGPR("<--xgbe_powerdown\n");
1124 
1125 	return 0;
1126 }
1127 
1128 int xgbe_powerup(struct net_device *netdev, unsigned int caller)
1129 {
1130 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1131 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1132 	unsigned long flags;
1133 
1134 	DBGPR("-->xgbe_powerup\n");
1135 
1136 	if (!netif_running(netdev) ||
1137 	    (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
1138 		netdev_alert(netdev, "Device is already powered up\n");
1139 		DBGPR("<--xgbe_powerup\n");
1140 		return -EINVAL;
1141 	}
1142 
1143 	spin_lock_irqsave(&pdata->lock, flags);
1144 
1145 	pdata->power_down = 0;
1146 
1147 	xgbe_napi_enable(pdata, 0);
1148 
1149 	hw_if->powerup_tx(pdata);
1150 	hw_if->powerup_rx(pdata);
1151 
1152 	if (caller == XGMAC_DRIVER_CONTEXT)
1153 		netif_device_attach(netdev);
1154 
1155 	netif_tx_start_all_queues(netdev);
1156 
1157 	xgbe_start_timers(pdata);
1158 
1159 	spin_unlock_irqrestore(&pdata->lock, flags);
1160 
1161 	DBGPR("<--xgbe_powerup\n");
1162 
1163 	return 0;
1164 }
1165 
1166 static void xgbe_free_memory(struct xgbe_prv_data *pdata)
1167 {
1168 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1169 
1170 	/* Free the ring descriptors and buffers */
1171 	desc_if->free_ring_resources(pdata);
1172 
1173 	/* Free the channel and ring structures */
1174 	xgbe_free_channels(pdata);
1175 }
1176 
1177 static int xgbe_alloc_memory(struct xgbe_prv_data *pdata)
1178 {
1179 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1180 	struct net_device *netdev = pdata->netdev;
1181 	int ret;
1182 
1183 	if (pdata->new_tx_ring_count) {
1184 		pdata->tx_ring_count = pdata->new_tx_ring_count;
1185 		pdata->tx_q_count = pdata->tx_ring_count;
1186 		pdata->new_tx_ring_count = 0;
1187 	}
1188 
1189 	if (pdata->new_rx_ring_count) {
1190 		pdata->rx_ring_count = pdata->new_rx_ring_count;
1191 		pdata->new_rx_ring_count = 0;
1192 	}
1193 
1194 	/* Calculate the Rx buffer size before allocating rings */
1195 	pdata->rx_buf_size = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1196 
1197 	/* Allocate the channel and ring structures */
1198 	ret = xgbe_alloc_channels(pdata);
1199 	if (ret)
1200 		return ret;
1201 
1202 	/* Allocate the ring descriptors and buffers */
1203 	ret = desc_if->alloc_ring_resources(pdata);
1204 	if (ret)
1205 		goto err_channels;
1206 
1207 	/* Initialize the service and Tx timers */
1208 	xgbe_init_timers(pdata);
1209 
1210 	return 0;
1211 
1212 err_channels:
1213 	xgbe_free_memory(pdata);
1214 
1215 	return ret;
1216 }
1217 
1218 static int xgbe_start(struct xgbe_prv_data *pdata)
1219 {
1220 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1221 	struct xgbe_phy_if *phy_if = &pdata->phy_if;
1222 	struct net_device *netdev = pdata->netdev;
1223 	unsigned int i;
1224 	int ret;
1225 
1226 	/* Set the number of queues */
1227 	ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
1228 	if (ret) {
1229 		netdev_err(netdev, "error setting real tx queue count\n");
1230 		return ret;
1231 	}
1232 
1233 	ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
1234 	if (ret) {
1235 		netdev_err(netdev, "error setting real rx queue count\n");
1236 		return ret;
1237 	}
1238 
1239 	/* Set RSS lookup table data for programming */
1240 	for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
1241 		XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
1242 			       i % pdata->rx_ring_count);
1243 
1244 	ret = hw_if->init(pdata);
1245 	if (ret)
1246 		return ret;
1247 
1248 	xgbe_napi_enable(pdata, 1);
1249 
1250 	ret = xgbe_request_irqs(pdata);
1251 	if (ret)
1252 		goto err_napi;
1253 
1254 	ret = phy_if->phy_start(pdata);
1255 	if (ret)
1256 		goto err_irqs;
1257 
1258 	hw_if->enable_tx(pdata);
1259 	hw_if->enable_rx(pdata);
1260 
1261 	udp_tunnel_nic_reset_ntf(netdev);
1262 
1263 	netif_tx_start_all_queues(netdev);
1264 
1265 	xgbe_start_timers(pdata);
1266 	queue_work(pdata->dev_workqueue, &pdata->service_work);
1267 
1268 	clear_bit(XGBE_STOPPED, &pdata->dev_state);
1269 
1270 	return 0;
1271 
1272 err_irqs:
1273 	xgbe_free_irqs(pdata);
1274 
1275 err_napi:
1276 	xgbe_napi_disable(pdata, 1);
1277 
1278 	hw_if->exit(pdata);
1279 
1280 	return ret;
1281 }
1282 
1283 static void xgbe_stop(struct xgbe_prv_data *pdata)
1284 {
1285 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1286 	struct xgbe_phy_if *phy_if = &pdata->phy_if;
1287 	struct xgbe_channel *channel;
1288 	struct net_device *netdev = pdata->netdev;
1289 	struct netdev_queue *txq;
1290 	unsigned int i;
1291 
1292 	DBGPR("-->xgbe_stop\n");
1293 
1294 	if (test_bit(XGBE_STOPPED, &pdata->dev_state))
1295 		return;
1296 
1297 	netif_tx_stop_all_queues(netdev);
1298 	netif_carrier_off(pdata->netdev);
1299 
1300 	xgbe_stop_timers(pdata);
1301 	flush_workqueue(pdata->dev_workqueue);
1302 
1303 	xgbe_vxlan_unset_port(netdev, 0, 0, NULL);
1304 
1305 	hw_if->disable_tx(pdata);
1306 	hw_if->disable_rx(pdata);
1307 
1308 	phy_if->phy_stop(pdata);
1309 
1310 	xgbe_free_irqs(pdata);
1311 
1312 	xgbe_napi_disable(pdata, 1);
1313 
1314 	hw_if->exit(pdata);
1315 
1316 	for (i = 0; i < pdata->channel_count; i++) {
1317 		channel = pdata->channel[i];
1318 		if (!channel->tx_ring)
1319 			continue;
1320 
1321 		txq = netdev_get_tx_queue(netdev, channel->queue_index);
1322 		netdev_tx_reset_queue(txq);
1323 	}
1324 
1325 	set_bit(XGBE_STOPPED, &pdata->dev_state);
1326 
1327 	DBGPR("<--xgbe_stop\n");
1328 }
1329 
1330 static void xgbe_stopdev(struct work_struct *work)
1331 {
1332 	struct xgbe_prv_data *pdata = container_of(work,
1333 						   struct xgbe_prv_data,
1334 						   stopdev_work);
1335 
1336 	rtnl_lock();
1337 
1338 	xgbe_stop(pdata);
1339 
1340 	xgbe_free_tx_data(pdata);
1341 	xgbe_free_rx_data(pdata);
1342 
1343 	rtnl_unlock();
1344 
1345 	netdev_alert(pdata->netdev, "device stopped\n");
1346 }
1347 
1348 void xgbe_full_restart_dev(struct xgbe_prv_data *pdata)
1349 {
1350 	/* If not running, "restart" will happen on open */
1351 	if (!netif_running(pdata->netdev))
1352 		return;
1353 
1354 	xgbe_stop(pdata);
1355 
1356 	xgbe_free_memory(pdata);
1357 	xgbe_alloc_memory(pdata);
1358 
1359 	xgbe_start(pdata);
1360 }
1361 
1362 void xgbe_restart_dev(struct xgbe_prv_data *pdata)
1363 {
1364 	/* If not running, "restart" will happen on open */
1365 	if (!netif_running(pdata->netdev))
1366 		return;
1367 
1368 	xgbe_stop(pdata);
1369 
1370 	xgbe_free_tx_data(pdata);
1371 	xgbe_free_rx_data(pdata);
1372 
1373 	xgbe_start(pdata);
1374 }
1375 
1376 static void xgbe_restart(struct work_struct *work)
1377 {
1378 	struct xgbe_prv_data *pdata = container_of(work,
1379 						   struct xgbe_prv_data,
1380 						   restart_work);
1381 
1382 	rtnl_lock();
1383 
1384 	xgbe_restart_dev(pdata);
1385 
1386 	rtnl_unlock();
1387 }
1388 
1389 static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1390 {
1391 	if (skb_vlan_tag_present(skb))
1392 		packet->vlan_ctag = skb_vlan_tag_get(skb);
1393 }
1394 
1395 static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1396 {
1397 	int ret;
1398 
1399 	if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1400 			    TSO_ENABLE))
1401 		return 0;
1402 
1403 	ret = skb_cow_head(skb, 0);
1404 	if (ret)
1405 		return ret;
1406 
1407 	if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, VXLAN)) {
1408 		packet->header_len = skb_inner_tcp_all_headers(skb);
1409 		packet->tcp_header_len = inner_tcp_hdrlen(skb);
1410 	} else {
1411 		packet->header_len = skb_tcp_all_headers(skb);
1412 		packet->tcp_header_len = tcp_hdrlen(skb);
1413 	}
1414 	packet->tcp_payload_len = skb->len - packet->header_len;
1415 	packet->mss = skb_shinfo(skb)->gso_size;
1416 
1417 	DBGPR("  packet->header_len=%u\n", packet->header_len);
1418 	DBGPR("  packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1419 	      packet->tcp_header_len, packet->tcp_payload_len);
1420 	DBGPR("  packet->mss=%u\n", packet->mss);
1421 
1422 	/* Update the number of packets that will ultimately be transmitted
1423 	 * along with the extra bytes for each extra packet
1424 	 */
1425 	packet->tx_packets = skb_shinfo(skb)->gso_segs;
1426 	packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1427 
1428 	return 0;
1429 }
1430 
1431 static bool xgbe_is_vxlan(struct sk_buff *skb)
1432 {
1433 	if (!skb->encapsulation)
1434 		return false;
1435 
1436 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1437 		return false;
1438 
1439 	switch (skb->protocol) {
1440 	case htons(ETH_P_IP):
1441 		if (ip_hdr(skb)->protocol != IPPROTO_UDP)
1442 			return false;
1443 		break;
1444 
1445 	case htons(ETH_P_IPV6):
1446 		if (ipv6_hdr(skb)->nexthdr != IPPROTO_UDP)
1447 			return false;
1448 		break;
1449 
1450 	default:
1451 		return false;
1452 	}
1453 
1454 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
1455 	    skb->inner_protocol != htons(ETH_P_TEB) ||
1456 	    (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
1457 	     sizeof(struct udphdr) + sizeof(struct vxlanhdr)))
1458 		return false;
1459 
1460 	return true;
1461 }
1462 
1463 static int xgbe_is_tso(struct sk_buff *skb)
1464 {
1465 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1466 		return 0;
1467 
1468 	if (!skb_is_gso(skb))
1469 		return 0;
1470 
1471 	DBGPR("  TSO packet to be processed\n");
1472 
1473 	return 1;
1474 }
1475 
1476 static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1477 			     struct xgbe_ring *ring, struct sk_buff *skb,
1478 			     struct xgbe_packet_data *packet)
1479 {
1480 	skb_frag_t *frag;
1481 	unsigned int context_desc;
1482 	unsigned int len;
1483 	unsigned int i;
1484 
1485 	packet->skb = skb;
1486 
1487 	context_desc = 0;
1488 	packet->rdesc_count = 0;
1489 
1490 	packet->tx_packets = 1;
1491 	packet->tx_bytes = skb->len;
1492 
1493 	if (xgbe_is_tso(skb)) {
1494 		/* TSO requires an extra descriptor if mss is different */
1495 		if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1496 			context_desc = 1;
1497 			packet->rdesc_count++;
1498 		}
1499 
1500 		/* TSO requires an extra descriptor for TSO header */
1501 		packet->rdesc_count++;
1502 
1503 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1504 			       TSO_ENABLE, 1);
1505 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1506 			       CSUM_ENABLE, 1);
1507 	} else if (skb->ip_summed == CHECKSUM_PARTIAL)
1508 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1509 			       CSUM_ENABLE, 1);
1510 
1511 	if (xgbe_is_vxlan(skb))
1512 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1513 			       VXLAN, 1);
1514 
1515 	if (skb_vlan_tag_present(skb)) {
1516 		/* VLAN requires an extra descriptor if tag is different */
1517 		if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
1518 			/* We can share with the TSO context descriptor */
1519 			if (!context_desc) {
1520 				context_desc = 1;
1521 				packet->rdesc_count++;
1522 			}
1523 
1524 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1525 			       VLAN_CTAG, 1);
1526 	}
1527 
1528 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1529 	    (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1530 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1531 			       PTP, 1);
1532 
1533 	for (len = skb_headlen(skb); len;) {
1534 		packet->rdesc_count++;
1535 		len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1536 	}
1537 
1538 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1539 		frag = &skb_shinfo(skb)->frags[i];
1540 		for (len = skb_frag_size(frag); len; ) {
1541 			packet->rdesc_count++;
1542 			len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1543 		}
1544 	}
1545 }
1546 
1547 static int xgbe_open(struct net_device *netdev)
1548 {
1549 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1550 	int ret;
1551 
1552 	/* Create the various names based on netdev name */
1553 	snprintf(pdata->an_name, sizeof(pdata->an_name) - 1, "%s-pcs",
1554 		 netdev_name(netdev));
1555 
1556 	snprintf(pdata->ecc_name, sizeof(pdata->ecc_name) - 1, "%s-ecc",
1557 		 netdev_name(netdev));
1558 
1559 	snprintf(pdata->i2c_name, sizeof(pdata->i2c_name) - 1, "%s-i2c",
1560 		 netdev_name(netdev));
1561 
1562 	/* Create workqueues */
1563 	pdata->dev_workqueue =
1564 		create_singlethread_workqueue(netdev_name(netdev));
1565 	if (!pdata->dev_workqueue) {
1566 		netdev_err(netdev, "device workqueue creation failed\n");
1567 		return -ENOMEM;
1568 	}
1569 
1570 	pdata->an_workqueue =
1571 		create_singlethread_workqueue(pdata->an_name);
1572 	if (!pdata->an_workqueue) {
1573 		netdev_err(netdev, "phy workqueue creation failed\n");
1574 		ret = -ENOMEM;
1575 		goto err_dev_wq;
1576 	}
1577 
1578 	/* Reset the phy settings */
1579 	ret = xgbe_phy_reset(pdata);
1580 	if (ret)
1581 		goto err_an_wq;
1582 
1583 	/* Enable the clocks */
1584 	ret = clk_prepare_enable(pdata->sysclk);
1585 	if (ret) {
1586 		netdev_alert(netdev, "dma clk_prepare_enable failed\n");
1587 		goto err_an_wq;
1588 	}
1589 
1590 	ret = clk_prepare_enable(pdata->ptpclk);
1591 	if (ret) {
1592 		netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1593 		goto err_sysclk;
1594 	}
1595 
1596 	INIT_WORK(&pdata->service_work, xgbe_service);
1597 	INIT_WORK(&pdata->restart_work, xgbe_restart);
1598 	INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
1599 	INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
1600 
1601 	/* Initialize PTP timestamping and clock. */
1602 	xgbe_init_ptp(pdata);
1603 
1604 	ret = xgbe_alloc_memory(pdata);
1605 	if (ret)
1606 		goto err_ptpclk;
1607 
1608 	ret = xgbe_start(pdata);
1609 	if (ret)
1610 		goto err_mem;
1611 
1612 	clear_bit(XGBE_DOWN, &pdata->dev_state);
1613 
1614 	return 0;
1615 
1616 err_mem:
1617 	xgbe_free_memory(pdata);
1618 
1619 err_ptpclk:
1620 	clk_disable_unprepare(pdata->ptpclk);
1621 
1622 err_sysclk:
1623 	clk_disable_unprepare(pdata->sysclk);
1624 
1625 err_an_wq:
1626 	destroy_workqueue(pdata->an_workqueue);
1627 
1628 err_dev_wq:
1629 	destroy_workqueue(pdata->dev_workqueue);
1630 
1631 	return ret;
1632 }
1633 
1634 static int xgbe_close(struct net_device *netdev)
1635 {
1636 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1637 
1638 	/* Stop the device */
1639 	xgbe_stop(pdata);
1640 
1641 	xgbe_free_memory(pdata);
1642 
1643 	/* Disable the clocks */
1644 	clk_disable_unprepare(pdata->ptpclk);
1645 	clk_disable_unprepare(pdata->sysclk);
1646 
1647 	destroy_workqueue(pdata->an_workqueue);
1648 
1649 	destroy_workqueue(pdata->dev_workqueue);
1650 
1651 	set_bit(XGBE_DOWN, &pdata->dev_state);
1652 
1653 	return 0;
1654 }
1655 
1656 static netdev_tx_t xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1657 {
1658 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1659 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1660 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1661 	struct xgbe_channel *channel;
1662 	struct xgbe_ring *ring;
1663 	struct xgbe_packet_data *packet;
1664 	struct netdev_queue *txq;
1665 	netdev_tx_t ret;
1666 
1667 	DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1668 
1669 	channel = pdata->channel[skb->queue_mapping];
1670 	txq = netdev_get_tx_queue(netdev, channel->queue_index);
1671 	ring = channel->tx_ring;
1672 	packet = &ring->packet_data;
1673 
1674 	ret = NETDEV_TX_OK;
1675 
1676 	if (skb->len == 0) {
1677 		netif_err(pdata, tx_err, netdev,
1678 			  "empty skb received from stack\n");
1679 		dev_kfree_skb_any(skb);
1680 		goto tx_netdev_return;
1681 	}
1682 
1683 	/* Calculate preliminary packet info */
1684 	memset(packet, 0, sizeof(*packet));
1685 	xgbe_packet_info(pdata, ring, skb, packet);
1686 
1687 	/* Check that there are enough descriptors available */
1688 	ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
1689 	if (ret)
1690 		goto tx_netdev_return;
1691 
1692 	ret = xgbe_prep_tso(skb, packet);
1693 	if (ret) {
1694 		netif_err(pdata, tx_err, netdev,
1695 			  "error processing TSO packet\n");
1696 		dev_kfree_skb_any(skb);
1697 		goto tx_netdev_return;
1698 	}
1699 	xgbe_prep_vlan(skb, packet);
1700 
1701 	if (!desc_if->map_tx_skb(channel, skb)) {
1702 		dev_kfree_skb_any(skb);
1703 		goto tx_netdev_return;
1704 	}
1705 
1706 	xgbe_prep_tx_tstamp(pdata, skb, packet);
1707 
1708 	/* Report on the actual number of bytes (to be) sent */
1709 	netdev_tx_sent_queue(txq, packet->tx_bytes);
1710 
1711 	/* Configure required descriptor fields for transmission */
1712 	hw_if->dev_xmit(channel);
1713 
1714 	if (netif_msg_pktdata(pdata))
1715 		xgbe_print_pkt(netdev, skb, true);
1716 
1717 	/* Stop the queue in advance if there may not be enough descriptors */
1718 	xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
1719 
1720 	ret = NETDEV_TX_OK;
1721 
1722 tx_netdev_return:
1723 	return ret;
1724 }
1725 
1726 static void xgbe_set_rx_mode(struct net_device *netdev)
1727 {
1728 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1729 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1730 
1731 	DBGPR("-->xgbe_set_rx_mode\n");
1732 
1733 	hw_if->config_rx_mode(pdata);
1734 
1735 	DBGPR("<--xgbe_set_rx_mode\n");
1736 }
1737 
1738 static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
1739 {
1740 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1741 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1742 	struct sockaddr *saddr = addr;
1743 
1744 	DBGPR("-->xgbe_set_mac_address\n");
1745 
1746 	if (!is_valid_ether_addr(saddr->sa_data))
1747 		return -EADDRNOTAVAIL;
1748 
1749 	eth_hw_addr_set(netdev, saddr->sa_data);
1750 
1751 	hw_if->set_mac_address(pdata, netdev->dev_addr);
1752 
1753 	DBGPR("<--xgbe_set_mac_address\n");
1754 
1755 	return 0;
1756 }
1757 
1758 static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
1759 {
1760 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1761 	int ret;
1762 
1763 	switch (cmd) {
1764 	case SIOCGHWTSTAMP:
1765 		ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
1766 		break;
1767 
1768 	case SIOCSHWTSTAMP:
1769 		ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
1770 		break;
1771 
1772 	default:
1773 		ret = -EOPNOTSUPP;
1774 	}
1775 
1776 	return ret;
1777 }
1778 
1779 static int xgbe_change_mtu(struct net_device *netdev, int mtu)
1780 {
1781 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1782 	int ret;
1783 
1784 	DBGPR("-->xgbe_change_mtu\n");
1785 
1786 	ret = xgbe_calc_rx_buf_size(netdev, mtu);
1787 	if (ret < 0)
1788 		return ret;
1789 
1790 	pdata->rx_buf_size = ret;
1791 	WRITE_ONCE(netdev->mtu, mtu);
1792 
1793 	xgbe_restart_dev(pdata);
1794 
1795 	DBGPR("<--xgbe_change_mtu\n");
1796 
1797 	return 0;
1798 }
1799 
1800 static void xgbe_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1801 {
1802 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1803 
1804 	netdev_warn(netdev, "tx timeout, device restarting\n");
1805 	schedule_work(&pdata->restart_work);
1806 }
1807 
1808 static void xgbe_get_stats64(struct net_device *netdev,
1809 			     struct rtnl_link_stats64 *s)
1810 {
1811 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1812 	struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
1813 
1814 	DBGPR("-->%s\n", __func__);
1815 
1816 	pdata->hw_if.read_mmc_stats(pdata);
1817 
1818 	s->rx_packets = pstats->rxframecount_gb;
1819 	s->rx_bytes = pstats->rxoctetcount_gb;
1820 	s->rx_errors = pstats->rxframecount_gb -
1821 		       pstats->rxbroadcastframes_g -
1822 		       pstats->rxmulticastframes_g -
1823 		       pstats->rxunicastframes_g;
1824 	s->multicast = pstats->rxmulticastframes_g;
1825 	s->rx_length_errors = pstats->rxlengtherror;
1826 	s->rx_crc_errors = pstats->rxcrcerror;
1827 	s->rx_fifo_errors = pstats->rxfifooverflow;
1828 
1829 	s->tx_packets = pstats->txframecount_gb;
1830 	s->tx_bytes = pstats->txoctetcount_gb;
1831 	s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
1832 	s->tx_dropped = netdev->stats.tx_dropped;
1833 
1834 	DBGPR("<--%s\n", __func__);
1835 }
1836 
1837 static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1838 				u16 vid)
1839 {
1840 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1841 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1842 
1843 	DBGPR("-->%s\n", __func__);
1844 
1845 	set_bit(vid, pdata->active_vlans);
1846 	hw_if->update_vlan_hash_table(pdata);
1847 
1848 	DBGPR("<--%s\n", __func__);
1849 
1850 	return 0;
1851 }
1852 
1853 static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1854 				 u16 vid)
1855 {
1856 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1857 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1858 
1859 	DBGPR("-->%s\n", __func__);
1860 
1861 	clear_bit(vid, pdata->active_vlans);
1862 	hw_if->update_vlan_hash_table(pdata);
1863 
1864 	DBGPR("<--%s\n", __func__);
1865 
1866 	return 0;
1867 }
1868 
1869 #ifdef CONFIG_NET_POLL_CONTROLLER
1870 static void xgbe_poll_controller(struct net_device *netdev)
1871 {
1872 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1873 	struct xgbe_channel *channel;
1874 	unsigned int i;
1875 
1876 	DBGPR("-->xgbe_poll_controller\n");
1877 
1878 	if (pdata->per_channel_irq) {
1879 		for (i = 0; i < pdata->channel_count; i++) {
1880 			channel = pdata->channel[i];
1881 			xgbe_dma_isr(channel->dma_irq, channel);
1882 		}
1883 	} else {
1884 		disable_irq(pdata->dev_irq);
1885 		xgbe_isr(pdata->dev_irq, pdata);
1886 		enable_irq(pdata->dev_irq);
1887 	}
1888 
1889 	DBGPR("<--xgbe_poll_controller\n");
1890 }
1891 #endif /* End CONFIG_NET_POLL_CONTROLLER */
1892 
1893 static int xgbe_setup_tc(struct net_device *netdev, enum tc_setup_type type,
1894 			 void *type_data)
1895 {
1896 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1897 	struct tc_mqprio_qopt *mqprio = type_data;
1898 	u8 tc;
1899 
1900 	if (type != TC_SETUP_QDISC_MQPRIO)
1901 		return -EOPNOTSUPP;
1902 
1903 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1904 	tc = mqprio->num_tc;
1905 
1906 	if (tc > pdata->hw_feat.tc_cnt)
1907 		return -EINVAL;
1908 
1909 	pdata->num_tcs = tc;
1910 	pdata->hw_if.config_tc(pdata);
1911 
1912 	return 0;
1913 }
1914 
1915 static netdev_features_t xgbe_fix_features(struct net_device *netdev,
1916 					   netdev_features_t features)
1917 {
1918 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1919 	netdev_features_t vxlan_base;
1920 
1921 	vxlan_base = NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RX_UDP_TUNNEL_PORT;
1922 
1923 	if (!pdata->hw_feat.vxn)
1924 		return features;
1925 
1926 	/* VXLAN CSUM requires VXLAN base */
1927 	if ((features & NETIF_F_GSO_UDP_TUNNEL_CSUM) &&
1928 	    !(features & NETIF_F_GSO_UDP_TUNNEL)) {
1929 		netdev_notice(netdev,
1930 			      "forcing tx udp tunnel support\n");
1931 		features |= NETIF_F_GSO_UDP_TUNNEL;
1932 	}
1933 
1934 	/* Can't do one without doing the other */
1935 	if ((features & vxlan_base) != vxlan_base) {
1936 		netdev_notice(netdev,
1937 			      "forcing both tx and rx udp tunnel support\n");
1938 		features |= vxlan_base;
1939 	}
1940 
1941 	if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1942 		if (!(features & NETIF_F_GSO_UDP_TUNNEL_CSUM)) {
1943 			netdev_notice(netdev,
1944 				      "forcing tx udp tunnel checksumming on\n");
1945 			features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
1946 		}
1947 	} else {
1948 		if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM) {
1949 			netdev_notice(netdev,
1950 				      "forcing tx udp tunnel checksumming off\n");
1951 			features &= ~NETIF_F_GSO_UDP_TUNNEL_CSUM;
1952 		}
1953 	}
1954 
1955 	return features;
1956 }
1957 
1958 static int xgbe_set_features(struct net_device *netdev,
1959 			     netdev_features_t features)
1960 {
1961 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1962 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1963 	netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
1964 	int ret = 0;
1965 
1966 	rxhash = pdata->netdev_features & NETIF_F_RXHASH;
1967 	rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
1968 	rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
1969 	rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
1970 
1971 	if ((features & NETIF_F_RXHASH) && !rxhash)
1972 		ret = hw_if->enable_rss(pdata);
1973 	else if (!(features & NETIF_F_RXHASH) && rxhash)
1974 		ret = hw_if->disable_rss(pdata);
1975 	if (ret)
1976 		return ret;
1977 
1978 	if ((features & NETIF_F_RXCSUM) && !rxcsum) {
1979 		hw_if->enable_sph(pdata);
1980 		hw_if->enable_vxlan(pdata);
1981 		hw_if->enable_rx_csum(pdata);
1982 		schedule_work(&pdata->restart_work);
1983 	} else if (!(features & NETIF_F_RXCSUM) && rxcsum) {
1984 		hw_if->disable_sph(pdata);
1985 		hw_if->disable_vxlan(pdata);
1986 		hw_if->disable_rx_csum(pdata);
1987 		schedule_work(&pdata->restart_work);
1988 	}
1989 
1990 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
1991 		hw_if->enable_rx_vlan_stripping(pdata);
1992 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
1993 		hw_if->disable_rx_vlan_stripping(pdata);
1994 
1995 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
1996 		hw_if->enable_rx_vlan_filtering(pdata);
1997 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
1998 		hw_if->disable_rx_vlan_filtering(pdata);
1999 
2000 	pdata->netdev_features = features;
2001 
2002 	DBGPR("<--xgbe_set_features\n");
2003 
2004 	return 0;
2005 }
2006 
2007 static netdev_features_t xgbe_features_check(struct sk_buff *skb,
2008 					     struct net_device *netdev,
2009 					     netdev_features_t features)
2010 {
2011 	features = vlan_features_check(skb, features);
2012 	features = vxlan_features_check(skb, features);
2013 
2014 	return features;
2015 }
2016 
2017 static const struct net_device_ops xgbe_netdev_ops = {
2018 	.ndo_open		= xgbe_open,
2019 	.ndo_stop		= xgbe_close,
2020 	.ndo_start_xmit		= xgbe_xmit,
2021 	.ndo_set_rx_mode	= xgbe_set_rx_mode,
2022 	.ndo_set_mac_address	= xgbe_set_mac_address,
2023 	.ndo_validate_addr	= eth_validate_addr,
2024 	.ndo_eth_ioctl		= xgbe_ioctl,
2025 	.ndo_change_mtu		= xgbe_change_mtu,
2026 	.ndo_tx_timeout		= xgbe_tx_timeout,
2027 	.ndo_get_stats64	= xgbe_get_stats64,
2028 	.ndo_vlan_rx_add_vid	= xgbe_vlan_rx_add_vid,
2029 	.ndo_vlan_rx_kill_vid	= xgbe_vlan_rx_kill_vid,
2030 #ifdef CONFIG_NET_POLL_CONTROLLER
2031 	.ndo_poll_controller	= xgbe_poll_controller,
2032 #endif
2033 	.ndo_setup_tc		= xgbe_setup_tc,
2034 	.ndo_fix_features	= xgbe_fix_features,
2035 	.ndo_set_features	= xgbe_set_features,
2036 	.ndo_features_check	= xgbe_features_check,
2037 };
2038 
2039 const struct net_device_ops *xgbe_get_netdev_ops(void)
2040 {
2041 	return &xgbe_netdev_ops;
2042 }
2043 
2044 static void xgbe_rx_refresh(struct xgbe_channel *channel)
2045 {
2046 	struct xgbe_prv_data *pdata = channel->pdata;
2047 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2048 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
2049 	struct xgbe_ring *ring = channel->rx_ring;
2050 	struct xgbe_ring_data *rdata;
2051 
2052 	while (ring->dirty != ring->cur) {
2053 		rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
2054 
2055 		/* Reset rdata values */
2056 		desc_if->unmap_rdata(pdata, rdata);
2057 
2058 		if (desc_if->map_rx_buffer(pdata, ring, rdata))
2059 			break;
2060 
2061 		hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
2062 
2063 		ring->dirty++;
2064 	}
2065 
2066 	/* Make sure everything is written before the register write */
2067 	wmb();
2068 
2069 	/* Update the Rx Tail Pointer Register with address of
2070 	 * the last cleaned entry */
2071 	rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
2072 	XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
2073 			  lower_32_bits(rdata->rdesc_dma));
2074 }
2075 
2076 static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
2077 				       struct napi_struct *napi,
2078 				       struct xgbe_ring_data *rdata,
2079 				       unsigned int len)
2080 {
2081 	struct sk_buff *skb;
2082 	u8 *packet;
2083 
2084 	skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
2085 	if (!skb)
2086 		return NULL;
2087 
2088 	/* Pull in the header buffer which may contain just the header
2089 	 * or the header plus data
2090 	 */
2091 	dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
2092 				      rdata->rx.hdr.dma_off,
2093 				      rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
2094 
2095 	packet = page_address(rdata->rx.hdr.pa.pages) +
2096 		 rdata->rx.hdr.pa.pages_offset;
2097 	skb_copy_to_linear_data(skb, packet, len);
2098 	skb_put(skb, len);
2099 
2100 	return skb;
2101 }
2102 
2103 static unsigned int xgbe_rx_buf1_len(struct xgbe_ring_data *rdata,
2104 				     struct xgbe_packet_data *packet)
2105 {
2106 	/* Always zero if not the first descriptor */
2107 	if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST))
2108 		return 0;
2109 
2110 	/* First descriptor with split header, return header length */
2111 	if (rdata->rx.hdr_len)
2112 		return rdata->rx.hdr_len;
2113 
2114 	/* First descriptor but not the last descriptor and no split header,
2115 	 * so the full buffer was used
2116 	 */
2117 	if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2118 		return rdata->rx.hdr.dma_len;
2119 
2120 	/* First descriptor and last descriptor and no split header, so
2121 	 * calculate how much of the buffer was used
2122 	 */
2123 	return min_t(unsigned int, rdata->rx.hdr.dma_len, rdata->rx.len);
2124 }
2125 
2126 static unsigned int xgbe_rx_buf2_len(struct xgbe_ring_data *rdata,
2127 				     struct xgbe_packet_data *packet,
2128 				     unsigned int len)
2129 {
2130 	/* Always the full buffer if not the last descriptor */
2131 	if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2132 		return rdata->rx.buf.dma_len;
2133 
2134 	/* Last descriptor so calculate how much of the buffer was used
2135 	 * for the last bit of data
2136 	 */
2137 	return rdata->rx.len - len;
2138 }
2139 
2140 static int xgbe_tx_poll(struct xgbe_channel *channel)
2141 {
2142 	struct xgbe_prv_data *pdata = channel->pdata;
2143 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2144 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
2145 	struct xgbe_ring *ring = channel->tx_ring;
2146 	struct xgbe_ring_data *rdata;
2147 	struct xgbe_ring_desc *rdesc;
2148 	struct net_device *netdev = pdata->netdev;
2149 	struct netdev_queue *txq;
2150 	int processed = 0;
2151 	unsigned int tx_packets = 0, tx_bytes = 0;
2152 	unsigned int cur;
2153 
2154 	DBGPR("-->xgbe_tx_poll\n");
2155 
2156 	/* Nothing to do if there isn't a Tx ring for this channel */
2157 	if (!ring)
2158 		return 0;
2159 
2160 	cur = ring->cur;
2161 
2162 	/* Be sure we get ring->cur before accessing descriptor data */
2163 	smp_rmb();
2164 
2165 	txq = netdev_get_tx_queue(netdev, channel->queue_index);
2166 
2167 	while ((processed < XGBE_TX_DESC_MAX_PROC) &&
2168 	       (ring->dirty != cur)) {
2169 		rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
2170 		rdesc = rdata->rdesc;
2171 
2172 		if (!hw_if->tx_complete(rdesc))
2173 			break;
2174 
2175 		/* Make sure descriptor fields are read after reading the OWN
2176 		 * bit */
2177 		dma_rmb();
2178 
2179 		if (netif_msg_tx_done(pdata))
2180 			xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
2181 
2182 		if (hw_if->is_last_desc(rdesc)) {
2183 			tx_packets += rdata->tx.packets;
2184 			tx_bytes += rdata->tx.bytes;
2185 		}
2186 
2187 		/* Free the SKB and reset the descriptor for re-use */
2188 		desc_if->unmap_rdata(pdata, rdata);
2189 		hw_if->tx_desc_reset(rdata);
2190 
2191 		processed++;
2192 		ring->dirty++;
2193 	}
2194 
2195 	if (!processed)
2196 		return 0;
2197 
2198 	netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
2199 
2200 	if ((ring->tx.queue_stopped == 1) &&
2201 	    (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
2202 		ring->tx.queue_stopped = 0;
2203 		netif_tx_wake_queue(txq);
2204 	}
2205 
2206 	DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
2207 
2208 	return processed;
2209 }
2210 
2211 static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
2212 {
2213 	struct xgbe_prv_data *pdata = channel->pdata;
2214 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2215 	struct xgbe_ring *ring = channel->rx_ring;
2216 	struct xgbe_ring_data *rdata;
2217 	struct xgbe_packet_data *packet;
2218 	struct net_device *netdev = pdata->netdev;
2219 	struct napi_struct *napi;
2220 	struct sk_buff *skb;
2221 	struct skb_shared_hwtstamps *hwtstamps;
2222 	unsigned int last, error, context_next, context;
2223 	unsigned int len, buf1_len, buf2_len, max_len;
2224 	unsigned int received = 0;
2225 	int packet_count = 0;
2226 
2227 	DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
2228 
2229 	/* Nothing to do if there isn't a Rx ring for this channel */
2230 	if (!ring)
2231 		return 0;
2232 
2233 	last = 0;
2234 	context_next = 0;
2235 
2236 	napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
2237 
2238 	rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2239 	packet = &ring->packet_data;
2240 	while (packet_count < budget) {
2241 		DBGPR("  cur = %d\n", ring->cur);
2242 
2243 		/* First time in loop see if we need to restore state */
2244 		if (!received && rdata->state_saved) {
2245 			skb = rdata->state.skb;
2246 			error = rdata->state.error;
2247 			len = rdata->state.len;
2248 		} else {
2249 			memset(packet, 0, sizeof(*packet));
2250 			skb = NULL;
2251 			error = 0;
2252 			len = 0;
2253 		}
2254 
2255 read_again:
2256 		rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2257 
2258 		if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
2259 			xgbe_rx_refresh(channel);
2260 
2261 		if (hw_if->dev_read(channel))
2262 			break;
2263 
2264 		received++;
2265 		ring->cur++;
2266 
2267 		last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2268 				      LAST);
2269 		context_next = XGMAC_GET_BITS(packet->attributes,
2270 					      RX_PACKET_ATTRIBUTES,
2271 					      CONTEXT_NEXT);
2272 		context = XGMAC_GET_BITS(packet->attributes,
2273 					 RX_PACKET_ATTRIBUTES,
2274 					 CONTEXT);
2275 
2276 		/* Earlier error, just drain the remaining data */
2277 		if ((!last || context_next) && error)
2278 			goto read_again;
2279 
2280 		if (error || packet->errors) {
2281 			if (packet->errors)
2282 				netif_err(pdata, rx_err, netdev,
2283 					  "error in received packet\n");
2284 			dev_kfree_skb(skb);
2285 			goto next_packet;
2286 		}
2287 
2288 		if (!context) {
2289 			/* Get the data length in the descriptor buffers */
2290 			buf1_len = xgbe_rx_buf1_len(rdata, packet);
2291 			len += buf1_len;
2292 			buf2_len = xgbe_rx_buf2_len(rdata, packet, len);
2293 			len += buf2_len;
2294 
2295 			if (buf2_len > rdata->rx.buf.dma_len) {
2296 				/* Hardware inconsistency within the descriptors
2297 				 * that has resulted in a length underflow.
2298 				 */
2299 				error = 1;
2300 				goto skip_data;
2301 			}
2302 
2303 			if (!skb) {
2304 				skb = xgbe_create_skb(pdata, napi, rdata,
2305 						      buf1_len);
2306 				if (!skb) {
2307 					error = 1;
2308 					goto skip_data;
2309 				}
2310 			}
2311 
2312 			if (buf2_len) {
2313 				dma_sync_single_range_for_cpu(pdata->dev,
2314 							rdata->rx.buf.dma_base,
2315 							rdata->rx.buf.dma_off,
2316 							rdata->rx.buf.dma_len,
2317 							DMA_FROM_DEVICE);
2318 
2319 				skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2320 						rdata->rx.buf.pa.pages,
2321 						rdata->rx.buf.pa.pages_offset,
2322 						buf2_len,
2323 						rdata->rx.buf.dma_len);
2324 				rdata->rx.buf.pa.pages = NULL;
2325 			}
2326 		}
2327 
2328 skip_data:
2329 		if (!last || context_next)
2330 			goto read_again;
2331 
2332 		if (!skb || error) {
2333 			dev_kfree_skb(skb);
2334 			goto next_packet;
2335 		}
2336 
2337 		/* Be sure we don't exceed the configured MTU */
2338 		max_len = netdev->mtu + ETH_HLEN;
2339 		if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2340 		    (skb->protocol == htons(ETH_P_8021Q)))
2341 			max_len += VLAN_HLEN;
2342 
2343 		if (skb->len > max_len) {
2344 			netif_err(pdata, rx_err, netdev,
2345 				  "packet length exceeds configured MTU\n");
2346 			dev_kfree_skb(skb);
2347 			goto next_packet;
2348 		}
2349 
2350 		if (netif_msg_pktdata(pdata))
2351 			xgbe_print_pkt(netdev, skb, false);
2352 
2353 		skb_checksum_none_assert(skb);
2354 		if (XGMAC_GET_BITS(packet->attributes,
2355 				   RX_PACKET_ATTRIBUTES, CSUM_DONE))
2356 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2357 
2358 		if (XGMAC_GET_BITS(packet->attributes,
2359 				   RX_PACKET_ATTRIBUTES, TNP)) {
2360 			skb->encapsulation = 1;
2361 
2362 			if (XGMAC_GET_BITS(packet->attributes,
2363 					   RX_PACKET_ATTRIBUTES, TNPCSUM_DONE))
2364 				skb->csum_level = 1;
2365 		}
2366 
2367 		if (XGMAC_GET_BITS(packet->attributes,
2368 				   RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2369 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2370 					       packet->vlan_ctag);
2371 
2372 		if (XGMAC_GET_BITS(packet->attributes,
2373 				   RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2374 			hwtstamps = skb_hwtstamps(skb);
2375 			hwtstamps->hwtstamp = ns_to_ktime(packet->rx_tstamp);
2376 		}
2377 
2378 		if (XGMAC_GET_BITS(packet->attributes,
2379 				   RX_PACKET_ATTRIBUTES, RSS_HASH))
2380 			skb_set_hash(skb, packet->rss_hash,
2381 				     packet->rss_hash_type);
2382 
2383 		skb->dev = netdev;
2384 		skb->protocol = eth_type_trans(skb, netdev);
2385 		skb_record_rx_queue(skb, channel->queue_index);
2386 
2387 		napi_gro_receive(napi, skb);
2388 
2389 next_packet:
2390 		packet_count++;
2391 	}
2392 
2393 	/* Check if we need to save state before leaving */
2394 	if (received && (!last || context_next)) {
2395 		rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2396 		rdata->state_saved = 1;
2397 		rdata->state.skb = skb;
2398 		rdata->state.len = len;
2399 		rdata->state.error = error;
2400 	}
2401 
2402 	DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
2403 
2404 	return packet_count;
2405 }
2406 
2407 static int xgbe_one_poll(struct napi_struct *napi, int budget)
2408 {
2409 	struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2410 						    napi);
2411 	struct xgbe_prv_data *pdata = channel->pdata;
2412 	int processed = 0;
2413 
2414 	DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2415 
2416 	/* Cleanup Tx ring first */
2417 	xgbe_tx_poll(channel);
2418 
2419 	/* Process Rx ring next */
2420 	processed = xgbe_rx_poll(channel, budget);
2421 
2422 	/* If we processed everything, we are done */
2423 	if ((processed < budget) && napi_complete_done(napi, processed)) {
2424 		/* Enable Tx and Rx interrupts */
2425 		if (pdata->channel_irq_mode)
2426 			xgbe_enable_rx_tx_int(pdata, channel);
2427 		else
2428 			enable_irq(channel->dma_irq);
2429 	}
2430 
2431 	DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2432 
2433 	return processed;
2434 }
2435 
2436 static int xgbe_all_poll(struct napi_struct *napi, int budget)
2437 {
2438 	struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2439 						   napi);
2440 	struct xgbe_channel *channel;
2441 	int ring_budget;
2442 	int processed, last_processed;
2443 	unsigned int i;
2444 
2445 	DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
2446 
2447 	processed = 0;
2448 	ring_budget = budget / pdata->rx_ring_count;
2449 	do {
2450 		last_processed = processed;
2451 
2452 		for (i = 0; i < pdata->channel_count; i++) {
2453 			channel = pdata->channel[i];
2454 
2455 			/* Cleanup Tx ring first */
2456 			xgbe_tx_poll(channel);
2457 
2458 			/* Process Rx ring next */
2459 			if (ring_budget > (budget - processed))
2460 				ring_budget = budget - processed;
2461 			processed += xgbe_rx_poll(channel, ring_budget);
2462 		}
2463 	} while ((processed < budget) && (processed != last_processed));
2464 
2465 	/* If we processed everything, we are done */
2466 	if ((processed < budget) && napi_complete_done(napi, processed)) {
2467 		/* Enable Tx and Rx interrupts */
2468 		xgbe_enable_rx_tx_ints(pdata);
2469 	}
2470 
2471 	DBGPR("<--xgbe_all_poll: received = %d\n", processed);
2472 
2473 	return processed;
2474 }
2475 
2476 void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2477 		       unsigned int idx, unsigned int count, unsigned int flag)
2478 {
2479 	struct xgbe_ring_data *rdata;
2480 	struct xgbe_ring_desc *rdesc;
2481 
2482 	while (count--) {
2483 		rdata = XGBE_GET_DESC_DATA(ring, idx);
2484 		rdesc = rdata->rdesc;
2485 		netdev_dbg(pdata->netdev,
2486 			   "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2487 			   (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2488 			   le32_to_cpu(rdesc->desc0),
2489 			   le32_to_cpu(rdesc->desc1),
2490 			   le32_to_cpu(rdesc->desc2),
2491 			   le32_to_cpu(rdesc->desc3));
2492 		idx++;
2493 	}
2494 }
2495 
2496 void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2497 		       unsigned int idx)
2498 {
2499 	struct xgbe_ring_data *rdata;
2500 	struct xgbe_ring_desc *rdesc;
2501 
2502 	rdata = XGBE_GET_DESC_DATA(ring, idx);
2503 	rdesc = rdata->rdesc;
2504 	netdev_dbg(pdata->netdev,
2505 		   "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
2506 		   idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2507 		   le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
2508 }
2509 
2510 void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2511 {
2512 	struct ethhdr *eth = (struct ethhdr *)skb->data;
2513 	unsigned char buffer[128];
2514 	unsigned int i;
2515 
2516 	netdev_dbg(netdev, "\n************** SKB dump ****************\n");
2517 
2518 	netdev_dbg(netdev, "%s packet of %d bytes\n",
2519 		   (tx_rx ? "TX" : "RX"), skb->len);
2520 
2521 	netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
2522 	netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
2523 	netdev_dbg(netdev, "Protocol: %#06x\n", ntohs(eth->h_proto));
2524 
2525 	for (i = 0; i < skb->len; i += 32) {
2526 		unsigned int len = min(skb->len - i, 32U);
2527 
2528 		hex_dump_to_buffer(&skb->data[i], len, 32, 1,
2529 				   buffer, sizeof(buffer), false);
2530 		netdev_dbg(netdev, "  %#06x: %s\n", i, buffer);
2531 	}
2532 
2533 	netdev_dbg(netdev, "\n************** SKB dump ****************\n");
2534 }
2535