include: Include headers where needed
[qemu.git] / hw / rdma / rdma_utils.c
blobc948baf052f9ec5579e4243af2b562656f06fbbc
1 /*
2 * QEMU paravirtual RDMA - Generic RDMA backend
4 * Copyright (C) 2018 Oracle
5 * Copyright (C) 2018 Red Hat Inc
7 * Authors:
8 * Yuval Shaia <yuval.shaia@oracle.com>
9 * Marcel Apfelbaum <marcel@redhat.com>
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.
16 #include "qemu/osdep.h"
17 #include "hw/pci/pci_device.h"
18 #include "trace.h"
19 #include "rdma_utils.h"
21 void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len)
23 void *p;
24 dma_addr_t pci_len = len;
26 if (!addr) {
27 rdma_error_report("addr is NULL");
28 return NULL;
31 p = pci_dma_map(dev, addr, &pci_len, DMA_DIRECTION_TO_DEVICE);
32 if (!p) {
33 rdma_error_report("pci_dma_map fail, addr=0x%"PRIx64", len=%"PRId64,
34 addr, pci_len);
35 return NULL;
38 if (pci_len != len) {
39 rdma_pci_dma_unmap(dev, p, pci_len);
40 return NULL;
43 trace_rdma_pci_dma_map(addr, p, pci_len);
45 return p;
48 void rdma_pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len)
50 trace_rdma_pci_dma_unmap(buffer);
51 if (buffer) {
52 pci_dma_unmap(dev, buffer, len, DMA_DIRECTION_TO_DEVICE, 0);
56 void rdma_protected_gqueue_init(RdmaProtectedGQueue *list)
58 qemu_mutex_init(&list->lock);
59 list->list = g_queue_new();
62 void rdma_protected_gqueue_destroy(RdmaProtectedGQueue *list)
64 if (list->list) {
65 g_queue_free_full(list->list, g_free);
66 qemu_mutex_destroy(&list->lock);
67 list->list = NULL;
71 void rdma_protected_gqueue_append_int64(RdmaProtectedGQueue *list,
72 int64_t value)
74 qemu_mutex_lock(&list->lock);
75 g_queue_push_tail(list->list, g_memdup(&value, sizeof(value)));
76 qemu_mutex_unlock(&list->lock);
79 int64_t rdma_protected_gqueue_pop_int64(RdmaProtectedGQueue *list)
81 int64_t *valp;
82 int64_t val;
84 qemu_mutex_lock(&list->lock);
86 valp = g_queue_pop_head(list->list);
87 qemu_mutex_unlock(&list->lock);
89 if (!valp) {
90 return -ENOENT;
93 val = *valp;
94 g_free(valp);
95 return val;
98 void rdma_protected_gslist_init(RdmaProtectedGSList *list)
100 qemu_mutex_init(&list->lock);
103 void rdma_protected_gslist_destroy(RdmaProtectedGSList *list)
105 if (list->list) {
106 g_slist_free(list->list);
107 qemu_mutex_destroy(&list->lock);
108 list->list = NULL;
112 void rdma_protected_gslist_append_int32(RdmaProtectedGSList *list,
113 int32_t value)
115 qemu_mutex_lock(&list->lock);
116 list->list = g_slist_prepend(list->list, GINT_TO_POINTER(value));
117 qemu_mutex_unlock(&list->lock);
120 void rdma_protected_gslist_remove_int32(RdmaProtectedGSList *list,
121 int32_t value)
123 qemu_mutex_lock(&list->lock);
124 list->list = g_slist_remove(list->list, GINT_TO_POINTER(value));
125 qemu_mutex_unlock(&list->lock);