enable CAAM's HWRNG as default
v8 cover enable CAAM's HWRNG as default
Everyone:
This series is a continuation of original [discussion]. I don't know if what's in the series is enough to use CAAMs HWRNG system wide, but I am hoping that with enough iterations and feedback it will be.
Changes since [v1]:
- Original hw_random replaced with the one using output of TRNG directly
- SEC4 DRNG IP block exposed via crypto API
- Small fix regarding use of GFP_DMA added to the series
Chagnes since [v2]:
- msleep in polling loop to avoid wasting CPU cycles
- caam_trng_read() bails out early if 'wait' is set to 'false'
- fixed typo in ZII's name
Changes since [v3]:
- DRNG's .cra_name is now "stdrng"
- collected Reviewd-by tag from Lucas
- typo fixes in commit messages of the series
Changes since [v4]:
- Dropped "crypto: caam - RNG4 TRNG errata" and "crypto: caam - enable prediction resistance in HRWNG" to limit the scope of the series. Those two patches are not yet ready and can be submitted separately later.
- Collected Tested-by from Chris
Changes since [v5]:
- Series is converted back to implementing HWRNG using a job ring as per feedback from Horia.
Changes since [v6]:
- "crypto: caam - drop global context pointer and init_done" changed to use devres group to allow freeing HWRNG resource independently of the parent device lifecycle. Code to deal with circular deallocation dependency is added as well
- Removed worker self-scheduling in "crypto: caam - simplify RNG implementation". It didn't bring much value, but meant that simple cleanup with just a call to flush_work() wasn't good enough.
- Added a simple function with a FIXME item for MC firmware check in "crypto: caam - enable prediction resistance in HRWNG"
- "crypto: caam - limit single JD RNG output to maximum of 16 bytes" now shrinks async FIFO size from 32K to 64 bytes, since having a buffer that big doesn't seem to do any good given that througput of TRNG
Changes since [v7]:
- Collected Reviewd-bys from Horia
- updated "crypto: caam - simplify RNG implementation" to drop custom type and fix comments
- updated "crypto: caam - enable prediction resistance in HRWNG" to integrate code from Andrei Botila
- updated "crypto: caam - drop global context pointer and init_done" to use .priv instead of container_of for private data pointer
Feedback is welcome!
Thanks,
Andrey Smirnov
[discussion] https://patchwork.kernel.org/patch/9850669/
[v1] https://lore.kernel.org/lkml/[email protected]
[v2] https://lore.kernel.org/lkml/[email protected]
[v3] https://lore.kernel.org/lkml/[email protected]
[v4] https://lore.kernel.org/lkml/[email protected]
[v5] https://lore.kernel.org/lkml/[email protected]
[v6] https://lore.kernel.org/lkml/[email protected]
[v7] https://lore.kernel.org/lkml/[email protected]
Andrey Smirnov (8):
crypto: caam - allocate RNG instantiation descriptor with GFP_DMA
crypto: caam - use struct hwrng's .init for initialization
crypto: caam - drop global context pointer and init_done
crypto: caam - simplify RNG implementation
crypto: caam - check if RNG job failed
crypto: caam - invalidate entropy register during RNG initialization
crypto: caam - enable prediction resistance in HRWNG
crypto: caam - limit single JD RNG output to maximum of 16 bytes
drivers/crypto/caam/Kconfig | 1 + drivers/crypto/caam/caamrng.c | 405 +++++++++++++--------------------- drivers/crypto/caam/ctrl.c | 88 ++++++-- drivers/crypto/caam/desc.h | 2 + drivers/crypto/caam/intern.h | 7 +- drivers/crypto/caam/jr.c | 13 +- drivers/crypto/caam/regs.h | 7 +- 7 files changed, 243 insertions(+), 280 deletions(-)
--
2.21.0
PATCH v8 1/8 crypto: caam - allocate RNG instantiation descriptor with GFP_DMA
Be consistent with the rest of the codebase and use GFP_DMA when allocating memory for a CAAM JR descriptor.
--- drivers/crypto/caam/ctrl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/caam/ctrl.c +2 -2
@@ -196,7 +196,7 @@ static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)u32 *desc, status;int sh_idx, ret =0 ; - desc =kmalloc (CAAM_CMD_SZ *3 ,GFP_KERNEL ); + desc =kmalloc (CAAM_CMD_SZ *3 ,GFP_KERNEL |GFP_DMA );if (!desc)return -ENOMEM ;
@@ -273,7 +273,7 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask,int ret =0 , sh_idx; ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; - desc =kmalloc (CAAM_CMD_SZ *7 ,GFP_KERNEL ); + desc =kmalloc (CAAM_CMD_SZ *7 ,GFP_KERNEL |GFP_DMA );if (!desc)return -ENOMEM ;
Signature · 1 line
2.21.0
PATCH v8 2/8 crypto: caam - use struct hwrng's .init for initialization
In reply to Andrey SmirnovMake caamrng code a bit more symmetric by moving initialization code to .init hook of struct hwrng.
--- drivers/crypto/caam/caamrng.c | 47 ++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 23 deletions(-)
drivers/crypto/caam/caamrng.c +24 -23
@@ -256,6 +256,7 @@ static void caam_cleanup(struct hwrng *rng) }rng_unmap_ctx (rng_ctx); +caam_jr_free (rng_ctx->jrdev); }static int caam_init_buf (struct caam_rng_ctx *ctx,int buf_id)
@@ -274,28 +275,43 @@ static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)return 0 ; } -static int caam_init_rng (struct caam_rng_ctx *ctx,struct device *jrdev) +static int caam_init (struct hwrng *rng) { +struct caam_rng_ctx *ctx = rng_ctx;int err; - ctx->jrdev = jrdev; + ctx->jrdev =caam_jr_alloc (); + err =PTR_ERR_OR_ZERO (ctx->jrdev); +if (err) { +pr_err ("Job Ring Device allocation for transform failed\n" ); +return err; + } err =rng_create_sh_desc (ctx);if (err) -return err; + goto free_jrdev; ctx->current_buf =0 ; ctx->cur_buf_idx =0 ; err =caam_init_buf (ctx,0 );if (err) -return err; + goto free_jrdev; + + err =caam_init_buf (ctx,1 ); +if (err) + goto free_jrdev; -return caam_init_buf (ctx,1 ); +return 0 ; + +free_jrdev: +caam_jr_free (ctx->jrdev); +return err; }static struct hwrng caam_rng = { .name ="rng-caam" , + .init = caam_init, .cleanup = caam_cleanup, .read = caam_read, };
@@ -305,14 +321,12 @@ void caam_rng_exit(void)if (!init_done)return ; -caam_jr_free (rng_ctx->jrdev);hwrng_unregister (&caam_rng);kfree (rng_ctx); }int caam_rng_init (struct device *ctrldev) { -struct device *dev;u32 rng_inst;struct caam_drv_private *priv =dev_get_drvdata (ctrldev);int err;
@@ -328,21 +342,11 @@ int caam_rng_init(struct device *ctrldev)if (!rng_inst)return 0 ; - dev =caam_jr_alloc (); -if (IS_ERR (dev)) { -pr_err ("Job Ring Device allocation for transform failed\n" ); -return PTR_ERR (dev); - } rng_ctx =kmalloc (sizeof (*rng_ctx),GFP_DMA |GFP_KERNEL ); -if (!rng_ctx) { - err = -ENOMEM ; - goto free_caam_alloc; - } - err =caam_init_rng (rng_ctx, dev); -if (err) - goto free_rng_ctx; +if (!rng_ctx) +return -ENOMEM ; -dev_info (dev,"registering rng-caam\n" ); +dev_info (ctrldev,"registering rng-caam\n" ); err =hwrng_register (&caam_rng);if (!err) {
@@ -350,9 +354,6 @@ int caam_rng_init(struct device *ctrldev)return err; } -free_rng_ctx:kfree (rng_ctx); -free_caam_alloc: -caam_jr_free (dev);return err; }
Signature · 1 line
2.21.0
PATCH v8 3/8 crypto: caam - drop global context pointer and init_done
In reply to Andrey SmirnovLeverage devres to get rid of code storing global context as well as init_done flag.
Original code also has a circular deallocation dependency where unregister_algs() -> caam_rng_exit() -> caam_jr_free() chain would only happen if all of JRs were freed. Fix this by moving caam_rng_exit() outside of unregister_algs() and doing it specifically for JR that instantiated HWRNG.
--- drivers/crypto/caam/caamrng.c | 67 +++++++++++++++++------------------ drivers/crypto/caam/intern.h | 7 ++-- drivers/crypto/caam/jr.c | 13 ++++--- 3 files changed, 44 insertions(+), 43 deletions(-)
drivers/crypto/caam/caamrng.c +33 -34
@@ -70,6 +70,7 @@ struct buf_data {/* rng per-device context */ struct caam_rng_ctx { +struct hwrng rng;struct device *jrdev;dma_addr_t sh_desc_dma;u32 sh_desc[DESC_RNG_LEN ];
@@ -78,13 +79,10 @@ struct caam_rng_ctx {struct buf_data bufs[2 ]; }; -static struct caam_rng_ctx *rng_ctx; - -/* -* Variable used to avoid double free of resources in case -* algorithm registration was unsuccessful -*/ -static bool init_done; +static struct caam_rng_ctx *to_caam_rng_ctx (struct hwrng *r) +{ +return (struct caam_rng_ctx *)r->priv; +}static inline void rng_unmap_buf (struct device *jrdev,struct buf_data *bd) {
@@ -143,7 +141,7 @@ static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)static int caam_read (struct hwrng *rng,void *data,size_t max,bool wait) { -struct caam_rng_ctx *ctx = rng_ctx; +struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng);struct buf_data *bd = &ctx->bufs[ctx->current_buf];int next_buf_idx, copied_idx;int err;
@@ -246,17 +244,18 @@ static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id)static void caam_cleanup (struct hwrng *rng) { +struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng);int i;struct buf_data *bd;for (i =0 ; i <2 ; i++) { - bd = &rng_ctx->bufs[i]; + bd = &ctx->bufs[i];if (atomic_read (&bd->empty) ==BUF_PENDING )wait_for_completion (&bd->filled); } -rng_unmap_ctx (rng_ctx); -caam_jr_free (rng_ctx->jrdev); +rng_unmap_ctx (ctx); +caam_jr_free (ctx->jrdev); }static int caam_init_buf (struct caam_rng_ctx *ctx,int buf_id)
@@ -277,7 +276,7 @@ static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)static int caam_init (struct hwrng *rng) { -struct caam_rng_ctx *ctx = rng_ctx; +struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng);int err; ctx->jrdev =caam_jr_alloc ();
@@ -309,28 +308,19 @@ static int caam_init(struct hwrng *rng)return err; } -static struct hwrng caam_rng = { - .name ="rng-caam" , - .init = caam_init, - .cleanup = caam_cleanup, - .read = caam_read, -}; +int caam_rng_init (struct device *ctrldev); -void caam_rng_exit (void ) +void caam_rng_exit (struct device *ctrldev) { -if (!init_done) -return ; - -hwrng_unregister (&caam_rng); -kfree (rng_ctx); +devres_release_group (ctrldev, caam_rng_init); }int caam_rng_init (struct device *ctrldev) { +struct caam_rng_ctx *ctx;u32 rng_inst;struct caam_drv_private *priv =dev_get_drvdata (ctrldev); -int err; - init_done = false; +int ret;/* Check for an instantiated RNG before registration */ if (priv->era <10 )
@@ -342,18 +332,27 @@ int caam_rng_init(struct device *ctrldev)if (!rng_inst)return 0 ; - rng_ctx =kmalloc (sizeof (*rng_ctx),GFP_DMA |GFP_KERNEL ); -if (!rng_ctx) +if (!devres_open_group (ctrldev, caam_rng_init,GFP_KERNEL )) +return -ENOMEM ; + + ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_DMA |GFP_KERNEL ); +if (!ctx)return -ENOMEM ; + ctx->rng.name ="rng-caam" ; + ctx->rng.init = caam_init; + ctx->rng.cleanup = caam_cleanup; + ctx->rng.read = caam_read; + ctx->rng.priv = (unsigned long )ctx; +dev_info (ctrldev,"registering rng-caam\n" ); - err =hwrng_register (&caam_rng); -if (!err) { - init_done = true; -return err; + ret =devm_hwrng_register (ctrldev, &ctx->rng); +if (ret) { +caam_rng_exit (ctrldev); +return ret; } -kfree (rng_ctx); -return err; +devres_close_group (ctrldev, caam_rng_init); +return 0 ; }
drivers/crypto/caam/intern.h +3 -4
@@ -47,6 +47,7 @@ struct caam_drv_private_jr {struct caam_job_ring __iomem *rregs;/* JobR's register space */ struct tasklet_struct irqtask;int irq;/* One per queue */ +bool hwrng;/* Number of scatterlist crypt transforms active on the JobR */ atomic_t tfm_count____cacheline_aligned ;
@@ -163,7 +164,7 @@ static inline void caam_pkc_exit(void)#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API int caam_rng_init (struct device *dev); -void caam_rng_exit (void ); +void caam_rng_exit (struct device *dev);#else
@@ -172,9 +173,7 @@ static inline int caam_rng_init(struct device *dev) return 0; } -static inline void caam_rng_exit (void ) -{ -} +static inline void caam_rng_exit (struct device *dev) {}#endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API */
drivers/crypto/caam/jr.c +8 -5
@@ -27,7 +27,8 @@ static struct jr_driver_data driver_data;static DEFINE_MUTEX (algs_lock);static unsigned int active_devs; -static void register_algs (struct device *dev) +static void register_algs (struct caam_drv_private_jr *jrpriv, +struct device *dev) {mutex_lock (&algs_lock);
@@ -37,7 +38,7 @@ static void register_algs(struct device *dev)caam_algapi_init (dev);caam_algapi_hash_init (dev);caam_pkc_init (dev); -caam_rng_init (dev); + jrpriv->hwrng = !caam_rng_init (dev);caam_qi_algapi_init (dev); algs_unlock:
@@ -53,7 +54,6 @@ static void unregister_algs(void)caam_qi_algapi_exit (); -caam_rng_exit ();caam_pkc_exit ();caam_algapi_hash_exit ();caam_algapi_exit ();
@@ -135,6 +135,9 @@ static int caam_jr_remove(struct platform_device *pdev) jrdev = &pdev->dev; jrpriv =dev_get_drvdata (jrdev); +if (jrpriv->hwrng) +caam_rng_exit (jrdev->parent); +/* * Return EBUSY if job ring already allocated. */
@@ -514,7 +517,7 @@ static int caam_jr_probe(struct platform_device *pdev)int error; jrdev = &pdev->dev; - jrpriv =devm_kmalloc (jrdev,sizeof (*jrpriv),GFP_KERNEL ); + jrpriv =devm_kzalloc (jrdev,sizeof (*jrpriv),GFP_KERNEL );if (!jrpriv)return -ENOMEM ;
@@ -590,7 +593,7 @@ static int caam_jr_probe(struct platform_device *pdev)atomic_set (&jrpriv->tfm_count,0 ); -register_algs (jrdev->parent); +register_algs (jrpriv, jrdev->parent);return 0 ; }
Signature · 1 line
2.21.0
PATCH v8 4/8 crypto: caam - simplify RNG implementation
In reply to Andrey SmirnovRework CAAM RNG implementation as follows:
- Make use of the fact that HWRNG supports partial reads and will handle such cases gracefully by removing recursion in caam_read()
- Convert blocking caam_read() codepath to do a single blocking job read directly into requested buffer, bypassing any intermediary buffers
- Convert async caam_read() codepath into a simple single reader/single writer FIFO use-case, thus simplifying concurrency handling and delegating buffer read/write position management to KFIFO subsystem.
- Leverage the same low level RNG data extraction code for both async and blocking caam_read() scenarios, get rid of the shared job descriptor and make non-shared one as a simple as possible (just HEADER + ALGORITHM OPERATION + FIFO STORE)
- Split private context from DMA related memory, so that the former could be allocated without GFP_DMA.
NOTE: On its face value this commit decreased throughput numbers reported by
dd if=/dev/hwrng of=/dev/null bs=1 count=100K [iflag=nonblock]
by about 15%, however commits that enable prediction resistance and limit JR total size impact the performance so much and move the bottleneck such as to make this regression irrelevant.
NOTE: On the bright side, this commit reduces RNG in kernel DMA buffer memory usage from 2 x RN_BUF_SIZE (~256K) to 32K.
--- drivers/crypto/caam/caamrng.c | 323 ++++++++++++---------------------- 1 file changed, 108 insertions(+), 215 deletions(-)
drivers/crypto/caam/caamrng.c +108 -215
@@ -7,35 +7,12 @@* * Based on caamalg.c crypto API driver. * -* relationship between job descriptors to shared descriptors: -* -* --------------- -------------- -* | JobDesc #0 |-------------------->| ShareDesc | -* | *(buffer 0) | |------------->| (generate) | -* --------------- | | (move) | -* | | (store) | -* --------------- | -------------- -* | JobDesc #1 |------| -* | *(buffer 1) | -* --------------- -* -* A job desc looks like this: -* -* --------------------- -* | Header | -* | ShareDesc Pointer | -* | SEQ_OUT_PTR | -* | (output buffer) | -* --------------------- -* -* The SharedDesc never changes, and each job descriptor points to one of two -* buffers for each device, from which the data will be copied into the -* requested destination */ #include <linux/hw_random.h> #include <linux/completion.h> #include <linux/atomic.h> +#include <linux/kfifo.h> #include "compat.h"
@@ -45,38 +22,26 @@#include "jr.h" #include "error.h" +#define CAAM_RNG_MAX_FIFO_STORE_SIZE U16_MAX + +#define CAAM_RNG_FIFO_LEN SZ_32K/* Must be a multiple of 2 */ +/* -* Maximum buffer size: maximum number of random, cache-aligned bytes that -* will be generated and moved to seq out ptr (extlen not allowed) +* Length of used descriptors, see caam_init_desc() */ -#define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \ - L1_CACHE_BYTES) - -/* length of descriptors */ -#define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ_MAX * 2) -#define DESC_RNG_LEN (3 * CAAM_CMD_SZ) - -/* Buffer, its dma address and lock */ -struct buf_data { -u8 buf[RN_BUF_SIZE ]____cacheline_aligned ; -dma_addr_t addr; -struct completion filled; -u32 hw_desc[DESC_JOB_O_LEN ]; -#define BUF_NOT_EMPTY 0 -#define BUF_EMPTY 1 -#define BUF_PENDING 2/* Empty, but with job pending --don't submit another */ -atomic_t empty; -}; +#define CAAM_RNG_DESC_LEN (CAAM_CMD_SZ + \ + CAAM_CMD_SZ + \ + CAAM_CMD_SZ + CAAM_PTR_SZ_MAX)/* rng per-device context */ struct caam_rng_ctx {struct hwrng rng;struct device *jrdev; -dma_addr_t sh_desc_dma; -u32 sh_desc[DESC_RNG_LEN ]; -unsigned int cur_buf_idx; -int current_buf; -struct buf_data bufs[2 ]; +struct device *ctrldev; +void *desc_async; +void *desc_sync; +struct work_struct worker; +struct kfifo fifo; };static struct caam_rng_ctx *to_caam_rng_ctx (struct hwrng *r)
@@ -84,228 +49,153 @@ static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r)return (struct caam_rng_ctx *)r->priv; } -static inline void rng_unmap_buf (struct device *jrdev,struct buf_data *bd) +static void caam_rng_done (struct device *jrdev,u32 *desc,u32 err, +void *context) { -if (bd->addr) -dma_unmap_single (jrdev, bd->addr,RN_BUF_SIZE , -DMA_FROM_DEVICE ); -} - -static inline void rng_unmap_ctx (struct caam_rng_ctx *ctx) -{ -struct device *jrdev = ctx->jrdev; - -if (ctx->sh_desc_dma) -dma_unmap_single (jrdev, ctx->sh_desc_dma, -desc_bytes (ctx->sh_desc),DMA_TO_DEVICE ); -rng_unmap_buf (jrdev, &ctx->bufs[0 ]); -rng_unmap_buf (jrdev, &ctx->bufs[1 ]); -} - -static void rng_done (struct device *jrdev,u32 *desc,u32 err,void *context) -{ -struct buf_data *bd; - - bd = container_of(desc, struct buf_data, hw_desc[0]); +struct completion *done = context;if (err)caam_jr_strstatus (jrdev, err); -atomic_set (&bd->empty,BUF_NOT_EMPTY ); -complete (&bd->filled); - -/* Buffer refilled, invalidate cache */ -dma_sync_single_for_cpu (jrdev, bd->addr,RN_BUF_SIZE ,DMA_FROM_DEVICE ); - -print_hex_dump_debug ("rng refreshed buf@: " ,DUMP_PREFIX_ADDRESS ,16 ,4 , - bd->buf,RN_BUF_SIZE ,1 ); +complete (done); } -static inline int submit_job (struct caam_rng_ctx *ctx,int to_current) +static u32 *caam_init_desc (u32 *desc,dma_addr_t dst_dma,int len) { -struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)]; -struct device *jrdev = ctx->jrdev; -u32 *desc = bd->hw_desc; -int err; +init_job_desc (desc,0 );/* + 1 cmd_sz */ +/* Generate random bytes: + 1 cmd_sz */ +append_operation (desc,OP_ALG_ALGSEL_RNG |OP_TYPE_CLASS1_ALG ); +/* Store bytes: + 1 cmd_sz + caam_ptr_sz */ +append_fifo_store (desc, dst_dma, len,FIFOST_TYPE_RNGSTORE ); -dev_dbg (jrdev,"submitting job %d\n" , !(to_current ^ ctx->current_buf)); -init_completion (&bd->filled); - err =caam_jr_enqueue (jrdev, desc, rng_done, ctx); -if (err != -EINPROGRESS ) -complete (&bd->filled);/* don't wait on failed job*/ -else -atomic_inc (&bd->empty);/* note if pending */ +print_hex_dump_debug ("rng job desc@: " ,DUMP_PREFIX_ADDRESS , +16 ,4 , desc,desc_bytes (desc),1 ); -return err; +return desc; } -static int caam_read (struct hwrng *rng,void *data,size_t max,bool wait) +static int caam_rng_read_one (struct device *jrdev, +void *dst,int len, +void *desc, +struct completion *done) { -struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng); -struct buf_data *bd = &ctx->bufs[ctx->current_buf]; -int next_buf_idx, copied_idx; +dma_addr_t dst_dma;int err; -if (atomic_read (&bd->empty)) { -/* try to submit job if there wasn't one */ -if (atomic_read (&bd->empty) ==BUF_EMPTY ) { - err =submit_job (ctx,1 ); -/* if can't submit job, can't even wait */ -if (err != -EINPROGRESS ) -return 0 ; - } -/* no immediate data, so exit if not waiting */ -if (!wait) -return 0 ; - -/* waiting for pending job */ -if (atomic_read (&bd->empty)) -wait_for_completion (&bd->filled); - } - - next_buf_idx = ctx->cur_buf_idx + max; -dev_dbg (ctx->jrdev,"%s: start reading at buffer %d, idx %d\n" , - __func__, ctx->current_buf, ctx->cur_buf_idx); + len =min_t (int, len,CAAM_RNG_MAX_FIFO_STORE_SIZE ); -/* if enough data in current buffer */ -if (next_buf_idx <RN_BUF_SIZE ) { -memcpy (data, bd->buf + ctx->cur_buf_idx, max); - ctx->cur_buf_idx = next_buf_idx; -return max; + dst_dma =dma_map_single (jrdev, dst, len,DMA_FROM_DEVICE ); +if (dma_mapping_error (jrdev, dst_dma)) { +dev_err (jrdev,"unable to map destination memory\n" ); +return -ENOMEM ; } -/* else, copy what's left... */ - copied_idx =RN_BUF_SIZE - ctx->cur_buf_idx; -memcpy (data, bd->buf + ctx->cur_buf_idx, copied_idx); - ctx->cur_buf_idx =0 ; -atomic_set (&bd->empty,BUF_EMPTY ); - -/* ...refill... */ -submit_job (ctx,1 ); +init_completion (done); + err =caam_jr_enqueue (jrdev, +caam_init_desc (desc, dst_dma, len), + caam_rng_done, done); +if (err == -EINPROGRESS ) { +wait_for_completion (done); + err =0 ; + } -/* and use next buffer */ - ctx->current_buf = !ctx->current_buf; -dev_dbg (ctx->jrdev,"switched to buffer %d\n" , ctx->current_buf); +dma_unmap_single (jrdev, dst_dma, len,DMA_FROM_DEVICE ); -/* since there already is some data read, don't wait */ -return copied_idx +caam_read (rng, data + copied_idx, - max - copied_idx, false); +return err ?: len; } -static inline int rng_create_sh_desc (struct caam_rng_ctx *ctx) +static void caam_rng_fill_async (struct caam_rng_ctx *ctx) { -struct device *jrdev = ctx->jrdev; -u32 *desc = ctx->sh_desc; - -init_sh_desc (desc,HDR_SHARE_SERIAL ); - -/* Generate random bytes */ -append_operation (desc,OP_ALG_ALGSEL_RNG |OP_TYPE_CLASS1_ALG ); - -/* Store bytes */ -append_seq_fifo_store (desc,RN_BUF_SIZE ,FIFOST_TYPE_RNGSTORE ); - - ctx->sh_desc_dma =dma_map_single (jrdev, desc,desc_bytes (desc), -DMA_TO_DEVICE ); -if (dma_mapping_error (jrdev, ctx->sh_desc_dma)) { -dev_err (jrdev,"unable to map shared descriptor\n" ); -return -ENOMEM ; - } - -print_hex_dump_debug ("rng shdesc@: " ,DUMP_PREFIX_ADDRESS ,16 ,4 , - desc,desc_bytes (desc),1 ); +struct scatterlist sg[1 ]; +struct completion done; +int len, nents; + +sg_init_table (sg,ARRAY_SIZE (sg)); + nents =kfifo_dma_in_prepare (&ctx->fifo, sg,ARRAY_SIZE (sg), +CAAM_RNG_FIFO_LEN ); +if (!nents) +return ; + + len =caam_rng_read_one (ctx->jrdev,sg_virt (&sg[0 ]), + sg[0 ].length, + ctx->desc_async, + &done); +if (len <0 ) +return ; + +kfifo_dma_in_finish (&ctx->fifo, len); +} -return 0 ; +static void caam_rng_worker (struct work_struct *work) +{ + struct caam_rng_ctx *ctx = container_of(work, struct caam_rng_ctx, + worker); +caam_rng_fill_async (ctx); } -static inline int rng_create_job_desc (struct caam_rng_ctx *ctx,int buf_id) +static int caam_read (struct hwrng *rng,void *dst,size_t max,bool wait) { -struct device *jrdev = ctx->jrdev; -struct buf_data *bd = &ctx->bufs[buf_id]; -u32 *desc = bd->hw_desc; -int sh_len =desc_len (ctx->sh_desc); +struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng); +int out; -init_job_desc_shared (desc, ctx->sh_desc_dma, sh_len,HDR_SHARE_DEFER | -HDR_REVERSE ); +if (wait) { +struct completion done; - bd->addr =dma_map_single (jrdev, bd->buf,RN_BUF_SIZE ,DMA_FROM_DEVICE ); -if (dma_mapping_error (jrdev, bd->addr)) { -dev_err (jrdev,"unable to map dst\n" ); -return -ENOMEM ; +return caam_rng_read_one (ctx->jrdev, dst, max, + ctx->desc_sync, &done); } -append_seq_out_ptr_intlen (desc, bd->addr,RN_BUF_SIZE ,0 ); + out =kfifo_out (&ctx->fifo, dst, max); +if (kfifo_len (&ctx->fifo) <=CAAM_RNG_FIFO_LEN /2 ) +schedule_work (&ctx->worker); -print_hex_dump_debug ("rng job desc@: " ,DUMP_PREFIX_ADDRESS ,16 ,4 , - desc,desc_bytes (desc),1 ); - -return 0 ; +return out; }static void caam_cleanup (struct hwrng *rng) {struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng); -int i; -struct buf_data *bd; -for (i =0 ; i <2 ; i++) { - bd = &ctx->bufs[i]; -if (atomic_read (&bd->empty) ==BUF_PENDING ) -wait_for_completion (&bd->filled); - } - -rng_unmap_ctx (ctx); +flush_work (&ctx->worker);caam_jr_free (ctx->jrdev); +kfifo_free (&ctx->fifo); } -static int caam_init_buf (struct caam_rng_ctx *ctx,int buf_id) +static int caam_init (struct hwrng *rng) { -struct buf_data *bd = &ctx->bufs[buf_id]; +struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng);int err; - err =rng_create_job_desc (ctx, buf_id); -if (err) -return err; + ctx->desc_sync =devm_kzalloc (ctx->ctrldev,CAAM_RNG_DESC_LEN , +GFP_DMA |GFP_KERNEL ); +if (!ctx->desc_sync) +return -ENOMEM ; -atomic_set (&bd->empty,BUF_EMPTY ); -submit_job (ctx, buf_id == ctx->current_buf); -wait_for_completion (&bd->filled); + ctx->desc_async =devm_kzalloc (ctx->ctrldev,CAAM_RNG_DESC_LEN , +GFP_DMA |GFP_KERNEL ); +if (!ctx->desc_async) +return -ENOMEM ; -return 0 ; -} +if (kfifo_alloc (&ctx->fifo,CAAM_RNG_FIFO_LEN ,GFP_DMA |GFP_KERNEL )) +return -ENOMEM ; -static int caam_init (struct hwrng *rng) -{ -struct caam_rng_ctx *ctx =to_caam_rng_ctx (rng); -int err; +INIT_WORK (&ctx->worker, caam_rng_worker); ctx->jrdev =caam_jr_alloc (); err =PTR_ERR_OR_ZERO (ctx->jrdev);if (err) { +kfifo_free (&ctx->fifo);pr_err ("Job Ring Device allocation for transform failed\n" );return err; } - err =rng_create_sh_desc (ctx); -if (err) - goto free_jrdev; - - ctx->current_buf =0 ; - ctx->cur_buf_idx =0 ; - - err =caam_init_buf (ctx,0 ); -if (err) - goto free_jrdev; - - err =caam_init_buf (ctx,1 ); -if (err) - goto free_jrdev; +/* +* Fill async buffer to have early randomness data for +* hw_random +*/ +caam_rng_fill_async (ctx);return 0 ; - -free_jrdev: -caam_jr_free (ctx->jrdev); -return err; }int caam_rng_init (struct device *ctrldev);
@@ -335,15 +225,18 @@ int caam_rng_init(struct device *ctrldev)if (!devres_open_group (ctrldev, caam_rng_init,GFP_KERNEL ))return -ENOMEM ; - ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_DMA |GFP_KERNEL ); + ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_KERNEL );if (!ctx)return -ENOMEM ; + ctx->ctrldev = ctrldev; + ctx->rng.name ="rng-caam" ; ctx->rng.init = caam_init; ctx->rng.cleanup = caam_cleanup; ctx->rng.read = caam_read; ctx->rng.priv = (unsigned long )ctx; + ctx->rng.quality =1024 ;dev_info (ctrldev,"registering rng-caam\n" );
Signature · 1 line
2.21.0
PATCH v8 7/8 crypto: caam - enable prediction resistance in HRWNG
In reply to Andrey SmirnovInstantiate CAAM RNG with prediction resistance enabled to improve its quality (with PR on DRNG is forced to reseed from TRNG every time random data is generated).
Management Complex firmware with version lower than 10.20.0 doesn't provide prediction resistance support. Consider this and only instantiate rng when mc f/w version is lower.
--- drivers/crypto/caam/Kconfig | 1 + drivers/crypto/caam/caamrng.c | 3 +- drivers/crypto/caam/ctrl.c | 73 ++++++++++++++++++++++++++++------- drivers/crypto/caam/desc.h | 2 + drivers/crypto/caam/regs.h | 4 +- 5 files changed, 68 insertions(+), 15 deletions(-)
drivers/crypto/caam/Kconfig +1 -0
@@ -13,6 +13,7 @@ config CRYPTO_DEV_FSL_CAAM depends on FSL_SOC || ARCH_MXC || ARCH_LAYERSCAPE select SOC_BUS select CRYPTO_DEV_FSL_CAAM_COMMON + imply FSL_MC_BUS help Enables the driver module for Freescale's Cryptographic Accelerator and Assurance Module (CAAM), also known as the SEC version 4 (SEC4).
drivers/crypto/caam/caamrng.c +2 -1
@@ -69,7 +69,8 @@ static u32 *caam_init_desc(u32 *desc, dma_addr_t dst_dma, int len) {init_job_desc (desc,0 );/* + 1 cmd_sz */ /* Generate random bytes: + 1 cmd_sz */ -append_operation (desc,OP_ALG_ALGSEL_RNG |OP_TYPE_CLASS1_ALG ); +append_operation (desc,OP_ALG_ALGSEL_RNG |OP_TYPE_CLASS1_ALG | +OP_ALG_PR_ON );/* Store bytes: + 1 cmd_sz + caam_ptr_sz */ append_fifo_store (desc, dst_dma, len,FIFOST_TYPE_RNGSTORE );
drivers/crypto/caam/ctrl.c +60 -13
147 lines not on this page · raw message
drivers/crypto/caam/desc.h +2 -0
@@ -1254,6 +1254,8 @@#define OP_ALG_ICV_OFF (0 << OP_ALG_ICV_SHIFT)#define OP_ALG_ICV_ON (1 << OP_ALG_ICV_SHIFT) +#define OP_ALG_PR_ON BIT(1) +#define OP_ALG_DIR_SHIFT 0#define OP_ALG_DIR_MASK 1#define OP_ALG_DECRYPT 0
drivers/crypto/caam/regs.h +3 -1
@@ -524,9 +524,11 @@ struct rng4tst {u32 rsvd1[40 ];#define RDSTA_SKVT 0x80000000#define RDSTA_SKVN 0x40000000 +#define RDSTA_PR0 BIT(4) +#define RDSTA_PR1 BIT(5)#define RDSTA_IF0 0x00000001#define RDSTA_IF1 0x00000002 -#define RDSTA_IFMASK (RDSTA_IF1 | RDSTA_IF0) +#define RDSTA_MASK (RDSTA_PR1 | RDSTA_PR0 | RDSTA_IF1 | RDSTA_IF0)u32 rdsta;u32 rsvd2[15 ]; };
Signature · 1 line
2.21.0
PATCH v8 5/8 crypto: caam - check if RNG job failed
In reply to Andrey SmirnovWe shouldn't stay silent if RNG job fails. Add appropriate code to check for that case and propagate error code up appropriately.
--- drivers/crypto/caam/caamrng.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/crypto/caam/caamrng.c +15 -6
@@ -44,6 +44,11 @@ struct caam_rng_ctx {struct kfifo fifo; }; +struct caam_rng_job_ctx { +struct completion *done; +int *err; +}; +static struct caam_rng_ctx *to_caam_rng_ctx (struct hwrng *r) {return (struct caam_rng_ctx *)r->priv;
@@ -52,12 +57,12 @@ static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r)static void caam_rng_done (struct device *jrdev,u32 *desc,u32 err,void *context) { -struct completion *done = context; +struct caam_rng_job_ctx *jctx = context;if (err) -caam_jr_strstatus (jrdev, err); + *jctx->err =caam_jr_strstatus (jrdev, err); -complete (done); +complete (jctx->done); }static u32 *caam_init_desc (u32 *desc,dma_addr_t dst_dma,int len)
@@ -80,7 +85,11 @@ static int caam_rng_read_one(struct device *jrdev, struct completion *done) {dma_addr_t dst_dma; -int err; +int err, ret =0 ; +struct caam_rng_job_ctx jctx = { + .done = done, + .err = &ret, + }; len =min_t (int, len,CAAM_RNG_MAX_FIFO_STORE_SIZE );
@@ -93,7 +102,7 @@ static int caam_rng_read_one(struct device *jrdev,init_completion (done); err =caam_jr_enqueue (jrdev,caam_init_desc (desc, dst_dma, len), - caam_rng_done, done); + caam_rng_done, &jctx);if (err == -EINPROGRESS ) {wait_for_completion (done); err =0 ;
@@ -101,7 +110,7 @@ static int caam_rng_read_one(struct device *jrdev,dma_unmap_single (jrdev, dst_dma, len,DMA_FROM_DEVICE ); -return err ?: len; +return err ?: (ret ?: len); }static void caam_rng_fill_async (struct caam_rng_ctx *ctx)
Signature · 1 line
2.21.0
PATCH v8 6/8 crypto: caam - invalidate entropy register during RNG initialization
In reply to Andrey SmirnovIn order to make sure that we always use non-stale entropy data, change the code to invalidate entropy register during RNG initialization.
--- drivers/crypto/caam/ctrl.c | 11 ++++++++--- drivers/crypto/caam/regs.h | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-)
drivers/crypto/caam/ctrl.c +8 -3
@@ -341,8 +341,12 @@ static void kick_trng(struct platform_device *pdev, int ent_delay) ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; r4tst = &ctrl->r4tst[0 ]; -/* put RNG4 into program mode */ -clrsetbits_32 (&r4tst->rtmctl,0 ,RTMCTL_PRGM ); +/* +* Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to +* properly invalidate the entropy in the entropy register and +* force re-generation. +*/ +clrsetbits_32 (&r4tst->rtmctl,0 ,RTMCTL_PRGM |RTMCTL_ACC );/* * Performance-wise, it does not make sense to
@@ -372,7 +376,8 @@ static void kick_trng(struct platform_device *pdev, int ent_delay)* select raw sampling in both entropy shifter * and statistical checker; ; put RNG4 into run mode */ -clrsetbits_32 (&r4tst->rtmctl,RTMCTL_PRGM ,RTMCTL_SAMP_MODE_RAW_ES_SC ); +clrsetbits_32 (&r4tst->rtmctl,RTMCTL_PRGM |RTMCTL_ACC , +RTMCTL_SAMP_MODE_RAW_ES_SC ); }static int caam_get_era_from_hw (struct caam_ctrl __iomem *ctrl)
drivers/crypto/caam/regs.h +2 -1
@@ -487,7 +487,8 @@ struct rngtst {/* RNG4 TRNG test registers */ struct rng4tst { -#define RTMCTL_PRGM 0x00010000/* 1 -> program mode, 0 -> run mode */ +#define RTMCTL_ACC BIT(5)/* TRNG access mode */ +#define RTMCTL_PRGM BIT(16)/* 1 -> program mode, 0 -> run mode */ #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC 0/* use von Neumann data in both entropy shifter and statistical checker */
Signature · 1 line
2.21.0
PATCH v8 8/8 crypto: caam - limit single JD RNG output to maximum of 16 bytes
In reply to Andrey SmirnovIn order to follow recommendation in SP800-90C (section "9.4 The Oversampling-NRBG Construction") limit the output of "generate" JD submitted to CAAM. See
https://lore.kernel.org/linux-crypto/VI1PR0402MB3485EF10976A4A69F90E5B0F98580@VI1PR0402MB3485.eurprd04.prod.outlook.com/
for more details.
This change should make CAAM's hwrng driver good enough to have 1024 quality rating.
--- drivers/crypto/caam/caamrng.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/crypto/caam/caamrng.c +10 -10
@@ -22,9 +22,7 @@#include "jr.h" #include "error.h" -#define CAAM_RNG_MAX_FIFO_STORE_SIZE U16_MAX - -#define CAAM_RNG_FIFO_LEN SZ_32K/* Must be a multiple of 2 */ +#define CAAM_RNG_MAX_FIFO_STORE_SIZE 16/* * Length of used descriptors, see caam_init_desc()
@@ -65,14 +63,15 @@ static void caam_rng_done(struct device *jrdev, u32 *desc, u32 err,complete (jctx->done); } -static u32 *caam_init_desc (u32 *desc,dma_addr_t dst_dma,int len) +static u32 *caam_init_desc (u32 *desc,dma_addr_t dst_dma) {init_job_desc (desc,0 );/* + 1 cmd_sz */ /* Generate random bytes: + 1 cmd_sz */ append_operation (desc,OP_ALG_ALGSEL_RNG |OP_TYPE_CLASS1_ALG |OP_ALG_PR_ON );/* Store bytes: + 1 cmd_sz + caam_ptr_sz */ -append_fifo_store (desc, dst_dma, len,FIFOST_TYPE_RNGSTORE ); +append_fifo_store (desc, dst_dma, +CAAM_RNG_MAX_FIFO_STORE_SIZE ,FIFOST_TYPE_RNGSTORE );print_hex_dump_debug ("rng job desc@: " ,DUMP_PREFIX_ADDRESS ,16 ,4 , desc,desc_bytes (desc),1 );
@@ -92,7 +91,7 @@ static int caam_rng_read_one(struct device *jrdev, .err = &ret, }; - len =min_t (int, len,CAAM_RNG_MAX_FIFO_STORE_SIZE ); + len =CAAM_RNG_MAX_FIFO_STORE_SIZE ; dst_dma =dma_map_single (jrdev, dst, len,DMA_FROM_DEVICE );if (dma_mapping_error (jrdev, dst_dma)) {
@@ -102,7 +101,7 @@ static int caam_rng_read_one(struct device *jrdev,init_completion (done); err =caam_jr_enqueue (jrdev, -caam_init_desc (desc, dst_dma, len), +caam_init_desc (desc, dst_dma), caam_rng_done, &jctx);if (err == -EINPROGRESS ) {wait_for_completion (done);
@@ -122,7 +121,7 @@ static void caam_rng_fill_async(struct caam_rng_ctx *ctx)sg_init_table (sg,ARRAY_SIZE (sg)); nents =kfifo_dma_in_prepare (&ctx->fifo, sg,ARRAY_SIZE (sg), -CAAM_RNG_FIFO_LEN ); +CAAM_RNG_MAX_FIFO_STORE_SIZE );if (!nents)return ;
@@ -156,7 +155,7 @@ static int caam_read(struct hwrng *rng, void *dst, size_t max, bool wait) } out =kfifo_out (&ctx->fifo, dst, max); -if (kfifo_len (&ctx->fifo) <=CAAM_RNG_FIFO_LEN /2 ) +if (kfifo_is_empty (&ctx->fifo))schedule_work (&ctx->worker);return out;
@@ -186,7 +185,8 @@ static int caam_init(struct hwrng *rng)if (!ctx->desc_async)return -ENOMEM ; -if (kfifo_alloc (&ctx->fifo,CAAM_RNG_FIFO_LEN ,GFP_DMA |GFP_KERNEL )) +if (kfifo_alloc (&ctx->fifo,CAAM_RNG_MAX_FIFO_STORE_SIZE , +GFP_DMA |GFP_KERNEL ))return -ENOMEM ;INIT_WORK (&ctx->worker, caam_rng_worker);
Signature · 1 line
2.21.0
kbuild test robot
build botRe: [PATCH v8 7/8] crypto: caam - enable prediction resistance in HRWNG
In reply to Andrey SmirnovThank you for the patch! Yet something to improve:
Hi Andrey,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on cryptodev/master]
[also build test ERROR on next-20200316]
[cannot apply to crypto/master v5.6-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Andrey-Smirnov/enable-CAAM-s-HWRNG-as-default/20200317-072420 base: https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master config: arm64-defconfig (attached as .config) compiler: aarch64-linux-gcc (GCC) 9.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=9.2.0 make.cross ARCH=arm64
If you fix the issue, kindly add following tag
All error/warnings (new ones prefixed by >>):
>> drivers/crypto/caam/ctrl.c:583:34: warning: 'struct fsl_mc_version' declared inside parameter list will not be visible outside of this definition or declaration
583 | static bool check_version(struct fsl_mc_version *mc_version, u32 major,
| ^~~~~~~~~~~~~~
drivers/crypto/caam/ctrl.c: In function 'check_version':
>> drivers/crypto/caam/ctrl.c:586:16: error: dereferencing pointer to incomplete type 'struct fsl_mc_version'
586 | if (mc_version->major > major)
| ^~
drivers/crypto/caam/ctrl.c: In function 'caam_probe':
>> drivers/crypto/caam/ctrl.c:714:16: error: implicit declaration of function 'fsl_mc_get_version'; did you mean 'fsl_mc_get_endpoint'? [-Werror=implicit-function-declaration]
714 | mc_version = fsl_mc_get_version();
| ^~~~~~~~~~~~~~~~~~
| fsl_mc_get_endpoint
>> drivers/crypto/caam/ctrl.c:714:14: warning: assignment to 'struct fsl_mc_version *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
714 | mc_version = fsl_mc_get_version();
| ^
>> drivers/crypto/caam/ctrl.c:716:31: error: passing argument 1 of 'check_version' from incompatible pointer type [-Werror=incompatible-pointer-types]
716 | pr_support = check_version(mc_version, 10, 20, 0);
| ^~~~~~~~~~
| |
| struct fsl_mc_version * drivers/crypto/caam/ctrl.c:583:50: note: expected 'struct fsl_mc_version *' but argument is of type 'struct fsl_mc_version *'
583 | static bool check_version(struct fsl_mc_version *mc_version, u32 major,
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
cc1: some warnings being treated as errors
vim +586 drivers/crypto/caam/ctrl.c
581
582 #ifdef CONFIG_FSL_MC_BUS
> 583 static bool check_version(struct fsl_mc_version *mc_version, u32 major,
584 u32 minor, u32 revision)
585 {
> 586 if (mc_version->major > major)
587 return true;
588
589 if (mc_version->major == major) {
590 if (mc_version->minor > minor)
591 return true;
592
593 if (mc_version->minor == minor && mc_version->revision > 0)
594 return true;
595 }
596
597 return false;
598 }
599 #endif
600
601
602 /* Probe routine for CAAM top (controller) level */
603 static int caam_probe(struct platform_device *pdev)
604 {
605 int ret, ring, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
606 u64 caam_id;
607 const struct soc_device_attribute *imx_soc_match;
608 struct device *dev;
609 struct device_node *nprop, *np;
610 struct caam_ctrl __iomem *ctrl;
611 struct caam_drv_private *ctrlpriv;
612 #ifdef CONFIG_DEBUG_FS
613 struct caam_perfmon *perfmon;
614 struct dentry *dfs_root;
615 #endif
616 u32 scfgr, comp_params;
617 u8 rng_vid;
618 int pg_size;
619 int BLOCK_OFFSET = 0;
620 bool pr_support = false;
621
622 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
623 if (!ctrlpriv)
624 return -ENOMEM;
625
626 dev = &pdev->dev;
627 dev_set_drvdata(dev, ctrlpriv);
628 nprop = pdev->dev.of_node;
629
630 imx_soc_match = soc_device_match(caam_imx_soc_table);
631 caam_imx = (bool)imx_soc_match;
632
633 if (imx_soc_match) {
634 if (!imx_soc_match->data) {
635 dev_err(dev, "No clock data provided for i.MX SoC");
636 return -EINVAL;
637 }
638
639 ret = init_clocks(dev, imx_soc_match->data);
640 if (ret)
641 return ret;
642 }
643
644
645 /* Get configuration properties from device tree */
646 /* First, get register page */
647 ctrl = devm_of_iomap(dev, nprop, 0, NULL);
648 ret = PTR_ERR_OR_ZERO(ctrl);
649 if (ret) {
650 dev_err(dev, "caam: of_iomap() failed\n");
651 return ret;
652 }
653
654 caam_little_end = !(bool)(rd_reg32(&ctrl->perfmon.status) &
655 (CSTA_PLEND | CSTA_ALT_PLEND));
656 comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
657 if (comp_params & CTPR_MS_PS && rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR)
658 caam_ptr_sz = sizeof(u64);
659 else
660 caam_ptr_sz = sizeof(u32);
661 caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2);
662 ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK);
663
664 #ifdef CONFIG_CAAM_QI
665 /* If (DPAA 1.x) QI present, check whether dependencies are available */
666 if (ctrlpriv->qi_present && !caam_dpaa2) {
667 ret = qman_is_probed();
668 if (!ret) {
669 return -EPROBE_DEFER;
670 } else if (ret < 0) {
671 dev_err(dev, "failing probe due to qman probe error\n");
672 return -ENODEV;
673 }
674
675 ret = qman_portals_probed();
676 if (!ret) {
677 return -EPROBE_DEFER;
678 } else if (ret < 0) {
679 dev_err(dev, "failing probe due to qman portals probe error\n");
680 return -ENODEV;
681 }
682 }
683 #endif
684
685 /* Allocating the BLOCK_OFFSET based on the supported page size on
686 * the platform
687 */
688 pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
689 if (pg_size == 0)
690 BLOCK_OFFSET = PG_SIZE_4K;
691 else
692 BLOCK_OFFSET = PG_SIZE_64K;
693
694 ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl;
695 ctrlpriv->assure = (struct caam_assurance __iomem __force *)
696 ((__force uint8_t *)ctrl +
697 BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
698 );
699 ctrlpriv->deco = (struct caam_deco __iomem __force *)
700 ((__force uint8_t *)ctrl +
701 BLOCK_OFFSET * DECO_BLOCK_NUMBER
702 );
703
704 /* Get the IRQ of the controller (for security violations only) */
705 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
706 np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc");
707 ctrlpriv->mc_en = !!np;
708 of_node_put(np);
709
710 #ifdef CONFIG_FSL_MC_BUS
711 if (ctrlpriv->mc_en) {
712 struct fsl_mc_version *mc_version;
713
> 714 mc_version = fsl_mc_get_version();
715 if (mc_version)
> 716 pr_support = check_version(mc_version, 10, 20, 0); 717 else
718 return -EPROBE_DEFER;
719 }
720 #endif
721
722 /*
723 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
724 * long pointers in master configuration register.
725 * In case of SoCs with Management Complex, MC f/w performs
726 * the configuration.
727 */
728 if (!ctrlpriv->mc_en)
729 clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK,
730 MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF |
731 MCFGR_WDENABLE | MCFGR_LARGE_BURST);
732
733 handle_imx6_err005766(&ctrl->mcr);
734
735 /*
736 * Read the Compile Time paramters and SCFGR to determine
737 * if Virtualization is enabled for this platform
738 */
739 scfgr = rd_reg32(&ctrl->scfgr);
740
741 ctrlpriv->virt_en = 0;
742 if (comp_params & CTPR_MS_VIRT_EN_INCL) {
743 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
744 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
745 */
746 if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
747 (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
748 (scfgr & SCFGR_VIRT_EN)))
749 ctrlpriv->virt_en = 1;
750 } else {
751 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
752 if (comp_params & CTPR_MS_VIRT_EN_POR)
753 ctrlpriv->virt_en = 1;
754 }
755
756 if (ctrlpriv->virt_en == 1)
757 clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
758 JRSTART_JR1_START | JRSTART_JR2_START |
759 JRSTART_JR3_START);
760
761 ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev));
762 if (ret) {
763 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
764 return ret;
765 }
766
767 ctrlpriv->era = caam_get_era(ctrl);
768 ctrlpriv->domain = iommu_get_domain_for_dev(dev);
769
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Horia Geantă
maintainerQuoted 2 lines · Andrey Smirnov
Feedback is welcome!
The patch set does not compile.
Patch "bus: fsl-mc: add api to retrieve mc version"
https://patchwork.kernel.org/patch/11352493/
was not applied, the plan being to merge it via cryptodev-2.6 tree, see my comment here:
https://lore.kernel.org/linux-arm-kernel/VI1PR0402MB3485DB40CE1C1631D920EE7598040@VI1PR0402MB3485.eurprd04.prod.outlook.com/
I think it needs to be formally included in the patch set.
Thanks,
Horia
Re: [PATCH v8 1/8] crypto: caam - allocate RNG instantiation descriptor with GFP_DMA
In reply to Andrey SmirnovQuoted 12 lines · Andrey Smirnov
Be consistent with the rest of the codebase and use
GFP_DMAwhen allocating memory for a CAAM JR descriptor.Signed-off-by Andrey Smirnov <[email protected]>Cc Chris Healy <[email protected]>Cc Lucas Stach <[email protected]>Cc Horia Geantă <[email protected]>Cc Herbert Xu <[email protected]>Cc Iuliana Prodan <[email protected]>
Thanks,
Horia
Horia Geantă
maintainerRe: [PATCH v8 3/8] crypto: caam - drop global context pointer and init_done
In reply to Andrey SmirnovQuoted 15 lines · Andrey Smirnov
drivers/crypto/caam/caamrng.c +1
-0@@ -70,6 +70,7 @@ struct buf_data {/* rng per-device context */ struct caam_rng_ctx { +struct hwrng rng;struct device *jrdev;dma_addr_t sh_desc_dma;u32 sh_desc[DESC_RNG_LEN ];@@ -78,13 +79,10 @@ struct caam_rng_ctx {struct buf_data bufs[2 ]; };
[...]
Quoted 4 lines
drivers/crypto/caam/caamrng.c +4
-0+static struct caam_rng_ctx *to_caam_rng_ctx (struct hwrng *r) +{ +return (struct caam_rng_ctx *)r->priv; +}
[...]
drivers/crypto/caam/caamrng.c static int caam_init(struct hwrng *rng) · line 317
Quoted 6 lines
drivers/crypto/caam/caamrng.c +0
-6-static struct hwrng caam_rng = { - .name ="rng-caam" , - .init = caam_init, - .cleanup = caam_cleanup, - .read = caam_read, -};
I would keep this statically allocated, see below.
Quoted 16 lines
drivers/crypto/caam/caamrng.c +8
-2@@ -342,18 +332,27 @@ int caam_rng_init(struct device *ctrldev)if (!rng_inst)return 0 ; - rng_ctx =kmalloc (sizeof (*rng_ctx),GFP_DMA |GFP_KERNEL ); -if (!rng_ctx) +if (!devres_open_group (ctrldev, caam_rng_init,GFP_KERNEL )) +return -ENOMEM ; + + ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_DMA |GFP_KERNEL ); +if (!ctx)return -ENOMEM ; + ctx->rng.name ="rng-caam" ; + ctx->rng.init = caam_init; + ctx->rng.cleanup = caam_cleanup;
drivers/crypto/caam/caamrng.c int caam_rng_init(struct device *ctrldev) · line 350
Quoted 10 lines
drivers/crypto/caam/caamrng.c +4
-4+ ctx->rng.read = caam_read; + ctx->rng.priv = (unsigned long )ctx; +dev_info (ctrldev,"registering rng-caam\n" ); - err =hwrng_register (&caam_rng); -if (!err) { - init_done = true; -return err; + ret =devm_hwrng_register (ctrldev, &ctx->rng);
Now that hwrng.priv is used to keep driver's private data / caam_rng_ctx, and thus container_of() is no longer needed to get from hwrng struct to caam_rng_ctx, it's no longer needed to embed struct hwrng into caam_rng_ctx.
Horia
Re: [PATCH v8 5/8] crypto: caam - check if RNG job failed
In reply to Andrey SmirnovQuoted 12 lines · Andrey Smirnov
We shouldn't stay silent if RNG job fails. Add appropriate code to check for that case and propagate error code up appropriately.
Signed-off-by Andrey Smirnov <[email protected]>Cc Chris Healy <[email protected]>Cc Lucas Stach <[email protected]>Cc Horia Geantă <[email protected]>Cc Herbert Xu <[email protected]>Cc Iuliana Prodan <[email protected]>
Thanks,
Horia
Horia Geantă
maintainerRe: [PATCH v8 7/8] crypto: caam - enable prediction resistance in HRWNG
In reply to Andrey SmirnovQuoted 8 lines · Andrey Smirnov
Instantiate CAAM RNG with prediction resistance enabled to improve its quality (with PR on DRNG is forced to reseed from TRNG every time random data is generated).
Management Complex firmware with version lower than 10.20.0 doesn't provide prediction resistance support. Consider this and only instantiate rng when mc f/w version is lower.
As mentioned previously, this does not compile.
Please include the dependency
"bus: fsl-mc: add api to retrieve mc version"
https://patchwork.kernel.org/patch/11352493/
in the patch set.
Quoted 6 lines
drivers/crypto/caam/ctrl.c +2
-0@@ -564,6 +579,26 @@ static void caam_remove_debugfs(void *root) }#endif +#ifdef CONFIG_FSL_MC_BUS +static bool check_version(struct fsl_mc_version *mc_version, u32 major,
drivers/crypto/caam/ctrl.c static void caam_remove_debugfs(void *root) · line 593
Quoted 10 lines
drivers/crypto/caam/ctrl.c +10
-0+ u32 minor, u32 revision) +{ +if (mc_version->major > major) +return true; + +if (mc_version->major == major) { +if (mc_version->minor > minor) +return true; + +if (mc_version->minor == minor && mc_version->revision >0 )
There's a typo in the code we provided, sorry for this. Should be:
if (mc_version->minor == minor && mc_version->revision > revision)
Thanks,
Horia
Horia Geantă
maintainerRe: [PATCH v8 7/8] crypto: caam - enable prediction resistance in HRWNG
In reply to Andrey SmirnovQuoted 14 lines · Andrey Smirnov
drivers/crypto/caam/ctrl.c +10
-0@@ -564,6 +579,26 @@ static void caam_remove_debugfs(void *root) }#endif +#ifdef CONFIG_FSL_MC_BUS +static bool check_version (struct fsl_mc_version *mc_version,u32 major, +u32 minor,u32 revision) +{ +if (mc_version->major > major) +return true; + +if (mc_version->major == major) { +if (mc_version->minor > minor) +return true;
drivers/crypto/caam/ctrl.c static void caam_remove_debugfs(void *root) · line 601
Quoted 10 lines
drivers/crypto/caam/ctrl.c +10
-0+ +if (mc_version->minor == minor && mc_version->revision >0 ) + return true; + } + +return false; +} +#endif + +
Nitpick - checkpatch complains here:
CHECK: Please don't use multiple blank lines
Horia
Re: [PATCH v8 4/8] crypto: caam - simplify RNG implementation
In reply to Andrey SmirnovQuoted 34 lines · Andrey Smirnov
Rework CAAM RNG implementation as follows:
- Make use of the fact that HWRNG supports partial reads and will handle such cases gracefully by removing recursion in
caam_read()- Convert blocking
caam_read()codepath to do a single blocking job read directly into requested buffer, bypassing any intermediary buffers- Convert async
caam_read()codepath into a simple single reader/single writer FIFO use-case, thus simplifying concurrency handling and delegating buffer read/write position management to KFIFO subsystem.- Leverage the same low level RNG data extraction code for both async and blocking
caam_read()scenarios, get rid of the shared job descriptor and make non-shared one as a simple as possible (just HEADER + ALGORITHM OPERATION + FIFO STORE)- Split private context from DMA related memory, so that the former could be allocated without
GFP_DMA.NOTE: On its face value this commit decreased throughput numbers reported by
dd if=/dev/hwrng of=/dev/null bs=1 count=100K [iflag=nonblock]
by about 15%, however commits that enable prediction resistance and limit JR total size impact the performance so much and move the bottleneck such as to make this regression irrelevant.
NOTE: On the bright side, this commit reduces RNG in kernel DMA buffer memory usage from 2 x
RN_BUF_SIZE(~256K) to 32K.
Quoted 9 lines
Signed-off-by Andrey Smirnov <[email protected]>Cc Chris Healy <[email protected]>Cc Lucas Stach <[email protected]>Cc Horia Geantă <[email protected]>Cc Herbert Xu <[email protected]>Cc Iuliana Prodan <[email protected]>
Thanks,
Horia
Re: [PATCH v8 8/8] crypto: caam - limit single JD RNG output to maximum of 16 bytes
In reply to Andrey SmirnovQuoted 9 lines · Andrey Smirnov
In order to follow recommendation in SP800-90C (section "9.4 The Oversampling-NRBG Construction") limit the output of "generate" JD submitted to CAAM. See
https://lore.kernel.org/linux-crypto/VI1PR0402MB3485EF10976A4A69F90E5B0F98580@VI1PR0402MB3485.eurprd04.prod.outlook.com/
for more details.This change should make CAAM's hwrng driver good enough to have 1024 quality rating.
Quoted 9 lines
Signed-off-by Andrey Smirnov <[email protected]>Cc Chris Healy <[email protected]>Cc Lucas Stach <[email protected]>Cc Horia Geantă <[email protected]>Cc Herbert Xu <[email protected]>Cc Iuliana Prodan <[email protected]>
Thanks,
Horia
Horia Geantă
maintainerRe: [PATCH v8 4/8] crypto: caam - simplify RNG implementation
In reply to Andrey SmirnovQuoted 8 lines · Andrey Smirnov
drivers/crypto/caam/caamrng.c +1
-1@@ -335,15 +225,18 @@ int caam_rng_init(struct device *ctrldev)if (!devres_open_group (ctrldev, caam_rng_init,GFP_KERNEL ))return -ENOMEM ; - ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_DMA |GFP_KERNEL ); + ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_KERNEL );if (!ctx)return -ENOMEM ;
drivers/crypto/caam/caamrng.c int caam_rng_init(struct device *ctrldev) · line 240
Quoted 10 lines
drivers/crypto/caam/caamrng.c +3
-0+ ctx->ctrldev = ctrldev; + ctx->rng.name ="rng-caam" ; ctx->rng.init = caam_init; ctx->rng.cleanup = caam_cleanup; ctx->rng.read = caam_read; ctx->rng.priv = (unsigned long )ctx; + ctx->rng.quality =1024 ;
Nitpick: setting the quality should be moved to patch "crypto: caam - limit single JD RNG output to maximum of 16 bytes"
Horia
Re: [PATCH v8 3/8] crypto: caam - drop global context pointer and init_done
In reply to Horia GeantăQuoted 52 lines · Horia Geantă
Quoted 15 lines · Andrey Smirnov
drivers/crypto/caam/caamrng.c +1
-0@@ -70,6 +70,7 @@ struct buf_data {/* rng per-device context */ struct caam_rng_ctx { +struct hwrng rng;struct device *jrdev;dma_addr_t sh_desc_dma;u32 sh_desc[DESC_RNG_LEN ];@@ -78,13 +79,10 @@ struct caam_rng_ctx {struct buf_data bufs[2 ]; };[...]
Quoted 4 lines
drivers/crypto/caam/caamrng.c +4
-0+static struct caam_rng_ctx *to_caam_rng_ctx (struct hwrng *r) +{ +return (struct caam_rng_ctx *)r->priv; +}[...]
drivers/crypto/caam/caamrng.c static int caam_init(struct hwrng *rng) · line 317
Quoted 6 lines
drivers/crypto/caam/caamrng.c +0
-6-static struct hwrng caam_rng = { - .name ="rng-caam" , - .init = caam_init, - .cleanup = caam_cleanup, - .read = caam_read, -};I would keep this statically allocated, see below.
Quoted 21 lines
drivers/crypto/caam/caamrng.c +11
-2@@ -342,18 +332,27 @@ int caam_rng_init(struct device *ctrldev)if (!rng_inst)return 0 ; - rng_ctx =kmalloc (sizeof (*rng_ctx),GFP_DMA |GFP_KERNEL ); -if (!rng_ctx) +if (!devres_open_group (ctrldev, caam_rng_init,GFP_KERNEL )) +return -ENOMEM ; + + ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_DMA |GFP_KERNEL ); +if (!ctx)return -ENOMEM ; + ctx->rng.name ="rng-caam" ; + ctx->rng.init = caam_init; + ctx->rng.cleanup = caam_cleanup; + ctx->rng.read = caam_read; + ctx->rng.priv = (unsigned long )ctx; +dev_info (ctrldev,"registering rng-caam\n" );
Quoted 10 lines
drivers/crypto/caam/caamrng.c int caam_rng_init(struct device *ctrldev) · line 350
Quoted 5 lines
drivers/crypto/caam/caamrng.c +1
-4- err =hwrng_register (&caam_rng); -if (!err) { - init_done = true; -return err; + ret =devm_hwrng_register (ctrldev, &ctx->rng);Now that hwrng.priv is used to keep driver's private data /
caam_rng_ctx, and thuscontainer_of()is no longer needed to get from hwrng struct tocaam_rng_ctx, it's no longer needed to embed struct hwrng intocaam_rng_ctx.
Why do we want this change though? It doesn't allow us to constify "struct hwrng", don't seem to give any other benefits (maybe I am missing something?) at the same time creating a new implicit global variable. I'd rather not do this.
Thanks,
Andrey Smirnov
Re: [PATCH v8 4/8] crypto: caam - simplify RNG implementation
In reply to Horia GeantăQuoted 11 lines · Horia Geantă
Quoted 9 lines · Andrey Smirnov
drivers/crypto/caam/caamrng.c +1
-1@@ -335,15 +225,18 @@ int caam_rng_init(struct device *ctrldev)if (!devres_open_group (ctrldev, caam_rng_init,GFP_KERNEL ))return -ENOMEM ; - ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_DMA |GFP_KERNEL ); + ctx =devm_kzalloc (ctrldev,sizeof (*ctx),GFP_KERNEL );if (!ctx)return -ENOMEM ;
Quoted 12 lines
drivers/crypto/caam/caamrng.c int caam_rng_init(struct device *ctrldev) · line 240
Quoted 9 lines
drivers/crypto/caam/caamrng.c +3
-0+ ctx->ctrldev = ctrldev; + ctx->rng.name ="rng-caam" ; ctx->rng.init = caam_init; ctx->rng.cleanup = caam_cleanup; ctx->rng.read = caam_read; ctx->rng.priv = (unsigned long )ctx; + ctx->rng.quality =1024 ;Nitpick: setting the quality should be moved to patch "crypto: caam - limit single JD RNG output to maximum of 16 bytes"
Ugh, looks like I messed this up while making v8. Will fix in v9.
Thanks,
Andrey Smirnov