1 /* 2 * vhost_scsi host device 3 * 4 * Copyright IBM, Corp. 2011 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 8 * 9 * Changes for QEMU mainline + tcm_vhost kernel upstream: 10 * Nicholas Bellinger <nab@risingtidesystems.com> 11 * 12 * This work is licensed under the terms of the GNU LGPL, version 2 or later. 13 * See the COPYING.LIB file in the top-level directory. 14 * 15 */ 16 17 #include <sys/ioctl.h> 18 #include "config.h" 19 #include "qemu/queue.h" 20 #include "monitor/monitor.h" 21 #include "migration/migration.h" 22 #include "hw/virtio/vhost-scsi.h" 23 #include "hw/virtio/vhost.h" 24 #include "hw/virtio/virtio-scsi.h" 25 #include "hw/virtio/virtio-bus.h" 26 #include "hw/virtio/virtio-access.h" 27 28 /* Features supported by host kernel. */ 29 static const int kernel_feature_bits[] = { 30 VIRTIO_F_NOTIFY_ON_EMPTY, 31 VIRTIO_RING_F_INDIRECT_DESC, 32 VIRTIO_RING_F_EVENT_IDX, 33 VIRTIO_SCSI_F_HOTPLUG, 34 VHOST_INVALID_FEATURE_BIT 35 }; 36 37 static int vhost_scsi_set_endpoint(VHostSCSI *s) 38 { 39 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 40 const VhostOps *vhost_ops = s->dev.vhost_ops; 41 struct vhost_scsi_target backend; 42 int ret; 43 44 memset(&backend, 0, sizeof(backend)); 45 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 46 ret = vhost_ops->vhost_call(&s->dev, VHOST_SCSI_SET_ENDPOINT, &backend); 47 if (ret < 0) { 48 return -errno; 49 } 50 return 0; 51 } 52 53 static void vhost_scsi_clear_endpoint(VHostSCSI *s) 54 { 55 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 56 struct vhost_scsi_target backend; 57 const VhostOps *vhost_ops = s->dev.vhost_ops; 58 59 memset(&backend, 0, sizeof(backend)); 60 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 61 vhost_ops->vhost_call(&s->dev, VHOST_SCSI_CLEAR_ENDPOINT, &backend); 62 } 63 64 static int vhost_scsi_start(VHostSCSI *s) 65 { 66 int ret, abi_version, i; 67 VirtIODevice *vdev = VIRTIO_DEVICE(s); 68 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 69 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 70 const VhostOps *vhost_ops = s->dev.vhost_ops; 71 72 if (!k->set_guest_notifiers) { 73 error_report("binding does not support guest notifiers"); 74 return -ENOSYS; 75 } 76 77 ret = vhost_ops->vhost_call(&s->dev, 78 VHOST_SCSI_GET_ABI_VERSION, &abi_version); 79 if (ret < 0) { 80 return -errno; 81 } 82 if (abi_version > VHOST_SCSI_ABI_VERSION) { 83 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:" 84 " %d is greater than vhost_scsi userspace supports: %d, please" 85 " upgrade your version of QEMU\n", abi_version, 86 VHOST_SCSI_ABI_VERSION); 87 return -ENOSYS; 88 } 89 90 ret = vhost_dev_enable_notifiers(&s->dev, vdev); 91 if (ret < 0) { 92 return ret; 93 } 94 95 s->dev.acked_features = vdev->guest_features; 96 ret = vhost_dev_start(&s->dev, vdev); 97 if (ret < 0) { 98 error_report("Error start vhost dev"); 99 goto err_notifiers; 100 } 101 102 ret = vhost_scsi_set_endpoint(s); 103 if (ret < 0) { 104 error_report("Error set vhost-scsi endpoint"); 105 goto err_vhost_stop; 106 } 107 108 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); 109 if (ret < 0) { 110 error_report("Error binding guest notifier"); 111 goto err_endpoint; 112 } 113 114 /* guest_notifier_mask/pending not used yet, so just unmask 115 * everything here. virtio-pci will do the right thing by 116 * enabling/disabling irqfd. 117 */ 118 for (i = 0; i < s->dev.nvqs; i++) { 119 vhost_virtqueue_mask(&s->dev, vdev, i, false); 120 } 121 122 return ret; 123 124 err_endpoint: 125 vhost_scsi_clear_endpoint(s); 126 err_vhost_stop: 127 vhost_dev_stop(&s->dev, vdev); 128 err_notifiers: 129 vhost_dev_disable_notifiers(&s->dev, vdev); 130 return ret; 131 } 132 133 static void vhost_scsi_stop(VHostSCSI *s) 134 { 135 VirtIODevice *vdev = VIRTIO_DEVICE(s); 136 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 137 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 138 int ret = 0; 139 140 if (k->set_guest_notifiers) { 141 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); 142 if (ret < 0) { 143 error_report("vhost guest notifier cleanup failed: %d\n", ret); 144 } 145 } 146 assert(ret >= 0); 147 148 vhost_scsi_clear_endpoint(s); 149 vhost_dev_stop(&s->dev, vdev); 150 vhost_dev_disable_notifiers(&s->dev, vdev); 151 } 152 153 static uint32_t vhost_scsi_get_features(VirtIODevice *vdev, 154 uint32_t features) 155 { 156 VHostSCSI *s = VHOST_SCSI(vdev); 157 158 return vhost_get_features(&s->dev, kernel_feature_bits, features); 159 } 160 161 static void vhost_scsi_set_config(VirtIODevice *vdev, 162 const uint8_t *config) 163 { 164 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 165 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 166 167 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size || 168 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) { 169 error_report("vhost-scsi does not support changing the sense data and CDB sizes"); 170 exit(1); 171 } 172 } 173 174 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val) 175 { 176 VHostSCSI *s = (VHostSCSI *)vdev; 177 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK); 178 179 if (s->dev.started == start) { 180 return; 181 } 182 183 if (start) { 184 int ret; 185 186 ret = vhost_scsi_start(s); 187 if (ret < 0) { 188 error_report("virtio-scsi: unable to start vhost: %s\n", 189 strerror(-ret)); 190 191 /* There is no userspace virtio-scsi fallback so exit */ 192 exit(1); 193 } 194 } else { 195 vhost_scsi_stop(s); 196 } 197 } 198 199 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) 200 { 201 } 202 203 static void vhost_scsi_realize(DeviceState *dev, Error **errp) 204 { 205 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 206 VHostSCSI *s = VHOST_SCSI(dev); 207 Error *err = NULL; 208 int vhostfd = -1; 209 int ret; 210 211 if (!vs->conf.wwpn) { 212 error_setg(errp, "vhost-scsi: missing wwpn"); 213 return; 214 } 215 216 if (vs->conf.vhostfd) { 217 vhostfd = monitor_handle_fd_param(cur_mon, vs->conf.vhostfd); 218 if (vhostfd == -1) { 219 error_setg(errp, "vhost-scsi: unable to parse vhostfd"); 220 return; 221 } 222 } else { 223 vhostfd = open("/dev/vhost-scsi", O_RDWR); 224 if (vhostfd < 0) { 225 error_setg(errp, "vhost-scsi: open vhost char device failed: %s", 226 strerror(errno)); 227 return; 228 } 229 } 230 231 virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output, 232 vhost_dummy_handle_output, 233 vhost_dummy_handle_output); 234 if (err != NULL) { 235 error_propagate(errp, err); 236 close(vhostfd); 237 return; 238 } 239 240 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; 241 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs); 242 s->dev.vq_index = 0; 243 s->dev.backend_features = 0; 244 245 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd, 246 VHOST_BACKEND_TYPE_KERNEL, true); 247 if (ret < 0) { 248 error_setg(errp, "vhost-scsi: vhost initialization failed: %s", 249 strerror(-ret)); 250 return; 251 } 252 253 error_setg(&s->migration_blocker, 254 "vhost-scsi does not support migration"); 255 migrate_add_blocker(s->migration_blocker); 256 } 257 258 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp) 259 { 260 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 261 VHostSCSI *s = VHOST_SCSI(dev); 262 263 migrate_del_blocker(s->migration_blocker); 264 error_free(s->migration_blocker); 265 266 /* This will stop vhost backend. */ 267 vhost_scsi_set_status(vdev, 0); 268 269 g_free(s->dev.vqs); 270 271 virtio_scsi_common_unrealize(dev, errp); 272 } 273 274 static Property vhost_scsi_properties[] = { 275 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSI, parent_obj.conf), 276 DEFINE_PROP_END_OF_LIST(), 277 }; 278 279 static void vhost_scsi_class_init(ObjectClass *klass, void *data) 280 { 281 DeviceClass *dc = DEVICE_CLASS(klass); 282 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 283 284 dc->props = vhost_scsi_properties; 285 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 286 vdc->realize = vhost_scsi_realize; 287 vdc->unrealize = vhost_scsi_unrealize; 288 vdc->get_features = vhost_scsi_get_features; 289 vdc->set_config = vhost_scsi_set_config; 290 vdc->set_status = vhost_scsi_set_status; 291 } 292 293 static const TypeInfo vhost_scsi_info = { 294 .name = TYPE_VHOST_SCSI, 295 .parent = TYPE_VIRTIO_SCSI_COMMON, 296 .instance_size = sizeof(VHostSCSI), 297 .class_init = vhost_scsi_class_init, 298 }; 299 300 static void virtio_register_types(void) 301 { 302 type_register_static(&vhost_scsi_info); 303 } 304 305 type_init(virtio_register_types) 306