From f01c1ddc862fc07ce247c8d64083918cf84009c8 Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Wed, 16 Mar 2022 18:55:28 +0800
Subject: [PATCH 107/109] uadk: parse epoll flag from environment variable

1.wd_set_epoll_en is used to parse epoll enable flag
from environment variable and store it to global variable.
2.When uadk works in synchronize mode, it will check the
global variable and decide whether to use wd_ctx_wait.
3.HPRE algorithm(ecc, rsa, dh) is also support epoll wait now.
4.hisi_qm_enable_interrupt is used to enable interrupt mode dynamically
when uadk send packets, but since we can init queue with fixed mode now,
it is not so efficient and can be replaced.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 docs/wd_environment_variable |  5 +++
 drv/hisi_comp.c              |  5 +--
 drv/hisi_hpre.c              |  3 ++
 drv/hisi_qm_udrv.c           | 21 ++----------
 drv/hisi_sec.c               | 15 ++-------
 include/drv/wd_aead_drv.h    |  2 --
 include/drv/wd_cipher_drv.h  |  2 --
 include/drv/wd_comp_drv.h    |  2 --
 include/drv/wd_digest_drv.h  |  2 --
 include/hisi_qm_udrv.h       |  4 +--
 include/wd_alg_common.h      |  3 ++
 include/wd_util.h            |  9 +++++
 wd_aead.c                    | 23 +++++--------
 wd_cipher.c                  | 23 +++++--------
 wd_comp.c                    | 26 +++++++--------
 wd_dh.c                      | 23 ++++++++-----
 wd_digest.c                  | 24 ++++++--------
 wd_ecc.c                     | 23 ++++++++-----
 wd_rsa.c                     | 23 ++++++++-----
 wd_sched.c                   |  2 +-
 wd_util.c                    | 64 ++++++++++++++++++++++++++++--------
 21 files changed, 163 insertions(+), 141 deletions(-)

diff --git a/docs/wd_environment_variable b/docs/wd_environment_variable
index 3ada5fb..8ac2da7 100644
--- a/docs/wd_environment_variable
+++ b/docs/wd_environment_variable
@@ -47,6 +47,11 @@ WD_<alg>_ASYNC_POLL_NUM
  WD_<alg>_ASYNC_POLL_NUM=2@0,4@2 means to configure 2 async polling threads in
  node0, and 4 polling threads in node2.
 
+WD_<alg>_EPOLL_EN
+ Define if wd_do_<alg>_sync enable wd_ctx_wait in WD. WD_<alg>_EPOLL_EN=1
+ means enable wd_ctx_wait in WD, driver will wait to receive the package until
+ hardware is done with package, otherwise driver will try to receive the package
+ directly after the package is sent.
 
 2. User model
 =============
diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c
index 860da43..9fdf3f1 100644
--- a/drv/hisi_comp.c
+++ b/drv/hisi_comp.c
@@ -780,6 +780,9 @@ static int hisi_zip_init(struct wd_ctx_config_internal *config, void *priv)
 		qm_priv.sqe_size = sizeof(struct hisi_zip_sqe);
 		qm_priv.op_type = config->ctxs[i].op_type;
 		qm_priv.qp_mode = config->ctxs[i].ctx_mode;
+		/* Setting the epoll en to 0 for ASYNC ctx */
+		qm_priv.epoll_en = (qm_priv.qp_mode == CTX_MODE_SYNC) ?
+				   config->epoll_en : 0;
 		qm_priv.idx = i;
 		h_qp = hisi_qm_alloc_qp(&qm_priv, h_ctx);
 		if (unlikely(!h_qp))
@@ -883,8 +886,6 @@ static int hisi_zip_comp_send(handle_t ctx, struct wd_comp_msg *msg, void *priv)
 		return ret;
 	}
 
