ena_com.h 32 KB

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