@@ -22,6 +22,7 @@
#include "virtio-net.h"
#include "virtio-serial.h"
#include "virtio-scsi.h"
+#include "vhost-scsi.h"
#include "pci.h"
#include "qemu-error.h"
#include "msi.h"
@@ -1036,6 +1037,7 @@ static void virtio_scsi_exit_pci(PCIDevice *pci_dev)
}
static Property virtio_scsi_properties[] = {
+ DEFINE_PROP_VHOST_SCSI("vhost-scsi", VirtIOPCIProxy, scsi.vhost_scsi),
DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
@@ -13,9 +13,13 @@
*
*/
+#include "qemu-common.h"
+#include "qemu-error.h"
+#include "vhost-scsi.h"
#include "virtio-scsi.h"
#include <hw/scsi.h>
#include <hw/scsi-defs.h>
+#include "vhost.h"
#define VIRTIO_SCSI_VQ_SIZE 128
#define VIRTIO_SCSI_CDB_SIZE 32
@@ -144,6 +148,10 @@ typedef struct {
uint32_t cdb_size;
int resetting;
bool events_dropped;
+
+ bool vhost_started;
+ VHostSCSI *vhost_scsi;
+
VirtQueue *ctrl_vq;
VirtQueue *event_vq;
VirtQueue *cmd_vqs[0];
@@ -699,6 +707,38 @@ static struct SCSIBusInfo virtio_scsi_scsi_info = {
.load_request = virtio_scsi_load_request,
};
+static bool virtio_scsi_started(VirtIOSCSI *s, uint8_t val)
+{
+ return (val & VIRTIO_CONFIG_S_DRIVER_OK) && s->vdev.vm_running;
+}
+
+static void virtio_scsi_set_status(VirtIODevice *vdev, uint8_t val)
+{
+ VirtIOSCSI *s = (VirtIOSCSI *)vdev;
+ bool start = virtio_scsi_started(s, val);
+
+ if (s->vhost_started == start) {
+ return;
+ }
+
+ if (start) {
+ int ret;
+
+ ret = vhost_scsi_start(s->vhost_scsi, vdev);
+ if (ret < 0) {
+ error_report("virtio-scsi: unable to start vhost: %s\n",
+ strerror(-ret));
+
+ /* There is no userspace virtio-scsi fallback so exit */
+ exit(1);
+ }
+ } else {
+ vhost_scsi_stop(s->vhost_scsi, vdev);
+ }
+
+ s->vhost_started = start;
+}
+
VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
{
VirtIOSCSI *s;
@@ -712,12 +752,17 @@ VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
s->qdev = dev;
s->conf = proxyconf;
+ s->vhost_started = false;
+ s->vhost_scsi = s->conf->vhost_scsi;
/* TODO set up vdev function pointers */
s->vdev.get_config = virtio_scsi_get_config;
s->vdev.set_config = virtio_scsi_set_config;
s->vdev.get_features = virtio_scsi_get_features;
s->vdev.reset = virtio_scsi_reset;
+ if (s->vhost_scsi) {
+ s->vdev.set_status = virtio_scsi_set_status;
+ }
s->ctrl_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
virtio_scsi_handle_ctrl);
@@ -743,5 +788,9 @@ void virtio_scsi_exit(VirtIODevice *vdev)
{
VirtIOSCSI *s = (VirtIOSCSI *)vdev;
unregister_savevm(s->qdev, "virtio-scsi", s);
+
+ /* This will stop vhost backend if appropriate. */
+ virtio_scsi_set_status(vdev, 0);
+
virtio_cleanup(vdev);
}
@@ -22,6 +22,7 @@
#define VIRTIO_ID_SCSI 8
struct VirtIOSCSIConf {
+ VHostSCSI *vhost_scsi;
uint32_t num_queues;
uint32_t max_sectors;
uint32_t cmd_per_lun;