Blame 1008-scsi-move-non-emulation-specific-code-to-scsi.patch

Paolo Bonzini 0fb2b2
From 6949ca76b864b54a0ef1d2aa321c3df4dc102c90 Mon Sep 17 00:00:00 2001
Paolo Bonzini 0fb2b2
From: Paolo Bonzini <pbonzini@redhat.com>
Paolo Bonzini 0fb2b2
Date: Tue, 22 Aug 2017 07:08:27 +0200
Paolo Bonzini 0fb2b2
Subject: [PATCH 08/15] scsi: move non-emulation specific code to scsi/
Paolo Bonzini 0fb2b2
MIME-Version: 1.0
Paolo Bonzini 0fb2b2
Content-Type: text/plain; charset=UTF-8
Paolo Bonzini 0fb2b2
Content-Transfer-Encoding: 8bit
Paolo Bonzini 0fb2b2
Paolo Bonzini 0fb2b2
util/scsi.c includes some SCSI code that is shared by block/iscsi.c and
Paolo Bonzini 0fb2b2
hw/scsi, but the introduction of the persistent reservation helper
Paolo Bonzini 0fb2b2
will add many more instances of this.  There is also include/block/scsi.h,
Paolo Bonzini 0fb2b2
which actually is not part of the core block layer.
Paolo Bonzini 0fb2b2
Paolo Bonzini 0fb2b2
The persistent reservation manager will also need a home.  A scsi/
Paolo Bonzini 0fb2b2
directory provides one for both the aforementioned shared code and
Paolo Bonzini 0fb2b2
the PR manager code.
Paolo Bonzini 0fb2b2
Paolo Bonzini 0fb2b2
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Paolo Bonzini 0fb2b2
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Paolo Bonzini 0fb2b2
---
Paolo Bonzini 0fb2b2
 MAINTAINERS            |   7 +
Paolo Bonzini 0fb2b2
 Makefile.objs          |   2 +-
Paolo Bonzini 0fb2b2
 block/iscsi.c          |   6 +-
Paolo Bonzini 0fb2b2
 hw/scsi/scsi-bus.c     | 397 ---------------------------------------
Paolo Bonzini 0fb2b2
 hw/scsi/scsi-generic.c |   8 -
Paolo Bonzini 0fb2b2
 include/block/scsi.h   |   2 -
Paolo Bonzini 0fb2b2
 include/hw/scsi/scsi.h |  94 +---------
Paolo Bonzini 0fb2b2
 include/scsi/scsi.h    |  20 --
Paolo Bonzini 0fb2b2
 include/scsi/utils.h   | 119 ++++++++++++
Paolo Bonzini 0fb2b2
 scsi/Makefile.objs     |   1 +
Paolo Bonzini 0fb2b2
 scsi/utils.c           | 492 +++++++++++++++++++++++++++++++++++++++++++++++++
Paolo Bonzini 0fb2b2
 util/Makefile.objs     |   1 -
Paolo Bonzini 0fb2b2
 util/scsi.c            |  90 ---------
Paolo Bonzini 0fb2b2
 13 files changed, 626 insertions(+), 613 deletions(-)
Paolo Bonzini 0fb2b2
 delete mode 100644 include/scsi/scsi.h
Paolo Bonzini 0fb2b2
 create mode 100644 include/scsi/utils.h
Paolo Bonzini 0fb2b2
 create mode 100644 scsi/Makefile.objs
Paolo Bonzini 0fb2b2
 create mode 100644 scsi/utils.c
Paolo Bonzini 0fb2b2
 delete mode 100644 util/scsi.c
Paolo Bonzini 0fb2b2
Paolo Bonzini 0fb2b2
diff --git a/MAINTAINERS b/MAINTAINERS
Paolo Bonzini 0fb2b2
index 2a4e5036ae..074da8c976 100644
Paolo Bonzini 0fb2b2
--- a/MAINTAINERS
Paolo Bonzini 0fb2b2
+++ b/MAINTAINERS
Paolo Bonzini 0fb2b2
@@ -1215,6 +1215,13 @@ F: migration/block*
Paolo Bonzini 0fb2b2
 F: include/block/aio.h
Paolo Bonzini 0fb2b2
 T: git git://github.com/stefanha/qemu.git block
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
+Block SCSI subsystem
Paolo Bonzini 0fb2b2
+M: Paolo Bonzini <pbonzini@redhat.com>
Paolo Bonzini 0fb2b2
+L: qemu-block@nongnu.org
Paolo Bonzini 0fb2b2
+S: Supported
Paolo Bonzini 0fb2b2
+F: include/scsi/*
Paolo Bonzini 0fb2b2
+F: scsi/*
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
 Block Jobs
Paolo Bonzini 0fb2b2
 M: Jeff Cody <jcody@redhat.com>
Paolo Bonzini 0fb2b2
 L: qemu-block@nongnu.org
Paolo Bonzini 0fb2b2
diff --git a/Makefile.objs b/Makefile.objs
Paolo Bonzini 0fb2b2
index 24a4ea08b8..f68aa3b60d 100644
Paolo Bonzini 0fb2b2
--- a/Makefile.objs
Paolo Bonzini 0fb2b2
+++ b/Makefile.objs
Paolo Bonzini 0fb2b2
@@ -11,7 +11,7 @@ chardev-obj-y = chardev/
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
 block-obj-y += nbd/
Paolo Bonzini 0fb2b2
 block-obj-y += block.o blockjob.o
Paolo Bonzini 0fb2b2
-block-obj-y += block/
Paolo Bonzini 0fb2b2
+block-obj-y += block/ scsi/
Paolo Bonzini 0fb2b2
 block-obj-y += qemu-io-cmds.o
Paolo Bonzini 0fb2b2
 block-obj-$(CONFIG_REPLICATION) += replication.o
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
diff --git a/block/iscsi.c b/block/iscsi.c
Paolo Bonzini 0fb2b2
index 4bed63cd6d..40adc3c493 100644
Paolo Bonzini 0fb2b2
--- a/block/iscsi.c
Paolo Bonzini 0fb2b2
+++ b/block/iscsi.c
Paolo Bonzini 0fb2b2
@@ -40,10 +40,14 @@
Paolo Bonzini 0fb2b2
 #include "qmp-commands.h"
Paolo Bonzini 0fb2b2
 #include "qapi/qmp/qstring.h"
Paolo Bonzini 0fb2b2
 #include "crypto/secret.h"
Paolo Bonzini 0fb2b2
-#include "scsi/scsi.h"
Paolo Bonzini 0fb2b2
+#include "scsi/utils.h"
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
+/* Conflict between scsi/utils.h and libiscsi! :( */
Paolo Bonzini 0fb2b2
+#define SCSI_XFER_NONE ISCSI_XFER_NONE
Paolo Bonzini 0fb2b2
 #include <iscsi/iscsi.h>
Paolo Bonzini 0fb2b2
 #include <iscsi/scsi-lowlevel.h>
Paolo Bonzini 0fb2b2
+#undef SCSI_XFER_NONE
Paolo Bonzini 0fb2b2
+QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE != (int)ISCSI_XFER_NONE);
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
 #ifdef __linux__
Paolo Bonzini 0fb2b2
 #include <scsi/sg.h>
Paolo Bonzini 0fb2b2
diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c
Paolo Bonzini 0fb2b2
index fac360e20f..42920d5422 100644
Paolo Bonzini 0fb2b2
--- a/hw/scsi/scsi-bus.c
Paolo Bonzini 0fb2b2
+++ b/hw/scsi/scsi-bus.c
Paolo Bonzini 0fb2b2
@@ -956,36 +956,6 @@ static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
Paolo Bonzini 0fb2b2
     return xfer * unit;
