]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioNetDxe/SnpInitialize.c
OvmfPkg/VirtioNetDxe: negotiate VIRTIO_F_IOMMU_PLATFORM
[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
bd114d9f
BS
150 of free descriptor chains or failed to init\r
151 TxBufCollection.\r
891f016c
BS
152 @return Status codes from VIRTIO_DEVICE_PROTOCOL.\r
153 AllocateSharedPages() or\r
154 VirtioMapAllBytesInSharedBuffer()\r
12384f9b
LE
155 @retval EFI_SUCCESS TX setup successful.\r
156*/\r
157\r
158STATIC\r
159EFI_STATUS\r
160EFIAPI\r
161VirtioNetInitTx (\r
162 IN OUT VNET_DEV *Dev\r
163 )\r
164{\r
891f016c
BS
165 UINTN TxSharedReqSize;\r
166 UINTN PktIdx;\r
167 EFI_STATUS Status;\r
168 EFI_PHYSICAL_ADDRESS DeviceAddress;\r
169 VOID *TxSharedReqBuffer;\r
12384f9b 170\r
9f3acbb5
LE
171 Dev->TxMaxPending = (UINT16) MIN (Dev->TxRing.QueueSize / 2,\r
172 VNET_MAX_PENDING);\r
12384f9b
LE
173 Dev->TxCurPending = 0;\r
174 Dev->TxFreeStack = AllocatePool (Dev->TxMaxPending *\r
175 sizeof *Dev->TxFreeStack);\r
176 if (Dev->TxFreeStack == NULL) {\r
177 return EFI_OUT_OF_RESOURCES;\r
178 }\r
179\r
bd114d9f
BS
180 Dev->TxBufCollection = OrderedCollectionInit (\r
181 VirtioNetTxBufMapInfoCompare,\r
182 VirtioNetTxBufDeviceAddressCompare\r
183 );\r
184 if (Dev->TxBufCollection == NULL) {\r
185 Status = EFI_OUT_OF_RESOURCES;\r
186 goto FreeTxFreeStack;\r
187 }\r
188\r
891f016c
BS
189 //\r
190 // Allocate TxSharedReq header and map with BusMasterCommonBuffer so that it\r
191 // can be accessed equally by both processor and device.\r
192 //\r
193 Status = Dev->VirtIo->AllocateSharedPages (\r
194 Dev->VirtIo,\r
195 EFI_SIZE_TO_PAGES (sizeof *Dev->TxSharedReq),\r
196 &TxSharedReqBuffer\r
197 );\r
198 if (EFI_ERROR (Status)) {\r
bd114d9f 199 goto UninitTxBufCollection;\r
891f016c
BS
200 }\r
201\r
202 ZeroMem (TxSharedReqBuffer, sizeof *Dev->TxSharedReq);\r
203\r
204 Status = VirtioMapAllBytesInSharedBuffer (\r
205 Dev->VirtIo,\r
206 VirtioOperationBusMasterCommonBuffer,\r
207 TxSharedReqBuffer,\r
208 sizeof *(Dev->TxSharedReq),\r
209 &DeviceAddress,\r
210 &Dev->TxSharedReqMap\r
211 );\r
212 if (EFI_ERROR (Status)) {\r
213 goto FreeTxSharedReqBuffer;\r
214 }\r
215\r
216 Dev->TxSharedReq = TxSharedReqBuffer;\r
217\r
218\r
c6e2d064
LE
219 //\r
220 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on\r
221 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.\r
222 //\r
223 TxSharedReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?\r
891f016c
BS
224 sizeof (Dev->TxSharedReq->V0_9_5) :\r
225 sizeof *Dev->TxSharedReq;\r
c6e2d064 226\r
12384f9b
LE
227 for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {\r
228 UINT16 DescIdx;\r
229\r
230 DescIdx = (UINT16) (2 * PktIdx);\r
231 Dev->TxFreeStack[PktIdx] = DescIdx;\r
232\r
233 //\r
234 // For each possibly pending packet, lay out the descriptor for the common\r
235 // (unmodified by the host) virtio-net request header.\r
236 //\r
891f016c 237 Dev->TxRing.Desc[DescIdx].Addr = DeviceAddress;\r
c6e2d064 238 Dev->TxRing.Desc[DescIdx].Len = (UINT32) TxSharedReqSize;\r
12384f9b 239 Dev->TxRing.Desc[DescIdx].Flags = VRING_DESC_F_NEXT;\r
9f3acbb5 240 Dev->TxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);\r
12384f9b
LE
241\r
242 //\r
243 // The second descriptor of each pending TX packet is updated on the fly,\r
244 // but it always terminates the descriptor chain of the packet.\r
245 //\r
246 Dev->TxRing.Desc[DescIdx + 1].Flags = 0;\r
247 }\r
248\r
249 //\r
250 // virtio-0.9.5, Appendix C, Packet Transmission\r
251 //\r
891f016c
BS
252 Dev->TxSharedReq->V0_9_5.Flags = 0;\r
253 Dev->TxSharedReq->V0_9_5.GsoType = VIRTIO_NET_HDR_GSO_NONE;\r
c6e2d064
LE
254\r
255 //\r
256 // For VirtIo 1.0 only -- the field exists, but it is unused\r
257 //\r
891f016c 258 Dev->TxSharedReq->NumBuffers = 0;\r
12384f9b
LE
259\r
260 //\r
261 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
262 //\r
263 MemoryFence ();\r
264 Dev->TxLastUsed = *Dev->TxRing.Used.Idx;\r
265 ASSERT (Dev->TxLastUsed == 0);\r
266\r
267 //\r
268 // want no interrupt when a transmit completes\r
269 //\r
270 *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
271\r
272 return EFI_SUCCESS;\r
891f016c
BS
273\r
274FreeTxSharedReqBuffer:\r
275 Dev->VirtIo->FreeSharedPages (\r
276 Dev->VirtIo,\r
277 EFI_SIZE_TO_PAGES (sizeof *(Dev->TxSharedReq)),\r
278 TxSharedReqBuffer\r
279 );\r
bd114d9f
BS
280\r
281UninitTxBufCollection:\r
282 OrderedCollectionUninit (Dev->TxBufCollection);\r
283\r
891f016c
BS
284FreeTxFreeStack:\r
285 FreePool (Dev->TxFreeStack);\r
286\r
287 return Status;\r
12384f9b
LE
288}\r
289\r
290\r
291/**\r
292 Set up static scaffolding for the VirtioNetReceive() SNP method and enable\r
293 live device operation.\r
294\r
295 This function may only be called as VirtioNetInitialize()'s final step.\r
296\r
297 The structures laid out and resources configured include:\r
298 - destination area for the host to write virtio-net request headers and\r
299 packet data into,\r
300 - select polling over RX interrupt,\r
301 - fully populate the RX queue with a static pattern of virtio descriptor\r
302 chains.\r
303\r
304 @param[in,out] Dev The VNET_DEV driver instance about to enter the\r
305 EfiSimpleNetworkInitialized state.\r
306\r
46b11f00
BS
307 @return Status codes from VIRTIO_CFG_WRITE() or\r
308 VIRTIO_DEVICE_PROTOCOL.AllocateSharedPages or\r
309 VirtioMapAllBytesInSharedBuffer().\r
12384f9b
LE
310 @retval EFI_SUCCESS RX setup successful. The device is live and may\r
311 already be writing to the receive area.\r
312*/\r
313\r
314STATIC\r
315EFI_STATUS\r
316EFIAPI\r
317VirtioNetInitRx (\r
318 IN OUT VNET_DEV *Dev\r
319 )\r
320{\r
46b11f00
BS
321 EFI_STATUS Status;\r
322 UINTN VirtioNetReqSize;\r
323 UINTN RxBufSize;\r
324 UINT16 RxAlwaysPending;\r
325 UINTN PktIdx;\r
326 UINT16 DescIdx;\r
327 UINTN NumBytes;\r
328 EFI_PHYSICAL_ADDRESS RxBufDeviceAddress;\r
329 VOID *RxBuffer;\r
12384f9b 330\r
c6e2d064
LE
331 //\r
332 // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on\r
333 // VIRTIO_NET_F_MRG_RXBUF, which we never negotiate.\r
334 //\r
335 VirtioNetReqSize = (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) ?\r
336 sizeof (VIRTIO_NET_REQ) :\r
337 sizeof (VIRTIO_1_0_NET_REQ);\r
338\r
12384f9b
LE
339 //\r
340 // For each incoming packet we must supply two descriptors:\r
341 // - the recipient for the virtio-net request header, plus\r
342 // - the recipient for the network data (which consists of Ethernet header\r
343 // and Ethernet payload).\r
344 //\r
c6e2d064 345 RxBufSize = VirtioNetReqSize +\r
12384f9b
LE
346 (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);\r
347\r
348 //\r
349 // Limit the number of pending RX packets if the queue is big. The division\r
350 // by two is due to the above "two descriptors per packet" trait.\r
351 //\r
9f3acbb5 352 RxAlwaysPending = (UINT16) MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);\r
12384f9b 353\r
46b11f00
BS
354 //\r
355 // The RxBuf is shared between guest and hypervisor, use\r
356 // AllocateSharedPages() to allocate this memory region and map it with\r
357 // BusMasterCommonBuffer so that it can be accessed by both guest and\r
358 // hypervisor.\r
359 //\r
360 NumBytes = RxAlwaysPending * RxBufSize;\r
361 Dev->RxBufNrPages = EFI_SIZE_TO_PAGES (NumBytes);\r
362 Status = Dev->VirtIo->AllocateSharedPages (\r
363 Dev->VirtIo,\r
364 Dev->RxBufNrPages,\r
365 &RxBuffer\r
366 );\r
367 if (EFI_ERROR (Status)) {\r
368 return Status;\r
369 }\r
370\r
371 ZeroMem (RxBuffer, NumBytes);\r
372\r
373 Status = VirtioMapAllBytesInSharedBuffer (\r
374 Dev->VirtIo,\r
375 VirtioOperationBusMasterCommonBuffer,\r
376 RxBuffer,\r
377 NumBytes,\r
378 &Dev->RxBufDeviceBase,\r
379 &Dev->RxBufMap\r
380 );\r
381 if (EFI_ERROR (Status)) {\r
382 goto FreeSharedBuffer;\r
12384f9b
LE
383 }\r
384\r
46b11f00
BS
385 Dev->RxBuf = RxBuffer;\r
386\r
12384f9b
LE
387 //\r
388 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
389 //\r
390 MemoryFence ();\r
391 Dev->RxLastUsed = *Dev->RxRing.Used.Idx;\r
392 ASSERT (Dev->RxLastUsed == 0);\r
393\r
394 //\r
395 // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:\r
396 // the host should not send interrupts, we'll poll in VirtioNetReceive()\r
397 // and VirtioNetIsPacketAvailable().\r
398 //\r
399 *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
400\r
401 //\r
402 // now set up a separate, two-part descriptor chain for each RX packet, and\r
403 // link each chain into (from) the available ring as well\r
404 //\r
405 DescIdx = 0;\r
46b11f00 406 RxBufDeviceAddress = Dev->RxBufDeviceBase;\r
12384f9b
LE
407 for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {\r
408 //\r
409 // virtio-0.9.5, 2.4.1.2 Updating the Available Ring\r
410 // invisible to the host until we update the Index Field\r
411 //\r
412 Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;\r
413\r
414 //\r
415 // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table\r
416 //\r
46b11f00 417 Dev->RxRing.Desc[DescIdx].Addr = RxBufDeviceAddress;\r
c6e2d064 418 Dev->RxRing.Desc[DescIdx].Len = (UINT32) VirtioNetReqSize;\r
12384f9b 419 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE | VRING_DESC_F_NEXT;\r
9f3acbb5 420 Dev->RxRing.Desc[DescIdx].Next = (UINT16) (DescIdx + 1);\r
46b11f00 421 RxBufDeviceAddress += Dev->RxRing.Desc[DescIdx++].Len;\r
12384f9b 422\r
46b11f00 423 Dev->RxRing.Desc[DescIdx].Addr = RxBufDeviceAddress;\r
c6e2d064 424 Dev->RxRing.Desc[DescIdx].Len = (UINT32) (RxBufSize - VirtioNetReqSize);\r
12384f9b 425 Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE;\r
46b11f00 426 RxBufDeviceAddress += Dev->RxRing.Desc[DescIdx++].Len;\r
12384f9b
LE
427 }\r
428\r
429 //\r
430 // virtio-0.9.5, 2.4.1.3 Updating the Index Field\r
431 //\r
432 MemoryFence ();\r
433 *Dev->RxRing.Avail.Idx = RxAlwaysPending;\r
434\r
435 //\r
436 // At this point reception may already be running. In order to make it sure,\r
437 // kick the hypervisor. If we fail to kick it, we must first abort reception\r
438 // before tearing down anything, because reception may have been already\r
439 // running even without the kick.\r
440 //\r
441 // virtio-0.9.5, 2.4.1.4 Notifying the Device\r
442 //\r
443 MemoryFence ();\r
56f65ed8 444 Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);\r
12384f9b 445 if (EFI_ERROR (Status)) {\r
56f65ed8 446 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
46b11f00 447 goto UnmapSharedBuffer;\r
12384f9b
LE
448 }\r
449\r
450 return Status;\r
46b11f00
BS
451\r
452UnmapSharedBuffer:\r
453 Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RxBufMap);\r
454\r
455FreeSharedBuffer:\r
456 Dev->VirtIo->FreeSharedPages (\r
457 Dev->VirtIo,\r
458 Dev->RxBufNrPages,\r
459 RxBuffer\r
460 );\r
461 return Status;\r
12384f9b
LE
462}\r
463\r
464\r
465/**\r
466 Resets a network adapter and allocates the transmit and receive buffers\r
467 required by the network interface; optionally, also requests allocation of\r
468 additional transmit and receive buffers.\r
469\r
470 @param This The protocol instance pointer.\r
471 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer\r
472 space that the driver should allocate for the\r
473 network interface. Some network interfaces will not\r
474 be able to use the extra buffer, and the caller\r
475 will not know if it is actually being used.\r
476 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer\r
477 space that the driver should allocate for the\r
478 network interface. Some network interfaces will not\r
479 be able to use the extra buffer, and the caller\r
480 will not know if it is actually being used.\r
481\r
482 @retval EFI_SUCCESS The network interface was initialized.\r
483 @retval EFI_NOT_STARTED The network interface has not been started.\r
484 @retval EFI_OUT_OF_RESOURCES There was not enough memory for the transmit\r
485 and receive buffers.\r
486 @retval EFI_INVALID_PARAMETER One or more of the parameters has an\r
487 unsupported value.\r
488 @retval EFI_DEVICE_ERROR The command could not be sent to the network\r
489 interface.\r
490 @retval EFI_UNSUPPORTED This function is not supported by the network\r
491 interface.\r
492\r
493**/\r
494\r
495EFI_STATUS\r
496EFIAPI\r
497VirtioNetInitialize (\r
498 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,\r
499 IN UINTN ExtraRxBufferSize OPTIONAL,\r
500 IN UINTN ExtraTxBufferSize OPTIONAL\r
501 )\r
502{\r
503 VNET_DEV *Dev;\r
504 EFI_TPL OldTpl;\r
505 EFI_STATUS Status;\r
506 UINT8 NextDevStat;\r
bc8fde6f 507 UINT64 Features;\r
12384f9b
LE
508\r
509 if (This == NULL) {\r
510 return EFI_INVALID_PARAMETER;\r
511 }\r
512 if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {\r
513 return EFI_UNSUPPORTED;\r
514 }\r
515\r
516 Dev = VIRTIO_NET_FROM_SNP (This);\r
517 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
518 if (Dev->Snm.State != EfiSimpleNetworkStarted) {\r
519 Status = EFI_NOT_STARTED;\r
520 goto InitFailed;\r
521 }\r
522\r
523 //\r
524 // In the EfiSimpleNetworkStarted state the virtio-net device has status\r
525 // value 0 (= reset) -- see the state diagram, the full call chain to\r
526 // the end of VirtioNetGetFeatures() (considering we're here now),\r
527 // the DeviceFailed label below, and VirtioNetShutdown().\r
528 //\r
529 // Accordingly, the below is a subsequence of the steps found in the\r
530 // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.\r
531 //\r
532 NextDevStat = VSTAT_ACK; // step 2 -- acknowledge device presence\r
56f65ed8 533 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
12384f9b
LE
534 if (EFI_ERROR (Status)) {\r
535 goto InitFailed;\r
536 }\r
537\r
538 NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it\r
56f65ed8
OM
539 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
540 if (EFI_ERROR (Status)) {\r
541 goto DeviceFailed;\r
542 }\r
543\r
544 //\r
545 // Set Page Size - MMIO VirtIo Specific\r
546 //\r
547 Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);\r
12384f9b
LE
548 if (EFI_ERROR (Status)) {\r
549 goto DeviceFailed;\r
550 }\r
551\r
552 //\r
553 // step 4a -- retrieve features. Note that we're past validating required\r
554 // features in VirtioNetGetFeatures().\r
555 //\r
56f65ed8 556 Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);\r
12384f9b
LE
557 if (EFI_ERROR (Status)) {\r
558 goto DeviceFailed;\r
559 }\r
56f65ed8 560\r
12384f9b
LE
561 ASSERT (Features & VIRTIO_NET_F_MAC);\r
562 ASSERT (Dev->Snm.MediaPresentSupported ==\r
563 !!(Features & VIRTIO_NET_F_STATUS));\r
564\r
f60ed5ab
BS
565 Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_F_VERSION_1 |\r
566 VIRTIO_F_IOMMU_PLATFORM;\r
33c6b934
LE
567\r
568 //\r
569 // In virtio-1.0, feature negotiation is expected to complete before queue\r
570 // discovery, and the device can also reject the selected set of features.\r
571 //\r
572 if (Dev->VirtIo->Revision >= VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
573 Status = Virtio10WriteFeatures (Dev->VirtIo, Features, &NextDevStat);\r
574 if (EFI_ERROR (Status)) {\r
575 goto DeviceFailed;\r
576 }\r
577 }\r
578\r
12384f9b
LE
579 //\r
580 // step 4b, 4c -- allocate and report virtqueues\r
581 //\r
940baec0
BS
582 Status = VirtioNetInitRing (\r
583 Dev,\r
584 VIRTIO_NET_Q_RX,\r
585 &Dev->RxRing,\r
586 &Dev->RxRingMap\r
587 );\r
12384f9b
LE
588 if (EFI_ERROR (Status)) {\r
589 goto DeviceFailed;\r
590 }\r
591\r
940baec0
BS
592 Status = VirtioNetInitRing (\r
593 Dev,\r
594 VIRTIO_NET_Q_TX,\r
595 &Dev->TxRing,\r
596 &Dev->TxRingMap\r
597 );\r
12384f9b
LE
598 if (EFI_ERROR (Status)) {\r
599 goto ReleaseRxRing;\r
600 }\r
601\r
602 //\r
603 // step 5 -- keep only the features we want\r
604 //\r
33c6b934 605 if (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
f60ed5ab 606 Features &= ~(UINT64)(VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM);\r
33c6b934
LE
607 Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);\r
608 if (EFI_ERROR (Status)) {\r
609 goto ReleaseTxRing;\r
610 }\r
12384f9b
LE
611 }\r
612\r
613 //\r
614 // step 6 -- virtio-net initialization complete\r
615 //\r
616 NextDevStat |= VSTAT_DRIVER_OK;\r
56f65ed8 617 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
12384f9b
LE
618 if (EFI_ERROR (Status)) {\r
619 goto ReleaseTxRing;\r
620 }\r
621\r
622 Status = VirtioNetInitTx (Dev);\r
623 if (EFI_ERROR (Status)) {\r
624 goto AbortDevice;\r
625 }\r
626\r
627 //\r
628 // start receiving\r
629 //\r
630 Status = VirtioNetInitRx (Dev);\r
631 if (EFI_ERROR (Status)) {\r
632 goto ReleaseTxAux;\r
633 }\r
634\r
635 Dev->Snm.State = EfiSimpleNetworkInitialized;\r
636 gBS->RestoreTPL (OldTpl);\r
637 return EFI_SUCCESS;\r
638\r
639ReleaseTxAux:\r
640 VirtioNetShutdownTx (Dev);\r
641\r
642AbortDevice:\r
56f65ed8 643 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
12384f9b
LE
644\r
645ReleaseTxRing:\r
940baec0 646 VirtioNetUninitRing (Dev, &Dev->TxRing, Dev->TxRingMap);\r
12384f9b
LE
647\r
648ReleaseRxRing:\r
940baec0 649 VirtioNetUninitRing (Dev, &Dev->RxRing, Dev->RxRingMap);\r
12384f9b
LE
650\r
651DeviceFailed:\r
652 //\r
653 // restore device status invariant for the EfiSimpleNetworkStarted state\r
654 //\r
56f65ed8 655 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
12384f9b
LE
656\r
657InitFailed:\r
658 gBS->RestoreTPL (OldTpl);\r
659 return Status;\r
660}\r