From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-co1nam03on0078.outbound.protection.outlook.com [104.47.40.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id D0A6621E3EA73 for ; Thu, 14 Sep 2017 04:05:43 -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=8PuKztmS9JjzzCGX9tBdyWahhLX1U/HjvR5LG0tTRAo=; b=Z4sScUupHfrUfzjIVF2rcyrqh3a6o8qBs9FDwMDMMFLyML+VMQqlDUGy/JtRCU44UydXG52LJOE4lk0BXouI93BAkebYsDCG1MotSQ9Lrsi8o/YHf7z4SL7YAXlVKiv9Q0U6R/EL72/H+G86SAOuYUbe169ncRNKYVbV2Zt/Kw8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from ubuntu-010236106000.amd.com (165.204.78.1) by DM2PR12MB0156.namprd12.prod.outlook.com (2a01:111:e400:50ce::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.35.12; Thu, 14 Sep 2017 11:08:40 +0000 From: Brijesh Singh To: edk2-devel@lists.01.org Cc: Brijesh Singh , Ard Biesheuvel , Jordan Justen , Tom Lendacky , Laszlo Ersek Date: Thu, 14 Sep 2017 06:08:20 -0500 Message-Id: <20170914110822.112540-7-brijesh.singh@amd.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20170914110822.112540-1-brijesh.singh@amd.com> References: <20170914110822.112540-1-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.78.1] X-ClientProxiedBy: BN6PR08CA0083.namprd08.prod.outlook.com (2603:10b6:404:b6::21) To DM2PR12MB0156.namprd12.prod.outlook.com (2a01:111:e400:50ce::19) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 470a621e-0bff-454d-003b-08d4fb60f4be X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:DM2PR12MB0156; X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0156; 3:e6oce7NbfxvjwB1ilqpIaEfmbgw7PjGsgRQrTyDwwsjdUL7aLO4ggcLPlORvywQz1ShDWJHV6litQs7/nUaapNq2e+ea2Qlo+1agSPFnX9l9stdChzg+WdubxKIMHjDEORK1+jCR++7xpZgQv+EAmADZwjdYY7COXKmSJf0c9hBFHSJUbFUI84BqP3w0f1tQYdYYD5oDeQgsmTR892ugRPoV8xIaTGc0IaHr4uwtjCPbygHz/H6daqHbvbIHz1L7; 25:hevlDrbfeUK6+t1uE4e9re+u6gNaDt8L2pNRzpFjYRQlerKOXQq64F1IjGDMI7UU8BTS8ZnDnuBTYn2rUB1sNW5bODIgPuti7293+VVPVbx4gy91mLu+s4wkSSoV3vSb8v/lDOcbfBAZJZjOmMPpkzkH/96MoScGXZfkoKeuibRty6HpkZpbUmdrHE3tpTfwe/pDBWow7c9orOS4Hid+rh3+MQoYqqS1I4rQoXOkC3vnhO7yT3TgAd/41m1o6NuuFeF1xZB8hQhDn/YhsCh2Cm1BSw5FlajUqr5H5tq2EfDhHmjxOH+MMsWDekoNVucKIYola8Kf3VFPzBctYrEl7g==; 31:jvhXCMtUec/5OBb1KavOCl89x3/nVwVnsZCFUHHdw4UwFualy9+qvIjeUTepUdNYLMaZ+YjQjtJy29GNY4nMJR+DQVHLl9mxE9mE/1qcgbApo2BgPUWHsLlU8JZVwlhPMMfpXyLbBehKLPSdFn7BRW5wqudd7mR+PjRVFIJkiQsowupbClh0fXgjQV+E8PBW9j9YkizFnXr212Zf6Dda7fUL9prjluTSnjgEJblaY44= X-MS-TrafficTypeDiagnostic: DM2PR12MB0156: X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0156; 20:WxFwTPf5wfEGllOGUTBLKkQC67URWOvF/KD0Cgl88BycpFi9xoyFu3MKZb3VGwFWIPjXjf1WuusXsjtvR4FmZ/tum3IEO7Hk1hMIHaktJ6zRAHxCGDdhKwa1I95i+YiiIkVYMhaA+iw9SXqFfwOaFKlpr04dgmpa158paIME2kiLCyoun2InTd5JddLvjQyOZguLgo6ZiQi6iwgna3KT9WVUSmpwxaObcikZTq4pDi2u+ZIknZ1BzxlBiOWyB627/RwO3uKaU5ev2zP/jBb2EUJ7/d5dk47FveeOAQ2BruhWmnWIRABzT1IgnvruSTRl1pBaqT87H9YR/VwNOjqA8zd7McGoqwT+dREFrkIM0mP8rm8pU3/WWuaZ2RGjL9LSQGVQbIsIHODW8yM3l7yRftEqte3r6dfBRzB4hV6uP+xIzhHkVl+F+QJcmFulTOyPBdsKUqGlwhPBVFitV1JMAqzSrMVBG3JNlQXqNP3mREMgma7pjSiW9aEqIuBHxwUA; 4:AiLxYp6nGV1UnWwMmF/ou3d8yJHu5WwcnnzLmugrdOxKCXoeupqi4OqoVaq6vJViFH9n4zmO++bXn4x+jVpTqbMfek55QhPSnFCuEeDJuFZ9EdLDGhGPpwesrD3T78rV+PCo1hKKsN2Ip1NIGLgjZP2nB3B9zIuSFMNf9Q8LOXVoxoRf+TpQo4h7cVA+dzKPq9iMurIGLyGEHzJi81yLCdoaYLvIoFc8kp61oJ0/2NJaQcW4va3e2mLIbez7V7ttbn84g9h9GUHEZ1dyjCBdt+vfLHPuzfX9JQ4YRYkBqybjKrG4+WAsaGrRn5wJ2dbc7Iyub+oFoQ2ZElCQ8A0qvQ== 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)(2401047)(8121501046)(5005006)(10201501046)(93006095)(93001095)(3002001)(100000703101)(100105400095)(6055026)(6041248)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123560025)(20161123558100)(20161123564025)(20161123562025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:DM2PR12MB0156; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:DM2PR12MB0156; X-Forefront-PRVS: 0430FA5CB7 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(346002)(376002)(39860400002)(189002)(199003)(51234002)(189998001)(50986999)(76176999)(105586002)(106356001)(33646002)(2361001)(2351001)(5660300001)(6666003)(6916009)(2950100002)(6116002)(25786009)(36756003)(1076002)(101416001)(68736007)(3846002)(53416004)(16586007)(6486002)(478600001)(97736004)(50226002)(110136004)(81156014)(47776003)(48376002)(16526017)(81166006)(54906002)(8676002)(8936002)(7736002)(316002)(305945005)(2906002)(4326008)(53936002)(86362001)(5003940100001)(50466002)(66066001); DIR:OUT; SFP:1101; SCL:1; SRVR:DM2PR12MB0156; H:ubuntu-010236106000.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; DM2PR12MB0156; 23:ULQHzWT7ZXvMSHiu7bDojZD0+LxNLOliWNjZMIekJ?= =?us-ascii?Q?jl28qJCeit+nWwRNJHB5jIgphE4vVg4EH5Ap92j1BI/7PglIf7cVR77f4KEq?= =?us-ascii?Q?YThHAEdIANl3ZPSsX5LVPpAMCxwksTqPPEwFtG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FmrZSrxM0+75W3ngqQp2NumdjJ54TypERSEcHb84NQEZhnT/lYEdVvlz+GjboRCEGm+mXQ= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0156; 20:GQaUUzuoQ50qroUpfXT5xxmCdCb7g7pJebpo+o/cXeAo8uYtbbenPemJFFD5Gwt+i8p5aIt2R8aqLXVSRTXSRJpC8vBZAT6+0XqciObVwYONtz2dy/Tiyz2C0aD7TpR47ntFYosYt8N+eyglkIJHTTntp9E3r6giSGYClNTRaKBsWX0BX4coympJ6wIBXkuCL8eRA3bHvW6visrZwZQPSjMEBRORMBKMkYInw07vHe4ERC0G8T8lYrhhIfGBXoUi X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2017 11:08:40.1357 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR12MB0156 Subject: [PATCH v3 6/8] OvmfPkg/VirtioNetDxe: add Tx packet map/unmap helper functions 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: Thu, 14 Sep 2017 11:05:44 -0000 Content-Type: text/plain When device is behind IOMMU, driver is require to pass the device address of TxBuf in the Tx VRING. The patch adds helper functions and data structure to map and unmap the TxBuf system physical address to a device address. Since the TxBuf is returned back to caller from VirtioNetGetStatus() hence we use OrderedCollection interface to save the TxBuf system physical to device address mapping. After the TxBuf is succesfully transmitted VirtioNetUnmapTxBuf() does the reverse lookup in OrderedCollection data structure to get the system physical address of TxBuf for a given device address. 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/VirtioNetDxe/VirtioNet.inf | 1 + OvmfPkg/VirtioNetDxe/VirtioNet.h | 38 ++++ OvmfPkg/VirtioNetDxe/SnpInitialize.c | 18 +- OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c | 224 ++++++++++++++++++++ 4 files changed, 279 insertions(+), 2 deletions(-) diff --git a/OvmfPkg/VirtioNetDxe/VirtioNet.inf b/OvmfPkg/VirtioNetDxe/VirtioNet.inf index a855ad4ac154..9ff6d87e6190 100644 --- a/OvmfPkg/VirtioNetDxe/VirtioNet.inf +++ b/OvmfPkg/VirtioNetDxe/VirtioNet.inf @@ -49,6 +49,7 @@ DebugLib DevicePathLib MemoryAllocationLib + OrderedCollectionLib UefiBootServicesTableLib UefiDriverEntryPoint UefiLib diff --git a/OvmfPkg/VirtioNetDxe/VirtioNet.h b/OvmfPkg/VirtioNetDxe/VirtioNet.h index 027f75993e8e..3fc88cfb790e 100644 --- a/OvmfPkg/VirtioNetDxe/VirtioNet.h +++ b/OvmfPkg/VirtioNetDxe/VirtioNet.h @@ -26,6 +26,7 @@ #include #include #include +#include #define VNET_SIG SIGNATURE_32 ('V', 'N', 'E', 'T') @@ -100,6 +101,7 @@ typedef struct { VIRTIO_1_0_NET_REQ *TxSharedReq; // VirtioNetInitTx VOID *TxSharedReqMap; // VirtioNetInitTx UINT16 TxLastUsed; // VirtioNetInitTx + ORDERED_COLLECTION *TxBufCollection; // VirtioNetInitTx } VNET_DEV; @@ -281,6 +283,42 @@ VirtioNetUninitRing ( ); // +// utility functions to map caller-supplied Tx buffer system physical address +// to a device address and vice versa +// +EFI_STATUS +EFIAPI +VirtioNetMapTxBuf ( + IN VNET_DEV *Dev, + IN VOID *Buffer, + IN UINTN NumberOfBytes, + OUT EFI_PHYSICAL_ADDRESS *DeviceAddress + ); + +EFI_STATUS +EFIAPI +VirtioNetUnmapTxBuf ( + IN VNET_DEV *Dev, + OUT VOID **Buffer, + IN EFI_PHYSICAL_ADDRESS DeviceAddress + ); + +INTN +EFIAPI +VirtioNetTxBufMapInfoCompare ( + IN CONST VOID *UserStruct1, + IN CONST VOID *UserStruct2 + ); + +INTN +EFIAPI +VirtioNetTxBufDeviceAddressCompare ( + IN CONST VOID *StandaloneKey, + IN CONST VOID *UserStruct + ); + + +// // event callbacks // VOID diff --git a/OvmfPkg/VirtioNetDxe/SnpInitialize.c b/OvmfPkg/VirtioNetDxe/SnpInitialize.c index 9621f936d2cb..ffb3deefe00c 100644 --- a/OvmfPkg/VirtioNetDxe/SnpInitialize.c +++ b/OvmfPkg/VirtioNetDxe/SnpInitialize.c @@ -147,7 +147,8 @@ ReleaseQueue: EfiSimpleNetworkInitialized state. @retval EFI_OUT_OF_RESOURCES Failed to allocate the stack to track the heads - of free descriptor chains. + of free descriptor chains or failed to init + TxBufCollection. @return Status codes from VIRTIO_DEVICE_PROTOCOL. AllocateSharedPages() or VirtioMapAllBytesInSharedBuffer() @@ -176,6 +177,15 @@ VirtioNetInitTx ( return EFI_OUT_OF_RESOURCES; } + Dev->TxBufCollection = OrderedCollectionInit ( + VirtioNetTxBufMapInfoCompare, + VirtioNetTxBufDeviceAddressCompare + ); + if (Dev->TxBufCollection == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto FreeTxFreeStack; + } + // // Allocate TxSharedReq header and map with BusMasterCommonBuffer so that it // can be accessed equally by both processor and device. @@ -186,7 +196,7 @@ VirtioNetInitTx ( &TxSharedReqBuffer ); if (EFI_ERROR (Status)) { - goto FreeTxFreeStack; + goto UninitTxBufCollection; } ZeroMem (TxSharedReqBuffer, sizeof *Dev->TxSharedReq); @@ -267,6 +277,10 @@ FreeTxSharedReqBuffer: EFI_SIZE_TO_PAGES (sizeof *(Dev->TxSharedReq)), TxSharedReqBuffer ); + +UninitTxBufCollection: + OrderedCollectionUninit (Dev->TxBufCollection); + FreeTxFreeStack: FreePool (Dev->TxFreeStack); diff --git a/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c b/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c index 2fce8142d554..18dbf1812541 100644 --- a/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c +++ b/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c @@ -18,6 +18,16 @@ #include "VirtioNet.h" +// +// The user structure for the ordered collection that will track the mapping +// info of the packets queued in TxRing +// +typedef struct { + VOID *Buffer; + EFI_PHYSICAL_ADDRESS DeviceAddress; // lookup key for reverse mapping + VOID *BufMap; +} TX_BUF_MAP_INFO; + /** Release RX and TX resources on the boundary of the EfiSimpleNetworkInitialized state. @@ -54,6 +64,10 @@ VirtioNetShutdownTx ( IN OUT VNET_DEV *Dev ) { + ORDERED_COLLECTION_ENTRY *Entry, *Entry2; + TX_BUF_MAP_INFO *TxBufMapInfo; + VOID *UserStruct; + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Dev->TxSharedReqMap); Dev->VirtIo->FreeSharedPages ( Dev->VirtIo, @@ -61,6 +75,17 @@ VirtioNetShutdownTx ( Dev->TxSharedReq ); + for (Entry = OrderedCollectionMin (Dev->TxBufCollection); + Entry != NULL; + Entry = Entry2) { + Entry2 = OrderedCollectionNext (Entry); + OrderedCollectionDelete (Dev->TxBufCollection, Entry, &UserStruct); + TxBufMapInfo = UserStruct; + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, TxBufMapInfo->BufMap); + FreePool (TxBufMapInfo); + } + OrderedCollectionUninit (Dev->TxBufCollection); + FreePool (Dev->TxFreeStack); } @@ -83,3 +108,202 @@ VirtioNetUninitRing ( Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, RingMap); VirtioRingUninit (Dev->VirtIo, Ring); } + + +/** + Map Caller-supplied TxBuf buffer to the device-mapped address + + @param[in] Dev The VNET_DEV driver instance which wants to + map the Tx packet. + @param[in] Buffer The system physical address of TxBuf + @param[in] NumberOfBytes Number of bytes to map + @param[out] DeviceAddress The resulting device address for the bus + master access. + + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to + a lack of resources. + @return Status codes from + VirtioMapAllBytesInSharedBuffer() + @retval EFI_SUCCESS Caller-supplied buffer is succesfully mapped. +*/ +EFI_STATUS +EFIAPI +VirtioNetMapTxBuf ( + IN VNET_DEV *Dev, + IN VOID *Buffer, + IN UINTN NumberOfBytes, + OUT EFI_PHYSICAL_ADDRESS *DeviceAddress + ) +{ + EFI_STATUS Status; + TX_BUF_MAP_INFO *TxBufMapInfo; + EFI_PHYSICAL_ADDRESS Address; + VOID *Mapping; + + TxBufMapInfo = AllocatePool (sizeof (*TxBufMapInfo)); + if (TxBufMapInfo == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = VirtioMapAllBytesInSharedBuffer ( + Dev->VirtIo, + VirtioOperationBusMasterRead, + Buffer, + NumberOfBytes, + &Address, + &Mapping + ); + if (EFI_ERROR (Status)) { + goto FreeTxBufMapInfo; + } + + TxBufMapInfo->Buffer = Buffer; + TxBufMapInfo->DeviceAddress = Address; + TxBufMapInfo->BufMap = Mapping; + + Status = OrderedCollectionInsert ( + Dev->TxBufCollection, + NULL, + TxBufMapInfo + ); + switch (Status) { + case EFI_OUT_OF_RESOURCES: + goto UnmapTxBuf; + case EFI_ALREADY_STARTED: + // + // This should never happen: it implies + // + // - an identity-mapping VIRTIO_DEVICE_PROTOCOL.MapSharedBuffer() + // implementation -- which is fine, + // + // - and an SNP client that queues multiple instances of the exact same + // buffer address with SNP.Transmit() -- which is undefined behavior, + // based on the TxBuf language in UEFI-2.7, + // EFI_SIMPLE_NETWORK.GetStatus(). + // + ASSERT (FALSE); + Status = EFI_INVALID_PARAMETER; + goto UnmapTxBuf; + default: + ASSERT_EFI_ERROR (Status); + break; + } + + *DeviceAddress = Address; + return EFI_SUCCESS; + +UnmapTxBuf: + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, Mapping); + +FreeTxBufMapInfo: + FreePool (TxBufMapInfo); + return Status; +} + +/** + Unmap (aka reverse mapping) device mapped TxBuf buffer to the system + physical address + + @param[in] Dev The VNET_DEV driver instance which wants to + reverse- and unmap the Tx packet. + @param[out] Buffer The system physical address of TxBuf + @param[in] DeviceAddress The device address for the TxBuf + + @retval EFI_INVALID_PARAMETER The DeviceAddress is not mapped + @retval EFI_SUCCESS The TxBuf at DeviceAddress has been unmapped, + and Buffer has been set to TxBuf's system + physical address. + +*/ +EFI_STATUS +EFIAPI +VirtioNetUnmapTxBuf ( + IN VNET_DEV *Dev, + OUT VOID **Buffer, + IN EFI_PHYSICAL_ADDRESS DeviceAddress + ) +{ + ORDERED_COLLECTION_ENTRY *Entry; + TX_BUF_MAP_INFO *TxBufMapInfo; + VOID *UserStruct; + + Entry = OrderedCollectionFind (Dev->TxBufCollection, &DeviceAddress); + if (Entry == NULL) { + return EFI_INVALID_PARAMETER; + } + + OrderedCollectionDelete (Dev->TxBufCollection, Entry, &UserStruct); + + TxBufMapInfo = UserStruct; + + *Buffer = TxBufMapInfo->Buffer; + Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, TxBufMapInfo->BufMap); + FreePool (TxBufMapInfo); + + return EFI_SUCCESS; +} + +/** + Comparator function for two TX_BUF_MAP_INFO objects. + + @param[in] UserStruct1 Pointer to the first TX_BUF_MAP_INFO object. + + @param[in] UserStruct2 Pointer to the second TX_BUF_MAP_INFO object. + + @retval <0 If UserStruct1 compares less than UserStruct2. + + @retval 0 If UserStruct1 compares equal to UserStruct2. + + @retval >0 If UserStruct1 compares greater than UserStruct2. +*/ +INTN +EFIAPI +VirtioNetTxBufMapInfoCompare ( + IN CONST VOID *UserStruct1, + IN CONST VOID *UserStruct2 + ) +{ + CONST TX_BUF_MAP_INFO *MapInfo1; + CONST TX_BUF_MAP_INFO *MapInfo2; + + MapInfo1 = UserStruct1; + MapInfo2 = UserStruct2; + + return MapInfo1->DeviceAddress < MapInfo2->DeviceAddress ? -1 : + MapInfo1->DeviceAddress > MapInfo2->DeviceAddress ? 1 : + 0; +} + +/** + Compare a standalone DeviceAddress against a TX_BUF_MAP_INFO object + containing an embedded DeviceAddress. + + @param[in] StandaloneKey Pointer to DeviceAddress, which has type + EFI_PHYSICAL_ADDRESS. + + @param[in] UserStruct Pointer to the TX_BUF_MAP_INFO object with the + embedded DeviceAddress. + + @retval <0 If StandaloneKey compares less than UserStruct's key. + + @retval 0 If StandaloneKey compares equal to UserStruct's key. + + @retval >0 If StandaloneKey compares greater than UserStruct's key. +**/ +INTN +EFIAPI +VirtioNetTxBufDeviceAddressCompare ( + IN CONST VOID *StandaloneKey, + IN CONST VOID *UserStruct + ) +{ + CONST EFI_PHYSICAL_ADDRESS *DeviceAddress; + CONST TX_BUF_MAP_INFO *MapInfo; + + DeviceAddress = StandaloneKey; + MapInfo = UserStruct; + + return *DeviceAddress < MapInfo->DeviceAddress ? -1 : + *DeviceAddress > MapInfo->DeviceAddress ? 1 : + 0; +} -- 2.9.5