2014-09-23 11:49:24 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:07 +03:00
|
|
|
#include "qemu/osdep.h"
|
2014-09-23 11:49:24 +04:00
|
|
|
#include "hw/virtio/virtio-scsi.h"
|
|
|
|
#include "qemu/error-report.h"
|
2014-10-07 15:59:18 +04:00
|
|
|
#include "sysemu/block-backend.h"
|
2016-06-22 20:11:19 +03:00
|
|
|
#include "hw/scsi/scsi.h"
|
|
|
|
#include "block/scsi.h"
|
|
|
|
#include "hw/virtio/virtio-bus.h"
|
2014-09-23 11:49:24 +04:00
|
|
|
#include "hw/virtio/virtio-access.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. */
|
2016-06-10 12:04:14 +03:00
|
|
|
if (!k->set_guest_notifiers || !k->ioeventfd_started) {
|
2014-09-23 11:49:24 +04:00
|
|
|
fprintf(stderr, "virtio-scsi: Failed to set iothread "
|
|
|
|
"(transport does not support notifiers)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:16:27 +03:00
|
|
|
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))
|
2014-09-23 11:49:24 +04:00
|
|
|
{
|
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
|
2014-10-15 17:15:24 +04:00
|
|
|
int rc;
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
/* Set up virtqueue notify */
|
2016-06-10 12:04:10 +03:00
|
|
|
rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true);
|
2014-10-15 17:15:24 +04:00
|
|
|
if (rc != 0) {
|
|
|
|
fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
|
|
|
|
rc);
|
2014-10-15 17:15:26 +04:00
|
|
|
s->dataplane_fenced = true;
|
2016-02-14 20:17:10 +03:00
|
|
|
return rc;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
2015-03-18 12:42:12 +03:00
|
|
|
|
2016-04-06 13:16:28 +03:00
|
|
|
virtio_queue_aio_set_host_notifier_handler(vq, s->ctx, fn);
|
2016-02-14 20:17:10 +03:00
|
|
|
return 0;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
2016-02-14 20:17:10 +03:00
|
|
|
void virtio_scsi_dataplane_notify(VirtIODevice *vdev, VirtIOSCSIReq *req)
|
2014-09-23 11:49:24 +04:00
|
|
|
{
|
2016-02-14 20:17:10 +03:00
|
|
|
if (virtio_should_notify(vdev, req->vq)) {
|
|
|
|
event_notifier_set(virtio_queue_get_guest_notifier(req->vq));
|
2014-09-23 11:49:28 +04:00
|
|
|
}
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
2014-10-15 17:15:25 +04:00
|
|
|
/* assumes s->ctx held */
|
|
|
|
static void virtio_scsi_clear_aio(VirtIOSCSI *s)
|
|
|
|
{
|
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
|
|
|
|
int i;
|
|
|
|
|
2016-04-06 13:16:28 +03:00
|
|
|
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);
|
2016-02-14 20:17:10 +03:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2016-04-06 13:16:28 +03:00
|
|
|
virtio_queue_aio_set_host_notifier_handler(vs->cmd_vqs[i], s->ctx, NULL);
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:49:24 +04:00
|
|
|
/* 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 ||
|
2014-10-15 17:15:26 +04:00
|
|
|
s->dataplane_fenced ||
|
2014-09-23 11:49:24 +04:00
|
|
|
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) {
|
2014-10-15 17:15:24 +04:00
|
|
|
fprintf(stderr, "virtio-scsi: Failed to set guest notifiers (%d), "
|
|
|
|
"ensure -enable-kvm is set\n", rc);
|
2014-10-15 17:15:25 +04:00
|
|
|
goto fail_guest_notifiers;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aio_context_acquire(s->ctx);
|
2016-04-06 13:16:27 +03:00
|
|
|
rc = virtio_scsi_vring_init(s, vs->ctrl_vq, 0,
|
|
|
|
virtio_scsi_data_plane_handle_ctrl);
|
2016-02-14 20:17:10 +03:00
|
|
|
if (rc) {
|
2014-10-15 17:15:25 +04:00
|
|
|
goto fail_vrings;
|
|
|
|
}
|
2016-04-06 13:16:27 +03:00
|
|
|
rc = virtio_scsi_vring_init(s, vs->event_vq, 1,
|
|
|
|
virtio_scsi_data_plane_handle_event);
|
2016-02-14 20:17:10 +03:00
|
|
|
if (rc) {
|
2014-10-15 17:15:25 +04:00
|
|
|
goto fail_vrings;
|
|
|
|
}
|
2014-09-23 11:49:24 +04:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2016-04-06 13:16:27 +03:00
|
|
|
rc = virtio_scsi_vring_init(s, vs->cmd_vqs[i], i + 2,
|
|
|
|
virtio_scsi_data_plane_handle_cmd);
|
2016-02-14 20:17:10 +03:00
|
|
|
if (rc) {
|
2014-10-15 17:15:25 +04:00
|
|
|
goto fail_vrings;
|
|
|
|
}
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
s->dataplane_starting = false;
|
|
|
|
s->dataplane_started = true;
|
2014-10-31 19:38:04 +03:00
|
|
|
aio_context_release(s->ctx);
|
|
|
|
return;
|
2014-10-15 17:15:25 +04:00
|
|
|
|
|
|
|
fail_vrings:
|
|
|
|
virtio_scsi_clear_aio(s);
|
|
|
|
aio_context_release(s->ctx);
|
|
|
|
for (i = 0; i < vs->conf.num_queues + 2; i++) {
|
2016-06-10 12:04:14 +03:00
|
|
|
virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
|
|
|
|
fail_guest_notifiers:
|
2016-02-14 20:17:10 +03:00
|
|
|
s->dataplane_fenced = true;
|
2014-10-15 17:15:25 +04:00
|
|
|
s->dataplane_starting = false;
|
2016-02-14 20:17:10 +03:00
|
|
|
s->dataplane_started = true;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2016-06-10 12:04:14 +03:00
|
|
|
int i;
|
2014-09-23 11:49:24 +04:00
|
|
|
|
2016-02-14 20:17:10 +03:00
|
|
|
if (!s->dataplane_started || s->dataplane_stopping) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-15 17:15:26 +04:00
|
|
|
/* Better luck next time. */
|
|
|
|
if (s->dataplane_fenced) {
|
|
|
|
s->dataplane_fenced = false;
|
2016-02-14 20:17:10 +03:00
|
|
|
s->dataplane_started = false;
|
2014-09-23 11:49:24 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
s->dataplane_stopping = true;
|
|
|
|
assert(s->ctx == iothread_get_aio_context(vs->conf.iothread));
|
|
|
|
|
|
|
|
aio_context_acquire(s->ctx);
|
|
|
|
|
2016-02-14 20:17:10 +03:00
|
|
|
virtio_scsi_clear_aio(s);
|
2014-09-23 11:49:24 +04:00
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_drain_all(); /* ensure there are no in-flight requests */
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
aio_context_release(s->ctx);
|
|
|
|
|
|
|
|
for (i = 0; i < vs->conf.num_queues + 2; i++) {
|
2016-06-10 12:04:14 +03:00
|
|
|
virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|