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