aboutsummaryrefslogtreecommitdiff
path: root/hw/rdma/vmw/pvrdma_dev_ring.c
blob: 01247fc041d413f501d7891994c6284a2e5c88c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * QEMU paravirtual RDMA - Device rings
 *
 * 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.
 *
 */

#include "qemu/osdep.h"
#include "hw/pci/pci.h"
#include "cpu.h"

#include "../rdma_utils.h"
#include "standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_ring.h"
#include "pvrdma_dev_ring.h"

int pvrdma_ring_init(PvrdmaRing *ring, const char *name, PCIDevice *dev,
                     struct pvrdma_ring *ring_state, uint32_t max_elems,
                     size_t elem_sz, dma_addr_t *tbl, uint32_t npages)
{
    int i;
    int rc = 0;

    strncpy(ring->name, name, MAX_RING_NAME_SZ);
    ring->name[MAX_RING_NAME_SZ - 1] = 0;
    pr_dbg("Initializing %s ring\n", ring->name);
    ring->dev = dev;
    ring->ring_state = ring_state;
    ring->max_elems = max_elems;
    ring->elem_sz = elem_sz;
    pr_dbg("ring->elem_sz=%zu\n", ring->elem_sz);
    pr_dbg("npages=%d\n", npages);
    /* TODO: Give a moment to think if we want to redo driver settings
    atomic_set(&ring->ring_state->prod_tail, 0);
    atomic_set(&ring->ring_state->cons_head, 0);
    */
    ring->npages = npages;
    ring->pages = g_malloc(npages * sizeof(void *));

    for (i = 0; i < npages; i++) {
        if (!tbl[i]) {
            pr_err("npages=%ld but tbl[%d] is NULL\n", (long)npages, i);
            continue;
        }

        ring->pages[i] = rdma_pci_dma_map(dev, tbl[i], TARGET_PAGE_SIZE);
        if (!ring->pages[i]) {
            rc = -ENOMEM;
            pr_dbg("Failed to map to page %d\n", i);
            goto out_free;
        }
        memset(ring->pages[i], 0, TARGET_PAGE_SIZE);
    }

    goto out;

out_free:
    while (i--) {
        rdma_pci_dma_unmap(dev, ring->pages[i], TARGET_PAGE_SIZE);
    }
    g_free(ring->pages);

out:
    return rc;
}

void *pvrdma_ring_next_elem_read(PvrdmaRing *ring)
{
    unsigned int idx = 0, offset;

    /*
    pr_dbg("%s: t=%d, h=%d\n", ring->name, ring->ring_state->prod_tail,
           ring->ring_state->cons_head);
    */

    if (!pvrdma_idx_ring_has_data(ring->ring_state, ring->max_elems, &idx)) {
        pr_dbg("No more data in ring\n");
        return NULL;
    }

    offset = idx * ring->elem_sz;
    /*
    pr_dbg("idx=%d\n", idx);
    pr_dbg("offset=%d\n", offset);
    */
    return ring->pages[offset / TARGET_PAGE_SIZE] + (offset % TARGET_PAGE_SIZE);
}

void pvrdma_ring_read_inc(PvrdmaRing *ring)
{
    pvrdma_idx_ring_inc(&ring->ring_state->cons_head, ring->max_elems);
    /*
    pr_dbg("%s: t=%d, h=%d, m=%ld\n", ring->name,
           ring->ring_state->prod_tail, ring->ring_state->cons_head,
           ring->max_elems);
    */
}

void *pvrdma_ring_next_elem_write(PvrdmaRing *ring)
{
    unsigned int idx, offset, tail;

    /*
    pr_dbg("%s: t=%d, h=%d\n", ring->name, ring->ring_state->prod_tail,
           ring->ring_state->cons_head);
    */

    if (!pvrdma_idx_ring_has_space(ring->ring_state, ring->max_elems, &tail)) {
        pr_dbg("CQ is full\n");
        return NULL;
    }

    idx = pvrdma_idx(&ring->ring_state->prod_tail, ring->max_elems);
    /* TODO: tail == idx */

    offset = idx * ring->elem_sz;
    return ring->pages[offset / TARGET_PAGE_SIZE] + (offset % TARGET_PAGE_SIZE);
}

void pvrdma_ring_write_inc(PvrdmaRing *ring)
{
    pvrdma_idx_ring_inc(&ring->ring_state->prod_tail, ring->max_elems);
    /*
    pr_dbg("%s: t=%d, h=%d, m=%ld\n", ring->name,
           ring->ring_state->prod_tail, ring->ring_state->cons_head,
           ring->max_elems);
    */
}

void pvrdma_ring_free(PvrdmaRing *ring)
{
    if (!ring) {
        return;
    }

    if (!ring->pages) {
        return;
    }

    pr_dbg("ring->npages=%d\n", ring->npages);
    while (ring->npages--) {
        rdma_pci_dma_unmap(ring->dev, ring->pages[ring->npages],
                           TARGET_PAGE_SIZE);
    }

    g_free(ring->pages);
    ring->pages = NULL;
}