Blame SOURCES/kvm-hw-scsi-centralize-SG_IO-calls-into-single-function.patch

7711c0
From bf544897c8e513b68b9262f6f9315b8a49421429 Mon Sep 17 00:00:00 2001
7711c0
From: Paolo Bonzini <pbonzini@redhat.com>
7711c0
Date: Wed, 7 Nov 2018 18:00:01 +0100
7711c0
Subject: [PATCH 27/34] hw/scsi: centralize SG_IO calls into single function
7711c0
MIME-Version: 1.0
7711c0
Content-Type: text/plain; charset=UTF-8
7711c0
Content-Transfer-Encoding: 8bit
7711c0
7711c0
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
7711c0
Message-id: <20181107180007.22954-4-pbonzini@redhat.com>
7711c0
Patchwork-id: 82947
7711c0
O-Subject: [RHEL7.6.z qemu-kvm-rhev PATCH 3/9] hw/scsi: centralize SG_IO calls into single function
7711c0
Bugzilla: 1566195
7711c0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
7711c0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
7711c0
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
7711c0
7711c0
From: Daniel Henrique Barboza <danielhb413@gmail.com>
7711c0
7711c0
For the VPD Block Limits emulation with SCSI passthrough,
7711c0
we'll issue an Inquiry request with EVPD set to retrieve
7711c0
the available VPD pages of the device. This would be done in
7711c0
a way similar of what scsi_generic_read_device_identification
7711c0
does: create a SCSI command and a reply buffer, fill in the
7711c0
sg_io_hdr_t structure, call blk_ioctl, check if an error
7711c0
occurred, process the response.
7711c0
7711c0
This same process is done in other 2 functions, get_device_type
7711c0
and get_stream_blocksize. They differ in the command/reply
7711c0
buffer and post-processing, everything else is almost a
7711c0
copy/paste.
7711c0
7711c0
Instead of adding a forth copy/pasted-ish code when adding
7711c0
the passthrough VPD BL emulation, this patch extirpates
7711c0
this repetition of those 3 functions and put it into
7711c0
a new one called scsi_SG_IO_FROM_DEV. Any future code that
7711c0
wants to execute an SG_DXFER_FROM_DEV to the device can
7711c0
use it, avoiding filling sg_io_hdr_t again and et cetera.
7711c0
7711c0
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
7711c0
Message-Id: <20180627172432.11120-3-danielhb413@gmail.com>
7711c0
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
7711c0
(cherry picked from commit a0c7e35b17b3d2cade8a5fc8e57904e02fb91fe4)
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 hw/scsi/scsi-disk.c    | 18 +++------------
7711c0
 hw/scsi/scsi-generic.c | 61 +++++++++++++++++++++++++-------------------------
7711c0
 include/hw/scsi/scsi.h |  2 ++
7711c0
 3 files changed, 36 insertions(+), 45 deletions(-)
