From 50b68c0405544702181deb9dda28381169b17d09 Mon Sep 17 00:00:00 2001
From: Junchong Pan <panjunchong@hisilicon.com>
Date: Fri, 4 Mar 2022 02:25:54 +0000
Subject: [PATCH 081/109] uadk: cleanup hpre code

Includes:
1.Remove useless interface param.
2.Remove ASSERT.
3.Remove print address.

Signed-off-by: Junchong Pan <panjunchong@hisilicon.com>
---
 drv/hisi_hpre.c         | 26 +++++++++++++++-------
 drv/hisi_qm_udrv.c      |  2 --
 v1/drv/hisi_hpre_udrv.c | 48 ++++++++++++++++++++++++++++-------------
 v1/wd_ecc.c             | 24 ++++++++++-----------
 wd_ecc.c                | 24 ++++++++++-----------
 5 files changed, 75 insertions(+), 49 deletions(-)

diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c
index fbb38e0..f1d7720 100644
--- a/drv/hisi_hpre.c
+++ b/drv/hisi_hpre.c
@@ -127,7 +127,7 @@ static bool is_hpre_bin_fmt(const char *data, int dsz, int bsz)
 }
 
 static int crypto_bin_to_hpre_bin(char *dst, const char *src,
-				  int b_size, int d_size, const char *p_name)
+				  __u32 b_size, __u32 d_size, const char *p_name)
 {
 	int i = d_size - 1;
 	bool is_hpre_bin;
@@ -1469,6 +1469,11 @@ static struct wd_ecc_out *create_ecdh_out(struct wd_ecc_msg *msg)
 	struct wd_ecc_dh_out *dh_out;
 	struct wd_ecc_out *out;
 
+	if (!hsz) {
+		WD_ERR("get msg key size error!\n");
+		return NULL;
+	}
+
 	out = malloc(len);
 	if (!out) {
 		WD_ERR("failed to alloc, sz = %u!\n", len);
@@ -1601,6 +1606,11 @@ static int ecc_fill(struct wd_ecc_msg *msg, struct hisi_hpre_sqe *hw_msg)
 		return -WD_EINVAL;
 	}
 
+	if (!hw_sz) {
+		WD_ERR("get msg key size error!\n");
+		return -WD_EINVAL;
+	}
+
 	memset(hw_msg, 0, sizeof(*hw_msg));
 
 	/* prepare algorithm */
@@ -1646,7 +1656,7 @@ static int sm2_enc_send(handle_t ctx, struct wd_ecc_msg *msg)
 	handle_t h_qp = (handle_t)wd_ctx_get_priv(ctx);
 	struct wd_sm2_enc_in *ein = msg->req.src;
 	struct wd_ecc_msg *msg_dst[2] = {NULL};
-	struct hisi_hpre_sqe hw_msg[2] = {{0}};
+	struct hisi_hpre_sqe hw_msg[2] = {0};
 	struct wd_hash_mt *hash = &msg->hash;
 	__u16 send_cnt = 0;
 	int ret;
@@ -1918,7 +1928,7 @@ static __u32 get_hash_bytes(__u8 type)
 	return val;
 }
 
-static void msg_pack(char *dst, __u64 dst_len, __u64 *out_len,
+static void msg_pack(char *dst, __u64 *out_len,
 		     const void *src, __u32 src_len)
 {
 	if (unlikely(!src || !src_len))
@@ -1961,8 +1971,8 @@ static int sm2_kdf(struct wd_dtb *out, struct wd_ecc_point *x2y2,
 		ctr[2] = (i >> 8) & 0xFF;
 		ctr[1] = (i >> 16) & 0xFF;
 		ctr[0] = (i >> 24) & 0xFF;
-		msg_pack(p_in, lens, &in_len, x2y2->x.data, x2y2_len);
-		msg_pack(p_in, lens, &in_len, ctr, sizeof(ctr));
+		msg_pack(p_in, &in_len, x2y2->x.data, x2y2_len);
+		msg_pack(p_in, &in_len, ctr, sizeof(ctr));
 
 		t_out = m_len >= h_bytes ? tmp : p_out;
 		ret = hash->cb(p_in, in_len, t_out, h_bytes, hash->usr);
@@ -2026,9 +2036,9 @@ static int sm2_hash(struct wd_dtb *out, struct wd_ecc_point *x2y2,
 	if (unlikely(!p_in))
 		return -WD_ENOMEM;
 
-	msg_pack(p_in, lens, &in_len, x2y2->x.data, x2y2->x.dsize);
-	msg_pack(p_in, lens, &in_len, msg->data, msg->dsize);
-	msg_pack(p_in, lens, &in_len, x2y2->y.data, x2y2->y.dsize);
+	msg_pack(p_in, &in_len, x2y2->x.data, x2y2->x.dsize);
+	msg_pack(p_in, &in_len, msg->data, msg->dsize);
+	msg_pack(p_in, &in_len, x2y2->y.data, x2y2->y.dsize);
 	ret = hash->cb(p_in, in_len, hash_out, h_bytes, hash->usr);
 	if (unlikely(ret)) {
 		WD_ERR("failed to hash cb, ret = %d!\n", ret);
diff --git a/drv/hisi_qm_udrv.c b/drv/hisi_qm_udrv.c
index 845fa46..2e692a1 100644
--- a/drv/hisi_qm_udrv.c
+++ b/drv/hisi_qm_udrv.c
@@ -896,8 +896,6 @@ void hisi_qm_dump_sgl(void *sgl)
 	int i;
 
 	while (tmp) {
-		WD_ERR("sgl = %p\n", tmp);
-		WD_ERR("sgl->next_dma : 0x%lx\n", tmp->next_dma);
 		WD_ERR("sgl->entry_sum_in_chain : %u\n",
 		       tmp->entry_sum_in_chain);
 		WD_ERR("sgl->entry_sum_in_sgl : %u\n",
diff --git a/v1/drv/hisi_hpre_udrv.c b/v1/drv/hisi_hpre_udrv.c
index c6ca0ad..ba32114 100644
--- a/v1/drv/hisi_hpre_udrv.c
+++ b/v1/drv/hisi_hpre_udrv.c
@@ -499,7 +499,6 @@ int qm_fill_rsa_sqe(void *message, struct qm_queue_info *info, __u16 i)
 		hw_msg->low_tag = tag->ctx_id;
 	hw_msg->done = 0x1;
 	hw_msg->etype = 0x0;
-	ASSERT(!info->req_cache[i]);
 	info->req_cache[i] = msg;
 
 	return WD_SUCCESS;
@@ -517,7 +516,10 @@ int qm_parse_rsa_sqe(void *msg, const struct qm_queue_info *info,
 	__u16 kbytes;
 	int ret;
 
-	ASSERT(rsa_msg);
+	if (unlikely(!rsa_msg)) {
+		WD_ERR("info->req_cache is null at index:%hu\n", i);
+		return 0;
+	}
 
 	/* if this hardware message not belong to me, then try again */
 	if (usr && LOW_U16(hw_msg->low_tag) != usr)
@@ -700,7 +702,6 @@ int qm_fill_dh_sqe(void *message, struct qm_queue_info *info, __u16 i)
 		if (unlikely(ret))
 			goto map_xp_fail;
 	}
-	ASSERT(!info->req_cache[i]);
 	info->req_cache[i] = msg;
 
 	ret = qm_final_fill_dh_sqe(q, msg, hw_msg);
@@ -726,7 +727,11 @@ int qm_parse_dh_sqe(void *msg, const struct qm_queue_info *info,
 	struct wd_queue *q = info->q;
 	int ret;
 
-	ASSERT(dh_msg);
+	if (unlikely(!dh_msg)) {
+		WD_ERR("info->req_cache is null at index:%hu\n", i);
+		return 0;
+	}
+
 	if (usr && LOW_U16(hw_msg->low_tag) != usr)
 		return 0;
 	if (hw_msg->done != HPRE_HW_TASK_DONE || hw_msg->etype) {
@@ -1668,7 +1673,6 @@ static int qm_fill_ecc_sqe_general(void *message, struct qm_queue_info *info,
 		hw_msg->low_tag = tag->ctx_id;
 	hw_msg->done = 0x1;
 	hw_msg->etype = 0x0;
-	ASSERT(!info->req_cache[i]);
 	info->req_cache[i] = msg;
 
 	return WD_SUCCESS;
@@ -1942,6 +1946,7 @@ static int fill_sm2_dec_sqe(void *message, struct qm_queue_info *info, __u16 i)
 	struct wd_mm_br *br = &qinfo->br;
 	__u32 ksz = req_src->key_bytes;
 	struct wcrypto_ecc_msg *dst;
+	int ret;
 
 	/* c2 data lens <= 4096 bit */
 	if (din->c2.dsize <= BITS_TO_BYTES(4096) &&
@@ -1966,11 +1971,21 @@ static int fill_sm2_dec_sqe(void *message, struct qm_queue_info *info, __u16 i)
 	dst->in = (void *)&din->c1;
 	dst->out = br->alloc(br->usr, ECDH_OUT_PARAMS_SZ(ksz));
 	if (unlikely(!dst->out)) {
-		free(dst);
-		return -WD_ENOMEM;
+		ret = -WD_ENOMEM;
+		goto free_dst;
 	}
 
-	return qm_fill_ecc_sqe_general(dst, info, i);
+	ret = qm_fill_ecc_sqe_general(dst, info, i);
+	if (ret)
+		goto free_out;
+
+	return ret;
+
+free_out:
+	br->free(br->usr, dst->out);
+free_dst:
+	free(dst);
+	return ret;
 }
 
 int qm_fill_ecc_sqe(void *message, struct qm_queue_info *info, __u16 i)
@@ -1997,7 +2012,10 @@ static int qm_parse_ecc_sqe_general(void *msg, const struct qm_queue_info *info,
 	__u16 kbytes;
 	int ret;
 
-	ASSERT(ecc_msg);
+	if (unlikely(!ecc_msg)) {
+		WD_ERR("info->req_cache is null at index:%hu\n", i);
+		return 0;
+	}
 
 	/* if this hw msg not belong to me, then try again */
 	if (usr && LOW_U16(hw_msg->low_tag) != usr)
@@ -2146,7 +2164,7 @@ static __u32 get_hash_bytes(__u8 type)
 	return val;
 }
 
-static void msg_pack(char *dst, __u64 dst_len, __u64 *out_len,
+static void msg_pack(char *dst, __u64 *out_len,
 		     const void *src, __u32 src_len)
 {
 	if (unlikely(!src || !src_len))
@@ -2191,8 +2209,8 @@ static int sm2_kdf(struct wd_dtb *out, struct wcrypto_ecc_point *x2y2,
 		ctr[1] = (i >> 16) & 0xFF;
 		ctr[0] = (i >> 24) & 0xFF;
 		in_len = 0;
-		msg_pack(p_in, lens, &in_len, x2y2->x.data, x2y2_len);
-		msg_pack(p_in, lens, &in_len, ctr, sizeof(ctr));
+		msg_pack(p_in, &in_len, x2y2->x.data, x2y2_len);
+		msg_pack(p_in, &in_len, ctr, sizeof(ctr));
 
 		t_out = m_len >= h_bytes ? tmp : p_out;
 		ret = hash->cb(p_in, in_len, t_out, h_bytes, hash->usr);
@@ -2257,9 +2275,9 @@ static int sm2_hash(struct wd_dtb *out, struct wcrypto_ecc_point *x2y2,
 	if (unlikely(!p_in))
 		return -WD_ENOMEM;
 
-	msg_pack(p_in, lens, &in_len, x2y2->x.data, x2y2->x.dsize);
-	msg_pack(p_in, lens, &in_len, msg->data, msg->dsize);
-	msg_pack(p_in, lens, &in_len, x2y2->y.data, x2y2->y.dsize);
+	msg_pack(p_in, &in_len, x2y2->x.data, x2y2->x.dsize);
+	msg_pack(p_in, &in_len, msg->data, msg->dsize);
+	msg_pack(p_in, &in_len, x2y2->y.data, x2y2->y.dsize);
 	ret = hash->cb(p_in, in_len, hash_out, h_bytes, hash->usr);
 	if (unlikely(ret)) {
 		WD_ERR("failed to hash cb, ret = %d!\n", ret);
diff --git a/v1/wd_ecc.c b/v1/wd_ecc.c
index c710d8a..2358243 100644
--- a/v1/wd_ecc.c
+++ b/v1/wd_ecc.c
@@ -1508,7 +1508,7 @@ static int ecc_request_init(struct wcrypto_ecc_msg *req,
 	return WD_SUCCESS;
 }
 
-static void msg_pack(char *dst, __u64 dst_len, __u64 *out_len,
+static void msg_pack(char *dst, __u64 *out_len,
 		     const void *src, __u32 src_len)
 {
 	if (unlikely(!src || !src_len))
@@ -1831,17 +1831,17 @@ static int sm2_compute_za_hash(__u8 *za, __u32 *len, struct wd_dtb *id,
 
 	memset(p_in, 0, lens);
 	temp = id_bits >> 8;
-	msg_pack(p_in, lens, &in_len, &temp, sizeof(__u8));
+	msg_pack(p_in, &in_len, &temp, sizeof(__u8));
 	temp = id_bits & 0xFF;
-	msg_pack(p_in, lens, &in_len, &temp, sizeof(__u8));
+	msg_pack(p_in, &in_len, &temp, sizeof(__u8));
 	if (id)
-		msg_pack(p_in, lens, &in_len, id->data, id_bytes);
-	msg_pack(p_in, lens, &in_len, cv->a.data, key_size);
-	msg_pack(p_in, lens, &in_len, cv->b.data, key_size);
-	msg_pack(p_in, lens, &in_len, cv->g.x.data, key_size);
-	msg_pack(p_in, lens, &in_len, cv->g.y.data, key_size);
-	msg_pack(p_in, lens, &in_len, pub->x.data, key_size);
-	msg_pack(p_in, lens, &in_len, pub->y.data, key_size);
+		msg_pack(p_in, &in_len, id->data, id_bytes);
+	msg_pack(p_in, &in_len, cv->a.data, key_size);
+	msg_pack(p_in, &in_len, cv->b.data, key_size);
+	msg_pack(p_in, &in_len, cv->g.x.data, key_size);
+	msg_pack(p_in, &in_len, cv->g.y.data, key_size);
+	msg_pack(p_in, &in_len, pub->x.data, key_size);
+	msg_pack(p_in, &in_len, pub->y.data, key_size);
 
 	hash_bytes = get_hash_bytes(hash->type);
 	*len = hash_bytes;
@@ -1885,8 +1885,8 @@ static int sm2_compute_digest(void *ctx, struct wd_dtb *hash_msg,
 
 	/* e = h(ZA || M) */
 	memset(p_in, 0, lens);
-	msg_pack(p_in, lens, &in_len, za, za_len);
-	msg_pack(p_in, lens, &in_len, plaintext->data, plaintext->dsize);
+	msg_pack(p_in, &in_len, za, za_len);
+	msg_pack(p_in, &in_len, plaintext->data, plaintext->dsize);
 	hash_msg->dsize = hash_bytes;
 	ret = hash->cb((const char *)p_in, in_len, hash_msg->data,
 			hash_bytes, hash->usr);
diff --git a/wd_ecc.c b/wd_ecc.c
index 3232e67..a9994b2 100644
--- a/wd_ecc.c
+++ b/wd_ecc.c
@@ -1396,7 +1396,7 @@ static int fill_ecc_msg(struct wd_ecc_msg *msg, struct wd_ecc_req *req,
 	return WD_SUCCESS;
 }
 
-static void msg_pack(char *dst, __u64 dst_len, __u64 *out_len,
+static void msg_pack(char *dst, __u64 *out_len,
 		     const void *src, __u32 src_len)
 {
 	if (!src || !src_len)
@@ -1596,17 +1596,17 @@ static int sm2_compute_za_hash(__u8 *za, __u32 *len, struct wd_dtb *id,
 
 	memset(p_in, 0, lens);
 	temp = id_bits >> 8;
-	msg_pack(p_in, lens, &in_len, &temp, sizeof(__u8));
+	msg_pack(p_in, &in_len, &temp, sizeof(__u8));
 	temp = id_bits & 0xFF;
-	msg_pack(p_in, lens, &in_len, &temp, sizeof(__u8));
+	msg_pack(p_in, &in_len, &temp, sizeof(__u8));
 	if (id)
-		msg_pack(p_in, lens, &in_len, id->data, id_bytes);
-	msg_pack(p_in, lens, &in_len, cv->a.data, key_size);
-	msg_pack(p_in, lens, &in_len, cv->b.data, key_size);
-	msg_pack(p_in, lens, &in_len, cv->g.x.data, key_size);
-	msg_pack(p_in, lens, &in_len, cv->g.y.data, key_size);
-	msg_pack(p_in, lens, &in_len, pub->x.data, key_size);
-	msg_pack(p_in, lens, &in_len, pub->y.data, key_size);
+		msg_pack(p_in, &in_len, id->data, id_bytes);
+	msg_pack(p_in, &in_len, cv->a.data, key_size);
+	msg_pack(p_in, &in_len, cv->b.data, key_size);
+	msg_pack(p_in, &in_len, cv->g.x.data, key_size);
+	msg_pack(p_in, &in_len, cv->g.y.data, key_size);
+	msg_pack(p_in, &in_len, pub->x.data, key_size);
+	msg_pack(p_in, &in_len, pub->y.data, key_size);
 	hash_bytes = get_hash_bytes(hash->type);
 	*len = hash_bytes;
 	ret = hash->cb((const char *)p_in, in_len,
@@ -1649,8 +1649,8 @@ static int sm2_compute_digest(struct wd_ecc_sess *sess, struct wd_dtb *hash_msg,
 
 	/* e = h(ZA || M) */
 	memset(p_in, 0, lens);
-	msg_pack(p_in, lens, &in_len, za, za_len);
-	msg_pack(p_in, lens, &in_len, plaintext->data, plaintext->dsize);
+	msg_pack(p_in, &in_len, za, za_len);
+	msg_pack(p_in, &in_len, plaintext->data, plaintext->dsize);
 	hash_msg->dsize = hash_bytes;
 	ret = hash->cb((const char *)p_in, in_len, hash_msg->data,
 			hash_bytes, hash->usr);
-- 
2.27.0

