Commit 3d21c41f authored by Ryder Lee's avatar Ryder Lee Committed by Herbert Xu

crypto: mediatek - simplify descriptor ring management

This patch replaces cmd_pos/res_pos with pointer cmd_next/res_next.

In old code, we must to add one to shift ring to the next segment, and
then use this value to caculate current offset from ring base for each
DMA operation. Now these pointers helps us to simplify flow, so we just
need to move pointers and check the boundaries of ring.
Signed-off-by: default avatarRyder Lee <ryder.lee@mediatek.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 82445fe9
...@@ -262,7 +262,7 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct mtk_aes_rec *aes) ...@@ -262,7 +262,7 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
/* Write command descriptors */ /* Write command descriptors */
for (nents = 0; nents < slen; ++nents, ssg = sg_next(ssg)) { for (nents = 0; nents < slen; ++nents, ssg = sg_next(ssg)) {
cmd = ring->cmd_base + ring->cmd_pos; cmd = ring->cmd_next;
cmd->hdr = MTK_DESC_BUF_LEN(ssg->length); cmd->hdr = MTK_DESC_BUF_LEN(ssg->length);
cmd->buf = cpu_to_le32(sg_dma_address(ssg)); cmd->buf = cpu_to_le32(sg_dma_address(ssg));
...@@ -274,22 +274,24 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct mtk_aes_rec *aes) ...@@ -274,22 +274,24 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
cmd->tfm = cpu_to_le32(aes->ctx->tfm_dma); cmd->tfm = cpu_to_le32(aes->ctx->tfm_dma);
} }
if (++ring->cmd_pos == MTK_DESC_NUM) /* Shift ring buffer and check boundary */
ring->cmd_pos = 0; if (++ring->cmd_next == ring->cmd_base + MTK_DESC_NUM)
ring->cmd_next = ring->cmd_base;
} }
cmd->hdr |= MTK_DESC_LAST; cmd->hdr |= MTK_DESC_LAST;
/* Prepare result descriptors */ /* Prepare result descriptors */
for (nents = 0; nents < dlen; ++nents, dsg = sg_next(dsg)) { for (nents = 0; nents < dlen; ++nents, dsg = sg_next(dsg)) {
res = ring->res_base + ring->res_pos; res = ring->res_next;
res->hdr = MTK_DESC_BUF_LEN(dsg->length); res->hdr = MTK_DESC_BUF_LEN(dsg->length);
res->buf = cpu_to_le32(sg_dma_address(dsg)); res->buf = cpu_to_le32(sg_dma_address(dsg));
if (nents == 0) if (nents == 0)
res->hdr |= MTK_DESC_FIRST; res->hdr |= MTK_DESC_FIRST;
if (++ring->res_pos == MTK_DESC_NUM) /* Shift ring buffer and check boundary */
ring->res_pos = 0; if (++ring->res_next == ring->res_base + MTK_DESC_NUM)
ring->res_next = ring->res_base;
} }
res->hdr |= MTK_DESC_LAST; res->hdr |= MTK_DESC_LAST;
......
...@@ -465,6 +465,9 @@ static int mtk_desc_ring_alloc(struct mtk_cryp *cryp) ...@@ -465,6 +465,9 @@ static int mtk_desc_ring_alloc(struct mtk_cryp *cryp)
GFP_KERNEL); GFP_KERNEL);
if (!ring[i]->res_base) if (!ring[i]->res_base)
goto err_cleanup; goto err_cleanup;
ring[i]->cmd_next = ring[i]->cmd_base;
ring[i]->res_next = ring[i]->res_base;
} }
return 0; return 0;
......
...@@ -84,11 +84,11 @@ struct mtk_desc { ...@@ -84,11 +84,11 @@ struct mtk_desc {
/** /**
* struct mtk_ring - Descriptor ring * struct mtk_ring - Descriptor ring
* @cmd_base: pointer to command descriptor ring base * @cmd_base: pointer to command descriptor ring base
* @cmd_next: pointer to the next command descriptor
* @cmd_dma: DMA address of command descriptor ring * @cmd_dma: DMA address of command descriptor ring
* @cmd_pos: current position in the command descriptor ring
* @res_base: pointer to result descriptor ring base * @res_base: pointer to result descriptor ring base
* @res_next: pointer to the next result descriptor
* @res_dma: DMA address of result descriptor ring * @res_dma: DMA address of result descriptor ring
* @res_pos: current position in the result descriptor ring
* *
* A descriptor ring is a circular buffer that is used to manage * A descriptor ring is a circular buffer that is used to manage
* one or more descriptors. There are two type of descriptor rings; * one or more descriptors. There are two type of descriptor rings;
...@@ -96,11 +96,11 @@ struct mtk_desc { ...@@ -96,11 +96,11 @@ struct mtk_desc {
*/ */
struct mtk_ring { struct mtk_ring {
struct mtk_desc *cmd_base; struct mtk_desc *cmd_base;
struct mtk_desc *cmd_next;
dma_addr_t cmd_dma; dma_addr_t cmd_dma;
u32 cmd_pos;
struct mtk_desc *res_base; struct mtk_desc *res_base;
struct mtk_desc *res_next;
dma_addr_t res_dma; dma_addr_t res_dma;
u32 res_pos;
}; };
/** /**
......
...@@ -152,6 +152,21 @@ static inline void mtk_sha_write(struct mtk_cryp *cryp, ...@@ -152,6 +152,21 @@ static inline void mtk_sha_write(struct mtk_cryp *cryp,
writel_relaxed(value, cryp->base + offset); writel_relaxed(value, cryp->base + offset);
} }
static inline void mtk_sha_ring_shift(struct mtk_ring *ring,
struct mtk_desc **cmd_curr,
struct mtk_desc **res_curr,
int *count)
{
*cmd_curr = ring->cmd_next++;
*res_curr = ring->res_next++;
(*count)++;
if (ring->cmd_next == ring->cmd_base + MTK_DESC_NUM) {
ring->cmd_next = ring->cmd_base;
ring->res_next = ring->res_base;
}
}
static struct mtk_cryp *mtk_sha_find_dev(struct mtk_sha_ctx *tctx) static struct mtk_cryp *mtk_sha_find_dev(struct mtk_sha_ctx *tctx)
{ {
struct mtk_cryp *cryp = NULL; struct mtk_cryp *cryp = NULL;
...@@ -426,8 +441,7 @@ static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha, ...@@ -426,8 +441,7 @@ static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
{ {
struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req); struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
struct mtk_ring *ring = cryp->ring[sha->id]; struct mtk_ring *ring = cryp->ring[sha->id];
struct mtk_desc *cmd = ring->cmd_base + ring->cmd_pos; struct mtk_desc *cmd, *res;
struct mtk_desc *res = ring->res_base + ring->res_pos;
int err, count = 0; int err, count = 0;
err = mtk_sha_info_update(cryp, sha, len1, len2); err = mtk_sha_info_update(cryp, sha, len1, len2);
...@@ -435,6 +449,8 @@ static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha, ...@@ -435,6 +449,8 @@ static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
return err; return err;
/* Fill in the command/result descriptors */ /* Fill in the command/result descriptors */
mtk_sha_ring_shift(ring, &cmd, &res, &count);
res->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1); res->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1);
cmd->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1) | cmd->hdr = MTK_DESC_FIRST | MTK_DESC_BUF_LEN(len1) |
MTK_DESC_CT_LEN(ctx->ct_size); MTK_DESC_CT_LEN(ctx->ct_size);
...@@ -443,25 +459,12 @@ static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha, ...@@ -443,25 +459,12 @@ static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
cmd->ct_hdr = ctx->ct_hdr; cmd->ct_hdr = ctx->ct_hdr;
cmd->tfm = cpu_to_le32(ctx->tfm_dma); cmd->tfm = cpu_to_le32(ctx->tfm_dma);
if (++ring->cmd_pos == MTK_DESC_NUM)
ring->cmd_pos = 0;
ring->res_pos = ring->cmd_pos;
count++;
if (len2) { if (len2) {
cmd = ring->cmd_base + ring->cmd_pos; mtk_sha_ring_shift(ring, &cmd, &res, &count);
res = ring->res_base + ring->res_pos;
res->hdr = MTK_DESC_BUF_LEN(len2); res->hdr = MTK_DESC_BUF_LEN(len2);
cmd->hdr = MTK_DESC_BUF_LEN(len2); cmd->hdr = MTK_DESC_BUF_LEN(len2);
cmd->buf = cpu_to_le32(addr2); cmd->buf = cpu_to_le32(addr2);
if (++ring->cmd_pos == MTK_DESC_NUM)
ring->cmd_pos = 0;
ring->res_pos = ring->cmd_pos;
count++;
} }
cmd->hdr |= MTK_DESC_LAST; cmd->hdr |= MTK_DESC_LAST;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment