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