Skip to contentSkip to conversation

Enable virtio-scsi boot from /dev/sgX

RFC PATCH Superseded Eric Farman 6 participants 22 messages Apr 26, 2017 – May 8, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#4 lore raw

RFC PATCH 2/5 pc-bios/s390-ccw: Remove duplicate blk_factor adjustment

In reply to Eric Farman

When using virtio-scsi, we multiply the READ(10) data_size by a block factor twice when building the I/O. This is fine, since it's only 1 for SCSI disks, but let's clean it up.

---
 pc-bios/s390-ccw/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/virtio-scsi.c +1 -1
index d850a8d..69b7a93 100644
@@ -154,7 +154,7 @@ static bool scsi_read_10(VDev *vdev,
     VirtioCmd read_10[] = {
         { &req, sizeof(req), VRING_DESC_F_NEXT },
         { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
-        { data, data_size * f, VRING_DESC_F_WRITE },
+        { data, data_size, VRING_DESC_F_WRITE },
     };
 
     debug_print_int("read_10  sector", sector);
Signature · 1 line
2.10.2
#0 lore raw

cover Enable virtio-scsi boot from /dev/sgX

Today, trying to boot a guest from a SCSI LUN on s390x yields the following:

virtio-blk = OK
virtio-scsi and /dev/sdX = OK
virtio-scsi and /dev/sgX = FAIL

Example of the failing scenario:

/usr/bin/qemu-system-s390x ...

    -device virtio-scsi-ccw,id=scsi0,devno=fe.0.0001
    -drive file=/dev/sg2,if=none,id=drive0,format=raw
    -device scsi-generic,bus=scsi0.0,channel=0,scsi-id=0,drive=drive0,id=disk0

LOADPARM=[........]
Using virtio-scsi.
Using SCSI scheme.
..
! virtio-scsi:read_many: STATUS=02 RSPN=70 KEY=0b CODE=00 QLFR=06, sure !

Why do we care? Well, libvirt converts a virtio-scsi device from the host SCSI address (host:bus:target:lun) into the associated /dev/sgX device, which means we can't boot from virtio-scsi and have to rely on virtio-blk for this action.

The short version of what happens is the host device driver rejects our requests because the transfer lengths are too long for it to satisfy. A virtio-scsi disk connected via scsi-generic is fine as a non-boot device because the guest kernel is able to break up the requests for us. So we just need to handle this situation for the boot process.

Patches 2-N in this series do that, but rely on us to specify the max_sectors parameter for the virtio-scsi-ccw device:

/usr/bin/qemu-system-s390x ...

    -device virtio-scsi-ccw,id=scsi0,devno=fe.0.0001,max_sectors=2048

Without this, the default of xFFFF is used and we still end up exceeding the limit in the host drivers. Which is why we have Patch 1, and thus the "RFC" tag on this series. The backing block device has a maximum transfer value calculated with the help of the BLKSECTGET ioctl[1][2].

With patch 1 applied, we are able to go through the children devices and apply any known limit received from this IOCTL, and thus break up our requests before submitting them to the host. It is admittedly a little coarse, since the max_sectors value is associated with the virtio-scsi controller and multiple devices can be attached to it, not all of which are associated with our boot device. Meanwhile, each attached device can have its own maximum transfer from the backing device.

[1] http://git.qemu-project.org/?p=qemu.git;a=commitdiff;h=482652502e98b1d570de0585f01dd55e35fdebfb
[2] http://git.qemu-project.org/?p=qemu.git;a=commitdiff;h=c4c41a0a65d650b241dec9efedf4ee3f00a16a30

Eric Farman (5):
hw/scsi: Override the max_sectors value for virtio-scsi pc-bios/s390-ccw: Remove duplicate blk_factor adjustment pc-bios/s390-ccw: Move SCSI block factor to outer read pc-bios/s390-ccw: Break up virtio-scsi read into multiples pc-bios/s390-ccw.img: rebuild image

 hw/scsi/virtio-scsi.c          |  12 ++++++++++++
 pc-bios/s390-ccw.img           | Bin 26456 -> 26456 bytes
 pc-bios/s390-ccw/s390-ccw.h    |   4 ++++
 pc-bios/s390-ccw/virtio-scsi.c |  29 ++++++++++++++++++++---------
 4 files changed, 36 insertions(+), 9 deletions(-)
Signature · 1 line
2.10.2
#1 lore raw

RFC PATCH 1/5 hw/scsi: Override the max_sectors value for virtio-scsi

The virtio spec states that the max_sectors field is "a hint to the driver for the maximum transfer size" that would be used for a virtio-scsi request.

It's currently hardcoded to xFFFF unless one is established by the max_sectors parameter on the command line, but let's roll through the associated devices and set it to anything lower if one is set for the underlying block device and retrieved by the BLKSECTGET ioctl.

---
 hw/scsi/virtio-scsi.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/scsi/virtio-scsi.c +12 -0
index 46a3e3f..bca9461 100644
@@ -640,7 +640,11 @@ static void virtio_scsi_get_config(VirtIODevice *vdev,
                                    uint8_t *config)
 {
     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
+    VirtIOSCSI *vs = VIRTIO_SCSI(vdev);
     VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
+    SCSIDevice *d;
+    BusChild *kid;
+    unsigned int max_transfer;
 
     virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
     virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2);
@@ -652,6 +656,14 @@ static void virtio_scsi_get_config(VirtIODevice *vdev,
     virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
     virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
     virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
+
+    QTAILQ_FOREACH(kid, &vs->bus.qbus.children, sibling) {
+        d = SCSI_DEVICE(kid->child);
+        max_transfer = blk_get_max_transfer(d->conf.blk) / d->blocksize;
+        virtio_stl_p(vdev,
+                     &scsiconf->max_sectors,
+                     MIN_NON_ZERO(max_transfer, scsiconf->max_sectors));
+    }
 }
 
 static void virtio_scsi_set_config(VirtIODevice *vdev,
Signature · 1 line
2.10.2
#6 lore raw

RFC PATCH 3/5 pc-bios/s390-ccw: Move SCSI block factor to outer read

In reply to Eric Farman

Simple refactoring so that the blk_factor adjustment is moved into virtio_scsi_read_many routine, in preparation for another change.

---
 pc-bios/s390-ccw/virtio-scsi.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
pc-bios/s390-ccw/virtio-scsi.c +8 -6
index 69b7a93..6d070e2 100644
@@ -142,14 +142,13 @@ static bool scsi_report_luns(VDev *vdev, void *data, uint32_t data_size)
 }
 
 static bool scsi_read_10(VDev *vdev,
-                         ulong sector, int sectors, void *data)
+                         ulong sector, int sectors, void *data,
+                         unsigned int data_size)
 {
-    int f = vdev->blk_factor;
-    unsigned int data_size = sectors * virtio_get_block_size() * f;
     ScsiCdbRead10 cdb = {
         .command = 0x28,
-        .lba = sector * f,
-        .xfer_length = sectors * f,
+        .lba = sector,
+        .xfer_length = sectors,
     };
     VirtioCmd read_10[] = {
         { &req, sizeof(req), VRING_DESC_F_NEXT },
@@ -255,7 +254,10 @@ static void virtio_scsi_locate_device(VDev *vdev)
 int virtio_scsi_read_many(VDev *vdev,
                           ulong sector, void *load_addr, int sec_num)
 {
-    if (!scsi_read_10(vdev, sector, sec_num, load_addr)) {
+    int f = vdev->blk_factor;
+    unsigned int data_size = sec_num * virtio_get_block_size() * f;
+
+    if (!scsi_read_10(vdev, sector * f, sec_num * f, load_addr, data_size)) {
         virtio_scsi_verify_response(&resp, "virtio-scsi:read_many");
     }
 
Signature · 1 line
2.10.2
#7 lore raw

RFC PATCH 4/5 pc-bios/s390-ccw: Break up virtio-scsi read into multiples

In reply to Eric Farman

A virtio-scsi request that goes through the host sd driver and exceeds the maximum transfer size is automatically broken up for us. But the equivalent request going to the sg driver presumes that any length requirements have already been honored. Let's use the max_sectors field from the device and break up all virtio-scsi requests (both sd and sg) to avoid problem from the host drivers.

---
 pc-bios/s390-ccw/s390-ccw.h    |  4 ++++
 pc-bios/s390-ccw/virtio-scsi.c | 19 ++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +4 -0
index ded67bc..e1f3751 100644
@@ -42,6 +42,10 @@ typedef unsigned long long __u64;
 #ifndef NULL
 #define NULL    0
 #endif
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b));
+#endif
+
 
 #include "cio.h"
 #include "iplb.h"
pc-bios/s390-ccw/virtio-scsi.c +14 -5
index 6d070e2..90a8cc8 100644
@@ -254,12 +254,21 @@ static void virtio_scsi_locate_device(VDev *vdev)
 int virtio_scsi_read_many(VDev *vdev,
                           ulong sector, void *load_addr, int sec_num)
 {
+    int sector_count;
     int f = vdev->blk_factor;
-    unsigned int data_size = sec_num * virtio_get_block_size() * f;
-
-    if (!scsi_read_10(vdev, sector * f, sec_num * f, load_addr, data_size)) {
-        virtio_scsi_verify_response(&resp, "virtio-scsi:read_many");
-    }
+    unsigned int data_size;
+
+    do {
+        sector_count = MIN(sec_num, vdev->config.scsi.max_sectors);
+        data_size = sector_count * virtio_get_block_size() * f;
+        if (!scsi_read_10(vdev, sector * f, sector_count * f, load_addr,
+                          data_size)) {
+            virtio_scsi_verify_response(&resp, "virtio-scsi:read_many");
+        }
+        load_addr += data_size;
+        sector += sector_count;
+        sec_num -= sector_count;
+    } while (sec_num > 0);
 
     return 0;
 }
Signature · 1 line
2.10.2
#12 lore raw

RFC PATCH 5/5 pc-bios/s390-ccw.img: rebuild image

In reply to Eric Farman

Contains the following commits:
- pc-bios/s390-ccw: Remove duplicate blk_factor adjustment
- pc-bios/s390-ccw: Move SCSI block factor to outer read
- pc-bios/s390-ccw: Break up virtio-scsi read into multiples

---
 pc-bios/s390-ccw.img | Bin 26456 -> 26456 bytes
 1 file changed, 0 insertions(+), 0 deletions(-)
pc-bios/s390-ccw.img binary +0 -0
index 2a4adfa654844040dbc84e90a9c2e0af56fdb9fa..0451d319e691544ded585846cfcb8f1799b3fbf5 100644
GIT binary patch
Signature · 1 line
2.10.2
#8 lore raw

Re: [Qemu-devel] [RFC PATCH v1 4/5] pc-bios/s390-ccw: Break up virtio-scsi read into multiples

In reply to Eric Farman
Quoted 19 lines · Eric Farman

A virtio-scsi request that goes through the host sd driver and exceeds the maximum transfer size is automatically broken up for us. But the equivalent request going to the sg driver presumes that any length requirements have already been honored. Let's use the max_sectors field from the device and break up all virtio-scsi requests (both sd and sg) to avoid problem from the host drivers.

---
 pc-bios/s390-ccw/s390-ccw.h    |  4 ++++
 pc-bios/s390-ccw/virtio-scsi.c | 19 ++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +0 -0
index ded67bc..e1f3751 100644

pc-bios/s390-ccw/s390-ccw.h typedef unsigned long long __u64; · line 47

Quoted 6 lines
pc-bios/s390-ccw/s390-ccw.h +3 -0
@@ -42,6 +42,10 @@ typedef unsigned long long __u64;
 #ifndef NULL
 #define NULL    0
 #endif
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b));
+#endif

osdep.h defines MIN() for ALL files, so this hunk is spurious.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#9 lore raw

Re: [Qemu-devel] [RFC PATCH v1 4/5] pc-bios/s390-ccw: Break up virtio-scsi read into multiples

Quoted 15 lines · Eric Blake
Quoted 14 lines · Eric Farman

A virtio-scsi request that goes through the host sd driver and exceeds the maximum transfer size is automatically broken up for us. But the equivalent request going to the sg driver presumes that any length requirements have already been honored. Let's use the max_sectors field from the device and break up all virtio-scsi requests (both sd and sg) to avoid problem from the host drivers.

---
 pc-bios/s390-ccw/s390-ccw.h    |  4 ++++
 pc-bios/s390-ccw/virtio-scsi.c | 19 ++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)
