From nobody Sat Apr 27 10:06:02 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+84960+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+84960+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1639634072; cv=none; d=zohomail.com; s=zohoarc; b=mqaM66REe+z5JccVmqSw4U5zkpa7skxAscOGmKHTlm/V26yi29nbwETm5j3KZNFZzXptHRWfAIePbvdPgYTw9r00qGNtMhAgEi1OTGJjRmtfopXO6a85gvsldVtvJmIiudrWWyRnJGE2KYWkl3ZOT6DZf1JIRp4A6tN3G9+OuBg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1639634072; 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=5lffiA3JKHwJAsS3zHk3ERloLSHoDl+Ilqb2WdcQJiQ=; b=Nu6plgkLDwbaIpm7pICsdB+LOM4423jd3oUj6Kf6ejmf3bskYyTxSndN41k/w+xkyJDQ5YLDCGEoaSvdQO6TdBEc5CHZZKmltlbJspK/AruoWoStxGMXaIN9YK0p2BQAJdmmmc5oJupBh+ZndYyYtF/dSpwfZBnS57X17u7ST7k= 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+84960+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 1639634072392996.4205167964031; Wed, 15 Dec 2021 21:54:32 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id JAhDYY1788612xuZThI7P7xS; Wed, 15 Dec 2021 21:54:32 -0800 X-Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web08.6613.1639634071081859473 for ; Wed, 15 Dec 2021 21:54:31 -0800 X-IronPort-AV: E=McAfee;i="6200,9189,10199"; a="226269251" X-IronPort-AV: E=Sophos;i="5.88,210,1635231600"; d="scan'208";a="226269251" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2021 21:54:27 -0800 X-IronPort-AV: E=Sophos;i="5.88,210,1635231600"; d="scan'208";a="568219570" X-Received: from kxinfux-win10.gar.corp.intel.com ([10.227.107.41]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2021 21:54:26 -0800 From: "VincentX Ke" To: devel@edk2.groups.io Cc: VincentX Ke , Hao A Wu , Ray Ni , Ian Chiu , Maggie Chu Subject: [edk2-devel] [PATCH] MdeModulePkg: Retry up to 5 times while sending UFS DME request to fix timing problem. Date: Thu, 16 Dec 2021 13:53:10 +0800 Message-Id: <20211216055310.16252-1-vincentx.ke@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,vincentx.ke@intel.com X-Gm-Message-State: FeAulucklzHdMpyS5Xa5PhA7x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1639634072; bh=PW6sPWA/kp40MQu5Yw/AIEoAOSrcfnzkPqmUSDfmV7Y=; h=Cc:Date:From:Reply-To:Subject:To; b=Pb/UI+9A+CXjU7mYXH10WWUOPhRr+TRp9qKliE7HONqkTQ1SVBBLDYG9X/OCiooIHuZ NdimHbIC0bOoq4r+jfFTcbv9JiCQhLThQSsGkd1cvUkJDVsWy4G3IdFipC+N8RwvgTGG9 7dJtFaHdNz6spaVdCJ3GfIV2k8i2/Vr0uqA= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1639634073274100001 Content-Type: text/plain; charset="utf-8" BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3775 Refactoring UFS DME request sending function and retry up to 5 times. Cc: Hao A Wu Cc: Ray Ni Cc: Ian Chiu Cc: Maggie Chu Signed-off-by: VincentX Ke --- MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c | 306 +++++++++++--------- 1 file changed, 176 insertions(+), 130 deletions(-) diff --git a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c b/MdeModulePkg/Bus= /Ufs/UfsBlockIoPei/UfsHci.c index 2baa57593e..9fa5fcf46f 100644 --- a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c +++ b/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c @@ -1,6 +1,6 @@ /** @file =20 - Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.
+ Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -747,60 +747,91 @@ UfsStopExecCmd ( } =20 /** - Read or write specified device descriptor of a UFS device. + Extracts return data from query response upiu. =20 - @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DAT= A data structure. - @param[in] Read The boolean variable to show r/w direction. - @param[in] DescId The ID of device descriptor. - @param[in] Index The Index of device descriptor. - @param[in] Selector The Selector of device descriptor. - @param[in, out] Descriptor The buffer of device descriptor to be read= or written. - @param[in] DescSize The size of device descriptor buffer. + @param[in] Packet Pointer to the UFS_DEVICE_MANAGEMENT_REQUE= ST_PACKET. + @param[in] QueryResp Pointer to the query response. =20 - @retval EFI_SUCCESS The device descriptor was read/written suc= cessfully. - @retval EFI_DEVICE_ERROR A device error occurred while attempting t= o r/w the device descriptor. - @retval EFI_TIMEOUT A timeout occurred while waiting for the c= ompletion of r/w the device descriptor. + @retval EFI_INVALID_PARAMETER Packet or QueryResp are empty or opcode is= invalid. + @retval EFI_DEVICE_ERROR Data returned from device is invalid. + @retval EFI_SUCCESS Data extracted. =20 **/ EFI_STATUS -UfsRwDeviceDesc ( - IN UFS_PEIM_HC_PRIVATE_DATA *Private, - IN BOOLEAN Read, - IN UINT8 DescId, - IN UINT8 Index, - IN UINT8 Selector, - IN OUT VOID *Descriptor, - IN UINT32 DescSize +UfsGetReturnDataFromQueryResponse ( + IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET *Packet, + IN UTP_QUERY_RESP_UPIU *QueryResp ) { - EFI_STATUS Status; - UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet; - UINT8 Slot; - UTP_TRD *Trd; - UINTN Address; - UTP_QUERY_RESP_UPIU *QueryResp; - UINT8 *CmdDescBase; - UINT32 CmdDescSize; - UINT16 ReturnDataSize; + UINT16 ReturnDataSize; =20 - ZeroMem (&Packet, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET)); + ReturnDataSize =3D 0; =20 - if (Read) { - Packet.DataDirection =3D UfsDataIn; - Packet.InDataBuffer =3D Descriptor; - Packet.InTransferLength =3D DescSize; - Packet.Opcode =3D UtpQueryFuncOpcodeRdDesc; - } else { - Packet.DataDirection =3D UfsDataOut; - Packet.OutDataBuffer =3D Descriptor; - Packet.OutTransferLength =3D DescSize; - Packet.Opcode =3D UtpQueryFuncOpcodeWrDesc; + if (Packet =3D=3D NULL || QueryResp =3D=3D NULL) { + return EFI_INVALID_PARAMETER; } =20 - Packet.DescId =3D DescId; - Packet.Index =3D Index; - Packet.Selector =3D Selector; - Packet.Timeout =3D UFS_TIMEOUT; + switch (Packet->Opcode) { + case UtpQueryFuncOpcodeRdDesc: + ReturnDataSize =3D QueryResp->Tsf.Length; + SwapLittleEndianToBigEndian ((UINT8 *) &ReturnDataSize, sizeof (UINT= 16)); + // + // Make sure the hardware device does not return more data than expe= cted. + // + if (ReturnDataSize > Packet->InTransferLength) { + return EFI_DEVICE_ERROR; + } + + CopyMem (Packet->InDataBuffer, (QueryResp + 1), ReturnDataSize); + Packet->InTransferLength =3D ReturnDataSize; + break; + case UtpQueryFuncOpcodeWrDesc: + ReturnDataSize =3D QueryResp->Tsf.Length; + SwapLittleEndianToBigEndian ((UINT8 *) &ReturnDataSize, sizeof (UINT= 16)); + Packet->OutTransferLength =3D ReturnDataSize; + break; + case UtpQueryFuncOpcodeRdFlag: + case UtpQueryFuncOpcodeSetFlag: + case UtpQueryFuncOpcodeClrFlag: + case UtpQueryFuncOpcodeTogFlag: + // + // The 'FLAG VALUE' field is at byte offset 3 of QueryResp->Tsf.Value + // + *((UINT8 *) (Packet->OutDataBuffer)) =3D *((UINT8 *) &(QueryResp->Ts= f.Value) + 3); + break; + default: + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + Creates Transfer Request descriptor and sends Query Request to the devic= e. + + @param[in] Private Pointer to the UFS_PEIM_HC_PRIVATE_DATA. + @param[in] Packet Pointer to the UFS_DEVICE_MANAGEMENT_REQUE= ST_PACKET. + + @retval EFI_SUCCESS The device descriptor was read/written suc= cessfully. + @retval EFI_INVALID_PARAMETER The DescId, Index and Selector fields in P= acket are invalid + combination to point to a type of UFS devi= ce descriptor. + @retval EFI_DEVICE_ERROR A device error occurred while attempting t= o r/w the device descriptor. + @retval EFI_TIMEOUT A timeout occurred while waiting for the c= ompletion of r/w the device descriptor. + +**/ +EFI_STATUS +UfsSendDmRequestRetry ( + IN UFS_PEIM_HC_PRIVATE_DATA *Private, + IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET *Packet + ) +{ + UINT8 Slot; + EFI_STATUS Status; + UTP_TRD *Trd; + UINTN Address; + UTP_QUERY_RESP_UPIU *QueryResp; + UINT8 *CmdDescBase; + UINT32 CmdDescSize; =20 // // Find out which slot of transfer request list is available. @@ -810,20 +841,21 @@ UfsRwDeviceDesc ( return Status; } =20 - Trd =3D ((UTP_TRD *)Private->UtpTrlBase) + Slot; + Trd =3D ((UTP_TRD*) Private->UtpTrlBase) + Slot; // // Fill transfer request descriptor to this slot. // - Status =3D UfsCreateDMCommandDesc (Private, &Packet, Trd); + Status =3D UfsCreateDMCommandDesc (Private, Packet, Trd); if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to create DM command descriptor\n")); return Status; } =20 // // Check the transfer request result. // - CmdDescBase =3D (UINT8 *)(UINTN)(LShiftU64 ((UINT64)Trd->UcdBaU, 32) | L= ShiftU64 ((UINT64)Trd->UcdBa, 7)); - QueryResp =3D (UTP_QUERY_RESP_UPIU *)(CmdDescBase + Trd->RuO * sizeof = (UINT32)); + CmdDescBase =3D (UINT8 *) (UINTN) (LShiftU64 ((UINT64) Trd->UcdBaU, 32) = | LShiftU64 ((UINT64) Trd->UcdBa, 7)); + QueryResp =3D (UTP_QUERY_RESP_UPIU *) (CmdDescBase + Trd->RuO * sizeof= (UINT32)); CmdDescSize =3D Trd->RuO * sizeof (UINT32) + Trd->RuL * sizeof (UINT32); =20 // @@ -835,43 +867,117 @@ UfsRwDeviceDesc ( // Wait for the completion of the transfer request. // Address =3D Private->UfsHcBase + UFS_HC_UTRLDBR_OFFSET; - Status =3D UfsWaitMemSet (Address, BIT0 << Slot, 0, Packet.Timeout); + Status =3D UfsWaitMemSet (Address, BIT0 << Slot, 0, Packet->Timeout); if (EFI_ERROR (Status)) { goto Exit; } =20 if (QueryResp->QueryResp !=3D 0) { + DEBUG ((DEBUG_ERROR, "Failed to send query request, OCS =3D %X, QueryR= esp =3D %X\n", Trd->Ocs, QueryResp->QueryResp)); DumpQueryResponseResult (QueryResp->QueryResp); Status =3D EFI_DEVICE_ERROR; goto Exit; } =20 - if (Trd->Ocs =3D=3D 0) { - ReturnDataSize =3D QueryResp->Tsf.Length; - SwapLittleEndianToBigEndian ((UINT8 *)&ReturnDataSize, sizeof (UINT16)= ); + Status =3D UfsGetReturnDataFromQueryResponse (Packet, QueryResp); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to get return data from query response\n"= )); + goto Exit; + } =20 - if (Read) { - // - // Make sure the hardware device does not return more data than expe= cted. - // - if (ReturnDataSize > Packet.InTransferLength) { - Status =3D EFI_DEVICE_ERROR; - goto Exit; - } +Exit: + UfsStopExecCmd (Private, Slot); + UfsPeimFreeMem (Private->Pool, CmdDescBase, CmdDescSize); =20 - CopyMem (Packet.InDataBuffer, (QueryResp + 1), ReturnDataSize); - Packet.InTransferLength =3D ReturnDataSize; - } else { - Packet.OutTransferLength =3D ReturnDataSize; + return Status; + +} + +/** + Sends Query Request to the device. Query is sent until device responds c= orrectly or counter runs out. + + @param[in] Private Pointer to the UFS_PEIM_HC_PRIVATE_DATA. + @param[in] Packet Pointer to the UFS_DEVICE_MANAGEMENT_REQUE= ST_PACKET. + + @retval EFI_SUCCESS The device responded correctly to the Quer= y request. + @retval EFI_INVALID_PARAMETER The DescId, Index and Selector fields in P= acket are invalid + combination to point to a type of UFS devi= ce descriptor. + @retval EFI_DEVICE_ERROR A device error occurred while waiting for = the response from the device. + @retval EFI_TIMEOUT A timeout occurred while waiting for the c= ompletion of the operation. + +**/ +EFI_STATUS +UfsSendDmRequest ( + IN UFS_PEIM_HC_PRIVATE_DATA *Private, + IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET *Packet + ) +{ + EFI_STATUS Status; + UINT8 Retry; + + Status =3D EFI_SUCCESS; + + for (Retry =3D 0; Retry < 5; Retry ++) { + Status =3D UfsSendDmRequestRetry (Private, Packet); + if (!EFI_ERROR (Status)) { + return EFI_SUCCESS; } + } + + DEBUG ((DEBUG_ERROR, "Failed to get response from the device after %d re= tries\n", Retry)); + return Status; +} + +/** + Read or write specified device descriptor of a UFS device. + + @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DAT= A data structure. + @param[in] Read The boolean variable to show r/w direction. + @param[in] DescId The ID of device descriptor. + @param[in] Index The Index of device descriptor. + @param[in] Selector The Selector of device descriptor. + @param[in, out] Descriptor The buffer of device descriptor to be read= or written. + @param[in] DescSize The size of device descriptor buffer. + + @retval EFI_SUCCESS The device descriptor was read/written suc= cessfully. + @retval EFI_DEVICE_ERROR A device error occurred while attempting t= o r/w the device descriptor. + @retval EFI_TIMEOUT A timeout occurred while waiting for the c= ompletion of r/w the device descriptor. + +**/ +EFI_STATUS +UfsRwDeviceDesc ( + IN UFS_PEIM_HC_PRIVATE_DATA *Private, + IN BOOLEAN Read, + IN UINT8 DescId, + IN UINT8 Index, + IN UINT8 Selector, + IN OUT VOID *Descriptor, + IN UINT32 DescSize + ) +{ + EFI_STATUS Status; + UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet; + + ZeroMem (&Packet, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET)); + + if (Read) { + Packet.DataDirection =3D UfsDataIn; + Packet.InDataBuffer =3D Descriptor; + Packet.InTransferLength =3D DescSize; + Packet.Opcode =3D UtpQueryFuncOpcodeRdDesc; } else { - Status =3D EFI_DEVICE_ERROR; + Packet.DataDirection =3D UfsDataOut; + Packet.OutDataBuffer =3D Descriptor; + Packet.OutTransferLength =3D DescSize; + Packet.Opcode =3D UtpQueryFuncOpcodeWrDesc; } =20 -Exit: - UfsStopExecCmd (Private, Slot); - UfsPeimFreeMem (Private->Pool, CmdDescBase, CmdDescSize); + Packet.DescId =3D DescId; + Packet.Index =3D Index; + Packet.Selector =3D Selector; + Packet.Timeout =3D UFS_TIMEOUT; =20 + Status =3D UfsSendDmRequest (Private, &Packet); return Status; } =20 @@ -898,12 +1004,6 @@ UfsRwFlags ( { EFI_STATUS Status; UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet; - UINT8 Slot; - UTP_TRD *Trd; - UINTN Address; - UTP_QUERY_RESP_UPIU *QueryResp; - UINT8 *CmdDescBase; - UINT32 CmdDescSize; =20 if (Value =3D=3D NULL) { return EFI_INVALID_PARAMETER; @@ -931,62 +1031,8 @@ UfsRwFlags ( Packet.Selector =3D 0; Packet.Timeout =3D UFS_TIMEOUT; =20 - // - // Find out which slot of transfer request list is available. - // - Status =3D UfsFindAvailableSlotInTrl (Private, &Slot); - if (EFI_ERROR (Status)) { - return Status; - } - - // - // Fill transfer request descriptor to this slot. - // - Trd =3D ((UTP_TRD *)Private->UtpTrlBase) + Slot; - Status =3D UfsCreateDMCommandDesc (Private, &Packet, Trd); - if (EFI_ERROR (Status)) { - return Status; - } - - // - // Check the transfer request result. - // - CmdDescBase =3D (UINT8 *)(UINTN)(LShiftU64 ((UINT64)Trd->UcdBaU, 32) | L= ShiftU64 ((UINT64)Trd->UcdBa, 7)); - QueryResp =3D (UTP_QUERY_RESP_UPIU *)(CmdDescBase + Trd->RuO * sizeof = (UINT32)); - CmdDescSize =3D Trd->RuO * sizeof (UINT32) + Trd->RuL * sizeof (UINT32); - - // - // Start to execute the transfer request. - // - UfsStartExecCmd (Private, Slot); - - // - // Wait for the completion of the transfer request. - // - Address =3D Private->UfsHcBase + UFS_HC_UTRLDBR_OFFSET; - Status =3D UfsWaitMemSet (Address, BIT0 << Slot, 0, Packet.Timeout); - if (EFI_ERROR (Status)) { - goto Exit; - } - - if (QueryResp->QueryResp !=3D 0) { - DumpQueryResponseResult (QueryResp->QueryResp); - Status =3D EFI_DEVICE_ERROR; - goto Exit; - } - - if (Trd->Ocs =3D=3D 0) { - // - // The 'FLAG VALUE' field is at byte offset 3 of QueryResp->Tsf.Value - // - *Value =3D *((UINT8 *)&(QueryResp->Tsf.Value) + 3); - } else { - Status =3D EFI_DEVICE_ERROR; - } - -Exit: - UfsStopExecCmd (Private, Slot); - UfsPeimFreeMem (Private->Pool, CmdDescBase, CmdDescSize); + Status =3D UfsSendDmRequest (Private, &Packet); + *Value =3D *((UINT8 *) (Packet.OutDataBuffer)); =20 return Status; } --=20 2.18.0.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 (#84960): https://edk2.groups.io/g/devel/message/84960 Mute This Topic: https://groups.io/mt/87761959/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-