xref: /linux/drivers/net/ethernet/intel/iavf/iavf_txrx.c (revision b54c9d5bd6e38edac9ce3a3f95f14a1292b5268d)
1ae06c70bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0
251dce24bSJeff Kirsher /* Copyright(c) 2013 - 2018 Intel Corporation. */
37f12ad74SGreg Rose 
47ed3f5f0SPaul Gortmaker #include <linux/prefetch.h>
57ed3f5f0SPaul Gortmaker 
65ec8b7d1SJesse Brandeburg #include "iavf.h"
7ad64ed8bSJesse Brandeburg #include "iavf_trace.h"
866bc8e0fSJesse Brandeburg #include "iavf_prototype.h"
97f12ad74SGreg Rose 
107f12ad74SGreg Rose static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
117f12ad74SGreg Rose 				u32 td_tag)
127f12ad74SGreg Rose {
13f1cad2ceSJesse Brandeburg 	return cpu_to_le64(IAVF_TX_DESC_DTYPE_DATA |
1456184e01SJesse Brandeburg 			   ((u64)td_cmd  << IAVF_TXD_QW1_CMD_SHIFT) |
1556184e01SJesse Brandeburg 			   ((u64)td_offset << IAVF_TXD_QW1_OFFSET_SHIFT) |
1656184e01SJesse Brandeburg 			   ((u64)size  << IAVF_TXD_QW1_TX_BUF_SZ_SHIFT) |
1756184e01SJesse Brandeburg 			   ((u64)td_tag  << IAVF_TXD_QW1_L2TAG1_SHIFT));
187f12ad74SGreg Rose }
197f12ad74SGreg Rose 
2056184e01SJesse Brandeburg #define IAVF_TXD_CMD (IAVF_TX_DESC_CMD_EOP | IAVF_TX_DESC_CMD_RS)
217f12ad74SGreg Rose 
227f12ad74SGreg Rose /**
2356184e01SJesse Brandeburg  * iavf_unmap_and_free_tx_resource - Release a Tx buffer
247f12ad74SGreg Rose  * @ring:      the ring that owns the buffer
257f12ad74SGreg Rose  * @tx_buffer: the buffer to free
267f12ad74SGreg Rose  **/
2756184e01SJesse Brandeburg static void iavf_unmap_and_free_tx_resource(struct iavf_ring *ring,
2856184e01SJesse Brandeburg 					    struct iavf_tx_buffer *tx_buffer)
297f12ad74SGreg Rose {
307f12ad74SGreg Rose 	if (tx_buffer->skb) {
3156184e01SJesse Brandeburg 		if (tx_buffer->tx_flags & IAVF_TX_FLAGS_FD_SB)
3264bfd68eSAlexander Duyck 			kfree(tx_buffer->raw_buf);
3364bfd68eSAlexander Duyck 		else
347f12ad74SGreg Rose 			dev_kfree_skb_any(tx_buffer->skb);
357f12ad74SGreg Rose 		if (dma_unmap_len(tx_buffer, len))
367f12ad74SGreg Rose 			dma_unmap_single(ring->dev,
377f12ad74SGreg Rose 					 dma_unmap_addr(tx_buffer, dma),
387f12ad74SGreg Rose 					 dma_unmap_len(tx_buffer, len),
397f12ad74SGreg Rose 					 DMA_TO_DEVICE);
407f12ad74SGreg Rose 	} else if (dma_unmap_len(tx_buffer, len)) {
417f12ad74SGreg Rose 		dma_unmap_page(ring->dev,
427f12ad74SGreg Rose 			       dma_unmap_addr(tx_buffer, dma),
437f12ad74SGreg Rose 			       dma_unmap_len(tx_buffer, len),
447f12ad74SGreg Rose 			       DMA_TO_DEVICE);
457f12ad74SGreg Rose 	}
46a42e7a36SKiran Patil 
477f12ad74SGreg Rose 	tx_buffer->next_to_watch = NULL;
487f12ad74SGreg Rose 	tx_buffer->skb = NULL;
497f12ad74SGreg Rose 	dma_unmap_len_set(tx_buffer, len, 0);
507f12ad74SGreg Rose 	/* tx_buffer must be completely set up in the transmit path */
517f12ad74SGreg Rose }
527f12ad74SGreg Rose 
537f12ad74SGreg Rose /**
54129cf89eSJesse Brandeburg  * iavf_clean_tx_ring - Free any empty Tx buffers
557f12ad74SGreg Rose  * @tx_ring: ring to be cleaned
567f12ad74SGreg Rose  **/
5756184e01SJesse Brandeburg void iavf_clean_tx_ring(struct iavf_ring *tx_ring)
587f12ad74SGreg Rose {
597f12ad74SGreg Rose 	unsigned long bi_size;
607f12ad74SGreg Rose 	u16 i;
617f12ad74SGreg Rose 
627f12ad74SGreg Rose 	/* ring already cleared, nothing to do */
637f12ad74SGreg Rose 	if (!tx_ring->tx_bi)
647f12ad74SGreg Rose 		return;
657f12ad74SGreg Rose 
667f12ad74SGreg Rose 	/* Free all the Tx ring sk_buffs */
677f12ad74SGreg Rose 	for (i = 0; i < tx_ring->count; i++)
6856184e01SJesse Brandeburg 		iavf_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
697f12ad74SGreg Rose 
7056184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count;
717f12ad74SGreg Rose 	memset(tx_ring->tx_bi, 0, bi_size);
727f12ad74SGreg Rose 
737f12ad74SGreg Rose 	/* Zero out the descriptor ring */
747f12ad74SGreg Rose 	memset(tx_ring->desc, 0, tx_ring->size);
757f12ad74SGreg Rose 
767f12ad74SGreg Rose 	tx_ring->next_to_use = 0;
777f12ad74SGreg Rose 	tx_ring->next_to_clean = 0;
787f12ad74SGreg Rose 
797f12ad74SGreg Rose 	if (!tx_ring->netdev)
807f12ad74SGreg Rose 		return;
817f12ad74SGreg Rose 
827f12ad74SGreg Rose 	/* cleanup Tx queue statistics */
83e486bdfdSAlexander Duyck 	netdev_tx_reset_queue(txring_txq(tx_ring));
847f12ad74SGreg Rose }
857f12ad74SGreg Rose 
867f12ad74SGreg Rose /**
87129cf89eSJesse Brandeburg  * iavf_free_tx_resources - Free Tx resources per queue
887f12ad74SGreg Rose  * @tx_ring: Tx descriptor ring for a specific queue
897f12ad74SGreg Rose  *
907f12ad74SGreg Rose  * Free all transmit software resources
917f12ad74SGreg Rose  **/
9256184e01SJesse Brandeburg void iavf_free_tx_resources(struct iavf_ring *tx_ring)
937f12ad74SGreg Rose {
94129cf89eSJesse Brandeburg 	iavf_clean_tx_ring(tx_ring);
957f12ad74SGreg Rose 	kfree(tx_ring->tx_bi);
967f12ad74SGreg Rose 	tx_ring->tx_bi = NULL;
977f12ad74SGreg Rose 
987f12ad74SGreg Rose 	if (tx_ring->desc) {
997f12ad74SGreg Rose 		dma_free_coherent(tx_ring->dev, tx_ring->size,
1007f12ad74SGreg Rose 				  tx_ring->desc, tx_ring->dma);
1017f12ad74SGreg Rose 		tx_ring->desc = NULL;
1027f12ad74SGreg Rose 	}
1037f12ad74SGreg Rose }
1047f12ad74SGreg Rose 
1057f12ad74SGreg Rose /**
106129cf89eSJesse Brandeburg  * iavf_get_tx_pending - how many Tx descriptors not processed
107f5254429SJacob Keller  * @ring: the ring of descriptors
108dd353109SAnjali Singhai Jain  * @in_sw: is tx_pending being checked in SW or HW
109a68de58dSJesse Brandeburg  *
1109c6c1259SKiran Patil  * Since there is no access to the ring head register
1119c6c1259SKiran Patil  * in XL710, we need to use our local copies
112a68de58dSJesse Brandeburg  **/
11356184e01SJesse Brandeburg u32 iavf_get_tx_pending(struct iavf_ring *ring, bool in_sw)
114a68de58dSJesse Brandeburg {
1159c6c1259SKiran Patil 	u32 head, tail;
116a68de58dSJesse Brandeburg 
117dd353109SAnjali Singhai Jain 	head = ring->next_to_clean;
1189c6c1259SKiran Patil 	tail = readl(ring->tail);
1199c6c1259SKiran Patil 
1209c6c1259SKiran Patil 	if (head != tail)
1219c6c1259SKiran Patil 		return (head < tail) ?
1229c6c1259SKiran Patil 			tail - head : (tail + ring->count - head);
1239c6c1259SKiran Patil 
1249c6c1259SKiran Patil 	return 0;
125a68de58dSJesse Brandeburg }
126a68de58dSJesse Brandeburg 
12707d44190SSudheer Mogilappagari /**
128129cf89eSJesse Brandeburg  * iavf_detect_recover_hung - Function to detect and recover hung_queues
12907d44190SSudheer Mogilappagari  * @vsi:  pointer to vsi struct with tx queues
13007d44190SSudheer Mogilappagari  *
13107d44190SSudheer Mogilappagari  * VSI has netdev and netdev has TX queues. This function is to check each of
13207d44190SSudheer Mogilappagari  * those TX queues if they are hung, trigger recovery by issuing SW interrupt.
13307d44190SSudheer Mogilappagari  **/
13456184e01SJesse Brandeburg void iavf_detect_recover_hung(struct iavf_vsi *vsi)
13507d44190SSudheer Mogilappagari {
13656184e01SJesse Brandeburg 	struct iavf_ring *tx_ring = NULL;
13707d44190SSudheer Mogilappagari 	struct net_device *netdev;
13807d44190SSudheer Mogilappagari 	unsigned int i;
13907d44190SSudheer Mogilappagari 	int packets;
14007d44190SSudheer Mogilappagari 
14107d44190SSudheer Mogilappagari 	if (!vsi)
14207d44190SSudheer Mogilappagari 		return;
14307d44190SSudheer Mogilappagari 
14456184e01SJesse Brandeburg 	if (test_bit(__IAVF_VSI_DOWN, vsi->state))
14507d44190SSudheer Mogilappagari 		return;
14607d44190SSudheer Mogilappagari 
14707d44190SSudheer Mogilappagari 	netdev = vsi->netdev;
14807d44190SSudheer Mogilappagari 	if (!netdev)
14907d44190SSudheer Mogilappagari 		return;
15007d44190SSudheer Mogilappagari 
15107d44190SSudheer Mogilappagari 	if (!netif_carrier_ok(netdev))
15207d44190SSudheer Mogilappagari 		return;
15307d44190SSudheer Mogilappagari 
15407d44190SSudheer Mogilappagari 	for (i = 0; i < vsi->back->num_active_queues; i++) {
15507d44190SSudheer Mogilappagari 		tx_ring = &vsi->back->tx_rings[i];
15607d44190SSudheer Mogilappagari 		if (tx_ring && tx_ring->desc) {
15707d44190SSudheer Mogilappagari 			/* If packet counter has not changed the queue is
15807d44190SSudheer Mogilappagari 			 * likely stalled, so force an interrupt for this
15907d44190SSudheer Mogilappagari 			 * queue.
16007d44190SSudheer Mogilappagari 			 *
16107d44190SSudheer Mogilappagari 			 * prev_pkt_ctr would be negative if there was no
16207d44190SSudheer Mogilappagari 			 * pending work.
16307d44190SSudheer Mogilappagari 			 */
16407d44190SSudheer Mogilappagari 			packets = tx_ring->stats.packets & INT_MAX;
16507d44190SSudheer Mogilappagari 			if (tx_ring->tx_stats.prev_pkt_ctr == packets) {
166129cf89eSJesse Brandeburg 				iavf_force_wb(vsi, tx_ring->q_vector);
16707d44190SSudheer Mogilappagari 				continue;
16807d44190SSudheer Mogilappagari 			}
16907d44190SSudheer Mogilappagari 
17007d44190SSudheer Mogilappagari 			/* Memory barrier between read of packet count and call
171129cf89eSJesse Brandeburg 			 * to iavf_get_tx_pending()
17207d44190SSudheer Mogilappagari 			 */
17307d44190SSudheer Mogilappagari 			smp_rmb();
17407d44190SSudheer Mogilappagari 			tx_ring->tx_stats.prev_pkt_ctr =
175129cf89eSJesse Brandeburg 			  iavf_get_tx_pending(tx_ring, true) ? packets : -1;
17607d44190SSudheer Mogilappagari 		}
17707d44190SSudheer Mogilappagari 	}
17807d44190SSudheer Mogilappagari }
17907d44190SSudheer Mogilappagari 
1801dc8b538SAlexander Duyck #define WB_STRIDE 4
181c29af37fSAnjali Singhai Jain 
1821943d8baSJesse Brandeburg /**
18356184e01SJesse Brandeburg  * iavf_clean_tx_irq - Reclaim resources after transmit completes
184a619afe8SAlexander Duyck  * @vsi: the VSI we care about
185a619afe8SAlexander Duyck  * @tx_ring: Tx ring to clean
186a619afe8SAlexander Duyck  * @napi_budget: Used to determine if we are in netpoll
1877f12ad74SGreg Rose  *
1887f12ad74SGreg Rose  * Returns true if there's any budget left (e.g. the clean is finished)
1897f12ad74SGreg Rose  **/
19056184e01SJesse Brandeburg static bool iavf_clean_tx_irq(struct iavf_vsi *vsi,
19156184e01SJesse Brandeburg 			      struct iavf_ring *tx_ring, int napi_budget)
1927f12ad74SGreg Rose {
193168d91cfSMitch Williams 	int i = tx_ring->next_to_clean;
19456184e01SJesse Brandeburg 	struct iavf_tx_buffer *tx_buf;
19556184e01SJesse Brandeburg 	struct iavf_tx_desc *tx_desc;
196a619afe8SAlexander Duyck 	unsigned int total_bytes = 0, total_packets = 0;
197a619afe8SAlexander Duyck 	unsigned int budget = vsi->work_limit;
1987f12ad74SGreg Rose 
1997f12ad74SGreg Rose 	tx_buf = &tx_ring->tx_bi[i];
200f1cad2ceSJesse Brandeburg 	tx_desc = IAVF_TX_DESC(tx_ring, i);
2017f12ad74SGreg Rose 	i -= tx_ring->count;
2027f12ad74SGreg Rose 
2037f12ad74SGreg Rose 	do {
20456184e01SJesse Brandeburg 		struct iavf_tx_desc *eop_desc = tx_buf->next_to_watch;
2057f12ad74SGreg Rose 
2067f12ad74SGreg Rose 		/* if next_to_watch is not set then there is no work pending */
2077f12ad74SGreg Rose 		if (!eop_desc)
2087f12ad74SGreg Rose 			break;
2097f12ad74SGreg Rose 
2107f12ad74SGreg Rose 		/* prevent any other reads prior to eop_desc */
211f72271e2SBrian King 		smp_rmb();
2127f12ad74SGreg Rose 
213ad64ed8bSJesse Brandeburg 		iavf_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
214b1cb07dbSPreethi Banala 		/* if the descriptor isn't done, no work yet to do */
215b1cb07dbSPreethi Banala 		if (!(eop_desc->cmd_type_offset_bsz &
216f1cad2ceSJesse Brandeburg 		      cpu_to_le64(IAVF_TX_DESC_DTYPE_DESC_DONE)))
2177f12ad74SGreg Rose 			break;
2187f12ad74SGreg Rose 
2197f12ad74SGreg Rose 		/* clear next_to_watch to prevent false hangs */
2207f12ad74SGreg Rose 		tx_buf->next_to_watch = NULL;
2217f12ad74SGreg Rose 
2227f12ad74SGreg Rose 		/* update the statistics for this packet */
2237f12ad74SGreg Rose 		total_bytes += tx_buf->bytecount;
2247f12ad74SGreg Rose 		total_packets += tx_buf->gso_segs;
2257f12ad74SGreg Rose 
2267f12ad74SGreg Rose 		/* free the skb */
227a619afe8SAlexander Duyck 		napi_consume_skb(tx_buf->skb, napi_budget);
2287f12ad74SGreg Rose 
2297f12ad74SGreg Rose 		/* unmap skb header data */
2307f12ad74SGreg Rose 		dma_unmap_single(tx_ring->dev,
2317f12ad74SGreg Rose 				 dma_unmap_addr(tx_buf, dma),
2327f12ad74SGreg Rose 				 dma_unmap_len(tx_buf, len),
2337f12ad74SGreg Rose 				 DMA_TO_DEVICE);
2347f12ad74SGreg Rose 
2357f12ad74SGreg Rose 		/* clear tx_buffer data */
2367f12ad74SGreg Rose 		tx_buf->skb = NULL;
2377f12ad74SGreg Rose 		dma_unmap_len_set(tx_buf, len, 0);
2387f12ad74SGreg Rose 
2397f12ad74SGreg Rose 		/* unmap remaining buffers */
2407f12ad74SGreg Rose 		while (tx_desc != eop_desc) {
241ad64ed8bSJesse Brandeburg 			iavf_trace(clean_tx_irq_unmap,
242ed0980c4SScott Peterson 				   tx_ring, tx_desc, tx_buf);
2437f12ad74SGreg Rose 
2447f12ad74SGreg Rose 			tx_buf++;
2457f12ad74SGreg Rose 			tx_desc++;
2467f12ad74SGreg Rose 			i++;
2477f12ad74SGreg Rose 			if (unlikely(!i)) {
2487f12ad74SGreg Rose 				i -= tx_ring->count;
2497f12ad74SGreg Rose 				tx_buf = tx_ring->tx_bi;
250f1cad2ceSJesse Brandeburg 				tx_desc = IAVF_TX_DESC(tx_ring, 0);
2517f12ad74SGreg Rose 			}
2527f12ad74SGreg Rose 
2537f12ad74SGreg Rose 			/* unmap any remaining paged data */
2547f12ad74SGreg Rose 			if (dma_unmap_len(tx_buf, len)) {
2557f12ad74SGreg Rose 				dma_unmap_page(tx_ring->dev,
2567f12ad74SGreg Rose 					       dma_unmap_addr(tx_buf, dma),
2577f12ad74SGreg Rose 					       dma_unmap_len(tx_buf, len),
2587f12ad74SGreg Rose 					       DMA_TO_DEVICE);
2597f12ad74SGreg Rose 				dma_unmap_len_set(tx_buf, len, 0);
2607f12ad74SGreg Rose 			}
2617f12ad74SGreg Rose 		}
2627f12ad74SGreg Rose 
2637f12ad74SGreg Rose 		/* move us one more past the eop_desc for start of next pkt */
2647f12ad74SGreg Rose 		tx_buf++;
2657f12ad74SGreg Rose 		tx_desc++;
2667f12ad74SGreg Rose 		i++;
2677f12ad74SGreg Rose 		if (unlikely(!i)) {
2687f12ad74SGreg Rose 			i -= tx_ring->count;
2697f12ad74SGreg Rose 			tx_buf = tx_ring->tx_bi;
270f1cad2ceSJesse Brandeburg 			tx_desc = IAVF_TX_DESC(tx_ring, 0);
2717f12ad74SGreg Rose 		}
2727f12ad74SGreg Rose 
273016890b9SJesse Brandeburg 		prefetch(tx_desc);
274016890b9SJesse Brandeburg 
2757f12ad74SGreg Rose 		/* update budget accounting */
2767f12ad74SGreg Rose 		budget--;
2777f12ad74SGreg Rose 	} while (likely(budget));
2787f12ad74SGreg Rose 
2797f12ad74SGreg Rose 	i += tx_ring->count;
2807f12ad74SGreg Rose 	tx_ring->next_to_clean = i;
2817f12ad74SGreg Rose 	u64_stats_update_begin(&tx_ring->syncp);
2827f12ad74SGreg Rose 	tx_ring->stats.bytes += total_bytes;
2837f12ad74SGreg Rose 	tx_ring->stats.packets += total_packets;
2847f12ad74SGreg Rose 	u64_stats_update_end(&tx_ring->syncp);
2857f12ad74SGreg Rose 	tx_ring->q_vector->tx.total_bytes += total_bytes;
2867f12ad74SGreg Rose 	tx_ring->q_vector->tx.total_packets += total_packets;
2877f12ad74SGreg Rose 
28856184e01SJesse Brandeburg 	if (tx_ring->flags & IAVF_TXR_FLAGS_WB_ON_ITR) {
289f6d83d13SAnjali Singhai Jain 		/* check to see if there are < 4 descriptors
290f6d83d13SAnjali Singhai Jain 		 * waiting to be written back, then kick the hardware to force
291f6d83d13SAnjali Singhai Jain 		 * them to be written back in case we stay in NAPI.
292f6d83d13SAnjali Singhai Jain 		 * In this mode on X722 we do not enable Interrupt.
293f6d83d13SAnjali Singhai Jain 		 */
294129cf89eSJesse Brandeburg 		unsigned int j = iavf_get_tx_pending(tx_ring, false);
295f6d83d13SAnjali Singhai Jain 
296f6d83d13SAnjali Singhai Jain 		if (budget &&
2971dc8b538SAlexander Duyck 		    ((j / WB_STRIDE) == 0) && (j > 0) &&
29856184e01SJesse Brandeburg 		    !test_bit(__IAVF_VSI_DOWN, vsi->state) &&
29956184e01SJesse Brandeburg 		    (IAVF_DESC_UNUSED(tx_ring) != tx_ring->count))
300f6d83d13SAnjali Singhai Jain 			tx_ring->arm_wb = true;
301f6d83d13SAnjali Singhai Jain 	}
302f6d83d13SAnjali Singhai Jain 
303e486bdfdSAlexander Duyck 	/* notify netdev of completed buffers */
304e486bdfdSAlexander Duyck 	netdev_tx_completed_queue(txring_txq(tx_ring),
3057f12ad74SGreg Rose 				  total_packets, total_bytes);
3067f12ad74SGreg Rose 
307b85c94b6SJesse Brandeburg #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
3087f12ad74SGreg Rose 	if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
30956184e01SJesse Brandeburg 		     (IAVF_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
3107f12ad74SGreg Rose 		/* Make sure that anybody stopping the queue after this
3117f12ad74SGreg Rose 		 * sees the new next_to_clean.
3127f12ad74SGreg Rose 		 */
3137f12ad74SGreg Rose 		smp_mb();
3147f12ad74SGreg Rose 		if (__netif_subqueue_stopped(tx_ring->netdev,
3157f12ad74SGreg Rose 					     tx_ring->queue_index) &&
31656184e01SJesse Brandeburg 		   !test_bit(__IAVF_VSI_DOWN, vsi->state)) {
3177f12ad74SGreg Rose 			netif_wake_subqueue(tx_ring->netdev,
3187f12ad74SGreg Rose 					    tx_ring->queue_index);
3197f12ad74SGreg Rose 			++tx_ring->tx_stats.restart_queue;
3207f12ad74SGreg Rose 		}
3217f12ad74SGreg Rose 	}
3227f12ad74SGreg Rose 
323b03a8c1fSKiran Patil 	return !!budget;
3247f12ad74SGreg Rose }
3257f12ad74SGreg Rose 
3267f12ad74SGreg Rose /**
327129cf89eSJesse Brandeburg  * iavf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
328c29af37fSAnjali Singhai Jain  * @vsi: the VSI we care about
329ecc6a239SAnjali Singhai Jain  * @q_vector: the vector on which to enable writeback
330c29af37fSAnjali Singhai Jain  *
331c29af37fSAnjali Singhai Jain  **/
33256184e01SJesse Brandeburg static void iavf_enable_wb_on_itr(struct iavf_vsi *vsi,
33356184e01SJesse Brandeburg 				  struct iavf_q_vector *q_vector)
334c29af37fSAnjali Singhai Jain {
3358e0764b4SAnjali Singhai Jain 	u16 flags = q_vector->tx.ring[0].flags;
3368e0764b4SAnjali Singhai Jain 	u32 val;
3378e0764b4SAnjali Singhai Jain 
33856184e01SJesse Brandeburg 	if (!(flags & IAVF_TXR_FLAGS_WB_ON_ITR))
339ecc6a239SAnjali Singhai Jain 		return;
340ecc6a239SAnjali Singhai Jain 
3418e0764b4SAnjali Singhai Jain 	if (q_vector->arm_wb_state)
3428e0764b4SAnjali Singhai Jain 		return;
3438e0764b4SAnjali Singhai Jain 
344f1cad2ceSJesse Brandeburg 	val = IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
345f1cad2ceSJesse Brandeburg 	      IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
3468e0764b4SAnjali Singhai Jain 
3478e0764b4SAnjali Singhai Jain 	wr32(&vsi->back->hw,
348f1cad2ceSJesse Brandeburg 	     IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx), val);
3498e0764b4SAnjali Singhai Jain 	q_vector->arm_wb_state = true;
350ecc6a239SAnjali Singhai Jain }
351ecc6a239SAnjali Singhai Jain 
352ecc6a239SAnjali Singhai Jain /**
353129cf89eSJesse Brandeburg  * iavf_force_wb - Issue SW Interrupt so HW does a wb
354ecc6a239SAnjali Singhai Jain  * @vsi: the VSI we care about
355ecc6a239SAnjali Singhai Jain  * @q_vector: the vector  on which to force writeback
356ecc6a239SAnjali Singhai Jain  *
357ecc6a239SAnjali Singhai Jain  **/
35856184e01SJesse Brandeburg void iavf_force_wb(struct iavf_vsi *vsi, struct iavf_q_vector *q_vector)
359ecc6a239SAnjali Singhai Jain {
360f1cad2ceSJesse Brandeburg 	u32 val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
361f1cad2ceSJesse Brandeburg 		  IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
362f1cad2ceSJesse Brandeburg 		  IAVF_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
363f1cad2ceSJesse Brandeburg 		  IAVF_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
364ecc6a239SAnjali Singhai Jain 		  /* allow 00 to be written to the index */;
365c29af37fSAnjali Singhai Jain 
366c29af37fSAnjali Singhai Jain 	wr32(&vsi->back->hw,
367f1cad2ceSJesse Brandeburg 	     IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx),
368ecc6a239SAnjali Singhai Jain 	     val);
369c29af37fSAnjali Singhai Jain }
370c29af37fSAnjali Singhai Jain 
37156184e01SJesse Brandeburg static inline bool iavf_container_is_rx(struct iavf_q_vector *q_vector,
37256184e01SJesse Brandeburg 					struct iavf_ring_container *rc)
373a0073a4bSAlexander Duyck {
374a0073a4bSAlexander Duyck 	return &q_vector->rx == rc;
375a0073a4bSAlexander Duyck }
376a0073a4bSAlexander Duyck 
37756184e01SJesse Brandeburg static inline unsigned int iavf_itr_divisor(struct iavf_q_vector *q_vector)
378a0073a4bSAlexander Duyck {
379a0073a4bSAlexander Duyck 	unsigned int divisor;
380a0073a4bSAlexander Duyck 
381a0073a4bSAlexander Duyck 	switch (q_vector->adapter->link_speed) {
3827af36e32SAlice Michael 	case IAVF_LINK_SPEED_40GB:
38356184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 1024;
384a0073a4bSAlexander Duyck 		break;
3857af36e32SAlice Michael 	case IAVF_LINK_SPEED_25GB:
3867af36e32SAlice Michael 	case IAVF_LINK_SPEED_20GB:
38756184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 512;
388a0073a4bSAlexander Duyck 		break;
389a0073a4bSAlexander Duyck 	default:
3907af36e32SAlice Michael 	case IAVF_LINK_SPEED_10GB:
39156184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 256;
392a0073a4bSAlexander Duyck 		break;
3937af36e32SAlice Michael 	case IAVF_LINK_SPEED_1GB:
3947af36e32SAlice Michael 	case IAVF_LINK_SPEED_100MB:
39556184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 32;
396a0073a4bSAlexander Duyck 		break;
397a0073a4bSAlexander Duyck 	}
398a0073a4bSAlexander Duyck 
399a0073a4bSAlexander Duyck 	return divisor;
400a0073a4bSAlexander Duyck }
401a0073a4bSAlexander Duyck 
402c29af37fSAnjali Singhai Jain /**
40356184e01SJesse Brandeburg  * iavf_update_itr - update the dynamic ITR value based on statistics
404a0073a4bSAlexander Duyck  * @q_vector: structure containing interrupt and ring information
4057f12ad74SGreg Rose  * @rc: structure containing ring performance data
4067f12ad74SGreg Rose  *
407a0073a4bSAlexander Duyck  * Stores a new ITR value based on packets and byte
408a0073a4bSAlexander Duyck  * counts during the last interrupt.  The advantage of per interrupt
409a0073a4bSAlexander Duyck  * computation is faster updates and more accurate ITR for the current
410a0073a4bSAlexander Duyck  * traffic pattern.  Constants in this function were computed
411a0073a4bSAlexander Duyck  * based on theoretical maximum wire speed and thresholds were set based
412a0073a4bSAlexander Duyck  * on testing data as well as attempting to minimize response time
4137f12ad74SGreg Rose  * while increasing bulk throughput.
4147f12ad74SGreg Rose  **/
41556184e01SJesse Brandeburg static void iavf_update_itr(struct iavf_q_vector *q_vector,
41656184e01SJesse Brandeburg 			    struct iavf_ring_container *rc)
4177f12ad74SGreg Rose {
418a0073a4bSAlexander Duyck 	unsigned int avg_wire_size, packets, bytes, itr;
419a0073a4bSAlexander Duyck 	unsigned long next_update = jiffies;
4207f12ad74SGreg Rose 
421a0073a4bSAlexander Duyck 	/* If we don't have any rings just leave ourselves set for maximum
422a0073a4bSAlexander Duyck 	 * possible latency so we take ourselves out of the equation.
423a0073a4bSAlexander Duyck 	 */
42471dc3719SAlexander Duyck 	if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting))
425a0073a4bSAlexander Duyck 		return;
42671dc3719SAlexander Duyck 
427a0073a4bSAlexander Duyck 	/* For Rx we want to push the delay up and default to low latency.
428a0073a4bSAlexander Duyck 	 * for Tx we want to pull the delay down and default to high latency.
429742c9875SJacob Keller 	 */
43056184e01SJesse Brandeburg 	itr = iavf_container_is_rx(q_vector, rc) ?
43156184e01SJesse Brandeburg 	      IAVF_ITR_ADAPTIVE_MIN_USECS | IAVF_ITR_ADAPTIVE_LATENCY :
43256184e01SJesse Brandeburg 	      IAVF_ITR_ADAPTIVE_MAX_USECS | IAVF_ITR_ADAPTIVE_LATENCY;
433a0073a4bSAlexander Duyck 
434a0073a4bSAlexander Duyck 	/* If we didn't update within up to 1 - 2 jiffies we can assume
435a0073a4bSAlexander Duyck 	 * that either packets are coming in so slow there hasn't been
436a0073a4bSAlexander Duyck 	 * any work, or that there is so much work that NAPI is dealing
437a0073a4bSAlexander Duyck 	 * with interrupt moderation and we don't need to do anything.
438a0073a4bSAlexander Duyck 	 */
439a0073a4bSAlexander Duyck 	if (time_after(next_update, rc->next_update))
440a0073a4bSAlexander Duyck 		goto clear_counts;
441a0073a4bSAlexander Duyck 
442a0073a4bSAlexander Duyck 	/* If itr_countdown is set it means we programmed an ITR within
443a0073a4bSAlexander Duyck 	 * the last 4 interrupt cycles. This has a side effect of us
444a0073a4bSAlexander Duyck 	 * potentially firing an early interrupt. In order to work around
445a0073a4bSAlexander Duyck 	 * this we need to throw out any data received for a few
446a0073a4bSAlexander Duyck 	 * interrupts following the update.
447a0073a4bSAlexander Duyck 	 */
448a0073a4bSAlexander Duyck 	if (q_vector->itr_countdown) {
449a0073a4bSAlexander Duyck 		itr = rc->target_itr;
450a0073a4bSAlexander Duyck 		goto clear_counts;
451742c9875SJacob Keller 	}
452742c9875SJacob Keller 
453a0073a4bSAlexander Duyck 	packets = rc->total_packets;
454a0073a4bSAlexander Duyck 	bytes = rc->total_bytes;
455a0073a4bSAlexander Duyck 
45656184e01SJesse Brandeburg 	if (iavf_container_is_rx(q_vector, rc)) {
457a0073a4bSAlexander Duyck 		/* If Rx there are 1 to 4 packets and bytes are less than
458a0073a4bSAlexander Duyck 		 * 9000 assume insufficient data to use bulk rate limiting
459a0073a4bSAlexander Duyck 		 * approach unless Tx is already in bulk rate limiting. We
460a0073a4bSAlexander Duyck 		 * are likely latency driven.
461a0073a4bSAlexander Duyck 		 */
462a0073a4bSAlexander Duyck 		if (packets && packets < 4 && bytes < 9000 &&
46356184e01SJesse Brandeburg 		    (q_vector->tx.target_itr & IAVF_ITR_ADAPTIVE_LATENCY)) {
46456184e01SJesse Brandeburg 			itr = IAVF_ITR_ADAPTIVE_LATENCY;
465a0073a4bSAlexander Duyck 			goto adjust_by_size;
466a0073a4bSAlexander Duyck 		}
467a0073a4bSAlexander Duyck 	} else if (packets < 4) {
468a0073a4bSAlexander Duyck 		/* If we have Tx and Rx ITR maxed and Tx ITR is running in
469a0073a4bSAlexander Duyck 		 * bulk mode and we are receiving 4 or fewer packets just
470a0073a4bSAlexander Duyck 		 * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
471a0073a4bSAlexander Duyck 		 * that the Rx can relax.
472a0073a4bSAlexander Duyck 		 */
47356184e01SJesse Brandeburg 		if (rc->target_itr == IAVF_ITR_ADAPTIVE_MAX_USECS &&
47456184e01SJesse Brandeburg 		    (q_vector->rx.target_itr & IAVF_ITR_MASK) ==
47556184e01SJesse Brandeburg 		     IAVF_ITR_ADAPTIVE_MAX_USECS)
476a0073a4bSAlexander Duyck 			goto clear_counts;
477a0073a4bSAlexander Duyck 	} else if (packets > 32) {
478a0073a4bSAlexander Duyck 		/* If we have processed over 32 packets in a single interrupt
479a0073a4bSAlexander Duyck 		 * for Tx assume we need to switch over to "bulk" mode.
480a0073a4bSAlexander Duyck 		 */
48156184e01SJesse Brandeburg 		rc->target_itr &= ~IAVF_ITR_ADAPTIVE_LATENCY;
482a0073a4bSAlexander Duyck 	}
483a0073a4bSAlexander Duyck 
484a0073a4bSAlexander Duyck 	/* We have no packets to actually measure against. This means
485a0073a4bSAlexander Duyck 	 * either one of the other queues on this vector is active or
486a0073a4bSAlexander Duyck 	 * we are a Tx queue doing TSO with too high of an interrupt rate.
48751cc6d9fSJesse Brandeburg 	 *
488a0073a4bSAlexander Duyck 	 * Between 4 and 56 we can assume that our current interrupt delay
489a0073a4bSAlexander Duyck 	 * is only slightly too low. As such we should increase it by a small
490a0073a4bSAlexander Duyck 	 * fixed amount.
4917f12ad74SGreg Rose 	 */
492a0073a4bSAlexander Duyck 	if (packets < 56) {
49356184e01SJesse Brandeburg 		itr = rc->target_itr + IAVF_ITR_ADAPTIVE_MIN_INC;
49456184e01SJesse Brandeburg 		if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) {
49556184e01SJesse Brandeburg 			itr &= IAVF_ITR_ADAPTIVE_LATENCY;
49656184e01SJesse Brandeburg 			itr += IAVF_ITR_ADAPTIVE_MAX_USECS;
497a0073a4bSAlexander Duyck 		}
498a0073a4bSAlexander Duyck 		goto clear_counts;
4997f12ad74SGreg Rose 	}
500c56625d5SJesse Brandeburg 
501a0073a4bSAlexander Duyck 	if (packets <= 256) {
502a0073a4bSAlexander Duyck 		itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
50356184e01SJesse Brandeburg 		itr &= IAVF_ITR_MASK;
5047f12ad74SGreg Rose 
505a0073a4bSAlexander Duyck 		/* Between 56 and 112 is our "goldilocks" zone where we are
506a0073a4bSAlexander Duyck 		 * working out "just right". Just report that our current
507a0073a4bSAlexander Duyck 		 * ITR is good for us.
508a0073a4bSAlexander Duyck 		 */
509a0073a4bSAlexander Duyck 		if (packets <= 112)
510a0073a4bSAlexander Duyck 			goto clear_counts;
511a0073a4bSAlexander Duyck 
512a0073a4bSAlexander Duyck 		/* If packet count is 128 or greater we are likely looking
513a0073a4bSAlexander Duyck 		 * at a slight overrun of the delay we want. Try halving
514a0073a4bSAlexander Duyck 		 * our delay to see if that will cut the number of packets
515a0073a4bSAlexander Duyck 		 * in half per interrupt.
516a0073a4bSAlexander Duyck 		 */
517a0073a4bSAlexander Duyck 		itr /= 2;
51856184e01SJesse Brandeburg 		itr &= IAVF_ITR_MASK;
51956184e01SJesse Brandeburg 		if (itr < IAVF_ITR_ADAPTIVE_MIN_USECS)
52056184e01SJesse Brandeburg 			itr = IAVF_ITR_ADAPTIVE_MIN_USECS;
521a0073a4bSAlexander Duyck 
522a0073a4bSAlexander Duyck 		goto clear_counts;
5237f12ad74SGreg Rose 	}
5247f12ad74SGreg Rose 
525a0073a4bSAlexander Duyck 	/* The paths below assume we are dealing with a bulk ITR since
526a0073a4bSAlexander Duyck 	 * number of packets is greater than 256. We are just going to have
527a0073a4bSAlexander Duyck 	 * to compute a value and try to bring the count under control,
528a0073a4bSAlexander Duyck 	 * though for smaller packet sizes there isn't much we can do as
529a0073a4bSAlexander Duyck 	 * NAPI polling will likely be kicking in sooner rather than later.
530a0073a4bSAlexander Duyck 	 */
53156184e01SJesse Brandeburg 	itr = IAVF_ITR_ADAPTIVE_BULK;
532a0073a4bSAlexander Duyck 
533a0073a4bSAlexander Duyck adjust_by_size:
534a0073a4bSAlexander Duyck 	/* If packet counts are 256 or greater we can assume we have a gross
535a0073a4bSAlexander Duyck 	 * overestimation of what the rate should be. Instead of trying to fine
536a0073a4bSAlexander Duyck 	 * tune it just use the formula below to try and dial in an exact value
537a0073a4bSAlexander Duyck 	 * give the current packet size of the frame.
538a0073a4bSAlexander Duyck 	 */
539a0073a4bSAlexander Duyck 	avg_wire_size = bytes / packets;
540a0073a4bSAlexander Duyck 
541a0073a4bSAlexander Duyck 	/* The following is a crude approximation of:
542a0073a4bSAlexander Duyck 	 *  wmem_default / (size + overhead) = desired_pkts_per_int
543a0073a4bSAlexander Duyck 	 *  rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
544a0073a4bSAlexander Duyck 	 *  (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
545a0073a4bSAlexander Duyck 	 *
546a0073a4bSAlexander Duyck 	 * Assuming wmem_default is 212992 and overhead is 640 bytes per
547a0073a4bSAlexander Duyck 	 * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
548a0073a4bSAlexander Duyck 	 * formula down to
549a0073a4bSAlexander Duyck 	 *
550a0073a4bSAlexander Duyck 	 *  (170 * (size + 24)) / (size + 640) = ITR
551a0073a4bSAlexander Duyck 	 *
552a0073a4bSAlexander Duyck 	 * We first do some math on the packet size and then finally bitshift
553a0073a4bSAlexander Duyck 	 * by 8 after rounding up. We also have to account for PCIe link speed
554a0073a4bSAlexander Duyck 	 * difference as ITR scales based on this.
555a0073a4bSAlexander Duyck 	 */
556a0073a4bSAlexander Duyck 	if (avg_wire_size <= 60) {
557a0073a4bSAlexander Duyck 		/* Start at 250k ints/sec */
558a0073a4bSAlexander Duyck 		avg_wire_size = 4096;
559a0073a4bSAlexander Duyck 	} else if (avg_wire_size <= 380) {
560a0073a4bSAlexander Duyck 		/* 250K ints/sec to 60K ints/sec */
561a0073a4bSAlexander Duyck 		avg_wire_size *= 40;
562a0073a4bSAlexander Duyck 		avg_wire_size += 1696;
563a0073a4bSAlexander Duyck 	} else if (avg_wire_size <= 1084) {
564a0073a4bSAlexander Duyck 		/* 60K ints/sec to 36K ints/sec */
565a0073a4bSAlexander Duyck 		avg_wire_size *= 15;
566a0073a4bSAlexander Duyck 		avg_wire_size += 11452;
567a0073a4bSAlexander Duyck 	} else if (avg_wire_size <= 1980) {
568a0073a4bSAlexander Duyck 		/* 36K ints/sec to 30K ints/sec */
569a0073a4bSAlexander Duyck 		avg_wire_size *= 5;
570a0073a4bSAlexander Duyck 		avg_wire_size += 22420;
571a0073a4bSAlexander Duyck 	} else {
572a0073a4bSAlexander Duyck 		/* plateau at a limit of 30K ints/sec */
573a0073a4bSAlexander Duyck 		avg_wire_size = 32256;
574a0073a4bSAlexander Duyck 	}
575a0073a4bSAlexander Duyck 
576a0073a4bSAlexander Duyck 	/* If we are in low latency mode halve our delay which doubles the
577a0073a4bSAlexander Duyck 	 * rate to somewhere between 100K to 16K ints/sec
578a0073a4bSAlexander Duyck 	 */
57956184e01SJesse Brandeburg 	if (itr & IAVF_ITR_ADAPTIVE_LATENCY)
580a0073a4bSAlexander Duyck 		avg_wire_size /= 2;
581a0073a4bSAlexander Duyck 
582a0073a4bSAlexander Duyck 	/* Resultant value is 256 times larger than it needs to be. This
583a0073a4bSAlexander Duyck 	 * gives us room to adjust the value as needed to either increase
584a0073a4bSAlexander Duyck 	 * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc.
585a0073a4bSAlexander Duyck 	 *
586a0073a4bSAlexander Duyck 	 * Use addition as we have already recorded the new latency flag
587a0073a4bSAlexander Duyck 	 * for the ITR value.
588a0073a4bSAlexander Duyck 	 */
58956184e01SJesse Brandeburg 	itr += DIV_ROUND_UP(avg_wire_size, iavf_itr_divisor(q_vector)) *
59056184e01SJesse Brandeburg 	       IAVF_ITR_ADAPTIVE_MIN_INC;
591a0073a4bSAlexander Duyck 
59256184e01SJesse Brandeburg 	if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) {
59356184e01SJesse Brandeburg 		itr &= IAVF_ITR_ADAPTIVE_LATENCY;
59456184e01SJesse Brandeburg 		itr += IAVF_ITR_ADAPTIVE_MAX_USECS;
595a0073a4bSAlexander Duyck 	}
596a0073a4bSAlexander Duyck 
597a0073a4bSAlexander Duyck clear_counts:
598a0073a4bSAlexander Duyck 	/* write back value */
599a0073a4bSAlexander Duyck 	rc->target_itr = itr;
600a0073a4bSAlexander Duyck 
601a0073a4bSAlexander Duyck 	/* next update should occur within next jiffy */
602a0073a4bSAlexander Duyck 	rc->next_update = next_update + 1;
603a0073a4bSAlexander Duyck 
6047f12ad74SGreg Rose 	rc->total_bytes = 0;
6057f12ad74SGreg Rose 	rc->total_packets = 0;
6067f12ad74SGreg Rose }
6077f12ad74SGreg Rose 
6084eeb1fffSJesse Brandeburg /**
609129cf89eSJesse Brandeburg  * iavf_setup_tx_descriptors - Allocate the Tx descriptors
6107f12ad74SGreg Rose  * @tx_ring: the tx ring to set up
6117f12ad74SGreg Rose  *
6127f12ad74SGreg Rose  * Return 0 on success, negative on error
6137f12ad74SGreg Rose  **/
61456184e01SJesse Brandeburg int iavf_setup_tx_descriptors(struct iavf_ring *tx_ring)
6157f12ad74SGreg Rose {
6167f12ad74SGreg Rose 	struct device *dev = tx_ring->dev;
6177f12ad74SGreg Rose 	int bi_size;
6187f12ad74SGreg Rose 
6197f12ad74SGreg Rose 	if (!dev)
6207f12ad74SGreg Rose 		return -ENOMEM;
6217f12ad74SGreg Rose 
62267c818a1SMitch Williams 	/* warn if we are about to overwrite the pointer */
62367c818a1SMitch Williams 	WARN_ON(tx_ring->tx_bi);
62456184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count;
6257f12ad74SGreg Rose 	tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
6267f12ad74SGreg Rose 	if (!tx_ring->tx_bi)
6277f12ad74SGreg Rose 		goto err;
6287f12ad74SGreg Rose 
6297f12ad74SGreg Rose 	/* round up to nearest 4K */
63056184e01SJesse Brandeburg 	tx_ring->size = tx_ring->count * sizeof(struct iavf_tx_desc);
6317f12ad74SGreg Rose 	tx_ring->size = ALIGN(tx_ring->size, 4096);
6327f12ad74SGreg Rose 	tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
6337f12ad74SGreg Rose 					   &tx_ring->dma, GFP_KERNEL);
6347f12ad74SGreg Rose 	if (!tx_ring->desc) {
6357f12ad74SGreg Rose 		dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
6367f12ad74SGreg Rose 			 tx_ring->size);
6377f12ad74SGreg Rose 		goto err;
6387f12ad74SGreg Rose 	}
6397f12ad74SGreg Rose 
6407f12ad74SGreg Rose 	tx_ring->next_to_use = 0;
6417f12ad74SGreg Rose 	tx_ring->next_to_clean = 0;
64207d44190SSudheer Mogilappagari 	tx_ring->tx_stats.prev_pkt_ctr = -1;
6437f12ad74SGreg Rose 	return 0;
6447f12ad74SGreg Rose 
6457f12ad74SGreg Rose err:
6467f12ad74SGreg Rose 	kfree(tx_ring->tx_bi);
6477f12ad74SGreg Rose 	tx_ring->tx_bi = NULL;
6487f12ad74SGreg Rose 	return -ENOMEM;
6497f12ad74SGreg Rose }
6507f12ad74SGreg Rose 
6517f12ad74SGreg Rose /**
652129cf89eSJesse Brandeburg  * iavf_clean_rx_ring - Free Rx buffers
6537f12ad74SGreg Rose  * @rx_ring: ring to be cleaned
6547f12ad74SGreg Rose  **/
65556184e01SJesse Brandeburg void iavf_clean_rx_ring(struct iavf_ring *rx_ring)
6567f12ad74SGreg Rose {
6577f12ad74SGreg Rose 	unsigned long bi_size;
6587f12ad74SGreg Rose 	u16 i;
6597f12ad74SGreg Rose 
6607f12ad74SGreg Rose 	/* ring already cleared, nothing to do */
6617f12ad74SGreg Rose 	if (!rx_ring->rx_bi)
6627f12ad74SGreg Rose 		return;
6637f12ad74SGreg Rose 
664e72e5659SScott Peterson 	if (rx_ring->skb) {
665e72e5659SScott Peterson 		dev_kfree_skb(rx_ring->skb);
666e72e5659SScott Peterson 		rx_ring->skb = NULL;
667e72e5659SScott Peterson 	}
668e72e5659SScott Peterson 
6697f12ad74SGreg Rose 	/* Free all the Rx ring sk_buffs */
6707f12ad74SGreg Rose 	for (i = 0; i < rx_ring->count; i++) {
67156184e01SJesse Brandeburg 		struct iavf_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
672ab9ad98eSJesse Brandeburg 
673ab9ad98eSJesse Brandeburg 		if (!rx_bi->page)
674ab9ad98eSJesse Brandeburg 			continue;
675ab9ad98eSJesse Brandeburg 
67659605bc0SAlexander Duyck 		/* Invalidate cache lines that may have been written to by
67759605bc0SAlexander Duyck 		 * device so that we avoid corrupting memory.
67859605bc0SAlexander Duyck 		 */
67959605bc0SAlexander Duyck 		dma_sync_single_range_for_cpu(rx_ring->dev,
68059605bc0SAlexander Duyck 					      rx_bi->dma,
68159605bc0SAlexander Duyck 					      rx_bi->page_offset,
68298efd694SAlexander Duyck 					      rx_ring->rx_buf_len,
68359605bc0SAlexander Duyck 					      DMA_FROM_DEVICE);
68459605bc0SAlexander Duyck 
68559605bc0SAlexander Duyck 		/* free resources associated with mapping */
68659605bc0SAlexander Duyck 		dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
68756184e01SJesse Brandeburg 				     iavf_rx_pg_size(rx_ring),
68859605bc0SAlexander Duyck 				     DMA_FROM_DEVICE,
68956184e01SJesse Brandeburg 				     IAVF_RX_DMA_ATTR);
69098efd694SAlexander Duyck 
6911793668cSAlexander Duyck 		__page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
692ab9ad98eSJesse Brandeburg 
6937f12ad74SGreg Rose 		rx_bi->page = NULL;
6947f12ad74SGreg Rose 		rx_bi->page_offset = 0;
6957f12ad74SGreg Rose 	}
6967f12ad74SGreg Rose 
69756184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count;
6987f12ad74SGreg Rose 	memset(rx_ring->rx_bi, 0, bi_size);
6997f12ad74SGreg Rose 
7007f12ad74SGreg Rose 	/* Zero out the descriptor ring */
7017f12ad74SGreg Rose 	memset(rx_ring->desc, 0, rx_ring->size);
7027f12ad74SGreg Rose 
703ab9ad98eSJesse Brandeburg 	rx_ring->next_to_alloc = 0;
7047f12ad74SGreg Rose 	rx_ring->next_to_clean = 0;
7057f12ad74SGreg Rose 	rx_ring->next_to_use = 0;
7067f12ad74SGreg Rose }
7077f12ad74SGreg Rose 
7087f12ad74SGreg Rose /**
709129cf89eSJesse Brandeburg  * iavf_free_rx_resources - Free Rx resources
7107f12ad74SGreg Rose  * @rx_ring: ring to clean the resources from
7117f12ad74SGreg Rose  *
7127f12ad74SGreg Rose  * Free all receive software resources
7137f12ad74SGreg Rose  **/
71456184e01SJesse Brandeburg void iavf_free_rx_resources(struct iavf_ring *rx_ring)
7157f12ad74SGreg Rose {
716129cf89eSJesse Brandeburg 	iavf_clean_rx_ring(rx_ring);
7177f12ad74SGreg Rose 	kfree(rx_ring->rx_bi);
7187f12ad74SGreg Rose 	rx_ring->rx_bi = NULL;
7197f12ad74SGreg Rose 
7207f12ad74SGreg Rose 	if (rx_ring->desc) {
7217f12ad74SGreg Rose 		dma_free_coherent(rx_ring->dev, rx_ring->size,
7227f12ad74SGreg Rose 				  rx_ring->desc, rx_ring->dma);
7237f12ad74SGreg Rose 		rx_ring->desc = NULL;
7247f12ad74SGreg Rose 	}
7257f12ad74SGreg Rose }
7267f12ad74SGreg Rose 
7277f12ad74SGreg Rose /**
728129cf89eSJesse Brandeburg  * iavf_setup_rx_descriptors - Allocate Rx descriptors
7297f12ad74SGreg Rose  * @rx_ring: Rx descriptor ring (for a specific queue) to setup
7307f12ad74SGreg Rose  *
7317f12ad74SGreg Rose  * Returns 0 on success, negative on failure
7327f12ad74SGreg Rose  **/
73356184e01SJesse Brandeburg int iavf_setup_rx_descriptors(struct iavf_ring *rx_ring)
7347f12ad74SGreg Rose {
7357f12ad74SGreg Rose 	struct device *dev = rx_ring->dev;
7367f12ad74SGreg Rose 	int bi_size;
7377f12ad74SGreg Rose 
73867c818a1SMitch Williams 	/* warn if we are about to overwrite the pointer */
73967c818a1SMitch Williams 	WARN_ON(rx_ring->rx_bi);
74056184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count;
7417f12ad74SGreg Rose 	rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
7427f12ad74SGreg Rose 	if (!rx_ring->rx_bi)
7437f12ad74SGreg Rose 		goto err;
7447f12ad74SGreg Rose 
745f217d6caSCarolyn Wyborny 	u64_stats_init(&rx_ring->syncp);
746638702bdSCarolyn Wyborny 
7477f12ad74SGreg Rose 	/* Round up to nearest 4K */
74856184e01SJesse Brandeburg 	rx_ring->size = rx_ring->count * sizeof(union iavf_32byte_rx_desc);
7497f12ad74SGreg Rose 	rx_ring->size = ALIGN(rx_ring->size, 4096);
7507f12ad74SGreg Rose 	rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
7517f12ad74SGreg Rose 					   &rx_ring->dma, GFP_KERNEL);
7527f12ad74SGreg Rose 
7537f12ad74SGreg Rose 	if (!rx_ring->desc) {
7547f12ad74SGreg Rose 		dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
7557f12ad74SGreg Rose 			 rx_ring->size);
7567f12ad74SGreg Rose 		goto err;
7577f12ad74SGreg Rose 	}
7587f12ad74SGreg Rose 
759ab9ad98eSJesse Brandeburg 	rx_ring->next_to_alloc = 0;
7607f12ad74SGreg Rose 	rx_ring->next_to_clean = 0;
7617f12ad74SGreg Rose 	rx_ring->next_to_use = 0;
7627f12ad74SGreg Rose 
7637f12ad74SGreg Rose 	return 0;
7647f12ad74SGreg Rose err:
7657f12ad74SGreg Rose 	kfree(rx_ring->rx_bi);
7667f12ad74SGreg Rose 	rx_ring->rx_bi = NULL;
7677f12ad74SGreg Rose 	return -ENOMEM;
7687f12ad74SGreg Rose }
7697f12ad74SGreg Rose 
7707f12ad74SGreg Rose /**
77156184e01SJesse Brandeburg  * iavf_release_rx_desc - Store the new tail and head values
7727f12ad74SGreg Rose  * @rx_ring: ring to bump
7737f12ad74SGreg Rose  * @val: new head index
7747f12ad74SGreg Rose  **/
77556184e01SJesse Brandeburg static inline void iavf_release_rx_desc(struct iavf_ring *rx_ring, u32 val)
7767f12ad74SGreg Rose {
7777f12ad74SGreg Rose 	rx_ring->next_to_use = val;
778ab9ad98eSJesse Brandeburg 
779ab9ad98eSJesse Brandeburg 	/* update next to alloc since we have filled the ring */
780ab9ad98eSJesse Brandeburg 	rx_ring->next_to_alloc = val;
781ab9ad98eSJesse Brandeburg 
7827f12ad74SGreg Rose 	/* Force memory writes to complete before letting h/w
7837f12ad74SGreg Rose 	 * know there are new descriptors to fetch.  (Only
7847f12ad74SGreg Rose 	 * applicable for weak-ordered memory model archs,
7857f12ad74SGreg Rose 	 * such as IA-64).
7867f12ad74SGreg Rose 	 */
7877f12ad74SGreg Rose 	wmb();
7887f12ad74SGreg Rose 	writel(val, rx_ring->tail);
7897f12ad74SGreg Rose }
7907f12ad74SGreg Rose 
7917f12ad74SGreg Rose /**
79256184e01SJesse Brandeburg  * iavf_rx_offset - Return expected offset into page to access data
793ca9ec088SAlexander Duyck  * @rx_ring: Ring we are requesting offset of
794ca9ec088SAlexander Duyck  *
795ca9ec088SAlexander Duyck  * Returns the offset value for ring into the data buffer.
796ca9ec088SAlexander Duyck  */
79756184e01SJesse Brandeburg static inline unsigned int iavf_rx_offset(struct iavf_ring *rx_ring)
798ca9ec088SAlexander Duyck {
79956184e01SJesse Brandeburg 	return ring_uses_build_skb(rx_ring) ? IAVF_SKB_PAD : 0;
800ca9ec088SAlexander Duyck }
801ca9ec088SAlexander Duyck 
802ca9ec088SAlexander Duyck /**
80356184e01SJesse Brandeburg  * iavf_alloc_mapped_page - recycle or make a new page
804ab9ad98eSJesse Brandeburg  * @rx_ring: ring to use
805ab9ad98eSJesse Brandeburg  * @bi: rx_buffer struct to modify
806c2e245abSJesse Brandeburg  *
807ab9ad98eSJesse Brandeburg  * Returns true if the page was successfully allocated or
808ab9ad98eSJesse Brandeburg  * reused.
8097f12ad74SGreg Rose  **/
81056184e01SJesse Brandeburg static bool iavf_alloc_mapped_page(struct iavf_ring *rx_ring,
81156184e01SJesse Brandeburg 				   struct iavf_rx_buffer *bi)
812a132af24SMitch Williams {
813ab9ad98eSJesse Brandeburg 	struct page *page = bi->page;
814ab9ad98eSJesse Brandeburg 	dma_addr_t dma;
815a132af24SMitch Williams 
816ab9ad98eSJesse Brandeburg 	/* since we are recycling buffers we should seldom need to alloc */
817ab9ad98eSJesse Brandeburg 	if (likely(page)) {
818f16704e5SMitch Williams 		rx_ring->rx_stats.page_reuse_count++;
819c2e245abSJesse Brandeburg 		return true;
820a132af24SMitch Williams 	}
821a132af24SMitch Williams 
822ab9ad98eSJesse Brandeburg 	/* alloc new page for storage */
82356184e01SJesse Brandeburg 	page = dev_alloc_pages(iavf_rx_pg_order(rx_ring));
824ab9ad98eSJesse Brandeburg 	if (unlikely(!page)) {
825ab9ad98eSJesse Brandeburg 		rx_ring->rx_stats.alloc_page_failed++;
826c2e245abSJesse Brandeburg 		return false;
8277f12ad74SGreg Rose 	}
8287f12ad74SGreg Rose 
829ab9ad98eSJesse Brandeburg 	/* map page for use */
83059605bc0SAlexander Duyck 	dma = dma_map_page_attrs(rx_ring->dev, page, 0,
83156184e01SJesse Brandeburg 				 iavf_rx_pg_size(rx_ring),
83259605bc0SAlexander Duyck 				 DMA_FROM_DEVICE,
83356184e01SJesse Brandeburg 				 IAVF_RX_DMA_ATTR);
8347f12ad74SGreg Rose 
835ab9ad98eSJesse Brandeburg 	/* if mapping failed free memory back to system since
836ab9ad98eSJesse Brandeburg 	 * there isn't much point in holding memory we can't use
837c2e245abSJesse Brandeburg 	 */
838ab9ad98eSJesse Brandeburg 	if (dma_mapping_error(rx_ring->dev, dma)) {
83956184e01SJesse Brandeburg 		__free_pages(page, iavf_rx_pg_order(rx_ring));
840ab9ad98eSJesse Brandeburg 		rx_ring->rx_stats.alloc_page_failed++;
841ab9ad98eSJesse Brandeburg 		return false;
842ab9ad98eSJesse Brandeburg 	}
843ab9ad98eSJesse Brandeburg 
844ab9ad98eSJesse Brandeburg 	bi->dma = dma;
845ab9ad98eSJesse Brandeburg 	bi->page = page;
84656184e01SJesse Brandeburg 	bi->page_offset = iavf_rx_offset(rx_ring);
847a0cfc313SAlexander Duyck 
848a0cfc313SAlexander Duyck 	/* initialize pagecnt_bias to 1 representing we fully own page */
8491793668cSAlexander Duyck 	bi->pagecnt_bias = 1;
850ab9ad98eSJesse Brandeburg 
851c2e245abSJesse Brandeburg 	return true;
8527f12ad74SGreg Rose }
8537f12ad74SGreg Rose 
8547f12ad74SGreg Rose /**
85556184e01SJesse Brandeburg  * iavf_receive_skb - Send a completed packet up the stack
8567f12ad74SGreg Rose  * @rx_ring:  rx ring in play
8577f12ad74SGreg Rose  * @skb: packet to send up
8587f12ad74SGreg Rose  * @vlan_tag: vlan tag for packet
8597f12ad74SGreg Rose  **/
86056184e01SJesse Brandeburg static void iavf_receive_skb(struct iavf_ring *rx_ring,
8617f12ad74SGreg Rose 			     struct sk_buff *skb, u16 vlan_tag)
8627f12ad74SGreg Rose {
86356184e01SJesse Brandeburg 	struct iavf_q_vector *q_vector = rx_ring->q_vector;
8647f12ad74SGreg Rose 
865a149f2c3SJesse Brandeburg 	if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
866a149f2c3SJesse Brandeburg 	    (vlan_tag & VLAN_VID_MASK))
8677f12ad74SGreg Rose 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
8687f12ad74SGreg Rose 
8697f12ad74SGreg Rose 	napi_gro_receive(&q_vector->napi, skb);
8707f12ad74SGreg Rose }
8717f12ad74SGreg Rose 
8727f12ad74SGreg Rose /**
873129cf89eSJesse Brandeburg  * iavf_alloc_rx_buffers - Replace used receive buffers
874ab9ad98eSJesse Brandeburg  * @rx_ring: ring to place buffers on
875ab9ad98eSJesse Brandeburg  * @cleaned_count: number of buffers to replace
876ab9ad98eSJesse Brandeburg  *
877ab9ad98eSJesse Brandeburg  * Returns false if all allocations were successful, true if any fail
878ab9ad98eSJesse Brandeburg  **/
87956184e01SJesse Brandeburg bool iavf_alloc_rx_buffers(struct iavf_ring *rx_ring, u16 cleaned_count)
880ab9ad98eSJesse Brandeburg {
881ab9ad98eSJesse Brandeburg 	u16 ntu = rx_ring->next_to_use;
88256184e01SJesse Brandeburg 	union iavf_rx_desc *rx_desc;
88356184e01SJesse Brandeburg 	struct iavf_rx_buffer *bi;
884ab9ad98eSJesse Brandeburg 
885ab9ad98eSJesse Brandeburg 	/* do nothing if no valid netdev defined */
886ab9ad98eSJesse Brandeburg 	if (!rx_ring->netdev || !cleaned_count)
887ab9ad98eSJesse Brandeburg 		return false;
888ab9ad98eSJesse Brandeburg 
889f1cad2ceSJesse Brandeburg 	rx_desc = IAVF_RX_DESC(rx_ring, ntu);
890ab9ad98eSJesse Brandeburg 	bi = &rx_ring->rx_bi[ntu];
891ab9ad98eSJesse Brandeburg 
892ab9ad98eSJesse Brandeburg 	do {
89356184e01SJesse Brandeburg 		if (!iavf_alloc_mapped_page(rx_ring, bi))
894ab9ad98eSJesse Brandeburg 			goto no_buffers;
895ab9ad98eSJesse Brandeburg 
89659605bc0SAlexander Duyck 		/* sync the buffer for use by the device */
89759605bc0SAlexander Duyck 		dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
89859605bc0SAlexander Duyck 						 bi->page_offset,
89998efd694SAlexander Duyck 						 rx_ring->rx_buf_len,
90059605bc0SAlexander Duyck 						 DMA_FROM_DEVICE);
90159605bc0SAlexander Duyck 
902ab9ad98eSJesse Brandeburg 		/* Refresh the desc even if buffer_addrs didn't change
903ab9ad98eSJesse Brandeburg 		 * because each write-back erases this info.
904ab9ad98eSJesse Brandeburg 		 */
905ab9ad98eSJesse Brandeburg 		rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
906ab9ad98eSJesse Brandeburg 
907ab9ad98eSJesse Brandeburg 		rx_desc++;
908ab9ad98eSJesse Brandeburg 		bi++;
909ab9ad98eSJesse Brandeburg 		ntu++;
910ab9ad98eSJesse Brandeburg 		if (unlikely(ntu == rx_ring->count)) {
911f1cad2ceSJesse Brandeburg 			rx_desc = IAVF_RX_DESC(rx_ring, 0);
912ab9ad98eSJesse Brandeburg 			bi = rx_ring->rx_bi;
913ab9ad98eSJesse Brandeburg 			ntu = 0;
914ab9ad98eSJesse Brandeburg 		}
915ab9ad98eSJesse Brandeburg 
916ab9ad98eSJesse Brandeburg 		/* clear the status bits for the next_to_use descriptor */
917ab9ad98eSJesse Brandeburg 		rx_desc->wb.qword1.status_error_len = 0;
918ab9ad98eSJesse Brandeburg 
919ab9ad98eSJesse Brandeburg 		cleaned_count--;
920ab9ad98eSJesse Brandeburg 	} while (cleaned_count);
921ab9ad98eSJesse Brandeburg 
922ab9ad98eSJesse Brandeburg 	if (rx_ring->next_to_use != ntu)
92356184e01SJesse Brandeburg 		iavf_release_rx_desc(rx_ring, ntu);
924ab9ad98eSJesse Brandeburg 
925ab9ad98eSJesse Brandeburg 	return false;
926ab9ad98eSJesse Brandeburg 
927ab9ad98eSJesse Brandeburg no_buffers:
928ab9ad98eSJesse Brandeburg 	if (rx_ring->next_to_use != ntu)
92956184e01SJesse Brandeburg 		iavf_release_rx_desc(rx_ring, ntu);
930ab9ad98eSJesse Brandeburg 
931ab9ad98eSJesse Brandeburg 	/* make sure to come back via polling to try again after
932ab9ad98eSJesse Brandeburg 	 * allocation failure
933ab9ad98eSJesse Brandeburg 	 */
934ab9ad98eSJesse Brandeburg 	return true;
935ab9ad98eSJesse Brandeburg }
936ab9ad98eSJesse Brandeburg 
937ab9ad98eSJesse Brandeburg /**
93856184e01SJesse Brandeburg  * iavf_rx_checksum - Indicate in skb if hw indicated a good cksum
9397f12ad74SGreg Rose  * @vsi: the VSI we care about
9407f12ad74SGreg Rose  * @skb: skb currently being received and modified
941ab9ad98eSJesse Brandeburg  * @rx_desc: the receive descriptor
9427f12ad74SGreg Rose  **/
94356184e01SJesse Brandeburg static inline void iavf_rx_checksum(struct iavf_vsi *vsi,
9447f12ad74SGreg Rose 				    struct sk_buff *skb,
94556184e01SJesse Brandeburg 				    union iavf_rx_desc *rx_desc)
9467f12ad74SGreg Rose {
94756184e01SJesse Brandeburg 	struct iavf_rx_ptype_decoded decoded;
948ab9ad98eSJesse Brandeburg 	u32 rx_error, rx_status;
949858296c8SAlexander Duyck 	bool ipv4, ipv6;
950ab9ad98eSJesse Brandeburg 	u8 ptype;
951ab9ad98eSJesse Brandeburg 	u64 qword;
952ab9ad98eSJesse Brandeburg 
953ab9ad98eSJesse Brandeburg 	qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
95456184e01SJesse Brandeburg 	ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >> IAVF_RXD_QW1_PTYPE_SHIFT;
95556184e01SJesse Brandeburg 	rx_error = (qword & IAVF_RXD_QW1_ERROR_MASK) >>
95656184e01SJesse Brandeburg 		   IAVF_RXD_QW1_ERROR_SHIFT;
95756184e01SJesse Brandeburg 	rx_status = (qword & IAVF_RXD_QW1_STATUS_MASK) >>
95856184e01SJesse Brandeburg 		    IAVF_RXD_QW1_STATUS_SHIFT;
959ab9ad98eSJesse Brandeburg 	decoded = decode_rx_desc_ptype(ptype);
9607f12ad74SGreg Rose 
9617f12ad74SGreg Rose 	skb->ip_summed = CHECKSUM_NONE;
9627f12ad74SGreg Rose 
963ab9ad98eSJesse Brandeburg 	skb_checksum_none_assert(skb);
964ab9ad98eSJesse Brandeburg 
9657f12ad74SGreg Rose 	/* Rx csum enabled and ip headers found? */
9668a3c91ccSJesse Brandeburg 	if (!(vsi->netdev->features & NETIF_F_RXCSUM))
9677f12ad74SGreg Rose 		return;
9687f12ad74SGreg Rose 
9698a3c91ccSJesse Brandeburg 	/* did the hardware decode the packet and checksum? */
970f1cad2ceSJesse Brandeburg 	if (!(rx_status & BIT(IAVF_RX_DESC_STATUS_L3L4P_SHIFT)))
9718a3c91ccSJesse Brandeburg 		return;
9728a3c91ccSJesse Brandeburg 
9738a3c91ccSJesse Brandeburg 	/* both known and outer_ip must be set for the below code to work */
9748a3c91ccSJesse Brandeburg 	if (!(decoded.known && decoded.outer_ip))
9758a3c91ccSJesse Brandeburg 		return;
9768a3c91ccSJesse Brandeburg 
97756184e01SJesse Brandeburg 	ipv4 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) &&
97856184e01SJesse Brandeburg 	       (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV4);
97956184e01SJesse Brandeburg 	ipv6 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) &&
98056184e01SJesse Brandeburg 	       (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV6);
9818a3c91ccSJesse Brandeburg 
9828a3c91ccSJesse Brandeburg 	if (ipv4 &&
983f1cad2ceSJesse Brandeburg 	    (rx_error & (BIT(IAVF_RX_DESC_ERROR_IPE_SHIFT) |
984f1cad2ceSJesse Brandeburg 			 BIT(IAVF_RX_DESC_ERROR_EIPE_SHIFT))))
9858a3c91ccSJesse Brandeburg 		goto checksum_fail;
9868a3c91ccSJesse Brandeburg 
987ddf1d0d7SJesse Brandeburg 	/* likely incorrect csum if alternate IP extension headers found */
9888a3c91ccSJesse Brandeburg 	if (ipv6 &&
989f1cad2ceSJesse Brandeburg 	    rx_status & BIT(IAVF_RX_DESC_STATUS_IPV6EXADD_SHIFT))
9908a3c91ccSJesse Brandeburg 		/* don't increment checksum err here, non-fatal err */
9917f12ad74SGreg Rose 		return;
9927f12ad74SGreg Rose 
9938a3c91ccSJesse Brandeburg 	/* there was some L4 error, count error and punt packet to the stack */
994f1cad2ceSJesse Brandeburg 	if (rx_error & BIT(IAVF_RX_DESC_ERROR_L4E_SHIFT))
9958a3c91ccSJesse Brandeburg 		goto checksum_fail;
9967f12ad74SGreg Rose 
9978a3c91ccSJesse Brandeburg 	/* handle packets that were not able to be checksummed due
9988a3c91ccSJesse Brandeburg 	 * to arrival speed, in this case the stack can compute
9998a3c91ccSJesse Brandeburg 	 * the csum.
10008a3c91ccSJesse Brandeburg 	 */
1001f1cad2ceSJesse Brandeburg 	if (rx_error & BIT(IAVF_RX_DESC_ERROR_PPRS_SHIFT))
10028a3c91ccSJesse Brandeburg 		return;
10038a3c91ccSJesse Brandeburg 
1004858296c8SAlexander Duyck 	/* Only report checksum unnecessary for TCP, UDP, or SCTP */
1005858296c8SAlexander Duyck 	switch (decoded.inner_prot) {
100656184e01SJesse Brandeburg 	case IAVF_RX_PTYPE_INNER_PROT_TCP:
100756184e01SJesse Brandeburg 	case IAVF_RX_PTYPE_INNER_PROT_UDP:
100856184e01SJesse Brandeburg 	case IAVF_RX_PTYPE_INNER_PROT_SCTP:
10097f12ad74SGreg Rose 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1010858296c8SAlexander Duyck 		/* fall though */
1011858296c8SAlexander Duyck 	default:
1012858296c8SAlexander Duyck 		break;
1013858296c8SAlexander Duyck 	}
10148a3c91ccSJesse Brandeburg 
10158a3c91ccSJesse Brandeburg 	return;
10168a3c91ccSJesse Brandeburg 
10178a3c91ccSJesse Brandeburg checksum_fail:
10188a3c91ccSJesse Brandeburg 	vsi->back->hw_csum_rx_error++;
10197f12ad74SGreg Rose }
10207f12ad74SGreg Rose 
10217f12ad74SGreg Rose /**
102256184e01SJesse Brandeburg  * iavf_ptype_to_htype - get a hash type
1023206812b5SJesse Brandeburg  * @ptype: the ptype value from the descriptor
1024206812b5SJesse Brandeburg  *
1025206812b5SJesse Brandeburg  * Returns a hash type to be used by skb_set_hash
1026206812b5SJesse Brandeburg  **/
102756184e01SJesse Brandeburg static inline int iavf_ptype_to_htype(u8 ptype)
1028206812b5SJesse Brandeburg {
102956184e01SJesse Brandeburg 	struct iavf_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
1030206812b5SJesse Brandeburg 
1031206812b5SJesse Brandeburg 	if (!decoded.known)
1032206812b5SJesse Brandeburg 		return PKT_HASH_TYPE_NONE;
1033206812b5SJesse Brandeburg 
103456184e01SJesse Brandeburg 	if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP &&
103556184e01SJesse Brandeburg 	    decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY4)
1036206812b5SJesse Brandeburg 		return PKT_HASH_TYPE_L4;
103756184e01SJesse Brandeburg 	else if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP &&
103856184e01SJesse Brandeburg 		 decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY3)
1039206812b5SJesse Brandeburg 		return PKT_HASH_TYPE_L3;
1040206812b5SJesse Brandeburg 	else
1041206812b5SJesse Brandeburg 		return PKT_HASH_TYPE_L2;
1042206812b5SJesse Brandeburg }
1043206812b5SJesse Brandeburg 
1044206812b5SJesse Brandeburg /**
104556184e01SJesse Brandeburg  * iavf_rx_hash - set the hash value in the skb
1046857942fdSAnjali Singhai Jain  * @ring: descriptor ring
1047857942fdSAnjali Singhai Jain  * @rx_desc: specific descriptor
1048f5254429SJacob Keller  * @skb: skb currently being received and modified
1049f5254429SJacob Keller  * @rx_ptype: Rx packet type
1050857942fdSAnjali Singhai Jain  **/
105156184e01SJesse Brandeburg static inline void iavf_rx_hash(struct iavf_ring *ring,
105256184e01SJesse Brandeburg 				union iavf_rx_desc *rx_desc,
1053857942fdSAnjali Singhai Jain 				struct sk_buff *skb,
1054857942fdSAnjali Singhai Jain 				u8 rx_ptype)
1055857942fdSAnjali Singhai Jain {
1056857942fdSAnjali Singhai Jain 	u32 hash;
1057857942fdSAnjali Singhai Jain 	const __le64 rss_mask =
1058f1cad2ceSJesse Brandeburg 		cpu_to_le64((u64)IAVF_RX_DESC_FLTSTAT_RSS_HASH <<
1059f1cad2ceSJesse Brandeburg 			    IAVF_RX_DESC_STATUS_FLTSTAT_SHIFT);
1060857942fdSAnjali Singhai Jain 
1061857942fdSAnjali Singhai Jain 	if (ring->netdev->features & NETIF_F_RXHASH)
1062857942fdSAnjali Singhai Jain 		return;
1063857942fdSAnjali Singhai Jain 
1064857942fdSAnjali Singhai Jain 	if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
1065857942fdSAnjali Singhai Jain 		hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
106656184e01SJesse Brandeburg 		skb_set_hash(skb, hash, iavf_ptype_to_htype(rx_ptype));
1067857942fdSAnjali Singhai Jain 	}
1068857942fdSAnjali Singhai Jain }
1069857942fdSAnjali Singhai Jain 
1070857942fdSAnjali Singhai Jain /**
1071129cf89eSJesse Brandeburg  * iavf_process_skb_fields - Populate skb header fields from Rx descriptor
1072ab9ad98eSJesse Brandeburg  * @rx_ring: rx descriptor ring packet is being transacted on
1073ab9ad98eSJesse Brandeburg  * @rx_desc: pointer to the EOP Rx descriptor
1074ab9ad98eSJesse Brandeburg  * @skb: pointer to current skb being populated
1075ab9ad98eSJesse Brandeburg  * @rx_ptype: the packet type decoded by hardware
10767f12ad74SGreg Rose  *
1077ab9ad98eSJesse Brandeburg  * This function checks the ring, descriptor, and packet information in
1078ab9ad98eSJesse Brandeburg  * order to populate the hash, checksum, VLAN, protocol, and
1079ab9ad98eSJesse Brandeburg  * other fields within the skb.
10807f12ad74SGreg Rose  **/
1081ab9ad98eSJesse Brandeburg static inline
108256184e01SJesse Brandeburg void iavf_process_skb_fields(struct iavf_ring *rx_ring,
108356184e01SJesse Brandeburg 			     union iavf_rx_desc *rx_desc, struct sk_buff *skb,
1084ab9ad98eSJesse Brandeburg 			     u8 rx_ptype)
10857f12ad74SGreg Rose {
108656184e01SJesse Brandeburg 	iavf_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
1087857942fdSAnjali Singhai Jain 
108856184e01SJesse Brandeburg 	iavf_rx_checksum(rx_ring->vsi, skb, rx_desc);
1089a132af24SMitch Williams 
1090ab9ad98eSJesse Brandeburg 	skb_record_rx_queue(skb, rx_ring->queue_index);
1091a5b268e4SAlexander Duyck 
1092a5b268e4SAlexander Duyck 	/* modifies the skb - consumes the enet header */
1093a5b268e4SAlexander Duyck 	skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1094a132af24SMitch Williams }
1095a132af24SMitch Williams 
1096a132af24SMitch Williams /**
109756184e01SJesse Brandeburg  * iavf_cleanup_headers - Correct empty headers
1098ab9ad98eSJesse Brandeburg  * @rx_ring: rx descriptor ring packet is being transacted on
1099ab9ad98eSJesse Brandeburg  * @skb: pointer to current skb being fixed
1100ab9ad98eSJesse Brandeburg  *
1101ab9ad98eSJesse Brandeburg  * Also address the case where we are pulling data in on pages only
1102ab9ad98eSJesse Brandeburg  * and as such no data is present in the skb header.
1103ab9ad98eSJesse Brandeburg  *
1104ab9ad98eSJesse Brandeburg  * In addition if skb is not at least 60 bytes we need to pad it so that
1105ab9ad98eSJesse Brandeburg  * it is large enough to qualify as a valid Ethernet frame.
1106ab9ad98eSJesse Brandeburg  *
1107ab9ad98eSJesse Brandeburg  * Returns true if an error was encountered and skb was freed.
1108a132af24SMitch Williams  **/
110956184e01SJesse Brandeburg static bool iavf_cleanup_headers(struct iavf_ring *rx_ring, struct sk_buff *skb)
1110ab9ad98eSJesse Brandeburg {
1111ab9ad98eSJesse Brandeburg 	/* if eth_skb_pad returns an error the skb was freed */
1112ab9ad98eSJesse Brandeburg 	if (eth_skb_pad(skb))
1113ab9ad98eSJesse Brandeburg 		return true;
1114ab9ad98eSJesse Brandeburg 
1115ab9ad98eSJesse Brandeburg 	return false;
1116ab9ad98eSJesse Brandeburg }
1117ab9ad98eSJesse Brandeburg 
1118ab9ad98eSJesse Brandeburg /**
111956184e01SJesse Brandeburg  * iavf_reuse_rx_page - page flip buffer and store it back on the ring
1120ab9ad98eSJesse Brandeburg  * @rx_ring: rx descriptor ring to store buffers on
1121ab9ad98eSJesse Brandeburg  * @old_buff: donor buffer to have page reused
1122ab9ad98eSJesse Brandeburg  *
1123ab9ad98eSJesse Brandeburg  * Synchronizes page for reuse by the adapter
1124ab9ad98eSJesse Brandeburg  **/
112556184e01SJesse Brandeburg static void iavf_reuse_rx_page(struct iavf_ring *rx_ring,
112656184e01SJesse Brandeburg 			       struct iavf_rx_buffer *old_buff)
1127ab9ad98eSJesse Brandeburg {
112856184e01SJesse Brandeburg 	struct iavf_rx_buffer *new_buff;
1129ab9ad98eSJesse Brandeburg 	u16 nta = rx_ring->next_to_alloc;
1130ab9ad98eSJesse Brandeburg 
1131ab9ad98eSJesse Brandeburg 	new_buff = &rx_ring->rx_bi[nta];
1132ab9ad98eSJesse Brandeburg 
1133ab9ad98eSJesse Brandeburg 	/* update, and store next to alloc */
1134ab9ad98eSJesse Brandeburg 	nta++;
1135ab9ad98eSJesse Brandeburg 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1136ab9ad98eSJesse Brandeburg 
1137ab9ad98eSJesse Brandeburg 	/* transfer page from old buffer to new buffer */
11381793668cSAlexander Duyck 	new_buff->dma		= old_buff->dma;
11391793668cSAlexander Duyck 	new_buff->page		= old_buff->page;
11401793668cSAlexander Duyck 	new_buff->page_offset	= old_buff->page_offset;
11411793668cSAlexander Duyck 	new_buff->pagecnt_bias	= old_buff->pagecnt_bias;
1142ab9ad98eSJesse Brandeburg }
1143ab9ad98eSJesse Brandeburg 
1144ab9ad98eSJesse Brandeburg /**
114556184e01SJesse Brandeburg  * iavf_page_is_reusable - check if any reuse is possible
1146ab9ad98eSJesse Brandeburg  * @page: page struct to check
11479b37c937SScott Peterson  *
11489b37c937SScott Peterson  * A page is not reusable if it was allocated under low memory
11499b37c937SScott Peterson  * conditions, or it's not in the same NUMA node as this CPU.
1150ab9ad98eSJesse Brandeburg  */
115156184e01SJesse Brandeburg static inline bool iavf_page_is_reusable(struct page *page)
1152ab9ad98eSJesse Brandeburg {
11539b37c937SScott Peterson 	return (page_to_nid(page) == numa_mem_id()) &&
11549b37c937SScott Peterson 		!page_is_pfmemalloc(page);
11559b37c937SScott Peterson }
11569b37c937SScott Peterson 
11579b37c937SScott Peterson /**
115856184e01SJesse Brandeburg  * iavf_can_reuse_rx_page - Determine if this page can be reused by
11599b37c937SScott Peterson  * the adapter for another receive
11609b37c937SScott Peterson  *
11619b37c937SScott Peterson  * @rx_buffer: buffer containing the page
11629b37c937SScott Peterson  *
11639b37c937SScott Peterson  * If page is reusable, rx_buffer->page_offset is adjusted to point to
11649b37c937SScott Peterson  * an unused region in the page.
11659b37c937SScott Peterson  *
11669b37c937SScott Peterson  * For small pages, @truesize will be a constant value, half the size
11679b37c937SScott Peterson  * of the memory at page.  We'll attempt to alternate between high and
11689b37c937SScott Peterson  * low halves of the page, with one half ready for use by the hardware
11699b37c937SScott Peterson  * and the other half being consumed by the stack.  We use the page
11709b37c937SScott Peterson  * ref count to determine whether the stack has finished consuming the
11719b37c937SScott Peterson  * portion of this page that was passed up with a previous packet.  If
11729b37c937SScott Peterson  * the page ref count is >1, we'll assume the "other" half page is
11739b37c937SScott Peterson  * still busy, and this page cannot be reused.
11749b37c937SScott Peterson  *
11759b37c937SScott Peterson  * For larger pages, @truesize will be the actual space used by the
11769b37c937SScott Peterson  * received packet (adjusted upward to an even multiple of the cache
11779b37c937SScott Peterson  * line size).  This will advance through the page by the amount
11789b37c937SScott Peterson  * actually consumed by the received packets while there is still
11799b37c937SScott Peterson  * space for a buffer.  Each region of larger pages will be used at
11809b37c937SScott Peterson  * most once, after which the page will not be reused.
11819b37c937SScott Peterson  *
11829b37c937SScott Peterson  * In either case, if the page is reusable its refcount is increased.
11839b37c937SScott Peterson  **/
118456184e01SJesse Brandeburg static bool iavf_can_reuse_rx_page(struct iavf_rx_buffer *rx_buffer)
11859b37c937SScott Peterson {
1186a0cfc313SAlexander Duyck 	unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1187a0cfc313SAlexander Duyck 	struct page *page = rx_buffer->page;
11889b37c937SScott Peterson 
11899b37c937SScott Peterson 	/* Is any reuse possible? */
119056184e01SJesse Brandeburg 	if (unlikely(!iavf_page_is_reusable(page)))
11919b37c937SScott Peterson 		return false;
11929b37c937SScott Peterson 
11939b37c937SScott Peterson #if (PAGE_SIZE < 8192)
11949b37c937SScott Peterson 	/* if we are only owner of page we can reuse it */
1195a0cfc313SAlexander Duyck 	if (unlikely((page_count(page) - pagecnt_bias) > 1))
11969b37c937SScott Peterson 		return false;
11979b37c937SScott Peterson #else
119856184e01SJesse Brandeburg #define IAVF_LAST_OFFSET \
119956184e01SJesse Brandeburg 	(SKB_WITH_OVERHEAD(PAGE_SIZE) - IAVF_RXBUFFER_2048)
120056184e01SJesse Brandeburg 	if (rx_buffer->page_offset > IAVF_LAST_OFFSET)
12019b37c937SScott Peterson 		return false;
12029b37c937SScott Peterson #endif
12039b37c937SScott Peterson 
12041793668cSAlexander Duyck 	/* If we have drained the page fragment pool we need to update
12051793668cSAlexander Duyck 	 * the pagecnt_bias and page count so that we fully restock the
12061793668cSAlexander Duyck 	 * number of references the driver holds.
12071793668cSAlexander Duyck 	 */
1208a0cfc313SAlexander Duyck 	if (unlikely(!pagecnt_bias)) {
12091793668cSAlexander Duyck 		page_ref_add(page, USHRT_MAX);
12101793668cSAlexander Duyck 		rx_buffer->pagecnt_bias = USHRT_MAX;
12111793668cSAlexander Duyck 	}
12129b37c937SScott Peterson 
12139b37c937SScott Peterson 	return true;
1214ab9ad98eSJesse Brandeburg }
1215ab9ad98eSJesse Brandeburg 
1216ab9ad98eSJesse Brandeburg /**
121756184e01SJesse Brandeburg  * iavf_add_rx_frag - Add contents of Rx buffer to sk_buff
1218ab9ad98eSJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
1219ab9ad98eSJesse Brandeburg  * @rx_buffer: buffer containing page to add
1220ab9ad98eSJesse Brandeburg  * @skb: sk_buff to place the data into
1221a0cfc313SAlexander Duyck  * @size: packet length from rx_desc
1222ab9ad98eSJesse Brandeburg  *
1223ab9ad98eSJesse Brandeburg  * This function will add the data contained in rx_buffer->page to the skb.
1224fa2343e9SAlexander Duyck  * It will just attach the page as a frag to the skb.
1225ab9ad98eSJesse Brandeburg  *
1226fa2343e9SAlexander Duyck  * The function will then update the page offset.
1227ab9ad98eSJesse Brandeburg  **/
122856184e01SJesse Brandeburg static void iavf_add_rx_frag(struct iavf_ring *rx_ring,
122956184e01SJesse Brandeburg 			     struct iavf_rx_buffer *rx_buffer,
1230a0cfc313SAlexander Duyck 			     struct sk_buff *skb,
1231a0cfc313SAlexander Duyck 			     unsigned int size)
1232ab9ad98eSJesse Brandeburg {
1233ab9ad98eSJesse Brandeburg #if (PAGE_SIZE < 8192)
123456184e01SJesse Brandeburg 	unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
1235ab9ad98eSJesse Brandeburg #else
123656184e01SJesse Brandeburg 	unsigned int truesize = SKB_DATA_ALIGN(size + iavf_rx_offset(rx_ring));
1237ab9ad98eSJesse Brandeburg #endif
12389b37c937SScott Peterson 
1239efa14c39SMitch Williams 	if (!size)
1240efa14c39SMitch Williams 		return;
1241efa14c39SMitch Williams 
1242fa2343e9SAlexander Duyck 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1243fa2343e9SAlexander Duyck 			rx_buffer->page_offset, size, truesize);
12449b37c937SScott Peterson 
1245a0cfc313SAlexander Duyck 	/* page is being used so we must update the page offset */
1246a0cfc313SAlexander Duyck #if (PAGE_SIZE < 8192)
1247a0cfc313SAlexander Duyck 	rx_buffer->page_offset ^= truesize;
1248a0cfc313SAlexander Duyck #else
1249a0cfc313SAlexander Duyck 	rx_buffer->page_offset += truesize;
1250a0cfc313SAlexander Duyck #endif
1251ab9ad98eSJesse Brandeburg }
1252ab9ad98eSJesse Brandeburg 
1253ab9ad98eSJesse Brandeburg /**
125456184e01SJesse Brandeburg  * iavf_get_rx_buffer - Fetch Rx buffer and synchronize data for use
12559a064128SAlexander Duyck  * @rx_ring: rx descriptor ring to transact packets on
12569a064128SAlexander Duyck  * @size: size of buffer to add to skb
12579a064128SAlexander Duyck  *
12589a064128SAlexander Duyck  * This function will pull an Rx buffer from the ring and synchronize it
12599a064128SAlexander Duyck  * for use by the CPU.
12609a064128SAlexander Duyck  */
126156184e01SJesse Brandeburg static struct iavf_rx_buffer *iavf_get_rx_buffer(struct iavf_ring *rx_ring,
12629a064128SAlexander Duyck 						 const unsigned int size)
12639a064128SAlexander Duyck {
126456184e01SJesse Brandeburg 	struct iavf_rx_buffer *rx_buffer;
12659a064128SAlexander Duyck 
1266efa14c39SMitch Williams 	if (!size)
1267efa14c39SMitch Williams 		return NULL;
1268efa14c39SMitch Williams 
12699a064128SAlexander Duyck 	rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
12709a064128SAlexander Duyck 	prefetchw(rx_buffer->page);
12719a064128SAlexander Duyck 
12729a064128SAlexander Duyck 	/* we are reusing so sync this buffer for CPU use */
12739a064128SAlexander Duyck 	dma_sync_single_range_for_cpu(rx_ring->dev,
12749a064128SAlexander Duyck 				      rx_buffer->dma,
12759a064128SAlexander Duyck 				      rx_buffer->page_offset,
12769a064128SAlexander Duyck 				      size,
12779a064128SAlexander Duyck 				      DMA_FROM_DEVICE);
12789a064128SAlexander Duyck 
1279a0cfc313SAlexander Duyck 	/* We have pulled a buffer for use, so decrement pagecnt_bias */
1280a0cfc313SAlexander Duyck 	rx_buffer->pagecnt_bias--;
1281a0cfc313SAlexander Duyck 
12829a064128SAlexander Duyck 	return rx_buffer;
12839a064128SAlexander Duyck }
12849a064128SAlexander Duyck 
12859a064128SAlexander Duyck /**
128656184e01SJesse Brandeburg  * iavf_construct_skb - Allocate skb and populate it
1287ab9ad98eSJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
12889a064128SAlexander Duyck  * @rx_buffer: rx buffer to pull data from
1289d57c0e08SAlexander Duyck  * @size: size of buffer to add to skb
1290ab9ad98eSJesse Brandeburg  *
1291fa2343e9SAlexander Duyck  * This function allocates an skb.  It then populates it with the page
1292fa2343e9SAlexander Duyck  * data from the current receive descriptor, taking care to set up the
1293fa2343e9SAlexander Duyck  * skb correctly.
1294ab9ad98eSJesse Brandeburg  */
129556184e01SJesse Brandeburg static struct sk_buff *iavf_construct_skb(struct iavf_ring *rx_ring,
129656184e01SJesse Brandeburg 					  struct iavf_rx_buffer *rx_buffer,
1297d57c0e08SAlexander Duyck 					  unsigned int size)
1298ab9ad98eSJesse Brandeburg {
12999fe06a51SColin Ian King 	void *va;
1300fa2343e9SAlexander Duyck #if (PAGE_SIZE < 8192)
130156184e01SJesse Brandeburg 	unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
1302fa2343e9SAlexander Duyck #else
1303fa2343e9SAlexander Duyck 	unsigned int truesize = SKB_DATA_ALIGN(size);
1304fa2343e9SAlexander Duyck #endif
1305fa2343e9SAlexander Duyck 	unsigned int headlen;
1306fa2343e9SAlexander Duyck 	struct sk_buff *skb;
1307ab9ad98eSJesse Brandeburg 
1308efa14c39SMitch Williams 	if (!rx_buffer)
1309efa14c39SMitch Williams 		return NULL;
1310ab9ad98eSJesse Brandeburg 	/* prefetch first cache line of first page */
13119fe06a51SColin Ian King 	va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1312fa2343e9SAlexander Duyck 	prefetch(va);
1313ab9ad98eSJesse Brandeburg #if L1_CACHE_BYTES < 128
1314fa2343e9SAlexander Duyck 	prefetch(va + L1_CACHE_BYTES);
1315ab9ad98eSJesse Brandeburg #endif
1316ab9ad98eSJesse Brandeburg 
1317ab9ad98eSJesse Brandeburg 	/* allocate a skb to store the frags */
1318ab9ad98eSJesse Brandeburg 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
131956184e01SJesse Brandeburg 			       IAVF_RX_HDR_SIZE,
1320ab9ad98eSJesse Brandeburg 			       GFP_ATOMIC | __GFP_NOWARN);
1321fa2343e9SAlexander Duyck 	if (unlikely(!skb))
1322ab9ad98eSJesse Brandeburg 		return NULL;
1323ab9ad98eSJesse Brandeburg 
1324fa2343e9SAlexander Duyck 	/* Determine available headroom for copy */
1325fa2343e9SAlexander Duyck 	headlen = size;
132656184e01SJesse Brandeburg 	if (headlen > IAVF_RX_HDR_SIZE)
1327c43f1255SStanislav Fomichev 		headlen = eth_get_headlen(skb->dev, va, IAVF_RX_HDR_SIZE);
1328fa2343e9SAlexander Duyck 
1329fa2343e9SAlexander Duyck 	/* align pull length to size of long to optimize memcpy performance */
1330fa2343e9SAlexander Duyck 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
1331fa2343e9SAlexander Duyck 
1332fa2343e9SAlexander Duyck 	/* update all of the pointers */
1333fa2343e9SAlexander Duyck 	size -= headlen;
1334fa2343e9SAlexander Duyck 	if (size) {
1335fa2343e9SAlexander Duyck 		skb_add_rx_frag(skb, 0, rx_buffer->page,
1336fa2343e9SAlexander Duyck 				rx_buffer->page_offset + headlen,
1337fa2343e9SAlexander Duyck 				size, truesize);
1338fa2343e9SAlexander Duyck 
1339fa2343e9SAlexander Duyck 		/* buffer is used by skb, update page_offset */
1340fa2343e9SAlexander Duyck #if (PAGE_SIZE < 8192)
1341fa2343e9SAlexander Duyck 		rx_buffer->page_offset ^= truesize;
1342fa2343e9SAlexander Duyck #else
1343fa2343e9SAlexander Duyck 		rx_buffer->page_offset += truesize;
1344fa2343e9SAlexander Duyck #endif
1345fa2343e9SAlexander Duyck 	} else {
1346fa2343e9SAlexander Duyck 		/* buffer is unused, reset bias back to rx_buffer */
1347fa2343e9SAlexander Duyck 		rx_buffer->pagecnt_bias++;
1348fa2343e9SAlexander Duyck 	}
1349a0cfc313SAlexander Duyck 
1350a0cfc313SAlexander Duyck 	return skb;
1351a0cfc313SAlexander Duyck }
1352a0cfc313SAlexander Duyck 
1353a0cfc313SAlexander Duyck /**
135456184e01SJesse Brandeburg  * iavf_build_skb - Build skb around an existing buffer
1355f8b45b74SAlexander Duyck  * @rx_ring: Rx descriptor ring to transact packets on
1356f8b45b74SAlexander Duyck  * @rx_buffer: Rx buffer to pull data from
1357f8b45b74SAlexander Duyck  * @size: size of buffer to add to skb
1358f8b45b74SAlexander Duyck  *
1359f8b45b74SAlexander Duyck  * This function builds an skb around an existing Rx buffer, taking care
1360f8b45b74SAlexander Duyck  * to set up the skb correctly and avoid any memcpy overhead.
1361f8b45b74SAlexander Duyck  */
136256184e01SJesse Brandeburg static struct sk_buff *iavf_build_skb(struct iavf_ring *rx_ring,
136356184e01SJesse Brandeburg 				      struct iavf_rx_buffer *rx_buffer,
1364f8b45b74SAlexander Duyck 				      unsigned int size)
1365f8b45b74SAlexander Duyck {
13669fe06a51SColin Ian King 	void *va;
1367f8b45b74SAlexander Duyck #if (PAGE_SIZE < 8192)
136856184e01SJesse Brandeburg 	unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
1369f8b45b74SAlexander Duyck #else
13702aae918cSBjörn Töpel 	unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
137156184e01SJesse Brandeburg 				SKB_DATA_ALIGN(IAVF_SKB_PAD + size);
1372f8b45b74SAlexander Duyck #endif
1373f8b45b74SAlexander Duyck 	struct sk_buff *skb;
1374f8b45b74SAlexander Duyck 
1375efa14c39SMitch Williams 	if (!rx_buffer)
1376efa14c39SMitch Williams 		return NULL;
1377f8b45b74SAlexander Duyck 	/* prefetch first cache line of first page */
13789fe06a51SColin Ian King 	va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1379f8b45b74SAlexander Duyck 	prefetch(va);
1380f8b45b74SAlexander Duyck #if L1_CACHE_BYTES < 128
1381f8b45b74SAlexander Duyck 	prefetch(va + L1_CACHE_BYTES);
1382f8b45b74SAlexander Duyck #endif
1383f8b45b74SAlexander Duyck 	/* build an skb around the page buffer */
138456184e01SJesse Brandeburg 	skb = build_skb(va - IAVF_SKB_PAD, truesize);
1385f8b45b74SAlexander Duyck 	if (unlikely(!skb))
1386f8b45b74SAlexander Duyck 		return NULL;
1387f8b45b74SAlexander Duyck 
1388f8b45b74SAlexander Duyck 	/* update pointers within the skb to store the data */
138956184e01SJesse Brandeburg 	skb_reserve(skb, IAVF_SKB_PAD);
1390f8b45b74SAlexander Duyck 	__skb_put(skb, size);
1391f8b45b74SAlexander Duyck 
1392f8b45b74SAlexander Duyck 	/* buffer is used by skb, update page_offset */
1393f8b45b74SAlexander Duyck #if (PAGE_SIZE < 8192)
1394f8b45b74SAlexander Duyck 	rx_buffer->page_offset ^= truesize;
1395f8b45b74SAlexander Duyck #else
1396f8b45b74SAlexander Duyck 	rx_buffer->page_offset += truesize;
1397f8b45b74SAlexander Duyck #endif
1398f8b45b74SAlexander Duyck 
1399f8b45b74SAlexander Duyck 	return skb;
1400f8b45b74SAlexander Duyck }
1401f8b45b74SAlexander Duyck 
1402f8b45b74SAlexander Duyck /**
140356184e01SJesse Brandeburg  * iavf_put_rx_buffer - Clean up used buffer and either recycle or free
1404a0cfc313SAlexander Duyck  * @rx_ring: rx descriptor ring to transact packets on
1405a0cfc313SAlexander Duyck  * @rx_buffer: rx buffer to pull data from
1406a0cfc313SAlexander Duyck  *
1407a0cfc313SAlexander Duyck  * This function will clean up the contents of the rx_buffer.  It will
140811a350c9SAlan Brady  * either recycle the buffer or unmap it and free the associated resources.
1409a0cfc313SAlexander Duyck  */
141056184e01SJesse Brandeburg static void iavf_put_rx_buffer(struct iavf_ring *rx_ring,
141156184e01SJesse Brandeburg 			       struct iavf_rx_buffer *rx_buffer)
1412a0cfc313SAlexander Duyck {
1413efa14c39SMitch Williams 	if (!rx_buffer)
1414efa14c39SMitch Williams 		return;
1415efa14c39SMitch Williams 
141656184e01SJesse Brandeburg 	if (iavf_can_reuse_rx_page(rx_buffer)) {
1417ab9ad98eSJesse Brandeburg 		/* hand second half of page back to the ring */
141856184e01SJesse Brandeburg 		iavf_reuse_rx_page(rx_ring, rx_buffer);
1419ab9ad98eSJesse Brandeburg 		rx_ring->rx_stats.page_reuse_count++;
1420ab9ad98eSJesse Brandeburg 	} else {
1421ab9ad98eSJesse Brandeburg 		/* we are not reusing the buffer so unmap it */
142298efd694SAlexander Duyck 		dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
142356184e01SJesse Brandeburg 				     iavf_rx_pg_size(rx_ring),
142456184e01SJesse Brandeburg 				     DMA_FROM_DEVICE, IAVF_RX_DMA_ATTR);
14251793668cSAlexander Duyck 		__page_frag_cache_drain(rx_buffer->page,
14261793668cSAlexander Duyck 					rx_buffer->pagecnt_bias);
1427ab9ad98eSJesse Brandeburg 	}
1428ab9ad98eSJesse Brandeburg 
1429ab9ad98eSJesse Brandeburg 	/* clear contents of buffer_info */
1430ab9ad98eSJesse Brandeburg 	rx_buffer->page = NULL;
1431ab9ad98eSJesse Brandeburg }
1432ab9ad98eSJesse Brandeburg 
1433ab9ad98eSJesse Brandeburg /**
143456184e01SJesse Brandeburg  * iavf_is_non_eop - process handling of non-EOP buffers
1435ab9ad98eSJesse Brandeburg  * @rx_ring: Rx ring being processed
1436ab9ad98eSJesse Brandeburg  * @rx_desc: Rx descriptor for current buffer
1437ab9ad98eSJesse Brandeburg  * @skb: Current socket buffer containing buffer in progress
1438ab9ad98eSJesse Brandeburg  *
1439ab9ad98eSJesse Brandeburg  * This function updates next to clean.  If the buffer is an EOP buffer
1440ab9ad98eSJesse Brandeburg  * this function exits returning false, otherwise it will place the
1441ab9ad98eSJesse Brandeburg  * sk_buff in the next buffer to be chained and return true indicating
1442ab9ad98eSJesse Brandeburg  * that this is in fact a non-EOP buffer.
1443ab9ad98eSJesse Brandeburg  **/
144456184e01SJesse Brandeburg static bool iavf_is_non_eop(struct iavf_ring *rx_ring,
144556184e01SJesse Brandeburg 			    union iavf_rx_desc *rx_desc,
1446ab9ad98eSJesse Brandeburg 			    struct sk_buff *skb)
1447ab9ad98eSJesse Brandeburg {
1448ab9ad98eSJesse Brandeburg 	u32 ntc = rx_ring->next_to_clean + 1;
1449ab9ad98eSJesse Brandeburg 
1450ab9ad98eSJesse Brandeburg 	/* fetch, update, and store next to clean */
1451ab9ad98eSJesse Brandeburg 	ntc = (ntc < rx_ring->count) ? ntc : 0;
1452ab9ad98eSJesse Brandeburg 	rx_ring->next_to_clean = ntc;
1453ab9ad98eSJesse Brandeburg 
1454f1cad2ceSJesse Brandeburg 	prefetch(IAVF_RX_DESC(rx_ring, ntc));
1455ab9ad98eSJesse Brandeburg 
1456ab9ad98eSJesse Brandeburg 	/* if we are the last buffer then there is nothing else to do */
145756184e01SJesse Brandeburg #define IAVF_RXD_EOF BIT(IAVF_RX_DESC_STATUS_EOF_SHIFT)
145856184e01SJesse Brandeburg 	if (likely(iavf_test_staterr(rx_desc, IAVF_RXD_EOF)))
1459ab9ad98eSJesse Brandeburg 		return false;
1460ab9ad98eSJesse Brandeburg 
1461ab9ad98eSJesse Brandeburg 	rx_ring->rx_stats.non_eop_descs++;
1462ab9ad98eSJesse Brandeburg 
1463ab9ad98eSJesse Brandeburg 	return true;
1464ab9ad98eSJesse Brandeburg }
1465ab9ad98eSJesse Brandeburg 
1466ab9ad98eSJesse Brandeburg /**
146756184e01SJesse Brandeburg  * iavf_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
1468ab9ad98eSJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
1469ab9ad98eSJesse Brandeburg  * @budget: Total limit on number of packets to process
1470ab9ad98eSJesse Brandeburg  *
1471ab9ad98eSJesse Brandeburg  * This function provides a "bounce buffer" approach to Rx interrupt
1472ab9ad98eSJesse Brandeburg  * processing.  The advantage to this is that on systems that have
1473ab9ad98eSJesse Brandeburg  * expensive overhead for IOMMU access this provides a means of avoiding
1474ab9ad98eSJesse Brandeburg  * it by maintaining the mapping of the page to the system.
1475ab9ad98eSJesse Brandeburg  *
1476ab9ad98eSJesse Brandeburg  * Returns amount of work completed
1477ab9ad98eSJesse Brandeburg  **/
147856184e01SJesse Brandeburg static int iavf_clean_rx_irq(struct iavf_ring *rx_ring, int budget)
1479a132af24SMitch Williams {
1480a132af24SMitch Williams 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1481e72e5659SScott Peterson 	struct sk_buff *skb = rx_ring->skb;
148256184e01SJesse Brandeburg 	u16 cleaned_count = IAVF_DESC_UNUSED(rx_ring);
1483c2e245abSJesse Brandeburg 	bool failure = false;
1484ab9ad98eSJesse Brandeburg 
1485b85c94b6SJesse Brandeburg 	while (likely(total_rx_packets < (unsigned int)budget)) {
148656184e01SJesse Brandeburg 		struct iavf_rx_buffer *rx_buffer;
148756184e01SJesse Brandeburg 		union iavf_rx_desc *rx_desc;
1488d57c0e08SAlexander Duyck 		unsigned int size;
1489ab9ad98eSJesse Brandeburg 		u16 vlan_tag;
1490a132af24SMitch Williams 		u8 rx_ptype;
1491a132af24SMitch Williams 		u64 qword;
1492a132af24SMitch Williams 
1493a132af24SMitch Williams 		/* return some buffers to hardware, one at a time is too slow */
149456184e01SJesse Brandeburg 		if (cleaned_count >= IAVF_RX_BUFFER_WRITE) {
1495c2e245abSJesse Brandeburg 			failure = failure ||
1496129cf89eSJesse Brandeburg 				  iavf_alloc_rx_buffers(rx_ring, cleaned_count);
1497a132af24SMitch Williams 			cleaned_count = 0;
1498a132af24SMitch Williams 		}
1499a132af24SMitch Williams 
1500f1cad2ceSJesse Brandeburg 		rx_desc = IAVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
1501ab9ad98eSJesse Brandeburg 
1502ab9ad98eSJesse Brandeburg 		/* status_error_len will always be zero for unused descriptors
1503ab9ad98eSJesse Brandeburg 		 * because it's cleared in cleanup, and overlaps with hdr_addr
1504ab9ad98eSJesse Brandeburg 		 * which is always zero because packet split isn't used, if the
1505d57c0e08SAlexander Duyck 		 * hardware wrote DD then the length will be non-zero
1506ab9ad98eSJesse Brandeburg 		 */
1507d57c0e08SAlexander Duyck 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
1508ab9ad98eSJesse Brandeburg 
1509a132af24SMitch Williams 		/* This memory barrier is needed to keep us from reading
1510d57c0e08SAlexander Duyck 		 * any other fields out of the rx_desc until we have
1511d57c0e08SAlexander Duyck 		 * verified the descriptor has been written back.
1512a132af24SMitch Williams 		 */
151367317166SAlexander Duyck 		dma_rmb();
1514efa14c39SMitch Williams #define IAVF_RXD_DD BIT(IAVF_RX_DESC_STATUS_DD_SHIFT)
1515efa14c39SMitch Williams 		if (!iavf_test_staterr(rx_desc, IAVF_RXD_DD))
1516efa14c39SMitch Williams 			break;
1517a132af24SMitch Williams 
151856184e01SJesse Brandeburg 		size = (qword & IAVF_RXD_QW1_LENGTH_PBUF_MASK) >>
151956184e01SJesse Brandeburg 		       IAVF_RXD_QW1_LENGTH_PBUF_SHIFT;
15200e626ff7SAlexander Duyck 
1521ad64ed8bSJesse Brandeburg 		iavf_trace(clean_rx_irq, rx_ring, rx_desc, skb);
152256184e01SJesse Brandeburg 		rx_buffer = iavf_get_rx_buffer(rx_ring, size);
15239a064128SAlexander Duyck 
1524fa2343e9SAlexander Duyck 		/* retrieve a buffer from the ring */
1525fa2343e9SAlexander Duyck 		if (skb)
152656184e01SJesse Brandeburg 			iavf_add_rx_frag(rx_ring, rx_buffer, skb, size);
1527f8b45b74SAlexander Duyck 		else if (ring_uses_build_skb(rx_ring))
152856184e01SJesse Brandeburg 			skb = iavf_build_skb(rx_ring, rx_buffer, size);
1529fa2343e9SAlexander Duyck 		else
153056184e01SJesse Brandeburg 			skb = iavf_construct_skb(rx_ring, rx_buffer, size);
1531fa2343e9SAlexander Duyck 
1532fa2343e9SAlexander Duyck 		/* exit if we failed to retrieve a buffer */
1533fa2343e9SAlexander Duyck 		if (!skb) {
1534fa2343e9SAlexander Duyck 			rx_ring->rx_stats.alloc_buff_failed++;
1535efa14c39SMitch Williams 			if (rx_buffer)
1536fa2343e9SAlexander Duyck 				rx_buffer->pagecnt_bias++;
1537ab9ad98eSJesse Brandeburg 			break;
1538fa2343e9SAlexander Duyck 		}
1539a132af24SMitch Williams 
154056184e01SJesse Brandeburg 		iavf_put_rx_buffer(rx_ring, rx_buffer);
1541a132af24SMitch Williams 		cleaned_count++;
1542a132af24SMitch Williams 
154356184e01SJesse Brandeburg 		if (iavf_is_non_eop(rx_ring, rx_desc, skb))
1544a132af24SMitch Williams 			continue;
1545a132af24SMitch Williams 
1546ab9ad98eSJesse Brandeburg 		/* ERR_MASK will only have valid bits if EOP set, and
1547ab9ad98eSJesse Brandeburg 		 * what we are doing here is actually checking
1548f1cad2ceSJesse Brandeburg 		 * IAVF_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
1549ab9ad98eSJesse Brandeburg 		 * the error field
1550ab9ad98eSJesse Brandeburg 		 */
155156184e01SJesse Brandeburg 		if (unlikely(iavf_test_staterr(rx_desc, BIT(IAVF_RXD_QW1_ERROR_SHIFT)))) {
1552a132af24SMitch Williams 			dev_kfree_skb_any(skb);
1553741b8b83SAlexander Duyck 			skb = NULL;
1554a132af24SMitch Williams 			continue;
15557f12ad74SGreg Rose 		}
15567f12ad74SGreg Rose 
155756184e01SJesse Brandeburg 		if (iavf_cleanup_headers(rx_ring, skb)) {
1558e72e5659SScott Peterson 			skb = NULL;
1559ab9ad98eSJesse Brandeburg 			continue;
1560e72e5659SScott Peterson 		}
1561ab9ad98eSJesse Brandeburg 
15627f12ad74SGreg Rose 		/* probably a little skewed due to removing CRC */
15637f12ad74SGreg Rose 		total_rx_bytes += skb->len;
15647f12ad74SGreg Rose 
156599dad8b3SAlexander Duyck 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
156656184e01SJesse Brandeburg 		rx_ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >>
156756184e01SJesse Brandeburg 			   IAVF_RXD_QW1_PTYPE_SHIFT;
156899dad8b3SAlexander Duyck 
1569ab9ad98eSJesse Brandeburg 		/* populate checksum, VLAN, and protocol */
1570129cf89eSJesse Brandeburg 		iavf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
15717f12ad74SGreg Rose 
15727f12ad74SGreg Rose 
1573f1cad2ceSJesse Brandeburg 		vlan_tag = (qword & BIT(IAVF_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
1574ab9ad98eSJesse Brandeburg 			   le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
1575ab9ad98eSJesse Brandeburg 
1576ad64ed8bSJesse Brandeburg 		iavf_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
157756184e01SJesse Brandeburg 		iavf_receive_skb(rx_ring, skb, vlan_tag);
1578e72e5659SScott Peterson 		skb = NULL;
15797f12ad74SGreg Rose 
1580ab9ad98eSJesse Brandeburg 		/* update budget accounting */
1581ab9ad98eSJesse Brandeburg 		total_rx_packets++;
1582ab9ad98eSJesse Brandeburg 	}
15837f12ad74SGreg Rose 
1584e72e5659SScott Peterson 	rx_ring->skb = skb;
1585e72e5659SScott Peterson 
15867f12ad74SGreg Rose 	u64_stats_update_begin(&rx_ring->syncp);
15877f12ad74SGreg Rose 	rx_ring->stats.packets += total_rx_packets;
15887f12ad74SGreg Rose 	rx_ring->stats.bytes += total_rx_bytes;
15897f12ad74SGreg Rose 	u64_stats_update_end(&rx_ring->syncp);
15907f12ad74SGreg Rose 	rx_ring->q_vector->rx.total_packets += total_rx_packets;
15917f12ad74SGreg Rose 	rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
15927f12ad74SGreg Rose 
1593ab9ad98eSJesse Brandeburg 	/* guarantee a trip back through this routine if there was a failure */
1594b85c94b6SJesse Brandeburg 	return failure ? budget : (int)total_rx_packets;
15957f12ad74SGreg Rose }
15967f12ad74SGreg Rose 
159756184e01SJesse Brandeburg static inline u32 iavf_buildreg_itr(const int type, u16 itr)
15988f5e39ceSJesse Brandeburg {
15998f5e39ceSJesse Brandeburg 	u32 val;
16008f5e39ceSJesse Brandeburg 
16014ff17929SAlexander Duyck 	/* We don't bother with setting the CLEARPBA bit as the data sheet
16024ff17929SAlexander Duyck 	 * points out doing so is "meaningless since it was already
16034ff17929SAlexander Duyck 	 * auto-cleared". The auto-clearing happens when the interrupt is
16044ff17929SAlexander Duyck 	 * asserted.
16054ff17929SAlexander Duyck 	 *
16064ff17929SAlexander Duyck 	 * Hardware errata 28 for also indicates that writing to a
16074ff17929SAlexander Duyck 	 * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear
16084ff17929SAlexander Duyck 	 * an event in the PBA anyway so we need to rely on the automask
16094ff17929SAlexander Duyck 	 * to hold pending events for us until the interrupt is re-enabled
161092418fb1SAlexander Duyck 	 *
161192418fb1SAlexander Duyck 	 * The itr value is reported in microseconds, and the register
161292418fb1SAlexander Duyck 	 * value is recorded in 2 microsecond units. For this reason we
161392418fb1SAlexander Duyck 	 * only need to shift by the interval shift - 1 instead of the
161492418fb1SAlexander Duyck 	 * full value.
16154ff17929SAlexander Duyck 	 */
161656184e01SJesse Brandeburg 	itr &= IAVF_ITR_MASK;
161792418fb1SAlexander Duyck 
1618f1cad2ceSJesse Brandeburg 	val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1619f1cad2ceSJesse Brandeburg 	      (type << IAVF_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
1620f1cad2ceSJesse Brandeburg 	      (itr << (IAVF_VFINT_DYN_CTLN1_INTERVAL_SHIFT - 1));
16218f5e39ceSJesse Brandeburg 
16228f5e39ceSJesse Brandeburg 	return val;
16238f5e39ceSJesse Brandeburg }
16248f5e39ceSJesse Brandeburg 
16258f5e39ceSJesse Brandeburg /* a small macro to shorten up some long lines */
1626f1cad2ceSJesse Brandeburg #define INTREG IAVF_VFINT_DYN_CTLN1
16278f5e39ceSJesse Brandeburg 
1628a0073a4bSAlexander Duyck /* The act of updating the ITR will cause it to immediately trigger. In order
1629a0073a4bSAlexander Duyck  * to prevent this from throwing off adaptive update statistics we defer the
1630a0073a4bSAlexander Duyck  * update so that it can only happen so often. So after either Tx or Rx are
1631a0073a4bSAlexander Duyck  * updated we make the adaptive scheme wait until either the ITR completely
1632a0073a4bSAlexander Duyck  * expires via the next_update expiration or we have been through at least
1633a0073a4bSAlexander Duyck  * 3 interrupts.
1634a0073a4bSAlexander Duyck  */
1635a0073a4bSAlexander Duyck #define ITR_COUNTDOWN_START 3
1636a0073a4bSAlexander Duyck 
16377f12ad74SGreg Rose /**
163856184e01SJesse Brandeburg  * iavf_update_enable_itr - Update itr and re-enable MSIX interrupt
1639de32e3efSCarolyn Wyborny  * @vsi: the VSI we care about
1640de32e3efSCarolyn Wyborny  * @q_vector: q_vector for which itr is being updated and interrupt enabled
1641de32e3efSCarolyn Wyborny  *
1642de32e3efSCarolyn Wyborny  **/
164356184e01SJesse Brandeburg static inline void iavf_update_enable_itr(struct iavf_vsi *vsi,
164456184e01SJesse Brandeburg 					  struct iavf_q_vector *q_vector)
1645de32e3efSCarolyn Wyborny {
1646f349daa5SJesse Brandeburg 	struct iavf_hw *hw = &vsi->back->hw;
1647556fdfd6SAlexander Duyck 	u32 intval;
16488f5e39ceSJesse Brandeburg 
1649a0073a4bSAlexander Duyck 	/* These will do nothing if dynamic updates are not enabled */
165056184e01SJesse Brandeburg 	iavf_update_itr(q_vector, &q_vector->tx);
165156184e01SJesse Brandeburg 	iavf_update_itr(q_vector, &q_vector->rx);
1652ee2319cfSJesse Brandeburg 
1653a0073a4bSAlexander Duyck 	/* This block of logic allows us to get away with only updating
1654a0073a4bSAlexander Duyck 	 * one ITR value with each interrupt. The idea is to perform a
1655a0073a4bSAlexander Duyck 	 * pseudo-lazy update with the following criteria.
1656a0073a4bSAlexander Duyck 	 *
1657a0073a4bSAlexander Duyck 	 * 1. Rx is given higher priority than Tx if both are in same state
1658a0073a4bSAlexander Duyck 	 * 2. If we must reduce an ITR that is given highest priority.
1659a0073a4bSAlexander Duyck 	 * 3. We then give priority to increasing ITR based on amount.
16608f5e39ceSJesse Brandeburg 	 */
1661a0073a4bSAlexander Duyck 	if (q_vector->rx.target_itr < q_vector->rx.current_itr) {
1662a0073a4bSAlexander Duyck 		/* Rx ITR needs to be reduced, this is highest priority */
166356184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_RX_ITR,
1664556fdfd6SAlexander Duyck 					   q_vector->rx.target_itr);
1665556fdfd6SAlexander Duyck 		q_vector->rx.current_itr = q_vector->rx.target_itr;
1666a0073a4bSAlexander Duyck 		q_vector->itr_countdown = ITR_COUNTDOWN_START;
1667a0073a4bSAlexander Duyck 	} else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) ||
1668a0073a4bSAlexander Duyck 		   ((q_vector->rx.target_itr - q_vector->rx.current_itr) <
1669a0073a4bSAlexander Duyck 		    (q_vector->tx.target_itr - q_vector->tx.current_itr))) {
1670a0073a4bSAlexander Duyck 		/* Tx ITR needs to be reduced, this is second priority
1671a0073a4bSAlexander Duyck 		 * Tx ITR needs to be increased more than Rx, fourth priority
1672556fdfd6SAlexander Duyck 		 */
167356184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_TX_ITR,
1674556fdfd6SAlexander Duyck 					   q_vector->tx.target_itr);
1675556fdfd6SAlexander Duyck 		q_vector->tx.current_itr = q_vector->tx.target_itr;
1676a0073a4bSAlexander Duyck 		q_vector->itr_countdown = ITR_COUNTDOWN_START;
1677a0073a4bSAlexander Duyck 	} else if (q_vector->rx.current_itr != q_vector->rx.target_itr) {
1678a0073a4bSAlexander Duyck 		/* Rx ITR needs to be increased, third priority */
167956184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_RX_ITR,
1680a0073a4bSAlexander Duyck 					   q_vector->rx.target_itr);
1681a0073a4bSAlexander Duyck 		q_vector->rx.current_itr = q_vector->rx.target_itr;
1682a0073a4bSAlexander Duyck 		q_vector->itr_countdown = ITR_COUNTDOWN_START;
1683556fdfd6SAlexander Duyck 	} else {
1684a0073a4bSAlexander Duyck 		/* No ITR update, lowest priority */
168556184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_ITR_NONE, 0);
1686a0073a4bSAlexander Duyck 		if (q_vector->itr_countdown)
1687a0073a4bSAlexander Duyck 			q_vector->itr_countdown--;
1688556fdfd6SAlexander Duyck 	}
1689556fdfd6SAlexander Duyck 
169056184e01SJesse Brandeburg 	if (!test_bit(__IAVF_VSI_DOWN, vsi->state))
1691556fdfd6SAlexander Duyck 		wr32(hw, INTREG(q_vector->reg_idx), intval);
1692de32e3efSCarolyn Wyborny }
1693de32e3efSCarolyn Wyborny 
1694de32e3efSCarolyn Wyborny /**
1695129cf89eSJesse Brandeburg  * iavf_napi_poll - NAPI polling Rx/Tx cleanup routine
16967f12ad74SGreg Rose  * @napi: napi struct with our devices info in it
16977f12ad74SGreg Rose  * @budget: amount of work driver is allowed to do this pass, in packets
16987f12ad74SGreg Rose  *
16997f12ad74SGreg Rose  * This function will clean all queues associated with a q_vector.
17007f12ad74SGreg Rose  *
17017f12ad74SGreg Rose  * Returns the amount of work done
17027f12ad74SGreg Rose  **/
1703129cf89eSJesse Brandeburg int iavf_napi_poll(struct napi_struct *napi, int budget)
17047f12ad74SGreg Rose {
170556184e01SJesse Brandeburg 	struct iavf_q_vector *q_vector =
170656184e01SJesse Brandeburg 			       container_of(napi, struct iavf_q_vector, napi);
170756184e01SJesse Brandeburg 	struct iavf_vsi *vsi = q_vector->vsi;
170856184e01SJesse Brandeburg 	struct iavf_ring *ring;
17097f12ad74SGreg Rose 	bool clean_complete = true;
1710c29af37fSAnjali Singhai Jain 	bool arm_wb = false;
17117f12ad74SGreg Rose 	int budget_per_ring;
171232b3e08fSJesse Brandeburg 	int work_done = 0;
17137f12ad74SGreg Rose 
171456184e01SJesse Brandeburg 	if (test_bit(__IAVF_VSI_DOWN, vsi->state)) {
17157f12ad74SGreg Rose 		napi_complete(napi);
17167f12ad74SGreg Rose 		return 0;
17177f12ad74SGreg Rose 	}
17187f12ad74SGreg Rose 
17197f12ad74SGreg Rose 	/* Since the actual Tx work is minimal, we can give the Tx a larger
17207f12ad74SGreg Rose 	 * budget and be more aggressive about cleaning up the Tx descriptors.
17217f12ad74SGreg Rose 	 */
172256184e01SJesse Brandeburg 	iavf_for_each_ring(ring, q_vector->tx) {
172356184e01SJesse Brandeburg 		if (!iavf_clean_tx_irq(vsi, ring, budget)) {
1724f2edaaaaSAlexander Duyck 			clean_complete = false;
1725f2edaaaaSAlexander Duyck 			continue;
1726f2edaaaaSAlexander Duyck 		}
1727f2edaaaaSAlexander Duyck 		arm_wb |= ring->arm_wb;
17280deda868SJesse Brandeburg 		ring->arm_wb = false;
1729c29af37fSAnjali Singhai Jain 	}
17307f12ad74SGreg Rose 
1731c67cacebSAlexander Duyck 	/* Handle case where we are called by netpoll with a budget of 0 */
1732c67cacebSAlexander Duyck 	if (budget <= 0)
1733c67cacebSAlexander Duyck 		goto tx_only;
1734c67cacebSAlexander Duyck 
17357f12ad74SGreg Rose 	/* We attempt to distribute budget to each Rx queue fairly, but don't
17367f12ad74SGreg Rose 	 * allow the budget to go below 1 because that would exit polling early.
17377f12ad74SGreg Rose 	 */
17387f12ad74SGreg Rose 	budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
17397f12ad74SGreg Rose 
174056184e01SJesse Brandeburg 	iavf_for_each_ring(ring, q_vector->rx) {
174156184e01SJesse Brandeburg 		int cleaned = iavf_clean_rx_irq(ring, budget_per_ring);
174232b3e08fSJesse Brandeburg 
174332b3e08fSJesse Brandeburg 		work_done += cleaned;
1744f2edaaaaSAlexander Duyck 		/* if we clean as many as budgeted, we must not be done */
1745f2edaaaaSAlexander Duyck 		if (cleaned >= budget_per_ring)
1746f2edaaaaSAlexander Duyck 			clean_complete = false;
1747a132af24SMitch Williams 	}
17487f12ad74SGreg Rose 
17497f12ad74SGreg Rose 	/* If work not completed, return budget and polling will return */
1750c29af37fSAnjali Singhai Jain 	if (!clean_complete) {
175196db776aSAlan Brady 		int cpu_id = smp_processor_id();
175296db776aSAlan Brady 
175396db776aSAlan Brady 		/* It is possible that the interrupt affinity has changed but,
175496db776aSAlan Brady 		 * if the cpu is pegged at 100%, polling will never exit while
175596db776aSAlan Brady 		 * traffic continues and the interrupt will be stuck on this
175696db776aSAlan Brady 		 * cpu.  We check to make sure affinity is correct before we
175796db776aSAlan Brady 		 * continue to poll, otherwise we must stop polling so the
175896db776aSAlan Brady 		 * interrupt can move to the correct cpu.
175996db776aSAlan Brady 		 */
17606d977729SJacob Keller 		if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
17616d977729SJacob Keller 			/* Tell napi that we are done polling */
17626d977729SJacob Keller 			napi_complete_done(napi, work_done);
17636d977729SJacob Keller 
17646d977729SJacob Keller 			/* Force an interrupt */
1765129cf89eSJesse Brandeburg 			iavf_force_wb(vsi, q_vector);
17666d977729SJacob Keller 
17676d977729SJacob Keller 			/* Return budget-1 so that polling stops */
17686d977729SJacob Keller 			return budget - 1;
17696d977729SJacob Keller 		}
1770c67cacebSAlexander Duyck tx_only:
1771164c9f54SAnjali Singhai Jain 		if (arm_wb) {
1772164c9f54SAnjali Singhai Jain 			q_vector->tx.ring[0].tx_stats.tx_force_wb++;
177356184e01SJesse Brandeburg 			iavf_enable_wb_on_itr(vsi, q_vector);
1774164c9f54SAnjali Singhai Jain 		}
17757f12ad74SGreg Rose 		return budget;
1776c29af37fSAnjali Singhai Jain 	}
17777f12ad74SGreg Rose 
177856184e01SJesse Brandeburg 	if (vsi->back->flags & IAVF_TXR_FLAGS_WB_ON_ITR)
17798e0764b4SAnjali Singhai Jain 		q_vector->arm_wb_state = false;
17808e0764b4SAnjali Singhai Jain 
17810bcd952fSJesse Brandeburg 	/* Exit the polling mode, but don't re-enable interrupts if stack might
17820bcd952fSJesse Brandeburg 	 * poll us due to busy-polling
17830bcd952fSJesse Brandeburg 	 */
17840bcd952fSJesse Brandeburg 	if (likely(napi_complete_done(napi, work_done)))
178556184e01SJesse Brandeburg 		iavf_update_enable_itr(vsi, q_vector);
178696db776aSAlan Brady 
17876beb84a7SAlexander Duyck 	return min(work_done, budget - 1);
17887f12ad74SGreg Rose }
17897f12ad74SGreg Rose 
17907f12ad74SGreg Rose /**
1791129cf89eSJesse Brandeburg  * iavf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
17927f12ad74SGreg Rose  * @skb:     send buffer
17937f12ad74SGreg Rose  * @tx_ring: ring to send buffer on
17947f12ad74SGreg Rose  * @flags:   the tx flags to be set
17957f12ad74SGreg Rose  *
17967f12ad74SGreg Rose  * Checks the skb and set up correspondingly several generic transmit flags
17977f12ad74SGreg Rose  * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
17987f12ad74SGreg Rose  *
17997f12ad74SGreg Rose  * Returns error code indicate the frame should be dropped upon error and the
18007f12ad74SGreg Rose  * otherwise  returns 0 to indicate the flags has been set properly.
18017f12ad74SGreg Rose  **/
1802129cf89eSJesse Brandeburg static inline int iavf_tx_prepare_vlan_flags(struct sk_buff *skb,
180356184e01SJesse Brandeburg 					     struct iavf_ring *tx_ring,
18047f12ad74SGreg Rose 					     u32 *flags)
18057f12ad74SGreg Rose {
18067f12ad74SGreg Rose 	__be16 protocol = skb->protocol;
18077f12ad74SGreg Rose 	u32  tx_flags = 0;
18087f12ad74SGreg Rose 
180931eaaccfSGreg Rose 	if (protocol == htons(ETH_P_8021Q) &&
181031eaaccfSGreg Rose 	    !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
181131eaaccfSGreg Rose 		/* When HW VLAN acceleration is turned off by the user the
181231eaaccfSGreg Rose 		 * stack sets the protocol to 8021q so that the driver
181331eaaccfSGreg Rose 		 * can take any steps required to support the SW only
181431eaaccfSGreg Rose 		 * VLAN handling.  In our case the driver doesn't need
181531eaaccfSGreg Rose 		 * to take any further steps so just set the protocol
181631eaaccfSGreg Rose 		 * to the encapsulated ethertype.
181731eaaccfSGreg Rose 		 */
181831eaaccfSGreg Rose 		skb->protocol = vlan_get_protocol(skb);
181931eaaccfSGreg Rose 		goto out;
182031eaaccfSGreg Rose 	}
182131eaaccfSGreg Rose 
18227f12ad74SGreg Rose 	/* if we have a HW VLAN tag being added, default to the HW one */
1823df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
182456184e01SJesse Brandeburg 		tx_flags |= skb_vlan_tag_get(skb) << IAVF_TX_FLAGS_VLAN_SHIFT;
182556184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_HW_VLAN;
18267f12ad74SGreg Rose 	/* else if it is a SW VLAN, check the next protocol and store the tag */
18277f12ad74SGreg Rose 	} else if (protocol == htons(ETH_P_8021Q)) {
18287f12ad74SGreg Rose 		struct vlan_hdr *vhdr, _vhdr;
18296995b36cSJesse Brandeburg 
18307f12ad74SGreg Rose 		vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
18317f12ad74SGreg Rose 		if (!vhdr)
18327f12ad74SGreg Rose 			return -EINVAL;
18337f12ad74SGreg Rose 
18347f12ad74SGreg Rose 		protocol = vhdr->h_vlan_encapsulated_proto;
183556184e01SJesse Brandeburg 		tx_flags |= ntohs(vhdr->h_vlan_TCI) << IAVF_TX_FLAGS_VLAN_SHIFT;
183656184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_SW_VLAN;
18377f12ad74SGreg Rose 	}
18387f12ad74SGreg Rose 
183931eaaccfSGreg Rose out:
18407f12ad74SGreg Rose 	*flags = tx_flags;
18417f12ad74SGreg Rose 	return 0;
18427f12ad74SGreg Rose }
18437f12ad74SGreg Rose 
18447f12ad74SGreg Rose /**
184556184e01SJesse Brandeburg  * iavf_tso - set up the tso context descriptor
184652ea3e80SAlexander Duyck  * @first:    pointer to first Tx buffer for xmit
18477f12ad74SGreg Rose  * @hdr_len:  ptr to the size of the packet header
18489c883bd3SShannon Nelson  * @cd_type_cmd_tso_mss: Quad Word 1
18497f12ad74SGreg Rose  *
18507f12ad74SGreg Rose  * Returns 0 if no TSO can happen, 1 if tso is going, or error
18517f12ad74SGreg Rose  **/
185256184e01SJesse Brandeburg static int iavf_tso(struct iavf_tx_buffer *first, u8 *hdr_len,
185352ea3e80SAlexander Duyck 		    u64 *cd_type_cmd_tso_mss)
18547f12ad74SGreg Rose {
185552ea3e80SAlexander Duyck 	struct sk_buff *skb = first->skb;
185603f9d6a5SAlexander Duyck 	u64 cd_cmd, cd_tso_len, cd_mss;
1857c777019aSAlexander Duyck 	union {
1858c777019aSAlexander Duyck 		struct iphdr *v4;
1859c777019aSAlexander Duyck 		struct ipv6hdr *v6;
1860c777019aSAlexander Duyck 		unsigned char *hdr;
1861c777019aSAlexander Duyck 	} ip;
1862c49a7bc3SAlexander Duyck 	union {
1863c49a7bc3SAlexander Duyck 		struct tcphdr *tcp;
18645453205cSAlexander Duyck 		struct udphdr *udp;
1865c49a7bc3SAlexander Duyck 		unsigned char *hdr;
1866c49a7bc3SAlexander Duyck 	} l4;
1867c49a7bc3SAlexander Duyck 	u32 paylen, l4_offset;
186852ea3e80SAlexander Duyck 	u16 gso_segs, gso_size;
18697f12ad74SGreg Rose 	int err;
18707f12ad74SGreg Rose 
1871e9f6563dSShannon Nelson 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1872e9f6563dSShannon Nelson 		return 0;
1873e9f6563dSShannon Nelson 
18747f12ad74SGreg Rose 	if (!skb_is_gso(skb))
18757f12ad74SGreg Rose 		return 0;
18767f12ad74SGreg Rose 
1877fe6d4aa4SFrancois Romieu 	err = skb_cow_head(skb, 0);
1878fe6d4aa4SFrancois Romieu 	if (err < 0)
18797f12ad74SGreg Rose 		return err;
18807f12ad74SGreg Rose 
1881c777019aSAlexander Duyck 	ip.hdr = skb_network_header(skb);
1882c777019aSAlexander Duyck 	l4.hdr = skb_transport_header(skb);
188385e76d03SAnjali Singhai 
1884c777019aSAlexander Duyck 	/* initialize outer IP header fields */
1885c777019aSAlexander Duyck 	if (ip.v4->version == 4) {
1886c777019aSAlexander Duyck 		ip.v4->tot_len = 0;
1887c777019aSAlexander Duyck 		ip.v4->check = 0;
1888c49a7bc3SAlexander Duyck 	} else {
1889c777019aSAlexander Duyck 		ip.v6->payload_len = 0;
1890c777019aSAlexander Duyck 	}
1891c777019aSAlexander Duyck 
1892577389a5SAlexander Duyck 	if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
18931c7b4a23SAlexander Duyck 					 SKB_GSO_GRE_CSUM |
18947e13318dSTom Herbert 					 SKB_GSO_IPXIP4 |
1895bf2d1df3SAlexander Duyck 					 SKB_GSO_IPXIP6 |
1896577389a5SAlexander Duyck 					 SKB_GSO_UDP_TUNNEL |
18975453205cSAlexander Duyck 					 SKB_GSO_UDP_TUNNEL_CSUM)) {
18981c7b4a23SAlexander Duyck 		if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
18991c7b4a23SAlexander Duyck 		    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
19001c7b4a23SAlexander Duyck 			l4.udp->len = 0;
19011c7b4a23SAlexander Duyck 
19025453205cSAlexander Duyck 			/* determine offset of outer transport header */
19035453205cSAlexander Duyck 			l4_offset = l4.hdr - skb->data;
19045453205cSAlexander Duyck 
19055453205cSAlexander Duyck 			/* remove payload length from outer checksum */
190624d41e5eSAlexander Duyck 			paylen = skb->len - l4_offset;
1907b9c015d4SJacob Keller 			csum_replace_by_diff(&l4.udp->check,
1908b9c015d4SJacob Keller 					     (__force __wsum)htonl(paylen));
19095453205cSAlexander Duyck 		}
19105453205cSAlexander Duyck 
1911c777019aSAlexander Duyck 		/* reset pointers to inner headers */
1912c777019aSAlexander Duyck 		ip.hdr = skb_inner_network_header(skb);
1913c777019aSAlexander Duyck 		l4.hdr = skb_inner_transport_header(skb);
1914c777019aSAlexander Duyck 
1915c777019aSAlexander Duyck 		/* initialize inner IP header fields */
1916c777019aSAlexander Duyck 		if (ip.v4->version == 4) {
1917c777019aSAlexander Duyck 			ip.v4->tot_len = 0;
1918c777019aSAlexander Duyck 			ip.v4->check = 0;
1919c777019aSAlexander Duyck 		} else {
1920c777019aSAlexander Duyck 			ip.v6->payload_len = 0;
1921c777019aSAlexander Duyck 		}
19227f12ad74SGreg Rose 	}
19237f12ad74SGreg Rose 
1924c49a7bc3SAlexander Duyck 	/* determine offset of inner transport header */
1925c49a7bc3SAlexander Duyck 	l4_offset = l4.hdr - skb->data;
1926c49a7bc3SAlexander Duyck 
1927c49a7bc3SAlexander Duyck 	/* remove payload length from inner checksum */
192824d41e5eSAlexander Duyck 	paylen = skb->len - l4_offset;
1929b9c015d4SJacob Keller 	csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
1930c49a7bc3SAlexander Duyck 
1931c49a7bc3SAlexander Duyck 	/* compute length of segmentation header */
1932c49a7bc3SAlexander Duyck 	*hdr_len = (l4.tcp->doff * 4) + l4_offset;
19337f12ad74SGreg Rose 
193452ea3e80SAlexander Duyck 	/* pull values out of skb_shinfo */
193552ea3e80SAlexander Duyck 	gso_size = skb_shinfo(skb)->gso_size;
193652ea3e80SAlexander Duyck 	gso_segs = skb_shinfo(skb)->gso_segs;
193752ea3e80SAlexander Duyck 
193852ea3e80SAlexander Duyck 	/* update GSO size and bytecount with header size */
193952ea3e80SAlexander Duyck 	first->gso_segs = gso_segs;
194052ea3e80SAlexander Duyck 	first->bytecount += (first->gso_segs - 1) * *hdr_len;
194152ea3e80SAlexander Duyck 
19427f12ad74SGreg Rose 	/* find the field values */
194356184e01SJesse Brandeburg 	cd_cmd = IAVF_TX_CTX_DESC_TSO;
19447f12ad74SGreg Rose 	cd_tso_len = skb->len - *hdr_len;
194552ea3e80SAlexander Duyck 	cd_mss = gso_size;
194656184e01SJesse Brandeburg 	*cd_type_cmd_tso_mss |= (cd_cmd << IAVF_TXD_CTX_QW1_CMD_SHIFT) |
194756184e01SJesse Brandeburg 				(cd_tso_len << IAVF_TXD_CTX_QW1_TSO_LEN_SHIFT) |
194856184e01SJesse Brandeburg 				(cd_mss << IAVF_TXD_CTX_QW1_MSS_SHIFT);
19497f12ad74SGreg Rose 	return 1;
19507f12ad74SGreg Rose }
19517f12ad74SGreg Rose 
19527f12ad74SGreg Rose /**
195356184e01SJesse Brandeburg  * iavf_tx_enable_csum - Enable Tx checksum offloads
19547f12ad74SGreg Rose  * @skb: send buffer
195589232c3bSAnjali Singhai Jain  * @tx_flags: pointer to Tx flags currently set
19567f12ad74SGreg Rose  * @td_cmd: Tx descriptor command bits to set
19577f12ad74SGreg Rose  * @td_offset: Tx descriptor header offsets to set
1958529f1f65SAlexander Duyck  * @tx_ring: Tx descriptor ring
19597f12ad74SGreg Rose  * @cd_tunneling: ptr to context desc bits
19607f12ad74SGreg Rose  **/
196156184e01SJesse Brandeburg static int iavf_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
19627f12ad74SGreg Rose 			       u32 *td_cmd, u32 *td_offset,
196356184e01SJesse Brandeburg 			       struct iavf_ring *tx_ring,
19647f12ad74SGreg Rose 			       u32 *cd_tunneling)
19657f12ad74SGreg Rose {
1966b96b78f2SAlexander Duyck 	union {
1967b96b78f2SAlexander Duyck 		struct iphdr *v4;
1968b96b78f2SAlexander Duyck 		struct ipv6hdr *v6;
1969b96b78f2SAlexander Duyck 		unsigned char *hdr;
1970b96b78f2SAlexander Duyck 	} ip;
1971b96b78f2SAlexander Duyck 	union {
1972b96b78f2SAlexander Duyck 		struct tcphdr *tcp;
1973b96b78f2SAlexander Duyck 		struct udphdr *udp;
1974b96b78f2SAlexander Duyck 		unsigned char *hdr;
1975b96b78f2SAlexander Duyck 	} l4;
1976a3fd9d88SAlexander Duyck 	unsigned char *exthdr;
1977d1bd743bSJesse Brandeburg 	u32 offset, cmd = 0;
1978a3fd9d88SAlexander Duyck 	__be16 frag_off;
1979b96b78f2SAlexander Duyck 	u8 l4_proto = 0;
1980b96b78f2SAlexander Duyck 
1981529f1f65SAlexander Duyck 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1982529f1f65SAlexander Duyck 		return 0;
1983529f1f65SAlexander Duyck 
1984b96b78f2SAlexander Duyck 	ip.hdr = skb_network_header(skb);
1985b96b78f2SAlexander Duyck 	l4.hdr = skb_transport_header(skb);
19867f12ad74SGreg Rose 
1987475b4205SAlexander Duyck 	/* compute outer L2 header size */
1988f1cad2ceSJesse Brandeburg 	offset = ((ip.hdr - skb->data) / 2) << IAVF_TX_DESC_LENGTH_MACLEN_SHIFT;
1989475b4205SAlexander Duyck 
19907f12ad74SGreg Rose 	if (skb->encapsulation) {
1991d1bd743bSJesse Brandeburg 		u32 tunnel = 0;
1992a0064728SAlexander Duyck 		/* define outer network header type */
199356184e01SJesse Brandeburg 		if (*tx_flags & IAVF_TX_FLAGS_IPV4) {
199456184e01SJesse Brandeburg 			tunnel |= (*tx_flags & IAVF_TX_FLAGS_TSO) ?
199556184e01SJesse Brandeburg 				  IAVF_TX_CTX_EXT_IP_IPV4 :
199656184e01SJesse Brandeburg 				  IAVF_TX_CTX_EXT_IP_IPV4_NO_CSUM;
1997475b4205SAlexander Duyck 
1998a0064728SAlexander Duyck 			l4_proto = ip.v4->protocol;
199956184e01SJesse Brandeburg 		} else if (*tx_flags & IAVF_TX_FLAGS_IPV6) {
200056184e01SJesse Brandeburg 			tunnel |= IAVF_TX_CTX_EXT_IP_IPV6;
2001a3fd9d88SAlexander Duyck 
2002a3fd9d88SAlexander Duyck 			exthdr = ip.hdr + sizeof(*ip.v6);
2003a0064728SAlexander Duyck 			l4_proto = ip.v6->nexthdr;
2004a3fd9d88SAlexander Duyck 			if (l4.hdr != exthdr)
2005a3fd9d88SAlexander Duyck 				ipv6_skip_exthdr(skb, exthdr - skb->data,
2006a3fd9d88SAlexander Duyck 						 &l4_proto, &frag_off);
2007a0064728SAlexander Duyck 		}
2008a0064728SAlexander Duyck 
2009a0064728SAlexander Duyck 		/* define outer transport */
2010a0064728SAlexander Duyck 		switch (l4_proto) {
201145991204SAnjali Singhai Jain 		case IPPROTO_UDP:
201256184e01SJesse Brandeburg 			tunnel |= IAVF_TXD_CTX_UDP_TUNNELING;
201356184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
201445991204SAnjali Singhai Jain 			break;
2015a0064728SAlexander Duyck 		case IPPROTO_GRE:
201656184e01SJesse Brandeburg 			tunnel |= IAVF_TXD_CTX_GRE_TUNNELING;
201756184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
2018a0064728SAlexander Duyck 			break;
2019577389a5SAlexander Duyck 		case IPPROTO_IPIP:
2020577389a5SAlexander Duyck 		case IPPROTO_IPV6:
202156184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
2022577389a5SAlexander Duyck 			l4.hdr = skb_inner_network_header(skb);
2023577389a5SAlexander Duyck 			break;
202445991204SAnjali Singhai Jain 		default:
202556184e01SJesse Brandeburg 			if (*tx_flags & IAVF_TX_FLAGS_TSO)
2026529f1f65SAlexander Duyck 				return -1;
2027529f1f65SAlexander Duyck 
2028529f1f65SAlexander Duyck 			skb_checksum_help(skb);
2029529f1f65SAlexander Duyck 			return 0;
203045991204SAnjali Singhai Jain 		}
2031b96b78f2SAlexander Duyck 
2032577389a5SAlexander Duyck 		/* compute outer L3 header size */
2033577389a5SAlexander Duyck 		tunnel |= ((l4.hdr - ip.hdr) / 4) <<
203456184e01SJesse Brandeburg 			  IAVF_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
2035577389a5SAlexander Duyck 
2036577389a5SAlexander Duyck 		/* switch IP header pointer from outer to inner header */
2037577389a5SAlexander Duyck 		ip.hdr = skb_inner_network_header(skb);
2038577389a5SAlexander Duyck 
2039475b4205SAlexander Duyck 		/* compute tunnel header size */
2040475b4205SAlexander Duyck 		tunnel |= ((ip.hdr - l4.hdr) / 2) <<
204156184e01SJesse Brandeburg 			  IAVF_TXD_CTX_QW0_NATLEN_SHIFT;
2042475b4205SAlexander Duyck 
20435453205cSAlexander Duyck 		/* indicate if we need to offload outer UDP header */
204456184e01SJesse Brandeburg 		if ((*tx_flags & IAVF_TX_FLAGS_TSO) &&
20451c7b4a23SAlexander Duyck 		    !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
20465453205cSAlexander Duyck 		    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
204756184e01SJesse Brandeburg 			tunnel |= IAVF_TXD_CTX_QW0_L4T_CS_MASK;
20485453205cSAlexander Duyck 
2049475b4205SAlexander Duyck 		/* record tunnel offload values */
2050475b4205SAlexander Duyck 		*cd_tunneling |= tunnel;
2051475b4205SAlexander Duyck 
2052b96b78f2SAlexander Duyck 		/* switch L4 header pointer from outer to inner */
2053b96b78f2SAlexander Duyck 		l4.hdr = skb_inner_transport_header(skb);
2054a0064728SAlexander Duyck 		l4_proto = 0;
20557f12ad74SGreg Rose 
2056a0064728SAlexander Duyck 		/* reset type as we transition from outer to inner headers */
205756184e01SJesse Brandeburg 		*tx_flags &= ~(IAVF_TX_FLAGS_IPV4 | IAVF_TX_FLAGS_IPV6);
2058a0064728SAlexander Duyck 		if (ip.v4->version == 4)
205956184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_IPV4;
2060a0064728SAlexander Duyck 		if (ip.v6->version == 6)
206156184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_IPV6;
206285e76d03SAnjali Singhai 	}
20637f12ad74SGreg Rose 
20647f12ad74SGreg Rose 	/* Enable IP checksum offloads */
206556184e01SJesse Brandeburg 	if (*tx_flags & IAVF_TX_FLAGS_IPV4) {
2066b96b78f2SAlexander Duyck 		l4_proto = ip.v4->protocol;
20677f12ad74SGreg Rose 		/* the stack computes the IP header already, the only time we
20687f12ad74SGreg Rose 		 * need the hardware to recompute it is in the case of TSO.
20697f12ad74SGreg Rose 		 */
207056184e01SJesse Brandeburg 		cmd |= (*tx_flags & IAVF_TX_FLAGS_TSO) ?
2071f1cad2ceSJesse Brandeburg 		       IAVF_TX_DESC_CMD_IIPT_IPV4_CSUM :
2072f1cad2ceSJesse Brandeburg 		       IAVF_TX_DESC_CMD_IIPT_IPV4;
207356184e01SJesse Brandeburg 	} else if (*tx_flags & IAVF_TX_FLAGS_IPV6) {
2074f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_IIPT_IPV6;
2075a3fd9d88SAlexander Duyck 
2076a3fd9d88SAlexander Duyck 		exthdr = ip.hdr + sizeof(*ip.v6);
2077a3fd9d88SAlexander Duyck 		l4_proto = ip.v6->nexthdr;
2078a3fd9d88SAlexander Duyck 		if (l4.hdr != exthdr)
2079a3fd9d88SAlexander Duyck 			ipv6_skip_exthdr(skb, exthdr - skb->data,
2080a3fd9d88SAlexander Duyck 					 &l4_proto, &frag_off);
20817f12ad74SGreg Rose 	}
2082b96b78f2SAlexander Duyck 
2083475b4205SAlexander Duyck 	/* compute inner L3 header size */
2084f1cad2ceSJesse Brandeburg 	offset |= ((l4.hdr - ip.hdr) / 4) << IAVF_TX_DESC_LENGTH_IPLEN_SHIFT;
20857f12ad74SGreg Rose 
20867f12ad74SGreg Rose 	/* Enable L4 checksum offloads */
2087b96b78f2SAlexander Duyck 	switch (l4_proto) {
20887f12ad74SGreg Rose 	case IPPROTO_TCP:
20897f12ad74SGreg Rose 		/* enable checksum offloads */
2090f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_TCP;
2091f1cad2ceSJesse Brandeburg 		offset |= l4.tcp->doff << IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
20927f12ad74SGreg Rose 		break;
20937f12ad74SGreg Rose 	case IPPROTO_SCTP:
20947f12ad74SGreg Rose 		/* enable SCTP checksum offload */
2095f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_SCTP;
2096475b4205SAlexander Duyck 		offset |= (sizeof(struct sctphdr) >> 2) <<
2097f1cad2ceSJesse Brandeburg 			  IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
20987f12ad74SGreg Rose 		break;
20997f12ad74SGreg Rose 	case IPPROTO_UDP:
21007f12ad74SGreg Rose 		/* enable UDP checksum offload */
2101f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_UDP;
2102475b4205SAlexander Duyck 		offset |= (sizeof(struct udphdr) >> 2) <<
2103f1cad2ceSJesse Brandeburg 			  IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
21047f12ad74SGreg Rose 		break;
21057f12ad74SGreg Rose 	default:
210656184e01SJesse Brandeburg 		if (*tx_flags & IAVF_TX_FLAGS_TSO)
2107529f1f65SAlexander Duyck 			return -1;
2108529f1f65SAlexander Duyck 		skb_checksum_help(skb);
2109529f1f65SAlexander Duyck 		return 0;
21107f12ad74SGreg Rose 	}
2111475b4205SAlexander Duyck 
2112475b4205SAlexander Duyck 	*td_cmd |= cmd;
2113475b4205SAlexander Duyck 	*td_offset |= offset;
2114529f1f65SAlexander Duyck 
2115529f1f65SAlexander Duyck 	return 1;
21167f12ad74SGreg Rose }
21177f12ad74SGreg Rose 
21187f12ad74SGreg Rose /**
211956184e01SJesse Brandeburg  * iavf_create_tx_ctx Build the Tx context descriptor
21207f12ad74SGreg Rose  * @tx_ring:  ring to create the descriptor on
21217f12ad74SGreg Rose  * @cd_type_cmd_tso_mss: Quad Word 1
21227f12ad74SGreg Rose  * @cd_tunneling: Quad Word 0 - bits 0-31
21237f12ad74SGreg Rose  * @cd_l2tag2: Quad Word 0 - bits 32-63
21247f12ad74SGreg Rose  **/
212556184e01SJesse Brandeburg static void iavf_create_tx_ctx(struct iavf_ring *tx_ring,
21267f12ad74SGreg Rose 			       const u64 cd_type_cmd_tso_mss,
21277f12ad74SGreg Rose 			       const u32 cd_tunneling, const u32 cd_l2tag2)
21287f12ad74SGreg Rose {
212956184e01SJesse Brandeburg 	struct iavf_tx_context_desc *context_desc;
21307f12ad74SGreg Rose 	int i = tx_ring->next_to_use;
21317f12ad74SGreg Rose 
2132f1cad2ceSJesse Brandeburg 	if ((cd_type_cmd_tso_mss == IAVF_TX_DESC_DTYPE_CONTEXT) &&
2133ff40dd5dSJesse Brandeburg 	    !cd_tunneling && !cd_l2tag2)
21347f12ad74SGreg Rose 		return;
21357f12ad74SGreg Rose 
21367f12ad74SGreg Rose 	/* grab the next descriptor */
2137f1cad2ceSJesse Brandeburg 	context_desc = IAVF_TX_CTXTDESC(tx_ring, i);
21387f12ad74SGreg Rose 
21397f12ad74SGreg Rose 	i++;
21407f12ad74SGreg Rose 	tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
21417f12ad74SGreg Rose 
21427f12ad74SGreg Rose 	/* cpu_to_le32 and assign to struct fields */
21437f12ad74SGreg Rose 	context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
21447f12ad74SGreg Rose 	context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
21453efbbb20SJesse Brandeburg 	context_desc->rsvd = cpu_to_le16(0);
21467f12ad74SGreg Rose 	context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
21477f12ad74SGreg Rose }
21487f12ad74SGreg Rose 
21497f12ad74SGreg Rose /**
2150129cf89eSJesse Brandeburg  * __iavf_chk_linearize - Check if there are more than 8 buffers per packet
215171da6197SAnjali Singhai  * @skb:      send buffer
215271da6197SAnjali Singhai  *
21533f3f7cb8SAlexander Duyck  * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
21543f3f7cb8SAlexander Duyck  * and so we need to figure out the cases where we need to linearize the skb.
21553f3f7cb8SAlexander Duyck  *
21563f3f7cb8SAlexander Duyck  * For TSO we need to count the TSO header and segment payload separately.
21573f3f7cb8SAlexander Duyck  * As such we need to check cases where we have 7 fragments or more as we
21583f3f7cb8SAlexander Duyck  * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
21593f3f7cb8SAlexander Duyck  * the segment payload in the first descriptor, and another 7 for the
21603f3f7cb8SAlexander Duyck  * fragments.
216171da6197SAnjali Singhai  **/
2162129cf89eSJesse Brandeburg bool __iavf_chk_linearize(struct sk_buff *skb)
216371da6197SAnjali Singhai {
2164d7840976SMatthew Wilcox (Oracle) 	const skb_frag_t *frag, *stale;
21653f3f7cb8SAlexander Duyck 	int nr_frags, sum;
216671da6197SAnjali Singhai 
21673f3f7cb8SAlexander Duyck 	/* no need to check if number of frags is less than 7 */
21682d37490bSAlexander Duyck 	nr_frags = skb_shinfo(skb)->nr_frags;
216956184e01SJesse Brandeburg 	if (nr_frags < (IAVF_MAX_BUFFER_TXD - 1))
21702d37490bSAlexander Duyck 		return false;
217171da6197SAnjali Singhai 
21722d37490bSAlexander Duyck 	/* We need to walk through the list and validate that each group
2173841493a3SAlexander Duyck 	 * of 6 fragments totals at least gso_size.
21742d37490bSAlexander Duyck 	 */
217556184e01SJesse Brandeburg 	nr_frags -= IAVF_MAX_BUFFER_TXD - 2;
217671da6197SAnjali Singhai 	frag = &skb_shinfo(skb)->frags[0];
21772d37490bSAlexander Duyck 
21782d37490bSAlexander Duyck 	/* Initialize size to the negative value of gso_size minus 1.  We
21792d37490bSAlexander Duyck 	 * use this as the worst case scenerio in which the frag ahead
21802d37490bSAlexander Duyck 	 * of us only provides one byte which is why we are limited to 6
21812d37490bSAlexander Duyck 	 * descriptors for a single transmit as the header and previous
21822d37490bSAlexander Duyck 	 * fragment are already consuming 2 descriptors.
21832d37490bSAlexander Duyck 	 */
21843f3f7cb8SAlexander Duyck 	sum = 1 - skb_shinfo(skb)->gso_size;
21852d37490bSAlexander Duyck 
21863f3f7cb8SAlexander Duyck 	/* Add size of frags 0 through 4 to create our initial sum */
21873f3f7cb8SAlexander Duyck 	sum += skb_frag_size(frag++);
21883f3f7cb8SAlexander Duyck 	sum += skb_frag_size(frag++);
21893f3f7cb8SAlexander Duyck 	sum += skb_frag_size(frag++);
21903f3f7cb8SAlexander Duyck 	sum += skb_frag_size(frag++);
21913f3f7cb8SAlexander Duyck 	sum += skb_frag_size(frag++);
21922d37490bSAlexander Duyck 
21932d37490bSAlexander Duyck 	/* Walk through fragments adding latest fragment, testing it, and
21942d37490bSAlexander Duyck 	 * then removing stale fragments from the sum.
21952d37490bSAlexander Duyck 	 */
2196248de22eSAlexander Duyck 	for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
2197248de22eSAlexander Duyck 		int stale_size = skb_frag_size(stale);
2198248de22eSAlexander Duyck 
21993f3f7cb8SAlexander Duyck 		sum += skb_frag_size(frag++);
22002d37490bSAlexander Duyck 
2201248de22eSAlexander Duyck 		/* The stale fragment may present us with a smaller
2202248de22eSAlexander Duyck 		 * descriptor than the actual fragment size. To account
2203248de22eSAlexander Duyck 		 * for that we need to remove all the data on the front and
2204248de22eSAlexander Duyck 		 * figure out what the remainder would be in the last
2205248de22eSAlexander Duyck 		 * descriptor associated with the fragment.
2206248de22eSAlexander Duyck 		 */
220756184e01SJesse Brandeburg 		if (stale_size > IAVF_MAX_DATA_PER_TXD) {
2208*b54c9d5bSJonathan Lemon 			int align_pad = -(skb_frag_off(stale)) &
220956184e01SJesse Brandeburg 					(IAVF_MAX_READ_REQ_SIZE - 1);
2210248de22eSAlexander Duyck 
2211248de22eSAlexander Duyck 			sum -= align_pad;
2212248de22eSAlexander Duyck 			stale_size -= align_pad;
2213248de22eSAlexander Duyck 
2214248de22eSAlexander Duyck 			do {
221556184e01SJesse Brandeburg 				sum -= IAVF_MAX_DATA_PER_TXD_ALIGNED;
221656184e01SJesse Brandeburg 				stale_size -= IAVF_MAX_DATA_PER_TXD_ALIGNED;
221756184e01SJesse Brandeburg 			} while (stale_size > IAVF_MAX_DATA_PER_TXD);
2218248de22eSAlexander Duyck 		}
2219248de22eSAlexander Duyck 
22202d37490bSAlexander Duyck 		/* if sum is negative we failed to make sufficient progress */
22212d37490bSAlexander Duyck 		if (sum < 0)
22222d37490bSAlexander Duyck 			return true;
22232d37490bSAlexander Duyck 
2224841493a3SAlexander Duyck 		if (!nr_frags--)
222571da6197SAnjali Singhai 			break;
22262d37490bSAlexander Duyck 
2227248de22eSAlexander Duyck 		sum -= stale_size;
222871da6197SAnjali Singhai 	}
222971da6197SAnjali Singhai 
22302d37490bSAlexander Duyck 	return false;
223171da6197SAnjali Singhai }
223271da6197SAnjali Singhai 
223371da6197SAnjali Singhai /**
2234129cf89eSJesse Brandeburg  * __iavf_maybe_stop_tx - 2nd level check for tx stop conditions
22358f6a2b05SJesse Brandeburg  * @tx_ring: the ring to be checked
22368f6a2b05SJesse Brandeburg  * @size:    the size buffer we want to assure is available
22378f6a2b05SJesse Brandeburg  *
22388f6a2b05SJesse Brandeburg  * Returns -EBUSY if a stop is needed, else 0
22398f6a2b05SJesse Brandeburg  **/
224056184e01SJesse Brandeburg int __iavf_maybe_stop_tx(struct iavf_ring *tx_ring, int size)
22418f6a2b05SJesse Brandeburg {
22428f6a2b05SJesse Brandeburg 	netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
22438f6a2b05SJesse Brandeburg 	/* Memory barrier before checking head and tail */
22448f6a2b05SJesse Brandeburg 	smp_mb();
22458f6a2b05SJesse Brandeburg 
22468f6a2b05SJesse Brandeburg 	/* Check again in a case another CPU has just made room available. */
224756184e01SJesse Brandeburg 	if (likely(IAVF_DESC_UNUSED(tx_ring) < size))
22488f6a2b05SJesse Brandeburg 		return -EBUSY;
22498f6a2b05SJesse Brandeburg 
22508f6a2b05SJesse Brandeburg 	/* A reprieve! - use start_queue because it doesn't call schedule */
22518f6a2b05SJesse Brandeburg 	netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
22528f6a2b05SJesse Brandeburg 	++tx_ring->tx_stats.restart_queue;
22538f6a2b05SJesse Brandeburg 	return 0;
22548f6a2b05SJesse Brandeburg }
22558f6a2b05SJesse Brandeburg 
22568f6a2b05SJesse Brandeburg /**
2257129cf89eSJesse Brandeburg  * iavf_tx_map - Build the Tx descriptor
22587f12ad74SGreg Rose  * @tx_ring:  ring to send buffer on
22597f12ad74SGreg Rose  * @skb:      send buffer
22607f12ad74SGreg Rose  * @first:    first buffer info buffer to use
22617f12ad74SGreg Rose  * @tx_flags: collected send information
22627f12ad74SGreg Rose  * @hdr_len:  size of the packet header
22637f12ad74SGreg Rose  * @td_cmd:   the command field in the descriptor
22647f12ad74SGreg Rose  * @td_offset: offset for checksum or crc
22657f12ad74SGreg Rose  **/
226656184e01SJesse Brandeburg static inline void iavf_tx_map(struct iavf_ring *tx_ring, struct sk_buff *skb,
226756184e01SJesse Brandeburg 			       struct iavf_tx_buffer *first, u32 tx_flags,
22687f12ad74SGreg Rose 			       const u8 hdr_len, u32 td_cmd, u32 td_offset)
22697f12ad74SGreg Rose {
22707f12ad74SGreg Rose 	unsigned int data_len = skb->data_len;
22717f12ad74SGreg Rose 	unsigned int size = skb_headlen(skb);
2272d7840976SMatthew Wilcox (Oracle) 	skb_frag_t *frag;
227356184e01SJesse Brandeburg 	struct iavf_tx_buffer *tx_bi;
227456184e01SJesse Brandeburg 	struct iavf_tx_desc *tx_desc;
22757f12ad74SGreg Rose 	u16 i = tx_ring->next_to_use;
22767f12ad74SGreg Rose 	u32 td_tag = 0;
22777f12ad74SGreg Rose 	dma_addr_t dma;
22787f12ad74SGreg Rose 
227956184e01SJesse Brandeburg 	if (tx_flags & IAVF_TX_FLAGS_HW_VLAN) {
2280f1cad2ceSJesse Brandeburg 		td_cmd |= IAVF_TX_DESC_CMD_IL2TAG1;
228156184e01SJesse Brandeburg 		td_tag = (tx_flags & IAVF_TX_FLAGS_VLAN_MASK) >>
228256184e01SJesse Brandeburg 			 IAVF_TX_FLAGS_VLAN_SHIFT;
22837f12ad74SGreg Rose 	}
22847f12ad74SGreg Rose 
22857f12ad74SGreg Rose 	first->tx_flags = tx_flags;
22867f12ad74SGreg Rose 
22877f12ad74SGreg Rose 	dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
22887f12ad74SGreg Rose 
2289f1cad2ceSJesse Brandeburg 	tx_desc = IAVF_TX_DESC(tx_ring, i);
22907f12ad74SGreg Rose 	tx_bi = first;
22917f12ad74SGreg Rose 
22927f12ad74SGreg Rose 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
229356184e01SJesse Brandeburg 		unsigned int max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED;
22945c4654daSAlexander Duyck 
22957f12ad74SGreg Rose 		if (dma_mapping_error(tx_ring->dev, dma))
22967f12ad74SGreg Rose 			goto dma_error;
22977f12ad74SGreg Rose 
22987f12ad74SGreg Rose 		/* record length, and DMA address */
22997f12ad74SGreg Rose 		dma_unmap_len_set(tx_bi, len, size);
23007f12ad74SGreg Rose 		dma_unmap_addr_set(tx_bi, dma, dma);
23017f12ad74SGreg Rose 
23025c4654daSAlexander Duyck 		/* align size to end of page */
230356184e01SJesse Brandeburg 		max_data += -dma & (IAVF_MAX_READ_REQ_SIZE - 1);
23047f12ad74SGreg Rose 		tx_desc->buffer_addr = cpu_to_le64(dma);
23057f12ad74SGreg Rose 
230656184e01SJesse Brandeburg 		while (unlikely(size > IAVF_MAX_DATA_PER_TXD)) {
23077f12ad74SGreg Rose 			tx_desc->cmd_type_offset_bsz =
23087f12ad74SGreg Rose 				build_ctob(td_cmd, td_offset,
23095c4654daSAlexander Duyck 					   max_data, td_tag);
23107f12ad74SGreg Rose 
23117f12ad74SGreg Rose 			tx_desc++;
23127f12ad74SGreg Rose 			i++;
23136a7fded7SAnjali Singhai Jain 
23147f12ad74SGreg Rose 			if (i == tx_ring->count) {
2315f1cad2ceSJesse Brandeburg 				tx_desc = IAVF_TX_DESC(tx_ring, 0);
23167f12ad74SGreg Rose 				i = 0;
23177f12ad74SGreg Rose 			}
23187f12ad74SGreg Rose 
23195c4654daSAlexander Duyck 			dma += max_data;
23205c4654daSAlexander Duyck 			size -= max_data;
23217f12ad74SGreg Rose 
232256184e01SJesse Brandeburg 			max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED;
23237f12ad74SGreg Rose 			tx_desc->buffer_addr = cpu_to_le64(dma);
23247f12ad74SGreg Rose 		}
23257f12ad74SGreg Rose 
23267f12ad74SGreg Rose 		if (likely(!data_len))
23277f12ad74SGreg Rose 			break;
23287f12ad74SGreg Rose 
23297f12ad74SGreg Rose 		tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
23307f12ad74SGreg Rose 							  size, td_tag);
23317f12ad74SGreg Rose 
23327f12ad74SGreg Rose 		tx_desc++;
23337f12ad74SGreg Rose 		i++;
23346a7fded7SAnjali Singhai Jain 
23357f12ad74SGreg Rose 		if (i == tx_ring->count) {
2336f1cad2ceSJesse Brandeburg 			tx_desc = IAVF_TX_DESC(tx_ring, 0);
23377f12ad74SGreg Rose 			i = 0;
23387f12ad74SGreg Rose 		}
23397f12ad74SGreg Rose 
23407f12ad74SGreg Rose 		size = skb_frag_size(frag);
23417f12ad74SGreg Rose 		data_len -= size;
23427f12ad74SGreg Rose 
23437f12ad74SGreg Rose 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
23447f12ad74SGreg Rose 				       DMA_TO_DEVICE);
23457f12ad74SGreg Rose 
23467f12ad74SGreg Rose 		tx_bi = &tx_ring->tx_bi[i];
23477f12ad74SGreg Rose 	}
23487f12ad74SGreg Rose 
23491dc8b538SAlexander Duyck 	netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
23507f12ad74SGreg Rose 
23517f12ad74SGreg Rose 	i++;
23527f12ad74SGreg Rose 	if (i == tx_ring->count)
23537f12ad74SGreg Rose 		i = 0;
23547f12ad74SGreg Rose 
23557f12ad74SGreg Rose 	tx_ring->next_to_use = i;
23567f12ad74SGreg Rose 
235756184e01SJesse Brandeburg 	iavf_maybe_stop_tx(tx_ring, DESC_NEEDED);
23586a7fded7SAnjali Singhai Jain 
2359b1cb07dbSPreethi Banala 	/* write last descriptor with RS and EOP bits */
236056184e01SJesse Brandeburg 	td_cmd |= IAVF_TXD_CMD;
23616a7fded7SAnjali Singhai Jain 	tx_desc->cmd_type_offset_bsz =
23621dc8b538SAlexander Duyck 			build_ctob(td_cmd, td_offset, size, td_tag);
23636a7fded7SAnjali Singhai Jain 
2364a9e51058SJacob Keller 	skb_tx_timestamp(skb);
2365a9e51058SJacob Keller 
23661dc8b538SAlexander Duyck 	/* Force memory writes to complete before letting h/w know there
23671dc8b538SAlexander Duyck 	 * are new descriptors to fetch.
23681dc8b538SAlexander Duyck 	 *
23691dc8b538SAlexander Duyck 	 * We also use this memory barrier to make certain all of the
23701dc8b538SAlexander Duyck 	 * status bits have been updated before next_to_watch is written.
23716a7fded7SAnjali Singhai Jain 	 */
23726a7fded7SAnjali Singhai Jain 	wmb();
23731dc8b538SAlexander Duyck 
23741dc8b538SAlexander Duyck 	/* set next_to_watch value indicating a packet is present */
23751dc8b538SAlexander Duyck 	first->next_to_watch = tx_desc;
23761dc8b538SAlexander Duyck 
23771dc8b538SAlexander Duyck 	/* notify HW of packet */
23786b16f9eeSFlorian Westphal 	if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
23796a7fded7SAnjali Singhai Jain 		writel(i, tx_ring->tail);
23806a7fded7SAnjali Singhai Jain 	}
23811dc8b538SAlexander Duyck 
23827f12ad74SGreg Rose 	return;
23837f12ad74SGreg Rose 
23847f12ad74SGreg Rose dma_error:
23857f12ad74SGreg Rose 	dev_info(tx_ring->dev, "TX DMA map failed\n");
23867f12ad74SGreg Rose 
23877f12ad74SGreg Rose 	/* clear dma mappings for failed tx_bi map */
23887f12ad74SGreg Rose 	for (;;) {
23897f12ad74SGreg Rose 		tx_bi = &tx_ring->tx_bi[i];
239056184e01SJesse Brandeburg 		iavf_unmap_and_free_tx_resource(tx_ring, tx_bi);
23917f12ad74SGreg Rose 		if (tx_bi == first)
23927f12ad74SGreg Rose 			break;
23937f12ad74SGreg Rose 		if (i == 0)
23947f12ad74SGreg Rose 			i = tx_ring->count;
23957f12ad74SGreg Rose 		i--;
23967f12ad74SGreg Rose 	}
23977f12ad74SGreg Rose 
23987f12ad74SGreg Rose 	tx_ring->next_to_use = i;
23997f12ad74SGreg Rose }
24007f12ad74SGreg Rose 
24017f12ad74SGreg Rose /**
240256184e01SJesse Brandeburg  * iavf_xmit_frame_ring - Sends buffer on Tx ring
24037f12ad74SGreg Rose  * @skb:     send buffer
24047f12ad74SGreg Rose  * @tx_ring: ring to send buffer on
24057f12ad74SGreg Rose  *
24067f12ad74SGreg Rose  * Returns NETDEV_TX_OK if sent, else an error code
24077f12ad74SGreg Rose  **/
240856184e01SJesse Brandeburg static netdev_tx_t iavf_xmit_frame_ring(struct sk_buff *skb,
240956184e01SJesse Brandeburg 					struct iavf_ring *tx_ring)
24107f12ad74SGreg Rose {
2411f1cad2ceSJesse Brandeburg 	u64 cd_type_cmd_tso_mss = IAVF_TX_DESC_DTYPE_CONTEXT;
24127f12ad74SGreg Rose 	u32 cd_tunneling = 0, cd_l2tag2 = 0;
241356184e01SJesse Brandeburg 	struct iavf_tx_buffer *first;
24147f12ad74SGreg Rose 	u32 td_offset = 0;
24157f12ad74SGreg Rose 	u32 tx_flags = 0;
24167f12ad74SGreg Rose 	__be16 protocol;
24177f12ad74SGreg Rose 	u32 td_cmd = 0;
24187f12ad74SGreg Rose 	u8 hdr_len = 0;
24194ec441dfSAlexander Duyck 	int tso, count;
24206995b36cSJesse Brandeburg 
2421b74118f0SJesse Brandeburg 	/* prefetch the data, we'll need it later */
2422b74118f0SJesse Brandeburg 	prefetch(skb->data);
2423b74118f0SJesse Brandeburg 
2424ad64ed8bSJesse Brandeburg 	iavf_trace(xmit_frame_ring, skb, tx_ring);
2425ed0980c4SScott Peterson 
242656184e01SJesse Brandeburg 	count = iavf_xmit_descriptor_count(skb);
242756184e01SJesse Brandeburg 	if (iavf_chk_linearize(skb, count)) {
242852ea3e80SAlexander Duyck 		if (__skb_linearize(skb)) {
242952ea3e80SAlexander Duyck 			dev_kfree_skb_any(skb);
243052ea3e80SAlexander Duyck 			return NETDEV_TX_OK;
243152ea3e80SAlexander Duyck 		}
243256184e01SJesse Brandeburg 		count = iavf_txd_use_count(skb->len);
24332d37490bSAlexander Duyck 		tx_ring->tx_stats.tx_linearize++;
24342d37490bSAlexander Duyck 	}
24354ec441dfSAlexander Duyck 
243656184e01SJesse Brandeburg 	/* need: 1 descriptor per page * PAGE_SIZE/IAVF_MAX_DATA_PER_TXD,
243756184e01SJesse Brandeburg 	 *       + 1 desc for skb_head_len/IAVF_MAX_DATA_PER_TXD,
24384ec441dfSAlexander Duyck 	 *       + 4 desc gap to avoid the cache line where head is,
24394ec441dfSAlexander Duyck 	 *       + 1 desc for context descriptor,
24404ec441dfSAlexander Duyck 	 * otherwise try next time
24414ec441dfSAlexander Duyck 	 */
244256184e01SJesse Brandeburg 	if (iavf_maybe_stop_tx(tx_ring, count + 4 + 1)) {
24434ec441dfSAlexander Duyck 		tx_ring->tx_stats.tx_busy++;
24447f12ad74SGreg Rose 		return NETDEV_TX_BUSY;
24454ec441dfSAlexander Duyck 	}
24467f12ad74SGreg Rose 
244752ea3e80SAlexander Duyck 	/* record the location of the first descriptor for this packet */
244852ea3e80SAlexander Duyck 	first = &tx_ring->tx_bi[tx_ring->next_to_use];
244952ea3e80SAlexander Duyck 	first->skb = skb;
245052ea3e80SAlexander Duyck 	first->bytecount = skb->len;
245152ea3e80SAlexander Duyck 	first->gso_segs = 1;
245252ea3e80SAlexander Duyck 
24537f12ad74SGreg Rose 	/* prepare the xmit flags */
2454129cf89eSJesse Brandeburg 	if (iavf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
24557f12ad74SGreg Rose 		goto out_drop;
24567f12ad74SGreg Rose 
24577f12ad74SGreg Rose 	/* obtain protocol of skb */
2458a12c4158SVlad Yasevich 	protocol = vlan_get_protocol(skb);
24597f12ad74SGreg Rose 
24607f12ad74SGreg Rose 	/* setup IPv4/IPv6 offloads */
24617f12ad74SGreg Rose 	if (protocol == htons(ETH_P_IP))
246256184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_IPV4;
24637f12ad74SGreg Rose 	else if (protocol == htons(ETH_P_IPV6))
246456184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_IPV6;
24657f12ad74SGreg Rose 
246656184e01SJesse Brandeburg 	tso = iavf_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
24677f12ad74SGreg Rose 
24687f12ad74SGreg Rose 	if (tso < 0)
24697f12ad74SGreg Rose 		goto out_drop;
24707f12ad74SGreg Rose 	else if (tso)
247156184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_TSO;
24727f12ad74SGreg Rose 
24737f12ad74SGreg Rose 	/* Always offload the checksum, since it's in the data descriptor */
247456184e01SJesse Brandeburg 	tso = iavf_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
24757f12ad74SGreg Rose 				  tx_ring, &cd_tunneling);
2476529f1f65SAlexander Duyck 	if (tso < 0)
2477529f1f65SAlexander Duyck 		goto out_drop;
24787f12ad74SGreg Rose 
24793bc67973SAlexander Duyck 	/* always enable CRC insertion offload */
2480f1cad2ceSJesse Brandeburg 	td_cmd |= IAVF_TX_DESC_CMD_ICRC;
24813bc67973SAlexander Duyck 
248256184e01SJesse Brandeburg 	iavf_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
24837f12ad74SGreg Rose 			   cd_tunneling, cd_l2tag2);
24847f12ad74SGreg Rose 
2485129cf89eSJesse Brandeburg 	iavf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
24867f12ad74SGreg Rose 		    td_cmd, td_offset);
24877f12ad74SGreg Rose 
24887f12ad74SGreg Rose 	return NETDEV_TX_OK;
24897f12ad74SGreg Rose 
24907f12ad74SGreg Rose out_drop:
2491ad64ed8bSJesse Brandeburg 	iavf_trace(xmit_frame_ring_drop, first->skb, tx_ring);
249252ea3e80SAlexander Duyck 	dev_kfree_skb_any(first->skb);
249352ea3e80SAlexander Duyck 	first->skb = NULL;
24947f12ad74SGreg Rose 	return NETDEV_TX_OK;
24957f12ad74SGreg Rose }
24967f12ad74SGreg Rose 
24977f12ad74SGreg Rose /**
2498129cf89eSJesse Brandeburg  * iavf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
24997f12ad74SGreg Rose  * @skb:    send buffer
25007f12ad74SGreg Rose  * @netdev: network interface device structure
25017f12ad74SGreg Rose  *
25027f12ad74SGreg Rose  * Returns NETDEV_TX_OK if sent, else an error code
25037f12ad74SGreg Rose  **/
2504129cf89eSJesse Brandeburg netdev_tx_t iavf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
25057f12ad74SGreg Rose {
2506129cf89eSJesse Brandeburg 	struct iavf_adapter *adapter = netdev_priv(netdev);
250756184e01SJesse Brandeburg 	struct iavf_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
25087f12ad74SGreg Rose 
25097f12ad74SGreg Rose 	/* hardware can't handle really short frames, hardware padding works
25107f12ad74SGreg Rose 	 * beyond this point
25117f12ad74SGreg Rose 	 */
251256184e01SJesse Brandeburg 	if (unlikely(skb->len < IAVF_MIN_TX_LEN)) {
251356184e01SJesse Brandeburg 		if (skb_pad(skb, IAVF_MIN_TX_LEN - skb->len))
25147f12ad74SGreg Rose 			return NETDEV_TX_OK;
251556184e01SJesse Brandeburg 		skb->len = IAVF_MIN_TX_LEN;
251656184e01SJesse Brandeburg 		skb_set_tail_pointer(skb, IAVF_MIN_TX_LEN);
25177f12ad74SGreg Rose 	}
25187f12ad74SGreg Rose 
251956184e01SJesse Brandeburg 	return iavf_xmit_frame_ring(skb, tx_ring);
25207f12ad74SGreg Rose }
2521