virtio-scsi: fix dataplane
[qemu-kvm.git] / hw / scsi / virtio-scsi-dataplane.c
blob9651e6f274f648cd2ccec4c4c44bb8471c418f3b
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 "hw/virtio/virtio-scsi.h"
15 #include "qemu/error-report.h"
16 #include "sysemu/block-backend.h"
17 #include <hw/scsi/scsi.h>
18 #include <block/scsi.h>
19 #include <hw/virtio/virtio-bus.h>
20 #include "hw/virtio/virtio-access.h"
21 #include "stdio.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->set_host_notifier) {
35 fprintf(stderr, "virtio-scsi: Failed to set iothread "
36 "(transport does not support notifiers)");
37 exit(1);
41 static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
42 VirtQueue *vq,
43 EventNotifierHandler *handler,
44 int n)
46 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
47 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
48 VirtIOSCSIVring *r = g_slice_new(VirtIOSCSIVring);
49 int rc;
51 /* Set up virtqueue notify */
52 rc = k->set_host_notifier(qbus->parent, n, true);
53 if (rc != 0) {
54 fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
55 rc);
56 s->dataplane_fenced = true;
57 return NULL;
59 r->host_notifier = *virtio_queue_get_host_notifier(vq);
60 r->guest_notifier = *virtio_queue_get_guest_notifier(vq);
61 aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
63 r->parent = s;
65 if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
66 fprintf(stderr, "virtio-scsi: VRing setup failed\n");
67 goto fail_vring;
69 return r;
71 fail_vring:
72 aio_set_event_notifier(s->ctx, &r->host_notifier, NULL);
73 k->set_host_notifier(qbus->parent, n, false);
74 g_slice_free(VirtIOSCSIVring, r);
75 return NULL;
78 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s,
79 VirtIOSCSIVring *vring)
81 VirtIOSCSIReq *req = virtio_scsi_init_req(s, NULL);
82 int r;
84 req->vring = vring;
85 r = vring_pop((VirtIODevice *)s, &vring->vring, &req->elem);
86 if (r < 0) {
87 virtio_scsi_free_req(req);
88 req = NULL;
90 return req;
93 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
95 vring_push(&req->vring->vring, &req->elem,
96 req->qsgl.size + req->resp_iov.size);
97 event_notifier_set(&req->vring->guest_notifier);
100 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
102 VirtIOSCSIVring *vring = container_of(notifier,
103 VirtIOSCSIVring, host_notifier);
104 VirtIOSCSI *s = VIRTIO_SCSI(vring->parent);
105 VirtIOSCSIReq *req;
107 event_notifier_test_and_clear(notifier);
108 while ((req = virtio_scsi_pop_req_vring(s, vring))) {
109 virtio_scsi_handle_ctrl_req(s, req);
113 static void virtio_scsi_iothread_handle_event(EventNotifier *notifier)
115 VirtIOSCSIVring *vring = container_of(notifier,
116 VirtIOSCSIVring, host_notifier);
117 VirtIOSCSI *s = vring->parent;
118 VirtIODevice *vdev = VIRTIO_DEVICE(s);
120 event_notifier_test_and_clear(notifier);
122 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
123 return;
126 if (s->events_dropped) {
127 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
131 static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier)
133 VirtIOSCSIVring *vring = container_of(notifier,
134 VirtIOSCSIVring, host_notifier);
135 VirtIOSCSI *s = (VirtIOSCSI *)vring->parent;
136 VirtIOSCSIReq *req, *next;
137 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
139 event_notifier_test_and_clear(notifier);
140 while ((req = virtio_scsi_pop_req_vring(s, vring))) {
141 if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
142 QTAILQ_INSERT_TAIL(&reqs, req, next);
146 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
147 virtio_scsi_handle_cmd_req_submit(s, req);
151 /* assumes s->ctx held */
152 static void virtio_scsi_clear_aio(VirtIOSCSI *s)
154 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
155 int i;
157 if (s->ctrl_vring) {
158 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
160 if (s->event_vring) {
161 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
163 if (s->cmd_vrings) {
164 for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) {
165 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL);
170 static void virtio_scsi_vring_teardown(VirtIOSCSI *s)
172 VirtIODevice *vdev = VIRTIO_DEVICE(s);
173 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
174 int i;
176 if (s->ctrl_vring) {
177 vring_teardown(&s->ctrl_vring->vring, vdev, 0);
179 if (s->event_vring) {
180 vring_teardown(&s->event_vring->vring, vdev, 1);
182 if (s->cmd_vrings) {
183 for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) {
184 vring_teardown(&s->cmd_vrings[i]->vring, vdev, 2 + i);
186 free(s->cmd_vrings);
187 s->cmd_vrings = NULL;
191 /* Context: QEMU global mutex held */
192 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
194 int i;
195 int rc;
196 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
197 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
198 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
200 if (s->dataplane_started ||
201 s->dataplane_starting ||
202 s->dataplane_fenced ||
203 s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
204 return;
207 s->dataplane_starting = true;
209 assert(!s->blocker);
210 error_setg(&s->blocker, "block device is in use by data plane");
211 /* Set up guest notifier (irq) */
212 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
213 if (rc != 0) {
214 fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
215 "ensure -enable-kvm is set\n", rc);
216 s->dataplane_fenced = true;
217 goto fail_guest_notifiers;
220 aio_context_acquire(s->ctx);
221 s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
222 virtio_scsi_iothread_handle_ctrl,
224 if (!s->ctrl_vring) {
225 goto fail_vrings;
227 s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
228 virtio_scsi_iothread_handle_event,
230 if (!s->event_vring) {
231 goto fail_vrings;
233 s->cmd_vrings = g_malloc0(sizeof(VirtIOSCSIVring) * vs->conf.num_queues);
234 for (i = 0; i < vs->conf.num_queues; i++) {
235 s->cmd_vrings[i] =
236 virtio_scsi_vring_init(s, vs->cmd_vqs[i],
237 virtio_scsi_iothread_handle_cmd,
238 i + 2);
239 if (!s->cmd_vrings[i]) {
240 goto fail_vrings;
244 s->dataplane_starting = false;
245 s->dataplane_started = true;
246 aio_context_release(s->ctx);
247 return;
249 fail_vrings:
250 virtio_scsi_clear_aio(s);
251 aio_context_release(s->ctx);
252 virtio_scsi_vring_teardown(s);
253 for (i = 0; i < vs->conf.num_queues + 2; i++) {
254 k->set_host_notifier(qbus->parent, i, false);
256 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
257 fail_guest_notifiers:
258 s->dataplane_starting = false;
261 /* Context: QEMU global mutex held */
262 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
264 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
265 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
266 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
267 int i;
269 /* Better luck next time. */
270 if (s->dataplane_fenced) {
271 s->dataplane_fenced = false;
272 return;
274 if (!s->dataplane_started || s->dataplane_stopping) {
275 return;
277 error_free(s->blocker);
278 s->blocker = NULL;
279 s->dataplane_stopping = true;
280 assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
282 aio_context_acquire(s->ctx);
284 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
285 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
286 for (i = 0; i < vs->conf.num_queues; i++) {
287 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL);
290 blk_drain_all(); /* ensure there are no in-flight requests */
292 aio_context_release(s->ctx);
294 /* Sync vring state back to virtqueue so that non-dataplane request
295 * processing can continue when we disable the host notifier below.
297 virtio_scsi_vring_teardown(s);
299 for (i = 0; i < vs->conf.num_queues + 2; i++) {
300 k->set_host_notifier(qbus->parent, i, false);
303 /* Clean up guest notifier (irq) */
304 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
305 s->dataplane_stopping = false;
306 s->dataplane_started = false;