From nobody Sun Apr 28 07:38:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+42705+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+42705+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1561130436; cv=none; d=zoho.com; s=zohoarc; b=F6qfVDPApjVb5KNEmL0+mu2qXqxb0xYP/mFJX9zda54QVyBXTCFVUgxNqebFRP+AP+hhjOj4r35NORVxyj2cM1245swgxExzWuoaFF0YuK/fwbnxPb9M/s8LjJqZ+GXKEqtB81DAPmmjlGj49bURGucFbXK5B8MysR74Wg6z8Ms= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561130436; h=Cc:Date:From:List-Id:List-Unsubscribe:Message-ID:Reply-To:Sender:Subject:To:ARC-Authentication-Results; bh=i4lA2eu6t3N0eufztUsokoSRnGWJBPq4X/Xz3DYTyzE=; b=Frobn+N1Fox4kX51c1yAGYx2h6YGXLSJUx2zrN4ud0hyODhHwJlMecC2J+GUGpFvNH8dWlDt9c3BsVXSmJn/s7s4q8pvFUpN/kZG/X1C9WuqlMZB6FihNv2yWqG8EwxYwOjKnCWsJNMAqg001ao9P+MFfBOekvGZDuhfpwZxjCQ= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+42705+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1561130436819778.3647865624882; Fri, 21 Jun 2019 08:20:36 -0700 (PDT) Return-Path: X-Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by groups.io with SMTP; Fri, 21 Jun 2019 08:20:35 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Jun 2019 08:20:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,400,1557212400"; d="scan'208";a="151281736" X-Received: from gklab-27-32.ger.corp.intel.com ([10.102.10.44]) by orsmga007.jf.intel.com with ESMTP; 21 Jun 2019 08:20:33 -0700 From: "Albecki, Mateusz" To: devel@edk2.groups.io Cc: Mateusz Albecki , Hao A Wu Subject: [edk2-devel] [PATCH] MdeModulePkg/UfsPassThruDxe: Fix unaligned data transfer handling Date: Fri, 21 Jun 2019 17:20:23 +0200 Message-Id: <20190621152023.324-1-mateusz.albecki@intel.com> Precedence: Bulk List-Unsubscribe: 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,mateusz.albecki@intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1561130436; bh=0CZ+f4GaSJinAim2VRquGbY2EkbO04rovmwaMlzXXQw=; h=Cc:Date:From:Reply-To:Subject:To; b=w8fLmTU005onN83UXLtNRZ3sVH0H0IPPflK93S77sy+rjK9dz3hAfo9XGtBZHzc0ewl o9UTB6j/Z4m1w5NImnP9Q4HyHkJNHJeqvwLNbaoX8UOI1nvPY+af1FySzaWFWHor31DYM M9LFzyYN8Y8COanAc5ptjGUzEScI3BsLhEk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1341 Since UFS specification requirs the data buffer specified in PRDT to be DWORD aligned in size we had a code in UfsInitUtpPrdt that aligned the data buffer by rounding down the buffer size to DWORD boundary. This meant that for SCSI commands that wanted to perform unaligned data transfer(such as SENSE command) we specified to small buffer for the data to fit and transfer was aborted. This change introduces code that allocates auxiliary DWORD aligned data buffer for unaligned transfer. Device transfers data to aligned buffer and when data transfer is over driver copies data from aligned buffer to data buffer passed by user. Cc: Hao A Wu Signed-off-by: Mateusz Albecki --- MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h | 2 + .../Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c | 194 +++++++++++++++--= ---- 2 files changed, 142 insertions(+), 54 deletions(-) diff --git a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h b/MdeModuleP= kg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h index 6c98b3a76a..9b68db5ffe 100644 --- a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h +++ b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h @@ -92,6 +92,8 @@ typedef struct { UINT32 CmdDescSize; VOID *CmdDescHost; VOID *CmdDescMapping; + VOID *AlignedDataBuf; + UINTN AlignedDataBufSize; VOID *DataBufMapping; =20 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet; diff --git a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c b/MdeModu= lePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c index 4b93821f38..6eb50f09ac 100644 --- a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c +++ b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c @@ -394,17 +394,13 @@ UfsInitUtpPrdt ( UINT8 *Remaining; UINTN PrdtNumber; =20 - if ((BufferSize & (BIT0 | BIT1)) !=3D 0) { - BufferSize &=3D ~(BIT0 | BIT1); - DEBUG ((DEBUG_WARN, "UfsInitUtpPrdt: The BufferSize [%d] is not dword-= aligned!\n", BufferSize)); - } + ASSERT (((UINTN)Buffer & (BIT0 | BIT1)) =3D=3D 0); + ASSERT ((BufferSize & (BIT1 | BIT0)) =3D=3D 0); =20 if (BufferSize =3D=3D 0) { return EFI_SUCCESS; } =20 - ASSERT (((UINTN)Buffer & (BIT0 | BIT1)) =3D=3D 0); - RemainingLen =3D BufferSize; Remaining =3D Buffer; PrdtNumber =3D (UINTN)DivU64x32 ((UINT64)BufferSize + UFS_MAX_DATA_LEN= _PER_PRD - 1, UFS_MAX_DATA_LEN_PER_PRD); @@ -1317,6 +1313,135 @@ Exit: return Status; } =20 +/** + Cleanup data buffers after data transfer. This function + also takes care to copy all data to user memory pool for + unaligned data transfers. + + @param[in] Private Pointer to the UFS_PASS_THRU_PRIVATE_DATA + @param[in] TransReq Pointer to the transfer request +**/ +VOID +UfsReconcileDataTransferBuffer ( + IN UFS_PASS_THRU_PRIVATE_DATA *Private, + IN UFS_PASS_THRU_TRANS_REQ *TransReq + ) +{ + if (TransReq->DataBufMapping !=3D NULL) { + Private->UfsHostController->Unmap ( + Private->UfsHostController, + TransReq->DataBufMapping + ); + } + + // + // Check if unaligned transfer was performed. If it was and we read + // data from device copy memory to user data buffers before cleanup. + // The assumption is if auxiliary aligned data buffer is not NULL then + // unaligned transfer has been performed. + // + if (TransReq->AlignedDataBuf !=3D NULL) { + if (TransReq->Packet->DataDirection =3D=3D EFI_EXT_SCSI_DATA_DIRECTION= _READ) { + CopyMem (TransReq->Packet->InDataBuffer, TransReq->AlignedDataBuf, T= ransReq->Packet->InTransferLength); + } + FreeAlignedPages (TransReq->AlignedDataBuf, EFI_SIZE_TO_PAGES (TransRe= q->AlignedDataBufSize)); + TransReq->AlignedDataBuf =3D NULL; + } +} + +/** + Prepare data buffer for transfer + + @param[in] Private Pointer to the UFS_PASS_THRU_PRIVATE_DATA + @param[in, out] TransReq Pointer to the transfer request + + @retval EFI_DEVICE_ERROR Failed to prepare buffer for transfer + @retval EFI_SUCCESS Buffer ready for transfer +**/ +EFI_STATUS +UfsPrepareDataTransferBuffer ( + IN UFS_PASS_THRU_PRIVATE_DATA *Private, + IN OUT UFS_PASS_THRU_TRANS_REQ *TransReq + ) +{ + EFI_STATUS Status; + VOID *DataBuf; + UINT32 DataLen; + UINTN MapLength; + EFI_PHYSICAL_ADDRESS DataBufPhyAddr; + EDKII_UFS_HOST_CONTROLLER_OPERATION Flag; + UTP_TR_PRD *PrdtBase; + + DataBufPhyAddr =3D (EFI_PHYSICAL_ADDRESS) NULL; + DataBuf =3D NULL; + + // + // For unaligned data transfers we allocate auxiliary DWORD aligned memo= ry pool. + // When command is finished auxiliary memory pool is copied into actual = user memory. + // This is requiered to assure data transfer safety(DWORD alignment requ= ired by UFS spec.) + // + if (TransReq->Packet->DataDirection =3D=3D EFI_EXT_SCSI_DATA_DIRECTION_R= EAD) { + if (((UINTN)TransReq->Packet->InDataBuffer % 4 !=3D 0) || (TransReq->P= acket->InTransferLength % 4 !=3D 0)) { + DataLen =3D TransReq->Packet->InTransferLength + (4 - (TransReq->Pac= ket->InTransferLength % 4)); + DataBuf =3D AllocateAlignedPages (EFI_SIZE_TO_PAGES (DataLen), 4); + if (DataBuf =3D=3D NULL) { + return EFI_DEVICE_ERROR; + } + ZeroMem (DataBuf, DataLen); + TransReq->AlignedDataBuf =3D DataBuf; + TransReq->AlignedDataBufSize =3D DataLen; + } else { + DataLen =3D TransReq->Packet->InTransferLength; + DataBuf =3D TransReq->Packet->InDataBuffer; + } + Flag =3D EdkiiUfsHcOperationBusMasterWrite; + } else { + if (((UINTN)TransReq->Packet->OutDataBuffer % 4 !=3D 0) || (TransReq->= Packet->OutTransferLength % 4 !=3D 0)) { + DataLen =3D TransReq->Packet->OutTransferLength + (4 - (TransReq->Pa= cket->OutTransferLength % 4)); + DataBuf =3D AllocateAlignedPages (EFI_SIZE_TO_PAGES (DataLen), 4); + if (DataBuf =3D=3D NULL) { + return EFI_DEVICE_ERROR; + } + CopyMem (DataBuf, TransReq->Packet->OutDataBuffer, TransReq->Packet-= >OutTransferLength); + TransReq->AlignedDataBuf =3D DataBuf; + TransReq->AlignedDataBufSize =3D DataLen; + } else { + DataLen =3D TransReq->Packet->OutTransferLength; + DataBuf =3D TransReq->Packet->OutDataBuffer; + } + Flag =3D EdkiiUfsHcOperationBusMasterRead; + } + + if (DataLen !=3D 0) { + MapLength =3D DataLen; + Status =3D Private->UfsHostController->Map ( + Private->UfsHostController, + Flag, + DataBuf, + &MapLength, + &DataBufPhyAddr, + &TransReq->DataBufMapping + ); + + if (EFI_ERROR (Status) || (DataLen !=3D MapLength)) { + if (TransReq->AlignedDataBuf !=3D NULL) { + FreePages (TransReq->AlignedDataBuf, EFI_SIZE_TO_PAGES (TransReq->= AlignedDataBufSize)); + TransReq->AlignedDataBuf =3D NULL; + } + return EFI_DEVICE_ERROR; + } + } + + // + // Fill PRDT table of Command UPIU for executed SCSI cmd. + // + PrdtBase =3D (UTP_TR_PRD*)((UINT8*)TransReq->CmdDescHost + ROUNDUP8 (siz= eof (UTP_COMMAND_UPIU)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU))); + ASSERT (PrdtBase !=3D NULL); + UfsInitUtpPrdt (PrdtBase, (VOID*)(UINTN)DataBufPhyAddr, DataLen); + + return EFI_SUCCESS; +} + /** Sends a UFS-supported SCSI Request Packet to a UFS device that is attach= ed to the UFS host controller. =20 @@ -1353,24 +1478,19 @@ UfsExecScsiCmds ( UTP_RESPONSE_UPIU *Response; UINT16 SenseDataLen; UINT32 ResTranCount; - VOID *DataBuf; - EFI_PHYSICAL_ADDRESS DataBufPhyAddr; - UINT32 DataLen; - UINTN MapLength; - EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc; - EDKII_UFS_HOST_CONTROLLER_OPERATION Flag; - UTP_TR_PRD *PrdtBase; EFI_TPL OldTpl; UFS_PASS_THRU_TRANS_REQ *TransReq; + EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc; =20 - TransReq =3D AllocateZeroPool (sizeof (UFS_PASS_THRU_TRANS_REQ)); + TransReq =3D AllocateZeroPool (sizeof (UFS_PASS_THRU_TRANS_REQ)); if (TransReq =3D=3D NULL) { return EFI_OUT_OF_RESOURCES; } =20 TransReq->Signature =3D UFS_PASS_THRU_TRANS_REQ_SIG; TransReq->TimeoutRemain =3D Packet->Timeout; - DataBufPhyAddr =3D 0; + TransReq->Packet =3D Packet; + UfsHc =3D Private->UfsHostController; // // Find out which slot of transfer request list is available. @@ -1399,44 +1519,16 @@ UfsExecScsiCmds ( =20 TransReq->CmdDescSize =3D TransReq->Trd->PrdtO * sizeof (UINT32) + Trans= Req->Trd->PrdtL * sizeof (UTP_TR_PRD); =20 - if (Packet->DataDirection =3D=3D EFI_EXT_SCSI_DATA_DIRECTION_READ) { - DataBuf =3D Packet->InDataBuffer; - DataLen =3D Packet->InTransferLength; - Flag =3D EdkiiUfsHcOperationBusMasterWrite; - } else { - DataBuf =3D Packet->OutDataBuffer; - DataLen =3D Packet->OutTransferLength; - Flag =3D EdkiiUfsHcOperationBusMasterRead; - } - - if (DataLen !=3D 0) { - MapLength =3D DataLen; - Status =3D UfsHc->Map ( - UfsHc, - Flag, - DataBuf, - &MapLength, - &DataBufPhyAddr, - &TransReq->DataBufMapping - ); - - if (EFI_ERROR (Status) || (DataLen !=3D MapLength)) { - goto Exit1; - } + Status =3D UfsPrepareDataTransferBuffer (Private, TransReq); + if (EFI_ERROR (Status)) { + goto Exit1; } - // - // Fill PRDT table of Command UPIU for executed SCSI cmd. - // - PrdtBase =3D (UTP_TR_PRD*)((UINT8*)TransReq->CmdDescHost + ROUNDUP8 (siz= eof (UTP_COMMAND_UPIU)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU))); - ASSERT (PrdtBase !=3D NULL); - UfsInitUtpPrdt (PrdtBase, (VOID*)(UINTN)DataBufPhyAddr, DataLen); =20 // // Insert the async SCSI cmd to the Async I/O list // if (Event !=3D NULL) { OldTpl =3D gBS->RaiseTPL (TPL_NOTIFY); - TransReq->Packet =3D Packet; TransReq->CallerEvent =3D Event; InsertTailList (&Private->Queue, &TransReq->TransferList); gBS->RestoreTPL (OldTpl); @@ -1515,9 +1607,7 @@ Exit: =20 UfsStopExecCmd (Private, TransReq->Slot); =20 - if (TransReq->DataBufMapping !=3D NULL) { - UfsHc->Unmap (UfsHc, TransReq->DataBufMapping); - } + UfsReconcileDataTransferBuffer (Private, TransReq); =20 Exit1: if (TransReq->CmdDescMapping !=3D NULL) { @@ -1532,7 +1622,6 @@ Exit1: return Status; } =20 - /** Sent UIC DME_LINKSTARTUP command to start the link startup procedure. =20 @@ -2100,7 +2189,6 @@ UfsControllerStop ( return EFI_SUCCESS; } =20 - /** Internal helper function which will signal the caller event and clean up resources. @@ -2132,9 +2220,7 @@ SignalCallerEvent ( =20 UfsStopExecCmd (Private, TransReq->Slot); =20 - if (TransReq->DataBufMapping !=3D NULL) { - UfsHc->Unmap (UfsHc, TransReq->DataBufMapping); - } + UfsReconcileDataTransferBuffer (Private, TransReq); =20 if (TransReq->CmdDescMapping !=3D NULL) { UfsHc->Unmap (UfsHc, TransReq->CmdDescMapping); --=20 2.14.1.windows.1 -------------------------------------------------------------------- Intel Technology Poland sp. z o.o. ul. Slowackiego 173 | 80-298 Gdansk | Sad Rejonowy Gdansk Polnoc | VII Wydz= ial Gospodarczy Krajowego Rejestru Sadowego - KRS 101882 | NIP 957-07-52-31= 6 | Kapital zakladowy 200.000 PLN. Ta wiadomosc wraz z zalacznikami jest przeznaczona dla okreslonego adresata= i moze zawierac informacje poufne. W razie przypadkowego otrzymania tej wi= adomosci, prosimy o powiadomienie nadawcy oraz trwale jej usuniecie; jakiek= olwiek przegladanie lub rozpowszechnianie jest zabronione. This e-mail and any attachments may contain confidential material for the s= ole use of the intended recipient(s). If you are not the intended recipient= , please contact the sender and delete all copies; any review or distributi= on by others is strictly prohibited. -=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 (#42705): https://edk2.groups.io/g/devel/message/42705 Mute This Topic: https://groups.io/mt/32159485/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-