From 85b6e60f3ec97d2479373111b2a4bceb6ef286cd Mon Sep 17 00:00:00 2001
From: Chengchang Tang <tangchengchang@huawei.com>
Date: Fri, 19 Apr 2024 12:06:12 +0800
Subject: [PATCH] libhns: Fix ext_sge page size

driver inclusion
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I98HIN

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

The buffer for extending SGE must be equal to the WQE buffer page
size.

Fixes: bf57963e729c ("libhns: Support flexible WQE buffer page size")
Signed-off-by: Chengchang Tang <tangchengchang@huawei.com>
---
 providers/hns/hns_roce_u_verbs.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/providers/hns/hns_roce_u_verbs.c b/providers/hns/hns_roce_u_verbs.c
index 8964d53..a680339 100644
--- a/providers/hns/hns_roce_u_verbs.c
+++ b/providers/hns/hns_roce_u_verbs.c
@@ -1270,8 +1270,10 @@ static void get_best_multi_region_pg_shift(struct hns_roce_device *hr_dev,
 	/*
 	* The larger the pagesize used, the better the performance, but it
 	* may waste more memory. Therefore, we use the least common multiple
-	* (aligned to power of 2) of sq wqe buffer size, rq wqe buffer size,
 	* and ext_sge buffer size as the pagesize. Additionally, since the
+	* (aligned to power of 2) of sq wqe buffer size and rq wqe buffer
+	* size as the pagesize. And the wqe buffer page cannot be larger
+	* than the buffer size used by extend sge. Additionally, since the
 	* kernel cannot guarantee the allocation of contiguous memory larger
 	* than the system page, the pagesize must be smaller than the system
 	* page.
@@ -1281,8 +1283,10 @@ static void get_best_multi_region_pg_shift(struct hns_roce_device *hr_dev,
 	rq_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
 
 	pg_shift = max_t(uint8_t, sq_size ? hr_ilog32(sq_size) : 0,
-			 ext_sge_size ? hr_ilog32(ext_sge_size) : 0);
-	pg_shift = max_t(uint8_t, pg_shift, rq_size ? hr_ilog32(rq_size) : 0);
+			 rq_size ? hr_ilog32(rq_size) : 0);
+	pg_shift = ext_sge_size ?
+			min_t(uint8_t, pg_shift, hr_ilog32(ext_sge_size)) :
+			pg_shift;
 	pg_shift = max_t(uint8_t, pg_shift,  HNS_HW_PAGE_SHIFT);
 	qp->pageshift = min_t(uint8_t, pg_shift, hr_ilog32(hr_dev->page_size));
 }
-- 
2.33.0

