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 enum ena_intr_moder_level {
105 	ENA_INTR_MODER_LOWEST = 0,
106 	ENA_INTR_MODER_LOW,
107 	ENA_INTR_MODER_MID,
108 	ENA_INTR_MODER_HIGH,
109 	ENA_INTR_MODER_HIGHEST,
110 	ENA_INTR_MAX_NUM_OF_LEVELS,
111 };
112 
113 struct ena_llq_configurations {
114 	enum ena_admin_llq_header_location llq_header_location;
115 	enum ena_admin_llq_ring_entry_size llq_ring_entry_size;
116 	enum ena_admin_llq_stride_ctrl  llq_stride_ctrl;
117 	enum ena_admin_llq_num_descs_before_header llq_num_decs_before_header;
118 	u16 llq_ring_entry_size_value;
119 };
120 
121 struct ena_intr_moder_entry {
122 	unsigned int intr_moder_interval;
123 	unsigned int pkts_per_interval;
124 	unsigned int bytes_per_interval;
125 };
126 
127 enum queue_direction {
128 	ENA_COM_IO_QUEUE_DIRECTION_TX,
129 	ENA_COM_IO_QUEUE_DIRECTION_RX
130 };
131 
132 struct ena_com_buf {
133 	dma_addr_t paddr; /**< Buffer physical address */
134 	u16 len; /**< Buffer length in bytes */
135 };
136 
137 struct ena_com_rx_buf_info {
138 	u16 len;
139 	u16 req_id;
140 };
141 
142 struct ena_com_io_desc_addr {
143 	u8 __iomem *pbuf_dev_addr; /* LLQ address */
144 	u8 *virt_addr;
145 	dma_addr_t phys_addr;
146 };
147 
148 struct ena_com_tx_meta {
149 	u16 mss;
150 	u16 l3_hdr_len;
151 	u16 l3_hdr_offset;
152 	u16 l4_hdr_len; /* In words */
153 };
154 
155 struct ena_com_llq_info {
156 	u16 header_location_ctrl;
157 	u16 desc_stride_ctrl;
158 	u16 desc_list_entry_size_ctrl;
159 	u16 desc_list_entry_size;
160 	u16 descs_num_before_header;
161 	u16 descs_per_entry;
162 };
163 
164 struct ena_com_io_cq {
165 	struct ena_com_io_desc_addr cdesc_addr;
166 
167 	/* Interrupt unmask register */
168 	u32 __iomem *unmask_reg;
169 
170 	/* The completion queue head doorbell register */
171 	u32 __iomem *cq_head_db_reg;
172 
173 	/* numa configuration register (for TPH) */
174 	u32 __iomem *numa_node_cfg_reg;
175 
176 	/* The value to write to the above register to unmask
177 	 * the interrupt of this queue
178 	 */
179 	u32 msix_vector;
180 
181 	enum queue_direction direction;
182 
183 	/* holds the number of cdesc of the current packet */
184 	u16 cur_rx_pkt_cdesc_count;
185 	/* save the firt cdesc idx of the current packet */
186 	u16 cur_rx_pkt_cdesc_start_idx;
187 
188 	u16 q_depth;
189 	/* Caller qid */
190 	u16 qid;
191 
192 	/* Device queue index */
193 	u16 idx;
194 	u16 head;
195 	u16 last_head_update;
196 	u8 phase;
197 	u8 cdesc_entry_size_in_bytes;
198 
199 } ____cacheline_aligned;
200 
201 struct ena_com_io_bounce_buffer_control {
202 	u8 *base_buffer;
203 	u16 next_to_use;
204 	u16 buffer_size;
205 	u16 buffers_num;  /* Must be a power of 2 */
206 };
207 
208 /* This struct is to keep tracking the current location of the next llq entry */
209 struct ena_com_llq_pkt_ctrl {
210 	u8 *curr_bounce_buf;
211 	u16 idx;
212 	u16 descs_left_in_line;
213 };
214 
215 struct ena_com_io_sq {
216 	struct ena_com_io_desc_addr desc_addr;
217 
218 	u32 __iomem *db_addr;
219 	u8 __iomem *header_addr;
220 
221 	enum queue_direction direction;
222 	enum ena_admin_placement_policy_type mem_queue_type;
223 
224 	u32 msix_vector;
225 	struct ena_com_tx_meta cached_tx_meta;
226 	struct ena_com_llq_info llq_info;
227 	struct ena_com_llq_pkt_ctrl llq_buf_ctrl;
228 	struct ena_com_io_bounce_buffer_control bounce_buf_ctrl;
229 
230 	u16 q_depth;
231 	u16 qid;
232 
233 	u16 idx;
234 	u16 tail;
235 	u16 next_to_comp;
236 	u16 llq_last_copy_tail;
237 	u32 tx_max_header_size;
238 	u8 phase;
239 	u8 desc_entry_size;
240 	u8 dma_addr_bits;
241 } ____cacheline_aligned;
242 
243 struct ena_com_admin_cq {
244 	struct ena_admin_acq_entry *entries;
245 	dma_addr_t dma_addr;
246 
247 	u16 head;
248 	u8 phase;
249 };
250 
251 struct ena_com_admin_sq {
252 	struct ena_admin_aq_entry *entries;
253 	dma_addr_t dma_addr;
254 
255 	u32 __iomem *db_addr;
256 
257 	u16 head;
258 	u16 tail;
259 	u8 phase;
260 
261 };
262 
263 struct ena_com_stats_admin {
264 	u32 aborted_cmd;
265 	u32 submitted_cmd;
266 	u32 completed_cmd;
267 	u32 out_of_space;
268 	u32 no_completion;
269 };
270 
271 struct ena_com_admin_queue {
272 	void *q_dmadev;
273 	spinlock_t q_lock; /* spinlock for the admin queue */
274 
275 	struct ena_comp_ctx *comp_ctx;
276 	u32 completion_timeout;
277 	u16 q_depth;
278 	struct ena_com_admin_cq cq;
279 	struct ena_com_admin_sq sq;
280 
281 	/* Indicate if the admin queue should poll for completion */
282 	bool polling;
283 
284 	u16 curr_cmd_id;
285 
286 	/* Indicate that the ena was initialized and can
287 	 * process new admin commands
288 	 */
289 	bool running_state;
290 
291 	/* Count the number of outstanding admin commands */
292 	atomic_t outstanding_cmds;
293 
294 	struct ena_com_stats_admin stats;
295 };
296 
297 struct ena_aenq_handlers;
298 
299 struct ena_com_aenq {
300 	u16 head;
301 	u8 phase;
302 	struct ena_admin_aenq_entry *entries;
303 	dma_addr_t dma_addr;
304 	u16 q_depth;
305 	struct ena_aenq_handlers *aenq_handlers;
306 };
307 
308 struct ena_com_mmio_read {
309 	struct ena_admin_ena_mmio_req_read_less_resp *read_resp;
310 	dma_addr_t read_resp_dma_addr;
311 	u32 reg_read_to; /* in us */
312 	u16 seq_num;
313 	bool readless_supported;
314 	/* spin lock to ensure a single outstanding read */
315 	spinlock_t lock;
316 };
317 
318 struct ena_rss {
319 	/* Indirect table */
320 	u16 *host_rss_ind_tbl;
321 	struct ena_admin_rss_ind_table_entry *rss_ind_tbl;
322 	dma_addr_t rss_ind_tbl_dma_addr;
323 	u16 tbl_log_size;
324 
325 	/* Hash key */
326 	enum ena_admin_hash_functions hash_func;
327 	struct ena_admin_feature_rss_flow_hash_control *hash_key;
328 	dma_addr_t hash_key_dma_addr;
329 	u32 hash_init_val;
330 
331 	/* Flow Control */
332 	struct ena_admin_feature_rss_hash_control *hash_ctrl;
333 	dma_addr_t hash_ctrl_dma_addr;
334 
335 };
336 
337 struct ena_host_attribute {
338 	/* Debug area */
339 	u8 *debug_area_virt_addr;
340 	dma_addr_t debug_area_dma_addr;
341 	u32 debug_area_size;
342 
343 	/* Host information */
344 	struct ena_admin_host_info *host_info;
345 	dma_addr_t host_info_dma_addr;
346 };
347 
348 /* Each ena_dev is a PCI function. */
349 struct ena_com_dev {
350 	struct ena_com_admin_queue admin_queue;
351 	struct ena_com_aenq aenq;
352 	struct ena_com_io_cq io_cq_queues[ENA_TOTAL_NUM_QUEUES];
353 	struct ena_com_io_sq io_sq_queues[ENA_TOTAL_NUM_QUEUES];
354 	u8 __iomem *reg_bar;
355 	void __iomem *mem_bar;
356 	void *dmadev;
357 
358 	enum ena_admin_placement_policy_type tx_mem_queue_type;
359 	u32 tx_max_header_size;
360 	u16 stats_func; /* Selected function for extended statistic dump */
361 	u16 stats_queue; /* Selected queue for extended statistic dump */
362 
363 	struct ena_com_mmio_read mmio_read;
364 
365 	struct ena_rss rss;
366 	u32 supported_features;
367 	u32 dma_addr_bits;
368 
369 	struct ena_host_attribute host_attr;
370 	bool adaptive_coalescing;
371 	u16 intr_delay_resolution;
372 	u32 intr_moder_tx_interval;
373 	struct ena_intr_moder_entry *intr_moder_tbl;
374 
375 	struct ena_com_llq_info llq_info;
376 };
377 
378 struct ena_com_dev_get_features_ctx {
379 	struct ena_admin_queue_feature_desc max_queues;
380 	struct ena_admin_device_attr_feature_desc dev_attr;
381 	struct ena_admin_feature_aenq_desc aenq;
382 	struct ena_admin_feature_offload_desc offload;
383 	struct ena_admin_ena_hw_hints hw_hints;
384 	struct ena_admin_feature_llq_desc llq;
385 };
386 
387 struct ena_com_create_io_ctx {
388 	enum ena_admin_placement_policy_type mem_queue_type;
389 	enum queue_direction direction;
390 	int numa_node;
391 	u32 msix_vector;
392 	u16 queue_size;
393 	u16 qid;
394 };
395 
396 typedef void (*ena_aenq_handler)(void *data,
397 	struct ena_admin_aenq_entry *aenq_e);
398 
399 /* Holds aenq handlers. Indexed by AENQ event group */
400 struct ena_aenq_handlers {
401 	ena_aenq_handler handlers[ENA_MAX_HANDLERS];
402 	ena_aenq_handler unimplemented_handler;
403 };
404 
405 /*****************************************************************************/
406 /*****************************************************************************/
407 
408 /* ena_com_mmio_reg_read_request_init - Init the mmio reg read mechanism
409  * @ena_dev: ENA communication layer struct
410  *
411  * Initialize the register read mechanism.
412  *
413  * @note: This method must be the first stage in the initialization sequence.
414  *
415  * @return - 0 on success, negative value on failure.
416  */
417 int ena_com_mmio_reg_read_request_init(struct ena_com_dev *ena_dev);
418 
419 /* ena_com_set_mmio_read_mode - Enable/disable the mmio reg read mechanism
420  * @ena_dev: ENA communication layer struct
421  * @readless_supported: readless mode (enable/disable)
422  */
423 void ena_com_set_mmio_read_mode(struct ena_com_dev *ena_dev,
424 				bool readless_supported);
425 
426 /* ena_com_mmio_reg_read_request_write_dev_addr - Write the mmio reg read return
427  * value physical address.
428  * @ena_dev: ENA communication layer struct
429  */
430 void ena_com_mmio_reg_read_request_write_dev_addr(struct ena_com_dev *ena_dev);
431 
432 /* ena_com_mmio_reg_read_request_destroy - Destroy the mmio reg read mechanism
433  * @ena_dev: ENA communication layer struct
434  */
435 void ena_com_mmio_reg_read_request_destroy(struct ena_com_dev *ena_dev);
436 
437 /* ena_com_admin_init - Init the admin and the async queues
438  * @ena_dev: ENA communication layer struct
439  * @aenq_handlers: Those handlers to be called upon event.
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 
449 /* ena_com_admin_destroy - Destroy the admin and the async events queues.
450  * @ena_dev: ENA communication layer struct
451  *
452  * @note: Before calling this method, the caller must validate that the device
453  * won't send any additional admin completions/aenq.
454  * To achieve that, a FLR is recommended.
455  */
456 void ena_com_admin_destroy(struct ena_com_dev *ena_dev);
457 
458 /* ena_com_dev_reset - Perform device FLR to the device.
459  * @ena_dev: ENA communication layer struct
460  * @reset_reason: Specify what is the trigger for the reset in case of an error.
461  *
462  * @return - 0 on success, negative value on failure.
463  */
464 int ena_com_dev_reset(struct ena_com_dev *ena_dev,
465 		      enum ena_regs_reset_reason_types reset_reason);
466 
467 /* ena_com_create_io_queue - Create io queue.
468  * @ena_dev: ENA communication layer struct
469  * @ctx - create context structure
470  *
471  * Create the submission and the completion queues.
472  *
473  * @return - 0 on success, negative value on failure.
474  */
475 int ena_com_create_io_queue(struct ena_com_dev *ena_dev,
476 			    struct ena_com_create_io_ctx *ctx);
477 
478 /* ena_com_destroy_io_queue - Destroy IO queue with the queue id - qid.
479  * @ena_dev: ENA communication layer struct
480  * @qid - the caller virtual queue id.
481  */
482 void ena_com_destroy_io_queue(struct ena_com_dev *ena_dev, u16 qid);
483 
484 /* ena_com_get_io_handlers - Return the io queue handlers
485  * @ena_dev: ENA communication layer struct
486  * @qid - the caller virtual queue id.
487  * @io_sq - IO submission queue handler
488  * @io_cq - IO completion queue handler.
489  *
490  * @return - 0 on success, negative value on failure.
491  */
492 int ena_com_get_io_handlers(struct ena_com_dev *ena_dev, u16 qid,
493 			    struct ena_com_io_sq **io_sq,
494 			    struct ena_com_io_cq **io_cq);
495 
496 /* ena_com_admin_aenq_enable - ENAble asynchronous event notifications
497  * @ena_dev: ENA communication layer struct
498  *
499  * After this method, aenq event can be received via AENQ.
500  */
501 void ena_com_admin_aenq_enable(struct ena_com_dev *ena_dev);
502 
503 /* ena_com_set_admin_running_state - Set the state of the admin queue
504  * @ena_dev: ENA communication layer struct
505  *
506  * Change the state of the admin queue (enable/disable)
507  */
508 void ena_com_set_admin_running_state(struct ena_com_dev *ena_dev, bool state);
509 
510 /* ena_com_get_admin_running_state - Get the admin queue state
511  * @ena_dev: ENA communication layer struct
512  *
513  * Retrieve the state of the admin queue (enable/disable)
514  *
515  * @return - current polling mode (enable/disable)
516  */
517 bool ena_com_get_admin_running_state(struct ena_com_dev *ena_dev);
518 
519 /* ena_com_set_admin_polling_mode - Set the admin completion queue polling mode
520  * @ena_dev: ENA communication layer struct
521  * @polling: ENAble/Disable polling mode
522  *
523  * Set the admin completion mode.
524  */
525 void ena_com_set_admin_polling_mode(struct ena_com_dev *ena_dev, bool polling);
526 
527 /* ena_com_set_admin_polling_mode - Get the admin completion queue polling mode
528  * @ena_dev: ENA communication layer struct
529  *
530  * Get the admin completion mode.
531  * If polling mode is on, ena_com_execute_admin_command will perform a
532  * polling on the admin completion queue for the commands completion,
533  * otherwise it will wait on wait event.
534  *
535  * @return state
536  */
537 bool ena_com_get_ena_admin_polling_mode(struct ena_com_dev *ena_dev);
538 
539 /* ena_com_admin_q_comp_intr_handler - admin queue interrupt handler
540  * @ena_dev: ENA communication layer struct
541  *
542  * This method go over the admin completion queue and wake up all the pending
543  * threads that wait on the commands wait event.
544  *
545  * @note: Should be called after MSI-X interrupt.
546  */
547 void ena_com_admin_q_comp_intr_handler(struct ena_com_dev *ena_dev);
548 
549 /* ena_com_aenq_intr_handler - AENQ interrupt handler
550  * @ena_dev: ENA communication layer struct
551  *
552  * This method go over the async event notification queue and call the proper
553  * aenq handler.
554  */
555 void ena_com_aenq_intr_handler(struct ena_com_dev *dev, void *data);
556 
557 /* ena_com_abort_admin_commands - Abort all the outstanding admin commands.
558  * @ena_dev: ENA communication layer struct
559  *
560  * This method aborts all the outstanding admin commands.
561  * The caller should then call ena_com_wait_for_abort_completion to make sure
562  * all the commands were completed.
563  */
564 void ena_com_abort_admin_commands(struct ena_com_dev *ena_dev);
565 
566 /* ena_com_wait_for_abort_completion - Wait for admin commands abort.
567  * @ena_dev: ENA communication layer struct
568  *
569  * This method wait until all the outstanding admin commands will be completed.
570  */
571 void ena_com_wait_for_abort_completion(struct ena_com_dev *ena_dev);
572 
573 /* ena_com_validate_version - Validate the device parameters
574  * @ena_dev: ENA communication layer struct
575  *
576  * This method validate the device parameters are the same as the saved
577  * parameters in ena_dev.
578  * This method is useful after device reset, to validate the device mac address
579  * and the device offloads are the same as before the reset.
580  *
581  * @return - 0 on success negative value otherwise.
582  */
583 int ena_com_validate_version(struct ena_com_dev *ena_dev);
584 
585 /* ena_com_get_link_params - Retrieve physical link parameters.
586  * @ena_dev: ENA communication layer struct
587  * @resp: Link parameters
588  *
589  * Retrieve the physical link parameters,
590  * like speed, auto-negotiation and full duplex support.
591  *
592  * @return - 0 on Success negative value otherwise.
593  */
594 int ena_com_get_link_params(struct ena_com_dev *ena_dev,
595 			    struct ena_admin_get_feat_resp *resp);
596 
597 /* ena_com_get_dma_width - Retrieve physical dma address width the device
598  * supports.
599  * @ena_dev: ENA communication layer struct
600  *
601  * Retrieve the maximum physical address bits the device can handle.
602  *
603  * @return: > 0 on Success and negative value otherwise.
604  */
605 int ena_com_get_dma_width(struct ena_com_dev *ena_dev);
606 
607 /* ena_com_set_aenq_config - Set aenq groups configurations
608  * @ena_dev: ENA communication layer struct
609  * @groups flag: bit fields flags of enum ena_admin_aenq_group.
610  *
611  * Configure which aenq event group the driver would like to receive.
612  *
613  * @return: 0 on Success and negative value otherwise.
614  */
615 int ena_com_set_aenq_config(struct ena_com_dev *ena_dev, u32 groups_flag);
616 
617 /* ena_com_get_dev_attr_feat - Get device features
618  * @ena_dev: ENA communication layer struct
619  * @get_feat_ctx: returned context that contain the get features.
620  *
621  * @return: 0 on Success and negative value otherwise.
622  */
623 int ena_com_get_dev_attr_feat(struct ena_com_dev *ena_dev,
624 			      struct ena_com_dev_get_features_ctx *get_feat_ctx);
625 
626 /* ena_com_get_dev_basic_stats - Get device basic statistics
627  * @ena_dev: ENA communication layer struct
628  * @stats: stats return value
629  *
630  * @return: 0 on Success and negative value otherwise.
631  */
632 int ena_com_get_dev_basic_stats(struct ena_com_dev *ena_dev,
633 				struct ena_admin_basic_stats *stats);
634 
635 /* ena_com_set_dev_mtu - Configure the device mtu.
636  * @ena_dev: ENA communication layer struct
637  * @mtu: mtu value
638  *
639  * @return: 0 on Success and negative value otherwise.
640  */
641 int ena_com_set_dev_mtu(struct ena_com_dev *ena_dev, int mtu);
642 
643 /* ena_com_get_offload_settings - Retrieve the device offloads capabilities
644  * @ena_dev: ENA communication layer struct
645  * @offlad: offload return value
646  *
647  * @return: 0 on Success and negative value otherwise.
648  */
649 int ena_com_get_offload_settings(struct ena_com_dev *ena_dev,
650 				 struct ena_admin_feature_offload_desc *offload);
651 
652 /* ena_com_rss_init - Init RSS
653  * @ena_dev: ENA communication layer struct
654  * @log_size: indirection log size
655  *
656  * Allocate RSS/RFS resources.
657  * The caller then can configure rss using ena_com_set_hash_function,
658  * ena_com_set_hash_ctrl and ena_com_indirect_table_set.
659  *
660  * @return: 0 on Success and negative value otherwise.
661  */
662 int ena_com_rss_init(struct ena_com_dev *ena_dev, u16 log_size);
663 
664 /* ena_com_rss_destroy - Destroy rss
665  * @ena_dev: ENA communication layer struct
666  *
667  * Free all the RSS/RFS resources.
668  */
669 void ena_com_rss_destroy(struct ena_com_dev *ena_dev);
670 
671 /* ena_com_fill_hash_function - Fill RSS hash function
672  * @ena_dev: ENA communication layer struct
673  * @func: The hash function (Toeplitz or crc)
674  * @key: Hash key (for toeplitz hash)
675  * @key_len: key length (max length 10 DW)
676  * @init_val: initial value for the hash function
677  *
678  * Fill the ena_dev resources with the desire hash function, hash key, key_len
679  * and key initial value (if needed by the hash function).
680  * To flush the key into the device the caller should call
681  * ena_com_set_hash_function.
682  *
683  * @return: 0 on Success and negative value otherwise.
684  */
685 int ena_com_fill_hash_function(struct ena_com_dev *ena_dev,
686 			       enum ena_admin_hash_functions func,
687 			       const u8 *key, u16 key_len, u32 init_val);
688 
689 /* ena_com_set_hash_function - Flush the hash function and it dependencies to
690  * the device.
691  * @ena_dev: ENA communication layer struct
692  *
693  * Flush the hash function and it dependencies (key, key length and
694  * initial value) if needed.
695  *
696  * @note: Prior to this method the caller should call ena_com_fill_hash_function
697  *
698  * @return: 0 on Success and negative value otherwise.
699  */
700 int ena_com_set_hash_function(struct ena_com_dev *ena_dev);
701 
702 /* ena_com_get_hash_function - Retrieve the hash function and the hash key
703  * from the device.
704  * @ena_dev: ENA communication layer struct
705  * @func: hash function
706  * @key: hash key
707  *
708  * Retrieve the hash function and the hash key from the device.
709  *
710  * @note: If the caller called ena_com_fill_hash_function but didn't flash
711  * it to the device, the new configuration will be lost.
712  *
713  * @return: 0 on Success and negative value otherwise.
714  */
715 int ena_com_get_hash_function(struct ena_com_dev *ena_dev,
716 			      enum ena_admin_hash_functions *func,
717 			      u8 *key);
718 
719 /* ena_com_fill_hash_ctrl - Fill RSS hash control
720  * @ena_dev: ENA communication layer struct.
721  * @proto: The protocol to configure.
722  * @hash_fields: bit mask of ena_admin_flow_hash_fields
723  *
724  * Fill the ena_dev resources with the desire hash control (the ethernet
725  * fields that take part of the hash) for a specific protocol.
726  * To flush the hash control to the device, the caller should call
727  * ena_com_set_hash_ctrl.
728  *
729  * @return: 0 on Success and negative value otherwise.
730  */
731 int ena_com_fill_hash_ctrl(struct ena_com_dev *ena_dev,
732 			   enum ena_admin_flow_hash_proto proto,
733 			   u16 hash_fields);
734 
735 /* ena_com_set_hash_ctrl - Flush the hash control resources to the device.
736  * @ena_dev: ENA communication layer struct
737  *
738  * Flush the hash control (the ethernet fields that take part of the hash)
739  *
740  * @note: Prior to this method the caller should call ena_com_fill_hash_ctrl.
741  *
742  * @return: 0 on Success and negative value otherwise.
743  */
744 int ena_com_set_hash_ctrl(struct ena_com_dev *ena_dev);
745 
746 /* ena_com_get_hash_ctrl - Retrieve the hash control from the device.
747  * @ena_dev: ENA communication layer struct
748  * @proto: The protocol to retrieve.
749  * @fields: bit mask of ena_admin_flow_hash_fields.
750  *
751  * Retrieve the hash control from the device.
752  *
753  * @note, If the caller called ena_com_fill_hash_ctrl but didn't flash
754  * it to the device, the new configuration will be lost.
755  *
756  * @return: 0 on Success and negative value otherwise.
757  */
758 int ena_com_get_hash_ctrl(struct ena_com_dev *ena_dev,
759 			  enum ena_admin_flow_hash_proto proto,
760 			  u16 *fields);
761 
762 /* ena_com_set_default_hash_ctrl - Set the hash control to a default
763  * configuration.
764  * @ena_dev: ENA communication layer struct
765  *
766  * Fill the ena_dev resources with the default hash control configuration.
767  * To flush the hash control to the device, the caller should call
768  * ena_com_set_hash_ctrl.
769  *
770  * @return: 0 on Success and negative value otherwise.
771  */
772 int ena_com_set_default_hash_ctrl(struct ena_com_dev *ena_dev);
773 
774 /* ena_com_indirect_table_fill_entry - Fill a single entry in the RSS
775  * indirection table
776  * @ena_dev: ENA communication layer struct.
777  * @entry_idx - indirection table entry.
778  * @entry_value - redirection value
779  *
780  * Fill a single entry of the RSS indirection table in the ena_dev resources.
781  * To flush the indirection table to the device, the called should call
782  * ena_com_indirect_table_set.
783  *
784  * @return: 0 on Success and negative value otherwise.
785  */
786 int ena_com_indirect_table_fill_entry(struct ena_com_dev *ena_dev,
787 				      u16 entry_idx, u16 entry_value);
788 
789 /* ena_com_indirect_table_set - Flush the indirection table to the device.
790  * @ena_dev: ENA communication layer struct
791  *
792  * Flush the indirection hash control to the device.
793  * Prior to this method the caller should call ena_com_indirect_table_fill_entry
794  *
795  * @return: 0 on Success and negative value otherwise.
796  */
797 int ena_com_indirect_table_set(struct ena_com_dev *ena_dev);
798 
799 /* ena_com_indirect_table_get - Retrieve the indirection table from the device.
800  * @ena_dev: ENA communication layer struct
801  * @ind_tbl: indirection table
802  *
803  * Retrieve the RSS indirection table from the device.
804  *
805  * @note: If the caller called ena_com_indirect_table_fill_entry but didn't flash
806  * it to the device, the new configuration will be lost.
807  *
808  * @return: 0 on Success and negative value otherwise.
809  */
810 int ena_com_indirect_table_get(struct ena_com_dev *ena_dev, u32 *ind_tbl);
811 
812 /* ena_com_allocate_host_info - Allocate host info resources.
813  * @ena_dev: ENA communication layer struct
814  *
815  * @return: 0 on Success and negative value otherwise.
816  */
817 int ena_com_allocate_host_info(struct ena_com_dev *ena_dev);
818 
819 /* ena_com_allocate_debug_area - Allocate debug area.
820  * @ena_dev: ENA communication layer struct
821  * @debug_area_size - debug area size.
822  *
823  * @return: 0 on Success and negative value otherwise.
824  */
825 int ena_com_allocate_debug_area(struct ena_com_dev *ena_dev,
826 				u32 debug_area_size);
827 
828 /* ena_com_delete_debug_area - Free the debug area resources.
829  * @ena_dev: ENA communication layer struct
830  *
831  * Free the allocate debug area.
832  */
833 void ena_com_delete_debug_area(struct ena_com_dev *ena_dev);
834 
835 /* ena_com_delete_host_info - Free the host info resources.
836  * @ena_dev: ENA communication layer struct
837  *
838  * Free the allocate host info.
839  */
840 void ena_com_delete_host_info(struct ena_com_dev *ena_dev);
841 
842 /* ena_com_set_host_attributes - Update the device with the host
843  * attributes (debug area and host info) base address.
844  * @ena_dev: ENA communication layer struct
845  *
846  * @return: 0 on Success and negative value otherwise.
847  */
848 int ena_com_set_host_attributes(struct ena_com_dev *ena_dev);
849 
850 /* ena_com_create_io_cq - Create io completion queue.
851  * @ena_dev: ENA communication layer struct
852  * @io_cq - io completion queue handler
853 
854  * Create IO completion queue.
855  *
856  * @return - 0 on success, negative value on failure.
857  */
858 int ena_com_create_io_cq(struct ena_com_dev *ena_dev,
859 			 struct ena_com_io_cq *io_cq);
860 
861 /* ena_com_destroy_io_cq - Destroy io completion queue.
862  * @ena_dev: ENA communication layer struct
863  * @io_cq - io completion queue handler
864 
865  * Destroy IO completion queue.
866  *
867  * @return - 0 on success, negative value on failure.
868  */
869 int ena_com_destroy_io_cq(struct ena_com_dev *ena_dev,
870 			  struct ena_com_io_cq *io_cq);
871 
872 /* ena_com_execute_admin_command - Execute admin command
873  * @admin_queue: admin queue.
874  * @cmd: the admin command to execute.
875  * @cmd_size: the command size.
876  * @cmd_completion: command completion return value.
877  * @cmd_comp_size: command completion size.
878 
879  * Submit an admin command and then wait until the device will return a
880  * completion.
881  * The completion will be copyed into cmd_comp.
882  *
883  * @return - 0 on success, negative value on failure.
884  */
885 int ena_com_execute_admin_command(struct ena_com_admin_queue *admin_queue,
886 				  struct ena_admin_aq_entry *cmd,
887 				  size_t cmd_size,
888 				  struct ena_admin_acq_entry *cmd_comp,
889 				  size_t cmd_comp_size);
890 
891 /* ena_com_init_interrupt_moderation - Init interrupt moderation
892  * @ena_dev: ENA communication layer struct
893  *
894  * @return - 0 on success, negative value on failure.
895  */
896 int ena_com_init_interrupt_moderation(struct ena_com_dev *ena_dev);
897 
898 /* ena_com_destroy_interrupt_moderation - Destroy interrupt moderation resources
899  * @ena_dev: ENA communication layer struct
900  */
901 void ena_com_destroy_interrupt_moderation(struct ena_com_dev *ena_dev);
902 
903 /* ena_com_interrupt_moderation_supported - Return if interrupt moderation
904  * capability is supported by the device.
905  *
906  * @return - supported or not.
907  */
908 bool ena_com_interrupt_moderation_supported(struct ena_com_dev *ena_dev);
909 
910 /* ena_com_config_default_interrupt_moderation_table - Restore the interrupt
911  * moderation table back to the default parameters.
912  * @ena_dev: ENA communication layer struct
913  */
914 void ena_com_config_default_interrupt_moderation_table(struct ena_com_dev *ena_dev);
915 
916 /* ena_com_update_nonadaptive_moderation_interval_tx - Update the
917  * non-adaptive interval in Tx direction.
918  * @ena_dev: ENA communication layer struct
919  * @tx_coalesce_usecs: Interval in usec.
920  *
921  * @return - 0 on success, negative value on failure.
922  */
923 int ena_com_update_nonadaptive_moderation_interval_tx(struct ena_com_dev *ena_dev,
924 						      u32 tx_coalesce_usecs);
925 
926 /* ena_com_update_nonadaptive_moderation_interval_rx - Update the
927  * non-adaptive interval in Rx direction.
928  * @ena_dev: ENA communication layer struct
929  * @rx_coalesce_usecs: Interval in usec.
930  *
931  * @return - 0 on success, negative value on failure.
932  */
933 int ena_com_update_nonadaptive_moderation_interval_rx(struct ena_com_dev *ena_dev,
934 						      u32 rx_coalesce_usecs);
935 
936 /* ena_com_get_nonadaptive_moderation_interval_tx - Retrieve the
937  * non-adaptive interval in Tx direction.
938  * @ena_dev: ENA communication layer struct
939  *
940  * @return - interval in usec
941  */
942 unsigned int ena_com_get_nonadaptive_moderation_interval_tx(struct ena_com_dev *ena_dev);
943 
944 /* ena_com_get_nonadaptive_moderation_interval_rx - Retrieve the
945  * non-adaptive interval in Rx direction.
946  * @ena_dev: ENA communication layer struct
947  *
948  * @return - interval in usec
949  */
950 unsigned int ena_com_get_nonadaptive_moderation_interval_rx(struct ena_com_dev *ena_dev);
951 
952 /* ena_com_init_intr_moderation_entry - Update a single entry in the interrupt
953  * moderation table.
954  * @ena_dev: ENA communication layer struct
955  * @level: Interrupt moderation table level
956  * @entry: Entry value
957  *
958  * Update a single entry in the interrupt moderation table.
959  */
960 void ena_com_init_intr_moderation_entry(struct ena_com_dev *ena_dev,
961 					enum ena_intr_moder_level level,
962 					struct ena_intr_moder_entry *entry);
963 
964 /* ena_com_get_intr_moderation_entry - Init ena_intr_moder_entry.
965  * @ena_dev: ENA communication layer struct
966  * @level: Interrupt moderation table level
967  * @entry: Entry to fill.
968  *
969  * Initialize the entry according to the adaptive interrupt moderation table.
970  */
971 void ena_com_get_intr_moderation_entry(struct ena_com_dev *ena_dev,
972 				       enum ena_intr_moder_level level,
973 				       struct ena_intr_moder_entry *entry);
974 
975 /* ena_com_config_dev_mode - Configure the placement policy of the device.
976  * @ena_dev: ENA communication layer struct
977  * @llq_features: LLQ feature descriptor, retrieve via
978  *                ena_com_get_dev_attr_feat.
979  * @ena_llq_config: The default driver LLQ parameters configurations
980  */
981 int ena_com_config_dev_mode(struct ena_com_dev *ena_dev,
982 			    struct ena_admin_feature_llq_desc *llq_features,
983 			    struct ena_llq_configurations *llq_default_config);
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 		pr_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 	prefetchw(bounce_buf_ctrl->base_buffer +
1106 		(bounce_buf_ctrl->next_to_use & (buffers_num - 1)) * size);
1107 
1108 	return buf;
1109 }
1110 
1111 #endif /* !(ENA_COM) */
1112