From nobody Mon Apr 29 07:52:24 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1502076714042140.98976920232758; Sun, 6 Aug 2017 20:31:54 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A47B821DF37BE; Sun, 6 Aug 2017 20:29:32 -0700 (PDT) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 1672621CE7417 for ; Sun, 6 Aug 2017 20:29:31 -0700 (PDT) Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Aug 2017 20:31:46 -0700 Received: from ray-dev.ccr.corp.intel.com ([10.239.9.2]) by orsmga004.jf.intel.com with ESMTP; 06 Aug 2017 20:31:45 -0700 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.41,336,1498546800"; d="scan'208";a="116307069" From: Ruiyu Ni To: edk2-devel@lists.01.org Date: Mon, 7 Aug 2017 11:31:41 +0800 Message-Id: <20170807033142.177248-2-ruiyu.ni@intel.com> X-Mailer: git-send-email 2.12.2.windows.2 In-Reply-To: <20170807033142.177248-1-ruiyu.ni@intel.com> References: <20170807033142.177248-1-ruiyu.ni@intel.com> Subject: [edk2] [PATCH 1/2] MdePkg/Nvme: Add NVME shutdown notification related macros X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hao A Wu MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ruiyu Ni Cc: Hao A Wu Reviewed-by: Hao Wu --- MdePkg/Include/IndustryStandard/Nvme.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MdePkg/Include/IndustryStandard/Nvme.h b/MdePkg/Include/Indust= ryStandard/Nvme.h index 85649f0d3f..d0bd354139 100644 --- a/MdePkg/Include/IndustryStandard/Nvme.h +++ b/MdePkg/Include/IndustryStandard/Nvme.h @@ -2,6 +2,7 @@ Definitions based on NVMe spec. version 1.1. =20 (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ Copyright (c) 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -86,6 +87,8 @@ typedef struct { UINT8 Iocqes:4; // I/O Completion Queue Entry Size UINT8 Rsvd2; } NVME_CC; +#define NVME_CC_SHN_NORMAL_SHUTDOWN 1 +#define NVME_CC_SHN_ABRUPT_SHUTDOWN 2 =20 // // 3.1.6 Offset 1Ch: CSTS - Controller Status @@ -97,7 +100,8 @@ typedef struct { UINT32 Nssro:1; // NVM Subsystem Reset Occurred UINT32 Rsvd1:27; } NVME_CSTS; - +#define NVME_CSTS_SHST_SHUTDOWN_OCCURRING 1 +#define NVME_CSTS_SHST_SHUTDOWN_COMPLETED 2 // // 3.1.8 Offset 24h: AQA - Admin Queue Attributes // --=20 2.12.2.windows.2 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Mon Apr 29 07:52:24 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1502076714782140.7263012244606; Sun, 6 Aug 2017 20:31:54 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id E228521DF37CF; Sun, 6 Aug 2017 20:29:33 -0700 (PDT) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 05A2E21E1DACF for ; Sun, 6 Aug 2017 20:29:32 -0700 (PDT) Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Aug 2017 20:31:47 -0700 Received: from ray-dev.ccr.corp.intel.com ([10.239.9.2]) by orsmga004.jf.intel.com with ESMTP; 06 Aug 2017 20:31:46 -0700 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.41,336,1498546800"; d="scan'208";a="116307073" From: Ruiyu Ni To: edk2-devel@lists.01.org Date: Mon, 7 Aug 2017 11:31:42 +0800 Message-Id: <20170807033142.177248-3-ruiyu.ni@intel.com> X-Mailer: git-send-email 2.12.2.windows.2 In-Reply-To: <20170807033142.177248-1-ruiyu.ni@intel.com> References: <20170807033142.177248-1-ruiyu.ni@intel.com> Subject: [edk2] [PATCH 2/2] MdeModulePkg/NvmExpressDxe: Notify NVME HW when system reset happens X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hao A Wu MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Per NVM Express Spec, software should notify NVME HW when shutdown occurs. 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 Cc: Hao A Wu Reviewed-by: Hao Wu --- MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.c | 7 +- MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.h | 23 ++- .../Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf | 3 +- MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c | 181 +++++++++++++++++= +++- 4 files changed, 210 insertions(+), 4 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.c b/MdeModulePkg= /Bus/Pci/NvmExpressDxe/NvmExpress.c index de5c2a05ea..571c2e1b78 100644 --- a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.c +++ b/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.c @@ -2,7 +2,7 @@ NvmExpressDxe driver is used to manage non-volatile memory subsystem whi= ch follows NVM Express specification. =20 - Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -1046,6 +1046,8 @@ NvmExpressDriverBindingStart ( if (EFI_ERROR (Status)) { goto Exit; } + + NvmeRegisterShutdownNotification (); } else { Status =3D gBS->OpenProtocol ( Controller, @@ -1239,6 +1241,9 @@ NvmExpressDriverBindingStop ( This->DriverBindingHandle, Controller ); + + NvmeUnregisterShutdownNotification (); + return EFI_SUCCESS; } =20 diff --git a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.h b/MdeModulePkg= /Bus/Pci/NvmExpressDxe/NvmExpress.h index fa4a34ab53..ad0d9b8966 100644 --- a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.h +++ b/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.h @@ -3,7 +3,7 @@ NVM Express specification. =20 (C) Copyright 2016 Hewlett Packard Enterprise Development LP
- Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -34,6 +34,7 @@ #include #include #include +#include =20 #include #include @@ -721,4 +722,24 @@ NvmeDumpStatus ( IN NVME_CQ *Cq ); =20 +/** + Register the shutdown notification through the ResetNotification protoco= l. + + Register the shutdown notification when mNvmeControllerNumber increased = from 0 to 1. +**/ +VOID +NvmeRegisterShutdownNotification ( + VOID + ); + +/** + Unregister the shutdown notification through the ResetNotification proto= col. + + Unregister the shutdown notification when mNvmeControllerNumber decrease= d from 1 to 0. +**/ +VOID +NvmeUnregisterShutdownNotification ( + VOID + ); + #endif diff --git a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf b/MdeModu= lePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf index 3270042e02..4918696104 100644 --- a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf +++ b/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf @@ -4,7 +4,7 @@ # NvmExpressDxe driver is used to manage non-volatile memory subsystem wh= ich follows # NVM Express specification. # -# Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+# Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.
# # This program and the accompanying materials # are licensed and made available under the terms and conditions of the B= SD License @@ -71,6 +71,7 @@ [Protocols] gEfiDiskInfoProtocolGuid ## BY_START gEfiStorageSecurityCommandProtocolGuid ## BY_START gEfiDriverSupportedEfiVersionProtocolGuid ## PRODUCES + gEfiResetNotificationProtocolGuid ## CONSUMES =20 # [Event] # EVENT_TYPE_RELATIVE_TIMER ## SOMETIMES_CONSUMES diff --git a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c b/MdeModule= Pkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c index ad6cdb15a5..ea48e78827 100644 --- a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c +++ b/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressHci.c @@ -2,7 +2,7 @@ NvmExpressDxe driver is used to manage non-volatile memory subsystem whi= ch follows NVM Express specification. =20 - Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BS= D License which accompanies this distribution. The full text of the license may b= e found at @@ -15,6 +15,14 @@ =20 #include "NvmExpress.h" =20 +#define NVME_SHUTDOWN_PROCESS_TIMEOUT 45 + +// +// The number of NVME controllers managed by this driver, used by +// NvmeRegisterShutdownNotification() and NvmeUnregisterShutdownNotificati= on(). +// +UINTN mNvmeControllerNumber =3D 0; + /** Read Nvm Express controller capability register. =20 @@ -1050,3 +1058,174 @@ NvmeControllerInit ( return Status; } =20 +/** + This routine is called to properly shutdown the Nvm Express controller pe= r NVMe spec. + + @param[in] ResetType The type of reset to perform. + @param[in] ResetStatus The status code for the reset. + @param[in] DataSize The size, in bytes, of ResetData. + @param[in] ResetData For a ResetType of EfiResetCold, EfiResetW= arm, or + EfiResetShutdown the data buffer starts wi= th a Null-terminated + string, optionally followed by additional = binary data. + The string is a description that the calle= r may use to further + indicate the reason for the system reset. = ResetData is only + valid if ResetStatus is something other th= an EFI_SUCCESS + unless the ResetType is EfiResetPlatformSp= ecific + where a minimum amount of ResetData is alw= ays required. + For a ResetType of EfiResetPlatformSpecifi= c the data buffer + also starts with a Null-terminated string = that is followed + by an EFI_GUID that describes the specific= type of reset to perform. +**/ +VOID +EFIAPI +NvmeShutdownAllControllers ( + IN EFI_RESET_TYPE ResetType, + IN EFI_STATUS ResetStatus, + IN UINTN DataSize, + IN VOID *ResetData OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_HANDLE *Handles; + UINTN HandleCount; + UINTN HandleIndex; + EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfos; + UINTN OpenInfoCount; + UINTN OpenInfoIndex; + EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *NvmePassThru; + NVME_CC Cc; + NVME_CSTS Csts; + UINTN Index; + NVME_CONTROLLER_PRIVATE_DATA *Private; + + Status =3D gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiPciIoProtocolGuid, + NULL, + &HandleCount, + &Handles + ); + if (EFI_ERROR (Status)) { + HandleCount =3D 0; + } + + for (HandleIndex =3D 0; HandleIndex < HandleCount; HandleIndex++) { + Status =3D gBS->OpenProtocolInformation ( + Handles[HandleIndex], + &gEfiPciIoProtocolGuid, + &OpenInfos, + &OpenInfoCount + ); + if (EFI_ERROR (Status)) { + continue; + } + + for (OpenInfoIndex =3D 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex= ++) { + // + // Find all the NVME controller managed by this driver. + // gImageHandle equals to DriverBinding handle for this driver. + // + if (((OpenInfos[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRI= VER) !=3D 0) && + (OpenInfos[OpenInfoIndex].AgentHandle =3D=3D gImageHandle)) { + Status =3D gBS->OpenProtocol ( + OpenInfos[OpenInfoIndex].ControllerHandle, + &gEfiNvmExpressPassThruProtocolGuid, + (VOID **) &NvmePassThru, + NULL, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + continue; + } + Private =3D NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (NvmePassT= hru); + + // + // Read Controller Configuration Register. + // + Status =3D ReadNvmeControllerConfiguration (Private, &Cc); + if (EFI_ERROR(Status)) { + continue; + } + // + // The host should set the Shutdown Notification (CC.SHN) field to= 01b + // to indicate a normal shutdown operation. + // + Cc.Shn =3D NVME_CC_SHN_NORMAL_SHUTDOWN; + Status =3D WriteNvmeControllerConfiguration (Private, &Cc); + if (EFI_ERROR(Status)) { + continue; + } + + // + // The controller indicates when shutdown processing is completed = by updating the + // Shutdown Status (CSTS.SHST) field to 10b. + // Wait up to 45 seconds (break down to 4500 x 10ms) for the shutd= own to complete. + // + for (Index =3D 0; Index < NVME_SHUTDOWN_PROCESS_TIMEOUT * 100; Ind= ex++) { + Status =3D ReadNvmeControllerStatus (Private, &Csts); + if (!EFI_ERROR(Status) && (Csts.Shst =3D=3D NVME_CSTS_SHST_SHUTD= OWN_COMPLETED)) { + DEBUG((DEBUG_INFO, "NvmeShutdownController: shutdown processin= g is completed after %dms.\n", Index * 10)); + break; + } + // + // Stall for 10ms + // + gBS->Stall (10 * 1000); + } + + if (Index =3D=3D NVME_SHUTDOWN_PROCESS_TIMEOUT * 100) { + DEBUG((DEBUG_ERROR, "NvmeShutdownController: shutdown processing= is timed out\n")); + } + } + } + } +} + +/** + Register the shutdown notification through the ResetNotification protoco= l. + + Register the shutdown notification when mNvmeControllerNumber increased = from 0 to 1. +**/ +VOID +NvmeRegisterShutdownNotification ( + VOID + ) +{ + EFI_STATUS Status; + EFI_RESET_NOTIFICATION_PROTOCOL *ResetNotify; + + mNvmeControllerNumber++; + if (mNvmeControllerNumber =3D=3D 1) { + Status =3D gBS->LocateProtocol (&gEfiResetNotificationProtocolGuid, NU= LL, (VOID **) &ResetNotify); + if (!EFI_ERROR (Status)) { + Status =3D ResetNotify->RegisterResetNotify (ResetNotify, NvmeShutdo= wnAllControllers); + ASSERT_EFI_ERROR (Status); + } else { + DEBUG ((DEBUG_WARN, "NVME: ResetNotification absent! Shutdown notifi= cation cannot be performed!\n")); + } + } +} + +/** + Unregister the shutdown notification through the ResetNotification proto= col. + + Unregister the shutdown notification when mNvmeControllerNumber decrease= d from 1 to 0. +**/ +VOID +NvmeUnregisterShutdownNotification ( + VOID + ) +{ + EFI_STATUS Status; + EFI_RESET_NOTIFICATION_PROTOCOL *ResetNotify; + + mNvmeControllerNumber--; + if (mNvmeControllerNumber =3D=3D 0) { + Status =3D gBS->LocateProtocol (&gEfiResetNotificationProtocolGuid, NU= LL, (VOID **) &ResetNotify); + if (!EFI_ERROR (Status)) { + Status =3D ResetNotify->UnregisterResetNotify (ResetNotify, NvmeShut= downAllControllers); + ASSERT_EFI_ERROR (Status); + } + } +} --=20 2.12.2.windows.2 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel