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