1 /* 2 * vhost-scsi-common 3 * 4 * Copyright (c) 2016 Nutanix Inc. All rights reserved. 5 * 6 * Author: 7 * Felipe Franciosi <felipe@nutanix.com> 8 * 9 * This work is largely based on the "vhost-scsi" implementation by: 10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 11 * Nicholas Bellinger <nab@risingtidesystems.com> 12 * 13 * This work is licensed under the terms of the GNU LGPL, version 2 or later. 14 * See the COPYING.LIB file in the top-level directory. 15 * 16 */ 17 18 #include "qemu/osdep.h" 19 #include "qemu/error-report.h" 20 #include "qemu/module.h" 21 #include "hw/virtio/vhost.h" 22 #include "hw/virtio/vhost-scsi-common.h" 23 #include "hw/virtio/virtio-scsi.h" 24 #include "hw/virtio/virtio-bus.h" 25 #include "hw/virtio/virtio-access.h" 26 #include "hw/fw-path-provider.h" 27 28 int vhost_scsi_common_start(VHostSCSICommon *vsc) 29 { 30 int ret, i; 31 VirtIODevice *vdev = VIRTIO_DEVICE(vsc); 32 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 33 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 34 35 if (!k->set_guest_notifiers) { 36 error_report("binding does not support guest notifiers"); 37 return -ENOSYS; 38 } 39 40 ret = vhost_dev_enable_notifiers(&vsc->dev, vdev); 41 if (ret < 0) { 42 return ret; 43 } 44 45 ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, true); 46 if (ret < 0) { 47 error_report("Error binding guest notifier"); 48 goto err_host_notifiers; 49 } 50 51 vsc->dev.acked_features = vdev->guest_features; 52 ret = vhost_dev_start(&vsc->dev, vdev); 53 if (ret < 0) { 54 error_report("Error start vhost dev"); 55 goto err_guest_notifiers; 56 } 57 58 /* guest_notifier_mask/pending not used yet, so just unmask 59 * everything here. virtio-pci will do the right thing by 60 * enabling/disabling irqfd. 61 */ 62 for (i = 0; i < vsc->dev.nvqs; i++) { 63 vhost_virtqueue_mask(&vsc->dev, vdev, vsc->dev.vq_index + i, false); 64 } 65 66 return ret; 67 68 err_guest_notifiers: 69 k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); 70 err_host_notifiers: 71 vhost_dev_disable_notifiers(&vsc->dev, vdev); 72 return ret; 73 } 74 75 void vhost_scsi_common_stop(VHostSCSICommon *vsc) 76 { 77 VirtIODevice *vdev = VIRTIO_DEVICE(vsc); 78 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 79 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 80 int ret = 0; 81 82 vhost_dev_stop(&vsc->dev, vdev); 83 84 if (k->set_guest_notifiers) { 85 ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); 86 if (ret < 0) { 87 error_report("vhost guest notifier cleanup failed: %d", ret); 88 } 89 } 90 assert(ret >= 0); 91 92 vhost_dev_disable_notifiers(&vsc->dev, vdev); 93 } 94 95 uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t features, 96 Error **errp) 97 { 98 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev); 99 100 /* Turn on predefined features supported by this device */ 101 features |= vsc->host_features; 102 103 return vhost_get_features(&vsc->dev, vsc->feature_bits, features); 104 } 105 106 void vhost_scsi_common_set_config(VirtIODevice *vdev, const uint8_t *config) 107 { 108 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 109 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 110 111 if ((uint32_t)virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size || 112 (uint32_t)virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) { 113 error_report("vhost-scsi does not support changing the sense data and " 114 "CDB sizes"); 115 exit(1); 116 } 117 } 118 119 /* 120 * Implementation of an interface to adjust firmware path 121 * for the bootindex property handling. 122 */ 123 char *vhost_scsi_common_get_fw_dev_path(FWPathProvider *p, BusState *bus, 124 DeviceState *dev) 125 { 126 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev); 127 /* format: /channel@channel/vhost-scsi@target,lun */ 128 return g_strdup_printf("/channel@%x/%s@%x,%x", vsc->channel, 129 qdev_fw_name(dev), vsc->target, vsc->lun); 130 } 131 132 static const TypeInfo vhost_scsi_common_info = { 133 .name = TYPE_VHOST_SCSI_COMMON, 134 .parent = TYPE_VIRTIO_SCSI_COMMON, 135 .instance_size = sizeof(VHostSCSICommon), 136 .abstract = true, 137 }; 138 139 static void virtio_register_types(void) 140 { 141 type_register_static(&vhost_scsi_common_info); 142 } 143 144 type_init(virtio_register_types) 145