Paolo Bonzini 0fb2b2
 }
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-uint32_t scsi_data_cdb_xfer(uint8_t *buf)
Paolo Bonzini 0fb2b2
-{
Paolo Bonzini 0fb2b2
-    if ((buf[0] >> 5) == 0 && buf[4] == 0) {
Paolo Bonzini 0fb2b2
-        return 256;
Paolo Bonzini 0fb2b2
-    } else {
Paolo Bonzini 0fb2b2
-        return scsi_cdb_xfer(buf);
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-uint32_t scsi_cdb_xfer(uint8_t *buf)
Paolo Bonzini 0fb2b2
-{
Paolo Bonzini 0fb2b2
-    switch (buf[0] >> 5) {
Paolo Bonzini 0fb2b2
-    case 0:
Paolo Bonzini 0fb2b2
-        return buf[4];
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 1:
Paolo Bonzini 0fb2b2
-    case 2:
Paolo Bonzini 0fb2b2
-        return lduw_be_p(&buf[7]);
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 4:
Paolo Bonzini 0fb2b2
-        return ldl_be_p(&buf[10]) & 0xffffffffULL;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 5:
Paolo Bonzini 0fb2b2
-        return ldl_be_p(&buf[6]) & 0xffffffffULL;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    default:
Paolo Bonzini 0fb2b2
-        return -1;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
Paolo Bonzini 0fb2b2
 {
Paolo Bonzini 0fb2b2
     cmd->xfer = scsi_cdb_xfer(buf);
Paolo Bonzini 0fb2b2
@@ -1298,53 +1268,6 @@ static void scsi_cmd_xfer_mode(SCSICommand *cmd)
Paolo Bonzini 0fb2b2
     }
Paolo Bonzini 0fb2b2
 }
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-static uint64_t scsi_cmd_lba(SCSICommand *cmd)
Paolo Bonzini 0fb2b2
-{
Paolo Bonzini 0fb2b2
-    uint8_t *buf = cmd->buf;
Paolo Bonzini 0fb2b2
-    uint64_t lba;
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-    switch (buf[0] >> 5) {
Paolo Bonzini 0fb2b2
-    case 0:
Paolo Bonzini 0fb2b2
-        lba = ldl_be_p(&buf[0]) & 0x1fffff;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 1:
Paolo Bonzini 0fb2b2
-    case 2:
Paolo Bonzini 0fb2b2
-    case 5:
Paolo Bonzini 0fb2b2
-        lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 4:
Paolo Bonzini 0fb2b2
-        lba = ldq_be_p(&buf[2]);
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    default:
Paolo Bonzini 0fb2b2
-        lba = -1;
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-    return lba;
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-int scsi_cdb_length(uint8_t *buf) {
Paolo Bonzini 0fb2b2
-    int cdb_len;
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-    switch (buf[0] >> 5) {
Paolo Bonzini 0fb2b2
-    case 0:
Paolo Bonzini 0fb2b2
-        cdb_len = 6;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 1:
Paolo Bonzini 0fb2b2
-    case 2:
Paolo Bonzini 0fb2b2
-        cdb_len = 10;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 4:
Paolo Bonzini 0fb2b2
-        cdb_len = 16;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 5:
Paolo Bonzini 0fb2b2
-        cdb_len = 12;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    default:
Paolo Bonzini 0fb2b2
-        cdb_len = -1;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-    return cdb_len;
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
Paolo Bonzini 0fb2b2
 {
Paolo Bonzini 0fb2b2
     int rc;
Paolo Bonzini 0fb2b2
@@ -1391,326 +1314,6 @@ void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
Paolo Bonzini 0fb2b2
     }
Paolo Bonzini 0fb2b2
 }
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-/*
Paolo Bonzini 0fb2b2
- * Predefined sense codes
Paolo Bonzini 0fb2b2
- */
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* No sense data available */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_NO_SENSE = {
Paolo Bonzini 0fb2b2
-    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* LUN not ready, Manual intervention required */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_LUN_NOT_READY = {
Paolo Bonzini 0fb2b2
-    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* LUN not ready, Medium not present */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_NO_MEDIUM = {
Paolo Bonzini 0fb2b2
-    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* LUN not ready, medium removal prevented */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
Paolo Bonzini 0fb2b2
-    .key = NOT_READY, .asc = 0x53, .ascq = 0x02
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Hardware error, internal target failure */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_TARGET_FAILURE = {
Paolo Bonzini 0fb2b2
-    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, invalid command operation code */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_INVALID_OPCODE = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, LBA out of range */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, Invalid field in CDB */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_INVALID_FIELD = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, Invalid field in parameter list */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_INVALID_PARAM = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, Parameter list length error */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_INVALID_PARAM_LEN = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, LUN not supported */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, Saving parameters not supported */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, Incompatible medium installed */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, medium removal prevented */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Illegal request, Invalid Transfer Tag */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_INVALID_TAG = {
Paolo Bonzini 0fb2b2
-    .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Command aborted, I/O process terminated */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_IO_ERROR = {
Paolo Bonzini 0fb2b2
-    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Command aborted, I_T Nexus loss occurred */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
Paolo Bonzini 0fb2b2
-    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Command aborted, Logical Unit failure */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_LUN_FAILURE = {
Paolo Bonzini 0fb2b2
-    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Command aborted, Overlapped Commands Attempted */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
Paolo Bonzini 0fb2b2
-    .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Unit attention, Capacity data has changed */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_CAPACITY_CHANGED = {
Paolo Bonzini 0fb2b2
-    .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Unit attention, Power on, reset or bus device reset occurred */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_RESET = {
Paolo Bonzini 0fb2b2
-    .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Unit attention, No medium */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
Paolo Bonzini 0fb2b2
-    .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Unit attention, Medium may have changed */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_MEDIUM_CHANGED = {
Paolo Bonzini 0fb2b2
-    .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Unit attention, Reported LUNs data has changed */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
Paolo Bonzini 0fb2b2
-    .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Unit attention, Device internal reset */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
Paolo Bonzini 0fb2b2
-    .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Data Protection, Write Protected */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_WRITE_PROTECTED = {
Paolo Bonzini 0fb2b2
-    .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* Data Protection, Space Allocation Failed Write Protect */
Paolo Bonzini 0fb2b2
-const struct SCSISense sense_code_SPACE_ALLOC_FAILED = {
Paolo Bonzini 0fb2b2
-    .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x07
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/*
Paolo Bonzini 0fb2b2
- * scsi_convert_sense
Paolo Bonzini 0fb2b2
- *
Paolo Bonzini 0fb2b2
- * Convert between fixed and descriptor sense buffers
Paolo Bonzini 0fb2b2
- */
Paolo Bonzini 0fb2b2
-int scsi_convert_sense(uint8_t *in_buf, int in_len,
Paolo Bonzini 0fb2b2
-                       uint8_t *buf, int len, bool fixed)
Paolo Bonzini 0fb2b2
-{
Paolo Bonzini 0fb2b2
-    bool fixed_in;
Paolo Bonzini 0fb2b2
-    SCSISense sense;
Paolo Bonzini 0fb2b2
-    if (!fixed && len < 8) {
Paolo Bonzini 0fb2b2
-        return 0;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-    if (in_len == 0) {
Paolo Bonzini 0fb2b2
-        sense.key = NO_SENSE;
Paolo Bonzini 0fb2b2
-        sense.asc = 0;
Paolo Bonzini 0fb2b2
-        sense.ascq = 0;
Paolo Bonzini 0fb2b2
-    } else {
Paolo Bonzini 0fb2b2
-        fixed_in = (in_buf[0] & 2) == 0;
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-        if (fixed == fixed_in) {
Paolo Bonzini 0fb2b2
-            memcpy(buf, in_buf, MIN(len, in_len));
Paolo Bonzini 0fb2b2
-            return MIN(len, in_len);
Paolo Bonzini 0fb2b2
-        }
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-        if (fixed_in) {
Paolo Bonzini 0fb2b2
-            sense.key = in_buf[2];
Paolo Bonzini 0fb2b2
-            sense.asc = in_buf[12];
Paolo Bonzini 0fb2b2
-            sense.ascq = in_buf[13];
Paolo Bonzini 0fb2b2
-        } else {
Paolo Bonzini 0fb2b2
-            sense.key = in_buf[1];
Paolo Bonzini 0fb2b2
-            sense.asc = in_buf[2];
Paolo Bonzini 0fb2b2
-            sense.ascq = in_buf[3];
Paolo Bonzini 0fb2b2
-        }
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-    memset(buf, 0, len);
Paolo Bonzini 0fb2b2
-    if (fixed) {
Paolo Bonzini 0fb2b2
-        /* Return fixed format sense buffer */
Paolo Bonzini 0fb2b2
-        buf[0] = 0x70;
Paolo Bonzini 0fb2b2
-        buf[2] = sense.key;
Paolo Bonzini 0fb2b2
-        buf[7] = 10;
Paolo Bonzini 0fb2b2
-        buf[12] = sense.asc;
Paolo Bonzini 0fb2b2
-        buf[13] = sense.ascq;
Paolo Bonzini 0fb2b2
-        return MIN(len, SCSI_SENSE_LEN);
Paolo Bonzini 0fb2b2
-    } else {
Paolo Bonzini 0fb2b2
-        /* Return descriptor format sense buffer */
Paolo Bonzini 0fb2b2
-        buf[0] = 0x72;
Paolo Bonzini 0fb2b2
-        buf[1] = sense.key;
Paolo Bonzini 0fb2b2
-        buf[2] = sense.asc;
Paolo Bonzini 0fb2b2
-        buf[3] = sense.ascq;
Paolo Bonzini 0fb2b2
-        return 8;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-const char *scsi_command_name(uint8_t cmd)
Paolo Bonzini 0fb2b2
-{
Paolo Bonzini 0fb2b2
-    static const char *names[] = {
Paolo Bonzini 0fb2b2
-        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
Paolo Bonzini 0fb2b2
-        [ REWIND                   ] = "REWIND",
Paolo Bonzini 0fb2b2
-        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
Paolo Bonzini 0fb2b2
-        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
Paolo Bonzini 0fb2b2
-        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
Paolo Bonzini 0fb2b2
-        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
Paolo Bonzini 0fb2b2
-        /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
Paolo Bonzini 0fb2b2
-        [ READ_6                   ] = "READ_6",
Paolo Bonzini 0fb2b2
-        [ WRITE_6                  ] = "WRITE_6",
Paolo Bonzini 0fb2b2
-        [ SET_CAPACITY             ] = "SET_CAPACITY",
Paolo Bonzini 0fb2b2
-        [ READ_REVERSE             ] = "READ_REVERSE",
Paolo Bonzini 0fb2b2
-        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
Paolo Bonzini 0fb2b2
-        [ SPACE                    ] = "SPACE",
Paolo Bonzini 0fb2b2
-        [ INQUIRY                  ] = "INQUIRY",
Paolo Bonzini 0fb2b2
-        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
Paolo Bonzini 0fb2b2
-        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
Paolo Bonzini 0fb2b2
-        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
Paolo Bonzini 0fb2b2
-        [ MODE_SELECT              ] = "MODE_SELECT",
Paolo Bonzini 0fb2b2
-        [ RESERVE                  ] = "RESERVE",
Paolo Bonzini 0fb2b2
-        [ RELEASE                  ] = "RELEASE",
Paolo Bonzini 0fb2b2
-        [ COPY                     ] = "COPY",
Paolo Bonzini 0fb2b2
-        [ ERASE                    ] = "ERASE",
Paolo Bonzini 0fb2b2
-        [ MODE_SENSE               ] = "MODE_SENSE",
Paolo Bonzini 0fb2b2
-        [ START_STOP               ] = "START_STOP/LOAD_UNLOAD",
Paolo Bonzini 0fb2b2
-        /* LOAD_UNLOAD and START_STOP use the same operation code */
Paolo Bonzini 0fb2b2
-        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
Paolo Bonzini 0fb2b2
-        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
Paolo Bonzini 0fb2b2
-        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
Paolo Bonzini 0fb2b2
-        [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
Paolo Bonzini 0fb2b2
-        [ READ_10                  ] = "READ_10",
Paolo Bonzini 0fb2b2
-        [ WRITE_10                 ] = "WRITE_10",
Paolo Bonzini 0fb2b2
-        [ SEEK_10                  ] = "SEEK_10/POSITION_TO_ELEMENT",
Paolo Bonzini 0fb2b2
-        /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
Paolo Bonzini 0fb2b2
-        [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
Paolo Bonzini 0fb2b2
-        [ VERIFY_10                ] = "VERIFY_10",
Paolo Bonzini 0fb2b2
-        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
Paolo Bonzini 0fb2b2
-        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
Paolo Bonzini 0fb2b2
-        [ SEARCH_LOW               ] = "SEARCH_LOW",
Paolo Bonzini 0fb2b2
-        [ SET_LIMITS               ] = "SET_LIMITS",
Paolo Bonzini 0fb2b2
-        [ PRE_FETCH                ] = "PRE_FETCH/READ_POSITION",
Paolo Bonzini 0fb2b2
-        /* READ_POSITION and PRE_FETCH use the same operation code */
Paolo Bonzini 0fb2b2
-        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
Paolo Bonzini 0fb2b2
-        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
Paolo Bonzini 0fb2b2
-        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
Paolo Bonzini 0fb2b2
-        /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
Paolo Bonzini 0fb2b2
-        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
Paolo Bonzini 0fb2b2
-        [ COMPARE                  ] = "COMPARE",
Paolo Bonzini 0fb2b2
-        [ COPY_VERIFY              ] = "COPY_VERIFY",
Paolo Bonzini 0fb2b2
-        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
Paolo Bonzini 0fb2b2
-        [ READ_BUFFER              ] = "READ_BUFFER",
Paolo Bonzini 0fb2b2
-        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
Paolo Bonzini 0fb2b2
-        [ READ_LONG_10             ] = "READ_LONG_10",
Paolo Bonzini 0fb2b2
-        [ WRITE_LONG_10            ] = "WRITE_LONG_10",
Paolo Bonzini 0fb2b2
-        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
Paolo Bonzini 0fb2b2
-        [ WRITE_SAME_10            ] = "WRITE_SAME_10",
Paolo Bonzini 0fb2b2
-        [ UNMAP                    ] = "UNMAP",
Paolo Bonzini 0fb2b2
-        [ READ_TOC                 ] = "READ_TOC",
Paolo Bonzini 0fb2b2
-        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
Paolo Bonzini 0fb2b2
-        [ SANITIZE                 ] = "SANITIZE",
Paolo Bonzini 0fb2b2
-        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
Paolo Bonzini 0fb2b2
-        [ LOG_SELECT               ] = "LOG_SELECT",
Paolo Bonzini 0fb2b2
-        [ LOG_SENSE                ] = "LOG_SENSE",
Paolo Bonzini 0fb2b2
-        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
Paolo Bonzini 0fb2b2
-        [ RESERVE_10               ] = "RESERVE_10",
Paolo Bonzini 0fb2b2
-        [ RELEASE_10               ] = "RELEASE_10",
Paolo Bonzini 0fb2b2
-        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
Paolo Bonzini 0fb2b2
-        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
Paolo Bonzini 0fb2b2
-        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
Paolo Bonzini 0fb2b2
-        [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
Paolo Bonzini 0fb2b2
-        [ EXTENDED_COPY            ] = "EXTENDED_COPY",
Paolo Bonzini 0fb2b2
-        [ ATA_PASSTHROUGH_16       ] = "ATA_PASSTHROUGH_16",
Paolo Bonzini 0fb2b2
-        [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
Paolo Bonzini 0fb2b2
-        [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
Paolo Bonzini 0fb2b2
-        [ READ_16                  ] = "READ_16",
Paolo Bonzini 0fb2b2
-        [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
Paolo Bonzini 0fb2b2
-        [ WRITE_16                 ] = "WRITE_16",
Paolo Bonzini 0fb2b2
-        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
Paolo Bonzini 0fb2b2
-        [ VERIFY_16                ] = "VERIFY_16",
Paolo Bonzini 0fb2b2
-        [ PRE_FETCH_16             ] = "PRE_FETCH_16",
Paolo Bonzini 0fb2b2
-        [ SYNCHRONIZE_CACHE_16     ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
Paolo Bonzini 0fb2b2
-        /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
Paolo Bonzini 0fb2b2
-        [ LOCATE_16                ] = "LOCATE_16",
Paolo Bonzini 0fb2b2
-        [ WRITE_SAME_16            ] = "ERASE_16/WRITE_SAME_16",
Paolo Bonzini 0fb2b2
-        /* ERASE_16 and WRITE_SAME_16 use the same operation code */
Paolo Bonzini 0fb2b2
-        [ SERVICE_ACTION_IN_16     ] = "SERVICE_ACTION_IN_16",
Paolo Bonzini 0fb2b2
-        [ WRITE_LONG_16            ] = "WRITE_LONG_16",
Paolo Bonzini 0fb2b2
-        [ REPORT_LUNS              ] = "REPORT_LUNS",
Paolo Bonzini 0fb2b2
-        [ ATA_PASSTHROUGH_12       ] = "BLANK/ATA_PASSTHROUGH_12",
Paolo Bonzini 0fb2b2
-        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
Paolo Bonzini 0fb2b2
-        [ EXCHANGE_MEDIUM          ] = "EXCHANGE MEDIUM",
Paolo Bonzini 0fb2b2
-        [ READ_12                  ] = "READ_12",
Paolo Bonzini 0fb2b2
-        [ WRITE_12                 ] = "WRITE_12",
Paolo Bonzini 0fb2b2
-        [ ERASE_12                 ] = "ERASE_12/GET_PERFORMANCE",
Paolo Bonzini 0fb2b2
-        /* ERASE_12 and GET_PERFORMANCE use the same operation code */
Paolo Bonzini 0fb2b2
-        [ SERVICE_ACTION_IN_12     ] = "SERVICE_ACTION_IN_12",
Paolo Bonzini 0fb2b2
-        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
Paolo Bonzini 0fb2b2
-        [ VERIFY_12                ] = "VERIFY_12",
Paolo Bonzini 0fb2b2
-        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
Paolo Bonzini 0fb2b2
-        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
Paolo Bonzini 0fb2b2
-        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
Paolo Bonzini 0fb2b2
-        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
Paolo Bonzini 0fb2b2
-        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG/SET_STREAMING",
Paolo Bonzini 0fb2b2
-        /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
Paolo Bonzini 0fb2b2
-        [ READ_CD                  ] = "READ_CD",
Paolo Bonzini 0fb2b2
-        [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
Paolo Bonzini 0fb2b2
-        [ READ_DVD_STRUCTURE       ] = "READ_DVD_STRUCTURE",
Paolo Bonzini 0fb2b2
-        [ RESERVE_TRACK            ] = "RESERVE_TRACK",
Paolo Bonzini 0fb2b2
-        [ SEND_CUE_SHEET           ] = "SEND_CUE_SHEET",
Paolo Bonzini 0fb2b2
-        [ SEND_DVD_STRUCTURE       ] = "SEND_DVD_STRUCTURE",
Paolo Bonzini 0fb2b2
-        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
Paolo Bonzini 0fb2b2
-        [ SET_READ_AHEAD           ] = "SET_READ_AHEAD",
Paolo Bonzini 0fb2b2
-        [ ALLOW_OVERWRITE          ] = "ALLOW_OVERWRITE",
Paolo Bonzini 0fb2b2
-        [ MECHANISM_STATUS         ] = "MECHANISM_STATUS",
Paolo Bonzini 0fb2b2
-        [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION",
Paolo Bonzini 0fb2b2
-        [ READ_DISC_INFORMATION    ] = "READ_DISC_INFORMATION",
Paolo Bonzini 0fb2b2
-    };
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
Paolo Bonzini 0fb2b2
-        return "*UNKNOWN*";
Paolo Bonzini 0fb2b2
-    return names[cmd];
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 SCSIRequest *scsi_req_ref(SCSIRequest *req)
Paolo Bonzini 0fb2b2
 {
Paolo Bonzini 0fb2b2
     assert(req->refcount > 0);
Paolo Bonzini 0fb2b2
diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c
Paolo Bonzini 0fb2b2
index 7e1cbab77e..7a8f500934 100644
Paolo Bonzini 0fb2b2
--- a/hw/scsi/scsi-generic.c
Paolo Bonzini 0fb2b2
+++ b/hw/scsi/scsi-generic.c
Paolo Bonzini 0fb2b2
@@ -36,14 +36,6 @@ do { fprintf(stderr, "scsi-generic: " fmt , ## __VA_ARGS__); } while (0)
Paolo Bonzini 0fb2b2
 #include <scsi/sg.h>
Paolo Bonzini 0fb2b2
 #include "block/scsi.h"
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-#define SG_ERR_DRIVER_TIMEOUT  0x06
Paolo Bonzini 0fb2b2
-#define SG_ERR_DRIVER_SENSE    0x08
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-#define SG_ERR_DID_OK          0x00
Paolo Bonzini 0fb2b2
-#define SG_ERR_DID_NO_CONNECT  0x01
Paolo Bonzini 0fb2b2
-#define SG_ERR_DID_BUS_BUSY    0x02
Paolo Bonzini 0fb2b2
-#define SG_ERR_DID_TIME_OUT    0x03
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 #ifndef MAX_UINT
Paolo Bonzini 0fb2b2
 #define MAX_UINT ((unsigned int)-1)
Paolo Bonzini 0fb2b2
 #endif
Paolo Bonzini 0fb2b2
diff --git a/include/block/scsi.h b/include/block/scsi.h
Paolo Bonzini 0fb2b2
index cdf0a58a07..a141dd71f8 100644
Paolo Bonzini 0fb2b2
--- a/include/block/scsi.h
Paolo Bonzini 0fb2b2
+++ b/include/block/scsi.h
Paolo Bonzini 0fb2b2
@@ -150,8 +150,6 @@
Paolo Bonzini 0fb2b2
 #define READ_CD               0xbe
Paolo Bonzini 0fb2b2
 #define SEND_DVD_STRUCTURE    0xbf
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-const char *scsi_command_name(uint8_t cmd);
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 /*
Paolo Bonzini 0fb2b2
  * SERVICE ACTION IN subcodes
Paolo Bonzini 0fb2b2
  */
Paolo Bonzini 0fb2b2
diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h
Paolo Bonzini 0fb2b2
index 6ef67fb504..23a8ee6a7d 100644
Paolo Bonzini 0fb2b2
--- a/include/hw/scsi/scsi.h
Paolo Bonzini 0fb2b2
+++ b/include/hw/scsi/scsi.h
Paolo Bonzini 0fb2b2
@@ -4,45 +4,20 @@
Paolo Bonzini 0fb2b2
 #include "hw/qdev.h"
Paolo Bonzini 0fb2b2
 #include "hw/block/block.h"
Paolo Bonzini 0fb2b2
 #include "sysemu/sysemu.h"
Paolo Bonzini 0fb2b2
+#include "scsi/utils.h"
Paolo Bonzini 0fb2b2
 #include "qemu/notify.h"
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
 #define MAX_SCSI_DEVS	255
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-#define SCSI_CMD_BUF_SIZE      16
Paolo Bonzini 0fb2b2
-#define SCSI_SENSE_LEN         18
Paolo Bonzini 0fb2b2
-#define SCSI_SENSE_LEN_SCANNER 32
Paolo Bonzini 0fb2b2
-#define SCSI_INQUIRY_LEN       36
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 typedef struct SCSIBus SCSIBus;
Paolo Bonzini 0fb2b2
 typedef struct SCSIBusInfo SCSIBusInfo;
Paolo Bonzini 0fb2b2
-typedef struct SCSICommand SCSICommand;
Paolo Bonzini 0fb2b2
 typedef struct SCSIDevice SCSIDevice;
Paolo Bonzini 0fb2b2
 typedef struct SCSIRequest SCSIRequest;
Paolo Bonzini 0fb2b2
 typedef struct SCSIReqOps SCSIReqOps;
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-enum SCSIXferMode {
Paolo Bonzini 0fb2b2
-    SCSI_XFER_NONE,      /*  TEST_UNIT_READY, ...            */
Paolo Bonzini 0fb2b2
-    SCSI_XFER_FROM_DEV,  /*  READ, INQUIRY, MODE_SENSE, ...  */
Paolo Bonzini 0fb2b2
-    SCSI_XFER_TO_DEV,    /*  WRITE, MODE_SELECT, ...         */
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-typedef struct SCSISense {
Paolo Bonzini 0fb2b2
-    uint8_t key;
Paolo Bonzini 0fb2b2
-    uint8_t asc;
Paolo Bonzini 0fb2b2
-    uint8_t ascq;
Paolo Bonzini 0fb2b2
-} SCSISense;
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 #define SCSI_SENSE_BUF_SIZE_OLD 96
Paolo Bonzini 0fb2b2
 #define SCSI_SENSE_BUF_SIZE 252
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-struct SCSICommand {
Paolo Bonzini 0fb2b2
-    uint8_t buf[SCSI_CMD_BUF_SIZE];
Paolo Bonzini 0fb2b2
-    int len;
Paolo Bonzini 0fb2b2
-    size_t xfer;
Paolo Bonzini 0fb2b2
-    uint64_t lba;
Paolo Bonzini 0fb2b2
-    enum SCSIXferMode mode;
Paolo Bonzini 0fb2b2
-};
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 struct SCSIRequest {
Paolo Bonzini 0fb2b2
     SCSIBus           *bus;
Paolo Bonzini 0fb2b2
     SCSIDevice        *dev;
Paolo Bonzini 0fb2b2
@@ -180,73 +155,6 @@ SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
Paolo Bonzini 0fb2b2
 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, bool deprecated);
Paolo Bonzini 0fb2b2
 void scsi_legacy_handle_cmdline(void);
Paolo Bonzini 0fb2b2
 
Paolo Bonzini 0fb2b2
-/*
Paolo Bonzini 0fb2b2
- * Predefined sense codes
Paolo Bonzini 0fb2b2
- */
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-/* No sense data available */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_NO_SENSE;
Paolo Bonzini 0fb2b2
-/* LUN not ready, Manual intervention required */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_LUN_NOT_READY;
Paolo Bonzini 0fb2b2
-/* LUN not ready, Medium not present */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_NO_MEDIUM;
Paolo Bonzini 0fb2b2
-/* LUN not ready, medium removal prevented */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
Paolo Bonzini 0fb2b2
-/* Hardware error, internal target failure */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_TARGET_FAILURE;
Paolo Bonzini 0fb2b2
-/* Illegal request, invalid command operation code */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_INVALID_OPCODE;
Paolo Bonzini 0fb2b2
-/* Illegal request, LBA out of range */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
Paolo Bonzini 0fb2b2
-/* Illegal request, Invalid field in CDB */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_INVALID_FIELD;
Paolo Bonzini 0fb2b2
-/* Illegal request, Invalid field in parameter list */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_INVALID_PARAM;
Paolo Bonzini 0fb2b2
-/* Illegal request, Parameter list length error */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
Paolo Bonzini 0fb2b2
-/* Illegal request, LUN not supported */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
Paolo Bonzini 0fb2b2
-/* Illegal request, Saving parameters not supported */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
Paolo Bonzini 0fb2b2
-/* Illegal request, Incompatible format */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
Paolo Bonzini 0fb2b2
-/* Illegal request, medium removal prevented */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
Paolo Bonzini 0fb2b2
-/* Illegal request, Invalid Transfer Tag */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_INVALID_TAG;
Paolo Bonzini 0fb2b2
-/* Command aborted, I/O process terminated */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_IO_ERROR;
Paolo Bonzini 0fb2b2
-/* Command aborted, I_T Nexus loss occurred */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
Paolo Bonzini 0fb2b2
-/* Command aborted, Logical Unit failure */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_LUN_FAILURE;
Paolo Bonzini 0fb2b2
-/* Command aborted, Overlapped Commands Attempted */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
Paolo Bonzini 0fb2b2
-/* LUN not ready, Capacity data has changed */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_CAPACITY_CHANGED;
Paolo Bonzini 0fb2b2
-/* LUN not ready, Medium not present */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
Paolo Bonzini 0fb2b2
-/* Unit attention, Power on, reset or bus device reset occurred */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_RESET;
Paolo Bonzini 0fb2b2
-/* Unit attention, Medium may have changed*/
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_MEDIUM_CHANGED;
Paolo Bonzini 0fb2b2
-/* Unit attention, Reported LUNs data has changed */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
Paolo Bonzini 0fb2b2
-/* Unit attention, Device internal reset */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
Paolo Bonzini 0fb2b2
-/* Data Protection, Write Protected */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_WRITE_PROTECTED;
Paolo Bonzini 0fb2b2
-/* Data Protection, Space Allocation Failed Write Protect */
Paolo Bonzini 0fb2b2
-extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-#define SENSE_CODE(x) sense_code_ ## x
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-uint32_t scsi_data_cdb_xfer(uint8_t *buf);
Paolo Bonzini 0fb2b2
-uint32_t scsi_cdb_xfer(uint8_t *buf);
Paolo Bonzini 0fb2b2
-int scsi_cdb_length(uint8_t *buf);
Paolo Bonzini 0fb2b2
-int scsi_convert_sense(uint8_t *in_buf, int in_len,
Paolo Bonzini 0fb2b2
-                       uint8_t *buf, int len, bool fixed);
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
Paolo Bonzini 0fb2b2
                             uint32_t tag, uint32_t lun, void *hba_private);
Paolo Bonzini 0fb2b2
 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
Paolo Bonzini 0fb2b2
diff --git a/include/scsi/scsi.h b/include/scsi/scsi.h
Paolo Bonzini 0fb2b2
deleted file mode 100644
Paolo Bonzini 0fb2b2
index fe330385d8..0000000000
Paolo Bonzini 0fb2b2
--- a/include/scsi/scsi.h
Paolo Bonzini 0fb2b2
+++ /dev/null
Paolo Bonzini 0fb2b2
@@ -1,20 +0,0 @@
Paolo Bonzini 0fb2b2
-/*
Paolo Bonzini 0fb2b2
- *  SCSI helpers
Paolo Bonzini 0fb2b2
- *
Paolo Bonzini 0fb2b2
- *  Copyright 2017 Red Hat, Inc.
Paolo Bonzini 0fb2b2
- *
Paolo Bonzini 0fb2b2
- *  Authors:
Paolo Bonzini 0fb2b2
- *   Fam Zheng <famz@redhat.com>
Paolo Bonzini 0fb2b2
- *
Paolo Bonzini 0fb2b2
- * This program is free software; you can redistribute it and/or modify it
Paolo Bonzini 0fb2b2
- * under the terms of the GNU General Public License as published by the Free
Paolo Bonzini 0fb2b2
- * Software Foundation; either version 2 of the License, or (at your option)
Paolo Bonzini 0fb2b2
- * any later version.
Paolo Bonzini 0fb2b2
- */
Paolo Bonzini 0fb2b2
-#ifndef QEMU_SCSI_H
Paolo Bonzini 0fb2b2
-#define QEMU_SCSI_H
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-int scsi_sense_to_errno(int key, int asc, int ascq);
Paolo Bonzini 0fb2b2
-int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size);
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-#endif
Paolo Bonzini 0fb2b2
diff --git a/include/scsi/utils.h b/include/scsi/utils.h
Paolo Bonzini 0fb2b2
new file mode 100644
Paolo Bonzini 0fb2b2
index 0000000000..90bf4dce6e
Paolo Bonzini 0fb2b2
--- /dev/null
Paolo Bonzini 0fb2b2
+++ b/include/scsi/utils.h
Paolo Bonzini 0fb2b2
@@ -0,0 +1,119 @@
Paolo Bonzini 0fb2b2
+#ifndef SCSI_UTILS_H
Paolo Bonzini 0fb2b2
+#define SCSI_UTILS_H 1
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+#ifdef CONFIG_LINUX
Paolo Bonzini 0fb2b2
+#include <scsi/sg.h>
Paolo Bonzini 0fb2b2
+#endif
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+#define SCSI_CMD_BUF_SIZE      16
Paolo Bonzini 0fb2b2
+#define SCSI_SENSE_LEN         18
Paolo Bonzini 0fb2b2
+#define SCSI_SENSE_LEN_SCANNER 32
Paolo Bonzini 0fb2b2
+#define SCSI_INQUIRY_LEN       36
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+enum SCSIXferMode {
Paolo Bonzini 0fb2b2
+    SCSI_XFER_NONE,      /*  TEST_UNIT_READY, ...            */
Paolo Bonzini 0fb2b2
+    SCSI_XFER_FROM_DEV,  /*  READ, INQUIRY, MODE_SENSE, ...  */
Paolo Bonzini 0fb2b2
+    SCSI_XFER_TO_DEV,    /*  WRITE, MODE_SELECT, ...         */
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+typedef struct SCSICommand {
Paolo Bonzini 0fb2b2
+    uint8_t buf[SCSI_CMD_BUF_SIZE];
Paolo Bonzini 0fb2b2
+    int len;
Paolo Bonzini 0fb2b2
+    size_t xfer;
Paolo Bonzini 0fb2b2
+    uint64_t lba;
Paolo Bonzini 0fb2b2
+    enum SCSIXferMode mode;
Paolo Bonzini 0fb2b2
+} SCSICommand;
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+typedef struct SCSISense {
Paolo Bonzini 0fb2b2
+    uint8_t key;
Paolo Bonzini 0fb2b2
+    uint8_t asc;
Paolo Bonzini 0fb2b2
+    uint8_t ascq;
Paolo Bonzini 0fb2b2
+} SCSISense;
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/*
Paolo Bonzini 0fb2b2
+ * Predefined sense codes
Paolo Bonzini 0fb2b2
+ */
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* No sense data available */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_NO_SENSE;
Paolo Bonzini 0fb2b2
+/* LUN not ready, Manual intervention required */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_LUN_NOT_READY;
Paolo Bonzini 0fb2b2
+/* LUN not ready, Medium not present */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_NO_MEDIUM;
Paolo Bonzini 0fb2b2
+/* LUN not ready, medium removal prevented */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
Paolo Bonzini 0fb2b2
+/* Hardware error, internal target failure */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_TARGET_FAILURE;
Paolo Bonzini 0fb2b2
+/* Illegal request, invalid command operation code */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_INVALID_OPCODE;
Paolo Bonzini 0fb2b2
+/* Illegal request, LBA out of range */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
Paolo Bonzini 0fb2b2
+/* Illegal request, Invalid field in CDB */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_INVALID_FIELD;
Paolo Bonzini 0fb2b2
+/* Illegal request, Invalid field in parameter list */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_INVALID_PARAM;
Paolo Bonzini 0fb2b2
+/* Illegal request, Parameter list length error */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
Paolo Bonzini 0fb2b2
+/* Illegal request, LUN not supported */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
Paolo Bonzini 0fb2b2
+/* Illegal request, Saving parameters not supported */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
Paolo Bonzini 0fb2b2
+/* Illegal request, Incompatible format */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
Paolo Bonzini 0fb2b2
+/* Illegal request, medium removal prevented */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
Paolo Bonzini 0fb2b2
+/* Illegal request, Invalid Transfer Tag */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_INVALID_TAG;
Paolo Bonzini 0fb2b2
+/* Command aborted, I/O process terminated */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_IO_ERROR;
Paolo Bonzini 0fb2b2
+/* Command aborted, I_T Nexus loss occurred */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
Paolo Bonzini 0fb2b2
+/* Command aborted, Logical Unit failure */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_LUN_FAILURE;
Paolo Bonzini 0fb2b2
+/* Command aborted, Overlapped Commands Attempted */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
Paolo Bonzini 0fb2b2
+/* LUN not ready, Capacity data has changed */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_CAPACITY_CHANGED;
Paolo Bonzini 0fb2b2
+/* LUN not ready, Medium not present */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
Paolo Bonzini 0fb2b2
+/* Unit attention, Power on, reset or bus device reset occurred */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_RESET;
Paolo Bonzini 0fb2b2
+/* Unit attention, Medium may have changed*/
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_MEDIUM_CHANGED;
Paolo Bonzini 0fb2b2
+/* Unit attention, Reported LUNs data has changed */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
Paolo Bonzini 0fb2b2
+/* Unit attention, Device internal reset */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
Paolo Bonzini 0fb2b2
+/* Data Protection, Write Protected */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_WRITE_PROTECTED;
Paolo Bonzini 0fb2b2
+/* Data Protection, Space Allocation Failed Write Protect */
Paolo Bonzini 0fb2b2
+extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+#define SENSE_CODE(x) sense_code_ ## x
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+int scsi_sense_to_errno(int key, int asc, int ascq);
Paolo Bonzini 0fb2b2
+int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size);
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+int scsi_convert_sense(uint8_t *in_buf, int in_len,
Paolo Bonzini 0fb2b2
+                       uint8_t *buf, int len, bool fixed);
Paolo Bonzini 0fb2b2
+const char *scsi_command_name(uint8_t cmd);
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+uint64_t scsi_cmd_lba(SCSICommand *cmd);
Paolo Bonzini 0fb2b2
+uint32_t scsi_data_cdb_xfer(uint8_t *buf);
Paolo Bonzini 0fb2b2
+uint32_t scsi_cdb_xfer(uint8_t *buf);
Paolo Bonzini 0fb2b2
+int scsi_cdb_length(uint8_t *buf);
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Linux SG_IO interface.  */
Paolo Bonzini 0fb2b2
+#ifdef CONFIG_LINUX
Paolo Bonzini 0fb2b2
+#define SG_ERR_DRIVER_TIMEOUT  0x06
Paolo Bonzini 0fb2b2
+#define SG_ERR_DRIVER_SENSE    0x08
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+#define SG_ERR_DID_OK          0x00
Paolo Bonzini 0fb2b2
+#define SG_ERR_DID_NO_CONNECT  0x01
Paolo Bonzini 0fb2b2
+#define SG_ERR_DID_BUS_BUSY    0x02
Paolo Bonzini 0fb2b2
+#define SG_ERR_DID_TIME_OUT    0x03
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+#define SG_ERR_DRIVER_SENSE    0x08
Paolo Bonzini 0fb2b2
+#endif
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+#endif
Paolo Bonzini 0fb2b2
diff --git a/scsi/Makefile.objs b/scsi/Makefile.objs
Paolo Bonzini 0fb2b2
new file mode 100644
Paolo Bonzini 0fb2b2
index 0000000000..31b82a5a36
Paolo Bonzini 0fb2b2
--- /dev/null
Paolo Bonzini 0fb2b2
+++ b/scsi/Makefile.objs
Paolo Bonzini 0fb2b2
@@ -0,0 +1 @@
Paolo Bonzini 0fb2b2
+block-obj-y += utils.o
Paolo Bonzini 0fb2b2
diff --git a/scsi/utils.c b/scsi/utils.c
Paolo Bonzini 0fb2b2
new file mode 100644
Paolo Bonzini 0fb2b2
index 0000000000..2327e06da0
Paolo Bonzini 0fb2b2
--- /dev/null
Paolo Bonzini 0fb2b2
+++ b/scsi/utils.c
Paolo Bonzini 0fb2b2
@@ -0,0 +1,492 @@
Paolo Bonzini 0fb2b2
+/*
Paolo Bonzini 0fb2b2
+ *  SCSI helpers
Paolo Bonzini 0fb2b2
+ *
Paolo Bonzini 0fb2b2
+ *  Copyright 2017 Red Hat, Inc.
Paolo Bonzini 0fb2b2
+ *
Paolo Bonzini 0fb2b2
+ *  Authors:
Paolo Bonzini 0fb2b2
+ *   Fam Zheng <famz@redhat.com>
Paolo Bonzini 0fb2b2
+ *   Paolo Bonzini <pbonzini@redhat.com>
Paolo Bonzini 0fb2b2
+ *
Paolo Bonzini 0fb2b2
+ * This program is free software; you can redistribute it and/or modify it
Paolo Bonzini 0fb2b2
+ * under the terms of the GNU General Public License as published by the Free
Paolo Bonzini 0fb2b2
+ * Software Foundation; either version 2 of the License, or (at your option)
Paolo Bonzini 0fb2b2
+ * any later version.
Paolo Bonzini 0fb2b2
+ */
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+#include "qemu/osdep.h"
Paolo Bonzini 0fb2b2
+#include "block/scsi.h"
Paolo Bonzini 0fb2b2
+#include "scsi/utils.h"
Paolo Bonzini 0fb2b2
+#include "qemu/bswap.h"
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+uint32_t scsi_data_cdb_xfer(uint8_t *buf)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    if ((buf[0] >> 5) == 0 && buf[4] == 0) {
Paolo Bonzini 0fb2b2
+        return 256;
Paolo Bonzini 0fb2b2
+    } else {
Paolo Bonzini 0fb2b2
+        return scsi_cdb_xfer(buf);
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+uint32_t scsi_cdb_xfer(uint8_t *buf)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    switch (buf[0] >> 5) {
Paolo Bonzini 0fb2b2
+    case 0:
Paolo Bonzini 0fb2b2
+        return buf[4];
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 1:
Paolo Bonzini 0fb2b2
+    case 2:
Paolo Bonzini 0fb2b2
+        return lduw_be_p(&buf[7]);
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 4:
Paolo Bonzini 0fb2b2
+        return ldl_be_p(&buf[10]) & 0xffffffffULL;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 5:
Paolo Bonzini 0fb2b2
+        return ldl_be_p(&buf[6]) & 0xffffffffULL;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    default:
Paolo Bonzini 0fb2b2
+        return -1;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+uint64_t scsi_cmd_lba(SCSICommand *cmd)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    uint8_t *buf = cmd->buf;
Paolo Bonzini 0fb2b2
+    uint64_t lba;
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+    switch (buf[0] >> 5) {
Paolo Bonzini 0fb2b2
+    case 0:
Paolo Bonzini 0fb2b2
+        lba = ldl_be_p(&buf[0]) & 0x1fffff;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 1:
Paolo Bonzini 0fb2b2
+    case 2:
Paolo Bonzini 0fb2b2
+    case 5:
Paolo Bonzini 0fb2b2
+        lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 4:
Paolo Bonzini 0fb2b2
+        lba = ldq_be_p(&buf[2]);
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    default:
Paolo Bonzini 0fb2b2
+        lba = -1;
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+    return lba;
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+int scsi_cdb_length(uint8_t *buf)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    int cdb_len;
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+    switch (buf[0] >> 5) {
Paolo Bonzini 0fb2b2
+    case 0:
Paolo Bonzini 0fb2b2
+        cdb_len = 6;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 1:
Paolo Bonzini 0fb2b2
+    case 2:
Paolo Bonzini 0fb2b2
+        cdb_len = 10;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 4:
Paolo Bonzini 0fb2b2
+        cdb_len = 16;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 5:
Paolo Bonzini 0fb2b2
+        cdb_len = 12;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    default:
Paolo Bonzini 0fb2b2
+        cdb_len = -1;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+    return cdb_len;
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/*
Paolo Bonzini 0fb2b2
+ * Predefined sense codes
Paolo Bonzini 0fb2b2
+ */
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* No sense data available */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_NO_SENSE = {
Paolo Bonzini 0fb2b2
+    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* LUN not ready, Manual intervention required */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_LUN_NOT_READY = {
Paolo Bonzini 0fb2b2
+    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* LUN not ready, Medium not present */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_NO_MEDIUM = {
Paolo Bonzini 0fb2b2
+    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* LUN not ready, medium removal prevented */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
Paolo Bonzini 0fb2b2
+    .key = NOT_READY, .asc = 0x53, .ascq = 0x02
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Hardware error, internal target failure */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_TARGET_FAILURE = {
Paolo Bonzini 0fb2b2
+    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, invalid command operation code */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_INVALID_OPCODE = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, LBA out of range */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, Invalid field in CDB */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_INVALID_FIELD = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, Invalid field in parameter list */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_INVALID_PARAM = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, Parameter list length error */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_INVALID_PARAM_LEN = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, LUN not supported */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, Saving parameters not supported */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, Incompatible medium installed */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, medium removal prevented */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Illegal request, Invalid Transfer Tag */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_INVALID_TAG = {
Paolo Bonzini 0fb2b2
+    .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Command aborted, I/O process terminated */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_IO_ERROR = {
Paolo Bonzini 0fb2b2
+    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Command aborted, I_T Nexus loss occurred */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
Paolo Bonzini 0fb2b2
+    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Command aborted, Logical Unit failure */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_LUN_FAILURE = {
Paolo Bonzini 0fb2b2
+    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Command aborted, Overlapped Commands Attempted */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
Paolo Bonzini 0fb2b2
+    .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Unit attention, Capacity data has changed */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_CAPACITY_CHANGED = {
Paolo Bonzini 0fb2b2
+    .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Unit attention, Power on, reset or bus device reset occurred */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_RESET = {
Paolo Bonzini 0fb2b2
+    .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Unit attention, No medium */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
Paolo Bonzini 0fb2b2
+    .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Unit attention, Medium may have changed */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_MEDIUM_CHANGED = {
Paolo Bonzini 0fb2b2
+    .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Unit attention, Reported LUNs data has changed */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
Paolo Bonzini 0fb2b2
+    .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Unit attention, Device internal reset */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
Paolo Bonzini 0fb2b2
+    .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Data Protection, Write Protected */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_WRITE_PROTECTED = {
Paolo Bonzini 0fb2b2
+    .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/* Data Protection, Space Allocation Failed Write Protect */
Paolo Bonzini 0fb2b2
+const struct SCSISense sense_code_SPACE_ALLOC_FAILED = {
Paolo Bonzini 0fb2b2
+    .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x07
Paolo Bonzini 0fb2b2
+};
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+/*
Paolo Bonzini 0fb2b2
+ * scsi_convert_sense
Paolo Bonzini 0fb2b2
+ *
Paolo Bonzini 0fb2b2
+ * Convert between fixed and descriptor sense buffers
Paolo Bonzini 0fb2b2
+ */
Paolo Bonzini 0fb2b2
+int scsi_convert_sense(uint8_t *in_buf, int in_len,
Paolo Bonzini 0fb2b2
+                       uint8_t *buf, int len, bool fixed)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    bool fixed_in;
Paolo Bonzini 0fb2b2
+    SCSISense sense;
Paolo Bonzini 0fb2b2
+    if (!fixed && len < 8) {
Paolo Bonzini 0fb2b2
+        return 0;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+    if (in_len == 0) {
Paolo Bonzini 0fb2b2
+        sense.key = NO_SENSE;
Paolo Bonzini 0fb2b2
+        sense.asc = 0;
Paolo Bonzini 0fb2b2
+        sense.ascq = 0;
Paolo Bonzini 0fb2b2
+    } else {
Paolo Bonzini 0fb2b2
+        fixed_in = (in_buf[0] & 2) == 0;
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+        if (fixed == fixed_in) {
Paolo Bonzini 0fb2b2
+            memcpy(buf, in_buf, MIN(len, in_len));
Paolo Bonzini 0fb2b2
+            return MIN(len, in_len);
Paolo Bonzini 0fb2b2
+        }
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+        if (fixed_in) {
Paolo Bonzini 0fb2b2
+            sense.key = in_buf[2];
Paolo Bonzini 0fb2b2
+            sense.asc = in_buf[12];
Paolo Bonzini 0fb2b2
+            sense.ascq = in_buf[13];
Paolo Bonzini 0fb2b2
+        } else {
Paolo Bonzini 0fb2b2
+            sense.key = in_buf[1];
Paolo Bonzini 0fb2b2
+            sense.asc = in_buf[2];
Paolo Bonzini 0fb2b2
+            sense.ascq = in_buf[3];
Paolo Bonzini 0fb2b2
+        }
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+    memset(buf, 0, len);
Paolo Bonzini 0fb2b2
+    if (fixed) {
Paolo Bonzini 0fb2b2
+        /* Return fixed format sense buffer */
Paolo Bonzini 0fb2b2
+        buf[0] = 0x70;
Paolo Bonzini 0fb2b2
+        buf[2] = sense.key;
Paolo Bonzini 0fb2b2
+        buf[7] = 10;
Paolo Bonzini 0fb2b2
+        buf[12] = sense.asc;
Paolo Bonzini 0fb2b2
+        buf[13] = sense.ascq;
Paolo Bonzini 0fb2b2
+        return MIN(len, SCSI_SENSE_LEN);
Paolo Bonzini 0fb2b2
+    } else {
Paolo Bonzini 0fb2b2
+        /* Return descriptor format sense buffer */
Paolo Bonzini 0fb2b2
+        buf[0] = 0x72;
Paolo Bonzini 0fb2b2
+        buf[1] = sense.key;
Paolo Bonzini 0fb2b2
+        buf[2] = sense.asc;
Paolo Bonzini 0fb2b2
+        buf[3] = sense.ascq;
Paolo Bonzini 0fb2b2
+        return 8;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+int scsi_sense_to_errno(int key, int asc, int ascq)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    switch (key) {
Paolo Bonzini 0fb2b2
+    case 0x00: /* NO SENSE */
Paolo Bonzini 0fb2b2
+    case 0x01: /* RECOVERED ERROR */
Paolo Bonzini 0fb2b2
+    case 0x06: /* UNIT ATTENTION */
Paolo Bonzini 0fb2b2
+        /* These sense keys are not errors */
Paolo Bonzini 0fb2b2
+        return 0;
Paolo Bonzini 0fb2b2
+    case 0x0b: /* COMMAND ABORTED */
Paolo Bonzini 0fb2b2
+        return ECANCELED;
Paolo Bonzini 0fb2b2
+    case 0x02: /* NOT READY */
Paolo Bonzini 0fb2b2
+    case 0x05: /* ILLEGAL REQUEST */
Paolo Bonzini 0fb2b2
+    case 0x07: /* DATA PROTECTION */
Paolo Bonzini 0fb2b2
+        /* Parse ASCQ */
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    default:
Paolo Bonzini 0fb2b2
+        return EIO;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+    switch ((asc << 8) | ascq) {
Paolo Bonzini 0fb2b2
+    case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
Paolo Bonzini 0fb2b2
+    case 0x2000: /* INVALID OPERATION CODE */
Paolo Bonzini 0fb2b2
+    case 0x2400: /* INVALID FIELD IN CDB */
Paolo Bonzini 0fb2b2
+    case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
Paolo Bonzini 0fb2b2
+        return EINVAL;
Paolo Bonzini 0fb2b2
+    case 0x2100: /* LBA OUT OF RANGE */
Paolo Bonzini 0fb2b2
+    case 0x2707: /* SPACE ALLOC FAILED */
Paolo Bonzini 0fb2b2
+        return ENOSPC;
Paolo Bonzini 0fb2b2
+    case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
Paolo Bonzini 0fb2b2
+        return ENOTSUP;
Paolo Bonzini 0fb2b2
+    case 0x3a00: /* MEDIUM NOT PRESENT */
Paolo Bonzini 0fb2b2
+    case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
Paolo Bonzini 0fb2b2
+    case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
Paolo Bonzini 0fb2b2
+        return ENOMEDIUM;
Paolo Bonzini 0fb2b2
+    case 0x2700: /* WRITE PROTECTED */
Paolo Bonzini 0fb2b2
+        return EACCES;
Paolo Bonzini 0fb2b2
+    case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
Paolo Bonzini 0fb2b2
+        return EAGAIN;
Paolo Bonzini 0fb2b2
+    case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
Paolo Bonzini 0fb2b2
+        return ENOTCONN;
Paolo Bonzini 0fb2b2
+    default:
Paolo Bonzini 0fb2b2
+        return EIO;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    int key, asc, ascq;
Paolo Bonzini 0fb2b2
+    if (sense_size < 1) {
Paolo Bonzini 0fb2b2
+        return EIO;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+    switch (sense[0]) {
Paolo Bonzini 0fb2b2
+    case 0x70: /* Fixed format sense data. */
Paolo Bonzini 0fb2b2
+        if (sense_size < 14) {
Paolo Bonzini 0fb2b2
+            return EIO;
Paolo Bonzini 0fb2b2
+        }
Paolo Bonzini 0fb2b2
+        key = sense[2] & 0xF;
Paolo Bonzini 0fb2b2
+        asc = sense[12];
Paolo Bonzini 0fb2b2
+        ascq = sense[13];
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    case 0x72: /* Descriptor format sense data. */
Paolo Bonzini 0fb2b2
+        if (sense_size < 4) {
Paolo Bonzini 0fb2b2
+            return EIO;
Paolo Bonzini 0fb2b2
+        }
Paolo Bonzini 0fb2b2
+        key = sense[1] & 0xF;
Paolo Bonzini 0fb2b2
+        asc = sense[2];
Paolo Bonzini 0fb2b2
+        ascq = sense[3];
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    default:
Paolo Bonzini 0fb2b2
+        return EIO;
Paolo Bonzini 0fb2b2
+        break;
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+    return scsi_sense_to_errno(key, asc, ascq);
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+const char *scsi_command_name(uint8_t cmd)
Paolo Bonzini 0fb2b2
+{
Paolo Bonzini 0fb2b2
+    static const char *names[] = {
Paolo Bonzini 0fb2b2
+        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
Paolo Bonzini 0fb2b2
+        [ REWIND                   ] = "REWIND",
Paolo Bonzini 0fb2b2
+        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
Paolo Bonzini 0fb2b2
+        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
Paolo Bonzini 0fb2b2
+        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
Paolo Bonzini 0fb2b2
+        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
Paolo Bonzini 0fb2b2
+        /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
Paolo Bonzini 0fb2b2
+        [ READ_6                   ] = "READ_6",
Paolo Bonzini 0fb2b2
+        [ WRITE_6                  ] = "WRITE_6",
Paolo Bonzini 0fb2b2
+        [ SET_CAPACITY             ] = "SET_CAPACITY",
Paolo Bonzini 0fb2b2
+        [ READ_REVERSE             ] = "READ_REVERSE",
Paolo Bonzini 0fb2b2
+        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
Paolo Bonzini 0fb2b2
+        [ SPACE                    ] = "SPACE",
Paolo Bonzini 0fb2b2
+        [ INQUIRY                  ] = "INQUIRY",
Paolo Bonzini 0fb2b2
+        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
Paolo Bonzini 0fb2b2
+        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
Paolo Bonzini 0fb2b2
+        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
Paolo Bonzini 0fb2b2
+        [ MODE_SELECT              ] = "MODE_SELECT",
Paolo Bonzini 0fb2b2
+        [ RESERVE                  ] = "RESERVE",
Paolo Bonzini 0fb2b2
+        [ RELEASE                  ] = "RELEASE",
Paolo Bonzini 0fb2b2
+        [ COPY                     ] = "COPY",
Paolo Bonzini 0fb2b2
+        [ ERASE                    ] = "ERASE",
Paolo Bonzini 0fb2b2
+        [ MODE_SENSE               ] = "MODE_SENSE",
Paolo Bonzini 0fb2b2
+        [ START_STOP               ] = "START_STOP/LOAD_UNLOAD",
Paolo Bonzini 0fb2b2
+        /* LOAD_UNLOAD and START_STOP use the same operation code */
Paolo Bonzini 0fb2b2
+        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
Paolo Bonzini 0fb2b2
+        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
Paolo Bonzini 0fb2b2
+        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
Paolo Bonzini 0fb2b2
+        [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
Paolo Bonzini 0fb2b2
+        [ READ_10                  ] = "READ_10",
Paolo Bonzini 0fb2b2
+        [ WRITE_10                 ] = "WRITE_10",
Paolo Bonzini 0fb2b2
+        [ SEEK_10                  ] = "SEEK_10/POSITION_TO_ELEMENT",
Paolo Bonzini 0fb2b2
+        /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
Paolo Bonzini 0fb2b2
+        [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
Paolo Bonzini 0fb2b2
+        [ VERIFY_10                ] = "VERIFY_10",
Paolo Bonzini 0fb2b2
+        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
Paolo Bonzini 0fb2b2
+        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
Paolo Bonzini 0fb2b2
+        [ SEARCH_LOW               ] = "SEARCH_LOW",
Paolo Bonzini 0fb2b2
+        [ SET_LIMITS               ] = "SET_LIMITS",
Paolo Bonzini 0fb2b2
+        [ PRE_FETCH                ] = "PRE_FETCH/READ_POSITION",
Paolo Bonzini 0fb2b2
+        /* READ_POSITION and PRE_FETCH use the same operation code */
Paolo Bonzini 0fb2b2
+        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
Paolo Bonzini 0fb2b2
+        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
Paolo Bonzini 0fb2b2
+        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
Paolo Bonzini 0fb2b2
+        /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
Paolo Bonzini 0fb2b2
+        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
Paolo Bonzini 0fb2b2
+        [ COMPARE                  ] = "COMPARE",
Paolo Bonzini 0fb2b2
+        [ COPY_VERIFY              ] = "COPY_VERIFY",
Paolo Bonzini 0fb2b2
+        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
Paolo Bonzini 0fb2b2
+        [ READ_BUFFER              ] = "READ_BUFFER",
Paolo Bonzini 0fb2b2
+        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
Paolo Bonzini 0fb2b2
+        [ READ_LONG_10             ] = "READ_LONG_10",
Paolo Bonzini 0fb2b2
+        [ WRITE_LONG_10            ] = "WRITE_LONG_10",
Paolo Bonzini 0fb2b2
+        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
Paolo Bonzini 0fb2b2
+        [ WRITE_SAME_10            ] = "WRITE_SAME_10",
Paolo Bonzini 0fb2b2
+        [ UNMAP                    ] = "UNMAP",
Paolo Bonzini 0fb2b2
+        [ READ_TOC                 ] = "READ_TOC",
Paolo Bonzini 0fb2b2
+        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
Paolo Bonzini 0fb2b2
+        [ SANITIZE                 ] = "SANITIZE",
Paolo Bonzini 0fb2b2
+        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
Paolo Bonzini 0fb2b2
+        [ LOG_SELECT               ] = "LOG_SELECT",
Paolo Bonzini 0fb2b2
+        [ LOG_SENSE                ] = "LOG_SENSE",
Paolo Bonzini 0fb2b2
+        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
Paolo Bonzini 0fb2b2
+        [ RESERVE_10               ] = "RESERVE_10",
Paolo Bonzini 0fb2b2
+        [ RELEASE_10               ] = "RELEASE_10",
Paolo Bonzini 0fb2b2
+        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
Paolo Bonzini 0fb2b2
+        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
Paolo Bonzini 0fb2b2
+        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
Paolo Bonzini 0fb2b2
+        [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
Paolo Bonzini 0fb2b2
+        [ EXTENDED_COPY            ] = "EXTENDED_COPY",
Paolo Bonzini 0fb2b2
+        [ ATA_PASSTHROUGH_16       ] = "ATA_PASSTHROUGH_16",
Paolo Bonzini 0fb2b2
+        [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
Paolo Bonzini 0fb2b2
+        [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
Paolo Bonzini 0fb2b2
+        [ READ_16                  ] = "READ_16",
Paolo Bonzini 0fb2b2
+        [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
Paolo Bonzini 0fb2b2
+        [ WRITE_16                 ] = "WRITE_16",
Paolo Bonzini 0fb2b2
+        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
Paolo Bonzini 0fb2b2
+        [ VERIFY_16                ] = "VERIFY_16",
Paolo Bonzini 0fb2b2
+        [ PRE_FETCH_16             ] = "PRE_FETCH_16",
Paolo Bonzini 0fb2b2
+        [ SYNCHRONIZE_CACHE_16     ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
Paolo Bonzini 0fb2b2
+        /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
Paolo Bonzini 0fb2b2
+        [ LOCATE_16                ] = "LOCATE_16",
Paolo Bonzini 0fb2b2
+        [ WRITE_SAME_16            ] = "ERASE_16/WRITE_SAME_16",
Paolo Bonzini 0fb2b2
+        /* ERASE_16 and WRITE_SAME_16 use the same operation code */
Paolo Bonzini 0fb2b2
+        [ SERVICE_ACTION_IN_16     ] = "SERVICE_ACTION_IN_16",
Paolo Bonzini 0fb2b2
+        [ WRITE_LONG_16            ] = "WRITE_LONG_16",
Paolo Bonzini 0fb2b2
+        [ REPORT_LUNS              ] = "REPORT_LUNS",
Paolo Bonzini 0fb2b2
+        [ ATA_PASSTHROUGH_12       ] = "BLANK/ATA_PASSTHROUGH_12",
Paolo Bonzini 0fb2b2
+        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
Paolo Bonzini 0fb2b2
+        [ EXCHANGE_MEDIUM          ] = "EXCHANGE MEDIUM",
Paolo Bonzini 0fb2b2
+        [ READ_12                  ] = "READ_12",
Paolo Bonzini 0fb2b2
+        [ WRITE_12                 ] = "WRITE_12",
Paolo Bonzini 0fb2b2
+        [ ERASE_12                 ] = "ERASE_12/GET_PERFORMANCE",
Paolo Bonzini 0fb2b2
+        /* ERASE_12 and GET_PERFORMANCE use the same operation code */
Paolo Bonzini 0fb2b2
+        [ SERVICE_ACTION_IN_12     ] = "SERVICE_ACTION_IN_12",
Paolo Bonzini 0fb2b2
+        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
Paolo Bonzini 0fb2b2
+        [ VERIFY_12                ] = "VERIFY_12",
Paolo Bonzini 0fb2b2
+        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
Paolo Bonzini 0fb2b2
+        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
Paolo Bonzini 0fb2b2
+        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
Paolo Bonzini 0fb2b2
+        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
Paolo Bonzini 0fb2b2
+        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG/SET_STREAMING",
Paolo Bonzini 0fb2b2
+        /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
Paolo Bonzini 0fb2b2
+        [ READ_CD                  ] = "READ_CD",
Paolo Bonzini 0fb2b2
+        [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
Paolo Bonzini 0fb2b2
+        [ READ_DVD_STRUCTURE       ] = "READ_DVD_STRUCTURE",
Paolo Bonzini 0fb2b2
+        [ RESERVE_TRACK            ] = "RESERVE_TRACK",
Paolo Bonzini 0fb2b2
+        [ SEND_CUE_SHEET           ] = "SEND_CUE_SHEET",
Paolo Bonzini 0fb2b2
+        [ SEND_DVD_STRUCTURE       ] = "SEND_DVD_STRUCTURE",
Paolo Bonzini 0fb2b2
+        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
Paolo Bonzini 0fb2b2
+        [ SET_READ_AHEAD           ] = "SET_READ_AHEAD",
Paolo Bonzini 0fb2b2
+        [ ALLOW_OVERWRITE          ] = "ALLOW_OVERWRITE",
Paolo Bonzini 0fb2b2
+        [ MECHANISM_STATUS         ] = "MECHANISM_STATUS",
Paolo Bonzini 0fb2b2
+        [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION",
Paolo Bonzini 0fb2b2
+        [ READ_DISC_INFORMATION    ] = "READ_DISC_INFORMATION",
Paolo Bonzini 0fb2b2
+    };
Paolo Bonzini 0fb2b2
+
Paolo Bonzini 0fb2b2
+    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) {
Paolo Bonzini 0fb2b2
+        return "*UNKNOWN*";
Paolo Bonzini 0fb2b2
+    }
Paolo Bonzini 0fb2b2
+    return names[cmd];
Paolo Bonzini 0fb2b2
+}
Paolo Bonzini 0fb2b2
diff --git a/util/Makefile.objs b/util/Makefile.objs
Paolo Bonzini 0fb2b2
index c9e6c493d3..50a55ecc75 100644
Paolo Bonzini 0fb2b2
--- a/util/Makefile.objs
Paolo Bonzini 0fb2b2
+++ b/util/Makefile.objs
Paolo Bonzini 0fb2b2
@@ -45,4 +45,3 @@ util-obj-y += qht.o
Paolo Bonzini 0fb2b2
 util-obj-y += range.o
Paolo Bonzini 0fb2b2
 util-obj-y += stats64.o
Paolo Bonzini 0fb2b2
 util-obj-y += systemd.o
Paolo Bonzini 0fb2b2
-util-obj-y += scsi.o
Paolo Bonzini 0fb2b2
diff --git a/util/scsi.c b/util/scsi.c
Paolo Bonzini 0fb2b2
deleted file mode 100644
Paolo Bonzini 0fb2b2
index 472293d59b..0000000000
Paolo Bonzini 0fb2b2
--- a/util/scsi.c
Paolo Bonzini 0fb2b2
+++ /dev/null
Paolo Bonzini 0fb2b2
@@ -1,90 +0,0 @@
Paolo Bonzini 0fb2b2
-/*
Paolo Bonzini 0fb2b2
- *  SCSI helpers
Paolo Bonzini 0fb2b2
- *
Paolo Bonzini 0fb2b2
- *  Copyright 2017 Red Hat, Inc.
Paolo Bonzini 0fb2b2
- *
Paolo Bonzini 0fb2b2
- *  Authors:
Paolo Bonzini 0fb2b2
- *   Fam Zheng <famz@redhat.com>
Paolo Bonzini 0fb2b2
- *
Paolo Bonzini 0fb2b2
- * This program is free software; you can redistribute it and/or modify it
Paolo Bonzini 0fb2b2
- * under the terms of the GNU General Public License as published by the Free
Paolo Bonzini 0fb2b2
- * Software Foundation; either version 2 of the License, or (at your option)
Paolo Bonzini 0fb2b2
- * any later version.
Paolo Bonzini 0fb2b2
- */
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-#include "qemu/osdep.h"
Paolo Bonzini 0fb2b2
-#include "scsi/scsi.h"
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-int scsi_sense_to_errno(int key, int asc, int ascq)
Paolo Bonzini 0fb2b2
-{
Paolo Bonzini 0fb2b2
-    switch (key) {
Paolo Bonzini 0fb2b2
-    case 0x00: /* NO SENSE */
Paolo Bonzini 0fb2b2
-    case 0x01: /* RECOVERED ERROR */
Paolo Bonzini 0fb2b2
-    case 0x06: /* UNIT ATTENTION */
Paolo Bonzini 0fb2b2
-        /* These sense keys are not errors */
Paolo Bonzini 0fb2b2
-        return 0;
Paolo Bonzini 0fb2b2
-    case 0x0b: /* COMMAND ABORTED */
Paolo Bonzini 0fb2b2
-        return ECANCELED;
Paolo Bonzini 0fb2b2
-    case 0x02: /* NOT READY */
Paolo Bonzini 0fb2b2
-    case 0x05: /* ILLEGAL REQUEST */
Paolo Bonzini 0fb2b2
-    case 0x07: /* DATA PROTECTION */
Paolo Bonzini 0fb2b2
-        /* Parse ASCQ */
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    default:
Paolo Bonzini 0fb2b2
-        return EIO;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-    switch ((asc << 8) | ascq) {
Paolo Bonzini 0fb2b2
-    case 0x1a00: /* PARAMETER LIST LENGTH ERROR */
Paolo Bonzini 0fb2b2
-    case 0x2000: /* INVALID OPERATION CODE */
Paolo Bonzini 0fb2b2
-    case 0x2400: /* INVALID FIELD IN CDB */
Paolo Bonzini 0fb2b2
-    case 0x2600: /* INVALID FIELD IN PARAMETER LIST */
Paolo Bonzini 0fb2b2
-        return EINVAL;
Paolo Bonzini 0fb2b2
-    case 0x2100: /* LBA OUT OF RANGE */
Paolo Bonzini 0fb2b2
-    case 0x2707: /* SPACE ALLOC FAILED */
Paolo Bonzini 0fb2b2
-        return ENOSPC;
Paolo Bonzini 0fb2b2
-    case 0x2500: /* LOGICAL UNIT NOT SUPPORTED */
Paolo Bonzini 0fb2b2
-        return ENOTSUP;
Paolo Bonzini 0fb2b2
-    case 0x3a00: /* MEDIUM NOT PRESENT */
Paolo Bonzini 0fb2b2
-    case 0x3a01: /* MEDIUM NOT PRESENT TRAY CLOSED */
Paolo Bonzini 0fb2b2
-    case 0x3a02: /* MEDIUM NOT PRESENT TRAY OPEN */
Paolo Bonzini 0fb2b2
-        return ENOMEDIUM;
Paolo Bonzini 0fb2b2
-    case 0x2700: /* WRITE PROTECTED */
Paolo Bonzini 0fb2b2
-        return EACCES;
Paolo Bonzini 0fb2b2
-    case 0x0401: /* NOT READY, IN PROGRESS OF BECOMING READY */
Paolo Bonzini 0fb2b2
-        return EAGAIN;
Paolo Bonzini 0fb2b2
-    case 0x0402: /* NOT READY, INITIALIZING COMMAND REQUIRED */
Paolo Bonzini 0fb2b2
-        return ENOTCONN;
Paolo Bonzini 0fb2b2
-    default:
Paolo Bonzini 0fb2b2
-        return EIO;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-
Paolo Bonzini 0fb2b2
-int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size)
Paolo Bonzini 0fb2b2
-{
Paolo Bonzini 0fb2b2
-    int key, asc, ascq;
Paolo Bonzini 0fb2b2
-    if (sense_size < 1) {
Paolo Bonzini 0fb2b2
-        return EIO;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-    switch (sense[0]) {
Paolo Bonzini 0fb2b2
-    case 0x70: /* Fixed format sense data. */
Paolo Bonzini 0fb2b2
-        if (sense_size < 14) {
Paolo Bonzini 0fb2b2
-            return EIO;
Paolo Bonzini 0fb2b2
-        }
Paolo Bonzini 0fb2b2
-        key = sense[2] & 0xF;
Paolo Bonzini 0fb2b2
-        asc = sense[12];
Paolo Bonzini 0fb2b2
-        ascq = sense[13];
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    case 0x72: /* Descriptor format sense data. */
Paolo Bonzini 0fb2b2
-        if (sense_size < 4) {
Paolo Bonzini 0fb2b2
-            return EIO;
Paolo Bonzini 0fb2b2
-        }
Paolo Bonzini 0fb2b2
-        key = sense[1] & 0xF;
Paolo Bonzini 0fb2b2
-        asc = sense[2];
Paolo Bonzini 0fb2b2
-        ascq = sense[3];
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    default:
Paolo Bonzini 0fb2b2
-        return EIO;
Paolo Bonzini 0fb2b2
-        break;
Paolo Bonzini 0fb2b2
-    }
Paolo Bonzini 0fb2b2
-    return scsi_sense_to_errno(key, asc, ascq);
Paolo Bonzini 0fb2b2
-}
Paolo Bonzini 0fb2b2
-- 
Paolo Bonzini 0fb2b2
2.13.5
Paolo Bonzini 0fb2b2