cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
26ba25
From 51c8629ca55d0b6e51faffcddfc5e3b10898fc5c Mon Sep 17 00:00:00 2001
26ba25
From: Paolo Bonzini <pbonzini@redhat.com>
26ba25
Date: Thu, 20 Dec 2018 12:30:58 +0000
26ba25
Subject: [PATCH 3/8] hw/scsi: add VPD Block Limits emulation
26ba25
26ba25
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
26ba25
Message-id: <20181220123103.29579-4-pbonzini@redhat.com>
26ba25
Patchwork-id: 83713
26ba25
O-Subject: [PATCH 3/8] hw/scsi: add VPD Block Limits emulation
26ba25
Bugzilla: 1639957
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
26ba25
From: Daniel Henrique Barboza <danielhb413@gmail.com>
26ba25
26ba25
The VPD Block Limits Inquiry page is optional, allowing SCSI devices
26ba25
to not implement it. This is the case for devices like the MegaRAID
26ba25
SAS 9361-8i and Microsemi PM8069.
26ba25
26ba25
In case of SCSI passthrough, the response of this request is used by
26ba25
the QEMU SCSI layer to set the max_io_sectors that the guest
26ba25
device will support, based on the value of the max_sectors_kb that
26ba25
the device has set in the host at that time. Without this response,
26ba25
the guest kernel is free to assume any value of max_io_sectors
26ba25
for the SCSI device. If this value is greater than the value from
26ba25
the host, SCSI Sense errors will occur because the guest will send
26ba25
read/write requests that are larger than the underlying host device
26ba25
is configured to support. An example of this behavior can be seen
26ba25
in [1].
26ba25
26ba25
A workaround is to set the max_sectors_kb host value back in the guest
26ba25
kernel (a process that can be automated using rc.local startup scripts
26ba25
and the like), but this has several drawbacks:
26ba25
26ba25
- it can be troublesome if the guest has many passthrough devices that
26ba25
needs this tuning;
26ba25
26ba25
- if a change in max_sectors_kb is made in the host side, manual change
26ba25
in the guests will also be required;
26ba25
26ba25
- during an OS install it is difficult, and sometimes not possible, to
26ba25
go to a terminal and change the max_sectors_kb prior to the installation.
26ba25
This means that the disk can't be used during the install process. The
26ba25
easiest alternative here is to roll back to scsi-hd, install the guest
26ba25
and then go back to SCSI passthrough when the installation is done and
26ba25
max_sectors_kb can be set.
26ba25
26ba25
An easier way would be to QEMU handle the absence of the Block Limits
26ba25
VPD device response, setting max_io_sectors accordingly and allowing
26ba25
the guest to use the device without the hassle.
26ba25
26ba25
This patch adds emulation of the Block Limits VPD response for
26ba25
SCSI passthrough devices of type TYPE_DISK that doesn't support
26ba25
it. The following changes were made:
26ba25
26ba25
- scsi_handle_inquiry_reply will now check the available VPD
26ba25
pages from the Inquiry EVPD reply. In case the device does not
26ba25
26ba25
- a new function called scsi_generic_set_vpd_bl_emulation,
26ba25
that is called during device realize,  was created to set a
26ba25
new flag 'needs_vpd_bl_emulation' of the device. This function
26ba25
retrieves the Inquiry EVPD response of the device to check for
26ba25
VPD BL support.
26ba25
26ba25
- scsi_handle_inquiry_reply will now check the available VPD
26ba25
pages from the Inquiry EVPD reply in case the device needs
26ba25
VPD BL emulation, adding the Block Limits page (0xb0) to
26ba25
the list. This will make the guest kernel aware of the
26ba25
support that we're now providing by emulation.
26ba25
26ba25
- a new function scsi_emulate_block_limits creates the
26ba25
emulated Block Limits response. This function is called
26ba25
inside scsi_read_complete in case the device requires
26ba25
Block Limits VPD emulation and we detected a SCSI Sense
26ba25
error in the VPD Block Limits reply that was issued
26ba25
from the guest kernel to the device. This error is
26ba25
expected: we're reporting support from our side, but
26ba25
the device isn't aware of it.
26ba25
26ba25
With this patch, the guest now queries the Block Limits
26ba25
page during the device configuration because it is being
26ba25
advertised in the Supported Pages response. It will either
26ba25
receive the Block Limits page from the hardware, if it supports
26ba25
it, or will receive an emulated response from QEMU. At any rate,
26ba25
the guest now has the information to set the max_sectors_kb
26ba25
parameter accordingly, sparing the user of SCSI sense errors
26ba25
that would happen without the emulated response and in the
26ba25
absence of Block Limits support from the hardware.
26ba25
26ba25
[1] https://bugzilla.redhat.com/show_bug.cgi?id=1566195
26ba25
26ba25
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1566195
26ba25
Reported-by: Dac Nguyen <dacng@us.ibm.com>
26ba25
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
26ba25
Message-Id: <20180627172432.11120-4-danielhb413@gmail.com>
26ba25
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
26ba25
(cherry picked from commit a71c775b24ebc664129eb1d9b4c360590353efd5)
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 hw/scsi/scsi-disk.c    |   2 +-
26ba25
 hw/scsi/scsi-generic.c | 132 +++++++++++++++++++++++++++++++++++++++++++++----
