RxBuf is shared between guest and hypervisor, use AllocateSharedPages()
to allocate this memory region and Map() it with BusMasterCommonBuffer
operation so that it can be accessed by guest and hypervisor.

Cc: Ard Biesheuvel <ard.biesheu...@linaro.org>
Cc: Jordan Justen <jordan.l.jus...@intel.com>
Cc: Tom Lendacky <thomas.lenda...@amd.com>
Cc: Laszlo Ersek <ler...@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Brijesh Singh <brijesh.si...@amd.com>
---
 OvmfPkg/VirtioNetDxe/VirtioNet.h        |  3 ++
 OvmfPkg/VirtioNetDxe/Events.c           |  5 +++
 OvmfPkg/VirtioNetDxe/SnpInitialize.c    | 33 +++++++++++++++++---
 OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c | 10 +++++-
 4 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/OvmfPkg/VirtioNetDxe/VirtioNet.h b/OvmfPkg/VirtioNetDxe/VirtioNet.h
index 2964c946e26e..a4661bd5d2fe 100644
--- a/OvmfPkg/VirtioNetDxe/VirtioNet.h
+++ b/OvmfPkg/VirtioNetDxe/VirtioNet.h
@@ -4,6 +4,7 @@
   Protocol instances for virtio-net devices.
 
   Copyright (C) 2013, Red Hat, Inc.
+  Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
 
   This program and the accompanying materials are licensed and made available
   under the terms and conditions of the BSD License which accompanies this
@@ -85,6 +86,8 @@ typedef struct {
   VOID                        *RxRingMap;        // VirtioNetInitRing
   UINT8                       *RxBuf;            // VirtioNetInitRx
   UINT16                      RxLastUsed;        // VirtioNetInitRx
+  UINTN                       RxBufNoPages;      // VirtioNetInitRx
+  VOID                        *RxBufMap;         // VirtioNetInitRx
 
   VRING                       TxRing;            // VirtioNetInitRing
   VOID                        *TxRingMap;        // VirtioNetInitRing
diff --git a/OvmfPkg/VirtioNetDxe/Events.c b/OvmfPkg/VirtioNetDxe/Events.c
index 9eb129ca2006..171b144518a9 100644
--- a/OvmfPkg/VirtioNetDxe/Events.c
+++ b/OvmfPkg/VirtioNetDxe/Events.c
@@ -102,4 +102,9 @@ VirtioNetExitBoot (
     VirtioRingUnmap (Dev->VirtIo, &Dev->RxRing, Dev->RxRingMap);
     Dev->RxRingMap = NULL;
   }
+
+  if (Dev->RxBufMap != NULL) {
+    VirtioUnmapSharedBuffer (Dev->VirtIo, Dev->RxBufMap);
+    Dev->RxBufMap = NULL;
+  }
 }
diff --git a/OvmfPkg/VirtioNetDxe/SnpInitialize.c 
b/OvmfPkg/VirtioNetDxe/SnpInitialize.c
index cbc9c51cb643..59c9f1ed3516 100644
--- a/OvmfPkg/VirtioNetDxe/SnpInitialize.c
+++ b/OvmfPkg/VirtioNetDxe/SnpInitialize.c
@@ -244,6 +244,7 @@ VirtioNetInitRx (
   UINTN      PktIdx;
   UINT16     DescIdx;
   UINT8      *RxPtr;
+  UINTN      NumBytes;
 
   //
   // In VirtIo 1.0, the NumBuffers field is mandatory. In 0.9.5, it depends on
@@ -268,9 +269,24 @@ VirtioNetInitRx (
   //
   RxAlwaysPending = (UINT16) MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);
 
-  Dev->RxBuf = AllocatePool (RxAlwaysPending * RxBufSize);
-  if (Dev->RxBuf == NULL) {
-    return EFI_OUT_OF_RESOURCES;
+  //
+  // The RxBuf is shared between guest and hypervisor, use SharedPages() to
+  // allocate this memory region and Map() it using BusMasterCommonBuffer
+  // operation so that it can be accessed equally by both guest and
+  // hypervisor.
+  //
+  NumBytes = RxAlwaysPending * RxBufSize;
+  Dev->RxBufNoPages = EFI_SIZE_TO_PAGES (NumBytes);
+  Status = VirtioAllocateSharedPages (Dev->VirtIo, Dev->RxBufNoPages,
+             (VOID *) &Dev->RxBuf);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = VirtioMapSharedBufferCommon (Dev->VirtIo, Dev->RxBuf, NumBytes,
+             &Dev->RxBufMap);
+  if (EFI_ERROR (Status)) {
+    goto Failed;
   }
 
   //
@@ -333,10 +349,19 @@ VirtioNetInitRx (
   Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_RX);
   if (EFI_ERROR (Status)) {
     Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);
-    FreePool (Dev->RxBuf);
+    goto Failed;
   }
 
   return Status;
+
+Failed:
+  if (Dev->RxBufMap != NULL) {
+    VirtioUnmapSharedBuffer (Dev->VirtIo, Dev->RxBufMap);
+    Dev->RxBufMap = NULL;
+  }
+
+  VirtioFreeSharedPages (Dev->VirtIo, Dev->RxBufNoPages, (VOID *) Dev->RxBuf);
+  return Status;
 }
 
 
diff --git a/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c 
b/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c
index 9fedb72fdbd4..48cd4900911c 100644
--- a/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c
+++ b/OvmfPkg/VirtioNetDxe/SnpSharedHelpers.c
@@ -39,7 +39,15 @@ VirtioNetShutdownRx (
   IN OUT VNET_DEV *Dev
   )
 {
-  FreePool (Dev->RxBuf);
+  //
+  // If RxBuf mapping exist then unmap it.
+  //
+  if (Dev->RxBufMap != NULL) {
+    VirtioUnmapSharedBuffer (Dev->VirtIo, Dev->RxBufMap);
+    Dev->RxBufMap = NULL;
+  }
+
+  VirtioFreeSharedPages (Dev->VirtIo, Dev->RxBufNoPages, (VOID *) Dev->RxBuf);
 }
 
 
-- 
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to