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