Quoted 13 lines

pc-bios/s390-ccw/s390-ccw.h typedef unsigned long long __u64; · line 47

Quoted 11 lines
pc-bios/s390-ccw/s390-ccw.h +3 -0
index ded67bc..e1f3751 100644
@@ -42,6 +42,10 @@ typedef unsigned long long __u64;
 #ifndef NULL
 #define NULL    0
 #endif
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b));
+#endif

osdep.h defines MIN() for ALL files, so this hunk is spurious.

Ah, so it does. Okay, removed. Thanks!

I guess it would make sense for the hunk that uses it to use MIN_NON_ZERO instead, which is also defined there.

Quoted 1 line
#11 lore raw

Re: [Qemu-devel] [RFC PATCH v1 4/5] pc-bios/s390-ccw: Break up virtio-scsi read into multiples

In reply to Eric Blake
Quoted 15 lines · On Wed, 26 Apr 2017 10:17:36 -0500 Eric Blake <[email protected]> wrote:
Quoted 14 lines · Eric Farman

A virtio-scsi request that goes through the host sd driver and exceeds the maximum transfer size is automatically broken up for us. But the equivalent request going to the sg driver presumes that any length requirements have already been honored. Let's use the max_sectors field from the device and break up all virtio-scsi requests (both sd and sg) to avoid problem from the host drivers.

