This allows to specify a bus or target ID as the scsi-id, and
automatically creates all the missing chains down to the LUN level.
The given scsi-id is used for the first level; subsequent levels
use id 0 as it should be for backward compatibility.

Having to parse the number manually kind of sucks. :(  Unfortunately
qdev properties will not be parsed until after the device has been
created.  The right way to fix it is to wait for the QCFG fairies,
or to otherwise make qdev use QObject rather than QemuOpts as the
basis for its parsing.  Not something I am going to do soon anyway.

Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
---
 hw/scsi-bus.c |   45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 44 insertions(+), 1 deletions(-)

diff --git a/hw/scsi-bus.c b/hw/scsi-bus.c
index 43a1cd7..277bcc0 100644
--- a/hw/scsi-bus.c
+++ b/hw/scsi-bus.c
@@ -7,11 +7,14 @@
 #include "trace.h"
 
 static char *scsibus_get_fw_dev_path(DeviceState *dev);
+static BusState *scsibus_realize_topology(BusState *qbus, DeviceInfo *base,
+                                         QemuOpts *opts);
 
 static struct BusInfo scsi_bus_info = {
     .name  = "SCSI",
     .size  = sizeof(SCSIBus),
-    .get_fw_dev_path = scsibus_get_fw_dev_path,
+    .get_fw_dev_path  = scsibus_get_fw_dev_path,
+    .realize_topology = scsibus_realize_topology,
     .props = (Property[]) {
         DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
         DEFINE_PROP_END_OF_LIST(),
@@ -32,6 +35,46 @@ void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, 
int ndev,
     bus->qbus.allow_hotplug = 1;
 }
 
+static BusState *scsibus_realize_topology(BusState *qbus, DeviceInfo *base,
+                                         QemuOpts *opts)
+{
+    SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
+    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
+    const char *scsi_id_str;
+    int scsi_id;
+    DeviceState *qdev;
+    SCSIDevice *dev;
+
+    scsi_id_str = qemu_opt_get(opts, "scsi-id");
+    if (scsi_id_str) {
+        char *end;
+       scsi_id = strtoul(scsi_id_str, &end, 0);
+       if ((*end != '\0') || (end == scsi_id_str)) {
+            /* We'll fail when parsing the property.  */
+            return &bus->qbus;
+       }
+    } else {
+       scsi_id = -1;
+    }
+
+    if (bus->level >= info->level - 1) {
+        return &bus->qbus;
+    }
+    if (bus->level == SCSI_PATH) {
+        qdev = qdev_create(&bus->qbus, "scsi-target");
+    } else {
+        qdev = qdev_create(&bus->qbus, "scsi-path");
+    }
+    if (scsi_id != -1) {
+        qdev_prop_set_uint32(qdev, "scsi-id", scsi_id);
+        qemu_opt_set(opts, "scsi-id", "0");
+    }
+
+    qdev_init_nofail(qdev);
+    dev = DO_UPCAST(SCSIDevice, qdev, qdev);
+    return &dev->children->qbus;
+}
+
 static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
 {
     SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
-- 
1.7.4.4



Reply via email to