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