scsi: Change device init to realize
cover scsi: Change device init to realize
DeviceClass->init is the old interface, let's convert scsi devices to the new ->realize API.
A user visible change is the error message shown in qemu-iotests reference output, but I don't know what's the best way to retain it. So please review if and how this could be improved.
Fam
Fam Zheng (2):
block: Pass errp in blkconf_geometry
scsi-bus: Convert DeviceClass init to realize
hw/block/block.c | 18 +++++----- hw/block/virtio-blk.c | 7 ++-- hw/ide/qdev.c | 11 ++++-- hw/scsi/lsi53c895a.c | 2 ++ hw/scsi/scsi-bus.c | 64 +++++++++++++++++------------------ hw/scsi/scsi-disk.c | 83 +++++++++++++++++++++++++--------------------- hw/scsi/scsi-generic.c | 37 ++++++++++----------- include/hw/block/block.h | 6 ++-- include/hw/scsi/scsi.h | 7 ++-- tests/qemu-iotests/051.out | 4 +-- 10 files changed, 125 insertions(+), 114 deletions(-)
Signature · 1 line
2.0.3
PATCH 1/2 block: Pass errp in blkconf_geometry
This allows us to pass error information to caller.
--- hw/block/block.c | 18 +++++++++--------- hw/block/virtio-blk.c | 7 +++---- hw/ide/qdev.c | 11 ++++++++--- hw/scsi/scsi-disk.c | 11 ++++++++--- include/hw/block/block.h | 6 ++++-- 5 files changed, 32 insertions(+), 21 deletions(-)
hw/block/block.c +9 -9
@@ -22,8 +22,9 @@ void blkconf_serial(BlockConf *conf, char **serial) } } -int blkconf_geometry (BlockConf *conf,int *ptrans, -unsigned cyls_max,unsigned heads_max,unsigned secs_max) +void blkconf_geometry (BlockConf *conf,int *ptrans, +unsigned cyls_max,unsigned heads_max,unsigned secs_max, +Error **errp) {DriveInfo *dinfo;
@@ -46,17 +47,16 @@ int blkconf_geometry(BlockConf *conf, int *ptrans, }if (conf->cyls || conf->heads || conf->secs) {if (conf->cyls <1 || conf->cyls > cyls_max) { -error_report ("cyls must be between 1 and %u" , cyls_max); -return -1 ; +error_setg (errp,"cyls must be between 1 and %u" , cyls_max); +return ; }if (conf->heads <1 || conf->heads > heads_max) { -error_report ("heads must be between 1 and %u" , heads_max); -return -1 ; +error_setg (errp,"heads must be between 1 and %u" , heads_max); +return ; }if (conf->secs <1 || conf->secs > secs_max) { -error_report ("secs must be between 1 and %u" , secs_max); -return -1 ; +error_setg (errp,"secs must be between 1 and %u" , secs_max); +return ; } } -return 0 ; }
hw/block/virtio-blk.c +3 -4
@@ -728,9 +728,7 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)VirtIODevice *vdev =VIRTIO_DEVICE (dev);VirtIOBlock *s =VIRTIO_BLK (dev);VirtIOBlkConf *blk = &(s->blk); -#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE Error *err =NULL ; -#endif static int virtio_blk_id;if (!blk->conf.bs) {
@@ -744,8 +742,9 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)blkconf_serial (&blk->conf, &blk->serial); s->original_wce =bdrv_enable_write_cache (blk->conf.bs); -if (blkconf_geometry (&blk->conf,NULL ,65535 ,255 ,255 ) <0 ) { -error_setg (errp,"Error setting geometry" ); +blkconf_geometry (&blk->conf,NULL ,65535 ,255 ,255 , &err); +if (err) { +error_propagate (errp, err);return ; }
hw/ide/qdev.c +8 -3
@@ -151,6 +151,7 @@ static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind) {IDEBus *bus =DO_UPCAST (IDEBus, qbus, dev->qdev.parent_bus);IDEState *s = bus->ifs + dev->unit; +Error *err =NULL ;if (dev->conf.discard_granularity ==-1 ) { dev->conf.discard_granularity =512 ;
@@ -161,9 +162,13 @@ static int ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind) }blkconf_serial (&dev->conf, &dev->serial); -if (kind !=IDE_CD - &&blkconf_geometry (&dev->conf, &dev->chs_trans,65536 ,16 ,255 ) <0 ) { -return -1 ; +if (kind !=IDE_CD ) { +blkconf_geometry (&dev->conf, &dev->chs_trans,65536 ,16 ,255 , &err); +if (err) { +error_report ("%s" ,error_get_pretty (err)); +error_free (err); +return -1 ; + } } if (ide_init_drive(s, dev->conf.bs, kind,
hw/scsi/scsi-disk.c +8 -3
@@ -2237,6 +2237,7 @@ static void scsi_disk_unit_attention_reported(SCSIDevice *dev)static int scsi_initfn (SCSIDevice *dev) {SCSIDiskState *s =DO_UPCAST (SCSIDiskState, qdev, dev); +Error *err =NULL ;if (!s->qdev.conf.bs) {error_report ("drive property not set" );
@@ -2250,9 +2251,13 @@ static int scsi_initfn(SCSIDevice *dev) }blkconf_serial (&s->qdev.conf, &s->serial); -if (dev->type ==TYPE_DISK - &&blkconf_geometry (&dev->conf,NULL ,65535 ,255 ,255 ) <0 ) { -return -1 ; +if (dev->type ==TYPE_DISK ) { +blkconf_geometry (&dev->conf,NULL ,65535 ,255 ,255 , &err); +if (err) { +error_report ("%s" ,error_get_pretty (err)); +error_free (err); +return -1 ; + } }if (s->qdev.conf.discard_granularity ==-1 ) {
include/hw/block/block.h +4 -2
@@ -12,6 +12,7 @@#define HW_BLOCK_COMMON_H #include "qemu-common.h" +#include "qapi/error.h" /* Configuration */
@@ -60,8 +61,9 @@ static inline unsigned int get_physical_block_exp(BlockConf *conf)/* Configuration helpers */ void blkconf_serial (BlockConf *conf,char **serial); -int blkconf_geometry (BlockConf *conf,int *trans, -unsigned cyls_max,unsigned heads_max,unsigned secs_max); +void blkconf_geometry (BlockConf *conf,int *trans, +unsigned cyls_max,unsigned heads_max,unsigned secs_max, +Error **errp);/* Hard disk geometry */
Signature · 1 line
2.0.3
PATCH 2/2 scsi-bus: Convert DeviceClass init to realize
In reply to Fam ZhengReplace "init/destroy" with "realize/unrealize" in SCSIDeviceClass, which has errp as a parameter. So all the implementations now uses error_setg instead of error_report for reporting error.
Also in lsi53c895a, report the error when initializing the if=scsi devices, before dropping it, because the callee's error_report is changed to error_segs.
--- hw/scsi/lsi53c895a.c | 2 ++ hw/scsi/scsi-bus.c | 64 ++++++++++++++++++------------------- hw/scsi/scsi-disk.c | 78 ++++++++++++++++++++++++---------------------- hw/scsi/scsi-generic.c | 37 +++++++++++----------- include/hw/scsi/scsi.h | 7 +++-- tests/qemu-iotests/051.out | 4 +-- 6 files changed, 96 insertions(+), 96 deletions(-)
hw/scsi/lsi53c895a.c +2 -0
@@ -19,6 +19,7 @@#include "hw/pci/pci.h" #include "hw/scsi/scsi.h" #include "sysemu/dma.h" +#include "qemu/error-report.h" //#define DEBUG_LSI //#define DEBUG_LSI_REG
@@ -2121,6 +2122,7 @@ static int lsi_scsi_init(PCIDevice *dev)if (!d->hotplugged) {scsi_bus_legacy_handle_cmdline (&s->bus, &err);if (err !=NULL ) { +error_report ("%s" ,error_get_pretty (err));error_free (err);return -1 ; }
hw/scsi/scsi-bus.c +30 -34
136 lines not on this page · raw message
hw/scsi/scsi-disk.c +40 -38
200 lines not on this page · raw message
hw/scsi/scsi-generic.c +18 -19
@@ -386,49 +386,49 @@ static void scsi_generic_reset(DeviceState *dev)scsi_device_purge_requests (s,SENSE_CODE (RESET )); } -static void scsi_destroy (SCSIDevice *s) +static void scsi_unrealize (SCSIDevice *s,Error **errp) {scsi_device_purge_requests (s,SENSE_CODE (NO_SENSE ));blockdev_mark_auto_del (s->conf.bs); } -static int scsi_generic_initfn (SCSIDevice *s) +static void scsi_generic_realize (SCSIDevice *s,Error **errp) {int rc;int sg_version;struct sg_scsi_id scsiid;if (!s->conf.bs) { -error_report ("drive property not set" ); -return -1 ; +error_setg (errp,"drive property not set" ); +return ; }if (bdrv_get_on_error (s->conf.bs,0 ) !=BLOCKDEV_ON_ERROR_ENOSPC ) { -error_report ("Device doesn't support drive option werror" ); -return -1 ; +error_setg (errp,"Device doesn't support drive option werror" ); +return ; }if (bdrv_get_on_error (s->conf.bs,1 ) !=BLOCKDEV_ON_ERROR_REPORT ) { -error_report ("Device doesn't support drive option rerror" ); -return -1 ; +error_setg (errp,"Device doesn't support drive option rerror" ); +return ; }/* check we are using a driver managing SG_IO (version 3 and after */ rc =bdrv_ioctl (s->conf.bs,SG_GET_VERSION_NUM , &sg_version);if (rc <0 ) { -error_report ("cannot get SG_IO version number: %s. " -"Is this a SCSI device?" , -strerror (-rc)); -return -1 ; +error_setg (errp,"cannot get SG_IO version number: %s. " +"Is this a SCSI device?" , +strerror (-rc)); +return ; }if (sg_version <30000 ) { -error_report ("scsi generic interface too old" ); -return -1 ; +error_setg (errp,"scsi generic interface too old" ); +return ; }/* get LUN of the /dev/sg? */ if (bdrv_ioctl (s->conf.bs,SG_GET_SCSI_ID , &scsiid)) { -error_report ("SG_GET_SCSI_ID ioctl failed" ); -return -1 ; +error_setg (errp,"SG_GET_SCSI_ID ioctl failed" ); +return ; }/* define device state */
@@ -460,7 +460,6 @@ static int scsi_generic_initfn(SCSIDevice *s) }DPRINTF ("block size %d\n" , s->blocksize); -return 0 ; }const SCSIReqOps scsi_generic_req_ops = {
@@ -495,8 +494,8 @@ static void scsi_generic_class_initfn(ObjectClass *klass, void *data)DeviceClass *dc =DEVICE_CLASS (klass);SCSIDeviceClass *sc =SCSI_DEVICE_CLASS (klass); - sc->init = scsi_generic_initfn; - sc->destroy = scsi_destroy; + sc->realize = scsi_generic_realize; + sc->unrealize = scsi_unrealize; sc->alloc_req = scsi_new_request; dc->fw_name ="disk" ; dc->desc ="pass through generic scsi device (/dev/sg*)" ;
include/hw/scsi/scsi.h +5 -2
@@ -72,10 +72,13 @@ struct SCSIRequest {#define SCSI_DEVICE_GET_CLASS (obj) \ OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE) +typedef void (*SCSIDeviceRealize )(SCSIDevice *dev,Error **errp); +typedef void (*SCSIDeviceUnrealize )(SCSIDevice *dev,Error **errp); + typedef struct SCSIDeviceClass { DeviceClass parent_class; - int (*init)(SCSIDevice *dev); - void (*destroy)(SCSIDevice *s); + SCSIDeviceRealize realize; + SCSIDeviceUnrealize unrealize; SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun, uint8_t *buf, void *hba_private); void (*unit_attention_reported)(SCSIDevice *s);
tests/qemu-iotests/051.out +1 -3
@@ -122,7 +122,7 @@ QEMU_PROG: -drive if=virtio: Device 'virtio-blk-pci' could not be initialized Testing: -drive if=scsi QEMU X.Y.Z monitor - type 'help' for more information -(qemu) QEMU_PROG: -drive if=scsi: Device needs media, but drive is empty +(qemu) QEMU_PROG: Device needs media, but drive is empty QEMU_PROG: Device initialization failed. QEMU_PROG: Initialization of device lsi53c895a failed
@@ -149,13 +149,11 @@ QEMU_PROG: -device ide-hd,drive=disk: Device 'ide-hd' could not be initialized Testing: -drive if=none,id=disk -device lsi53c895a -device scsi-disk,drive=disk QEMU X.Y.Z monitor - type 'help' for more information (qemu) QEMU_PROG: -device scsi-disk,drive=disk: Device needs media, but drive is empty -QEMU_PROG: -device scsi-disk,drive=disk: Device initialization failed. QEMU_PROG: -device scsi-disk,drive=disk: Device 'scsi-disk' could not be initialized Testing: -drive if=none,id=disk -device lsi53c895a -device scsi-hd,drive=disk QEMU X.Y.Z monitor - type 'help' for more information (qemu) QEMU_PROG: -device scsi-hd,drive=disk: Device needs media, but drive is empty -QEMU_PROG: -device scsi-hd,drive=disk: Device initialization failed. QEMU_PROG: -device scsi-hd,drive=disk: Device 'scsi-hd' could not be initialized
Signature · 1 line
2.0.3
Re: [Qemu-devel] [PATCH 2/2] scsi-bus: Convert DeviceClass init to realize
Quoted 2 lines · Fam Zheng
Replace "init/destroy" with "realize/unrealize" in SCSIDeviceClass, which has errp as a parameter. So all the implementations now uses
s/uses/use/
Quoted 5 lines
error_setginstead oferror_reportfor reporting error.Also in lsi53c895a, report the error when initializing the if=scsi devices, before dropping it, because the callee's
error_reportis changed toerror_segs.
s/error_segs/error_setg/
Fam
Re: [Qemu-devel] [PATCH 2/2] scsi-bus: Convert DeviceClass init to realize
In reply to Fam ZhengAm 05.08.2014 um 11:11 hat Fam Zheng geschrieben:
Quoted 31 lines
Replace "init/destroy" with "realize/unrealize" in SCSIDeviceClass, which has errp as a parameter. So all the implementations now uses
error_setginstead oferror_reportfor reporting error.Also in lsi53c895a, report the error when initializing the if=scsi devices, before dropping it, because the callee's
error_reportis changed toerror_segs.Signed-off-by Fam Zheng <[email protected]>--- hw/scsi/lsi53c895a.c | 2 ++ hw/scsi/scsi-bus.c | 64 ++++++++++++++++++------------------- hw/scsi/scsi-disk.c | 78 ++++++++++++++++++++++++---------------------- hw/scsi/scsi-generic.c | 37 +++++++++++----------- include/hw/scsi/scsi.h | 7 +++-- tests/qemu-iotests/051.out | 4 +-- 6 files changed, 96 insertions(+), 96 deletions(-)hw/scsi/lsi53c895a.c +1
-0@@ -19,6 +19,7 @@#include "hw/pci/pci.h" #include "hw/scsi/scsi.h" #include "sysemu/dma.h" +#include "qemu/error-report.h" //#define DEBUG_LSI //#define DEBUG_LSI_REG
hw/scsi/lsi53c895a.c static int lsi_scsi_init(PCIDevice *dev) · line 2128
Quoted 7 lines
hw/scsi/lsi53c895a.c +1
-0@@ -2121,6 +2122,7 @@ static int lsi_scsi_init(PCIDevice *dev)if (!d->hotplugged) {scsi_bus_legacy_handle_cmdline (&s->bus, &err);if (err !=NULL ) { +error_report ("%s" ,error_get_pretty (err));error_free (err);return -1 ; }
Wouldn't qerror_report_err() be more useful? Or is already a QMP error emitted in a different place in the callchain?
The same question is true for the added error_report() calls in patch 1.
Quoted 20 lines
hw/scsi/lsi53c895a.c +4
-4@@ -169,43 +168,40 @@ static int scsi_qdev_init(DeviceState *qdev) d =scsi_device_find (bus, dev->channel, dev->id, ++lun); }while (d && d->lun == lun && lun < bus->info->max_lun);if (d && d->lun == lun) { -error_report ("no free lun" ); - goto err; +error_setg (errp,"no free lun" ); +return ; } dev->lun = lun; }else { d =scsi_device_find (bus, dev->channel, dev->id, dev->lun);assert (d);if (d->lun == dev->lun && dev != d) { -error_report ("lun already used by '%s'" , d->qdev.id); - goto err; +error_setg (errp,"lun already used by '%s'" , d->qdev.id); +return ; } }
hw/scsi/lsi53c895a.c static int scsi_qdev_init(DeviceState *qdev) · line 190
Quoted 10 lines
hw/scsi/lsi53c895a.c +3
-2QTAILQ_INIT (&dev->requests); - rc =scsi_device_init (dev); -if (rc ==0 ) { +scsi_device_realize (dev, &local_err); +if (local_err) { dev->vmsentry =qemu_add_vm_change_state_handler (scsi_dma_restart_cb, dev); +error_propagate (errp, local_err); }
Maybe I'm misunderstanding something, but it looks to me as if the handler was previously installed in case of success, and now it's only installed on failure?
Quoted 14 lines
hw/scsi/lsi53c895a.c +0
-3if (bus->info->hotplug) { bus->info->hotplug (bus, dev); } - -err: -return rc; }tests/qemu-iotests/051.out +0
-0
tests/qemu-iotests/051.out QEMU_PROG: -drive if=virtio: Device 'virtio-blk-pci' could not be initialized · line 127
Quoted 7 lines
tests/qemu-iotests/051.out +1
-1@@ -122,7 +122,7 @@ QEMU_PROG: -drive if=virtio: Device 'virtio-blk-pci' could not be initialized Testing: -drive if=scsi QEMU X.Y.Z monitor - type 'help' for more information -(qemu) QEMU_PROG: -drive if=scsi: Device needs media, but drive is empty +(qemu) QEMU_PROG: Device needs media, but drive is empty QEMU_PROG: Device initialization failed. QEMU_PROG: Initialization of device lsi53c895a failed
The old error message was certainly more useful. Not sure if there's a good way to retain it, though.
Kevin
Re: [Qemu-devel] [PATCH 2/2] scsi-bus: Convert DeviceClass init to realize
Quoted 29 lines · Kevin Wolf
Am 05.08.2014 um 11:11 hat Fam Zheng geschrieben:
Quoted 28 lines
Replace "init/destroy" with "realize/unrealize" in SCSIDeviceClass, which has errp as a parameter. So all the implementations now uses
error_setginstead oferror_reportfor reporting error.Also in lsi53c895a, report the error when initializing the if=scsi devices, before dropping it, because the callee's
error_reportis changed toerror_segs.Signed-off-by Fam Zheng <[email protected]>--- hw/scsi/lsi53c895a.c | 2 ++ hw/scsi/scsi-bus.c | 64 ++++++++++++++++++------------------- hw/scsi/scsi-disk.c | 78 ++++++++++++++++++++++++---------------------- hw/scsi/scsi-generic.c | 37 +++++++++++----------- include/hw/scsi/scsi.h | 7 +++-- tests/qemu-iotests/051.out | 4 +-- 6 files changed, 96 insertions(+), 96 deletions(-)hw/scsi/lsi53c895a.c +1
-0@@ -19,6 +19,7 @@#include "hw/pci/pci.h" #include "hw/scsi/scsi.h" #include "sysemu/dma.h" +#include "qemu/error-report.h"
Quoted 13 lines
hw/scsi/lsi53c895a.c static int lsi_scsi_init(PCIDevice *dev) · line 2128
Quoted 10 lines
hw/scsi/lsi53c895a.c +1
-0//#define DEBUG_LSI //#define DEBUG_LSI_REG @@ -2121,6 +2122,7 @@ static int lsi_scsi_init(PCIDevice *dev)if (!d->hotplugged) {scsi_bus_legacy_handle_cmdline (&s->bus, &err);if (err !=NULL ) { +error_report ("%s" ,error_get_pretty (err));error_free (err);return -1 ; }Wouldn't
qerror_report_err()be more useful? Or is already a QMP error emitted in a different place in the callchain?
This code block is specifically for command line, see above "if (!d->hotplugged)" condition check and "scsi_bus_legacy_handle_cmdline(&s->bus, &err);" call. So I think error_report is good enough.
Quoted 2 lines
The same question is true for the added
error_report()calls in patch 1.
Two error_report() added in patch 1: the first is consistent with the other error_report() in the context function; the second is replaced by error_setg in this patch when errp is added.
Quoted 22 lines
Quoted 22 lines
hw/scsi/lsi53c895a.c +4
-4@@ -169,43 +168,40 @@ static int scsi_qdev_init(DeviceState *qdev) d =scsi_device_find (bus, dev->channel, dev->id, ++lun); }while (d && d->lun == lun && lun < bus->info->max_lun);if (d && d->lun == lun) { -error_report ("no free lun" ); - goto err; +error_setg (errp,"no free lun" ); +return ; } dev->lun = lun; }else { d =scsi_device_find (bus, dev->channel, dev->id, dev->lun);assert (d);if (d->lun == dev->lun && dev != d) { -error_report ("lun already used by '%s'" , d->qdev.id); - goto err; +error_setg (errp,"lun already used by '%s'" , d->qdev.id); +return ; } }
Quoted 13 lines
hw/scsi/lsi53c895a.c static int scsi_qdev_init(DeviceState *qdev) · line 190
Quoted 9 lines
hw/scsi/lsi53c895a.c +3
-2QTAILQ_INIT (&dev->requests); - rc =scsi_device_init (dev); -if (rc ==0 ) { +scsi_device_realize (dev, &local_err); +if (local_err) { dev->vmsentry =qemu_add_vm_change_state_handler (scsi_dma_restart_cb, dev); +error_propagate (errp, local_err); }Maybe I'm misunderstanding something, but it looks to me as if the handler was previously installed in case of success, and now it's only installed on failure?
Good catch! Will fix.
Thanks,
Fam
Il 05/08/2014 11:11, Fam Zheng ha scritto:
Quoted 6 lines
DeviceClass->initis the old interface, let's convert scsi devices to the new ->realize API.A user visible change is the error message shown in qemu-iotests reference output, but I don't know what's the best way to retain it. So please review if and how this could be improved.
I think the new message is an improvement.
Kevin/Stefan, are you taking both patches or can you alternatively ack the first?
Paolo
Quoted 18 lines
Fam
Fam Zheng (2):
block: Pass errp inblkconf_geometry
scsi-bus: Convert DeviceClass init to realizehw/block/block.c | 18 +++++----- hw/block/virtio-blk.c | 7 ++-- hw/ide/qdev.c | 11 ++++-- hw/scsi/lsi53c895a.c | 2 ++ hw/scsi/scsi-bus.c | 64 +++++++++++++++++------------------ hw/scsi/scsi-disk.c | 83 +++++++++++++++++++++++++--------------------- hw/scsi/scsi-generic.c | 37 ++++++++++----------- include/hw/block/block.h | 6 ++-- include/hw/scsi/scsi.h | 7 ++-- tests/qemu-iotests/051.out | 4 +-- 10 files changed, 125 insertions(+), 114 deletions(-)