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