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