rdma_utils.c 2.0 KB
Newer Older
Y
Yuval Shaia 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * QEMU paravirtual RDMA - Generic RDMA backend
 *
 * Copyright (C) 2018 Oracle
 * Copyright (C) 2018 Red Hat Inc
 *
 * Authors:
 *     Yuval Shaia <yuval.shaia@oracle.com>
 *     Marcel Apfelbaum <marcel@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */

M
Markus Armbruster 已提交
16
#include "qemu/osdep.h"
Y
Yuval Shaia 已提交
17 18
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qnum.h"
19
#include "trace.h"
Y
Yuval Shaia 已提交
20 21 22 23 24 25 26 27
#include "rdma_utils.h"

void *rdma_pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t plen)
{
    void *p;
    hwaddr len = plen;

    if (!addr) {
28
        rdma_error_report("addr is NULL");
Y
Yuval Shaia 已提交
29 30 31 32 33
        return NULL;
    }

    p = pci_dma_map(dev, addr, &len, DMA_DIRECTION_TO_DEVICE);
    if (!p) {
34 35
        rdma_error_report("pci_dma_map fail, addr=0x%"PRIx64", len=%"PRId64,
                          addr, len);
Y
Yuval Shaia 已提交
36 37 38 39 40 41 42 43
        return NULL;
    }

    if (len != plen) {
        rdma_pci_dma_unmap(dev, p, len);
        return NULL;
    }

44
    trace_rdma_pci_dma_map(addr, p, len);
Y
Yuval Shaia 已提交
45 46 47 48 49 50

    return p;
}

void rdma_pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len)
{
51
    trace_rdma_pci_dma_unmap(buffer);
Y
Yuval Shaia 已提交
52 53 54 55
    if (buffer) {
        pci_dma_unmap(dev, buffer, len, DMA_DIRECTION_TO_DEVICE, 0);
    }
}
Y
Yuval Shaia 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

void rdma_protected_qlist_init(RdmaProtectedQList *list)
{
    qemu_mutex_init(&list->lock);
    list->list = qlist_new();
}

void rdma_protected_qlist_destroy(RdmaProtectedQList *list)
{
    if (list->list) {
        qlist_destroy_obj(QOBJECT(list->list));
        qemu_mutex_destroy(&list->lock);
        list->list = NULL;
    }
}

void rdma_protected_qlist_append_int64(RdmaProtectedQList *list, int64_t value)
{
    qemu_mutex_lock(&list->lock);
    qlist_append_int(list->list, value);
    qemu_mutex_unlock(&list->lock);
}

int64_t rdma_protected_qlist_pop_int64(RdmaProtectedQList *list)
{
    QObject *obj;

    qemu_mutex_lock(&list->lock);
    obj = qlist_pop(list->list);
    qemu_mutex_unlock(&list->lock);

    if (!obj) {
        return -ENOENT;
    }

    return qnum_get_uint(qobject_to(QNum, obj));
}