-	hisi_qm_enable_interrupt(ctx, msg->is_polled);
-
 	return 0;
 }
 
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c
index d9546a6..7e14027 100644
--- a/drv/hisi_hpre.c
+++ b/drv/hisi_hpre.c
@@ -466,6 +466,9 @@ static int hpre_init(struct wd_ctx_config_internal *config, void *priv, const ch
 	for (i = 0; i < config->ctx_num; i++) {
 		h_ctx = config->ctxs[i].ctx;
 		qm_priv.qp_mode = config->ctxs[i].ctx_mode;
+		/* Setting the epoll en to 0 for ASYNC ctx */
+		qm_priv.epoll_en = (qm_priv.qp_mode == CTX_MODE_SYNC) ?
+				   config->epoll_en : 0;
 		qm_priv.idx = i;
 		h_qp = hisi_qm_alloc_qp(&qm_priv, h_ctx);
 		if (!h_qp) {
diff --git a/drv/hisi_qm_udrv.c b/drv/hisi_qm_udrv.c
index be06570..2c0d87c 100644
--- a/drv/hisi_qm_udrv.c
+++ b/drv/hisi_qm_udrv.c
@@ -309,6 +309,7 @@ static int hisi_qm_setup_info(struct hisi_qp *qp, struct hisi_qm_priv *config)
 	}
 
 	q_info->qp_mode = config->qp_mode;
+	q_info->epoll_en = config->epoll_en;
 	q_info->idx = config->idx;
 	q_info->sqe_size = config->sqe_size;
 	q_info->cqc_phase = 1;
@@ -479,7 +480,7 @@ static int hisi_qm_recv_single(struct hisi_qm_queue_info *q_info, void *resp)
 		i++;
 	}
 
-	q_info->db(q_info, QM_DBELL_CMD_CQ, i, 0);
+	q_info->db(q_info, QM_DBELL_CMD_CQ, i, q_info->epoll_en);
 
 	/* only support one thread poll one queue, so no need protect */
 	q_info->cq_head_index = i;
@@ -919,21 +920,3 @@ __u32 hisi_qm_get_list_size(struct wd_datalist *start_node,
 
 	return lits_size;
 }
-
-void hisi_qm_enable_interrupt(handle_t ctx, __u8 enable)
-{
-	struct hisi_qm_queue_info *q_info;
-	struct hisi_qp *qp;
-	handle_t h_qp;
-
-	if (!enable)
-		return;
-
-	h_qp = (handle_t)wd_ctx_get_priv(ctx);
-	qp = (struct hisi_qp *)h_qp;
-	q_info =  &qp->q_info;
-
-	pthread_spin_lock(&q_info->lock);
-	q_info->db(q_info, QM_DBELL_CMD_CQ, q_info->cq_head_index, 1);
-	pthread_spin_unlock(&q_info->lock);
-}
diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c
index aa86a6b..0732cdc 100644
--- a/drv/hisi_sec.c
+++ b/drv/hisi_sec.c
@@ -959,8 +959,6 @@ int hisi_sec_cipher_send(handle_t ctx, struct wd_cipher_msg *msg)
 		return ret;
 	}
 
-	hisi_qm_enable_interrupt(ctx, msg->is_polled);
-
 	return 0;
 }
 
@@ -1162,8 +1160,6 @@ int hisi_sec_cipher_send_v3(handle_t ctx, struct wd_cipher_msg *msg)
 		return ret;
 	}
 
-	hisi_qm_enable_interrupt(ctx, msg->is_polled);
-
 	return 0;
 }
 
@@ -1403,8 +1399,6 @@ int hisi_sec_digest_send(handle_t ctx, struct wd_digest_msg *msg)
 		goto put_sgl;
 	}
 
-	hisi_qm_enable_interrupt(ctx, msg->is_polled);
-
 	return 0;
 
 put_sgl:
@@ -1560,8 +1554,6 @@ int hisi_sec_digest_send_v3(handle_t ctx, struct wd_digest_msg *msg)
 		goto put_sgl;
 	}
 
-	hisi_qm_enable_interrupt(ctx, msg->is_polled);
-
 	return 0;
 
 put_sgl:
@@ -1913,8 +1905,6 @@ int hisi_sec_aead_send(handle_t ctx, struct wd_aead_msg *msg)
 		return ret;
 	}
 
-	hisi_qm_enable_interrupt(ctx, msg->is_polled);
-
 	return 0;
 }
 
@@ -2181,8 +2171,6 @@ int hisi_sec_aead_send_v3(handle_t ctx, struct wd_aead_msg *msg)
 		return ret;
 	}
 
-	hisi_qm_enable_interrupt(ctx, msg->is_polled);
-
 	return 0;
 }
 
@@ -2285,6 +2273,9 @@ int hisi_sec_init(struct wd_ctx_config_internal *config, void *priv)
 		/* setting the type is 0 for sqc_type */
 		qm_priv.op_type = 0;
 		qm_priv.qp_mode = config->ctxs[i].ctx_mode;
+		/* Setting the epoll en to 0 for ASYNC ctx */
+		qm_priv.epoll_en = (qm_priv.qp_mode == CTX_MODE_SYNC) ?
+				   config->epoll_en : 0;
 		qm_priv.idx = i;
 		h_qp = hisi_qm_alloc_qp(&qm_priv, h_ctx);
 		if (!h_qp)
