162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/* Copyright(c) 2013 - 2018 Intel Corporation. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#ifndef _I40E_TXRX_H_
562306a36Sopenharmony_ci#define _I40E_TXRX_H_
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <net/xdp.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/* Interrupt Throttling and Rate Limiting Goodies */
1062306a36Sopenharmony_ci#define I40E_DEFAULT_IRQ_WORK      256
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/* The datasheet for the X710 and XL710 indicate that the maximum value for
1362306a36Sopenharmony_ci * the ITR is 8160usec which is then called out as 0xFF0 with a 2usec
1462306a36Sopenharmony_ci * resolution. 8160 is 0x1FE0 when written out in hex. So instead of storing
1562306a36Sopenharmony_ci * the register value which is divided by 2 lets use the actual values and
1662306a36Sopenharmony_ci * avoid an excessive amount of translation.
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci#define I40E_ITR_DYNAMIC	0x8000	/* use top bit as a flag */
1962306a36Sopenharmony_ci#define I40E_ITR_MASK		0x1FFE	/* mask for ITR register value */
2062306a36Sopenharmony_ci#define I40E_MIN_ITR		     2	/* reg uses 2 usec resolution */
2162306a36Sopenharmony_ci#define I40E_ITR_20K		    50
2262306a36Sopenharmony_ci#define I40E_ITR_8K		   122
2362306a36Sopenharmony_ci#define I40E_MAX_ITR		  8160	/* maximum value as per datasheet */
2462306a36Sopenharmony_ci#define ITR_TO_REG(setting) ((setting) & ~I40E_ITR_DYNAMIC)
2562306a36Sopenharmony_ci#define ITR_REG_ALIGN(setting) __ALIGN_MASK(setting, ~I40E_ITR_MASK)
2662306a36Sopenharmony_ci#define ITR_IS_DYNAMIC(setting) (!!((setting) & I40E_ITR_DYNAMIC))
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define I40E_ITR_RX_DEF		(I40E_ITR_20K | I40E_ITR_DYNAMIC)
2962306a36Sopenharmony_ci#define I40E_ITR_TX_DEF		(I40E_ITR_20K | I40E_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 I40E_MAX_INTRL             0x3B    /* reg uses 4 usec resolution */
3662306a36Sopenharmony_ci#define INTRL_REG_TO_USEC(intrl) ((intrl & ~INTRL_ENA) << 2)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/**
3962306a36Sopenharmony_ci * i40e_intrl_usec_to_reg - convert interrupt rate limit to register
4062306a36Sopenharmony_ci * @intrl: interrupt rate limit to convert
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * This function converts a decimal interrupt rate limit to the appropriate
4362306a36Sopenharmony_ci * register format expected by the firmware when setting interrupt rate limit.
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_cistatic inline u16 i40e_intrl_usec_to_reg(int intrl)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	if (intrl >> 2)
4862306a36Sopenharmony_ci		return ((intrl >> 2) | INTRL_ENA);
4962306a36Sopenharmony_ci	else
5062306a36Sopenharmony_ci		return 0;
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define I40E_QUEUE_END_OF_LIST 0x7FF
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* this enum matches hardware bits and is meant to be used by DYN_CTLN
5662306a36Sopenharmony_ci * registers and QINT registers or more generally anywhere in the manual
5762306a36Sopenharmony_ci * mentioning ITR_INDX, ITR_NONE cannot be used as an index 'n' into any
5862306a36Sopenharmony_ci * register but instead is a special value meaning "don't update" ITR0/1/2.
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_cienum i40e_dyn_idx_t {
6162306a36Sopenharmony_ci	I40E_IDX_ITR0 = 0,
6262306a36Sopenharmony_ci	I40E_IDX_ITR1 = 1,
6362306a36Sopenharmony_ci	I40E_IDX_ITR2 = 2,
6462306a36Sopenharmony_ci	I40E_ITR_NONE = 3	/* ITR_NONE must not be used as an index */
6562306a36Sopenharmony_ci};
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/* these are indexes into ITRN registers */
6862306a36Sopenharmony_ci#define I40E_RX_ITR    I40E_IDX_ITR0
6962306a36Sopenharmony_ci#define I40E_TX_ITR    I40E_IDX_ITR1
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/* Supported RSS offloads */
7262306a36Sopenharmony_ci#define I40E_DEFAULT_RSS_HENA ( \
7362306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_UDP) | \
7462306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_SCTP) | \
7562306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP) | \
7662306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) | \
7762306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4) | \
7862306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_UDP) | \
7962306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP) | \
8062306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_SCTP) | \
8162306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) | \
8262306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6) | \
8362306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_L2_PAYLOAD))
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define I40E_DEFAULT_RSS_HENA_EXPANDED (I40E_DEFAULT_RSS_HENA | \
8662306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK) | \
8762306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) | \
8862306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) | \
8962306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK) | \
9062306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) | \
9162306a36Sopenharmony_ci	BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP))
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define i40e_pf_get_default_rss_hena(pf) \
9462306a36Sopenharmony_ci	(((pf)->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) ? \
9562306a36Sopenharmony_ci	  I40E_DEFAULT_RSS_HENA_EXPANDED : I40E_DEFAULT_RSS_HENA)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/* Supported Rx Buffer Sizes (a multiple of 128) */
9862306a36Sopenharmony_ci#define I40E_RXBUFFER_256   256
9962306a36Sopenharmony_ci#define I40E_RXBUFFER_1536  1536  /* 128B aligned standard Ethernet frame */
10062306a36Sopenharmony_ci#define I40E_RXBUFFER_2048  2048
10162306a36Sopenharmony_ci#define I40E_RXBUFFER_3072  3072  /* Used for large frames w/ padding */
10262306a36Sopenharmony_ci#define I40E_MAX_RXBUFFER   9728  /* largest size for single descriptor */
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci/* NOTE: netdev_alloc_skb reserves up to 64 bytes, NET_IP_ALIGN means we
10562306a36Sopenharmony_ci * reserve 2 more, and skb_shared_info adds an additional 384 bytes more,
10662306a36Sopenharmony_ci * this adds up to 512 bytes of extra data meaning the smallest allocation
10762306a36Sopenharmony_ci * we could have is 1K.
10862306a36Sopenharmony_ci * i.e. RXBUFFER_256 --> 960 byte skb (size-1024 slab)
10962306a36Sopenharmony_ci * i.e. RXBUFFER_512 --> 1216 byte skb (size-2048 slab)
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_ci#define I40E_RX_HDR_SIZE I40E_RXBUFFER_256
11262306a36Sopenharmony_ci#define I40E_PACKET_HDR_PAD (ETH_HLEN + ETH_FCS_LEN + (VLAN_HLEN * 2))
11362306a36Sopenharmony_ci#define i40e_rx_desc i40e_16byte_rx_desc
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define I40E_RX_DMA_ATTR \
11662306a36Sopenharmony_ci	(DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING)
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/* Attempt to maximize the headroom available for incoming frames.  We
11962306a36Sopenharmony_ci * use a 2K buffer for receives and need 1536/1534 to store the data for
12062306a36Sopenharmony_ci * the frame.  This leaves us with 512 bytes of room.  From that we need
12162306a36Sopenharmony_ci * to deduct the space needed for the shared info and the padding needed
12262306a36Sopenharmony_ci * to IP align the frame.
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * Note: For cache line sizes 256 or larger this value is going to end
12562306a36Sopenharmony_ci *	 up negative.  In these cases we should fall back to the legacy
12662306a36Sopenharmony_ci *	 receive path.
12762306a36Sopenharmony_ci */
12862306a36Sopenharmony_ci#if (PAGE_SIZE < 8192)
12962306a36Sopenharmony_ci#define I40E_2K_TOO_SMALL_WITH_PADDING \
13062306a36Sopenharmony_ci((NET_SKB_PAD + I40E_RXBUFFER_1536) > SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048))
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic inline int i40e_compute_pad(int rx_buf_len)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	int page_size, pad_size;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	page_size = ALIGN(rx_buf_len, PAGE_SIZE / 2);
13762306a36Sopenharmony_ci	pad_size = SKB_WITH_OVERHEAD(page_size) - rx_buf_len;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	return pad_size;
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic inline int i40e_skb_pad(void)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	int rx_buf_len;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/* If a 2K buffer cannot handle a standard Ethernet frame then
14762306a36Sopenharmony_ci	 * optimize padding for a 3K buffer instead of a 1.5K buffer.
14862306a36Sopenharmony_ci	 *
14962306a36Sopenharmony_ci	 * For a 3K buffer we need to add enough padding to allow for
15062306a36Sopenharmony_ci	 * tailroom due to NET_IP_ALIGN possibly shifting us out of
15162306a36Sopenharmony_ci	 * cache-line alignment.
15262306a36Sopenharmony_ci	 */
15362306a36Sopenharmony_ci	if (I40E_2K_TOO_SMALL_WITH_PADDING)
15462306a36Sopenharmony_ci		rx_buf_len = I40E_RXBUFFER_3072 + SKB_DATA_ALIGN(NET_IP_ALIGN);
15562306a36Sopenharmony_ci	else
15662306a36Sopenharmony_ci		rx_buf_len = I40E_RXBUFFER_1536;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	/* if needed make room for NET_IP_ALIGN */
15962306a36Sopenharmony_ci	rx_buf_len -= NET_IP_ALIGN;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	return i40e_compute_pad(rx_buf_len);
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci#define I40E_SKB_PAD i40e_skb_pad()
16562306a36Sopenharmony_ci#else
16662306a36Sopenharmony_ci#define I40E_2K_TOO_SMALL_WITH_PADDING false
16762306a36Sopenharmony_ci#define I40E_SKB_PAD (NET_SKB_PAD + NET_IP_ALIGN)
16862306a36Sopenharmony_ci#endif
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci/**
17162306a36Sopenharmony_ci * i40e_test_staterr - tests bits in Rx descriptor status and error fields
17262306a36Sopenharmony_ci * @rx_desc: pointer to receive descriptor (in le64 format)
17362306a36Sopenharmony_ci * @stat_err_bits: value to mask
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci * This function does some fast chicanery in order to return the
17662306a36Sopenharmony_ci * value of the mask which is really only used for boolean tests.
17762306a36Sopenharmony_ci * The status_error_len doesn't need to be shifted because it begins
17862306a36Sopenharmony_ci * at offset zero.
17962306a36Sopenharmony_ci */
18062306a36Sopenharmony_cistatic inline bool i40e_test_staterr(union i40e_rx_desc *rx_desc,
18162306a36Sopenharmony_ci				     const u64 stat_err_bits)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	return !!(rx_desc->wb.qword1.status_error_len &
18462306a36Sopenharmony_ci		  cpu_to_le64(stat_err_bits));
18562306a36Sopenharmony_ci}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/* How many Rx Buffers do we bundle into one write to the hardware ? */
18862306a36Sopenharmony_ci#define I40E_RX_BUFFER_WRITE	32	/* Must be power of 2 */
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci#define I40E_RX_NEXT_DESC(r, i, n)		\
19162306a36Sopenharmony_ci	do {					\
19262306a36Sopenharmony_ci		(i)++;				\
19362306a36Sopenharmony_ci		if ((i) == (r)->count)		\
19462306a36Sopenharmony_ci			i = 0;			\
19562306a36Sopenharmony_ci		(n) = I40E_RX_DESC((r), (i));	\
19662306a36Sopenharmony_ci	} while (0)
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci#define I40E_MAX_BUFFER_TXD	8
20062306a36Sopenharmony_ci#define I40E_MIN_TX_LEN		17
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci/* The size limit for a transmit buffer in a descriptor is (16K - 1).
20362306a36Sopenharmony_ci * In order to align with the read requests we will align the value to
20462306a36Sopenharmony_ci * the nearest 4K which represents our maximum read request size.
20562306a36Sopenharmony_ci */
20662306a36Sopenharmony_ci#define I40E_MAX_READ_REQ_SIZE		4096
20762306a36Sopenharmony_ci#define I40E_MAX_DATA_PER_TXD		(16 * 1024 - 1)
20862306a36Sopenharmony_ci#define I40E_MAX_DATA_PER_TXD_ALIGNED \
20962306a36Sopenharmony_ci	(I40E_MAX_DATA_PER_TXD & ~(I40E_MAX_READ_REQ_SIZE - 1))
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci/**
21262306a36Sopenharmony_ci * i40e_txd_use_count  - estimate the number of descriptors needed for Tx
21362306a36Sopenharmony_ci * @size: transmit request size in bytes
21462306a36Sopenharmony_ci *
21562306a36Sopenharmony_ci * Due to hardware alignment restrictions (4K alignment), we need to
21662306a36Sopenharmony_ci * assume that we can have no more than 12K of data per descriptor, even
21762306a36Sopenharmony_ci * though each descriptor can take up to 16K - 1 bytes of aligned memory.
21862306a36Sopenharmony_ci * Thus, we need to divide by 12K. But division is slow! Instead,
21962306a36Sopenharmony_ci * we decompose the operation into shifts and one relatively cheap
22062306a36Sopenharmony_ci * multiply operation.
22162306a36Sopenharmony_ci *
22262306a36Sopenharmony_ci * To divide by 12K, we first divide by 4K, then divide by 3:
22362306a36Sopenharmony_ci *     To divide by 4K, shift right by 12 bits
22462306a36Sopenharmony_ci *     To divide by 3, multiply by 85, then divide by 256
22562306a36Sopenharmony_ci *     (Divide by 256 is done by shifting right by 8 bits)
22662306a36Sopenharmony_ci * Finally, we add one to round up. Because 256 isn't an exact multiple of
22762306a36Sopenharmony_ci * 3, we'll underestimate near each multiple of 12K. This is actually more
22862306a36Sopenharmony_ci * accurate as we have 4K - 1 of wiggle room that we can fit into the last
22962306a36Sopenharmony_ci * segment.  For our purposes this is accurate out to 1M which is orders of
23062306a36Sopenharmony_ci * magnitude greater than our largest possible GSO size.
23162306a36Sopenharmony_ci *
23262306a36Sopenharmony_ci * This would then be implemented as:
23362306a36Sopenharmony_ci *     return (((size >> 12) * 85) >> 8) + 1;
23462306a36Sopenharmony_ci *
23562306a36Sopenharmony_ci * Since multiplication and division are commutative, we can reorder
23662306a36Sopenharmony_ci * operations into:
23762306a36Sopenharmony_ci *     return ((size * 85) >> 20) + 1;
23862306a36Sopenharmony_ci */
23962306a36Sopenharmony_cistatic inline unsigned int i40e_txd_use_count(unsigned int size)
24062306a36Sopenharmony_ci{
24162306a36Sopenharmony_ci	return ((size * 85) >> 20) + 1;
24262306a36Sopenharmony_ci}
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci/* Tx Descriptors needed, worst case */
24562306a36Sopenharmony_ci#define DESC_NEEDED (MAX_SKB_FRAGS + 6)
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci#define I40E_TX_FLAGS_HW_VLAN		BIT(1)
24862306a36Sopenharmony_ci#define I40E_TX_FLAGS_SW_VLAN		BIT(2)
24962306a36Sopenharmony_ci#define I40E_TX_FLAGS_TSO		BIT(3)
25062306a36Sopenharmony_ci#define I40E_TX_FLAGS_IPV4		BIT(4)
25162306a36Sopenharmony_ci#define I40E_TX_FLAGS_IPV6		BIT(5)
25262306a36Sopenharmony_ci#define I40E_TX_FLAGS_TSYN		BIT(8)
25362306a36Sopenharmony_ci#define I40E_TX_FLAGS_FD_SB		BIT(9)
25462306a36Sopenharmony_ci#define I40E_TX_FLAGS_UDP_TUNNEL	BIT(10)
25562306a36Sopenharmony_ci#define I40E_TX_FLAGS_VLAN_MASK		0xffff0000
25662306a36Sopenharmony_ci#define I40E_TX_FLAGS_VLAN_PRIO_MASK	0xe0000000
25762306a36Sopenharmony_ci#define I40E_TX_FLAGS_VLAN_PRIO_SHIFT	29
25862306a36Sopenharmony_ci#define I40E_TX_FLAGS_VLAN_SHIFT	16
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_cistruct i40e_tx_buffer {
26162306a36Sopenharmony_ci	struct i40e_tx_desc *next_to_watch;
26262306a36Sopenharmony_ci	union {
26362306a36Sopenharmony_ci		struct xdp_frame *xdpf;
26462306a36Sopenharmony_ci		struct sk_buff *skb;
26562306a36Sopenharmony_ci		void *raw_buf;
26662306a36Sopenharmony_ci	};
26762306a36Sopenharmony_ci	unsigned int bytecount;
26862306a36Sopenharmony_ci	unsigned short gso_segs;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	DEFINE_DMA_UNMAP_ADDR(dma);
27162306a36Sopenharmony_ci	DEFINE_DMA_UNMAP_LEN(len);
27262306a36Sopenharmony_ci	u32 tx_flags;
27362306a36Sopenharmony_ci};
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_cistruct i40e_rx_buffer {
27662306a36Sopenharmony_ci	dma_addr_t dma;
27762306a36Sopenharmony_ci	struct page *page;
27862306a36Sopenharmony_ci	__u32 page_offset;
27962306a36Sopenharmony_ci	__u16 pagecnt_bias;
28062306a36Sopenharmony_ci	__u32 page_count;
28162306a36Sopenharmony_ci};
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_cistruct i40e_queue_stats {
28462306a36Sopenharmony_ci	u64 packets;
28562306a36Sopenharmony_ci	u64 bytes;
28662306a36Sopenharmony_ci};
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_cistruct i40e_tx_queue_stats {
28962306a36Sopenharmony_ci	u64 restart_queue;
29062306a36Sopenharmony_ci	u64 tx_busy;
29162306a36Sopenharmony_ci	u64 tx_done_old;
29262306a36Sopenharmony_ci	u64 tx_linearize;
29362306a36Sopenharmony_ci	u64 tx_force_wb;
29462306a36Sopenharmony_ci	u64 tx_stopped;
29562306a36Sopenharmony_ci	int prev_pkt_ctr;
29662306a36Sopenharmony_ci};
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_cistruct i40e_rx_queue_stats {
29962306a36Sopenharmony_ci	u64 non_eop_descs;
30062306a36Sopenharmony_ci	u64 alloc_page_failed;
30162306a36Sopenharmony_ci	u64 alloc_buff_failed;
30262306a36Sopenharmony_ci	u64 page_reuse_count;
30362306a36Sopenharmony_ci	u64 page_alloc_count;
30462306a36Sopenharmony_ci	u64 page_waive_count;
30562306a36Sopenharmony_ci	u64 page_busy_count;
30662306a36Sopenharmony_ci};
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_cienum i40e_ring_state_t {
30962306a36Sopenharmony_ci	__I40E_TX_FDIR_INIT_DONE,
31062306a36Sopenharmony_ci	__I40E_TX_XPS_INIT_DONE,
31162306a36Sopenharmony_ci	__I40E_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 I40E_RX_DTYPE_HEADER_SPLIT  1
31862306a36Sopenharmony_ci#define I40E_RX_SPLIT_L2      0x1
31962306a36Sopenharmony_ci#define I40E_RX_SPLIT_IP      0x2
32062306a36Sopenharmony_ci#define I40E_RX_SPLIT_TCP_UDP 0x4
32162306a36Sopenharmony_ci#define I40E_RX_SPLIT_SCTP    0x8
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci/* struct that defines a descriptor ring, associated with a VSI */
32462306a36Sopenharmony_cistruct i40e_ring {
32562306a36Sopenharmony_ci	struct i40e_ring *next;		/* pointer to next ring in q_vector */
32662306a36Sopenharmony_ci	void *desc;			/* Descriptor ring memory */
32762306a36Sopenharmony_ci	struct device *dev;		/* Used for DMA mapping */
32862306a36Sopenharmony_ci	struct net_device *netdev;	/* netdev ring maps to */
32962306a36Sopenharmony_ci	struct bpf_prog *xdp_prog;
33062306a36Sopenharmony_ci	union {
33162306a36Sopenharmony_ci		struct i40e_tx_buffer *tx_bi;
33262306a36Sopenharmony_ci		struct i40e_rx_buffer *rx_bi;
33362306a36Sopenharmony_ci		struct xdp_buff **rx_bi_zc;
33462306a36Sopenharmony_ci	};
33562306a36Sopenharmony_ci	DECLARE_BITMAP(state, __I40E_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	/* Storing xdp_buff on ring helps in saving the state of partially built
34162306a36Sopenharmony_ci	 * packet when i40e_clean_rx_ring_irq() must return before it sees EOP
34262306a36Sopenharmony_ci	 * and to resume packet building for this ring in the next call to
34362306a36Sopenharmony_ci	 * i40e_clean_rx_ring_irq().
34462306a36Sopenharmony_ci	 */
34562306a36Sopenharmony_ci	struct xdp_buff xdp;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	/* Next descriptor to be processed; next_to_clean is updated only on
34862306a36Sopenharmony_ci	 * processing EOP descriptor
34962306a36Sopenharmony_ci	 */
35062306a36Sopenharmony_ci	u16 next_to_process;
35162306a36Sopenharmony_ci	/* high bit set means dynamic, use accessor routines to read/write.
35262306a36Sopenharmony_ci	 * hardware only supports 2us resolution for the ITR registers.
35362306a36Sopenharmony_ci	 * these values always store the USER setting, and must be converted
35462306a36Sopenharmony_ci	 * before programming to a register.
35562306a36Sopenharmony_ci	 */
35662306a36Sopenharmony_ci	u16 itr_setting;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	u16 count;			/* Number of descriptors */
35962306a36Sopenharmony_ci	u16 reg_idx;			/* HW register index of the ring */
36062306a36Sopenharmony_ci	u16 rx_buf_len;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	/* used in interrupt processing */
36362306a36Sopenharmony_ci	u16 next_to_use;
36462306a36Sopenharmony_ci	u16 next_to_clean;
36562306a36Sopenharmony_ci	u16 xdp_tx_active;
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci	u8 atr_sample_rate;
36862306a36Sopenharmony_ci	u8 atr_count;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	bool ring_active;		/* is ring online or not */
37162306a36Sopenharmony_ci	bool arm_wb;		/* do something to arm write back */
37262306a36Sopenharmony_ci	u8 packet_stride;
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	u16 flags;
37562306a36Sopenharmony_ci#define I40E_TXR_FLAGS_WB_ON_ITR		BIT(0)
37662306a36Sopenharmony_ci#define I40E_RXR_FLAGS_BUILD_SKB_ENABLED	BIT(1)
37762306a36Sopenharmony_ci#define I40E_TXR_FLAGS_XDP			BIT(2)
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	/* stats structs */
38062306a36Sopenharmony_ci	struct i40e_queue_stats	stats;
38162306a36Sopenharmony_ci	struct u64_stats_sync syncp;
38262306a36Sopenharmony_ci	union {
38362306a36Sopenharmony_ci		struct i40e_tx_queue_stats tx_stats;
38462306a36Sopenharmony_ci		struct i40e_rx_queue_stats rx_stats;
38562306a36Sopenharmony_ci	};
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	unsigned int size;		/* length of descriptor ring in bytes */
38862306a36Sopenharmony_ci	dma_addr_t dma;			/* physical address of ring */
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	struct i40e_vsi *vsi;		/* Backreference to associated VSI */
39162306a36Sopenharmony_ci	struct i40e_q_vector *q_vector;	/* Backreference to associated vector */
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci	struct rcu_head rcu;		/* to avoid race on free */
39462306a36Sopenharmony_ci	u16 next_to_alloc;
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	struct i40e_channel *ch;
39762306a36Sopenharmony_ci	u16 rx_offset;
39862306a36Sopenharmony_ci	struct xdp_rxq_info xdp_rxq;
39962306a36Sopenharmony_ci	struct xsk_buff_pool *xsk_pool;
40062306a36Sopenharmony_ci} ____cacheline_internodealigned_in_smp;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_cistatic inline bool ring_uses_build_skb(struct i40e_ring *ring)
40362306a36Sopenharmony_ci{
40462306a36Sopenharmony_ci	return !!(ring->flags & I40E_RXR_FLAGS_BUILD_SKB_ENABLED);
40562306a36Sopenharmony_ci}
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_cistatic inline void set_ring_build_skb_enabled(struct i40e_ring *ring)
40862306a36Sopenharmony_ci{
40962306a36Sopenharmony_ci	ring->flags |= I40E_RXR_FLAGS_BUILD_SKB_ENABLED;
41062306a36Sopenharmony_ci}
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_cistatic inline void clear_ring_build_skb_enabled(struct i40e_ring *ring)
41362306a36Sopenharmony_ci{
41462306a36Sopenharmony_ci	ring->flags &= ~I40E_RXR_FLAGS_BUILD_SKB_ENABLED;
41562306a36Sopenharmony_ci}
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_cistatic inline bool ring_is_xdp(struct i40e_ring *ring)
41862306a36Sopenharmony_ci{
41962306a36Sopenharmony_ci	return !!(ring->flags & I40E_TXR_FLAGS_XDP);
42062306a36Sopenharmony_ci}
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_cistatic inline void set_ring_xdp(struct i40e_ring *ring)
42362306a36Sopenharmony_ci{
42462306a36Sopenharmony_ci	ring->flags |= I40E_TXR_FLAGS_XDP;
42562306a36Sopenharmony_ci}
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci#define I40E_ITR_ADAPTIVE_MIN_INC	0x0002
42862306a36Sopenharmony_ci#define I40E_ITR_ADAPTIVE_MIN_USECS	0x0002
42962306a36Sopenharmony_ci#define I40E_ITR_ADAPTIVE_MAX_USECS	0x007e
43062306a36Sopenharmony_ci#define I40E_ITR_ADAPTIVE_LATENCY	0x8000
43162306a36Sopenharmony_ci#define I40E_ITR_ADAPTIVE_BULK		0x0000
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_cistruct i40e_ring_container {
43462306a36Sopenharmony_ci	struct i40e_ring *ring;		/* pointer to linked list of ring(s) */
43562306a36Sopenharmony_ci	unsigned long next_update;	/* jiffies value of next update */
43662306a36Sopenharmony_ci	unsigned int total_bytes;	/* total bytes processed this int */
43762306a36Sopenharmony_ci	unsigned int total_packets;	/* total packets processed this int */
43862306a36Sopenharmony_ci	u16 count;
43962306a36Sopenharmony_ci	u16 target_itr;			/* target ITR setting for ring(s) */
44062306a36Sopenharmony_ci	u16 current_itr;		/* current ITR setting for ring(s) */
44162306a36Sopenharmony_ci};
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci/* iterator for handling rings in ring container */
44462306a36Sopenharmony_ci#define i40e_for_each_ring(pos, head) \
44562306a36Sopenharmony_ci	for (pos = (head).ring; pos != NULL; pos = pos->next)
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_cistatic inline unsigned int i40e_rx_pg_order(struct i40e_ring *ring)
44862306a36Sopenharmony_ci{
44962306a36Sopenharmony_ci#if (PAGE_SIZE < 8192)
45062306a36Sopenharmony_ci	if (ring->rx_buf_len > (PAGE_SIZE / 2))
45162306a36Sopenharmony_ci		return 1;
45262306a36Sopenharmony_ci#endif
45362306a36Sopenharmony_ci	return 0;
45462306a36Sopenharmony_ci}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci#define i40e_rx_pg_size(_ring) (PAGE_SIZE << i40e_rx_pg_order(_ring))
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_cibool i40e_alloc_rx_buffers(struct i40e_ring *rxr, u16 cleaned_count);
45962306a36Sopenharmony_cinetdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
46062306a36Sopenharmony_ciu16 i40e_lan_select_queue(struct net_device *netdev, struct sk_buff *skb,
46162306a36Sopenharmony_ci			  struct net_device *sb_dev);
46262306a36Sopenharmony_civoid i40e_clean_tx_ring(struct i40e_ring *tx_ring);
46362306a36Sopenharmony_civoid i40e_clean_rx_ring(struct i40e_ring *rx_ring);
46462306a36Sopenharmony_ciint i40e_setup_tx_descriptors(struct i40e_ring *tx_ring);
46562306a36Sopenharmony_ciint i40e_setup_rx_descriptors(struct i40e_ring *rx_ring);
46662306a36Sopenharmony_civoid i40e_free_tx_resources(struct i40e_ring *tx_ring);
46762306a36Sopenharmony_civoid i40e_free_rx_resources(struct i40e_ring *rx_ring);
46862306a36Sopenharmony_ciint i40e_napi_poll(struct napi_struct *napi, int budget);
46962306a36Sopenharmony_civoid i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector);
47062306a36Sopenharmony_ciu32 i40e_get_tx_pending(struct i40e_ring *ring, bool in_sw);
47162306a36Sopenharmony_civoid i40e_detect_recover_hung(struct i40e_vsi *vsi);
47262306a36Sopenharmony_ciint __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size);
47362306a36Sopenharmony_cibool __i40e_chk_linearize(struct sk_buff *skb);
47462306a36Sopenharmony_ciint i40e_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
47562306a36Sopenharmony_ci		  u32 flags);
47662306a36Sopenharmony_cibool i40e_is_non_eop(struct i40e_ring *rx_ring,
47762306a36Sopenharmony_ci		     union i40e_rx_desc *rx_desc);
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci/**
48062306a36Sopenharmony_ci * i40e_get_head - Retrieve head from head writeback
48162306a36Sopenharmony_ci * @tx_ring:  tx ring to fetch head of
48262306a36Sopenharmony_ci *
48362306a36Sopenharmony_ci * Returns value of Tx ring head based on value stored
48462306a36Sopenharmony_ci * in head write-back location
48562306a36Sopenharmony_ci **/
48662306a36Sopenharmony_cistatic inline u32 i40e_get_head(struct i40e_ring *tx_ring)
48762306a36Sopenharmony_ci{
48862306a36Sopenharmony_ci	void *head = (struct i40e_tx_desc *)tx_ring->desc + tx_ring->count;
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	return le32_to_cpu(*(volatile __le32 *)head);
49162306a36Sopenharmony_ci}
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci/**
49462306a36Sopenharmony_ci * i40e_xmit_descriptor_count - calculate number of Tx descriptors needed
49562306a36Sopenharmony_ci * @skb:     send buffer
49662306a36Sopenharmony_ci *
49762306a36Sopenharmony_ci * Returns number of data descriptors needed for this skb. Returns 0 to indicate
49862306a36Sopenharmony_ci * there is not enough descriptors available in this ring since we need at least
49962306a36Sopenharmony_ci * one descriptor.
50062306a36Sopenharmony_ci **/
50162306a36Sopenharmony_cistatic inline int i40e_xmit_descriptor_count(struct sk_buff *skb)
50262306a36Sopenharmony_ci{
50362306a36Sopenharmony_ci	const skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
50462306a36Sopenharmony_ci	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
50562306a36Sopenharmony_ci	int count = 0, size = skb_headlen(skb);
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	for (;;) {
50862306a36Sopenharmony_ci		count += i40e_txd_use_count(size);
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci		if (!nr_frags--)
51162306a36Sopenharmony_ci			break;
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci		size = skb_frag_size(frag++);
51462306a36Sopenharmony_ci	}
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci	return count;
51762306a36Sopenharmony_ci}
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci/**
52062306a36Sopenharmony_ci * i40e_maybe_stop_tx - 1st level check for Tx stop conditions
52162306a36Sopenharmony_ci * @tx_ring: the ring to be checked
52262306a36Sopenharmony_ci * @size:    the size buffer we want to assure is available
52362306a36Sopenharmony_ci *
52462306a36Sopenharmony_ci * Returns 0 if stop is not needed
52562306a36Sopenharmony_ci **/
52662306a36Sopenharmony_cistatic inline int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
52762306a36Sopenharmony_ci{
52862306a36Sopenharmony_ci	if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
52962306a36Sopenharmony_ci		return 0;
53062306a36Sopenharmony_ci	return __i40e_maybe_stop_tx(tx_ring, size);
53162306a36Sopenharmony_ci}
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci/**
53462306a36Sopenharmony_ci * i40e_chk_linearize - Check if there are more than 8 fragments per packet
53562306a36Sopenharmony_ci * @skb:      send buffer
53662306a36Sopenharmony_ci * @count:    number of buffers used
53762306a36Sopenharmony_ci *
53862306a36Sopenharmony_ci * Note: Our HW can't scatter-gather more than 8 fragments to build
53962306a36Sopenharmony_ci * a packet on the wire and so we need to figure out the cases where we
54062306a36Sopenharmony_ci * need to linearize the skb.
54162306a36Sopenharmony_ci **/
54262306a36Sopenharmony_cistatic inline bool i40e_chk_linearize(struct sk_buff *skb, int count)
54362306a36Sopenharmony_ci{
54462306a36Sopenharmony_ci	/* Both TSO and single send will work if count is less than 8 */
54562306a36Sopenharmony_ci	if (likely(count < I40E_MAX_BUFFER_TXD))
54662306a36Sopenharmony_ci		return false;
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	if (skb_is_gso(skb))
54962306a36Sopenharmony_ci		return __i40e_chk_linearize(skb);
55062306a36Sopenharmony_ci
55162306a36Sopenharmony_ci	/* we can support up to 8 data buffers for a single send */
55262306a36Sopenharmony_ci	return count != I40E_MAX_BUFFER_TXD;
55362306a36Sopenharmony_ci}
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci/**
55662306a36Sopenharmony_ci * txring_txq - Find the netdev Tx ring based on the i40e Tx ring
55762306a36Sopenharmony_ci * @ring: Tx ring to find the netdev equivalent of
55862306a36Sopenharmony_ci **/
55962306a36Sopenharmony_cistatic inline struct netdev_queue *txring_txq(const struct i40e_ring *ring)
56062306a36Sopenharmony_ci{
56162306a36Sopenharmony_ci	return netdev_get_tx_queue(ring->netdev, ring->queue_index);
56262306a36Sopenharmony_ci}
56362306a36Sopenharmony_ci#endif /* _I40E_TXRX_H_ */
564