[PATCH 08/17] crypto: inside-secure - remove unused parameter in invalidate_cache

2017-12-14 Thread Antoine Tenart
From: Ofer Heifetz 

The SafeXcel context isn't used in the cache invalidation function. This
cosmetic patch removes it (as well as from the function prototype in the
header file and when the function is called).

Signed-off-by: Ofer Heifetz 
[Antoine: commit message]
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c| 1 -
 drivers/crypto/inside-secure/safexcel.h| 1 -
 drivers/crypto/inside-secure/safexcel_cipher.c | 2 +-
 drivers/crypto/inside-secure/safexcel_hash.c   | 2 +-
 4 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index 4bcef78a08aa..f250c3c1ab0f 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -540,7 +540,6 @@ void safexcel_inv_complete(struct crypto_async_request 
*req, int error)
 }
 
 int safexcel_invalidate_cache(struct crypto_async_request *async,
- struct safexcel_context *ctx,
  struct safexcel_crypto_priv *priv,
  dma_addr_t ctxr_dma, int ring,
  struct safexcel_request *request)
diff --git a/drivers/crypto/inside-secure/safexcel.h 
b/drivers/crypto/inside-secure/safexcel.h
index 304c5838c11a..d12c2b479a5e 100644
--- a/drivers/crypto/inside-secure/safexcel.h
+++ b/drivers/crypto/inside-secure/safexcel.h
@@ -539,7 +539,6 @@ void safexcel_free_context(struct safexcel_crypto_priv 
*priv,
  struct crypto_async_request *req,
  int result_sz);
 int safexcel_invalidate_cache(struct crypto_async_request *async,
- struct safexcel_context *ctx,
  struct safexcel_crypto_priv *priv,
  dma_addr_t ctxr_dma, int ring,
  struct safexcel_request *request);
diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c 
b/drivers/crypto/inside-secure/safexcel_cipher.c
index 62382a7c4cbe..fe1d588d6a25 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -395,7 +395,7 @@ static int safexcel_cipher_send_inv(struct 
crypto_async_request *async,
struct safexcel_crypto_priv *priv = ctx->priv;
int ret;
 
-   ret = safexcel_invalidate_cache(async, >base, priv,
+   ret = safexcel_invalidate_cache(async, priv,
ctx->base.ctxr_dma, ring, request);
if (unlikely(ret))
return ret;
diff --git a/drivers/crypto/inside-secure/safexcel_hash.c 
b/drivers/crypto/inside-secure/safexcel_hash.c
index 4a4d95302a93..d233f4a09712 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -417,7 +417,7 @@ static int safexcel_ahash_send_inv(struct 
crypto_async_request *async,
struct safexcel_ahash_ctx *ctx = 
crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
int ret;
 
-   ret = safexcel_invalidate_cache(async, >base, ctx->priv,
+   ret = safexcel_invalidate_cache(async, ctx->priv,
ctx->base.ctxr_dma, ring, request);
if (unlikely(ret))
return ret;
-- 
2.14.3



[PATCH 16/17] crypto: inside-secure - EIP97 support

2017-12-14 Thread Antoine Tenart
The Inside Secure SafeXcel driver was firstly designed to support the
EIP197 cryptographic engine which is an evolution (with much more
feature, better performances) of the EIP97 cryptographic engine. This
patch convert the Inside Secure SafeXcel driver to support both engines
(EIP97 + EIP197).

The main differences are the register offsets and the context
invalidation process which is EIP197 specific. This patch adds an
indirection on the register offsets and adds checks not to send any
invalidation request when driving the EIP97. A new compatible is added
as well to bind the driver from device trees.

Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c| 212 +++--
 drivers/crypto/inside-secure/safexcel.h| 151 --
 drivers/crypto/inside-secure/safexcel_cipher.c |  20 ++-
 drivers/crypto/inside-secure/safexcel_hash.c   |  19 ++-
 4 files changed, 264 insertions(+), 138 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index 4c7f205d83f0..0d685be75241 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -108,10 +108,10 @@ static void eip197_write_firmware(struct 
safexcel_crypto_priv *priv,
writel(EIP197_PE_ICE_x_CTRL_SW_RESET |
   EIP197_PE_ICE_x_CTRL_CLR_ECC_CORR |
   EIP197_PE_ICE_x_CTRL_CLR_ECC_NON_CORR,
-  priv->base + ctrl);
+  EIP197_PE(priv) + ctrl);
 
/* Enable access to the program memory */
-   writel(prog_en, priv->base + EIP197_PE_ICE_RAM_CTRL);
+   writel(prog_en, EIP197_PE(priv) + EIP197_PE_ICE_RAM_CTRL);
 
/* Write the firmware */
for (i = 0; i < fw->size / sizeof(u32); i++)
@@ -119,12 +119,12 @@ static void eip197_write_firmware(struct 
safexcel_crypto_priv *priv,
   priv->base + EIP197_CLASSIFICATION_RAMS + i * 
sizeof(u32));
 
/* Disable access to the program memory */
-   writel(0, priv->base + EIP197_PE_ICE_RAM_CTRL);
+   writel(0, EIP197_PE(priv) + EIP197_PE_ICE_RAM_CTRL);
 
/* Release engine from reset */
-   val = readl(priv->base + ctrl);
+   val = readl(EIP197_PE(priv) + ctrl);
val &= ~EIP197_PE_ICE_x_CTRL_SW_RESET;
-   writel(val, priv->base + ctrl);
+   writel(val, EIP197_PE(priv) + ctrl);
 }
 
 static int eip197_load_firmwares(struct safexcel_crypto_priv *priv)
@@ -145,14 +145,14 @@ static int eip197_load_firmwares(struct 
safexcel_crypto_priv *priv)
 }
 
/* Clear the scratchpad memory */
-   val = readl(priv->base + EIP197_PE_ICE_SCRATCH_CTRL);
+   val = readl(EIP197_PE(priv) + EIP197_PE_ICE_SCRATCH_CTRL);
val |= EIP197_PE_ICE_SCRATCH_CTRL_CHANGE_TIMER |
   EIP197_PE_ICE_SCRATCH_CTRL_TIMER_EN |
   EIP197_PE_ICE_SCRATCH_CTRL_SCRATCH_ACCESS |
   EIP197_PE_ICE_SCRATCH_CTRL_CHANGE_ACCESS;
-   writel(val, priv->base + EIP197_PE_ICE_SCRATCH_CTRL);
+   writel(val, EIP197_PE(priv) + EIP197_PE_ICE_SCRATCH_CTRL);
 
-   memset(priv->base + EIP197_PE_ICE_SCRATCH_RAM, 0,
+   memset(EIP197_PE(priv) + EIP197_PE_ICE_SCRATCH_RAM, 0,
   EIP197_NUM_OF_SCRATCH_BLOCKS * sizeof(u32));
 
eip197_write_firmware(priv, fw[FW_IFPP], EIP197_PE_ICE_FPP_CTRL,
@@ -173,7 +173,7 @@ static int safexcel_hw_setup_cdesc_rings(struct 
safexcel_crypto_priv *priv)
u32 hdw, cd_size_rnd, val;
int i;
 
-   hdw = readl(priv->base + EIP197_HIA_OPTIONS);
+   hdw = readl(EIP197_HIA_AIC_G(priv) + EIP197_HIA_OPTIONS);
hdw &= GENMASK(27, 25);
hdw >>= 25;
 
@@ -182,26 +182,25 @@ static int safexcel_hw_setup_cdesc_rings(struct 
safexcel_crypto_priv *priv)
for (i = 0; i < priv->config.rings; i++) {
/* ring base address */
writel(lower_32_bits(priv->ring[i].cdr.base_dma),
-  priv->base + EIP197_HIA_CDR(i) + 
EIP197_HIA_xDR_RING_BASE_ADDR_LO);
+  EIP197_HIA_CDR(priv, i) + 
EIP197_HIA_xDR_RING_BASE_ADDR_LO);
writel(upper_32_bits(priv->ring[i].cdr.base_dma),
-  priv->base + EIP197_HIA_CDR(i) + 
EIP197_HIA_xDR_RING_BASE_ADDR_HI);
+  EIP197_HIA_CDR(priv, i) + 
EIP197_HIA_xDR_RING_BASE_ADDR_HI);
 
writel(EIP197_xDR_DESC_MODE_64BIT | (priv->config.cd_offset << 
16) |
   priv->config.cd_size,
-  priv->base + EIP197_HIA_CDR(i) + 
EIP197_HIA_xDR_DESC_SIZE);
+  EIP197_HIA_CDR(priv, i) + EIP197_HIA_xDR_DESC_SIZE);
writel(((EIP197_FETCH_COUNT * (cd_size_rnd << hdw)) << 16) |
   (EIP197_FETCH_COUNT * priv->config.cd_offset),
-  priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_CFG);
+  EIP197_HIA_CDR(priv, i) + EIP197_HIA_xDR_CFG);
 
/* 

[PATCH 06/17] crypto: inside-secure - EBUSY is not an error on async request

2017-12-14 Thread Antoine Tenart
From: Ofer Heifetz 

When initializing the IVs crypto_ahash_update() is called, which at some
point will call crypto_enqueue_request(). This function can return
-EBUSY when no resource is available and the request is queued. Since
this is a valid case, -EBUSY shouldn't be treated as an error.

Signed-off-by: Ofer Heifetz 
[Antoine: commit message]
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel_hash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c 
b/drivers/crypto/inside-secure/safexcel_hash.c
index d156637d6144..4a4d95302a93 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -862,7 +862,7 @@ static int safexcel_hmac_init_iv(struct ahash_request *areq,
req->last_req = true;
 
ret = crypto_ahash_update(areq);
-   if (ret && ret != -EINPROGRESS)
+   if (ret && ret != -EINPROGRESS && ret != -EBUSY)
return ret;
 
wait_for_completion_interruptible();
-- 
2.14.3



[PATCH 12/17] crypto: inside-secure - increase the ring size

2017-12-14 Thread Antoine Tenart
Increase the ring size to handle more requests in parallel, while
keeping the batch size (for interrupt coalescing) to its previous value.
The ring size and batch size are now unlinked.

Suggested-by: Ofer Heifetz 
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.h 
b/drivers/crypto/inside-secure/safexcel.h
index 531e3e9d8384..2a0ab6ce716a 100644
--- a/drivers/crypto/inside-secure/safexcel.h
+++ b/drivers/crypto/inside-secure/safexcel.h
@@ -19,11 +19,11 @@
 #define EIP197_HIA_VERSION_BE  0x35ca
 
 /* Static configuration */
-#define EIP197_DEFAULT_RING_SIZE   64
+#define EIP197_DEFAULT_RING_SIZE   400
 #define EIP197_MAX_TOKENS  5
 #define EIP197_MAX_RINGS   4
 #define EIP197_FETCH_COUNT 1
-#define EIP197_MAX_BATCH_SZEIP197_DEFAULT_RING_SIZE
+#define EIP197_MAX_BATCH_SZ64
 
 #define EIP197_GFP_FLAGS(base) ((base).flags & CRYPTO_TFM_REQ_MAY_SLEEP ? \
 GFP_KERNEL : GFP_ATOMIC)
