vhost-scsi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #include "qemu/osdep.h"
  17. #include <linux/vhost.h>
  18. #include <sys/ioctl.h>
  19. #include "qapi/error.h"
  20. #include "qemu/error-report.h"
  21. #include "qemu/module.h"
  22. #include "monitor/monitor.h"
  23. #include "migration/blocker.h"
  24. #include "hw/virtio/vhost-scsi.h"
  25. #include "hw/virtio/vhost.h"
  26. #include "hw/virtio/virtio-scsi.h"
  27. #include "hw/virtio/virtio-bus.h"
  28. #include "hw/virtio/virtio-access.h"
  29. #include "hw/fw-path-provider.h"
  30. #include "hw/qdev-properties.h"
  31. #include "qemu/cutils.h"
  32. #include "sysemu/sysemu.h"
  33. /* Features supported by host kernel. */
  34. static const int kernel_feature_bits[] = {
  35. VIRTIO_F_NOTIFY_ON_EMPTY,
  36. VIRTIO_RING_F_INDIRECT_DESC,
  37. VIRTIO_RING_F_EVENT_IDX,
  38. VIRTIO_SCSI_F_HOTPLUG,
  39. VHOST_INVALID_FEATURE_BIT
  40. };
  41. static int vhost_scsi_set_endpoint(VHostSCSI *s)
  42. {
  43. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
  44. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  45. const VhostOps *vhost_ops = vsc->dev.vhost_ops;
  46. struct vhost_scsi_target backend;
  47. int ret;
  48. memset(&backend, 0, sizeof(backend));
  49. pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
  50. ret = vhost_ops->vhost_scsi_set_endpoint(&vsc->dev, &backend);
  51. if (ret < 0) {
  52. return -errno;
  53. }
  54. return 0;
  55. }
  56. static void vhost_scsi_clear_endpoint(VHostSCSI *s)
  57. {
  58. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
  59. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  60. struct vhost_scsi_target backend;
  61. const VhostOps *vhost_ops = vsc->dev.vhost_ops;
  62. memset(&backend, 0, sizeof(backend));
  63. pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
  64. vhost_ops->vhost_scsi_clear_endpoint(&vsc->dev, &backend);
  65. }
  66. static int vhost_scsi_start(VHostSCSI *s)
  67. {
  68. int ret, abi_version;
  69. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  70. const VhostOps *vhost_ops = vsc->dev.vhost_ops;
  71. ret = vhost_ops->vhost_scsi_get_abi_version(&vsc->dev, &abi_version);
  72. if (ret < 0) {
  73. return -errno;
  74. }
  75. if (abi_version > VHOST_SCSI_ABI_VERSION) {
  76. error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
  77. " %d is greater than vhost_scsi userspace supports: %d,"
  78. " please upgrade your version of QEMU", abi_version,
  79. VHOST_SCSI_ABI_VERSION);
  80. return -ENOSYS;
  81. }
  82. ret = vhost_scsi_common_start(vsc);
  83. if (ret < 0) {
  84. return ret;
  85. }
  86. ret = vhost_scsi_set_endpoint(s);
  87. if (ret < 0) {
  88. error_report("Error setting vhost-scsi endpoint");
  89. vhost_scsi_common_stop(vsc);
  90. }
  91. return ret;
  92. }
  93. static void vhost_scsi_stop(VHostSCSI *s)
  94. {
  95. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  96. vhost_scsi_clear_endpoint(s);
  97. vhost_scsi_common_stop(vsc);
  98. }
  99. static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
  100. {
  101. VHostSCSI *s = VHOST_SCSI(vdev);
  102. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  103. bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
  104. if (!vdev->vm_running) {
  105. start = false;
  106. }
  107. if (vsc->dev.started == start) {
  108. return;
  109. }
  110. if (start) {
  111. int ret;
  112. ret = vhost_scsi_start(s);
  113. if (ret < 0) {
  114. error_report("unable to start vhost-scsi: %s", strerror(-ret));
  115. exit(1);
  116. }
  117. } else {
  118. vhost_scsi_stop(s);
  119. }
  120. }
  121. static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
  122. {
  123. }
  124. static int vhost_scsi_pre_save(void *opaque)
  125. {
  126. VHostSCSICommon *vsc = opaque;
  127. /* At this point, backend must be stopped, otherwise
  128. * it might keep writing to memory. */
  129. assert(!vsc->dev.started);
  130. return 0;
  131. }
  132. static const VMStateDescription vmstate_virtio_vhost_scsi = {
  133. .name = "virtio-vhost_scsi",
  134. .minimum_version_id = 1,
  135. .version_id = 1,
  136. .fields = (VMStateField[]) {
  137. VMSTATE_VIRTIO_DEVICE,
  138. VMSTATE_END_OF_LIST()
  139. },
  140. .pre_save = vhost_scsi_pre_save,
  141. };
  142. static void vhost_scsi_realize(DeviceState *dev, Error **errp)
  143. {
  144. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  145. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
  146. Error *err = NULL;
  147. int vhostfd = -1;
  148. int ret;
  149. if (!vs->conf.wwpn) {
  150. error_setg(errp, "vhost-scsi: missing wwpn");
  151. return;
  152. }
  153. if (vs->conf.vhostfd) {
  154. vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, errp);
  155. if (vhostfd == -1) {
  156. error_prepend(errp, "vhost-scsi: unable to parse vhostfd: ");
  157. return;
  158. }
  159. } else {
  160. vhostfd = open("/dev/vhost-scsi", O_RDWR);
  161. if (vhostfd < 0) {
  162. error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
  163. strerror(errno));
  164. return;
  165. }
  166. }
  167. virtio_scsi_common_realize(dev,
  168. vhost_dummy_handle_output,
  169. vhost_dummy_handle_output,
  170. vhost_dummy_handle_output,
  171. &err);
  172. if (err != NULL) {
  173. error_propagate(errp, err);
  174. goto close_fd;
  175. }
  176. if (!vsc->migratable) {
  177. error_setg(&vsc->migration_blocker,
  178. "vhost-scsi does not support migration in all cases. "
  179. "When external environment supports it (Orchestrator migrates "
  180. "target SCSI device state or use shared storage over network), "
  181. "set 'migratable' property to true to enable migration.");
  182. migrate_add_blocker(vsc->migration_blocker, &err);
  183. if (err) {
  184. error_propagate(errp, err);
  185. error_free(vsc->migration_blocker);
  186. goto free_virtio;
  187. }
  188. }
  189. vsc->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
  190. vsc->dev.vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs);
  191. vsc->dev.vq_index = 0;
  192. vsc->dev.backend_features = 0;
  193. ret = vhost_dev_init(&vsc->dev, (void *)(uintptr_t)vhostfd,
  194. VHOST_BACKEND_TYPE_KERNEL, 0);
  195. if (ret < 0) {
  196. error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
  197. strerror(-ret));
  198. goto free_vqs;
  199. }
  200. /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
  201. vsc->channel = 0;
  202. vsc->lun = 0;
  203. /* Note: we can also get the minimum tpgt from kernel */
  204. vsc->target = vs->conf.boot_tpgt;
  205. return;
  206. free_vqs:
  207. if (!vsc->migratable) {
  208. migrate_del_blocker(vsc->migration_blocker);
  209. }
  210. g_free(vsc->dev.vqs);
  211. free_virtio:
  212. virtio_scsi_common_unrealize(dev);
  213. close_fd:
  214. close(vhostfd);
  215. return;
  216. }
  217. static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
  218. {
  219. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  220. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
  221. struct vhost_virtqueue *vqs = vsc->dev.vqs;
  222. if (!vsc->migratable) {
  223. migrate_del_blocker(vsc->migration_blocker);
  224. error_free(vsc->migration_blocker);
  225. }
  226. /* This will stop vhost backend. */
  227. vhost_scsi_set_status(vdev, 0);
  228. vhost_dev_cleanup(&vsc->dev);
  229. g_free(vqs);
  230. virtio_scsi_common_unrealize(dev);
  231. }
  232. static Property vhost_scsi_properties[] = {
  233. DEFINE_PROP_STRING("vhostfd", VirtIOSCSICommon, conf.vhostfd),
  234. DEFINE_PROP_STRING("wwpn", VirtIOSCSICommon, conf.wwpn),
  235. DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
  236. DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues, 1),
  237. DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
  238. 128),
  239. DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
  240. 0xFFFF),
  241. DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
  242. DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
  243. VIRTIO_SCSI_F_T10_PI,
  244. false),
  245. DEFINE_PROP_BOOL("migratable", VHostSCSICommon, migratable, false),
  246. DEFINE_PROP_END_OF_LIST(),
  247. };
  248. static void vhost_scsi_class_init(ObjectClass *klass, void *data)
  249. {
  250. DeviceClass *dc = DEVICE_CLASS(klass);
  251. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  252. FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
  253. dc->props = vhost_scsi_properties;
  254. dc->vmsd = &vmstate_virtio_vhost_scsi;
  255. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  256. vdc->realize = vhost_scsi_realize;
  257. vdc->unrealize = vhost_scsi_unrealize;
  258. vdc->get_features = vhost_scsi_common_get_features;
  259. vdc->set_config = vhost_scsi_common_set_config;
  260. vdc->set_status = vhost_scsi_set_status;
  261. fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
  262. }
  263. static void vhost_scsi_instance_init(Object *obj)
  264. {
  265. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
  266. vsc->feature_bits = kernel_feature_bits;
  267. device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
  268. DEVICE(vsc), NULL);
  269. }
  270. static const TypeInfo vhost_scsi_info = {
  271. .name = TYPE_VHOST_SCSI,
  272. .parent = TYPE_VHOST_SCSI_COMMON,
  273. .instance_size = sizeof(VHostSCSI),
  274. .class_init = vhost_scsi_class_init,
  275. .instance_init = vhost_scsi_instance_init,
  276. .interfaces = (InterfaceInfo[]) {
  277. { TYPE_FW_PATH_PROVIDER },
  278. { }
  279. },
  280. };
  281. static void virtio_register_types(void)
  282. {
  283. type_register_static(&vhost_scsi_info);
  284. }
  285. type_init(virtio_register_types)