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"
|
2016-10-21 23:48:10 +03:00
|
|
|
#include "qapi/error.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"
|
2017-08-22 10:23:55 +03:00
|
|
|
#include "scsi/constants.h"
|
2016-06-22 20:11:19 +03:00
|
|
|
#include "hw/virtio/virtio-bus.h"
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
/* Context: QEMU global mutex held */
|
2016-10-21 23:48:10 +03:00
|
|
|
void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
|
2014-09-23 11:49:24 +04:00
|
|
|
{
|
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
|
2016-10-21 23:48:10 +03:00
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(s);
|
|
|
|
BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2014-09-23 11:49:24 +04:00
|
|
|
|
2016-10-21 23:48:10 +03: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 11:49:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:35:00 +03:00
|
|
|
static int virtio_scsi_set_host_notifier(VirtIOSCSI *s, VirtQueue *vq, int n)
|
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-02-14 20:17:10 +03:00
|
|
|
return 0;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
2018-03-07 17:42:04 +03:00
|
|
|
/* Context: BH in IOThread */
|
|
|
|
static void virtio_scsi_dataplane_stop_bh(void *opaque)
|
2014-10-15 17:15:25 +04:00
|
|
|
{
|
2018-03-07 17:42:04 +03:00
|
|
|
VirtIOSCSI *s = opaque;
|
2014-10-15 17:15:25 +04:00
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
|
2023-05-16 22:02:34 +03:00
|
|
|
EventNotifier *host_notifier;
|
2014-10-15 17:15:25 +04:00
|
|
|
int i;
|
|
|
|
|
2021-12-07 16:23:36 +03:00
|
|
|
virtio_queue_aio_detach_host_notifier(vs->ctrl_vq, s->ctx);
|
2023-05-16 22:02:34 +03:00
|
|
|
host_notifier = virtio_queue_get_host_notifier(vs->ctrl_vq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test and clear notifier after disabling event, in case poll callback
|
|
|
|
* didn't have time to run.
|
|
|
|
*/
|
|
|
|
virtio_queue_host_notifier_read(host_notifier);
|
|
|
|
|
2021-12-07 16:23:36 +03:00
|
|
|
virtio_queue_aio_detach_host_notifier(vs->event_vq, s->ctx);
|
2023-05-16 22:02:34 +03:00
|
|
|
host_notifier = virtio_queue_get_host_notifier(vs->event_vq);
|
|
|
|
virtio_queue_host_notifier_read(host_notifier);
|
|
|
|
|
2016-02-14 20:17:10 +03:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2021-12-07 16:23:36 +03:00
|
|
|
virtio_queue_aio_detach_host_notifier(vs->cmd_vqs[i], s->ctx);
|
2023-05-16 22:02:34 +03:00
|
|
|
host_notifier = virtio_queue_get_host_notifier(vs->cmd_vqs[i]);
|
|
|
|
virtio_queue_host_notifier_read(host_notifier);
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:49:24 +04:00
|
|
|
/* Context: QEMU global mutex held */
|
2016-10-21 23:48:10 +03:00
|
|
|
int virtio_scsi_dataplane_start(VirtIODevice *vdev)
|
2014-09-23 11:49:24 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rc;
|
2020-12-17 18:00:39 +03:00
|
|
|
int vq_init_count = 0;
|
2016-10-21 23:48:10 +03:00
|
|
|
BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
|
2014-09-23 11:49:24 +04:00
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2016-10-21 23:48:10 +03:00
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
|
|
|
|
VirtIOSCSI *s = VIRTIO_SCSI(vdev);
|
2014-09-23 11:49:24 +04:00
|
|
|
|
|
|
|
if (s->dataplane_started ||
|
|
|
|
s->dataplane_starting ||
|
2016-10-21 23:48:10 +03:00
|
|
|
s->dataplane_fenced) {
|
|
|
|
return 0;
|
2014-09-23 11:49:24 +04: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 08:05:19 +03:00
|
|
|
error_report("virtio-scsi: Failed to set guest notifiers (%d), "
|
|
|
|
"ensure -accel kvm is set.", rc);
|
2014-10-15 17:15:25 +04:00
|
|
|
goto fail_guest_notifiers;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
2021-05-17 16:26:37 +03:00
|
|
|
/*
|
|
|
|
* Batch all the host notifiers in a single transaction to avoid
|
|
|
|
* quadratic time complexity in address_space_update_ioeventfds().
|
|
|
|
*/
|
2021-04-07 17:35:01 +03:00
|
|
|
memory_region_transaction_begin();
|
|
|
|
|
2021-04-07 17:35:00 +03:00
|
|
|
rc = virtio_scsi_set_host_notifier(s, vs->ctrl_vq, 0);
|
|
|
|
if (rc != 0) {
|
|
|
|
goto fail_host_notifiers;
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
2020-12-17 18:00:39 +03:00
|
|
|
|
|
|
|
vq_init_count++;
|
2021-04-07 17:35:00 +03:00
|
|
|
rc = virtio_scsi_set_host_notifier(s, vs->event_vq, 1);
|
|
|
|
if (rc != 0) {
|
|
|
|
goto fail_host_notifiers;
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
2020-12-17 18:00:39 +03:00
|
|
|
|
|
|
|
vq_init_count++;
|
2021-04-07 17:35:00 +03:00
|
|
|
|
2014-09-23 11:49:24 +04:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
2021-04-07 17:35:00 +03:00
|
|
|
rc = virtio_scsi_set_host_notifier(s, vs->cmd_vqs[i], i + 2);
|
2016-02-14 20:17:10 +03:00
|
|
|
if (rc) {
|
2021-04-07 17:35:00 +03:00
|
|
|
goto fail_host_notifiers;
|
2014-10-15 17:15:25 +04:00
|
|
|
}
|
2020-12-17 18:00:39 +03:00
|
|
|
vq_init_count++;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:35:01 +03:00
|
|
|
memory_region_transaction_commit();
|
|
|
|
|
2022-08-08 19:21:34 +03:00
|
|
|
/*
|
|
|
|
* These fields are visible to the IOThread so we rely on implicit barriers
|
|
|
|
* in aio_context_acquire() on the write side and aio_notify_accept() on
|
|
|
|
* the read side.
|
|
|
|
*/
|
|
|
|
s->dataplane_starting = false;
|
|
|
|
s->dataplane_started = true;
|
|
|
|
|
2023-05-16 22:02:36 +03:00
|
|
|
if (s->bus.drain_count == 0) {
|
|
|
|
aio_context_acquire(s->ctx);
|
|
|
|
virtio_queue_aio_attach_host_notifier(vs->ctrl_vq, s->ctx);
|
|
|
|
virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq, s->ctx);
|
2021-04-07 17:35:00 +03:00
|
|
|
|
2023-05-16 22:02:36 +03:00
|
|
|
for (i = 0; i < vs->conf.num_queues; i++) {
|
|
|
|
virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], s->ctx);
|
|
|
|
}
|
|
|
|
aio_context_release(s->ctx);
|
2021-04-07 17:35:00 +03:00
|
|
|
}
|
2016-10-21 23:48:10 +03:00
|
|
|
return 0;
|
2014-10-15 17:15:25 +04:00
|
|
|
|
2021-04-07 17:35:00 +03:00
|
|
|
fail_host_notifiers:
|
2020-12-17 18:00:39 +03:00
|
|
|
for (i = 0; i < vq_init_count; i++) {
|
2016-06-10 12:04:14 +03:00
|
|
|
virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
|
2021-04-07 17:35:01 +03:00
|
|
|
}
|
|
|
|
|
2021-05-17 16:26:37 +03:00
|
|
|
/*
|
|
|
|
* The transaction expects the ioeventfds to be open when it
|
|
|
|
* commits. Do it now, before the cleanup loop.
|
|
|
|
*/
|
2021-04-07 17:35:01 +03:00
|
|
|
memory_region_transaction_commit();
|
|
|
|
|
|
|
|
for (i = 0; i < vq_init_count; i++) {
|
2018-01-29 17:20:56 +03:00
|
|
|
virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
|
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;
|
2016-10-21 23:48:10 +03:00
|
|
|
return -ENOSYS;
|
2014-09-23 11:49:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Context: QEMU global mutex held */
|
2016-10-21 23:48:10 +03:00
|
|
|
void virtio_scsi_dataplane_stop(VirtIODevice *vdev)
|
2014-09-23 11:49:24 +04:00
|
|
|
{
|
2016-10-21 23:48:10 +03:00
|
|
|
BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
|
2014-09-23 11:49:24 +04:00
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2016-10-21 23:48:10 +03:00
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
|
|
|
|
VirtIOSCSI *s = VIRTIO_SCSI(vdev);
|
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;
|
|
|
|
|
2023-05-16 22:02:36 +03:00
|
|
|
if (s->bus.drain_count == 0) {
|
|
|
|
aio_wait_bh_oneshot(s->ctx, virtio_scsi_dataplane_stop_bh, 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
|
|
|
|
2021-05-17 16:26:37 +03:00
|
|
|
/*
|
|
|
|
* Batch all the host notifiers in a single transaction to avoid
|
|
|
|
* quadratic time complexity in address_space_update_ioeventfds().
|
|
|
|
*/
|
2021-04-07 17:35:01 +03:00
|
|
|
memory_region_transaction_begin();
|
|
|
|
|
2014-09-23 11:49:24 +04:00
|
|
|
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);
|
2021-04-07 17:35:01 +03:00
|
|
|
}
|
|
|
|
|
2021-05-17 16:26:37 +03:00
|
|
|
/*
|
|
|
|
* The transaction expects the ioeventfds to be open when it
|
|
|
|
* commits. Do it now, before the cleanup loop.
|
|
|
|
*/
|
2021-04-07 17:35:01 +03:00
|
|
|
memory_region_transaction_commit();
|
|
|
|
|
|
|
|
for (i = 0; i < vs->conf.num_queues + 2; i++) {
|
2018-01-29 17:20:56 +03:00
|
|
|
virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
|
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;
|
|
|
|
}
|