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