You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ena.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. #ifndef _ENA_H
  2. #define _ENA_H
  3. /** @file
  4. *
  5. * Amazon ENA network driver
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/if_ether.h>
  11. /** BAR size */
  12. #define ENA_BAR_SIZE 16384
  13. /** Queue alignment */
  14. #define ENA_ALIGN 4096
  15. /** Number of admin queue entries */
  16. #define ENA_AQ_COUNT 2
  17. /** Number of admin completion queue entries */
  18. #define ENA_ACQ_COUNT 2
  19. /** Number of transmit queue entries */
  20. #define ENA_TX_COUNT 16
  21. /** Number of receive queue entries */
  22. #define ENA_RX_COUNT 16
  23. /** Base address low register offset */
  24. #define ENA_BASE_LO 0x0
  25. /** Base address high register offset */
  26. #define ENA_BASE_HI 0x4
  27. /** Capability register value */
  28. #define ENA_CAPS( count, size ) ( ( (size) << 16 ) | ( (count) << 0 ) )
  29. /** Admin queue base address register */
  30. #define ENA_AQ_BASE 0x10
  31. /** Admin queue capabilities register */
  32. #define ENA_AQ_CAPS 0x18
  33. /** Admin completion queue base address register */
  34. #define ENA_ACQ_BASE 0x20
  35. /** Admin completion queue capabilities register */
  36. #define ENA_ACQ_CAPS 0x28
  37. /** Admin queue doorbell register */
  38. #define ENA_AQ_DB 0x2c
  39. /** Maximum time to wait for admin requests */
  40. #define ENA_ADMIN_MAX_WAIT_MS 5000
  41. /** Device control register */
  42. #define ENA_CTRL 0x54
  43. #define ENA_CTRL_RESET 0x00000001UL /**< Reset */
  44. /** Maximum time to wait for reset */
  45. #define ENA_RESET_MAX_WAIT_MS 1000
  46. /** Device status register */
  47. #define ENA_STAT 0x58
  48. #define ENA_STAT_READY 0x00000001UL /**< Ready */
  49. /** Admin queue entry header */
  50. struct ena_aq_header {
  51. /** Request identifier */
  52. uint8_t id;
  53. /** Reserved */
  54. uint8_t reserved;
  55. /** Opcode */
  56. uint8_t opcode;
  57. /** Flags */
  58. uint8_t flags;
  59. } __attribute__ (( packed ));
  60. /** Admin queue ownership phase flag */
  61. #define ENA_AQ_PHASE 0x01
  62. /** Admin completion queue entry header */
  63. struct ena_acq_header {
  64. /** Request identifier */
  65. uint8_t id;
  66. /** Reserved */
  67. uint8_t reserved;
  68. /** Status */
  69. uint8_t status;
  70. /** Flags */
  71. uint8_t flags;
  72. /** Extended status */
  73. uint16_t ext;
  74. /** Consumer index */
  75. uint16_t cons;
  76. } __attribute__ (( packed ));
  77. /** Admin completion queue ownership phase flag */
  78. #define ENA_ACQ_PHASE 0x01
  79. /** Device attributes */
  80. #define ENA_DEVICE_ATTRIBUTES 1
  81. /** Device attributes */
  82. struct ena_device_attributes {
  83. /** Implementation */
  84. uint32_t implementation;
  85. /** Device version */
  86. uint32_t version;
  87. /** Supported features */
  88. uint32_t features;
  89. /** Reserved */
  90. uint8_t reserved_a[4];
  91. /** Physical address width */
  92. uint32_t physical;
  93. /** Virtual address width */
  94. uint32_t virtual;
  95. /** MAC address */
  96. uint8_t mac[ETH_ALEN];
  97. /** Reserved */
  98. uint8_t reserved_b[2];
  99. /** Maximum MTU */
  100. uint32_t mtu;
  101. } __attribute__ (( packed ));
  102. /** Feature */
  103. union ena_feature {
  104. /** Device attributes */
  105. struct ena_device_attributes device;
  106. };
  107. /** Submission queue direction */
  108. enum ena_sq_direction {
  109. /** Transmit */
  110. ENA_SQ_TX = 0x20,
  111. /** Receive */
  112. ENA_SQ_RX = 0x40,
  113. };
  114. /** Create submission queue */
  115. #define ENA_CREATE_SQ 1
  116. /** Create submission queue request */
  117. struct ena_create_sq_req {
  118. /** Header */
  119. struct ena_aq_header header;
  120. /** Direction */
  121. uint8_t direction;
  122. /** Reserved */
  123. uint8_t reserved_a;
  124. /** Policy */
  125. uint16_t policy;
  126. /** Completion queue identifier */
  127. uint16_t cq_id;
  128. /** Number of entries */
  129. uint16_t count;
  130. /** Base address */
  131. uint64_t address;
  132. /** Writeback address */
  133. uint64_t writeback;
  134. /** Reserved */
  135. uint8_t reserved_b[8];
  136. } __attribute__ (( packed ));
  137. /** Submission queue policy */
  138. enum ena_sq_policy {
  139. /** Use host memory */
  140. ENA_SQ_HOST_MEMORY = 0x0001,
  141. /** Memory is contiguous */
  142. ENA_SQ_CONTIGUOUS = 0x0100,
  143. };
  144. /** Create submission queue response */
  145. struct ena_create_sq_rsp {
  146. /** Header */
  147. struct ena_acq_header header;
  148. /** Submission queue identifier */
  149. uint16_t id;
  150. /** Reserved */
  151. uint8_t reserved[2];
  152. /** Doorbell register offset */
  153. uint32_t doorbell;
  154. /** LLQ descriptor ring offset */
  155. uint32_t llq_desc;
  156. /** LLQ header offset */
  157. uint32_t llq_data;
  158. } __attribute__ (( packed ));
  159. /** Destroy submission queue */
  160. #define ENA_DESTROY_SQ 2
  161. /** Destroy submission queue request */
  162. struct ena_destroy_sq_req {
  163. /** Header */
  164. struct ena_aq_header header;
  165. /** Submission queue identifier */
  166. uint16_t id;
  167. /** Direction */
  168. uint8_t direction;
  169. /** Reserved */
  170. uint8_t reserved;
  171. } __attribute__ (( packed ));
  172. /** Destroy submission queue response */
  173. struct ena_destroy_sq_rsp {
  174. /** Header */
  175. struct ena_acq_header header;
  176. } __attribute__ (( packed ));
  177. /** Create completion queue */
  178. #define ENA_CREATE_CQ 3
  179. /** Create completion queue request */
  180. struct ena_create_cq_req {
  181. /** Header */
  182. struct ena_aq_header header;
  183. /** Interrupts enabled */
  184. uint8_t intr;
  185. /** Entry size (in 32-bit words) */
  186. uint8_t size;
  187. /** Number of entries */
  188. uint16_t count;
  189. /** MSI-X vector */
  190. uint32_t vector;
  191. /** Base address */
  192. uint64_t address;
  193. } __attribute__ (( packed ));
  194. /** Create completion queue response */
  195. struct ena_create_cq_rsp {
  196. /** Header */
  197. struct ena_acq_header header;
  198. /** Completion queue identifier */
  199. uint16_t id;
  200. /** Actual number of entries */
  201. uint16_t count;
  202. /** NUMA node register offset */
  203. uint32_t node;
  204. /** Doorbell register offset */
  205. uint32_t doorbell;
  206. /** Interrupt unmask register offset */
  207. uint32_t intr;
  208. } __attribute__ (( packed ));
  209. /** Destroy completion queue */
  210. #define ENA_DESTROY_CQ 4
  211. /** Destroy completion queue request */
  212. struct ena_destroy_cq_req {
  213. /** Header */
  214. struct ena_aq_header header;
  215. /** Completion queue identifier */
  216. uint16_t id;
  217. /** Reserved */
  218. uint8_t reserved[2];
  219. } __attribute__ (( packed ));
  220. /** Destroy completion queue response */
  221. struct ena_destroy_cq_rsp {
  222. /** Header */
  223. struct ena_acq_header header;
  224. } __attribute__ (( packed ));
  225. /** Get feature */
  226. #define ENA_GET_FEATURE 8
  227. /** Get feature request */
  228. struct ena_get_feature_req {
  229. /** Header */
  230. struct ena_aq_header header;
  231. /** Length */
  232. uint32_t len;
  233. /** Address */
  234. uint64_t address;
  235. /** Flags */
  236. uint8_t flags;
  237. /** Feature identifier */
  238. uint8_t id;
  239. /** Reserved */
  240. uint8_t reserved[2];
  241. } __attribute__ (( packed ));
  242. /** Get feature response */
  243. struct ena_get_feature_rsp {
  244. /** Header */
  245. struct ena_acq_header header;
  246. /** Feature */
  247. union ena_feature feature;
  248. } __attribute__ (( packed ));
  249. /** Get statistics */
  250. #define ENA_GET_STATS 11
  251. /** Get statistics request */
  252. struct ena_get_stats_req {
  253. /** Header */
  254. struct ena_aq_header header;
  255. /** Reserved */
  256. uint8_t reserved_a[12];
  257. /** Type */
  258. uint8_t type;
  259. /** Scope */
  260. uint8_t scope;
  261. /** Reserved */
  262. uint8_t reserved_b[2];
  263. /** Queue ID */
  264. uint16_t queue;
  265. /** Device ID */
  266. uint16_t device;
  267. } __attribute__ (( packed ));
  268. /** Basic statistics */
  269. #define ENA_STATS_TYPE_BASIC 0
  270. /** Ethernet statistics */
  271. #define ENA_STATS_SCOPE_ETH 1
  272. /** My device */
  273. #define ENA_DEVICE_MINE 0xffff
  274. /** Get statistics response */
  275. struct ena_get_stats_rsp {
  276. /** Header */
  277. struct ena_acq_header header;
  278. /** Transmit byte count */
  279. uint64_t tx_bytes;
  280. /** Transmit packet count */
  281. uint64_t tx_packets;
  282. /** Receive byte count */
  283. uint64_t rx_bytes;
  284. /** Receive packet count */
  285. uint64_t rx_packets;
  286. /** Receive drop count */
  287. uint64_t rx_drops;
  288. } __attribute__ (( packed ));
  289. /** Admin queue request */
  290. union ena_aq_req {
  291. /** Header */
  292. struct ena_aq_header header;
  293. /** Create submission queue */
  294. struct ena_create_sq_req create_sq;
  295. /** Destroy submission queue */
  296. struct ena_destroy_sq_req destroy_sq;
  297. /** Create completion queue */
  298. struct ena_create_cq_req create_cq;
  299. /** Destroy completion queue */
  300. struct ena_destroy_cq_req destroy_cq;
  301. /** Get feature */
  302. struct ena_get_feature_req get_feature;
  303. /** Get statistics */
  304. struct ena_get_stats_req get_stats;
  305. /** Padding */
  306. uint8_t pad[64];
  307. };
  308. /** Admin completion queue response */
  309. union ena_acq_rsp {
  310. /** Header */
  311. struct ena_acq_header header;
  312. /** Create submission queue */
  313. struct ena_create_sq_rsp create_sq;
  314. /** Destroy submission queue */
  315. struct ena_destroy_sq_rsp destroy_sq;
  316. /** Create completion queue */
  317. struct ena_create_cq_rsp create_cq;
  318. /** Destroy completion queue */
  319. struct ena_destroy_cq_rsp destroy_cq;
  320. /** Get feature */
  321. struct ena_get_feature_rsp get_feature;
  322. /** Get statistics */
  323. struct ena_get_stats_rsp get_stats;
  324. /** Padding */
  325. uint8_t pad[64];
  326. };
  327. /** Admin queue */
  328. struct ena_aq {
  329. /** Requests */
  330. union ena_aq_req *req;
  331. /** Producer counter */
  332. unsigned int prod;
  333. };
  334. /** Admin completion queue */
  335. struct ena_acq {
  336. /** Responses */
  337. union ena_acq_rsp *rsp;
  338. /** Consumer counter */
  339. unsigned int cons;
  340. /** Phase */
  341. unsigned int phase;
  342. };
  343. /** Transmit submission queue entry */
  344. struct ena_tx_sqe {
  345. /** Length */
  346. uint16_t len;
  347. /** Reserved */
  348. uint8_t reserved_a;
  349. /** Flags */
  350. uint8_t flags;
  351. /** Reserved */
  352. uint8_t reserved_b[3];
  353. /** Request identifier */
  354. uint8_t id;
  355. /** Address */
  356. uint64_t address;
  357. } __attribute__ (( packed ));
  358. /** Receive submission queue entry */
  359. struct ena_rx_sqe {
  360. /** Length */
  361. uint16_t len;
  362. /** Reserved */
  363. uint8_t reserved_a;
  364. /** Flags */
  365. uint8_t flags;
  366. /** Request identifier */
  367. uint16_t id;
  368. /** Reserved */
  369. uint8_t reserved_b[2];
  370. /** Address */
  371. uint64_t address;
  372. } __attribute__ (( packed ));
  373. /** Submission queue ownership phase flag */
  374. #define ENA_SQE_PHASE 0x01
  375. /** This is the first descriptor */
  376. #define ENA_SQE_FIRST 0x04
  377. /** This is the last descriptor */
  378. #define ENA_SQE_LAST 0x08
  379. /** Request completion */
  380. #define ENA_SQE_CPL 0x10
  381. /** Transmit completion queue entry */
  382. struct ena_tx_cqe {
  383. /** Request identifier */
  384. uint16_t id;
  385. /** Status */
  386. uint8_t status;
  387. /** Flags */
  388. uint8_t flags;
  389. /** Reserved */
  390. uint8_t reserved[2];
  391. /** Consumer index */
  392. uint16_t cons;
  393. } __attribute__ (( packed ));
  394. /** Receive completion queue entry */
  395. struct ena_rx_cqe {
  396. /** Reserved */
  397. uint8_t reserved_a[3];
  398. /** Flags */
  399. uint8_t flags;
  400. /** Length */
  401. uint16_t len;
  402. /** Request identifier */
  403. uint16_t id;
  404. /** Reserved */
  405. uint8_t reserved_b[8];
  406. } __attribute__ (( packed ));
  407. /** Completion queue ownership phase flag */
  408. #define ENA_CQE_PHASE 0x01
  409. /** Submission queue */
  410. struct ena_sq {
  411. /** Entries */
  412. union {
  413. /** Transmit submission queue entries */
  414. struct ena_tx_sqe *tx;
  415. /** Receive submission queue entries */
  416. struct ena_rx_sqe *rx;
  417. /** Raw data */
  418. void *raw;
  419. } sqe;
  420. /** Doorbell register offset */
  421. unsigned int doorbell;
  422. /** Total length of entries */
  423. size_t len;
  424. /** Producer counter */
  425. unsigned int prod;
  426. /** Phase */
  427. unsigned int phase;
  428. /** Submission queue identifier */
  429. uint16_t id;
  430. /** Direction */
  431. uint8_t direction;
  432. /** Number of entries */
  433. uint8_t count;
  434. };
  435. /**
  436. * Initialise submission queue
  437. *
  438. * @v sq Submission queue
  439. * @v direction Direction
  440. * @v count Number of entries
  441. * @v size Size of each entry
  442. */
  443. static inline __attribute__ (( always_inline )) void
  444. ena_sq_init ( struct ena_sq *sq, unsigned int direction, unsigned int count,
  445. size_t size ) {
  446. sq->len = ( count * size );
  447. sq->direction = direction;
  448. sq->count = count;
  449. }
  450. /** Completion queue */
  451. struct ena_cq {
  452. /** Entries */
  453. union {
  454. /** Transmit completion queue entries */
  455. struct ena_tx_cqe *tx;
  456. /** Receive completion queue entries */
  457. struct ena_rx_cqe *rx;
  458. /** Raw data */
  459. void *raw;
  460. } cqe;
  461. /** Doorbell register offset */
  462. unsigned int doorbell;
  463. /** Total length of entries */
  464. size_t len;
  465. /** Consumer counter */
  466. unsigned int cons;
  467. /** Phase */
  468. unsigned int phase;
  469. /** Completion queue identifier */
  470. uint16_t id;
  471. /** Entry size (in 32-bit words) */
  472. uint8_t size;
  473. /** Requested number of entries */
  474. uint8_t requested;
  475. /** Actual number of entries */
  476. uint8_t actual;
  477. /** Actual number of entries minus one */
  478. uint8_t mask;
  479. };
  480. /**
  481. * Initialise completion queue
  482. *
  483. * @v cq Completion queue
  484. * @v count Number of entries
  485. * @v size Size of each entry
  486. */
  487. static inline __attribute__ (( always_inline )) void
  488. ena_cq_init ( struct ena_cq *cq, unsigned int count, size_t size ) {
  489. cq->len = ( count * size );
  490. cq->size = ( size / sizeof ( uint32_t ) );
  491. cq->requested = count;
  492. }
  493. /** Queue pair */
  494. struct ena_qp {
  495. /** Submission queue */
  496. struct ena_sq sq;
  497. /** Completion queue */
  498. struct ena_cq cq;
  499. };
  500. /** An ENA network card */
  501. struct ena_nic {
  502. /** Registers */
  503. void *regs;
  504. /** Admin queue */
  505. struct ena_aq aq;
  506. /** Admin completion queue */
  507. struct ena_acq acq;
  508. /** Transmit queue */
  509. struct ena_qp tx;
  510. /** Receive queue */
  511. struct ena_qp rx;
  512. /** Receive I/O buffers */
  513. struct io_buffer *rx_iobuf[ENA_RX_COUNT];
  514. };
  515. #endif /* _ENA_H */