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

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