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 #include "hw/fw-path-provider.h" 28 29 /* Features supported by host kernel. */ 30 static const int kernel_feature_bits[] = { 31 VIRTIO_F_NOTIFY_ON_EMPTY, 32 VIRTIO_RING_F_INDIRECT_DESC, 33 VIRTIO_RING_F_EVENT_IDX, 34 VIRTIO_SCSI_F_HOTPLUG, 35 VHOST_INVALID_FEATURE_BIT 36 }; 37 38 static int vhost_scsi_set_endpoint(VHostSCSI *s) 39 { 40 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 41 const VhostOps *vhost_ops = s->dev.vhost_ops; 42 struct vhost_scsi_target backend; 43 int ret; 44 45 memset(&backend, 0, sizeof(backend)); 46 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 47 ret = vhost_ops->vhost_call(&s->dev, VHOST_SCSI_SET_ENDPOINT, &backend); 48 if (ret < 0) { 49 return -errno; 50 } 51 return 0; 52 } 53 54 static void vhost_scsi_clear_endpoint(VHostSCSI *s) 55 { 56 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 57 struct vhost_scsi_target backend; 58 const VhostOps *vhost_ops = s->dev.vhost_ops; 59 60 memset(&backend, 0, sizeof(backend)); 61 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn); 62 vhost_ops->vhost_call(&s->dev, VHOST_SCSI_CLEAR_ENDPOINT, &backend); 63 } 64 65 static int vhost_scsi_start(VHostSCSI *s) 66 { 67 int ret, abi_version, i; 68 VirtIODevice *vdev = VIRTIO_DEVICE(s); 69 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 70 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 71 const VhostOps *vhost_ops = s->dev.vhost_ops; 72 73 if (!k->set_guest_notifiers) { 74 error_report("binding does not support guest notifiers"); 75 return -ENOSYS; 76 } 77 78 ret = vhost_ops->vhost_call(&s->dev, 79 VHOST_SCSI_GET_ABI_VERSION, &abi_version); 80 if (ret < 0) { 81 return -errno; 82 } 83 if (abi_version > VHOST_SCSI_ABI_VERSION) { 84 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:" 85 " %d is greater than vhost_scsi userspace supports: %d, please" 86 " upgrade your version of QEMU", abi_version, 87 VHOST_SCSI_ABI_VERSION); 88 return -ENOSYS; 89 } 90 91 ret = vhost_dev_enable_notifiers(&s->dev, vdev); 92 if (ret < 0) { 93 return ret; 94 } 95 96 s->dev.acked_features = vdev->guest_features; 97 ret = vhost_dev_start(&s->dev, vdev); 98 if (ret < 0) { 99 error_report("Error start vhost dev"); 100 goto err_notifiers; 101 } 102 103 ret = vhost_scsi_set_endpoint(s); 104 if (ret < 0) { 105 error_report("Error set vhost-scsi endpoint"); 106 goto err_vhost_stop; 107 } 108 109 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); 110 if (ret < 0) { 111 error_report("Error binding guest notifier"); 112 goto err_endpoint; 113 } 114 115 /* guest_notifier_mask/pending not used yet, so just unmask 116 * everything here. virtio-pci will do the right thing by 117 * enabling/disabling irqfd. 118 */ 119 for (i = 0; i < s->dev.nvqs; i++) { 120 vhost_virtqueue_mask(&s->dev, vdev, i, false); 121 } 122 123 return ret; 124 125 err_endpoint: 126 vhost_scsi_clear_endpoint(s); 127 err_vhost_stop: 128 vhost_dev_stop(&s->dev, vdev); 129 err_notifiers: 130 vhost_dev_disable_notifiers(&s->dev, vdev); 131 return ret; 132 } 133 134 static void vhost_scsi_stop(VHostSCSI *s) 135 { 136 VirtIODevice *vdev = VIRTIO_DEVICE(s); 137 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 138 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 139 int ret = 0; 140 141 if (k->set_guest_notifiers) { 142 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); 143 if (ret < 0) { 144 error_report("vhost guest notifier cleanup failed: %d", ret); 145 } 146 } 147 assert(ret >= 0); 148 149 vhost_scsi_clear_endpoint(s); 150 vhost_dev_stop(&s->dev, vdev); 151 vhost_dev_disable_notifiers(&s->dev, vdev); 152 } 153 154 static uint64_t vhost_scsi_get_features(VirtIODevice *vdev, 155 uint64_t features) 156 { 157 VHostSCSI *s = VHOST_SCSI(vdev); 158 159 return vhost_get_features(&s->dev, kernel_feature_bits, features); 160 } 161 162 static void vhost_scsi_set_config(VirtIODevice *vdev, 163 const uint8_t *config) 164 { 165 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 166 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 167 168 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size || 169 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) { 170 error_report("vhost-scsi does not support changing the sense data and CDB sizes"); 171 exit(1); 172 } 173 } 174 175 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val) 176 { 177 VHostSCSI *s = (VHostSCSI *)vdev; 178 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK); 179 180 if (s->dev.started == start) { 181 return; 182 } 183 184 if (start) { 185 int ret; 186 187 ret = vhost_scsi_start(s); 188 if (ret < 0) { 189 error_report("virtio-scsi: unable to start vhost: %s", 190 strerror(-ret)); 191 192 /* There is no userspace virtio-scsi fallback so exit */ 193 exit(1); 194 } 195 } else { 196 vhost_scsi_stop(s); 197 } 198 } 199 200 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) 201 { 202 } 203 204 static void vhost_scsi_realize(DeviceState *dev, Error **errp) 205 { 206 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 207 VHostSCSI *s = VHOST_SCSI(dev); 208 Error *err = NULL; 209 int vhostfd = -1; 210 int ret; 211 212 if (!vs->conf.wwpn) { 213 error_setg(errp, "vhost-scsi: missing wwpn"); 214 return; 215 } 216 217 if (vs->conf.vhostfd) { 218 vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, &err); 219 if (vhostfd == -1) { 220 error_setg(errp, "vhost-scsi: unable to parse vhostfd: %s", 221 error_get_pretty(err)); 222 error_free(err); 223 return; 224 } 225 } else { 226 vhostfd = open("/dev/vhost-scsi", O_RDWR); 227 if (vhostfd < 0) { 228 error_setg(errp, "vhost-scsi: open vhost char device failed: %s", 229 strerror(errno)); 230 return; 231 } 232 } 233 234 virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output, 235 vhost_dummy_handle_output, 236 vhost_dummy_handle_output); 237 if (err != NULL) { 238 error_propagate(errp, err); 239 close(vhostfd); 240 return; 241 } 242 243 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; 244 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs); 245 s->dev.vq_index = 0; 246 s->dev.backend_features = 0; 247 248 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd, 249 VHOST_BACKEND_TYPE_KERNEL, true); 250 if (ret < 0) { 251 error_setg(errp, "vhost-scsi: vhost initialization failed: %s", 252 strerror(-ret)); 253 return; 254 } 255 256 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */ 257 s->channel = 0; 258 s->lun = 0; 259 /* Note: we can also get the minimum tpgt from kernel */ 260 s->target = vs->conf.boot_tpgt; 261 262 error_setg(&s->migration_blocker, 263 "vhost-scsi does not support migration"); 264 migrate_add_blocker(s->migration_blocker); 265 } 266 267 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp) 268 { 269 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 270 VHostSCSI *s = VHOST_SCSI(dev); 271 272 migrate_del_blocker(s->migration_blocker); 273 error_free(s->migration_blocker); 274 275 /* This will stop vhost backend. */ 276 vhost_scsi_set_status(vdev, 0); 277 278 g_free(s->dev.vqs); 279 280 virtio_scsi_common_unrealize(dev, errp); 281 } 282 283 /* 284 * Implementation of an interface to adjust firmware path 285 * for the bootindex property handling. 286 */ 287 static char *vhost_scsi_get_fw_dev_path(FWPathProvider *p, BusState *bus, 288 DeviceState *dev) 289 { 290 VHostSCSI *s = VHOST_SCSI(dev); 291 /* format: channel@channel/vhost-scsi@target,lun */ 292 return g_strdup_printf("channel@%x/%s@%x,%x", s->channel, 293 qdev_fw_name(dev), s->target, s->lun); 294 } 295 296 static Property vhost_scsi_properties[] = { 297 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSI, parent_obj.conf), 298 DEFINE_PROP_END_OF_LIST(), 299 }; 300 301 static void vhost_scsi_class_init(ObjectClass *klass, void *data) 302 { 303 DeviceClass *dc = DEVICE_CLASS(klass); 304 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 305 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass); 306 307 dc->props = vhost_scsi_properties; 308 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 309 vdc->realize = vhost_scsi_realize; 310 vdc->unrealize = vhost_scsi_unrealize; 311 vdc->get_features = vhost_scsi_get_features; 312 vdc->set_config = vhost_scsi_set_config; 313 vdc->set_status = vhost_scsi_set_status; 314 fwc->get_dev_path = vhost_scsi_get_fw_dev_path; 315 } 316 317 static void vhost_scsi_instance_init(Object *obj) 318 { 319 VHostSCSI *dev = VHOST_SCSI(obj); 320 321 device_add_bootindex_property(obj, &dev->bootindex, "bootindex", NULL, 322 DEVICE(dev), NULL); 323 } 324 325 static const TypeInfo vhost_scsi_info = { 326 .name = TYPE_VHOST_SCSI, 327 .parent = TYPE_VIRTIO_SCSI_COMMON, 328 .instance_size = sizeof(VHostSCSI), 329 .class_init = vhost_scsi_class_init, 330 .instance_init = vhost_scsi_instance_init, 331 .interfaces = (InterfaceInfo[]) { 332 { TYPE_FW_PATH_PROVIDER }, 333 { } 334 }, 335 }; 336 337 static void virtio_register_types(void) 338 { 339 type_register_static(&vhost_scsi_info); 340 } 341 342 type_init(virtio_register_types) 343