1 /* SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 * Google virtual Ethernet (gve) driver
4 * Copyright (C) 2015-2021 Google, Inc.
10 #include <linux/dma-mapping.h>
11 #include <linux/netdevice.h>
12 #include <linux/pci.h>
13 #include <linux/u64_stats_sync.h>
17 #include "gve_desc_dqo.h"
19 #ifndef PCI_VENDOR_ID_GOOGLE
20 #define PCI_VENDOR_ID_GOOGLE 0x1ae0
23 #define PCI_DEV_ID_GVNIC 0x0042
25 #define GVE_REGISTER_BAR 0
26 #define GVE_DOORBELL_BAR 2
28 /* Driver can alloc up to 2 segments for the header and 2 for the payload. */
29 #define GVE_TX_MAX_IOVEC 4
30 /* 1 for management, 1 for rx, 1 for tx */
31 #define GVE_MIN_MSIX 3
33 /* Numbers of gve tx/rx stats in stats report. */
34 #define GVE_TX_STATS_REPORT_NUM 6
35 #define GVE_RX_STATS_REPORT_NUM 2
37 /* Interval to schedule a stats report update, 20000ms. */
38 #define GVE_STATS_REPORT_TIMER_PERIOD 20000
40 /* Numbers of NIC tx/rx stats in stats report. */
41 #define NIC_TX_STATS_REPORT_NUM 0
42 #define NIC_RX_STATS_REPORT_NUM 4
44 #define GVE_DATA_SLOT_ADDR_PAGE_MASK (~(PAGE_SIZE - 1))
46 /* PTYPEs are always 10 bits. */
47 #define GVE_NUM_PTYPES 1024
49 #define GVE_RX_BUFFER_SIZE_DQO 2048
51 #define GVE_XDP_ACTIONS 5
53 #define GVE_GQ_TX_MIN_PKT_DESC_BYTES 182
55 #define DQO_QPL_DEFAULT_TX_PAGES 512
56 #define DQO_QPL_DEFAULT_RX_PAGES 2048
58 /* Maximum TSO size supported on DQO */
59 #define GVE_DQO_TX_MAX 0x3FFFF
61 #define GVE_TX_BUF_SHIFT_DQO 11
63 /* 2K buffers for DQO-QPL */
64 #define GVE_TX_BUF_SIZE_DQO BIT(GVE_TX_BUF_SHIFT_DQO)
65 #define GVE_TX_BUFS_PER_PAGE_DQO (PAGE_SIZE >> GVE_TX_BUF_SHIFT_DQO)
66 #define GVE_MAX_TX_BUFS_PER_PKT (DIV_ROUND_UP(GVE_DQO_TX_MAX, GVE_TX_BUF_SIZE_DQO))
68 /* If number of free/recyclable buffers are less than this threshold; driver
69 * allocs and uses a non-qpl page on the receive path of DQO QPL to free
71 * Value is set big enough to post at least 3 64K LRO packet via 2K buffer to NIC.
73 #define GVE_DQO_QPL_ONDEMAND_ALLOC_THRESHOLD 96
75 /* Each slot in the desc ring has a 1:1 mapping to a slot in the data ring */
76 struct gve_rx_desc_queue {
77 struct gve_rx_desc *desc_ring; /* the descriptor ring */
78 dma_addr_t bus; /* the bus for the desc_ring */
79 u8 seqno; /* the next expected seqno for this desc*/
82 /* The page info for a single slot in the RX data queue */
83 struct gve_rx_slot_page_info {
86 u32 page_offset; /* offset to write to in page */
87 int pagecnt_bias; /* expected pagecnt if only the driver has a ref */
88 u16 pad; /* adjustment for rx padding */
89 u8 can_flip; /* tracks if the networking stack is using the page */
92 /* A list of pages registered with the device during setup and used by a queue
95 struct gve_queue_page_list {
96 u32 id; /* unique id */
98 struct page **pages; /* list of num_entries pages */
99 dma_addr_t *page_buses; /* the dma addrs of the pages */
102 /* Each slot in the data ring has a 1:1 mapping to a slot in the desc ring */
103 struct gve_rx_data_queue {
104 union gve_rx_data_slot *data_ring; /* read by NIC */
105 dma_addr_t data_bus; /* dma mapping of the slots */
106 struct gve_rx_slot_page_info *page_info; /* page info of the buffers */
107 struct gve_queue_page_list *qpl; /* qpl assigned to this queue */
108 u8 raw_addressing; /* use raw_addressing? */
113 /* RX buffer queue for posting buffers to HW.
114 * Each RX (completion) queue has a corresponding buffer queue.
116 struct gve_rx_buf_queue_dqo {
117 struct gve_rx_desc_dqo *desc_ring;
119 u32 head; /* Pointer to start cleaning buffers at. */
120 u32 tail; /* Last posted buffer index + 1 */
121 u32 mask; /* Mask for indices to the size of the ring */
124 /* RX completion queue to receive packets from HW. */
125 struct gve_rx_compl_queue_dqo {
126 struct gve_rx_compl_desc_dqo *desc_ring;
129 /* Number of slots which did not have a buffer posted yet. We should not
130 * post more buffers than the queue size to avoid HW overrunning the
135 /* HW uses a "generation bit" to notify SW of new descriptors. When a
136 * descriptor's generation bit is different from the current generation,
137 * that descriptor is ready to be consumed by SW.
141 /* Pointer into desc_ring where the next completion descriptor will be
145 u32 mask; /* Mask for indices to the size of the ring */
148 /* Stores state for tracking buffers posted to HW */
149 struct gve_rx_buf_state_dqo {
150 /* The page posted to HW. */
151 struct gve_rx_slot_page_info page_info;
153 /* The DMA address corresponding to `page_info`. */
156 /* Last offset into the page when it only had a single reference, at
157 * which point every other offset is free to be reused.
159 u32 last_single_ref_offset;
161 /* Linked list index to next element in the list, or -1 if none */
165 /* `head` and `tail` are indices into an array, or -1 if empty. */
166 struct gve_index_list {
171 /* A single received packet split across multiple buffers may be
172 * reconstructed using the information in this structure.
175 /* head and tail of skb chain for the current packet or NULL if none */
176 struct sk_buff *skb_head;
177 struct sk_buff *skb_tail;
188 u16 desc_err_pkt_cnt;
191 /* Contains datapath state used to represent an RX queue. */
193 struct gve_priv *gve;
197 struct gve_rx_desc_queue desc;
198 struct gve_rx_data_queue data;
200 /* threshold for posting new buffs and descs */
202 u16 packet_buffer_size;
204 u32 qpl_copy_pool_mask;
205 u32 qpl_copy_pool_head;
206 struct gve_rx_slot_page_info *qpl_copy_pool;
211 struct gve_rx_buf_queue_dqo bufq;
212 struct gve_rx_compl_queue_dqo complq;
214 struct gve_rx_buf_state_dqo *buf_states;
217 /* Linked list of gve_rx_buf_state_dqo. Index into
218 * buf_states, or -1 if empty.
222 /* Linked list of gve_rx_buf_state_dqo. Indexes into
223 * buf_states, or -1 if empty.
225 * This list contains buf_states which are pointing to
228 * We use a FIFO here in order to increase the
229 * probability that buffers can be reused by increasing
230 * the time between usages.
232 struct gve_index_list recycled_buf_states;
234 /* Linked list of gve_rx_buf_state_dqo. Indexes into
235 * buf_states, or -1 if empty.
237 * This list contains buf_states which have buffers
238 * which cannot be reused yet.
240 struct gve_index_list used_buf_states;
242 /* qpl assigned to this queue */
243 struct gve_queue_page_list *qpl;
245 /* index into queue page list */
246 u32 next_qpl_page_idx;
248 /* track number of used buffers */
249 u16 used_buf_states_cnt;
253 u64 rbytes; /* free-running bytes received */
254 u64 rpackets; /* free-running packets received */
255 u32 cnt; /* free-running total number of completed packets */
256 u32 fill_cnt; /* free-running total number of descs and buffs posted */
257 u32 mask; /* masks the cnt and fill_cnt to the size of the ring */
258 u64 rx_copybreak_pkt; /* free-running count of copybreak packets */
259 u64 rx_copied_pkt; /* free-running total number of copied packets */
260 u64 rx_skb_alloc_fail; /* free-running count of skb alloc fails */
261 u64 rx_buf_alloc_fail; /* free-running count of buffer alloc fails */
262 u64 rx_desc_err_dropped_pkt; /* free-running count of packets dropped by descriptor error */
263 u64 rx_cont_packet_cnt; /* free-running multi-fragment packets received */
264 u64 rx_frag_flip_cnt; /* free-running count of rx segments where page_flip was used */
265 u64 rx_frag_copy_cnt; /* free-running count of rx segments copied */
266 u64 rx_frag_alloc_cnt; /* free-running count of rx page allocations */
268 u64 xdp_redirect_errors;
270 u64 xdp_actions[GVE_XDP_ACTIONS];
271 u32 q_num; /* queue index */
272 u32 ntfy_id; /* notification block index */
273 struct gve_queue_resources *q_resources; /* head and tail pointer idx */
274 dma_addr_t q_resources_bus; /* dma address for the queue resources */
275 struct u64_stats_sync statss; /* sync stats for 32bit archs */
277 struct gve_rx_ctx ctx; /* Info for packet currently being processed in this ring. */
280 struct xdp_rxq_info xdp_rxq;
281 struct xdp_rxq_info xsk_rxq;
282 struct xsk_buff_pool *xsk_pool;
283 struct page_frag_cache page_cache; /* Page cache to allocate XDP frames */
286 /* A TX desc ring entry */
288 struct gve_tx_pkt_desc pkt; /* first desc for a packet */
289 struct gve_tx_mtd_desc mtd; /* optional metadata descriptor */
290 struct gve_tx_seg_desc seg; /* subsequent descs for a packet */
293 /* Tracks the memory in the fifo occupied by a segment of a packet */
294 struct gve_tx_iovec {
295 u32 iov_offset; /* offset into this segment */
296 u32 iov_len; /* length */
297 u32 iov_padding; /* padding associated with this segment */
300 /* Tracks the memory in the fifo occupied by the skb. Mapped 1:1 to a desc
301 * ring entry but only used for a pkt_desc not a seg_desc
303 struct gve_tx_buffer_state {
305 struct sk_buff *skb; /* skb for this pkt */
306 struct xdp_frame *xdp_frame; /* xdp_frame */
309 u16 size; /* size of xmitted xdp pkt */
310 u8 is_xsk; /* xsk buff */
313 struct gve_tx_iovec iov[GVE_TX_MAX_IOVEC]; /* segments of this pkt */
315 DEFINE_DMA_UNMAP_ADDR(dma);
316 DEFINE_DMA_UNMAP_LEN(len);
321 /* A TX buffer - each queue has one */
323 void *base; /* address of base of FIFO */
324 u32 size; /* total size */
325 atomic_t available; /* how much space is still available */
326 u32 head; /* offset to write at */
327 struct gve_queue_page_list *qpl; /* QPL mapped into this FIFO */
330 /* TX descriptor for DQO format */
331 union gve_tx_desc_dqo {
332 struct gve_tx_pkt_desc_dqo pkt;
333 struct gve_tx_tso_context_desc_dqo tso_ctx;
334 struct gve_tx_general_context_desc_dqo general_ctx;
337 enum gve_packet_state {
338 /* Packet is in free list, available to be allocated.
339 * This should always be zero since state is not explicitly initialized.
341 GVE_PACKET_STATE_UNALLOCATED,
342 /* Packet is expecting a regular data completion or miss completion */
343 GVE_PACKET_STATE_PENDING_DATA_COMPL,
344 /* Packet has received a miss completion and is expecting a
345 * re-injection completion.
347 GVE_PACKET_STATE_PENDING_REINJECT_COMPL,
348 /* No valid completion received within the specified timeout. */
349 GVE_PACKET_STATE_TIMED_OUT_COMPL,
352 struct gve_tx_pending_packet_dqo {
353 struct sk_buff *skb; /* skb for this packet */
355 /* 0th element corresponds to the linear portion of `skb`, should be
356 * unmapped with `dma_unmap_single`.
358 * All others correspond to `skb`'s frags and should be unmapped with
363 DEFINE_DMA_UNMAP_ADDR(dma[MAX_SKB_FRAGS + 1]);
364 DEFINE_DMA_UNMAP_LEN(len[MAX_SKB_FRAGS + 1]);
366 s16 tx_qpl_buf_ids[GVE_MAX_TX_BUFS_PER_PKT];
371 /* Linked list index to next element in the list, or -1 if none */
374 /* Linked list index to prev element in the list, or -1 if none.
375 * Used for tracking either outstanding miss completions or prematurely
380 /* Identifies the current state of the packet as defined in
381 * `enum gve_packet_state`.
385 /* If packet is an outstanding miss completion, then the packet is
386 * freed if the corresponding re-injection completion is not received
387 * before kernel jiffies exceeds timeout_jiffies.
389 unsigned long timeout_jiffies;
392 /* Contains datapath state used to represent a TX queue. */
394 /* Cacheline 0 -- Accessed & dirtied during transmit */
398 struct gve_tx_fifo tx_fifo;
399 u32 req; /* driver tracked head pointer */
400 u32 done; /* driver tracked tail pointer */
405 /* Linked list of gve_tx_pending_packet_dqo. Index into
406 * pending_packets, or -1 if empty.
408 * This is a consumer list owned by the TX path. When it
409 * runs out, the producer list is stolen from the
410 * completion handling path
411 * (dqo_compl.free_pending_packets).
413 s16 free_pending_packets;
415 /* Cached value of `dqo_compl.hw_tx_head` */
417 u32 tail; /* Last posted buffer index + 1 */
419 /* Index of the last descriptor with "report event" bit
424 /* free running number of packet buf descriptors posted */
425 u16 posted_packet_desc_cnt;
426 /* free running number of packet buf descriptors completed */
427 u16 completed_packet_desc_cnt;
431 /* Linked list of gve_tx_buf_dqo. Index into
432 * tx_qpl_buf_next, or -1 if empty.
434 * This is a consumer list owned by the TX path. When it
435 * runs out, the producer list is stolen from the
436 * completion handling path
437 * (dqo_compl.free_tx_qpl_buf_head).
439 s16 free_tx_qpl_buf_head;
441 /* Free running count of the number of QPL tx buffers
444 u32 alloc_tx_qpl_buf_cnt;
446 /* Cached value of `dqo_compl.free_tx_qpl_buf_cnt` */
447 u32 free_tx_qpl_buf_cnt;
452 /* Cacheline 1 -- Accessed & dirtied during gve_clean_tx_done */
456 /* Spinlock for when cleanup in progress */
457 spinlock_t clean_lock;
458 /* Spinlock for XDP tx traffic */
464 u32 head; /* Last read on compl_desc */
466 /* Tracks the current gen bit of compl_q */
469 /* Linked list of gve_tx_pending_packet_dqo. Index into
470 * pending_packets, or -1 if empty.
472 * This is the producer list, owned by the completion
473 * handling path. When the consumer list
474 * (dqo_tx.free_pending_packets) is runs out, this list
477 atomic_t free_pending_packets;
479 /* Last TX ring index fetched by HW */
482 /* List to track pending packets which received a miss
483 * completion but not a corresponding reinjection.
485 struct gve_index_list miss_completions;
487 /* List to track pending packets that were completed
488 * before receiving a valid completion because they
489 * reached a specified timeout.
491 struct gve_index_list timed_out_completions;
495 /* Linked list of gve_tx_buf_dqo. Index into
496 * tx_qpl_buf_next, or -1 if empty.
498 * This is the producer list, owned by the completion
499 * handling path. When the consumer list
500 * (dqo_tx.free_tx_qpl_buf_head) is runs out, this list
503 atomic_t free_tx_qpl_buf_head;
505 /* Free running count of the number of tx buffers
508 atomic_t free_tx_qpl_buf_cnt;
511 } ____cacheline_aligned;
512 u64 pkt_done; /* free-running - total packets completed */
513 u64 bytes_done; /* free-running - total bytes completed */
514 u64 dropped_pkt; /* free-running - total packets dropped */
515 u64 dma_mapping_error; /* count of dma mapping errors */
517 /* Cacheline 2 -- Read-mostly fields */
521 union gve_tx_desc *desc;
523 /* Maps 1:1 to a desc */
524 struct gve_tx_buffer_state *info;
529 union gve_tx_desc_dqo *tx_ring;
530 struct gve_tx_compl_desc *compl_ring;
532 struct gve_tx_pending_packet_dqo *pending_packets;
533 s16 num_pending_packets;
535 u32 complq_mask; /* complq size is complq_mask + 1 */
539 /* qpl assigned to this queue */
540 struct gve_queue_page_list *qpl;
542 /* Each QPL page is divided into TX bounce buffers
543 * of size GVE_TX_BUF_SIZE_DQO. tx_qpl_buf_next is
544 * an array to manage linked lists of TX buffers.
545 * An entry j at index i implies that j'th buffer
546 * is next on the list after i
548 s16 *tx_qpl_buf_next;
552 } ____cacheline_aligned;
553 struct netdev_queue *netdev_txq;
554 struct gve_queue_resources *q_resources; /* head and tail pointer idx */
556 u32 mask; /* masks req and done down to queue size */
557 u8 raw_addressing; /* use raw_addressing? */
559 /* Slow-path fields */
560 u32 q_num ____cacheline_aligned; /* queue idx */
561 u32 stop_queue; /* count of queue stops */
562 u32 wake_queue; /* count of queue wakes */
563 u32 queue_timeout; /* count of queue timeouts */
564 u32 ntfy_id; /* notification block index */
565 u32 last_kick_msec; /* Last time the queue was kicked */
566 dma_addr_t bus; /* dma address of the descr ring */
567 dma_addr_t q_resources_bus; /* dma address of the queue resources */
568 dma_addr_t complq_bus_dqo; /* dma address of the dqo.compl_ring */
569 struct u64_stats_sync statss; /* sync stats for 32bit archs */
570 struct xsk_buff_pool *xsk_pool;
576 } ____cacheline_aligned;
578 /* Wraps the info for one irq including the napi struct and the queues
579 * associated with that irq.
581 struct gve_notify_block {
582 __be32 *irq_db_index; /* pointer to idx into Bar2 */
583 char name[IFNAMSIZ + 16]; /* name registered with the kernel */
584 struct napi_struct napi; /* kernel napi struct for this block */
585 struct gve_priv *priv;
586 struct gve_tx_ring *tx; /* tx rings on this block */
587 struct gve_rx_ring *rx; /* rx rings on this block */
590 /* Tracks allowed and current queue settings */
591 struct gve_queue_config {
593 u16 num_queues; /* current */
596 /* Tracks the available and used qpl IDs */
597 struct gve_qpl_config {
598 u32 qpl_map_size; /* map memory size */
599 unsigned long *qpl_id_map; /* bitmap of used qpl ids */
602 struct gve_options_dqo_rda {
603 u16 tx_comp_ring_entries; /* number of tx_comp descriptors */
604 u16 rx_buff_ring_entries; /* number of rx_buff descriptors */
609 } ____cacheline_aligned;
612 u8 l3_type; /* `gve_l3_type` in gve_adminq.h */
613 u8 l4_type; /* `gve_l4_type` in gve_adminq.h */
616 struct gve_ptype_lut {
617 struct gve_ptype ptypes[GVE_NUM_PTYPES];
620 /* GVE_QUEUE_FORMAT_UNSPECIFIED must be zero since 0 is the default value
621 * when the entire configure_device_resources command is zeroed out and the
622 * queue_format is not specified.
624 enum gve_queue_format {
625 GVE_QUEUE_FORMAT_UNSPECIFIED = 0x0,
626 GVE_GQI_RDA_FORMAT = 0x1,
627 GVE_GQI_QPL_FORMAT = 0x2,
628 GVE_DQO_RDA_FORMAT = 0x3,
629 GVE_DQO_QPL_FORMAT = 0x4,
633 struct net_device *dev;
634 struct gve_tx_ring *tx; /* array of tx_cfg.num_queues */
635 struct gve_rx_ring *rx; /* array of rx_cfg.num_queues */
636 struct gve_queue_page_list *qpls; /* array of num qpls */
637 struct gve_notify_block *ntfy_blocks; /* array of num_ntfy_blks */
638 struct gve_irq_db *irq_db_indices; /* array of num_ntfy_blks */
639 dma_addr_t irq_db_indices_bus;
640 struct msix_entry *msix_vectors; /* array of num_ntfy_blks + 1 */
641 char mgmt_msix_name[IFNAMSIZ + 16];
643 __be32 *counter_array; /* array of num_event_counters */
644 dma_addr_t counter_array_bus;
646 u16 num_event_counters;
647 u16 tx_desc_cnt; /* num desc per ring */
648 u16 rx_desc_cnt; /* num desc per ring */
649 u16 tx_pages_per_qpl; /* Suggested number of pages per qpl for TX queues by NIC */
650 u16 rx_pages_per_qpl; /* Suggested number of pages per qpl for RX queues by NIC */
651 u16 rx_data_slot_cnt; /* rx buffer length */
652 u64 max_registered_pages;
653 u64 num_registered_pages; /* num pages registered with NIC */
654 struct bpf_prog *xdp_prog; /* XDP BPF program */
655 u32 rx_copybreak; /* copy packets smaller than this */
656 u16 default_num_queues; /* default num queues to set up */
659 struct gve_queue_config tx_cfg;
660 struct gve_queue_config rx_cfg;
661 struct gve_qpl_config qpl_cfg; /* map used QPL ids */
662 u32 num_ntfy_blks; /* spilt between TX and RX so must be even */
664 struct gve_registers __iomem *reg_bar0; /* see gve_register.h */
665 __be32 __iomem *db_bar2; /* "array" of doorbells */
666 u32 msg_enable; /* level for netif* netdev print macros */
667 struct pci_dev *pdev;
672 /* Admin queue - see gve_adminq.h*/
673 union gve_adminq_command *adminq;
674 dma_addr_t adminq_bus_addr;
675 u32 adminq_mask; /* masks prod_cnt to adminq size */
676 u32 adminq_prod_cnt; /* free-running count of AQ cmds executed */
677 u32 adminq_cmd_fail; /* free-running count of AQ cmds failed */
678 u32 adminq_timeouts; /* free-running count of AQ cmds timeouts */
679 /* free-running count of per AQ cmd executed */
680 u32 adminq_describe_device_cnt;
681 u32 adminq_cfg_device_resources_cnt;
682 u32 adminq_register_page_list_cnt;
683 u32 adminq_unregister_page_list_cnt;
684 u32 adminq_create_tx_queue_cnt;
685 u32 adminq_create_rx_queue_cnt;
686 u32 adminq_destroy_tx_queue_cnt;
687 u32 adminq_destroy_rx_queue_cnt;
688 u32 adminq_dcfg_device_resources_cnt;
689 u32 adminq_set_driver_parameter_cnt;
690 u32 adminq_report_stats_cnt;
691 u32 adminq_report_link_speed_cnt;
692 u32 adminq_get_ptype_map_cnt;
693 u32 adminq_verify_driver_compatibility_cnt;
696 u32 interface_up_cnt; /* count of times interface turned up since last reset */
697 u32 interface_down_cnt; /* count of times interface turned down since last reset */
698 u32 reset_cnt; /* count of reset */
699 u32 page_alloc_fail; /* count of page alloc fails */
700 u32 dma_mapping_error; /* count of dma mapping errors */
701 u32 stats_report_trigger_cnt; /* count of device-requested stats-reports since last reset */
702 u32 suspend_cnt; /* count of times suspended */
703 u32 resume_cnt; /* count of times resumed */
704 struct workqueue_struct *gve_wq;
705 struct work_struct service_task;
706 struct work_struct stats_report_task;
707 unsigned long service_task_flags;
708 unsigned long state_flags;
710 struct gve_stats_report *stats_report;
711 u64 stats_report_len;
712 dma_addr_t stats_report_bus; /* dma address for the stats report */
713 unsigned long ethtool_flags;
715 unsigned long stats_report_timer_period;
716 struct timer_list stats_report_timer;
718 /* Gvnic device link speed from hypervisor. */
720 bool up_before_suspend; /* True if dev was up before suspend */
722 struct gve_options_dqo_rda options_dqo_rda;
723 struct gve_ptype_lut *ptype_lut_dqo;
725 /* Must be a power of two. */
726 int data_buffer_size_dqo;
728 enum gve_queue_format queue_format;
730 /* Interrupt coalescing settings */
731 u32 tx_coalesce_usecs;
732 u32 rx_coalesce_usecs;
735 enum gve_service_task_flags_bit {
736 GVE_PRIV_FLAGS_DO_RESET = 1,
737 GVE_PRIV_FLAGS_RESET_IN_PROGRESS = 2,
738 GVE_PRIV_FLAGS_PROBE_IN_PROGRESS = 3,
739 GVE_PRIV_FLAGS_DO_REPORT_STATS = 4,
742 enum gve_state_flags_bit {
743 GVE_PRIV_FLAGS_ADMIN_QUEUE_OK = 1,
744 GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK = 2,
745 GVE_PRIV_FLAGS_DEVICE_RINGS_OK = 3,
746 GVE_PRIV_FLAGS_NAPI_ENABLED = 4,
749 enum gve_ethtool_flags_bit {
750 GVE_PRIV_FLAGS_REPORT_STATS = 0,
753 static inline bool gve_get_do_reset(struct gve_priv *priv)
755 return test_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
758 static inline void gve_set_do_reset(struct gve_priv *priv)
760 set_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
763 static inline void gve_clear_do_reset(struct gve_priv *priv)
765 clear_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
768 static inline bool gve_get_reset_in_progress(struct gve_priv *priv)
770 return test_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS,
771 &priv->service_task_flags);
774 static inline void gve_set_reset_in_progress(struct gve_priv *priv)
776 set_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
779 static inline void gve_clear_reset_in_progress(struct gve_priv *priv)
781 clear_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
784 static inline bool gve_get_probe_in_progress(struct gve_priv *priv)
786 return test_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS,
787 &priv->service_task_flags);
790 static inline void gve_set_probe_in_progress(struct gve_priv *priv)
792 set_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
795 static inline void gve_clear_probe_in_progress(struct gve_priv *priv)
797 clear_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
800 static inline bool gve_get_do_report_stats(struct gve_priv *priv)
802 return test_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS,
803 &priv->service_task_flags);
806 static inline void gve_set_do_report_stats(struct gve_priv *priv)
808 set_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
811 static inline void gve_clear_do_report_stats(struct gve_priv *priv)
813 clear_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
816 static inline bool gve_get_admin_queue_ok(struct gve_priv *priv)
818 return test_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
821 static inline void gve_set_admin_queue_ok(struct gve_priv *priv)
823 set_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
826 static inline void gve_clear_admin_queue_ok(struct gve_priv *priv)
828 clear_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
831 static inline bool gve_get_device_resources_ok(struct gve_priv *priv)
833 return test_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
836 static inline void gve_set_device_resources_ok(struct gve_priv *priv)
838 set_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
841 static inline void gve_clear_device_resources_ok(struct gve_priv *priv)
843 clear_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
846 static inline bool gve_get_device_rings_ok(struct gve_priv *priv)
848 return test_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
851 static inline void gve_set_device_rings_ok(struct gve_priv *priv)
853 set_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
856 static inline void gve_clear_device_rings_ok(struct gve_priv *priv)
858 clear_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
861 static inline bool gve_get_napi_enabled(struct gve_priv *priv)
863 return test_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
866 static inline void gve_set_napi_enabled(struct gve_priv *priv)
868 set_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
871 static inline void gve_clear_napi_enabled(struct gve_priv *priv)
873 clear_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
876 static inline bool gve_get_report_stats(struct gve_priv *priv)
878 return test_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
881 static inline void gve_clear_report_stats(struct gve_priv *priv)
883 clear_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
886 /* Returns the address of the ntfy_blocks irq doorbell
888 static inline __be32 __iomem *gve_irq_doorbell(struct gve_priv *priv,
889 struct gve_notify_block *block)
891 return &priv->db_bar2[be32_to_cpu(*block->irq_db_index)];
894 /* Returns the index into ntfy_blocks of the given tx ring's block
896 static inline u32 gve_tx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
901 /* Returns the index into ntfy_blocks of the given rx ring's block
903 static inline u32 gve_rx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
905 return (priv->num_ntfy_blks / 2) + queue_idx;
908 static inline bool gve_is_qpl(struct gve_priv *priv)
910 return priv->queue_format == GVE_GQI_QPL_FORMAT ||
911 priv->queue_format == GVE_DQO_QPL_FORMAT;
914 /* Returns the number of tx queue page lists
916 static inline u32 gve_num_tx_qpls(struct gve_priv *priv)
918 if (!gve_is_qpl(priv))
921 return priv->tx_cfg.num_queues + priv->num_xdp_queues;
924 /* Returns the number of XDP tx queue page lists
926 static inline u32 gve_num_xdp_qpls(struct gve_priv *priv)
928 if (priv->queue_format != GVE_GQI_QPL_FORMAT)
931 return priv->num_xdp_queues;
934 /* Returns the number of rx queue page lists
936 static inline u32 gve_num_rx_qpls(struct gve_priv *priv)
938 if (!gve_is_qpl(priv))
941 return priv->rx_cfg.num_queues;
944 static inline u32 gve_tx_qpl_id(struct gve_priv *priv, int tx_qid)
949 static inline u32 gve_rx_qpl_id(struct gve_priv *priv, int rx_qid)
951 return priv->tx_cfg.max_queues + rx_qid;
954 static inline u32 gve_tx_start_qpl_id(struct gve_priv *priv)
956 return gve_tx_qpl_id(priv, 0);
959 static inline u32 gve_rx_start_qpl_id(struct gve_priv *priv)
961 return gve_rx_qpl_id(priv, 0);
964 /* Returns a pointer to the next available tx qpl in the list of qpls
967 struct gve_queue_page_list *gve_assign_tx_qpl(struct gve_priv *priv, int tx_qid)
969 int id = gve_tx_qpl_id(priv, tx_qid);
971 /* QPL already in use */
972 if (test_bit(id, priv->qpl_cfg.qpl_id_map))
975 set_bit(id, priv->qpl_cfg.qpl_id_map);
976 return &priv->qpls[id];
979 /* Returns a pointer to the next available rx qpl in the list of qpls
982 struct gve_queue_page_list *gve_assign_rx_qpl(struct gve_priv *priv, int rx_qid)
984 int id = gve_rx_qpl_id(priv, rx_qid);
986 /* QPL already in use */
987 if (test_bit(id, priv->qpl_cfg.qpl_id_map))
990 set_bit(id, priv->qpl_cfg.qpl_id_map);
991 return &priv->qpls[id];
994 /* Unassigns the qpl with the given id
996 static inline void gve_unassign_qpl(struct gve_priv *priv, int id)
998 clear_bit(id, priv->qpl_cfg.qpl_id_map);
1001 /* Returns the correct dma direction for tx and rx qpls
1003 static inline enum dma_data_direction gve_qpl_dma_dir(struct gve_priv *priv,
1006 if (id < gve_rx_start_qpl_id(priv))
1007 return DMA_TO_DEVICE;
1009 return DMA_FROM_DEVICE;
1012 static inline bool gve_is_gqi(struct gve_priv *priv)
1014 return priv->queue_format == GVE_GQI_RDA_FORMAT ||
1015 priv->queue_format == GVE_GQI_QPL_FORMAT;
1018 static inline u32 gve_num_tx_queues(struct gve_priv *priv)
1020 return priv->tx_cfg.num_queues + priv->num_xdp_queues;
1023 static inline u32 gve_xdp_tx_queue_id(struct gve_priv *priv, u32 queue_id)
1025 return priv->tx_cfg.num_queues + queue_id;
1028 static inline u32 gve_xdp_tx_start_queue_id(struct gve_priv *priv)
1030 return gve_xdp_tx_queue_id(priv, 0);
1034 int gve_alloc_page(struct gve_priv *priv, struct device *dev,
1035 struct page **page, dma_addr_t *dma,
1036 enum dma_data_direction, gfp_t gfp_flags);
1037 void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
1038 enum dma_data_direction);
1040 netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev);
1041 int gve_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
1043 int gve_xdp_xmit_one(struct gve_priv *priv, struct gve_tx_ring *tx,
1044 void *data, int len, void *frame_p);
1045 void gve_xdp_tx_flush(struct gve_priv *priv, u32 xdp_qid);
1046 bool gve_tx_poll(struct gve_notify_block *block, int budget);
1047 bool gve_xdp_poll(struct gve_notify_block *block, int budget);
1048 int gve_tx_alloc_rings(struct gve_priv *priv, int start_id, int num_rings);
1049 void gve_tx_free_rings_gqi(struct gve_priv *priv, int start_id, int num_rings);
1050 u32 gve_tx_load_event_counter(struct gve_priv *priv,
1051 struct gve_tx_ring *tx);
1052 bool gve_tx_clean_pending(struct gve_priv *priv, struct gve_tx_ring *tx);
1054 void gve_rx_write_doorbell(struct gve_priv *priv, struct gve_rx_ring *rx);
1055 int gve_rx_poll(struct gve_notify_block *block, int budget);
1056 bool gve_rx_work_pending(struct gve_rx_ring *rx);
1057 int gve_rx_alloc_rings(struct gve_priv *priv);
1058 void gve_rx_free_rings_gqi(struct gve_priv *priv);
1060 void gve_schedule_reset(struct gve_priv *priv);
1061 int gve_reset(struct gve_priv *priv, bool attempt_teardown);
1062 int gve_adjust_queues(struct gve_priv *priv,
1063 struct gve_queue_config new_rx_config,
1064 struct gve_queue_config new_tx_config);
1065 /* report stats handling */
1066 void gve_handle_report_stats(struct gve_priv *priv);
1067 /* exported by ethtool.c */
1068 extern const struct ethtool_ops gve_ethtool_ops;
1069 /* needed by ethtool */
1070 extern char gve_driver_name[];
1071 extern const char gve_version_str[];
1072 #endif /* _GVE_H_ */