diff options
author | Brijesh Singh <brijesh.singh@amd.com> | 2017-08-31 10:25:27 -0400 |
---|---|---|
committer | Laszlo Ersek <lersek@redhat.com> | 2017-08-31 21:28:26 +0200 |
commit | 9d0d5050c73b142ea03c82cd078be8c703a556f4 (patch) | |
tree | 2624a34084334054413de5a5462f712160bf7b54 /OvmfPkg/VirtioScsiDxe | |
parent | 1b15eb06c7f2687ee92cf90f1581c7044691d71f (diff) | |
download | edk2-9d0d5050c73b142ea03c82cd078be8c703a556f4.zip edk2-9d0d5050c73b142ea03c82cd078be8c703a556f4.tar.gz edk2-9d0d5050c73b142ea03c82cd078be8c703a556f4.tar.bz2 |
OvmfPkg/VirtioScsiDxe: map virtio-scsi request and response buffers
When device is behind the IOMMU, driver is require to pass the device
address of virtio request, response and any memory referenced by those
request/response to the bus master.
The patch uses IOMMU-like member functions from VIRTIO_DEVICE_PROTOCOL to
map request and response buffers system physical address to the device
address.
- If the buffer need to be accessed by both the processor and a bus
master then map with BusMasterCommonBuffer.
- If the buffer need to be accessed for a write operation by a bus master
then map with BusMasterWrite.
However, after a BusMasterWrite Unmap() failure, error reporting via
EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET would be very complex,
therefore we map such buffers too with BusMasterCommonBuffer.
- If the buffer need to be accessed for a read operation by a bus master
then map with BusMasterRead.
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
[lersek@redhat.com: restore lost sentence/paragraph in commit message]
[lersek@redhat.com: reindent/reflow "InDataBuffer" comment block]
[lersek@redhat.com: cast arg, not result, of EFI_SIZE_TO_PAGES() to UINTN]
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Laszlo Ersek <lersek@redhat.com>
Diffstat (limited to 'OvmfPkg/VirtioScsiDxe')
-rw-r--r-- | OvmfPkg/VirtioScsiDxe/VirtioScsi.c | 221 |
1 files changed, 205 insertions, 16 deletions
diff --git a/OvmfPkg/VirtioScsiDxe/VirtioScsi.c b/OvmfPkg/VirtioScsiDxe/VirtioScsi.c index 5e5ad88..aa22f85 100644 --- a/OvmfPkg/VirtioScsiDxe/VirtioScsi.c +++ b/OvmfPkg/VirtioScsiDxe/VirtioScsi.c @@ -440,26 +440,157 @@ VirtioScsiPassThru ( UINT16 TargetValue;
EFI_STATUS Status;
volatile VIRTIO_SCSI_REQ Request;
- volatile VIRTIO_SCSI_RESP Response;
+ volatile VIRTIO_SCSI_RESP *Response;
+ VOID *ResponseBuffer;
DESC_INDICES Indices;
+ VOID *RequestMapping;
+ VOID *ResponseMapping;
+ VOID *InDataMapping;
+ VOID *OutDataMapping;
+ EFI_PHYSICAL_ADDRESS RequestDeviceAddress;
+ EFI_PHYSICAL_ADDRESS ResponseDeviceAddress;
+ EFI_PHYSICAL_ADDRESS InDataDeviceAddress;
+ EFI_PHYSICAL_ADDRESS OutDataDeviceAddress;
+ VOID *InDataBuffer;
+ UINTN InDataNumPages;
+ BOOLEAN OutDataBufferIsMapped;
ZeroMem ((VOID*) &Request, sizeof (Request));
- ZeroMem ((VOID*) &Response, sizeof (Response));
Dev = VIRTIO_SCSI_FROM_PASS_THRU (This);
CopyMem (&TargetValue, Target, sizeof TargetValue);
+ InDataBuffer = NULL;
+ OutDataBufferIsMapped = FALSE;
+ InDataNumPages = 0;
+
Status = PopulateRequest (Dev, TargetValue, Lun, Packet, &Request);
if (EFI_ERROR (Status)) {
return Status;
}
- VirtioPrepare (&Dev->Ring, &Indices);
+ //
+ // Map the virtio-scsi Request header buffer
+ //
+ Status = VirtioMapAllBytesInSharedBuffer (
+ Dev->VirtIo,
+ VirtioOperationBusMasterRead,
+ (VOID *) &Request,
+ sizeof Request,
+ &RequestDeviceAddress,
+ &RequestMapping);
+ if (EFI_ERROR (Status)) {
+ return ReportHostAdapterError (Packet);
+ }
+
+ //
+ // Map the input buffer
+ //
+ if (Packet->InTransferLength > 0) {
+ //
+ // Allocate a intermediate input buffer. This is mainly to handle the
+ // following case:
+ // * caller submits a bi-directional request
+ // * we perform the request fine
+ // * but we fail to unmap the "InDataMapping"
+ //
+ // In that case simply returing the EFI_DEVICE_ERROR is not sufficient. In
+ // addition to the error code we also need to update Packet fields
+ // accordingly so that we report the full loss of the incoming transfer.
+ //
+ // We allocate a temporary buffer and map it with BusMasterCommonBuffer. If
+ // the Virtio request is successful then we copy the data from temporary
+ // buffer into Packet->InDataBuffer.
+ //
+ InDataNumPages = EFI_SIZE_TO_PAGES ((UINTN)Packet->InTransferLength);
+ Status = Dev->VirtIo->AllocateSharedPages (
+ Dev->VirtIo,
+ InDataNumPages,
+ &InDataBuffer
+ );
+ if (EFI_ERROR (Status)) {
+ Status = ReportHostAdapterError (Packet);
+ goto UnmapRequestBuffer;
+ }
+
+ ZeroMem (InDataBuffer, Packet->InTransferLength);
+
+ Status = VirtioMapAllBytesInSharedBuffer (
+ Dev->VirtIo,
+ VirtioOperationBusMasterCommonBuffer,
+ InDataBuffer,
+ Packet->InTransferLength,
+ &InDataDeviceAddress,
+ &InDataMapping
+ );
+ if (EFI_ERROR (Status)) {
+ Status = ReportHostAdapterError (Packet);
+ goto FreeInDataBuffer;
+ }
+ }
+
+ //
+ // Map the output buffer
+ //
+ if (Packet->OutTransferLength > 0) {
+ Status = VirtioMapAllBytesInSharedBuffer (
+ Dev->VirtIo,
+ VirtioOperationBusMasterRead,
+ Packet->OutDataBuffer,
+ Packet->OutTransferLength,
+ &OutDataDeviceAddress,
+ &OutDataMapping
+ );
+ if (EFI_ERROR (Status)) {
+ Status = ReportHostAdapterError (Packet);
+ goto UnmapInDataBuffer;
+ }
+
+ OutDataBufferIsMapped = TRUE;
+ }
+
+ //
+ // Response header is bi-direction (we preset with host status and expect
+ // the device to update it). Allocate a response buffer which can be mapped
+ // to access equally by both processor and device.
+ //
+ Status = Dev->VirtIo->AllocateSharedPages (
+ Dev->VirtIo,
+ EFI_SIZE_TO_PAGES (sizeof *Response),
+ &ResponseBuffer
+ );
+ if (EFI_ERROR (Status)) {
+ Status = ReportHostAdapterError (Packet);
+ goto UnmapOutDataBuffer;
+ }
+
+ Response = ResponseBuffer;
+
+ ZeroMem ((VOID *)Response, sizeof (*Response));
//
// preset a host status for ourselves that we do not accept as success
//
- Response.Response = VIRTIO_SCSI_S_FAILURE;
+ Response->Response = VIRTIO_SCSI_S_FAILURE;
+
+ //
+ // Map the response buffer with BusMasterCommonBuffer so that response
+ // buffer can be accessed by both host and device.
+ //
+ Status = VirtioMapAllBytesInSharedBuffer (
+ Dev->VirtIo,
+ VirtioOperationBusMasterCommonBuffer,
+ ResponseBuffer,
+ sizeof (*Response),
+ &ResponseDeviceAddress,
+ &ResponseMapping
+ );
+ if (EFI_ERROR (Status)) {
+ Status = ReportHostAdapterError (Packet);
+ goto FreeResponseBuffer;
+ }
+
+ VirtioPrepare (&Dev->Ring, &Indices);
//
// ensured by VirtioScsiInit() -- this predicate, in combination with the
@@ -470,31 +601,49 @@ VirtioScsiPassThru ( //
// enqueue Request
//
- VirtioAppendDesc (&Dev->Ring, (UINTN) &Request, sizeof Request,
- VRING_DESC_F_NEXT, &Indices);
+ VirtioAppendDesc (
+ &Dev->Ring,
+ RequestDeviceAddress,
+ sizeof Request,
+ VRING_DESC_F_NEXT,
+ &Indices
+ );
//
// enqueue "dataout" if any
//
if (Packet->OutTransferLength > 0) {
- VirtioAppendDesc (&Dev->Ring, (UINTN) Packet->OutDataBuffer,
- Packet->OutTransferLength, VRING_DESC_F_NEXT, &Indices);
+ VirtioAppendDesc (
+ &Dev->Ring,
+ OutDataDeviceAddress,
+ Packet->OutTransferLength,
+ VRING_DESC_F_NEXT,
+ &Indices
+ );
}
//
// enqueue Response, to be written by the host
//
- VirtioAppendDesc (&Dev->Ring, (UINTN) &Response, sizeof Response,
- VRING_DESC_F_WRITE | (Packet->InTransferLength > 0 ?
- VRING_DESC_F_NEXT : 0),
- &Indices);
+ VirtioAppendDesc (
+ &Dev->Ring,
+ ResponseDeviceAddress,
+ sizeof *Response,
+ VRING_DESC_F_WRITE | (Packet->InTransferLength > 0 ? VRING_DESC_F_NEXT : 0),
+ &Indices
+ );
//
// enqueue "datain" if any, to be written by the host
//
if (Packet->InTransferLength > 0) {
- VirtioAppendDesc (&Dev->Ring, (UINTN) Packet->InDataBuffer,
- Packet->InTransferLength, VRING_DESC_F_WRITE, &Indices);
+ VirtioAppendDesc (
+ &Dev->Ring,
+ InDataDeviceAddress,
+ Packet->InTransferLength,
+ VRING_DESC_F_WRITE,
+ &Indices
+ );
}
// If kicking the host fails, we must fake a host adapter error.
@@ -503,10 +652,50 @@ VirtioScsiPassThru ( //
if (VirtioFlush (Dev->VirtIo, VIRTIO_SCSI_REQUEST_QUEUE, &Dev->Ring,
&Indices, NULL) != EFI_SUCCESS) {
- return ReportHostAdapterError (Packet);
+ Status = ReportHostAdapterError (Packet);
+ goto UnmapResponseBuffer;
+ }
+
+ Status = ParseResponse (Packet, Response);
+
+ //
+ // If virtio request was successful and it was a CPU read request then we
+ // have used an intermediate buffer. Copy the data from intermediate buffer
+ // to the final buffer.
+ //
+ if (InDataBuffer != NULL) {
+ CopyMem (Packet->InDataBuffer, InDataBuffer, Packet->InTransferLength);
+ }
+
+UnmapResponseBuffer:
+ Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, ResponseMapping);
+
+FreeResponseBuffer:
+ Dev->VirtIo->FreeSharedPages (
+ Dev->VirtIo,
+ EFI_SIZE_TO_PAGES (sizeof *Response),
+ ResponseBuffer
+ );
+
+UnmapOutDataBuffer:
+ if (OutDataBufferIsMapped) {
+ Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, OutDataMapping);
+ }
+
+UnmapInDataBuffer:
+ if (InDataBuffer != NULL) {
+ Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, InDataMapping);
}
- return ParseResponse (Packet, &Response);
+FreeInDataBuffer:
+ if (InDataBuffer != NULL) {
+ Dev->VirtIo->FreeSharedPages (Dev->VirtIo, InDataNumPages, InDataBuffer);
+ }
+
+UnmapRequestBuffer:
+ Dev->VirtIo->UnmapSharedBuffer (Dev->VirtIo, RequestMapping);
+
+ return Status;
}
|