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