2018-02-09 16:39:19 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-03-21 18:22:07 +03:00
|
|
|
#include "qemu/osdep.h"
|
2020-03-16 19:07:02 +03:00
|
|
|
#include "qemu/cutils.h"
|
2018-03-21 18:22:07 +03:00
|
|
|
#include "hw/pci/pci.h"
|
|
|
|
#include "cpu.h"
|
2020-03-18 16:48:49 +03:00
|
|
|
#include "qemu/cutils.h"
|
2018-02-09 16:39:19 +03:00
|
|
|
|
2019-03-11 13:29:05 +03:00
|
|
|
#include "trace.h"
|
|
|
|
|
2018-02-09 16:39:19 +03:00
|
|
|
#include "../rdma_utils.h"
|
|
|
|
#include "pvrdma_dev_ring.h"
|
|
|
|
|
|
|
|
int pvrdma_ring_init(PvrdmaRing *ring, const char *name, PCIDevice *dev,
|
2021-01-22 21:00:29 +03:00
|
|
|
PvrdmaRingState *ring_state, uint32_t max_elems,
|
2018-03-22 12:52:20 +03:00
|
|
|
size_t elem_sz, dma_addr_t *tbl, uint32_t npages)
|
2018-02-09 16:39:19 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rc = 0;
|
|
|
|
|
2020-03-18 16:48:49 +03:00
|
|
|
pstrcpy(ring->name, MAX_RING_NAME_SZ, name);
|
2018-02-09 16:39:19 +03:00
|
|
|
ring->dev = dev;
|
|
|
|
ring->ring_state = ring_state;
|
|
|
|
ring->max_elems = max_elems;
|
|
|
|
ring->elem_sz = elem_sz;
|
|
|
|
/* TODO: Give a moment to think if we want to redo driver settings
|
2020-09-23 13:56:46 +03:00
|
|
|
qatomic_set(&ring->ring_state->prod_tail, 0);
|
|
|
|
qatomic_set(&ring->ring_state->cons_head, 0);
|
2018-02-09 16:39:19 +03:00
|
|
|
*/
|
|
|
|
ring->npages = npages;
|
|
|
|
ring->pages = g_malloc(npages * sizeof(void *));
|
|
|
|
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
if (!tbl[i]) {
|
2019-03-11 13:29:05 +03:00
|
|
|
rdma_error_report("npages=%d but tbl[%d] is NULL", npages, i);
|
2018-02-09 16:39:19 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ring->pages[i] = rdma_pci_dma_map(dev, tbl[i], TARGET_PAGE_SIZE);
|
|
|
|
if (!ring->pages[i]) {
|
|
|
|
rc = -ENOMEM;
|
2019-03-11 13:29:05 +03:00
|
|
|
rdma_error_report("Failed to map to page %d in ring %s", i, name);
|
2018-02-09 16:39:19 +03:00
|
|
|
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)
|
|
|
|
{
|
2021-01-22 21:00:29 +03:00
|
|
|
unsigned int idx, offset;
|
|
|
|
const uint32_t tail = qatomic_read(&ring->ring_state->prod_tail);
|
|
|
|
const uint32_t head = qatomic_read(&ring->ring_state->cons_head);
|
2018-02-09 16:39:19 +03:00
|
|
|
|
2021-01-22 21:00:29 +03:00
|
|
|
if (tail & ~((ring->max_elems << 1) - 1) ||
|
|
|
|
head & ~((ring->max_elems << 1) - 1) ||
|
|
|
|
tail == head) {
|
2019-03-11 13:29:05 +03:00
|
|
|
trace_pvrdma_ring_next_elem_read_no_data(ring->name);
|
2018-02-09 16:39:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-22 21:00:29 +03:00
|
|
|
idx = head & (ring->max_elems - 1);
|
2018-02-09 16:39:19 +03:00
|
|
|
offset = idx * ring->elem_sz;
|
|
|
|
return ring->pages[offset / TARGET_PAGE_SIZE] + (offset % TARGET_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pvrdma_ring_read_inc(PvrdmaRing *ring)
|
|
|
|
{
|
2021-01-22 21:00:29 +03:00
|
|
|
uint32_t idx = qatomic_read(&ring->ring_state->cons_head);
|
|
|
|
|
|
|
|
idx = (idx + 1) & ((ring->max_elems << 1) - 1);
|
|
|
|
qatomic_set(&ring->ring_state->cons_head, idx);
|
2018-02-09 16:39:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void *pvrdma_ring_next_elem_write(PvrdmaRing *ring)
|
|
|
|
{
|
2021-01-22 21:00:29 +03:00
|
|
|
unsigned int idx, offset;
|
|
|
|
const uint32_t tail = qatomic_read(&ring->ring_state->prod_tail);
|
|
|
|
const uint32_t head = qatomic_read(&ring->ring_state->cons_head);
|
2018-02-09 16:39:19 +03:00
|
|
|
|
2021-01-22 21:00:29 +03:00
|
|
|
if (tail & ~((ring->max_elems << 1) - 1) ||
|
|
|
|
head & ~((ring->max_elems << 1) - 1) ||
|
|
|
|
tail == (head ^ ring->max_elems)) {
|
2019-03-11 13:29:05 +03:00
|
|
|
rdma_error_report("CQ is full");
|
2018-02-09 16:39:19 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-22 21:00:29 +03:00
|
|
|
idx = tail & (ring->max_elems - 1);
|
2018-02-09 16:39:19 +03:00
|
|
|
offset = idx * ring->elem_sz;
|
|
|
|
return ring->pages[offset / TARGET_PAGE_SIZE] + (offset % TARGET_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pvrdma_ring_write_inc(PvrdmaRing *ring)
|
|
|
|
{
|
2021-01-22 21:00:29 +03:00
|
|
|
uint32_t idx = qatomic_read(&ring->ring_state->prod_tail);
|
|
|
|
|
|
|
|
idx = (idx + 1) & ((ring->max_elems << 1) - 1);
|
|
|
|
qatomic_set(&ring->ring_state->prod_tail, idx);
|
2018-02-09 16:39:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void pvrdma_ring_free(PvrdmaRing *ring)
|
|
|
|
{
|
|
|
|
if (!ring) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ring->pages) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ring->npages--) {
|
|
|
|
rdma_pci_dma_unmap(ring->dev, ring->pages[ring->npages],
|
|
|
|
TARGET_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(ring->pages);
|
|
|
|
ring->pages = NULL;
|
|
|
|
}
|