Skip to contentSkip to conversation

spapr_vscsi: Fix REPORT_LUNS handling

PATCH Superseded Nathan Whitehorn 4 participants 13 messages Oct 18, 2013 – Jan 12, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spapr_vscsi: Fix REPORT_LUNS handling

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

---
 hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +57 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
 #define SCSI_SENSE_BUF_SIZE     96
 #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
 typedef union vscsi_crq {
     struct viosrp_crq s;
     uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
     }
 }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;
+
+    resp_data = malloc(len);
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else
+            resp_data[i] = (2 << 6);
+        resp_data[i] |= dev->id;
+        resp_data[i+1] = (dev->channel << 5);
+        resp_data[i+1] |= dev->lun;
+        i += 8;
+    }
+
+    vscsi_preprocess_desc(req);
+    rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
+    free(resp_data);
+    if (rc < 0) {
+        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
+        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
+    } else {
+        vscsi_send_rsp(s, req, 0, len - rc, 0);
+    }
+}
+
 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
 {
     union srp_iu *srp = &req->iu.srp;
     SCSIDevice *sdev;
     int n, lun;
 
+    if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN)      && srp->cmd.cdb[0] == REPORT_LUNS) {
+        vscsi_report_luns(s, req);
+        return 0;
+    }
+
     sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
     if (!sdev) {
         DPRINTF("VSCSI: Command for lun %08" PRIx64 " with no drive\n",
Signature · 1 line
1.8.4
#1 lore raw

Any news on this? FreeBSD is unbootable from CDROM devices in QEMU/pseries without this patch. -Nathan

Quoted 102 lines · Nathan Whitehorn

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

---
  hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +57 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
  #define SCSI_SENSE_BUF_SIZE     96
  #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
  typedef union vscsi_crq {
      struct viosrp_crq s;
      uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
      }
  }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;
+
+    resp_data = malloc(len);
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else
+            resp_data[i] = (2 << 6);
+        resp_data[i] |= dev->id;
+        resp_data[i+1] = (dev->channel << 5);
+        resp_data[i+1] |= dev->lun;
+        i += 8;
+    }
+
+    vscsi_preprocess_desc(req);
+    rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
+    free(resp_data);
+    if (rc < 0) {
+        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
+        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
+    } else {
+        vscsi_send_rsp(s, req, 0, len - rc, 0);
+    }
+}
+
  static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
  {
      union srp_iu *srp = &req->iu.srp;
      SCSIDevice *sdev;
      int n, lun;
 
+    if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN)      && srp->cmd.cdb[0] == REPORT_LUNS) {
+        vscsi_report_luns(s, req);
+        return 0;
+    }
+
      sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
      if (!sdev) {
          DPRINTF("VSCSI: Command for lun %08" PRIx64 " with no drive\n",
Acked-by
#2 lore raw

Il 02/12/2013 18:51, Nathan Whitehorn ha scritto:

Quoted 3 lines

Any news on this? FreeBSD is unbootable from CDROM devices in QEMU/pseries without this patch.
-Nathan

Alex, can you pick it up?

#3 lore raw
Quoted 7 lines · Paolo Bonzini

Il 02/12/2013 18:51, Nathan Whitehorn ha scritto:

Quoted 3 lines

Any news on this? FreeBSD is unbootable from CDROM devices in QEMU/pseries without this patch.
-Nathan

Alex, can you pick it up?

Any updates?
-Nathan

#4 lore raw
In reply to Nathan Whitehorn
Quoted 11 lines · Nathan Whitehorn

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Quoted 6 lines

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

This patch fails on checkpatch.pl. Please fix those warnings up :).

WARNING: braces {} are necessary for all arms of this statement #65: FILE: hw/scsi/spapr_vscsi.c:738:
+ if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
[...]

WARNING: braces {} are necessary for all arms of this statement #81: FILE: hw/scsi/spapr_vscsi.c:754:
+ if (dev->id == 0 && dev->channel == 0)
[...]
+ else
[...]

WARNING: line over 80 characters
#108: FILE: hw/scsi/spapr_vscsi.c:781:
+ if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN) && srp->cmd.cdb[0] == REPORT_LUNS) {

total: 0 errors, 3 warnings, 75 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Quoted 31 lines

---
hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +12 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
 #define SCSI_SENSE_BUF_SIZE     96
 #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
 typedef union vscsi_crq {
    struct viosrp_crq s;
    uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
    }
 }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 744

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 746

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+
+    resp_data = malloc(len);

