]> git.proxmox.com Git - qemu.git/blobdiff - hw/qdev.c
qdev: sysbus_get_default must not return a NULL pointer (fix regression)
[qemu.git] / hw / qdev.c
index 3ce69be47e7a4d68cd1ba068dce6402ac9eb1fdf..6fc9b02a38e3d2ec5f729748aab44fb409fcd64a 100644 (file)
--- a/hw/qdev.c
+++ b/hw/qdev.c
 #include "qdev.h"
 #include "sysemu.h"
 #include "monitor.h"
+#include "blockdev.h"
 
-/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
 static int qdev_hotplug = 0;
 
+/* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
 static BusState *main_system_bus;
 
-static DeviceInfo *device_info_list;
+DeviceInfo *device_info_list;
 
 static BusState *qbus_find_recursive(BusState *bus, const char *name,
                                      const BusInfo *info);
@@ -77,62 +78,60 @@ static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
     return NULL;
 }
 
-/* Create a new device.  This only initializes the device state structure
-   and allows properties to be set.  qdev_init should be called to
-   initialize the actual device emulation.  */
-DeviceState *qdev_create(BusState *bus, const char *name)
+static DeviceState *qdev_create_from_info(BusState *bus, DeviceInfo *info)
 {
-    DeviceInfo *info;
     DeviceState *dev;
 
-    if (!bus) {
-        if (!main_system_bus) {
-            main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
-        }
-        bus = main_system_bus;
-    }
-
-    info = qdev_find_info(bus->info, name);
-    if (!info) {
-        hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
-    }
-
+    assert(bus->info == info->bus_info);
     dev = qemu_mallocz(info->size);
     dev->info = info;
     dev->parent_bus = bus;
     qdev_prop_set_defaults(dev, dev->info->props);
     qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
-    qdev_prop_set_compat(dev);
+    qdev_prop_set_globals(dev);
     QLIST_INSERT_HEAD(&bus->children, dev, sibling);
     if (qdev_hotplug) {
         assert(bus->allow_hotplug);
         dev->hotplugged = 1;
     }
+    dev->instance_id_alias = -1;
     dev->state = DEV_STATE_CREATED;
     return dev;
 }
 
-static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len)
+/* Create a new device.  This only initializes the device state structure
+   and allows properties to be set.  qdev_init should be called to
+   initialize the actual device emulation.  */
+DeviceState *qdev_create(BusState *bus, const char *name)
 {
-    int pos = 0;
-    int ret;
+    DeviceInfo *info;
 
-    ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
-                   info->name, info->bus_info->name);
-    pos += MIN(len-pos,ret);
+    if (!bus) {
+        bus = sysbus_get_default();
+    }
+
+    info = qdev_find_info(bus->info, name);
+    if (!info) {
+        hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
+    }
+
+    return qdev_create_from_info(bus, info);
+}
+
+static void qdev_print_devinfo(DeviceInfo *info)
+{
+    error_printf("name \"%s\", bus %s",
+                 info->name, info->bus_info->name);
     if (info->alias) {
-        ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
-        pos += MIN(len-pos,ret);
+        error_printf(", alias \"%s\"", info->alias);
     }
     if (info->desc) {
-        ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
-        pos += MIN(len-pos,ret);
+        error_printf(", desc \"%s\"", info->desc);
     }
     if (info->no_user) {
-        ret = snprintf(dest+pos, len-pos, ", no-user");
-        pos += MIN(len-pos,ret);
+        error_printf(", no-user");
     }
-    return pos;
+    error_printf("\n");
 }
 
 static int set_property(const char *name, const char *value, void *opaque)
@@ -145,13 +144,52 @@ static int set_property(const char *name, const char *value, void *opaque)
         return 0;
 
     if (qdev_prop_parse(dev, name, value) == -1) {
-        qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
-                   name, value, dev->info->name);
         return -1;
     }
     return 0;
 }
 
+int qdev_device_help(QemuOpts *opts)
+{
+    const char *driver;
+    DeviceInfo *info;
+    Property *prop;
+
+    driver = qemu_opt_get(opts, "driver");
+    if (driver && !strcmp(driver, "?")) {
+        for (info = device_info_list; info != NULL; info = info->next) {
+            if (info->no_user) {
+                continue;       /* not available, don't show */
+            }
+            qdev_print_devinfo(info);
+        }
+        return 1;
+    }
+
+    if (!qemu_opt_get(opts, "?")) {
+        return 0;
+    }
+
+    info = qdev_find_info(NULL, driver);
+    if (!info) {
+        return 0;
+    }
+
+    for (prop = info->props; prop && prop->name; prop++) {
+        /*
+         * TODO Properties without a parser are just for dirty hacks.
+         * qdev_prop_ptr is the only such PropertyInfo.  It's marked
+         * for removal.  This conditional should be removed along with
+         * it.
+         */
+        if (!prop->info->parse) {
+            continue;           /* no way to set it, don't show */
+        }
+        error_printf("%s.%s=%s\n", info->name, prop->name, prop->info->name);
+    }
+    return 1;
+}
+
 DeviceState *qdev_device_add(QemuOpts *opts)
 {
     const char *driver, *path, *id;
@@ -161,28 +199,15 @@ DeviceState *qdev_device_add(QemuOpts *opts)
 
     driver = qemu_opt_get(opts, "driver");
     if (!driver) {
-        qemu_error("-device: no driver specified\n");
-        return NULL;
-    }
-    if (strcmp(driver, "?") == 0) {
-        char msg[256];
-        for (info = device_info_list; info != NULL; info = info->next) {
-            qdev_print_devinfo(info, msg, sizeof(msg));
-            qemu_error("%s\n", msg);
-        }
+        qerror_report(QERR_MISSING_PARAMETER, "driver");
         return NULL;
     }
 
     /* find driver */
     info = qdev_find_info(NULL, driver);
-    if (!info) {
-        qemu_error("Device \"%s\" not found.  Try -device '?' for a list.\n",
-                   driver);
-        return NULL;
-    }
-    if (info->no_user) {
-        qemu_error("device \"%s\" can't be added via command line\n",
-                   info->name);
+    if (!info || info->no_user) {
+        qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "a driver name");
+        error_printf_unless_qmp("Try with argument '?' for a list.\n");
         return NULL;
     }
 
@@ -190,22 +215,29 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     path = qemu_opt_get(opts, "bus");
     if (path != NULL) {
         bus = qbus_find(path);
+        if (!bus) {
+            return NULL;
+        }
+        if (bus->info != info->bus_info) {
+            qerror_report(QERR_BAD_BUS_FOR_DEVICE,
+                           driver, bus->info->name);
+            return NULL;
+        }
     } else {
         bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
-    }
-    if (!bus) {
-        qemu_error("Did not find %s bus for %s\n",
-                   path ? path : info->bus_info->name, info->name);
-        return NULL;
+        if (!bus) {
+            qerror_report(QERR_NO_BUS_FOR_DEVICE,
+                           info->name, info->bus_info->name);
+            return NULL;
+        }
     }
     if (qdev_hotplug && !bus->allow_hotplug) {
-        qemu_error("Bus %s does not support hotplugging\n",
-                   bus->name);
+        qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
         return NULL;
     }
 
     /* create device, set properties */
-    qdev = qdev_create(bus, driver);
+    qdev = qdev_create_from_info(bus, info);
     id = qemu_opts_id(opts);
     if (id) {
         qdev->id = id;
@@ -214,8 +246,8 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         qdev_free(qdev);
         return NULL;
     }
-    if (qdev_init(qdev) != 0) {
-        qdev_free(qdev);
+    if (qdev_init(qdev) < 0) {
+        qerror_report(QERR_DEVICE_INIT_FAILED, driver);
         return NULL;
     }
     qdev->opts = opts;
@@ -224,33 +256,83 @@ DeviceState *qdev_device_add(QemuOpts *opts)
 
 /* Initialize a device.  Device properties should be set before calling
    this function.  IRQs and MMIO regions should be connected/mapped after
-   calling this function.  */
+   calling this function.
+   On failure, destroy the device and return negative value.
+   Return 0 on success.  */
 int qdev_init(DeviceState *dev)
 {
     int rc;
 
     assert(dev->state == DEV_STATE_CREATED);
     rc = dev->info->init(dev, dev->info);
-    if (rc < 0)
+    if (rc < 0) {
+        qdev_free(dev);
         return rc;
-    if (dev->info->reset)
-        qemu_register_reset(dev->info->reset, dev);
-    if (dev->info->vmsd)
-        vmstate_register(-1, dev->info->vmsd, dev);
+    }
+    if (dev->info->vmsd) {
+        vmstate_register_with_alias_id(dev, -1, dev->info->vmsd, dev,
+                                       dev->instance_id_alias,
+                                       dev->alias_required_for_version);
+    }
     dev->state = DEV_STATE_INITIALIZED;
     return 0;
 }
 
+void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
+                                 int required_for_version)
+{
+    assert(dev->state == DEV_STATE_CREATED);
+    dev->instance_id_alias = alias_id;
+    dev->alias_required_for_version = required_for_version;
+}
+
 int qdev_unplug(DeviceState *dev)
 {
     if (!dev->parent_bus->allow_hotplug) {
-        qemu_error("Bus %s does not support hotplugging\n",
-                   dev->parent_bus->name);
+        qerror_report(QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return -1;
     }
+    assert(dev->info->unplug != NULL);
+
     return dev->info->unplug(dev);
 }
 
+static int qdev_reset_one(DeviceState *dev, void *opaque)
+{
+    if (dev->info->reset) {
+        dev->info->reset(dev);
+    }
+
+    return 0;
+}
+
+BusState *sysbus_get_default(void)
+{
+    if (!main_system_bus) {
+        main_system_bus = qbus_create(&system_bus_info, NULL,
+                                      "main-system-bus");
+    }
+    return main_system_bus;
+}
+
+static int qbus_reset_one(BusState *bus, void *opaque)
+{
+    if (bus->info->reset) {
+        return bus->info->reset(bus);
+    }
+    return 0;
+}
+
+void qdev_reset_all(DeviceState *dev)
+{
+    qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
+}
+
+void qbus_reset_all(BusState *bus)
+{
+    qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
+}
+
 /* can be used as ->unplug() callback for the simple cases */
 int qdev_simple_unplug_cb(DeviceState *dev)
 {
@@ -259,28 +341,47 @@ int qdev_simple_unplug_cb(DeviceState *dev)
     return 0;
 }
 
+/* Like qdev_init(), but terminate program via hw_error() instead of
+   returning an error value.  This is okay during machine creation.
+   Don't use for hotplug, because there callers need to recover from
+   failure.  Exception: if you know the device's init() callback can't
+   fail, then qdev_init_nofail() can't fail either, and is therefore
+   usable even then.  But relying on the device implementation that
+   way is somewhat unclean, and best avoided.  */
+void qdev_init_nofail(DeviceState *dev)
+{
+    DeviceInfo *info = dev->info;
+
+    if (qdev_init(dev) < 0) {
+        error_report("Initialization of device %s failed\n", info->name);
+        exit(1);
+    }
+}
+
 /* Unlink device from bus and free the structure.  */
 void qdev_free(DeviceState *dev)
 {
     BusState *bus;
+    Property *prop;
 
     if (dev->state == DEV_STATE_INITIALIZED) {
         while (dev->num_child_bus) {
             bus = QLIST_FIRST(&dev->child_bus);
             qbus_free(bus);
         }
-#if 0 /* FIXME: need sane vmstate_unregister function */
         if (dev->info->vmsd)
-            vmstate_unregister(dev->info->vmsd, dev);
-#endif
-        if (dev->info->reset)
-            qemu_unregister_reset(dev->info->reset, dev);
+            vmstate_unregister(dev, dev->info->vmsd, dev);
         if (dev->info->exit)
             dev->info->exit(dev);
         if (dev->opts)
             qemu_opts_del(dev->opts);
     }
     QLIST_REMOVE(dev, sibling);
+    for (prop = dev->info->props; prop && prop->name; prop++) {
+        if (prop->info->free) {
+            prop->info->free(dev, prop);
+        }
+    }
     qemu_free(dev);
 }
 
@@ -297,13 +398,9 @@ void qdev_machine_creation_done(void)
 CharDriverState *qdev_init_chardev(DeviceState *dev)
 {
     static int next_serial;
-    static int next_virtconsole;
-    /* FIXME: This is a nasty hack that needs to go away.  */
-    if (strncmp(dev->info->name, "virtio", 6) == 0) {
-        return virtcon_hds[next_virtconsole++];
-    } else {
-        return serial_hds[next_serial++];
-    }
+
+    /* FIXME: This function needs to go away: use chardev properties!  */
+    return serial_hds[next_serial++];
 }
 
 BusState *qdev_get_parent_bus(DeviceState *dev)
@@ -337,24 +434,17 @@ void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
     dev->gpio_out[n] = pin;
 }
 
-VLANClientState *qdev_get_vlan_client(DeviceState *dev,
-                                      NetCanReceive *can_receive,
-                                      NetReceive *receive,
-                                      NetReceiveIOV *receive_iov,
-                                      NetCleanup *cleanup,
-                                      void *opaque)
+void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
 {
-    NICInfo *nd = dev->nd;
-    assert(nd);
-    nd->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name, can_receive,
-                                  receive, receive_iov, cleanup, opaque);
-    return nd->vc;
-}
-
-
-void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
-{
-    memcpy(macaddr, dev->nd->macaddr, 6);
+    qdev_prop_set_macaddr(dev, "mac", nd->macaddr);
+    if (nd->vlan)
+        qdev_prop_set_vlan(dev, "vlan", nd->vlan);
+    if (nd->netdev)
+        qdev_prop_set_netdev(dev, "netdev", nd->netdev);
+    if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
+        qdev_prop_exists(dev, "vectors")) {
+        qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
+    }
 }
 
 static int next_block_unit[IF_COUNT];
@@ -383,6 +473,52 @@ BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
     return NULL;
 }
 
+int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
+                       qbus_walkerfn *busfn, void *opaque)
+{
+    DeviceState *dev;
+    int err;
+
+    if (busfn) {
+        err = busfn(bus, opaque);
+        if (err) {
+            return err;
+        }
+    }
+
+    QLIST_FOREACH(dev, &bus->children, sibling) {
+        err = qdev_walk_children(dev, devfn, busfn, opaque);
+        if (err < 0) {
+            return err;
+        }
+    }
+
+    return 0;
+}
+
+int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
+                       qbus_walkerfn *busfn, void *opaque)
+{
+    BusState *bus;
+    int err;
+
+    if (devfn) {
+        err = devfn(dev, opaque);
+        if (err) {
+            return err;
+        }
+    }
+
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        err = qbus_walk_children(bus, devfn, busfn, opaque);
+        if (err < 0) {
+            return err;
+        }
+    }
+
+    return 0;
+}
+
 static BusState *qbus_find_recursive(BusState *bus, const char *name,
                                      const BusInfo *info)
 {
@@ -429,35 +565,33 @@ static DeviceState *qdev_find_recursive(BusState *bus, const char *id)
     return NULL;
 }
 
-static void qbus_list_bus(DeviceState *dev, char *dest, int len)
+static void qbus_list_bus(DeviceState *dev)
 {
     BusState *child;
     const char *sep = " ";
-    int pos = 0;
 
-    pos += snprintf(dest+pos, len-pos,"child busses at \"%s\":",
-                    dev->id ? dev->id : dev->info->name);
+    error_printf("child busses at \"%s\":",
+                 dev->id ? dev->id : dev->info->name);
     QLIST_FOREACH(child, &dev->child_bus, sibling) {
-        pos += snprintf(dest+pos, len-pos, "%s\"%s\"", sep, child->name);
+        error_printf("%s\"%s\"", sep, child->name);
         sep = ", ";
     }
+    error_printf("\n");
 }
 
