qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Tony Asleson <tasleson@redhat.com>
To: qemu-devel@nongnu.org, kwolf@redhat.com
Subject: [RFC 2/4] SCSI media error reporting
Date: Thu, 19 Sep 2019 14:48:45 -0500	[thread overview]
Message-ID: <20190919194847.18518-3-tasleson@redhat.com> (raw)
In-Reply-To: <20190919194847.18518-1-tasleson@redhat.com>

Add abilility to return media errors for SCSI block devices.  Needs
improvement.

Signed-off-by: Tony Asleson <tasleson@redhat.com>
---
 hw/scsi/scsi-disk.c  | 33 +++++++++++++++++++++++++++++++++
 include/scsi/utils.h |  4 ++++
 scsi/utils.c         | 31 +++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)

diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
index 915641a0f1..a7f3274cdf 100644
--- a/hw/scsi/scsi-disk.c
+++ b/hw/scsi/scsi-disk.c
@@ -37,6 +37,7 @@
 #include "sysemu/dma.h"
 #include "qemu/cutils.h"
 #include "trace.h"
+#include "block/error_inject.h"
 
 #ifdef __linux
 #include <scsi/sg.h>
@@ -132,6 +133,18 @@ static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense)
     scsi_req_complete(&r->req, CHECK_CONDITION);
 }
 
+/* Helper function for SCSI media error  */
+static void scsi_media_error(SCSIDiskReq *r, SCSISense sense, uint32_t lba)
+{
+    trace_scsi_disk_check_condition(r->req.tag, sense.key, sense.asc,
+                                    sense.ascq);
+
+    r->req.sense_len = scsi_build_sense_buf_info(r->req.sense,
+                                                  SCSI_SENSE_LEN, sense, lba,
+                                                  0x80, 32);
+    scsi_req_complete(&r->req, CHECK_CONDITION);
+}
+
 static void scsi_init_iovec(SCSIDiskReq *r, size_t size)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
@@ -2170,6 +2183,26 @@ static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf)
         }
         r->sector = r->req.cmd.lba * (s->qdev.blocksize / 512);
         r->sector_count = len * (s->qdev.blocksize / 512);
+
+        /*
+         * TODO Move this check to a more appropriate spot.  Additionally we
+         * also need to transfer the data that was read up before encountering
+         * the media error.
+         */
+        uint64_t error_sector = 0;
+        char device_id[32];
+        sprintf(device_id, "%lu", s->qdev.wwn);
+        if (error_in_read(device_id, r->sector, r->sector_count,
+                          &error_sector)) {
+            /*
+             * TODO Fix error reporting for disks > 2TiB
+             */
+            if (error_sector > 0xFFFFFFFF) {
+                error_sector = 0xFFFFFFFF;
+            }
+            scsi_media_error(r, SENSE_CODE(READ_ERROR), (uint32_t)error_sector);
+            return 0;
+        }
         break;
     case WRITE_6:
     case WRITE_10:
diff --git a/include/scsi/utils.h b/include/scsi/utils.h
index fbc5588279..02ae74287f 100644
--- a/include/scsi/utils.h
+++ b/include/scsi/utils.h
@@ -35,6 +35,10 @@ SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len);
 int scsi_build_sense_buf(uint8_t *buf, size_t max_size, SCSISense sense,
                          bool fixed_sense);
 
+int scsi_build_sense_buf_info(uint8_t *out_buf, size_t size, SCSISense sense,
+                              uint32_t information, uint8_t sksv,
+                              uint16_t sense_key_specific_info);
+
 /*
  * Predefined sense codes
  */
diff --git a/scsi/utils.c b/scsi/utils.c
index c50e81fdb8..9c9f1735d0 100644
--- a/scsi/utils.c
+++ b/scsi/utils.c
@@ -147,6 +147,37 @@ int scsi_build_sense_buf(uint8_t *out_buf, size_t size, SCSISense sense,
     return len;
 }
 
+int scsi_build_sense_buf_info(uint8_t *out_buf, size_t size, SCSISense sense,
+                                uint32_t information, uint8_t sksv,
+                                uint16_t sense_key_specific_info)
+{
+    uint8_t buf[SCSI_SENSE_LEN] = { 0 };
+
+    buf[0] = 0xF0;  /* Valid bit set. */
+    buf[2] = sense.key;
+
+    /*
+     * Set bytes 3, 4, 5, 6 value of information field
+     */
+    *((uint32_t *)(&buf[3])) = cpu_to_be32(information);
+
+    buf[7] = 10;
+    buf[12] = sense.asc;
+    buf[13] = sense.ascq;
+
+    if (sksv) {
+        buf[15] = sksv;
+        /*
+         * Set bytes 16, 17 to Sense-key specific bytes
+         */
+        *((uint16_t *)&buf[16]) = cpu_to_be16(sense_key_specific_info);
+    }
+
+    int len = MIN(SCSI_SENSE_LEN, size);
+    memcpy(out_buf, buf, len);
+    return len;
+}
+
 int scsi_build_sense(uint8_t *buf, SCSISense sense)
 {
     return scsi_build_sense_buf(buf, SCSI_SENSE_LEN, sense, true);
-- 
2.21.0



  parent reply	other threads:[~2019-09-19 20:08 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-19 19:48 [RFC 0/4] POC: Generating realistic block errors Tony Asleson
2019-09-19 19:48 ` [RFC 1/4] Add qapi for block error injection Tony Asleson
2019-09-20  9:03   ` Philippe Mathieu-Daudé
2019-09-20 15:17     ` Tony Asleson
2019-09-19 19:48 ` Tony Asleson [this message]
2019-09-19 19:48 ` [RFC 3/4] NVMe media error reporting Tony Asleson
2019-09-19 19:48 ` [RFC 4/4] ahci " Tony Asleson
2019-09-19 20:43   ` John Snow
2019-09-19 21:49     ` Tony Asleson
2019-09-20 17:22       ` John Snow
2019-09-20  8:43     ` Kevin Wolf
2019-09-20 16:18       ` John Snow
2019-09-20 19:25         ` Tony Asleson
2019-09-20 19:29           ` John Snow
2019-09-20  8:36 ` [RFC 0/4] POC: Generating realistic block errors Kevin Wolf
2019-09-20 16:41   ` Tony Asleson
2019-09-20 17:08     ` Eric Blake
2019-09-20 19:15       ` Tony Asleson
2019-09-20 18:11     ` Kevin Wolf
2019-09-20 18:55       ` Tony Asleson
2019-09-30 14:54         ` Kevin Wolf
2019-09-20  9:22 ` Stefan Hajnoczi
2019-09-20 17:28   ` Tony Asleson
2019-11-14 15:47     ` Tony Asleson
2019-11-21 10:30       ` Stefan Hajnoczi
2019-11-21 11:12         ` Kevin Wolf
2019-11-26 18:19         ` Tony Asleson
2019-11-26 19:28           ` Kevin Wolf
2019-09-20  9:25 ` Stefan Hajnoczi
2019-09-20 14:41 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190919194847.18518-3-tasleson@redhat.com \
    --to=tasleson@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).