[PATCH v1] crypto: mediatek - Add empty messages check in GCM mode

2017-08-22 Thread Ryder Lee
Currently, empty messages are not supported in GCM mode, hence add
a check to prevent producing incorrect results.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
changes since v1: 
- fix build erro: add a missing semicolon.

 drivers/crypto/mediatek/mtk-aes.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 9e845e8..72e4549 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -928,8 +928,13 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 static int mtk_aes_gcm_crypt(struct aead_request *req, u64 mode)
 {
struct mtk_aes_base_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
+   struct mtk_aes_gcm_ctx *gctx = mtk_aes_gcm_ctx_cast(ctx);
struct mtk_aes_reqctx *rctx = aead_request_ctx(req);
 
+   /* Empty messages are not supported yet */
+   if (!gctx->textlen && !req->assoclen)
+   return -EINVAL;
+
rctx->mode = AES_FLAGS_GCM | mode;
 
return mtk_aes_handle_queue(ctx->cryp, !!(mode & AES_FLAGS_ENCRYPT),
-- 
1.9.1



Re: [PATCH] crypto: mediatek - Add empty messages check in GCM mode

2017-08-22 Thread Ryder Lee
Hi,
On Tue, 2017-08-22 at 15:01 +0800, Herbert Xu wrote:
> On Wed, Aug 16, 2017 at 07:19:48PM +0800, Ryder Lee wrote:
> > Currently, empty messages are not supported in GCM mode, hence add
> > a check to prevent producing incorrect results.
> > 
> > Signed-off-by: Ryder Lee <ryder@mediatek.com>
> > ---
> >  drivers/crypto/mediatek/mtk-aes.c | 5 +
> >  1 file changed, 5 insertions(+)
> > 
> > diff --git a/drivers/crypto/mediatek/mtk-aes.c 
> > b/drivers/crypto/mediatek/mtk-aes.c
> > index 9e845e8..efe4e63 100644
> > --- a/drivers/crypto/mediatek/mtk-aes.c
> > +++ b/drivers/crypto/mediatek/mtk-aes.c
> > @@ -928,8 +928,13 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, 
> > struct mtk_aes_rec *aes)
> >  static int mtk_aes_gcm_crypt(struct aead_request *req, u64 mode)
> >  {
> > struct mtk_aes_base_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
> > +   struct mtk_aes_gcm_ctx *gctx = mtk_aes_gcm_ctx_cast(ctx);
> > struct mtk_aes_reqctx *rctx = aead_request_ctx(req);
> >  
> > +   /* Empty messages are not supported yet */
> > +   if (!gctx->textlen && !req->assoclen)
> > +   return -EINVAL
> 
> Your patch does not compile.

Ohh..Somehow I delete the semicolon. I will send v1 soon, sorry about
that.

Ryder




Re: [PATCH] crypto: mediatek - Add empty messages check in GCM mode

2017-08-16 Thread Ryder Lee
Hi,

On Wed, 2017-08-16 at 14:18 +0200, Stephan Mueller wrote:
> Am Mittwoch, 16. August 2017, 13:19:48 CEST schrieb Ryder Lee:
> 
> Hi Ryder,
> 
> > Currently, empty messages are not supported in GCM mode, hence add
> > a check to prevent producing incorrect results.
> 
> In case the caller would provide empty messages, would there be just wrong 
> data or a real problem? Note, unprivileged user space can make such requests.
> 
> If there would be a real problem (like crash), this should also go to stable.
> 
> Ciao
> Stephan

This does not cause any problem and it just returns a wrong result. 

Ryder



[PATCH] crypto: mediatek - Add empty messages check in GCM mode

2017-08-16 Thread Ryder Lee
Currently, empty messages are not supported in GCM mode, hence add
a check to prevent producing incorrect results.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 9e845e8..efe4e63 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -928,8 +928,13 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 static int mtk_aes_gcm_crypt(struct aead_request *req, u64 mode)
 {
struct mtk_aes_base_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
+   struct mtk_aes_gcm_ctx *gctx = mtk_aes_gcm_ctx_cast(ctx);
struct mtk_aes_reqctx *rctx = aead_request_ctx(req);
 
+   /* Empty messages are not supported yet */
+   if (!gctx->textlen && !req->assoclen)
+   return -EINVAL
+
rctx->mode = AES_FLAGS_GCM | mode;
 
return mtk_aes_handle_queue(ctx->cryp, !!(mode & AES_FLAGS_ENCRYPT),
-- 
1.9.1



[PATCH v1 2/2] dt-bindings: crypto: remove mediatek ethif clock

2017-05-31 Thread Ryder Lee
This patch removes the parent clock 'ethif' in bindings, since we don't
need to control the parent of a clock in current clock framework.

Moreover, the clocks are get by name in the driver, thus this change
does not break backwards compatibility.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
Reviewed-by: Matthias Brugger <matthias@gmail.com>
---
 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt | 8 +++-
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt 
b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
index c204725..450da36 100644
--- a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
+++ b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
@@ -6,8 +6,7 @@ Required properties:
 - interrupts: Should contain the five crypto engines interrupts in numeric
order. These are global system and four descriptor rings.
 - clocks: the clock used by the core
-- clock-names: the names of the clock listed in the clocks property. These are
-   "ethif", "cryp"
+- clock-names: Must contain "cryp".
 - power-domains: Must contain a reference to the PM domain.
 
 
@@ -20,8 +19,7 @@ Example:
 ,
 ,
 ;
-   clocks = < CLK_TOP_ETHIF_SEL>,
-< CLK_ETHSYS_CRYPTO>;
-   clock-names = "ethif","cryp";
+   clocks = < CLK_ETHSYS_CRYPTO>;
+   clock-names = "cryp";
power-domains = < MT2701_POWER_DOMAIN_ETH>;
};
-- 
1.9.1



[PATCH v1 1/2] crypto: mediatek - remove redundant clock setting

2017-05-31 Thread Ryder Lee
This patch removes redundant clock setting for 'clk_ethif', which is
the parent of 'clk_cryp'. Hence, we just need to handle its child.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-platform.c | 10 +-
 drivers/crypto/mediatek/mtk-platform.h |  2 --
 2 files changed, 1 insertion(+), 11 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-platform.c 
b/drivers/crypto/mediatek/mtk-platform.c
index b6ecc28..9b4437c 100644
--- a/drivers/crypto/mediatek/mtk-platform.c
+++ b/drivers/crypto/mediatek/mtk-platform.c
@@ -504,19 +504,14 @@ static int mtk_crypto_probe(struct platform_device *pdev)
}
}
 
-   cryp->clk_ethif = devm_clk_get(>dev, "ethif");
cryp->clk_cryp = devm_clk_get(>dev, "cryp");
-   if (IS_ERR(cryp->clk_ethif) || IS_ERR(cryp->clk_cryp))
+   if (IS_ERR(cryp->clk_cryp))
return -EPROBE_DEFER;
 
cryp->dev = >dev;
pm_runtime_enable(cryp->dev);
pm_runtime_get_sync(cryp->dev);
 
-   err = clk_prepare_enable(cryp->clk_ethif);
-   if (err)
-   goto err_clk_ethif;
-
err = clk_prepare_enable(cryp->clk_cryp);
if (err)
goto err_clk_cryp;
@@ -559,8 +554,6 @@ static int mtk_crypto_probe(struct platform_device *pdev)
 err_resource:
clk_disable_unprepare(cryp->clk_cryp);
 err_clk_cryp:
-   clk_disable_unprepare(cryp->clk_ethif);
-err_clk_ethif:
pm_runtime_put_sync(cryp->dev);
pm_runtime_disable(cryp->dev);
 
@@ -576,7 +569,6 @@ static int mtk_crypto_remove(struct platform_device *pdev)
mtk_desc_dma_free(cryp);
 
clk_disable_unprepare(cryp->clk_cryp);
-   clk_disable_unprepare(cryp->clk_ethif);
 
pm_runtime_put_sync(cryp->dev);
pm_runtime_disable(cryp->dev);
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 303c152..f0831f1 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -200,7 +200,6 @@ struct mtk_sha_rec {
  * struct mtk_cryp - Cryptographic device
  * @base:  pointer to mapped register I/O base
  * @dev:   pointer to device
- * @clk_ethif: pointer to ethif clock
  * @clk_cryp:  pointer to crypto clock
  * @irq:   global system and rings IRQ
  * @ring:  pointer to descriptor rings
@@ -215,7 +214,6 @@ struct mtk_sha_rec {
 struct mtk_cryp {
void __iomem *base;
struct device *dev;
-   struct clk *clk_ethif;
struct clk *clk_cryp;
int irq[MTK_IRQ_NUM];
 
-- 
1.9.1



[PATCH v1 0/2] remove mediatek ethif clock in crypto driver

2017-05-31 Thread Ryder Lee
This patch series only remove redundant clock setting in mediatek crypto driver.

Changes since v1:
-rework the commit message.

Ryder Lee (2):
  crypto: mediatek - remove redundant clock setting
  dt-bindings: crypto: remove mediatek ethif clock

 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt |  8 +++-
 drivers/crypto/mediatek/mtk-platform.c   | 10 +-
 drivers/crypto/mediatek/mtk-platform.h   |  2 --
 3 files changed, 4 insertions(+), 16 deletions(-)

-- 
1.9.1



Re: [PATCH 2/2] crypto: mediatek - update DT binding documentation

2017-05-31 Thread Ryder Lee
On Wed, 2017-05-31 at 13:46 -0500, Rob Herring wrote:
> On Fri, May 26, 2017 at 05:43:09PM +0800, Ryder Lee wrote:
> > This patch removes unnecessary clock in binding file.
> 
> Why is the clock unnecessary? A h/w block either has a clock or doesn't.
> 
> The subject needs some work. Perhaps "dt-bindings: crypto: remove 
> mediatek ethif clock".
> 
> Rob

I'll write more clearly, thanks.

Ryder.




[PATCH 1/2] crypto: mediatek - remove redundant clock setting

2017-05-26 Thread Ryder Lee
This patch removes redundant clock setting for 'clk_ethif', which is
the parent for 'clk_cryp'. Hence, we just need to handle its child.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-platform.c | 10 +-
 drivers/crypto/mediatek/mtk-platform.h |  2 --
 2 files changed, 1 insertion(+), 11 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-platform.c 
b/drivers/crypto/mediatek/mtk-platform.c
index b6ecc28..9b4437c 100644
--- a/drivers/crypto/mediatek/mtk-platform.c
+++ b/drivers/crypto/mediatek/mtk-platform.c
@@ -504,19 +504,14 @@ static int mtk_crypto_probe(struct platform_device *pdev)
}
}
 
-   cryp->clk_ethif = devm_clk_get(>dev, "ethif");
cryp->clk_cryp = devm_clk_get(>dev, "cryp");
-   if (IS_ERR(cryp->clk_ethif) || IS_ERR(cryp->clk_cryp))
+   if (IS_ERR(cryp->clk_cryp))
return -EPROBE_DEFER;
 
cryp->dev = >dev;
pm_runtime_enable(cryp->dev);
pm_runtime_get_sync(cryp->dev);
 
-   err = clk_prepare_enable(cryp->clk_ethif);
-   if (err)
-   goto err_clk_ethif;
-
err = clk_prepare_enable(cryp->clk_cryp);
if (err)
goto err_clk_cryp;
@@ -559,8 +554,6 @@ static int mtk_crypto_probe(struct platform_device *pdev)
 err_resource:
clk_disable_unprepare(cryp->clk_cryp);
 err_clk_cryp:
-   clk_disable_unprepare(cryp->clk_ethif);
-err_clk_ethif:
pm_runtime_put_sync(cryp->dev);
pm_runtime_disable(cryp->dev);
 
@@ -576,7 +569,6 @@ static int mtk_crypto_remove(struct platform_device *pdev)
mtk_desc_dma_free(cryp);
 
clk_disable_unprepare(cryp->clk_cryp);
-   clk_disable_unprepare(cryp->clk_ethif);
 
pm_runtime_put_sync(cryp->dev);
pm_runtime_disable(cryp->dev);
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 303c152..f0831f1 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -200,7 +200,6 @@ struct mtk_sha_rec {
  * struct mtk_cryp - Cryptographic device
  * @base:  pointer to mapped register I/O base
  * @dev:   pointer to device
- * @clk_ethif: pointer to ethif clock
  * @clk_cryp:  pointer to crypto clock
  * @irq:   global system and rings IRQ
  * @ring:  pointer to descriptor rings
@@ -215,7 +214,6 @@ struct mtk_sha_rec {
 struct mtk_cryp {
void __iomem *base;
struct device *dev;
-   struct clk *clk_ethif;
struct clk *clk_cryp;
int irq[MTK_IRQ_NUM];
 
-- 
1.9.1



[PATCH 2/2] crypto: mediatek - update DT binding documentation

2017-05-26 Thread Ryder Lee
This patch removes unnecessary clock in binding file.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt | 8 +++-
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt 
b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
index c204725..450da36 100644
--- a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
+++ b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
@@ -6,8 +6,7 @@ Required properties:
 - interrupts: Should contain the five crypto engines interrupts in numeric
order. These are global system and four descriptor rings.
 - clocks: the clock used by the core
-- clock-names: the names of the clock listed in the clocks property. These are
-   "ethif", "cryp"
+- clock-names: Must contain "cryp".
 - power-domains: Must contain a reference to the PM domain.
 
 
@@ -20,8 +19,7 @@ Example:
 ,
 ,
 ;
-   clocks = < CLK_TOP_ETHIF_SEL>,
-< CLK_ETHSYS_CRYPTO>;
-   clock-names = "ethif","cryp";
+   clocks = < CLK_ETHSYS_CRYPTO>;
+   clock-names = "cryp";
power-domains = < MT2701_POWER_DOMAIN_ETH>;
};
-- 
1.9.1



[PATCH v1 3/8] crypto: mediatek - make mtk_sha_xmit() more generic

2017-03-08 Thread Ryder Lee
This is a transitional patch. It merges mtk_sha_xmit() and mtk_sha_xmit2()
to make transmit function more generic.
In addition, res->buf and cryp->tmp_dma in mtk_sha_xmit() are useless, since
crypto engine writes the result digests into ctx->tfm.digest instead of
res->buf. It's better to remove it.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-platform.h |   4 --
 drivers/crypto/mediatek/mtk-sha.c  | 116 ++---
 2 files changed, 34 insertions(+), 86 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 3bbe9b0..78ce54e 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -202,8 +202,6 @@ struct mtk_sha_rec {
  * @sha:   pointer to operation record of SHA
  * @aes_list:  device list of AES
  * @sha_list:  device list of SHA
- * @tmp:   pointer to temporary buffer for internal use
- * @tmp_dma:   DMA address of temporary buffer
  * @rec:   it's used to select SHA record for tfm
  *
  * Structure storing cryptographic device information.
@@ -222,8 +220,6 @@ struct mtk_cryp {
struct list_head aes_list;
struct list_head sha_list;
 
-   void *tmp;
-   dma_addr_t tmp_dma;
bool rec;
 };
 
diff --git a/drivers/crypto/mediatek/mtk-sha.c 
b/drivers/crypto/mediatek/mtk-sha.c
index b4f0c62..ef6fb20 100644
--- a/drivers/crypto/mediatek/mtk-sha.c
+++ b/drivers/crypto/mediatek/mtk-sha.c
@@ -17,7 +17,6 @@
 
 #define SHA_ALIGN_MSK  (sizeof(u32) - 1)
 #define SHA_QUEUE_SIZE 512
-#define SHA_TMP_BUF_SIZE   512
 #define SHA_BUF_SIZE   ((u32)PAGE_SIZE)
 
 #define SHA_OP_UPDATE  1
@@ -319,7 +318,7 @@ static void mtk_sha_info_init(struct mtk_sha_reqctx *ctx)
  */
 static int mtk_sha_info_update(struct mtk_cryp *cryp,
   struct mtk_sha_rec *sha,
-  size_t len)
+  size_t len1, size_t len2)
 {
struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
struct mtk_sha_info *info = >info;
@@ -331,11 +330,11 @@ static int mtk_sha_info_update(struct mtk_cryp *cryp,
ct->ctrl[0] &= ~SHA_TFM_START;
 
ctx->ct_hdr &= ~SHA_DATA_LEN_MSK;
-   ctx->ct_hdr |= cpu_to_le32(len);
+   ctx->ct_hdr |= cpu_to_le32(len1 + len2);
ct->cmd[0] &= ~SHA_DATA_LEN_MSK;
-   ct->cmd[0] |= cpu_to_le32(len);
+   ct->cmd[0] |= cpu_to_le32(len1 + len2);
 
-   ctx->digcnt += len;
+   ctx->digcnt += len1;
 
ctx->ct_dma = dma_map_single(cryp->dev, info, sizeof(*info),
 DMA_BIDIRECTIONAL);
@@ -422,67 +421,24 @@ static int mtk_sha_init(struct ahash_request *req)
 }
 
 static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
-   dma_addr_t addr, size_t len)
+   dma_addr_t addr1, size_t len1,
+   dma_addr_t addr2, size_t len2)
 {
struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
struct mtk_ring *ring = cryp->ring[sha->id];
struct mtk_desc *cmd = ring->cmd_base + ring->cmd_pos;
struct mtk_desc *res = ring->res_base + ring->res_pos;
-   int err;
-
-   err = mtk_sha_info_update(cryp, sha, len);
-   if (err)
-   return err;
-
-   /* Fill in the command/result descriptors */
-   res->hdr = MTK_DESC_FIRST | MTK_DESC_LAST | MTK_DESC_BUF_LEN(len);
-   res->buf = cpu_to_le32(cryp->tmp_dma);
-
-   cmd->hdr = MTK_DESC_FIRST | MTK_DESC_LAST | MTK_DESC_BUF_LEN(len) |
-  MTK_DESC_CT_LEN(ctx->ct_size);
-
-   cmd->buf = cpu_to_le32(addr);
-   cmd->ct = cpu_to_le32(ctx->ct_dma);
-   cmd->ct_hdr = ctx->ct_hdr;
-   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;
-   /*
-* Make sure that all changes to the DMA ring are done before we
-* start engine.
-*/
-   wmb();
-   /* Start DMA transfer */
-   mtk_sha_write(cryp, RDR_PREP_COUNT(sha->id), MTK_DESC_CNT(1));
-   mtk_sha_write(cryp, CDR_PREP_COUNT(sha->id), MTK_DESC_CNT(1));
-
-   return -EINPROGRESS;
-}
-
-static int mtk_sha_xmit2(struct mtk_cryp *cryp,
-struct mtk_sha_rec *sha,
-struct mtk_sha_reqctx *ctx,
-size_t len1, size_t len2)
-{
-   struct mtk_ring *ring = cryp->ring[sha->id];
-   struct mtk_desc *cmd = ring->cmd_base + ring->cmd_pos;
-   struct mtk_desc *res = ring->res_base + ring->res_pos;
-   int err;
+   int err, count = 0;
 
-   err = mtk_sha_info_update(cryp, sha, len1 + len2);
+  

[PATCH v1 6/8] crypto: mediatek - fix error handling in mtk_aes_complete()

2017-03-08 Thread Ryder Lee
This patch fixes how errors should be handled by mtk_aes_complete().

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 39 +++
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 140e9a3..6a0180d 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -248,6 +248,17 @@ static inline void mtk_aes_restore_sg(const struct 
mtk_aes_dma *dma)
sg->length += dma->remainder;
 }
 
+static inline int mtk_aes_complete(struct mtk_cryp *cryp,
+  struct mtk_aes_rec *aes,
+  int err)
+{
+   aes->flags &= ~AES_FLAGS_BUSY;
+   aes->areq->complete(aes->areq, err);
+   /* Handle new request */
+   tasklet_schedule(>queue_task);
+   return err;
+}
+
 /*
  * Write descriptors for processing. This will configure the engine, load
  * the transform information and then start the packet processing.
@@ -352,7 +363,7 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
ctx->ct_dma = dma_map_single(cryp->dev, >ct, sizeof(ctx->ct),
 DMA_TO_DEVICE);
if (unlikely(dma_mapping_error(cryp->dev, ctx->ct_dma)))
-   return -EINVAL;
+   goto exit;
 
ctx->tfm_dma = dma_map_single(cryp->dev, >tfm, sizeof(ctx->tfm),
  DMA_TO_DEVICE);
@@ -389,8 +400,8 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 tfm_map_err:
dma_unmap_single(cryp->dev, ctx->ct_dma, sizeof(ctx->ct),
 DMA_TO_DEVICE);
-
-   return -EINVAL;
+exit:
+   return mtk_aes_complete(cryp, aes, -EINVAL);
 }
 
 /* Initialize transform information of CBC/ECB/CTR mode */
@@ -467,7 +478,7 @@ static int mtk_aes_dma(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes,
padlen = mtk_aes_padlen(len);
 
if (len + padlen > AES_BUF_SIZE)
-   return -ENOMEM;
+   return mtk_aes_complete(cryp, aes, -ENOMEM);
 
if (!src_aligned) {
sg_copy_to_buffer(src, sg_nents(src), aes->buf, len);
@@ -527,14 +538,10 @@ static int mtk_aes_handle_queue(struct mtk_cryp *cryp, u8 
id,
return ctx->start(cryp, aes);
 }
 
-static int mtk_aes_complete(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
+static int mtk_aes_transfer_complete(struct mtk_cryp *cryp,
+struct mtk_aes_rec *aes)
 {
-   aes->flags &= ~AES_FLAGS_BUSY;
-   aes->areq->complete(aes->areq, 0);
-
-   /* Handle new request */
-   tasklet_schedule(>queue_task);
-   return 0;
+   return mtk_aes_complete(cryp, aes, 0);
 }
 
 static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
@@ -543,7 +550,7 @@ static int mtk_aes_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
struct mtk_aes_reqctx *rctx = ablkcipher_request_ctx(req);
 
mtk_aes_set_mode(aes, rctx);
-   aes->resume = mtk_aes_complete;
+   aes->resume = mtk_aes_transfer_complete;
 
return mtk_aes_dma(cryp, aes, req->src, req->dst, req->nbytes);
 }
@@ -568,7 +575,7 @@ static int mtk_aes_ctr_transfer(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes)
/* Check for transfer completion. */
cctx->offset += aes->total;
if (cctx->offset >= req->nbytes)
-   return mtk_aes_complete(cryp, aes);
+   return mtk_aes_transfer_complete(cryp, aes);
 
/* Compute data length. */
datalen = req->nbytes - cctx->offset;
@@ -602,7 +609,6 @@ static int mtk_aes_ctr_transfer(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes)
cctx->iv[3] = cpu_to_be32(ctr);
crypto_inc((u8 *)cctx->iv, AES_BLOCK_SIZE);
}
-   aes->resume = mtk_aes_ctr_transfer;
 
return mtk_aes_dma(cryp, aes, src, dst, datalen);
 }
@@ -618,6 +624,7 @@ static int mtk_aes_ctr_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
memcpy(cctx->iv, req->info, AES_BLOCK_SIZE);
cctx->offset = 0;
aes->total = 0;
+   aes->resume = mtk_aes_ctr_transfer;
 
return mtk_aes_ctr_transfer(cryp, aes);
 }
@@ -859,7 +866,7 @@ static int mtk_aes_gcm_dma(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes,
 
if (!src_aligned || !dst_aligned) {
if (aes->total > AES_BUF_SIZE)
-   return -ENOMEM;
+   return mtk_aes_complete(cryp, aes, -ENOMEM);
 
if (!src_aligned) {
sg_copy_to_buffer(src, sg_nents(src), aes->buf, len);
@@ -905,7 +912,7 @@ static int mtk_aes_gcm_start(s

[PATCH v1 5/8] crypto: mediatek - add queue_task tasklet

2017-03-08 Thread Ryder Lee
This patch adds 'queue_task' to dequeue crypto requset. This will help to
avoid directly calling mtk_aes_handle_queue() / mtk_sha_handle_queue()
from done tasklet or error handler.

In order to avoid confusion, the new code properly renames DMA completion
"task" to "done_task".

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 20 
 drivers/crypto/mediatek/mtk-platform.h | 12 
 drivers/crypto/mediatek/mtk-sha.c  | 19 +++
 3 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 21f3e59..140e9a3 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -533,7 +533,8 @@ static int mtk_aes_complete(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
aes->areq->complete(aes->areq, 0);
 
/* Handle new request */
-   return mtk_aes_handle_queue(cryp, aes->id, NULL);
+   tasklet_schedule(>queue_task);
+   return 0;
 }
 
 static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
@@ -1094,6 +1095,13 @@ static void mtk_aes_gcm_exit(struct crypto_aead *aead)
},
 };
 
+static void mtk_aes_queue_task(unsigned long data)
+{
+   struct mtk_aes_rec *aes = (struct mtk_aes_rec *)data;
+
+   mtk_aes_handle_queue(aes->cryp, aes->id, NULL);
+}
+
 static void mtk_aes_done_task(unsigned long data)
 {
struct mtk_aes_rec *aes = (struct mtk_aes_rec *)data;
@@ -1116,7 +1124,7 @@ static irqreturn_t mtk_aes_irq(int irq, void *dev_id)
mtk_aes_write(cryp, RDR_THRESH(aes->id),
  MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
 
-   tasklet_schedule(>task);
+   tasklet_schedule(>done_task);
} else {
dev_warn(cryp->dev, "AES interrupt when no active requests.\n");
}
@@ -1149,7 +1157,9 @@ static int mtk_aes_record_init(struct mtk_cryp *cryp)
spin_lock_init([i]->lock);
crypto_init_queue([i]->queue, AES_QUEUE_SIZE);
 
-   tasklet_init([i]->task, mtk_aes_done_task,
+   tasklet_init([i]->queue_task, mtk_aes_queue_task,
+(unsigned long)aes[i]);
+   tasklet_init([i]->done_task, mtk_aes_done_task,
 (unsigned long)aes[i]);
}
 
@@ -1173,7 +1183,9 @@ static void mtk_aes_record_free(struct mtk_cryp *cryp)
int i;
 
for (i = 0; i < MTK_REC_NUM; i++) {
-   tasklet_kill(>aes[i]->task);
+   tasklet_kill(>aes[i]->done_task);
+   tasklet_kill(>aes[i]->queue_task);
+
free_page((unsigned long)cryp->aes[i]->buf);
kfree(cryp->aes[i]);
}
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 218e30d..cc98c2c 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -128,7 +128,8 @@ struct mtk_aes_dma {
  * @cryp:  pointer to Cryptographic device
  * @queue: crypto request queue
  * @areq:  pointer to async request
- * @task:  the tasklet is use in AES interrupt
+ * @done_task: the tasklet is use in AES interrupt
+ * @queue_task:the tasklet is used to dequeue request
  * @ctx:   pointer to current context
  * @src:   the structure that holds source sg list info
  * @dst:   the structure that holds destination sg list info
@@ -147,7 +148,8 @@ struct mtk_aes_rec {
struct mtk_cryp *cryp;
struct crypto_queue queue;
struct crypto_async_request *areq;
-   struct tasklet_struct task;
+   struct tasklet_struct done_task;
+   struct tasklet_struct queue_task;
struct mtk_aes_base_ctx *ctx;
struct mtk_aes_dma src;
struct mtk_aes_dma dst;
@@ -171,7 +173,8 @@ struct mtk_aes_rec {
  * @cryp:  pointer to Cryptographic device
  * @queue: crypto request queue
  * @req:   pointer to ahash request
- * @task:  the tasklet is use in SHA interrupt
+ * @done_task: the tasklet is use in SHA interrupt
+ * @queue_task:the tasklet is used to dequeue request
  * @id:the current use of ring
  * @flags: it's describing SHA operation state
  * @lock:  the async queue lock
@@ -182,7 +185,8 @@ struct mtk_sha_rec {
struct mtk_cryp *cryp;
struct crypto_queue queue;
struct ahash_request *req;
-   struct tasklet_struct task;
+   struct tasklet_struct done_task;
+   struct tasklet_struct queue_task;
 
u8 id;
unsigned long flags;
diff --git a/drivers/crypto/mediatek/mtk-sha.c 
b/drivers/crypto/mediatek/mtk-sha.c
index 0884d62..dd3582b 100644
--- a/drivers/crypto/mediatek/mtk-sha.c
+++ b/drivers/crypto/mediatek/mtk-sha.c
@@

[PATCH v1 2/8] crypto: mediatek - add MTK_* prefix and correct annotations.

2017-03-08 Thread Ryder Lee
Dummy patch to add MTK_* prefix to ring enum and fix incorrect annotations.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 12 ++--
 drivers/crypto/mediatek/mtk-platform.c | 12 ++--
 drivers/crypto/mediatek/mtk-platform.h | 26 +-
 drivers/crypto/mediatek/mtk-sha.c  | 14 +++---
 4 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index e67881f..b57b68f 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -1152,8 +1152,8 @@ static int mtk_aes_record_init(struct mtk_cryp *cryp)
}
 
/* Link to ring0 and ring1 respectively */
-   aes[0]->id = RING0;
-   aes[1]->id = RING1;
+   aes[0]->id = MTK_RING0;
+   aes[1]->id = MTK_RING1;
 
return 0;
 
@@ -1221,14 +1221,14 @@ int mtk_cipher_alg_register(struct mtk_cryp *cryp)
if (ret)
goto err_record;
 
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING0], mtk_aes_irq,
+   ret = devm_request_irq(cryp->dev, cryp->irq[MTK_RING0], mtk_aes_irq,
   0, "mtk-aes", cryp->aes[0]);
if (ret) {
dev_err(cryp->dev, "unable to request AES irq.\n");
goto err_res;
}
 
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING1], mtk_aes_irq,
+   ret = devm_request_irq(cryp->dev, cryp->irq[MTK_RING1], mtk_aes_irq,
   0, "mtk-aes", cryp->aes[1]);
if (ret) {
dev_err(cryp->dev, "unable to request AES irq.\n");
@@ -1236,8 +1236,8 @@ int mtk_cipher_alg_register(struct mtk_cryp *cryp)
}
 
/* Enable ring0 and ring1 interrupt */
-   mtk_aes_write(cryp, AIC_ENABLE_SET(RING0), MTK_IRQ_RDR0);
-   mtk_aes_write(cryp, AIC_ENABLE_SET(RING1), MTK_IRQ_RDR1);
+   mtk_aes_write(cryp, AIC_ENABLE_SET(MTK_RING0), MTK_IRQ_RDR0);
+   mtk_aes_write(cryp, AIC_ENABLE_SET(MTK_RING1), MTK_IRQ_RDR1);
 
spin_lock(_aes.lock);
list_add_tail(>aes_list, _aes.dev_list);
diff --git a/drivers/crypto/mediatek/mtk-platform.c 
b/drivers/crypto/mediatek/mtk-platform.c
index a9c713d..50de335 100644
--- a/drivers/crypto/mediatek/mtk-platform.c
+++ b/drivers/crypto/mediatek/mtk-platform.c
@@ -334,7 +334,7 @@ static int mtk_packet_engine_setup(struct mtk_cryp *cryp)
/* Enable the 4 rings for the packet engines. */
mtk_desc_ring_link(cryp, 0xf);
 
-   for (i = 0; i < RING_MAX; i++) {
+   for (i = 0; i < MTK_RING_MAX; i++) {
mtk_cmd_desc_ring_setup(cryp, i, );
mtk_res_desc_ring_setup(cryp, i, );
}
@@ -359,7 +359,7 @@ static int mtk_aic_cap_check(struct mtk_cryp *cryp, int hw)
 {
u32 val;
 
-   if (hw == RING_MAX)
+   if (hw == MTK_RING_MAX)
val = readl(cryp->base + AIC_G_VERSION);
else
val = readl(cryp->base + AIC_VERSION(hw));
@@ -368,7 +368,7 @@ static int mtk_aic_cap_check(struct mtk_cryp *cryp, int hw)
if (val != MTK_AIC_VER11 && val != MTK_AIC_VER12)
return -ENXIO;
 
-   if (hw == RING_MAX)
+   if (hw == MTK_RING_MAX)
val = readl(cryp->base + AIC_G_OPTIONS);
else
val = readl(cryp->base + AIC_OPTIONS(hw));
@@ -389,7 +389,7 @@ static int mtk_aic_init(struct mtk_cryp *cryp, int hw)
return err;
 
/* Disable all interrupts and set initial configuration */
-   if (hw == RING_MAX) {
+   if (hw == MTK_RING_MAX) {
writel(0, cryp->base + AIC_G_ENABLE_CTRL);
writel(0, cryp->base + AIC_G_POL_CTRL);
writel(0, cryp->base + AIC_G_TYPE_CTRL);
@@ -431,7 +431,7 @@ static void mtk_desc_dma_free(struct mtk_cryp *cryp)
 {
int i;
 
-   for (i = 0; i < RING_MAX; i++) {
+   for (i = 0; i < MTK_RING_MAX; i++) {
dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ,
  cryp->ring[i]->res_base,
  cryp->ring[i]->res_dma);
@@ -447,7 +447,7 @@ static int mtk_desc_ring_alloc(struct mtk_cryp *cryp)
struct mtk_ring **ring = cryp->ring;
int i, err = ENOMEM;
 
-   for (i = 0; i < RING_MAX; i++) {
+   for (i = 0; i < MTK_RING_MAX; i++) {
ring[i] = kzalloc(sizeof(**ring), GFP_KERNEL);
if (!ring[i])
goto err_cleanup;
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index d2a1db6..3bbe9b0 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -38,14 +38,14 @@
  * Ring 2/3 are used by SHA.
  */
 enum {
-   RING0 = 

[PATCH v1 4/8] crypto: mediatek - simplify descriptor ring management

2017-03-08 Thread Ryder Lee
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: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 14 --
 drivers/crypto/mediatek/mtk-platform.c |  3 +++
 drivers/crypto/mediatek/mtk-platform.h |  8 
 drivers/crypto/mediatek/mtk-sha.c  | 35 ++
 4 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index b57b68f..21f3e59 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -262,7 +262,7 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 
/* Write command descriptors */
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->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)
cmd->tfm = cpu_to_le32(aes->ctx->tfm_dma);
}
 
-   if (++ring->cmd_pos == MTK_DESC_NUM)
-   ring->cmd_pos = 0;
+   /* Shift ring buffer and check boundary */
+   if (++ring->cmd_next == ring->cmd_base + MTK_DESC_NUM)
+   ring->cmd_next = ring->cmd_base;
}
cmd->hdr |= MTK_DESC_LAST;
 
/* Prepare result descriptors */
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->buf = cpu_to_le32(sg_dma_address(dsg));
 
if (nents == 0)
res->hdr |= MTK_DESC_FIRST;
 
-   if (++ring->res_pos == MTK_DESC_NUM)
-   ring->res_pos = 0;
+   /* Shift ring buffer and check boundary */
+   if (++ring->res_next == ring->res_base + MTK_DESC_NUM)
+   ring->res_next = ring->res_base;
}
res->hdr |= MTK_DESC_LAST;
 
diff --git a/drivers/crypto/mediatek/mtk-platform.c 
b/drivers/crypto/mediatek/mtk-platform.c
index 50de335..b6ecc28 100644
--- a/drivers/crypto/mediatek/mtk-platform.c
+++ b/drivers/crypto/mediatek/mtk-platform.c
@@ -465,6 +465,9 @@ static int mtk_desc_ring_alloc(struct mtk_cryp *cryp)
   GFP_KERNEL);
if (!ring[i]->res_base)
goto err_cleanup;
+
+   ring[i]->cmd_next = ring[i]->cmd_base;
+   ring[i]->res_next = ring[i]->res_base;
}
return 0;
 
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 78ce54e..218e30d 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -84,11 +84,11 @@ struct mtk_desc {
 /**
  * struct mtk_ring - Descriptor ring
  * @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_pos:   current position in the command descriptor ring
  * @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_pos:   current position in the result descriptor ring
  *
  * A descriptor ring is a circular buffer that is used to manage
  * one or more descriptors. There are two type of descriptor rings;
@@ -96,11 +96,11 @@ struct mtk_desc {
  */
 struct mtk_ring {
struct mtk_desc *cmd_base;
+   struct mtk_desc *cmd_next;
dma_addr_t cmd_dma;
-   u32 cmd_pos;
struct mtk_desc *res_base;
+   struct mtk_desc *res_next;
dma_addr_t res_dma;
-   u32 res_pos;
 };
 
 /**
diff --git a/drivers/crypto/mediatek/mtk-sha.c 
b/drivers/crypto/mediatek/mtk-sha.c
index ef6fb20..0884d62 100644
--- a/drivers/crypto/mediatek/mtk-sha.c
+++ b/drivers/crypto/mediatek/mtk-sha.c
@@ -152,6 +152,21 @@ static inline void mtk_sha_write(struct mtk_cryp *cryp,
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,
+   

[PATCH v1 1/8] crypto: mediatek - rework interrupt handler

2017-03-08 Thread Ryder Lee
This patch removes redundant task that used to handle interrupt
from ring manager, so that the same task/handler can be shared.
It also uses aes->id and sha-id to distinguish interrupt sources.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 73 +++---
 drivers/crypto/mediatek/mtk-platform.h |  4 ++
 drivers/crypto/mediatek/mtk-sha.c  | 73 ++
 3 files changed, 49 insertions(+), 101 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 3a47cdb..e67881f 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -1092,55 +1092,26 @@ static void mtk_aes_gcm_exit(struct crypto_aead *aead)
},
 };
 
-static void mtk_aes_enc_task(unsigned long data)
+static void mtk_aes_done_task(unsigned long data)
 {
-   struct mtk_cryp *cryp = (struct mtk_cryp *)data;
-   struct mtk_aes_rec *aes = cryp->aes[0];
+   struct mtk_aes_rec *aes = (struct mtk_aes_rec *)data;
+   struct mtk_cryp *cryp = aes->cryp;
 
mtk_aes_unmap(cryp, aes);
aes->resume(cryp, aes);
 }
 
-static void mtk_aes_dec_task(unsigned long data)
+static irqreturn_t mtk_aes_irq(int irq, void *dev_id)
 {
-   struct mtk_cryp *cryp = (struct mtk_cryp *)data;
-   struct mtk_aes_rec *aes = cryp->aes[1];
+   struct mtk_aes_rec *aes  = (struct mtk_aes_rec *)dev_id;
+   struct mtk_cryp *cryp = aes->cryp;
+   u32 val = mtk_aes_read(cryp, RDR_STAT(aes->id));
 
-   mtk_aes_unmap(cryp, aes);
-   aes->resume(cryp, aes);
-}
-
-static irqreturn_t mtk_aes_enc_irq(int irq, void *dev_id)
-{
-   struct mtk_cryp *cryp = (struct mtk_cryp *)dev_id;
-   struct mtk_aes_rec *aes = cryp->aes[0];
-   u32 val = mtk_aes_read(cryp, RDR_STAT(RING0));
-
-   mtk_aes_write(cryp, RDR_STAT(RING0), val);
-
-   if (likely(AES_FLAGS_BUSY & aes->flags)) {
-   mtk_aes_write(cryp, RDR_PROC_COUNT(RING0), MTK_CNT_RST);
-   mtk_aes_write(cryp, RDR_THRESH(RING0),
- MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
-
-   tasklet_schedule(>task);
-   } else {
-   dev_warn(cryp->dev, "AES interrupt when no active requests.\n");
-   }
-   return IRQ_HANDLED;
-}
-
-static irqreturn_t mtk_aes_dec_irq(int irq, void *dev_id)
-{
-   struct mtk_cryp *cryp = (struct mtk_cryp *)dev_id;
-   struct mtk_aes_rec *aes = cryp->aes[1];
-   u32 val = mtk_aes_read(cryp, RDR_STAT(RING1));
-
-   mtk_aes_write(cryp, RDR_STAT(RING1), val);
+   mtk_aes_write(cryp, RDR_STAT(aes->id), val);
 
if (likely(AES_FLAGS_BUSY & aes->flags)) {
-   mtk_aes_write(cryp, RDR_PROC_COUNT(RING1), MTK_CNT_RST);
-   mtk_aes_write(cryp, RDR_THRESH(RING1),
+   mtk_aes_write(cryp, RDR_PROC_COUNT(aes->id), MTK_CNT_RST);
+   mtk_aes_write(cryp, RDR_THRESH(aes->id),
  MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
 
tasklet_schedule(>task);
@@ -1171,14 +1142,18 @@ static int mtk_aes_record_init(struct mtk_cryp *cryp)
if (!aes[i]->buf)
goto err_cleanup;
 
-   aes[i]->id = i;
+   aes[i]->cryp = cryp;
 
spin_lock_init([i]->lock);
crypto_init_queue([i]->queue, AES_QUEUE_SIZE);
+
+   tasklet_init([i]->task, mtk_aes_done_task,
+(unsigned long)aes[i]);
}
 
-   tasklet_init([0]->task, mtk_aes_enc_task, (unsigned long)cryp);
-   tasklet_init([1]->task, mtk_aes_dec_task, (unsigned long)cryp);
+   /* Link to ring0 and ring1 respectively */
+   aes[0]->id = RING0;
+   aes[1]->id = RING1;
 
return 0;
 
@@ -1246,19 +1221,17 @@ int mtk_cipher_alg_register(struct mtk_cryp *cryp)
if (ret)
goto err_record;
 
-   /* Ring0 is use by encryption record */
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING0], mtk_aes_enc_irq,
-  IRQF_TRIGGER_LOW, "mtk-aes", cryp);
+   ret = devm_request_irq(cryp->dev, cryp->irq[RING0], mtk_aes_irq,
+  0, "mtk-aes", cryp->aes[0]);
if (ret) {
-   dev_err(cryp->dev, "unable to request AES encryption irq.\n");
+   dev_err(cryp->dev, "unable to request AES irq.\n");
goto err_res;
}
 
-   /* Ring1 is use by decryption record */
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING1], mtk_aes_dec_irq,
-  IRQF_TRIGGER_LOW, "mtk-aes", cryp);
+   ret = devm_request_irq(cryp->dev, cryp->irq[RING1], mtk_aes_irq,
+  

[PATCH v1 0/8] improve performances on mediatek crypto driver

2017-03-08 Thread Ryder Lee
Hi all,

Some patches of this series improve the performances whereas others
clean up code and refine data structure to make it more efficient

Changes since v1:
- drop OFB and CFB patch

Ryder Lee (8):
  crypto: mediatek - rework interrupt handler
  crypto: mediatek - add MTK_* prefix and correct annotations.
  crypto: mediatek - make mtk_sha_xmit() more generic
  crypto: mediatek - simplify descriptor ring management
  crypto: mediatek - add queue_task tasklet
  crypto: mediatek - fix error handling in mtk_aes_complete()
  crypto: mediatek - add mtk_aes_gcm_tag_verify()
  crypto: mediatek - make hardware operation flow more efficient

 drivers/crypto/mediatek/mtk-aes.c  | 421 +
 drivers/crypto/mediatek/mtk-platform.c |  15 +-
 drivers/crypto/mediatek/mtk-platform.h |  56 +++--
 drivers/crypto/mediatek/mtk-sha.c  | 309 +---
 4 files changed, 369 insertions(+), 432 deletions(-)

-- 
1.9.1



[PATCH v1 7/8] crypto: mediatek - add mtk_aes_gcm_tag_verify()

2017-03-08 Thread Ryder Lee
This patch adds mtk_aes_gcm_tag_verify() which is used to compare
authenticated tag.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 24 ++--
 drivers/crypto/mediatek/mtk-platform.h |  2 ++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 6a0180d..8f3efa5 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -70,6 +70,8 @@
 #define AES_FLAGS_ENCRYPT  BIT(4)
 #define AES_FLAGS_BUSY BIT(5)
 
+#define AES_AUTH_TAG_ERR   cpu_to_le32(BIT(26))
+
 /**
  * Command token(CT) is a set of hardware instructions that
  * are used to control engine's processing flow of AES.
@@ -306,6 +308,9 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
}
res->hdr |= MTK_DESC_LAST;
 
+   /* Pointer to current result descriptor */
+   ring->res_prev = res;
+
/* Prepare enough space for authenticated tag */
if (aes->flags & AES_FLAGS_GCM)
res->hdr += AES_BLOCK_SIZE;
@@ -799,6 +804,19 @@ static int mtk_aes_ctr_cra_init(struct crypto_tfm *tfm)
return container_of(ctx, struct mtk_aes_gcm_ctx, base);
 }
 
+/*
+ * Engine will verify and compare tag automatically, so we just need
+ * to check returned status which stored in the result descriptor.
+ */
+static int mtk_aes_gcm_tag_verify(struct mtk_cryp *cryp,
+ struct mtk_aes_rec *aes)
+{
+   u32 status = cryp->ring[aes->id]->res_prev->ct;
+
+   return mtk_aes_complete(cryp, aes, (status & AES_AUTH_TAG_ERR) ?
+   -EBADMSG : 0);
+}
+
 /* Initialize transform information of GCM mode */
 static void mtk_aes_gcm_info_init(struct mtk_cryp *cryp,
  struct mtk_aes_rec *aes,
@@ -902,6 +920,8 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 
if (aes->flags & AES_FLAGS_ENCRYPT) {
u32 tag[4];
+
+   aes->resume = mtk_aes_transfer_complete;
/* Compute total process length. */
aes->total = len + gctx->authsize;
/* Compute text length. */
@@ -909,10 +929,10 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes)
/* Hardware will append authenticated tag to output buffer */
scatterwalk_map_and_copy(tag, req->dst, len, gctx->authsize, 1);
} else {
+   aes->resume = mtk_aes_gcm_tag_verify;
aes->total = len;
gctx->textlen = req->cryptlen - gctx->authsize;
}
-   aes->resume = mtk_aes_transfer_complete;
 
return mtk_aes_gcm_dma(cryp, aes, req->src, req->dst, len);
 }
@@ -925,7 +945,7 @@ static int mtk_aes_gcm_crypt(struct aead_request *req, u64 
mode)
rctx->mode = AES_FLAGS_GCM | mode;
 
return mtk_aes_handle_queue(ctx->cryp, !!(mode & AES_FLAGS_ENCRYPT),
-   >base);
+   >base);
 }
 
 static void mtk_gcm_setkey_done(struct crypto_async_request *req, int err)
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index cc98c2c..303c152 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -88,6 +88,7 @@ struct mtk_desc {
  * @cmd_dma:   DMA address of command descriptor ring
  * @res_base:  pointer to result descriptor ring base
  * @res_next:  pointer to the next result descriptor
+ * @res_prev:  pointer to the previous result descriptor
  * @res_dma:   DMA address of result descriptor ring
  *
  * A descriptor ring is a circular buffer that is used to manage
@@ -100,6 +101,7 @@ struct mtk_ring {
dma_addr_t cmd_dma;
struct mtk_desc *res_base;
struct mtk_desc *res_next;
+   struct mtk_desc *res_prev;
dma_addr_t res_dma;
 };
 
-- 
1.9.1



[PATCH v1 8/8] crypto: mediatek - make hardware operation flow more efficient

2017-03-08 Thread Ryder Lee
This patch refines data structures, which are used to control engine's
data path, to make it more efficient. Hence current change are:

- gathers the broken pieces of structures 'mtk_aes_ct''mtk_aes_tfm'
into struct mtk_aes_info hence avoiding additional DMA-mapping.

- adds 'keymode' in struct mtk_aes_base_ctx. When .setkey() callback is
called, we store keybit setting in keymode. Doing so, there is no need
to check keylen second time in mtk_aes_info_init() / mtk_aes_gcm_info_init().

Besides, this patch also removes unused macro definitions and adds helper
inline function to write security information(key, IV,...) to info->state.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 263 ++
 drivers/crypto/mediatek/mtk-sha.c |  90 ++---
 2 files changed, 165 insertions(+), 188 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 8f3efa5..9e845e8 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -19,13 +19,10 @@
 #define AES_BUF_ORDER  2
 #define AES_BUF_SIZE   ((PAGE_SIZE << AES_BUF_ORDER) \
& ~(AES_BLOCK_SIZE - 1))
+#define AES_MAX_STATE_BUF_SIZE SIZE_IN_WORDS(AES_KEYSIZE_256 + \
+   AES_BLOCK_SIZE * 2)
+#define AES_MAX_CT_SIZE6
 
-/* AES command token size */
-#define AES_CT_SIZE_ECB2
-#define AES_CT_SIZE_CBC3
-#define AES_CT_SIZE_CTR3
-#define AES_CT_SIZE_GCM_OUT5
-#define AES_CT_SIZE_GCM_IN 6
 #define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
 
 /* AES-CBC/ECB/CTR command token */
@@ -50,6 +47,8 @@
 #define AES_TFM_128BITScpu_to_le32(0xb << 16)
 #define AES_TFM_192BITScpu_to_le32(0xd << 16)
 #define AES_TFM_256BITScpu_to_le32(0xf << 16)
+#define AES_TFM_GHASH_DIGEST   cpu_to_le32(0x2 << 21)
+#define AES_TFM_GHASH  cpu_to_le32(0x4 << 23)
 /* AES transform information word 1 fields */
 #define AES_TFM_ECBcpu_to_le32(0x0 << 0)
 #define AES_TFM_CBCcpu_to_le32(0x1 << 0)
@@ -59,10 +58,9 @@
 #define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)   /* using IV 0-3 
*/
 #define AES_TFM_IV_CTR_MODEcpu_to_le32(0x1 << 10)
 #define AES_TFM_ENC_HASH   cpu_to_le32(0x1 << 17)
-#define AES_TFM_GHASH_DIG  cpu_to_le32(0x2 << 21)
-#define AES_TFM_GHASH  cpu_to_le32(0x4 << 23)
 
 /* AES flags */
+#define AES_FLAGS_CIPHER_MSK   GENMASK(2, 0)
 #define AES_FLAGS_ECB  BIT(0)
 #define AES_FLAGS_CBC  BIT(1)
 #define AES_FLAGS_CTR  BIT(2)
@@ -73,18 +71,12 @@
 #define AES_AUTH_TAG_ERR   cpu_to_le32(BIT(26))
 
 /**
- * Command token(CT) is a set of hardware instructions that
- * are used to control engine's processing flow of AES.
- *
- * Transform information(TFM) is used to define AES state and
- * contains all keys and initial vectors.
- *
- * The engine requires CT and TFM to do:
- * - Commands decoding and control of the engine's data path.
- * - Coordinating hardware data fetch and store operations.
- * - Result token construction and output.
+ * mtk_aes_info - hardware information of AES
+ * @cmd:   command token, hardware instruction
+ * @tfm:   transform state of cipher algorithm.
+ * @state: contains keys and initial vectors.
  *
- * Memory map of GCM's TFM:
+ * Memory layout of GCM buffer:
  * /---\
  * |  AES KEY  | 128/196/256 bits
  * |---|
@@ -92,14 +84,16 @@
  * |---|
  * |IVs| 4 * 4 bytes
  * \---/
+ *
+ * The engine requires all these info to do:
+ * - Commands decoding and control of the engine's data path.
+ * - Coordinating hardware data fetch and store operations.
+ * - Result token construction and output.
  */
-struct mtk_aes_ct {
-   __le32 cmd[AES_CT_SIZE_GCM_IN];
-};
-
-struct mtk_aes_tfm {
-   __le32 ctrl[2];
-   __le32 state[SIZE_IN_WORDS(AES_KEYSIZE_256 + AES_BLOCK_SIZE * 2)];
+struct mtk_aes_info {
+   __le32 cmd[AES_MAX_CT_SIZE];
+   __le32 tfm[2];
+   __le32 state[AES_MAX_STATE_BUF_SIZE];
 };
 
 struct mtk_aes_reqctx {
@@ -109,11 +103,12 @@ struct mtk_aes_reqctx {
 struct mtk_aes_base_ctx {
struct mtk_cryp *cryp;
u32 keylen;
+   __le32 keymode;
+
mtk_aes_fn start;
 
-   struct mtk_aes_ct ct;
+   struct mtk_aes_info info;
dma_addr_t ct_dma;
-   struct mtk_aes_tfm tfm;
dma_addr_t tfm_dma;
 
__le32 ct_hdr;
@@ -250,6 +245,22 @@ static inline void mtk_aes_restore_sg(const struct 
mtk_aes_dma *dma)
sg->length += dma->remainder;
 }
 
+static inline void mtk_aes_write_state_le(__le32 *dst, const u32 *src, u32 
size)
+{
+   int i;
+
+   for (i = 0; i < SIZE_IN_WORDS(size); i++)
+   

[PATCH 6/9] crypto: mediatek - fix error handling in mtk_aes_complete()

2017-02-20 Thread Ryder Lee
This patch fixes how errors should be handled by mtk_aes_complete().

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 39 +++
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 140e9a3..6a0180d 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -248,6 +248,17 @@ static inline void mtk_aes_restore_sg(const struct 
mtk_aes_dma *dma)
sg->length += dma->remainder;
 }
 
+static inline int mtk_aes_complete(struct mtk_cryp *cryp,
+  struct mtk_aes_rec *aes,
+  int err)
+{
+   aes->flags &= ~AES_FLAGS_BUSY;
+   aes->areq->complete(aes->areq, err);
+   /* Handle new request */
+   tasklet_schedule(>queue_task);
+   return err;
+}
+
 /*
  * Write descriptors for processing. This will configure the engine, load
  * the transform information and then start the packet processing.
@@ -352,7 +363,7 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
ctx->ct_dma = dma_map_single(cryp->dev, >ct, sizeof(ctx->ct),
 DMA_TO_DEVICE);
if (unlikely(dma_mapping_error(cryp->dev, ctx->ct_dma)))
-   return -EINVAL;
+   goto exit;
 
ctx->tfm_dma = dma_map_single(cryp->dev, >tfm, sizeof(ctx->tfm),
  DMA_TO_DEVICE);
@@ -389,8 +400,8 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 tfm_map_err:
dma_unmap_single(cryp->dev, ctx->ct_dma, sizeof(ctx->ct),
 DMA_TO_DEVICE);
-
-   return -EINVAL;
+exit:
+   return mtk_aes_complete(cryp, aes, -EINVAL);
 }
 
 /* Initialize transform information of CBC/ECB/CTR mode */
@@ -467,7 +478,7 @@ static int mtk_aes_dma(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes,
padlen = mtk_aes_padlen(len);
 
if (len + padlen > AES_BUF_SIZE)
-   return -ENOMEM;
+   return mtk_aes_complete(cryp, aes, -ENOMEM);
 
if (!src_aligned) {
sg_copy_to_buffer(src, sg_nents(src), aes->buf, len);
@@ -527,14 +538,10 @@ static int mtk_aes_handle_queue(struct mtk_cryp *cryp, u8 
id,
return ctx->start(cryp, aes);
 }
 
-static int mtk_aes_complete(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
+static int mtk_aes_transfer_complete(struct mtk_cryp *cryp,
+struct mtk_aes_rec *aes)
 {
-   aes->flags &= ~AES_FLAGS_BUSY;
-   aes->areq->complete(aes->areq, 0);
-
-   /* Handle new request */
-   tasklet_schedule(>queue_task);
-   return 0;
+   return mtk_aes_complete(cryp, aes, 0);
 }
 
 static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
@@ -543,7 +550,7 @@ static int mtk_aes_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
struct mtk_aes_reqctx *rctx = ablkcipher_request_ctx(req);
 
mtk_aes_set_mode(aes, rctx);
-   aes->resume = mtk_aes_complete;
+   aes->resume = mtk_aes_transfer_complete;
 
return mtk_aes_dma(cryp, aes, req->src, req->dst, req->nbytes);
 }
@@ -568,7 +575,7 @@ static int mtk_aes_ctr_transfer(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes)
/* Check for transfer completion. */
cctx->offset += aes->total;
if (cctx->offset >= req->nbytes)
-   return mtk_aes_complete(cryp, aes);
+   return mtk_aes_transfer_complete(cryp, aes);
 
/* Compute data length. */
datalen = req->nbytes - cctx->offset;
@@ -602,7 +609,6 @@ static int mtk_aes_ctr_transfer(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes)
cctx->iv[3] = cpu_to_be32(ctr);
crypto_inc((u8 *)cctx->iv, AES_BLOCK_SIZE);
}
-   aes->resume = mtk_aes_ctr_transfer;
 
return mtk_aes_dma(cryp, aes, src, dst, datalen);
 }
@@ -618,6 +624,7 @@ static int mtk_aes_ctr_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
memcpy(cctx->iv, req->info, AES_BLOCK_SIZE);
cctx->offset = 0;
aes->total = 0;
+   aes->resume = mtk_aes_ctr_transfer;
 
return mtk_aes_ctr_transfer(cryp, aes);
 }
@@ -859,7 +866,7 @@ static int mtk_aes_gcm_dma(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes,
 
if (!src_aligned || !dst_aligned) {
if (aes->total > AES_BUF_SIZE)
-   return -ENOMEM;
+   return mtk_aes_complete(cryp, aes, -ENOMEM);
 
if (!src_aligned) {
sg_copy_to_buffer(src, sg_nents(src), aes->buf, len);
@@ -905,7 +912,7 @@ static int mtk_aes_gcm_start(s

[PATCH 7/9] crypto: mediatek - add mtk_aes_gcm_tag_verify()

2017-02-20 Thread Ryder Lee
This patch adds mtk_aes_gcm_tag_verify() which is used to compare
authenticated tag.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 24 ++--
 drivers/crypto/mediatek/mtk-platform.h |  2 ++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 6a0180d..8f3efa5 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -70,6 +70,8 @@
 #define AES_FLAGS_ENCRYPT  BIT(4)
 #define AES_FLAGS_BUSY BIT(5)
 
+#define AES_AUTH_TAG_ERR   cpu_to_le32(BIT(26))
+
 /**
  * Command token(CT) is a set of hardware instructions that
  * are used to control engine's processing flow of AES.
@@ -306,6 +308,9 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
}
res->hdr |= MTK_DESC_LAST;
 
+   /* Pointer to current result descriptor */
+   ring->res_prev = res;
+
/* Prepare enough space for authenticated tag */
if (aes->flags & AES_FLAGS_GCM)
res->hdr += AES_BLOCK_SIZE;
@@ -799,6 +804,19 @@ static int mtk_aes_ctr_cra_init(struct crypto_tfm *tfm)
return container_of(ctx, struct mtk_aes_gcm_ctx, base);
 }
 
+/*
+ * Engine will verify and compare tag automatically, so we just need
+ * to check returned status which stored in the result descriptor.
+ */
+static int mtk_aes_gcm_tag_verify(struct mtk_cryp *cryp,
+ struct mtk_aes_rec *aes)
+{
+   u32 status = cryp->ring[aes->id]->res_prev->ct;
+
+   return mtk_aes_complete(cryp, aes, (status & AES_AUTH_TAG_ERR) ?
+   -EBADMSG : 0);
+}
+
 /* Initialize transform information of GCM mode */
 static void mtk_aes_gcm_info_init(struct mtk_cryp *cryp,
  struct mtk_aes_rec *aes,
@@ -902,6 +920,8 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 
if (aes->flags & AES_FLAGS_ENCRYPT) {
u32 tag[4];
+
+   aes->resume = mtk_aes_transfer_complete;
/* Compute total process length. */
aes->total = len + gctx->authsize;
/* Compute text length. */
@@ -909,10 +929,10 @@ static int mtk_aes_gcm_start(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes)
/* Hardware will append authenticated tag to output buffer */
scatterwalk_map_and_copy(tag, req->dst, len, gctx->authsize, 1);
} else {
+   aes->resume = mtk_aes_gcm_tag_verify;
aes->total = len;
gctx->textlen = req->cryptlen - gctx->authsize;
}
-   aes->resume = mtk_aes_transfer_complete;
 
return mtk_aes_gcm_dma(cryp, aes, req->src, req->dst, len);
 }
@@ -925,7 +945,7 @@ static int mtk_aes_gcm_crypt(struct aead_request *req, u64 
mode)
rctx->mode = AES_FLAGS_GCM | mode;
 
return mtk_aes_handle_queue(ctx->cryp, !!(mode & AES_FLAGS_ENCRYPT),
-   >base);
+   >base);
 }
 
 static void mtk_gcm_setkey_done(struct crypto_async_request *req, int err)
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index cc98c2c..303c152 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -88,6 +88,7 @@ struct mtk_desc {
  * @cmd_dma:   DMA address of command descriptor ring
  * @res_base:  pointer to result descriptor ring base
  * @res_next:  pointer to the next result descriptor
+ * @res_prev:  pointer to the previous result descriptor
  * @res_dma:   DMA address of result descriptor ring
  *
  * A descriptor ring is a circular buffer that is used to manage
@@ -100,6 +101,7 @@ struct mtk_ring {
dma_addr_t cmd_dma;
struct mtk_desc *res_base;
struct mtk_desc *res_next;
+   struct mtk_desc *res_prev;
dma_addr_t res_dma;
 };
 
-- 
1.9.1



[PATCH 5/9] crypto: mediatek - add queue_task tasklet

2017-02-20 Thread Ryder Lee
This patch adds 'queue_task' to dequeue crypto requset. This will help to
avoid directly calling mtk_aes_handle_queue() / mtk_sha_handle_queue()
from done tasklet or error handler.

In order to avoid confusion, the new code properly renames DMA completion
"task" to "done_task".

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 20 
 drivers/crypto/mediatek/mtk-platform.h | 12 
 drivers/crypto/mediatek/mtk-sha.c  | 19 +++
 3 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 21f3e59..140e9a3 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -533,7 +533,8 @@ static int mtk_aes_complete(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
aes->areq->complete(aes->areq, 0);
 
/* Handle new request */
-   return mtk_aes_handle_queue(cryp, aes->id, NULL);
+   tasklet_schedule(>queue_task);
+   return 0;
 }
 
 static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
@@ -1094,6 +1095,13 @@ static void mtk_aes_gcm_exit(struct crypto_aead *aead)
},
 };
 
+static void mtk_aes_queue_task(unsigned long data)
+{
+   struct mtk_aes_rec *aes = (struct mtk_aes_rec *)data;
+
+   mtk_aes_handle_queue(aes->cryp, aes->id, NULL);
+}
+
 static void mtk_aes_done_task(unsigned long data)
 {
struct mtk_aes_rec *aes = (struct mtk_aes_rec *)data;
@@ -1116,7 +1124,7 @@ static irqreturn_t mtk_aes_irq(int irq, void *dev_id)
mtk_aes_write(cryp, RDR_THRESH(aes->id),
  MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
 
-   tasklet_schedule(>task);
+   tasklet_schedule(>done_task);
} else {
dev_warn(cryp->dev, "AES interrupt when no active requests.\n");
}
@@ -1149,7 +1157,9 @@ static int mtk_aes_record_init(struct mtk_cryp *cryp)
spin_lock_init([i]->lock);
crypto_init_queue([i]->queue, AES_QUEUE_SIZE);
 
-   tasklet_init([i]->task, mtk_aes_done_task,
+   tasklet_init([i]->queue_task, mtk_aes_queue_task,
+(unsigned long)aes[i]);
+   tasklet_init([i]->done_task, mtk_aes_done_task,
 (unsigned long)aes[i]);
}
 
@@ -1173,7 +1183,9 @@ static void mtk_aes_record_free(struct mtk_cryp *cryp)
int i;
 
for (i = 0; i < MTK_REC_NUM; i++) {
-   tasklet_kill(>aes[i]->task);
+   tasklet_kill(>aes[i]->done_task);
+   tasklet_kill(>aes[i]->queue_task);
+
free_page((unsigned long)cryp->aes[i]->buf);
kfree(cryp->aes[i]);
}
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 218e30d..cc98c2c 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -128,7 +128,8 @@ struct mtk_aes_dma {
  * @cryp:  pointer to Cryptographic device
  * @queue: crypto request queue
  * @areq:  pointer to async request
- * @task:  the tasklet is use in AES interrupt
+ * @done_task: the tasklet is use in AES interrupt
+ * @queue_task:the tasklet is used to dequeue request
  * @ctx:   pointer to current context
  * @src:   the structure that holds source sg list info
  * @dst:   the structure that holds destination sg list info
@@ -147,7 +148,8 @@ struct mtk_aes_rec {
struct mtk_cryp *cryp;
struct crypto_queue queue;
struct crypto_async_request *areq;
-   struct tasklet_struct task;
+   struct tasklet_struct done_task;
+   struct tasklet_struct queue_task;
struct mtk_aes_base_ctx *ctx;
struct mtk_aes_dma src;
struct mtk_aes_dma dst;
@@ -171,7 +173,8 @@ struct mtk_aes_rec {
  * @cryp:  pointer to Cryptographic device
  * @queue: crypto request queue
  * @req:   pointer to ahash request
- * @task:  the tasklet is use in SHA interrupt
+ * @done_task: the tasklet is use in SHA interrupt
+ * @queue_task:the tasklet is used to dequeue request
  * @id:the current use of ring
  * @flags: it's describing SHA operation state
  * @lock:  the async queue lock
@@ -182,7 +185,8 @@ struct mtk_sha_rec {
struct mtk_cryp *cryp;
struct crypto_queue queue;
struct ahash_request *req;
-   struct tasklet_struct task;
+   struct tasklet_struct done_task;
+   struct tasklet_struct queue_task;
 
u8 id;
unsigned long flags;
diff --git a/drivers/crypto/mediatek/mtk-sha.c 
b/drivers/crypto/mediatek/mtk-sha.c
index 0884d62..dd3582b 100644
--- a/drivers/crypto/mediatek/mtk-sha.c
+++ b/drivers/crypto/mediatek/mtk-sha.c
@@

[PATCH 8/9] crypto: mediatek - make hardware operation flow more efficient

2017-02-20 Thread Ryder Lee
This patch refines data structures, which are used to control engine's
data path, to make it more efficient. Hence current change are:

- gathers the broken pieces of structures 'mtk_aes_ct''mtk_aes_tfm'
into struct mtk_aes_info hence avoiding additional DMA-mapping.

- adds 'keymode' in struct mtk_aes_base_ctx. When .setkey() callback is
called, we store keybit setting in keymode. Doing so, there is no need
to check keylen second time in mtk_aes_info_init() / mtk_aes_gcm_info_init().

Besides, this patch also removes unused macro definitions and adds helper
inline function to write security information(key, IV,...) to info->state.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 263 ++
 drivers/crypto/mediatek/mtk-sha.c |  90 ++---
 2 files changed, 165 insertions(+), 188 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 8f3efa5..9e845e8 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -19,13 +19,10 @@
 #define AES_BUF_ORDER  2
 #define AES_BUF_SIZE   ((PAGE_SIZE << AES_BUF_ORDER) \
& ~(AES_BLOCK_SIZE - 1))
+#define AES_MAX_STATE_BUF_SIZE SIZE_IN_WORDS(AES_KEYSIZE_256 + \
+   AES_BLOCK_SIZE * 2)
+#define AES_MAX_CT_SIZE6
 
-/* AES command token size */
-#define AES_CT_SIZE_ECB2
-#define AES_CT_SIZE_CBC3
-#define AES_CT_SIZE_CTR3
-#define AES_CT_SIZE_GCM_OUT5
-#define AES_CT_SIZE_GCM_IN 6
 #define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
 
 /* AES-CBC/ECB/CTR command token */
@@ -50,6 +47,8 @@
 #define AES_TFM_128BITScpu_to_le32(0xb << 16)
 #define AES_TFM_192BITScpu_to_le32(0xd << 16)
 #define AES_TFM_256BITScpu_to_le32(0xf << 16)
+#define AES_TFM_GHASH_DIGEST   cpu_to_le32(0x2 << 21)
+#define AES_TFM_GHASH  cpu_to_le32(0x4 << 23)
 /* AES transform information word 1 fields */
 #define AES_TFM_ECBcpu_to_le32(0x0 << 0)
 #define AES_TFM_CBCcpu_to_le32(0x1 << 0)
@@ -59,10 +58,9 @@
 #define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)   /* using IV 0-3 
*/
 #define AES_TFM_IV_CTR_MODEcpu_to_le32(0x1 << 10)
 #define AES_TFM_ENC_HASH   cpu_to_le32(0x1 << 17)
-#define AES_TFM_GHASH_DIG  cpu_to_le32(0x2 << 21)
-#define AES_TFM_GHASH  cpu_to_le32(0x4 << 23)
 
 /* AES flags */
+#define AES_FLAGS_CIPHER_MSK   GENMASK(2, 0)
 #define AES_FLAGS_ECB  BIT(0)
 #define AES_FLAGS_CBC  BIT(1)
 #define AES_FLAGS_CTR  BIT(2)
@@ -73,18 +71,12 @@
 #define AES_AUTH_TAG_ERR   cpu_to_le32(BIT(26))
 
 /**
- * Command token(CT) is a set of hardware instructions that
- * are used to control engine's processing flow of AES.
- *
- * Transform information(TFM) is used to define AES state and
- * contains all keys and initial vectors.
- *
- * The engine requires CT and TFM to do:
- * - Commands decoding and control of the engine's data path.
- * - Coordinating hardware data fetch and store operations.
- * - Result token construction and output.
+ * mtk_aes_info - hardware information of AES
+ * @cmd:   command token, hardware instruction
+ * @tfm:   transform state of cipher algorithm.
+ * @state: contains keys and initial vectors.
  *
- * Memory map of GCM's TFM:
+ * Memory layout of GCM buffer:
  * /---\
  * |  AES KEY  | 128/196/256 bits
  * |---|
@@ -92,14 +84,16 @@
  * |---|
  * |IVs| 4 * 4 bytes
  * \---/
+ *
+ * The engine requires all these info to do:
+ * - Commands decoding and control of the engine's data path.
+ * - Coordinating hardware data fetch and store operations.
+ * - Result token construction and output.
  */
-struct mtk_aes_ct {
-   __le32 cmd[AES_CT_SIZE_GCM_IN];
-};
-
-struct mtk_aes_tfm {
-   __le32 ctrl[2];
-   __le32 state[SIZE_IN_WORDS(AES_KEYSIZE_256 + AES_BLOCK_SIZE * 2)];
+struct mtk_aes_info {
+   __le32 cmd[AES_MAX_CT_SIZE];
+   __le32 tfm[2];
+   __le32 state[AES_MAX_STATE_BUF_SIZE];
 };
 
 struct mtk_aes_reqctx {
@@ -109,11 +103,12 @@ struct mtk_aes_reqctx {
 struct mtk_aes_base_ctx {
struct mtk_cryp *cryp;
u32 keylen;
+   __le32 keymode;
+
mtk_aes_fn start;
 
-   struct mtk_aes_ct ct;
+   struct mtk_aes_info info;
dma_addr_t ct_dma;
-   struct mtk_aes_tfm tfm;
dma_addr_t tfm_dma;
 
__le32 ct_hdr;
@@ -250,6 +245,22 @@ static inline void mtk_aes_restore_sg(const struct 
mtk_aes_dma *dma)
sg->length += dma->remainder;
 }
 
+static inline void mtk_aes_write_state_le(__le32 *dst, const u32 *src, u32 
size)
+{
+   int i;
+
+   for (i = 0; i < SIZE_IN_WORDS(size); i++)
+   

[PATCH 9/9] crypto: mediatek - add support to OFB mode and CFB128 mode

2017-02-20 Thread Ryder Lee
This patch adds support to OFB mode and CFB128 mode.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 84 ---
 1 file changed, 78 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 9e845e8..c2bdfb9 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -25,7 +25,7 @@
 
 #define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
 
-/* AES-CBC/ECB/CTR command token */
+/* AES-CBC/ECB/CTR/OFB/CFB command token */
 #define AES_CMD0   cpu_to_le32(0x0500)
 #define AES_CMD1   cpu_to_le32(0x2d06)
 #define AES_CMD2   cpu_to_le32(0xe4a63806)
@@ -52,6 +52,8 @@
 /* AES transform information word 1 fields */
 #define AES_TFM_ECBcpu_to_le32(0x0 << 0)
 #define AES_TFM_CBCcpu_to_le32(0x1 << 0)
+#define AES_TFM_OFBcpu_to_le32(0x4 << 0)
+#define AES_TFM_CFB128 cpu_to_le32(0x5 << 0)
 #define AES_TFM_CTR_INIT   cpu_to_le32(0x2 << 0)   /* init counter to 1 */
 #define AES_TFM_CTR_LOAD   cpu_to_le32(0x6 << 0)   /* load/reuse counter */
 #define AES_TFM_3IVcpu_to_le32(0x7 << 5)   /* using IV 0-2 */
@@ -60,13 +62,15 @@
 #define AES_TFM_ENC_HASH   cpu_to_le32(0x1 << 17)
 
 /* AES flags */
-#define AES_FLAGS_CIPHER_MSK   GENMASK(2, 0)
+#define AES_FLAGS_CIPHER_MSK   GENMASK(4, 0)
 #define AES_FLAGS_ECB  BIT(0)
 #define AES_FLAGS_CBC  BIT(1)
 #define AES_FLAGS_CTR  BIT(2)
-#define AES_FLAGS_GCM  BIT(3)
-#define AES_FLAGS_ENCRYPT  BIT(4)
-#define AES_FLAGS_BUSY BIT(5)
+#define AES_FLAGS_OFB  BIT(3)
+#define AES_FLAGS_CFB128   BIT(4)
+#define AES_FLAGS_GCM  BIT(5)
+#define AES_FLAGS_ENCRYPT  BIT(6)
+#define AES_FLAGS_BUSY BIT(7)
 
 #define AES_AUTH_TAG_ERR   cpu_to_le32(BIT(26))
 
@@ -412,7 +416,7 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
return mtk_aes_complete(cryp, aes, -EINVAL);
 }
 
-/* Initialize transform information of CBC/ECB/CTR mode */
+/* Initialize transform information of CBC/ECB/CTR/OFB/CFB mode */
 static void mtk_aes_info_init(struct mtk_cryp *cryp, struct mtk_aes_rec *aes,
  size_t len)
 {
@@ -441,6 +445,12 @@ static void mtk_aes_info_init(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes,
case AES_FLAGS_CTR:
info->tfm[1] = AES_TFM_CTR_LOAD;
goto ctr;
+   case AES_FLAGS_OFB:
+   info->tfm[1] = AES_TFM_OFB;
+   break;
+   case AES_FLAGS_CFB128:
+   info->tfm[1] = AES_TFM_CFB128;
+   break;
 
default:
/* Should not happen... */
@@ -699,6 +709,26 @@ static int mtk_aes_ctr_decrypt(struct ablkcipher_request 
*req)
return mtk_aes_crypt(req, AES_FLAGS_CTR);
 }
 
+static int mtk_aes_ofb_encrypt(struct ablkcipher_request *req)
+{
+   return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_OFB);
+}
+
+static int mtk_aes_ofb_decrypt(struct ablkcipher_request *req)
+{
+   return mtk_aes_crypt(req, AES_FLAGS_OFB);
+}
+
+static int mtk_aes_cfb_encrypt(struct ablkcipher_request *req)
+{
+   return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CFB128);
+}
+
+static int mtk_aes_cfb_decrypt(struct ablkcipher_request *req)
+{
+   return mtk_aes_crypt(req, AES_FLAGS_CFB128);
+}
+
 static int mtk_aes_cra_init(struct crypto_tfm *tfm)
 {
struct mtk_aes_ctx *ctx = crypto_tfm_ctx(tfm);
@@ -794,6 +824,48 @@ static int mtk_aes_ctr_cra_init(struct crypto_tfm *tfm)
.decrypt= mtk_aes_ctr_decrypt,
}
 },
+{
+   .cra_name   = "ofb(aes)",
+   .cra_driver_name= "ofb-aes-mtk",
+   .cra_priority   = 400,
+   .cra_flags  = CRYPTO_ALG_TYPE_ABLKCIPHER |
+ CRYPTO_ALG_ASYNC,
+   .cra_init   = mtk_aes_cra_init,
+   .cra_blocksize  = 1,
+   .cra_ctxsize= sizeof(struct mtk_aes_ctx),
+   .cra_alignmask  = 0xf,
+   .cra_type   = _ablkcipher_type,
+   .cra_module = THIS_MODULE,
+   .cra_u.ablkcipher = {
+   .min_keysize= AES_MIN_KEY_SIZE,
+   .max_keysize= AES_MAX_KEY_SIZE,
+   .ivsize = AES_BLOCK_SIZE,
+   .setkey = mtk_aes_setkey,
+   .encrypt= mtk_aes_ofb_encrypt,
+   .decrypt= mtk_aes_ofb_decrypt,
+   }
+},
+{
+   .cra_name   = "cfb(aes)",
+   .cra_driver_name= "cfb-aes-mtk",
+   .cra_priority   = 400,
+   .cra_flags  = CRYPTO_ALG_TYPE_ABLKCIPHER |
+ CR

[PATCH 1/9] crypto: mediatek - rework interrupt handler

2017-02-20 Thread Ryder Lee
This patch removes redundant task that used to handle interrupt
from ring manager, so that the same task/handler can be shared.
It also uses aes->id and sha-id to distinguish interrupt sources.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 73 +++---
 drivers/crypto/mediatek/mtk-platform.h |  4 ++
 drivers/crypto/mediatek/mtk-sha.c  | 73 ++
 3 files changed, 49 insertions(+), 101 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 3a47cdb..e67881f 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -1092,55 +1092,26 @@ static void mtk_aes_gcm_exit(struct crypto_aead *aead)
},
 };
 
-static void mtk_aes_enc_task(unsigned long data)
+static void mtk_aes_done_task(unsigned long data)
 {
-   struct mtk_cryp *cryp = (struct mtk_cryp *)data;
-   struct mtk_aes_rec *aes = cryp->aes[0];
+   struct mtk_aes_rec *aes = (struct mtk_aes_rec *)data;
+   struct mtk_cryp *cryp = aes->cryp;
 
mtk_aes_unmap(cryp, aes);
aes->resume(cryp, aes);
 }
 
-static void mtk_aes_dec_task(unsigned long data)
+static irqreturn_t mtk_aes_irq(int irq, void *dev_id)
 {
-   struct mtk_cryp *cryp = (struct mtk_cryp *)data;
-   struct mtk_aes_rec *aes = cryp->aes[1];
+   struct mtk_aes_rec *aes  = (struct mtk_aes_rec *)dev_id;
+   struct mtk_cryp *cryp = aes->cryp;
+   u32 val = mtk_aes_read(cryp, RDR_STAT(aes->id));
 
-   mtk_aes_unmap(cryp, aes);
-   aes->resume(cryp, aes);
-}
-
-static irqreturn_t mtk_aes_enc_irq(int irq, void *dev_id)
-{
-   struct mtk_cryp *cryp = (struct mtk_cryp *)dev_id;
-   struct mtk_aes_rec *aes = cryp->aes[0];
-   u32 val = mtk_aes_read(cryp, RDR_STAT(RING0));
-
-   mtk_aes_write(cryp, RDR_STAT(RING0), val);
-
-   if (likely(AES_FLAGS_BUSY & aes->flags)) {
-   mtk_aes_write(cryp, RDR_PROC_COUNT(RING0), MTK_CNT_RST);
-   mtk_aes_write(cryp, RDR_THRESH(RING0),
- MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
-
-   tasklet_schedule(>task);
-   } else {
-   dev_warn(cryp->dev, "AES interrupt when no active requests.\n");
-   }
-   return IRQ_HANDLED;
-}
-
-static irqreturn_t mtk_aes_dec_irq(int irq, void *dev_id)
-{
-   struct mtk_cryp *cryp = (struct mtk_cryp *)dev_id;
-   struct mtk_aes_rec *aes = cryp->aes[1];
-   u32 val = mtk_aes_read(cryp, RDR_STAT(RING1));
-
-   mtk_aes_write(cryp, RDR_STAT(RING1), val);
+   mtk_aes_write(cryp, RDR_STAT(aes->id), val);
 
if (likely(AES_FLAGS_BUSY & aes->flags)) {
-   mtk_aes_write(cryp, RDR_PROC_COUNT(RING1), MTK_CNT_RST);
-   mtk_aes_write(cryp, RDR_THRESH(RING1),
+   mtk_aes_write(cryp, RDR_PROC_COUNT(aes->id), MTK_CNT_RST);
+   mtk_aes_write(cryp, RDR_THRESH(aes->id),
  MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE);
 
tasklet_schedule(>task);
@@ -1171,14 +1142,18 @@ static int mtk_aes_record_init(struct mtk_cryp *cryp)
if (!aes[i]->buf)
goto err_cleanup;
 
-   aes[i]->id = i;
+   aes[i]->cryp = cryp;
 
spin_lock_init([i]->lock);
crypto_init_queue([i]->queue, AES_QUEUE_SIZE);
+
+   tasklet_init([i]->task, mtk_aes_done_task,
+(unsigned long)aes[i]);
}
 
-   tasklet_init([0]->task, mtk_aes_enc_task, (unsigned long)cryp);
-   tasklet_init([1]->task, mtk_aes_dec_task, (unsigned long)cryp);
+   /* Link to ring0 and ring1 respectively */
+   aes[0]->id = RING0;
+   aes[1]->id = RING1;
 
return 0;
 
@@ -1246,19 +1221,17 @@ int mtk_cipher_alg_register(struct mtk_cryp *cryp)
if (ret)
goto err_record;
 
-   /* Ring0 is use by encryption record */
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING0], mtk_aes_enc_irq,
-  IRQF_TRIGGER_LOW, "mtk-aes", cryp);
+   ret = devm_request_irq(cryp->dev, cryp->irq[RING0], mtk_aes_irq,
+  0, "mtk-aes", cryp->aes[0]);
if (ret) {
-   dev_err(cryp->dev, "unable to request AES encryption irq.\n");
+   dev_err(cryp->dev, "unable to request AES irq.\n");
goto err_res;
}
 
-   /* Ring1 is use by decryption record */
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING1], mtk_aes_dec_irq,
-  IRQF_TRIGGER_LOW, "mtk-aes", cryp);
+   ret = devm_request_irq(cryp->dev, cryp->irq[RING1], mtk_aes_irq,
+  

[PATCH 3/9] crypto: mediatek - make mtk_sha_xmit() more generic

2017-02-20 Thread Ryder Lee
This is a transitional patch. It merges mtk_sha_xmit() and mtk_sha_xmit2()
to make transmit function more generic.
In addition, res->buf and cryp->tmp_dma in mtk_sha_xmit() are useless, since
crypto engine writes the result digests into ctx->tfm.digest instead of
res->buf. It's better to remove it.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-platform.h |   4 --
 drivers/crypto/mediatek/mtk-sha.c  | 116 ++---
 2 files changed, 34 insertions(+), 86 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 3bbe9b0..78ce54e 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -202,8 +202,6 @@ struct mtk_sha_rec {
  * @sha:   pointer to operation record of SHA
  * @aes_list:  device list of AES
  * @sha_list:  device list of SHA
- * @tmp:   pointer to temporary buffer for internal use
- * @tmp_dma:   DMA address of temporary buffer
  * @rec:   it's used to select SHA record for tfm
  *
  * Structure storing cryptographic device information.
@@ -222,8 +220,6 @@ struct mtk_cryp {
struct list_head aes_list;
struct list_head sha_list;
 
-   void *tmp;
-   dma_addr_t tmp_dma;
bool rec;
 };
 
diff --git a/drivers/crypto/mediatek/mtk-sha.c 
b/drivers/crypto/mediatek/mtk-sha.c
index b4f0c62..ef6fb20 100644
--- a/drivers/crypto/mediatek/mtk-sha.c
+++ b/drivers/crypto/mediatek/mtk-sha.c
@@ -17,7 +17,6 @@
 
 #define SHA_ALIGN_MSK  (sizeof(u32) - 1)
 #define SHA_QUEUE_SIZE 512
-#define SHA_TMP_BUF_SIZE   512
 #define SHA_BUF_SIZE   ((u32)PAGE_SIZE)
 
 #define SHA_OP_UPDATE  1
@@ -319,7 +318,7 @@ static void mtk_sha_info_init(struct mtk_sha_reqctx *ctx)
  */
 static int mtk_sha_info_update(struct mtk_cryp *cryp,
   struct mtk_sha_rec *sha,
-  size_t len)
+  size_t len1, size_t len2)
 {
struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
struct mtk_sha_info *info = >info;
@@ -331,11 +330,11 @@ static int mtk_sha_info_update(struct mtk_cryp *cryp,
ct->ctrl[0] &= ~SHA_TFM_START;
 
ctx->ct_hdr &= ~SHA_DATA_LEN_MSK;
-   ctx->ct_hdr |= cpu_to_le32(len);
+   ctx->ct_hdr |= cpu_to_le32(len1 + len2);
ct->cmd[0] &= ~SHA_DATA_LEN_MSK;
-   ct->cmd[0] |= cpu_to_le32(len);
+   ct->cmd[0] |= cpu_to_le32(len1 + len2);
 
-   ctx->digcnt += len;
+   ctx->digcnt += len1;
 
ctx->ct_dma = dma_map_single(cryp->dev, info, sizeof(*info),
 DMA_BIDIRECTIONAL);
@@ -422,67 +421,24 @@ static int mtk_sha_init(struct ahash_request *req)
 }
 
 static int mtk_sha_xmit(struct mtk_cryp *cryp, struct mtk_sha_rec *sha,
-   dma_addr_t addr, size_t len)
+   dma_addr_t addr1, size_t len1,
+   dma_addr_t addr2, size_t len2)
 {
struct mtk_sha_reqctx *ctx = ahash_request_ctx(sha->req);
struct mtk_ring *ring = cryp->ring[sha->id];
struct mtk_desc *cmd = ring->cmd_base + ring->cmd_pos;
struct mtk_desc *res = ring->res_base + ring->res_pos;
-   int err;
-
-   err = mtk_sha_info_update(cryp, sha, len);
-   if (err)
-   return err;
-
-   /* Fill in the command/result descriptors */
-   res->hdr = MTK_DESC_FIRST | MTK_DESC_LAST | MTK_DESC_BUF_LEN(len);
-   res->buf = cpu_to_le32(cryp->tmp_dma);
-
-   cmd->hdr = MTK_DESC_FIRST | MTK_DESC_LAST | MTK_DESC_BUF_LEN(len) |
-  MTK_DESC_CT_LEN(ctx->ct_size);
-
-   cmd->buf = cpu_to_le32(addr);
-   cmd->ct = cpu_to_le32(ctx->ct_dma);
-   cmd->ct_hdr = ctx->ct_hdr;
-   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;
-   /*
-* Make sure that all changes to the DMA ring are done before we
-* start engine.
-*/
-   wmb();
-   /* Start DMA transfer */
-   mtk_sha_write(cryp, RDR_PREP_COUNT(sha->id), MTK_DESC_CNT(1));
-   mtk_sha_write(cryp, CDR_PREP_COUNT(sha->id), MTK_DESC_CNT(1));
-
-   return -EINPROGRESS;
-}
-
-static int mtk_sha_xmit2(struct mtk_cryp *cryp,
-struct mtk_sha_rec *sha,
-struct mtk_sha_reqctx *ctx,
-size_t len1, size_t len2)
-{
-   struct mtk_ring *ring = cryp->ring[sha->id];
-   struct mtk_desc *cmd = ring->cmd_base + ring->cmd_pos;
-   struct mtk_desc *res = ring->res_base + ring->res_pos;
-   int err;
+   int err, count = 0;
 
-   err = mtk_sha_info_update(cryp, sha, len1 + len2);
+  

[PATCH 2/9] crypto: mediatek - add MTK_* prefix and correct annotations.

2017-02-20 Thread Ryder Lee
Dummy patch to add MTK_* prefix to ring enum and fix incorrect annotations.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 12 ++--
 drivers/crypto/mediatek/mtk-platform.c | 12 ++--
 drivers/crypto/mediatek/mtk-platform.h | 26 +-
 drivers/crypto/mediatek/mtk-sha.c  | 14 +++---
 4 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index e67881f..b57b68f 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -1152,8 +1152,8 @@ static int mtk_aes_record_init(struct mtk_cryp *cryp)
}
 
/* Link to ring0 and ring1 respectively */
-   aes[0]->id = RING0;
-   aes[1]->id = RING1;
+   aes[0]->id = MTK_RING0;
+   aes[1]->id = MTK_RING1;
 
return 0;
 
@@ -1221,14 +1221,14 @@ int mtk_cipher_alg_register(struct mtk_cryp *cryp)
if (ret)
goto err_record;
 
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING0], mtk_aes_irq,
+   ret = devm_request_irq(cryp->dev, cryp->irq[MTK_RING0], mtk_aes_irq,
   0, "mtk-aes", cryp->aes[0]);
if (ret) {
dev_err(cryp->dev, "unable to request AES irq.\n");
goto err_res;
}
 
-   ret = devm_request_irq(cryp->dev, cryp->irq[RING1], mtk_aes_irq,
+   ret = devm_request_irq(cryp->dev, cryp->irq[MTK_RING1], mtk_aes_irq,
   0, "mtk-aes", cryp->aes[1]);
if (ret) {
dev_err(cryp->dev, "unable to request AES irq.\n");
@@ -1236,8 +1236,8 @@ int mtk_cipher_alg_register(struct mtk_cryp *cryp)
}
 
/* Enable ring0 and ring1 interrupt */
-   mtk_aes_write(cryp, AIC_ENABLE_SET(RING0), MTK_IRQ_RDR0);
-   mtk_aes_write(cryp, AIC_ENABLE_SET(RING1), MTK_IRQ_RDR1);
+   mtk_aes_write(cryp, AIC_ENABLE_SET(MTK_RING0), MTK_IRQ_RDR0);
+   mtk_aes_write(cryp, AIC_ENABLE_SET(MTK_RING1), MTK_IRQ_RDR1);
 
spin_lock(_aes.lock);
list_add_tail(>aes_list, _aes.dev_list);
diff --git a/drivers/crypto/mediatek/mtk-platform.c 
b/drivers/crypto/mediatek/mtk-platform.c
index a9c713d..50de335 100644
--- a/drivers/crypto/mediatek/mtk-platform.c
+++ b/drivers/crypto/mediatek/mtk-platform.c
@@ -334,7 +334,7 @@ static int mtk_packet_engine_setup(struct mtk_cryp *cryp)
/* Enable the 4 rings for the packet engines. */
mtk_desc_ring_link(cryp, 0xf);
 
-   for (i = 0; i < RING_MAX; i++) {
+   for (i = 0; i < MTK_RING_MAX; i++) {
mtk_cmd_desc_ring_setup(cryp, i, );
mtk_res_desc_ring_setup(cryp, i, );
}
@@ -359,7 +359,7 @@ static int mtk_aic_cap_check(struct mtk_cryp *cryp, int hw)
 {
u32 val;
 
-   if (hw == RING_MAX)
+   if (hw == MTK_RING_MAX)
val = readl(cryp->base + AIC_G_VERSION);
else
val = readl(cryp->base + AIC_VERSION(hw));
@@ -368,7 +368,7 @@ static int mtk_aic_cap_check(struct mtk_cryp *cryp, int hw)
if (val != MTK_AIC_VER11 && val != MTK_AIC_VER12)
return -ENXIO;
 
-   if (hw == RING_MAX)
+   if (hw == MTK_RING_MAX)
val = readl(cryp->base + AIC_G_OPTIONS);
else
val = readl(cryp->base + AIC_OPTIONS(hw));
@@ -389,7 +389,7 @@ static int mtk_aic_init(struct mtk_cryp *cryp, int hw)
return err;
 
/* Disable all interrupts and set initial configuration */
-   if (hw == RING_MAX) {
+   if (hw == MTK_RING_MAX) {
writel(0, cryp->base + AIC_G_ENABLE_CTRL);
writel(0, cryp->base + AIC_G_POL_CTRL);
writel(0, cryp->base + AIC_G_TYPE_CTRL);
@@ -431,7 +431,7 @@ static void mtk_desc_dma_free(struct mtk_cryp *cryp)
 {
int i;
 
-   for (i = 0; i < RING_MAX; i++) {
+   for (i = 0; i < MTK_RING_MAX; i++) {
dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ,
  cryp->ring[i]->res_base,
  cryp->ring[i]->res_dma);
@@ -447,7 +447,7 @@ static int mtk_desc_ring_alloc(struct mtk_cryp *cryp)
struct mtk_ring **ring = cryp->ring;
int i, err = ENOMEM;
 
-   for (i = 0; i < RING_MAX; i++) {
+   for (i = 0; i < MTK_RING_MAX; i++) {
ring[i] = kzalloc(sizeof(**ring), GFP_KERNEL);
if (!ring[i])
goto err_cleanup;
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index d2a1db6..3bbe9b0 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -38,14 +38,14 @@
  * Ring 2/3 are used by SHA.
  */
 enum {
-   RING0 = 

[PATCH 4/9] crypto: mediatek - simplify descriptor ring management

2017-02-20 Thread Ryder Lee
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: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 14 --
 drivers/crypto/mediatek/mtk-platform.c |  3 +++
 drivers/crypto/mediatek/mtk-platform.h |  8 
 drivers/crypto/mediatek/mtk-sha.c  | 35 ++
 4 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index b57b68f..21f3e59 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -262,7 +262,7 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 
/* Write command descriptors */
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->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)
cmd->tfm = cpu_to_le32(aes->ctx->tfm_dma);
}
 
-   if (++ring->cmd_pos == MTK_DESC_NUM)
-   ring->cmd_pos = 0;
+   /* Shift ring buffer and check boundary */
+   if (++ring->cmd_next == ring->cmd_base + MTK_DESC_NUM)
+   ring->cmd_next = ring->cmd_base;
}
cmd->hdr |= MTK_DESC_LAST;
 
/* Prepare result descriptors */
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->buf = cpu_to_le32(sg_dma_address(dsg));
 
if (nents == 0)
res->hdr |= MTK_DESC_FIRST;
 
-   if (++ring->res_pos == MTK_DESC_NUM)
-   ring->res_pos = 0;
+   /* Shift ring buffer and check boundary */
+   if (++ring->res_next == ring->res_base + MTK_DESC_NUM)
+   ring->res_next = ring->res_base;
}
res->hdr |= MTK_DESC_LAST;
 
diff --git a/drivers/crypto/mediatek/mtk-platform.c 
b/drivers/crypto/mediatek/mtk-platform.c
index 50de335..b6ecc28 100644
--- a/drivers/crypto/mediatek/mtk-platform.c
+++ b/drivers/crypto/mediatek/mtk-platform.c
@@ -465,6 +465,9 @@ static int mtk_desc_ring_alloc(struct mtk_cryp *cryp)
   GFP_KERNEL);
if (!ring[i]->res_base)
goto err_cleanup;
+
+   ring[i]->cmd_next = ring[i]->cmd_base;
+   ring[i]->res_next = ring[i]->res_base;
}
return 0;
 
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 78ce54e..218e30d 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -84,11 +84,11 @@ struct mtk_desc {
 /**
  * struct mtk_ring - Descriptor ring
  * @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_pos:   current position in the command descriptor ring
  * @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_pos:   current position in the result descriptor ring
  *
  * A descriptor ring is a circular buffer that is used to manage
  * one or more descriptors. There are two type of descriptor rings;
@@ -96,11 +96,11 @@ struct mtk_desc {
  */
 struct mtk_ring {
struct mtk_desc *cmd_base;
+   struct mtk_desc *cmd_next;
dma_addr_t cmd_dma;
-   u32 cmd_pos;
struct mtk_desc *res_base;
+   struct mtk_desc *res_next;
dma_addr_t res_dma;
-   u32 res_pos;
 };
 
 /**
diff --git a/drivers/crypto/mediatek/mtk-sha.c 
b/drivers/crypto/mediatek/mtk-sha.c
index ef6fb20..0884d62 100644
--- a/drivers/crypto/mediatek/mtk-sha.c
+++ b/drivers/crypto/mediatek/mtk-sha.c
@@ -152,6 +152,21 @@ static inline void mtk_sha_write(struct mtk_cryp *cryp,
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,
+   

[PATCH 0/9] improve performances on mediatek crypto driver

2017-02-20 Thread Ryder Lee
Hi all,

Some patches of this series improve the performances whereas others
clean up code and refine data structure to make it more efficient.

It also adds support to AES-OFB and AES-CFB128. The new mode has been
tested by using the tcrypt

Ryder Lee (9):
  crypto: mediatek - rework interrupt handler
  crypto: mediatek - add MTK_* prefix and correct annotations.
  crypto: mediatek - make mtk_sha_xmit() more generic
  crypto: mediatek - simplify descriptor ring management
  crypto: mediatek - add queue_task tasklet
  crypto: mediatek - fix error handling in mtk_aes_complete()
  crypto: mediatek - add mtk_aes_gcm_tag_verify()
  crypto: mediatek - make hardware operation flow more efficient
  crypto: mediatek - add support to OFB mode and CFB128 mode

 drivers/crypto/mediatek/mtk-aes.c  | 503 +++--
 drivers/crypto/mediatek/mtk-platform.c |  15 +-
 drivers/crypto/mediatek/mtk-platform.h |  56 ++--
 drivers/crypto/mediatek/mtk-sha.c  | 309 
 4 files changed, 446 insertions(+), 437 deletions(-)

-- 
1.9.1



[PATCH 3/8] crypto: mediatek - make crypto request queue management more generic

2017-01-19 Thread Ryder Lee
This patch changes mtk_aes_handle_queue() to make it more generic.
The function argument is now a pointer to struct crypto_async_request,
which is the common base of struct ablkcipher_request and
struct aead_request.

Also this patch introduces struct mtk_aes_base_ctx which will be the
common base of all the transformation contexts.

Hence the very same queue will be used to manage both block cipher and
AEAD requests (such as gcm and authenc implemented in further patches).

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 75 --
 drivers/crypto/mediatek/mtk-platform.h | 14 ---
 2 files changed, 53 insertions(+), 36 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index b658cb9..7e5a8e0 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -73,9 +73,10 @@ struct mtk_aes_reqctx {
u64 mode;
 };
 
-struct mtk_aes_ctx {
+struct mtk_aes_base_ctx {
struct mtk_cryp *cryp;
u32 keylen;
+   mtk_aes_fn start;
 
struct mtk_aes_ct ct;
dma_addr_t ct_dma;
@@ -86,6 +87,10 @@ struct mtk_aes_ctx {
u32 ct_size;
 };
 
+struct mtk_aes_ctx {
+   struct mtk_aes_base_ctx base;
+};
+
 struct mtk_aes_drv {
struct list_head dev_list;
/* Device list lock */
@@ -108,7 +113,7 @@ static inline void mtk_aes_write(struct mtk_cryp *cryp,
writel_relaxed(value, cryp->base + offset);
 }
 
-static struct mtk_cryp *mtk_aes_find_dev(struct mtk_aes_ctx *ctx)
+static struct mtk_cryp *mtk_aes_find_dev(struct mtk_aes_base_ctx *ctx)
 {
struct mtk_cryp *cryp = NULL;
struct mtk_cryp *tmp;
@@ -170,7 +175,8 @@ static int mtk_aes_info_map(struct mtk_cryp *cryp,
struct mtk_aes_rec *aes,
size_t len)
 {
-   struct mtk_aes_ctx *ctx = aes->ctx;
+   struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
+   struct mtk_aes_base_ctx *ctx = aes->ctx;
 
ctx->ct_hdr = AES_CT_CTRL_HDR | cpu_to_le32(len);
ctx->ct.cmd[0] = AES_CMD0 | cpu_to_le32(len);
@@ -189,7 +195,7 @@ static int mtk_aes_info_map(struct mtk_cryp *cryp,
ctx->tfm.ctrl[0] |= AES_TFM_192BITS;
 
if (aes->flags & AES_FLAGS_CBC) {
-   const u32 *iv = (const u32 *)aes->req->info;
+   const u32 *iv = (const u32 *)req->info;
u32 *iv_state = ctx->tfm.state + ctx->keylen;
int i;
 
@@ -299,11 +305,10 @@ static inline void mtk_aes_restore_sg(const struct 
mtk_aes_dma *dma)
sg->length += dma->remainder;
 }
 
-static int mtk_aes_map(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
+static int mtk_aes_map(struct mtk_cryp *cryp, struct mtk_aes_rec *aes,
+  struct scatterlist *src, struct scatterlist *dst,
+  size_t len)
 {
-   struct scatterlist *src = aes->req->src;
-   struct scatterlist *dst = aes->req->dst;
-   size_t len = aes->req->nbytes;
size_t padlen = 0;
bool src_aligned, dst_aligned;
 
@@ -366,18 +371,17 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 }
 
 static int mtk_aes_handle_queue(struct mtk_cryp *cryp, u8 id,
-   struct ablkcipher_request *req)
+   struct crypto_async_request *new_areq)
 {
struct mtk_aes_rec *aes = cryp->aes[id];
struct crypto_async_request *areq, *backlog;
-   struct mtk_aes_reqctx *rctx;
-   struct mtk_aes_ctx *ctx;
+   struct mtk_aes_base_ctx *ctx;
unsigned long flags;
-   int err, ret = 0;
+   int ret = 0;
 
spin_lock_irqsave(>lock, flags);
-   if (req)
-   ret = ablkcipher_enqueue_request(>queue, req);
+   if (new_areq)
+   ret = crypto_enqueue_request(>queue, new_areq);
if (aes->flags & AES_FLAGS_BUSY) {
spin_unlock_irqrestore(>lock, flags);
return ret;
@@ -394,16 +398,25 @@ static int mtk_aes_handle_queue(struct mtk_cryp *cryp, u8 
id,
if (backlog)
backlog->complete(backlog, -EINPROGRESS);
 
-   req = ablkcipher_request_cast(areq);
-   ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
+   ctx = crypto_tfm_ctx(areq->tfm);
+
+   aes->areq = areq;
+   aes->ctx = ctx;
+
+   return ctx->start(cryp, aes);
+}
+
+static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
+{
+   struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
+   struct mtk_aes_reqctx *rctx = ablkcipher_request_ctx(req);
+   int err;
+
rctx = ablkcipher_request_ctx(req);
rctx->mode &= AES_FLAGS_MODE_MSK;
-   /* Assign new request to device */
-   aes

[PATCH 7/8] crypto: mediatek - add support to CTR mode

2017-01-19 Thread Ryder Lee
This patch adds support to the CTR mode.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 151 --
 1 file changed, 146 insertions(+), 5 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 5e7c3ce..bb5b4ff 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -23,8 +23,10 @@
 /* AES command token size */
 #define AES_CT_SIZE_ECB2
 #define AES_CT_SIZE_CBC3
+#define AES_CT_SIZE_CTR3
 #define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
-/* AES-CBC/ECB command token */
+
+/* AES-CBC/ECB/CTR command token */
 #define AES_CMD0   cpu_to_le32(0x0500)
 #define AES_CMD1   cpu_to_le32(0x2d06)
 #define AES_CMD2   cpu_to_le32(0xe4a63806)
@@ -39,13 +41,15 @@
 /* AES transform information word 1 fields */
 #define AES_TFM_ECBcpu_to_le32(0x0 << 0)
 #define AES_TFM_CBCcpu_to_le32(0x1 << 0)
-#define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)
+#define AES_TFM_CTR_LOAD   cpu_to_le32(0x6 << 0)   /* load/reuse counter */
+#define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)   /* using IV 0-3 
*/
 
 /* AES flags */
 #define AES_FLAGS_ECB  BIT(0)
 #define AES_FLAGS_CBC  BIT(1)
-#define AES_FLAGS_ENCRYPT  BIT(2)
-#define AES_FLAGS_BUSY BIT(3)
+#define AES_FLAGS_CTR  BIT(2)
+#define AES_FLAGS_ENCRYPT  BIT(3)
+#define AES_FLAGS_BUSY BIT(4)
 
 /**
  * Command token(CT) is a set of hardware instructions that
@@ -90,6 +94,15 @@ struct mtk_aes_ctx {
struct mtk_aes_base_ctx base;
 };
 
+struct mtk_aes_ctr_ctx {
+   struct mtk_aes_base_ctx base;
+
+   u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
+   size_t offset;
+   struct scatterlist src[2];
+   struct scatterlist dst[2];
+};
+
 struct mtk_aes_drv {
struct list_head dev_list;
/* Device list lock */
@@ -332,7 +345,7 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
return -EINVAL;
 }
 
-/* Initialize transform information of CBC/ECB mode */
+/* Initialize transform information of CBC/ECB/CTR mode */
 static void mtk_aes_info_init(struct mtk_cryp *cryp, struct mtk_aes_rec *aes,
  size_t len)
 {
@@ -374,6 +387,13 @@ static void mtk_aes_info_init(struct mtk_cryp *cryp, 
struct mtk_aes_rec *aes,
ctx->tfm.ctrl[1] = AES_TFM_ECB;
 
ctx->ct_size = AES_CT_SIZE_ECB;
+   } else if (aes->flags & AES_FLAGS_CTR) {
+   ctx->tfm.ctrl[0] |= AES_TFM_SIZE(ctx->keylen +
+   SIZE_IN_WORDS(AES_BLOCK_SIZE));
+   ctx->tfm.ctrl[1] = AES_TFM_CTR_LOAD | AES_TFM_FULL_IV;
+
+   ctx->ct.cmd[2] = AES_CMD2;
+   ctx->ct_size = AES_CT_SIZE_CTR;
}
 }
 
@@ -479,6 +499,80 @@ static int mtk_aes_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
return mtk_aes_dma(cryp, aes, req->src, req->dst, req->nbytes);
 }
 
+static inline struct mtk_aes_ctr_ctx *
+mtk_aes_ctr_ctx_cast(struct mtk_aes_base_ctx *ctx)
+{
+   return container_of(ctx, struct mtk_aes_ctr_ctx, base);
+}
+
+static int mtk_aes_ctr_transfer(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
+{
+   struct mtk_aes_base_ctx *ctx = aes->ctx;
+   struct mtk_aes_ctr_ctx *cctx = mtk_aes_ctr_ctx_cast(ctx);
+   struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
+   struct scatterlist *src, *dst;
+   int i;
+   u32 start, end, ctr, blocks, *iv_state;
+   size_t datalen;
+   bool fragmented = false;
+
+   /* Check for transfer completion. */
+   cctx->offset += aes->total;
+   if (cctx->offset >= req->nbytes)
+   return mtk_aes_complete(cryp, aes);
+
+   /* Compute data length. */
+   datalen = req->nbytes - cctx->offset;
+   blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
+   ctr = be32_to_cpu(cctx->iv[3]);
+
+   /* Check 32bit counter overflow. */
+   start = ctr;
+   end = start + blocks - 1;
+   if (end < start) {
+   ctr |= 0x;
+   datalen = AES_BLOCK_SIZE * -start;
+   fragmented = true;
+   }
+
+   /* Jump to offset. */
+   src = scatterwalk_ffwd(cctx->src, req->src, cctx->offset);
+   dst = ((req->src == req->dst) ? src :
+  scatterwalk_ffwd(cctx->dst, req->dst, cctx->offset));
+
+   /* Write IVs into transform state buffer. */
+   iv_state = ctx->tfm.state + ctx->keylen;
+   for (i = 0; i < SIZE_IN_WORDS(AES_BLOCK_SIZE); i++)
+   iv_state[i] = cpu_to_le32(cctx->iv[i]);
+
+   if (unlikely(fragmented)) {
+   /*
+* I

[PATCH 1/8] crypto: mediatek - move HW control data to transformation context

2017-01-19 Thread Ryder Lee
This patch moves hardware control block members from
mtk_*_rec to transformation context and refines related
definition. This makes operational context to manage its
own control information easily for each DMA transfer.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 144 -
 drivers/crypto/mediatek/mtk-platform.h |  26 +-
 drivers/crypto/mediatek/mtk-sha.c  | 101 ---
 3 files changed, 126 insertions(+), 145 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 1370cab..126b93c 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -20,23 +20,25 @@
 #define AES_BUF_SIZE   ((PAGE_SIZE << AES_BUF_ORDER) \
& ~(AES_BLOCK_SIZE - 1))
 
-/* AES command token */
+/* AES command token size */
 #define AES_CT_SIZE_ECB2
 #define AES_CT_SIZE_CBC3
 #define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
-#define AES_COMMAND0   cpu_to_le32(0x0500)
-#define AES_COMMAND1   cpu_to_le32(0x2d06)
-#define AES_COMMAND2   cpu_to_le32(0xe4a63806)
-
-/* AES transform information */
-#define AES_TFM_ECBcpu_to_le32(0x0 << 0)
-#define AES_TFM_CBCcpu_to_le32(0x1 << 0)
-#define AES_TFM_DECRYPTcpu_to_le32(0x5 << 0)
-#define AES_TFM_ENCRYPTcpu_to_le32(0x4 << 0)
+/* AES-CBC/ECB command token */
+#define AES_CMD0   cpu_to_le32(0x0500)
+#define AES_CMD1   cpu_to_le32(0x2d06)
+#define AES_CMD2   cpu_to_le32(0xe4a63806)
+
+/* AES transform information word 0 fields */
+#define AES_TFM_BASIC_OUT  cpu_to_le32(0x4 << 0)
+#define AES_TFM_BASIC_IN   cpu_to_le32(0x5 << 0)
 #define AES_TFM_SIZE(x)cpu_to_le32((x) << 8)
 #define AES_TFM_128BITScpu_to_le32(0xb << 16)
 #define AES_TFM_192BITScpu_to_le32(0xd << 16)
 #define AES_TFM_256BITScpu_to_le32(0xf << 16)
+/* AES transform information word 1 fields */
+#define AES_TFM_ECBcpu_to_le32(0x0 << 0)
+#define AES_TFM_CBCcpu_to_le32(0x1 << 0)
 #define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)
 
 /* AES flags */
@@ -47,47 +49,41 @@
 #define AES_FLAGS_BUSY BIT(3)
 
 /**
- * mtk_aes_ct is a set of hardware instructions(command token)
- * that are used to control engine's processing flow of AES.
+ * Command token(CT) is a set of hardware instructions that
+ * are used to control engine's processing flow of AES.
+ *
+ * Transform information(TFM) is used to define AES state and
+ * contains all keys and initial vectors.
+ *
+ * The engine requires CT and TFM to do:
+ * - Commands decoding and control of the engine's data path.
+ * - Coordinating hardware data fetch and store operations.
+ * - Result token construction and output.
  */
 struct mtk_aes_ct {
-   __le32 ct_ctrl0;
-   __le32 ct_ctrl1;
-   __le32 ct_ctrl2;
+   __le32 cmd[AES_CT_SIZE_CBC];
 };
 
-/**
- * mtk_aes_tfm is used to define AES transform state
- * and contains all keys and initial vectors.
- */
 struct mtk_aes_tfm {
-   __le32 tfm_ctrl0;
-   __le32 tfm_ctrl1;
+   __le32 ctrl[2];
__le32 state[SIZE_IN_WORDS(AES_KEYSIZE_256 + AES_BLOCK_SIZE)];
 };
 
-/**
- * mtk_aes_info consists of command token and transform state of AES,
- * which should be encapsulated in command and result descriptors.
- *
- * The engine requires this information to do:
- * - Commands decoding and control of the engine's data path.
- * - Coordinating hardware data fetch and store operations.
- * - Result token construction and output.
- */
-struct mtk_aes_info {
-   struct mtk_aes_ct ct;
-   struct mtk_aes_tfm tfm;
-};
-
 struct mtk_aes_reqctx {
u64 mode;
 };
 
 struct mtk_aes_ctx {
struct mtk_cryp *cryp;
-   struct mtk_aes_info info;
u32 keylen;
+
+   struct mtk_aes_ct ct;
+   dma_addr_t ct_dma;
+   struct mtk_aes_tfm tfm;
+   dma_addr_t tfm_dma;
+
+   __le32 ct_hdr;
+   u32 ct_size;
 };
 
 struct mtk_aes_drv {
@@ -174,57 +170,57 @@ static int mtk_aes_info_map(struct mtk_cryp *cryp,
struct mtk_aes_rec *aes,
size_t len)
 {
-   struct mtk_aes_ctx *ctx = crypto_ablkcipher_ctx(
-   crypto_ablkcipher_reqtfm(aes->req));
-   struct mtk_aes_info *info = aes->info;
-   struct mtk_aes_ct *ct = >ct;
-   struct mtk_aes_tfm *tfm = >tfm;
+   struct mtk_aes_ctx *ctx = aes->ctx;
 
-   aes->ct_hdr = AES_CT_CTRL_HDR | cpu_to_le32(len);
+   ctx->ct_hdr = AES_CT_CTRL_HDR | cpu_to_le32(len);
+   ctx->ct.cmd[0] = AES_CMD0 | cpu_to_le32(len);
+   ctx

[PATCH 0/8] update mediatek crypto driver

2017-01-19 Thread Ryder Lee
Hi,

This series of patches is a global rework of the mtk driver.
Fix bug - incomplete DMA data transfer when SG buffer dst.len != src.len

It also updates some part of the code to make them more generic. For
instance the crypto request queue management supports both async block
cipher and AEAD requests, which allows us to add support the the GCM mode.
GMAC mode is not supported yet.

Current implementation was validated using the tcrypt
module running modes:
- 10: ecb(aes), cbc(aes), ctr(aes), rfc3686(ctr(aes))
- 35: gcm(aes)
- 2,6,11,12: sha1, sha2 family

tcrypt speed test was run with modes:
- 211: rfc4106(gcm(aes)), gcm(aes)
- 500: ecb(aes), cbc(aes), ctr(aes), rfc3686(ctr(aes))
- 403 ~ 406: sha1, sha2 family

IxChariot multiple pairs throughput 24 hours test:
- IPSec VPN
- MACSec

Ryder Lee (8):
  crypto: mediatek - move HW control data to transformation context
  crypto: mediatek - fix incorrect data transfer result
  crypto: mediatek - make crypto request queue management more generic
  crypto: mediatek - rework crypto request completion
  crypto: mediatek - regroup functions by usage
  crypto: mediatek - fix typo and indentation
  crypto: mediatek - add support to CTR mode
  crypto: mediatek - add support to GCM mode

 drivers/crypto/Kconfig |2 +
 drivers/crypto/mediatek/mtk-aes.c  | 1026 
 drivers/crypto/mediatek/mtk-platform.h |   47 +-
 drivers/crypto/mediatek/mtk-sha.c  |  170 +++---
 4 files changed, 886 insertions(+), 359 deletions(-)

-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 4/8] crypto: mediatek - rework crypto request completion

2017-01-19 Thread Ryder Lee
This patch introduces a new callback 'resume' in the struct mtk_aes_rec.
This callback is run to resume/complete the processing of the crypto
request when woken up by AES interrupts when DMA completion.

This callback will help implementing the GCM mode support in further
patches.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 25 +
 drivers/crypto/mediatek/mtk-platform.h |  3 +++
 2 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 7e5a8e0..9c4e468 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -406,6 +406,15 @@ static int mtk_aes_handle_queue(struct mtk_cryp *cryp, u8 
id,
return ctx->start(cryp, aes);
 }
 
+static int mtk_aes_complete(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
+{
+   aes->flags &= ~AES_FLAGS_BUSY;
+   aes->areq->complete(aes->areq, 0);
+
+   /* Handle new request */
+   return mtk_aes_handle_queue(cryp, aes->id, NULL);
+}
+
 static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
 {
struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
@@ -416,6 +425,8 @@ static int mtk_aes_start(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
rctx->mode &= AES_FLAGS_MODE_MSK;
aes->flags = (aes->flags & ~AES_FLAGS_MODE_MSK) | rctx->mode;
 
+   aes->resume = mtk_aes_complete;
+
err = mtk_aes_map(cryp, aes, req->src, req->dst, req->nbytes);
if (err)
return err;
@@ -458,16 +469,6 @@ static void mtk_aes_unmap(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
aes->buf, aes->total);
 }
 
-static inline void mtk_aes_complete(struct mtk_cryp *cryp,
-   struct mtk_aes_rec *aes)
-{
-   aes->flags &= ~AES_FLAGS_BUSY;
-   aes->areq->complete(aes->areq, 0);
-
-   /* Handle new request */
-   mtk_aes_handle_queue(cryp, aes->id, NULL);
-}
-
 /* Check and set the AES key to transform state buffer */
 static int mtk_aes_setkey(struct crypto_ablkcipher *tfm,
  const u8 *key, u32 keylen)
@@ -591,7 +592,7 @@ static void mtk_aes_enc_task(unsigned long data)
struct mtk_aes_rec *aes = cryp->aes[0];
 
mtk_aes_unmap(cryp, aes);
-   mtk_aes_complete(cryp, aes);
+   aes->resume(cryp, aes);
 }
 
 static void mtk_aes_dec_task(unsigned long data)
@@ -600,7 +601,7 @@ static void mtk_aes_dec_task(unsigned long data)
struct mtk_aes_rec *aes = cryp->aes[1];
 
mtk_aes_unmap(cryp, aes);
-   mtk_aes_complete(cryp, aes);
+   aes->resume(cryp, aes);
 }
 
 static irqreturn_t mtk_aes_enc_irq(int irq, void *dev_id)
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 9f5210c..36d166b 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -131,6 +131,7 @@ struct mtk_aes_dma {
  * @dst:   the structure that holds destination sg list info
  * @aligned_sg:the scatter list is use to alignment
  * @real_dst:  pointer to the destination sg list
+ * @resume:pointer to resume function
  * @total: request buffer length
  * @buf:   pointer to page buffer
  * @id:record identification
@@ -150,6 +151,8 @@ struct mtk_aes_rec {
struct scatterlist aligned_sg;
struct scatterlist *real_dst;
 
+   mtk_aes_fn resume;
+
size_t total;
void *buf;
 
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 8/8] crypto: mediatek - add support to GCM mode

2017-01-19 Thread Ryder Lee
This patch adds support to the GCM mode.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/Kconfig |   2 +
 drivers/crypto/mediatek/mtk-aes.c  | 369 -
 drivers/crypto/mediatek/mtk-platform.h |   2 +
 3 files changed, 369 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
index ee5057a..bf7da55 100644
--- a/drivers/crypto/Kconfig
+++ b/drivers/crypto/Kconfig
@@ -557,7 +557,9 @@ config CRYPTO_DEV_MEDIATEK
tristate "MediaTek's EIP97 Cryptographic Engine driver"
depends on (ARM && ARCH_MEDIATEK) || COMPILE_TEST
select CRYPTO_AES
+   select CRYPTO_AEAD
select CRYPTO_BLKCIPHER
+   select CRYPTO_CTR
select CRYPTO_SHA1
select CRYPTO_SHA256
select CRYPTO_SHA512
diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index bb5b4ff..3a47cdb 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -24,16 +24,28 @@
 #define AES_CT_SIZE_ECB2
 #define AES_CT_SIZE_CBC3
 #define AES_CT_SIZE_CTR3
+#define AES_CT_SIZE_GCM_OUT5
+#define AES_CT_SIZE_GCM_IN 6
 #define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
 
 /* AES-CBC/ECB/CTR command token */
 #define AES_CMD0   cpu_to_le32(0x0500)
 #define AES_CMD1   cpu_to_le32(0x2d06)
 #define AES_CMD2   cpu_to_le32(0xe4a63806)
+/* AES-GCM command token */
+#define AES_GCM_CMD0   cpu_to_le32(0x0b00)
+#define AES_GCM_CMD1   cpu_to_le32(0xa080)
+#define AES_GCM_CMD2   cpu_to_le32(0x2510)
+#define AES_GCM_CMD3   cpu_to_le32(0x0f02)
+#define AES_GCM_CMD4   cpu_to_le32(0x21e6)
+#define AES_GCM_CMD5   cpu_to_le32(0x40e6)
+#define AES_GCM_CMD6   cpu_to_le32(0xd007)
 
 /* AES transform information word 0 fields */
 #define AES_TFM_BASIC_OUT  cpu_to_le32(0x4 << 0)
 #define AES_TFM_BASIC_IN   cpu_to_le32(0x5 << 0)
+#define AES_TFM_GCM_OUTcpu_to_le32(0x6 << 0)
+#define AES_TFM_GCM_IN cpu_to_le32(0xf << 0)
 #define AES_TFM_SIZE(x)cpu_to_le32((x) << 8)
 #define AES_TFM_128BITScpu_to_le32(0xb << 16)
 #define AES_TFM_192BITScpu_to_le32(0xd << 16)
@@ -41,15 +53,22 @@
 /* AES transform information word 1 fields */
 #define AES_TFM_ECBcpu_to_le32(0x0 << 0)
 #define AES_TFM_CBCcpu_to_le32(0x1 << 0)
+#define AES_TFM_CTR_INIT   cpu_to_le32(0x2 << 0)   /* init counter to 1 */
 #define AES_TFM_CTR_LOAD   cpu_to_le32(0x6 << 0)   /* load/reuse counter */
+#define AES_TFM_3IVcpu_to_le32(0x7 << 5)   /* using IV 0-2 */
 #define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)   /* using IV 0-3 
*/
+#define AES_TFM_IV_CTR_MODEcpu_to_le32(0x1 << 10)
+#define AES_TFM_ENC_HASH   cpu_to_le32(0x1 << 17)
+#define AES_TFM_GHASH_DIG  cpu_to_le32(0x2 << 21)
+#define AES_TFM_GHASH  cpu_to_le32(0x4 << 23)
 
 /* AES flags */
 #define AES_FLAGS_ECB  BIT(0)
 #define AES_FLAGS_CBC  BIT(1)
 #define AES_FLAGS_CTR  BIT(2)
-#define AES_FLAGS_ENCRYPT  BIT(3)
-#define AES_FLAGS_BUSY BIT(4)
+#define AES_FLAGS_GCM  BIT(3)
+#define AES_FLAGS_ENCRYPT  BIT(4)
+#define AES_FLAGS_BUSY BIT(5)
 
 /**
  * Command token(CT) is a set of hardware instructions that
@@ -62,14 +81,23 @@
  * - Commands decoding and control of the engine's data path.
  * - Coordinating hardware data fetch and store operations.
  * - Result token construction and output.
+ *
+ * Memory map of GCM's TFM:
+ * /---\
+ * |  AES KEY  | 128/196/256 bits
+ * |---|
+ * |  HASH KEY | a string 128 zero bits encrypted using the block cipher
+ * |---|
+ * |IVs| 4 * 4 bytes
+ * \---/
  */
 struct mtk_aes_ct {
-   __le32 cmd[AES_CT_SIZE_CBC];
+   __le32 cmd[AES_CT_SIZE_GCM_IN];
 };
 
 struct mtk_aes_tfm {
__le32 ctrl[2];
-   __le32 state[SIZE_IN_WORDS(AES_KEYSIZE_256 + AES_BLOCK_SIZE)];
+   __le32 state[SIZE_IN_WORDS(AES_KEYSIZE_256 + AES_BLOCK_SIZE * 2)];
 };
 
 struct mtk_aes_reqctx {
@@ -103,6 +131,20 @@ struct mtk_aes_ctr_ctx {
struct scatterlist dst[2];
 };
 
+struct mtk_aes_gcm_ctx {
+   struct mtk_aes_base_ctx base;
+
+   u32 authsize;
+   size_t textlen;
+
+   struct crypto_skcipher *ctr;
+};
+
+struct mtk_aes_gcm_setkey_result {
+   int err;
+   struct completion completion;
+};
+
 struct mtk_aes_drv {
struct list_head dev_list;
/* Device list lock */
@@ -251,6 +293,10 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
}
res->hdr |= MTK_DESC_LAST;
 
+   /*

[PATCH 6/8] crypto: mediatek - fix typo and indentation

2017-01-19 Thread Ryder Lee
Dummy patch to fix typo and indentation.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 90 +-
 drivers/crypto/mediatek/mtk-platform.h |  2 +-
 drivers/crypto/mediatek/mtk-sha.c  | 40 +++
 3 files changed, 63 insertions(+), 69 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index b5946e9..5e7c3ce 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -314,8 +314,8 @@ static int mtk_aes_map(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
aes->dst.sg_len = dma_map_sg(cryp->dev, aes->dst.sg,
 aes->dst.nents, DMA_FROM_DEVICE);
if (unlikely(!aes->dst.sg_len)) {
-   dma_unmap_sg(cryp->dev, aes->src.sg,
-aes->src.nents, DMA_TO_DEVICE);
+   dma_unmap_sg(cryp->dev, aes->src.sg, aes->src.nents,
+DMA_TO_DEVICE);
goto sg_map_err;
}
}
@@ -484,7 +484,7 @@ static int mtk_aes_setkey(struct crypto_ablkcipher *tfm,
  const u8 *key, u32 keylen)
 {
struct mtk_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
-   const u32 *key_tmp = (const u32 *)key;
+   const u32 *aes_key = (const u32 *)key;
u32 *key_state = ctx->tfm.state;
int i;
 
@@ -498,7 +498,7 @@ static int mtk_aes_setkey(struct crypto_ablkcipher *tfm,
ctx->keylen = SIZE_IN_WORDS(keylen);
 
for (i = 0; i < ctx->keylen; i++)
-   key_state[i] = cpu_to_le32(key_tmp[i]);
+   key_state[i] = cpu_to_le32(aes_key[i]);
 
return 0;
 }
@@ -512,26 +512,26 @@ static int mtk_aes_crypt(struct ablkcipher_request *req, 
u64 mode)
rctx = ablkcipher_request_ctx(req);
rctx->mode = mode;
 
-   return mtk_aes_handle_queue(ctx->cryp,
-   !(mode & AES_FLAGS_ENCRYPT), >base);
+   return mtk_aes_handle_queue(ctx->cryp, !(mode & AES_FLAGS_ENCRYPT),
+   >base);
 }
 
-static int mtk_ecb_encrypt(struct ablkcipher_request *req)
+static int mtk_aes_ecb_encrypt(struct ablkcipher_request *req)
 {
return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_ECB);
 }
 
-static int mtk_ecb_decrypt(struct ablkcipher_request *req)
+static int mtk_aes_ecb_decrypt(struct ablkcipher_request *req)
 {
return mtk_aes_crypt(req, AES_FLAGS_ECB);
 }
 
-static int mtk_cbc_encrypt(struct ablkcipher_request *req)
+static int mtk_aes_cbc_encrypt(struct ablkcipher_request *req)
 {
return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CBC);
 }
 
-static int mtk_cbc_decrypt(struct ablkcipher_request *req)
+static int mtk_aes_cbc_decrypt(struct ablkcipher_request *req)
 {
return mtk_aes_crypt(req, AES_FLAGS_CBC);
 }
@@ -554,44 +554,44 @@ static int mtk_aes_cra_init(struct crypto_tfm *tfm)
 
 static struct crypto_alg aes_algs[] = {
 {
-   .cra_name   =   "cbc(aes)",
-   .cra_driver_name=   "cbc-aes-mtk",
-   .cra_priority   =   400,
-   .cra_flags  =   CRYPTO_ALG_TYPE_ABLKCIPHER |
-   CRYPTO_ALG_ASYNC,
-   .cra_init   =   mtk_aes_cra_init,
-   .cra_blocksize  =   AES_BLOCK_SIZE,
-   .cra_ctxsize=   sizeof(struct mtk_aes_ctx),
-   .cra_alignmask  =   15,
-   .cra_type   =   _ablkcipher_type,
-   .cra_module =   THIS_MODULE,
-   .cra_u.ablkcipher   =   {
-   .min_keysize=   AES_MIN_KEY_SIZE,
-   .max_keysize=   AES_MAX_KEY_SIZE,
-   .setkey =   mtk_aes_setkey,
-   .encrypt=   mtk_cbc_encrypt,
-   .decrypt=   mtk_cbc_decrypt,
-   .ivsize =   AES_BLOCK_SIZE,
+   .cra_name   = "cbc(aes)",
+   .cra_driver_name= "cbc-aes-mtk",
+   .cra_priority   = 400,
+   .cra_flags  = CRYPTO_ALG_TYPE_ABLKCIPHER |
+ CRYPTO_ALG_ASYNC,
+   .cra_init   = mtk_aes_cra_init,
+   .cra_blocksize  = AES_BLOCK_SIZE,
+   .cra_ctxsize= sizeof(struct mtk_aes_ctx),
+   .cra_alignmask  = 0xf,
+   .cra_type   = _ablkcipher_type,
+   .cra_module = THIS_MODULE,
+   .cra_u.ablkcipher = {
+   .min_keysize= AES_MIN_KEY_SIZE,
+   .max_keysize= AES_MAX_KEY_SIZE,
+   .setkey = mtk_aes_setkey,
+   .encrypt= mtk_aes_cb

[PATCH 2/8] crypto: mediatek - fix incorrect data transfer result

2017-01-19 Thread Ryder Lee
This patch fixes mtk_aes_xmit() data transfer bug.

The original function uses the same loop and ring->pos
to handle both command and result descriptors. But this
produces incomplete results when src.sg_len != dst.sg_len.

To solve the problem, we splits the descriptors into different
loops and uses cmd_pos and res_pos to record them respectively.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c  | 44 --
 drivers/crypto/mediatek/mtk-platform.h |  6 +++--
 drivers/crypto/mediatek/mtk-sha.c  | 29 --
 3 files changed, 47 insertions(+), 32 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 126b93c..b658cb9 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -225,29 +225,25 @@ static int mtk_aes_info_map(struct mtk_cryp *cryp,
return 0;
 }
 
+/*
+ * Write descriptors for processing. This will configure the engine, load
+ * the transform information and then start the packet processing.
+ */
 static int mtk_aes_xmit(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
 {
struct mtk_ring *ring = cryp->ring[aes->id];
struct mtk_desc *cmd = NULL, *res = NULL;
-   struct scatterlist *ssg, *dsg;
-   u32 len = aes->src.sg_len;
+   struct scatterlist *ssg = aes->src.sg, *dsg = aes->dst.sg;
+   u32 slen = aes->src.sg_len, dlen = aes->dst.sg_len;
int nents;
 
-   /* Fill in the command/result descriptors */
-   for (nents = 0; nents < len; ++nents) {
-   ssg = >src.sg[nents];
-   dsg = >dst.sg[nents];
-
-   cmd = ring->cmd_base + ring->pos;
+   /* Write command descriptors */
+   for (nents = 0; nents < slen; ++nents, ssg = sg_next(ssg)) {
+   cmd = ring->cmd_base + ring->cmd_pos;
cmd->hdr = MTK_DESC_BUF_LEN(ssg->length);
cmd->buf = cpu_to_le32(sg_dma_address(ssg));
 
-   res = ring->res_base + ring->pos;
-   res->hdr = MTK_DESC_BUF_LEN(dsg->length);
-   res->buf = cpu_to_le32(sg_dma_address(dsg));
-
if (nents == 0) {
-   res->hdr |= MTK_DESC_FIRST;
cmd->hdr |= MTK_DESC_FIRST |
MTK_DESC_CT_LEN(aes->ctx->ct_size);
cmd->ct = cpu_to_le32(aes->ctx->ct_dma);
@@ -255,11 +251,23 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
cmd->tfm = cpu_to_le32(aes->ctx->tfm_dma);
}
 
-   if (++ring->pos == MTK_DESC_NUM)
-   ring->pos = 0;
+   if (++ring->cmd_pos == MTK_DESC_NUM)
+   ring->cmd_pos = 0;
}
-
cmd->hdr |= MTK_DESC_LAST;
+
+   /* Prepare result descriptors */
+   for (nents = 0; nents < dlen; ++nents, dsg = sg_next(dsg)) {
+   res = ring->res_base + ring->res_pos;
+   res->hdr = MTK_DESC_BUF_LEN(dsg->length);
+   res->buf = cpu_to_le32(sg_dma_address(dsg));
+
+   if (nents == 0)
+   res->hdr |= MTK_DESC_FIRST;
+
+   if (++ring->res_pos == MTK_DESC_NUM)
+   ring->res_pos = 0;
+   }
res->hdr |= MTK_DESC_LAST;
 
/*
@@ -268,8 +276,8 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
 */
wmb();
/* Start DMA transfer */
-   mtk_aes_write(cryp, RDR_PREP_COUNT(aes->id), MTK_DESC_CNT(len));
-   mtk_aes_write(cryp, CDR_PREP_COUNT(aes->id), MTK_DESC_CNT(len));
+   mtk_aes_write(cryp, RDR_PREP_COUNT(aes->id), MTK_DESC_CNT(dlen));
+   mtk_aes_write(cryp, CDR_PREP_COUNT(aes->id), MTK_DESC_CNT(slen));
 
return -EINPROGRESS;
 }
diff --git a/drivers/crypto/mediatek/mtk-platform.h 
b/drivers/crypto/mediatek/mtk-platform.h
index 1516786..8c50b74 100644
--- a/drivers/crypto/mediatek/mtk-platform.h
+++ b/drivers/crypto/mediatek/mtk-platform.h
@@ -83,9 +83,10 @@ struct mtk_desc {
  * struct mtk_ring - Descriptor ring
  * @cmd_base:  pointer to command descriptor ring base
  * @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_dma:   DMA address of result descriptor ring
- * @pos:   current position in the ring
+ * @res_pos:   current position in the result descriptor ring
  *
  * A descriptor ring is a circular buffer that is used to manage
  * one or more descriptors. There are two type of descriptor rings;
@@ -94,9 +95,10 @@ struct mtk_desc {
 struct mtk_ring {
struct mtk_desc *cmd_base;
dma_addr_t cmd_dma;
+   

[PATCH 5/8] crypto: mediatek - regroup functions by usage

2017-01-19 Thread Ryder Lee
This patch only regroup functions by usage.
This will help to integrate the GCM support patch later by
adjusting some shared code section, such as common code which
will be reused by GCM, AES mode setting, and DMA transfer.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/mediatek/mtk-aes.c | 272 --
 1 file changed, 141 insertions(+), 131 deletions(-)

diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
index 9c4e468..b5946e9 100644
--- a/drivers/crypto/mediatek/mtk-aes.c
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -42,7 +42,6 @@
 #define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)
 
 /* AES flags */
-#define AES_FLAGS_MODE_MSK 0x7
 #define AES_FLAGS_ECB  BIT(0)
 #define AES_FLAGS_CBC  BIT(1)
 #define AES_FLAGS_ENCRYPT  BIT(2)
@@ -170,65 +169,28 @@ static bool mtk_aes_check_aligned(struct scatterlist *sg, 
size_t len,
return false;
 }
 
-/* Initialize and map transform information of AES */
-static int mtk_aes_info_map(struct mtk_cryp *cryp,
-   struct mtk_aes_rec *aes,
-   size_t len)
+static inline void mtk_aes_set_mode(struct mtk_aes_rec *aes,
+   const struct mtk_aes_reqctx *rctx)
 {
-   struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq);
-   struct mtk_aes_base_ctx *ctx = aes->ctx;
-
-   ctx->ct_hdr = AES_CT_CTRL_HDR | cpu_to_le32(len);
-   ctx->ct.cmd[0] = AES_CMD0 | cpu_to_le32(len);
-   ctx->ct.cmd[1] = AES_CMD1;
-
-   if (aes->flags & AES_FLAGS_ENCRYPT)
-   ctx->tfm.ctrl[0] = AES_TFM_BASIC_OUT;
-   else
-   ctx->tfm.ctrl[0] = AES_TFM_BASIC_IN;
-
-   if (ctx->keylen == SIZE_IN_WORDS(AES_KEYSIZE_128))
-   ctx->tfm.ctrl[0] |= AES_TFM_128BITS;
-   else if (ctx->keylen == SIZE_IN_WORDS(AES_KEYSIZE_256))
-   ctx->tfm.ctrl[0] |= AES_TFM_256BITS;
-   else if (ctx->keylen == SIZE_IN_WORDS(AES_KEYSIZE_192))
-   ctx->tfm.ctrl[0] |= AES_TFM_192BITS;
-
-   if (aes->flags & AES_FLAGS_CBC) {
-   const u32 *iv = (const u32 *)req->info;
-   u32 *iv_state = ctx->tfm.state + ctx->keylen;
-   int i;
-
-   ctx->tfm.ctrl[0] |= AES_TFM_SIZE(ctx->keylen +
- SIZE_IN_WORDS(AES_BLOCK_SIZE));
-   ctx->tfm.ctrl[1] = AES_TFM_CBC | AES_TFM_FULL_IV;
-
-   for (i = 0; i < SIZE_IN_WORDS(AES_BLOCK_SIZE); i++)
-   iv_state[i] = cpu_to_le32(iv[i]);
+   /* Clear all but persistent flags and set request flags. */
+   aes->flags = (aes->flags & AES_FLAGS_BUSY) | rctx->mode;
+}
 
-   ctx->ct.cmd[2] = AES_CMD2;
-   ctx->ct_size  = AES_CT_SIZE_CBC;
-   } else if (aes->flags & AES_FLAGS_ECB) {
-   ctx->tfm.ctrl[0] |= AES_TFM_SIZE(ctx->keylen);
-   ctx->tfm.ctrl[1] = AES_TFM_ECB;
+static inline void mtk_aes_restore_sg(const struct mtk_aes_dma *dma)
+{
+   struct scatterlist *sg = dma->sg;
+   int nents = dma->nents;
 
-   ctx->ct_size = AES_CT_SIZE_ECB;
-   }
+   if (!dma->remainder)
+   return;
 
-   ctx->ct_dma = dma_map_single(cryp->dev, >ct, sizeof(ctx->ct),
-DMA_TO_DEVICE);
-   if (unlikely(dma_mapping_error(cryp->dev, ctx->ct_dma)))
-   return -EINVAL;
+   while (--nents > 0 && sg)
+   sg = sg_next(sg);
 
-   ctx->tfm_dma = dma_map_single(cryp->dev, >tfm, sizeof(ctx->tfm),
- DMA_TO_DEVICE);
-   if (unlikely(dma_mapping_error(cryp->dev, ctx->tfm_dma))) {
-   dma_unmap_single(cryp->dev, ctx->tfm_dma, sizeof(ctx->tfm),
-DMA_TO_DEVICE);
-   return -EINVAL;
-   }
+   if (!sg)
+   return;
 
-   return 0;
+   sg->length += dma->remainder;
 }
 
 /*
@@ -288,24 +250,134 @@ static int mtk_aes_xmit(struct mtk_cryp *cryp, struct 
mtk_aes_rec *aes)
return -EINPROGRESS;
 }
 
-static inline void mtk_aes_restore_sg(const struct mtk_aes_dma *dma)
+static void mtk_aes_unmap(struct mtk_cryp *cryp, struct mtk_aes_rec *aes)
 {
-   struct scatterlist *sg = dma->sg;
-   int nents = dma->nents;
+   struct mtk_aes_base_ctx *ctx = aes->ctx;
 
-   if (!dma->remainder)
-   return;
+   dma_unmap_single(cryp->dev, ctx->ct_dma, sizeof(ctx->ct),
+DMA_TO_DEVICE);
+   dma_unmap_single(cryp->dev, ctx->tfm_dma, sizeof(ctx->tfm),
+DMA_TO_DEVICE);
 
-   while (--nents > 0 && sg)
-   sg = sg_

[PATCH v3 1/2] Add crypto driver support for some MediaTek chips

2016-12-18 Thread Ryder Lee
This adds support for the MediaTek hardware accelerator on
mt7623/mt2701/mt8521p SoC.

This driver currently implement:
- SHA1 and SHA2 family(HMAC) hash algorithms.
- AES block cipher in CBC/ECB mode with 128/196/256 bits keys.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/Kconfig |   17 +
 drivers/crypto/Makefile|1 +
 drivers/crypto/mediatek/Makefile   |2 +
 drivers/crypto/mediatek/mtk-aes.c  |  765 +
 drivers/crypto/mediatek/mtk-platform.c |  604 ++
 drivers/crypto/mediatek/mtk-platform.h |  238 ++
 drivers/crypto/mediatek/mtk-regs.h |  194 +
 drivers/crypto/mediatek/mtk-sha.c  | 1437 
 8 files changed, 3258 insertions(+)
 create mode 100644 drivers/crypto/mediatek/Makefile
 create mode 100644 drivers/crypto/mediatek/mtk-aes.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.h
 create mode 100644 drivers/crypto/mediatek/mtk-regs.h
 create mode 100644 drivers/crypto/mediatek/mtk-sha.c

diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
index 4d2b81f..937039d 100644
--- a/drivers/crypto/Kconfig
+++ b/drivers/crypto/Kconfig
@@ -553,6 +553,23 @@ config CRYPTO_DEV_ROCKCHIP
  This driver interfaces with the hardware crypto accelerator.
  Supporting cbc/ecb chainmode, and aes/des/des3_ede cipher mode.
 
+config CRYPTO_DEV_MEDIATEK
+   tristate "MediaTek's EIP97 Cryptographic Engine driver"
+   depends on ARM && (ARCH_MEDIATEK || COMPILE_TEST)
+   select NEON
+   select KERNEL_MODE_NEON
+   select ARM_CRYPTO
+   select CRYPTO_AES
+   select CRYPTO_BLKCIPHER
+   select CRYPTO_SHA1_ARM_NEON
+   select CRYPTO_SHA256_ARM
+   select CRYPTO_SHA512_ARM
+   select CRYPTO_HMAC
+   help
+ This driver allows you to utilize the hardware crypto accelerator
+ EIP97 which can be found on the MT7623 MT2701, MT8521p, etc 
+ Select this if you want to use it for AES/SHA1/SHA2 algorithms.
+
 source "drivers/crypto/chelsio/Kconfig"
 
 endif # CRYPTO_HW
diff --git a/drivers/crypto/Makefile b/drivers/crypto/Makefile
index ad7250f..272b51a 100644
--- a/drivers/crypto/Makefile
+++ b/drivers/crypto/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_CRYPTO_DEV_IMGTEC_HASH) += img-hash.o
 obj-$(CONFIG_CRYPTO_DEV_IXP4XX) += ixp4xx_crypto.o
 obj-$(CONFIG_CRYPTO_DEV_MV_CESA) += mv_cesa.o
 obj-$(CONFIG_CRYPTO_DEV_MARVELL_CESA) += marvell/
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mediatek/
 obj-$(CONFIG_CRYPTO_DEV_MXS_DCP) += mxs-dcp.o
 obj-$(CONFIG_CRYPTO_DEV_NIAGARA2) += n2_crypto.o
 n2_crypto-y := n2_core.o n2_asm.o
diff --git a/drivers/crypto/mediatek/Makefile b/drivers/crypto/mediatek/Makefile
new file mode 100644
index 000..187be79
--- /dev/null
+++ b/drivers/crypto/mediatek/Makefile
@@ -0,0 +1,2 @@
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mtk-crypto.o
+mtk-crypto-objs:= mtk-platform.o mtk-aes.o mtk-sha.o
diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
new file mode 100644
index 000..3271471
--- /dev/null
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -0,0 +1,765 @@
+/*
+ * Cryptographic API.
+ *
+ * Driver for EIP97 AES acceleration.
+ *
+ * Copyright (c) 2016 Ryder Lee <ryder@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Some ideas are from atmel-aes.c drivers.
+ */
+
+#include 
+#include "mtk-platform.h"
+
+#define AES_QUEUE_SIZE 512
+#define AES_BUF_ORDER  2
+#define AES_BUF_SIZE   ((PAGE_SIZE << AES_BUF_ORDER) \
+   & ~(AES_BLOCK_SIZE - 1))
+
+/* AES command token */
+#define AES_CT_SIZE_ECB2
+#define AES_CT_SIZE_CBC3
+#define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
+#define AES_COMMAND0   cpu_to_le32(0x0500)
+#define AES_COMMAND1   cpu_to_le32(0x2d06)
+#define AES_COMMAND2   cpu_to_le32(0xe4a63806)
+
+/* AES transform information */
+#define AES_TFM_ECBcpu_to_le32(0x0 << 0)
+#define AES_TFM_CBCcpu_to_le32(0x1 << 0)
+#define AES_TFM_DECRYPTcpu_to_le32(0x5 << 0)
+#define AES_TFM_ENCRYPTcpu_to_le32(0x4 << 0)
+#define AES_TFM_SIZE(x)cpu_to_le32((x) << 8)
+#define AES_TFM_128BITScpu_to_le32(0xb << 16)
+#define AES_TFM_192BITScpu_to_le32(0xd << 16)
+#define AES_TFM_256BITScpu_to_le32(0xf << 16)
+#define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)
+
+/* AES flags */
+#define AES_FLAGS_MODE_MSK 0x7
+#define AES_FLAGS_ECB  BIT(0)
+#define AES_FLAGS_CBC  

[PATCH v2 2/2] crypto: mediatek - add DT bindings documentation

2016-12-12 Thread Ryder Lee
Add DT bindings documentation for the crypto driver

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 .../devicetree/bindings/crypto/mediatek-crypto.txt | 32 ++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt

diff --git a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt 
b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
new file mode 100644
index 000..47a786e
--- /dev/null
+++ b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
@@ -0,0 +1,32 @@
+MediaTek cryptographic accelerators
+
+Required properties:
+- compatible: Should be "mediatek,eip97-crypto"
+- reg: Address and length of the register set for the device
+- interrupts: Should contain the five crypto engines interrupts in numeric
+   order. These are global system and four descriptor rings.
+- clocks: the clock used by the core
+- clock-names: the names of the clock listed in the clocks property. These are
+   "ethif", "cryp"
+- power-domains: Must contain a reference to the PM domain.
+
+
+Optional properties:
+- interrupt-parent: Should be the phandle for the interrupt controller
+  that services interrupts for this device
+
+
+Example:
+   crypto: crypto@1b24 {
+   compatible = "mediatek,eip97-crypto";
+   reg = <0 0x1b24 0 0x2>;
+   interrupts = ,
+,
+,
+,
+;
+   clocks = < CLK_TOP_ETHIF_SEL>,
+< CLK_ETHSYS_CRYPTO>;
+   clock-names = "ethif","cryp";
+   power-domains = < MT2701_POWER_DOMAIN_ETH>;
+   };
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 1/2] Add crypto driver support for some MediaTek chips

2016-12-12 Thread Ryder Lee
This adds support for the MediaTek hardware accelerator on
mt7623/mt2701/mt8521p SoC.

This driver currently implement:
- SHA1 and SHA2 family(HMAC) hash algorithms.
- AES block cipher in CBC/ECB mode with 128/196/256 bits keys.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/Kconfig |   17 +
 drivers/crypto/Makefile|1 +
 drivers/crypto/mediatek/Makefile   |2 +
 drivers/crypto/mediatek/mtk-aes.c  |  766 +
 drivers/crypto/mediatek/mtk-platform.c |  604 ++
 drivers/crypto/mediatek/mtk-platform.h |  238 ++
 drivers/crypto/mediatek/mtk-regs.h |  194 +
 drivers/crypto/mediatek/mtk-sha.c  | 1437 
 8 files changed, 3259 insertions(+)
 create mode 100644 drivers/crypto/mediatek/Makefile
 create mode 100644 drivers/crypto/mediatek/mtk-aes.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.h
 create mode 100644 drivers/crypto/mediatek/mtk-regs.h
 create mode 100644 drivers/crypto/mediatek/mtk-sha.c

diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
index 4d2b81f..937039d 100644
--- a/drivers/crypto/Kconfig
+++ b/drivers/crypto/Kconfig
@@ -553,6 +553,23 @@ config CRYPTO_DEV_ROCKCHIP
  This driver interfaces with the hardware crypto accelerator.
  Supporting cbc/ecb chainmode, and aes/des/des3_ede cipher mode.
 
+config CRYPTO_DEV_MEDIATEK
+   tristate "MediaTek's EIP97 Cryptographic Engine driver"
+   depends on ARM && (ARCH_MEDIATEK || COMPILE_TEST)
+   select NEON
+   select KERNEL_MODE_NEON
+   select ARM_CRYPTO
+   select CRYPTO_AES
+   select CRYPTO_BLKCIPHER
+   select CRYPTO_SHA1_ARM_NEON
+   select CRYPTO_SHA256_ARM
+   select CRYPTO_SHA512_ARM
+   select CRYPTO_HMAC
+   help
+ This driver allows you to utilize the hardware crypto accelerator
+ EIP97 which can be found on the MT7623 MT2701, MT8521p, etc 
+ Select this if you want to use it for AES/SHA1/SHA2 algorithms.
+
 source "drivers/crypto/chelsio/Kconfig"
 
 endif # CRYPTO_HW
diff --git a/drivers/crypto/Makefile b/drivers/crypto/Makefile
index ad7250f..272b51a 100644
--- a/drivers/crypto/Makefile
+++ b/drivers/crypto/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_CRYPTO_DEV_IMGTEC_HASH) += img-hash.o
 obj-$(CONFIG_CRYPTO_DEV_IXP4XX) += ixp4xx_crypto.o
 obj-$(CONFIG_CRYPTO_DEV_MV_CESA) += mv_cesa.o
 obj-$(CONFIG_CRYPTO_DEV_MARVELL_CESA) += marvell/
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mediatek/
 obj-$(CONFIG_CRYPTO_DEV_MXS_DCP) += mxs-dcp.o
 obj-$(CONFIG_CRYPTO_DEV_NIAGARA2) += n2_crypto.o
 n2_crypto-y := n2_core.o n2_asm.o
diff --git a/drivers/crypto/mediatek/Makefile b/drivers/crypto/mediatek/Makefile
new file mode 100644
index 000..187be79
--- /dev/null
+++ b/drivers/crypto/mediatek/Makefile
@@ -0,0 +1,2 @@
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mtk-crypto.o
+mtk-crypto-objs:= mtk-platform.o mtk-aes.o mtk-sha.o
diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
new file mode 100644
index 000..aa915c6
--- /dev/null
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -0,0 +1,766 @@
+/*
+ * Cryptographic API.
+ *
+ * Driver for EIP97 AES acceleration.
+ *
+ * Copyright (c) 2016 Ryder Lee <ryder@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Some ideas are from atmel-aes.c drivers.
+ */
+
+#include 
+#include "mtk-platform.h"
+
+#define AES_QUEUE_SIZE 512
+#define AES_BUF_ORDER  2
+#define AES_BUF_SIZE   ((PAGE_SIZE << AES_BUF_ORDER) \
+   & ~(AES_BLOCK_SIZE - 1))
+
+/* AES command token */
+#define AES_CT_SIZE_ECB2
+#define AES_CT_SIZE_CBC3
+#define AES_CT_CTRL_HDRcpu_to_le32(0x0022)
+#define AES_COMMAND0   cpu_to_le32(0x0500)
+#define AES_COMMAND1   cpu_to_le32(0x2d06)
+#define AES_COMMAND2   cpu_to_le32(0xe4a63806)
+
+/* AES transform information */
+#define AES_TFM_ECBcpu_to_le32(0x0 << 0)
+#define AES_TFM_CBCcpu_to_le32(0x1 << 0)
+#define AES_TFM_DECRYPTcpu_to_le32(0x5 << 0)
+#define AES_TFM_ENCRYPTcpu_to_le32(0x4 << 0)
+#define AES_TFM_SIZE(x)cpu_to_le32((x) << 8)
+#define AES_TFM_128BITScpu_to_le32(0xb << 16)
+#define AES_TFM_192BITScpu_to_le32(0xd << 16)
+#define AES_TFM_256BITScpu_to_le32(0xf << 16)
+#define AES_TFM_FULL_IVcpu_to_le32(0xf << 5)
+
+/* AES flags */
+#define AES_FLAGS_MODE_MSK 0x7
+#define AES_FLAGS_ECB  BIT(0)
+#define AES_FLAGS_CBC  

[PATCH v2 0/2] Add MediaTek crypto accelerator driver

2016-12-12 Thread Ryder Lee
Hello,

This adds support for the MediaTek hardware accelerator on
some SoCs.

This driver currently implement: 
- SHA1 and SHA2 family(HMAC) hash algorithms.
- AES block cipher in CBC/ECB mode with 128/196/256 bits keys.

Changes since v2:
- use byteorder conversion macros and type identifiers for descriptors
- revise register definition macros to make it more clear
- revise DT compatiable string

Changes since v1:
- remove EXPORT_SYMBOL
- remove unused PRNG setting
- sort headers in alphabetical order
- add a definition for IRQ unmber
- replace ambiguous definition
- add more annotation and function comment
- add COMPILE_TEST in Kconfig

Ryder Lee (2):
  Add crypto driver support for some MediaTek chips
  crypto: mediatek - add DT bindings documentation

 .../devicetree/bindings/crypto/mediatek-crypto.txt |   32 +
 drivers/crypto/Kconfig |   17 +
 drivers/crypto/Makefile|1 +
 drivers/crypto/mediatek/Makefile   |2 +
 drivers/crypto/mediatek/mtk-aes.c  |  766 +++
 drivers/crypto/mediatek/mtk-platform.c |  604 
 drivers/crypto/mediatek/mtk-platform.h |  238 
 drivers/crypto/mediatek/mtk-regs.h |  194 +++
 drivers/crypto/mediatek/mtk-sha.c  | 1437 
 9 files changed, 3291 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
 create mode 100644 drivers/crypto/mediatek/Makefile
 create mode 100644 drivers/crypto/mediatek/mtk-aes.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.h
 create mode 100644 drivers/crypto/mediatek/mtk-regs.h
 create mode 100644 drivers/crypto/mediatek/mtk-sha.c

-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v1 2/2] crypto: mediatek - add DT bindings documentation

2016-12-08 Thread Ryder Lee
Hello,

On Mon, 2016-12-05 at 11:18 +0100, Matthias Brugger wrote:
> 
> On 05/12/16 08:01, Ryder Lee wrote:
> > Add DT bindings documentation for the crypto driver
> >
> > Signed-off-by: Ryder Lee <ryder@mediatek.com>
> > ---
> >  .../devicetree/bindings/crypto/mediatek-crypto.txt | 32 
> > ++
> >  1 file changed, 32 insertions(+)
> >  create mode 100644 
> > Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
> >
> > diff --git a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt 
> > b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
> > new file mode 100644
> > index 000..8b1db08
> > --- /dev/null
> > +++ b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
> > @@ -0,0 +1,32 @@
> > +MediaTek cryptographic accelerators
> > +
> > +Required properties:
> > +- compatible: Should be "mediatek,mt7623-crypto"
> 
> Do you know how big the difference is between the crypto engine for 
> mt7623/mt2701/mt8521p in comparison, let's say mt8173 or mt6797?
> Do this SoCs have a crypot engine? If so and they are quite similar, we 
> might think of adding a mtk-crypto binding and add soc specific bindings.

This engine is just available in mt7623/mt2701/mt8521p series SoCs and
they have no difference.

But there are still other crypto IPs in MTK, i think maybe we could use
"mediatek,{IP name}-cryptoā€¯ to distinguish them ?

> Regards,
> Matthias
> 
> > +- reg: Address and length of the register set for the device
> > +- interrupts: Should contain the five crypto engines interrupts in numeric
> > +   order. These are global system and four descriptor rings.
> > +- clocks: the clock used by the core
> > +- clock-names: the names of the clock listed in the clocks property. These 
> > are
> > +   "ethif", "cryp"
> > +- power-domains: Must contain a reference to the PM domain.
> > +
> > +
> > +Optional properties:
> > +- interrupt-parent: Should be the phandle for the interrupt controller
> > +  that services interrupts for this device
> > +
> > +
> > +Example:
> > +   crypto: crypto@1b24 {
> > +   compatible = "mediatek,mt7623-crypto";
> > +   reg = <0 0x1b24 0 0x2>;
> > +   interrupts = ,
> > +,
> > +,
> > +,
> > +;
> > +   clocks = < CLK_TOP_ETHIF_SEL>,
> > +< CLK_ETHSYS_CRYPTO>;
> > +   clock-names = "ethif","cryp";
> > +   power-domains = < MT2701_POWER_DOMAIN_ETH>;
> > +   };
> >


--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v1 1/2] Add crypto driver support for some MediaTek chips

2016-12-08 Thread Ryder Lee
Hello,

On Mon, 2016-12-05 at 09:52 +0100, Corentin Labbe wrote:
> Hello
> 
> I have two minor comment.
> 
> On Mon, Dec 05, 2016 at 03:01:23PM +0800, Ryder Lee wrote:
> > This adds support for the MediaTek hardware accelerator on
> > mt7623/mt2701/mt8521p SoC.
> > 
> > This driver currently implement:
> > - SHA1 and SHA2 family(HMAC) hash alogrithms.
> 
> There is a typo for algorithms.
> 
> [...]
> > +/**
> > + * struct mtk_desc - DMA descriptor
> > + * @hdr:   the descriptor control header
> > + * @buf:   DMA address of input buffer segment
> > + * @ct:DMA address of command token that control operation flow
> > + * @ct_hdr:the command token control header
> > + * @tag:   the user-defined field
> > + * @tfm:   DMA address of transform state
> > + * @bound: align descriptors offset boundary
> > + *
> > + * Structure passed to the crypto engine to describe where source
> > + * data needs to be fetched and how it needs to be processed.
> > + */
> > +struct mtk_desc {
> > +   u32 hdr;
> > +   u32 buf;
> > +   u32 ct;
> > +   u32 ct_hdr;
> > +   u32 tag;
> > +   u32 tfm;
> > +   u32 bound[2];
> > +};
> 
> Do you have tested this descriptor with BE/LE kernel ?

I did not test it with BE kernel, because both CPU and accelerator in
our SoC just run on LE system. 

Thanks for reminding me, i will use byteorder conversion macros and type
identifiers.

> Regards
> Corentin Labbe




--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v1 2/2] crypto: mediatek - add DT bindings documentation

2016-12-04 Thread Ryder Lee
Add DT bindings documentation for the crypto driver

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 .../devicetree/bindings/crypto/mediatek-crypto.txt | 32 ++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt

diff --git a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt 
b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
new file mode 100644
index 000..8b1db08
--- /dev/null
+++ b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
@@ -0,0 +1,32 @@
+MediaTek cryptographic accelerators
+
+Required properties:
+- compatible: Should be "mediatek,mt7623-crypto"
+- reg: Address and length of the register set for the device
+- interrupts: Should contain the five crypto engines interrupts in numeric
+   order. These are global system and four descriptor rings.
+- clocks: the clock used by the core
+- clock-names: the names of the clock listed in the clocks property. These are
+   "ethif", "cryp"
+- power-domains: Must contain a reference to the PM domain.
+
+
+Optional properties:
+- interrupt-parent: Should be the phandle for the interrupt controller
+  that services interrupts for this device
+
+
+Example:
+   crypto: crypto@1b24 {
+   compatible = "mediatek,mt7623-crypto";
+   reg = <0 0x1b24 0 0x2>;
+   interrupts = ,
+,
+,
+,
+;
+   clocks = < CLK_TOP_ETHIF_SEL>,
+< CLK_ETHSYS_CRYPTO>;
+   clock-names = "ethif","cryp";
+   power-domains = < MT2701_POWER_DOMAIN_ETH>;
+   };
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v1 0/2] Add MediaTek crypto acclelrator driver

2016-12-04 Thread Ryder Lee
Hello,

This adds support for the MediaTek hardware accelerator on 
mt7623 SoC.

This driver currently implement: 
- SHA1 and SHA2 family(HMAC) hash alogrithms.
- AES block cipher in CBC/ECB mode with 128/196/256 bits keys.

Changes since v1:
- remove EXPORT_SYMBOL
- remove unused PRNG setting
- sort headers in alphabetical order
- add a definition for IRQ unmber
- replace ambiguous definition
- add more annotation and function comment
- add COMPILE_TEST in Kconfig


Ryder Lee (2):
  Add crypto driver support for some MediaTek chips
  crypto: mediatek - add DT bindings documentation

 .../devicetree/bindings/crypto/mediatek-crypto.txt |   32 +
 drivers/crypto/Kconfig |   17 +
 drivers/crypto/Makefile|1 +
 drivers/crypto/mediatek/Makefile   |2 +
 drivers/crypto/mediatek/mtk-aes.c  |  763 +++
 drivers/crypto/mediatek/mtk-platform.c |  580 
 drivers/crypto/mediatek/mtk-platform.h |  235 
 drivers/crypto/mediatek/mtk-regs.h |  194 +++
 drivers/crypto/mediatek/mtk-sha.c  | 1423 
 9 files changed, 3247 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
 create mode 100644 drivers/crypto/mediatek/Makefile
 create mode 100644 drivers/crypto/mediatek/mtk-aes.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.h
 create mode 100644 drivers/crypto/mediatek/mtk-regs.h
 create mode 100644 drivers/crypto/mediatek/mtk-sha.c

-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v1 1/2] Add crypto driver support for some MediaTek chips

2016-12-04 Thread Ryder Lee
This adds support for the MediaTek hardware accelerator on
mt7623/mt2701/mt8521p SoC.

This driver currently implement:
- SHA1 and SHA2 family(HMAC) hash alogrithms.
- AES block cipher in CBC/ECB mode with 128/196/256 bits keys.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/Kconfig |   17 +
 drivers/crypto/Makefile|1 +
 drivers/crypto/mediatek/Makefile   |2 +
 drivers/crypto/mediatek/mtk-aes.c  |  763 +
 drivers/crypto/mediatek/mtk-platform.c |  580 +
 drivers/crypto/mediatek/mtk-platform.h |  235 ++
 drivers/crypto/mediatek/mtk-regs.h |  194 +
 drivers/crypto/mediatek/mtk-sha.c  | 1423 
 8 files changed, 3215 insertions(+)
 create mode 100644 drivers/crypto/mediatek/Makefile
 create mode 100644 drivers/crypto/mediatek/mtk-aes.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.h
 create mode 100644 drivers/crypto/mediatek/mtk-regs.h
 create mode 100644 drivers/crypto/mediatek/mtk-sha.c

diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
index 4d2b81f..ad0a00b 100644
--- a/drivers/crypto/Kconfig
+++ b/drivers/crypto/Kconfig
@@ -553,6 +553,23 @@ config CRYPTO_DEV_ROCKCHIP
  This driver interfaces with the hardware crypto accelerator.
  Supporting cbc/ecb chainmode, and aes/des/des3_ede cipher mode.
 
+config CRYPTO_DEV_MEDIATEK
+   tristate "MediaTek's Cryptographic Engine driver"
+   depends on ARM && (ARCH_MEDIATEK || COMPILE_TEST)
+   select NEON
+   select KERNEL_MODE_NEON
+   select ARM_CRYPTO
+   select CRYPTO_AES
+   select CRYPTO_BLKCIPHER
+   select CRYPTO_SHA1_ARM_NEON
+   select CRYPTO_SHA256_ARM
+   select CRYPTO_SHA512_ARM
+   select CRYPTO_HMAC
+   help
+ This driver allows you to utilize the hardware crypto accelerator
+ which can be found on the MT7623 MT2701, MT8521p, etc 
+ Select this if you want to use it for AES/SHA1/SHA2 algorithms.
+
 source "drivers/crypto/chelsio/Kconfig"
 
 endif # CRYPTO_HW
diff --git a/drivers/crypto/Makefile b/drivers/crypto/Makefile
index ad7250f..272b51a 100644
--- a/drivers/crypto/Makefile
+++ b/drivers/crypto/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_CRYPTO_DEV_IMGTEC_HASH) += img-hash.o
 obj-$(CONFIG_CRYPTO_DEV_IXP4XX) += ixp4xx_crypto.o
 obj-$(CONFIG_CRYPTO_DEV_MV_CESA) += mv_cesa.o
 obj-$(CONFIG_CRYPTO_DEV_MARVELL_CESA) += marvell/
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mediatek/
 obj-$(CONFIG_CRYPTO_DEV_MXS_DCP) += mxs-dcp.o
 obj-$(CONFIG_CRYPTO_DEV_NIAGARA2) += n2_crypto.o
 n2_crypto-y := n2_core.o n2_asm.o
diff --git a/drivers/crypto/mediatek/Makefile b/drivers/crypto/mediatek/Makefile
new file mode 100644
index 000..187be79
--- /dev/null
+++ b/drivers/crypto/mediatek/Makefile
@@ -0,0 +1,2 @@
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mtk-crypto.o
+mtk-crypto-objs:= mtk-platform.o mtk-aes.o mtk-sha.o
diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
new file mode 100644
index 000..0208981
--- /dev/null
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -0,0 +1,763 @@
+/*
+ * Cryptographic API.
+ *
+ * Support for MediaTek AES hardware accelerator.
+ *
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Ryder Lee <ryder@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Some ideas are from atmel-aes.c drivers.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "mtk-platform.h"
+#include "mtk-regs.h"
+
+#define AES_QUEUE_LENGTH   512
+#define AES_BUFFER_ORDER   2
+#define AES_BUFFER_SIZE((PAGE_SIZE << AES_BUFFER_ORDER) \
+   & ~(AES_BLOCK_SIZE - 1))
+
+/* AES command token */
+#define AES_CT_SIZE_ECB2
+#define AES_CT_SIZE_CBC3
+#define AES_CT_CTRL_HDR0x0022
+#define AES_COMMAND0   0x0500
+#define AES_COMMAND1   0x2d06
+#define AES_COMMAND2   0xe4a63806
+
+/* AES transform information */
+#define AES_TFM_ECB(0x0 << 0)
+#define AES_TFM_CBC(0x1 << 0)
+#define AES_TFM_DECRYPT(0x5 << 0)
+#define AES_TFM_ENCRYPT(0x4 << 0)
+#define AES_TFM_SIZE(x)((x) << 8)
+#define AES_TFM_128BITS(0xb << 16)
+#define AES_TFM_192BITS(0xd << 16)
+#define AES_TFM_256BITS(0xf << 16)
+#define AES_TFM_FULL_IV(0xf << 5)
+
+/* AES flags */
+#define AES_FLAGS_MODE_MSK GENMASK(2, 0)
+#define AES_FLAGS_ECB  BIT(0)
+#define AES_FLAGS_CBC  BIT(1)
+#defin

Re: [PATCH 1/2] Add crypto driver support for some MediaTek chips

2016-12-02 Thread Ryder Lee
Hello,

On Fri, 2016-12-02 at 09:18 +0100, Corentin Labbe wrote:
> Hello
> 
> I have some minor comment inline
> 
> On Fri, Dec 02, 2016 at 11:26:44AM +0800, Ryder Lee wrote:
> > This adds support for the MediaTek hardware accelerator on
> > mt7623/mt2701/mt8521p SoC.
> > 
> > This driver currently implement:
> > - SHA1 and SHA2 family(HMAC) hash alogrithms.
> > - AES block cipher in CBC/ECB mode with 128/196/256 bits keys.
> 
> I see also a PRNG but is seems not really used.

Yes, PRNG is not implemented yet, i will remove it temporarily.

> > 
> > Signed-off-by: Ryder Lee <ryder@mediatek.com>
> > ---
> >  drivers/crypto/Kconfig |   17 +
> >  drivers/crypto/Makefile|1 +
> >  drivers/crypto/mediatek/Makefile   |2 +
> >  drivers/crypto/mediatek/mtk-aes.c  |  734 +
> >  drivers/crypto/mediatek/mtk-platform.c |  575 +
> >  drivers/crypto/mediatek/mtk-platform.h |  230 ++
> >  drivers/crypto/mediatek/mtk-regs.h |  194 +
> >  drivers/crypto/mediatek/mtk-sha.c  | 1384

> >  8 files changed, 3137 insertions(+)
> >  create mode 100644 drivers/crypto/mediatek/Makefile
> >  create mode 100644 drivers/crypto/mediatek/mtk-aes.c
> >  create mode 100644 drivers/crypto/mediatek/mtk-platform.c
> >  create mode 100644 drivers/crypto/mediatek/mtk-platform.h
> >  create mode 100644 drivers/crypto/mediatek/mtk-regs.h
> >  create mode 100644 drivers/crypto/mediatek/mtk-sha.c
> > 
> > diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
> > index 4d2b81f..5d9c803 100644
> > --- a/drivers/crypto/Kconfig
> > +++ b/drivers/crypto/Kconfig
> > @@ -553,6 +553,23 @@ config CRYPTO_DEV_ROCKCHIP
> >   This driver interfaces with the hardware crypto accelerator.
> >   Supporting cbc/ecb chainmode, and aes/des/des3_ede cipher
mode.
> >  
> > +config CRYPTO_DEV_MEDIATEK
> > +   tristate "MediaTek's Cryptographic Engine driver"
> > +   depends on ARM && ARCH_MEDIATEK
> > +   select NEON
> > +   select KERNEL_MODE_NEON
> > +   select ARM_CRYPTO
> > +   select CRYPTO_AES
> > +   select CRYPTO_BLKCIPHER
> > +   select CRYPTO_SHA1_ARM_NEON
> > +   select CRYPTO_SHA256_ARM
> > +   select CRYPTO_SHA512_ARM
> > +   select CRYPTO_HMAC
> 
> Why do you select accelerated algos ?
> Adding COMPILE_TEST could be helpfull also.

Our Hardware has complex procedure on calculate HMAC, and it get a bad
performance So i decide to use ARM NEON instruction as fallback to
speedup it.
I will add COMPILE_TEST.

> [...]
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include "mtk-platform.h"
> > +#include "mtk-regs.h"
> > +
> 
> Sort headers in alphabetical order
> 
> [...]
> > +
> > +   mtk_aes_unregister_algs();
> > +   mtk_aes_record_free(cryp);
> > +}
> > +EXPORT_SYMBOL(mtk_cipher_alg_release);
> 
> Why not EXPORT_SYMBOL_GPL ?
> Furthermore do you really need it to be exported ?

My mistake. I will remove it.

> [...]
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include "mtk-platform.h"
> > +#include "mtk-regs.h"
> > +
> 
> Sort headers in alphabetical order
> 
> [...]
> > +
> > +static void mtk_prng_reseed(struct mtk_cryp *cryp)
> > +{
> > +   /* 8 words to seed the PRNG to provide IVs */
> > +   void __iomem *base = cryp->base;
> > +   const u32 prng_key[8] = {0x48c24cfd, 0x6c07f742,
> > +   0xaee75681, 0x0f27c239,
> > +   0x79947198, 0xe2991275,
> > +   0x21ac3c7c, 0xd008c4b4};
> 
> Why do you seed with thoses constant ?
> 
> [...]
> > +
> > +static int mtk_accelerator_init(struct mtk_cryp *cryp)
> > +{
> > +   int i, err;
> > +
> > +   /* Initialize advanced interrupt controller(AIC) */
> > +   for (i = 0; i < 5; i++) {
> 
> I see this 5 for interrupt away, so perhaps a define could be used
> 
> [...]
> 
> here 
> 
> > +   for (i = 0; i < 5; i++) {
> > +   cryp->irq[i] = platform_get_irq(pdev, i);
> > +   if (cryp->irq[i] < 0) {
> > +   dev_err(cryp->dev, "no IRQ:%d resource info\n",
i);
> > +   return -ENXIO;
> > +   }
> > +   }
> [.

[PATCH 1/2] Add crypto driver support for some MediaTek chips

2016-12-01 Thread Ryder Lee
This adds support for the MediaTek hardware accelerator on
mt7623/mt2701/mt8521p SoC.

This driver currently implement:
- SHA1 and SHA2 family(HMAC) hash alogrithms.
- AES block cipher in CBC/ECB mode with 128/196/256 bits keys.

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 drivers/crypto/Kconfig |   17 +
 drivers/crypto/Makefile|1 +
 drivers/crypto/mediatek/Makefile   |2 +
 drivers/crypto/mediatek/mtk-aes.c  |  734 +
 drivers/crypto/mediatek/mtk-platform.c |  575 +
 drivers/crypto/mediatek/mtk-platform.h |  230 ++
 drivers/crypto/mediatek/mtk-regs.h |  194 +
 drivers/crypto/mediatek/mtk-sha.c  | 1384 
 8 files changed, 3137 insertions(+)
 create mode 100644 drivers/crypto/mediatek/Makefile
 create mode 100644 drivers/crypto/mediatek/mtk-aes.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.h
 create mode 100644 drivers/crypto/mediatek/mtk-regs.h
 create mode 100644 drivers/crypto/mediatek/mtk-sha.c

diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
index 4d2b81f..5d9c803 100644
--- a/drivers/crypto/Kconfig
+++ b/drivers/crypto/Kconfig
@@ -553,6 +553,23 @@ config CRYPTO_DEV_ROCKCHIP
  This driver interfaces with the hardware crypto accelerator.
  Supporting cbc/ecb chainmode, and aes/des/des3_ede cipher mode.
 
+config CRYPTO_DEV_MEDIATEK
+   tristate "MediaTek's Cryptographic Engine driver"
+   depends on ARM && ARCH_MEDIATEK
+   select NEON
+   select KERNEL_MODE_NEON
+   select ARM_CRYPTO
+   select CRYPTO_AES
+   select CRYPTO_BLKCIPHER
+   select CRYPTO_SHA1_ARM_NEON
+   select CRYPTO_SHA256_ARM
+   select CRYPTO_SHA512_ARM
+   select CRYPTO_HMAC
+   help
+ This driver allows you to utilize the hardware crypto accelerator
+ which can be found on the MT7623 MT2701, MT8521p, etc 
+ Select this if you want to use it for AES/SHA1/SHA2 algorithms.
+
 source "drivers/crypto/chelsio/Kconfig"
 
 endif # CRYPTO_HW
diff --git a/drivers/crypto/Makefile b/drivers/crypto/Makefile
index ad7250f..272b51a 100644
--- a/drivers/crypto/Makefile
+++ b/drivers/crypto/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_CRYPTO_DEV_IMGTEC_HASH) += img-hash.o
 obj-$(CONFIG_CRYPTO_DEV_IXP4XX) += ixp4xx_crypto.o
 obj-$(CONFIG_CRYPTO_DEV_MV_CESA) += mv_cesa.o
 obj-$(CONFIG_CRYPTO_DEV_MARVELL_CESA) += marvell/
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mediatek/
 obj-$(CONFIG_CRYPTO_DEV_MXS_DCP) += mxs-dcp.o
 obj-$(CONFIG_CRYPTO_DEV_NIAGARA2) += n2_crypto.o
 n2_crypto-y := n2_core.o n2_asm.o
diff --git a/drivers/crypto/mediatek/Makefile b/drivers/crypto/mediatek/Makefile
new file mode 100644
index 000..187be79
--- /dev/null
+++ b/drivers/crypto/mediatek/Makefile
@@ -0,0 +1,2 @@
+obj-$(CONFIG_CRYPTO_DEV_MEDIATEK) += mtk-crypto.o
+mtk-crypto-objs:= mtk-platform.o mtk-aes.o mtk-sha.o
diff --git a/drivers/crypto/mediatek/mtk-aes.c 
b/drivers/crypto/mediatek/mtk-aes.c
new file mode 100644
index 000..feb0e57
--- /dev/null
+++ b/drivers/crypto/mediatek/mtk-aes.c
@@ -0,0 +1,734 @@
+/*
+ * Cryptographic API.
+ *
+ * Support for MediaTek AES hardware accelerator.
+ *
+ * Copyright (c) 2016 MediaTek Inc.
+ * Author: Ryder Lee <ryder@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Some ideas are from atmel-aes.c drivers.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "mtk-platform.h"
+#include "mtk-regs.h"
+
+#define AES_QUEUE_LENGTH   512
+#define AES_BUFFER_ORDER   2
+#define AES_BUFFER_SIZE((PAGE_SIZE << AES_BUFFER_ORDER) \
+   & ~(AES_BLOCK_SIZE - 1))
+
+/* AES command token */
+#define AES_CT_SIZE_ECB2
+#define AES_CT_SIZE_CBC3
+#define AES_CT_CTRL_HDR0x0022
+#define AES_COMMAND0   0x0500
+#define AES_COMMAND1   0x2d06
+#define AES_COMMAND2   0xe4a63806
+
+/* AES transform information */
+#define AES_TFM_ECB(0x0 << 0)
+#define AES_TFM_CBC(0x1 << 0)
+#define AES_TFM_DECRYPT(0x5 << 0)
+#define AES_TFM_ENCRYPT(0x4 << 0)
+#define AES_TFM_SIZE(x)((x) << 8)
+#define AES_TFM_128BITS(0xb << 16)
+#define AES_TFM_192BITS(0xd << 16)
+#define AES_TFM_256BITS(0xf << 16)
+#define AES_TFM_FULL_IV(0xf << 5)
+
+/* AES flags */
+#define AES_FLAGS_MODE_MSK GENMASK(2, 0)
+#define AES_FLAGS_ECB  BIT(0)
+#define AES_FLAGS_CBC  BIT(1)
+#define AES_FLAGS_ENC

[PATCH 2/2] crypto: mediatek - add DT bindings documentation

2016-12-01 Thread Ryder Lee
Add DT bindings documentation for the crypto driver

Signed-off-by: Ryder Lee <ryder@mediatek.com>
---
 .../devicetree/bindings/crypto/mediatek-crypto.txt | 32 ++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt

diff --git a/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt 
b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
new file mode 100644
index 000..8b1db08
--- /dev/null
+++ b/Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
@@ -0,0 +1,32 @@
+MediaTek cryptographic accelerators
+
+Required properties:
+- compatible: Should be "mediatek,mt7623-crypto"
+- reg: Address and length of the register set for the device
+- interrupts: Should contain the five crypto engines interrupts in numeric
+   order. These are global system and four descriptor rings.
+- clocks: the clock used by the core
+- clock-names: the names of the clock listed in the clocks property. These are
+   "ethif", "cryp"
+- power-domains: Must contain a reference to the PM domain.
+
+
+Optional properties:
+- interrupt-parent: Should be the phandle for the interrupt controller
+  that services interrupts for this device
+
+
+Example:
+   crypto: crypto@1b24 {
+   compatible = "mediatek,mt7623-crypto";
+   reg = <0 0x1b24 0 0x2>;
+   interrupts = ,
+,
+,
+,
+;
+   clocks = < CLK_TOP_ETHIF_SEL>,
+< CLK_ETHSYS_CRYPTO>;
+   clock-names = "ethif","cryp";
+   power-domains = < MT2701_POWER_DOMAIN_ETH>;
+   };
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 0/2] Add MediaTek crypto acclelrator driver

2016-12-01 Thread Ryder Lee
This adds support for the MediaTek hardware accelerator on 
mt7623 SoC.

This driver currently implement: 
- SHA1 and SHA2 family(HMAC) hash alogrithms.
- AES block cipher in CBC/ECB mode with 128/196/256 bits keys.

Ryder Lee (2):
  Add crypto driver support for some MediaTek chips
  crypto: mediatek - add DT bindings documentation

 .../devicetree/bindings/crypto/mediatek-crypto.txt |   32 +
 drivers/crypto/Kconfig |   17 +
 drivers/crypto/Makefile|1 +
 drivers/crypto/mediatek/Makefile   |2 +
 drivers/crypto/mediatek/mtk-aes.c  |  734 +++
 drivers/crypto/mediatek/mtk-platform.c |  575 
 drivers/crypto/mediatek/mtk-platform.h |  230 
 drivers/crypto/mediatek/mtk-regs.h |  194 +++
 drivers/crypto/mediatek/mtk-sha.c  | 1384 
 9 files changed, 3169 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/crypto/mediatek-crypto.txt
 create mode 100644 drivers/crypto/mediatek/Makefile
 create mode 100644 drivers/crypto/mediatek/mtk-aes.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.c
 create mode 100644 drivers/crypto/mediatek/mtk-platform.h
 create mode 100644 drivers/crypto/mediatek/mtk-regs.h
 create mode 100644 drivers/crypto/mediatek/mtk-sha.c

-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html