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 "sysemu/block-backend.h" 17 #include <hw/scsi/scsi.h> 18 #include <block/scsi.h> 19 #include <hw/virtio/virtio-bus.h> 20 #include "hw/virtio/virtio-access.h" 21 #include "stdio.h" 22 23 /* Context: QEMU global mutex held */ 24 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread) 25 { 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); 29 30 assert(!s->ctx); 31 s->ctx = iothread_get_aio_context(vs->conf.iothread); 32 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)"); 37 exit(1); 38 } 39 } 40 41 static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s, 42 VirtQueue *vq, 43 EventNotifierHandler *handler, 44 int n) 45 { 46 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); 47 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 48 VirtIOSCSIVring *r = g_slice_new(VirtIOSCSIVring); 49 int rc; 50 51 /* Set up virtqueue notify */ 52 rc = k->set_host_notifier(qbus->parent, n, true); 53 if (rc != 0) { 54 fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n", 55 rc); 56 s->dataplane_fenced = true; 57 return NULL; 58 } 59 r->host_notifier = *virtio_queue_get_host_notifier(vq); 60 r->guest_notifier = *virtio_queue_get_guest_notifier(vq); 61 aio_set_event_notifier(s->ctx, &r->host_notifier, handler); 62 63 r->parent = s; 64 65 if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) { 66 fprintf(stderr, "virtio-scsi: VRing setup failed\n"); 67 goto fail_vring; 68 } 69 return r; 70 71 fail_vring: 72 aio_set_event_notifier(s->ctx, &r->host_notifier, NULL); 73 k->set_host_notifier(qbus->parent, n, false); 74 g_slice_free(VirtIOSCSIVring, r); 75 return NULL; 76 } 77 78 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s, 79 VirtIOSCSIVring *vring) 80 { 81 VirtIOSCSIReq *req = virtio_scsi_init_req(s, NULL); 82 int r; 83 84 req->vring = vring; 85 r = vring_pop((VirtIODevice *)s, &vring->vring, &req->elem); 86 if (r < 0) { 87 virtio_scsi_free_req(req); 88 req = NULL; 89 } 90 return req; 91 } 92 93 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req) 94 { 95 VirtIODevice *vdev = VIRTIO_DEVICE(req->vring->parent); 96 97 vring_push(vdev, &req->vring->vring, &req->elem, 98 req->qsgl.size + req->resp_iov.size); 99 100 if (vring_should_notify(vdev, &req->vring->vring)) { 101 event_notifier_set(&req->vring->guest_notifier); 102 } 103 } 104 105 static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier) 106 { 107 VirtIOSCSIVring *vring = container_of(notifier, 108 VirtIOSCSIVring, host_notifier); 109 VirtIOSCSI *s = VIRTIO_SCSI(vring->parent); 110 VirtIOSCSIReq *req; 111 112 event_notifier_test_and_clear(notifier); 113 while ((req = virtio_scsi_pop_req_vring(s, vring))) { 114 virtio_scsi_handle_ctrl_req(s, req); 115 } 116 } 117 118 static void virtio_scsi_iothread_handle_event(EventNotifier *notifier) 119 { 120 VirtIOSCSIVring *vring = container_of(notifier, 121 VirtIOSCSIVring, host_notifier); 122 VirtIOSCSI *s = vring->parent; 123 VirtIODevice *vdev = VIRTIO_DEVICE(s); 124 125 event_notifier_test_and_clear(notifier); 126 127 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 128 return; 129 } 130 131 if (s->events_dropped) { 132 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); 133 } 134 } 135 136 static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier) 137 { 138 VirtIOSCSIVring *vring = container_of(notifier, 139 VirtIOSCSIVring, host_notifier); 140 VirtIOSCSI *s = (VirtIOSCSI *)vring->parent; 141 VirtIOSCSIReq *req, *next; 142 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs); 143 144 event_notifier_test_and_clear(notifier); 145 while ((req = virtio_scsi_pop_req_vring(s, vring))) { 146 if (virtio_scsi_handle_cmd_req_prepare(s, req)) { 147 QTAILQ_INSERT_TAIL(&reqs, req, next); 148 } 149 } 150 151 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) { 152 virtio_scsi_handle_cmd_req_submit(s, req); 153 } 154 } 155 156 /* assumes s->ctx held */ 157 static void virtio_scsi_clear_aio(VirtIOSCSI *s) 158 { 159 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 160 int i; 161 162 if (s->ctrl_vring) { 163 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL); 164 } 165 if (s->event_vring) { 166 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL); 167 } 168 if (s->cmd_vrings) { 169 for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) { 170 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL); 171 } 172 } 173 } 174 175 static void virtio_scsi_vring_teardown(VirtIOSCSI *s) 176 { 177 VirtIODevice *vdev = VIRTIO_DEVICE(s); 178 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 179 int i; 180 181 if (s->ctrl_vring) { 182 vring_teardown(&s->ctrl_vring->vring, vdev, 0); 183 } 184 if (s->event_vring) { 185 vring_teardown(&s->event_vring->vring, vdev, 1); 186 } 187 if (s->cmd_vrings) { 188 for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) { 189 vring_teardown(&s->cmd_vrings[i]->vring, vdev, 2 + i); 190 } 191 free(s->cmd_vrings); 192 s->cmd_vrings = NULL; 193 } 194 } 195 196 /* Context: QEMU global mutex held */ 197 void virtio_scsi_dataplane_start(VirtIOSCSI *s) 198 { 199 int i; 200 int rc; 201 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); 202 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 203 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 204 205 if (s->dataplane_started || 206 s->dataplane_starting || 207 s->dataplane_fenced || 208 s->ctx != iothread_get_aio_context(vs->conf.iothread)) { 209 return; 210 } 211 212 s->dataplane_starting = true; 213 214 /* Set up guest notifier (irq) */ 215 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true); 216 if (rc != 0) { 217 fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), " 218 "ensure -enable-kvm is set\n", rc); 219 s->dataplane_fenced = true; 220 goto fail_guest_notifiers; 221 } 222 223 aio_context_acquire(s->ctx); 224 s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq, 225 virtio_scsi_iothread_handle_ctrl, 226 0); 227 if (!s->ctrl_vring) { 228 goto fail_vrings; 229 } 230 s->event_vring = virtio_scsi_vring_init(s, vs->event_vq, 231 virtio_scsi_iothread_handle_event, 232 1); 233 if (!s->event_vring) { 234 goto fail_vrings; 235 } 236 s->cmd_vrings = g_new(VirtIOSCSIVring *, vs->conf.num_queues); 237 for (i = 0; i < vs->conf.num_queues; i++) { 238 s->cmd_vrings[i] = 239 virtio_scsi_vring_init(s, vs->cmd_vqs[i], 240 virtio_scsi_iothread_handle_cmd, 241 i + 2); 242 if (!s->cmd_vrings[i]) { 243 goto fail_vrings; 244 } 245 } 246 247 s->dataplane_starting = false; 248 s->dataplane_started = true; 249 aio_context_release(s->ctx); 250 return; 251 252 fail_vrings: 253 virtio_scsi_clear_aio(s); 254 aio_context_release(s->ctx); 255 virtio_scsi_vring_teardown(s); 256 for (i = 0; i < vs->conf.num_queues + 2; i++) { 257 k->set_host_notifier(qbus->parent, i, false); 258 } 259 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 260 fail_guest_notifiers: 261 s->dataplane_starting = false; 262 } 263 264 /* Context: QEMU global mutex held */ 265 void virtio_scsi_dataplane_stop(VirtIOSCSI *s) 266 { 267 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); 268 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 269 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 270 int i; 271 272 /* Better luck next time. */ 273 if (s->dataplane_fenced) { 274 s->dataplane_fenced = false; 275 return; 276 } 277 if (!s->dataplane_started || s->dataplane_stopping) { 278 return; 279 } 280 s->dataplane_stopping = true; 281 assert(s->ctx == iothread_get_aio_context(vs->conf.iothread)); 282 283 aio_context_acquire(s->ctx); 284 285 aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier, NULL); 286 aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier, NULL); 287 for (i = 0; i < vs->conf.num_queues; i++) { 288 aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier, NULL); 289 } 290 291 blk_drain_all(); /* ensure there are no in-flight requests */ 292 293 aio_context_release(s->ctx); 294 295 /* Sync vring state back to virtqueue so that non-dataplane request 296 * processing can continue when we disable the host notifier below. 297 */ 298 virtio_scsi_vring_teardown(s); 299 300 for (i = 0; i < vs->conf.num_queues + 2; i++) { 301 k->set_host_notifier(qbus->parent, i, false); 302 } 303 304 /* Clean up guest notifier (irq) */ 305 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 306 s->dataplane_stopping = false; 307 s->dataplane_started = false; 308 } 309