xref: /openbmc/qemu/hw/scsi/virtio-scsi-dataplane.c (revision b5682aa4ca790dbcce16de6dc991c72bc298019a)
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 <hw/scsi/scsi.h>
17 #include <block/scsi.h>
18 #include <hw/virtio/virtio-bus.h>
19 #include "hw/virtio/virtio-access.h"
20 #include "stdio.h"
21 
22 /* Context: QEMU global mutex held */
23 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread)
24 {
25     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
26     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
27     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
28 
29     assert(!s->ctx);
30     s->ctx = iothread_get_aio_context(vs->conf.iothread);
31 
32     /* Don't try if transport does not support notifiers. */
33     if (!k->set_guest_notifiers || !k->set_host_notifier) {
34         fprintf(stderr, "virtio-scsi: Failed to set iothread "
35                    "(transport does not support notifiers)");
36         exit(1);
37     }
38 }
39 
40 static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
41                                                VirtQueue *vq,
42                                                EventNotifierHandler *handler,
43                                                int n)
44 {
45     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
46     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
47     VirtIOSCSIVring *r = g_slice_new(VirtIOSCSIVring);
48 
49     /* Set up virtqueue notify */
50     if (k->set_host_notifier(qbus->parent, n, true) != 0) {
51         fprintf(stderr, "virtio-scsi: Failed to set host notifier\n");
52         exit(1);
53     }
54     r->host_notifier = *virtio_queue_get_host_notifier(vq);
55     r->guest_notifier = *virtio_queue_get_guest_notifier(vq);
56     aio_set_event_notifier(s->ctx, &r->host_notifier, handler);
57 
58     r->parent = s;
59 
60     if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
61         fprintf(stderr, "virtio-scsi: VRing setup failed\n");
62         exit(1);
63     }
64     return r;
65 }
66 
67 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s,
68                                          VirtIOSCSIVring *vring)
69 {
70     VirtIOSCSIReq *req = virtio_scsi_init_req(s, NULL);
71     int r;
72 
73     req->vring = vring;
74     r = vring_pop((VirtIODevice *)s, &vring->vring, &req->elem);
75     if (r < 0) {
76         virtio_scsi_free_req(req);
77         req = NULL;
78     }
79     return req;
80 }
81 
82 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
83 {
84     vring_push(&req->vring->vring, &req->elem,
85                req->qsgl.size + req->resp_iov.size);
86     event_notifier_set(&req->vring->guest_notifier);
87 }
88 
89 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
90 {
91     VirtIOSCSIVring *vring = container_of(notifier,
92                                           VirtIOSCSIVring, host_notifier);
93     VirtIOSCSI *s = VIRTIO_SCSI(vring->parent);
94     VirtIOSCSIReq *req;
95 
96     event_notifier_test_and_clear(notifier);
97     while ((req = virtio_scsi_pop_req_vring(s, vring))) {
98         virtio_scsi_handle_ctrl_req(s, req);
99     }
100 }
101 
102 static void virtio_scsi_iothread_handle_event(EventNotifier *notifier)
103 {
104     VirtIOSCSIVring *vring = container_of(notifier,
105                                           VirtIOSCSIVring, host_notifier);
106     VirtIOSCSI *s = vring->parent;
107     VirtIODevice *vdev = VIRTIO_DEVICE(s);
108 
109     event_notifier_test_and_clear(notifier);
110 
111     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
112         return;
113     }
114 
115     if (s->events_dropped) {
116         virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
117     }
118 }
119 
120 static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier)
121 {
122     VirtIOSCSIVring *vring = container_of(notifier,
123                                           VirtIOSCSIVring, host_notifier);
124     VirtIOSCSI *s = (VirtIOSCSI *)vring->parent;
125     VirtIOSCSIReq *req, *next;
126     QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
127 
128     event_notifier_test_and_clear(notifier);
129     while ((req = virtio_scsi_pop_req_vring(s, vring))) {
130         if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
131             QTAILQ_INSERT_TAIL(&reqs, req, next);
132         }
133     }
134 
135     QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
136         virtio_scsi_handle_cmd_req_submit(s, req);
137     }
138 }
139 
140 /* Context: QEMU global mutex held */
141 void virtio_scsi_dataplane_start(VirtIOSCSI *s)
142 {
143     int i;
144     int rc;
145     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
146     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
147     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
148 
149     if (s->dataplane_started ||
150         s->dataplane_starting ||
151         s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
152         return;
153     }
154 
155     s->dataplane_starting = true;
156 
157     /* Set up guest notifier (irq) */
158     rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
159     if (rc != 0) {
160         fprintf(stderr, "virtio-scsi: Failed to set guest notifiers, "
161                 "ensure -enable-kvm is set\n");
162         exit(1);
163     }
164 
165     aio_context_acquire(s->ctx);
166     s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
167                                            virtio_scsi_iothread_handle_ctrl,
168                                            0);
169     s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
170                                             virtio_scsi_iothread_handle_event,
171                                             1);
172     s->cmd_vrings = g_malloc0(sizeof(VirtIOSCSIVring) * vs->conf.num_queues);
173     for (i = 0; i < vs->conf.num_queues; i++) {
174         s->cmd_vrings[i] =
175             virtio_scsi_vring_init(s, vs->cmd_vqs[i],
176                                    virtio_scsi_iothread_handle_cmd,
177                                    i + 2);
178     }
179 
180     aio_context_release(s->ctx);
181     s->dataplane_starting = false;
182     s->dataplane_started = true;
183 }
184 
185 /* Context: QEMU global mutex held */
186 void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
187 {
188     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
189     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
190     VirtIODevice *vdev = VIRTIO_DEVICE(s);
191     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
192     int i;
193 
194     if (!s->dataplane_started || s->dataplane_stopping) {
195         return;
196     }
197     s->dataplane_stopping = true;
198     assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
199 
200     aio_context_acquire(s->ctx);
201 
202     aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL);
203     aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL);
204     for (i = 0; i < vs->conf.num_queues; i++) {
205         aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL);
206     }
207 
208     bdrv_drain_all(); /* ensure there are no in-flight requests */
209 
210     aio_context_release(s->ctx);
211 
212     /* Sync vring state back to virtqueue so that non-dataplane request
213      * processing can continue when we disable the host notifier below.
214      */
215     vring_teardown(&s->ctrl_vring->vring, vdev, 0);
216     vring_teardown(&s->event_vring->vring, vdev, 1);
217     for (i = 0; i < vs->conf.num_queues; i++) {
218         vring_teardown(&s->cmd_vrings[i]->vring, vdev, 2 + i);
219     }
220 
221     for (i = 0; i < vs->conf.num_queues + 2; i++) {
222         k->set_host_notifier(qbus->parent, i, false);
223     }
224 
225     /* Clean up guest notifier (irq) */
226     k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
227     s->dataplane_stopping = false;
228     s->dataplane_started = false;
229 }
230