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