---
 pc-bios/s390-ccw/s390-ccw.h    |  4 ++++
 pc-bios/s390-ccw/virtio-scsi.c | 19 ++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)
Quoted 14 lines

pc-bios/s390-ccw/s390-ccw.h typedef unsigned long long __u64; · line 47

Quoted 11 lines
pc-bios/s390-ccw/s390-ccw.h +3 -0
index ded67bc..e1f3751 100644
@@ -42,6 +42,10 @@ typedef unsigned long long __u64;
 #ifndef NULL
 #define NULL    0
 #endif
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b));
+#endif

osdep.h defines MIN() for ALL files, so this hunk is spurious.

This is a separate binary, which does not include the normal qemu headers, though.

#13 lore raw
In reply to Eric Farman
Quoted 23 lines · Eric Farman

Today, trying to boot a guest from a SCSI LUN on s390x yields the following:

virtio-blk = OK
virtio-scsi and /dev/sdX = OK
virtio-scsi and /dev/sgX = FAIL

Example of the failing scenario:

/usr/bin/qemu-system-s390x ...

    -device virtio-scsi-ccw,id=scsi0,devno=fe.0.0001
    -drive file=/dev/sg2,if=none,id=drive0,format=raw
    -device scsi-generic,bus=scsi0.0,channel=0,scsi-id=0,drive=drive0,id=disk0

