]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/VirtioScsiDxe/VirtioScsi.c
OvmfPkg:Fix VS2012 build failure
[mirror_edk2.git] / OvmfPkg / VirtioScsiDxe / VirtioScsi.c
index 2223c9c33e8ec774bb7ac22d0ee60571afc29dd4..e773ecf7cc009dbe39056610cf93fd6704586420 100644 (file)
@@ -26,7 +26,8 @@
     unreasonable for now.\r
 \r
   Copyright (C) 2012, Red Hat, Inc.\r
-  Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2017, AMD Inc, All rights reserved.<BR>\r
 \r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
                        one of UINT8, UINT16, UINT32, UINT64.\r
 \r
 \r
-  @return  Status codes returned by VirtioWriteDevice() / VirtioReadDevice().\r
+  @return  Status codes returned by Virtio->WriteDevice() / Virtio->ReadDevice().\r
 \r
 **/\r
 \r
-#define VIRTIO_CFG_WRITE(Dev, Field, Value)  (VirtioWriteDevice (        \\r
-                                                (Dev)->VirtIo,           \\r
-                                                OFFSET_OF_VSCSI (Field), \\r
-                                                SIZE_OF_VSCSI (Field),   \\r
-                                                (Value)                  \\r
+#define VIRTIO_CFG_WRITE(Dev, Field, Value)  ((Dev)->VirtIo->WriteDevice (  \\r
+                                                (Dev)->VirtIo,              \\r
+                                                OFFSET_OF_VSCSI (Field),    \\r
+                                                SIZE_OF_VSCSI (Field),      \\r
+                                                (Value)                     \\r
                                                 ))\r
 \r
-#define VIRTIO_CFG_READ(Dev, Field, Pointer) (VirtioReadDevice (         \\r
-                                                (Dev)->VirtIo,           \\r
-                                                OFFSET_OF_VSCSI (Field), \\r
-                                                SIZE_OF_VSCSI (Field),   \\r
-                                                sizeof *(Pointer),       \\r
-                                                (Pointer)                \\r
+#define VIRTIO_CFG_READ(Dev, Field, Pointer) ((Dev)->VirtIo->ReadDevice (   \\r
+                                                (Dev)->VirtIo,              \\r
+                                                OFFSET_OF_VSCSI (Field),    \\r
+                                                SIZE_OF_VSCSI (Field),      \\r
+                                                sizeof *(Pointer),          \\r
+                                                (Pointer)                   \\r
                                                 ))\r
 \r
 \r
@@ -96,7 +97,7 @@
 // set some fields in the EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET in/out\r
 // parameter on return. The following is a full list of those fields, for\r
 // easier validation of PopulateRequest(), ParseResponse(), and\r
-// VirtioScsiPassThru() below.\r
+// ReportHostAdapterError() below.\r
 //\r
 // - InTransferLength\r
 // - OutTransferLength\r
