SnpInitialize.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /** @file
  2. Implementation of the SNP.Initialize() function and its private helpers if
  3. any.
  4. Copyright (C) 2013, Red Hat, Inc.
  5. Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
  6. Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include <Library/BaseLib.h>
  10. #include <Library/BaseMemoryLib.h>
  11. #include <Library/MemoryAllocationLib.h>
  12. #include <Library/UefiBootServicesTableLib.h>
  13. #include "VirtioNet.h"
  14. /**
  15. Initialize a virtio ring for a specific transfer direction of the virtio-net
  16. device.
  17. This function may only be called by VirtioNetInitialize().
  18. @param[in,out] Dev The VNET_DEV driver instance about to enter the
  19. EfiSimpleNetworkInitialized state.
  20. @param[in] Selector Identifies the transfer direction (virtio queue) of
  21. the network device.
  22. @param[out] Ring The virtio-ring inside the VNET_DEV structure,
  23. corresponding to Selector.
  24. @param[out] Mapping A resulting token to pass to VirtioNetUninitRing()
  25. @retval EFI_UNSUPPORTED The queue size reported by the virtio-net device is
  26. too small.
  27. @return Status codes from VIRTIO_CFG_WRITE(),
  28. VIRTIO_CFG_READ(), VirtioRingInit() and
  29. VirtioRingMap().
  30. @retval EFI_SUCCESS Ring initialized.
  31. */
  32. STATIC
  33. EFI_STATUS
  34. EFIAPI
  35. VirtioNetInitRing (
  36. IN OUT VNET_DEV *Dev,
  37. IN UINT16 Selector,
  38. OUT VRING *Ring,
  39. OUT VOID **Mapping
  40. )
  41. {
  42. EFI_STATUS Status;
  43. UINT16 QueueSize;
  44. UINT64 RingBaseShift;
  45. VOID *MapInfo;
  46. //
  47. // step 4b -- allocate selected queue
  48. //
  49. Status = Dev->VirtIo->SetQueueSel (Dev->VirtIo, Selector);
  50. if (EFI_ERROR (Status)) {
  51. return Status;
  52. }
  53. Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);
  54. if (EFI_ERROR (Status)) {
  55. return Status;
  56. }
  57. //
  58. // For each packet (RX and TX alike), we need two descriptors:
  59. // one for the virtio-net request header, and another one for the data
  60. //
  61. if (QueueSize < 2) {
  62. return EFI_UNSUPPORTED;
  63. }
  64. Status = VirtioRingInit (Dev->VirtIo, QueueSize, Ring);
  65. if (EFI_ERROR (Status)) {
  66. return Status;
  67. }
  68. //
  69. // If anything fails from here on, we must release the ring resources.
  70. //
  71. Status = VirtioRingMap (Dev->VirtIo, Ring, &RingBaseShift, &MapInfo);
  72. if (EFI_ERROR (Status)) {
  73. goto ReleaseQueue;
  74. }
  75. //
  76. // Additional steps for MMIO: align the queue appropriately, and set the
  77. // size. If anything fails from here on, we must unmap the ring resources.
  78. //
  79. Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);
  80. if (EFI_ERROR (Status)) {
  81. goto UnmapQueue;
  82. }
  83. Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);
  84. if (EFI_ERROR (Status)) {
  85. goto UnmapQueue;
  86. }
  87. //
  88. // step 4c -- report GPFN (guest-physical frame number) of queue
  89. //
  90. Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo, Ring, RingBaseShift);
  91. if (EFI_ERROR (Status)) {
  92. goto UnmapQueue;
  93. }
  94. *Mapping = MapInfo;
  95. return EFI_SUCCESS;
  96. UnmapQueue:
  97. Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, MapInfo);
  98. ReleaseQueue:
  99. VirtioRingUninit (Dev->VirtIo, Ring);
  100. return Status;
  101. }
  102. /**
  103. Set up static scaffolding for the VirtioNetTransmit() and
  104. VirtioNetGetStatus() SNP methods.
  105. This function may only be called by VirtioNetInitialize().
  106. The structures laid out and resources configured include:
  107. - fully populate the TX queue with a static pattern of virtio descriptor
  108. chains,
  109. - tracking of heads of free descriptor chains from the above,
  110. - one common virtio-net request header (never modified by the host) for all
  111. pending TX packets,
  112. - select polling over TX interrupt.
  113. @param[in,out] Dev The VNET_DEV driver instance about to enter the
  114. EfiSimpleNetworkInitialized state.
  115. @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads
  116. of free descriptor chains or failed to init
  117. TxBufCollection.
  118. @return Status codes from VIRTIO_DEVICE_PROTOCOL.
  119. AllocateSharedPages() or
  120. VirtioMapAllBytesInSharedBuffer()
  121. @retval EFI_SUCCESS TX setup successful.
  122. */
  123. STATIC
  124. EFI_STATUS
  125. EFIAPI
  126. VirtioNetInitTx (
  127. IN OUT VNET_DEV *Dev
  128. )
  129. {
  130. UINTN TxSharedReqSize;
  131. UINTN PktIdx;
  132. EFI_STATUS Status;
  133. EFI_PHYSICAL_ADDRESS DeviceAddress;
  134. VOID *TxSharedReqBuffer;
  135. Dev->TxMaxPending = (UINT16)MIN (
  136. Dev->TxRing.QueueSize / 2,
  137. VNET_MAX_PENDING
  138. );
  139. Dev->TxCurPending = 0;
  140. Dev->TxFreeStack = AllocatePool (
  141. Dev->TxMaxPending *
  142. sizeof *Dev->TxFreeStack
  143. );
  144. if (Dev->TxFreeStack == NULL) {
  145. return EFI_OUT_OF_RESOURCES;
  146. }
  147. Dev->TxBufCollection = OrderedCollectionInit (
  148. VirtioNetTxBufMapInfoCompare,
  149. VirtioNetTxBufDeviceAddressCompare
  150. );
  151. if (Dev->TxBufCollection == NULL) {
  152. Status = EFI_OUT_OF_RESOURCES;
  153. goto FreeTxFreeStack;
  154. }
  155. //
  156. // Allocate TxSharedReq header and map with BusMasterCommonBuffer so that it
  157. // can be accessed equally by both processor and device.
  158. //
  159. Status = Dev->VirtIo->AllocateSharedPages (
  160. Dev->VirtIo,
  161. EFI_SIZE_TO_PAGES (sizeof *Dev->TxSharedReq),
  162. &TxSharedReqBuffer
  163. );
  164. if (EFI_ERROR (Status)) {
  165. goto UninitTxBufCollection;
  166. }
  167. ZeroMem (TxSharedReqBuffer, sizeof *Dev->TxSharedReq);
  168. Status = VirtioMapAllBytesInSharedBuffer (
  169. Dev->VirtIo,
  170. VirtioOperationBusMasterCommonBuffer,
  171. TxSharedReqBuffer,
  172. sizeof *(Dev->TxSharedReq),
  173. &DeviceAddress,
  174. &Dev->TxSharedReqMap
  175. );
  176. if (EFI_ERROR (Status)) {
  177. goto FreeTxSharedReqBuffer;
  178. }
  179. Dev->TxSharedReq = TxSharedReqBuffer;
  180. //
  181. // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
  182. // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
  183. //
  184. TxSharedReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?
  185. sizeof (Dev->TxSharedReq->V0_9_5) :
  186. sizeof *Dev->TxSharedReq;
  187. for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {
  188. UINT16 DescIdx;
  189. DescIdx = (UINT16)(2 * PktIdx);
  190. Dev->TxFreeStack[PktIdx] = DescIdx;
  191. //
  192. // For each possibly pending packet, lay out the descriptor for the common
  193. // (unmodified by the host) virtio-net request header.
  194. //
  195. Dev->TxRing.Desc[DescIdx].Addr = DeviceAddress;
  196. Dev->TxRing.Desc[DescIdx].Len = (UINT32)TxSharedReqSize;
  197. Dev->TxRing.Desc[DescIdx].Flags = VRING_DESC_F_NEXT;
  198. Dev->TxRing.Desc[DescIdx].Next = (UINT16)(DescIdx + 1);
  199. //
  200. // The second descriptor of each pending TX packet is updated on the fly,
  201. // but it always terminates the descriptor chain of the packet.
  202. //
  203. Dev->TxRing.Desc[DescIdx + 1].Flags = 0;
  204. }
  205. //
  206. // virtio-0.9.5, Appendix C, Packet Transmission
  207. //
  208. Dev->TxSharedReq->V0_9_5.Flags = 0;
  209. Dev->TxSharedReq->V0_9_5.GsoType = VIRTIO_NET_HDR_GSO_NONE;
  210. //
  211. // For VirtIo 1.0 only -- the field exists, but it is unused
  212. //
  213. Dev->TxSharedReq->NumBuffers = 0;
  214. //
  215. // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
  216. //
  217. MemoryFence ();
  218. Dev->TxLastUsed = *Dev->TxRing.Used.Idx;
  219. ASSERT (Dev->TxLastUsed == 0);
  220. //
  221. // want no interrupt when a transmit completes
  222. //
  223. *Dev->TxRing.Avail.Flags = (UINT16)VRING_AVAIL_F_NO_INTERRUPT;
  224. return EFI_SUCCESS;
  225. FreeTxSharedReqBuffer:
  226. Dev->VirtIo->FreeSharedPages (
  227. Dev->VirtIo,
  228. EFI_SIZE_TO_PAGES (sizeof *(Dev->TxSharedReq)),
  229. TxSharedReqBuffer
  230. );
  231. UninitTxBufCollection:
  232. OrderedCollectionUninit (Dev->TxBufCollection);
  233. FreeTxFreeStack:
  234. FreePool (Dev->TxFreeStack);
  235. return Status;
  236. }
  237. /**
  238. Set up static scaffolding for the VirtioNetReceive() SNP method and enable
  239. live device operation.
  240. This function may only be called as VirtioNetInitialize()'s final step.
  241. The structures laid out and resources configured include:
  242. - destination area for the host to write virtio-net request headers and
  243. packet data into,
  244. - select polling over RX interrupt,
  245. - fully populate the RX queue with a static pattern of virtio descriptor
  246. chains.
  247. @param[in,out] Dev The VNET_DEV driver instance about to enter the
  248. EfiSimpleNetworkInitialized state.
  249. @return Status codes from VIRTIO_CFG_WRITE() or
  250. VIRTIO_DEVICE_PROTOCOL.AllocateSharedPages or
  251. VirtioMapAllBytesInSharedBuffer().
  252. @retval EFI_SUCCESS RX setup successful. The device is live and may
  253. already be writing to the receive area.
  254. */
  255. STATIC
  256. EFI_STATUS
  257. EFIAPI
  258. VirtioNetInitRx (
  259. IN OUT VNET_DEV *Dev
  260. )
  261. {
  262. EFI_STATUS Status;
  263. UINTN VirtioNetReqSize;
  264. UINTN RxBufSize;
  265. UINT16 RxAlwaysPending;
  266. UINTN PktIdx;
  267. UINT16 DescIdx;
  268. UINTN NumBytes;
  269. EFI_PHYSICAL_ADDRESS RxBufDeviceAddress;
  270. VOID *RxBuffer;
  271. //
  272. // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
  273. // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.
  274. //
  275. VirtioNetReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?
  276. sizeof (VIRTIO_NET_REQ) :
  277. sizeof (VIRTIO_1_0_NET_REQ);
  278. //
  279. // For each incoming packet we must supply two descriptors:
  280. // - the recipient for the virtio-net request header, plus
  281. // - the recipient for the network data (which consists of Ethernet header
  282. // and Ethernet payload).
  283. //
  284. RxBufSize = VirtioNetReqSize +
  285. (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);
  286. //
  287. // Limit the number of pending RX packets if the queue is big. The division
  288. // by two is due to the above "two descriptors per packet" trait.
  289. //
  290. RxAlwaysPending = (UINT16)MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);
  291. //
  292. // The RxBuf is shared between guest and hypervisor, use
  293. // AllocateSharedPages() to allocate this memory region and map it with
  294. // BusMasterCommonBuffer so that it can be accessed by both guest and
  295. // hypervisor.
  296. //
  297. NumBytes = RxAlwaysPending * RxBufSize;
  298. Dev->RxBufNrPages = EFI_SIZE_TO_PAGES (NumBytes);
  299. Status = Dev->VirtIo->AllocateSharedPages (
  300. Dev->VirtIo,
  301. Dev->RxBufNrPages,
  302. &RxBuffer
  303. );
  304. if (EFI_ERROR (Status)) {
  305. return Status;
  306. }
  307. ZeroMem (RxBuffer, NumBytes);
  308. Status = VirtioMapAllBytesInSharedBuffer (
  309. Dev->VirtIo,
  310. VirtioOperationBusMasterCommonBuffer,
  311. RxBuffer,
  312. NumBytes,
  313. &Dev->RxBufDeviceBase,
  314. &Dev->RxBufMap
  315. );
  316. if (EFI_ERROR (Status)) {
  317. goto FreeSharedBuffer;
  318. }
  319. Dev->RxBuf = RxBuffer;
  320. //
  321. // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device
  322. //
  323. MemoryFence ();
  324. Dev->RxLastUsed = *Dev->RxRing.Used.Idx;
  325. ASSERT (Dev->RxLastUsed == 0);
  326. //
  327. // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:
  328. // the host should not send interrupts, we'll poll in VirtioNetReceive()
  329. // and VirtioNetIsPacketAvailable().
  330. //
  331. *Dev->RxRing.Avail.Flags = (UINT16)VRING_AVAIL_F_NO_INTERRUPT;
  332. //
  333. // now set up a separate, two-part descriptor chain for each RX packet, and
  334. // link each chain into (from) the available ring as well
  335. //
  336. DescIdx = 0;
  337. RxBufDeviceAddress = Dev->RxBufDeviceBase;
  338. for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {
  339. //
  340. // virtio-0.9.5, 2.4.1.2 Updating the Available Ring
  341. // invisible to the host until we update the Index Field
  342. //
  343. Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;
  344. //
  345. // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table
  346. //
  347. Dev->RxRing.Desc[DescIdx].Addr = RxBufDeviceAddress;
  348. Dev->RxRing.Desc[DescIdx].Len = (UINT32)VirtioNetReqSize;
  349. Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE | VRING_DESC_F_NEXT;
  350. Dev->RxRing.Desc[DescIdx].Next = (UINT16)(DescIdx + 1);
  351. RxBufDeviceAddress += Dev->RxRing.Desc[DescIdx++].Len;
  352. Dev->RxRing.Desc[DescIdx].Addr = RxBufDeviceAddress;
  353. Dev->RxRing.Desc[DescIdx].Len = (UINT32)(RxBufSize - VirtioNetReqSize);
  354. Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE;
  355. RxBufDeviceAddress += Dev->RxRing.Desc[DescIdx++].Len;
  356. }
  357. //
  358. // virtio-0.9.5, 2.4.1.3 Updating the Index Field
  359. //
  360. MemoryFence ();
  361. *Dev->RxRing.Avail.Idx = RxAlwaysPending;
  362. //
  363. // At this point reception may already be running. In order to make it sure,
  364. // kick the hypervisor. If we fail to kick it, we must first abort reception
  365. // before tearing down anything, because reception may have been already
  366. // running even without the kick.
  367. //
  368. // virtio-0.9.5, 2.4.1.4 Notifying the Device
  369. //
  370. MemoryFence ();
  371. Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);
  372. if (EFI_ERROR (Status)) {
  373. Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
  374. goto UnmapSharedBuffer;
  375. }
  376. return Status;
  377. UnmapSharedBuffer:
  378. Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RxBufMap);
  379. FreeSharedBuffer:
  380. Dev->VirtIo->FreeSharedPages (
  381. Dev->VirtIo,
  382. Dev->RxBufNrPages,
  383. RxBuffer
  384. );
  385. return Status;
  386. }
  387. /**
  388. Resets a network adapter and allocates the transmit and receive buffers
  389. required by the network interface; optionally, also requests allocation of
  390. additional transmit and receive buffers.
  391. @param This The protocol instance pointer.
  392. @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer
  393. space that the driver should allocate for the
  394. network interface. Some network interfaces will not
  395. be able to use the extra buffer, and the caller
  396. will not know if it is actually being used.
  397. @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer
  398. space that the driver should allocate for the
  399. network interface. Some network interfaces will not
  400. be able to use the extra buffer, and the caller
  401. will not know if it is actually being used.
  402. @retval EFI_SUCCESS The network interface was initialized.
  403. @retval EFI_NOT_STARTED The network interface has not been started.
  404. @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit
  405. and receive buffers.
  406. @retval EFI_INVALID_PARAMETER One or more of the parameters has an
  407. unsupported value.
  408. @retval EFI_DEVICE_ERROR The command could not be sent to the network
  409. interface.
  410. @retval EFI_UNSUPPORTED This function is not supported by the network
  411. interface.
  412. **/
  413. EFI_STATUS
  414. EFIAPI
  415. VirtioNetInitialize (
  416. IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
  417. IN UINTN ExtraRxBufferSize OPTIONAL,
  418. IN UINTN ExtraTxBufferSize OPTIONAL
  419. )
  420. {
  421. VNET_DEV *Dev;
  422. EFI_TPL OldTpl;
  423. EFI_STATUS Status;
  424. UINT8 NextDevStat;
  425. UINT64 Features;
  426. if (This == NULL) {
  427. return EFI_INVALID_PARAMETER;
  428. }
  429. if ((ExtraRxBufferSize > 0) || (ExtraTxBufferSize > 0)) {
  430. return EFI_UNSUPPORTED;
  431. }
  432. Dev = VIRTIO_NET_FROM_SNP (This);
  433. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  434. if (Dev->Snm.State != EfiSimpleNetworkStarted) {
  435. Status = EFI_NOT_STARTED;
  436. goto InitFailed;
  437. }
  438. //
  439. // In the EfiSimpleNetworkStarted state the virtio-net device has status
  440. // value 0 (= reset) -- see the state diagram, the full call chain to
  441. // the end of VirtioNetGetFeatures() (considering we're here now),
  442. // the DeviceFailed label below, and VirtioNetShutdown().
  443. //
  444. // Accordingly, the below is a subsequence of the steps found in the
  445. // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.
  446. //
  447. NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence
  448. Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
  449. if (EFI_ERROR (Status)) {
  450. goto InitFailed;
  451. }
  452. NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it
  453. Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
  454. if (EFI_ERROR (Status)) {
  455. goto DeviceFailed;
  456. }
  457. //
  458. // Set Page Size - MMIO VirtIo Specific
  459. //
  460. Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);
  461. if (EFI_ERROR (Status)) {
  462. goto DeviceFailed;
  463. }
  464. //
  465. // step 4a -- retrieve features. Note that we're past validating required
  466. // features in VirtioNetGetFeatures().
  467. //
  468. Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);
  469. if (EFI_ERROR (Status)) {
  470. goto DeviceFailed;
  471. }
  472. ASSERT (Features & VIRTIO_NET_F_MAC);
  473. ASSERT (
  474. Dev->Snm.MediaPresentSupported ==
  475. !!(Features & VIRTIO_NET_F_STATUS)
  476. );
  477. Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_F_VERSION_1 |
  478. VIRTIO_F_IOMMU_PLATFORM;
  479. //
  480. // In virtio-1.0, feature negotiation is expected to complete before queue
  481. // discovery, and the device can also reject the selected set of features.
  482. //
  483. if (Dev->VirtIo->Revision >= VIRTIO_SPEC_REVISION (1, 0, 0)) {
  484. Status = Virtio10WriteFeatures (Dev->VirtIo, Features, &NextDevStat);
  485. if (EFI_ERROR (Status)) {
  486. goto DeviceFailed;
  487. }
  488. }
  489. //
  490. // step 4b, 4c -- allocate and report virtqueues
  491. //
  492. Status = VirtioNetInitRing (
  493. Dev,
  494. VIRTIO_NET_Q_RX,
  495. &Dev->RxRing,
  496. &Dev->RxRingMap
  497. );
  498. if (EFI_ERROR (Status)) {
  499. goto DeviceFailed;
  500. }
  501. Status = VirtioNetInitRing (
  502. Dev,
  503. VIRTIO_NET_Q_TX,
  504. &Dev->TxRing,
  505. &Dev->TxRingMap
  506. );
  507. if (EFI_ERROR (Status)) {
  508. goto ReleaseRxRing;
  509. }
  510. //
  511. // step 5 -- keep only the features we want
  512. //
  513. if (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {
  514. Features &= ~(UINT64)(VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM);
  515. Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);
  516. if (EFI_ERROR (Status)) {
  517. goto ReleaseTxRing;
  518. }
  519. }
  520. //
  521. // step 6 -- virtio-net initialization complete
  522. //
  523. NextDevStat |= VSTAT_DRIVER_OK;
  524. Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);
  525. if (EFI_ERROR (Status)) {
  526. goto ReleaseTxRing;
  527. }
  528. Status = VirtioNetInitTx (Dev);
  529. if (EFI_ERROR (Status)) {
  530. goto AbortDevice;
  531. }
  532. //
  533. // start receiving
  534. //
  535. Status = VirtioNetInitRx (Dev);
  536. if (EFI_ERROR (Status)) {
  537. goto ReleaseTxAux;
  538. }
  539. Dev->Snm.State = EfiSimpleNetworkInitialized;
  540. gBS->RestoreTPL (OldTpl);
  541. return EFI_SUCCESS;
  542. ReleaseTxAux:
  543. VirtioNetShutdownTx (Dev);
  544. AbortDevice:
  545. Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
  546. ReleaseTxRing:
  547. VirtioNetUninitRing (Dev, &Dev->TxRing, Dev->TxRingMap);
  548. ReleaseRxRing:
  549. VirtioNetUninitRing (Dev, &Dev->RxRing, Dev->RxRingMap);
  550. DeviceFailed:
  551. //
  552. // restore device status invariant for the EfiSimpleNetworkStarted state
  553. //
  554. Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
  555. InitFailed:
  556. gBS->RestoreTPL (OldTpl);
  557. return Status;
  558. }