From nobody Fri Apr 26 23:24:13 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+91936+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+91936+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1658998381; cv=none; d=zohomail.com; s=zohoarc; b=WwaReQwYw/y+vizokAzXvR7Wdu1OXAKPz/Xo0ey9HLyi1eApZNV66JBrzOwSHIXEkqKl23peGKP4Y4BDzWMo81ibe5ZsEDw1Crr7fD1RmiKv00UlCFUuCmwk56e+elzNHAbV7xDUnzghZR2oxoWh4+zJk/rEjdTq1eGlN8uRWmQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658998381; 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=tG5p2pWKvDP71hVb88Tce3fgYtaouoc8u7non6B0Tcc=; b=QXvGhfnhNAa0aeF3r/CkkpLg4U68Q6bdkYYYhfr3vdFh7bY2gabula7v3f1SGZ1ahzpE2Fw3C5Q86PAWbuCKsdMwuUGjCKHJbi1uAlcqJKiioJTE1pb56APnfhAvLMZC0BFaA9LH2vrTR4o1LJEuaKKy9M95vn9CBjsMokAUwlU= 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+91936+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 1658998381676824.1831329751583; Thu, 28 Jul 2022 01:53:01 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id jidoYY1788612xMFKhDHeq9Q; Thu, 28 Jul 2022 01:53:01 -0700 X-Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mx.groups.io with SMTP id smtpd.web12.30186.1658998380264028532 for ; Thu, 28 Jul 2022 01:53:00 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10421"; a="289231060" X-IronPort-AV: E=Sophos;i="5.93,196,1654585200"; d="scan'208";a="289231060" X-Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jul 2022 01:52:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,196,1654585200"; d="scan'208";a="928197523" X-Received: from shwdesssddpdqi.ccr.corp.intel.com ([10.239.157.129]) by fmsmga005.fm.intel.com with ESMTP; 28 Jul 2022 01:52:41 -0700 From: "Qi Zhang" To: devel@edk2.groups.io Cc: Qi Zhang , Jiewen Yao , Jian J Wang , Swapnil Patil Subject: [edk2-devel] [PATCH v3] SecurityPkg: Add retry mechanism for tpm command Date: Thu, 28 Jul 2022 16:51:25 +0800 Message-Id: <20220728085125.2202-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: VFQfKtMbBuGrfD2jwWSB9Qckx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1658998381; bh=EjPlcVXMoFbTN4ntmebQUzr0G3lOsCb/gErVD5qv3lU=; h=Cc:Date:From:Reply-To:Subject:To; b=HyvBfaZ1VXN2HT9Seu0GpvHhUYh84V5+rUj4JtqEkmK8UDOYQmmQHO1MZM0iVTB0sfq l+tFmUiJEKloItzPh3XMD0//e+BLEJCm7hUJnI8bLO5tzOImqTG1Jnxk4zfzaELWmo5Sw SXAd4+e+pv5hhEZjmgkxbwjomRa5ZHxUxyc= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1658998382764100001 Content-Type: text/plain; charset="utf-8" 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 | 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 (#91936): https://edk2.groups.io/g/devel/message/91936 Mute This Topic: https://groups.io/mt/92666619/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-