7711c0
7711c0
diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
7711c0
index ae5b4c0..ea86849 100644
7711c0
--- a/hw/scsi/scsi-disk.c
7711c0
+++ b/hw/scsi/scsi-disk.c
7711c0
@@ -2579,8 +2579,6 @@ static int get_device_type(SCSIDiskState *s)
7711c0
 {
7711c0
     uint8_t cmd[16];
7711c0
     uint8_t buf[36];
7711c0
-    uint8_t sensebuf[8];
7711c0
-    sg_io_hdr_t io_header;
7711c0
     int ret;
7711c0
 
7711c0
     memset(cmd, 0, sizeof(cmd));
7711c0
@@ -2588,19 +2586,9 @@ static int get_device_type(SCSIDiskState *s)
7711c0
     cmd[0] = INQUIRY;
7711c0
     cmd[4] = sizeof(buf);
7711c0
 
7711c0
-    memset(&io_header, 0, sizeof(io_header));
7711c0
-    io_header.interface_id = 'S';
7711c0
-    io_header.dxfer_direction = SG_DXFER_FROM_DEV;
7711c0
-    io_header.dxfer_len = sizeof(buf);
7711c0
-    io_header.dxferp = buf;
7711c0
-    io_header.cmdp = cmd;
7711c0
-    io_header.cmd_len = sizeof(cmd);
7711c0
-    io_header.mx_sb_len = sizeof(sensebuf);
7711c0
-    io_header.sbp = sensebuf;
7711c0
-    io_header.timeout = 6000; /* XXX */
7711c0
-
7711c0
-    ret = blk_ioctl(s->qdev.conf.blk, SG_IO, &io_header);
7711c0
-    if (ret < 0 || io_header.driver_status || io_header.host_status) {
7711c0
+    ret = scsi_SG_IO_FROM_DEV(s->qdev.conf.blk, cmd, sizeof(cmd),
7711c0
+                              buf, sizeof(buf));
7711c0
+    if (ret < 0) {
7711c0
         return -1;
7711c0
     }
7711c0
     s->qdev.type = buf[0];
7711c0
diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c
7711c0
index 796162c..c6307a8 100644
7711c0
--- a/hw/scsi/scsi-generic.c
7711c0
+++ b/hw/scsi/scsi-generic.c
7711c0
@@ -410,35 +410,48 @@ static int read_naa_id(const uint8_t *p, uint64_t *p_wwn)
7711c0
     return -EINVAL;
7711c0
 }
7711c0
 
7711c0
-void scsi_generic_read_device_identification(SCSIDevice *s)
7711c0
+int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
7711c0
+                        uint8_t *buf, uint8_t buf_size)
7711c0
 {
7711c0
-    uint8_t cmd[6];
7711c0
-    uint8_t buf[250];
7711c0
-    uint8_t sensebuf[8];
7711c0
     sg_io_hdr_t io_header;
7711c0
+    uint8_t sensebuf[8];
7711c0
     int ret;
7711c0
-    int i, len;
7711c0
-
7711c0
-    memset(cmd, 0, sizeof(cmd));
7711c0
-    memset(buf, 0, sizeof(buf));
7711c0
-    cmd[0] = INQUIRY;
7711c0
-    cmd[1] = 1;
7711c0
-    cmd[2] = 0x83;
7711c0
-    cmd[4] = sizeof(buf);
7711c0
 
7711c0
     memset(&io_header, 0, sizeof(io_header));
7711c0
     io_header.interface_id = 'S';
7711c0
     io_header.dxfer_direction = SG_DXFER_FROM_DEV;
7711c0
-    io_header.dxfer_len = sizeof(buf);
7711c0
+    io_header.dxfer_len = buf_size;
7711c0
     io_header.dxferp = buf;
7711c0
     io_header.cmdp = cmd;
7711c0
-    io_header.cmd_len = sizeof(cmd);
7711c0
+    io_header.cmd_len = cmd_size;
7711c0
     io_header.mx_sb_len = sizeof(sensebuf);
7711c0
     io_header.sbp = sensebuf;
7711c0
     io_header.timeout = 6000; /* XXX */
7711c0
 
7711c0
-    ret = blk_ioctl(s->conf.blk, SG_IO, &io_header);
7711c0
+    ret = blk_ioctl(blk, SG_IO, &io_header);
7711c0
     if (ret < 0 || io_header.driver_status || io_header.host_status) {
7711c0
+        return -1;
7711c0
+    }
7711c0
+    return 0;
7711c0
+}
7711c0
+
7711c0
+void scsi_generic_read_device_identification(SCSIDevice *s)
7711c0
+{
7711c0
+    uint8_t cmd[6];
7711c0
+    uint8_t buf[250];
7711c0
+    int ret;
7711c0
+    int i, len;
7711c0
+
7711c0
+    memset(cmd, 0, sizeof(cmd));
7711c0
+    memset(buf, 0, sizeof(buf));
7711c0
+    cmd[0] = INQUIRY;
7711c0
+    cmd[1] = 1;
7711c0
+    cmd[2] = 0x83;
7711c0
+    cmd[4] = sizeof(buf);
7711c0
+
7711c0
+    ret = scsi_SG_IO_FROM_DEV(s->conf.blk, cmd, sizeof(cmd),
7711c0
+                              buf, sizeof(buf));
7711c0
+    if (ret < 0) {
7711c0
         return;
7711c0
     }
7711c0
 
7711c0
@@ -471,8 +484,6 @@ static int get_stream_blocksize(BlockBackend *blk)
7711c0
 {
7711c0
     uint8_t cmd[6];
7711c0
     uint8_t buf[12];
7711c0
-    uint8_t sensebuf[8];
7711c0
-    sg_io_hdr_t io_header;
7711c0
     int ret;
7711c0
 
7711c0
     memset(cmd, 0, sizeof(cmd));
7711c0
@@ -480,21 +491,11 @@ static int get_stream_blocksize(BlockBackend *blk)
7711c0
     cmd[0] = MODE_SENSE;
7711c0
     cmd[4] = sizeof(buf);
7711c0
 
7711c0
-    memset(&io_header, 0, sizeof(io_header));
7711c0
-    io_header.interface_id = 'S';
7711c0
-    io_header.dxfer_direction = SG_DXFER_FROM_DEV;
7711c0
-    io_header.dxfer_len = sizeof(buf);
7711c0
-    io_header.dxferp = buf;
7711c0
-    io_header.cmdp = cmd;
7711c0
-    io_header.cmd_len = sizeof(cmd);
7711c0
-    io_header.mx_sb_len = sizeof(sensebuf);
7711c0
-    io_header.sbp = sensebuf;
7711c0
-    io_header.timeout = 6000; /* XXX */
7711c0
-
7711c0
-    ret = blk_ioctl(blk, SG_IO, &io_header);
7711c0
-    if (ret < 0 || io_header.driver_status || io_header.host_status) {
7711c0
+    ret = scsi_SG_IO_FROM_DEV(blk, cmd, sizeof(cmd), buf, sizeof(buf));
7711c0
+    if (ret < 0) {
7711c0
         return -1;
7711c0
     }
7711c0
+
7711c0
     return (buf[9] << 16) | (buf[10] << 8) | buf[11];
7711c0
 }
7711c0
 
7711c0
diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h
7711c0
index 5930a43..b6e05c4 100644
7711c0
--- a/include/hw/scsi/scsi.h
7711c0
+++ b/include/hw/scsi/scsi.h
7711c0
@@ -189,6 +189,8 @@ void scsi_device_unit_attention_reported(SCSIDevice *dev);
7711c0
 void scsi_generic_read_device_identification(SCSIDevice *dev);
7711c0
 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
7711c0
 int scsi_disk_emulate_vpd_page(SCSIRequest *req, uint8_t *outbuf);
7711c0
+int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size,
7711c0
+                        uint8_t *buf, uint8_t buf_size);
7711c0
 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
7711c0
 
7711c0
 /* scsi-generic.c. */
7711c0
-- 
7711c0
1.8.3.1
7711c0