xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/google/gve/gve.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: (GPL-2.0 OR MIT)
2*4882a593Smuzhiyun  * Google virtual Ethernet (gve) driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2015-2019 Google, Inc.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef _GVE_H_
8*4882a593Smuzhiyun #define _GVE_H_
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/dma-mapping.h>
11*4882a593Smuzhiyun #include <linux/netdevice.h>
12*4882a593Smuzhiyun #include <linux/pci.h>
13*4882a593Smuzhiyun #include <linux/u64_stats_sync.h>
14*4882a593Smuzhiyun #include "gve_desc.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #ifndef PCI_VENDOR_ID_GOOGLE
17*4882a593Smuzhiyun #define PCI_VENDOR_ID_GOOGLE	0x1ae0
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define PCI_DEV_ID_GVNIC	0x0042
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define GVE_REGISTER_BAR	0
23*4882a593Smuzhiyun #define GVE_DOORBELL_BAR	2
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /* Driver can alloc up to 2 segments for the header and 2 for the payload. */
26*4882a593Smuzhiyun #define GVE_TX_MAX_IOVEC	4
27*4882a593Smuzhiyun /* 1 for management, 1 for rx, 1 for tx */
28*4882a593Smuzhiyun #define GVE_MIN_MSIX 3
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /* Numbers of gve tx/rx stats in stats report. */
31*4882a593Smuzhiyun #define GVE_TX_STATS_REPORT_NUM	6
32*4882a593Smuzhiyun #define GVE_RX_STATS_REPORT_NUM	2
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* Interval to schedule a stats report update, 20000ms. */
35*4882a593Smuzhiyun #define GVE_STATS_REPORT_TIMER_PERIOD	20000
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* Numbers of NIC tx/rx stats in stats report. */
38*4882a593Smuzhiyun #define NIC_TX_STATS_REPORT_NUM	0
39*4882a593Smuzhiyun #define NIC_RX_STATS_REPORT_NUM	4
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /* Each slot in the desc ring has a 1:1 mapping to a slot in the data ring */
42*4882a593Smuzhiyun struct gve_rx_desc_queue {
43*4882a593Smuzhiyun 	struct gve_rx_desc *desc_ring; /* the descriptor ring */
44*4882a593Smuzhiyun 	dma_addr_t bus; /* the bus for the desc_ring */
45*4882a593Smuzhiyun 	u8 seqno; /* the next expected seqno for this desc*/
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /* The page info for a single slot in the RX data queue */
49*4882a593Smuzhiyun struct gve_rx_slot_page_info {
50*4882a593Smuzhiyun 	struct page *page;
51*4882a593Smuzhiyun 	void *page_address;
52*4882a593Smuzhiyun 	u32 page_offset; /* offset to write to in page */
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /* A list of pages registered with the device during setup and used by a queue
56*4882a593Smuzhiyun  * as buffers
57*4882a593Smuzhiyun  */
58*4882a593Smuzhiyun struct gve_queue_page_list {
59*4882a593Smuzhiyun 	u32 id; /* unique id */
60*4882a593Smuzhiyun 	u32 num_entries;
61*4882a593Smuzhiyun 	struct page **pages; /* list of num_entries pages */
62*4882a593Smuzhiyun 	dma_addr_t *page_buses; /* the dma addrs of the pages */
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* Each slot in the data ring has a 1:1 mapping to a slot in the desc ring */
66*4882a593Smuzhiyun struct gve_rx_data_queue {
67*4882a593Smuzhiyun 	struct gve_rx_data_slot *data_ring; /* read by NIC */
68*4882a593Smuzhiyun 	dma_addr_t data_bus; /* dma mapping of the slots */
69*4882a593Smuzhiyun 	struct gve_rx_slot_page_info *page_info; /* page info of the buffers */
70*4882a593Smuzhiyun 	struct gve_queue_page_list *qpl; /* qpl assigned to this queue */
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun struct gve_priv;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /* An RX ring that contains a power-of-two sized desc and data ring. */
76*4882a593Smuzhiyun struct gve_rx_ring {
77*4882a593Smuzhiyun 	struct gve_priv *gve;
78*4882a593Smuzhiyun 	struct gve_rx_desc_queue desc;
79*4882a593Smuzhiyun 	struct gve_rx_data_queue data;
80*4882a593Smuzhiyun 	u64 rbytes; /* free-running bytes received */
81*4882a593Smuzhiyun 	u64 rpackets; /* free-running packets received */
82*4882a593Smuzhiyun 	u32 cnt; /* free-running total number of completed packets */
83*4882a593Smuzhiyun 	u32 fill_cnt; /* free-running total number of descs and buffs posted */
84*4882a593Smuzhiyun 	u32 mask; /* masks the cnt and fill_cnt to the size of the ring */
85*4882a593Smuzhiyun 	u64 rx_copybreak_pkt; /* free-running count of copybreak packets */
86*4882a593Smuzhiyun 	u64 rx_copied_pkt; /* free-running total number of copied packets */
87*4882a593Smuzhiyun 	u64 rx_skb_alloc_fail; /* free-running count of skb alloc fails */
88*4882a593Smuzhiyun 	u64 rx_buf_alloc_fail; /* free-running count of buffer alloc fails */
89*4882a593Smuzhiyun 	u64 rx_desc_err_dropped_pkt; /* free-running count of packets dropped by descriptor error */
90*4882a593Smuzhiyun 	u32 q_num; /* queue index */
91*4882a593Smuzhiyun 	u32 ntfy_id; /* notification block index */
92*4882a593Smuzhiyun 	struct gve_queue_resources *q_resources; /* head and tail pointer idx */
93*4882a593Smuzhiyun 	dma_addr_t q_resources_bus; /* dma address for the queue resources */
94*4882a593Smuzhiyun 	struct u64_stats_sync statss; /* sync stats for 32bit archs */
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* A TX desc ring entry */
98*4882a593Smuzhiyun union gve_tx_desc {
99*4882a593Smuzhiyun 	struct gve_tx_pkt_desc pkt; /* first desc for a packet */
100*4882a593Smuzhiyun 	struct gve_tx_seg_desc seg; /* subsequent descs for a packet */
101*4882a593Smuzhiyun };
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Tracks the memory in the fifo occupied by a segment of a packet */
104*4882a593Smuzhiyun struct gve_tx_iovec {
105*4882a593Smuzhiyun 	u32 iov_offset; /* offset into this segment */
106*4882a593Smuzhiyun 	u32 iov_len; /* length */
107*4882a593Smuzhiyun 	u32 iov_padding; /* padding associated with this segment */
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun /* Tracks the memory in the fifo occupied by the skb. Mapped 1:1 to a desc
111*4882a593Smuzhiyun  * ring entry but only used for a pkt_desc not a seg_desc
112*4882a593Smuzhiyun  */
113*4882a593Smuzhiyun struct gve_tx_buffer_state {
114*4882a593Smuzhiyun 	struct sk_buff *skb; /* skb for this pkt */
115*4882a593Smuzhiyun 	struct gve_tx_iovec iov[GVE_TX_MAX_IOVEC]; /* segments of this pkt */
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /* A TX buffer - each queue has one */
119*4882a593Smuzhiyun struct gve_tx_fifo {
120*4882a593Smuzhiyun 	void *base; /* address of base of FIFO */
121*4882a593Smuzhiyun 	u32 size; /* total size */
122*4882a593Smuzhiyun 	atomic_t available; /* how much space is still available */
123*4882a593Smuzhiyun 	u32 head; /* offset to write at */
124*4882a593Smuzhiyun 	struct gve_queue_page_list *qpl; /* QPL mapped into this FIFO */
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /* A TX ring that contains a power-of-two sized desc ring and a FIFO buffer */
128*4882a593Smuzhiyun struct gve_tx_ring {
129*4882a593Smuzhiyun 	/* Cacheline 0 -- Accessed & dirtied during transmit */
130*4882a593Smuzhiyun 	struct gve_tx_fifo tx_fifo;
131*4882a593Smuzhiyun 	u32 req; /* driver tracked head pointer */
132*4882a593Smuzhiyun 	u32 done; /* driver tracked tail pointer */
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/* Cacheline 1 -- Accessed & dirtied during gve_clean_tx_done */
135*4882a593Smuzhiyun 	__be32 last_nic_done ____cacheline_aligned; /* NIC tail pointer */
136*4882a593Smuzhiyun 	u64 pkt_done; /* free-running - total packets completed */
137*4882a593Smuzhiyun 	u64 bytes_done; /* free-running - total bytes completed */
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* Cacheline 2 -- Read-mostly fields */
140*4882a593Smuzhiyun 	union gve_tx_desc *desc ____cacheline_aligned;
141*4882a593Smuzhiyun 	struct gve_tx_buffer_state *info; /* Maps 1:1 to a desc */
142*4882a593Smuzhiyun 	struct netdev_queue *netdev_txq;
143*4882a593Smuzhiyun 	struct gve_queue_resources *q_resources; /* head and tail pointer idx */
144*4882a593Smuzhiyun 	u32 mask; /* masks req and done down to queue size */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/* Slow-path fields */
147*4882a593Smuzhiyun 	u32 q_num ____cacheline_aligned; /* queue idx */
148*4882a593Smuzhiyun 	u32 stop_queue; /* count of queue stops */
149*4882a593Smuzhiyun 	u32 wake_queue; /* count of queue wakes */
150*4882a593Smuzhiyun 	u32 queue_timeout; /* count of queue timeouts */
151*4882a593Smuzhiyun 	u32 ntfy_id; /* notification block index */
152*4882a593Smuzhiyun 	u32 last_kick_msec; /* Last time the queue was kicked */
153*4882a593Smuzhiyun 	dma_addr_t bus; /* dma address of the descr ring */
154*4882a593Smuzhiyun 	dma_addr_t q_resources_bus; /* dma address of the queue resources */
155*4882a593Smuzhiyun 	struct u64_stats_sync statss; /* sync stats for 32bit archs */
156*4882a593Smuzhiyun } ____cacheline_aligned;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /* Wraps the info for one irq including the napi struct and the queues
159*4882a593Smuzhiyun  * associated with that irq.
160*4882a593Smuzhiyun  */
161*4882a593Smuzhiyun struct gve_notify_block {
162*4882a593Smuzhiyun 	__be32 irq_db_index; /* idx into Bar2 - set by device, must be 1st */
163*4882a593Smuzhiyun 	char name[IFNAMSIZ + 16]; /* name registered with the kernel */
164*4882a593Smuzhiyun 	struct napi_struct napi; /* kernel napi struct for this block */
165*4882a593Smuzhiyun 	struct gve_priv *priv;
166*4882a593Smuzhiyun 	struct gve_tx_ring *tx; /* tx rings on this block */
167*4882a593Smuzhiyun 	struct gve_rx_ring *rx; /* rx rings on this block */
168*4882a593Smuzhiyun } ____cacheline_aligned;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /* Tracks allowed and current queue settings */
171*4882a593Smuzhiyun struct gve_queue_config {
172*4882a593Smuzhiyun 	u16 max_queues;
173*4882a593Smuzhiyun 	u16 num_queues; /* current */
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /* Tracks the available and used qpl IDs */
177*4882a593Smuzhiyun struct gve_qpl_config {
178*4882a593Smuzhiyun 	u32 qpl_map_size; /* map memory size */
179*4882a593Smuzhiyun 	unsigned long *qpl_id_map; /* bitmap of used qpl ids */
180*4882a593Smuzhiyun };
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun struct gve_priv {
183*4882a593Smuzhiyun 	struct net_device *dev;
184*4882a593Smuzhiyun 	struct gve_tx_ring *tx; /* array of tx_cfg.num_queues */
185*4882a593Smuzhiyun 	struct gve_rx_ring *rx; /* array of rx_cfg.num_queues */
186*4882a593Smuzhiyun 	struct gve_queue_page_list *qpls; /* array of num qpls */
187*4882a593Smuzhiyun 	struct gve_notify_block *ntfy_blocks; /* array of num_ntfy_blks */
188*4882a593Smuzhiyun 	dma_addr_t ntfy_block_bus;
189*4882a593Smuzhiyun 	struct msix_entry *msix_vectors; /* array of num_ntfy_blks + 1 */
190*4882a593Smuzhiyun 	char mgmt_msix_name[IFNAMSIZ + 16];
191*4882a593Smuzhiyun 	u32 mgmt_msix_idx;
192*4882a593Smuzhiyun 	__be32 *counter_array; /* array of num_event_counters */
193*4882a593Smuzhiyun 	dma_addr_t counter_array_bus;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	u16 num_event_counters;
196*4882a593Smuzhiyun 	u16 tx_desc_cnt; /* num desc per ring */
197*4882a593Smuzhiyun 	u16 rx_desc_cnt; /* num desc per ring */
198*4882a593Smuzhiyun 	u16 tx_pages_per_qpl; /* tx buffer length */
199*4882a593Smuzhiyun 	u16 rx_pages_per_qpl; /* rx buffer length */
200*4882a593Smuzhiyun 	u64 max_registered_pages;
201*4882a593Smuzhiyun 	u64 num_registered_pages; /* num pages registered with NIC */
202*4882a593Smuzhiyun 	u32 rx_copybreak; /* copy packets smaller than this */
203*4882a593Smuzhiyun 	u16 default_num_queues; /* default num queues to set up */
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	struct gve_queue_config tx_cfg;
206*4882a593Smuzhiyun 	struct gve_queue_config rx_cfg;
207*4882a593Smuzhiyun 	struct gve_qpl_config qpl_cfg; /* map used QPL ids */
208*4882a593Smuzhiyun 	u32 num_ntfy_blks; /* spilt between TX and RX so must be even */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	struct gve_registers __iomem *reg_bar0; /* see gve_register.h */
211*4882a593Smuzhiyun 	__be32 __iomem *db_bar2; /* "array" of doorbells */
212*4882a593Smuzhiyun 	u32 msg_enable;	/* level for netif* netdev print macros	*/
213*4882a593Smuzhiyun 	struct pci_dev *pdev;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/* metrics */
216*4882a593Smuzhiyun 	u32 tx_timeo_cnt;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* Admin queue - see gve_adminq.h*/
219*4882a593Smuzhiyun 	union gve_adminq_command *adminq;
220*4882a593Smuzhiyun 	dma_addr_t adminq_bus_addr;
221*4882a593Smuzhiyun 	u32 adminq_mask; /* masks prod_cnt to adminq size */
222*4882a593Smuzhiyun 	u32 adminq_prod_cnt; /* free-running count of AQ cmds executed */
223*4882a593Smuzhiyun 	u32 adminq_cmd_fail; /* free-running count of AQ cmds failed */
224*4882a593Smuzhiyun 	u32 adminq_timeouts; /* free-running count of AQ cmds timeouts */
225*4882a593Smuzhiyun 	/* free-running count of per AQ cmd executed */
226*4882a593Smuzhiyun 	u32 adminq_describe_device_cnt;
227*4882a593Smuzhiyun 	u32 adminq_cfg_device_resources_cnt;
228*4882a593Smuzhiyun 	u32 adminq_register_page_list_cnt;
229*4882a593Smuzhiyun 	u32 adminq_unregister_page_list_cnt;
230*4882a593Smuzhiyun 	u32 adminq_create_tx_queue_cnt;
231*4882a593Smuzhiyun 	u32 adminq_create_rx_queue_cnt;
232*4882a593Smuzhiyun 	u32 adminq_destroy_tx_queue_cnt;
233*4882a593Smuzhiyun 	u32 adminq_destroy_rx_queue_cnt;
234*4882a593Smuzhiyun 	u32 adminq_dcfg_device_resources_cnt;
235*4882a593Smuzhiyun 	u32 adminq_set_driver_parameter_cnt;
236*4882a593Smuzhiyun 	u32 adminq_report_stats_cnt;
237*4882a593Smuzhiyun 	u32 adminq_report_link_speed_cnt;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/* Global stats */
240*4882a593Smuzhiyun 	u32 interface_up_cnt; /* count of times interface turned up since last reset */
241*4882a593Smuzhiyun 	u32 interface_down_cnt; /* count of times interface turned down since last reset */
242*4882a593Smuzhiyun 	u32 reset_cnt; /* count of reset */
243*4882a593Smuzhiyun 	u32 page_alloc_fail; /* count of page alloc fails */
244*4882a593Smuzhiyun 	u32 dma_mapping_error; /* count of dma mapping errors */
245*4882a593Smuzhiyun 	u32 stats_report_trigger_cnt; /* count of device-requested stats-reports since last reset */
246*4882a593Smuzhiyun 	struct workqueue_struct *gve_wq;
247*4882a593Smuzhiyun 	struct work_struct service_task;
248*4882a593Smuzhiyun 	struct work_struct stats_report_task;
249*4882a593Smuzhiyun 	unsigned long service_task_flags;
250*4882a593Smuzhiyun 	unsigned long state_flags;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	struct gve_stats_report *stats_report;
253*4882a593Smuzhiyun 	u64 stats_report_len;
254*4882a593Smuzhiyun 	dma_addr_t stats_report_bus; /* dma address for the stats report */
255*4882a593Smuzhiyun 	unsigned long ethtool_flags;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	unsigned long stats_report_timer_period;
258*4882a593Smuzhiyun 	struct timer_list stats_report_timer;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* Gvnic device link speed from hypervisor. */
261*4882a593Smuzhiyun 	u64 link_speed;
262*4882a593Smuzhiyun };
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun enum gve_service_task_flags_bit {
265*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_DO_RESET			= 1,
266*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_RESET_IN_PROGRESS	= 2,
267*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_PROBE_IN_PROGRESS	= 3,
268*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_DO_REPORT_STATS = 4,
269*4882a593Smuzhiyun };
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun enum gve_state_flags_bit {
272*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_ADMIN_QUEUE_OK		= 1,
273*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK	= 2,
274*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_DEVICE_RINGS_OK		= 3,
275*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_NAPI_ENABLED		= 4,
276*4882a593Smuzhiyun };
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun enum gve_ethtool_flags_bit {
279*4882a593Smuzhiyun 	GVE_PRIV_FLAGS_REPORT_STATS		= 0,
280*4882a593Smuzhiyun };
281*4882a593Smuzhiyun 
gve_get_do_reset(struct gve_priv * priv)282*4882a593Smuzhiyun static inline bool gve_get_do_reset(struct gve_priv *priv)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
gve_set_do_reset(struct gve_priv * priv)287*4882a593Smuzhiyun static inline void gve_set_do_reset(struct gve_priv *priv)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
gve_clear_do_reset(struct gve_priv * priv)292*4882a593Smuzhiyun static inline void gve_clear_do_reset(struct gve_priv *priv)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_DO_RESET, &priv->service_task_flags);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
gve_get_reset_in_progress(struct gve_priv * priv)297*4882a593Smuzhiyun static inline bool gve_get_reset_in_progress(struct gve_priv *priv)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS,
300*4882a593Smuzhiyun 			&priv->service_task_flags);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
gve_set_reset_in_progress(struct gve_priv * priv)303*4882a593Smuzhiyun static inline void gve_set_reset_in_progress(struct gve_priv *priv)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
gve_clear_reset_in_progress(struct gve_priv * priv)308*4882a593Smuzhiyun static inline void gve_clear_reset_in_progress(struct gve_priv *priv)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_RESET_IN_PROGRESS, &priv->service_task_flags);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
gve_get_probe_in_progress(struct gve_priv * priv)313*4882a593Smuzhiyun static inline bool gve_get_probe_in_progress(struct gve_priv *priv)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS,
316*4882a593Smuzhiyun 			&priv->service_task_flags);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
gve_set_probe_in_progress(struct gve_priv * priv)319*4882a593Smuzhiyun static inline void gve_set_probe_in_progress(struct gve_priv *priv)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
gve_clear_probe_in_progress(struct gve_priv * priv)324*4882a593Smuzhiyun static inline void gve_clear_probe_in_progress(struct gve_priv *priv)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_PROBE_IN_PROGRESS, &priv->service_task_flags);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
gve_get_do_report_stats(struct gve_priv * priv)329*4882a593Smuzhiyun static inline bool gve_get_do_report_stats(struct gve_priv *priv)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS,
332*4882a593Smuzhiyun 			&priv->service_task_flags);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
gve_set_do_report_stats(struct gve_priv * priv)335*4882a593Smuzhiyun static inline void gve_set_do_report_stats(struct gve_priv *priv)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
gve_clear_do_report_stats(struct gve_priv * priv)340*4882a593Smuzhiyun static inline void gve_clear_do_report_stats(struct gve_priv *priv)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_DO_REPORT_STATS, &priv->service_task_flags);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
gve_get_admin_queue_ok(struct gve_priv * priv)345*4882a593Smuzhiyun static inline bool gve_get_admin_queue_ok(struct gve_priv *priv)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
gve_set_admin_queue_ok(struct gve_priv * priv)350*4882a593Smuzhiyun static inline void gve_set_admin_queue_ok(struct gve_priv *priv)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
gve_clear_admin_queue_ok(struct gve_priv * priv)355*4882a593Smuzhiyun static inline void gve_clear_admin_queue_ok(struct gve_priv *priv)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_ADMIN_QUEUE_OK, &priv->state_flags);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
gve_get_device_resources_ok(struct gve_priv * priv)360*4882a593Smuzhiyun static inline bool gve_get_device_resources_ok(struct gve_priv *priv)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
gve_set_device_resources_ok(struct gve_priv * priv)365*4882a593Smuzhiyun static inline void gve_set_device_resources_ok(struct gve_priv *priv)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun 
gve_clear_device_resources_ok(struct gve_priv * priv)370*4882a593Smuzhiyun static inline void gve_clear_device_resources_ok(struct gve_priv *priv)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RESOURCES_OK, &priv->state_flags);
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
gve_get_device_rings_ok(struct gve_priv * priv)375*4882a593Smuzhiyun static inline bool gve_get_device_rings_ok(struct gve_priv *priv)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
gve_set_device_rings_ok(struct gve_priv * priv)380*4882a593Smuzhiyun static inline void gve_set_device_rings_ok(struct gve_priv *priv)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun 
gve_clear_device_rings_ok(struct gve_priv * priv)385*4882a593Smuzhiyun static inline void gve_clear_device_rings_ok(struct gve_priv *priv)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_DEVICE_RINGS_OK, &priv->state_flags);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
gve_get_napi_enabled(struct gve_priv * priv)390*4882a593Smuzhiyun static inline bool gve_get_napi_enabled(struct gve_priv *priv)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
gve_set_napi_enabled(struct gve_priv * priv)395*4882a593Smuzhiyun static inline void gve_set_napi_enabled(struct gve_priv *priv)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun 	set_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
gve_clear_napi_enabled(struct gve_priv * priv)400*4882a593Smuzhiyun static inline void gve_clear_napi_enabled(struct gve_priv *priv)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_NAPI_ENABLED, &priv->state_flags);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
gve_get_report_stats(struct gve_priv * priv)405*4882a593Smuzhiyun static inline bool gve_get_report_stats(struct gve_priv *priv)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	return test_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
gve_clear_report_stats(struct gve_priv * priv)410*4882a593Smuzhiyun static inline void gve_clear_report_stats(struct gve_priv *priv)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	clear_bit(GVE_PRIV_FLAGS_REPORT_STATS, &priv->ethtool_flags);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun /* Returns the address of the ntfy_blocks irq doorbell
416*4882a593Smuzhiyun  */
gve_irq_doorbell(struct gve_priv * priv,struct gve_notify_block * block)417*4882a593Smuzhiyun static inline __be32 __iomem *gve_irq_doorbell(struct gve_priv *priv,
418*4882a593Smuzhiyun 					       struct gve_notify_block *block)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun 	return &priv->db_bar2[be32_to_cpu(block->irq_db_index)];
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun /* Returns the index into ntfy_blocks of the given tx ring's block
424*4882a593Smuzhiyun  */
gve_tx_idx_to_ntfy(struct gve_priv * priv,u32 queue_idx)425*4882a593Smuzhiyun static inline u32 gve_tx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	return queue_idx;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun /* Returns the index into ntfy_blocks of the given rx ring's block
431*4882a593Smuzhiyun  */
gve_rx_idx_to_ntfy(struct gve_priv * priv,u32 queue_idx)432*4882a593Smuzhiyun static inline u32 gve_rx_idx_to_ntfy(struct gve_priv *priv, u32 queue_idx)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun 	return (priv->num_ntfy_blks / 2) + queue_idx;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /* Returns the number of tx queue page lists
438*4882a593Smuzhiyun  */
gve_num_tx_qpls(struct gve_priv * priv)439*4882a593Smuzhiyun static inline u32 gve_num_tx_qpls(struct gve_priv *priv)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	return priv->tx_cfg.num_queues;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun /* Returns the number of rx queue page lists
445*4882a593Smuzhiyun  */
gve_num_rx_qpls(struct gve_priv * priv)446*4882a593Smuzhiyun static inline u32 gve_num_rx_qpls(struct gve_priv *priv)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun 	return priv->rx_cfg.num_queues;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun /* Returns a pointer to the next available tx qpl in the list of qpls
452*4882a593Smuzhiyun  */
453*4882a593Smuzhiyun static inline
gve_assign_tx_qpl(struct gve_priv * priv)454*4882a593Smuzhiyun struct gve_queue_page_list *gve_assign_tx_qpl(struct gve_priv *priv)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun 	int id = find_first_zero_bit(priv->qpl_cfg.qpl_id_map,
457*4882a593Smuzhiyun 				     priv->qpl_cfg.qpl_map_size);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	/* we are out of tx qpls */
460*4882a593Smuzhiyun 	if (id >= gve_num_tx_qpls(priv))
461*4882a593Smuzhiyun 		return NULL;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	set_bit(id, priv->qpl_cfg.qpl_id_map);
464*4882a593Smuzhiyun 	return &priv->qpls[id];
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /* Returns a pointer to the next available rx qpl in the list of qpls
468*4882a593Smuzhiyun  */
469*4882a593Smuzhiyun static inline
gve_assign_rx_qpl(struct gve_priv * priv)470*4882a593Smuzhiyun struct gve_queue_page_list *gve_assign_rx_qpl(struct gve_priv *priv)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	int id = find_next_zero_bit(priv->qpl_cfg.qpl_id_map,
473*4882a593Smuzhiyun 				    priv->qpl_cfg.qpl_map_size,
474*4882a593Smuzhiyun 				    gve_num_tx_qpls(priv));
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	/* we are out of rx qpls */
477*4882a593Smuzhiyun 	if (id == gve_num_tx_qpls(priv) + gve_num_rx_qpls(priv))
478*4882a593Smuzhiyun 		return NULL;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	set_bit(id, priv->qpl_cfg.qpl_id_map);
481*4882a593Smuzhiyun 	return &priv->qpls[id];
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun /* Unassigns the qpl with the given id
485*4882a593Smuzhiyun  */
gve_unassign_qpl(struct gve_priv * priv,int id)486*4882a593Smuzhiyun static inline void gve_unassign_qpl(struct gve_priv *priv, int id)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	clear_bit(id, priv->qpl_cfg.qpl_id_map);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun /* Returns the correct dma direction for tx and rx qpls
492*4882a593Smuzhiyun  */
gve_qpl_dma_dir(struct gve_priv * priv,int id)493*4882a593Smuzhiyun static inline enum dma_data_direction gve_qpl_dma_dir(struct gve_priv *priv,
494*4882a593Smuzhiyun 						      int id)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	if (id < gve_num_tx_qpls(priv))
497*4882a593Smuzhiyun 		return DMA_TO_DEVICE;
498*4882a593Smuzhiyun 	else
499*4882a593Smuzhiyun 		return DMA_FROM_DEVICE;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun /* Returns true if the max mtu allows page recycling */
gve_can_recycle_pages(struct net_device * dev)503*4882a593Smuzhiyun static inline bool gve_can_recycle_pages(struct net_device *dev)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	/* We can't recycle the pages if we can't fit a packet into half a
506*4882a593Smuzhiyun 	 * page.
507*4882a593Smuzhiyun 	 */
508*4882a593Smuzhiyun 	return dev->max_mtu <= PAGE_SIZE / 2;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun /* buffers */
512*4882a593Smuzhiyun int gve_alloc_page(struct gve_priv *priv, struct device *dev,
513*4882a593Smuzhiyun 		   struct page **page, dma_addr_t *dma,
514*4882a593Smuzhiyun 		   enum dma_data_direction);
515*4882a593Smuzhiyun void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
516*4882a593Smuzhiyun 		   enum dma_data_direction);
517*4882a593Smuzhiyun /* tx handling */
518*4882a593Smuzhiyun netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev);
519*4882a593Smuzhiyun bool gve_tx_poll(struct gve_notify_block *block, int budget);
520*4882a593Smuzhiyun int gve_tx_alloc_rings(struct gve_priv *priv);
521*4882a593Smuzhiyun void gve_tx_free_rings(struct gve_priv *priv);
522*4882a593Smuzhiyun __be32 gve_tx_load_event_counter(struct gve_priv *priv,
523*4882a593Smuzhiyun 				 struct gve_tx_ring *tx);
524*4882a593Smuzhiyun /* rx handling */
525*4882a593Smuzhiyun void gve_rx_write_doorbell(struct gve_priv *priv, struct gve_rx_ring *rx);
526*4882a593Smuzhiyun bool gve_rx_poll(struct gve_notify_block *block, int budget);
527*4882a593Smuzhiyun int gve_rx_alloc_rings(struct gve_priv *priv);
528*4882a593Smuzhiyun void gve_rx_free_rings(struct gve_priv *priv);
529*4882a593Smuzhiyun bool gve_clean_rx_done(struct gve_rx_ring *rx, int budget,
530*4882a593Smuzhiyun 		       netdev_features_t feat);
531*4882a593Smuzhiyun /* Reset */
532*4882a593Smuzhiyun void gve_schedule_reset(struct gve_priv *priv);
533*4882a593Smuzhiyun int gve_reset(struct gve_priv *priv, bool attempt_teardown);
534*4882a593Smuzhiyun int gve_adjust_queues(struct gve_priv *priv,
535*4882a593Smuzhiyun 		      struct gve_queue_config new_rx_config,
536*4882a593Smuzhiyun 		      struct gve_queue_config new_tx_config);
537*4882a593Smuzhiyun /* report stats handling */
538*4882a593Smuzhiyun void gve_handle_report_stats(struct gve_priv *priv);
539*4882a593Smuzhiyun /* exported by ethtool.c */
540*4882a593Smuzhiyun extern const struct ethtool_ops gve_ethtool_ops;
541*4882a593Smuzhiyun /* needed by ethtool */
542*4882a593Smuzhiyun extern const char gve_version_str[];
543*4882a593Smuzhiyun #endif /* _GVE_H_ */
544