xref: /openbmc/qemu/hw/rdma/rdma_utils.c (revision edf5ca5d)
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 
16 #include "qemu/osdep.h"
17 #include "hw/pci/pci_device.h"
18 #include "trace.h"
19 #include "rdma_utils.h"
20 
rdma_pci_dma_map(PCIDevice * dev,dma_addr_t addr,dma_addr_t len)21 void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t len)
22 {
23     void *p;
24     dma_addr_t pci_len = len;
25 
26     if (!addr) {
27         rdma_error_report("addr is NULL");
28         return NULL;
29     }
30 
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;
36     }
37 
38     if (pci_len != len) {
39         rdma_pci_dma_unmap(dev, p, pci_len);
40         return NULL;
41     }
42 
43     trace_rdma_pci_dma_map(addr, p, pci_len);
44 
45     return p;
46 }
47 
rdma_pci_dma_unmap(PCIDevice * dev,void * buffer,dma_addr_t len)48 void rdma_pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len)
49 {
50     trace_rdma_pci_dma_unmap(buffer);
51     if (buffer) {
52         pci_dma_unmap(dev, buffer, len, DMA_DIRECTION_TO_DEVICE, 0);
53     }
54 }
55 
rdma_protected_gqueue_init(RdmaProtectedGQueue * list)56 void rdma_protected_gqueue_init(RdmaProtectedGQueue *list)
57 {
58     qemu_mutex_init(&list->lock);
59     list->list = g_queue_new();
60 }
61 
rdma_protected_gqueue_destroy(RdmaProtectedGQueue * list)62 void rdma_protected_gqueue_destroy(RdmaProtectedGQueue *list)
63 {
64     if (list->list) {
65         g_queue_free_full(list->list, g_free);
66         qemu_mutex_destroy(&list->lock);
67         list->list = NULL;
68     }
69 }
70 
rdma_protected_gqueue_append_int64(RdmaProtectedGQueue * list,int64_t value)71 void rdma_protected_gqueue_append_int64(RdmaProtectedGQueue *list,
72                                         int64_t value)
73 {
74     qemu_mutex_lock(&list->lock);
75     g_queue_push_tail(list->list, g_memdup(&value, sizeof(value)));
76     qemu_mutex_unlock(&list->lock);
77 }
78 
rdma_protected_gqueue_pop_int64(RdmaProtectedGQueue * list)79 int64_t rdma_protected_gqueue_pop_int64(RdmaProtectedGQueue *list)
80 {
81     int64_t *valp;
82     int64_t val;
83 
84     qemu_mutex_lock(&list->lock);
85 
86     valp = g_queue_pop_head(list->list);
87     qemu_mutex_unlock(&list->lock);
88 
89     if (!valp) {
90         return -ENOENT;
91     }
92 
93     val = *valp;
94     g_free(valp);
95     return val;
96 }
97 
rdma_protected_gslist_init(RdmaProtectedGSList * list)98 void rdma_protected_gslist_init(RdmaProtectedGSList *list)
99 {
100     qemu_mutex_init(&list->lock);
101 }
102 
rdma_protected_gslist_destroy(RdmaProtectedGSList * list)103 void rdma_protected_gslist_destroy(RdmaProtectedGSList *list)
104 {
105     if (list->list) {
106         g_slist_free(list->list);
107         qemu_mutex_destroy(&list->lock);
108         list->list = NULL;
109     }
110 }
111 
rdma_protected_gslist_append_int32(RdmaProtectedGSList * list,int32_t value)112 void rdma_protected_gslist_append_int32(RdmaProtectedGSList *list,
113                                         int32_t value)
114 {
115     qemu_mutex_lock(&list->lock);
116     list->list = g_slist_prepend(list->list, GINT_TO_POINTER(value));
117     qemu_mutex_unlock(&list->lock);
118 }
119 
rdma_protected_gslist_remove_int32(RdmaProtectedGSList * list,int32_t value)120 void rdma_protected_gslist_remove_int32(RdmaProtectedGSList *list,
121                                         int32_t value)
122 {
123     qemu_mutex_lock(&list->lock);
124     list->list = g_slist_remove(list->list, GINT_TO_POINTER(value));
125     qemu_mutex_unlock(&list->lock);
126 }
127