From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web08.3660.1659061584944152843 for ; Thu, 28 Jul 2022 19:26:25 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=XEEgk52i; spf=pass (domain: intel.com, ip: 192.55.52.93, mailfrom: qi1.zhang@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659061584; x=1690597584; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Nze4rByAleFEtyrSCJEb429NRU7L/4O+5Oa4NQD8P/c=; b=XEEgk52iAJmn0VcIOeIbrrHweKgjYH3nyyDxIYPaPuu5TyN5HkSnBBiZ LOvTtdt1VbJ23M3iMPwjSapmkIt5rTP37zV4wXlCLBmj0xDTUuypr3VSb m2kw96Y3eltlYGAY+JuS/G57gY94C5VdBzL7dr2EPGjUsmB2ZNhq4Mc+5 xDYWE2d6O0U73gbFL1zjfmRQ6SR9Dcq/Dpt8wdVkSaaGH+k/mzH7zaSu5 m355oXPzj6p02p+JWZgf//Bg5aTa1UQocYfa2qikozTplOhVH6wD2WSec HL64coltimz80oiMC8TFEvHWEtPMUJONjojQ7OuB2RLgFzJ1C9KPwtr/S Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10422"; a="286221923" X-IronPort-AV: E=Sophos;i="5.93,200,1654585200"; d="scan'208";a="286221923" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jul 2022 19:26:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,200,1654585200"; d="scan'208";a="928572692" Received: from shwdesssddpdqi.ccr.corp.intel.com ([10.239.157.129]) by fmsmga005.fm.intel.com with ESMTP; 28 Jul 2022 19:26:22 -0700 From: "Qi Zhang" To: devel@edk2.groups.io Cc: Qi Zhang , Jiewen Yao , Jian J Wang , Swapnil Patil Subject: [PATCH v4] SecurityPkg: Add retry mechanism for tpm command Date: Fri, 29 Jul 2022 10:26:20 +0800 Message-Id: <20220729022620.1786-1-qi1.zhang@intel.com> X-Mailer: git-send-email 2.26.2.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3980 As per TCG PC Client Device Driver Design Principle document, if tpm commands fails due to timeout condition, then it should have retry mechanism (3 retry attempts). Existing implementation of PtpCrbTpmCommand does not have retry mechanism if it fails with EFI_TIMEOUT. See TCG PC Client Device Driver Design Principles for TPM 2.0 https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClient_Device_Dr= iver_Design_Principles_TPM2p0_v1p1_r4_211104_final.pdf Vision 1.1, Revision 0.04 Section 7.2.1 Signed-off-by: Qi Zhang Cc: Jiewen Yao Cc: Jian J Wang Reviewed-by: Jiewen Yao Tested-by: Swapnil Patil --- .../Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c | 108 +++++++++++------- 1 file changed, 69 insertions(+), 39 deletions(-) diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c b/SecurityPkg/= Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c index 1d99beaa10..840265292a 100644 --- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c +++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c @@ -33,6 +33,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent //=0D #define TPMCMDBUFLENGTH 0x500=0D =0D +//=0D +// Max retry count according to Spec TCG PC Client Device Driver Design Pr= inciples=0D +// for TPM2.0, Version 1.1, Revision 0.04, Section 7.2.1=0D +//=0D +#define RETRY_CNT_MAX 3=0D +=0D /**=0D Check whether TPM PTP register exist.=0D =0D @@ -153,6 +159,7 @@ PtpCrbTpmCommand ( UINT32 TpmOutSize;=0D UINT16 Data16;=0D UINT32 Data32;=0D + UINT8 RetryCnt;=0D =0D DEBUG_CODE_BEGIN ();=0D UINTN DebugSize;=0D @@ -179,53 +186,76 @@ PtpCrbTpmCommand ( DEBUG_CODE_END ();=0D TpmOutSize =3D 0;=0D =0D - //=0D - // STEP 0:=0D - // if CapCRbIdelByPass =3D=3D 0, enforce Idle state before sending comma= nd=0D - //=0D - if ((GetCachedIdleByPass () =3D=3D 0) && ((MmioRead32 ((UINTN)&CrbReg->C= rbControlStatus) & PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE) =3D=3D 0)) {=0D + RetryCnt =3D 0;=0D + while (TRUE) {=0D + //=0D + // STEP 0:=0D + // if CapCRbIdelByPass =3D=3D 0, enforce Idle state before sending com= mand=0D + //=0D + if ((GetCachedIdleByPass () =3D=3D 0) && ((MmioRead32 ((UINTN)&CrbReg-= >CrbControlStatus) & PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE) =3D=3D 0)) {=0D + Status =3D PtpCrbWaitRegisterBits (=0D + &CrbReg->CrbControlStatus,=0D + PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE,=0D + 0,=0D + PTP_TIMEOUT_C=0D + );=0D + if (EFI_ERROR (Status)) {=0D + RetryCnt++;=0D + if (RetryCnt < RETRY_CNT_MAX) {=0D + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_= AREA_REQUEST_GO_IDLE);=0D + continue;=0D + } else {=0D + //=0D + // Try to goIdle to recover TPM=0D + //=0D + Status =3D EFI_DEVICE_ERROR;=0D + goto GoIdle_Exit;=0D + }=0D + }=0D + }=0D +=0D + //=0D + // STEP 1:=0D + // Ready is any time the TPM is ready to receive a command, following = a write=0D + // of 1 by software to Request.cmdReady, as indicated by the Status fi= eld=0D + // being cleared to 0.=0D + //=0D + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AREA_R= EQUEST_COMMAND_READY);=0D Status =3D PtpCrbWaitRegisterBits (=0D - &CrbReg->CrbControlStatus,=0D - PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE,=0D + &CrbReg->CrbControlRequest,=0D 0,=0D + PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY,=0D PTP_TIMEOUT_C=0D );=0D if (EFI_ERROR (Status)) {=0D - //=0D - // Try to goIdle to recover TPM=0D - //=0D - Status =3D EFI_DEVICE_ERROR;=0D - goto GoIdle_Exit;=0D + RetryCnt++;=0D + if (RetryCnt < RETRY_CNT_MAX) {=0D + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AR= EA_REQUEST_GO_IDLE);=0D + continue;=0D + } else {=0D + Status =3D EFI_DEVICE_ERROR;=0D + goto GoIdle_Exit;=0D + }=0D }=0D - }=0D =0D - //=0D - // STEP 1:=0D - // Ready is any time the TPM is ready to receive a command, following a = write=0D - // of 1 by software to Request.cmdReady, as indicated by the Status fiel= d=0D - // being cleared to 0.=0D - //=0D - MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AREA_REQ= UEST_COMMAND_READY);=0D - Status =3D PtpCrbWaitRegisterBits (=0D - &CrbReg->CrbControlRequest,=0D - 0,=0D - PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY,=0D - PTP_TIMEOUT_C=0D - );=0D - if (EFI_ERROR (Status)) {=0D - Status =3D EFI_DEVICE_ERROR;=0D - goto GoIdle_Exit;=0D - }=0D + Status =3D PtpCrbWaitRegisterBits (=0D + &CrbReg->CrbControlStatus,=0D + 0,=0D + PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE,=0D + PTP_TIMEOUT_C=0D + );=0D + if (EFI_ERROR (Status)) {=0D + RetryCnt++;=0D + if (RetryCnt < RETRY_CNT_MAX) {=0D + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AR= EA_REQUEST_GO_IDLE);=0D + continue;=0D + } else {=0D + Status =3D EFI_DEVICE_ERROR;=0D + goto GoIdle_Exit;=0D + }=0D + }=0D =0D - Status =3D PtpCrbWaitRegisterBits (=0D - &CrbReg->CrbControlStatus,=0D - 0,=0D - PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE,=0D - PTP_TIMEOUT_C=0D - );=0D - if (EFI_ERROR (Status)) {=0D - Status =3D EFI_DEVICE_ERROR;=0D - goto GoIdle_Exit;=0D + break;=0D }=0D =0D //=0D --=20 2.26.2.windows.1