sun8i_crypto.c revision 1.23 1 1.23 thorpej /* $NetBSD: sun8i_crypto.c,v 1.23 2021/01/27 03:10:20 thorpej Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad /*
33 1.1 riastrad * sun8i_crypto -- Allwinner Crypto Engine driver
34 1.1 riastrad *
35 1.1 riastrad * The Crypto Engine is documented in Sec. 3.15 of the Allwinner A64
36 1.1 riastrad * User Manual v1.1, on pp. 230--241. We only use it for the TRNG at
37 1.1 riastrad * the moment, but in principle it could be wired up with opencrypto(9)
38 1.1 riastrad * to compute AES, DES, 3DES, MD5, SHA-1, SHA-224, SHA-256, HMAC-SHA1,
39 1.1 riastrad * HMAC-HA256, RSA, and an undocumented PRNG. It also seems to support
40 1.1 riastrad * AES keys in SRAM (for some kind of HDMI HDCP stuff?).
41 1.1 riastrad *
42 1.1 riastrad * https://linux-sunxi.org/images/b/b4/Allwinner_A64_User_Manual_V1.1.pdf
43 1.1 riastrad */
44 1.1 riastrad
45 1.1 riastrad #include <sys/cdefs.h>
46 1.23 thorpej __KERNEL_RCSID(1, "$NetBSD: sun8i_crypto.c,v 1.23 2021/01/27 03:10:20 thorpej Exp $");
47 1.1 riastrad
48 1.1 riastrad #include <sys/types.h>
49 1.1 riastrad #include <sys/param.h>
50 1.1 riastrad #include <sys/atomic.h>
51 1.1 riastrad #include <sys/bus.h>
52 1.1 riastrad #include <sys/callout.h>
53 1.1 riastrad #include <sys/conf.h>
54 1.17 riastrad #include <sys/cprng.h>
55 1.1 riastrad #include <sys/device.h>
56 1.1 riastrad #include <sys/kernel.h>
57 1.1 riastrad #include <sys/kmem.h>
58 1.17 riastrad #include <sys/mbuf.h>
59 1.1 riastrad #include <sys/mutex.h>
60 1.1 riastrad #include <sys/rndsource.h>
61 1.17 riastrad #include <sys/sdt.h>
62 1.1 riastrad #include <sys/sysctl.h>
63 1.1 riastrad #include <sys/workqueue.h>
64 1.1 riastrad
65 1.1 riastrad #include <dev/fdt/fdtvar.h>
66 1.1 riastrad
67 1.17 riastrad #include <opencrypto/cryptodev.h>
68 1.17 riastrad
69 1.1 riastrad #include <arm/sunxi/sun8i_crypto.h>
70 1.1 riastrad
71 1.1 riastrad #define SUN8I_CRYPTO_TIMEOUT hz
72 1.9 riastrad #define SUN8I_CRYPTO_RNGENTROPY 100 /* estimated bits per bit of entropy */
73 1.9 riastrad #define SUN8I_CRYPTO_RNGBYTES PAGE_SIZE
74 1.1 riastrad
75 1.1 riastrad struct sun8i_crypto_task;
76 1.1 riastrad
77 1.1 riastrad struct sun8i_crypto_buf {
78 1.1 riastrad bus_dma_segment_t cb_seg[1];
79 1.1 riastrad int cb_nsegs;
80 1.1 riastrad void *cb_kva;
81 1.1 riastrad };
82 1.1 riastrad
83 1.1 riastrad struct sun8i_crypto_softc {
84 1.1 riastrad device_t sc_dev;
85 1.1 riastrad bus_space_tag_t sc_bst;
86 1.1 riastrad bus_space_handle_t sc_bsh;
87 1.1 riastrad bus_dma_tag_t sc_dmat;
88 1.16 riastrad struct pool_cache *sc_taskpool;
89 1.1 riastrad kmutex_t sc_lock;
90 1.1 riastrad struct sun8i_crypto_chan {
91 1.1 riastrad struct sun8i_crypto_task *cc_task;
92 1.1 riastrad unsigned cc_starttime;
93 1.1 riastrad } sc_chan[SUN8I_CRYPTO_NCHAN];
94 1.1 riastrad struct callout sc_timeout;
95 1.1 riastrad struct workqueue *sc_wq;
96 1.1 riastrad struct work sc_work;
97 1.1 riastrad void *sc_ih;
98 1.1 riastrad uint32_t sc_done;
99 1.1 riastrad uint32_t sc_esr;
100 1.1 riastrad bool sc_work_pending;
101 1.1 riastrad struct sun8i_crypto_rng {
102 1.1 riastrad struct sun8i_crypto_buf cr_buf;
103 1.1 riastrad struct sun8i_crypto_task *cr_task;
104 1.1 riastrad struct krndsource cr_rndsource;
105 1.1 riastrad bool cr_pending;
106 1.1 riastrad } sc_rng;
107 1.1 riastrad struct sun8i_crypto_selftest {
108 1.1 riastrad struct sun8i_crypto_buf cs_in;
109 1.1 riastrad struct sun8i_crypto_buf cs_key;
110 1.1 riastrad struct sun8i_crypto_buf cs_out;
111 1.1 riastrad struct sun8i_crypto_task *cs_task;
112 1.1 riastrad } sc_selftest;
113 1.1 riastrad struct sun8i_crypto_sysctl {
114 1.1 riastrad struct sysctllog *cy_log;
115 1.1 riastrad const struct sysctlnode *cy_root_node;
116 1.1 riastrad const struct sysctlnode *cy_trng_node;
117 1.1 riastrad } sc_sysctl;
118 1.17 riastrad struct sun8i_crypto_opencrypto {
119 1.17 riastrad uint32_t co_driverid;
120 1.17 riastrad } sc_opencrypto;
121 1.1 riastrad };
122 1.1 riastrad
123 1.1 riastrad struct sun8i_crypto_task {
124 1.16 riastrad struct sun8i_crypto_buf ct_descbuf;
125 1.1 riastrad struct sun8i_crypto_taskdesc *ct_desc;
126 1.17 riastrad struct sun8i_crypto_buf ct_ivbuf;
127 1.17 riastrad void *ct_iv;
128 1.17 riastrad struct sun8i_crypto_buf ct_ctrbuf;
129 1.17 riastrad void *ct_ctr;
130 1.16 riastrad bus_dmamap_t ct_descmap;
131 1.16 riastrad bus_dmamap_t ct_keymap;
132 1.17 riastrad bus_dmamap_t ct_ivmap; /* IV input */
133 1.17 riastrad bus_dmamap_t ct_ctrmap; /* updated IV output */
134 1.16 riastrad bus_dmamap_t ct_srcmap;
135 1.16 riastrad bus_dmamap_t ct_dstmap;
136 1.16 riastrad uint32_t ct_nbytes;
137 1.16 riastrad int ct_flags;
138 1.16 riastrad #define TASK_KEY __BIT(0)
139 1.16 riastrad #define TASK_IV __BIT(1)
140 1.16 riastrad #define TASK_CTR __BIT(2)
141 1.16 riastrad #define TASK_SRC __BIT(3)
142 1.16 riastrad #define TASK_BYTES __BIT(4) /* datalen is in bytes, not words */
143 1.1 riastrad void (*ct_callback)(struct sun8i_crypto_softc *,
144 1.1 riastrad struct sun8i_crypto_task *, void *, int);
145 1.1 riastrad void *ct_cookie;
146 1.1 riastrad };
147 1.1 riastrad
148 1.16 riastrad #define SUN8I_CRYPTO_MAXDMASIZE PAGE_SIZE
149 1.16 riastrad #define SUN8I_CRYPTO_MAXDMASEGSIZE PAGE_SIZE
150 1.16 riastrad
151 1.16 riastrad CTASSERT(SUN8I_CRYPTO_MAXDMASIZE <= SUN8I_CRYPTO_MAXDATALEN);
152 1.16 riastrad CTASSERT(SUN8I_CRYPTO_MAXDMASEGSIZE <= SUN8I_CRYPTO_MAXSEGLEN);
153 1.16 riastrad
154 1.1 riastrad /*
155 1.1 riastrad * Forward declarations
156 1.1 riastrad */
157 1.1 riastrad
158 1.1 riastrad static int sun8i_crypto_match(device_t, cfdata_t, void *);
159 1.1 riastrad static void sun8i_crypto_attach(device_t, device_t, void *);
160 1.1 riastrad
161 1.16 riastrad static int sun8i_crypto_task_ctor(void *, void *, int);
162 1.16 riastrad static void sun8i_crypto_task_dtor(void *, void *);
163 1.1 riastrad static struct sun8i_crypto_task *
164 1.1 riastrad sun8i_crypto_task_get(struct sun8i_crypto_softc *,
165 1.1 riastrad void (*)(struct sun8i_crypto_softc *,
166 1.1 riastrad struct sun8i_crypto_task *, void *, int),
167 1.16 riastrad void *, int);
168 1.1 riastrad static void sun8i_crypto_task_put(struct sun8i_crypto_softc *,
169 1.1 riastrad struct sun8i_crypto_task *);
170 1.1 riastrad
171 1.16 riastrad static int sun8i_crypto_task_load(struct sun8i_crypto_softc *,
172 1.16 riastrad struct sun8i_crypto_task *, uint32_t,
173 1.16 riastrad uint32_t, uint32_t, uint32_t);
174 1.17 riastrad static int sun8i_crypto_task_scatter(struct sun8i_crypto_task *,
175 1.17 riastrad struct sun8i_crypto_adrlen *, bus_dmamap_t, uint32_t);
176 1.1 riastrad
177 1.16 riastrad static int sun8i_crypto_task_load_trng(struct sun8i_crypto_softc *,
178 1.1 riastrad struct sun8i_crypto_task *, uint32_t);
179 1.16 riastrad static int sun8i_crypto_task_load_aesecb(struct sun8i_crypto_softc *,
180 1.1 riastrad struct sun8i_crypto_task *, uint32_t, uint32_t, uint32_t);
181 1.16 riastrad
182 1.1 riastrad static int sun8i_crypto_submit(struct sun8i_crypto_softc *,
183 1.1 riastrad struct sun8i_crypto_task *);
184 1.1 riastrad
185 1.1 riastrad static void sun8i_crypto_timeout(void *);
186 1.1 riastrad static int sun8i_crypto_intr(void *);
187 1.1 riastrad static void sun8i_crypto_schedule_worker(struct sun8i_crypto_softc *);
188 1.1 riastrad static void sun8i_crypto_worker(struct work *, void *);
189 1.1 riastrad static void sun8i_crypto_chan_done(struct sun8i_crypto_softc *, unsigned,
190 1.1 riastrad int);
191 1.1 riastrad
192 1.1 riastrad static int sun8i_crypto_allocbuf(struct sun8i_crypto_softc *, size_t,
193 1.16 riastrad struct sun8i_crypto_buf *, int);
194 1.1 riastrad static void sun8i_crypto_freebuf(struct sun8i_crypto_softc *, size_t,
195 1.1 riastrad struct sun8i_crypto_buf *);
196 1.1 riastrad
197 1.1 riastrad static void sun8i_crypto_rng_attach(struct sun8i_crypto_softc *);
198 1.1 riastrad static void sun8i_crypto_rng_get(size_t, void *);
199 1.1 riastrad static void sun8i_crypto_rng_done(struct sun8i_crypto_softc *,
200 1.1 riastrad struct sun8i_crypto_task *, void *, int);
201 1.1 riastrad
202 1.1 riastrad static void sun8i_crypto_selftest(device_t);
203 1.1 riastrad static void sun8i_crypto_selftest_done(struct sun8i_crypto_softc *,
204 1.1 riastrad struct sun8i_crypto_task *, void *, int);
205 1.1 riastrad
206 1.1 riastrad static void sun8i_crypto_sysctl_attach(struct sun8i_crypto_softc *);
207 1.1 riastrad static int sun8i_crypto_sysctl_rng(SYSCTLFN_ARGS);
208 1.1 riastrad static void sun8i_crypto_sysctl_rng_done(struct sun8i_crypto_softc *,
209 1.1 riastrad struct sun8i_crypto_task *, void *, int);
210 1.1 riastrad
211 1.17 riastrad static void sun8i_crypto_register(struct sun8i_crypto_softc *);
212 1.17 riastrad static void sun8i_crypto_register1(struct sun8i_crypto_softc *, uint32_t);
213 1.17 riastrad static int sun8i_crypto_newsession(void *, uint32_t *,
214 1.17 riastrad struct cryptoini *);
215 1.17 riastrad static int sun8i_crypto_freesession(void *, uint64_t);
216 1.17 riastrad static u_int sun8i_crypto_ivlen(const struct cryptodesc *);
217 1.17 riastrad static int sun8i_crypto_process(void *, struct cryptop *, int);
218 1.17 riastrad static void sun8i_crypto_callback(struct sun8i_crypto_softc *,
219 1.17 riastrad struct sun8i_crypto_task *, void *, int);
220 1.17 riastrad
221 1.17 riastrad /*
222 1.17 riastrad * Probes
223 1.17 riastrad */
224 1.17 riastrad
225 1.17 riastrad SDT_PROBE_DEFINE2(sdt, sun8i_crypto, register, read,
226 1.17 riastrad "bus_size_t"/*reg*/,
227 1.17 riastrad "uint32_t"/*value*/);
228 1.17 riastrad SDT_PROBE_DEFINE2(sdt, sun8i_crypto, register, write,
229 1.17 riastrad "bus_size_t"/*reg*/,
230 1.17 riastrad "uint32_t"/*write*/);
231 1.17 riastrad
232 1.17 riastrad SDT_PROBE_DEFINE1(sdt, sun8i_crypto, task, ctor__success,
233 1.17 riastrad "struct sun8i_crypto_task *"/*task*/);
234 1.17 riastrad SDT_PROBE_DEFINE1(sdt, sun8i_crypto, task, ctor__failure,
235 1.17 riastrad "int"/*error*/);
236 1.17 riastrad SDT_PROBE_DEFINE1(sdt, sun8i_crypto, task, dtor,
237 1.17 riastrad "struct sun8i_crypto_task *"/*task*/);
238 1.17 riastrad SDT_PROBE_DEFINE1(sdt, sun8i_crypto, task, get,
239 1.17 riastrad "struct sun8i_crypto_task *"/*task*/);
240 1.17 riastrad SDT_PROBE_DEFINE1(sdt, sun8i_crypto, task, put,
241 1.17 riastrad "struct sun8i_crypto_task *"/*task*/);
242 1.17 riastrad
243 1.17 riastrad SDT_PROBE_DEFINE6(sdt, sun8i_crypto, task, load,
244 1.17 riastrad "struct sun8i_crypto_task *"/*task*/,
245 1.17 riastrad "uint32_t"/*tdqc*/,
246 1.17 riastrad "uint32_t"/*tdqs*/,
247 1.17 riastrad "uint32_t"/*tdqa*/,
248 1.17 riastrad "struct sun8i_crypto_taskdesc *"/*desc*/,
249 1.17 riastrad "int"/*error*/);
250 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, task, misaligned,
251 1.17 riastrad "struct sun8i_crypto_task *"/*task*/,
252 1.17 riastrad "bus_addr_t"/*ds_addr*/,
253 1.17 riastrad "bus_size_t"/*ds_len*/);
254 1.17 riastrad SDT_PROBE_DEFINE2(sdt, sun8i_crypto, task, done,
255 1.17 riastrad "struct sun8i_crypto_task *"/*task*/,
256 1.17 riastrad "int"/*error*/);
257 1.17 riastrad
258 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, engine, submit__failure,
259 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
260 1.17 riastrad "struct sun8i_crypto_task *"/*task*/,
261 1.17 riastrad "int"/*error*/);
262 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, engine, submit__success,
263 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
264 1.17 riastrad "struct sun8i_crypto_task *"/*task*/,
265 1.17 riastrad "unsigned"/*chan*/);
266 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, engine, intr,
267 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
268 1.17 riastrad "uint32_t"/*isr*/,
269 1.17 riastrad "uint32_t"/*esr*/);
270 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, engine, done,
271 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
272 1.17 riastrad "unsigned"/*chan*/,
273 1.17 riastrad "int"/*error*/);
274 1.17 riastrad
275 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, process, entry,
276 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
277 1.17 riastrad "struct cryptop *"/*crp*/,
278 1.17 riastrad "int"/*hint*/);
279 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, process, busy,
280 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
281 1.17 riastrad "struct cryptop *"/*crp*/,
282 1.17 riastrad "int"/*hint*/);
283 1.17 riastrad SDT_PROBE_DEFINE4(sdt, sun8i_crypto, process, queued,
284 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
285 1.17 riastrad "struct cryptop *"/*crp*/,
286 1.17 riastrad "int"/*hint*/,
287 1.17 riastrad "struct sun8i_crypto_task *"/*task*/);
288 1.17 riastrad SDT_PROBE_DEFINE3(sdt, sun8i_crypto, process, done,
289 1.17 riastrad "struct sun8i_crypto_softc *"/*sc*/,
290 1.17 riastrad "struct cryptop *"/*crp*/,
291 1.17 riastrad "int"/*error*/);
292 1.17 riastrad
293 1.1 riastrad /*
294 1.1 riastrad * Register access
295 1.1 riastrad */
296 1.1 riastrad
297 1.1 riastrad static uint32_t
298 1.17 riastrad sun8i_crypto_read(struct sun8i_crypto_softc *sc, bus_size_t reg)
299 1.1 riastrad {
300 1.17 riastrad uint32_t v = bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
301 1.17 riastrad
302 1.17 riastrad SDT_PROBE2(sdt, sun8i_crypto, register, read, reg, v);
303 1.17 riastrad return v;
304 1.1 riastrad }
305 1.1 riastrad
306 1.1 riastrad static void
307 1.17 riastrad sun8i_crypto_write(struct sun8i_crypto_softc *sc, bus_size_t reg, uint32_t v)
308 1.1 riastrad {
309 1.17 riastrad
310 1.17 riastrad SDT_PROBE2(sdt, sun8i_crypto, register, write, reg, v);
311 1.1 riastrad bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, v);
312 1.1 riastrad }
313 1.1 riastrad
314 1.1 riastrad /*
315 1.1 riastrad * Autoconf goo
316 1.1 riastrad */
317 1.1 riastrad
318 1.1 riastrad CFATTACH_DECL_NEW(sun8i_crypto, sizeof(struct sun8i_crypto_softc),
319 1.1 riastrad sun8i_crypto_match, sun8i_crypto_attach, NULL, NULL);
320 1.1 riastrad
321 1.20 thorpej static const struct device_compatible_entry compat_data[] = {
322 1.20 thorpej { .compat = "allwinner,sun50i-a64-crypto" },
323 1.22 thorpej DEVICE_COMPAT_EOL
324 1.1 riastrad };
325 1.1 riastrad
326 1.1 riastrad static int
327 1.1 riastrad sun8i_crypto_match(device_t parent, cfdata_t cf, void *aux)
328 1.1 riastrad {
329 1.1 riastrad const struct fdt_attach_args *const faa = aux;
330 1.1 riastrad
331 1.23 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
332 1.1 riastrad }
333 1.1 riastrad
334 1.1 riastrad static void
335 1.1 riastrad sun8i_crypto_attach(device_t parent, device_t self, void *aux)
336 1.1 riastrad {
337 1.1 riastrad struct sun8i_crypto_softc *const sc = device_private(self);
338 1.1 riastrad const struct fdt_attach_args *const faa = aux;
339 1.1 riastrad bus_addr_t addr;
340 1.1 riastrad bus_size_t size;
341 1.1 riastrad const int phandle = faa->faa_phandle;
342 1.1 riastrad char intrstr[128];
343 1.1 riastrad struct clk *clk;
344 1.1 riastrad struct fdtbus_reset *rst;
345 1.1 riastrad
346 1.1 riastrad sc->sc_dev = self;
347 1.1 riastrad sc->sc_dmat = faa->faa_dmat;
348 1.1 riastrad sc->sc_bst = faa->faa_bst;
349 1.16 riastrad sc->sc_taskpool = pool_cache_init(sizeof(struct sun8i_crypto_task),
350 1.16 riastrad 0, 0, 0, "sun8icry", NULL, IPL_VM,
351 1.16 riastrad &sun8i_crypto_task_ctor, &sun8i_crypto_task_dtor, sc);
352 1.1 riastrad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
353 1.1 riastrad callout_init(&sc->sc_timeout, CALLOUT_MPSAFE);
354 1.1 riastrad callout_setfunc(&sc->sc_timeout, &sun8i_crypto_timeout, sc);
355 1.1 riastrad if (workqueue_create(&sc->sc_wq, device_xname(self),
356 1.1 riastrad &sun8i_crypto_worker, sc, PRI_NONE, IPL_VM, WQ_MPSAFE) != 0) {
357 1.1 riastrad aprint_error(": couldn't create workqueue\n");
358 1.1 riastrad return;
359 1.1 riastrad }
360 1.1 riastrad
361 1.17 riastrad /*
362 1.17 riastrad * Prime the pool with enough tasks that each channel can be
363 1.17 riastrad * busy with a task as we prepare another task for when it's
364 1.17 riastrad * done.
365 1.17 riastrad */
366 1.17 riastrad pool_cache_prime(sc->sc_taskpool, 2*SUN8I_CRYPTO_NCHAN);
367 1.17 riastrad
368 1.1 riastrad /* Get and map device registers. */
369 1.1 riastrad if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
370 1.1 riastrad aprint_error(": couldn't get registers\n");
371 1.1 riastrad return;
372 1.1 riastrad }
373 1.1 riastrad if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
374 1.1 riastrad aprint_error(": couldn't map registers\n");
375 1.1 riastrad return;
376 1.1 riastrad }
377 1.1 riastrad
378 1.1 riastrad /* Get an interrupt handle. */
379 1.1 riastrad if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
380 1.1 riastrad aprint_error(": failed to decode interrupt\n");
381 1.1 riastrad return;
382 1.1 riastrad }
383 1.1 riastrad
384 1.1 riastrad /* Enable the bus clock. */
385 1.1 riastrad if (fdtbus_clock_enable(phandle, "bus", true) != 0) {
386 1.1 riastrad aprint_error(": couldn't enable bus clock\n");
387 1.1 riastrad return;
388 1.1 riastrad }
389 1.1 riastrad
390 1.1 riastrad /* Get the module clock and set it to 300 MHz. */
391 1.1 riastrad if ((clk = fdtbus_clock_get(phandle, "mod")) != NULL) {
392 1.1 riastrad if (clk_enable(clk) != 0) {
393 1.1 riastrad aprint_error(": couldn't enable CE clock\n");
394 1.1 riastrad return;
395 1.1 riastrad }
396 1.1 riastrad if (clk_set_rate(clk, 300*1000*1000) != 0) {
397 1.1 riastrad aprint_error(": couldn't set CE clock to 300MHz\n");
398 1.1 riastrad return;
399 1.1 riastrad }
400 1.1 riastrad }
401 1.1 riastrad
402 1.1 riastrad /* Get a reset handle if we need and try to deassert it. */
403 1.1 riastrad if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) {
404 1.1 riastrad if (fdtbus_reset_deassert(rst) != 0) {
405 1.1 riastrad aprint_error(": couldn't de-assert reset\n");
406 1.1 riastrad return;
407 1.1 riastrad }
408 1.1 riastrad }
409 1.1 riastrad
410 1.1 riastrad aprint_naive("\n");
411 1.1 riastrad aprint_normal(": Crypto Engine\n");
412 1.1 riastrad aprint_debug_dev(self, ": clock freq %d\n", clk_get_rate(clk));
413 1.1 riastrad
414 1.1 riastrad /* Disable and clear interrupts. */
415 1.1 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_ICR, 0);
416 1.1 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_ISR, 0);
417 1.1 riastrad
418 1.1 riastrad /* Establish an interrupt handler. */
419 1.19 jmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
420 1.19 jmcneill FDT_INTR_MPSAFE, &sun8i_crypto_intr, sc, device_xname(self));
421 1.1 riastrad if (sc->sc_ih == NULL) {
422 1.1 riastrad aprint_error_dev(self, "failed to establish interrupt on %s\n",
423 1.1 riastrad intrstr);
424 1.1 riastrad return;
425 1.1 riastrad }
426 1.1 riastrad aprint_normal_dev(self, "interrupting on %s\n", intrstr);
427 1.1 riastrad
428 1.1 riastrad /* Set up the RNG. */
429 1.1 riastrad sun8i_crypto_rng_attach(sc);
430 1.1 riastrad
431 1.1 riastrad /* Attach the sysctl. */
432 1.1 riastrad sun8i_crypto_sysctl_attach(sc);
433 1.1 riastrad
434 1.1 riastrad /* Perform self-tests. */
435 1.1 riastrad config_interrupts(self, sun8i_crypto_selftest);
436 1.17 riastrad
437 1.17 riastrad /* Register opencrypto handlers. */
438 1.17 riastrad sun8i_crypto_register(sc);
439 1.1 riastrad }
440 1.1 riastrad
441 1.16 riastrad static int
442 1.16 riastrad sun8i_crypto_task_ctor(void *cookie, void *vtask, int pflags)
443 1.1 riastrad {
444 1.16 riastrad struct sun8i_crypto_softc *sc = cookie;
445 1.16 riastrad struct sun8i_crypto_task *task = vtask;
446 1.16 riastrad int dmaflags = (pflags & PR_WAITOK) ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
447 1.1 riastrad int error;
448 1.1 riastrad
449 1.16 riastrad /* Create a DMA buffer for the task descriptor. */
450 1.2 riastrad error = sun8i_crypto_allocbuf(sc, sizeof(*task->ct_desc),
451 1.16 riastrad &task->ct_descbuf, dmaflags);
452 1.1 riastrad if (error)
453 1.1 riastrad goto fail0;
454 1.16 riastrad task->ct_desc = task->ct_descbuf.cb_kva;
455 1.16 riastrad
456 1.17 riastrad /* Create DMA buffers for the IV and CTR. */
457 1.17 riastrad error = sun8i_crypto_allocbuf(sc, SUN8I_CRYPTO_MAXIVBYTES,
458 1.17 riastrad &task->ct_ivbuf, dmaflags);
459 1.17 riastrad if (error)
460 1.17 riastrad goto fail1;
461 1.17 riastrad task->ct_iv = task->ct_ivbuf.cb_kva;
462 1.17 riastrad error = sun8i_crypto_allocbuf(sc, SUN8I_CRYPTO_MAXCTRBYTES,
463 1.17 riastrad &task->ct_ctrbuf, dmaflags);
464 1.17 riastrad if (error)
465 1.17 riastrad goto fail2;
466 1.17 riastrad task->ct_ctr = task->ct_ctrbuf.cb_kva;
467 1.17 riastrad
468 1.16 riastrad /* Create a DMA map for the task descriptor and preload it. */
469 1.16 riastrad error = bus_dmamap_create(sc->sc_dmat, sizeof(*task->ct_desc), 1,
470 1.16 riastrad sizeof(*task->ct_desc), 0, dmaflags, &task->ct_descmap);
471 1.16 riastrad if (error)
472 1.17 riastrad goto fail3;
473 1.16 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_descmap, task->ct_desc,
474 1.16 riastrad sizeof(*task->ct_desc), NULL, BUS_DMA_WAITOK);
475 1.16 riastrad if (error)
476 1.17 riastrad goto fail4;
477 1.16 riastrad
478 1.16 riastrad /* Create DMA maps for the key, IV, and CTR. */
479 1.16 riastrad error = bus_dmamap_create(sc->sc_dmat, SUN8I_CRYPTO_MAXKEYBYTES, 1,
480 1.16 riastrad SUN8I_CRYPTO_MAXKEYBYTES, 0, dmaflags, &task->ct_keymap);
481 1.16 riastrad if (error)
482 1.17 riastrad goto fail5;
483 1.16 riastrad error = bus_dmamap_create(sc->sc_dmat, SUN8I_CRYPTO_MAXIVBYTES, 1,
484 1.16 riastrad SUN8I_CRYPTO_MAXIVBYTES, 0, dmaflags, &task->ct_ivmap);
485 1.16 riastrad if (error)
486 1.17 riastrad goto fail6;
487 1.16 riastrad error = bus_dmamap_create(sc->sc_dmat, SUN8I_CRYPTO_MAXCTRBYTES, 1,
488 1.16 riastrad SUN8I_CRYPTO_MAXCTRBYTES, 0, dmaflags, &task->ct_ctrmap);
489 1.16 riastrad if (error)
490 1.17 riastrad goto fail7;
491 1.1 riastrad
492 1.16 riastrad /* Create DMA maps for the src and dst scatter/gather vectors. */
493 1.16 riastrad error = bus_dmamap_create(sc->sc_dmat, SUN8I_CRYPTO_MAXDMASIZE,
494 1.16 riastrad SUN8I_CRYPTO_MAXSEGS, SUN8I_CRYPTO_MAXDMASEGSIZE, 0, dmaflags,
495 1.16 riastrad &task->ct_srcmap);
496 1.16 riastrad if (error)
497 1.17 riastrad goto fail8;
498 1.16 riastrad error = bus_dmamap_create(sc->sc_dmat, SUN8I_CRYPTO_MAXDMASIZE,
499 1.16 riastrad SUN8I_CRYPTO_MAXSEGS, SUN8I_CRYPTO_MAXDMASEGSIZE, 0, dmaflags,
500 1.16 riastrad &task->ct_dstmap);
501 1.16 riastrad if (error)
502 1.17 riastrad goto fail9;
503 1.16 riastrad
504 1.16 riastrad /* Success! */
505 1.17 riastrad SDT_PROBE1(sdt, sun8i_crypto, task, ctor__success, task);
506 1.16 riastrad return 0;
507 1.1 riastrad
508 1.17 riastrad fail10: __unused
509 1.16 riastrad bus_dmamap_destroy(sc->sc_dmat, task->ct_dstmap);
510 1.17 riastrad fail9: bus_dmamap_destroy(sc->sc_dmat, task->ct_srcmap);
511 1.17 riastrad fail8: bus_dmamap_destroy(sc->sc_dmat, task->ct_ctrmap);
512 1.17 riastrad fail7: bus_dmamap_destroy(sc->sc_dmat, task->ct_ivmap);
513 1.17 riastrad fail6: bus_dmamap_destroy(sc->sc_dmat, task->ct_keymap);
514 1.17 riastrad fail5: bus_dmamap_unload(sc->sc_dmat, task->ct_descmap);
515 1.17 riastrad fail4: bus_dmamap_destroy(sc->sc_dmat, task->ct_descmap);
516 1.17 riastrad fail3: sun8i_crypto_freebuf(sc, SUN8I_CRYPTO_MAXIVBYTES, &task->ct_ivbuf);
517 1.17 riastrad fail2: sun8i_crypto_freebuf(sc, SUN8I_CRYPTO_MAXCTRBYTES, &task->ct_ctrbuf);
518 1.16 riastrad fail1: sun8i_crypto_freebuf(sc, sizeof(*task->ct_desc), &task->ct_descbuf);
519 1.17 riastrad fail0: SDT_PROBE1(sdt, sun8i_crypto, task, ctor__failure, error);
520 1.17 riastrad return error;
521 1.1 riastrad }
522 1.1 riastrad
523 1.1 riastrad static void
524 1.16 riastrad sun8i_crypto_task_dtor(void *cookie, void *vtask)
525 1.1 riastrad {
526 1.16 riastrad struct sun8i_crypto_softc *sc = cookie;
527 1.16 riastrad struct sun8i_crypto_task *task = vtask;
528 1.16 riastrad
529 1.17 riastrad SDT_PROBE1(sdt, sun8i_crypto, task, dtor, task);
530 1.17 riastrad
531 1.16 riastrad /* XXX Zero the bounce buffers if there are any. */
532 1.1 riastrad
533 1.16 riastrad bus_dmamap_destroy(sc->sc_dmat, task->ct_dstmap);
534 1.16 riastrad bus_dmamap_destroy(sc->sc_dmat, task->ct_srcmap);
535 1.16 riastrad bus_dmamap_destroy(sc->sc_dmat, task->ct_ctrmap);
536 1.16 riastrad bus_dmamap_destroy(sc->sc_dmat, task->ct_ivmap);
537 1.16 riastrad bus_dmamap_destroy(sc->sc_dmat, task->ct_keymap);
538 1.16 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_descmap);
539 1.16 riastrad bus_dmamap_destroy(sc->sc_dmat, task->ct_descmap);
540 1.17 riastrad sun8i_crypto_freebuf(sc, SUN8I_CRYPTO_MAXIVBYTES, &task->ct_ivbuf);
541 1.17 riastrad sun8i_crypto_freebuf(sc, SUN8I_CRYPTO_MAXCTRBYTES, &task->ct_ctrbuf);
542 1.16 riastrad sun8i_crypto_freebuf(sc, sizeof(*task->ct_desc), &task->ct_descbuf);
543 1.1 riastrad }
544 1.1 riastrad
545 1.1 riastrad /*
546 1.16 riastrad * sun8i_crypto_task_get(sc, callback, cookie, pflags)
547 1.1 riastrad *
548 1.16 riastrad * Allocate a task that will call callback(sc, task, cookie,
549 1.16 riastrad * error) when done. pflags is PR_WAITOK or PR_NOWAIT; if
550 1.16 riastrad * PR_NOWAIT, may fail and return NULL. No further allocation is
551 1.16 riastrad * needed to submit the task if this succeeds (although task
552 1.16 riastrad * submission may still fail if all channels are busy).
553 1.1 riastrad */
554 1.16 riastrad static struct sun8i_crypto_task *
555 1.16 riastrad sun8i_crypto_task_get(struct sun8i_crypto_softc *sc,
556 1.16 riastrad void (*callback)(struct sun8i_crypto_softc *, struct sun8i_crypto_task *,
557 1.16 riastrad void *, int),
558 1.16 riastrad void *cookie, int pflags)
559 1.16 riastrad {
560 1.16 riastrad struct sun8i_crypto_task *task;
561 1.16 riastrad
562 1.16 riastrad /* Allocate a task, or fail if we can't. */
563 1.16 riastrad task = pool_cache_get(sc->sc_taskpool, pflags);
564 1.16 riastrad if (task == NULL)
565 1.17 riastrad goto out;
566 1.16 riastrad
567 1.16 riastrad /* Set up flags and the callback. */
568 1.16 riastrad task->ct_flags = 0;
569 1.16 riastrad task->ct_callback = callback;
570 1.16 riastrad task->ct_cookie = cookie;
571 1.17 riastrad
572 1.17 riastrad out: SDT_PROBE1(sdt, sun8i_crypto, task, get, task);
573 1.16 riastrad return task;
574 1.16 riastrad }
575 1.1 riastrad
576 1.16 riastrad /*
577 1.16 riastrad * sun8i_crypto_task_invalid(sc, task, cookie, error)
578 1.16 riastrad *
579 1.16 riastrad * Callback for a task not currently in use, to detect errors.
580 1.16 riastrad */
581 1.1 riastrad static void
582 1.16 riastrad sun8i_crypto_task_invalid(struct sun8i_crypto_softc *sc,
583 1.16 riastrad struct sun8i_crypto_task *task, void *cookie, int error)
584 1.1 riastrad {
585 1.16 riastrad void (*callback)(struct sun8i_crypto_softc *,
586 1.16 riastrad struct sun8i_crypto_task *, void *, int) = cookie;
587 1.1 riastrad
588 1.16 riastrad panic("task for callback %p used after free", callback);
589 1.1 riastrad }
590 1.1 riastrad
591 1.16 riastrad /*
592 1.16 riastrad * sun8i_crypto_task_put(sc, task)
593 1.16 riastrad *
594 1.16 riastrad * Free a task obtained with sun8i_crypto_task_get.
595 1.16 riastrad */
596 1.1 riastrad static void
597 1.16 riastrad sun8i_crypto_task_put(struct sun8i_crypto_softc *sc,
598 1.16 riastrad struct sun8i_crypto_task *task)
599 1.1 riastrad {
600 1.1 riastrad
601 1.17 riastrad SDT_PROBE1(sdt, sun8i_crypto, task, put, task);
602 1.17 riastrad
603 1.16 riastrad task->ct_cookie = task->ct_callback;
604 1.16 riastrad task->ct_callback = &sun8i_crypto_task_invalid;
605 1.16 riastrad pool_cache_put(sc->sc_taskpool, task);
606 1.1 riastrad }
607 1.1 riastrad
608 1.16 riastrad /*
609 1.16 riastrad * sun8i_crypto_task_load(sc, task, nbytes, tdqc, tdqs, tdqa)
610 1.16 riastrad *
611 1.16 riastrad * Set up the task descriptor after the relevant DMA maps have
612 1.16 riastrad * been loaded for a transfer of nbytes. bus_dmamap_sync matches
613 1.16 riastrad * sun8i_crypto_chan_done. May fail if input is inadequately
614 1.16 riastrad * aligned.
615 1.16 riastrad *
616 1.16 riastrad * XXX Teach this to support task chains.
617 1.16 riastrad */
618 1.16 riastrad static int
619 1.16 riastrad sun8i_crypto_task_load(struct sun8i_crypto_softc *sc,
620 1.16 riastrad struct sun8i_crypto_task *task, uint32_t nbytes,
621 1.16 riastrad uint32_t tdqc, uint32_t tdqs, uint32_t tdqa)
622 1.1 riastrad {
623 1.16 riastrad struct sun8i_crypto_taskdesc *desc = task->ct_desc;
624 1.16 riastrad int error;
625 1.1 riastrad
626 1.16 riastrad KASSERT(tdqs == 0 || tdqa == 0);
627 1.16 riastrad KASSERT(nbytes % 4 == 0);
628 1.1 riastrad
629 1.16 riastrad memset(desc, 0, sizeof(*desc));
630 1.1 riastrad
631 1.17 riastrad /* Always enable interrupt for the task. */
632 1.17 riastrad tdqc |= SUN8I_CRYPTO_TDQC_INTR_EN;
633 1.17 riastrad
634 1.16 riastrad desc->td_tdqc = htole32(tdqc);
635 1.16 riastrad desc->td_tdqs = htole32(tdqs);
636 1.16 riastrad desc->td_tdqa = htole32(tdqa);
637 1.1 riastrad
638 1.16 riastrad if (task->ct_flags & TASK_KEY) {
639 1.16 riastrad bus_dmamap_t keymap = task->ct_keymap;
640 1.16 riastrad KASSERT(keymap->dm_nsegs == 1);
641 1.16 riastrad desc->td_keydesc = htole32(keymap->dm_segs[0].ds_addr);
642 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, keymap, 0,
643 1.16 riastrad keymap->dm_segs[0].ds_len, BUS_DMASYNC_PREWRITE);
644 1.16 riastrad }
645 1.16 riastrad if (task->ct_flags & TASK_IV) {
646 1.16 riastrad bus_dmamap_t ivmap = task->ct_ivmap;
647 1.16 riastrad KASSERT(ivmap->dm_nsegs == 1);
648 1.16 riastrad desc->td_ivdesc = htole32(ivmap->dm_segs[0].ds_addr);
649 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, ivmap, 0,
650 1.16 riastrad ivmap->dm_segs[0].ds_len, BUS_DMASYNC_PREWRITE);
651 1.16 riastrad }
652 1.16 riastrad if (task->ct_flags & TASK_CTR) {
653 1.16 riastrad bus_dmamap_t ctrmap = task->ct_ctrmap;
654 1.16 riastrad KASSERT(ctrmap->dm_nsegs == 1);
655 1.16 riastrad desc->td_ctrdesc = htole32(ctrmap->dm_segs[0].ds_addr);
656 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, ctrmap, 0,
657 1.17 riastrad ctrmap->dm_segs[0].ds_len, BUS_DMASYNC_PREREAD);
658 1.16 riastrad }
659 1.16 riastrad
660 1.16 riastrad if (task->ct_flags & TASK_BYTES)
661 1.16 riastrad desc->td_datalen = htole32(nbytes);
662 1.16 riastrad else
663 1.16 riastrad desc->td_datalen = htole32(nbytes/4);
664 1.16 riastrad
665 1.16 riastrad if (task->ct_flags & TASK_SRC) {
666 1.16 riastrad bus_dmamap_t srcmap = task->ct_srcmap;
667 1.16 riastrad KASSERT(srcmap->dm_mapsize == task->ct_dstmap->dm_mapsize);
668 1.17 riastrad error = sun8i_crypto_task_scatter(task, desc->td_src, srcmap,
669 1.16 riastrad nbytes);
670 1.16 riastrad if (error)
671 1.16 riastrad return error;
672 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, srcmap, 0, nbytes,
673 1.16 riastrad BUS_DMASYNC_PREWRITE);
674 1.16 riastrad }
675 1.1 riastrad
676 1.17 riastrad error = sun8i_crypto_task_scatter(task, desc->td_dst, task->ct_dstmap,
677 1.16 riastrad nbytes);
678 1.16 riastrad if (error)
679 1.17 riastrad goto out;
680 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_dstmap, 0, nbytes,
681 1.16 riastrad BUS_DMASYNC_PREREAD);
682 1.1 riastrad
683 1.16 riastrad task->ct_nbytes = nbytes;
684 1.1 riastrad
685 1.16 riastrad /* Success! */
686 1.17 riastrad error = 0;
687 1.17 riastrad
688 1.17 riastrad out: SDT_PROBE6(sdt, sun8i_crypto, task, load,
689 1.17 riastrad task, tdqc, tdqs, tdqa, desc, error);
690 1.17 riastrad return error;
691 1.1 riastrad }
692 1.1 riastrad
693 1.16 riastrad /*
694 1.17 riastrad * sun8i_crypto_task_scatter(task, adrlen, map, nbytes)
695 1.16 riastrad *
696 1.16 riastrad * Set up a task's scatter/gather vector -- src or dst -- with the
697 1.16 riastrad * given DMA map for a transfer of nbytes. May fail if input is
698 1.16 riastrad * inadequately aligned.
699 1.16 riastrad */
700 1.16 riastrad static int
701 1.17 riastrad sun8i_crypto_task_scatter(struct sun8i_crypto_task *task,
702 1.17 riastrad struct sun8i_crypto_adrlen *adrlen, bus_dmamap_t map,
703 1.16 riastrad uint32_t nbytes __diagused)
704 1.1 riastrad {
705 1.1 riastrad uint32_t total __diagused = 0;
706 1.1 riastrad unsigned i;
707 1.1 riastrad
708 1.17 riastrad /*
709 1.17 riastrad * Verify that the alignment is correct and initialize the
710 1.17 riastrad * scatter/gather vector.
711 1.17 riastrad */
712 1.1 riastrad KASSERT(map->dm_nsegs <= SUN8I_CRYPTO_MAXSEGS);
713 1.1 riastrad for (i = 0; i < map->dm_nsegs; i++) {
714 1.17 riastrad if ((map->dm_segs[i].ds_addr % 4) |
715 1.17 riastrad (map->dm_segs[i].ds_len % 4)) {
716 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, task, misaligned,
717 1.17 riastrad task,
718 1.17 riastrad map->dm_segs[i].ds_addr,
719 1.17 riastrad map->dm_segs[i].ds_len);
720 1.17 riastrad return EINVAL;
721 1.17 riastrad }
722 1.1 riastrad KASSERT(map->dm_segs[i].ds_addr <= UINT32_MAX);
723 1.1 riastrad KASSERT(map->dm_segs[i].ds_len <= UINT32_MAX - total);
724 1.1 riastrad adrlen[i].adr = htole32(map->dm_segs[i].ds_addr);
725 1.1 riastrad adrlen[i].len = htole32(map->dm_segs[i].ds_len/4);
726 1.1 riastrad total += map->dm_segs[i].ds_len;
727 1.1 riastrad }
728 1.1 riastrad
729 1.17 riastrad /* Set the remainder to zero. */
730 1.1 riastrad for (; i < SUN8I_CRYPTO_MAXSEGS; i++) {
731 1.17 riastrad adrlen[i].adr = 0;
732 1.17 riastrad adrlen[i].len = 0;
733 1.1 riastrad }
734 1.1 riastrad
735 1.16 riastrad /* Verify the total size matches the transfer length. */
736 1.16 riastrad KASSERT(total == nbytes);
737 1.16 riastrad
738 1.16 riastrad /* Success! */
739 1.16 riastrad return 0;
740 1.1 riastrad }
741 1.1 riastrad
742 1.1 riastrad /*
743 1.16 riastrad * sun8i_crypto_task_load_trng(task, nbytes)
744 1.1 riastrad *
745 1.16 riastrad * Set up the task descriptor for a transfer of nbytes from the
746 1.16 riastrad * TRNG.
747 1.1 riastrad */
748 1.1 riastrad static int
749 1.16 riastrad sun8i_crypto_task_load_trng(struct sun8i_crypto_softc *sc,
750 1.16 riastrad struct sun8i_crypto_task *task, uint32_t nbytes)
751 1.1 riastrad {
752 1.1 riastrad uint32_t tdqc = 0;
753 1.1 riastrad
754 1.16 riastrad /* Caller must provide dst only. */
755 1.16 riastrad KASSERT((task->ct_flags & TASK_KEY) == 0);
756 1.16 riastrad KASSERT((task->ct_flags & TASK_IV) == 0);
757 1.16 riastrad KASSERT((task->ct_flags & TASK_CTR) == 0);
758 1.16 riastrad KASSERT((task->ct_flags & TASK_SRC) == 0);
759 1.1 riastrad
760 1.1 riastrad /* Set up the task descriptor queue control words. */
761 1.1 riastrad tdqc |= __SHIFTIN(SUN8I_CRYPTO_TDQC_METHOD_TRNG,
762 1.1 riastrad SUN8I_CRYPTO_TDQC_METHOD);
763 1.1 riastrad
764 1.16 riastrad /* Fill in the descriptor. */
765 1.16 riastrad return sun8i_crypto_task_load(sc, task, nbytes, tdqc, 0, 0);
766 1.1 riastrad }
767 1.1 riastrad
768 1.1 riastrad static int
769 1.16 riastrad sun8i_crypto_task_load_aesecb(struct sun8i_crypto_softc *sc,
770 1.1 riastrad struct sun8i_crypto_task *task,
771 1.16 riastrad uint32_t nbytes, uint32_t keysize, uint32_t dir)
772 1.1 riastrad {
773 1.1 riastrad uint32_t tdqc = 0, tdqs = 0;
774 1.1 riastrad
775 1.16 riastrad /* Caller must provide key, src, and dst only. */
776 1.16 riastrad KASSERT(task->ct_flags & TASK_KEY);
777 1.16 riastrad KASSERT((task->ct_flags & TASK_IV) == 0);
778 1.16 riastrad KASSERT((task->ct_flags & TASK_CTR) == 0);
779 1.16 riastrad KASSERT(task->ct_flags & TASK_SRC);
780 1.1 riastrad
781 1.1 riastrad /* Set up the task descriptor queue control word. */
782 1.1 riastrad tdqc |= __SHIFTIN(SUN8I_CRYPTO_TDQC_METHOD_AES,
783 1.1 riastrad SUN8I_CRYPTO_TDQC_METHOD);
784 1.17 riastrad tdqc |= __SHIFTIN(dir, SUN8I_CRYPTO_TDQC_OP_DIR);
785 1.16 riastrad
786 1.16 riastrad #ifdef DIAGNOSTIC
787 1.16 riastrad switch (keysize) {
788 1.16 riastrad case SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128:
789 1.16 riastrad KASSERT(task->ct_keymap->dm_segs[0].ds_len == 16);
790 1.16 riastrad break;
791 1.16 riastrad case SUN8I_CRYPTO_TDQS_AES_KEYSIZE_192:
792 1.16 riastrad KASSERT(task->ct_keymap->dm_segs[0].ds_len == 24);
793 1.16 riastrad break;
794 1.16 riastrad case SUN8I_CRYPTO_TDQS_AES_KEYSIZE_256:
795 1.16 riastrad KASSERT(task->ct_keymap->dm_segs[0].ds_len == 32);
796 1.16 riastrad break;
797 1.16 riastrad }
798 1.16 riastrad #endif
799 1.1 riastrad
800 1.1 riastrad /* Set up the symmetric control word. */
801 1.1 riastrad tdqs |= __SHIFTIN(SUN8I_CRYPTO_TDQS_SKEY_SELECT_SS_KEYx,
802 1.1 riastrad SUN8I_CRYPTO_TDQS_SKEY_SELECT);
803 1.1 riastrad tdqs |= __SHIFTIN(SUN8I_CRYPTO_TDQS_OP_MODE_ECB,
804 1.1 riastrad SUN8I_CRYPTO_TDQS_OP_MODE);
805 1.16 riastrad tdqs |= __SHIFTIN(keysize, SUN8I_CRYPTO_TDQS_AES_KEYSIZE);
806 1.1 riastrad
807 1.16 riastrad /* Fill in the descriptor. */
808 1.16 riastrad return sun8i_crypto_task_load(sc, task, nbytes, tdqc, tdqs, 0);
809 1.1 riastrad }
810 1.1 riastrad
811 1.16 riastrad /*
812 1.16 riastrad * sun8i_crypto_submit(sc, task)
813 1.16 riastrad *
814 1.16 riastrad * Submit a task to the crypto engine after it has been loaded
815 1.16 riastrad * with sun8i_crypto_task_load. On success, guarantees to
816 1.16 riastrad * eventually call the task's callback.
817 1.16 riastrad */
818 1.1 riastrad static int
819 1.1 riastrad sun8i_crypto_submit(struct sun8i_crypto_softc *sc,
820 1.1 riastrad struct sun8i_crypto_task *task)
821 1.1 riastrad {
822 1.1 riastrad unsigned i, retries = 0;
823 1.1 riastrad uint32_t icr;
824 1.1 riastrad int error = 0;
825 1.1 riastrad
826 1.1 riastrad /* One at a time at the device registers, please. */
827 1.1 riastrad mutex_enter(&sc->sc_lock);
828 1.1 riastrad
829 1.1 riastrad /* Find a channel. */
830 1.1 riastrad for (i = 0; i < SUN8I_CRYPTO_NCHAN; i++) {
831 1.1 riastrad if (sc->sc_chan[i].cc_task == NULL)
832 1.1 riastrad break;
833 1.1 riastrad }
834 1.1 riastrad if (i == SUN8I_CRYPTO_NCHAN) {
835 1.1 riastrad device_printf(sc->sc_dev, "no free channels\n");
836 1.1 riastrad error = ERESTART;
837 1.1 riastrad goto out;
838 1.1 riastrad }
839 1.1 riastrad
840 1.1 riastrad /*
841 1.1 riastrad * Set the channel id. Caller is responsible for setting up
842 1.1 riastrad * all other parts of the descriptor.
843 1.1 riastrad */
844 1.1 riastrad task->ct_desc->td_cid = htole32(i);
845 1.1 riastrad
846 1.16 riastrad /*
847 1.16 riastrad * Prepare to send the descriptor to the device by DMA.
848 1.16 riastrad * Matches POSTWRITE in sun8i_crypto_chan_done.
849 1.16 riastrad */
850 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_descmap, 0,
851 1.1 riastrad sizeof(*task->ct_desc), BUS_DMASYNC_PREWRITE);
852 1.1 riastrad
853 1.1 riastrad /* Confirm we're ready to go. */
854 1.1 riastrad if (sun8i_crypto_read(sc, SUN8I_CRYPTO_TLR) & SUN8I_CRYPTO_TLR_LOAD) {
855 1.1 riastrad device_printf(sc->sc_dev, "TLR not clear\n");
856 1.1 riastrad error = EIO;
857 1.1 riastrad goto out;
858 1.1 riastrad }
859 1.1 riastrad
860 1.1 riastrad /* Enable interrupts for this channel. */
861 1.1 riastrad icr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ICR);
862 1.1 riastrad icr |= __SHIFTIN(SUN8I_CRYPTO_ICR_INTR_EN_CHAN(i),
863 1.1 riastrad SUN8I_CRYPTO_ICR_INTR_EN);
864 1.1 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_ICR, icr);
865 1.1 riastrad
866 1.1 riastrad /* Set the task descriptor queue address. */
867 1.1 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_TDQ,
868 1.16 riastrad task->ct_descmap->dm_segs[0].ds_addr);
869 1.1 riastrad
870 1.1 riastrad /* Notify the engine to load it, and wait for acknowledgement. */
871 1.1 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_TLR, SUN8I_CRYPTO_TLR_LOAD);
872 1.1 riastrad while (sun8i_crypto_read(sc, SUN8I_CRYPTO_TLR) & SUN8I_CRYPTO_TLR_LOAD)
873 1.1 riastrad {
874 1.1 riastrad /*
875 1.1 riastrad * XXX Timeout pulled from arse. Is it even important
876 1.1 riastrad * to wait here?
877 1.1 riastrad */
878 1.1 riastrad if (++retries == 1000) {
879 1.1 riastrad device_printf(sc->sc_dev, "TLR didn't clear: %08x\n",
880 1.1 riastrad sun8i_crypto_read(sc, SUN8I_CRYPTO_TLR));
881 1.1 riastrad /*
882 1.1 riastrad * Hope it clears eventually; if not, we'll
883 1.1 riastrad * time out.
884 1.1 riastrad */
885 1.1 riastrad break;
886 1.1 riastrad }
887 1.1 riastrad DELAY(1);
888 1.1 riastrad }
889 1.1 riastrad
890 1.16 riastrad /*
891 1.16 riastrad * Loaded up and ready to go. Start a timer ticking if it's
892 1.16 riastrad * not already.
893 1.16 riastrad */
894 1.1 riastrad sc->sc_chan[i].cc_task = task;
895 1.14 maxv sc->sc_chan[i].cc_starttime = getticks();
896 1.16 riastrad if (!callout_pending(&sc->sc_timeout))
897 1.16 riastrad callout_schedule(&sc->sc_timeout, SUN8I_CRYPTO_TIMEOUT);
898 1.1 riastrad
899 1.1 riastrad /* XXX Consider polling if cold to get entropy earlier. */
900 1.1 riastrad
901 1.1 riastrad out: /* Done! */
902 1.17 riastrad if (error)
903 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, engine, submit__failure,
904 1.17 riastrad sc, task, error);
905 1.17 riastrad else
906 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, engine, submit__success,
907 1.17 riastrad sc, task, i);
908 1.1 riastrad mutex_exit(&sc->sc_lock);
909 1.1 riastrad return error;
910 1.1 riastrad }
911 1.1 riastrad
912 1.16 riastrad /*
913 1.16 riastrad * sun8i_crypto_timeout(cookie)
914 1.16 riastrad *
915 1.16 riastrad * Timeout handler. Schedules work in a thread to cancel all
916 1.16 riastrad * pending tasks that were started long enough ago we're bored of
917 1.16 riastrad * waiting for them, and reschedules another timeout unless the
918 1.16 riastrad * channels are all idle.
919 1.16 riastrad */
920 1.1 riastrad static void
921 1.1 riastrad sun8i_crypto_timeout(void *cookie)
922 1.1 riastrad {
923 1.1 riastrad struct sun8i_crypto_softc *sc = cookie;
924 1.1 riastrad unsigned i;
925 1.1 riastrad
926 1.1 riastrad mutex_enter(&sc->sc_lock);
927 1.1 riastrad
928 1.1 riastrad /* Check whether there are any tasks pending. */
929 1.1 riastrad for (i = 0; i < SUN8I_CRYPTO_NCHAN; i++) {
930 1.1 riastrad if (sc->sc_chan[i].cc_task)
931 1.1 riastrad break;
932 1.1 riastrad }
933 1.1 riastrad if (i == SUN8I_CRYPTO_NCHAN)
934 1.1 riastrad /* None pending, so nothing to do. */
935 1.1 riastrad goto out;
936 1.1 riastrad
937 1.1 riastrad /*
938 1.1 riastrad * Schedule the worker to check for timeouts, and schedule
939 1.1 riastrad * another timeout in case we need it.
940 1.1 riastrad */
941 1.1 riastrad sun8i_crypto_schedule_worker(sc);
942 1.1 riastrad callout_schedule(&sc->sc_timeout, SUN8I_CRYPTO_TIMEOUT);
943 1.1 riastrad
944 1.1 riastrad out: mutex_exit(&sc->sc_lock);
945 1.1 riastrad }
946 1.1 riastrad
947 1.16 riastrad /*
948 1.16 riastrad * sun8i_crypto_intr(cookie)
949 1.16 riastrad *
950 1.16 riastrad * Device interrupt handler. Find what channels have completed,
951 1.16 riastrad * whether with success or with failure, and schedule work in
952 1.16 riastrad * thread context to invoke the appropriate callbacks.
953 1.16 riastrad */
954 1.1 riastrad static int
955 1.1 riastrad sun8i_crypto_intr(void *cookie)
956 1.1 riastrad {
957 1.1 riastrad struct sun8i_crypto_softc *sc = cookie;
958 1.1 riastrad uint32_t isr, esr;
959 1.1 riastrad
960 1.1 riastrad mutex_enter(&sc->sc_lock);
961 1.1 riastrad
962 1.1 riastrad /*
963 1.1 riastrad * Get and acknowledge the interrupts and error status.
964 1.1 riastrad *
965 1.1 riastrad * XXX Data sheet says the error status register is read-only,
966 1.1 riastrad * but then advises writing 1 to bit x1xx (keysram access error
967 1.1 riastrad * for AES, SUN8I_CRYPTO_ESR_KEYSRAMERR) to clear it. What do?
968 1.1 riastrad */
969 1.1 riastrad isr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ISR);
970 1.1 riastrad esr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ESR);
971 1.1 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_ISR, isr);
972 1.8 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_ESR, esr);
973 1.1 riastrad
974 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, engine, intr, sc, isr, esr);
975 1.17 riastrad
976 1.1 riastrad /* Start the worker if necessary. */
977 1.1 riastrad sun8i_crypto_schedule_worker(sc);
978 1.1 riastrad
979 1.1 riastrad /* Tell the worker what to do. */
980 1.1 riastrad sc->sc_done |= __SHIFTOUT(isr, SUN8I_CRYPTO_ISR_DONE);
981 1.1 riastrad sc->sc_esr |= esr;
982 1.1 riastrad
983 1.1 riastrad mutex_exit(&sc->sc_lock);
984 1.1 riastrad
985 1.1 riastrad return __SHIFTOUT(isr, SUN8I_CRYPTO_ISR_DONE) != 0;
986 1.1 riastrad }
987 1.1 riastrad
988 1.16 riastrad /*
989 1.16 riastrad * sun8i_crypto_schedule_worker(sc)
990 1.16 riastrad *
991 1.16 riastrad * Ensure that crypto engine thread context work to invoke task
992 1.16 riastrad * callbacks will run promptly. Idempotent.
993 1.16 riastrad */
994 1.1 riastrad static void
995 1.1 riastrad sun8i_crypto_schedule_worker(struct sun8i_crypto_softc *sc)
996 1.1 riastrad {
997 1.1 riastrad
998 1.1 riastrad KASSERT(mutex_owned(&sc->sc_lock));
999 1.1 riastrad
1000 1.1 riastrad /* Start the worker if necessary. */
1001 1.1 riastrad if (!sc->sc_work_pending) {
1002 1.1 riastrad workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL);
1003 1.1 riastrad sc->sc_work_pending = true;
1004 1.1 riastrad }
1005 1.1 riastrad }
1006 1.1 riastrad
1007 1.16 riastrad /*
1008 1.16 riastrad * sun8i_crypto_worker(wk, cookie)
1009 1.16 riastrad *
1010 1.16 riastrad * Thread-context worker: Invoke all task callbacks for which the
1011 1.16 riastrad * device has notified us of completion or for which we gave up
1012 1.16 riastrad * waiting.
1013 1.16 riastrad */
1014 1.1 riastrad static void
1015 1.1 riastrad sun8i_crypto_worker(struct work *wk, void *cookie)
1016 1.1 riastrad {
1017 1.1 riastrad struct sun8i_crypto_softc *sc = cookie;
1018 1.1 riastrad uint32_t done, esr, esr_chan;
1019 1.1 riastrad unsigned i, now;
1020 1.1 riastrad int error;
1021 1.1 riastrad
1022 1.1 riastrad /*
1023 1.1 riastrad * Acquire the lock. Note: We will be releasing and
1024 1.1 riastrad * reacquiring it throughout the loop.
1025 1.1 riastrad */
1026 1.1 riastrad mutex_enter(&sc->sc_lock);
1027 1.1 riastrad
1028 1.1 riastrad /* Acknowledge the work. */
1029 1.1 riastrad KASSERT(sc->sc_work_pending);
1030 1.1 riastrad sc->sc_work_pending = false;
1031 1.1 riastrad
1032 1.1 riastrad /*
1033 1.1 riastrad * Claim the done mask and error status once; we will be
1034 1.1 riastrad * releasing and reacquiring the lock for the callbacks, so
1035 1.1 riastrad * they may change.
1036 1.1 riastrad */
1037 1.1 riastrad done = sc->sc_done;
1038 1.1 riastrad esr = sc->sc_esr;
1039 1.1 riastrad sc->sc_done = 0;
1040 1.1 riastrad sc->sc_esr = 0;
1041 1.1 riastrad
1042 1.1 riastrad /* Check the time to determine what's timed out. */
1043 1.14 maxv now = getticks();
1044 1.1 riastrad
1045 1.1 riastrad /* Process the channels. */
1046 1.1 riastrad for (i = 0; i < SUN8I_CRYPTO_NCHAN; i++) {
1047 1.6 riastrad /* Check whether the channel is done. */
1048 1.1 riastrad if (!ISSET(done, SUN8I_CRYPTO_ISR_DONE_CHAN(i))) {
1049 1.6 riastrad /* Nope. Do we have a task to time out? */
1050 1.1 riastrad if ((sc->sc_chan[i].cc_task != NULL) &&
1051 1.1 riastrad ((now - sc->sc_chan[i].cc_starttime) >=
1052 1.1 riastrad SUN8I_CRYPTO_TIMEOUT))
1053 1.1 riastrad sun8i_crypto_chan_done(sc, i, ETIMEDOUT);
1054 1.1 riastrad continue;
1055 1.1 riastrad }
1056 1.6 riastrad
1057 1.6 riastrad /* Channel is done. Interpret the error if any. */
1058 1.1 riastrad esr_chan = __SHIFTOUT(esr, SUN8I_CRYPTO_ESR_CHAN(i));
1059 1.1 riastrad if (esr_chan & SUN8I_CRYPTO_ESR_CHAN_ALGNOTSUP) {
1060 1.1 riastrad device_printf(sc->sc_dev, "channel %u:"
1061 1.1 riastrad " alg not supported\n", i);
1062 1.1 riastrad error = ENODEV;
1063 1.1 riastrad } else if (esr_chan & SUN8I_CRYPTO_ESR_CHAN_DATALENERR) {
1064 1.1 riastrad device_printf(sc->sc_dev, "channel %u:"
1065 1.1 riastrad " data length error\n", i);
1066 1.1 riastrad error = EIO; /* XXX */
1067 1.1 riastrad } else if (esr_chan & SUN8I_CRYPTO_ESR_CHAN_KEYSRAMERR) {
1068 1.1 riastrad device_printf(sc->sc_dev, "channel %u:"
1069 1.1 riastrad " key sram error\n", i);
1070 1.1 riastrad error = EIO; /* XXX */
1071 1.1 riastrad } else if (esr_chan != 0) {
1072 1.1 riastrad error = EIO; /* generic I/O error */
1073 1.1 riastrad } else {
1074 1.1 riastrad error = 0;
1075 1.1 riastrad }
1076 1.1 riastrad
1077 1.6 riastrad /*
1078 1.6 riastrad * Notify the task of completion. May release the lock
1079 1.6 riastrad * to invoke a callback.
1080 1.6 riastrad */
1081 1.1 riastrad sun8i_crypto_chan_done(sc, i, error);
1082 1.1 riastrad }
1083 1.1 riastrad
1084 1.1 riastrad /* All one; release the lock one last time. */
1085 1.1 riastrad mutex_exit(&sc->sc_lock);
1086 1.1 riastrad }
1087 1.1 riastrad
1088 1.16 riastrad /*
1089 1.16 riastrad * sun8i_crypto_chan_done(sc, i, error)
1090 1.16 riastrad *
1091 1.16 riastrad * Notify the callback for the task on channel i, if there is one,
1092 1.16 riastrad * of the specified error, or 0 for success.
1093 1.16 riastrad */
1094 1.1 riastrad static void
1095 1.1 riastrad sun8i_crypto_chan_done(struct sun8i_crypto_softc *sc, unsigned i, int error)
1096 1.1 riastrad {
1097 1.1 riastrad struct sun8i_crypto_task *task;
1098 1.16 riastrad uint32_t nbytes;
1099 1.1 riastrad uint32_t icr;
1100 1.1 riastrad
1101 1.1 riastrad KASSERT(mutex_owned(&sc->sc_lock));
1102 1.1 riastrad
1103 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, engine, done, sc, i, error);
1104 1.17 riastrad
1105 1.1 riastrad /* Claim the task if there is one; bail if not. */
1106 1.1 riastrad if ((task = sc->sc_chan[i].cc_task) == NULL) {
1107 1.1 riastrad device_printf(sc->sc_dev, "channel %u: no task but error=%d\n",
1108 1.1 riastrad i, error);
1109 1.1 riastrad return;
1110 1.1 riastrad }
1111 1.1 riastrad sc->sc_chan[i].cc_task = NULL;
1112 1.1 riastrad
1113 1.1 riastrad /* Disable interrupts on this channel. */
1114 1.1 riastrad icr = sun8i_crypto_read(sc, SUN8I_CRYPTO_ICR);
1115 1.1 riastrad icr &= ~__SHIFTIN(SUN8I_CRYPTO_ICR_INTR_EN_CHAN(i),
1116 1.1 riastrad SUN8I_CRYPTO_ICR_INTR_EN);
1117 1.1 riastrad sun8i_crypto_write(sc, SUN8I_CRYPTO_ICR, icr);
1118 1.1 riastrad
1119 1.16 riastrad /*
1120 1.16 riastrad * Finished sending the descriptor to the device by DMA.
1121 1.16 riastrad * Matches PREWRITE in sun8i_crypto_task_submit.
1122 1.16 riastrad */
1123 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_descmap, 0,
1124 1.1 riastrad sizeof(*task->ct_desc), BUS_DMASYNC_POSTWRITE);
1125 1.1 riastrad
1126 1.16 riastrad /*
1127 1.16 riastrad * Finished with all the other bits of DMA too. Matches
1128 1.16 riastrad * sun8i_crypto_task_load.
1129 1.16 riastrad */
1130 1.16 riastrad nbytes = task->ct_nbytes;
1131 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_dstmap, 0, nbytes,
1132 1.16 riastrad BUS_DMASYNC_POSTREAD);
1133 1.16 riastrad if (task->ct_flags & TASK_SRC)
1134 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_srcmap, 0, nbytes,
1135 1.16 riastrad BUS_DMASYNC_POSTWRITE);
1136 1.16 riastrad if (task->ct_flags & TASK_CTR)
1137 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_ctrmap, 0,
1138 1.17 riastrad task->ct_ctrmap->dm_segs[0].ds_len, BUS_DMASYNC_POSTREAD);
1139 1.16 riastrad if (task->ct_flags & TASK_IV)
1140 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_ivmap, 0,
1141 1.16 riastrad task->ct_ivmap->dm_segs[0].ds_len, BUS_DMASYNC_POSTWRITE);
1142 1.16 riastrad if (task->ct_flags & TASK_KEY)
1143 1.16 riastrad /* XXX Can we zero the bounce buffer if there is one? */
1144 1.16 riastrad bus_dmamap_sync(sc->sc_dmat, task->ct_keymap, 0,
1145 1.16 riastrad task->ct_keymap->dm_segs[0].ds_len, BUS_DMASYNC_POSTWRITE);
1146 1.16 riastrad
1147 1.1 riastrad /* Temporarily release the lock to invoke the callback. */
1148 1.1 riastrad mutex_exit(&sc->sc_lock);
1149 1.17 riastrad SDT_PROBE2(sdt, sun8i_crypto, task, done, task, error);
1150 1.1 riastrad (*task->ct_callback)(sc, task, task->ct_cookie, error);
1151 1.1 riastrad mutex_enter(&sc->sc_lock);
1152 1.1 riastrad }
1153 1.1 riastrad
1154 1.1 riastrad /*
1155 1.16 riastrad * sun8i_crypto_allocbuf(sc, size, buf, dmaflags)
1156 1.16 riastrad *
1157 1.16 riastrad * Allocate a single-segment DMA-safe buffer and map it into KVA.
1158 1.16 riastrad * May fail if dmaflags is BUS_DMA_NOWAIT.
1159 1.1 riastrad */
1160 1.1 riastrad static int
1161 1.1 riastrad sun8i_crypto_allocbuf(struct sun8i_crypto_softc *sc, size_t size,
1162 1.16 riastrad struct sun8i_crypto_buf *buf, int dmaflags)
1163 1.1 riastrad {
1164 1.1 riastrad int error;
1165 1.1 riastrad
1166 1.1 riastrad /* Allocate a DMA-safe buffer. */
1167 1.17 riastrad error = bus_dmamem_alloc(sc->sc_dmat, size, sizeof(uint32_t), 0,
1168 1.17 riastrad buf->cb_seg, __arraycount(buf->cb_seg), &buf->cb_nsegs, dmaflags);
1169 1.1 riastrad if (error)
1170 1.1 riastrad goto fail0;
1171 1.1 riastrad
1172 1.1 riastrad /* Map the buffer into kernel virtual address space. */
1173 1.1 riastrad error = bus_dmamem_map(sc->sc_dmat, buf->cb_seg, buf->cb_nsegs,
1174 1.16 riastrad size, &buf->cb_kva, dmaflags);
1175 1.1 riastrad if (error)
1176 1.1 riastrad goto fail1;
1177 1.1 riastrad
1178 1.1 riastrad /* Success! */
1179 1.1 riastrad return 0;
1180 1.1 riastrad
1181 1.16 riastrad fail2: __unused
1182 1.16 riastrad bus_dmamem_unmap(sc->sc_dmat, buf->cb_kva, size);
1183 1.1 riastrad fail1: bus_dmamem_free(sc->sc_dmat, buf->cb_seg, buf->cb_nsegs);
1184 1.1 riastrad fail0: return error;
1185 1.1 riastrad }
1186 1.1 riastrad
1187 1.16 riastrad /*
1188 1.16 riastrad * sun8i_crypto_freebuf(sc, buf)
1189 1.16 riastrad *
1190 1.16 riastrad * Unmap buf and free it.
1191 1.16 riastrad */
1192 1.1 riastrad static void
1193 1.1 riastrad sun8i_crypto_freebuf(struct sun8i_crypto_softc *sc, size_t size,
1194 1.1 riastrad struct sun8i_crypto_buf *buf)
1195 1.1 riastrad {
1196 1.1 riastrad
1197 1.1 riastrad bus_dmamem_unmap(sc->sc_dmat, buf->cb_kva, size);
1198 1.1 riastrad bus_dmamem_free(sc->sc_dmat, buf->cb_seg, buf->cb_nsegs);
1199 1.1 riastrad }
1200 1.1 riastrad
1201 1.1 riastrad /*
1202 1.16 riastrad * sun8i_crypto_rng_attach(sc)
1203 1.16 riastrad *
1204 1.16 riastrad * Attach an rndsource for the crypto engine's TRNG.
1205 1.1 riastrad */
1206 1.1 riastrad static void
1207 1.1 riastrad sun8i_crypto_rng_attach(struct sun8i_crypto_softc *sc)
1208 1.1 riastrad {
1209 1.1 riastrad device_t self = sc->sc_dev;
1210 1.1 riastrad struct sun8i_crypto_rng *rng = &sc->sc_rng;
1211 1.16 riastrad struct sun8i_crypto_task *task;
1212 1.1 riastrad int error;
1213 1.1 riastrad
1214 1.1 riastrad /* Preallocate a buffer to reuse. */
1215 1.16 riastrad error = sun8i_crypto_allocbuf(sc, SUN8I_CRYPTO_RNGBYTES, &rng->cr_buf,
1216 1.16 riastrad BUS_DMA_WAITOK);
1217 1.17 riastrad if (error) {
1218 1.17 riastrad aprint_error_dev(self, "failed to allocate RNG buffer: %d\n",
1219 1.17 riastrad error);
1220 1.1 riastrad goto fail0;
1221 1.17 riastrad }
1222 1.1 riastrad
1223 1.1 riastrad /* Create a task to reuse. */
1224 1.16 riastrad task = rng->cr_task = sun8i_crypto_task_get(sc, sun8i_crypto_rng_done,
1225 1.16 riastrad rng, PR_WAITOK);
1226 1.16 riastrad if (rng->cr_task == NULL) {
1227 1.17 riastrad aprint_error_dev(self, "failed to allocate RNG task\n");
1228 1.16 riastrad error = ENOMEM;
1229 1.1 riastrad goto fail1;
1230 1.16 riastrad }
1231 1.16 riastrad
1232 1.16 riastrad /* Preload the destination map. */
1233 1.16 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_dstmap,
1234 1.16 riastrad rng->cr_buf.cb_kva, SUN8I_CRYPTO_RNGBYTES, NULL, BUS_DMA_NOWAIT);
1235 1.17 riastrad if (error) {
1236 1.17 riastrad aprint_error_dev(self, "failed to load RNG buffer: %d\n",
1237 1.17 riastrad error);
1238 1.16 riastrad goto fail2;
1239 1.17 riastrad }
1240 1.1 riastrad
1241 1.1 riastrad /*
1242 1.1 riastrad * Attach the rndsource. This is _not_ marked as RND_TYPE_RNG
1243 1.1 riastrad * because the output is not uniformly distributed. The bits
1244 1.1 riastrad * are heavily weighted toward 0 or 1, at different times, and
1245 1.1 riastrad * I haven't scienced a satisfactory story out of it yet.
1246 1.1 riastrad */
1247 1.1 riastrad rndsource_setcb(&rng->cr_rndsource, sun8i_crypto_rng_get, sc);
1248 1.1 riastrad rnd_attach_source(&rng->cr_rndsource, device_xname(self),
1249 1.1 riastrad RND_TYPE_UNKNOWN,
1250 1.1 riastrad RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_VALUE|RND_FLAG_HASCB);
1251 1.1 riastrad
1252 1.1 riastrad /* Success! */
1253 1.1 riastrad return;
1254 1.1 riastrad
1255 1.16 riastrad fail3: __unused
1256 1.16 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_dstmap);
1257 1.16 riastrad fail2: sun8i_crypto_task_put(sc, task);
1258 1.1 riastrad fail1: sun8i_crypto_freebuf(sc, SUN8I_CRYPTO_RNGBYTES, &rng->cr_buf);
1259 1.17 riastrad fail0: return;
1260 1.1 riastrad }
1261 1.1 riastrad
1262 1.16 riastrad /*
1263 1.16 riastrad * sun8i_crypto_rng_get(nbytes, cookie)
1264 1.16 riastrad *
1265 1.16 riastrad * On-demand rndsource callback: try to gather nbytes of entropy
1266 1.16 riastrad * and enter them into the pool ASAP.
1267 1.16 riastrad */
1268 1.1 riastrad static void
1269 1.1 riastrad sun8i_crypto_rng_get(size_t nbytes, void *cookie)
1270 1.1 riastrad {
1271 1.1 riastrad struct sun8i_crypto_softc *sc = cookie;
1272 1.1 riastrad struct sun8i_crypto_rng *rng = &sc->sc_rng;
1273 1.16 riastrad struct sun8i_crypto_task *task = rng->cr_task;
1274 1.1 riastrad bool pending;
1275 1.1 riastrad int error;
1276 1.1 riastrad
1277 1.1 riastrad /*
1278 1.1 riastrad * Test and set the RNG-pending flag. If it's already in
1279 1.1 riastrad * progress, nothing to do here.
1280 1.1 riastrad */
1281 1.1 riastrad mutex_enter(&sc->sc_lock);
1282 1.1 riastrad pending = rng->cr_pending;
1283 1.1 riastrad rng->cr_pending = true;
1284 1.1 riastrad mutex_exit(&sc->sc_lock);
1285 1.1 riastrad if (pending)
1286 1.1 riastrad return;
1287 1.1 riastrad
1288 1.16 riastrad /* Load the task descriptor. */
1289 1.16 riastrad error = sun8i_crypto_task_load_trng(sc, task, SUN8I_CRYPTO_RNGBYTES);
1290 1.16 riastrad if (error)
1291 1.16 riastrad goto fail;
1292 1.1 riastrad
1293 1.16 riastrad /* Submit! */
1294 1.16 riastrad error = sun8i_crypto_submit(sc, task);
1295 1.1 riastrad if (error)
1296 1.1 riastrad goto fail;
1297 1.1 riastrad
1298 1.1 riastrad /* All done! */
1299 1.1 riastrad return;
1300 1.1 riastrad
1301 1.1 riastrad fail: mutex_enter(&sc->sc_lock);
1302 1.1 riastrad rng->cr_pending = false;
1303 1.1 riastrad mutex_exit(&sc->sc_lock);
1304 1.1 riastrad }
1305 1.1 riastrad
1306 1.1 riastrad static void
1307 1.1 riastrad sun8i_crypto_rng_done(struct sun8i_crypto_softc *sc,
1308 1.1 riastrad struct sun8i_crypto_task *task, void *cookie, int error)
1309 1.1 riastrad {
1310 1.1 riastrad struct sun8i_crypto_rng *rng = cookie;
1311 1.1 riastrad uint8_t *buf = rng->cr_buf.cb_kva;
1312 1.1 riastrad uint32_t entropybits;
1313 1.1 riastrad
1314 1.1 riastrad KASSERT(rng == &sc->sc_rng);
1315 1.1 riastrad
1316 1.1 riastrad /* If anything went wrong, forget about it. */
1317 1.1 riastrad if (error)
1318 1.1 riastrad goto out;
1319 1.1 riastrad
1320 1.1 riastrad /*
1321 1.1 riastrad * This TRNG has quite low entropy at best. But if it fails a
1322 1.1 riastrad * repeated output test, then assume it's busted.
1323 1.1 riastrad */
1324 1.9 riastrad CTASSERT(SUN8I_CRYPTO_RNGBYTES <= UINT32_MAX/NBBY);
1325 1.9 riastrad entropybits = (NBBY*SUN8I_CRYPTO_RNGBYTES)/SUN8I_CRYPTO_RNGENTROPY;
1326 1.1 riastrad if (consttime_memequal(buf, buf + SUN8I_CRYPTO_RNGBYTES/2,
1327 1.1 riastrad SUN8I_CRYPTO_RNGBYTES/2)) {
1328 1.1 riastrad device_printf(sc->sc_dev, "failed repeated output test\n");
1329 1.1 riastrad entropybits = 0;
1330 1.1 riastrad }
1331 1.1 riastrad
1332 1.10 riastrad /*
1333 1.10 riastrad * Actually we don't believe in any of the entropy until this
1334 1.10 riastrad * device has had more scrutiny.
1335 1.10 riastrad */
1336 1.10 riastrad entropybits = 0;
1337 1.10 riastrad
1338 1.1 riastrad /* Success! Enter and erase the data. */
1339 1.1 riastrad rnd_add_data(&rng->cr_rndsource, buf, SUN8I_CRYPTO_RNGBYTES,
1340 1.1 riastrad entropybits);
1341 1.1 riastrad explicit_memset(buf, 0, SUN8I_CRYPTO_RNGBYTES);
1342 1.1 riastrad
1343 1.1 riastrad out: /* Done -- clear the RNG-pending flag. */
1344 1.1 riastrad mutex_enter(&sc->sc_lock);
1345 1.1 riastrad rng->cr_pending = false;
1346 1.1 riastrad mutex_exit(&sc->sc_lock);
1347 1.1 riastrad }
1348 1.1 riastrad
1349 1.1 riastrad /*
1350 1.1 riastrad * Self-test
1351 1.1 riastrad */
1352 1.1 riastrad
1353 1.3 riastrad static const uint8_t selftest_input[16];
1354 1.3 riastrad static const uint8_t selftest_key[16];
1355 1.3 riastrad static const uint8_t selftest_output[16] = {
1356 1.1 riastrad 0x66,0xe9,0x4b,0xd4,0xef,0x8a,0x2c,0x3b,
1357 1.1 riastrad 0x88,0x4c,0xfa,0x59,0xca,0x34,0x2b,0x2e,
1358 1.1 riastrad };
1359 1.1 riastrad
1360 1.1 riastrad static void
1361 1.1 riastrad sun8i_crypto_selftest(device_t self)
1362 1.1 riastrad {
1363 1.16 riastrad const size_t keybytes = sizeof selftest_key;
1364 1.16 riastrad const size_t nbytes = sizeof selftest_input;
1365 1.1 riastrad struct sun8i_crypto_softc *sc = device_private(self);
1366 1.1 riastrad struct sun8i_crypto_selftest *selftest = &sc->sc_selftest;
1367 1.16 riastrad struct sun8i_crypto_task *task;
1368 1.1 riastrad int error;
1369 1.1 riastrad
1370 1.1 riastrad CTASSERT(sizeof selftest_input == sizeof selftest_output);
1371 1.1 riastrad
1372 1.1 riastrad /* Allocate an input buffer. */
1373 1.16 riastrad error = sun8i_crypto_allocbuf(sc, nbytes, &selftest->cs_in,
1374 1.16 riastrad BUS_DMA_WAITOK);
1375 1.1 riastrad if (error)
1376 1.1 riastrad goto fail0;
1377 1.1 riastrad
1378 1.1 riastrad /* Allocate a key buffer. */
1379 1.16 riastrad error = sun8i_crypto_allocbuf(sc, keybytes, &selftest->cs_key,
1380 1.16 riastrad BUS_DMA_WAITOK);
1381 1.1 riastrad if (error)
1382 1.1 riastrad goto fail1;
1383 1.1 riastrad
1384 1.1 riastrad /* Allocate an output buffer. */
1385 1.16 riastrad error = sun8i_crypto_allocbuf(sc, nbytes, &selftest->cs_out,
1386 1.16 riastrad BUS_DMA_WAITOK);
1387 1.1 riastrad if (error)
1388 1.1 riastrad goto fail2;
1389 1.1 riastrad
1390 1.1 riastrad /* Allocate a task descriptor. */
1391 1.16 riastrad task = selftest->cs_task = sun8i_crypto_task_get(sc,
1392 1.16 riastrad sun8i_crypto_selftest_done, selftest, PR_WAITOK);
1393 1.16 riastrad if (selftest->cs_task == NULL) {
1394 1.16 riastrad error = ENOMEM;
1395 1.1 riastrad goto fail3;
1396 1.16 riastrad }
1397 1.1 riastrad
1398 1.1 riastrad /* Copy the input and key into their buffers. */
1399 1.16 riastrad memcpy(selftest->cs_in.cb_kva, selftest_input, nbytes);
1400 1.16 riastrad memcpy(selftest->cs_key.cb_kva, selftest_key, keybytes);
1401 1.1 riastrad
1402 1.16 riastrad /* Load the key, src, and dst for DMA transfers. */
1403 1.16 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_keymap,
1404 1.16 riastrad selftest->cs_key.cb_kva, keybytes, NULL, BUS_DMA_WAITOK);
1405 1.16 riastrad if (error)
1406 1.16 riastrad goto fail4;
1407 1.16 riastrad task->ct_flags |= TASK_KEY;
1408 1.16 riastrad
1409 1.16 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_srcmap,
1410 1.16 riastrad selftest->cs_in.cb_kva, nbytes, NULL, BUS_DMA_WAITOK);
1411 1.16 riastrad if (error)
1412 1.16 riastrad goto fail5;
1413 1.16 riastrad task->ct_flags |= TASK_SRC;
1414 1.16 riastrad
1415 1.16 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_dstmap,
1416 1.16 riastrad selftest->cs_out.cb_kva, nbytes, NULL, BUS_DMA_WAITOK);
1417 1.16 riastrad if (error)
1418 1.16 riastrad goto fail6;
1419 1.1 riastrad
1420 1.1 riastrad /* Set up the task descriptor. */
1421 1.17 riastrad error = sun8i_crypto_task_load_aesecb(sc, task, nbytes,
1422 1.16 riastrad SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128, SUN8I_CRYPTO_TDQC_OP_DIR_ENC);
1423 1.17 riastrad if (error)
1424 1.17 riastrad goto fail7;
1425 1.1 riastrad
1426 1.16 riastrad /* Submit! */
1427 1.16 riastrad error = sun8i_crypto_submit(sc, task);
1428 1.1 riastrad if (error)
1429 1.16 riastrad goto fail7;
1430 1.1 riastrad
1431 1.1 riastrad device_printf(sc->sc_dev, "AES-128 self-test initiated\n");
1432 1.1 riastrad
1433 1.1 riastrad /* Success! */
1434 1.1 riastrad return;
1435 1.1 riastrad
1436 1.16 riastrad fail7: bus_dmamap_unload(sc->sc_dmat, task->ct_dstmap);
1437 1.16 riastrad fail6: bus_dmamap_unload(sc->sc_dmat, task->ct_srcmap);
1438 1.16 riastrad fail5: bus_dmamap_unload(sc->sc_dmat, task->ct_keymap);
1439 1.16 riastrad fail4: sun8i_crypto_task_put(sc, task);
1440 1.16 riastrad fail3: sun8i_crypto_freebuf(sc, nbytes, &selftest->cs_out);
1441 1.16 riastrad fail2: sun8i_crypto_freebuf(sc, keybytes, &selftest->cs_key);
1442 1.16 riastrad fail1: sun8i_crypto_freebuf(sc, nbytes, &selftest->cs_in);
1443 1.1 riastrad fail0: aprint_error_dev(self, "failed to run self-test, error=%d\n", error);
1444 1.1 riastrad }
1445 1.1 riastrad
1446 1.3 riastrad static bool
1447 1.3 riastrad sun8i_crypto_selftest_check(struct sun8i_crypto_softc *sc, const char *title,
1448 1.3 riastrad size_t n, const void *expected, const void *actual)
1449 1.3 riastrad {
1450 1.3 riastrad const uint8_t *e = expected;
1451 1.3 riastrad const uint8_t *a = actual;
1452 1.3 riastrad size_t i;
1453 1.3 riastrad
1454 1.3 riastrad if (memcmp(e, a, n) == 0)
1455 1.3 riastrad return true;
1456 1.3 riastrad
1457 1.3 riastrad device_printf(sc->sc_dev, "self-test: %s\n", title);
1458 1.3 riastrad printf("expected: ");
1459 1.3 riastrad for (i = 0; i < n; i++)
1460 1.3 riastrad printf("%02hhx", e[i]);
1461 1.3 riastrad printf("\n");
1462 1.3 riastrad printf("actual: ");
1463 1.3 riastrad for (i = 0; i < n; i++)
1464 1.3 riastrad printf("%02hhx", a[i]);
1465 1.3 riastrad printf("\n");
1466 1.3 riastrad return false;
1467 1.3 riastrad }
1468 1.3 riastrad
1469 1.1 riastrad static void
1470 1.1 riastrad sun8i_crypto_selftest_done(struct sun8i_crypto_softc *sc,
1471 1.1 riastrad struct sun8i_crypto_task *task, void *cookie, int error)
1472 1.1 riastrad {
1473 1.16 riastrad const size_t keybytes = sizeof selftest_key;
1474 1.16 riastrad const size_t nbytes = sizeof selftest_input;
1475 1.1 riastrad struct sun8i_crypto_selftest *selftest = cookie;
1476 1.1 riastrad bool ok = true;
1477 1.1 riastrad
1478 1.1 riastrad KASSERT(selftest == &sc->sc_selftest);
1479 1.1 riastrad
1480 1.3 riastrad /* If anything went wrong, fail now. */
1481 1.1 riastrad if (error) {
1482 1.1 riastrad device_printf(sc->sc_dev, "self-test error=%d\n", error);
1483 1.1 riastrad goto out;
1484 1.1 riastrad }
1485 1.1 riastrad
1486 1.3 riastrad /*
1487 1.3 riastrad * Verify the input and key weren't clobbered, and verify the
1488 1.3 riastrad * output matches what we expect.
1489 1.3 riastrad */
1490 1.16 riastrad ok &= sun8i_crypto_selftest_check(sc, "input clobbered", nbytes,
1491 1.16 riastrad selftest_input, selftest->cs_in.cb_kva);
1492 1.16 riastrad ok &= sun8i_crypto_selftest_check(sc, "key clobbered", keybytes,
1493 1.16 riastrad selftest_key, selftest->cs_key.cb_kva);
1494 1.16 riastrad ok &= sun8i_crypto_selftest_check(sc, "output mismatch", nbytes,
1495 1.16 riastrad selftest_output, selftest->cs_out.cb_kva);
1496 1.1 riastrad
1497 1.1 riastrad /* XXX Disable the RNG and other stuff if this fails... */
1498 1.1 riastrad if (ok)
1499 1.1 riastrad device_printf(sc->sc_dev, "AES-128 self-test passed\n");
1500 1.1 riastrad
1501 1.16 riastrad out: bus_dmamap_unload(sc->sc_dmat, task->ct_dstmap);
1502 1.16 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_srcmap);
1503 1.16 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_keymap);
1504 1.16 riastrad sun8i_crypto_task_put(sc, task);
1505 1.16 riastrad sun8i_crypto_freebuf(sc, nbytes, &selftest->cs_out);
1506 1.16 riastrad sun8i_crypto_freebuf(sc, keybytes, &selftest->cs_key);
1507 1.16 riastrad sun8i_crypto_freebuf(sc, nbytes, &selftest->cs_in);
1508 1.1 riastrad }
1509 1.1 riastrad
1510 1.1 riastrad /*
1511 1.1 riastrad * Sysctl for testing
1512 1.1 riastrad */
1513 1.1 riastrad
1514 1.1 riastrad struct sun8i_crypto_userreq {
1515 1.1 riastrad kmutex_t cu_lock;
1516 1.1 riastrad kcondvar_t cu_cv;
1517 1.1 riastrad size_t cu_size;
1518 1.1 riastrad struct sun8i_crypto_buf cu_buf;
1519 1.1 riastrad struct sun8i_crypto_task *cu_task;
1520 1.1 riastrad int cu_error;
1521 1.1 riastrad bool cu_done;
1522 1.1 riastrad bool cu_cancel;
1523 1.1 riastrad };
1524 1.1 riastrad
1525 1.1 riastrad static void
1526 1.1 riastrad sun8i_crypto_sysctl_attach(struct sun8i_crypto_softc *sc)
1527 1.1 riastrad {
1528 1.1 riastrad struct sun8i_crypto_sysctl *cy = &sc->sc_sysctl;
1529 1.1 riastrad int error;
1530 1.1 riastrad
1531 1.6 riastrad /* hw.sun8icryptoN (node) */
1532 1.1 riastrad error = sysctl_createv(&cy->cy_log, 0, NULL, &cy->cy_root_node,
1533 1.1 riastrad CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
1534 1.1 riastrad SYSCTL_DESCR("sun8i crypto engine knobs"),
1535 1.1 riastrad NULL, 0, NULL, 0,
1536 1.1 riastrad CTL_HW, CTL_CREATE, CTL_EOL);
1537 1.1 riastrad if (error) {
1538 1.1 riastrad aprint_error_dev(sc->sc_dev,
1539 1.1 riastrad "failed to set up sysctl hw.%s: %d\n",
1540 1.1 riastrad device_xname(sc->sc_dev), error);
1541 1.1 riastrad return;
1542 1.1 riastrad }
1543 1.1 riastrad
1544 1.9 riastrad /* hw.sun8icryptoN.rng (`struct', 4096-byte array) */
1545 1.12 riastrad sysctl_createv(&cy->cy_log, 0, &cy->cy_root_node, &cy->cy_trng_node,
1546 1.1 riastrad CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_PRIVATE, CTLTYPE_STRUCT,
1547 1.9 riastrad "rng", SYSCTL_DESCR("Read up to 4096 bytes out of the TRNG"),
1548 1.1 riastrad &sun8i_crypto_sysctl_rng, 0, sc, 0, CTL_CREATE, CTL_EOL);
1549 1.1 riastrad if (error) {
1550 1.1 riastrad aprint_error_dev(sc->sc_dev,
1551 1.1 riastrad "failed to set up sysctl hw.%s.rng: %d\n",
1552 1.1 riastrad device_xname(sc->sc_dev), error);
1553 1.1 riastrad return;
1554 1.1 riastrad }
1555 1.1 riastrad }
1556 1.1 riastrad
1557 1.1 riastrad static int
1558 1.1 riastrad sun8i_crypto_sysctl_rng(SYSCTLFN_ARGS)
1559 1.1 riastrad {
1560 1.1 riastrad struct sysctlnode node = *rnode;
1561 1.1 riastrad struct sun8i_crypto_softc *sc = node.sysctl_data;
1562 1.1 riastrad struct sun8i_crypto_userreq *req;
1563 1.16 riastrad struct sun8i_crypto_task *task;
1564 1.1 riastrad size_t size;
1565 1.1 riastrad int error;
1566 1.1 riastrad
1567 1.1 riastrad /* If oldp == NULL, the caller wants to learn the size. */
1568 1.1 riastrad if (oldp == NULL) {
1569 1.9 riastrad *oldlenp = 4096;
1570 1.1 riastrad return 0;
1571 1.1 riastrad }
1572 1.1 riastrad
1573 1.15 riastrad /* Truncate to 4096 bytes. */
1574 1.15 riastrad size = MIN(4096, *oldlenp);
1575 1.1 riastrad if (size == 0)
1576 1.1 riastrad return 0; /* nothing to do */
1577 1.1 riastrad
1578 1.1 riastrad /* Allocate a request context. */
1579 1.1 riastrad req = kmem_alloc(sizeof(*req), KM_NOSLEEP);
1580 1.1 riastrad if (req == NULL)
1581 1.1 riastrad return ENOMEM;
1582 1.1 riastrad
1583 1.1 riastrad /* Initialize the request context. */
1584 1.1 riastrad mutex_init(&req->cu_lock, MUTEX_DEFAULT, IPL_NONE);
1585 1.1 riastrad cv_init(&req->cu_cv, "sun8isy");
1586 1.1 riastrad req->cu_size = size;
1587 1.1 riastrad req->cu_error = EIO;
1588 1.1 riastrad req->cu_done = false;
1589 1.1 riastrad req->cu_cancel = false;
1590 1.1 riastrad
1591 1.1 riastrad /* Allocate a buffer for the RNG output. */
1592 1.16 riastrad error = sun8i_crypto_allocbuf(sc, size, &req->cu_buf, BUS_DMA_NOWAIT);
1593 1.1 riastrad if (error)
1594 1.1 riastrad goto out0;
1595 1.1 riastrad
1596 1.1 riastrad /* Allocate a task. */
1597 1.16 riastrad task = req->cu_task = sun8i_crypto_task_get(sc,
1598 1.16 riastrad sun8i_crypto_sysctl_rng_done, req, PR_NOWAIT);
1599 1.16 riastrad if (task == NULL) {
1600 1.16 riastrad error = ENOMEM;
1601 1.1 riastrad goto out1;
1602 1.16 riastrad }
1603 1.1 riastrad
1604 1.1 riastrad /* Set the task up for TRNG to our buffer. */
1605 1.16 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_dstmap,
1606 1.16 riastrad req->cu_buf.cb_kva, SUN8I_CRYPTO_RNGBYTES, NULL, BUS_DMA_NOWAIT);
1607 1.16 riastrad if (error)
1608 1.16 riastrad goto out2;
1609 1.16 riastrad error = sun8i_crypto_task_load_trng(sc, task, SUN8I_CRYPTO_RNGBYTES);
1610 1.16 riastrad if (error)
1611 1.16 riastrad goto out3;
1612 1.1 riastrad
1613 1.16 riastrad /* Submit! */
1614 1.16 riastrad error = sun8i_crypto_submit(sc, task);
1615 1.4 riastrad if (error) {
1616 1.16 riastrad /* Make sure we don't restart the syscall -- just fail. */
1617 1.4 riastrad if (error == ERESTART)
1618 1.4 riastrad error = EBUSY;
1619 1.16 riastrad goto out3;
1620 1.4 riastrad }
1621 1.1 riastrad
1622 1.1 riastrad /* Wait for the request to complete. */
1623 1.1 riastrad mutex_enter(&req->cu_lock);
1624 1.1 riastrad while (!req->cu_done) {
1625 1.1 riastrad error = cv_wait_sig(&req->cu_cv, &req->cu_lock);
1626 1.1 riastrad if (error) {
1627 1.1 riastrad /*
1628 1.5 riastrad * If we finished while waiting to acquire the
1629 1.5 riastrad * lock, ignore the error and just return now.
1630 1.5 riastrad * Otherwise, notify the callback that it has
1631 1.5 riastrad * to clean up after us.
1632 1.1 riastrad */
1633 1.5 riastrad if (req->cu_done)
1634 1.5 riastrad error = 0;
1635 1.5 riastrad else
1636 1.5 riastrad req->cu_cancel = true;
1637 1.1 riastrad break;
1638 1.1 riastrad }
1639 1.1 riastrad }
1640 1.1 riastrad mutex_exit(&req->cu_lock);
1641 1.1 riastrad
1642 1.1 riastrad /*
1643 1.1 riastrad * Return early on error from cv_wait_sig, which means
1644 1.1 riastrad * interruption; the callback will clean up instead.
1645 1.1 riastrad */
1646 1.1 riastrad if (error)
1647 1.1 riastrad return error;
1648 1.1 riastrad
1649 1.1 riastrad /* Check for error from the device. */
1650 1.1 riastrad error = req->cu_error;
1651 1.1 riastrad if (error)
1652 1.16 riastrad goto out3;
1653 1.1 riastrad
1654 1.1 riastrad /* Copy out the data. */
1655 1.1 riastrad node.sysctl_data = req->cu_buf.cb_kva;
1656 1.1 riastrad node.sysctl_size = size;
1657 1.1 riastrad error = sysctl_lookup(SYSCTLFN_CALL(&node));
1658 1.1 riastrad
1659 1.1 riastrad /* Clear the buffer. */
1660 1.1 riastrad explicit_memset(req->cu_buf.cb_kva, 0, size);
1661 1.1 riastrad
1662 1.5 riastrad /* Clean up. */
1663 1.16 riastrad out3: bus_dmamap_unload(sc->sc_dmat, task->ct_dstmap);
1664 1.16 riastrad out2: sun8i_crypto_task_put(sc, task);
1665 1.1 riastrad out1: sun8i_crypto_freebuf(sc, req->cu_size, &req->cu_buf);
1666 1.1 riastrad out0: cv_destroy(&req->cu_cv);
1667 1.1 riastrad mutex_destroy(&req->cu_lock);
1668 1.7 riastrad kmem_free(req, sizeof(*req));
1669 1.1 riastrad return error;
1670 1.1 riastrad }
1671 1.1 riastrad
1672 1.1 riastrad static void
1673 1.1 riastrad sun8i_crypto_sysctl_rng_done(struct sun8i_crypto_softc *sc,
1674 1.1 riastrad struct sun8i_crypto_task *task, void *cookie, int error)
1675 1.1 riastrad {
1676 1.1 riastrad struct sun8i_crypto_userreq *req = cookie;
1677 1.1 riastrad bool cancel;
1678 1.1 riastrad
1679 1.1 riastrad /*
1680 1.1 riastrad * Notify the waiting thread of the error, and find out whether
1681 1.1 riastrad * that thread cancelled.
1682 1.1 riastrad */
1683 1.1 riastrad mutex_enter(&req->cu_lock);
1684 1.1 riastrad cancel = req->cu_cancel;
1685 1.1 riastrad req->cu_error = error;
1686 1.1 riastrad req->cu_done = true;
1687 1.1 riastrad cv_broadcast(&req->cu_cv);
1688 1.1 riastrad mutex_exit(&req->cu_lock);
1689 1.1 riastrad
1690 1.1 riastrad /*
1691 1.1 riastrad * If it wasn't cancelled, we're done -- the main thread will
1692 1.1 riastrad * clean up after itself.
1693 1.1 riastrad */
1694 1.1 riastrad if (!cancel)
1695 1.1 riastrad return;
1696 1.1 riastrad
1697 1.5 riastrad /* Clean up after the main thread cancelled. */
1698 1.16 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_dstmap);
1699 1.16 riastrad sun8i_crypto_task_put(sc, task);
1700 1.1 riastrad sun8i_crypto_freebuf(sc, req->cu_size, &req->cu_buf);
1701 1.1 riastrad cv_destroy(&req->cu_cv);
1702 1.1 riastrad mutex_destroy(&req->cu_lock);
1703 1.7 riastrad kmem_free(req, sizeof(*req));
1704 1.1 riastrad }
1705 1.17 riastrad
1706 1.17 riastrad /*
1707 1.17 riastrad * sun8i_crypto_register(sc)
1708 1.17 riastrad *
1709 1.17 riastrad * Register opencrypto algorithms supported by the crypto engine.
1710 1.17 riastrad */
1711 1.17 riastrad static void
1712 1.17 riastrad sun8i_crypto_register(struct sun8i_crypto_softc *sc)
1713 1.17 riastrad {
1714 1.17 riastrad struct sun8i_crypto_opencrypto *co = &sc->sc_opencrypto;
1715 1.17 riastrad
1716 1.17 riastrad co->co_driverid = crypto_get_driverid(0);
1717 1.17 riastrad if (co->co_driverid == (uint32_t)-1) {
1718 1.17 riastrad aprint_error_dev(sc->sc_dev,
1719 1.17 riastrad "failed to register crypto driver\n");
1720 1.17 riastrad return;
1721 1.17 riastrad }
1722 1.17 riastrad
1723 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_AES_CBC);
1724 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_AES_CTR);
1725 1.17 riastrad #ifdef CRYPTO_AES_ECB
1726 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_AES_ECB);
1727 1.17 riastrad #endif
1728 1.17 riastrad #ifdef CRYPTO_AES_XTS
1729 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_AES_XTS);
1730 1.17 riastrad #endif
1731 1.17 riastrad #ifdef CRYPTO_DES_CBC
1732 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_DES_CBC);
1733 1.17 riastrad #endif
1734 1.17 riastrad #ifdef CRYPTO_DES_ECB
1735 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_DES_ECB);
1736 1.17 riastrad #endif
1737 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_3DES_CBC);
1738 1.17 riastrad #ifdef CRYPTO_3DES_ECB
1739 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_3DES_ECB);
1740 1.17 riastrad #endif
1741 1.17 riastrad
1742 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_MD5);
1743 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_SHA1);
1744 1.17 riastrad #ifdef CRYPTO_SHA224
1745 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_SHA224);
1746 1.17 riastrad #endif
1747 1.17 riastrad #ifdef CRYPTO_SHA256
1748 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_SHA256);
1749 1.17 riastrad #endif
1750 1.17 riastrad
1751 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_SHA1_HMAC);
1752 1.17 riastrad sun8i_crypto_register1(sc, CRYPTO_SHA2_256_HMAC);
1753 1.17 riastrad
1754 1.17 riastrad //sun8i_crypto_kregister(sc, CRK_MOD_EXP); /* XXX unclear */
1755 1.17 riastrad }
1756 1.17 riastrad
1757 1.17 riastrad /*
1758 1.17 riastrad * sun8i_crypto_register1(sc, alg)
1759 1.17 riastrad *
1760 1.17 riastrad * Register support for one algorithm alg using
1761 1.17 riastrad * sun8i_crypto_newsession/freesession/process.
1762 1.17 riastrad */
1763 1.17 riastrad static void
1764 1.17 riastrad sun8i_crypto_register1(struct sun8i_crypto_softc *sc, uint32_t alg)
1765 1.17 riastrad {
1766 1.17 riastrad
1767 1.17 riastrad crypto_register(sc->sc_opencrypto.co_driverid, alg, 0, 0,
1768 1.17 riastrad sun8i_crypto_newsession,
1769 1.17 riastrad sun8i_crypto_freesession,
1770 1.17 riastrad sun8i_crypto_process,
1771 1.17 riastrad sc);
1772 1.17 riastrad }
1773 1.17 riastrad
1774 1.17 riastrad /*
1775 1.17 riastrad * sun8i_crypto_newsession(cookie, sidp, cri)
1776 1.17 riastrad *
1777 1.17 riastrad * Called by opencrypto to allocate a new session. We don't keep
1778 1.17 riastrad * track of sessions, since there are no persistent keys in the
1779 1.17 riastrad * hardware that we take advantage of, so this only validates the
1780 1.17 riastrad * crypto operations and returns a zero session id.
1781 1.17 riastrad */
1782 1.17 riastrad static int
1783 1.17 riastrad sun8i_crypto_newsession(void *cookie, uint32_t *sidp, struct cryptoini *cri)
1784 1.17 riastrad {
1785 1.17 riastrad
1786 1.17 riastrad /* No composition of operations is supported here. */
1787 1.17 riastrad if (cri->cri_next)
1788 1.17 riastrad return EINVAL;
1789 1.17 riastrad
1790 1.17 riastrad /*
1791 1.17 riastrad * No variation of rounds is supported here. (XXX Unused and
1792 1.17 riastrad * unimplemented in opencrypto(9) altogether?
1793 1.17 riastrad */
1794 1.17 riastrad if (cri->cri_rnd)
1795 1.17 riastrad return EINVAL;
1796 1.17 riastrad
1797 1.17 riastrad /*
1798 1.17 riastrad * Validate per-algorithm key length.
1799 1.17 riastrad *
1800 1.17 riastrad * XXX Does opencrypto(9) do this internally?
1801 1.17 riastrad */
1802 1.17 riastrad switch (cri->cri_alg) {
1803 1.17 riastrad case CRYPTO_MD5:
1804 1.17 riastrad case CRYPTO_SHA1:
1805 1.17 riastrad #ifdef CRYPTO_SHA224
1806 1.17 riastrad case CRYPTO_SHA224:
1807 1.17 riastrad #endif
1808 1.17 riastrad #ifdef CRYPTO_SHA256
1809 1.17 riastrad case CRYPTO_SHA256:
1810 1.17 riastrad #endif
1811 1.17 riastrad if (cri->cri_klen)
1812 1.17 riastrad return EINVAL;
1813 1.17 riastrad break;
1814 1.17 riastrad case CRYPTO_AES_CBC:
1815 1.17 riastrad #ifdef CRYPTO_AES_ECB
1816 1.17 riastrad case CRYPTO_AES_ECB:
1817 1.17 riastrad #endif
1818 1.17 riastrad switch (cri->cri_klen) {
1819 1.17 riastrad case 128:
1820 1.17 riastrad case 192:
1821 1.17 riastrad case 256:
1822 1.17 riastrad break;
1823 1.17 riastrad default:
1824 1.17 riastrad return EINVAL;
1825 1.17 riastrad }
1826 1.17 riastrad break;
1827 1.17 riastrad case CRYPTO_AES_CTR:
1828 1.17 riastrad /*
1829 1.17 riastrad * opencrypto `AES-CTR' takes four bytes of the input
1830 1.17 riastrad * block as the last four bytes of the key, for reasons
1831 1.17 riastrad * that are not entirely clear.
1832 1.17 riastrad */
1833 1.17 riastrad switch (cri->cri_klen) {
1834 1.17 riastrad case 128 + 32:
1835 1.17 riastrad case 192 + 32:
1836 1.17 riastrad case 256 + 32:
1837 1.17 riastrad break;
1838 1.17 riastrad default:
1839 1.17 riastrad return EINVAL;
1840 1.17 riastrad }
1841 1.17 riastrad break;
1842 1.17 riastrad #ifdef CRYPTO_AES_XTS
1843 1.17 riastrad case CRYPTO_AES_XTS:
1844 1.17 riastrad switch (cri->cri_klen) {
1845 1.17 riastrad case 256:
1846 1.17 riastrad case 384:
1847 1.17 riastrad case 512:
1848 1.17 riastrad break;
1849 1.17 riastrad default:
1850 1.17 riastrad return EINVAL;
1851 1.17 riastrad }
1852 1.17 riastrad break;
1853 1.17 riastrad #endif
1854 1.17 riastrad case CRYPTO_DES_CBC:
1855 1.17 riastrad #ifdef CRYPTO_DES_ECB
1856 1.17 riastrad case CRYPTO_DES_ECB:
1857 1.17 riastrad #endif
1858 1.17 riastrad switch (cri->cri_klen) {
1859 1.17 riastrad case 64:
1860 1.17 riastrad break;
1861 1.17 riastrad default:
1862 1.17 riastrad return EINVAL;
1863 1.17 riastrad }
1864 1.17 riastrad break;
1865 1.17 riastrad case CRYPTO_3DES_CBC:
1866 1.17 riastrad #ifdef CRYPTO_3DES_ECB
1867 1.17 riastrad case CRYPTO_3DES_ECB:
1868 1.17 riastrad #endif
1869 1.17 riastrad switch (cri->cri_klen) {
1870 1.17 riastrad case 192:
1871 1.17 riastrad break;
1872 1.17 riastrad default:
1873 1.17 riastrad return EINVAL;
1874 1.17 riastrad }
1875 1.17 riastrad break;
1876 1.17 riastrad case CRYPTO_SHA1_HMAC:
1877 1.17 riastrad /*
1878 1.17 riastrad * XXX Unclear what the length limit is, but since HMAC
1879 1.17 riastrad * behaves qualitatively different for a key of at
1880 1.17 riastrad * least the full block size -- and is generally best
1881 1.17 riastrad * to use with half the block size -- let's limit it to
1882 1.17 riastrad * one block.
1883 1.17 riastrad */
1884 1.17 riastrad if (cri->cri_klen % 8)
1885 1.17 riastrad return EINVAL;
1886 1.17 riastrad if (cri->cri_klen > 512)
1887 1.17 riastrad return EINVAL;
1888 1.17 riastrad break;
1889 1.17 riastrad case CRYPTO_SHA2_256_HMAC:
1890 1.17 riastrad if (cri->cri_klen % 8)
1891 1.17 riastrad return EINVAL;
1892 1.17 riastrad if (cri->cri_klen > 512)
1893 1.17 riastrad return EINVAL;
1894 1.17 riastrad break;
1895 1.17 riastrad default:
1896 1.17 riastrad panic("unsupported algorithm %d", cri->cri_alg);
1897 1.17 riastrad }
1898 1.17 riastrad
1899 1.17 riastrad KASSERT(cri->cri_klen % 8 == 0);
1900 1.17 riastrad
1901 1.17 riastrad /* Success! */
1902 1.17 riastrad *sidp = 1;
1903 1.17 riastrad return 0;
1904 1.17 riastrad }
1905 1.17 riastrad
1906 1.17 riastrad /*
1907 1.17 riastrad * sun8i_crypto_freesession(cookie, dsid)
1908 1.17 riastrad *
1909 1.17 riastrad * Called by opencrypto to free a session. We don't keep track of
1910 1.17 riastrad * sessions, since there are no persistent keys in the hardware
1911 1.17 riastrad * that we take advantage of, so this is a no-op.
1912 1.17 riastrad *
1913 1.17 riastrad * Note: dsid is actually a 64-bit quantity containing both the
1914 1.17 riastrad * driver id in the high half and the session id in the low half.
1915 1.17 riastrad */
1916 1.17 riastrad static int
1917 1.17 riastrad sun8i_crypto_freesession(void *cookie, uint64_t dsid)
1918 1.17 riastrad {
1919 1.17 riastrad
1920 1.17 riastrad KASSERT((dsid & 0xffffffff) == 1);
1921 1.17 riastrad
1922 1.17 riastrad /* Success! */
1923 1.17 riastrad return 0;
1924 1.17 riastrad }
1925 1.17 riastrad
1926 1.17 riastrad /*
1927 1.17 riastrad * sun8i_crypto_ivlen(crd)
1928 1.17 riastrad *
1929 1.17 riastrad * Return the crypto engine's notion of `IV length', in bytes, for
1930 1.17 riastrad * an opencrypto operation.
1931 1.17 riastrad */
1932 1.17 riastrad static u_int
1933 1.17 riastrad sun8i_crypto_ivlen(const struct cryptodesc *crd)
1934 1.17 riastrad {
1935 1.17 riastrad
1936 1.17 riastrad switch (crd->crd_alg) {
1937 1.17 riastrad case CRYPTO_AES_CBC:
1938 1.17 riastrad return 16;
1939 1.17 riastrad #ifdef CRYPTO_AES_XTS
1940 1.17 riastrad case CRYPTO_AES_XTS:
1941 1.17 riastrad return 16;
1942 1.17 riastrad #endif
1943 1.17 riastrad case CRYPTO_AES_CTR: /* XXX opencrypto quirk */
1944 1.17 riastrad return 8;
1945 1.17 riastrad #ifdef CRYPTO_DES_CBC
1946 1.17 riastrad case CRYPTO_DES_CBC:
1947 1.17 riastrad return 8;
1948 1.17 riastrad #endif
1949 1.17 riastrad case CRYPTO_3DES_CBC:
1950 1.17 riastrad return 8;
1951 1.17 riastrad case CRYPTO_MD5:
1952 1.17 riastrad return 16;
1953 1.17 riastrad #ifdef CRYPTO_SHA224
1954 1.17 riastrad case CRYPTO_SHA224:
1955 1.17 riastrad return 32;
1956 1.17 riastrad #endif
1957 1.17 riastrad #ifdef CRYPTO_SHA256
1958 1.17 riastrad case CRYPTO_SHA256:
1959 1.17 riastrad return 32;
1960 1.17 riastrad #endif
1961 1.17 riastrad case CRYPTO_SHA1_HMAC:
1962 1.17 riastrad return 20;
1963 1.17 riastrad case CRYPTO_SHA2_256_HMAC:
1964 1.17 riastrad return 32;
1965 1.17 riastrad default:
1966 1.17 riastrad return 0;
1967 1.17 riastrad }
1968 1.17 riastrad }
1969 1.17 riastrad
1970 1.17 riastrad /*
1971 1.17 riastrad * sun8i_crypto_process(cookie, crp, hint)
1972 1.17 riastrad *
1973 1.17 riastrad * Main opencrypto processing dispatch.
1974 1.17 riastrad */
1975 1.17 riastrad static int
1976 1.17 riastrad sun8i_crypto_process(void *cookie, struct cryptop *crp, int hint)
1977 1.17 riastrad {
1978 1.17 riastrad struct sun8i_crypto_softc *sc = cookie;
1979 1.17 riastrad struct sun8i_crypto_task *task;
1980 1.17 riastrad struct cryptodesc *crd = crp->crp_desc;
1981 1.17 riastrad unsigned klen, ivlen;
1982 1.17 riastrad uint32_t tdqc = 0, tdqs = 0;
1983 1.17 riastrad uint32_t dir, method, mode = 0, ctrwidth = 0, aeskeysize = 0;
1984 1.17 riastrad const uint32_t tdqa = 0;
1985 1.17 riastrad int error;
1986 1.17 riastrad
1987 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, process, entry, sc, crp, hint);
1988 1.17 riastrad
1989 1.17 riastrad /* Reject compositions -- we do not handle them. */
1990 1.17 riastrad if (crd->crd_next != NULL) {
1991 1.17 riastrad error = EOPNOTSUPP;
1992 1.17 riastrad goto fail0;
1993 1.17 riastrad }
1994 1.17 riastrad
1995 1.17 riastrad /* Reject transfers with nonsense skip. */
1996 1.17 riastrad if (crd->crd_skip < 0) {
1997 1.17 riastrad error = EINVAL;
1998 1.17 riastrad goto fail0;
1999 1.17 riastrad }
2000 1.17 riastrad
2001 1.17 riastrad /*
2002 1.17 riastrad * Actually just reject any nonzero skip, because it requires
2003 1.17 riastrad * DMA segment bookkeeping that we don't do yet.
2004 1.17 riastrad */
2005 1.17 riastrad if (crd->crd_skip) {
2006 1.17 riastrad error = EOPNOTSUPP;
2007 1.17 riastrad goto fail0;
2008 1.17 riastrad }
2009 1.17 riastrad
2010 1.17 riastrad /* Reject large transfers. */
2011 1.17 riastrad if (crd->crd_len > SUN8I_CRYPTO_MAXDMASIZE) {
2012 1.17 riastrad error = EFBIG;
2013 1.17 riastrad goto fail0;
2014 1.17 riastrad }
2015 1.17 riastrad
2016 1.17 riastrad /* Reject nonsense, unaligned, or mismatched lengths. */
2017 1.17 riastrad if (crd->crd_len < 0 ||
2018 1.17 riastrad crd->crd_len % 4 ||
2019 1.17 riastrad crd->crd_len != crp->crp_ilen) {
2020 1.17 riastrad error = EINVAL;
2021 1.17 riastrad goto fail0;
2022 1.17 riastrad }
2023 1.17 riastrad
2024 1.17 riastrad /* Reject mismatched buffer lengths. */
2025 1.17 riastrad /* XXX Handle crd_skip. */
2026 1.17 riastrad if (crp->crp_flags & CRYPTO_F_IMBUF) {
2027 1.17 riastrad struct mbuf *m = crp->crp_buf;
2028 1.17 riastrad uint32_t nbytes = 0;
2029 1.17 riastrad while (m != NULL) {
2030 1.17 riastrad KASSERT(m->m_len >= 0);
2031 1.17 riastrad if (m->m_len > crd->crd_len ||
2032 1.17 riastrad nbytes > crd->crd_len - m->m_len) {
2033 1.17 riastrad error = EINVAL;
2034 1.17 riastrad goto fail0;
2035 1.17 riastrad }
2036 1.17 riastrad nbytes += m->m_len;
2037 1.17 riastrad m = m->m_next;
2038 1.17 riastrad }
2039 1.17 riastrad if (nbytes != crd->crd_len) {
2040 1.17 riastrad error = EINVAL;
2041 1.17 riastrad goto fail0;
2042 1.17 riastrad }
2043 1.17 riastrad } else if (crp->crp_flags & CRYPTO_F_IOV) {
2044 1.17 riastrad struct uio *uio = crp->crp_buf;
2045 1.17 riastrad if (uio->uio_resid != crd->crd_len) {
2046 1.17 riastrad error = EINVAL;
2047 1.17 riastrad goto fail0;
2048 1.17 riastrad }
2049 1.17 riastrad }
2050 1.17 riastrad
2051 1.17 riastrad /* Get a task, or fail with ERESTART if we can't. */
2052 1.17 riastrad task = sun8i_crypto_task_get(sc, &sun8i_crypto_callback, crp,
2053 1.17 riastrad PR_NOWAIT);
2054 1.17 riastrad if (task == NULL) {
2055 1.17 riastrad /*
2056 1.17 riastrad * Don't invoke crypto_done -- we are asking the
2057 1.17 riastrad * opencrypto(9) machinery to queue the request and get
2058 1.17 riastrad * back to us.
2059 1.17 riastrad */
2060 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, process, busy, sc, crp, hint);
2061 1.17 riastrad return ERESTART;
2062 1.17 riastrad }
2063 1.17 riastrad
2064 1.17 riastrad /* Load key in, if relevant. */
2065 1.17 riastrad klen = crd->crd_klen;
2066 1.17 riastrad if (klen) {
2067 1.17 riastrad if (crd->crd_alg == CRYPTO_AES_CTR)
2068 1.17 riastrad /* AES-CTR is special -- see IV processing below. */
2069 1.17 riastrad klen -= 32;
2070 1.17 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_keymap,
2071 1.17 riastrad crd->crd_key, klen/8, NULL, BUS_DMA_NOWAIT);
2072 1.17 riastrad if (error)
2073 1.17 riastrad goto fail1;
2074 1.17 riastrad task->ct_flags |= TASK_KEY;
2075 1.17 riastrad }
2076 1.17 riastrad
2077 1.17 riastrad /* Handle the IV, if relevant. */
2078 1.17 riastrad ivlen = sun8i_crypto_ivlen(crd);
2079 1.17 riastrad if (ivlen) {
2080 1.17 riastrad void *iv;
2081 1.17 riastrad
2082 1.17 riastrad /*
2083 1.17 riastrad * If there's an explicit IV, use it; otherwise
2084 1.17 riastrad * randomly generate one.
2085 1.17 riastrad */
2086 1.17 riastrad if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
2087 1.17 riastrad iv = crd->crd_iv;
2088 1.17 riastrad } else {
2089 1.17 riastrad cprng_fast(task->ct_iv, ivlen);
2090 1.17 riastrad iv = task->ct_iv;
2091 1.17 riastrad }
2092 1.17 riastrad
2093 1.17 riastrad /*
2094 1.17 riastrad * If the IV is not already present in the user's
2095 1.17 riastrad * buffer, copy it over.
2096 1.17 riastrad */
2097 1.17 riastrad if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
2098 1.17 riastrad if (crp->crp_flags & CRYPTO_F_IMBUF) {
2099 1.17 riastrad m_copyback(crp->crp_buf, crd->crd_inject,
2100 1.17 riastrad ivlen, iv);
2101 1.17 riastrad } else if (crp->crp_flags & CRYPTO_F_IOV) {
2102 1.17 riastrad cuio_copyback(crp->crp_buf, crd->crd_inject,
2103 1.17 riastrad ivlen, iv);
2104 1.17 riastrad } else {
2105 1.17 riastrad panic("invalid buffer type %x",
2106 1.17 riastrad crp->crp_flags);
2107 1.17 riastrad }
2108 1.17 riastrad }
2109 1.17 riastrad
2110 1.17 riastrad /*
2111 1.17 riastrad * opencrypto's idea of `AES-CTR' is special.
2112 1.17 riastrad *
2113 1.17 riastrad * - The low 4 bytes of the input block are drawn from
2114 1.17 riastrad * an extra 4 bytes at the end of the key.
2115 1.17 riastrad *
2116 1.17 riastrad * - The next 8 bytes of the input block are drawn from
2117 1.17 riastrad * the opencrypto iv.
2118 1.17 riastrad *
2119 1.17 riastrad * - The high 4 bytes are the big-endian block counter,
2120 1.17 riastrad * which starts at 1 because why not.
2121 1.17 riastrad */
2122 1.17 riastrad if (crd->crd_alg == CRYPTO_AES_CTR) {
2123 1.17 riastrad uint8_t block[16];
2124 1.17 riastrad uint32_t blkno = 1;
2125 1.17 riastrad
2126 1.17 riastrad /* Format the initial input block. */
2127 1.17 riastrad memcpy(block, crd->crd_key + klen/8, 4);
2128 1.17 riastrad memcpy(block + 4, iv, 8);
2129 1.17 riastrad be32enc(block + 12, blkno);
2130 1.17 riastrad
2131 1.17 riastrad /* Copy it into the DMA buffer. */
2132 1.17 riastrad memcpy(task->ct_iv, block, 16);
2133 1.17 riastrad iv = task->ct_iv;
2134 1.17 riastrad ivlen = 16;
2135 1.17 riastrad }
2136 1.17 riastrad
2137 1.17 riastrad /* Load the IV. */
2138 1.17 riastrad error = bus_dmamap_load(sc->sc_dmat, task->ct_ivmap, iv, ivlen,
2139 1.17 riastrad NULL, BUS_DMA_NOWAIT);
2140 1.17 riastrad if (error)
2141 1.17 riastrad goto fail1;
2142 1.17 riastrad task->ct_flags |= TASK_IV;
2143 1.17 riastrad }
2144 1.17 riastrad
2145 1.17 riastrad /* Load the src and dst. */
2146 1.17 riastrad if (crp->crp_flags & CRYPTO_F_IMBUF) {
2147 1.17 riastrad struct mbuf *m = crp->crp_buf;
2148 1.17 riastrad
2149 1.17 riastrad /* XXX Handle crd_skip. */
2150 1.17 riastrad KASSERT(crd->crd_skip == 0);
2151 1.17 riastrad error = bus_dmamap_load_mbuf(sc->sc_dmat, task->ct_srcmap, m,
2152 1.17 riastrad BUS_DMA_NOWAIT);
2153 1.17 riastrad if (error)
2154 1.17 riastrad goto fail1;
2155 1.17 riastrad task->ct_flags |= TASK_SRC;
2156 1.17 riastrad
2157 1.17 riastrad /* XXX Handle crd_skip. */
2158 1.17 riastrad KASSERT(crd->crd_skip == 0);
2159 1.17 riastrad error = bus_dmamap_load_mbuf(sc->sc_dmat, task->ct_dstmap, m,
2160 1.17 riastrad BUS_DMA_NOWAIT);
2161 1.17 riastrad if (error)
2162 1.17 riastrad goto fail1;
2163 1.17 riastrad } else if (crp->crp_flags & CRYPTO_F_IOV) {
2164 1.17 riastrad struct uio *uio = crp->crp_buf;
2165 1.17 riastrad
2166 1.17 riastrad /* XXX Handle crd_skip. */
2167 1.17 riastrad KASSERT(crd->crd_skip == 0);
2168 1.17 riastrad error = bus_dmamap_load_uio(sc->sc_dmat, task->ct_srcmap, uio,
2169 1.17 riastrad BUS_DMA_NOWAIT);
2170 1.17 riastrad if (error)
2171 1.17 riastrad goto fail1;
2172 1.17 riastrad task->ct_flags |= TASK_SRC;
2173 1.17 riastrad
2174 1.17 riastrad /* XXX Handle crd_skip. */
2175 1.17 riastrad KASSERT(crd->crd_skip == 0);
2176 1.17 riastrad error = bus_dmamap_load_uio(sc->sc_dmat, task->ct_dstmap, uio,
2177 1.17 riastrad BUS_DMA_NOWAIT);
2178 1.17 riastrad if (error)
2179 1.17 riastrad goto fail1;
2180 1.17 riastrad } else {
2181 1.17 riastrad panic("invalid buffer type %x", crp->crp_flags);
2182 1.17 riastrad }
2183 1.17 riastrad
2184 1.17 riastrad /* Set the encryption direction. */
2185 1.17 riastrad if (crd->crd_flags & CRD_F_ENCRYPT)
2186 1.17 riastrad dir = SUN8I_CRYPTO_TDQC_OP_DIR_ENC;
2187 1.17 riastrad else
2188 1.17 riastrad dir = SUN8I_CRYPTO_TDQC_OP_DIR_DEC;
2189 1.17 riastrad tdqc |= __SHIFTIN(dir, SUN8I_CRYPTO_TDQC_OP_DIR);
2190 1.17 riastrad
2191 1.17 riastrad /* Set the method. */
2192 1.17 riastrad switch (crd->crd_alg) {
2193 1.17 riastrad case CRYPTO_AES_CBC:
2194 1.17 riastrad case CRYPTO_AES_CTR:
2195 1.17 riastrad #ifdef CRYPTO_AES_ECB
2196 1.17 riastrad case CRYPTO_AES_ECB:
2197 1.17 riastrad #endif
2198 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_AES;
2199 1.17 riastrad break;
2200 1.17 riastrad #ifdef CRYPTO_AES_XTS
2201 1.17 riastrad case CRYPTO_AES_XTS:
2202 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_AES;
2203 1.17 riastrad break;
2204 1.17 riastrad #endif
2205 1.17 riastrad case CRYPTO_DES_CBC:
2206 1.17 riastrad #ifdef CRYPTO_DES_ECB
2207 1.17 riastrad case CRYPTO_DES_ECB:
2208 1.17 riastrad #endif
2209 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_DES;
2210 1.17 riastrad break;
2211 1.17 riastrad case CRYPTO_3DES_CBC:
2212 1.17 riastrad #ifdef CRYPTO_3DES_ECB
2213 1.17 riastrad case CRYPTO_3DES_ECB:
2214 1.17 riastrad #endif
2215 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_3DES;
2216 1.17 riastrad break;
2217 1.17 riastrad case CRYPTO_MD5:
2218 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_MD5;
2219 1.17 riastrad break;
2220 1.17 riastrad case CRYPTO_SHA1:
2221 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_SHA1;
2222 1.17 riastrad break;
2223 1.17 riastrad #ifdef CRYPTO_SHA224
2224 1.17 riastrad case CRYPTO_SHA224:
2225 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_SHA224;
2226 1.17 riastrad break;
2227 1.17 riastrad #endif
2228 1.17 riastrad #ifdef CRYPTO_SHA256
2229 1.17 riastrad case CRYPTO_SHA256:
2230 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_SHA256;
2231 1.17 riastrad break;
2232 1.17 riastrad #endif
2233 1.17 riastrad case CRYPTO_SHA1_HMAC:
2234 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_HMAC_SHA1;
2235 1.17 riastrad break;
2236 1.17 riastrad case CRYPTO_SHA2_256_HMAC:
2237 1.17 riastrad method = SUN8I_CRYPTO_TDQC_METHOD_HMAC_SHA256;
2238 1.17 riastrad break;
2239 1.17 riastrad default:
2240 1.17 riastrad panic("unknown algorithm %d", crd->crd_alg);
2241 1.17 riastrad }
2242 1.17 riastrad tdqc |= __SHIFTIN(method, SUN8I_CRYPTO_TDQC_METHOD);
2243 1.17 riastrad
2244 1.17 riastrad /* Set the key selector. No idea how to use the internal keys. */
2245 1.17 riastrad tdqs |= __SHIFTIN(SUN8I_CRYPTO_TDQS_SKEY_SELECT_SS_KEYx,
2246 1.17 riastrad SUN8I_CRYPTO_TDQS_SKEY_SELECT);
2247 1.17 riastrad
2248 1.17 riastrad /* XXX Deal with AES_CTS_Last_Block_Flag. */
2249 1.17 riastrad
2250 1.17 riastrad /* Set the mode. */
2251 1.17 riastrad switch (crd->crd_alg) {
2252 1.17 riastrad #ifdef CRYPTO_AES_ECB
2253 1.17 riastrad case CRYPTO_AES_ECB:
2254 1.17 riastrad mode = SUN8I_CRYPTO_TDQS_OP_MODE_ECB;
2255 1.17 riastrad break;
2256 1.17 riastrad #endif
2257 1.17 riastrad #ifdef CRYPTO_DES_ECB
2258 1.17 riastrad case CRYPTO_DES_ECB:
2259 1.17 riastrad mode = SUN8I_CRYPTO_TDQS_OP_MODE_ECB;
2260 1.17 riastrad break;
2261 1.17 riastrad #endif
2262 1.17 riastrad #ifdef CRYPTO_3DES_ECB
2263 1.17 riastrad case CRYPTO_3DES_ECB:
2264 1.17 riastrad mode = SUN8I_CRYPTO_TDQS_OP_MODE_ECB;
2265 1.17 riastrad break;
2266 1.17 riastrad #endif
2267 1.17 riastrad case CRYPTO_AES_CBC:
2268 1.17 riastrad case CRYPTO_DES_CBC:
2269 1.17 riastrad case CRYPTO_3DES_CBC:
2270 1.17 riastrad mode = SUN8I_CRYPTO_TDQS_OP_MODE_CBC;
2271 1.17 riastrad break;
2272 1.17 riastrad case CRYPTO_AES_CTR:
2273 1.17 riastrad mode = SUN8I_CRYPTO_TDQS_OP_MODE_CTR;
2274 1.17 riastrad break;
2275 1.17 riastrad #ifdef CRYPTO_AES_XTS
2276 1.17 riastrad case CRYPTO_AES_XTS:
2277 1.17 riastrad mode = SUN8I_CRYPTO_TDQS_OP_MODE_CTS;
2278 1.17 riastrad break;
2279 1.17 riastrad #endif
2280 1.17 riastrad default:
2281 1.17 riastrad panic("unknown algorithm %d", crd->crd_alg);
2282 1.17 riastrad }
2283 1.17 riastrad tdqs |= __SHIFTIN(mode, SUN8I_CRYPTO_TDQS_OP_MODE);
2284 1.17 riastrad
2285 1.17 riastrad /* Set the CTR width. */
2286 1.17 riastrad switch (crd->crd_alg) {
2287 1.17 riastrad case CRYPTO_AES_CTR:
2288 1.17 riastrad ctrwidth = SUN8I_CRYPTO_TDQS_CTR_WIDTH_32;
2289 1.17 riastrad break;
2290 1.17 riastrad }
2291 1.17 riastrad tdqs |= __SHIFTIN(ctrwidth, SUN8I_CRYPTO_TDQS_CTR_WIDTH);
2292 1.17 riastrad
2293 1.17 riastrad /* Set the AES key size. */
2294 1.17 riastrad switch (crd->crd_alg) {
2295 1.17 riastrad case CRYPTO_AES_CBC:
2296 1.17 riastrad #ifdef CRYPTO_AES_ECB
2297 1.17 riastrad case CRYPTO_AES_ECB:
2298 1.17 riastrad #endif
2299 1.17 riastrad switch (crd->crd_klen) {
2300 1.17 riastrad case 128:
2301 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128;
2302 1.17 riastrad break;
2303 1.17 riastrad case 192:
2304 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_192;
2305 1.17 riastrad break;
2306 1.17 riastrad case 256:
2307 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_256;
2308 1.17 riastrad break;
2309 1.17 riastrad default:
2310 1.17 riastrad panic("invalid AES key size in bits: %u",
2311 1.17 riastrad crd->crd_klen);
2312 1.17 riastrad }
2313 1.17 riastrad break;
2314 1.17 riastrad case CRYPTO_AES_CTR:
2315 1.17 riastrad switch (crd->crd_klen) {
2316 1.17 riastrad case 128 + 32:
2317 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128;
2318 1.17 riastrad break;
2319 1.17 riastrad case 192 + 32:
2320 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_192;
2321 1.17 riastrad break;
2322 1.17 riastrad case 256 + 32:
2323 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_256;
2324 1.17 riastrad break;
2325 1.17 riastrad default:
2326 1.17 riastrad panic("invalid `AES-CTR' ` ``key'' size' in bits: %u",
2327 1.17 riastrad crd->crd_klen);
2328 1.17 riastrad }
2329 1.17 riastrad break;
2330 1.17 riastrad #ifdef CRYPTO_AES_XTS
2331 1.17 riastrad case CRYPTO_AES_XTS:
2332 1.17 riastrad switch (crd->crd_klen) {
2333 1.17 riastrad case 256:
2334 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_128;
2335 1.17 riastrad break;
2336 1.17 riastrad case 384:
2337 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_192;
2338 1.17 riastrad break;
2339 1.17 riastrad case 512:
2340 1.17 riastrad aeskeysize = SUN8I_CRYPTO_TDQS_AES_KEYSIZE_256;
2341 1.17 riastrad break;
2342 1.17 riastrad default:
2343 1.17 riastrad panic("invalid AES-XTS key size in bits: %u",
2344 1.17 riastrad crd->crd_klen);
2345 1.17 riastrad }
2346 1.17 riastrad break;
2347 1.17 riastrad #endif
2348 1.17 riastrad }
2349 1.17 riastrad tdqs |= __SHIFTIN(aeskeysize, SUN8I_CRYPTO_TDQS_AES_KEYSIZE);
2350 1.17 riastrad
2351 1.17 riastrad /* Set up the task descriptor. */
2352 1.17 riastrad error = sun8i_crypto_task_load(sc, task, crd->crd_len,
2353 1.17 riastrad tdqc, tdqs, tdqa);
2354 1.17 riastrad if (error)
2355 1.17 riastrad goto fail2;
2356 1.17 riastrad
2357 1.17 riastrad /* Submit! */
2358 1.17 riastrad error = sun8i_crypto_submit(sc, task);
2359 1.17 riastrad if (error)
2360 1.17 riastrad goto fail2;
2361 1.17 riastrad
2362 1.17 riastrad /* Success! */
2363 1.17 riastrad SDT_PROBE4(sdt, sun8i_crypto, process, queued, sc, crp, hint, task);
2364 1.17 riastrad return 0;
2365 1.17 riastrad
2366 1.17 riastrad fail2: bus_dmamap_unload(sc->sc_dmat, task->ct_dstmap);
2367 1.17 riastrad fail1: if (task->ct_flags & TASK_SRC)
2368 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_srcmap);
2369 1.17 riastrad if (task->ct_flags & TASK_CTR)
2370 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_ctrmap);
2371 1.17 riastrad if (task->ct_flags & TASK_IV)
2372 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_ivmap);
2373 1.17 riastrad if (task->ct_flags & TASK_KEY)
2374 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_keymap);
2375 1.17 riastrad sun8i_crypto_task_put(sc, task);
2376 1.17 riastrad fail0: KASSERT(error);
2377 1.17 riastrad KASSERT(error != ERESTART);
2378 1.17 riastrad crp->crp_etype = error;
2379 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, process, done, sc, crp, error);
2380 1.17 riastrad crypto_done(crp);
2381 1.17 riastrad return 0;
2382 1.17 riastrad }
2383 1.17 riastrad
2384 1.17 riastrad /*
2385 1.17 riastrad * sun8i_crypto_callback(sc, task, cookie, error)
2386 1.17 riastrad *
2387 1.17 riastrad * Completion callback for a task submitted via opencrypto.
2388 1.17 riastrad * Release the task and pass the error on to opencrypto with
2389 1.17 riastrad * crypto_done.
2390 1.17 riastrad */
2391 1.17 riastrad static void
2392 1.17 riastrad sun8i_crypto_callback(struct sun8i_crypto_softc *sc,
2393 1.17 riastrad struct sun8i_crypto_task *task, void *cookie, int error)
2394 1.17 riastrad {
2395 1.17 riastrad struct cryptop *crp = cookie;
2396 1.18 ad struct cryptodesc *crd __diagused = crp->crp_desc;
2397 1.17 riastrad
2398 1.17 riastrad KASSERT(error != ERESTART);
2399 1.17 riastrad KASSERT(crd != NULL);
2400 1.17 riastrad KASSERT(crd->crd_next == NULL);
2401 1.17 riastrad
2402 1.17 riastrad /* Return the number of bytes processed. */
2403 1.17 riastrad crp->crp_olen = error ? 0 : crp->crp_ilen;
2404 1.17 riastrad
2405 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_dstmap);
2406 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_srcmap);
2407 1.17 riastrad if (task->ct_flags & TASK_CTR)
2408 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_ctrmap);
2409 1.17 riastrad if (task->ct_flags & TASK_IV)
2410 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_ivmap);
2411 1.17 riastrad if (task->ct_flags & TASK_KEY)
2412 1.17 riastrad bus_dmamap_unload(sc->sc_dmat, task->ct_keymap);
2413 1.17 riastrad sun8i_crypto_task_put(sc, task);
2414 1.17 riastrad KASSERT(error != ERESTART);
2415 1.17 riastrad crp->crp_etype = error;
2416 1.17 riastrad SDT_PROBE3(sdt, sun8i_crypto, process, done, sc, crp, error);
2417 1.17 riastrad crypto_done(crp);
2418 1.17 riastrad }
2419