Blame SOURCES/kvm-scsi-introduce-sg_io_sense_from_errno.patch

4a2fec
From b1aa9df721c90e02c917eba8f753fa50d3c1e952 Mon Sep 17 00:00:00 2001
4a2fec
From: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
Date: Sat, 2 Dec 2017 12:19:46 +0100
4a2fec
Subject: [PATCH 20/36] scsi: introduce sg_io_sense_from_errno
4a2fec
4a2fec
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
Message-id: <20171202121953.13317-11-pbonzini@redhat.com>
4a2fec
Patchwork-id: 78080
4a2fec
O-Subject: [RHEL7.4 qemu-kvm-rhev PATCH 10/17] scsi: introduce sg_io_sense_from_errno
4a2fec
Bugzilla: 1464908
4a2fec
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
4a2fec
RH-Acked-by: John Snow <jsnow@redhat.com>
4a2fec
4a2fec
Move more knowledge of SG_IO out of hw/scsi/scsi-generic.c, for
4a2fec
reusability.
4a2fec
4a2fec
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
(cherry picked from commit 1ead6b4e242e59711976cdf2502dd5c7cd5d340a)
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 hw/scsi/scsi-generic.c | 40 +++++++---------------------------------
4a2fec
 include/scsi/utils.h   |  3 +++
4a2fec
 scsi/utils.c           | 35 +++++++++++++++++++++++++++++++++++
4a2fec
 3 files changed, 45 insertions(+), 33 deletions(-)
4a2fec
4a2fec
diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c
4a2fec
index 7a8f500..04c687e 100644
4a2fec
--- a/hw/scsi/scsi-generic.c
4a2fec
+++ b/hw/scsi/scsi-generic.c
4a2fec
@@ -81,6 +81,7 @@ static void scsi_free_request(SCSIRequest *req)
4a2fec
 static void scsi_command_complete_noio(SCSIGenericReq *r, int ret)
4a2fec
 {
4a2fec
     int status;
4a2fec
+    SCSISense sense;
4a2fec
 
4a2fec
     assert(r->req.aiocb == NULL);
4a2fec
 
4a2fec
@@ -88,42 +89,15 @@ static void scsi_command_complete_noio(SCSIGenericReq *r, int ret)
4a2fec
         scsi_req_cancel_complete(&r->req);
4a2fec
         goto done;
4a2fec
     }
