162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/* Copyright(c) 2013 - 2018 Intel Corporation. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#ifndef _IAVF_TXRX_H_
562306a36Sopenharmony_ci#define _IAVF_TXRX_H_
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci/* Interrupt Throttling and Rate Limiting Goodies */
862306a36Sopenharmony_ci#define IAVF_DEFAULT_IRQ_WORK      256
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/* The datasheet for the X710 and XL710 indicate that the maximum value for
1162306a36Sopenharmony_ci * the ITR is 8160usec which is then called out as 0xFF0 with a 2usec
1262306a36Sopenharmony_ci * resolution. 8160 is 0x1FE0 when written out in hex. So instead of storing
1362306a36Sopenharmony_ci * the register value which is divided by 2 lets use the actual values and
1462306a36Sopenharmony_ci * avoid an excessive amount of translation.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci#define IAVF_ITR_DYNAMIC	0x8000	/* use top bit as a flag */
1762306a36Sopenharmony_ci#define IAVF_ITR_MASK		0x1FFE	/* mask for ITR register value */
1862306a36Sopenharmony_ci#define IAVF_ITR_100K		    10	/* all values below must be even */
1962306a36Sopenharmony_ci#define IAVF_ITR_50K		    20
2062306a36Sopenharmony_ci#define IAVF_ITR_20K		    50
2162306a36Sopenharmony_ci#define IAVF_ITR_18K		    60
2262306a36Sopenharmony_ci#define IAVF_ITR_8K		   122
2362306a36Sopenharmony_ci#define IAVF_MAX_ITR		  8160	/* maximum value as per datasheet */
2462306a36Sopenharmony_ci#define ITR_TO_REG(setting) ((setting) & ~IAVF_ITR_DYNAMIC)
2562306a36Sopenharmony_ci#define ITR_REG_ALIGN(setting) __ALIGN_MASK(setting, ~IAVF_ITR_MASK)
2662306a36Sopenharmony_ci#define ITR_IS_DYNAMIC(setting) (!!((setting) & IAVF_ITR_DYNAMIC))
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define IAVF_ITR_RX_DEF		(IAVF_ITR_20K | IAVF_ITR_DYNAMIC)
2962306a36Sopenharmony_ci#define IAVF_ITR_TX_DEF		(IAVF_ITR_20K | IAVF_ITR_DYNAMIC)
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* 0x40 is the enable bit for interrupt rate limiting, and must be set if
3262306a36Sopenharmony_ci * the value of the rate limit is non-zero
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci#define INTRL_ENA                  BIT(6)
3562306a36Sopenharmony_ci#define IAVF_MAX_INTRL             0x3B    /* reg uses 4 usec resolution */
3662306a36Sopenharmony_ci#define INTRL_REG_TO_USEC(intrl) ((intrl & ~INTRL_ENA) << 2)
3762306a36Sopenharmony_ci#define INTRL_USEC_TO_REG(set) ((set) ? ((set) >> 2) | INTRL_ENA : 0)
3862306a36Sopenharmony_ci#define IAVF_INTRL_8K              125     /* 8000 ints/sec */
3962306a36Sopenharmony_ci#define IAVF_INTRL_62K             16      /* 62500 ints/sec */
4062306a36Sopenharmony_ci#define IAVF_INTRL_83K             12      /* 83333 ints/sec */
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define IAVF_QUEUE_END_OF_LIST 0x7FF
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/* this enum matches hardware bits and is meant to be used by DYN_CTLN
4562306a36Sopenharmony_ci * registers and QINT registers or more generally anywhere in the manual
4662306a36Sopenharmony_ci * mentioning ITR_INDX, ITR_NONE cannot be used as an index 'n' into any
4762306a36Sopenharmony_ci * register but instead is a special value meaning "don't update" ITR0/1/2.
4862306a36Sopenharmony_ci */
4962306a36Sopenharmony_cienum iavf_dyn_idx_t {
5062306a36Sopenharmony_ci	IAVF_IDX_ITR0 = 0,
5162306a36Sopenharmony_ci	IAVF_IDX_ITR1 = 1,
5262306a36Sopenharmony_ci	IAVF_IDX_ITR2 = 2,
5362306a36Sopenharmony_ci	IAVF_ITR_NONE = 3	/* ITR_NONE must not be used as an index */
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/* these are indexes into ITRN registers */
5762306a36Sopenharmony_ci#define IAVF_RX_ITR    IAVF_IDX_ITR0
5862306a36Sopenharmony_ci#define IAVF_TX_ITR    IAVF_IDX_ITR1
5962306a36Sopenharmony_ci#define IAVF_PE_ITR    IAVF_IDX_ITR2
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/* Supported RSS offloads */
6262306a36Sopenharmony_ci#define IAVF_DEFAULT_RSS_HENA ( \
6362306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_UDP) | \
6462306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_SCTP) | \
6562306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_TCP) | \
6662306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_OTHER) | \
6762306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_FRAG_IPV4) | \
6862306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_UDP) | \
6962306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_TCP) | \
7062306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_SCTP) | \
7162306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_OTHER) | \
7262306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_FRAG_IPV6) | \
7362306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_L2_PAYLOAD))
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#define IAVF_DEFAULT_RSS_HENA_EXPANDED (IAVF_DEFAULT_RSS_HENA | \
7662306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK) | \
7762306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) | \
7862306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) | \
7962306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK) | \
8062306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) | \
8162306a36Sopenharmony_ci	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP))
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/* Supported Rx Buffer Sizes (a multiple of 128) */
8462306a36Sopenharmony_ci#define IAVF_RXBUFFER_256   256
8562306a36Sopenharmony_ci#define IAVF_RXBUFFER_1536  1536  /* 128B aligned standard Ethernet frame */
8662306a36Sopenharmony_ci#define IAVF_RXBUFFER_2048  2048
8762306a36Sopenharmony_ci#define IAVF_RXBUFFER_3072  3072  /* Used for large frames w/ padding */
8862306a36Sopenharmony_ci#define IAVF_MAX_RXBUFFER   9728  /* largest size for single descriptor */
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/* NOTE: netdev_alloc_skb reserves up to 64 bytes, NET_IP_ALIGN means we
9162306a36Sopenharmony_ci * reserve 2 more, and skb_shared_info adds an additional 384 bytes more,
9262306a36Sopenharmony_ci * this adds up to 512 bytes of extra data meaning the smallest allocation
9362306a36Sopenharmony_ci * we could have is 1K.
9462306a36Sopenharmony_ci * i.e. RXBUFFER_256 --> 960 byte skb (size-1024 slab)
9562306a36Sopenharmony_ci * i.e. RXBUFFER_512 --> 1216 byte skb (size-2048 slab)
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci#define IAVF_RX_HDR_SIZE IAVF_RXBUFFER_256
9862306a36Sopenharmony_ci#define IAVF_PACKET_HDR_PAD (ETH_HLEN + ETH_FCS_LEN + (VLAN_HLEN * 2))
9962306a36Sopenharmony_ci#define iavf_rx_desc iavf_32byte_rx_desc
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#define IAVF_RX_DMA_ATTR \
10262306a36Sopenharmony_ci	(DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/* Attempt to maximize the headroom available for incoming frames.  We
10562306a36Sopenharmony_ci * use a 2K buffer for receives and need 1536/1534 to store the data for
10662306a36Sopenharmony_ci * the frame.  This leaves us with 512 bytes of room.  From that we need
10762306a36Sopenharmony_ci * to deduct the space needed for the shared info and the padding needed
10862306a36Sopenharmony_ci * to IP align the frame.
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * Note: For cache line sizes 256 or larger this value is going to end
11162306a36Sopenharmony_ci *	 up negative.  In these cases we should fall back to the legacy
11262306a36Sopenharmony_ci *	 receive path.
11362306a36Sopenharmony_ci */
11462306a36Sopenharmony_ci#if (PAGE_SIZE < 8192)
11562306a36Sopenharmony_ci#define IAVF_2K_TOO_SMALL_WITH_PADDING \
11662306a36Sopenharmony_ci((NET_SKB_PAD + IAVF_RXBUFFER_1536) > SKB_WITH_OVERHEAD(IAVF_RXBUFFER_2048))
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic inline int iavf_compute_pad(int rx_buf_len)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	int page_size, pad_size;
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	page_size = ALIGN(rx_buf_len, PAGE_SIZE / 2);
12362306a36Sopenharmony_ci	pad_size = SKB_WITH_OVERHEAD(page_size) - rx_buf_len;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	return pad_size;
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistatic inline int iavf_skb_pad(void)
12962306a36Sopenharmony_ci{
13062306a36Sopenharmony_ci	int rx_buf_len;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	/* If a 2K buffer cannot handle a standard Ethernet frame then
13362306a36Sopenharmony_ci	 * optimize padding for a 3K buffer instead of a 1.5K buffer.
13462306a36Sopenharmony_ci	 *
13562306a36Sopenharmony_ci	 * For a 3K buffer we need to add enough padding to allow for
13662306a36Sopenharmony_ci	 * tailroom due to NET_IP_ALIGN possibly shifting us out of
13762306a36Sopenharmony_ci	 * cache-line alignment.
13862306a36Sopenharmony_ci	 */
13962306a36Sopenharmony_ci	if (IAVF_2K_TOO_SMALL_WITH_PADDING)
14062306a36Sopenharmony_ci		rx_buf_len = IAVF_RXBUFFER_3072 + SKB_DATA_ALIGN(NET_IP_ALIGN);
14162306a36Sopenharmony_ci	else
14262306a36Sopenharmony_ci		rx_buf_len = IAVF_RXBUFFER_1536;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	/* if needed make room for NET_IP_ALIGN */
14562306a36Sopenharmony_ci	rx_buf_len -= NET_IP_ALIGN;
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	return iavf_compute_pad(rx_buf_len);
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci#define IAVF_SKB_PAD iavf_skb_pad()
15162306a36Sopenharmony_ci#else
15262306a36Sopenharmony_ci#define IAVF_2K_TOO_SMALL_WITH_PADDING false
15362306a36Sopenharmony_ci#define IAVF_SKB_PAD (NET_SKB_PAD + NET_IP_ALIGN)
15462306a36Sopenharmony_ci#endif
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci/**
15762306a36Sopenharmony_ci * iavf_test_staterr - tests bits in Rx descriptor status and error fields
15862306a36Sopenharmony_ci * @rx_desc: pointer to receive descriptor (in le64 format)
15962306a36Sopenharmony_ci * @stat_err_bits: value to mask
16062306a36Sopenharmony_ci *
16162306a36Sopenharmony_ci * This function does some fast chicanery in order to return the
16262306a36Sopenharmony_ci * value of the mask which is really only used for boolean tests.
16362306a36Sopenharmony_ci * The status_error_len doesn't need to be shifted because it begins
16462306a36Sopenharmony_ci * at offset zero.
16562306a36Sopenharmony_ci */
16662306a36Sopenharmony_cistatic inline bool iavf_test_staterr(union iavf_rx_desc *rx_desc,
16762306a36Sopenharmony_ci				     const u64 stat_err_bits)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	return !!(rx_desc->wb.qword1.status_error_len &
17062306a36Sopenharmony_ci		  cpu_to_le64(stat_err_bits));
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci/* How many Rx Buffers do we bundle into one write to the hardware ? */
17462306a36Sopenharmony_ci#define IAVF_RX_INCREMENT(r, i) \
17562306a36Sopenharmony_ci	do {					\
17662306a36Sopenharmony_ci		(i)++;				\
17762306a36Sopenharmony_ci		if ((i) == (r)->count)		\
17862306a36Sopenharmony_ci			i = 0;			\
17962306a36Sopenharmony_ci		r->next_to_clean = i;		\
18062306a36Sopenharmony_ci	} while (0)
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci#define IAVF_RX_NEXT_DESC(r, i, n)		\
18362306a36Sopenharmony_ci	do {					\
18462306a36Sopenharmony_ci		(i)++;				\
18562306a36Sopenharmony_ci		if ((i) == (r)->count)		\
18662306a36Sopenharmony_ci			i = 0;			\
18762306a36Sopenharmony_ci		(n) = IAVF_RX_DESC((r), (i));	\
18862306a36Sopenharmony_ci	} while (0)
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci#define IAVF_RX_NEXT_DESC_PREFETCH(r, i, n)		\
19162306a36Sopenharmony_ci	do {						\
19262306a36Sopenharmony_ci		IAVF_RX_NEXT_DESC((r), (i), (n));	\
19362306a36Sopenharmony_ci		prefetch((n));				\
19462306a36Sopenharmony_ci	} while (0)
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci#define IAVF_MAX_BUFFER_TXD	8
19762306a36Sopenharmony_ci#define IAVF_MIN_TX_LEN		17
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci/* The size limit for a transmit buffer in a descriptor is (16K - 1).
20062306a36Sopenharmony_ci * In order to align with the read requests we will align the value to
20162306a36Sopenharmony_ci * the nearest 4K which represents our maximum read request size.
20262306a36Sopenharmony_ci */
20362306a36Sopenharmony_ci#define IAVF_MAX_READ_REQ_SIZE		4096
20462306a36Sopenharmony_ci#define IAVF_MAX_DATA_PER_TXD		(16 * 1024 - 1)
20562306a36Sopenharmony_ci#define IAVF_MAX_DATA_PER_TXD_ALIGNED \
20662306a36Sopenharmony_ci	(IAVF_MAX_DATA_PER_TXD & ~(IAVF_MAX_READ_REQ_SIZE - 1))
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci/**
20962306a36Sopenharmony_ci * iavf_txd_use_count  - estimate the number of descriptors needed for Tx
21062306a36Sopenharmony_ci * @size: transmit request size in bytes
21162306a36Sopenharmony_ci *
21262306a36Sopenharmony_ci * Due to hardware alignment restrictions (4K alignment), we need to
21362306a36Sopenharmony_ci * assume that we can have no more than 12K of data per descriptor, even
21462306a36Sopenharmony_ci * though each descriptor can take up to 16K - 1 bytes of aligned memory.
21562306a36Sopenharmony_ci * Thus, we need to divide by 12K. But division is slow! Instead,
21662306a36Sopenharmony_ci * we decompose the operation into shifts and one relatively cheap
21762306a36Sopenharmony_ci * multiply operation.
21862306a36Sopenharmony_ci *
21962306a36Sopenharmony_ci * To divide by 12K, we first divide by 4K, then divide by 3:
22062306a36Sopenharmony_ci *     To divide by 4K, shift right by 12 bits
22162306a36Sopenharmony_ci *     To divide by 3, multiply by 85, then divide by 256
22262306a36Sopenharmony_ci *     (Divide by 256 is done by shifting right by 8 bits)
22362306a36Sopenharmony_ci * Finally, we add one to round up. Because 256 isn't an exact multiple of
22462306a36Sopenharmony_ci * 3, we'll underestimate near each multiple of 12K. This is actually more
22562306a36Sopenharmony_ci * accurate as we have 4K - 1 of wiggle room that we can fit into the last
22662306a36Sopenharmony_ci * segment.  For our purposes this is accurate out to 1M which is orders of
22762306a36Sopenharmony_ci * magnitude greater than our largest possible GSO size.
22862306a36Sopenharmony_ci *
22962306a36Sopenharmony_ci * This would then be implemented as:
23062306a36Sopenharmony_ci *     return (((size >> 12) * 85) >> 8) + 1;
23162306a36Sopenharmony_ci *
23262306a36Sopenharmony_ci * Since multiplication and division are commutative, we can reorder
23362306a36Sopenharmony_ci * operations into:
23462306a36Sopenharmony_ci *     return ((size * 85) >> 20) + 1;
23562306a36Sopenharmony_ci */
23662306a36Sopenharmony_cistatic inline unsigned int iavf_txd_use_count(unsigned int size)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	return ((size * 85) >> 20) + 1;
23962306a36Sopenharmony_ci}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci/* Tx Descriptors needed, worst case */
24262306a36Sopenharmony_ci#define DESC_NEEDED (MAX_SKB_FRAGS + 6)
24362306a36Sopenharmony_ci#define IAVF_MIN_DESC_PENDING	4
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci#define IAVF_TX_FLAGS_HW_VLAN			BIT(1)
24662306a36Sopenharmony_ci#define IAVF_TX_FLAGS_SW_VLAN			BIT(2)
24762306a36Sopenharmony_ci#define IAVF_TX_FLAGS_TSO			BIT(3)
24862306a36Sopenharmony_ci#define IAVF_TX_FLAGS_IPV4			BIT(4)
24962306a36Sopenharmony_ci#define IAVF_TX_FLAGS_IPV6			BIT(5)
25062306a36Sopenharmony_ci#define IAVF_TX_FLAGS_FCCRC			BIT(6)
25162306a36Sopenharmony_ci#define IAVF_TX_FLAGS_FSO			BIT(7)
25262306a36Sopenharmony_ci#define IAVF_TX_FLAGS_FD_SB			BIT(9)
25362306a36Sopenharmony_ci#define IAVF_TX_FLAGS_VXLAN_TUNNEL		BIT(10)
25462306a36Sopenharmony_ci#define IAVF_TX_FLAGS_HW_OUTER_SINGLE_VLAN	BIT(11)
25562306a36Sopenharmony_ci#define IAVF_TX_FLAGS_VLAN_MASK			0xffff0000
25662306a36Sopenharmony_ci#define IAVF_TX_FLAGS_VLAN_PRIO_MASK		0xe0000000
25762306a36Sopenharmony_ci#define IAVF_TX_FLAGS_VLAN_PRIO_SHIFT		29
25862306a36Sopenharmony_ci#define IAVF_TX_FLAGS_VLAN_SHIFT		16
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_cistruct iavf_tx_buffer {
26162306a36Sopenharmony_ci	struct iavf_tx_desc *next_to_watch;
26262306a36Sopenharmony_ci	union {
26362306a36Sopenharmony_ci		struct sk_buff *skb;
26462306a36Sopenharmony_ci		void *raw_buf;
26562306a36Sopenharmony_ci	};
26662306a36Sopenharmony_ci	unsigned int bytecount;
26762306a36Sopenharmony_ci	unsigned short gso_segs;
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	DEFINE_DMA_UNMAP_ADDR(dma);
27062306a36Sopenharmony_ci	DEFINE_DMA_UNMAP_LEN(len);
27162306a36Sopenharmony_ci	u32 tx_flags;
27262306a36Sopenharmony_ci};
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_cistruct iavf_rx_buffer {
27562306a36Sopenharmony_ci	dma_addr_t dma;
27662306a36Sopenharmony_ci	struct page *page;
27762306a36Sopenharmony_ci#if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
27862306a36Sopenharmony_ci	__u32 page_offset;
27962306a36Sopenharmony_ci#else
28062306a36Sopenharmony_ci	__u16 page_offset;
28162306a36Sopenharmony_ci#endif
28262306a36Sopenharmony_ci	__u16 pagecnt_bias;
28362306a36Sopenharmony_ci};
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_cistruct iavf_queue_stats {
28662306a36Sopenharmony_ci	u64 packets;
28762306a36Sopenharmony_ci	u64 bytes;
28862306a36Sopenharmony_ci};
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_cistruct iavf_tx_queue_stats {
29162306a36Sopenharmony_ci	u64 restart_queue;
29262306a36Sopenharmony_ci	u64 tx_busy;
29362306a36Sopenharmony_ci	u64 tx_done_old;
29462306a36Sopenharmony_ci	u64 tx_linearize;
29562306a36Sopenharmony_ci	u64 tx_force_wb;
29662306a36Sopenharmony_ci	int prev_pkt_ctr;
29762306a36Sopenharmony_ci	u64 tx_lost_interrupt;
29862306a36Sopenharmony_ci};
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_cistruct iavf_rx_queue_stats {
30162306a36Sopenharmony_ci	u64 non_eop_descs;
30262306a36Sopenharmony_ci	u64 alloc_page_failed;
30362306a36Sopenharmony_ci	u64 alloc_buff_failed;
30462306a36Sopenharmony_ci	u64 page_reuse_count;
30562306a36Sopenharmony_ci	u64 realloc_count;
30662306a36Sopenharmony_ci};
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_cienum iavf_ring_state_t {
30962306a36Sopenharmony_ci	__IAVF_TX_FDIR_INIT_DONE,
31062306a36Sopenharmony_ci	__IAVF_TX_XPS_INIT_DONE,
31162306a36Sopenharmony_ci	__IAVF_RING_STATE_NBITS /* must be last */
31262306a36Sopenharmony_ci};
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci/* some useful defines for virtchannel interface, which
31562306a36Sopenharmony_ci * is the only remaining user of header split
31662306a36Sopenharmony_ci */
31762306a36Sopenharmony_ci#define IAVF_RX_DTYPE_NO_SPLIT      0
31862306a36Sopenharmony_ci#define IAVF_RX_DTYPE_HEADER_SPLIT  1
31962306a36Sopenharmony_ci#define IAVF_RX_DTYPE_SPLIT_ALWAYS  2
32062306a36Sopenharmony_ci#define IAVF_RX_SPLIT_L2      0x1
32162306a36Sopenharmony_ci#define IAVF_RX_SPLIT_IP      0x2
32262306a36Sopenharmony_ci#define IAVF_RX_SPLIT_TCP_UDP 0x4
32362306a36Sopenharmony_ci#define IAVF_RX_SPLIT_SCTP    0x8
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci/* struct that defines a descriptor ring, associated with a VSI */
32662306a36Sopenharmony_cistruct iavf_ring {
32762306a36Sopenharmony_ci	struct iavf_ring *next;		/* pointer to next ring in q_vector */
32862306a36Sopenharmony_ci	void *desc;			/* Descriptor ring memory */
32962306a36Sopenharmony_ci	struct device *dev;		/* Used for DMA mapping */
33062306a36Sopenharmony_ci	struct net_device *netdev;	/* netdev ring maps to */
33162306a36Sopenharmony_ci	union {
33262306a36Sopenharmony_ci		struct iavf_tx_buffer *tx_bi;
33362306a36Sopenharmony_ci		struct iavf_rx_buffer *rx_bi;
33462306a36Sopenharmony_ci	};
33562306a36Sopenharmony_ci	DECLARE_BITMAP(state, __IAVF_RING_STATE_NBITS);
33662306a36Sopenharmony_ci	u16 queue_index;		/* Queue number of ring */
33762306a36Sopenharmony_ci	u8 dcb_tc;			/* Traffic class of ring */
33862306a36Sopenharmony_ci	u8 __iomem *tail;
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci	/* high bit set means dynamic, use accessors routines to read/write.
34162306a36Sopenharmony_ci	 * hardware only supports 2us resolution for the ITR registers.
34262306a36Sopenharmony_ci	 * these values always store the USER setting, and must be converted
34362306a36Sopenharmony_ci	 * before programming to a register.
34462306a36Sopenharmony_ci	 */
34562306a36Sopenharmony_ci	u16 itr_setting;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	u16 count;			/* Number of descriptors */
34862306a36Sopenharmony_ci	u16 reg_idx;			/* HW register index of the ring */
34962306a36Sopenharmony_ci	u16 rx_buf_len;
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	/* used in interrupt processing */
35262306a36Sopenharmony_ci	u16 next_to_use;
35362306a36Sopenharmony_ci	u16 next_to_clean;
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	u8 atr_sample_rate;
35662306a36Sopenharmony_ci	u8 atr_count;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	bool ring_active;		/* is ring online or not */
35962306a36Sopenharmony_ci	bool arm_wb;		/* do something to arm write back */
36062306a36Sopenharmony_ci	u8 packet_stride;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	u16 flags;
36362306a36Sopenharmony_ci#define IAVF_TXR_FLAGS_WB_ON_ITR		BIT(0)
36462306a36Sopenharmony_ci#define IAVF_RXR_FLAGS_BUILD_SKB_ENABLED	BIT(1)
36562306a36Sopenharmony_ci#define IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1	BIT(3)
36662306a36Sopenharmony_ci#define IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2	BIT(4)
36762306a36Sopenharmony_ci#define IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2	BIT(5)
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	/* stats structs */
37062306a36Sopenharmony_ci	struct iavf_queue_stats	stats;
37162306a36Sopenharmony_ci	struct u64_stats_sync syncp;
37262306a36Sopenharmony_ci	union {
37362306a36Sopenharmony_ci		struct iavf_tx_queue_stats tx_stats;
37462306a36Sopenharmony_ci		struct iavf_rx_queue_stats rx_stats;
37562306a36Sopenharmony_ci	};
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	unsigned int size;		/* length of descriptor ring in bytes */
37862306a36Sopenharmony_ci	dma_addr_t dma;			/* physical address of ring */
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	struct iavf_vsi *vsi;		/* Backreference to associated VSI */
38162306a36Sopenharmony_ci	struct iavf_q_vector *q_vector;	/* Backreference to associated vector */
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	struct rcu_head rcu;		/* to avoid race on free */
38462306a36Sopenharmony_ci	u16 next_to_alloc;
38562306a36Sopenharmony_ci	struct sk_buff *skb;		/* When iavf_clean_rx_ring_irq() must
38662306a36Sopenharmony_ci					 * return before it sees the EOP for
38762306a36Sopenharmony_ci					 * the current packet, we save that skb
38862306a36Sopenharmony_ci					 * here and resume receiving this
38962306a36Sopenharmony_ci					 * packet the next time
39062306a36Sopenharmony_ci					 * iavf_clean_rx_ring_irq() is called
39162306a36Sopenharmony_ci					 * for this ring.
39262306a36Sopenharmony_ci					 */
39362306a36Sopenharmony_ci} ____cacheline_internodealigned_in_smp;
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_cistatic inline bool ring_uses_build_skb(struct iavf_ring *ring)
39662306a36Sopenharmony_ci{
39762306a36Sopenharmony_ci	return !!(ring->flags & IAVF_RXR_FLAGS_BUILD_SKB_ENABLED);
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_cistatic inline void set_ring_build_skb_enabled(struct iavf_ring *ring)
40162306a36Sopenharmony_ci{
40262306a36Sopenharmony_ci	ring->flags |= IAVF_RXR_FLAGS_BUILD_SKB_ENABLED;
40362306a36Sopenharmony_ci}
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_cistatic inline void clear_ring_build_skb_enabled(struct iavf_ring *ring)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	ring->flags &= ~IAVF_RXR_FLAGS_BUILD_SKB_ENABLED;
40862306a36Sopenharmony_ci}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci#define IAVF_ITR_ADAPTIVE_MIN_INC	0x0002
41162306a36Sopenharmony_ci#define IAVF_ITR_ADAPTIVE_MIN_USECS	0x0002
41262306a36Sopenharmony_ci#define IAVF_ITR_ADAPTIVE_MAX_USECS	0x007e
41362306a36Sopenharmony_ci#define IAVF_ITR_ADAPTIVE_LATENCY	0x8000
41462306a36Sopenharmony_ci#define IAVF_ITR_ADAPTIVE_BULK		0x0000
41562306a36Sopenharmony_ci#define ITR_IS_BULK(x) (!((x) & IAVF_ITR_ADAPTIVE_LATENCY))
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_cistruct iavf_ring_container {
41862306a36Sopenharmony_ci	struct iavf_ring *ring;		/* pointer to linked list of ring(s) */
41962306a36Sopenharmony_ci	unsigned long next_update;	/* jiffies value of next update */
42062306a36Sopenharmony_ci	unsigned int total_bytes;	/* total bytes processed this int */
42162306a36Sopenharmony_ci	unsigned int total_packets;	/* total packets processed this int */
42262306a36Sopenharmony_ci	u16 count;
42362306a36Sopenharmony_ci	u16 target_itr;			/* target ITR setting for ring(s) */
42462306a36Sopenharmony_ci	u16 current_itr;		/* current ITR setting for ring(s) */
42562306a36Sopenharmony_ci};
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci/* iterator for handling rings in ring container */
42862306a36Sopenharmony_ci#define iavf_for_each_ring(pos, head) \
42962306a36Sopenharmony_ci	for (pos = (head).ring; pos != NULL; pos = pos->next)
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_cistatic inline unsigned int iavf_rx_pg_order(struct iavf_ring *ring)
43262306a36Sopenharmony_ci{
43362306a36Sopenharmony_ci#if (PAGE_SIZE < 8192)
43462306a36Sopenharmony_ci	if (ring->rx_buf_len > (PAGE_SIZE / 2))
43562306a36Sopenharmony_ci		return 1;
43662306a36Sopenharmony_ci#endif
43762306a36Sopenharmony_ci	return 0;
43862306a36Sopenharmony_ci}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci#define iavf_rx_pg_size(_ring) (PAGE_SIZE << iavf_rx_pg_order(_ring))
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_cibool iavf_alloc_rx_buffers(struct iavf_ring *rxr, u16 cleaned_count);
44362306a36Sopenharmony_cinetdev_tx_t iavf_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
44462306a36Sopenharmony_ciint iavf_setup_tx_descriptors(struct iavf_ring *tx_ring);
44562306a36Sopenharmony_ciint iavf_setup_rx_descriptors(struct iavf_ring *rx_ring);
44662306a36Sopenharmony_civoid iavf_free_tx_resources(struct iavf_ring *tx_ring);
44762306a36Sopenharmony_civoid iavf_free_rx_resources(struct iavf_ring *rx_ring);
44862306a36Sopenharmony_ciint iavf_napi_poll(struct napi_struct *napi, int budget);
44962306a36Sopenharmony_civoid iavf_detect_recover_hung(struct iavf_vsi *vsi);
45062306a36Sopenharmony_ciint __iavf_maybe_stop_tx(struct iavf_ring *tx_ring, int size);
45162306a36Sopenharmony_cibool __iavf_chk_linearize(struct sk_buff *skb);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci/**
45462306a36Sopenharmony_ci * iavf_xmit_descriptor_count - calculate number of Tx descriptors needed
45562306a36Sopenharmony_ci * @skb:     send buffer
45662306a36Sopenharmony_ci *
45762306a36Sopenharmony_ci * Returns number of data descriptors needed for this skb. Returns 0 to indicate
45862306a36Sopenharmony_ci * there is not enough descriptors available in this ring since we need at least
45962306a36Sopenharmony_ci * one descriptor.
46062306a36Sopenharmony_ci **/
46162306a36Sopenharmony_cistatic inline int iavf_xmit_descriptor_count(struct sk_buff *skb)
46262306a36Sopenharmony_ci{
46362306a36Sopenharmony_ci	const skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
46462306a36Sopenharmony_ci	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
46562306a36Sopenharmony_ci	int count = 0, size = skb_headlen(skb);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	for (;;) {
46862306a36Sopenharmony_ci		count += iavf_txd_use_count(size);
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci		if (!nr_frags--)
47162306a36Sopenharmony_ci			break;
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci		size = skb_frag_size(frag++);
47462306a36Sopenharmony_ci	}
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	return count;
47762306a36Sopenharmony_ci}
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci/**
48062306a36Sopenharmony_ci * iavf_maybe_stop_tx - 1st level check for Tx stop conditions
48162306a36Sopenharmony_ci * @tx_ring: the ring to be checked
48262306a36Sopenharmony_ci * @size:    the size buffer we want to assure is available
48362306a36Sopenharmony_ci *
48462306a36Sopenharmony_ci * Returns 0 if stop is not needed
48562306a36Sopenharmony_ci **/
48662306a36Sopenharmony_cistatic inline int iavf_maybe_stop_tx(struct iavf_ring *tx_ring, int size)
48762306a36Sopenharmony_ci{
48862306a36Sopenharmony_ci	if (likely(IAVF_DESC_UNUSED(tx_ring) >= size))
48962306a36Sopenharmony_ci		return 0;
49062306a36Sopenharmony_ci	return __iavf_maybe_stop_tx(tx_ring, size);
49162306a36Sopenharmony_ci}
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci/**
49462306a36Sopenharmony_ci * iavf_chk_linearize - Check if there are more than 8 fragments per packet
49562306a36Sopenharmony_ci * @skb:      send buffer
49662306a36Sopenharmony_ci * @count:    number of buffers used
49762306a36Sopenharmony_ci *
49862306a36Sopenharmony_ci * Note: Our HW can't scatter-gather more than 8 fragments to build
49962306a36Sopenharmony_ci * a packet on the wire and so we need to figure out the cases where we
50062306a36Sopenharmony_ci * need to linearize the skb.
50162306a36Sopenharmony_ci **/
50262306a36Sopenharmony_cistatic inline bool iavf_chk_linearize(struct sk_buff *skb, int count)
50362306a36Sopenharmony_ci{
50462306a36Sopenharmony_ci	/* Both TSO and single send will work if count is less than 8 */
50562306a36Sopenharmony_ci	if (likely(count < IAVF_MAX_BUFFER_TXD))
50662306a36Sopenharmony_ci		return false;
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	if (skb_is_gso(skb))
50962306a36Sopenharmony_ci		return __iavf_chk_linearize(skb);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	/* we can support up to 8 data buffers for a single send */
51262306a36Sopenharmony_ci	return count != IAVF_MAX_BUFFER_TXD;
51362306a36Sopenharmony_ci}
51462306a36Sopenharmony_ci/**
51562306a36Sopenharmony_ci * txring_txq - helper to convert from a ring to a queue
51662306a36Sopenharmony_ci * @ring: Tx ring to find the netdev equivalent of
51762306a36Sopenharmony_ci **/
51862306a36Sopenharmony_cistatic inline struct netdev_queue *txring_txq(const struct iavf_ring *ring)
51962306a36Sopenharmony_ci{
52062306a36Sopenharmony_ci	return netdev_get_tx_queue(ring->netdev, ring->queue_index);
52162306a36Sopenharmony_ci}
52262306a36Sopenharmony_ci#endif /* _IAVF_TXRX_H_ */
523