From 746d250131c3727e658f11f37fbb0a8cb18d1b18 Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Sat, 23 Jul 2022 16:50:22 +0800
Subject: [PATCH 155/183] uadk: v1: fix goto warning

1. The goto statement can only jump down.
2. Fix wd_recv return value check.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 v1/drv/hisi_rng_udrv.c |  4 +--
 v1/wd.c                | 33 ++++++++++---------
 v1/wd_comp.c           | 25 ++++++++-------
 v1/wd_dh.c             | 72 ++++++++++++++++++++++++++----------------
 v1/wd_ecc.c            | 15 ++++++---
 v1/wd_rng.c            | 54 +++++++++++++++++--------------
 v1/wd_rsa.c            | 71 +++++++++++++++++++++++++----------------
 7 files changed, 164 insertions(+), 110 deletions(-)

diff --git a/v1/drv/hisi_rng_udrv.c b/v1/drv/hisi_rng_udrv.c
index a66bca5..b86a948 100644
--- a/v1/drv/hisi_rng_udrv.c
+++ b/v1/drv/hisi_rng_udrv.c
@@ -97,7 +97,6 @@ static int rng_read(struct rng_queue_info *info, struct wcrypto_rng_msg *msg)
 	int val;
 
 	do {
-recv_again:
 		val = wd_reg_read((void *)((uintptr_t)info->mmio_base +
 						RNG_NUM_OFFSET));
 		if (!val) {
@@ -105,8 +104,9 @@ recv_again:
 				WD_ERR("read random data timeout\n");
 				break;
 			}
+
 			usleep(1);
-			goto recv_again;
+			continue;
 		}
 
 		recv_count = 0;
diff --git a/v1/wd.c b/v1/wd.c
index 8ef83c6..2c435ba 100644
--- a/v1/wd.c
+++ b/v1/wd.c
@@ -524,7 +524,7 @@ static int get_queue_from_dev(struct wd_queue *q, const struct dev_info *dev)
 	qinfo->fd = open(q_path, O_RDWR | O_CLOEXEC);
 	if (qinfo->fd == -1) {
 		WD_ERR("open %s failed, errno = %d!\n", q_path, errno);
-		return -ENODEV;
+		return -WD_ENODEV;
 	}
 
 	qinfo->hw_type = dev->api;
@@ -575,23 +575,26 @@ int wd_request_queue(struct wd_queue *q)
 		return -WD_ENOMEM;
 	};
 	q->qinfo = dinfop + 1;
-try_again:
-	ret = find_available_res(q, dinfop, NULL);
-	if (ret) {
-		WD_ERR("cannot find available device\n");
-		goto err_with_dev;
-	}
 