-static void qbus_list_dev(BusState *bus, char *dest, int len)
+static void qbus_list_dev(BusState *bus)
 {
     DeviceState *dev;
     const char *sep = " ";
-    int pos = 0;
 
-    pos += snprintf(dest+pos, len-pos, "devices at \"%s\":",
-                    bus->name);
+    error_printf("devices at \"%s\":", bus->name);
     QLIST_FOREACH(dev, &bus->children, sibling) {
-        pos += snprintf(dest+pos, len-pos, "%s\"%s\"",
-                        sep, dev->info->name);
+        error_printf("%s\"%s\"", sep, dev->info->name);
         if (dev->id)
-            pos += snprintf(dest+pos, len-pos, "/\"%s\"", dev->id);
+            error_printf("/\"%s\"", dev->id);
         sep = ", ";
     }
+    error_printf("\n");
 }
 
 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
@@ -504,7 +638,7 @@ static BusState *qbus_find(const char *path)
 {
     DeviceState *dev;
     BusState *bus;
-    char elem[128], msg[256];
+    char elem[128];
     int pos, len;
 
     /* find start element */
@@ -513,62 +647,75 @@ static BusState *qbus_find(const char *path)
         pos = 0;
     } else {
         if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\")\n", path);
-            return NULL;
+            assert(!path[0]);
+            elem[0] = len = 0;
         }
         bus = qbus_find_recursive(main_system_bus, elem, NULL);
         if (!bus) {
-            qemu_error("bus \"%s\" not found\n", elem);
+            qerror_report(QERR_BUS_NOT_FOUND, elem);
             return NULL;
         }
         pos = len;
     }
 
     for (;;) {
+        assert(path[pos] == '/' || !path[pos]);
+        while (path[pos] == '/') {
+            pos++;
+        }
         if (path[pos] == '\0') {
-            /* we are done */
             return bus;
         }
 
         /* find device */
-        if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
-            return NULL;
+        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
+            assert(0);
+            elem[0] = len = 0;
         }
         pos += len;
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
-            qbus_list_dev(bus, msg, sizeof(msg));
-            qemu_error("device \"%s\" not found\n%s\n", elem, msg);
+            qerror_report(QERR_DEVICE_NOT_FOUND, elem);
+            if (!monitor_cur_is_qmp()) {
+                qbus_list_dev(bus);
+            }
             return NULL;
         }
