virtio-scsi-dataplane.c 9.8 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 16
#include "hw/virtio/virtio-scsi.h"
#include "qemu/error-report.h"
17
#include "sysemu/block-backend.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <hw/scsi/scsi.h>
#include <block/scsi.h>
#include <hw/virtio/virtio-bus.h>
#include "hw/virtio/virtio-access.h"
#include "stdio.h"

/* Context: QEMU global mutex held */
void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread)
{
    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);

    assert(!s->ctx);
    s->ctx = iothread_get_aio_context(vs->conf.iothread);

    /* Don't try if transport does not support notifiers. */
    if (!k->set_guest_notifiers || !k->set_host_notifier) {
        fprintf(stderr, "virtio-scsi: Failed to set iothread "
                   "(transport does not support notifiers)");
        exit(1);
    }
}

static VirtIOSCSIVring *virtio_scsi_vring_init(VirtIOSCSI *s,
                                               VirtQueue *vq,
                                               EventNotifierHandler *handler,
                                               int n)
{
    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
49
    VirtIOSCSIVring *r;
50
    int rc;
51 52

    /* Set up virtqueue notify */
53 54 55 56
    rc = k->set_host_notifier(qbus->parent, n, true);
    if (rc != 0) {
        fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
                rc);
57
        s->dataplane_fenced = true;
58
        return NULL;
59
    }
60

61
    r = g_new(VirtIOSCSIVring, 1);
62 63
    r->host_notifier = *virtio_queue_get_host_notifier(vq);
    r->guest_notifier = *virtio_queue_get_guest_notifier(vq);
64
    aio_set_event_notifier(s->ctx, &r->host_notifier, true, handler);
65 66 67 68 69

    r->parent = s;

    if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
        fprintf(stderr, "virtio-scsi: VRing setup failed\n");
70
        goto fail_vring;
71 72
    }
    return r;
73 74

fail_vring:
75
    aio_set_event_notifier(s->ctx, &r->host_notifier, true, NULL);
76
    k->set_host_notifier(qbus->parent, n, false);
77
    g_free(r);
78
    return NULL;
79 80 81 82 83
}

VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s,
                                         VirtIOSCSIVring *vring)
{
84 85
    VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s;
    VirtIOSCSIReq *req;
86

87 88 89 90
    req = vring_pop((VirtIODevice *)s, &vring->vring,
                    sizeof(VirtIOSCSIReq) + vs->cdb_size);
    if (!req) {
        return NULL;
91
    }
92 93
    virtio_scsi_init_req(s, NULL, req);
    req->vring = vring;
94 95 96 97 98
    return req;
}

void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
{
99 100
    VirtIODevice *vdev = VIRTIO_DEVICE(req->vring->parent);

101
    vring_push(vdev, &req->vring->vring, &req->elem,
102
               req->qsgl.size + req->resp_iov.size);
103 104 105 106

    if (vring_should_notify(vdev, &req->vring->vring)) {
        event_notifier_set(&req->vring->guest_notifier);
    }
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
}

static void virtio_scsi_iothread_handle_ctrl(EventNotifier *notifier)
{
    VirtIOSCSIVring *vring = container_of(notifier,
                                          VirtIOSCSIVring, host_notifier);
    VirtIOSCSI *s = VIRTIO_SCSI(vring->parent);
    VirtIOSCSIReq *req;

    event_notifier_test_and_clear(notifier);
    while ((req = virtio_scsi_pop_req_vring(s, vring))) {
        virtio_scsi_handle_ctrl_req(s, req);
    }
}

static void virtio_scsi_iothread_handle_event(EventNotifier *notifier)
{
    VirtIOSCSIVring *vring = container_of(notifier,
                                          VirtIOSCSIVring, host_notifier);
    VirtIOSCSI *s = vring->parent;
    VirtIODevice *vdev = VIRTIO_DEVICE(s);

    event_notifier_test_and_clear(notifier);

    if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
        return;
    }

    if (s->events_dropped) {
        virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
    }
}

static void virtio_scsi_iothread_handle_cmd(EventNotifier *notifier)
{
    VirtIOSCSIVring *vring = container_of(notifier,
                                          VirtIOSCSIVring, host_notifier);
    VirtIOSCSI *s = (VirtIOSCSI *)vring->parent;
145 146
    VirtIOSCSIReq *req, *next;
    QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
147 148 149

    event_notifier_test_and_clear(notifier);
    while ((req = virtio_scsi_pop_req_vring(s, vring))) {
150
        if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
151
            QTAILQ_INSERT_TAIL(&reqs, req, next);
152
        }
153
    }
154 155 156 157

    QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
        virtio_scsi_handle_cmd_req_submit(s, req);
    }
158 159
}

160 161 162 163 164 165 166
/* assumes s->ctx held */
static void virtio_scsi_clear_aio(VirtIOSCSI *s)
{
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
    int i;

    if (s->ctrl_vring) {
167
        aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier,
168
                               true, NULL);
169 170
    }
    if (s->event_vring) {
171
        aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier,
172
                               true, NULL);
173 174 175
    }
    if (s->cmd_vrings) {
        for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) {
176
            aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier,
177
                                   true, NULL);
178 179 180 181 182 183 184 185 186 187 188 189
        }
    }
}

static void virtio_scsi_vring_teardown(VirtIOSCSI *s)
{
    VirtIODevice *vdev = VIRTIO_DEVICE(s);
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
    int i;

    if (s->ctrl_vring) {
        vring_teardown(&s->ctrl_vring->vring, vdev, 0);
190
        g_free(s->ctrl_vring);
191
        s->ctrl_vring = NULL;
192 193 194
    }
    if (s->event_vring) {
        vring_teardown(&s->event_vring->vring, vdev, 1);
195
        g_free(s->event_vring);
196
        s->event_vring = NULL;
197 198 199 200
    }
    if (s->cmd_vrings) {
        for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) {
            vring_teardown(&s->cmd_vrings[i]->vring, vdev, 2 + i);
201
            g_free(s->cmd_vrings[i]);
202
            s->cmd_vrings[i] = NULL;
203 204 205 206 207 208
        }
        free(s->cmd_vrings);
        s->cmd_vrings = NULL;
    }
}

209 210 211 212 213 214 215 216 217 218 219
/* Context: QEMU global mutex held */
void virtio_scsi_dataplane_start(VirtIOSCSI *s)
{
    int i;
    int rc;
    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);

    if (s->dataplane_started ||
        s->dataplane_starting ||
220
        s->dataplane_fenced ||
221 222 223 224 225 226 227 228 229
        s->ctx != iothread_get_aio_context(vs->conf.iothread)) {
        return;
    }

    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) {
230 231
        fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
                "ensure -enable-kvm is set\n", rc);
232
        s->dataplane_fenced = true;
233
        goto fail_guest_notifiers;
234 235 236 237 238 239
    }

    aio_context_acquire(s->ctx);
    s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
                                           virtio_scsi_iothread_handle_ctrl,
                                           0);
240 241 242
    if (!s->ctrl_vring) {
        goto fail_vrings;
    }
243 244 245
    s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
                                            virtio_scsi_iothread_handle_event,
                                            1);
246 247 248
    if (!s->event_vring) {
        goto fail_vrings;
    }
249
    s->cmd_vrings = g_new(VirtIOSCSIVring *, vs->conf.num_queues);
250 251 252 253 254
    for (i = 0; i < vs->conf.num_queues; i++) {
        s->cmd_vrings[i] =
            virtio_scsi_vring_init(s, vs->cmd_vqs[i],
                                   virtio_scsi_iothread_handle_cmd,
                                   i + 2);
255 256 257
        if (!s->cmd_vrings[i]) {
            goto fail_vrings;
        }
258 259 260 261
    }

    s->dataplane_starting = false;
    s->dataplane_started = true;
P
Paolo Bonzini 已提交
262 263
    aio_context_release(s->ctx);
    return;
264 265 266 267 268 269 270 271 272 273 274

fail_vrings:
    virtio_scsi_clear_aio(s);
    aio_context_release(s->ctx);
    virtio_scsi_vring_teardown(s);
    for (i = 0; i < vs->conf.num_queues + 2; i++) {
        k->set_host_notifier(qbus->parent, i, false);
    }
    k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
fail_guest_notifiers:
    s->dataplane_starting = false;
275 276 277 278 279 280 281 282 283 284
}

/* Context: QEMU global mutex held */
void virtio_scsi_dataplane_stop(VirtIOSCSI *s)
{
    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
    int i;

285 286 287 288 289
    /* Better luck next time. */
    if (s->dataplane_fenced) {
        s->dataplane_fenced = false;
        return;
    }
290 291 292 293 294 295 296 297
    if (!s->dataplane_started || s->dataplane_stopping) {
        return;
    }
    s->dataplane_stopping = true;
    assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));

    aio_context_acquire(s->ctx);

298
    aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier,
299
                           true, NULL);
300
    aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier,
301
                           true, NULL);
302
    for (i = 0; i < vs->conf.num_queues; i++) {
303
        aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier,
304
                               true, NULL);
305 306
    }

307
    blk_drain_all(); /* ensure there are no in-flight requests */
308 309 310 311 312 313

    aio_context_release(s->ctx);

    /* Sync vring state back to virtqueue so that non-dataplane request
     * processing can continue when we disable the host notifier below.
     */
314
    virtio_scsi_vring_teardown(s);
315 316 317 318 319 320 321 322 323 324

    for (i = 0; i < vs->conf.num_queues + 2; i++) {
        k->set_host_notifier(qbus->parent, i, false);
    }

    /* 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;
}