26ba25
 include/hw/scsi/scsi.h |   3 +-
26ba25
 3 files changed, 125 insertions(+), 12 deletions(-)
26ba25
26ba25
diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
26ba25
index ea86849..b3d53ec 100644
26ba25
--- a/hw/scsi/scsi-disk.c
26ba25
+++ b/hw/scsi/scsi-disk.c
26ba25
@@ -2646,7 +2646,7 @@ static void scsi_block_realize(SCSIDevice *dev, Error **errp)
26ba25
     s->features |= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS);
26ba25
 
26ba25
     scsi_realize(&s->qdev, errp);
26ba25
-    scsi_generic_read_device_identification(&s->qdev);
26ba25
+    scsi_generic_read_device_inquiry(&s->qdev);
26ba25
 }
26ba25
 
26ba25
 typedef struct SCSIBlockReq {
26ba25
diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c
26ba25
index c6307a8..4266003 100644
26ba25
--- a/hw/scsi/scsi-generic.c
26ba25
+++ b/hw/scsi/scsi-generic.c
26ba25
@@ -145,6 +145,8 @@ static int execute_command(BlockBackend *blk,
26ba25
 
26ba25
 static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s)
26ba25
 {
26ba25
+    uint8_t page, page_len;
26ba25
+
26ba25
     /*
26ba25
      *  EVPD set to zero returns the standard INQUIRY data.
26ba25
      *
26ba25
@@ -168,22 +170,57 @@ static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s)
26ba25
             s->scsi_version = r->buf[2];
26ba25
         }
26ba25
     }
26ba25
-    if (s->type == TYPE_DISK && r->req.cmd.buf[2] == 0xb0) {
26ba25
-        uint32_t max_transfer =
26ba25
-            blk_get_max_transfer(s->conf.blk) / s->blocksize;
26ba25
 
26ba25
-        assert(max_transfer);
26ba25
-        stl_be_p(&r->buf[8], max_transfer);
26ba25
-        /* Also take care of the opt xfer len. */
26ba25
-        stl_be_p(&r->buf[12],
26ba25
-                 MIN_NON_ZERO(max_transfer, ldl_be_p(&r->buf[12])));
26ba25
+    if (s->type == TYPE_DISK && (r->req.cmd.buf[1] & 0x01)) {
26ba25
+        page = r->req.cmd.buf[2];
26ba25
+        if (page == 0xb0) {
26ba25
+            uint32_t max_transfer =
26ba25
+                blk_get_max_transfer(s->conf.blk) / s->blocksize;
26ba25
+
26ba25
+            assert(max_transfer);
26ba25
+            stl_be_p(&r->buf[8], max_transfer);
26ba25
+            /* Also take care of the opt xfer len. */
26ba25
+            stl_be_p(&r->buf[12],
26ba25
+                    MIN_NON_ZERO(max_transfer, ldl_be_p(&r->buf[12])));
26ba25
+        } else if (page == 0x00 && s->needs_vpd_bl_emulation) {
26ba25
+            /*
26ba25
+             * Now we're capable of supplying the VPD Block Limits
26ba25
+             * response if the hardware can't. Add it in the INQUIRY
26ba25
+             * Supported VPD pages response in case we are using the
26ba25
+             * emulation for this device.
26ba25
+             *
26ba25
+             * This way, the guest kernel will be aware of the support
26ba25
+             * and will use it to proper setup the SCSI device.
26ba25
+             */
26ba25
+            page_len = r->buf[3];
26ba25
+            r->buf[page_len + 4] = 0xb0;
26ba25
+            r->buf[3] = ++page_len;
26ba25
+        }
26ba25
     }
26ba25
 }
26ba25
 
26ba25
+static int scsi_emulate_block_limits(SCSIGenericReq *r)
26ba25
+{
26ba25
+    r->buflen = scsi_disk_emulate_vpd_page(&r->req, r->buf);
26ba25
+    r->io_header.sb_len_wr = 0;
26ba25
+
26ba25
+    /*
26ba25
+    * We have valid contents in the reply buffer but the
26ba25
+    * io_header can report a sense error coming from
26ba25
+    * the hardware in scsi_command_complete_noio. Clean
26ba25
+    * up the io_header to avoid reporting it.
26ba25
+    */
26ba25
+    r->io_header.driver_status = 0;
26ba25
+    r->io_header.status = 0;
26ba25
+
26ba25
+    return r->buflen;
26ba25
+}
26ba25
+
26ba25
 static void scsi_read_complete(void * opaque, int ret)
26ba25
 {
26ba25
     SCSIGenericReq *r = (SCSIGenericReq *)opaque;
26ba25
     SCSIDevice *s = r->req.dev;
26ba25
+    SCSISense sense;
26ba25
     int len;
26ba25
 
26ba25
     assert(r->req.aiocb != NULL);
26ba25
@@ -200,6 +237,27 @@ static void scsi_read_complete(void * opaque, int ret)
26ba25
     DPRINTF("Data ready tag=0x%x len=%d\n", r->req.tag, len);
26ba25
 
26ba25
     r->len = -1;
26ba25
+
26ba25
+    /*
26ba25
+     * Check if this is a VPD Block Limits request that
26ba25
+     * resulted in sense error but would need emulation.
26ba25
+     * In this case, emulate a valid VPD response.
26ba25
+     */
26ba25
+    if (s->needs_vpd_bl_emulation) {
26ba25
+        int is_vpd_bl = r->req.cmd.buf[0] == INQUIRY &&
26ba25
+                         r->req.cmd.buf[1] & 0x01 &&
26ba25
+                         r->req.cmd.buf[2] == 0xb0;
26ba25
+
26ba25
+        if (is_vpd_bl && sg_io_sense_from_errno(-ret, &r->io_header, &sense)) {
26ba25
+            len = scsi_emulate_block_limits(r);
26ba25
+            /*
26ba25
+             * No need to let scsi_read_complete go on and handle an
26ba25
+             * INQUIRY VPD BL request we created manually.
26ba25
+             */
26ba25
+            goto req_complete;
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
     if (len == 0) {
26ba25
         scsi_command_complete_noio(r, 0);
26ba25
         goto done;
26ba25
@@ -234,6 +292,8 @@ static void scsi_read_complete(void * opaque, int ret)
26ba25
     if (r->req.cmd.buf[0] == INQUIRY) {
26ba25
         scsi_handle_inquiry_reply(r, s);
26ba25
     }
26ba25
+
26ba25
+req_complete:
26ba25
     scsi_req_data(&r->req, len);
26ba25
     scsi_req_unref(&r->req);
26ba25
 
26ba25
@@ -435,7 +495,49 @@ int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
-void scsi_generic_read_device_identification(SCSIDevice *s)
26ba25
+/*
26ba25
+ * Executes an INQUIRY request with EVPD set to retrieve the
26ba25
+ * available VPD pages of the device. If the device does
26ba25
+ * not support the Block Limits page (page 0xb0), set
26ba25
+ * the needs_vpd_bl_emulation flag for future use.
26ba25
+ */
26ba25
+static void scsi_generic_set_vpd_bl_emulation(SCSIDevice *s)
26ba25
+{
26ba25
+    uint8_t cmd[6];
26ba25
+    uint8_t buf[250];
26ba25
+    uint8_t page_len;
26ba25
+    int ret, i;
26ba25
+
26ba25
+    memset(cmd, 0, sizeof(cmd));
26ba25
+    memset(buf, 0, sizeof(buf));
26ba25
+    cmd[0] = INQUIRY;
26ba25
+    cmd[1] = 1;
26ba25
+    cmd[2] = 0x00;
26ba25
+    cmd[4] = sizeof(buf);
26ba25
+
26ba25
+    ret = scsi_SG_IO_FROM_DEV(s->conf.blk, cmd, sizeof(cmd),
26ba25
+                              buf, sizeof(buf));
26ba25
+    if (ret < 0) {
26ba25
+        /*
26ba25
+         * Do not assume anything if we can't retrieve the
26ba25
+         * INQUIRY response to assert the VPD Block Limits
26ba25
+         * support.
26ba25
+         */
26ba25
+        s->needs_vpd_bl_emulation = false;
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    page_len = buf[3];
26ba25
+    for (i = 4; i < page_len + 4; i++) {
26ba25
+        if (buf[i] == 0xb0) {
26ba25
+            s->needs_vpd_bl_emulation = false;
26ba25
+            return;
26ba25
+        }
26ba25
+    }
26ba25
+    s->needs_vpd_bl_emulation = true;
26ba25
+}
26ba25
+
26ba25
+static void scsi_generic_read_device_identification(SCSIDevice *s)
26ba25
 {
26ba25
     uint8_t cmd[6];
26ba25
     uint8_t buf[250];
26ba25
@@ -480,6 +582,16 @@ void scsi_generic_read_device_identification(SCSIDevice *s)
26ba25
     }
26ba25
 }
26ba25
 
26ba25
+void scsi_generic_read_device_inquiry(SCSIDevice *s)
26ba25
+{
26ba25
+    scsi_generic_read_device_identification(s);
26ba25
+    if (s->type == TYPE_DISK) {
26ba25
+        scsi_generic_set_vpd_bl_emulation(s);
26ba25
+    } else {
26ba25
+        s->needs_vpd_bl_emulation = false;
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
 static int get_stream_blocksize(BlockBackend *blk)
26ba25
 {
26ba25
     uint8_t cmd[6];
26ba25
@@ -581,7 +693,7 @@ static void scsi_generic_realize(SCSIDevice *s, Error **errp)
26ba25
 
26ba25
     /* Only used by scsi-block, but initialize it nevertheless to be clean.  */
26ba25
     s->default_scsi_version = -1;
26ba25
-    scsi_generic_read_device_identification(s);
26ba25
+    scsi_generic_read_device_inquiry(s);
26ba25
 }
26ba25
 
26ba25
 const SCSIReqOps scsi_generic_req_ops = {
26ba25
diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h
26ba25
index b6e05c4..ee3a411 100644
26ba25
--- a/include/hw/scsi/scsi.h
26ba25
+++ b/include/hw/scsi/scsi.h
26ba25
@@ -87,6 +87,7 @@ struct SCSIDevice
26ba25
     uint64_t port_wwn;
26ba25
     int scsi_version;
26ba25
     int default_scsi_version;
26ba25
+    bool needs_vpd_bl_emulation;
26ba25
 };
26ba25
 
26ba25
 extern const VMStateDescription vmstate_scsi_device;
26ba25
@@ -186,7 +187,7 @@ void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
26ba25
 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
26ba25
 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
26ba25
 void scsi_device_unit_attention_reported(SCSIDevice *dev);
26ba25
-void scsi_generic_read_device_identification(SCSIDevice *dev);
26ba25
+void scsi_generic_read_device_inquiry(SCSIDevice *dev);
26ba25
 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
26ba25
 int scsi_disk_emulate_vpd_page(SCSIRequest *req, uint8_t *outbuf);
26ba25
 int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
26ba25
-- 
26ba25
1.8.3.1
26ba25