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.c 25KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * Copyright (C) 2018 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/netdevice.h>
  30. #include <ipxe/ethernet.h>
  31. #include <ipxe/if_ether.h>
  32. #include <ipxe/iobuf.h>
  33. #include <ipxe/malloc.h>
  34. #include <ipxe/pci.h>
  35. #include "ena.h"
  36. /** @file
  37. *
  38. * Amazon ENA network driver
  39. *
  40. */
  41. /**
  42. * Get direction name (for debugging)
  43. *
  44. * @v direction Direction
  45. * @ret name Direction name
  46. */
  47. static const char * ena_direction ( unsigned int direction ) {
  48. switch ( direction ) {
  49. case ENA_SQ_TX: return "TX";
  50. case ENA_SQ_RX: return "RX";
  51. default: return "<UNKNOWN>";
  52. }
  53. }
  54. /******************************************************************************
  55. *
  56. * Device reset
  57. *
  58. ******************************************************************************
  59. */
  60. /**
  61. * Reset hardware
  62. *
  63. * @v ena ENA device
  64. * @ret rc Return status code
  65. */
  66. static int ena_reset ( struct ena_nic *ena ) {
  67. uint32_t stat;
  68. unsigned int i;
  69. /* Trigger reset */
  70. writel ( ENA_CTRL_RESET, ( ena->regs + ENA_CTRL ) );
  71. /* Wait for reset to complete */
  72. for ( i = 0 ; i < ENA_RESET_MAX_WAIT_MS ; i++ ) {
  73. /* Check if device is ready */
  74. stat = readl ( ena->regs + ENA_STAT );
  75. if ( stat & ENA_STAT_READY )
  76. return 0;
  77. /* Delay */
  78. mdelay ( 1 );
  79. }
  80. DBGC ( ena, "ENA %p timed out waiting for reset (status %#08x)\n",
  81. ena, stat );
  82. return -ETIMEDOUT;
  83. }
  84. /******************************************************************************
  85. *
  86. * Admin queue
  87. *
  88. ******************************************************************************
  89. */
  90. /**
  91. * Set queue base address
  92. *
  93. * @v ena ENA device
  94. * @v offset Register offset
  95. * @v address Base address
  96. */
  97. static inline void ena_set_base ( struct ena_nic *ena, unsigned int offset,
  98. void *base ) {
  99. physaddr_t phys = virt_to_bus ( base );
  100. /* Program base address registers */
  101. writel ( ( phys & 0xffffffffUL ),
  102. ( ena->regs + offset + ENA_BASE_LO ) );
  103. if ( sizeof ( phys ) > sizeof ( uint32_t ) ) {
  104. writel ( ( ( ( uint64_t ) phys ) >> 32 ),
  105. ( ena->regs + offset + ENA_BASE_HI ) );
  106. } else {
  107. writel ( 0, ( ena->regs + offset + ENA_BASE_HI ) );
  108. }
  109. }
  110. /**
  111. * Set queue capabilities
  112. *
  113. * @v ena ENA device
  114. * @v offset Register offset
  115. * @v count Number of entries
  116. * @v size Size of each entry
  117. */
  118. static inline __attribute__ (( always_inline )) void
  119. ena_set_caps ( struct ena_nic *ena, unsigned int offset, unsigned int count,
  120. size_t size ) {
  121. /* Program capabilities register */
  122. writel ( ENA_CAPS ( count, size ), ( ena->regs + offset ) );
  123. }
  124. /**
  125. * Clear queue capabilities
  126. *
  127. * @v ena ENA device
  128. * @v offset Register offset
  129. */
  130. static inline __attribute__ (( always_inline )) void
  131. ena_clear_caps ( struct ena_nic *ena, unsigned int offset ) {
  132. /* Clear capabilities register */
  133. writel ( 0, ( ena->regs + offset ) );
  134. }
  135. /**
  136. * Create admin queues
  137. *
  138. * @v ena ENA device
  139. * @ret rc Return status code
  140. */
  141. static int ena_create_admin ( struct ena_nic *ena ) {
  142. size_t aq_len = ( ENA_AQ_COUNT * sizeof ( ena->aq.req[0] ) );
  143. size_t acq_len = ( ENA_ACQ_COUNT * sizeof ( ena->acq.rsp[0] ) );
  144. int rc;
  145. /* Allocate admin completion queue */
  146. ena->acq.rsp = malloc_dma ( acq_len, acq_len );
  147. if ( ! ena->acq.rsp ) {
  148. rc = -ENOMEM;
  149. goto err_alloc_acq;
  150. }
  151. memset ( ena->acq.rsp, 0, acq_len );
  152. /* Allocate admin queue */
  153. ena->aq.req = malloc_dma ( aq_len, aq_len );
  154. if ( ! ena->aq.req ) {
  155. rc = -ENOMEM;
  156. goto err_alloc_aq;
  157. }
  158. memset ( ena->aq.req, 0, aq_len );
  159. /* Program queue addresses and capabilities */
  160. ena_set_base ( ena, ENA_ACQ_BASE, ena->acq.rsp );
  161. ena_set_caps ( ena, ENA_ACQ_CAPS, ENA_ACQ_COUNT,
  162. sizeof ( ena->acq.rsp[0] ) );
  163. ena_set_base ( ena, ENA_AQ_BASE, ena->aq.req );
  164. ena_set_caps ( ena, ENA_AQ_CAPS, ENA_AQ_COUNT,
  165. sizeof ( ena->aq.req[0] ) );
  166. DBGC ( ena, "ENA %p AQ [%08lx,%08lx) ACQ [%08lx,%08lx)\n",
  167. ena, virt_to_phys ( ena->aq.req ),
  168. ( virt_to_phys ( ena->aq.req ) + aq_len ),
  169. virt_to_phys ( ena->acq.rsp ),
  170. ( virt_to_phys ( ena->acq.rsp ) + acq_len ) );
  171. return 0;
  172. ena_clear_caps ( ena, ENA_AQ_CAPS );
  173. ena_clear_caps ( ena, ENA_ACQ_CAPS );
  174. free_dma ( ena->aq.req, aq_len );
  175. err_alloc_aq:
  176. free_dma ( ena->acq.rsp, acq_len );
  177. err_alloc_acq:
  178. return rc;
  179. }
  180. /**
  181. * Destroy admin queues
  182. *
  183. * @v ena ENA device
  184. */
  185. static void ena_destroy_admin ( struct ena_nic *ena ) {
  186. size_t aq_len = ( ENA_AQ_COUNT * sizeof ( ena->aq.req[0] ) );
  187. size_t acq_len = ( ENA_ACQ_COUNT * sizeof ( ena->acq.rsp[0] ) );
  188. /* Clear queue capabilities */
  189. ena_clear_caps ( ena, ENA_AQ_CAPS );
  190. ena_clear_caps ( ena, ENA_ACQ_CAPS );
  191. wmb();
  192. /* Free queues */
  193. free_dma ( ena->aq.req, aq_len );
  194. free_dma ( ena->acq.rsp, acq_len );
  195. DBGC ( ena, "ENA %p AQ and ACQ destroyed\n", ena );
  196. }
  197. /**
  198. * Get next available admin queue request
  199. *
  200. * @v ena ENA device
  201. * @ret req Admin queue request
  202. */
  203. static union ena_aq_req * ena_admin_req ( struct ena_nic *ena ) {
  204. union ena_aq_req *req;
  205. unsigned int index;
  206. /* Get next request */
  207. index = ( ena->aq.prod % ENA_AQ_COUNT );
  208. req = &ena->aq.req[index];
  209. /* Initialise request */
  210. memset ( ( ( ( void * ) req ) + sizeof ( req->header ) ), 0,
  211. ( sizeof ( *req ) - sizeof ( req->header ) ) );
  212. req->header.id = ena->aq.prod;
  213. /* Increment producer counter */
  214. ena->aq.prod++;
  215. return req;
  216. }
  217. /**
  218. * Issue admin queue request
  219. *
  220. * @v ena ENA device
  221. * @v req Admin queue request
  222. * @v rsp Admin queue response to fill in
  223. * @ret rc Return status code
  224. */
  225. static int ena_admin ( struct ena_nic *ena, union ena_aq_req *req,
  226. union ena_acq_rsp **rsp ) {
  227. unsigned int index;
  228. unsigned int i;
  229. int rc;
  230. /* Locate response */
  231. index = ( ena->acq.cons % ENA_ACQ_COUNT );
  232. *rsp = &ena->acq.rsp[index];
  233. /* Mark request as ready */
  234. req->header.flags ^= ENA_AQ_PHASE;
  235. wmb();
  236. DBGC2 ( ena, "ENA %p admin request %#x:\n",
  237. ena, le16_to_cpu ( req->header.id ) );
  238. DBGC2_HDA ( ena, virt_to_phys ( req ), req, sizeof ( *req ) );
  239. /* Ring doorbell */
  240. writel ( ena->aq.prod, ( ena->regs + ENA_AQ_DB ) );
  241. /* Wait for response */
  242. for ( i = 0 ; i < ENA_ADMIN_MAX_WAIT_MS ; i++ ) {
  243. /* Check for response */
  244. if ( ( (*rsp)->header.flags ^ ena->acq.phase ) & ENA_ACQ_PHASE){
  245. mdelay ( 1 );
  246. continue;
  247. }
  248. DBGC2 ( ena, "ENA %p admin response %#x:\n",
  249. ena, le16_to_cpu ( (*rsp)->header.id ) );
  250. DBGC2_HDA ( ena, virt_to_phys ( *rsp ), *rsp, sizeof ( **rsp ));
  251. /* Increment consumer counter */
  252. ena->acq.cons++;
  253. if ( ( ena->acq.cons % ENA_ACQ_COUNT ) == 0 )
  254. ena->acq.phase ^= ENA_ACQ_PHASE;
  255. /* Check command identifier */
  256. if ( (*rsp)->header.id != req->header.id ) {
  257. DBGC ( ena, "ENA %p admin response %#x mismatch:\n",
  258. ena, le16_to_cpu ( (*rsp)->header.id ) );
  259. rc = -EILSEQ;
  260. goto err;
  261. }
  262. /* Check status */
  263. if ( (*rsp)->header.status != 0 ) {
  264. DBGC ( ena, "ENA %p admin response %#x status %d:\n",
  265. ena, le16_to_cpu ( (*rsp)->header.id ),
  266. (*rsp)->header.status );
  267. rc = -EIO;
  268. goto err;
  269. }
  270. /* Success */
  271. return 0;
  272. }
  273. rc = -ETIMEDOUT;
  274. DBGC ( ena, "ENA %p timed out waiting for admin request %#x:\n",
  275. ena, le16_to_cpu ( req->header.id ) );
  276. err:
  277. DBGC_HDA ( ena, virt_to_phys ( req ), req, sizeof ( *req ) );
  278. DBGC_HDA ( ena, virt_to_phys ( *rsp ), *rsp, sizeof ( **rsp ) );
  279. return rc;
  280. }
  281. /**
  282. * Create submission queue
  283. *
  284. * @v ena ENA device
  285. * @v sq Submission queue
  286. * @v cq Corresponding completion queue
  287. * @ret rc Return status code
  288. */
  289. static int ena_create_sq ( struct ena_nic *ena, struct ena_sq *sq,
  290. struct ena_cq *cq ) {
  291. union ena_aq_req *req;
  292. union ena_acq_rsp *rsp;
  293. int rc;
  294. /* Allocate submission queue entries */
  295. sq->sqe.raw = malloc_dma ( sq->len, ENA_ALIGN );
  296. if ( ! sq->sqe.raw ) {
  297. rc = -ENOMEM;
  298. goto err_alloc;
  299. }
  300. memset ( sq->sqe.raw, 0, sq->len );
  301. /* Construct request */
  302. req = ena_admin_req ( ena );
  303. req->header.opcode = ENA_CREATE_SQ;
  304. req->create_sq.direction = sq->direction;
  305. req->create_sq.policy = cpu_to_le16 ( ENA_SQ_HOST_MEMORY |
  306. ENA_SQ_CONTIGUOUS );
  307. req->create_sq.cq_id = cpu_to_le16 ( cq->id );
  308. req->create_sq.count = cpu_to_le16 ( sq->count );
  309. req->create_sq.address = cpu_to_le64 ( virt_to_bus ( sq->sqe.raw ) );
  310. /* Issue request */
  311. if ( ( rc = ena_admin ( ena, req, &rsp ) ) != 0 )
  312. goto err_admin;
  313. /* Parse response */
  314. sq->id = le16_to_cpu ( rsp->create_sq.id );
  315. sq->doorbell = le32_to_cpu ( rsp->create_sq.doorbell );
  316. /* Reset producer counter and phase */
  317. sq->prod = 0;
  318. sq->phase = ENA_SQE_PHASE;
  319. DBGC ( ena, "ENA %p %s SQ%d at [%08lx,%08lx) db +%04x CQ%d\n",
  320. ena, ena_direction ( sq->direction ), sq->id,
  321. virt_to_phys ( sq->sqe.raw ),
  322. ( virt_to_phys ( sq->sqe.raw ) + sq->len ),
  323. sq->doorbell, cq->id );
  324. return 0;
  325. err_admin:
  326. free_dma ( sq->sqe.raw, sq->len );
  327. err_alloc:
  328. return rc;
  329. }
  330. /**
  331. * Destroy submission queue
  332. *
  333. * @v ena ENA device
  334. * @v sq Submission queue
  335. * @ret rc Return status code
  336. */
  337. static int ena_destroy_sq ( struct ena_nic *ena, struct ena_sq *sq ) {
  338. union ena_aq_req *req;
  339. union ena_acq_rsp *rsp;
  340. int rc;
  341. /* Construct request */
  342. req = ena_admin_req ( ena );
  343. req->header.opcode = ENA_DESTROY_SQ;
  344. req->destroy_sq.id = cpu_to_le16 ( sq->id );
  345. req->destroy_sq.direction = sq->direction;
  346. /* Issue request */
  347. if ( ( rc = ena_admin ( ena, req, &rsp ) ) != 0 )
  348. return rc;
  349. /* Free submission queue entries */
  350. free_dma ( sq->sqe.raw, sq->len );
  351. DBGC ( ena, "ENA %p %s SQ%d destroyed\n",
  352. ena, ena_direction ( sq->direction ), sq->id );
  353. return 0;
  354. }
  355. /**
  356. * Create completion queue
  357. *
  358. * @v ena ENA device
  359. * @v cq Completion queue
  360. * @ret rc Return status code
  361. */
  362. static int ena_create_cq ( struct ena_nic *ena, struct ena_cq *cq ) {
  363. union ena_aq_req *req;
  364. union ena_acq_rsp *rsp;
  365. int rc;
  366. /* Allocate completion queue entries */
  367. cq->cqe.raw = malloc_dma ( cq->len, ENA_ALIGN );
  368. if ( ! cq->cqe.raw ) {
  369. rc = -ENOMEM;
  370. goto err_alloc;
  371. }
  372. memset ( cq->cqe.raw, 0, cq->len );
  373. /* Construct request */
  374. req = ena_admin_req ( ena );
  375. req->header.opcode = ENA_CREATE_CQ;
  376. req->create_cq.size = cq->size;
  377. req->create_cq.count = cpu_to_le16 ( cq->requested );
  378. req->create_cq.address = cpu_to_le64 ( virt_to_bus ( cq->cqe.raw ) );
  379. /* Issue request */
  380. if ( ( rc = ena_admin ( ena, req, &rsp ) ) != 0 )
  381. goto err_admin;
  382. /* Parse response */
  383. cq->id = le16_to_cpu ( rsp->create_cq.id );
  384. cq->actual = le16_to_cpu ( rsp->create_cq.count );
  385. cq->doorbell = le32_to_cpu ( rsp->create_cq.doorbell );
  386. cq->mask = ( cq->actual - 1 );
  387. if ( cq->actual != cq->requested ) {
  388. DBGC ( ena, "ENA %p CQ%d requested %d actual %d\n",
  389. ena, cq->id, cq->requested, cq->actual );
  390. }
  391. /* Reset consumer counter and phase */
  392. cq->cons = 0;
  393. cq->phase = ENA_CQE_PHASE;
  394. DBGC ( ena, "ENA %p CQ%d at [%08lx,%08lx) db +%04x\n",
  395. ena, cq->id, virt_to_phys ( cq->cqe.raw ),
  396. ( virt_to_phys ( cq->cqe.raw ) + cq->len ), cq->doorbell );
  397. return 0;
  398. err_admin:
  399. free_dma ( cq->cqe.raw, cq->len );
  400. err_alloc:
  401. return rc;
  402. }
  403. /**
  404. * Destroy completion queue
  405. *
  406. * @v ena ENA device
  407. * @v cq Completion queue
  408. * @ret rc Return status code
  409. */
  410. static int ena_destroy_cq ( struct ena_nic *ena, struct ena_cq *cq ) {
  411. union ena_aq_req *req;
  412. union ena_acq_rsp *rsp;
  413. int rc;
  414. /* Construct request */
  415. req = ena_admin_req ( ena );
  416. req->header.opcode = ENA_DESTROY_CQ;
  417. req->destroy_cq.id = cpu_to_le16 ( cq->id );
  418. /* Issue request */
  419. if ( ( rc = ena_admin ( ena, req, &rsp ) ) != 0 )
  420. return rc;
  421. /* Free completion queue entries */
  422. free_dma ( cq->cqe.raw, cq->len );
  423. DBGC ( ena, "ENA %p CQ%d destroyed\n", ena, cq->id );
  424. return 0;
  425. }
  426. /**
  427. * Create queue pair
  428. *
  429. * @v ena ENA device
  430. * @v qp Queue pair
  431. * @ret rc Return status code
  432. */
  433. static int ena_create_qp ( struct ena_nic *ena, struct ena_qp *qp ) {
  434. int rc;
  435. /* Create completion queue */
  436. if ( ( rc = ena_create_cq ( ena, &qp->cq ) ) != 0 )
  437. goto err_create_cq;
  438. /* Create submission queue */
  439. if ( ( rc = ena_create_sq ( ena, &qp->sq, &qp->cq ) ) != 0 )
  440. goto err_create_sq;
  441. return 0;
  442. ena_destroy_sq ( ena, &qp->sq );
  443. err_create_sq:
  444. ena_destroy_cq ( ena, &qp->cq );
  445. err_create_cq:
  446. return rc;
  447. }
  448. /**
  449. * Destroy queue pair
  450. *
  451. * @v ena ENA device
  452. * @v qp Queue pair
  453. * @ret rc Return status code
  454. */
  455. static int ena_destroy_qp ( struct ena_nic *ena, struct ena_qp *qp ) {
  456. /* Destroy submission queue */
  457. ena_destroy_sq ( ena, &qp->sq );
  458. /* Destroy completion queue */
  459. ena_destroy_cq ( ena, &qp->cq );
  460. return 0;
  461. }
  462. /**
  463. * Get device attributes
  464. *
  465. * @v netdev Network device
  466. * @ret rc Return status code
  467. */
  468. static int ena_get_device_attributes ( struct net_device *netdev ) {
  469. struct ena_nic *ena = netdev->priv;
  470. union ena_aq_req *req;
  471. union ena_acq_rsp *rsp;
  472. union ena_feature *feature;
  473. int rc;
  474. /* Construct request */
  475. req = ena_admin_req ( ena );
  476. req->header.opcode = ENA_GET_FEATURE;
  477. req->get_feature.id = ENA_DEVICE_ATTRIBUTES;
  478. /* Issue request */
  479. if ( ( rc = ena_admin ( ena, req, &rsp ) ) != 0 )
  480. return rc;
  481. /* Parse response */
  482. feature = &rsp->get_feature.feature;
  483. memcpy ( netdev->hw_addr, feature->device.mac, ETH_ALEN );
  484. netdev->max_pkt_len = le32_to_cpu ( feature->device.mtu );
  485. netdev->mtu = ( netdev->max_pkt_len - ETH_HLEN );
  486. DBGC ( ena, "ENA %p MAC %s MTU %zd\n",
  487. ena, eth_ntoa ( netdev->hw_addr ), netdev->max_pkt_len );
  488. return 0;
  489. }
  490. /**
  491. * Get statistics (for debugging)
  492. *
  493. * @v ena ENA device
  494. * @ret rc Return status code
  495. */
  496. static int ena_get_stats ( struct ena_nic *ena ) {
  497. union ena_aq_req *req;
  498. union ena_acq_rsp *rsp;
  499. struct ena_get_stats_rsp *stats;
  500. int rc;
  501. /* Do nothing unless debug messages are enabled */
  502. if ( ! DBG_LOG )
  503. return 0;
  504. /* Construct request */
  505. req = ena_admin_req ( ena );
  506. req->header.opcode = ENA_GET_STATS;
  507. req->get_stats.type = ENA_STATS_TYPE_BASIC;
  508. req->get_stats.scope = ENA_STATS_SCOPE_ETH;
  509. req->get_stats.device = ENA_DEVICE_MINE;
  510. /* Issue request */
  511. if ( ( rc = ena_admin ( ena, req, &rsp ) ) != 0 )
  512. return rc;
  513. /* Parse response */
  514. stats = &rsp->get_stats;
  515. DBGC ( ena, "ENA %p TX bytes %#llx packets %#llx\n", ena,
  516. ( ( unsigned long long ) le64_to_cpu ( stats->tx_bytes ) ),
  517. ( ( unsigned long long ) le64_to_cpu ( stats->tx_packets ) ) );
  518. DBGC ( ena, "ENA %p RX bytes %#llx packets %#llx drops %#llx\n", ena,
  519. ( ( unsigned long long ) le64_to_cpu ( stats->rx_bytes ) ),
  520. ( ( unsigned long long ) le64_to_cpu ( stats->rx_packets ) ),
  521. ( ( unsigned long long ) le64_to_cpu ( stats->rx_drops ) ) );
  522. return 0;
  523. }
  524. /******************************************************************************
  525. *
  526. * Network device interface
  527. *
  528. ******************************************************************************
  529. */
  530. /**
  531. * Refill receive queue
  532. *
  533. * @v netdev Network device
  534. */
  535. static void ena_refill_rx ( struct net_device *netdev ) {
  536. struct ena_nic *ena = netdev->priv;
  537. struct io_buffer *iobuf;
  538. struct ena_rx_sqe *sqe;
  539. unsigned int index;
  540. physaddr_t address;
  541. size_t len = netdev->max_pkt_len;
  542. unsigned int refilled = 0;
  543. /* Refill queue */
  544. while ( ( ena->rx.sq.prod - ena->rx.cq.cons ) < ENA_RX_COUNT ) {
  545. /* Allocate I/O buffer */
  546. iobuf = alloc_iob ( len );
  547. if ( ! iobuf ) {
  548. /* Wait for next refill */
  549. break;
  550. }
  551. /* Get next submission queue entry */
  552. index = ( ena->rx.sq.prod % ENA_RX_COUNT );
  553. sqe = &ena->rx.sq.sqe.rx[index];
  554. /* Construct submission queue entry */
  555. address = virt_to_bus ( iobuf->data );
  556. sqe->len = cpu_to_le16 ( len );
  557. sqe->id = cpu_to_le16 ( ena->rx.sq.prod );
  558. sqe->address = cpu_to_le64 ( address );
  559. wmb();
  560. sqe->flags = ( ENA_SQE_FIRST | ENA_SQE_LAST | ENA_SQE_CPL |
  561. ena->rx.sq.phase );
  562. /* Increment producer counter */
  563. ena->rx.sq.prod++;
  564. if ( ( ena->rx.sq.prod % ENA_RX_COUNT ) == 0 )
  565. ena->rx.sq.phase ^= ENA_SQE_PHASE;
  566. /* Record I/O buffer */
  567. assert ( ena->rx_iobuf[index] == NULL );
  568. ena->rx_iobuf[index] = iobuf;
  569. DBGC2 ( ena, "ENA %p RX %d at [%08llx,%08llx)\n", ena, sqe->id,
  570. ( ( unsigned long long ) address ),
  571. ( ( unsigned long long ) address + len ) );
  572. refilled++;
  573. }
  574. /* Ring doorbell, if applicable */
  575. if ( refilled ) {
  576. wmb();
  577. writel ( ena->rx.sq.prod, ( ena->regs + ena->rx.sq.doorbell ) );
  578. }
  579. }
  580. /**
  581. * Discard unused receive I/O buffers
  582. *
  583. * @v ena ENA device
  584. */
  585. static void ena_empty_rx ( struct ena_nic *ena ) {
  586. unsigned int i;
  587. for ( i = 0 ; i < ENA_RX_COUNT ; i++ ) {
  588. if ( ena->rx_iobuf[i] )
  589. free_iob ( ena->rx_iobuf[i] );
  590. ena->rx_iobuf[i] = NULL;
  591. }
  592. }
  593. /**
  594. * Open network device
  595. *
  596. * @v netdev Network device
  597. * @ret rc Return status code
  598. */
  599. static int ena_open ( struct net_device *netdev ) {
  600. struct ena_nic *ena = netdev->priv;
  601. int rc;
  602. /* Create transmit queue pair */
  603. if ( ( rc = ena_create_qp ( ena, &ena->tx ) ) != 0 )
  604. goto err_create_tx;
  605. /* Create receive queue pair */
  606. if ( ( rc = ena_create_qp ( ena, &ena->rx ) ) != 0 )
  607. goto err_create_rx;
  608. /* Refill receive queue */
  609. ena_refill_rx ( netdev );
  610. return 0;
  611. ena_destroy_qp ( ena, &ena->rx );
  612. err_create_rx:
  613. ena_destroy_qp ( ena, &ena->tx );
  614. err_create_tx:
  615. return rc;
  616. }
  617. /**
  618. * Close network device
  619. *
  620. * @v netdev Network device
  621. */
  622. static void ena_close ( struct net_device *netdev ) {
  623. struct ena_nic *ena = netdev->priv;
  624. /* Dump statistics (for debugging) */
  625. ena_get_stats ( ena );
  626. /* Destroy receive queue pair */
  627. ena_destroy_qp ( ena, &ena->rx );
  628. /* Discard any unused receive buffers */
  629. ena_empty_rx ( ena );
  630. /* Destroy transmit queue pair */
  631. ena_destroy_qp ( ena, &ena->tx );
  632. }
  633. /**
  634. * Transmit packet
  635. *
  636. * @v netdev Network device
  637. * @v iobuf I/O buffer
  638. * @ret rc Return status code
  639. */
  640. static int ena_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
  641. struct ena_nic *ena = netdev->priv;
  642. struct ena_tx_sqe *sqe;
  643. unsigned int index;
  644. physaddr_t address;
  645. size_t len;
  646. /* Get next submission queue entry */
  647. if ( ( ena->tx.sq.prod - ena->tx.cq.cons ) >= ENA_TX_COUNT ) {
  648. DBGC ( ena, "ENA %p out of transmit descriptors\n", ena );
  649. return -ENOBUFS;
  650. }
  651. index = ( ena->tx.sq.prod % ENA_TX_COUNT );
  652. sqe = &ena->tx.sq.sqe.tx[index];
  653. /* Construct submission queue entry */
  654. address = virt_to_bus ( iobuf->data );
  655. len = iob_len ( iobuf );
  656. sqe->len = cpu_to_le16 ( len );
  657. sqe->id = ena->tx.sq.prod;
  658. sqe->address = cpu_to_le64 ( address );
  659. wmb();
  660. sqe->flags = ( ENA_SQE_FIRST | ENA_SQE_LAST | ENA_SQE_CPL |
  661. ena->tx.sq.phase );
  662. wmb();
  663. /* Increment producer counter */
  664. ena->tx.sq.prod++;
  665. if ( ( ena->tx.sq.prod % ENA_TX_COUNT ) == 0 )
  666. ena->tx.sq.phase ^= ENA_SQE_PHASE;
  667. /* Ring doorbell */
  668. writel ( ena->tx.sq.prod, ( ena->regs + ena->tx.sq.doorbell ) );
  669. DBGC2 ( ena, "ENA %p TX %d at [%08llx,%08llx)\n", ena, sqe->id,
  670. ( ( unsigned long long ) address ),
  671. ( ( unsigned long long ) address + len ) );
  672. return 0;
  673. }
  674. /**
  675. * Poll for completed transmissions
  676. *
  677. * @v netdev Network device
  678. */
  679. static void ena_poll_tx ( struct net_device *netdev ) {
  680. struct ena_nic *ena = netdev->priv;
  681. struct ena_tx_cqe *cqe;
  682. unsigned int index;
  683. /* Check for completed packets */
  684. while ( ena->tx.cq.cons != ena->tx.sq.prod ) {
  685. /* Get next completion queue entry */
  686. index = ( ena->tx.cq.cons & ena->tx.cq.mask );
  687. cqe = &ena->tx.cq.cqe.tx[index];
  688. /* Stop if completion queue entry is empty */
  689. if ( ( cqe->flags ^ ena->tx.cq.phase ) & ENA_CQE_PHASE )
  690. return;
  691. DBGC2 ( ena, "ENA %p TX %d complete\n", ena,
  692. ( le16_to_cpu ( cqe->id ) >> 2 /* Don't ask */ ) );
  693. /* Increment consumer counter */
  694. ena->tx.cq.cons++;
  695. if ( ! ( ena->tx.cq.cons & ena->tx.cq.mask ) )
  696. ena->tx.cq.phase ^= ENA_CQE_PHASE;
  697. /* Complete transmit */
  698. netdev_tx_complete_next ( netdev );
  699. }
  700. }
  701. /**
  702. * Poll for received packets
  703. *
  704. * @v netdev Network device
  705. */
  706. static void ena_poll_rx ( struct net_device *netdev ) {
  707. struct ena_nic *ena = netdev->priv;
  708. struct ena_rx_cqe *cqe;
  709. struct io_buffer *iobuf;
  710. unsigned int index;
  711. size_t len;
  712. /* Check for received packets */
  713. while ( ena->rx.cq.cons != ena->rx.sq.prod ) {
  714. /* Get next completion queue entry */
  715. index = ( ena->rx.cq.cons % ENA_RX_COUNT );
  716. cqe = &ena->rx.cq.cqe.rx[index];
  717. /* Stop if completion queue entry is empty */
  718. if ( ( cqe->flags ^ ena->rx.cq.phase ) & ENA_CQE_PHASE )
  719. return;
  720. /* Increment consumer counter */
  721. ena->rx.cq.cons++;
  722. if ( ! ( ena->rx.cq.cons & ena->rx.cq.mask ) )
  723. ena->rx.cq.phase ^= ENA_CQE_PHASE;
  724. /* Populate I/O buffer */
  725. iobuf = ena->rx_iobuf[index];
  726. ena->rx_iobuf[index] = NULL;
  727. len = le16_to_cpu ( cqe->len );
  728. iob_put ( iobuf, len );
  729. /* Hand off to network stack */
  730. DBGC2 ( ena, "ENA %p RX %d complete (length %zd)\n",
  731. ena, le16_to_cpu ( cqe->id ), len );
  732. netdev_rx ( netdev, iobuf );
  733. }
  734. }
  735. /**
  736. * Poll for completed and received packets
  737. *
  738. * @v netdev Network device
  739. */
  740. static void ena_poll ( struct net_device *netdev ) {
  741. /* Poll for transmit completions */
  742. ena_poll_tx ( netdev );
  743. /* Poll for receive completions */
  744. ena_poll_rx ( netdev );
  745. /* Refill receive ring */
  746. ena_refill_rx ( netdev );
  747. }
  748. /** ENA network device operations */
  749. static struct net_device_operations ena_operations = {
  750. .open = ena_open,
  751. .close = ena_close,
  752. .transmit = ena_transmit,
  753. .poll = ena_poll,
  754. };
  755. /******************************************************************************
  756. *
  757. * PCI interface
  758. *
  759. ******************************************************************************
  760. */
  761. /**
  762. * Probe PCI device
  763. *
  764. * @v pci PCI device
  765. * @ret rc Return status code
  766. */
  767. static int ena_probe ( struct pci_device *pci ) {
  768. struct net_device *netdev;
  769. struct ena_nic *ena;
  770. int rc;
  771. /* Allocate and initialise net device */
  772. netdev = alloc_etherdev ( sizeof ( *ena ) );
  773. if ( ! netdev ) {
  774. rc = -ENOMEM;
  775. goto err_alloc;
  776. }
  777. netdev_init ( netdev, &ena_operations );
  778. ena = netdev->priv;
  779. pci_set_drvdata ( pci, netdev );
  780. netdev->dev = &pci->dev;
  781. memset ( ena, 0, sizeof ( *ena ) );
  782. ena->acq.phase = ENA_ACQ_PHASE;
  783. ena_cq_init ( &ena->tx.cq, ENA_TX_COUNT,
  784. sizeof ( ena->tx.cq.cqe.tx[0] ) );
  785. ena_sq_init ( &ena->tx.sq, ENA_SQ_TX, ENA_TX_COUNT,
  786. sizeof ( ena->tx.sq.sqe.tx[0] ) );
  787. ena_cq_init ( &ena->rx.cq, ENA_RX_COUNT,
  788. sizeof ( ena->rx.cq.cqe.rx[0] ) );
  789. ena_sq_init ( &ena->rx.sq, ENA_SQ_RX, ENA_RX_COUNT,
  790. sizeof ( ena->rx.sq.sqe.rx[0] ) );
  791. /* Fix up PCI device */
  792. adjust_pci_device ( pci );
  793. /* Map registers */
  794. ena->regs = ioremap ( pci->membase, ENA_BAR_SIZE );
  795. if ( ! ena->regs ) {
  796. rc = -ENODEV;
  797. goto err_ioremap;
  798. }
  799. /* Reset the NIC */
  800. if ( ( rc = ena_reset ( ena ) ) != 0 )
  801. goto err_reset;
  802. /* Create admin queues */
  803. if ( ( rc = ena_create_admin ( ena ) ) != 0 )
  804. goto err_create_admin;
  805. /* Fetch MAC address */
  806. if ( ( rc = ena_get_device_attributes ( netdev ) ) != 0 )
  807. goto err_get_device_attributes;
  808. /* Register network device */
  809. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  810. goto err_register_netdev;
  811. /* Mark as link up, since we have no way to test link state on
  812. * this hardware.
  813. */
  814. netdev_link_up ( netdev );
  815. return 0;
  816. unregister_netdev ( netdev );
  817. err_register_netdev:
  818. err_get_device_attributes:
  819. ena_destroy_admin ( ena );
  820. err_create_admin:
  821. ena_reset ( ena );
  822. err_reset:
  823. iounmap ( ena->regs );
  824. err_ioremap:
  825. netdev_nullify ( netdev );
  826. netdev_put ( netdev );
  827. err_alloc:
  828. return rc;
  829. }
  830. /**
  831. * Remove PCI device
  832. *
  833. * @v pci PCI device
  834. */
  835. static void ena_remove ( struct pci_device *pci ) {
  836. struct net_device *netdev = pci_get_drvdata ( pci );
  837. struct ena_nic *ena = netdev->priv;
  838. /* Unregister network device */
  839. unregister_netdev ( netdev );
  840. /* Destroy admin queues */
  841. ena_destroy_admin ( ena );
  842. /* Reset card */
  843. ena_reset ( ena );
  844. /* Free network device */
  845. iounmap ( ena->regs );
  846. netdev_nullify ( netdev );
  847. netdev_put ( netdev );
  848. }
  849. /** ENA PCI device IDs */
  850. static struct pci_device_id ena_nics[] = {
  851. PCI_ROM ( 0x1d0f, 0xec20, "ena-vf", "ENA VF", 0 ),
  852. PCI_ROM ( 0x1d0f, 0xec21, "ena-vf-llq", "ENA VF (LLQ)", 0 ),
  853. };
  854. /** ENA PCI driver */
  855. struct pci_driver ena_driver __pci_driver = {
  856. .ids = ena_nics,
  857. .id_count = ( sizeof ( ena_nics ) / sizeof ( ena_nics[0] ) ),
  858. .probe = ena_probe,
  859. .remove = ena_remove,
  860. };