vhost-user-scsi.c 6.7 KB

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