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