\r
Utility functions used by virtio device drivers.\r
\r
- Copyright (C) 2012, Red Hat, Inc.\r
+ Copyright (C) 2012-2016, Red Hat, Inc.\r
+ Portion of Copyright (C) 2013, ARM Ltd.\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
\r
**/\r
\r
-#include <IndustryStandard/Pci22.h>\r
#include <Library/BaseLib.h>\r
#include <Library/BaseMemoryLib.h>\r
#include <Library/DebugLib.h>\r
#include <Library/VirtioLib.h>\r
\r
\r
-/**\r
-\r
- Write a word into Region 0 of the device specified by PciIo.\r
-\r
- Region 0 must be an iomem region. This is an internal function for the\r
- driver-specific VIRTIO_CFG_WRITE() macros.\r
-\r
- @param[in] PciIo Target PCI device.\r
-\r
- @param[in] FieldOffset Destination offset.\r
-\r
- @param[in] FieldSize Destination field size, must be in { 1, 2, 4, 8 }.\r
-\r
- @param[in] Value Little endian value to write, converted to UINT64.\r
- The least significant FieldSize bytes will be used.\r
-\r
-\r
- @return Status code returned by PciIo->Io.Write().\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VirtioWrite (\r
- IN EFI_PCI_IO_PROTOCOL *PciIo,\r
- IN UINTN FieldOffset,\r
- IN UINTN FieldSize,\r
- IN UINT64 Value\r
- )\r
-{\r
- UINTN Count;\r
- EFI_PCI_IO_PROTOCOL_WIDTH Width;\r
-\r
- Count = 1;\r
- switch (FieldSize) {\r
- case 1:\r
- Width = EfiPciIoWidthUint8;\r
- break;\r
-\r
- case 2:\r
- Width = EfiPciIoWidthUint16;\r
- break;\r
-\r
- case 8:\r
- Count = 2;\r
- // fall through\r
-\r
- case 4:\r
- Width = EfiPciIoWidthUint32;\r
- break;\r
-\r
- default:\r
- ASSERT (FALSE);\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- return PciIo->Io.Write (\r
- PciIo,\r
- Width,\r
- PCI_BAR_IDX0,\r
- FieldOffset,\r
- Count,\r
- &Value\r
- );\r
-}\r
-\r
-\r
-/**\r
-\r
- Read a word from Region 0 of the device specified by PciIo.\r
-\r
- Region 0 must be an iomem region. This is an internal function for the\r
- driver-specific VIRTIO_CFG_READ() macros.\r
-\r
- @param[in] PciIo Source PCI device.\r
-\r
- @param[in] FieldOffset Source offset.\r
-\r
- @param[in] FieldSize Source field size, must be in { 1, 2, 4, 8 }.\r
-\r
- @param[in] BufferSize Number of bytes available in the target buffer. Must\r
- equal FieldSize.\r
-\r
- @param[out] Buffer Target buffer.\r
-\r
-\r
- @return Status code returned by PciIo->Io.Read().\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-VirtioRead (\r
- IN EFI_PCI_IO_PROTOCOL *PciIo,\r
- IN UINTN FieldOffset,\r
- IN UINTN FieldSize,\r
- IN UINTN BufferSize,\r
- OUT VOID *Buffer\r
- )\r
-{\r
- UINTN Count;\r
- EFI_PCI_IO_PROTOCOL_WIDTH Width;\r
-\r
- ASSERT (FieldSize == BufferSize);\r
-\r
- Count = 1;\r
- switch (FieldSize) {\r
- case 1:\r
- Width = EfiPciIoWidthUint8;\r
- break;\r
-\r
- case 2:\r
- Width = EfiPciIoWidthUint16;\r
- break;\r
-\r
- case 8:\r
- Count = 2;\r
- // fall through\r
-\r
- case 4:\r
- Width = EfiPciIoWidthUint32;\r
- break;\r
-\r
- default:\r
- ASSERT (FALSE);\r
- return EFI_INVALID_PARAMETER;\r
- }\r
-\r
- return PciIo->Io.Read (\r
- PciIo,\r
- Width,\r
- PCI_BAR_IDX0,\r
- FieldOffset,\r
- Count,\r
- Buffer\r
- );\r
-}\r
-\r
-\r
/**\r
\r
Configure a virtio ring.\r
Notify the host about the descriptor chain just built, and wait until the\r
host processes it.\r
\r
- @param[in] PciIo The target virtio PCI device to notify.\r
+ @param[in] VirtIo The target virtio device to notify.\r
\r
@param[in] VirtQueueId Identifies the queue for the target device.\r
\r
Indices->HeadDescIdx identifies the head descriptor\r
of the descriptor chain.\r
\r
+ @param[out] UsedLen On success, the total number of bytes, consecutively\r
+ across the buffers linked by the descriptor chain,\r
+ that the host wrote. May be NULL if the caller\r
+ doesn't care, or can compute the same information\r
+ from device-specific request structures linked by the\r
+ descriptor chain.\r
\r
- @return Error code from VirtioWrite() if it fails.\r
+ @return Error code from VirtIo->SetQueueNotify() if it fails.\r
\r
@retval EFI_SUCCESS Otherwise, the host processed all descriptors.\r
\r
EFI_STATUS\r
EFIAPI\r
VirtioFlush (\r
- IN EFI_PCI_IO_PROTOCOL *PciIo,\r
- IN UINT16 VirtQueueId,\r
- IN OUT VRING *Ring,\r
- IN DESC_INDICES *Indices\r
+ IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
+ IN UINT16 VirtQueueId,\r
+ IN OUT VRING *Ring,\r
+ IN DESC_INDICES *Indices,\r
+ OUT UINT32 *UsedLen OPTIONAL\r
)\r
{\r
UINT16 NextAvailIdx;\r
+ UINT16 LastUsedIdx;\r
EFI_STATUS Status;\r
UINTN PollPeriodUsecs;\r
\r
// head descriptor of any given descriptor chain.\r
//\r
NextAvailIdx = *Ring->Avail.Idx;\r
+ //\r
+ // (Due to our lock-step progress, this is where the host will produce the\r
+ // used element with the head descriptor's index in it.)\r
+ //\r
+ LastUsedIdx = NextAvailIdx;\r
Ring->Avail.Ring[NextAvailIdx++ % Ring->QueueSize] =\r
Indices->HeadDescIdx % Ring->QueueSize;\r
\r
// OK.\r
//\r
MemoryFence();\r
- Status = VirtioWrite (\r
- PciIo,\r
- OFFSET_OF (VIRTIO_HDR, VhdrQueueNotify),\r
- sizeof (UINT16),\r
- VirtQueueId\r
- );\r
+ Status = VirtIo->SetQueueNotify (VirtIo, VirtQueueId);\r
if (EFI_ERROR (Status)) {\r
return Status;\r
}\r
MemoryFence();\r
}\r
\r
+ MemoryFence();\r
+\r
+ if (UsedLen != NULL) {\r
+ volatile CONST VRING_USED_ELEM *UsedElem;\r
+\r
+ UsedElem = &Ring->Used.UsedElem[LastUsedIdx % Ring->QueueSize];\r
+ ASSERT (UsedElem->Id == Indices->HeadDescIdx);\r
+ *UsedLen = UsedElem->Len;\r
+ }\r
+\r
return EFI_SUCCESS;\r
}\r
+\r
+\r
+/**\r
+\r
+ Report the feature bits to the VirtIo 1.0 device that the VirtIo 1.0 driver\r
+ understands.\r
+\r
+ In VirtIo 1.0, a device can reject a self-inconsistent feature bitmap through\r
+ the new VSTAT_FEATURES_OK status bit. (For example if the driver requests a\r
+ higher level feature but clears a prerequisite feature.) This function is a\r
+ small wrapper around VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures() that also\r
+ verifies if the VirtIo 1.0 device accepts the feature bitmap.\r
+\r
+ @param[in] VirtIo Report feature bits to this device.\r
+\r
+ @param[in] Features The set of feature bits that the driver wishes\r
+ to report. The caller is responsible to perform\r
+ any masking before calling this function; the\r
+ value is directly written with\r
+ VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures().\r
+\r
+ @param[in,out] DeviceStatus On input, the status byte most recently written\r
+ to the device's status register. On output (even\r
+ on error), DeviceStatus will be updated so that\r
+ it is suitable for further status bit\r
+ manipulation and writing to the device's status\r
+ register.\r
+\r
+ @retval EFI_SUCCESS The device accepted the configuration in Features.\r
+\r
+ @return EFI_UNSUPPORTED The device rejected the configuration in Features.\r
+\r
+ @retval EFI_UNSUPPORTED VirtIo->Revision is smaller than 1.0.0.\r
+\r
+ @return Error codes from the SetGuestFeatures(),\r
+ SetDeviceStatus(), GetDeviceStatus() member\r
+ functions.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Virtio10WriteFeatures (\r
+ IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r
+ IN UINT64 Features,\r
+ IN OUT UINT8 *DeviceStatus\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ if (VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ Status = VirtIo->SetGuestFeatures (VirtIo, Features);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ *DeviceStatus |= VSTAT_FEATURES_OK;\r
+ Status = VirtIo->SetDeviceStatus (VirtIo, *DeviceStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ Status = VirtIo->GetDeviceStatus (VirtIo, DeviceStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ if ((*DeviceStatus & VSTAT_FEATURES_OK) == 0) {\r
+ Status = EFI_UNSUPPORTED;\r
+ }\r
+\r
+ return Status;\r
+}\r