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