treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / crypto / caam / caamrng.c
blobe8baacaabe07fbd245ac76456eb9da9ffce84167
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * caam - Freescale FSL CAAM support for hw_random
5 * Copyright 2011 Freescale Semiconductor, Inc.
6 * Copyright 2018-2019 NXP
8 * Based on caamalg.c crypto API driver.
10 * relationship between job descriptors to shared descriptors:
12 * --------------- --------------
13 * | JobDesc #0 |-------------------->| ShareDesc |
14 * | *(buffer 0) | |------------->| (generate) |
15 * --------------- | | (move) |
16 * | | (store) |
17 * --------------- | --------------
18 * | JobDesc #1 |------|
19 * | *(buffer 1) |
20 * ---------------
22 * A job desc looks like this:
24 * ---------------------
25 * | Header |
26 * | ShareDesc Pointer |
27 * | SEQ_OUT_PTR |
28 * | (output buffer) |
29 * ---------------------
31 * The SharedDesc never changes, and each job descriptor points to one of two
32 * buffers for each device, from which the data will be copied into the
33 * requested destination
36 #include <linux/hw_random.h>
37 #include <linux/completion.h>
38 #include <linux/atomic.h>
40 #include "compat.h"
42 #include "regs.h"
43 #include "intern.h"
44 #include "desc_constr.h"
45 #include "jr.h"
46 #include "error.h"
49 * Maximum buffer size: maximum number of random, cache-aligned bytes that
50 * will be generated and moved to seq out ptr (extlen not allowed)
52 #define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \
53 L1_CACHE_BYTES)
55 /* length of descriptors */
56 #define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ_MAX * 2)
57 #define DESC_RNG_LEN (3 * CAAM_CMD_SZ)
59 /* Buffer, its dma address and lock */
60 struct buf_data {
61 u8 buf[RN_BUF_SIZE] ____cacheline_aligned;
62 dma_addr_t addr;
63 struct completion filled;
64 u32 hw_desc[DESC_JOB_O_LEN];
65 #define BUF_NOT_EMPTY 0
66 #define BUF_EMPTY 1
67 #define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */
68 atomic_t empty;
71 /* rng per-device context */
72 struct caam_rng_ctx {
73 struct device *jrdev;
74 dma_addr_t sh_desc_dma;
75 u32 sh_desc[DESC_RNG_LEN];
76 unsigned int cur_buf_idx;
77 int current_buf;
78 struct buf_data bufs[2];
81 static struct caam_rng_ctx *rng_ctx;
84 * Variable used to avoid double free of resources in case
85 * algorithm registration was unsuccessful
87 static bool init_done;
89 static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd)
91 if (bd->addr)
92 dma_unmap_single(jrdev, bd->addr, RN_BUF_SIZE,
93 DMA_FROM_DEVICE);
96 static inline void rng_unmap_ctx(struct caam_rng_ctx *ctx)
98 struct device *jrdev = ctx->jrdev;
100 if (ctx->sh_desc_dma)
101 dma_unmap_single(jrdev, ctx->sh_desc_dma,
102 desc_bytes(ctx->sh_desc), DMA_TO_DEVICE);
103 rng_unmap_buf(jrdev, &ctx->bufs[0]);
104 rng_unmap_buf(jrdev, &ctx->bufs[1]);
107 static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context)
109 struct buf_data *bd;
111 bd = container_of(desc, struct buf_data, hw_desc[0]);
113 if (err)
114 caam_jr_strstatus(jrdev, err);
116 atomic_set(&bd->empty, BUF_NOT_EMPTY);
117 complete(&bd->filled);
119 /* Buffer refilled, invalidate cache */
120 dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE);
122 print_hex_dump_debug("rng refreshed buf@: ", DUMP_PREFIX_ADDRESS, 16, 4,
123 bd->buf, RN_BUF_SIZE, 1);
126 static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
128 struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)];
129 struct device *jrdev = ctx->jrdev;
130 u32 *desc = bd->hw_desc;
131 int err;
133 dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf));
134 init_completion(&bd->filled);
135 err = caam_jr_enqueue(jrdev, desc, rng_done, ctx);
136 if (err)
137 complete(&bd->filled); /* don't wait on failed job*/
138 else
139 atomic_inc(&bd->empty); /* note if pending */
141 return err;
144 static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait)
146 struct caam_rng_ctx *ctx = rng_ctx;
147 struct buf_data *bd = &ctx->bufs[ctx->current_buf];
148 int next_buf_idx, copied_idx;
149 int err;
151 if (atomic_read(&bd->empty)) {
152 /* try to submit job if there wasn't one */
153 if (atomic_read(&bd->empty) == BUF_EMPTY) {
154 err = submit_job(ctx, 1);
155 /* if can't submit job, can't even wait */
156 if (err)
157 return 0;
159 /* no immediate data, so exit if not waiting */
160 if (!wait)
161 return 0;
163 /* waiting for pending job */
164 if (atomic_read(&bd->empty))
165 wait_for_completion(&bd->filled);
168 next_buf_idx = ctx->cur_buf_idx + max;
169 dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n",
170 __func__, ctx->current_buf, ctx->cur_buf_idx);
172 /* if enough data in current buffer */
173 if (next_buf_idx < RN_BUF_SIZE) {
174 memcpy(data, bd->buf + ctx->cur_buf_idx, max);
175 ctx->cur_buf_idx = next_buf_idx;
176 return max;
179 /* else, copy what's left... */
180 copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx;
181 memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx);
182 ctx->cur_buf_idx = 0;
183 atomic_set(&bd->empty, BUF_EMPTY);
185 /* ...refill... */
186 submit_job(ctx, 1);
188 /* and use next buffer */
189 ctx->current_buf = !ctx->current_buf;
190 dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
192 /* since there already is some data read, don't wait */
193 return copied_idx + caam_read(rng, data + copied_idx,
194 max - copied_idx, false);
197 static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx)
199 struct device *jrdev = ctx->jrdev;
200 u32 *desc = ctx->sh_desc;
202 init_sh_desc(desc, HDR_SHARE_SERIAL);
204 /* Generate random bytes */
205 append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG);
207 /* Store bytes */
208 append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE);
210 ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
211 DMA_TO_DEVICE);
212 if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) {
213 dev_err(jrdev, "unable to map shared descriptor\n");
214 return -ENOMEM;
217 print_hex_dump_debug("rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
218 desc, desc_bytes(desc), 1);
220 return 0;
223 static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id)
225 struct device *jrdev = ctx->jrdev;
226 struct buf_data *bd = &ctx->bufs[buf_id];
227 u32 *desc = bd->hw_desc;
228 int sh_len = desc_len(ctx->sh_desc);
230 init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER |
231 HDR_REVERSE);
233 bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE);
234 if (dma_mapping_error(jrdev, bd->addr)) {
235 dev_err(jrdev, "unable to map dst\n");
236 return -ENOMEM;
239 append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0);
241 print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4,
242 desc, desc_bytes(desc), 1);
244 return 0;
247 static void caam_cleanup(struct hwrng *rng)
249 int i;
250 struct buf_data *bd;
252 for (i = 0; i < 2; i++) {
253 bd = &rng_ctx->bufs[i];
254 if (atomic_read(&bd->empty) == BUF_PENDING)
255 wait_for_completion(&bd->filled);
258 rng_unmap_ctx(rng_ctx);
261 static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
263 struct buf_data *bd = &ctx->bufs[buf_id];
264 int err;
266 err = rng_create_job_desc(ctx, buf_id);
267 if (err)
268 return err;
270 atomic_set(&bd->empty, BUF_EMPTY);
271 submit_job(ctx, buf_id == ctx->current_buf);
272 wait_for_completion(&bd->filled);
274 return 0;
277 static int caam_init_rng(struct caam_rng_ctx *ctx, struct device *jrdev)
279 int err;
281 ctx->jrdev = jrdev;
283 err = rng_create_sh_desc(ctx);
284 if (err)
285 return err;
287 ctx->current_buf = 0;
288 ctx->cur_buf_idx = 0;
290 err = caam_init_buf(ctx, 0);
291 if (err)
292 return err;
294 return caam_init_buf(ctx, 1);
297 static struct hwrng caam_rng = {
298 .name = "rng-caam",
299 .cleanup = caam_cleanup,
300 .read = caam_read,
303 void caam_rng_exit(void)
305 if (!init_done)
306 return;
308 caam_jr_free(rng_ctx->jrdev);
309 hwrng_unregister(&caam_rng);
310 kfree(rng_ctx);
313 int caam_rng_init(struct device *ctrldev)
315 struct device *dev;
316 u32 rng_inst;
317 struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
318 int err;
319 init_done = false;
321 /* Check for an instantiated RNG before registration */
322 if (priv->era < 10)
323 rng_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) &
324 CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT;
325 else
326 rng_inst = rd_reg32(&priv->ctrl->vreg.rng) & CHA_VER_NUM_MASK;
328 if (!rng_inst)
329 return 0;
331 dev = caam_jr_alloc();
332 if (IS_ERR(dev)) {
333 pr_err("Job Ring Device allocation for transform failed\n");
334 return PTR_ERR(dev);
336 rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL);
337 if (!rng_ctx) {
338 err = -ENOMEM;
339 goto free_caam_alloc;
341 err = caam_init_rng(rng_ctx, dev);
342 if (err)
343 goto free_rng_ctx;
345 dev_info(dev, "registering rng-caam\n");
347 err = hwrng_register(&caam_rng);
348 if (!err) {
349 init_done = true;
350 return err;
353 free_rng_ctx:
354 kfree(rng_ctx);
355 free_caam_alloc:
356 caam_jr_free(dev);
357 return err;