exec: remove ram_addr argument from qemu_ram_block_from_host
[qemu.git] / hw / scsi / virtio-scsi-dataplane.c
blob1a49f1e4b73e944d3627b97e53a668fbbfb7e576
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->set_host_notifier) {
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 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
73 int rc;
75 /* Set up virtqueue notify */
76 rc = k->set_host_notifier(qbus->parent, n, true);
77 if (rc != 0) {
78 fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
79 rc);
80 s->dataplane_fenced = true;
81 return rc;
84 virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, fn);
85 return 0;
88 void virtio_scsi_dataplane_notify(VirtIODevice *vdev, VirtIOSCSIReq *req)
90 if (virtio_should_notify(vdev, req->vq)) {
91 event_notifier_set(virtio_queue_get_guest_notifier(req->vq));
95 /* assumes s->ctx held */
96 static void virtio_scsi_clear_aio(VirtIOSCSI *s)
98 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
99 int i;
101 virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx, NULL);
102 virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx, NULL);
103 for (i = 0; i < vs->conf.num_queues; i++) {
104 virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL);
108 /* Context: QEMU global mutex held */
109 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
111 int i;
112 int rc;
113 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
114 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
115 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
117 if (s->dataplane_started ||
118 s->dataplane_starting ||
119 s->dataplane_fenced ||
120 s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
121 return;
124 s->dataplane_starting = true;
126 /* Set up guest notifier (irq) */
127 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
128 if (rc != 0) {
129 fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
130 "ensure -enable-kvm is set\n", rc);
131 goto fail_guest_notifiers;
134 aio_context_acquire(s->ctx);
135 rc = virtio_scsi_vring_init(s, vs->ctrl_vq, 0,
136 virtio_scsi_data_plane_handle_ctrl);
137 if (rc) {
138 goto fail_vrings;
140 rc = virtio_scsi_vring_init(s, vs->event_vq, 1,
141 virtio_scsi_data_plane_handle_event);
142 if (rc) {
143 goto fail_vrings;
145 for (i = 0; i < vs->conf.num_queues; i++) {
146 rc = virtio_scsi_vring_init(s, vs->cmd_vqs[i], i + 2,
147 virtio_scsi_data_plane_handle_cmd);
148 if (rc) {
149 goto fail_vrings;
153 s->dataplane_starting = false;
154 s->dataplane_started = true;
155 aio_context_release(s->ctx);
156 return;
158 fail_vrings:
159 virtio_scsi_clear_aio(s);
160 aio_context_release(s->ctx);
161 for (i = 0; i < vs->conf.num_queues + 2; i++) {
162 k->set_host_notifier(qbus->parent, i, false);
164 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
165 fail_guest_notifiers:
166 s->dataplane_fenced = true;
167 s->dataplane_starting = false;
168 s->dataplane_started = true;
171 /* Context: QEMU global mutex held */
172 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
174 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
175 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
176 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
177 int i;
179 if (!s->dataplane_started || s->dataplane_stopping) {
180 return;
183 /* Better luck next time. */
184 if (s->dataplane_fenced) {
185 s->dataplane_fenced = false;
186 s->dataplane_started = false;
187 return;
189 s->dataplane_stopping = true;
190 assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
192 aio_context_acquire(s->ctx);
194 virtio_scsi_clear_aio(s);
196 blk_drain_all(); /* ensure there are no in-flight requests */
198 aio_context_release(s->ctx);
200 for (i = 0; i < vs->conf.num_queues + 2; i++) {
201 k->set_host_notifier(qbus->parent, i, false);
204 /* Clean up guest notifier (irq) */
205 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
206 s->dataplane_stopping = false;
207 s->dataplane_started = false;