Blob Blame History Raw
From f635140407bf6d024045ad18e7a1f8f802690b36 Mon Sep 17 00:00:00 2001
From: Paolo Bonzini <pbonzini@redhat.com>
Date: Sat, 2 Dec 2017 12:19:42 +0100
Subject: [PATCH 16/36] scsi: Introduce scsi_sense_buf_to_errno

RH-Author: Paolo Bonzini <pbonzini@redhat.com>
Message-id: <20171202121953.13317-7-pbonzini@redhat.com>
Patchwork-id: 78082
O-Subject: [RHEL7.4 qemu-kvm-rhev PATCH 06/17] scsi: Introduce scsi_sense_buf_to_errno
Bugzilla: 1464908
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
RH-Acked-by: John Snow <jsnow@redhat.com>

From: Fam Zheng <famz@redhat.com>

This recognizes the "fixed" and "descriptor" format sense data, extracts
the sense key/asc/ascq fields then converts them to an errno.

Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20170821141008.19383-4-famz@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
(cherry picked from commit a485b23425c755867d6caa6ab590be4e0473e35a)
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 include/scsi/scsi.h |  1 +
 util/scsi.c         | 30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/include/scsi/scsi.h b/include/scsi/scsi.h
index f894ace..fe33038 100644
--- a/include/scsi/scsi.h
+++ b/include/scsi/scsi.h
@@ -15,5 +15,6 @@
 #define QEMU_SCSI_H
 
 int scsi_sense_to_errno(int key, int asc, int ascq);
+int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size);
 
 #endif
diff --git a/util/scsi.c b/util/scsi.c
index 472eb5b..472293d 100644
--- a/util/scsi.c
+++ b/util/scsi.c
@@ -58,3 +58,33 @@ int scsi_sense_to_errno(int key, int asc, int ascq)
         return EIO;
     }
 }
+
+int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size)
+{
+    int key, asc, ascq;
+    if (sense_size < 1) {
+        return EIO;
+    }
+    switch (sense[0]) {
+    case 0x70: /* Fixed format sense data. */
+        if (sense_size < 14) {
+            return EIO;
+        }
+        key = sense[2] & 0xF;
+        asc = sense[12];
+        ascq = sense[13];
+        break;
+    case 0x72: /* Descriptor format sense data. */
+        if (sense_size < 4) {
+            return EIO;
+        }
+        key = sense[1] & 0xF;
+        asc = sense[2];
+        ascq = sense[3];
+        break;
+    default:
+        return EIO;
+        break;
+    }
+    return scsi_sense_to_errno(key, asc, ascq);
+}
-- 
1.8.3.1