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 "hw/virtio/virtio-scsi.h"
16 #include "qemu/error-report.h"
17 #include "sysemu/block-backend.h"
18 #include <hw/scsi/scsi.h>
19 #include <block/scsi.h>
20 #include <hw/virtio/virtio-bus.h>
21 #include "hw/virtio/virtio-access.h"
23 /* Context: QEMU global mutex held */
24 void virtio_scsi_set_iothread(VirtIOSCSI
*s
, IOThread
*iothread
)
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
);
31 s
->ctx
= iothread_get_aio_context(vs
->conf
.iothread
);
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)");
41 static void virtio_scsi_data_plane_handle_cmd(VirtIODevice
*vdev
,
44 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
46 assert(s
->ctx
&& s
->dataplane_started
);
47 virtio_scsi_handle_cmd_vq(s
, vq
);
50 static void virtio_scsi_data_plane_handle_ctrl(VirtIODevice
*vdev
,
53 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
55 assert(s
->ctx
&& s
->dataplane_started
);
56 virtio_scsi_handle_ctrl_vq(s
, vq
);
59 static void virtio_scsi_data_plane_handle_event(VirtIODevice
*vdev
,
62 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
64 assert(s
->ctx
&& s
->dataplane_started
);
65 virtio_scsi_handle_event_vq(s
, vq
);
68 static int virtio_scsi_vring_init(VirtIOSCSI
*s
, VirtQueue
*vq
, int n
,
69 void (*fn
)(VirtIODevice
*vdev
, VirtQueue
*vq
))
71 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
)));
72 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
75 /* Set up virtqueue notify */
76 rc
= k
->set_host_notifier(qbus
->parent
, n
, true);
78 fprintf(stderr
, "virtio-scsi: Failed to set host notifier (%d)\n",
80 s
->dataplane_fenced
= true;
84 virtio_queue_aio_set_host_notifier_handler(vq
, s
->ctx
, fn
);
88 void virtio_scsi_dataplane_notify(VirtIODevice
*vdev
, VirtIOSCSIReq
*req
)
90 if (virtio_should_notify(vdev
, req
->vq
)) {
91 event_notifier_set(virtio_queue_get_guest_notifier(req
->vq
));
95 /* assumes s->ctx held */
96 static void virtio_scsi_clear_aio(VirtIOSCSI
*s
)
98 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
101 virtio_queue_aio_set_host_notifier_handler(vs
->ctrl_vq
, s
->ctx
, NULL
);
102 virtio_queue_aio_set_host_notifier_handler(vs
->event_vq
, s
->ctx
, NULL
);
103 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
104 virtio_queue_aio_set_host_notifier_handler(vs
->cmd_vqs
[i
], s
->ctx
, NULL
);
108 /* Context: QEMU global mutex held */
109 void virtio_scsi_dataplane_start(VirtIOSCSI
*s
)
113 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
)));
114 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
115 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
117 if (s
->dataplane_started
||
118 s
->dataplane_starting
||
119 s
->dataplane_fenced
||
120 s
->ctx
!= iothread_get_aio_context(vs
->conf
.iothread
)) {
124 s
->dataplane_starting
= true;
126 /* Set up guest notifier (irq) */
127 rc
= k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, true);
129 fprintf(stderr
, "virtio-scsi: Failed to set guest notifiers (%d), "
130 "ensure -enable-kvm is set\n", rc
);
131 goto fail_guest_notifiers
;
134 aio_context_acquire(s
->ctx
);
135 rc
= virtio_scsi_vring_init(s
, vs
->ctrl_vq
, 0,
136 virtio_scsi_data_plane_handle_ctrl
);
140 rc
= virtio_scsi_vring_init(s
, vs
->event_vq
, 1,
141 virtio_scsi_data_plane_handle_event
);
145 for (i
= 0; i
< vs
->conf
.num_queues
; i
++) {
146 rc
= virtio_scsi_vring_init(s
, vs
->cmd_vqs
[i
], i
+ 2,
147 virtio_scsi_data_plane_handle_cmd
);
153 s
->dataplane_starting
= false;
154 s
->dataplane_started
= true;
155 aio_context_release(s
->ctx
);
159 virtio_scsi_clear_aio(s
);
160 aio_context_release(s
->ctx
);
161 for (i
= 0; i
< vs
->conf
.num_queues
+ 2; i
++) {
162 k
->set_host_notifier(qbus
->parent
, i
, false);
164 k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, false);
165 fail_guest_notifiers
:
166 s
->dataplane_fenced
= true;
167 s
->dataplane_starting
= false;
168 s
->dataplane_started
= true;
171 /* Context: QEMU global mutex held */
172 void virtio_scsi_dataplane_stop(VirtIOSCSI
*s
)
174 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(s
)));
175 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
176 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
179 if (!s
->dataplane_started
|| s
->dataplane_stopping
) {
183 /* Better luck next time. */
184 if (s
->dataplane_fenced
) {
185 s
->dataplane_fenced
= false;
186 s
->dataplane_started
= false;
189 s
->dataplane_stopping
= true;
190 assert(s
->ctx
== iothread_get_aio_context(vs
->conf
.iothread
));
192 aio_context_acquire(s
->ctx
);
194 virtio_scsi_clear_aio(s
);
196 blk_drain_all(); /* ensure there are no in-flight requests */
198 aio_context_release(s
->ctx
);
200 for (i
= 0; i
< vs
->conf
.num_queues
+ 2; i
++) {
201 k
->set_host_notifier(qbus
->parent
, i
, false);
204 /* Clean up guest notifier (irq) */
205 k
->set_guest_notifiers(qbus
->parent
, vs
->conf
.num_queues
+ 2, false);
206 s
->dataplane_stopping
= false;
207 s
->dataplane_started
= false;