]>
Commit | Line | Data |
---|---|---|
e24f7c9e YK |
1 | /* |
2 | * caam - Freescale FSL CAAM support for hw_random | |
3 | * | |
4 | * Copyright 2011 Freescale Semiconductor, Inc. | |
5 | * | |
6 | * Based on caamalg.c crypto API driver. | |
7 | * | |
8 | * relationship between job descriptors to shared descriptors: | |
9 | * | |
10 | * --------------- -------------- | |
11 | * | JobDesc #0 |-------------------->| ShareDesc | | |
12 | * | *(buffer 0) | |------------->| (generate) | | |
13 | * --------------- | | (move) | | |
14 | * | | (store) | | |
15 | * --------------- | -------------- | |
16 | * | JobDesc #1 |------| | |
17 | * | *(buffer 1) | | |
18 | * --------------- | |
19 | * | |
20 | * A job desc looks like this: | |
21 | * | |
22 | * --------------------- | |
23 | * | Header | | |
24 | * | ShareDesc Pointer | | |
25 | * | SEQ_OUT_PTR | | |
26 | * | (output buffer) | | |
27 | * --------------------- | |
28 | * | |
29 | * The SharedDesc never changes, and each job descriptor points to one of two | |
30 | * buffers for each device, from which the data will be copied into the | |
31 | * requested destination | |
32 | */ | |
33 | ||
34 | #include <linux/hw_random.h> | |
35 | #include <linux/completion.h> | |
36 | #include <linux/atomic.h> | |
37 | ||
38 | #include "compat.h" | |
39 | ||
40 | #include "regs.h" | |
41 | #include "intern.h" | |
42 | #include "desc_constr.h" | |
43 | #include "jr.h" | |
44 | #include "error.h" | |
45 | ||
46 | /* | |
47 | * Maximum buffer size: maximum number of random, cache-aligned bytes that | |
48 | * will be generated and moved to seq out ptr (extlen not allowed) | |
49 | */ | |
50 | #define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \ | |
51 | L1_CACHE_BYTES) | |
52 | ||
53 | /* length of descriptors */ | |
54 | #define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ * 2) | |
39957c8e | 55 | #define DESC_RNG_LEN (3 * CAAM_CMD_SZ) |
e24f7c9e YK |
56 | |
57 | /* Buffer, its dma address and lock */ | |
58 | struct buf_data { | |
412c98c1 | 59 | u8 buf[RN_BUF_SIZE] ____cacheline_aligned; |
e24f7c9e YK |
60 | dma_addr_t addr; |
61 | struct completion filled; | |
62 | u32 hw_desc[DESC_JOB_O_LEN]; | |
63 | #define BUF_NOT_EMPTY 0 | |
64 | #define BUF_EMPTY 1 | |
65 | #define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */ | |
66 | atomic_t empty; | |
67 | }; | |
68 | ||
69 | /* rng per-device context */ | |
70 | struct caam_rng_ctx { | |
71 | struct device *jrdev; | |
72 | dma_addr_t sh_desc_dma; | |
73 | u32 sh_desc[DESC_RNG_LEN]; | |
74 | unsigned int cur_buf_idx; | |
75 | int current_buf; | |
76 | struct buf_data bufs[2]; | |
77 | }; | |
78 | ||
6e4e603a | 79 | static struct caam_rng_ctx *rng_ctx; |
e24f7c9e YK |
80 | |
81 | static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd) | |
82 | { | |
83 | if (bd->addr) | |
84 | dma_unmap_single(jrdev, bd->addr, RN_BUF_SIZE, | |
85 | DMA_FROM_DEVICE); | |
86 | } | |
87 | ||
88 | static inline void rng_unmap_ctx(struct caam_rng_ctx *ctx) | |
89 | { | |
90 | struct device *jrdev = ctx->jrdev; | |
91 | ||
92 | if (ctx->sh_desc_dma) | |
4842234f YJ |
93 | dma_unmap_single(jrdev, ctx->sh_desc_dma, |
94 | desc_bytes(ctx->sh_desc), DMA_TO_DEVICE); | |
e24f7c9e YK |
95 | rng_unmap_buf(jrdev, &ctx->bufs[0]); |
96 | rng_unmap_buf(jrdev, &ctx->bufs[1]); | |
97 | } | |
98 | ||
99 | static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context) | |
100 | { | |
101 | struct buf_data *bd; | |
102 | ||
4ca7c7d8 | 103 | bd = container_of(desc, struct buf_data, hw_desc[0]); |
e24f7c9e | 104 | |
fa9659cd MV |
105 | if (err) |
106 | caam_jr_strstatus(jrdev, err); | |
e24f7c9e YK |
107 | |
108 | atomic_set(&bd->empty, BUF_NOT_EMPTY); | |
109 | complete(&bd->filled); | |
e7472422 VM |
110 | |
111 | /* Buffer refilled, invalidate cache */ | |
112 | dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE); | |
113 | ||
e24f7c9e YK |
114 | #ifdef DEBUG |
115 | print_hex_dump(KERN_ERR, "rng refreshed buf@: ", | |
116 | DUMP_PREFIX_ADDRESS, 16, 4, bd->buf, RN_BUF_SIZE, 1); | |
117 | #endif | |
118 | } | |
119 | ||
120 | static inline int submit_job(struct caam_rng_ctx *ctx, int to_current) | |
121 | { | |
122 | struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)]; | |
123 | struct device *jrdev = ctx->jrdev; | |
124 | u32 *desc = bd->hw_desc; | |
125 | int err; | |
126 | ||
127 | dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf)); | |
128 | init_completion(&bd->filled); | |
129 | err = caam_jr_enqueue(jrdev, desc, rng_done, ctx); | |
130 | if (err) | |
131 | complete(&bd->filled); /* don't wait on failed job*/ | |
132 | else | |
133 | atomic_inc(&bd->empty); /* note if pending */ | |
134 | ||
135 | return err; | |
136 | } | |
137 | ||
138 | static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait) | |
139 | { | |
6e4e603a | 140 | struct caam_rng_ctx *ctx = rng_ctx; |
e24f7c9e YK |
141 | struct buf_data *bd = &ctx->bufs[ctx->current_buf]; |
142 | int next_buf_idx, copied_idx; | |
143 | int err; | |
144 | ||
145 | if (atomic_read(&bd->empty)) { | |
146 | /* try to submit job if there wasn't one */ | |
147 | if (atomic_read(&bd->empty) == BUF_EMPTY) { | |
148 | err = submit_job(ctx, 1); | |
149 | /* if can't submit job, can't even wait */ | |
150 | if (err) | |
151 | return 0; | |
152 | } | |
153 | /* no immediate data, so exit if not waiting */ | |
154 | if (!wait) | |
155 | return 0; | |
156 | ||
157 | /* waiting for pending job */ | |
158 | if (atomic_read(&bd->empty)) | |
159 | wait_for_completion(&bd->filled); | |
160 | } | |
161 | ||
162 | next_buf_idx = ctx->cur_buf_idx + max; | |
163 | dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n", | |
164 | __func__, ctx->current_buf, ctx->cur_buf_idx); | |
165 | ||
166 | /* if enough data in current buffer */ | |
167 | if (next_buf_idx < RN_BUF_SIZE) { | |
168 | memcpy(data, bd->buf + ctx->cur_buf_idx, max); | |
169 | ctx->cur_buf_idx = next_buf_idx; | |
170 | return max; | |
171 | } | |
172 | ||
173 | /* else, copy what's left... */ | |
174 | copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx; | |
175 | memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx); | |
176 | ctx->cur_buf_idx = 0; | |
177 | atomic_set(&bd->empty, BUF_EMPTY); | |
178 | ||
179 | /* ...refill... */ | |
180 | submit_job(ctx, 1); | |
181 | ||
182 | /* and use next buffer */ | |
183 | ctx->current_buf = !ctx->current_buf; | |
184 | dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf); | |
185 | ||
186 | /* since there already is some data read, don't wait */ | |
187 | return copied_idx + caam_read(rng, data + copied_idx, | |
188 | max - copied_idx, false); | |
189 | } | |
190 | ||
ce572085 | 191 | static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx) |
e24f7c9e YK |
192 | { |
193 | struct device *jrdev = ctx->jrdev; | |
194 | u32 *desc = ctx->sh_desc; | |
195 | ||
61bb86bb | 196 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
e24f7c9e | 197 | |
e24f7c9e YK |
198 | /* Generate random bytes */ |
199 | append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG); | |
200 | ||
201 | /* Store bytes */ | |
202 | append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE); | |
203 | ||
204 | ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc), | |
205 | DMA_TO_DEVICE); | |
ce572085 HG |
206 | if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) { |
207 | dev_err(jrdev, "unable to map shared descriptor\n"); | |
208 | return -ENOMEM; | |
209 | } | |
e24f7c9e YK |
210 | #ifdef DEBUG |
211 | print_hex_dump(KERN_ERR, "rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4, | |
212 | desc, desc_bytes(desc), 1); | |
213 | #endif | |
ce572085 | 214 | return 0; |
e24f7c9e YK |
215 | } |
216 | ||
ce572085 | 217 | static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id) |
e24f7c9e YK |
218 | { |
219 | struct device *jrdev = ctx->jrdev; | |
220 | struct buf_data *bd = &ctx->bufs[buf_id]; | |
221 | u32 *desc = bd->hw_desc; | |
222 | int sh_len = desc_len(ctx->sh_desc); | |
223 | ||
224 | init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER | | |
225 | HDR_REVERSE); | |
226 | ||
227 | bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE); | |
ce572085 HG |
228 | if (dma_mapping_error(jrdev, bd->addr)) { |
229 | dev_err(jrdev, "unable to map dst\n"); | |
230 | return -ENOMEM; | |
231 | } | |
e24f7c9e YK |
232 | |
233 | append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0); | |
234 | #ifdef DEBUG | |
235 | print_hex_dump(KERN_ERR, "rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4, | |
236 | desc, desc_bytes(desc), 1); | |
237 | #endif | |
ce572085 | 238 | return 0; |
e24f7c9e YK |
239 | } |
240 | ||
241 | static void caam_cleanup(struct hwrng *rng) | |
242 | { | |
243 | int i; | |
244 | struct buf_data *bd; | |
245 | ||
246 | for (i = 0; i < 2; i++) { | |
6e4e603a | 247 | bd = &rng_ctx->bufs[i]; |
e24f7c9e YK |
248 | if (atomic_read(&bd->empty) == BUF_PENDING) |
249 | wait_for_completion(&bd->filled); | |
250 | } | |
251 | ||
6e4e603a | 252 | rng_unmap_ctx(rng_ctx); |
e24f7c9e YK |
253 | } |
254 | ||
ce572085 | 255 | static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) |
e24f7c9e YK |
256 | { |
257 | struct buf_data *bd = &ctx->bufs[buf_id]; | |
ce572085 HG |
258 | int err; |
259 | ||
260 | err = rng_create_job_desc(ctx, buf_id); | |
261 | if (err) | |
262 | return err; | |
e24f7c9e | 263 | |
e24f7c9e YK |
264 | atomic_set(&bd->empty, BUF_EMPTY); |
265 | submit_job(ctx, buf_id == ctx->current_buf); | |
266 | wait_for_completion(&bd->filled); | |
ce572085 HG |
267 | |
268 | return 0; | |
e24f7c9e YK |
269 | } |
270 | ||
ce572085 | 271 | static int caam_init_rng(struct caam_rng_ctx *ctx, struct device *jrdev) |
e24f7c9e | 272 | { |
ce572085 HG |
273 | int err; |
274 | ||
e24f7c9e | 275 | ctx->jrdev = jrdev; |
ce572085 HG |
276 | |
277 | err = rng_create_sh_desc(ctx); | |
278 | if (err) | |
279 | return err; | |
280 | ||
e24f7c9e YK |
281 | ctx->current_buf = 0; |
282 | ctx->cur_buf_idx = 0; | |
ce572085 HG |
283 | |
284 | err = caam_init_buf(ctx, 0); | |
285 | if (err) | |
286 | return err; | |
287 | ||
288 | err = caam_init_buf(ctx, 1); | |
289 | if (err) | |
290 | return err; | |
291 | ||
292 | return 0; | |
e24f7c9e YK |
293 | } |
294 | ||
295 | static struct hwrng caam_rng = { | |
296 | .name = "rng-caam", | |
297 | .cleanup = caam_cleanup, | |
298 | .read = caam_read, | |
299 | }; | |
300 | ||
301 | static void __exit caam_rng_exit(void) | |
302 | { | |
6e4e603a | 303 | caam_jr_free(rng_ctx->jrdev); |
e24f7c9e | 304 | hwrng_unregister(&caam_rng); |
6e4e603a | 305 | kfree(rng_ctx); |
e24f7c9e YK |
306 | } |
307 | ||
308 | static int __init caam_rng_init(void) | |
309 | { | |
cfc6f11b | 310 | struct device *dev; |
35af6403 RG |
311 | struct device_node *dev_node; |
312 | struct platform_device *pdev; | |
313 | struct device *ctrldev; | |
bf83490e | 314 | struct caam_drv_private *priv; |
ce572085 | 315 | int err; |
35af6403 RG |
316 | |
317 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); | |
318 | if (!dev_node) { | |
319 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0"); | |
320 | if (!dev_node) | |
321 | return -ENODEV; | |
322 | } | |
323 | ||
324 | pdev = of_find_device_by_node(dev_node); | |
325 | if (!pdev) { | |
326 | of_node_put(dev_node); | |
327 | return -ENODEV; | |
328 | } | |
329 | ||
330 | ctrldev = &pdev->dev; | |
331 | priv = dev_get_drvdata(ctrldev); | |
332 | of_node_put(dev_node); | |
333 | ||
334 | /* | |
335 | * If priv is NULL, it's probably because the caam driver wasn't | |
336 | * properly initialized (e.g. RNG4 init failed). Thus, bail out here. | |
337 | */ | |
338 | if (!priv) | |
339 | return -ENODEV; | |
e24f7c9e | 340 | |
bf83490e VM |
341 | /* Check for an instantiated RNG before registration */ |
342 | if (!(rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & CHA_ID_LS_RNG_MASK)) | |
343 | return -ENODEV; | |
344 | ||
cfc6f11b RG |
345 | dev = caam_jr_alloc(); |
346 | if (IS_ERR(dev)) { | |
347 | pr_err("Job Ring Device allocation for transform failed\n"); | |
348 | return PTR_ERR(dev); | |
349 | } | |
c530e341 | 350 | rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL); |
ac8ad307 FE |
351 | if (!rng_ctx) { |
352 | err = -ENOMEM; | |
353 | goto free_caam_alloc; | |
354 | } | |
ce572085 HG |
355 | err = caam_init_rng(rng_ctx, dev); |
356 | if (err) | |
ac8ad307 | 357 | goto free_rng_ctx; |
e24f7c9e | 358 | |
cfc6f11b | 359 | dev_info(dev, "registering rng-caam\n"); |
e24f7c9e | 360 | return hwrng_register(&caam_rng); |
ac8ad307 FE |
361 | |
362 | free_rng_ctx: | |
363 | kfree(rng_ctx); | |
364 | free_caam_alloc: | |
365 | caam_jr_free(dev); | |
366 | return err; | |
e24f7c9e YK |
367 | } |
368 | ||
369 | module_init(caam_rng_init); | |
370 | module_exit(caam_rng_exit); | |
371 | ||
372 | MODULE_LICENSE("GPL"); | |
373 | MODULE_DESCRIPTION("FSL CAAM support for hw_random API"); | |
374 | MODULE_AUTHOR("Freescale Semiconductor - NMG"); |