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 23 /* Context: QEMU global mutex held */ 24 void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp) 25 { 26 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 27 VirtIODevice *vdev = VIRTIO_DEVICE(s); 28 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); 29 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 30 31 if (vs->conf.iothread) { 32 if (!k->set_guest_notifiers || !k->ioeventfd_assign) { 33 error_setg(errp, 34 "device is incompatible with iothread " 35 "(transport does not support notifiers)"); 36 return; 37 } 38 if (!virtio_device_ioeventfd_enabled(vdev)) { 39 error_setg(errp, "ioeventfd is required for iothread"); 40 return; 41 } 42 s->ctx = iothread_get_aio_context(vs->conf.iothread); 43 } else { 44 if (!virtio_device_ioeventfd_enabled(vdev)) { 45 return; 46 } 47 s->ctx = qemu_get_aio_context(); 48 } 49 } 50 51 static int virtio_scsi_set_host_notifier(VirtIOSCSI *s, VirtQueue *vq, int n) 52 { 53 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); 54 int rc; 55 56 /* Set up virtqueue notify */ 57 rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true); 58 if (rc != 0) { 59 fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n", 60 rc); 61 s->dataplane_fenced = true; 62 return rc; 63 } 64 65 return 0; 66 } 67 68 /* Context: BH in IOThread */ 69 static void virtio_scsi_dataplane_stop_bh(void *opaque) 70 { 71 VirtIOSCSI *s = opaque; 72 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 73 EventNotifier *host_notifier; 74 int i; 75 76 virtio_queue_aio_detach_host_notifier(vs->ctrl_vq, s->ctx); 77 host_notifier = virtio_queue_get_host_notifier(vs->ctrl_vq); 78 79 /* 80 * Test and clear notifier after disabling event, in case poll callback 81 * didn't have time to run. 82 */ 83 virtio_queue_host_notifier_read(host_notifier); 84 85 virtio_queue_aio_detach_host_notifier(vs->event_vq, s->ctx); 86 host_notifier = virtio_queue_get_host_notifier(vs->event_vq); 87 virtio_queue_host_notifier_read(host_notifier); 88 89 for (i = 0; i < vs->conf.num_queues; i++) { 90 virtio_queue_aio_detach_host_notifier(vs->cmd_vqs[i], s->ctx); 91 host_notifier = virtio_queue_get_host_notifier(vs->cmd_vqs[i]); 92 virtio_queue_host_notifier_read(host_notifier); 93 } 94 } 95 96 /* Context: QEMU global mutex held */ 97 int virtio_scsi_dataplane_start(VirtIODevice *vdev) 98 { 99 int i; 100 int rc; 101 int vq_init_count = 0; 102 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); 103 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 104 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 105 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 106 107 if (s->dataplane_started || 108 s->dataplane_starting || 109 s->dataplane_fenced) { 110 return 0; 111 } 112 113 s->dataplane_starting = true; 114 115 /* Set up guest notifier (irq) */ 116 rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true); 117 if (rc != 0) { 118 error_report("virtio-scsi: Failed to set guest notifiers (%d), " 119 "ensure -accel kvm is set.", rc); 120 goto fail_guest_notifiers; 121 } 122 123 /* 124 * Batch all the host notifiers in a single transaction to avoid 125 * quadratic time complexity in address_space_update_ioeventfds(). 126 */ 127 memory_region_transaction_begin(); 128 129 rc = virtio_scsi_set_host_notifier(s, vs->ctrl_vq, 0); 130 if (rc != 0) { 131 goto fail_host_notifiers; 132 } 133 134 vq_init_count++; 135 rc = virtio_scsi_set_host_notifier(s, vs->event_vq, 1); 136 if (rc != 0) { 137 goto fail_host_notifiers; 138 } 139 140 vq_init_count++; 141 142 for (i = 0; i < vs->conf.num_queues; i++) { 143 rc = virtio_scsi_set_host_notifier(s, vs->cmd_vqs[i], i + 2); 144 if (rc) { 145 goto fail_host_notifiers; 146 } 147 vq_init_count++; 148 } 149 150 memory_region_transaction_commit(); 151 152 /* 153 * These fields are visible to the IOThread so we rely on implicit barriers 154 * in aio_context_acquire() on the write side and aio_notify_accept() on 155 * the read side. 156 */ 157 s->dataplane_starting = false; 158 s->dataplane_started = true; 159 160 if (s->bus.drain_count == 0) { 161 aio_context_acquire(s->ctx); 162 virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx); 163 virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq, s->ctx); 164 165 for (i = 0; i < vs->conf.num_queues; i++) { 166 virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx); 167 } 168 aio_context_release(s->ctx); 169 } 170 return 0; 171 172 fail_host_notifiers: 173 for (i = 0; i < vq_init_count; i++) { 174 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); 175 } 176 177 /* 178 * The transaction expects the ioeventfds to be open when it 179 * commits. Do it now, before the cleanup loop. 180 */ 181 memory_region_transaction_commit(); 182 183 for (i = 0; i < vq_init_count; i++) { 184 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); 185 } 186 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 187 fail_guest_notifiers: 188 s->dataplane_fenced = true; 189 s->dataplane_starting = false; 190 s->dataplane_started = true; 191 return -ENOSYS; 192 } 193 194 /* Context: QEMU global mutex held */ 195 void virtio_scsi_dataplane_stop(VirtIODevice *vdev) 196 { 197 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); 198 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 199 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 200 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 201 int i; 202 203 if (!s->dataplane_started || s->dataplane_stopping) { 204 return; 205 } 206 207 /* Better luck next time. */ 208 if (s->dataplane_fenced) { 209 s->dataplane_fenced = false; 210 s->dataplane_started = false; 211 return; 212 } 213 s->dataplane_stopping = true; 214 215 if (s->bus.drain_count == 0) { 216 aio_wait_bh_oneshot(s->ctx, virtio_scsi_dataplane_stop_bh, s); 217 } 218 219 blk_drain_all(); /* ensure there are no in-flight requests */ 220 221 /* 222 * Batch all the host notifiers in a single transaction to avoid 223 * quadratic time complexity in address_space_update_ioeventfds(). 224 */ 225 memory_region_transaction_begin(); 226 227 for (i = 0; i < vs->conf.num_queues + 2; i++) { 228 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); 229 } 230 231 /* 232 * The transaction expects the ioeventfds to be open when it 233 * commits. Do it now, before the cleanup loop. 234 */ 235 memory_region_transaction_commit(); 236 237 for (i = 0; i < vs->conf.num_queues + 2; i++) { 238 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); 239 } 240 241 /* Clean up guest notifier (irq) */ 242 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 243 s->dataplane_stopping = false; 244 s->dataplane_started = false; 245 } 246