g_malloc0

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 766

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+            resp_data[i] = (2 << 6);
+        resp_data[i] |= dev->id;
+        resp_data[i+1] = (dev->channel << 5);
+        resp_data[i+1] |= dev->lun;
+        i += 8;
+    }
+
+    vscsi_preprocess_desc(req);
+    rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
+    free(resp_data);

g_free

Alex

Quoted 26 lines
hw/scsi/spapr_vscsi.c +13 -0
+    if (rc < 0) {
+        vscsi_makeup_sense(s, req, HARDWARE_ERROR, 0, 0);
+        vscsi_send_rsp(s, req, CHECK_CONDITION, 0, 0);
+    } else {
+        vscsi_send_rsp(s, req, 0, len - rc, 0);
+    }
+}
+
 static int vscsi_queue_cmd(VSCSIState *s, vscsi_req *req)
 {
    union srp_iu *srp = &req->iu.srp;
    SCSIDevice *sdev;
    int n, lun;
 
+    if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN)      && srp->cmd.cdb[0] == REPORT_LUNS) {
+        vscsi_report_luns(s, req);
+        return 0;
+    }
+
    sdev = vscsi_device_find(&s->bus, be64_to_cpu(srp->cmd.lun), &lun);
    if (!sdev) {
        DPRINTF("VSCSI: Command for lun %08" PRIx64 " with no drive\n",
Signature · 1 line
1.8.4
#5 lore raw
Quoted 91 lines · Alexander Graf
Quoted 11 lines · Nathan Whitehorn

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Quoted 6 lines

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

This patch fails on checkpatch.pl. Please fix those warnings up :).

WARNING: braces {} are necessary for all arms of this statement #65: FILE: hw/scsi/spapr_vscsi.c:738:
+ if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
[...]

WARNING: braces {} are necessary for all arms of this statement #81: FILE: hw/scsi/spapr_vscsi.c:754:

hw/scsi/spapr_vscsi.c +2 -0
+        if (dev->id == 0 && dev->channel == 0)
[...]
+        else
[...]

WARNING: line over 80 characters
#108: FILE: hw/scsi/spapr_vscsi.c:781:
+ if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN) && srp->cmd.cdb[0] == REPORT_LUNS) {

total: 0 errors, 3 warnings, 75 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Quoted 31 lines

---
hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +12 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
 #define SCSI_SENSE_BUF_SIZE     96
 #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
 typedef union vscsi_crq {
   struct viosrp_crq s;
   uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
   }
 }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 744

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Quoted 19 lines

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 746

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+
+    resp_data = malloc(len);

g_malloc0

Quoted 11 lines
hw/scsi/spapr_vscsi.c +11 -0
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else
+            resp_data[i] = (2 << 6);

Ah, I almost forgot this one. Please convert that into something more verbose through a define. Whatever that bit means ... :).

Quoted 3 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 760

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+        resp_data[i] |= dev->id;
+        resp_data[i+1] = (dev->channel << 5);
+        resp_data[i+1] |= dev->lun;

What are the other 6 bytes reserved for?

Alex

#6 lore raw
Quoted 102 lines · Alexander Graf
Quoted 99 lines · Alexander Graf
Quoted 11 lines · Nathan Whitehorn

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Quoted 6 lines

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

This patch fails on checkpatch.pl. Please fix those warnings up :).

WARNING: braces {} are necessary for all arms of this statement #65: FILE: hw/scsi/spapr_vscsi.c:738:
+ if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
[...]

WARNING: braces {} are necessary for all arms of this statement #81: FILE: hw/scsi/spapr_vscsi.c:754:

hw/scsi/spapr_vscsi.c +2 -0
+        if (dev->id == 0 && dev->channel == 0)
[...]
+        else
[...]

WARNING: line over 80 characters
#108: FILE: hw/scsi/spapr_vscsi.c:781:
+ if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN) && srp->cmd.cdb[0] == REPORT_LUNS) {

total: 0 errors, 3 warnings, 75 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Quoted 31 lines

---
hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +12 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
 #define SCSI_SENSE_BUF_SIZE     96
 #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
 typedef union vscsi_crq {
   struct viosrp_crq s;
   uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
   }
 }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 744

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 746

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+
+    resp_data = malloc(len);

g_malloc0

