rdma_utils.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "hw/pci/pci_device.h"
  17. #include "trace.h"
  18. #include "rdma_utils.h"
  19. void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len)
  20. {
  21. void *p;
  22. dma_addr_t pci_len = len;
  23. if (!addr) {
  24. rdma_error_report("addr is NULL");
  25. return NULL;
  26. }
  27. p = pci_dma_map(dev, addr, &pci_len, DMA_DIRECTION_TO_DEVICE);
  28. if (!p) {
  29. rdma_error_report("pci_dma_map fail, addr=0x%"PRIx64", len=%"PRId64,
  30. addr, pci_len);
  31. return NULL;
  32. }
  33. if (pci_len != len) {
  34. rdma_pci_dma_unmap(dev, p, pci_len);
  35. return NULL;
  36. }
  37. trace_rdma_pci_dma_map(addr, p, pci_len);
  38. return p;
  39. }
  40. void rdma_pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len)
  41. {
  42. trace_rdma_pci_dma_unmap(buffer);
  43. if (buffer) {
  44. pci_dma_unmap(dev, buffer, len, DMA_DIRECTION_TO_DEVICE, 0);
  45. }
  46. }
  47. void rdma_protected_gqueue_init(RdmaProtectedGQueue *list)
  48. {
  49. qemu_mutex_init(&list->lock);
  50. list->list = g_queue_new();
  51. }
  52. void rdma_protected_gqueue_destroy(RdmaProtectedGQueue *list)
  53. {
  54. if (list->list) {
  55. g_queue_free_full(list->list, g_free);
  56. qemu_mutex_destroy(&list->lock);
  57. list->list = NULL;
  58. }
  59. }
  60. void rdma_protected_gqueue_append_int64(RdmaProtectedGQueue *list,
  61. int64_t value)
  62. {
  63. qemu_mutex_lock(&list->lock);
  64. g_queue_push_tail(list->list, g_memdup(&value, sizeof(value)));
  65. qemu_mutex_unlock(&list->lock);
  66. }
  67. int64_t rdma_protected_gqueue_pop_int64(RdmaProtectedGQueue *list)
  68. {
  69. int64_t *valp;
  70. int64_t val;
  71. qemu_mutex_lock(&list->lock);
  72. valp = g_queue_pop_head(list->list);
  73. qemu_mutex_unlock(&list->lock);
  74. if (!valp) {
  75. return -ENOENT;
  76. }
  77. val = *valp;
  78. g_free(valp);
  79. return val;
  80. }
  81. void rdma_protected_gslist_init(RdmaProtectedGSList *list)
  82. {
  83. qemu_mutex_init(&list->lock);
  84. }
  85. void rdma_protected_gslist_destroy(RdmaProtectedGSList *list)
  86. {
  87. if (list->list) {
  88. g_slist_free(list->list);
  89. qemu_mutex_destroy(&list->lock);
  90. list->list = NULL;
  91. }
  92. }
  93. void rdma_protected_gslist_append_int32(RdmaProtectedGSList *list,
  94. int32_t value)
  95. {
  96. qemu_mutex_lock(&list->lock);
  97. list->list = g_slist_prepend(list->list, GINT_TO_POINTER(value));
  98. qemu_mutex_unlock(&list->lock);
  99. }
  100. void rdma_protected_gslist_remove_int32(RdmaProtectedGSList *list,
  101. int32_t value)
  102. {
  103. qemu_mutex_lock(&list->lock);
  104. list->list = g_slist_remove(list->list, GINT_TO_POINTER(value));
  105. qemu_mutex_unlock(&list->lock);
  106. }