From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM02-BL2-obe.outbound.protection.outlook.com (mail-bl2nam02on0084.outbound.protection.outlook.com [104.47.38.84]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B8D5621E47D5B for ; Sun, 27 Aug 2017 17:32:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=lQ0+Hfnqb3MVS8fhJ9NyuODZqnshESI5S0ihmR1bZrc=; b=m6V77gz6NttQM6G36N0Lglzkki8zmv1RPEXoztNvmvdF6cwnCpw2qO31OhsvtyRLjlg5MpjC+CLxjPYNTRXBAp2t8hYZoaeo++E5INIZJb82diwyraMxAW3PsmXTxxuid/x8NRnFRN9hNuX6VANBrICYff2jhZZgd8lJIoYWhKM= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from brijesh-build-machine.amd.com (165.204.77.1) by CY1PR12MB0152.namprd12.prod.outlook.com (10.161.173.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.1385.9; Mon, 28 Aug 2017 00:35:02 +0000 From: Brijesh Singh To: edk2-devel@lists.01.org Cc: Brijesh Singh , Ard Biesheuvel , Jordan Justen , Tom Lendacky , Laszlo Ersek Date: Sun, 27 Aug 2017 20:34:28 -0400 Message-Id: <1503880469-15999-3-git-send-email-brijesh.singh@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1503880469-15999-1-git-send-email-brijesh.singh@amd.com> References: <1503880469-15999-1-git-send-email-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR2001CA0024.namprd20.prod.outlook.com (10.172.112.162) To CY1PR12MB0152.namprd12.prod.outlook.com (10.161.173.22) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: bd50dd21-00e8-4ba6-b40d-08d4edac9f3d X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(300000502095)(300135100095)(22001)(2017030254152)(300000503095)(300135400095)(48565401081)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:CY1PR12MB0152; X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0152; 3:OreU0PmADjVw8smOcLOstRJ2upz5leW7gJJ1BAIKcsZGgbi1fCRFfOJhsjZCK0r4hCCJnwoz1mfiFdwPcmGDAPBLipjOgtjAtDAI0CRVi8ncpJYMIu6HvSA10le0OGrTevewN0Ylfk+o0aRWWXNcz3cRG3as5gGj6WHiOPWEZYbnlAUrPJvemSiLgkwL4yJt5aZJ7JbyXntB1GPtPWsfWfOCxBeR9wfyDTUWJQkqCA46pllDTD/YdJqqoGl73Xhe; 25:RUxcqQu1iVBxELhPT6IdSgG6MaIukxT3hFjEpMGFdH3oSP4ISqCfDHdiJIfSm4qTSvpi0STt0vy3N/PMxGPLXcn8yO8GpMOFmuoQsG8QeMZIG4URbzshuMJzQAlE6yUYJIzMOn3Lt65uKKNogzpGc7xR76WQLkTlanziywmHb+jnJVl394RIAF2qRD1iwGKpBLsFjtRbvtbK7mi3XyRF4aZX9T1PcyWwn1BaYKrvJU5/S32fWaxcvqkaIf/BByr7WSczURuV9wuPt0ucSs1Sm+hF6vwslxrmKpol+NthWO1TOf9SyqR7YbgklabuwI1XNL7OjHZHu+U7vw7le9xCzQ==; 31:HHN8z8JjKcOuxDtvYkzMFKAE3mwHGX4EUFgRiG8rXFRCIFanUAFNu6gJ9dOw9SiZ6+9P1pRT2gZpPRS4z1IptdzHZg1DeZDCewTcYTotZtJR1lNq6iLG3vXbXhtCcIlsep1IgDL6R11tZqdGyzHZlIxagnpcLFSVsyMJ5mnp6ERmvcLGZCK6VRtWdxhD6F6TcIAHIfizjDXP8pRordlvVPw0Zlb03ka+fN1OXECbX28= X-MS-TrafficTypeDiagnostic: CY1PR12MB0152: X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0152; 20:mpD83QHKUtnRCatRMMT93I/O/wYRsA1mXQoja4sYGm2JSFRvyAHWijh7Rwj7Us/yFL0oTdeuVpr9NRyHCxSJJvVsEteoqWNlwzyaX5VOqxS5rkxMSF8GwmDP/j4U1sWjMjm0N2jID7AkSLR3bU4UIbDvHsfIlzX7LPyKaFjyP+sdGAS6WeKndeKCzfufIKJsfrgooZxjOdMEOnhY22LI2AZzgtyZLsNH2hl4r6YZk/zYs2kOWSPNsLCcZpinzaSTklxMoElIQDCYSEQuPTAVBsnGL1cnLNARxUrpTPqKnPKkZobbHWJlzYXbNnRoSJTHzLbvUEkOLypXblRCUSXSzG2bOiuY+M10nAC2EUvnuKVabzWzTnfrzJkG98fBzOjR1JpcsPSGpm51CvRKAIyzk/vLAfXTqRBTQTNLVrJKavS8KPpEW8wGrGQEohxQIcCuXrxSUl9YVvowH99vo2jn3706h8+kVQZduaB7pLjF9JKrwmwAxK9Z/23GkexZWnLw; 4:UkpdItb9ZiErMSWNHEFCnIrmSyYMGHYwK6VTp3ByDtHOAHKId5iMnIqxo/jTxyzR0x4zcAqhRwjnE/bcAqwvE48NvRV3eLHivQ4xt2acaYkZ9Fs6l1mc5vHyQmigHzgJYmY6mVyWB3ny7RhXxFs9FQ43Z9q7UnUftNDOXdHj11rYh9lGQ9evsLT0o+DQtRuGHcgNMRHf6eLZ5CCE6Y55B1njH2WP7QZgkjR0+gF+5IoqSA72SP8mdqrgW86fJt2yGS2Bi1DCqmh6H1EruHn9NSy2oRc+yhW6je1dZNeeNhkDE/bMMtCdI9yEAk3iZluTs0bjlxPaBUp7wFwMtcQDbQ== X-Exchange-Antispam-Report-Test: UriScan:(767451399110)(228905959029699); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(100000703101)(100105400095)(3002001)(93006095)(93001095)(6055026)(6041248)(20161123564025)(20161123555025)(20161123560025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:CY1PR12MB0152; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:CY1PR12MB0152; X-Forefront-PRVS: 0413C9F1ED X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(7370300001)(4630300001)(6009001)(39860400002)(189002)(199003)(2906002)(53936002)(6666003)(25786009)(7350300001)(478600001)(36756003)(2950100002)(76176999)(66066001)(47776003)(6116002)(97736004)(4326008)(6486002)(81166006)(8676002)(54906002)(81156014)(48376002)(110136004)(5003940100001)(50466002)(305945005)(6916009)(53416004)(68736007)(42186005)(2351001)(189998001)(7736002)(2361001)(50986999)(86362001)(3846002)(33646002)(105586002)(101416001)(50226002)(106356001)(5660300001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR12MB0152; H:brijesh-build-machine.amd.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY1PR12MB0152; 23:EySw0EJw9c5h+bBLB04DvdQGIPbhd+HbEp38a4DBC?= =?us-ascii?Q?eRYhcPakigau6ICBn2jyZVwJWk6MVRim44oe7rlnMz0Z0S0ZGlMpA4NloDBa?= =?us-ascii?Q?VTybFVW2hSMbHetAzuhBSvFr+byBUjhQLz37swJmP8UrkWOesLPDBds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c2UaXESQbb/iyN7zFQKlLx6Oj1O?= =?us-ascii?Q?//Nf+ypGA9YKhnYLcG5N+/NQ45+E+SuAmuvoLAgNNnOiUDggyXnE3R8doXsb?= =?us-ascii?Q?U90rPxHT30GQUonR1V9fMji1wCuh8sB?= X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0152; 6:vRnFNFcNCSiseeChh/iRSJO1gEYPbA9N1fkVKQuiXoLEyoOgWPyFsrXzlR3sVEq+T2jMAOEJBpXUQ1O74Gcux+q80ThA2JIZgPkdp4hEkHEmFzkokM/TSYR6iYWe9R7VpMERegF4NH0eaJw1Cmq1906TsXPwQwMpm2vj/y0f9y7oOZokOfzdSgTDcu9JpBfGdwQZZNZbBt48uCjMAON0Bz/AgdJfq9ex7h2vfAheoyx9bBkQvJZo2DyfEc7GrxYCx4arBiT1MIf4ODwdKZgKRHNuByyApLzSGCS5bVur7bwKQGEX6C85gnAq/mFlIMx4nWy5osFlxG28SHu9tc4Oxw==; 5:/ZWVtbeINaAxscAOBYVqNoLyasBQFpNSUrNDLne89g2ai0SZ2x5Zx3L6d55952KGQXnPg6XEtnMYaiKNN4OhQFuVq395BmCA5tajbJLXocC9CSKFvz0D0Sc4yenpL28OiXnVFKj/jOTbDRNRA/X8mA==; 24:oZ+YQzgWHehWHqjZD/tZApF1Vze65P5qqMtz20VFIgJ0JAMCRjCS/CavQiGNE/g5WY+/737gAMtjtGwN9r1tahNLLupbFzj5Dt8OLasHEuE=; 7:oc3jqgnAz80fJCdGZjdFRaDhl3b5MBwXVkgZq5k4EBbzfS34QoLASof33Js2aATZ9vSITNQZ9qljHngxXFqttPuAh8Jk8AxqNJ/4OAaSgsBxPU4loBWDnS40tFJA8wVB4JKL+BA4N9vInHONjskj76pYSz3Tk1DrG8gSH2wcBuSUNanNsMS3J3iY46+1jmKjMTnCEKqwJ6SLNzyNEvh4l4QmfP9Misn1KXWYUfdaRbU= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY1PR12MB0152; 20:agYlRMEM/AE8XoJlzKrhLSueJVRwEOUTb9oH+kyIXgawAzXPaw4NLLyK8h2ubhn4HRBaI2U+XF6Z4gNMt1LQ1NOzPKcRDkBWoNrhosqqDdW68RTAVC+QjUBY15KRj4W7QHIO7rgvD8rgA6ln6x+qY3ke2r7nk1imAneQQxceSXMz14Eur0wI/VofXfr7hd/chcXIkTDai26ZQQVc0yW+rLG3IYzN3MG6w2sYfmlkzXInShCbe88FXzn46qqPlsRF X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Aug 2017 00:35:02.0673 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR12MB0152 Subject: [PATCH v2 2/3] Ovmfpkg/VirtioBlkDxe: map virtio-blk request and response buffers X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Aug 2017 00:32:26 -0000 Content-Type: text/plain When device is behind the IOMMU, driver is require to pass the device address of virtio request, response and any memory referenced by those request/response to the bus master. The patch uses IOMMU-like member functions from VIRTIO_DEVICE_PROTOCOL to map request and response buffers system physical address to the device address. - If the buffer need to be accessed by both the processor and a bus master then map with BusMasterCommonBuffer. - If the buffer need to be accessed for a write operation by a bus master then map with BusMasterWrite. - If the buffer need to be accessed for a read operation by a bus master then map with BusMasterRead. Cc: Ard Biesheuvel Cc: Jordan Justen Cc: Tom Lendacky Cc: Laszlo Ersek Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Brijesh Singh --- OvmfPkg/VirtioBlkDxe/VirtioBlk.c | 138 ++++++++++++++++++-- 1 file changed, 125 insertions(+), 13 deletions(-) diff --git a/OvmfPkg/VirtioBlkDxe/VirtioBlk.c b/OvmfPkg/VirtioBlkDxe/VirtioBlk.c index 663ba281ab73..c9c42aa41243 100644 --- a/OvmfPkg/VirtioBlkDxe/VirtioBlk.c +++ b/OvmfPkg/VirtioBlkDxe/VirtioBlk.c @@ -232,7 +232,8 @@ VerifyReadWriteRequest ( @retval EFI_DEVICE_ERROR Failed to notify host side via VirtIo write, or unable to parse host response, or host response - is not VIRTIO_BLK_S_OK. + is not VIRTIO_BLK_S_OK or failed to map Buffer + for a bus master operation. **/ @@ -249,8 +250,16 @@ SynchronousRequest ( { UINT32 BlockSize; volatile VIRTIO_BLK_REQ Request; - volatile UINT8 HostStatus; + volatile UINT8 *HostStatus; + VOID *HostStatusBuffer; DESC_INDICES Indices; + VOID *RequestMapping; + VOID *StatusMapping; + VOID *BufferMapping; + EFI_PHYSICAL_ADDRESS BufferDeviceAddress; + EFI_PHYSICAL_ADDRESS HostStatusDeviceAddress; + EFI_PHYSICAL_ADDRESS RequestDeviceAddress; + EFI_STATUS Status; BlockSize = Dev->BlockIoMedia.BlockSize; @@ -275,12 +284,82 @@ SynchronousRequest ( Request.IoPrio = 0; Request.Sector = MultU64x32(Lba, BlockSize / 512); - VirtioPrepare (&Dev->Ring, &Indices); + // + // Host status is bi-directional (we preset with a value and expect the + // device to update it). Allocate a host status buffer which can be mapped + // to access equally by both processor and the device. + // + Status = Dev->VirtIo->AllocateSharedPages ( + Dev->VirtIo, + EFI_SIZE_TO_PAGES (sizeof *HostStatus), + &HostStatusBuffer + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + HostStatus = HostStatusBuffer; + + // + // Map virtio-blk request header (must be done after request header is + // populated) + // + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterRead, + (VOID *) &Request, + sizeof Request, + &RequestDeviceAddress, + &RequestMapping + ); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto FreeHostStatusBuffer; + } + + // + // Map data buffer + // + if (BufferSize > 0) { + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + (RequestIsWrite ? + VirtioOperationBusMasterRead : + VirtioOperationBusMasterWrite), + (VOID *) Buffer, + BufferSize, + &BufferDeviceAddress, + &BufferMapping + ); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto UnmapRequestBuffer; + } + } // // preset a host status for ourselves that we do not accept as success // - HostStatus = VIRTIO_BLK_S_IOERR; + *HostStatus = VIRTIO_BLK_S_IOERR; + + // + // Map the Status Buffer with VirtioOperationBusMasterCommonBuffer so that + // both processor and device can access it. + // + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterCommonBuffer, + HostStatusBuffer, + sizeof *HostStatus, + &HostStatusDeviceAddress, + &StatusMapping + ); + if (EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + goto UnmapDataBuffer; + } + + VirtioPrepare (&Dev->Ring, &Indices); // // ensured by VirtioBlkInit() -- this predicate, in combination with the @@ -291,8 +370,13 @@ SynchronousRequest ( // // virtio-blk header in first desc // - VirtioAppendDesc (&Dev->Ring, (UINTN) &Request, sizeof Request, - VRING_DESC_F_NEXT, &Indices); + VirtioAppendDesc ( + &Dev->Ring, + RequestDeviceAddress, + sizeof Request, + VRING_DESC_F_NEXT, + &Indices + ); // // data buffer for read/write in second desc @@ -311,27 +395,55 @@ SynchronousRequest ( // // VRING_DESC_F_WRITE is interpreted from the host's point of view. // - VirtioAppendDesc (&Dev->Ring, (UINTN) Buffer, (UINT32) BufferSize, + VirtioAppendDesc ( + &Dev->Ring, + BufferDeviceAddress, + (UINT32) BufferSize, VRING_DESC_F_NEXT | (RequestIsWrite ? 0 : VRING_DESC_F_WRITE), - &Indices); + &Indices + ); } // // host status in last (second or third) desc // - VirtioAppendDesc (&Dev->Ring, (UINTN) &HostStatus, sizeof HostStatus, - VRING_DESC_F_WRITE, &Indices); + VirtioAppendDesc ( + &Dev->Ring, + HostStatusDeviceAddress, + sizeof *HostStatus, + VRING_DESC_F_WRITE, + &Indices + ); // // virtio-blk's only virtqueue is #0, called "requestq" (see Appendix D). // if (VirtioFlush (Dev->VirtIo, 0, &Dev->Ring, &Indices, NULL) == EFI_SUCCESS && - HostStatus == VIRTIO_BLK_S_OK) { - return EFI_SUCCESS; + *HostStatus == VIRTIO_BLK_S_OK) { + Status = EFI_SUCCESS; + } else { + Status = EFI_DEVICE_ERROR; } - return EFI_DEVICE_ERROR; + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, StatusMapping); + +UnmapDataBuffer: + if (BufferSize > 0) { + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, BufferMapping); + } + +UnmapRequestBuffer: + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, RequestMapping); + +FreeHostStatusBuffer: + Dev->VirtIo->FreeSharedPages ( + Dev->VirtIo, + EFI_SIZE_TO_PAGES (sizeof *HostStatus), + HostStatusBuffer + ); + + return Status; } -- 2.7.4