xref: /freebsd/sys/contrib/ena-com/ena_com.h (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2015-2021 Amazon.com, Inc. or its affiliates.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef ENA_COM
35 #define ENA_COM
36 
37 #include "ena_plat.h"
38 
39 #define ENA_MAX_NUM_IO_QUEUES 128U
40 /* We need to queues for each IO (on for Tx and one for Rx) */
41 #define ENA_TOTAL_NUM_QUEUES (2 * (ENA_MAX_NUM_IO_QUEUES))
42 
43 #define ENA_MAX_HANDLERS 256
44 
45 #define ENA_MAX_PHYS_ADDR_SIZE_BITS 48
46 
47 /* Unit in usec */
48 #define ENA_REG_READ_TIMEOUT 200000
49 
50 #define ADMIN_SQ_SIZE(depth)	((depth) * sizeof(struct ena_admin_aq_entry))
51 #define ADMIN_CQ_SIZE(depth)	((depth) * sizeof(struct ena_admin_acq_entry))
52 #define ADMIN_AENQ_SIZE(depth)	((depth) * sizeof(struct ena_admin_aenq_entry))
53 
54 /*****************************************************************************/
55 /*****************************************************************************/
56 /* ENA adaptive interrupt moderation settings */
57 
58 #define ENA_INTR_INITIAL_TX_INTERVAL_USECS ENA_INTR_INITIAL_TX_INTERVAL_USECS_PLAT
59 #define ENA_INTR_INITIAL_RX_INTERVAL_USECS 0
60 #define ENA_DEFAULT_INTR_DELAY_RESOLUTION 1
61 
62 #define ENA_HASH_KEY_SIZE 40
63 
64 #define ENA_HW_HINTS_NO_TIMEOUT	0xFFFF
65 
66 #define ENA_FEATURE_MAX_QUEUE_EXT_VER 1
67 
68 struct ena_llq_configurations {
69 	enum ena_admin_llq_header_location llq_header_location;
70 	enum ena_admin_llq_ring_entry_size llq_ring_entry_size;
71 	enum ena_admin_llq_stride_ctrl  llq_stride_ctrl;
72 	enum ena_admin_llq_num_descs_before_header llq_num_decs_before_header;
73 	u16 llq_ring_entry_size_value;
74 };
75 
76 enum queue_direction {
77 	ENA_COM_IO_QUEUE_DIRECTION_TX,
78 	ENA_COM_IO_QUEUE_DIRECTION_RX
79 };
80 
81 struct ena_com_buf {
82 	dma_addr_t paddr; /**< Buffer physical address */
83 	u16 len; /**< Buffer length in bytes */
84 };
85 
86 struct ena_com_rx_buf_info {
87 	u16 len;
88 	u16 req_id;
89 };
90 
91 struct ena_com_io_desc_addr {
92 	u8 __iomem *pbuf_dev_addr; /* LLQ address */
93 	u8 *virt_addr;
94 	dma_addr_t phys_addr;
95 	ena_mem_handle_t mem_handle;
96 };
97 
98 struct ena_com_tx_meta {
99 	u16 mss;
100 	u16 l3_hdr_len;
101 	u16 l3_hdr_offset;
102 	u16 l4_hdr_len; /* In words */
103 };
104 
105 struct ena_com_llq_info {
106 	u16 header_location_ctrl;
107 	u16 desc_stride_ctrl;
108 	u16 desc_list_entry_size_ctrl;
109 	u16 desc_list_entry_size;
110 	u16 descs_num_before_header;
111 	u16 descs_per_entry;
112 	u16 max_entries_in_tx_burst;
113 	bool disable_meta_caching;
114 };
115 
116 struct ena_com_io_cq {
117 	struct ena_com_io_desc_addr cdesc_addr;
118 	void *bus;
119 
120 	/* Interrupt unmask register */
121 	u32 __iomem *unmask_reg;
122 
123 	/* The completion queue head doorbell register */
124 	u32 __iomem *cq_head_db_reg;
125 
126 	/* numa configuration register (for TPH) */
127 	u32 __iomem *numa_node_cfg_reg;
128 
129 	/* The value to write to the above register to unmask
130 	 * the interrupt of this queue
131 	 */
132 	u32 msix_vector;
133 
134 	enum queue_direction direction;
135 
136 	/* holds the number of cdesc of the current packet */
137 	u16 cur_rx_pkt_cdesc_count;
138 	/* save the firt cdesc idx of the current packet */
139 	u16 cur_rx_pkt_cdesc_start_idx;
140 
141 	u16 q_depth;
142 	/* Caller qid */
143 	u16 qid;
144 
145 	/* Device queue index */
146 	u16 idx;
147 	u16 head;
148 	u16 last_head_update;
149 	u8 phase;
150 	u8 cdesc_entry_size_in_bytes;
151 
152 } ____cacheline_aligned;
153 
154 struct ena_com_io_bounce_buffer_control {
155 	u8 *base_buffer;
156 	u16 next_to_use;
157 	u16 buffer_size;
158 	u16 buffers_num;  /* Must be a power of 2 */
159 };
160 
161 /* This struct is to keep tracking the current location of the next llq entry */
162 struct ena_com_llq_pkt_ctrl {
163 	u8 *curr_bounce_buf;
164 	u16 idx;
165 	u16 descs_left_in_line;
166 };
167 
168 struct ena_com_io_sq {
169 	struct ena_com_io_desc_addr desc_addr;
170 	void *bus;
171 
172 	u32 __iomem *db_addr;
173 	u8 __iomem *header_addr;
174 
175 	enum queue_direction direction;
176 	enum ena_admin_placement_policy_type mem_queue_type;
177 
178 	bool disable_meta_caching;
179 
180 	u32 msix_vector;
181 	struct ena_com_tx_meta cached_tx_meta;
182 	struct ena_com_llq_info llq_info;
183 	struct ena_com_llq_pkt_ctrl llq_buf_ctrl;
184 	struct ena_com_io_bounce_buffer_control bounce_buf_ctrl;
185 
186 	u16 q_depth;
187 	u16 qid;
188 
189 	u16 idx;
190 	u16 tail;
191 	u16 next_to_comp;
192 	u16 llq_last_copy_tail;
193 	u32 tx_max_header_size;
194 	u8 phase;
195 	u8 desc_entry_size;
196 	u8 dma_addr_bits;
197 	u16 entries_in_tx_burst_left;
198 } ____cacheline_aligned;
199 
200 struct ena_com_admin_cq {
201 	struct ena_admin_acq_entry *entries;
202 	ena_mem_handle_t mem_handle;
203 	dma_addr_t dma_addr;
204 
205 	u16 head;
206 	u8 phase;
207 };
208 
209 struct ena_com_admin_sq {
210 	struct ena_admin_aq_entry *entries;
211 	ena_mem_handle_t mem_handle;
212 	dma_addr_t dma_addr;
213 
214 	u32 __iomem *db_addr;
215 
216 	u16 head;
217 	u16 tail;
218 	u8 phase;
219 
220 };
221 
222 struct ena_com_stats_admin {
223 	u64 aborted_cmd;
224 	u64 submitted_cmd;
225 	u64 completed_cmd;
226 	u64 out_of_space;
227 	u64 no_completion;
228 };
229 
230 struct ena_com_admin_queue {
231 	void *q_dmadev;
232 	void *bus;
233 	struct ena_com_dev *ena_dev;
234 	ena_spinlock_t q_lock; /* spinlock for the admin queue */
235 
236 	struct ena_comp_ctx *comp_ctx;
237 	u32 completion_timeout;
238 	u16 q_depth;
239 	struct ena_com_admin_cq cq;
240 	struct ena_com_admin_sq sq;
241 
242 	/* Indicate if the admin queue should poll for completion */
243 	bool polling;
244 
245 	/* Define if fallback to polling mode should occur */
246 	bool auto_polling;
247 
248 	u16 curr_cmd_id;
249 
250 	/* Indicate that the ena was initialized and can
251 	 * process new admin commands
252 	 */
253 	bool running_state;
254 
255 	/* Count the number of outstanding admin commands */
256 	ena_atomic32_t outstanding_cmds;
257 
258 	struct ena_com_stats_admin stats;
259 };
260 
261 struct ena_aenq_handlers;
262 
263 struct ena_com_aenq {
264 	u16 head;
265 	u8 phase;
266 	struct ena_admin_aenq_entry *entries;
267 	dma_addr_t dma_addr;
268 	ena_mem_handle_t mem_handle;
269 	u16 q_depth;
270 	struct ena_aenq_handlers *aenq_handlers;
271 };
272 
273 struct ena_com_mmio_read {
274 	struct ena_admin_ena_mmio_req_read_less_resp *read_resp;
275 	dma_addr_t read_resp_dma_addr;
276 	ena_mem_handle_t read_resp_mem_handle;
277 	u32 reg_read_to; /* in us */
278 	u16 seq_num;
279 	bool readless_supported;
280 	/* spin lock to ensure a single outstanding read */
281 	ena_spinlock_t lock;
282 };
283 
284 struct ena_rss {
285 	/* Indirect table */
286 	u16 *host_rss_ind_tbl;
287 	struct ena_admin_rss_ind_table_entry *rss_ind_tbl;
288 	dma_addr_t rss_ind_tbl_dma_addr;
289 	ena_mem_handle_t rss_ind_tbl_mem_handle;
290 	u16 tbl_log_size;
291 
292 	/* Hash key */
293 	enum ena_admin_hash_functions hash_func;
294 	struct ena_admin_feature_rss_flow_hash_control *hash_key;
295 	dma_addr_t hash_key_dma_addr;
296 	ena_mem_handle_t hash_key_mem_handle;
297 	u32 hash_init_val;
298 
299 	/* Flow Control */
300 	struct ena_admin_feature_rss_hash_control *hash_ctrl;
301 	dma_addr_t hash_ctrl_dma_addr;
302 	ena_mem_handle_t hash_ctrl_mem_handle;
303 
304 };
305 
306 struct ena_host_attribute {
307 	/* Debug area */
308 	u8 *debug_area_virt_addr;
309 	dma_addr_t debug_area_dma_addr;
310 	ena_mem_handle_t debug_area_dma_handle;
311 	u32 debug_area_size;
312 
313 	/* Host information */
314 	struct ena_admin_host_info *host_info;
315 	dma_addr_t host_info_dma_addr;
316 	ena_mem_handle_t host_info_dma_handle;
317 };
318 
319 /* Each ena_dev is a PCI function. */
320 struct ena_com_dev {
321 	struct ena_com_admin_queue admin_queue;
322 	struct ena_com_aenq aenq;
323 	struct ena_com_io_cq io_cq_queues[ENA_TOTAL_NUM_QUEUES];
324 	struct ena_com_io_sq io_sq_queues[ENA_TOTAL_NUM_QUEUES];
325 	u8 __iomem *reg_bar;
326 	void __iomem *mem_bar;
327 	void *dmadev;
328 	void *bus;
329 	ena_netdev *net_device;
330 
331 	enum ena_admin_placement_policy_type tx_mem_queue_type;
332 	u32 tx_max_header_size;
333 	u16 stats_func; /* Selected function for extended statistic dump */
334 	u16 stats_queue; /* Selected queue for extended statistic dump */
335 
336 	struct ena_com_mmio_read mmio_read;
337 
338 	struct ena_rss rss;
339 	u32 supported_features;
340 	u32 dma_addr_bits;
341 
342 	struct ena_host_attribute host_attr;
343 	bool adaptive_coalescing;
344 	u16 intr_delay_resolution;
345 
346 	/* interrupt moderation intervals are in usec divided by
347 	 * intr_delay_resolution, which is supplied by the device.
348 	 */
349 	u32 intr_moder_tx_interval;
350 	u32 intr_moder_rx_interval;
351 
352 	struct ena_intr_moder_entry *intr_moder_tbl;
353 
354 	struct ena_com_llq_info llq_info;
355 
356 	u32 ena_min_poll_delay_us;
357 };
358 
359 struct ena_com_dev_get_features_ctx {
360 	struct ena_admin_queue_feature_desc max_queues;
361 	struct ena_admin_queue_ext_feature_desc max_queue_ext;
362 	struct ena_admin_device_attr_feature_desc dev_attr;
363 	struct ena_admin_feature_aenq_desc aenq;
364 	struct ena_admin_feature_offload_desc offload;
365 	struct ena_admin_ena_hw_hints hw_hints;
366 	struct ena_admin_feature_llq_desc llq;
367 };
368 
369 struct ena_com_create_io_ctx {
370 	enum ena_admin_placement_policy_type mem_queue_type;
371 	enum queue_direction direction;
372 	int numa_node;
373 	u32 msix_vector;
374 	u16 queue_size;
375 	u16 qid;
376 };
377 
378 typedef void (*ena_aenq_handler)(void *data,
379 	struct ena_admin_aenq_entry *aenq_e);
380 
381 /* Holds aenq handlers. Indexed by AENQ event group */
382 struct ena_aenq_handlers {
383 	ena_aenq_handler handlers[ENA_MAX_HANDLERS];
384 	ena_aenq_handler unimplemented_handler;
385 };
386 
387 /*****************************************************************************/
388 /*****************************************************************************/
389 #if defined(__cplusplus)
390 extern "C" {
391 #endif
392 
393 /* ena_com_mmio_reg_read_request_init - Init the mmio reg read mechanism
394  * @ena_dev: ENA communication layer struct
395  *
396  * Initialize the register read mechanism.
397  *
398  * @note: This method must be the first stage in the initialization sequence.
399  *
400  * @return - 0 on success, negative value on failure.
401  */
402 int ena_com_mmio_reg_read_request_init(struct ena_com_dev *ena_dev);
403 
404 /* ena_com_set_mmio_read_mode - Enable/disable the indirect mmio reg read mechanism
405  * @ena_dev: ENA communication layer struct
406  * @readless_supported: readless mode (enable/disable)
407  */
408 void ena_com_set_mmio_read_mode(struct ena_com_dev *ena_dev,
409 				bool readless_supported);
410 
411 /* ena_com_mmio_reg_read_request_write_dev_addr - Write the mmio reg read return
412  * value physical address.
413  * @ena_dev: ENA communication layer struct
414  */
415 void ena_com_mmio_reg_read_request_write_dev_addr(struct ena_com_dev *ena_dev);
416 
417 /* ena_com_mmio_reg_read_request_destroy - Destroy the mmio reg read mechanism
418  * @ena_dev: ENA communication layer struct
419  */
420 void ena_com_mmio_reg_read_request_destroy(struct ena_com_dev *ena_dev);
421 
422 /* ena_com_admin_init - Init the admin and the async queues
423  * @ena_dev: ENA communication layer struct
424  * @aenq_handlers: Those handlers to be called upon event.
425  *
426  * Initialize the admin submission and completion queues.
427  * Initialize the asynchronous events notification queues.
428  *
429  * @return - 0 on success, negative value on failure.
430  */
431 int ena_com_admin_init(struct ena_com_dev *ena_dev,
432 		       struct ena_aenq_handlers *aenq_handlers);
433 
434 /* ena_com_admin_destroy - Destroy the admin and the async events queues.
435  * @ena_dev: ENA communication layer struct
436  *
437  * @note: Before calling this method, the caller must validate that the device
438  * won't send any additional admin completions/aenq.
439  * To achieve that, a FLR is recommended.
440  */
441 void ena_com_admin_destroy(struct ena_com_dev *ena_dev);
442 
443 /* ena_com_dev_reset - Perform device FLR to the device.
444  * @ena_dev: ENA communication layer struct
445  * @reset_reason: Specify what is the trigger for the reset in case of an error.
446  *
447  * @return - 0 on success, negative value on failure.
448  */
449 int ena_com_dev_reset(struct ena_com_dev *ena_dev,
450 		      enum ena_regs_reset_reason_types reset_reason);
451 
452 /* ena_com_create_io_queue - Create io queue.
453  * @ena_dev: ENA communication layer struct
454  * @ctx - create context structure
455  *
456  * Create the submission and the completion queues.
457  *
458  * @return - 0 on success, negative value on failure.
459  */
460 int ena_com_create_io_queue(struct ena_com_dev *ena_dev,
461 			    struct ena_com_create_io_ctx *ctx);
462 
463 /* ena_com_destroy_io_queue - Destroy IO queue with the queue id - qid.
464  * @ena_dev: ENA communication layer struct
465  * @qid - the caller virtual queue id.
466  */
467 void ena_com_destroy_io_queue(struct ena_com_dev *ena_dev, u16 qid);
468 
469 /* ena_com_get_io_handlers - Return the io queue handlers
470  * @ena_dev: ENA communication layer struct
471  * @qid - the caller virtual queue id.
472  * @io_sq - IO submission queue handler
473  * @io_cq - IO completion queue handler.
474  *
475  * @return - 0 on success, negative value on failure.
476  */
477 int ena_com_get_io_handlers(struct ena_com_dev *ena_dev, u16 qid,
478 			    struct ena_com_io_sq **io_sq,
479 			    struct ena_com_io_cq **io_cq);
480 
481 /* ena_com_admin_aenq_enable - ENAble asynchronous event notifications
482  * @ena_dev: ENA communication layer struct
483  *
484  * After this method, aenq event can be received via AENQ.
485  */
486 void ena_com_admin_aenq_enable(struct ena_com_dev *ena_dev);
487 
488 /* ena_com_set_admin_running_state - Set the state of the admin queue
489  * @ena_dev: ENA communication layer struct
490  *
491  * Change the state of the admin queue (enable/disable)
492  */
493 void ena_com_set_admin_running_state(struct ena_com_dev *ena_dev, bool state);
494 
495 /* ena_com_get_admin_running_state - Get the admin queue state
496  * @ena_dev: ENA communication layer struct
497  *
498  * Retrieve the state of the admin queue (enable/disable)
499  *
500  * @return - current polling mode (enable/disable)
501  */
502 bool ena_com_get_admin_running_state(struct ena_com_dev *ena_dev);
503 
504 /* ena_com_set_admin_polling_mode - Set the admin completion queue polling mode
505  * @ena_dev: ENA communication layer struct
506  * @polling: ENAble/Disable polling mode
507  *
508  * Set the admin completion mode.
509  */
510 void ena_com_set_admin_polling_mode(struct ena_com_dev *ena_dev, bool polling);
511 
512 /* ena_com_get_admin_polling_mode - Get the admin completion queue polling mode
513  * @ena_dev: ENA communication layer struct
514  *
515  * Get the admin completion mode.
516  * If polling mode is on, ena_com_execute_admin_command will perform a
517  * polling on the admin completion queue for the commands completion,
518  * otherwise it will wait on wait event.
519  *
520  * @return state
521  */
522 bool ena_com_get_admin_polling_mode(struct ena_com_dev *ena_dev);
523 
524 /* ena_com_set_admin_auto_polling_mode - Enable autoswitch to polling mode
525  * @ena_dev: ENA communication layer struct
526  * @polling: Enable/Disable polling mode
527  *
528  * Set the autopolling mode.
529  * If autopolling is on:
530  * In case of missing interrupt when data is available switch to polling.
531  */
532 void ena_com_set_admin_auto_polling_mode(struct ena_com_dev *ena_dev,
533 					 bool polling);
534 
535 /* ena_com_admin_q_comp_intr_handler - admin queue interrupt handler
536  * @ena_dev: ENA communication layer struct
537  *
538  * This method goes over the admin completion queue and wakes up all the pending
539  * threads that wait on the commands wait event.
540  *
541  * @note: Should be called after MSI-X interrupt.
542  */
543 void ena_com_admin_q_comp_intr_handler(struct ena_com_dev *ena_dev);
544 
545 /* ena_com_aenq_intr_handler - AENQ interrupt handler
546  * @ena_dev: ENA communication layer struct
547  *
548  * This method goes over the async event notification queue and calls the proper
549  * aenq handler.
550  */
551 void ena_com_aenq_intr_handler(struct ena_com_dev *ena_dev, void *data);
552 
553 /* ena_com_abort_admin_commands - Abort all the outstanding admin commands.
554  * @ena_dev: ENA communication layer struct
555  *
556  * This method aborts all the outstanding admin commands.
557  * The caller should then call ena_com_wait_for_abort_completion to make sure
558  * all the commands were completed.
559  */
560 void ena_com_abort_admin_commands(struct ena_com_dev *ena_dev);
561 
562 /* ena_com_wait_for_abort_completion - Wait for admin commands abort.
563  * @ena_dev: ENA communication layer struct
564  *
565  * This method waits until all the outstanding admin commands are completed.
566  */
567 void ena_com_wait_for_abort_completion(struct ena_com_dev *ena_dev);
568 
569 /* ena_com_validate_version - Validate the device parameters
570  * @ena_dev: ENA communication layer struct
571  *
572  * This method verifies the device parameters are the same as the saved
573  * parameters in ena_dev.
574  * This method is useful after device reset, to validate the device mac address
575  * and the device offloads are the same as before the reset.
576  *
577  * @return - 0 on success negative value otherwise.
578  */
579 int ena_com_validate_version(struct ena_com_dev *ena_dev);
580 
581 /* ena_com_get_link_params - Retrieve physical link parameters.
582  * @ena_dev: ENA communication layer struct
583  * @resp: Link parameters
584  *
585  * Retrieve the physical link parameters,
586  * like speed, auto-negotiation and full duplex support.
587  *
588  * @return - 0 on Success negative value otherwise.
589  */
590 int ena_com_get_link_params(struct ena_com_dev *ena_dev,
591 			    struct ena_admin_get_feat_resp *resp);
592 
593 /* ena_com_get_dma_width - Retrieve physical dma address width the device
594  * supports.
595  * @ena_dev: ENA communication layer struct
596  *
597  * Retrieve the maximum physical address bits the device can handle.
598  *
599  * @return: > 0 on Success and negative value otherwise.
600  */
601 int ena_com_get_dma_width(struct ena_com_dev *ena_dev);
602 
603 /* ena_com_set_aenq_config - Set aenq groups configurations
604  * @ena_dev: ENA communication layer struct
605  * @groups flag: bit fields flags of enum ena_admin_aenq_group.
606  *
607  * Configure which aenq event group the driver would like to receive.
608  *
609  * @return: 0 on Success and negative value otherwise.
610  */
611 int ena_com_set_aenq_config(struct ena_com_dev *ena_dev, u32 groups_flag);
612 
613 /* ena_com_get_dev_attr_feat - Get device features
614  * @ena_dev: ENA communication layer struct
615  * @get_feat_ctx: returned context that contain the get features.
616  *
617  * @return: 0 on Success and negative value otherwise.
618  */
619 int ena_com_get_dev_attr_feat(struct ena_com_dev *ena_dev,
620 			      struct ena_com_dev_get_features_ctx *get_feat_ctx);
621 
622 /* ena_com_get_dev_basic_stats - Get device basic statistics
623  * @ena_dev: ENA communication layer struct
624  * @stats: stats return value
625  *
626  * @return: 0 on Success and negative value otherwise.
627  */
628 int ena_com_get_dev_basic_stats(struct ena_com_dev *ena_dev,
629 				struct ena_admin_basic_stats *stats);
630 
631 /* ena_com_get_eni_stats - Get extended network interface statistics
632  * @ena_dev: ENA communication layer struct
633  * @stats: stats return value
634  *
635  * @return: 0 on Success and negative value otherwise.
636  */
637 int ena_com_get_eni_stats(struct ena_com_dev *ena_dev,
638 			  struct ena_admin_eni_stats *stats);
639 
640 /* ena_com_set_dev_mtu - Configure the device mtu.
641  * @ena_dev: ENA communication layer struct
642  * @mtu: mtu value
643  *
644  * @return: 0 on Success and negative value otherwise.
645  */
646 int ena_com_set_dev_mtu(struct ena_com_dev *ena_dev, u32 mtu);
647 
648 /* ena_com_get_offload_settings - Retrieve the device offloads capabilities
649  * @ena_dev: ENA communication layer struct
650  * @offlad: offload return value
651  *
652  * @return: 0 on Success and negative value otherwise.
653  */
654 int ena_com_get_offload_settings(struct ena_com_dev *ena_dev,
655 				 struct ena_admin_feature_offload_desc *offload);
656 
657 /* ena_com_rss_init - Init RSS
658  * @ena_dev: ENA communication layer struct
659  * @log_size: indirection log size
660  *
661  * Allocate RSS/RFS resources.
662  * The caller then can configure rss using ena_com_set_hash_function,
663  * ena_com_set_hash_ctrl and ena_com_indirect_table_set.
664  *
665  * @return: 0 on Success and negative value otherwise.
666  */
667 int ena_com_rss_init(struct ena_com_dev *ena_dev, u16 log_size);
668 
669 /* ena_com_rss_destroy - Destroy rss
670  * @ena_dev: ENA communication layer struct
671  *
672  * Free all the RSS/RFS resources.
673  */
674 void ena_com_rss_destroy(struct ena_com_dev *ena_dev);
675 
676 /* ena_com_get_current_hash_function - Get RSS hash function
677  * @ena_dev: ENA communication layer struct
678  *
679  * Return the current hash function.
680  * @return: 0 or one of the ena_admin_hash_functions values.
681  */
682 int ena_com_get_current_hash_function(struct ena_com_dev *ena_dev);
683 
684 /* ena_com_fill_hash_function - Fill RSS hash function
685  * @ena_dev: ENA communication layer struct
686  * @func: The hash function (Toeplitz or crc)
687  * @key: Hash key (for toeplitz hash)
688  * @key_len: key length (max length 10 DW)
689  * @init_val: initial value for the hash function
690  *
691  * Fill the ena_dev resources with the desire hash function, hash key, key_len
692  * and key initial value (if needed by the hash function).
693  * To flush the key into the device the caller should call
694  * ena_com_set_hash_function.
695  *
696  * @return: 0 on Success and negative value otherwise.
697  */
698 int ena_com_fill_hash_function(struct ena_com_dev *ena_dev,
699 			       enum ena_admin_hash_functions func,
700 			       const u8 *key, u16 key_len, u32 init_val);
701 
702 /* ena_com_set_hash_function - Flush the hash function and it dependencies to
703  * the device.
704  * @ena_dev: ENA communication layer struct
705  *
706  * Flush the hash function and it dependencies (key, key length and
707  * initial value) if needed.
708  *
709  * @note: Prior to this method the caller should call ena_com_fill_hash_function
710  *
711  * @return: 0 on Success and negative value otherwise.
712  */
713 int ena_com_set_hash_function(struct ena_com_dev *ena_dev);
714 
715 /* ena_com_get_hash_function - Retrieve the hash function from the device.
716  * @ena_dev: ENA communication layer struct
717  * @func: hash function
718  *
719  * Retrieve the hash function from the device.
720  *
721  * @note: If the caller called ena_com_fill_hash_function but didn't flush
722  * it to the device, the new configuration will be lost.
723  *
724  * @return: 0 on Success and negative value otherwise.
725  */
726 int ena_com_get_hash_function(struct ena_com_dev *ena_dev,
727 			      enum ena_admin_hash_functions *func);
728 
729 /* ena_com_get_hash_key - Retrieve the hash key
730  * @ena_dev: ENA communication layer struct
731  * @key: hash key
732  *
733  * Retrieve the hash key.
734  *
735  * @note: If the caller called ena_com_fill_hash_key but didn't flush
736  * it to the device, the new configuration will be lost.
737  *
738  * @return: 0 on Success and negative value otherwise.
739  */
740 int ena_com_get_hash_key(struct ena_com_dev *ena_dev, u8 *key);
741 /* ena_com_fill_hash_ctrl - Fill RSS hash control
742  * @ena_dev: ENA communication layer struct.
743  * @proto: The protocol to configure.
744  * @hash_fields: bit mask of ena_admin_flow_hash_fields
745  *
746  * Fill the ena_dev resources with the desire hash control (the ethernet
747  * fields that take part of the hash) for a specific protocol.
748  * To flush the hash control to the device, the caller should call
749  * ena_com_set_hash_ctrl.
750  *
751  * @return: 0 on Success and negative value otherwise.
752  */
753 int ena_com_fill_hash_ctrl(struct ena_com_dev *ena_dev,
754 			   enum ena_admin_flow_hash_proto proto,
755 			   u16 hash_fields);
756 
757 /* ena_com_set_hash_ctrl - Flush the hash control resources to the device.
758  * @ena_dev: ENA communication layer struct
759  *
760  * Flush the hash control (the ethernet fields that take part of the hash)
761  *
762  * @note: Prior to this method the caller should call ena_com_fill_hash_ctrl.
763  *
764  * @return: 0 on Success and negative value otherwise.
765  */
766 int ena_com_set_hash_ctrl(struct ena_com_dev *ena_dev);
767 
768 /* ena_com_get_hash_ctrl - Retrieve the hash control from the device.
769  * @ena_dev: ENA communication layer struct
770  * @proto: The protocol to retrieve.
771  * @fields: bit mask of ena_admin_flow_hash_fields.
772  *
773  * Retrieve the hash control from the device.
774  *
775  * @note: If the caller called ena_com_fill_hash_ctrl but didn't flush
776  * it to the device, the new configuration will be lost.
777  *
778  * @return: 0 on Success and negative value otherwise.
779  */
780 int ena_com_get_hash_ctrl(struct ena_com_dev *ena_dev,
781 			  enum ena_admin_flow_hash_proto proto,
782 			  u16 *fields);
783 
784 /* ena_com_set_default_hash_ctrl - Set the hash control to a default
785  * configuration.
786  * @ena_dev: ENA communication layer struct
787  *
788  * Fill the ena_dev resources with the default hash control configuration.
789  * To flush the hash control to the device, the caller should call
790  * ena_com_set_hash_ctrl.
791  *
792  * @return: 0 on Success and negative value otherwise.
793  */
794 int ena_com_set_default_hash_ctrl(struct ena_com_dev *ena_dev);
795 
796 /* ena_com_indirect_table_fill_entry - Fill a single entry in the RSS
797  * indirection table
798  * @ena_dev: ENA communication layer struct.
799  * @entry_idx - indirection table entry.
800  * @entry_value - redirection value
801  *
802  * Fill a single entry of the RSS indirection table in the ena_dev resources.
803  * To flush the indirection table to the device, the called should call
804  * ena_com_indirect_table_set.
805  *
806  * @return: 0 on Success and negative value otherwise.
807  */
808 int ena_com_indirect_table_fill_entry(struct ena_com_dev *ena_dev,
809 				      u16 entry_idx, u16 entry_value);
810 
811 /* ena_com_indirect_table_set - Flush the indirection table to the device.
812  * @ena_dev: ENA communication layer struct
813  *
814  * Flush the indirection hash control to the device.
815  * Prior to this method the caller should call ena_com_indirect_table_fill_entry
816  *
817  * @return: 0 on Success and negative value otherwise.
818  */
819 int ena_com_indirect_table_set(struct ena_com_dev *ena_dev);
820 
821 /* ena_com_indirect_table_get - Retrieve the indirection table from the device.
822  * @ena_dev: ENA communication layer struct
823  * @ind_tbl: indirection table
824  *
825  * Retrieve the RSS indirection table from the device.
826  *
827  * @note: If the caller called ena_com_indirect_table_fill_entry but didn't flush
828  * it to the device, the new configuration will be lost.
829  *
830  * @return: 0 on Success and negative value otherwise.
831  */
832 int ena_com_indirect_table_get(struct ena_com_dev *ena_dev, u32 *ind_tbl);
833 
834 /* ena_com_allocate_host_info - Allocate host info resources.
835  * @ena_dev: ENA communication layer struct
836  *
837  * @return: 0 on Success and negative value otherwise.
838  */
839 int ena_com_allocate_host_info(struct ena_com_dev *ena_dev);
840 
841 /* ena_com_allocate_debug_area - Allocate debug area.
842  * @ena_dev: ENA communication layer struct
843  * @debug_area_size - debug area size.
844  *
845  * @return: 0 on Success and negative value otherwise.
846  */
847 int ena_com_allocate_debug_area(struct ena_com_dev *ena_dev,
848 				u32 debug_area_size);
849 
850 /* ena_com_delete_debug_area - Free the debug area resources.
851  * @ena_dev: ENA communication layer struct
852  *
853  * Free the allocated debug area.
854  */
855 void ena_com_delete_debug_area(struct ena_com_dev *ena_dev);
856 
857 /* ena_com_delete_host_info - Free the host info resources.
858  * @ena_dev: ENA communication layer struct
859  *
860  * Free the allocated host info.
861  */
862 void ena_com_delete_host_info(struct ena_com_dev *ena_dev);
863 
864 /* ena_com_set_host_attributes - Update the device with the host
865  * attributes (debug area and host info) base address.
866  * @ena_dev: ENA communication layer struct
867  *
868  * @return: 0 on Success and negative value otherwise.
869  */
870 int ena_com_set_host_attributes(struct ena_com_dev *ena_dev);
871 
872 /* ena_com_create_io_cq - Create io completion queue.
873  * @ena_dev: ENA communication layer struct
874  * @io_cq - io completion queue handler
875 
876  * Create IO completion queue.
877  *
878  * @return - 0 on success, negative value on failure.
879  */
880 int ena_com_create_io_cq(struct ena_com_dev *ena_dev,
881 			 struct ena_com_io_cq *io_cq);
882 
883 /* ena_com_destroy_io_cq - Destroy io completion queue.
884  * @ena_dev: ENA communication layer struct
885  * @io_cq - io completion queue handler
886 
887  * Destroy IO completion queue.
888  *
889  * @return - 0 on success, negative value on failure.
890  */
891 int ena_com_destroy_io_cq(struct ena_com_dev *ena_dev,
892 			  struct ena_com_io_cq *io_cq);
893 
894 /* ena_com_execute_admin_command - Execute admin command
895  * @admin_queue: admin queue.
896  * @cmd: the admin command to execute.
897  * @cmd_size: the command size.
898  * @cmd_completion: command completion return value.
899  * @cmd_comp_size: command completion size.
900 
901  * Submit an admin command and then wait until the device returns a
902  * completion.
903  * The completion will be copied into cmd_comp.
904  *
905  * @return - 0 on success, negative value on failure.
906  */
907 int ena_com_execute_admin_command(struct ena_com_admin_queue *admin_queue,
908 				  struct ena_admin_aq_entry *cmd,
909 				  size_t cmd_size,
910 				  struct ena_admin_acq_entry *cmd_comp,
911 				  size_t cmd_comp_size);
912 
913 /* ena_com_init_interrupt_moderation - Init interrupt moderation
914  * @ena_dev: ENA communication layer struct
915  *
916  * @return - 0 on success, negative value on failure.
917  */
918 int ena_com_init_interrupt_moderation(struct ena_com_dev *ena_dev);
919 
920 /* ena_com_interrupt_moderation_supported - Return if interrupt moderation
921  * capability is supported by the device.
922  *
923  * @return - supported or not.
924  */
925 bool ena_com_interrupt_moderation_supported(struct ena_com_dev *ena_dev);
926 
927 /* ena_com_update_nonadaptive_moderation_interval_tx - Update the
928  * non-adaptive interval in Tx direction.
929  * @ena_dev: ENA communication layer struct
930  * @tx_coalesce_usecs: Interval in usec.
931  *
932  * @return - 0 on success, negative value on failure.
933  */
934 int ena_com_update_nonadaptive_moderation_interval_tx(struct ena_com_dev *ena_dev,
935 						      u32 tx_coalesce_usecs);
936 
937 /* ena_com_update_nonadaptive_moderation_interval_rx - Update the
938  * non-adaptive interval in Rx direction.
939  * @ena_dev: ENA communication layer struct
940  * @rx_coalesce_usecs: Interval in usec.
941  *
942  * @return - 0 on success, negative value on failure.
943  */
944 int ena_com_update_nonadaptive_moderation_interval_rx(struct ena_com_dev *ena_dev,
945 						      u32 rx_coalesce_usecs);
946 
947 /* ena_com_get_nonadaptive_moderation_interval_tx - Retrieve the
948  * non-adaptive interval in Tx direction.
949  * @ena_dev: ENA communication layer struct
950  *
951  * @return - interval in usec
952  */
953 unsigned int ena_com_get_nonadaptive_moderation_interval_tx(struct ena_com_dev *ena_dev);
954 
955 /* ena_com_get_nonadaptive_moderation_interval_rx - Retrieve the
956  * non-adaptive interval in Rx direction.
957  * @ena_dev: ENA communication layer struct
958  *
959  * @return - interval in usec
960  */
961 unsigned int ena_com_get_nonadaptive_moderation_interval_rx(struct ena_com_dev *ena_dev);
962 
963 /* ena_com_config_dev_mode - Configure the placement policy of the device.
964  * @ena_dev: ENA communication layer struct
965  * @llq_features: LLQ feature descriptor, retrieve via
966  *		   ena_com_get_dev_attr_feat.
967  * @ena_llq_config: The default driver LLQ parameters configurations
968  */
969 int ena_com_config_dev_mode(struct ena_com_dev *ena_dev,
970 			    struct ena_admin_feature_llq_desc *llq_features,
971 			    struct ena_llq_configurations *llq_default_config);
972 
973 /* ena_com_io_sq_to_ena_dev - Extract ena_com_dev using contained field io_sq.
974  * @io_sq: IO submit queue struct
975  *
976  * @return - ena_com_dev struct extracted from io_sq
977  */
978 static inline struct ena_com_dev *ena_com_io_sq_to_ena_dev(struct ena_com_io_sq *io_sq)
979 {
980 	return container_of(io_sq, struct ena_com_dev, io_sq_queues[io_sq->qid]);
981 }
982 
983 /* ena_com_io_cq_to_ena_dev - Extract ena_com_dev using contained field io_cq.
984  * @io_sq: IO submit queue struct
985  *
986  * @return - ena_com_dev struct extracted from io_sq
987  */
988 static inline struct ena_com_dev *ena_com_io_cq_to_ena_dev(struct ena_com_io_cq *io_cq)
989 {
990 	return container_of(io_cq, struct ena_com_dev, io_cq_queues[io_cq->qid]);
991 }
992 
993 static inline bool ena_com_get_adaptive_moderation_enabled(struct ena_com_dev *ena_dev)
994 {
995 	return ena_dev->adaptive_coalescing;
996 }
997 
998 static inline void ena_com_enable_adaptive_moderation(struct ena_com_dev *ena_dev)
999 {
1000 	ena_dev->adaptive_coalescing = true;
1001 }
1002 
1003 static inline void ena_com_disable_adaptive_moderation(struct ena_com_dev *ena_dev)
1004 {
1005 	ena_dev->adaptive_coalescing = false;
1006 }
1007 
1008 /* ena_com_update_intr_reg - Prepare interrupt register
1009  * @intr_reg: interrupt register to update.
1010  * @rx_delay_interval: Rx interval in usecs
1011  * @tx_delay_interval: Tx interval in usecs
1012  * @unmask: unmask enable/disable
1013  *
1014  * Prepare interrupt update register with the supplied parameters.
1015  */
1016 static inline void ena_com_update_intr_reg(struct ena_eth_io_intr_reg *intr_reg,
1017 					   u32 rx_delay_interval,
1018 					   u32 tx_delay_interval,
1019 					   bool unmask)
1020 {
1021 	intr_reg->intr_control = 0;
1022 	intr_reg->intr_control |= rx_delay_interval &
1023 		ENA_ETH_IO_INTR_REG_RX_INTR_DELAY_MASK;
1024 
1025 	intr_reg->intr_control |=
1026 		(tx_delay_interval << ENA_ETH_IO_INTR_REG_TX_INTR_DELAY_SHIFT)
1027 		& ENA_ETH_IO_INTR_REG_TX_INTR_DELAY_MASK;
1028 
1029 	if (unmask)
1030 		intr_reg->intr_control |= ENA_ETH_IO_INTR_REG_INTR_UNMASK_MASK;
1031 }
1032 
1033 static inline u8 *ena_com_get_next_bounce_buffer(struct ena_com_io_bounce_buffer_control *bounce_buf_ctrl)
1034 {
1035 	u16 size, buffers_num;
1036 	u8 *buf;
1037 
1038 	size = bounce_buf_ctrl->buffer_size;
1039 	buffers_num = bounce_buf_ctrl->buffers_num;
1040 
1041 	buf = bounce_buf_ctrl->base_buffer +
1042 		(bounce_buf_ctrl->next_to_use++ & (buffers_num - 1)) * size;
1043 
1044 	prefetchw(bounce_buf_ctrl->base_buffer +
1045 		(bounce_buf_ctrl->next_to_use & (buffers_num - 1)) * size);
1046 
1047 	return buf;
1048 }
1049 
1050 #ifdef ENA_EXTENDED_STATS
1051 int ena_com_get_dev_extended_stats(struct ena_com_dev *ena_dev, char *buff,
1052 				   u32 len);
1053 
1054 int ena_com_extended_stats_set_func_queue(struct ena_com_dev *ena_dev,
1055 					  u32 funct_queue);
1056 #endif
1057 #if defined(__cplusplus)
1058 }
1059 #endif /* __cplusplus */
1060 #endif /* !(ENA_COM) */
1061