mlx5: Adopt struct ibv_buf for internal buffer allocations

Replace the separate void *buf and size_t length fields in struct
mlx5_buf with an embedded struct ibv_buf, aligning the provider's
internal buffer representation with the common ibv_buf abstraction.

Link struct mlx5_buf to the ibv_buf API (pd, addr, size) instead of
maintaining duplicate fields. Add a pd argument to the internal
allocation helpers and initialize the embedded buffer through
ibv_buf_init(), so each buffer records its owning protection domain.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
diff --git a/providers/mlx5/buf.c b/providers/mlx5/buf.c
index 3a3a792..b44ee7d 100644
--- a/providers/mlx5/buf.c
+++ b/providers/mlx5/buf.c
@@ -120,18 +120,21 @@
 }
 
 static int alloc_huge_buf(struct mlx5_context *mctx, struct mlx5_buf *buf,
-			  size_t size, int page_size)
+			  size_t size, int page_size, struct ibv_pd *pd)
 {
 	int found = 0;
 	int nchunk;
 	struct mlx5_hugetlb_mem *hmem;
 	int ret;
 
-	buf->length = align(size, MLX5_Q_CHUNK_SIZE);
-	nchunk = buf->length / MLX5_Q_CHUNK_SIZE;
+	buf->ibv_buf.size = align(size, MLX5_Q_CHUNK_SIZE);
+	nchunk = buf->ibv_buf.size / MLX5_Q_CHUNK_SIZE;
 
-	if (!nchunk)
+	if (!nchunk) {
+		ibv_buf_init(&buf->ibv_buf, pd, NULL, 0);
+		buf->type = MLX5_ALLOC_TYPE_HUGE;
 		return 0;
+	}
 
 	mlx5_spin_lock(&mctx->hugetlb_lock);
 	list_for_each(&mctx->hugetlb_list, hmem, entry) {
@@ -151,7 +154,7 @@
 	mlx5_spin_unlock(&mctx->hugetlb_lock);
 
 	if (!found) {
-		hmem = alloc_huge_mem(buf->length);
+		hmem = alloc_huge_mem(buf->ibv_buf.size);
 		if (!hmem)
 			return -1;
 
@@ -169,9 +172,11 @@
 		mlx5_spin_unlock(&mctx->hugetlb_lock);
 	}
 
-	buf->buf = hmem->shmaddr + buf->base * MLX5_Q_CHUNK_SIZE;
+	ibv_buf_init(&buf->ibv_buf, pd,
+		     hmem->shmaddr + buf->base * MLX5_Q_CHUNK_SIZE,
+		     buf->ibv_buf.size);
 
-	ret = ibv_dontfork_range(buf->buf, buf->length);
+	ret = ibv_dontfork_range(buf->ibv_buf.addr, buf->ibv_buf.size);
 	if (ret) {
 		mlx5_dbg(stderr, MLX5_DBG_CONTIG, "\n");
 		goto out_fork;
@@ -197,7 +202,7 @@
 {
 	int nchunk;
 
-	nchunk = buf->length / MLX5_Q_CHUNK_SIZE;
+	nchunk = buf->ibv_buf.size / MLX5_Q_CHUNK_SIZE;
 	if (!nchunk)
 		return;
 
@@ -213,12 +218,12 @@
 
 void mlx5_free_buf_extern(struct mlx5_context *ctx, struct mlx5_buf *buf)
 {
-	ibv_dofork_range(buf->buf, buf->length);
-	ctx->extern_alloc.free(buf->buf, ctx->extern_alloc.data);
+	ibv_dofork_range(buf->ibv_buf.addr, buf->ibv_buf.size);
+	ctx->extern_alloc.free(buf->ibv_buf.addr, ctx->extern_alloc.data);
 }
 
 int mlx5_alloc_buf_extern(struct mlx5_context *ctx, struct mlx5_buf *buf,
-		size_t size)
+		size_t size, struct ibv_pd *pd)
 {
 	void *addr;
 
@@ -231,8 +236,7 @@
 				ctx->extern_alloc.data);
 			return -1;
 		}
-		buf->buf = addr;
-		buf->length = size;
+		ibv_buf_init(&buf->ibv_buf, pd, addr, size);
 		buf->type = MLX5_ALLOC_TYPE_EXTERNAL;
 		return 0;
 	}
@@ -244,18 +248,19 @@
 static void mlx5_free_buf_custom(struct mlx5_context *ctx,
 			  struct mlx5_buf *buf)
 {
-	struct mlx5_parent_domain *mparent_domain = buf->mparent_domain;
+	struct mlx5_parent_domain *mparent_domain =
+		to_mparent_domain(buf->ibv_buf.pd);
 
 	mparent_domain->free(&mparent_domain->mpd.ibv_pd,
 			     mparent_domain->pd_context,
-			     buf->buf,
+			     buf->ibv_buf.addr,
 			     buf->resource_type);
 }
 
 static int mlx5_alloc_buf_custom(struct mlx5_context *ctx,
-			  struct mlx5_buf *buf, size_t size)
+			  struct mlx5_buf *buf, size_t size, struct ibv_pd *pd)
 {
-	struct mlx5_parent_domain *mparent_domain = buf->mparent_domain;
+	struct mlx5_parent_domain *mparent_domain = to_mparent_domain(pd);
 	void *addr;
 
 	addr = mparent_domain->alloc(&mparent_domain->mpd.ibv_pd,
@@ -266,8 +271,7 @@
 		return 1;
 
 	if (addr || size == 0) {
-		buf->buf = addr;
-		buf->length = size;
+		ibv_buf_init(&buf->ibv_buf, pd, addr, size);
 		buf->type = MLX5_ALLOC_TYPE_CUSTOM;
 		return 0;
 	}
@@ -279,12 +283,13 @@
 			    struct mlx5_buf *buf,
 			    size_t size, int page_size,
 			    enum mlx5_alloc_type type,
-			    const char *component)
+			    const char *component,
+			    struct ibv_pd *pd)
 {
 	int ret;
 
 	if (type == MLX5_ALLOC_TYPE_CUSTOM) {
-		ret = mlx5_alloc_buf_custom(mctx, buf, size);
+		ret = mlx5_alloc_buf_custom(mctx, buf, size, pd);
 		if (ret <= 0)
 			return ret;
 
@@ -300,7 +305,7 @@
 	if (type == MLX5_ALLOC_TYPE_HUGE ||
 	    type == MLX5_ALLOC_TYPE_PREFER_HUGE ||
 	    type == MLX5_ALLOC_TYPE_ALL) {
-		ret = alloc_huge_buf(mctx, buf, size, page_size);
+		ret = alloc_huge_buf(mctx, buf, size, page_size, pd);
 		if (!ret)
 			return 0;
 
@@ -315,7 +320,8 @@
 	if (type == MLX5_ALLOC_TYPE_CONTIG ||
 	    type == MLX5_ALLOC_TYPE_PREFER_CONTIG ||
 	    type == MLX5_ALLOC_TYPE_ALL) {
-		ret = mlx5_alloc_buf_contig(mctx, buf, size, page_size, component);
+		ret = mlx5_alloc_buf_contig(mctx, buf, size, page_size, component,
+					    pd);
 		if (!ret)
 			return 0;
 
@@ -326,9 +332,9 @@
 	}
 
 	if (type == MLX5_ALLOC_TYPE_EXTERNAL)
-		return mlx5_alloc_buf_extern(mctx, buf, size);
+		return mlx5_alloc_buf_extern(mctx, buf, size, pd);
 
-	return mlx5_alloc_buf(buf, size, page_size);
+	return mlx5_alloc_buf(buf, size, page_size, pd);
 
 }
 
@@ -483,7 +489,7 @@
 int mlx5_alloc_buf_contig(struct mlx5_context *mctx,
 			  struct mlx5_buf *buf, size_t size,
 			  int page_size,
-			  const char *component)
+			  const char *component, struct ibv_pd *pd)
 {
 	void *addr = MAP_FAILED;
 	int block_size_exp;
@@ -529,8 +535,7 @@
 		return -1;
 	}
 
-	buf->buf = addr;
-	buf->length = size;
+	ibv_buf_init(&buf->ibv_buf, pd, addr, size);
 	buf->type = MLX5_ALLOC_TYPE_CONTIG;
 
 	return 0;
@@ -538,26 +543,28 @@
 
 void mlx5_free_buf_contig(struct mlx5_context *mctx, struct mlx5_buf *buf)
 {
-	ibv_dofork_range(buf->buf, buf->length);
-	munmap(buf->buf, buf->length);
+	ibv_dofork_range(buf->ibv_buf.addr, buf->ibv_buf.size);
+	munmap(buf->ibv_buf.addr, buf->ibv_buf.size);
 }
 
-int mlx5_alloc_buf(struct mlx5_buf *buf, size_t size, int page_size)
+int mlx5_alloc_buf(struct mlx5_buf *buf, size_t size, int page_size,
+		   struct ibv_pd *pd)
 {
+	void *addr;
 	int ret;
 	int al_size;
 
 	al_size = align(size, page_size);
-	ret = posix_memalign(&buf->buf, page_size, al_size);
+	ret = posix_memalign(&addr, page_size, al_size);
 	if (ret)
 		return ret;
 
-	ret = ibv_dontfork_range(buf->buf, al_size);
+	ret = ibv_dontfork_range(addr, al_size);
 	if (ret)
-		free(buf->buf);
+		free(addr);
 
 	if (!ret) {
-		buf->length = al_size;
+		ibv_buf_init(&buf->ibv_buf, pd, addr, al_size);
 		buf->type = MLX5_ALLOC_TYPE_ANON;
 	}
 
@@ -566,6 +573,6 @@
 
 void mlx5_free_buf(struct mlx5_buf *buf)
 {
-	ibv_dofork_range(buf->buf, buf->length);
-	free(buf->buf);
+	ibv_dofork_range(buf->ibv_buf.addr, buf->ibv_buf.size);
+	free(buf->ibv_buf.addr);
 }
diff --git a/providers/mlx5/cq.c b/providers/mlx5/cq.c
index b2dd664..4d2ad7f 100644
--- a/providers/mlx5/cq.c
+++ b/providers/mlx5/cq.c
@@ -128,12 +128,12 @@
 
 static void *get_buf_cqe(struct mlx5_buf *buf, int n, int cqe_sz)
 {
-	return buf->buf + n * cqe_sz;
+	return buf->ibv_buf.addr + n * cqe_sz;
 }
 
 static void *get_cqe(struct mlx5_cq *cq, int n)
 {
-	return cq->active_buf->buf + n * cq->cqe_sz;
+	return cq->active_buf->ibv_buf.addr + n * cq->cqe_sz;
 }
 
 static void *get_sw_cqe(struct mlx5_cq *cq, int n)
@@ -1947,7 +1947,6 @@
 			    MLX5_CQ_PREFIX, &type, default_type);
 
 	if (type == MLX5_ALLOC_TYPE_CUSTOM) {
-		buf->mparent_domain = to_mparent_domain(cq->parent_domain);
 		buf->req_alignment = dev->page_size;
 		buf->resource_type = MLX5DV_RES_TYPE_CQ;
 	}
@@ -1956,16 +1955,16 @@
 				      align(nent * cqe_sz, dev->page_size),
 				      dev->page_size,
 				      type,
-				      MLX5_CQ_PREFIX);
+				      MLX5_CQ_PREFIX, cq->parent_domain);
 
 	if (ret)
 		return -1;
 
 	if (buf->type != MLX5_ALLOC_TYPE_CUSTOM)
-		memset(buf->buf, 0, nent * cqe_sz);
+		memset(buf->ibv_buf.addr, 0, nent * cqe_sz);
 
 	for (i = 0; i < nent; ++i) {
-		cqe = buf->buf + i * cqe_sz;
+		cqe = buf->ibv_buf.addr + i * cqe_sz;
 		cqe += cqe_sz == 128 ? 1 : 0;
 		cqe->op_own = MLX5_CQE_INVALID << 4;
 	}
diff --git a/providers/mlx5/dbrec.c b/providers/mlx5/dbrec.c
index e35183f..c6ef941 100644
--- a/providers/mlx5/dbrec.c
+++ b/providers/mlx5/dbrec.c
@@ -47,7 +47,8 @@
 	unsigned long			free[0];
 };
 
-static struct mlx5_db_page *__add_page(struct mlx5_context *context)
+static struct mlx5_db_page *__add_page(struct mlx5_context *context,
+				       struct ibv_pd *pd)
 {
 	struct mlx5_db_page *page;
 	int ps = to_mdev(context->ibv_ctx.context.device)->page_size;
@@ -64,9 +65,9 @@
 		return NULL;
 
 	if (mlx5_is_extern_alloc(context))
-		ret = mlx5_alloc_buf_extern(context, &page->buf, ps);
+		ret = mlx5_alloc_buf_extern(context, &page->buf, ps, pd);
 	else
-		ret = mlx5_alloc_buf(&page->buf, ps, ps);
+		ret = mlx5_alloc_buf(&page->buf, ps, ps, pd);
 	if (ret) {
 		free(page);
 		return NULL;
@@ -77,7 +78,7 @@
 	for (i = 0; i < nlong; ++i)
 		page->free[i] = ~0;
 
-	cl_qmap_insert(&context->dbr_map, (uintptr_t) page->buf.buf,
+	cl_qmap_insert(&context->dbr_map, (uintptr_t) page->buf.ibv_buf.addr,
 		       &page->cl_map);
 	list_add(&context->dbr_available_pages, &page->available);
 
@@ -116,7 +117,7 @@
 	if (page)
 		goto found;
 
-	page = __add_page(context);
+	page = __add_page(context, pd);
 	if (!page)
 		goto out;
 
@@ -131,7 +132,7 @@
 	j = ffsl(page->free[i]);
 	--j;
 	page->free[i] &= ~(1UL << j);
-	db = page->buf.buf + (i * 8 * sizeof(long) + j) * context->cache_line_size;
+	db = page->buf.ibv_buf.addr + (i * 8 * sizeof(long) + j) * context->cache_line_size;
 
 out:
 	pthread_mutex_unlock(&context->dbr_map_mutex);
@@ -164,7 +165,7 @@
 	assert(item != cl_qmap_end(&context->dbr_map));
 
 	page = (container_of(item, struct mlx5_db_page, cl_map));
-	i = ((void *) db - page->buf.buf) / context->cache_line_size;
+	i = ((void *) db - page->buf.ibv_buf.addr) / context->cache_line_size;
 	page->free[i / (8 * sizeof(long))] |= 1UL << (i % (8 * sizeof(long)));
 	if (page->use_cnt == page->num_db)
 		list_add(&context->dbr_available_pages, &page->available);
diff --git a/providers/mlx5/dr_send.c b/providers/mlx5/dr_send.c
index 0018b17..0369f53 100644
--- a/providers/mlx5/dr_send.c
+++ b/providers/mlx5/dr_send.c
@@ -251,6 +251,7 @@
 static int dr_qp_alloc_buf(struct dr_qp *dr_qp, int size)
 {
 	int al_size;
+	void *addr;
 	int ret;
 
 	dr_qp->sq.wqe_head = malloc(dr_qp->sq.wqe_cnt *
@@ -261,15 +262,15 @@
 	}
 
 	al_size = align(size, sysconf(_SC_PAGESIZE));
-	ret = posix_memalign(&dr_qp->buf.buf, sysconf(_SC_PAGESIZE), al_size);
+	ret = posix_memalign(&addr, sysconf(_SC_PAGESIZE), al_size);
 	if (ret) {
 		errno = ret;
 		goto free_wqe_head;
 	}
 
-	dr_qp->buf.length = al_size;
+	ibv_buf_init(&dr_qp->buf.ibv_buf, NULL, addr, al_size);
 	dr_qp->buf.type = MLX5_ALLOC_TYPE_ANON;
-	memset(dr_qp->buf.buf, 0, dr_qp->buf.length);
+	memset(dr_qp->buf.ibv_buf.addr, 0, dr_qp->buf.ibv_buf.size);
 
 	return 0;
 
@@ -298,8 +299,8 @@
 	if (dr_qp_alloc_buf(dr_qp, size))
 		goto err_alloc_bufs;
 
-	dr_qp->sq_start = dr_qp->buf.buf + dr_qp->sq.offset;
-	dr_qp->sq.qend = dr_qp->buf.buf + dr_qp->sq.offset +
+	dr_qp->sq_start = dr_qp->buf.ibv_buf.addr + dr_qp->sq.offset;
+	dr_qp->sq.qend = dr_qp->buf.ibv_buf.addr + dr_qp->sq.offset +
 		(dr_qp->sq.wqe_cnt << dr_qp->sq.wqe_shift);
 	dr_qp->rq.head = 0;
 	dr_qp->rq.tail = 0;
@@ -320,8 +321,8 @@
 	if (!dr_qp->db_umem)
 		goto err_db_umem;
 
-	dr_qp->buf_umem = mlx5dv_devx_umem_reg(ctx, dr_qp->buf.buf,
-					       dr_qp->buf.length,
+	dr_qp->buf_umem = mlx5dv_devx_umem_reg(ctx, dr_qp->buf.ibv_buf.addr,
+					       dr_qp->buf.ibv_buf.size,
 					       IBV_ACCESS_LOCAL_WRITE |
 					       IBV_ACCESS_REMOTE_WRITE |
 					       IBV_ACCESS_REMOTE_READ);
@@ -360,7 +361,7 @@
 	free(dr_qp->db);
 err_db_alloc:
 	free(dr_qp->sq.wqe_head);
-	free(dr_qp->buf.buf);
+	free(dr_qp->buf.ibv_buf.addr);
 err_alloc_bufs:
 	free(dr_qp);
 	return NULL;
@@ -384,7 +385,7 @@
 
 	free(dr_qp->db);
 	free(dr_qp->sq.wqe_head);
-	free(dr_qp->buf.buf);
+	free(dr_qp->buf.ibv_buf.addr);
 	free(dr_qp);
 
 	return 0;
diff --git a/providers/mlx5/mlx5.c b/providers/mlx5/mlx5.c
index 857f7ad..8cc9ad6 100644
--- a/providers/mlx5/mlx5.c
+++ b/providers/mlx5/mlx5.c
@@ -1022,13 +1022,13 @@
 
 	if (mqp->sq_buf_size)
 		/* IBV_QPT_RAW_PACKET */
-		qp_out->sq.buf = (void *)((uintptr_t)mqp->sq_buf.buf);
+		qp_out->sq.buf = (void *)((uintptr_t)mqp->sq_buf.ibv_buf.addr);
 	else
-		qp_out->sq.buf = (void *)((uintptr_t)mqp->buf.buf + mqp->sq.offset);
+		qp_out->sq.buf = (void *)((uintptr_t)mqp->buf.ibv_buf.addr + mqp->sq.offset);
 	qp_out->sq.wqe_cnt = mqp->sq.wqe_cnt;
 	qp_out->sq.stride  = 1 << mqp->sq.wqe_shift;
 
-	qp_out->rq.buf     = (void *)((uintptr_t)mqp->buf.buf + mqp->rq.offset);
+	qp_out->rq.buf     = (void *)((uintptr_t)mqp->buf.ibv_buf.addr + mqp->rq.offset);
 	qp_out->rq.wqe_cnt = mqp->rq.wqe_cnt;
 	qp_out->rq.stride  = 1 << mqp->rq.wqe_shift;
 
@@ -1072,7 +1072,7 @@
 	cq_out->cqn       = mcq->cqn;
 	cq_out->cqe_cnt   = mcq->verbs_cq.cq.cqe + 1;
 	cq_out->cqe_size  = mcq->cqe_sz;
-	cq_out->buf       = mcq->active_buf->buf;
+	cq_out->buf       = mcq->active_buf->ibv_buf.addr;
 	cq_out->dbrec     = mcq->dbrec;
 	cq_out->cq_uar	  = mctx->cq_uar_reg;
 
@@ -1103,7 +1103,7 @@
 
 	msrq = container_of(srq_in, struct mlx5_srq, vsrq.srq);
 
-	srq_out->buf       = msrq->buf.buf;
+	srq_out->buf       = msrq->buf.ibv_buf.addr;
 	srq_out->dbrec     = msrq->db;
 	srq_out->stride    = 1 << msrq->wqe_shift;
 	srq_out->head      = msrq->head;
diff --git a/providers/mlx5/mlx5.h b/providers/mlx5/mlx5.h
index bff29af..c0c66df 100644
--- a/providers/mlx5/mlx5.h
+++ b/providers/mlx5/mlx5.h
@@ -437,14 +437,12 @@
 };
 
 struct mlx5_buf {
-	void			       *buf;
-	size_t				length;
+	struct ibv_buf			ibv_buf;
 	int                             base;
 	struct mlx5_hugetlb_mem	       *hmem;
 	enum mlx5_alloc_type		type;
 	uint64_t			resource_type;
 	size_t				req_alignment;
-	struct mlx5_parent_domain	*mparent_domain;
 };
 
 struct mlx5_td {
@@ -1113,16 +1111,18 @@
 void mlx5_close_debug_file(FILE *dbg_fp);
 void mlx5_set_debug_mask(void);
 
-int mlx5_alloc_buf(struct mlx5_buf *buf, size_t size, int page_size);
+int mlx5_alloc_buf(struct mlx5_buf *buf, size_t size, int page_size,
+		   struct ibv_pd *pd);
 void mlx5_free_buf(struct mlx5_buf *buf);
 int mlx5_alloc_buf_contig(struct mlx5_context *mctx, struct mlx5_buf *buf,
-			  size_t size, int page_size, const char *component);
+			  size_t size, int page_size, const char *component,
+			  struct ibv_pd *pd);
 void mlx5_free_buf_contig(struct mlx5_context *mctx, struct mlx5_buf *buf);
 int mlx5_alloc_prefered_buf(struct mlx5_context *mctx,
 			    struct mlx5_buf *buf,
 			    size_t size, int page_size,
 			    enum mlx5_alloc_type alloc_type,
-			    const char *component);
+			    const char *component, struct ibv_pd *pd);
 int mlx5_free_actual_buf(struct mlx5_context *ctx, struct mlx5_buf *buf);
 void mlx5_get_alloc_type(struct mlx5_context *context,
 			 struct ibv_pd *pd,
@@ -1133,7 +1133,7 @@
 bool mlx5_is_custom_alloc(struct ibv_pd *pd);
 bool mlx5_is_extern_alloc(struct mlx5_context *context);
 int mlx5_alloc_buf_extern(struct mlx5_context *ctx, struct mlx5_buf *buf,
-			  size_t size);
+			  size_t size, struct ibv_pd *pd);
 void mlx5_free_buf_extern(struct mlx5_context *ctx, struct mlx5_buf *buf);
 
 __be32 *mlx5_alloc_dbrec(struct mlx5_context *context, struct ibv_pd *pd,
diff --git a/providers/mlx5/qp.c b/providers/mlx5/qp.c
index 60f0e2c..b28273c 100644
--- a/providers/mlx5/qp.c
+++ b/providers/mlx5/qp.c
@@ -64,7 +64,7 @@
 
 static void *get_recv_wqe(struct mlx5_qp *qp, int n)
 {
-	return qp->buf.buf + qp->rq.offset + (n << qp->rq.wqe_shift);
+	return qp->buf.ibv_buf.addr + qp->rq.offset + (n << qp->rq.wqe_shift);
 }
 
 static void *get_wq_recv_wqe(struct mlx5_rwq *rwq, int n)
diff --git a/providers/mlx5/srq.c b/providers/mlx5/srq.c
index b1cc96d..95ad20e 100644
--- a/providers/mlx5/srq.c
+++ b/providers/mlx5/srq.c
@@ -42,7 +42,7 @@
 
 static void *get_wqe(struct mlx5_srq *srq, int n)
 {
-	return srq->buf.buf + (n << srq->wqe_shift);
+	return srq->buf.ibv_buf.addr + (n << srq->wqe_shift);
 }
 
 static inline void set_next_tail(struct mlx5_srq *srq, int next_tail)
@@ -390,7 +390,6 @@
 			    MLX5_ALLOC_TYPE_ANON);
 
 	if (alloc_type == MLX5_ALLOC_TYPE_CUSTOM) {
-		srq->buf.mparent_domain = to_mparent_domain(pd);
 		srq->buf.req_alignment = to_mdev(context->device)->page_size;
 		srq->buf.resource_type = MLX5DV_RES_TYPE_SRQ;
 	}
@@ -399,11 +398,11 @@
 				    &srq->buf, buf_size,
 				    to_mdev(context->device)->page_size,
 				    alloc_type,
-				    MLX5_SRQ_PREFIX))
+				    MLX5_SRQ_PREFIX, pd))
 		return -1;
 
 	if (srq->buf.type != MLX5_ALLOC_TYPE_CUSTOM)
-		memset(srq->buf.buf, 0, buf_size);
+		memset(srq->buf.ibv_buf.addr, 0, buf_size);
 
 	srq->head = 0;
 	srq->tail = align_queue_size(orig_max_wr + 1) - 1;
diff --git a/providers/mlx5/verbs.c b/providers/mlx5/verbs.c
index 1c93f09..2439ccd 100644
--- a/providers/mlx5/verbs.c
+++ b/providers/mlx5/verbs.c
@@ -1118,7 +1118,7 @@
 	cq->cqe_sz			= cqe_sz;
 	cq->flags			= cq_alloc_flags;
 
-	cmd_drv->buf_addr = (uintptr_t) cq->buf_a.buf;
+	cmd_drv->buf_addr = (uintptr_t) cq->buf_a.ibv_buf.addr;
 	cmd_drv->db_addr  = (uintptr_t) cq->dbrec;
 	cmd_drv->cqe_size = cqe_sz;
 
@@ -1313,7 +1313,7 @@
 		goto out;
 	}
 
-	cmd.buf_addr = (uintptr_t)cq->resize_buf->buf;
+	cmd.buf_addr = (uintptr_t)cq->resize_buf->ibv_buf.addr;
 	cmd.cqe_size = cq->resize_cqe_sz;
 
 	err = ibv_cmd_resize_cq(ibcq, cqe - 1, &cmd.ibv_cmd, sizeof(cmd),
@@ -1419,7 +1419,7 @@
 	if (!srq->custom_db)
 		*srq->db = 0;
 
-	cmd.buf_addr = (uintptr_t) srq->buf.buf;
+	cmd.buf_addr = (uintptr_t) srq->buf.ibv_buf.addr;
 	cmd.db_addr  = (uintptr_t) srq->db;
 	srq->wq_sig = srq_sig_enabled();
 	if (srq->wq_sig)
@@ -2021,7 +2021,6 @@
 			    &alloc_type, default_alloc_type);
 
 	if (alloc_type == MLX5_ALLOC_TYPE_CUSTOM) {
-		qp->buf.mparent_domain = to_mparent_domain(attr->pd);
 		if (attr->qp_type != IBV_QPT_RAW_PACKET &&
 		    !(qp->flags & MLX5_QP_FLAGS_USE_UNDERLAY))
 			req_align = mlx5_set_custom_qp_alignment(context, qp);
@@ -2033,7 +2032,7 @@
 				      align(qp->buf_size, req_align),
 				      to_mdev(context->device)->page_size,
 				      alloc_type,
-				      MLX5_QP_PREFIX);
+				      MLX5_QP_PREFIX, attr->pd);
 
 	if (err) {
 		err = -ENOMEM;
@@ -2041,7 +2040,7 @@
 	}
 
 	if (qp->buf.type != MLX5_ALLOC_TYPE_CUSTOM)
-		memset(qp->buf.buf, 0, qp->buf_size);
+		memset(qp->buf.ibv_buf.addr, 0, qp->buf_size);
 
 	if (attr->qp_type == IBV_QPT_RAW_PACKET ||
 	    qp->flags & MLX5_QP_FLAGS_USE_UNDERLAY) {
@@ -2049,7 +2048,6 @@
 						   to_mdev(context->device)->page_size);
 
 		if (alloc_type == MLX5_ALLOC_TYPE_CUSTOM) {
-			qp->sq_buf.mparent_domain = to_mparent_domain(attr->pd);
 			qp->sq_buf.req_alignment = to_mdev(context->device)->page_size;
 			qp->sq_buf.resource_type = MLX5DV_RES_TYPE_QP;
 		}
@@ -2059,14 +2057,14 @@
 					      aligned_sq_buf_size,
 					      to_mdev(context->device)->page_size,
 					      alloc_type,
-					      MLX5_QP_PREFIX);
+					      MLX5_QP_PREFIX, attr->pd);
 		if (err) {
 			err = -ENOMEM;
 			goto rq_buf;
 		}
 
 		if (qp->sq_buf.type != MLX5_ALLOC_TYPE_CUSTOM)
-			memset(qp->sq_buf.buf, 0, aligned_sq_buf_size);
+			memset(qp->sq_buf.ibv_buf.addr, 0, aligned_sq_buf_size);
 	}
 
 	return 0;
@@ -2091,7 +2089,7 @@
 {
 	mlx5_free_actual_buf(ctx, &qp->buf);
 
-	if (qp->sq_buf.buf)
+	if (qp->sq_buf.ibv_buf.addr)
 		mlx5_free_actual_buf(ctx, &qp->sq_buf);
 
 	if (qp->rq.wrid)
@@ -2665,12 +2663,12 @@
 
 	if (attr->qp_type == IBV_QPT_RAW_PACKET ||
 	    qp->flags & MLX5_QP_FLAGS_USE_UNDERLAY) {
-		qp->sq_start = qp->sq_buf.buf;
-		qp->sq.qend = qp->sq_buf.buf +
+		qp->sq_start = qp->sq_buf.ibv_buf.addr;
+		qp->sq.qend = qp->sq_buf.ibv_buf.addr +
 				(qp->sq.wqe_cnt << qp->sq.wqe_shift);
 	} else {
-		qp->sq_start = qp->buf.buf + qp->sq.offset;
-		qp->sq.qend = qp->buf.buf + qp->sq.offset +
+		qp->sq_start = qp->buf.ibv_buf.addr + qp->sq.offset;
+		qp->sq.qend = qp->buf.ibv_buf.addr + qp->sq.offset +
 				(qp->sq.wqe_cnt << qp->sq.wqe_shift);
 	}
 
@@ -2691,10 +2689,10 @@
 		qp->db[MLX5_SND_DBR] = 0;
 	}
 
-	cmd.buf_addr = (uintptr_t) qp->buf.buf;
+	cmd.buf_addr = (uintptr_t) qp->buf.ibv_buf.addr;
 	cmd.sq_buf_addr = (attr->qp_type == IBV_QPT_RAW_PACKET ||
 			   qp->flags & MLX5_QP_FLAGS_USE_UNDERLAY) ?
-			  (uintptr_t) qp->sq_buf.buf : 0;
+			  (uintptr_t) qp->sq_buf.ibv_buf.addr : 0;
 	cmd.db_addr  = (uintptr_t) qp->db;
 	cmd.sq_wqe_count = qp->sq.wqe_cnt;
 	cmd.rq_wqe_count = qp->rq.wqe_cnt;
@@ -3826,7 +3824,7 @@
 	if (!msrq->custom_db)
 		*msrq->db = 0;
 
-	cmd.buf_addr = (uintptr_t)msrq->buf.buf;
+	cmd.buf_addr = (uintptr_t)msrq->buf.ibv_buf.addr;
 	cmd.db_addr  = (uintptr_t)msrq->db;
 	msrq->wq_sig = srq_sig_enabled();
 	if (msrq->wq_sig)
@@ -4382,7 +4380,6 @@
 	}
 
 	if (alloc_type == MLX5_ALLOC_TYPE_CUSTOM) {
-		rwq->buf.mparent_domain = to_mparent_domain(pd);
 		rwq->buf.req_alignment = to_mdev(context->device)->page_size;
 		rwq->buf.resource_type = MLX5DV_RES_TYPE_RWQ;
 	}
@@ -4392,7 +4389,7 @@
 				      (context->device)->page_size),
 				      to_mdev(context->device)->page_size,
 				      alloc_type,
-				      MLX5_RWQ_PREFIX);
+				      MLX5_RWQ_PREFIX, pd);
 
 	if (err) {
 		free(rwq->rq.wrid);
@@ -4454,9 +4451,9 @@
 		rwq->db[MLX5_SND_DBR] = 0;
 	}
 
-	rwq->pbuff = rwq->buf.buf + rwq->rq.offset;
+	rwq->pbuff = rwq->buf.ibv_buf.addr + rwq->rq.offset;
 	rwq->recv_db =  &rwq->db[MLX5_RCV_DBR];
-	cmd.buf_addr = (uintptr_t)rwq->buf.buf;
+	cmd.buf_addr = (uintptr_t)rwq->buf.ibv_buf.addr;
 	cmd.db_addr  = (uintptr_t)rwq->db;
 	cmd.rq_wqe_count = rwq->rq.wqe_cnt;
 	cmd.rq_wqe_shift = rwq->rq.wqe_shift;