From nobody Tue Apr 23 15:58:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+91910+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+91910+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1658970374; cv=none; d=zohomail.com; s=zohoarc; b=n2BH13gJ117/R1mXG1SMRbEqG5uTwGBeRjgqcp+87RcCoaB7EPqxgpZfz779JywsnLZd4zjtWCmrt4LB1Sk/QnDdfqVS8XjUEofbO263Fr94ZBZ72aZK96o60qTywi2fQ/5WFvjvcDgvkvamhSRrN6PDFU3eI2gAM/nxPNchB8I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658970374; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=z98i1OPaxzdtGKHZf+c8K3sH/ikyVvT2CsVIZrU6kx4=; b=GFrUa4ooJCmNuEmQb7QUeRd8Gfhh8VlT514XRH4v97jlAfRGChPVR2RoMcThibXjzZQ4F11cBk4o1bKN3WEVD+CEpBWphX5yxaHJmfaeyLnvQQTJvqT53iprVnfaT8rPMRYUo70Tukd49htwgv74BQHhZA9dsBcCUDr+JDE9Bqc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+91910+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1658970374670996.1864164865369; Wed, 27 Jul 2022 18:06:14 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 4fycYY1788612xNPsdX3nKcS; Wed, 27 Jul 2022 18:06:14 -0700 X-Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web09.27526.1658970373511902770 for ; Wed, 27 Jul 2022 18:06:13 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10421"; a="285933709" X-IronPort-AV: E=Sophos;i="5.93,196,1654585200"; d="scan'208";a="285933709" X-Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2022 18:06:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,196,1654585200"; d="scan'208";a="551064267" X-Received: from shwdesssddpdqi.ccr.corp.intel.com ([10.239.157.129]) by orsmga003.jf.intel.com with ESMTP; 27 Jul 2022 18:06:10 -0700 From: "Qi Zhang" To: devel@edk2.groups.io Cc: Qi Zhang , Jiewen Yao , Jian J Wang , Swapnil Patil Subject: [edk2-devel] [PATCH v2] SecurityPkg: Add retry mechanism for tpm command Date: Thu, 28 Jul 2022 09:06:07 +0800 Message-Id: <20220728010607.988-1-qi1.zhang@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,qi1.zhang@intel.com X-Gm-Message-State: RKNieGgqkN1X6xjD2mqvrQGox1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1658970374; bh=7KbGdhMIOUD4cPYa1id7HIlXC5xcfixU++FtV0PMWqs=; h=Cc:Date:From:Reply-To:Subject:To; b=A88LKaAG0+O1wixAp1bUbsiqUGNAnWNnOm5UrKxkWOta3DFjXcSqx5z+6zwRxBpBUyz yZ1eMWjnWgehZSLlmQ5UFaLPmNCPbDUZL7UiZWiLu34fhAQ8LWA3n3g5ESN1TbpuUhhsi mBZ0PuVW2tenKuvKlaUqufjG8KgG6eKRqUw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1658970375372100001 Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3980 Signed-off-by: Qi Zhang Cc: Jiewen Yao Cc: Jian J Wang Reviewed-by: Jiewen Yao Tested-by: Swapnil Patil --- .../Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c | 107 +++++++++++------- 1 file changed, 68 insertions(+), 39 deletions(-) diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c b/SecurityPkg/= Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c index 1d99beaa10..6b5994fde2 100644 --- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c +++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c @@ -33,6 +33,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent // #define TPMCMDBUFLENGTH 0x500 =20 +// +// Max retry count +// +#define RETRY_CNT_MAX 3 + /** Check whether TPM PTP register exist. =20 @@ -153,6 +158,7 @@ PtpCrbTpmCommand ( UINT32 TpmOutSize; UINT16 Data16; UINT32 Data32; + UINT8 RetryCnt; =20 DEBUG_CODE_BEGIN (); UINTN DebugSize; @@ -179,53 +185,76 @@ PtpCrbTpmCommand ( DEBUG_CODE_END (); TpmOutSize =3D 0; =20 - // - // STEP 0: - // if CapCRbIdelByPass =3D=3D 0, enforce Idle state before sending comma= nd - // - if ((GetCachedIdleByPass () =3D=3D 0) && ((MmioRead32 ((UINTN)&CrbReg->C= rbControlStatus) & PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE) =3D=3D 0)) { + RetryCnt =3D 0; + while (TRUE) { + // + // STEP 0: + // if CapCRbIdelByPass =3D=3D 0, enforce Idle state before sending com= mand + // + if ((GetCachedIdleByPass () =3D=3D 0) && ((MmioRead32 ((UINTN)&CrbReg-= >CrbControlStatus) & PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE) =3D=3D 0)) { + Status =3D PtpCrbWaitRegisterBits ( + &CrbReg->CrbControlStatus, + PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE, + 0, + PTP_TIMEOUT_C + ); + if (EFI_ERROR (Status)) { + RetryCnt++; + if (RetryCnt < RETRY_CNT_MAX) { + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_= AREA_REQUEST_GO_IDLE); + continue; + } else { + // + // Try to goIdle to recover TPM + // + Status =3D EFI_DEVICE_ERROR; + goto GoIdle_Exit; + } + } + } + + // + // STEP 1: + // Ready is any time the TPM is ready to receive a command, following = a write + // of 1 by software to Request.cmdReady, as indicated by the Status fi= eld + // being cleared to 0. + // + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AREA_R= EQUEST_COMMAND_READY); Status =3D PtpCrbWaitRegisterBits ( - &CrbReg->CrbControlStatus, - PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE, + &CrbReg->CrbControlRequest, 0, + PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY, PTP_TIMEOUT_C ); if (EFI_ERROR (Status)) { - // - // Try to goIdle to recover TPM - // - Status =3D EFI_DEVICE_ERROR; - goto GoIdle_Exit; + RetryCnt++; + if (RetryCnt < RETRY_CNT_MAX) { + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AR= EA_REQUEST_GO_IDLE); + continue; + } else { + Status =3D EFI_DEVICE_ERROR; + goto GoIdle_Exit; + } } - } =20 - // - // STEP 1: - // Ready is any time the TPM is ready to receive a command, following a = write - // of 1 by software to Request.cmdReady, as indicated by the Status field - // being cleared to 0. - // - MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AREA_REQ= UEST_COMMAND_READY); - Status =3D PtpCrbWaitRegisterBits ( - &CrbReg->CrbControlRequest, - 0, - PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY, - PTP_TIMEOUT_C - ); - if (EFI_ERROR (Status)) { - Status =3D EFI_DEVICE_ERROR; - goto GoIdle_Exit; - } + Status =3D PtpCrbWaitRegisterBits ( + &CrbReg->CrbControlStatus, + 0, + PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE, + PTP_TIMEOUT_C + ); + if (EFI_ERROR (Status)) { + RetryCnt++; + if (RetryCnt < RETRY_CNT_MAX) { + MmioWrite32 ((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AR= EA_REQUEST_GO_IDLE); + continue; + } else { + Status =3D EFI_DEVICE_ERROR; + goto GoIdle_Exit; + } + } =20 - Status =3D PtpCrbWaitRegisterBits ( - &CrbReg->CrbControlStatus, - 0, - PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE, - PTP_TIMEOUT_C - ); - if (EFI_ERROR (Status)) { - Status =3D EFI_DEVICE_ERROR; - goto GoIdle_Exit; + break; } =20 // --=20 2.26.2.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#91910): https://edk2.groups.io/g/devel/message/91910 Mute This Topic: https://groups.io/mt/92662206/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-