From 0b33b387d5b806804ae9278d3911289f8619dfd2 Mon Sep 17 00:00:00 2001
From: Chengchang Tang <tangchengchang@huawei.com>
Date: Tue, 26 Sep 2023 19:19:07 +0800
Subject: [PATCH 15/18] libhns: return error when post send in reset state

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

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

If the device has been resetted, the original business will not be able
to continue. The current design is to allow users to continue issuing IO.
Such a design is meaningless, the user should perceive the exception and
restore the business as soon as possible.

The current kernel mode directly returns an error when device has been
resetted, and this patch can unify the behavior of the kernel mode and
user mode.

Signed-off-by: Chengchang Tang <tangchengchang@huawei.com>
---
 providers/hns/hns_roce_u_hw_v2.c | 66 ++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 15 deletions(-)

diff --git a/providers/hns/hns_roce_u_hw_v2.c b/providers/hns/hns_roce_u_hw_v2.c
index 2119c4c..fe22b43 100644
--- a/providers/hns/hns_roce_u_hw_v2.c
+++ b/providers/hns/hns_roce_u_hw_v2.c
@@ -956,14 +956,24 @@ static int hns_roce_u_v2_arm_cq(struct ibv_cq *ibvcq, int solicited)
 	return 0;
 }
 
-static inline int check_qp_send(struct ibv_qp *qp)
+static int check_qp_send(struct hns_roce_qp *qp, struct hns_roce_context *ctx)
 {
-	if (unlikely(qp->state == IBV_QPS_RESET ||
-		     qp->state == IBV_QPS_INIT ||
-		     qp->state == IBV_QPS_RTR))
+	struct ibv_qp *ibvqp = &qp->verbs_qp.qp;
+	int ret = 0;
+
+	if (unlikely(ibvqp->state == IBV_QPS_RESET ||
+		     ibvqp->state == IBV_QPS_INIT ||
+		     ibvqp->state == IBV_QPS_RTR)){
+		verbs_err(verbs_get_ctx(qp->verbs_qp.qp.context),
+			  "unsupported qp state, state = %d.\n", ibvqp->state);
 		return EINVAL;
+	} else if (unlikely(hns_roce_reseted(ctx))) {
+		verbs_err_datapath(&ctx->ibv_ctx,
+				   "failed to send, device has been reseted!\n");
+		return EIO;
+	}
 
-	return 0;
+	return ret;
 }
 
 static void set_rc_sge(struct hns_roce_v2_wqe_data_seg *dseg,
@@ -1453,7 +1463,7 @@ int hns_roce_u_v2_post_send(struct ibv_qp *ibvqp, struct ibv_send_wr *wr,
 	unsigned int wqe_idx, nreq;
 	int ret;
 
-	ret = check_qp_send(ibvqp);
+	ret = check_qp_send(qp, ctx);
 	if (unlikely(ret)) {
 		*bad_wr = wr;
 		return ret;
@@ -1531,12 +1541,22 @@ out:
 	return ret;
 }
 
-static inline int check_qp_recv(struct ibv_qp *qp)
+static int check_qp_recv(struct hns_roce_qp *qp, struct hns_roce_context *ctx)
 {
-	if (qp->state == IBV_QPS_RESET)
+	struct ibv_qp *ibvqp = &qp->verbs_qp.qp;
+	int ret = 0;
+
+	if (ibvqp->state == IBV_QPS_RESET) {
+		verbs_err(verbs_get_ctx(qp->verbs_qp.qp.context),
+			  "unsupported qp state, state = %d.\n", ibvqp->state);
 		return EINVAL;
+	} else if (unlikely(hns_roce_reseted(ctx))) {
+		verbs_err_datapath(&ctx->ibv_ctx,
+				   "fail to recv, device has been reseted!\n");
+		return EIO;
+	}
 
-	return 0;
+	return ret;
 }
 
 static void fill_recv_sge_to_wqe(struct ibv_recv_wr *wr, void *wqe,
@@ -1603,7 +1623,7 @@ static int hns_roce_u_v2_post_recv(struct ibv_qp *ibvqp, struct ibv_recv_wr *wr,
 	struct ibv_qp_attr attr = {};
 	int ret;
 
-	ret = check_qp_recv(ibvqp);
+	ret = check_qp_recv(qp, ctx);
 	if (unlikely(ret)) {
 		*bad_wr = wr;
 		return ret;
@@ -1933,6 +1953,16 @@ static void update_srq_db(struct hns_roce_context *ctx, struct hns_roce_db *db,
 			 (__le32 *)db);
 }
 
+static int check_srq_recv(struct hns_roce_context *ctx)
+{
+	if (hns_roce_reseted(ctx)) {
+		verbs_err_datapath(&ctx->ibv_ctx,
+			"srq failed to recv, device has been reseted!\n");
+		return EIO;
+	}
+	return 0;
+}
+
 static int hns_roce_u_v2_post_srq_recv(struct ibv_srq *ib_srq,
 				       struct ibv_recv_wr *wr,
 				       struct ibv_recv_wr **bad_wr)
@@ -1944,6 +1974,12 @@ static int hns_roce_u_v2_post_srq_recv(struct ibv_srq *ib_srq,
 	int ret = 0;
 	void *wqe;
 
+	ret = check_srq_recv(ctx);
+	if (ret) {
+		*bad_wr = wr;
+		return ret;
+	}
+
 	hns_roce_spin_lock(&srq->hr_lock);
 
 	max_sge = srq->max_gs - srq->rsv_sge;
@@ -2751,13 +2787,13 @@ static void wr_set_inline_data_list_ud(struct ibv_qp_ex *ibv_qp, size_t num_buf,
 
 static void wr_start(struct ibv_qp_ex *ibv_qp)
 {
+	struct hns_roce_context *ctx = to_hr_ctx(ibv_qp->qp_base.context);
 	struct hns_roce_qp *qp = to_hr_qp(&ibv_qp->qp_base);
-	enum ibv_qp_state state = ibv_qp->qp_base.state;
+	int ret;
 
-	if (state == IBV_QPS_RESET ||
-	    state == IBV_QPS_INIT ||
-	    state == IBV_QPS_RTR) {
-		qp->err = EINVAL;
+	ret = check_qp_send(qp, ctx);
+	if (ret) {
+		qp->err = ret;
 		return;
 	}
 
-- 
2.33.0

