Skip to contentSkip to conversation

virtio-scsi: Send "REPORTED LUNS CHANGED" sense data upon disk hotplug events.

PATCH Superseded Venu Busireddy 2 participants 3 messages Sep 15, 2022 – Sep 21, 2022

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 virtio-scsi: Send "REPORTED LUNS CHANGED" sense data upon disk hotplug events.

Section 5.6.6.3 of VirtIO specification states, "Events will also be reported via sense codes..." However, no sense data is sent when VIRTIO_SCSI_EVT_RESET_RESCAN or VIRTIO_SCSI_EVT_RESET_REMOVED events are reported (when disk hotplug/hotunplug events occur). SCSI layer on Solaris depends on this sense data, and hence does not handle disk hotplug/hotunplug events.

As specified in SAM-4, Section 5.14 (h), return a CHECK_CONDITION status with sense data of 0x06/0x3F/0x0E (sense code REPORTED_LUNS_CHANGED) when the disk inventory changes and a command other than INQUIRY, REPORT_LUNS, or REQUEST_SENSE is received.

v1 -> v2:
- Send the sense data for VIRTIO_SCSI_EVT_RESET_REMOVED event too.

---
 hw/scsi/virtio-scsi.c           | 16 +++++++++++++++-
 include/hw/virtio/virtio-scsi.h |  1 +
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/scsi/virtio-scsi.c +15 -1
index 41f2a5630173..69194c7ae23c 100644
@@ -608,7 +608,19 @@ static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid)
 
     req->resp.cmd.response = VIRTIO_SCSI_S_OK;
     req->resp.cmd.status = r->status;
-    if (req->resp.cmd.status == GOOD) {
+    if (req->dev->reported_luns_changed &&
+            (req->req.cmd.cdb[0] != INQUIRY) &&
+            (req->req.cmd.cdb[0] != REPORT_LUNS) &&
+            (req->req.cmd.cdb[0] != REQUEST_SENSE)) {
+        req->dev->reported_luns_changed = false;
+        req->resp.cmd.resid = 0;
+        req->resp.cmd.status_qualifier = 0;
+        req->resp.cmd.status = CHECK_CONDITION;
+        sense_len = scsi_build_sense(sense, SENSE_CODE(REPORTED_LUNS_CHANGED));
+        qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
+                            sense, sense_len);
+        req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
+    } else if (req->resp.cmd.status == GOOD) {
         req->resp.cmd.resid = virtio_tswap32(vdev, resid);
     } else {
         req->resp.cmd.resid = 0;
@@ -956,6 +968,7 @@ static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
         virtio_scsi_push_event(s, sd,
                                VIRTIO_SCSI_T_TRANSPORT_RESET,
                                VIRTIO_SCSI_EVT_RESET_RESCAN);
+        s->reported_luns_changed = true;
         virtio_scsi_release(s);
     }
 }
@@ -973,6 +986,7 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
         virtio_scsi_push_event(s, sd,
                                VIRTIO_SCSI_T_TRANSPORT_RESET,
                                VIRTIO_SCSI_EVT_RESET_REMOVED);
+        s->reported_luns_changed = true;
         virtio_scsi_release(s);
     }
 
include/hw/virtio/virtio-scsi.h +1 -0
index a36aad9c8695..efbcf9ba069a 100644
@@ -81,6 +81,7 @@ struct VirtIOSCSI {
     SCSIBus bus;
     int resetting;
     bool events_dropped;
+    bool reported_luns_changed;
 
     /* Fields for dataplane below */
     AioContext *ctx; /* one iothread per virtio-scsi-pci for now */
#1 lore raw
Quoted 15 lines · Venu Busireddy
hw/scsi/virtio-scsi.c +6 -1
index 41f2a5630173..69194c7ae23c 100644
@@ -608,7 +608,19 @@ static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid)
 
     req->resp.cmd.response = VIRTIO_SCSI_S_OK;
     req->resp.cmd.status = r->status;
-    if (req->resp.cmd.status == GOOD) {
+    if (req->dev->reported_luns_changed &&
+            (req->req.cmd.cdb[0] != INQUIRY) &&
+            (req->req.cmd.cdb[0] != REPORT_LUNS) &&
+            (req->req.cmd.cdb[0] != REQUEST_SENSE)) {
+        req->dev->reported_luns_changed = false;
+        req->resp.cmd.resid = 0;

hw/scsi/virtio-scsi.c static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid) · line 626

Quoted 10 lines
hw/scsi/virtio-scsi.c +7 -0
+        req->resp.cmd.status_qualifier = 0;
+        req->resp.cmd.status = CHECK_CONDITION;
+        sense_len = scsi_build_sense(sense, SENSE_CODE(REPORTED_LUNS_CHANGED));
+        qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
+                            sense, sense_len);
+        req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
+    } else if (req->resp.cmd.status == GOOD) {
         req->resp.cmd.resid = virtio_tswap32(vdev, resid);
     } else {
         req->resp.cmd.resid = 0;

Hi,

a unit attention sense must be sent _instead_ of executing the command.

QEMU already has a function scsi_device_set_ua() that handles everything; you have to call it, if reported_luns_changed is true, from virtio_scsi_handle_cmd_req_prepare() before scsi_req_new().

It will also skip GET_CONFIGURATION and GET_EVENT_STATUS_NOTIFICATION commands which are further special-cased in 4.1.6.1 of the MMC specification.

Thanks,

Paolo

Quoted 29 lines
hw/scsi/virtio-scsi.c +2 -0
@@ -956,6 +968,7 @@ static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
         virtio_scsi_push_event(s, sd,
                                VIRTIO_SCSI_T_TRANSPORT_RESET,
                                VIRTIO_SCSI_EVT_RESET_RESCAN);
+        s->reported_luns_changed = true;
         virtio_scsi_release(s);
     }
 }
@@ -973,6 +986,7 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
         virtio_scsi_push_event(s, sd,
                                VIRTIO_SCSI_T_TRANSPORT_RESET,
                                VIRTIO_SCSI_EVT_RESET_REMOVED);
+        s->reported_luns_changed = true;
         virtio_scsi_release(s);
     }
 
include/hw/virtio/virtio-scsi.h +1 -0
index a36aad9c8695..efbcf9ba069a 100644
@@ -81,6 +81,7 @@ struct VirtIOSCSI {
     SCSIBus bus;
     int resetting;
     bool events_dropped;
+    bool reported_luns_changed;
 
     /* Fields for dataplane below */
     AioContext *ctx; /* one iothread per virtio-scsi-pci for now */
#2 lore raw
Quoted 32 lines · Paolo Bonzini
Quoted 15 lines · Venu Busireddy
hw/scsi/virtio-scsi.c +6 -1
index 41f2a5630173..69194c7ae23c 100644
@@ -608,7 +608,19 @@ static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid)
 
     req->resp.cmd.response = VIRTIO_SCSI_S_OK;
     req->resp.cmd.status = r->status;
-    if (req->resp.cmd.status == GOOD) {
+    if (req->dev->reported_luns_changed &&
+            (req->req.cmd.cdb[0] != INQUIRY) &&
+            (req->req.cmd.cdb[0] != REPORT_LUNS) &&
+            (req->req.cmd.cdb[0] != REQUEST_SENSE)) {
+        req->dev->reported_luns_changed = false;
+        req->resp.cmd.resid = 0;

hw/scsi/virtio-scsi.c static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid) · line 626

Quoted 10 lines
hw/scsi/virtio-scsi.c +7 -0
+        req->resp.cmd.status_qualifier = 0;
+        req->resp.cmd.status = CHECK_CONDITION;
+        sense_len = scsi_build_sense(sense, SENSE_CODE(REPORTED_LUNS_CHANGED));
+        qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
+                            sense, sense_len);
+        req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
+    } else if (req->resp.cmd.status == GOOD) {
         req->resp.cmd.resid = virtio_tswap32(vdev, resid);
     } else {
         req->resp.cmd.resid = 0;

Hi,

a unit attention sense must be sent _instead_ of executing the command.

Quoted 7 lines

QEMU already has a function scsi_device_set_ua() that handles everything; you have to call it, if reported_luns_changed is true, from virtio_scsi_handle_cmd_req_prepare() before scsi_req_new().

It will also skip GET_CONFIGURATION and GET_EVENT_STATUS_NOTIFICATION commands which are further special-cased in 4.1.6.1 of the MMC specification.

Thanks, Paolo. I will test your suggestion (as soon as I finish what I am working currently), and get back with either more questions, or with a v3 post.

Venu

Quoted 30 lines
Quoted 29 lines
hw/scsi/virtio-scsi.c +2 -0
@@ -956,6 +968,7 @@ static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
         virtio_scsi_push_event(s, sd,
                                VIRTIO_SCSI_T_TRANSPORT_RESET,
                                VIRTIO_SCSI_EVT_RESET_RESCAN);
+        s->reported_luns_changed = true;
         virtio_scsi_release(s);
     }
 }
@@ -973,6 +986,7 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
         virtio_scsi_push_event(s, sd,
                                VIRTIO_SCSI_T_TRANSPORT_RESET,
                                VIRTIO_SCSI_EVT_RESET_REMOVED);
+        s->reported_luns_changed = true;
         virtio_scsi_release(s);
     }
 
include/hw/virtio/virtio-scsi.h +1 -0
index a36aad9c8695..efbcf9ba069a 100644
@@ -81,6 +81,7 @@ struct VirtIOSCSI {
     SCSIBus bus;
     int resetting;
     bool events_dropped;
+    bool reported_luns_changed;
 
     /* Fields for dataplane below */
     AioContext *ctx; /* one iothread per virtio-scsi-pci for now */