rdma_utils.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * QEMU paravirtual RDMA - Generic RDMA backend
  3. *
  4. * Copyright (C) 2018 Oracle
  5. * Copyright (C) 2018 Red Hat Inc
  6. *
  7. * Authors:
  8. * Yuval Shaia <yuval.shaia@oracle.com>
  9. * Marcel Apfelbaum <marcel@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. *
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qapi/qmp/qlist.h"
  17. #include "qapi/qmp/qnum.h"
  18. #include "trace.h"
  19. #include "rdma_utils.h"
  20. void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t plen)
  21. {
  22. void *p;
  23. hwaddr len = plen;
  24. if (!addr) {
  25. rdma_error_report("addr is NULL");
  26. return NULL;
  27. }
  28. p = pci_dma_map(dev, addr, &len, DMA_DIRECTION_TO_DEVICE);
  29. if (!p) {
  30. rdma_error_report("pci_dma_map fail, addr=0x%"PRIx64", len=%"PRId64,
  31. addr, len);
  32. return NULL;
  33. }
  34. if (len != plen) {
  35. rdma_pci_dma_unmap(dev, p, len);
  36. return NULL;
  37. }
  38. trace_rdma_pci_dma_map(addr, p, len);
  39. return p;
  40. }
  41. void rdma_pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len)
  42. {
  43. trace_rdma_pci_dma_unmap(buffer);
  44. if (buffer) {
  45. pci_dma_unmap(dev, buffer, len, DMA_DIRECTION_TO_DEVICE, 0);
  46. }
  47. }
  48. void rdma_protected_qlist_init(RdmaProtectedQList *list)
  49. {
  50. qemu_mutex_init(&list->lock);
  51. list->list = qlist_new();
  52. }
  53. void rdma_protected_qlist_destroy(RdmaProtectedQList *list)
  54. {
  55. if (list->list) {
  56. qlist_destroy_obj(QOBJECT(list->list));
  57. qemu_mutex_destroy(&list->lock);
  58. list->list = NULL;
  59. }
  60. }
  61. void rdma_protected_qlist_append_int64(RdmaProtectedQList *list, int64_t value)
  62. {
  63. qemu_mutex_lock(&list->lock);
  64. qlist_append_int(list->list, value);
  65. qemu_mutex_unlock(&list->lock);
  66. }
  67. int64_t rdma_protected_qlist_pop_int64(RdmaProtectedQList *list)
  68. {
  69. QObject *obj;
  70. qemu_mutex_lock(&list->lock);
  71. obj = qlist_pop(list->list);
  72. qemu_mutex_unlock(&list->lock);
  73. if (!obj) {
  74. return -ENOENT;
  75. }
  76. return qnum_get_uint(qobject_to(QNum, obj));
  77. }
  78. void rdma_protected_gslist_init(RdmaProtectedGSList *list)
  79. {
  80. qemu_mutex_init(&list->lock);
  81. }
  82. void rdma_protected_gslist_destroy(RdmaProtectedGSList *list)
  83. {
  84. if (list->list) {
  85. g_slist_free(list->list);
  86. list->list = NULL;
  87. }
  88. }
  89. void rdma_protected_gslist_append_int32(RdmaProtectedGSList *list,
  90. int32_t value)
  91. {
  92. qemu_mutex_lock(&list->lock);
  93. list->list = g_slist_prepend(list->list, GINT_TO_POINTER(value));
  94. qemu_mutex_unlock(&list->lock);
  95. }
  96. void rdma_protected_gslist_remove_int32(RdmaProtectedGSList *list,
  97. int32_t value)
  98. {
  99. qemu_mutex_lock(&list->lock);
  100. list->list = g_slist_remove(list->list, GINT_TO_POINTER(value));
  101. qemu_mutex_unlock(&list->lock);
  102. }