LOADPARM=[........]
Using virtio-scsi.
Using SCSI scheme.
..
! virtio-scsi:read_many: STATUS=02 RSPN=70 KEY=0b CODE=00 QLFR=06, sure !

Why do we care? Well, libvirt converts a virtio-scsi device from the host SCSI address (host:bus:target:lun) into the associated /dev/sgX device, which means we can't boot from virtio-scsi and have to rely on virtio-blk for this action.

Quoted 5 lines

The short version of what happens is the host device driver rejects our requests because the transfer lengths are too long for it to satisfy. A virtio-scsi disk connected via scsi-generic is fine as a non-boot device because the guest kernel is able to break up the requests for us. So we just need to handle this situation for the boot process.

Out of curiosity. Why is the guest kernel submitting "short enough" requests? If it querying the device itself via scsi commands before issuing commands?

#10 lore raw

Re: [Qemu-devel] [RFC PATCH v1 4/5] pc-bios/s390-ccw: Break up virtio-scsi read into multiples

In reply to Eric Farman
Quoted 32 lines · Eric Farman
Quoted 15 lines · Eric Blake
Quoted 14 lines · Eric Farman

A virtio-scsi request that goes through the host sd driver and exceeds the maximum transfer size is automatically broken up for us. But the equivalent request going to the sg driver presumes that any length requirements have already been honored. Let's use the max_sectors field from the device and break up all virtio-scsi requests (both sd and sg) to avoid problem from the host drivers.

---
 pc-bios/s390-ccw/s390-ccw.h    |  4 ++++
 pc-bios/s390-ccw/virtio-scsi.c | 19 ++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)
Quoted 13 lines

pc-bios/s390-ccw/s390-ccw.h typedef unsigned long long __u64; · line 47

Quoted 11 lines
pc-bios/s390-ccw/s390-ccw.h +3 -0
index ded67bc..e1f3751 100644
@@ -42,6 +42,10 @@ typedef unsigned long long __u64;
 #ifndef NULL
 #define NULL    0
 #endif
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b));
+#endif

osdep.h defines MIN() for ALL files, so this hunk is spurious.

Quoted 1 line

Ah, so it does. Okay, removed. Thanks!

Er, not. Changing this header file didn't force virtio-scsi.c to recompile, so it bombs out as Cornelia points out.

Quoted 7 lines

I guess it would make sense for the hunk that uses it to use MIN_NON_ZERO instead, which is also defined there.

