2
0

vhost-user-scsi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "qemu/osdep.h"
  18. #include "qapi/error.h"
  19. #include "qemu/error-report.h"
  20. #include "qom/object.h"
  21. #include "hw/fw-path-provider.h"
  22. #include "hw/qdev-core.h"
  23. #include "hw/qdev-properties.h"
  24. #include "hw/virtio/vhost.h"
  25. #include "hw/virtio/vhost-backend.h"
  26. #include "hw/virtio/vhost-user-scsi.h"
  27. #include "hw/virtio/virtio.h"
  28. #include "hw/virtio/virtio-access.h"
  29. #include "chardev/char-fe.h"
  30. #include "sysemu/sysemu.h"
  31. /* Features supported by the host application */
  32. static const int user_feature_bits[] = {
  33. VIRTIO_F_NOTIFY_ON_EMPTY,
  34. VIRTIO_RING_F_INDIRECT_DESC,
  35. VIRTIO_RING_F_EVENT_IDX,
  36. VIRTIO_SCSI_F_HOTPLUG,
  37. VHOST_INVALID_FEATURE_BIT
  38. };
  39. static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status)
  40. {
  41. VHostUserSCSI *s = (VHostUserSCSI *)vdev;
  42. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  43. bool start = (status & VIRTIO_CONFIG_S_DRIVER_OK) && vdev->vm_running;
  44. if (vsc->dev.started == start) {
  45. return;
  46. }
  47. if (start) {
  48. int ret;
  49. ret = vhost_scsi_common_start(vsc);
  50. if (ret < 0) {
  51. error_report("unable to start vhost-user-scsi: %s", strerror(-ret));
  52. exit(1);
  53. }
  54. } else {
  55. vhost_scsi_common_stop(vsc);
  56. }
  57. }
  58. static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
  59. {
  60. }
  61. static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
  62. {
  63. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  64. VHostUserSCSI *s = VHOST_USER_SCSI(dev);
  65. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  66. struct vhost_virtqueue *vqs = NULL;
  67. Error *err = NULL;
  68. int ret;
  69. if (!vs->conf.chardev.chr) {
  70. error_setg(errp, "vhost-user-scsi: missing chardev");
  71. return;
  72. }
  73. virtio_scsi_common_realize(dev, vhost_dummy_handle_output,
  74. vhost_dummy_handle_output,
  75. vhost_dummy_handle_output, &err);
  76. if (err != NULL) {
  77. error_propagate(errp, err);
  78. return;
  79. }
  80. if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
  81. goto free_virtio;
  82. }
  83. vsc->dev.nvqs = 2 + vs->conf.num_queues;
  84. vsc->dev.vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs);
  85. vsc->dev.vq_index = 0;
  86. vsc->dev.backend_features = 0;
  87. vqs = vsc->dev.vqs;
  88. ret = vhost_dev_init(&vsc->dev, &s->vhost_user,
  89. VHOST_BACKEND_TYPE_USER, 0);
  90. if (ret < 0) {
  91. error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s",
  92. strerror(-ret));
  93. goto free_vhost;
  94. }
  95. /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
  96. vsc->channel = 0;
  97. vsc->lun = 0;
  98. vsc->target = vs->conf.boot_tpgt;
  99. return;
  100. free_vhost:
  101. vhost_user_cleanup(&s->vhost_user);
  102. g_free(vqs);
  103. free_virtio:
  104. virtio_scsi_common_unrealize(dev);
  105. }
  106. static void vhost_user_scsi_unrealize(DeviceState *dev, Error **errp)
  107. {
  108. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  109. VHostUserSCSI *s = VHOST_USER_SCSI(dev);
  110. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  111. struct vhost_virtqueue *vqs = vsc->dev.vqs;
  112. /* This will stop the vhost backend. */
  113. vhost_user_scsi_set_status(vdev, 0);
  114. vhost_dev_cleanup(&vsc->dev);
  115. g_free(vqs);
  116. virtio_scsi_common_unrealize(dev);
  117. vhost_user_cleanup(&s->vhost_user);
  118. }
  119. static Property vhost_user_scsi_properties[] = {
  120. DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev),
  121. DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
  122. DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues, 1),
  123. DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
  124. 128),
  125. DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
  126. 0xFFFF),
  127. DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
  128. DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features,
  129. VIRTIO_SCSI_F_HOTPLUG,
  130. true),
  131. DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features,
  132. VIRTIO_SCSI_F_CHANGE,
  133. true),
  134. DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
  135. VIRTIO_SCSI_F_T10_PI,
  136. false),
  137. DEFINE_PROP_END_OF_LIST(),
  138. };
  139. static const VMStateDescription vmstate_vhost_scsi = {
  140. .name = "virtio-scsi",
  141. .minimum_version_id = 1,
  142. .version_id = 1,
  143. .fields = (VMStateField[]) {
  144. VMSTATE_VIRTIO_DEVICE,
  145. VMSTATE_END_OF_LIST()
  146. },
  147. };
  148. static void vhost_user_scsi_class_init(ObjectClass *klass, void *data)
  149. {
  150. DeviceClass *dc = DEVICE_CLASS(klass);
  151. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  152. FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
  153. dc->props = vhost_user_scsi_properties;
  154. dc->vmsd = &vmstate_vhost_scsi;
  155. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  156. vdc->realize = vhost_user_scsi_realize;
  157. vdc->unrealize = vhost_user_scsi_unrealize;
  158. vdc->get_features = vhost_scsi_common_get_features;
  159. vdc->set_config = vhost_scsi_common_set_config;
  160. vdc->set_status = vhost_user_scsi_set_status;
  161. fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
  162. }
  163. static void vhost_user_scsi_instance_init(Object *obj)
  164. {
  165. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
  166. vsc->feature_bits = user_feature_bits;
  167. /* Add the bootindex property for this object */
  168. device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
  169. DEVICE(vsc), NULL);
  170. }
  171. static const TypeInfo vhost_user_scsi_info = {
  172. .name = TYPE_VHOST_USER_SCSI,
  173. .parent = TYPE_VHOST_SCSI_COMMON,
  174. .instance_size = sizeof(VHostUserSCSI),
  175. .class_init = vhost_user_scsi_class_init,
  176. .instance_init = vhost_user_scsi_instance_init,
  177. .interfaces = (InterfaceInfo[]) {
  178. { TYPE_FW_PATH_PROVIDER },
  179. { }
  180. },
  181. };
  182. static void virtio_register_types(void)
  183. {
  184. type_register_static(&vhost_user_scsi_info);
  185. }
  186. type_init(virtio_register_types)