xref: /linux/drivers/net/ethernet/google/gve/gve.h (revision 6f3bc487)
1893ce44dSCatherine Sullivan /* SPDX-License-Identifier: (GPL-2.0 OR MIT)
2893ce44dSCatherine Sullivan  * Google virtual Ethernet (gve) driver
3893ce44dSCatherine Sullivan  *
4*6f3bc487SJeroen de Borst  * Copyright (C) 2015-2024 Google LLC
5893ce44dSCatherine Sullivan  */
6893ce44dSCatherine Sullivan 
7893ce44dSCatherine Sullivan #ifndef _GVE_H_
8893ce44dSCatherine Sullivan #define _GVE_H_
9893ce44dSCatherine Sullivan 
10893ce44dSCatherine Sullivan #include <linux/dma-mapping.h>
11955f4d3bSJohn Fraker #include <linux/dmapool.h>
125e37d825SJeroen de Borst #include <linux/ethtool_netlink.h>
13893ce44dSCatherine Sullivan #include <linux/netdevice.h>
14893ce44dSCatherine Sullivan #include <linux/pci.h>
15f5cedc84SCatherine Sullivan #include <linux/u64_stats_sync.h>
1692272ec4SJakub Kicinski #include <net/xdp.h>
17c4b87ac8SBailey Forrest 
18f5cedc84SCatherine Sullivan #include "gve_desc.h"
19a4aa1f1eSBailey Forrest #include "gve_desc_dqo.h"
20893ce44dSCatherine Sullivan 
21893ce44dSCatherine Sullivan #ifndef PCI_VENDOR_ID_GOOGLE
22893ce44dSCatherine Sullivan #define PCI_VENDOR_ID_GOOGLE	0x1ae0
23893ce44dSCatherine Sullivan #endif
24893ce44dSCatherine Sullivan 
25893ce44dSCatherine Sullivan #define PCI_DEV_ID_GVNIC	0x0042
26893ce44dSCatherine Sullivan 
27893ce44dSCatherine Sullivan #define GVE_REGISTER_BAR	0
28893ce44dSCatherine Sullivan #define GVE_DOORBELL_BAR	2
29893ce44dSCatherine Sullivan 
30f5cedc84SCatherine Sullivan /* Driver can alloc up to 2 segments for the header and 2 for the payload. */
31f5cedc84SCatherine Sullivan #define GVE_TX_MAX_IOVEC	4
32f5cedc84SCatherine Sullivan /* 1 for management, 1 for rx, 1 for tx */
33893ce44dSCatherine Sullivan #define GVE_MIN_MSIX 3
34893ce44dSCatherine Sullivan 
3524aeb56fSKuo Zhao /* Numbers of gve tx/rx stats in stats report. */
3687a7f321SJohn Fraker #define GVE_TX_STATS_REPORT_NUM	6
3724aeb56fSKuo Zhao #define GVE_RX_STATS_REPORT_NUM	2
3824aeb56fSKuo Zhao 
3924aeb56fSKuo Zhao /* Interval to schedule a stats report update, 20000ms. */
4024aeb56fSKuo Zhao #define GVE_STATS_REPORT_TIMER_PERIOD	20000
4124aeb56fSKuo Zhao 
422f523dc3SDavid Awogbemila /* Numbers of NIC tx/rx stats in stats report. */
432f523dc3SDavid Awogbemila #define NIC_TX_STATS_REPORT_NUM	0
442f523dc3SDavid Awogbemila #define NIC_RX_STATS_REPORT_NUM	4
452f523dc3SDavid Awogbemila 
46955f4d3bSJohn Fraker #define GVE_ADMINQ_BUFFER_SIZE 4096
47955f4d3bSJohn Fraker 
48ede3fcf5SCatherine Sullivan #define GVE_DATA_SLOT_ADDR_PAGE_MASK (~(PAGE_SIZE - 1))
49ede3fcf5SCatherine Sullivan 
50c4b87ac8SBailey Forrest /* PTYPEs are always 10 bits. */
51c4b87ac8SBailey Forrest #define GVE_NUM_PTYPES	1024
52c4b87ac8SBailey Forrest 
53ed4fb326SHarshitha Ramamurthy /* Default minimum ring size */
54ed4fb326SHarshitha Ramamurthy #define GVE_DEFAULT_MIN_TX_RING_SIZE 256
55ed4fb326SHarshitha Ramamurthy #define GVE_DEFAULT_MIN_RX_RING_SIZE 512
56ed4fb326SHarshitha Ramamurthy 
57da7d4b42SJohn Fraker #define GVE_DEFAULT_RX_BUFFER_SIZE 2048
58da7d4b42SJohn Fraker 
590b43cf52SJeroen de Borst #define GVE_MAX_RX_BUFFER_SIZE 4096
600b43cf52SJeroen de Borst 
61da7d4b42SJohn Fraker #define GVE_DEFAULT_RX_BUFFER_OFFSET 2048
625e8c5adfSBailey Forrest 
6357718b60SJeroen de Borst #define GVE_FLOW_RULES_CACHE_SIZE \
6457718b60SJeroen de Borst 	(GVE_ADMINQ_BUFFER_SIZE / sizeof(struct gve_adminq_queried_flow_rule))
6557718b60SJeroen de Borst #define GVE_FLOW_RULE_IDS_CACHE_SIZE \
6657718b60SJeroen de Borst 	(GVE_ADMINQ_BUFFER_SIZE / sizeof(((struct gve_adminq_queried_flow_rule *)0)->location))
6757718b60SJeroen de Borst 
6875eaae15SPraveen Kaligineedi #define GVE_XDP_ACTIONS 5
6975eaae15SPraveen Kaligineedi 
703ce93455SShailend Chand #define GVE_GQ_TX_MIN_PKT_DESC_BYTES 182
713ce93455SShailend Chand 
720b43cf52SJeroen de Borst #define GVE_DEFAULT_HEADER_BUFFER_SIZE 128
730b43cf52SJeroen de Borst 
7466ce8e6bSRushil Gupta #define DQO_QPL_DEFAULT_TX_PAGES 512
7566ce8e6bSRushil Gupta 
7666ce8e6bSRushil Gupta /* Maximum TSO size supported on DQO */
7766ce8e6bSRushil Gupta #define GVE_DQO_TX_MAX	0x3FFFF
7866ce8e6bSRushil Gupta 
79a6fb8d5aSRushil Gupta #define GVE_TX_BUF_SHIFT_DQO 11
80a6fb8d5aSRushil Gupta 
81a6fb8d5aSRushil Gupta /* 2K buffers for DQO-QPL */
82a6fb8d5aSRushil Gupta #define GVE_TX_BUF_SIZE_DQO BIT(GVE_TX_BUF_SHIFT_DQO)
83a6fb8d5aSRushil Gupta #define GVE_TX_BUFS_PER_PAGE_DQO (PAGE_SIZE >> GVE_TX_BUF_SHIFT_DQO)
84a6fb8d5aSRushil Gupta #define GVE_MAX_TX_BUFS_PER_PKT (DIV_ROUND_UP(GVE_DQO_TX_MAX, GVE_TX_BUF_SIZE_DQO))
85a6fb8d5aSRushil Gupta 
86a6fb8d5aSRushil Gupta /* If number of free/recyclable buffers are less than this threshold; driver
87a6fb8d5aSRushil Gupta  * allocs and uses a non-qpl page on the receive path of DQO QPL to free
88a6fb8d5aSRushil Gupta  * up buffers.
89a6fb8d5aSRushil Gupta  * Value is set big enough to post at least 3 64K LRO packet via 2K buffer to NIC.
90a6fb8d5aSRushil Gupta  */
91a6fb8d5aSRushil Gupta #define GVE_DQO_QPL_ONDEMAND_ALLOC_THRESHOLD 96
92a6fb8d5aSRushil Gupta 
93f5cedc84SCatherine Sullivan /* Each slot in the desc ring has a 1:1 mapping to a slot in the data ring */
94f5cedc84SCatherine Sullivan struct gve_rx_desc_queue {
95f5cedc84SCatherine Sullivan 	struct gve_rx_desc *desc_ring; /* the descriptor ring */
96f5cedc84SCatherine Sullivan 	dma_addr_t bus; /* the bus for the desc_ring */
97f5cedc84SCatherine Sullivan 	u8 seqno; /* the next expected seqno for this desc*/
98f5cedc84SCatherine Sullivan };
99f5cedc84SCatherine Sullivan 
100f5cedc84SCatherine Sullivan /* The page info for a single slot in the RX data queue */
101f5cedc84SCatherine Sullivan struct gve_rx_slot_page_info {
102f5cedc84SCatherine Sullivan 	struct page *page;
103f5cedc84SCatherine Sullivan 	void *page_address;
104920fb451SBailey Forrest 	u32 page_offset; /* offset to write to in page */
1059b8dd5e5SBailey Forrest 	int pagecnt_bias; /* expected pagecnt if only the driver has a ref */
10682fd151dSShailend Chand 	u16 pad; /* adjustment for rx padding */
10782fd151dSShailend Chand 	u8 can_flip; /* tracks if the networking stack is using the page */
108f5cedc84SCatherine Sullivan };
109f5cedc84SCatherine Sullivan 
110f5cedc84SCatherine Sullivan /* A list of pages registered with the device during setup and used by a queue
111f5cedc84SCatherine Sullivan  * as buffers
112f5cedc84SCatherine Sullivan  */
113f5cedc84SCatherine Sullivan struct gve_queue_page_list {
114f5cedc84SCatherine Sullivan 	u32 id; /* unique id */
115f5cedc84SCatherine Sullivan 	u32 num_entries;
116f5cedc84SCatherine Sullivan 	struct page **pages; /* list of num_entries pages */
117f5cedc84SCatherine Sullivan 	dma_addr_t *page_buses; /* the dma addrs of the pages */
118f5cedc84SCatherine Sullivan };
119f5cedc84SCatherine Sullivan 
120f5cedc84SCatherine Sullivan /* Each slot in the data ring has a 1:1 mapping to a slot in the desc ring */
121f5cedc84SCatherine Sullivan struct gve_rx_data_queue {
122ede3fcf5SCatherine Sullivan 	union gve_rx_data_slot *data_ring; /* read by NIC */
123f5cedc84SCatherine Sullivan 	dma_addr_t data_bus; /* dma mapping of the slots */
124f5cedc84SCatherine Sullivan 	struct gve_rx_slot_page_info *page_info; /* page info of the buffers */
125f5cedc84SCatherine Sullivan 	struct gve_queue_page_list *qpl; /* qpl assigned to this queue */
126ede3fcf5SCatherine Sullivan 	u8 raw_addressing; /* use raw_addressing? */
127f5cedc84SCatherine Sullivan };
128f5cedc84SCatherine Sullivan 
129f5cedc84SCatherine Sullivan struct gve_priv;
130f5cedc84SCatherine Sullivan 
131a4aa1f1eSBailey Forrest /* RX buffer queue for posting buffers to HW.
132a4aa1f1eSBailey Forrest  * Each RX (completion) queue has a corresponding buffer queue.
133a4aa1f1eSBailey Forrest  */
134a4aa1f1eSBailey Forrest struct gve_rx_buf_queue_dqo {
135a4aa1f1eSBailey Forrest 	struct gve_rx_desc_dqo *desc_ring;
136a4aa1f1eSBailey Forrest 	dma_addr_t bus;
137a4aa1f1eSBailey Forrest 	u32 head; /* Pointer to start cleaning buffers at. */
138a4aa1f1eSBailey Forrest 	u32 tail; /* Last posted buffer index + 1 */
139a4aa1f1eSBailey Forrest 	u32 mask; /* Mask for indices to the size of the ring */
140a4aa1f1eSBailey Forrest };
141a4aa1f1eSBailey Forrest 
142a4aa1f1eSBailey Forrest /* RX completion queue to receive packets from HW. */
143a4aa1f1eSBailey Forrest struct gve_rx_compl_queue_dqo {
144a4aa1f1eSBailey Forrest 	struct gve_rx_compl_desc_dqo *desc_ring;
145a4aa1f1eSBailey Forrest 	dma_addr_t bus;
146a4aa1f1eSBailey Forrest 
147a4aa1f1eSBailey Forrest 	/* Number of slots which did not have a buffer posted yet. We should not
148a4aa1f1eSBailey Forrest 	 * post more buffers than the queue size to avoid HW overrunning the
149a4aa1f1eSBailey Forrest 	 * queue.
150a4aa1f1eSBailey Forrest 	 */
151a4aa1f1eSBailey Forrest 	int num_free_slots;
152a4aa1f1eSBailey Forrest 
153a4aa1f1eSBailey Forrest 	/* HW uses a "generation bit" to notify SW of new descriptors. When a
154a4aa1f1eSBailey Forrest 	 * descriptor's generation bit is different from the current generation,
155a4aa1f1eSBailey Forrest 	 * that descriptor is ready to be consumed by SW.
156a4aa1f1eSBailey Forrest 	 */
157a4aa1f1eSBailey Forrest 	u8 cur_gen_bit;
158a4aa1f1eSBailey Forrest 
159a4aa1f1eSBailey Forrest 	/* Pointer into desc_ring where the next completion descriptor will be
160a4aa1f1eSBailey Forrest 	 * received.
161a4aa1f1eSBailey Forrest 	 */
162a4aa1f1eSBailey Forrest 	u32 head;
163a4aa1f1eSBailey Forrest 	u32 mask; /* Mask for indices to the size of the ring */
164a4aa1f1eSBailey Forrest };
165a4aa1f1eSBailey Forrest 
1665e37d825SJeroen de Borst struct gve_header_buf {
1675e37d825SJeroen de Borst 	u8 *data;
1685e37d825SJeroen de Borst 	dma_addr_t addr;
1695e37d825SJeroen de Borst };
1705e37d825SJeroen de Borst 
171a4aa1f1eSBailey Forrest /* Stores state for tracking buffers posted to HW */
172a4aa1f1eSBailey Forrest struct gve_rx_buf_state_dqo {
173a4aa1f1eSBailey Forrest 	/* The page posted to HW. */
174a4aa1f1eSBailey Forrest 	struct gve_rx_slot_page_info page_info;
175a4aa1f1eSBailey Forrest 
176a4aa1f1eSBailey Forrest 	/* The DMA address corresponding to `page_info`. */
177a4aa1f1eSBailey Forrest 	dma_addr_t addr;
178a4aa1f1eSBailey Forrest 
179a4aa1f1eSBailey Forrest 	/* Last offset into the page when it only had a single reference, at
180a4aa1f1eSBailey Forrest 	 * which point every other offset is free to be reused.
181a4aa1f1eSBailey Forrest 	 */
182a4aa1f1eSBailey Forrest 	u32 last_single_ref_offset;
183a4aa1f1eSBailey Forrest 
184a4aa1f1eSBailey Forrest 	/* Linked list index to next element in the list, or -1 if none */
185a4aa1f1eSBailey Forrest 	s16 next;
186a4aa1f1eSBailey Forrest };
187a4aa1f1eSBailey Forrest 
188a4aa1f1eSBailey Forrest /* `head` and `tail` are indices into an array, or -1 if empty. */
189a4aa1f1eSBailey Forrest struct gve_index_list {
190a4aa1f1eSBailey Forrest 	s16 head;
191a4aa1f1eSBailey Forrest 	s16 tail;
192a4aa1f1eSBailey Forrest };
193a4aa1f1eSBailey Forrest 
1941344e751SDavid Awogbemila /* A single received packet split across multiple buffers may be
1951344e751SDavid Awogbemila  * reconstructed using the information in this structure.
1961344e751SDavid Awogbemila  */
1971344e751SDavid Awogbemila struct gve_rx_ctx {
1981344e751SDavid Awogbemila 	/* head and tail of skb chain for the current packet or NULL if none */
1991344e751SDavid Awogbemila 	struct sk_buff *skb_head;
2001344e751SDavid Awogbemila 	struct sk_buff *skb_tail;
20182fd151dSShailend Chand 	u32 total_size;
20282fd151dSShailend Chand 	u8 frag_cnt;
20382fd151dSShailend Chand 	bool drop_pkt;
20482fd151dSShailend Chand };
20582fd151dSShailend Chand 
20682fd151dSShailend Chand struct gve_rx_cnts {
20782fd151dSShailend Chand 	u32 ok_pkt_bytes;
20882fd151dSShailend Chand 	u16 ok_pkt_cnt;
20982fd151dSShailend Chand 	u16 total_pkt_cnt;
21082fd151dSShailend Chand 	u16 cont_pkt_cnt;
21182fd151dSShailend Chand 	u16 desc_err_pkt_cnt;
2121344e751SDavid Awogbemila };
2131344e751SDavid Awogbemila 
214a4aa1f1eSBailey Forrest /* Contains datapath state used to represent an RX queue. */
215f5cedc84SCatherine Sullivan struct gve_rx_ring {
216f5cedc84SCatherine Sullivan 	struct gve_priv *gve;
217a4aa1f1eSBailey Forrest 	union {
218a4aa1f1eSBailey Forrest 		/* GQI fields */
219a4aa1f1eSBailey Forrest 		struct {
220f5cedc84SCatherine Sullivan 			struct gve_rx_desc_queue desc;
221f5cedc84SCatherine Sullivan 			struct gve_rx_data_queue data;
222a4aa1f1eSBailey Forrest 
223a4aa1f1eSBailey Forrest 			/* threshold for posting new buffs and descs */
224a4aa1f1eSBailey Forrest 			u32 db_threshold;
22537149e93SDavid Awogbemila 			u16 packet_buffer_size;
22682fd151dSShailend Chand 
22782fd151dSShailend Chand 			u32 qpl_copy_pool_mask;
22882fd151dSShailend Chand 			u32 qpl_copy_pool_head;
22982fd151dSShailend Chand 			struct gve_rx_slot_page_info *qpl_copy_pool;
230a4aa1f1eSBailey Forrest 		};
231a4aa1f1eSBailey Forrest 
232a4aa1f1eSBailey Forrest 		/* DQO fields. */
233a4aa1f1eSBailey Forrest 		struct {
234a4aa1f1eSBailey Forrest 			struct gve_rx_buf_queue_dqo bufq;
235a4aa1f1eSBailey Forrest 			struct gve_rx_compl_queue_dqo complq;
236a4aa1f1eSBailey Forrest 
237a4aa1f1eSBailey Forrest 			struct gve_rx_buf_state_dqo *buf_states;
238a4aa1f1eSBailey Forrest 			u16 num_buf_states;
239a4aa1f1eSBailey Forrest 
240a4aa1f1eSBailey Forrest 			/* Linked list of gve_rx_buf_state_dqo. Index into
241a4aa1f1eSBailey Forrest 			 * buf_states, or -1 if empty.
242a4aa1f1eSBailey Forrest 			 */
243a4aa1f1eSBailey Forrest 			s16 free_buf_states;
244a4aa1f1eSBailey Forrest 
245a4aa1f1eSBailey Forrest 			/* Linked list of gve_rx_buf_state_dqo. Indexes into
246a4aa1f1eSBailey Forrest 			 * buf_states, or -1 if empty.
247a4aa1f1eSBailey Forrest 			 *
248a4aa1f1eSBailey Forrest 			 * This list contains buf_states which are pointing to
249a4aa1f1eSBailey Forrest 			 * valid buffers.
250a4aa1f1eSBailey Forrest 			 *
251a4aa1f1eSBailey Forrest 			 * We use a FIFO here in order to increase the
252a4aa1f1eSBailey Forrest 			 * probability that buffers can be reused by increasing
253a4aa1f1eSBailey Forrest 			 * the time between usages.
254a4aa1f1eSBailey Forrest 			 */
255a4aa1f1eSBailey Forrest 			struct gve_index_list recycled_buf_states;
256a4aa1f1eSBailey Forrest 
257a4aa1f1eSBailey Forrest 			/* Linked list of gve_rx_buf_state_dqo. Indexes into
258a4aa1f1eSBailey Forrest 			 * buf_states, or -1 if empty.
259a4aa1f1eSBailey Forrest 			 *
260a4aa1f1eSBailey Forrest 			 * This list contains buf_states which have buffers
261a4aa1f1eSBailey Forrest 			 * which cannot be reused yet.
262a4aa1f1eSBailey Forrest 			 */
263a4aa1f1eSBailey Forrest 			struct gve_index_list used_buf_states;
26466ce8e6bSRushil Gupta 
26566ce8e6bSRushil Gupta 			/* qpl assigned to this queue */
26666ce8e6bSRushil Gupta 			struct gve_queue_page_list *qpl;
267e7075ab4SRushil Gupta 
268e7075ab4SRushil Gupta 			/* index into queue page list */
269e7075ab4SRushil Gupta 			u32 next_qpl_page_idx;
270e7075ab4SRushil Gupta 
271e7075ab4SRushil Gupta 			/* track number of used buffers */
272e7075ab4SRushil Gupta 			u16 used_buf_states_cnt;
2735e37d825SJeroen de Borst 
2745e37d825SJeroen de Borst 			/* Address info of the buffers for header-split */
2755e37d825SJeroen de Borst 			struct gve_header_buf hdr_bufs;
276a4aa1f1eSBailey Forrest 		} dqo;
277a4aa1f1eSBailey Forrest 	};
278a4aa1f1eSBailey Forrest 
279f5cedc84SCatherine Sullivan 	u64 rbytes; /* free-running bytes received */
280056a7092SJeroen de Borst 	u64 rx_hsplit_bytes; /* free-running header bytes received */
281f5cedc84SCatherine Sullivan 	u64 rpackets; /* free-running packets received */
282438b43bdSCatherine Sullivan 	u32 cnt; /* free-running total number of completed packets */
283438b43bdSCatherine Sullivan 	u32 fill_cnt; /* free-running total number of descs and buffs posted */
284438b43bdSCatherine Sullivan 	u32 mask; /* masks the cnt and fill_cnt to the size of the ring */
285056a7092SJeroen de Borst 	u64 rx_hsplit_pkt; /* free-running packets with headers split */
286433e274bSKuo Zhao 	u64 rx_copybreak_pkt; /* free-running count of copybreak packets */
287433e274bSKuo Zhao 	u64 rx_copied_pkt; /* free-running total number of copied packets */
288433e274bSKuo Zhao 	u64 rx_skb_alloc_fail; /* free-running count of skb alloc fails */
289433e274bSKuo Zhao 	u64 rx_buf_alloc_fail; /* free-running count of buffer alloc fails */
290433e274bSKuo Zhao 	u64 rx_desc_err_dropped_pkt; /* free-running count of packets dropped by descriptor error */
291056a7092SJeroen de Borst 	/* free-running count of unsplit packets due to header buffer overflow or hdr_len is 0 */
292056a7092SJeroen de Borst 	u64 rx_hsplit_unsplit_pkt;
29337149e93SDavid Awogbemila 	u64 rx_cont_packet_cnt; /* free-running multi-fragment packets received */
29437149e93SDavid Awogbemila 	u64 rx_frag_flip_cnt; /* free-running count of rx segments where page_flip was used */
29582fd151dSShailend Chand 	u64 rx_frag_copy_cnt; /* free-running count of rx segments copied */
29682fd151dSShailend Chand 	u64 rx_frag_alloc_cnt; /* free-running count of rx page allocations */
29775eaae15SPraveen Kaligineedi 	u64 xdp_tx_errors;
29875eaae15SPraveen Kaligineedi 	u64 xdp_redirect_errors;
29939a7f4aaSPraveen Kaligineedi 	u64 xdp_alloc_fails;
30075eaae15SPraveen Kaligineedi 	u64 xdp_actions[GVE_XDP_ACTIONS];
301f5cedc84SCatherine Sullivan 	u32 q_num; /* queue index */
302f5cedc84SCatherine Sullivan 	u32 ntfy_id; /* notification block index */
303f5cedc84SCatherine Sullivan 	struct gve_queue_resources *q_resources; /* head and tail pointer idx */
304f5cedc84SCatherine Sullivan 	dma_addr_t q_resources_bus; /* dma address for the queue resources */
305f5cedc84SCatherine Sullivan 	struct u64_stats_sync statss; /* sync stats for 32bit archs */
3069c1a59a2SBailey Forrest 
3071344e751SDavid Awogbemila 	struct gve_rx_ctx ctx; /* Info for packet currently being processed in this ring. */
30875eaae15SPraveen Kaligineedi 
30975eaae15SPraveen Kaligineedi 	/* XDP stuff */
31075eaae15SPraveen Kaligineedi 	struct xdp_rxq_info xdp_rxq;
311fd8e4032SPraveen Kaligineedi 	struct xdp_rxq_info xsk_rxq;
312fd8e4032SPraveen Kaligineedi 	struct xsk_buff_pool *xsk_pool;
31339a7f4aaSPraveen Kaligineedi 	struct page_frag_cache page_cache; /* Page cache to allocate XDP frames */
314f5cedc84SCatherine Sullivan };
315f5cedc84SCatherine Sullivan 
316f5cedc84SCatherine Sullivan /* A TX desc ring entry */
317f5cedc84SCatherine Sullivan union gve_tx_desc {
318f5cedc84SCatherine Sullivan 	struct gve_tx_pkt_desc pkt; /* first desc for a packet */
319497dbb2bSWillem de Bruijn 	struct gve_tx_mtd_desc mtd; /* optional metadata descriptor */
320f5cedc84SCatherine Sullivan 	struct gve_tx_seg_desc seg; /* subsequent descs for a packet */
321f5cedc84SCatherine Sullivan };
322f5cedc84SCatherine Sullivan 
323f5cedc84SCatherine Sullivan /* Tracks the memory in the fifo occupied by a segment of a packet */
324f5cedc84SCatherine Sullivan struct gve_tx_iovec {
325f5cedc84SCatherine Sullivan 	u32 iov_offset; /* offset into this segment */
326f5cedc84SCatherine Sullivan 	u32 iov_len; /* length */
327f5cedc84SCatherine Sullivan 	u32 iov_padding; /* padding associated with this segment */
328f5cedc84SCatherine Sullivan };
329f5cedc84SCatherine Sullivan 
330f5cedc84SCatherine Sullivan /* Tracks the memory in the fifo occupied by the skb. Mapped 1:1 to a desc
331f5cedc84SCatherine Sullivan  * ring entry but only used for a pkt_desc not a seg_desc
332f5cedc84SCatherine Sullivan  */
333f5cedc84SCatherine Sullivan struct gve_tx_buffer_state {
33439a7f4aaSPraveen Kaligineedi 	union {
335f5cedc84SCatherine Sullivan 		struct sk_buff *skb; /* skb for this pkt */
33639a7f4aaSPraveen Kaligineedi 		struct xdp_frame *xdp_frame; /* xdp_frame */
33739a7f4aaSPraveen Kaligineedi 	};
33875eaae15SPraveen Kaligineedi 	struct {
33975eaae15SPraveen Kaligineedi 		u16 size; /* size of xmitted xdp pkt */
340fd8e4032SPraveen Kaligineedi 		u8 is_xsk; /* xsk buff */
34175eaae15SPraveen Kaligineedi 	} xdp;
3426f007c64SCatherine Sullivan 	union {
343f5cedc84SCatherine Sullivan 		struct gve_tx_iovec iov[GVE_TX_MAX_IOVEC]; /* segments of this pkt */
3441e0083bdSArnd Bergmann 		struct {
3451e0083bdSArnd Bergmann 			DEFINE_DMA_UNMAP_ADDR(dma);
3461e0083bdSArnd Bergmann 			DEFINE_DMA_UNMAP_LEN(len);
3471e0083bdSArnd Bergmann 		};
3486f007c64SCatherine Sullivan 	};
349f5cedc84SCatherine Sullivan };
350f5cedc84SCatherine Sullivan 
351f5cedc84SCatherine Sullivan /* A TX buffer - each queue has one */
352f5cedc84SCatherine Sullivan struct gve_tx_fifo {
353f5cedc84SCatherine Sullivan 	void *base; /* address of base of FIFO */
354f5cedc84SCatherine Sullivan 	u32 size; /* total size */
355f5cedc84SCatherine Sullivan 	atomic_t available; /* how much space is still available */
356f5cedc84SCatherine Sullivan 	u32 head; /* offset to write at */
357f5cedc84SCatherine Sullivan 	struct gve_queue_page_list *qpl; /* QPL mapped into this FIFO */
358f5cedc84SCatherine Sullivan };
359f5cedc84SCatherine Sullivan 
360a4aa1f1eSBailey Forrest /* TX descriptor for DQO format */
361a4aa1f1eSBailey Forrest union gve_tx_desc_dqo {
362a4aa1f1eSBailey Forrest 	struct gve_tx_pkt_desc_dqo pkt;
363a4aa1f1eSBailey Forrest 	struct gve_tx_tso_context_desc_dqo tso_ctx;
364a4aa1f1eSBailey Forrest 	struct gve_tx_general_context_desc_dqo general_ctx;
365a4aa1f1eSBailey Forrest };
366a4aa1f1eSBailey Forrest 
367a4aa1f1eSBailey Forrest enum gve_packet_state {
368a4aa1f1eSBailey Forrest 	/* Packet is in free list, available to be allocated.
369a4aa1f1eSBailey Forrest 	 * This should always be zero since state is not explicitly initialized.
370a4aa1f1eSBailey Forrest 	 */
371a4aa1f1eSBailey Forrest 	GVE_PACKET_STATE_UNALLOCATED,
372a4aa1f1eSBailey Forrest 	/* Packet is expecting a regular data completion or miss completion */
373a4aa1f1eSBailey Forrest 	GVE_PACKET_STATE_PENDING_DATA_COMPL,
374a4aa1f1eSBailey Forrest 	/* Packet has received a miss completion and is expecting a
375a4aa1f1eSBailey Forrest 	 * re-injection completion.
376a4aa1f1eSBailey Forrest 	 */
377a4aa1f1eSBailey Forrest 	GVE_PACKET_STATE_PENDING_REINJECT_COMPL,
378a4aa1f1eSBailey Forrest 	/* No valid completion received within the specified timeout. */
379a4aa1f1eSBailey Forrest 	GVE_PACKET_STATE_TIMED_OUT_COMPL,
380a4aa1f1eSBailey Forrest };
381a4aa1f1eSBailey Forrest 
382a4aa1f1eSBailey Forrest struct gve_tx_pending_packet_dqo {
383a4aa1f1eSBailey Forrest 	struct sk_buff *skb; /* skb for this packet */
384a4aa1f1eSBailey Forrest 
385a4aa1f1eSBailey Forrest 	/* 0th element corresponds to the linear portion of `skb`, should be
386a4aa1f1eSBailey Forrest 	 * unmapped with `dma_unmap_single`.
387a4aa1f1eSBailey Forrest 	 *
388a4aa1f1eSBailey Forrest 	 * All others correspond to `skb`'s frags and should be unmapped with
389a4aa1f1eSBailey Forrest 	 * `dma_unmap_page`.
390a4aa1f1eSBailey Forrest 	 */
391a6fb8d5aSRushil Gupta 	union {
392a6fb8d5aSRushil Gupta 		struct {
3931e0083bdSArnd Bergmann 			DEFINE_DMA_UNMAP_ADDR(dma[MAX_SKB_FRAGS + 1]);
3941e0083bdSArnd Bergmann 			DEFINE_DMA_UNMAP_LEN(len[MAX_SKB_FRAGS + 1]);
395a6fb8d5aSRushil Gupta 		};
396a6fb8d5aSRushil Gupta 		s16 tx_qpl_buf_ids[GVE_MAX_TX_BUFS_PER_PKT];
397a6fb8d5aSRushil Gupta 	};
398a6fb8d5aSRushil Gupta 
399a4aa1f1eSBailey Forrest 	u16 num_bufs;
400a4aa1f1eSBailey Forrest 
401a4aa1f1eSBailey Forrest 	/* Linked list index to next element in the list, or -1 if none */
402a4aa1f1eSBailey Forrest 	s16 next;
403a4aa1f1eSBailey Forrest 
404a4aa1f1eSBailey Forrest 	/* Linked list index to prev element in the list, or -1 if none.
405a4aa1f1eSBailey Forrest 	 * Used for tracking either outstanding miss completions or prematurely
406a4aa1f1eSBailey Forrest 	 * freed packets.
407a4aa1f1eSBailey Forrest 	 */
408a4aa1f1eSBailey Forrest 	s16 prev;
409a4aa1f1eSBailey Forrest 
410a4aa1f1eSBailey Forrest 	/* Identifies the current state of the packet as defined in
411a4aa1f1eSBailey Forrest 	 * `enum gve_packet_state`.
412a4aa1f1eSBailey Forrest 	 */
413a4aa1f1eSBailey Forrest 	u8 state;
414a4aa1f1eSBailey Forrest 
415a4aa1f1eSBailey Forrest 	/* If packet is an outstanding miss completion, then the packet is
416a4aa1f1eSBailey Forrest 	 * freed if the corresponding re-injection completion is not received
417a4aa1f1eSBailey Forrest 	 * before kernel jiffies exceeds timeout_jiffies.
418a4aa1f1eSBailey Forrest 	 */
419a4aa1f1eSBailey Forrest 	unsigned long timeout_jiffies;
420a4aa1f1eSBailey Forrest };
421a4aa1f1eSBailey Forrest 
422a4aa1f1eSBailey Forrest /* Contains datapath state used to represent a TX queue. */
423f5cedc84SCatherine Sullivan struct gve_tx_ring {
424f5cedc84SCatherine Sullivan 	/* Cacheline 0 -- Accessed & dirtied during transmit */
425a4aa1f1eSBailey Forrest 	union {
426a4aa1f1eSBailey Forrest 		/* GQI fields */
427a4aa1f1eSBailey Forrest 		struct {
428f5cedc84SCatherine Sullivan 			struct gve_tx_fifo tx_fifo;
429f5cedc84SCatherine Sullivan 			u32 req; /* driver tracked head pointer */
430f5cedc84SCatherine Sullivan 			u32 done; /* driver tracked tail pointer */
431a4aa1f1eSBailey Forrest 		};
432a4aa1f1eSBailey Forrest 
433a4aa1f1eSBailey Forrest 		/* DQO fields. */
434a4aa1f1eSBailey Forrest 		struct {
435a4aa1f1eSBailey Forrest 			/* Linked list of gve_tx_pending_packet_dqo. Index into
436a4aa1f1eSBailey Forrest 			 * pending_packets, or -1 if empty.
437a4aa1f1eSBailey Forrest 			 *
438a4aa1f1eSBailey Forrest 			 * This is a consumer list owned by the TX path. When it
439a4aa1f1eSBailey Forrest 			 * runs out, the producer list is stolen from the
440a4aa1f1eSBailey Forrest 			 * completion handling path
441a4aa1f1eSBailey Forrest 			 * (dqo_compl.free_pending_packets).
442a4aa1f1eSBailey Forrest 			 */
443a4aa1f1eSBailey Forrest 			s16 free_pending_packets;
444a4aa1f1eSBailey Forrest 
445a4aa1f1eSBailey Forrest 			/* Cached value of `dqo_compl.hw_tx_head` */
446a4aa1f1eSBailey Forrest 			u32 head;
447a4aa1f1eSBailey Forrest 			u32 tail; /* Last posted buffer index + 1 */
448a4aa1f1eSBailey Forrest 
449a4aa1f1eSBailey Forrest 			/* Index of the last descriptor with "report event" bit
450a4aa1f1eSBailey Forrest 			 * set.
451a4aa1f1eSBailey Forrest 			 */
452a4aa1f1eSBailey Forrest 			u32 last_re_idx;
453a6fb8d5aSRushil Gupta 
454a6fb8d5aSRushil Gupta 			/* free running number of packet buf descriptors posted */
455a6fb8d5aSRushil Gupta 			u16 posted_packet_desc_cnt;
456a6fb8d5aSRushil Gupta 			/* free running number of packet buf descriptors completed */
457a6fb8d5aSRushil Gupta 			u16 completed_packet_desc_cnt;
458a6fb8d5aSRushil Gupta 
459a6fb8d5aSRushil Gupta 			/* QPL fields */
460a6fb8d5aSRushil Gupta 			struct {
461a6fb8d5aSRushil Gupta 			       /* Linked list of gve_tx_buf_dqo. Index into
462a6fb8d5aSRushil Gupta 				* tx_qpl_buf_next, or -1 if empty.
463a6fb8d5aSRushil Gupta 				*
464a6fb8d5aSRushil Gupta 				* This is a consumer list owned by the TX path. When it
465a6fb8d5aSRushil Gupta 				* runs out, the producer list is stolen from the
466a6fb8d5aSRushil Gupta 				* completion handling path
467a6fb8d5aSRushil Gupta 				* (dqo_compl.free_tx_qpl_buf_head).
468a6fb8d5aSRushil Gupta 				*/
469a6fb8d5aSRushil Gupta 				s16 free_tx_qpl_buf_head;
470a6fb8d5aSRushil Gupta 
471a6fb8d5aSRushil Gupta 			       /* Free running count of the number of QPL tx buffers
472a6fb8d5aSRushil Gupta 				* allocated
473a6fb8d5aSRushil Gupta 				*/
474a6fb8d5aSRushil Gupta 				u32 alloc_tx_qpl_buf_cnt;
475a6fb8d5aSRushil Gupta 
476a6fb8d5aSRushil Gupta 				/* Cached value of `dqo_compl.free_tx_qpl_buf_cnt` */
477a6fb8d5aSRushil Gupta 				u32 free_tx_qpl_buf_cnt;
478a6fb8d5aSRushil Gupta 			};
479a4aa1f1eSBailey Forrest 		} dqo_tx;
480a4aa1f1eSBailey Forrest 	};
481f5cedc84SCatherine Sullivan 
482f5cedc84SCatherine Sullivan 	/* Cacheline 1 -- Accessed & dirtied during gve_clean_tx_done */
483a4aa1f1eSBailey Forrest 	union {
484a4aa1f1eSBailey Forrest 		/* GQI fields */
485a4aa1f1eSBailey Forrest 		struct {
48661d72c7eSTao Liu 			/* Spinlock for when cleanup in progress */
48761d72c7eSTao Liu 			spinlock_t clean_lock;
48839a7f4aaSPraveen Kaligineedi 			/* Spinlock for XDP tx traffic */
48939a7f4aaSPraveen Kaligineedi 			spinlock_t xdp_lock;
490a4aa1f1eSBailey Forrest 		};
491a4aa1f1eSBailey Forrest 
492a4aa1f1eSBailey Forrest 		/* DQO fields. */
493a4aa1f1eSBailey Forrest 		struct {
494a4aa1f1eSBailey Forrest 			u32 head; /* Last read on compl_desc */
495a4aa1f1eSBailey Forrest 
496a4aa1f1eSBailey Forrest 			/* Tracks the current gen bit of compl_q */
497a4aa1f1eSBailey Forrest 			u8 cur_gen_bit;
498a4aa1f1eSBailey Forrest 
499a4aa1f1eSBailey Forrest 			/* Linked list of gve_tx_pending_packet_dqo. Index into
500a4aa1f1eSBailey Forrest 			 * pending_packets, or -1 if empty.
501a4aa1f1eSBailey Forrest 			 *
502a4aa1f1eSBailey Forrest 			 * This is the producer list, owned by the completion
503a4aa1f1eSBailey Forrest 			 * handling path. When the consumer list
504a4aa1f1eSBailey Forrest 			 * (dqo_tx.free_pending_packets) is runs out, this list
505a4aa1f1eSBailey Forrest 			 * will be stolen.
506a4aa1f1eSBailey Forrest 			 */
507a4aa1f1eSBailey Forrest 			atomic_t free_pending_packets;
508a4aa1f1eSBailey Forrest 
509a4aa1f1eSBailey Forrest 			/* Last TX ring index fetched by HW */
510a4aa1f1eSBailey Forrest 			atomic_t hw_tx_head;
511a4aa1f1eSBailey Forrest 
512a4aa1f1eSBailey Forrest 			/* List to track pending packets which received a miss
513a4aa1f1eSBailey Forrest 			 * completion but not a corresponding reinjection.
514a4aa1f1eSBailey Forrest 			 */
515a4aa1f1eSBailey Forrest 			struct gve_index_list miss_completions;
516a4aa1f1eSBailey Forrest 
517a4aa1f1eSBailey Forrest 			/* List to track pending packets that were completed
518a4aa1f1eSBailey Forrest 			 * before receiving a valid completion because they
519a4aa1f1eSBailey Forrest 			 * reached a specified timeout.
520a4aa1f1eSBailey Forrest 			 */
521a4aa1f1eSBailey Forrest 			struct gve_index_list timed_out_completions;
522a6fb8d5aSRushil Gupta 
523a6fb8d5aSRushil Gupta 			/* QPL fields */
524a6fb8d5aSRushil Gupta 			struct {
525a6fb8d5aSRushil Gupta 				/* Linked list of gve_tx_buf_dqo. Index into
526a6fb8d5aSRushil Gupta 				 * tx_qpl_buf_next, or -1 if empty.
527a6fb8d5aSRushil Gupta 				 *
528a6fb8d5aSRushil Gupta 				 * This is the producer list, owned by the completion
529a6fb8d5aSRushil Gupta 				 * handling path. When the consumer list
530a6fb8d5aSRushil Gupta 				 * (dqo_tx.free_tx_qpl_buf_head) is runs out, this list
531a6fb8d5aSRushil Gupta 				 * will be stolen.
532a6fb8d5aSRushil Gupta 				 */
533a6fb8d5aSRushil Gupta 				atomic_t free_tx_qpl_buf_head;
534a6fb8d5aSRushil Gupta 
535a6fb8d5aSRushil Gupta 				/* Free running count of the number of tx buffers
536a6fb8d5aSRushil Gupta 				 * freed
537a6fb8d5aSRushil Gupta 				 */
538a6fb8d5aSRushil Gupta 				atomic_t free_tx_qpl_buf_cnt;
539a6fb8d5aSRushil Gupta 			};
540a4aa1f1eSBailey Forrest 		} dqo_compl;
541a4aa1f1eSBailey Forrest 	} ____cacheline_aligned;
542f5cedc84SCatherine Sullivan 	u64 pkt_done; /* free-running - total packets completed */
543f5cedc84SCatherine Sullivan 	u64 bytes_done; /* free-running - total bytes completed */
5446f007c64SCatherine Sullivan 	u64 dropped_pkt; /* free-running - total packets dropped */
5456f007c64SCatherine Sullivan 	u64 dma_mapping_error; /* count of dma mapping errors */
546f5cedc84SCatherine Sullivan 
547f5cedc84SCatherine Sullivan 	/* Cacheline 2 -- Read-mostly fields */
548a4aa1f1eSBailey Forrest 	union {
549a4aa1f1eSBailey Forrest 		/* GQI fields */
550a4aa1f1eSBailey Forrest 		struct {
551a4aa1f1eSBailey Forrest 			union gve_tx_desc *desc;
552a4aa1f1eSBailey Forrest 
553a4aa1f1eSBailey Forrest 			/* Maps 1:1 to a desc */
554a4aa1f1eSBailey Forrest 			struct gve_tx_buffer_state *info;
555a4aa1f1eSBailey Forrest 		};
556a4aa1f1eSBailey Forrest 
557a4aa1f1eSBailey Forrest 		/* DQO fields. */
558a4aa1f1eSBailey Forrest 		struct {
559a4aa1f1eSBailey Forrest 			union gve_tx_desc_dqo *tx_ring;
560a4aa1f1eSBailey Forrest 			struct gve_tx_compl_desc *compl_ring;
561a4aa1f1eSBailey Forrest 
562a4aa1f1eSBailey Forrest 			struct gve_tx_pending_packet_dqo *pending_packets;
563a4aa1f1eSBailey Forrest 			s16 num_pending_packets;
564a4aa1f1eSBailey Forrest 
565a4aa1f1eSBailey Forrest 			u32 complq_mask; /* complq size is complq_mask + 1 */
56666ce8e6bSRushil Gupta 
56766ce8e6bSRushil Gupta 			/* QPL fields */
56866ce8e6bSRushil Gupta 			struct {
56966ce8e6bSRushil Gupta 				/* qpl assigned to this queue */
57066ce8e6bSRushil Gupta 				struct gve_queue_page_list *qpl;
571a6fb8d5aSRushil Gupta 
572a6fb8d5aSRushil Gupta 				/* Each QPL page is divided into TX bounce buffers
573a6fb8d5aSRushil Gupta 				 * of size GVE_TX_BUF_SIZE_DQO. tx_qpl_buf_next is
574a6fb8d5aSRushil Gupta 				 * an array to manage linked lists of TX buffers.
575a6fb8d5aSRushil Gupta 				 * An entry j at index i implies that j'th buffer
576a6fb8d5aSRushil Gupta 				 * is next on the list after i
577a6fb8d5aSRushil Gupta 				 */
578a6fb8d5aSRushil Gupta 				s16 *tx_qpl_buf_next;
579a6fb8d5aSRushil Gupta 				u32 num_tx_qpl_bufs;
58066ce8e6bSRushil Gupta 			};
581a4aa1f1eSBailey Forrest 		} dqo;
582a4aa1f1eSBailey Forrest 	} ____cacheline_aligned;
583f5cedc84SCatherine Sullivan 	struct netdev_queue *netdev_txq;
584f5cedc84SCatherine Sullivan 	struct gve_queue_resources *q_resources; /* head and tail pointer idx */
5856f007c64SCatherine Sullivan 	struct device *dev;
586f5cedc84SCatherine Sullivan 	u32 mask; /* masks req and done down to queue size */
5876f007c64SCatherine Sullivan 	u8 raw_addressing; /* use raw_addressing? */
588f5cedc84SCatherine Sullivan 
589f5cedc84SCatherine Sullivan 	/* Slow-path fields */
590f5cedc84SCatherine Sullivan 	u32 q_num ____cacheline_aligned; /* queue idx */
591f5cedc84SCatherine Sullivan 	u32 stop_queue; /* count of queue stops */
592f5cedc84SCatherine Sullivan 	u32 wake_queue; /* count of queue wakes */
59387a7f321SJohn Fraker 	u32 queue_timeout; /* count of queue timeouts */
594f5cedc84SCatherine Sullivan 	u32 ntfy_id; /* notification block index */
59587a7f321SJohn Fraker 	u32 last_kick_msec; /* Last time the queue was kicked */
596f5cedc84SCatherine Sullivan 	dma_addr_t bus; /* dma address of the descr ring */
597f5cedc84SCatherine Sullivan 	dma_addr_t q_resources_bus; /* dma address of the queue resources */
598a4aa1f1eSBailey Forrest 	dma_addr_t complq_bus_dqo; /* dma address of the dqo.compl_ring */
599f5cedc84SCatherine Sullivan 	struct u64_stats_sync statss; /* sync stats for 32bit archs */
600fd8e4032SPraveen Kaligineedi 	struct xsk_buff_pool *xsk_pool;
601fd8e4032SPraveen Kaligineedi 	u32 xdp_xsk_wakeup;
602fd8e4032SPraveen Kaligineedi 	u32 xdp_xsk_done;
603fd8e4032SPraveen Kaligineedi 	u64 xdp_xsk_sent;
60439a7f4aaSPraveen Kaligineedi 	u64 xdp_xmit;
60539a7f4aaSPraveen Kaligineedi 	u64 xdp_xmit_errors;
606f5cedc84SCatherine Sullivan } ____cacheline_aligned;
607f5cedc84SCatherine Sullivan 
608f5cedc84SCatherine Sullivan /* Wraps the info for one irq including the napi struct and the queues
609f5cedc84SCatherine Sullivan  * associated with that irq.
610f5cedc84SCatherine Sullivan  */
611893ce44dSCatherine Sullivan struct gve_notify_block {
612d30baaccSCatherine Sullivan 	__be32 *irq_db_index; /* pointer to idx into Bar2 */
613893ce44dSCatherine Sullivan 	char name[IFNAMSIZ + 16]; /* name registered with the kernel */
614893ce44dSCatherine Sullivan 	struct napi_struct napi; /* kernel napi struct for this block */
615893ce44dSCatherine Sullivan 	struct gve_priv *priv;
616f5cedc84SCatherine Sullivan 	struct gve_tx_ring *tx; /* tx rings on this block */
617f5cedc84SCatherine Sullivan 	struct gve_rx_ring *rx; /* rx rings on this block */
6189a5e0776SShailend Chand 	u32 irq;
619d30baaccSCatherine Sullivan };
620893ce44dSCatherine Sullivan 
621f5cedc84SCatherine Sullivan /* Tracks allowed and current queue settings */
622f5cedc84SCatherine Sullivan struct gve_queue_config {
623f5cedc84SCatherine Sullivan 	u16 max_queues;
624f5cedc84SCatherine Sullivan 	u16 num_queues; /* current */
625f5cedc84SCatherine Sullivan };
626f5cedc84SCatherine Sullivan 
627f5cedc84SCatherine Sullivan /* Tracks the available and used qpl IDs */
628f5cedc84SCatherine Sullivan struct gve_qpl_config {
629f5cedc84SCatherine Sullivan 	u32 qpl_map_size; /* map memory size */
630f5cedc84SCatherine Sullivan 	unsigned long *qpl_id_map; /* bitmap of used qpl ids */
631f5cedc84SCatherine Sullivan };
632f5cedc84SCatherine Sullivan 
633d30baaccSCatherine Sullivan struct gve_irq_db {
634d30baaccSCatherine Sullivan 	__be32 index;
635d30baaccSCatherine Sullivan } ____cacheline_aligned;
636d30baaccSCatherine Sullivan 
637c4b87ac8SBailey Forrest struct gve_ptype {
638c4b87ac8SBailey Forrest 	u8 l3_type;  /* `gve_l3_type` in gve_adminq.h */
639c4b87ac8SBailey Forrest 	u8 l4_type;  /* `gve_l4_type` in gve_adminq.h */
640c4b87ac8SBailey Forrest };
641c4b87ac8SBailey Forrest 
642c4b87ac8SBailey Forrest struct gve_ptype_lut {
643c4b87ac8SBailey Forrest 	struct gve_ptype ptypes[GVE_NUM_PTYPES];
644c4b87ac8SBailey Forrest };
645c4b87ac8SBailey Forrest 
6467cea48b9SShailend Chand /* Parameters for allocating resources for tx queues */
6477cea48b9SShailend Chand struct gve_tx_alloc_rings_cfg {
6487cea48b9SShailend Chand 	struct gve_queue_config *qcfg;
6497cea48b9SShailend Chand 
6507cea48b9SShailend Chand 	u16 ring_size;
6517cea48b9SShailend Chand 	u16 start_idx;
6527cea48b9SShailend Chand 	u16 num_rings;
6537cea48b9SShailend Chand 	bool raw_addressing;
6547cea48b9SShailend Chand 
6557cea48b9SShailend Chand 	/* Allocated resources are returned here */
6567cea48b9SShailend Chand 	struct gve_tx_ring *tx;
6577cea48b9SShailend Chand };
6587cea48b9SShailend Chand 
6597cea48b9SShailend Chand /* Parameters for allocating resources for rx queues */
6607cea48b9SShailend Chand struct gve_rx_alloc_rings_cfg {
6617cea48b9SShailend Chand 	/* tx config is also needed to determine QPL ids */
6627cea48b9SShailend Chand 	struct gve_queue_config *qcfg;
6637cea48b9SShailend Chand 	struct gve_queue_config *qcfg_tx;
6647cea48b9SShailend Chand 
6657cea48b9SShailend Chand 	u16 ring_size;
6665e37d825SJeroen de Borst 	u16 packet_buffer_size;
6677cea48b9SShailend Chand 	bool raw_addressing;
6687cea48b9SShailend Chand 	bool enable_header_split;
6697cea48b9SShailend Chand 
6707cea48b9SShailend Chand 	/* Allocated resources are returned here */
6717cea48b9SShailend Chand 	struct gve_rx_ring *rx;
6727cea48b9SShailend Chand };
6737cea48b9SShailend Chand 
674a5886ef4SBailey Forrest /* GVE_QUEUE_FORMAT_UNSPECIFIED must be zero since 0 is the default value
675a5886ef4SBailey Forrest  * when the entire configure_device_resources command is zeroed out and the
676a5886ef4SBailey Forrest  * queue_format is not specified.
677a5886ef4SBailey Forrest  */
678a5886ef4SBailey Forrest enum gve_queue_format {
679a5886ef4SBailey Forrest 	GVE_QUEUE_FORMAT_UNSPECIFIED	= 0x0,
680a5886ef4SBailey Forrest 	GVE_GQI_RDA_FORMAT		= 0x1,
681a5886ef4SBailey Forrest 	GVE_GQI_QPL_FORMAT		= 0x2,
682a5886ef4SBailey Forrest 	GVE_DQO_RDA_FORMAT		= 0x3,
68366ce8e6bSRushil Gupta 	GVE_DQO_QPL_FORMAT		= 0x4,
684a5886ef4SBailey Forrest };
685a5886ef4SBailey Forrest 
68657718b60SJeroen de Borst struct gve_flow_spec {
68757718b60SJeroen de Borst 	__be32 src_ip[4];
68857718b60SJeroen de Borst 	__be32 dst_ip[4];
68957718b60SJeroen de Borst 	union {
69057718b60SJeroen de Borst 		struct {
69157718b60SJeroen de Borst 			__be16 src_port;
69257718b60SJeroen de Borst 			__be16 dst_port;
69357718b60SJeroen de Borst 		};
69457718b60SJeroen de Borst 		__be32 spi;
69557718b60SJeroen de Borst 	};
69657718b60SJeroen de Borst 	union {
69757718b60SJeroen de Borst 		u8 tos;
69857718b60SJeroen de Borst 		u8 tclass;
69957718b60SJeroen de Borst 	};
70057718b60SJeroen de Borst };
70157718b60SJeroen de Borst 
70257718b60SJeroen de Borst struct gve_flow_rule {
70357718b60SJeroen de Borst 	u32 location;
70457718b60SJeroen de Borst 	u16 flow_type;
70557718b60SJeroen de Borst 	u16 action;
70657718b60SJeroen de Borst 	struct gve_flow_spec key;
70757718b60SJeroen de Borst 	struct gve_flow_spec mask;
70857718b60SJeroen de Borst };
70957718b60SJeroen de Borst 
71057718b60SJeroen de Borst struct gve_flow_rules_cache {
71157718b60SJeroen de Borst 	bool rules_cache_synced; /* False if the driver's rules_cache is outdated */
71257718b60SJeroen de Borst 	struct gve_adminq_queried_flow_rule *rules_cache;
71357718b60SJeroen de Borst 	__be32 *rule_ids_cache;
71457718b60SJeroen de Borst 	/* The total number of queried rules that stored in the caches */
71557718b60SJeroen de Borst 	u32 rules_cache_num;
71657718b60SJeroen de Borst 	u32 rule_ids_cache_num;
71757718b60SJeroen de Borst };
71857718b60SJeroen de Borst 
719893ce44dSCatherine Sullivan struct gve_priv {
720893ce44dSCatherine Sullivan 	struct net_device *dev;
721f5cedc84SCatherine Sullivan 	struct gve_tx_ring *tx; /* array of tx_cfg.num_queues */
722f5cedc84SCatherine Sullivan 	struct gve_rx_ring *rx; /* array of rx_cfg.num_queues */
723893ce44dSCatherine Sullivan 	struct gve_notify_block *ntfy_blocks; /* array of num_ntfy_blks */
724d30baaccSCatherine Sullivan 	struct gve_irq_db *irq_db_indices; /* array of num_ntfy_blks */
725d30baaccSCatherine Sullivan 	dma_addr_t irq_db_indices_bus;
726893ce44dSCatherine Sullivan 	struct msix_entry *msix_vectors; /* array of num_ntfy_blks + 1 */
727893ce44dSCatherine Sullivan 	char mgmt_msix_name[IFNAMSIZ + 16];
728893ce44dSCatherine Sullivan 	u32 mgmt_msix_idx;
729893ce44dSCatherine Sullivan 	__be32 *counter_array; /* array of num_event_counters */
730893ce44dSCatherine Sullivan 	dma_addr_t counter_array_bus;
731893ce44dSCatherine Sullivan 
732893ce44dSCatherine Sullivan 	u16 num_event_counters;
733f5cedc84SCatherine Sullivan 	u16 tx_desc_cnt; /* num desc per ring */
734f5cedc84SCatherine Sullivan 	u16 rx_desc_cnt; /* num desc per ring */
735ed4fb326SHarshitha Ramamurthy 	u16 max_tx_desc_cnt;
736ed4fb326SHarshitha Ramamurthy 	u16 max_rx_desc_cnt;
737ed4fb326SHarshitha Ramamurthy 	u16 min_tx_desc_cnt;
738ed4fb326SHarshitha Ramamurthy 	u16 min_rx_desc_cnt;
739ed4fb326SHarshitha Ramamurthy 	bool modify_ring_size_enabled;
740ed4fb326SHarshitha Ramamurthy 	bool default_min_ring_size;
74166ce8e6bSRushil Gupta 	u16 tx_pages_per_qpl; /* Suggested number of pages per qpl for TX queues by NIC */
742f5cedc84SCatherine Sullivan 	u64 max_registered_pages;
743f5cedc84SCatherine Sullivan 	u64 num_registered_pages; /* num pages registered with NIC */
74475eaae15SPraveen Kaligineedi 	struct bpf_prog *xdp_prog; /* XDP BPF program */
745f5cedc84SCatherine Sullivan 	u32 rx_copybreak; /* copy packets smaller than this */
746f5cedc84SCatherine Sullivan 	u16 default_num_queues; /* default num queues to set up */
747893ce44dSCatherine Sullivan 
74875eaae15SPraveen Kaligineedi 	u16 num_xdp_queues;
749f5cedc84SCatherine Sullivan 	struct gve_queue_config tx_cfg;
750f5cedc84SCatherine Sullivan 	struct gve_queue_config rx_cfg;
751893ce44dSCatherine Sullivan 	u32 num_ntfy_blks; /* spilt between TX and RX so must be even */
752893ce44dSCatherine Sullivan 
753893ce44dSCatherine Sullivan 	struct gve_registers __iomem *reg_bar0; /* see gve_register.h */
754893ce44dSCatherine Sullivan 	__be32 __iomem *db_bar2; /* "array" of doorbells */
755893ce44dSCatherine Sullivan 	u32 msg_enable;	/* level for netif* netdev print macros	*/
756893ce44dSCatherine Sullivan 	struct pci_dev *pdev;
757893ce44dSCatherine Sullivan 
758f5cedc84SCatherine Sullivan 	/* metrics */
759f5cedc84SCatherine Sullivan 	u32 tx_timeo_cnt;
760f5cedc84SCatherine Sullivan 
761893ce44dSCatherine Sullivan 	/* Admin queue - see gve_adminq.h*/
762893ce44dSCatherine Sullivan 	union gve_adminq_command *adminq;
763893ce44dSCatherine Sullivan 	dma_addr_t adminq_bus_addr;
764955f4d3bSJohn Fraker 	struct dma_pool *adminq_pool;
7651108566cSZiwei Xiao 	struct mutex adminq_lock; /* Protects adminq command execution */
766893ce44dSCatherine Sullivan 	u32 adminq_mask; /* masks prod_cnt to adminq size */
767893ce44dSCatherine Sullivan 	u32 adminq_prod_cnt; /* free-running count of AQ cmds executed */
768433e274bSKuo Zhao 	u32 adminq_cmd_fail; /* free-running count of AQ cmds failed */
769433e274bSKuo Zhao 	u32 adminq_timeouts; /* free-running count of AQ cmds timeouts */
770433e274bSKuo Zhao 	/* free-running count of per AQ cmd executed */
771433e274bSKuo Zhao 	u32 adminq_describe_device_cnt;
772433e274bSKuo Zhao 	u32 adminq_cfg_device_resources_cnt;
773433e274bSKuo Zhao 	u32 adminq_register_page_list_cnt;
774433e274bSKuo Zhao 	u32 adminq_unregister_page_list_cnt;
775433e274bSKuo Zhao 	u32 adminq_create_tx_queue_cnt;
776433e274bSKuo Zhao 	u32 adminq_create_rx_queue_cnt;
777433e274bSKuo Zhao 	u32 adminq_destroy_tx_queue_cnt;
778433e274bSKuo Zhao 	u32 adminq_destroy_rx_queue_cnt;
779433e274bSKuo Zhao 	u32 adminq_dcfg_device_resources_cnt;
780433e274bSKuo Zhao 	u32 adminq_set_driver_parameter_cnt;
78124aeb56fSKuo Zhao 	u32 adminq_report_stats_cnt;
7827e074d5aSDavid Awogbemila 	u32 adminq_report_link_speed_cnt;
783c4b87ac8SBailey Forrest 	u32 adminq_get_ptype_map_cnt;
784c2a0c3edSJeroen de Borst 	u32 adminq_verify_driver_compatibility_cnt;
78557718b60SJeroen de Borst 	u32 adminq_query_flow_rules_cnt;
78657718b60SJeroen de Borst 	u32 adminq_cfg_flow_rule_cnt;
787433e274bSKuo Zhao 
788433e274bSKuo Zhao 	/* Global stats */
789433e274bSKuo Zhao 	u32 interface_up_cnt; /* count of times interface turned up since last reset */
790433e274bSKuo Zhao 	u32 interface_down_cnt; /* count of times interface turned down since last reset */
791433e274bSKuo Zhao 	u32 reset_cnt; /* count of reset */
792433e274bSKuo Zhao 	u32 page_alloc_fail; /* count of page alloc fails */
793433e274bSKuo Zhao 	u32 dma_mapping_error; /* count of dma mapping errors */
79424aeb56fSKuo Zhao 	u32 stats_report_trigger_cnt; /* count of device-requested stats-reports since last reset */
795974365e5SCatherine Sullivan 	u32 suspend_cnt; /* count of times suspended */
796974365e5SCatherine Sullivan 	u32 resume_cnt; /* count of times resumed */
7979e5f7d26SCatherine Sullivan 	struct workqueue_struct *gve_wq;
7989e5f7d26SCatherine Sullivan 	struct work_struct service_task;
79924aeb56fSKuo Zhao 	struct work_struct stats_report_task;
8009e5f7d26SCatherine Sullivan 	unsigned long service_task_flags;
801893ce44dSCatherine Sullivan 	unsigned long state_flags;
80224aeb56fSKuo Zhao 
80324aeb56fSKuo Zhao 	struct gve_stats_report *stats_report;
80424aeb56fSKuo Zhao 	u64 stats_report_len;
80524aeb56fSKuo Zhao 	dma_addr_t stats_report_bus; /* dma address for the stats report */
80624aeb56fSKuo Zhao 	unsigned long ethtool_flags;
80724aeb56fSKuo Zhao 
80824aeb56fSKuo Zhao 	unsigned long stats_report_timer_period;
80924aeb56fSKuo Zhao 	struct timer_list stats_report_timer;
81024aeb56fSKuo Zhao 
8117e074d5aSDavid Awogbemila 	/* Gvnic device link speed from hypervisor. */
8127e074d5aSDavid Awogbemila 	u64 link_speed;
813974365e5SCatherine Sullivan 	bool up_before_suspend; /* True if dev was up before suspend */
814a5886ef4SBailey Forrest 
815c4b87ac8SBailey Forrest 	struct gve_ptype_lut *ptype_lut_dqo;
8165ca2265eSBailey Forrest 
8171f6228e4SBailey Forrest 	/* Must be a power of two. */
8180b43cf52SJeroen de Borst 	u16 data_buffer_size_dqo;
8190b43cf52SJeroen de Borst 	u16 max_rx_buffer_size; /* device limit */
8201f6228e4SBailey Forrest 
821a5886ef4SBailey Forrest 	enum gve_queue_format queue_format;
8226081ac20STao Liu 
8236081ac20STao Liu 	/* Interrupt coalescing settings */
8246081ac20STao Liu 	u32 tx_coalesce_usecs;
8256081ac20STao Liu 	u32 rx_coalesce_usecs;
8260b43cf52SJeroen de Borst 
8270b43cf52SJeroen de Borst 	u16 header_buf_size; /* device configured, header-split supported if non-zero */
8285e37d825SJeroen de Borst 	bool header_split_enabled; /* True if the header split is enabled by the user */
8293519c005SJeroen de Borst 
8303519c005SJeroen de Borst 	u32 max_flow_rules;
83157718b60SJeroen de Borst 	u32 num_flow_rules;
83257718b60SJeroen de Borst 
83357718b60SJeroen de Borst 	struct gve_flow_rules_cache flow_rules_cache;
834893ce44dSCatherine Sullivan };
835893ce44dSCatherine Sullivan 
83624aeb56fSKuo Zhao enum gve_service_task_flags_bit {
83724aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_DO_RESET			= 1,
83824aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_RESET_IN_PROGRESS	= 2,
83924aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_PROBE_IN_PROGRESS	= 3,
84024aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_DO_REPORT_STATS = 4,
8419e5f7d26SCatherine Sullivan };
8429e5f7d26SCatherine Sullivan 
84324aeb56fSKuo Zhao enum gve_state_flags_bit {
84424aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_ADMIN_QUEUE_OK		= 1,
84524aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK	= 2,
84624aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_DEVICE_RINGS_OK		= 3,
84724aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_NAPI_ENABLED		= 4,
84824aeb56fSKuo Zhao };
84924aeb56fSKuo Zhao 
85024aeb56fSKuo Zhao enum gve_ethtool_flags_bit {
85124aeb56fSKuo Zhao 	GVE_PRIV_FLAGS_REPORT_STATS		= 0,
852893ce44dSCatherine Sullivan };
853893ce44dSCatherine Sullivan 
gve_get_do_reset(struct gve_priv * priv)8549e5f7d26SCatherine Sullivan static inline bool gve_get_do_reset(struct gve_priv *priv)
8559e5f7d26SCatherine Sullivan {
8569e5f7d26SCatherine Sullivan 	return test_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
8579e5f7d26SCatherine Sullivan }
8589e5f7d26SCatherine Sullivan 
gve_set_do_reset(struct gve_priv * priv)8599e5f7d26SCatherine Sullivan static inline void gve_set_do_reset(struct gve_priv *priv)
8609e5f7d26SCatherine Sullivan {
8619e5f7d26SCatherine Sullivan 	set_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
8629e5f7d26SCatherine Sullivan }
8639e5f7d26SCatherine Sullivan 
gve_clear_do_reset(struct gve_priv * priv)8649e5f7d26SCatherine Sullivan static inline void gve_clear_do_reset(struct gve_priv *priv)
8659e5f7d26SCatherine Sullivan {
8669e5f7d26SCatherine Sullivan 	clear_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
8679e5f7d26SCatherine Sullivan }
8689e5f7d26SCatherine Sullivan 
gve_get_reset_in_progress(struct gve_priv * priv)8699e5f7d26SCatherine Sullivan static inline bool gve_get_reset_in_progress(struct gve_priv *priv)
8709e5f7d26SCatherine Sullivan {
8719e5f7d26SCatherine Sullivan 	return test_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS,
8729e5f7d26SCatherine Sullivan 			&priv->service_task_flags);
8739e5f7d26SCatherine Sullivan }
8749e5f7d26SCatherine Sullivan 
gve_set_reset_in_progress(struct gve_priv * priv)8759e5f7d26SCatherine Sullivan static inline void gve_set_reset_in_progress(struct gve_priv *priv)
8769e5f7d26SCatherine Sullivan {
8779e5f7d26SCatherine Sullivan 	set_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
8789e5f7d26SCatherine Sullivan }
8799e5f7d26SCatherine Sullivan 
gve_clear_reset_in_progress(struct gve_priv * priv)8809e5f7d26SCatherine Sullivan static inline void gve_clear_reset_in_progress(struct gve_priv *priv)
8819e5f7d26SCatherine Sullivan {
8829e5f7d26SCatherine Sullivan 	clear_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
8839e5f7d26SCatherine Sullivan }
8849e5f7d26SCatherine Sullivan 
gve_get_probe_in_progress(struct gve_priv * priv)8859e5f7d26SCatherine Sullivan static inline bool gve_get_probe_in_progress(struct gve_priv *priv)
8869e5f7d26SCatherine Sullivan {
8879e5f7d26SCatherine Sullivan 	return test_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS,
8889e5f7d26SCatherine Sullivan 			&priv->service_task_flags);
8899e5f7d26SCatherine Sullivan }
8909e5f7d26SCatherine Sullivan 
gve_set_probe_in_progress(struct gve_priv * priv)8919e5f7d26SCatherine Sullivan static inline void gve_set_probe_in_progress(struct gve_priv *priv)
8929e5f7d26SCatherine Sullivan {
8939e5f7d26SCatherine Sullivan 	set_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
8949e5f7d26SCatherine Sullivan }
8959e5f7d26SCatherine Sullivan 
gve_clear_probe_in_progress(struct gve_priv * priv)8969e5f7d26SCatherine Sullivan static inline void gve_clear_probe_in_progress(struct gve_priv *priv)
8979e5f7d26SCatherine Sullivan {
8989e5f7d26SCatherine Sullivan 	clear_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
8999e5f7d26SCatherine Sullivan }
9009e5f7d26SCatherine Sullivan 
gve_get_do_report_stats(struct gve_priv * priv)90124aeb56fSKuo Zhao static inline bool gve_get_do_report_stats(struct gve_priv *priv)
90224aeb56fSKuo Zhao {
90324aeb56fSKuo Zhao 	return test_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS,
90424aeb56fSKuo Zhao 			&priv->service_task_flags);
90524aeb56fSKuo Zhao }
90624aeb56fSKuo Zhao 
gve_set_do_report_stats(struct gve_priv * priv)90724aeb56fSKuo Zhao static inline void gve_set_do_report_stats(struct gve_priv *priv)
90824aeb56fSKuo Zhao {
90924aeb56fSKuo Zhao 	set_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
91024aeb56fSKuo Zhao }
91124aeb56fSKuo Zhao 
gve_clear_do_report_stats(struct gve_priv * priv)91224aeb56fSKuo Zhao static inline void gve_clear_do_report_stats(struct gve_priv *priv)
91324aeb56fSKuo Zhao {
91424aeb56fSKuo Zhao 	clear_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
91524aeb56fSKuo Zhao }
91624aeb56fSKuo Zhao 
gve_get_admin_queue_ok(struct gve_priv * priv)917893ce44dSCatherine Sullivan static inline bool gve_get_admin_queue_ok(struct gve_priv *priv)
918893ce44dSCatherine Sullivan {
919893ce44dSCatherine Sullivan 	return test_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
920893ce44dSCatherine Sullivan }
921893ce44dSCatherine Sullivan 
gve_set_admin_queue_ok(struct gve_priv * priv)922893ce44dSCatherine Sullivan static inline void gve_set_admin_queue_ok(struct gve_priv *priv)
923893ce44dSCatherine Sullivan {
924893ce44dSCatherine Sullivan 	set_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
925893ce44dSCatherine Sullivan }
926893ce44dSCatherine Sullivan 
gve_clear_admin_queue_ok(struct gve_priv * priv)927893ce44dSCatherine Sullivan static inline void gve_clear_admin_queue_ok(struct gve_priv *priv)
928893ce44dSCatherine Sullivan {
929893ce44dSCatherine Sullivan 	clear_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
930893ce44dSCatherine Sullivan }
931893ce44dSCatherine Sullivan 
gve_get_device_resources_ok(struct gve_priv * priv)932893ce44dSCatherine Sullivan static inline bool gve_get_device_resources_ok(struct gve_priv *priv)
933893ce44dSCatherine Sullivan {
934893ce44dSCatherine Sullivan 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
935893ce44dSCatherine Sullivan }
936893ce44dSCatherine Sullivan 
gve_set_device_resources_ok(struct gve_priv * priv)937893ce44dSCatherine Sullivan static inline void gve_set_device_resources_ok(struct gve_priv *priv)
938893ce44dSCatherine Sullivan {
939893ce44dSCatherine Sullivan 	set_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
940893ce44dSCatherine Sullivan }
941893ce44dSCatherine Sullivan 
gve_clear_device_resources_ok(struct gve_priv * priv)942893ce44dSCatherine Sullivan static inline void gve_clear_device_resources_ok(struct gve_priv *priv)
943893ce44dSCatherine Sullivan {
944893ce44dSCatherine Sullivan 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
945893ce44dSCatherine Sullivan }
946893ce44dSCatherine Sullivan 
gve_get_device_rings_ok(struct gve_priv * priv)947893ce44dSCatherine Sullivan static inline bool gve_get_device_rings_ok(struct gve_priv *priv)
948893ce44dSCatherine Sullivan {
949893ce44dSCatherine Sullivan 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
950893ce44dSCatherine Sullivan }
951893ce44dSCatherine Sullivan 
gve_set_device_rings_ok(struct gve_priv * priv)952893ce44dSCatherine Sullivan static inline void gve_set_device_rings_ok(struct gve_priv *priv)
953893ce44dSCatherine Sullivan {
954893ce44dSCatherine Sullivan 	set_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
955893ce44dSCatherine Sullivan }
956893ce44dSCatherine Sullivan 
gve_clear_device_rings_ok(struct gve_priv * priv)957893ce44dSCatherine Sullivan static inline void gve_clear_device_rings_ok(struct gve_priv *priv)
958893ce44dSCatherine Sullivan {
959893ce44dSCatherine Sullivan 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
960893ce44dSCatherine Sullivan }
961893ce44dSCatherine Sullivan 
gve_get_napi_enabled(struct gve_priv * priv)962893ce44dSCatherine Sullivan static inline bool gve_get_napi_enabled(struct gve_priv *priv)
963893ce44dSCatherine Sullivan {
964893ce44dSCatherine Sullivan 	return test_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
965893ce44dSCatherine Sullivan }
966893ce44dSCatherine Sullivan 
gve_set_napi_enabled(struct gve_priv * priv)967893ce44dSCatherine Sullivan static inline void gve_set_napi_enabled(struct gve_priv *priv)
968893ce44dSCatherine Sullivan {
969893ce44dSCatherine Sullivan 	set_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
970893ce44dSCatherine Sullivan }
971893ce44dSCatherine Sullivan 
gve_clear_napi_enabled(struct gve_priv * priv)972893ce44dSCatherine Sullivan static inline void gve_clear_napi_enabled(struct gve_priv *priv)
973893ce44dSCatherine Sullivan {
974893ce44dSCatherine Sullivan 	clear_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
975893ce44dSCatherine Sullivan }
976893ce44dSCatherine Sullivan 
gve_get_report_stats(struct gve_priv * priv)97724aeb56fSKuo Zhao static inline bool gve_get_report_stats(struct gve_priv *priv)
97824aeb56fSKuo Zhao {
97924aeb56fSKuo Zhao 	return test_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
98024aeb56fSKuo Zhao }
98124aeb56fSKuo Zhao 
gve_clear_report_stats(struct gve_priv * priv)98224aeb56fSKuo Zhao static inline void gve_clear_report_stats(struct gve_priv *priv)
98324aeb56fSKuo Zhao {
98424aeb56fSKuo Zhao 	clear_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
98524aeb56fSKuo Zhao }
98624aeb56fSKuo Zhao 
987893ce44dSCatherine Sullivan /* Returns the address of the ntfy_blocks irq doorbell
988893ce44dSCatherine Sullivan  */
gve_irq_doorbell(struct gve_priv * priv,struct gve_notify_block * block)989893ce44dSCatherine Sullivan static inline __be32 __iomem *gve_irq_doorbell(struct gve_priv *priv,
990893ce44dSCatherine Sullivan 					       struct gve_notify_block *block)
991893ce44dSCatherine Sullivan {
992d30baaccSCatherine Sullivan 	return &priv->db_bar2[be32_to_cpu(*block->irq_db_index)];
993893ce44dSCatherine Sullivan }
994f5cedc84SCatherine Sullivan 
995f5cedc84SCatherine Sullivan /* Returns the index into ntfy_blocks of the given tx ring's block
996f5cedc84SCatherine Sullivan  */
gve_tx_idx_to_ntfy(struct gve_priv * priv,u32 queue_idx)997f5cedc84SCatherine Sullivan static inline u32 gve_tx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
998f5cedc84SCatherine Sullivan {
999f5cedc84SCatherine Sullivan 	return queue_idx;
1000f5cedc84SCatherine Sullivan }
1001f5cedc84SCatherine Sullivan 
1002f5cedc84SCatherine Sullivan /* Returns the index into ntfy_blocks of the given rx ring's block
1003f5cedc84SCatherine Sullivan  */
gve_rx_idx_to_ntfy(struct gve_priv * priv,u32 queue_idx)1004f5cedc84SCatherine Sullivan static inline u32 gve_rx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
1005f5cedc84SCatherine Sullivan {
1006f5cedc84SCatherine Sullivan 	return (priv->num_ntfy_blks / 2) + queue_idx;
1007f5cedc84SCatherine Sullivan }
1008f5cedc84SCatherine Sullivan 
gve_is_qpl(struct gve_priv * priv)100966ce8e6bSRushil Gupta static inline bool gve_is_qpl(struct gve_priv *priv)
101066ce8e6bSRushil Gupta {
101166ce8e6bSRushil Gupta 	return priv->queue_format == GVE_GQI_QPL_FORMAT ||
101266ce8e6bSRushil Gupta 		priv->queue_format == GVE_DQO_QPL_FORMAT;
101366ce8e6bSRushil Gupta }
101466ce8e6bSRushil Gupta 
1015f13697ccSShailend Chand /* Returns the number of tx queue page lists */
gve_num_tx_qpls(const struct gve_queue_config * tx_cfg,int num_xdp_queues,bool is_qpl)1016f13697ccSShailend Chand static inline u32 gve_num_tx_qpls(const struct gve_queue_config *tx_cfg,
1017f13697ccSShailend Chand 				  int num_xdp_queues,
1018f13697ccSShailend Chand 				  bool is_qpl)
1019f5cedc84SCatherine Sullivan {
1020f13697ccSShailend Chand 	if (!is_qpl)
1021a5886ef4SBailey Forrest 		return 0;
1022f13697ccSShailend Chand 	return tx_cfg->num_queues + num_xdp_queues;
102375eaae15SPraveen Kaligineedi }
102475eaae15SPraveen Kaligineedi 
102575eaae15SPraveen Kaligineedi /* Returns the number of XDP tx queue page lists
102675eaae15SPraveen Kaligineedi  */
gve_num_xdp_qpls(struct gve_priv * priv)102775eaae15SPraveen Kaligineedi static inline u32 gve_num_xdp_qpls(struct gve_priv *priv)
102875eaae15SPraveen Kaligineedi {
102975eaae15SPraveen Kaligineedi 	if (priv->queue_format != GVE_GQI_QPL_FORMAT)
103075eaae15SPraveen Kaligineedi 		return 0;
103175eaae15SPraveen Kaligineedi 
103275eaae15SPraveen Kaligineedi 	return priv->num_xdp_queues;
1033f5cedc84SCatherine Sullivan }
1034f5cedc84SCatherine Sullivan 
1035f13697ccSShailend Chand /* Returns the number of rx queue page lists */
gve_num_rx_qpls(const struct gve_queue_config * rx_cfg,bool is_qpl)1036f13697ccSShailend Chand static inline u32 gve_num_rx_qpls(const struct gve_queue_config *rx_cfg,
1037f13697ccSShailend Chand 				  bool is_qpl)
1038f5cedc84SCatherine Sullivan {
1039f13697ccSShailend Chand 	if (!is_qpl)
1040a5886ef4SBailey Forrest 		return 0;
1041f13697ccSShailend Chand 	return rx_cfg->num_queues;
1042f5cedc84SCatherine Sullivan }
1043f5cedc84SCatherine Sullivan 
gve_tx_qpl_id(struct gve_priv * priv,int tx_qid)10447fc2bf78SPraveen Kaligineedi static inline u32 gve_tx_qpl_id(struct gve_priv *priv, int tx_qid)
10457fc2bf78SPraveen Kaligineedi {
10467fc2bf78SPraveen Kaligineedi 	return tx_qid;
10477fc2bf78SPraveen Kaligineedi }
10487fc2bf78SPraveen Kaligineedi 
gve_rx_qpl_id(struct gve_priv * priv,int rx_qid)10497fc2bf78SPraveen Kaligineedi static inline u32 gve_rx_qpl_id(struct gve_priv *priv, int rx_qid)
10507fc2bf78SPraveen Kaligineedi {
10517fc2bf78SPraveen Kaligineedi 	return priv->tx_cfg.max_queues + rx_qid;
10527fc2bf78SPraveen Kaligineedi }
10537fc2bf78SPraveen Kaligineedi 
gve_get_rx_qpl_id(const struct gve_queue_config * tx_cfg,int rx_qid)1054f13697ccSShailend Chand static inline u32 gve_get_rx_qpl_id(const struct gve_queue_config *tx_cfg, int rx_qid)
1055f13697ccSShailend Chand {
1056f13697ccSShailend Chand 	return tx_cfg->max_queues + rx_qid;
1057f13697ccSShailend Chand }
1058f13697ccSShailend Chand 
gve_tx_start_qpl_id(struct gve_priv * priv)10597fc2bf78SPraveen Kaligineedi static inline u32 gve_tx_start_qpl_id(struct gve_priv *priv)
10607fc2bf78SPraveen Kaligineedi {
10617fc2bf78SPraveen Kaligineedi 	return gve_tx_qpl_id(priv, 0);
10627fc2bf78SPraveen Kaligineedi }
10637fc2bf78SPraveen Kaligineedi 
gve_rx_start_qpl_id(const struct gve_queue_config * tx_cfg)1064f13697ccSShailend Chand static inline u32 gve_rx_start_qpl_id(const struct gve_queue_config *tx_cfg)
10657fc2bf78SPraveen Kaligineedi {
1066f13697ccSShailend Chand 	return gve_get_rx_qpl_id(tx_cfg, 0);
10677fc2bf78SPraveen Kaligineedi }
10687fc2bf78SPraveen Kaligineedi 
gve_get_rx_pages_per_qpl_dqo(u32 rx_desc_cnt)1069b94d3703SHarshitha Ramamurthy static inline u32 gve_get_rx_pages_per_qpl_dqo(u32 rx_desc_cnt)
1070b94d3703SHarshitha Ramamurthy {
1071b94d3703SHarshitha Ramamurthy 	/* For DQO, page count should be more than ring size for
1072b94d3703SHarshitha Ramamurthy 	 * out-of-order completions. Set it to two times of ring size.
1073b94d3703SHarshitha Ramamurthy 	 */
1074b94d3703SHarshitha Ramamurthy 	return 2 * rx_desc_cnt;
1075b94d3703SHarshitha Ramamurthy }
1076b94d3703SHarshitha Ramamurthy 
1077f13697ccSShailend Chand /* Returns the correct dma direction for tx and rx qpls */
gve_qpl_dma_dir(struct gve_priv * priv,int id)1078f5cedc84SCatherine Sullivan static inline enum dma_data_direction gve_qpl_dma_dir(struct gve_priv *priv,
1079f5cedc84SCatherine Sullivan 						      int id)
1080f5cedc84SCatherine Sullivan {
1081f13697ccSShailend Chand 	if (id < gve_rx_start_qpl_id(&priv->tx_cfg))
1082f5cedc84SCatherine Sullivan 		return DMA_TO_DEVICE;
1083f5cedc84SCatherine Sullivan 	else
1084f5cedc84SCatherine Sullivan 		return DMA_FROM_DEVICE;
1085f5cedc84SCatherine Sullivan }
1086f5cedc84SCatherine Sullivan 
gve_is_gqi(struct gve_priv * priv)10875ca2265eSBailey Forrest static inline bool gve_is_gqi(struct gve_priv *priv)
10885ca2265eSBailey Forrest {
10895ca2265eSBailey Forrest 	return priv->queue_format == GVE_GQI_RDA_FORMAT ||
10905ca2265eSBailey Forrest 		priv->queue_format == GVE_GQI_QPL_FORMAT;
10915ca2265eSBailey Forrest }
10925ca2265eSBailey Forrest 
gve_num_tx_queues(struct gve_priv * priv)10932e80aeaeSPraveen Kaligineedi static inline u32 gve_num_tx_queues(struct gve_priv *priv)
10942e80aeaeSPraveen Kaligineedi {
109575eaae15SPraveen Kaligineedi 	return priv->tx_cfg.num_queues + priv->num_xdp_queues;
109675eaae15SPraveen Kaligineedi }
109775eaae15SPraveen Kaligineedi 
gve_xdp_tx_queue_id(struct gve_priv * priv,u32 queue_id)109875eaae15SPraveen Kaligineedi static inline u32 gve_xdp_tx_queue_id(struct gve_priv *priv, u32 queue_id)
109975eaae15SPraveen Kaligineedi {
110075eaae15SPraveen Kaligineedi 	return priv->tx_cfg.num_queues + queue_id;
110175eaae15SPraveen Kaligineedi }
110275eaae15SPraveen Kaligineedi 
gve_xdp_tx_start_queue_id(struct gve_priv * priv)110375eaae15SPraveen Kaligineedi static inline u32 gve_xdp_tx_start_queue_id(struct gve_priv *priv)
110475eaae15SPraveen Kaligineedi {
110575eaae15SPraveen Kaligineedi 	return gve_xdp_tx_queue_id(priv, 0);
11062e80aeaeSPraveen Kaligineedi }
11072e80aeaeSPraveen Kaligineedi 
11081dfc2e46SShailend Chand /* gqi napi handler defined in gve_main.c */
11091dfc2e46SShailend Chand int gve_napi_poll(struct napi_struct *napi, int budget);
11101dfc2e46SShailend Chand 
1111f5cedc84SCatherine Sullivan /* buffers */
1112433e274bSKuo Zhao int gve_alloc_page(struct gve_priv *priv, struct device *dev,
1113433e274bSKuo Zhao 		   struct page **page, dma_addr_t *dma,
1114a92f7a6fSCatherine Sullivan 		   enum dma_data_direction, gfp_t gfp_flags);
1115f5cedc84SCatherine Sullivan void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
1116f5cedc84SCatherine Sullivan 		   enum dma_data_direction);
1117ee24284eSShailend Chand /* qpls */
1118ee24284eSShailend Chand struct gve_queue_page_list *gve_alloc_queue_page_list(struct gve_priv *priv,
1119ee24284eSShailend Chand 						      u32 id, int pages);
1120ee24284eSShailend Chand void gve_free_queue_page_list(struct gve_priv *priv,
1121ee24284eSShailend Chand 			      struct gve_queue_page_list *qpl,
1122ee24284eSShailend Chand 			      u32 id);
1123f5cedc84SCatherine Sullivan /* tx handling */
1124f5cedc84SCatherine Sullivan netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev);
112539a7f4aaSPraveen Kaligineedi int gve_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
112639a7f4aaSPraveen Kaligineedi 		 u32 flags);
112775eaae15SPraveen Kaligineedi int gve_xdp_xmit_one(struct gve_priv *priv, struct gve_tx_ring *tx,
112839a7f4aaSPraveen Kaligineedi 		     void *data, int len, void *frame_p);
112975eaae15SPraveen Kaligineedi void gve_xdp_tx_flush(struct gve_priv *priv, u32 xdp_qid);
1130f5cedc84SCatherine Sullivan bool gve_tx_poll(struct gve_notify_block *block, int budget);
113175eaae15SPraveen Kaligineedi bool gve_xdp_poll(struct gve_notify_block *block, int budget);
1132f13697ccSShailend Chand int gve_tx_alloc_rings_gqi(struct gve_priv *priv,
1133f13697ccSShailend Chand 			   struct gve_tx_alloc_rings_cfg *cfg);
1134f13697ccSShailend Chand void gve_tx_free_rings_gqi(struct gve_priv *priv,
1135f13697ccSShailend Chand 			   struct gve_tx_alloc_rings_cfg *cfg);
1136f13697ccSShailend Chand void gve_tx_start_ring_gqi(struct gve_priv *priv, int idx);
1137f13697ccSShailend Chand void gve_tx_stop_ring_gqi(struct gve_priv *priv, int idx);
113861d72c7eSTao Liu u32 gve_tx_load_event_counter(struct gve_priv *priv,
1139f5cedc84SCatherine Sullivan 			      struct gve_tx_ring *tx);
114061d72c7eSTao Liu bool gve_tx_clean_pending(struct gve_priv *priv, struct gve_tx_ring *tx);
1141f5cedc84SCatherine Sullivan /* rx handling */
1142f5cedc84SCatherine Sullivan void gve_rx_write_doorbell(struct gve_priv *priv, struct gve_rx_ring *rx);
11432cb67ab1SYangchun Fu int gve_rx_poll(struct gve_notify_block *block, int budget);
11442cb67ab1SYangchun Fu bool gve_rx_work_pending(struct gve_rx_ring *rx);
1145c93462b9SShailend Chand int gve_rx_alloc_ring_gqi(struct gve_priv *priv,
1146c93462b9SShailend Chand 			  struct gve_rx_alloc_rings_cfg *cfg,
1147c93462b9SShailend Chand 			  struct gve_rx_ring *rx,
1148c93462b9SShailend Chand 			  int idx);
1149c93462b9SShailend Chand void gve_rx_free_ring_gqi(struct gve_priv *priv, struct gve_rx_ring *rx,
1150c93462b9SShailend Chand 			  struct gve_rx_alloc_rings_cfg *cfg);
1151f5cedc84SCatherine Sullivan int gve_rx_alloc_rings(struct gve_priv *priv);
1152f13697ccSShailend Chand int gve_rx_alloc_rings_gqi(struct gve_priv *priv,
1153f13697ccSShailend Chand 			   struct gve_rx_alloc_rings_cfg *cfg);
1154f13697ccSShailend Chand void gve_rx_free_rings_gqi(struct gve_priv *priv,
1155f13697ccSShailend Chand 			   struct gve_rx_alloc_rings_cfg *cfg);
1156f13697ccSShailend Chand void gve_rx_start_ring_gqi(struct gve_priv *priv, int idx);
1157f13697ccSShailend Chand void gve_rx_stop_ring_gqi(struct gve_priv *priv, int idx);
11585e37d825SJeroen de Borst u16 gve_get_pkt_buf_size(const struct gve_priv *priv, bool enable_hplit);
11595e37d825SJeroen de Borst bool gve_header_split_supported(const struct gve_priv *priv);
11605e37d825SJeroen de Borst int gve_set_hsplit_config(struct gve_priv *priv, u8 tcp_data_split);
11619e5f7d26SCatherine Sullivan /* Reset */
11629e5f7d26SCatherine Sullivan void gve_schedule_reset(struct gve_priv *priv);
11639e5f7d26SCatherine Sullivan int gve_reset(struct gve_priv *priv, bool attempt_teardown);
1164834f9458SHarshitha Ramamurthy void gve_get_curr_alloc_cfgs(struct gve_priv *priv,
1165834f9458SHarshitha Ramamurthy 			     struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1166834f9458SHarshitha Ramamurthy 			     struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1167834f9458SHarshitha Ramamurthy int gve_adjust_config(struct gve_priv *priv,
1168834f9458SHarshitha Ramamurthy 		      struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1169834f9458SHarshitha Ramamurthy 		      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
11709e5f7d26SCatherine Sullivan int gve_adjust_queues(struct gve_priv *priv,
11719e5f7d26SCatherine Sullivan 		      struct gve_queue_config new_rx_config,
11729e5f7d26SCatherine Sullivan 		      struct gve_queue_config new_tx_config);
1173*6f3bc487SJeroen de Borst /* flow steering rule */
1174*6f3bc487SJeroen de Borst int gve_get_flow_rule_entry(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1175*6f3bc487SJeroen de Borst int gve_get_flow_rule_ids(struct gve_priv *priv, struct ethtool_rxnfc *cmd, u32 *rule_locs);
1176*6f3bc487SJeroen de Borst int gve_add_flow_rule(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1177*6f3bc487SJeroen de Borst int gve_del_flow_rule(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1178*6f3bc487SJeroen de Borst int gve_flow_rules_reset(struct gve_priv *priv);
117924aeb56fSKuo Zhao /* report stats handling */
118024aeb56fSKuo Zhao void gve_handle_report_stats(struct gve_priv *priv);
1181e5b845dcSCatherine Sullivan /* exported by ethtool.c */
1182e5b845dcSCatherine Sullivan extern const struct ethtool_ops gve_ethtool_ops;
1183e5b845dcSCatherine Sullivan /* needed by ethtool */
11849d0aba98SJunfeng Guo extern char gve_driver_name[];
1185e5b845dcSCatherine Sullivan extern const char gve_version_str[];
1186893ce44dSCatherine Sullivan #endif /* _GVE_H_ */
1187