xref: /qemu/hw/scsi/virtio-scsi-dataplane.c (revision 61fc57bf)
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 "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"
23 
24 /* Context: QEMU global mutex held */
25 void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
26 {
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);
31 
32     if (vs->conf.iothread) {
33         if (!k->set_guest_notifiers || !k->ioeventfd_assign) {
34             error_setg(errp,
35                        "device is incompatible with iothread "
36                        "(transport does not support notifiers)");
37             return;
38         }
39         if (!virtio_device_ioeventfd_enabled(vdev)) {
40             error_setg(errp, "ioeventfd is required for iothread");
41             return;
42         }
43         s->ctx = iothread_get_aio_context(vs->conf.iothread);
44     } else {
45         if (!virtio_device_ioeventfd_enabled(vdev)) {
46             return;
47         }
48         s->ctx = qemu_get_aio_context();
49     }
50 }
51 
52 static bool virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev,
53                                               VirtQueue *vq)
54 {
55     bool progress = false;
56     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
57 
58     virtio_scsi_acquire(s);
59     if (!s->dataplane_fenced) {
60         assert(s->ctx && s->dataplane_started);
61         progress = virtio_scsi_handle_cmd_vq(s, vq);
62     }
63     virtio_scsi_release(s);
64     return progress;
65 }
66 
67 static bool virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev,
68                                                VirtQueue *vq)
69 {
70     bool progress = false;
71     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
72 
73     virtio_scsi_acquire(s);
74     if (!s->dataplane_fenced) {
75         assert(s->ctx && s->dataplane_started);
76         progress = virtio_scsi_handle_ctrl_vq(s, vq);
77     }
78     virtio_scsi_release(s);
79     return progress;
80 }
81 
82 static bool virtio_scsi_data_plane_handle_event(VirtIODevice *vdev,
83                                                 VirtQueue *vq)
84 {
85     bool progress = false;
86     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
87 
88     virtio_scsi_acquire(s);
89     if (!s->dataplane_fenced) {
90         assert(s->ctx && s->dataplane_started);
91         progress = virtio_scsi_handle_event_vq(s, vq);
92     }
93     virtio_scsi_release(s);
94     return progress;
95 }
96 
97 static int virtio_scsi_set_host_notifier(VirtIOSCSI *s, VirtQueue *vq, int n)
98 {
99     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
100     int rc;
101 
102     /* Set up virtqueue notify */
103     rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true);
104     if (rc != 0) {
105         fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
106                 rc);
107         s->dataplane_fenced = true;
108         return rc;
109     }
110 
111     return 0;
112 }
113 
114 /* Context: BH in IOThread */
115 static void virtio_scsi_dataplane_stop_bh(void *opaque)
116 {
117     VirtIOSCSI *s = opaque;
118     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
119     int i;
120 
121     virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx, NULL);
122     virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx, NULL);
123     for (i = 0; i < vs->conf.num_queues; i++) {
124         virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL);
125     }
126 }
127 
128 /* Context: QEMU global mutex held */
129 int virtio_scsi_dataplane_start(VirtIODevice *vdev)
130 {
131     int i;
132     int rc;
133     int vq_init_count = 0;
134     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
135     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
136     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
137     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
138 
139     if (s->dataplane_started ||
140         s->dataplane_starting ||
141         s->dataplane_fenced) {
142         return 0;
143     }
144 
145     s->dataplane_starting = true;
146 
147     /* Set up guest notifier (irq) */
148     rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
149     if (rc != 0) {
150         error_report("virtio-scsi: Failed to set guest notifiers (%d), "
151                      "ensure -accel kvm is set.", rc);
152         goto fail_guest_notifiers;
153     }
154 
155     rc = virtio_scsi_set_host_notifier(s, vs->ctrl_vq, 0);
156     if (rc != 0) {
157         goto fail_host_notifiers;
158     }
159 
160     vq_init_count++;
161     rc = virtio_scsi_set_host_notifier(s, vs->event_vq, 1);
162     if (rc != 0) {
163         goto fail_host_notifiers;
164     }
165 
166     vq_init_count++;
167 
168     for (i = 0; i < vs->conf.num_queues; i++) {
169         rc = virtio_scsi_set_host_notifier(s, vs->cmd_vqs[i], i + 2);
170         if (rc) {
171             goto fail_host_notifiers;
172         }
173         vq_init_count++;
174     }
175 
176     aio_context_acquire(s->ctx);
177     virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx,
178                                             virtio_scsi_data_plane_handle_ctrl);
179     virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx,
180                                            virtio_scsi_data_plane_handle_event);
181 
182     for (i = 0; i < vs->conf.num_queues; i++) {
183         virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx,
184                                              virtio_scsi_data_plane_handle_cmd);
185     }
186 
187     s->dataplane_starting = false;
188     s->dataplane_started = true;
189     aio_context_release(s->ctx);
190     return 0;
191 
192 fail_host_notifiers:
193     for (i = 0; i < vq_init_count; i++) {
194         virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
195         virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
196     }
197     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
198 fail_guest_notifiers:
199     s->dataplane_fenced = true;
200     s->dataplane_starting = false;
201     s->dataplane_started = true;
202     return -ENOSYS;
203 }
204 
205 /* Context: QEMU global mutex held */
206 void virtio_scsi_dataplane_stop(VirtIODevice *vdev)
207 {
208     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
209     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
210     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
211     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
212     int i;
213 
214     if (!s->dataplane_started || s->dataplane_stopping) {
215         return;
216     }
217 
218     /* Better luck next time. */
219     if (s->dataplane_fenced) {
220         s->dataplane_fenced = false;
221         s->dataplane_started = false;
222         return;
223     }
224     s->dataplane_stopping = true;
225 
226     aio_context_acquire(s->ctx);
227     aio_wait_bh_oneshot(s->ctx, virtio_scsi_dataplane_stop_bh, s);
228     aio_context_release(s->ctx);
229 
230     blk_drain_all(); /* ensure there are no in-flight requests */
231 
232     for (i = 0; i < vs->conf.num_queues + 2; i++) {
233         virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
234         virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
235     }
236 
237     /* Clean up guest notifier (irq) */
238     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
239     s->dataplane_stopping = false;
240     s->dataplane_started = false;
241 }
242