]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/VirtioScsiDxe/VirtioScsi.c
OvmfPkg: VirtioScsiDxe: drop 64-bit shift in PopulateRequest() (VS2010)
[mirror_edk2.git] / OvmfPkg / VirtioScsiDxe / VirtioScsi.c
index 66f6d31d74b3be684d06c6cc843941d80d0b25b8..2cb3f43bb01c25c29297f0e7027d11dcd5391b8d 100644 (file)
@@ -26,7 +26,7 @@
     unreasonable for now.\r
 \r
   Copyright (C) 2012, Red Hat, Inc.\r
-  Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2014, 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
@@ -38,7 +38,6 @@
 \r
 **/\r
 \r
-#include <IndustryStandard/Pci.h>\r
 #include <IndustryStandard/VirtioScsi.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 \r
 /**\r
 \r
-  Convenience macros to read and write region 0 IO space elements of the\r
-  virtio-scsi PCI device, for configuration purposes.\r
+  Convenience macros to read and write configuration elements of the\r
+  virtio-scsi VirtIo device.\r
 \r
   The following macros make it possible to specify only the "core parameters"\r
   for such accesses and to derive the rest. By the time VIRTIO_CFG_WRITE()\r
   returns, the transaction will have been completed.\r
 \r
-  @param[in] Dev       Pointer to the VSCSI_DEV structure whose PCI IO space\r
-                       we're accessing. Dev->PciIo must be valid.\r
+  @param[in] Dev       Pointer to the VSCSI_DEV structure.\r
 \r
   @param[in] Field     A field name from VSCSI_HDR, identifying the virtio-scsi\r
                        configuration item to access.\r
                        one of UINT8, UINT16, UINT32, UINT64.\r
 \r
 \r
-  @return  Status codes returned by VirtioWrite() / VirtioRead().\r
+  @return  Status codes returned by Virtio->WriteDevice() / Virtio->ReadDevice().\r
 \r
 **/\r
 \r
-#define VIRTIO_CFG_WRITE(Dev, Field, Value)  (VirtioWrite (              \\r
-                                                (Dev)->PciIo,            \\r
-                                                OFFSET_OF_VSCSI (Field), \\r
-                                                SIZE_OF_VSCSI (Field),   \\r
-                                                (Value)                  \\r
+#define VIRTIO_CFG_WRITE(Dev, Field, Value)  ((Dev)->VirtIo->WriteDevice (  \\r
+                                                (Dev)->VirtIo,              \\r
+                                                OFFSET_OF_VSCSI (Field),    \\r
+                                                SIZE_OF_VSCSI (Field),      \\r
+                                                (Value)                     \\r
                                                 ))\r
 \r
-#define VIRTIO_CFG_READ(Dev, Field, Pointer) (VirtioRead (               \\r
-                                                (Dev)->PciIo,            \\r
-                                                OFFSET_OF_VSCSI (Field), \\r
-                                                SIZE_OF_VSCSI (Field),   \\r
-                                                sizeof *(Pointer),       \\r
-                                                (Pointer)                \\r
+#define VIRTIO_CFG_READ(Dev, Field, Pointer) ((Dev)->VirtIo->ReadDevice (   \\r
+                                                (Dev)->VirtIo,              \\r
+                                                OFFSET_OF_VSCSI (Field),    \\r
+                                                SIZE_OF_VSCSI (Field),      \\r
+                                                sizeof *(Pointer),          \\r
+                                                (Pointer)                   \\r
                                                 ))\r
 \r
 \r
@@ -255,7 +253,7 @@ PopulateRequest (
   //\r
   Request->Lun[0] = 1;\r
   Request->Lun[1] = (UINT8) Target;\r
-  Request->Lun[2] = (UINT8) ((Lun >> 8) | 0x40);\r
+  Request->Lun[2] = (UINT8) (((UINT32)Lun >> 8) | 0x40);\r
   Request->Lun[3] = (UINT8) Lun;\r
 \r
   //\r
@@ -413,19 +411,8 @@ VirtioScsiPassThru (
   volatile VIRTIO_SCSI_RESP Response;\r
   DESC_INDICES              Indices;\r
 \r
-  //\r
-  // Zero-initialization of Request & Response with "= { 0 };" doesn't build\r
-  // with gcc-4.4: "undefined reference to `memset'". Direct SetMem() is not\r
-  // allowed as it would cast away the volatile qualifier. Work it around.\r
-  //\r
-  union {\r
-    VIRTIO_SCSI_REQ  Request;\r
-    VIRTIO_SCSI_RESP Response;\r
-  } Zero;\r
-\r
-  SetMem (&Zero, sizeof Zero, 0x00);\r
-  Request  = Zero.Request;\r
-  Response = Zero.Response;\r
+  ZeroMem ((VOID*) &Request, sizeof (Request));\r
+  ZeroMem ((VOID*) &Response, sizeof (Response));\r
 \r
   Dev = VIRTIO_SCSI_FROM_PASS_THRU (This);\r
   CopyMem (&TargetValue, Target, sizeof TargetValue);\r
@@ -482,7 +469,7 @@ VirtioScsiPassThru (
   // EFI_NOT_READY would save us the effort, but it would also suggest that the\r
   // caller retry.\r
   //\r
-  if (VirtioFlush (Dev->PciIo, VIRTIO_SCSI_REQUEST_QUEUE, &Dev->Ring,\r
+  if (VirtioFlush (Dev->VirtIo, VIRTIO_SCSI_REQUEST_QUEUE, &Dev->Ring,\r
         &Indices) != EFI_SUCCESS) {\r
     Packet->InTransferLength  = 0;\r
     Packet->OutTransferLength = 0;\r
@@ -729,19 +716,27 @@ VirtioScsiInit (
   // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence.\r
   //\r
   NextDevStat = 0;             // step 1 -- reset device\r
-  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
 \r
   NextDevStat |= VSTAT_ACK;    // step 2 -- acknowledge device presence\r
-  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
 \r
   NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it\r
-  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Failed;\r
+  }\r
+\r
+  //\r
+  // Set Page Size - MMIO VirtIo Specific\r
+  //\r
+  Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
@@ -749,13 +744,13 @@ VirtioScsiInit (
   //\r
   // step 4a -- retrieve and validate features\r
   //\r
-  Status = VIRTIO_CFG_READ (Dev, Generic.VhdrDeviceFeatureBits, &Features);\r
+  Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
-  Dev->InOutSupported = !!(Features & VIRTIO_SCSI_F_INOUT);\r
+  Dev->InOutSupported = (BOOLEAN) ((Features & VIRTIO_SCSI_F_INOUT) != 0);\r
 \r
-  Status = VIRTIO_CFG_READ (Dev, VhdrMaxChannel, &MaxChannel);\r
+  Status = VIRTIO_CFG_READ (Dev, MaxChannel, &MaxChannel);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
@@ -767,7 +762,7 @@ VirtioScsiInit (
     goto Failed;\r
   }\r
 \r
-  Status = VIRTIO_CFG_READ (Dev, VhdrNumQueues, &NumQueues);\r
+  Status = VIRTIO_CFG_READ (Dev, NumQueues, &NumQueues);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
@@ -776,7 +771,7 @@ VirtioScsiInit (
     goto Failed;\r
   }\r
 \r
-  Status = VIRTIO_CFG_READ (Dev, VhdrMaxTarget, &Dev->MaxTarget);\r
+  Status = VIRTIO_CFG_READ (Dev, MaxTarget, &Dev->MaxTarget);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
@@ -784,7 +779,7 @@ VirtioScsiInit (
     Dev->MaxTarget = PcdGet16 (PcdVirtioScsiMaxTargetLimit);\r
   }\r
 \r
-  Status = VIRTIO_CFG_READ (Dev, VhdrMaxLun, &Dev->MaxLun);\r
+  Status = VIRTIO_CFG_READ (Dev, MaxLun, &Dev->MaxLun);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
@@ -792,7 +787,7 @@ VirtioScsiInit (
     Dev->MaxLun = PcdGet32 (PcdVirtioScsiMaxLunLimit);\r
   }\r
 \r
-  Status = VIRTIO_CFG_READ (Dev, VhdrMaxSectors, &Dev->MaxSectors);\r
+  Status = VIRTIO_CFG_READ (Dev, MaxSectors, &Dev->MaxSectors);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
@@ -808,12 +803,11 @@ VirtioScsiInit (
   //\r
   // step 4b -- allocate request virtqueue\r
   //\r
-  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrQueueSelect,\r
-             VIRTIO_SCSI_REQUEST_QUEUE);\r
+  Status = Dev->VirtIo->SetQueueSel (Dev->VirtIo, VIRTIO_SCSI_REQUEST_QUEUE);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
-  Status = VIRTIO_CFG_READ (Dev, Generic.VhdrQueueSize, &QueueSize);\r
+  Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);\r
   if (EFI_ERROR (Status)) {\r
     goto Failed;\r
   }\r
@@ -831,11 +825,24 @@ VirtioScsiInit (
   }\r
 \r
   //\r
-  // step 4c -- Report GPFN (guest-physical frame number) of queue. If anything\r
-  // fails from here on, we must release the ring resources.\r
+  // Additional steps for MMIO: align the queue appropriately, and set the\r
+  // size. If anything fails from here on, we must release the ring resources.\r
+  //\r
+  Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ReleaseQueue;\r
+  }\r
+\r
+  Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ReleaseQueue;\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) Dev->Ring.Base >> EFI_PAGE_SHIFT);\r
+  Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo,\r
+      (UINT32) ((UINTN) Dev->Ring.Base >> EFI_PAGE_SHIFT));\r
   if (EFI_ERROR (Status)) {\r
     goto ReleaseQueue;\r
   }\r
@@ -845,8 +852,8 @@ VirtioScsiInit (
   // the known (or unknown) VIRTIO_SCSI_F_* or VIRTIO_F_* capabilities (see\r
   // virtio-0.9.5, Appendices B and I), except bidirectional transfers.\r
   //\r
-  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrGuestFeatureBits,\r
-             Features & VIRTIO_SCSI_F_INOUT);\r
+  Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo,\r
+      Features & VIRTIO_SCSI_F_INOUT);\r
   if (EFI_ERROR (Status)) {\r
     goto ReleaseQueue;\r
   }\r
@@ -855,11 +862,11 @@ VirtioScsiInit (
   // We expect these maximum sizes from the host. Since they are\r
   // guest-negotiable, ask for them rather than just checking them.\r
   //\r
-  Status = VIRTIO_CFG_WRITE (Dev, VhdrCdbSize, VIRTIO_SCSI_CDB_SIZE);\r
+  Status = VIRTIO_CFG_WRITE (Dev, CdbSize, VIRTIO_SCSI_CDB_SIZE);\r
   if (EFI_ERROR (Status)) {\r
     goto ReleaseQueue;\r
   }\r
-  Status = VIRTIO_CFG_WRITE (Dev, VhdrSenseSize, VIRTIO_SCSI_SENSE_SIZE);\r
+  Status = VIRTIO_CFG_WRITE (Dev, SenseSize, VIRTIO_SCSI_SENSE_SIZE);\r
   if (EFI_ERROR (Status)) {\r
     goto ReleaseQueue;\r
   }\r
@@ -868,7 +875,7 @@ VirtioScsiInit (
   // step 6 -- initialization complete\r
   //\r
   NextDevStat |= VSTAT_DRIVER_OK;\r
-  Status = VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
   if (EFI_ERROR (Status)) {\r
     goto ReleaseQueue;\r
   }\r
@@ -912,10 +919,10 @@ ReleaseQueue:
 Failed:\r
   //\r
   // Notify the host about our failure to setup: virtio-0.9.5, 2.2.2.1 Device\r
-  // Status. PCI IO access failure here should not mask the original error.\r
+  // Status. VirtIo access failure here should not mask the original error.\r
   //\r
   NextDevStat |= VSTAT_FAILED;\r
-  VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, NextDevStat);\r
+  Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
 \r
   Dev->InOutSupported = FALSE;\r
   Dev->MaxTarget      = 0;\r
@@ -939,7 +946,7 @@ VirtioScsiUninit (
   // VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from\r
   // the old comms area.\r
   //\r
-  VIRTIO_CFG_WRITE (Dev, Generic.VhdrDeviceStatus, 0);\r
+  Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
 \r
   Dev->InOutSupported = FALSE;\r
   Dev->MaxTarget      = 0;\r
@@ -964,11 +971,8 @@ VirtioScsiUninit (
 // The implementation follows:\r
 // - Driver Writer's Guide for UEFI 2.3.1 v1.01\r
 //   - 5.1.3.4 OpenProtocol() and CloseProtocol()\r
-//   - 18      PCI Driver Design Guidelines\r
-//   - 18.3    PCI drivers\r
 // - UEFI Spec 2.3.1 + Errata C\r
 //   -  6.3 Protocol Handler Services\r
-//   - 13.4 EFI PCI I/O Protocol\r
 //\r
 \r
 EFI_STATUS\r
@@ -979,57 +983,37 @@ VirtioScsiDriverBindingSupported (
   IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath\r
   )\r
 {\r
-  EFI_STATUS          Status;\r
-  EFI_PCI_IO_PROTOCOL *PciIo;\r
-  PCI_TYPE00          Pci;\r
+  EFI_STATUS             Status;\r
+  VIRTIO_DEVICE_PROTOCOL *VirtIo;\r
 \r
   //\r
-  // Attempt to open the device with the PciIo set of interfaces. On success,\r
-  // the protocol is "instantiated" for the PCI device. Covers duplicate open\r
+  // Attempt to open the device with the VirtIo set of interfaces. On success,\r
+  // the protocol is "instantiated" for the VirtIo device. Covers duplicate open\r
   // attempts (EFI_ALREADY_STARTED).\r
   //\r
   Status = gBS->OpenProtocol (\r
                   DeviceHandle,               // candidate device\r
-                  &gEfiPciIoProtocolGuid,     // for generic PCI access\r
-                  (VOID **)&PciIo,            // handle to instantiate\r
+                  &gVirtioDeviceProtocolGuid, // for generic VirtIo access\r
+                  (VOID **)&VirtIo,           // handle to instantiate\r
                   This->DriverBindingHandle,  // requestor driver identity\r
                   DeviceHandle,               // ControllerHandle, according to\r
                                               // the UEFI Driver Model\r
-                  EFI_OPEN_PROTOCOL_BY_DRIVER // get exclusive PciIo access to\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER // get exclusive VirtIo access to\r
                                               // the device; to be released\r
                   );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  //\r
-  // Read entire PCI configuration header for more extensive check ahead.\r
-  //\r
-  Status = PciIo->Pci.Read (\r
-                        PciIo,                        // (protocol, device)\r
-                                                      // handle\r
-                        EfiPciIoWidthUint32,          // access width & copy\r
-                                                      // mode\r
-                        0,                            // Offset\r
-                        sizeof Pci / sizeof (UINT32), // Count\r
-                        &Pci                          // target buffer\r
-                        );\r
-\r
-  if (Status == EFI_SUCCESS) {\r
-    //\r
-    // virtio-0.9.5, 2.1 PCI Discovery\r
-    //\r
-    Status = (Pci.Hdr.VendorId == 0x1AF4 &&\r
-              Pci.Hdr.DeviceId >= 0x1000 && Pci.Hdr.DeviceId <= 0x103F &&\r
-              Pci.Hdr.RevisionID == 0x00 &&\r
-              Pci.Device.SubsystemID == 0x08) ? EFI_SUCCESS : EFI_UNSUPPORTED;\r
+  if (VirtIo->SubSystemDeviceId != VIRTIO_SUBSYSTEM_SCSI_HOST) {\r
+    Status = EFI_UNSUPPORTED;\r
   }\r
 \r
   //\r
-  // We needed PCI IO access only transitorily, to see whether we support the\r
+  // We needed VirtIo access only transitorily, to see whether we support the\r
   // device or not.\r
   //\r
-  gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r
+  gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
          This->DriverBindingHandle, DeviceHandle);\r
   return Status;\r
 }\r
@@ -1051,43 +1035,19 @@ VirtioScsiDriverBindingStart (
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  Status = gBS->OpenProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r
-                  (VOID **)&Dev->PciIo, This->DriverBindingHandle,\r
+  Status = gBS->OpenProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
+                  (VOID **)&Dev->VirtIo, This->DriverBindingHandle,\r
                   DeviceHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);\r
   if (EFI_ERROR (Status)) {\r
     goto FreeVirtioScsi;\r
   }\r
 \r
   //\r
-  // We must retain and ultimately restore the original PCI attributes of the\r
-  // device. See Driver Writer's Guide for UEFI 2.3.1 v1.01, 18.3 PCI drivers /\r
-  // 18.3.2 Start() and Stop().\r
-  //\r
-  // The third parameter ("Attributes", input) is ignored by the Get operation.\r
-  // The fourth parameter ("Result", output) is ignored by the Enable and Set\r
-  // operations.\r
-  //\r
-  // For virtio-scsi we only need IO space access.\r
-  //\r
-  Status = Dev->PciIo->Attributes (Dev->PciIo, EfiPciIoAttributeOperationGet,\r
-                         0, &Dev->OriginalPciAttributes);\r
-  if (EFI_ERROR (Status)) {\r
-    goto ClosePciIo;\r
-  }\r
-\r
-  Status = Dev->PciIo->Attributes (Dev->PciIo,\r
-                         EfiPciIoAttributeOperationEnable,\r
-                         EFI_PCI_IO_ATTRIBUTE_IO, NULL);\r
-  if (EFI_ERROR (Status)) {\r
-    goto ClosePciIo;\r
-  }\r
-\r
-  //\r
-  // PCI IO access granted, configure virtio-scsi device.\r
+  // VirtIo access granted, configure virtio-scsi device.\r
   //\r
   Status = VirtioScsiInit (Dev);\r
   if (EFI_ERROR (Status)) {\r
-    goto RestorePciAttributes;\r
+    goto CloseVirtIo;\r
   }\r
 \r
   //\r
@@ -1107,12 +1067,8 @@ VirtioScsiDriverBindingStart (
 UninitDev:\r
   VirtioScsiUninit (Dev);\r
 \r
-RestorePciAttributes:\r
-  Dev->PciIo->Attributes (Dev->PciIo, EfiPciIoAttributeOperationSet,\r
-                Dev->OriginalPciAttributes, NULL);\r
-\r
-ClosePciIo:\r
-  gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r
+CloseVirtIo:\r
+  gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
          This->DriverBindingHandle, DeviceHandle);\r
 \r
 FreeVirtioScsi:\r
@@ -1160,10 +1116,7 @@ VirtioScsiDriverBindingStop (
 \r
   VirtioScsiUninit (Dev);\r
 \r
-  Dev->PciIo->Attributes (Dev->PciIo, EfiPciIoAttributeOperationSet,\r
-                Dev->OriginalPciAttributes, NULL);\r
-\r
-  gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r
+  gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
          This->DriverBindingHandle, DeviceHandle);\r
 \r
   FreePool (Dev);\r