2
0

virtio-scsi-dataplane.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Virtio SCSI dataplane
  3. *
  4. * Copyright Red Hat, Inc. 2014
  5. *
  6. * Authors:
  7. * Fam Zheng <famz@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "hw/virtio/virtio-scsi.h"
  16. #include "qemu/error-report.h"
  17. #include "system/block-backend.h"
  18. #include "hw/scsi/scsi.h"
  19. #include "scsi/constants.h"
  20. #include "hw/virtio/iothread-vq-mapping.h"
  21. #include "hw/virtio/virtio-bus.h"
  22. /* Context: BQL held */
  23. void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp)
  24. {
  25. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
  26. VirtIODevice *vdev = VIRTIO_DEVICE(s);
  27. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  28. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  29. if (vs->conf.iothread && vs->conf.iothread_vq_mapping_list) {
  30. error_setg(errp,
  31. "iothread and iothread-vq-mapping properties cannot be set "
  32. "at the same time");
  33. return;
  34. }
  35. if (vs->conf.iothread || vs->conf.iothread_vq_mapping_list) {
  36. if (!k->set_guest_notifiers || !k->ioeventfd_assign) {
  37. error_setg(errp,
  38. "device is incompatible with iothread "
  39. "(transport does not support notifiers)");
  40. return;
  41. }
  42. if (!virtio_device_ioeventfd_enabled(vdev)) {
  43. error_setg(errp, "ioeventfd is required for iothread");
  44. return;
  45. }
  46. }
  47. s->vq_aio_context = g_new(AioContext *, vs->conf.num_queues +
  48. VIRTIO_SCSI_VQ_NUM_FIXED);
  49. /*
  50. * Handle the ctrl virtqueue in the main loop thread where device resets
  51. * can be performed.
  52. */
  53. s->vq_aio_context[0] = qemu_get_aio_context();
  54. /*
  55. * Handle the event virtqueue in the main loop thread where its no_poll
  56. * behavior won't stop IOThread polling.
  57. */
  58. s->vq_aio_context[1] = qemu_get_aio_context();
  59. if (vs->conf.iothread_vq_mapping_list) {
  60. if (!iothread_vq_mapping_apply(vs->conf.iothread_vq_mapping_list,
  61. &s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED],
  62. vs->conf.num_queues, errp)) {
  63. g_free(s->vq_aio_context);
  64. s->vq_aio_context = NULL;
  65. return;
  66. }
  67. } else if (vs->conf.iothread) {
  68. AioContext *ctx = iothread_get_aio_context(vs->conf.iothread);
  69. for (uint16_t i = 0; i < vs->conf.num_queues; i++) {
  70. s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED + i] = ctx;
  71. }
  72. /* Released in virtio_scsi_dataplane_cleanup() */
  73. object_ref(OBJECT(vs->conf.iothread));
  74. } else {
  75. AioContext *ctx = qemu_get_aio_context();
  76. for (unsigned i = 0; i < vs->conf.num_queues; i++) {
  77. s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED + i] = ctx;
  78. }
  79. }
  80. }
  81. /* Context: BQL held */
  82. void virtio_scsi_dataplane_cleanup(VirtIOSCSI *s)
  83. {
  84. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
  85. if (vs->conf.iothread_vq_mapping_list) {
  86. iothread_vq_mapping_cleanup(vs->conf.iothread_vq_mapping_list);
  87. }
  88. if (vs->conf.iothread) {
  89. object_unref(OBJECT(vs->conf.iothread));
  90. }
  91. g_free(s->vq_aio_context);
  92. s->vq_aio_context = NULL;
  93. }
  94. static int virtio_scsi_set_host_notifier(VirtIOSCSI *s, VirtQueue *vq, int n)
  95. {
  96. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
  97. int rc;
  98. /* Set up virtqueue notify */
  99. rc = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), n, true);
  100. if (rc != 0) {
  101. fprintf(stderr, "virtio-scsi: Failed to set host notifier (%d)\n",
  102. rc);
  103. s->dataplane_fenced = true;
  104. return rc;
  105. }
  106. return 0;
  107. }
  108. /* Context: BH in IOThread */
  109. static void virtio_scsi_dataplane_stop_vq_bh(void *opaque)
  110. {
  111. AioContext *ctx = qemu_get_current_aio_context();
  112. VirtQueue *vq = opaque;
  113. EventNotifier *host_notifier;
  114. virtio_queue_aio_detach_host_notifier(vq, ctx);
  115. host_notifier = virtio_queue_get_host_notifier(vq);
  116. /*
  117. * Test and clear notifier after disabling event, in case poll callback
  118. * didn't have time to run.
  119. */
  120. virtio_queue_host_notifier_read(host_notifier);
  121. }
  122. /* Context: BQL held */
  123. int virtio_scsi_dataplane_start(VirtIODevice *vdev)
  124. {
  125. int i;
  126. int rc;
  127. int vq_init_count = 0;
  128. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  129. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  130. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
  131. VirtIOSCSI *s = VIRTIO_SCSI(vdev);
  132. if (s->dataplane_started ||
  133. s->dataplane_starting ||
  134. s->dataplane_fenced) {
  135. return 0;
  136. }
  137. s->dataplane_starting = true;
  138. /* Set up guest notifier (irq) */
  139. rc = k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, true);
  140. if (rc != 0) {
  141. error_report("virtio-scsi: Failed to set guest notifiers (%d), "
  142. "ensure -accel kvm is set.", rc);
  143. goto fail_guest_notifiers;
  144. }
  145. /*
  146. * Batch all the host notifiers in a single transaction to avoid
  147. * quadratic time complexity in address_space_update_ioeventfds().
  148. */
  149. memory_region_transaction_begin();
  150. rc = virtio_scsi_set_host_notifier(s, vs->ctrl_vq, 0);
  151. if (rc != 0) {
  152. goto fail_host_notifiers;
  153. }
  154. vq_init_count++;
  155. rc = virtio_scsi_set_host_notifier(s, vs->event_vq, 1);
  156. if (rc != 0) {
  157. goto fail_host_notifiers;
  158. }
  159. vq_init_count++;
  160. for (i = 0; i < vs->conf.num_queues; i++) {
  161. rc = virtio_scsi_set_host_notifier(s, vs->cmd_vqs[i], i + 2);
  162. if (rc) {
  163. goto fail_host_notifiers;
  164. }
  165. vq_init_count++;
  166. }
  167. memory_region_transaction_commit();
  168. s->dataplane_starting = false;
  169. s->dataplane_started = true;
  170. smp_wmb(); /* paired with aio_notify_accept() */
  171. if (s->bus.drain_count == 0) {
  172. virtio_queue_aio_attach_host_notifier(vs->ctrl_vq,
  173. s->vq_aio_context[0]);
  174. virtio_queue_aio_attach_host_notifier_no_poll(vs->event_vq,
  175. s->vq_aio_context[1]);
  176. for (i = 0; i < vs->conf.num_queues; i++) {
  177. AioContext *ctx = s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED + i];
  178. virtio_queue_aio_attach_host_notifier(vs->cmd_vqs[i], ctx);
  179. }
  180. }
  181. return 0;
  182. fail_host_notifiers:
  183. for (i = 0; i < vq_init_count; i++) {
  184. virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
  185. }
  186. /*
  187. * The transaction expects the ioeventfds to be open when it
  188. * commits. Do it now, before the cleanup loop.
  189. */
  190. memory_region_transaction_commit();
  191. for (i = 0; i < vq_init_count; i++) {
  192. virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
  193. }
  194. k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
  195. fail_guest_notifiers:
  196. s->dataplane_fenced = true;
  197. s->dataplane_starting = false;
  198. s->dataplane_started = true;
  199. return -ENOSYS;
  200. }
  201. /* Context: BQL held */
  202. void virtio_scsi_dataplane_stop(VirtIODevice *vdev)
  203. {
  204. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  205. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  206. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
  207. VirtIOSCSI *s = VIRTIO_SCSI(vdev);
  208. int i;
  209. if (!s->dataplane_started || s->dataplane_stopping) {
  210. return;
  211. }
  212. /* Better luck next time. */
  213. if (s->dataplane_fenced) {
  214. s->dataplane_fenced = false;
  215. s->dataplane_started = false;
  216. return;
  217. }
  218. s->dataplane_stopping = true;
  219. if (s->bus.drain_count == 0) {
  220. for (i = 0; i < vs->conf.num_queues + VIRTIO_SCSI_VQ_NUM_FIXED; i++) {
  221. VirtQueue *vq = virtio_get_queue(&vs->parent_obj, i);
  222. AioContext *ctx = s->vq_aio_context[i];
  223. aio_wait_bh_oneshot(ctx, virtio_scsi_dataplane_stop_vq_bh, vq);
  224. }
  225. }
  226. blk_drain_all(); /* ensure there are no in-flight requests */
  227. /*
  228. * Batch all the host notifiers in a single transaction to avoid
  229. * quadratic time complexity in address_space_update_ioeventfds().
  230. */
  231. memory_region_transaction_begin();
  232. for (i = 0; i < vs->conf.num_queues + 2; i++) {
  233. virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
  234. }
  235. /*
  236. * The transaction expects the ioeventfds to be open when it
  237. * commits. Do it now, before the cleanup loop.
  238. */
  239. memory_region_transaction_commit();
  240. for (i = 0; i < vs->conf.num_queues + 2; i++) {
  241. virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
  242. }
  243. /* Clean up guest notifier (irq) */
  244. k->set_guest_notifiers(qbus->parent, vs->conf.num_queues + 2, false);
  245. s->dataplane_stopping = false;
  246. s->dataplane_started = false;
  247. }