2
0

vhost-scsi-common.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if (!k->set_guest_notifiers) {
  33. error_report("binding does not support guest notifiers");
  34. return -ENOSYS;
  35. }
  36. ret = vhost_dev_enable_notifiers(&vsc->dev, vdev);
  37. if (ret < 0) {
  38. return ret;
  39. }
  40. ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, true);
  41. if (ret < 0) {
  42. error_report("Error binding guest notifier");
  43. goto err_host_notifiers;
  44. }
  45. vsc->dev.acked_features = vdev->guest_features;
  46. ret = vhost_dev_start(&vsc->dev, vdev);
  47. if (ret < 0) {
  48. error_report("Error start vhost dev");
  49. goto err_guest_notifiers;
  50. }
  51. /* guest_notifier_mask/pending not used yet, so just unmask
  52. * everything here. virtio-pci will do the right thing by
  53. * enabling/disabling irqfd.
  54. */
  55. for (i = 0; i < vsc->dev.nvqs; i++) {
  56. vhost_virtqueue_mask(&vsc->dev, vdev, vsc->dev.vq_index + i, false);
  57. }
  58. return ret;
  59. err_guest_notifiers:
  60. k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
  61. err_host_notifiers:
  62. vhost_dev_disable_notifiers(&vsc->dev, vdev);
  63. return ret;
  64. }
  65. void vhost_scsi_common_stop(VHostSCSICommon *vsc)
  66. {
  67. VirtIODevice *vdev = VIRTIO_DEVICE(vsc);
  68. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  69. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  70. int ret = 0;
  71. vhost_dev_stop(&vsc->dev, vdev);
  72. if (k->set_guest_notifiers) {
  73. ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false);
  74. if (ret < 0) {
  75. error_report("vhost guest notifier cleanup failed: %d", ret);
  76. }
  77. }
  78. assert(ret >= 0);
  79. vhost_dev_disable_notifiers(&vsc->dev, vdev);
  80. }
  81. uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t features,
  82. Error **errp)
  83. {
  84. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
  85. /* Turn on predefined features supported by this device */
  86. features |= vsc->host_features;
  87. return vhost_get_features(&vsc->dev, vsc->feature_bits, features);
  88. }
  89. void vhost_scsi_common_set_config(VirtIODevice *vdev, const uint8_t *config)
  90. {
  91. VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
  92. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
  93. if ((uint32_t)virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size ||
  94. (uint32_t)virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) {
  95. error_report("vhost-scsi does not support changing the sense data and "
  96. "CDB sizes");
  97. exit(1);
  98. }
  99. }
  100. /*
  101. * Implementation of an interface to adjust firmware path
  102. * for the bootindex property handling.
  103. */
  104. char *vhost_scsi_common_get_fw_dev_path(FWPathProvider *p, BusState *bus,
  105. DeviceState *dev)
  106. {
  107. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
  108. /* format: /channel@channel/vhost-scsi@target,lun */
  109. return g_strdup_printf("/channel@%x/%s@%x,%x", vsc->channel,
  110. qdev_fw_name(dev), vsc->target, vsc->lun);
  111. }
  112. static const TypeInfo vhost_scsi_common_info = {
  113. .name = TYPE_VHOST_SCSI_COMMON,
  114. .parent = TYPE_VIRTIO_SCSI_COMMON,
  115. .instance_size = sizeof(VHostSCSICommon),
  116. .abstract = true,
  117. };
  118. static void virtio_register_types(void)
  119. {
  120. type_register_static(&vhost_scsi_common_info);
  121. }
  122. type_init(virtio_register_types)