4a2fec
-    if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) {
4a2fec
-        r->req.sense_len = r->io_header.sb_len_wr;
4a2fec
-    }
4a2fec
-
4a2fec
-    if (ret != 0) {
4a2fec
-        switch (ret) {
4a2fec
-        case -EDOM:
4a2fec
-            status = TASK_SET_FULL;
4a2fec
-            break;
4a2fec
-        case -ENOMEM:
4a2fec
-            status = CHECK_CONDITION;
4a2fec
-            scsi_req_build_sense(&r->req, SENSE_CODE(TARGET_FAILURE));
4a2fec
-            break;
4a2fec
-        default:
4a2fec
-            status = CHECK_CONDITION;
4a2fec
-            scsi_req_build_sense(&r->req, SENSE_CODE(IO_ERROR));
4a2fec
-            break;
4a2fec
-        }
4a2fec
-    } else {
4a2fec
-        if (r->io_header.host_status == SG_ERR_DID_NO_CONNECT ||
4a2fec
-            r->io_header.host_status == SG_ERR_DID_BUS_BUSY ||
4a2fec
-            r->io_header.host_status == SG_ERR_DID_TIME_OUT ||
4a2fec
-            (r->io_header.driver_status & SG_ERR_DRIVER_TIMEOUT)) {
4a2fec
-            status = BUSY;
4a2fec
-            BADF("Driver Timeout\n");
4a2fec
-        } else if (r->io_header.host_status) {
4a2fec
-            status = CHECK_CONDITION;
4a2fec
-            scsi_req_build_sense(&r->req, SENSE_CODE(I_T_NEXUS_LOSS));
4a2fec
-        } else if (r->io_header.status) {
4a2fec
-            status = r->io_header.status;
4a2fec
-        } else if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) {
4a2fec
-            status = CHECK_CONDITION;
4a2fec
+    status = sg_io_sense_from_errno(-ret, &r->io_header, &sense);
4a2fec
+    if (status == CHECK_CONDITION) {
4a2fec
+        if (r->io_header.driver_status & SG_ERR_DRIVER_SENSE) {
4a2fec
+            r->req.sense_len = r->io_header.sb_len_wr;
4a2fec
         } else {
4a2fec
-            status = GOOD;
4a2fec
+            scsi_req_build_sense(&r->req, sense);
4a2fec
         }
4a2fec
     }
4a2fec
+
4a2fec
     DPRINTF("Command complete 0x%p tag=0x%x status=%d\n",
4a2fec
             r, r->req.tag, status);
4a2fec
 
4a2fec
diff --git a/include/scsi/utils.h b/include/scsi/utils.h
4a2fec
index b49392d..d301b31 100644
4a2fec
--- a/include/scsi/utils.h
4a2fec
+++ b/include/scsi/utils.h
4a2fec
@@ -116,6 +116,9 @@ int scsi_cdb_length(uint8_t *buf);
4a2fec
 #define SG_ERR_DID_TIME_OUT    0x03
4a2fec
 
4a2fec
 #define SG_ERR_DRIVER_SENSE    0x08
4a2fec
+
4a2fec
+int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
4a2fec
+                           SCSISense *sense);
4a2fec
 #endif
4a2fec
 
4a2fec
 #endif
4a2fec
diff --git a/scsi/utils.c b/scsi/utils.c
4a2fec
index 89d9167..6ee9f40 100644
4a2fec
--- a/scsi/utils.c
4a2fec
+++ b/scsi/utils.c
4a2fec
@@ -501,3 +501,38 @@ const char *scsi_command_name(uint8_t cmd)
4a2fec
     }
4a2fec
     return names[cmd];
4a2fec
 }
4a2fec
+
4a2fec
+#ifdef CONFIG_LINUX
4a2fec
+int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
4a2fec
+                           SCSISense *sense)
4a2fec
+{
4a2fec
+    if (errno_value != 0) {
4a2fec
+        switch (errno_value) {
4a2fec
+        case EDOM:
4a2fec
+            return TASK_SET_FULL;
4a2fec
+        case ENOMEM:
4a2fec
+            *sense = SENSE_CODE(TARGET_FAILURE);
4a2fec
+            return CHECK_CONDITION;
4a2fec
+        default:
4a2fec
+            *sense = SENSE_CODE(IO_ERROR);
4a2fec
+            return CHECK_CONDITION;
4a2fec
+        }
4a2fec
+    } else {
4a2fec
+        if (io_hdr->host_status == SG_ERR_DID_NO_CONNECT ||
4a2fec
+            io_hdr->host_status == SG_ERR_DID_BUS_BUSY ||
4a2fec
+            io_hdr->host_status == SG_ERR_DID_TIME_OUT ||
4a2fec
+            (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT)) {
4a2fec
+            return BUSY;
4a2fec
+        } else if (io_hdr->host_status) {
4a2fec
+            *sense = SENSE_CODE(I_T_NEXUS_LOSS);
4a2fec
+            return CHECK_CONDITION;
4a2fec
+        } else if (io_hdr->status) {
4a2fec
+            return io_hdr->status;
4a2fec
+        } else if (io_hdr->driver_status & SG_ERR_DRIVER_SENSE) {
4a2fec
+            return CHECK_CONDITION;
4a2fec
+        } else {
4a2fec
+            return GOOD;
4a2fec
+        }
4a2fec
+    }
4a2fec
+}
4a2fec
+#endif
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec