From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from userp2120.oracle.com (userp2120.oracle.com [156.151.31.85]) by mx.groups.io with SMTP id smtpd.web10.69877.1584370958442879360 for ; Mon, 16 Mar 2020 08:02:38 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=V1jxNJn+; spf=pass (domain: oracle.com, ip: 156.151.31.85, mailfrom: liran.alon@oracle.com) Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 02GErlJ7156949; Mon, 16 Mar 2020 15:02:38 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=r4An+1SemKQKclrZ/r4kR3yWW9JuTGr1ayniv77oQXU=; b=V1jxNJn+6Bx1UShx5tF0t8bQ1Z7MhCcza5AXYt+y/IP9NibfwtDpLoCeDNHlgoGZCZoW ESgp7ATcSqOq9fwaXSZP1fNSafxtlUchF5cWI55CC7hu9lfKxoEvffuetwDNn4QaHcnu Mfy487Z0X6FOw3yLKrrhWZZmsDmc/vNx3TP8sCoKjcb21i9ZwMW7c5lFzoRQzoN3D0vR 3tTPLiwD57mIf839gpT5E6B+yqReMbknnrtWJDyT2eOKpPVa891Gd0j/fLu6VT8+GPe/ 1TIB2PNSejaoTUTzXxrmtLnI8rx8PTE8U6LIITB31ziIWJOXWiAFKqDVV3iQeXA6B4mU Gg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by userp2120.oracle.com with ESMTP id 2yrqwmyayk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 16 Mar 2020 15:02:36 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 02GEqWR7173329; Mon, 16 Mar 2020 15:00:36 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userp3020.oracle.com with ESMTP id 2ys8yvyjjg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 16 Mar 2020 15:00:36 +0000 Received: from abhmp0009.oracle.com (abhmp0009.oracle.com [141.146.116.15]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 02GF0ZwC011674; Mon, 16 Mar 2020 15:00:35 GMT Received: from spark.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 16 Mar 2020 08:00:35 -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: [PATCH 13/17] OvmfPkg/PvScsiDxe: Setup requests and completions rings Date: Mon, 16 Mar 2020 17:01:09 +0200 Message-Id: <20200316150113.104630-14-liran.alon@oracle.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200316150113.104630-1-liran.alon@oracle.com> References: <20200316150113.104630-1-liran.alon@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9561 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 bulkscore=0 malwarescore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2003160072 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9561 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 lowpriorityscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 phishscore=0 spamscore=0 suspectscore=0 malwarescore=0 priorityscore=1501 clxscore=1015 adultscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2003160072 Content-Transfer-Encoding: 8bit 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=2567 Reviewed-by: Nikita Leshenko Signed-off-by: Liran Alon --- OvmfPkg/PvScsiDxe/PvScsi.c | 235 +++++++++++++++++++++++++++++++++++++ OvmfPkg/PvScsiDxe/PvScsi.h | 17 +++ 2 files changed, 252 insertions(+) diff --git a/OvmfPkg/PvScsiDxe/PvScsi.c b/OvmfPkg/PvScsiDxe/PvScsi.c index fb2407d2adb2..c3f5d38f3d30 100644 --- a/OvmfPkg/PvScsiDxe/PvScsi.c +++ b/OvmfPkg/PvScsiDxe/PvScsi.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "PvScsi.h" @@ -396,6 +397,209 @@ PvScsiSetPCIAttributes ( return EFI_SUCCESS; } +STATIC +EFI_STATUS +PvScsiAllocatePages ( + IN PVSCSI_DEV *Dev, + IN UINTN Pages, + IN OUT VOID **HostAddress + ) +{ + return Dev->PciIo->AllocateBuffer ( + Dev->PciIo, + AllocateAnyPages, + EfiBootServicesData, + Pages, + HostAddress, + EFI_PCI_ATTRIBUTE_MEMORY_CACHED + ); +} + +STATIC +VOID +PvScsiFreePages ( + IN PVSCSI_DEV *Dev, + IN UINTN Pages, + IN VOID *HostAddress + ) +{ + Dev->PciIo->FreeBuffer ( + Dev->PciIo, + Pages, + HostAddress + ); +} + +STATIC +EFI_STATUS +PvScsiMapBuffer ( + IN PVSCSI_DEV *Dev, + IN EFI_PCI_IO_PROTOCOL_OPERATION PciIoOperation, + IN VOID *HostAddress, + IN UINTN NumberOfBytes, + OUT PVSCSI_DMA_DESC *DmaDesc + ) +{ + EFI_STATUS Status; + UINTN BytesMapped; + + BytesMapped = NumberOfBytes; + Status = Dev->PciIo->Map ( + Dev->PciIo, + PciIoOperation, + HostAddress, + &BytesMapped, + &DmaDesc->DeviceAddress, + &DmaDesc->Mapping + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (BytesMapped != NumberOfBytes) { + Status = EFI_OUT_OF_RESOURCES; + goto Unmap; + } + + return EFI_SUCCESS; + +Unmap: + Dev->PciIo->Unmap (Dev->PciIo, DmaDesc->Mapping); + DmaDesc->Mapping = NULL; + + return Status; +} + +STATIC +VOID +PvScsiUnmapBuffer ( + IN PVSCSI_DEV *Dev, + IN OUT PVSCSI_DMA_DESC *DmaDesc) +{ + Dev->PciIo->Unmap (Dev->PciIo, DmaDesc->Mapping); +} + +STATIC +EFI_STATUS +PvScsiAllocateSharedPages ( + IN PVSCSI_DEV *Dev, + IN UINTN Pages, + IN EFI_PCI_IO_PROTOCOL_OPERATION PciIoOperation, + OUT VOID **HostAddress, + OUT PVSCSI_DMA_DESC *DmaDesc + ) +{ + EFI_STATUS Status; + + *HostAddress = NULL; + DmaDesc->Mapping = NULL; + + Status = PvScsiAllocatePages (Dev, Pages, HostAddress); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = PvScsiMapBuffer ( + Dev, + PciIoOperation, + *HostAddress, + Pages * EFI_PAGE_SIZE, + DmaDesc + ); + if (EFI_ERROR (Status)) { + goto FreePages; + } + + return EFI_SUCCESS; + +FreePages: + PvScsiFreePages (Dev, Pages, *HostAddress); + *HostAddress = NULL; + + return Status; +} + +STATIC +VOID +PvScsiFreeSharedPages ( + IN PVSCSI_DEV *Dev, + IN UINTN Pages, + IN OUT VOID **HostAddress, + IN OUT PVSCSI_DMA_DESC *DmaDesc + ) +{ + if (*HostAddress) { + if (DmaDesc->Mapping) { + PvScsiUnmapBuffer (Dev, DmaDesc); + DmaDesc->Mapping = NULL; + } + + PvScsiFreePages (Dev, Pages, *HostAddress); + *HostAddress = NULL; + } +} + +STATIC +EFI_STATUS +PvScsiInitRings ( + IN OUT PVSCSI_DEV *Dev + ) +{ + EFI_STATUS Status; + PVSCSI_CMD_DESC_SETUP_RINGS Cmd; + + Status = PvScsiAllocateSharedPages ( + Dev, + 1, + EfiPciIoOperationBusMasterCommonBuffer, + (VOID **)&Dev->RingDesc.RingState, + &Dev->RingDesc.RingStateDmaDesc + ); + if (EFI_ERROR (Status)) { + return Status; + } + ZeroMem (Dev->RingDesc.RingState, EFI_PAGE_SIZE); + + Status = PvScsiAllocateSharedPages ( + Dev, + 1, + EfiPciIoOperationBusMasterCommonBuffer, + (VOID **)&Dev->RingDesc.RingReqs, + &Dev->RingDesc.RingReqsDmaDesc + ); + if (EFI_ERROR (Status)) { + return Status; + } + ZeroMem (Dev->RingDesc.RingReqs, EFI_PAGE_SIZE); + + Status = PvScsiAllocateSharedPages ( + Dev, + 1, + EfiPciIoOperationBusMasterCommonBuffer, + (VOID **)&Dev->RingDesc.RingCmps, + &Dev->RingDesc.RingCmpsDmaDesc + ); + if (EFI_ERROR (Status)) { + return Status; + } + ZeroMem (Dev->RingDesc.RingCmps, EFI_PAGE_SIZE); + + ZeroMem (&Cmd, sizeof Cmd); + Cmd.ReqRingNumPages = 1; + Cmd.CmpRingNumPages = 1; + Cmd.RingsStatePPN = + ((UINT64) Dev->RingDesc.RingStateDmaDesc.DeviceAddress) >> + EFI_PAGE_SHIFT; + Cmd.ReqRingPPNs[0] = + ((UINT64) Dev->RingDesc.RingReqsDmaDesc.DeviceAddress) >> + EFI_PAGE_SHIFT; + Cmd.CmpRingPPNs[0] = + ((UINT64) Dev->RingDesc.RingCmpsDmaDesc.DeviceAddress) >> + EFI_PAGE_SHIFT; + + return PvScsiWriteCmdDesc(Dev, PVSCSI_CMD_SETUP_RINGS, &Cmd, sizeof Cmd); +} + STATIC EFI_STATUS PvScsiInit ( @@ -425,6 +629,15 @@ PvScsiInit ( if (EFI_ERROR (Status)) { return Status; } + + // + // Init PVSCSI rings + // + Status = PvScsiInitRings (Dev); + if (EFI_ERROR (Status)) { + return Status; + } + // // Populate the exported interface's attributes // @@ -463,6 +676,28 @@ PvScsiUninit ( IN OUT PVSCSI_DEV *Dev ) { + // + // Free PVSCSI rings + // + PvScsiFreeSharedPages ( + Dev, + 1, + (VOID **)&Dev->RingDesc.RingCmps, + &Dev->RingDesc.RingCmpsDmaDesc + ); + PvScsiFreeSharedPages ( + Dev, + 1, + (VOID **)&Dev->RingDesc.RingReqs, + &Dev->RingDesc.RingReqsDmaDesc + ); + PvScsiFreeSharedPages ( + Dev, + 1, + (VOID **)&Dev->RingDesc.RingState, + &Dev->RingDesc.RingStateDmaDesc + ); + // // Restore PCI Attributes // 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 +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') 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; -- 2.20.1