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\n", 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\n", 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 uint32_t vhost_scsi_get_features(VirtIODevice *vdev, 155 uint32_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\n", 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_handle_fd_param(cur_mon, vs->conf.vhostfd); 219 if (vhostfd == -1) { 220 error_setg(errp, "vhost-scsi: unable to parse vhostfd"); 221 return; 222 } 223 } else { 224 vhostfd = open("/dev/vhost-scsi", O_RDWR); 225 if (vhostfd < 0) { 226 error_setg(errp, "vhost-scsi: open vhost char device failed: %s", 227 strerror(errno)); 228 return; 229 } 230 } 231 232 virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output, 233 vhost_dummy_handle_output, 234 vhost_dummy_handle_output); 235 if (err != NULL) { 236 error_propagate(errp, err); 237 close(vhostfd); 238 return; 239 } 240 241 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; 242 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs); 243 s->dev.vq_index = 0; 244 s->dev.backend_features = 0; 245 246 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd, 247 VHOST_BACKEND_TYPE_KERNEL, true); 248 if (ret < 0) { 249 error_setg(errp, "vhost-scsi: vhost initialization failed: %s", 250 strerror(-ret)); 251 return; 252 } 253 254 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */ 255 s->channel = 0; 256 s->lun = 0; 257 /* Note: we can also get the minimum tpgt from kernel */ 258 s->target = vs->conf.boot_tpgt; 259 260 error_setg(&s->migration_blocker, 261 "vhost-scsi does not support migration"); 262 migrate_add_blocker(s->migration_blocker); 263 } 264 265 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp) 266 { 267 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 268 VHostSCSI *s = VHOST_SCSI(dev); 269 270 migrate_del_blocker(s->migration_blocker); 271 error_free(s->migration_blocker); 272 273 /* This will stop vhost backend. */ 274 vhost_scsi_set_status(vdev, 0); 275 276 g_free(s->dev.vqs); 277 278 virtio_scsi_common_unrealize(dev, errp); 279 } 280 281 /* 282 * Implementation of an interface to adjust firmware path 283 * for the bootindex property handling. 284 */ 285 static char *vhost_scsi_get_fw_dev_path(FWPathProvider *p, BusState *bus, 286 DeviceState *dev) 287 { 288 VHostSCSI *s = VHOST_SCSI(dev); 289 /* format: channel@channel/vhost-scsi@target,lun */ 290 return g_strdup_printf("channel@%x/%s@%x,%x", s->channel, 291 qdev_fw_name(dev), s->target, s->lun); 292 } 293 294 static Property vhost_scsi_properties[] = { 295 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSI, parent_obj.conf), 296 DEFINE_PROP_END_OF_LIST(), 297 }; 298 299 static void vhost_scsi_class_init(ObjectClass *klass, void *data) 300 { 301 DeviceClass *dc = DEVICE_CLASS(klass); 302 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 303 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass); 304 305 dc->props = vhost_scsi_properties; 306 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 307 vdc->realize = vhost_scsi_realize; 308 vdc->unrealize = vhost_scsi_unrealize; 309 vdc->get_features = vhost_scsi_get_features; 310 vdc->set_config = vhost_scsi_set_config; 311 vdc->set_status = vhost_scsi_set_status; 312 fwc->get_dev_path = vhost_scsi_get_fw_dev_path; 313 } 314 315 static void vhost_scsi_instance_init(Object *obj) 316 { 317 VHostSCSI *dev = VHOST_SCSI(obj); 318 319 device_add_bootindex_property(obj, &dev->bootindex, "bootindex", NULL, 320 DEVICE(dev), NULL); 321 } 322 323 static const TypeInfo vhost_scsi_info = { 324 .name = TYPE_VHOST_SCSI, 325 .parent = TYPE_VIRTIO_SCSI_COMMON, 326 .instance_size = sizeof(VHostSCSI), 327 .class_init = vhost_scsi_class_init, 328 .instance_init = vhost_scsi_instance_init, 329 .interfaces = (InterfaceInfo[]) { 330 { TYPE_FW_PATH_PROVIDER }, 331 { } 332 }, 333 }; 334 335 static void virtio_register_types(void) 336 { 337 type_register_static(&vhost_scsi_info); 338 } 339 340 type_init(virtio_register_types) 341