Quoted 1 line
#14 lore raw
In reply to Christian Borntraeger
Quoted 24 lines · Christian Borntraeger
Quoted 23 lines · Eric Farman

Today, trying to boot a guest from a SCSI LUN on s390x yields the following:

virtio-blk = OK
virtio-scsi and /dev/sdX = OK
virtio-scsi and /dev/sgX = FAIL

Example of the failing scenario:

/usr/bin/qemu-system-s390x ...

    -device virtio-scsi-ccw,id=scsi0,devno=fe.0.0001
    -drive file=/dev/sg2,if=none,id=drive0,format=raw
    -device scsi-generic,bus=scsi0.0,channel=0,scsi-id=0,drive=drive0,id=disk0

LOADPARM=[........]
Using virtio-scsi.
Using SCSI scheme.
..
! virtio-scsi:read_many: STATUS=02 RSPN=70 KEY=0b CODE=00 QLFR=06, sure !

Why do we care? Well, libvirt converts a virtio-scsi device from the host SCSI address (host:bus:target:lun) into the associated /dev/sgX device, which means we can't boot from virtio-scsi and have to rely on virtio-blk for this action.

Quoted 9 lines
Quoted 5 lines

The short version of what happens is the host device driver rejects our requests because the transfer lengths are too long for it to satisfy. A virtio-scsi disk connected via scsi-generic is fine as a non-boot device because the guest kernel is able to break up the requests for us. So we just need to handle this situation for the boot process.

Out of curiosity. Why is the guest kernel submitting "short enough" requests? If it querying the device itself via scsi commands before issuing commands?

Looking at some old traces I have, I think I misspoke that it's the guest kernel. In the failing case, the host kernel is processing an ioctl system call via sg_ioctl, and just passing our I/O transaction to the block driver. It blows up because the number of iovecs needed for the transaction exceeds UIO_MAXIOV. In the working case (boot from virtio-scsi as /dev/sdX or virtio-blk), the host kernel goes through read system call, and the I/O is broken up by do_generic_file_read.

Reviewed-by on v1 2/5
#5 lore raw

Re: [Qemu-devel] [RFC PATCH v1 2/5] pc-bios/s390-ccw: Remove duplicate blk_factor adjustment

In reply to Eric Farman
Quoted 5 lines · Eric Farman

When using virtio-scsi, we multiply the READ(10) data_size by a block factor twice when building the I/O. This is fine, since it's only 1 for SCSI disks, but let's clean it up.

Quoted 18 lines
---
 pc-bios/s390-ccw/virtio-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/virtio-scsi.c +1 -1
