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