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