sun8i_crypto.c revision 1.4 1 /* $NetBSD: sun8i_crypto.c,v 1.4 2019/12/09 14:56:18 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * sun8i_crypto -- Allwinner Crypto Engine driver
34 *
35 * The Crypto Engine is documented in Sec. 3.15 of the Allwinner A64
36 * User Manual v1.1, on pp. 230--241. We only use it for the TRNG at
37 * the moment, but in principle it could be wired up with opencrypto(9)
38 * to compute AES, DES, 3DES, MD5, SHA-1, SHA-224, SHA-256, HMAC-SHA1,
39 * HMAC-HA256, RSA, and an undocumented PRNG. It also seems to support
40 * AES keys in SRAM (for some kind of HDMI HDCP stuff?).
41 *
42 * https://linux-sunxi.org/images/b/b4/Allwinner_A64_User_Manual_V1.1.pdf
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(1, "$NetBSD: sun8i_crypto.c,v 1.4 2019/12/09 14:56:18 riastradh Exp $");
47
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/atomic.h>
51 #include <sys/bus.h>
52 #include <sys/callout.h>
53 #include <sys/conf.h>
54 #include <sys/device.h>
55 #include <sys/kernel.h>
56 #include <sys/kmem.h>
57 #include <sys/mutex.h>
58 #include <sys/rndpool.h>
59 #include <sys/rndsource.h>
60 #include <sys/sysctl.h>
61 #include <sys/workqueue.h>
62
63 #include <dev/fdt/fdtvar.h>
64
65 #include <arm/sunxi/sun8i_crypto.h>
66
67 #define SUN8I_CRYPTO_TIMEOUT hz
68 #define SUN8I_CRYPTO_RNGENTROPY 8 /* estimated bits per bit of entropy */
69 #define SUN8I_CRYPTO_RNGBYTES \
70 (SUN8I_CRYPTO_RNGENTROPY*howmany(RND_POOLBITS, NBBY))
71
72 struct sun8i_crypto_task;
73
74 struct sun8i_crypto_buf {
75 bus_dma_segment_t cb_seg[1];
76 int cb_nsegs;
77 bus_dmamap_t cb_map;
78 void *cb_kva;
79 };
80
81 struct sun8i_crypto_softc {
82 device_t sc_dev;
83 bus_space_tag_t sc_bst;
84 bus_space_handle_t sc_bsh;
85 bus_dma_tag_t sc_dmat;
86 kmutex_t sc_lock;
87 struct sun8i_crypto_chan {
88 struct sun8i_crypto_task *cc_task;
89 unsigned cc_starttime;
90 } sc_chan[SUN8I_CRYPTO_NCHAN];
91 struct callout sc_timeout;
92 struct workqueue *sc_wq;
93 struct work sc_work;
94 void *sc_ih;
95 uint32_t sc_done;
96 uint32_t sc_esr;
97 bool sc_work_pending;
98 struct sun8i_crypto_rng {
99 struct sun8i_crypto_buf cr_buf;
100 struct sun8i_crypto_task *cr_task;
101 struct krndsource cr_rndsource;
102 bool cr_pending;
103 } sc_rng;
104 struct sun8i_crypto_selftest {
105 struct sun8i_crypto_buf cs_in;
106 struct sun8i_crypto_buf cs_key;
107 struct sun8i_crypto_buf cs_out;
108 struct sun8i_crypto_task *cs_task;
109 } sc_selftest;
110 struct sun8i_crypto_sysctl {
111 struct sysctllog *cy_log;
112 const struct sysctlnode *cy_root_node;
113 const struct sysctlnode *cy_trng_node;
114 } sc_sysctl;
115 };
116
117 struct sun8i_crypto_task {
118 struct sun8i_crypto_buf ct_buf;
119 struct sun8i_crypto_taskdesc *ct_desc;
120 void (*ct_callback)(struct sun8i_crypto_softc *,
121 struct sun8i_crypto_task *, void *, int);
122 void *ct_cookie;
123 };
124
125 /*
126 * Forward declarations
127 */
128
129 static int sun8i_crypto_match(device_t, cfdata_t, void *);
130 static void sun8i_crypto_attach(device_t, device_t, void *);
131
132 static struct sun8i_crypto_task *
133 sun8i_crypto_task_get(struct sun8i_crypto_softc *,
134 void (*)(struct sun8i_crypto_softc *,
135 struct sun8i_crypto_task *, void *, int),
136 void *);
137 static void sun8i_crypto_task_put(struct sun8i_crypto_softc *,
138 struct sun8i_crypto_task *);
139 static void sun8i_crypto_task_reset(struct sun8i_crypto_task *);
140
141 static void sun8i_crypto_task_set_key(struct sun8i_crypto_task *,
142 bus_dmamap_t);
143 static void sun8i_crypto_task_set_iv(struct sun8i_crypto_task *,
144 bus_dmamap_t);
145 static void sun8i_crypto_task_set_ctr(struct sun8i_crypto_task *,
146 bus_dmamap_t);
147 static void sun8i_crypto_task_set_input(struct sun8i_crypto_task *,
148 bus_dmamap_t);
149 static void sun8i_crypto_task_set_output(struct sun8i_crypto_task *,
150 bus_dmamap_t);
151
152 static void sun8i_crypto_task_scatter(struct sun8i_crypto_adrlen *,
153 bus_dmamap_t);
154
155 static int sun8i_crypto_submit_trng(struct sun8i_crypto_softc *,
156 struct sun8i_crypto_task *, uint32_t);
157 static int sun8i_crypto_submit_aesecb(struct sun8i_crypto_softc *,
158 struct sun8i_crypto_task *, uint32_t, uint32_t, uint32_t);
159 static int sun8i_crypto_submit(struct sun8i_crypto_softc *,
160 struct sun8i_crypto_task *);
161
162 static void sun8i_crypto_timeout(void *);
163 static int sun8i_crypto_intr(void *);
164 static void sun8i_crypto_schedule_worker(struct sun8i_crypto_softc *);
165 static void sun8i_crypto_worker(struct work *, void *);
166 static void sun8i_crypto_chan_done(struct sun8i_crypto_softc *, unsigned,
167 int);
168
169 static int sun8i_crypto_allocbuf(struct sun8i_crypto_softc *, size_t,
170 struct sun8i_crypto_buf *);
171 static void sun8i_crypto_freebuf(struct sun8i_crypto_softc *, size_t,
172 struct sun8i_crypto_buf *);
173
174 static void sun8i_crypto_rng_attach(struct sun8i_crypto_softc *);
175 static void sun8i_crypto_rng_get(size_t, void *);
176 static void sun8i_crypto_rng_done(struct sun8i_crypto_softc *,
177 struct sun8i_crypto_task *, void *, int);
178
179 static void sun8i_crypto_selftest(device_t);
180 static void sun8i_crypto_selftest_done(struct sun8i_crypto_softc *,
181 struct sun8i_crypto_task *, void *, int);
182
183 static void sun8i_crypto_sysctl_attach(struct sun8i_crypto_softc *);
184 static int sun8i_crypto_sysctl_rng(SYSCTLFN_ARGS);
185 static void sun8i_crypto_sysctl_rng_done(struct sun8i_crypto_softc *,
186 struct sun8i_crypto_task *, void *, int);
187
188 /*
189 * Register access
190 */
191
192 static uint32_t
193 sun8i_crypto_read(struct sun8i_crypto_softc *sc, bus_addr_t reg)
194 {
195 return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
196 }
197
198 static void
199 sun8i_crypto_write(struct sun8i_crypto_softc *sc, bus_addr_t reg, uint32_t v)
200 {
201 bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, v);
202 }
203
204 /*
205 * Autoconf goo
206 */
207
208 CFATTACH_DECL_NEW(sun8i_crypto, sizeof(struct sun8i_crypto_softc),
209 sun8i_crypto_match, sun8i_crypto_attach, NULL, NULL);
210
211 static const struct of_compat_data compat_data[] = {
212 {"allwinner,sun50i-a64-crypto", 0},
213 {NULL}
214 };
215
216 static int
217 sun8i_crypto_match(device_t parent, cfdata_t cf, void *aux)
218 {
219 const struct fdt_attach_args *const faa = aux;
220
221 return of_match_compat_data(faa->faa_phandle, compat_data);
222 }
223
224 static void
225 sun8i_crypto_attach(device_t parent, device_t self, void *aux)
226 {
227 struct sun8i_crypto_softc *const sc = device_private(self);
228 const struct fdt_attach_args *const faa = aux;
229 bus_addr_t addr;
230 bus_size_t size;
231 const int phandle = faa->faa_phandle;
232 char intrstr[128];
233 struct clk *clk;
234 struct fdtbus_reset *rst;
235
236 sc->sc_dev = self;
237 sc->sc_dmat = faa->faa_dmat;
238 sc->sc_bst = faa->faa_bst;
239 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
240 callout_init(&sc->sc_timeout, CALLOUT_MPSAFE);
241 callout_setfunc(&sc->sc_timeout, &sun8i_crypto_timeout, sc);
242 if (workqueue_create(&sc->sc_wq, device_xname(self),
243 &sun8i_crypto_worker, sc, PRI_NONE, IPL_VM, WQ_MPSAFE) != 0) {
244 aprint_error(": couldn't create workqueue\n");
245 return;
246 }
247
248 /* Get and map device registers. */
249 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
250 aprint_error(": couldn't get registers\n");
251 return;
252 }
253 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
254 aprint_error(": couldn't map registers\n");
255 return;
256 }
257
258 /* Get an interrupt handle. */
259 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
260 aprint_error(": failed to decode interrupt\n");
261 return;
262 }
263
264 /* Enable the bus clock. */
265 if (fdtbus_clock_enable(phandle, "bus", true) != 0) {
266 aprint_error(": couldn't enable bus clock\n");
267 return;
268 }
269
270 /* Get the module clock and set it to 300 MHz. */
271 if ((clk = fdtbus_clock_get(phandle, "mod")) != NULL) {
272 if (clk_enable(clk) != 0) {
273 aprint_error(": couldn't enable CE clock\n");
274 return;
275 }
276 if (clk_set_rate(clk, 300*1000*1000) != 0) {
277 aprint_error(": couldn't set CE clock to 300MHz\n");
278 return;
279 }
280 }
281
282 /* Get a reset handle if we need and try to deassert it. */
283 if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) {
284 if (fdtbus_reset_deassert(rst) != 0) {
285 aprint_error(": couldn't de-assert reset\n");
286 return;
287 }
288 }
289
290 aprint_naive("\n");
291 aprint_normal(": Crypto Engine\n");
292 aprint_debug_dev(self, ": clock freq %d\n", clk_get_rate(clk));
293
294 /* Disable and clear interrupts. */
295 sun8i_crypto_write(sc, SUN8I_CRYPTO_ICR, 0);
296 sun8i_crypto_write(sc, SUN8I_CRYPTO_ISR, 0);
297
298 /* Establish an interrupt handler. */
299 sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
300 &sun8i_crypto_intr, sc);
301 if (sc->sc_ih == NULL) {
302 aprint_error_dev(self, "failed to establish interrupt on %s\n",
303 intrstr);
304 return;
305 }
306 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
307
308 /* Set up the RNG. */
309 sun8i_crypto_rng_attach(sc);
310
311 /* Attach the sysctl. */
312 sun8i_crypto_sysctl_attach(sc);
313
314 /* Perform self-tests. */
315 config_interrupts(self, sun8i_crypto_selftest);
316 }
317
318 /*
319 * Task allocation
320 */
321
322 static struct sun8i_crypto_task *
323 sun8i_crypto_task_get(struct sun8i_crypto_softc *sc,
324 void (*callback)(struct sun8i_crypto_softc *, struct sun8i_crypto_task *,
325 void *, int),
326 void *cookie)
327 {
328 struct sun8i_crypto_task *task;
329 int error;
330
331 /* Allocate a task. */
332 task = kmem_zalloc(sizeof(*task), KM_SLEEP);
333
334 /* Allocate a buffer for the descriptor. */
335 error = sun8i_crypto_allocbuf(sc, sizeof(*task->ct_desc),
336 &task->ct_buf);
337 if (error)
338 goto fail0;
339
340 /* Initialize the task object and return it. */
341 task->ct_desc = task->ct_buf.cb_kva;
342 task->ct_callback = callback;
343 task->ct_cookie = cookie;
344 return task;
345
346 fail1: __unused
347 sun8i_crypto_freebuf(sc, sizeof(*task->ct_desc), &task->ct_buf);
348 fail0: kmem_free(task, sizeof(*task));
349 return NULL;
350 }
351
352 static void
353 sun8i_crypto_task_put(struct sun8i_crypto_softc *sc,
354 struct sun8i_crypto_task *task)
355 {
356
357 sun8i_crypto_freebuf(sc, sizeof(*task->ct_desc), &task->ct_buf);
358 kmem_free(task, sizeof(*task));
359 }
360
361 /*
362 * Task descriptor setup
363 *
364 * WARNING: Task descriptor fields are little-endian, not host-endian.
365 */
366
367 static void
368 sun8i_crypto_task_reset(struct sun8i_crypto_task *task)
369 {
370
371 memset(task->ct_desc, 0, sizeof(*task->ct_desc));
372 }
373
374 static void
375 sun8i_crypto_task_set_key(struct sun8i_crypto_task *task, bus_dmamap_t map)
376 {
377
378 KASSERT(map->dm_nsegs == 1);
379 task->ct_desc->td_keydesc = htole32(map->dm_segs[0].ds_addr);
380 }
381
382 static void __unused /* XXX opencrypto(9) */
383 sun8i_crypto_task_set_iv(struct sun8i_crypto_task *task, bus_dmamap_t map)
384 {
385
386 KASSERT(map->dm_nsegs == 1);
387 task->ct_desc->td_ivdesc = htole32(map->dm_segs[0].ds_addr);
388 }
389
390 static void __unused /* XXX opencrypto(9) */
391 sun8i_crypto_task_set_ctr(struct sun8i_crypto_task *task, bus_dmamap_t map)
392 {
393
394 KASSERT(map->dm_nsegs == 1);
395 task->ct_desc->td_ctrdesc = htole32(map->dm_segs[0].ds_addr);
396 }
397
398 static void
399 sun8i_crypto_task_set_input(struct sun8i_crypto_task *task, bus_dmamap_t map)
400 {
401
402 sun8i_crypto_task_scatter(task->ct_desc->td_src, map);
403 }
404
405 static void
406 sun8i_crypto_task_set_output(struct sun8i_crypto_task *task, bus_dmamap_t map)
407 {
408
409 sun8i_crypto_task_scatter(task->ct_desc->td_dst, map);
410 }
411
412 static void
413 sun8i_crypto_task_scatter(struct sun8i_crypto_adrlen *adrlen, bus_dmamap_t map)
414 {
415 uint32_t total __diagused = 0;
416 unsigned i;
417
418 KASSERT(map->dm_nsegs <= SUN8I_CRYPTO_MAXSEGS);
419 for (i = 0; i < map->dm_nsegs; i++) {
420 KASSERT((map->dm_segs[i].ds_addr % 4) == 0);
421 KASSERT(map->dm_segs[i].ds_addr <= UINT32_MAX);
422 KASSERT(map->dm_segs[i].ds_len <= UINT32_MAX - total);
423 adrlen[i].adr = htole32(map->dm_segs[i].ds_addr);
424 adrlen[i].len = htole32(map->dm_segs[i].ds_len/4);
425 total += map->dm_segs[i].ds_len;
426 }
427
428 /* Verify the remainder are zero. */
429 for (; i < SUN8I_CRYPTO_MAXSEGS; i++) {
430 KASSERT(adrlen[i].adr == 0);
431 KASSERT(adrlen[i].len == 0);
432 }
433
434 /* Verify the total size matches the DMA map. */
435 KASSERT(total == map->dm_mapsize);
436 }
437
438 /*
439 * Task submission
440 *
441 * WARNING: Task descriptor fields are little-endian, not host-endian.
442 */
443
444 static int
445 sun8i_crypto_submit_trng(struct sun8i_crypto_softc *sc,
446 struct sun8i_crypto_task *task, uint32_t datalen)
447 {
448 struct sun8i_crypto_taskdesc *desc = task->ct_desc;
449 uint32_t tdqc = 0;
450 uint32_t total __diagused;
451 unsigned i __diagused;
452
453 /* Data length must be a multiple of 4 because...reasons. */
454 KASSERT((datalen % 4) == 0);
455
456 /* All of the sources should be empty. */
457 for (total = 0, i = 0; i < SUN8I_CRYPTO_MAXSEGS; i++)
458 KASSERT(le32toh(task->ct_desc->td_src[i].len) == 0);
459
460 /* Verify the total output length -- should be datalen/4. */
461 for (total = 0, i = 0; i < SUN8I_CRYPTO_MAXSEGS; i++) {
462 uint32_t len = le32toh(task->ct_desc->td_dst[i].len);
463 KASSERT(len <= UINT32_MAX - total);
464 total += len;
465 }
466 KASSERT(total == datalen/4);
467
468 /* Verify the key, IV, and CTR are unset. */
469 KASSERT(desc->td_keydesc == 0);
470 KASSERT(desc->td_ivdesc == 0);
471 KASSERT(desc->td_ctrdesc == 0);
472
473 /* Set up the task descriptor queue control words. */
474 tdqc |= SUN8I_CRYPTO_TDQC_INTR_EN;
475 tdqc |= __SHIFTIN(SUN8I_CRYPTO_TDQC_METHOD_TRNG,
476 SUN8I_CRYPTO_TDQC_METHOD);
477 desc->td_tdqc = htole32(tdqc);
478 desc->td_tdqs = 0; /* no symmetric crypto */
479 desc->td_tdqa = 0; /* no asymmetric crypto */
480
481 /* Set the data length for the output. */
482 desc->td_datalen = htole32(datalen/4);
483
484 /* Submit! */
485 return sun8i_crypto_submit(sc, task);
486 }
487
488 static int
489 sun8i_crypto_submit_aesecb(struct sun8i_crypto_softc *sc,
490 struct sun8i_crypto_task *task,
491 uint32_t datalen, uint32_t keysize, uint32_t dir)
492 {
493 struct sun8i_crypto_taskdesc *desc = task->ct_desc;
494 uint32_t tdqc = 0, tdqs = 0;
495 uint32_t total __diagused;
496 unsigned i __diagused;
497
498 /*
499 * Data length must be a multiple of 4 because...reasons.
500 *
501 * WARNING: For `AES-CTS' (maybe that means AES-XTS?), datalen
502 * is in units of bytes, not units of words -- but everything
503 * _else_ is in units of words. This routine applies only to
504 * AES-ECB for the self-test.
505 */
506 KASSERT((datalen % 4) == 0);
507
508 /* Verify the total input length -- should be datalen/4. */
509 for (total = 0, i = 0; i < SUN8I_CRYPTO_MAXSEGS; i++) {
510 uint32_t len = le32toh(task->ct_desc->td_src[i].len);
511 KASSERT(len <= UINT32_MAX - total);
512 total += len;
513 }
514 KASSERT(total == datalen/4);
515
516 /* Verify the total output length -- should be datalen/4. */
517 for (total = 0, i = 0; i < SUN8I_CRYPTO_MAXSEGS; i++) {
518 uint32_t len = le32toh(task->ct_desc->td_dst[i].len);
519 KASSERT(len <= UINT32_MAX - total);
520 total += len;
521 }
522 KASSERT(total == datalen/4);
523
524 /* Set up the task descriptor queue control word. */
525 tdqc |= SUN8I_CRYPTO_TDQC_INTR_EN;
526 tdqc |= __SHIFTIN(SUN8I_CRYPTO_TDQC_METHOD_AES,
527 SUN8I_CRYPTO_TDQC_METHOD);
528 desc->td_tdqc = htole32(tdqc);
529
530 /* Set up the symmetric control word. */
531 tdqs |= __SHIFTIN(SUN8I_CRYPTO_TDQS_SKEY_SELECT_SS_KEYx,
532 SUN8I_CRYPTO_TDQS_SKEY_SELECT);
533 tdqs |= __SHIFTIN(SUN8I_CRYPTO_TDQS_OP_MODE_ECB,
534 SUN8I_CRYPTO_TDQS_OP_MODE);
535 tdqs |= __SHIFTIN(SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128,
536 SUN8I_CRYPTO_TDQS_AES_KEYSIZE);
537 desc->td_tdqs = htole32(tdqs);
538
539 desc->td_tdqa = 0; /* no asymmetric crypto */
540
541 /* Set the data length for the output. */
542 desc->td_datalen = htole32(datalen/4);
543
544 /* Submit! */
545 return sun8i_crypto_submit(sc, task);
546 }
547
548 static int
549 sun8i_crypto_submit(struct sun8i_crypto_softc *sc,
550 struct sun8i_crypto_task *task)
551 {
552 unsigned i, retries = 0;
553 uint32_t icr;
554 int error = 0;
555
556 /* One at a time at the device registers, please. */
557 mutex_enter(&sc->sc_lock);
558
559 /* Find a channel. */
560 for (i = 0; i < SUN8I_CRYPTO_NCHAN; i++) {
561 if (sc->sc_chan[i].cc_task == NULL)
562 break;
563 }
564 if (i == SUN8I_CRYPTO_NCHAN) {
565 device_printf(sc->sc_dev, "no free channels\n");
566 error = ERESTART;
567 goto out;
568 }
569
570 /*
571 * Set the channel id. Caller is responsible for setting up
572 * all other parts of the descriptor.
573 */
574 task->ct_desc->td_cid = htole32(i);
575
576 /* Prepare to send the descriptor to the device by DMA. */
577 bus_dmamap_sync(sc->sc_dmat, task->ct_buf.cb_map, 0,
578 sizeof(*task->ct_desc), BUS_DMASYNC_PREWRITE);
579
580 /* Confirm we're ready to go. */
581 if (sun8i_crypto_read(sc, SUN8I_CRYPTO_TLR) & SUN8I_CRYPTO_TLR_LOAD) {
582 device_printf(sc->sc_dev, "TLR not clear\n");
583 error = EIO;
584 goto out;
585 }
586
587 /* Enable interrupts for this channel. */
588 icr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ICR);
589 icr |= __SHIFTIN(SUN8I_CRYPTO_ICR_INTR_EN_CHAN(i),
590 SUN8I_CRYPTO_ICR_INTR_EN);
591 sun8i_crypto_write(sc, SUN8I_CRYPTO_ICR, icr);
592
593 /* Set the task descriptor queue address. */
594 sun8i_crypto_write(sc, SUN8I_CRYPTO_TDQ,
595 task->ct_buf.cb_map->dm_segs[0].ds_addr);
596
597 /* Notify the engine to load it, and wait for acknowledgement. */
598 sun8i_crypto_write(sc, SUN8I_CRYPTO_TLR, SUN8I_CRYPTO_TLR_LOAD);
599 while (sun8i_crypto_read(sc, SUN8I_CRYPTO_TLR) & SUN8I_CRYPTO_TLR_LOAD)
600 {
601 /*
602 * XXX Timeout pulled from arse. Is it even important
603 * to wait here?
604 */
605 if (++retries == 1000) {
606 device_printf(sc->sc_dev, "TLR didn't clear: %08x\n",
607 sun8i_crypto_read(sc, SUN8I_CRYPTO_TLR));
608 /*
609 * Hope it clears eventually; if not, we'll
610 * time out.
611 */
612 break;
613 }
614 DELAY(1);
615 }
616
617 /* Loaded up and ready to go. Start a timer ticking. */
618 sc->sc_chan[i].cc_task = task;
619 sc->sc_chan[i].cc_starttime = atomic_load_relaxed(&hardclock_ticks);
620 callout_schedule(&sc->sc_timeout, SUN8I_CRYPTO_TIMEOUT);
621
622 /* XXX Consider polling if cold to get entropy earlier. */
623
624 out: /* Done! */
625 mutex_exit(&sc->sc_lock);
626 return error;
627 }
628
629 static void
630 sun8i_crypto_timeout(void *cookie)
631 {
632 struct sun8i_crypto_softc *sc = cookie;
633 unsigned i;
634
635 mutex_enter(&sc->sc_lock);
636
637 /* Check whether there are any tasks pending. */
638 for (i = 0; i < SUN8I_CRYPTO_NCHAN; i++) {
639 if (sc->sc_chan[i].cc_task)
640 break;
641 }
642 if (i == SUN8I_CRYPTO_NCHAN)
643 /* None pending, so nothing to do. */
644 goto out;
645
646 /*
647 * Schedule the worker to check for timeouts, and schedule
648 * another timeout in case we need it.
649 */
650 sun8i_crypto_schedule_worker(sc);
651 callout_schedule(&sc->sc_timeout, SUN8I_CRYPTO_TIMEOUT);
652
653 out: mutex_exit(&sc->sc_lock);
654 }
655
656 static int
657 sun8i_crypto_intr(void *cookie)
658 {
659 struct sun8i_crypto_softc *sc = cookie;
660 uint32_t isr, esr;
661
662 mutex_enter(&sc->sc_lock);
663
664 /*
665 * Get and acknowledge the interrupts and error status.
666 *
667 * XXX Data sheet says the error status register is read-only,
668 * but then advises writing 1 to bit x1xx (keysram access error
669 * for AES, SUN8I_CRYPTO_ESR_KEYSRAMERR) to clear it. What do?
670 */
671 isr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ISR);
672 esr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ESR);
673 sun8i_crypto_write(sc, SUN8I_CRYPTO_ISR, isr);
674 sun8i_crypto_write(sc, SUN8I_CRYPTO_ISR, esr);
675
676 /* Start the worker if necessary. */
677 sun8i_crypto_schedule_worker(sc);
678
679 /* Tell the worker what to do. */
680 sc->sc_done |= __SHIFTOUT(isr, SUN8I_CRYPTO_ISR_DONE);
681 sc->sc_esr |= esr;
682
683 mutex_exit(&sc->sc_lock);
684
685 return __SHIFTOUT(isr, SUN8I_CRYPTO_ISR_DONE) != 0;
686 }
687
688 static void
689 sun8i_crypto_schedule_worker(struct sun8i_crypto_softc *sc)
690 {
691
692 KASSERT(mutex_owned(&sc->sc_lock));
693
694 /* Start the worker if necessary. */
695 if (!sc->sc_work_pending) {
696 workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL);
697 sc->sc_work_pending = true;
698 }
699 }
700
701 static void
702 sun8i_crypto_worker(struct work *wk, void *cookie)
703 {
704 struct sun8i_crypto_softc *sc = cookie;
705 uint32_t done, esr, esr_chan;
706 unsigned i, now;
707 int error;
708
709 /*
710 * Acquire the lock. Note: We will be releasing and
711 * reacquiring it throughout the loop.
712 */
713 mutex_enter(&sc->sc_lock);
714
715 /* Acknowledge the work. */
716 KASSERT(sc->sc_work_pending);
717 sc->sc_work_pending = false;
718
719 /*
720 * Claim the done mask and error status once; we will be
721 * releasing and reacquiring the lock for the callbacks, so
722 * they may change.
723 */
724 done = sc->sc_done;
725 esr = sc->sc_esr;
726 sc->sc_done = 0;
727 sc->sc_esr = 0;
728
729 /* Check the time to determine what's timed out. */
730 now = atomic_load_relaxed(&hardclock_ticks);
731
732 /* Process the channels. */
733 for (i = 0; i < SUN8I_CRYPTO_NCHAN; i++) {
734 if (!ISSET(done, SUN8I_CRYPTO_ISR_DONE_CHAN(i))) {
735 /* Check to see if we have a task that timed out. */
736 if ((sc->sc_chan[i].cc_task != NULL) &&
737 ((now - sc->sc_chan[i].cc_starttime) >=
738 SUN8I_CRYPTO_TIMEOUT))
739 sun8i_crypto_chan_done(sc, i, ETIMEDOUT);
740 continue;
741 }
742 esr_chan = __SHIFTOUT(esr, SUN8I_CRYPTO_ESR_CHAN(i));
743 if (esr_chan & SUN8I_CRYPTO_ESR_CHAN_ALGNOTSUP) {
744 device_printf(sc->sc_dev, "channel %u:"
745 " alg not supported\n", i);
746 error = ENODEV;
747 } else if (esr_chan & SUN8I_CRYPTO_ESR_CHAN_DATALENERR) {
748 device_printf(sc->sc_dev, "channel %u:"
749 " data length error\n", i);
750 error = EIO; /* XXX */
751 } else if (esr_chan & SUN8I_CRYPTO_ESR_CHAN_KEYSRAMERR) {
752 device_printf(sc->sc_dev, "channel %u:"
753 " key sram error\n", i);
754 error = EIO; /* XXX */
755 } else if (esr_chan != 0) {
756 error = EIO; /* generic I/O error */
757 } else {
758 error = 0;
759 }
760
761 /* May release the lock to invoke a callback. */
762 sun8i_crypto_chan_done(sc, i, error);
763 }
764
765 /* All one; release the lock one last time. */
766 mutex_exit(&sc->sc_lock);
767 }
768
769 static void
770 sun8i_crypto_chan_done(struct sun8i_crypto_softc *sc, unsigned i, int error)
771 {
772 struct sun8i_crypto_task *task;
773 uint32_t icr;
774
775 KASSERT(mutex_owned(&sc->sc_lock));
776
777 /* Claim the task if there is one; bail if not. */
778 if ((task = sc->sc_chan[i].cc_task) == NULL) {
779 device_printf(sc->sc_dev, "channel %u: no task but error=%d\n",
780 i, error);
781 return;
782 }
783 sc->sc_chan[i].cc_task = NULL;
784
785 /* Disable interrupts on this channel. */
786 icr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ICR);
787 icr &= ~__SHIFTIN(SUN8I_CRYPTO_ICR_INTR_EN_CHAN(i),
788 SUN8I_CRYPTO_ICR_INTR_EN);
789 sun8i_crypto_write(sc, SUN8I_CRYPTO_ICR, icr);
790
791 /* Finished sending the descriptor to the device by DMA. */
792 bus_dmamap_sync(sc->sc_dmat, task->ct_buf.cb_map, 0,
793 sizeof(*task->ct_desc), BUS_DMASYNC_POSTWRITE);
794
795 /* Temporarily release the lock to invoke the callback. */
796 mutex_exit(&sc->sc_lock);
797 (*task->ct_callback)(sc, task, task->ct_cookie, error);
798 mutex_enter(&sc->sc_lock);
799 }
800
801 /*
802 * DMA buffers
803 */
804
805 static int
806 sun8i_crypto_allocbuf(struct sun8i_crypto_softc *sc, size_t size,
807 struct sun8i_crypto_buf *buf)
808 {
809 int error;
810
811 /* Allocate a DMA-safe buffer. */
812 error = bus_dmamem_alloc(sc->sc_dmat, size, 0, 0, buf->cb_seg,
813 __arraycount(buf->cb_seg), &buf->cb_nsegs, BUS_DMA_WAITOK);
814 if (error)
815 goto fail0;
816
817 /* Map the buffer into kernel virtual address space. */
818 error = bus_dmamem_map(sc->sc_dmat, buf->cb_seg, buf->cb_nsegs,
819 size, &buf->cb_kva, BUS_DMA_WAITOK);
820 if (error)
821 goto fail1;
822
823 /* Create a DMA map for the buffer. */
824 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
825 BUS_DMA_WAITOK, &buf->cb_map);
826 if (error)
827 goto fail2;
828
829 /* Load the buffer into the DMA map. */
830 error = bus_dmamap_load(sc->sc_dmat, buf->cb_map, buf->cb_kva, size,
831 NULL, BUS_DMA_WAITOK);
832 if (error)
833 goto fail3;
834
835 /* Success! */
836 return 0;
837
838 fail4: __unused
839 bus_dmamap_unload(sc->sc_dmat, buf->cb_map);
840 fail3: bus_dmamap_destroy(sc->sc_dmat, buf->cb_map);
841 fail2: bus_dmamem_unmap(sc->sc_dmat, buf->cb_kva, size);
842 fail1: bus_dmamem_free(sc->sc_dmat, buf->cb_seg, buf->cb_nsegs);
843 fail0: return error;
844 }
845
846 static void
847 sun8i_crypto_freebuf(struct sun8i_crypto_softc *sc, size_t size,
848 struct sun8i_crypto_buf *buf)
849 {
850
851 bus_dmamap_unload(sc->sc_dmat, buf->cb_map);
852 bus_dmamap_destroy(sc->sc_dmat, buf->cb_map);
853 bus_dmamem_unmap(sc->sc_dmat, buf->cb_kva, size);
854 bus_dmamem_free(sc->sc_dmat, buf->cb_seg, buf->cb_nsegs);
855 }
856
857 /*
858 * Crypto Engine - TRNG
859 */
860
861 static void
862 sun8i_crypto_rng_attach(struct sun8i_crypto_softc *sc)
863 {
864 device_t self = sc->sc_dev;
865 struct sun8i_crypto_rng *rng = &sc->sc_rng;
866 int error;
867
868 /* Preallocate a buffer to reuse. */
869 error = sun8i_crypto_allocbuf(sc, SUN8I_CRYPTO_RNGBYTES, &rng->cr_buf);
870 if (error)
871 goto fail0;
872
873 /* Create a task to reuse. */
874 rng->cr_task = sun8i_crypto_task_get(sc, sun8i_crypto_rng_done, rng);
875 if (rng->cr_task == NULL)
876 goto fail1;
877
878 /*
879 * Attach the rndsource. This is _not_ marked as RND_TYPE_RNG
880 * because the output is not uniformly distributed. The bits
881 * are heavily weighted toward 0 or 1, at different times, and
882 * I haven't scienced a satisfactory story out of it yet.
883 */
884 rndsource_setcb(&rng->cr_rndsource, sun8i_crypto_rng_get, sc);
885 rnd_attach_source(&rng->cr_rndsource, device_xname(self),
886 RND_TYPE_UNKNOWN,
887 RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_VALUE|RND_FLAG_HASCB);
888
889 /* Success! */
890 return;
891
892 fail2: __unused
893 sun8i_crypto_task_put(sc, rng->cr_task);
894 fail1: sun8i_crypto_freebuf(sc, SUN8I_CRYPTO_RNGBYTES, &rng->cr_buf);
895 fail0: aprint_error_dev(self, "failed to set up RNG, error=%d\n", error);
896 }
897
898 static void
899 sun8i_crypto_rng_get(size_t nbytes, void *cookie)
900 {
901 struct sun8i_crypto_softc *sc = cookie;
902 struct sun8i_crypto_rng *rng = &sc->sc_rng;
903 bool pending;
904 int error;
905
906 /*
907 * Test and set the RNG-pending flag. If it's already in
908 * progress, nothing to do here.
909 */
910 mutex_enter(&sc->sc_lock);
911 pending = rng->cr_pending;
912 rng->cr_pending = true;
913 mutex_exit(&sc->sc_lock);
914 if (pending)
915 return;
916
917 /* Prepare for a DMA read into the buffer. */
918 bus_dmamap_sync(sc->sc_dmat, rng->cr_buf.cb_map,
919 0, SUN8I_CRYPTO_RNGBYTES, BUS_DMASYNC_PREREAD);
920
921 /* Set the task up for TRNG to our buffer. */
922 sun8i_crypto_task_reset(rng->cr_task);
923 sun8i_crypto_task_set_output(rng->cr_task, rng->cr_buf.cb_map);
924
925 /* Submit the TRNG task. */
926 error = sun8i_crypto_submit_trng(sc, rng->cr_task,
927 SUN8I_CRYPTO_RNGBYTES);
928 if (error)
929 goto fail;
930
931 /* All done! */
932 return;
933
934 fail: mutex_enter(&sc->sc_lock);
935 rng->cr_pending = false;
936 mutex_exit(&sc->sc_lock);
937 }
938
939 static void
940 sun8i_crypto_rng_done(struct sun8i_crypto_softc *sc,
941 struct sun8i_crypto_task *task, void *cookie, int error)
942 {
943 struct sun8i_crypto_rng *rng = cookie;
944 uint8_t *buf = rng->cr_buf.cb_kva;
945 uint32_t entropybits;
946
947 KASSERT(rng == &sc->sc_rng);
948
949 /* Finished the DMA read into the buffer. */
950 bus_dmamap_sync(sc->sc_dmat, rng->cr_buf.cb_map,
951 0, SUN8I_CRYPTO_RNGBYTES, BUS_DMASYNC_POSTREAD);
952
953 /* If anything went wrong, forget about it. */
954 if (error)
955 goto out;
956
957 /*
958 * This TRNG has quite low entropy at best. But if it fails a
959 * repeated output test, then assume it's busted.
960 */
961 CTASSERT((SUN8I_CRYPTO_RNGBYTES % SUN8I_CRYPTO_RNGENTROPY) == 0);
962 entropybits = NBBY * (SUN8I_CRYPTO_RNGBYTES/SUN8I_CRYPTO_RNGENTROPY);
963 if (consttime_memequal(buf, buf + SUN8I_CRYPTO_RNGBYTES/2,
964 SUN8I_CRYPTO_RNGBYTES/2)) {
965 device_printf(sc->sc_dev, "failed repeated output test\n");
966 entropybits = 0;
967 }
968
969 /* Success! Enter and erase the data. */
970 rnd_add_data(&rng->cr_rndsource, buf, SUN8I_CRYPTO_RNGBYTES,
971 entropybits);
972 explicit_memset(buf, 0, SUN8I_CRYPTO_RNGBYTES);
973
974 out: /* Done -- clear the RNG-pending flag. */
975 mutex_enter(&sc->sc_lock);
976 rng->cr_pending = false;
977 mutex_exit(&sc->sc_lock);
978 }
979
980 /*
981 * Self-test
982 */
983
984 static const uint8_t selftest_input[16];
985 static const uint8_t selftest_key[16];
986 static const uint8_t selftest_output[16] = {
987 0x66,0xe9,0x4b,0xd4,0xef,0x8a,0x2c,0x3b,
988 0x88,0x4c,0xfa,0x59,0xca,0x34,0x2b,0x2e,
989 };
990
991 static void
992 sun8i_crypto_selftest(device_t self)
993 {
994 const size_t datalen = sizeof selftest_input;
995 struct sun8i_crypto_softc *sc = device_private(self);
996 struct sun8i_crypto_selftest *selftest = &sc->sc_selftest;
997 int error;
998
999 CTASSERT(sizeof selftest_input == sizeof selftest_output);
1000
1001 /* Allocate an input buffer. */
1002 error = sun8i_crypto_allocbuf(sc, sizeof selftest_input,
1003 &selftest->cs_in);
1004 if (error)
1005 goto fail0;
1006
1007 /* Allocate a key buffer. */
1008 error = sun8i_crypto_allocbuf(sc, sizeof selftest_key,
1009 &selftest->cs_key);
1010 if (error)
1011 goto fail1;
1012
1013 /* Allocate an output buffer. */
1014 error = sun8i_crypto_allocbuf(sc, sizeof selftest_output,
1015 &selftest->cs_out);
1016 if (error)
1017 goto fail2;
1018
1019 /* Allocate a task descriptor. */
1020 selftest->cs_task = sun8i_crypto_task_get(sc,
1021 sun8i_crypto_selftest_done, selftest);
1022 if (selftest->cs_task == NULL)
1023 goto fail3;
1024
1025 /* Copy the input and key into their buffers. */
1026 memcpy(selftest->cs_in.cb_kva, selftest_input, sizeof selftest_input);
1027 memcpy(selftest->cs_key.cb_kva, selftest_key, sizeof selftest_key);
1028
1029 /* Prepare for a DMA write from the input and key buffers. */
1030 bus_dmamap_sync(sc->sc_dmat, selftest->cs_in.cb_map, 0,
1031 sizeof selftest_input, BUS_DMASYNC_PREWRITE);
1032 bus_dmamap_sync(sc->sc_dmat, selftest->cs_key.cb_map, 0,
1033 sizeof selftest_key, BUS_DMASYNC_PREWRITE);
1034
1035 /* Prepare for a DMA read into the output buffer. */
1036 bus_dmamap_sync(sc->sc_dmat, selftest->cs_out.cb_map, 0,
1037 sizeof selftest_output, BUS_DMASYNC_PREREAD);
1038
1039 /* Set up the task descriptor. */
1040 sun8i_crypto_task_reset(selftest->cs_task);
1041 sun8i_crypto_task_set_key(selftest->cs_task, selftest->cs_key.cb_map);
1042 sun8i_crypto_task_set_input(selftest->cs_task, selftest->cs_in.cb_map);
1043 sun8i_crypto_task_set_output(selftest->cs_task,
1044 selftest->cs_out.cb_map);
1045
1046 /* Submit the AES-128 ECB task. */
1047 error = sun8i_crypto_submit_aesecb(sc, selftest->cs_task, datalen,
1048 SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128, SUN8I_CRYPTO_TDQC_OP_DIR_ENC);
1049 if (error)
1050 goto fail4;
1051
1052 device_printf(sc->sc_dev, "AES-128 self-test initiated\n");
1053
1054 /* Success! */
1055 return;
1056
1057 fail4: sun8i_crypto_task_put(sc, selftest->cs_task);
1058 fail3: sun8i_crypto_freebuf(sc, sizeof selftest_output, &selftest->cs_out);
1059 fail2: sun8i_crypto_freebuf(sc, sizeof selftest_key, &selftest->cs_key);
1060 fail1: sun8i_crypto_freebuf(sc, sizeof selftest_input, &selftest->cs_in);
1061 fail0: aprint_error_dev(self, "failed to run self-test, error=%d\n", error);
1062 }
1063
1064 static bool
1065 sun8i_crypto_selftest_check(struct sun8i_crypto_softc *sc, const char *title,
1066 size_t n, const void *expected, const void *actual)
1067 {
1068 const uint8_t *e = expected;
1069 const uint8_t *a = actual;
1070 size_t i;
1071
1072 if (memcmp(e, a, n) == 0)
1073 return true;
1074
1075 device_printf(sc->sc_dev, "self-test: %s\n", title);
1076 printf("expected: ");
1077 for (i = 0; i < n; i++)
1078 printf("%02hhx", e[i]);
1079 printf("\n");
1080 printf("actual: ");
1081 for (i = 0; i < n; i++)
1082 printf("%02hhx", a[i]);
1083 printf("\n");
1084 return false;
1085 }
1086
1087 static void
1088 sun8i_crypto_selftest_done(struct sun8i_crypto_softc *sc,
1089 struct sun8i_crypto_task *task, void *cookie, int error)
1090 {
1091 struct sun8i_crypto_selftest *selftest = cookie;
1092 bool ok = true;
1093
1094 KASSERT(selftest == &sc->sc_selftest);
1095
1096 /*
1097 * Finished the DMA read into the output buffer, and finished
1098 * the DMA writes from the key buffer and input buffer.
1099 */
1100 bus_dmamap_sync(sc->sc_dmat, selftest->cs_out.cb_map, 0,
1101 sizeof selftest_output, BUS_DMASYNC_POSTREAD);
1102 bus_dmamap_sync(sc->sc_dmat, selftest->cs_key.cb_map, 0,
1103 sizeof selftest_key, BUS_DMASYNC_POSTWRITE);
1104 bus_dmamap_sync(sc->sc_dmat, selftest->cs_in.cb_map, 0,
1105 sizeof selftest_input, BUS_DMASYNC_POSTWRITE);
1106
1107 /* If anything went wrong, fail now. */
1108 if (error) {
1109 device_printf(sc->sc_dev, "self-test error=%d\n", error);
1110 goto out;
1111 }
1112
1113 /*
1114 * Verify the input and key weren't clobbered, and verify the
1115 * output matches what we expect.
1116 */
1117 ok &= sun8i_crypto_selftest_check(sc, "input clobbered",
1118 sizeof selftest_input, selftest_input, selftest->cs_in.cb_kva);
1119 ok &= sun8i_crypto_selftest_check(sc, "key clobbered",
1120 sizeof selftest_key, selftest_key, selftest->cs_key.cb_kva);
1121 ok &= sun8i_crypto_selftest_check(sc, "output mismatch",
1122 sizeof selftest_output, selftest_output, selftest->cs_out.cb_kva);
1123
1124 /* XXX Disable the RNG and other stuff if this fails... */
1125 if (ok)
1126 device_printf(sc->sc_dev, "AES-128 self-test passed\n");
1127
1128 out: sun8i_crypto_task_put(sc, task);
1129 sun8i_crypto_freebuf(sc, sizeof selftest_output, &selftest->cs_out);
1130 sun8i_crypto_freebuf(sc, sizeof selftest_key, &selftest->cs_key);
1131 sun8i_crypto_freebuf(sc, sizeof selftest_input, &selftest->cs_in);
1132 }
1133
1134 /*
1135 * Sysctl for testing
1136 */
1137
1138 struct sun8i_crypto_userreq {
1139 kmutex_t cu_lock;
1140 kcondvar_t cu_cv;
1141 size_t cu_size;
1142 struct sun8i_crypto_buf cu_buf;
1143 struct sun8i_crypto_task *cu_task;
1144 int cu_error;
1145 bool cu_done;
1146 bool cu_cancel;
1147 };
1148
1149 static void
1150 sun8i_crypto_sysctl_attach(struct sun8i_crypto_softc *sc)
1151 {
1152 struct sun8i_crypto_sysctl *cy = &sc->sc_sysctl;
1153 int error;
1154
1155 error = sysctl_createv(&cy->cy_log, 0, NULL, &cy->cy_root_node,
1156 CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
1157 SYSCTL_DESCR("sun8i crypto engine knobs"),
1158 NULL, 0, NULL, 0,
1159 CTL_HW, CTL_CREATE, CTL_EOL);
1160 if (error) {
1161 aprint_error_dev(sc->sc_dev,
1162 "failed to set up sysctl hw.%s: %d\n",
1163 device_xname(sc->sc_dev), error);
1164 return;
1165 }
1166
1167 sysctl_createv(&cy->cy_log, 0, &cy->cy_root_node, NULL,
1168 CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_PRIVATE, CTLTYPE_STRUCT,
1169 "rng", SYSCTL_DESCR("Read up to 1024 bytes out of the TRNG"),
1170 &sun8i_crypto_sysctl_rng, 0, sc, 0, CTL_CREATE, CTL_EOL);
1171 if (error) {
1172 aprint_error_dev(sc->sc_dev,
1173 "failed to set up sysctl hw.%s.rng: %d\n",
1174 device_xname(sc->sc_dev), error);
1175 return;
1176 }
1177 }
1178
1179 static int
1180 sun8i_crypto_sysctl_rng(SYSCTLFN_ARGS)
1181 {
1182 struct sysctlnode node = *rnode;
1183 struct sun8i_crypto_softc *sc = node.sysctl_data;
1184 struct sun8i_crypto_userreq *req;
1185 size_t size;
1186 int error;
1187
1188 /* If oldp == NULL, the caller wants to learn the size. */
1189 if (oldp == NULL) {
1190 *oldlenp = 1024;
1191 return 0;
1192 }
1193
1194 /* Verify the output buffer size is reasonable. */
1195 size = *oldlenp;
1196 if (size > 1024) /* size_t, so never negative */
1197 return E2BIG;
1198 if (size == 0)
1199 return 0; /* nothing to do */
1200
1201 /* Allocate a request context. */
1202 req = kmem_alloc(sizeof(*req), KM_NOSLEEP);
1203 if (req == NULL)
1204 return ENOMEM;
1205
1206 /* Initialize the request context. */
1207 mutex_init(&req->cu_lock, MUTEX_DEFAULT, IPL_NONE);
1208 cv_init(&req->cu_cv, "sun8isy");
1209 req->cu_size = size;
1210 req->cu_error = EIO;
1211 req->cu_done = false;
1212 req->cu_cancel = false;
1213
1214 /* Allocate a buffer for the RNG output. */
1215 error = sun8i_crypto_allocbuf(sc, size, &req->cu_buf);
1216 if (error)
1217 goto out0;
1218
1219 /* Allocate a task. */
1220 req->cu_task = sun8i_crypto_task_get(sc, sun8i_crypto_sysctl_rng_done,
1221 req);
1222 if (req->cu_task == NULL)
1223 goto out1;
1224
1225 /* Prepare for a DMA read into the buffer. */
1226 bus_dmamap_sync(sc->sc_dmat, req->cu_buf.cb_map, 0, size,
1227 BUS_DMASYNC_PREREAD);
1228
1229 /* Set the task up for TRNG to our buffer. */
1230 sun8i_crypto_task_reset(req->cu_task);
1231 sun8i_crypto_task_set_output(req->cu_task, req->cu_buf.cb_map);
1232
1233 /* Submit the TRNG task. */
1234 error = sun8i_crypto_submit_trng(sc, req->cu_task, size);
1235 if (error) {
1236 if (error == ERESTART)
1237 error = EBUSY;
1238 goto out2;
1239 }
1240
1241 /* Wait for the request to complete. */
1242 mutex_enter(&req->cu_lock);
1243 while (!req->cu_done) {
1244 error = cv_wait_sig(&req->cu_cv, &req->cu_lock);
1245 if (error) {
1246 /*
1247 * Never mind -- notify the callback that it
1248 * has to clean up after itself.
1249 */
1250 req->cu_cancel = true;
1251 break;
1252 }
1253 }
1254 mutex_exit(&req->cu_lock);
1255
1256 /*
1257 * Return early on error from cv_wait_sig, which means
1258 * interruption; the callback will clean up instead.
1259 */
1260 if (error)
1261 return error;
1262
1263 /* Check for error from the device. */
1264 error = req->cu_error;
1265 if (error)
1266 goto out2;
1267
1268 /* Finished the DMA read into the buffer. */
1269 bus_dmamap_sync(sc->sc_dmat, req->cu_buf.cb_map, 0, req->cu_size,
1270 BUS_DMASYNC_POSTREAD);
1271
1272 /* Copy out the data. */
1273 node.sysctl_data = req->cu_buf.cb_kva;
1274 node.sysctl_size = size;
1275 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1276
1277 /* Clear the buffer. */
1278 explicit_memset(req->cu_buf.cb_kva, 0, size);
1279
1280 out2: sun8i_crypto_task_put(sc, req->cu_task);
1281 out1: sun8i_crypto_freebuf(sc, req->cu_size, &req->cu_buf);
1282 out0: cv_destroy(&req->cu_cv);
1283 mutex_destroy(&req->cu_lock);
1284 return error;
1285 }
1286
1287 static void
1288 sun8i_crypto_sysctl_rng_done(struct sun8i_crypto_softc *sc,
1289 struct sun8i_crypto_task *task, void *cookie, int error)
1290 {
1291 struct sun8i_crypto_userreq *req = cookie;
1292 bool cancel;
1293
1294 /*
1295 * Notify the waiting thread of the error, and find out whether
1296 * that thread cancelled.
1297 */
1298 mutex_enter(&req->cu_lock);
1299 cancel = req->cu_cancel;
1300 req->cu_error = error;
1301 req->cu_done = true;
1302 cv_broadcast(&req->cu_cv);
1303 mutex_exit(&req->cu_lock);
1304
1305 /*
1306 * If it wasn't cancelled, we're done -- the main thread will
1307 * clean up after itself.
1308 */
1309 if (!cancel)
1310 return;
1311
1312 /* Clean up. */
1313 sun8i_crypto_task_put(sc, req->cu_task);
1314 sun8i_crypto_freebuf(sc, req->cu_size, &req->cu_buf);
1315 cv_destroy(&req->cu_cv);
1316 mutex_destroy(&req->cu_lock);
1317 }
1318