target-s390x: support OC and NC in the EX instruction
[qemu/qmp-unstable.git] / hw / scsi / virtio-scsi-dataplane.c
blob03a1e8cfcfca5caf90ec1adb8be006d7e3ec88fa
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 VirtIODevice *vdev = VIRTIO_DEVICE(req->vring->parent);
97 vring_push(&req->vring->vring, &req->elem,
98 req->qsgl.size + req->resp_iov.size);
100 if (vring_should_notify(vdev, &req->vring->vring)) {
101 event_notifier_set(&req->vring->guest_notifier);
105 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
107 VirtIOSCSIVring *vring = container_of(notifier,
108 VirtIOSCSIVring, host_notifier);
109 VirtIOSCSI *s = VIRTIO_SCSI(vring->parent);
110 VirtIOSCSIReq *req;
112 event_notifier_test_and_clear(notifier);
113 while ((req = virtio_scsi_pop_req_vring(s, vring))) {
114 virtio_scsi_handle_ctrl_req(s, req);
118 static void virtio_scsi_iothread_handle_event(EventNotifier *notifier)
120 VirtIOSCSIVring *vring = container_of(notifier,
121 VirtIOSCSIVring, host_notifier);
122 VirtIOSCSI *s = vring->parent;
123 VirtIODevice *vdev = VIRTIO_DEVICE(s);
125 event_notifier_test_and_clear(notifier);
127 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
128 return;
131 if (s->events_dropped) {
132 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
136 static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier)
138 VirtIOSCSIVring *vring = container_of(notifier,
139 VirtIOSCSIVring, host_notifier);
140 VirtIOSCSI *s = (VirtIOSCSI *)vring->parent;
141 VirtIOSCSIReq *req, *next;
142 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
144 event_notifier_test_and_clear(notifier);
145 while ((req = virtio_scsi_pop_req_vring(s, vring))) {
146 if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
147 QTAILQ_INSERT_TAIL(&reqs, req, next);
151 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
152 virtio_scsi_handle_cmd_req_submit(s, req);
156 /* assumes s->ctx held */
157 static void virtio_scsi_clear_aio(VirtIOSCSI *s)
159 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
160 int i;
162 if (s->ctrl_vring) {
163 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
165 if (s->event_vring) {
166 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
168 if (s->cmd_vrings) {
169 for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) {
170 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL);
175 static void virtio_scsi_vring_teardown(VirtIOSCSI *s)
177 VirtIODevice *vdev = VIRTIO_DEVICE(s);
178 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
179 int i;
181 if (s->ctrl_vring) {
182 vring_teardown(&s->ctrl_vring->vring, vdev, 0);
184 if (s->event_vring) {
185 vring_teardown(&s->event_vring->vring, vdev, 1);
187 if (s->cmd_vrings) {
188 for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) {
189 vring_teardown(&s->cmd_vrings[i]->vring, vdev, 2 + i);
191 free(s->cmd_vrings);
192 s->cmd_vrings = NULL;
196 /* Context: QEMU global mutex held */
197 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
199 int i;
200 int rc;
201 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
202 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
203 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
205 if (s->dataplane_started ||
206 s->dataplane_starting ||
207 s->dataplane_fenced ||
208 s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
209 return;
212 s->dataplane_starting = true;
214 assert(!s->blocker);
215 error_setg(&s->blocker, "block device is in use by data plane");
216 /* Set up guest notifier (irq) */
217 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
218 if (rc != 0) {
219 fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
220 "ensure -enable-kvm is set\n", rc);
221 s->dataplane_fenced = true;
222 goto fail_guest_notifiers;
225 aio_context_acquire(s->ctx);
226 s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
227 virtio_scsi_iothread_handle_ctrl,
229 if (!s->ctrl_vring) {
230 goto fail_vrings;
232 s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
233 virtio_scsi_iothread_handle_event,
235 if (!s->event_vring) {
236 goto fail_vrings;
238 s->cmd_vrings = g_new(VirtIOSCSIVring *, vs->conf.num_queues);
239 for (i = 0; i < vs->conf.num_queues; i++) {
240 s->cmd_vrings[i] =
241 virtio_scsi_vring_init(s, vs->cmd_vqs[i],
242 virtio_scsi_iothread_handle_cmd,
243 i + 2);
244 if (!s->cmd_vrings[i]) {
245 goto fail_vrings;
249 s->dataplane_starting = false;
250 s->dataplane_started = true;
251 aio_context_release(s->ctx);
252 return;
254 fail_vrings:
255 virtio_scsi_clear_aio(s);
256 aio_context_release(s->ctx);
257 virtio_scsi_vring_teardown(s);
258 for (i = 0; i < vs->conf.num_queues + 2; i++) {
259 k->set_host_notifier(qbus->parent, i, false);
261 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
262 fail_guest_notifiers:
263 s->dataplane_starting = false;
266 /* Context: QEMU global mutex held */
267 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
269 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
270 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
271 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
272 int i;
274 /* Better luck next time. */
275 if (s->dataplane_fenced) {
276 s->dataplane_fenced = false;
277 return;
279 if (!s->dataplane_started || s->dataplane_stopping) {
280 return;
282 error_free(s->blocker);
283 s->blocker = NULL;
284 s->dataplane_stopping = true;
285 assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
287 aio_context_acquire(s->ctx);
289 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
290 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
291 for (i = 0; i < vs->conf.num_queues; i++) {
292 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL);
295 blk_drain_all(); /* ensure there are no in-flight requests */
297 aio_context_release(s->ctx);
299 /* Sync vring state back to virtqueue so that non-dataplane request
300 * processing can continue when we disable the host notifier below.
302 virtio_scsi_vring_teardown(s);
304 for (i = 0; i < vs->conf.num_queues + 2; i++) {
305 k->set_host_notifier(qbus->parent, i, false);
308 /* Clean up guest notifier (irq) */
309 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
310 s->dataplane_stopping = false;
311 s->dataplane_started = false;