virtio-scsi-dataplane.c 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Virtio SCSI dataplane
 *
 * Copyright Red Hat, Inc. 2014
 *
 * Authors:
 *   Fam Zheng <famz@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */

P
Peter Maydell 已提交
14
#include "qemu/osdep.h"
15
#include "qapi/error.h"
16 17
#include "hw/virtio/virtio-scsi.h"
#include "qemu/error-report.h"
18
#include "sysemu/block-backend.h"
19 20 21
#include "hw/scsi/scsi.h"
#include "block/scsi.h"
#include "hw/virtio/virtio-bus.h"
22 23 24
#include "hw/virtio/virtio-access.h"

/* Context: QEMU global mutex held */
25
void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
26 27
{
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
28 29 30
    VirtIODevice *vdev = VIRTIO_DEVICE(s);
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    if (vs->conf.iothread) {
        if (!k->set_guest_notifiers || !k->ioeventfd_assign) {
            error_setg(errp,
                       "device is incompatible with iothread "
                       "(transport does not support notifiers)");
            return;
        }
        if (!virtio_device_ioeventfd_enabled(vdev)) {
            error_setg(errp, "ioeventfd is required for iothread");
            return;
        }
        s->ctx = iothread_get_aio_context(vs->conf.iothread);
    } else {
        if (!virtio_device_ioeventfd_enabled(vdev)) {
            return;
        }
        s->ctx = qemu_get_aio_context();
49 50 51
    }
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static void virtio_scsi_data_plane_handle_cmd(VirtIODevice *vdev,
                                              VirtQueue *vq)
{
    VirtIOSCSI *s = (VirtIOSCSI *)vdev;

    assert(s->ctx && s->dataplane_started);
    virtio_scsi_handle_cmd_vq(s, vq);
}

static void virtio_scsi_data_plane_handle_ctrl(VirtIODevice *vdev,
                                               VirtQueue *vq)
{
    VirtIOSCSI *s = VIRTIO_SCSI(vdev);

    assert(s->ctx && s->dataplane_started);
    virtio_scsi_handle_ctrl_vq(s, vq);
}

static void virtio_scsi_data_plane_handle_event(VirtIODevice *vdev,
                                                VirtQueue *vq)
{
    VirtIOSCSI *s = VIRTIO_SCSI(vdev);

    assert(s->ctx && s->dataplane_started);
    virtio_scsi_handle_event_vq(s, vq);
}

static int virtio_scsi_vring_init(VirtIOSCSI *s, VirtQueue *vq, int n,
                                  void (*fn)(VirtIODevice *vdev, VirtQueue *vq))
81 82
{
    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
83
    int rc;
84 85

    /* Set up virtqueue notify */
86
    rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true);
87 88 89
    if (rc != 0) {
        fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
                rc);
90
        s->dataplane_fenced = true;
91
        return rc;
92
    }
93

94
    virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, fn);
95
    return 0;
96 97
}

98 99 100 101 102 103
/* assumes s->ctx held */
static void virtio_scsi_clear_aio(VirtIOSCSI *s)
{
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
    int i;

104 105
    virtio_queue_aio_set_host_notifier_handler(vs->ctrl_vq, s->ctx, NULL);
    virtio_queue_aio_set_host_notifier_handler(vs->event_vq, s->ctx, NULL);
106
    for (i = 0; i < vs->conf.num_queues; i++) {
107
        virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL);
108 109 110
    }
}

111
/* Context: QEMU global mutex held */
112
int virtio_scsi_dataplane_start(VirtIODevice *vdev)
113 114 115
{
    int i;
    int rc;
116
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
117
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
118 119
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
    VirtIOSCSI *s = VIRTIO_SCSI(vdev);
120 121 122

    if (s->dataplane_started ||
        s->dataplane_starting ||
123 124
        s->dataplane_fenced) {
        return 0;
125 126 127 128 129 130 131
    }

    s->dataplane_starting = true;

    /* Set up guest notifier (irq) */
    rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
    if (rc != 0) {
132 133
        fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
                "ensure -enable-kvm is set\n", rc);
134
        goto fail_guest_notifiers;
135 136 137
    }

    aio_context_acquire(s->ctx);
138 139
    rc = virtio_scsi_vring_init(s, vs->ctrl_vq, 0,
                                virtio_scsi_data_plane_handle_ctrl);
140
    if (rc) {
141 142
        goto fail_vrings;
    }
143 144
    rc = virtio_scsi_vring_init(s, vs->event_vq, 1,
                                virtio_scsi_data_plane_handle_event);
145
    if (rc) {
146 147
        goto fail_vrings;
    }
148
    for (i = 0; i < vs->conf.num_queues; i++) {
149 150
        rc = virtio_scsi_vring_init(s, vs->cmd_vqs[i], i + 2,
                                    virtio_scsi_data_plane_handle_cmd);
151
        if (rc) {
152 153
            goto fail_vrings;
        }
154 155 156 157
    }

    s->dataplane_starting = false;
    s->dataplane_started = true;
P
Paolo Bonzini 已提交
158
    aio_context_release(s->ctx);
159
    return 0;
160 161 162 163 164

fail_vrings:
    virtio_scsi_clear_aio(s);
    aio_context_release(s->ctx);
    for (i = 0; i < vs->conf.num_queues + 2; i++) {
165
        virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
166 167 168
    }
    k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
fail_guest_notifiers:
169
    s->dataplane_fenced = true;
170
    s->dataplane_starting = false;
171
    s->dataplane_started = true;
172
    return -ENOSYS;
173 174 175
}

/* Context: QEMU global mutex held */
176
void virtio_scsi_dataplane_stop(VirtIODevice *vdev)
177
{
178
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
179
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
180 181
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
    VirtIOSCSI *s = VIRTIO_SCSI(vdev);
182
    int i;
183

184 185 186 187
    if (!s->dataplane_started || s->dataplane_stopping) {
        return;
    }

188 189 190
    /* Better luck next time. */
    if (s->dataplane_fenced) {
        s->dataplane_fenced = false;
191
        s->dataplane_started = false;
192 193 194 195 196
        return;
    }
    s->dataplane_stopping = true;

    aio_context_acquire(s->ctx);
197
    virtio_scsi_clear_aio(s);
198
    aio_context_release(s->ctx);
199

200
    blk_drain_all(); /* ensure there are no in-flight requests */
201 202

    for (i = 0; i < vs->conf.num_queues + 2; i++) {
203
        virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
204 205 206 207 208 209 210
    }

    /* Clean up guest notifier (irq) */
    k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
    s->dataplane_stopping = false;
    s->dataplane_started = false;
}