-	ret = get_queue_from_dev(q, (const struct dev_info *)dinfop);
-	if (ret == -WD_ENODEV) {
-		try_cnt++;
-		if (try_cnt < _TRY_REQUEST_TIMES) {
+	do {
+		ret = find_available_res(q, dinfop, NULL);
+		if (ret) {
+			WD_ERR("cannot find available device\n");
+			goto err_with_dev;
+		}
+
+		ret = get_queue_from_dev(q, (const struct dev_info *)dinfop);
+		if (!ret) {
+			break;
+		} else {
+			if (try_cnt++ > _TRY_REQUEST_TIMES) {
+				WD_ERR("fail to get queue!\n");
+				goto err_with_dev;
+			}
+
 			memset(dinfop, 0, sizeof(*dinfop));
-			goto try_again;
 		}
-		WD_ERR("fail to get queue!\n");
-		goto err_with_dev;
-	}
+	} while (true);
 
 	ret = drv_open(q);
 	if (ret) {
diff --git a/v1/wd_comp.c b/v1/wd_comp.c
index 33546d4..2f1b8bd 100644
--- a/v1/wd_comp.c
+++ b/v1/wd_comp.c
@@ -243,19 +243,22 @@ int wcrypto_do_comp(void *ctx, struct wcrypto_comp_op_data *opdata, void *tag)
 		return ret;
 
 	resp = (void *)(uintptr_t)cctx->ctx_id;
-recv_again:
-	ret = wd_recv(cctx->q, (void **)&resp);
-	if (ret == -WD_HW_EACCESS) {
-		WD_ERR("wd_recv hw err!\n");
-		goto err_put_cookie;
-	} else if (ret == 0) {
-		if (++recv_count > MAX_RETRY_COUNTS) {
-			WD_ERR("wd_recv timeout fail!\n");
-			ret = -ETIMEDOUT;
+
+	do {
+		ret = wd_recv(cctx->q, (void **)&resp);
+		if (ret > 0) {
+			break;
+		} else if (!ret) {
+			if (++recv_count > MAX_RETRY_COUNTS) {
+				WD_ERR("wd_recv timeout fail!\n");
+				ret = -ETIMEDOUT;
+				goto err_put_cookie;
+			}
+		} else {
+			WD_ERR("failed to recv msg: ret = %d!\n", ret);
 			goto err_put_cookie;
 		}
-		goto recv_again;
-	}
+	} while (true);
 
 	opdata->consumed = resp->in_cons;
 	opdata->produced = resp->produced;
diff --git a/v1/wd_dh.c b/v1/wd_dh.c
index 49f52d9..1164f2c 100644
--- a/v1/wd_dh.c
+++ b/v1/wd_dh.c
@@ -307,6 +307,31 @@ static int do_dh_prepare(struct wcrypto_dh_op_data *opdata,
 	return 0;
 }
 
+static int dh_send(struct wcrypto_dh_ctx *ctx, struct wcrypto_dh_msg *req)
+{
+	uint32_t tx_cnt = 0;
+	int ret;
+
+	do {
+		ret = wd_send(ctx->q, req);
+		if (!ret) {
+			break;
+		} else if (ret == -WD_EBUSY) {
+			if (tx_cnt++ > DH_RESEND_CNT) {
+				WD_ERR("do dh send cnt %u, exit!\n", tx_cnt);
+				break;
+			}
+
+			usleep(1);
+		} else {
+			WD_ERR("do dh wd_send err!\n");
+			break;
+		}
+	} while (true);
+
+	return ret;
+}
+
 int wcrypto_do_dh(void *ctx, struct wcrypto_dh_op_data *opdata, void *tag)
 {
 	struct wcrypto_dh_msg *resp = NULL;
@@ -314,46 +339,39 @@ int wcrypto_do_dh(void *ctx, struct wcrypto_dh_op_data *opdata, void *tag)
 	struct wcrypto_dh_cookie *cookie;
 	struct wcrypto_dh_msg *req;
 	uint32_t rx_cnt = 0;
-	uint32_t tx_cnt = 0;
 	int ret;
 
 	ret = do_dh_prepare(opdata, &cookie, ctxt, &req, tag);
 	if (unlikely(ret))
 		return ret;
 
-send_again:
-	ret = wd_send(ctxt->q, req);
-	if (ret == -WD_EBUSY) {
-		usleep(1);
-		if (tx_cnt++ < DH_RESEND_CNT)
-			goto send_again;
-		else {
-			WD_ERR("do dh send cnt %u, exit!\n", tx_cnt);
-			goto fail_with_cookie;
-		}
-	} else if (unlikely(ret)) {
-		WD_ERR("do dh wd_send err!\n");
+	ret = dh_send(ctxt, req);
+	if (unlikely(ret))
 		goto fail_with_cookie;
-	}
 
 	if (tag)
 		return ret;
 
 	resp = (void *)(uintptr_t)ctxt->ctx_id;
-recv_again:
-	ret = wd_recv(ctxt->q, (void **)&resp);
-	if (!ret) {
-		if (unlikely(rx_cnt++ >= DH_RECV_MAX_CNT)) {
-			WD_ERR("failed to receive: timeout!\n");
-			return -WD_ETIMEDOUT;
-		} else if (balance > DH_BALANCE_THRHD) {
-			usleep(1);
+
+	do {
+		ret = wd_recv(ctxt->q, (void **)&resp);
+		if (ret > 0) {
+			break;
+		} else if (!ret) {
+			if (unlikely(rx_cnt++ >= DH_RECV_MAX_CNT)) {
+				WD_ERR("failed to receive: timeout!\n");
+				ret = -WD_ETIMEDOUT;
+				goto fail_with_cookie;
+			}
+
+			if (balance > DH_BALANCE_THRHD)
+				usleep(1);
+		} else {
+			WD_ERR("do dh wd_recv err!\n");
+			goto fail_with_cookie;
 		}
-		goto recv_again;
-	} else if (unlikely(ret < 0)) {
-		WD_ERR("do dh wd_recv err!\n");
-		goto fail_with_cookie;
-	}
+	} while (true);
 
 	balance = rx_cnt;
 	opdata->pri = (void *)resp->out;
diff --git a/v1/wd_ecc.c b/v1/wd_ecc.c
index 58dabfc..4f9ec53 100644
--- a/v1/wd_ecc.c
+++ b/v1/wd_ecc.c
@@ -1523,17 +1523,20 @@ static int ecc_send(struct wcrypto_ecc_ctx *ctx, struct wcrypto_ecc_msg *req)
 
 	do {
 		ret = wd_send(ctx->q, req);
-		if (ret == -WD_EBUSY) {
+		if (!ret) {
+			break;
+		} else if (ret == -WD_EBUSY) {
 			if (tx_cnt++ >= ECC_RESEND_CNT) {
 				WD_ERR("failed to send: retry exit!\n");
 				break;
 			}
+
 			usleep(1);
 		} else if (unlikely(ret)) {
 			WD_ERR("failed to send: send error = %d!\n", ret);
 			break;
 		}
-	} while (ret);
+	} while (true);
 
 	return ret;
 }
@@ -1549,7 +1552,9 @@ static int ecc_sync_recv(struct wcrypto_ecc_ctx *ctx,
 
 	do {
 		ret = wd_recv(ctx->q, (void **)&resp);
-		if (!ret) {
+		if (ret > 0) {
+			break;
+		} else if (!ret) {
 			if (rx_cnt++ >= ECC_RECV_MAX_CNT) {
 				WD_ERR("failed to recv: timeout!\n");
 				return -WD_ETIMEDOUT;
@@ -1557,11 +1562,11 @@ static int ecc_sync_recv(struct wcrypto_ecc_ctx *ctx,
 
 			if (balance > ECC_BALANCE_THRHD)
 				usleep(1);
-		} else if (unlikely(ret < 0)) {
+		} else {
 			WD_ERR("failed to recv: error = %d!\n", ret);
 			return ret;
 		}
-	} while (!ret);
+	} while (true);
 
 	balance = rx_cnt;
 	opdata->out = resp->out;
diff --git a/v1/wd_rng.c b/v1/wd_rng.c
index 7b3bda3..2077578 100644
--- a/v1/wd_rng.c
+++ b/v1/wd_rng.c
@@ -174,7 +174,7 @@ int wcrypto_rng_poll(struct wd_queue *q, unsigned int num)
 		if (!ret)
 			break;
 
-		if (ret == -WD_EINVAL) {
+		if (ret < 0) {
 			WD_ERR("recv err at trng poll!\n");
 			return ret;
 		}
@@ -241,37 +241,45 @@ int wcrypto_do_rng(void *ctx, struct wcrypto_rng_op_data *opdata, void *tag)
 	if (ret)
 		return ret;
 
-send_again:
-	ret = wd_send(ctxt->q, req);
-	if (ret) {
-		if (++tx_cnt > RNG_RESEND_CNT) {
-			WD_ERR("do trng send cnt %u, exit!\n", tx_cnt);
+	do {
+		ret = wd_send(ctxt->q, req);
+		if (!ret) {
+			break;
+		} else if (ret == -WD_EBUSY) {
+			if (++tx_cnt > RNG_RESEND_CNT) {
+				WD_ERR("do trng send cnt %u, exit!\n", tx_cnt);
+				goto fail_with_cookie;
+			}
+
+			usleep(1);
+		} else {
+			WD_ERR("do rng wd_send err!\n");
 			goto fail_with_cookie;
 		}
-		usleep(1);
-		goto send_again;
-	}
+	} while (true);
 
 	if (tag)
 		return ret;
 
 	resp = (void *)(uintptr_t)ctxt->ctx_id;
-recv_again:
-	ret = wd_recv(ctxt->q, (void **)&resp);
-	if (!ret) {
-		if (++rx_cnt > RNG_RECV_CNT) {
-			WD_ERR("do trng recv cnt %u, exit!\n", rx_cnt);
-			ret = -WD_ETIMEDOUT;
+
+	do {
+		ret = wd_recv(ctxt->q, (void **)&resp);
+		if (ret > 0) {
+			break;
+		} else if (!ret) {
+			if (++rx_cnt > RNG_RECV_CNT) {
+				WD_ERR("do trng recv cnt %u, exit!\n", rx_cnt);
+				ret = -WD_ETIMEDOUT;
+				goto fail_with_cookie;
+			}
+
+			usleep(1);
+		} else {
+			WD_ERR("do trng recv err!\n");
 			goto fail_with_cookie;
 		}
-		usleep(1);
-		goto recv_again;
-	}
-
-	if (ret < 0) {
-		WD_ERR("do trng recv err!\n");
-		goto fail_with_cookie;
-	}
+	} while (true);
 
 	opdata->out_bytes = resp->out_bytes;
 	ret = WD_SUCCESS;
diff --git a/v1/wd_rsa.c b/v1/wd_rsa.c
index 6ba29d7..f0cdda7 100644
--- a/v1/wd_rsa.c
+++ b/v1/wd_rsa.c
@@ -970,6 +970,31 @@ static int do_rsa_prepare(struct wcrypto_rsa_ctx *ctxt,
 	return 0;
 }
 
+static int rsa_send(struct wcrypto_rsa_ctx *ctx, struct wcrypto_rsa_msg *req)
+{
+	uint32_t tx_cnt = 0;
+	int ret;
+
+	do {
+		ret = wd_send(ctx->q, req);
+		if (!ret) {
+			break;
+		} else if (ret == -WD_EBUSY) {
+			if (tx_cnt++ > RSA_RESEND_CNT) {
+				WD_ERR("do rsa send cnt %u, exit!\n", tx_cnt);
+				break;
+			}
+
+			usleep(1);
+		} else {
+			WD_ERR("do rsa wd_send err!\n");
+			break;
+		}
+	} while (true);
+
+	return ret;
+}
+
 int wcrypto_do_rsa(void *ctx, struct wcrypto_rsa_op_data *opdata, void *tag)
 {
 	struct wcrypto_rsa_msg *resp = NULL;
@@ -977,46 +1002,38 @@ int wcrypto_do_rsa(void *ctx, struct wcrypto_rsa_op_data *opdata, void *tag)
 	struct wcrypto_rsa_cookie *cookie;
 	struct wcrypto_rsa_msg *req;
 	uint32_t rx_cnt = 0;
-	uint32_t tx_cnt = 0;
 	int ret;
 
 	ret = do_rsa_prepare(ctxt, opdata, &cookie, &req, tag);
 	if (unlikely(ret))
 		return ret;
 
-send_again:
-	ret = wd_send(ctxt->q, req);
-	if (ret == -WD_EBUSY) {
-		usleep(1);
-		if (tx_cnt++ < RSA_RESEND_CNT)
-			goto send_again;
-		else {
-			WD_ERR("do rsa send cnt %u, exit!\n", tx_cnt);
-			goto fail_with_cookie;
-		}
-	} else if (unlikely(ret)) {
-		WD_ERR("do rsa wd_send err!\n");
+	ret = rsa_send(ctxt, req);
+	if (unlikely(ret))
 		goto fail_with_cookie;
-	}
 
 	if (tag)
 		return ret;
 
 	resp = (void *)(uintptr_t)ctxt->ctx_id;
-recv_again:
-	ret = wd_recv(ctxt->q, (void **)&resp);
-	if (!ret) {
-		if (unlikely(rx_cnt++ >= RSA_RECV_MAX_CNT)) {
-			WD_ERR("failed to recv: timeout!\n");
-			return -WD_ETIMEDOUT;
-		} else if (balance > RSA_BALANCE_THRHD) {
-			usleep(1);
+	do {
+		ret = wd_recv(ctxt->q, (void **)&resp);
+		if (ret > 0) {
+			break;
+		} else if (!ret) {
+			if (unlikely(rx_cnt++ >= RSA_RECV_MAX_CNT)) {
+				WD_ERR("failed to recv: timeout!\n");
+				ret = -WD_ETIMEDOUT;
+				goto fail_with_cookie;
+			}
+
+			if (balance > RSA_BALANCE_THRHD)
+				usleep(1);
+		} else {
+			WD_ERR("do rsa wd_recv err!\n");
+			goto fail_with_cookie;
 		}
-		goto recv_again;
-	} else if (unlikely(ret < 0)) {
-		WD_ERR("do rsa wd_recv err!\n");
-		goto fail_with_cookie;
-	}
+	} while (true);
 
 	balance = rx_cnt;
 	opdata->out = (void *)resp->out;
-- 
2.27.0

