]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg NvmExpressDxe: Add BlockIo2 support
authorHao Wu <hao.a.wu@intel.com>
Wed, 27 Apr 2016 05:15:24 +0000 (13:15 +0800)
committerHao Wu <hao.a.wu@intel.com>
Thu, 19 May 2016 04:37:59 +0000 (12:37 +0800)
Together with EFI_BLOCK_IO_PROTOCOL, EFI_BLOCK_IO2_PROTOCOL is also
produced on NVMe devices.

The following Block I/O 2 functions are implemented:
Reset
ReadBlocksEx
WriteBlocksEx
FlushBlocksEx

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Feng Tian <feng.tian@intel.com>
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.c
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.h
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressBlockIo.c
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressBlockIo.h
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressPassthru.c

index cad061bdf76a8ecbb54f88f32e45b18d20af7ffb..cb25b3e08dc62f9a6b2db5fcb56520557406d07a 100644 (file)
@@ -157,6 +157,16 @@ EnumerateNvmeDevNamespace (
     Device->BlockIo.WriteBlocks  = NvmeBlockIoWriteBlocks;\r
     Device->BlockIo.FlushBlocks  = NvmeBlockIoFlushBlocks;\r
 \r
+    //\r
+    // Create BlockIo2 Protocol instance\r
+    //\r
+    Device->BlockIo2.Media          = &Device->Media;\r
+    Device->BlockIo2.Reset          = NvmeBlockIoResetEx;\r
+    Device->BlockIo2.ReadBlocksEx   = NvmeBlockIoReadBlocksEx;\r
+    Device->BlockIo2.WriteBlocksEx  = NvmeBlockIoWriteBlocksEx;\r
+    Device->BlockIo2.FlushBlocksEx  = NvmeBlockIoFlushBlocksEx;\r
+    InitializeListHead (&Device->AsyncQueue);\r
+\r
     //\r
     // Create StorageSecurityProtocol Instance\r
     //\r
@@ -213,6 +223,8 @@ EnumerateNvmeDevNamespace (
                     Device->DevicePath,\r
                     &gEfiBlockIoProtocolGuid,\r
                     &Device->BlockIo,\r
+                    &gEfiBlockIo2ProtocolGuid,\r
+                    &Device->BlockIo2,\r
                     &gEfiDiskInfoProtocolGuid,\r
                     &Device->DiskInfo,\r
                     NULL\r
@@ -239,6 +251,8 @@ EnumerateNvmeDevNamespace (
                Device->DevicePath,\r
                &gEfiBlockIoProtocolGuid,\r
                &Device->BlockIo,\r
+               &gEfiBlockIo2ProtocolGuid,\r
+               &Device->BlockIo2,\r
                &gEfiDiskInfoProtocolGuid,\r
                &Device->DiskInfo,\r
                NULL\r
@@ -380,6 +394,8 @@ UnregisterNvmeNamespace (
   NVME_DEVICE_PRIVATE_DATA                 *Device;\r
   NVME_CONTROLLER_PRIVATE_DATA             *Private;\r
   EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *StorageSecurity;\r
+  BOOLEAN                                  IsEmpty;\r
+  EFI_TPL                                  OldTpl;\r
 \r
   BlockIo = NULL;\r
 \r
@@ -398,6 +414,21 @@ UnregisterNvmeNamespace (
   Device  = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (BlockIo);\r
   Private = Device->Controller;\r
 \r
+  //\r
+  // Wait for the device's asynchronous I/O queue to become empty.\r
+  //\r
+  while (TRUE) {\r
+    OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+    IsEmpty = IsListEmpty (&Device->AsyncQueue);\r
+    gBS->RestoreTPL (OldTpl);\r
+\r
+    if (IsEmpty) {\r
+      break;\r
+    }\r
+\r
+    gBS->Stall (100);\r
+  }\r
+\r
   //\r
   // Close the child handle\r
   //\r
@@ -418,6 +449,8 @@ UnregisterNvmeNamespace (
                   Device->DevicePath,\r
                   &gEfiBlockIoProtocolGuid,\r
                   &Device->BlockIo,\r
+                  &gEfiBlockIo2ProtocolGuid,\r
+                  &Device->BlockIo2,\r
                   &gEfiDiskInfoProtocolGuid,\r
                   &Device->DiskInfo,\r
                   NULL\r
@@ -479,6 +512,170 @@ UnregisterNvmeNamespace (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Call back function when the timer event is signaled.\r
+\r
+  @param[in]  Event     The Event this notify function registered to.\r
+  @param[in]  Context   Pointer to the context data registered to the\r
+                        Event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+ProcessAsyncTaskList (\r
+  IN EFI_EVENT                    Event,\r
+  IN VOID*                        Context\r
+  )\r
+{\r
+  NVME_CONTROLLER_PRIVATE_DATA         *Private;\r
+  EFI_PCI_IO_PROTOCOL                  *PciIo;\r
+  NVME_CQ                              *Cq;\r
+  UINT16                               QueueId;\r
+  UINT32                               Data;\r
+  LIST_ENTRY                           *Link;\r
+  LIST_ENTRY                           *NextLink;\r
+  NVME_PASS_THRU_ASYNC_REQ             *AsyncRequest;\r
+  NVME_BLKIO2_SUBTASK                  *Subtask;\r
+  NVME_BLKIO2_REQUEST                  *BlkIo2Request;\r
+  EFI_BLOCK_IO2_TOKEN                  *Token;\r
+  BOOLEAN                              HasNewItem;\r
+  EFI_STATUS                           Status;\r
+\r
+  Private    = (NVME_CONTROLLER_PRIVATE_DATA*)Context;\r
+  QueueId    = 2;\r
+  Cq         = Private->CqBuffer[QueueId] + Private->CqHdbl[QueueId].Cqh;\r
+  HasNewItem = FALSE;\r
+\r
+  //\r
+  // Submit asynchronous subtasks to the NVMe Submission Queue\r
+  //\r
+  for (Link = GetFirstNode (&Private->UnsubmittedSubtasks);\r
+       !IsNull (&Private->UnsubmittedSubtasks, Link);\r
+       Link = NextLink) {\r
+    NextLink      = GetNextNode (&Private->UnsubmittedSubtasks, Link);\r
+    Subtask       = NVME_BLKIO2_SUBTASK_FROM_LINK (Link);\r
+    BlkIo2Request = Subtask->BlockIo2Request;\r
+    Token         = BlkIo2Request->Token;\r
+    RemoveEntryList (Link);\r
+    BlkIo2Request->UnsubmittedSubtaskNum--;\r
+\r
+    //\r
+    // If any previous subtask fails, do not process subsequent ones.\r
+    //\r
+    if (Token->TransactionStatus != EFI_SUCCESS) {\r
+      if (IsListEmpty (&BlkIo2Request->SubtasksQueue) &&\r
+          BlkIo2Request->LastSubtaskSubmitted &&\r
+          (BlkIo2Request->UnsubmittedSubtaskNum == 0)) {\r
+        //\r
+        // Remove the BlockIo2 request from the device asynchronous queue.\r
+        //\r
+        RemoveEntryList (&BlkIo2Request->Link);\r
+        FreePool (BlkIo2Request);\r
+        gBS->SignalEvent (Token->Event);\r
+      }\r
+\r
+      FreePool (Subtask->CommandPacket->NvmeCmd);\r
+      FreePool (Subtask->CommandPacket->NvmeCompletion);\r
+      FreePool (Subtask->CommandPacket);\r
+      FreePool (Subtask);\r
+\r
+      continue;\r
+    }\r
+\r
+    Status = Private->Passthru.PassThru (\r
+                                 &Private->Passthru,\r
+                                 Subtask->NamespaceId,\r
+                                 Subtask->CommandPacket,\r
+                                 Subtask->Event\r
+                                 );\r
+    if (Status == EFI_NOT_READY) {\r
+      InsertHeadList (&Private->UnsubmittedSubtasks, Link);\r
+      BlkIo2Request->UnsubmittedSubtaskNum++;\r
+      break;\r
+    } else if (EFI_ERROR (Status)) {\r
+      Token->TransactionStatus = EFI_DEVICE_ERROR;\r
+\r
+      if (IsListEmpty (&BlkIo2Request->SubtasksQueue) &&\r
+          Subtask->IsLast) {\r
+        //\r
+        // Remove the BlockIo2 request from the device asynchronous queue.\r
+        //\r
+        RemoveEntryList (&BlkIo2Request->Link);\r
+        FreePool (BlkIo2Request);\r
+        gBS->SignalEvent (Token->Event);\r
+      }\r
+\r
+      FreePool (Subtask->CommandPacket->NvmeCmd);\r
+      FreePool (Subtask->CommandPacket->NvmeCompletion);\r
+      FreePool (Subtask->CommandPacket);\r
+      FreePool (Subtask);\r
+    } else {\r
+      InsertTailList (&BlkIo2Request->SubtasksQueue, Link);\r
+      if (Subtask->IsLast) {\r
+        BlkIo2Request->LastSubtaskSubmitted = TRUE;\r
+      }\r
+    }\r
+  }\r
+\r
+  while (Cq->Pt != Private->Pt[QueueId]) {\r
+    ASSERT (Cq->Sqid == QueueId);\r
+\r
+    HasNewItem = TRUE;\r
+\r
+    //\r
+    // Find the command with given Command Id.\r
+    //\r
+    for (Link = GetFirstNode (&Private->AsyncPassThruQueue);\r
+         !IsNull (&Private->AsyncPassThruQueue, Link);\r
+         Link = NextLink) {\r
+      NextLink = GetNextNode (&Private->AsyncPassThruQueue, Link);\r
+      AsyncRequest = NVME_PASS_THRU_ASYNC_REQ_FROM_THIS (Link);\r
+      if (AsyncRequest->CommandId == Cq->Cid) {\r
+        //\r
+        // Copy the Respose Queue entry for this command to the callers\r
+        // response buffer.\r
+        //\r
+        CopyMem (\r
+          AsyncRequest->Packet->NvmeCompletion,\r
+          Cq,\r
+          sizeof(EFI_NVM_EXPRESS_COMPLETION)\r
+          );\r
+\r
+        RemoveEntryList (Link);\r
+        gBS->SignalEvent (AsyncRequest->CallerEvent);\r
+        FreePool (AsyncRequest);\r
+\r
+        //\r
+        // Update submission queue head.\r
+        //\r
+        Private->AsyncSqHead = Cq->Sqhd;\r
+        break;\r
+      }\r
+    }\r
+\r
+    Private->CqHdbl[QueueId].Cqh++;\r
+    if (Private->CqHdbl[QueueId].Cqh > NVME_ASYNC_CCQ_SIZE) {\r
+      Private->CqHdbl[QueueId].Cqh = 0;\r
+      Private->Pt[QueueId] ^= 1;\r
+    }\r
+\r
+    Cq = Private->CqBuffer[QueueId] + Private->CqHdbl[QueueId].Cqh;\r
+  }\r
+\r
+  if (HasNewItem) {\r
+    PciIo = Private->PciIo;\r
+    Data  = ReadUnaligned32 ((UINT32*)&Private->CqHdbl[QueueId]);\r
+    PciIo->Mem.Write (\r
+                 PciIo,\r
+                 EfiPciIoWidthUint32,\r
+                 NVME_BAR,\r
+                 NVME_CQHDBL_OFFSET(QueueId, Private->Cap.Dstrd),\r
+                 1,\r
+                 &Data\r
+                 );\r
+  }\r
+}\r
+\r
 /**\r
   Tests to see if this driver supports a given controller. If a child device is provided,\r
   it further tests to see if this driver supports creating a handle for the specified child device.\r
@@ -736,19 +933,21 @@ NvmExpressDriverBindingStart (
     }\r
 \r
     //\r
-    // 4 x 4kB aligned buffers will be carved out of this buffer.\r
+    // 6 x 4kB aligned buffers will be carved out of this buffer.\r
     // 1st 4kB boundary is the start of the admin submission queue.\r
     // 2nd 4kB boundary is the start of the admin completion queue.\r
     // 3rd 4kB boundary is the start of I/O submission queue #1.\r
     // 4th 4kB boundary is the start of I/O completion queue #1.\r
+    // 5th 4kB boundary is the start of I/O submission queue #2.\r
+    // 6th 4kB boundary is the start of I/O completion queue #2.\r
     //\r
-    // Allocate 4 pages of memory, then map it for bus master read and write.\r
+    // Allocate 6 pages of memory, then map it for bus master read and write.\r
     //\r
     Status = PciIo->AllocateBuffer (\r
                       PciIo,\r
                       AllocateAnyPages,\r
                       EfiBootServicesData,\r
-                      4,\r
+                      6,\r
                       (VOID**)&Private->Buffer,\r
                       0\r
                       );\r
@@ -756,7 +955,7 @@ NvmExpressDriverBindingStart (
       goto Exit;\r
     }\r
 \r
-    Bytes = EFI_PAGES_TO_SIZE (4);\r
+    Bytes = EFI_PAGES_TO_SIZE (6);\r
     Status = PciIo->Map (\r
                       PciIo,\r
                       EfiPciIoOperationBusMasterCommonBuffer,\r
@@ -766,7 +965,7 @@ NvmExpressDriverBindingStart (
                       &Private->Mapping\r
                       );\r
 \r
-    if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (4))) {\r
+    if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (6))) {\r
       goto Exit;\r
     }\r
 \r
@@ -784,12 +983,37 @@ NvmExpressDriverBindingStart (
     Private->Passthru.BuildDevicePath  = NvmExpressBuildDevicePath;\r
     Private->Passthru.GetNamespace     = NvmExpressGetNamespace;\r
     CopyMem (&Private->PassThruMode, &gEfiNvmExpressPassThruMode, sizeof (EFI_NVM_EXPRESS_PASS_THRU_MODE));\r
+    InitializeListHead (&Private->AsyncPassThruQueue);\r
+    InitializeListHead (&Private->UnsubmittedSubtasks);\r
 \r
     Status = NvmeControllerInit (Private);\r
     if (EFI_ERROR(Status)) {\r
       goto Exit;\r
     }\r
 \r
+    //\r
+    // Start the asynchronous I/O completion monitor\r
+    //\r
+    Status = gBS->CreateEvent (\r
+                    EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                    TPL_NOTIFY,\r
+                    ProcessAsyncTaskList,\r
+                    Private,\r
+                    &Private->TimerEvent\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Exit;\r
+    }\r
+\r
+    Status = gBS->SetTimer (\r
+                    Private->TimerEvent,\r
+                    TimerPeriodic,\r
+                    NVME_HC_ASYNC_TIMER\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Exit;\r
+    }\r
+\r
     Status = gBS->InstallMultipleProtocolInterfaces (\r
                     &Controller,\r
                     &gEfiNvmExpressPassThruProtocolGuid,\r
@@ -850,7 +1074,7 @@ Exit:
   }\r
 \r
   if ((Private != NULL) && (Private->Buffer != NULL)) {\r
-    PciIo->FreeBuffer (PciIo, 4, Private->Buffer);\r
+    PciIo->FreeBuffer (PciIo, 6, Private->Buffer);\r
   }\r
 \r
   if ((Private != NULL) && (Private->ControllerData != NULL)) {\r
@@ -858,6 +1082,10 @@ Exit:
   }\r
 \r
   if (Private != NULL) {\r
+    if (Private->TimerEvent != NULL) {\r
+      gBS->CloseEvent (Private->TimerEvent);\r
+    }\r
+\r
     FreePool (Private);\r
   }\r
 \r
@@ -921,6 +1149,8 @@ NvmExpressDriverBindingStop (
   UINTN                               Index;\r
   NVME_CONTROLLER_PRIVATE_DATA        *Private;\r
   EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL  *PassThru;\r
+  BOOLEAN                             IsEmpty;\r
+  EFI_TPL                             OldTpl;\r
 \r
   if (NumberOfChildren == 0) {\r
     Status = gBS->OpenProtocol (\r
@@ -934,6 +1164,23 @@ NvmExpressDriverBindingStop (
 \r
     if (!EFI_ERROR (Status)) {\r
       Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (PassThru);\r
+\r
+      //\r
+      // Wait for the asynchronous PassThru queue to become empty.\r
+      //\r
+      while (TRUE) {\r
+        OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+        IsEmpty = IsListEmpty (&Private->AsyncPassThruQueue) &&\r
+                  IsListEmpty (&Private->UnsubmittedSubtasks);\r
+        gBS->RestoreTPL (OldTpl);\r
+\r
+        if (IsEmpty) {\r
+          break;\r
+        }\r
+\r
+        gBS->Stall (100);\r
+      }\r
+\r
       gBS->UninstallMultipleProtocolInterfaces (\r
             Controller,\r
             &gEfiNvmExpressPassThruProtocolGuid,\r
@@ -941,12 +1188,16 @@ NvmExpressDriverBindingStop (
             NULL\r
             );\r
 \r
+      if (Private->TimerEvent != NULL) {\r
+        gBS->CloseEvent (Private->TimerEvent);\r
+      }\r
+\r
       if (Private->Mapping != NULL) {\r
         Private->PciIo->Unmap (Private->PciIo, Private->Mapping);\r
       }\r
 \r
       if (Private->Buffer != NULL) {\r
-        Private->PciIo->FreeBuffer (Private->PciIo, 4, Private->Buffer);\r
+        Private->PciIo->FreeBuffer (Private->PciIo, 6, Private->Buffer);\r
       }\r
 \r
       FreePool (Private->ControllerData);\r
index 6cbef3e380d42448fceba0b0165a70e802f66d69..519772b3c728d1755a93a3d7eb15a5744cbbc582 100644 (file)
@@ -28,6 +28,7 @@
 #include <Protocol/PciIo.h>\r
 #include <Protocol/NvmExpressPassthru.h>\r
 #include <Protocol/BlockIo.h>\r
+#include <Protocol/BlockIo2.h>\r
 #include <Protocol/DiskInfo.h>\r
 #include <Protocol/DriverSupportedEfiVersion.h>\r
 #include <Protocol/StorageSecurityCommand.h>\r
@@ -63,7 +64,18 @@ extern EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL  gNvmExpressDriverSupportedEfiV
 #define NVME_CSQ_SIZE                             1     // Number of I/O submission queue entries, which is 0-based\r
 #define NVME_CCQ_SIZE                             1     // Number of I/O completion queue entries, which is 0-based\r
 \r
-#define NVME_MAX_QUEUES                           2     // Number of queues supported by the driver\r
+//\r
+// Number of asynchronous I/O submission queue entries, which is 0-based.\r
+// The asynchronous I/O submission queue size is 4kB in total.\r
+//\r
+#define NVME_ASYNC_CSQ_SIZE                       63\r
+//\r
+// Number of asynchronous I/O completion queue entries, which is 0-based.\r
+// The asynchronous I/O completion queue size is 4kB in total.\r
+//\r
+#define NVME_ASYNC_CCQ_SIZE                       255\r
+\r
+#define NVME_MAX_QUEUES                           3     // Number of queues supported by the driver\r
 \r
 #define NVME_CONTROLLER_ID                        0\r
 \r
@@ -72,6 +84,11 @@ extern EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL  gNvmExpressDriverSupportedEfiV
 //\r
 #define NVME_GENERIC_TIMEOUT                      EFI_TIMER_PERIOD_SECONDS (5)\r
 \r
+//\r
+// Nvme async transfer timer interval, set by experience.\r
+//\r
+#define NVME_HC_ASYNC_TIMER                       EFI_TIMER_PERIOD_MILLISECONDS (1)\r
+\r
 //\r
 // Unique signature for private data structure.\r
 //\r
@@ -101,11 +118,13 @@ struct _NVME_CONTROLLER_PRIVATE_DATA {
   NVME_ADMIN_CONTROLLER_DATA          *ControllerData;\r
 \r
   //\r
-  // 4 x 4kB aligned buffers will be carved out of this buffer.\r
+  // 6 x 4kB aligned buffers will be carved out of this buffer.\r
   // 1st 4kB boundary is the start of the admin submission queue.\r
   // 2nd 4kB boundary is the start of the admin completion queue.\r
   // 3rd 4kB boundary is the start of I/O submission queue #1.\r
   // 4th 4kB boundary is the start of I/O completion queue #1.\r
+  // 5th 4kB boundary is the start of I/O submission queue #2.\r
+  // 6th 4kB boundary is the start of I/O completion queue #2.\r
   //\r
   UINT8                               *Buffer;\r
   UINT8                               *BufferPciAddr;\r
@@ -123,6 +142,7 @@ struct _NVME_CONTROLLER_PRIVATE_DATA {
   //\r
   NVME_SQTDBL                         SqTdbl[NVME_MAX_QUEUES];\r
   NVME_CQHDBL                         CqHdbl[NVME_MAX_QUEUES];\r
+  UINT16                              AsyncSqHead;\r
 \r
   UINT8                               Pt[NVME_MAX_QUEUES];\r
   UINT16                              Cid[NVME_MAX_QUEUES];\r
@@ -133,6 +153,13 @@ struct _NVME_CONTROLLER_PRIVATE_DATA {
   NVME_CAP                            Cap;\r
 \r
   VOID                                *Mapping;\r
+\r
+  //\r
+  // For Non-blocking operations.\r
+  //\r
+  EFI_EVENT                           TimerEvent;\r
+  LIST_ENTRY                          AsyncPassThruQueue;\r
+  LIST_ENTRY                          UnsubmittedSubtasks;\r
 };\r
 \r
 #define NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU(a) \\r
@@ -166,9 +193,12 @@ struct _NVME_DEVICE_PRIVATE_DATA {
 \r
   EFI_BLOCK_IO_MEDIA                       Media;\r
   EFI_BLOCK_IO_PROTOCOL                    BlockIo;\r
+  EFI_BLOCK_IO2_PROTOCOL                   BlockIo2;\r
   EFI_DISK_INFO_PROTOCOL                   DiskInfo;\r
   EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    StorageSecurity;\r
 \r
+  LIST_ENTRY                               AsyncQueue;\r
+\r
   EFI_LBA                                  NumBlocks;\r
 \r
   CHAR16                                   ModelName[80];\r
@@ -188,6 +218,13 @@ struct _NVME_DEVICE_PRIVATE_DATA {
       NVME_DEVICE_PRIVATE_DATA_SIGNATURE \\r
       )\r
 \r
+#define NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2(a) \\r
+  CR (a, \\r
+      NVME_DEVICE_PRIVATE_DATA, \\r
+      BlockIo2, \\r
+      NVME_DEVICE_PRIVATE_DATA_SIGNATURE \\r
+      )\r
+\r
 #define NVME_DEVICE_PRIVATE_DATA_FROM_DISK_INFO(a) \\r
   CR (a, \\r
       NVME_DEVICE_PRIVATE_DATA, \\r
@@ -202,6 +239,67 @@ struct _NVME_DEVICE_PRIVATE_DATA {
       NVME_DEVICE_PRIVATE_DATA_SIGNATURE                 \\r
       )\r
 \r
+//\r
+// Nvme block I/O 2 request.\r
+//\r
+#define NVME_BLKIO2_REQUEST_SIGNATURE      SIGNATURE_32 ('N', 'B', '2', 'R')\r
+\r
+typedef struct {\r
+  UINT32                                   Signature;\r
+  LIST_ENTRY                               Link;\r
+\r
+  EFI_BLOCK_IO2_TOKEN                      *Token;\r
+  UINTN                                    UnsubmittedSubtaskNum;\r
+  BOOLEAN                                  LastSubtaskSubmitted;\r
+  //\r
+  // The queue for Nvme read/write sub-tasks of a BlockIo2 request.\r
+  //\r
+  LIST_ENTRY                               SubtasksQueue;\r
+} NVME_BLKIO2_REQUEST;\r
+\r
+#define NVME_BLKIO2_REQUEST_FROM_LINK(a) \\r
+  CR (a, NVME_BLKIO2_REQUEST, Link, NVME_BLKIO2_REQUEST_SIGNATURE)\r
+\r
+#define NVME_BLKIO2_SUBTASK_SIGNATURE      SIGNATURE_32 ('N', 'B', '2', 'S')\r
+\r
+typedef struct {\r
+  UINT32                                   Signature;\r
+  LIST_ENTRY                               Link;\r
+\r
+  BOOLEAN                                  IsLast;\r
+  UINT32                                   NamespaceId;\r
+  EFI_EVENT                                Event;\r
+  EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET *CommandPacket;\r
+  //\r
+  // The BlockIo2 request this subtask belongs to\r
+  //\r
+  NVME_BLKIO2_REQUEST                      *BlockIo2Request;\r
+} NVME_BLKIO2_SUBTASK;\r
+\r
+#define NVME_BLKIO2_SUBTASK_FROM_LINK(a) \\r
+  CR (a, NVME_BLKIO2_SUBTASK, Link, NVME_BLKIO2_SUBTASK_SIGNATURE)\r
+\r
+//\r
+// Nvme asynchronous passthru request.\r
+//\r
+#define NVME_PASS_THRU_ASYNC_REQ_SIG       SIGNATURE_32 ('N', 'P', 'A', 'R')\r
+\r
+typedef struct {\r
+  UINT32                                   Signature;\r
+  LIST_ENTRY                               Link;\r
+\r
+  EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET *Packet;\r
+  UINT16                                   CommandId;\r
+  EFI_EVENT                                CallerEvent;\r
+} NVME_PASS_THRU_ASYNC_REQ;\r
+\r
+#define NVME_PASS_THRU_ASYNC_REQ_FROM_THIS(a) \\r
+  CR (a,                                                 \\r
+      NVME_PASS_THRU_ASYNC_REQ,                          \\r
+      Link,                                              \\r
+      NVME_PASS_THRU_ASYNC_REQ_SIG                       \\r
+      )\r
+\r
 /**\r
   Retrieves a Unicode string that is the user readable name of the driver.\r
 \r
@@ -605,4 +703,15 @@ NvmExpressBuildDevicePath (
   IN OUT EFI_DEVICE_PATH_PROTOCOL                    **DevicePath\r
   );\r
 \r
+/**\r
+  Dump the execution status from a given completion queue entry.\r
+\r
+  @param[in]     Cq               A pointer to the NVME_CQ item.\r
+\r
+**/\r
+VOID\r
+NvmeDumpStatus (\r
+  IN NVME_CQ             *Cq\r
+  );\r
+\r
 #endif\r
index 92d7d923df437cb8c6f998a2aa627464deac943f..734e1286c61e501cf39c5d4410e28cef5dea01a5 100644 (file)
@@ -172,6 +172,23 @@ NvmeRead (
   NVME_CONTROLLER_PRIVATE_DATA     *Private;\r
   UINT32                           MaxTransferBlocks;\r
   UINTN                            OrginalBlocks;\r
+  BOOLEAN                          IsEmpty;\r
+  EFI_TPL                          OldTpl;\r
+\r
+  //\r
+  // Wait for the device's asynchronous I/O queue to become empty.\r
+  //\r
+  while (TRUE) {\r
+    OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+    IsEmpty = IsListEmpty (&Device->AsyncQueue);\r
+    gBS->RestoreTPL (OldTpl);\r
+\r
+    if (IsEmpty) {\r
+      break;\r
+    }\r
+\r
+    gBS->Stall (100);\r
+  }\r
 \r
   Status        = EFI_SUCCESS;\r
   Private       = Device->Controller;\r
@@ -233,6 +250,23 @@ NvmeWrite (
   NVME_CONTROLLER_PRIVATE_DATA     *Private;\r
   UINT32                           MaxTransferBlocks;\r
   UINTN                            OrginalBlocks;\r
+  BOOLEAN                          IsEmpty;\r
+  EFI_TPL                          OldTpl;\r
+\r
+  //\r
+  // Wait for the device's asynchronous I/O queue to become empty.\r
+  //\r
+  while (TRUE) {\r
+    OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+    IsEmpty = IsListEmpty (&Device->AsyncQueue);\r
+    gBS->RestoreTPL (OldTpl);\r
+\r
+    if (IsEmpty) {\r
+      break;\r
+    }\r
+\r
+    gBS->Stall (100);\r
+  }\r
 \r
   Status        = EFI_SUCCESS;\r
   Private       = Device->Controller;\r
@@ -313,6 +347,587 @@ NvmeFlush (
   return Status;\r
 }\r
 \r
+/**\r
+  Nonblocking I/O callback funtion when the event is signaled.\r
+\r
+  @param[in]  Event     The Event this notify function registered to.\r
+  @param[in]  Context   Pointer to the context data registered to the\r
+                        Event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+AsyncIoCallback (\r
+  IN EFI_EVENT                Event,\r
+  IN VOID                     *Context\r
+  )\r
+{\r
+  NVME_BLKIO2_SUBTASK         *Subtask;\r
+  NVME_BLKIO2_REQUEST         *Request;\r
+  NVME_CQ                     *Completion;\r
+  EFI_BLOCK_IO2_TOKEN         *Token;\r
+\r
+  gBS->CloseEvent (Event);\r
+\r
+  Subtask    = (NVME_BLKIO2_SUBTASK *) Context;\r
+  Completion = (NVME_CQ *) Subtask->CommandPacket->NvmeCompletion;\r
+  Request    = Subtask->BlockIo2Request;\r
+  Token      = Request->Token;\r
+\r
+  if (Token->TransactionStatus == EFI_SUCCESS) {\r
+    //\r
+    // If previous subtask already fails, do not check the result of\r
+    // subsequent subtasks.\r
+    //\r
+    if ((Completion->Sct != 0) || (Completion->Sc != 0)) {\r
+      Token->TransactionStatus = EFI_DEVICE_ERROR;\r
+\r
+      //\r
+      // Dump completion entry status for debugging.\r
+      //\r
+      DEBUG_CODE_BEGIN();\r
+        NvmeDumpStatus (Completion);\r
+      DEBUG_CODE_END();\r
+    }\r
+  }\r
+\r
+  //\r
+  // Remove the subtask from the BlockIo2 subtasks list.\r
+  //\r
+  RemoveEntryList (&Subtask->Link);\r
+\r
+  if (IsListEmpty (&Request->SubtasksQueue) && Request->LastSubtaskSubmitted) {\r
+    //\r
+    // Remove the BlockIo2 request from the device asynchronous queue.\r
+    //\r
+    RemoveEntryList (&Request->Link);\r
+    FreePool (Request);\r
+    gBS->SignalEvent (Token->Event);\r
+  }\r
+\r
+  FreePool (Subtask->CommandPacket->NvmeCmd);\r
+  FreePool (Subtask->CommandPacket->NvmeCompletion);\r
+  FreePool (Subtask->CommandPacket);\r
+  FreePool (Subtask);\r
+}\r
+\r
+/**\r
+  Read some sectors from the device in an asynchronous manner.\r
+\r
+  @param  Device        The pointer to the NVME_DEVICE_PRIVATE_DATA data\r
+                        structure.\r
+  @param  Request       The pointer to the NVME_BLKIO2_REQUEST data structure.\r
+  @param  Buffer        The buffer used to store the data read from the device.\r
+  @param  Lba           The start block number.\r
+  @param  Blocks        Total block number to be read.\r
+  @param  IsLast        The last subtask of an asynchronous read request.\r
+\r
+  @retval EFI_SUCCESS   Asynchronous read request has been queued.\r
+  @retval Others        Fail to send the asynchronous request.\r
+\r
+**/\r
+EFI_STATUS\r
+AsyncReadSectors (\r
+  IN NVME_DEVICE_PRIVATE_DATA           *Device,\r
+  IN NVME_BLKIO2_REQUEST                *Request,\r
+  IN UINT64                             Buffer,\r
+  IN UINT64                             Lba,\r
+  IN UINT32                             Blocks,\r
+  IN BOOLEAN                            IsLast\r
+  )\r
+{\r
+  NVME_CONTROLLER_PRIVATE_DATA             *Private;\r
+  UINT32                                   Bytes;\r
+  NVME_BLKIO2_SUBTASK                      *Subtask;\r
+  EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET *CommandPacket;\r
+  EFI_NVM_EXPRESS_COMMAND                  *Command;\r
+  EFI_NVM_EXPRESS_COMPLETION               *Completion;\r
+  EFI_STATUS                               Status;\r
+  UINT32                                   BlockSize;\r
+  EFI_TPL                                  OldTpl;\r
+\r
+  Private       = Device->Controller;\r
+  BlockSize     = Device->Media.BlockSize;\r
+  Bytes         = Blocks * BlockSize;\r
+  CommandPacket = NULL;\r
+  Command       = NULL;\r
+  Completion    = NULL;\r
+\r
+  Subtask = AllocateZeroPool (sizeof (NVME_BLKIO2_SUBTASK));\r
+  if (Subtask == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  }\r
+\r
+  Subtask->Signature       = NVME_BLKIO2_SUBTASK_SIGNATURE;\r
+  Subtask->IsLast          = IsLast;\r
+  Subtask->NamespaceId     = Device->NamespaceId;\r
+  Subtask->BlockIo2Request = Request;\r
+\r
+  CommandPacket = AllocateZeroPool (sizeof (EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
+  if (CommandPacket == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  } else {\r
+    Subtask->CommandPacket = CommandPacket;\r
+  }\r
+\r
+  Command = AllocateZeroPool (sizeof (EFI_NVM_EXPRESS_COMMAND));\r
+  if (Command == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  }\r
+\r
+  Completion = AllocateZeroPool (sizeof (EFI_NVM_EXPRESS_COMPLETION));\r
+  if (Completion == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  }\r
+\r
+  //\r
+  // Create Event\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  AsyncIoCallback,\r
+                  Subtask,\r
+                  &Subtask->Event\r
+                  );\r
+  if (EFI_ERROR(Status)) {\r
+    goto ErrorExit;\r
+  }\r
+\r
+  CommandPacket->NvmeCmd        = Command;\r
+  CommandPacket->NvmeCompletion = Completion;\r
+\r
+  CommandPacket->NvmeCmd->Cdw0.Opcode = NVME_IO_READ_OPC;\r
+  CommandPacket->NvmeCmd->Nsid        = Device->NamespaceId;\r
+  CommandPacket->TransferBuffer       = (VOID *)(UINTN)Buffer;\r
+\r
+  CommandPacket->TransferLength = Bytes;\r
+  CommandPacket->CommandTimeout = NVME_GENERIC_TIMEOUT;\r
+  CommandPacket->QueueType      = NVME_IO_QUEUE;\r
+\r
+  CommandPacket->NvmeCmd->Cdw10 = (UINT32)Lba;\r
+  CommandPacket->NvmeCmd->Cdw11 = (UINT32)RShiftU64(Lba, 32);\r
+  CommandPacket->NvmeCmd->Cdw12 = (Blocks - 1) & 0xFFFF;\r
+\r
+  CommandPacket->NvmeCmd->Flags = CDW10_VALID | CDW11_VALID | CDW12_VALID;\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+  InsertTailList (&Private->UnsubmittedSubtasks, &Subtask->Link);\r
+  Request->UnsubmittedSubtaskNum++;\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return EFI_SUCCESS;\r
+\r
+ErrorExit:\r
+  //\r
+  // Resource cleanup if asynchronous read request has not been queued.\r
+  //\r
+  if (Completion != NULL) {\r
+    FreePool (Completion);\r
+  }\r
+\r
+  if (Command != NULL) {\r
+    FreePool (Command);\r
+  }\r
+\r
+  if (CommandPacket != NULL) {\r
+    FreePool (CommandPacket);\r
+  }\r
+\r
+  if (Subtask != NULL) {\r
+    if (Subtask->Event != NULL) {\r
+      gBS->CloseEvent (Subtask->Event);\r
+    }\r
+\r
+    FreePool (Subtask);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Write some sectors from the device in an asynchronous manner.\r
+\r
+  @param  Device        The pointer to the NVME_DEVICE_PRIVATE_DATA data\r
+                        structure.\r
+  @param  Request       The pointer to the NVME_BLKIO2_REQUEST data structure.\r
+  @param  Buffer        The buffer used to store the data written to the\r
+                        device.\r
+  @param  Lba           The start block number.\r
+  @param  Blocks        Total block number to be written.\r
+  @param  IsLast        The last subtask of an asynchronous write request.\r
+\r
+  @retval EFI_SUCCESS   Asynchronous write request has been queued.\r
+  @retval Others        Fail to send the asynchronous request.\r
+\r
+**/\r
+EFI_STATUS\r
+AsyncWriteSectors (\r
+  IN NVME_DEVICE_PRIVATE_DATA           *Device,\r
+  IN NVME_BLKIO2_REQUEST                *Request,\r
+  IN UINT64                             Buffer,\r
+  IN UINT64                             Lba,\r
+  IN UINT32                             Blocks,\r
+  IN BOOLEAN                            IsLast\r
+  )\r
+{\r
+  NVME_CONTROLLER_PRIVATE_DATA             *Private;\r
+  UINT32                                   Bytes;\r
+  NVME_BLKIO2_SUBTASK                      *Subtask;\r
+  EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET *CommandPacket;\r
+  EFI_NVM_EXPRESS_COMMAND                  *Command;\r
+  EFI_NVM_EXPRESS_COMPLETION               *Completion;\r
+  EFI_STATUS                               Status;\r
+  UINT32                                   BlockSize;\r
+  EFI_TPL                                  OldTpl;\r
+\r
+  Private       = Device->Controller;\r
+  BlockSize     = Device->Media.BlockSize;\r
+  Bytes         = Blocks * BlockSize;\r
+  CommandPacket = NULL;\r
+  Command       = NULL;\r
+  Completion    = NULL;\r
+\r
+  Subtask = AllocateZeroPool (sizeof (NVME_BLKIO2_SUBTASK));\r
+  if (Subtask == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  }\r
+\r
+  Subtask->Signature       = NVME_BLKIO2_SUBTASK_SIGNATURE;\r
+  Subtask->IsLast          = IsLast;\r
+  Subtask->NamespaceId     = Device->NamespaceId;\r
+  Subtask->BlockIo2Request = Request;\r
+\r
+  CommandPacket = AllocateZeroPool (sizeof (EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
+  if (CommandPacket == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  } else {\r
+    Subtask->CommandPacket = CommandPacket;\r
+  }\r
+\r
+  Command = AllocateZeroPool (sizeof (EFI_NVM_EXPRESS_COMMAND));\r
+  if (Command == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  }\r
+\r
+  Completion = AllocateZeroPool (sizeof (EFI_NVM_EXPRESS_COMPLETION));\r
+  if (Completion == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ErrorExit;\r
+  }\r
+\r
+  //\r
+  // Create Event\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  AsyncIoCallback,\r
+                  Subtask,\r
+                  &Subtask->Event\r
+                  );\r
+  if (EFI_ERROR(Status)) {\r
+    goto ErrorExit;\r
+  }\r
+\r
+  CommandPacket->NvmeCmd        = Command;\r
+  CommandPacket->NvmeCompletion = Completion;\r
+\r
+  CommandPacket->NvmeCmd->Cdw0.Opcode = NVME_IO_WRITE_OPC;\r
+  CommandPacket->NvmeCmd->Nsid        = Device->NamespaceId;\r
+  CommandPacket->TransferBuffer       = (VOID *)(UINTN)Buffer;\r
+\r
+  CommandPacket->TransferLength = Bytes;\r
+  CommandPacket->CommandTimeout = NVME_GENERIC_TIMEOUT;\r
+  CommandPacket->QueueType      = NVME_IO_QUEUE;\r
+\r
+  CommandPacket->NvmeCmd->Cdw10 = (UINT32)Lba;\r
+  CommandPacket->NvmeCmd->Cdw11 = (UINT32)RShiftU64(Lba, 32);\r
+  //\r
+  // Set Force Unit Access bit (bit 30) to use write-through behaviour\r
+  //\r
+  CommandPacket->NvmeCmd->Cdw12 = ((Blocks - 1) & 0xFFFF) | BIT30;\r
+\r
+  CommandPacket->NvmeCmd->Flags = CDW10_VALID | CDW11_VALID | CDW12_VALID;\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+  InsertTailList (&Private->UnsubmittedSubtasks, &Subtask->Link);\r
+  Request->UnsubmittedSubtaskNum++;\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return EFI_SUCCESS;\r
+\r
+ErrorExit:\r
+  //\r
+  // Resource cleanup if asynchronous read request has not been queued.\r
+  //\r
+  if (Completion != NULL) {\r
+    FreePool (Completion);\r
+  }\r
+\r
+  if (Command != NULL) {\r
+    FreePool (Command);\r
+  }\r
+\r
+  if (CommandPacket != NULL) {\r
+    FreePool (CommandPacket);\r
+  }\r
+\r
+  if (Subtask != NULL) {\r
+    if (Subtask->Event != NULL) {\r
+      gBS->CloseEvent (Subtask->Event);\r
+    }\r
+\r
+    FreePool (Subtask);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Read some blocks from the device in an asynchronous manner.\r
+\r
+  @param  Device        The pointer to the NVME_DEVICE_PRIVATE_DATA data\r
+                        structure.\r
+  @param  Buffer        The buffer used to store the data read from the device.\r
+  @param  Lba           The start block number.\r
+  @param  Blocks        Total block number to be read.\r
+  @param  Token         A pointer to the token associated with the transaction.\r
+\r
+  @retval EFI_SUCCESS   Data are read from the device.\r
+  @retval Others        Fail to read all the data.\r
+\r
+**/\r
+EFI_STATUS\r
+NvmeAsyncRead (\r
+  IN     NVME_DEVICE_PRIVATE_DATA       *Device,\r
+     OUT VOID                           *Buffer,\r
+  IN     UINT64                         Lba,\r
+  IN     UINTN                          Blocks,\r
+  IN     EFI_BLOCK_IO2_TOKEN            *Token\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  UINT32                           BlockSize;\r
+  NVME_CONTROLLER_PRIVATE_DATA     *Private;\r
+  NVME_BLKIO2_REQUEST              *BlkIo2Req;\r
+  UINT32                           MaxTransferBlocks;\r
+  UINTN                            OrginalBlocks;\r
+  BOOLEAN                          IsEmpty;\r
+  EFI_TPL                          OldTpl;\r
+\r
+  Status        = EFI_SUCCESS;\r
+  Private       = Device->Controller;\r
+  BlockSize     = Device->Media.BlockSize;\r
+  OrginalBlocks = Blocks;\r
+  BlkIo2Req     = AllocateZeroPool (sizeof (NVME_BLKIO2_REQUEST));\r
+  if (BlkIo2Req == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  BlkIo2Req->Signature = NVME_BLKIO2_REQUEST_SIGNATURE;\r
+  BlkIo2Req->Token     = Token;\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+  InsertTailList (&Device->AsyncQueue, &BlkIo2Req->Link);\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  InitializeListHead (&BlkIo2Req->SubtasksQueue);\r
+\r
+  if (Private->ControllerData->Mdts != 0) {\r
+    MaxTransferBlocks = (1 << (Private->ControllerData->Mdts)) * (1 << (Private->Cap.Mpsmin + 12)) / BlockSize;\r
+  } else {\r
+    MaxTransferBlocks = 1024;\r
+  }\r
+\r
+  while (Blocks > 0) {\r
+    if (Blocks > MaxTransferBlocks) {\r
+      Status = AsyncReadSectors (\r
+                 Device,\r
+                 BlkIo2Req, (UINT64)(UINTN)Buffer,\r
+                 Lba,\r
+                 MaxTransferBlocks,\r
+                 FALSE\r
+                 );\r
+\r
+      Blocks -= MaxTransferBlocks;\r
+      Buffer  = (VOID *)(UINTN)((UINT64)(UINTN)Buffer + MaxTransferBlocks * BlockSize);\r
+      Lba    += MaxTransferBlocks;\r
+    } else {\r
+      Status = AsyncReadSectors (\r
+                 Device,\r
+                 BlkIo2Req,\r
+                 (UINT64)(UINTN)Buffer,\r
+                 Lba,\r
+                 (UINT32)Blocks,\r
+                 TRUE\r
+                 );\r
+\r
+      Blocks = 0;\r
+    }\r
+\r
+    if (EFI_ERROR(Status)) {\r
+      OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+      IsEmpty = IsListEmpty (&BlkIo2Req->SubtasksQueue) &&\r
+                (BlkIo2Req->UnsubmittedSubtaskNum == 0);\r
+\r
+      if (IsEmpty) {\r
+        //\r
+        // Remove the BlockIo2 request from the device asynchronous queue.\r
+        //\r
+        RemoveEntryList (&BlkIo2Req->Link);\r
+        FreePool (BlkIo2Req);\r
+        Status = EFI_DEVICE_ERROR;\r
+      } else {\r
+        //\r
+        // There are previous BlockIo2 subtasks still running, EFI_SUCCESS\r
+        // should be returned to make sure that the caller does not free\r
+        // resources still using by these requests.\r
+        //\r
+        Status = EFI_SUCCESS;\r
+        Token->TransactionStatus = EFI_DEVICE_ERROR;\r
+        BlkIo2Req->LastSubtaskSubmitted = TRUE;\r
+      }\r
+\r
+      gBS->RestoreTPL (OldTpl);\r
+\r
+      break;\r
+    }\r
+  }\r
+\r
+  DEBUG ((EFI_D_VERBOSE, "%a: Lba = 0x%08Lx, Original = 0x%08Lx, "\r
+    "Remaining = 0x%08Lx, BlockSize = 0x%x, Status = %r\n", __FUNCTION__, Lba,\r
+    (UINT64)OrginalBlocks, (UINT64)Blocks, BlockSize, Status));\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Write some blocks from the device in an asynchronous manner.\r
+\r
+  @param  Device        The pointer to the NVME_DEVICE_PRIVATE_DATA data\r
+                        structure.\r
+  @param  Buffer        The buffer used to store the data written to the\r
+                        device.\r
+  @param  Lba           The start block number.\r
+  @param  Blocks        Total block number to be written.\r
+  @param  Token         A pointer to the token associated with the transaction.\r
+\r
+  @retval EFI_SUCCESS   Data are written to the device.\r
+  @retval Others        Fail to write all the data.\r
+\r
+**/\r
+EFI_STATUS\r
+NvmeAsyncWrite (\r
+  IN NVME_DEVICE_PRIVATE_DATA           *Device,\r
+  IN VOID                               *Buffer,\r
+  IN UINT64                             Lba,\r
+  IN UINTN                              Blocks,\r
+  IN EFI_BLOCK_IO2_TOKEN                *Token\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  UINT32                           BlockSize;\r
+  NVME_CONTROLLER_PRIVATE_DATA     *Private;\r
+  NVME_BLKIO2_REQUEST              *BlkIo2Req;\r
+  UINT32                           MaxTransferBlocks;\r
+  UINTN                            OrginalBlocks;\r
+  BOOLEAN                          IsEmpty;\r
+  EFI_TPL                          OldTpl;\r
+\r
+  Status        = EFI_SUCCESS;\r
+  Private       = Device->Controller;\r
+  BlockSize     = Device->Media.BlockSize;\r
+  OrginalBlocks = Blocks;\r
+  BlkIo2Req     = AllocateZeroPool (sizeof (NVME_BLKIO2_REQUEST));\r
+  if (BlkIo2Req == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  BlkIo2Req->Signature = NVME_BLKIO2_REQUEST_SIGNATURE;\r
+  BlkIo2Req->Token     = Token;\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+  InsertTailList (&Device->AsyncQueue, &BlkIo2Req->Link);\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  InitializeListHead (&BlkIo2Req->SubtasksQueue);\r
+\r
+  if (Private->ControllerData->Mdts != 0) {\r
+    MaxTransferBlocks = (1 << (Private->ControllerData->Mdts)) * (1 << (Private->Cap.Mpsmin + 12)) / BlockSize;\r
+  } else {\r
+    MaxTransferBlocks = 1024;\r
+  }\r
+\r
+  while (Blocks > 0) {\r
+    if (Blocks > MaxTransferBlocks) {\r
+      Status  = AsyncWriteSectors (\r
+                  Device,\r
+                  BlkIo2Req,\r
+                  (UINT64)(UINTN)Buffer,\r
+                  Lba,\r
+                  MaxTransferBlocks,\r
+                  FALSE\r
+                  );\r
+\r
+      Blocks -= MaxTransferBlocks;\r
+      Buffer  = (VOID *)(UINTN)((UINT64)(UINTN)Buffer + MaxTransferBlocks * BlockSize);\r
+      Lba    += MaxTransferBlocks;\r
+    } else {\r
+      Status = AsyncWriteSectors (\r
+                 Device,\r
+                 BlkIo2Req,\r
+                 (UINT64)(UINTN)Buffer,\r
+                 Lba,\r
+                 (UINT32)Blocks,\r
+                 TRUE\r
+                 );\r
+\r
+      Blocks = 0;\r
+    }\r
+\r
+    if (EFI_ERROR(Status)) {\r
+      OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+      IsEmpty = IsListEmpty (&BlkIo2Req->SubtasksQueue) &&\r
+                (BlkIo2Req->UnsubmittedSubtaskNum == 0);\r
+\r
+      if (IsEmpty) {\r
+        //\r
+        // Remove the BlockIo2 request from the device asynchronous queue.\r
+        //\r
+        RemoveEntryList (&BlkIo2Req->Link);\r
+        FreePool (BlkIo2Req);\r
+        Status = EFI_DEVICE_ERROR;\r
+      } else {\r
+        //\r
+        // There are previous BlockIo2 subtasks still running, EFI_SUCCESS\r
+        // should be returned to make sure that the caller does not free\r
+        // resources still using by these requests.\r
+        //\r
+        Status = EFI_SUCCESS;\r
+        Token->TransactionStatus = EFI_DEVICE_ERROR;\r
+        BlkIo2Req->LastSubtaskSubmitted = TRUE;\r
+      }\r
+\r
+      gBS->RestoreTPL (OldTpl);\r
+\r
+      break;\r
+    }\r
+  }\r
+\r
+  DEBUG ((EFI_D_VERBOSE, "%a: Lba = 0x%08Lx, Original = 0x%08Lx, "\r
+    "Remaining = 0x%08Lx, BlockSize = 0x%x, Status = %r\n", __FUNCTION__, Lba,\r
+    (UINT64)OrginalBlocks, (UINT64)Blocks, BlockSize, Status));\r
+\r
+  return Status;\r
+}\r
 \r
 /**\r
   Reset the Block Device.\r
@@ -567,6 +1182,361 @@ NvmeBlockIoFlushBlocks (
   return Status;\r
 }\r
 \r
+/**\r
+  Reset the block device hardware.\r
+\r
+  @param[in]  This                 Indicates a pointer to the calling context.\r
+  @param[in]  ExtendedVerification Indicates that the driver may perform a more\r
+                                   exhausive verfication operation of the\r
+                                   device during reset.\r
+\r
+  @retval EFI_SUCCESS          The device was reset.\r
+  @retval EFI_DEVICE_ERROR     The device is not functioning properly and could\r
+                               not be reset.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoResetEx (\r
+  IN EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN BOOLEAN                 ExtendedVerification\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  NVME_DEVICE_PRIVATE_DATA        *Device;\r
+  NVME_CONTROLLER_PRIVATE_DATA    *Private;\r
+  BOOLEAN                         IsEmpty;\r
+  EFI_TPL                         OldTpl;\r
+\r
+  if (This == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Device  = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);\r
+  Private = Device->Controller;\r
+\r
+  //\r
+  // Wait for the asynchronous PassThru queue to become empty.\r
+  //\r
+  while (TRUE) {\r
+    OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+    IsEmpty = IsListEmpty (&Private->AsyncPassThruQueue) &&\r
+              IsListEmpty (&Private->UnsubmittedSubtasks);\r
+    gBS->RestoreTPL (OldTpl);\r
+\r
+    if (IsEmpty) {\r
+      break;\r
+    }\r
+\r
+    gBS->Stall (100);\r
+  }\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  Status  = NvmeControllerInit (Private);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Read BufferSize bytes from Lba into Buffer.\r
+\r
+  This function reads the requested number of blocks from the device. All the\r
+  blocks are read, or an error is returned.\r
+  If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and\r
+  non-blocking I/O is being used, the Event associated with this request will\r
+  not be signaled.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    Id of the media, changes every time the media is\r
+                              replaced.\r
+  @param[in]       Lba        The starting Logical Block Address to read from.\r
+  @param[in, out]  Token      A pointer to the token associated with the\r
+                              transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device\r
+                              block size.\r
+  @param[out]      Buffer     A pointer to the destination buffer for the data.\r
+                              The caller is responsible for either having\r
+                              implicit or explicit ownership of the buffer.\r
+\r
+  @retval EFI_SUCCESS           The read request was queued if Token->Event is\r
+                                not NULL.The data was read correctly from the\r
+                                device if the Token->Event is NULL.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing\r
+                                the read.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED     The MediaId is not for the current media.\r
+  @retval EFI_BAD_BUFFER_SIZE   The BufferSize parameter is not a multiple of\r
+                                the intrinsic block size of the device.\r
+  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not\r
+                                valid, or the buffer is not on proper\r
+                                alignment.\r
+  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a\r
+                                lack of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoReadBlocksEx (\r
+  IN     EFI_BLOCK_IO2_PROTOCOL *This,\r
+  IN     UINT32                 MediaId,\r
+  IN     EFI_LBA                Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN    *Token,\r
+  IN     UINTN                  BufferSize,\r
+     OUT VOID                  *Buffer\r
+  )\r
+{\r
+  NVME_DEVICE_PRIVATE_DATA        *Device;\r
+  EFI_STATUS                      Status;\r
+  EFI_BLOCK_IO_MEDIA              *Media;\r
+  UINTN                           BlockSize;\r
+  UINTN                           NumberOfBlocks;\r
+  UINTN                           IoAlign;\r
+  EFI_TPL                         OldTpl;\r
+\r
+  //\r
+  // Check parameters.\r
+  //\r
+  if (This == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Media = This->Media;\r
+\r
+  if (MediaId != Media->MediaId) {\r
+    return EFI_MEDIA_CHANGED;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (BufferSize == 0) {\r
+    if ((Token != NULL) && (Token->Event != NULL)) {\r
+      Token->TransactionStatus = EFI_SUCCESS;\r
+      gBS->SignalEvent (Token->Event);\r
+    }\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  BlockSize = Media->BlockSize;\r
+  if ((BufferSize % BlockSize) != 0) {\r
+    return EFI_BAD_BUFFER_SIZE;\r
+  }\r
+\r
+  NumberOfBlocks  = BufferSize / BlockSize;\r
+  if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  IoAlign = Media->IoAlign;\r
+  if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);\r
+\r
+  if ((Token != NULL) && (Token->Event != NULL)) {\r
+    Token->TransactionStatus = EFI_SUCCESS;\r
+    Status = NvmeAsyncRead (Device, Buffer, Lba, NumberOfBlocks, Token);\r
+  } else {\r
+    Status = NvmeRead (Device, Buffer, Lba, NumberOfBlocks);\r
+  }\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Write BufferSize bytes from Lba into Buffer.\r
+\r
+  This function writes the requested number of blocks to the device. All blocks\r
+  are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,\r
+  EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is\r
+  being used, the Event associated with this request will not be signaled.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    The media ID that the write request is for.\r
+  @param[in]       Lba        The starting logical block address to be written.\r
+                              The caller is responsible for writing to only\r
+                              legitimate locations.\r
+  @param[in, out]  Token      A pointer to the token associated with the\r
+                              transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device\r
+                              block size.\r
+  @param[in]       Buffer     A pointer to the source buffer for the data.\r
+\r
+  @retval EFI_SUCCESS           The write request was queued if Event is not\r
+                                NULL.\r
+                                The data was written correctly to the device if\r
+                                the Event is NULL.\r
+  @retval EFI_WRITE_PROTECTED   The device can not be written to.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current\r
+                                device.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing\r
+                                the write.\r
+  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size\r
+                                of the device.\r
+  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not\r
+                                valid, or the buffer is not on proper\r
+                                alignment.\r
+  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a\r
+                                lack of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoWriteBlocksEx (\r
+  IN     EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN     UINT32                 MediaId,\r
+  IN     EFI_LBA                Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN    *Token,\r
+  IN     UINTN                  BufferSize,\r
+  IN     VOID                   *Buffer\r
+  )\r
+{\r
+  NVME_DEVICE_PRIVATE_DATA        *Device;\r
+  EFI_STATUS                      Status;\r
+  EFI_BLOCK_IO_MEDIA              *Media;\r
+  UINTN                           BlockSize;\r
+  UINTN                           NumberOfBlocks;\r
+  UINTN                           IoAlign;\r
+  EFI_TPL                         OldTpl;\r
+\r
+  //\r
+  // Check parameters.\r
+  //\r
+  if (This == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Media = This->Media;\r
+\r
+  if (MediaId != Media->MediaId) {\r
+    return EFI_MEDIA_CHANGED;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (BufferSize == 0) {\r
+    if ((Token != NULL) && (Token->Event != NULL)) {\r
+      Token->TransactionStatus = EFI_SUCCESS;\r
+      gBS->SignalEvent (Token->Event);\r
+    }\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  BlockSize = Media->BlockSize;\r
+  if ((BufferSize % BlockSize) != 0) {\r
+    return EFI_BAD_BUFFER_SIZE;\r
+  }\r
+\r
+  NumberOfBlocks  = BufferSize / BlockSize;\r
+  if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  IoAlign = Media->IoAlign;\r
+  if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);\r
+\r
+  if ((Token != NULL) && (Token->Event != NULL)) {\r
+    Token->TransactionStatus = EFI_SUCCESS;\r
+    Status = NvmeAsyncWrite (Device, Buffer, Lba, NumberOfBlocks, Token);\r
+  } else {\r
+    Status = NvmeWrite (Device, Buffer, Lba, NumberOfBlocks);\r
+  }\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Flush the Block Device.\r
+\r
+  If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED\r
+  is returned and non-blocking I/O is being used, the Event associated with\r
+  this request will not be signaled.\r
+\r
+  @param[in]      This     Indicates a pointer to the calling context.\r
+  @param[in,out]  Token    A pointer to the token associated with the\r
+                           transaction.\r
+\r
+  @retval EFI_SUCCESS          The flush request was queued if Event is not\r
+                               NULL.\r
+                               All outstanding data was written correctly to\r
+                               the device if the Event is NULL.\r
+  @retval EFI_DEVICE_ERROR     The device reported an error while writting back\r
+                               the data.\r
+  @retval EFI_WRITE_PROTECTED  The device cannot be written to.\r
+  @retval EFI_NO_MEDIA         There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED    The MediaId is not for the current media.\r
+  @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
+                               of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoFlushBlocksEx (\r
+  IN     EFI_BLOCK_IO2_PROTOCOL   *This,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN      *Token\r
+  )\r
+{\r
+  NVME_DEVICE_PRIVATE_DATA        *Device;\r
+  BOOLEAN                         IsEmpty;\r
+  EFI_TPL                         OldTpl;\r
+\r
+  //\r
+  // Check parameters.\r
+  //\r
+  if (This == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);\r
+\r
+  //\r
+  // Wait for the asynchronous I/O queue to become empty.\r
+  //\r
+  while (TRUE) {\r
+    OldTpl  = gBS->RaiseTPL (TPL_NOTIFY);\r
+    IsEmpty = IsListEmpty (&Device->AsyncQueue);\r
+    gBS->RestoreTPL (OldTpl);\r
+\r
+    if (IsEmpty) {\r
+      break;\r
+    }\r
+\r
+    gBS->Stall (100);\r
+  }\r
+\r
+  //\r
+  // Signal caller event\r
+  //\r
+  if ((Token != NULL) && (Token->Event != NULL)) {\r
+    Token->TransactionStatus = EFI_SUCCESS;\r
+    gBS->SignalEvent (Token->Event);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   Trust transfer data from/to NVMe device.\r
 \r
@@ -619,7 +1589,7 @@ TrustTransferNvmeDevice (
   CommandPacket.NvmeCmd        = &Command;\r
   CommandPacket.NvmeCompletion = &Completion;\r
 \r
-  // \r
+  //\r
   // Change Endianness of SecurityProtocolSpecificData\r
   //\r
   SpecificData = (((SecurityProtocolSpecificData << 8) & 0xFF00) | (SecurityProtocolSpecificData >> 8));\r
@@ -748,7 +1718,7 @@ NvmeStorageSecurityReceiveData (
 {\r
   EFI_STATUS                       Status;\r
   NVME_DEVICE_PRIVATE_DATA         *Device;\r
-  \r
+\r
   Status  = EFI_SUCCESS;\r
 \r
   if ((PayloadBuffer == NULL) || (PayloadTransferSize == NULL) || (PayloadBufferSize == 0)) {\r
@@ -852,7 +1822,7 @@ NvmeStorageSecuritySendData (
   IN VOID                                     *PayloadBuffer\r
   )\r
 {\r
-  EFI_STATUS                       Status; \r
+  EFI_STATUS                       Status;\r
   NVME_DEVICE_PRIVATE_DATA         *Device;\r
 \r
   Status  = EFI_SUCCESS;\r
index 1c71a81ec5ac7bccb35fe6c34f77965c47b99cd5..9199f284d84fcfe5a9f642688593ff63cbb2d576 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Header file for EFI_BLOCK_IO_PROTOCOL interface.\r
 \r
-Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -108,6 +108,154 @@ NvmeBlockIoFlushBlocks (
   IN  EFI_BLOCK_IO_PROTOCOL   *This\r
   );\r
 \r
+/**\r
+  Reset the block device hardware.\r
+\r
+  @param[in]  This                 Indicates a pointer to the calling context.\r
+  @param[in]  ExtendedVerification Indicates that the driver may perform a more\r
+                                   exhausive verfication operation of the\r
+                                   device during reset.\r
+\r
+  @retval EFI_SUCCESS          The device was reset.\r
+  @retval EFI_DEVICE_ERROR     The device is not functioning properly and could\r
+                               not be reset.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoResetEx (\r
+  IN EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN BOOLEAN                 ExtendedVerification\r
+  );\r
+\r
+/**\r
+  Read BufferSize bytes from Lba into Buffer.\r
+\r
+  This function reads the requested number of blocks from the device. All the\r
+  blocks are read, or an error is returned.\r
+  If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and\r
+  non-blocking I/O is being used, the Event associated with this request will\r
+  not be signaled.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    Id of the media, changes every time the media is\r
+                              replaced.\r
+  @param[in]       Lba        The starting Logical Block Address to read from.\r
+  @param[in, out]  Token      A pointer to the token associated with the\r
+                              transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device\r
+                              block size.\r
+  @param[out]      Buffer     A pointer to the destination buffer for the data.\r
+                              The caller is responsible for either having\r
+                              implicit or explicit ownership of the buffer.\r
+\r
+  @retval EFI_SUCCESS           The read request was queued if Token->Event is\r
+                                not NULL.The data was read correctly from the\r
+                                device if the Token->Event is NULL.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing\r
+                                the read.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED     The MediaId is not for the current media.\r
+  @retval EFI_BAD_BUFFER_SIZE   The BufferSize parameter is not a multiple of\r
+                                the intrinsic block size of the device.\r
+  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not\r
+                                valid, or the buffer is not on proper\r
+                                alignment.\r
+  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a\r
+                                lack of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoReadBlocksEx (\r
+  IN     EFI_BLOCK_IO2_PROTOCOL *This,\r
+  IN     UINT32                 MediaId,\r
+  IN     EFI_LBA                Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN    *Token,\r
+  IN     UINTN                  BufferSize,\r
+     OUT VOID                  *Buffer\r
+  );\r
+\r
+/**\r
+  Write BufferSize bytes from Lba into Buffer.\r
+\r
+  This function writes the requested number of blocks to the device. All blocks\r
+  are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,\r
+  EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is\r
+  being used, the Event associated with this request will not be signaled.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    The media ID that the write request is for.\r
+  @param[in]       Lba        The starting logical block address to be written.\r
+                              The caller is responsible for writing to only\r
+                              legitimate locations.\r
+  @param[in, out]  Token      A pointer to the token associated with the\r
+                              transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device\r
+                              block size.\r
+  @param[in]       Buffer     A pointer to the source buffer for the data.\r
+\r
+  @retval EFI_SUCCESS           The write request was queued if Event is not\r
+                                NULL.\r
+                                The data was written correctly to the device if\r
+                                the Event is NULL.\r
+  @retval EFI_WRITE_PROTECTED   The device can not be written to.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current\r
+                                device.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing\r
+                                the write.\r
+  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size\r
+                                of the device.\r
+  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not\r
+                                valid, or the buffer is not on proper\r
+                                alignment.\r
+  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a\r
+                                lack of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoWriteBlocksEx (\r
+  IN     EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN     UINT32                 MediaId,\r
+  IN     EFI_LBA                Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN    *Token,\r
+  IN     UINTN                  BufferSize,\r
+  IN     VOID                   *Buffer\r
+  );\r
+\r
+/**\r
+  Flush the Block Device.\r
+\r
+  If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED\r
+  is returned and non-blocking I/O is being used, the Event associated with\r
+  this request will not be signaled.\r
+\r
+  @param[in]      This     Indicates a pointer to the calling context.\r
+  @param[in,out]  Token    A pointer to the token associated with the\r
+                           transaction.\r
+\r
+  @retval EFI_SUCCESS          The flush request was queued if Event is not\r
+                               NULL.\r
+                               All outstanding data was written correctly to\r
+                               the device if the Event is NULL.\r
+  @retval EFI_DEVICE_ERROR     The device reported an error while writting back\r
+                               the data.\r
+  @retval EFI_WRITE_PROTECTED  The device cannot be written to.\r
+  @retval EFI_NO_MEDIA         There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED    The MediaId is not for the current media.\r
+  @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
+                               of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NvmeBlockIoFlushBlocksEx (\r
+  IN     EFI_BLOCK_IO2_PROTOCOL   *This,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN      *Token\r
+  );\r
+\r
 /**\r
   Send a security protocol command to a device that receives data and/or the result\r
   of one or more commands sent by SendData.\r
index 00acf2b5cf9c539d40f20731da4d10c20da207ec..3270042e026e38a0e8c096c978168c6a6c0a2d26 100644 (file)
@@ -4,7 +4,7 @@
 #  NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows\r
 #  NVM Express specification.\r
 #\r
-#  Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #  This program and the accompanying materials\r
 #  are licensed and made available under the terms and conditions of the BSD License\r
@@ -67,6 +67,7 @@
   gEfiDevicePathProtocolGuid\r
   gEfiNvmExpressPassThruProtocolGuid          ## BY_START\r
   gEfiBlockIoProtocolGuid                     ## BY_START\r
+  gEfiBlockIo2ProtocolGuid                    ## BY_START\r
   gEfiDiskInfoProtocolGuid                    ## BY_START\r
   gEfiStorageSecurityCommandProtocolGuid      ## BY_START\r
   gEfiDriverSupportedEfiVersionProtocolGuid   ## PRODUCES\r
index e2201b9cef3b08fe4a5aff69704769f6af9c8bf4..dcfe1e865de569395434a4ec82ce17939b7d536e 100644 (file)
@@ -682,33 +682,39 @@ NvmeCreateIoCompletionQueue (
   EFI_NVM_EXPRESS_COMPLETION               Completion;\r
   EFI_STATUS                               Status;\r
   NVME_ADMIN_CRIOCQ                        CrIoCq;\r
-\r
-  ZeroMem (&CommandPacket, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
-  ZeroMem (&Command, sizeof(EFI_NVM_EXPRESS_COMMAND));\r
-  ZeroMem (&Completion, sizeof(EFI_NVM_EXPRESS_COMPLETION));\r
-  ZeroMem (&CrIoCq, sizeof(NVME_ADMIN_CRIOCQ));\r
-\r
-  CommandPacket.NvmeCmd        = &Command;\r
-  CommandPacket.NvmeCompletion = &Completion;\r
-\r
-  Command.Cdw0.Opcode = NVME_ADMIN_CRIOCQ_CMD;\r
-  CommandPacket.TransferBuffer = Private->CqBufferPciAddr[1];\r
-  CommandPacket.TransferLength = EFI_PAGE_SIZE;\r
-  CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;\r
-  CommandPacket.QueueType      = NVME_ADMIN_QUEUE;\r
-\r
-  CrIoCq.Qid   = NVME_IO_QUEUE;\r
-  CrIoCq.Qsize = NVME_CCQ_SIZE;\r
-  CrIoCq.Pc    = 1;\r
-  CopyMem (&CommandPacket.NvmeCmd->Cdw10, &CrIoCq, sizeof (NVME_ADMIN_CRIOCQ));\r
-  CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID;\r
-\r
-  Status = Private->Passthru.PassThru (\r
-                               &Private->Passthru,\r
-                               0,\r
-                               &CommandPacket,\r
-                               NULL\r
-                               );\r
+  UINT32                                   Index;\r
+\r
+  for (Index = 1; Index < NVME_MAX_QUEUES; Index++) {\r
+    ZeroMem (&CommandPacket, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
+    ZeroMem (&Command, sizeof(EFI_NVM_EXPRESS_COMMAND));\r
+    ZeroMem (&Completion, sizeof(EFI_NVM_EXPRESS_COMPLETION));\r
+    ZeroMem (&CrIoCq, sizeof(NVME_ADMIN_CRIOCQ));\r
+\r
+    CommandPacket.NvmeCmd        = &Command;\r
+    CommandPacket.NvmeCompletion = &Completion;\r
+\r
+    Command.Cdw0.Opcode = NVME_ADMIN_CRIOCQ_CMD;\r
+    CommandPacket.TransferBuffer = Private->CqBufferPciAddr[Index];\r
+    CommandPacket.TransferLength = EFI_PAGE_SIZE;\r
+    CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;\r
+    CommandPacket.QueueType      = NVME_ADMIN_QUEUE;\r
+\r
+    CrIoCq.Qid   = Index;\r
+    CrIoCq.Qsize = (Index == 1) ? NVME_CCQ_SIZE : NVME_ASYNC_CCQ_SIZE;\r
+    CrIoCq.Pc    = 1;\r
+    CopyMem (&CommandPacket.NvmeCmd->Cdw10, &CrIoCq, sizeof (NVME_ADMIN_CRIOCQ));\r
+    CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID;\r
+\r
+    Status = Private->Passthru.PassThru (\r
+                                 &Private->Passthru,\r
+                                 0,\r
+                                 &CommandPacket,\r
+                                 NULL\r
+                                 );\r
+    if (EFI_ERROR (Status)) {\r
+      break;\r
+    }\r
+  }\r
 \r
   return Status;\r
 }\r
@@ -732,35 +738,41 @@ NvmeCreateIoSubmissionQueue (
   EFI_NVM_EXPRESS_COMPLETION               Completion;\r
   EFI_STATUS                               Status;\r
   NVME_ADMIN_CRIOSQ                        CrIoSq;\r
-\r
-  ZeroMem (&CommandPacket, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
-  ZeroMem (&Command, sizeof(EFI_NVM_EXPRESS_COMMAND));\r
-  ZeroMem (&Completion, sizeof(EFI_NVM_EXPRESS_COMPLETION));\r
-  ZeroMem (&CrIoSq, sizeof(NVME_ADMIN_CRIOSQ));\r
-\r
-  CommandPacket.NvmeCmd        = &Command;\r
-  CommandPacket.NvmeCompletion = &Completion;\r
-\r
-  Command.Cdw0.Opcode = NVME_ADMIN_CRIOSQ_CMD;\r
-  CommandPacket.TransferBuffer = Private->SqBufferPciAddr[1];\r
-  CommandPacket.TransferLength = EFI_PAGE_SIZE;\r
-  CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;\r
-  CommandPacket.QueueType      = NVME_ADMIN_QUEUE;\r
-\r
-  CrIoSq.Qid   = NVME_IO_QUEUE;\r
-  CrIoSq.Qsize = NVME_CSQ_SIZE;\r
-  CrIoSq.Pc    = 1;\r
-  CrIoSq.Cqid  = NVME_IO_QUEUE;\r
-  CrIoSq.Qprio = 0;\r
-  CopyMem (&CommandPacket.NvmeCmd->Cdw10, &CrIoSq, sizeof (NVME_ADMIN_CRIOSQ));\r
-  CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID;\r
-\r
-  Status = Private->Passthru.PassThru (\r
-                               &Private->Passthru,\r
-                               0,\r
-                               &CommandPacket,\r
-                               NULL\r
-                               );\r
+  UINT32                                   Index;\r
+\r
+  for (Index = 1; Index < NVME_MAX_QUEUES; Index++) {\r
+    ZeroMem (&CommandPacket, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
+    ZeroMem (&Command, sizeof(EFI_NVM_EXPRESS_COMMAND));\r
+    ZeroMem (&Completion, sizeof(EFI_NVM_EXPRESS_COMPLETION));\r
+    ZeroMem (&CrIoSq, sizeof(NVME_ADMIN_CRIOSQ));\r
+\r
+    CommandPacket.NvmeCmd        = &Command;\r
+    CommandPacket.NvmeCompletion = &Completion;\r
+\r
+    Command.Cdw0.Opcode = NVME_ADMIN_CRIOSQ_CMD;\r
+    CommandPacket.TransferBuffer = Private->SqBufferPciAddr[Index];\r
+    CommandPacket.TransferLength = EFI_PAGE_SIZE;\r
+    CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;\r
+    CommandPacket.QueueType      = NVME_ADMIN_QUEUE;\r
+\r
+    CrIoSq.Qid   = Index;\r
+    CrIoSq.Qsize = (Index == 1) ? NVME_CSQ_SIZE : NVME_ASYNC_CSQ_SIZE;\r
+    CrIoSq.Pc    = 1;\r
+    CrIoSq.Cqid  = Index;\r
+    CrIoSq.Qprio = 0;\r
+    CopyMem (&CommandPacket.NvmeCmd->Cdw10, &CrIoSq, sizeof (NVME_ADMIN_CRIOSQ));\r
+    CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID;\r
+\r
+    Status = Private->Passthru.PassThru (\r
+                                 &Private->Passthru,\r
+                                 0,\r
+                                 &CommandPacket,\r
+                                 NULL\r
+                                 );\r
+    if (EFI_ERROR (Status)) {\r
+      break;\r
+    }\r
+  }\r
 \r
   return Status;\r
 }\r
@@ -844,12 +856,17 @@ NvmeControllerInit (
 \r
   Private->Cid[0] = 0;\r
   Private->Cid[1] = 0;\r
+  Private->Cid[2] = 0;\r
   Private->Pt[0]  = 0;\r
   Private->Pt[1]  = 0;\r
+  Private->Pt[2]  = 0;\r
   Private->SqTdbl[0].Sqt = 0;\r
   Private->SqTdbl[1].Sqt = 0;\r
+  Private->SqTdbl[2].Sqt = 0;\r
   Private->CqHdbl[0].Cqh = 0;\r
   Private->CqHdbl[1].Cqh = 0;\r
+  Private->CqHdbl[2].Cqh = 0;\r
+  Private->AsyncSqHead   = 0;\r
 \r
   Status = NvmeDisableController (Private);\r
 \r
@@ -878,7 +895,7 @@ NvmeControllerInit (
   //\r
   // Address of I/O submission & completion queue.\r
   //\r
-  ZeroMem (Private->Buffer, EFI_PAGES_TO_SIZE (4));\r
+  ZeroMem (Private->Buffer, EFI_PAGES_TO_SIZE (6));\r
   Private->SqBuffer[0]        = (NVME_SQ *)(UINTN)(Private->Buffer);\r
   Private->SqBufferPciAddr[0] = (NVME_SQ *)(UINTN)(Private->BufferPciAddr);\r
   Private->CqBuffer[0]        = (NVME_CQ *)(UINTN)(Private->Buffer + 1 * EFI_PAGE_SIZE);\r
@@ -887,14 +904,20 @@ NvmeControllerInit (
   Private->SqBufferPciAddr[1] = (NVME_SQ *)(UINTN)(Private->BufferPciAddr + 2 * EFI_PAGE_SIZE);\r
   Private->CqBuffer[1]        = (NVME_CQ *)(UINTN)(Private->Buffer + 3 * EFI_PAGE_SIZE);\r
   Private->CqBufferPciAddr[1] = (NVME_CQ *)(UINTN)(Private->BufferPciAddr + 3 * EFI_PAGE_SIZE);\r
+  Private->SqBuffer[2]        = (NVME_SQ *)(UINTN)(Private->Buffer + 4 * EFI_PAGE_SIZE);\r
+  Private->SqBufferPciAddr[2] = (NVME_SQ *)(UINTN)(Private->BufferPciAddr + 4 * EFI_PAGE_SIZE);\r
+  Private->CqBuffer[2]        = (NVME_CQ *)(UINTN)(Private->Buffer + 5 * EFI_PAGE_SIZE);\r
+  Private->CqBufferPciAddr[2] = (NVME_CQ *)(UINTN)(Private->BufferPciAddr + 5 * EFI_PAGE_SIZE);\r
 \r
   DEBUG ((EFI_D_INFO, "Private->Buffer = [%016X]\n", (UINT64)(UINTN)Private->Buffer));\r
-  DEBUG ((EFI_D_INFO, "Admin Submission Queue size (Aqa.Asqs) = [%08X]\n", Aqa.Asqs));\r
-  DEBUG ((EFI_D_INFO, "Admin Completion Queue size (Aqa.Acqs) = [%08X]\n", Aqa.Acqs));\r
-  DEBUG ((EFI_D_INFO, "Admin Submission Queue (SqBuffer[0]) = [%016X]\n", Private->SqBuffer[0]));\r
-  DEBUG ((EFI_D_INFO, "Admin Completion Queue (CqBuffer[0]) = [%016X]\n", Private->CqBuffer[0]));\r
-  DEBUG ((EFI_D_INFO, "I/O   Submission Queue (SqBuffer[1]) = [%016X]\n", Private->SqBuffer[1]));\r
-  DEBUG ((EFI_D_INFO, "I/O   Completion Queue (CqBuffer[1]) = [%016X]\n", Private->CqBuffer[1]));\r
+  DEBUG ((EFI_D_INFO, "Admin     Submission Queue size (Aqa.Asqs) = [%08X]\n", Aqa.Asqs));\r
+  DEBUG ((EFI_D_INFO, "Admin     Completion Queue size (Aqa.Acqs) = [%08X]\n", Aqa.Acqs));\r
+  DEBUG ((EFI_D_INFO, "Admin     Submission Queue (SqBuffer[0]) = [%016X]\n", Private->SqBuffer[0]));\r
+  DEBUG ((EFI_D_INFO, "Admin     Completion Queue (CqBuffer[0]) = [%016X]\n", Private->CqBuffer[0]));\r
+  DEBUG ((EFI_D_INFO, "Sync  I/O Submission Queue (SqBuffer[1]) = [%016X]\n", Private->SqBuffer[1]));\r
+  DEBUG ((EFI_D_INFO, "Sync  I/O Completion Queue (CqBuffer[1]) = [%016X]\n", Private->CqBuffer[1]));\r
+  DEBUG ((EFI_D_INFO, "Async I/O Submission Queue (SqBuffer[2]) = [%016X]\n", Private->SqBuffer[2]));\r
+  DEBUG ((EFI_D_INFO, "Async I/O Completion Queue (CqBuffer[2]) = [%016X]\n", Private->CqBuffer[2]));\r
 \r
   //\r
   // Program admin queue attributes.\r
@@ -971,7 +994,8 @@ NvmeControllerInit (
   DEBUG ((EFI_D_INFO, "    NN        : 0x%x\n", Private->ControllerData->Nn));\r
 \r
   //\r
-  // Create one I/O completion queue.\r
+  // Create two I/O completion queues.\r
+  // One for blocking I/O, one for non-blocking I/O.\r
   //\r
   Status = NvmeCreateIoCompletionQueue (Private);\r
   if (EFI_ERROR(Status)) {\r
@@ -979,7 +1003,8 @@ NvmeControllerInit (
   }\r
 \r
   //\r
-  // Create one I/O Submission queue.\r
+  // Create two I/O Submission queues.\r
+  // One for blocking I/O, one for non-blocking I/O.\r
   //\r
   Status = NvmeCreateIoSubmissionQueue (Private);\r
   if (EFI_ERROR(Status)) {\r
index f9871527dd889405dcae061ce94e002c9e67dab0..02219219b66e29808529133d2569f59d63ecce17 100644 (file)
@@ -362,7 +362,7 @@ NvmExpressPassThru (
   EFI_PCI_IO_PROTOCOL           *PciIo;\r
   NVME_SQ                       *Sq;\r
   NVME_CQ                       *Cq;\r
-  UINT8                         QueueType;\r
+  UINT16                        QueueId;\r
   UINT32                        Bytes;\r
   UINT16                        Offset;\r
   EFI_EVENT                     TimerEvent;\r
@@ -376,6 +376,8 @@ NvmExpressPassThru (
   VOID                          *PrpListHost;\r
   UINTN                         PrpListNo;\r
   UINT32                        Data;\r
+  NVME_PASS_THRU_ASYNC_REQ      *AsyncRequest;\r
+  EFI_TPL                       OldTpl;\r
 \r
   //\r
   // check the data fields in Packet parameter.\r
@@ -403,9 +405,25 @@ NvmExpressPassThru (
   TimerEvent  = NULL;\r
   Status      = EFI_SUCCESS;\r
 \r
-  QueueType = Packet->QueueType;\r
-  Sq  = Private->SqBuffer[QueueType] + Private->SqTdbl[QueueType].Sqt;\r
-  Cq  = Private->CqBuffer[QueueType] + Private->CqHdbl[QueueType].Cqh;\r
+  if (Packet->QueueType == NVME_ADMIN_QUEUE) {\r
+    QueueId = 0;\r
+  } else {\r
+    if (Event == NULL) {\r
+      QueueId = 1;\r
+    } else {\r
+      QueueId = 2;\r
+\r
+      //\r
+      // Submission queue full check.\r
+      //\r
+      if ((Private->SqTdbl[QueueId].Sqt + 1) % (NVME_ASYNC_CSQ_SIZE + 1) ==\r
+          Private->AsyncSqHead) {\r
+        return EFI_NOT_READY;\r
+      }\r
+    }\r
+  }\r
+  Sq  = Private->SqBuffer[QueueId] + Private->SqTdbl[QueueId].Sqt;\r
+  Cq  = Private->CqBuffer[QueueId] + Private->CqHdbl[QueueId].Cqh;\r
 \r
   if (Packet->NvmeCmd->Nsid != NamespaceId) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -414,7 +432,7 @@ NvmExpressPassThru (
   ZeroMem (Sq, sizeof (NVME_SQ));\r
   Sq->Opc  = (UINT8)Packet->NvmeCmd->Cdw0.Opcode;\r
   Sq->Fuse = (UINT8)Packet->NvmeCmd->Cdw0.FusedOperation;\r
-  Sq->Cid  = Private->Cid[QueueType]++;\r
+  Sq->Cid  = Private->Cid[QueueId]++;\r
   Sq->Nsid = Packet->NvmeCmd->Nsid;\r
 \r
   //\r
@@ -528,17 +546,45 @@ NvmExpressPassThru (
   //\r
   // Ring the submission queue doorbell.\r
   //\r
-  Private->SqTdbl[QueueType].Sqt ^= 1;\r
-  Data = ReadUnaligned32 ((UINT32*)&Private->SqTdbl[QueueType]);\r
+  if (Event != NULL) {\r
+    Private->SqTdbl[QueueId].Sqt =\r
+      (Private->SqTdbl[QueueId].Sqt + 1) % (NVME_ASYNC_CSQ_SIZE + 1);\r
+  } else {\r
+    Private->SqTdbl[QueueId].Sqt ^= 1;\r
+  }\r
+  Data = ReadUnaligned32 ((UINT32*)&Private->SqTdbl[QueueId]);\r
   PciIo->Mem.Write (\r
                PciIo,\r
                EfiPciIoWidthUint32,\r
                NVME_BAR,\r
-               NVME_SQTDBL_OFFSET(QueueType, Private->Cap.Dstrd),\r
+               NVME_SQTDBL_OFFSET(QueueId, Private->Cap.Dstrd),\r
                1,\r
                &Data\r
                );\r
 \r
+  //\r
+  // For non-blocking requests, return directly if the command is placed\r
+  // in the submission queue.\r
+  //\r
+  if (Event != NULL) {\r
+    AsyncRequest = AllocateZeroPool (sizeof (NVME_PASS_THRU_ASYNC_REQ));\r
+    if (AsyncRequest == NULL) {\r
+      Status = EFI_DEVICE_ERROR;\r
+      goto EXIT;\r
+    }\r
+\r
+    AsyncRequest->Signature     = NVME_PASS_THRU_ASYNC_REQ_SIG;\r
+    AsyncRequest->Packet        = Packet;\r
+    AsyncRequest->CommandId     = Sq->Cid;\r
+    AsyncRequest->CallerEvent   = Event;\r
+\r
+    OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+    InsertTailList (&Private->AsyncPassThruQueue, &AsyncRequest->Link);\r
+    gBS->RestoreTPL (OldTpl);\r
+\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   Status = gBS->CreateEvent (\r
                   EVT_TIMER,\r
                   TPL_CALLBACK,\r
@@ -561,7 +607,7 @@ NvmExpressPassThru (
   //\r
   Status = EFI_TIMEOUT;\r
   while (EFI_ERROR (gBS->CheckEvent (TimerEvent))) {\r
-    if (Cq->Pt != Private->Pt[QueueType]) {\r
+    if (Cq->Pt != Private->Pt[QueueId]) {\r
       Status = EFI_SUCCESS;\r
       break;\r
     }\r
@@ -589,16 +635,16 @@ NvmExpressPassThru (
     }\r
   }\r
 \r
-  if ((Private->CqHdbl[QueueType].Cqh ^= 1) == 0) {\r
-    Private->Pt[QueueType] ^= 1;\r
+  if ((Private->CqHdbl[QueueId].Cqh ^= 1) == 0) {\r
+    Private->Pt[QueueId] ^= 1;\r
   }\r
 \r
-  Data = ReadUnaligned32 ((UINT32*)&Private->CqHdbl[QueueType]);\r
+  Data = ReadUnaligned32 ((UINT32*)&Private->CqHdbl[QueueId]);\r
   PciIo->Mem.Write (\r
                PciIo,\r
                EfiPciIoWidthUint32,\r
                NVME_BAR,\r
-               NVME_CQHDBL_OFFSET(QueueType, Private->Cap.Dstrd),\r
+               NVME_CQHDBL_OFFSET(QueueId, Private->Cap.Dstrd),\r
                1,\r
                &Data\r
                );\r