]> git.proxmox.com Git - mirror_qemu.git/blob - hw/scsi/virtio-scsi-dataplane.c
virtio-scsi: dataplane: print why starting failed
[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 exit(1);
57 }
58 r->host_notifier = *virtio_queue_get_host_notifier(vq);
59 r->guest_notifier = *virtio_queue_get_guest_notifier(vq);
60 aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
61
62 r->parent = s;
63
64 if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
65 fprintf(stderr, "virtio-scsi: VRing setup failed\n");
66 exit(1);
67 }
68 return r;
69 }
70
71 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s,
72 VirtIOSCSIVring *vring)
73 {
74 VirtIOSCSIReq *req = virtio_scsi_init_req(s, NULL);
75 int r;
76
77 req->vring = vring;
78 r = vring_pop((VirtIODevice *)s, &vring->vring, &req->elem);
79 if (r < 0) {
80 virtio_scsi_free_req(req);
81 req = NULL;
82 }
83 return req;
84 }
85
86 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
87 {
88 vring_push(&req->vring->vring, &req->elem,
89 req->qsgl.size + req->resp_iov.size);
90 event_notifier_set(&req->vring->guest_notifier);
91 }
92
93 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
94 {
95 VirtIOSCSIVring *vring = container_of(notifier,
96 VirtIOSCSIVring, host_notifier);
97 VirtIOSCSI *s = VIRTIO_SCSI(vring->parent);
98 VirtIOSCSIReq *req;
99
100 event_notifier_test_and_clear(notifier);
101 while ((req = virtio_scsi_pop_req_vring(s, vring))) {
102 virtio_scsi_handle_ctrl_req(s, req);
103 }
104 }
105
106 static void virtio_scsi_iothread_handle_event(EventNotifier *notifier)
107 {
108 VirtIOSCSIVring *vring = container_of(notifier,
109 VirtIOSCSIVring, host_notifier);
110 VirtIOSCSI *s = vring->parent;
111 VirtIODevice *vdev = VIRTIO_DEVICE(s);
112
113 event_notifier_test_and_clear(notifier);
114
115 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
116 return;
117 }
118
119 if (s->events_dropped) {
120 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
121 }
122 }
123
124 static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier)
125 {
126 VirtIOSCSIVring *vring = container_of(notifier,
127 VirtIOSCSIVring, host_notifier);
128 VirtIOSCSI *s = (VirtIOSCSI *)vring->parent;
129 VirtIOSCSIReq *req, *next;
130 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
131
132 event_notifier_test_and_clear(notifier);
133 while ((req = virtio_scsi_pop_req_vring(s, vring))) {
134 if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
135 QTAILQ_INSERT_TAIL(&reqs, req, next);
136 }
137 }
138
139 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
140 virtio_scsi_handle_cmd_req_submit(s, req);
141 }
142 }
143
144 /* Context: QEMU global mutex held */
145 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
146 {
147 int i;
148 int rc;
149 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
150 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
151 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
152
153 if (s->dataplane_started ||
154 s->dataplane_starting ||
155 s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
156 return;
157 }
158
159 s->dataplane_starting = true;
160
161 assert(!s->blocker);
162 error_setg(&s->blocker, "block device is in use by data plane");
163 /* Set up guest notifier (irq) */
164 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
165 if (rc != 0) {
166 fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
167 "ensure -enable-kvm is set\n", rc);
168 exit(1);
169 }
170
171 aio_context_acquire(s->ctx);
172 s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
173 virtio_scsi_iothread_handle_ctrl,
174 0);
175 s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
176 virtio_scsi_iothread_handle_event,
177 1);
178 s->cmd_vrings = g_malloc0(sizeof(VirtIOSCSIVring) * vs->conf.num_queues);
179 for (i = 0; i < vs->conf.num_queues; i++) {
180 s->cmd_vrings[i] =
181 virtio_scsi_vring_init(s, vs->cmd_vqs[i],
182 virtio_scsi_iothread_handle_cmd,
183 i + 2);
184 }
185
186 aio_context_release(s->ctx);
187 s->dataplane_starting = false;
188 s->dataplane_started = true;
189 }
190
191 /* Context: QEMU global mutex held */
192 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
193 {
194 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
195 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
196 VirtIODevice *vdev = VIRTIO_DEVICE(s);
197 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
198 int i;
199
200 if (!s->dataplane_started || s->dataplane_stopping) {
201 return;
202 }
203 error_free(s->blocker);
204 s->blocker = NULL;
205 s->dataplane_stopping = true;
206 assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
207
208 aio_context_acquire(s->ctx);
209
210 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
211 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
212 for (i = 0; i < vs->conf.num_queues; i++) {
213 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL);
214 }
215
216 blk_drain_all(); /* ensure there are no in-flight requests */
217
218 aio_context_release(s->ctx);
219
220 /* Sync vring state back to virtqueue so that non-dataplane request
221 * processing can continue when we disable the host notifier below.
222 */
223 vring_teardown(&s->ctrl_vring->vring, vdev, 0);
224 vring_teardown(&s->event_vring->vring, vdev, 1);
225 for (i = 0; i < vs->conf.num_queues; i++) {
226 vring_teardown(&s->cmd_vrings[i]->vring, vdev, 2 + i);
227 }
228
229 for (i = 0; i < vs->conf.num_queues + 2; i++) {
230 k->set_host_notifier(qbus->parent, i, false);
231 }
232
233 /* Clean up guest notifier (irq) */
234 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
235 s->dataplane_stopping = false;
236 s->dataplane_started = false;
237 }