-- 
2.14.3



[PATCH 09/17] crypto: inside-secure - move request dequeueing into a workqueue

2017-12-14 Thread Antoine Tenart
This patch moves the request dequeueing into a workqueue to improve the
coalescing of interrupts when sending requests to the engine; as the
engine is capable of having one single interrupt for n requests sent.
Using a workqueue allows to send more request at once.

Suggested-by: Ofer Heifetz 
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c| 29 ++
 drivers/crypto/inside-secure/safexcel.h|  2 +-
 drivers/crypto/inside-secure/safexcel_cipher.c | 12 +--
 drivers/crypto/inside-secure/safexcel_hash.c   | 12 +--
 4 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index f250c3c1ab0f..f422af3eed2f 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -429,8 +429,6 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
struct safexcel_request *request;
int ret, nreq = 0, cdesc = 0, rdesc = 0, commands, results;
 
-   priv->ring[ring].need_dequeue = false;
-
do {
spin_lock_bh(>ring[ring].queue_lock);
backlog = crypto_get_backlog(>ring[ring].queue);
@@ -445,8 +443,6 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
spin_lock_bh(>ring[ring].queue_lock);
crypto_enqueue_request(>ring[ring].queue, req);
spin_unlock_bh(>ring[ring].queue_lock);
-
-   priv->ring[ring].need_dequeue = true;
goto finalize;
}
 
@@ -455,7 +451,6 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
if (ret) {
kfree(request);
req->complete(req, ret);
-   priv->ring[ring].need_dequeue = true;
goto finalize;
}
 
@@ -471,9 +466,7 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
} while (nreq++ < EIP197_MAX_BATCH_SZ);
 
 finalize:
-   if (nreq == EIP197_MAX_BATCH_SZ)
-   priv->ring[ring].need_dequeue = true;
-   else if (!nreq)
+   if (!nreq)
return;
 
spin_lock_bh(>ring[ring].lock);
@@ -628,13 +621,18 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
 static void safexcel_handle_result_work(struct work_struct *work)
 {
struct safexcel_work_data *data =
-   container_of(work, struct safexcel_work_data, work);
+   container_of(work, struct safexcel_work_data, 
result_work);
struct safexcel_crypto_priv *priv = data->priv;
 
safexcel_handle_result_descriptor(priv, data->ring);
+}
+
+static void safexcel_dequeue_work(struct work_struct *work)
+{
+   struct safexcel_work_data *data =
+   container_of(work, struct safexcel_work_data, work);
 
-   if (priv->ring[data->ring].need_dequeue)
-   safexcel_dequeue(data->priv, data->ring);
+   safexcel_dequeue(data->priv, data->ring);
 }
 
 struct safexcel_ring_irq_data {
@@ -665,7 +663,10 @@ static irqreturn_t safexcel_irq_ring(int irq, void *data)
 */
dev_err(priv->dev, "RDR: fatal error.");
} else if (likely(stat & EIP197_xDR_THRESH)) {
-   queue_work(priv->ring[ring].workqueue, 
>ring[ring].work_data.work);
+   queue_work(priv->ring[ring].workqueue,
+  >ring[ring].work_data.result_work);
+   queue_work(priv->ring[ring].workqueue,
+  >ring[ring].work_data.work);
}
 
/* ACK the interrupts */
@@ -846,7 +847,9 @@ static int safexcel_probe(struct platform_device *pdev)
 
priv->ring[i].work_data.priv = priv;
priv->ring[i].work_data.ring = i;
-   INIT_WORK(>ring[i].work_data.work, 
safexcel_handle_result_work);
+   INIT_WORK(>ring[i].work_data.result_work,
+ safexcel_handle_result_work);
+   INIT_WORK(>ring[i].work_data.work, safexcel_dequeue_work);
 
