ena_com.h 32 KB

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