| 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 "qapi/error.h" |
| 20 | #include "qemu/error-report.h" |
| 21 | #include "qemu/module.h" |
| 22 | #include "hw/virtio/vhost.h" |
| 23 | #include "hw/virtio/vhost-scsi-common.h" |
| 24 | #include "hw/virtio/virtio-scsi.h" |
| 25 | #include "hw/virtio/virtio-bus.h" |
| 26 | #include "hw/virtio/virtio-access.h" |
| 27 | #include "hw/core/fw-path-provider.h" |
| 28 | |
| 29 | int vhost_scsi_common_start(VHostSCSICommon *vsc, Error **errp) |
| 30 | { |
| 31 | int ret, i; |
| 32 | VirtIODevice *vdev = VIRTIO_DEVICE(vsc); |
| 33 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 34 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 35 | |
| 36 | VirtIOSCSICommon *vs = (VirtIOSCSICommon *)vsc; |
| 37 | |
| 38 | if (!k->set_guest_notifiers) { |
| 39 | error_setg(errp, "binding does not support guest notifiers"); |
| 40 | return -ENOSYS; |
| 41 | } |
| 42 | |
| 43 | ret = vhost_dev_enable_notifiers(&vsc->dev, vdev); |
| 44 | if (ret < 0) { |
| 45 | error_setg_errno(errp, -ret, "Error enabling host notifiers"); |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, true); |
| 50 | if (ret < 0) { |
| 51 | error_setg_errno(errp, -ret, "Error binding guest notifier"); |
| 52 | goto err_host_notifiers; |
| 53 | } |
| 54 | |
| 55 | vsc->dev.acked_features = vdev->guest_features; |
| 56 | |
| 57 | ret = vhost_dev_prepare_inflight(&vsc->dev, vdev); |
| 58 | if (ret < 0) { |
| 59 | error_setg_errno(errp, -ret, "Error setting inflight format"); |
| 60 | goto err_guest_notifiers; |
| 61 | } |
| 62 | |
| 63 | if (vsc->inflight) { |
| 64 | if (!vsc->inflight->addr) { |
| 65 | ret = vhost_dev_get_inflight(&vsc->dev, |
| 66 | vs->conf.virtqueue_size, |
| 67 | vsc->inflight); |
| 68 | if (ret < 0) { |
| 69 | error_setg_errno(errp, -ret, "Error getting inflight"); |
| 70 | goto err_guest_notifiers; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | ret = vhost_dev_set_inflight(&vsc->dev, vsc->inflight); |
| 75 | if (ret < 0) { |
| 76 | error_setg_errno(errp, -ret, "Error setting inflight"); |
| 77 | goto err_guest_notifiers; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | ret = vhost_dev_start(&vsc->dev, vdev, true); |
| 82 | if (ret < 0) { |
| 83 | error_setg_errno(errp, -ret, "Error starting vhost dev"); |
| 84 | goto err_guest_notifiers; |
| 85 | } |
| 86 | |
| 87 | /* guest_notifier_mask/pending not used yet, so just unmask |
| 88 | * everything here. virtio-pci will do the right thing by |
| 89 | * enabling/disabling irqfd. |
| 90 | */ |
| 91 | for (i = 0; i < vsc->dev.nvqs; i++) { |
| 92 | vhost_virtqueue_mask(&vsc->dev, vdev, vsc->dev.vq_index + i, false); |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | |
| 97 | err_guest_notifiers: |
| 98 | k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); |
| 99 | err_host_notifiers: |
| 100 | vhost_dev_disable_notifiers(&vsc->dev, vdev); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | int vhost_scsi_common_stop(VHostSCSICommon *vsc) |
| 105 | { |
| 106 | VirtIODevice *vdev = VIRTIO_DEVICE(vsc); |
| 107 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 108 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 109 | int ret = 0; |
| 110 | |
| 111 | ret = vhost_dev_stop(&vsc->dev, vdev, true); |
| 112 | |
| 113 | if (k->set_guest_notifiers) { |
| 114 | int r = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); |
| 115 | if (r < 0) { |
| 116 | error_report("vhost guest notifier cleanup failed: %d", r); |
| 117 | return r; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | vhost_dev_disable_notifiers(&vsc->dev, vdev); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t features, |
| 126 | Error **errp) |
| 127 | { |
| 128 | VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev); |
| 129 | |
| 130 | /* Turn on predefined features supported by this device */ |
| 131 | features |= vsc->host_features; |
| 132 | |
| 133 | return vhost_get_features(&vsc->dev, vsc->feature_bits, features); |
| 134 | } |
| 135 | |
| 136 | void vhost_scsi_common_set_config(VirtIODevice *vdev, const uint8_t *config) |
| 137 | { |
| 138 | VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; |
| 139 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); |
| 140 | |
| 141 | if ((uint32_t)virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size || |
| 142 | (uint32_t)virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) { |
| 143 | error_report("vhost-scsi does not support changing the sense data and " |
| 144 | "CDB sizes"); |
| 145 | exit(1); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Implementation of an interface to adjust firmware path |
| 151 | * for the bootindex property handling. |
| 152 | */ |
| 153 | char *vhost_scsi_common_get_fw_dev_path(FWPathProvider *p, BusState *bus, |
| 154 | DeviceState *dev) |
| 155 | { |
| 156 | VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev); |
| 157 | /* format: /channel@channel/vhost-scsi@target,lun */ |
| 158 | return g_strdup_printf("/channel@%x/%s@%x,%x", vsc->channel, |
| 159 | qdev_fw_name(dev), vsc->target, vsc->lun); |
| 160 | } |
| 161 | |
| 162 | static const TypeInfo vhost_scsi_common_info = { |
| 163 | .name = TYPE_VHOST_SCSI_COMMON, |
| 164 | .parent = TYPE_VIRTIO_SCSI_COMMON, |
| 165 | .instance_size = sizeof(VHostSCSICommon), |
| 166 | .abstract = true, |
| 167 | }; |
| 168 | |
| 169 | static void virtio_register_types(void) |
| 170 | { |
| 171 | type_register_static(&vhost_scsi_common_info); |
| 172 | } |
| 173 | |
| 174 | type_init(virtio_register_types) |