@@ -253,7 +254,7 @@ PopulateRequest (
   //\r
   Request->Lun[0] = 1;\r
   Request->Lun[1] = (UINT8) Target;\r
-  Request->Lun[2] = (UINT8) ((Lun >> 8) | 0x40);\r
+  Request->Lun[2] = (UINT8) (((UINT32)Lun >> 8) | 0x40);\r
   Request->Lun[3] = (UINT8) Lun;\r
 \r
   //\r
@@ -387,6 +388,37 @@ ParseResponse (
 }\r
 \r
 \r
+/**\r
+\r
+  The function can be used to create a fake host adapter error.\r
+\r
+  When VirtioScsiPassThru() is failed due to some reasons then this function\r
+  can be called to construct a host adapter error.\r
+\r
+  @param[out] Packet  The Extended SCSI Pass Thru Protocol packet that the host\r
+                      adapter error shall be placed in.\r
+\r
+\r
+  @retval EFI_DEVICE_ERROR  The function returns this status code\r
+                            unconditionally, to be propagated by\r
+                            VirtioScsiPassThru().\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+ReportHostAdapterError (\r
+  OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET  *Packet\r
+  )\r
+{\r
+  Packet->InTransferLength  = 0;\r
+  Packet->OutTransferLength = 0;\r
+  Packet->HostAdapterStatus = EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OTHER;\r
+  Packet->TargetStatus      = EFI_EXT_SCSI_STATUS_TARGET_GOOD;\r
+  Packet->SenseDataLength   = 0;\r
+  return EFI_DEVICE_ERROR;\r
+}\r
+\r
+\r
 //\r
 // The next seven functions implement EFI_EXT_SCSI_PASS_THRU_PROTOCOL\r
 // for the virtio-scsi HBA. Refer to UEFI Spec 2.3.1 + Errata C, sections\r
@@ -408,26 +440,166 @@ VirtioScsiPassThru (
   UINT16                    TargetValue;\r
   EFI_STATUS                Status;\r
   volatile VIRTIO_SCSI_REQ  Request;\r
-  volatile VIRTIO_SCSI_RESP Response;\r
+  volatile VIRTIO_SCSI_RESP *Response;\r
+  VOID                      *ResponseBuffer;\r
   DESC_INDICES              Indices;\r
+  VOID                      *RequestMapping;\r
+  VOID                      *ResponseMapping;\r
+  VOID                      *InDataMapping;\r
+  VOID                      *OutDataMapping;\r
+  EFI_PHYSICAL_ADDRESS      RequestDeviceAddress;\r
+  EFI_PHYSICAL_ADDRESS      ResponseDeviceAddress;\r
+  EFI_PHYSICAL_ADDRESS      InDataDeviceAddress;\r
+  EFI_PHYSICAL_ADDRESS      OutDataDeviceAddress;\r
+  VOID                      *InDataBuffer;\r
+  UINTN                     InDataNumPages;\r
+  BOOLEAN                   OutDataBufferIsMapped;\r
+\r
+  //\r
+  // Set InDataMapping,OutDataMapping,InDataDeviceAddress and OutDataDeviceAddress to\r
+  // suppress incorrect compiler/analyzer warnings.\r
+  //\r
+  InDataMapping        = NULL;\r
+  OutDataMapping       = NULL;\r
+  InDataDeviceAddress  = 0;\r
+  OutDataDeviceAddress = 0;\r
 \r
   ZeroMem ((VOID*) &Request, sizeof (Request));\r
-  ZeroMem ((VOID*) &Response, sizeof (Response));\r
 \r
   Dev = VIRTIO_SCSI_FROM_PASS_THRU (This);\r
   CopyMem (&TargetValue, Target, sizeof TargetValue);\r
 \r
+  InDataBuffer = NULL;\r
+  OutDataBufferIsMapped = FALSE;\r
+  InDataNumPages = 0;\r
+\r
   Status = PopulateRequest (Dev, TargetValue, Lun, Packet, &Request);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  VirtioPrepare (&Dev->Ring, &Indices);\r
+  //\r
+  // Map the virtio-scsi Request header buffer\r
+  //\r
+  Status = VirtioMapAllBytesInSharedBuffer (\r
+             Dev->VirtIo,\r
+             VirtioOperationBusMasterRead,\r
+             (VOID *) &Request,\r
+             sizeof Request,\r
+             &RequestDeviceAddress,\r
+             &RequestMapping);\r
+  if (EFI_ERROR (Status)) {\r
+    return ReportHostAdapterError (Packet);\r
+  }\r
+\r
+  //\r
+  // Map the input buffer\r
+  //\r
+  if (Packet->InTransferLength > 0) {\r
+    //\r
+    // Allocate a intermediate input buffer. This is mainly to handle the\r
+    // following case:\r
+    //  * caller submits a bi-directional request\r
+    //  * we perform the request fine\r
+    //  * but we fail to unmap the "InDataMapping"\r
+    //\r
+    // In that case simply returing the EFI_DEVICE_ERROR is not sufficient. In\r
+    // addition to the error code we also need to update Packet fields\r
+    // accordingly so that we report the full loss of the incoming transfer.\r
+    //\r
+    // We allocate a temporary buffer and map it with BusMasterCommonBuffer. If\r
+    // the Virtio request is successful then we copy the data from temporary\r
+    // buffer into Packet->InDataBuffer.\r
+    //\r
+    InDataNumPages = EFI_SIZE_TO_PAGES ((UINTN)Packet->InTransferLength);\r
+    Status = Dev->VirtIo->AllocateSharedPages (\r
+                            Dev->VirtIo,\r
+                            InDataNumPages,\r
+                            &InDataBuffer\r
+                            );\r
+    if (EFI_ERROR (Status)) {\r
+      Status = ReportHostAdapterError (Packet);\r
+      goto UnmapRequestBuffer;\r
+    }\r
+\r
+    ZeroMem (InDataBuffer, Packet->InTransferLength);\r
+\r
+    Status = VirtioMapAllBytesInSharedBuffer (\r
+               Dev->VirtIo,\r
+               VirtioOperationBusMasterCommonBuffer,\r
+               InDataBuffer,\r
+               Packet->InTransferLength,\r
+               &InDataDeviceAddress,\r
+               &InDataMapping\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      Status = ReportHostAdapterError (Packet);\r
+      goto FreeInDataBuffer;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Map the output buffer\r
+  //\r
+  if (Packet->OutTransferLength > 0) {\r
+    Status = VirtioMapAllBytesInSharedBuffer (\r
+               Dev->VirtIo,\r
+               VirtioOperationBusMasterRead,\r
+               Packet->OutDataBuffer,\r
+               Packet->OutTransferLength,\r
+               &OutDataDeviceAddress,\r
+               &OutDataMapping\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      Status = ReportHostAdapterError (Packet);\r
+      goto UnmapInDataBuffer;\r
+    }\r
+\r
+    OutDataBufferIsMapped = TRUE;\r
+  }\r
+\r
+  //\r
+  // Response header is bi-direction (we preset with host status and expect\r
+  // the device to update it). Allocate a response buffer which can be mapped\r
+  // to access equally by both processor and device.\r
+  //\r
+  Status = Dev->VirtIo->AllocateSharedPages (\r
+                          Dev->VirtIo,\r
+                          EFI_SIZE_TO_PAGES (sizeof *Response),\r
+                          &ResponseBuffer\r
+                          );\r
+  if (EFI_ERROR (Status)) {\r
+    Status = ReportHostAdapterError (Packet);\r
+    goto UnmapOutDataBuffer;\r
+  }\r
+\r
+  Response = ResponseBuffer;\r
+\r
+  ZeroMem ((VOID *)Response, sizeof (*Response));\r
 \r
   //\r
   // preset a host status for ourselves that we do not accept as success\r
   //\r
-  Response.Response = VIRTIO_SCSI_S_FAILURE;\r
+  Response->Response = VIRTIO_SCSI_S_FAILURE;\r
+\r
+  //\r
+  // Map the response buffer with BusMasterCommonBuffer so that response\r
+  // buffer can be accessed by both host and device.\r
+  //\r
+  Status = VirtioMapAllBytesInSharedBuffer (\r
+             Dev->VirtIo,\r
+             VirtioOperationBusMasterCommonBuffer,\r
+             ResponseBuffer,\r
+             sizeof (*Response),\r
+             &ResponseDeviceAddress,\r
+             &ResponseMapping\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    Status = ReportHostAdapterError (Packet);\r
+    goto FreeResponseBuffer;\r
+  }\r
+\r
+  VirtioPrepare (&Dev->Ring, &Indices);\r
 \r
   //\r
   // ensured by VirtioScsiInit() -- this predicate, in combination with the\r
@@ -438,31 +610,49 @@ VirtioScsiPassThru (
   //\r
   // enqueue Request\r
   //\r
-  VirtioAppendDesc (&Dev->Ring, (UINTN) &Request, sizeof Request,\r
-    VRING_DESC_F_NEXT, &Indices);\r
+  VirtioAppendDesc (\r
+    &Dev->Ring,\r
+    RequestDeviceAddress,\r
+    sizeof Request,\r
+    VRING_DESC_F_NEXT,\r
+    &Indices\r
+    );\r
 \r
   //\r
   // enqueue "dataout" if any\r
   //\r
   if (Packet->OutTransferLength > 0) {\r
-    VirtioAppendDesc (&Dev->Ring, (UINTN) Packet->OutDataBuffer,\r
-      Packet->OutTransferLength, VRING_DESC_F_NEXT, &Indices);\r
+    VirtioAppendDesc (\r
+      &Dev->Ring,\r
+      OutDataDeviceAddress,\r
+      Packet->OutTransferLength,\r
+      VRING_DESC_F_NEXT,\r
+      &Indices\r
+      );\r
   }\r
 \r
   //\r
   // enqueue Response, to be written by the host\r
   //\r
-  VirtioAppendDesc (&Dev->Ring, (UINTN) &Response, sizeof Response,\r
-    VRING_DESC_F_WRITE | (Packet->InTransferLength > 0 ?\r
-                          VRING_DESC_F_NEXT : 0),\r
-    &Indices);\r
+  VirtioAppendDesc (\r
+    &Dev->Ring,\r
+    ResponseDeviceAddress,\r
+    sizeof *Response,\r
+    VRING_DESC_F_WRITE | (Packet->InTransferLength > 0 ? VRING_DESC_F_NEXT : 0),\r
+    &Indices\r
+    );\r
 \r
   //\r
   // enqueue "datain" if any, to be written by the host\r
   //\r
   if (Packet->InTransferLength > 0) {\r
-    VirtioAppendDesc (&Dev->Ring, (UINTN) Packet->InDataBuffer,\r
-      Packet->InTransferLength, VRING_DESC_F_WRITE, &Indices);\r
+    VirtioAppendDesc (\r
+      &Dev->Ring,\r
+      InDataDeviceAddress,\r
+      Packet->InTransferLength,\r
+      VRING_DESC_F_WRITE,\r
+      &Indices\r
+      );\r
   }\r
 \r
   // If kicking the host fails, we must fake a host adapter error.\r
@@ -470,16 +660,51 @@ VirtioScsiPassThru (
   // caller retry.\r
   //\r
   if (VirtioFlush (Dev->VirtIo, VIRTIO_SCSI_REQUEST_QUEUE, &Dev->Ring,\r
-        &Indices) != EFI_SUCCESS) {\r
-    Packet->InTransferLength  = 0;\r
-    Packet->OutTransferLength = 0;\r
-    Packet->HostAdapterStatus = EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OTHER;\r
-    Packet->TargetStatus      = EFI_EXT_SCSI_STATUS_TARGET_GOOD;\r
-    Packet->SenseDataLength   = 0;\r
-    return EFI_DEVICE_ERROR;\r
+        &Indices, NULL) != EFI_SUCCESS) {\r
+    Status = ReportHostAdapterError (Packet);\r
+    goto UnmapResponseBuffer;\r
+  }\r
+\r
+  Status = ParseResponse (Packet, Response);\r
+\r
+  //\r
+  // If virtio request was successful and it was a CPU read request then we\r
+  // have used an intermediate buffer. Copy the data from intermediate buffer\r
+  // to the final buffer.\r
+  //\r
+  if (InDataBuffer != NULL) {\r
+    CopyMem (Packet->InDataBuffer, InDataBuffer, Packet->InTransferLength);\r
+  }\r
+\r
+UnmapResponseBuffer:\r
+  Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, ResponseMapping);\r
+\r
+FreeResponseBuffer:\r
+  Dev->VirtIo->FreeSharedPages (\r
+                 Dev->VirtIo,\r
+                 EFI_SIZE_TO_PAGES (sizeof *Response),\r
+                 ResponseBuffer\r
+                 );\r
+\r
+UnmapOutDataBuffer:\r
+  if (OutDataBufferIsMapped) {\r
+    Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, OutDataMapping);\r
+  }\r
+\r
+UnmapInDataBuffer:\r
+  if (InDataBuffer != NULL) {\r
+    Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, InDataMapping);\r
   }\r
 \r
-  return ParseResponse (Packet, &Response);\r
+FreeInDataBuffer:\r
+  if (InDataBuffer != NULL) {\r
+    Dev->VirtIo->FreeSharedPages (Dev->VirtIo, InDataNumPages, InDataBuffer);\r
+  }\r
+\r
+UnmapRequestBuffer:\r
+  Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, RequestMapping);\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -706,8 +931,8 @@ VirtioScsiInit (
 {\r
   UINT8      NextDevStat;\r
   EFI_STATUS Status;\r
-\r
-  UINT32     Features;\r
+  UINT64     RingBaseShift;\r
+  UINT64     Features;\r
   UINT16     MaxChannel; // for validation only\r
   UINT32     NumQueues;  // for validation only\r
   UINT16     QueueSize;\r
@@ -748,7 +973,7 @@ VirtioScsiInit (
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
-  Dev->InOutSupported = !!(Features & VIRTIO_SCSI_F_INOUT);\r
+  Dev->InOutSupported = (BOOLEAN) ((Features & VIRTIO_SCSI_F_INOUT) != 0);\r
 \r
   Status = VIRTIO_CFG_READ (Dev, MaxChannel, &MaxChannel);\r
   if (EFI_ERROR (Status)) {\r
@@ -800,6 +1025,20 @@ VirtioScsiInit (
     goto Failed;\r
   }\r
 \r
+  Features &= VIRTIO_SCSI_F_INOUT | VIRTIO_F_VERSION_1 |\r
+              VIRTIO_F_IOMMU_PLATFORM;\r
+\r
+  //\r
+  // In virtio-1.0, feature negotiation is expected to complete before queue\r
+  // discovery, and the device can also reject the selected set of features.\r
+  //\r
+  if (Dev->VirtIo->Revision >= VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
+    Status = Virtio10WriteFeatures (Dev->VirtIo, Features, &NextDevStat);\r
+    if (EFI_ERROR (Status)) {\r
+      goto Failed;\r
+    }\r
+  }\r
+\r
   //\r
   // step 4b -- allocate request virtqueue\r
   //\r
@@ -819,43 +1058,59 @@ VirtioScsiInit (
     goto Failed;\r
   }\r
 \r
-  Status = VirtioRingInit (QueueSize, &Dev->Ring);\r
+  Status = VirtioRingInit (Dev->VirtIo, QueueSize, &Dev->Ring);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
 \r
+  //\r
+  // If anything fails from here on, we must release the ring resources\r
+  //\r
+  Status = VirtioRingMap (\r
+             Dev->VirtIo,\r
+             &Dev->Ring,\r
+             &RingBaseShift,\r
+             &Dev->RingMap\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ReleaseQueue;\r
+  }\r
+\r
   //\r
   // Additional steps for MMIO: align the queue appropriately, and set the\r
-  // size. If anything fails from here on, we must release the ring resources.\r
+  // size. If anything fails from here on, we must unmap the ring resources.\r
   //\r
   Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);\r
   if (EFI_ERROR (Status)) {\r
-    goto ReleaseQueue;\r
+    goto UnmapQueue;\r
   }\r
 \r
   Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);\r
   if (EFI_ERROR (Status)) {\r
-    goto ReleaseQueue;\r
+    goto UnmapQueue;\r
   }\r
 \r
   //\r
   // step 4c -- Report GPFN (guest-physical frame number) of queue.\r
   //\r
-  Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo,\r
-      (UINT32)(UINTN) Dev->Ring.Base >> EFI_PAGE_SHIFT);\r
+  Status = Dev->VirtIo->SetQueueAddress (\r
+                          Dev->VirtIo,\r
+                          &Dev->Ring,\r
+                          RingBaseShift\r
+                          );\r
   if (EFI_ERROR (Status)) {\r
-    goto ReleaseQueue;\r
+    goto UnmapQueue;\r
   }\r
 \r
   //\r
-  // step 5 -- Report understood features and guest-tuneables. We want none of\r
-  // the known (or unknown) VIRTIO_SCSI_F_* or VIRTIO_F_* capabilities (see\r
-  // virtio-0.9.5, Appendices B and I), except bidirectional transfers.\r
+  // step 5 -- Report understood features and guest-tuneables.\r
   //\r
-  Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo,\r
-      Features & VIRTIO_SCSI_F_INOUT);\r
-  if (EFI_ERROR (Status)) {\r
-    goto ReleaseQueue;\r
+  if (Dev->VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
+    Features &= ~(UINT64)(VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM);\r
+    Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);\r
+    if (EFI_ERROR (Status)) {\r
+      goto UnmapQueue;\r
+    }\r
   }\r
 \r
   //\r
@@ -864,11 +1119,11 @@ VirtioScsiInit (
   //\r
   Status = VIRTIO_CFG_WRITE (Dev, CdbSize, VIRTIO_SCSI_CDB_SIZE);\r
   if (EFI_ERROR (Status)) {\r
-    goto ReleaseQueue;\r
+    goto UnmapQueue;\r
   }\r
   Status = VIRTIO_CFG_WRITE (Dev, SenseSize, VIRTIO_SCSI_SENSE_SIZE);\r
   if (EFI_ERROR (Status)) {\r
-    goto ReleaseQueue;\r
+    goto UnmapQueue;\r
   }\r
 \r
   //\r
@@ -877,7 +1132,7 @@ VirtioScsiInit (
   NextDevStat |= VSTAT_DRIVER_OK;\r
   Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
   if (EFI_ERROR (Status)) {\r
-    goto ReleaseQueue;\r
+    goto UnmapQueue;\r
   }\r
 \r
   //\r
@@ -913,8 +1168,11 @@ VirtioScsiInit (
 \r
   return EFI_SUCCESS;\r
 \r
+UnmapQueue:\r
+  Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap);\r
+\r
 ReleaseQueue:\r
-  VirtioRingUninit (&Dev->Ring);\r
+  VirtioRingUninit (Dev->VirtIo, &Dev->Ring);\r
 \r
 Failed:\r
   //\r
@@ -933,7 +1191,6 @@ Failed:
 }\r
 \r
 \r
-\r
 STATIC\r
 VOID\r
 EFIAPI\r
@@ -953,13 +1210,41 @@ VirtioScsiUninit (
   Dev->MaxLun         = 0;\r
   Dev->MaxSectors     = 0;\r
 \r
-  VirtioRingUninit (&Dev->Ring);\r
+  Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->RingMap);\r
+  VirtioRingUninit (Dev->VirtIo, &Dev->Ring);\r
 \r
   SetMem (&Dev->PassThru,     sizeof Dev->PassThru,     0x00);\r
   SetMem (&Dev->PassThruMode, sizeof Dev->PassThruMode, 0x00);\r
 }\r
 \r
 \r
+//\r
+// Event notification function enqueued by ExitBootServices().\r
+//\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+VirtioScsiExitBoot (\r
+  IN  EFI_EVENT Event,\r
+  IN  VOID      *Context\r
+  )\r
+{\r
+  VSCSI_DEV *Dev;\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a: Context=0x%p\n", __FUNCTION__, Context));\r
+  //\r
+  // Reset the device. This causes the hypervisor to forget about the virtio\r
+  // ring.\r
+  //\r
+  // We allocated said ring in EfiBootServicesData type memory, and code\r
+  // executing after ExitBootServices() is permitted to overwrite it.\r
+  //\r
+  Dev = Context;\r
+  Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
+}\r
+\r
+\r
 //\r
 // Probe, start and stop functions of this driver, called by the DXE core for\r
 // specific devices.\r
@@ -1050,6 +1335,12 @@ VirtioScsiDriverBindingStart (
     goto CloseVirtIo;\r
   }\r
 \r
+  Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,\r
+                  &VirtioScsiExitBoot, Dev, &Dev->ExitBoot);\r
+  if (EFI_ERROR (Status)) {\r
+    goto UninitDev;\r
+  }\r
+\r
   //\r
   // Setup complete, attempt to export the driver instance's PassThru\r
   // interface.\r
@@ -1059,11 +1350,14 @@ VirtioScsiDriverBindingStart (
                   &gEfiExtScsiPassThruProtocolGuid, EFI_NATIVE_INTERFACE,\r
                   &Dev->PassThru);\r
   if (EFI_ERROR (Status)) {\r
-    goto UninitDev;\r
+    goto CloseExitBoot;\r
   }\r
 \r
   return EFI_SUCCESS;\r
 \r
+CloseExitBoot:\r
+  gBS->CloseEvent (Dev->ExitBoot);\r
+\r
 UninitDev:\r
   VirtioScsiUninit (Dev);\r
 \r
@@ -1114,6 +1408,8 @@ VirtioScsiDriverBindingStop (
     return Status;\r
   }\r
 \r
+  gBS->CloseEvent (Dev->ExitBoot);\r
+\r
   VirtioScsiUninit (Dev);\r
 \r
   gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r