peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame qemu_virtio-scsi_support.patch

Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/default-configs/pci.mak qemu-kvm-1.0.virtio-scsi/default-configs/pci.mak
Justin M. Forbes 357a44
--- qemu-kvm-1.0/default-configs/pci.mak	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/default-configs/pci.mak	2012-02-07 14:44:53.424905251 -0600
Justin M. Forbes 357a44
@@ -1,5 +1,6 @@
Justin M. Forbes 357a44
 CONFIG_PCI=y
Justin M. Forbes 357a44
 CONFIG_VIRTIO_PCI=y
Justin M. Forbes 357a44
+CONFIG_VIRTIO_SCSI=y
Justin M. Forbes 357a44
 CONFIG_VIRTIO=y
Justin M. Forbes 357a44
 CONFIG_USB_UHCI=y
Justin M. Forbes 357a44
 CONFIG_USB_OHCI=y
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/default-configs/s390x-softmmu.mak qemu-kvm-1.0.virtio-scsi/default-configs/s390x-softmmu.mak
Justin M. Forbes 357a44
--- qemu-kvm-1.0/default-configs/s390x-softmmu.mak	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/default-configs/s390x-softmmu.mak	2012-02-07 14:44:53.424905251 -0600
Justin M. Forbes 357a44
@@ -1 +1,2 @@
Justin M. Forbes 357a44
 CONFIG_VIRTIO=y
Justin M. Forbes 357a44
+CONFIG_VIRTIO_SCSI=y
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/dma.h qemu-kvm-1.0.virtio-scsi/dma.h
Justin M. Forbes 357a44
--- qemu-kvm-1.0/dma.h	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/dma.h	2012-02-07 14:44:53.425905267 -0600
Justin M. Forbes 357a44
@@ -17,6 +17,13 @@
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 typedef struct ScatterGatherEntry ScatterGatherEntry;
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+struct QEMUSGList {
Justin M. Forbes 357a44
+    ScatterGatherEntry *sg;
Justin M. Forbes 357a44
+    int nsg;
Justin M. Forbes 357a44
+    int nalloc;
Justin M. Forbes 357a44
+    size_t size;
Justin M. Forbes 357a44
+};
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 #if defined(TARGET_PHYS_ADDR_BITS)
Justin M. Forbes 357a44
 typedef target_phys_addr_t dma_addr_t;
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
@@ -32,13 +39,6 @@ struct ScatterGatherEntry {
Justin M. Forbes 357a44
     dma_addr_t len;
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-struct QEMUSGList {
Justin M. Forbes 357a44
-    ScatterGatherEntry *sg;
Justin M. Forbes 357a44
-    int nsg;
Justin M. Forbes 357a44
-    int nalloc;
Justin M. Forbes 357a44
-    dma_addr_t size;
Justin M. Forbes 357a44
-};
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
 void qemu_sglist_init(QEMUSGList *qsg, int alloc_hint);
Justin M. Forbes 357a44
 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len);
Justin M. Forbes 357a44
 void qemu_sglist_destroy(QEMUSGList *qsg);
Justin M. Forbes 357a44
@@ -58,4 +58,10 @@ BlockDriverAIOCB *dma_bdrv_read(BlockDri
Justin M. Forbes 357a44
 BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
Justin M. Forbes 357a44
                                  QEMUSGList *sg, uint64_t sector,
Justin M. Forbes 357a44
                                  BlockDriverCompletionFunc *cb, void *opaque);
Justin M. Forbes 357a44
+uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg);
Justin M. Forbes 357a44
+uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
Justin M. Forbes 357a44
+                    QEMUSGList *sg, enum BlockAcctType type);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 #endif
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/dma-helpers.c qemu-kvm-1.0.virtio-scsi/dma-helpers.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/dma-helpers.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/dma-helpers.c	2012-02-07 14:44:53.424905251 -0600
Justin M. Forbes 357a44
@@ -196,3 +196,39 @@ BlockDriverAIOCB *dma_bdrv_write(BlockDr
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, true);
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static uint64_t dma_buf_rw(uint8_t *ptr, int32_t len, QEMUSGList *sg, bool to_dev)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    uint64_t resid;
Justin M. Forbes 357a44
+    int sg_cur_index;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    resid = sg->size;
Justin M. Forbes 357a44
+    sg_cur_index = 0;
Justin M. Forbes 357a44
+    len = MIN(len, resid);
Justin M. Forbes 357a44
+    while (len > 0) {
Justin M. Forbes 357a44
+        ScatterGatherEntry entry = sg->sg[sg_cur_index++];
Justin M. Forbes 357a44
+        cpu_physical_memory_rw(entry.base, ptr, MIN(len, entry.len), !to_dev);
Justin M. Forbes 357a44
+        ptr += entry.len;
Justin M. Forbes 357a44
+        len -= entry.len;
Justin M. Forbes 357a44
+        resid -= entry.len;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    return resid;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+uint64_t dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    return dma_buf_rw(ptr, len, sg, 0);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    return dma_buf_rw(ptr, len, sg, 1);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
Justin M. Forbes 357a44
+                    QEMUSGList *sg, enum BlockAcctType type)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    bdrv_acct_start(bs, cookie, sg->size, type);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/esp.c qemu-kvm-1.0.virtio-scsi/hw/esp.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/esp.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/esp.c	2012-02-07 14:44:53.425905267 -0600
Justin M. Forbes 357a44
@@ -389,7 +389,8 @@ static void esp_do_dma(ESPState *s)
Justin M. Forbes 357a44
     esp_dma_done(s);
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-static void esp_command_complete(SCSIRequest *req, uint32_t status)
Justin M. Forbes 357a44
+static void esp_command_complete(SCSIRequest *req, uint32_t status,
Justin M. Forbes 357a44
+                                 int32_t resid)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     ESPState *s = DO_UPCAST(ESPState, busdev.qdev, req->bus->qbus.parent);
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/ide/ahci.c qemu-kvm-1.0.virtio-scsi/hw/ide/ahci.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/ide/ahci.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/ide/ahci.c	2012-02-07 14:44:53.426905283 -0600
Justin M. Forbes 357a44
@@ -425,55 +425,6 @@ static void ahci_reg_init(AHCIState *s)
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-static uint32_t read_from_sglist(uint8_t *buffer, uint32_t len,
Justin M. Forbes 357a44
-                                 QEMUSGList *sglist)
Justin M. Forbes 357a44
-{
Justin M. Forbes 357a44
-    uint32_t i = 0;
Justin M. Forbes 357a44
-    uint32_t total = 0, once;
Justin M. Forbes 357a44
-    ScatterGatherEntry *cur_prd;
Justin M. Forbes 357a44
-    uint32_t sgcount;
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-    cur_prd = sglist->sg;
Justin M. Forbes 357a44
-    sgcount = sglist->nsg;
Justin M. Forbes 357a44
-    for (i = 0; len && sgcount; i++) {
Justin M. Forbes 357a44
-        once = MIN(cur_prd->len, len);
Justin M. Forbes 357a44
-        cpu_physical_memory_read(cur_prd->base, buffer, once);
Justin M. Forbes 357a44
-        cur_prd++;
Justin M. Forbes 357a44
-        sgcount--;
Justin M. Forbes 357a44
-        len -= once;
Justin M. Forbes 357a44
-        buffer += once;
Justin M. Forbes 357a44
-        total += once;
Justin M. Forbes 357a44
-    }
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-    return total;
Justin M. Forbes 357a44
-}
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-static uint32_t write_to_sglist(uint8_t *buffer, uint32_t len,
Justin M. Forbes 357a44
-                                QEMUSGList *sglist)
Justin M. Forbes 357a44
-{
Justin M. Forbes 357a44
-    uint32_t i = 0;
Justin M. Forbes 357a44
-    uint32_t total = 0, once;
Justin M. Forbes 357a44
-    ScatterGatherEntry *cur_prd;
Justin M. Forbes 357a44
-    uint32_t sgcount;
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-    DPRINTF(-1, "total: 0x%x bytes\n", len);
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-    cur_prd = sglist->sg;
Justin M. Forbes 357a44
-    sgcount = sglist->nsg;
Justin M. Forbes 357a44
-    for (i = 0; len && sgcount; i++) {
Justin M. Forbes 357a44
-        once = MIN(cur_prd->len, len);
Justin M. Forbes 357a44
-        DPRINTF(-1, "write 0x%x bytes to 0x%lx\n", once, (long)cur_prd->base);
Justin M. Forbes 357a44
-        cpu_physical_memory_write(cur_prd->base, buffer, once);
Justin M. Forbes 357a44
-        cur_prd++;
Justin M. Forbes 357a44
-        sgcount--;
Justin M. Forbes 357a44
-        len -= once;
Justin M. Forbes 357a44
-        buffer += once;
Justin M. Forbes 357a44
-        total += once;
Justin M. Forbes 357a44
-    }
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-    return total;
Justin M. Forbes 357a44
-}
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
 static void check_cmd(AHCIState *s, int port)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     AHCIPortRegs *pr = &s->dev[port].port_regs;