Quoted 11 lines
Quoted 11 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 757

Quoted 11 lines
hw/scsi/spapr_vscsi.c +11 -0
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else
+            resp_data[i] = (2 << 6);

This looks odd.
Shouldn't this rather be
resp_data[i] = (1 << 6);
to set the LUN address method to 01b meaning Single Level LUN structure. (SAM5 4.7.3)

He is setting the address method to 10b but there is no such address method afaik.

Quoted 4 lines

Ah, I almost forgot this one. Please convert that into something more verbose through a define. Whatever that bit means ... :).

Quoted 1 line
Quoted 1 line
hw/scsi/spapr_vscsi.c +1 -0
+        resp_data[i] |= dev->id;

He should do something like :

    resp_data[i] |= dev->id & 0x3f;

here to avoid a dev->id > 63 from spilling into the address method field.

Or probably should have a check for
if dev->id > 3 then fail

Quoted 9 lines
Quoted 2 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 760

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+        resp_data[i+1] = (dev->channel << 5);
+        resp_data[i+1] |= dev->lun;

What are the other 6 bytes reserved for?

Alex

#7 lore raw
Quoted 109 lines · ronnie sahlberg
Quoted 97 lines · Alexander Graf
Quoted 95 lines · Alexander Graf
Quoted 11 lines · Nathan Whitehorn

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Quoted 6 lines

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

This patch fails on checkpatch.pl. Please fix those warnings up :).

WARNING: braces {} are necessary for all arms of this statement #65: FILE: hw/scsi/spapr_vscsi.c:738:
+ if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
[...]

WARNING: braces {} are necessary for all arms of this statement #81: FILE: hw/scsi/spapr_vscsi.c:754:

hw/scsi/spapr_vscsi.c +2 -0
+        if (dev->id == 0 && dev->channel == 0)
[...]
+        else
[...]

WARNING: line over 80 characters
#108: FILE: hw/scsi/spapr_vscsi.c:781:
+ if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN) && srp->cmd.cdb[0] == REPORT_LUNS) {

total: 0 errors, 3 warnings, 75 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Quoted 31 lines

---
hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +12 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
 #define SCSI_SENSE_BUF_SIZE     96
 #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
 typedef union vscsi_crq {
   struct viosrp_crq s;
   uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
   }
 }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 744

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 746

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+
+    resp_data = malloc(len);

g_malloc0

Quoted 11 lines
Quoted 11 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 757

Quoted 11 lines
hw/scsi/spapr_vscsi.c +11 -0
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else
+            resp_data[i] = (2 << 6);
Quoted 8 lines

This looks odd.
Shouldn't this rather be
resp_data[i] = (1 << 6);
to set the LUN address method to 01b meaning Single Level LUN structure. (SAM5 4.7.3)

He is setting the address method to 10b but there is no such address method afaik

There is. This uses the "Logical unit addressing" method of SAM5 section 4.7.7.4, following the rest of the code in this module.

Quoted 10 lines
Quoted 4 lines

Ah, I almost forgot this one. Please convert that into something more verbose through a define. Whatever that bit means ... :).

Quoted 1 line
Quoted 1 line
hw/scsi/spapr_vscsi.c +1 -0
+        resp_data[i] |= dev->id;

He should do something like :

    resp_data[i] |= dev->id & 0x3f;

here to avoid a dev->id > 63 from spilling into the address method field.

Or probably should have a check for
if dev->id > 3 then fail

OK.

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 760

Quoted 3 lines
hw/scsi/spapr_vscsi.c +2 -0
+        resp_data[i+1] = (dev->channel << 5);
+        resp_data[i+1] |= dev->lun;

What are the other 6 bytes reserved for?

Alex

SRP, and so VSCSI, uses hierarchical LUNs. QEMU doesn't actually implement that, however, so the hierarchy only has one level -- the remaining 6 bytes are therefore zero.
-Nathan

#8 lore raw

Il 02/01/2014 19:14, Nathan Whitehorn ha scritto:

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

He should do something like :

    resp_data[i] |= dev->id & 0x3f;

here to avoid a dev->id > 63 from spilling into the address method field.

Or probably should have a check for
if dev->id > 3 then fail

OK.

No need for that:

    .max_channel = 7, /* logical unit addressing format */
    .max_target = 63,
    .max_lun = 31,

dev->id is thus bounded to 0..63.

Paolo