diff --git a/include/drv/wd_aead_drv.h b/include/drv/wd_aead_drv.h
index 78a5673..137e70a 100644
--- a/include/drv/wd_aead_drv.h
+++ b/include/drv/wd_aead_drv.h
@@ -31,8 +31,6 @@ struct wd_aead_msg {
 	__u8 data_fmt;
 	/* Operation result, denoted by WD error code */
 	__u8 result;
-	/* epoll flag */
-	__u8 is_polled;
 
 	/* in bytes */
 	__u32 in_bytes;
diff --git a/include/drv/wd_cipher_drv.h b/include/drv/wd_cipher_drv.h
index e649869..87b77d3 100644
--- a/include/drv/wd_cipher_drv.h
+++ b/include/drv/wd_cipher_drv.h
@@ -28,8 +28,6 @@ struct wd_cipher_msg {
 	__u8 data_fmt;
 	/* Operation result, denoted by WD error code */
 	__u8 result;
-	/* epoll flag */
-	__u8 is_polled;
 
 	/* Key bytes */
 	__u16 key_bytes;
diff --git a/include/drv/wd_comp_drv.h b/include/drv/wd_comp_drv.h
index 8146a50..73403b5 100644
--- a/include/drv/wd_comp_drv.h
+++ b/include/drv/wd_comp_drv.h
@@ -50,8 +50,6 @@ struct wd_comp_msg {
 	__u32 checksum;
 	/* Request identifier */
 	__u32 tag;
-	/* Epoll flag */
-	__u8 is_polled;
 };
 
 struct wd_comp_driver {
diff --git a/include/drv/wd_digest_drv.h b/include/drv/wd_digest_drv.h
index fdde772..6907bc5 100644
--- a/include/drv/wd_digest_drv.h
+++ b/include/drv/wd_digest_drv.h
@@ -27,8 +27,6 @@ struct wd_digest_msg {
 	__u8 data_fmt;
 	/* Operation result, denoted by WD error code */
 	__u8 result;
-	/* epoll flag */
-	__u8 is_polled;
 	/* user identifier: struct wcrypto_cb_tag */
 	__u64 usr_data;
 
diff --git a/include/hisi_qm_udrv.h b/include/hisi_qm_udrv.h
index 707d0e3..68de837 100644
--- a/include/hisi_qm_udrv.h
+++ b/include/hisi_qm_udrv.h
@@ -47,6 +47,7 @@ struct hisi_qm_priv {
 	__u16 op_type;
 	/* index of ctxs */
 	__u32 idx;
+	bool epoll_en;
 };
 
 struct hisi_qm_queue_info {
@@ -71,6 +72,7 @@ struct hisi_qm_queue_info {
 	bool cqc_phase;
 	pthread_spinlock_t lock;
 	unsigned long region_size[UACCE_QFRT_MAX];
+	bool epoll_en;
 };
 
 struct hisi_qp {
@@ -182,8 +184,6 @@ int hisi_qm_get_free_sqe_num(handle_t h_qp);
 __u32 hisi_qm_get_list_size(struct wd_datalist *start_node,
 			    struct wd_datalist *end_node);
 
-void hisi_qm_enable_interrupt(handle_t ctx, __u8 enable);
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h
index 94336f0..30d244f 100644
--- a/include/wd_alg_common.h
+++ b/include/wd_alg_common.h
@@ -9,6 +9,7 @@
 
 #include <asm/types.h>
 #include <pthread.h>
+#include <stdbool.h>
 #include "wd.h"
 #include "wd_common.h"
 
@@ -27,6 +28,7 @@ extern "C" {
 #define ARRAY_SIZE(x)		(sizeof(x) / sizeof((x)[0]))
 #define MAX_STR_LEN		256
 #define CTX_TYPE_INVALID	9999
+#define POLL_TIME		1000
 
 enum wd_ctx_mode {
 	CTX_MODE_SYNC = 0,
@@ -74,6 +76,7 @@ struct wd_ctx_config_internal {
 	struct wd_ctx_internal *ctxs;
 	void *priv;
 	int pid;
+	bool epoll_en;
 };
 
 /**
diff --git a/include/wd_util.h b/include/wd_util.h
index 2d3c1e4..a41b4c9 100644
--- a/include/wd_util.h
+++ b/include/wd_util.h
@@ -314,6 +314,15 @@ int wd_set_ctx_attr(struct wd_ctx_attr *ctx_attr,
  */
 int wd_check_ctx(struct wd_ctx_config_internal *config, __u8 mode, __u32 idx);
 
+/**
+ * wd_set_epoll_en() - set epoll enable flag from environment variable value.
+ * @var_name: Environment variable name string.
+ * @epoll_en: epoll enable flag.
+ *
+ * Return 0 if the value is 0 or 1, otherwise return -WD_EINVAL.
+ */
+int wd_set_epoll_en(const char *var_name, bool *epoll_en);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/wd_aead.c b/wd_aead.c
index 74047b5..2e006c3 100644
--- a/wd_aead.c
+++ b/wd_aead.c
@@ -21,8 +21,6 @@
 #define WD_POOL_MAX_ENTRIES	1024
 #define MAX_RETRY_COUNTS	200000000
 
-#define POLL_SIZE		70000
-#define POLL_TIME		1000
 
 static int g_aead_mac_len[WD_DIGEST_TYPE_MAX] = {
 	WD_DIGEST_SM3_LEN, WD_DIGEST_MD5_LEN, WD_DIGEST_SHA1_LEN,
@@ -414,17 +412,18 @@ int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	if (ret)
 		return ret;
 
+	ret = wd_set_epoll_en("WD_AEAD_EPOLL_EN",
+			      &wd_aead_setting.config.epoll_en);
+	if (ret < 0)
+		return ret;
+
 	ret = wd_init_ctx_config(&wd_aead_setting.config, config);
-	if (ret) {
-		WD_ERR("failed to set config, ret = %d\n", ret);
+	if (ret)
 		return ret;
-	}
 
 	ret = wd_init_sched(&wd_aead_setting.sched, sched);
-	if (ret < 0) {
-		WD_ERR("failed to set sched, ret = %d\n", ret);
+	if (ret < 0)
 		goto out;
-	}
 
 	/* set driver */
 #ifdef WD_STATIC_DRV
@@ -435,10 +434,8 @@ int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	ret = wd_init_async_request_pool(&wd_aead_setting.pool,
 				config->ctx_num, WD_POOL_MAX_ENTRIES,
 				sizeof(struct wd_aead_msg));
-	if (ret < 0) {
-		WD_ERR("failed to init aead aysnc request pool.\n");
+	if (ret < 0)
 		goto out_sched;
-	}
 
 	/* init ctx related resources in specific driver */
 	priv = calloc(1, wd_aead_setting.driver->drv_ctx_size);
@@ -525,7 +522,7 @@ static int send_recv_sync(struct wd_ctx_internal *ctx,
 	}
 
 	do {
-		if (msg->is_polled) {
+		if (wd_aead_setting.config.epoll_en) {
 			ret = wd_ctx_wait(ctx->ctx, POLL_TIME);
 			if (unlikely(ret < 0))
 				WD_ERR("wd aead ctx wait timeout(%d)!\n", ret);
@@ -563,7 +560,6 @@ int wd_do_aead_sync(handle_t h_sess, struct wd_aead_req *req)
 
 	memset(&msg, 0, sizeof(struct wd_aead_msg));
 	fill_request_msg(&msg, req, sess);
-	msg.is_polled = (req->in_bytes >= POLL_SIZE);
 	req->state = 0;
 
 	idx = wd_aead_setting.sched.pick_next_ctx(
@@ -616,7 +612,6 @@ int wd_do_aead_async(handle_t h_sess, struct wd_aead_req *req)
 
 	fill_request_msg(msg, req, sess);
 	msg->tag = msg_id;
-	msg->is_polled = 0;
 
 	ret = wd_aead_setting.driver->aead_send(ctx->ctx, msg);
 	if (unlikely(ret < 0)) {
diff --git a/wd_cipher.c b/wd_cipher.c
index 563eece..6d286f9 100644
--- a/wd_cipher.c
+++ b/wd_cipher.c
@@ -22,8 +22,6 @@
 #define DES_WEAK_KEY_NUM	16
 #define MAX_RETRY_COUNTS	200000000
 
-#define POLL_SIZE		100000
-#define POLL_TIME		1000
 
 static const unsigned char des_weak_keys[DES_WEAK_KEY_NUM][DES_KEY_SIZE] = {
 	/* weak keys */
@@ -255,17 +253,18 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	if (ret)
 		return ret;
 
+	ret = wd_set_epoll_en("WD_CIPHER_EPOLL_EN",
+			      &wd_cipher_setting.config.epoll_en);
+	if (ret < 0)
+		return ret;
+
 	ret = wd_init_ctx_config(&wd_cipher_setting.config, config);
-	if (ret < 0) {
-		WD_ERR("failed to set config, ret = %d!\n", ret);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = wd_init_sched(&wd_cipher_setting.sched, sched);
-	if (ret < 0) {
-		WD_ERR("failed to set sched, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out;
-	}
 
 #ifdef WD_STATIC_DRV
 	/* set driver */
@@ -276,10 +275,8 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	ret = wd_init_async_request_pool(&wd_cipher_setting.pool,
 					 config->ctx_num, WD_POOL_MAX_ENTRIES,
 					 sizeof(struct wd_cipher_msg));
-	if (ret < 0) {
-		WD_ERR("failed to init req pool, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out_sched;
-	}
 
 	/* init ctx related resources in specific driver */
 	priv = calloc(1, wd_cipher_setting.driver->drv_ctx_size);
@@ -436,7 +433,7 @@ static int send_recv_sync(struct wd_ctx_internal *ctx,
 	}
 
 	do {
-		if (msg->is_polled) {
+		if (wd_cipher_setting.config.epoll_en) {
 			ret = wd_ctx_wait(ctx->ctx, POLL_TIME);
 			if (unlikely(ret < 0))
 				WD_ERR("wd cipher ctx wait timeout(%d)!\n", ret);
@@ -476,7 +473,6 @@ int wd_do_cipher_sync(handle_t h_sess, struct wd_cipher_req *req)
 
 	memset(&msg, 0, sizeof(struct wd_cipher_msg));
 	fill_request_msg(&msg, req, sess);
-	msg.is_polled = (req->in_bytes >= POLL_SIZE);
 	req->state = 0;
 
 	idx = wd_cipher_setting.sched.pick_next_ctx(
@@ -526,7 +522,6 @@ int wd_do_cipher_async(handle_t h_sess, struct wd_cipher_req *req)
 
 	fill_request_msg(msg, req, sess);
 	msg->tag = msg_id;
-	msg->is_polled = 0;
 
 	ret = wd_cipher_setting.driver->cipher_send(ctx->ctx, msg);
 	if (unlikely(ret < 0)) {
diff --git a/wd_comp.c b/wd_comp.c
index 9a71dfc..3bd7f43 100644
--- a/wd_comp.c
+++ b/wd_comp.c
@@ -22,8 +22,6 @@
 #define HW_CTX_SIZE			(64 * 1024)
 #define STREAM_CHUNK			(128 * 1024)
 
-#define POLL_SIZE			250000
-#define POLL_TIME			1000
 
 #define swap_byte(x) \
 	((((x) & 0x000000ff) << 24) | \
@@ -97,16 +95,18 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched)
 		return -WD_EINVAL;
 	}
 
+	ret = wd_set_epoll_en("WD_COMP_EPOLL_EN",
+			      &wd_comp_setting.config.epoll_en);
+	if (ret < 0)
+		return ret;
+
 	ret = wd_init_ctx_config(&wd_comp_setting.config, config);
-	if (ret < 0) {
-		WD_ERR("failed to set config, ret = %d!\n", ret);
+	if (ret < 0)
 		return ret;
-	}
+
 	ret = wd_init_sched(&wd_comp_setting.sched, sched);
-	if (ret < 0) {
-		WD_ERR("failed to set sched, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out;
-	}
 	/*
 	 * Fix me: ctx could be passed into wd_comp_set_static_drv to help to
 	 * choose static compiled vendor driver. For dynamic vendor driver,
@@ -125,10 +125,9 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	ret = wd_init_async_request_pool(&wd_comp_setting.pool,
 					 config->ctx_num, WD_POOL_MAX_ENTRIES,
 					 sizeof(struct wd_comp_msg));
-	if (ret < 0) {
-		WD_ERR("failed to init req pool, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out_sched;
-	}
+
 	/* init ctx related resources in specific driver */
 	priv = calloc(1, wd_comp_setting.driver->drv_ctx_size);
 	if (!priv) {
@@ -429,7 +428,7 @@ static int wd_comp_sync_job(struct wd_comp_sess *sess,
 	}
 
 	do {
-		if (msg->is_polled) {
+		if (config->epoll_en) {
 			ret = wd_ctx_wait(ctx->ctx, POLL_TIME);
 			if (unlikely(ret < 0))
 				WD_ERR("wd ctx wait timeout, ret = %d!\n", ret);
@@ -472,7 +471,6 @@ int wd_do_comp_sync(handle_t h_sess, struct wd_comp_req *req)
 
 	fill_comp_msg(sess, &msg, req);
 	msg.ctx_buf = sess->ctx_buf;
-	msg.is_polled = (req->src_len >= POLL_SIZE);
 	msg.stream_mode = WD_COMP_STATELESS;
 
 	ret = wd_comp_sync_job(sess, req, &msg);
@@ -643,7 +641,6 @@ int wd_do_comp_strm(handle_t h_sess, struct wd_comp_req *req)
 	/* fill true flag */
 	msg.req.last = req->last;
 	msg.stream_mode = WD_COMP_STATEFUL;
-	msg.is_polled = (req->src_len >= POLL_SIZE);
 
 	src_len = req->src_len;
 
@@ -701,7 +698,6 @@ int wd_do_comp_async(handle_t h_sess, struct wd_comp_req *req)
 	fill_comp_msg(sess, msg, req);
 	msg->tag = tag;
 	msg->stream_mode = WD_COMP_STATELESS;
-	msg->is_polled = 0;
 
 	pthread_spin_lock(&ctx->lock);
 
diff --git a/wd_dh.c b/wd_dh.c
index c0d3e00..aaea812 100644
--- a/wd_dh.c
+++ b/wd_dh.c
@@ -101,17 +101,18 @@ int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	if (param_check(config, sched))
 		return -WD_EINVAL;
 
+	ret = wd_set_epoll_en("WD_DH_EPOLL_EN",
+			      &wd_dh_setting.config.epoll_en);
+	if (ret < 0)
+		return ret;
+
 	ret = wd_init_ctx_config(&wd_dh_setting.config, config);
-	if (ret) {
-		WD_ERR("failed to initialize ctx config, ret = %d!\n", ret);
+	if (ret)
 		return ret;
-	}
 
 	ret = wd_init_sched(&wd_dh_setting.sched, sched);
-	if (ret) {
-		WD_ERR("failed to initialize sched, ret = %d!\n", ret);
+	if (ret)
 		goto out;
-	}
 
 #ifdef WD_STATIC_DRV
 	wd_dh_set_static_drv();
@@ -121,10 +122,8 @@ int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	ret = wd_init_async_request_pool(&wd_dh_setting.pool,
 					 config->ctx_num, WD_POOL_MAX_ENTRIES,
 					 sizeof(struct wd_dh_msg));
-	if (ret) {
-		WD_ERR("failed to initialize async req pool, ret = %d!\n", ret);
+	if (ret)
 		goto out_sched;
-	}
 
 	/* initialize ctx related resources in specific driver */
 	priv = calloc(1, wd_dh_setting.driver->drv_ctx_size);
@@ -236,6 +235,12 @@ static int dh_recv_sync(handle_t ctx, struct wd_dh_msg *msg)
 	int ret;
 
 	do {
+		if (wd_dh_setting.config.epoll_en) {
+			ret = wd_ctx_wait(ctx, POLL_TIME);
+			if (ret < 0)
+				WD_ERR("wd ctx wait timeout(%d)!\n", ret);
+		}
+
 		ret = wd_dh_setting.driver->recv(ctx, msg);
 		if (ret == -WD_EAGAIN) {
 			if (rx_cnt++ >= DH_RECV_MAX_CNT) {
diff --git a/wd_digest.c b/wd_digest.c
index 0ddc074..7afbd9c 100644
--- a/wd_digest.c
+++ b/wd_digest.c
@@ -19,8 +19,6 @@
 #define DES_WEAK_KEY_NUM	4
 #define MAX_RETRY_COUNTS	200000000
 
-#define POLL_SIZE		100000
-#define POLL_TIME		1000
 
 static int g_digest_mac_len[WD_DIGEST_TYPE_MAX] = {
 	WD_DIGEST_SM3_LEN, WD_DIGEST_MD5_LEN, WD_DIGEST_SHA1_LEN,
@@ -28,6 +26,7 @@ static int g_digest_mac_len[WD_DIGEST_TYPE_MAX] = {
 	WD_DIGEST_SHA384_LEN, WD_DIGEST_SHA512_LEN,
 	WD_DIGEST_SHA512_224_LEN, WD_DIGEST_SHA512_256_LEN
 };
+
 struct wd_digest_setting {
 	struct wd_ctx_config_internal config;
 	struct wd_sched	sched;
@@ -175,17 +174,18 @@ int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	if (ret)
 		return ret;
 
+	ret = wd_set_epoll_en("WD_DIGEST_EPOLL_EN",
+			      &wd_digest_setting.config.epoll_en);
+	if (ret < 0)
+		return ret;
+
 	ret = wd_init_ctx_config(&wd_digest_setting.config, config);
-	if (ret < 0) {
-		WD_ERR("failed to set config, ret = %d!\n", ret);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = wd_init_sched(&wd_digest_setting.sched, sched);
-	if (ret < 0) {
-		WD_ERR("failed to set sched, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out;
-	}
 
 	/* set driver */
 #ifdef WD_STATIC_DRV
@@ -196,10 +196,8 @@ int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	ret = wd_init_async_request_pool(&wd_digest_setting.pool,
 					 config->ctx_num, WD_POOL_MAX_ENTRIES,
 					 sizeof(struct wd_digest_msg));
-	if (ret < 0) {
-		WD_ERR("failed to init req pool, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out_sched;
-	}
 
 	/* init ctx related resources in specific driver */
 	priv = calloc(1, wd_digest_setting.driver->drv_ctx_size);
@@ -322,7 +320,7 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, struct wd_digest_sess *ds
 	}
 
 	do {
-		if (msg->is_polled) {
+		if (wd_digest_setting.config.epoll_en) {
 			ret = wd_ctx_wait(ctx->ctx, POLL_TIME);
 			if (unlikely(ret < 0))
 				WD_ERR("wd digest ctx wait timeout(%d)!\n", ret);
@@ -367,7 +365,6 @@ int wd_do_digest_sync(handle_t h_sess, struct wd_digest_req *req)
 
 	memset(&msg, 0, sizeof(struct wd_digest_msg));
 	fill_request_msg(&msg, req, dsess);
-	msg.is_polled = (req->in_bytes >= POLL_SIZE);
 	req->state = 0;
 
 	idx = wd_digest_setting.sched.pick_next_ctx(
@@ -420,7 +417,6 @@ int wd_do_digest_async(handle_t h_sess, struct wd_digest_req *req)
 
 	fill_request_msg(msg, req, dsess);
 	msg->tag = msg_id;
-	msg->is_polled = 0;
 
 	ret = wd_digest_setting.driver->digest_send(ctx->ctx, msg);
 	if (unlikely(ret < 0)) {
diff --git a/wd_ecc.c b/wd_ecc.c
index af62b9a..a6c2209 100644
--- a/wd_ecc.c
+++ b/wd_ecc.c
@@ -155,17 +155,18 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	if (init_param_check(config, sched))
 		return -WD_EINVAL;
 
+	ret = wd_set_epoll_en("WD_ECC_EPOLL_EN",
+			      &wd_ecc_setting.config.epoll_en);
+	if (ret < 0)
+		return ret;
+
 	ret = wd_init_ctx_config(&wd_ecc_setting.config, config);
-	if (ret < 0) {
-		WD_ERR("failed to set config, ret = %d!\n", ret);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = wd_init_sched(&wd_ecc_setting.sched, sched);
-	if (ret < 0) {
-		WD_ERR("failed to set sched, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out;
-	}
 
 #ifdef WD_STATIC_DRV
 	wd_ecc_set_static_drv();
@@ -175,10 +176,8 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	ret = wd_init_async_request_pool(&wd_ecc_setting.pool,
 					 config->ctx_num, WD_POOL_MAX_ENTRIES,
 					 sizeof(struct wd_ecc_msg));
-	if (ret < 0) {
-		WD_ERR("failed to initialize async req pool, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out_sched;
-	}
 
 	/* initialize ctx related resources in specific driver */
 	priv = calloc(1, wd_ecc_setting.driver->drv_ctx_size);
@@ -1435,6 +1434,12 @@ static int ecc_recv_sync(handle_t ctx, struct wd_ecc_msg *msg)
 	int ret;
 
 	do {
+		if (wd_ecc_setting.config.epoll_en) {
+			ret = wd_ctx_wait(ctx, POLL_TIME);
+			if (ret < 0)
+				WD_ERR("wd ctx wait timeout(%d)!\n", ret);
+		}
+
 		ret = wd_ecc_setting.driver->recv(ctx, msg);
 		if (ret == -WD_EAGAIN) {
 			if (rx_cnt++ >= ECC_RECV_MAX_CNT) {
diff --git a/wd_rsa.c b/wd_rsa.c
index 8371475..ca39cff 100644
--- a/wd_rsa.c
+++ b/wd_rsa.c
@@ -141,17 +141,18 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	if (param_check(config, sched))
 		return -WD_EINVAL;
 
+	ret = wd_set_epoll_en("WD_RSA_EPOLL_EN",
+			      &wd_rsa_setting.config.epoll_en);
+	if (ret < 0)
+		return ret;
+
 	ret = wd_init_ctx_config(&wd_rsa_setting.config, config);
-	if (ret < 0) {
-		WD_ERR("failed to set config, ret = %d!\n", ret);
+	if (ret < 0)
 		return ret;
-	}
 
 	ret = wd_init_sched(&wd_rsa_setting.sched, sched);
-	if (ret < 0) {
-		WD_ERR("failed to set sched, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out;
-	}
 
 #ifdef WD_STATIC_DRV
 	wd_rsa_set_static_drv();
@@ -161,10 +162,8 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	ret = wd_init_async_request_pool(&wd_rsa_setting.pool,
 					 config->ctx_num, WD_POOL_MAX_ENTRIES,
 					 sizeof(struct wd_rsa_msg));
-	if (ret < 0) {
-		WD_ERR("failed to initialize async req pool, ret = %d!\n", ret);
+	if (ret < 0)
 		goto out_sched;
-	}
 
 	/* initialize ctx related resources in specific driver */
 	priv = calloc(1, wd_rsa_setting.driver->drv_ctx_size);
@@ -296,6 +295,12 @@ static int rsa_recv_sync(handle_t ctx, struct wd_rsa_msg *msg)
 	int ret;
 
 	do {
+		if (wd_rsa_setting.config.epoll_en) {
+			ret = wd_ctx_wait(ctx, POLL_TIME);
+			if (ret < 0)
+				WD_ERR("wd ctx wait timeout(%d)!\n", ret);
+		}
+
 		ret = wd_rsa_setting.driver->recv(ctx, msg);
 		if (ret == -WD_EAGAIN) {
 			if (rx_cnt++ >= RSA_RECV_MAX_CNT) {
diff --git a/wd_sched.c b/wd_sched.c
index 9b1998c..dfd390b 100644
--- a/wd_sched.c
+++ b/wd_sched.c
@@ -213,7 +213,7 @@ static int session_sched_poll_policy(handle_t sched_ctx,
 	}
 
 	if (ctx->numa_num > NUMA_NUM_NODES) {
-		WD_ERR("invalid: ctx's numa number is %d!\n", ctx->numa_num);
+		WD_ERR("invalid: ctx's numa number is %u!\n", ctx->numa_num);
 		return -WD_EINVAL;
 	}
 
diff --git a/wd_util.c b/wd_util.c
index 03316b6..57dafa7 100644
--- a/wd_util.c
+++ b/wd_util.c
@@ -85,8 +85,10 @@ int wd_init_ctx_config(struct wd_ctx_config_internal *in,
 	}
 
 	ctxs = calloc(1, cfg->ctx_num * sizeof(struct wd_ctx_internal));
-	if (!ctxs)
+	if (!ctxs) {
+		WD_ERR("failed to alloc memory for internal ctxs!\n");
 		return -WD_ENOMEM;
+	}
 
 	for (i = 0; i < cfg->ctx_num; i++) {
 		if (!cfg->ctxs[i].ctx) {
@@ -115,8 +117,10 @@ int wd_init_ctx_config(struct wd_ctx_config_internal *in,
 int wd_init_sched(struct wd_sched *in, struct wd_sched *from)
 {
 	if (!from->name || !from->sched_init ||
-	    !from->pick_next_ctx || !from->poll_policy)
+	    !from->pick_next_ctx || !from->poll_policy) {
+		WD_ERR("invalid: member of wd_sched is NULL!\n");
 		return -WD_EINVAL;
+	}
 
 	in->h_sched_ctx = from->h_sched_ctx;
 	in->name = strdup(from->name);
@@ -170,13 +174,16 @@ void wd_memset_zero(void *data, __u32 size)
 static int init_msg_pool(struct msg_pool *pool, __u32 msg_num, __u32 msg_size)
 {
 	pool->msgs = calloc(1, msg_num * msg_size);
-	if (!pool->msgs)
+	if (!pool->msgs) {
+		WD_ERR("failed to alloc memory for msgs arrary of msg pool!\n");
 		return -WD_ENOMEM;
+	}
 
 	pool->used = calloc(1, msg_num * sizeof(int));
 	if (!pool->used) {
 		free(pool->msgs);
 		pool->msgs = NULL;
+		WD_ERR("failed to alloc memory for used arrary of msg pool!\n");
 		return -WD_ENOMEM;
 	}
 
@@ -204,8 +211,10 @@ int wd_init_async_request_pool(struct wd_async_msg_pool *pool, __u32 pool_num,
 	pool->pool_num = pool_num;
 
 	pool->pools = calloc(1, pool->pool_num * sizeof(struct msg_pool));
-	if (!pool->pools)
+	if (!pool->pools) {
+		WD_ERR("failed to alloc memory for async msg pools!\n");
 		return -WD_ENOMEM;
+	}
 
 	for (i = 0; i < pool->pool_num; i++) {
 		ret = init_msg_pool(&pool->pools[i], msg_num, msg_size);
@@ -510,27 +519,33 @@ static int is_number(const char *str)
 	return 1;
 }
 
-/* 1 enable, 0 disable, others error */
-int wd_parse_async_poll_en(struct wd_env_config *config, const char *s)
+static int str_to_bool(const char *s, bool *target)
 {
 	int tmp;
 
-	if (!is_number(s)) {
-		WD_ERR("invalid: async poll en flag is %s!\n", s);
+	if (!is_number(s))
 		return -WD_EINVAL;
-	}
 
 	tmp = strtol(s, NULL, 10);
-	if (tmp != 0 && tmp != 1) {
-		WD_ERR("invalid: async poll en flag is not 0 or 1!\n");
+	if (tmp != 0 && tmp != 1)
 		return -WD_EINVAL;
-	}
 
-	config->enable_internal_poll = tmp;
+	*target = tmp;
 
 	return 0;
 }
 
+int wd_parse_async_poll_en(struct wd_env_config *config, const char *s)
+{
+	int ret;
+
+	ret = str_to_bool(s, &config->enable_internal_poll);
+	if (ret)
+		WD_ERR("failed to parse async poll enable flag(%s)!\n", s);
+
+	return ret;
+}
+
 static int parse_num_on_numa(const char *s, int *num, int *node)
 {
 	char *sep, *start, *left;
@@ -1589,3 +1604,26 @@ int wd_check_ctx(struct wd_ctx_config_internal *config, __u8 mode, __u32 idx)
 
 	return 0;
 }
+
+int wd_set_epoll_en(const char *var_name, bool *epoll_en)
+{
+	const char *s;
+	int ret;
+
+	s = secure_getenv(var_name);
+	if (!s || !strlen(s)) {
+		*epoll_en = 0;
+		return 0;
+	}
+
+	ret = str_to_bool(s, epoll_en);
+	if (ret) {
+		WD_ERR("failed to parse %s!\n", var_name);
+		return ret;
+	}
+
+	if (*epoll_en)
+		WD_ERR("epoll wait is enabled!\n");
+
+	return 0;
+}
-- 
2.27.0

