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 "block/scsi.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
,
55 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
57 assert(s
->ctx
&& s
->dataplane_started
);
58 return virtio_scsi_handle_cmd_vq(s
, vq
);
61 static bool virtio_scsi_data_plane_handle_ctrl(VirtIODevice
*vdev
,
64 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
66 assert(s
->ctx
&& s
->dataplane_started
);
67 return virtio_scsi_handle_ctrl_vq(s
, vq
);
70 static bool virtio_scsi_data_plane_handle_event(VirtIODevice
*vdev
,
73 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
75 assert(s
->ctx
&& s
->dataplane_started
);
76 return virtio_scsi_handle_event_vq(s
, vq
);
79 static int virtio_scsi_vring_init(VirtIOSCSI
*s
, VirtQueue
*vq
, int n
,
80 VirtIOHandleAIOOutput fn
)
82 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
)));
85 /* Set up virtqueue notify */
86 rc
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), n
, true);
88 fprintf(stderr
, "virtio-scsi: Failed to set host notifier (%d)\n",
90 s
->dataplane_fenced
= true;
94 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
, fn
);
98 /* assumes s->ctx held */
99 static void virtio_scsi_clear_aio(VirtIOSCSI
*s
)
101 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
104 virtio_queue_aio_set_host_notifier_handler(vs
->ctrl_vq
, s
->ctx
, NULL
);
105 virtio_queue_aio_set_host_notifier_handler(vs
->event_vq
, s
->ctx
, NULL
);
106 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
107 virtio_queue_aio_set_host_notifier_handler(vs
->cmd_vqs
[i
], s
->ctx
, NULL
);
111 /* Context: QEMU global mutex held */
112 int virtio_scsi_dataplane_start(VirtIODevice
*vdev
)
116 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
117 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
118 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
119 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
121 if (s
->dataplane_started
||
122 s
->dataplane_starting
||
123 s
->dataplane_fenced
) {
127 s
->dataplane_starting
= true;
129 /* Set up guest notifier (irq) */
130 rc
= k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, true);
132 fprintf(stderr
, "virtio-scsi: Failed to set guest notifiers (%d), "
133 "ensure -enable-kvm is set\n", rc
);
134 goto fail_guest_notifiers
;
137 aio_context_acquire(s
->ctx
);
138 rc
= virtio_scsi_vring_init(s
, vs
->ctrl_vq
, 0,
139 virtio_scsi_data_plane_handle_ctrl
);
143 rc
= virtio_scsi_vring_init(s
, vs
->event_vq
, 1,
144 virtio_scsi_data_plane_handle_event
);
148 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
149 rc
= virtio_scsi_vring_init(s
, vs
->cmd_vqs
[i
], i
+ 2,
150 virtio_scsi_data_plane_handle_cmd
);
156 s
->dataplane_starting
= false;
157 s
->dataplane_started
= true;
158 aio_context_release(s
->ctx
);
162 virtio_scsi_clear_aio(s
);
163 aio_context_release(s
->ctx
);
164 for (i
= 0; i
< vs
->conf
.num_queues
+ 2; i
++) {
165 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
167 k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, false);
168 fail_guest_notifiers
:
169 s
->dataplane_fenced
= true;
170 s
->dataplane_starting
= false;
171 s
->dataplane_started
= true;
175 /* Context: QEMU global mutex held */
176 void virtio_scsi_dataplane_stop(VirtIODevice
*vdev
)
178 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
179 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
180 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
181 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
184 if (!s
->dataplane_started
|| s
->dataplane_stopping
) {
188 /* Better luck next time. */
189 if (s
->dataplane_fenced
) {
190 s
->dataplane_fenced
= false;
191 s
->dataplane_started
= false;
194 s
->dataplane_stopping
= true;
196 aio_context_acquire(s
->ctx
);
197 virtio_scsi_clear_aio(s
);
198 aio_context_release(s
->ctx
);
200 blk_drain_all(); /* ensure there are no in-flight requests */
202 for (i
= 0; i
< vs
->conf
.num_queues
+ 2; i
++) {
203 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), i
, false);
206 /* Clean up guest notifier (irq) */
207 k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, false);
208 s
->dataplane_stopping
= false;
209 s
->dataplane_started
= false;