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