From 484e58e2acbf8313bfd7331971874e33934231e0 Mon Sep 17 00:00:00 2001
From: Aashvij Shenai <a-shenai@ti.com>
Date: Mon, 19 Aug 2024 16:13:26 +0530
Subject: [PATCH 2/6] sa2ul: moving completion

It is observed that the dma callback is happening even before the
init_completion is happening which causes issues when complete is called

Extra tests were failing for digest. By converting it to
init-update-final, it is working. Have to debug why its failing

Signed-off-by: Aashvij Shenai <a-shenai@ti.com>
---
 crypto/testmgr.c       |  2 --
 drivers/crypto/sa2ul.c | 28 ++++++++++++++++------------
 2 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/drivers/crypto/sa2ul.c b/drivers/crypto/sa2ul.c
index e0524f4e57a1..5d5b344d1aef 100644
--- a/drivers/crypto/sa2ul.c
+++ b/drivers/crypto/sa2ul.c
@@ -390,7 +390,6 @@ static void sa_sha_dma_in_callback_tx_out(void *data)
 	struct sa_tx_data *txd = data;
 
         complete(&txd->done);
-        printk("%s: %d\n",__func__,__LINE__);
 }
 
 /*
@@ -1290,7 +1289,6 @@ static int sa_run_sha512(struct sa_req *req, int flags, u8 *buf, u8 *bufr, size_
 		sa_ctx->epib[1] &= ~((1U << 16U) & 0x10000); //EVICT
 		sa_ctx->epib[1] &= ~((1U << 17U) & 0x20000); //TEARDOWN
 		cmdl[3] = 0;
-                printk("update length = %d\n", req->auth_size);
 	}
 	else {
 		sa_ctx->epib[1] &= ~((1U << 19U) & 0x80000); //FRAG BIT
@@ -1298,7 +1296,6 @@ static int sa_run_sha512(struct sa_req *req, int flags, u8 *buf, u8 *bufr, size_
 
 		sa_ctx->epib[1] |= (1U << 16U) & 0x10000; //EVICT
 		sa_ctx->epib[1] |= (1U << 17U) & 0x20000; //TEARDOWN
-                printk("final len = %d\n", req->auth_size);
 		cmdl[3] = (req->auth_size<<3);
 	}
 	sa_prepare_tx_desc(mdptr, (sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS *
@@ -1308,6 +1305,10 @@ static int sa_run_sha512(struct sa_req *req, int flags, u8 *buf, u8 *bufr, size_
 	ml = sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS * sizeof(u32));
 	dmaengine_desc_set_metadata_len(tx_out, req->mdata_size);
 
+        init_completion(&txd->done);
+        init_completion(&rxd->done);
+
+
 	dmaengine_submit(tx_out);
 	dmaengine_submit(rxd->tx_in);
         if(dma_rx_extra) {
@@ -1321,8 +1322,6 @@ static int sa_run_sha512(struct sa_req *req, int flags, u8 *buf, u8 *bufr, size_
 
 	dma_async_issue_pending(pdata->dma_tx);
 
-        init_completion(&txd->done);
-        init_completion(&rxd->done);
         
         wait_for_completion(&txd->done);
         wait_for_completion(&rxd->done);
@@ -1713,7 +1712,6 @@ static void sa_sha_dma_in_callback_sha512_update(void *data)
 	u32 *result;
 	__be32 *mdptr;
 
-	printk("%s\n", __func__);
 
 	sa_sync_from_device(rxd);
 	req = container_of(rxd->req, struct ahash_request, base);
@@ -1960,8 +1958,7 @@ static int sa_sha_digest(struct ahash_request *req)
         struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
 	struct algo_data ad = { 0 };
 
-	printk("count = %d\n",count++);
-
+	count++;
         ad.aalg_id = SA_AALG_ID_SHA2_512;
         ad.hash_size = SHA512_DIGEST_SIZE;
         ad.auth_ctrl = SA_AUTH_SW_CTRL_SHA512;
@@ -1979,7 +1976,7 @@ static int sa_sha_init_sha512(struct ahash_request *req)
 	struct sa_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
 	struct algo_data ad = { 0 };
 
-	printk("count = %d\n",count++);
+	count++;
 	size_t auth_len = req->nbytes;
 
 	dev_dbg(sa_k3_dev, "init: digest size: %u, rctx=%p\n",
@@ -2065,7 +2062,6 @@ static int sa_sha_update_sha512(struct ahash_request *req)
 	rctx->fallback_req.src = req->src;
 	count = 0;
 
-	mdelay(1);
 
 	if(ctx->first_pkt) 
 	  {
@@ -2133,7 +2129,6 @@ static int sa_sha_final_sha512(struct ahash_request *req)
 
 	size_t auth_len = req->nbytes;
 
-	mdelay(1);
 
 	if(ctx->first_pkt) {
                 if (!auth_len || ctx->go_to_fallback ||
@@ -2206,6 +2201,15 @@ static int sa_sha_finup_sha512(struct ahash_request *req)
 	return crypto_ahash_final(req);
 }
 
+static int sa_sha_digest_sha512(struct ahash_request *req)
+{
+	sa_sha_init_sha512(req);
+
+	sa_sha_update_sha512(req);
+
+	return sa_sha_final_sha512(req);
+
+}
 static int sa_sha_init(struct ahash_request *req)
 {
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
@@ -2861,7 +2865,7 @@ static struct sa_alg_tmpl sa_algs[] = {
 			.update			= sa_sha_update_sha512,
 			.final			= sa_sha_final_sha512,
 			.finup			= sa_sha_finup_sha512,
-			.digest			= sa_sha_digest,
+			.digest			= sa_sha_digest_sha512,
 			.export			= sa_sha_export,
 			.import			= sa_sha_import,
 		},
-- 
2.34.1

