pcihp: acpi: ignore coldplugged bridges when composing hotpluggable slots
[qemu.git] / hw / block / dataplane / virtio-blk.c
blobb28d81737ef5ef077ab7a656f874bdeb8dd6c019
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/main-loop.h"
20 #include "qemu/thread.h"
21 #include "qemu/error-report.h"
22 #include "hw/virtio/virtio-access.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;
34 VirtIODevice *vdev;
35 QEMUBH *bh; /* bh for guest notification */
36 unsigned long *batch_notify_vqs;
37 bool batch_notifications;
39 /* Note that these EventNotifiers are assigned by value. This is
40 * fine as long as you do not call event_notifier_cleanup on them
41 * (because you don't own the file descriptor or handle; you just
42 * use it).
44 IOThread *iothread;
45 AioContext *ctx;
48 /* Raise an interrupt to signal guest, if necessary */
49 void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s, VirtQueue *vq)
51 if (s->batch_notifications) {
52 set_bit(virtio_get_queue_index(vq), s->batch_notify_vqs);
53 qemu_bh_schedule(s->bh);
54 } else {
55 virtio_notify_irqfd(s->vdev, vq);
59 static void notify_guest_bh(void *opaque)
61 VirtIOBlockDataPlane *s = opaque;
62 unsigned nvqs = s->conf->num_queues;
63 unsigned long bitmap[BITS_TO_LONGS(nvqs)];
64 unsigned j;
66 memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap));
67 memset(s->batch_notify_vqs, 0, sizeof(bitmap));
69 for (j = 0; j < nvqs; j += BITS_PER_LONG) {
70 unsigned long bits = bitmap[j / BITS_PER_LONG];
72 while (bits != 0) {
73 unsigned i = j + ctzl(bits);
74 VirtQueue *vq = virtio_get_queue(s->vdev, i);
76 virtio_notify_irqfd(s->vdev, vq);
78 bits &= bits - 1; /* clear right-most bit */
83 /* Context: QEMU global mutex held */
84 bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
85 VirtIOBlockDataPlane **dataplane,
86 Error **errp)
88 VirtIOBlockDataPlane *s;
89 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
90 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
92 *dataplane = NULL;
94 if (conf->iothread) {
95 if (!k->set_guest_notifiers || !k->ioeventfd_assign) {
96 error_setg(errp,
97 "device is incompatible with iothread "
98 "(transport does not support notifiers)");
99 return false;
101 if (!virtio_device_ioeventfd_enabled(vdev)) {
102 error_setg(errp, "ioeventfd is required for iothread");
103 return false;
106 /* If dataplane is (re-)enabled while the guest is running there could
107 * be block jobs that can conflict.
109 if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
110 error_prepend(errp, "cannot start virtio-blk dataplane: ");
111 return false;
114 /* Don't try if transport does not support notifiers. */
115 if (!virtio_device_ioeventfd_enabled(vdev)) {
116 return false;
119 s = g_new0(VirtIOBlockDataPlane, 1);
120 s->vdev = vdev;
121 s->conf = conf;
123 if (conf->iothread) {
124 s->iothread = conf->iothread;
125 object_ref(OBJECT(s->iothread));
126 s->ctx = iothread_get_aio_context(s->iothread);
127 } else {
128 s->ctx = qemu_get_aio_context();
130 s->bh = aio_bh_new(s->ctx, notify_guest_bh, s);
131 s->batch_notify_vqs = bitmap_new(conf->num_queues);
133 *dataplane = s;
135 return true;
138 /* Context: QEMU global mutex held */
139 void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
141 VirtIOBlock *vblk;
143 if (!s) {
144 return;
147 vblk = VIRTIO_BLK(s->vdev);
148 assert(!vblk->dataplane_started);
149 g_free(s->batch_notify_vqs);
150 qemu_bh_delete(s->bh);
151 if (s->iothread) {
152 object_unref(OBJECT(s->iothread));
154 g_free(s);
157 /* Context: QEMU global mutex held */
158 int virtio_blk_data_plane_start(VirtIODevice *vdev)
160 VirtIOBlock *vblk = VIRTIO_BLK(vdev);
161 VirtIOBlockDataPlane *s = vblk->dataplane;
162 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vblk)));
163 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
164 AioContext *old_context;
165 unsigned i;
166 unsigned nvqs = s->conf->num_queues;
167 Error *local_err = NULL;
168 int r;
170 if (vblk->dataplane_started || s->starting) {
171 return 0;
174 s->starting = true;
176 if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
177 s->batch_notifications = true;
178 } else {
179 s->batch_notifications = false;
182 /* Set up guest notifier (irq) */
183 r = k->set_guest_notifiers(qbus->parent, nvqs, true);
184 if (r != 0) {
185 error_report("virtio-blk failed to set guest notifier (%d), "
186 "ensure -accel kvm is set.", r);
187 goto fail_guest_notifiers;
191 * Batch all the host notifiers in a single transaction to avoid
192 * quadratic time complexity in address_space_update_ioeventfds().
194 memory_region_transaction_begin();
196 /* Set up virtqueue notify */
197 for (i = 0; i < nvqs; i++) {
198 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, true);
199 if (r != 0) {
200 int j = i;
202 fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
203 while (i--) {
204 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
208 * The transaction expects the ioeventfds to be open when it
209 * commits. Do it now, before the cleanup loop.
211 memory_region_transaction_commit();
213 while (j--) {
214 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), j);
216 goto fail_host_notifiers;
220 memory_region_transaction_commit();
223 * These fields are visible to the IOThread so we rely on implicit barriers
224 * in aio_context_acquire() on the write side and aio_notify_accept() on
225 * the read side.
227 s->starting = false;
228 vblk->dataplane_started = true;
229 trace_virtio_blk_data_plane_start(s);
231 old_context = blk_get_aio_context(s->conf->conf.blk);
232 aio_context_acquire(old_context);
233 r = blk_set_aio_context(s->conf->conf.blk, s->ctx, &local_err);
234 aio_context_release(old_context);
235 if (r < 0) {
236 error_report_err(local_err);
237 goto fail_aio_context;
240 /* Kick right away to begin processing requests already in vring */
241 for (i = 0; i < nvqs; i++) {
242 VirtQueue *vq = virtio_get_queue(s->vdev, i);
244 event_notifier_set(virtio_queue_get_host_notifier(vq));
247 /* Get this show started by hooking up our callbacks */
248 aio_context_acquire(s->ctx);
249 for (i = 0; i < nvqs; i++) {
250 VirtQueue *vq = virtio_get_queue(s->vdev, i);
252 virtio_queue_aio_attach_host_notifier(vq, s->ctx);
254 aio_context_release(s->ctx);
255 return 0;
257 fail_aio_context:
258 memory_region_transaction_begin();
260 for (i = 0; i < nvqs; i++) {
261 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
264 memory_region_transaction_commit();
266 for (i = 0; i < nvqs; i++) {
267 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
269 fail_host_notifiers:
270 k->set_guest_notifiers(qbus->parent, nvqs, false);
271 fail_guest_notifiers:
272 vblk->dataplane_disabled = true;
273 s->starting = false;
274 vblk->dataplane_started = true;
275 return -ENOSYS;
278 /* Stop notifications for new requests from guest.
280 * Context: BH in IOThread
282 static void virtio_blk_data_plane_stop_bh(void *opaque)
284 VirtIOBlockDataPlane *s = opaque;
285 unsigned i;
287 for (i = 0; i < s->conf->num_queues; i++) {
288 VirtQueue *vq = virtio_get_queue(s->vdev, i);
290 virtio_queue_aio_detach_host_notifier(vq, s->ctx);
294 /* Context: QEMU global mutex held */
295 void virtio_blk_data_plane_stop(VirtIODevice *vdev)
297 VirtIOBlock *vblk = VIRTIO_BLK(vdev);
298 VirtIOBlockDataPlane *s = vblk->dataplane;
299 BusState *qbus = qdev_get_parent_bus(DEVICE(vblk));
300 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
301 unsigned i;
302 unsigned nvqs = s->conf->num_queues;
304 if (!vblk->dataplane_started || s->stopping) {
305 return;
308 /* Better luck next time. */
309 if (vblk->dataplane_disabled) {
310 vblk->dataplane_disabled = false;
311 vblk->dataplane_started = false;
312 return;
314 s->stopping = true;
315 trace_virtio_blk_data_plane_stop(s);
317 aio_context_acquire(s->ctx);
318 aio_wait_bh_oneshot(s->ctx, virtio_blk_data_plane_stop_bh, s);
320 /* Wait for virtio_blk_dma_restart_bh() and in flight I/O to complete */
321 blk_drain(s->conf->conf.blk);
324 * Try to switch bs back to the QEMU main loop. If other users keep the
325 * BlockBackend in the iothread, that's ok
327 blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context(), NULL);
329 aio_context_release(s->ctx);
332 * Batch all the host notifiers in a single transaction to avoid
333 * quadratic time complexity in address_space_update_ioeventfds().
335 memory_region_transaction_begin();
337 for (i = 0; i < nvqs; i++) {
338 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
342 * The transaction expects the ioeventfds to be open when it
343 * commits. Do it now, before the cleanup loop.
345 memory_region_transaction_commit();
347 for (i = 0; i < nvqs; i++) {
348 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
351 qemu_bh_cancel(s->bh);
352 notify_guest_bh(s); /* final chance to notify guest */
354 /* Clean up guest notifier (irq) */
355 k->set_guest_notifiers(qbus->parent, nvqs, false);
357 vblk->dataplane_started = false;
358 s->stopping = false;