2014-09-23 09:49:24 +02: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 19:17:07 +01:00
|
|
|
#include "qemu/osdep.h"
|
2016-10-21 22:48:10 +02:00
|
|
|
#include "qapi/error.h"
|
2014-09-23 09:49:24 +02:00
|
|
|
#include "hw/virtio/virtio-scsi.h"
|
|
|
|
#include "qemu/error-report.h"
|
2014-10-07 13:59:18 +02:00
|
|
|
#include "sysemu/block-backend.h"
|
2016-06-22 19:11:19 +02:00
|
|
|
#include "hw/scsi/scsi.h"
|
2017-08-22 09:23:55 +02:00
|
|
|
#include "scsi/constants.h"
|
2016-06-22 19:11:19 +02:00
|
|
|
#include "hw/virtio/virtio-bus.h"
|
2014-09-23 09:49:24 +02:00
|
|
|
#include "hw/virtio/virtio-access.h"
|
|
|
|
|
|
|
|
/* Context: QEMU global mutex held */
|
2016-10-21 22:48:10 +02:00
|
|
|
void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
|
2014-09-23 09:49:24 +02:00
|
|
|
{
|
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
|
2016-10-21 22:48:10 +02:00
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(s);
|
|
|
|
BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2014-09-23 09:49:24 +02:00
|
|
|
|
2016-10-21 22:48:10 +02:00
|
|
|
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();
|
2014-09-23 09:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 16:35:00 +02:00
|
|
|
static int virtio_scsi_set_host_notifier(VirtIOSCSI *s, VirtQueue *vq, int n)
|
2014-09-23 09:49:24 +02:00
|
|
|
{
|
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
|
2014-10-15 15:15:24 +02:00
|
|
|
int rc;
|
2014-09-23 09:49:24 +02:00
|
|
|
|
|
|
|
/* Set up virtqueue notify */
|
2016-06-10 11:04:10 +02:00
|
|
|
rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true);
|
2014-10-15 15:15:24 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
|
|
|
|
rc);
|
2014-10-15 15:15:26 +02:00
|
|
|
s->dataplane_fenced = true;
|
2016-02-14 18:17:10 +01:00
|
|
|
return rc;
|
2014-09-23 09:49:24 +02:00
|
|
|
}
|
2015-03-18 10:42:12 +01:00
|
|
|
|
2016-02-14 18:17:10 +01:00
|
|
|
return 0;
|
2014-09-23 09:49:24 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:42:04 +01:00
|
|
|
/* Context: BH in IOThread */
|
|
|
|
static void virtio_scsi_dataplane_stop_bh(void *opaque)
|
2014-10-15 15:15:25 +02:00
|
|
|
{
|
2018-03-07 15:42:04 +01:00
|
|
|
VirtIOSCSI *s = opaque;
|
2014-10-15 15:15:25 +02:00
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
|
|
|
|
int i;
|
|
|
|
|
2021-12-07 14:23:36 +01:00
|
|
|
virtio_queue_aio_detach_host_notifier(vs->ctrl_vq, s->ctx);
|
|
|
|
virtio_queue_aio_detach_host_notifier(vs->event_vq, s->ctx);
|
2016-02-14 18:17:10 +01:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2021-12-07 14:23:36 +01:00
|
|
|
virtio_queue_aio_detach_host_notifier(vs->cmd_vqs[i], s->ctx);
|
2014-10-15 15:15:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 09:49:24 +02:00
|
|
|
/* Context: QEMU global mutex held */
|
2016-10-21 22:48:10 +02:00
|
|
|
int virtio_scsi_dataplane_start(VirtIODevice *vdev)
|
2014-09-23 09:49:24 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rc;
|
2020-12-17 16:00:39 +01:00
|
|
|
int vq_init_count = 0;
|
2016-10-21 22:48:10 +02:00
|
|
|
BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
|
2014-09-23 09:49:24 +02:00
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2016-10-21 22:48:10 +02:00
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
|
|
|
|
VirtIOSCSI *s = VIRTIO_SCSI(vdev);
|
2014-09-23 09:49:24 +02:00
|
|
|
|
|
|
|
if (s->dataplane_started ||
|
|
|
|
s->dataplane_starting ||
|
2016-10-21 22:48:10 +02:00
|
|
|
s->dataplane_fenced) {
|
|
|
|
return 0;
|
2014-09-23 09:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-06-13 07:05:19 +02:00
|
|
|
error_report("virtio-scsi: Failed to set guest notifiers (%d), "
|
|
|
|
"ensure -accel kvm is set.", rc);
|
2014-10-15 15:15:25 +02:00
|
|
|
goto fail_guest_notifiers;
|
2014-09-23 09:49:24 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:26:37 +02:00
|
|
|
/*
|
|
|
|
* Batch all the host notifiers in a single transaction to avoid
|
|
|
|
* quadratic time complexity in address_space_update_ioeventfds().
|
|
|
|
*/
|
2021-04-07 16:35:01 +02:00
|
|
|
memory_region_transaction_begin();
|
|
|
|
|
2021-04-07 16:35:00 +02:00
|
|
|
rc = virtio_scsi_set_host_notifier(s, vs->ctrl_vq, 0);
|
|
|
|
if (rc != 0) {
|
|
|
|
goto fail_host_notifiers;
|
2014-10-15 15:15:25 +02:00
|
|
|
}
|
2020-12-17 16:00:39 +01:00
|
|
|
|
|
|
|
vq_init_count++;
|
2021-04-07 16:35:00 +02:00
|
|
|
rc = virtio_scsi_set_host_notifier(s, vs->event_vq, 1);
|
|
|
|
if (rc != 0) {
|
|
|
|
goto fail_host_notifiers;
|
2014-10-15 15:15:25 +02:00
|
|
|
}
|
2020-12-17 16:00:39 +01:00
|
|
|
|
|
|
|
vq_init_count++;
|
2021-04-07 16:35:00 +02:00
|
|
|
|
2014-09-23 09:49:24 +02:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2021-04-07 16:35:00 +02:00
|
|
|
rc = virtio_scsi_set_host_notifier(s, vs->cmd_vqs[i], i + 2);
|
2016-02-14 18:17:10 +01:00
|
|
|
if (rc) {
|
2021-04-07 16:35:00 +02:00
|
|
|
goto fail_host_notifiers;
|
2014-10-15 15:15:25 +02:00
|
|
|
}
|
2020-12-17 16:00:39 +01:00
|
|
|
vq_init_count++;
|
2014-09-23 09:49:24 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 16:35:01 +02:00
|
|
|
memory_region_transaction_commit();
|
|
|
|
|
2021-04-07 16:35:00 +02:00
|
|
|
aio_context_acquire(s->ctx);
|
2021-12-07 14:23:36 +01:00
|
|
|
virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx);
|
|
|
|
virtio_queue_aio_attach_host_notifier(vs->event_vq, s->ctx);
|
2021-04-07 16:35:00 +02:00
|
|
|
|
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2021-12-07 14:23:36 +01:00
|
|
|
virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx);
|
2021-04-07 16:35:00 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 09:49:24 +02:00
|
|
|
s->dataplane_starting = false;
|
|
|
|
s->dataplane_started = true;
|
2014-10-31 17:38:04 +01:00
|
|
|
aio_context_release(s->ctx);
|
2016-10-21 22:48:10 +02:00
|
|
|
return 0;
|
2014-10-15 15:15:25 +02:00
|
|
|
|
2021-04-07 16:35:00 +02:00
|
|
|
fail_host_notifiers:
|
2020-12-17 16:00:39 +01:00
|
|
|
for (i = 0; i < vq_init_count; i++) {
|
2016-06-10 11:04:14 +02:00
|
|
|
virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
|
2021-04-07 16:35:01 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:26:37 +02:00
|
|
|
/*
|
|
|
|
* The transaction expects the ioeventfds to be open when it
|
|
|
|
* commits. Do it now, before the cleanup loop.
|
|
|
|
*/
|
2021-04-07 16:35:01 +02:00
|
|
|
memory_region_transaction_commit();
|
|
|
|
|
|
|
|
for (i = 0; i < vq_init_count; i++) {
|
2018-01-29 15:20:56 +01:00
|
|
|
virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
|
2014-10-15 15:15:25 +02:00
|
|
|
}
|
|
|
|
k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
|
|
|
|
fail_guest_notifiers:
|
2016-02-14 18:17:10 +01:00
|
|
|
s->dataplane_fenced = true;
|
2014-10-15 15:15:25 +02:00
|
|
|
s->dataplane_starting = false;
|
2016-02-14 18:17:10 +01:00
|
|
|
s->dataplane_started = true;
|
2016-10-21 22:48:10 +02:00
|
|
|
return -ENOSYS;
|
2014-09-23 09:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Context: QEMU global mutex held */
|
2016-10-21 22:48:10 +02:00
|
|
|
void virtio_scsi_dataplane_stop(VirtIODevice *vdev)
|
2014-09-23 09:49:24 +02:00
|
|
|
{
|
2016-10-21 22:48:10 +02:00
|
|
|
BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
|
2014-09-23 09:49:24 +02:00
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2016-10-21 22:48:10 +02:00
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
|
|
|
|
VirtIOSCSI *s = VIRTIO_SCSI(vdev);
|
2016-06-10 11:04:14 +02:00
|
|
|
int i;
|
2014-09-23 09:49:24 +02:00
|
|
|
|
2016-02-14 18:17:10 +01:00
|
|
|
if (!s->dataplane_started || s->dataplane_stopping) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-15 15:15:26 +02:00
|
|
|
/* Better luck next time. */
|
|
|
|
if (s->dataplane_fenced) {
|
|
|
|
s->dataplane_fenced = false;
|
2016-02-14 18:17:10 +01:00
|
|
|
s->dataplane_started = false;
|
2014-09-23 09:49:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
s->dataplane_stopping = true;
|
|
|
|
|
|
|
|
aio_context_acquire(s->ctx);
|
2018-03-07 15:42:04 +01:00
|
|
|
aio_wait_bh_oneshot(s->ctx, virtio_scsi_dataplane_stop_bh, s);
|
2016-10-27 12:49:05 +02:00
|
|
|
aio_context_release(s->ctx);
|
2014-09-23 09:49:24 +02:00
|
|
|
|
2014-10-07 13:59:18 +02:00
|
|
|
blk_drain_all(); /* ensure there are no in-flight requests */
|
2014-09-23 09:49:24 +02:00
|
|
|
|
2021-05-17 15:26:37 +02:00
|
|
|
/*
|
|
|
|
* Batch all the host notifiers in a single transaction to avoid
|
|
|
|
* quadratic time complexity in address_space_update_ioeventfds().
|
|
|
|
*/
|
2021-04-07 16:35:01 +02:00
|
|
|
memory_region_transaction_begin();
|
|
|
|
|
2014-09-23 09:49:24 +02:00
|
|
|
for (i = 0; i < vs->conf.num_queues + 2; i++) {
|
2016-06-10 11:04:14 +02:00
|
|
|
virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
|
2021-04-07 16:35:01 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 15:26:37 +02:00
|
|
|
/*
|
|
|
|
* The transaction expects the ioeventfds to be open when it
|
|
|
|
* commits. Do it now, before the cleanup loop.
|
|
|
|
*/
|
2021-04-07 16:35:01 +02:00
|
|
|
memory_region_transaction_commit();
|
|
|
|
|
|
|
|
for (i = 0; i < vs->conf.num_queues + 2; i++) {
|
2018-01-29 15:20:56 +01:00
|
|
|
virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
|
2014-09-23 09:49:24 +02: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;
|
|
|
|
}
|