From nobody Tue Nov 26 09:36:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+56546+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56546+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=oracle.com ARC-Seal: i=1; a=rsa-sha256; t=1585425548; cv=none; d=zohomail.com; s=zohoarc; b=WzatFsU+0ggoCANj8MmNXn9Krdkixl0dtARWrFU5tgEPWjW5gqzKYU2SKzOWv39gCKS6KOu1yxhCVd4AwVwlUIxebFdCH1OYU4WBgQzTIM41OiHTiCcnV2fpzFqlSje8spmlcp8QhYn9evfeJK2LiLFAtiCAqVX4dWpcCPkgoik= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585425548; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=tky7p+KHo0mZlAjvldK7yaXBRc3QnTS+C+lqbOiD60A=; b=XmfHP+KRn6lb2lO6Yp0iajtDUSkQDX+KdSaigLNkfM3rgNWtTroj4G7cCV/pYB+UnyWWtGQsE3B89k6Co5W7KmPAE9juwthSMmWkCa65WjI/kt0mkpHTEVMBrq41UVPnPjMnOfXyTvEkYknRqOJ05CRgOBnLIEDBctdL85VbXwE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+56546+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 1585425548271468.7206499053983; Sat, 28 Mar 2020 12:59:08 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id SwaDYY1788612xHMkrrGLilk; Sat, 28 Mar 2020 12:59:07 -0700 X-Received: from userp2130.oracle.com (userp2130.oracle.com [156.151.31.86]) by mx.groups.io with SMTP id smtpd.web11.14083.1585425547328295934 for ; Sat, 28 Mar 2020 12:59:07 -0700 X-Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 02SJnKOS013982; Sat, 28 Mar 2020 19:59:07 GMT X-Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by userp2130.oracle.com with ESMTP id 301x0qsj8r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 28 Mar 2020 19:59:06 +0000 X-Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 02SJom9R032737; Sat, 28 Mar 2020 19:59:06 GMT X-Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3030.oracle.com with ESMTP id 301vm90q8p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 28 Mar 2020 19:59:06 +0000 X-Received: from abhmp0017.oracle.com (abhmp0017.oracle.com [141.146.116.23]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 02SJx5qj025080; Sat, 28 Mar 2020 19:59:05 GMT X-Received: from spark.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 28 Mar 2020 12:59:05 -0700 From: "Liran Alon" To: devel@edk2.groups.io, lersek@redhat.com Cc: nikita.leshchenko@oracle.com, aaron.young@oracle.com, jordan.l.justen@intel.com, ard.biesheuvel@linaro.org, Liran Alon Subject: [edk2-devel] [PATCH v3 13/17] OvmfPkg/PvScsiDxe: Setup requests and completions rings Date: Sat, 28 Mar 2020 23:00:56 +0300 Message-Id: <20200328200100.60786-14-liran.alon@oracle.com> In-Reply-To: <20200328200100.60786-1-liran.alon@oracle.com> References: <20200328200100.60786-1-liran.alon@oracle.com> MIME-Version: 1.0 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,liran.alon@oracle.com X-Gm-Message-State: qYoduWkUORBSM2wwr2Io4dtOx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1585425547; bh=bA0CQNqiRTjv36h8nZ/w+6e5shN95qOuMmUSskDfgiI=; h=Cc:Date:From:Reply-To:Subject:To; b=H05z/a0v0FKimnTWSKTfOSaJEyNPtyT68YT4kigmWKiz+vtjGGbubPE20AE/7EjFXIz W5g8QTHbgL+vNghl5VInTKNadgPivAnZn2wRLxuV3AFHp7Z8MiEXcuAFt5d194CHqvqKG riy/Ozp/rWHwaoWlE8zwtgyFZW2ZGkElnzk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" These rings are shared memory buffers between host and device in which a cyclic buffer is managed to send request descriptors from host to device and receive completion descriptors from device to host. Note that because device may be constrained by IOMMU or guest may be run under AMD SEV, we make sure to map these rings to device by using PciIo->Map(). Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2567 Signed-off-by: Liran Alon Reviewed-by: Laszlo Ersek --- OvmfPkg/PvScsiDxe/PvScsi.c | 219 ++++++++++++++++++++++++++++++++ OvmfPkg/PvScsiDxe/PvScsi.h | 17 +++ OvmfPkg/PvScsiDxe/PvScsiDxe.inf | 1 + 3 files changed, 237 insertions(+) diff --git a/OvmfPkg/PvScsiDxe/PvScsi.c b/OvmfPkg/PvScsiDxe/PvScsi.c index cf75884350ee..c7d367e83a2d 100644 --- a/OvmfPkg/PvScsiDxe/PvScsi.c +++ b/OvmfPkg/PvScsiDxe/PvScsi.c @@ -11,11 +11,13 @@ =20 #include #include +#include #include #include #include #include #include +#include #include =20 #include "PvScsi.h" @@ -436,6 +438,207 @@ PvScsiRestorePciAttributes ( ); } =20 +STATIC +EFI_STATUS +PvScsiAllocateSharedPages ( + IN PVSCSI_DEV *Dev, + IN UINTN Pages, + OUT VOID **HostAddress, + OUT PVSCSI_DMA_DESC *DmaDesc + ) +{ + EFI_STATUS Status; + UINTN NumberOfBytes; + + Status =3D Dev->PciIo->AllocateBuffer ( + Dev->PciIo, + AllocateAnyPages, + EfiBootServicesData, + Pages, + HostAddress, + EFI_PCI_ATTRIBUTE_MEMORY_CACHED + ); + if (EFI_ERROR (Status)) { + return Status; + } + + NumberOfBytes =3D EFI_PAGES_TO_SIZE (Pages); + Status =3D Dev->PciIo->Map ( + Dev->PciIo, + EfiPciIoOperationBusMasterCommonBuffer, + *HostAddress, + &NumberOfBytes, + &DmaDesc->DeviceAddress, + &DmaDesc->Mapping + ); + if (EFI_ERROR (Status)) { + goto FreeBuffer; + } + + if (NumberOfBytes !=3D EFI_PAGES_TO_SIZE (Pages)) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Unmap; + } + + return EFI_SUCCESS; + +Unmap: + Dev->PciIo->Unmap (Dev->PciIo, DmaDesc->Mapping); + +FreeBuffer: + Dev->PciIo->FreeBuffer (Dev->PciIo, Pages, *HostAddress); + + return Status; +} + +STATIC +VOID +PvScsiFreeSharedPages ( + IN PVSCSI_DEV *Dev, + IN UINTN Pages, + IN VOID *HostAddress, + IN PVSCSI_DMA_DESC *DmaDesc + ) +{ + Dev->PciIo->Unmap (Dev->PciIo, DmaDesc->Mapping); + Dev->PciIo->FreeBuffer (Dev->PciIo, Pages, HostAddress); +} + +STATIC +EFI_STATUS +PvScsiInitRings ( + IN OUT PVSCSI_DEV *Dev + ) +{ + EFI_STATUS Status; + union { + PVSCSI_CMD_DESC_SETUP_RINGS Cmd; + UINT32 Uint32; + } AlignedCmd; + PVSCSI_CMD_DESC_SETUP_RINGS *Cmd; + + Cmd =3D &AlignedCmd.Cmd; + + Status =3D PvScsiAllocateSharedPages ( + Dev, + 1, + (VOID **)&Dev->RingDesc.RingState, + &Dev->RingDesc.RingStateDmaDesc + ); + if (EFI_ERROR (Status)) { + return Status; + } + ZeroMem (Dev->RingDesc.RingState, EFI_PAGE_SIZE); + + Status =3D PvScsiAllocateSharedPages ( + Dev, + 1, + (VOID **)&Dev->RingDesc.RingReqs, + &Dev->RingDesc.RingReqsDmaDesc + ); + if (EFI_ERROR (Status)) { + goto FreeRingState; + } + ZeroMem (Dev->RingDesc.RingReqs, EFI_PAGE_SIZE); + + Status =3D PvScsiAllocateSharedPages ( + Dev, + 1, + (VOID **)&Dev->RingDesc.RingCmps, + &Dev->RingDesc.RingCmpsDmaDesc + ); + if (EFI_ERROR (Status)) { + goto FreeRingReqs; + } + ZeroMem (Dev->RingDesc.RingCmps, EFI_PAGE_SIZE); + + ZeroMem (Cmd, sizeof (*Cmd)); + Cmd->ReqRingNumPages =3D 1; + Cmd->CmpRingNumPages =3D 1; + Cmd->RingsStatePPN =3D RShiftU64 ( + Dev->RingDesc.RingStateDmaDesc.DeviceAddress, + EFI_PAGE_SHIFT + ); + Cmd->ReqRingPPNs[0] =3D RShiftU64 ( + Dev->RingDesc.RingReqsDmaDesc.DeviceAddress, + EFI_PAGE_SHIFT + ); + Cmd->CmpRingPPNs[0] =3D RShiftU64 ( + Dev->RingDesc.RingCmpsDmaDesc.DeviceAddress, + EFI_PAGE_SHIFT + ); + + STATIC_ASSERT ( + sizeof (*Cmd) % sizeof (UINT32) =3D=3D 0, + "Cmd must be multiple of 32-bit words" + ); + Status =3D PvScsiWriteCmdDesc ( + Dev, + PvScsiCmdSetupRings, + (UINT32 *)Cmd, + sizeof (*Cmd) / sizeof (UINT32) + ); + if (EFI_ERROR (Status)) { + goto FreeRingCmps; + } + + return EFI_SUCCESS; + +FreeRingCmps: + PvScsiFreeSharedPages ( + Dev, + 1, + Dev->RingDesc.RingCmps, + &Dev->RingDesc.RingCmpsDmaDesc + ); + +FreeRingReqs: + PvScsiFreeSharedPages ( + Dev, + 1, + Dev->RingDesc.RingReqs, + &Dev->RingDesc.RingReqsDmaDesc + ); + +FreeRingState: + PvScsiFreeSharedPages ( + Dev, + 1, + Dev->RingDesc.RingState, + &Dev->RingDesc.RingStateDmaDesc + ); + + return Status; +} + +STATIC +VOID +PvScsiFreeRings ( + IN OUT PVSCSI_DEV *Dev + ) +{ + PvScsiFreeSharedPages ( + Dev, + 1, + Dev->RingDesc.RingCmps, + &Dev->RingDesc.RingCmpsDmaDesc + ); + + PvScsiFreeSharedPages ( + Dev, + 1, + Dev->RingDesc.RingReqs, + &Dev->RingDesc.RingReqsDmaDesc + ); + + PvScsiFreeSharedPages ( + Dev, + 1, + Dev->RingDesc.RingState, + &Dev->RingDesc.RingStateDmaDesc + ); +} + STATIC EFI_STATUS PvScsiInit ( @@ -466,6 +669,14 @@ PvScsiInit ( goto RestorePciAttributes; } =20 + // + // Init PVSCSI rings + // + Status =3D PvScsiInitRings (Dev); + if (EFI_ERROR (Status)) { + goto RestorePciAttributes; + } + // // Populate the exported interface's attributes // @@ -509,6 +720,14 @@ PvScsiUninit ( IN OUT PVSCSI_DEV *Dev ) { + // + // Reset device to stop device usage of the rings. + // This is required to safely free the rings. + // + PvScsiResetAdapter (Dev); + + PvScsiFreeRings (Dev); + PvScsiRestorePciAttributes (Dev); } =20 diff --git a/OvmfPkg/PvScsiDxe/PvScsi.h b/OvmfPkg/PvScsiDxe/PvScsi.h index 5f611dbbc98c..6d23b6e1eccf 100644 --- a/OvmfPkg/PvScsiDxe/PvScsi.h +++ b/OvmfPkg/PvScsiDxe/PvScsi.h @@ -15,12 +15,29 @@ #include #include =20 +typedef struct { + EFI_PHYSICAL_ADDRESS DeviceAddress; + VOID *Mapping; +} PVSCSI_DMA_DESC; + +typedef struct { + PVSCSI_RINGS_STATE *RingState; + PVSCSI_DMA_DESC RingStateDmaDesc; + + PVSCSI_RING_REQ_DESC *RingReqs; + PVSCSI_DMA_DESC RingReqsDmaDesc; + + PVSCSI_RING_CMP_DESC *RingCmps; + PVSCSI_DMA_DESC RingCmpsDmaDesc; +} PVSCSI_RING_DESC; + #define PVSCSI_SIG SIGNATURE_32 ('P', 'S', 'C', 'S') =20 typedef struct { UINT32 Signature; EFI_PCI_IO_PROTOCOL *PciIo; UINT64 OriginalPciAttributes; + PVSCSI_RING_DESC RingDesc; UINT8 MaxTarget; UINT8 MaxLun; EFI_EXT_SCSI_PASS_THRU_PROTOCOL PassThru; diff --git a/OvmfPkg/PvScsiDxe/PvScsiDxe.inf b/OvmfPkg/PvScsiDxe/PvScsiDxe.= inf index fcffc90d46c8..6200533698fc 100644 --- a/OvmfPkg/PvScsiDxe/PvScsiDxe.inf +++ b/OvmfPkg/PvScsiDxe/PvScsiDxe.inf @@ -26,6 +26,7 @@ OvmfPkg/OvmfPkg.dec =20 [LibraryClasses] + BaseLib BaseMemoryLib DebugLib MemoryAllocationLib --=20 2.20.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 (#56546): https://edk2.groups.io/g/devel/message/56546 Mute This Topic: https://groups.io/mt/72617147/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-