]> git.proxmox.com Git - mirror_qemu.git/blob - hw/scsi/virtio-scsi-dataplane.c
Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' 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 VirtIODevice *vdev = VIRTIO_DEVICE(req->vring->parent);
96
97 vring_push(vdev, &req->vring->vring, &req->elem,
98 req->qsgl.size + req->resp_iov.size);
99
100 if (vring_should_notify(vdev, &req->vring->vring)) {
101 event_notifier_set(&req->vring->guest_notifier);
102 }
103 }
104
105 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
106 {
107 VirtIOSCSIVring *vring = container_of(notifier,
108 VirtIOSCSIVring, host_notifier);
109 VirtIOSCSI *s = VIRTIO_SCSI(vring->parent);
110 VirtIOSCSIReq *req;
111
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);
115 }
116 }
117
118 static void virtio_scsi_iothread_handle_event(EventNotifier *notifier)
119 {
120 VirtIOSCSIVring *vring = container_of(notifier,
121 VirtIOSCSIVring, host_notifier);
122 VirtIOSCSI *s = vring->parent;
123 VirtIODevice *vdev = VIRTIO_DEVICE(s);
124
125 event_notifier_test_and_clear(notifier);
126
127 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
128 return;
129 }
130
131 if (s->events_dropped) {
132 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
133 }
134 }
135
136 static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier)
137 {
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);
143
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);
148 }
149 }
150
151 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
152 virtio_scsi_handle_cmd_req_submit(s, req);
153 }
154 }
155
156 /* assumes s->ctx held */
157 static void virtio_scsi_clear_aio(VirtIOSCSI *s)
158 {
159 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
160 int i;
161
162 if (s->ctrl_vring) {
163 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
164 }
165 if (s->event_vring) {
166 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
167 }
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);
171 }
172 }
173 }
174
175 static void virtio_scsi_vring_teardown(VirtIOSCSI *s)
176 {
177 VirtIODevice *vdev = VIRTIO_DEVICE(s);
178 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
179 int i;
180
181 if (s->ctrl_vring) {
182 vring_teardown(&s->ctrl_vring->vring, vdev, 0);
183 }
184 if (s->event_vring) {
185 vring_teardown(&s->event_vring->vring, vdev, 1);
186 }
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);
190 }
191 free(s->cmd_vrings);
192 s->cmd_vrings = NULL;
193 }
194 }
195
196 /* Context: QEMU global mutex held */
197 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
198 {
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);
204
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;
210 }
211
212 s->dataplane_starting = true;
213
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;
223 }
224
225 aio_context_acquire(s->ctx);
226 s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
227 virtio_scsi_iothread_handle_ctrl,
228 0);
229 if (!s->ctrl_vring) {
230 goto fail_vrings;
231 }
232 s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
233 virtio_scsi_iothread_handle_event,
234 1);
235 if (!s->event_vring) {
236 goto fail_vrings;
237 }
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;
246 }
247 }
248
249 s->dataplane_starting = false;
250 s->dataplane_started = true;
251 aio_context_release(s->ctx);
252 return;
253
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);
260 }
261 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
262 fail_guest_notifiers:
263 s->dataplane_starting = false;
264 }
265
266 /* Context: QEMU global mutex held */
267 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
268 {
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;
273
274 /* Better luck next time. */
275 if (s->dataplane_fenced) {
276 s->dataplane_fenced = false;
277 return;
278 }
279 if (!s->dataplane_started || s->dataplane_stopping) {
280 return;
281 }
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));
286
287 aio_context_acquire(s->ctx);
288
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);
293 }
294
295 blk_drain_all(); /* ensure there are no in-flight requests */
296
297 aio_context_release(s->ctx);
298
299 /* Sync vring state back to virtqueue so that non-dataplane request
300 * processing can continue when we disable the host notifier below.
301 */
302 virtio_scsi_vring_teardown(s);
303
304 for (i = 0; i < vs->conf.num_queues + 2; i++) {
305 k->set_host_notifier(qbus->parent, i, false);
306 }
307
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;
312 }