From: Zhushuai Yin <yinzhushuai@huawei.com> STATE_FAIL (0xFFFF) was locally defined in three files (aead/cipher/digest) and RSA/DH/ECC used hardcoded -1 for req.status. UADK_E_POLL_SUCCESS and UADK_E_POLL_FAIL were locally defined in both uadk_dh.c and uadk_rsa.c, while uadk_cipher.c and uadk_digest.c used hardcoded 0/-1 for the same purpose. This duplication makes it easy to miss the pre-set when adding new async paths. Move all four macros to uadk.h with documentation: - STATE_FAIL / STATUS_FAIL: async send error sentinels - UADK_E_POLL_SUCCESS / UADK_E_POLL_FAIL: async poll return values Remove duplicate local definitions from aead/cipher/digest/dh/rsa, and replace the hardcoded 0/-1 in cipher/digest/aead poll functions with the macros for consistency. Signed-off-by: Zhushuai Yin <yinzhushuai@huawei.com> --- src/uadk.h | 15 +++++++++++++++ src/uadk_aead.c | 3 +-- src/uadk_cipher.c | 6 ++---- src/uadk_dh.c | 4 +--- src/uadk_digest.c | 5 ++--- src/uadk_pkey.c | 2 +- src/uadk_rsa.c | 4 +--- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/uadk.h b/src/uadk.h index 2c52643..a6db774 100644 --- a/src/uadk.h +++ b/src/uadk.h @@ -29,6 +29,21 @@ #define UADK_INIT_FAIL 2 #define UADK_DEVICE_ERROR 3 +/* + * Async send error sentinel: pre-set before wd_*_async() so that + * callback-not-invoked cases (poll thread gives up) are caught by + * the existing req.state/status != 0 soft-fallback check. + * + * STATE_FAIL (0xFFFF) is for __u16 state fields (cipher/digest/aead). + * STATUS_FAIL (-1) is for int status fields (RSA/DH/ECC). + */ +#define STATE_FAIL 0xFFFF +#define STATUS_FAIL (-1) + +/* Async poll return values */ +#define UADK_E_POLL_SUCCESS 0 +#define UADK_E_POLL_FAIL (-1) + enum { HW_V2, HW_V3, diff --git a/src/uadk_aead.c b/src/uadk_aead.c index 20e0631..a026284 100644 --- a/src/uadk_aead.c +++ b/src/uadk_aead.c @@ -30,7 +30,6 @@ #define UADK_E_SUCCESS 1 #define UADK_E_FAIL 0 #define RET_FAIL (-1) -#define STATE_FAIL 0xFFFF #define CTX_SYNC_ENC 0 #define CTX_SYNC_DEC 1 #define CTX_ASYNC_ENC 2 @@ -114,7 +113,7 @@ static int uadk_e_aead_poll(void *ctx) do { ret = wd_aead_poll_ctx(idx, expt, &recv); if (!ret && recv == expt) - return 0; + return UADK_E_POLL_SUCCESS; else if (ret == -EAGAIN) rx_cnt++; else diff --git a/src/uadk_cipher.c b/src/uadk_cipher.c index 5c28d62..2b1537c 100644 --- a/src/uadk_cipher.c +++ b/src/uadk_cipher.c @@ -55,8 +55,6 @@ struct sw_cipher_t { const EVP_CIPHER *(*get_cipher)(void); }; -#define STATE_FAIL 0xFFFF - struct cipher_priv_ctx { handle_t sess; struct wd_cipher_sess_setup setup; @@ -323,11 +321,11 @@ static int uadk_e_cipher_poll(void *ctx) do { ret = wd_cipher_poll_ctx(idx, expt, &recv); if (!ret && recv == expt) - return 0; + return UADK_E_POLL_SUCCESS; else if (ret == -EAGAIN) rx_cnt++; else - return -1; + return UADK_E_POLL_FAIL; } while (rx_cnt < ENGINE_RECV_MAX_CNT); fprintf(stderr, "failed to recv msg: timeout!\n"); diff --git a/src/uadk_dh.c b/src/uadk_dh.c index dc8db21..ab8a305 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -48,8 +48,6 @@ #define UADK_DO_SOFT (-0xE0) #define UADK_E_SUCCESS 1 #define UADK_E_FAIL 0 -#define UADK_E_POLL_SUCCESS 0 -#define UADK_E_POLL_FAIL (-1) #define UADK_E_INIT_SUCCESS 0 #define ENV_ENABLED 1 #define KEY_GEN_BY_ENGINE 1 @@ -746,7 +744,7 @@ static int dh_do_async(struct uadk_dh_sess *dh_sess, struct async_op *op) cb_param->priv = &dh_sess->req; dh_sess->req.cb = uadk_e_dh_cb; dh_sess->req.cb_param = cb_param; - dh_sess->req.status = -1; + dh_sess->req.status = STATUS_FAIL; ret = async_get_free_task(&idx); if (!ret) goto free_cb_param; diff --git a/src/uadk_digest.c b/src/uadk_digest.c index 75cc5d9..29448b7 100644 --- a/src/uadk_digest.c +++ b/src/uadk_digest.c @@ -49,7 +49,6 @@ #define SHA_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT (512) #define MAX_DIGEST_LENGTH 64 #define DIGEST_BLOCK_SIZE (512 * 1024) -#define STATE_FAIL 0xFFFF /* * copied form openssl/include/internal/sm3.h @@ -403,11 +402,11 @@ static int uadk_e_digest_poll(void *ctx) do { ret = wd_digest_poll_ctx(CTX_ASYNC, expt, &recv); if (!ret && recv == expt) - return 0; + return UADK_E_POLL_SUCCESS; else if (ret == -EAGAIN) rx_cnt++; else - return -1; + return UADK_E_POLL_FAIL; } while (rx_cnt < ENGINE_RECV_MAX_CNT); fprintf(stderr, "failed to recv msg: timeout!\n"); diff --git a/src/uadk_pkey.c b/src/uadk_pkey.c index e6592e3..1fbee88 100644 --- a/src/uadk_pkey.c +++ b/src/uadk_pkey.c @@ -335,7 +335,7 @@ static int uadk_ecc_do_async(handle_t sess, struct wd_ecc_req *req, cb_param->priv = req; req->cb_param = cb_param; req->cb = uadk_e_ecc_cb; - req->status = -1; + req->status = STATUS_FAIL; ret = async_get_free_task(&idx); if (!ret) goto free_cb_param; diff --git a/src/uadk_rsa.c b/src/uadk_rsa.c index 8e5a3f1..8774d5d 100644 --- a/src/uadk_rsa.c +++ b/src/uadk_rsa.c @@ -49,8 +49,6 @@ #define UADK_E_SUCCESS 1 #define UADK_E_FAIL 0 #define UADK_DO_SOFT (-0xE0) -#define UADK_E_POLL_SUCCESS 0 -#define UADK_E_POLL_FAIL (-1) #define UADK_E_INIT_SUCCESS 0 #define CHECK_PADDING_FAIL (-1) #define ENV_ENABLED 1 @@ -1198,7 +1196,7 @@ static int rsa_do_async(struct uadk_rsa_sess *rsa_sess, struct async_op *op) cb_param->priv = &rsa_sess->req; rsa_sess->req.cb = uadk_e_rsa_cb; rsa_sess->req.cb_param = cb_param; - rsa_sess->req.status = -1; + rsa_sess->req.status = STATUS_FAIL; ret = async_get_free_task(&idx); if (!ret) goto free_cb_param; -- 2.43.0