vhost-user-scsi.c 7.2 KB

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