@@ -222,9 +222,107 @@ static uint64_t vhost_vdpa_device_get_features(VirtIODevice *vdev,
return backend_features;
}
+static int vhost_vdpa_device_start(VirtIODevice *vdev, Error **errp)
+{
+ VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
+ BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+ VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+ int i, ret;
+
+ if (!k->set_guest_notifiers) {
+ error_setg(errp, "binding does not support guest notifiers");
+ return -ENOSYS;
+ }
+
+ ret = vhost_dev_enable_notifiers(&s->dev, vdev);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Error enabling host notifiers");
+ return ret;
+ }
+
+ ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Error binding guest notifier");
+ goto err_host_notifiers;
+ }
+
+ s->dev.acked_features = vdev->guest_features;
+
+ ret = vhost_dev_start(&s->dev, vdev);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Error starting vhost");
+ goto err_guest_notifiers;
+ }
+ s->started = true;
+
+ /*
+ * guest_notifier_mask/pending not used yet, so just unmask
+ * everything here. virtio-pci will do the right thing by
+ * enabling/disabling irqfd.
+ */
+ for (i = 0; i < s->dev.nvqs; i++) {
+ vhost_virtqueue_mask(&s->dev, vdev, i, false);
+ }
+
+ return ret;
+
+err_guest_notifiers:
+ k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
+err_host_notifiers:
+ vhost_dev_disable_notifiers(&s->dev, vdev);
+ return ret;
+}
+
+static void vhost_vdpa_device_stop(VirtIODevice *vdev)
+{
+ VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
+ BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+ VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+ int ret;
+
+ if (!s->started) {
+ return;
+ }
+ s->started = false;
+
+ if (!k->set_guest_notifiers) {
+ return;
+ }
+
+ vhost_dev_stop(&s->dev, vdev);
+
+ ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
+ if (ret < 0) {
+ error_report("vhost guest notifier cleanup failed: %d", ret);
+ return;
+ }
+
+ vhost_dev_disable_notifiers(&s->dev, vdev);
+}
+
static void vhost_vdpa_device_set_status(VirtIODevice *vdev, uint8_t status)
{
- return;
+ VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
+ bool should_start = virtio_device_started(vdev, status);
+ Error *local_err = NULL;
+ int ret;
+
+ if (!vdev->vm_running) {
+ should_start = false;
+ }
+
+ if (s->started == should_start) {
+ return;
+ }
+
+ if (should_start) {
+ ret = vhost_vdpa_device_start(vdev, &local_err);
+ if (ret < 0) {
+ error_reportf_err(local_err, "vhost-vdpa-device: start failed: ");
+ }
+ } else {
+ vhost_vdpa_device_stop(vdev);
+ }
}
static Property vhost_vdpa_device_properties[] = {