From: Steffen Trumtrar <s.trumtrar@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Subject: [PATCH 4/5] crypto: caam: add RNG support
Date: Wed, 10 Feb 2016 12:05:54 +0100 [thread overview]
Message-ID: <1455102355-26102-5-git-send-email-s.trumtrar@pengutronix.de> (raw)
In-Reply-To: <1455102355-26102-1-git-send-email-s.trumtrar@pengutronix.de>
The code is based on the Linux v4.1 driver of the same name.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
Changes since v2:
- change cdev parent from jobring device to rng device
drivers/crypto/caam/Kconfig | 7 +
drivers/crypto/caam/Makefile | 1 +
drivers/crypto/caam/caamrng.c | 319 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 327 insertions(+)
create mode 100644 drivers/crypto/caam/caamrng.c
diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig
index d54754c7a89b..4bf1c937c9a5 100644
--- a/drivers/crypto/caam/Kconfig
+++ b/drivers/crypto/caam/Kconfig
@@ -35,3 +35,10 @@ config CRYPTO_DEV_FSL_CAAM_RINGSIZE
7 => 128
8 => 256
9 => 512
+
+config CRYPTO_DEV_FSL_CAAM_RNG
+ bool "Register caam RNG device"
+ depends on CRYPTO_DEV_FSL_CAAM && CRYPTO_DEV_FSL_CAAM_JR
+ default n
+ help
+ Selecting this will register the SEC4 hardware rng.
diff --git a/drivers/crypto/caam/Makefile b/drivers/crypto/caam/Makefile
index cd46936fb6b4..ea63f5894441 100644
--- a/drivers/crypto/caam/Makefile
+++ b/drivers/crypto/caam/Makefile
@@ -3,3 +3,4 @@
#
obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM) += ctrl.o error.o
obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_JR) += jr.o
+obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG) += caamrng.o
diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c
new file mode 100644
index 000000000000..4ec52fb17563
--- /dev/null
+++ b/drivers/crypto/caam/caamrng.c
@@ -0,0 +1,319 @@
+/*
+ * caam - Freescale FSL CAAM support for hw_random
+ *
+ * Copyright (C) 2011-2015 Freescale Semiconductor, Inc.
+ *
+ * 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 <common.h>
+#include <dma.h>
+#include <driver.h>
+#include <init.h>
+#include <linux/spinlock.h>
+
+#include "regs.h"
+#include "intern.h"
+#include "desc_constr.h"
+#include "jr.h"
+#include "error.h"
+
+/*
+ * Maximum buffer size: maximum number of random, cache-aligned bytes that
+ * will be generated and moved to seq out ptr (extlen not allowed)
+ */
+#define RN_BUF_SIZE 32767
+
+/* length of descriptors */
+#define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ * 2)
+#define DESC_RNG_LEN (10 * CAAM_CMD_SZ)
+
+/* Buffer, its dma address and lock */
+struct buf_data {
+ u8 buf[RN_BUF_SIZE];
+ dma_addr_t addr;
+ 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 */
+ int empty;
+};
+
+/* rng per-device context */
+struct caam_rng_ctx {
+ struct device_d *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 cdev cdev;
+};
+
+static struct caam_rng_ctx *rng_ctx;
+
+static void rng_done(struct device_d *jrdev, u32 *desc, u32 err, void *context)
+{
+ struct buf_data *bd;
+
+ bd = (struct buf_data *)((char *)desc -
+ offsetof(struct buf_data, hw_desc));
+
+ if (err)
+ caam_jr_strstatus(jrdev, err);
+
+ bd->empty = BUF_NOT_EMPTY;
+
+ /* Buffer refilled, invalidate cache */
+ dma_sync_single_for_cpu(bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE);
+
+#ifdef DEBUG
+ print_hex_dump(KERN_ERR, "rng refreshed buf@: ",
+ DUMP_PREFIX_OFFSET, 16, 4, bd->buf, RN_BUF_SIZE, 1);
+#endif
+}
+
+static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
+{
+ struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)];
+ struct device_d *jrdev = ctx->jrdev;
+ u32 *desc = bd->hw_desc;
+ int err;
+
+ dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf));
+
+ dma_sync_single_for_device((unsigned long)desc, desc_bytes(desc),
+ DMA_TO_DEVICE);
+
+ err = caam_jr_enqueue(jrdev, desc, rng_done, ctx);
+ if (!err)
+ bd->empty += 1; /* note if pending */
+
+ return err;
+}
+
+static int caam_read(struct caam_rng_ctx *ctx, void *data, size_t max, bool wait)
+{
+ struct buf_data *bd = &ctx->bufs[ctx->current_buf];
+ int next_buf_idx, copied_idx;
+ int err;
+
+ if (bd->empty) {
+ /* try to submit job if there wasn't one */
+ if (bd->empty == BUF_EMPTY) {
+ err = submit_job(ctx, 1);
+ /* if can't submit job, can't even wait */
+ if (err)
+ return 0;
+ }
+ /* no immediate data, so exit if not waiting */
+ if (!wait)
+ return 0;
+ }
+
+ 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);
+
+ /* 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;
+ }
+
+ /* 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;
+ bd->empty = BUF_EMPTY;
+
+ /* ...refill... */
+ err = submit_job(ctx, 1);
+ if (err)
+ return err;
+
+ /* and use next buffer */
+ ctx->current_buf = !ctx->current_buf;
+ dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
+
+ /* since there already is some data read, don't wait */
+ return copied_idx + caam_read(ctx, data + copied_idx,
+ max - copied_idx, false);
+}
+
+static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx)
+{
+ u32 *desc = ctx->sh_desc;
+
+ init_sh_desc(desc, HDR_SHARE_SERIAL);
+
+ /* Propagate errors from shared to job descriptor */
+ append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
+
+ /* 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_addr_t)desc;
+
+ dma_sync_single_for_device((unsigned long)desc, desc_bytes(desc),
+ DMA_TO_DEVICE);
+#ifdef DEBUG
+ print_hex_dump(KERN_ERR, "rng shdesc@: ", DUMP_PREFIX_OFFSET, 16, 4,
+ desc, desc_bytes(desc), 1);
+#endif
+ return 0;
+}
+
+static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id)
+{
+ struct buf_data *bd = &ctx->bufs[buf_id];
+ u32 *desc = bd->hw_desc;
+ int sh_len = desc_len(ctx->sh_desc);
+
+ init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER |
+ HDR_REVERSE);
+
+ bd->addr = (dma_addr_t)bd->buf;
+
+ append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0);
+#ifdef DEBUG
+ print_hex_dump(KERN_ERR, "rng job desc@: ", DUMP_PREFIX_OFFSET, 16, 4,
+ desc, desc_bytes(desc), 1);
+#endif
+ return 0;
+}
+
+static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
+{
+ struct buf_data *bd = &ctx->bufs[buf_id];
+ int err;
+
+ err = rng_create_job_desc(ctx, buf_id);
+ if (err)
+ return err;
+
+ bd->empty = BUF_EMPTY;
+ return submit_job(ctx, buf_id == ctx->current_buf);
+}
+
+static int caam_init_rng(struct caam_rng_ctx *ctx, struct device_d *jrdev)
+{
+ int err;
+
+ ctx->jrdev = jrdev;
+
+ err = rng_create_sh_desc(ctx);
+ if (err)
+ return err;
+
+ ctx->current_buf = 0;
+ ctx->cur_buf_idx = 0;
+
+ err = caam_init_buf(ctx, 0);
+ if (err)
+ return err;
+
+ err = caam_init_buf(ctx, 1);
+ if (err)
+ return err;
+
+ return 0;
+}
+
+static ssize_t random_read(struct cdev *cdev, void *buf, size_t count,
+ loff_t offset, ulong flags)
+{
+ struct caam_rng_ctx *ctx = container_of(cdev, struct caam_rng_ctx, cdev);
+
+ return caam_read(ctx, buf, count, true);
+}
+
+static struct file_operations randomops = {
+ .read = random_read,
+ .lseek = dev_lseek_default,
+};
+
+static int caam_init_devrandom(struct caam_rng_ctx *ctx, struct device_d *dev)
+{
+ ctx->cdev.name = "hwrng";
+ ctx->cdev.flags = DEVFS_IS_CHARACTER_DEV;
+ ctx->cdev.ops = &randomops;
+ ctx->cdev.dev = dev;
+
+ return devfs_create(&ctx->cdev);
+}
+
+static int caam_rng_probe(struct device_d *dev)
+{
+ struct device_d *jrdev;
+ struct device_node *dev_node;
+ int err;
+
+ dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0-job-ring");
+ if (!dev_node) {
+ dev_node = of_find_compatible_node(NULL, NULL,
+ "fsl,sec4.0-job-ring");
+ if (!dev_node)
+ return -ENODEV;
+ }
+
+ jrdev = of_find_device_by_node(dev_node);
+ if (!jrdev)
+ return -ENODEV;
+
+ rng_ctx = xzalloc(sizeof(*rng_ctx));
+
+ err = caam_init_rng(rng_ctx, jrdev);
+ if (err)
+ return err;
+
+ err = caam_init_devrandom(rng_ctx, dev);
+ if (err)
+ return err;
+
+ dev_info(dev, "registering rng-caam\n");
+
+ return 0;
+}
+
+static struct of_device_id caam_rng_ids[] = {
+ {
+ .compatible = "caam_rng",
+ },
+ {},
+};
+
+static struct driver_d caam_rng_driver = {
+ .name = "caam_rng",
+ .of_compatible = DRV_OF_COMPAT(caam_rng_ids),
+ .probe = caam_rng_probe,
+};
+device_platform_driver(caam_rng_driver);
--
2.7.0.rc3
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2016-02-10 11:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-10 11:05 [PATCH 0/5] i.MX6: CAAM engine support Steffen Trumtrar
2016-02-10 11:05 ` [PATCH 1/5] include: linux: add circular buffers Steffen Trumtrar
2016-02-10 11:05 ` [PATCH 2/5] ARM: imx6: add caam clks Steffen Trumtrar
2016-02-10 11:05 ` [PATCH 3/5] crypto: add i.MX6 CAAM support Steffen Trumtrar
2016-02-10 11:05 ` Steffen Trumtrar [this message]
2016-02-10 11:05 ` [PATCH 5/5] ARM: i.MX6: riotboard: enable CAAM RNG Steffen Trumtrar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1455102355-26102-5-git-send-email-s.trumtrar@pengutronix.de \
--to=s.trumtrar@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox