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 "block/scsi.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 void virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev, 53 VirtQueue *vq) 54 { 55 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 56 57 assert(s->ctx && s->dataplane_started); 58 virtio_scsi_handle_cmd_vq(s, vq); 59 } 60 61 static void virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev, 62 VirtQueue *vq) 63 { 64 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 65 66 assert(s->ctx && s->dataplane_started); 67 virtio_scsi_handle_ctrl_vq(s, vq); 68 } 69 70 static void virtio_scsi_data_plane_handle_event(VirtIODevice *vdev, 71 VirtQueue *vq) 72 { 73 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 74 75 assert(s->ctx && s->dataplane_started); 76 virtio_scsi_handle_event_vq(s, vq); 77 } 78 79 static int virtio_scsi_vring_init(VirtIOSCSI *s, VirtQueue *vq, int n, 80 void (*fn)(VirtIODevice *vdev, VirtQueue *vq)) 81 { 82 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); 83 int rc; 84 85 /* Set up virtqueue notify */ 86 rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true); 87 if (rc != 0) { 88 fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n", 89 rc); 90 s->dataplane_fenced = true; 91 return rc; 92 } 93 94 virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, fn); 95 return 0; 96 } 97 98 /* assumes s->ctx held */ 99 static void virtio_scsi_clear_aio(VirtIOSCSI *s) 100 { 101 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 102 int i; 103 104 virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx, NULL); 105 virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx, NULL); 106 for (i = 0; i < vs->conf.num_queues; i++) { 107 virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL); 108 } 109 } 110 111 /* Context: QEMU global mutex held */ 112 int virtio_scsi_dataplane_start(VirtIODevice *vdev) 113 { 114 int i; 115 int rc; 116 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); 117 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 118 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 119 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 120 121 if (s->dataplane_started || 122 s->dataplane_starting || 123 s->dataplane_fenced) { 124 return 0; 125 } 126 127 s->dataplane_starting = true; 128 129 /* Set up guest notifier (irq) */ 130 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true); 131 if (rc != 0) { 132 fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), " 133 "ensure -enable-kvm is set\n", rc); 134 goto fail_guest_notifiers; 135 } 136 137 aio_context_acquire(s->ctx); 138 rc = virtio_scsi_vring_init(s, vs->ctrl_vq, 0, 139 virtio_scsi_data_plane_handle_ctrl); 140 if (rc) { 141 goto fail_vrings; 142 } 143 rc = virtio_scsi_vring_init(s, vs->event_vq, 1, 144 virtio_scsi_data_plane_handle_event); 145 if (rc) { 146 goto fail_vrings; 147 } 148 for (i = 0; i < vs->conf.num_queues; i++) { 149 rc = virtio_scsi_vring_init(s, vs->cmd_vqs[i], i + 2, 150 virtio_scsi_data_plane_handle_cmd); 151 if (rc) { 152 goto fail_vrings; 153 } 154 } 155 156 s->dataplane_starting = false; 157 s->dataplane_started = true; 158 aio_context_release(s->ctx); 159 return 0; 160 161 fail_vrings: 162 virtio_scsi_clear_aio(s); 163 aio_context_release(s->ctx); 164 for (i = 0; i < vs->conf.num_queues + 2; i++) { 165 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); 166 } 167 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 168 fail_guest_notifiers: 169 s->dataplane_fenced = true; 170 s->dataplane_starting = false; 171 s->dataplane_started = true; 172 return -ENOSYS; 173 } 174 175 /* Context: QEMU global mutex held */ 176 void virtio_scsi_dataplane_stop(VirtIODevice *vdev) 177 { 178 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); 179 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 180 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 181 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 182 int i; 183 184 if (!s->dataplane_started || s->dataplane_stopping) { 185 return; 186 } 187 188 /* Better luck next time. */ 189 if (s->dataplane_fenced) { 190 s->dataplane_fenced = false; 191 s->dataplane_started = false; 192 return; 193 } 194 s->dataplane_stopping = true; 195 196 aio_context_acquire(s->ctx); 197 virtio_scsi_clear_aio(s); 198 aio_context_release(s->ctx); 199 200 blk_drain_all(); /* ensure there are no in-flight requests */ 201 202 for (i = 0; i < vs->conf.num_queues + 2; i++) { 203 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); 204 } 205 206 /* Clean up guest notifier (irq) */ 207 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 208 s->dataplane_stopping = false; 209 s->dataplane_started = false; 210 } 211