1 /* 2 * vhost-user-scsi host device 3 * 4 * Copyright (c) 2016 Nutanix Inc. All rights reserved. 5 * 6 * Author: 7 * Felipe Franciosi <felipe@nutanix.com> 8 * 9 * This work is largely based on the "vhost-scsi" implementation by: 10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 11 * Nicholas Bellinger <nab@risingtidesystems.com> 12 * 13 * This work is licensed under the terms of the GNU LGPL, version 2 or later. 14 * See the COPYING.LIB file in the top-level directory. 15 * 16 */ 17 18 #include "qemu/osdep.h" 19 #include "qapi/error.h" 20 #include "qemu/error-report.h" 21 #include "hw/fw-path-provider.h" 22 #include "hw/qdev-core.h" 23 #include "hw/qdev-properties.h" 24 #include "hw/qdev-properties-system.h" 25 #include "hw/virtio/vhost.h" 26 #include "hw/virtio/vhost-backend.h" 27 #include "hw/virtio/vhost-user-scsi.h" 28 #include "hw/virtio/virtio.h" 29 #include "chardev/char-fe.h" 30 #include "system/system.h" 31 32 /* Features supported by the host application */ 33 static const int user_feature_bits[] = { 34 VIRTIO_F_NOTIFY_ON_EMPTY, 35 VIRTIO_RING_F_INDIRECT_DESC, 36 VIRTIO_RING_F_EVENT_IDX, 37 VIRTIO_SCSI_F_HOTPLUG, 38 VIRTIO_F_RING_RESET, 39 VIRTIO_F_IN_ORDER, 40 VIRTIO_F_NOTIFICATION_DATA, 41 VHOST_INVALID_FEATURE_BIT 42 }; 43 44 static int vhost_user_scsi_start(VHostUserSCSI *s, Error **errp) 45 { 46 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 47 int ret; 48 49 ret = vhost_scsi_common_start(vsc, errp); 50 s->started_vu = !(ret < 0); 51 52 return ret; 53 } 54 55 static int vhost_user_scsi_stop(VHostUserSCSI *s) 56 { 57 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 58 59 if (!s->started_vu) { 60 return 0; 61 } 62 s->started_vu = false; 63 64 return vhost_scsi_common_stop(vsc); 65 } 66 67 static int vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status) 68 { 69 VHostUserSCSI *s = (VHostUserSCSI *)vdev; 70 DeviceState *dev = DEVICE(vdev); 71 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 72 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 73 bool should_start = virtio_device_should_start(vdev, status); 74 Error *local_err = NULL; 75 int ret; 76 77 if (!s->connected) { 78 return -1; 79 } 80 81 if (vhost_dev_is_started(&vsc->dev) == should_start) { 82 return 0; 83 } 84 85 if (should_start) { 86 ret = vhost_user_scsi_start(s, &local_err); 87 if (ret < 0) { 88 error_reportf_err(local_err, 89 "unable to start vhost-user-scsi: %s: ", 90 strerror(-ret)); 91 qemu_chr_fe_disconnect(&vs->conf.chardev); 92 } 93 } else { 94 ret = vhost_user_scsi_stop(s); 95 if (ret) { 96 return ret; 97 } 98 } 99 return 0; 100 } 101 102 static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *vq) 103 { 104 VHostUserSCSI *s = (VHostUserSCSI *)vdev; 105 DeviceState *dev = DEVICE(vdev); 106 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 107 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 108 109 Error *local_err = NULL; 110 int i, ret; 111 112 if (!vdev->start_on_kick) { 113 return; 114 } 115 116 if (!s->connected) { 117 return; 118 } 119 120 if (vhost_dev_is_started(&vsc->dev)) { 121 return; 122 } 123 124 /* 125 * Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start 126 * vhost here instead of waiting for .set_status(). 127 */ 128 ret = vhost_user_scsi_start(s, &local_err); 129 if (ret < 0) { 130 error_reportf_err(local_err, "vhost-user-scsi: vhost start failed: "); 131 qemu_chr_fe_disconnect(&vs->conf.chardev); 132 return; 133 } 134 135 /* Kick right away to begin processing requests already in vring */ 136 for (i = 0; i < vsc->dev.nvqs; i++) { 137 VirtQueue *kick_vq = virtio_get_queue(vdev, i); 138 139 if (!virtio_queue_get_desc_addr(vdev, i)) { 140 continue; 141 } 142 event_notifier_set(virtio_queue_get_host_notifier(kick_vq)); 143 } 144 } 145 146 static int vhost_user_scsi_connect(DeviceState *dev, Error **errp) 147 { 148 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 149 VHostUserSCSI *s = VHOST_USER_SCSI(vdev); 150 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 151 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 152 int ret = 0; 153 154 if (s->connected) { 155 return 0; 156 } 157 158 vsc->dev.num_queues = vs->conf.num_queues; 159 vsc->dev.nvqs = VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; 160 vsc->dev.vqs = s->vhost_vqs; 161 vsc->dev.vq_index = 0; 162 vsc->dev.backend_features = 0; 163 164 ret = vhost_dev_init(&vsc->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0, 165 errp); 166 if (ret < 0) { 167 return ret; 168 } 169 170 s->connected = true; 171 172 /* restore vhost state */ 173 if (virtio_device_started(vdev, vdev->status)) { 174 ret = vhost_user_scsi_start(s, errp); 175 } 176 177 return ret; 178 } 179 180 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event); 181 182 static void vhost_user_scsi_disconnect(DeviceState *dev) 183 { 184 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 185 VHostUserSCSI *s = VHOST_USER_SCSI(vdev); 186 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 187 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 188 189 if (!s->connected) { 190 goto done; 191 } 192 s->connected = false; 193 194 vhost_user_scsi_stop(s); 195 196 vhost_dev_cleanup(&vsc->dev); 197 198 done: 199 /* Re-instate the event handler for new connections */ 200 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, 201 vhost_user_scsi_event, NULL, dev, NULL, true); 202 } 203 204 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event) 205 { 206 DeviceState *dev = opaque; 207 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 208 VHostUserSCSI *s = VHOST_USER_SCSI(vdev); 209 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 210 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 211 Error *local_err = NULL; 212 213 switch (event) { 214 case CHR_EVENT_OPENED: 215 if (vhost_user_scsi_connect(dev, &local_err) < 0) { 216 error_report_err(local_err); 217 qemu_chr_fe_disconnect(&vs->conf.chardev); 218 return; 219 } 220 break; 221 case CHR_EVENT_CLOSED: 222 /* defer close until later to avoid circular close */ 223 vhost_user_async_close(dev, &vs->conf.chardev, &vsc->dev, 224 vhost_user_scsi_disconnect); 225 break; 226 case CHR_EVENT_BREAK: 227 case CHR_EVENT_MUX_IN: 228 case CHR_EVENT_MUX_OUT: 229 /* Ignore */ 230 break; 231 } 232 } 233 234 static int vhost_user_scsi_realize_connect(VHostUserSCSI *s, Error **errp) 235 { 236 DeviceState *dev = DEVICE(s); 237 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 238 int ret; 239 240 s->connected = false; 241 242 ret = qemu_chr_fe_wait_connected(&vs->conf.chardev, errp); 243 if (ret < 0) { 244 return ret; 245 } 246 247 ret = vhost_user_scsi_connect(dev, errp); 248 if (ret < 0) { 249 qemu_chr_fe_disconnect(&vs->conf.chardev); 250 return ret; 251 } 252 assert(s->connected); 253 254 return 0; 255 } 256 257 static void vhost_user_scsi_realize(DeviceState *dev, Error **errp) 258 { 259 ERRP_GUARD(); 260 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 261 VHostUserSCSI *s = VHOST_USER_SCSI(dev); 262 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 263 Error *err = NULL; 264 int ret; 265 int retries = VU_REALIZE_CONN_RETRIES; 266 267 if (!vs->conf.chardev.chr) { 268 error_setg(errp, "vhost-user-scsi: missing chardev"); 269 return; 270 } 271 272 virtio_scsi_common_realize(dev, vhost_user_scsi_handle_output, 273 vhost_user_scsi_handle_output, 274 vhost_user_scsi_handle_output, &err); 275 if (err != NULL) { 276 error_propagate(errp, err); 277 return; 278 } 279 280 if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) { 281 goto free_virtio; 282 } 283 284 vsc->inflight = g_new0(struct vhost_inflight, 1); 285 s->vhost_vqs = g_new0(struct vhost_virtqueue, 286 VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues); 287 288 assert(!*errp); 289 do { 290 if (*errp) { 291 error_prepend(errp, "Reconnecting after error: "); 292 error_report_err(*errp); 293 *errp = NULL; 294 } 295 ret = vhost_user_scsi_realize_connect(s, errp); 296 } while (ret < 0 && retries--); 297 298 if (ret < 0) { 299 goto free_vhost; 300 } 301 302 /* we're fully initialized, now we can operate, so add the handler */ 303 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, 304 vhost_user_scsi_event, NULL, (void *)dev, 305 NULL, true); 306 /* Channel and lun both are 0 for bootable vhost-user-scsi disk */ 307 vsc->channel = 0; 308 vsc->lun = 0; 309 vsc->target = vs->conf.boot_tpgt; 310 311 return; 312 313 free_vhost: 314 g_free(s->vhost_vqs); 315 s->vhost_vqs = NULL; 316 g_free(vsc->inflight); 317 vsc->inflight = NULL; 318 vhost_user_cleanup(&s->vhost_user); 319 320 free_virtio: 321 virtio_scsi_common_unrealize(dev); 322 } 323 324 static void vhost_user_scsi_unrealize(DeviceState *dev) 325 { 326 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 327 VHostUserSCSI *s = VHOST_USER_SCSI(dev); 328 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 329 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 330 331 /* This will stop the vhost backend. */ 332 vhost_user_scsi_set_status(vdev, 0); 333 qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, NULL, NULL, NULL, 334 NULL, false); 335 336 vhost_dev_cleanup(&vsc->dev); 337 g_free(s->vhost_vqs); 338 s->vhost_vqs = NULL; 339 340 vhost_dev_free_inflight(vsc->inflight); 341 g_free(vsc->inflight); 342 vsc->inflight = NULL; 343 344 vhost_user_cleanup(&s->vhost_user); 345 virtio_scsi_common_unrealize(dev); 346 } 347 348 static const Property vhost_user_scsi_properties[] = { 349 DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev), 350 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0), 351 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues, 352 VIRTIO_SCSI_AUTO_NUM_QUEUES), 353 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size, 354 128), 355 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors, 356 0xFFFF), 357 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128), 358 DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features, 359 VIRTIO_SCSI_F_HOTPLUG, 360 true), 361 DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features, 362 VIRTIO_SCSI_F_CHANGE, 363 true), 364 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features, 365 VIRTIO_SCSI_F_T10_PI, 366 false), 367 }; 368 369 static void vhost_user_scsi_reset(VirtIODevice *vdev) 370 { 371 VHostUserSCSI *s = VHOST_USER_SCSI(vdev); 372 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); 373 374 vhost_dev_free_inflight(vsc->inflight); 375 } 376 377 static struct vhost_dev *vhost_user_scsi_get_vhost(VirtIODevice *vdev) 378 { 379 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev); 380 return &vsc->dev; 381 } 382 383 static const VMStateDescription vmstate_vhost_scsi = { 384 .name = "virtio-scsi", 385 .minimum_version_id = 1, 386 .version_id = 1, 387 .fields = (const VMStateField[]) { 388 VMSTATE_VIRTIO_DEVICE, 389 VMSTATE_END_OF_LIST() 390 }, 391 }; 392 393 static void vhost_user_scsi_class_init(ObjectClass *klass, const void *data) 394 { 395 DeviceClass *dc = DEVICE_CLASS(klass); 396 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 397 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass); 398 399 device_class_set_props(dc, vhost_user_scsi_properties); 400 dc->vmsd = &vmstate_vhost_scsi; 401 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 402 vdc->realize = vhost_user_scsi_realize; 403 vdc->unrealize = vhost_user_scsi_unrealize; 404 vdc->get_features = vhost_scsi_common_get_features; 405 vdc->set_config = vhost_scsi_common_set_config; 406 vdc->set_status = vhost_user_scsi_set_status; 407 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path; 408 vdc->reset = vhost_user_scsi_reset; 409 vdc->get_vhost = vhost_user_scsi_get_vhost; 410 } 411 412 static void vhost_user_scsi_instance_init(Object *obj) 413 { 414 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj); 415 416 vsc->feature_bits = user_feature_bits; 417 418 /* Add the bootindex property for this object */ 419 device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL, 420 DEVICE(vsc)); 421 } 422 423 static const TypeInfo vhost_user_scsi_info = { 424 .name = TYPE_VHOST_USER_SCSI, 425 .parent = TYPE_VHOST_SCSI_COMMON, 426 .instance_size = sizeof(VHostUserSCSI), 427 .class_init = vhost_user_scsi_class_init, 428 .instance_init = vhost_user_scsi_instance_init, 429 .interfaces = (const InterfaceInfo[]) { 430 { TYPE_FW_PATH_PROVIDER }, 431 { } 432 }, 433 }; 434 435 static void virtio_register_types(void) 436 { 437 type_register_static(&vhost_user_scsi_info); 438 } 439 440 type_init(virtio_register_types) 441