Skip to content

Commit 04d088c

Browse files
Marek Vasutherbertx
authored andcommitted
crypto: mxs-dcp - Optimize hashing
Optimize the hashing operation in the MXS-DCP by doing two adjustments: 1) Given that the output buffer for the hash is now always correctly aligned, we can just use the buffer for the DCP DMA to store the resulting hash. We thus get rid of one copying of data. Moreover, we remove an entry from dcp_coherent_block{} and thus lower the memory footprint of the driver. 2) We map the output buffer for the hash for DMA only in case we will output the hash, not always, as it was now. Signed-off-by: Marek Vasut <[email protected]> Cc: David S. Miller <[email protected]> Cc: Fabio Estevam <[email protected]> Cc: Herbert Xu <[email protected]> Cc: Shawn Guo <[email protected]> Cc: Tom Lendacky <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 1a7c685 commit 04d088c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

drivers/crypto/mxs-dcp.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ struct dcp_coherent_block {
5050
uint8_t sha_in_buf[DCP_BUF_SZ];
5151

5252
uint8_t aes_key[2 * AES_KEYSIZE_128];
53-
uint8_t sha_digest[SHA256_DIGEST_SIZE];
5453

5554
struct dcp_dma_desc desc[DCP_MAX_CHANS];
5655
};
@@ -516,13 +515,11 @@ static int mxs_dcp_run_sha(struct ahash_request *req)
516515
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
517516
struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
518517
struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
518+
struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
519519

520520
struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
521-
dma_addr_t digest_phys = dma_map_single(sdcp->dev,
522-
sdcp->coh->sha_digest,
523-
SHA256_DIGEST_SIZE,
524-
DMA_FROM_DEVICE);
525521

522+
dma_addr_t digest_phys = 0;
526523
dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
527524
DCP_BUF_SZ, DMA_TO_DEVICE);
528525

@@ -543,14 +540,18 @@ static int mxs_dcp_run_sha(struct ahash_request *req)
543540

544541
/* Set HASH_TERM bit for last transfer block. */
545542
if (rctx->fini) {
543+
digest_phys = dma_map_single(sdcp->dev, req->result,
544+
halg->digestsize, DMA_FROM_DEVICE);
546545
desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
547546
desc->payload = digest_phys;
548547
}
549548

550549
ret = mxs_dcp_start_dma(actx);
551550

552-
dma_unmap_single(sdcp->dev, digest_phys, SHA256_DIGEST_SIZE,
553-
DMA_FROM_DEVICE);
551+
if (rctx->fini)
552+
dma_unmap_single(sdcp->dev, digest_phys, halg->digestsize,
553+
DMA_FROM_DEVICE);
554+
554555
dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
555556

556557
return ret;
@@ -567,7 +568,6 @@ static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
567568
struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
568569
const int nents = sg_nents(req->src);
569570

570-
uint8_t *digest = sdcp->coh->sha_digest;
571571
uint8_t *in_buf = sdcp->coh->sha_in_buf;
572572

573573
uint8_t *src_buf;
@@ -614,14 +614,20 @@ static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
614614
rctx->fini = 1;
615615

616616
/* Submit whatever is left. */
617+
if (!req->result)
618+
return -EINVAL;
619+
617620
ret = mxs_dcp_run_sha(req);
618-
if (ret || !req->result)
621+
if (ret)
619622
return ret;
623+
620624
actx->fill = 0;
621625

622626
/* For some reason, the result is flipped. */
623-
for (i = 0; i < halg->digestsize; i++)
624-
req->result[i] = digest[halg->digestsize - i - 1];
627+
for (i = 0; i < halg->digestsize / 2; i++) {
628+
swap(req->result[i],
629+
req->result[halg->digestsize - i - 1]);
630+
}
625631
}
626632

627633
return 0;

0 commit comments

Comments
 (0)