From 7bd22fed52a1828b0d44a990b52266e9e1d92b5d Mon Sep 17 00:00:00 2001
From: Chengchang Tang <tangchengchang@huawei.com>
Date: Tue, 30 Jan 2024 21:00:46 +0800
Subject: [PATCH 45/46] libhns: fix incorrectly using fixed pagesize

driver inclusion
category: bugfix
bugzilla: https://gitee.com/src-openeuler/rdma-core/issues/IB66RT

------------------------------------------------------------------

Currently, actually used page size is fixed, causing the flexible wqe
buffer size feature to not take effect.

Fixes: 9ab7600d832b ("libhns: Add support for attaching QP's WQE buffer")
Signed-off-by: Chengchang Tang <tangchengchang@huawei.com>
Signed-off-by: Xinghai Cen <cenxinghai@h-partners.com>
---
 providers/hns/hns_roce_u_verbs.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/providers/hns/hns_roce_u_verbs.c b/providers/hns/hns_roce_u_verbs.c
index bce215e..848f836 100644
--- a/providers/hns/hns_roce_u_verbs.c
+++ b/providers/hns/hns_roce_u_verbs.c
@@ -1296,14 +1296,14 @@ static void free_recv_rinl_buf(struct hns_roce_rinl_buf *rinl_buf)
 
 static void get_best_multi_region_pg_shift(struct hns_roce_device *hr_dev,
 					   struct hns_roce_context *ctx,
-					   struct hns_roce_qp *qp)
+					   struct hns_roce_qp *qp, bool dca_en)
 {
 	uint32_t ext_sge_size;
 	uint32_t sq_size;
 	uint32_t rq_size;
 	uint8_t pg_shift;
 
-	if (!(ctx->config & HNS_ROCE_UCTX_RSP_DYN_QP_PGSZ)) {
+	if (!(ctx->config & HNS_ROCE_UCTX_RSP_DYN_QP_PGSZ || dca_en)) {
 		qp->pageshift = HNS_HW_PAGE_SHIFT;
 		return;
 	}
@@ -1334,7 +1334,7 @@ static void get_best_multi_region_pg_shift(struct hns_roce_device *hr_dev,
 
 static int calc_qp_buff_size(struct hns_roce_device *hr_dev,
 			     struct hns_roce_context *ctx,
-			     struct hns_roce_qp *qp)
+			     struct hns_roce_qp *qp, bool dca_en)
 {
 	struct hns_roce_wq *sq = &qp->sq;
 	struct hns_roce_wq *rq = &qp->rq;
@@ -1342,7 +1342,7 @@ static int calc_qp_buff_size(struct hns_roce_device *hr_dev,
 	unsigned int size;
 
 	qp->buf_size = 0;
-	get_best_multi_region_pg_shift(hr_dev, ctx, qp);
+	get_best_multi_region_pg_shift(hr_dev, ctx, qp, dca_en);
 	page_size = 1 << qp->pageshift;
 
 	/* SQ WQE */
@@ -1384,7 +1384,7 @@ static inline bool check_qp_support_dca(struct hns_roce_dca_ctx *dca_ctx,
 	if (hns_attr &&
 	    (hns_attr->comp_mask & HNSDV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS) &&
 	    (hns_attr->create_flags & HNSDV_QP_CREATE_ENABLE_DCA_MODE))
-		return true;
+		return dca_ctx->max_size > 0;
 
 	return false;
 }
@@ -1408,9 +1408,12 @@ static int qp_alloc_wqe(struct ibv_qp_init_attr_ex *attr,
 			struct hns_roce_qp *qp, struct hns_roce_context *ctx)
 {
 	struct hns_roce_device *hr_dev = to_hr_dev(ctx->ibv_ctx.context.device);
+	bool dca_en = check_qp_support_dca(&ctx->dca_ctx, attr, hns_attr);
+	int ret;
 
-	if (calc_qp_buff_size(hr_dev, ctx, qp))
-		return -EINVAL;
+	ret = calc_qp_buff_size(hr_dev, ctx, qp, dca_en);
+	if (ret)
+		return ret;
 
 	qp->sq.wrid = malloc(qp->sq.wqe_cnt * sizeof(uint64_t));
 	if (!qp->sq.wrid)
@@ -1428,19 +1431,18 @@ static int qp_alloc_wqe(struct ibv_qp_init_attr_ex *attr,
 			goto err_alloc;
 	}
 
-	if (check_qp_support_dca(&ctx->dca_ctx, attr, hns_attr) &&
-	    ctx->dca_ctx.max_size > 0) {
+	if (check_qp_support_dca(&ctx->dca_ctx, attr, hns_attr)) {
 		/* when DCA is enabled, use a buffer list to store page addr */
 		qp->buf.buf = NULL;
 		qp->dca_wqe.max_cnt = hr_hw_page_count(qp->buf_size);
-		qp->dca_wqe.shift = HNS_HW_PAGE_SHIFT;
+		qp->dca_wqe.shift = qp->pageshift;
 		qp->dca_wqe.bufs = calloc(qp->dca_wqe.max_cnt, sizeof(void *));
 		if (!qp->dca_wqe.bufs)
 			goto err_alloc;
 		verbs_debug(&ctx->ibv_ctx, "alloc DCA buf.\n");
 	} else {
 		if (hns_roce_alloc_buf(&qp->buf, qp->buf_size,
-				       HNS_HW_PAGE_SIZE))
+				       1 << qp->pageshift))
 			goto err_alloc;
 	}
 
-- 
2.33.0

