xref: /linux/drivers/net/ethernet/google/gve/gve.h (revision fab183d632628381b466a41479489541ac0e29a0)
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 };
678 
679 /* Tracks allowed and current rx queue settings */
680 struct gve_rx_queue_config {
681 	u16 max_queues;
682 	u16 num_queues;
683 	u16 packet_buffer_size;
684 };
685 
686 /* Tracks allowed and current tx queue settings */
687 struct gve_tx_queue_config {
688 	u16 max_queues;
689 	u16 num_queues; /* number of TX queues, excluding XDP queues */
690 	u16 num_xdp_queues;
691 };
692 
693 /* Tracks the available and used qpl IDs */
694 struct gve_qpl_config {
695 	u32 qpl_map_size; /* map memory size */
696 	unsigned long *qpl_id_map; /* bitmap of used qpl ids */
697 };
698 
699 struct gve_irq_db {
700 	__be32 index;
701 } ____cacheline_aligned;
702 
703 struct gve_ptype {
704 	u8 l3_type;  /* `gve_l3_type` in gve_adminq.h */
705 	u8 l4_type;  /* `gve_l4_type` in gve_adminq.h */
706 };
707 
708 struct gve_ptype_lut {
709 	struct gve_ptype ptypes[GVE_NUM_PTYPES];
710 };
711 
712 /* Parameters for allocating resources for tx queues */
713 struct gve_tx_alloc_rings_cfg {
714 	struct gve_tx_queue_config *qcfg;
715 	u16 pages_per_qpl;
716 
717 	u16 num_xdp_rings;
718 
719 	u16 ring_size;
720 	bool raw_addressing;
721 
722 	/* Allocated resources are returned here */
723 	struct gve_tx_ring *tx;
724 };
725 
726 /* Parameters for allocating resources for rx queues */
727 struct gve_rx_alloc_rings_cfg {
728 	/* tx config is also needed to determine QPL ids */
729 	struct gve_rx_queue_config *qcfg_rx;
730 	struct gve_tx_queue_config *qcfg_tx;
731 	u16 pages_per_qpl;
732 
733 	u16 ring_size;
734 	u16 packet_buffer_size;
735 	bool raw_addressing;
736 	bool enable_header_split;
737 	bool reset_rss;
738 	bool xdp;
739 
740 	/* Allocated resources are returned here */
741 	struct gve_rx_ring *rx;
742 };
743 
744 /* GVE_QUEUE_FORMAT_UNSPECIFIED must be zero since 0 is the default value
745  * when the entire configure_device_resources command is zeroed out and the
746  * queue_format is not specified.
747  */
748 enum gve_queue_format {
749 	GVE_QUEUE_FORMAT_UNSPECIFIED	= 0x0,
750 	GVE_GQI_RDA_FORMAT		= 0x1,
751 	GVE_GQI_QPL_FORMAT		= 0x2,
752 	GVE_DQO_RDA_FORMAT		= 0x3,
753 	GVE_DQO_QPL_FORMAT		= 0x4,
754 };
755 
756 struct gve_flow_spec {
757 	__be32 src_ip[4];
758 	__be32 dst_ip[4];
759 	union {
760 		struct {
761 			__be16 src_port;
762 			__be16 dst_port;
763 		};
764 		__be32 spi;
765 	};
766 	union {
767 		u8 tos;
768 		u8 tclass;
769 	};
770 };
771 
772 struct gve_flow_rule {
773 	u32 location;
774 	u16 flow_type;
775 	u16 action;
776 	struct gve_flow_spec key;
777 	struct gve_flow_spec mask;
778 };
779 
780 struct gve_flow_rules_cache {
781 	bool rules_cache_synced; /* False if the driver's rules_cache is outdated */
782 	struct gve_adminq_queried_flow_rule *rules_cache;
783 	__be32 *rule_ids_cache;
784 	/* The total number of queried rules that stored in the caches */
785 	u32 rules_cache_num;
786 	u32 rule_ids_cache_num;
787 };
788 
789 struct gve_rss_config {
790 	u8 *hash_key;
791 	u32 *hash_lut;
792 };
793 
794 struct gve_ptp {
795 	struct ptp_clock_info info;
796 	struct ptp_clock *clock;
797 	struct gve_priv *priv;
798 };
799 
800 struct gve_priv {
801 	struct net_device *dev;
802 	struct gve_tx_ring *tx; /* array of tx_cfg.num_queues */
803 	struct gve_rx_ring *rx; /* array of rx_cfg.num_queues */
804 	struct gve_notify_block *ntfy_blocks; /* array of num_ntfy_blks */
805 	struct gve_irq_db *irq_db_indices; /* array of num_ntfy_blks */
806 	dma_addr_t irq_db_indices_bus;
807 	struct msix_entry *msix_vectors; /* array of num_ntfy_blks + 1 */
808 	char mgmt_msix_name[IFNAMSIZ + 16];
809 	u32 mgmt_msix_idx;
810 	__be32 *counter_array; /* array of num_event_counters */
811 	dma_addr_t counter_array_bus;
812 
813 	u16 num_event_counters;
814 	u16 tx_desc_cnt; /* num desc per ring */
815 	u16 rx_desc_cnt; /* num desc per ring */
816 	u16 max_tx_desc_cnt;
817 	u16 max_rx_desc_cnt;
818 	u16 min_tx_desc_cnt;
819 	u16 min_rx_desc_cnt;
820 	bool modify_ring_size_enabled;
821 	bool default_min_ring_size;
822 	u16 tx_pages_per_qpl;
823 	u16 rx_pages_per_qpl;
824 	u64 max_registered_pages;
825 	u64 num_registered_pages; /* num pages registered with NIC */
826 	struct bpf_prog *xdp_prog; /* XDP BPF program */
827 	u32 rx_copybreak; /* copy packets smaller than this */
828 	u16 default_num_queues; /* default num queues to set up */
829 
830 	struct gve_tx_queue_config tx_cfg;
831 	struct gve_rx_queue_config rx_cfg;
832 	unsigned long *xsk_pools; /* bitmap of RX queues with XSK pools */
833 	u32 num_ntfy_blks; /* split between TX and RX so must be even */
834 	int numa_node;
835 
836 	struct gve_registers __iomem *reg_bar0; /* see gve_register.h */
837 	__be32 __iomem *db_bar2; /* "array" of doorbells */
838 	u32 msg_enable;	/* level for netif* netdev print macros	*/
839 	struct pci_dev *pdev;
840 
841 	/* metrics */
842 	u32 tx_timeo_cnt;
843 
844 	/* Admin queue - see gve_adminq.h*/
845 	union gve_adminq_command *adminq;
846 	dma_addr_t adminq_bus_addr;
847 	struct dma_pool *adminq_pool;
848 	struct mutex adminq_lock; /* Protects adminq command execution */
849 	u32 adminq_mask; /* masks prod_cnt to adminq size */
850 	u32 adminq_prod_cnt; /* free-running count of AQ cmds executed */
851 	u32 adminq_cmd_fail; /* free-running count of AQ cmds failed */
852 	u32 adminq_timeouts; /* free-running count of AQ cmds timeouts */
853 	/* free-running count of per AQ cmd executed */
854 	u32 adminq_describe_device_cnt;
855 	u32 adminq_cfg_device_resources_cnt;
856 	u32 adminq_register_page_list_cnt;
857 	u32 adminq_unregister_page_list_cnt;
858 	u32 adminq_create_tx_queue_cnt;
859 	u32 adminq_create_rx_queue_cnt;
860 	u32 adminq_destroy_tx_queue_cnt;
861 	u32 adminq_destroy_rx_queue_cnt;
862 	u32 adminq_dcfg_device_resources_cnt;
863 	u32 adminq_set_driver_parameter_cnt;
864 	u32 adminq_report_stats_cnt;
865 	u32 adminq_report_link_speed_cnt;
866 	u32 adminq_report_nic_timestamp_cnt;
867 	u32 adminq_get_ptype_map_cnt;
868 	u32 adminq_verify_driver_compatibility_cnt;
869 	u32 adminq_query_flow_rules_cnt;
870 	u32 adminq_cfg_flow_rule_cnt;
871 	u32 adminq_cfg_rss_cnt;
872 	u32 adminq_query_rss_cnt;
873 
874 	/* Global stats */
875 	u32 interface_up_cnt; /* count of times interface turned up since last reset */
876 	u32 interface_down_cnt; /* count of times interface turned down since last reset */
877 	u32 reset_cnt; /* count of reset */
878 	u32 page_alloc_fail; /* count of page alloc fails */
879 	u32 dma_mapping_error; /* count of dma mapping errors */
880 	u32 stats_report_trigger_cnt; /* count of device-requested stats-reports since last reset */
881 	u32 suspend_cnt; /* count of times suspended */
882 	u32 resume_cnt; /* count of times resumed */
883 	struct workqueue_struct *gve_wq;
884 	struct work_struct service_task;
885 	struct work_struct stats_report_task;
886 	unsigned long service_task_flags;
887 	unsigned long state_flags;
888 
889 	struct gve_stats_report *stats_report;
890 	u64 stats_report_len;
891 	dma_addr_t stats_report_bus; /* dma address for the stats report */
892 	unsigned long ethtool_flags;
893 
894 	unsigned long stats_report_timer_period;
895 	struct timer_list stats_report_timer;
896 
897 	/* Gvnic device link speed from hypervisor. */
898 	u64 link_speed;
899 	bool up_before_suspend; /* True if dev was up before suspend */
900 
901 	struct gve_ptype_lut *ptype_lut_dqo;
902 
903 	/* Must be a power of two. */
904 	u16 max_rx_buffer_size; /* device limit */
905 
906 	enum gve_queue_format queue_format;
907 
908 	/* Interrupt coalescing settings */
909 	u32 tx_coalesce_usecs;
910 	u32 rx_coalesce_usecs;
911 
912 	u16 header_buf_size; /* device configured, header-split supported if non-zero */
913 	bool header_split_enabled; /* True if the header split is enabled by the user */
914 
915 	u32 max_flow_rules;
916 	u32 num_flow_rules;
917 
918 	struct gve_flow_rules_cache flow_rules_cache;
919 
920 	u16 rss_key_size;
921 	u16 rss_lut_size;
922 	bool cache_rss_config;
923 	struct gve_rss_config rss_config;
924 
925 	/* True if the device supports reading the nic clock */
926 	bool nic_timestamp_supported;
927 	struct gve_ptp *ptp;
928 	struct kernel_hwtstamp_config ts_config;
929 	struct gve_nic_ts_report *nic_ts_report;
930 	dma_addr_t nic_ts_report_bus;
931 	u64 last_sync_nic_counter; /* Clock counter from last NIC TS report */
932 };
933 
934 enum gve_service_task_flags_bit {
935 	GVE_PRIV_FLAGS_DO_RESET			= 1,
936 	GVE_PRIV_FLAGS_RESET_IN_PROGRESS	= 2,
937 	GVE_PRIV_FLAGS_PROBE_IN_PROGRESS	= 3,
938 	GVE_PRIV_FLAGS_DO_REPORT_STATS = 4,
939 };
940 
941 enum gve_state_flags_bit {
942 	GVE_PRIV_FLAGS_ADMIN_QUEUE_OK		= 1,
943 	GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK	= 2,
944 	GVE_PRIV_FLAGS_DEVICE_RINGS_OK		= 3,
945 	GVE_PRIV_FLAGS_NAPI_ENABLED		= 4,
946 };
947 
948 enum gve_ethtool_flags_bit {
949 	GVE_PRIV_FLAGS_REPORT_STATS		= 0,
950 };
951 
gve_get_do_reset(struct gve_priv * priv)952 static inline bool gve_get_do_reset(struct gve_priv *priv)
953 {
954 	return test_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
955 }
956 
gve_set_do_reset(struct gve_priv * priv)957 static inline void gve_set_do_reset(struct gve_priv *priv)
958 {
959 	set_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
960 }
961 
gve_clear_do_reset(struct gve_priv * priv)962 static inline void gve_clear_do_reset(struct gve_priv *priv)
963 {
964 	clear_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
965 }
966 
gve_get_reset_in_progress(struct gve_priv * priv)967 static inline bool gve_get_reset_in_progress(struct gve_priv *priv)
968 {
969 	return test_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS,
970 			&priv->service_task_flags);
971 }
972 
gve_set_reset_in_progress(struct gve_priv * priv)973 static inline void gve_set_reset_in_progress(struct gve_priv *priv)
974 {
975 	set_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
976 }
977 
gve_clear_reset_in_progress(struct gve_priv * priv)978 static inline void gve_clear_reset_in_progress(struct gve_priv *priv)
979 {
980 	clear_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
981 }
982 
gve_get_probe_in_progress(struct gve_priv * priv)983 static inline bool gve_get_probe_in_progress(struct gve_priv *priv)
984 {
985 	return test_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS,
986 			&priv->service_task_flags);
987 }
988 
gve_set_probe_in_progress(struct gve_priv * priv)989 static inline void gve_set_probe_in_progress(struct gve_priv *priv)
990 {
991 	set_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
992 }
993 
gve_clear_probe_in_progress(struct gve_priv * priv)994 static inline void gve_clear_probe_in_progress(struct gve_priv *priv)
995 {
996 	clear_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
997 }
998 
gve_get_do_report_stats(struct gve_priv * priv)999 static inline bool gve_get_do_report_stats(struct gve_priv *priv)
1000 {
1001 	return test_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS,
1002 			&priv->service_task_flags);
1003 }
1004 
gve_set_do_report_stats(struct gve_priv * priv)1005 static inline void gve_set_do_report_stats(struct gve_priv *priv)
1006 {
1007 	set_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
1008 }
1009 
gve_clear_do_report_stats(struct gve_priv * priv)1010 static inline void gve_clear_do_report_stats(struct gve_priv *priv)
1011 {
1012 	clear_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
1013 }
1014 
gve_get_admin_queue_ok(struct gve_priv * priv)1015 static inline bool gve_get_admin_queue_ok(struct gve_priv *priv)
1016 {
1017 	return test_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
1018 }
1019 
gve_set_admin_queue_ok(struct gve_priv * priv)1020 static inline void gve_set_admin_queue_ok(struct gve_priv *priv)
1021 {
1022 	set_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
1023 }
1024 
gve_clear_admin_queue_ok(struct gve_priv * priv)1025 static inline void gve_clear_admin_queue_ok(struct gve_priv *priv)
1026 {
1027 	clear_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
1028 }
1029 
gve_get_device_resources_ok(struct gve_priv * priv)1030 static inline bool gve_get_device_resources_ok(struct gve_priv *priv)
1031 {
1032 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
1033 }
1034 
gve_set_device_resources_ok(struct gve_priv * priv)1035 static inline void gve_set_device_resources_ok(struct gve_priv *priv)
1036 {
1037 	set_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
1038 }
1039 
gve_clear_device_resources_ok(struct gve_priv * priv)1040 static inline void gve_clear_device_resources_ok(struct gve_priv *priv)
1041 {
1042 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
1043 }
1044 
gve_get_device_rings_ok(struct gve_priv * priv)1045 static inline bool gve_get_device_rings_ok(struct gve_priv *priv)
1046 {
1047 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
1048 }
1049 
gve_set_device_rings_ok(struct gve_priv * priv)1050 static inline void gve_set_device_rings_ok(struct gve_priv *priv)
1051 {
1052 	set_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
1053 }
1054 
gve_clear_device_rings_ok(struct gve_priv * priv)1055 static inline void gve_clear_device_rings_ok(struct gve_priv *priv)
1056 {
1057 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
1058 }
1059 
gve_get_napi_enabled(struct gve_priv * priv)1060 static inline bool gve_get_napi_enabled(struct gve_priv *priv)
1061 {
1062 	return test_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
1063 }
1064 
gve_set_napi_enabled(struct gve_priv * priv)1065 static inline void gve_set_napi_enabled(struct gve_priv *priv)
1066 {
1067 	set_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
1068 }
1069 
gve_clear_napi_enabled(struct gve_priv * priv)1070 static inline void gve_clear_napi_enabled(struct gve_priv *priv)
1071 {
1072 	clear_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
1073 }
1074 
gve_get_report_stats(struct gve_priv * priv)1075 static inline bool gve_get_report_stats(struct gve_priv *priv)
1076 {
1077 	return test_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
1078 }
1079 
gve_clear_report_stats(struct gve_priv * priv)1080 static inline void gve_clear_report_stats(struct gve_priv *priv)
1081 {
1082 	clear_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
1083 }
1084 
1085 /* Returns the address of the ntfy_blocks irq doorbell
1086  */
gve_irq_doorbell(struct gve_priv * priv,struct gve_notify_block * block)1087 static inline __be32 __iomem *gve_irq_doorbell(struct gve_priv *priv,
1088 					       struct gve_notify_block *block)
1089 {
1090 	return &priv->db_bar2[be32_to_cpu(*block->irq_db_index)];
1091 }
1092 
1093 /* Returns the index into ntfy_blocks of the given tx ring's block
1094  */
gve_tx_idx_to_ntfy(struct gve_priv * priv,u32 queue_idx)1095 static inline u32 gve_tx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
1096 {
1097 	return queue_idx;
1098 }
1099 
1100 /* Returns the index into ntfy_blocks of the given rx ring's block
1101  */
gve_rx_idx_to_ntfy(struct gve_priv * priv,u32 queue_idx)1102 static inline u32 gve_rx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
1103 {
1104 	return (priv->num_ntfy_blks / 2) + queue_idx;
1105 }
1106 
gve_is_qpl(struct gve_priv * priv)1107 static inline bool gve_is_qpl(struct gve_priv *priv)
1108 {
1109 	return priv->queue_format == GVE_GQI_QPL_FORMAT ||
1110 		priv->queue_format == GVE_DQO_QPL_FORMAT;
1111 }
1112 
1113 /* Returns the number of tx queue page lists */
gve_num_tx_qpls(const struct gve_tx_queue_config * tx_cfg,bool is_qpl)1114 static inline u32 gve_num_tx_qpls(const struct gve_tx_queue_config *tx_cfg,
1115 				  bool is_qpl)
1116 {
1117 	if (!is_qpl)
1118 		return 0;
1119 	return tx_cfg->num_queues + tx_cfg->num_xdp_queues;
1120 }
1121 
1122 /* Returns the number of rx queue page lists */
gve_num_rx_qpls(const struct gve_rx_queue_config * rx_cfg,bool is_qpl)1123 static inline u32 gve_num_rx_qpls(const struct gve_rx_queue_config *rx_cfg,
1124 				  bool is_qpl)
1125 {
1126 	if (!is_qpl)
1127 		return 0;
1128 	return rx_cfg->num_queues;
1129 }
1130 
gve_tx_qpl_id(struct gve_priv * priv,int tx_qid)1131 static inline u32 gve_tx_qpl_id(struct gve_priv *priv, int tx_qid)
1132 {
1133 	return tx_qid;
1134 }
1135 
gve_rx_qpl_id(struct gve_priv * priv,int rx_qid)1136 static inline u32 gve_rx_qpl_id(struct gve_priv *priv, int rx_qid)
1137 {
1138 	return priv->tx_cfg.max_queues + rx_qid;
1139 }
1140 
gve_get_rx_qpl_id(const struct gve_tx_queue_config * tx_cfg,int rx_qid)1141 static inline u32 gve_get_rx_qpl_id(const struct gve_tx_queue_config *tx_cfg,
1142 				    int rx_qid)
1143 {
1144 	return tx_cfg->max_queues + rx_qid;
1145 }
1146 
gve_tx_start_qpl_id(struct gve_priv * priv)1147 static inline u32 gve_tx_start_qpl_id(struct gve_priv *priv)
1148 {
1149 	return gve_tx_qpl_id(priv, 0);
1150 }
1151 
gve_rx_start_qpl_id(const struct gve_tx_queue_config * tx_cfg)1152 static inline u32 gve_rx_start_qpl_id(const struct gve_tx_queue_config *tx_cfg)
1153 {
1154 	return gve_get_rx_qpl_id(tx_cfg, 0);
1155 }
1156 
1157 /* Returns the correct dma direction for tx and rx qpls */
gve_qpl_dma_dir(struct gve_priv * priv,int id)1158 static inline enum dma_data_direction gve_qpl_dma_dir(struct gve_priv *priv,
1159 						      int id)
1160 {
1161 	if (id < gve_rx_start_qpl_id(&priv->tx_cfg))
1162 		return DMA_TO_DEVICE;
1163 	else
1164 		return DMA_FROM_DEVICE;
1165 }
1166 
gve_is_gqi(struct gve_priv * priv)1167 static inline bool gve_is_gqi(struct gve_priv *priv)
1168 {
1169 	return priv->queue_format == GVE_GQI_RDA_FORMAT ||
1170 		priv->queue_format == GVE_GQI_QPL_FORMAT;
1171 }
1172 
gve_is_dqo(struct gve_priv * priv)1173 static inline bool gve_is_dqo(struct gve_priv *priv)
1174 {
1175 	return priv->queue_format == GVE_DQO_RDA_FORMAT ||
1176 	       priv->queue_format == GVE_DQO_QPL_FORMAT;
1177 }
1178 
gve_num_tx_queues(struct gve_priv * priv)1179 static inline u32 gve_num_tx_queues(struct gve_priv *priv)
1180 {
1181 	return priv->tx_cfg.num_queues + priv->tx_cfg.num_xdp_queues;
1182 }
1183 
gve_xdp_tx_queue_id(struct gve_priv * priv,u32 queue_id)1184 static inline u32 gve_xdp_tx_queue_id(struct gve_priv *priv, u32 queue_id)
1185 {
1186 	return priv->tx_cfg.num_queues + queue_id;
1187 }
1188 
gve_xdp_tx_start_queue_id(struct gve_priv * priv)1189 static inline u32 gve_xdp_tx_start_queue_id(struct gve_priv *priv)
1190 {
1191 	return gve_xdp_tx_queue_id(priv, 0);
1192 }
1193 
gve_supports_xdp_xmit(struct gve_priv * priv)1194 static inline bool gve_supports_xdp_xmit(struct gve_priv *priv)
1195 {
1196 	switch (priv->queue_format) {
1197 	case GVE_GQI_QPL_FORMAT:
1198 	case GVE_DQO_RDA_FORMAT:
1199 		return true;
1200 	default:
1201 		return false;
1202 	}
1203 }
1204 
gve_is_clock_enabled(struct gve_priv * priv)1205 static inline bool gve_is_clock_enabled(struct gve_priv *priv)
1206 {
1207 	return priv->nic_ts_report;
1208 }
1209 
1210 /* gqi napi handler defined in gve_main.c */
1211 int gve_napi_poll(struct napi_struct *napi, int budget);
1212 
1213 /* buffers */
1214 int gve_alloc_page(struct gve_priv *priv, struct device *dev,
1215 		   struct page **page, dma_addr_t *dma,
1216 		   enum dma_data_direction, gfp_t gfp_flags);
1217 void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
1218 		   enum dma_data_direction);
1219 /* qpls */
1220 struct gve_queue_page_list *gve_alloc_queue_page_list(struct gve_priv *priv,
1221 						      u32 id, int pages);
1222 void gve_free_queue_page_list(struct gve_priv *priv,
1223 			      struct gve_queue_page_list *qpl,
1224 			      u32 id);
1225 /* tx handling */
1226 netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev);
1227 int gve_xdp_xmit_gqi(struct net_device *dev, int n, struct xdp_frame **frames,
1228 		     u32 flags);
1229 int gve_xdp_xmit_dqo(struct net_device *dev, int n, struct xdp_frame **frames,
1230 		     u32 flags);
1231 int gve_xdp_xmit_one(struct gve_priv *priv, struct gve_tx_ring *tx,
1232 		     void *data, int len, void *frame_p);
1233 void gve_xdp_tx_flush(struct gve_priv *priv, u32 xdp_qid);
1234 int gve_xdp_xmit_one_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1235 			 struct xdp_frame *xdpf);
1236 bool gve_tx_poll(struct gve_notify_block *block, int budget);
1237 bool gve_xdp_poll(struct gve_notify_block *block, int budget);
1238 int gve_xsk_tx_poll(struct gve_notify_block *block, int budget);
1239 int gve_tx_alloc_rings_gqi(struct gve_priv *priv,
1240 			   struct gve_tx_alloc_rings_cfg *cfg);
1241 void gve_tx_free_rings_gqi(struct gve_priv *priv,
1242 			   struct gve_tx_alloc_rings_cfg *cfg);
1243 void gve_tx_start_ring_gqi(struct gve_priv *priv, int idx);
1244 void gve_tx_stop_ring_gqi(struct gve_priv *priv, int idx);
1245 u32 gve_tx_load_event_counter(struct gve_priv *priv,
1246 			      struct gve_tx_ring *tx);
1247 bool gve_tx_clean_pending(struct gve_priv *priv, struct gve_tx_ring *tx);
1248 /* rx handling */
1249 void gve_rx_write_doorbell(struct gve_priv *priv, struct gve_rx_ring *rx);
1250 int gve_rx_poll(struct gve_notify_block *block, int budget);
1251 bool gve_rx_work_pending(struct gve_rx_ring *rx);
1252 int gve_rx_alloc_ring_gqi(struct gve_priv *priv,
1253 			  struct gve_rx_alloc_rings_cfg *cfg,
1254 			  struct gve_rx_ring *rx,
1255 			  int idx);
1256 void gve_rx_free_ring_gqi(struct gve_priv *priv, struct gve_rx_ring *rx,
1257 			  struct gve_rx_alloc_rings_cfg *cfg);
1258 int gve_rx_alloc_rings_gqi(struct gve_priv *priv,
1259 			   struct gve_rx_alloc_rings_cfg *cfg);
1260 void gve_rx_free_rings_gqi(struct gve_priv *priv,
1261 			   struct gve_rx_alloc_rings_cfg *cfg);
1262 void gve_rx_start_ring_gqi(struct gve_priv *priv, int idx);
1263 void gve_rx_stop_ring_gqi(struct gve_priv *priv, int idx);
1264 bool gve_header_split_supported(const struct gve_priv *priv);
1265 int gve_set_rx_buf_len_config(struct gve_priv *priv, u32 rx_buf_len,
1266 			      struct netlink_ext_ack *extack,
1267 			      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1268 int gve_set_hsplit_config(struct gve_priv *priv, u8 tcp_data_split,
1269 			  struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1270 /* rx buffer handling */
1271 int gve_buf_ref_cnt(struct gve_rx_buf_state_dqo *bs);
1272 void gve_free_page_dqo(struct gve_priv *priv, struct gve_rx_buf_state_dqo *bs,
1273 		       bool free_page);
1274 struct gve_rx_buf_state_dqo *gve_alloc_buf_state(struct gve_rx_ring *rx);
1275 bool gve_buf_state_is_allocated(struct gve_rx_ring *rx,
1276 				struct gve_rx_buf_state_dqo *buf_state);
1277 void gve_free_buf_state(struct gve_rx_ring *rx,
1278 			struct gve_rx_buf_state_dqo *buf_state);
1279 struct gve_rx_buf_state_dqo *gve_dequeue_buf_state(struct gve_rx_ring *rx,
1280 						   struct gve_index_list *list);
1281 void gve_enqueue_buf_state(struct gve_rx_ring *rx, struct gve_index_list *list,
1282 			   struct gve_rx_buf_state_dqo *buf_state);
1283 struct gve_rx_buf_state_dqo *gve_get_recycled_buf_state(struct gve_rx_ring *rx);
1284 void gve_try_recycle_buf(struct gve_priv *priv, struct gve_rx_ring *rx,
1285 			 struct gve_rx_buf_state_dqo *buf_state);
1286 void gve_free_to_page_pool(struct gve_rx_ring *rx,
1287 			   struct gve_rx_buf_state_dqo *buf_state,
1288 			   bool allow_direct);
1289 int gve_alloc_qpl_page_dqo(struct gve_rx_ring *rx,
1290 			   struct gve_rx_buf_state_dqo *buf_state);
1291 void gve_free_qpl_page_dqo(struct gve_rx_buf_state_dqo *buf_state);
1292 void gve_reuse_buffer(struct gve_rx_ring *rx,
1293 		      struct gve_rx_buf_state_dqo *buf_state);
1294 void gve_free_buffer(struct gve_rx_ring *rx,
1295 		     struct gve_rx_buf_state_dqo *buf_state);
1296 int gve_alloc_buffer(struct gve_rx_ring *rx, struct gve_rx_desc_dqo *desc);
1297 struct page_pool *gve_rx_create_page_pool(struct gve_priv *priv,
1298 					  struct gve_rx_ring *rx,
1299 					  bool xdp);
1300 
1301 /* Reset */
1302 void gve_schedule_reset(struct gve_priv *priv);
1303 int gve_reset(struct gve_priv *priv, bool attempt_teardown);
1304 void gve_get_curr_alloc_cfgs(struct gve_priv *priv,
1305 			     struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1306 			     struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1307 void gve_update_num_qpl_pages(struct gve_priv *priv,
1308 			      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg,
1309 			      struct gve_tx_alloc_rings_cfg *tx_alloc_cfg);
1310 int gve_adjust_config(struct gve_priv *priv,
1311 		      struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1312 		      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg);
1313 int gve_adjust_queues(struct gve_priv *priv,
1314 		      struct gve_rx_queue_config new_rx_config,
1315 		      struct gve_tx_queue_config new_tx_config,
1316 		      bool reset_rss);
1317 /* flow steering rule */
1318 int gve_get_flow_rule_entry(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1319 int gve_get_flow_rule_ids(struct gve_priv *priv, struct ethtool_rxnfc *cmd, u32 *rule_locs);
1320 int gve_add_flow_rule(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1321 int gve_del_flow_rule(struct gve_priv *priv, struct ethtool_rxnfc *cmd);
1322 int gve_flow_rules_reset(struct gve_priv *priv);
1323 /* RSS config */
1324 int gve_init_rss_config(struct gve_priv *priv, u16 num_queues);
1325 /* PTP and timestamping */
1326 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
1327 int gve_clock_nic_ts_read(struct gve_priv *priv);
1328 int gve_init_clock(struct gve_priv *priv);
1329 void gve_teardown_clock(struct gve_priv *priv);
1330 #else /* CONFIG_PTP_1588_CLOCK */
gve_clock_nic_ts_read(struct gve_priv * priv)1331 static inline int gve_clock_nic_ts_read(struct gve_priv *priv)
1332 {
1333 	return -EOPNOTSUPP;
1334 }
1335 
gve_init_clock(struct gve_priv * priv)1336 static inline int gve_init_clock(struct gve_priv *priv)
1337 {
1338 	return 0;
1339 }
1340 
gve_teardown_clock(struct gve_priv * priv)1341 static inline void gve_teardown_clock(struct gve_priv *priv) { }
1342 #endif /* CONFIG_PTP_1588_CLOCK */
1343 /* report stats handling */
1344 void gve_handle_report_stats(struct gve_priv *priv);
1345 /* exported by ethtool.c */
1346 extern const struct ethtool_ops gve_ethtool_ops;
1347 /* needed by ethtool */
1348 extern char gve_driver_name[];
1349 extern const char gve_version_str[];
1350 #endif /* _GVE_H_ */
1351