+
+        assert(path[pos] == '/' || !path[pos]);
+        while (path[pos] == '/') {
+            pos++;
+        }
         if (path[pos] == '\0') {
             /* last specified element is a device.  If it has exactly
              * one child bus accept it nevertheless */
             switch (dev->num_child_bus) {
             case 0:
-                qemu_error("device has no child bus (%s)\n", path);
+                qerror_report(QERR_DEVICE_NO_BUS, elem);
                 return NULL;
             case 1:
                 return QLIST_FIRST(&dev->child_bus);
             default:
-                qbus_list_bus(dev, msg, sizeof(msg));
-                qemu_error("device has multiple child busses (%s)\n%s\n",
-                           path, msg);
+                qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
+                if (!monitor_cur_is_qmp()) {
+                    qbus_list_bus(dev);
+                }
                 return NULL;
             }
         }
 
         /* find bus */
-        if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
-            qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
-            return NULL;
+        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
+            assert(0);
+            elem[0] = len = 0;
         }
         pos += len;
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
-            qbus_list_bus(dev, msg, sizeof(msg));
-            qemu_error("child bus \"%s\" not found\n%s\n", elem, msg);
+            qerror_report(QERR_BUS_NOT_FOUND, elem);
+            if (!monitor_cur_is_qmp()) {
+                qbus_list_bus(dev);
+            }
             return NULL;
         }
     }
@@ -632,6 +779,7 @@ void qbus_free(BusState *bus)
         QLIST_REMOVE(bus, sibling);
         bus->parent->num_child_bus--;
     }
+    qemu_free((void*)bus->name);
     if (bus->qdev_allocated) {
         qemu_free(bus);
     }
@@ -648,6 +796,12 @@ static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
     if (!props)
         return;
     while (props->name) {
+        /*
+         * TODO Properties without a print method are just for dirty
+         * hacks.  qdev_prop_ptr is the only such PropertyInfo.  It's
+         * marked for removal.  The test props->info->print should be
+         * removed along with it.
+         */
         if (props->info->print) {
             props->info->print(dev, props, buf, sizeof(buf));
             qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
@@ -699,33 +853,72 @@ void do_info_qtree(Monitor *mon)
 void do_info_qdm(Monitor *mon)
 {
     DeviceInfo *info;
-    char msg[256];
 
     for (info = device_info_list; info != NULL; info = info->next) {
-        qdev_print_devinfo(info, msg, sizeof(msg));
-        monitor_printf(mon, "%s\n", msg);
+        qdev_print_devinfo(info);
     }
 }
 
-void do_device_add(Monitor *mon, const QDict *qdict)
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     QemuOpts *opts;
 
-    opts = qemu_opts_parse(&qemu_device_opts,
-                           qdict_get_str(qdict, "config"), "driver");
-    if (opts)
-        qdev_device_add(opts);
+    opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
+    if (!opts) {
+        return -1;
+    }
+    if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
+        qemu_opts_del(opts);
+        return 0;
+    }
+    if (!qdev_device_add(opts)) {
+        qemu_opts_del(opts);
+        return -1;
+    }
+    return 0;
 }
 
-void do_device_del(Monitor *mon, const QDict *qdict)
+int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     const char *id = qdict_get_str(qdict, "id");
     DeviceState *dev;
 
     dev = qdev_find_recursive(main_system_bus, id);
     if (NULL == dev) {
-        qemu_error("Device '%s' not found\n", id);
-        return;
+        qerror_report(QERR_DEVICE_NOT_FOUND, id);
+        return -1;
     }
-    qdev_unplug(dev);
+    return qdev_unplug(dev);
+}
+
+static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
+{
+    int l = 0;
+
+    if (dev && dev->parent_bus) {
+        char *d;
+        l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
+        if (dev->parent_bus->info->get_fw_dev_path) {
+            d = dev->parent_bus->info->get_fw_dev_path(dev);
+            l += snprintf(p + l, size - l, "%s", d);
+            qemu_free(d);
+        } else {
+            l += snprintf(p + l, size - l, "%s", dev->info->name);
+        }
+    }
+    l += snprintf(p + l , size - l, "/");
+
+    return l;
+}
+
+char* qdev_get_fw_dev_path(DeviceState *dev)
+{
+    char path[128];
+    int l;
+
+    l = qdev_get_fw_dev_path_helper(dev, path, 128);
+
+    path[l-1] = '\0';
+
+    return strdup(path);
 }