virtio-bus: have callers tolerate new host notifier api
[qemu.git] / hw / block / dataplane / virtio-blk.c
blobfdf5fd1190375d6eca6165668c6dde1ee3740eb2
1 /*
2 * Dedicated thread for virtio-blk I/O processing
4 * Copyright 2012 IBM, Corp.
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
7 * Authors:
8 * Stefan Hajnoczi <stefanha@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "trace.h"
18 #include "qemu/iov.h"
19 #include "qemu/thread.h"
20 #include "qemu/error-report.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "sysemu/block-backend.h"
23 #include "hw/virtio/virtio-blk.h"
24 #include "virtio-blk.h"
25 #include "block/aio.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "qom/object_interfaces.h"
29 struct VirtIOBlockDataPlane {
30 bool starting;
31 bool stopping;
33 VirtIOBlkConf *conf;
35 VirtIODevice *vdev;
36 VirtQueue *vq; /* virtqueue vring */
37 EventNotifier *guest_notifier; /* irq */
38 QEMUBH *bh; /* bh for guest notification */
40 /* Note that these EventNotifiers are assigned by value. This is
41 * fine as long as you do not call event_notifier_cleanup on them
42 * (because you don't own the file descriptor or handle; you just
43 * use it).
45 IOThread *iothread;
46 AioContext *ctx;
49 /* Raise an interrupt to signal guest, if necessary */
50 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s)
52 qemu_bh_schedule(s->bh);
55 static void notify_guest_bh(void *opaque)
57 VirtIOBlockDataPlane *s = opaque;
59 if (!virtio_should_notify(s->vdev, s->vq)) {
60 return;
63 event_notifier_set(s->guest_notifier);
66 /* Context: QEMU global mutex held */
67 void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
68 VirtIOBlockDataPlane **dataplane,
69 Error **errp)
71 VirtIOBlockDataPlane *s;
72 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
73 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
75 *dataplane = NULL;
77 if (!conf->iothread) {
78 return;
81 /* Don't try if transport does not support notifiers. */
82 if (!k->set_guest_notifiers ||
83 (!k->set_host_notifier && !k->ioeventfd_started)) {
84 error_setg(errp,
85 "device is incompatible with dataplane "
86 "(transport does not support notifiers)");
87 return;
90 /* If dataplane is (re-)enabled while the guest is running there could be
91 * block jobs that can conflict.
93 if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
94 error_prepend(errp, "cannot start dataplane thread: ");
95 return;
98 s = g_new0(VirtIOBlockDataPlane, 1);
99 s->vdev = vdev;
100 s->conf = conf;
102 if (conf->iothread) {
103 s->iothread = conf->iothread;
104 object_ref(OBJECT(s->iothread));
106 s->ctx = iothread_get_aio_context(s->iothread);
107 s->bh = aio_bh_new(s->ctx, notify_guest_bh, s);
109 *dataplane = s;
112 /* Context: QEMU global mutex held */
113 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
115 if (!s) {
116 return;
119 virtio_blk_data_plane_stop(s);
120 qemu_bh_delete(s->bh);
121 object_unref(OBJECT(s->iothread));
122 g_free(s);
125 static void virtio_blk_data_plane_handle_output(VirtIODevice *vdev,
126 VirtQueue *vq)
128 VirtIOBlock *s = (VirtIOBlock *)vdev;
130 assert(s->dataplane);
131 assert(s->dataplane_started);
133 virtio_blk_handle_vq(s, vq);
136 /* Context: QEMU global mutex held */
137 void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
139 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
140 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
141 VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
142 int r;
144 if (vblk->dataplane_started || s->starting) {
145 return;
148 s->starting = true;
149 s->vq = virtio_get_queue(s->vdev, 0);
151 /* Set up guest notifier (irq) */
152 r = k->set_guest_notifiers(qbus->parent, 1, true);
153 if (r != 0) {
154 fprintf(stderr, "virtio-blk failed to set guest notifier (%d), "
155 "ensure -enable-kvm is set\n", r);
156 goto fail_guest_notifiers;
158 s->guest_notifier = virtio_queue_get_guest_notifier(s->vq);
160 /* Set up virtqueue notify */
161 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), 0, true);
162 if (r == -ENOSYS) {
163 r = k->set_host_notifier(qbus->parent, 0, true);
165 if (r != 0) {
166 fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
167 goto fail_host_notifier;
170 s->starting = false;
171 vblk->dataplane_started = true;
172 trace_virtio_blk_data_plane_start(s);
174 blk_set_aio_context(s->conf->conf.blk, s->ctx);
176 /* Kick right away to begin processing requests already in vring */
177 event_notifier_set(virtio_queue_get_host_notifier(s->vq));
179 /* Get this show started by hooking up our callbacks */
180 aio_context_acquire(s->ctx);
181 virtio_queue_aio_set_host_notifier_handler(s->vq, s->ctx,
182 virtio_blk_data_plane_handle_output);
183 aio_context_release(s->ctx);
184 return;
186 fail_host_notifier:
187 k->set_guest_notifiers(qbus->parent, 1, false);
188 fail_guest_notifiers:
189 vblk->dataplane_disabled = true;
190 s->starting = false;
191 vblk->dataplane_started = true;
194 /* Context: QEMU global mutex held */
195 void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
197 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
198 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
199 VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
200 int r;
202 if (!vblk->dataplane_started || s->stopping) {
203 return;
206 /* Better luck next time. */
207 if (vblk->dataplane_disabled) {
208 vblk->dataplane_disabled = false;
209 vblk->dataplane_started = false;
210 return;
212 s->stopping = true;
213 trace_virtio_blk_data_plane_stop(s);
215 aio_context_acquire(s->ctx);
217 /* Stop notifications for new requests from guest */
218 virtio_queue_aio_set_host_notifier_handler(s->vq, s->ctx, NULL);
220 /* Drain and switch bs back to the QEMU main loop */
221 blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
223 aio_context_release(s->ctx);
225 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), 0, false);
226 if (r == -ENOSYS) {
227 k->set_host_notifier(qbus->parent, 0, false);
230 /* Clean up guest notifier (irq) */
231 k->set_guest_notifiers(qbus->parent, 1, false);
233 vblk->dataplane_started = false;
234 s->stopping = false;