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