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 "qemu/osdep.h" 18 #include <linux/vhost.h> 19 #include <sys/ioctl.h> 20 #include "qapi/error.h" 21 #include "qemu/error-report.h" 22 #include "qemu/module.h" 23 #include "monitor/monitor.h" 24 #include "migration/blocker.h" 25 #include "hw/virtio/vhost-scsi.h" 26 #include "hw/virtio/vhost.h" 27 #include "hw/virtio/virtio-scsi.h" 28 #include "hw/virtio/virtio-bus.h" 29 #include "hw/fw-path-provider.h" 30 #include "hw/qdev-properties.h" 31 #include "qemu/cutils.h" 32 #include "system/system.h" 33 34 /* Features supported by host kernel. */ 35 static const int kernel_feature_bits[] = { 36 VIRTIO_F_NOTIFY_ON_EMPTY, 37 VIRTIO_RING_F_INDIRECT_DESC, 38 VIRTIO_RING_F_EVENT_IDX, 39 VIRTIO_SCSI_F_HOTPLUG, 40 VIRTIO_F_RING_RESET, 41 VIRTIO_F_IN_ORDER, 42 VIRTIO_F_NOTIFICATION_DATA, 43 VHOST_INVALID_FEATURE_BIT 44 }; 45 46 static int vhost_scsi_set_endpoint(VHostSCSI *s) 47 { 48 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 49 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 50 const VhostOps *vhost_ops = vsc->dev.vhost_ops; 51 struct vhost_scsi_target backend; 52 int ret; 53 54 memset(&backend, 0, sizeof(backend)); 55 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 56 ret = vhost_ops->vhost_scsi_set_endpoint(&vsc->dev, &backend); 57 if (ret < 0) { 58 return -errno; 59 } 60 return 0; 61 } 62 63 static void vhost_scsi_clear_endpoint(VHostSCSI *s) 64 { 65 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 66 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 67 struct vhost_scsi_target backend; 68 const VhostOps *vhost_ops = vsc->dev.vhost_ops; 69 70 memset(&backend, 0, sizeof(backend)); 71 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 72 vhost_ops->vhost_scsi_clear_endpoint(&vsc->dev, &backend); 73 } 74 75 static int vhost_scsi_start(VHostSCSI *s) 76 { 77 int ret, abi_version; 78 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 79 const VhostOps *vhost_ops = vsc->dev.vhost_ops; 80 Error *local_err = NULL; 81 82 ret = vhost_ops->vhost_scsi_get_abi_version(&vsc->dev, &abi_version); 83 if (ret < 0) { 84 return -errno; 85 } 86 if (abi_version > VHOST_SCSI_ABI_VERSION) { 87 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:" 88 " %d is greater than vhost_scsi userspace supports: %d," 89 " please upgrade your version of QEMU", abi_version, 90 VHOST_SCSI_ABI_VERSION); 91 return -ENOSYS; 92 } 93 94 ret = vhost_scsi_common_start(vsc, &local_err); 95 if (ret < 0) { 96 error_reportf_err(local_err, "Error starting vhost-scsi: "); 97 return ret; 98 } 99 100 ret = vhost_scsi_set_endpoint(s); 101 if (ret < 0) { 102 error_report("Error setting vhost-scsi endpoint"); 103 vhost_scsi_common_stop(vsc); 104 } 105 106 return ret; 107 } 108 109 static void vhost_scsi_stop(VHostSCSI *s) 110 { 111 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 112 113 vhost_scsi_clear_endpoint(s); 114 vhost_scsi_common_stop(vsc); 115 } 116 117 static int vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val) 118 { 119 VHostSCSI *s = VHOST_SCSI(vdev); 120 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 121 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK); 122 123 if (!vdev->vm_running) { 124 start = false; 125 } 126 127 if (vhost_dev_is_started(&vsc->dev) == start) { 128 return 0; 129 } 130 131 if (start) { 132 int ret; 133 134 ret = vhost_scsi_start(s); 135 if (ret < 0) { 136 error_report("unable to start vhost-scsi: %s", strerror(-ret)); 137 exit(1); 138 } 139 } else { 140 vhost_scsi_stop(s); 141 } 142 return 0; 143 } 144 145 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) 146 { 147 } 148 149 static int vhost_scsi_pre_save(void *opaque) 150 { 151 VHostSCSICommon *vsc = opaque; 152 153 /* At this point, backend must be stopped, otherwise 154 * it might keep writing to memory. */ 155 assert(!vhost_dev_is_started(&vsc->dev)); 156 157 return 0; 158 } 159 160 static const VMStateDescription vmstate_virtio_vhost_scsi = { 161 .name = "virtio-vhost_scsi", 162 .minimum_version_id = 1, 163 .version_id = 1, 164 .fields = (const VMStateField[]) { 165 VMSTATE_VIRTIO_DEVICE, 166 VMSTATE_END_OF_LIST() 167 }, 168 .pre_save = vhost_scsi_pre_save, 169 }; 170 171 static int vhost_scsi_set_workers(VHostSCSICommon *vsc, bool per_virtqueue) 172 { 173 struct vhost_dev *dev = &vsc->dev; 174 struct vhost_vring_worker vq_worker; 175 struct vhost_worker_state worker; 176 int i, ret = 0; 177 178 /* Use default worker */ 179 if (!per_virtqueue || dev->nvqs == VHOST_SCSI_VQ_NUM_FIXED + 1) { 180 return 0; 181 } 182 183 /* 184 * ctl/evt share the first worker since it will be rare for them 185 * to send cmds while IO is running. 186 */ 187 for (i = VHOST_SCSI_VQ_NUM_FIXED + 1; i < dev->nvqs; i++) { 188 memset(&worker, 0, sizeof(worker)); 189 190 ret = dev->vhost_ops->vhost_new_worker(dev, &worker); 191 if (ret == -ENOTTY) { 192 /* 193 * worker ioctls are not implemented so just ignore and 194 * and continue device setup. 195 */ 196 warn_report("vhost-scsi: Backend supports a single worker. " 197 "Ignoring worker_per_virtqueue=true setting."); 198 ret = 0; 199 break; 200 } else if (ret) { 201 break; 202 } 203 204 memset(&vq_worker, 0, sizeof(vq_worker)); 205 vq_worker.worker_id = worker.worker_id; 206 vq_worker.index = i; 207 208 ret = dev->vhost_ops->vhost_attach_vring_worker(dev, &vq_worker); 209 if (ret == -ENOTTY) { 210 /* 211 * It's a bug for the kernel to have supported the worker creation 212 * ioctl but not attach. 213 */ 214 dev->vhost_ops->vhost_free_worker(dev, &worker); 215 break; 216 } else if (ret) { 217 break; 218 } 219 } 220 221 return ret; 222 } 223 224 static void vhost_scsi_realize(DeviceState *dev, Error **errp) 225 { 226 ERRP_GUARD(); 227 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 228 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev); 229 Error *err = NULL; 230 int vhostfd = -1; 231 int ret; 232 struct vhost_virtqueue *vqs = NULL; 233 234 if (!vs->conf.wwpn) { 235 error_setg(errp, "vhost-scsi: missing wwpn"); 236 return; 237 } 238 239 if (vs->conf.vhostfd) { 240 vhostfd = monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp); 241 if (vhostfd == -1) { 242 error_prepend(errp, "vhost-scsi: unable to parse vhostfd: "); 243 return; 244 } 245 } else { 246 vhostfd = open("/dev/vhost-scsi", O_RDWR); 247 if (vhostfd < 0) { 248 error_setg(errp, "vhost-scsi: open vhost char device failed: %s", 249 strerror(errno)); 250 return; 251 } 252 } 253 254 virtio_scsi_common_realize(dev, 255 vhost_dummy_handle_output, 256 vhost_dummy_handle_output, 257 vhost_dummy_handle_output, 258 &err); 259 if (err != NULL) { 260 error_propagate(errp, err); 261 goto close_fd; 262 } 263 264 if (!vsc->migratable) { 265 error_setg(&vsc->migration_blocker, 266 "vhost-scsi does not support migration in all cases. " 267 "When external environment supports it (Orchestrator migrates " 268 "target SCSI device state or use shared storage over network), " 269 "set 'migratable' property to true to enable migration."); 270 if (migrate_add_blocker_normal(&vsc->migration_blocker, errp) < 0) { 271 goto free_virtio; 272 } 273 } 274 275 vsc->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; 276 vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs); 277 vsc->dev.vqs = vqs; 278 vsc->dev.vq_index = 0; 279 vsc->dev.backend_features = 0; 280 281 ret = vhost_dev_init(&vsc->dev, (void *)(uintptr_t)vhostfd, 282 VHOST_BACKEND_TYPE_KERNEL, 0, errp); 283 if (ret < 0) { 284 /* 285 * vhost_dev_init calls vhost_dev_cleanup on error, which closes 286 * vhostfd, don't double close it. 287 */ 288 vhostfd = -1; 289 goto free_vqs; 290 } 291 292 ret = vhost_scsi_set_workers(vsc, vs->conf.worker_per_virtqueue); 293 if (ret < 0) { 294 error_setg(errp, "vhost-scsi: vhost worker setup failed: %s", 295 strerror(-ret)); 296 goto free_vqs; 297 } 298 299 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */ 300 vsc->channel = 0; 301 vsc->lun = 0; 302 /* Note: we can also get the minimum tpgt from kernel */ 303 vsc->target = vs->conf.boot_tpgt; 304 305 return; 306 307 free_vqs: 308 g_free(vqs); 309 if (!vsc->migratable) { 310 migrate_del_blocker(&vsc->migration_blocker); 311 } 312 free_virtio: 313 virtio_scsi_common_unrealize(dev); 314 close_fd: 315 if (vhostfd >= 0) { 316 close(vhostfd); 317 } 318 } 319 320 static void vhost_scsi_unrealize(DeviceState *dev) 321 { 322 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 323 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev); 324 struct vhost_virtqueue *vqs = vsc->dev.vqs; 325 326 if (!vsc->migratable) { 327 migrate_del_blocker(&vsc->migration_blocker); 328 } 329 330 /* This will stop vhost backend. */ 331 vhost_scsi_set_status(vdev, 0); 332 333 vhost_dev_cleanup(&vsc->dev); 334 g_free(vqs); 335 336 virtio_scsi_common_unrealize(dev); 337 } 338 339 static struct vhost_dev *vhost_scsi_get_vhost(VirtIODevice *vdev) 340 { 341 VHostSCSI *s = VHOST_SCSI(vdev); 342 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 343 return &vsc->dev; 344 } 345 346 static const Property vhost_scsi_properties[] = { 347 DEFINE_PROP_STRING("vhostfd", VirtIOSCSICommon, conf.vhostfd), 348 DEFINE_PROP_STRING("wwpn", VirtIOSCSICommon, conf.wwpn), 349 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0), 350 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues, 351 VIRTIO_SCSI_AUTO_NUM_QUEUES), 352 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size, 353 128), 354 DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSICommon, conf.seg_max_adjust, 355 true), 356 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors, 357 0xFFFF), 358 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128), 359 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features, 360 VIRTIO_SCSI_F_T10_PI, 361 false), 362 DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features, 363 VIRTIO_SCSI_F_HOTPLUG, 364 false), 365 DEFINE_PROP_BOOL("migratable", VHostSCSICommon, migratable, false), 366 DEFINE_PROP_BOOL("worker_per_virtqueue", VirtIOSCSICommon, 367 conf.worker_per_virtqueue, false), 368 }; 369 370 static void vhost_scsi_class_init(ObjectClass *klass, const void *data) 371 { 372 DeviceClass *dc = DEVICE_CLASS(klass); 373 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 374 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass); 375 376 device_class_set_props(dc, vhost_scsi_properties); 377 dc->vmsd = &vmstate_virtio_vhost_scsi; 378 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 379 vdc->realize = vhost_scsi_realize; 380 vdc->unrealize = vhost_scsi_unrealize; 381 vdc->get_features = vhost_scsi_common_get_features; 382 vdc->set_config = vhost_scsi_common_set_config; 383 vdc->set_status = vhost_scsi_set_status; 384 vdc->get_vhost = vhost_scsi_get_vhost; 385 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path; 386 } 387 388 static void vhost_scsi_instance_init(Object *obj) 389 { 390 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj); 391 392 vsc->feature_bits = kernel_feature_bits; 393 394 device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL, 395 DEVICE(vsc)); 396 } 397 398 static const TypeInfo vhost_scsi_info = { 399 .name = TYPE_VHOST_SCSI, 400 .parent = TYPE_VHOST_SCSI_COMMON, 401 .instance_size = sizeof(VHostSCSI), 402 .class_init = vhost_scsi_class_init, 403 .instance_init = vhost_scsi_instance_init, 404 .interfaces = (const InterfaceInfo[]) { 405 { TYPE_FW_PATH_PROVIDER }, 406 { } 407 }, 408 }; 409 410 static void virtio_register_types(void) 411 { 412 type_register_static(&vhost_scsi_info); 413 } 414 415 type_init(virtio_register_types) 416