2 * Virtio SCSI dataplane
4 * Copyright Red Hat, Inc. 2014
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 "qapi/error.h"
16 #include "hw/virtio/virtio-scsi.h"
17 #include "qemu/error-report.h"
18 #include "sysemu/block-backend.h"
19 #include "hw/scsi/scsi.h"
20 #include "scsi/constants.h"
21 #include "hw/virtio/virtio-bus.h"
22 #include "hw/virtio/virtio-access.h"
24 /* Context: QEMU global mutex held */
25 void virtio_scsi_dataplane_setup(VirtIOSCSI
*s
, Error
**errp
)
27 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
28 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
29 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
30 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
32 if (vs
->conf
.iothread
) {
33 if (!k
->set_guest_notifiers
|| !k
->ioeventfd_assign
) {
35 "device is incompatible with iothread "
36 "(transport does not support notifiers)");
39 if (!virtio_device_ioeventfd_enabled(vdev
)) {
40 error_setg(errp
, "ioeventfd is required for iothread");
43 s
->ctx
= iothread_get_aio_context(vs
->conf
.iothread
);
45 if (!virtio_device_ioeventfd_enabled(vdev
)) {
48 s
->ctx
= qemu_get_aio_context();
52 static bool virtio_scsi_data_plane_handle_cmd(VirtIODevice
*vdev
,
56 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
58 virtio_scsi_acquire(s
);
59 assert(s
->ctx
&& s
->dataplane_started
);
60 progress
= virtio_scsi_handle_cmd_vq(s
, vq
);
61 virtio_scsi_release(s
);
65 static bool virtio_scsi_data_plane_handle_ctrl(VirtIODevice
*vdev
,
69 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
71 virtio_scsi_acquire(s
);
72 assert(s
->ctx
&& s
->dataplane_started
);
73 progress
= virtio_scsi_handle_ctrl_vq(s
, vq
);
74 virtio_scsi_release(s
);
78 static bool virtio_scsi_data_plane_handle_event(VirtIODevice
*vdev
,
82 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
84 virtio_scsi_acquire(s
);
85 assert(s
->ctx
&& s
->dataplane_started
);
86 progress
= virtio_scsi_handle_event_vq(s
, vq
);
87 virtio_scsi_release(s
);
91 static int virtio_scsi_vring_init(VirtIOSCSI
*s
, VirtQueue
*vq
, int n
,
92 VirtIOHandleAIOOutput fn
)
94 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
)));
97 /* Set up virtqueue notify */
98 rc
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), n
, true);
100 fprintf(stderr
, "virtio-scsi: Failed to set host notifier (%d)\n",
102 s
->dataplane_fenced
= true;
106 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
, fn
);
110 /* assumes s->ctx held */
111 static void virtio_scsi_clear_aio(VirtIOSCSI
*s
)
113 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
116 virtio_queue_aio_set_host_notifier_handler(vs
->ctrl_vq
, s
->ctx
, NULL
);
117 virtio_queue_aio_set_host_notifier_handler(vs
->event_vq
, s
->ctx
, NULL
);
118 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
119 virtio_queue_aio_set_host_notifier_handler(vs
->cmd_vqs
[i
], s
->ctx
, NULL
);
123 /* Context: QEMU global mutex held */
124 int virtio_scsi_dataplane_start(VirtIODevice
*vdev
)
128 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
129 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
130 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
131 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
133 if (s
->dataplane_started
||
134 s
->dataplane_starting
||
135 s
->dataplane_fenced
) {
139 s
->dataplane_starting
= true;
141 /* Set up guest notifier (irq) */
142 rc
= k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, true);
144 fprintf(stderr
, "virtio-scsi: Failed to set guest notifiers (%d), "
145 "ensure -enable-kvm is set\n", rc
);
146 goto fail_guest_notifiers
;
149 aio_context_acquire(s
->ctx
);
150 rc
= virtio_scsi_vring_init(s
, vs
->ctrl_vq
, 0,
151 virtio_scsi_data_plane_handle_ctrl
);
155 rc
= virtio_scsi_vring_init(s
, vs
->event_vq
, 1,
156 virtio_scsi_data_plane_handle_event
);
160 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
161 rc
= virtio_scsi_vring_init(s
, vs
->cmd_vqs
[i
], i
+ 2,
162 virtio_scsi_data_plane_handle_cmd
);
168 s
->dataplane_starting
= false;
169 s
->dataplane_started
= true;
170 aio_context_release(s
->ctx
);
174 virtio_scsi_clear_aio(s
);
175 aio_context_release(s
->ctx
);
176 for (i
= 0; i
< vs
->conf
.num_queues
+ 2; i
++) {
177 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
179 k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, false);
180 fail_guest_notifiers
:
181 s
->dataplane_fenced
= true;
182 s
->dataplane_starting
= false;
183 s
->dataplane_started
= true;
187 /* Context: QEMU global mutex held */
188 void virtio_scsi_dataplane_stop(VirtIODevice
*vdev
)
190 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
191 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
192 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
193 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
196 if (!s
->dataplane_started
|| s
->dataplane_stopping
) {
200 /* Better luck next time. */
201 if (s
->dataplane_fenced
) {
202 s
->dataplane_fenced
= false;
203 s
->dataplane_started
= false;
206 s
->dataplane_stopping
= true;
208 aio_context_acquire(s
->ctx
);
209 virtio_scsi_clear_aio(s
);
210 aio_context_release(s
->ctx
);
212 blk_drain_all(); /* ensure there are no in-flight requests */
214 for (i
= 0; i
< vs
->conf
.num_queues
+ 2; i
++) {
215 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
218 /* Clean up guest notifier (irq) */
219 k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, false);
220 s
->dataplane_stopping
= false;
221 s
->dataplane_started
= false;