Justin M. Forbes 357a44
@@ -794,9 +745,8 @@ static void process_ncq_command(AHCIStat
Justin M. Forbes 357a44
             DPRINTF(port, "tag %d aio read %"PRId64"\n",
Justin M. Forbes 357a44
                     ncq_tfs->tag, ncq_tfs->lba);
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-            bdrv_acct_start(ncq_tfs->drive->port.ifs[0].bs, &ncq_tfs->acct,
Justin M. Forbes 357a44
-                            (ncq_tfs->sector_count-1) * BDRV_SECTOR_SIZE,
Justin M. Forbes 357a44
-                            BDRV_ACCT_READ);
Justin M. Forbes 357a44
+            dma_acct_start(ncq_tfs->drive->port.ifs[0].bs, &ncq_tfs->acct,
Justin M. Forbes 357a44
+                           &ncq_tfs->sglist, BDRV_ACCT_READ);
Justin M. Forbes 357a44
             ncq_tfs->aiocb = dma_bdrv_read(ncq_tfs->drive->port.ifs[0].bs,
Justin M. Forbes 357a44
                                            &ncq_tfs->sglist, ncq_tfs->lba,
Justin M. Forbes 357a44
                                            ncq_cb, ncq_tfs);
Justin M. Forbes 357a44
@@ -808,9 +758,8 @@ static void process_ncq_command(AHCIStat
Justin M. Forbes 357a44
             DPRINTF(port, "tag %d aio write %"PRId64"\n",
Justin M. Forbes 357a44
                     ncq_tfs->tag, ncq_tfs->lba);
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-            bdrv_acct_start(ncq_tfs->drive->port.ifs[0].bs, &ncq_tfs->acct,
Justin M. Forbes 357a44
-                            (ncq_tfs->sector_count-1) * BDRV_SECTOR_SIZE,
Justin M. Forbes 357a44
-                            BDRV_ACCT_WRITE);
Justin M. Forbes 357a44
+            dma_acct_start(ncq_tfs->drive->port.ifs[0].bs, &ncq_tfs->acct,
Justin M. Forbes 357a44
+                           &ncq_tfs->sglist, BDRV_ACCT_WRITE);
Justin M. Forbes 357a44
             ncq_tfs->aiocb = dma_bdrv_write(ncq_tfs->drive->port.ifs[0].bs,
Justin M. Forbes 357a44
                                             &ncq_tfs->sglist, ncq_tfs->lba,
Justin M. Forbes 357a44
                                             ncq_cb, ncq_tfs);
Justin M. Forbes 357a44
@@ -1015,12 +964,12 @@ static int ahci_start_transfer(IDEDMA *d
Justin M. Forbes 357a44
             is_write ? "writ" : "read", size, is_atapi ? "atapi" : "ata",
Justin M. Forbes 357a44
             has_sglist ? "" : "o");
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-    if (is_write && has_sglist && (s->data_ptr < s->data_end)) {
Justin M. Forbes 357a44
-        read_from_sglist(s->data_ptr, size, &s->sg);
Justin M. Forbes 357a44
-    }
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-    if (!is_write && has_sglist && (s->data_ptr < s->data_end)) {
Justin M. Forbes 357a44
-        write_to_sglist(s->data_ptr, size, &s->sg);
Justin M. Forbes 357a44
+    if (has_sglist && size) {
Justin M. Forbes 357a44
+        if (is_write) {
Justin M. Forbes 357a44
+            dma_buf_write(s->data_ptr, size, &s->sg);
Justin M. Forbes 357a44
+        } else {
Justin M. Forbes 357a44
+            dma_buf_read(s->data_ptr, size, &s->sg);
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     /* update number of transferred bytes */
Justin M. Forbes 357a44
@@ -1059,14 +1008,9 @@ static int ahci_dma_prepare_buf(IDEDMA *
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
Justin M. Forbes 357a44
     IDEState *s = &ad->port.ifs[0];
Justin M. Forbes 357a44
-    int i;
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     ahci_populate_sglist(ad, &s->sg);
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
-    s->io_buffer_size = 0;
Justin M. Forbes 357a44
-    for (i = 0; i < s->sg.nsg; i++) {
Justin M. Forbes 357a44
-        s->io_buffer_size += s->sg.sg[i].len;
Justin M. Forbes 357a44
-    }
Justin M. Forbes 357a44
+    s->io_buffer_size = s->sg.size;
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     DPRINTF(ad->port_no, "len=%#x\n", s->io_buffer_size);
Justin M. Forbes 357a44
     return s->io_buffer_size != 0;
Justin M. Forbes 357a44
@@ -1084,9 +1028,9 @@ static int ahci_dma_rw_buf(IDEDMA *dma,
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     if (is_write) {
Justin M. Forbes 357a44
-        write_to_sglist(p, l, &s->sg);
Justin M. Forbes 357a44
+        dma_buf_read(p, l, &s->sg);
Justin M. Forbes 357a44
     } else {
Justin M. Forbes 357a44
-        read_from_sglist(p, l, &s->sg);
Justin M. Forbes 357a44
+        dma_buf_write(p, l, &s->sg);
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     /* update number of transferred bytes */
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/lsi53c895a.c qemu-kvm-1.0.virtio-scsi/hw/lsi53c895a.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/lsi53c895a.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/lsi53c895a.c	2012-02-07 14:44:53.427905299 -0600
Justin M. Forbes 357a44
@@ -699,7 +699,7 @@ static int lsi_queue_req(LSIState *s, SC
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
  /* Callback to indicate that the SCSI layer has completed a command.  */
Justin M. Forbes 357a44
-static void lsi_command_complete(SCSIRequest *req, uint32_t status)
Justin M. Forbes 357a44
+static void lsi_command_complete(SCSIRequest *req, uint32_t status, int32_t resid)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
Justin M. Forbes 357a44
     int out;
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/pci.h qemu-kvm-1.0.virtio-scsi/hw/pci.h
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/pci.h	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/pci.h	2012-02-07 14:44:53.427905299 -0600
Justin M. Forbes 357a44
@@ -76,6 +76,7 @@
Justin M. Forbes 357a44
 #define PCI_DEVICE_ID_VIRTIO_BLOCK       0x1001
Justin M. Forbes 357a44
 #define PCI_DEVICE_ID_VIRTIO_BALLOON     0x1002
Justin M. Forbes 357a44
 #define PCI_DEVICE_ID_VIRTIO_CONSOLE     0x1003
Justin M. Forbes 357a44
+#define PCI_DEVICE_ID_VIRTIO_SCSI        0x1004
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 #define FMT_PCIBUS                      PRIx64
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/s390-virtio-bus.c qemu-kvm-1.0.virtio-scsi/hw/s390-virtio-bus.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/s390-virtio-bus.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/s390-virtio-bus.c	2012-02-07 14:44:53.428905315 -0600
Justin M. Forbes 357a44
@@ -158,6 +158,18 @@ static int s390_virtio_serial_init(VirtI
Justin M. Forbes 357a44
     return r;
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+static int s390_virtio_scsi_init(VirtIOS390Device *dev)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIODevice *vdev;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi);
Justin M. Forbes 357a44
+    if (!vdev) {
Justin M. Forbes 357a44
+        return -1;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    return s390_virtio_device_init(dev, vdev);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 static uint64_t s390_virtio_device_vq_token(VirtIOS390Device *dev, int vq)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     ram_addr_t token_off;
Justin M. Forbes 357a44
@@ -370,6 +382,17 @@ static VirtIOS390DeviceInfo s390_virtio_
Justin M. Forbes 357a44
     },
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+static VirtIOS390DeviceInfo s390_virtio_scsi = {
Justin M. Forbes 357a44
+    .init = s390_virtio_scsi_init,
Justin M. Forbes 357a44
+    .qdev.name = "virtio-scsi-s390",
Justin M. Forbes 357a44
+    .qdev.alias = "virtio-scsi",
Justin M. Forbes 357a44
+    .qdev.size = sizeof(VirtIOS390Device),
Justin M. Forbes 357a44
+    .qdev.props = (Property[]) {
Justin M. Forbes 357a44
+        DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOS390Device, host_features, scsi),
Justin M. Forbes 357a44
+        DEFINE_PROP_END_OF_LIST(),
Justin M. Forbes 357a44
+    },
Justin M. Forbes 357a44
+};
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 static int s390_virtio_busdev_init(DeviceState *dev, DeviceInfo *info)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     VirtIOS390DeviceInfo *_info = (VirtIOS390DeviceInfo *)info;
Justin M. Forbes 357a44
@@ -392,6 +415,7 @@ static void s390_virtio_register(void)
Justin M. Forbes 357a44
     s390_virtio_bus_register_withprop(&s390_virtio_serial);
Justin M. Forbes 357a44
     s390_virtio_bus_register_withprop(&s390_virtio_blk);
Justin M. Forbes 357a44
     s390_virtio_bus_register_withprop(&s390_virtio_net);
Justin M. Forbes 357a44
+    s390_virtio_bus_register_withprop(&s390_virtio_scsi);
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 device_init(s390_virtio_register);
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/s390-virtio-bus.h qemu-kvm-1.0.virtio-scsi/hw/s390-virtio-bus.h
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/s390-virtio-bus.h	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/s390-virtio-bus.h	2012-02-07 14:44:53.428905315 -0600
Justin M. Forbes 357a44
@@ -19,6 +19,7 @@
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 #include "virtio-net.h"
Justin M. Forbes 357a44
 #include "virtio-serial.h"
Justin M. Forbes 357a44
+#include "virtio-scsi.h"
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 #define VIRTIO_DEV_OFFS_TYPE		0	/* 8 bits */
Justin M. Forbes 357a44
 #define VIRTIO_DEV_OFFS_NUM_VQ		1	/* 8 bits */
Justin M. Forbes 357a44
@@ -47,6 +48,7 @@ typedef struct VirtIOS390Device {
Justin M. Forbes 357a44
     uint32_t host_features;
Justin M. Forbes 357a44
     virtio_serial_conf serial;
Justin M. Forbes 357a44
     virtio_net_conf net;
Justin M. Forbes 357a44
+    VirtIOSCSIConf scsi;
Justin M. Forbes 357a44
 } VirtIOS390Device;
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 typedef struct VirtIOS390Bus {
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/scsi-bus.c qemu-kvm-1.0.virtio-scsi/hw/scsi-bus.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/scsi-bus.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/scsi-bus.c	2012-02-07 14:44:53.428905315 -0600
Justin M. Forbes 357a44
@@ -5,6 +5,7 @@
Justin M. Forbes 357a44
 #include "qdev.h"
Justin M. Forbes 357a44
 #include "blockdev.h"
Justin M. Forbes 357a44
 #include "trace.h"
Justin M. Forbes 357a44
+#include "dma.h"
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 static char *scsibus_get_fw_dev_path(DeviceState *dev);
Justin M. Forbes 357a44
 static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
Justin M. Forbes 357a44
@@ -50,6 +51,7 @@ static void scsi_dma_restart_bh(void *op
Justin M. Forbes 357a44
                 scsi_req_continue(req);
Justin M. Forbes 357a44
                 break;
Justin M. Forbes 357a44
             case SCSI_XFER_NONE:
Justin M. Forbes 357a44
+                assert(!req->sg);
Justin M. Forbes 357a44
                 scsi_req_dequeue(req);
Justin M. Forbes 357a44
                 scsi_req_enqueue(req);
Justin M. Forbes 357a44
                 break;
Justin M. Forbes 357a44
@@ -512,6 +514,8 @@ SCSIRequest *scsi_req_new(SCSIDevice *d,
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     req->cmd = cmd;
Justin M. Forbes 357a44
+    req->resid = req->cmd.xfer;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
     switch (buf[0]) {
Justin M. Forbes 357a44
     case INQUIRY:
Justin M. Forbes 357a44
         trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
Justin M. Forbes 357a44
@@ -624,15 +628,25 @@ void scsi_req_build_sense(SCSIRequest *r
Justin M. Forbes 357a44
     req->sense_len = 18;
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-int32_t scsi_req_enqueue(SCSIRequest *req)
Justin M. Forbes 357a44
+static void scsi_req_enqueue_internal(SCSIRequest *req)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
-    int32_t rc;
Justin M. Forbes 357a44
-
Justin M. Forbes 357a44
     assert(!req->enqueued);
Justin M. Forbes 357a44
     scsi_req_ref(req);
Justin M. Forbes 357a44
+    if (req->bus->info->get_sg_list) {
Justin M. Forbes 357a44
+        req->sg = req->bus->info->get_sg_list(req);
Justin M. Forbes 357a44
+    } else {
Justin M. Forbes 357a44
+        req->sg = NULL;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
     req->enqueued = true;
Justin M. Forbes 357a44
     QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+int32_t scsi_req_enqueue(SCSIRequest *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    int32_t rc;
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+    assert (!req->retry);
Justin M. Forbes 357a44
+    scsi_req_enqueue_internal(req);
Justin M. Forbes 357a44
     scsi_req_ref(req);
Justin M. Forbes 357a44
     rc = req->ops->send_command(req, req->cmd.buf);
Justin M. Forbes 357a44
     scsi_req_unref(req);
Justin M. Forbes 357a44
@@ -1254,12 +1268,32 @@ void scsi_req_continue(SCSIRequest *req)
Justin M. Forbes 357a44
    Once it completes, calling scsi_req_continue will restart I/O.  */
Justin M. Forbes 357a44
 void scsi_req_data(SCSIRequest *req, int len)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
+    uint8_t *buf;
Justin M. Forbes 357a44
     if (req->io_canceled) {
Justin M. Forbes 357a44
         trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
Justin M. Forbes 357a44
-    } else {
Justin M. Forbes 357a44
-        trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
Justin M. Forbes 357a44
+        return;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
Justin M. Forbes 357a44
+    assert(req->cmd.mode != SCSI_XFER_NONE);
Justin M. Forbes 357a44
+    if (!req->sg) {
Justin M. Forbes 357a44
+        req->resid -= len;
Justin M. Forbes 357a44
         req->bus->info->transfer_data(req, len);
Justin M. Forbes 357a44
+        return;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    /* If the device calls scsi_req_data and the HBA specified a
Justin M. Forbes 357a44
+     * scatter/gather list, the transfer has to happen in a single
Justin M. Forbes 357a44
+     * step.  */
Justin M. Forbes 357a44
+    assert(!req->dma_started);
Justin M. Forbes 357a44
+    req->dma_started = true;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    buf = scsi_req_get_buf(req);
Justin M. Forbes 357a44
+    if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
Justin M. Forbes 357a44
+        req->resid = dma_buf_read(buf, len, req->sg);
Justin M. Forbes 357a44
+    } else {
Justin M. Forbes 357a44
+        req->resid = dma_buf_write(buf, len, req->sg);
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
+    scsi_req_continue(req);
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 void scsi_req_print(SCSIRequest *req)
Justin M. Forbes 357a44
@@ -1318,7 +1352,7 @@ void scsi_req_complete(SCSIRequest *req,
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     scsi_req_ref(req);
Justin M. Forbes 357a44
     scsi_req_dequeue(req);
Justin M. Forbes 357a44
-    req->bus->info->complete(req, req->status);
Justin M. Forbes 357a44
+    req->bus->info->complete(req, req->status, req->resid);
Justin M. Forbes 357a44
     scsi_req_unref(req);
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
@@ -1393,3 +1427,100 @@ SCSIDevice *scsi_device_find(SCSIBus *bu
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
     return target_dev;
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* SCSI request list.  For simplicity, pv points to the whole device */
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void put_scsi_requests(QEMUFile *f, void *pv, size_t size)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIDevice *s = pv;
Justin M. Forbes 357a44
+    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
Justin M. Forbes 357a44
+    SCSIRequest *req;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    QTAILQ_FOREACH(req, &s->requests, next) {
Justin M. Forbes 357a44
+        assert(!req->io_canceled);
Justin M. Forbes 357a44
+        assert(req->status == -1);
Justin M. Forbes 357a44
+        assert(req->retry);
Justin M. Forbes 357a44
+        assert(req->enqueued);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        qemu_put_sbyte(f, 1);
Justin M. Forbes 357a44
+        qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
Justin M. Forbes 357a44
+        qemu_put_be32s(f, &req->tag);
Justin M. Forbes 357a44
+        qemu_put_be32s(f, &req->lun);
Justin M. Forbes 357a44
+        if (bus->info->save_request) {
Justin M. Forbes 357a44
+            bus->info->save_request(f, req);
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        if (req->ops->save_request) {
Justin M. Forbes 357a44
+            req->ops->save_request(f, req);
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    qemu_put_sbyte(f, 0);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static int get_scsi_requests(QEMUFile *f, void *pv, size_t size)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIDevice *s = pv;
Justin M. Forbes 357a44
+    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    while (qemu_get_sbyte(f)) {
Justin M. Forbes 357a44
+        uint8_t buf[SCSI_CMD_BUF_SIZE];
Justin M. Forbes 357a44
+        uint32_t tag;
Justin M. Forbes 357a44
+        uint32_t lun;
Justin M. Forbes 357a44
+        SCSIRequest *req;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        qemu_get_buffer(f, buf, sizeof(buf));
Justin M. Forbes 357a44
+        qemu_get_be32s(f, &tag;;
Justin M. Forbes 357a44
+        qemu_get_be32s(f, &lun;;
Justin M. Forbes 357a44
+        req = scsi_req_new(s, tag, lun, buf, NULL);
Justin M. Forbes 357a44
+        if (bus->info->load_request) {
Justin M. Forbes 357a44
+            req->hba_private = bus->info->load_request(f, req);
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        if (req->ops->load_request) {
Justin M. Forbes 357a44
+            req->ops->load_request(f, req);
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        /* Just restart it later.  */
Justin M. Forbes 357a44
+        req->retry = true;
Justin M. Forbes 357a44
+        scsi_req_enqueue_internal(req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        /* At this point, the request will be kept alive by the reference
Justin M. Forbes 357a44
+         * added by scsi_req_enqueue_internal, so we can release our reference.
Justin M. Forbes 357a44
+         * The HBA of course will add its own reference in the load_request
Justin M. Forbes 357a44
+         * callback if it needs to hold on the SCSIRequest.
Justin M. Forbes 357a44
+         */
Justin M. Forbes 357a44
+        scsi_req_unref(req);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    return 0;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+const VMStateInfo vmstate_info_scsi_requests = {
Justin M. Forbes 357a44
+    .name = "scsi-requests",
Justin M. Forbes 357a44
+    .get  = get_scsi_requests,
Justin M. Forbes 357a44
+    .put  = put_scsi_requests,
Justin M. Forbes 357a44
+};
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+const VMStateDescription vmstate_scsi_device = {
Justin M. Forbes 357a44
+    .name = "SCSIDevice",
Justin M. Forbes 357a44
+    .version_id = 1,
Justin M. Forbes 357a44
+    .minimum_version_id = 1,
Justin M. Forbes 357a44
+    .minimum_version_id_old = 1,
Justin M. Forbes 357a44
+    .fields = (VMStateField[]) {
Justin M. Forbes 357a44
+        VMSTATE_UINT8(unit_attention.key, SCSIDevice),
Justin M. Forbes 357a44
+        VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
Justin M. Forbes 357a44
+        VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
Justin M. Forbes 357a44
+        VMSTATE_BOOL(sense_is_ua, SCSIDevice),
Justin M. Forbes 357a44
+        VMSTATE_UINT8_ARRAY(sense, SCSIDevice, SCSI_SENSE_BUF_SIZE),
Justin M. Forbes 357a44
+        VMSTATE_UINT32(sense_len, SCSIDevice),
Justin M. Forbes 357a44
+        {
Justin M. Forbes 357a44
+            .name         = "requests",
Justin M. Forbes 357a44
+            .version_id   = 0,
Justin M. Forbes 357a44
+            .field_exists = NULL,
Justin M. Forbes 357a44
+            .size         = 0,   /* ouch */
Justin M. Forbes 357a44
+            .info         = &vmstate_info_scsi_requests,
Justin M. Forbes 357a44
+            .flags        = VMS_SINGLE,
Justin M. Forbes 357a44
+            .offset       = 0,
Justin M. Forbes 357a44
+        },
Justin M. Forbes 357a44
+        VMSTATE_END_OF_LIST()
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+};
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/scsi-disk.c qemu-kvm-1.0.virtio-scsi/hw/scsi-disk.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/scsi-disk.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/scsi-disk.c	2012-02-07 14:44:53.429905331 -0600
Justin M. Forbes 357a44
@@ -38,6 +38,7 @@ do { fprintf(stderr, "scsi-disk: " fmt ,
Justin M. Forbes 357a44
 #include "sysemu.h"
Justin M. Forbes 357a44
 #include "blockdev.h"
Justin M. Forbes 357a44
 #include "block_int.h"
Justin M. Forbes 357a44
+#include "dma.h"
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 #ifdef __linux
Justin M. Forbes 357a44
 #include <scsi/sg.h>
Justin M. Forbes 357a44
@@ -110,12 +111,12 @@ static void scsi_cancel_io(SCSIRequest *
Justin M. Forbes 357a44
     r->req.aiocb = NULL;
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-static uint32_t scsi_init_iovec(SCSIDiskReq *r)
Justin M. Forbes 357a44
+static uint32_t scsi_init_iovec(SCSIDiskReq *r, size_t size)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
     if (!r->iov.iov_base) {
Justin M. Forbes 357a44
-        r->buflen = SCSI_DMA_BUF_SIZE;
Justin M. Forbes 357a44
+        r->buflen = size;
Justin M. Forbes 357a44
         r->iov.iov_base = qemu_blockalign(s->qdev.conf.bs, r->buflen);
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
     r->iov.iov_len = MIN(r->sector_count * 512, r->buflen);
Justin M. Forbes 357a44
@@ -123,6 +124,56 @@ static uint32_t scsi_init_iovec(SCSIDisk
Justin M. Forbes 357a44
     return r->qiov.size / 512;
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+static void scsi_disk_save_request(QEMUFile *f, SCSIRequest *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    qemu_put_be64s(f, &r->sector);
Justin M. Forbes 357a44
+    qemu_put_be32s(f, &r->sector_count);
Justin M. Forbes 357a44
+    qemu_put_be32s(f, &r->buflen);
Justin M. Forbes 357a44
+    if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) {
Justin M. Forbes 357a44
+        qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    qemu_get_be64s(f, &r->sector);
Justin M. Forbes 357a44
+    qemu_get_be32s(f, &r->sector_count);
Justin M. Forbes 357a44
+    qemu_get_be32s(f, &r->buflen);
Justin M. Forbes 357a44
+    if (r->buflen) {
Justin M. Forbes 357a44
+        scsi_init_iovec(r, r->buflen);
Justin M. Forbes 357a44
+        if (r->req.cmd.mode == SCSI_XFER_TO_DEV) {
Justin M. Forbes 357a44
+            qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len);
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    qemu_iovec_init_external(&r->qiov, &r->iov, 1);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void scsi_dma_complete(void * opaque, int ret)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIDiskReq *r = (SCSIDiskReq *)opaque;
Justin M. Forbes 357a44
+    SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    bdrv_acct_done(s->qdev.conf.bs, &r->acct);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    if (ret) {
Justin M. Forbes 357a44
+        if (scsi_handle_rw_error(r, -ret)) {
Justin M. Forbes 357a44
+            goto done;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    r->sector += r->sector_count;
Justin M. Forbes 357a44
+    r->sector_count = 0;
Justin M. Forbes 357a44
+    scsi_req_complete(&r->req, GOOD);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+done:
Justin M. Forbes 357a44
+    scsi_req_unref(&r->req);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 static void scsi_read_complete(void * opaque, int ret)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     SCSIDiskReq *r = (SCSIDiskReq *)opaque;
Justin M. Forbes 357a44
@@ -213,10 +264,17 @@ static void scsi_read_data(SCSIRequest *
Justin M. Forbes 357a44
         return;
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-    n = scsi_init_iovec(r);
Justin M. Forbes 357a44
-    bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
Justin M. Forbes 357a44
-    r->req.aiocb = bdrv_aio_readv(s->qdev.conf.bs, r->sector, &r->qiov, n,
Justin M. Forbes 357a44
-                              scsi_read_complete, r);
Justin M. Forbes 357a44
+    if (r->req.sg) {
Justin M. Forbes 357a44
+        dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BDRV_ACCT_READ);
Justin M. Forbes 357a44
+	r->req.resid -= r->req.sg->size;
Justin M. Forbes 357a44
+        r->req.aiocb = dma_bdrv_read(s->qdev.conf.bs, r->req.sg, r->sector,
Justin M. Forbes 357a44
+                                     scsi_dma_complete, r);
Justin M. Forbes 357a44
+    } else {
Justin M. Forbes 357a44
+        n = scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
Justin M. Forbes 357a44
+        bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
Justin M. Forbes 357a44
+        r->req.aiocb = bdrv_aio_readv(s->qdev.conf.bs, r->sector, &r->qiov, n,
Justin M. Forbes 357a44
+                                      scsi_read_complete, r);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
     if (r->req.aiocb == NULL) {
Justin M. Forbes 357a44
         scsi_read_complete(r, -EIO);
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
@@ -290,7 +348,7 @@ static void scsi_write_complete(void * o
Justin M. Forbes 357a44
     if (r->sector_count == 0) {
Justin M. Forbes 357a44
         scsi_req_complete(&r->req, GOOD);
Justin M. Forbes 357a44
     } else {
Justin M. Forbes 357a44
-        scsi_init_iovec(r);
Justin M. Forbes 357a44
+        scsi_init_iovec(r, SCSI_DMA_BUF_SIZE);
Justin M. Forbes 357a44
         DPRINTF("Write complete tag=0x%x more=%d\n", r->req.tag, r->qiov.size);
Justin M. Forbes 357a44
         scsi_req_data(&r->req, r->qiov.size);
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
@@ -318,21 +376,29 @@ static void scsi_write_data(SCSIRequest
Justin M. Forbes 357a44
         return;
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-    n = r->qiov.size / 512;
Justin M. Forbes 357a44
-    if (n) {
Justin M. Forbes 357a44
-        if (s->tray_open) {
Justin M. Forbes 357a44
-            scsi_write_complete(r, -ENOMEDIUM);
Justin M. Forbes 357a44
-            return;
Justin M. Forbes 357a44
-        }
Justin M. Forbes 357a44
+    if (!r->req.sg && !r->qiov.size) {
Justin M. Forbes 357a44
+        /* Called for the first time.  Ask the driver to send us more data.  */
Justin M. Forbes 357a44
+        scsi_write_complete(r, 0);
Justin M. Forbes 357a44
+        return;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    if (s->tray_open) {
Justin M. Forbes 357a44
+        scsi_write_complete(r, -ENOMEDIUM);
Justin M. Forbes 357a44
+        return;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    if (r->req.sg) {
Justin M. Forbes 357a44
+        dma_acct_start(s->qdev.conf.bs, &r->acct, r->req.sg, BDRV_ACCT_WRITE);
Justin M. Forbes 357a44
+	r->req.resid -= r->req.sg->size;
Justin M. Forbes 357a44
+        r->req.aiocb = dma_bdrv_write(s->qdev.conf.bs, r->req.sg, r->sector,
Justin M. Forbes 357a44
+                                      scsi_dma_complete, r);
Justin M. Forbes 357a44
+    } else {
Justin M. Forbes 357a44
+        n = r->qiov.size / 512;
Justin M. Forbes 357a44
         bdrv_acct_start(s->qdev.conf.bs, &r->acct, n * BDRV_SECTOR_SIZE, BDRV_ACCT_WRITE);
Justin M. Forbes 357a44
         r->req.aiocb = bdrv_aio_writev(s->qdev.conf.bs, r->sector, &r->qiov, n,
Justin M. Forbes 357a44
                                        scsi_write_complete, r);
Justin M. Forbes 357a44
-        if (r->req.aiocb == NULL) {
Justin M. Forbes 357a44
-            scsi_write_complete(r, -ENOMEM);
Justin M. Forbes 357a44
-        }
Justin M. Forbes 357a44
-    } else {
Justin M. Forbes 357a44
-        /* Called for the first time.  Ask the driver to send us more data.  */
Justin M. Forbes 357a44
-        scsi_write_complete(r, 0);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    if (r->req.aiocb == NULL) {
Justin M. Forbes 357a44
+        scsi_write_complete(r, -ENOMEM);
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
@@ -1601,6 +1667,8 @@ static const SCSIReqOps scsi_disk_reqops
Justin M. Forbes 357a44
     .write_data   = scsi_write_data,
Justin M. Forbes 357a44
     .cancel_io    = scsi_cancel_io,
Justin M. Forbes 357a44
     .get_buf      = scsi_get_buf,
Justin M. Forbes 357a44
+    .load_request = scsi_disk_load_request,
Justin M. Forbes 357a44
+    .save_request = scsi_disk_save_request,
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
Justin M. Forbes 357a44
@@ -1729,6 +1797,22 @@ static SCSIRequest *scsi_block_new_reque
Justin M. Forbes 357a44
     DEFINE_PROP_STRING("ver",  SCSIDiskState, version),         \
Justin M. Forbes 357a44
     DEFINE_PROP_STRING("serial",  SCSIDiskState, serial)
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+static const VMStateDescription vmstate_scsi_disk_state = {
Justin M. Forbes 357a44
+    .name = "scsi-disk",
Justin M. Forbes 357a44
+    .version_id = 1,
Justin M. Forbes 357a44
+    .minimum_version_id = 1,
Justin M. Forbes 357a44
+    .minimum_version_id_old = 1,
Justin M. Forbes 357a44
+    .fields = (VMStateField[]) {
Justin M. Forbes 357a44
+        VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState),
Justin M. Forbes 357a44
+        VMSTATE_BOOL(media_changed, SCSIDiskState),
Justin M. Forbes 357a44
+        VMSTATE_BOOL(media_event, SCSIDiskState),
Justin M. Forbes 357a44
+        VMSTATE_BOOL(eject_request, SCSIDiskState),
Justin M. Forbes 357a44
+        VMSTATE_BOOL(tray_open, SCSIDiskState),
Justin M. Forbes 357a44
+        VMSTATE_BOOL(tray_locked, SCSIDiskState),
Justin M. Forbes 357a44
+        VMSTATE_END_OF_LIST()
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+};
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 static SCSIDeviceInfo scsi_disk_info[] = {
Justin M. Forbes 357a44
     {
Justin M. Forbes 357a44
         .qdev.name    = "scsi-hd",
Justin M. Forbes 357a44
@@ -1736,6 +1820,7 @@ static SCSIDeviceInfo scsi_disk_info[] =
Justin M. Forbes 357a44
         .qdev.desc    = "virtual SCSI disk",
Justin M. Forbes 357a44
         .qdev.size    = sizeof(SCSIDiskState),
Justin M. Forbes 357a44
         .qdev.reset   = scsi_disk_reset,
Justin M. Forbes 357a44
+        .qdev.vmsd    = &vmstate_scsi_disk_state,
Justin M. Forbes 357a44
         .init         = scsi_hd_initfn,
Justin M. Forbes 357a44
         .destroy      = scsi_destroy,
Justin M. Forbes 357a44
         .alloc_req    = scsi_new_request,
Justin M. Forbes 357a44
@@ -1751,6 +1836,7 @@ static SCSIDeviceInfo scsi_disk_info[] =
Justin M. Forbes 357a44
         .qdev.desc    = "virtual SCSI CD-ROM",
Justin M. Forbes 357a44
         .qdev.size    = sizeof(SCSIDiskState),
Justin M. Forbes 357a44
         .qdev.reset   = scsi_disk_reset,
Justin M. Forbes 357a44
+        .qdev.vmsd    = &vmstate_scsi_disk_state,
Justin M. Forbes 357a44
         .init         = scsi_cd_initfn,
Justin M. Forbes 357a44
         .destroy      = scsi_destroy,
Justin M. Forbes 357a44
         .alloc_req    = scsi_new_request,
Justin M. Forbes 357a44
@@ -1766,6 +1852,7 @@ static SCSIDeviceInfo scsi_disk_info[] =
Justin M. Forbes 357a44
         .qdev.desc    = "SCSI block device passthrough",
Justin M. Forbes 357a44
         .qdev.size    = sizeof(SCSIDiskState),
Justin M. Forbes 357a44
         .qdev.reset   = scsi_disk_reset,
Justin M. Forbes 357a44
+        .qdev.vmsd    = &vmstate_scsi_disk_state,
Justin M. Forbes 357a44
         .init         = scsi_block_initfn,
Justin M. Forbes 357a44
         .destroy      = scsi_destroy,
Justin M. Forbes 357a44
         .alloc_req    = scsi_block_new_request,
Justin M. Forbes 357a44
@@ -1780,6 +1867,7 @@ static SCSIDeviceInfo scsi_disk_info[] =
Justin M. Forbes 357a44
         .qdev.desc    = "virtual SCSI disk or CD-ROM (legacy)",
Justin M. Forbes 357a44
         .qdev.size    = sizeof(SCSIDiskState),
Justin M. Forbes 357a44
         .qdev.reset   = scsi_disk_reset,
Justin M. Forbes 357a44
+        .qdev.vmsd    = &vmstate_scsi_disk_state,
Justin M. Forbes 357a44
         .init         = scsi_disk_initfn,
Justin M. Forbes 357a44
         .destroy      = scsi_destroy,
Justin M. Forbes 357a44
         .alloc_req    = scsi_new_request,
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/scsi-generic.c qemu-kvm-1.0.virtio-scsi/hw/scsi-generic.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/scsi-generic.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/scsi-generic.c	2012-02-07 14:44:53.430905347 -0600
Justin M. Forbes 357a44
@@ -59,6 +59,28 @@ typedef struct SCSIGenericReq {
Justin M. Forbes 357a44
     sg_io_hdr_t io_header;
Justin M. Forbes 357a44
 } SCSIGenericReq;
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+static void scsi_generic_save_request(QEMUFile *f, SCSIRequest *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    qemu_put_sbe32s(f, &r->buflen);
Justin M. Forbes 357a44
+    if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) {
Justin M. Forbes 357a44
+        assert(!r->req.sg);
Justin M. Forbes 357a44
+        qemu_put_buffer(f, r->buf, r->req.cmd.xfer);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void scsi_generic_load_request(QEMUFile *f, SCSIRequest *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    qemu_get_sbe32s(f, &r->buflen);
Justin M. Forbes 357a44
+    if (r->buflen && r->req.cmd.mode == SCSI_XFER_TO_DEV) {
Justin M. Forbes 357a44
+        assert(!r->req.sg);
Justin M. Forbes 357a44
+        qemu_get_buffer(f, r->buf, r->req.cmd.xfer);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 static void scsi_free_request(SCSIRequest *req)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     SCSIGenericReq *r = DO_UPCAST(SCSIGenericReq, req, req);
Justin M. Forbes 357a44
@@ -450,6 +472,8 @@ const SCSIReqOps scsi_generic_req_ops =
Justin M. Forbes 357a44
     .write_data   = scsi_write_data,
Justin M. Forbes 357a44
     .cancel_io    = scsi_cancel_io,
Justin M. Forbes 357a44
     .get_buf      = scsi_get_buf,
Justin M. Forbes 357a44
+    .load_request = scsi_generic_load_request,
Justin M. Forbes 357a44
+    .save_request = scsi_generic_save_request,
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,
Justin M. Forbes 357a44
@@ -467,6 +491,7 @@ static SCSIDeviceInfo scsi_generic_info
Justin M. Forbes 357a44
     .qdev.desc    = "pass through generic scsi device (/dev/sg*)",
Justin M. Forbes 357a44
     .qdev.size    = sizeof(SCSIDevice),
Justin M. Forbes 357a44
     .qdev.reset   = scsi_generic_reset,
Justin M. Forbes 357a44
+    .qdev.vmsd    = &vmstate_scsi_device,
Justin M. Forbes 357a44
     .init         = scsi_generic_initfn,
Justin M. Forbes 357a44
     .destroy      = scsi_destroy,
Justin M. Forbes 357a44
     .alloc_req    = scsi_new_request,
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/scsi.h qemu-kvm-1.0.virtio-scsi/hw/scsi.h
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/scsi.h	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/scsi.h	2012-02-07 14:44:53.430905347 -0600
Justin M. Forbes 357a44
@@ -47,8 +47,11 @@ struct SCSIRequest {
Justin M. Forbes 357a44
     uint32_t          tag;
Justin M. Forbes 357a44
     uint32_t          lun;
Justin M. Forbes 357a44
     uint32_t          status;
Justin M. Forbes 357a44
+    size_t            resid;
Justin M. Forbes 357a44
     SCSICommand       cmd;
Justin M. Forbes 357a44
     BlockDriverAIOCB  *aiocb;
Justin M. Forbes 357a44
+    QEMUSGList        *sg;
Justin M. Forbes 357a44
+    bool              dma_started;
Justin M. Forbes 357a44
     uint8_t sense[SCSI_SENSE_BUF_SIZE];
Justin M. Forbes 357a44
     uint32_t sense_len;
Justin M. Forbes 357a44
     bool enqueued;
Justin M. Forbes 357a44
@@ -78,6 +81,16 @@ struct SCSIDevice
Justin M. Forbes 357a44
     uint64_t max_lba;
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+extern const VMStateDescription vmstate_scsi_device;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+#define VMSTATE_SCSI_DEVICE(_field, _state) {                        \
Justin M. Forbes 357a44
+    .name       = (stringify(_field)),                               \
Justin M. Forbes 357a44
+    .size       = sizeof(SCSIDevice),                                \
Justin M. Forbes 357a44
+    .vmsd       = &vmstate_scsi_device,                              \
Justin M. Forbes 357a44
+    .flags      = VMS_STRUCT,                                        \
Justin M. Forbes 357a44
+    .offset     = vmstate_offset_value(_state, _field, SCSIDevice),  \
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 /* cdrom.c */
Justin M. Forbes 357a44
 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
Justin M. Forbes 357a44
 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
Justin M. Forbes 357a44
@@ -91,6 +104,9 @@ struct SCSIReqOps {
Justin M. Forbes 357a44
     void (*write_data)(SCSIRequest *req);
Justin M. Forbes 357a44
     void (*cancel_io)(SCSIRequest *req);
Justin M. Forbes 357a44
     uint8_t *(*get_buf)(SCSIRequest *req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    void (*save_request)(QEMUFile *f, SCSIRequest *req);
Justin M. Forbes 357a44
+    void (*load_request)(QEMUFile *f, SCSIRequest *req);
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 typedef int (*scsi_qdev_initfn)(SCSIDevice *dev);
Justin M. Forbes 357a44
@@ -107,8 +123,12 @@ struct SCSIBusInfo {
Justin M. Forbes 357a44
     int tcq;
Justin M. Forbes 357a44
     int max_channel, max_target, max_lun;
Justin M. Forbes 357a44
     void (*transfer_data)(SCSIRequest *req, uint32_t arg);
Justin M. Forbes 357a44
-    void (*complete)(SCSIRequest *req, uint32_t arg);
Justin M. Forbes 357a44
+    void (*complete)(SCSIRequest *req, uint32_t arg, int32_t len);
Justin M. Forbes 357a44
     void (*cancel)(SCSIRequest *req);
Justin M. Forbes 357a44
+    QEMUSGList *(*get_sg_list)(SCSIRequest *req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    void (*save_request)(QEMUFile *f, SCSIRequest *req);
Justin M. Forbes 357a44
+    void *(*load_request)(QEMUFile *f, SCSIRequest *req);
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 struct SCSIBus {
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/spapr_vscsi.c qemu-kvm-1.0.virtio-scsi/hw/spapr_vscsi.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/spapr_vscsi.c	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/spapr_vscsi.c	2012-02-07 14:44:53.430905347 -0600
Justin M. Forbes 357a44
@@ -494,7 +494,7 @@ static void vscsi_transfer_data(SCSIRequ
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 /* Callback to indicate that the SCSI layer has completed a transfer.  */
Justin M. Forbes 357a44
-static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status)
Justin M. Forbes 357a44
+static void vscsi_command_complete(SCSIRequest *sreq, uint32_t status, int32_t resid)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     VSCSIState *s = DO_UPCAST(VSCSIState, vdev.qdev, sreq->bus->qbus.parent);
Justin M. Forbes 357a44
     vscsi_req *req = sreq->hba_private;
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/usb-msd.c qemu-kvm-1.0.virtio-scsi/hw/usb-msd.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/usb-msd.c	2012-02-07 14:44:04.881123501 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/usb-msd.c	2012-02-07 14:44:53.431905363 -0600
Justin M. Forbes 357a44
@@ -223,7 +223,7 @@ static void usb_msd_transfer_data(SCSIRe
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
-static void usb_msd_command_complete(SCSIRequest *req, uint32_t status)
Justin M. Forbes 357a44
+static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, int32_t resid)
Justin M. Forbes 357a44
 {
Justin M. Forbes 357a44
     MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
Justin M. Forbes 357a44
     USBPacket *p = s->packet;
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/virtio.h qemu-kvm-1.0.virtio-scsi/hw/virtio.h
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/virtio.h	2011-12-04 04:38:06.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/virtio.h	2012-02-07 14:44:53.433905395 -0600
Justin M. Forbes 357a44
@@ -199,6 +199,8 @@ VirtIODevice *virtio_net_init(DeviceStat
Justin M. Forbes 357a44
 typedef struct virtio_serial_conf virtio_serial_conf;
Justin M. Forbes 357a44
 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *serial);
Justin M. Forbes 357a44
 VirtIODevice *virtio_balloon_init(DeviceState *dev);
Justin M. Forbes 357a44
+typedef struct VirtIOSCSIConf VirtIOSCSIConf;
Justin M. Forbes 357a44
+VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *conf);
Justin M. Forbes 357a44
 #ifdef CONFIG_LINUX
Justin M. Forbes 357a44
 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);
Justin M. Forbes 357a44
 #endif
Justin M. Forbes 357a44
@@ -208,6 +210,7 @@ void virtio_net_exit(VirtIODevice *vdev)
Justin M. Forbes 357a44
 void virtio_blk_exit(VirtIODevice *vdev);
Justin M. Forbes 357a44
 void virtio_serial_exit(VirtIODevice *vdev);
Justin M. Forbes 357a44
 void virtio_balloon_exit(VirtIODevice *vdev);
Justin M. Forbes 357a44
+void virtio_scsi_exit(VirtIODevice *vdev);
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
Justin M. Forbes 357a44
 	DEFINE_PROP_BIT("indirect_desc", _state, _field, \
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/virtio-pci.c qemu-kvm-1.0.virtio-scsi/hw/virtio-pci.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/virtio-pci.c	2012-02-07 14:44:04.850123002 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/virtio-pci.c	2012-02-07 14:44:53.432905379 -0600
Justin M. Forbes 357a44
@@ -19,6 +19,7 @@
Justin M. Forbes 357a44
 #include "virtio-blk.h"
Justin M. Forbes 357a44
 #include "virtio-net.h"
Justin M. Forbes 357a44
 #include "virtio-serial.h"
Justin M. Forbes 357a44
+#include "virtio-scsi.h"
Justin M. Forbes 357a44
 #include "pci.h"
Justin M. Forbes 357a44
 #include "qemu-error.h"
Justin M. Forbes 357a44
 #include "msix.h"
Justin M. Forbes 357a44
@@ -855,6 +856,32 @@ static int virtio_balloon_exit_pci(PCIDe
Justin M. Forbes 357a44
     return virtio_exit_pci(pci_dev);
Justin M. Forbes 357a44
 }
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
+static int virtio_scsi_init_pci(PCIDevice *pci_dev)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
Justin M. Forbes 357a44
+    VirtIODevice *vdev;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
Justin M. Forbes 357a44
+    if (!vdev) {
Justin M. Forbes 357a44
+        return -EINVAL;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    vdev->nvectors = proxy->nvectors;
Justin M. Forbes 357a44
+    virtio_init_pci(proxy, vdev);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    /* make the actual value visible */
Justin M. Forbes 357a44
+    proxy->nvectors = vdev->nvectors;
Justin M. Forbes 357a44
+    return 0;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static int virtio_scsi_exit_pci(PCIDevice *pci_dev)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    virtio_scsi_exit(proxy->vdev);
Justin M. Forbes 357a44
+    return virtio_exit_pci(pci_dev);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
 static PCIDeviceInfo virtio_info[] = {
Justin M. Forbes 357a44
     {
Justin M. Forbes 357a44
         .qdev.name = "virtio-blk-pci",
Justin M. Forbes 357a44
@@ -940,6 +967,21 @@ static PCIDeviceInfo virtio_info[] = {
Justin M. Forbes 357a44
         },
Justin M. Forbes 357a44
         .qdev.reset = virtio_pci_reset,
Justin M. Forbes 357a44
     },{
Justin M. Forbes 357a44
+        .qdev.name = "virtio-scsi-pci",
Justin M. Forbes 357a44
+        .qdev.alias = "virtio-scsi",
Justin M. Forbes 357a44
+        .qdev.size = sizeof(VirtIOPCIProxy),
Justin M. Forbes 357a44
+        .init      = virtio_scsi_init_pci,
Justin M. Forbes 357a44
+        .exit      = virtio_scsi_exit_pci,
Justin M. Forbes 357a44
+        .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
Justin M. Forbes 357a44
+        .device_id = PCI_DEVICE_ID_VIRTIO_SCSI,
Justin M. Forbes 357a44
+        .class_id  = PCI_CLASS_STORAGE_SCSI,
Justin M. Forbes 357a44
+        .revision  = 0x00,
Justin M. Forbes 357a44
+        .qdev.props = (Property[]) {
Justin M. Forbes 357a44
+            DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
Justin M. Forbes 357a44
+            DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
Justin M. Forbes 357a44
+            DEFINE_PROP_END_OF_LIST(),
Justin M. Forbes 357a44
+        },
Justin M. Forbes 357a44
+    }, {
Justin M. Forbes 357a44
         /* end of list */
Justin M. Forbes 357a44
     }
Justin M. Forbes 357a44
 };
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/virtio-pci.h qemu-kvm-1.0.virtio-scsi/hw/virtio-pci.h
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/virtio-pci.h	2012-02-07 14:44:04.850123002 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/virtio-pci.h	2012-02-07 14:44:53.432905379 -0600
Justin M. Forbes 357a44
@@ -17,6 +17,7 @@
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 #include "virtio-net.h"
Justin M. Forbes 357a44
 #include "virtio-serial.h"
Justin M. Forbes 357a44
+#include "virtio-scsi.h"
Justin M. Forbes 357a44
 
Justin M. Forbes 357a44
 /* Performance improves when virtqueue kick processing is decoupled from the
Justin M. Forbes 357a44
  * vcpu thread using ioeventfd for some devices. */
Justin M. Forbes 357a44
@@ -40,6 +41,7 @@ typedef struct {
Justin M. Forbes 357a44
 #endif
Justin M. Forbes 357a44
     virtio_serial_conf serial;
Justin M. Forbes 357a44
     virtio_net_conf net;
Justin M. Forbes 357a44
+    VirtIOSCSIConf scsi;
Justin M. Forbes 357a44
     bool ioeventfd_disabled;
Justin M. Forbes 357a44
     bool ioeventfd_started;
Justin M. Forbes 357a44
 } VirtIOPCIProxy;
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/virtio-scsi.c qemu-kvm-1.0.virtio-scsi/hw/virtio-scsi.c
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/virtio-scsi.c	1969-12-31 18:00:00.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/virtio-scsi.c	2012-02-07 14:44:53.432905379 -0600
Justin M. Forbes 357a44
@@ -0,0 +1,607 @@
Justin M. Forbes 357a44
+/*
Justin M. Forbes 357a44
+ * Virtio SCSI HBA
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ * Copyright IBM, Corp. 2010
Justin M. Forbes 357a44
+ * Copyright Red Hat, Inc. 2011
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ * Authors:
Justin M. Forbes 357a44
+ *   Stefan Hajnoczi    <stefanha@linux.vnet.ibm.com>
Justin M. Forbes 357a44
+ *   Paolo Bonzini      <pbonzini@redhat.com>
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
Justin M. Forbes 357a44
+ * See the COPYING file in the top-level directory.
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ */
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+#include "virtio-scsi.h"
Justin M. Forbes 357a44
+#include <hw/scsi.h>
Justin M. Forbes 357a44
+#include <hw/scsi-defs.h>
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_VQ_SIZE     128
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_CDB_SIZE    32
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_SENSE_SIZE  96
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_MAX_CHANNEL	0
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_MAX_TARGET  255
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_MAX_LUN     16383
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* Response codes */
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_OK                       0
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_OVERRUN                  1
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_ABORTED                  2
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_BAD_TARGET               3
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_RESET                    4
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_BUSY                     5
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_TRANSPORT_FAILURE        6
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_TARGET_FAILURE           7
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_NEXUS_FAILURE            8
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_FAILURE                  9
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED       10
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_FUNCTION_REJECTED        11
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_S_INCORRECT_LUN            12
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* Controlq type codes.  */
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF                      0
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_AN_QUERY                 1
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_AN_SUBSCRIBE             2
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* Valid TMF subtypes.  */
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_ABORT_TASK           0
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET       1
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_CLEAR_ACA            2
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET       3
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET      4
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET   5
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_QUERY_TASK           6
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET       7
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* Events.  */
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_EVENTS_MISSED            0x80000000
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_NO_EVENT                 0
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_TRANSPORT_RESET          1
Justin M. Forbes 357a44
+#define VIRTIO_SCSI_T_ASYNC_NOTIFY             2
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* SCSI command request, followed by data-out */
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint8_t lun[8];              /* Logical Unit Number */
Justin M. Forbes 357a44
+    uint64_t tag;                /* Command identifier */
Justin M. Forbes 357a44
+    uint8_t task_attr;           /* Task attribute */
Justin M. Forbes 357a44
+    uint8_t prio;
Justin M. Forbes 357a44
+    uint8_t crn;
Justin M. Forbes 357a44
+    uint8_t cdb[];
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSICmdReq;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* Response, followed by sense data and data-in */
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint32_t sense_len;          /* Sense data length */
Justin M. Forbes 357a44
+    uint32_t resid;              /* Residual bytes in data buffer */
Justin M. Forbes 357a44
+    uint16_t status_qualifier;   /* Status qualifier */
Justin M. Forbes 357a44
+    uint8_t status;              /* Command completion status */
Justin M. Forbes 357a44
+    uint8_t response;            /* Response values */
Justin M. Forbes 357a44
+    uint8_t sense[];
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSICmdResp;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* Task Management Request */
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint32_t type;
Justin M. Forbes 357a44
+    uint32_t subtype;
Justin M. Forbes 357a44
+    uint8_t lun[8];
Justin M. Forbes 357a44
+    uint64_t tag;
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSICtrlTMFReq;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint8_t response;
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSICtrlTMFResp;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* Asynchronous notification query/subscription */
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint32_t type;
Justin M. Forbes 357a44
+    uint8_t lun[8];
Justin M. Forbes 357a44
+    uint32_t event_requested;
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSICtrlANReq;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint32_t event_actual;
Justin M. Forbes 357a44
+    uint8_t response;
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSICtrlANResp;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint32_t event;
Justin M. Forbes 357a44
+    uint8_t lun[8];
Justin M. Forbes 357a44
+    uint32_t reason;
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSIEvent;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    uint32_t num_queues;
Justin M. Forbes 357a44
+    uint32_t seg_max;
Justin M. Forbes 357a44
+    uint32_t max_sectors;
Justin M. Forbes 357a44
+    uint32_t cmd_per_lun;
Justin M. Forbes 357a44
+    uint32_t event_info_size;
Justin M. Forbes 357a44
+    uint32_t sense_size;
Justin M. Forbes 357a44
+    uint32_t cdb_size;
Justin M. Forbes 357a44
+    uint16_t max_channel;
Justin M. Forbes 357a44
+    uint16_t max_target;
Justin M. Forbes 357a44
+    uint32_t max_lun;
Justin M. Forbes 357a44
+} QEMU_PACKED VirtIOSCSIConfig;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+typedef struct {
Justin M. Forbes 357a44
+    VirtIODevice vdev;
Justin M. Forbes 357a44
+    DeviceState *qdev;
Justin M. Forbes 357a44
+    VirtIOSCSIConf *conf;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    SCSIBus bus;
Justin M. Forbes 357a44
+    VirtQueue *ctrl_vq;
Justin M. Forbes 357a44
+    VirtQueue *event_vq;
Justin M. Forbes 357a44
+    VirtQueue *cmd_vq;
Justin M. Forbes 357a44
+    uint32_t sense_size;
Justin M. Forbes 357a44
+    uint32_t cdb_size;
Justin M. Forbes 357a44
+    bool resetting;
Justin M. Forbes 357a44
+} VirtIOSCSI;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+typedef struct VirtIOSCSIReq {
Justin M. Forbes 357a44
+    VirtIOSCSI *dev;
Justin M. Forbes 357a44
+    VirtQueue *vq;
Justin M. Forbes 357a44
+    VirtQueueElement elem;
Justin M. Forbes 357a44
+    QEMUSGList qsgl;
Justin M. Forbes 357a44
+    SCSIRequest *sreq;
Justin M. Forbes 357a44
+    union {
Justin M. Forbes 357a44
+        char                  *buf;
Justin M. Forbes 357a44
+        VirtIOSCSICmdReq      *cmd;
Justin M. Forbes 357a44
+        VirtIOSCSICtrlTMFReq  *tmf;
Justin M. Forbes 357a44
+        VirtIOSCSICtrlANReq   *an;
Justin M. Forbes 357a44
+    } req;
Justin M. Forbes 357a44
+    union {
Justin M. Forbes 357a44
+        char                  *buf;
Justin M. Forbes 357a44
+        VirtIOSCSICmdResp     *cmd;
Justin M. Forbes 357a44
+        VirtIOSCSICtrlTMFResp *tmf;
Justin M. Forbes 357a44
+        VirtIOSCSICtrlANResp  *an;
Justin M. Forbes 357a44
+        VirtIOSCSIEvent       *event;
Justin M. Forbes 357a44
+    } resp;
Justin M. Forbes 357a44
+} VirtIOSCSIReq;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static inline int virtio_scsi_get_lun(uint8_t *lun)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    return ((lun[2] << 8) | lun[3]) & 0x3FFF;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    if (lun[0] != 1) {
Justin M. Forbes 357a44
+        return NULL;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
Justin M. Forbes 357a44
+        return NULL;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSI *s = req->dev;
Justin M. Forbes 357a44
+    VirtQueue *vq = req->vq;
Justin M. Forbes 357a44
+    virtqueue_push(vq, &req->elem, req->qsgl.size + req->elem.in_sg[0].iov_len);
Justin M. Forbes 357a44
+    qemu_sglist_destroy(&req->qsgl);
Justin M. Forbes 357a44
+    if (req->sreq) {
Justin M. Forbes 357a44
+        req->sreq->hba_private = NULL;
Justin M. Forbes 357a44
+        scsi_req_unref(req->sreq);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    g_free(req);
Justin M. Forbes 357a44
+    virtio_notify(&s->vdev, vq);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_bad_req(void)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    error_report("wrong size for virtio-scsi headers");
Justin M. Forbes 357a44
+    exit(1);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void qemu_sgl_init_external(QEMUSGList *qsgl, struct iovec *sg,
Justin M. Forbes 357a44
+                                   target_phys_addr_t *addr, int num)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    memset(qsgl, 0, sizeof(*qsgl));
Justin M. Forbes 357a44
+    while (num--) {
Justin M. Forbes 357a44
+        qemu_sglist_add(qsgl, *(addr++), (sg++)->iov_len);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_parse_req(VirtIOSCSI *s, VirtQueue *vq,
Justin M. Forbes 357a44
+                                  VirtIOSCSIReq *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    assert(req->elem.out_num && req->elem.in_num);
Justin M. Forbes 357a44
+    req->vq = vq;
Justin M. Forbes 357a44
+    req->dev = s;
Justin M. Forbes 357a44
+    req->sreq = NULL;
Justin M. Forbes 357a44
+    req->req.buf = req->elem.out_sg[0].iov_base;
Justin M. Forbes 357a44
+    req->resp.buf = req->elem.in_sg[0].iov_base;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    if (req->elem.out_num > 1) {
Justin M. Forbes 357a44
+        qemu_sgl_init_external(&req->qsgl, &req->elem.out_sg[1],
Justin M. Forbes 357a44
+                               &req->elem.out_addr[1],
Justin M. Forbes 357a44
+                               req->elem.out_num - 1);
Justin M. Forbes 357a44
+    } else {
Justin M. Forbes 357a44
+        qemu_sgl_init_external(&req->qsgl, &req->elem.in_sg[1],
Justin M. Forbes 357a44
+                               &req->elem.in_addr[1],
Justin M. Forbes 357a44
+                               req->elem.in_num - 1);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req;
Justin M. Forbes 357a44
+    req = g_malloc(sizeof(*req));
Justin M. Forbes 357a44
+    if (!virtqueue_pop(vq, &req->elem)) {
Justin M. Forbes 357a44
+        g_free(req);
Justin M. Forbes 357a44
+        return NULL;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    virtio_scsi_parse_req(s, vq, req);
Justin M. Forbes 357a44
+    return req;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req = sreq->hba_private;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIBus *bus = sreq->bus;
Justin M. Forbes 357a44
+    VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    req = g_malloc(sizeof(*req));
Justin M. Forbes 357a44
+    qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
Justin M. Forbes 357a44
+    virtio_scsi_parse_req(s, s->cmd_vq, req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    scsi_req_ref(sreq);
Justin M. Forbes 357a44
+    req->sreq = sreq;
Justin M. Forbes 357a44
+    if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
Justin M. Forbes 357a44
+        int req_mode =
Justin M. Forbes 357a44
+            (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+	assert (req->sreq->cmd.mode == req_mode);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    return req;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    SCSIDevice *d = virtio_scsi_device_find(s, req->req.cmd->lun);
Justin M. Forbes 357a44
+    SCSIRequest *r, *next;
Justin M. Forbes 357a44
+    DeviceState *qdev;
Justin M. Forbes 357a44
+    int target;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    switch (req->req.tmf->subtype) {
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_ABORT_TASK:
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_QUERY_TASK:
Justin M. Forbes 357a44
+        d = virtio_scsi_device_find(s, req->req.cmd->lun);
Justin M. Forbes 357a44
+        if (!d) {
Justin M. Forbes 357a44
+            goto fail;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        if (d->lun != virtio_scsi_get_lun(req->req.cmd->lun)) {
Justin M. Forbes 357a44
+            req->resp.tmf->response = VIRTIO_SCSI_S_INCORRECT_LUN;
Justin M. Forbes 357a44
+            break;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
Justin M. Forbes 357a44
+            if (r->tag == req->req.cmd->tag) {
Justin M. Forbes 357a44
+                break;
Justin M. Forbes 357a44
+            }
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        if (r && r->hba_private) {
Justin M. Forbes 357a44
+            if (req->req.tmf->subtype == VIRTIO_SCSI_T_TMF_ABORT_TASK) {
Justin M. Forbes 357a44
+                scsi_req_cancel(r);
Justin M. Forbes 357a44
+            }
Justin M. Forbes 357a44
+            req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
Justin M. Forbes 357a44
+        } else {
Justin M. Forbes 357a44
+            req->resp.tmf->response = VIRTIO_SCSI_S_OK;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        break;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
Justin M. Forbes 357a44
+        d = virtio_scsi_device_find(s, req->req.cmd->lun);
Justin M. Forbes 357a44
+        if (!d) {
Justin M. Forbes 357a44
+            goto fail;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        if (d->lun == virtio_scsi_get_lun(req->req.cmd->lun)) {
Justin M. Forbes 357a44
+            s->resetting++;
Justin M. Forbes 357a44
+            qdev_reset_all(&d->qdev);
Justin M. Forbes 357a44
+            s->resetting--;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        break;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
Justin M. Forbes 357a44
+        d = virtio_scsi_device_find(s, req->req.cmd->lun);
Justin M. Forbes 357a44
+        if (!d) {
Justin M. Forbes 357a44
+            goto fail;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        if (d->lun != virtio_scsi_get_lun(req->req.cmd->lun)) {
Justin M. Forbes 357a44
+            req->resp.tmf->response = VIRTIO_SCSI_S_INCORRECT_LUN;
Justin M. Forbes 357a44
+            break;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        req->resp.tmf->response = VIRTIO_SCSI_S_OK;
Justin M. Forbes 357a44
+        QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
Justin M. Forbes 357a44
+            if (r->hba_private) {
Justin M. Forbes 357a44
+                if (req->req.tmf->subtype != VIRTIO_SCSI_T_TMF_QUERY_TASK) {
Justin M. Forbes 357a44
+                    scsi_req_cancel(r);
Justin M. Forbes 357a44
+                }
Justin M. Forbes 357a44
+                req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
Justin M. Forbes 357a44
+            }
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        break;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
Justin M. Forbes 357a44
+        target = req->req.cmd->lun[1];
Justin M. Forbes 357a44
+        s->resetting++;
Justin M. Forbes 357a44
+        QTAILQ_FOREACH(qdev, &s->bus.qbus.children, sibling) {
Justin M. Forbes 357a44
+             d = DO_UPCAST(SCSIDevice, qdev, qdev);
Justin M. Forbes 357a44
+             if (d->channel == 0 && d->id == target) {
Justin M. Forbes 357a44
+                qdev_reset_all(&d->qdev);
Justin M. Forbes 357a44
+             }
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        s->resetting--;
Justin M. Forbes 357a44
+        break;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
Justin M. Forbes 357a44
+    default:
Justin M. Forbes 357a44
+        req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
Justin M. Forbes 357a44
+        break;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    return;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+fail:
Justin M. Forbes 357a44
+    req->resp.tmf->response = VIRTIO_SCSI_S_BAD_TARGET;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSI *s = (VirtIOSCSI *)vdev;
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    while ((req = virtio_scsi_pop_req(s, vq))) {
Justin M. Forbes 357a44
+        int out_size, in_size;
Justin M. Forbes 357a44
+        if (req->elem.out_num < 1 || req->elem.in_num < 1) {
Justin M. Forbes 357a44
+            virtio_scsi_bad_req();
Justin M. Forbes 357a44
+            continue;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        out_size = req->elem.out_sg[0].iov_len;
Justin M. Forbes 357a44
+        in_size = req->elem.in_sg[0].iov_len;
Justin M. Forbes 357a44
+        if (req->req.tmf->type == VIRTIO_SCSI_T_TMF) {
Justin M. Forbes 357a44
+            if (out_size < sizeof(VirtIOSCSICtrlTMFReq) ||
Justin M. Forbes 357a44
+                in_size < sizeof(VirtIOSCSICtrlTMFResp)) {
Justin M. Forbes 357a44
+                virtio_scsi_bad_req();
Justin M. Forbes 357a44
+            }
Justin M. Forbes 357a44
+            virtio_scsi_do_tmf(s, req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        } else if (req->req.tmf->type == VIRTIO_SCSI_T_AN_QUERY ||
Justin M. Forbes 357a44
+                   req->req.tmf->type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
Justin M. Forbes 357a44
+            if (out_size < sizeof(VirtIOSCSICtrlANReq) ||
Justin M. Forbes 357a44
+                in_size < sizeof(VirtIOSCSICtrlANResp)) {
Justin M. Forbes 357a44
+                virtio_scsi_bad_req();
Justin M. Forbes 357a44
+            }
Justin M. Forbes 357a44
+            req->resp.an->event_actual = 0;
Justin M. Forbes 357a44
+            req->resp.an->response = VIRTIO_SCSI_S_OK;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        virtio_scsi_complete_req(req);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status,
Justin M. Forbes 357a44
+                                         int32_t resid)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req = r->hba_private;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    req->resp.cmd->response = VIRTIO_SCSI_S_OK;
Justin M. Forbes 357a44
+    req->resp.cmd->status = status;
Justin M. Forbes 357a44
+    if (req->resp.cmd->status == GOOD) {
Justin M. Forbes 357a44
+        req->resp.cmd->resid = resid;
Justin M. Forbes 357a44
+    } else {
Justin M. Forbes 357a44
+       req->resp.cmd->resid = 0;
Justin M. Forbes 357a44
+       scsi_req_get_sense(r, req->resp.cmd->sense, VIRTIO_SCSI_SENSE_SIZE);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    virtio_scsi_complete_req(req);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req = r->hba_private;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    return &req->qsgl;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_request_cancelled(SCSIRequest *r)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req = r->hba_private;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    if (!req) {
Justin M. Forbes 357a44
+        return;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    if (req->dev->resetting) {
Justin M. Forbes 357a44
+        req->resp.cmd->response = VIRTIO_SCSI_S_RESET;
Justin M. Forbes 357a44
+    } else {
Justin M. Forbes 357a44
+        req->resp.cmd->response = VIRTIO_SCSI_S_ABORTED;
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+    virtio_scsi_complete_req(req);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    req->resp.cmd->response = VIRTIO_SCSI_S_FAILURE;
Justin M. Forbes 357a44
+    virtio_scsi_complete_req(req);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSI *s = (VirtIOSCSI *)vdev;
Justin M. Forbes 357a44
+    VirtIOSCSIReq *req;
Justin M. Forbes 357a44
+    int n;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    while ((req = virtio_scsi_pop_req(s, vq))) {
Justin M. Forbes 357a44
+        SCSIDevice *d;
Justin M. Forbes 357a44
+        int out_size, in_size;
Justin M. Forbes 357a44
+        if (req->elem.out_num < 1 || req->elem.in_num < 1) {
Justin M. Forbes 357a44
+            virtio_scsi_bad_req();
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        out_size = req->elem.out_sg[0].iov_len;
Justin M. Forbes 357a44
+        in_size = req->elem.in_sg[0].iov_len;
Justin M. Forbes 357a44
+        if (out_size < sizeof(VirtIOSCSICmdReq) + s->cdb_size ||
Justin M. Forbes 357a44
+            in_size < sizeof(VirtIOSCSICmdResp) + s->sense_size) {
Justin M. Forbes 357a44
+            virtio_scsi_bad_req();
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        if (req->elem.out_num > 1 && req->elem.in_num > 1) {
Justin M. Forbes 357a44
+            virtio_scsi_fail_cmd_req(req);
Justin M. Forbes 357a44
+            continue;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        d = virtio_scsi_device_find(s, req->req.cmd->lun);
Justin M. Forbes 357a44
+        if (!d) {
Justin M. Forbes 357a44
+            req->resp.cmd->response = VIRTIO_SCSI_S_BAD_TARGET;
Justin M. Forbes 357a44
+            virtio_scsi_complete_req(req);
Justin M. Forbes 357a44
+            continue;
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+        req->sreq = scsi_req_new(d, req->req.cmd->tag,
Justin M. Forbes 357a44
+                                 virtio_scsi_get_lun(req->req.cmd->lun),
Justin M. Forbes 357a44
+                                 req->req.cmd->cdb, req);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
Justin M. Forbes 357a44
+            int req_mode =
Justin M. Forbes 357a44
+                (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+            if (req->sreq->cmd.mode != req_mode ||
Justin M. Forbes 357a44
+                req->sreq->cmd.xfer > req->qsgl.size) {
Justin M. Forbes 357a44
+                req->resp.cmd->response = VIRTIO_SCSI_S_OVERRUN;
Justin M. Forbes 357a44
+                virtio_scsi_complete_req(req);
Justin M. Forbes 357a44
+                continue;
Justin M. Forbes 357a44
+            }
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+        n = scsi_req_enqueue(req->sreq);
Justin M. Forbes 357a44
+        if (n) {
Justin M. Forbes 357a44
+            scsi_req_continue(req->sreq);
Justin M. Forbes 357a44
+        }
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_get_config(VirtIODevice *vdev,
Justin M. Forbes 357a44
+                                   uint8_t *config)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
Justin M. Forbes 357a44
+    VirtIOSCSI *s = (VirtIOSCSI *)vdev;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->num_queues, s->conf->num_queues);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->seg_max, 128 - 2);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->max_sectors, s->conf->max_sectors);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->cmd_per_lun, s->conf->cmd_per_lun);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->sense_size, s->sense_size);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->cdb_size, s->cdb_size);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
Justin M. Forbes 357a44
+    stl_raw(&scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_set_config(VirtIODevice *vdev,
Justin M. Forbes 357a44
+                                   const uint8_t *config)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
Justin M. Forbes 357a44
+    VirtIOSCSI *s = (VirtIOSCSI *)vdev;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    if ((uint32_t) ldl_raw(&scsiconf->sense_size) >= 65536 ||
Justin M. Forbes 357a44
+        (uint32_t) ldl_raw(&scsiconf->cdb_size) >= 256) {
Justin M. Forbes 357a44
+        error_report("bad data written to virtio-scsi configuration space");
Justin M. Forbes 357a44
+        exit(1);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    s->sense_size = ldl_raw(&scsiconf->sense_size);
Justin M. Forbes 357a44
+    s->cdb_size = ldl_raw(&scsiconf->cdb_size);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static uint32_t virtio_scsi_get_features(VirtIODevice *vdev,
Justin M. Forbes 357a44
+                                         uint32_t requested_features)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    return requested_features;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static void virtio_scsi_reset(VirtIODevice *vdev)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSI *s = (VirtIOSCSI *)vdev;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    s->sense_size = VIRTIO_SCSI_SENSE_SIZE;
Justin M. Forbes 357a44
+    s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* The device does not have anything to save beyond the virtio data.
Justin M. Forbes 357a44
+ * Request data is saved with callbacks from SCSI devices.
Justin M. Forbes 357a44
+ */
Justin M. Forbes 357a44
+static void virtio_scsi_save(QEMUFile *f, void *opaque)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSI *s = opaque;
Justin M. Forbes 357a44
+    virtio_save(&s->vdev, f);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSI *s = opaque;
Justin M. Forbes 357a44
+    virtio_load(&s->vdev, f);
Justin M. Forbes 357a44
+    return 0;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+static struct SCSIBusInfo virtio_scsi_scsi_info = {
Justin M. Forbes 357a44
+    .tcq = true,
Justin M. Forbes 357a44
+    .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
Justin M. Forbes 357a44
+    .max_target = VIRTIO_SCSI_MAX_TARGET,
Justin M. Forbes 357a44
+    .max_lun = VIRTIO_SCSI_MAX_LUN,
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    .complete = virtio_scsi_command_complete,
Justin M. Forbes 357a44
+    .cancel = virtio_scsi_request_cancelled,
Justin M. Forbes 357a44
+    .get_sg_list = virtio_scsi_get_sg_list,
Justin M. Forbes 357a44
+    .save_request = virtio_scsi_save_request,
Justin M. Forbes 357a44
+    .load_request = virtio_scsi_load_request,
Justin M. Forbes 357a44
+};
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    VirtIOSCSI *s;
Justin M. Forbes 357a44
+    static int virtio_scsi_id;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    s = (VirtIOSCSI *)virtio_common_init("virtio-scsi", VIRTIO_ID_SCSI,
Justin M. Forbes 357a44
+                                         sizeof(VirtIOSCSIConfig),
Justin M. Forbes 357a44
+                                         sizeof(VirtIOSCSI));
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    s->qdev = dev;
Justin M. Forbes 357a44
+    s->conf = proxyconf;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    /* TODO set up vdev function pointers */
Justin M. Forbes 357a44
+    s->vdev.get_config = virtio_scsi_get_config;
Justin M. Forbes 357a44
+    s->vdev.set_config = virtio_scsi_set_config;
Justin M. Forbes 357a44
+    s->vdev.get_features = virtio_scsi_get_features;
Justin M. Forbes 357a44
+    s->vdev.reset = virtio_scsi_reset;
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    s->ctrl_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
Justin M. Forbes 357a44
+                                   virtio_scsi_handle_ctrl);
Justin M. Forbes 357a44
+    s->event_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
Justin M. Forbes 357a44
+                                   NULL);
Justin M. Forbes 357a44
+    s->cmd_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
Justin M. Forbes 357a44
+                                   virtio_scsi_handle_cmd);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    scsi_bus_new(&s->bus, dev, &virtio_scsi_scsi_info);
Justin M. Forbes 357a44
+    if (!dev->hotplugged) {
Justin M. Forbes 357a44
+        scsi_bus_legacy_handle_cmdline(&s->bus);
Justin M. Forbes 357a44
+    }
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1,
Justin M. Forbes 357a44
+                    virtio_scsi_save, virtio_scsi_load, s);
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+    return &s->vdev;
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+void virtio_scsi_exit(VirtIODevice *vdev)
Justin M. Forbes 357a44
+{
Justin M. Forbes 357a44
+    virtio_cleanup(vdev);
Justin M. Forbes 357a44
+}
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/hw/virtio-scsi.h qemu-kvm-1.0.virtio-scsi/hw/virtio-scsi.h
Justin M. Forbes 357a44
--- qemu-kvm-1.0/hw/virtio-scsi.h	1969-12-31 18:00:00.000000000 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/hw/virtio-scsi.h	2012-02-07 14:44:53.432905379 -0600
Justin M. Forbes 357a44
@@ -0,0 +1,36 @@
Justin M. Forbes 357a44
+/*
Justin M. Forbes 357a44
+ * Virtio SCSI HBA
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ * Copyright IBM, Corp. 2010
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ * Authors:
Justin M. Forbes 357a44
+ *  Stefan Hajnoczi    <stefanha@linux.vnet.ibm.com>
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
Justin M. Forbes 357a44
+ * the COPYING file in the top-level directory.
Justin M. Forbes 357a44
+ *
Justin M. Forbes 357a44
+ */
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+#ifndef _QEMU_VIRTIO_SCSI_H
Justin M. Forbes 357a44
+#define _QEMU_VIRTIO_SCSI_H
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+#include "virtio.h"
Justin M. Forbes 357a44
+#include "net.h"
Justin M. Forbes 357a44
+#include "pci.h"
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+/* The ID for virtio_scsi */
Justin M. Forbes 357a44
+#define VIRTIO_ID_SCSI  8
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+struct VirtIOSCSIConf {
Justin M. Forbes 357a44
+    uint32_t num_queues;
Justin M. Forbes 357a44
+    uint32_t max_sectors;
Justin M. Forbes 357a44
+    uint32_t cmd_per_lun;
Justin M. Forbes 357a44
+};
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+#define DEFINE_VIRTIO_SCSI_PROPERTIES(_state, _features_field, _conf_field) \
Justin M. Forbes 357a44
+    DEFINE_VIRTIO_COMMON_FEATURES(_state, _features_field), \
Justin M. Forbes 357a44
+    DEFINE_PROP_UINT32("num_queues", _state, _conf_field.num_queues, 1), \
Justin M. Forbes 357a44
+    DEFINE_PROP_UINT32("max_sectors", _state, _conf_field.max_sectors, 0xFFFF), \
Justin M. Forbes 357a44
+    DEFINE_PROP_UINT32("cmd_per_lun", _state, _conf_field.cmd_per_lun, 128)
Justin M. Forbes 357a44
+
Justin M. Forbes 357a44
+#endif /* _QEMU_VIRTIO_SCSI_H */
Justin M. Forbes 357a44
diff -ruNp qemu-kvm-1.0/Makefile.target qemu-kvm-1.0.virtio-scsi/Makefile.target
Justin M. Forbes 357a44
--- qemu-kvm-1.0/Makefile.target	2012-02-07 14:44:04.965124855 -0600
Justin M. Forbes 357a44
+++ qemu-kvm-1.0.virtio-scsi/Makefile.target	2012-02-07 14:44:53.126900450 -0600
Justin M. Forbes 357a44
@@ -205,6 +205,7 @@ obj-y = arch_init.o cpus.o monitor.o mac
Justin M. Forbes 357a44
 obj-$(CONFIG_NO_PCI) += pci-stub.o
Justin M. Forbes 357a44
 obj-$(CONFIG_PCI) += pci.o
Justin M. Forbes 357a44
 obj-$(CONFIG_VIRTIO) += virtio.o virtio-blk.o virtio-balloon.o virtio-net.o virtio-serial-bus.o
Justin M. Forbes 357a44
+obj-$(CONFIG_VIRTIO_SCSI) += virtio-scsi.o
Justin M. Forbes 357a44
 obj-y += vhost_net.o
Justin M. Forbes 357a44
 obj-$(CONFIG_VHOST_NET) += vhost.o
Justin M. Forbes 357a44
 obj-$(CONFIG_REALLY_VIRTFS) += 9pfs/virtio-9p-device.o