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