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