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