]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/NvmExpressDxe: Notify NVME HW when system reset happens
authorRuiyu Ni <ruiyu.ni@intel.com>
Fri, 4 Aug 2017 09:07:31 +0000 (17:07 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Wed, 9 Aug 2017 07:59:13 +0000 (15:59 +0800)
Per NVM Express Spec, software should notify NVME HW when shutdown
occurs.

The NVME controller requires notification for shutdown as part of
its management of internal structures. Even with FUA, failing to
notify the NVME controller to shutdown power off causes the NVME
controller to take quite some time to organize its tables on the
next power on. This time exceeds the normal timeout, so we would
fail to boot the NVME disk.

The host should set the Shutdown Notification (CC.SHN) field to 01b
to indicate a normal shutdown operation. The controller indicates
when shutdown processing is completed by updating the Shutdown Status
(CSTS.SHST) field to 10b.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.c
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.h
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c

index de5c2a05eae1b4d5e00a24d733414f94ca3e46b5..571c2e1b78ddcc3e8124137f28ee0ad392874ac3 100644 (file)
@@ -2,7 +2,7 @@
   NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows\r
   NVM Express specification.\r
 \r
-  Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2013 - 2017, 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
@@ -1046,6 +1046,8 @@ NvmExpressDriverBindingStart (
     if (EFI_ERROR (Status)) {\r
       goto Exit;\r
     }\r
+\r
+    NvmeRegisterShutdownNotification ();\r
   } else {\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
@@ -1239,6 +1241,9 @@ NvmExpressDriverBindingStop (
           This->DriverBindingHandle,\r
           Controller\r
           );\r
+\r
+    NvmeUnregisterShutdownNotification ();\r
+\r
     return EFI_SUCCESS;\r
   }\r
 \r
index fa4a34ab53cb4b9a4dd378fa9ecc8aee7b9afb71..ad0d9b8966a94fb3e88bd2a982228c7535bf4eb8 100644 (file)
@@ -3,7 +3,7 @@
   NVM Express specification.\r
 \r
   (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
-  Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2013 - 2017, 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
@@ -34,6 +34,7 @@
 #include <Protocol/DiskInfo.h>\r
 #include <Protocol/DriverSupportedEfiVersion.h>\r
 #include <Protocol/StorageSecurityCommand.h>\r
+#include <Protocol/ResetNotification.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
@@ -721,4 +722,24 @@ NvmeDumpStatus (
   IN NVME_CQ             *Cq\r
   );\r
 \r
+/**\r
+  Register the shutdown notification through the ResetNotification protocol.\r
+\r
+  Register the shutdown notification when mNvmeControllerNumber increased from 0 to 1.\r
+**/\r
+VOID\r
+NvmeRegisterShutdownNotification (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Unregister the shutdown notification through the ResetNotification protocol.\r
+\r
+  Unregister the shutdown notification when mNvmeControllerNumber decreased from 1 to 0.\r
+**/\r
+VOID\r
+NvmeUnregisterShutdownNotification (\r
+  VOID\r
+  );\r
+\r
 #endif\r
index 3270042e026e38a0e8c096c978168c6a6c0a2d26..491869610423f525a5052071f1d20ae43aef82ff 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 - 2016, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2013 - 2017, 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
@@ -71,6 +71,7 @@
   gEfiDiskInfoProtocolGuid                    ## BY_START\r
   gEfiStorageSecurityCommandProtocolGuid      ## BY_START\r
   gEfiDriverSupportedEfiVersionProtocolGuid   ## PRODUCES\r
+  gEfiResetNotificationProtocolGuid           ## CONSUMES\r
 \r
 # [Event]\r
 # EVENT_TYPE_RELATIVE_TIMER ## SOMETIMES_CONSUMES\r
index ad6cdb15a59a9220687901e517c581e82b800cbe..ea48e78827a6457f831d08959e1695842c4d74c9 100644 (file)
@@ -2,7 +2,7 @@
   NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows\r
   NVM Express specification.\r
 \r
-  Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2013 - 2017, 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
+#define NVME_SHUTDOWN_PROCESS_TIMEOUT 45\r
+\r
+//\r
+// The number of NVME controllers managed by this driver, used by\r
+// NvmeRegisterShutdownNotification() and NvmeUnregisterShutdownNotification().\r
+//\r
+UINTN                           mNvmeControllerNumber = 0;\r
+\r
 /**\r
   Read Nvm Express controller capability register.\r
 \r
@@ -1050,3 +1058,174 @@ NvmeControllerInit (
   return Status;\r
 }\r
 \r
+/**\r
+ This routine is called to properly shutdown the Nvm Express controller per NVMe spec.\r
+\r
+  @param[in]  ResetType         The type of reset to perform.\r
+  @param[in]  ResetStatus       The status code for the reset.\r
+  @param[in]  DataSize          The size, in bytes, of ResetData.\r
+  @param[in]  ResetData         For a ResetType of EfiResetCold, EfiResetWarm, or\r
+                                EfiResetShutdown the data buffer starts with a Null-terminated\r
+                                string, optionally followed by additional binary data.\r
+                                The string is a description that the caller may use to further\r
+                                indicate the reason for the system reset. ResetData is only\r
+                                valid if ResetStatus is something other than EFI_SUCCESS\r
+                                unless the ResetType is EfiResetPlatformSpecific\r
+                                where a minimum amount of ResetData is always required.\r
+                                For a ResetType of EfiResetPlatformSpecific the data buffer\r
+                                also starts with a Null-terminated string that is followed\r
+                                by an EFI_GUID that describes the specific type of reset to perform.\r
+**/\r
+VOID\r
+EFIAPI\r
+NvmeShutdownAllControllers (\r
+  IN EFI_RESET_TYPE           ResetType,\r
+  IN EFI_STATUS               ResetStatus,\r
+  IN UINTN                    DataSize,\r
+  IN VOID                     *ResetData OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_HANDLE                          *Handles;\r
+  UINTN                               HandleCount;\r
+  UINTN                               HandleIndex;\r
+  EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfos;\r
+  UINTN                               OpenInfoCount;\r
+  UINTN                               OpenInfoIndex;\r
+  EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL  *NvmePassThru;\r
+  NVME_CC                             Cc;\r
+  NVME_CSTS                           Csts;\r
+  UINTN                               Index;\r
+  NVME_CONTROLLER_PRIVATE_DATA        *Private;\r
+\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiPciIoProtocolGuid,\r
+                  NULL,\r
+                  &HandleCount,\r
+                  &Handles\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    HandleCount = 0;\r
+  }\r
+\r
+  for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
+    Status = gBS->OpenProtocolInformation (\r
+                    Handles[HandleIndex],\r
+                    &gEfiPciIoProtocolGuid,\r
+                    &OpenInfos,\r
+                    &OpenInfoCount\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {\r
+      //\r
+      // Find all the NVME controller managed by this driver.\r
+      // gImageHandle equals to DriverBinding handle for this driver.\r
+      //\r
+      if (((OpenInfos[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) != 0) &&\r
+          (OpenInfos[OpenInfoIndex].AgentHandle == gImageHandle)) {\r
+        Status = gBS->OpenProtocol (\r
+                        OpenInfos[OpenInfoIndex].ControllerHandle,\r
+                        &gEfiNvmExpressPassThruProtocolGuid,\r
+                        (VOID **) &NvmePassThru,\r
+                        NULL,\r
+                        NULL,\r
+                        EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                        );\r
+        if (EFI_ERROR (Status)) {\r
+          continue;\r
+        }\r
+        Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (NvmePassThru);\r
+\r
+        //\r
+        // Read Controller Configuration Register.\r
+        //\r
+        Status = ReadNvmeControllerConfiguration (Private, &Cc);\r
+        if (EFI_ERROR(Status)) {\r
+          continue;\r
+        }\r
+        //\r
+        // The host should set the Shutdown Notification (CC.SHN) field to 01b\r
+        // to indicate a normal shutdown operation.\r
+        //\r
+        Cc.Shn = NVME_CC_SHN_NORMAL_SHUTDOWN;\r
+        Status = WriteNvmeControllerConfiguration (Private, &Cc);\r
+        if (EFI_ERROR(Status)) {\r
+          continue;\r
+        }\r
+\r
+        //\r
+        // The controller indicates when shutdown processing is completed by updating the\r
+        // Shutdown Status (CSTS.SHST) field to 10b.\r
+        // Wait up to 45 seconds (break down to 4500 x 10ms) for the shutdown to complete.\r
+        //\r
+        for (Index = 0; Index < NVME_SHUTDOWN_PROCESS_TIMEOUT * 100; Index++) {\r
+          Status = ReadNvmeControllerStatus (Private, &Csts);\r
+          if (!EFI_ERROR(Status) && (Csts.Shst == NVME_CSTS_SHST_SHUTDOWN_COMPLETED)) {\r
+            DEBUG((DEBUG_INFO, "NvmeShutdownController: shutdown processing is completed after %dms.\n", Index * 10));\r
+            break;\r
+          }\r
+          //\r
+          // Stall for 10ms\r
+          //\r
+          gBS->Stall (10 * 1000);\r
+        }\r
+\r
+        if (Index == NVME_SHUTDOWN_PROCESS_TIMEOUT * 100) {\r
+          DEBUG((DEBUG_ERROR, "NvmeShutdownController: shutdown processing is timed out\n"));\r
+        }\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Register the shutdown notification through the ResetNotification protocol.\r
+\r
+  Register the shutdown notification when mNvmeControllerNumber increased from 0 to 1.\r
+**/\r
+VOID\r
+NvmeRegisterShutdownNotification (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_RESET_NOTIFICATION_PROTOCOL *ResetNotify;\r
+\r
+  mNvmeControllerNumber++;\r
+  if (mNvmeControllerNumber == 1) {\r
+    Status = gBS->LocateProtocol (&gEfiResetNotificationProtocolGuid, NULL, (VOID **) &ResetNotify);\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = ResetNotify->RegisterResetNotify (ResetNotify, NvmeShutdownAllControllers);\r
+      ASSERT_EFI_ERROR (Status);\r
+    } else {\r
+      DEBUG ((DEBUG_WARN, "NVME: ResetNotification absent! Shutdown notification cannot be performed!\n"));\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Unregister the shutdown notification through the ResetNotification protocol.\r
+\r
+  Unregister the shutdown notification when mNvmeControllerNumber decreased from 1 to 0.\r
+**/\r
+VOID\r
+NvmeUnregisterShutdownNotification (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_RESET_NOTIFICATION_PROTOCOL *ResetNotify;\r
+\r
+  mNvmeControllerNumber--;\r
+  if (mNvmeControllerNumber == 0) {\r
+    Status = gBS->LocateProtocol (&gEfiResetNotificationProtocolGuid, NULL, (VOID **) &ResetNotify);\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = ResetNotify->UnregisterResetNotify (ResetNotify, NvmeShutdownAllControllers);\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+  }\r
+}\r