block: Accept any target node for transactional blockdev-backup
[qemu.git] / hw / scsi / virtio-scsi-dataplane.c
blobb173b94949f73a25aa6617cf7c988263564652c8
1 /*
2 * Virtio SCSI dataplane
4 * Copyright Red Hat, Inc. 2014
6 * Authors:
7 * Fam Zheng <famz@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "hw/virtio/virtio-scsi.h"
16 #include "qemu/error-report.h"
17 #include "sysemu/block-backend.h"
18 #include "hw/scsi/scsi.h"
19 #include "block/scsi.h"
20 #include "hw/virtio/virtio-bus.h"
21 #include "hw/virtio/virtio-access.h"
23 /* Context: QEMU global mutex held */
24 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread)
26 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
27 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
28 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
30 assert(!s->ctx);
31 s->ctx = iothread_get_aio_context(vs->conf.iothread);
33 /* Don't try if transport does not support notifiers. */
34 if (!k->set_guest_notifiers || !k->ioeventfd_started) {
35 fprintf(stderr, "virtio-scsi: Failed to set iothread "
36 "(transport does not support notifiers)");
37 exit(1);
41 static void virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev,
42 VirtQueue *vq)
44 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
46 assert(s->ctx && s->dataplane_started);
47 virtio_scsi_handle_cmd_vq(s, vq);
50 static void virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev,
51 VirtQueue *vq)
53 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
55 assert(s->ctx && s->dataplane_started);
56 virtio_scsi_handle_ctrl_vq(s, vq);
59 static void virtio_scsi_data_plane_handle_event(VirtIODevice *vdev,
60 VirtQueue *vq)
62 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
64 assert(s->ctx && s->dataplane_started);
65 virtio_scsi_handle_event_vq(s, vq);
68 static int virtio_scsi_vring_init(VirtIOSCSI *s, VirtQueue *vq, int n,
69 void (*fn)(VirtIODevice *vdev, VirtQueue *vq))
71 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
72 int rc;
74 /* Set up virtqueue notify */
75 rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true);
76 if (rc != 0) {
77 fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
78 rc);
79 s->dataplane_fenced = true;
80 return rc;
83 virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, fn);
84 return 0;
87 void virtio_scsi_dataplane_notify(VirtIODevice *vdev, VirtIOSCSIReq *req)
89 if (virtio_should_notify(vdev, req->vq)) {
90 event_notifier_set(virtio_queue_get_guest_notifier(req->vq));
94 /* assumes s->ctx held */
95 static void virtio_scsi_clear_aio(VirtIOSCSI *s)
97 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
98 int i;
100 virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx, NULL);
101 virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx, NULL);
102 for (i = 0; i < vs->conf.num_queues; i++) {
103 virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL);
107 /* Context: QEMU global mutex held */
108 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
110 int i;
111 int rc;
112 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
113 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
114 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
116 if (s->dataplane_started ||
117 s->dataplane_starting ||
118 s->dataplane_fenced ||
119 s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
120 return;
123 s->dataplane_starting = true;
125 /* Set up guest notifier (irq) */
126 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
127 if (rc != 0) {
128 fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
129 "ensure -enable-kvm is set\n", rc);
130 goto fail_guest_notifiers;
133 aio_context_acquire(s->ctx);
134 rc = virtio_scsi_vring_init(s, vs->ctrl_vq, 0,
135 virtio_scsi_data_plane_handle_ctrl);
136 if (rc) {
137 goto fail_vrings;
139 rc = virtio_scsi_vring_init(s, vs->event_vq, 1,
140 virtio_scsi_data_plane_handle_event);
141 if (rc) {
142 goto fail_vrings;
144 for (i = 0; i < vs->conf.num_queues; i++) {
145 rc = virtio_scsi_vring_init(s, vs->cmd_vqs[i], i + 2,
146 virtio_scsi_data_plane_handle_cmd);
147 if (rc) {
148 goto fail_vrings;
152 s->dataplane_starting = false;
153 s->dataplane_started = true;
154 aio_context_release(s->ctx);
155 return;
157 fail_vrings:
158 virtio_scsi_clear_aio(s);
159 aio_context_release(s->ctx);
160 for (i = 0; i < vs->conf.num_queues + 2; i++) {
161 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
163 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
164 fail_guest_notifiers:
165 s->dataplane_fenced = true;
166 s->dataplane_starting = false;
167 s->dataplane_started = true;
170 /* Context: QEMU global mutex held */
171 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
173 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
174 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
175 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
176 int i;
178 if (!s->dataplane_started || s->dataplane_stopping) {
179 return;
182 /* Better luck next time. */
183 if (s->dataplane_fenced) {
184 s->dataplane_fenced = false;
185 s->dataplane_started = false;
186 return;
188 s->dataplane_stopping = true;
189 assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
191 aio_context_acquire(s->ctx);
193 virtio_scsi_clear_aio(s);
195 blk_drain_all(); /* ensure there are no in-flight requests */
197 aio_context_release(s->ctx);
199 for (i = 0; i < vs->conf.num_queues + 2; i++) {
200 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
203 /* Clean up guest notifier (irq) */
204 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
205 s->dataplane_stopping = false;
206 s->dataplane_started = false;