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