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 "qemu/queue.h" 24 #include "monitor/monitor.h" 25 #include "migration/blocker.h" 26 #include "hw/virtio/vhost-scsi.h" 27 #include "hw/virtio/vhost.h" 28 #include "hw/virtio/virtio-scsi.h" 29 #include "hw/virtio/virtio-bus.h" 30 #include "hw/virtio/virtio-access.h" 31 #include "hw/fw-path-provider.h" 32 #include "qemu/cutils.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 VHOST_INVALID_FEATURE_BIT 41 }; 42 43 static int vhost_scsi_set_endpoint(VHostSCSI *s) 44 { 45 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 46 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 47 const VhostOps *vhost_ops = vsc->dev.vhost_ops; 48 struct vhost_scsi_target backend; 49 int ret; 50 51 memset(&backend, 0, sizeof(backend)); 52 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 53 ret = vhost_ops->vhost_scsi_set_endpoint(&vsc->dev, &backend); 54 if (ret < 0) { 55 return -errno; 56 } 57 return 0; 58 } 59 60 static void vhost_scsi_clear_endpoint(VHostSCSI *s) 61 { 62 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 63 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 64 struct vhost_scsi_target backend; 65 const VhostOps *vhost_ops = vsc->dev.vhost_ops; 66 67 memset(&backend, 0, sizeof(backend)); 68 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 69 vhost_ops->vhost_scsi_clear_endpoint(&vsc->dev, &backend); 70 } 71 72 static int vhost_scsi_start(VHostSCSI *s) 73 { 74 int ret, abi_version; 75 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 76 const VhostOps *vhost_ops = vsc->dev.vhost_ops; 77 78 ret = vhost_ops->vhost_scsi_get_abi_version(&vsc->dev, &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," 85 " please upgrade your version of QEMU", abi_version, 86 VHOST_SCSI_ABI_VERSION); 87 return -ENOSYS; 88 } 89 90 ret = vhost_scsi_common_start(vsc); 91 if (ret < 0) { 92 return ret; 93 } 94 95 ret = vhost_scsi_set_endpoint(s); 96 if (ret < 0) { 97 error_report("Error setting vhost-scsi endpoint"); 98 vhost_scsi_common_stop(vsc); 99 } 100 101 return ret; 102 } 103 104 static void vhost_scsi_stop(VHostSCSI *s) 105 { 106 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 107 108 vhost_scsi_clear_endpoint(s); 109 vhost_scsi_common_stop(vsc); 110 } 111 112 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val) 113 { 114 VHostSCSI *s = VHOST_SCSI(vdev); 115 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 116 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK); 117 118 if (!vdev->vm_running) { 119 start = false; 120 } 121 122 if (vsc->dev.started == start) { 123 return; 124 } 125 126 if (start) { 127 int ret; 128 129 ret = vhost_scsi_start(s); 130 if (ret < 0) { 131 error_report("unable to start vhost-scsi: %s", strerror(-ret)); 132 exit(1); 133 } 134 } else { 135 vhost_scsi_stop(s); 136 } 137 } 138 139 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) 140 { 141 } 142 143 static int vhost_scsi_pre_save(void *opaque) 144 { 145 VHostSCSICommon *vsc = opaque; 146 147 /* At this point, backend must be stopped, otherwise 148 * it might keep writing to memory. */ 149 assert(!vsc->dev.started); 150 151 return 0; 152 } 153 154 static const VMStateDescription vmstate_virtio_vhost_scsi = { 155 .name = "virtio-vhost_scsi", 156 .minimum_version_id = 1, 157 .version_id = 1, 158 .fields = (VMStateField[]) { 159 VMSTATE_VIRTIO_DEVICE, 160 VMSTATE_END_OF_LIST() 161 }, 162 .pre_save = vhost_scsi_pre_save, 163 }; 164 165 static void vhost_scsi_realize(DeviceState *dev, Error **errp) 166 { 167 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 168 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev); 169 Error *err = NULL; 170 int vhostfd = -1; 171 int ret; 172 173 if (!vs->conf.wwpn) { 174 error_setg(errp, "vhost-scsi: missing wwpn"); 175 return; 176 } 177 178 if (vs->conf.vhostfd) { 179 vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, errp); 180 if (vhostfd == -1) { 181 error_prepend(errp, "vhost-scsi: unable to parse vhostfd: "); 182 return; 183 } 184 } else { 185 vhostfd = open("/dev/vhost-scsi", O_RDWR); 186 if (vhostfd < 0) { 187 error_setg(errp, "vhost-scsi: open vhost char device failed: %s", 188 strerror(errno)); 189 return; 190 } 191 } 192 193 virtio_scsi_common_realize(dev, 194 vhost_dummy_handle_output, 195 vhost_dummy_handle_output, 196 vhost_dummy_handle_output, 197 &err); 198 if (err != NULL) { 199 error_propagate(errp, err); 200 goto close_fd; 201 } 202 203 if (!vsc->migratable) { 204 error_setg(&vsc->migration_blocker, 205 "vhost-scsi does not support migration in all cases. " 206 "When external environment supports it (Orchestrator migrates " 207 "target SCSI device state or use shared storage over network), " 208 "set 'migratable' property to true to enable migration."); 209 migrate_add_blocker(vsc->migration_blocker, &err); 210 if (err) { 211 error_propagate(errp, err); 212 error_free(vsc->migration_blocker); 213 goto close_fd; 214 } 215 } 216 217 vsc->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; 218 vsc->dev.vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs); 219 vsc->dev.vq_index = 0; 220 vsc->dev.backend_features = 0; 221 222 ret = vhost_dev_init(&vsc->dev, (void *)(uintptr_t)vhostfd, 223 VHOST_BACKEND_TYPE_KERNEL, 0); 224 if (ret < 0) { 225 error_setg(errp, "vhost-scsi: vhost initialization failed: %s", 226 strerror(-ret)); 227 goto free_vqs; 228 } 229 230 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */ 231 vsc->channel = 0; 232 vsc->lun = 0; 233 /* Note: we can also get the minimum tpgt from kernel */ 234 vsc->target = vs->conf.boot_tpgt; 235 236 return; 237 238 free_vqs: 239 if (!vsc->migratable) { 240 migrate_del_blocker(vsc->migration_blocker); 241 } 242 g_free(vsc->dev.vqs); 243 close_fd: 244 close(vhostfd); 245 return; 246 } 247 248 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp) 249 { 250 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 251 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev); 252 struct vhost_virtqueue *vqs = vsc->dev.vqs; 253 254 if (!vsc->migratable) { 255 migrate_del_blocker(vsc->migration_blocker); 256 error_free(vsc->migration_blocker); 257 } 258 259 /* This will stop vhost backend. */ 260 vhost_scsi_set_status(vdev, 0); 261 262 vhost_dev_cleanup(&vsc->dev); 263 g_free(vqs); 264 265 virtio_scsi_common_unrealize(dev, errp); 266 } 267 268 static Property vhost_scsi_properties[] = { 269 DEFINE_PROP_STRING("vhostfd", VirtIOSCSICommon, conf.vhostfd), 270 DEFINE_PROP_STRING("wwpn", VirtIOSCSICommon, conf.wwpn), 271 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0), 272 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues, 1), 273 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size, 274 128), 275 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors, 276 0xFFFF), 277 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128), 278 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features, 279 VIRTIO_SCSI_F_T10_PI, 280 false), 281 DEFINE_PROP_BOOL("migratable", VHostSCSICommon, migratable, false), 282 DEFINE_PROP_END_OF_LIST(), 283 }; 284 285 static void vhost_scsi_class_init(ObjectClass *klass, void *data) 286 { 287 DeviceClass *dc = DEVICE_CLASS(klass); 288 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 289 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass); 290 291 dc->props = vhost_scsi_properties; 292 dc->vmsd = &vmstate_virtio_vhost_scsi; 293 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 294 vdc->realize = vhost_scsi_realize; 295 vdc->unrealize = vhost_scsi_unrealize; 296 vdc->get_features = vhost_scsi_common_get_features; 297 vdc->set_config = vhost_scsi_common_set_config; 298 vdc->set_status = vhost_scsi_set_status; 299 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path; 300 } 301 302 static void vhost_scsi_instance_init(Object *obj) 303 { 304 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj); 305 306 vsc->feature_bits = kernel_feature_bits; 307 308 device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL, 309 DEVICE(vsc), NULL); 310 } 311 312 static const TypeInfo vhost_scsi_info = { 313 .name = TYPE_VHOST_SCSI, 314 .parent = TYPE_VHOST_SCSI_COMMON, 315 .instance_size = sizeof(VHostSCSI), 316 .class_init = vhost_scsi_class_init, 317 .instance_init = vhost_scsi_instance_init, 318 .interfaces = (InterfaceInfo[]) { 319 { TYPE_FW_PATH_PROVIDER }, 320 { } 321 }, 322 }; 323 324 static void virtio_register_types(void) 325 { 326 type_register_static(&vhost_scsi_info); 327 } 328 329 type_init(virtio_register_types) 330