2017-03-03 02:25:51 +08:00
|
|
|
/*
|
|
|
|
* vhost-scsi-common
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Nutanix Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Felipe Franciosi <felipe@nutanix.com>
|
|
|
|
*
|
|
|
|
* This work is largely based on the "vhost-scsi" implementation by:
|
|
|
|
* Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
|
|
|
|
* Nicholas Bellinger <nab@risingtidesystems.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 22:35:07 +08:00
|
|
|
#include "qemu/module.h"
|
2017-03-03 02:25:51 +08:00
|
|
|
#include "hw/virtio/vhost.h"
|
|
|
|
#include "hw/virtio/vhost-scsi-common.h"
|
|
|
|
#include "hw/virtio/virtio-scsi.h"
|
|
|
|
#include "hw/virtio/virtio-bus.h"
|
|
|
|
#include "hw/virtio/virtio-access.h"
|
|
|
|
#include "hw/fw-path-provider.h"
|
|
|
|
|
|
|
|
int vhost_scsi_common_start(VHostSCSICommon *vsc)
|
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vsc);
|
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
|
|
|
|
2020-09-09 20:20:21 +08:00
|
|
|
VirtIOSCSICommon *vs = (VirtIOSCSICommon *)vsc;
|
|
|
|
|
2017-03-03 02:25:51 +08:00
|
|
|
if (!k->set_guest_notifiers) {
|
|
|
|
error_report("binding does not support guest notifiers");
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = vhost_dev_enable_notifiers(&vsc->dev, vdev);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, true);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report("Error binding guest notifier");
|
|
|
|
goto err_host_notifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
vsc->dev.acked_features = vdev->guest_features;
|
2020-09-09 20:20:21 +08:00
|
|
|
|
|
|
|
assert(vsc->inflight == NULL);
|
|
|
|
vsc->inflight = g_new0(struct vhost_inflight, 1);
|
|
|
|
ret = vhost_dev_get_inflight(&vsc->dev,
|
|
|
|
vs->conf.virtqueue_size,
|
|
|
|
vsc->inflight);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report("Error get inflight: %d", -ret);
|
|
|
|
goto err_guest_notifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = vhost_dev_set_inflight(&vsc->dev, vsc->inflight);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report("Error set inflight: %d", -ret);
|
|
|
|
goto err_guest_notifiers;
|
|
|
|
}
|
|
|
|
|
2017-03-03 02:25:51 +08:00
|
|
|
ret = vhost_dev_start(&vsc->dev, vdev);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report("Error start vhost dev");
|
|
|
|
goto err_guest_notifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* guest_notifier_mask/pending not used yet, so just unmask
|
|
|
|
* everything here. virtio-pci will do the right thing by
|
|
|
|
* enabling/disabling irqfd.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < vsc->dev.nvqs; i++) {
|
|
|
|
vhost_virtqueue_mask(&vsc->dev, vdev, vsc->dev.vq_index + i, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err_guest_notifiers:
|
2020-09-09 20:20:21 +08:00
|
|
|
g_free(vsc->inflight);
|
|
|
|
vsc->inflight = NULL;
|
|
|
|
|
2017-03-03 02:25:51 +08:00
|
|
|
k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
|
|
|
|
err_host_notifiers:
|
|
|
|
vhost_dev_disable_notifiers(&vsc->dev, vdev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vhost_scsi_common_stop(VHostSCSICommon *vsc)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vsc);
|
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
vhost_dev_stop(&vsc->dev, vdev);
|
|
|
|
|
|
|
|
if (k->set_guest_notifiers) {
|
|
|
|
ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report("vhost guest notifier cleanup failed: %d", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(ret >= 0);
|
|
|
|
|
2020-09-09 20:20:21 +08:00
|
|
|
if (vsc->inflight) {
|
|
|
|
vhost_dev_free_inflight(vsc->inflight);
|
|
|
|
vsc->inflight = NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-03 02:25:51 +08:00
|
|
|
vhost_dev_disable_notifiers(&vsc->dev, vdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t features,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
|
|
|
|
|
2018-08-09 03:52:34 +08:00
|
|
|
/* Turn on predefined features supported by this device */
|
|
|
|
features |= vsc->host_features;
|
|
|
|
|
2017-03-03 02:25:51 +08:00
|
|
|
return vhost_get_features(&vsc->dev, vsc->feature_bits, features);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vhost_scsi_common_set_config(VirtIODevice *vdev, const uint8_t *config)
|
|
|
|
{
|
|
|
|
VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
|
|
|
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
|
|
|
|
|
|
|
|
if ((uint32_t)virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size ||
|
|
|
|
(uint32_t)virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) {
|
|
|
|
error_report("vhost-scsi does not support changing the sense data and "
|
|
|
|
"CDB sizes");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of an interface to adjust firmware path
|
|
|
|
* for the bootindex property handling.
|
|
|
|
*/
|
|
|
|
char *vhost_scsi_common_get_fw_dev_path(FWPathProvider *p, BusState *bus,
|
|
|
|
DeviceState *dev)
|
|
|
|
{
|
|
|
|
VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
|
|
|
|
/* format: /channel@channel/vhost-scsi@target,lun */
|
|
|
|
return g_strdup_printf("/channel@%x/%s@%x,%x", vsc->channel,
|
|
|
|
qdev_fw_name(dev), vsc->target, vsc->lun);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo vhost_scsi_common_info = {
|
|
|
|
.name = TYPE_VHOST_SCSI_COMMON,
|
|
|
|
.parent = TYPE_VIRTIO_SCSI_COMMON,
|
|
|
|
.instance_size = sizeof(VHostSCSICommon),
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&vhost_scsi_common_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(virtio_register_types)
|