snprintf(wq_name, 9, "wq_ring%d", i);
priv->ring[i].workqueue = 
create_singlethread_workqueue(wq_name);
diff --git a/drivers/crypto/inside-secure/safexcel.h 
b/drivers/crypto/inside-secure/safexcel.h
index d12c2b479a5e..8e9c65183439 100644
--- a/drivers/crypto/inside-secure/safexcel.h
+++ b/drivers/crypto/inside-secure/safexcel.h
@@ -459,6 +459,7 @@ struct safexcel_config {
 
 struct safexcel_work_data {
struct work_struct work;
+   struct work_struct result_work;
struct safexcel_crypto_priv *priv;
int ring;
 };
@@ -489,7 +490,6 @@ struct 

[PATCH 07/17] crypto: inside-secure - move cipher crypto mode to request context

2017-12-14 Thread Antoine Tenart
From: Ofer Heifetz 

The cipher direction can be different for requests within the same
transformation context. This patch moves the direction flag from the
context to the request scope.

Signed-off-by: Ofer Heifetz 
[Antoine: commit message]
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel_cipher.c | 11 +++
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c 
b/drivers/crypto/inside-secure/safexcel_cipher.c
index 794849b859a7..62382a7c4cbe 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -27,7 +27,6 @@ struct safexcel_cipher_ctx {
struct safexcel_context base;
struct safexcel_crypto_priv *priv;
 
-   enum safexcel_cipher_direction direction;
u32 mode;
 
__le32 key[8];
@@ -35,6 +34,7 @@ struct safexcel_cipher_ctx {
 };
 
 struct safexcel_cipher_req {
+   enum safexcel_cipher_direction direction;
bool needs_inv;
 };
 
@@ -97,12 +97,15 @@ static int safexcel_aes_setkey(struct crypto_skcipher 
*ctfm, const u8 *key,
 }
 
 static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
+   struct crypto_async_request *async,
struct safexcel_command_desc *cdesc)
 {
struct safexcel_crypto_priv *priv = ctx->priv;
+   struct skcipher_request *req = skcipher_request_cast(async);
+   struct safexcel_cipher_req *sreq = skcipher_request_ctx(req);
int ctrl_size;
 
-   if (ctx->direction == SAFEXCEL_ENCRYPT)
+   if (sreq->direction == SAFEXCEL_ENCRYPT)
cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_CRYPTO_OUT;
else
cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_CRYPTO_IN;
@@ -245,7 +248,7 @@ static int safexcel_aes_send(struct crypto_async_request 
*async,
n_cdesc++;
 
if (n_cdesc == 1) {
-   safexcel_context_control(ctx, cdesc);
+   safexcel_context_control(ctx, async, cdesc);
safexcel_cipher_token(ctx, async, cdesc, req->cryptlen);
}
 
@@ -469,7 +472,7 @@ static int safexcel_aes(struct skcipher_request *req,
int ret, ring;
 
sreq->needs_inv = false;
-   ctx->direction = dir;
+   sreq->direction = dir;
ctx->mode = mode;
 
if (ctx->base.ctxr) {
-- 
2.14.3



[PATCH 05/17] crypto: inside-secure - refrain from unneeded invalidations

2017-12-14 Thread Antoine Tenart
From: Ofer Heifetz 

The check to know if an invalidation is needed (i.e. when the context
changes) is done even if the context does not exist yet. This happens
when first setting a key for ciphers and/or hmac operations.

This commits adds a check in the _setkey functions to only check if an
invalidation is needed when a context exists, as there is no need to
perform this check otherwise.

Signed-off-by: Ofer Heifetz 
[Antoine: commit message and added a comment and reworked one of the
checks]
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel_cipher.c | 10 ++
 drivers/crypto/inside-secure/safexcel_hash.c   | 24 
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c 
b/drivers/crypto/inside-secure/safexcel_cipher.c
index fcc0a606d748..794849b859a7 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -78,10 +78,12 @@ static int safexcel_aes_setkey(struct crypto_skcipher 
*ctfm, const u8 *key,
return ret;
}
 
-   for (i = 0; i < len / sizeof(u32); i++) {
-   if (ctx->key[i] != cpu_to_le32(aes.key_enc[i])) {
-   ctx->base.needs_inv = true;
-   break;
+   if (ctx->base.ctxr_dma) {
+   for (i = 0; i < len / sizeof(u32); i++) {
+   if (ctx->key[i] != cpu_to_le32(aes.key_enc[i])) {
+   ctx->base.needs_inv = true;
+   break;
+   }
}
}
 
diff --git a/drivers/crypto/inside-secure/safexcel_hash.c 
b/drivers/crypto/inside-secure/safexcel_hash.c
index e53e6b2331db..d156637d6144 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -527,10 +527,16 @@ static int safexcel_ahash_enqueue(struct ahash_request 
*areq)
 
req->needs_inv = false;
 
-   if (req->processed && ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED)
-   ctx->base.needs_inv = safexcel_ahash_needs_inv_get(areq);
-
if (ctx->base.ctxr) {
+   if (!ctx->base.needs_inv && req->processed &&
+   ctx->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED)
+   /* We're still setting needs_inv here, even though it is
+* cleared right away, because the needs_inv flag can be
+* set in other functions and we want to keep the same
+* logic.
+*/
+   ctx->base.needs_inv = 
safexcel_ahash_needs_inv_get(areq);
+
if (ctx->base.needs_inv) {
ctx->base.needs_inv = false;
req->needs_inv = true;
@@ -928,11 +934,13 @@ static int safexcel_hmac_sha1_setkey(struct crypto_ahash 
*tfm, const u8 *key,
if (ret)
return ret;
 
-   for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
-   if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) ||
-   ctx->opad[i] != le32_to_cpu(ostate.state[i])) {
-   ctx->base.needs_inv = true;
-   break;
+   if (ctx->base.ctxr) {
+   for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
+   if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) ||
+   ctx->opad[i] != le32_to_cpu(ostate.state[i])) {
+   ctx->base.needs_inv = true;
+   break;
+   }
}
}
 
-- 
2.14.3



[PATCH 02/17] crypto: inside-secure - fix typo in a comment

2017-12-14 Thread Antoine Tenart
Cosmetic patch fixing one typo in one of the driver's comments.

Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel_hash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c 
b/drivers/crypto/inside-secure/safexcel_hash.c
index d2d4551565fa..d4afc070ff58 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -504,7 +504,7 @@ static int safexcel_ahash_cache(struct ahash_request *areq)
return areq->nbytes;
}
 
-   /* We could'nt cache all the data */
+   /* We couldn't cache all the data */
return -E2BIG;
 }
 
-- 
2.14.3



[PATCH 00/17] crypto: inside-secure - various improvements

2017-12-14 Thread Antoine Tenart
Hi Herbert,

This series depends on the previous one, "crypto: inside-secure - set of
fixes"[1]. The series contains various improvements to the Inside Secure
SafeXcel driver, which are bundled in a single series to avoid any
conflict.

- Patches 1-4 are cosmetic fixes (typo, more comments and dead code removal).
- Patches 5-8 are small improvements, mostly to improve the invalidation
  path.
- Patches 9-15 improve the request processing in the driver, by moving
  the requests dequeing into a workqueue and the result handling into a
  threaded IRQ thread. Other small improvements are made in the same
  direction.
- Patches 16-17 introduce the support of another revision of this crypto
  engine, similar to the one already supported. This crypto engine can be
  found on Marvell boards. (The corresponding device tree patches will follow
  once this series lands in).

Thanks!
Antoine

[1] https://www.spinics.net/lists/linux-crypto/msg30313.html

Antoine Tenart (13):
  crypto: inside-secure - remove extra empty line
  crypto: inside-secure - fix typo in a comment
  crypto: inside-secure - remove useless memset
  crypto: inside-secure - comment the caching function
  crypto: inside-secure - move request dequeueing into a workqueue
  crypto: inside-secure - use threaded IRQs for result handling
  crypto: inside-secure - dequeue all requests at once
  crypto: inside-secure - increase the ring size
  crypto: inside-secure - acknowledge the result requests all at once
  crypto: inside-secure - handle more result requests when counter is
full
  crypto: inside-secure - retry to proceed the request later on fail
  crypto: inside-secure - EIP97 support
  Documentation/bindings: crypto: document the SafeXcel EIP97 compatible

Ofer Heifetz (4):
  crypto: inside-secure - refrain from unneeded invalidations
  crypto: inside-secure - EBUSY is not an error on async request
  crypto: inside-secure - move cipher crypto mode to request context
  crypto: inside-secure - remove unused parameter in invalidate_cache

 .../bindings/crypto/inside-secure-safexcel.txt |   3 +-
 drivers/crypto/inside-secure/safexcel.c| 370 ++---
 drivers/crypto/inside-secure/safexcel.h| 173 +++---
 drivers/crypto/inside-secure/safexcel_cipher.c |  53 +--
 drivers/crypto/inside-secure/safexcel_hash.c   |  69 ++--
 5 files changed, 449 insertions(+), 219 deletions(-)

-- 
2.14.3



[PATCH 15/17] crypto: inside-secure - retry to proceed the request later on fail

2017-12-14 Thread Antoine Tenart
The dequeueing function was putting back a request in the crypto queue
on failure (when not enough resources are available) which is not
perfect as the request will be handled much later. This patch updates
this logic by keeping a reference on the failed request to try
proceeding it later when enough resources are available.

Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c | 32 +++-
 drivers/crypto/inside-secure/safexcel.h |  6 ++
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index 8042922b4ed8..4c7f205d83f0 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -446,29 +446,36 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
struct safexcel_request *request;
int ret, nreq = 0, cdesc = 0, rdesc = 0, commands, results;
 
+   /* If a request wasn't properly dequeued because of a lack of resources,
+* proceeded it first,
+*/
+   req = priv->ring[ring].req;
+   backlog = priv->ring[ring].backlog;
+   if (req)
+   goto handle_req;
+
while (true) {
spin_lock_bh(>ring[ring].queue_lock);
backlog = crypto_get_backlog(>ring[ring].queue);
req = crypto_dequeue_request(>ring[ring].queue);
spin_unlock_bh(>ring[ring].queue_lock);
 
-   if (!req)
+   if (!req) {
+   priv->ring[ring].req = NULL;
+   priv->ring[ring].backlog = NULL;
goto finalize;
+   }
 
+handle_req:
request = kzalloc(sizeof(*request), EIP197_GFP_FLAGS(*req));
-   if (!request) {
-   spin_lock_bh(>ring[ring].queue_lock);
-   crypto_enqueue_request(>ring[ring].queue, req);
-   spin_unlock_bh(>ring[ring].queue_lock);
-   goto finalize;
-   }
+   if (!request)
+   goto request_failed;
 
ctx = crypto_tfm_ctx(req->tfm);
ret = ctx->send(req, ring, request, , );
if (ret) {
kfree(request);
-   req->complete(req, ret);
-   goto finalize;
+   goto request_failed;
}
 
if (backlog)
@@ -483,6 +490,13 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
nreq++;
}
 
+request_failed:
+   /* Not enough resources to handle all the requests. Bail out and save
+* the request and the backlog for the next dequeue call (per-ring).
+*/
+   priv->ring[ring].req = req;
+   priv->ring[ring].backlog = backlog;
+
 finalize:
if (!nreq)
return;
diff --git a/drivers/crypto/inside-secure/safexcel.h 
b/drivers/crypto/inside-secure/safexcel.h
index 0c47e792192d..d4955abf873b 100644
--- a/drivers/crypto/inside-secure/safexcel.h
+++ b/drivers/crypto/inside-secure/safexcel.h
@@ -499,6 +499,12 @@ struct safexcel_crypto_priv {
 
/* The ring is currently handling at least one request */
bool busy;
+
+   /* Store for current requests when bailing out of the dequeueing
+* function when no enough resources are available.
+*/
+   struct crypto_async_request *req;
+   struct crypto_async_request *backlog;
} ring[EIP197_MAX_RINGS];
 };
 
-- 
2.14.3



[PATCH 04/17] crypto: inside-secure - comment the caching function

2017-12-14 Thread Antoine Tenart
Cosmetic patch adding a few comments to the ahash caching function to
understand easily what calculations are made in the functions; and how
the function is working.

Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel_hash.c | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c 
b/drivers/crypto/inside-secure/safexcel_hash.c
index ac922b91b2d7..e53e6b2331db 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -484,13 +484,23 @@ static int safexcel_ahash_exit_inv(struct crypto_tfm *tfm)
return 0;
 }
 
+/* safexcel_ahash_cache: cache data until at least one request can be sent to
+ * the engine, aka. when there is at least 1 block size in the pipe.
+ */
 static int safexcel_ahash_cache(struct ahash_request *areq)
 {
struct safexcel_ahash_req *req = ahash_request_ctx(areq);
struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
int queued, cache_len;
 
+   /* cache_len: everyting accepted by the driver but not sent yet,
+* tot sz handled by update() - last req sz - tot sz handled by send()
+*/
cache_len = req->len - areq->nbytes - req->processed;
+   /* queued: everything accepted by the driver which will be handled by
+* the next send() calls.
+* tot sz handled by update() - tot sz handled by send()
+*/
queued = req->len - req->processed;
 
/*
-- 
2.14.3



[PATCH 01/17] crypto: inside-secure - remove extra empty line

2017-12-14 Thread Antoine Tenart
Cosmetic patch removing an extra empty line between header inclusions.

Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel_hash.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c 
b/drivers/crypto/inside-secure/safexcel_hash.c
index 0c5a5820b06e..d2d4551565fa 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -14,7 +14,6 @@
 #include 
 #include 
 
-
 #include "safexcel.h"
 
 struct safexcel_ahash_ctx {
-- 
2.14.3



[PATCH 03/17] crypto: inside-secure - remove useless memset

2017-12-14 Thread Antoine Tenart
This patch removes an useless memset in the ahash_export function, as
the zeroed buffer will be entirely overridden the next line.

Suggested-by: Ofer Heifetz 
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel_hash.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c 
b/drivers/crypto/inside-secure/safexcel_hash.c
index d4afc070ff58..ac922b91b2d7 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -624,7 +624,6 @@ static int safexcel_ahash_export(struct ahash_request 
*areq, void *out)
export->processed = req->processed;
 
memcpy(export->state, req->state, req->state_sz);
-   memset(export->cache, 0, crypto_ahash_blocksize(ahash));
memcpy(export->cache, req->cache, crypto_ahash_blocksize(ahash));
 
return 0;
-- 
2.14.3



[PATCH 14/17] crypto: inside-secure - handle more result requests when counter is full

2017-12-14 Thread Antoine Tenart
This patch modifies the result handling logic to continue handling
results when the completed requests counter is full and not showing the
actual number of requests to handle.

Suggested-by: Ofer Heifetz 
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c | 15 ---
 drivers/crypto/inside-secure/safexcel.h |  2 ++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index 36c9a72c47b1..8042922b4ed8 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -600,12 +600,15 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
 {
struct safexcel_request *sreq;
struct safexcel_context *ctx;
-   int ret, i, nreq, ndesc = 0, tot_descs = 0, done;
+   int ret, i, nreq, ndesc, tot_descs, done;
bool should_complete;
 
+handle_results:
+   tot_descs = 0;
+
nreq = readl(priv->base + EIP197_HIA_RDR(ring) + 
EIP197_HIA_xDR_PROC_COUNT);
-   nreq >>= 24;
-   nreq &= GENMASK(6, 0);
+   nreq >>= EIP197_xDR_PROC_xD_PKT_OFFSET;
+   nreq &= EIP197_xDR_PROC_xD_PKT_MASK;
if (!nreq)
goto requests_left;
 
@@ -642,6 +645,12 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
   priv->base + EIP197_HIA_RDR(ring) + 
EIP197_HIA_xDR_PROC_COUNT);
}
 
+   /* If the number of requests overflowed the counter, try to proceed more
+* requests.
+*/
+   if (nreq == EIP197_xDR_PROC_xD_PKT_MASK)
+   goto handle_results;
+
 requests_left:
spin_lock_bh(>ring[ring].egress_lock);
 
diff --git a/drivers/crypto/inside-secure/safexcel.h 
b/drivers/crypto/inside-secure/safexcel.h
index 2a0ab6ce716a..0c47e792192d 100644
--- a/drivers/crypto/inside-secure/safexcel.h
+++ b/drivers/crypto/inside-secure/safexcel.h
@@ -117,6 +117,8 @@
 #define EIP197_xDR_PREP_CLR_COUNT  BIT(31)
 
 /* EIP197_HIA_xDR_PROC_COUNT */
+#define EIP197_xDR_PROC_xD_PKT_OFFSET  24
+#define EIP197_xDR_PROC_xD_PKT_MASKGENMASK(6, 0)
 #define EIP197_xDR_PROC_xD_COUNT(n)((n) << 2)
 #define EIP197_xDR_PROC_xD_PKT(n)  ((n) << 24)
 #define EIP197_xDR_PROC_CLR_COUNT  BIT(31)
-- 
2.14.3



[PATCH 10/17] crypto: inside-secure - use threaded IRQs for result handling

2017-12-14 Thread Antoine Tenart
This patch moves the result handling from an IRQ handler to a threaded
IRQ handler, to improve the number of complete requests being handled at
once.

Suggested-by: Ofer Heifetz 
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c | 41 ++---
 drivers/crypto/inside-secure/safexcel.h |  1 -
 2 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index f422af3eed2f..aa4943cad55e 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -618,15 +618,6 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
}
 }
 
-static void safexcel_handle_result_work(struct work_struct *work)
-{
-   struct safexcel_work_data *data =
-   container_of(work, struct safexcel_work_data, 
result_work);
-   struct safexcel_crypto_priv *priv = data->priv;
-
-   safexcel_handle_result_descriptor(priv, data->ring);
-}
-
 static void safexcel_dequeue_work(struct work_struct *work)
 {
struct safexcel_work_data *data =
@@ -644,12 +635,12 @@ static irqreturn_t safexcel_irq_ring(int irq, void *data)
 {
struct safexcel_ring_irq_data *irq_data = data;
struct safexcel_crypto_priv *priv = irq_data->priv;
-   int ring = irq_data->ring;
+   int ring = irq_data->ring, rc = IRQ_NONE;
u32 status, stat;
 
status = readl(priv->base + EIP197_HIA_AIC_R_ENABLED_STAT(ring));
if (!status)
-   return IRQ_NONE;
+   return rc;
 
/* RDR interrupts */
if (status & EIP197_RDR_IRQ(ring)) {
@@ -663,10 +654,7 @@ static irqreturn_t safexcel_irq_ring(int irq, void *data)
 */
dev_err(priv->dev, "RDR: fatal error.");
} else if (likely(stat & EIP197_xDR_THRESH)) {
-   queue_work(priv->ring[ring].workqueue,
-  >ring[ring].work_data.result_work);
-   queue_work(priv->ring[ring].workqueue,
-  >ring[ring].work_data.work);
+   rc = IRQ_WAKE_THREAD;
}
 
/* ACK the interrupts */
@@ -677,11 +665,26 @@ static irqreturn_t safexcel_irq_ring(int irq, void *data)
/* ACK the interrupts */
writel(status, priv->base + EIP197_HIA_AIC_R_ACK(ring));
 
+   return rc;
+}
+
+static irqreturn_t safexcel_irq_ring_thread(int irq, void *data)
+{
+   struct safexcel_ring_irq_data *irq_data = data;
+   struct safexcel_crypto_priv *priv = irq_data->priv;
+   int ring = irq_data->ring;
+
+   safexcel_handle_result_descriptor(priv, ring);
+
+   queue_work(priv->ring[ring].workqueue,
+  >ring[ring].work_data.work);
+
return IRQ_HANDLED;
 }
 
 static int safexcel_request_ring_irq(struct platform_device *pdev, const char 
*name,
 irq_handler_t handler,
+irq_handler_t threaded_handler,
 struct safexcel_ring_irq_data 
*ring_irq_priv)
 {
int ret, irq = platform_get_irq_byname(pdev, name);
@@ -691,8 +694,9 @@ static int safexcel_request_ring_irq(struct platform_device 
*pdev, const char *n
return irq;
}
 
-   ret = devm_request_irq(>dev, irq, handler, 0,
-  dev_name(>dev), ring_irq_priv);
+   ret = devm_request_threaded_irq(>dev, irq, handler,
+   threaded_handler, IRQF_ONESHOT,
+   dev_name(>dev), ring_irq_priv);
if (ret) {
dev_err(>dev, "unable to request IRQ %d\n", irq);
return ret;
@@ -839,6 +843,7 @@ static int safexcel_probe(struct platform_device *pdev)
 
snprintf(irq_name, 6, "ring%d", i);
irq = safexcel_request_ring_irq(pdev, irq_name, 
safexcel_irq_ring,
+   safexcel_irq_ring_thread,
ring_irq);
if (irq < 0) {
ret = irq;
@@ -847,8 +852,6 @@ static int safexcel_probe(struct platform_device *pdev)
 
priv->ring[i].work_data.priv = priv;
priv->ring[i].work_data.ring = i;
-   INIT_WORK(>ring[i].work_data.result_work,
- safexcel_handle_result_work);
INIT_WORK(>ring[i].work_data.work, safexcel_dequeue_work);
 
snprintf(wq_name, 9, "wq_ring%d", i);
diff --git a/drivers/crypto/inside-secure/safexcel.h 
b/drivers/crypto/inside-secure/safexcel.h
index 8e9c65183439..fffddefb0d9b 100644
--- a/drivers/crypto/inside-secure/safexcel.h
+++ b/drivers/crypto/inside-secure/safexcel.h
@@ -459,7 

[PATCH 17/17] Documentation/bindings: crypto: document the SafeXcel EIP97 compatible

2017-12-14 Thread Antoine Tenart
This patch adds the SafeXcel EIP97 compatible to the Inside Secure
device tree bindings documentation.

Signed-off-by: Antoine Tenart 
---
 Documentation/devicetree/bindings/crypto/inside-secure-safexcel.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git 
a/Documentation/devicetree/bindings/crypto/inside-secure-safexcel.txt 
b/Documentation/devicetree/bindings/crypto/inside-secure-safexcel.txt
index fbc07d12322f..30c3ce6b502e 100644
--- a/Documentation/devicetree/bindings/crypto/inside-secure-safexcel.txt
+++ b/Documentation/devicetree/bindings/crypto/inside-secure-safexcel.txt
@@ -1,7 +1,8 @@
 Inside Secure SafeXcel cryptographic engine
 
 Required properties:
-- compatible: Should be "inside-secure,safexcel-eip197".
+- compatible: Should be "inside-secure,safexcel-eip197" or
+  "inside-secure,safexcel-eip97".
 - reg: Base physical address of the engine and length of memory mapped region.
 - interrupts: Interrupt numbers for the rings and engine.
 - interrupt-names: Should be "ring0", "ring1", "ring2", "ring3", "eip", "mem".
-- 
2.14.3



[PATCH 11/17] crypto: inside-secure - dequeue all requests at once

2017-12-14 Thread Antoine Tenart
This patch updates the dequeueing logic to dequeue all requests at once.
Since we can have many requests in the queue, the interrupt coalescing
is kept so that the ring interrupt fires every EIP197_MAX_BATCH_SZ at
most.

To allow dequeueing all requests at once while still using reasonable
settings for the interrupt coalescing, the result handling function was
updated to setup the threshold interrupt when needed (i.e. when more
requests than EIP197_MAX_BATCH_SZ are in the queue). When using this
capability the ring is marked as busy so that the dequeue function
enqueue new requests without setting the threshold interrupt.

Suggested-by: Ofer Heifetz 
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c | 60 ++---
 drivers/crypto/inside-secure/safexcel.h |  8 +
 2 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index aa4943cad55e..db7ad9d3eeed 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -422,6 +422,23 @@ static int safexcel_hw_init(struct safexcel_crypto_priv 
*priv)
return 0;
 }
 
+/* Called with ring's lock taken */
+int safexcel_try_push_requests(struct safexcel_crypto_priv *priv, int ring,
+  int reqs)
+{
+   int coal = min_t(int, reqs, EIP197_MAX_BATCH_SZ);
+
+   if (!coal)
+   return 0;
+
+   /* Configure when we want an interrupt */
+   writel(EIP197_HIA_RDR_THRESH_PKT_MODE |
+  EIP197_HIA_RDR_THRESH_PROC_PKT(coal),
+  priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_THRESH);
+
+   return coal;
+}
+
 void safexcel_dequeue(struct safexcel_crypto_priv *priv, int ring)
 {
struct crypto_async_request *req, *backlog;
@@ -429,7 +446,7 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
struct safexcel_request *request;
int ret, nreq = 0, cdesc = 0, rdesc = 0, commands, results;
 
-   do {
+   while (true) {
spin_lock_bh(>ring[ring].queue_lock);
backlog = crypto_get_backlog(>ring[ring].queue);
req = crypto_dequeue_request(>ring[ring].queue);
@@ -463,18 +480,24 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
 
cdesc += commands;
rdesc += results;
-   } while (nreq++ < EIP197_MAX_BATCH_SZ);
+   nreq++;
+   }
 
 finalize:
if (!nreq)
return;
 
-   spin_lock_bh(>ring[ring].lock);
+   spin_lock_bh(>ring[ring].egress_lock);
 
-   /* Configure when we want an interrupt */
-   writel(EIP197_HIA_RDR_THRESH_PKT_MODE |
-  EIP197_HIA_RDR_THRESH_PROC_PKT(nreq),
-  priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_THRESH);
+   if (!priv->ring[ring].busy) {
+   nreq -= safexcel_try_push_requests(priv, ring, nreq);
+   if (nreq)
+   priv->ring[ring].busy = true;
+   }
+
+   priv->ring[ring].requests_left += nreq;
+
+   spin_unlock_bh(>ring[ring].egress_lock);
 
/* let the RDR know we have pending descriptors */
writel((rdesc * priv->config.rd_offset) << 2,
@@ -483,8 +506,6 @@ void safexcel_dequeue(struct safexcel_crypto_priv *priv, 
int ring)
/* let the CDR know we have pending descriptors */
writel((cdesc * priv->config.cd_offset) << 2,
   priv->base + EIP197_HIA_CDR(ring) + EIP197_HIA_xDR_PREP_COUNT);
-
-   spin_unlock_bh(>ring[ring].lock);
 }
 
 void safexcel_free_context(struct safexcel_crypto_priv *priv,
@@ -579,14 +600,14 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
 {
struct safexcel_request *sreq;
struct safexcel_context *ctx;
-   int ret, i, nreq, ndesc = 0;
+   int ret, i, nreq, ndesc = 0, done;
bool should_complete;
 
nreq = readl(priv->base + EIP197_HIA_RDR(ring) + 
EIP197_HIA_xDR_PROC_COUNT);
nreq >>= 24;
nreq &= GENMASK(6, 0);
if (!nreq)
-   return;
+   goto requests_left;
 
for (i = 0; i < nreq; i++) {
spin_lock_bh(>ring[ring].egress_lock);
@@ -601,7 +622,7 @@ static inline void safexcel_handle_result_descriptor(struct 
safexcel_crypto_priv
if (ndesc < 0) {
kfree(sreq);
dev_err(priv->dev, "failed to handle result (%d)", 
ndesc);
-   return;
+   goto requests_left;
}
 
writel(EIP197_xDR_PROC_xD_PKT(1) |
@@ -616,6 +637,18 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
 
kfree(sreq);
}
+
+requests_left:
+   spin_lock_bh(>ring[ring].egress_lock);
+
+   done = 

[PATCH 13/17] crypto: inside-secure - acknowledge the result requests all at once

2017-12-14 Thread Antoine Tenart
This patches moves the result request acknowledgment from a per request
process to acknowledging all the result requests handled at once.

Suggested-by: Ofer Heifetz 
Signed-off-by: Antoine Tenart 
---
 drivers/crypto/inside-secure/safexcel.c | 16 ++--
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel.c 
b/drivers/crypto/inside-secure/safexcel.c
index db7ad9d3eeed..36c9a72c47b1 100644
--- a/drivers/crypto/inside-secure/safexcel.c
+++ b/drivers/crypto/inside-secure/safexcel.c
@@ -600,7 +600,7 @@ static inline void safexcel_handle_result_descriptor(struct 
safexcel_crypto_priv
 {
struct safexcel_request *sreq;
struct safexcel_context *ctx;
-   int ret, i, nreq, ndesc = 0, done;
+   int ret, i, nreq, ndesc = 0, tot_descs = 0, done;
bool should_complete;
 
nreq = readl(priv->base + EIP197_HIA_RDR(ring) + 
EIP197_HIA_xDR_PROC_COUNT);
@@ -622,13 +622,9 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
if (ndesc < 0) {
kfree(sreq);
dev_err(priv->dev, "failed to handle result (%d)", 
ndesc);
-   goto requests_left;
+   goto acknowledge;
}
 
-   writel(EIP197_xDR_PROC_xD_PKT(1) |
-  EIP197_xDR_PROC_xD_COUNT(ndesc * priv->config.rd_offset),
-  priv->base + EIP197_HIA_RDR(ring) + 
EIP197_HIA_xDR_PROC_COUNT);
-
if (should_complete) {
local_bh_disable();
sreq->req->complete(sreq->req, ret);
@@ -636,6 +632,14 @@ static inline void 
safexcel_handle_result_descriptor(struct safexcel_crypto_priv
}
 
kfree(sreq);
+   tot_descs += ndesc;
+   }
+
+acknowledge:
+   if (i) {
+   writel(EIP197_xDR_PROC_xD_PKT(i) |
+  EIP197_xDR_PROC_xD_COUNT(tot_descs * 
priv->config.rd_offset),
+  priv->base + EIP197_HIA_RDR(ring) + 
EIP197_HIA_xDR_PROC_COUNT);
}
 
 requests_left:
-- 
2.14.3



[PATCH 09/10] staging: ccree: fix fips event irq handling build

2017-12-14 Thread Gilad Ben-Yossef
When moving from internal for kernel FIPS infrastructure the FIPS event irq
handling code was left with the old ifdef by mistake. Fix it.

Fixes: b7e607bf33a2 ("staging: ccree: move FIPS support to kernel 
infrastructure")
Cc: sta...@vger.kernel.org
Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/ssi_driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index df6d415..56b5d45 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -123,7 +123,7 @@ static irqreturn_t cc_isr(int irq, void *dev_id)
irr &= ~CC_COMP_IRQ_MASK;
complete_request(drvdata);
}
-#ifdef CC_SUPPORT_FIPS
+#ifdef CONFIG_CRYPTO_FIPS
/* TEE FIPS interrupt */
if (irr & CC_GPR0_IRQ_MASK) {
/* Mask interrupt - will be unmasked in Deferred service
-- 
2.7.4



[PATCH 08/10] staging: ccree: remove ssi_config.h

2017-12-14 Thread Gilad Ben-Yossef
Now that the only thing left in ssi_config is the maximum
DMA mask length we get move that to ssi_driver.h and get
rid of the file.

All of ccree is now runtime configurable or under Kbuild control.

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/cc_debugfs.c  |  1 -
 drivers/staging/ccree/ssi_aead.c|  1 -
 drivers/staging/ccree/ssi_buffer_mgr.h  |  1 -
 drivers/staging/ccree/ssi_cipher.c  |  1 -
 drivers/staging/ccree/ssi_config.h  | 30 --
 drivers/staging/ccree/ssi_driver.c  |  1 -
 drivers/staging/ccree/ssi_driver.h  |  4 +++-
 drivers/staging/ccree/ssi_fips.c|  1 -
 drivers/staging/ccree/ssi_hash.c|  1 -
 drivers/staging/ccree/ssi_ivgen.c   |  1 -
 drivers/staging/ccree/ssi_pm.c  |  1 -
 drivers/staging/ccree/ssi_pm.h  |  1 -
 drivers/staging/ccree/ssi_request_mgr.c |  1 -
 13 files changed, 3 insertions(+), 42 deletions(-)
 delete mode 100644 drivers/staging/ccree/ssi_config.h

diff --git a/drivers/staging/ccree/cc_debugfs.c 
b/drivers/staging/ccree/cc_debugfs.c
index 7768a7b..7cd33957 100644
--- a/drivers/staging/ccree/cc_debugfs.c
+++ b/drivers/staging/ccree/cc_debugfs.c
@@ -17,7 +17,6 @@
 #include 
 #include 
 #include 
-#include "ssi_config.h"
 #include "ssi_driver.h"
 #include "cc_crypto_ctx.h"
 
diff --git a/drivers/staging/ccree/ssi_aead.c b/drivers/staging/ccree/ssi_aead.c
index ce26e68..1522b00 100644
--- a/drivers/staging/ccree/ssi_aead.c
+++ b/drivers/staging/ccree/ssi_aead.c
@@ -28,7 +28,6 @@
 #include 
 #include 
 #include 
-#include "ssi_config.h"
 #include "ssi_driver.h"
 #include "ssi_buffer_mgr.h"
 #include "ssi_aead.h"
diff --git a/drivers/staging/ccree/ssi_buffer_mgr.h 
b/drivers/staging/ccree/ssi_buffer_mgr.h
index da43354..395c93f 100644
--- a/drivers/staging/ccree/ssi_buffer_mgr.h
+++ b/drivers/staging/ccree/ssi_buffer_mgr.h
@@ -23,7 +23,6 @@
 
 #include 
 
-#include "ssi_config.h"
 #include "ssi_driver.h"
 
 enum cc_req_dma_buf_type {
diff --git a/drivers/staging/ccree/ssi_cipher.c 
b/drivers/staging/ccree/ssi_cipher.c
index f940568..86800a7 100644
--- a/drivers/staging/ccree/ssi_cipher.c
+++ b/drivers/staging/ccree/ssi_cipher.c
@@ -26,7 +26,6 @@
 #include 
 #include 
 
-#include "ssi_config.h"
 #include "ssi_driver.h"
 #include "cc_lli_defs.h"
 #include "ssi_buffer_mgr.h"
diff --git a/drivers/staging/ccree/ssi_config.h 
b/drivers/staging/ccree/ssi_config.h
deleted file mode 100644
index bc90ad0..000
--- a/drivers/staging/ccree/ssi_config.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2012-2017 ARM Limited or its affiliates.
- *
- * 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.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see .
- */
-
-/* \file ssi_config.h
- * Definitions for ARM CryptoCell Linux Crypto Driver
- */
-
-#ifndef __CC_CONFIG_H__
-#define __CC_CONFIG_H__
-
-#include 
-
-/* was 32 bit, but for juno's sake it was enlarged to 48 bit */
-#define DMA_BIT_MASK_LEN   48
-
-#endif /*__CC_CONFIG_H__*/
-
diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index 195fb27..df6d415 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -59,7 +59,6 @@
 #include 
 #include 
 
-#include "ssi_config.h"
 #include "ssi_driver.h"
 #include "ssi_request_mgr.h"
 #include "ssi_buffer_mgr.h"
diff --git a/drivers/staging/ccree/ssi_driver.h 
b/drivers/staging/ccree/ssi_driver.h
index 12e2a8b..5a56f7a 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -21,7 +21,6 @@
 #ifndef __CC_DRIVER_H__
 #define __CC_DRIVER_H__
 
-#include "ssi_config.h"
 #ifdef COMP_IN_WQ
 #include 
 #else
@@ -56,6 +55,9 @@ extern bool cc_dump_bytes;
 #define CC_DEV_NAME_STR "cc715ree"
 #define CC_COHERENT_CACHE_PARAMS 0xEEE
 
+/* Maximum DMA mask supported by IP */
+#define DMA_BIT_MASK_LEN 48
+
 #define CC_AXI_IRQ_MASK ((1 << CC_AXIM_CFG_BRESPMASK_BIT_SHIFT) | \
  (1 << CC_AXIM_CFG_RRESPMASK_BIT_SHIFT) | \
  (1 << CC_AXIM_CFG_INFLTMASK_BIT_SHIFT) | \
diff --git a/drivers/staging/ccree/ssi_fips.c b/drivers/staging/ccree/ssi_fips.c
index a1d7782..9ca6857 100644
--- a/drivers/staging/ccree/ssi_fips.c
+++ b/drivers/staging/ccree/ssi_fips.c
@@ -17,7 +17,6 @@
 #include 
 #include 
 
-#include "ssi_config.h"
 #include "ssi_driver.h"
 #include "ssi_fips.h"
 
diff --git a/drivers/staging/ccree/ssi_hash.c b/drivers/staging/ccree/ssi_hash.c
index 

[PATCH 10/10] staging: ccree: update TODO

2017-12-14 Thread Gilad Ben-Yossef
Update TODO with handling on sysfs -> debugfs transition

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/TODO | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/ccree/TODO b/drivers/staging/ccree/TODO
index f44edcd..6d8702b 100644
--- a/drivers/staging/ccree/TODO
+++ b/drivers/staging/ccree/TODO
@@ -6,6 +6,5 @@
 *  *
 *
 
-1. Migrate sysfs to debugfs.
-2. Handle HW FIFO fullness more cleanly.
+1. Handle HW FIFO fullness more cleanly.
 
-- 
2.7.4



[PATCH 01/10] staging: ccree: drop ifdef CONFIG_OF in code

2017-12-14 Thread Gilad Ben-Yossef
As we already depend on CONFIG_OF via Kconfig no need to
support conditional build without it.

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/ssi_driver.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index 28cfbb4..fbf0338 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -512,20 +512,16 @@ static const struct dev_pm_ops arm_cc7x_driver_pm = {
 #defineCC_DRIVER_RUNTIME_PMNULL
 #endif
 
-#ifdef CONFIG_OF
 static const struct of_device_id arm_cc7x_dev_of_match[] = {
{.compatible = "arm,cryptocell-712-ree"},
{}
 };
 MODULE_DEVICE_TABLE(of, arm_cc7x_dev_of_match);
-#endif
 
 static struct platform_driver cc7x_driver = {
.driver = {
   .name = "cc7xree",
-#ifdef CONFIG_OF
   .of_match_table = arm_cc7x_dev_of_match,
-#endif
   .pm = CC_DRIVER_RUNTIME_PM,
},
.probe = cc7x_probe,
-- 
2.7.4



[PATCH 05/10] staging: ccree: remove CC_IRQ_DELAY dead code

2017-12-14 Thread Gilad Ben-Yossef
Remove dead code from older version which is not supported in current
hardware.

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/ssi_config.h |  1 -
 drivers/staging/ccree/ssi_driver.c | 11 ---
 2 files changed, 12 deletions(-)

diff --git a/drivers/staging/ccree/ssi_config.h 
b/drivers/staging/ccree/ssi_config.h
index 15725cc..e809103 100644
--- a/drivers/staging/ccree/ssi_config.h
+++ b/drivers/staging/ccree/ssi_config.h
@@ -28,7 +28,6 @@
 //#define CC_DUMP_DESCS
 // #define CC_DUMP_BYTES
 // #define CC_DEBUG
-//#define CC_IRQ_DELAY 10
 /* was 32 bit, but for juno's sake it was enlarged to 48 bit */
 #define DMA_BIT_MASK_LEN   48
 
diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index 14705dc..6158794 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -171,17 +171,6 @@ int init_cc_regs(struct cc_drvdata *drvdata, bool is_probe)
   CC_GPR0_IRQ_MASK));
cc_iowrite(drvdata, CC_REG(HOST_IMR), val);
 
-#ifdef CC_HOST_IRQ_TIMER_INIT_VAL_REG_OFFSET
-#ifdef CC_IRQ_DELAY
-   /* Set CC IRQ delay */
-   cc_iowrite(drvdata, CC_REG(HOST_IRQ_TIMER_INIT_VAL), CC_IRQ_DELAY);
-#endif
-   if (cc_ioread(drvdata, CC_REG(HOST_IRQ_TIMER_INIT_VAL)) > 0) {
-   dev_dbg(dev, "irq_delay=%d CC cycles\n",
-   cc_ioread(drvdata, CC_REG(HOST_IRQ_TIMER_INIT_VAL)));
-   }
-#endif
-
cache_params = (drvdata->coherent ? CC_COHERENT_CACHE_PARAMS : 0x0);
 
val = cc_ioread(drvdata, CC_REG(AXIM_CACHE_PARAMS));
-- 
2.7.4



[PATCH 04/10] staging: ccree: staging: ccree: replace sysfs by debugfs interface

2017-12-14 Thread Gilad Ben-Yossef
The ccree driver has had a none standard sysfs interface for debugging.
Replace it with a proper debugfs interface.

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/Makefile  |   3 +-
 drivers/staging/ccree/cc_debugfs.c  | 114 +++
 drivers/staging/ccree/cc_debugfs.h  |  45 
 drivers/staging/ccree/ssi_aead.c|   1 -
 drivers/staging/ccree/ssi_cipher.c  |   1 -
 drivers/staging/ccree/ssi_config.h  |   2 -
 drivers/staging/ccree/ssi_driver.c  |  27 ++---
 drivers/staging/ccree/ssi_driver.h  |   2 +-
 drivers/staging/ccree/ssi_hash.c|   1 -
 drivers/staging/ccree/ssi_pm.c  |   1 -
 drivers/staging/ccree/ssi_request_mgr.c |   1 -
 drivers/staging/ccree/ssi_sysfs.c   | 192 
 drivers/staging/ccree/ssi_sysfs.h   |  32 --
 13 files changed, 176 insertions(+), 246 deletions(-)
 create mode 100644 drivers/staging/ccree/cc_debugfs.c
 create mode 100644 drivers/staging/ccree/cc_debugfs.h
 delete mode 100644 drivers/staging/ccree/ssi_sysfs.c
 delete mode 100644 drivers/staging/ccree/ssi_sysfs.h

diff --git a/drivers/staging/ccree/Makefile b/drivers/staging/ccree/Makefile
index ae702f3..ab9f073 100644
--- a/drivers/staging/ccree/Makefile
+++ b/drivers/staging/ccree/Makefile
@@ -1,3 +1,4 @@
 obj-$(CONFIG_CRYPTO_DEV_CCREE) := ccree.o
-ccree-y := ssi_driver.o ssi_sysfs.o ssi_buffer_mgr.o ssi_request_mgr.o 
ssi_cipher.o ssi_hash.o ssi_aead.o ssi_ivgen.o ssi_sram_mgr.o ssi_pm.o
+ccree-y := ssi_driver.o ssi_buffer_mgr.o ssi_request_mgr.o ssi_cipher.o 
ssi_hash.o ssi_aead.o ssi_ivgen.o ssi_sram_mgr.o ssi_pm.o
 ccree-$(CONFIG_CRYPTO_FIPS) += ssi_fips.o
+ccree-$(CONFIG_DEBUG_FS) += cc_debugfs.o
diff --git a/drivers/staging/ccree/cc_debugfs.c 
b/drivers/staging/ccree/cc_debugfs.c
new file mode 100644
index 000..7768a7b
--- /dev/null
+++ b/drivers/staging/ccree/cc_debugfs.c
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2012-2017 ARM Limited or its affiliates.
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see .
+ */
+
+#include 
+#include 
+#include 
+#include "ssi_config.h"
+#include "ssi_driver.h"
+#include "cc_crypto_ctx.h"
+
+struct cc_debugfs_ctx {
+   struct dentry *dir;
+};
+
+#define CC_DEBUG_REG(_X) { \
+   .name = __stringify(_X),\
+   .offset = CC_REG(_X)\
+   }
+
+/*
+ * This is a global var for the dentry of the
+ * debugfs ccree/ dir. It is not tied down to
+ * a specific instance of ccree, hence it is
+ * global.
+ */
+static struct dentry *cc_debugfs_dir;
+
+struct debugfs_reg32 debug_regs[] = {
+   CC_DEBUG_REG(HOST_SIGNATURE),
+   CC_DEBUG_REG(HOST_IRR),
+   CC_DEBUG_REG(HOST_POWER_DOWN_EN),
+   CC_DEBUG_REG(AXIM_MON_ERR),
+   CC_DEBUG_REG(DSCRPTR_QUEUE_CONTENT),
+   CC_DEBUG_REG(HOST_IMR),
+   CC_DEBUG_REG(AXIM_CFG),
+   CC_DEBUG_REG(AXIM_CACHE_PARAMS),
+   CC_DEBUG_REG(HOST_VERSION),
+   CC_DEBUG_REG(GPR_HOST),
+   CC_DEBUG_REG(AXIM_MON_COMP),
+};
+
+int cc_debugfs_global_init(void)
+{
+   cc_debugfs_dir = debugfs_create_dir("ccree", NULL);
+
+   return !cc_debugfs_dir;
+}
+
+void cc_debugfs_global_fini(void)
+{
+   debugfs_remove(cc_debugfs_dir);
+}
+
+int cc_debugfs_init(struct cc_drvdata *drvdata)
+{
+   struct device *dev = drvdata_to_dev(drvdata);
+   struct cc_debugfs_ctx *ctx;
+   struct debugfs_regset32 *regset;
+   struct dentry *file;
+
+   ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
+   if (!ctx)
+   return -ENOMEM;
+
+   regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL);
+   if (!regset)
+   return -ENOMEM;
+
+   regset->regs = debug_regs;
+   regset->nregs = ARRAY_SIZE(debug_regs);
+   regset->base = drvdata->cc_base;
+
+   ctx->dir = debugfs_create_dir(drvdata->plat_dev->name, cc_debugfs_dir);
+   if (!ctx->dir)
+   return -ENFILE;
+
+   file = debugfs_create_regset32("regs", 0400, ctx->dir, regset);
+   if (!file) {
+   debugfs_remove(ctx->dir);
+   return -ENFILE;
+   }
+
+   file = debugfs_create_bool("coherent", 0400, ctx->dir,
+  >coherent);
+
+   if (!file) {
+   debugfs_remove_recursive(ctx->dir);
+   return -ENFILE;
+   }
+
+   drvdata->debugfs = ctx;
+
+   return 0;
+}
+
+void cc_debugfs_fini(struct cc_drvdata 

[PATCH 03/10] staging: ccree: add explicit module init/exit func

2017-12-14 Thread Gilad Ben-Yossef
We need to do a module global scope init/exit operation to support
the debugfs interface we are about to introduce in the next patch,
so wean the module of the boiler plate saving macro as it will no
longer be sufficient.

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/ssi_driver.c | 13 -
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index ad02d92..5427c7f 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -517,7 +517,18 @@ static struct platform_driver cc7x_driver = {
.probe = cc7x_probe,
.remove = cc7x_remove,
 };
-module_platform_driver(cc7x_driver);
+
+static int __init ccree_init(void)
+{
+   return platform_driver_register(_driver);
+}
+module_init(ccree_init);
+
+static void __exit ccree_exit(void)
+{
+   platform_driver_unregister(_driver);
+}
+module_exit(ccree_exit);
 
 /* Module description */
 MODULE_DESCRIPTION("ARM TrustZone CryptoCell REE Driver");
-- 
2.7.4



[PATCH 02/10] staging: ccree: clean up PM registration

2017-12-14 Thread Gilad Ben-Yossef
Clean up power management registration.

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/ssi_driver.c | 16 +++-
 drivers/staging/ccree/ssi_pm.c |  4 
 drivers/staging/ccree/ssi_pm.h |  3 +++
 3 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index fbf0338..ad02d92 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -500,18 +500,6 @@ static int cc7x_remove(struct platform_device *plat_dev)
return 0;
 }
 
-#if defined(CONFIG_PM)
-static const struct dev_pm_ops arm_cc7x_driver_pm = {
-   SET_RUNTIME_PM_OPS(cc_pm_suspend, cc_pm_resume, NULL)
-};
-#endif
-
-#if defined(CONFIG_PM)
-#defineCC_DRIVER_RUNTIME_PM(_cc7x_driver_pm)
-#else
-#defineCC_DRIVER_RUNTIME_PMNULL
-#endif
-
 static const struct of_device_id arm_cc7x_dev_of_match[] = {
{.compatible = "arm,cryptocell-712-ree"},
{}
@@ -522,7 +510,9 @@ static struct platform_driver cc7x_driver = {
.driver = {
   .name = "cc7xree",
   .of_match_table = arm_cc7x_dev_of_match,
-  .pm = CC_DRIVER_RUNTIME_PM,
+#ifdef CONFIG_PM
+  .pm = _pm,
+#endif
},
.probe = cc7x_probe,
.remove = cc7x_remove,
diff --git a/drivers/staging/ccree/ssi_pm.c b/drivers/staging/ccree/ssi_pm.c
index 3c4892b..0db935d 100644
--- a/drivers/staging/ccree/ssi_pm.c
+++ b/drivers/staging/ccree/ssi_pm.c
@@ -34,6 +34,10 @@
 #define POWER_DOWN_ENABLE 0x01
 #define POWER_DOWN_DISABLE 0x00
 
+const struct dev_pm_ops ccree_pm = {
+   SET_RUNTIME_PM_OPS(cc_pm_suspend, cc_pm_resume, NULL)
+};
+
 int cc_pm_suspend(struct device *dev)
 {
struct cc_drvdata *drvdata = dev_get_drvdata(dev);
diff --git a/drivers/staging/ccree/ssi_pm.h b/drivers/staging/ccree/ssi_pm.h
index a5f2b1b..91140a3 100644
--- a/drivers/staging/ccree/ssi_pm.h
+++ b/drivers/staging/ccree/ssi_pm.h
@@ -30,6 +30,9 @@ int cc_pm_init(struct cc_drvdata *drvdata);
 void cc_pm_fini(struct cc_drvdata *drvdata);
 
 #if defined(CONFIG_PM)
+
+extern const struct dev_pm_ops ccree_pm;
+
 int cc_pm_suspend(struct device *dev);
 
 int cc_pm_resume(struct device *dev);
-- 
2.7.4



[PATCH 07/10] staging: ccree: turn compile time debug log to params

2017-12-14 Thread Gilad Ben-Yossef
The ccree driver has some support to dump runtime data
to kernel log to assist in debugging. The code used to be
enabled by a build time flag. Refactor to enable it via
module/kernel parameters.

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/ssi_config.h  |  2 --
 drivers/staging/ccree/ssi_driver.c  | 18 --
 drivers/staging/ccree/ssi_driver.h  | 16 ++--
 drivers/staging/ccree/ssi_request_mgr.c | 26 +-
 4 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/drivers/staging/ccree/ssi_config.h 
b/drivers/staging/ccree/ssi_config.h
index b530974..bc90ad0 100644
--- a/drivers/staging/ccree/ssi_config.h
+++ b/drivers/staging/ccree/ssi_config.h
@@ -23,8 +23,6 @@
 
 #include 
 
-//#define CC_DUMP_DESCS
-// #define CC_DUMP_BYTES
 /* was 32 bit, but for juno's sake it was enlarged to 48 bit */
 #define DMA_BIT_MASK_LEN   48
 
diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index dbca241..195fb27 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -72,20 +72,26 @@
 #include "ssi_pm.h"
 #include "ssi_fips.h"
 
-#ifdef CC_DUMP_BYTES
-void dump_byte_array(const char *name, const u8 *buf, size_t len)
+bool cc_dump_desc;
+module_param_named(dump_desc, cc_dump_desc, bool, 0600);
+MODULE_PARM_DESC(cc_dump_desc, "Dump descriptors to kernel log as debugging 
aid");
+
+bool cc_dump_bytes;
+module_param_named(dump_bytes, cc_dump_bytes, bool, 0600);
+MODULE_PARM_DESC(cc_dump_bytes, "Dump buffers to kernel log as debugging aid");
+
+void __dump_byte_array(const char *name, const u8 *buf, size_t len)
 {
-   char prefix[NAME_LEN];
+   char prefix[64];
 
if (!buf)
return;
 
snprintf(prefix, sizeof(prefix), "%s[%lu]: ", name, len);
 
-   print_hex_dump(KERN_DEBUG, prefix, DUMP_PREFIX_ADDRESS, 16, 1, len,
-  false);
+   print_hex_dump(KERN_DEBUG, prefix, DUMP_PREFIX_ADDRESS, 16, 1, buf,
+  len, false);
 }
-#endif
 
 static irqreturn_t cc_isr(int irq, void *dev_id)
 {
diff --git a/drivers/staging/ccree/ssi_driver.h 
b/drivers/staging/ccree/ssi_driver.h
index 4e05386..12e2a8b 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -48,6 +48,9 @@
 #include "cc_hw_queue_defs.h"
 #include "ssi_sram_mgr.h"
 
+extern bool cc_dump_desc;
+extern bool cc_dump_bytes;
+
 #define DRV_MODULE_VERSION "3.0"
 
 #define CC_DEV_NAME_STR "cc715ree"
@@ -169,13 +172,14 @@ static inline struct device *drvdata_to_dev(struct 
cc_drvdata *drvdata)
return >plat_dev->dev;
 }
 
-#ifdef CC_DUMP_BYTES
-void dump_byte_array(const char *name, const u8 *the_array,
-unsigned long size);
-#else
+void __dump_byte_array(const char *name, const u8 *the_array,
+  unsigned long size);
 static inline void dump_byte_array(const char *name, const u8 *the_array,
-  unsigned long size) {};
-#endif
+  unsigned long size)
+{
+   if (cc_dump_bytes)
+   __dump_byte_array(name, the_array, size);
+}
 
 int init_cc_regs(struct cc_drvdata *drvdata, bool is_probe);
 void fini_cc_regs(struct cc_drvdata *drvdata);
diff --git a/drivers/staging/ccree/ssi_request_mgr.c 
b/drivers/staging/ccree/ssi_request_mgr.c
index f6374b0..2aa21f8 100644
--- a/drivers/staging/ccree/ssi_request_mgr.c
+++ b/drivers/staging/ccree/ssi_request_mgr.c
@@ -161,11 +161,12 @@ int cc_req_mgr_init(struct cc_drvdata *drvdata)
return rc;
 }
 
-static void enqueue_seq(void __iomem *cc_base, struct cc_hw_desc seq[],
+static void enqueue_seq(struct cc_drvdata *drvdata, struct cc_hw_desc seq[],
unsigned int seq_len)
 {
int i, w;
-   void * __iomem reg = cc_base + CC_REG(DSCRPTR_QUEUE_WORD0);
+   void * __iomem reg = drvdata->cc_base + CC_REG(DSCRPTR_QUEUE_WORD0);
+   struct device *dev = drvdata_to_dev(drvdata);
 
/*
 * We do indeed write all 6 command words to the same
@@ -175,11 +176,12 @@ static void enqueue_seq(void __iomem *cc_base, struct 
cc_hw_desc seq[],
for (i = 0; i < seq_len; i++) {
for (w = 0; w <= 5; w++)
writel_relaxed(seq[i].word[w], reg);
-#ifdef CC_DUMP_DESCS
-   dev_dbg(dev, "desc[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 
0x%08X\n",
-   i, seq[i].word[0], seq[i].word[1], seq[i].word[2],
-   seq[i].word[3], seq[i].word[4], seq[i].word[5]);
-#endif
+
+   if (cc_dump_desc)
+   dev_dbg(dev, "desc[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X 
0x%08X 0x%08X\n",
+   i, seq[i].word[0], seq[i].word[1],
+   seq[i].word[2], seq[i].word[3],
+   seq[i].word[4], seq[i].word[5]);
}
 }
 
@@ -256,7 +258,6 @@ 

[PATCH 00/10] staging: ccree: cleanups & fixes

2017-12-14 Thread Gilad Ben-Yossef
- More cleanups and dead code removal.
- Handle TODO item of moving none standard sysfs interface
  to debugfs
- One fix to FIPS event irq handling code

Gilad Ben-Yossef (10):
  staging: ccree: drop ifdef CONFIG_OF in code
  staging: ccree: clean up PM registration
  staging: ccree: add explicit module init/exit func
  staging: ccree: staging: ccree: replace sysfs by debugfs interface
  staging: ccree: remove CC_IRQ_DELAY dead code
  staging: ccree: remove useless debug code
  staging: ccree: turn compile time debug log to params
  staging: ccree: remove ssi_config.h
  staging: ccree: fix fips event irq handling build
  staging: ccree: update TODO

 drivers/staging/ccree/Makefile  |   3 +-
 drivers/staging/ccree/TODO  |   3 +-
 drivers/staging/ccree/cc_debugfs.c  | 113 +++
 drivers/staging/ccree/cc_debugfs.h  |  45 
 drivers/staging/ccree/ssi_aead.c|  47 
 drivers/staging/ccree/ssi_buffer_mgr.h  |   1 -
 drivers/staging/ccree/ssi_cipher.c  |   2 -
 drivers/staging/ccree/ssi_config.h  |  38 ---
 drivers/staging/ccree/ssi_driver.c  | 103 +++--
 drivers/staging/ccree/ssi_driver.h  |  22 ++--
 drivers/staging/ccree/ssi_fips.c|   1 -
 drivers/staging/ccree/ssi_hash.c|   2 -
 drivers/staging/ccree/ssi_ivgen.c   |   1 -
 drivers/staging/ccree/ssi_pm.c  |   6 +-
 drivers/staging/ccree/ssi_pm.h  |   4 +-
 drivers/staging/ccree/ssi_request_mgr.c |  51 +++--
 drivers/staging/ccree/ssi_sysfs.c   | 192 
 drivers/staging/ccree/ssi_sysfs.h   |  32 --
 18 files changed, 236 insertions(+), 430 deletions(-)
 create mode 100644 drivers/staging/ccree/cc_debugfs.c
 create mode 100644 drivers/staging/ccree/cc_debugfs.h
 delete mode 100644 drivers/staging/ccree/ssi_config.h
 delete mode 100644 drivers/staging/ccree/ssi_sysfs.c
 delete mode 100644 drivers/staging/ccree/ssi_sysfs.h

-- 
2.7.4



[PATCH 06/10] staging: ccree: remove useless debug code

2017-12-14 Thread Gilad Ben-Yossef
Remove a bunch of useless debug code ifdef'ed out

Signed-off-by: Gilad Ben-Yossef 
---
 drivers/staging/ccree/ssi_aead.c| 45 -
 drivers/staging/ccree/ssi_config.h  |  3 ---
 drivers/staging/ccree/ssi_driver.c  | 13 --
 drivers/staging/ccree/ssi_request_mgr.c | 23 -
 4 files changed, 84 deletions(-)

diff --git a/drivers/staging/ccree/ssi_aead.c b/drivers/staging/ccree/ssi_aead.c
index dde6797..ce26e68 100644
--- a/drivers/staging/ccree/ssi_aead.c
+++ b/drivers/staging/ccree/ssi_aead.c
@@ -1834,51 +1834,6 @@ static int cc_gcm(struct aead_request *req, struct 
cc_hw_desc desc[],
return 0;
 }
 
-#ifdef CC_DEBUG
-static void cc_dump_gcm(const char *title, struct aead_request *req)
-{
-   struct crypto_aead *tfm = crypto_aead_reqtfm(req);
-   struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
-   struct aead_req_ctx *req_ctx = aead_request_ctx(req);
-
-   if (ctx->cipher_mode != DRV_CIPHER_GCTR)
-   return;
-
-   if (title) {
-   dev_dbg(dev, 
"--");
-   dev_dbg(dev, "%s\n", title);
-   }
-
-   dev_dbg(dev, "cipher_mode %d, authsize %d, enc_keylen %d, assoclen %d, 
cryptlen %d\n",
-   ctx->cipher_mode, ctx->authsize, ctx->enc_keylen,
-   req->assoclen, req_ctx->cryptlen);
-
-   if (ctx->enckey)
-   dump_byte_array("mac key", ctx->enckey, 16);
-
-   dump_byte_array("req->iv", req->iv, AES_BLOCK_SIZE);
-
-   dump_byte_array("gcm_iv_inc1", req_ctx->gcm_iv_inc1, AES_BLOCK_SIZE);
-
-   dump_byte_array("gcm_iv_inc2", req_ctx->gcm_iv_inc2, AES_BLOCK_SIZE);
-
-   dump_byte_array("hkey", req_ctx->hkey, AES_BLOCK_SIZE);
-
-   dump_byte_array("mac_buf", req_ctx->mac_buf, AES_BLOCK_SIZE);
-
-   dump_byte_array("gcm_len_block", req_ctx->gcm_len_block.len_a,
-   AES_BLOCK_SIZE);
-
-   if (req->src && req->cryptlen)
-   dump_byte_array("req->src", sg_virt(req->src),
-   req->cryptlen + req->assoclen);
-
-   if (req->dst)
-   dump_byte_array("req->dst", sg_virt(req->dst),
-   req->cryptlen + ctx->authsize + req->assoclen);
-}
-#endif
-
 static int config_gcm_context(struct aead_request *req)
 {
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
diff --git a/drivers/staging/ccree/ssi_config.h 
b/drivers/staging/ccree/ssi_config.h
index e809103..b530974 100644
--- a/drivers/staging/ccree/ssi_config.h
+++ b/drivers/staging/ccree/ssi_config.h
@@ -23,11 +23,8 @@
 
 #include 
 
-//#define FLUSH_CACHE_ALL
-//#define COMPLETION_DELAY
 //#define CC_DUMP_DESCS
 // #define CC_DUMP_BYTES
-// #define CC_DEBUG
 /* was 32 bit, but for juno's sake it was enlarged to 48 bit */
 #define DMA_BIT_MASK_LEN   48
 
diff --git a/drivers/staging/ccree/ssi_driver.c 
b/drivers/staging/ccree/ssi_driver.c
index 6158794..dbca241 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -446,19 +446,6 @@ static int cc7x_probe(struct platform_device *plat_dev)
 {
int rc;
struct device *dev = _dev->dev;
-#if defined(CONFIG_ARM) && defined(CC_DEBUG)
-   u32 ctr, cacheline_size;
-
-   asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
-   cacheline_size =  4 << ((ctr >> 16) & 0xf);
-   dev_dbg(dev, "CP15(L1_CACHE_BYTES) = %u , Kconfig(L1_CACHE_BYTES) = 
%u\n",
-   cacheline_size, L1_CACHE_BYTES);
-
-   asm volatile("mrc p15, 0, %0, c0, c0, 0" : "=r" (ctr));
-   dev_dbg(dev, "Main ID register (MIDR): Implementer 0x%02X, Arch 0x%01X, 
Part 0x%03X, Rev r%dp%d\n",
-   (ctr >> 24), (ctr >> 16) & 0xF, (ctr >> 4) & 0xFFF,
-   (ctr >> 20) & 0xF, ctr & 0xF);
-#endif
 
/* Map registers space */
rc = init_cc_resources(plat_dev);
diff --git a/drivers/staging/ccree/ssi_request_mgr.c 
b/drivers/staging/ccree/ssi_request_mgr.c
index 07b9404..f6374b0 100644
--- a/drivers/staging/ccree/ssi_request_mgr.c
+++ b/drivers/staging/ccree/ssi_request_mgr.c
@@ -20,9 +20,6 @@
 #include 
 #include 
 #include 
-#ifdef FLUSH_CACHE_ALL
-#include 
-#endif
 #include 
 #include "ssi_driver.h"
 #include "ssi_buffer_mgr.h"
@@ -359,9 +356,6 @@ int send_request(struct cc_drvdata *drvdata, struct 
cc_crypto_req *cc_req,
 
dev_dbg(dev, "Enqueue request head=%u\n", req_mgr_h->req_queue_head);
 
-#ifdef FLUSH_CACHE_ALL
-   flush_cache_all();
-#endif
/*
 * We are about to push command to the HW via the command registers
 * that may refernece hsot memory. We need to issue a memory barrier
@@ -493,23 +487,6 @@ static void proc_completions(struct cc_drvdata *drvdata)
 
cc_req = _mgr_handle->req_queue[*tail];
 
-#ifdef FLUSH_CACHE_ALL
-   flush_cache_all();
-#endif
-
-#ifdef COMPLETION_DELAY
-

Re: [PATCH 04/10] staging: ccree: staging: ccree: replace sysfs by debugfs interface

2017-12-14 Thread Philippe Ombredanne
Gilad,

On Thu, Dec 14, 2017 at 3:02 PM, Gilad Ben-Yossef  wrote:
> The ccree driver has had a none standard sysfs interface for debugging.
> Replace it with a proper debugfs interface.
>
> Signed-off-by: Gilad Ben-Yossef 



> --- /dev/null
> +++ b/drivers/staging/ccree/cc_debugfs.c
> @@ -0,0 +1,114 @@
> +/*
> + * Copyright (C) 2012-2017 ARM Limited or its affiliates.
> + *
> + * 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.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, see .
> + */

Could you use the new SPDX tags instead of this fine and long
boilerplate? See Thomas doc for details [1]

You could spare the world from ~14 lines of this fine legalese with this:

// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2012-2017 ARM Limited or its affiliates.

If you wonder about the C++ style comment, read Thomas doc patches [1]
and check the comments made on the list by Linus.

If you could spread the word out in your team too, this would be much welcomed!
Thanks!

[1] https://lkml.org/lkml/2017/12/4/934

--
Cordially
Philippe Ombredanne