From a57d5dfbc2701b9d0c47eb70a1bb82b16170a7d2 Mon Sep 17 00:00:00 2001
From: Luoyouming <luoyouming@huawei.com>
Date: Tue, 20 Sep 2022 11:53:18 +0800
Subject: [PATCH v4 02/10] libhns: Fix ext_sge num error when post send

The max_gs is the sum of extended sge and standard sge. In function
fill_ext_sge_inl_data, max_gs does not subtract the number of extended
sges, but is directly used to calculate the size of extended sges.

Fixes:b7814b7b9715("libhns: Support inline data in extented sge space for RC")

Signed-off-by: Luoyouming <luoyouming@huawei.com>
Reviewed-by: Yangyang Li <liyangyang20@huawei.com>
---
 providers/hns/hns_roce_u_hw_v2.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/providers/hns/hns_roce_u_hw_v2.c b/providers/hns/hns_roce_u_hw_v2.c
index d9ea18e..bb4298f 100644
--- a/providers/hns/hns_roce_u_hw_v2.c
+++ b/providers/hns/hns_roce_u_hw_v2.c
@@ -841,6 +841,14 @@ static void get_src_buf_info(void **src_addr, uint32_t *src_len,
 	}
 }
 
+static unsigned int get_std_sge_num(struct hns_roce_qp *qp)
+{
+	if (qp->verbs_qp.qp.qp_type == IBV_QPT_UD)
+		return 0;
+
+	return HNS_ROCE_SGE_IN_WQE;
+}
+
 static int fill_ext_sge_inl_data(struct hns_roce_qp *qp,
 				 struct hns_roce_sge_info *sge_info,
 				 const void *buf_list,
@@ -850,9 +858,12 @@ static int fill_ext_sge_inl_data(struct hns_roce_qp *qp,
 	unsigned int sge_mask = qp->ex_sge.sge_cnt - 1;
 	void *dst_addr, *src_addr, *tail_bound_addr;
 	uint32_t src_len, tail_len;
+	unsigned int std_sge_num;
 	int i;
 
-	if (sge_info->total_len > qp->sq.max_gs * HNS_ROCE_SGE_SIZE)
+	std_sge_num = get_std_sge_num(qp);
+	if (sge_info->total_len >
+	    (qp->sq.max_gs - std_sge_num) * HNS_ROCE_SGE_SIZE)
 		return EINVAL;
 
 	dst_addr = get_send_sge_ex(qp, sge_info->start_idx & sge_mask);
-- 
2.30.0

