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