Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (GPL-2.0 OR MIT)
0002  * Google virtual Ethernet (gve) driver
0003  *
0004  * Copyright (C) 2015-2021 Google, Inc.
0005  */
0006 
0007 #ifndef _GVE_H_
0008 #define _GVE_H_
0009 
0010 #include <linux/dma-mapping.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/pci.h>
0013 #include <linux/u64_stats_sync.h>
0014 
0015 #include "gve_desc.h"
0016 #include "gve_desc_dqo.h"
0017 
0018 #ifndef PCI_VENDOR_ID_GOOGLE
0019 #define PCI_VENDOR_ID_GOOGLE    0x1ae0
0020 #endif
0021 
0022 #define PCI_DEV_ID_GVNIC    0x0042
0023 
0024 #define GVE_REGISTER_BAR    0
0025 #define GVE_DOORBELL_BAR    2
0026 
0027 /* Driver can alloc up to 2 segments for the header and 2 for the payload. */
0028 #define GVE_TX_MAX_IOVEC    4
0029 /* 1 for management, 1 for rx, 1 for tx */
0030 #define GVE_MIN_MSIX 3
0031 
0032 /* Numbers of gve tx/rx stats in stats report. */
0033 #define GVE_TX_STATS_REPORT_NUM 6
0034 #define GVE_RX_STATS_REPORT_NUM 2
0035 
0036 /* Interval to schedule a stats report update, 20000ms. */
0037 #define GVE_STATS_REPORT_TIMER_PERIOD   20000
0038 
0039 /* Numbers of NIC tx/rx stats in stats report. */
0040 #define NIC_TX_STATS_REPORT_NUM 0
0041 #define NIC_RX_STATS_REPORT_NUM 4
0042 
0043 #define GVE_DATA_SLOT_ADDR_PAGE_MASK (~(PAGE_SIZE - 1))
0044 
0045 /* PTYPEs are always 10 bits. */
0046 #define GVE_NUM_PTYPES  1024
0047 
0048 #define GVE_RX_BUFFER_SIZE_DQO 2048
0049 
0050 /* Each slot in the desc ring has a 1:1 mapping to a slot in the data ring */
0051 struct gve_rx_desc_queue {
0052     struct gve_rx_desc *desc_ring; /* the descriptor ring */
0053     dma_addr_t bus; /* the bus for the desc_ring */
0054     u8 seqno; /* the next expected seqno for this desc*/
0055 };
0056 
0057 /* The page info for a single slot in the RX data queue */
0058 struct gve_rx_slot_page_info {
0059     struct page *page;
0060     void *page_address;
0061     u32 page_offset; /* offset to write to in page */
0062     int pagecnt_bias; /* expected pagecnt if only the driver has a ref */
0063     u8 can_flip;
0064 };
0065 
0066 /* A list of pages registered with the device during setup and used by a queue
0067  * as buffers
0068  */
0069 struct gve_queue_page_list {
0070     u32 id; /* unique id */
0071     u32 num_entries;
0072     struct page **pages; /* list of num_entries pages */
0073     dma_addr_t *page_buses; /* the dma addrs of the pages */
0074 };
0075 
0076 /* Each slot in the data ring has a 1:1 mapping to a slot in the desc ring */
0077 struct gve_rx_data_queue {
0078     union gve_rx_data_slot *data_ring; /* read by NIC */
0079     dma_addr_t data_bus; /* dma mapping of the slots */
0080     struct gve_rx_slot_page_info *page_info; /* page info of the buffers */
0081     struct gve_queue_page_list *qpl; /* qpl assigned to this queue */
0082     u8 raw_addressing; /* use raw_addressing? */
0083 };
0084 
0085 struct gve_priv;
0086 
0087 /* RX buffer queue for posting buffers to HW.
0088  * Each RX (completion) queue has a corresponding buffer queue.
0089  */
0090 struct gve_rx_buf_queue_dqo {
0091     struct gve_rx_desc_dqo *desc_ring;
0092     dma_addr_t bus;
0093     u32 head; /* Pointer to start cleaning buffers at. */
0094     u32 tail; /* Last posted buffer index + 1 */
0095     u32 mask; /* Mask for indices to the size of the ring */
0096 };
0097 
0098 /* RX completion queue to receive packets from HW. */
0099 struct gve_rx_compl_queue_dqo {
0100     struct gve_rx_compl_desc_dqo *desc_ring;
0101     dma_addr_t bus;
0102 
0103     /* Number of slots which did not have a buffer posted yet. We should not
0104      * post more buffers than the queue size to avoid HW overrunning the
0105      * queue.
0106      */
0107     int num_free_slots;
0108 
0109     /* HW uses a "generation bit" to notify SW of new descriptors. When a
0110      * descriptor's generation bit is different from the current generation,
0111      * that descriptor is ready to be consumed by SW.
0112      */
0113     u8 cur_gen_bit;
0114 
0115     /* Pointer into desc_ring where the next completion descriptor will be
0116      * received.
0117      */
0118     u32 head;
0119     u32 mask; /* Mask for indices to the size of the ring */
0120 };
0121 
0122 /* Stores state for tracking buffers posted to HW */
0123 struct gve_rx_buf_state_dqo {
0124     /* The page posted to HW. */
0125     struct gve_rx_slot_page_info page_info;
0126 
0127     /* The DMA address corresponding to `page_info`. */
0128     dma_addr_t addr;
0129 
0130     /* Last offset into the page when it only had a single reference, at
0131      * which point every other offset is free to be reused.
0132      */
0133     u32 last_single_ref_offset;
0134 
0135     /* Linked list index to next element in the list, or -1 if none */
0136     s16 next;
0137 };
0138 
0139 /* `head` and `tail` are indices into an array, or -1 if empty. */
0140 struct gve_index_list {
0141     s16 head;
0142     s16 tail;
0143 };
0144 
0145 /* A single received packet split across multiple buffers may be
0146  * reconstructed using the information in this structure.
0147  */
0148 struct gve_rx_ctx {
0149     /* head and tail of skb chain for the current packet or NULL if none */
0150     struct sk_buff *skb_head;
0151     struct sk_buff *skb_tail;
0152     u16 total_expected_size;
0153     u8 expected_frag_cnt;
0154     u8 curr_frag_cnt;
0155     u8 reuse_frags;
0156 };
0157 
0158 /* Contains datapath state used to represent an RX queue. */
0159 struct gve_rx_ring {
0160     struct gve_priv *gve;
0161     union {
0162         /* GQI fields */
0163         struct {
0164             struct gve_rx_desc_queue desc;
0165             struct gve_rx_data_queue data;
0166 
0167             /* threshold for posting new buffs and descs */
0168             u32 db_threshold;
0169             u16 packet_buffer_size;
0170         };
0171 
0172         /* DQO fields. */
0173         struct {
0174             struct gve_rx_buf_queue_dqo bufq;
0175             struct gve_rx_compl_queue_dqo complq;
0176 
0177             struct gve_rx_buf_state_dqo *buf_states;
0178             u16 num_buf_states;
0179 
0180             /* Linked list of gve_rx_buf_state_dqo. Index into
0181              * buf_states, or -1 if empty.
0182              */
0183             s16 free_buf_states;
0184 
0185             /* Linked list of gve_rx_buf_state_dqo. Indexes into
0186              * buf_states, or -1 if empty.
0187              *
0188              * This list contains buf_states which are pointing to
0189              * valid buffers.
0190              *
0191              * We use a FIFO here in order to increase the
0192              * probability that buffers can be reused by increasing
0193              * the time between usages.
0194              */
0195             struct gve_index_list recycled_buf_states;
0196 
0197             /* Linked list of gve_rx_buf_state_dqo. Indexes into
0198              * buf_states, or -1 if empty.
0199              *
0200              * This list contains buf_states which have buffers
0201              * which cannot be reused yet.
0202              */
0203             struct gve_index_list used_buf_states;
0204         } dqo;
0205     };
0206 
0207     u64 rbytes; /* free-running bytes received */
0208     u64 rpackets; /* free-running packets received */
0209     u32 cnt; /* free-running total number of completed packets */
0210     u32 fill_cnt; /* free-running total number of descs and buffs posted */
0211     u32 mask; /* masks the cnt and fill_cnt to the size of the ring */
0212     u64 rx_copybreak_pkt; /* free-running count of copybreak packets */
0213     u64 rx_copied_pkt; /* free-running total number of copied packets */
0214     u64 rx_skb_alloc_fail; /* free-running count of skb alloc fails */
0215     u64 rx_buf_alloc_fail; /* free-running count of buffer alloc fails */
0216     u64 rx_desc_err_dropped_pkt; /* free-running count of packets dropped by descriptor error */
0217     u64 rx_cont_packet_cnt; /* free-running multi-fragment packets received */
0218     u64 rx_frag_flip_cnt; /* free-running count of rx segments where page_flip was used */
0219     u64 rx_frag_copy_cnt; /* free-running count of rx segments copied into skb linear portion */
0220     u32 q_num; /* queue index */
0221     u32 ntfy_id; /* notification block index */
0222     struct gve_queue_resources *q_resources; /* head and tail pointer idx */
0223     dma_addr_t q_resources_bus; /* dma address for the queue resources */
0224     struct u64_stats_sync statss; /* sync stats for 32bit archs */
0225 
0226     struct gve_rx_ctx ctx; /* Info for packet currently being processed in this ring. */
0227 };
0228 
0229 /* A TX desc ring entry */
0230 union gve_tx_desc {
0231     struct gve_tx_pkt_desc pkt; /* first desc for a packet */
0232     struct gve_tx_mtd_desc mtd; /* optional metadata descriptor */
0233     struct gve_tx_seg_desc seg; /* subsequent descs for a packet */
0234 };
0235 
0236 /* Tracks the memory in the fifo occupied by a segment of a packet */
0237 struct gve_tx_iovec {
0238     u32 iov_offset; /* offset into this segment */
0239     u32 iov_len; /* length */
0240     u32 iov_padding; /* padding associated with this segment */
0241 };
0242 
0243 /* Tracks the memory in the fifo occupied by the skb. Mapped 1:1 to a desc
0244  * ring entry but only used for a pkt_desc not a seg_desc
0245  */
0246 struct gve_tx_buffer_state {
0247     struct sk_buff *skb; /* skb for this pkt */
0248     union {
0249         struct gve_tx_iovec iov[GVE_TX_MAX_IOVEC]; /* segments of this pkt */
0250         struct {
0251             DEFINE_DMA_UNMAP_ADDR(dma);
0252             DEFINE_DMA_UNMAP_LEN(len);
0253         };
0254     };
0255 };
0256 
0257 /* A TX buffer - each queue has one */
0258 struct gve_tx_fifo {
0259     void *base; /* address of base of FIFO */
0260     u32 size; /* total size */
0261     atomic_t available; /* how much space is still available */
0262     u32 head; /* offset to write at */
0263     struct gve_queue_page_list *qpl; /* QPL mapped into this FIFO */
0264 };
0265 
0266 /* TX descriptor for DQO format */
0267 union gve_tx_desc_dqo {
0268     struct gve_tx_pkt_desc_dqo pkt;
0269     struct gve_tx_tso_context_desc_dqo tso_ctx;
0270     struct gve_tx_general_context_desc_dqo general_ctx;
0271 };
0272 
0273 enum gve_packet_state {
0274     /* Packet is in free list, available to be allocated.
0275      * This should always be zero since state is not explicitly initialized.
0276      */
0277     GVE_PACKET_STATE_UNALLOCATED,
0278     /* Packet is expecting a regular data completion or miss completion */
0279     GVE_PACKET_STATE_PENDING_DATA_COMPL,
0280     /* Packet has received a miss completion and is expecting a
0281      * re-injection completion.
0282      */
0283     GVE_PACKET_STATE_PENDING_REINJECT_COMPL,
0284     /* No valid completion received within the specified timeout. */
0285     GVE_PACKET_STATE_TIMED_OUT_COMPL,
0286 };
0287 
0288 struct gve_tx_pending_packet_dqo {
0289     struct sk_buff *skb; /* skb for this packet */
0290 
0291     /* 0th element corresponds to the linear portion of `skb`, should be
0292      * unmapped with `dma_unmap_single`.
0293      *
0294      * All others correspond to `skb`'s frags and should be unmapped with
0295      * `dma_unmap_page`.
0296      */
0297     DEFINE_DMA_UNMAP_ADDR(dma[MAX_SKB_FRAGS + 1]);
0298     DEFINE_DMA_UNMAP_LEN(len[MAX_SKB_FRAGS + 1]);
0299     u16 num_bufs;
0300 
0301     /* Linked list index to next element in the list, or -1 if none */
0302     s16 next;
0303 
0304     /* Linked list index to prev element in the list, or -1 if none.
0305      * Used for tracking either outstanding miss completions or prematurely
0306      * freed packets.
0307      */
0308     s16 prev;
0309 
0310     /* Identifies the current state of the packet as defined in
0311      * `enum gve_packet_state`.
0312      */
0313     u8 state;
0314 
0315     /* If packet is an outstanding miss completion, then the packet is
0316      * freed if the corresponding re-injection completion is not received
0317      * before kernel jiffies exceeds timeout_jiffies.
0318      */
0319     unsigned long timeout_jiffies;
0320 };
0321 
0322 /* Contains datapath state used to represent a TX queue. */
0323 struct gve_tx_ring {
0324     /* Cacheline 0 -- Accessed & dirtied during transmit */
0325     union {
0326         /* GQI fields */
0327         struct {
0328             struct gve_tx_fifo tx_fifo;
0329             u32 req; /* driver tracked head pointer */
0330             u32 done; /* driver tracked tail pointer */
0331         };
0332 
0333         /* DQO fields. */
0334         struct {
0335             /* Linked list of gve_tx_pending_packet_dqo. Index into
0336              * pending_packets, or -1 if empty.
0337              *
0338              * This is a consumer list owned by the TX path. When it
0339              * runs out, the producer list is stolen from the
0340              * completion handling path
0341              * (dqo_compl.free_pending_packets).
0342              */
0343             s16 free_pending_packets;
0344 
0345             /* Cached value of `dqo_compl.hw_tx_head` */
0346             u32 head;
0347             u32 tail; /* Last posted buffer index + 1 */
0348 
0349             /* Index of the last descriptor with "report event" bit
0350              * set.
0351              */
0352             u32 last_re_idx;
0353         } dqo_tx;
0354     };
0355 
0356     /* Cacheline 1 -- Accessed & dirtied during gve_clean_tx_done */
0357     union {
0358         /* GQI fields */
0359         struct {
0360             /* Spinlock for when cleanup in progress */
0361             spinlock_t clean_lock;
0362         };
0363 
0364         /* DQO fields. */
0365         struct {
0366             u32 head; /* Last read on compl_desc */
0367 
0368             /* Tracks the current gen bit of compl_q */
0369             u8 cur_gen_bit;
0370 
0371             /* Linked list of gve_tx_pending_packet_dqo. Index into
0372              * pending_packets, or -1 if empty.
0373              *
0374              * This is the producer list, owned by the completion
0375              * handling path. When the consumer list
0376              * (dqo_tx.free_pending_packets) is runs out, this list
0377              * will be stolen.
0378              */
0379             atomic_t free_pending_packets;
0380 
0381             /* Last TX ring index fetched by HW */
0382             atomic_t hw_tx_head;
0383 
0384             /* List to track pending packets which received a miss
0385              * completion but not a corresponding reinjection.
0386              */
0387             struct gve_index_list miss_completions;
0388 
0389             /* List to track pending packets that were completed
0390              * before receiving a valid completion because they
0391              * reached a specified timeout.
0392              */
0393             struct gve_index_list timed_out_completions;
0394         } dqo_compl;
0395     } ____cacheline_aligned;
0396     u64 pkt_done; /* free-running - total packets completed */
0397     u64 bytes_done; /* free-running - total bytes completed */
0398     u64 dropped_pkt; /* free-running - total packets dropped */
0399     u64 dma_mapping_error; /* count of dma mapping errors */
0400 
0401     /* Cacheline 2 -- Read-mostly fields */
0402     union {
0403         /* GQI fields */
0404         struct {
0405             union gve_tx_desc *desc;
0406 
0407             /* Maps 1:1 to a desc */
0408             struct gve_tx_buffer_state *info;
0409         };
0410 
0411         /* DQO fields. */
0412         struct {
0413             union gve_tx_desc_dqo *tx_ring;
0414             struct gve_tx_compl_desc *compl_ring;
0415 
0416             struct gve_tx_pending_packet_dqo *pending_packets;
0417             s16 num_pending_packets;
0418 
0419             u32 complq_mask; /* complq size is complq_mask + 1 */
0420         } dqo;
0421     } ____cacheline_aligned;
0422     struct netdev_queue *netdev_txq;
0423     struct gve_queue_resources *q_resources; /* head and tail pointer idx */
0424     struct device *dev;
0425     u32 mask; /* masks req and done down to queue size */
0426     u8 raw_addressing; /* use raw_addressing? */
0427 
0428     /* Slow-path fields */
0429     u32 q_num ____cacheline_aligned; /* queue idx */
0430     u32 stop_queue; /* count of queue stops */
0431     u32 wake_queue; /* count of queue wakes */
0432     u32 queue_timeout; /* count of queue timeouts */
0433     u32 ntfy_id; /* notification block index */
0434     u32 last_kick_msec; /* Last time the queue was kicked */
0435     dma_addr_t bus; /* dma address of the descr ring */
0436     dma_addr_t q_resources_bus; /* dma address of the queue resources */
0437     dma_addr_t complq_bus_dqo; /* dma address of the dqo.compl_ring */
0438     struct u64_stats_sync statss; /* sync stats for 32bit archs */
0439 } ____cacheline_aligned;
0440 
0441 /* Wraps the info for one irq including the napi struct and the queues
0442  * associated with that irq.
0443  */
0444 struct gve_notify_block {
0445     __be32 *irq_db_index; /* pointer to idx into Bar2 */
0446     char name[IFNAMSIZ + 16]; /* name registered with the kernel */
0447     struct napi_struct napi; /* kernel napi struct for this block */
0448     struct gve_priv *priv;
0449     struct gve_tx_ring *tx; /* tx rings on this block */
0450     struct gve_rx_ring *rx; /* rx rings on this block */
0451 };
0452 
0453 /* Tracks allowed and current queue settings */
0454 struct gve_queue_config {
0455     u16 max_queues;
0456     u16 num_queues; /* current */
0457 };
0458 
0459 /* Tracks the available and used qpl IDs */
0460 struct gve_qpl_config {
0461     u32 qpl_map_size; /* map memory size */
0462     unsigned long *qpl_id_map; /* bitmap of used qpl ids */
0463 };
0464 
0465 struct gve_options_dqo_rda {
0466     u16 tx_comp_ring_entries; /* number of tx_comp descriptors */
0467     u16 rx_buff_ring_entries; /* number of rx_buff descriptors */
0468 };
0469 
0470 struct gve_irq_db {
0471     __be32 index;
0472 } ____cacheline_aligned;
0473 
0474 struct gve_ptype {
0475     u8 l3_type;  /* `gve_l3_type` in gve_adminq.h */
0476     u8 l4_type;  /* `gve_l4_type` in gve_adminq.h */
0477 };
0478 
0479 struct gve_ptype_lut {
0480     struct gve_ptype ptypes[GVE_NUM_PTYPES];
0481 };
0482 
0483 /* GVE_QUEUE_FORMAT_UNSPECIFIED must be zero since 0 is the default value
0484  * when the entire configure_device_resources command is zeroed out and the
0485  * queue_format is not specified.
0486  */
0487 enum gve_queue_format {
0488     GVE_QUEUE_FORMAT_UNSPECIFIED    = 0x0,
0489     GVE_GQI_RDA_FORMAT      = 0x1,
0490     GVE_GQI_QPL_FORMAT      = 0x2,
0491     GVE_DQO_RDA_FORMAT      = 0x3,
0492 };
0493 
0494 struct gve_priv {
0495     struct net_device *dev;
0496     struct gve_tx_ring *tx; /* array of tx_cfg.num_queues */
0497     struct gve_rx_ring *rx; /* array of rx_cfg.num_queues */
0498     struct gve_queue_page_list *qpls; /* array of num qpls */
0499     struct gve_notify_block *ntfy_blocks; /* array of num_ntfy_blks */
0500     struct gve_irq_db *irq_db_indices; /* array of num_ntfy_blks */
0501     dma_addr_t irq_db_indices_bus;
0502     struct msix_entry *msix_vectors; /* array of num_ntfy_blks + 1 */
0503     char mgmt_msix_name[IFNAMSIZ + 16];
0504     u32 mgmt_msix_idx;
0505     __be32 *counter_array; /* array of num_event_counters */
0506     dma_addr_t counter_array_bus;
0507 
0508     u16 num_event_counters;
0509     u16 tx_desc_cnt; /* num desc per ring */
0510     u16 rx_desc_cnt; /* num desc per ring */
0511     u16 tx_pages_per_qpl; /* tx buffer length */
0512     u16 rx_data_slot_cnt; /* rx buffer length */
0513     u64 max_registered_pages;
0514     u64 num_registered_pages; /* num pages registered with NIC */
0515     u32 rx_copybreak; /* copy packets smaller than this */
0516     u16 default_num_queues; /* default num queues to set up */
0517 
0518     struct gve_queue_config tx_cfg;
0519     struct gve_queue_config rx_cfg;
0520     struct gve_qpl_config qpl_cfg; /* map used QPL ids */
0521     u32 num_ntfy_blks; /* spilt between TX and RX so must be even */
0522 
0523     struct gve_registers __iomem *reg_bar0; /* see gve_register.h */
0524     __be32 __iomem *db_bar2; /* "array" of doorbells */
0525     u32 msg_enable; /* level for netif* netdev print macros */
0526     struct pci_dev *pdev;
0527 
0528     /* metrics */
0529     u32 tx_timeo_cnt;
0530 
0531     /* Admin queue - see gve_adminq.h*/
0532     union gve_adminq_command *adminq;
0533     dma_addr_t adminq_bus_addr;
0534     u32 adminq_mask; /* masks prod_cnt to adminq size */
0535     u32 adminq_prod_cnt; /* free-running count of AQ cmds executed */
0536     u32 adminq_cmd_fail; /* free-running count of AQ cmds failed */
0537     u32 adminq_timeouts; /* free-running count of AQ cmds timeouts */
0538     /* free-running count of per AQ cmd executed */
0539     u32 adminq_describe_device_cnt;
0540     u32 adminq_cfg_device_resources_cnt;
0541     u32 adminq_register_page_list_cnt;
0542     u32 adminq_unregister_page_list_cnt;
0543     u32 adminq_create_tx_queue_cnt;
0544     u32 adminq_create_rx_queue_cnt;
0545     u32 adminq_destroy_tx_queue_cnt;
0546     u32 adminq_destroy_rx_queue_cnt;
0547     u32 adminq_dcfg_device_resources_cnt;
0548     u32 adminq_set_driver_parameter_cnt;
0549     u32 adminq_report_stats_cnt;
0550     u32 adminq_report_link_speed_cnt;
0551     u32 adminq_get_ptype_map_cnt;
0552 
0553     /* Global stats */
0554     u32 interface_up_cnt; /* count of times interface turned up since last reset */
0555     u32 interface_down_cnt; /* count of times interface turned down since last reset */
0556     u32 reset_cnt; /* count of reset */
0557     u32 page_alloc_fail; /* count of page alloc fails */
0558     u32 dma_mapping_error; /* count of dma mapping errors */
0559     u32 stats_report_trigger_cnt; /* count of device-requested stats-reports since last reset */
0560     u32 suspend_cnt; /* count of times suspended */
0561     u32 resume_cnt; /* count of times resumed */
0562     struct workqueue_struct *gve_wq;
0563     struct work_struct service_task;
0564     struct work_struct stats_report_task;
0565     unsigned long service_task_flags;
0566     unsigned long state_flags;
0567 
0568     struct gve_stats_report *stats_report;
0569     u64 stats_report_len;
0570     dma_addr_t stats_report_bus; /* dma address for the stats report */
0571     unsigned long ethtool_flags;
0572 
0573     unsigned long stats_report_timer_period;
0574     struct timer_list stats_report_timer;
0575 
0576     /* Gvnic device link speed from hypervisor. */
0577     u64 link_speed;
0578     bool up_before_suspend; /* True if dev was up before suspend */
0579 
0580     struct gve_options_dqo_rda options_dqo_rda;
0581     struct gve_ptype_lut *ptype_lut_dqo;
0582 
0583     /* Must be a power of two. */
0584     int data_buffer_size_dqo;
0585 
0586     enum gve_queue_format queue_format;
0587 
0588     /* Interrupt coalescing settings */
0589     u32 tx_coalesce_usecs;
0590     u32 rx_coalesce_usecs;
0591 };
0592 
0593 enum gve_service_task_flags_bit {
0594     GVE_PRIV_FLAGS_DO_RESET         = 1,
0595     GVE_PRIV_FLAGS_RESET_IN_PROGRESS    = 2,
0596     GVE_PRIV_FLAGS_PROBE_IN_PROGRESS    = 3,
0597     GVE_PRIV_FLAGS_DO_REPORT_STATS = 4,
0598 };
0599 
0600 enum gve_state_flags_bit {
0601     GVE_PRIV_FLAGS_ADMIN_QUEUE_OK       = 1,
0602     GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK  = 2,
0603     GVE_PRIV_FLAGS_DEVICE_RINGS_OK      = 3,
0604     GVE_PRIV_FLAGS_NAPI_ENABLED     = 4,
0605 };
0606 
0607 enum gve_ethtool_flags_bit {
0608     GVE_PRIV_FLAGS_REPORT_STATS     = 0,
0609 };
0610 
0611 static inline bool gve_get_do_reset(struct gve_priv *priv)
0612 {
0613     return test_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
0614 }
0615 
0616 static inline void gve_set_do_reset(struct gve_priv *priv)
0617 {
0618     set_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
0619 }
0620 
0621 static inline void gve_clear_do_reset(struct gve_priv *priv)
0622 {
0623     clear_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
0624 }
0625 
0626 static inline bool gve_get_reset_in_progress(struct gve_priv *priv)
0627 {
0628     return test_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS,
0629             &priv->service_task_flags);
0630 }
0631 
0632 static inline void gve_set_reset_in_progress(struct gve_priv *priv)
0633 {
0634     set_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
0635 }
0636 
0637 static inline void gve_clear_reset_in_progress(struct gve_priv *priv)
0638 {
0639     clear_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
0640 }
0641 
0642 static inline bool gve_get_probe_in_progress(struct gve_priv *priv)
0643 {
0644     return test_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS,
0645             &priv->service_task_flags);
0646 }
0647 
0648 static inline void gve_set_probe_in_progress(struct gve_priv *priv)
0649 {
0650     set_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
0651 }
0652 
0653 static inline void gve_clear_probe_in_progress(struct gve_priv *priv)
0654 {
0655     clear_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
0656 }
0657 
0658 static inline bool gve_get_do_report_stats(struct gve_priv *priv)
0659 {
0660     return test_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS,
0661             &priv->service_task_flags);
0662 }
0663 
0664 static inline void gve_set_do_report_stats(struct gve_priv *priv)
0665 {
0666     set_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
0667 }
0668 
0669 static inline void gve_clear_do_report_stats(struct gve_priv *priv)
0670 {
0671     clear_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
0672 }
0673 
0674 static inline bool gve_get_admin_queue_ok(struct gve_priv *priv)
0675 {
0676     return test_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
0677 }
0678 
0679 static inline void gve_set_admin_queue_ok(struct gve_priv *priv)
0680 {
0681     set_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
0682 }
0683 
0684 static inline void gve_clear_admin_queue_ok(struct gve_priv *priv)
0685 {
0686     clear_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
0687 }
0688 
0689 static inline bool gve_get_device_resources_ok(struct gve_priv *priv)
0690 {
0691     return test_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
0692 }
0693 
0694 static inline void gve_set_device_resources_ok(struct gve_priv *priv)
0695 {
0696     set_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
0697 }
0698 
0699 static inline void gve_clear_device_resources_ok(struct gve_priv *priv)
0700 {
0701     clear_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
0702 }
0703 
0704 static inline bool gve_get_device_rings_ok(struct gve_priv *priv)
0705 {
0706     return test_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
0707 }
0708 
0709 static inline void gve_set_device_rings_ok(struct gve_priv *priv)
0710 {
0711     set_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
0712 }
0713 
0714 static inline void gve_clear_device_rings_ok(struct gve_priv *priv)
0715 {
0716     clear_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
0717 }
0718 
0719 static inline bool gve_get_napi_enabled(struct gve_priv *priv)
0720 {
0721     return test_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
0722 }
0723 
0724 static inline void gve_set_napi_enabled(struct gve_priv *priv)
0725 {
0726     set_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
0727 }
0728 
0729 static inline void gve_clear_napi_enabled(struct gve_priv *priv)
0730 {
0731     clear_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
0732 }
0733 
0734 static inline bool gve_get_report_stats(struct gve_priv *priv)
0735 {
0736     return test_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
0737 }
0738 
0739 static inline void gve_clear_report_stats(struct gve_priv *priv)
0740 {
0741     clear_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
0742 }
0743 
0744 /* Returns the address of the ntfy_blocks irq doorbell
0745  */
0746 static inline __be32 __iomem *gve_irq_doorbell(struct gve_priv *priv,
0747                            struct gve_notify_block *block)
0748 {
0749     return &priv->db_bar2[be32_to_cpu(*block->irq_db_index)];
0750 }
0751 
0752 /* Returns the index into ntfy_blocks of the given tx ring's block
0753  */
0754 static inline u32 gve_tx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
0755 {
0756     return queue_idx;
0757 }
0758 
0759 /* Returns the index into ntfy_blocks of the given rx ring's block
0760  */
0761 static inline u32 gve_rx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
0762 {
0763     return (priv->num_ntfy_blks / 2) + queue_idx;
0764 }
0765 
0766 /* Returns the number of tx queue page lists
0767  */
0768 static inline u32 gve_num_tx_qpls(struct gve_priv *priv)
0769 {
0770     if (priv->queue_format != GVE_GQI_QPL_FORMAT)
0771         return 0;
0772 
0773     return priv->tx_cfg.num_queues;
0774 }
0775 
0776 /* Returns the number of rx queue page lists
0777  */
0778 static inline u32 gve_num_rx_qpls(struct gve_priv *priv)
0779 {
0780     if (priv->queue_format != GVE_GQI_QPL_FORMAT)
0781         return 0;
0782 
0783     return priv->rx_cfg.num_queues;
0784 }
0785 
0786 /* Returns a pointer to the next available tx qpl in the list of qpls
0787  */
0788 static inline
0789 struct gve_queue_page_list *gve_assign_tx_qpl(struct gve_priv *priv)
0790 {
0791     int id = find_first_zero_bit(priv->qpl_cfg.qpl_id_map,
0792                      priv->qpl_cfg.qpl_map_size);
0793 
0794     /* we are out of tx qpls */
0795     if (id >= gve_num_tx_qpls(priv))
0796         return NULL;
0797 
0798     set_bit(id, priv->qpl_cfg.qpl_id_map);
0799     return &priv->qpls[id];
0800 }
0801 
0802 /* Returns a pointer to the next available rx qpl in the list of qpls
0803  */
0804 static inline
0805 struct gve_queue_page_list *gve_assign_rx_qpl(struct gve_priv *priv)
0806 {
0807     int id = find_next_zero_bit(priv->qpl_cfg.qpl_id_map,
0808                     priv->qpl_cfg.qpl_map_size,
0809                     gve_num_tx_qpls(priv));
0810 
0811     /* we are out of rx qpls */
0812     if (id == gve_num_tx_qpls(priv) + gve_num_rx_qpls(priv))
0813         return NULL;
0814 
0815     set_bit(id, priv->qpl_cfg.qpl_id_map);
0816     return &priv->qpls[id];
0817 }
0818 
0819 /* Unassigns the qpl with the given id
0820  */
0821 static inline void gve_unassign_qpl(struct gve_priv *priv, int id)
0822 {
0823     clear_bit(id, priv->qpl_cfg.qpl_id_map);
0824 }
0825 
0826 /* Returns the correct dma direction for tx and rx qpls
0827  */
0828 static inline enum dma_data_direction gve_qpl_dma_dir(struct gve_priv *priv,
0829                               int id)
0830 {
0831     if (id < gve_num_tx_qpls(priv))
0832         return DMA_TO_DEVICE;
0833     else
0834         return DMA_FROM_DEVICE;
0835 }
0836 
0837 static inline bool gve_is_gqi(struct gve_priv *priv)
0838 {
0839     return priv->queue_format == GVE_GQI_RDA_FORMAT ||
0840         priv->queue_format == GVE_GQI_QPL_FORMAT;
0841 }
0842 
0843 /* buffers */
0844 int gve_alloc_page(struct gve_priv *priv, struct device *dev,
0845            struct page **page, dma_addr_t *dma,
0846            enum dma_data_direction, gfp_t gfp_flags);
0847 void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
0848            enum dma_data_direction);
0849 /* tx handling */
0850 netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev);
0851 bool gve_tx_poll(struct gve_notify_block *block, int budget);
0852 int gve_tx_alloc_rings(struct gve_priv *priv);
0853 void gve_tx_free_rings_gqi(struct gve_priv *priv);
0854 u32 gve_tx_load_event_counter(struct gve_priv *priv,
0855                   struct gve_tx_ring *tx);
0856 bool gve_tx_clean_pending(struct gve_priv *priv, struct gve_tx_ring *tx);
0857 /* rx handling */
0858 void gve_rx_write_doorbell(struct gve_priv *priv, struct gve_rx_ring *rx);
0859 int gve_rx_poll(struct gve_notify_block *block, int budget);
0860 bool gve_rx_work_pending(struct gve_rx_ring *rx);
0861 int gve_rx_alloc_rings(struct gve_priv *priv);
0862 void gve_rx_free_rings_gqi(struct gve_priv *priv);
0863 /* Reset */
0864 void gve_schedule_reset(struct gve_priv *priv);
0865 int gve_reset(struct gve_priv *priv, bool attempt_teardown);
0866 int gve_adjust_queues(struct gve_priv *priv,
0867               struct gve_queue_config new_rx_config,
0868               struct gve_queue_config new_tx_config);
0869 /* report stats handling */
0870 void gve_handle_report_stats(struct gve_priv *priv);
0871 /* exported by ethtool.c */
0872 extern const struct ethtool_ops gve_ethtool_ops;
0873 /* needed by ethtool */
0874 extern const char gve_version_str[];
0875 #endif /* _GVE_H_ */