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: BQL 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: BQL 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 s->dataplane_starting = false; 153 s->dataplane_started = true; 154 smp_wmb(); /* paired with aio_notify_accept() */ 155 156 if (s->bus.drain_count == 0) { 157 virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx); 158 virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq, s->ctx); 159 160 for (i = 0; i < vs->conf.num_queues; i++) { 161 virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx); 162 } 163 } 164 return 0; 165 166 fail_host_notifiers: 167 for (i = 0; i < vq_init_count; i++) { 168 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); 169 } 170 171 /* 172 * The transaction expects the ioeventfds to be open when it 173 * commits. Do it now, before the cleanup loop. 174 */ 175 memory_region_transaction_commit(); 176 177 for (i = 0; i < vq_init_count; i++) { 178 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); 179 } 180 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 181 fail_guest_notifiers: 182 s->dataplane_fenced = true; 183 s->dataplane_starting = false; 184 s->dataplane_started = true; 185 return -ENOSYS; 186 } 187 188 /* Context: BQL held */ 189 void virtio_scsi_dataplane_stop(VirtIODevice *vdev) 190 { 191 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); 192 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 193 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 194 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 195 int i; 196 197 if (!s->dataplane_started || s->dataplane_stopping) { 198 return; 199 } 200 201 /* Better luck next time. */ 202 if (s->dataplane_fenced) { 203 s->dataplane_fenced = false; 204 s->dataplane_started = false; 205 return; 206 } 207 s->dataplane_stopping = true; 208 209 if (s->bus.drain_count == 0) { 210 aio_wait_bh_oneshot(s->ctx, virtio_scsi_dataplane_stop_bh, s); 211 } 212 213 blk_drain_all(); /* ensure there are no in-flight requests */ 214 215 /* 216 * Batch all the host notifiers in a single transaction to avoid 217 * quadratic time complexity in address_space_update_ioeventfds(). 218 */ 219 memory_region_transaction_begin(); 220 221 for (i = 0; i < vs->conf.num_queues + 2; i++) { 222 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false); 223 } 224 225 /* 226 * The transaction expects the ioeventfds to be open when it 227 * commits. Do it now, before the cleanup loop. 228 */ 229 memory_region_transaction_commit(); 230 231 for (i = 0; i < vs->conf.num_queues + 2; i++) { 232 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i); 233 } 234 235 /* Clean up guest notifier (irq) */ 236 k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false); 237 s->dataplane_stopping = false; 238 s->dataplane_started = false; 239 } 240