PageRenderTime 37ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/hw/scsi/vhost-scsi-common.c

https://gitlab.com/paelzer/qemu
C | 171 lines | 117 code | 28 blank | 26 comment | 14 complexity | 598daf3c65f8800cce2a0465759af47d MD5 | raw file
  1. /*
  2. * vhost-scsi-common
  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 "qemu/error-report.h"
  19. #include "qemu/module.h"
  20. #include "hw/virtio/vhost.h"
  21. #include "hw/virtio/vhost-scsi-common.h"
  22. #include "hw/virtio/virtio-scsi.h"
  23. #include "hw/virtio/virtio-bus.h"
  24. #include "hw/virtio/virtio-access.h"
  25. #include "hw/fw-path-provider.h"
  26. int vhost_scsi_common_start(VHostSCSICommon *vsc)
  27. {
  28. int ret, i;
  29. VirtIODevice *vdev = VIRTIO_DEVICE(vsc);
  30. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  31. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  32. VirtIOSCSICommon *vs = (VirtIOSCSICommon *)vsc;
  33. if (!k->set_guest_notifiers) {
  34. error_report("binding does not support guest notifiers");
  35. return -ENOSYS;
  36. }
  37. ret = vhost_dev_enable_notifiers(&vsc->dev, vdev);
  38. if (ret < 0) {
  39. return ret;
  40. }
  41. ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, true);
  42. if (ret < 0) {
  43. error_report("Error binding guest notifier");
  44. goto err_host_notifiers;
  45. }
  46. vsc->dev.acked_features = vdev->guest_features;
  47. assert(vsc->inflight == NULL);
  48. vsc->inflight = g_new0(struct vhost_inflight, 1);
  49. ret = vhost_dev_get_inflight(&vsc->dev,
  50. vs->conf.virtqueue_size,
  51. vsc->inflight);
  52. if (ret < 0) {
  53. error_report("Error get inflight: %d", -ret);
  54. goto err_guest_notifiers;
  55. }
  56. ret = vhost_dev_set_inflight(&vsc->dev, vsc->inflight);
  57. if (ret < 0) {
  58. error_report("Error set inflight: %d", -ret);
  59. goto err_guest_notifiers;
  60. }
  61. ret = vhost_dev_start(&vsc->dev, vdev);
  62. if (ret < 0) {
  63. error_report("Error start vhost dev");
  64. goto err_guest_notifiers;
  65. }
  66. /* guest_notifier_mask/pending not used yet, so just unmask
  67. * everything here. virtio-pci will do the right thing by
  68. * enabling/disabling irqfd.
  69. */
  70. for (i = 0; i < vsc->dev.nvqs; i++) {
  71. vhost_virtqueue_mask(&vsc->dev, vdev, vsc->dev.vq_index + i, false);
  72. }
  73. return ret;
  74. err_guest_notifiers:
  75. g_free(vsc->inflight);
  76. vsc->inflight = NULL;
  77. k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
  78. err_host_notifiers:
  79. vhost_dev_disable_notifiers(&vsc->dev, vdev);
  80. return ret;
  81. }
  82. void vhost_scsi_common_stop(VHostSCSICommon *vsc)
  83. {
  84. VirtIODevice *vdev = VIRTIO_DEVICE(vsc);
  85. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  86. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  87. int ret = 0;
  88. vhost_dev_stop(&vsc->dev, vdev);
  89. if (k->set_guest_notifiers) {
  90. ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
  91. if (ret < 0) {
  92. error_report("vhost guest notifier cleanup failed: %d", ret);
  93. }
  94. }
  95. assert(ret >= 0);
  96. if (vsc->inflight) {
  97. vhost_dev_free_inflight(vsc->inflight);
  98. vsc->inflight = NULL;
  99. }
  100. vhost_dev_disable_notifiers(&vsc->dev, vdev);
  101. }
  102. uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t features,
  103. Error **errp)
  104. {
  105. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
  106. /* Turn on predefined features supported by this device */
  107. features |= vsc->host_features;
  108. return vhost_get_features(&vsc->dev, vsc->feature_bits, features);
  109. }
  110. void vhost_scsi_common_set_config(VirtIODevice *vdev, const uint8_t *config)
  111. {
  112. VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
  113. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
  114. if ((uint32_t)virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size ||
  115. (uint32_t)virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) {
  116. error_report("vhost-scsi does not support changing the sense data and "
  117. "CDB sizes");
  118. exit(1);
  119. }
  120. }
  121. /*
  122. * Implementation of an interface to adjust firmware path
  123. * for the bootindex property handling.
  124. */
  125. char *vhost_scsi_common_get_fw_dev_path(FWPathProvider *p, BusState *bus,
  126. DeviceState *dev)
  127. {
  128. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
  129. /* format: /channel@channel/vhost-scsi@target,lun */
  130. return g_strdup_printf("/channel@%x/%s@%x,%x", vsc->channel,
  131. qdev_fw_name(dev), vsc->target, vsc->lun);
  132. }
  133. static const TypeInfo vhost_scsi_common_info = {
  134. .name = TYPE_VHOST_SCSI_COMMON,
  135. .parent = TYPE_VIRTIO_SCSI_COMMON,
  136. .instance_size = sizeof(VHostSCSICommon),
  137. .abstract = true,
  138. };
  139. static void virtio_register_types(void)
  140. {
  141. type_register_static(&vhost_scsi_common_info);
  142. }
  143. type_init(virtio_register_types)