xref: /linux/drivers/net/ethernet/google/gve/gve.h (revision bbe522b0f237f7e25b68c57361b91953a563b733)
1 /* SPDX-License-Identifier: (GPL-2.0 OR MIT)
2  * Google virtual Ethernet (gve) driver
3  *
4  * Copyright (C) 2015-2024 Google LLC
5  */
6 
7 #ifndef _GVE_H_
8 #define _GVE_H_
9 
10 #include <linux/dma-mapping.h>
11 #include <linux/dmapool.h>
12 #include <linux/ethtool_netlink.h>
13 #include <linux/netdevice.h>
14 #include <linux/net_tstamp.h>
15 #include <linux/pci.h>
16 #include <linux/timer.h>
17 #include <linux/ptp_clock_kernel.h>
18 #include <linux/u64_stats_sync.h>
19 #include <net/page_pool/helpers.h>
20 #include <net/xdp.h>
21 
22 #include "gve_desc.h"
23 #include "gve_desc_dqo.h"
24 
25 #ifndef PCI_VENDOR_ID_GOOGLE
26 #define PCI_VENDOR_ID_GOOGLE	0x1ae0
27 #endif
28 
29 #define PCI_DEV_ID_GVNIC	0x0042
30 
31 #define GVE_REGISTER_BAR	0
32 #define GVE_DOORBELL_BAR	2
33 
34 /* Driver can alloc up to 2 segments for the header and 2 for the payload. */
35 #define GVE_TX_MAX_IOVEC	4
36 /* 1 for management, 1 for rx, 1 for tx */
37 #define GVE_MIN_MSIX 3
38 
39 /* Numbers of gve tx/rx stats in stats report. */
40 #define GVE_TX_STATS_REPORT_NUM	6
41 #define GVE_RX_STATS_REPORT_NUM	2
42 
43 /* Interval to schedule a stats report update, 20000ms. */
44 #define GVE_STATS_REPORT_TIMER_PERIOD	20000
45 #define GVE_RX_NAPI_RESCHED_MS 20 /* msecs */
46 
47 /* Numbers of NIC tx/rx stats in stats report. */
48 #define NIC_TX_STATS_REPORT_NUM	0
49 #define NIC_RX_STATS_REPORT_NUM	4
50 
51 #define GVE_ADMINQ_BUFFER_SIZE 4096
52 
53 #define GVE_DATA_SLOT_ADDR_PAGE_MASK (~(PAGE_SIZE - 1))
54 
55 /* PTYPEs are always 10 bits. */
56 #define GVE_NUM_PTYPES	1024
57 
58 /* Default minimum ring size */
59 #define GVE_DEFAULT_MIN_TX_RING_SIZE 256
60 #define GVE_DEFAULT_MIN_RX_RING_SIZE 512
61 
62 #define GVE_DEFAULT_RX_BUFFER_SIZE 2048
63 
64 #define GVE_XDP_RX_BUFFER_SIZE_DQO 4096
65 
66 #define GVE_DEFAULT_RX_BUFFER_OFFSET 2048
67 
68 #define GVE_PAGE_POOL_SIZE_MULTIPLIER 4
69 
70 #define GVE_FLOW_RULES_CACHE_SIZE \
71 	(GVE_ADMINQ_BUFFER_SIZE / sizeof(struct gve_adminq_queried_flow_rule))
72 #define GVE_FLOW_RULE_IDS_CACHE_SIZE \
73 	(GVE_ADMINQ_BUFFER_SIZE / sizeof(((struct gve_adminq_queried_flow_rule *)0)->location))
74 
75 #define GVE_RSS_KEY_SIZE	40
76 #define GVE_RSS_INDIR_SIZE	128
77 
78 #define GVE_XDP_ACTIONS 5
79 
80 #define GVE_GQ_TX_MIN_PKT_DESC_BYTES 182
81 
82 #define GVE_DEFAULT_HEADER_BUFFER_SIZE 128
83 
84 /* Maximum TSO size supported on DQO */
85 #define GVE_DQO_TX_MAX	0x3FFFF
86 
87 #define GVE_TX_BUF_SHIFT_DQO 11
88 
89 /* 2K buffers for DQO-QPL */
90 #define GVE_TX_BUF_SIZE_DQO BIT(GVE_TX_BUF_SHIFT_DQO)
91 #define GVE_TX_BUFS_PER_PAGE_DQO (PAGE_SIZE >> GVE_TX_BUF_SHIFT_DQO)
92 #define GVE_MAX_TX_BUFS_PER_PKT (DIV_ROUND_UP(GVE_DQO_TX_MAX, GVE_TX_BUF_SIZE_DQO))
93 
94 /* If number of free/recyclable buffers are less than this threshold; driver
95  * allocs and uses a non-qpl page on the receive path of DQO QPL to free
96  * up buffers.
97  * Value is set big enough to post at least 3 64K LRO packet via 2K buffer to NIC.
98  */
99 #define GVE_DQO_QPL_ONDEMAND_ALLOC_THRESHOLD 96
100 
101 #define GVE_DQO_RX_HWTSTAMP_VALID 0x1
102 
103 /* Each slot in the desc ring has a 1:1 mapping to a slot in the data ring */
104 struct gve_rx_desc_queue {
105 	struct gve_rx_desc *desc_ring; /* the descriptor ring */
106 	dma_addr_t bus; /* the bus for the desc_ring */
107 	u8 seqno; /* the next expected seqno for this desc*/
108 };
109 
110 /* The page info for a single slot in the RX data queue */
111 struct gve_rx_slot_page_info {
112 	/* netmem is used for DQO RDA mode
113 	 * page is used in all other modes
114 	 */
115 	union {
116 		struct page *page;
117 		netmem_ref netmem;
118 	};
119 	void *page_address;
120 	u32 page_offset; /* offset to write to in page */
121 	unsigned int buf_size;
122 	int pagecnt_bias; /* expected pagecnt if only the driver has a ref */
123 	u16 pad; /* adjustment for rx padding */
124 	u8 can_flip; /* tracks if the networking stack is using the page */
125 };
126 
127 /* A list of pages registered with the device during setup and used by a queue
128  * as buffers
129  */
130 struct gve_queue_page_list {
131 	u32 id; /* unique id */
132 	u32 num_entries;
133 	struct page **pages; /* list of num_entries pages */
134 	dma_addr_t *page_buses; /* the dma addrs of the pages */
135 };
136 
137 /* Each slot in the data ring has a 1:1 mapping to a slot in the desc ring */
138 struct gve_rx_data_queue {
139 	union gve_rx_data_slot *data_ring; /* read by NIC */
140 	dma_addr_t data_bus; /* dma mapping of the slots */
141 	struct gve_rx_slot_page_info *page_info; /* page info of the buffers */
142 	struct gve_queue_page_list *qpl; /* qpl assigned to this queue */
143 	u8 raw_addressing; /* use raw_addressing? */
144 };
145 
146 struct gve_priv;
147 
148 /* RX buffer queue for posting buffers to HW.
149  * Each RX (completion) queue has a corresponding buffer queue.
150  */
151 struct gve_rx_buf_queue_dqo {
152 	struct gve_rx_desc_dqo *desc_ring;
153 	dma_addr_t bus;
154 	u32 head; /* Pointer to start cleaning buffers at. */
155 	u32 tail; /* Last posted buffer index + 1 */
156 	u32 mask; /* Mask for indices to the size of the ring */
157 };
158 
159 /* RX completion queue to receive packets from HW. */
160 struct gve_rx_compl_queue_dqo {
161 	struct gve_rx_compl_desc_dqo *desc_ring;
162 	dma_addr_t bus;
163 
164 	/* Number of slots which did not have a buffer posted yet. We should not
165 	 * post more buffers than the queue size to avoid HW overrunning the
166 	 * queue.
167 	 */
168 	int num_free_slots;
169 
170 	/* HW uses a "generation bit" to notify SW of new descriptors. When a
171 	 * descriptor's generation bit is different from the current generation,
172 	 * that descriptor is ready to be consumed by SW.
173 	 */
174 	u8 cur_gen_bit;
175 
176 	/* Pointer into desc_ring where the next completion descriptor will be
177 	 * received.
178 	 */
179 	u32 head;
180 	u32 mask; /* Mask for indices to the size of the ring */
181 };
182 
183 struct gve_header_buf {
184 	u8 *data;
185 	dma_addr_t addr;
186 };
187 
188 /* Stores state for tracking buffers posted to HW */
189 struct gve_rx_buf_state_dqo {
190 	/* The page posted to HW. */
191 	struct gve_rx_slot_page_info page_info;
192 
193 	/* XSK buffer */
194 	struct xdp_buff *xsk_buff;
195 
196 	/* The DMA address corresponding to `page_info`. */
197 	dma_addr_t addr;
198 
199 	/* Last offset into the page when it only had a single reference, at
200 	 * which point every other offset is free to be reused.
201 	 */
202 	u32 last_single_ref_offset;
203 
204 	/* Linked list index to next element in the list, or -1 if none */
205 	s16 next;
206 };
207 
208 /* Wrapper for XDP Rx metadata */
209 struct gve_xdp_buff {
210 	struct xdp_buff xdp;
211 	struct gve_priv *gve;
212 	const struct gve_rx_compl_desc_dqo *compl_desc;
213 };
214 
215 /* `head` and `tail` are indices into an array, or -1 if empty. */
216 struct gve_index_list {
217 	s16 head;
218 	s16 tail;
219 };
220 
221 /* A single received packet split across multiple buffers may be
222  * reconstructed using the information in this structure.
223  */
224 struct gve_rx_ctx {
225 	/* head and tail of skb chain for the current packet or NULL if none */
226 	struct sk_buff *skb_head;
227 	struct sk_buff *skb_tail;
228 	u32 total_size;
229 	u8 frag_cnt;
230 	bool drop_pkt;
231 };
232 
233 struct gve_rx_cnts {
234 	u32 ok_pkt_bytes;
235 	u16 ok_pkt_cnt;
236 	u16 total_pkt_cnt;
237 	u16 cont_pkt_cnt;
238 	u16 desc_err_pkt_cnt;
239 };
240 
241 /* Contains datapath state used to represent an RX queue. */
242 struct gve_rx_ring {
243 	struct gve_priv *gve;
244 
245 	u16 packet_buffer_size;		/* Size of buffer posted to NIC */
246 	u16 packet_buffer_truesize;	/* Total size of RX buffer */
247 	u16 rx_headroom;
248 
249 	union {
250 		/* GQI fields */
251 		struct {
252 			struct gve_rx_desc_queue desc;
253 			struct gve_rx_data_queue data;
254 
255 			/* threshold for posting new buffs and descs */
256 			u32 db_threshold;
257 
258 			u32 qpl_copy_pool_mask;
259 			u32 qpl_copy_pool_head;
260 			struct gve_rx_slot_page_info *qpl_copy_pool;
261 		};
262 
263 		/* DQO fields. */
264 		struct {
265 			struct gve_rx_buf_queue_dqo bufq;
266 			struct gve_rx_compl_queue_dqo complq;
267 
268 			struct gve_rx_buf_state_dqo *buf_states;
269 			u16 num_buf_states;
270 
271 			/* Linked list of gve_rx_buf_state_dqo. Index into
272 			 * buf_states, or -1 if empty.
273 			 */
274 			s16 free_buf_states;
275 
276 			/* Linked list of gve_rx_buf_state_dqo. Indexes into
277 			 * buf_states, or -1 if empty.
278 			 *
279 			 * This list contains buf_states which are pointing to
280 			 * valid buffers.
281 			 *
282 			 * We use a FIFO here in order to increase the
283 			 * probability that buffers can be reused by increasing
284 			 * the time between usages.
285 			 */
286 			struct gve_index_list recycled_buf_states;
287 
288 			/* Linked list of gve_rx_buf_state_dqo. Indexes into
289 			 * buf_states, or -1 if empty.
290 			 *
291 			 * This list contains buf_states which have buffers
292 			 * which cannot be reused yet.
293 			 */
294 			struct gve_index_list used_buf_states;
295 
296 			/* qpl assigned to this queue */
297 			struct gve_queue_page_list *qpl;
298 
299 			/* index into queue page list */
300 			u32 next_qpl_page_idx;
301 
302 			/* track number of used buffers */
303 			u16 used_buf_states_cnt;
304 
305 			/* Address info of the buffers for header-split */
306 			struct gve_header_buf hdr_bufs;
307 
308 			struct page_pool *page_pool;
309 		} dqo;
310 	};
311 
312 	u64 rbytes; /* free-running bytes received */
313 	u64 rx_hsplit_bytes; /* free-running header bytes received */
314 	u64 rpackets; /* free-running packets received */
315 	u32 cnt; /* free-running total number of completed packets */
316 	u32 fill_cnt; /* free-running total number of descs and buffs posted */
317 	u32 mask; /* masks the cnt and fill_cnt to the size of the ring */
318 	u64 rx_hsplit_pkt; /* free-running packets with headers split */
319 	u64 rx_copybreak_pkt; /* free-running count of copybreak packets */
320 	u64 rx_copied_pkt; /* free-running total number of copied packets */
321 	u64 rx_skb_alloc_fail; /* free-running count of skb alloc fails */
322 	u64 rx_buf_alloc_fail; /* free-running count of buffer alloc fails */
323 	u64 rx_desc_err_dropped_pkt; /* free-running count of packets dropped by descriptor error */
324 	/* free-running count of unsplit packets due to header buffer overflow or hdr_len is 0 */
325 	u64 rx_hsplit_unsplit_pkt;
326 	u64 rx_cont_packet_cnt; /* free-running multi-fragment packets received */
327 	u64 rx_frag_flip_cnt; /* free-running count of rx segments where page_flip was used */
328 	u64 rx_frag_copy_cnt; /* free-running count of rx segments copied */
329 	u64 rx_frag_alloc_cnt; /* free-running count of rx page allocations */
330 	u64 xdp_tx_errors;
331 	u64 xdp_redirect_errors;
332 	u64 xdp_alloc_fails;
333 	u64 xdp_actions[GVE_XDP_ACTIONS];
334 	u32 q_num; /* queue index */
335 	u32 ntfy_id; /* notification block index */
336 	struct gve_queue_resources *q_resources; /* head and tail pointer idx */
337 	dma_addr_t q_resources_bus; /* dma address for the queue resources */
338 	struct u64_stats_sync statss; /* sync stats for 32bit archs */
339 
340 	struct gve_rx_ctx ctx; /* Info for packet currently being processed in this ring. */
341 
342 	/* XDP stuff */
343 	struct xdp_rxq_info xdp_rxq;
344 	struct xsk_buff_pool *xsk_pool;
345 	struct page_frag_cache page_cache; /* Page cache to allocate XDP frames */
346 	struct timer_list starvation_timer; /* for queue starvation recovery */
347 };
348 
349 /* A TX desc ring entry */
350 union gve_tx_desc {
351 	struct gve_tx_pkt_desc pkt; /* first desc for a packet */
352 	struct gve_tx_mtd_desc mtd; /* optional metadata descriptor */
353 	struct gve_tx_seg_desc seg; /* subsequent descs for a packet */
354 };
355 
356 /* Tracks the memory in the fifo occupied by a segment of a packet */
357 struct gve_tx_iovec {
358 	u32 iov_offset; /* offset into this segment */
359 	u32 iov_len; /* length */
360 	u32 iov_padding; /* padding associated with this segment */
361 };
362 
363 /* Tracks the memory in the fifo occupied by the skb. Mapped 1:1 to a desc
364  * ring entry but only used for a pkt_desc not a seg_desc
365  */
366 struct gve_tx_buffer_state {
367 	union {
368 		struct sk_buff *skb; /* skb for this pkt */
369 		struct xdp_frame *xdp_frame; /* xdp_frame */
370 	};
371 	struct {
372 		u16 size; /* size of xmitted xdp pkt */
373 		u8 is_xsk; /* xsk buff */
374 	} xdp;
375 	union {
376 		struct gve_tx_iovec iov[GVE_TX_MAX_IOVEC]; /* segments of this pkt */
377 		struct {
378 			DEFINE_DMA_UNMAP_ADDR(dma);
379 			DEFINE_DMA_UNMAP_LEN(len);
380 		};
381 	};
382 };
383 
384 /* A TX buffer - each queue has one */
385 struct gve_tx_fifo {
386 	void *base; /* address of base of FIFO */
387 	u32 size; /* total size */
388 	atomic_t available; /* how much space is still available */
389 	u32 head; /* offset to write at */
390 	struct gve_queue_page_list *qpl; /* QPL mapped into this FIFO */
391 };
392 
393 /* TX descriptor for DQO format */
394 union gve_tx_desc_dqo {
395 	struct gve_tx_pkt_desc_dqo pkt;
396 	struct gve_tx_tso_context_desc_dqo tso_ctx;
397 	struct gve_tx_general_context_desc_dqo general_ctx;
398 };
399 
400 enum gve_packet_state {
401 	/* Packet is in free list, available to be allocated.
402 	 * This should always be zero since state is not explicitly initialized.
403 	 */
404 	GVE_PACKET_STATE_UNALLOCATED,
405 	/* Packet is expecting a regular data completion or miss completion */
406 	GVE_PACKET_STATE_PENDING_DATA_COMPL,
407 	/* Packet has received a miss completion and is expecting a
408 	 * re-injection completion.
409 	 */
410 	GVE_PACKET_STATE_PENDING_REINJECT_COMPL,
411 	/* No valid completion received within the specified timeout. */
412 	GVE_PACKET_STATE_TIMED_OUT_COMPL,
413 	/* XSK pending packet has received a packet/reinjection completion, or
414 	 * has timed out. At this point, the pending packet can be counted by
415 	 * xsk_tx_complete and freed.
416 	 */
417 	GVE_PACKET_STATE_XSK_COMPLETE,
418 };
419 
420 enum gve_tx_pending_packet_dqo_type {
421 	GVE_TX_PENDING_PACKET_DQO_SKB,
422 	GVE_TX_PENDING_PACKET_DQO_XDP_FRAME,
423 	GVE_TX_PENDING_PACKET_DQO_XSK,
424 };
425 
426 struct gve_tx_pending_packet_dqo {
427 	union {
428 		struct sk_buff *skb;
429 		struct xdp_frame *xdpf;
430 	};
431 
432 	/* 0th element corresponds to the linear portion of `skb`, should be
433 	 * unmapped with `dma_unmap_single`.
434 	 *
435 	 * All others correspond to `skb`'s frags and should be unmapped with
436 	 * `dma_unmap_page`.
437 	 */
438 	union {
439 		struct {
440 			DEFINE_DMA_UNMAP_ADDR(dma[MAX_SKB_FRAGS + 1]);
441 			DEFINE_DMA_UNMAP_LEN(len[MAX_SKB_FRAGS + 1]);
442 		};
443 		s16 tx_qpl_buf_ids[GVE_MAX_TX_BUFS_PER_PKT];
444 	};
445 
446 	u16 num_bufs;
447 
448 	/* Linked list index to next element in the list, or -1 if none */
449 	s16 next;
450 
451 	/* Linked list index to prev element in the list, or -1 if none.
452 	 * Used for tracking either outstanding miss completions or prematurely
453 	 * freed packets.
454 	 */
455 	s16 prev;
456 
457 	/* Identifies the current state of the packet as defined in
458 	 * `enum gve_packet_state`.
459 	 */
460 	u8 state : 3;
461 
462 	/* gve_tx_pending_packet_dqo_type */
463 	u8 type : 2;
464 
465 	/* If packet is an outstanding miss completion, then the packet is
466 	 * freed if the corresponding re-injection completion is not received
467 	 * before kernel jiffies exceeds timeout_jiffies.
468 	 */
469 	unsigned long timeout_jiffies;
470 };
471 
472 /* Contains datapath state used to represent a TX queue. */
473 struct gve_tx_ring {
474 	/* Cacheline 0 -- Accessed & dirtied during transmit */
475 	union {
476 		/* GQI fields */
477 		struct {
478 			struct gve_tx_fifo tx_fifo;
479 			u32 req; /* driver tracked head pointer */
480 			u32 done; /* driver tracked tail pointer */
481 		};
482 
483 		/* DQO fields. */
484 		struct {
485 			/* Spinlock for XDP tx traffic */
486 			spinlock_t xdp_lock;
487 
488 			/* Linked list of gve_tx_pending_packet_dqo. Index into
489 			 * pending_packets, or -1 if empty.
490 			 *
491 			 * This is a consumer list owned by the TX path. When it
492 			 * runs out, the producer list is stolen from the
493 			 * completion handling path
494 			 * (dqo_compl.free_pending_packets).
495 			 */
496 			s16 free_pending_packets;
497 
498 			/* Cached value of `dqo_compl.hw_tx_head` */
499 			u32 head;
500 			u32 tail; /* Last posted buffer index + 1 */
501 
502 			/* Index of the last descriptor with "report event" bit
503 			 * set.
504 			 */
505 			u32 last_re_idx;
506 
507 			/* free running number of packet buf descriptors posted */
508 			u16 posted_packet_desc_cnt;
509 			/* free running number of packet buf descriptors completed */
510 			u16 completed_packet_desc_cnt;
511 
512 			/* QPL fields */
513 			struct {
514 			       /* Linked list of gve_tx_buf_dqo. Index into
515 				* tx_qpl_buf_next, or -1 if empty.
516 				*
517 				* This is a consumer list owned by the TX path. When it
518 				* runs out, the producer list is stolen from the
519 				* completion handling path
520 				* (dqo_compl.free_tx_qpl_buf_head).
521 				*/
522 				s16 free_tx_qpl_buf_head;
523 
524 			       /* Free running count of the number of QPL tx buffers
525 				* allocated
526 				*/
527 				u32 alloc_tx_qpl_buf_cnt;
528 
529 				/* Cached value of `dqo_compl.free_tx_qpl_buf_cnt` */
530 				u32 free_tx_qpl_buf_cnt;
531 			};
532 
533 			atomic_t xsk_reorder_queue_tail;
534 		} dqo_tx;
535 	};
536 
537 	/* Cacheline 1 -- Accessed & dirtied during gve_clean_tx_done */
538 	union {
539 		/* GQI fields */
540 		struct {
541 			/* Spinlock for when cleanup in progress */
542 			spinlock_t clean_lock;
543 			/* Spinlock for XDP tx traffic */
544 			spinlock_t xdp_lock;
545 		};
546 
547 		/* DQO fields. */
548 		struct {
549 			u32 head; /* Last read on compl_desc */
550 
551 			/* Tracks the current gen bit of compl_q */
552 			u8 cur_gen_bit;
553 
554 			/* Linked list of gve_tx_pending_packet_dqo. Index into
555 			 * pending_packets, or -1 if empty.
556 			 *
557 			 * This is the producer list, owned by the completion
558 			 * handling path. When the consumer list
559 			 * (dqo_tx.free_pending_packets) is runs out, this list
560 			 * will be stolen.
561 			 */
562 			atomic_t free_pending_packets;
563 
564 			/* Last TX ring index fetched by HW */
565 			atomic_t hw_tx_head;
566 
567 			u16 xsk_reorder_queue_head;
568 			u16 xsk_reorder_queue_tail;
569 
570 			/* List to track pending packets which received a miss
571 			 * completion but not a corresponding reinjection.
572 			 */
573 			struct gve_index_list miss_completions;
574 
575 			/* List to track pending packets that were completed
576 			 * before receiving a valid completion because they
577 			 * reached a specified timeout.
578 			 */
579 			struct gve_index_list timed_out_completions;
580 
581 			/* QPL fields */
582 			struct {
583 				/* Linked list of gve_tx_buf_dqo. Index into
584 				 * tx_qpl_buf_next, or -1 if empty.
585 				 *
586 				 * This is the producer list, owned by the completion
587 				 * handling path. When the consumer list
588 				 * (dqo_tx.free_tx_qpl_buf_head) is runs out, this list
589 				 * will be stolen.
590 				 */
591 				atomic_t free_tx_qpl_buf_head;
592 
593 				/* Free running count of the number of tx buffers
594 				 * freed
595 				 */
596 				atomic_t free_tx_qpl_buf_cnt;
597 			};
598 		} dqo_compl;
599 	} ____cacheline_aligned;
600 	u64 pkt_done; /* free-running - total packets completed */
601 	u64 bytes_done; /* free-running - total bytes completed */
602 	u64 dropped_pkt; /* free-running - total packets dropped */
603 	u64 dma_mapping_error; /* count of dma mapping errors */
604 
605 	/* Cacheline 2 -- Read-mostly fields */
606 	union {
607 		/* GQI fields */
608 		struct {
609 			union gve_tx_desc *desc;
610 
611 			/* Maps 1:1 to a desc */
612 			struct gve_tx_buffer_state *info;
613 		};
614 
615 		/* DQO fields. */
616 		struct {
617 			union gve_tx_desc_dqo *tx_ring;
618 			struct gve_tx_compl_desc *compl_ring;
619 
620 			struct gve_tx_pending_packet_dqo *pending_packets;
621 			s16 num_pending_packets;
622 
623 			u16 *xsk_reorder_queue;
624 
625 			u32 complq_mask; /* complq size is complq_mask + 1 */
626 
627 			/* QPL fields */
628 			struct {
629 				/* qpl assigned to this queue */
630 				struct gve_queue_page_list *qpl;
631 
632 				/* Each QPL page is divided into TX bounce buffers
633 				 * of size GVE_TX_BUF_SIZE_DQO. tx_qpl_buf_next is
634 				 * an array to manage linked lists of TX buffers.
635 				 * An entry j at index i implies that j'th buffer
636 				 * is next on the list after i
637 				 */
638 				s16 *tx_qpl_buf_next;
639 				u32 num_tx_qpl_bufs;
640 			};
641 		} dqo;
642 	} ____cacheline_aligned;
643 	struct netdev_queue *netdev_txq;
644 	struct gve_queue_resources *q_resources; /* head and tail pointer idx */
645 	struct device *dev;
646 	u32 mask; /* masks req and done down to queue size */
647 	u8 raw_addressing; /* use raw_addressing? */
648 
649 	/* Slow-path fields */
650 	u32 q_num ____cacheline_aligned; /* queue idx */
651 	u32 stop_queue; /* count of queue stops */
652 	u32 wake_queue; /* count of queue wakes */
653 	u32 queue_timeout; /* count of queue timeouts */
654 	u32 ntfy_id; /* notification block index */
655 	u32 last_kick_msec; /* Last time the queue was kicked */
656 	dma_addr_t bus; /* dma address of the descr ring */
657 	dma_addr_t q_resources_bus; /* dma address of the queue resources */
658 	dma_addr_t complq_bus_dqo; /* dma address of the dqo.compl_ring */
659 	struct u64_stats_sync statss; /* sync stats for 32bit archs */
660 	struct xsk_buff_pool *xsk_pool;
661 	u64 xdp_xsk_sent;
662 	u64 xdp_xmit;
663 	u64 xdp_xmit_errors;
664 } ____cacheline_aligned;
665 
666 /* Wraps the info for one irq including the napi struct and the queues
667  * associated with that irq.
668  */
669 struct gve_notify_block {
670 	__be32 *irq_db_index; /* pointer to idx into Bar2 */
671 	char name[IFNAMSIZ + 16]; /* name registered with the kernel */
672 	struct napi_struct napi; /* kernel napi struct for this block */
673 	struct gve_priv *priv;
674 	struct gve_tx_ring *tx; /* tx rings on this block */
675 	struct gve_rx_ring *rx; /* rx rings on this block */
676 	u32 irq;
677 	bool irq_requested;
678 };
679 
680 /* Tracks allowed and current rx queue settings */
681 struct gve_rx_queue_config {
682 	u16 max_queues;
683 	u16 num_queues;
684 	u16 packet_buffer_size;
685 };
686 
687 /* Tracks allowed and current tx queue settings */
688 struct gve_tx_queue_config {
689 	u16 max_queues;
690 	u16 num_queues; /* number of TX queues, excluding XDP queues */
691 	u16 num_xdp_queues;
692 };
693 
694 /* Tracks the available and used qpl IDs */
695 struct gve_qpl_config {
696 	u32 qpl_map_size; /* map memory size */
697 	unsigned long *qpl_id_map; /* bitmap of used qpl ids */
698 };
699 
700 struct gve_irq_db {
701 	__be32 index;
702 } ____cacheline_aligned;
703 
704 struct gve_ptype {
705 	u8 l3_type;  /* `gve_l3_type` in gve_adminq.h */
706 	u8 l4_type;  /* `gve_l4_type` in gve_adminq.h */
707 };
708 
709 struct gve_ptype_lut {
710 	struct gve_ptype ptypes[GVE_NUM_PTYPES];
711 };
712 
713 /* Parameters for allocating resources for tx queues */
714 struct gve_tx_alloc_rings_cfg {
715 	struct gve_tx_queue_config *qcfg;
716 	u16 pages_per_qpl;
717 
718 	u16 num_xdp_rings;
719 
720 	u16 ring_size;
721 	bool raw_addressing;
722 
723 	/* Allocated resources are returned here */
724 	struct gve_tx_ring *tx;
725 };
726 
727 /* Parameters for allocating resources for rx queues */
728 struct gve_rx_alloc_rings_cfg {
729 	/* tx config is also needed to determine QPL ids */
730 	struct gve_rx_queue_config *qcfg_rx;
731 	struct gve_tx_queue_config *qcfg_tx;
732 	u16 pages_per_qpl;
733 
734 	u16 ring_size;
735 	u16 packet_buffer_size;
736 	bool raw_addressing;
737 	bool enable_header_split;
738 	bool reset_rss;
739 	bool xdp;
740 
741 	/* Allocated resources are returned here */
742 	struct gve_rx_ring *rx;
743 };
744 
745 /* GVE_QUEUE_FORMAT_UNSPECIFIED must be zero since 0 is the default value
746  * when the entire configure_device_resources command is zeroed out and the
747  * queue_format is not specified.
748  */
749 enum gve_queue_format {
750 	GVE_QUEUE_FORMAT_UNSPECIFIED	= 0x0,
751 	GVE_GQI_RDA_FORMAT		= 0x1,
752 	GVE_GQI_QPL_FORMAT		= 0x2,
753 	GVE_DQO_RDA_FORMAT		= 0x3,
754 	GVE_DQO_QPL_FORMAT		= 0x4,
755 };
756 
757 struct gve_flow_spec {
758 	__be32 src_ip[4];
759 	__be32 dst_ip[4];
760 	union {
761 		struct {
762 			__be16 src_port;
763 			__be16 dst_port;
764 		};
765 		__be32 spi;
766 	};
767 	union {
768 		u8 tos;
769 		u8 tclass;
770 	};
771 };
772 
773 struct gve_flow_rule {
774 	u32 location;
775 	u16 flow_type;
776 	u16 action;
777 	struct gve_flow_spec key;
778 	struct gve_flow_spec mask;
779 };
780 
781 struct gve_flow_rules_cache {
782 	bool rules_cache_synced; /* False if the driver's rules_cache is outdated */
783 	struct gve_adminq_queried_flow_rule *rules_cache;
784 	__be32 *rule_ids_cache;
785 	/* The total number of queried rules that stored in the caches */
786 	u32 rules_cache_num;
787 	u32 rule_ids_cache_num;
788 };
789 
790 struct gve_rss_config {
791 	u8 *hash_key;
792 	u32 *hash_lut;
793 };
794 
795 struct gve_ptp {
796 	struct ptp_clock_info info;
797 	struct ptp_clock *clock;
798 	struct gve_priv *priv;
799 };
800 
801 struct gve_device_info {
802 	enum gve_queue_format queue_format;
803 	u16 default_tx_queues;
804 	u16 default_rx_queues;
805 	u16 max_tx_queues;
806 	u16 max_rx_queues;
807 	u16 default_tx_ring_size;
808 	u16 default_rx_ring_size;
809 	u16 max_tx_ring_size;
810 	u16 max_rx_ring_size;
811 	u16 min_tx_ring_size;
812 	u16 min_rx_ring_size;
813 	u16 max_mtu;
814 	u8 mac[ETH_ALEN];
815 	u16 max_rx_buffer_size;
816 	u16 header_buf_size;
817 	u32 max_flow_rules;
818 	u16 rss_key_size;
819 	u16 rss_lut_size;
820 	u16 tx_pages_per_qpl;
821 	u16 num_event_counters;
822 	u64 max_registered_pages;
823 	bool default_min_ring_size;
824 	bool nic_timestamp_supported;
825 	bool modify_ring_size_enabled;
826 	bool cache_rss_config;
827 };
828 
829 /**
830  * struct gve_ctrl_ops - Control plane operations structure
831  * @map_db_bar: Maps the doorbell BAR for the device and store in @priv.
832  * @unmap_db_bar: Unmaps the doorbell BAR previously mapped by @map_db_bar.
833  * @set_num_queues: Sets default and max TX/RX queues into allocation
834  *                  structures stored in @priv to be used during initialization.
835  * @set_num_ntfy_blks: Sets no. of vectors into @priv to be used during
836  *                     initialization.
837  * @request_db_info: Request and store doorbell information into @priv
838  * @release_db_resources: Release device hold on DMA memory holding doorbell
839  *			  info (AdminQ only)
840  * @setup_mgmt_irq: Setup control plane IRQ
841  * @teardown_mgmt_irq: Teardown control plane IRQ
842  * @get_ptype_map: Learn packet type map from device and store it in @priv
843  * @configure_rss: Set up default RSS configuration
844  * @setup_stats_report: Set up DMA region for stats report (AdminQ only)
845  * @reset_flow_rules: Flush all flow rules from device
846  * @create_queues: Sends commands to the device to create TX/RX queues.
847  * @destroy_queues: Sends commands to the device to destroy TX/RX queues.
848  * @report_link_status: Set link status into @priv->link_up
849  * @report_link_speed: Set link speed into @priv->link_speed
850  */
851 struct gve_ctrl_ops {
852 	int (*map_db_bar)(struct gve_priv *priv);
853 	void (*unmap_db_bar)(struct gve_priv *priv);
854 	void (*set_num_queues)(struct gve_priv *priv);
855 	int (*set_num_ntfy_blks)(struct gve_priv *priv);
856 	int (*request_db_info)(struct gve_priv *priv);
857 	void (*release_db_resources)(struct gve_priv *priv);
858 	int (*setup_mgmt_irq)(struct gve_priv *priv);
859 	void (*teardown_mgmt_irq)(struct gve_priv *priv);
860 	int (*get_ptype_map)(struct gve_priv *priv);
861 	int (*configure_rss)(struct gve_priv *priv,
862 			     struct ethtool_rxfh_param *param);
863 	int (*setup_stats_report)(struct gve_priv *priv,
864 				  u64 stats_report_len,
865 				  dma_addr_t stats_report_addr,
866 				  u64 interval_ms); /* AQ-specific */
867 	int (*reset_flow_rules)(struct gve_priv *priv);
868 	int (*create_queues)(struct gve_priv *priv);
869 	int (*destroy_queues)(struct gve_priv *priv);
870 	int (*report_link_status)(struct gve_priv *priv);
871 	int (*report_link_speed)(struct gve_priv *priv);
872 };
873 
874 struct gve_priv {
875 	struct net_device *dev;
876 	struct gve_tx_ring *tx; /* array of tx_cfg.num_queues */
877 	struct gve_rx_ring *rx; /* array of rx_cfg.num_queues */
878 	struct gve_notify_block *ntfy_blocks; /* array of num_ntfy_blks */
879 	struct gve_irq_db *irq_db_indices; /* array of num_ntfy_blks */
880 	dma_addr_t irq_db_indices_bus;
881 	struct msix_entry *msix_vectors; /* array of num_ntfy_blks + 1 */
882 	char mgmt_msix_name[IFNAMSIZ + 16];
883 	u32 mgmt_msix_idx;
884 	__be32 *counter_array; /* array of num_event_counters */
885 	dma_addr_t counter_array_bus;
886 
887 	u16 num_event_counters;
888 	u16 tx_desc_cnt; /* num desc per ring */
889 	u16 rx_desc_cnt; /* num desc per ring */
890 	u16 max_tx_desc_cnt;
891 	u16 max_rx_desc_cnt;
892 	u16 min_tx_desc_cnt;
893 	u16 min_rx_desc_cnt;
894 	bool modify_ring_size_enabled;
895 	bool default_min_ring_size;
896 	u16 tx_pages_per_qpl;
897 	u16 rx_pages_per_qpl;
898 	u64 max_registered_pages;
899 	u64 num_registered_pages; /* num pages registered with NIC */
900 	struct bpf_prog *xdp_prog; /* XDP BPF program */
901 	u32 rx_copybreak; /* copy packets smaller than this */
902 	u16 default_num_queues; /* default num queues to set up */
903 
904 	struct gve_tx_queue_config tx_cfg;
905 	struct gve_rx_queue_config rx_cfg;
906 	unsigned long *xsk_pools; /* bitmap of RX queues with XSK pools */
907 	u32 num_ntfy_blks; /* split between TX and RX so must be even */
908 	int numa_node;
909 
910 	struct gve_registers __iomem *reg_bar0; /* see gve_register.h */
911 	__be32 __iomem *db_bar2; /* "array" of doorbells */
912 	u32 msg_enable;	/* level for netif* netdev print macros	*/
913 	struct pci_dev *pdev;
914 
915 	/* metrics */
916 	u32 tx_timeo_cnt;
917 
918 	/* Admin queue - see gve_adminq.h*/
919 	union gve_adminq_command *adminq;
920 	dma_addr_t adminq_bus_addr;
921 	struct dma_pool *adminq_pool;
922 	struct mutex adminq_lock; /* Protects adminq command execution */
923 	u32 adminq_mask; /* masks prod_cnt to adminq size */
924 	u32 adminq_prod_cnt; /* free-running count of AQ cmds executed */
925 	u32 adminq_cmd_fail; /* free-running count of AQ cmds failed */
926 	u32 adminq_timeouts; /* free-running count of AQ cmds timeouts */
927 	/* free-running count of per AQ cmd executed */
928 	u32 adminq_describe_device_cnt;
929 	u32 adminq_cfg_device_resources_cnt;
930 	u32 adminq_register_page_list_cnt;
931 	u32 adminq_unregister_page_list_cnt;
932 	u32 adminq_create_tx_queue_cnt;
933 	u32 adminq_create_rx_queue_cnt;
934 	u32 adminq_destroy_tx_queue_cnt;
935 	u32 adminq_destroy_rx_queue_cnt;
936 	u32 adminq_dcfg_device_resources_cnt;
937 	u32 adminq_set_driver_parameter_cnt;
938 	u32 adminq_report_stats_cnt;
939 	u32 adminq_report_link_speed_cnt;
940 	u32 adminq_report_nic_timestamp_cnt;
941 	u32 adminq_get_ptype_map_cnt;
942 	u32 adminq_verify_driver_compatibility_cnt;
943 	u32 adminq_query_flow_rules_cnt;
944 	u32 adminq_cfg_flow_rule_cnt;
945 	u32 adminq_cfg_rss_cnt;
946 	u32 adminq_query_rss_cnt;
947 
948 	/* Global stats */
949 	u32 interface_up_cnt; /* count of times interface turned up since last reset */
950 	u32 interface_down_cnt; /* count of times interface turned down since last reset */
951 	u32 reset_cnt; /* count of reset */
952 	u32 page_alloc_fail; /* count of page alloc fails */
953 	u32 dma_mapping_error; /* count of dma mapping errors */
954 	u32 stats_report_trigger_cnt; /* count of device-requested stats-reports since last reset */
955 	u32 suspend_cnt; /* count of times suspended */
956 	u32 resume_cnt; /* count of times resumed */
957 	struct workqueue_struct *gve_wq;
958 	struct work_struct service_task;
959 	struct work_struct stats_report_task;
960 	unsigned long service_task_flags;
961 	unsigned long state_flags;
962 
963 	struct gve_stats_report *stats_report;
964 	u64 stats_report_len;
965 	dma_addr_t stats_report_bus; /* dma address for the stats report */
966 	unsigned long ethtool_flags;
967 
968 	unsigned long stats_report_timer_period;
969 	struct timer_list stats_report_timer;
970 
971 	/* Gvnic device link speed from hypervisor. */
972 	u64 link_speed;
973 	bool link_up;
974 	bool up_before_suspend; /* True if dev was up before suspend */
975 
976 	bool mgmt_irq_requested;
977 	struct gve_ptype_lut *ptype_lut_dqo;
978 
979 	/* Must be a power of two. */
980 	u16 max_rx_buffer_size; /* device limit */
981 
982 	enum gve_queue_format queue_format;
983 
984 	/* Interrupt coalescing settings */
985 	u32 tx_coalesce_usecs;
986 	u32 rx_coalesce_usecs;
987 
988 	u16 header_buf_size; /* device configured, header-split supported if non-zero */
989 	bool header_split_enabled; /* True if the header split is enabled by the user */
990 
991 	u32 max_flow_rules;
992 	u32 num_flow_rules;
993 
994 	struct gve_flow_rules_cache flow_rules_cache;
995 
996 	u16 rss_key_size;
997 	u16 rss_lut_size;
998 	bool cache_rss_config;
999 	struct gve_rss_config rss_config;
1000 
1001 	/* True if the device supports reading the nic clock */
1002 	bool nic_timestamp_supported;
1003 	struct gve_ptp *ptp;
1004 	struct kernel_hwtstamp_config ts_config;
1005 	struct gve_nic_ts_report *nic_ts_report;
1006 	dma_addr_t nic_ts_report_bus;
1007 	u64 last_sync_nic_counter; /* Clock counter from last NIC TS report */
1008 	struct gve_device_info device_info;
1009 	const struct gve_ctrl_ops *ctrl_ops;
1010 };
1011 
1012 enum gve_service_task_flags_bit {
1013 	GVE_PRIV_FLAGS_DO_RESET			= 1,
1014 	GVE_PRIV_FLAGS_RESET_IN_PROGRESS	= 2,
1015 	GVE_PRIV_FLAGS_PROBE_IN_PROGRESS	= 3,
1016 	GVE_PRIV_FLAGS_DO_REPORT_STATS = 4,
1017 };
1018 
1019 enum gve_state_flags_bit {
1020 	GVE_PRIV_FLAGS_ADMIN_QUEUE_OK		= 1,
1021 	GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK	= 2,
1022 	GVE_PRIV_FLAGS_DEVICE_RINGS_OK		= 3,
1023 	GVE_PRIV_FLAGS_NAPI_ENABLED		= 4,
1024 };
1025 
1026 enum gve_ethtool_flags_bit {
1027 	GVE_PRIV_FLAGS_REPORT_STATS		= 0,
1028 };
1029 
1030 static inline bool gve_get_do_reset(struct gve_priv *priv)
1031 {
1032 	return test_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
1033 }
1034 
1035 static inline void gve_set_do_reset(struct gve_priv *priv)
1036 {
1037 	set_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
1038 }
1039 
1040 static inline void gve_clear_do_reset(struct gve_priv *priv)
1041 {
1042 	clear_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
1043 }
1044 
1045 static inline bool gve_get_reset_in_progress(struct gve_priv *priv)
1046 {
1047 	return test_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS,
1048 			&priv->service_task_flags);
1049 }
1050 
1051 static inline void gve_set_reset_in_progress(struct gve_priv *priv)
1052 {
1053 	set_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
1054 }
1055 
1056 static inline void gve_clear_reset_in_progress(struct gve_priv *priv)
1057 {
1058 	clear_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
1059 }
1060 
1061 static inline bool gve_get_probe_in_progress(struct gve_priv *priv)
1062 {
1063 	return test_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS,
1064 			&priv->service_task_flags);
1065 }
1066 
1067 static inline void gve_set_probe_in_progress(struct gve_priv *priv)
1068 {
1069 	set_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
1070 }
1071 
1072 static inline void gve_clear_probe_in_progress(struct gve_priv *priv)
1073 {
1074 	clear_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
1075 }
1076 
1077 static inline bool gve_get_do_report_stats(struct gve_priv *priv)
1078 {
1079 	return test_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS,
1080 			&priv->service_task_flags);
1081 }
1082 
1083 static inline void gve_set_do_report_stats(struct gve_priv *priv)
1084 {
1085 	set_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
1086 }
1087 
1088 static inline void gve_clear_do_report_stats(struct gve_priv *priv)
1089 {
1090 	clear_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
1091 }
1092 
1093 static inline bool gve_get_admin_queue_ok(struct gve_priv *priv)
1094 {
1095 	return test_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
1096 }
1097 
1098 static inline void gve_set_admin_queue_ok(struct gve_priv *priv)
1099 {
1100 	set_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
1101 }
1102 
1103 static inline void gve_clear_admin_queue_ok(struct gve_priv *priv)
1104 {
1105 	clear_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
1106 }
1107 
1108 static inline bool gve_get_device_resources_ok(struct gve_priv *priv)
1109 {
1110 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
1111 }
1112 
1113 static inline void gve_set_device_resources_ok(struct gve_priv *priv)
1114 {
1115 	set_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
1116 }
1117 
1118 static inline void gve_clear_device_resources_ok(struct gve_priv *priv)
1119 {
1120 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
1121 }
1122 
1123 static inline bool gve_get_device_rings_ok(struct gve_priv *priv)
1124 {
1125 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
1126 }
1127 
1128 static inline void gve_set_device_rings_ok(struct gve_priv *priv)
1129 {
1130 	set_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
1131 }
1132 
1133 static inline void gve_clear_device_rings_ok(struct gve_priv *priv)
1134 {
1135 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
1136 }
1137 
1138 static inline bool gve_get_napi_enabled(struct gve_priv *priv)
1139 {
1140 	return test_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
1141 }
1142 
1143 static inline void gve_set_napi_enabled(struct gve_priv *priv)
1144 {
1145 	set_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
1146 }
1147 
1148 static inline void gve_clear_napi_enabled(struct gve_priv *priv)
1149 {
1150 	clear_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
1151 }
1152 
1153 static inline bool gve_get_report_stats(struct gve_priv *priv)
1154 {
1155 	return test_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
1156 }
1157 
1158 static inline void gve_clear_report_stats(struct gve_priv *priv)
1159 {
1160 	clear_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
1161 }
1162 
1163 /* Returns the address of the ntfy_blocks irq doorbell
1164  */
1165 static inline __be32 __iomem *gve_irq_doorbell(struct gve_priv *priv,
1166 					       struct gve_notify_block *block)
1167 {
1168 	return &priv->db_bar2[be32_to_cpu(*block->irq_db_index)];
1169 }
1170 
1171 /* Returns the index into ntfy_blocks of the given tx ring's block
1172  */
1173 static inline u32 gve_tx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
1174 {
1175 	return queue_idx;
1176 }
1177 
1178 /* Returns the index into ntfy_blocks of the given rx ring's block
1179  */
1180 static inline u32 gve_rx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
1181 {
1182 	return (priv->num_ntfy_blks / 2) + queue_idx;
1183 }
1184 
1185 static inline u32 gve_ntfy_to_msix_idx(struct gve_priv *priv, u32 ntfy_blk_idx)
1186 {
1187 	return ntfy_blk_idx;
1188 }
1189 
1190 static inline bool gve_is_qpl(struct gve_priv *priv)
1191 {
1192 	return priv->queue_format == GVE_GQI_QPL_FORMAT ||
1193 		priv->queue_format == GVE_DQO_QPL_FORMAT;
1194 }
1195 
1196 /* Returns the number of tx queue page lists */
1197 static inline u32 gve_num_tx_qpls(const struct gve_tx_queue_config *tx_cfg,
1198 				  bool is_qpl)
1199 {
1200 	if (!is_qpl)
1201 		return 0;
1202 	return tx_cfg->num_queues + tx_cfg->num_xdp_queues;
1203 }
1204 
1205 /* Returns the number of rx queue page lists */
1206 static inline u32 gve_num_rx_qpls(const struct gve_rx_queue_config *rx_cfg,
1207 				  bool is_qpl)
1208 {
1209 	if (!is_qpl)
1210 		return 0;
1211 	return rx_cfg->num_queues;
1212 }
1213 
1214 static inline u32 gve_tx_qpl_id(struct gve_priv *priv, int tx_qid)
1215 {
1216 	return tx_qid;
1217 }
1218 
1219 static inline u32 gve_rx_qpl_id(struct gve_priv *priv, int rx_qid)
1220 {
1221 	return priv->tx_cfg.max_queues + rx_qid;
1222 }
1223 
1224 static inline u32 gve_get_rx_qpl_id(const struct gve_tx_queue_config *tx_cfg,
1225 				    int rx_qid)
1226 {
1227 	return tx_cfg->max_queues + rx_qid;
1228 }
1229 
1230 static inline u32 gve_tx_start_qpl_id(struct gve_priv *priv)
1231 {
1232 	return gve_tx_qpl_id(priv, 0);
1233 }
1234 
1235 static inline u32 gve_rx_start_qpl_id(const struct gve_tx_queue_config *tx_cfg)
1236 {
1237 	return gve_get_rx_qpl_id(tx_cfg, 0);
1238 }
1239 
1240 /* Returns the correct dma direction for tx and rx qpls */
1241 static inline enum dma_data_direction gve_qpl_dma_dir(struct gve_priv *priv,
1242 						      int id)
1243 {
1244 	if (id < gve_rx_start_qpl_id(&priv->tx_cfg))
1245 		return DMA_TO_DEVICE;
1246 	else
1247 		return DMA_FROM_DEVICE;
1248 }
1249 
1250 static inline bool gve_is_gqi(struct gve_priv *priv)
1251 {
1252 	return priv->queue_format == GVE_GQI_RDA_FORMAT ||
1253 		priv->queue_format == GVE_GQI_QPL_FORMAT;
1254 }
1255 
1256 static inline bool gve_is_dqo(struct gve_priv *priv)
1257 {
1258 	return priv->queue_format == GVE_DQO_RDA_FORMAT ||
1259 	       priv->queue_format == GVE_DQO_QPL_FORMAT;
1260 }
1261 
1262 static inline u32 gve_num_tx_queues(struct gve_priv *priv)
1263 {
1264 	return priv->tx_cfg.num_queues + priv->tx_cfg.num_xdp_queues;
1265 }
1266 
1267 static inline u32 gve_xdp_tx_queue_id(struct gve_priv *priv, u32 queue_id)
1268 {
1269 	return priv->tx_cfg.num_queues + queue_id;
1270 }
1271 
1272 static inline u32 gve_xdp_tx_start_queue_id(struct gve_priv *priv)
1273 {
1274 	return gve_xdp_tx_queue_id(priv, 0);
1275 }
1276 
1277 static inline bool gve_supports_xdp_xmit(struct gve_priv *priv)
1278 {
1279 	switch (priv->queue_format) {
1280 	case GVE_GQI_QPL_FORMAT:
1281 	case GVE_DQO_RDA_FORMAT:
1282 		return true;
1283 	default:
1284 		return false;
1285 	}
1286 }
1287 
1288 static inline bool gve_is_clock_enabled(struct gve_priv *priv)
1289 {
1290 	return priv->nic_ts_report;
1291 }
1292 
1293 void gve_adminq_write_version(u8 __iomem *driver_version_register);
1294 
1295 /* gqi napi handler defined in gve_main.c */
1296 int gve_napi_poll(struct napi_struct *napi, int budget);
1297 
1298 /* buffers */
1299 int gve_alloc_page(struct gve_priv *priv, struct device *dev,
1300 		   struct page **page, dma_addr_t *dma,
1301 		   enum dma_data_direction, gfp_t gfp_flags);
1302 void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
1303 		   enum dma_data_direction);
1304 /* qpls */
1305 struct gve_queue_page_list *gve_alloc_queue_page_list(struct gve_priv *priv,
1306 						      u32 id, int pages);
1307 void gve_free_queue_page_list(struct gve_priv *priv,
1308 			      struct gve_queue_page_list *qpl,
1309 			      u32 id);
1310 /* tx handling */
1311 netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev);
1312 int gve_xdp_xmit_gqi(struct net_device *dev, int n, struct xdp_frame **frames,
1313 		     u32 flags);
1314 int gve_xdp_xmit_dqo(struct net_device *dev, int n, struct xdp_frame **frames,
1315 		     u32 flags);
1316 int gve_xdp_xmit_one(struct gve_priv *priv, struct gve_tx_ring *tx,
1317 		     void *data, int len, void *frame_p);
1318 void gve_xdp_tx_flush(struct gve_priv *priv, u32 xdp_qid);
1319 int gve_xdp_xmit_one_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1320 			 struct xdp_frame *xdpf);
1321 bool gve_tx_poll(struct gve_notify_block *block, int budget);
1322 bool gve_xdp_poll(struct gve_notify_block *block, int budget);
1323 int gve_xsk_tx_poll(struct gve_notify_block *block, int budget);
1324 int gve_tx_alloc_rings_gqi(struct gve_priv *priv,
1325 			   struct gve_tx_alloc_rings_cfg *cfg);
1326 void gve_tx_free_rings_gqi(struct gve_priv *priv,
1327 			   struct gve_tx_alloc_rings_cfg *cfg);
1328 void gve_tx_start_ring_gqi(struct gve_priv *priv, int idx);
1329 void gve_tx_stop_ring_gqi(struct gve_priv *priv, int idx);
1330 u32 gve_tx_load_event_counter(struct gve_priv *priv,
1331 			      struct gve_tx_ring *tx);
1332 bool gve_tx_clean_pending(struct gve_priv *priv, struct gve_tx_ring *tx);
1333 /* rx handling */
1334 void gve_rx_write_doorbell(struct gve_priv *priv, struct gve_rx_ring *rx);
1335 int gve_rx_poll(struct gve_notify_block *block, int budget);
1336 bool gve_rx_work_pending(struct gve_rx_ring *rx);
1337 int gve_rx_alloc_ring_gqi(struct gve_priv *priv,
1338 			  struct gve_rx_alloc_rings_cfg *cfg,
1339 			  struct gve_rx_ring *rx,
1340 			  int idx);
1341 void gve_rx_free_ring_gqi(struct gve_priv *priv, struct gve_rx_ring *rx,
1342 			  struct gve_rx_alloc_rings_cfg *cfg);
1343 int gve_rx_alloc_rings_gqi(struct gve_priv *priv,
1344 			   struct gve_rx_alloc_rings_cfg *cfg);
1345 void gve_rx_free_rings_gqi(struct gve_priv *priv,
1346 			   struct gve_rx_alloc_rings_cfg *cfg);
1347 void gve_rx_start_ring_gqi(struct gve_priv *priv, int idx);
1348 void gve_rx_stop_ring_gqi(struct gve_priv *priv, int idx);
1349 bool gve_header_split_supported(const struct gve_priv *priv);
1350 int gve_set_rx_buf_len_config(struct gve_priv *priv, u32 rx_buf_len,
1351 			      struct netlink_ext_ack *extack,
1352 			      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1353 int gve_set_hsplit_config(struct gve_priv *priv, u8 tcp_data_split,
1354 			  struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1355 /* rx buffer handling */
1356 int gve_buf_ref_cnt(struct gve_rx_buf_state_dqo *bs);
1357 void gve_free_page_dqo(struct gve_priv *priv, struct gve_rx_buf_state_dqo *bs,
1358 		       bool free_page);
1359 struct gve_rx_buf_state_dqo *gve_alloc_buf_state(struct gve_rx_ring *rx);
1360 bool gve_buf_state_is_allocated(struct gve_rx_ring *rx,
1361 				struct gve_rx_buf_state_dqo *buf_state);
1362 void gve_free_buf_state(struct gve_rx_ring *rx,
1363 			struct gve_rx_buf_state_dqo *buf_state);
1364 struct gve_rx_buf_state_dqo *gve_dequeue_buf_state(struct gve_rx_ring *rx,
1365 						   struct gve_index_list *list);
1366 void gve_enqueue_buf_state(struct gve_rx_ring *rx, struct gve_index_list *list,
1367 			   struct gve_rx_buf_state_dqo *buf_state);
1368 struct gve_rx_buf_state_dqo *gve_get_recycled_buf_state(struct gve_rx_ring *rx);
1369 void gve_try_recycle_buf(struct gve_priv *priv, struct gve_rx_ring *rx,
1370 			 struct gve_rx_buf_state_dqo *buf_state);
1371 void gve_free_to_page_pool(struct gve_rx_ring *rx,
1372 			   struct gve_rx_buf_state_dqo *buf_state,
1373 			   bool allow_direct);
1374 int gve_alloc_qpl_page_dqo(struct gve_rx_ring *rx,
1375 			   struct gve_rx_buf_state_dqo *buf_state);
1376 void gve_free_qpl_page_dqo(struct gve_rx_buf_state_dqo *buf_state);
1377 void gve_reuse_buffer(struct gve_rx_ring *rx,
1378 		      struct gve_rx_buf_state_dqo *buf_state);
1379 void gve_free_buffer(struct gve_rx_ring *rx,
1380 		     struct gve_rx_buf_state_dqo *buf_state);
1381 int gve_alloc_buffer(struct gve_rx_ring *rx, struct gve_rx_desc_dqo *desc);
1382 struct page_pool *gve_rx_create_page_pool(struct gve_priv *priv,
1383 					  struct gve_rx_ring *rx,
1384 					  bool xdp);
1385 
1386 /* Reset */
1387 void gve_schedule_reset(struct gve_priv *priv);
1388 int gve_reset(struct gve_priv *priv, bool skip_queue_setup);
1389 void gve_get_curr_alloc_cfgs(struct gve_priv *priv,
1390 			     struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1391 			     struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1392 void gve_update_num_qpl_pages(struct gve_priv *priv,
1393 			      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg,
1394 			      struct gve_tx_alloc_rings_cfg *tx_alloc_cfg);
1395 int gve_adjust_config(struct gve_priv *priv,
1396 		      struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1397 		      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1398 int gve_adjust_queues(struct gve_priv *priv,
1399 		      struct gve_rx_queue_config new_rx_config,
1400 		      struct gve_tx_queue_config new_tx_config,
1401 		      bool reset_rss);
1402 /* flow steering rule */
1403 int gve_get_flow_rule_entry(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1404 int gve_get_flow_rule_ids(struct gve_priv *priv, struct ethtool_rxnfc *cmd, u32 *rule_locs);
1405 int gve_add_flow_rule(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1406 int gve_del_flow_rule(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1407 int gve_flow_rules_reset(struct gve_priv *priv);
1408 /* RSS config */
1409 int gve_init_rss_config(struct gve_priv *priv, u16 num_queues);
1410 /* PTP and timestamping */
1411 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
1412 int gve_clock_nic_ts_read(struct gve_priv *priv);
1413 int gve_init_clock(struct gve_priv *priv);
1414 void gve_teardown_clock(struct gve_priv *priv);
1415 #else /* CONFIG_PTP_1588_CLOCK */
1416 static inline int gve_clock_nic_ts_read(struct gve_priv *priv)
1417 {
1418 	return -EOPNOTSUPP;
1419 }
1420 
1421 static inline int gve_init_clock(struct gve_priv *priv)
1422 {
1423 	return 0;
1424 }
1425 
1426 static inline void gve_teardown_clock(struct gve_priv *priv) { }
1427 #endif /* CONFIG_PTP_1588_CLOCK */
1428 /* report stats handling */
1429 void gve_handle_report_stats(struct gve_priv *priv);
1430 /* exported by ethtool.c */
1431 extern const struct ethtool_ops gve_ethtool_ops;
1432 /* needed by ethtool */
1433 extern char gve_driver_name[];
1434 extern const char gve_version_str[];
1435 #endif /* _GVE_H_ */
1436