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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hw/virtio/virtio-scsi.h"
|
|
|
|
#include "qemu/error-report.h"
|
2014-10-07 15:59:18 +04:00
|
|
|
#include "sysemu/block-backend.h"
|
2014-09-23 11:49:24 +04:00
|
|
|
#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);
|
2015-03-18 12:42:12 +03:00
|
|
|
VirtIOSCSIVring *r;
|
2014-10-15 17:15:24 +04:00
|
|
|
int rc;
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
/* Set up virtqueue notify */
|
2014-10-15 17:15:24 +04:00
|
|
|
rc = k->set_host_notifier(qbus->parent, n, true);
|
|
|
|
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;
|
2014-10-15 17:15:25 +04:00
|
|
|
return NULL;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
2015-03-18 12:42:12 +03:00
|
|
|
|
2015-10-01 13:59:01 +03:00
|
|
|
r = g_new(VirtIOSCSIVring, 1);
|
2014-09-23 11:49:24 +04:00
|
|
|
r->host_notifier = *virtio_queue_get_host_notifier(vq);
|
|
|
|
r->guest_notifier = *virtio_queue_get_guest_notifier(vq);
|
2015-10-23 06:08:07 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &r->host_notifier, true, handler);
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
r->parent = s;
|
|
|
|
|
|
|
|
if (!vring_setup(&r->vring, VIRTIO_DEVICE(s), n)) {
|
|
|
|
fprintf(stderr, "virtio-scsi: VRing setup failed\n");
|
2014-10-15 17:15:25 +04:00
|
|
|
goto fail_vring;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
return r;
|
2014-10-15 17:15:25 +04:00
|
|
|
|
|
|
|
fail_vring:
|
2015-10-23 06:08:07 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &r->host_notifier, true, NULL);
|
2014-10-15 17:15:25 +04:00
|
|
|
k->set_host_notifier(qbus->parent, n, false);
|
2015-10-01 13:59:01 +03:00
|
|
|
g_free(r);
|
2014-10-15 17:15:25 +04:00
|
|
|
return NULL;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s,
|
|
|
|
VirtIOSCSIVring *vring)
|
|
|
|
{
|
|
|
|
VirtIOSCSIReq *req = virtio_scsi_init_req(s, NULL);
|
|
|
|
int r;
|
|
|
|
|
|
|
|
req->vring = vring;
|
|
|
|
r = vring_pop((VirtIODevice *)s, &vring->vring, &req->elem);
|
|
|
|
if (r < 0) {
|
|
|
|
virtio_scsi_free_req(req);
|
|
|
|
req = NULL;
|
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req)
|
|
|
|
{
|
2014-11-12 06:24:35 +03:00
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(req->vring->parent);
|
|
|
|
|
2015-01-26 19:26:42 +03:00
|
|
|
vring_push(vdev, &req->vring->vring, &req->elem,
|
2014-09-23 11:49:24 +04:00
|
|
|
req->qsgl.size + req->resp_iov.size);
|
2014-11-12 06:24:35 +03:00
|
|
|
|
|
|
|
if (vring_should_notify(vdev, &req->vring->vring)) {
|
|
|
|
event_notifier_set(&req->vring->guest_notifier);
|
|
|
|
}
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-09-23 11:49:28 +04:00
|
|
|
VirtIOSCSIReq *req, *next;
|
|
|
|
QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
event_notifier_test_and_clear(notifier);
|
|
|
|
while ((req = virtio_scsi_pop_req_vring(s, vring))) {
|
2014-09-23 11:49:27 +04:00
|
|
|
if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
|
2014-09-23 11:49:28 +04:00
|
|
|
QTAILQ_INSERT_TAIL(&reqs, req, next);
|
2014-09-23 11:49:27 +04:00
|
|
|
}
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
2014-09-23 11:49:28 +04:00
|
|
|
|
|
|
|
QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
|
|
|
|
virtio_scsi_handle_cmd_req_submit(s, req);
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
if (s->ctrl_vring) {
|
2015-10-23 06:08:05 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier,
|
2015-10-23 06:08:07 +03:00
|
|
|
true, NULL);
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
if (s->event_vring) {
|
2015-10-23 06:08:05 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier,
|
2015-10-23 06:08:07 +03:00
|
|
|
true, NULL);
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
if (s->cmd_vrings) {
|
|
|
|
for (i = 0; i < vs->conf.num_queues && s->cmd_vrings[i]; i++) {
|
2015-10-23 06:08:05 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier,
|
2015-10-23 06:08:07 +03:00
|
|
|
true, NULL);
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-10-01 13:59:01 +03:00
|
|
|
g_free(s->ctrl_vring);
|
2015-03-26 10:42:32 +03:00
|
|
|
s->ctrl_vring = NULL;
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
if (s->event_vring) {
|
|
|
|
vring_teardown(&s->event_vring->vring, vdev, 1);
|
2015-10-01 13:59:01 +03:00
|
|
|
g_free(s->event_vring);
|
2015-03-26 10:42:32 +03:00
|
|
|
s->event_vring = NULL;
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
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);
|
2015-10-01 13:59:01 +03:00
|
|
|
g_free(s->cmd_vrings[i]);
|
2015-03-26 10:42:32 +03:00
|
|
|
s->cmd_vrings[i] = NULL;
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
free(s->cmd_vrings);
|
|
|
|
s->cmd_vrings = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:26 +04:00
|
|
|
s->dataplane_fenced = true;
|
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);
|
|
|
|
s->ctrl_vring = virtio_scsi_vring_init(s, vs->ctrl_vq,
|
|
|
|
virtio_scsi_iothread_handle_ctrl,
|
|
|
|
0);
|
2014-10-15 17:15:25 +04:00
|
|
|
if (!s->ctrl_vring) {
|
|
|
|
goto fail_vrings;
|
|
|
|
}
|
2014-09-23 11:49:24 +04:00
|
|
|
s->event_vring = virtio_scsi_vring_init(s, vs->event_vq,
|
|
|
|
virtio_scsi_iothread_handle_event,
|
|
|
|
1);
|
2014-10-15 17:15:25 +04:00
|
|
|
if (!s->event_vring) {
|
|
|
|
goto fail_vrings;
|
|
|
|
}
|
2014-11-11 04:17:09 +03:00
|
|
|
s->cmd_vrings = g_new(VirtIOSCSIVring *, vs->conf.num_queues);
|
2014-09-23 11:49:24 +04:00
|
|
|
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);
|
2014-10-15 17:15:25 +04:00
|
|
|
if (!s->cmd_vrings[i]) {
|
|
|
|
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);
|
|
|
|
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;
|
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);
|
|
|
|
int i;
|
|
|
|
|
2014-10-15 17:15:26 +04:00
|
|
|
/* Better luck next time. */
|
|
|
|
if (s->dataplane_fenced) {
|
|
|
|
s->dataplane_fenced = false;
|
|
|
|
return;
|
|
|
|
}
|
2014-09-23 11:49:24 +04:00
|
|
|
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);
|
|
|
|
|
2015-10-23 06:08:05 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &s->ctrl_vring->host_notifier,
|
2015-10-23 06:08:07 +03:00
|
|
|
true, NULL);
|
2015-10-23 06:08:05 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &s->event_vring->host_notifier,
|
2015-10-23 06:08:07 +03:00
|
|
|
true, NULL);
|
2014-09-23 11:49:24 +04:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2015-10-23 06:08:05 +03:00
|
|
|
aio_set_event_notifier(s->ctx, &s->cmd_vrings[i]->host_notifier,
|
2015-10-23 06:08:07 +03:00
|
|
|
true, NULL);
|
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);
|
|
|
|
|
|
|
|
/* Sync vring state back to virtqueue so that non-dataplane request
|
|
|
|
* processing can continue when we disable the host notifier below.
|
|
|
|
*/
|
2014-10-15 17:15:25 +04:00
|
|
|
virtio_scsi_vring_teardown(s);
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|