index d850a8d..69b7a93 100644
@@ -154,7 +154,7 @@ static bool scsi_read_10(VDev *vdev,
     VirtioCmd read_10[] = {
         { &req, sizeof(req), VRING_DESC_F_NEXT },
         { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
-        { data, data_size * f, VRING_DESC_F_WRITE },
+        { data, data_size, VRING_DESC_F_WRITE },
     };
 
     debug_print_int("read_10  sector", sector);
#2 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/5] hw/scsi: Override the max_sectors value for virtio-scsi

In reply to Eric Farman
Quoted 9 lines · Eric Farman

The virtio spec states that the max_sectors field is "a hint to the driver for the maximum transfer size" that would be used for a virtio-scsi request.

It's currently hardcoded to xFFFF unless one is established by the max_sectors parameter on the command line, but let's roll through the associated devices and set it to anything lower if one is set for the underlying block device and retrieved by the BLKSECTGET ioctl.

Have you tried looking at the block limits VPD page in the firmware? QEMU is passing BLKSECTGET down by patching that VPD page. Your solution would not work with hotplug.

Thanks,

Paolo

Quoted 37 lines
---
 hw/scsi/virtio-scsi.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/scsi/virtio-scsi.c +12 -0
index 46a3e3f..bca9461 100644
@@ -640,7 +640,11 @@ static void virtio_scsi_get_config(VirtIODevice *vdev,
                                    uint8_t *config)
 {
     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
+    VirtIOSCSI *vs = VIRTIO_SCSI(vdev);
     VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
+    SCSIDevice *d;
+    BusChild *kid;
+    unsigned int max_transfer;
 
     virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
     virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2);
@@ -652,6 +656,14 @@ static void virtio_scsi_get_config(VirtIODevice *vdev,
     virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
     virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
     virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
+
+    QTAILQ_FOREACH(kid, &vs->bus.qbus.children, sibling) {
+        d = SCSI_DEVICE(kid->child);
+        max_transfer = blk_get_max_transfer(d->conf.blk) / d->blocksize;
+        virtio_stl_p(vdev,
+                     &scsiconf->max_sectors,
+                     MIN_NON_ZERO(max_transfer, scsiconf->max_sectors));
+    }
 }
 
 static void virtio_scsi_set_config(VirtIODevice *vdev,
#15 lore raw
In reply to Eric Farman
Quoted 11 lines · Eric Farman

The short version of what happens is the host device driver rejects our requests because the transfer lengths are too long for it to satisfy. A virtio-scsi disk connected via scsi-generic is fine as a non-boot device because the guest kernel is able to break up the requests for us. So we just need to handle this situation for the boot process.

Patches 2-N in this series do that, but rely on us to specify the max_sectors parameter for the virtio-scsi-ccw device:

/usr/bin/qemu-system-s390x ...

    -device virtio-scsi-ccw,id=scsi0,devno=fe.0.0001,max_sectors=2048

Can you instead do an INQUIRY from the bios code to check the Block Limits page? The response is intercepted by hw/scsi/scsi-generic.c to merge in the host LUN's limits. That's how Linux kernel finds the granularity for request splitting.

That way, patch 1 is not necessary too. I don't like it because it doesn't always work considering LUN hotplug.

Fam

#3 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/5] hw/scsi: Override the max_sectors value for virtio-scsi

In reply to Eric Farman
Quoted 11 lines · Eric Farman

The virtio spec states that the max_sectors field is "a hint to the driver for the maximum transfer size" that would be used for a virtio-scsi request.

It's currently hardcoded to xFFFF unless one is established by the max_sectors parameter on the command line, but let's roll through the associated devices and set it to anything lower if one is set for the underlying block device and retrieved by the BLKSECTGET ioctl.

So since nobody else commented, I looked into this (not knowing anything about scsi before)

Your patch basically limits the HBA limit to the lowest size of all child devices. This will certainly work, as guests will use the minimum of per-device size and hba size.
Now: I think this might have some impact on cases where a HBA will have different scsi disks or even non disk type scsi devices (what does blk_get_max_transfer return for a scsi cdrom or old scsi-1 devices?)

So in essence I think what could be a better solution is to enhance the ccw bios to actually read the per-device config in addition to the hba config.

Linux seems to use the Block Limits VPD any maybe the ccw bios should do the same. So basically keep patch 4 and add an additional call to read the block VPD and use the minimum of HBA and device value?

Quoted 36 lines
---
 hw/scsi/virtio-scsi.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/scsi/virtio-scsi.c +12 -0
index 46a3e3f..bca9461 100644
@@ -640,7 +640,11 @@ static void virtio_scsi_get_config(VirtIODevice *vdev,
                                    uint8_t *config)
 {
     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
+    VirtIOSCSI *vs = VIRTIO_SCSI(vdev);
     VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
+    SCSIDevice *d;
+    BusChild *kid;
+    unsigned int max_transfer;
 
     virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
     virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2);
@@ -652,6 +656,14 @@ static void virtio_scsi_get_config(VirtIODevice *vdev,
     virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
     virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
     virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
+
+    QTAILQ_FOREACH(kid, &vs->bus.qbus.children, sibling) {
+        d = SCSI_DEVICE(kid->child);
+        max_transfer = blk_get_max_transfer(d->conf.blk) / d->blocksize;
+        virtio_stl_p(vdev,
+                     &scsiconf->max_sectors,
+                     MIN_NON_ZERO(max_transfer, scsiconf->max_sectors));
+    }
 }
 
 static void virtio_scsi_set_config(VirtIODevice *vdev,
#16 lore raw
In reply to Fam Zheng
Quoted 10 lines · Fam Zheng
Quoted 9 lines · Eric Farman

The short version of what happens is the host device driver rejects our requests because the transfer lengths are too long for it to satisfy. A virtio-scsi disk connected via scsi-generic is fine as a non-boot device because the guest kernel is able to break up the requests for us. So we just need to handle this situation for the boot process.

Patches 2-N in this series do that, but rely on us to specify the max_sectors parameter for the virtio-scsi-ccw device:

Quoted 6 lines
Quoted 2 lines

/usr/bin/qemu-system-s390x ...

    -device virtio-scsi-ccw,id=scsi0,devno=fe.0.0001,max_sectors=2048

Can you instead do an INQUIRY from the bios code to check the Block Limits page? The response is intercepted by hw/scsi/scsi-generic.c to merge in the host LUN's limits. That's how Linux kernel finds the granularity for request splitting.

It's a good idea (Thanks Christian, Paolo, and Fam :), but this leads to other difficulties.

We get a value of x3fffff when sending that to a scsi-disk from bios code. That's fully emulated though, in scsi_disk_emulate_inquiry. And that's the scenario that already works.

While there is indeed code in hw/scsi/scsi-generic.c to wire that in, that only happens after the I/O goes to the device itself. The Block Limits page isn't supported [1] and thus it gets rejected with "invalid field in cdb". We never get to that fixup code you reference, since the returned len is zero.

Should I be refactoring this code to always patch in that block limit regardless of a response from the host/device? (That is, when page xb0 isn't supported by the hw.)

- Eric

[1] If I issue an EVPD page x00 from the QEMU bios code, I only see pages xb1, xc0, and c1 are supported. If I look at the supported pages from the host, I see a few more but still not xb0:

$ sg_inq --page=0 /dev/sda
VPD INQUIRY: Supported VPD pages page
   Supported VPD pages:
     0x0	Supported VPD pages
     0x80	Unit serial number
     0x83	Device identification
     0x86	Extended INQUIRY data
     0xb1	Block device characteristics (sbc3)
     0xc0	vendor: Firmware numbers (seagate); Unit path report (EMC)
     0xc1	vendor: Date code (seagate)
$ sg_inq --page=0 /dev/sg0
VPD INQUIRY: Supported VPD pages page
   Supported VPD pages:
     0x0	Supported VPD pages
     0x80	Unit serial number
     0x83	Device identification
     0x86	Extended INQUIRY data
     0xb1	Block device characteristics (sbc3)
     0xc0	vendor: Firmware numbers (seagate); Unit path report (EMC)
     0xc1	vendor: Date code (seagate)
Quoted 6 lines

That way, patch 1 is not necessary too. I don't like it because it doesn't always work considering LUN hotplug.

Fam

#17 lore raw
Quoted 10 lines · Eric Farman

We get a value of x3fffff when sending that to a scsi-disk from bios code. That's fully emulated though, in scsi_disk_emulate_inquiry. And that's the scenario that already works.

While there is indeed code in hw/scsi/scsi-generic.c to wire that in, that only happens after the I/O goes to the device itself. The Block Limits page isn't supported [1] and thus it gets rejected with "invalid field in cdb". We never get to that fixup code you reference, since the returned len is zero.

Quoted 3 lines

Should I be refactoring this code to always patch in that block limit regardless of a response from the host/device? (That is, when page xb0 isn't supported by the hw.)

What is the BLKSECTGET value you get? Is there a sensible default value that you can use when page 0xb0 isn't supported by the hardware?

Thanks,

Paolo

#18 lore raw
Quoted 13 lines · Paolo Bonzini
Quoted 10 lines · Eric Farman

We get a value of x3fffff when sending that to a scsi-disk from bios code. That's fully emulated though, in scsi_disk_emulate_inquiry. And that's the scenario that already works.

While there is indeed code in hw/scsi/scsi-generic.c to wire that in, that only happens after the I/O goes to the device itself. The Block Limits page isn't supported [1] and thus it gets rejected with "invalid field in cdb". We never get to that fixup code you reference, since the returned len is zero.

Quoted 5 lines
Quoted 3 lines

Should I be refactoring this code to always patch in that block limit regardless of a response from the host/device? (That is, when page xb0 isn't supported by the hw.)

What is the BLKSECTGET value you get?

x140000 bytes when using /dev/sg0 (xa00 sectors when using /dev/sda).

Quoted 2 lines

Is there a sensible default value
that you can use when page 0xb0 isn't supported by the hardware?

I was setting max_sectors to x800 with good success, which was the power-of-2 floor that BLKSECTGET gave us. That kept us within the limits of the host biovec code. But it's a long way from the virtio-scsi value of xFFFF when max_sectors isn't specified, so don't know what side effects that may cause.

Quoted 5 lines

Thanks,

Paolo

#19 lore raw
Quoted 24 lines · Eric Farman
Quoted 13 lines · Paolo Bonzini
Quoted 10 lines · Eric Farman

We get a value of x3fffff when sending that to a scsi-disk from bios code. That's fully emulated though, in scsi_disk_emulate_inquiry. And that's the scenario that already works.

While there is indeed code in hw/scsi/scsi-generic.c to wire that in, that only happens after the I/O goes to the device itself. The Block Limits page isn't supported [1] and thus it gets rejected with "invalid field in cdb". We never get to that fixup code you reference, since the returned len is zero.

Quoted 5 lines
Quoted 3 lines

Should I be refactoring this code to always patch in that block limit regardless of a response from the host/device? (That is, when page xb0 isn't supported by the hw.)

What is the BLKSECTGET value you get?

x140000 bytes when using /dev/sg0 (xa00 sectors when using /dev/sda).

Quoted 8 lines
Quoted 2 lines

Is there a sensible default value
that you can use when page 0xb0 isn't supported by the hardware?

I was setting max_sectors to x800 with good success, which was the power-of-2 floor that BLKSECTGET gave us. That kept us within the limits of the host biovec code. But it's a long way from the virtio-scsi value of xFFFF when max_sectors isn't specified, so don't know what side effects that may cause.

It's just slower, but 0x800 is already a megabyte worth of data so it's not going to be that much slower.

Paolo

#20 lore raw
Quoted 36 lines · Paolo Bonzini
Quoted 24 lines · Eric Farman
Quoted 13 lines · Paolo Bonzini
Quoted 10 lines · Eric Farman

We get a value of x3fffff when sending that to a scsi-disk from bios code. That's fully emulated though, in scsi_disk_emulate_inquiry. And that's the scenario that already works.

While there is indeed code in hw/scsi/scsi-generic.c to wire that in, that only happens after the I/O goes to the device itself. The Block Limits page isn't supported [1] and thus it gets rejected with "invalid field in cdb". We never get to that fixup code you reference, since the returned len is zero.

Quoted 5 lines
Quoted 3 lines

Should I be refactoring this code to always patch in that block limit regardless of a response from the host/device? (That is, when page xb0 isn't supported by the hw.)

What is thec value you get?

x140000 bytes when using /dev/sg0 (xa00 sectors when using /dev/sda).

Quoted 8 lines
Quoted 2 lines

Is there a sensible default value
that you can use when page 0xb0 isn't supported by the hardware?

I was setting max_sectors to x800 with good success, which was the power-of-2 floor that BLKSECTGET gave us. That kept us within the limits of the host biovec code. But it's a long way from the virtio-scsi value of xFFFF when max_sectors isn't specified, so don't know what side effects that may cause.

Quoted 5 lines

It's just slower, but 0x800 is already a megabyte worth of data so it's not going to be that much slower.

Paolo

Eric, maybe that would be a safe variant. The bios boot code is not performance optimized anyway. Lets just always use a maximum size that will always work.

#21 lore raw
Quoted 40 lines · Christian Borntraeger
Quoted 39 lines · Paolo Bonzini
Quoted 24 lines · Eric Farman
Quoted 13 lines · Paolo Bonzini
Quoted 10 lines · Eric Farman

We get a value of x3fffff when sending that to a scsi-disk from bios code. That's fully emulated though, in scsi_disk_emulate_inquiry. And that's the scenario that already works.

While there is indeed code in hw/scsi/scsi-generic.c to wire that in, that only happens after the I/O goes to the device itself. The Block Limits page isn't supported [1] and thus it gets rejected with "invalid field in cdb". We never get to that fixup code you reference, since the returned len is zero.

Quoted 5 lines
Quoted 3 lines

Should I be refactoring this code to always patch in that block limit regardless of a response from the host/device? (That is, when page xb0 isn't supported by the hw.)

What is thec value you get?

x140000 bytes when using /dev/sg0 (xa00 sectors when using /dev/sda).

Quoted 8 lines
Quoted 2 lines

Is there a sensible default value
that you can use when page 0xb0 isn't supported by the hardware?

I was setting max_sectors to x800 with good success, which was the power-of-2 floor that BLKSECTGET gave us. That kept us within the limits of the host biovec code. But it's a long way from the virtio-scsi value of xFFFF when max_sectors isn't specified, so don't know what side effects that may cause.

It's just slower, but 0x800 is already a megabyte worth of data so it's not going to be that much slower.

Quoted 7 lines
Quoted 2 lines

Paolo

Eric, maybe that would be a safe variant. The bios boot code is not performance optimized anyway. Lets just always use a maximum size that will always work.

Sounds good to me. Let me work on getting my very ugly code to be slightly less ugly, and I'll send a v2 here. Thanks, everyone.

- Eric