]> git.proxmox.com Git - mirror_qemu.git/blob - hw/scsi/virtio-scsi-dataplane.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[mirror_qemu.git] / hw / scsi / virtio-scsi-dataplane.c
1 /*
2 * Virtio SCSI dataplane
3 *
4 * Copyright Red Hat, Inc. 2014
5 *
6 * Authors:
7 * Fam Zheng <famz@redhat.com>
8 *
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.
11 *
12 */
13
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"
22
23 /* Context: QEMU global mutex held */
24 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread)
25 {
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);
29
30 assert(!s->ctx);
31 s->ctx = iothread_get_aio_context(vs->conf.iothread);
32
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);
38 }
39 }
40
41 static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
42 VirtQueue *vq,
43 EventNotifierHandler *handler,
44 int n)
45 {
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;
50
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;
58 }
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);
62
63 r->parent = s;
64
65 if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
66 fprintf(stderr, "virtio-scsi: VRing setup failed\n");
67 goto fail_vring;
68 }
69 return r;
70
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;
76 }
77
78 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s,
79 VirtIOSCSIVring *vring)
80 {
81 VirtIOSCSIReq *req = virtio_scsi_init_req(s, NULL);
82 int r;
83
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;
89 }
90 return req;
91 }
92
93 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
94 {
95 vring_push(&req->vring->vring, &req->elem,
96 req->qsgl.size + req->resp_iov.size);
97 event_notifier_set(&req->vring->guest_notifier);
98 }
99
100 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
101 {
102 VirtIOSCSIVring *vring = container_of(notifier,
103 VirtIOSCSIVring, host_notifier);
104 VirtIOSCSI *s = VIRTIO_SCSI(vring->parent);
105 VirtIOSCSIReq *req;
106
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);
110 }
111 }
112
113 static void virtio_scsi_iothread_handle_event(EventNotifier *notifier)
114 {
115 VirtIOSCSIVring *vring = container_of(notifier,
116 VirtIOSCSIVring, host_notifier);
117 VirtIOSCSI *s = vring->parent;
118 VirtIODevice *vdev = VIRTIO_DEVICE(s);
119
120 event_notifier_test_and_clear(notifier);
121
122 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
123 return;
124 }
125
126 if (s->events_dropped) {
127 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
128 }
129 }
130
131 static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier)
132 {
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);
138
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);
143 }
144 }
145
146 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
147 virtio_scsi_handle_cmd_req_submit(s, req);
148 }
149 }
150
151 /* assumes s->ctx held */
152 static void virtio_scsi_clear_aio(VirtIOSCSI *s)
153 {
154 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
155 int i;
156
157 if (s->ctrl_vring) {
158 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
159 }
160 if (s->event_vring) {
161 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
162 }
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);
166 }
167 }
168 }
169
170 static void virtio_scsi_vring_teardown(VirtIOSCSI *s)
171 {
172 VirtIODevice *vdev = VIRTIO_DEVICE(s);
173 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
174 int i;
175
176 if (s->ctrl_vring) {
177 vring_teardown(&s->ctrl_vring->vring, vdev, 0);
178 }
179 if (s->event_vring) {
180 vring_teardown(&s->event_vring->vring, vdev, 1);
181 }
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);
185 }
186 free(s->cmd_vrings);
187 s->cmd_vrings = NULL;
188 }
189 }
190
191 /* Context: QEMU global mutex held */
192 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
193 {
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);
199
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;
205 }
206
207 s->dataplane_starting = true;
208
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;
218 }
219
220 aio_context_acquire(s->ctx);
221 s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
222 virtio_scsi_iothread_handle_ctrl,
223 0);
224 if (!s->ctrl_vring) {
225 goto fail_vrings;
226 }
227 s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
228 virtio_scsi_iothread_handle_event,
229 1);
230 if (!s->event_vring) {
231 goto fail_vrings;
232 }
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;
241 }
242 }
243
244 aio_context_release(s->ctx);
245 s->dataplane_starting = false;
246 s->dataplane_started = true;
247
248 fail_vrings:
249 virtio_scsi_clear_aio(s);
250 aio_context_release(s->ctx);
251 virtio_scsi_vring_teardown(s);
252 for (i = 0; i < vs->conf.num_queues + 2; i++) {
253 k->set_host_notifier(qbus->parent, i, false);
254 }
255 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
256 fail_guest_notifiers:
257 s->dataplane_starting = false;
258 }
259
260 /* Context: QEMU global mutex held */
261 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
262 {
263 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
264 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
265 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
266 int i;
267
268 /* Better luck next time. */
269 if (s->dataplane_fenced) {
270 s->dataplane_fenced = false;
271 return;
272 }
273 if (!s->dataplane_started || s->dataplane_stopping) {
274 return;
275 }
276 error_free(s->blocker);
277 s->blocker = NULL;
278 s->dataplane_stopping = true;
279 assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
280
281 aio_context_acquire(s->ctx);
282
283 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
284 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
285 for (i = 0; i < vs->conf.num_queues; i++) {
286 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL);
287 }
288
289 blk_drain_all(); /* ensure there are no in-flight requests */
290
291 aio_context_release(s->ctx);
292
293 /* Sync vring state back to virtqueue so that non-dataplane request
294 * processing can continue when we disable the host notifier below.
295 */
296 virtio_scsi_vring_teardown(s);
297
298 for (i = 0; i < vs->conf.num_queues + 2; i++) {
299 k->set_host_notifier(qbus->parent, i, false);
300 }
301
302 /* Clean up guest notifier (irq) */
303 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
304 s->dataplane_stopping = false;
305 s->dataplane_started = false;
306 }