From 2b4923b7290eba36bc2fefc517129fb30a3d207d Mon Sep 17 00:00:00 2001
From: Weili Qian <qianweili@huawei.com>
Date: Mon, 21 Feb 2022 09:14:17 +0800
Subject: [PATCH 64/64] uadk/v1: fix the waiting time for receiving task

SEC device processes tasks quickly. If 'usleep' is used to
wait for the hardware to process tasks, CPU scheduling will
reduce performance. Therefore, increase the number of cycles
to replace 'usleep'.

Signed-off-by: Weili Qian <qianweili@huawei.com>
---
 v1/wd_aead.c   | 15 ++++++++-------
 v1/wd_cipher.c | 15 ++++++++-------
 v1/wd_digest.c | 15 ++++++++-------
 3 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/v1/wd_aead.c b/v1/wd_aead.c
index 181b971..ad22426 100644
--- a/v1/wd_aead.c
+++ b/v1/wd_aead.c
@@ -35,8 +35,7 @@
 #define MAX_AEAD_AUTH_SIZE		64
 #define MAX_AEAD_ASSOC_SIZE		65536
 #define MAX_HMAC_KEY_SIZE		128
-#define MAX_AEAD_RETRY_CNT		2000000
-#define AEAD_SLEEP_INTERVAL		0xf
+#define MAX_AEAD_RETRY_CNT		20000000
 
 #define DES_KEY_SIZE 8
 #define SM4_KEY_SIZE 16
@@ -509,16 +508,18 @@ static int aead_recv_sync(struct wcrypto_aead_ctx *a_ctx,
 	while (true) {
 		ret = wd_burst_recv(a_ctx->q, (void **)(resp + recv_count),
 				    num - recv_count);
-		if (ret >= 0) {
+		if (ret > 0) {
 			recv_count += ret;
 			if (recv_count == num)
 				break;
 
-			if (++rx_cnt > MAX_AEAD_RETRY_CNT)
+			rx_cnt = 0;
+		} else if (ret == 0) {
+			if (++rx_cnt > MAX_AEAD_RETRY_CNT) {
+				WD_ERR("%s:wcrypto_recv timeout, num = %u, recv_count = %u!\n",
+					__func__, num, recv_count);
 				break;
-
-			if (!(rx_cnt & AEAD_SLEEP_INTERVAL))
-				usleep(1);
+			}
 		} else {
 			WD_ERR("do aead wcrypto_recv error!\n");
 			return ret;
diff --git a/v1/wd_cipher.c b/v1/wd_cipher.c
index 7df44e7..8bf71be 100644
--- a/v1/wd_cipher.c
+++ b/v1/wd_cipher.c
@@ -30,8 +30,7 @@
 #include "v1/wd_util.h"
 
 #define MAX_CIPHER_KEY_SIZE		64
-#define MAX_CIPHER_RETRY_CNT		2000000
-#define CIPHER_SLEEP_INTERVAL		0xf
+#define MAX_CIPHER_RETRY_CNT		20000000
 
 #define DES_KEY_SIZE 8
 #define SM4_KEY_SIZE 16
@@ -379,16 +378,18 @@ static int cipher_recv_sync(struct wcrypto_cipher_ctx *c_ctx,
 	while (true) {
 		ret = wd_burst_recv(c_ctx->q, (void **)(resp + recv_count),
 				    num - recv_count);
-		if (ret >= 0) {
+		if (ret > 0) {
 			recv_count += ret;
 			if (recv_count == num)
 				break;
 
-			if (++rx_cnt > MAX_CIPHER_RETRY_CNT)
+			rx_cnt = 0;
+		} else if (ret == 0) {
+			if (++rx_cnt > MAX_CIPHER_RETRY_CNT) {
+				WD_ERR("%s:wcrypto_recv timeout, num = %u, recv_count = %u!\n",
+					__func__, num, recv_count);
 				break;
-
-			if (!(rx_cnt & CIPHER_SLEEP_INTERVAL))
-				usleep(1);
+			}
 		} else {
 			WD_ERR("do cipher wcrypto_recv error!\n");
 			return ret;
diff --git a/v1/wd_digest.c b/v1/wd_digest.c
index aae4823..2179415 100644
--- a/v1/wd_digest.c
+++ b/v1/wd_digest.c
@@ -30,8 +30,7 @@
 #include "wd_util.h"
 
 #define MAX_HMAC_KEY_SIZE	128
-#define MAX_DIGEST_RETRY_CNT	2000000
-#define DIGEST_SLEEP_INTERVAL	0xf
+#define MAX_DIGEST_RETRY_CNT	20000000
 #define SEC_SHA1_ALIGN_SZ	64
 #define SEC_SHA512_ALIGN_SZ	128
 
@@ -275,16 +274,18 @@ static int digest_recv_sync(struct wcrypto_digest_ctx *d_ctx,
 	while (true) {
 		ret = wd_burst_recv(d_ctx->q, (void **)(resp + recv_count),
 				    num - recv_count);
-		if (ret >= 0) {
+		if (ret > 0) {
 			recv_count += ret;
 			if (recv_count == num)
 				break;
 
-			if (++rx_cnt > MAX_DIGEST_RETRY_CNT)
+			rx_cnt = 0;
+		} else if (ret == 0) {
+			if (++rx_cnt > MAX_DIGEST_RETRY_CNT) {
+				WD_ERR("%s:wcrypto_recv timeout, num = %u, recv_count = %u!\n",
+					__func__, num, recv_count);
 				break;
-
-			if (!(rx_cnt & DIGEST_SLEEP_INTERVAL))
-				usleep(1);
+			}
 		} else {
 			WD_ERR("do digest wcrypto_recv error!\n");
 			return ret;
-- 
2.25.1

