Blame SOURCES/kvm-hw-scsi-add-VPD-Block-Limits-emulation.patch

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