Patchwork [13/15] scsi: move dinfo to SCSIDevice

login
register
mail settings
Submitter Gerd Hoffmann
Date Nov. 17, 2009, 10:17 a.m.
Message ID <1258453071-3496-14-git-send-email-kraxel@redhat.com>
Download mbox | patch
Permalink /patch/38619/
State New
Headers show

Comments

Gerd Hoffmann - Nov. 17, 2009, 10:17 a.m.
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/scsi-disk.c    |   63 ++++++++++++++++++++++++++---------------------------
 hw/scsi-generic.c |   23 +++++++++----------
 hw/scsi.h         |    1 +
 3 files changed, 43 insertions(+), 44 deletions(-)
Christoph Hellwig - Nov. 17, 2009, noon
On Tue, Nov 17, 2009 at 11:17:49AM +0100, Gerd Hoffmann wrote:
> 
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>

Looks good,


Reviewed-by: Christoph Hellwig <hch@lst.de>

Patch

diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index c0c407b..e072226 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -54,7 +54,6 @@  typedef struct SCSIDiskReq {
 struct SCSIDiskState
 {
     SCSIDevice qdev;
-    DriveInfo *dinfo;
     /* The qemu block layer uses a fixed 512 byte sector size.
        This is the number of 512 byte blocks in a single scsi sector.  */
     int cluster_size;
@@ -179,7 +178,7 @@  static void scsi_read_data(SCSIDevice *d, uint32_t tag)
 
     r->iov.iov_len = n * 512;
     qemu_iovec_init_external(&r->qiov, &r->iov, 1);
-    r->req.aiocb = bdrv_aio_readv(s->dinfo->bdrv, r->sector, &r->qiov, n,
+    r->req.aiocb = bdrv_aio_readv(s->qdev.dinfo->bdrv, r->sector, &r->qiov, n,
                               scsi_read_complete, r);
     if (r->req.aiocb == NULL)
         scsi_command_complete(r, CHECK_CONDITION, HARDWARE_ERROR);
@@ -190,7 +189,7 @@  static void scsi_read_data(SCSIDevice *d, uint32_t tag)
 static int scsi_handle_write_error(SCSIDiskReq *r, int error)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
-    BlockInterfaceErrorAction action = drive_get_onerror(s->dinfo->bdrv);
+    BlockInterfaceErrorAction action = drive_get_onerror(s->qdev.dinfo->bdrv);
 
     if (action == BLOCK_ERR_IGNORE)
         return 0;
@@ -244,7 +243,7 @@  static void scsi_write_request(SCSIDiskReq *r)
     n = r->iov.iov_len / 512;
     if (n) {
         qemu_iovec_init_external(&r->qiov, &r->iov, 1);
-        r->req.aiocb = bdrv_aio_writev(s->dinfo->bdrv, r->sector, &r->qiov, n,
+        r->req.aiocb = bdrv_aio_writev(s->qdev.dinfo->bdrv, r->sector, &r->qiov, n,
                                    scsi_write_complete, r);
         if (r->req.aiocb == NULL)
             scsi_command_complete(r, CHECK_CONDITION,
@@ -403,7 +402,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
     switch (command) {
     case TEST_UNIT_READY:
 	DPRINTF("Test Unit Ready\n");
-        if (!bdrv_is_inserted(s->dinfo->bdrv))
+        if (!bdrv_is_inserted(s->qdev.dinfo->bdrv))
             goto notready;
 	break;
     case REQUEST_SENSE:
@@ -450,7 +449,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
 
                         r->iov.iov_len = 0;
 
-                        if (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM) {
+                        if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
                             outbuf[r->iov.iov_len++] = 5;
                         } else {
                             outbuf[r->iov.iov_len++] = 0;
@@ -481,7 +480,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                         r->iov.iov_len = 0;
 
                         /* Supported page codes */
-                        if (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM) {
+                        if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
                             outbuf[r->iov.iov_len++] = 5;
                         } else {
                             outbuf[r->iov.iov_len++] = 0;
@@ -499,14 +498,14 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                     {
                         /* Device identification page, mandatory */
                         int max_len = 255 - 8;
-                        int id_len = strlen(bdrv_get_device_name(s->dinfo->bdrv));
+                        int id_len = strlen(bdrv_get_device_name(s->qdev.dinfo->bdrv));
                         if (id_len > max_len)
                             id_len = max_len;
 
                         DPRINTF("Inquiry EVPD[Device identification] "
                                 "buffer size %d\n", len);
                         r->iov.iov_len = 0;
-                        if (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM) {
+                        if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
                             outbuf[r->iov.iov_len++] = 5;
                         } else {
                             outbuf[r->iov.iov_len++] = 0;
@@ -522,7 +521,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                         outbuf[r->iov.iov_len++] = id_len; // length of data following
 
                         memcpy(&outbuf[r->iov.iov_len],
-                               bdrv_get_device_name(s->dinfo->bdrv), id_len);
+                               bdrv_get_device_name(s->qdev.dinfo->bdrv), id_len);
                         r->iov.iov_len += id_len;
                     }
                     break;
@@ -562,7 +561,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
 
         if (lun || buf[1] >> 5) {
             outbuf[0] = 0x7f;	/* LUN not supported */
-	} else if (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM) {
+	} else if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
 	    outbuf[0] = 5;
             outbuf[1] = 0x80;
 	    memcpy(&outbuf[16], "QEMU CD-ROM    ", 16);
@@ -605,12 +604,12 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
             memset(p, 0, 4);
             outbuf[1] = 0; /* Default media type.  */
             outbuf[3] = 0; /* Block descriptor length.  */
-            if (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM ||
-                bdrv_is_read_only(s->dinfo->bdrv)) {
+            if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM ||
+                bdrv_is_read_only(s->qdev.dinfo->bdrv)) {
                 outbuf[2] = 0x80; /* Readonly.  */
             }
             p += 4;
-            bdrv_get_geometry(s->dinfo->bdrv, &nb_sectors);
+            bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors);
             if ((~dbd) & nb_sectors) {
                 nb_sectors /= s->cluster_size;
                 nb_sectors--;
@@ -635,7 +634,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                 p[0] = 4;
                 p[1] = 0x16;
                 /* if a geometry hint is available, use it */
-                bdrv_get_geometry_hint(s->dinfo->bdrv, &cylinders, &heads, &secs);
+                bdrv_get_geometry_hint(s->qdev.dinfo->bdrv, &cylinders, &heads, &secs);
                 p[2] = (cylinders >> 16) & 0xff;
                 p[3] = (cylinders >> 8) & 0xff;
                 p[4] = cylinders & 0xff;
@@ -669,7 +668,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                 p[2] = 5000 >> 8;
                 p[3] = 5000 & 0xff;
                 /* if a geometry hint is available, use it */
-                bdrv_get_geometry_hint(s->dinfo->bdrv, &cylinders, &heads, &secs);
+                bdrv_get_geometry_hint(s->qdev.dinfo->bdrv, &cylinders, &heads, &secs);
                 p[4] = heads & 0xff;
                 p[5] = secs & 0xff;
                 p[6] = s->cluster_size * 2;
@@ -702,13 +701,13 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                 memset(p,0,20);
                 p[0] = 8;
                 p[1] = 0x12;
-                if (bdrv_enable_write_cache(s->dinfo->bdrv)) {
+                if (bdrv_enable_write_cache(s->qdev.dinfo->bdrv)) {
                      p[2] = 4; /* WCE */
                 }
                 p += 20;
             }
             if ((page == 0x3f || page == 0x2a)
-                    && (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM)) {
+                    && (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM)) {
                 /* CD Capabilities and Mechanical Status page. */
                 p[0] = 0x2a;
                 p[1] = 0x14;
@@ -719,7 +718,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
                 p[5] = 0xff; /* CD DA, DA accurate, RW supported,
                                          RW corrected, C2 errors, ISRC,
                                          UPC, Bar code */
-                p[6] = 0x2d | (bdrv_is_locked(s->dinfo->bdrv)? 2 : 0);
+                p[6] = 0x2d | (bdrv_is_locked(s->qdev.dinfo->bdrv)? 2 : 0);
                 /* Locking supported, jumper present, eject, tray */
                 p[7] = 0; /* no volume & mute control, no
                                       changer */
@@ -745,20 +744,20 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
         break;
     case START_STOP:
         DPRINTF("Start Stop Unit\n");
-        if (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM &&
+        if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM &&
             (buf[4] & 2))
             /* load/eject medium */
-            bdrv_eject(s->dinfo->bdrv, !(buf[4] & 1));
+            bdrv_eject(s->qdev.dinfo->bdrv, !(buf[4] & 1));
 	break;
     case ALLOW_MEDIUM_REMOVAL:
         DPRINTF("Prevent Allow Medium Removal (prevent = %d)\n", buf[4] & 3);
-        bdrv_set_locked(s->dinfo->bdrv, buf[4] & 1);
+        bdrv_set_locked(s->qdev.dinfo->bdrv, buf[4] & 1);
 	break;
     case READ_CAPACITY:
 	DPRINTF("Read Capacity\n");
         /* The normal LEN field for this command is zero.  */
 	memset(outbuf, 0, 8);
-	bdrv_get_geometry(s->dinfo->bdrv, &nb_sectors);
+	bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors);
         nb_sectors /= s->cluster_size;
         /* Returned value is the address of the last sector.  */
         if (nb_sectors) {
@@ -804,7 +803,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
         break;
     case SYNCHRONIZE_CACHE:
         DPRINTF("Synchronise cache (sector %" PRId64 ", count %d)\n", lba, len);
-        bdrv_flush(s->dinfo->bdrv);
+        bdrv_flush(s->qdev.dinfo->bdrv);
         break;
     case READ_TOC:
         {
@@ -813,7 +812,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
             msf = buf[1] & 2;
             format = buf[2] & 0xf;
             start_track = buf[6];
-            bdrv_get_geometry(s->dinfo->bdrv, &nb_sectors);
+            bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors);
             DPRINTF("Read TOC (track %d format %d msf %d)\n", start_track, format, msf >> 1);
             nb_sectors /= s->cluster_size;
             switch(format) {
@@ -867,7 +866,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
         if ((buf[1] & 31) == 0x10) {
             DPRINTF("SAI READ CAPACITY(16)\n");
             memset(outbuf, 0, len);
-            bdrv_get_geometry(s->dinfo->bdrv, &nb_sectors);
+            bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors);
             nb_sectors /= s->cluster_size;
             /* Returned value is the address of the last sector.  */
             if (nb_sectors) {
@@ -938,7 +937,7 @@  static void scsi_destroy(SCSIDevice *dev)
         r = DO_UPCAST(SCSIDiskReq, req, QTAILQ_FIRST(&s->qdev.requests));
         scsi_remove_request(r);
     }
-    drive_uninit(s->dinfo);
+    drive_uninit(s->qdev.dinfo);
 }
 
 static int scsi_disk_initfn(SCSIDevice *dev)
@@ -946,24 +945,24 @@  static int scsi_disk_initfn(SCSIDevice *dev)
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
     uint64_t nb_sectors;
 
-    if (!s->dinfo || !s->dinfo->bdrv) {
+    if (!s->qdev.dinfo || !s->qdev.dinfo->bdrv) {
         qemu_error("scsi-disk: drive property not set\n");
         return -1;
     }
 
-    if (bdrv_get_type_hint(s->dinfo->bdrv) == BDRV_TYPE_CDROM) {
+    if (bdrv_get_type_hint(s->qdev.dinfo->bdrv) == BDRV_TYPE_CDROM) {
         s->cluster_size = 4;
     } else {
         s->cluster_size = 1;
     }
     s->qdev.blocksize = 512;
     s->qdev.type = TYPE_DISK;
-    bdrv_get_geometry(s->dinfo->bdrv, &nb_sectors);
+    bdrv_get_geometry(s->qdev.dinfo->bdrv, &nb_sectors);
     nb_sectors /= s->cluster_size;
     if (nb_sectors)
         nb_sectors--;
     s->max_lba = nb_sectors;
-    strncpy(s->drive_serial_str, drive_get_serial(s->dinfo->bdrv),
+    strncpy(s->drive_serial_str, drive_get_serial(s->qdev.dinfo->bdrv),
             sizeof(s->drive_serial_str));
     if (strlen(s->drive_serial_str) == 0)
         pstrcpy(s->drive_serial_str, sizeof(s->drive_serial_str), "0");
@@ -983,7 +982,7 @@  static SCSIDeviceInfo scsi_disk_info = {
     .cancel_io    = scsi_cancel_io,
     .get_buf      = scsi_get_buf,
     .qdev.props   = (Property[]) {
-        DEFINE_PROP_DRIVE("drive", SCSIDiskState, dinfo),
+        DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.dinfo),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
diff --git a/hw/scsi-generic.c b/hw/scsi-generic.c
index d9f08b6..150bd93 100644
--- a/hw/scsi-generic.c
+++ b/hw/scsi-generic.c
@@ -58,7 +58,6 @@  typedef struct SCSIGenericReq {
 struct SCSIGenericState
 {
     SCSIDevice qdev;
-    DriveInfo *dinfo;
     int lun;
     int driver_status;
     uint8_t sensebuf[SCSI_SENSE_BUF_SIZE];
@@ -237,7 +236,7 @@  static void scsi_read_data(SCSIDevice *d, uint32_t tag)
         return;
     }
 
-    ret = execute_command(s->dinfo->bdrv, r, SG_DXFER_FROM_DEV, scsi_read_complete);
+    ret = execute_command(s->qdev.dinfo->bdrv, r, SG_DXFER_FROM_DEV, scsi_read_complete);
     if (ret == -1) {
         scsi_command_complete(r, -EINVAL);
         return;
@@ -288,7 +287,7 @@  static int scsi_write_data(SCSIDevice *d, uint32_t tag)
         return 0;
     }
 
-    ret = execute_command(s->dinfo->bdrv, r, SG_DXFER_TO_DEV, scsi_write_complete);
+    ret = execute_command(s->qdev.dinfo->bdrv, r, SG_DXFER_TO_DEV, scsi_write_complete);
     if (ret == -1) {
         scsi_command_complete(r, -EINVAL);
         return 1;
@@ -392,7 +391,7 @@  static int32_t scsi_send_command(SCSIDevice *d, uint32_t tag,
             free(r->buf);
         r->buflen = 0;
         r->buf = NULL;
-        ret = execute_command(s->dinfo->bdrv, r, SG_DXFER_NONE, scsi_command_complete);
+        ret = execute_command(s->qdev.dinfo->bdrv, r, SG_DXFER_NONE, scsi_command_complete);
         if (ret == -1) {
             scsi_command_complete(r, -EINVAL);
             return 0;
@@ -487,7 +486,7 @@  static void scsi_destroy(SCSIDevice *d)
         r = DO_UPCAST(SCSIGenericReq, req, QTAILQ_FIRST(&s->qdev.requests));
         scsi_remove_request(r);
     }
-    drive_uninit(s->dinfo);
+    drive_uninit(s->qdev.dinfo);
 }
 
 static int scsi_generic_initfn(SCSIDevice *dev)
@@ -496,26 +495,26 @@  static int scsi_generic_initfn(SCSIDevice *dev)
     int sg_version;
     struct sg_scsi_id scsiid;
 
-    if (!s->dinfo || !s->dinfo->bdrv) {
+    if (!s->qdev.dinfo || !s->qdev.dinfo->bdrv) {
         qemu_error("scsi-generic: drive property not set\n");
         return -1;
     }
 
     /* check we are really using a /dev/sg* file */
-    if (!bdrv_is_sg(s->dinfo->bdrv)) {
+    if (!bdrv_is_sg(s->qdev.dinfo->bdrv)) {
         qemu_error("scsi-generic: not /dev/sg*\n");
         return -1;
     }
 
     /* check we are using a driver managing SG_IO (version 3 and after */
-    if (bdrv_ioctl(s->dinfo->bdrv, SG_GET_VERSION_NUM, &sg_version) < 0 ||
+    if (bdrv_ioctl(s->qdev.dinfo->bdrv, SG_GET_VERSION_NUM, &sg_version) < 0 ||
         sg_version < 30000) {
         qemu_error("scsi-generic: scsi generic interface too old\n");
         return -1;
     }
 
     /* get LUN of the /dev/sg? */
-    if (bdrv_ioctl(s->dinfo->bdrv, SG_GET_SCSI_ID, &scsiid)) {
+    if (bdrv_ioctl(s->qdev.dinfo->bdrv, SG_GET_SCSI_ID, &scsiid)) {
         qemu_error("scsi-generic: SG_GET_SCSI_ID ioctl failed\n");
         return -1;
     }
@@ -526,11 +525,11 @@  static int scsi_generic_initfn(SCSIDevice *dev)
     s->qdev.type = scsiid.scsi_type;
     DPRINTF("device type %d\n", s->qdev.type);
     if (s->qdev.type == TYPE_TAPE) {
-        s->qdev.blocksize = get_stream_blocksize(s->dinfo->bdrv);
+        s->qdev.blocksize = get_stream_blocksize(s->qdev.dinfo->bdrv);
         if (s->qdev.blocksize == -1)
             s->qdev.blocksize = 0;
     } else {
-        s->qdev.blocksize = get_blocksize(s->dinfo->bdrv);
+        s->qdev.blocksize = get_blocksize(s->qdev.dinfo->bdrv);
         /* removable media returns 0 if not present */
         if (s->qdev.blocksize <= 0) {
             if (s->qdev.type == TYPE_ROM || s->qdev.type  == TYPE_WORM)
@@ -557,7 +556,7 @@  static SCSIDeviceInfo scsi_generic_info = {
     .cancel_io    = scsi_cancel_io,
     .get_buf      = scsi_get_buf,
     .qdev.props   = (Property[]) {
-        DEFINE_PROP_DRIVE("drive", SCSIGenericState, dinfo),
+        DEFINE_PROP_DRIVE("drive", SCSIGenericState, qdev.dinfo),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
diff --git a/hw/scsi.h b/hw/scsi.h
index bdba1db..cb85864 100644
--- a/hw/scsi.h
+++ b/hw/scsi.h
@@ -48,6 +48,7 @@  struct SCSIDevice
 {
     DeviceState qdev;
     uint32_t id;
+    DriveInfo *dinfo;
     SCSIDeviceInfo *info;
     QTAILQ_HEAD(, SCSIRequest) requests;
     int blocksize;