xref: /freebsd/sys/dev/glxsb/glxsb.c (revision a51aa5d1f6672405b0b759e80a0b039106c09da1)
1a51aa5d1SPhilip Paeps /* $OpenBSD: glxsb.c,v 1.7 2007/02/12 14:31:45 tom Exp $ */
2a51aa5d1SPhilip Paeps 
3a51aa5d1SPhilip Paeps /*
4a51aa5d1SPhilip Paeps  * Copyright (c) 2006 Tom Cosgrove <tom@openbsd.org>
5a51aa5d1SPhilip Paeps  * Copyright (c) 2003, 2004 Theo de Raadt
6a51aa5d1SPhilip Paeps  * Copyright (c) 2003 Jason Wright
7a51aa5d1SPhilip Paeps  *
8a51aa5d1SPhilip Paeps  * Permission to use, copy, modify, and distribute this software for any
9a51aa5d1SPhilip Paeps  * purpose with or without fee is hereby granted, provided that the above
10a51aa5d1SPhilip Paeps  * copyright notice and this permission notice appear in all copies.
11a51aa5d1SPhilip Paeps  *
12a51aa5d1SPhilip Paeps  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13a51aa5d1SPhilip Paeps  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14a51aa5d1SPhilip Paeps  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15a51aa5d1SPhilip Paeps  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16a51aa5d1SPhilip Paeps  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17a51aa5d1SPhilip Paeps  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18a51aa5d1SPhilip Paeps  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19a51aa5d1SPhilip Paeps  */
20a51aa5d1SPhilip Paeps 
21a51aa5d1SPhilip Paeps /*
22a51aa5d1SPhilip Paeps  * Driver for the security block on the AMD Geode LX processors
23a51aa5d1SPhilip Paeps  * http://www.amd.com/files/connectivitysolutions/geode/geode_lx/33234d_lx_ds.pdf
24a51aa5d1SPhilip Paeps  */
25a51aa5d1SPhilip Paeps 
26a51aa5d1SPhilip Paeps #include <sys/cdefs.h>
27a51aa5d1SPhilip Paeps __FBSDID("$FreeBSD$");
28a51aa5d1SPhilip Paeps 
29a51aa5d1SPhilip Paeps #include <sys/param.h>
30a51aa5d1SPhilip Paeps #include <sys/systm.h>
31a51aa5d1SPhilip Paeps #include <sys/bus.h>
32a51aa5d1SPhilip Paeps #include <sys/errno.h>
33a51aa5d1SPhilip Paeps #include <sys/kernel.h>
34a51aa5d1SPhilip Paeps #include <sys/lock.h>
35a51aa5d1SPhilip Paeps #include <sys/malloc.h>
36a51aa5d1SPhilip Paeps #include <sys/mbuf.h>
37a51aa5d1SPhilip Paeps #include <sys/module.h>
38a51aa5d1SPhilip Paeps #include <sys/mutex.h>
39a51aa5d1SPhilip Paeps #include <sys/proc.h>
40a51aa5d1SPhilip Paeps #include <sys/random.h>
41a51aa5d1SPhilip Paeps #include <sys/rman.h>
42a51aa5d1SPhilip Paeps #include <sys/sysctl.h>
43a51aa5d1SPhilip Paeps #include <sys/taskqueue.h>
44a51aa5d1SPhilip Paeps 
45a51aa5d1SPhilip Paeps #include <machine/bus.h>
46a51aa5d1SPhilip Paeps #include <machine/cpufunc.h>
47a51aa5d1SPhilip Paeps #include <machine/resource.h>
48a51aa5d1SPhilip Paeps 
49a51aa5d1SPhilip Paeps #include <dev/pci/pcivar.h>
50a51aa5d1SPhilip Paeps #include <dev/pci/pcireg.h>
51a51aa5d1SPhilip Paeps 
52a51aa5d1SPhilip Paeps #include <opencrypto/cryptodev.h>
53a51aa5d1SPhilip Paeps #include <opencrypto/cryptosoft.h>
54a51aa5d1SPhilip Paeps #include <opencrypto/xform.h>
55a51aa5d1SPhilip Paeps 
56a51aa5d1SPhilip Paeps #include "cryptodev_if.h"
57a51aa5d1SPhilip Paeps #include "glxsb.h"
58a51aa5d1SPhilip Paeps 
59a51aa5d1SPhilip Paeps #define PCI_VENDOR_AMD			0x1022	/* AMD */
60a51aa5d1SPhilip Paeps #define PCI_PRODUCT_AMD_GEODE_LX_CRYPTO	0x2082	/* Geode LX Crypto */
61a51aa5d1SPhilip Paeps 
62a51aa5d1SPhilip Paeps #define SB_GLD_MSR_CAP		0x58002000	/* RO - Capabilities */
63a51aa5d1SPhilip Paeps #define SB_GLD_MSR_CONFIG	0x58002001	/* RW - Master Config */
64a51aa5d1SPhilip Paeps #define SB_GLD_MSR_SMI		0x58002002	/* RW - SMI */
65a51aa5d1SPhilip Paeps #define SB_GLD_MSR_ERROR	0x58002003	/* RW - Error */
66a51aa5d1SPhilip Paeps #define SB_GLD_MSR_PM		0x58002004	/* RW - Power Mgmt */
67a51aa5d1SPhilip Paeps #define SB_GLD_MSR_DIAG		0x58002005	/* RW - Diagnostic */
68a51aa5d1SPhilip Paeps #define SB_GLD_MSR_CTRL		0x58002006	/* RW - Security Block Cntrl */
69a51aa5d1SPhilip Paeps 
70a51aa5d1SPhilip Paeps 						/* For GLD_MSR_CTRL: */
71a51aa5d1SPhilip Paeps #define SB_GMC_DIV0		0x0000		/* AES update divisor values */
72a51aa5d1SPhilip Paeps #define SB_GMC_DIV1		0x0001
73a51aa5d1SPhilip Paeps #define SB_GMC_DIV2		0x0002
74a51aa5d1SPhilip Paeps #define SB_GMC_DIV3		0x0003
75a51aa5d1SPhilip Paeps #define SB_GMC_DIV_MASK		0x0003
76a51aa5d1SPhilip Paeps #define SB_GMC_SBI		0x0004		/* AES swap bits */
77a51aa5d1SPhilip Paeps #define SB_GMC_SBY		0x0008		/* AES swap bytes */
78a51aa5d1SPhilip Paeps #define SB_GMC_TW		0x0010		/* Time write (EEPROM) */
79a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL0		0x0000		/* RNG post-proc: none */
80a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL1		0x0100		/* RNG post-proc: LFSR */
81a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL2		0x0200		/* RNG post-proc: whitener */
82a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL3		0x0300		/* RNG LFSR+whitener */
83a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL_MASK	0x0300
84a51aa5d1SPhilip Paeps #define SB_GMC_T_NE		0x0400		/* Noise (generator) Enable */
85a51aa5d1SPhilip Paeps #define SB_GMC_T_TM		0x0800		/* RNG test mode */
86a51aa5d1SPhilip Paeps 						/*     (deterministic) */
87a51aa5d1SPhilip Paeps 
88a51aa5d1SPhilip Paeps /* Security Block configuration/control registers (offsets from base) */
89a51aa5d1SPhilip Paeps #define SB_CTL_A		0x0000		/* RW - SB Control A */
90a51aa5d1SPhilip Paeps #define SB_CTL_B		0x0004		/* RW - SB Control B */
91a51aa5d1SPhilip Paeps #define SB_AES_INT		0x0008		/* RW - SB AES Interrupt */
92a51aa5d1SPhilip Paeps #define SB_SOURCE_A		0x0010		/* RW - Source A */
93a51aa5d1SPhilip Paeps #define SB_DEST_A		0x0014		/* RW - Destination A */
94a51aa5d1SPhilip Paeps #define SB_LENGTH_A		0x0018		/* RW - Length A */
95a51aa5d1SPhilip Paeps #define SB_SOURCE_B		0x0020		/* RW - Source B */
96a51aa5d1SPhilip Paeps #define SB_DEST_B		0x0024		/* RW - Destination B */
97a51aa5d1SPhilip Paeps #define SB_LENGTH_B		0x0028		/* RW - Length B */
98a51aa5d1SPhilip Paeps #define SB_WKEY			0x0030		/* WO - Writable Key 0-3 */
99a51aa5d1SPhilip Paeps #define SB_WKEY_0		0x0030		/* WO - Writable Key 0 */
100a51aa5d1SPhilip Paeps #define SB_WKEY_1		0x0034		/* WO - Writable Key 1 */
101a51aa5d1SPhilip Paeps #define SB_WKEY_2		0x0038		/* WO - Writable Key 2 */
102a51aa5d1SPhilip Paeps #define SB_WKEY_3		0x003C		/* WO - Writable Key 3 */
103a51aa5d1SPhilip Paeps #define SB_CBC_IV		0x0040		/* RW - CBC IV 0-3 */
104a51aa5d1SPhilip Paeps #define SB_CBC_IV_0		0x0040		/* RW - CBC IV 0 */
105a51aa5d1SPhilip Paeps #define SB_CBC_IV_1		0x0044		/* RW - CBC IV 1 */
106a51aa5d1SPhilip Paeps #define SB_CBC_IV_2		0x0048		/* RW - CBC IV 2 */
107a51aa5d1SPhilip Paeps #define SB_CBC_IV_3		0x004C		/* RW - CBC IV 3 */
108a51aa5d1SPhilip Paeps #define SB_RANDOM_NUM		0x0050		/* RW - Random Number */
109a51aa5d1SPhilip Paeps #define SB_RANDOM_NUM_STATUS	0x0054		/* RW - Random Number Status */
110a51aa5d1SPhilip Paeps #define SB_EEPROM_COMM		0x0800		/* RW - EEPROM Command */
111a51aa5d1SPhilip Paeps #define SB_EEPROM_ADDR		0x0804		/* RW - EEPROM Address */
112a51aa5d1SPhilip Paeps #define SB_EEPROM_DATA		0x0808		/* RW - EEPROM Data */
113a51aa5d1SPhilip Paeps #define SB_EEPROM_SEC_STATE	0x080C		/* RW - EEPROM Security State */
114a51aa5d1SPhilip Paeps 
115a51aa5d1SPhilip Paeps 						/* For SB_CTL_A and _B */
116a51aa5d1SPhilip Paeps #define SB_CTL_ST		0x0001		/* Start operation (enc/dec) */
117a51aa5d1SPhilip Paeps #define SB_CTL_ENC		0x0002		/* Encrypt (0 is decrypt) */
118a51aa5d1SPhilip Paeps #define SB_CTL_DEC		0x0000		/* Decrypt */
119a51aa5d1SPhilip Paeps #define SB_CTL_WK		0x0004		/* Use writable key (we set) */
120a51aa5d1SPhilip Paeps #define SB_CTL_DC		0x0008		/* Destination coherent */
121a51aa5d1SPhilip Paeps #define SB_CTL_SC		0x0010		/* Source coherent */
122a51aa5d1SPhilip Paeps #define SB_CTL_CBC		0x0020		/* CBC (0 is ECB) */
123a51aa5d1SPhilip Paeps 
124a51aa5d1SPhilip Paeps 						/* For SB_AES_INT */
125a51aa5d1SPhilip Paeps #define SB_AI_DISABLE_AES_A	0x0001		/* Disable AES A compl int */
126a51aa5d1SPhilip Paeps #define SB_AI_ENABLE_AES_A	0x0000		/* Enable AES A compl int */
127a51aa5d1SPhilip Paeps #define SB_AI_DISABLE_AES_B	0x0002		/* Disable AES B compl int */
128a51aa5d1SPhilip Paeps #define SB_AI_ENABLE_AES_B	0x0000		/* Enable AES B compl int */
129a51aa5d1SPhilip Paeps #define SB_AI_DISABLE_EEPROM	0x0004		/* Disable EEPROM op comp int */
130a51aa5d1SPhilip Paeps #define SB_AI_ENABLE_EEPROM	0x0000		/* Enable EEPROM op compl int */
131a51aa5d1SPhilip Paeps #define SB_AI_AES_A_COMPLETE   0x10000		/* AES A operation complete */
132a51aa5d1SPhilip Paeps #define SB_AI_AES_B_COMPLETE   0x20000		/* AES B operation complete */
133a51aa5d1SPhilip Paeps #define SB_AI_EEPROM_COMPLETE  0x40000		/* EEPROM operation complete */
134a51aa5d1SPhilip Paeps 
135a51aa5d1SPhilip Paeps #define SB_AI_CLEAR_INTR \
136a51aa5d1SPhilip Paeps 	(SB_AI_DISABLE_AES_A | SB_AI_DISABLE_AES_B |\
137a51aa5d1SPhilip Paeps 	SB_AI_DISABLE_EEPROM | SB_AI_AES_A_COMPLETE |\
138a51aa5d1SPhilip Paeps 	SB_AI_AES_B_COMPLETE | SB_AI_EEPROM_COMPLETE)
139a51aa5d1SPhilip Paeps 
140a51aa5d1SPhilip Paeps #define SB_RNS_TRNG_VALID	0x0001		/* in SB_RANDOM_NUM_STATUS */
141a51aa5d1SPhilip Paeps 
142a51aa5d1SPhilip Paeps #define SB_MEM_SIZE		0x0810		/* Size of memory block */
143a51aa5d1SPhilip Paeps 
144a51aa5d1SPhilip Paeps #define SB_AES_ALIGN		0x0010		/* Source and dest buffers */
145a51aa5d1SPhilip Paeps 						/* must be 16-byte aligned */
146a51aa5d1SPhilip Paeps #define SB_AES_BLOCK_SIZE	0x0010
147a51aa5d1SPhilip Paeps 
148a51aa5d1SPhilip Paeps /*
149a51aa5d1SPhilip Paeps  * The Geode LX security block AES acceleration doesn't perform scatter-
150a51aa5d1SPhilip Paeps  * gather: it just takes source and destination addresses.  Therefore the
151a51aa5d1SPhilip Paeps  * plain- and ciphertexts need to be contiguous.  To this end, we allocate
152a51aa5d1SPhilip Paeps  * a buffer for both, and accept the overhead of copying in and out.  If
153a51aa5d1SPhilip Paeps  * the number of bytes in one operation is bigger than allowed for by the
154a51aa5d1SPhilip Paeps  * buffer (buffer is twice the size of the max length, as it has both input
155a51aa5d1SPhilip Paeps  * and output) then we have to perform multiple encryptions/decryptions.
156a51aa5d1SPhilip Paeps  */
157a51aa5d1SPhilip Paeps 
158a51aa5d1SPhilip Paeps #define GLXSB_MAX_AES_LEN	16384
159a51aa5d1SPhilip Paeps 
160a51aa5d1SPhilip Paeps MALLOC_DEFINE(M_GLXSB, "glxsb_data", "Glxsb Data");
161a51aa5d1SPhilip Paeps 
162a51aa5d1SPhilip Paeps struct glxsb_dma_map {
163a51aa5d1SPhilip Paeps 	bus_dmamap_t		dma_map;	/* DMA map */
164a51aa5d1SPhilip Paeps 	bus_dma_segment_t	dma_seg;	/* segments */
165a51aa5d1SPhilip Paeps 	int			dma_nsegs;	/* #segments */
166a51aa5d1SPhilip Paeps 	int			dma_size;	/* size */
167a51aa5d1SPhilip Paeps 	caddr_t 		dma_vaddr;	/* virtual address */
168a51aa5d1SPhilip Paeps 	bus_addr_t		dma_paddr;	/* physical address */
169a51aa5d1SPhilip Paeps };
170a51aa5d1SPhilip Paeps 
171a51aa5d1SPhilip Paeps struct glxsb_taskop {
172a51aa5d1SPhilip Paeps 	struct glxsb_session	*to_ses;	/* crypto session */
173a51aa5d1SPhilip Paeps 	struct cryptop		*to_crp;	/* cryptop to perfom */
174a51aa5d1SPhilip Paeps 	struct cryptodesc	*to_enccrd;	/* enccrd to perform */
175a51aa5d1SPhilip Paeps 	struct cryptodesc	*to_maccrd;	/* maccrd to perform */
176a51aa5d1SPhilip Paeps };
177a51aa5d1SPhilip Paeps 
178a51aa5d1SPhilip Paeps struct glxsb_softc {
179a51aa5d1SPhilip Paeps 	device_t		sc_dev;		/* device backpointer */
180a51aa5d1SPhilip Paeps 	struct resource		*sc_sr;		/* resource */
181a51aa5d1SPhilip Paeps 	int			sc_rid;		/* resource rid */
182a51aa5d1SPhilip Paeps 	struct callout		sc_rngco;	/* RNG callout */
183a51aa5d1SPhilip Paeps 	int			sc_rnghz;	/* RNG callout ticks */
184a51aa5d1SPhilip Paeps 	bus_dma_tag_t		sc_dmat;	/* DMA tag */
185a51aa5d1SPhilip Paeps 	struct glxsb_dma_map	sc_dma;		/* DMA map */
186a51aa5d1SPhilip Paeps 	int32_t			sc_cid;		/* crypto tag */
187a51aa5d1SPhilip Paeps 	uint32_t		sc_sid;		/* session id */
188a51aa5d1SPhilip Paeps 	TAILQ_HEAD(ses_head, glxsb_session)
189a51aa5d1SPhilip Paeps 				sc_sessions;	/* crypto sessions */
190a51aa5d1SPhilip Paeps 	struct mtx		sc_sessions_mtx;/* sessions mutex */
191a51aa5d1SPhilip Paeps 	struct mtx		sc_task_mtx;	/* task mutex */
192a51aa5d1SPhilip Paeps 	struct taskqueue	*sc_tq;		/* task queue */
193a51aa5d1SPhilip Paeps 	struct task		sc_cryptotask;	/* task */
194a51aa5d1SPhilip Paeps 	struct glxsb_taskop	sc_to;		/* task's crypto operation */
195a51aa5d1SPhilip Paeps 	int			sc_task_count;	/* tasks count */
196a51aa5d1SPhilip Paeps };
197a51aa5d1SPhilip Paeps 
198a51aa5d1SPhilip Paeps static int glxsb_probe(device_t);
199a51aa5d1SPhilip Paeps static int glxsb_attach(device_t);
200a51aa5d1SPhilip Paeps static int glxsb_detach(device_t);
201a51aa5d1SPhilip Paeps 
202a51aa5d1SPhilip Paeps static void glxsb_dmamap_cb(void *, bus_dma_segment_t *, int, int);
203a51aa5d1SPhilip Paeps static int  glxsb_dma_alloc(struct glxsb_softc *);
204a51aa5d1SPhilip Paeps static void glxsb_dma_pre_op(struct glxsb_softc *, struct glxsb_dma_map *);
205a51aa5d1SPhilip Paeps static void glxsb_dma_post_op(struct glxsb_softc *, struct glxsb_dma_map *);
206a51aa5d1SPhilip Paeps static void glxsb_dma_free(struct glxsb_softc *, struct glxsb_dma_map *);
207a51aa5d1SPhilip Paeps 
208a51aa5d1SPhilip Paeps static void glxsb_rnd(void *);
209a51aa5d1SPhilip Paeps static int  glxsb_crypto_setup(struct glxsb_softc *);
210a51aa5d1SPhilip Paeps static int  glxsb_crypto_newsession(device_t, uint32_t *, struct cryptoini *);
211a51aa5d1SPhilip Paeps static int  glxsb_crypto_freesession(device_t, uint64_t);
212a51aa5d1SPhilip Paeps static int  glxsb_aes(struct glxsb_softc *, uint32_t, uint32_t,
213a51aa5d1SPhilip Paeps 	uint32_t, void *, int, void *);
214a51aa5d1SPhilip Paeps 
215a51aa5d1SPhilip Paeps static int  glxsb_crypto_encdec(struct cryptop *, struct cryptodesc *,
216a51aa5d1SPhilip Paeps 	struct glxsb_session *, struct glxsb_softc *);
217a51aa5d1SPhilip Paeps 
218a51aa5d1SPhilip Paeps static void glxsb_crypto_task(void *, int);
219a51aa5d1SPhilip Paeps static int  glxsb_crypto_process(device_t, struct cryptop *, int);
220a51aa5d1SPhilip Paeps 
221a51aa5d1SPhilip Paeps static device_method_t glxsb_methods[] = {
222a51aa5d1SPhilip Paeps 	/* device interface */
223a51aa5d1SPhilip Paeps 	DEVMETHOD(device_probe,		glxsb_probe),
224a51aa5d1SPhilip Paeps 	DEVMETHOD(device_attach,	glxsb_attach),
225a51aa5d1SPhilip Paeps 	DEVMETHOD(device_detach,	glxsb_detach),
226a51aa5d1SPhilip Paeps 
227a51aa5d1SPhilip Paeps 	/* crypto device methods */
228a51aa5d1SPhilip Paeps 	DEVMETHOD(cryptodev_newsession,		glxsb_crypto_newsession),
229a51aa5d1SPhilip Paeps 	DEVMETHOD(cryptodev_freesession,	glxsb_crypto_freesession),
230a51aa5d1SPhilip Paeps 	DEVMETHOD(cryptodev_process,		glxsb_crypto_process),
231a51aa5d1SPhilip Paeps 
232a51aa5d1SPhilip Paeps 	{0,0}
233a51aa5d1SPhilip Paeps };
234a51aa5d1SPhilip Paeps 
235a51aa5d1SPhilip Paeps static driver_t glxsb_driver = {
236a51aa5d1SPhilip Paeps 	"glxsb",
237a51aa5d1SPhilip Paeps 	glxsb_methods,
238a51aa5d1SPhilip Paeps 	sizeof (struct glxsb_softc)
239a51aa5d1SPhilip Paeps };
240a51aa5d1SPhilip Paeps 
241a51aa5d1SPhilip Paeps static devclass_t glxsb_devclass;
242a51aa5d1SPhilip Paeps 
243a51aa5d1SPhilip Paeps DRIVER_MODULE(glxsb, pci, glxsb_driver, glxsb_devclass, 0, 0);
244a51aa5d1SPhilip Paeps MODULE_VERSION(glxsb, 1);
245a51aa5d1SPhilip Paeps MODULE_DEPEND(glxsb, crypto, 1, 1, 1);
246a51aa5d1SPhilip Paeps 
247a51aa5d1SPhilip Paeps static int
248a51aa5d1SPhilip Paeps glxsb_probe(device_t dev)
249a51aa5d1SPhilip Paeps {
250a51aa5d1SPhilip Paeps 
251a51aa5d1SPhilip Paeps 	if (pci_get_vendor(dev) == PCI_VENDOR_AMD &&
252a51aa5d1SPhilip Paeps 	    pci_get_device(dev) == PCI_PRODUCT_AMD_GEODE_LX_CRYPTO) {
253a51aa5d1SPhilip Paeps 		device_set_desc(dev,
254a51aa5d1SPhilip Paeps 		    "AMD Geode LX Security Block (AES-128-CBC, RNG)");
255a51aa5d1SPhilip Paeps 		return (BUS_PROBE_DEFAULT);
256a51aa5d1SPhilip Paeps 	}
257a51aa5d1SPhilip Paeps 
258a51aa5d1SPhilip Paeps 	return (ENXIO);
259a51aa5d1SPhilip Paeps }
260a51aa5d1SPhilip Paeps 
261a51aa5d1SPhilip Paeps static int
262a51aa5d1SPhilip Paeps glxsb_attach(device_t dev)
263a51aa5d1SPhilip Paeps {
264a51aa5d1SPhilip Paeps 	struct glxsb_softc *sc = device_get_softc(dev);
265a51aa5d1SPhilip Paeps 	uint64_t msr;
266a51aa5d1SPhilip Paeps 
267a51aa5d1SPhilip Paeps 	sc->sc_dev = dev;
268a51aa5d1SPhilip Paeps 	msr = rdmsr(SB_GLD_MSR_CAP);
269a51aa5d1SPhilip Paeps 
270a51aa5d1SPhilip Paeps 	if ((msr & 0xFFFF00) != 0x130400) {
271a51aa5d1SPhilip Paeps 		device_printf(dev, "unknown ID 0x%x\n",
272a51aa5d1SPhilip Paeps 		    (int)((msr & 0xFFFF00) >> 16));
273a51aa5d1SPhilip Paeps 		goto fail0;
274a51aa5d1SPhilip Paeps 	}
275a51aa5d1SPhilip Paeps 
276a51aa5d1SPhilip Paeps 	pci_enable_busmaster(dev);
277a51aa5d1SPhilip Paeps 
278a51aa5d1SPhilip Paeps 	/* Map in the security block configuration/control registers */
279a51aa5d1SPhilip Paeps 	sc->sc_rid = PCIR_BAR(0);
280a51aa5d1SPhilip Paeps 	sc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
281a51aa5d1SPhilip Paeps 			RF_ACTIVE);
282a51aa5d1SPhilip Paeps 	if (sc->sc_sr == NULL) {
283a51aa5d1SPhilip Paeps 		device_printf(dev, "cannot map register space\n");
284a51aa5d1SPhilip Paeps 		goto fail0;
285a51aa5d1SPhilip Paeps 	}
286a51aa5d1SPhilip Paeps 
287a51aa5d1SPhilip Paeps 	/*
288a51aa5d1SPhilip Paeps 	 * Configure the Security Block.
289a51aa5d1SPhilip Paeps 	 *
290a51aa5d1SPhilip Paeps 	 * We want to enable the noise generator (T_NE), and enable the
291a51aa5d1SPhilip Paeps 	 * linear feedback shift register and whitener post-processing
292a51aa5d1SPhilip Paeps 	 * (T_SEL = 3).  Also ensure that test mode (deterministic values)
293a51aa5d1SPhilip Paeps 	 * is disabled.
294a51aa5d1SPhilip Paeps 	 */
295a51aa5d1SPhilip Paeps 	msr = rdmsr(SB_GLD_MSR_CTRL);
296a51aa5d1SPhilip Paeps 	msr &= ~(SB_GMC_T_TM | SB_GMC_T_SEL_MASK);
297a51aa5d1SPhilip Paeps 	msr |= SB_GMC_T_NE | SB_GMC_T_SEL3;
298a51aa5d1SPhilip Paeps #if 0
299a51aa5d1SPhilip Paeps 	msr |= SB_GMC_SBI | SB_GMC_SBY;		/* for AES, if necessary */
300a51aa5d1SPhilip Paeps #endif
301a51aa5d1SPhilip Paeps 	wrmsr(SB_GLD_MSR_CTRL, msr);
302a51aa5d1SPhilip Paeps 
303a51aa5d1SPhilip Paeps 	/* Disable interrupts */
304a51aa5d1SPhilip Paeps 	bus_write_4(sc->sc_sr, SB_AES_INT, SB_AI_CLEAR_INTR);
305a51aa5d1SPhilip Paeps 
306a51aa5d1SPhilip Paeps 	/* Allocate a contiguous DMA-able buffer to work in */
307a51aa5d1SPhilip Paeps 	if (glxsb_dma_alloc(sc) != 0)
308a51aa5d1SPhilip Paeps 		goto fail1;
309a51aa5d1SPhilip Paeps 
310a51aa5d1SPhilip Paeps 	/* Initialize our task queue */
311a51aa5d1SPhilip Paeps 	sc->sc_tq = taskqueue_create("glxsb_taskq", M_NOWAIT | M_ZERO,
312a51aa5d1SPhilip Paeps 		taskqueue_thread_enqueue, &sc->sc_tq);
313a51aa5d1SPhilip Paeps 	if (sc->sc_tq == NULL) {
314a51aa5d1SPhilip Paeps 		device_printf(dev, "cannot create task queue\n");
315a51aa5d1SPhilip Paeps 		goto fail1;
316a51aa5d1SPhilip Paeps 	}
317a51aa5d1SPhilip Paeps 	if (taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
318a51aa5d1SPhilip Paeps 			device_get_nameunit(dev)) != 0) {
319a51aa5d1SPhilip Paeps 		device_printf(dev, "cannot start task queue\n");
320a51aa5d1SPhilip Paeps 		goto fail2;
321a51aa5d1SPhilip Paeps 	}
322a51aa5d1SPhilip Paeps 	TASK_INIT(&sc->sc_cryptotask, 0, glxsb_crypto_task, sc);
323a51aa5d1SPhilip Paeps 
324a51aa5d1SPhilip Paeps 	/* Initialize crypto */
325a51aa5d1SPhilip Paeps 	if (glxsb_crypto_setup(sc) != 0)
326a51aa5d1SPhilip Paeps 		goto fail2;
327a51aa5d1SPhilip Paeps 
328a51aa5d1SPhilip Paeps 	/* Install a periodic collector for the "true" (AMD's word) RNG */
329a51aa5d1SPhilip Paeps 	if (hz > 100)
330a51aa5d1SPhilip Paeps 		sc->sc_rnghz = hz / 100;
331a51aa5d1SPhilip Paeps 	else
332a51aa5d1SPhilip Paeps 		sc->sc_rnghz = 1;
333a51aa5d1SPhilip Paeps 	callout_init(&sc->sc_rngco, CALLOUT_MPSAFE);
334a51aa5d1SPhilip Paeps 	glxsb_rnd(sc);
335a51aa5d1SPhilip Paeps 
336a51aa5d1SPhilip Paeps 	return (0);
337a51aa5d1SPhilip Paeps 
338a51aa5d1SPhilip Paeps fail2:
339a51aa5d1SPhilip Paeps 	taskqueue_free(sc->sc_tq);
340a51aa5d1SPhilip Paeps fail1:
341a51aa5d1SPhilip Paeps 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_sr);
342a51aa5d1SPhilip Paeps fail0:
343a51aa5d1SPhilip Paeps 	return (ENXIO);
344a51aa5d1SPhilip Paeps }
345a51aa5d1SPhilip Paeps 
346a51aa5d1SPhilip Paeps static int
347a51aa5d1SPhilip Paeps glxsb_detach(device_t dev)
348a51aa5d1SPhilip Paeps {
349a51aa5d1SPhilip Paeps 	struct glxsb_softc *sc = device_get_softc(dev);
350a51aa5d1SPhilip Paeps 	struct glxsb_session *ses;
351a51aa5d1SPhilip Paeps 
352a51aa5d1SPhilip Paeps 	mtx_lock(&sc->sc_sessions_mtx);
353a51aa5d1SPhilip Paeps 	TAILQ_FOREACH(ses, &sc->sc_sessions, ses_next) {
354a51aa5d1SPhilip Paeps 		if (ses->ses_used != 0) {
355a51aa5d1SPhilip Paeps 			mtx_unlock(&sc->sc_sessions_mtx);
356a51aa5d1SPhilip Paeps 			device_printf(dev,
357a51aa5d1SPhilip Paeps 				"cannot detach, sessions still active.\n");
358a51aa5d1SPhilip Paeps 			return (EBUSY);
359a51aa5d1SPhilip Paeps 		}
360a51aa5d1SPhilip Paeps 	}
361a51aa5d1SPhilip Paeps 	for (ses = TAILQ_FIRST(&sc->sc_sessions);
362a51aa5d1SPhilip Paeps 		ses != NULL;
363a51aa5d1SPhilip Paeps 		ses = TAILQ_FIRST(&sc->sc_sessions)) {
364a51aa5d1SPhilip Paeps 
365a51aa5d1SPhilip Paeps 		TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next);
366a51aa5d1SPhilip Paeps 		free(ses, M_GLXSB);
367a51aa5d1SPhilip Paeps 	}
368a51aa5d1SPhilip Paeps 	mtx_unlock(&sc->sc_sessions_mtx);
369a51aa5d1SPhilip Paeps 	crypto_unregister_all(sc->sc_cid);
370a51aa5d1SPhilip Paeps 	callout_drain(&sc->sc_rngco);
371a51aa5d1SPhilip Paeps 	taskqueue_drain(sc->sc_tq, &sc->sc_cryptotask);
372a51aa5d1SPhilip Paeps 	bus_generic_detach(dev);
373a51aa5d1SPhilip Paeps 	glxsb_dma_free(sc, &sc->sc_dma);
374a51aa5d1SPhilip Paeps 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_sr);
375a51aa5d1SPhilip Paeps 	taskqueue_free(sc->sc_tq);
376a51aa5d1SPhilip Paeps 	mtx_destroy(&sc->sc_sessions_mtx);
377a51aa5d1SPhilip Paeps 	mtx_destroy(&sc->sc_task_mtx);
378a51aa5d1SPhilip Paeps 	return (0);
379a51aa5d1SPhilip Paeps }
380a51aa5d1SPhilip Paeps 
381a51aa5d1SPhilip Paeps /*
382a51aa5d1SPhilip Paeps  *	callback for bus_dmamap_load()
383a51aa5d1SPhilip Paeps  */
384a51aa5d1SPhilip Paeps static void
385a51aa5d1SPhilip Paeps glxsb_dmamap_cb(void *arg, bus_dma_segment_t *seg, int nseg, int error)
386a51aa5d1SPhilip Paeps {
387a51aa5d1SPhilip Paeps 
388a51aa5d1SPhilip Paeps 	bus_addr_t *paddr = (bus_addr_t*) arg;
389a51aa5d1SPhilip Paeps 	*paddr = seg[0].ds_addr;
390a51aa5d1SPhilip Paeps }
391a51aa5d1SPhilip Paeps 
392a51aa5d1SPhilip Paeps static int
393a51aa5d1SPhilip Paeps glxsb_dma_alloc(struct glxsb_softc *sc)
394a51aa5d1SPhilip Paeps {
395a51aa5d1SPhilip Paeps 	struct glxsb_dma_map *dma = &sc->sc_dma;
396a51aa5d1SPhilip Paeps 	int rc;
397a51aa5d1SPhilip Paeps 
398a51aa5d1SPhilip Paeps 	dma->dma_nsegs = 1;
399a51aa5d1SPhilip Paeps 	dma->dma_size = GLXSB_MAX_AES_LEN * 2;
400a51aa5d1SPhilip Paeps 
401a51aa5d1SPhilip Paeps 	/* Setup DMA descriptor area */
402a51aa5d1SPhilip Paeps 	rc = bus_dma_tag_create(NULL,			/* parent */
403a51aa5d1SPhilip Paeps 				SB_AES_ALIGN, 0,	/* alignments, bounds */
404a51aa5d1SPhilip Paeps 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
405a51aa5d1SPhilip Paeps 				BUS_SPACE_MAXADDR,	/* highaddr */
406a51aa5d1SPhilip Paeps 				NULL, NULL,		/* filter, filterarg */
407a51aa5d1SPhilip Paeps 				dma->dma_size,		/* maxsize */
408a51aa5d1SPhilip Paeps 				dma->dma_nsegs,		/* nsegments */
409a51aa5d1SPhilip Paeps 				dma->dma_size,		/* maxsegsize */
410a51aa5d1SPhilip Paeps 				BUS_DMA_ALLOCNOW,	/* flags */
411a51aa5d1SPhilip Paeps 				NULL, NULL,		/* lockfunc, lockarg */
412a51aa5d1SPhilip Paeps 				&sc->sc_dmat);
413a51aa5d1SPhilip Paeps 	if (rc != 0) {
414a51aa5d1SPhilip Paeps 		device_printf(sc->sc_dev,
415a51aa5d1SPhilip Paeps 		    "cannot allocate DMA tag (%d)\n", rc);
416a51aa5d1SPhilip Paeps 
417a51aa5d1SPhilip Paeps 		goto fail0;
418a51aa5d1SPhilip Paeps 	}
419a51aa5d1SPhilip Paeps 
420a51aa5d1SPhilip Paeps 	rc = bus_dmamem_alloc(sc->sc_dmat,
421a51aa5d1SPhilip Paeps 	    (void **)&dma->dma_vaddr, BUS_DMA_NOWAIT, &dma->dma_map);
422a51aa5d1SPhilip Paeps 	if (rc != 0) {
423a51aa5d1SPhilip Paeps 		device_printf(sc->sc_dev,
424a51aa5d1SPhilip Paeps 		    "cannot allocate DMA memory of %d bytes (%d)\n",
425a51aa5d1SPhilip Paeps 			dma->dma_size, rc);
426a51aa5d1SPhilip Paeps 		goto fail1;
427a51aa5d1SPhilip Paeps 	}
428a51aa5d1SPhilip Paeps 
429a51aa5d1SPhilip Paeps 	rc = bus_dmamap_load(sc->sc_dmat,
430a51aa5d1SPhilip Paeps 	    dma->dma_map, dma->dma_vaddr, dma->dma_size,
431a51aa5d1SPhilip Paeps 	    glxsb_dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
432a51aa5d1SPhilip Paeps 	if (rc != 0) {
433a51aa5d1SPhilip Paeps 		device_printf(sc->sc_dev,
434a51aa5d1SPhilip Paeps 		    "cannot load DMA memory for %d bytes (%d)\n",
435a51aa5d1SPhilip Paeps 		   dma->dma_size, rc);
436a51aa5d1SPhilip Paeps 
437a51aa5d1SPhilip Paeps 		goto fail2;
438a51aa5d1SPhilip Paeps 	}
439a51aa5d1SPhilip Paeps 
440a51aa5d1SPhilip Paeps 	return (0);
441a51aa5d1SPhilip Paeps 
442a51aa5d1SPhilip Paeps fail2:
443a51aa5d1SPhilip Paeps 	bus_dmamem_free(sc->sc_dmat, dma->dma_vaddr, dma->dma_map);
444a51aa5d1SPhilip Paeps fail1:
445a51aa5d1SPhilip Paeps 	bus_dma_tag_destroy(sc->sc_dmat);
446a51aa5d1SPhilip Paeps fail0:
447a51aa5d1SPhilip Paeps 	return (rc);
448a51aa5d1SPhilip Paeps }
449a51aa5d1SPhilip Paeps 
450a51aa5d1SPhilip Paeps static void
451a51aa5d1SPhilip Paeps glxsb_dma_pre_op(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
452a51aa5d1SPhilip Paeps {
453a51aa5d1SPhilip Paeps 
454a51aa5d1SPhilip Paeps 	bus_dmamap_sync(sc->sc_dmat, dma->dma_map,
455a51aa5d1SPhilip Paeps 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
456a51aa5d1SPhilip Paeps }
457a51aa5d1SPhilip Paeps 
458a51aa5d1SPhilip Paeps static void
459a51aa5d1SPhilip Paeps glxsb_dma_post_op(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
460a51aa5d1SPhilip Paeps {
461a51aa5d1SPhilip Paeps 
462a51aa5d1SPhilip Paeps 	bus_dmamap_sync(sc->sc_dmat, dma->dma_map,
463a51aa5d1SPhilip Paeps 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
464a51aa5d1SPhilip Paeps }
465a51aa5d1SPhilip Paeps 
466a51aa5d1SPhilip Paeps static void
467a51aa5d1SPhilip Paeps glxsb_dma_free(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
468a51aa5d1SPhilip Paeps {
469a51aa5d1SPhilip Paeps 
470a51aa5d1SPhilip Paeps 	bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
471a51aa5d1SPhilip Paeps 	bus_dmamem_free(sc->sc_dmat, dma->dma_vaddr, dma->dma_map);
472a51aa5d1SPhilip Paeps 	bus_dma_tag_destroy(sc->sc_dmat);
473a51aa5d1SPhilip Paeps }
474a51aa5d1SPhilip Paeps 
475a51aa5d1SPhilip Paeps static void
476a51aa5d1SPhilip Paeps glxsb_rnd(void *v)
477a51aa5d1SPhilip Paeps {
478a51aa5d1SPhilip Paeps 	struct glxsb_softc *sc = v;
479a51aa5d1SPhilip Paeps 	uint32_t status, value;
480a51aa5d1SPhilip Paeps 
481a51aa5d1SPhilip Paeps 	status = bus_read_4(sc->sc_sr, SB_RANDOM_NUM_STATUS);
482a51aa5d1SPhilip Paeps 	if (status & SB_RNS_TRNG_VALID) {
483a51aa5d1SPhilip Paeps 		value = bus_read_4(sc->sc_sr, SB_RANDOM_NUM);
484a51aa5d1SPhilip Paeps 		/* feed with one uint32 */
485a51aa5d1SPhilip Paeps 		random_harvest(&value, 4, 32, 0, RANDOM_PURE);
486a51aa5d1SPhilip Paeps 	}
487a51aa5d1SPhilip Paeps 
488a51aa5d1SPhilip Paeps 	callout_reset(&sc->sc_rngco, sc->sc_rnghz, glxsb_rnd, sc);
489a51aa5d1SPhilip Paeps }
490a51aa5d1SPhilip Paeps 
491a51aa5d1SPhilip Paeps static int
492a51aa5d1SPhilip Paeps glxsb_crypto_setup(struct glxsb_softc *sc)
493a51aa5d1SPhilip Paeps {
494a51aa5d1SPhilip Paeps 
495a51aa5d1SPhilip Paeps 	sc->sc_cid = crypto_get_driverid(sc->sc_dev, CRYPTOCAP_F_HARDWARE);
496a51aa5d1SPhilip Paeps 
497a51aa5d1SPhilip Paeps 	if (sc->sc_cid < 0) {
498a51aa5d1SPhilip Paeps 		device_printf(sc->sc_dev, "cannot get crypto driver id\n");
499a51aa5d1SPhilip Paeps 		return (ENOMEM);
500a51aa5d1SPhilip Paeps 	}
501a51aa5d1SPhilip Paeps 
502a51aa5d1SPhilip Paeps 	TAILQ_INIT(&sc->sc_sessions);
503a51aa5d1SPhilip Paeps 	sc->sc_sid = 1;
504a51aa5d1SPhilip Paeps 	mtx_init(&sc->sc_sessions_mtx, "glxsb_sessions_mtx", NULL, MTX_DEF);
505a51aa5d1SPhilip Paeps 	mtx_init(&sc->sc_task_mtx, "glxsb_crypto_mtx", NULL, MTX_DEF);
506a51aa5d1SPhilip Paeps 
507a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0) != 0)
508a51aa5d1SPhilip Paeps 		goto crypto_fail;
509a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_NULL_HMAC, 0, 0) != 0)
510a51aa5d1SPhilip Paeps 		goto crypto_fail;
511a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0) != 0)
512a51aa5d1SPhilip Paeps 		goto crypto_fail;
513a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0) != 0)
514a51aa5d1SPhilip Paeps 		goto crypto_fail;
515a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_RIPEMD160_HMAC, 0, 0) != 0)
516a51aa5d1SPhilip Paeps 		goto crypto_fail;
517a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_SHA2_256_HMAC, 0, 0) != 0)
518a51aa5d1SPhilip Paeps 		goto crypto_fail;
519a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_SHA2_384_HMAC, 0, 0) != 0)
520a51aa5d1SPhilip Paeps 		goto crypto_fail;
521a51aa5d1SPhilip Paeps 	if (crypto_register(sc->sc_cid, CRYPTO_SHA2_512_HMAC, 0, 0) != 0)
522a51aa5d1SPhilip Paeps 		goto crypto_fail;
523a51aa5d1SPhilip Paeps 
524a51aa5d1SPhilip Paeps 	return (0);
525a51aa5d1SPhilip Paeps 
526a51aa5d1SPhilip Paeps crypto_fail:
527a51aa5d1SPhilip Paeps 	device_printf(sc->sc_dev, "cannot register crypto\n");
528a51aa5d1SPhilip Paeps 	crypto_unregister_all(sc->sc_cid);
529a51aa5d1SPhilip Paeps 	mtx_destroy(&sc->sc_sessions_mtx);
530a51aa5d1SPhilip Paeps 	mtx_destroy(&sc->sc_task_mtx);
531a51aa5d1SPhilip Paeps 	return (ENOMEM);
532a51aa5d1SPhilip Paeps }
533a51aa5d1SPhilip Paeps 
534a51aa5d1SPhilip Paeps static int
535a51aa5d1SPhilip Paeps glxsb_crypto_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
536a51aa5d1SPhilip Paeps {
537a51aa5d1SPhilip Paeps 	struct glxsb_softc *sc = device_get_softc(dev);
538a51aa5d1SPhilip Paeps 	struct glxsb_session *ses = NULL;
539a51aa5d1SPhilip Paeps 	struct cryptoini *encini, *macini;
540a51aa5d1SPhilip Paeps 	int error;
541a51aa5d1SPhilip Paeps 
542a51aa5d1SPhilip Paeps 	if (sc == NULL || sidp == NULL || cri == NULL )
543a51aa5d1SPhilip Paeps 		return (EINVAL);
544a51aa5d1SPhilip Paeps 
545a51aa5d1SPhilip Paeps 	encini = macini = NULL;
546a51aa5d1SPhilip Paeps 	for (; cri != NULL; cri = cri->cri_next ) {
547a51aa5d1SPhilip Paeps 		switch(cri->cri_alg) {
548a51aa5d1SPhilip Paeps 		case CRYPTO_NULL_HMAC:
549a51aa5d1SPhilip Paeps 		case CRYPTO_MD5_HMAC:
550a51aa5d1SPhilip Paeps 		case CRYPTO_SHA1_HMAC:
551a51aa5d1SPhilip Paeps 		case CRYPTO_RIPEMD160_HMAC:
552a51aa5d1SPhilip Paeps 		case CRYPTO_SHA2_256_HMAC:
553a51aa5d1SPhilip Paeps 		case CRYPTO_SHA2_384_HMAC:
554a51aa5d1SPhilip Paeps 		case CRYPTO_SHA2_512_HMAC:
555a51aa5d1SPhilip Paeps 			if (macini != NULL)
556a51aa5d1SPhilip Paeps 				return (EINVAL);
557a51aa5d1SPhilip Paeps 			macini = cri;
558a51aa5d1SPhilip Paeps 			break;
559a51aa5d1SPhilip Paeps 		case CRYPTO_AES_CBC:
560a51aa5d1SPhilip Paeps 			if (encini != NULL)
561a51aa5d1SPhilip Paeps 				return (EINVAL);
562a51aa5d1SPhilip Paeps 			encini = cri;
563a51aa5d1SPhilip Paeps 			break;
564a51aa5d1SPhilip Paeps 		default:
565a51aa5d1SPhilip Paeps 			return (EINVAL);
566a51aa5d1SPhilip Paeps 		}
567a51aa5d1SPhilip Paeps 	}
568a51aa5d1SPhilip Paeps 
569a51aa5d1SPhilip Paeps 	/*
570a51aa5d1SPhilip Paeps 	 * We only support HMAC algorithms to be able to work with
571a51aa5d1SPhilip Paeps 	 * ipsec(4), so if we are asked only for authentication without
572a51aa5d1SPhilip Paeps 	 * encryption, don't pretend we can accellerate it.
573a51aa5d1SPhilip Paeps 	 */
574a51aa5d1SPhilip Paeps 	if (encini == NULL)
575a51aa5d1SPhilip Paeps 		return (EINVAL);
576a51aa5d1SPhilip Paeps 
577a51aa5d1SPhilip Paeps 	/*
578a51aa5d1SPhilip Paeps 	 * Look for a free session
579a51aa5d1SPhilip Paeps 	 *
580a51aa5d1SPhilip Paeps 	 * Free sessions goes first, so if first session is used, we need to
581a51aa5d1SPhilip Paeps 	 * allocate one.
582a51aa5d1SPhilip Paeps 	 */
583a51aa5d1SPhilip Paeps 
584a51aa5d1SPhilip Paeps 	mtx_lock(&sc->sc_sessions_mtx);
585a51aa5d1SPhilip Paeps 	ses = TAILQ_FIRST(&sc->sc_sessions);
586a51aa5d1SPhilip Paeps 	if (ses == NULL || ses->ses_used)
587a51aa5d1SPhilip Paeps 	 	ses = NULL;
588a51aa5d1SPhilip Paeps 	else {
589a51aa5d1SPhilip Paeps 		TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next);
590a51aa5d1SPhilip Paeps 		ses->ses_used = 1;
591a51aa5d1SPhilip Paeps 		TAILQ_INSERT_TAIL(&sc->sc_sessions, ses, ses_next);
592a51aa5d1SPhilip Paeps 	}
593a51aa5d1SPhilip Paeps 	mtx_unlock(&sc->sc_sessions_mtx);
594a51aa5d1SPhilip Paeps 	if (ses == NULL) {
595a51aa5d1SPhilip Paeps 		ses = malloc(sizeof(*ses), M_GLXSB, M_NOWAIT | M_ZERO);
596a51aa5d1SPhilip Paeps 		if (ses == NULL)
597a51aa5d1SPhilip Paeps 			return (ENOMEM);
598a51aa5d1SPhilip Paeps 		ses->ses_used = 1;
599a51aa5d1SPhilip Paeps 		mtx_lock(&sc->sc_sessions_mtx);
600a51aa5d1SPhilip Paeps 		ses->ses_id = sc->sc_sid++;
601a51aa5d1SPhilip Paeps 		TAILQ_INSERT_TAIL(&sc->sc_sessions, ses, ses_next);
602a51aa5d1SPhilip Paeps 		mtx_unlock(&sc->sc_sessions_mtx);
603a51aa5d1SPhilip Paeps 	}
604a51aa5d1SPhilip Paeps 
605a51aa5d1SPhilip Paeps 	if (encini->cri_alg == CRYPTO_AES_CBC) {
606a51aa5d1SPhilip Paeps 		if (encini->cri_klen != 128) {
607a51aa5d1SPhilip Paeps 			glxsb_crypto_freesession(sc->sc_dev, ses->ses_id);
608a51aa5d1SPhilip Paeps 			return (EINVAL);
609a51aa5d1SPhilip Paeps 		}
610a51aa5d1SPhilip Paeps 		arc4rand(ses->ses_iv, sizeof(ses->ses_iv), 0);
611a51aa5d1SPhilip Paeps 		ses->ses_klen = encini->cri_klen;
612a51aa5d1SPhilip Paeps 
613a51aa5d1SPhilip Paeps 		/* Copy the key (Geode LX wants the primary key only) */
614a51aa5d1SPhilip Paeps 		bcopy(encini->cri_key, ses->ses_key, sizeof(ses->ses_key));
615a51aa5d1SPhilip Paeps 	}
616a51aa5d1SPhilip Paeps 
617a51aa5d1SPhilip Paeps 	if (macini != NULL) {
618a51aa5d1SPhilip Paeps 		error = glxsb_hash_setup(ses, macini);
619a51aa5d1SPhilip Paeps 		if (error != 0) {
620a51aa5d1SPhilip Paeps 			glxsb_crypto_freesession(sc->sc_dev, ses->ses_id);
621a51aa5d1SPhilip Paeps 			return (error);
622a51aa5d1SPhilip Paeps 		}
623a51aa5d1SPhilip Paeps 	}
624a51aa5d1SPhilip Paeps 
625a51aa5d1SPhilip Paeps 	*sidp = ses->ses_id;
626a51aa5d1SPhilip Paeps 	return (0);
627a51aa5d1SPhilip Paeps }
628a51aa5d1SPhilip Paeps 
629a51aa5d1SPhilip Paeps static int
630a51aa5d1SPhilip Paeps glxsb_crypto_freesession(device_t dev, uint64_t tid)
631a51aa5d1SPhilip Paeps {
632a51aa5d1SPhilip Paeps 	struct glxsb_softc *sc = device_get_softc(dev);
633a51aa5d1SPhilip Paeps 	struct glxsb_session *ses = NULL;
634a51aa5d1SPhilip Paeps 	uint32_t sid = ((uint32_t)tid) & 0xffffffff;
635a51aa5d1SPhilip Paeps 
636a51aa5d1SPhilip Paeps 	if (sc == NULL)
637a51aa5d1SPhilip Paeps 		return (EINVAL);
638a51aa5d1SPhilip Paeps 
639a51aa5d1SPhilip Paeps 	mtx_lock(&sc->sc_sessions_mtx);
640a51aa5d1SPhilip Paeps 	TAILQ_FOREACH_REVERSE(ses, &sc->sc_sessions, ses_head, ses_next) {
641a51aa5d1SPhilip Paeps 		if (ses->ses_id == sid )
642a51aa5d1SPhilip Paeps 			break;
643a51aa5d1SPhilip Paeps 	}
644a51aa5d1SPhilip Paeps 	if (ses == NULL) {
645a51aa5d1SPhilip Paeps 		mtx_unlock(&sc->sc_sessions_mtx);
646a51aa5d1SPhilip Paeps 		return (EINVAL);
647a51aa5d1SPhilip Paeps 	}
648a51aa5d1SPhilip Paeps 	TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next);
649a51aa5d1SPhilip Paeps 	glxsb_hash_free(ses);
650a51aa5d1SPhilip Paeps 	bzero(ses, sizeof(*ses));
651a51aa5d1SPhilip Paeps 	ses->ses_used = 0;
652a51aa5d1SPhilip Paeps 	ses->ses_id = sid;
653a51aa5d1SPhilip Paeps 	TAILQ_INSERT_HEAD(&sc->sc_sessions, ses, ses_next);
654a51aa5d1SPhilip Paeps 	mtx_unlock(&sc->sc_sessions_mtx);
655a51aa5d1SPhilip Paeps 
656a51aa5d1SPhilip Paeps 	return (0);
657a51aa5d1SPhilip Paeps }
658a51aa5d1SPhilip Paeps 
659a51aa5d1SPhilip Paeps static int
660a51aa5d1SPhilip Paeps glxsb_aes(struct glxsb_softc *sc, uint32_t control, uint32_t psrc,
661a51aa5d1SPhilip Paeps     uint32_t pdst, void *key, int len, void *iv)
662a51aa5d1SPhilip Paeps {
663a51aa5d1SPhilip Paeps 	uint32_t status;
664a51aa5d1SPhilip Paeps 	int i;
665a51aa5d1SPhilip Paeps 
666a51aa5d1SPhilip Paeps 	if (len & 0xF) {
667a51aa5d1SPhilip Paeps 		device_printf(sc->sc_dev,
668a51aa5d1SPhilip Paeps 		    "len must be a multiple of 16 (not %d)\n", len);
669a51aa5d1SPhilip Paeps 		return (EINVAL);
670a51aa5d1SPhilip Paeps 	}
671a51aa5d1SPhilip Paeps 
672a51aa5d1SPhilip Paeps 	/* Set the source */
673a51aa5d1SPhilip Paeps 	bus_write_4(sc->sc_sr, SB_SOURCE_A, psrc);
674a51aa5d1SPhilip Paeps 
675a51aa5d1SPhilip Paeps 	/* Set the destination address */
676a51aa5d1SPhilip Paeps 	bus_write_4(sc->sc_sr, SB_DEST_A, pdst);
677a51aa5d1SPhilip Paeps 
678a51aa5d1SPhilip Paeps 	/* Set the data length */
679a51aa5d1SPhilip Paeps 	bus_write_4(sc->sc_sr, SB_LENGTH_A, len);
680a51aa5d1SPhilip Paeps 
681a51aa5d1SPhilip Paeps 	/* Set the IV */
682a51aa5d1SPhilip Paeps 	if (iv != NULL) {
683a51aa5d1SPhilip Paeps 		bus_write_region_4(sc->sc_sr, SB_CBC_IV, iv, 4);
684a51aa5d1SPhilip Paeps 		control |= SB_CTL_CBC;
685a51aa5d1SPhilip Paeps 	}
686a51aa5d1SPhilip Paeps 
687a51aa5d1SPhilip Paeps 	/* Set the key */
688a51aa5d1SPhilip Paeps 	bus_write_region_4(sc->sc_sr, SB_WKEY, key, 4);
689a51aa5d1SPhilip Paeps 
690a51aa5d1SPhilip Paeps 	/* Ask the security block to do it */
691a51aa5d1SPhilip Paeps 	bus_write_4(sc->sc_sr, SB_CTL_A,
692a51aa5d1SPhilip Paeps 	    control | SB_CTL_WK | SB_CTL_DC | SB_CTL_SC | SB_CTL_ST);
693a51aa5d1SPhilip Paeps 
694a51aa5d1SPhilip Paeps 	/*
695a51aa5d1SPhilip Paeps 	 * Now wait until it is done.
696a51aa5d1SPhilip Paeps 	 *
697a51aa5d1SPhilip Paeps 	 * We do a busy wait.  Obviously the number of iterations of
698a51aa5d1SPhilip Paeps 	 * the loop required to perform the AES operation depends upon
699a51aa5d1SPhilip Paeps 	 * the number of bytes to process.
700a51aa5d1SPhilip Paeps 	 *
701a51aa5d1SPhilip Paeps 	 * On a 500 MHz Geode LX we see
702a51aa5d1SPhilip Paeps 	 *
703a51aa5d1SPhilip Paeps 	 *	length (bytes)	typical max iterations
704a51aa5d1SPhilip Paeps 	 *	    16		   12
705a51aa5d1SPhilip Paeps 	 *	    64		   22
706a51aa5d1SPhilip Paeps 	 *	   256		   59
707a51aa5d1SPhilip Paeps 	 *	  1024		  212
708a51aa5d1SPhilip Paeps 	 *	  8192		1,537
709a51aa5d1SPhilip Paeps 	 *
710a51aa5d1SPhilip Paeps 	 * Since we have a maximum size of operation defined in
711a51aa5d1SPhilip Paeps 	 * GLXSB_MAX_AES_LEN, we use this constant to decide how long
712a51aa5d1SPhilip Paeps 	 * to wait.  Allow an order of magnitude longer than it should
713a51aa5d1SPhilip Paeps 	 * really take, just in case.
714a51aa5d1SPhilip Paeps 	 */
715a51aa5d1SPhilip Paeps 
716a51aa5d1SPhilip Paeps 	for (i = 0; i < GLXSB_MAX_AES_LEN * 10; i++) {
717a51aa5d1SPhilip Paeps 		status = bus_read_4(sc->sc_sr, SB_CTL_A);
718a51aa5d1SPhilip Paeps 		if ((status & SB_CTL_ST) == 0)		/* Done */
719a51aa5d1SPhilip Paeps 			return(0);
720a51aa5d1SPhilip Paeps 	}
721a51aa5d1SPhilip Paeps 
722a51aa5d1SPhilip Paeps 	device_printf(sc->sc_dev, "operation failed to complete\n");
723a51aa5d1SPhilip Paeps 	return (EIO);
724a51aa5d1SPhilip Paeps }
725a51aa5d1SPhilip Paeps 
726a51aa5d1SPhilip Paeps static int
727a51aa5d1SPhilip Paeps glxsb_crypto_encdec(struct cryptop *crp, struct cryptodesc *crd,
728a51aa5d1SPhilip Paeps     struct glxsb_session *ses, struct glxsb_softc *sc)
729a51aa5d1SPhilip Paeps {
730a51aa5d1SPhilip Paeps 	char *op_src, *op_dst;
731a51aa5d1SPhilip Paeps 	uint32_t op_psrc, op_pdst;
732a51aa5d1SPhilip Paeps 	uint8_t op_iv[SB_AES_BLOCK_SIZE], *piv;
733a51aa5d1SPhilip Paeps 	int err = 0;
734a51aa5d1SPhilip Paeps 	int len, tlen, xlen;
735a51aa5d1SPhilip Paeps 	int offset;
736a51aa5d1SPhilip Paeps 	uint32_t control;
737a51aa5d1SPhilip Paeps 
738a51aa5d1SPhilip Paeps 	if (crd == NULL || (crd->crd_len % SB_AES_BLOCK_SIZE) != 0) {
739a51aa5d1SPhilip Paeps 		err = EINVAL;
740a51aa5d1SPhilip Paeps 		goto out;
741a51aa5d1SPhilip Paeps 	}
742a51aa5d1SPhilip Paeps 
743a51aa5d1SPhilip Paeps 	/* How much of our buffer will we need to use? */
744a51aa5d1SPhilip Paeps 	xlen = crd->crd_len > GLXSB_MAX_AES_LEN ?
745a51aa5d1SPhilip Paeps 	    GLXSB_MAX_AES_LEN : crd->crd_len;
746a51aa5d1SPhilip Paeps 
747a51aa5d1SPhilip Paeps 	/*
748a51aa5d1SPhilip Paeps 	 * XXX Check if we can have input == output on Geode LX.
749a51aa5d1SPhilip Paeps 	 * XXX In the meantime, use two separate (adjacent) buffers.
750a51aa5d1SPhilip Paeps 	 */
751a51aa5d1SPhilip Paeps 	op_src = sc->sc_dma.dma_vaddr;
752a51aa5d1SPhilip Paeps 	op_dst = (char *)sc->sc_dma.dma_vaddr + xlen;
753a51aa5d1SPhilip Paeps 
754a51aa5d1SPhilip Paeps 	op_psrc = sc->sc_dma.dma_paddr;
755a51aa5d1SPhilip Paeps 	op_pdst = sc->sc_dma.dma_paddr + xlen;
756a51aa5d1SPhilip Paeps 
757a51aa5d1SPhilip Paeps 	if (crd->crd_flags & CRD_F_ENCRYPT) {
758a51aa5d1SPhilip Paeps 		control = SB_CTL_ENC;
759a51aa5d1SPhilip Paeps 		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
760a51aa5d1SPhilip Paeps 			bcopy(crd->crd_iv, op_iv, sizeof(op_iv));
761a51aa5d1SPhilip Paeps 		else
762a51aa5d1SPhilip Paeps 			bcopy(ses->ses_iv, op_iv, sizeof(op_iv));
763a51aa5d1SPhilip Paeps 
764a51aa5d1SPhilip Paeps 		if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
765a51aa5d1SPhilip Paeps 			crypto_copyback(crp->crp_flags, crp->crp_buf,
766a51aa5d1SPhilip Paeps 				crd->crd_inject, sizeof(op_iv), op_iv);
767a51aa5d1SPhilip Paeps 		}
768a51aa5d1SPhilip Paeps 	} else {
769a51aa5d1SPhilip Paeps 		control = SB_CTL_DEC;
770a51aa5d1SPhilip Paeps 		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
771a51aa5d1SPhilip Paeps 			bcopy(crd->crd_iv, op_iv, sizeof(op_iv));
772a51aa5d1SPhilip Paeps 		else {
773a51aa5d1SPhilip Paeps 			crypto_copydata(crp->crp_flags, crp->crp_buf,
774a51aa5d1SPhilip Paeps 				crd->crd_inject, sizeof(op_iv), op_iv);
775a51aa5d1SPhilip Paeps 		}
776a51aa5d1SPhilip Paeps 	}
777a51aa5d1SPhilip Paeps 
778a51aa5d1SPhilip Paeps 	offset = 0;
779a51aa5d1SPhilip Paeps 	tlen = crd->crd_len;
780a51aa5d1SPhilip Paeps 	piv = op_iv;
781a51aa5d1SPhilip Paeps 
782a51aa5d1SPhilip Paeps 	/* Process the data in GLXSB_MAX_AES_LEN chunks */
783a51aa5d1SPhilip Paeps 	while (tlen > 0) {
784a51aa5d1SPhilip Paeps 		len = (tlen > GLXSB_MAX_AES_LEN) ? GLXSB_MAX_AES_LEN : tlen;
785a51aa5d1SPhilip Paeps 		crypto_copydata(crp->crp_flags, crp->crp_buf,
786a51aa5d1SPhilip Paeps 			    crd->crd_skip + offset, len, op_src);
787a51aa5d1SPhilip Paeps 
788a51aa5d1SPhilip Paeps 		glxsb_dma_pre_op(sc, &sc->sc_dma);
789a51aa5d1SPhilip Paeps 
790a51aa5d1SPhilip Paeps 		err = glxsb_aes(sc, control, op_psrc, op_pdst, ses->ses_key,
791a51aa5d1SPhilip Paeps 		    len, op_iv);
792a51aa5d1SPhilip Paeps 
793a51aa5d1SPhilip Paeps 		glxsb_dma_post_op(sc, &sc->sc_dma);
794a51aa5d1SPhilip Paeps 		if (err != 0)
795a51aa5d1SPhilip Paeps 			goto out;
796a51aa5d1SPhilip Paeps 
797a51aa5d1SPhilip Paeps 		crypto_copyback(crp->crp_flags, crp->crp_buf,
798a51aa5d1SPhilip Paeps 			crd->crd_skip + offset, len, op_dst);
799a51aa5d1SPhilip Paeps 
800a51aa5d1SPhilip Paeps 		offset += len;
801a51aa5d1SPhilip Paeps 		tlen -= len;
802a51aa5d1SPhilip Paeps 
803a51aa5d1SPhilip Paeps 		if (tlen <= 0) {	/* Ideally, just == 0 */
804a51aa5d1SPhilip Paeps 			/* Finished - put the IV in session IV */
805a51aa5d1SPhilip Paeps 			piv = ses->ses_iv;
806a51aa5d1SPhilip Paeps 		}
807a51aa5d1SPhilip Paeps 
808a51aa5d1SPhilip Paeps 		/*
809a51aa5d1SPhilip Paeps 		 * Copy out last block for use as next iteration/session IV.
810a51aa5d1SPhilip Paeps 		 *
811a51aa5d1SPhilip Paeps 		 * piv is set to op_iv[] before the loop starts, but is
812a51aa5d1SPhilip Paeps 		 * set to ses->ses_iv if we're going to exit the loop this
813a51aa5d1SPhilip Paeps 		 * time.
814a51aa5d1SPhilip Paeps 		 */
815a51aa5d1SPhilip Paeps 		if (crd->crd_flags & CRD_F_ENCRYPT) {
816a51aa5d1SPhilip Paeps 			bcopy(op_dst + len - sizeof(op_iv),
817a51aa5d1SPhilip Paeps 			    piv, sizeof(op_iv));
818a51aa5d1SPhilip Paeps 		} else {
819a51aa5d1SPhilip Paeps 			/* Decryption, only need this if another iteration */
820a51aa5d1SPhilip Paeps 			if (tlen > 0) {
821a51aa5d1SPhilip Paeps 				bcopy(op_src + len - sizeof(op_iv),
822a51aa5d1SPhilip Paeps 				    piv, sizeof(op_iv));
823a51aa5d1SPhilip Paeps 			}
824a51aa5d1SPhilip Paeps 		}
825a51aa5d1SPhilip Paeps 	} /* while */
826a51aa5d1SPhilip Paeps 
827a51aa5d1SPhilip Paeps 	/* All AES processing has now been done. */
828a51aa5d1SPhilip Paeps 	bzero(sc->sc_dma.dma_vaddr, xlen * 2);
829a51aa5d1SPhilip Paeps 
830a51aa5d1SPhilip Paeps out:
831a51aa5d1SPhilip Paeps 	return (err);
832a51aa5d1SPhilip Paeps }
833a51aa5d1SPhilip Paeps 
834a51aa5d1SPhilip Paeps static void
835a51aa5d1SPhilip Paeps glxsb_crypto_task(void *arg, int pending)
836a51aa5d1SPhilip Paeps {
837a51aa5d1SPhilip Paeps 	struct glxsb_softc *sc = arg;
838a51aa5d1SPhilip Paeps 	struct glxsb_session *ses;
839a51aa5d1SPhilip Paeps 	struct cryptop *crp;
840a51aa5d1SPhilip Paeps 	struct cryptodesc *enccrd, *maccrd;
841a51aa5d1SPhilip Paeps 	int error = 0;
842a51aa5d1SPhilip Paeps 
843a51aa5d1SPhilip Paeps 	maccrd = sc->sc_to.to_maccrd;
844a51aa5d1SPhilip Paeps 	enccrd = sc->sc_to.to_enccrd;
845a51aa5d1SPhilip Paeps 	crp = sc->sc_to.to_crp;
846a51aa5d1SPhilip Paeps 	ses = sc->sc_to.to_ses;
847a51aa5d1SPhilip Paeps 
848a51aa5d1SPhilip Paeps 	/* Perform data authentication if requested before encryption */
849a51aa5d1SPhilip Paeps 	if (maccrd != NULL && maccrd->crd_next == enccrd) {
850a51aa5d1SPhilip Paeps 		error = glxsb_hash_process(ses, maccrd, crp);
851a51aa5d1SPhilip Paeps 		if (error != 0)
852a51aa5d1SPhilip Paeps 			goto out;
853a51aa5d1SPhilip Paeps 	}
854a51aa5d1SPhilip Paeps 
855a51aa5d1SPhilip Paeps 	error = glxsb_crypto_encdec(crp, enccrd, ses, sc);
856a51aa5d1SPhilip Paeps 	if (error != 0)
857a51aa5d1SPhilip Paeps 		goto out;
858a51aa5d1SPhilip Paeps 
859a51aa5d1SPhilip Paeps 	/* Perform data authentication if requested after encryption */
860a51aa5d1SPhilip Paeps 	if (maccrd != NULL && enccrd->crd_next == maccrd) {
861a51aa5d1SPhilip Paeps 		error = glxsb_hash_process(ses, maccrd, crp);
862a51aa5d1SPhilip Paeps 		if (error != 0)
863a51aa5d1SPhilip Paeps 			goto out;
864a51aa5d1SPhilip Paeps 	}
865a51aa5d1SPhilip Paeps out:
866a51aa5d1SPhilip Paeps 	mtx_lock(&sc->sc_task_mtx);
867a51aa5d1SPhilip Paeps 	sc->sc_task_count--;
868a51aa5d1SPhilip Paeps 	mtx_unlock(&sc->sc_task_mtx);
869a51aa5d1SPhilip Paeps 
870a51aa5d1SPhilip Paeps 	crp->crp_etype = error;
871a51aa5d1SPhilip Paeps 	crypto_unblock(sc->sc_cid, CRYPTO_SYMQ);
872a51aa5d1SPhilip Paeps 	crypto_done(crp);
873a51aa5d1SPhilip Paeps }
874a51aa5d1SPhilip Paeps 
875a51aa5d1SPhilip Paeps static int
876a51aa5d1SPhilip Paeps glxsb_crypto_process(device_t dev, struct cryptop *crp, int hint)
877a51aa5d1SPhilip Paeps {
878a51aa5d1SPhilip Paeps 	struct glxsb_softc *sc = device_get_softc(dev);
879a51aa5d1SPhilip Paeps 	struct glxsb_session *ses;
880a51aa5d1SPhilip Paeps 	struct cryptodesc *crd, *enccrd, *maccrd;
881a51aa5d1SPhilip Paeps 	uint32_t sid;
882a51aa5d1SPhilip Paeps 	int error = 0;
883a51aa5d1SPhilip Paeps 
884a51aa5d1SPhilip Paeps 	enccrd = maccrd = NULL;
885a51aa5d1SPhilip Paeps 
886a51aa5d1SPhilip Paeps 	if (crp == NULL ||
887a51aa5d1SPhilip Paeps 	    crp->crp_callback == NULL || crp->crp_desc == NULL) {
888a51aa5d1SPhilip Paeps 		error = EINVAL;
889a51aa5d1SPhilip Paeps 		goto fail;
890a51aa5d1SPhilip Paeps 	}
891a51aa5d1SPhilip Paeps 
892a51aa5d1SPhilip Paeps 	for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
893a51aa5d1SPhilip Paeps 		switch (crd->crd_alg) {
894a51aa5d1SPhilip Paeps 		case CRYPTO_NULL_HMAC:
895a51aa5d1SPhilip Paeps 		case CRYPTO_MD5_HMAC:
896a51aa5d1SPhilip Paeps 		case CRYPTO_SHA1_HMAC:
897a51aa5d1SPhilip Paeps 		case CRYPTO_RIPEMD160_HMAC:
898a51aa5d1SPhilip Paeps 		case CRYPTO_SHA2_256_HMAC:
899a51aa5d1SPhilip Paeps 		case CRYPTO_SHA2_384_HMAC:
900a51aa5d1SPhilip Paeps 		case CRYPTO_SHA2_512_HMAC:
901a51aa5d1SPhilip Paeps 			if (maccrd != NULL) {
902a51aa5d1SPhilip Paeps 				error = EINVAL;
903a51aa5d1SPhilip Paeps 				goto fail;
904a51aa5d1SPhilip Paeps 			}
905a51aa5d1SPhilip Paeps 			maccrd = crd;
906a51aa5d1SPhilip Paeps 			break;
907a51aa5d1SPhilip Paeps 		case CRYPTO_AES_CBC:
908a51aa5d1SPhilip Paeps 			if (enccrd != NULL) {
909a51aa5d1SPhilip Paeps 				error = EINVAL;
910a51aa5d1SPhilip Paeps 				goto fail;
911a51aa5d1SPhilip Paeps 			}
912a51aa5d1SPhilip Paeps 			enccrd = crd;
913a51aa5d1SPhilip Paeps 			break;
914a51aa5d1SPhilip Paeps 		default:
915a51aa5d1SPhilip Paeps 			error = EINVAL;
916a51aa5d1SPhilip Paeps 			goto fail;
917a51aa5d1SPhilip Paeps 		}
918a51aa5d1SPhilip Paeps 	}
919a51aa5d1SPhilip Paeps 
920a51aa5d1SPhilip Paeps 	if (enccrd == NULL || enccrd->crd_len % AES_BLOCK_LEN != 0) {
921a51aa5d1SPhilip Paeps 		error = EINVAL;
922a51aa5d1SPhilip Paeps 		goto fail;
923a51aa5d1SPhilip Paeps 	}
924a51aa5d1SPhilip Paeps 
925a51aa5d1SPhilip Paeps 	sid = crp->crp_sid & 0xffffffff;
926a51aa5d1SPhilip Paeps 	mtx_lock(&sc->sc_sessions_mtx);
927a51aa5d1SPhilip Paeps 	TAILQ_FOREACH_REVERSE(ses, &sc->sc_sessions, ses_head, ses_next) {
928a51aa5d1SPhilip Paeps 		if (ses->ses_id == sid)
929a51aa5d1SPhilip Paeps 			break;
930a51aa5d1SPhilip Paeps 	}
931a51aa5d1SPhilip Paeps 
932a51aa5d1SPhilip Paeps 	mtx_unlock(&sc->sc_sessions_mtx);
933a51aa5d1SPhilip Paeps 	if ( ses == NULL || ses->ses_used == 0 ) {
934a51aa5d1SPhilip Paeps 		error = EINVAL;
935a51aa5d1SPhilip Paeps 		goto fail;
936a51aa5d1SPhilip Paeps 	}
937a51aa5d1SPhilip Paeps 
938a51aa5d1SPhilip Paeps 	mtx_lock(&sc->sc_task_mtx);
939a51aa5d1SPhilip Paeps 	if (sc->sc_task_count != 0) {
940a51aa5d1SPhilip Paeps 		mtx_unlock(&sc->sc_task_mtx);
941a51aa5d1SPhilip Paeps 		return (ERESTART);
942a51aa5d1SPhilip Paeps 	}
943a51aa5d1SPhilip Paeps 	sc->sc_task_count++;
944a51aa5d1SPhilip Paeps 
945a51aa5d1SPhilip Paeps 	sc->sc_to.to_maccrd = maccrd;
946a51aa5d1SPhilip Paeps 	sc->sc_to.to_enccrd = enccrd;
947a51aa5d1SPhilip Paeps 	sc->sc_to.to_crp = crp;
948a51aa5d1SPhilip Paeps 	sc->sc_to.to_ses = ses;
949a51aa5d1SPhilip Paeps 	mtx_unlock(&sc->sc_task_mtx);
950a51aa5d1SPhilip Paeps 
951a51aa5d1SPhilip Paeps 	taskqueue_enqueue(sc->sc_tq, &sc->sc_cryptotask);
952a51aa5d1SPhilip Paeps 	return(0);
953a51aa5d1SPhilip Paeps 
954a51aa5d1SPhilip Paeps fail:
955a51aa5d1SPhilip Paeps 	crp->crp_etype = error;
956a51aa5d1SPhilip Paeps 	crypto_done(crp);
957a51aa5d1SPhilip Paeps 	return (error);
958a51aa5d1SPhilip Paeps }
959