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 #include <linux/netdevice.h>
48 
49 #include "ena_common_defs.h"
50 #include "ena_admin_defs.h"
51 #include "ena_eth_io_defs.h"
52 #include "ena_regs_defs.h"
53 
54 #undef pr_fmt
55 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56 
57 #define ENA_MAX_NUM_IO_QUEUES 128U
58 /* We need to queues for each IO (on for Tx and one for Rx) */
59 #define ENA_TOTAL_NUM_QUEUES (2 * (ENA_MAX_NUM_IO_QUEUES))
60 
61 #define ENA_MAX_HANDLERS 256
62 
63 #define ENA_MAX_PHYS_ADDR_SIZE_BITS 48
64 
65 /* Unit in usec */
66 #define ENA_REG_READ_TIMEOUT 200000
67 
68 #define ADMIN_SQ_SIZE(depth)	((depth) * sizeof(struct ena_admin_aq_entry))
69 #define ADMIN_CQ_SIZE(depth)	((depth) * sizeof(struct ena_admin_acq_entry))
70 #define ADMIN_AENQ_SIZE(depth)	((depth) * sizeof(struct ena_admin_aenq_entry))
71 
72 /*****************************************************************************/
73 /*****************************************************************************/
74 /* ENA adaptive interrupt moderation settings */
75 
76 #define ENA_INTR_INITIAL_TX_INTERVAL_USECS 64
77 #define ENA_INTR_INITIAL_RX_INTERVAL_USECS 0
78 #define ENA_DEFAULT_INTR_DELAY_RESOLUTION 1
79 
80 #define ENA_HASH_KEY_SIZE 40
81 
82 #define ENA_HW_HINTS_NO_TIMEOUT	0xFFFF
83 
84 #define ENA_FEATURE_MAX_QUEUE_EXT_VER 1
85 
86 struct ena_llq_configurations {
87 	enum ena_admin_llq_header_location llq_header_location;
88 	enum ena_admin_llq_ring_entry_size llq_ring_entry_size;
89 	enum ena_admin_llq_stride_ctrl  llq_stride_ctrl;
90 	enum ena_admin_llq_num_descs_before_header llq_num_decs_before_header;
91 	u16 llq_ring_entry_size_value;
92 };
93 
94 enum queue_direction {
95 	ENA_COM_IO_QUEUE_DIRECTION_TX,
96 	ENA_COM_IO_QUEUE_DIRECTION_RX
97 };
98 
99 struct ena_com_buf {
100 	dma_addr_t paddr; /**< Buffer physical address */
101 	u16 len; /**< Buffer length in bytes */
102 };
103 
104 struct ena_com_rx_buf_info {
105 	u16 len;
106 	u16 req_id;
107 };
108 
109 struct ena_com_io_desc_addr {
110 	u8 __iomem *pbuf_dev_addr; /* LLQ address */
111 	u8 *virt_addr;
112 	dma_addr_t phys_addr;
113 };
114 
115 struct ena_com_tx_meta {
116 	u16 mss;
117 	u16 l3_hdr_len;
118 	u16 l3_hdr_offset;
119 	u16 l4_hdr_len; /* In words */
120 };
121 
122 struct ena_com_llq_info {
123 	u16 header_location_ctrl;
124 	u16 desc_stride_ctrl;
125 	u16 desc_list_entry_size_ctrl;
126 	u16 desc_list_entry_size;
127 	u16 descs_num_before_header;
128 	u16 descs_per_entry;
129 	u16 max_entries_in_tx_burst;
130 	bool disable_meta_caching;
131 };
132 
133 struct ena_com_io_cq {
134 	struct ena_com_io_desc_addr cdesc_addr;
135 
136 	/* Interrupt unmask register */
137 	u32 __iomem *unmask_reg;
138 
139 	/* The completion queue head doorbell register */
140 	u32 __iomem *cq_head_db_reg;
141 
142 	/* numa configuration register (for TPH) */
143 	u32 __iomem *numa_node_cfg_reg;
144 
145 	/* The value to write to the above register to unmask
146 	 * the interrupt of this queue
147 	 */
148 	u32 msix_vector;
149 
150 	enum queue_direction direction;
151 
152 	/* holds the number of cdesc of the current packet */
153 	u16 cur_rx_pkt_cdesc_count;
154 	/* save the firt cdesc idx of the current packet */
155 	u16 cur_rx_pkt_cdesc_start_idx;
156 
157 	u16 q_depth;
158 	/* Caller qid */
159 	u16 qid;
160 
161 	/* Device queue index */
162 	u16 idx;
163 	u16 head;
164 	u16 last_head_update;
165 	u8 phase;
166 	u8 cdesc_entry_size_in_bytes;
167 
168 } ____cacheline_aligned;
169 
170 struct ena_com_io_bounce_buffer_control {
171 	u8 *base_buffer;
172 	u16 next_to_use;
173 	u16 buffer_size;
174 	u16 buffers_num;  /* Must be a power of 2 */
175 };
176 
177 /* This struct is to keep tracking the current location of the next llq entry */
178 struct ena_com_llq_pkt_ctrl {
179 	u8 *curr_bounce_buf;
180 	u16 idx;
181 	u16 descs_left_in_line;
182 };
183 
184 struct ena_com_io_sq {
185 	struct ena_com_io_desc_addr desc_addr;
186 
187 	u32 __iomem *db_addr;
188 	u8 __iomem *header_addr;
189 
190 	enum queue_direction direction;
191 	enum ena_admin_placement_policy_type mem_queue_type;
192 
193 	bool disable_meta_caching;
194 
195 	u32 msix_vector;
196 	struct ena_com_tx_meta cached_tx_meta;
197 	struct ena_com_llq_info llq_info;
198 	struct ena_com_llq_pkt_ctrl llq_buf_ctrl;
199 	struct ena_com_io_bounce_buffer_control bounce_buf_ctrl;
200 
201 	u16 q_depth;
202 	u16 qid;
203 
204 	u16 idx;
205 	u16 tail;
206 	u16 next_to_comp;
207 	u16 llq_last_copy_tail;
208 	u32 tx_max_header_size;
209 	u8 phase;
210 	u8 desc_entry_size;
211 	u8 dma_addr_bits;
212 	u16 entries_in_tx_burst_left;
213 } ____cacheline_aligned;
214 
215 struct ena_com_admin_cq {
216 	struct ena_admin_acq_entry *entries;
217 	dma_addr_t dma_addr;
218 
219 	u16 head;
220 	u8 phase;
221 };
222 
223 struct ena_com_admin_sq {
224 	struct ena_admin_aq_entry *entries;
225 	dma_addr_t dma_addr;
226 
227 	u32 __iomem *db_addr;
228 
229 	u16 head;
230 	u16 tail;
231 	u8 phase;
232 
233 };
234 
235 struct ena_com_stats_admin {
236 	u64 aborted_cmd;
237 	u64 submitted_cmd;
238 	u64 completed_cmd;
239 	u64 out_of_space;
240 	u64 no_completion;
241 };
242 
243 struct ena_com_admin_queue {
244 	void *q_dmadev;
245 	struct ena_com_dev *ena_dev;
246 	spinlock_t q_lock; /* spinlock for the admin queue */
247 
248 	struct ena_comp_ctx *comp_ctx;
249 	u32 completion_timeout;
250 	u16 q_depth;
251 	struct ena_com_admin_cq cq;
252 	struct ena_com_admin_sq sq;
253 
254 	/* Indicate if the admin queue should poll for completion */
255 	bool polling;
256 
257 	/* Define if fallback to polling mode should occur */
258 	bool auto_polling;
259 
260 	u16 curr_cmd_id;
261 
262 	/* Indicate that the ena was initialized and can
263 	 * process new admin commands
264 	 */
265 	bool running_state;
266 
267 	/* Count the number of outstanding admin commands */
268 	atomic_t outstanding_cmds;
269 
270 	struct ena_com_stats_admin stats;
271 };
272 
273 struct ena_aenq_handlers;
274 
275 struct ena_com_aenq {
276 	u16 head;
277 	u8 phase;
278 	struct ena_admin_aenq_entry *entries;
279 	dma_addr_t dma_addr;
280 	u16 q_depth;
281 	struct ena_aenq_handlers *aenq_handlers;
282 };
283 
284 struct ena_com_mmio_read {
285 	struct ena_admin_ena_mmio_req_read_less_resp *read_resp;
286 	dma_addr_t read_resp_dma_addr;
287 	u32 reg_read_to; /* in us */
288 	u16 seq_num;
289 	bool readless_supported;
290 	/* spin lock to ensure a single outstanding read */
291 	spinlock_t lock;
292 };
293 
294 struct ena_rss {
295 	/* Indirect table */
296 	u16 *host_rss_ind_tbl;
297 	struct ena_admin_rss_ind_table_entry *rss_ind_tbl;
298 	dma_addr_t rss_ind_tbl_dma_addr;
299 	u16 tbl_log_size;
300 
301 	/* Hash key */
302 	enum ena_admin_hash_functions hash_func;
303 	struct ena_admin_feature_rss_flow_hash_control *hash_key;
304 	dma_addr_t hash_key_dma_addr;
305 	u32 hash_init_val;
306 
307 	/* Flow Control */
308 	struct ena_admin_feature_rss_hash_control *hash_ctrl;
309 	dma_addr_t hash_ctrl_dma_addr;
310 
311 };
312 
313 struct ena_host_attribute {
314 	/* Debug area */
315 	u8 *debug_area_virt_addr;
316 	dma_addr_t debug_area_dma_addr;
317 	u32 debug_area_size;
318 
319 	/* Host information */
320 	struct ena_admin_host_info *host_info;
321 	dma_addr_t host_info_dma_addr;
322 };
323 
324 /* Each ena_dev is a PCI function. */
325 struct ena_com_dev {
326 	struct ena_com_admin_queue admin_queue;
327 	struct ena_com_aenq aenq;
328 	struct ena_com_io_cq io_cq_queues[ENA_TOTAL_NUM_QUEUES];
329 	struct ena_com_io_sq io_sq_queues[ENA_TOTAL_NUM_QUEUES];
330 	u8 __iomem *reg_bar;
331 	void __iomem *mem_bar;
332 	void *dmadev;
333 
334 	enum ena_admin_placement_policy_type tx_mem_queue_type;
335 	u32 tx_max_header_size;
336 	u16 stats_func; /* Selected function for extended statistic dump */
337 	u16 stats_queue; /* Selected queue for extended statistic dump */
338 
339 	struct ena_com_mmio_read mmio_read;
340 
341 	struct ena_rss rss;
342 	u32 supported_features;
343 	u32 dma_addr_bits;
344 
345 	struct ena_host_attribute host_attr;
346 	bool adaptive_coalescing;
347 	u16 intr_delay_resolution;
348 
349 	/* interrupt moderation intervals are in usec divided by
350 	 * intr_delay_resolution, which is supplied by the device.
351 	 */
352 	u32 intr_moder_tx_interval;
353 	u32 intr_moder_rx_interval;
354 
355 	struct ena_intr_moder_entry *intr_moder_tbl;
356 
357 	struct ena_com_llq_info llq_info;
358 
359 	u32 ena_min_poll_delay_us;
360 };
361 
362 struct ena_com_dev_get_features_ctx {
363 	struct ena_admin_queue_feature_desc max_queues;
364 	struct ena_admin_queue_ext_feature_desc max_queue_ext;
365 	struct ena_admin_device_attr_feature_desc dev_attr;
366 	struct ena_admin_feature_aenq_desc aenq;
367 	struct ena_admin_feature_offload_desc offload;
368 	struct ena_admin_ena_hw_hints hw_hints;
369 	struct ena_admin_feature_llq_desc llq;
370 };
371 
372 struct ena_com_create_io_ctx {
373 	enum ena_admin_placement_policy_type mem_queue_type;
374 	enum queue_direction direction;
375 	int numa_node;
376 	u32 msix_vector;
377 	u16 queue_size;
378 	u16 qid;
379 };
380 
381 typedef void (*ena_aenq_handler)(void *data,
382 	struct ena_admin_aenq_entry *aenq_e);
383 
384 /* Holds aenq handlers. Indexed by AENQ event group */
385 struct ena_aenq_handlers {
386 	ena_aenq_handler handlers[ENA_MAX_HANDLERS];
387 	ena_aenq_handler unimplemented_handler;
388 };
389 
390 /*****************************************************************************/
391 /*****************************************************************************/
392 
393 /* ena_com_mmio_reg_read_request_init - Init the mmio reg read mechanism
394  * @ena_dev: ENA communication layer struct
395  *
396  * Initialize the register read mechanism.
397  *
398  * @note: This method must be the first stage in the initialization sequence.
399  *
400  * @return - 0 on success, negative value on failure.
401  */
402 int ena_com_mmio_reg_read_request_init(struct ena_com_dev *ena_dev);
403 
404 /* ena_com_set_mmio_read_mode - Enable/disable the indirect mmio reg read mechanism
405  * @ena_dev: ENA communication layer struct
406  * @readless_supported: readless mode (enable/disable)
407  */
408 void ena_com_set_mmio_read_mode(struct ena_com_dev *ena_dev,
409 				bool readless_supported);
410 
411 /* ena_com_mmio_reg_read_request_write_dev_addr - Write the mmio reg read return
412  * value physical address.
413  * @ena_dev: ENA communication layer struct
414  */
415 void ena_com_mmio_reg_read_request_write_dev_addr(struct ena_com_dev *ena_dev);
416 
417 /* ena_com_mmio_reg_read_request_destroy - Destroy the mmio reg read mechanism
418  * @ena_dev: ENA communication layer struct
419  */
420 void ena_com_mmio_reg_read_request_destroy(struct ena_com_dev *ena_dev);
421 
422 /* ena_com_admin_init - Init the admin and the async queues
423  * @ena_dev: ENA communication layer struct
424  * @aenq_handlers: Those handlers to be called upon event.
425  *
426  * Initialize the admin submission and completion queues.
427  * Initialize the asynchronous events notification queues.
428  *
429  * @return - 0 on success, negative value on failure.
430  */
431 int ena_com_admin_init(struct ena_com_dev *ena_dev,
432 		       struct ena_aenq_handlers *aenq_handlers);
433 
434 /* ena_com_admin_destroy - Destroy the admin and the async events queues.
435  * @ena_dev: ENA communication layer struct
436  *
437  * @note: Before calling this method, the caller must validate that the device
438  * won't send any additional admin completions/aenq.
439  * To achieve that, a FLR is recommended.
440  */
441 void ena_com_admin_destroy(struct ena_com_dev *ena_dev);
442 
443 /* ena_com_dev_reset - Perform device FLR to the device.
444  * @ena_dev: ENA communication layer struct
445  * @reset_reason: Specify what is the trigger for the reset in case of an error.
446  *
447  * @return - 0 on success, negative value on failure.
448  */
449 int ena_com_dev_reset(struct ena_com_dev *ena_dev,
450 		      enum ena_regs_reset_reason_types reset_reason);
451 
452 /* ena_com_create_io_queue - Create io queue.
453  * @ena_dev: ENA communication layer struct
454  * @ctx - create context structure
455  *
456  * Create the submission and the completion queues.
457  *
458  * @return - 0 on success, negative value on failure.
459  */
460 int ena_com_create_io_queue(struct ena_com_dev *ena_dev,
461 			    struct ena_com_create_io_ctx *ctx);
462 
463 /* ena_com_destroy_io_queue - Destroy IO queue with the queue id - qid.
464  * @ena_dev: ENA communication layer struct
465  * @qid - the caller virtual queue id.
466  */
467 void ena_com_destroy_io_queue(struct ena_com_dev *ena_dev, u16 qid);
468 
469 /* ena_com_get_io_handlers - Return the io queue handlers
470  * @ena_dev: ENA communication layer struct
471  * @qid - the caller virtual queue id.
472  * @io_sq - IO submission queue handler
473  * @io_cq - IO completion queue handler.
474  *
475  * @return - 0 on success, negative value on failure.
476  */
477 int ena_com_get_io_handlers(struct ena_com_dev *ena_dev, u16 qid,
478 			    struct ena_com_io_sq **io_sq,
479 			    struct ena_com_io_cq **io_cq);
480 
481 /* ena_com_admin_aenq_enable - ENAble asynchronous event notifications
482  * @ena_dev: ENA communication layer struct
483  *
484  * After this method, aenq event can be received via AENQ.
485  */
486 void ena_com_admin_aenq_enable(struct ena_com_dev *ena_dev);
487 
488 /* ena_com_set_admin_running_state - Set the state of the admin queue
489  * @ena_dev: ENA communication layer struct
490  *
491  * Change the state of the admin queue (enable/disable)
492  */
493 void ena_com_set_admin_running_state(struct ena_com_dev *ena_dev, bool state);
494 
495 /* ena_com_get_admin_running_state - Get the admin queue state
496  * @ena_dev: ENA communication layer struct
497  *
498  * Retrieve the state of the admin queue (enable/disable)
499  *
500  * @return - current polling mode (enable/disable)
501  */
502 bool ena_com_get_admin_running_state(struct ena_com_dev *ena_dev);
503 
504 /* ena_com_set_admin_polling_mode - Set the admin completion queue polling mode
505  * @ena_dev: ENA communication layer struct
506  * @polling: ENAble/Disable polling mode
507  *
508  * Set the admin completion mode.
509  */
510 void ena_com_set_admin_polling_mode(struct ena_com_dev *ena_dev, bool polling);
511 
512 /* ena_com_set_admin_auto_polling_mode - Enable autoswitch to polling mode
513  * @ena_dev: ENA communication layer struct
514  * @polling: Enable/Disable polling mode
515  *
516  * Set the autopolling mode.
517  * If autopolling is on:
518  * In case of missing interrupt when data is available switch to polling.
519  */
520 void ena_com_set_admin_auto_polling_mode(struct ena_com_dev *ena_dev,
521 					 bool polling);
522 
523 /* ena_com_admin_q_comp_intr_handler - admin queue interrupt handler
524  * @ena_dev: ENA communication layer struct
525  *
526  * This method goes over the admin completion queue and wakes up all the pending
527  * threads that wait on the commands wait event.
528  *
529  * @note: Should be called after MSI-X interrupt.
530  */
531 void ena_com_admin_q_comp_intr_handler(struct ena_com_dev *ena_dev);
532 
533 /* ena_com_aenq_intr_handler - AENQ interrupt handler
534  * @ena_dev: ENA communication layer struct
535  *
536  * This method goes over the async event notification queue and calls the proper
537  * aenq handler.
538  */
539 void ena_com_aenq_intr_handler(struct ena_com_dev *dev, void *data);
540 
541 /* ena_com_abort_admin_commands - Abort all the outstanding admin commands.
542  * @ena_dev: ENA communication layer struct
543  *
544  * This method aborts all the outstanding admin commands.
545  * The caller should then call ena_com_wait_for_abort_completion to make sure
546  * all the commands were completed.
547  */
548 void ena_com_abort_admin_commands(struct ena_com_dev *ena_dev);
549 
550 /* ena_com_wait_for_abort_completion - Wait for admin commands abort.
551  * @ena_dev: ENA communication layer struct
552  *
553  * This method waits until all the outstanding admin commands are completed.
554  */
555 void ena_com_wait_for_abort_completion(struct ena_com_dev *ena_dev);
556 
557 /* ena_com_validate_version - Validate the device parameters
558  * @ena_dev: ENA communication layer struct
559  *
560  * This method verifies the device parameters are the same as the saved
561  * parameters in ena_dev.
562  * This method is useful after device reset, to validate the device mac address
563  * and the device offloads are the same as before the reset.
564  *
565  * @return - 0 on success negative value otherwise.
566  */
567 int ena_com_validate_version(struct ena_com_dev *ena_dev);
568 
569 /* ena_com_get_link_params - Retrieve physical link parameters.
570  * @ena_dev: ENA communication layer struct
571  * @resp: Link parameters
572  *
573  * Retrieve the physical link parameters,
574  * like speed, auto-negotiation and full duplex support.
575  *
576  * @return - 0 on Success negative value otherwise.
577  */
578 int ena_com_get_link_params(struct ena_com_dev *ena_dev,
579 			    struct ena_admin_get_feat_resp *resp);
580 
581 /* ena_com_get_dma_width - Retrieve physical dma address width the device
582  * supports.
583  * @ena_dev: ENA communication layer struct
584  *
585  * Retrieve the maximum physical address bits the device can handle.
586  *
587  * @return: > 0 on Success and negative value otherwise.
588  */
589 int ena_com_get_dma_width(struct ena_com_dev *ena_dev);
590 
591 /* ena_com_set_aenq_config - Set aenq groups configurations
592  * @ena_dev: ENA communication layer struct
593  * @groups flag: bit fields flags of enum ena_admin_aenq_group.
594  *
595  * Configure which aenq event group the driver would like to receive.
596  *
597  * @return: 0 on Success and negative value otherwise.
598  */
599 int ena_com_set_aenq_config(struct ena_com_dev *ena_dev, u32 groups_flag);
600 
601 /* ena_com_get_dev_attr_feat - Get device features
602  * @ena_dev: ENA communication layer struct
603  * @get_feat_ctx: returned context that contain the get features.
604  *
605  * @return: 0 on Success and negative value otherwise.
606  */
607 int ena_com_get_dev_attr_feat(struct ena_com_dev *ena_dev,
608 			      struct ena_com_dev_get_features_ctx *get_feat_ctx);
609 
610 /* ena_com_get_dev_basic_stats - Get device basic statistics
611  * @ena_dev: ENA communication layer struct
612  * @stats: stats return value
613  *
614  * @return: 0 on Success and negative value otherwise.
615  */
616 int ena_com_get_dev_basic_stats(struct ena_com_dev *ena_dev,
617 				struct ena_admin_basic_stats *stats);
618 
619 /* ena_com_set_dev_mtu - Configure the device mtu.
620  * @ena_dev: ENA communication layer struct
621  * @mtu: mtu value
622  *
623  * @return: 0 on Success and negative value otherwise.
624  */
625 int ena_com_set_dev_mtu(struct ena_com_dev *ena_dev, int mtu);
626 
627 /* ena_com_get_offload_settings - Retrieve the device offloads capabilities
628  * @ena_dev: ENA communication layer struct
629  * @offlad: offload return value
630  *
631  * @return: 0 on Success and negative value otherwise.
632  */
633 int ena_com_get_offload_settings(struct ena_com_dev *ena_dev,
634 				 struct ena_admin_feature_offload_desc *offload);
635 
636 /* ena_com_rss_init - Init RSS
637  * @ena_dev: ENA communication layer struct
638  * @log_size: indirection log size
639  *
640  * Allocate RSS/RFS resources.
641  * The caller then can configure rss using ena_com_set_hash_function,
642  * ena_com_set_hash_ctrl and ena_com_indirect_table_set.
643  *
644  * @return: 0 on Success and negative value otherwise.
645  */
646 int ena_com_rss_init(struct ena_com_dev *ena_dev, u16 log_size);
647 
648 /* ena_com_rss_destroy - Destroy rss
649  * @ena_dev: ENA communication layer struct
650  *
651  * Free all the RSS/RFS resources.
652  */
653 void ena_com_rss_destroy(struct ena_com_dev *ena_dev);
654 
655 /* ena_com_get_current_hash_function - Get RSS hash function
656  * @ena_dev: ENA communication layer struct
657  *
658  * Return the current hash function.
659  * @return: 0 or one of the ena_admin_hash_functions values.
660  */
661 int ena_com_get_current_hash_function(struct ena_com_dev *ena_dev);
662 
663 /* ena_com_fill_hash_function - Fill RSS hash function
664  * @ena_dev: ENA communication layer struct
665  * @func: The hash function (Toeplitz or crc)
666  * @key: Hash key (for toeplitz hash)
667  * @key_len: key length (max length 10 DW)
668  * @init_val: initial value for the hash function
669  *
670  * Fill the ena_dev resources with the desire hash function, hash key, key_len
671  * and key initial value (if needed by the hash function).
672  * To flush the key into the device the caller should call
673  * ena_com_set_hash_function.
674  *
675  * @return: 0 on Success and negative value otherwise.
676  */
677 int ena_com_fill_hash_function(struct ena_com_dev *ena_dev,
678 			       enum ena_admin_hash_functions func,
679 			       const u8 *key, u16 key_len, u32 init_val);
680 
681 /* ena_com_set_hash_function - Flush the hash function and it dependencies to
682  * the device.
683  * @ena_dev: ENA communication layer struct
684  *
685  * Flush the hash function and it dependencies (key, key length and
686  * initial value) if needed.
687  *
688  * @note: Prior to this method the caller should call ena_com_fill_hash_function
689  *
690  * @return: 0 on Success and negative value otherwise.
691  */
692 int ena_com_set_hash_function(struct ena_com_dev *ena_dev);
693 
694 /* ena_com_get_hash_function - Retrieve the hash function from the device.
695  * @ena_dev: ENA communication layer struct
696  * @func: hash function
697  *
698  * Retrieve the hash function from the device.
699  *
700  * @note: If the caller called ena_com_fill_hash_function but didn't flush
701  * it to the device, the new configuration will be lost.
702  *
703  * @return: 0 on Success and negative value otherwise.
704  */
705 int ena_com_get_hash_function(struct ena_com_dev *ena_dev,
706 			      enum ena_admin_hash_functions *func);
707 
708 /* ena_com_get_hash_key - Retrieve the hash key
709  * @ena_dev: ENA communication layer struct
710  * @key: hash key
711  *
712  * Retrieve the hash key.
713  *
714  * @note: If the caller called ena_com_fill_hash_key but didn't flush
715  * it to the device, the new configuration will be lost.
716  *
717  * @return: 0 on Success and negative value otherwise.
718  */
719 int ena_com_get_hash_key(struct ena_com_dev *ena_dev, u8 *key);
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 flush
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 flush
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 allocated 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 allocated 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 returns a
881  * completion.
882  * The completion will be copied 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_interrupt_moderation_supported - Return if interrupt moderation
900  * capability is supported by the device.
901  *
902  * @return - supported or not.
903  */
904 bool ena_com_interrupt_moderation_supported(struct ena_com_dev *ena_dev);
905 
906 /* ena_com_update_nonadaptive_moderation_interval_tx - Update the
907  * non-adaptive interval in Tx direction.
908  * @ena_dev: ENA communication layer struct
909  * @tx_coalesce_usecs: Interval in usec.
910  *
911  * @return - 0 on success, negative value on failure.
912  */
913 int ena_com_update_nonadaptive_moderation_interval_tx(struct ena_com_dev *ena_dev,
914 						      u32 tx_coalesce_usecs);
915 
916 /* ena_com_update_nonadaptive_moderation_interval_rx - Update the
917  * non-adaptive interval in Rx direction.
918  * @ena_dev: ENA communication layer struct
919  * @rx_coalesce_usecs: Interval in usec.
920  *
921  * @return - 0 on success, negative value on failure.
922  */
923 int ena_com_update_nonadaptive_moderation_interval_rx(struct ena_com_dev *ena_dev,
924 						      u32 rx_coalesce_usecs);
925 
926 /* ena_com_get_nonadaptive_moderation_interval_tx - Retrieve the
927  * non-adaptive interval in Tx direction.
928  * @ena_dev: ENA communication layer struct
929  *
930  * @return - interval in usec
931  */
932 unsigned int ena_com_get_nonadaptive_moderation_interval_tx(struct ena_com_dev *ena_dev);
933 
934 /* ena_com_get_nonadaptive_moderation_interval_rx - Retrieve the
935  * non-adaptive interval in Rx direction.
936  * @ena_dev: ENA communication layer struct
937  *
938  * @return - interval in usec
939  */
940 unsigned int ena_com_get_nonadaptive_moderation_interval_rx(struct ena_com_dev *ena_dev);
941 
942 /* ena_com_config_dev_mode - Configure the placement policy of the device.
943  * @ena_dev: ENA communication layer struct
944  * @llq_features: LLQ feature descriptor, retrieve via
945  *		   ena_com_get_dev_attr_feat.
946  * @ena_llq_config: The default driver LLQ parameters configurations
947  */
948 int ena_com_config_dev_mode(struct ena_com_dev *ena_dev,
949 			    struct ena_admin_feature_llq_desc *llq_features,
950 			    struct ena_llq_configurations *llq_default_config);
951 
952 static inline bool ena_com_get_adaptive_moderation_enabled(struct ena_com_dev *ena_dev)
953 {
954 	return ena_dev->adaptive_coalescing;
955 }
956 
957 static inline void ena_com_enable_adaptive_moderation(struct ena_com_dev *ena_dev)
958 {
959 	ena_dev->adaptive_coalescing = true;
960 }
961 
962 static inline void ena_com_disable_adaptive_moderation(struct ena_com_dev *ena_dev)
963 {
964 	ena_dev->adaptive_coalescing = false;
965 }
966 
967 /* ena_com_update_intr_reg - Prepare interrupt register
968  * @intr_reg: interrupt register to update.
969  * @rx_delay_interval: Rx interval in usecs
970  * @tx_delay_interval: Tx interval in usecs
971  * @unmask: unmask enable/disable
972  *
973  * Prepare interrupt update register with the supplied parameters.
974  */
975 static inline void ena_com_update_intr_reg(struct ena_eth_io_intr_reg *intr_reg,
976 					   u32 rx_delay_interval,
977 					   u32 tx_delay_interval,
978 					   bool unmask)
979 {
980 	intr_reg->intr_control = 0;
981 	intr_reg->intr_control |= rx_delay_interval &
982 		ENA_ETH_IO_INTR_REG_RX_INTR_DELAY_MASK;
983 
984 	intr_reg->intr_control |=
985 		(tx_delay_interval << ENA_ETH_IO_INTR_REG_TX_INTR_DELAY_SHIFT)
986 		& ENA_ETH_IO_INTR_REG_TX_INTR_DELAY_MASK;
987 
988 	if (unmask)
989 		intr_reg->intr_control |= ENA_ETH_IO_INTR_REG_INTR_UNMASK_MASK;
990 }
991 
992 static inline u8 *ena_com_get_next_bounce_buffer(struct ena_com_io_bounce_buffer_control *bounce_buf_ctrl)
993 {
994 	u16 size, buffers_num;
995 	u8 *buf;
996 
997 	size = bounce_buf_ctrl->buffer_size;
998 	buffers_num = bounce_buf_ctrl->buffers_num;
999 
1000 	buf = bounce_buf_ctrl->base_buffer +
1001 		(bounce_buf_ctrl->next_to_use++ & (buffers_num - 1)) * size;
1002 
1003 	prefetchw(bounce_buf_ctrl->base_buffer +
1004 		(bounce_buf_ctrl->next_to_use & (buffers_num - 1)) * size);
1005 
1006 	return buf;
1007 }
1008 
1009 #endif /* !(ENA_COM) */
1010