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