[PATCH 19/30] crypto: sa2ul - generate dynamic metadata length

From: Manorit Chawdhry

Date: Tue Sep 15 2026 - 06:43:51 EST


Calculate metadata length dynamically based on actual algorithm
requirements instead of using a fixed value and remove the code that is
not in the spec and not required to be passed to sa2ul.

Aligns the index as well after the removal of those extra bytes.

Assisted-by: Sisyphus:claude-sonnet-4-6
Signed-off-by: Manorit Chawdhry <m-chawdhry@xxxxxx>
---
drivers/crypto/sa2ul.c | 25 +++++++------------------
drivers/crypto/sa2ul.h | 23 +----------------------
2 files changed, 8 insertions(+), 40 deletions(-)

diff --git a/drivers/crypto/sa2ul.c b/drivers/crypto/sa2ul.c
index cc9f2881efda..a60b968c21f3 100644
--- a/drivers/crypto/sa2ul.c
+++ b/drivers/crypto/sa2ul.c
@@ -956,7 +956,7 @@ static int sa_3des_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key,

ad.mci_enc = mci_cbc_3des_enc_array;
ad.mci_dec = mci_cbc_3des_dec_array;
- ad.iv_idx = 6;
+ ad.iv_idx = 4;
ad.iv_out_size = 8;

return sa_cipher_setkey(tfm, key, keylen, &ad);
@@ -1028,7 +1028,7 @@ static void sa_aes_dma_in_callback(void *data)
skcipher_request_complete(req, 0);
}

-static void
+static int
sa_prepare_tx_desc(u32 *mdptr, u32 pslen, u32 *psdata, u32 epiblen, u32 *epib)
{
u32 *out, *in;
@@ -1041,6 +1041,8 @@ sa_prepare_tx_desc(u32 *mdptr, u32 pslen, u32 *psdata, u32 epiblen, u32 *epib)
for (out = &mdptr[5], in = psdata, i = 0;
i < pslen / sizeof(u32); i++)
*out++ = *in++;
+
+ return epiblen + sizeof(u32) + pslen;
}

static int sa_run(struct sa_req *req)
@@ -1097,16 +1099,6 @@ static int sa_run(struct sa_req *req)

sa_update_cmdl(req, cmdl, &sa_ctx->cmdl_upd_info);

- if (req->type != CRYPTO_ALG_TYPE_AHASH) {
- if (req->enc)
- req->type |=
- (SA_REQ_SUBTYPE_ENC << SA_REQ_SUBTYPE_SHIFT);
- else
- req->type |=
- (SA_REQ_SUBTYPE_DEC << SA_REQ_SUBTYPE_SHIFT);
- }
-
- cmdl[sa_ctx->cmdl_size / sizeof(u32)] = req->type;

/*
* Map the packets, first we check if the data fits into a single
@@ -1239,9 +1231,9 @@ static int sa_run(struct sa_req *req)
*/
mdptr = (u32 *)dmaengine_desc_get_metadata_ptr(tx_out, &pl, &ml);

- sa_prepare_tx_desc(mdptr, (sa_ctx->cmdl_size + (SA_PSDATA_CTX_WORDS *
- sizeof(u32))), cmdl, sizeof(sa_ctx->epib),
- sa_ctx->epib);
+ req->mdata_size = sa_prepare_tx_desc(mdptr, sa_ctx->cmdl_size,
+ cmdl, sizeof(sa_ctx->epib),
+ sa_ctx->epib);

dmaengine_desc_set_metadata_len(tx_out, req->mdata_size);

@@ -1298,7 +1290,6 @@ static int sa_cipher_run(struct skcipher_request *req, u8 *iv, int enc)
sa_req.type = CRYPTO_ALG_TYPE_SKCIPHER;
sa_req.enc = enc;
sa_req.callback = sa_aes_dma_in_callback;
- sa_req.mdata_size = 44;
sa_req.base = &req->base;
sa_req.ctx = ctx;

@@ -1398,7 +1389,6 @@ static int sa_sha_run(struct ahash_request *req)
sa_req.enc = true;
sa_req.type = CRYPTO_ALG_TYPE_AHASH;
sa_req.callback = sa_sha_dma_in_callback;
- sa_req.mdata_size = 28;
sa_req.ctx = ctx;
sa_req.base = &req->base;

@@ -1862,7 +1852,6 @@ static int sa_aead_run(struct aead_request *req, u8 *iv, int enc)
sa_req.type = CRYPTO_ALG_TYPE_AEAD;
sa_req.enc = enc;
sa_req.callback = sa_aead_dma_in_callback;
- sa_req.mdata_size = 52;
sa_req.base = &req->base;
sa_req.ctx = ctx;
sa_req.src = req->src;
diff --git a/drivers/crypto/sa2ul.h b/drivers/crypto/sa2ul.h
index 04669bf7fcb5..fbea98981f10 100644
--- a/drivers/crypto/sa2ul.h
+++ b/drivers/crypto/sa2ul.h
@@ -31,22 +31,9 @@ struct sa_tfm_ctx;
#define SA_EEC_CPPI_PORT_IN_EN 0x00000200
#define SA_EEC_CPPI_PORT_OUT_EN 0x00000800

-/*
- * Encoding used to identify the typo of crypto operation
- * performed on the packet when the packet is returned
- * by SA
- */
-#define SA_REQ_SUBTYPE_ENC 0x0001
-#define SA_REQ_SUBTYPE_DEC 0x0002
-#define SA_REQ_SUBTYPE_SHIFT 16
-#define SA_REQ_SUBTYPE_MASK 0xffff
-
/* Number of 32 bit words in EPIB */
#define SA_DMA_NUM_EPIB_WORDS 4

-/* Number of 32 bit words in PS data */
-#define SA_DMA_NUM_PS_WORDS 16
-
/*
* Maximum number of simultaeneous security contexts
* supported by the driver
@@ -219,16 +206,8 @@ struct sa_cmdl_upd_info {
struct sa_cmdl_param_info auth_offset;
};

-/*
- * Number of 32bit words appended after the command label
- * in PSDATA to identify the crypto request context.
- * word-0: Request type
- * word-1: pointer to request
- */
-#define SA_PSDATA_CTX_WORDS 4
-
/* Maximum size of Command label in 32 words */
-#define SA_MAX_CMDL_WORDS (SA_DMA_NUM_PS_WORDS - SA_PSDATA_CTX_WORDS)
+#define SA_MAX_CMDL_WORDS 24

/**
* struct sa_ctx_info: SA context information

--
2.43.0