15ec8b7d1SJesse Brandeburg // SPDX-License-Identifier: GPL-2.0
25ec8b7d1SJesse Brandeburg /* Copyright(c) 2013 - 2018 Intel Corporation. */
35ec8b7d1SJesse Brandeburg 
45ec8b7d1SJesse Brandeburg #include <linux/prefetch.h>
55ec8b7d1SJesse Brandeburg #include <net/busy_poll.h>
65ec8b7d1SJesse Brandeburg 
75ec8b7d1SJesse Brandeburg #include "iavf.h"
8ad64ed8bSJesse Brandeburg #include "iavf_trace.h"
966bc8e0fSJesse Brandeburg #include "iavf_prototype.h"
105ec8b7d1SJesse Brandeburg 
115ec8b7d1SJesse Brandeburg static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
125ec8b7d1SJesse Brandeburg 				u32 td_tag)
135ec8b7d1SJesse Brandeburg {
14f1cad2ceSJesse Brandeburg 	return cpu_to_le64(IAVF_TX_DESC_DTYPE_DATA |
1556184e01SJesse Brandeburg 			   ((u64)td_cmd  << IAVF_TXD_QW1_CMD_SHIFT) |
1656184e01SJesse Brandeburg 			   ((u64)td_offset << IAVF_TXD_QW1_OFFSET_SHIFT) |
1756184e01SJesse Brandeburg 			   ((u64)size  << IAVF_TXD_QW1_TX_BUF_SZ_SHIFT) |
1856184e01SJesse Brandeburg 			   ((u64)td_tag  << IAVF_TXD_QW1_L2TAG1_SHIFT));
195ec8b7d1SJesse Brandeburg }
205ec8b7d1SJesse Brandeburg 
2156184e01SJesse Brandeburg #define IAVF_TXD_CMD (IAVF_TX_DESC_CMD_EOP | IAVF_TX_DESC_CMD_RS)
225ec8b7d1SJesse Brandeburg 
235ec8b7d1SJesse Brandeburg /**
2456184e01SJesse Brandeburg  * iavf_unmap_and_free_tx_resource - Release a Tx buffer
255ec8b7d1SJesse Brandeburg  * @ring:      the ring that owns the buffer
265ec8b7d1SJesse Brandeburg  * @tx_buffer: the buffer to free
275ec8b7d1SJesse Brandeburg  **/
2856184e01SJesse Brandeburg static void iavf_unmap_and_free_tx_resource(struct iavf_ring *ring,
2956184e01SJesse Brandeburg 					    struct iavf_tx_buffer *tx_buffer)
305ec8b7d1SJesse Brandeburg {
315ec8b7d1SJesse Brandeburg 	if (tx_buffer->skb) {
3256184e01SJesse Brandeburg 		if (tx_buffer->tx_flags & IAVF_TX_FLAGS_FD_SB)
335ec8b7d1SJesse Brandeburg 			kfree(tx_buffer->raw_buf);
345ec8b7d1SJesse Brandeburg 		else
355ec8b7d1SJesse Brandeburg 			dev_kfree_skb_any(tx_buffer->skb);
365ec8b7d1SJesse Brandeburg 		if (dma_unmap_len(tx_buffer, len))
375ec8b7d1SJesse Brandeburg 			dma_unmap_single(ring->dev,
385ec8b7d1SJesse Brandeburg 					 dma_unmap_addr(tx_buffer, dma),
395ec8b7d1SJesse Brandeburg 					 dma_unmap_len(tx_buffer, len),
405ec8b7d1SJesse Brandeburg 					 DMA_TO_DEVICE);
415ec8b7d1SJesse Brandeburg 	} else if (dma_unmap_len(tx_buffer, len)) {
425ec8b7d1SJesse Brandeburg 		dma_unmap_page(ring->dev,
435ec8b7d1SJesse Brandeburg 			       dma_unmap_addr(tx_buffer, dma),
445ec8b7d1SJesse Brandeburg 			       dma_unmap_len(tx_buffer, len),
455ec8b7d1SJesse Brandeburg 			       DMA_TO_DEVICE);
465ec8b7d1SJesse Brandeburg 	}
475ec8b7d1SJesse Brandeburg 
485ec8b7d1SJesse Brandeburg 	tx_buffer->next_to_watch = NULL;
495ec8b7d1SJesse Brandeburg 	tx_buffer->skb = NULL;
505ec8b7d1SJesse Brandeburg 	dma_unmap_len_set(tx_buffer, len, 0);
515ec8b7d1SJesse Brandeburg 	/* tx_buffer must be completely set up in the transmit path */
525ec8b7d1SJesse Brandeburg }
535ec8b7d1SJesse Brandeburg 
545ec8b7d1SJesse Brandeburg /**
555ec8b7d1SJesse Brandeburg  * iavf_clean_tx_ring - Free any empty Tx buffers
565ec8b7d1SJesse Brandeburg  * @tx_ring: ring to be cleaned
575ec8b7d1SJesse Brandeburg  **/
5856184e01SJesse Brandeburg void iavf_clean_tx_ring(struct iavf_ring *tx_ring)
595ec8b7d1SJesse Brandeburg {
605ec8b7d1SJesse Brandeburg 	unsigned long bi_size;
615ec8b7d1SJesse Brandeburg 	u16 i;
625ec8b7d1SJesse Brandeburg 
635ec8b7d1SJesse Brandeburg 	/* ring already cleared, nothing to do */
645ec8b7d1SJesse Brandeburg 	if (!tx_ring->tx_bi)
655ec8b7d1SJesse Brandeburg 		return;
665ec8b7d1SJesse Brandeburg 
675ec8b7d1SJesse Brandeburg 	/* Free all the Tx ring sk_buffs */
685ec8b7d1SJesse Brandeburg 	for (i = 0; i < tx_ring->count; i++)
6956184e01SJesse Brandeburg 		iavf_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
705ec8b7d1SJesse Brandeburg 
7156184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count;
725ec8b7d1SJesse Brandeburg 	memset(tx_ring->tx_bi, 0, bi_size);
735ec8b7d1SJesse Brandeburg 
745ec8b7d1SJesse Brandeburg 	/* Zero out the descriptor ring */
755ec8b7d1SJesse Brandeburg 	memset(tx_ring->desc, 0, tx_ring->size);
765ec8b7d1SJesse Brandeburg 
775ec8b7d1SJesse Brandeburg 	tx_ring->next_to_use = 0;
785ec8b7d1SJesse Brandeburg 	tx_ring->next_to_clean = 0;
795ec8b7d1SJesse Brandeburg 
805ec8b7d1SJesse Brandeburg 	if (!tx_ring->netdev)
815ec8b7d1SJesse Brandeburg 		return;
825ec8b7d1SJesse Brandeburg 
835ec8b7d1SJesse Brandeburg 	/* cleanup Tx queue statistics */
845ec8b7d1SJesse Brandeburg 	netdev_tx_reset_queue(txring_txq(tx_ring));
855ec8b7d1SJesse Brandeburg }
865ec8b7d1SJesse Brandeburg 
875ec8b7d1SJesse Brandeburg /**
885ec8b7d1SJesse Brandeburg  * iavf_free_tx_resources - Free Tx resources per queue
895ec8b7d1SJesse Brandeburg  * @tx_ring: Tx descriptor ring for a specific queue
905ec8b7d1SJesse Brandeburg  *
915ec8b7d1SJesse Brandeburg  * Free all transmit software resources
925ec8b7d1SJesse Brandeburg  **/
9356184e01SJesse Brandeburg void iavf_free_tx_resources(struct iavf_ring *tx_ring)
945ec8b7d1SJesse Brandeburg {
955ec8b7d1SJesse Brandeburg 	iavf_clean_tx_ring(tx_ring);
965ec8b7d1SJesse Brandeburg 	kfree(tx_ring->tx_bi);
975ec8b7d1SJesse Brandeburg 	tx_ring->tx_bi = NULL;
985ec8b7d1SJesse Brandeburg 
995ec8b7d1SJesse Brandeburg 	if (tx_ring->desc) {
1005ec8b7d1SJesse Brandeburg 		dma_free_coherent(tx_ring->dev, tx_ring->size,
1015ec8b7d1SJesse Brandeburg 				  tx_ring->desc, tx_ring->dma);
1025ec8b7d1SJesse Brandeburg 		tx_ring->desc = NULL;
1035ec8b7d1SJesse Brandeburg 	}
1045ec8b7d1SJesse Brandeburg }
1055ec8b7d1SJesse Brandeburg 
1065ec8b7d1SJesse Brandeburg /**
1075ec8b7d1SJesse Brandeburg  * iavf_get_tx_pending - how many Tx descriptors not processed
1085ec8b7d1SJesse Brandeburg  * @ring: the ring of descriptors
1095ec8b7d1SJesse Brandeburg  * @in_sw: is tx_pending being checked in SW or HW
1105ec8b7d1SJesse Brandeburg  *
1115ec8b7d1SJesse Brandeburg  * Since there is no access to the ring head register
1125ec8b7d1SJesse Brandeburg  * in XL710, we need to use our local copies
1135ec8b7d1SJesse Brandeburg  **/
11456184e01SJesse Brandeburg u32 iavf_get_tx_pending(struct iavf_ring *ring, bool in_sw)
1155ec8b7d1SJesse Brandeburg {
1165ec8b7d1SJesse Brandeburg 	u32 head, tail;
1175ec8b7d1SJesse Brandeburg 
1185ec8b7d1SJesse Brandeburg 	head = ring->next_to_clean;
1195ec8b7d1SJesse Brandeburg 	tail = readl(ring->tail);
1205ec8b7d1SJesse Brandeburg 
1215ec8b7d1SJesse Brandeburg 	if (head != tail)
1225ec8b7d1SJesse Brandeburg 		return (head < tail) ?
1235ec8b7d1SJesse Brandeburg 			tail - head : (tail + ring->count - head);
1245ec8b7d1SJesse Brandeburg 
1255ec8b7d1SJesse Brandeburg 	return 0;
1265ec8b7d1SJesse Brandeburg }
1275ec8b7d1SJesse Brandeburg 
1285ec8b7d1SJesse Brandeburg /**
1295ec8b7d1SJesse Brandeburg  * iavf_detect_recover_hung - Function to detect and recover hung_queues
1305ec8b7d1SJesse Brandeburg  * @vsi:  pointer to vsi struct with tx queues
1315ec8b7d1SJesse Brandeburg  *
1325ec8b7d1SJesse Brandeburg  * VSI has netdev and netdev has TX queues. This function is to check each of
1335ec8b7d1SJesse Brandeburg  * those TX queues if they are hung, trigger recovery by issuing SW interrupt.
1345ec8b7d1SJesse Brandeburg  **/
13556184e01SJesse Brandeburg void iavf_detect_recover_hung(struct iavf_vsi *vsi)
1365ec8b7d1SJesse Brandeburg {
13756184e01SJesse Brandeburg 	struct iavf_ring *tx_ring = NULL;
1385ec8b7d1SJesse Brandeburg 	struct net_device *netdev;
1395ec8b7d1SJesse Brandeburg 	unsigned int i;
1405ec8b7d1SJesse Brandeburg 	int packets;
1415ec8b7d1SJesse Brandeburg 
1425ec8b7d1SJesse Brandeburg 	if (!vsi)
1435ec8b7d1SJesse Brandeburg 		return;
1445ec8b7d1SJesse Brandeburg 
14556184e01SJesse Brandeburg 	if (test_bit(__IAVF_VSI_DOWN, vsi->state))
1465ec8b7d1SJesse Brandeburg 		return;
1475ec8b7d1SJesse Brandeburg 
1485ec8b7d1SJesse Brandeburg 	netdev = vsi->netdev;
1495ec8b7d1SJesse Brandeburg 	if (!netdev)
1505ec8b7d1SJesse Brandeburg 		return;
1515ec8b7d1SJesse Brandeburg 
1525ec8b7d1SJesse Brandeburg 	if (!netif_carrier_ok(netdev))
1535ec8b7d1SJesse Brandeburg 		return;
1545ec8b7d1SJesse Brandeburg 
1555ec8b7d1SJesse Brandeburg 	for (i = 0; i < vsi->back->num_active_queues; i++) {
1565ec8b7d1SJesse Brandeburg 		tx_ring = &vsi->back->tx_rings[i];
1575ec8b7d1SJesse Brandeburg 		if (tx_ring && tx_ring->desc) {
1585ec8b7d1SJesse Brandeburg 			/* If packet counter has not changed the queue is
1595ec8b7d1SJesse Brandeburg 			 * likely stalled, so force an interrupt for this
1605ec8b7d1SJesse Brandeburg 			 * queue.
1615ec8b7d1SJesse Brandeburg 			 *
1625ec8b7d1SJesse Brandeburg 			 * prev_pkt_ctr would be negative if there was no
1635ec8b7d1SJesse Brandeburg 			 * pending work.
1645ec8b7d1SJesse Brandeburg 			 */
1655ec8b7d1SJesse Brandeburg 			packets = tx_ring->stats.packets & INT_MAX;
1665ec8b7d1SJesse Brandeburg 			if (tx_ring->tx_stats.prev_pkt_ctr == packets) {
1675ec8b7d1SJesse Brandeburg 				iavf_force_wb(vsi, tx_ring->q_vector);
1685ec8b7d1SJesse Brandeburg 				continue;
1695ec8b7d1SJesse Brandeburg 			}
1705ec8b7d1SJesse Brandeburg 
1715ec8b7d1SJesse Brandeburg 			/* Memory barrier between read of packet count and call
1725ec8b7d1SJesse Brandeburg 			 * to iavf_get_tx_pending()
1735ec8b7d1SJesse Brandeburg 			 */
1745ec8b7d1SJesse Brandeburg 			smp_rmb();
1755ec8b7d1SJesse Brandeburg 			tx_ring->tx_stats.prev_pkt_ctr =
1765ec8b7d1SJesse Brandeburg 			  iavf_get_tx_pending(tx_ring, true) ? packets : -1;
1775ec8b7d1SJesse Brandeburg 		}
1785ec8b7d1SJesse Brandeburg 	}
1795ec8b7d1SJesse Brandeburg }
1805ec8b7d1SJesse Brandeburg 
1815ec8b7d1SJesse Brandeburg #define WB_STRIDE 4
1825ec8b7d1SJesse Brandeburg 
1835ec8b7d1SJesse Brandeburg /**
18456184e01SJesse Brandeburg  * iavf_clean_tx_irq - Reclaim resources after transmit completes
1855ec8b7d1SJesse Brandeburg  * @vsi: the VSI we care about
1865ec8b7d1SJesse Brandeburg  * @tx_ring: Tx ring to clean
1875ec8b7d1SJesse Brandeburg  * @napi_budget: Used to determine if we are in netpoll
1885ec8b7d1SJesse Brandeburg  *
1895ec8b7d1SJesse Brandeburg  * Returns true if there's any budget left (e.g. the clean is finished)
1905ec8b7d1SJesse Brandeburg  **/
19156184e01SJesse Brandeburg static bool iavf_clean_tx_irq(struct iavf_vsi *vsi,
19256184e01SJesse Brandeburg 			      struct iavf_ring *tx_ring, int napi_budget)
1935ec8b7d1SJesse Brandeburg {
1945ec8b7d1SJesse Brandeburg 	u16 i = tx_ring->next_to_clean;
19556184e01SJesse Brandeburg 	struct iavf_tx_buffer *tx_buf;
19656184e01SJesse Brandeburg 	struct iavf_tx_desc *tx_desc;
1975ec8b7d1SJesse Brandeburg 	unsigned int total_bytes = 0, total_packets = 0;
1985ec8b7d1SJesse Brandeburg 	unsigned int budget = vsi->work_limit;
1995ec8b7d1SJesse Brandeburg 
2005ec8b7d1SJesse Brandeburg 	tx_buf = &tx_ring->tx_bi[i];
201f1cad2ceSJesse Brandeburg 	tx_desc = IAVF_TX_DESC(tx_ring, i);
2025ec8b7d1SJesse Brandeburg 	i -= tx_ring->count;
2035ec8b7d1SJesse Brandeburg 
2045ec8b7d1SJesse Brandeburg 	do {
20556184e01SJesse Brandeburg 		struct iavf_tx_desc *eop_desc = tx_buf->next_to_watch;
2065ec8b7d1SJesse Brandeburg 
2075ec8b7d1SJesse Brandeburg 		/* if next_to_watch is not set then there is no work pending */
2085ec8b7d1SJesse Brandeburg 		if (!eop_desc)
2095ec8b7d1SJesse Brandeburg 			break;
2105ec8b7d1SJesse Brandeburg 
2115ec8b7d1SJesse Brandeburg 		/* prevent any other reads prior to eop_desc */
2125ec8b7d1SJesse Brandeburg 		smp_rmb();
2135ec8b7d1SJesse Brandeburg 
214ad64ed8bSJesse Brandeburg 		iavf_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
2155ec8b7d1SJesse Brandeburg 		/* if the descriptor isn't done, no work yet to do */
2165ec8b7d1SJesse Brandeburg 		if (!(eop_desc->cmd_type_offset_bsz &
217f1cad2ceSJesse Brandeburg 		      cpu_to_le64(IAVF_TX_DESC_DTYPE_DESC_DONE)))
2185ec8b7d1SJesse Brandeburg 			break;
2195ec8b7d1SJesse Brandeburg 
2205ec8b7d1SJesse Brandeburg 		/* clear next_to_watch to prevent false hangs */
2215ec8b7d1SJesse Brandeburg 		tx_buf->next_to_watch = NULL;
2225ec8b7d1SJesse Brandeburg 
2235ec8b7d1SJesse Brandeburg 		/* update the statistics for this packet */
2245ec8b7d1SJesse Brandeburg 		total_bytes += tx_buf->bytecount;
2255ec8b7d1SJesse Brandeburg 		total_packets += tx_buf->gso_segs;
2265ec8b7d1SJesse Brandeburg 
2275ec8b7d1SJesse Brandeburg 		/* free the skb */
2285ec8b7d1SJesse Brandeburg 		napi_consume_skb(tx_buf->skb, napi_budget);
2295ec8b7d1SJesse Brandeburg 
2305ec8b7d1SJesse Brandeburg 		/* unmap skb header data */
2315ec8b7d1SJesse Brandeburg 		dma_unmap_single(tx_ring->dev,
2325ec8b7d1SJesse Brandeburg 				 dma_unmap_addr(tx_buf, dma),
2335ec8b7d1SJesse Brandeburg 				 dma_unmap_len(tx_buf, len),
2345ec8b7d1SJesse Brandeburg 				 DMA_TO_DEVICE);
2355ec8b7d1SJesse Brandeburg 
2365ec8b7d1SJesse Brandeburg 		/* clear tx_buffer data */
2375ec8b7d1SJesse Brandeburg 		tx_buf->skb = NULL;
2385ec8b7d1SJesse Brandeburg 		dma_unmap_len_set(tx_buf, len, 0);
2395ec8b7d1SJesse Brandeburg 
2405ec8b7d1SJesse Brandeburg 		/* unmap remaining buffers */
2415ec8b7d1SJesse Brandeburg 		while (tx_desc != eop_desc) {
242ad64ed8bSJesse Brandeburg 			iavf_trace(clean_tx_irq_unmap,
2435ec8b7d1SJesse Brandeburg 				   tx_ring, tx_desc, tx_buf);
2445ec8b7d1SJesse Brandeburg 
2455ec8b7d1SJesse Brandeburg 			tx_buf++;
2465ec8b7d1SJesse Brandeburg 			tx_desc++;
2475ec8b7d1SJesse Brandeburg 			i++;
2485ec8b7d1SJesse Brandeburg 			if (unlikely(!i)) {
2495ec8b7d1SJesse Brandeburg 				i -= tx_ring->count;
2505ec8b7d1SJesse Brandeburg 				tx_buf = tx_ring->tx_bi;
251f1cad2ceSJesse Brandeburg 				tx_desc = IAVF_TX_DESC(tx_ring, 0);
2525ec8b7d1SJesse Brandeburg 			}
2535ec8b7d1SJesse Brandeburg 
2545ec8b7d1SJesse Brandeburg 			/* unmap any remaining paged data */
2555ec8b7d1SJesse Brandeburg 			if (dma_unmap_len(tx_buf, len)) {
2565ec8b7d1SJesse Brandeburg 				dma_unmap_page(tx_ring->dev,
2575ec8b7d1SJesse Brandeburg 					       dma_unmap_addr(tx_buf, dma),
2585ec8b7d1SJesse Brandeburg 					       dma_unmap_len(tx_buf, len),
2595ec8b7d1SJesse Brandeburg 					       DMA_TO_DEVICE);
2605ec8b7d1SJesse Brandeburg 				dma_unmap_len_set(tx_buf, len, 0);
2615ec8b7d1SJesse Brandeburg 			}
2625ec8b7d1SJesse Brandeburg 		}
2635ec8b7d1SJesse Brandeburg 
2645ec8b7d1SJesse Brandeburg 		/* move us one more past the eop_desc for start of next pkt */
2655ec8b7d1SJesse Brandeburg 		tx_buf++;
2665ec8b7d1SJesse Brandeburg 		tx_desc++;
2675ec8b7d1SJesse Brandeburg 		i++;
2685ec8b7d1SJesse Brandeburg 		if (unlikely(!i)) {
2695ec8b7d1SJesse Brandeburg 			i -= tx_ring->count;
2705ec8b7d1SJesse Brandeburg 			tx_buf = tx_ring->tx_bi;
271f1cad2ceSJesse Brandeburg 			tx_desc = IAVF_TX_DESC(tx_ring, 0);
2725ec8b7d1SJesse Brandeburg 		}
2735ec8b7d1SJesse Brandeburg 
2745ec8b7d1SJesse Brandeburg 		prefetch(tx_desc);
2755ec8b7d1SJesse Brandeburg 
2765ec8b7d1SJesse Brandeburg 		/* update budget accounting */
2775ec8b7d1SJesse Brandeburg 		budget--;
2785ec8b7d1SJesse Brandeburg 	} while (likely(budget));
2795ec8b7d1SJesse Brandeburg 
2805ec8b7d1SJesse Brandeburg 	i += tx_ring->count;
2815ec8b7d1SJesse Brandeburg 	tx_ring->next_to_clean = i;
2825ec8b7d1SJesse Brandeburg 	u64_stats_update_begin(&tx_ring->syncp);
2835ec8b7d1SJesse Brandeburg 	tx_ring->stats.bytes += total_bytes;
2845ec8b7d1SJesse Brandeburg 	tx_ring->stats.packets += total_packets;
2855ec8b7d1SJesse Brandeburg 	u64_stats_update_end(&tx_ring->syncp);
2865ec8b7d1SJesse Brandeburg 	tx_ring->q_vector->tx.total_bytes += total_bytes;
2875ec8b7d1SJesse Brandeburg 	tx_ring->q_vector->tx.total_packets += total_packets;
2885ec8b7d1SJesse Brandeburg 
28956184e01SJesse Brandeburg 	if (tx_ring->flags & IAVF_TXR_FLAGS_WB_ON_ITR) {
2905ec8b7d1SJesse Brandeburg 		/* check to see if there are < 4 descriptors
2915ec8b7d1SJesse Brandeburg 		 * waiting to be written back, then kick the hardware to force
2925ec8b7d1SJesse Brandeburg 		 * them to be written back in case we stay in NAPI.
2935ec8b7d1SJesse Brandeburg 		 * In this mode on X722 we do not enable Interrupt.
2945ec8b7d1SJesse Brandeburg 		 */
2955ec8b7d1SJesse Brandeburg 		unsigned int j = iavf_get_tx_pending(tx_ring, false);
2965ec8b7d1SJesse Brandeburg 
2975ec8b7d1SJesse Brandeburg 		if (budget &&
2985ec8b7d1SJesse Brandeburg 		    ((j / WB_STRIDE) == 0) && (j > 0) &&
29956184e01SJesse Brandeburg 		    !test_bit(__IAVF_VSI_DOWN, vsi->state) &&
30056184e01SJesse Brandeburg 		    (IAVF_DESC_UNUSED(tx_ring) != tx_ring->count))
3015ec8b7d1SJesse Brandeburg 			tx_ring->arm_wb = true;
3025ec8b7d1SJesse Brandeburg 	}
3035ec8b7d1SJesse Brandeburg 
3045ec8b7d1SJesse Brandeburg 	/* notify netdev of completed buffers */
3055ec8b7d1SJesse Brandeburg 	netdev_tx_completed_queue(txring_txq(tx_ring),
3065ec8b7d1SJesse Brandeburg 				  total_packets, total_bytes);
3075ec8b7d1SJesse Brandeburg 
3085ec8b7d1SJesse Brandeburg #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
3095ec8b7d1SJesse Brandeburg 	if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
31056184e01SJesse Brandeburg 		     (IAVF_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
3115ec8b7d1SJesse Brandeburg 		/* Make sure that anybody stopping the queue after this
3125ec8b7d1SJesse Brandeburg 		 * sees the new next_to_clean.
3135ec8b7d1SJesse Brandeburg 		 */
3145ec8b7d1SJesse Brandeburg 		smp_mb();
3155ec8b7d1SJesse Brandeburg 		if (__netif_subqueue_stopped(tx_ring->netdev,
3165ec8b7d1SJesse Brandeburg 					     tx_ring->queue_index) &&
31756184e01SJesse Brandeburg 		   !test_bit(__IAVF_VSI_DOWN, vsi->state)) {
3185ec8b7d1SJesse Brandeburg 			netif_wake_subqueue(tx_ring->netdev,
3195ec8b7d1SJesse Brandeburg 					    tx_ring->queue_index);
3205ec8b7d1SJesse Brandeburg 			++tx_ring->tx_stats.restart_queue;
3215ec8b7d1SJesse Brandeburg 		}
3225ec8b7d1SJesse Brandeburg 	}
3235ec8b7d1SJesse Brandeburg 
3245ec8b7d1SJesse Brandeburg 	return !!budget;
3255ec8b7d1SJesse Brandeburg }
3265ec8b7d1SJesse Brandeburg 
3275ec8b7d1SJesse Brandeburg /**
3285ec8b7d1SJesse Brandeburg  * iavf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
3295ec8b7d1SJesse Brandeburg  * @vsi: the VSI we care about
3305ec8b7d1SJesse Brandeburg  * @q_vector: the vector on which to enable writeback
3315ec8b7d1SJesse Brandeburg  *
3325ec8b7d1SJesse Brandeburg  **/
33356184e01SJesse Brandeburg static void iavf_enable_wb_on_itr(struct iavf_vsi *vsi,
33456184e01SJesse Brandeburg 				  struct iavf_q_vector *q_vector)
3355ec8b7d1SJesse Brandeburg {
3365ec8b7d1SJesse Brandeburg 	u16 flags = q_vector->tx.ring[0].flags;
3375ec8b7d1SJesse Brandeburg 	u32 val;
3385ec8b7d1SJesse Brandeburg 
33956184e01SJesse Brandeburg 	if (!(flags & IAVF_TXR_FLAGS_WB_ON_ITR))
3405ec8b7d1SJesse Brandeburg 		return;
3415ec8b7d1SJesse Brandeburg 
3425ec8b7d1SJesse Brandeburg 	if (q_vector->arm_wb_state)
3435ec8b7d1SJesse Brandeburg 		return;
3445ec8b7d1SJesse Brandeburg 
345f1cad2ceSJesse Brandeburg 	val = IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
346f1cad2ceSJesse Brandeburg 	      IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
3475ec8b7d1SJesse Brandeburg 
3485ec8b7d1SJesse Brandeburg 	wr32(&vsi->back->hw,
349f1cad2ceSJesse Brandeburg 	     IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx), val);
3505ec8b7d1SJesse Brandeburg 	q_vector->arm_wb_state = true;
3515ec8b7d1SJesse Brandeburg }
3525ec8b7d1SJesse Brandeburg 
3535ec8b7d1SJesse Brandeburg /**
3545ec8b7d1SJesse Brandeburg  * iavf_force_wb - Issue SW Interrupt so HW does a wb
3555ec8b7d1SJesse Brandeburg  * @vsi: the VSI we care about
3565ec8b7d1SJesse Brandeburg  * @q_vector: the vector  on which to force writeback
3575ec8b7d1SJesse Brandeburg  *
3585ec8b7d1SJesse Brandeburg  **/
35956184e01SJesse Brandeburg void iavf_force_wb(struct iavf_vsi *vsi, struct iavf_q_vector *q_vector)
3605ec8b7d1SJesse Brandeburg {
361f1cad2ceSJesse Brandeburg 	u32 val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
362f1cad2ceSJesse Brandeburg 		  IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
363f1cad2ceSJesse Brandeburg 		  IAVF_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
364f1cad2ceSJesse Brandeburg 		  IAVF_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
3655ec8b7d1SJesse Brandeburg 		  /* allow 00 to be written to the index */;
3665ec8b7d1SJesse Brandeburg 
3675ec8b7d1SJesse Brandeburg 	wr32(&vsi->back->hw,
368f1cad2ceSJesse Brandeburg 	     IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx),
3695ec8b7d1SJesse Brandeburg 	     val);
3705ec8b7d1SJesse Brandeburg }
3715ec8b7d1SJesse Brandeburg 
37256184e01SJesse Brandeburg static inline bool iavf_container_is_rx(struct iavf_q_vector *q_vector,
37356184e01SJesse Brandeburg 					struct iavf_ring_container *rc)
3745ec8b7d1SJesse Brandeburg {
3755ec8b7d1SJesse Brandeburg 	return &q_vector->rx == rc;
3765ec8b7d1SJesse Brandeburg }
3775ec8b7d1SJesse Brandeburg 
37856184e01SJesse Brandeburg static inline unsigned int iavf_itr_divisor(struct iavf_q_vector *q_vector)
3795ec8b7d1SJesse Brandeburg {
3805ec8b7d1SJesse Brandeburg 	unsigned int divisor;
3815ec8b7d1SJesse Brandeburg 
3825ec8b7d1SJesse Brandeburg 	switch (q_vector->adapter->link_speed) {
3835ec8b7d1SJesse Brandeburg 	case I40E_LINK_SPEED_40GB:
38456184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 1024;
3855ec8b7d1SJesse Brandeburg 		break;
3865ec8b7d1SJesse Brandeburg 	case I40E_LINK_SPEED_25GB:
3875ec8b7d1SJesse Brandeburg 	case I40E_LINK_SPEED_20GB:
38856184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 512;
3895ec8b7d1SJesse Brandeburg 		break;
3905ec8b7d1SJesse Brandeburg 	default:
3915ec8b7d1SJesse Brandeburg 	case I40E_LINK_SPEED_10GB:
39256184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 256;
3935ec8b7d1SJesse Brandeburg 		break;
3945ec8b7d1SJesse Brandeburg 	case I40E_LINK_SPEED_1GB:
3955ec8b7d1SJesse Brandeburg 	case I40E_LINK_SPEED_100MB:
39656184e01SJesse Brandeburg 		divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 32;
3975ec8b7d1SJesse Brandeburg 		break;
3985ec8b7d1SJesse Brandeburg 	}
3995ec8b7d1SJesse Brandeburg 
4005ec8b7d1SJesse Brandeburg 	return divisor;
4015ec8b7d1SJesse Brandeburg }
4025ec8b7d1SJesse Brandeburg 
4035ec8b7d1SJesse Brandeburg /**
40456184e01SJesse Brandeburg  * iavf_update_itr - update the dynamic ITR value based on statistics
4055ec8b7d1SJesse Brandeburg  * @q_vector: structure containing interrupt and ring information
4065ec8b7d1SJesse Brandeburg  * @rc: structure containing ring performance data
4075ec8b7d1SJesse Brandeburg  *
4085ec8b7d1SJesse Brandeburg  * Stores a new ITR value based on packets and byte
4095ec8b7d1SJesse Brandeburg  * counts during the last interrupt.  The advantage of per interrupt
4105ec8b7d1SJesse Brandeburg  * computation is faster updates and more accurate ITR for the current
4115ec8b7d1SJesse Brandeburg  * traffic pattern.  Constants in this function were computed
4125ec8b7d1SJesse Brandeburg  * based on theoretical maximum wire speed and thresholds were set based
4135ec8b7d1SJesse Brandeburg  * on testing data as well as attempting to minimize response time
4145ec8b7d1SJesse Brandeburg  * while increasing bulk throughput.
4155ec8b7d1SJesse Brandeburg  **/
41656184e01SJesse Brandeburg static void iavf_update_itr(struct iavf_q_vector *q_vector,
41756184e01SJesse Brandeburg 			    struct iavf_ring_container *rc)
4185ec8b7d1SJesse Brandeburg {
4195ec8b7d1SJesse Brandeburg 	unsigned int avg_wire_size, packets, bytes, itr;
4205ec8b7d1SJesse Brandeburg 	unsigned long next_update = jiffies;
4215ec8b7d1SJesse Brandeburg 
4225ec8b7d1SJesse Brandeburg 	/* If we don't have any rings just leave ourselves set for maximum
4235ec8b7d1SJesse Brandeburg 	 * possible latency so we take ourselves out of the equation.
4245ec8b7d1SJesse Brandeburg 	 */
4255ec8b7d1SJesse Brandeburg 	if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting))
4265ec8b7d1SJesse Brandeburg 		return;
4275ec8b7d1SJesse Brandeburg 
4285ec8b7d1SJesse Brandeburg 	/* For Rx we want to push the delay up and default to low latency.
4295ec8b7d1SJesse Brandeburg 	 * for Tx we want to pull the delay down and default to high latency.
4305ec8b7d1SJesse Brandeburg 	 */
43156184e01SJesse Brandeburg 	itr = iavf_container_is_rx(q_vector, rc) ?
43256184e01SJesse Brandeburg 	      IAVF_ITR_ADAPTIVE_MIN_USECS | IAVF_ITR_ADAPTIVE_LATENCY :
43356184e01SJesse Brandeburg 	      IAVF_ITR_ADAPTIVE_MAX_USECS | IAVF_ITR_ADAPTIVE_LATENCY;
4345ec8b7d1SJesse Brandeburg 
4355ec8b7d1SJesse Brandeburg 	/* If we didn't update within up to 1 - 2 jiffies we can assume
4365ec8b7d1SJesse Brandeburg 	 * that either packets are coming in so slow there hasn't been
4375ec8b7d1SJesse Brandeburg 	 * any work, or that there is so much work that NAPI is dealing
4385ec8b7d1SJesse Brandeburg 	 * with interrupt moderation and we don't need to do anything.
4395ec8b7d1SJesse Brandeburg 	 */
4405ec8b7d1SJesse Brandeburg 	if (time_after(next_update, rc->next_update))
4415ec8b7d1SJesse Brandeburg 		goto clear_counts;
4425ec8b7d1SJesse Brandeburg 
4435ec8b7d1SJesse Brandeburg 	/* If itr_countdown is set it means we programmed an ITR within
4445ec8b7d1SJesse Brandeburg 	 * the last 4 interrupt cycles. This has a side effect of us
4455ec8b7d1SJesse Brandeburg 	 * potentially firing an early interrupt. In order to work around
4465ec8b7d1SJesse Brandeburg 	 * this we need to throw out any data received for a few
4475ec8b7d1SJesse Brandeburg 	 * interrupts following the update.
4485ec8b7d1SJesse Brandeburg 	 */
4495ec8b7d1SJesse Brandeburg 	if (q_vector->itr_countdown) {
4505ec8b7d1SJesse Brandeburg 		itr = rc->target_itr;
4515ec8b7d1SJesse Brandeburg 		goto clear_counts;
4525ec8b7d1SJesse Brandeburg 	}
4535ec8b7d1SJesse Brandeburg 
4545ec8b7d1SJesse Brandeburg 	packets = rc->total_packets;
4555ec8b7d1SJesse Brandeburg 	bytes = rc->total_bytes;
4565ec8b7d1SJesse Brandeburg 
45756184e01SJesse Brandeburg 	if (iavf_container_is_rx(q_vector, rc)) {
4585ec8b7d1SJesse Brandeburg 		/* If Rx there are 1 to 4 packets and bytes are less than
4595ec8b7d1SJesse Brandeburg 		 * 9000 assume insufficient data to use bulk rate limiting
4605ec8b7d1SJesse Brandeburg 		 * approach unless Tx is already in bulk rate limiting. We
4615ec8b7d1SJesse Brandeburg 		 * are likely latency driven.
4625ec8b7d1SJesse Brandeburg 		 */
4635ec8b7d1SJesse Brandeburg 		if (packets && packets < 4 && bytes < 9000 &&
46456184e01SJesse Brandeburg 		    (q_vector->tx.target_itr & IAVF_ITR_ADAPTIVE_LATENCY)) {
46556184e01SJesse Brandeburg 			itr = IAVF_ITR_ADAPTIVE_LATENCY;
4665ec8b7d1SJesse Brandeburg 			goto adjust_by_size;
4675ec8b7d1SJesse Brandeburg 		}
4685ec8b7d1SJesse Brandeburg 	} else if (packets < 4) {
4695ec8b7d1SJesse Brandeburg 		/* If we have Tx and Rx ITR maxed and Tx ITR is running in
4705ec8b7d1SJesse Brandeburg 		 * bulk mode and we are receiving 4 or fewer packets just
4715ec8b7d1SJesse Brandeburg 		 * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
4725ec8b7d1SJesse Brandeburg 		 * that the Rx can relax.
4735ec8b7d1SJesse Brandeburg 		 */
47456184e01SJesse Brandeburg 		if (rc->target_itr == IAVF_ITR_ADAPTIVE_MAX_USECS &&
47556184e01SJesse Brandeburg 		    (q_vector->rx.target_itr & IAVF_ITR_MASK) ==
47656184e01SJesse Brandeburg 		     IAVF_ITR_ADAPTIVE_MAX_USECS)
4775ec8b7d1SJesse Brandeburg 			goto clear_counts;
4785ec8b7d1SJesse Brandeburg 	} else if (packets > 32) {
4795ec8b7d1SJesse Brandeburg 		/* If we have processed over 32 packets in a single interrupt
4805ec8b7d1SJesse Brandeburg 		 * for Tx assume we need to switch over to "bulk" mode.
4815ec8b7d1SJesse Brandeburg 		 */
48256184e01SJesse Brandeburg 		rc->target_itr &= ~IAVF_ITR_ADAPTIVE_LATENCY;
4835ec8b7d1SJesse Brandeburg 	}
4845ec8b7d1SJesse Brandeburg 
4855ec8b7d1SJesse Brandeburg 	/* We have no packets to actually measure against. This means
4865ec8b7d1SJesse Brandeburg 	 * either one of the other queues on this vector is active or
4875ec8b7d1SJesse Brandeburg 	 * we are a Tx queue doing TSO with too high of an interrupt rate.
4885ec8b7d1SJesse Brandeburg 	 *
4895ec8b7d1SJesse Brandeburg 	 * Between 4 and 56 we can assume that our current interrupt delay
4905ec8b7d1SJesse Brandeburg 	 * is only slightly too low. As such we should increase it by a small
4915ec8b7d1SJesse Brandeburg 	 * fixed amount.
4925ec8b7d1SJesse Brandeburg 	 */
4935ec8b7d1SJesse Brandeburg 	if (packets < 56) {
49456184e01SJesse Brandeburg 		itr = rc->target_itr + IAVF_ITR_ADAPTIVE_MIN_INC;
49556184e01SJesse Brandeburg 		if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) {
49656184e01SJesse Brandeburg 			itr &= IAVF_ITR_ADAPTIVE_LATENCY;
49756184e01SJesse Brandeburg 			itr += IAVF_ITR_ADAPTIVE_MAX_USECS;
4985ec8b7d1SJesse Brandeburg 		}
4995ec8b7d1SJesse Brandeburg 		goto clear_counts;
5005ec8b7d1SJesse Brandeburg 	}
5015ec8b7d1SJesse Brandeburg 
5025ec8b7d1SJesse Brandeburg 	if (packets <= 256) {
5035ec8b7d1SJesse Brandeburg 		itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
50456184e01SJesse Brandeburg 		itr &= IAVF_ITR_MASK;
5055ec8b7d1SJesse Brandeburg 
5065ec8b7d1SJesse Brandeburg 		/* Between 56 and 112 is our "goldilocks" zone where we are
5075ec8b7d1SJesse Brandeburg 		 * working out "just right". Just report that our current
5085ec8b7d1SJesse Brandeburg 		 * ITR is good for us.
5095ec8b7d1SJesse Brandeburg 		 */
5105ec8b7d1SJesse Brandeburg 		if (packets <= 112)
5115ec8b7d1SJesse Brandeburg 			goto clear_counts;
5125ec8b7d1SJesse Brandeburg 
5135ec8b7d1SJesse Brandeburg 		/* If packet count is 128 or greater we are likely looking
5145ec8b7d1SJesse Brandeburg 		 * at a slight overrun of the delay we want. Try halving
5155ec8b7d1SJesse Brandeburg 		 * our delay to see if that will cut the number of packets
5165ec8b7d1SJesse Brandeburg 		 * in half per interrupt.
5175ec8b7d1SJesse Brandeburg 		 */
5185ec8b7d1SJesse Brandeburg 		itr /= 2;
51956184e01SJesse Brandeburg 		itr &= IAVF_ITR_MASK;
52056184e01SJesse Brandeburg 		if (itr < IAVF_ITR_ADAPTIVE_MIN_USECS)
52156184e01SJesse Brandeburg 			itr = IAVF_ITR_ADAPTIVE_MIN_USECS;
5225ec8b7d1SJesse Brandeburg 
5235ec8b7d1SJesse Brandeburg 		goto clear_counts;
5245ec8b7d1SJesse Brandeburg 	}
5255ec8b7d1SJesse Brandeburg 
5265ec8b7d1SJesse Brandeburg 	/* The paths below assume we are dealing with a bulk ITR since
5275ec8b7d1SJesse Brandeburg 	 * number of packets is greater than 256. We are just going to have
5285ec8b7d1SJesse Brandeburg 	 * to compute a value and try to bring the count under control,
5295ec8b7d1SJesse Brandeburg 	 * though for smaller packet sizes there isn't much we can do as
5305ec8b7d1SJesse Brandeburg 	 * NAPI polling will likely be kicking in sooner rather than later.
5315ec8b7d1SJesse Brandeburg 	 */
53256184e01SJesse Brandeburg 	itr = IAVF_ITR_ADAPTIVE_BULK;
5335ec8b7d1SJesse Brandeburg 
5345ec8b7d1SJesse Brandeburg adjust_by_size:
5355ec8b7d1SJesse Brandeburg 	/* If packet counts are 256 or greater we can assume we have a gross
5365ec8b7d1SJesse Brandeburg 	 * overestimation of what the rate should be. Instead of trying to fine
5375ec8b7d1SJesse Brandeburg 	 * tune it just use the formula below to try and dial in an exact value
5385ec8b7d1SJesse Brandeburg 	 * give the current packet size of the frame.
5395ec8b7d1SJesse Brandeburg 	 */
5405ec8b7d1SJesse Brandeburg 	avg_wire_size = bytes / packets;
5415ec8b7d1SJesse Brandeburg 
5425ec8b7d1SJesse Brandeburg 	/* The following is a crude approximation of:
5435ec8b7d1SJesse Brandeburg 	 *  wmem_default / (size + overhead) = desired_pkts_per_int
5445ec8b7d1SJesse Brandeburg 	 *  rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
5455ec8b7d1SJesse Brandeburg 	 *  (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
5465ec8b7d1SJesse Brandeburg 	 *
5475ec8b7d1SJesse Brandeburg 	 * Assuming wmem_default is 212992 and overhead is 640 bytes per
5485ec8b7d1SJesse Brandeburg 	 * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
5495ec8b7d1SJesse Brandeburg 	 * formula down to
5505ec8b7d1SJesse Brandeburg 	 *
5515ec8b7d1SJesse Brandeburg 	 *  (170 * (size + 24)) / (size + 640) = ITR
5525ec8b7d1SJesse Brandeburg 	 *
5535ec8b7d1SJesse Brandeburg 	 * We first do some math on the packet size and then finally bitshift
5545ec8b7d1SJesse Brandeburg 	 * by 8 after rounding up. We also have to account for PCIe link speed
5555ec8b7d1SJesse Brandeburg 	 * difference as ITR scales based on this.
5565ec8b7d1SJesse Brandeburg 	 */
5575ec8b7d1SJesse Brandeburg 	if (avg_wire_size <= 60) {
5585ec8b7d1SJesse Brandeburg 		/* Start at 250k ints/sec */
5595ec8b7d1SJesse Brandeburg 		avg_wire_size = 4096;
5605ec8b7d1SJesse Brandeburg 	} else if (avg_wire_size <= 380) {
5615ec8b7d1SJesse Brandeburg 		/* 250K ints/sec to 60K ints/sec */
5625ec8b7d1SJesse Brandeburg 		avg_wire_size *= 40;
5635ec8b7d1SJesse Brandeburg 		avg_wire_size += 1696;
5645ec8b7d1SJesse Brandeburg 	} else if (avg_wire_size <= 1084) {
5655ec8b7d1SJesse Brandeburg 		/* 60K ints/sec to 36K ints/sec */
5665ec8b7d1SJesse Brandeburg 		avg_wire_size *= 15;
5675ec8b7d1SJesse Brandeburg 		avg_wire_size += 11452;
5685ec8b7d1SJesse Brandeburg 	} else if (avg_wire_size <= 1980) {
5695ec8b7d1SJesse Brandeburg 		/* 36K ints/sec to 30K ints/sec */
5705ec8b7d1SJesse Brandeburg 		avg_wire_size *= 5;
5715ec8b7d1SJesse Brandeburg 		avg_wire_size += 22420;
5725ec8b7d1SJesse Brandeburg 	} else {
5735ec8b7d1SJesse Brandeburg 		/* plateau at a limit of 30K ints/sec */
5745ec8b7d1SJesse Brandeburg 		avg_wire_size = 32256;
5755ec8b7d1SJesse Brandeburg 	}
5765ec8b7d1SJesse Brandeburg 
5775ec8b7d1SJesse Brandeburg 	/* If we are in low latency mode halve our delay which doubles the
5785ec8b7d1SJesse Brandeburg 	 * rate to somewhere between 100K to 16K ints/sec
5795ec8b7d1SJesse Brandeburg 	 */
58056184e01SJesse Brandeburg 	if (itr & IAVF_ITR_ADAPTIVE_LATENCY)
5815ec8b7d1SJesse Brandeburg 		avg_wire_size /= 2;
5825ec8b7d1SJesse Brandeburg 
5835ec8b7d1SJesse Brandeburg 	/* Resultant value is 256 times larger than it needs to be. This
5845ec8b7d1SJesse Brandeburg 	 * gives us room to adjust the value as needed to either increase
5855ec8b7d1SJesse Brandeburg 	 * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc.
5865ec8b7d1SJesse Brandeburg 	 *
5875ec8b7d1SJesse Brandeburg 	 * Use addition as we have already recorded the new latency flag
5885ec8b7d1SJesse Brandeburg 	 * for the ITR value.
5895ec8b7d1SJesse Brandeburg 	 */
59056184e01SJesse Brandeburg 	itr += DIV_ROUND_UP(avg_wire_size, iavf_itr_divisor(q_vector)) *
59156184e01SJesse Brandeburg 	       IAVF_ITR_ADAPTIVE_MIN_INC;
5925ec8b7d1SJesse Brandeburg 
59356184e01SJesse Brandeburg 	if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) {
59456184e01SJesse Brandeburg 		itr &= IAVF_ITR_ADAPTIVE_LATENCY;
59556184e01SJesse Brandeburg 		itr += IAVF_ITR_ADAPTIVE_MAX_USECS;
5965ec8b7d1SJesse Brandeburg 	}
5975ec8b7d1SJesse Brandeburg 
5985ec8b7d1SJesse Brandeburg clear_counts:
5995ec8b7d1SJesse Brandeburg 	/* write back value */
6005ec8b7d1SJesse Brandeburg 	rc->target_itr = itr;
6015ec8b7d1SJesse Brandeburg 
6025ec8b7d1SJesse Brandeburg 	/* next update should occur within next jiffy */
6035ec8b7d1SJesse Brandeburg 	rc->next_update = next_update + 1;
6045ec8b7d1SJesse Brandeburg 
6055ec8b7d1SJesse Brandeburg 	rc->total_bytes = 0;
6065ec8b7d1SJesse Brandeburg 	rc->total_packets = 0;
6075ec8b7d1SJesse Brandeburg }
6085ec8b7d1SJesse Brandeburg 
6095ec8b7d1SJesse Brandeburg /**
6105ec8b7d1SJesse Brandeburg  * iavf_setup_tx_descriptors - Allocate the Tx descriptors
6115ec8b7d1SJesse Brandeburg  * @tx_ring: the tx ring to set up
6125ec8b7d1SJesse Brandeburg  *
6135ec8b7d1SJesse Brandeburg  * Return 0 on success, negative on error
6145ec8b7d1SJesse Brandeburg  **/
61556184e01SJesse Brandeburg int iavf_setup_tx_descriptors(struct iavf_ring *tx_ring)
6165ec8b7d1SJesse Brandeburg {
6175ec8b7d1SJesse Brandeburg 	struct device *dev = tx_ring->dev;
6185ec8b7d1SJesse Brandeburg 	int bi_size;
6195ec8b7d1SJesse Brandeburg 
6205ec8b7d1SJesse Brandeburg 	if (!dev)
6215ec8b7d1SJesse Brandeburg 		return -ENOMEM;
6225ec8b7d1SJesse Brandeburg 
6235ec8b7d1SJesse Brandeburg 	/* warn if we are about to overwrite the pointer */
6245ec8b7d1SJesse Brandeburg 	WARN_ON(tx_ring->tx_bi);
62556184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count;
6265ec8b7d1SJesse Brandeburg 	tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
6275ec8b7d1SJesse Brandeburg 	if (!tx_ring->tx_bi)
6285ec8b7d1SJesse Brandeburg 		goto err;
6295ec8b7d1SJesse Brandeburg 
6305ec8b7d1SJesse Brandeburg 	/* round up to nearest 4K */
63156184e01SJesse Brandeburg 	tx_ring->size = tx_ring->count * sizeof(struct iavf_tx_desc);
6325ec8b7d1SJesse Brandeburg 	tx_ring->size = ALIGN(tx_ring->size, 4096);
6335ec8b7d1SJesse Brandeburg 	tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
6345ec8b7d1SJesse Brandeburg 					   &tx_ring->dma, GFP_KERNEL);
6355ec8b7d1SJesse Brandeburg 	if (!tx_ring->desc) {
6365ec8b7d1SJesse Brandeburg 		dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
6375ec8b7d1SJesse Brandeburg 			 tx_ring->size);
6385ec8b7d1SJesse Brandeburg 		goto err;
6395ec8b7d1SJesse Brandeburg 	}
6405ec8b7d1SJesse Brandeburg 
6415ec8b7d1SJesse Brandeburg 	tx_ring->next_to_use = 0;
6425ec8b7d1SJesse Brandeburg 	tx_ring->next_to_clean = 0;
6435ec8b7d1SJesse Brandeburg 	tx_ring->tx_stats.prev_pkt_ctr = -1;
6445ec8b7d1SJesse Brandeburg 	return 0;
6455ec8b7d1SJesse Brandeburg 
6465ec8b7d1SJesse Brandeburg err:
6475ec8b7d1SJesse Brandeburg 	kfree(tx_ring->tx_bi);
6485ec8b7d1SJesse Brandeburg 	tx_ring->tx_bi = NULL;
6495ec8b7d1SJesse Brandeburg 	return -ENOMEM;
6505ec8b7d1SJesse Brandeburg }
6515ec8b7d1SJesse Brandeburg 
6525ec8b7d1SJesse Brandeburg /**
6535ec8b7d1SJesse Brandeburg  * iavf_clean_rx_ring - Free Rx buffers
6545ec8b7d1SJesse Brandeburg  * @rx_ring: ring to be cleaned
6555ec8b7d1SJesse Brandeburg  **/
65656184e01SJesse Brandeburg void iavf_clean_rx_ring(struct iavf_ring *rx_ring)
6575ec8b7d1SJesse Brandeburg {
6585ec8b7d1SJesse Brandeburg 	unsigned long bi_size;
6595ec8b7d1SJesse Brandeburg 	u16 i;
6605ec8b7d1SJesse Brandeburg 
6615ec8b7d1SJesse Brandeburg 	/* ring already cleared, nothing to do */
6625ec8b7d1SJesse Brandeburg 	if (!rx_ring->rx_bi)
6635ec8b7d1SJesse Brandeburg 		return;
6645ec8b7d1SJesse Brandeburg 
6655ec8b7d1SJesse Brandeburg 	if (rx_ring->skb) {
6665ec8b7d1SJesse Brandeburg 		dev_kfree_skb(rx_ring->skb);
6675ec8b7d1SJesse Brandeburg 		rx_ring->skb = NULL;
6685ec8b7d1SJesse Brandeburg 	}
6695ec8b7d1SJesse Brandeburg 
6705ec8b7d1SJesse Brandeburg 	/* Free all the Rx ring sk_buffs */
6715ec8b7d1SJesse Brandeburg 	for (i = 0; i < rx_ring->count; i++) {
67256184e01SJesse Brandeburg 		struct iavf_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
6735ec8b7d1SJesse Brandeburg 
6745ec8b7d1SJesse Brandeburg 		if (!rx_bi->page)
6755ec8b7d1SJesse Brandeburg 			continue;
6765ec8b7d1SJesse Brandeburg 
6775ec8b7d1SJesse Brandeburg 		/* Invalidate cache lines that may have been written to by
6785ec8b7d1SJesse Brandeburg 		 * device so that we avoid corrupting memory.
6795ec8b7d1SJesse Brandeburg 		 */
6805ec8b7d1SJesse Brandeburg 		dma_sync_single_range_for_cpu(rx_ring->dev,
6815ec8b7d1SJesse Brandeburg 					      rx_bi->dma,
6825ec8b7d1SJesse Brandeburg 					      rx_bi->page_offset,
6835ec8b7d1SJesse Brandeburg 					      rx_ring->rx_buf_len,
6845ec8b7d1SJesse Brandeburg 					      DMA_FROM_DEVICE);
6855ec8b7d1SJesse Brandeburg 
6865ec8b7d1SJesse Brandeburg 		/* free resources associated with mapping */
6875ec8b7d1SJesse Brandeburg 		dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
68856184e01SJesse Brandeburg 				     iavf_rx_pg_size(rx_ring),
6895ec8b7d1SJesse Brandeburg 				     DMA_FROM_DEVICE,
69056184e01SJesse Brandeburg 				     IAVF_RX_DMA_ATTR);
6915ec8b7d1SJesse Brandeburg 
6925ec8b7d1SJesse Brandeburg 		__page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
6935ec8b7d1SJesse Brandeburg 
6945ec8b7d1SJesse Brandeburg 		rx_bi->page = NULL;
6955ec8b7d1SJesse Brandeburg 		rx_bi->page_offset = 0;
6965ec8b7d1SJesse Brandeburg 	}
6975ec8b7d1SJesse Brandeburg 
69856184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count;
6995ec8b7d1SJesse Brandeburg 	memset(rx_ring->rx_bi, 0, bi_size);
7005ec8b7d1SJesse Brandeburg 
7015ec8b7d1SJesse Brandeburg 	/* Zero out the descriptor ring */
7025ec8b7d1SJesse Brandeburg 	memset(rx_ring->desc, 0, rx_ring->size);
7035ec8b7d1SJesse Brandeburg 
7045ec8b7d1SJesse Brandeburg 	rx_ring->next_to_alloc = 0;
7055ec8b7d1SJesse Brandeburg 	rx_ring->next_to_clean = 0;
7065ec8b7d1SJesse Brandeburg 	rx_ring->next_to_use = 0;
7075ec8b7d1SJesse Brandeburg }
7085ec8b7d1SJesse Brandeburg 
7095ec8b7d1SJesse Brandeburg /**
7105ec8b7d1SJesse Brandeburg  * iavf_free_rx_resources - Free Rx resources
7115ec8b7d1SJesse Brandeburg  * @rx_ring: ring to clean the resources from
7125ec8b7d1SJesse Brandeburg  *
7135ec8b7d1SJesse Brandeburg  * Free all receive software resources
7145ec8b7d1SJesse Brandeburg  **/
71556184e01SJesse Brandeburg void iavf_free_rx_resources(struct iavf_ring *rx_ring)
7165ec8b7d1SJesse Brandeburg {
7175ec8b7d1SJesse Brandeburg 	iavf_clean_rx_ring(rx_ring);
7185ec8b7d1SJesse Brandeburg 	kfree(rx_ring->rx_bi);
7195ec8b7d1SJesse Brandeburg 	rx_ring->rx_bi = NULL;
7205ec8b7d1SJesse Brandeburg 
7215ec8b7d1SJesse Brandeburg 	if (rx_ring->desc) {
7225ec8b7d1SJesse Brandeburg 		dma_free_coherent(rx_ring->dev, rx_ring->size,
7235ec8b7d1SJesse Brandeburg 				  rx_ring->desc, rx_ring->dma);
7245ec8b7d1SJesse Brandeburg 		rx_ring->desc = NULL;
7255ec8b7d1SJesse Brandeburg 	}
7265ec8b7d1SJesse Brandeburg }
7275ec8b7d1SJesse Brandeburg 
7285ec8b7d1SJesse Brandeburg /**
7295ec8b7d1SJesse Brandeburg  * iavf_setup_rx_descriptors - Allocate Rx descriptors
7305ec8b7d1SJesse Brandeburg  * @rx_ring: Rx descriptor ring (for a specific queue) to setup
7315ec8b7d1SJesse Brandeburg  *
7325ec8b7d1SJesse Brandeburg  * Returns 0 on success, negative on failure
7335ec8b7d1SJesse Brandeburg  **/
73456184e01SJesse Brandeburg int iavf_setup_rx_descriptors(struct iavf_ring *rx_ring)
7355ec8b7d1SJesse Brandeburg {
7365ec8b7d1SJesse Brandeburg 	struct device *dev = rx_ring->dev;
7375ec8b7d1SJesse Brandeburg 	int bi_size;
7385ec8b7d1SJesse Brandeburg 
7395ec8b7d1SJesse Brandeburg 	/* warn if we are about to overwrite the pointer */
7405ec8b7d1SJesse Brandeburg 	WARN_ON(rx_ring->rx_bi);
74156184e01SJesse Brandeburg 	bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count;
7425ec8b7d1SJesse Brandeburg 	rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
7435ec8b7d1SJesse Brandeburg 	if (!rx_ring->rx_bi)
7445ec8b7d1SJesse Brandeburg 		goto err;
7455ec8b7d1SJesse Brandeburg 
7465ec8b7d1SJesse Brandeburg 	u64_stats_init(&rx_ring->syncp);
7475ec8b7d1SJesse Brandeburg 
7485ec8b7d1SJesse Brandeburg 	/* Round up to nearest 4K */
74956184e01SJesse Brandeburg 	rx_ring->size = rx_ring->count * sizeof(union iavf_32byte_rx_desc);
7505ec8b7d1SJesse Brandeburg 	rx_ring->size = ALIGN(rx_ring->size, 4096);
7515ec8b7d1SJesse Brandeburg 	rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
7525ec8b7d1SJesse Brandeburg 					   &rx_ring->dma, GFP_KERNEL);
7535ec8b7d1SJesse Brandeburg 
7545ec8b7d1SJesse Brandeburg 	if (!rx_ring->desc) {
7555ec8b7d1SJesse Brandeburg 		dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
7565ec8b7d1SJesse Brandeburg 			 rx_ring->size);
7575ec8b7d1SJesse Brandeburg 		goto err;
7585ec8b7d1SJesse Brandeburg 	}
7595ec8b7d1SJesse Brandeburg 
7605ec8b7d1SJesse Brandeburg 	rx_ring->next_to_alloc = 0;
7615ec8b7d1SJesse Brandeburg 	rx_ring->next_to_clean = 0;
7625ec8b7d1SJesse Brandeburg 	rx_ring->next_to_use = 0;
7635ec8b7d1SJesse Brandeburg 
7645ec8b7d1SJesse Brandeburg 	return 0;
7655ec8b7d1SJesse Brandeburg err:
7665ec8b7d1SJesse Brandeburg 	kfree(rx_ring->rx_bi);
7675ec8b7d1SJesse Brandeburg 	rx_ring->rx_bi = NULL;
7685ec8b7d1SJesse Brandeburg 	return -ENOMEM;
7695ec8b7d1SJesse Brandeburg }
7705ec8b7d1SJesse Brandeburg 
7715ec8b7d1SJesse Brandeburg /**
77256184e01SJesse Brandeburg  * iavf_release_rx_desc - Store the new tail and head values
7735ec8b7d1SJesse Brandeburg  * @rx_ring: ring to bump
7745ec8b7d1SJesse Brandeburg  * @val: new head index
7755ec8b7d1SJesse Brandeburg  **/
77656184e01SJesse Brandeburg static inline void iavf_release_rx_desc(struct iavf_ring *rx_ring, u32 val)
7775ec8b7d1SJesse Brandeburg {
7785ec8b7d1SJesse Brandeburg 	rx_ring->next_to_use = val;
7795ec8b7d1SJesse Brandeburg 
7805ec8b7d1SJesse Brandeburg 	/* update next to alloc since we have filled the ring */
7815ec8b7d1SJesse Brandeburg 	rx_ring->next_to_alloc = val;
7825ec8b7d1SJesse Brandeburg 
7835ec8b7d1SJesse Brandeburg 	/* Force memory writes to complete before letting h/w
7845ec8b7d1SJesse Brandeburg 	 * know there are new descriptors to fetch.  (Only
7855ec8b7d1SJesse Brandeburg 	 * applicable for weak-ordered memory model archs,
7865ec8b7d1SJesse Brandeburg 	 * such as IA-64).
7875ec8b7d1SJesse Brandeburg 	 */
7885ec8b7d1SJesse Brandeburg 	wmb();
7895ec8b7d1SJesse Brandeburg 	writel(val, rx_ring->tail);
7905ec8b7d1SJesse Brandeburg }
7915ec8b7d1SJesse Brandeburg 
7925ec8b7d1SJesse Brandeburg /**
79356184e01SJesse Brandeburg  * iavf_rx_offset - Return expected offset into page to access data
7945ec8b7d1SJesse Brandeburg  * @rx_ring: Ring we are requesting offset of
7955ec8b7d1SJesse Brandeburg  *
7965ec8b7d1SJesse Brandeburg  * Returns the offset value for ring into the data buffer.
7975ec8b7d1SJesse Brandeburg  */
79856184e01SJesse Brandeburg static inline unsigned int iavf_rx_offset(struct iavf_ring *rx_ring)
7995ec8b7d1SJesse Brandeburg {
80056184e01SJesse Brandeburg 	return ring_uses_build_skb(rx_ring) ? IAVF_SKB_PAD : 0;
8015ec8b7d1SJesse Brandeburg }
8025ec8b7d1SJesse Brandeburg 
8035ec8b7d1SJesse Brandeburg /**
80456184e01SJesse Brandeburg  * iavf_alloc_mapped_page - recycle or make a new page
8055ec8b7d1SJesse Brandeburg  * @rx_ring: ring to use
8065ec8b7d1SJesse Brandeburg  * @bi: rx_buffer struct to modify
8075ec8b7d1SJesse Brandeburg  *
8085ec8b7d1SJesse Brandeburg  * Returns true if the page was successfully allocated or
8095ec8b7d1SJesse Brandeburg  * reused.
8105ec8b7d1SJesse Brandeburg  **/
81156184e01SJesse Brandeburg static bool iavf_alloc_mapped_page(struct iavf_ring *rx_ring,
81256184e01SJesse Brandeburg 				   struct iavf_rx_buffer *bi)
8135ec8b7d1SJesse Brandeburg {
8145ec8b7d1SJesse Brandeburg 	struct page *page = bi->page;
8155ec8b7d1SJesse Brandeburg 	dma_addr_t dma;
8165ec8b7d1SJesse Brandeburg 
8175ec8b7d1SJesse Brandeburg 	/* since we are recycling buffers we should seldom need to alloc */
8185ec8b7d1SJesse Brandeburg 	if (likely(page)) {
8195ec8b7d1SJesse Brandeburg 		rx_ring->rx_stats.page_reuse_count++;
8205ec8b7d1SJesse Brandeburg 		return true;
8215ec8b7d1SJesse Brandeburg 	}
8225ec8b7d1SJesse Brandeburg 
8235ec8b7d1SJesse Brandeburg 	/* alloc new page for storage */
82456184e01SJesse Brandeburg 	page = dev_alloc_pages(iavf_rx_pg_order(rx_ring));
8255ec8b7d1SJesse Brandeburg 	if (unlikely(!page)) {
8265ec8b7d1SJesse Brandeburg 		rx_ring->rx_stats.alloc_page_failed++;
8275ec8b7d1SJesse Brandeburg 		return false;
8285ec8b7d1SJesse Brandeburg 	}
8295ec8b7d1SJesse Brandeburg 
8305ec8b7d1SJesse Brandeburg 	/* map page for use */
8315ec8b7d1SJesse Brandeburg 	dma = dma_map_page_attrs(rx_ring->dev, page, 0,
83256184e01SJesse Brandeburg 				 iavf_rx_pg_size(rx_ring),
8335ec8b7d1SJesse Brandeburg 				 DMA_FROM_DEVICE,
83456184e01SJesse Brandeburg 				 IAVF_RX_DMA_ATTR);
8355ec8b7d1SJesse Brandeburg 
8365ec8b7d1SJesse Brandeburg 	/* if mapping failed free memory back to system since
8375ec8b7d1SJesse Brandeburg 	 * there isn't much point in holding memory we can't use
8385ec8b7d1SJesse Brandeburg 	 */
8395ec8b7d1SJesse Brandeburg 	if (dma_mapping_error(rx_ring->dev, dma)) {
84056184e01SJesse Brandeburg 		__free_pages(page, iavf_rx_pg_order(rx_ring));
8415ec8b7d1SJesse Brandeburg 		rx_ring->rx_stats.alloc_page_failed++;
8425ec8b7d1SJesse Brandeburg 		return false;
8435ec8b7d1SJesse Brandeburg 	}
8445ec8b7d1SJesse Brandeburg 
8455ec8b7d1SJesse Brandeburg 	bi->dma = dma;
8465ec8b7d1SJesse Brandeburg 	bi->page = page;
84756184e01SJesse Brandeburg 	bi->page_offset = iavf_rx_offset(rx_ring);
8485ec8b7d1SJesse Brandeburg 
8495ec8b7d1SJesse Brandeburg 	/* initialize pagecnt_bias to 1 representing we fully own page */
8505ec8b7d1SJesse Brandeburg 	bi->pagecnt_bias = 1;
8515ec8b7d1SJesse Brandeburg 
8525ec8b7d1SJesse Brandeburg 	return true;
8535ec8b7d1SJesse Brandeburg }
8545ec8b7d1SJesse Brandeburg 
8555ec8b7d1SJesse Brandeburg /**
85656184e01SJesse Brandeburg  * iavf_receive_skb - Send a completed packet up the stack
8575ec8b7d1SJesse Brandeburg  * @rx_ring:  rx ring in play
8585ec8b7d1SJesse Brandeburg  * @skb: packet to send up
8595ec8b7d1SJesse Brandeburg  * @vlan_tag: vlan tag for packet
8605ec8b7d1SJesse Brandeburg  **/
86156184e01SJesse Brandeburg static void iavf_receive_skb(struct iavf_ring *rx_ring,
8625ec8b7d1SJesse Brandeburg 			     struct sk_buff *skb, u16 vlan_tag)
8635ec8b7d1SJesse Brandeburg {
86456184e01SJesse Brandeburg 	struct iavf_q_vector *q_vector = rx_ring->q_vector;
8655ec8b7d1SJesse Brandeburg 
8665ec8b7d1SJesse Brandeburg 	if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
8675ec8b7d1SJesse Brandeburg 	    (vlan_tag & VLAN_VID_MASK))
8685ec8b7d1SJesse Brandeburg 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
8695ec8b7d1SJesse Brandeburg 
8705ec8b7d1SJesse Brandeburg 	napi_gro_receive(&q_vector->napi, skb);
8715ec8b7d1SJesse Brandeburg }
8725ec8b7d1SJesse Brandeburg 
8735ec8b7d1SJesse Brandeburg /**
8745ec8b7d1SJesse Brandeburg  * iavf_alloc_rx_buffers - Replace used receive buffers
8755ec8b7d1SJesse Brandeburg  * @rx_ring: ring to place buffers on
8765ec8b7d1SJesse Brandeburg  * @cleaned_count: number of buffers to replace
8775ec8b7d1SJesse Brandeburg  *
8785ec8b7d1SJesse Brandeburg  * Returns false if all allocations were successful, true if any fail
8795ec8b7d1SJesse Brandeburg  **/
88056184e01SJesse Brandeburg bool iavf_alloc_rx_buffers(struct iavf_ring *rx_ring, u16 cleaned_count)
8815ec8b7d1SJesse Brandeburg {
8825ec8b7d1SJesse Brandeburg 	u16 ntu = rx_ring->next_to_use;
88356184e01SJesse Brandeburg 	union iavf_rx_desc *rx_desc;
88456184e01SJesse Brandeburg 	struct iavf_rx_buffer *bi;
8855ec8b7d1SJesse Brandeburg 
8865ec8b7d1SJesse Brandeburg 	/* do nothing if no valid netdev defined */
8875ec8b7d1SJesse Brandeburg 	if (!rx_ring->netdev || !cleaned_count)
8885ec8b7d1SJesse Brandeburg 		return false;
8895ec8b7d1SJesse Brandeburg 
890f1cad2ceSJesse Brandeburg 	rx_desc = IAVF_RX_DESC(rx_ring, ntu);
8915ec8b7d1SJesse Brandeburg 	bi = &rx_ring->rx_bi[ntu];
8925ec8b7d1SJesse Brandeburg 
8935ec8b7d1SJesse Brandeburg 	do {
89456184e01SJesse Brandeburg 		if (!iavf_alloc_mapped_page(rx_ring, bi))
8955ec8b7d1SJesse Brandeburg 			goto no_buffers;
8965ec8b7d1SJesse Brandeburg 
8975ec8b7d1SJesse Brandeburg 		/* sync the buffer for use by the device */
8985ec8b7d1SJesse Brandeburg 		dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
8995ec8b7d1SJesse Brandeburg 						 bi->page_offset,
9005ec8b7d1SJesse Brandeburg 						 rx_ring->rx_buf_len,
9015ec8b7d1SJesse Brandeburg 						 DMA_FROM_DEVICE);
9025ec8b7d1SJesse Brandeburg 
9035ec8b7d1SJesse Brandeburg 		/* Refresh the desc even if buffer_addrs didn't change
9045ec8b7d1SJesse Brandeburg 		 * because each write-back erases this info.
9055ec8b7d1SJesse Brandeburg 		 */
9065ec8b7d1SJesse Brandeburg 		rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
9075ec8b7d1SJesse Brandeburg 
9085ec8b7d1SJesse Brandeburg 		rx_desc++;
9095ec8b7d1SJesse Brandeburg 		bi++;
9105ec8b7d1SJesse Brandeburg 		ntu++;
9115ec8b7d1SJesse Brandeburg 		if (unlikely(ntu == rx_ring->count)) {
912f1cad2ceSJesse Brandeburg 			rx_desc = IAVF_RX_DESC(rx_ring, 0);
9135ec8b7d1SJesse Brandeburg 			bi = rx_ring->rx_bi;
9145ec8b7d1SJesse Brandeburg 			ntu = 0;
9155ec8b7d1SJesse Brandeburg 		}
9165ec8b7d1SJesse Brandeburg 
9175ec8b7d1SJesse Brandeburg 		/* clear the status bits for the next_to_use descriptor */
9185ec8b7d1SJesse Brandeburg 		rx_desc->wb.qword1.status_error_len = 0;
9195ec8b7d1SJesse Brandeburg 
9205ec8b7d1SJesse Brandeburg 		cleaned_count--;
9215ec8b7d1SJesse Brandeburg 	} while (cleaned_count);
9225ec8b7d1SJesse Brandeburg 
9235ec8b7d1SJesse Brandeburg 	if (rx_ring->next_to_use != ntu)
92456184e01SJesse Brandeburg 		iavf_release_rx_desc(rx_ring, ntu);
9255ec8b7d1SJesse Brandeburg 
9265ec8b7d1SJesse Brandeburg 	return false;
9275ec8b7d1SJesse Brandeburg 
9285ec8b7d1SJesse Brandeburg no_buffers:
9295ec8b7d1SJesse Brandeburg 	if (rx_ring->next_to_use != ntu)
93056184e01SJesse Brandeburg 		iavf_release_rx_desc(rx_ring, ntu);
9315ec8b7d1SJesse Brandeburg 
9325ec8b7d1SJesse Brandeburg 	/* make sure to come back via polling to try again after
9335ec8b7d1SJesse Brandeburg 	 * allocation failure
9345ec8b7d1SJesse Brandeburg 	 */
9355ec8b7d1SJesse Brandeburg 	return true;
9365ec8b7d1SJesse Brandeburg }
9375ec8b7d1SJesse Brandeburg 
9385ec8b7d1SJesse Brandeburg /**
93956184e01SJesse Brandeburg  * iavf_rx_checksum - Indicate in skb if hw indicated a good cksum
9405ec8b7d1SJesse Brandeburg  * @vsi: the VSI we care about
9415ec8b7d1SJesse Brandeburg  * @skb: skb currently being received and modified
9425ec8b7d1SJesse Brandeburg  * @rx_desc: the receive descriptor
9435ec8b7d1SJesse Brandeburg  **/
94456184e01SJesse Brandeburg static inline void iavf_rx_checksum(struct iavf_vsi *vsi,
9455ec8b7d1SJesse Brandeburg 				    struct sk_buff *skb,
94656184e01SJesse Brandeburg 				    union iavf_rx_desc *rx_desc)
9475ec8b7d1SJesse Brandeburg {
94856184e01SJesse Brandeburg 	struct iavf_rx_ptype_decoded decoded;
9495ec8b7d1SJesse Brandeburg 	u32 rx_error, rx_status;
9505ec8b7d1SJesse Brandeburg 	bool ipv4, ipv6;
9515ec8b7d1SJesse Brandeburg 	u8 ptype;
9525ec8b7d1SJesse Brandeburg 	u64 qword;
9535ec8b7d1SJesse Brandeburg 
9545ec8b7d1SJesse Brandeburg 	qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
95556184e01SJesse Brandeburg 	ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >> IAVF_RXD_QW1_PTYPE_SHIFT;
95656184e01SJesse Brandeburg 	rx_error = (qword & IAVF_RXD_QW1_ERROR_MASK) >>
95756184e01SJesse Brandeburg 		   IAVF_RXD_QW1_ERROR_SHIFT;
95856184e01SJesse Brandeburg 	rx_status = (qword & IAVF_RXD_QW1_STATUS_MASK) >>
95956184e01SJesse Brandeburg 		    IAVF_RXD_QW1_STATUS_SHIFT;
9605ec8b7d1SJesse Brandeburg 	decoded = decode_rx_desc_ptype(ptype);
9615ec8b7d1SJesse Brandeburg 
9625ec8b7d1SJesse Brandeburg 	skb->ip_summed = CHECKSUM_NONE;
9635ec8b7d1SJesse Brandeburg 
9645ec8b7d1SJesse Brandeburg 	skb_checksum_none_assert(skb);
9655ec8b7d1SJesse Brandeburg 
9665ec8b7d1SJesse Brandeburg 	/* Rx csum enabled and ip headers found? */
9675ec8b7d1SJesse Brandeburg 	if (!(vsi->netdev->features & NETIF_F_RXCSUM))
9685ec8b7d1SJesse Brandeburg 		return;
9695ec8b7d1SJesse Brandeburg 
9705ec8b7d1SJesse Brandeburg 	/* did the hardware decode the packet and checksum? */
971f1cad2ceSJesse Brandeburg 	if (!(rx_status & BIT(IAVF_RX_DESC_STATUS_L3L4P_SHIFT)))
9725ec8b7d1SJesse Brandeburg 		return;
9735ec8b7d1SJesse Brandeburg 
9745ec8b7d1SJesse Brandeburg 	/* both known and outer_ip must be set for the below code to work */
9755ec8b7d1SJesse Brandeburg 	if (!(decoded.known && decoded.outer_ip))
9765ec8b7d1SJesse Brandeburg 		return;
9775ec8b7d1SJesse Brandeburg 
97856184e01SJesse Brandeburg 	ipv4 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) &&
97956184e01SJesse Brandeburg 	       (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV4);
98056184e01SJesse Brandeburg 	ipv6 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) &&
98156184e01SJesse Brandeburg 	       (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV6);
9825ec8b7d1SJesse Brandeburg 
9835ec8b7d1SJesse Brandeburg 	if (ipv4 &&
984f1cad2ceSJesse Brandeburg 	    (rx_error & (BIT(IAVF_RX_DESC_ERROR_IPE_SHIFT) |
985f1cad2ceSJesse Brandeburg 			 BIT(IAVF_RX_DESC_ERROR_EIPE_SHIFT))))
9865ec8b7d1SJesse Brandeburg 		goto checksum_fail;
9875ec8b7d1SJesse Brandeburg 
9885ec8b7d1SJesse Brandeburg 	/* likely incorrect csum if alternate IP extension headers found */
9895ec8b7d1SJesse Brandeburg 	if (ipv6 &&
990f1cad2ceSJesse Brandeburg 	    rx_status & BIT(IAVF_RX_DESC_STATUS_IPV6EXADD_SHIFT))
9915ec8b7d1SJesse Brandeburg 		/* don't increment checksum err here, non-fatal err */
9925ec8b7d1SJesse Brandeburg 		return;
9935ec8b7d1SJesse Brandeburg 
9945ec8b7d1SJesse Brandeburg 	/* there was some L4 error, count error and punt packet to the stack */
995f1cad2ceSJesse Brandeburg 	if (rx_error & BIT(IAVF_RX_DESC_ERROR_L4E_SHIFT))
9965ec8b7d1SJesse Brandeburg 		goto checksum_fail;
9975ec8b7d1SJesse Brandeburg 
9985ec8b7d1SJesse Brandeburg 	/* handle packets that were not able to be checksummed due
9995ec8b7d1SJesse Brandeburg 	 * to arrival speed, in this case the stack can compute
10005ec8b7d1SJesse Brandeburg 	 * the csum.
10015ec8b7d1SJesse Brandeburg 	 */
1002f1cad2ceSJesse Brandeburg 	if (rx_error & BIT(IAVF_RX_DESC_ERROR_PPRS_SHIFT))
10035ec8b7d1SJesse Brandeburg 		return;
10045ec8b7d1SJesse Brandeburg 
10055ec8b7d1SJesse Brandeburg 	/* Only report checksum unnecessary for TCP, UDP, or SCTP */
10065ec8b7d1SJesse Brandeburg 	switch (decoded.inner_prot) {
100756184e01SJesse Brandeburg 	case IAVF_RX_PTYPE_INNER_PROT_TCP:
100856184e01SJesse Brandeburg 	case IAVF_RX_PTYPE_INNER_PROT_UDP:
100956184e01SJesse Brandeburg 	case IAVF_RX_PTYPE_INNER_PROT_SCTP:
10105ec8b7d1SJesse Brandeburg 		skb->ip_summed = CHECKSUM_UNNECESSARY;
10115ec8b7d1SJesse Brandeburg 		/* fall though */
10125ec8b7d1SJesse Brandeburg 	default:
10135ec8b7d1SJesse Brandeburg 		break;
10145ec8b7d1SJesse Brandeburg 	}
10155ec8b7d1SJesse Brandeburg 
10165ec8b7d1SJesse Brandeburg 	return;
10175ec8b7d1SJesse Brandeburg 
10185ec8b7d1SJesse Brandeburg checksum_fail:
10195ec8b7d1SJesse Brandeburg 	vsi->back->hw_csum_rx_error++;
10205ec8b7d1SJesse Brandeburg }
10215ec8b7d1SJesse Brandeburg 
10225ec8b7d1SJesse Brandeburg /**
102356184e01SJesse Brandeburg  * iavf_ptype_to_htype - get a hash type
10245ec8b7d1SJesse Brandeburg  * @ptype: the ptype value from the descriptor
10255ec8b7d1SJesse Brandeburg  *
10265ec8b7d1SJesse Brandeburg  * Returns a hash type to be used by skb_set_hash
10275ec8b7d1SJesse Brandeburg  **/
102856184e01SJesse Brandeburg static inline int iavf_ptype_to_htype(u8 ptype)
10295ec8b7d1SJesse Brandeburg {
103056184e01SJesse Brandeburg 	struct iavf_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
10315ec8b7d1SJesse Brandeburg 
10325ec8b7d1SJesse Brandeburg 	if (!decoded.known)
10335ec8b7d1SJesse Brandeburg 		return PKT_HASH_TYPE_NONE;
10345ec8b7d1SJesse Brandeburg 
103556184e01SJesse Brandeburg 	if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP &&
103656184e01SJesse Brandeburg 	    decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY4)
10375ec8b7d1SJesse Brandeburg 		return PKT_HASH_TYPE_L4;
103856184e01SJesse Brandeburg 	else if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP &&
103956184e01SJesse Brandeburg 		 decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY3)
10405ec8b7d1SJesse Brandeburg 		return PKT_HASH_TYPE_L3;
10415ec8b7d1SJesse Brandeburg 	else
10425ec8b7d1SJesse Brandeburg 		return PKT_HASH_TYPE_L2;
10435ec8b7d1SJesse Brandeburg }
10445ec8b7d1SJesse Brandeburg 
10455ec8b7d1SJesse Brandeburg /**
104656184e01SJesse Brandeburg  * iavf_rx_hash - set the hash value in the skb
10475ec8b7d1SJesse Brandeburg  * @ring: descriptor ring
10485ec8b7d1SJesse Brandeburg  * @rx_desc: specific descriptor
10495ec8b7d1SJesse Brandeburg  * @skb: skb currently being received and modified
10505ec8b7d1SJesse Brandeburg  * @rx_ptype: Rx packet type
10515ec8b7d1SJesse Brandeburg  **/
105256184e01SJesse Brandeburg static inline void iavf_rx_hash(struct iavf_ring *ring,
105356184e01SJesse Brandeburg 				union iavf_rx_desc *rx_desc,
10545ec8b7d1SJesse Brandeburg 				struct sk_buff *skb,
10555ec8b7d1SJesse Brandeburg 				u8 rx_ptype)
10565ec8b7d1SJesse Brandeburg {
10575ec8b7d1SJesse Brandeburg 	u32 hash;
10585ec8b7d1SJesse Brandeburg 	const __le64 rss_mask =
1059f1cad2ceSJesse Brandeburg 		cpu_to_le64((u64)IAVF_RX_DESC_FLTSTAT_RSS_HASH <<
1060f1cad2ceSJesse Brandeburg 			    IAVF_RX_DESC_STATUS_FLTSTAT_SHIFT);
10615ec8b7d1SJesse Brandeburg 
10625ec8b7d1SJesse Brandeburg 	if (ring->netdev->features & NETIF_F_RXHASH)
10635ec8b7d1SJesse Brandeburg 		return;
10645ec8b7d1SJesse Brandeburg 
10655ec8b7d1SJesse Brandeburg 	if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
10665ec8b7d1SJesse Brandeburg 		hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
106756184e01SJesse Brandeburg 		skb_set_hash(skb, hash, iavf_ptype_to_htype(rx_ptype));
10685ec8b7d1SJesse Brandeburg 	}
10695ec8b7d1SJesse Brandeburg }
10705ec8b7d1SJesse Brandeburg 
10715ec8b7d1SJesse Brandeburg /**
10725ec8b7d1SJesse Brandeburg  * iavf_process_skb_fields - Populate skb header fields from Rx descriptor
10735ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring packet is being transacted on
10745ec8b7d1SJesse Brandeburg  * @rx_desc: pointer to the EOP Rx descriptor
10755ec8b7d1SJesse Brandeburg  * @skb: pointer to current skb being populated
10765ec8b7d1SJesse Brandeburg  * @rx_ptype: the packet type decoded by hardware
10775ec8b7d1SJesse Brandeburg  *
10785ec8b7d1SJesse Brandeburg  * This function checks the ring, descriptor, and packet information in
10795ec8b7d1SJesse Brandeburg  * order to populate the hash, checksum, VLAN, protocol, and
10805ec8b7d1SJesse Brandeburg  * other fields within the skb.
10815ec8b7d1SJesse Brandeburg  **/
10825ec8b7d1SJesse Brandeburg static inline
108356184e01SJesse Brandeburg void iavf_process_skb_fields(struct iavf_ring *rx_ring,
108456184e01SJesse Brandeburg 			     union iavf_rx_desc *rx_desc, struct sk_buff *skb,
10855ec8b7d1SJesse Brandeburg 			     u8 rx_ptype)
10865ec8b7d1SJesse Brandeburg {
108756184e01SJesse Brandeburg 	iavf_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
10885ec8b7d1SJesse Brandeburg 
108956184e01SJesse Brandeburg 	iavf_rx_checksum(rx_ring->vsi, skb, rx_desc);
10905ec8b7d1SJesse Brandeburg 
10915ec8b7d1SJesse Brandeburg 	skb_record_rx_queue(skb, rx_ring->queue_index);
10925ec8b7d1SJesse Brandeburg 
10935ec8b7d1SJesse Brandeburg 	/* modifies the skb - consumes the enet header */
10945ec8b7d1SJesse Brandeburg 	skb->protocol = eth_type_trans(skb, rx_ring->netdev);
10955ec8b7d1SJesse Brandeburg }
10965ec8b7d1SJesse Brandeburg 
10975ec8b7d1SJesse Brandeburg /**
109856184e01SJesse Brandeburg  * iavf_cleanup_headers - Correct empty headers
10995ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring packet is being transacted on
11005ec8b7d1SJesse Brandeburg  * @skb: pointer to current skb being fixed
11015ec8b7d1SJesse Brandeburg  *
11025ec8b7d1SJesse Brandeburg  * Also address the case where we are pulling data in on pages only
11035ec8b7d1SJesse Brandeburg  * and as such no data is present in the skb header.
11045ec8b7d1SJesse Brandeburg  *
11055ec8b7d1SJesse Brandeburg  * In addition if skb is not at least 60 bytes we need to pad it so that
11065ec8b7d1SJesse Brandeburg  * it is large enough to qualify as a valid Ethernet frame.
11075ec8b7d1SJesse Brandeburg  *
11085ec8b7d1SJesse Brandeburg  * Returns true if an error was encountered and skb was freed.
11095ec8b7d1SJesse Brandeburg  **/
111056184e01SJesse Brandeburg static bool iavf_cleanup_headers(struct iavf_ring *rx_ring, struct sk_buff *skb)
11115ec8b7d1SJesse Brandeburg {
11125ec8b7d1SJesse Brandeburg 	/* if eth_skb_pad returns an error the skb was freed */
11135ec8b7d1SJesse Brandeburg 	if (eth_skb_pad(skb))
11145ec8b7d1SJesse Brandeburg 		return true;
11155ec8b7d1SJesse Brandeburg 
11165ec8b7d1SJesse Brandeburg 	return false;
11175ec8b7d1SJesse Brandeburg }
11185ec8b7d1SJesse Brandeburg 
11195ec8b7d1SJesse Brandeburg /**
112056184e01SJesse Brandeburg  * iavf_reuse_rx_page - page flip buffer and store it back on the ring
11215ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring to store buffers on
11225ec8b7d1SJesse Brandeburg  * @old_buff: donor buffer to have page reused
11235ec8b7d1SJesse Brandeburg  *
11245ec8b7d1SJesse Brandeburg  * Synchronizes page for reuse by the adapter
11255ec8b7d1SJesse Brandeburg  **/
112656184e01SJesse Brandeburg static void iavf_reuse_rx_page(struct iavf_ring *rx_ring,
112756184e01SJesse Brandeburg 			       struct iavf_rx_buffer *old_buff)
11285ec8b7d1SJesse Brandeburg {
112956184e01SJesse Brandeburg 	struct iavf_rx_buffer *new_buff;
11305ec8b7d1SJesse Brandeburg 	u16 nta = rx_ring->next_to_alloc;
11315ec8b7d1SJesse Brandeburg 
11325ec8b7d1SJesse Brandeburg 	new_buff = &rx_ring->rx_bi[nta];
11335ec8b7d1SJesse Brandeburg 
11345ec8b7d1SJesse Brandeburg 	/* update, and store next to alloc */
11355ec8b7d1SJesse Brandeburg 	nta++;
11365ec8b7d1SJesse Brandeburg 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
11375ec8b7d1SJesse Brandeburg 
11385ec8b7d1SJesse Brandeburg 	/* transfer page from old buffer to new buffer */
11395ec8b7d1SJesse Brandeburg 	new_buff->dma		= old_buff->dma;
11405ec8b7d1SJesse Brandeburg 	new_buff->page		= old_buff->page;
11415ec8b7d1SJesse Brandeburg 	new_buff->page_offset	= old_buff->page_offset;
11425ec8b7d1SJesse Brandeburg 	new_buff->pagecnt_bias	= old_buff->pagecnt_bias;
11435ec8b7d1SJesse Brandeburg }
11445ec8b7d1SJesse Brandeburg 
11455ec8b7d1SJesse Brandeburg /**
114656184e01SJesse Brandeburg  * iavf_page_is_reusable - check if any reuse is possible
11475ec8b7d1SJesse Brandeburg  * @page: page struct to check
11485ec8b7d1SJesse Brandeburg  *
11495ec8b7d1SJesse Brandeburg  * A page is not reusable if it was allocated under low memory
11505ec8b7d1SJesse Brandeburg  * conditions, or it's not in the same NUMA node as this CPU.
11515ec8b7d1SJesse Brandeburg  */
115256184e01SJesse Brandeburg static inline bool iavf_page_is_reusable(struct page *page)
11535ec8b7d1SJesse Brandeburg {
11545ec8b7d1SJesse Brandeburg 	return (page_to_nid(page) == numa_mem_id()) &&
11555ec8b7d1SJesse Brandeburg 		!page_is_pfmemalloc(page);
11565ec8b7d1SJesse Brandeburg }
11575ec8b7d1SJesse Brandeburg 
11585ec8b7d1SJesse Brandeburg /**
115956184e01SJesse Brandeburg  * iavf_can_reuse_rx_page - Determine if this page can be reused by
11605ec8b7d1SJesse Brandeburg  * the adapter for another receive
11615ec8b7d1SJesse Brandeburg  *
11625ec8b7d1SJesse Brandeburg  * @rx_buffer: buffer containing the page
11635ec8b7d1SJesse Brandeburg  *
11645ec8b7d1SJesse Brandeburg  * If page is reusable, rx_buffer->page_offset is adjusted to point to
11655ec8b7d1SJesse Brandeburg  * an unused region in the page.
11665ec8b7d1SJesse Brandeburg  *
11675ec8b7d1SJesse Brandeburg  * For small pages, @truesize will be a constant value, half the size
11685ec8b7d1SJesse Brandeburg  * of the memory at page.  We'll attempt to alternate between high and
11695ec8b7d1SJesse Brandeburg  * low halves of the page, with one half ready for use by the hardware
11705ec8b7d1SJesse Brandeburg  * and the other half being consumed by the stack.  We use the page
11715ec8b7d1SJesse Brandeburg  * ref count to determine whether the stack has finished consuming the
11725ec8b7d1SJesse Brandeburg  * portion of this page that was passed up with a previous packet.  If
11735ec8b7d1SJesse Brandeburg  * the page ref count is >1, we'll assume the "other" half page is
11745ec8b7d1SJesse Brandeburg  * still busy, and this page cannot be reused.
11755ec8b7d1SJesse Brandeburg  *
11765ec8b7d1SJesse Brandeburg  * For larger pages, @truesize will be the actual space used by the
11775ec8b7d1SJesse Brandeburg  * received packet (adjusted upward to an even multiple of the cache
11785ec8b7d1SJesse Brandeburg  * line size).  This will advance through the page by the amount
11795ec8b7d1SJesse Brandeburg  * actually consumed by the received packets while there is still
11805ec8b7d1SJesse Brandeburg  * space for a buffer.  Each region of larger pages will be used at
11815ec8b7d1SJesse Brandeburg  * most once, after which the page will not be reused.
11825ec8b7d1SJesse Brandeburg  *
11835ec8b7d1SJesse Brandeburg  * In either case, if the page is reusable its refcount is increased.
11845ec8b7d1SJesse Brandeburg  **/
118556184e01SJesse Brandeburg static bool iavf_can_reuse_rx_page(struct iavf_rx_buffer *rx_buffer)
11865ec8b7d1SJesse Brandeburg {
11875ec8b7d1SJesse Brandeburg 	unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
11885ec8b7d1SJesse Brandeburg 	struct page *page = rx_buffer->page;
11895ec8b7d1SJesse Brandeburg 
11905ec8b7d1SJesse Brandeburg 	/* Is any reuse possible? */
119156184e01SJesse Brandeburg 	if (unlikely(!iavf_page_is_reusable(page)))
11925ec8b7d1SJesse Brandeburg 		return false;
11935ec8b7d1SJesse Brandeburg 
11945ec8b7d1SJesse Brandeburg #if (PAGE_SIZE < 8192)
11955ec8b7d1SJesse Brandeburg 	/* if we are only owner of page we can reuse it */
11965ec8b7d1SJesse Brandeburg 	if (unlikely((page_count(page) - pagecnt_bias) > 1))
11975ec8b7d1SJesse Brandeburg 		return false;
11985ec8b7d1SJesse Brandeburg #else
119956184e01SJesse Brandeburg #define IAVF_LAST_OFFSET \
120056184e01SJesse Brandeburg 	(SKB_WITH_OVERHEAD(PAGE_SIZE) - IAVF_RXBUFFER_2048)
120156184e01SJesse Brandeburg 	if (rx_buffer->page_offset > IAVF_LAST_OFFSET)
12025ec8b7d1SJesse Brandeburg 		return false;
12035ec8b7d1SJesse Brandeburg #endif
12045ec8b7d1SJesse Brandeburg 
12055ec8b7d1SJesse Brandeburg 	/* If we have drained the page fragment pool we need to update
12065ec8b7d1SJesse Brandeburg 	 * the pagecnt_bias and page count so that we fully restock the
12075ec8b7d1SJesse Brandeburg 	 * number of references the driver holds.
12085ec8b7d1SJesse Brandeburg 	 */
12095ec8b7d1SJesse Brandeburg 	if (unlikely(!pagecnt_bias)) {
12105ec8b7d1SJesse Brandeburg 		page_ref_add(page, USHRT_MAX);
12115ec8b7d1SJesse Brandeburg 		rx_buffer->pagecnt_bias = USHRT_MAX;
12125ec8b7d1SJesse Brandeburg 	}
12135ec8b7d1SJesse Brandeburg 
12145ec8b7d1SJesse Brandeburg 	return true;
12155ec8b7d1SJesse Brandeburg }
12165ec8b7d1SJesse Brandeburg 
12175ec8b7d1SJesse Brandeburg /**
121856184e01SJesse Brandeburg  * iavf_add_rx_frag - Add contents of Rx buffer to sk_buff
12195ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
12205ec8b7d1SJesse Brandeburg  * @rx_buffer: buffer containing page to add
12215ec8b7d1SJesse Brandeburg  * @skb: sk_buff to place the data into
12225ec8b7d1SJesse Brandeburg  * @size: packet length from rx_desc
12235ec8b7d1SJesse Brandeburg  *
12245ec8b7d1SJesse Brandeburg  * This function will add the data contained in rx_buffer->page to the skb.
12255ec8b7d1SJesse Brandeburg  * It will just attach the page as a frag to the skb.
12265ec8b7d1SJesse Brandeburg  *
12275ec8b7d1SJesse Brandeburg  * The function will then update the page offset.
12285ec8b7d1SJesse Brandeburg  **/
122956184e01SJesse Brandeburg static void iavf_add_rx_frag(struct iavf_ring *rx_ring,
123056184e01SJesse Brandeburg 			     struct iavf_rx_buffer *rx_buffer,
12315ec8b7d1SJesse Brandeburg 			     struct sk_buff *skb,
12325ec8b7d1SJesse Brandeburg 			     unsigned int size)
12335ec8b7d1SJesse Brandeburg {
12345ec8b7d1SJesse Brandeburg #if (PAGE_SIZE < 8192)
123556184e01SJesse Brandeburg 	unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
12365ec8b7d1SJesse Brandeburg #else
123756184e01SJesse Brandeburg 	unsigned int truesize = SKB_DATA_ALIGN(size + iavf_rx_offset(rx_ring));
12385ec8b7d1SJesse Brandeburg #endif
12395ec8b7d1SJesse Brandeburg 
12405ec8b7d1SJesse Brandeburg 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
12415ec8b7d1SJesse Brandeburg 			rx_buffer->page_offset, size, truesize);
12425ec8b7d1SJesse Brandeburg 
12435ec8b7d1SJesse Brandeburg 	/* page is being used so we must update the page offset */
12445ec8b7d1SJesse Brandeburg #if (PAGE_SIZE < 8192)
12455ec8b7d1SJesse Brandeburg 	rx_buffer->page_offset ^= truesize;
12465ec8b7d1SJesse Brandeburg #else
12475ec8b7d1SJesse Brandeburg 	rx_buffer->page_offset += truesize;
12485ec8b7d1SJesse Brandeburg #endif
12495ec8b7d1SJesse Brandeburg }
12505ec8b7d1SJesse Brandeburg 
12515ec8b7d1SJesse Brandeburg /**
125256184e01SJesse Brandeburg  * iavf_get_rx_buffer - Fetch Rx buffer and synchronize data for use
12535ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
12545ec8b7d1SJesse Brandeburg  * @size: size of buffer to add to skb
12555ec8b7d1SJesse Brandeburg  *
12565ec8b7d1SJesse Brandeburg  * This function will pull an Rx buffer from the ring and synchronize it
12575ec8b7d1SJesse Brandeburg  * for use by the CPU.
12585ec8b7d1SJesse Brandeburg  */
125956184e01SJesse Brandeburg static struct iavf_rx_buffer *iavf_get_rx_buffer(struct iavf_ring *rx_ring,
12605ec8b7d1SJesse Brandeburg 						 const unsigned int size)
12615ec8b7d1SJesse Brandeburg {
126256184e01SJesse Brandeburg 	struct iavf_rx_buffer *rx_buffer;
12635ec8b7d1SJesse Brandeburg 
12645ec8b7d1SJesse Brandeburg 	rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
12655ec8b7d1SJesse Brandeburg 	prefetchw(rx_buffer->page);
12665ec8b7d1SJesse Brandeburg 
12675ec8b7d1SJesse Brandeburg 	/* we are reusing so sync this buffer for CPU use */
12685ec8b7d1SJesse Brandeburg 	dma_sync_single_range_for_cpu(rx_ring->dev,
12695ec8b7d1SJesse Brandeburg 				      rx_buffer->dma,
12705ec8b7d1SJesse Brandeburg 				      rx_buffer->page_offset,
12715ec8b7d1SJesse Brandeburg 				      size,
12725ec8b7d1SJesse Brandeburg 				      DMA_FROM_DEVICE);
12735ec8b7d1SJesse Brandeburg 
12745ec8b7d1SJesse Brandeburg 	/* We have pulled a buffer for use, so decrement pagecnt_bias */
12755ec8b7d1SJesse Brandeburg 	rx_buffer->pagecnt_bias--;
12765ec8b7d1SJesse Brandeburg 
12775ec8b7d1SJesse Brandeburg 	return rx_buffer;
12785ec8b7d1SJesse Brandeburg }
12795ec8b7d1SJesse Brandeburg 
12805ec8b7d1SJesse Brandeburg /**
128156184e01SJesse Brandeburg  * iavf_construct_skb - Allocate skb and populate it
12825ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
12835ec8b7d1SJesse Brandeburg  * @rx_buffer: rx buffer to pull data from
12845ec8b7d1SJesse Brandeburg  * @size: size of buffer to add to skb
12855ec8b7d1SJesse Brandeburg  *
12865ec8b7d1SJesse Brandeburg  * This function allocates an skb.  It then populates it with the page
12875ec8b7d1SJesse Brandeburg  * data from the current receive descriptor, taking care to set up the
12885ec8b7d1SJesse Brandeburg  * skb correctly.
12895ec8b7d1SJesse Brandeburg  */
129056184e01SJesse Brandeburg static struct sk_buff *iavf_construct_skb(struct iavf_ring *rx_ring,
129156184e01SJesse Brandeburg 					  struct iavf_rx_buffer *rx_buffer,
12925ec8b7d1SJesse Brandeburg 					  unsigned int size)
12935ec8b7d1SJesse Brandeburg {
12945ec8b7d1SJesse Brandeburg 	void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
12955ec8b7d1SJesse Brandeburg #if (PAGE_SIZE < 8192)
129656184e01SJesse Brandeburg 	unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
12975ec8b7d1SJesse Brandeburg #else
12985ec8b7d1SJesse Brandeburg 	unsigned int truesize = SKB_DATA_ALIGN(size);
12995ec8b7d1SJesse Brandeburg #endif
13005ec8b7d1SJesse Brandeburg 	unsigned int headlen;
13015ec8b7d1SJesse Brandeburg 	struct sk_buff *skb;
13025ec8b7d1SJesse Brandeburg 
13035ec8b7d1SJesse Brandeburg 	/* prefetch first cache line of first page */
13045ec8b7d1SJesse Brandeburg 	prefetch(va);
13055ec8b7d1SJesse Brandeburg #if L1_CACHE_BYTES < 128
13065ec8b7d1SJesse Brandeburg 	prefetch(va + L1_CACHE_BYTES);
13075ec8b7d1SJesse Brandeburg #endif
13085ec8b7d1SJesse Brandeburg 
13095ec8b7d1SJesse Brandeburg 	/* allocate a skb to store the frags */
13105ec8b7d1SJesse Brandeburg 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
131156184e01SJesse Brandeburg 			       IAVF_RX_HDR_SIZE,
13125ec8b7d1SJesse Brandeburg 			       GFP_ATOMIC | __GFP_NOWARN);
13135ec8b7d1SJesse Brandeburg 	if (unlikely(!skb))
13145ec8b7d1SJesse Brandeburg 		return NULL;
13155ec8b7d1SJesse Brandeburg 
13165ec8b7d1SJesse Brandeburg 	/* Determine available headroom for copy */
13175ec8b7d1SJesse Brandeburg 	headlen = size;
131856184e01SJesse Brandeburg 	if (headlen > IAVF_RX_HDR_SIZE)
131956184e01SJesse Brandeburg 		headlen = eth_get_headlen(va, IAVF_RX_HDR_SIZE);
13205ec8b7d1SJesse Brandeburg 
13215ec8b7d1SJesse Brandeburg 	/* align pull length to size of long to optimize memcpy performance */
13225ec8b7d1SJesse Brandeburg 	memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
13235ec8b7d1SJesse Brandeburg 
13245ec8b7d1SJesse Brandeburg 	/* update all of the pointers */
13255ec8b7d1SJesse Brandeburg 	size -= headlen;
13265ec8b7d1SJesse Brandeburg 	if (size) {
13275ec8b7d1SJesse Brandeburg 		skb_add_rx_frag(skb, 0, rx_buffer->page,
13285ec8b7d1SJesse Brandeburg 				rx_buffer->page_offset + headlen,
13295ec8b7d1SJesse Brandeburg 				size, truesize);
13305ec8b7d1SJesse Brandeburg 
13315ec8b7d1SJesse Brandeburg 		/* buffer is used by skb, update page_offset */
13325ec8b7d1SJesse Brandeburg #if (PAGE_SIZE < 8192)
13335ec8b7d1SJesse Brandeburg 		rx_buffer->page_offset ^= truesize;
13345ec8b7d1SJesse Brandeburg #else
13355ec8b7d1SJesse Brandeburg 		rx_buffer->page_offset += truesize;
13365ec8b7d1SJesse Brandeburg #endif
13375ec8b7d1SJesse Brandeburg 	} else {
13385ec8b7d1SJesse Brandeburg 		/* buffer is unused, reset bias back to rx_buffer */
13395ec8b7d1SJesse Brandeburg 		rx_buffer->pagecnt_bias++;
13405ec8b7d1SJesse Brandeburg 	}
13415ec8b7d1SJesse Brandeburg 
13425ec8b7d1SJesse Brandeburg 	return skb;
13435ec8b7d1SJesse Brandeburg }
13445ec8b7d1SJesse Brandeburg 
13455ec8b7d1SJesse Brandeburg /**
134656184e01SJesse Brandeburg  * iavf_build_skb - Build skb around an existing buffer
13475ec8b7d1SJesse Brandeburg  * @rx_ring: Rx descriptor ring to transact packets on
13485ec8b7d1SJesse Brandeburg  * @rx_buffer: Rx buffer to pull data from
13495ec8b7d1SJesse Brandeburg  * @size: size of buffer to add to skb
13505ec8b7d1SJesse Brandeburg  *
13515ec8b7d1SJesse Brandeburg  * This function builds an skb around an existing Rx buffer, taking care
13525ec8b7d1SJesse Brandeburg  * to set up the skb correctly and avoid any memcpy overhead.
13535ec8b7d1SJesse Brandeburg  */
135456184e01SJesse Brandeburg static struct sk_buff *iavf_build_skb(struct iavf_ring *rx_ring,
135556184e01SJesse Brandeburg 				      struct iavf_rx_buffer *rx_buffer,
13565ec8b7d1SJesse Brandeburg 				      unsigned int size)
13575ec8b7d1SJesse Brandeburg {
13585ec8b7d1SJesse Brandeburg 	void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
13595ec8b7d1SJesse Brandeburg #if (PAGE_SIZE < 8192)
136056184e01SJesse Brandeburg 	unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2;
13615ec8b7d1SJesse Brandeburg #else
13625ec8b7d1SJesse Brandeburg 	unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
136356184e01SJesse Brandeburg 				SKB_DATA_ALIGN(IAVF_SKB_PAD + size);
13645ec8b7d1SJesse Brandeburg #endif
13655ec8b7d1SJesse Brandeburg 	struct sk_buff *skb;
13665ec8b7d1SJesse Brandeburg 
13675ec8b7d1SJesse Brandeburg 	/* prefetch first cache line of first page */
13685ec8b7d1SJesse Brandeburg 	prefetch(va);
13695ec8b7d1SJesse Brandeburg #if L1_CACHE_BYTES < 128
13705ec8b7d1SJesse Brandeburg 	prefetch(va + L1_CACHE_BYTES);
13715ec8b7d1SJesse Brandeburg #endif
13725ec8b7d1SJesse Brandeburg 	/* build an skb around the page buffer */
137356184e01SJesse Brandeburg 	skb = build_skb(va - IAVF_SKB_PAD, truesize);
13745ec8b7d1SJesse Brandeburg 	if (unlikely(!skb))
13755ec8b7d1SJesse Brandeburg 		return NULL;
13765ec8b7d1SJesse Brandeburg 
13775ec8b7d1SJesse Brandeburg 	/* update pointers within the skb to store the data */
137856184e01SJesse Brandeburg 	skb_reserve(skb, IAVF_SKB_PAD);
13795ec8b7d1SJesse Brandeburg 	__skb_put(skb, size);
13805ec8b7d1SJesse Brandeburg 
13815ec8b7d1SJesse Brandeburg 	/* buffer is used by skb, update page_offset */
13825ec8b7d1SJesse Brandeburg #if (PAGE_SIZE < 8192)
13835ec8b7d1SJesse Brandeburg 	rx_buffer->page_offset ^= truesize;
13845ec8b7d1SJesse Brandeburg #else
13855ec8b7d1SJesse Brandeburg 	rx_buffer->page_offset += truesize;
13865ec8b7d1SJesse Brandeburg #endif
13875ec8b7d1SJesse Brandeburg 
13885ec8b7d1SJesse Brandeburg 	return skb;
13895ec8b7d1SJesse Brandeburg }
13905ec8b7d1SJesse Brandeburg 
13915ec8b7d1SJesse Brandeburg /**
139256184e01SJesse Brandeburg  * iavf_put_rx_buffer - Clean up used buffer and either recycle or free
13935ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
13945ec8b7d1SJesse Brandeburg  * @rx_buffer: rx buffer to pull data from
13955ec8b7d1SJesse Brandeburg  *
13965ec8b7d1SJesse Brandeburg  * This function will clean up the contents of the rx_buffer.  It will
13975ec8b7d1SJesse Brandeburg  * either recycle the buffer or unmap it and free the associated resources.
13985ec8b7d1SJesse Brandeburg  */
139956184e01SJesse Brandeburg static void iavf_put_rx_buffer(struct iavf_ring *rx_ring,
140056184e01SJesse Brandeburg 			       struct iavf_rx_buffer *rx_buffer)
14015ec8b7d1SJesse Brandeburg {
140256184e01SJesse Brandeburg 	if (iavf_can_reuse_rx_page(rx_buffer)) {
14035ec8b7d1SJesse Brandeburg 		/* hand second half of page back to the ring */
140456184e01SJesse Brandeburg 		iavf_reuse_rx_page(rx_ring, rx_buffer);
14055ec8b7d1SJesse Brandeburg 		rx_ring->rx_stats.page_reuse_count++;
14065ec8b7d1SJesse Brandeburg 	} else {
14075ec8b7d1SJesse Brandeburg 		/* we are not reusing the buffer so unmap it */
14085ec8b7d1SJesse Brandeburg 		dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
140956184e01SJesse Brandeburg 				     iavf_rx_pg_size(rx_ring),
141056184e01SJesse Brandeburg 				     DMA_FROM_DEVICE, IAVF_RX_DMA_ATTR);
14115ec8b7d1SJesse Brandeburg 		__page_frag_cache_drain(rx_buffer->page,
14125ec8b7d1SJesse Brandeburg 					rx_buffer->pagecnt_bias);
14135ec8b7d1SJesse Brandeburg 	}
14145ec8b7d1SJesse Brandeburg 
14155ec8b7d1SJesse Brandeburg 	/* clear contents of buffer_info */
14165ec8b7d1SJesse Brandeburg 	rx_buffer->page = NULL;
14175ec8b7d1SJesse Brandeburg }
14185ec8b7d1SJesse Brandeburg 
14195ec8b7d1SJesse Brandeburg /**
142056184e01SJesse Brandeburg  * iavf_is_non_eop - process handling of non-EOP buffers
14215ec8b7d1SJesse Brandeburg  * @rx_ring: Rx ring being processed
14225ec8b7d1SJesse Brandeburg  * @rx_desc: Rx descriptor for current buffer
14235ec8b7d1SJesse Brandeburg  * @skb: Current socket buffer containing buffer in progress
14245ec8b7d1SJesse Brandeburg  *
14255ec8b7d1SJesse Brandeburg  * This function updates next to clean.  If the buffer is an EOP buffer
14265ec8b7d1SJesse Brandeburg  * this function exits returning false, otherwise it will place the
14275ec8b7d1SJesse Brandeburg  * sk_buff in the next buffer to be chained and return true indicating
14285ec8b7d1SJesse Brandeburg  * that this is in fact a non-EOP buffer.
14295ec8b7d1SJesse Brandeburg  **/
143056184e01SJesse Brandeburg static bool iavf_is_non_eop(struct iavf_ring *rx_ring,
143156184e01SJesse Brandeburg 			    union iavf_rx_desc *rx_desc,
14325ec8b7d1SJesse Brandeburg 			    struct sk_buff *skb)
14335ec8b7d1SJesse Brandeburg {
14345ec8b7d1SJesse Brandeburg 	u32 ntc = rx_ring->next_to_clean + 1;
14355ec8b7d1SJesse Brandeburg 
14365ec8b7d1SJesse Brandeburg 	/* fetch, update, and store next to clean */
14375ec8b7d1SJesse Brandeburg 	ntc = (ntc < rx_ring->count) ? ntc : 0;
14385ec8b7d1SJesse Brandeburg 	rx_ring->next_to_clean = ntc;
14395ec8b7d1SJesse Brandeburg 
1440f1cad2ceSJesse Brandeburg 	prefetch(IAVF_RX_DESC(rx_ring, ntc));
14415ec8b7d1SJesse Brandeburg 
14425ec8b7d1SJesse Brandeburg 	/* if we are the last buffer then there is nothing else to do */
144356184e01SJesse Brandeburg #define IAVF_RXD_EOF BIT(IAVF_RX_DESC_STATUS_EOF_SHIFT)
144456184e01SJesse Brandeburg 	if (likely(iavf_test_staterr(rx_desc, IAVF_RXD_EOF)))
14455ec8b7d1SJesse Brandeburg 		return false;
14465ec8b7d1SJesse Brandeburg 
14475ec8b7d1SJesse Brandeburg 	rx_ring->rx_stats.non_eop_descs++;
14485ec8b7d1SJesse Brandeburg 
14495ec8b7d1SJesse Brandeburg 	return true;
14505ec8b7d1SJesse Brandeburg }
14515ec8b7d1SJesse Brandeburg 
14525ec8b7d1SJesse Brandeburg /**
145356184e01SJesse Brandeburg  * iavf_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
14545ec8b7d1SJesse Brandeburg  * @rx_ring: rx descriptor ring to transact packets on
14555ec8b7d1SJesse Brandeburg  * @budget: Total limit on number of packets to process
14565ec8b7d1SJesse Brandeburg  *
14575ec8b7d1SJesse Brandeburg  * This function provides a "bounce buffer" approach to Rx interrupt
14585ec8b7d1SJesse Brandeburg  * processing.  The advantage to this is that on systems that have
14595ec8b7d1SJesse Brandeburg  * expensive overhead for IOMMU access this provides a means of avoiding
14605ec8b7d1SJesse Brandeburg  * it by maintaining the mapping of the page to the system.
14615ec8b7d1SJesse Brandeburg  *
14625ec8b7d1SJesse Brandeburg  * Returns amount of work completed
14635ec8b7d1SJesse Brandeburg  **/
146456184e01SJesse Brandeburg static int iavf_clean_rx_irq(struct iavf_ring *rx_ring, int budget)
14655ec8b7d1SJesse Brandeburg {
14665ec8b7d1SJesse Brandeburg 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
14675ec8b7d1SJesse Brandeburg 	struct sk_buff *skb = rx_ring->skb;
146856184e01SJesse Brandeburg 	u16 cleaned_count = IAVF_DESC_UNUSED(rx_ring);
14695ec8b7d1SJesse Brandeburg 	bool failure = false;
14705ec8b7d1SJesse Brandeburg 
14715ec8b7d1SJesse Brandeburg 	while (likely(total_rx_packets < (unsigned int)budget)) {
147256184e01SJesse Brandeburg 		struct iavf_rx_buffer *rx_buffer;
147356184e01SJesse Brandeburg 		union iavf_rx_desc *rx_desc;
14745ec8b7d1SJesse Brandeburg 		unsigned int size;
14755ec8b7d1SJesse Brandeburg 		u16 vlan_tag;
14765ec8b7d1SJesse Brandeburg 		u8 rx_ptype;
14775ec8b7d1SJesse Brandeburg 		u64 qword;
14785ec8b7d1SJesse Brandeburg 
14795ec8b7d1SJesse Brandeburg 		/* return some buffers to hardware, one at a time is too slow */
148056184e01SJesse Brandeburg 		if (cleaned_count >= IAVF_RX_BUFFER_WRITE) {
14815ec8b7d1SJesse Brandeburg 			failure = failure ||
14825ec8b7d1SJesse Brandeburg 				  iavf_alloc_rx_buffers(rx_ring, cleaned_count);
14835ec8b7d1SJesse Brandeburg 			cleaned_count = 0;
14845ec8b7d1SJesse Brandeburg 		}
14855ec8b7d1SJesse Brandeburg 
1486f1cad2ceSJesse Brandeburg 		rx_desc = IAVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
14875ec8b7d1SJesse Brandeburg 
14885ec8b7d1SJesse Brandeburg 		/* status_error_len will always be zero for unused descriptors
14895ec8b7d1SJesse Brandeburg 		 * because it's cleared in cleanup, and overlaps with hdr_addr
14905ec8b7d1SJesse Brandeburg 		 * which is always zero because packet split isn't used, if the
14915ec8b7d1SJesse Brandeburg 		 * hardware wrote DD then the length will be non-zero
14925ec8b7d1SJesse Brandeburg 		 */
14935ec8b7d1SJesse Brandeburg 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
14945ec8b7d1SJesse Brandeburg 
14955ec8b7d1SJesse Brandeburg 		/* This memory barrier is needed to keep us from reading
14965ec8b7d1SJesse Brandeburg 		 * any other fields out of the rx_desc until we have
14975ec8b7d1SJesse Brandeburg 		 * verified the descriptor has been written back.
14985ec8b7d1SJesse Brandeburg 		 */
14995ec8b7d1SJesse Brandeburg 		dma_rmb();
15005ec8b7d1SJesse Brandeburg 
150156184e01SJesse Brandeburg 		size = (qword & IAVF_RXD_QW1_LENGTH_PBUF_MASK) >>
150256184e01SJesse Brandeburg 		       IAVF_RXD_QW1_LENGTH_PBUF_SHIFT;
15035ec8b7d1SJesse Brandeburg 		if (!size)
15045ec8b7d1SJesse Brandeburg 			break;
15055ec8b7d1SJesse Brandeburg 
1506ad64ed8bSJesse Brandeburg 		iavf_trace(clean_rx_irq, rx_ring, rx_desc, skb);
150756184e01SJesse Brandeburg 		rx_buffer = iavf_get_rx_buffer(rx_ring, size);
15085ec8b7d1SJesse Brandeburg 
15095ec8b7d1SJesse Brandeburg 		/* retrieve a buffer from the ring */
15105ec8b7d1SJesse Brandeburg 		if (skb)
151156184e01SJesse Brandeburg 			iavf_add_rx_frag(rx_ring, rx_buffer, skb, size);
15125ec8b7d1SJesse Brandeburg 		else if (ring_uses_build_skb(rx_ring))
151356184e01SJesse Brandeburg 			skb = iavf_build_skb(rx_ring, rx_buffer, size);
15145ec8b7d1SJesse Brandeburg 		else
151556184e01SJesse Brandeburg 			skb = iavf_construct_skb(rx_ring, rx_buffer, size);
15165ec8b7d1SJesse Brandeburg 
15175ec8b7d1SJesse Brandeburg 		/* exit if we failed to retrieve a buffer */
15185ec8b7d1SJesse Brandeburg 		if (!skb) {
15195ec8b7d1SJesse Brandeburg 			rx_ring->rx_stats.alloc_buff_failed++;
15205ec8b7d1SJesse Brandeburg 			rx_buffer->pagecnt_bias++;
15215ec8b7d1SJesse Brandeburg 			break;
15225ec8b7d1SJesse Brandeburg 		}
15235ec8b7d1SJesse Brandeburg 
152456184e01SJesse Brandeburg 		iavf_put_rx_buffer(rx_ring, rx_buffer);
15255ec8b7d1SJesse Brandeburg 		cleaned_count++;
15265ec8b7d1SJesse Brandeburg 
152756184e01SJesse Brandeburg 		if (iavf_is_non_eop(rx_ring, rx_desc, skb))
15285ec8b7d1SJesse Brandeburg 			continue;
15295ec8b7d1SJesse Brandeburg 
15305ec8b7d1SJesse Brandeburg 		/* ERR_MASK will only have valid bits if EOP set, and
15315ec8b7d1SJesse Brandeburg 		 * what we are doing here is actually checking
1532f1cad2ceSJesse Brandeburg 		 * IAVF_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
15335ec8b7d1SJesse Brandeburg 		 * the error field
15345ec8b7d1SJesse Brandeburg 		 */
153556184e01SJesse Brandeburg 		if (unlikely(iavf_test_staterr(rx_desc, BIT(IAVF_RXD_QW1_ERROR_SHIFT)))) {
15365ec8b7d1SJesse Brandeburg 			dev_kfree_skb_any(skb);
15375ec8b7d1SJesse Brandeburg 			skb = NULL;
15385ec8b7d1SJesse Brandeburg 			continue;
15395ec8b7d1SJesse Brandeburg 		}
15405ec8b7d1SJesse Brandeburg 
154156184e01SJesse Brandeburg 		if (iavf_cleanup_headers(rx_ring, skb)) {
15425ec8b7d1SJesse Brandeburg 			skb = NULL;
15435ec8b7d1SJesse Brandeburg 			continue;
15445ec8b7d1SJesse Brandeburg 		}
15455ec8b7d1SJesse Brandeburg 
15465ec8b7d1SJesse Brandeburg 		/* probably a little skewed due to removing CRC */
15475ec8b7d1SJesse Brandeburg 		total_rx_bytes += skb->len;
15485ec8b7d1SJesse Brandeburg 
15495ec8b7d1SJesse Brandeburg 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
155056184e01SJesse Brandeburg 		rx_ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >>
155156184e01SJesse Brandeburg 			   IAVF_RXD_QW1_PTYPE_SHIFT;
15525ec8b7d1SJesse Brandeburg 
15535ec8b7d1SJesse Brandeburg 		/* populate checksum, VLAN, and protocol */
15545ec8b7d1SJesse Brandeburg 		iavf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
15555ec8b7d1SJesse Brandeburg 
15565ec8b7d1SJesse Brandeburg 
1557f1cad2ceSJesse Brandeburg 		vlan_tag = (qword & BIT(IAVF_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
15585ec8b7d1SJesse Brandeburg 			   le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
15595ec8b7d1SJesse Brandeburg 
1560ad64ed8bSJesse Brandeburg 		iavf_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
156156184e01SJesse Brandeburg 		iavf_receive_skb(rx_ring, skb, vlan_tag);
15625ec8b7d1SJesse Brandeburg 		skb = NULL;
15635ec8b7d1SJesse Brandeburg 
15645ec8b7d1SJesse Brandeburg 		/* update budget accounting */
15655ec8b7d1SJesse Brandeburg 		total_rx_packets++;
15665ec8b7d1SJesse Brandeburg 	}
15675ec8b7d1SJesse Brandeburg 
15685ec8b7d1SJesse Brandeburg 	rx_ring->skb = skb;
15695ec8b7d1SJesse Brandeburg 
15705ec8b7d1SJesse Brandeburg 	u64_stats_update_begin(&rx_ring->syncp);
15715ec8b7d1SJesse Brandeburg 	rx_ring->stats.packets += total_rx_packets;
15725ec8b7d1SJesse Brandeburg 	rx_ring->stats.bytes += total_rx_bytes;
15735ec8b7d1SJesse Brandeburg 	u64_stats_update_end(&rx_ring->syncp);
15745ec8b7d1SJesse Brandeburg 	rx_ring->q_vector->rx.total_packets += total_rx_packets;
15755ec8b7d1SJesse Brandeburg 	rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
15765ec8b7d1SJesse Brandeburg 
15775ec8b7d1SJesse Brandeburg 	/* guarantee a trip back through this routine if there was a failure */
15785ec8b7d1SJesse Brandeburg 	return failure ? budget : (int)total_rx_packets;
15795ec8b7d1SJesse Brandeburg }
15805ec8b7d1SJesse Brandeburg 
158156184e01SJesse Brandeburg static inline u32 iavf_buildreg_itr(const int type, u16 itr)
15825ec8b7d1SJesse Brandeburg {
15835ec8b7d1SJesse Brandeburg 	u32 val;
15845ec8b7d1SJesse Brandeburg 
15855ec8b7d1SJesse Brandeburg 	/* We don't bother with setting the CLEARPBA bit as the data sheet
15865ec8b7d1SJesse Brandeburg 	 * points out doing so is "meaningless since it was already
15875ec8b7d1SJesse Brandeburg 	 * auto-cleared". The auto-clearing happens when the interrupt is
15885ec8b7d1SJesse Brandeburg 	 * asserted.
15895ec8b7d1SJesse Brandeburg 	 *
15905ec8b7d1SJesse Brandeburg 	 * Hardware errata 28 for also indicates that writing to a
15915ec8b7d1SJesse Brandeburg 	 * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear
15925ec8b7d1SJesse Brandeburg 	 * an event in the PBA anyway so we need to rely on the automask
15935ec8b7d1SJesse Brandeburg 	 * to hold pending events for us until the interrupt is re-enabled
15945ec8b7d1SJesse Brandeburg 	 *
15955ec8b7d1SJesse Brandeburg 	 * The itr value is reported in microseconds, and the register
15965ec8b7d1SJesse Brandeburg 	 * value is recorded in 2 microsecond units. For this reason we
15975ec8b7d1SJesse Brandeburg 	 * only need to shift by the interval shift - 1 instead of the
15985ec8b7d1SJesse Brandeburg 	 * full value.
15995ec8b7d1SJesse Brandeburg 	 */
160056184e01SJesse Brandeburg 	itr &= IAVF_ITR_MASK;
16015ec8b7d1SJesse Brandeburg 
1602f1cad2ceSJesse Brandeburg 	val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1603f1cad2ceSJesse Brandeburg 	      (type << IAVF_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
1604f1cad2ceSJesse Brandeburg 	      (itr << (IAVF_VFINT_DYN_CTLN1_INTERVAL_SHIFT - 1));
16055ec8b7d1SJesse Brandeburg 
16065ec8b7d1SJesse Brandeburg 	return val;
16075ec8b7d1SJesse Brandeburg }
16085ec8b7d1SJesse Brandeburg 
16095ec8b7d1SJesse Brandeburg /* a small macro to shorten up some long lines */
1610f1cad2ceSJesse Brandeburg #define INTREG IAVF_VFINT_DYN_CTLN1
16115ec8b7d1SJesse Brandeburg 
16125ec8b7d1SJesse Brandeburg /* The act of updating the ITR will cause it to immediately trigger. In order
16135ec8b7d1SJesse Brandeburg  * to prevent this from throwing off adaptive update statistics we defer the
16145ec8b7d1SJesse Brandeburg  * update so that it can only happen so often. So after either Tx or Rx are
16155ec8b7d1SJesse Brandeburg  * updated we make the adaptive scheme wait until either the ITR completely
16165ec8b7d1SJesse Brandeburg  * expires via the next_update expiration or we have been through at least
16175ec8b7d1SJesse Brandeburg  * 3 interrupts.
16185ec8b7d1SJesse Brandeburg  */
16195ec8b7d1SJesse Brandeburg #define ITR_COUNTDOWN_START 3
16205ec8b7d1SJesse Brandeburg 
16215ec8b7d1SJesse Brandeburg /**
162256184e01SJesse Brandeburg  * iavf_update_enable_itr - Update itr and re-enable MSIX interrupt
16235ec8b7d1SJesse Brandeburg  * @vsi: the VSI we care about
16245ec8b7d1SJesse Brandeburg  * @q_vector: q_vector for which itr is being updated and interrupt enabled
16255ec8b7d1SJesse Brandeburg  *
16265ec8b7d1SJesse Brandeburg  **/
162756184e01SJesse Brandeburg static inline void iavf_update_enable_itr(struct iavf_vsi *vsi,
162856184e01SJesse Brandeburg 					  struct iavf_q_vector *q_vector)
16295ec8b7d1SJesse Brandeburg {
1630f349daa5SJesse Brandeburg 	struct iavf_hw *hw = &vsi->back->hw;
16315ec8b7d1SJesse Brandeburg 	u32 intval;
16325ec8b7d1SJesse Brandeburg 
16335ec8b7d1SJesse Brandeburg 	/* These will do nothing if dynamic updates are not enabled */
163456184e01SJesse Brandeburg 	iavf_update_itr(q_vector, &q_vector->tx);
163556184e01SJesse Brandeburg 	iavf_update_itr(q_vector, &q_vector->rx);
16365ec8b7d1SJesse Brandeburg 
16375ec8b7d1SJesse Brandeburg 	/* This block of logic allows us to get away with only updating
16385ec8b7d1SJesse Brandeburg 	 * one ITR value with each interrupt. The idea is to perform a
16395ec8b7d1SJesse Brandeburg 	 * pseudo-lazy update with the following criteria.
16405ec8b7d1SJesse Brandeburg 	 *
16415ec8b7d1SJesse Brandeburg 	 * 1. Rx is given higher priority than Tx if both are in same state
16425ec8b7d1SJesse Brandeburg 	 * 2. If we must reduce an ITR that is given highest priority.
16435ec8b7d1SJesse Brandeburg 	 * 3. We then give priority to increasing ITR based on amount.
16445ec8b7d1SJesse Brandeburg 	 */
16455ec8b7d1SJesse Brandeburg 	if (q_vector->rx.target_itr < q_vector->rx.current_itr) {
16465ec8b7d1SJesse Brandeburg 		/* Rx ITR needs to be reduced, this is highest priority */
164756184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_RX_ITR,
16485ec8b7d1SJesse Brandeburg 					   q_vector->rx.target_itr);
16495ec8b7d1SJesse Brandeburg 		q_vector->rx.current_itr = q_vector->rx.target_itr;
16505ec8b7d1SJesse Brandeburg 		q_vector->itr_countdown = ITR_COUNTDOWN_START;
16515ec8b7d1SJesse Brandeburg 	} else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) ||
16525ec8b7d1SJesse Brandeburg 		   ((q_vector->rx.target_itr - q_vector->rx.current_itr) <
16535ec8b7d1SJesse Brandeburg 		    (q_vector->tx.target_itr - q_vector->tx.current_itr))) {
16545ec8b7d1SJesse Brandeburg 		/* Tx ITR needs to be reduced, this is second priority
16555ec8b7d1SJesse Brandeburg 		 * Tx ITR needs to be increased more than Rx, fourth priority
16565ec8b7d1SJesse Brandeburg 		 */
165756184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_TX_ITR,
16585ec8b7d1SJesse Brandeburg 					   q_vector->tx.target_itr);
16595ec8b7d1SJesse Brandeburg 		q_vector->tx.current_itr = q_vector->tx.target_itr;
16605ec8b7d1SJesse Brandeburg 		q_vector->itr_countdown = ITR_COUNTDOWN_START;
16615ec8b7d1SJesse Brandeburg 	} else if (q_vector->rx.current_itr != q_vector->rx.target_itr) {
16625ec8b7d1SJesse Brandeburg 		/* Rx ITR needs to be increased, third priority */
166356184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_RX_ITR,
16645ec8b7d1SJesse Brandeburg 					   q_vector->rx.target_itr);
16655ec8b7d1SJesse Brandeburg 		q_vector->rx.current_itr = q_vector->rx.target_itr;
16665ec8b7d1SJesse Brandeburg 		q_vector->itr_countdown = ITR_COUNTDOWN_START;
16675ec8b7d1SJesse Brandeburg 	} else {
16685ec8b7d1SJesse Brandeburg 		/* No ITR update, lowest priority */
166956184e01SJesse Brandeburg 		intval = iavf_buildreg_itr(IAVF_ITR_NONE, 0);
16705ec8b7d1SJesse Brandeburg 		if (q_vector->itr_countdown)
16715ec8b7d1SJesse Brandeburg 			q_vector->itr_countdown--;
16725ec8b7d1SJesse Brandeburg 	}
16735ec8b7d1SJesse Brandeburg 
167456184e01SJesse Brandeburg 	if (!test_bit(__IAVF_VSI_DOWN, vsi->state))
16755ec8b7d1SJesse Brandeburg 		wr32(hw, INTREG(q_vector->reg_idx), intval);
16765ec8b7d1SJesse Brandeburg }
16775ec8b7d1SJesse Brandeburg 
16785ec8b7d1SJesse Brandeburg /**
16795ec8b7d1SJesse Brandeburg  * iavf_napi_poll - NAPI polling Rx/Tx cleanup routine
16805ec8b7d1SJesse Brandeburg  * @napi: napi struct with our devices info in it
16815ec8b7d1SJesse Brandeburg  * @budget: amount of work driver is allowed to do this pass, in packets
16825ec8b7d1SJesse Brandeburg  *
16835ec8b7d1SJesse Brandeburg  * This function will clean all queues associated with a q_vector.
16845ec8b7d1SJesse Brandeburg  *
16855ec8b7d1SJesse Brandeburg  * Returns the amount of work done
16865ec8b7d1SJesse Brandeburg  **/
16875ec8b7d1SJesse Brandeburg int iavf_napi_poll(struct napi_struct *napi, int budget)
16885ec8b7d1SJesse Brandeburg {
168956184e01SJesse Brandeburg 	struct iavf_q_vector *q_vector =
169056184e01SJesse Brandeburg 			       container_of(napi, struct iavf_q_vector, napi);
169156184e01SJesse Brandeburg 	struct iavf_vsi *vsi = q_vector->vsi;
169256184e01SJesse Brandeburg 	struct iavf_ring *ring;
16935ec8b7d1SJesse Brandeburg 	bool clean_complete = true;
16945ec8b7d1SJesse Brandeburg 	bool arm_wb = false;
16955ec8b7d1SJesse Brandeburg 	int budget_per_ring;
16965ec8b7d1SJesse Brandeburg 	int work_done = 0;
16975ec8b7d1SJesse Brandeburg 
169856184e01SJesse Brandeburg 	if (test_bit(__IAVF_VSI_DOWN, vsi->state)) {
16995ec8b7d1SJesse Brandeburg 		napi_complete(napi);
17005ec8b7d1SJesse Brandeburg 		return 0;
17015ec8b7d1SJesse Brandeburg 	}
17025ec8b7d1SJesse Brandeburg 
17035ec8b7d1SJesse Brandeburg 	/* Since the actual Tx work is minimal, we can give the Tx a larger
17045ec8b7d1SJesse Brandeburg 	 * budget and be more aggressive about cleaning up the Tx descriptors.
17055ec8b7d1SJesse Brandeburg 	 */
170656184e01SJesse Brandeburg 	iavf_for_each_ring(ring, q_vector->tx) {
170756184e01SJesse Brandeburg 		if (!iavf_clean_tx_irq(vsi, ring, budget)) {
17085ec8b7d1SJesse Brandeburg 			clean_complete = false;
17095ec8b7d1SJesse Brandeburg 			continue;
17105ec8b7d1SJesse Brandeburg 		}
17115ec8b7d1SJesse Brandeburg 		arm_wb |= ring->arm_wb;
17125ec8b7d1SJesse Brandeburg 		ring->arm_wb = false;
17135ec8b7d1SJesse Brandeburg 	}
17145ec8b7d1SJesse Brandeburg 
17155ec8b7d1SJesse Brandeburg 	/* Handle case where we are called by netpoll with a budget of 0 */
17165ec8b7d1SJesse Brandeburg 	if (budget <= 0)
17175ec8b7d1SJesse Brandeburg 		goto tx_only;
17185ec8b7d1SJesse Brandeburg 
17195ec8b7d1SJesse Brandeburg 	/* We attempt to distribute budget to each Rx queue fairly, but don't
17205ec8b7d1SJesse Brandeburg 	 * allow the budget to go below 1 because that would exit polling early.
17215ec8b7d1SJesse Brandeburg 	 */
17225ec8b7d1SJesse Brandeburg 	budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
17235ec8b7d1SJesse Brandeburg 
172456184e01SJesse Brandeburg 	iavf_for_each_ring(ring, q_vector->rx) {
172556184e01SJesse Brandeburg 		int cleaned = iavf_clean_rx_irq(ring, budget_per_ring);
17265ec8b7d1SJesse Brandeburg 
17275ec8b7d1SJesse Brandeburg 		work_done += cleaned;
17285ec8b7d1SJesse Brandeburg 		/* if we clean as many as budgeted, we must not be done */
17295ec8b7d1SJesse Brandeburg 		if (cleaned >= budget_per_ring)
17305ec8b7d1SJesse Brandeburg 			clean_complete = false;
17315ec8b7d1SJesse Brandeburg 	}
17325ec8b7d1SJesse Brandeburg 
17335ec8b7d1SJesse Brandeburg 	/* If work not completed, return budget and polling will return */
17345ec8b7d1SJesse Brandeburg 	if (!clean_complete) {
17355ec8b7d1SJesse Brandeburg 		int cpu_id = smp_processor_id();
17365ec8b7d1SJesse Brandeburg 
17375ec8b7d1SJesse Brandeburg 		/* It is possible that the interrupt affinity has changed but,
17385ec8b7d1SJesse Brandeburg 		 * if the cpu is pegged at 100%, polling will never exit while
17395ec8b7d1SJesse Brandeburg 		 * traffic continues and the interrupt will be stuck on this
17405ec8b7d1SJesse Brandeburg 		 * cpu.  We check to make sure affinity is correct before we
17415ec8b7d1SJesse Brandeburg 		 * continue to poll, otherwise we must stop polling so the
17425ec8b7d1SJesse Brandeburg 		 * interrupt can move to the correct cpu.
17435ec8b7d1SJesse Brandeburg 		 */
17445ec8b7d1SJesse Brandeburg 		if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
17455ec8b7d1SJesse Brandeburg 			/* Tell napi that we are done polling */
17465ec8b7d1SJesse Brandeburg 			napi_complete_done(napi, work_done);
17475ec8b7d1SJesse Brandeburg 
17485ec8b7d1SJesse Brandeburg 			/* Force an interrupt */
17495ec8b7d1SJesse Brandeburg 			iavf_force_wb(vsi, q_vector);
17505ec8b7d1SJesse Brandeburg 
17515ec8b7d1SJesse Brandeburg 			/* Return budget-1 so that polling stops */
17525ec8b7d1SJesse Brandeburg 			return budget - 1;
17535ec8b7d1SJesse Brandeburg 		}
17545ec8b7d1SJesse Brandeburg tx_only:
17555ec8b7d1SJesse Brandeburg 		if (arm_wb) {
17565ec8b7d1SJesse Brandeburg 			q_vector->tx.ring[0].tx_stats.tx_force_wb++;
175756184e01SJesse Brandeburg 			iavf_enable_wb_on_itr(vsi, q_vector);
17585ec8b7d1SJesse Brandeburg 		}
17595ec8b7d1SJesse Brandeburg 		return budget;
17605ec8b7d1SJesse Brandeburg 	}
17615ec8b7d1SJesse Brandeburg 
176256184e01SJesse Brandeburg 	if (vsi->back->flags & IAVF_TXR_FLAGS_WB_ON_ITR)
17635ec8b7d1SJesse Brandeburg 		q_vector->arm_wb_state = false;
17645ec8b7d1SJesse Brandeburg 
17655ec8b7d1SJesse Brandeburg 	/* Work is done so exit the polling mode and re-enable the interrupt */
17665ec8b7d1SJesse Brandeburg 	napi_complete_done(napi, work_done);
17675ec8b7d1SJesse Brandeburg 
176856184e01SJesse Brandeburg 	iavf_update_enable_itr(vsi, q_vector);
17695ec8b7d1SJesse Brandeburg 
17705ec8b7d1SJesse Brandeburg 	return min(work_done, budget - 1);
17715ec8b7d1SJesse Brandeburg }
17725ec8b7d1SJesse Brandeburg 
17735ec8b7d1SJesse Brandeburg /**
17745ec8b7d1SJesse Brandeburg  * iavf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
17755ec8b7d1SJesse Brandeburg  * @skb:     send buffer
17765ec8b7d1SJesse Brandeburg  * @tx_ring: ring to send buffer on
17775ec8b7d1SJesse Brandeburg  * @flags:   the tx flags to be set
17785ec8b7d1SJesse Brandeburg  *
17795ec8b7d1SJesse Brandeburg  * Checks the skb and set up correspondingly several generic transmit flags
17805ec8b7d1SJesse Brandeburg  * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
17815ec8b7d1SJesse Brandeburg  *
17825ec8b7d1SJesse Brandeburg  * Returns error code indicate the frame should be dropped upon error and the
17835ec8b7d1SJesse Brandeburg  * otherwise  returns 0 to indicate the flags has been set properly.
17845ec8b7d1SJesse Brandeburg  **/
17855ec8b7d1SJesse Brandeburg static inline int iavf_tx_prepare_vlan_flags(struct sk_buff *skb,
178656184e01SJesse Brandeburg 					     struct iavf_ring *tx_ring,
17875ec8b7d1SJesse Brandeburg 					     u32 *flags)
17885ec8b7d1SJesse Brandeburg {
17895ec8b7d1SJesse Brandeburg 	__be16 protocol = skb->protocol;
17905ec8b7d1SJesse Brandeburg 	u32  tx_flags = 0;
17915ec8b7d1SJesse Brandeburg 
17925ec8b7d1SJesse Brandeburg 	if (protocol == htons(ETH_P_8021Q) &&
17935ec8b7d1SJesse Brandeburg 	    !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
17945ec8b7d1SJesse Brandeburg 		/* When HW VLAN acceleration is turned off by the user the
17955ec8b7d1SJesse Brandeburg 		 * stack sets the protocol to 8021q so that the driver
17965ec8b7d1SJesse Brandeburg 		 * can take any steps required to support the SW only
17975ec8b7d1SJesse Brandeburg 		 * VLAN handling.  In our case the driver doesn't need
17985ec8b7d1SJesse Brandeburg 		 * to take any further steps so just set the protocol
17995ec8b7d1SJesse Brandeburg 		 * to the encapsulated ethertype.
18005ec8b7d1SJesse Brandeburg 		 */
18015ec8b7d1SJesse Brandeburg 		skb->protocol = vlan_get_protocol(skb);
18025ec8b7d1SJesse Brandeburg 		goto out;
18035ec8b7d1SJesse Brandeburg 	}
18045ec8b7d1SJesse Brandeburg 
18055ec8b7d1SJesse Brandeburg 	/* if we have a HW VLAN tag being added, default to the HW one */
18065ec8b7d1SJesse Brandeburg 	if (skb_vlan_tag_present(skb)) {
180756184e01SJesse Brandeburg 		tx_flags |= skb_vlan_tag_get(skb) << IAVF_TX_FLAGS_VLAN_SHIFT;
180856184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_HW_VLAN;
18095ec8b7d1SJesse Brandeburg 	/* else if it is a SW VLAN, check the next protocol and store the tag */
18105ec8b7d1SJesse Brandeburg 	} else if (protocol == htons(ETH_P_8021Q)) {
18115ec8b7d1SJesse Brandeburg 		struct vlan_hdr *vhdr, _vhdr;
18125ec8b7d1SJesse Brandeburg 
18135ec8b7d1SJesse Brandeburg 		vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
18145ec8b7d1SJesse Brandeburg 		if (!vhdr)
18155ec8b7d1SJesse Brandeburg 			return -EINVAL;
18165ec8b7d1SJesse Brandeburg 
18175ec8b7d1SJesse Brandeburg 		protocol = vhdr->h_vlan_encapsulated_proto;
181856184e01SJesse Brandeburg 		tx_flags |= ntohs(vhdr->h_vlan_TCI) << IAVF_TX_FLAGS_VLAN_SHIFT;
181956184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_SW_VLAN;
18205ec8b7d1SJesse Brandeburg 	}
18215ec8b7d1SJesse Brandeburg 
18225ec8b7d1SJesse Brandeburg out:
18235ec8b7d1SJesse Brandeburg 	*flags = tx_flags;
18245ec8b7d1SJesse Brandeburg 	return 0;
18255ec8b7d1SJesse Brandeburg }
18265ec8b7d1SJesse Brandeburg 
18275ec8b7d1SJesse Brandeburg /**
182856184e01SJesse Brandeburg  * iavf_tso - set up the tso context descriptor
18295ec8b7d1SJesse Brandeburg  * @first:    pointer to first Tx buffer for xmit
18305ec8b7d1SJesse Brandeburg  * @hdr_len:  ptr to the size of the packet header
18315ec8b7d1SJesse Brandeburg  * @cd_type_cmd_tso_mss: Quad Word 1
18325ec8b7d1SJesse Brandeburg  *
18335ec8b7d1SJesse Brandeburg  * Returns 0 if no TSO can happen, 1 if tso is going, or error
18345ec8b7d1SJesse Brandeburg  **/
183556184e01SJesse Brandeburg static int iavf_tso(struct iavf_tx_buffer *first, u8 *hdr_len,
18365ec8b7d1SJesse Brandeburg 		    u64 *cd_type_cmd_tso_mss)
18375ec8b7d1SJesse Brandeburg {
18385ec8b7d1SJesse Brandeburg 	struct sk_buff *skb = first->skb;
18395ec8b7d1SJesse Brandeburg 	u64 cd_cmd, cd_tso_len, cd_mss;
18405ec8b7d1SJesse Brandeburg 	union {
18415ec8b7d1SJesse Brandeburg 		struct iphdr *v4;
18425ec8b7d1SJesse Brandeburg 		struct ipv6hdr *v6;
18435ec8b7d1SJesse Brandeburg 		unsigned char *hdr;
18445ec8b7d1SJesse Brandeburg 	} ip;
18455ec8b7d1SJesse Brandeburg 	union {
18465ec8b7d1SJesse Brandeburg 		struct tcphdr *tcp;
18475ec8b7d1SJesse Brandeburg 		struct udphdr *udp;
18485ec8b7d1SJesse Brandeburg 		unsigned char *hdr;
18495ec8b7d1SJesse Brandeburg 	} l4;
18505ec8b7d1SJesse Brandeburg 	u32 paylen, l4_offset;
18515ec8b7d1SJesse Brandeburg 	u16 gso_segs, gso_size;
18525ec8b7d1SJesse Brandeburg 	int err;
18535ec8b7d1SJesse Brandeburg 
18545ec8b7d1SJesse Brandeburg 	if (skb->ip_summed != CHECKSUM_PARTIAL)
18555ec8b7d1SJesse Brandeburg 		return 0;
18565ec8b7d1SJesse Brandeburg 
18575ec8b7d1SJesse Brandeburg 	if (!skb_is_gso(skb))
18585ec8b7d1SJesse Brandeburg 		return 0;
18595ec8b7d1SJesse Brandeburg 
18605ec8b7d1SJesse Brandeburg 	err = skb_cow_head(skb, 0);
18615ec8b7d1SJesse Brandeburg 	if (err < 0)
18625ec8b7d1SJesse Brandeburg 		return err;
18635ec8b7d1SJesse Brandeburg 
18645ec8b7d1SJesse Brandeburg 	ip.hdr = skb_network_header(skb);
18655ec8b7d1SJesse Brandeburg 	l4.hdr = skb_transport_header(skb);
18665ec8b7d1SJesse Brandeburg 
18675ec8b7d1SJesse Brandeburg 	/* initialize outer IP header fields */
18685ec8b7d1SJesse Brandeburg 	if (ip.v4->version == 4) {
18695ec8b7d1SJesse Brandeburg 		ip.v4->tot_len = 0;
18705ec8b7d1SJesse Brandeburg 		ip.v4->check = 0;
18715ec8b7d1SJesse Brandeburg 	} else {
18725ec8b7d1SJesse Brandeburg 		ip.v6->payload_len = 0;
18735ec8b7d1SJesse Brandeburg 	}
18745ec8b7d1SJesse Brandeburg 
18755ec8b7d1SJesse Brandeburg 	if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
18765ec8b7d1SJesse Brandeburg 					 SKB_GSO_GRE_CSUM |
18775ec8b7d1SJesse Brandeburg 					 SKB_GSO_IPXIP4 |
18785ec8b7d1SJesse Brandeburg 					 SKB_GSO_IPXIP6 |
18795ec8b7d1SJesse Brandeburg 					 SKB_GSO_UDP_TUNNEL |
18805ec8b7d1SJesse Brandeburg 					 SKB_GSO_UDP_TUNNEL_CSUM)) {
18815ec8b7d1SJesse Brandeburg 		if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
18825ec8b7d1SJesse Brandeburg 		    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
18835ec8b7d1SJesse Brandeburg 			l4.udp->len = 0;
18845ec8b7d1SJesse Brandeburg 
18855ec8b7d1SJesse Brandeburg 			/* determine offset of outer transport header */
18865ec8b7d1SJesse Brandeburg 			l4_offset = l4.hdr - skb->data;
18875ec8b7d1SJesse Brandeburg 
18885ec8b7d1SJesse Brandeburg 			/* remove payload length from outer checksum */
18895ec8b7d1SJesse Brandeburg 			paylen = skb->len - l4_offset;
18905ec8b7d1SJesse Brandeburg 			csum_replace_by_diff(&l4.udp->check,
18915ec8b7d1SJesse Brandeburg 					     (__force __wsum)htonl(paylen));
18925ec8b7d1SJesse Brandeburg 		}
18935ec8b7d1SJesse Brandeburg 
18945ec8b7d1SJesse Brandeburg 		/* reset pointers to inner headers */
18955ec8b7d1SJesse Brandeburg 		ip.hdr = skb_inner_network_header(skb);
18965ec8b7d1SJesse Brandeburg 		l4.hdr = skb_inner_transport_header(skb);
18975ec8b7d1SJesse Brandeburg 
18985ec8b7d1SJesse Brandeburg 		/* initialize inner IP header fields */
18995ec8b7d1SJesse Brandeburg 		if (ip.v4->version == 4) {
19005ec8b7d1SJesse Brandeburg 			ip.v4->tot_len = 0;
19015ec8b7d1SJesse Brandeburg 			ip.v4->check = 0;
19025ec8b7d1SJesse Brandeburg 		} else {
19035ec8b7d1SJesse Brandeburg 			ip.v6->payload_len = 0;
19045ec8b7d1SJesse Brandeburg 		}
19055ec8b7d1SJesse Brandeburg 	}
19065ec8b7d1SJesse Brandeburg 
19075ec8b7d1SJesse Brandeburg 	/* determine offset of inner transport header */
19085ec8b7d1SJesse Brandeburg 	l4_offset = l4.hdr - skb->data;
19095ec8b7d1SJesse Brandeburg 
19105ec8b7d1SJesse Brandeburg 	/* remove payload length from inner checksum */
19115ec8b7d1SJesse Brandeburg 	paylen = skb->len - l4_offset;
19125ec8b7d1SJesse Brandeburg 	csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
19135ec8b7d1SJesse Brandeburg 
19145ec8b7d1SJesse Brandeburg 	/* compute length of segmentation header */
19155ec8b7d1SJesse Brandeburg 	*hdr_len = (l4.tcp->doff * 4) + l4_offset;
19165ec8b7d1SJesse Brandeburg 
19175ec8b7d1SJesse Brandeburg 	/* pull values out of skb_shinfo */
19185ec8b7d1SJesse Brandeburg 	gso_size = skb_shinfo(skb)->gso_size;
19195ec8b7d1SJesse Brandeburg 	gso_segs = skb_shinfo(skb)->gso_segs;
19205ec8b7d1SJesse Brandeburg 
19215ec8b7d1SJesse Brandeburg 	/* update GSO size and bytecount with header size */
19225ec8b7d1SJesse Brandeburg 	first->gso_segs = gso_segs;
19235ec8b7d1SJesse Brandeburg 	first->bytecount += (first->gso_segs - 1) * *hdr_len;
19245ec8b7d1SJesse Brandeburg 
19255ec8b7d1SJesse Brandeburg 	/* find the field values */
192656184e01SJesse Brandeburg 	cd_cmd = IAVF_TX_CTX_DESC_TSO;
19275ec8b7d1SJesse Brandeburg 	cd_tso_len = skb->len - *hdr_len;
19285ec8b7d1SJesse Brandeburg 	cd_mss = gso_size;
192956184e01SJesse Brandeburg 	*cd_type_cmd_tso_mss |= (cd_cmd << IAVF_TXD_CTX_QW1_CMD_SHIFT) |
193056184e01SJesse Brandeburg 				(cd_tso_len << IAVF_TXD_CTX_QW1_TSO_LEN_SHIFT) |
193156184e01SJesse Brandeburg 				(cd_mss << IAVF_TXD_CTX_QW1_MSS_SHIFT);
19325ec8b7d1SJesse Brandeburg 	return 1;
19335ec8b7d1SJesse Brandeburg }
19345ec8b7d1SJesse Brandeburg 
19355ec8b7d1SJesse Brandeburg /**
193656184e01SJesse Brandeburg  * iavf_tx_enable_csum - Enable Tx checksum offloads
19375ec8b7d1SJesse Brandeburg  * @skb: send buffer
19385ec8b7d1SJesse Brandeburg  * @tx_flags: pointer to Tx flags currently set
19395ec8b7d1SJesse Brandeburg  * @td_cmd: Tx descriptor command bits to set
19405ec8b7d1SJesse Brandeburg  * @td_offset: Tx descriptor header offsets to set
19415ec8b7d1SJesse Brandeburg  * @tx_ring: Tx descriptor ring
19425ec8b7d1SJesse Brandeburg  * @cd_tunneling: ptr to context desc bits
19435ec8b7d1SJesse Brandeburg  **/
194456184e01SJesse Brandeburg static int iavf_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
19455ec8b7d1SJesse Brandeburg 			       u32 *td_cmd, u32 *td_offset,
194656184e01SJesse Brandeburg 			       struct iavf_ring *tx_ring,
19475ec8b7d1SJesse Brandeburg 			       u32 *cd_tunneling)
19485ec8b7d1SJesse Brandeburg {
19495ec8b7d1SJesse Brandeburg 	union {
19505ec8b7d1SJesse Brandeburg 		struct iphdr *v4;
19515ec8b7d1SJesse Brandeburg 		struct ipv6hdr *v6;
19525ec8b7d1SJesse Brandeburg 		unsigned char *hdr;
19535ec8b7d1SJesse Brandeburg 	} ip;
19545ec8b7d1SJesse Brandeburg 	union {
19555ec8b7d1SJesse Brandeburg 		struct tcphdr *tcp;
19565ec8b7d1SJesse Brandeburg 		struct udphdr *udp;
19575ec8b7d1SJesse Brandeburg 		unsigned char *hdr;
19585ec8b7d1SJesse Brandeburg 	} l4;
19595ec8b7d1SJesse Brandeburg 	unsigned char *exthdr;
19605ec8b7d1SJesse Brandeburg 	u32 offset, cmd = 0;
19615ec8b7d1SJesse Brandeburg 	__be16 frag_off;
19625ec8b7d1SJesse Brandeburg 	u8 l4_proto = 0;
19635ec8b7d1SJesse Brandeburg 
19645ec8b7d1SJesse Brandeburg 	if (skb->ip_summed != CHECKSUM_PARTIAL)
19655ec8b7d1SJesse Brandeburg 		return 0;
19665ec8b7d1SJesse Brandeburg 
19675ec8b7d1SJesse Brandeburg 	ip.hdr = skb_network_header(skb);
19685ec8b7d1SJesse Brandeburg 	l4.hdr = skb_transport_header(skb);
19695ec8b7d1SJesse Brandeburg 
19705ec8b7d1SJesse Brandeburg 	/* compute outer L2 header size */
1971f1cad2ceSJesse Brandeburg 	offset = ((ip.hdr - skb->data) / 2) << IAVF_TX_DESC_LENGTH_MACLEN_SHIFT;
19725ec8b7d1SJesse Brandeburg 
19735ec8b7d1SJesse Brandeburg 	if (skb->encapsulation) {
19745ec8b7d1SJesse Brandeburg 		u32 tunnel = 0;
19755ec8b7d1SJesse Brandeburg 		/* define outer network header type */
197656184e01SJesse Brandeburg 		if (*tx_flags & IAVF_TX_FLAGS_IPV4) {
197756184e01SJesse Brandeburg 			tunnel |= (*tx_flags & IAVF_TX_FLAGS_TSO) ?
197856184e01SJesse Brandeburg 				  IAVF_TX_CTX_EXT_IP_IPV4 :
197956184e01SJesse Brandeburg 				  IAVF_TX_CTX_EXT_IP_IPV4_NO_CSUM;
19805ec8b7d1SJesse Brandeburg 
19815ec8b7d1SJesse Brandeburg 			l4_proto = ip.v4->protocol;
198256184e01SJesse Brandeburg 		} else if (*tx_flags & IAVF_TX_FLAGS_IPV6) {
198356184e01SJesse Brandeburg 			tunnel |= IAVF_TX_CTX_EXT_IP_IPV6;
19845ec8b7d1SJesse Brandeburg 
19855ec8b7d1SJesse Brandeburg 			exthdr = ip.hdr + sizeof(*ip.v6);
19865ec8b7d1SJesse Brandeburg 			l4_proto = ip.v6->nexthdr;
19875ec8b7d1SJesse Brandeburg 			if (l4.hdr != exthdr)
19885ec8b7d1SJesse Brandeburg 				ipv6_skip_exthdr(skb, exthdr - skb->data,
19895ec8b7d1SJesse Brandeburg 						 &l4_proto, &frag_off);
19905ec8b7d1SJesse Brandeburg 		}
19915ec8b7d1SJesse Brandeburg 
19925ec8b7d1SJesse Brandeburg 		/* define outer transport */
19935ec8b7d1SJesse Brandeburg 		switch (l4_proto) {
19945ec8b7d1SJesse Brandeburg 		case IPPROTO_UDP:
199556184e01SJesse Brandeburg 			tunnel |= IAVF_TXD_CTX_UDP_TUNNELING;
199656184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
19975ec8b7d1SJesse Brandeburg 			break;
19985ec8b7d1SJesse Brandeburg 		case IPPROTO_GRE:
199956184e01SJesse Brandeburg 			tunnel |= IAVF_TXD_CTX_GRE_TUNNELING;
200056184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
20015ec8b7d1SJesse Brandeburg 			break;
20025ec8b7d1SJesse Brandeburg 		case IPPROTO_IPIP:
20035ec8b7d1SJesse Brandeburg 		case IPPROTO_IPV6:
200456184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL;
20055ec8b7d1SJesse Brandeburg 			l4.hdr = skb_inner_network_header(skb);
20065ec8b7d1SJesse Brandeburg 			break;
20075ec8b7d1SJesse Brandeburg 		default:
200856184e01SJesse Brandeburg 			if (*tx_flags & IAVF_TX_FLAGS_TSO)
20095ec8b7d1SJesse Brandeburg 				return -1;
20105ec8b7d1SJesse Brandeburg 
20115ec8b7d1SJesse Brandeburg 			skb_checksum_help(skb);
20125ec8b7d1SJesse Brandeburg 			return 0;
20135ec8b7d1SJesse Brandeburg 		}
20145ec8b7d1SJesse Brandeburg 
20155ec8b7d1SJesse Brandeburg 		/* compute outer L3 header size */
20165ec8b7d1SJesse Brandeburg 		tunnel |= ((l4.hdr - ip.hdr) / 4) <<
201756184e01SJesse Brandeburg 			  IAVF_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
20185ec8b7d1SJesse Brandeburg 
20195ec8b7d1SJesse Brandeburg 		/* switch IP header pointer from outer to inner header */
20205ec8b7d1SJesse Brandeburg 		ip.hdr = skb_inner_network_header(skb);
20215ec8b7d1SJesse Brandeburg 
20225ec8b7d1SJesse Brandeburg 		/* compute tunnel header size */
20235ec8b7d1SJesse Brandeburg 		tunnel |= ((ip.hdr - l4.hdr) / 2) <<
202456184e01SJesse Brandeburg 			  IAVF_TXD_CTX_QW0_NATLEN_SHIFT;
20255ec8b7d1SJesse Brandeburg 
20265ec8b7d1SJesse Brandeburg 		/* indicate if we need to offload outer UDP header */
202756184e01SJesse Brandeburg 		if ((*tx_flags & IAVF_TX_FLAGS_TSO) &&
20285ec8b7d1SJesse Brandeburg 		    !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
20295ec8b7d1SJesse Brandeburg 		    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
203056184e01SJesse Brandeburg 			tunnel |= IAVF_TXD_CTX_QW0_L4T_CS_MASK;
20315ec8b7d1SJesse Brandeburg 
20325ec8b7d1SJesse Brandeburg 		/* record tunnel offload values */
20335ec8b7d1SJesse Brandeburg 		*cd_tunneling |= tunnel;
20345ec8b7d1SJesse Brandeburg 
20355ec8b7d1SJesse Brandeburg 		/* switch L4 header pointer from outer to inner */
20365ec8b7d1SJesse Brandeburg 		l4.hdr = skb_inner_transport_header(skb);
20375ec8b7d1SJesse Brandeburg 		l4_proto = 0;
20385ec8b7d1SJesse Brandeburg 
20395ec8b7d1SJesse Brandeburg 		/* reset type as we transition from outer to inner headers */
204056184e01SJesse Brandeburg 		*tx_flags &= ~(IAVF_TX_FLAGS_IPV4 | IAVF_TX_FLAGS_IPV6);
20415ec8b7d1SJesse Brandeburg 		if (ip.v4->version == 4)
204256184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_IPV4;
20435ec8b7d1SJesse Brandeburg 		if (ip.v6->version == 6)
204456184e01SJesse Brandeburg 			*tx_flags |= IAVF_TX_FLAGS_IPV6;
20455ec8b7d1SJesse Brandeburg 	}
20465ec8b7d1SJesse Brandeburg 
20475ec8b7d1SJesse Brandeburg 	/* Enable IP checksum offloads */
204856184e01SJesse Brandeburg 	if (*tx_flags & IAVF_TX_FLAGS_IPV4) {
20495ec8b7d1SJesse Brandeburg 		l4_proto = ip.v4->protocol;
20505ec8b7d1SJesse Brandeburg 		/* the stack computes the IP header already, the only time we
20515ec8b7d1SJesse Brandeburg 		 * need the hardware to recompute it is in the case of TSO.
20525ec8b7d1SJesse Brandeburg 		 */
205356184e01SJesse Brandeburg 		cmd |= (*tx_flags & IAVF_TX_FLAGS_TSO) ?
2054f1cad2ceSJesse Brandeburg 		       IAVF_TX_DESC_CMD_IIPT_IPV4_CSUM :
2055f1cad2ceSJesse Brandeburg 		       IAVF_TX_DESC_CMD_IIPT_IPV4;
205656184e01SJesse Brandeburg 	} else if (*tx_flags & IAVF_TX_FLAGS_IPV6) {
2057f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_IIPT_IPV6;
20585ec8b7d1SJesse Brandeburg 
20595ec8b7d1SJesse Brandeburg 		exthdr = ip.hdr + sizeof(*ip.v6);
20605ec8b7d1SJesse Brandeburg 		l4_proto = ip.v6->nexthdr;
20615ec8b7d1SJesse Brandeburg 		if (l4.hdr != exthdr)
20625ec8b7d1SJesse Brandeburg 			ipv6_skip_exthdr(skb, exthdr - skb->data,
20635ec8b7d1SJesse Brandeburg 					 &l4_proto, &frag_off);
20645ec8b7d1SJesse Brandeburg 	}
20655ec8b7d1SJesse Brandeburg 
20665ec8b7d1SJesse Brandeburg 	/* compute inner L3 header size */
2067f1cad2ceSJesse Brandeburg 	offset |= ((l4.hdr - ip.hdr) / 4) << IAVF_TX_DESC_LENGTH_IPLEN_SHIFT;
20685ec8b7d1SJesse Brandeburg 
20695ec8b7d1SJesse Brandeburg 	/* Enable L4 checksum offloads */
20705ec8b7d1SJesse Brandeburg 	switch (l4_proto) {
20715ec8b7d1SJesse Brandeburg 	case IPPROTO_TCP:
20725ec8b7d1SJesse Brandeburg 		/* enable checksum offloads */
2073f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_TCP;
2074f1cad2ceSJesse Brandeburg 		offset |= l4.tcp->doff << IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
20755ec8b7d1SJesse Brandeburg 		break;
20765ec8b7d1SJesse Brandeburg 	case IPPROTO_SCTP:
20775ec8b7d1SJesse Brandeburg 		/* enable SCTP checksum offload */
2078f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_SCTP;
20795ec8b7d1SJesse Brandeburg 		offset |= (sizeof(struct sctphdr) >> 2) <<
2080f1cad2ceSJesse Brandeburg 			  IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
20815ec8b7d1SJesse Brandeburg 		break;
20825ec8b7d1SJesse Brandeburg 	case IPPROTO_UDP:
20835ec8b7d1SJesse Brandeburg 		/* enable UDP checksum offload */
2084f1cad2ceSJesse Brandeburg 		cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_UDP;
20855ec8b7d1SJesse Brandeburg 		offset |= (sizeof(struct udphdr) >> 2) <<
2086f1cad2ceSJesse Brandeburg 			  IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
20875ec8b7d1SJesse Brandeburg 		break;
20885ec8b7d1SJesse Brandeburg 	default:
208956184e01SJesse Brandeburg 		if (*tx_flags & IAVF_TX_FLAGS_TSO)
20905ec8b7d1SJesse Brandeburg 			return -1;
20915ec8b7d1SJesse Brandeburg 		skb_checksum_help(skb);
20925ec8b7d1SJesse Brandeburg 		return 0;
20935ec8b7d1SJesse Brandeburg 	}
20945ec8b7d1SJesse Brandeburg 
20955ec8b7d1SJesse Brandeburg 	*td_cmd |= cmd;
20965ec8b7d1SJesse Brandeburg 	*td_offset |= offset;
20975ec8b7d1SJesse Brandeburg 
20985ec8b7d1SJesse Brandeburg 	return 1;
20995ec8b7d1SJesse Brandeburg }
21005ec8b7d1SJesse Brandeburg 
21015ec8b7d1SJesse Brandeburg /**
210256184e01SJesse Brandeburg  * iavf_create_tx_ctx Build the Tx context descriptor
21035ec8b7d1SJesse Brandeburg  * @tx_ring:  ring to create the descriptor on
21045ec8b7d1SJesse Brandeburg  * @cd_type_cmd_tso_mss: Quad Word 1
21055ec8b7d1SJesse Brandeburg  * @cd_tunneling: Quad Word 0 - bits 0-31
21065ec8b7d1SJesse Brandeburg  * @cd_l2tag2: Quad Word 0 - bits 32-63
21075ec8b7d1SJesse Brandeburg  **/
210856184e01SJesse Brandeburg static void iavf_create_tx_ctx(struct iavf_ring *tx_ring,
21095ec8b7d1SJesse Brandeburg 			       const u64 cd_type_cmd_tso_mss,
21105ec8b7d1SJesse Brandeburg 			       const u32 cd_tunneling, const u32 cd_l2tag2)
21115ec8b7d1SJesse Brandeburg {
211256184e01SJesse Brandeburg 	struct iavf_tx_context_desc *context_desc;
21135ec8b7d1SJesse Brandeburg 	int i = tx_ring->next_to_use;
21145ec8b7d1SJesse Brandeburg 
2115f1cad2ceSJesse Brandeburg 	if ((cd_type_cmd_tso_mss == IAVF_TX_DESC_DTYPE_CONTEXT) &&
21165ec8b7d1SJesse Brandeburg 	    !cd_tunneling && !cd_l2tag2)
21175ec8b7d1SJesse Brandeburg 		return;
21185ec8b7d1SJesse Brandeburg 
21195ec8b7d1SJesse Brandeburg 	/* grab the next descriptor */
2120f1cad2ceSJesse Brandeburg 	context_desc = IAVF_TX_CTXTDESC(tx_ring, i);
21215ec8b7d1SJesse Brandeburg 
21225ec8b7d1SJesse Brandeburg 	i++;
21235ec8b7d1SJesse Brandeburg 	tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
21245ec8b7d1SJesse Brandeburg 
21255ec8b7d1SJesse Brandeburg 	/* cpu_to_le32 and assign to struct fields */
21265ec8b7d1SJesse Brandeburg 	context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
21275ec8b7d1SJesse Brandeburg 	context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
21285ec8b7d1SJesse Brandeburg 	context_desc->rsvd = cpu_to_le16(0);
21295ec8b7d1SJesse Brandeburg 	context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
21305ec8b7d1SJesse Brandeburg }
21315ec8b7d1SJesse Brandeburg 
21325ec8b7d1SJesse Brandeburg /**
21335ec8b7d1SJesse Brandeburg  * __iavf_chk_linearize - Check if there are more than 8 buffers per packet
21345ec8b7d1SJesse Brandeburg  * @skb:      send buffer
21355ec8b7d1SJesse Brandeburg  *
21365ec8b7d1SJesse Brandeburg  * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
21375ec8b7d1SJesse Brandeburg  * and so we need to figure out the cases where we need to linearize the skb.
21385ec8b7d1SJesse Brandeburg  *
21395ec8b7d1SJesse Brandeburg  * For TSO we need to count the TSO header and segment payload separately.
21405ec8b7d1SJesse Brandeburg  * As such we need to check cases where we have 7 fragments or more as we
21415ec8b7d1SJesse Brandeburg  * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
21425ec8b7d1SJesse Brandeburg  * the segment payload in the first descriptor, and another 7 for the
21435ec8b7d1SJesse Brandeburg  * fragments.
21445ec8b7d1SJesse Brandeburg  **/
21455ec8b7d1SJesse Brandeburg bool __iavf_chk_linearize(struct sk_buff *skb)
21465ec8b7d1SJesse Brandeburg {
21475ec8b7d1SJesse Brandeburg 	const struct skb_frag_struct *frag, *stale;
21485ec8b7d1SJesse Brandeburg 	int nr_frags, sum;
21495ec8b7d1SJesse Brandeburg 
21505ec8b7d1SJesse Brandeburg 	/* no need to check if number of frags is less than 7 */
21515ec8b7d1SJesse Brandeburg 	nr_frags = skb_shinfo(skb)->nr_frags;
215256184e01SJesse Brandeburg 	if (nr_frags < (IAVF_MAX_BUFFER_TXD - 1))
21535ec8b7d1SJesse Brandeburg 		return false;
21545ec8b7d1SJesse Brandeburg 
21555ec8b7d1SJesse Brandeburg 	/* We need to walk through the list and validate that each group
21565ec8b7d1SJesse Brandeburg 	 * of 6 fragments totals at least gso_size.
21575ec8b7d1SJesse Brandeburg 	 */
215856184e01SJesse Brandeburg 	nr_frags -= IAVF_MAX_BUFFER_TXD - 2;
21595ec8b7d1SJesse Brandeburg 	frag = &skb_shinfo(skb)->frags[0];
21605ec8b7d1SJesse Brandeburg 
21615ec8b7d1SJesse Brandeburg 	/* Initialize size to the negative value of gso_size minus 1.  We
21625ec8b7d1SJesse Brandeburg 	 * use this as the worst case scenerio in which the frag ahead
21635ec8b7d1SJesse Brandeburg 	 * of us only provides one byte which is why we are limited to 6
21645ec8b7d1SJesse Brandeburg 	 * descriptors for a single transmit as the header and previous
21655ec8b7d1SJesse Brandeburg 	 * fragment are already consuming 2 descriptors.
21665ec8b7d1SJesse Brandeburg 	 */
21675ec8b7d1SJesse Brandeburg 	sum = 1 - skb_shinfo(skb)->gso_size;
21685ec8b7d1SJesse Brandeburg 
21695ec8b7d1SJesse Brandeburg 	/* Add size of frags 0 through 4 to create our initial sum */
21705ec8b7d1SJesse Brandeburg 	sum += skb_frag_size(frag++);
21715ec8b7d1SJesse Brandeburg 	sum += skb_frag_size(frag++);
21725ec8b7d1SJesse Brandeburg 	sum += skb_frag_size(frag++);
21735ec8b7d1SJesse Brandeburg 	sum += skb_frag_size(frag++);
21745ec8b7d1SJesse Brandeburg 	sum += skb_frag_size(frag++);
21755ec8b7d1SJesse Brandeburg 
21765ec8b7d1SJesse Brandeburg 	/* Walk through fragments adding latest fragment, testing it, and
21775ec8b7d1SJesse Brandeburg 	 * then removing stale fragments from the sum.
21785ec8b7d1SJesse Brandeburg 	 */
21795ec8b7d1SJesse Brandeburg 	for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
21805ec8b7d1SJesse Brandeburg 		int stale_size = skb_frag_size(stale);
21815ec8b7d1SJesse Brandeburg 
21825ec8b7d1SJesse Brandeburg 		sum += skb_frag_size(frag++);
21835ec8b7d1SJesse Brandeburg 
21845ec8b7d1SJesse Brandeburg 		/* The stale fragment may present us with a smaller
21855ec8b7d1SJesse Brandeburg 		 * descriptor than the actual fragment size. To account
21865ec8b7d1SJesse Brandeburg 		 * for that we need to remove all the data on the front and
21875ec8b7d1SJesse Brandeburg 		 * figure out what the remainder would be in the last
21885ec8b7d1SJesse Brandeburg 		 * descriptor associated with the fragment.
21895ec8b7d1SJesse Brandeburg 		 */
219056184e01SJesse Brandeburg 		if (stale_size > IAVF_MAX_DATA_PER_TXD) {
21915ec8b7d1SJesse Brandeburg 			int align_pad = -(stale->page_offset) &
219256184e01SJesse Brandeburg 					(IAVF_MAX_READ_REQ_SIZE - 1);
21935ec8b7d1SJesse Brandeburg 
21945ec8b7d1SJesse Brandeburg 			sum -= align_pad;
21955ec8b7d1SJesse Brandeburg 			stale_size -= align_pad;
21965ec8b7d1SJesse Brandeburg 
21975ec8b7d1SJesse Brandeburg 			do {
219856184e01SJesse Brandeburg 				sum -= IAVF_MAX_DATA_PER_TXD_ALIGNED;
219956184e01SJesse Brandeburg 				stale_size -= IAVF_MAX_DATA_PER_TXD_ALIGNED;
220056184e01SJesse Brandeburg 			} while (stale_size > IAVF_MAX_DATA_PER_TXD);
22015ec8b7d1SJesse Brandeburg 		}
22025ec8b7d1SJesse Brandeburg 
22035ec8b7d1SJesse Brandeburg 		/* if sum is negative we failed to make sufficient progress */
22045ec8b7d1SJesse Brandeburg 		if (sum < 0)
22055ec8b7d1SJesse Brandeburg 			return true;
22065ec8b7d1SJesse Brandeburg 
22075ec8b7d1SJesse Brandeburg 		if (!nr_frags--)
22085ec8b7d1SJesse Brandeburg 			break;
22095ec8b7d1SJesse Brandeburg 
22105ec8b7d1SJesse Brandeburg 		sum -= stale_size;
22115ec8b7d1SJesse Brandeburg 	}
22125ec8b7d1SJesse Brandeburg 
22135ec8b7d1SJesse Brandeburg 	return false;
22145ec8b7d1SJesse Brandeburg }
22155ec8b7d1SJesse Brandeburg 
22165ec8b7d1SJesse Brandeburg /**
22175ec8b7d1SJesse Brandeburg  * __iavf_maybe_stop_tx - 2nd level check for tx stop conditions
22185ec8b7d1SJesse Brandeburg  * @tx_ring: the ring to be checked
22195ec8b7d1SJesse Brandeburg  * @size:    the size buffer we want to assure is available
22205ec8b7d1SJesse Brandeburg  *
22215ec8b7d1SJesse Brandeburg  * Returns -EBUSY if a stop is needed, else 0
22225ec8b7d1SJesse Brandeburg  **/
222356184e01SJesse Brandeburg int __iavf_maybe_stop_tx(struct iavf_ring *tx_ring, int size)
22245ec8b7d1SJesse Brandeburg {
22255ec8b7d1SJesse Brandeburg 	netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
22265ec8b7d1SJesse Brandeburg 	/* Memory barrier before checking head and tail */
22275ec8b7d1SJesse Brandeburg 	smp_mb();
22285ec8b7d1SJesse Brandeburg 
22295ec8b7d1SJesse Brandeburg 	/* Check again in a case another CPU has just made room available. */
223056184e01SJesse Brandeburg 	if (likely(IAVF_DESC_UNUSED(tx_ring) < size))
22315ec8b7d1SJesse Brandeburg 		return -EBUSY;
22325ec8b7d1SJesse Brandeburg 
22335ec8b7d1SJesse Brandeburg 	/* A reprieve! - use start_queue because it doesn't call schedule */
22345ec8b7d1SJesse Brandeburg 	netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
22355ec8b7d1SJesse Brandeburg 	++tx_ring->tx_stats.restart_queue;
22365ec8b7d1SJesse Brandeburg 	return 0;
22375ec8b7d1SJesse Brandeburg }
22385ec8b7d1SJesse Brandeburg 
22395ec8b7d1SJesse Brandeburg /**
22405ec8b7d1SJesse Brandeburg  * iavf_tx_map - Build the Tx descriptor
22415ec8b7d1SJesse Brandeburg  * @tx_ring:  ring to send buffer on
22425ec8b7d1SJesse Brandeburg  * @skb:      send buffer
22435ec8b7d1SJesse Brandeburg  * @first:    first buffer info buffer to use
22445ec8b7d1SJesse Brandeburg  * @tx_flags: collected send information
22455ec8b7d1SJesse Brandeburg  * @hdr_len:  size of the packet header
22465ec8b7d1SJesse Brandeburg  * @td_cmd:   the command field in the descriptor
22475ec8b7d1SJesse Brandeburg  * @td_offset: offset for checksum or crc
22485ec8b7d1SJesse Brandeburg  **/
224956184e01SJesse Brandeburg static inline void iavf_tx_map(struct iavf_ring *tx_ring, struct sk_buff *skb,
225056184e01SJesse Brandeburg 			       struct iavf_tx_buffer *first, u32 tx_flags,
22515ec8b7d1SJesse Brandeburg 			       const u8 hdr_len, u32 td_cmd, u32 td_offset)
22525ec8b7d1SJesse Brandeburg {
22535ec8b7d1SJesse Brandeburg 	unsigned int data_len = skb->data_len;
22545ec8b7d1SJesse Brandeburg 	unsigned int size = skb_headlen(skb);
22555ec8b7d1SJesse Brandeburg 	struct skb_frag_struct *frag;
225656184e01SJesse Brandeburg 	struct iavf_tx_buffer *tx_bi;
225756184e01SJesse Brandeburg 	struct iavf_tx_desc *tx_desc;
22585ec8b7d1SJesse Brandeburg 	u16 i = tx_ring->next_to_use;
22595ec8b7d1SJesse Brandeburg 	u32 td_tag = 0;
22605ec8b7d1SJesse Brandeburg 	dma_addr_t dma;
22615ec8b7d1SJesse Brandeburg 
226256184e01SJesse Brandeburg 	if (tx_flags & IAVF_TX_FLAGS_HW_VLAN) {
2263f1cad2ceSJesse Brandeburg 		td_cmd |= IAVF_TX_DESC_CMD_IL2TAG1;
226456184e01SJesse Brandeburg 		td_tag = (tx_flags & IAVF_TX_FLAGS_VLAN_MASK) >>
226556184e01SJesse Brandeburg 			 IAVF_TX_FLAGS_VLAN_SHIFT;
22665ec8b7d1SJesse Brandeburg 	}
22675ec8b7d1SJesse Brandeburg 
22685ec8b7d1SJesse Brandeburg 	first->tx_flags = tx_flags;
22695ec8b7d1SJesse Brandeburg 
22705ec8b7d1SJesse Brandeburg 	dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
22715ec8b7d1SJesse Brandeburg 
2272f1cad2ceSJesse Brandeburg 	tx_desc = IAVF_TX_DESC(tx_ring, i);
22735ec8b7d1SJesse Brandeburg 	tx_bi = first;
22745ec8b7d1SJesse Brandeburg 
22755ec8b7d1SJesse Brandeburg 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
227656184e01SJesse Brandeburg 		unsigned int max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED;
22775ec8b7d1SJesse Brandeburg 
22785ec8b7d1SJesse Brandeburg 		if (dma_mapping_error(tx_ring->dev, dma))
22795ec8b7d1SJesse Brandeburg 			goto dma_error;
22805ec8b7d1SJesse Brandeburg 
22815ec8b7d1SJesse Brandeburg 		/* record length, and DMA address */
22825ec8b7d1SJesse Brandeburg 		dma_unmap_len_set(tx_bi, len, size);
22835ec8b7d1SJesse Brandeburg 		dma_unmap_addr_set(tx_bi, dma, dma);
22845ec8b7d1SJesse Brandeburg 
22855ec8b7d1SJesse Brandeburg 		/* align size to end of page */
228656184e01SJesse Brandeburg 		max_data += -dma & (IAVF_MAX_READ_REQ_SIZE - 1);
22875ec8b7d1SJesse Brandeburg 		tx_desc->buffer_addr = cpu_to_le64(dma);
22885ec8b7d1SJesse Brandeburg 
228956184e01SJesse Brandeburg 		while (unlikely(size > IAVF_MAX_DATA_PER_TXD)) {
22905ec8b7d1SJesse Brandeburg 			tx_desc->cmd_type_offset_bsz =
22915ec8b7d1SJesse Brandeburg 				build_ctob(td_cmd, td_offset,
22925ec8b7d1SJesse Brandeburg 					   max_data, td_tag);
22935ec8b7d1SJesse Brandeburg 
22945ec8b7d1SJesse Brandeburg 			tx_desc++;
22955ec8b7d1SJesse Brandeburg 			i++;
22965ec8b7d1SJesse Brandeburg 
22975ec8b7d1SJesse Brandeburg 			if (i == tx_ring->count) {
2298f1cad2ceSJesse Brandeburg 				tx_desc = IAVF_TX_DESC(tx_ring, 0);
22995ec8b7d1SJesse Brandeburg 				i = 0;
23005ec8b7d1SJesse Brandeburg 			}
23015ec8b7d1SJesse Brandeburg 
23025ec8b7d1SJesse Brandeburg 			dma += max_data;
23035ec8b7d1SJesse Brandeburg 			size -= max_data;
23045ec8b7d1SJesse Brandeburg 
230556184e01SJesse Brandeburg 			max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED;
23065ec8b7d1SJesse Brandeburg 			tx_desc->buffer_addr = cpu_to_le64(dma);
23075ec8b7d1SJesse Brandeburg 		}
23085ec8b7d1SJesse Brandeburg 
23095ec8b7d1SJesse Brandeburg 		if (likely(!data_len))
23105ec8b7d1SJesse Brandeburg 			break;
23115ec8b7d1SJesse Brandeburg 
23125ec8b7d1SJesse Brandeburg 		tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
23135ec8b7d1SJesse Brandeburg 							  size, td_tag);
23145ec8b7d1SJesse Brandeburg 
23155ec8b7d1SJesse Brandeburg 		tx_desc++;
23165ec8b7d1SJesse Brandeburg 		i++;
23175ec8b7d1SJesse Brandeburg 
23185ec8b7d1SJesse Brandeburg 		if (i == tx_ring->count) {
2319f1cad2ceSJesse Brandeburg 			tx_desc = IAVF_TX_DESC(tx_ring, 0);
23205ec8b7d1SJesse Brandeburg 			i = 0;
23215ec8b7d1SJesse Brandeburg 		}
23225ec8b7d1SJesse Brandeburg 
23235ec8b7d1SJesse Brandeburg 		size = skb_frag_size(frag);
23245ec8b7d1SJesse Brandeburg 		data_len -= size;
23255ec8b7d1SJesse Brandeburg 
23265ec8b7d1SJesse Brandeburg 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
23275ec8b7d1SJesse Brandeburg 				       DMA_TO_DEVICE);
23285ec8b7d1SJesse Brandeburg 
23295ec8b7d1SJesse Brandeburg 		tx_bi = &tx_ring->tx_bi[i];
23305ec8b7d1SJesse Brandeburg 	}
23315ec8b7d1SJesse Brandeburg 
23325ec8b7d1SJesse Brandeburg 	netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
23335ec8b7d1SJesse Brandeburg 
23345ec8b7d1SJesse Brandeburg 	i++;
23355ec8b7d1SJesse Brandeburg 	if (i == tx_ring->count)
23365ec8b7d1SJesse Brandeburg 		i = 0;
23375ec8b7d1SJesse Brandeburg 
23385ec8b7d1SJesse Brandeburg 	tx_ring->next_to_use = i;
23395ec8b7d1SJesse Brandeburg 
234056184e01SJesse Brandeburg 	iavf_maybe_stop_tx(tx_ring, DESC_NEEDED);
23415ec8b7d1SJesse Brandeburg 
23425ec8b7d1SJesse Brandeburg 	/* write last descriptor with RS and EOP bits */
234356184e01SJesse Brandeburg 	td_cmd |= IAVF_TXD_CMD;
23445ec8b7d1SJesse Brandeburg 	tx_desc->cmd_type_offset_bsz =
23455ec8b7d1SJesse Brandeburg 			build_ctob(td_cmd, td_offset, size, td_tag);
23465ec8b7d1SJesse Brandeburg 
23475ec8b7d1SJesse Brandeburg 	/* Force memory writes to complete before letting h/w know there
23485ec8b7d1SJesse Brandeburg 	 * are new descriptors to fetch.
23495ec8b7d1SJesse Brandeburg 	 *
23505ec8b7d1SJesse Brandeburg 	 * We also use this memory barrier to make certain all of the
23515ec8b7d1SJesse Brandeburg 	 * status bits have been updated before next_to_watch is written.
23525ec8b7d1SJesse Brandeburg 	 */
23535ec8b7d1SJesse Brandeburg 	wmb();
23545ec8b7d1SJesse Brandeburg 
23555ec8b7d1SJesse Brandeburg 	/* set next_to_watch value indicating a packet is present */
23565ec8b7d1SJesse Brandeburg 	first->next_to_watch = tx_desc;
23575ec8b7d1SJesse Brandeburg 
23585ec8b7d1SJesse Brandeburg 	/* notify HW of packet */
23595ec8b7d1SJesse Brandeburg 	if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
23605ec8b7d1SJesse Brandeburg 		writel(i, tx_ring->tail);
23615ec8b7d1SJesse Brandeburg 
23625ec8b7d1SJesse Brandeburg 		/* we need this if more than one processor can write to our tail
23635ec8b7d1SJesse Brandeburg 		 * at a time, it synchronizes IO on IA64/Altix systems
23645ec8b7d1SJesse Brandeburg 		 */
23655ec8b7d1SJesse Brandeburg 		mmiowb();
23665ec8b7d1SJesse Brandeburg 	}
23675ec8b7d1SJesse Brandeburg 
23685ec8b7d1SJesse Brandeburg 	return;
23695ec8b7d1SJesse Brandeburg 
23705ec8b7d1SJesse Brandeburg dma_error:
23715ec8b7d1SJesse Brandeburg 	dev_info(tx_ring->dev, "TX DMA map failed\n");
23725ec8b7d1SJesse Brandeburg 
23735ec8b7d1SJesse Brandeburg 	/* clear dma mappings for failed tx_bi map */
23745ec8b7d1SJesse Brandeburg 	for (;;) {
23755ec8b7d1SJesse Brandeburg 		tx_bi = &tx_ring->tx_bi[i];
237656184e01SJesse Brandeburg 		iavf_unmap_and_free_tx_resource(tx_ring, tx_bi);
23775ec8b7d1SJesse Brandeburg 		if (tx_bi == first)
23785ec8b7d1SJesse Brandeburg 			break;
23795ec8b7d1SJesse Brandeburg 		if (i == 0)
23805ec8b7d1SJesse Brandeburg 			i = tx_ring->count;
23815ec8b7d1SJesse Brandeburg 		i--;
23825ec8b7d1SJesse Brandeburg 	}
23835ec8b7d1SJesse Brandeburg 
23845ec8b7d1SJesse Brandeburg 	tx_ring->next_to_use = i;
23855ec8b7d1SJesse Brandeburg }
23865ec8b7d1SJesse Brandeburg 
23875ec8b7d1SJesse Brandeburg /**
238856184e01SJesse Brandeburg  * iavf_xmit_frame_ring - Sends buffer on Tx ring
23895ec8b7d1SJesse Brandeburg  * @skb:     send buffer
23905ec8b7d1SJesse Brandeburg  * @tx_ring: ring to send buffer on
23915ec8b7d1SJesse Brandeburg  *
23925ec8b7d1SJesse Brandeburg  * Returns NETDEV_TX_OK if sent, else an error code
23935ec8b7d1SJesse Brandeburg  **/
239456184e01SJesse Brandeburg static netdev_tx_t iavf_xmit_frame_ring(struct sk_buff *skb,
239556184e01SJesse Brandeburg 					struct iavf_ring *tx_ring)
23965ec8b7d1SJesse Brandeburg {
2397f1cad2ceSJesse Brandeburg 	u64 cd_type_cmd_tso_mss = IAVF_TX_DESC_DTYPE_CONTEXT;
23985ec8b7d1SJesse Brandeburg 	u32 cd_tunneling = 0, cd_l2tag2 = 0;
239956184e01SJesse Brandeburg 	struct iavf_tx_buffer *first;
24005ec8b7d1SJesse Brandeburg 	u32 td_offset = 0;
24015ec8b7d1SJesse Brandeburg 	u32 tx_flags = 0;
24025ec8b7d1SJesse Brandeburg 	__be16 protocol;
24035ec8b7d1SJesse Brandeburg 	u32 td_cmd = 0;
24045ec8b7d1SJesse Brandeburg 	u8 hdr_len = 0;
24055ec8b7d1SJesse Brandeburg 	int tso, count;
24065ec8b7d1SJesse Brandeburg 
24075ec8b7d1SJesse Brandeburg 	/* prefetch the data, we'll need it later */
24085ec8b7d1SJesse Brandeburg 	prefetch(skb->data);
24095ec8b7d1SJesse Brandeburg 
2410ad64ed8bSJesse Brandeburg 	iavf_trace(xmit_frame_ring, skb, tx_ring);
24115ec8b7d1SJesse Brandeburg 
241256184e01SJesse Brandeburg 	count = iavf_xmit_descriptor_count(skb);
241356184e01SJesse Brandeburg 	if (iavf_chk_linearize(skb, count)) {
24145ec8b7d1SJesse Brandeburg 		if (__skb_linearize(skb)) {
24155ec8b7d1SJesse Brandeburg 			dev_kfree_skb_any(skb);
24165ec8b7d1SJesse Brandeburg 			return NETDEV_TX_OK;
24175ec8b7d1SJesse Brandeburg 		}
241856184e01SJesse Brandeburg 		count = iavf_txd_use_count(skb->len);
24195ec8b7d1SJesse Brandeburg 		tx_ring->tx_stats.tx_linearize++;
24205ec8b7d1SJesse Brandeburg 	}
24215ec8b7d1SJesse Brandeburg 
242256184e01SJesse Brandeburg 	/* need: 1 descriptor per page * PAGE_SIZE/IAVF_MAX_DATA_PER_TXD,
242356184e01SJesse Brandeburg 	 *       + 1 desc for skb_head_len/IAVF_MAX_DATA_PER_TXD,
24245ec8b7d1SJesse Brandeburg 	 *       + 4 desc gap to avoid the cache line where head is,
24255ec8b7d1SJesse Brandeburg 	 *       + 1 desc for context descriptor,
24265ec8b7d1SJesse Brandeburg 	 * otherwise try next time
24275ec8b7d1SJesse Brandeburg 	 */
242856184e01SJesse Brandeburg 	if (iavf_maybe_stop_tx(tx_ring, count + 4 + 1)) {
24295ec8b7d1SJesse Brandeburg 		tx_ring->tx_stats.tx_busy++;
24305ec8b7d1SJesse Brandeburg 		return NETDEV_TX_BUSY;
24315ec8b7d1SJesse Brandeburg 	}
24325ec8b7d1SJesse Brandeburg 
24335ec8b7d1SJesse Brandeburg 	/* record the location of the first descriptor for this packet */
24345ec8b7d1SJesse Brandeburg 	first = &tx_ring->tx_bi[tx_ring->next_to_use];
24355ec8b7d1SJesse Brandeburg 	first->skb = skb;
24365ec8b7d1SJesse Brandeburg 	first->bytecount = skb->len;
24375ec8b7d1SJesse Brandeburg 	first->gso_segs = 1;
24385ec8b7d1SJesse Brandeburg 
24395ec8b7d1SJesse Brandeburg 	/* prepare the xmit flags */
24405ec8b7d1SJesse Brandeburg 	if (iavf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
24415ec8b7d1SJesse Brandeburg 		goto out_drop;
24425ec8b7d1SJesse Brandeburg 
24435ec8b7d1SJesse Brandeburg 	/* obtain protocol of skb */
24445ec8b7d1SJesse Brandeburg 	protocol = vlan_get_protocol(skb);
24455ec8b7d1SJesse Brandeburg 
24465ec8b7d1SJesse Brandeburg 	/* setup IPv4/IPv6 offloads */
24475ec8b7d1SJesse Brandeburg 	if (protocol == htons(ETH_P_IP))
244856184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_IPV4;
24495ec8b7d1SJesse Brandeburg 	else if (protocol == htons(ETH_P_IPV6))
245056184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_IPV6;
24515ec8b7d1SJesse Brandeburg 
245256184e01SJesse Brandeburg 	tso = iavf_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
24535ec8b7d1SJesse Brandeburg 
24545ec8b7d1SJesse Brandeburg 	if (tso < 0)
24555ec8b7d1SJesse Brandeburg 		goto out_drop;
24565ec8b7d1SJesse Brandeburg 	else if (tso)
245756184e01SJesse Brandeburg 		tx_flags |= IAVF_TX_FLAGS_TSO;
24585ec8b7d1SJesse Brandeburg 
24595ec8b7d1SJesse Brandeburg 	/* Always offload the checksum, since it's in the data descriptor */
246056184e01SJesse Brandeburg 	tso = iavf_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
24615ec8b7d1SJesse Brandeburg 				  tx_ring, &cd_tunneling);
24625ec8b7d1SJesse Brandeburg 	if (tso < 0)
24635ec8b7d1SJesse Brandeburg 		goto out_drop;
24645ec8b7d1SJesse Brandeburg 
24655ec8b7d1SJesse Brandeburg 	skb_tx_timestamp(skb);
24665ec8b7d1SJesse Brandeburg 
24675ec8b7d1SJesse Brandeburg 	/* always enable CRC insertion offload */
2468f1cad2ceSJesse Brandeburg 	td_cmd |= IAVF_TX_DESC_CMD_ICRC;
24695ec8b7d1SJesse Brandeburg 
247056184e01SJesse Brandeburg 	iavf_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
24715ec8b7d1SJesse Brandeburg 			   cd_tunneling, cd_l2tag2);
24725ec8b7d1SJesse Brandeburg 
24735ec8b7d1SJesse Brandeburg 	iavf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
24745ec8b7d1SJesse Brandeburg 		    td_cmd, td_offset);
24755ec8b7d1SJesse Brandeburg 
24765ec8b7d1SJesse Brandeburg 	return NETDEV_TX_OK;
24775ec8b7d1SJesse Brandeburg 
24785ec8b7d1SJesse Brandeburg out_drop:
2479ad64ed8bSJesse Brandeburg 	iavf_trace(xmit_frame_ring_drop, first->skb, tx_ring);
24805ec8b7d1SJesse Brandeburg 	dev_kfree_skb_any(first->skb);
24815ec8b7d1SJesse Brandeburg 	first->skb = NULL;
24825ec8b7d1SJesse Brandeburg 	return NETDEV_TX_OK;
24835ec8b7d1SJesse Brandeburg }
24845ec8b7d1SJesse Brandeburg 
24855ec8b7d1SJesse Brandeburg /**
24865ec8b7d1SJesse Brandeburg  * iavf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
24875ec8b7d1SJesse Brandeburg  * @skb:    send buffer
24885ec8b7d1SJesse Brandeburg  * @netdev: network interface device structure
24895ec8b7d1SJesse Brandeburg  *
24905ec8b7d1SJesse Brandeburg  * Returns NETDEV_TX_OK if sent, else an error code
24915ec8b7d1SJesse Brandeburg  **/
24925ec8b7d1SJesse Brandeburg netdev_tx_t iavf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
24935ec8b7d1SJesse Brandeburg {
24945ec8b7d1SJesse Brandeburg 	struct iavf_adapter *adapter = netdev_priv(netdev);
249556184e01SJesse Brandeburg 	struct iavf_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
24965ec8b7d1SJesse Brandeburg 
24975ec8b7d1SJesse Brandeburg 	/* hardware can't handle really short frames, hardware padding works
24985ec8b7d1SJesse Brandeburg 	 * beyond this point
24995ec8b7d1SJesse Brandeburg 	 */
250056184e01SJesse Brandeburg 	if (unlikely(skb->len < IAVF_MIN_TX_LEN)) {
250156184e01SJesse Brandeburg 		if (skb_pad(skb, IAVF_MIN_TX_LEN - skb->len))
25025ec8b7d1SJesse Brandeburg 			return NETDEV_TX_OK;
250356184e01SJesse Brandeburg 		skb->len = IAVF_MIN_TX_LEN;
250456184e01SJesse Brandeburg 		skb_set_tail_pointer(skb, IAVF_MIN_TX_LEN);
25055ec8b7d1SJesse Brandeburg 	}
25065ec8b7d1SJesse Brandeburg 
250756184e01SJesse Brandeburg 	return iavf_xmit_frame_ring(skb, tx_ring);
25085ec8b7d1SJesse Brandeburg }
2509