Home | History | Annotate | Line # | Download | only in pci
glxsb.c revision 1.2.14.1
      1 /*	$NetBSD: glxsb.c,v 1.2.14.1 2008/01/09 01:46:46 matt Exp $	*/
      2 
      3 /* $OpenBSD: glxsb.c,v 1.7 2007/02/12 14:31:45 tom Exp $ */
      4 
      5 /*
      6  * Copyright (c) 2006 Tom Cosgrove <tom (at) openbsd.org>
      7  * Copyright (c) 2003, 2004 Theo de Raadt
      8  * Copyright (c) 2003 Jason Wright
      9  *
     10  * Permission to use, copy, modify, and distribute this software for any
     11  * purpose with or without fee is hereby granted, provided that the above
     12  * copyright notice and this permission notice appear in all copies.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     21  */
     22 
     23 /*
     24  * Driver for the security block on the AMD Geode LX processors
     25  * http://www.amd.com/files/connectivitysolutions/geode/geode_lx/33234d_lx_ds.pdf
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: glxsb.c,v 1.2.14.1 2008/01/09 01:46:46 matt Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/device.h>
     34 #include <sys/malloc.h>
     35 #include <sys/mbuf.h>
     36 #include <sys/types.h>
     37 #include <sys/callout.h>
     38 #include <sys/rnd.h>
     39 #include <sys/bus.h>
     40 
     41 #include <machine/cpufunc.h>
     42 
     43 #include <dev/pci/pcivar.h>
     44 #include <dev/pci/pcidevs.h>
     45 
     46 #include <opencrypto/cryptodev.h>
     47 #include <crypto/rijndael/rijndael.h>
     48 
     49 #define SB_GLD_MSR_CAP		0x58002000	/* RO - Capabilities */
     50 #define SB_GLD_MSR_CONFIG	0x58002001	/* RW - Master Config */
     51 #define SB_GLD_MSR_SMI		0x58002002	/* RW - SMI */
     52 #define SB_GLD_MSR_ERROR	0x58002003	/* RW - Error */
     53 #define SB_GLD_MSR_PM		0x58002004	/* RW - Power Mgmt */
     54 #define SB_GLD_MSR_DIAG		0x58002005	/* RW - Diagnostic */
     55 #define SB_GLD_MSR_CTRL		0x58002006	/* RW - Security Block Cntrl */
     56 
     57 						/* For GLD_MSR_CTRL: */
     58 #define SB_GMC_DIV0		0x0000		/* AES update divisor values */
     59 #define SB_GMC_DIV1		0x0001
     60 #define SB_GMC_DIV2		0x0002
     61 #define SB_GMC_DIV3		0x0003
     62 #define SB_GMC_DIV_MASK		0x0003
     63 #define SB_GMC_SBI		0x0004		/* AES swap bits */
     64 #define SB_GMC_SBY		0x0008		/* AES swap bytes */
     65 #define SB_GMC_TW		0x0010		/* Time write (EEPROM) */
     66 #define SB_GMC_T_SEL0		0x0000		/* RNG post-proc: none */
     67 #define SB_GMC_T_SEL1		0x0100		/* RNG post-proc: LFSR */
     68 #define SB_GMC_T_SEL2		0x0200		/* RNG post-proc: whitener */
     69 #define SB_GMC_T_SEL3		0x0300		/* RNG LFSR+whitener */
     70 #define SB_GMC_T_SEL_MASK	0x0300
     71 #define SB_GMC_T_NE		0x0400		/* Noise (generator) Enable */
     72 #define SB_GMC_T_TM		0x0800		/* RNG test mode */
     73 						/*     (deterministic) */
     74 
     75 /* Security Block configuration/control registers (offsets from base) */
     76 
     77 #define SB_CTL_A		0x0000		/* RW - SB Control A */
     78 #define SB_CTL_B		0x0004		/* RW - SB Control B */
     79 #define SB_AES_INT		0x0008		/* RW - SB AES Interrupt */
     80 #define SB_SOURCE_A		0x0010		/* RW - Source A */
     81 #define SB_DEST_A		0x0014		/* RW - Destination A */
     82 #define SB_LENGTH_A		0x0018		/* RW - Length A */
     83 #define SB_SOURCE_B		0x0020		/* RW - Source B */
     84 #define SB_DEST_B		0x0024		/* RW - Destination B */
     85 #define SB_LENGTH_B		0x0028		/* RW - Length B */
     86 #define SB_WKEY			0x0030		/* WO - Writable Key 0-3 */
     87 #define SB_WKEY_0		0x0030		/* WO - Writable Key 0 */
     88 #define SB_WKEY_1		0x0034		/* WO - Writable Key 1 */
     89 #define SB_WKEY_2		0x0038		/* WO - Writable Key 2 */
     90 #define SB_WKEY_3		0x003C		/* WO - Writable Key 3 */
     91 #define SB_CBC_IV		0x0040		/* RW - CBC IV 0-3 */
     92 #define SB_CBC_IV_0		0x0040		/* RW - CBC IV 0 */
     93 #define SB_CBC_IV_1		0x0044		/* RW - CBC IV 1 */
     94 #define SB_CBC_IV_2		0x0048		/* RW - CBC IV 2 */
     95 #define SB_CBC_IV_3		0x004C		/* RW - CBC IV 3 */
     96 #define SB_RANDOM_NUM		0x0050		/* RW - Random Number */
     97 #define SB_RANDOM_NUM_STATUS	0x0054		/* RW - Random Number Status */
     98 #define SB_EEPROM_COMM		0x0800		/* RW - EEPROM Command */
     99 #define SB_EEPROM_ADDR		0x0804		/* RW - EEPROM Address */
    100 #define SB_EEPROM_DATA		0x0808		/* RW - EEPROM Data */
    101 #define SB_EEPROM_SEC_STATE	0x080C		/* RW - EEPROM Security State */
    102 
    103 						/* For SB_CTL_A and _B */
    104 #define SB_CTL_ST		0x0001		/* Start operation (enc/dec) */
    105 #define SB_CTL_ENC		0x0002		/* Encrypt (0 is decrypt) */
    106 #define SB_CTL_DEC		0x0000		/* Decrypt */
    107 #define SB_CTL_WK		0x0004		/* Use writable key (we set) */
    108 #define SB_CTL_DC		0x0008		/* Destination coherent */
    109 #define SB_CTL_SC		0x0010		/* Source coherent */
    110 #define SB_CTL_CBC		0x0020		/* CBC (0 is ECB) */
    111 
    112 						/* For SB_AES_INT */
    113 #define SB_AI_DISABLE_AES_A	0x0001		/* Disable AES A compl int */
    114 #define SB_AI_ENABLE_AES_A	0x0000		/* Enable AES A compl int */
    115 #define SB_AI_DISABLE_AES_B	0x0002		/* Disable AES B compl int */
    116 #define SB_AI_ENABLE_AES_B	0x0000		/* Enable AES B compl int */
    117 #define SB_AI_DISABLE_EEPROM	0x0004		/* Disable EEPROM op comp int */
    118 #define SB_AI_ENABLE_EEPROM	0x0000		/* Enable EEPROM op compl int */
    119 #define SB_AI_AES_A_COMPLETE	0x0100		/* AES A operation complete */
    120 #define SB_AI_AES_B_COMPLETE	0x0200		/* AES B operation complete */
    121 #define SB_AI_EEPROM_COMPLETE	0x0400		/* EEPROM operation complete */
    122 
    123 #define SB_RNS_TRNG_VALID	0x0001		/* in SB_RANDOM_NUM_STATUS */
    124 
    125 #define SB_MEM_SIZE		0x0810		/* Size of memory block */
    126 
    127 #define SB_AES_ALIGN		0x0010		/* Source and dest buffers */
    128 						/* must be 16-byte aligned */
    129 #define SB_AES_BLOCK_SIZE	0x0010
    130 
    131 /*
    132  * The Geode LX security block AES acceleration doesn't perform scatter-
    133  * gather: it just takes source and destination addresses.  Therefore the
    134  * plain- and ciphertexts need to be contiguous.  To this end, we allocate
    135  * a buffer for both, and accept the overhead of copying in and out.  If
    136  * the number of bytes in one operation is bigger than allowed for by the
    137  * buffer (buffer is twice the size of the max length, as it has both input
    138  * and output) then we have to perform multiple encryptions/decryptions.
    139  */
    140 #define GLXSB_MAX_AES_LEN	16384
    141 
    142 struct glxsb_dma_map {
    143 	bus_dmamap_t		dma_map;
    144 	bus_dma_segment_t	dma_seg;
    145 	int			dma_nsegs;
    146 	int			dma_size;
    147 	void *			dma_vaddr;
    148 	uint32_t		dma_paddr;
    149 };
    150 struct glxsb_session {
    151 	uint32_t	ses_key[4];
    152 	uint8_t		ses_iv[SB_AES_BLOCK_SIZE];
    153 	int		ses_klen;
    154 	int		ses_used;
    155 };
    156 
    157 struct glxsb_softc {
    158 	struct device		sc_dev;
    159 	bus_space_tag_t		sc_iot;
    160 	bus_space_handle_t	sc_ioh;
    161 	struct callout		sc_co;
    162 
    163 	bus_dma_tag_t		sc_dmat;
    164 	struct glxsb_dma_map	sc_dma;
    165 	int32_t			sc_cid;
    166 	int			sc_nsessions;
    167 	struct glxsb_session	*sc_sessions;
    168 
    169 	rndsource_element_t	sc_rnd_source;
    170 };
    171 
    172 int	glxsb_match(struct device *, struct cfdata *, void *);
    173 void	glxsb_attach(struct device *, struct device *, void *);
    174 void	glxsb_rnd(void *);
    175 
    176 CFATTACH_DECL(glxsb, sizeof(struct glxsb_softc), glxsb_match, glxsb_attach,
    177     NULL, NULL);
    178 
    179 #define GLXSB_SESSION(sid)		((sid) & 0x0fffffff)
    180 #define	GLXSB_SID(crd,ses)		(((crd) << 28) | ((ses) & 0x0fffffff))
    181 
    182 int glxsb_crypto_setup(struct glxsb_softc *);
    183 int glxsb_crypto_newsession(void *, uint32_t *, struct cryptoini *);
    184 int glxsb_crypto_process(void *, struct cryptop *, int);
    185 int glxsb_crypto_freesession(void *, uint64_t);
    186 static __inline void glxsb_aes(struct glxsb_softc *, uint32_t, uint32_t,
    187     uint32_t, void *, int, void *);
    188 
    189 int glxsb_dma_alloc(struct glxsb_softc *, int, struct glxsb_dma_map *);
    190 void glxsb_dma_pre_op(struct glxsb_softc *, struct glxsb_dma_map *);
    191 void glxsb_dma_post_op(struct glxsb_softc *, struct glxsb_dma_map *);
    192 void glxsb_dma_free(struct glxsb_softc *, struct glxsb_dma_map *);
    193 
    194 int
    195 glxsb_match(struct device *parent, struct cfdata *match, void *aux)
    196 {
    197 	struct pci_attach_args *pa = aux;
    198 
    199 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
    200 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_GEODELX_AES)
    201 		return (1);
    202 
    203 	return (0);
    204 }
    205 
    206 void
    207 glxsb_attach(struct device *parent, struct device *self, void *aux)
    208 {
    209 	struct glxsb_softc *sc = (void *) self;
    210 	struct pci_attach_args *pa = aux;
    211 	bus_addr_t membase;
    212 	bus_size_t memsize;
    213 	uint64_t msr;
    214 	uint32_t intr;
    215 
    216 	msr = rdmsr(SB_GLD_MSR_CAP);
    217 	if ((msr & 0xFFFF00) != 0x130400) {
    218 		printf(": unknown ID 0x%x\n", (int) ((msr & 0xFFFF00) >> 16));
    219 		return;
    220 	}
    221 
    222 	/* printf(": revision %d", (int) (msr & 0xFF)); */
    223 
    224 	/* Map in the security block configuration/control registers */
    225 	if (pci_mapreg_map(pa, PCI_MAPREG_START,
    226 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    227 	    &sc->sc_iot, &sc->sc_ioh, &membase, &memsize)) {
    228 		printf(": can't find mem space\n");
    229 		return;
    230 	}
    231 
    232 	/*
    233 	 * Configure the Security Block.
    234 	 *
    235 	 * We want to enable the noise generator (T_NE), and enable the
    236 	 * linear feedback shift register and whitener post-processing
    237 	 * (T_SEL = 3).  Also ensure that test mode (deterministic values)
    238 	 * is disabled.
    239 	 */
    240 	msr = rdmsr(SB_GLD_MSR_CTRL);
    241 	msr &= ~(SB_GMC_T_TM | SB_GMC_T_SEL_MASK);
    242 	msr |= SB_GMC_T_NE | SB_GMC_T_SEL3;
    243 #if 0
    244 	msr |= SB_GMC_SBI | SB_GMC_SBY;		/* for AES, if necessary */
    245 #endif
    246 	wrmsr(SB_GLD_MSR_CTRL, msr);
    247 
    248 	rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
    249 			  RND_TYPE_RNG, RND_FLAG_NO_ESTIMATE);
    250 
    251 	/* Install a periodic collector for the "true" (AMD's word) RNG */
    252 	callout_init(&sc->sc_co, 0);
    253 	callout_setfunc(&sc->sc_co, glxsb_rnd, sc);
    254 	glxsb_rnd(sc);
    255 	printf(": RNG");
    256 
    257 	/* We don't have an interrupt handler, so disable completion INTs */
    258 	intr = SB_AI_DISABLE_AES_A | SB_AI_DISABLE_AES_B |
    259 	    SB_AI_DISABLE_EEPROM | SB_AI_AES_A_COMPLETE |
    260 	    SB_AI_AES_B_COMPLETE | SB_AI_EEPROM_COMPLETE;
    261 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SB_AES_INT, intr);
    262 
    263 	sc->sc_dmat = pa->pa_dmat;
    264 
    265 	if (glxsb_crypto_setup(sc))
    266 		printf(" AES");
    267 
    268 	printf("\n");
    269 }
    270 
    271 void
    272 glxsb_rnd(void *v)
    273 {
    274 	struct glxsb_softc *sc = v;
    275 	uint32_t status, value;
    276 	extern int hz;
    277 
    278 	status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SB_RANDOM_NUM_STATUS);
    279 	if (status & SB_RNS_TRNG_VALID) {
    280 		value = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SB_RANDOM_NUM);
    281 		rnd_add_uint32(&sc->sc_rnd_source, value);
    282 	}
    283 
    284 	callout_schedule(&sc->sc_co, (hz > 100) ? (hz / 100) : 1);
    285 }
    286 
    287 int
    288 glxsb_crypto_setup(struct glxsb_softc *sc)
    289 {
    290 
    291 	/* Allocate a contiguous DMA-able buffer to work in */
    292 	if (glxsb_dma_alloc(sc, GLXSB_MAX_AES_LEN * 2, &sc->sc_dma) != 0)
    293 		return 0;
    294 
    295 	sc->sc_cid = crypto_get_driverid(0);
    296 	if (sc->sc_cid < 0)
    297 		return 0;
    298 
    299 	crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0,
    300 	    glxsb_crypto_newsession, glxsb_crypto_freesession,
    301 	    glxsb_crypto_process, sc);
    302 
    303 	sc->sc_nsessions = 0;
    304 
    305 	return 1;
    306 }
    307 
    308 int
    309 glxsb_crypto_newsession(void *aux, uint32_t *sidp, struct cryptoini *cri)
    310 {
    311 	struct glxsb_softc *sc = aux;
    312 	struct glxsb_session *ses = NULL;
    313 	int sesn;
    314 
    315 	if (sc == NULL || sidp == NULL || cri == NULL ||
    316 	    cri->cri_next != NULL || cri->cri_alg != CRYPTO_AES_CBC ||
    317 	    cri->cri_klen != 128)
    318 		return (EINVAL);
    319 
    320 	for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
    321 		if (sc->sc_sessions[sesn].ses_used == 0) {
    322 			ses = &sc->sc_sessions[sesn];
    323 			break;
    324 		}
    325 	}
    326 
    327 	if (ses == NULL) {
    328 		sesn = sc->sc_nsessions;
    329 		ses = malloc((sesn + 1) * sizeof(*ses), M_DEVBUF, M_NOWAIT);
    330 		if (ses == NULL)
    331 			return (ENOMEM);
    332 		if (sesn != 0) {
    333 			bcopy(sc->sc_sessions, ses, sesn * sizeof(*ses));
    334 			bzero(sc->sc_sessions, sesn * sizeof(*ses));
    335 			free(sc->sc_sessions, M_DEVBUF);
    336 		}
    337 		sc->sc_sessions = ses;
    338 		ses = &sc->sc_sessions[sesn];
    339 		sc->sc_nsessions++;
    340 	}
    341 
    342 	bzero(ses, sizeof(*ses));
    343 	ses->ses_used = 1;
    344 
    345 	arc4randbytes(ses->ses_iv, sizeof(ses->ses_iv));
    346 	ses->ses_klen = cri->cri_klen;
    347 
    348 	/* Copy the key (Geode LX wants the primary key only) */
    349 	bcopy(cri->cri_key, ses->ses_key, sizeof(ses->ses_key));
    350 
    351 	*sidp = GLXSB_SID(0, sesn);
    352 	return (0);
    353 }
    354 
    355 int
    356 glxsb_crypto_freesession(void *aux, uint64_t tid)
    357 {
    358 	struct glxsb_softc *sc = aux;
    359 	int sesn;
    360 	uint32_t sid = ((uint32_t)tid) & 0xffffffff;
    361 
    362 	if (sc == NULL)
    363 		return (EINVAL);
    364 	sesn = GLXSB_SESSION(sid);
    365 	if (sesn >= sc->sc_nsessions)
    366 		return (EINVAL);
    367 	bzero(&sc->sc_sessions[sesn], sizeof(sc->sc_sessions[sesn]));
    368 	return (0);
    369 }
    370 
    371 /*
    372  * Must be called at splnet() or higher
    373  */
    374 static __inline void
    375 glxsb_aes(struct glxsb_softc *sc, uint32_t control, uint32_t psrc,
    376     uint32_t pdst, void *key, int len, void *iv)
    377 {
    378 	uint32_t status;
    379 	int i;
    380 
    381 	if (len & 0xF) {
    382 		printf("%s: len must be a multiple of 16 (not %d)\n",
    383 		    sc->sc_dev.dv_xname, len);
    384 		return;
    385 	}
    386 
    387 	/* Set the source */
    388 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SB_SOURCE_A, psrc);
    389 
    390 	/* Set the destination address */
    391 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SB_DEST_A, pdst);
    392 
    393 	/* Set the data length */
    394 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SB_LENGTH_A, len);
    395 
    396 	/* Set the IV */
    397 	if (iv != NULL) {
    398 		bus_space_write_region_4(sc->sc_iot, sc->sc_ioh,
    399 		    SB_CBC_IV, iv, 4);
    400 		control |= SB_CTL_CBC;
    401 	}
    402 
    403 	/* Set the key */
    404 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, SB_WKEY, key, 4);
    405 
    406 	/* Ask the security block to do it */
    407 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SB_CTL_A,
    408 	    control | SB_CTL_WK | SB_CTL_DC | SB_CTL_SC | SB_CTL_ST);
    409 
    410 	/*
    411 	 * Now wait until it is done.
    412 	 *
    413 	 * We do a busy wait.  Obviously the number of iterations of
    414 	 * the loop required to perform the AES operation depends upon
    415 	 * the number of bytes to process.
    416 	 *
    417 	 * On a 500 MHz Geode LX we see
    418 	 *
    419 	 *	length (bytes)	typical max iterations
    420 	 *	    16		   12
    421 	 *	    64		   22
    422 	 *	   256		   59
    423 	 *	  1024		  212
    424 	 *	  8192		1,537
    425 	 *
    426 	 * Since we have a maximum size of operation defined in
    427 	 * GLXSB_MAX_AES_LEN, we use this constant to decide how long
    428 	 * to wait.  Allow an order of magnitude longer than it should
    429 	 * really take, just in case.
    430 	 */
    431 	for (i = 0; i < GLXSB_MAX_AES_LEN * 10; i++) {
    432 		status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SB_CTL_A);
    433 
    434 		if ((status & SB_CTL_ST) == 0)		/* Done */
    435 			return;
    436 	}
    437 
    438 	printf("%s: operation failed to complete\n", sc->sc_dev.dv_xname);
    439 }
    440 
    441 int
    442 glxsb_crypto_process(void *aux, struct cryptop *crp, int hint)
    443 {
    444 	struct glxsb_softc *sc = aux;
    445 	struct glxsb_session *ses;
    446 	struct cryptodesc *crd;
    447 	char *op_src, *op_dst;
    448 	uint32_t op_psrc, op_pdst;
    449 	uint8_t op_iv[SB_AES_BLOCK_SIZE], *piv;
    450 	int sesn, err = 0;
    451 	int len, tlen, xlen;
    452 	int offset;
    453 	uint32_t control;
    454 	int s;
    455 
    456 	s = splnet();
    457 
    458 	if (crp == NULL || crp->crp_callback == NULL) {
    459 		err = EINVAL;
    460 		goto out;
    461 	}
    462 	crd = crp->crp_desc;
    463 	if (crd == NULL || crd->crd_next != NULL ||
    464 	    crd->crd_alg != CRYPTO_AES_CBC ||
    465 	    (crd->crd_len % SB_AES_BLOCK_SIZE) != 0) {
    466 		err = EINVAL;
    467 		goto out;
    468 	}
    469 
    470 	sesn = GLXSB_SESSION(crp->crp_sid);
    471 	if (sesn >= sc->sc_nsessions) {
    472 		err = EINVAL;
    473 		goto out;
    474 	}
    475 	ses = &sc->sc_sessions[sesn];
    476 
    477 	/* How much of our buffer will we need to use? */
    478 	xlen = crd->crd_len > GLXSB_MAX_AES_LEN ?
    479 	    GLXSB_MAX_AES_LEN : crd->crd_len;
    480 
    481 	/*
    482 	 * XXX Check if we can have input == output on Geode LX.
    483 	 * XXX In the meantime, use two separate (adjacent) buffers.
    484 	 */
    485 	op_src = sc->sc_dma.dma_vaddr;
    486 	op_dst = (char *)sc->sc_dma.dma_vaddr + xlen;
    487 
    488 	op_psrc = sc->sc_dma.dma_paddr;
    489 	op_pdst = sc->sc_dma.dma_paddr + xlen;
    490 
    491 	if (crd->crd_flags & CRD_F_ENCRYPT) {
    492 		control = SB_CTL_ENC;
    493 		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
    494 			bcopy(crd->crd_iv, op_iv, sizeof(op_iv));
    495 		else
    496 			bcopy(ses->ses_iv, op_iv, sizeof(op_iv));
    497 
    498 		if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
    499 			if (crp->crp_flags & CRYPTO_F_IMBUF)
    500 				m_copyback((struct mbuf *)crp->crp_buf,
    501 				    crd->crd_inject, sizeof(op_iv), op_iv);
    502 			else if (crp->crp_flags & CRYPTO_F_IOV)
    503 				cuio_copyback((struct uio *)crp->crp_buf,
    504 				    crd->crd_inject, sizeof(op_iv), op_iv);
    505 			else
    506 				bcopy(op_iv,
    507 				    (char *)crp->crp_buf + crd->crd_inject,
    508 				    sizeof(op_iv));
    509 		}
    510 	} else {
    511 		control = SB_CTL_DEC;
    512 		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
    513 			bcopy(crd->crd_iv, op_iv, sizeof(op_iv));
    514 		else {
    515 			if (crp->crp_flags & CRYPTO_F_IMBUF)
    516 				m_copydata((struct mbuf *)crp->crp_buf,
    517 				    crd->crd_inject, sizeof(op_iv), op_iv);
    518 			else if (crp->crp_flags & CRYPTO_F_IOV)
    519 				cuio_copydata((struct uio *)crp->crp_buf,
    520 				    crd->crd_inject, sizeof(op_iv), op_iv);
    521 			else
    522 				bcopy((char *)crp->crp_buf + crd->crd_inject,
    523 				    op_iv, sizeof(op_iv));
    524 		}
    525 	}
    526 
    527 	offset = 0;
    528 	tlen = crd->crd_len;
    529 	piv = op_iv;
    530 
    531 	/* Process the data in GLXSB_MAX_AES_LEN chunks */
    532 	while (tlen > 0) {
    533 		len = (tlen > GLXSB_MAX_AES_LEN) ? GLXSB_MAX_AES_LEN : tlen;
    534 
    535 		if (crp->crp_flags & CRYPTO_F_IMBUF)
    536 			m_copydata((struct mbuf *)crp->crp_buf,
    537 			    crd->crd_skip + offset, len, op_src);
    538 		else if (crp->crp_flags & CRYPTO_F_IOV)
    539 			cuio_copydata((struct uio *)crp->crp_buf,
    540 			    crd->crd_skip + offset, len, op_src);
    541 		else
    542 			bcopy((char *)crp->crp_buf + crd->crd_skip + offset,
    543 			    op_src, len);
    544 
    545 		glxsb_dma_pre_op(sc, &sc->sc_dma);
    546 
    547 		glxsb_aes(sc, control, op_psrc, op_pdst, ses->ses_key,
    548 		    len, op_iv);
    549 
    550 		glxsb_dma_post_op(sc, &sc->sc_dma);
    551 
    552 		if (crp->crp_flags & CRYPTO_F_IMBUF)
    553 			m_copyback((struct mbuf *)crp->crp_buf,
    554 			    crd->crd_skip + offset, len, op_dst);
    555 		else if (crp->crp_flags & CRYPTO_F_IOV)
    556 			cuio_copyback((struct uio *)crp->crp_buf,
    557 			    crd->crd_skip + offset, len, op_dst);
    558 		else
    559 			bcopy(op_dst, (char *)crp->crp_buf + crd->crd_skip + offset,
    560 			    len);
    561 
    562 		offset += len;
    563 		tlen -= len;
    564 
    565 		if (tlen <= 0) {	/* Ideally, just == 0 */
    566 			/* Finished - put the IV in session IV */
    567 			piv = ses->ses_iv;
    568 		}
    569 
    570 		/*
    571 		 * Copy out last block for use as next iteration/session IV.
    572 		 *
    573 		 * piv is set to op_iv[] before the loop starts, but is
    574 		 * set to ses->ses_iv if we're going to exit the loop this
    575 		 * time.
    576 		 */
    577 		if (crd->crd_flags & CRD_F_ENCRYPT) {
    578 			bcopy(op_dst + len - sizeof(op_iv), piv, sizeof(op_iv));
    579 		} else {
    580 			/* Decryption, only need this if another iteration */
    581 			if (tlen > 0) {
    582 				bcopy(op_src + len - sizeof(op_iv), piv,
    583 				    sizeof(op_iv));
    584 			}
    585 		}
    586 	}
    587 
    588 	/* All AES processing has now been done. */
    589 
    590 	bzero(sc->sc_dma.dma_vaddr, xlen * 2);
    591 out:
    592 	crp->crp_etype = err;
    593 	crypto_done(crp);
    594 	splx(s);
    595 	return (err);
    596 }
    597 
    598 int
    599 glxsb_dma_alloc(struct glxsb_softc *sc, int size, struct glxsb_dma_map *dma)
    600 {
    601 	int rc;
    602 
    603 	dma->dma_nsegs = 1;
    604 	dma->dma_size = size;
    605 
    606 	rc = bus_dmamap_create(sc->sc_dmat, size, dma->dma_nsegs, size,
    607 	    0, BUS_DMA_NOWAIT, &dma->dma_map);
    608 	if (rc != 0) {
    609 		printf("%s: couldn't create DMA map for %d bytes (%d)\n",
    610 		    sc->sc_dev.dv_xname, size, rc);
    611 
    612 		goto fail0;
    613 	}
    614 
    615 	rc = bus_dmamem_alloc(sc->sc_dmat, size, SB_AES_ALIGN, 0,
    616 	    &dma->dma_seg, dma->dma_nsegs, &dma->dma_nsegs, BUS_DMA_NOWAIT);
    617 	if (rc != 0) {
    618 		printf("%s: couldn't allocate DMA memory of %d bytes (%d)\n",
    619 		    sc->sc_dev.dv_xname, size, rc);
    620 
    621 		goto fail1;
    622 	}
    623 
    624 	rc = bus_dmamem_map(sc->sc_dmat, &dma->dma_seg, 1, size,
    625 	    &dma->dma_vaddr, BUS_DMA_NOWAIT);
    626 	if (rc != 0) {
    627 		printf("%s: couldn't map DMA memory for %d bytes (%d)\n",
    628 		    sc->sc_dev.dv_xname, size, rc);
    629 
    630 		goto fail2;
    631 	}
    632 
    633 	rc = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_vaddr,
    634 	    size, NULL, BUS_DMA_NOWAIT);
    635 	if (rc != 0) {
    636 		printf("%s: couldn't load DMA memory for %d bytes (%d)\n",
    637 		    sc->sc_dev.dv_xname, size, rc);
    638 
    639 		goto fail3;
    640 	}
    641 
    642 	dma->dma_paddr = dma->dma_map->dm_segs[0].ds_addr;
    643 
    644 	return 0;
    645 
    646 fail3:
    647 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, size);
    648 fail2:
    649 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nsegs);
    650 fail1:
    651 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
    652 fail0:
    653 	return rc;
    654 }
    655 
    656 void
    657 glxsb_dma_pre_op(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
    658 {
    659 	bus_dmamap_sync(sc->sc_dmat, dma->dma_map, 0, dma->dma_size,
    660 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    661 }
    662 
    663 void
    664 glxsb_dma_post_op(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
    665 {
    666 	bus_dmamap_sync(sc->sc_dmat, dma->dma_map, 0, dma->dma_size,
    667 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    668 }
    669 
    670 void
    671 glxsb_dma_free(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
    672 {
    673 	bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
    674 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, dma->dma_size);
    675 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nsegs);
    676 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
    677 }
    678