]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/VirtioNetDxe/SnpInitialize.c
OvmfPkg: VirtioNetDxe: add SNP.Initialize and shared dependencies
[mirror_edk2.git] / OvmfPkg / VirtioNetDxe / SnpInitialize.c
diff --git a/OvmfPkg/VirtioNetDxe/SnpInitialize.c b/OvmfPkg/VirtioNetDxe/SnpInitialize.c
new file mode 100644 (file)
index 0000000..39282d9
--- /dev/null
@@ -0,0 +1,459 @@
+/** @file\r
+\r
+  Implementation of the SNP.Initialize() function and its private helpers if\r
+  any.\r
+\r
+  Copyright (C) 2013, Red Hat, Inc.\r
+  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution. The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
+  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+#include "VirtioNet.h"\r
+\r
+/**\r
+  Initialize a virtio ring for a specific transfer direction of the virtio-net\r
+  device.\r
+\r
+  This function may only be called by VirtioNetInitialize().\r
+\r
+  @param[in,out] Dev       The VNET_DEV driver instance about to enter the\r
+                           EfiSimpleNetworkInitialized state.\r
+  @param[in]     Selector  Identifies the transfer direction (virtio queue) of\r
+                           the network device.\r
+  @param[out]    Ring      The virtio-ring inside the VNET_DEV structure,\r
+                           corresponding to Selector.\r
+\r
+  @retval EFI_UNSUPPORTED  The queue size reported by the virtio-net device is\r
+                           too small.\r
+  @return                  Status codes from VIRTIO_CFG_WRITE(),\r
+                           VIRTIO_CFG_READ() and VirtioRingInit().\r
+  @retval EFI_SUCCESS      Ring initialized.\r
+*/\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+VirtioNetInitRing (\r
+  IN OUT VNET_DEV *Dev,\r
+  IN     UINT16   Selector,\r
+  OUT    VRING    *Ring\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  UINT16     QueueSize;\r
+\r
+  //\r
+  // step 4b -- allocate selected queue\r
+  //\r
+  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueSelect, Selector);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  Status = VIRTIO_CFG_READ (Dev, Generic.VhdrQueueSize, &QueueSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\r
+  // For each packet (RX and TX alike), we need two descriptors:\r
+  // one for the virtio-net request header, and another one for the data\r
+  //\r
+  if (QueueSize < 2) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  Status = VirtioRingInit (QueueSize, Ring);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // step 4c -- report GPFN (guest-physical frame number) of queue\r
+  //\r
+  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueAddress,\r
+             (UINTN) Ring->Base >> EFI_PAGE_SHIFT);\r
+  if (EFI_ERROR (Status)) {\r
+    VirtioRingUninit (Ring);\r
+  }\r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Set up static scaffolding for the VirtioNetTransmit() and\r
+  VirtioNetGetStatus() SNP methods.\r
+\r
+  This function may only be called by VirtioNetInitialize().\r
+\r
+  The structures laid out and resources configured include:\r
+  - fully populate the TX queue with a static pattern of virtio descriptor\r
+    chains,\r
+  - tracking of heads of free descriptor chains from the above,\r
+  - one common virtio-net request header (never modified by the host) for all\r
+    pending TX packets,\r
+  - select polling over TX interrupt.\r
+\r
+  @param[in,out] Dev       The VNET_DEV driver instance about to enter the\r
+                           EfiSimpleNetworkInitialized state.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate the stack to track the heads\r
+                                of free descriptor chains.\r
+  @retval EFI_SUCCESS           TX setup successful.\r
+*/\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+VirtioNetInitTx (\r
+  IN OUT VNET_DEV *Dev\r
+  )\r
+{\r
+  UINTN PktIdx;\r
+\r
+  Dev->TxMaxPending = MIN (Dev->TxRing.QueueSize / 2, VNET_MAX_PENDING);\r
+  Dev->TxCurPending = 0;\r
+  Dev->TxFreeStack  = AllocatePool (Dev->TxMaxPending *\r
+                        sizeof *Dev->TxFreeStack);\r
+  if (Dev->TxFreeStack == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  for (PktIdx = 0; PktIdx < Dev->TxMaxPending; ++PktIdx) {\r
+    UINT16 DescIdx;\r
+\r
+    DescIdx = (UINT16) (2 * PktIdx);\r
+    Dev->TxFreeStack[PktIdx] = DescIdx;\r
+\r
+    //\r
+    // For each possibly pending packet, lay out the descriptor for the common\r
+    // (unmodified by the host) virtio-net request header.\r
+    //\r
+    Dev->TxRing.Desc[DescIdx].Addr  = (UINTN) &Dev->TxSharedReq;\r
+    Dev->TxRing.Desc[DescIdx].Len   = sizeof Dev->TxSharedReq;\r
+    Dev->TxRing.Desc[DescIdx].Flags = VRING_DESC_F_NEXT;\r
+    Dev->TxRing.Desc[DescIdx].Next  = DescIdx + 1;\r
+\r
+    //\r
+    // The second descriptor of each pending TX packet is updated on the fly,\r
+    // but it always terminates the descriptor chain of the packet.\r
+    //\r
+    Dev->TxRing.Desc[DescIdx + 1].Flags = 0;\r
+  }\r
+\r
+  //\r
+  // virtio-0.9.5, Appendix C, Packet Transmission\r
+  //\r
+  Dev->TxSharedReq.Flags   = 0;\r
+  Dev->TxSharedReq.GsoType = VIRTIO_NET_HDR_GSO_NONE;\r
+\r
+  //\r
+  // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
+  //\r
+  MemoryFence ();\r
+  Dev->TxLastUsed = *Dev->TxRing.Used.Idx;\r
+  ASSERT (Dev->TxLastUsed == 0);\r
+\r
+  //\r
+  // want no interrupt when a transmit completes\r
+  //\r
+  *Dev->TxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Set up static scaffolding for the VirtioNetReceive() SNP method and enable\r
+  live device operation.\r
+\r
+  This function may only be called as VirtioNetInitialize()'s final step.\r
+\r
+  The structures laid out and resources configured include:\r
+  - destination area for the host to write virtio-net request headers and\r
+    packet data into,\r
+  - select polling over RX interrupt,\r
+  - fully populate the RX queue with a static pattern of virtio descriptor\r
+    chains.\r
+\r
+  @param[in,out] Dev       The VNET_DEV driver instance about to enter the\r
+                           EfiSimpleNetworkInitialized state.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate RX destination area.\r
+  @return                       Status codes from VIRTIO_CFG_WRITE().\r
+  @retval EFI_SUCCESS           RX setup successful. The device is live and may\r
+                                already be writing to the receive area.\r
+*/\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+VirtioNetInitRx (\r
+  IN OUT VNET_DEV *Dev\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  UINTN      RxBufSize;\r
+  UINT16     RxAlwaysPending;\r
+  UINTN      PktIdx;\r
+  UINT16     DescIdx;\r
+  UINT8      *RxPtr;\r
+\r
+  //\r
+  // For each incoming packet we must supply two descriptors:\r
+  // - the recipient for the virtio-net request header, plus\r
+  // - the recipient for the network data (which consists of Ethernet header\r
+  //   and Ethernet payload).\r
+  //\r
+  RxBufSize = sizeof (VIRTIO_NET_REQ) +\r
+              (Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize);\r
+\r
+  //\r
+  // Limit the number of pending RX packets if the queue is big. The division\r
+  // by two is due to the above "two descriptors per packet" trait.\r
+  //\r
+  RxAlwaysPending = MIN (Dev->RxRing.QueueSize / 2, VNET_MAX_PENDING);\r
+\r
+  Dev->RxBuf = AllocatePool (RxAlwaysPending * RxBufSize);\r
+  if (Dev->RxBuf == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r
+  //\r
+  MemoryFence ();\r
+  Dev->RxLastUsed = *Dev->RxRing.Used.Idx;\r
+  ASSERT (Dev->RxLastUsed == 0);\r
+\r
+  //\r
+  // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device:\r
+  // the host should not send interrupts, we'll poll in VirtioNetReceive()\r
+  // and VirtioNetIsPacketAvailable().\r
+  //\r
+  *Dev->RxRing.Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r
+\r
+  //\r
+  // now set up a separate, two-part descriptor chain for each RX packet, and\r
+  // link each chain into (from) the available ring as well\r
+  //\r
+  DescIdx = 0;\r
+  RxPtr = Dev->RxBuf;\r
+  for (PktIdx = 0; PktIdx < RxAlwaysPending; ++PktIdx) {\r
+    //\r
+    // virtio-0.9.5, 2.4.1.2 Updating the Available Ring\r
+    // invisible to the host until we update the Index Field\r
+    //\r
+    Dev->RxRing.Avail.Ring[PktIdx] = DescIdx;\r
+\r
+    //\r
+    // virtio-0.9.5, 2.4.1.1 Placing Buffers into the Descriptor Table\r
+    //\r
+    Dev->RxRing.Desc[DescIdx].Addr  = (UINTN) RxPtr;\r
+    Dev->RxRing.Desc[DescIdx].Len   = sizeof (VIRTIO_NET_REQ);\r
+    Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE | VRING_DESC_F_NEXT;\r
+    Dev->RxRing.Desc[DescIdx].Next  = DescIdx + 1;\r
+    RxPtr += Dev->RxRing.Desc[DescIdx++].Len;\r
+\r
+    Dev->RxRing.Desc[DescIdx].Addr  = (UINTN) RxPtr;\r
+    Dev->RxRing.Desc[DescIdx].Len   = RxBufSize - sizeof (VIRTIO_NET_REQ);\r
+    Dev->RxRing.Desc[DescIdx].Flags = VRING_DESC_F_WRITE;\r
+    RxPtr += Dev->RxRing.Desc[DescIdx++].Len;\r
+  }\r
+\r
+  //\r
+  // virtio-0.9.5, 2.4.1.3 Updating the Index Field\r
+  //\r
+  MemoryFence ();\r
+  *Dev->RxRing.Avail.Idx = RxAlwaysPending;\r
+\r
+  //\r
+  // At this point reception may already be running. In order to make it sure,\r
+  // kick the hypervisor. If we fail to kick it, we must first abort reception\r
+  // before tearing down anything, because reception may have been already\r
+  // running even without the kick.\r
+  //\r
+  // virtio-0.9.5, 2.4.1.4 Notifying the Device\r
+  //\r
+  MemoryFence ();\r
+  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueNotify, VIRTIO_NET_Q_RX);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);\r
+    FreePool (Dev->RxBuf);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+\r
+/**\r
+  Resets a network adapter and allocates the transmit and receive buffers\r
+  required by the network interface; optionally, also requests allocation  of\r
+  additional transmit and receive buffers.\r
+\r
+  @param  This              The protocol instance pointer.\r
+  @param  ExtraRxBufferSize The size, in bytes, of the extra receive buffer\r
+                            space that the driver should allocate for the\r
+                            network interface. Some network interfaces will not\r
+                            be able to use the extra buffer, and the caller\r
+                            will not know if it is actually being used.\r
+  @param  ExtraTxBufferSize The size, in bytes, of the extra transmit buffer\r
+                            space that the driver should allocate for the\r
+                            network interface. Some network interfaces will not\r
+                            be able to use the extra buffer, and the caller\r
+                            will not know if it is actually being used.\r
+\r
+  @retval EFI_SUCCESS           The network interface was initialized.\r
+  @retval EFI_NOT_STARTED       The network interface has not been started.\r
+  @retval EFI_OUT_OF_RESOURCES  There was not enough memory for the transmit\r
+                                and receive buffers.\r
+  @retval EFI_INVALID_PARAMETER One or more of the parameters has an\r
+                                unsupported value.\r
+  @retval EFI_DEVICE_ERROR      The command could not be sent to the network\r
+                                interface.\r
+  @retval EFI_UNSUPPORTED       This function is not supported by the network\r
+                                interface.\r
+\r
+**/\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+VirtioNetInitialize (\r
+  IN EFI_SIMPLE_NETWORK_PROTOCOL *This,\r
+  IN UINTN                       ExtraRxBufferSize  OPTIONAL,\r
+  IN UINTN                       ExtraTxBufferSize  OPTIONAL\r
+  )\r
+{\r
+  VNET_DEV   *Dev;\r
+  EFI_TPL    OldTpl;\r
+  EFI_STATUS Status;\r
+  UINT8      NextDevStat;\r
+  UINT32     Features;\r
+\r
+  if (This == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  if (ExtraRxBufferSize > 0 || ExtraTxBufferSize > 0) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  Dev = VIRTIO_NET_FROM_SNP (This);\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+  if (Dev->Snm.State != EfiSimpleNetworkStarted) {\r
+    Status = EFI_NOT_STARTED;\r
+    goto InitFailed;\r
+  }\r
+\r
+  //\r
+  // In the EfiSimpleNetworkStarted state the virtio-net device has status\r
+  // value 0 (= reset) -- see the state diagram, the full call chain to\r
+  // the end of VirtioNetGetFeatures() (considering we're here now),\r
+  // the DeviceFailed label below, and VirtioNetShutdown().\r
+  //\r
+  // Accordingly, the below is a subsequence of the steps found in the\r
+  // virtio-0.9.5 spec, 2.2.1 Device Initialization Sequence.\r
+  //\r
+  NextDevStat = VSTAT_ACK;    // step 2 -- acknowledge device presence\r
+  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  if (EFI_ERROR (Status)) {\r
+    goto InitFailed;\r
+  }\r
+\r
+  NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it\r
+  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  if (EFI_ERROR (Status)) {\r
+    goto DeviceFailed;\r
+  }\r
+\r
+  //\r
+  // step 4a -- retrieve features. Note that we're past validating required\r
+  // features in VirtioNetGetFeatures().\r
+  //\r
+  Status = VIRTIO_CFG_READ (Dev, Generic.VhdrDeviceFeatureBits, &Features);\r
+  if (EFI_ERROR (Status)) {\r
+    goto DeviceFailed;\r
+  }\r
+  ASSERT (Features & VIRTIO_NET_F_MAC);\r
+  ASSERT (Dev->Snm.MediaPresentSupported ==\r
+    !!(Features & VIRTIO_NET_F_STATUS));\r
+\r
+  //\r
+  // step 4b, 4c -- allocate and report virtqueues\r
+  //\r
+  Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_RX, &Dev->RxRing);\r
+  if (EFI_ERROR (Status)) {\r
+    goto DeviceFailed;\r
+  }\r
+\r
+  Status = VirtioNetInitRing (Dev, VIRTIO_NET_Q_TX, &Dev->TxRing);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ReleaseRxRing;\r
+  }\r
+\r
+  //\r
+  // step 5 -- keep only the features we want\r
+  //\r
+  Features &= VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS;\r
+  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrGuestFeatureBits, Features);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ReleaseTxRing;\r
+  }\r
+\r
+  //\r
+  // step 6 -- virtio-net initialization complete\r
+  //\r
+  NextDevStat |= VSTAT_DRIVER_OK;\r
+  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ReleaseTxRing;\r
+  }\r
+\r
+  Status = VirtioNetInitTx (Dev);\r
+  if (EFI_ERROR (Status)) {\r
+    goto AbortDevice;\r
+  }\r
+\r
+  //\r
+  // start receiving\r
+  //\r
+  Status = VirtioNetInitRx (Dev);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ReleaseTxAux;\r
+  }\r
+\r
+  Dev->Snm.State = EfiSimpleNetworkInitialized;\r
+  gBS->RestoreTPL (OldTpl);\r
+  return EFI_SUCCESS;\r
+\r
+ReleaseTxAux:\r
+  VirtioNetShutdownTx (Dev);\r
+\r
+AbortDevice:\r
+  VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);\r
+\r
+ReleaseTxRing:\r
+  VirtioRingUninit (&Dev->TxRing);\r
+\r
+ReleaseRxRing:\r
+  VirtioRingUninit (&Dev->RxRing);\r
+\r
+DeviceFailed:\r
+  //\r
+  // restore device status invariant for the EfiSimpleNetworkStarted state\r
+  //\r
+  VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);\r
+\r
+InitFailed:\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r