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