]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressPassthru.c
MdeModulePkg NvmExpressDxe: return error code in error path
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / NvmExpressDxe / NvmExpressPassthru.c
index 2fc4df0a43f6d3933168728fb5ffc2429f637f5a..cfad1587f299acff738723a73ca94d2a94d5fce2 100644 (file)
@@ -3,7 +3,7 @@
   NVM Express specification.\r
 \r
   (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2013 - 2018, 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
 \r
 #include "NvmExpress.h"\r
 \r
-//\r
-// Page size should be set in the Controller Configuration register\r
-// during controller init, and the controller configuration save in\r
-// the controller's private data.  The Max and Min supported page sizes\r
-// for the controller are specified in the Controller Capabilities register.\r
-//\r
-\r
-GLOBAL_REMOVE_IF_UNREFERENCED NVM_EXPRESS_PASS_THRU_MODE gNvmExpressPassThruMode = {\r
-  0,\r
-  NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL | NVM_EXPRESS_PASS_THRU_ATTRIBUTES_CMD_SET_NVME,\r
-  sizeof (UINTN),\r
-  0x10000,\r
-  0,\r
-  0\r
-};\r
-\r
-\r
 /**\r
   Dump the execution status from a given completion queue entry.\r
 \r
@@ -333,23 +316,115 @@ EXIT:
 }\r
 \r
 \r
+/**\r
+  Aborts the asynchronous PassThru requests.\r
+\r
+  @param[in] Private        The pointer to the NVME_CONTROLLER_PRIVATE_DATA\r
+                            data structure.\r
+\r
+  @retval EFI_SUCCESS       The asynchronous PassThru requests have been aborted.\r
+  @return EFI_DEVICE_ERROR  Fail to abort all the asynchronous PassThru requests.\r
+\r
+**/\r
+EFI_STATUS\r
+AbortAsyncPassThruTasks (\r
+  IN NVME_CONTROLLER_PRIVATE_DATA    *Private\r
+  )\r
+{\r
+  EFI_PCI_IO_PROTOCOL                *PciIo;\r
+  LIST_ENTRY                         *Link;\r
+  LIST_ENTRY                         *NextLink;\r
+  NVME_BLKIO2_SUBTASK                *Subtask;\r
+  NVME_BLKIO2_REQUEST                *BlkIo2Request;\r
+  NVME_PASS_THRU_ASYNC_REQ           *AsyncRequest;\r
+  EFI_BLOCK_IO2_TOKEN                *Token;\r
+  EFI_TPL                            OldTpl;\r
+  EFI_STATUS                         Status;\r
+\r
+  PciIo  = Private->PciIo;\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+  //\r
+  // Cancel the unsubmitted subtasks.\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
+\r
+    BlkIo2Request->UnsubmittedSubtaskNum--;\r
+    if (Subtask->IsLast) {\r
+      BlkIo2Request->LastSubtaskSubmitted = TRUE;\r
+    }\r
+    Token->TransactionStatus = EFI_ABORTED;\r
+\r
+    RemoveEntryList (Link);\r
+    InsertTailList (&BlkIo2Request->SubtasksQueue, Link);\r
+    gBS->SignalEvent (Subtask->Event);\r
+  }\r
+\r
+  //\r
+  // Cleanup the resources for the asynchronous PassThru requests.\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
+\r
+    if (AsyncRequest->MapData != NULL) {\r
+      PciIo->Unmap (PciIo, AsyncRequest->MapData);\r
+    }\r
+    if (AsyncRequest->MapMeta != NULL) {\r
+      PciIo->Unmap (PciIo, AsyncRequest->MapMeta);\r
+    }\r
+    if (AsyncRequest->MapPrpList != NULL) {\r
+      PciIo->Unmap (PciIo, AsyncRequest->MapPrpList);\r
+    }\r
+    if (AsyncRequest->PrpListHost != NULL) {\r
+      PciIo->FreeBuffer (\r
+               PciIo,\r
+               AsyncRequest->PrpListNo,\r
+               AsyncRequest->PrpListHost\r
+               );\r
+    }\r
+\r
+    RemoveEntryList (Link);\r
+    gBS->SignalEvent (AsyncRequest->CallerEvent);\r
+    FreePool (AsyncRequest);\r
+  }\r
+\r
+  if (IsListEmpty (&Private->AsyncPassThruQueue) &&\r
+      IsListEmpty (&Private->UnsubmittedSubtasks)) {\r
+    Status = EFI_SUCCESS;\r
+  } else {\r
+    Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return Status;\r
+}\r
+\r
+\r
 /**\r
   Sends an NVM Express Command Packet to an NVM Express controller or namespace. This function supports\r
-  both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the nonblocking\r
+  both blocking I/O and non-blocking I/O. The blocking I/O functionality is required, and the non-blocking\r
   I/O functionality is optional.\r
 \r
-  @param[in]     This                A pointer to the NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
-  @param[in]     NamespaceId         Is a 32 bit Namespace ID to which the Express HCI command packet will be sent.\r
-                                     A value of 0 denotes the NVM Express controller, a value of all 0FFh in the namespace\r
-                                     ID specifies that the command packet should be sent to all valid namespaces.\r
-  @param[in]     NamespaceUuid       Is a 64 bit Namespace UUID to which the Express HCI command packet will be sent.\r
-                                     A value of 0 denotes the NVM Express controller, a value of all 0FFh in the namespace\r
-                                     UUID specifies that the command packet should be sent to all valid namespaces.\r
-  @param[in,out] Packet              A pointer to the NVM Express HCI Command Packet to send to the NVMe namespace specified\r
-                                     by NamespaceId.\r
-  @param[in]     Event               If nonblocking I/O is not supported then Event is ignored, and blocking I/O is performed.\r
-                                     If Event is NULL, then blocking I/O is performed. If Event is not NULL and non blocking I/O\r
-                                     is supported, then nonblocking I/O is performed, and Event will be signaled when the NVM\r
+\r
+  @param[in]     This                A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
+  @param[in]     NamespaceId         A 32 bit namespace ID as defined in the NVMe specification to which the NVM Express Command\r
+                                     Packet will be sent.  A value of 0 denotes the NVM Express controller, a value of all 0xFF's\r
+                                     (all bytes are 0xFF) in the namespace ID specifies that the command packet should be sent to\r
+                                     all valid namespaces.\r
+  @param[in,out] Packet              A pointer to the NVM Express Command Packet.\r
+  @param[in]     Event               If non-blocking I/O is not supported then Event is ignored, and blocking I/O is performed.\r
+                                     If Event is NULL, then blocking I/O is performed. If Event is not NULL and non-blocking I/O\r
+                                     is supported, then non-blocking I/O is performed, and Event will be signaled when the NVM\r
                                      Express Command Packet completes.\r
 \r
   @retval EFI_SUCCESS                The NVM Express Command Packet was sent by the host. TransferLength bytes were transferred\r
@@ -359,42 +434,48 @@ EXIT:
   @retval EFI_NOT_READY              The NVM Express Command Packet could not be sent because the controller is not ready. The caller\r
                                      may retry again later.\r
   @retval EFI_DEVICE_ERROR           A device error occurred while attempting to send the NVM Express Command Packet.\r
-  @retval EFI_INVALID_PARAMETER      Namespace, or the contents of NVM_EXPRESS_PASS_THRU_COMMAND_PACKET are invalid. The NVM\r
+  @retval EFI_INVALID_PARAMETER      NamespaceId or the contents of EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET are invalid. The NVM\r
                                      Express Command Packet was not sent, so no additional status information is available.\r
-  @retval EFI_UNSUPPORTED            The command described by the NVM Express Command Packet is not supported by the host adapter.\r
-                                     The NVM Express Command Packet was not sent, so no additional status information is available.\r
+  @retval EFI_UNSUPPORTED            The command described by the NVM Express Command Packet is not supported by the NVM Express\r
+                                     controller. The NVM Express Command Packet was not sent so no additional status information\r
+                                     is available.\r
   @retval EFI_TIMEOUT                A timeout occurred while waiting for the NVM Express Command Packet to execute.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 NvmExpressPassThru (\r
-  IN     NVM_EXPRESS_PASS_THRU_PROTOCOL              *This,\r
+  IN     EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL          *This,\r
   IN     UINT32                                      NamespaceId,\r
-  IN     UINT64                                      NamespaceUuid,\r
-  IN OUT NVM_EXPRESS_PASS_THRU_COMMAND_PACKET        *Packet,\r
+  IN OUT EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET    *Packet,\r
   IN     EFI_EVENT                                   Event OPTIONAL\r
   )\r
 {\r
-  NVME_CONTROLLER_PRIVATE_DATA  *Private;\r
-  EFI_STATUS                    Status;\r
-  EFI_PCI_IO_PROTOCOL           *PciIo;\r
-  NVME_SQ                       *Sq;\r
-  NVME_CQ                       *Cq;\r
-  UINT8                         Qid;\r
-  UINT32                        Bytes;\r
-  UINT16                        Offset;\r
-  EFI_EVENT                     TimerEvent;\r
-  EFI_PCI_IO_PROTOCOL_OPERATION Flag;\r
-  EFI_PHYSICAL_ADDRESS          PhyAddr;\r
-  VOID                          *MapData;\r
-  VOID                          *MapMeta;\r
-  VOID                          *MapPrpList;\r
-  UINTN                         MapLength;\r
-  UINT64                        *Prp;\r
-  VOID                          *PrpListHost;\r
-  UINTN                         PrpListNo;\r
-  UINT32                        Data;\r
+  NVME_CONTROLLER_PRIVATE_DATA   *Private;\r
+  EFI_STATUS                     Status;\r
+  EFI_STATUS                     PreviousStatus;\r
+  EFI_PCI_IO_PROTOCOL            *PciIo;\r
+  NVME_SQ                        *Sq;\r
+  NVME_CQ                        *Cq;\r
+  UINT16                         QueueId;\r
+  UINT32                         Bytes;\r
+  UINT16                         Offset;\r
+  EFI_EVENT                      TimerEvent;\r
+  EFI_PCI_IO_PROTOCOL_OPERATION  Flag;\r
+  EFI_PHYSICAL_ADDRESS           PhyAddr;\r
+  VOID                           *MapData;\r
+  VOID                           *MapMeta;\r
+  VOID                           *MapPrpList;\r
+  UINTN                          MapLength;\r
+  UINT64                         *Prp;\r
+  VOID                           *PrpListHost;\r
+  UINTN                          PrpListNo;\r
+  UINT32                         Attributes;\r
+  UINT32                         IoAlign;\r
+  UINT32                         MaxTransLen;\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,15 +484,59 @@ NvmExpressPassThru (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if ((Packet->NvmeCmd == NULL) || (Packet->NvmeResponse == NULL)) {\r
+  if ((Packet->NvmeCmd == NULL) || (Packet->NvmeCompletion == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Packet->QueueType != NVME_ADMIN_QUEUE && Packet->QueueType != NVME_IO_QUEUE) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // 'Attributes' with neither EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_LOGICAL nor\r
+  // EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL set is an illegal\r
+  // configuration.\r
+  //\r
+  Attributes  = This->Mode->Attributes;\r
+  if ((Attributes & (EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL |\r
+    EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_LOGICAL)) == 0) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Buffer alignment check for TransferBuffer & MetadataBuffer.\r
+  //\r
+  IoAlign     = This->Mode->IoAlign;\r
+  if (IoAlign > 0 && (((UINTN) Packet->TransferBuffer & (IoAlign - 1)) != 0)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (Packet->QueueId != NVME_ADMIN_QUEUE && Packet->QueueId != NVME_IO_QUEUE) {\r
+  if (IoAlign > 0 && (((UINTN) Packet->MetadataBuffer & (IoAlign - 1)) != 0)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   Private     = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (This);\r
+\r
+  //\r
+  // Check NamespaceId is valid or not.\r
+  //\r
+  if ((NamespaceId > Private->ControllerData->Nn) &&\r
+      (NamespaceId != (UINT32) -1)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check whether TransferLength exceeds the maximum data transfer size.\r
+  //\r
+  if (Private->ControllerData->Mdts != 0) {\r
+    MaxTransLen = (1 << (Private->ControllerData->Mdts)) *\r
+                  (1 << (Private->Cap.Mpsmin + 12));\r
+    if (Packet->TransferLength > MaxTransLen) {\r
+      Packet->TransferLength = MaxTransLen;\r
+      return EFI_BAD_BUFFER_SIZE;\r
+    }\r
+  }\r
+\r
   PciIo       = Private->PciIo;\r
   MapData     = NULL;\r
   MapMeta     = NULL;\r
@@ -422,18 +547,34 @@ NvmExpressPassThru (
   TimerEvent  = NULL;\r
   Status      = EFI_SUCCESS;\r
 \r
-  Qid = Packet->QueueId;\r
-  Sq  = Private->SqBuffer[Qid] + Private->SqTdbl[Qid].Sqt;\r
-  Cq  = Private->CqBuffer[Qid] + Private->CqHdbl[Qid].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
   }\r
 \r
   ZeroMem (Sq, sizeof (NVME_SQ));\r
-  Sq->Opc  = Packet->NvmeCmd->Cdw0.Opcode;\r
-  Sq->Fuse = Packet->NvmeCmd->Cdw0.FusedOperation;\r
-  Sq->Cid  = Packet->NvmeCmd->Cdw0.Cid;\r
+  Sq->Opc  = (UINT8)Packet->NvmeCmd->Cdw0.Opcode;\r
+  Sq->Fuse = (UINT8)Packet->NvmeCmd->Cdw0.FusedOperation;\r
+  Sq->Cid  = Private->Cid[QueueId]++;\r
   Sq->Nsid = Packet->NvmeCmd->Nsid;\r
 \r
   //\r
@@ -452,7 +593,12 @@ NvmExpressPassThru (
   // these two cmds are special which requires their data buffer must support simultaneous access by both the\r
   // processor and a PCI Bus Master. It's caller's responsbility to ensure this.\r
   //\r
-  if (((Sq->Opc & (BIT0 | BIT1)) != 0) && (Sq->Opc != NVME_ADMIN_CRIOCQ_OPC) && (Sq->Opc != NVME_ADMIN_CRIOSQ_OPC)) {\r
+  if (((Sq->Opc & (BIT0 | BIT1)) != 0) &&\r
+      !((Packet->QueueType == NVME_ADMIN_QUEUE) && ((Sq->Opc == NVME_ADMIN_CRIOCQ_CMD) || (Sq->Opc == NVME_ADMIN_CRIOSQ_CMD)))) {\r
+    if ((Packet->TransferLength == 0) || (Packet->TransferBuffer == NULL)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
     if ((Sq->Opc & BIT0) != 0) {\r
       Flag = EfiPciIoOperationBusMasterRead;\r
     } else {\r
@@ -475,8 +621,7 @@ NvmExpressPassThru (
     Sq->Prp[0] = PhyAddr;\r
     Sq->Prp[1] = 0;\r
 \r
-    MapLength = Packet->MetadataLength;\r
-    if(Packet->MetadataBuffer != NULL) {\r
+    if((Packet->MetadataLength != 0) && (Packet->MetadataBuffer != NULL)) {\r
       MapLength = Packet->MetadataLength;\r
       Status = PciIo->Map (\r
                         PciIo,\r
@@ -511,6 +656,7 @@ NvmExpressPassThru (
     PhyAddr = (Sq->Prp[0] + EFI_PAGE_SIZE) & ~(EFI_PAGE_SIZE - 1);\r
     Prp = NvmeCreatePrpList (PciIo, PhyAddr, EFI_SIZE_TO_PAGES(Offset + Bytes) - 1, &PrpListHost, &PrpListNo, &MapPrpList);\r
     if (Prp == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
       goto EXIT;\r
     }\r
 \r
@@ -519,6 +665,12 @@ NvmExpressPassThru (
     Sq->Prp[1] = (Sq->Prp[0] + EFI_PAGE_SIZE) & ~(EFI_PAGE_SIZE - 1);\r
   }\r
 \r
+  if(Packet->NvmeCmd->Flags & CDW2_VALID) {\r
+    Sq->Rsvd2 = (UINT64)Packet->NvmeCmd->Cdw2;\r
+  }\r
+  if(Packet->NvmeCmd->Flags & CDW3_VALID) {\r
+    Sq->Rsvd2 |= LShiftU64 ((UINT64)Packet->NvmeCmd->Cdw3, 32);\r
+  }\r
   if(Packet->NvmeCmd->Flags & CDW10_VALID) {\r
     Sq->Payload.Raw.Cdw10 = Packet->NvmeCmd->Cdw10;\r
   }\r
@@ -541,17 +693,54 @@ NvmExpressPassThru (
   //\r
   // Ring the submission queue doorbell.\r
   //\r
-  Private->SqTdbl[Qid].Sqt ^= 1;\r
-  Data = ReadUnaligned32 ((UINT32*)&Private->SqTdbl[Qid]);\r
-  PciIo->Mem.Write (\r
+  if ((Event != NULL) && (QueueId != 0)) {\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
+  Status = PciIo->Mem.Write (\r
                PciIo,\r
                EfiPciIoWidthUint32,\r
                NVME_BAR,\r
-               NVME_SQTDBL_OFFSET(Qid, Private->Cap.Dstrd),\r
+               NVME_SQTDBL_OFFSET(QueueId, Private->Cap.Dstrd),\r
                1,\r
                &Data\r
                );\r
 \r
+  if (EFI_ERROR (Status)) {\r
+    goto EXIT;\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) && (QueueId != 0)) {\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
+    AsyncRequest->MapData       = MapData;\r
+    AsyncRequest->MapMeta       = MapMeta;\r
+    AsyncRequest->MapPrpList    = MapPrpList;\r
+    AsyncRequest->PrpListNo     = PrpListNo;\r
+    AsyncRequest->PrpListHost   = PrpListHost;\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
@@ -566,7 +755,6 @@ NvmExpressPassThru (
   Status = gBS->SetTimer(TimerEvent, TimerRelative, Packet->CommandTimeout);\r
 \r
   if (EFI_ERROR(Status)) {\r
-    Packet->ControllerStatus = NVM_EXPRESS_STATUS_CONTROLLER_DEVICE_ERROR;\r
     goto EXIT;\r
   }\r
 \r
@@ -574,40 +762,99 @@ NvmExpressPassThru (
   // Wait for completion queue to get filled in.\r
   //\r
   Status = EFI_TIMEOUT;\r
-  Packet->ControllerStatus = NVM_EXPRESS_STATUS_CONTROLLER_TIMEOUT_COMMAND;\r
   while (EFI_ERROR (gBS->CheckEvent (TimerEvent))) {\r
-    if (Cq->Pt != Private->Pt[Qid]) {\r
+    if (Cq->Pt != Private->Pt[QueueId]) {\r
       Status = EFI_SUCCESS;\r
-      Packet->ControllerStatus = NVM_EXPRESS_STATUS_CONTROLLER_READY;\r
       break;\r
     }\r
   }\r
 \r
-  if ((Private->CqHdbl[Qid].Cqh ^= 1) == 0) {\r
-    Private->Pt[Qid] ^= 1;\r
-  }\r
-\r
   //\r
-  // Copy the Respose Queue entry for this command to the callers response buffer\r
+  // Check the NVMe cmd execution result\r
   //\r
-  CopyMem(Packet->NvmeResponse, Cq, sizeof(NVM_EXPRESS_RESPONSE));\r
+  if (Status != EFI_TIMEOUT) {\r
+    if ((Cq->Sct == 0) && (Cq->Sc == 0)) {\r
+      Status = EFI_SUCCESS;\r
+    } else {\r
+      Status = EFI_DEVICE_ERROR;\r
+      //\r
+      // Copy the Respose Queue entry for this command to the callers response buffer\r
+      //\r
+      CopyMem(Packet->NvmeCompletion, Cq, sizeof(EFI_NVM_EXPRESS_COMPLETION));\r
+    \r
+      //\r
+      // Dump every completion entry status for debugging.\r
+      //\r
+      DEBUG_CODE_BEGIN();\r
+        NvmeDumpStatus(Cq);\r
+      DEBUG_CODE_END();\r
+    }\r
+  } else {\r
+    //\r
+    // Timeout occurs for an NVMe command. Reset the controller to abort the\r
+    // outstanding commands.\r
+    //\r
+    DEBUG ((DEBUG_ERROR, "NvmExpressPassThru: Timeout occurs for an NVMe command.\n"));\r
 \r
-  //\r
-  // Dump every completion entry status for debugging.\r
-  //\r
-  DEBUG_CODE_BEGIN();\r
-    NvmeDumpStatus(Cq);\r
-  DEBUG_CODE_END();\r
+    //\r
+    // Disable the timer to trigger the process of async transfers temporarily.\r
+    //\r
+    Status = gBS->SetTimer (Private->TimerEvent, TimerCancel, 0);\r
+    if (EFI_ERROR (Status)) {\r
+      goto EXIT;\r
+    }\r
+\r
+    //\r
+    // Reset the NVMe controller.\r
+    //\r
+    Status = NvmeControllerInit (Private);\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = AbortAsyncPassThruTasks (Private);\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // Re-enable the timer to trigger the process of async transfers.\r
+        //\r
+        Status = gBS->SetTimer (Private->TimerEvent, TimerPeriodic, NVME_HC_ASYNC_TIMER);\r
+        if (!EFI_ERROR (Status)) {\r
+          //\r
+          // Return EFI_TIMEOUT to indicate a timeout occurs for NVMe PassThru command.\r
+          //\r
+          Status = EFI_TIMEOUT;\r
+        }\r
+      }\r
+    } else {\r
+      Status = EFI_DEVICE_ERROR;\r
+    }\r
 \r
-  Data = ReadUnaligned32 ((UINT32*)&Private->CqHdbl[Qid]);\r
-  PciIo->Mem.Write (\r
+    goto EXIT;\r
+  }\r
+\r
+  if ((Private->CqHdbl[QueueId].Cqh ^= 1) == 0) {\r
+    Private->Pt[QueueId] ^= 1;\r
+  }\r
+\r
+  Data = ReadUnaligned32 ((UINT32*)&Private->CqHdbl[QueueId]);\r
+  PreviousStatus = Status;\r
+  Status = PciIo->Mem.Write (\r
                PciIo,\r
                EfiPciIoWidthUint32,\r
                NVME_BAR,\r
-               NVME_CQHDBL_OFFSET(Qid, Private->Cap.Dstrd),\r
+               NVME_CQHDBL_OFFSET(QueueId, Private->Cap.Dstrd),\r
                1,\r
                &Data\r
                );\r
+  // The return status of PciIo->Mem.Write should not override\r
+  // previous status if previous status contains error.\r
+  Status = EFI_ERROR (PreviousStatus) ? PreviousStatus : Status;\r
+\r
+  //\r
+  // For now, the code does not support the non-blocking feature for admin queue.\r
+  // If Event is not NULL for admin queue, signal the caller's event here.\r
+  //\r
+  if (Event != NULL) {\r
+    ASSERT (QueueId == 0);\r
+    gBS->SignalEvent (Event);\r
+  }\r
 \r
 EXIT:\r
   if (MapData != NULL) {\r
@@ -642,47 +889,43 @@ EXIT:
 }\r
 \r
 /**\r
-  Used to retrieve the list of namespaces defined on an NVM Express controller.\r
+  Used to retrieve the next namespace ID for this NVM Express controller.\r
+\r
+  The EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL.GetNextNamespace() function retrieves the next valid\r
+  namespace ID on this NVM Express controller.\r
 \r
-  The NVM_EXPRESS_PASS_THRU_PROTOCOL.GetNextNamespace() function retrieves a list of namespaces\r
-  defined on an NVM Express controller. If on input a NamespaceID is specified by all 0xFF in the\r
-  namespace buffer, then the first namespace defined on the NVM Express controller is returned in\r
-  NamespaceID, and a status of EFI_SUCCESS is returned.\r
+  If on input the value pointed to by NamespaceId is 0xFFFFFFFF, then the first valid namespace\r
+  ID defined on the NVM Express controller is returned in the location pointed to by NamespaceId\r
+  and a status of EFI_SUCCESS is returned.\r
 \r
-  If NamespaceId is a Namespace value that was returned on a previous call to GetNextNamespace(),\r
-  then the next valid NamespaceId  for an NVM Express SSD namespace on the NVM Express controller\r
-  is returned in NamespaceId, and EFI_SUCCESS is returned.\r
+  If on input the value pointed to by NamespaceId is an invalid namespace ID other than 0xFFFFFFFF,\r
+  then EFI_INVALID_PARAMETER is returned.\r
 \r
-  If Namespace array is not a 0xFFFFFFFF and NamespaceId was not returned on a previous call to\r
-  GetNextNamespace(), then EFI_INVALID_PARAMETER is returned.\r
+  If on input the value pointed to by NamespaceId is a valid namespace ID, then the next valid\r
+  namespace ID on the NVM Express controller is returned in the location pointed to by NamespaceId,\r
+  and EFI_SUCCESS is returned.\r
 \r
-  If NamespaceId is the NamespaceId of the last SSD namespace on the NVM Express controller, then\r
-  EFI_NOT_FOUND is returned\r
+  If the value pointed to by NamespaceId is the namespace ID of the last namespace on the NVM\r
+  Express controller, then EFI_NOT_FOUND is returned.\r
 \r
-  @param[in]     This           A pointer to the NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
+  @param[in]     This           A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
   @param[in,out] NamespaceId    On input, a pointer to a legal NamespaceId for an NVM Express\r
                                 namespace present on the NVM Express controller. On output, a\r
                                 pointer to the next NamespaceId of an NVM Express namespace on\r
                                 an NVM Express controller. An input value of 0xFFFFFFFF retrieves\r
                                 the first NamespaceId for an NVM Express namespace present on an\r
                                 NVM Express controller.\r
-  @param[out]    NamespaceUuid  On output, the UUID associated with the next namespace, if a UUID\r
-                                is defined for that NamespaceId, otherwise, zero is returned in\r
-                                this parameter. If the caller does not require a UUID, then a NULL\r
-                                pointer may be passed.\r
 \r
-  @retval EFI_SUCCESS           The NamespaceId of the next Namespace was returned.\r
+  @retval EFI_SUCCESS           The Namespace ID of the next Namespace was returned.\r
   @retval EFI_NOT_FOUND         There are no more namespaces defined on this controller.\r
-  @retval EFI_INVALID_PARAMETER Namespace array is not a 0xFFFFFFFF and NamespaceId was not returned\r
-                                on a previous call to GetNextNamespace().\r
+  @retval EFI_INVALID_PARAMETER NamespaceId is an invalid value other than 0xFFFFFFFF.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 NvmExpressGetNextNamespace (\r
-  IN     NVM_EXPRESS_PASS_THRU_PROTOCOL              *This,\r
-  IN OUT UINT32                                      *NamespaceId,\r
-     OUT UINT64                                      *NamespaceUuid  OPTIONAL\r
+  IN     EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL          *This,\r
+  IN OUT UINT32                                      *NamespaceId\r
   )\r
 {\r
   NVME_CONTROLLER_PRIVATE_DATA     *Private;\r
@@ -721,15 +964,16 @@ NvmExpressGetNextNamespace (
     }\r
 \r
     *NamespaceId = NextNamespaceId;\r
-    if (NamespaceUuid != NULL) {\r
-      *NamespaceUuid = NamespaceData->Eui64;\r
-    }\r
   } else {\r
-    if (*NamespaceId >= Private->ControllerData->Nn) {\r
+    if (*NamespaceId > Private->ControllerData->Nn) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
 \r
     NextNamespaceId = *NamespaceId + 1;\r
+    if (NextNamespaceId > Private->ControllerData->Nn) {\r
+      return EFI_NOT_FOUND;\r
+    }\r
+\r
     //\r
     // Allocate buffer for Identify Namespace data.\r
     //\r
@@ -744,9 +988,6 @@ NvmExpressGetNextNamespace (
     }\r
 \r
     *NamespaceId = NextNamespaceId;\r
-    if (NamespaceUuid != NULL) {\r
-      *NamespaceUuid = NamespaceData->Eui64;\r
-    }\r
   }\r
 \r
 Done:\r
@@ -758,41 +999,41 @@ Done:
 }\r
 \r
 /**\r
-  Used to translate a device path node to a Namespace ID and Namespace UUID.\r
+  Used to translate a device path node to a namespace ID.\r
 \r
-  The NVM_EXPRESS_PASS_THRU_PROTOCOL.GetNamwspace() function determines the Namespace ID and Namespace UUID\r
-  associated with the NVM Express SSD namespace described by DevicePath. If DevicePath is a device path node type\r
-  that the NVM Express Pass Thru driver supports, then the NVM Express Pass Thru driver will attempt to translate\r
-  the contents DevicePath into a Namespace ID and UUID. If this translation is successful, then that Namespace ID\r
-  and UUID are returned in NamespaceID and NamespaceUUID, and EFI_SUCCESS is returned.\r
+  The EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL.GetNamespace() function determines the namespace ID associated with the\r
+  namespace described by DevicePath.\r
 \r
-  @param[in]  This                A pointer to the NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
+  If DevicePath is a device path node type that the NVM Express Pass Thru driver supports, then the NVM Express\r
+  Pass Thru driver will attempt to translate the contents DevicePath into a namespace ID.\r
+\r
+  If this translation is successful, then that namespace ID is returned in NamespaceId, and EFI_SUCCESS is returned\r
+\r
+  @param[in]  This                A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
   @param[in]  DevicePath          A pointer to the device path node that describes an NVM Express namespace on\r
                                   the NVM Express controller.\r
   @param[out] NamespaceId         The NVM Express namespace ID contained in the device path node.\r
-  @param[out] NamespaceUuid       The NVM Express namespace contained in the device path node.\r
 \r
-  @retval EFI_SUCCESS             DevicePath was successfully translated to NamespaceId and NamespaceUuid.\r
-  @retval EFI_INVALID_PARAMETER   If DevicePath, NamespaceId, or NamespaceUuid are NULL, then EFI_INVALID_PARAMETER\r
-                                  is returned.\r
+  @retval EFI_SUCCESS             DevicePath was successfully translated to NamespaceId.\r
+  @retval EFI_INVALID_PARAMETER   If DevicePath or NamespaceId are NULL, then EFI_INVALID_PARAMETER is returned.\r
   @retval EFI_UNSUPPORTED         If DevicePath is not a device path node type that the NVM Express Pass Thru driver\r
                                   supports, then EFI_UNSUPPORTED is returned.\r
-  @retval EFI_NOT_FOUND           If DevicePath is a device path node type that the Nvm Express Pass Thru driver\r
-                                  supports, but there is not a valid translation from DevicePath to a NamespaceID\r
-                                  and NamespaceUuid, then EFI_NOT_FOUND is returned.\r
+  @retval EFI_NOT_FOUND           If DevicePath is a device path node type that the NVM Express Pass Thru driver\r
+                                  supports, but there is not a valid translation from DevicePath to a namespace ID,\r
+                                  then EFI_NOT_FOUND is returned.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 NvmExpressGetNamespace (\r
-  IN     NVM_EXPRESS_PASS_THRU_PROTOCOL              *This,\r
+  IN     EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL          *This,\r
   IN     EFI_DEVICE_PATH_PROTOCOL                    *DevicePath,\r
-     OUT UINT32                                      *NamespaceId,\r
-     OUT UINT64                                      *NamespaceUuid\r
+     OUT UINT32                                      *NamespaceId\r
   )\r
 {\r
   NVME_NAMESPACE_DEVICE_PATH       *Node;\r
+  NVME_CONTROLLER_PRIVATE_DATA     *Private;\r
 \r
-  if ((This == NULL) || (DevicePath == NULL) || (NamespaceId == NULL) || (NamespaceUuid == NULL)) {\r
+  if ((This == NULL) || (DevicePath == NULL) || (NamespaceId == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -800,15 +1041,23 @@ NvmExpressGetNamespace (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  Node = (NVME_NAMESPACE_DEVICE_PATH *)DevicePath;\r
+  Node    = (NVME_NAMESPACE_DEVICE_PATH *)DevicePath;\r
+  Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (This);\r
 \r
   if (DevicePath->SubType == MSG_NVME_NAMESPACE_DP) {\r
     if (DevicePathNodeLength(DevicePath) != sizeof(NVME_NAMESPACE_DEVICE_PATH)) {\r
       return EFI_NOT_FOUND;\r
     }\r
 \r
-    *NamespaceId   = Node->NamespaceId;\r
-    *NamespaceUuid = Node->NamespaceUuid;\r
+    //\r
+    // Check NamespaceId in the device path node is valid or not.\r
+    //\r
+    if ((Node->NamespaceId == 0) ||\r
+      (Node->NamespaceId > Private->ControllerData->Nn)) {\r
+      return EFI_NOT_FOUND;\r
+    }\r
+\r
+    *NamespaceId = Node->NamespaceId;\r
 \r
     return EFI_SUCCESS;\r
   } else {\r
@@ -819,10 +1068,10 @@ NvmExpressGetNamespace (
 /**\r
   Used to allocate and build a device path node for an NVM Express namespace on an NVM Express controller.\r
 \r
-  The NVM_EXPRESS_PASS_THRU_PROTOCOL.BuildDevicePath() function allocates and builds a single device\r
+  The EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL.BuildDevicePath() function allocates and builds a single device\r
   path node for the NVM Express namespace specified by NamespaceId.\r
 \r
-  If the namespace device specified by NamespaceId is not valid , then EFI_NOT_FOUND is returned.\r
+  If the NamespaceId is not valid, then EFI_NOT_FOUND is returned.\r
 \r
   If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.\r
 \r
@@ -831,12 +1080,10 @@ NvmExpressGetNamespace (
   Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of DevicePath are\r
   initialized to describe the NVM Express namespace specified by NamespaceId, and EFI_SUCCESS is returned.\r
 \r
-  @param[in]     This                A pointer to the NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
+  @param[in]     This                A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r
   @param[in]     NamespaceId         The NVM Express namespace ID  for which a device path node is to be\r
                                      allocated and built. Caller must set the NamespaceId to zero if the\r
                                      device path node will contain a valid UUID.\r
-  @param[in]     NamespaceUuid       The NVM Express namespace UUID for which a device path node is to be\r
-                                     allocated and built. UUID will only be valid of the Namespace ID is zero.\r
   @param[in,out] DevicePath          A pointer to a single device path node that describes the NVM Express\r
                                      namespace specified by NamespaceId. This function is responsible for\r
                                      allocating the buffer DevicePath with the boot service AllocatePool().\r
@@ -844,8 +1091,7 @@ NvmExpressGetNamespace (
                                      is finished with DevicePath.\r
   @retval EFI_SUCCESS                The device path node that describes the NVM Express namespace specified\r
                                      by NamespaceId was allocated and returned in DevicePath.\r
-  @retval EFI_NOT_FOUND              The NVM Express namespace specified by NamespaceId does not exist on the\r
-                                     NVM Express controller.\r
+  @retval EFI_NOT_FOUND              The NamespaceId is not valid.\r
   @retval EFI_INVALID_PARAMETER      DevicePath is NULL.\r
   @retval EFI_OUT_OF_RESOURCES       There are not enough resources to allocate the DevicePath node.\r
 \r
@@ -853,13 +1099,15 @@ NvmExpressGetNamespace (
 EFI_STATUS\r
 EFIAPI\r
 NvmExpressBuildDevicePath (\r
-  IN     NVM_EXPRESS_PASS_THRU_PROTOCOL              *This,\r
+  IN     EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL          *This,\r
   IN     UINT32                                      NamespaceId,\r
-  IN     UINT64                                      NamespaceUuid,\r
   IN OUT EFI_DEVICE_PATH_PROTOCOL                    **DevicePath\r
   )\r
 {\r
   NVME_NAMESPACE_DEVICE_PATH     *Node;\r
+  NVME_CONTROLLER_PRIVATE_DATA   *Private;\r
+  EFI_STATUS                     Status;\r
+  NVME_ADMIN_NAMESPACE_DATA      *NamespaceData;\r
 \r
   //\r
   // Validate parameters\r
@@ -868,12 +1116,18 @@ NvmExpressBuildDevicePath (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (NamespaceId == 0) {\r
+  Status  = EFI_SUCCESS;\r
+  Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (This);\r
+\r
+  //\r
+  // Check NamespaceId is valid or not.\r
+  //\r
+  if ((NamespaceId == 0) ||\r
+    (NamespaceId > Private->ControllerData->Nn)) {\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
   Node = (NVME_NAMESPACE_DEVICE_PATH *)AllocateZeroPool (sizeof (NVME_NAMESPACE_DEVICE_PATH));\r
-\r
   if (Node == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -882,9 +1136,43 @@ NvmExpressBuildDevicePath (
   Node->Header.SubType = MSG_NVME_NAMESPACE_DP;\r
   SetDevicePathNodeLength (&Node->Header, sizeof (NVME_NAMESPACE_DEVICE_PATH));\r
   Node->NamespaceId    = NamespaceId;\r
-  Node->NamespaceUuid  = NamespaceUuid;\r
+\r
+  //\r
+  // Allocate a buffer for Identify Namespace data.\r
+  //\r
+  NamespaceData = NULL;\r
+  NamespaceData = AllocateZeroPool(sizeof (NVME_ADMIN_NAMESPACE_DATA));\r
+  if(NamespaceData == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Exit;\r
+  }\r
+\r
+  //\r
+  // Get UUID from specified Identify Namespace data.\r
+  //\r
+  Status = NvmeIdentifyNamespace (\r
+             Private,\r
+             NamespaceId,\r
+             (VOID *)NamespaceData\r
+             );\r
+\r
+  if (EFI_ERROR(Status)) {\r
+    goto Exit;\r
+  }\r
+\r
+  Node->NamespaceUuid = NamespaceData->Eui64;\r
 \r
   *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)Node;\r
-  return EFI_SUCCESS;\r
+\r
+Exit:\r
+  if(NamespaceData != NULL) {\r
+    FreePool (NamespaceData);\r
+  }\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Node);\r
+  }\r
+\r
+  return Status;\r
 }\r
 \r