Skip to content

Commit

Permalink
bnxt_re/lib: Helper functions to allocate the queue structures
Browse files Browse the repository at this point in the history
Adds few helper functions for the series which reorganize
the queue memory allocation. Includes functions that allocate/free
queue memory and get a buffer address from the allocated memory.
Also, added some data structure changes.

Signed-off-by: Selvin Xavier <[email protected]>
  • Loading branch information
selvintxavier committed Mar 12, 2024
1 parent 3ff0150 commit 710f439
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions providers/bnxt_re/bnxt_re-abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ enum bnxt_re_db_epoch_flag_shift {
BNXT_RE_DB_EPOCH_HEAD_SHIFT = (BNXT_RE_DB_EPOCH_SHIFT - 1)
};

#define BNXT_RE_STATIC_WQE_MAX_SGE 0x06

enum bnxt_re_modes {
BNXT_RE_WQE_MODE_STATIC = 0x00,
BNXT_RE_WQE_MODE_VARIABLE = 0x01
Expand Down
3 changes: 2 additions & 1 deletion providers/bnxt_re/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ struct bnxt_re_srq {
};

struct bnxt_re_joint_queue {
struct bnxt_re_context *cntx;
struct bnxt_re_queue *hwque;
struct bnxt_re_wrid *swque;
uint32_t start_idx;
Expand Down Expand Up @@ -205,7 +206,7 @@ struct bnxt_re_qp {
uint8_t push_st_en;
uint16_t max_push_sz;
uint8_t qptyp;
/* irdord? */
struct bnxt_re_mem *mem;
};

struct bnxt_re_mr {
Expand Down
62 changes: 62 additions & 0 deletions providers/bnxt_re/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*/

#include <string.h>
#include <malloc.h>
#include <sys/mman.h>
#include <util/util.h>

Expand Down Expand Up @@ -75,3 +76,64 @@ void bnxt_re_free_aligned(struct bnxt_re_queue *que)
que->bytes = 0;
}
}

void bnxt_re_free_mem(struct bnxt_re_mem *mem)
{
if (mem->va_head) {
ibv_dofork_range(mem->va_head, mem->size);
munmap(mem->va_head, mem->size);
}

free(mem);
}

void *bnxt_re_alloc_mem(size_t size, uint32_t pg_size)
{
struct bnxt_re_mem *mem;

mem = calloc(1, sizeof(*mem));
if (!mem)
return NULL;

size = align(size, pg_size);
mem->size = size;
mem->va_head = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem->va_head == MAP_FAILED)
goto bail;

if (ibv_dontfork_range(mem->va_head, size))
goto unmap;

mem->head = 0;
mem->tail = 0;
mem->va_tail = (void *)((char *)mem->va_head + size);
return mem;
unmap:
munmap(mem->va_head, size);
bail:
free(mem);
return NULL;
}

void *bnxt_re_get_obj(struct bnxt_re_mem *mem, size_t req)
{
void *va;

if ((mem->size - mem->tail - req) < mem->head)
return NULL;
mem->tail += req;
va = (void *)((char *)mem->va_tail - mem->tail);
return va;
}

void *bnxt_re_get_ring(struct bnxt_re_mem *mem, size_t req)
{
void *va;

if ((mem->head + req) > (mem->size - mem->tail))
return NULL;
va = (void *)((char *)mem->va_head + mem->head);
mem->head += req;
return va;
}
26 changes: 26 additions & 0 deletions providers/bnxt_re/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@
#define __MEMORY_H__

#include <pthread.h>
#include "main.h"

struct bnxt_re_mem {
void *va_head;
void *va_tail;
uint32_t head;
uint32_t tail;
uint32_t size;
uint32_t pad;
};

#define BNXT_RE_QATTR_SQ_INDX 0
#define BNXT_RE_QATTR_RQ_INDX 1
struct bnxt_re_qattr {
uint32_t esize;
uint32_t slots;
uint32_t nwr;
uint32_t sz_ring;
uint32_t sz_shad;
uint32_t sw_nwr;
};

struct bnxt_re_queue {
void *va;
Expand Down Expand Up @@ -123,4 +144,9 @@ static inline void bnxt_re_incr_head(struct bnxt_re_queue *que, uint8_t cnt)
}
}

void bnxt_re_free_mem(struct bnxt_re_mem *mem);
void *bnxt_re_alloc_mem(size_t size, uint32_t pg_size);
void *bnxt_re_get_obj(struct bnxt_re_mem *mem, size_t req);
void *bnxt_re_get_ring(struct bnxt_re_mem *mem, size_t req);

#endif

0 comments on commit 710f439

Please sign in to comment.