From 06d3dff0723c712a4b109ced4243edf49ef850af Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Fri, 20 Sep 2013 13:31:39 +0200 Subject: virtio-bus: remove vdev field The vdev field is complicated to synchronize. Just access the BusState's list of children. Cc: qemu-stable@nongnu.org Acked-by: Andreas Faerber Signed-off-by: Paolo Bonzini --- include/hw/virtio/virtio-bus.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'include/hw') diff --git a/include/hw/virtio/virtio-bus.h b/include/hw/virtio/virtio-bus.h index 9217f85abc..ba0f86abf1 100644 --- a/include/hw/virtio/virtio-bus.h +++ b/include/hw/virtio/virtio-bus.h @@ -72,10 +72,6 @@ typedef struct VirtioBusClass { struct VirtioBusState { BusState parent_obj; - /* - * Only one VirtIODevice can be plugged on the bus. - */ - VirtIODevice *vdev; }; int virtio_bus_plug_device(VirtIODevice *vdev); @@ -98,4 +94,16 @@ void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config); /* Set config of the plugged device. */ void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config); +static inline VirtIODevice *virtio_bus_get_device(VirtioBusState *bus) +{ + BusState *qbus = &bus->parent_obj; + BusChild *kid = QTAILQ_FIRST(&qbus->children); + DeviceState *qdev = kid ? kid->child : NULL; + + /* This is used on the data path, the cast is guaranteed + * to succeed by the qdev machinery. + */ + return (VirtIODevice *)qdev; +} + #endif /* VIRTIO_BUS_H */ -- cgit v1.2.3-55-g7522 From 5e96f5d2f8d2696ef7d2d8d7282c18fa6023470b Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Fri, 20 Sep 2013 13:59:08 +0200 Subject: virtio-bus: cleanup plug/unplug interface Right now we have these pairs: - virtio_bus_plug_device/virtio_bus_destroy_device. The first takes a VirtIODevice, the second takes a VirtioBusState - device_plugged/device_unplug callbacks in the VirtioBusClass (here it's just the naming that is inconsistent) - virtio_bus_destroy_device is not called by anyone (and since it calls qdev_free, it would be called by the proxies---but then the callback is useless since the proxies can do whatever they want before calling virtio_bus_destroy_device) And there is a k->init but no k->exit, hence virtio_device_exit is overwritten by subclasses (except virtio-9p). This cleans it up by: - renaming the device_unplug callback to device_unplugged - renaming virtio_bus_plug_device to virtio_bus_device_plugged, matching the callback name - renaming virtio_bus_destroy_device to virtio_bus_device_unplugged, removing the qdev_free, making it take a VirtIODevice and calling it from virtio_device_exit - adding a k->exit callback virtio_device_exit is still overwritten, the next patches will fix that. Cc: qemu-stable@nongnu.org Acked-by: Andreas Faerber Signed-off-by: Paolo Bonzini --- hw/virtio/virtio-bus.c | 19 +++++++++---------- hw/virtio/virtio.c | 7 ++++++- include/hw/virtio/virtio-bus.h | 6 +++--- include/hw/virtio/virtio.h | 1 + 4 files changed, 19 insertions(+), 14 deletions(-) (limited to 'include/hw') diff --git a/hw/virtio/virtio-bus.c b/hw/virtio/virtio-bus.c index 17dd06e1a1..eb77019267 100644 --- a/hw/virtio/virtio-bus.c +++ b/hw/virtio/virtio-bus.c @@ -37,8 +37,8 @@ do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0) #define DPRINTF(fmt, ...) do { } while (0) #endif -/* Plug the VirtIODevice */ -int virtio_bus_plug_device(VirtIODevice *vdev) +/* A VirtIODevice is being plugged */ +int virtio_bus_device_plugged(VirtIODevice *vdev) { DeviceState *qdev = DEVICE(vdev); BusState *qbus = BUS(qdev_get_parent_bus(qdev)); @@ -64,20 +64,19 @@ void virtio_bus_reset(VirtioBusState *bus) } } -/* Destroy the VirtIODevice */ -void virtio_bus_destroy_device(VirtioBusState *bus) +/* A VirtIODevice is being unplugged */ +void virtio_bus_device_unplugged(VirtIODevice *vdev) { - BusState *qbus = BUS(bus); - VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus); - VirtIODevice *vdev = virtio_bus_get_device(bus); + DeviceState *qdev = DEVICE(vdev); + BusState *qbus = BUS(qdev_get_parent_bus(qdev)); + VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus); DPRINTF("%s: remove device.\n", qbus->name); if (vdev != NULL) { - if (klass->device_unplug != NULL) { - klass->device_unplug(qbus->parent); + if (klass->device_unplugged != NULL) { + klass->device_unplugged(qbus->parent); } - object_unparent(OBJECT(vdev)); } } diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 2f1e73bc75..965b2c0233 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1158,14 +1158,19 @@ static int virtio_device_init(DeviceState *qdev) if (k->init(vdev) < 0) { return -1; } - virtio_bus_plug_device(vdev); + virtio_bus_device_plugged(vdev); return 0; } static int virtio_device_exit(DeviceState *qdev) { VirtIODevice *vdev = VIRTIO_DEVICE(qdev); + VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(qdev); + virtio_bus_device_unplugged(vdev); + if (k->exit) { + k->exit(vdev); + } if (vdev->bus_name) { g_free(vdev->bus_name); vdev->bus_name = NULL; diff --git a/include/hw/virtio/virtio-bus.h b/include/hw/virtio/virtio-bus.h index ba0f86abf1..0756545d4d 100644 --- a/include/hw/virtio/virtio-bus.h +++ b/include/hw/virtio/virtio-bus.h @@ -61,7 +61,7 @@ typedef struct VirtioBusClass { * transport independent exit function. * This is called by virtio-bus just before the device is unplugged. */ - void (*device_unplug)(DeviceState *d); + void (*device_unplugged)(DeviceState *d); /* * Does the transport have variable vring alignment? * (ie can it ever call virtio_queue_set_align()?) @@ -74,9 +74,9 @@ struct VirtioBusState { BusState parent_obj; }; -int virtio_bus_plug_device(VirtIODevice *vdev); +int virtio_bus_device_plugged(VirtIODevice *vdev); void virtio_bus_reset(VirtioBusState *bus); -void virtio_bus_destroy_device(VirtioBusState *bus); +void virtio_bus_device_unplugged(VirtIODevice *bus); /* Get the device id of the plugged device. */ uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus); /* Get the config_len field of the plugged device. */ diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index a90522d6d6..59756c29b9 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -127,6 +127,7 @@ typedef struct VirtioDeviceClass { /* This is what a VirtioDevice must implement */ DeviceClass parent; int (*init)(VirtIODevice *vdev); + void (*exit)(VirtIODevice *vdev); uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features); uint32_t (*bad_features)(VirtIODevice *vdev); void (*set_features)(VirtIODevice *vdev, uint32_t val); -- cgit v1.2.3-55-g7522 From e3c9d76acc984218264bbc6435b0c09f959ed9b8 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Fri, 20 Sep 2013 14:06:08 +0200 Subject: virtio-scsi: switch exit callback to VirtioDeviceClass This ensures hot-unplug is handled properly by the proxy, and avoids leaking bus_name which is freed by virtio_device_exit. Cc: qemu-stable@nongnu.org Acked-by: Andreas Faerber Signed-off-by: Paolo Bonzini --- hw/scsi/vhost-scsi.c | 11 +++++------ hw/scsi/virtio-scsi.c | 15 +++++++-------- include/hw/virtio/virtio-scsi.h | 2 +- 3 files changed, 13 insertions(+), 15 deletions(-) (limited to 'include/hw') diff --git a/hw/scsi/vhost-scsi.c b/hw/scsi/vhost-scsi.c index 9e770fba98..5e3cc614c9 100644 --- a/hw/scsi/vhost-scsi.c +++ b/hw/scsi/vhost-scsi.c @@ -240,11 +240,10 @@ static int vhost_scsi_init(VirtIODevice *vdev) return 0; } -static int vhost_scsi_exit(DeviceState *qdev) +static void vhost_scsi_exit(VirtIODevice *vdev) { - VirtIODevice *vdev = VIRTIO_DEVICE(qdev); - VHostSCSI *s = VHOST_SCSI(qdev); - VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(qdev); + VHostSCSI *s = VHOST_SCSI(vdev); + VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); migrate_del_blocker(s->migration_blocker); error_free(s->migration_blocker); @@ -253,7 +252,7 @@ static int vhost_scsi_exit(DeviceState *qdev) vhost_scsi_set_status(vdev, 0); g_free(s->dev.vqs); - return virtio_scsi_common_exit(vs); + virtio_scsi_common_exit(vs); } static Property vhost_scsi_properties[] = { @@ -265,10 +264,10 @@ static void vhost_scsi_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); - dc->exit = vhost_scsi_exit; dc->props = vhost_scsi_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); vdc->init = vhost_scsi_init; + vdc->exit = vhost_scsi_exit; vdc->get_features = vhost_scsi_get_features; vdc->set_config = vhost_scsi_set_config; vdc->set_status = vhost_scsi_set_status; diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 26d95a14ec..83344eacc0 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -644,22 +644,21 @@ static int virtio_scsi_device_init(VirtIODevice *vdev) return 0; } -int virtio_scsi_common_exit(VirtIOSCSICommon *vs) +void virtio_scsi_common_exit(VirtIOSCSICommon *vs) { VirtIODevice *vdev = VIRTIO_DEVICE(vs); g_free(vs->cmd_vqs); virtio_cleanup(vdev); - return 0; } -static int virtio_scsi_device_exit(DeviceState *qdev) +static void virtio_scsi_device_exit(VirtIODevice *vdev) { - VirtIOSCSI *s = VIRTIO_SCSI(qdev); - VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(qdev); + VirtIOSCSI *s = VIRTIO_SCSI(vdev); + VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); - unregister_savevm(qdev, "virtio-scsi", s); - return virtio_scsi_common_exit(vs); + unregister_savevm(DEVICE(vdev), "virtio-scsi", s); + virtio_scsi_common_exit(vs); } static Property virtio_scsi_properties[] = { @@ -680,10 +679,10 @@ static void virtio_scsi_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); - dc->exit = virtio_scsi_device_exit; dc->props = virtio_scsi_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); vdc->init = virtio_scsi_device_init; + vdc->exit = virtio_scsi_device_exit; vdc->set_config = virtio_scsi_set_config; vdc->get_features = virtio_scsi_get_features; vdc->reset = virtio_scsi_reset; diff --git a/include/hw/virtio/virtio-scsi.h b/include/hw/virtio/virtio-scsi.h index 9a985403c2..206c61dbfd 100644 --- a/include/hw/virtio/virtio-scsi.h +++ b/include/hw/virtio/virtio-scsi.h @@ -187,6 +187,6 @@ typedef struct { VIRTIO_SCSI_F_CHANGE, true) int virtio_scsi_common_init(VirtIOSCSICommon *vs); -int virtio_scsi_common_exit(VirtIOSCSICommon *vs); +void virtio_scsi_common_exit(VirtIOSCSICommon *vs); #endif /* _QEMU_VIRTIO_SCSI_H */ -- cgit v1.2.3-55-g7522 From 1d244b42d200c02ad60eb564c75d8adea9243366 Mon Sep 17 00:00:00 2001 From: Andreas Färber Date: Tue, 30 Jul 2013 00:50:27 +0200 Subject: virtio: Start converting VirtioDevice to QOM realize Temporarily allow either VirtioDeviceClass::init or VirtioDeviceClass::realize. Introduce VirtioDeviceClass::unrealize for symmetry. Signed-off-by: Andreas Färber Signed-off-by: Paolo Bonzini --- hw/virtio/virtio.c | 42 +++++++++++++++++++++++++++--------------- include/hw/virtio/virtio.h | 4 +++- 2 files changed, 30 insertions(+), 16 deletions(-) (limited to 'include/hw') diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 965b2c0233..4070b37d64 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1150,40 +1150,52 @@ void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name) } } -static int virtio_device_init(DeviceState *qdev) -{ - VirtIODevice *vdev = VIRTIO_DEVICE(qdev); - VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(qdev); - assert(k->init != NULL); - if (k->init(vdev) < 0) { - return -1; +static void virtio_device_realize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); + Error *err = NULL; + + assert(vdc->init != NULL || vdc->realize != NULL); + if (vdc->realize != NULL) { + vdc->realize(dev, &err); + if (err != NULL) { + error_propagate(errp, err); + return; + } + } else { + if (vdc->init(vdev) < 0) { + error_setg(errp, "Device initialization failed."); + return; + } } virtio_bus_device_plugged(vdev); - return 0; } -static int virtio_device_exit(DeviceState *qdev) +static void virtio_device_unrealize(DeviceState *dev, Error **errp) { - VirtIODevice *vdev = VIRTIO_DEVICE(qdev); - VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(qdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(dev); virtio_bus_device_unplugged(vdev); - if (k->exit) { + + if (k->exit != NULL) { k->exit(vdev); } + if (vdev->bus_name) { g_free(vdev->bus_name); vdev->bus_name = NULL; } - return 0; } static void virtio_device_class_init(ObjectClass *klass, void *data) { /* Set the default value here. */ DeviceClass *dc = DEVICE_CLASS(klass); - dc->init = virtio_device_init; - dc->exit = virtio_device_exit; + + dc->realize = virtio_device_realize; + dc->unrealize = virtio_device_unrealize; dc->bus_type = TYPE_VIRTIO_BUS; } diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 59756c29b9..54c5efe4d0 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -124,10 +124,12 @@ struct VirtIODevice }; typedef struct VirtioDeviceClass { - /* This is what a VirtioDevice must implement */ DeviceClass parent; + + /* This is what a VirtioDevice must implement */ int (*init)(VirtIODevice *vdev); void (*exit)(VirtIODevice *vdev); + DeviceRealize realize; uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features); uint32_t (*bad_features)(VirtIODevice *vdev); void (*set_features)(VirtIODevice *vdev, uint32_t val); -- cgit v1.2.3-55-g7522 From a8d57dfb28bd8fd8ebddf08d0cfafdcb61a764fb Mon Sep 17 00:00:00 2001 From: Andreas Färber Date: Tue, 30 Jul 2013 02:57:37 +0200 Subject: virtio-rng: Convert to QOM realize Signed-off-by: Andreas Färber Signed-off-by: Paolo Bonzini --- hw/virtio/virtio-rng.c | 24 +++++++++++------------- include/hw/virtio/virtio-rng.h | 2 ++ 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'include/hw') diff --git a/hw/virtio/virtio-rng.c b/hw/virtio/virtio-rng.c index 9e2a66a635..fd61cb2edf 100644 --- a/hw/virtio/virtio-rng.c +++ b/hw/virtio/virtio-rng.c @@ -133,16 +133,16 @@ static void check_rate_limit(void *opaque) qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms); } -static int virtio_rng_device_init(VirtIODevice *vdev) +static void virtio_rng_device_realize(DeviceState *dev, Error **errp) { - DeviceState *dev = DEVICE(vdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); VirtIORNG *vrng = VIRTIO_RNG(dev); Error *local_err = NULL; if (!vrng->conf.period_ms > 0) { - qerror_report(QERR_INVALID_PARAMETER_VALUE, "period", - "a positive number"); - return -1; + error_set(errp, QERR_INVALID_PARAMETER_VALUE, "period", + "a positive number"); + return; } if (vrng->conf.rng == NULL) { @@ -162,15 +162,14 @@ static int virtio_rng_device_init(VirtIODevice *vdev) vrng->rng = vrng->conf.rng; if (vrng->rng == NULL) { - qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object"); - return -1; + error_set(errp, QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object"); + return; } rng_backend_open(vrng->rng, &local_err); if (local_err) { - qerror_report_err(local_err); - error_free(local_err); - return -1; + error_propagate(errp, local_err); + return; } vrng->vq = virtio_add_queue(vdev, 8, handle_input); @@ -186,8 +185,6 @@ static int virtio_rng_device_init(VirtIODevice *vdev) register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save, virtio_rng_load, vrng); - - return 0; } static void virtio_rng_device_exit(VirtIODevice *vdev) @@ -209,9 +206,10 @@ static void virtio_rng_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + dc->props = virtio_rng_properties; set_bit(DEVICE_CATEGORY_MISC, dc->categories); - vdc->init = virtio_rng_device_init; + vdc->realize = virtio_rng_device_realize; vdc->exit = virtio_rng_device_exit; vdc->get_features = get_features; } diff --git a/include/hw/virtio/virtio-rng.h b/include/hw/virtio/virtio-rng.h index debaa15d5a..14e85a5a3a 100644 --- a/include/hw/virtio/virtio-rng.h +++ b/include/hw/virtio/virtio-rng.h @@ -18,6 +18,8 @@ #define TYPE_VIRTIO_RNG "virtio-rng-device" #define VIRTIO_RNG(obj) \ OBJECT_CHECK(VirtIORNG, (obj), TYPE_VIRTIO_RNG) +#define VIRTIO_RNG_GET_PARENT_CLASS(obj) \ + OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_RNG) /* The Virtio ID for the virtio rng device */ #define VIRTIO_ID_RNG 4 -- cgit v1.2.3-55-g7522 From 71a6520b83414b4ebe3ecfdee3dc3a70db98c91f Mon Sep 17 00:00:00 2001 From: Andreas Färber Date: Tue, 30 Jul 2013 03:19:55 +0200 Subject: virtio-scsi: Convert to QOM realize Signed-off-by: Andreas Färber Signed-off-by: Paolo Bonzini --- hw/scsi/vhost-scsi.c | 33 +++++++++++++++++---------------- hw/scsi/virtio-scsi.c | 32 +++++++++++++++----------------- include/hw/virtio/virtio-scsi.h | 2 +- 3 files changed, 33 insertions(+), 34 deletions(-) (limited to 'include/hw') diff --git a/hw/scsi/vhost-scsi.c b/hw/scsi/vhost-scsi.c index 5e3cc614c9..1f1c9f3421 100644 --- a/hw/scsi/vhost-scsi.c +++ b/hw/scsi/vhost-scsi.c @@ -196,29 +196,31 @@ static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val) } } -static int vhost_scsi_init(VirtIODevice *vdev) +static void vhost_scsi_realize(DeviceState *dev, Error **errp) { - VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); - VHostSCSI *s = VHOST_SCSI(vdev); + VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); + VHostSCSI *s = VHOST_SCSI(dev); + Error *err = NULL; int vhostfd = -1; int ret; if (!vs->conf.wwpn) { - error_report("vhost-scsi: missing wwpn\n"); - return -EINVAL; + error_setg(errp, "vhost-scsi: missing wwpn"); + return; } if (vs->conf.vhostfd) { vhostfd = monitor_handle_fd_param(cur_mon, vs->conf.vhostfd); if (vhostfd == -1) { - error_report("vhost-scsi: unable to parse vhostfd\n"); - return -EINVAL; + error_setg(errp, "vhost-scsi: unable to parse vhostfd"); + return; } } - ret = virtio_scsi_common_init(vs); - if (ret < 0) { - return ret; + virtio_scsi_common_realize(dev, &err); + if (err != NULL) { + error_propagate(errp, err); + return; } s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; @@ -227,17 +229,15 @@ static int vhost_scsi_init(VirtIODevice *vdev) ret = vhost_dev_init(&s->dev, vhostfd, "/dev/vhost-scsi", true); if (ret < 0) { - error_report("vhost-scsi: vhost initialization failed: %s\n", - strerror(-ret)); - return ret; + error_setg(errp, "vhost-scsi: vhost initialization failed: %s", + strerror(-ret)); + return; } s->dev.backend_features = 0; error_setg(&s->migration_blocker, "vhost-scsi does not support migration"); migrate_add_blocker(s->migration_blocker); - - return 0; } static void vhost_scsi_exit(VirtIODevice *vdev) @@ -264,9 +264,10 @@ static void vhost_scsi_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + dc->props = vhost_scsi_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); - vdc->init = vhost_scsi_init; + vdc->realize = vhost_scsi_realize; vdc->exit = vhost_scsi_exit; vdc->get_features = vhost_scsi_get_features; vdc->set_config = vhost_scsi_set_config; diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index a5b957d3c3..0a41ddb6c1 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -589,9 +589,10 @@ static struct SCSIBusInfo virtio_scsi_scsi_info = { .load_request = virtio_scsi_load_request, }; -int virtio_scsi_common_init(VirtIOSCSICommon *s) +void virtio_scsi_common_realize(DeviceState *dev, Error **errp) { - VirtIODevice *vdev = VIRTIO_DEVICE(s); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); int i; virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, @@ -609,22 +610,19 @@ int virtio_scsi_common_init(VirtIOSCSICommon *s) s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, virtio_scsi_handle_cmd); } - - return 0; } -static int virtio_scsi_device_init(VirtIODevice *vdev) +static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) { - DeviceState *dev = DEVICE(vdev); - VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); VirtIOSCSI *s = VIRTIO_SCSI(dev); static int virtio_scsi_id; Error *err = NULL; - int ret; - ret = virtio_scsi_common_init(vs); - if (ret < 0) { - return ret; + virtio_scsi_common_realize(dev, &err); + if (err != NULL) { + error_propagate(errp, err); + return; } scsi_bus_new(&s->bus, sizeof(s->bus), dev, @@ -633,20 +631,19 @@ static int virtio_scsi_device_init(VirtIODevice *vdev) if (!dev->hotplugged) { scsi_bus_legacy_handle_cmdline(&s->bus, &err); if (err != NULL) { - error_free(err); - return -1; + error_propagate(errp, err); + return; } } register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1, virtio_scsi_save, virtio_scsi_load, s); - - return 0; } void virtio_scsi_common_exit(VirtIOSCSICommon *vs) { - VirtIODevice *vdev = VIRTIO_DEVICE(vs); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); g_free(vs->cmd_vqs); virtio_cleanup(vdev); @@ -679,9 +676,10 @@ static void virtio_scsi_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + dc->props = virtio_scsi_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); - vdc->init = virtio_scsi_device_init; + vdc->realize = virtio_scsi_device_realize; vdc->exit = virtio_scsi_device_exit; vdc->set_config = virtio_scsi_set_config; vdc->get_features = virtio_scsi_get_features; diff --git a/include/hw/virtio/virtio-scsi.h b/include/hw/virtio/virtio-scsi.h index 206c61dbfd..da343f1893 100644 --- a/include/hw/virtio/virtio-scsi.h +++ b/include/hw/virtio/virtio-scsi.h @@ -186,7 +186,7 @@ typedef struct { DEFINE_PROP_BIT("param_change", _state, _feature_field, \ VIRTIO_SCSI_F_CHANGE, true) -int virtio_scsi_common_init(VirtIOSCSICommon *vs); +void virtio_scsi_common_realize(DeviceState *dev, Error **errp); void virtio_scsi_common_exit(VirtIOSCSICommon *vs); #endif /* _QEMU_VIRTIO_SCSI_H */ -- cgit v1.2.3-55-g7522 From 0ba94b6f94a5b0bed9f125ce4c3348adc83db5de Mon Sep 17 00:00:00 2001 From: Andreas Färber Date: Tue, 30 Jul 2013 04:05:02 +0200 Subject: virtio: Complete converting VirtioDevice to QOM realize Drop VirtioDeviceClass::init. Signed-off-by: Andreas Färber Signed-off-by: Paolo Bonzini --- hw/scsi/virtio-scsi.c | 3 +-- hw/virtio/virtio.c | 6 ------ include/hw/virtio/virtio.h | 5 +++-- 3 files changed, 4 insertions(+), 10 deletions(-) (limited to 'include/hw') diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 0a41ddb6c1..a208cf3d89 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -642,8 +642,7 @@ static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) void virtio_scsi_common_exit(VirtIOSCSICommon *vs) { - VirtIODevice *vdev = VIRTIO_DEVICE(dev); - VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); + VirtIODevice *vdev = VIRTIO_DEVICE(vs); g_free(vs->cmd_vqs); virtio_cleanup(vdev); diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 4070b37d64..1dba284e4a 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1156,18 +1156,12 @@ static void virtio_device_realize(DeviceState *dev, Error **errp) VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); Error *err = NULL; - assert(vdc->init != NULL || vdc->realize != NULL); if (vdc->realize != NULL) { vdc->realize(dev, &err); if (err != NULL) { error_propagate(errp, err); return; } - } else { - if (vdc->init(vdev) < 0) { - error_setg(errp, "Device initialization failed."); - return; - } } virtio_bus_device_plugged(vdev); } diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 54c5efe4d0..6da88c0f41 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -124,12 +124,13 @@ struct VirtIODevice }; typedef struct VirtioDeviceClass { + /*< private >*/ DeviceClass parent; + /*< public >*/ /* This is what a VirtioDevice must implement */ - int (*init)(VirtIODevice *vdev); - void (*exit)(VirtIODevice *vdev); DeviceRealize realize; + void (*exit)(VirtIODevice *vdev); uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features); uint32_t (*bad_features)(VirtIODevice *vdev); void (*set_features)(VirtIODevice *vdev, uint32_t val); -- cgit v1.2.3-55-g7522 From 306ec6c3cece7004429c79c1ac93d49919f1f1cc Mon Sep 17 00:00:00 2001 From: Andreas Färber Date: Tue, 30 Jul 2013 03:50:44 +0200 Subject: virtio: Convert exit to unrealize Signed-off-by: Andreas Färber Signed-off-by: Paolo Bonzini --- hw/block/virtio-blk.c | 10 ++++++---- hw/char/virtio-serial-bus.c | 9 +++++---- hw/net/virtio-net.c | 9 +++++---- hw/scsi/vhost-scsi.c | 11 ++++++----- hw/scsi/virtio-scsi.c | 17 +++++++++-------- hw/virtio/virtio-balloon.c | 9 +++++---- hw/virtio/virtio-rng.c | 9 +++++---- hw/virtio/virtio.c | 13 ++++++++----- include/hw/virtio/virtio-scsi.h | 2 +- include/hw/virtio/virtio.h | 2 +- 10 files changed, 51 insertions(+), 40 deletions(-) (limited to 'include/hw') diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index cb29fdb3f0..19d0961a47 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -738,16 +738,18 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp) add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0"); } -static void virtio_blk_device_exit(VirtIODevice *vdev) +static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp) { - VirtIOBlock *s = VIRTIO_BLK(vdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOBlock *s = VIRTIO_BLK(dev); + #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE remove_migration_state_change_notifier(&s->migration_state_notifier); virtio_blk_data_plane_destroy(s->dataplane); s->dataplane = NULL; #endif qemu_del_vm_change_state_handler(s->change); - unregister_savevm(DEVICE(vdev), "virtio-blk", s); + unregister_savevm(dev, "virtio-blk", s); blockdev_mark_auto_del(s->bs); virtio_cleanup(vdev); } @@ -765,7 +767,7 @@ static void virtio_blk_class_init(ObjectClass *klass, void *data) dc->props = virtio_blk_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); vdc->realize = virtio_blk_device_realize; - vdc->exit = virtio_blk_device_exit; + vdc->unrealize = virtio_blk_device_unrealize; vdc->get_config = virtio_blk_update_config; vdc->set_config = virtio_blk_set_config; vdc->get_features = virtio_blk_get_features; diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c index df9611703a..226e9f9a3c 100644 --- a/hw/char/virtio-serial-bus.c +++ b/hw/char/virtio-serial-bus.c @@ -988,11 +988,12 @@ static const TypeInfo virtio_serial_port_type_info = { .class_init = virtio_serial_port_class_init, }; -static void virtio_serial_device_exit(VirtIODevice *vdev) +static void virtio_serial_device_unrealize(DeviceState *dev, Error **errp) { - VirtIOSerial *vser = VIRTIO_SERIAL(vdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSerial *vser = VIRTIO_SERIAL(dev); - unregister_savevm(DEVICE(vdev), "virtio-console", vser); + unregister_savevm(dev, "virtio-console", vser); g_free(vser->ivqs); g_free(vser->ovqs); @@ -1019,7 +1020,7 @@ static void virtio_serial_class_init(ObjectClass *klass, void *data) dc->props = virtio_serial_properties; set_bit(DEVICE_CATEGORY_INPUT, dc->categories); vdc->realize = virtio_serial_device_realize; - vdc->exit = virtio_serial_device_exit; + vdc->unrealize = virtio_serial_device_unrealize; vdc->get_features = get_features; vdc->get_config = get_config; vdc->set_config = set_config; diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index d9e4b8a5ab..2b193bf6df 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -1567,15 +1567,16 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp) add_boot_device_path(n->nic_conf.bootindex, dev, "/ethernet-phy@0"); } -static void virtio_net_device_exit(VirtIODevice *vdev) +static void virtio_net_device_unrealize(DeviceState *dev, Error **errp) { - VirtIONet *n = VIRTIO_NET(vdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIONet *n = VIRTIO_NET(dev); int i; /* This will stop vhost backend if appropriate. */ virtio_net_set_status(vdev, 0); - unregister_savevm(DEVICE(vdev), "virtio-net", n); + unregister_savevm(dev, "virtio-net", n); if (n->netclient_name) { g_free(n->netclient_name); @@ -1636,7 +1637,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data) dc->props = virtio_net_properties; set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); vdc->realize = virtio_net_device_realize; - vdc->exit = virtio_net_device_exit; + vdc->unrealize = virtio_net_device_unrealize; vdc->get_config = virtio_net_get_config; vdc->set_config = virtio_net_set_config; vdc->get_features = virtio_net_get_features; diff --git a/hw/scsi/vhost-scsi.c b/hw/scsi/vhost-scsi.c index 1f1c9f3421..3983a5b464 100644 --- a/hw/scsi/vhost-scsi.c +++ b/hw/scsi/vhost-scsi.c @@ -240,10 +240,10 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp) migrate_add_blocker(s->migration_blocker); } -static void vhost_scsi_exit(VirtIODevice *vdev) +static void vhost_scsi_unrealize(DeviceState *dev, Error **errp) { - VHostSCSI *s = VHOST_SCSI(vdev); - VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VHostSCSI *s = VHOST_SCSI(dev); migrate_del_blocker(s->migration_blocker); error_free(s->migration_blocker); @@ -252,7 +252,8 @@ static void vhost_scsi_exit(VirtIODevice *vdev) vhost_scsi_set_status(vdev, 0); g_free(s->dev.vqs); - virtio_scsi_common_exit(vs); + + virtio_scsi_common_unrealize(dev, errp); } static Property vhost_scsi_properties[] = { @@ -268,7 +269,7 @@ static void vhost_scsi_class_init(ObjectClass *klass, void *data) dc->props = vhost_scsi_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); vdc->realize = vhost_scsi_realize; - vdc->exit = vhost_scsi_exit; + vdc->unrealize = vhost_scsi_unrealize; vdc->get_features = vhost_scsi_get_features; vdc->set_config = vhost_scsi_set_config; vdc->set_status = vhost_scsi_set_status; diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index a208cf3d89..6dcdd1b91c 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -640,21 +640,22 @@ static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) virtio_scsi_save, virtio_scsi_load, s); } -void virtio_scsi_common_exit(VirtIOSCSICommon *vs) +void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp) { - VirtIODevice *vdev = VIRTIO_DEVICE(vs); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); g_free(vs->cmd_vqs); virtio_cleanup(vdev); } -static void virtio_scsi_device_exit(VirtIODevice *vdev) +static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp) { - VirtIOSCSI *s = VIRTIO_SCSI(vdev); - VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); + VirtIOSCSI *s = VIRTIO_SCSI(dev); + + unregister_savevm(dev, "virtio-scsi", s); - unregister_savevm(DEVICE(vdev), "virtio-scsi", s); - virtio_scsi_common_exit(vs); + virtio_scsi_common_unrealize(dev, errp); } static Property virtio_scsi_properties[] = { @@ -679,7 +680,7 @@ static void virtio_scsi_class_init(ObjectClass *klass, void *data) dc->props = virtio_scsi_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); vdc->realize = virtio_scsi_device_realize; - vdc->exit = virtio_scsi_device_exit; + vdc->unrealize = virtio_scsi_device_unrealize; vdc->set_config = virtio_scsi_set_config; vdc->get_features = virtio_scsi_get_features; vdc->reset = virtio_scsi_reset; diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c index 755e188fdb..d9754dbd33 100644 --- a/hw/virtio/virtio-balloon.c +++ b/hw/virtio/virtio-balloon.c @@ -370,13 +370,14 @@ static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) NULL, s, NULL); } -static void virtio_balloon_device_exit(VirtIODevice *vdev) +static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp) { - VirtIOBalloon *s = VIRTIO_BALLOON(vdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOBalloon *s = VIRTIO_BALLOON(dev); balloon_stats_destroy_timer(s); qemu_remove_balloon_handler(s); - unregister_savevm(DEVICE(vdev), "virtio-balloon", s); + unregister_savevm(dev, "virtio-balloon", s); virtio_cleanup(vdev); } @@ -392,7 +393,7 @@ static void virtio_balloon_class_init(ObjectClass *klass, void *data) dc->props = virtio_balloon_properties; set_bit(DEVICE_CATEGORY_MISC, dc->categories); vdc->realize = virtio_balloon_device_realize; - vdc->exit = virtio_balloon_device_exit; + vdc->unrealize = virtio_balloon_device_unrealize; vdc->get_config = virtio_balloon_get_config; vdc->set_config = virtio_balloon_set_config; vdc->get_features = virtio_balloon_get_features; diff --git a/hw/virtio/virtio-rng.c b/hw/virtio/virtio-rng.c index fd61cb2edf..755fdee628 100644 --- a/hw/virtio/virtio-rng.c +++ b/hw/virtio/virtio-rng.c @@ -187,13 +187,14 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) virtio_rng_load, vrng); } -static void virtio_rng_device_exit(VirtIODevice *vdev) +static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp) { - VirtIORNG *vrng = VIRTIO_RNG(vdev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIORNG *vrng = VIRTIO_RNG(dev); timer_del(vrng->rate_limit_timer); timer_free(vrng->rate_limit_timer); - unregister_savevm(DEVICE(vdev), "virtio-rng", vrng); + unregister_savevm(dev, "virtio-rng", vrng); virtio_cleanup(vdev); } @@ -210,7 +211,7 @@ static void virtio_rng_class_init(ObjectClass *klass, void *data) dc->props = virtio_rng_properties; set_bit(DEVICE_CATEGORY_MISC, dc->categories); vdc->realize = virtio_rng_device_realize; - vdc->exit = virtio_rng_device_exit; + vdc->unrealize = virtio_rng_device_unrealize; vdc->get_features = get_features; } diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 1dba284e4a..144b9ca2ef 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1169,12 +1169,15 @@ static void virtio_device_realize(DeviceState *dev, Error **errp) static void virtio_device_unrealize(DeviceState *dev, Error **errp) { VirtIODevice *vdev = VIRTIO_DEVICE(dev); - VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(dev); - - virtio_bus_device_unplugged(vdev); + VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev); + Error *err = NULL; - if (k->exit != NULL) { - k->exit(vdev); + if (vdc->unrealize != NULL) { + vdc->unrealize(dev, &err); + if (err != NULL) { + error_propagate(errp, err); + return; + } } if (vdev->bus_name) { diff --git a/include/hw/virtio/virtio-scsi.h b/include/hw/virtio/virtio-scsi.h index da343f1893..42b102487a 100644 --- a/include/hw/virtio/virtio-scsi.h +++ b/include/hw/virtio/virtio-scsi.h @@ -187,6 +187,6 @@ typedef struct { VIRTIO_SCSI_F_CHANGE, true) void virtio_scsi_common_realize(DeviceState *dev, Error **errp); -void virtio_scsi_common_exit(VirtIOSCSICommon *vs); +void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp); #endif /* _QEMU_VIRTIO_SCSI_H */ diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 6da88c0f41..3e54e90aad 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -130,7 +130,7 @@ typedef struct VirtioDeviceClass { /* This is what a VirtioDevice must implement */ DeviceRealize realize; - void (*exit)(VirtIODevice *vdev); + DeviceUnrealize unrealize; uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features); uint32_t (*bad_features)(VirtIODevice *vdev); void (*set_features)(VirtIODevice *vdev, uint32_t val); -- cgit v1.2.3-55-g7522