#9 lore raw
In reply to Alexander Graf
Quoted 108 lines · Alexander Graf
Quoted 86 lines · Alexander Graf
Quoted 11 lines · Nathan Whitehorn

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Quoted 6 lines

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

This patch fails on checkpatch.pl. Please fix those warnings up :).

WARNING: braces {} are necessary for all arms of this statement #65: FILE: hw/scsi/spapr_vscsi.c:738:
+ if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
[...]

WARNING: braces {} are necessary for all arms of this statement #81: FILE: hw/scsi/spapr_vscsi.c:754:

hw/scsi/spapr_vscsi.c +2 -0
+        if (dev->id == 0 && dev->channel == 0)
[...]
+        else
[...]

WARNING: line over 80 characters
#108: FILE: hw/scsi/spapr_vscsi.c:781:
+ if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN) && srp->cmd.cdb[0] == REPORT_LUNS) {

total: 0 errors, 3 warnings, 75 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Quoted 31 lines

---
hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +12 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
 #define SCSI_SENSE_BUF_SIZE     96
 #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
 typedef union vscsi_crq {
   struct viosrp_crq s;
   uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
   }
 }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 744

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

Quoted 20 lines

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 746

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+
+    resp_data = malloc(len);

g_malloc0

Quoted 11 lines
hw/scsi/spapr_vscsi.c +11 -0
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else
+            resp_data[i] = (2 << 6);
Quoted 1 line

Ah, I almost forgot this one. Please convert that into something more verbose through a define. Whatever that bit means ... :).

I've tried to maintain the existing (questionable) style here. See vscsi_device_find() for instance. This seemed like a bad occasion for a global style cleanup.

Quoted 4 lines
Quoted 3 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 760

Quoted 3 lines
hw/scsi/spapr_vscsi.c +3 -0
+        resp_data[i] |= dev->id;
+        resp_data[i+1] = (dev->channel << 5);
+        resp_data[i+1] |= dev->lun;

What are the other 6 bytes reserved for?

It's the hierarchical LUN fields. vscsi_device_find() defines the LUN format used by this module if you are curious.
-Nathan

#10 lore raw

Il 02/01/2014 19:23, Nathan Whitehorn ha scritto:

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

This is simply because you should not report lun 0 twice; even if it is not defined, LUN 0 is there as a dummy device that only answers a handful of commands (including INQUIRY and REPORT LUNS). There are many ways to write it, but unless you use GArray or something like that, it will look very much like Nathan and hw/scsi/scsi-bus.c's code.

Paolo

#11 lore raw
Quoted 16 lines · Paolo Bonzini

Il 02/01/2014 19:23, Nathan Whitehorn ha scritto:

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

Quoted 7 lines

This is simply because you should not report lun 0 twice; even if it is not defined, LUN 0 is there as a dummy device that only answers a handful of commands (including INQUIRY and REPORT LUNS). There are many ways to write it, but unless you use GArray or something like that, it will look very much like Nathan and hw/scsi/scsi-bus.c's code.

Paolo

I've sent a new version of this patch reflecting the discussion here. Please let me know if I have missed anything.
-Nathan

#12 lore raw
In reply to Alexander Graf
Quoted 37 lines · Alexander Graf
Quoted 11 lines · Nathan Whitehorn

Intercept REPORT_LUNS commands addressed either to SRP LUN 0 or the well-known LUN for REPORT_LUNS commands. This is required to implement the SAM and SPC specifications.

Since SRP implements only a single SCSI target port per connection, the SRP target is required to report all available LUNs in response to a REPORT_LUNS command addressed either to LUN 0 or the well-known LUN. Instead, QEMU was forwarding such requests to the first QEMU SCSI target, with the result that initiators that relied on this feature would only see LUNs on the first QEMU SCSI target.

Quoted 6 lines

Behavior for REPORT_LUNS commands addressed to any other LUN is not specified by the standard and so is left unchanged. This preserves behavior under Linux and SLOF, which enumerate possible LUNs by hand and so address no commands either to LUN 0 or the well-known REPORT_LUNS LUN.

This patch fails on checkpatch.pl. Please fix those warnings up :).

WARNING: braces {} are necessary for all arms of this statement #65: FILE: hw/scsi/spapr_vscsi.c:738:
+ if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
[...]

WARNING: braces {} are necessary for all arms of this statement #81: FILE: hw/scsi/spapr_vscsi.c:754:

hw/scsi/spapr_vscsi.c +2 -0
+        if (dev->id == 0 && dev->channel == 0)
[...]
+        else
[...]

WARNING: line over 80 characters
#108: FILE: hw/scsi/spapr_vscsi.c:781:
+ if ((srp->cmd.lun == 0 || be64_to_cpu(srp->cmd.lun) == SRP_REPORT_LUNS_WLUN) && srp->cmd.cdb[0] == REPORT_LUNS) {

Quoted 5 lines

total: 0 errors, 3 warnings, 75 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Will do. I'm traveling at the moment, so it may take a little while. If you felt like fixing it yourself, I'd appreciate it.

Quoted 43 lines
Quoted 31 lines

---
hw/scsi/spapr_vscsi.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 57 insertions(+)
hw/scsi/spapr_vscsi.c +12 -0
index 2a26042..87e0fb3 100644
@@ -63,6 +63,8 @@
 #define SCSI_SENSE_BUF_SIZE     96
 #define SRP_RSP_SENSE_DATA_LEN  18
 
+#define SRP_REPORT_LUNS_WLUN    0xc10100000000000
+
 typedef union vscsi_crq {
    struct viosrp_crq s;
    uint8_t raw[16];
@@ -720,12 +722,67 @@ static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req)
    }
 }
 
+static void vscsi_report_luns(VSCSIState *s, vscsi_req *req)
+{
+    BusChild *kid;
+    int i, len, n, rc;
+    uint8_t *resp_data;
+    bool found_lun0;
+
+    n = 0;
+    found_lun0 = false;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 744

Quoted 10 lines
hw/scsi/spapr_vscsi.c +10 -0
+        SCSIDevice *dev = SCSI_DEVICE(kid->child);
+
+        n += 8;
+        if (dev->channel == 0 && dev->id == 0 && dev->lun == 0)
+            found_lun0 = true;
+    }
+    if (!found_lun0) {
+        n += 8;
+    }
+    len = n+8;

Let me try to grasp what you're doing here. You're trying to figure out how many devices there are attached to the bus. For every device you reserve a buffer block. Lun0 is mandatory, all others are optional.

Quoted 3 lines

First off, I think the code would be easier to grasp if you'd count "number of entries" rather than "number of bytes". That way we don't have to mentally deal with the 8 byte block granularity.

Then IIUC you're jumping through a lot of hoops to count lun0 if it's there, but keep it reserved when it's not there. Why don't you just always reserve entry 0 for lun0? In the loop where you're actually filling in data you just skip lun0. Or is lun0 a terminator and always has to come last?

This is a duplicate of the logic (and code) in scsi_target_emulate_report_luns() in scsi-bus.c. I tried to keep them as similar as possible for maintenance reasons. LUN 0 doesn't actually even need to exist to follow SAM-5, but QEMU seems to add it (and doing so is non-harmful), so I've kept that. The whole code should probably be refactored anyway to support things like hierarchical LUNs in the SCSI core, which this SRP driver is trying to emulate for one level by wrapping the SCSI core, but that's a larger project.

Quoted 17 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 746

Quoted 2 lines
hw/scsi/spapr_vscsi.c +2 -0
+
+    resp_data = malloc(len);

g_malloc0

Quoted 13 lines
hw/scsi/spapr_vscsi.c +13 -0
+    memset(resp_data, 0, len);
+    stl_be_p(resp_data, n);
+    i = found_lun0 ? 8 : 16;
+    QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
+        DeviceState *qdev = kid->child;
+        SCSIDevice *dev = SCSI_DEVICE(qdev);
+
+        if (dev->id == 0 && dev->channel == 0)
+            resp_data[i] = 0;
+        else
+            resp_data[i] = (2 << 6);
+        resp_data[i] |= dev->id;
+        resp_data[i+1] = (dev->channel << 5);
Quoted 10 lines

hw/scsi/spapr_vscsi.c static void vscsi_inquiry_no_target(VSCSIState *s, vscsi_req *req) · line 766

Quoted 7 lines
hw/scsi/spapr_vscsi.c +7 -0
+        resp_data[i+1] |= dev->lun;
+        i += 8;
+    }
+
+    vscsi_preprocess_desc(req);
+    rc = vscsi_srp_transfer_data(s, req, 0, resp_data, len);
+    free(resp_data);

g_free

Thanks for the pointers here. I'm not too familiar with QEMU internals. -Nathan