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/param.h>
27a51aa5d1SPhilip Paeps #include <sys/systm.h>
28a51aa5d1SPhilip Paeps #include <sys/bus.h>
29a51aa5d1SPhilip Paeps #include <sys/errno.h>
30a51aa5d1SPhilip Paeps #include <sys/kernel.h>
31a51aa5d1SPhilip Paeps #include <sys/lock.h>
32a51aa5d1SPhilip Paeps #include <sys/malloc.h>
33a51aa5d1SPhilip Paeps #include <sys/mbuf.h>
34a51aa5d1SPhilip Paeps #include <sys/module.h>
35a51aa5d1SPhilip Paeps #include <sys/mutex.h>
36a51aa5d1SPhilip Paeps #include <sys/proc.h>
37a51aa5d1SPhilip Paeps #include <sys/random.h>
38a51aa5d1SPhilip Paeps #include <sys/rman.h>
39d303b48eSPawel Jakub Dawidek #include <sys/rwlock.h>
40a51aa5d1SPhilip Paeps #include <sys/sysctl.h>
41a51aa5d1SPhilip Paeps #include <sys/taskqueue.h>
42a51aa5d1SPhilip Paeps
43a51aa5d1SPhilip Paeps #include <machine/bus.h>
44a51aa5d1SPhilip Paeps #include <machine/cpufunc.h>
45a51aa5d1SPhilip Paeps #include <machine/resource.h>
46a51aa5d1SPhilip Paeps
47a51aa5d1SPhilip Paeps #include <dev/pci/pcivar.h>
48a51aa5d1SPhilip Paeps #include <dev/pci/pcireg.h>
49a51aa5d1SPhilip Paeps
50a51aa5d1SPhilip Paeps #include <opencrypto/cryptodev.h>
51a51aa5d1SPhilip Paeps #include <opencrypto/xform.h>
52a51aa5d1SPhilip Paeps
53a51aa5d1SPhilip Paeps #include "cryptodev_if.h"
54a51aa5d1SPhilip Paeps #include "glxsb.h"
55a51aa5d1SPhilip Paeps
56a51aa5d1SPhilip Paeps #define PCI_VENDOR_AMD 0x1022 /* AMD */
57a51aa5d1SPhilip Paeps #define PCI_PRODUCT_AMD_GEODE_LX_CRYPTO 0x2082 /* Geode LX Crypto */
58a51aa5d1SPhilip Paeps
59a51aa5d1SPhilip Paeps #define SB_GLD_MSR_CAP 0x58002000 /* RO - Capabilities */
60a51aa5d1SPhilip Paeps #define SB_GLD_MSR_CONFIG 0x58002001 /* RW - Master Config */
61a51aa5d1SPhilip Paeps #define SB_GLD_MSR_SMI 0x58002002 /* RW - SMI */
62a51aa5d1SPhilip Paeps #define SB_GLD_MSR_ERROR 0x58002003 /* RW - Error */
63a51aa5d1SPhilip Paeps #define SB_GLD_MSR_PM 0x58002004 /* RW - Power Mgmt */
64a51aa5d1SPhilip Paeps #define SB_GLD_MSR_DIAG 0x58002005 /* RW - Diagnostic */
65a51aa5d1SPhilip Paeps #define SB_GLD_MSR_CTRL 0x58002006 /* RW - Security Block Cntrl */
66a51aa5d1SPhilip Paeps
67a51aa5d1SPhilip Paeps /* For GLD_MSR_CTRL: */
68a51aa5d1SPhilip Paeps #define SB_GMC_DIV0 0x0000 /* AES update divisor values */
69a51aa5d1SPhilip Paeps #define SB_GMC_DIV1 0x0001
70a51aa5d1SPhilip Paeps #define SB_GMC_DIV2 0x0002
71a51aa5d1SPhilip Paeps #define SB_GMC_DIV3 0x0003
72a51aa5d1SPhilip Paeps #define SB_GMC_DIV_MASK 0x0003
73a51aa5d1SPhilip Paeps #define SB_GMC_SBI 0x0004 /* AES swap bits */
74a51aa5d1SPhilip Paeps #define SB_GMC_SBY 0x0008 /* AES swap bytes */
75a51aa5d1SPhilip Paeps #define SB_GMC_TW 0x0010 /* Time write (EEPROM) */
76a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL0 0x0000 /* RNG post-proc: none */
77a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL1 0x0100 /* RNG post-proc: LFSR */
78a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL2 0x0200 /* RNG post-proc: whitener */
79a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL3 0x0300 /* RNG LFSR+whitener */
80a51aa5d1SPhilip Paeps #define SB_GMC_T_SEL_MASK 0x0300
81a51aa5d1SPhilip Paeps #define SB_GMC_T_NE 0x0400 /* Noise (generator) Enable */
82a51aa5d1SPhilip Paeps #define SB_GMC_T_TM 0x0800 /* RNG test mode */
83a51aa5d1SPhilip Paeps /* (deterministic) */
84a51aa5d1SPhilip Paeps
85a51aa5d1SPhilip Paeps /* Security Block configuration/control registers (offsets from base) */
86a51aa5d1SPhilip Paeps #define SB_CTL_A 0x0000 /* RW - SB Control A */
87a51aa5d1SPhilip Paeps #define SB_CTL_B 0x0004 /* RW - SB Control B */
88a51aa5d1SPhilip Paeps #define SB_AES_INT 0x0008 /* RW - SB AES Interrupt */
89a51aa5d1SPhilip Paeps #define SB_SOURCE_A 0x0010 /* RW - Source A */
90a51aa5d1SPhilip Paeps #define SB_DEST_A 0x0014 /* RW - Destination A */
91a51aa5d1SPhilip Paeps #define SB_LENGTH_A 0x0018 /* RW - Length A */
92a51aa5d1SPhilip Paeps #define SB_SOURCE_B 0x0020 /* RW - Source B */
93a51aa5d1SPhilip Paeps #define SB_DEST_B 0x0024 /* RW - Destination B */
94a51aa5d1SPhilip Paeps #define SB_LENGTH_B 0x0028 /* RW - Length B */
95a51aa5d1SPhilip Paeps #define SB_WKEY 0x0030 /* WO - Writable Key 0-3 */
96a51aa5d1SPhilip Paeps #define SB_WKEY_0 0x0030 /* WO - Writable Key 0 */
97a51aa5d1SPhilip Paeps #define SB_WKEY_1 0x0034 /* WO - Writable Key 1 */
98a51aa5d1SPhilip Paeps #define SB_WKEY_2 0x0038 /* WO - Writable Key 2 */
99a51aa5d1SPhilip Paeps #define SB_WKEY_3 0x003C /* WO - Writable Key 3 */
100a51aa5d1SPhilip Paeps #define SB_CBC_IV 0x0040 /* RW - CBC IV 0-3 */
101a51aa5d1SPhilip Paeps #define SB_CBC_IV_0 0x0040 /* RW - CBC IV 0 */
102a51aa5d1SPhilip Paeps #define SB_CBC_IV_1 0x0044 /* RW - CBC IV 1 */
103a51aa5d1SPhilip Paeps #define SB_CBC_IV_2 0x0048 /* RW - CBC IV 2 */
104a51aa5d1SPhilip Paeps #define SB_CBC_IV_3 0x004C /* RW - CBC IV 3 */
105a51aa5d1SPhilip Paeps #define SB_RANDOM_NUM 0x0050 /* RW - Random Number */
106a51aa5d1SPhilip Paeps #define SB_RANDOM_NUM_STATUS 0x0054 /* RW - Random Number Status */
107a51aa5d1SPhilip Paeps #define SB_EEPROM_COMM 0x0800 /* RW - EEPROM Command */
108a51aa5d1SPhilip Paeps #define SB_EEPROM_ADDR 0x0804 /* RW - EEPROM Address */
109a51aa5d1SPhilip Paeps #define SB_EEPROM_DATA 0x0808 /* RW - EEPROM Data */
110a51aa5d1SPhilip Paeps #define SB_EEPROM_SEC_STATE 0x080C /* RW - EEPROM Security State */
111a51aa5d1SPhilip Paeps
112a51aa5d1SPhilip Paeps /* For SB_CTL_A and _B */
113a51aa5d1SPhilip Paeps #define SB_CTL_ST 0x0001 /* Start operation (enc/dec) */
114a51aa5d1SPhilip Paeps #define SB_CTL_ENC 0x0002 /* Encrypt (0 is decrypt) */
115a51aa5d1SPhilip Paeps #define SB_CTL_DEC 0x0000 /* Decrypt */
116a51aa5d1SPhilip Paeps #define SB_CTL_WK 0x0004 /* Use writable key (we set) */
117a51aa5d1SPhilip Paeps #define SB_CTL_DC 0x0008 /* Destination coherent */
118a51aa5d1SPhilip Paeps #define SB_CTL_SC 0x0010 /* Source coherent */
119a51aa5d1SPhilip Paeps #define SB_CTL_CBC 0x0020 /* CBC (0 is ECB) */
120a51aa5d1SPhilip Paeps
121a51aa5d1SPhilip Paeps /* For SB_AES_INT */
122a51aa5d1SPhilip Paeps #define SB_AI_DISABLE_AES_A 0x0001 /* Disable AES A compl int */
123a51aa5d1SPhilip Paeps #define SB_AI_ENABLE_AES_A 0x0000 /* Enable AES A compl int */
124a51aa5d1SPhilip Paeps #define SB_AI_DISABLE_AES_B 0x0002 /* Disable AES B compl int */
125a51aa5d1SPhilip Paeps #define SB_AI_ENABLE_AES_B 0x0000 /* Enable AES B compl int */
126a51aa5d1SPhilip Paeps #define SB_AI_DISABLE_EEPROM 0x0004 /* Disable EEPROM op comp int */
127a51aa5d1SPhilip Paeps #define SB_AI_ENABLE_EEPROM 0x0000 /* Enable EEPROM op compl int */
128a51aa5d1SPhilip Paeps #define SB_AI_AES_A_COMPLETE 0x10000 /* AES A operation complete */
129a51aa5d1SPhilip Paeps #define SB_AI_AES_B_COMPLETE 0x20000 /* AES B operation complete */
130a51aa5d1SPhilip Paeps #define SB_AI_EEPROM_COMPLETE 0x40000 /* EEPROM operation complete */
131a51aa5d1SPhilip Paeps
132a51aa5d1SPhilip Paeps #define SB_AI_CLEAR_INTR \
133a51aa5d1SPhilip Paeps (SB_AI_DISABLE_AES_A | SB_AI_DISABLE_AES_B |\
134a51aa5d1SPhilip Paeps SB_AI_DISABLE_EEPROM | SB_AI_AES_A_COMPLETE |\
135a51aa5d1SPhilip Paeps SB_AI_AES_B_COMPLETE | SB_AI_EEPROM_COMPLETE)
136a51aa5d1SPhilip Paeps
137a51aa5d1SPhilip Paeps #define SB_RNS_TRNG_VALID 0x0001 /* in SB_RANDOM_NUM_STATUS */
138a51aa5d1SPhilip Paeps
139a51aa5d1SPhilip Paeps #define SB_MEM_SIZE 0x0810 /* Size of memory block */
140a51aa5d1SPhilip Paeps
141a51aa5d1SPhilip Paeps #define SB_AES_ALIGN 0x0010 /* Source and dest buffers */
142a51aa5d1SPhilip Paeps /* must be 16-byte aligned */
143a51aa5d1SPhilip Paeps #define SB_AES_BLOCK_SIZE 0x0010
144a51aa5d1SPhilip Paeps
145a51aa5d1SPhilip Paeps /*
146a51aa5d1SPhilip Paeps * The Geode LX security block AES acceleration doesn't perform scatter-
147a51aa5d1SPhilip Paeps * gather: it just takes source and destination addresses. Therefore the
148a51aa5d1SPhilip Paeps * plain- and ciphertexts need to be contiguous. To this end, we allocate
149a51aa5d1SPhilip Paeps * a buffer for both, and accept the overhead of copying in and out. If
150a51aa5d1SPhilip Paeps * the number of bytes in one operation is bigger than allowed for by the
151a51aa5d1SPhilip Paeps * buffer (buffer is twice the size of the max length, as it has both input
152a51aa5d1SPhilip Paeps * and output) then we have to perform multiple encryptions/decryptions.
153a51aa5d1SPhilip Paeps */
154a51aa5d1SPhilip Paeps
155a51aa5d1SPhilip Paeps #define GLXSB_MAX_AES_LEN 16384
156a51aa5d1SPhilip Paeps
157a51aa5d1SPhilip Paeps MALLOC_DEFINE(M_GLXSB, "glxsb_data", "Glxsb Data");
158a51aa5d1SPhilip Paeps
159a51aa5d1SPhilip Paeps struct glxsb_dma_map {
160a51aa5d1SPhilip Paeps bus_dmamap_t dma_map; /* DMA map */
161a51aa5d1SPhilip Paeps bus_dma_segment_t dma_seg; /* segments */
162a51aa5d1SPhilip Paeps int dma_nsegs; /* #segments */
163a51aa5d1SPhilip Paeps int dma_size; /* size */
164a51aa5d1SPhilip Paeps caddr_t dma_vaddr; /* virtual address */
165a51aa5d1SPhilip Paeps bus_addr_t dma_paddr; /* physical address */
166a51aa5d1SPhilip Paeps };
167a51aa5d1SPhilip Paeps
168a51aa5d1SPhilip Paeps struct glxsb_taskop {
169a51aa5d1SPhilip Paeps struct glxsb_session *to_ses; /* crypto session */
170a51aa5d1SPhilip Paeps struct cryptop *to_crp; /* cryptop to perfom */
171a51aa5d1SPhilip Paeps };
172a51aa5d1SPhilip Paeps
173a51aa5d1SPhilip Paeps struct glxsb_softc {
174a51aa5d1SPhilip Paeps device_t sc_dev; /* device backpointer */
175a51aa5d1SPhilip Paeps struct resource *sc_sr; /* resource */
176a51aa5d1SPhilip Paeps int sc_rid; /* resource rid */
177a51aa5d1SPhilip Paeps struct callout sc_rngco; /* RNG callout */
178a51aa5d1SPhilip Paeps int sc_rnghz; /* RNG callout ticks */
179a51aa5d1SPhilip Paeps bus_dma_tag_t sc_dmat; /* DMA tag */
180a51aa5d1SPhilip Paeps struct glxsb_dma_map sc_dma; /* DMA map */
181a51aa5d1SPhilip Paeps int32_t sc_cid; /* crypto tag */
182a51aa5d1SPhilip Paeps struct mtx sc_task_mtx; /* task mutex */
183a51aa5d1SPhilip Paeps struct taskqueue *sc_tq; /* task queue */
184a51aa5d1SPhilip Paeps struct task sc_cryptotask; /* task */
185a51aa5d1SPhilip Paeps struct glxsb_taskop sc_to; /* task's crypto operation */
186a51aa5d1SPhilip Paeps int sc_task_count; /* tasks count */
187a51aa5d1SPhilip Paeps };
188a51aa5d1SPhilip Paeps
189a51aa5d1SPhilip Paeps static int glxsb_probe(device_t);
190a51aa5d1SPhilip Paeps static int glxsb_attach(device_t);
191a51aa5d1SPhilip Paeps static int glxsb_detach(device_t);
192a51aa5d1SPhilip Paeps
193a51aa5d1SPhilip Paeps static void glxsb_dmamap_cb(void *, bus_dma_segment_t *, int, int);
194a51aa5d1SPhilip Paeps static int glxsb_dma_alloc(struct glxsb_softc *);
195a51aa5d1SPhilip Paeps static void glxsb_dma_pre_op(struct glxsb_softc *, struct glxsb_dma_map *);
196a51aa5d1SPhilip Paeps static void glxsb_dma_post_op(struct glxsb_softc *, struct glxsb_dma_map *);
197a51aa5d1SPhilip Paeps static void glxsb_dma_free(struct glxsb_softc *, struct glxsb_dma_map *);
198a51aa5d1SPhilip Paeps
199a51aa5d1SPhilip Paeps static void glxsb_rnd(void *);
200a51aa5d1SPhilip Paeps static int glxsb_crypto_setup(struct glxsb_softc *);
201c0341432SJohn Baldwin static int glxsb_crypto_probesession(device_t,
202c0341432SJohn Baldwin const struct crypto_session_params *);
203c0341432SJohn Baldwin static int glxsb_crypto_newsession(device_t, crypto_session_t,
204c0341432SJohn Baldwin const struct crypto_session_params *);
205a2d5cc8fSConrad Meyer static void glxsb_crypto_freesession(device_t, crypto_session_t);
206a51aa5d1SPhilip Paeps static int glxsb_aes(struct glxsb_softc *, uint32_t, uint32_t,
207c0341432SJohn Baldwin uint32_t, const void *, int, const void *);
208a51aa5d1SPhilip Paeps
209c0341432SJohn Baldwin static int glxsb_crypto_encdec(struct cryptop *, struct glxsb_session *,
210c0341432SJohn Baldwin struct glxsb_softc *);
211a51aa5d1SPhilip Paeps
212a51aa5d1SPhilip Paeps static void glxsb_crypto_task(void *, int);
213a51aa5d1SPhilip Paeps static int glxsb_crypto_process(device_t, struct cryptop *, int);
214a51aa5d1SPhilip Paeps
215a51aa5d1SPhilip Paeps static device_method_t glxsb_methods[] = {
216a51aa5d1SPhilip Paeps /* device interface */
217a51aa5d1SPhilip Paeps DEVMETHOD(device_probe, glxsb_probe),
218a51aa5d1SPhilip Paeps DEVMETHOD(device_attach, glxsb_attach),
219a51aa5d1SPhilip Paeps DEVMETHOD(device_detach, glxsb_detach),
220a51aa5d1SPhilip Paeps
221a51aa5d1SPhilip Paeps /* crypto device methods */
222c0341432SJohn Baldwin DEVMETHOD(cryptodev_probesession, glxsb_crypto_probesession),
223a51aa5d1SPhilip Paeps DEVMETHOD(cryptodev_newsession, glxsb_crypto_newsession),
224a51aa5d1SPhilip Paeps DEVMETHOD(cryptodev_freesession, glxsb_crypto_freesession),
225a51aa5d1SPhilip Paeps DEVMETHOD(cryptodev_process, glxsb_crypto_process),
226a51aa5d1SPhilip Paeps
227a51aa5d1SPhilip Paeps {0,0}
228a51aa5d1SPhilip Paeps };
229a51aa5d1SPhilip Paeps
230a51aa5d1SPhilip Paeps static driver_t glxsb_driver = {
231a51aa5d1SPhilip Paeps "glxsb",
232a51aa5d1SPhilip Paeps glxsb_methods,
233a51aa5d1SPhilip Paeps sizeof(struct glxsb_softc)
234a51aa5d1SPhilip Paeps };
235a51aa5d1SPhilip Paeps
236*b272472cSJohn Baldwin DRIVER_MODULE(glxsb, pci, glxsb_driver, 0, 0);
237a51aa5d1SPhilip Paeps MODULE_VERSION(glxsb, 1);
238a51aa5d1SPhilip Paeps MODULE_DEPEND(glxsb, crypto, 1, 1, 1);
239a51aa5d1SPhilip Paeps
240a51aa5d1SPhilip Paeps static int
glxsb_probe(device_t dev)241a51aa5d1SPhilip Paeps glxsb_probe(device_t dev)
242a51aa5d1SPhilip Paeps {
243a51aa5d1SPhilip Paeps
244a51aa5d1SPhilip Paeps if (pci_get_vendor(dev) == PCI_VENDOR_AMD &&
245a51aa5d1SPhilip Paeps pci_get_device(dev) == PCI_PRODUCT_AMD_GEODE_LX_CRYPTO) {
246a51aa5d1SPhilip Paeps device_set_desc(dev,
247a51aa5d1SPhilip Paeps "AMD Geode LX Security Block (AES-128-CBC, RNG)");
248a51aa5d1SPhilip Paeps return (BUS_PROBE_DEFAULT);
249a51aa5d1SPhilip Paeps }
250a51aa5d1SPhilip Paeps
251a51aa5d1SPhilip Paeps return (ENXIO);
252a51aa5d1SPhilip Paeps }
253a51aa5d1SPhilip Paeps
254a51aa5d1SPhilip Paeps static int
glxsb_attach(device_t dev)255a51aa5d1SPhilip Paeps glxsb_attach(device_t dev)
256a51aa5d1SPhilip Paeps {
257a51aa5d1SPhilip Paeps struct glxsb_softc *sc = device_get_softc(dev);
258a51aa5d1SPhilip Paeps uint64_t msr;
259a51aa5d1SPhilip Paeps
260a51aa5d1SPhilip Paeps sc->sc_dev = dev;
261a51aa5d1SPhilip Paeps msr = rdmsr(SB_GLD_MSR_CAP);
262a51aa5d1SPhilip Paeps
263a51aa5d1SPhilip Paeps if ((msr & 0xFFFF00) != 0x130400) {
264a51aa5d1SPhilip Paeps device_printf(dev, "unknown ID 0x%x\n",
265a51aa5d1SPhilip Paeps (int)((msr & 0xFFFF00) >> 16));
266d303b48eSPawel Jakub Dawidek return (ENXIO);
267a51aa5d1SPhilip Paeps }
268a51aa5d1SPhilip Paeps
269a51aa5d1SPhilip Paeps pci_enable_busmaster(dev);
270a51aa5d1SPhilip Paeps
271a51aa5d1SPhilip Paeps /* Map in the security block configuration/control registers */
272a51aa5d1SPhilip Paeps sc->sc_rid = PCIR_BAR(0);
273a51aa5d1SPhilip Paeps sc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
274a51aa5d1SPhilip Paeps RF_ACTIVE);
275a51aa5d1SPhilip Paeps if (sc->sc_sr == NULL) {
276a51aa5d1SPhilip Paeps device_printf(dev, "cannot map register space\n");
277d303b48eSPawel Jakub Dawidek return (ENXIO);
278a51aa5d1SPhilip Paeps }
279a51aa5d1SPhilip Paeps
280a51aa5d1SPhilip Paeps /*
281a51aa5d1SPhilip Paeps * Configure the Security Block.
282a51aa5d1SPhilip Paeps *
283a51aa5d1SPhilip Paeps * We want to enable the noise generator (T_NE), and enable the
284a51aa5d1SPhilip Paeps * linear feedback shift register and whitener post-processing
285a51aa5d1SPhilip Paeps * (T_SEL = 3). Also ensure that test mode (deterministic values)
286a51aa5d1SPhilip Paeps * is disabled.
287a51aa5d1SPhilip Paeps */
288a51aa5d1SPhilip Paeps msr = rdmsr(SB_GLD_MSR_CTRL);
289a51aa5d1SPhilip Paeps msr &= ~(SB_GMC_T_TM | SB_GMC_T_SEL_MASK);
290a51aa5d1SPhilip Paeps msr |= SB_GMC_T_NE | SB_GMC_T_SEL3;
291a51aa5d1SPhilip Paeps #if 0
292a51aa5d1SPhilip Paeps msr |= SB_GMC_SBI | SB_GMC_SBY; /* for AES, if necessary */
293a51aa5d1SPhilip Paeps #endif
294a51aa5d1SPhilip Paeps wrmsr(SB_GLD_MSR_CTRL, msr);
295a51aa5d1SPhilip Paeps
296a51aa5d1SPhilip Paeps /* Disable interrupts */
297a51aa5d1SPhilip Paeps bus_write_4(sc->sc_sr, SB_AES_INT, SB_AI_CLEAR_INTR);
298a51aa5d1SPhilip Paeps
299a51aa5d1SPhilip Paeps /* Allocate a contiguous DMA-able buffer to work in */
300a51aa5d1SPhilip Paeps if (glxsb_dma_alloc(sc) != 0)
301d303b48eSPawel Jakub Dawidek goto fail0;
302a51aa5d1SPhilip Paeps
303a51aa5d1SPhilip Paeps /* Initialize our task queue */
304a51aa5d1SPhilip Paeps sc->sc_tq = taskqueue_create("glxsb_taskq", M_NOWAIT | M_ZERO,
305a51aa5d1SPhilip Paeps taskqueue_thread_enqueue, &sc->sc_tq);
306a51aa5d1SPhilip Paeps if (sc->sc_tq == NULL) {
307a51aa5d1SPhilip Paeps device_printf(dev, "cannot create task queue\n");
308d303b48eSPawel Jakub Dawidek goto fail0;
309a51aa5d1SPhilip Paeps }
310a51aa5d1SPhilip Paeps if (taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
311a51aa5d1SPhilip Paeps device_get_nameunit(dev)) != 0) {
312a51aa5d1SPhilip Paeps device_printf(dev, "cannot start task queue\n");
313d303b48eSPawel Jakub Dawidek goto fail1;
314a51aa5d1SPhilip Paeps }
315a51aa5d1SPhilip Paeps TASK_INIT(&sc->sc_cryptotask, 0, glxsb_crypto_task, sc);
316a51aa5d1SPhilip Paeps
317a51aa5d1SPhilip Paeps /* Initialize crypto */
318a51aa5d1SPhilip Paeps if (glxsb_crypto_setup(sc) != 0)
319d303b48eSPawel Jakub Dawidek goto fail1;
320a51aa5d1SPhilip Paeps
321a51aa5d1SPhilip Paeps /* Install a periodic collector for the "true" (AMD's word) RNG */
322a51aa5d1SPhilip Paeps if (hz > 100)
323a51aa5d1SPhilip Paeps sc->sc_rnghz = hz / 100;
324a51aa5d1SPhilip Paeps else
325a51aa5d1SPhilip Paeps sc->sc_rnghz = 1;
326fd90e2edSJung-uk Kim callout_init(&sc->sc_rngco, 1);
327a51aa5d1SPhilip Paeps glxsb_rnd(sc);
328a51aa5d1SPhilip Paeps
329a51aa5d1SPhilip Paeps return (0);
330a51aa5d1SPhilip Paeps
331a51aa5d1SPhilip Paeps fail1:
332d303b48eSPawel Jakub Dawidek taskqueue_free(sc->sc_tq);
333a51aa5d1SPhilip Paeps fail0:
334d303b48eSPawel Jakub Dawidek bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_sr);
335a51aa5d1SPhilip Paeps return (ENXIO);
336a51aa5d1SPhilip Paeps }
337a51aa5d1SPhilip Paeps
338a51aa5d1SPhilip Paeps static int
glxsb_detach(device_t dev)339a51aa5d1SPhilip Paeps glxsb_detach(device_t dev)
340a51aa5d1SPhilip Paeps {
341a51aa5d1SPhilip Paeps struct glxsb_softc *sc = device_get_softc(dev);
342a51aa5d1SPhilip Paeps
343a51aa5d1SPhilip Paeps crypto_unregister_all(sc->sc_cid);
344a2d5cc8fSConrad Meyer
345a51aa5d1SPhilip Paeps callout_drain(&sc->sc_rngco);
346a51aa5d1SPhilip Paeps taskqueue_drain(sc->sc_tq, &sc->sc_cryptotask);
347a51aa5d1SPhilip Paeps bus_generic_detach(dev);
348a51aa5d1SPhilip Paeps glxsb_dma_free(sc, &sc->sc_dma);
349a51aa5d1SPhilip Paeps bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_sr);
350a51aa5d1SPhilip Paeps taskqueue_free(sc->sc_tq);
351a51aa5d1SPhilip Paeps mtx_destroy(&sc->sc_task_mtx);
352a51aa5d1SPhilip Paeps return (0);
353a51aa5d1SPhilip Paeps }
354a51aa5d1SPhilip Paeps
355a51aa5d1SPhilip Paeps /*
356a51aa5d1SPhilip Paeps * callback for bus_dmamap_load()
357a51aa5d1SPhilip Paeps */
358a51aa5d1SPhilip Paeps static void
glxsb_dmamap_cb(void * arg,bus_dma_segment_t * seg,int nseg,int error)359a51aa5d1SPhilip Paeps glxsb_dmamap_cb(void *arg, bus_dma_segment_t *seg, int nseg, int error)
360a51aa5d1SPhilip Paeps {
361a51aa5d1SPhilip Paeps
362a51aa5d1SPhilip Paeps bus_addr_t *paddr = (bus_addr_t*) arg;
363a51aa5d1SPhilip Paeps *paddr = seg[0].ds_addr;
364a51aa5d1SPhilip Paeps }
365a51aa5d1SPhilip Paeps
366a51aa5d1SPhilip Paeps static int
glxsb_dma_alloc(struct glxsb_softc * sc)367a51aa5d1SPhilip Paeps glxsb_dma_alloc(struct glxsb_softc *sc)
368a51aa5d1SPhilip Paeps {
369a51aa5d1SPhilip Paeps struct glxsb_dma_map *dma = &sc->sc_dma;
370a51aa5d1SPhilip Paeps int rc;
371a51aa5d1SPhilip Paeps
372a51aa5d1SPhilip Paeps dma->dma_nsegs = 1;
373a51aa5d1SPhilip Paeps dma->dma_size = GLXSB_MAX_AES_LEN * 2;
374a51aa5d1SPhilip Paeps
375a51aa5d1SPhilip Paeps /* Setup DMA descriptor area */
37662ce43ccSScott Long rc = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */
377a51aa5d1SPhilip Paeps SB_AES_ALIGN, 0, /* alignments, bounds */
378a51aa5d1SPhilip Paeps BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
379a51aa5d1SPhilip Paeps BUS_SPACE_MAXADDR, /* highaddr */
380a51aa5d1SPhilip Paeps NULL, NULL, /* filter, filterarg */
381a51aa5d1SPhilip Paeps dma->dma_size, /* maxsize */
382a51aa5d1SPhilip Paeps dma->dma_nsegs, /* nsegments */
383a51aa5d1SPhilip Paeps dma->dma_size, /* maxsegsize */
384a51aa5d1SPhilip Paeps BUS_DMA_ALLOCNOW, /* flags */
385a51aa5d1SPhilip Paeps NULL, NULL, /* lockfunc, lockarg */
386a51aa5d1SPhilip Paeps &sc->sc_dmat);
387a51aa5d1SPhilip Paeps if (rc != 0) {
388a51aa5d1SPhilip Paeps device_printf(sc->sc_dev,
389a51aa5d1SPhilip Paeps "cannot allocate DMA tag (%d)\n", rc);
390d303b48eSPawel Jakub Dawidek return (rc);
391a51aa5d1SPhilip Paeps }
392a51aa5d1SPhilip Paeps
393d303b48eSPawel Jakub Dawidek rc = bus_dmamem_alloc(sc->sc_dmat, (void **)&dma->dma_vaddr,
394d303b48eSPawel Jakub Dawidek BUS_DMA_NOWAIT, &dma->dma_map);
395a51aa5d1SPhilip Paeps if (rc != 0) {
396a51aa5d1SPhilip Paeps device_printf(sc->sc_dev,
397a51aa5d1SPhilip Paeps "cannot allocate DMA memory of %d bytes (%d)\n",
398a51aa5d1SPhilip Paeps dma->dma_size, rc);
399d303b48eSPawel Jakub Dawidek goto fail0;
400a51aa5d1SPhilip Paeps }
401a51aa5d1SPhilip Paeps
402d303b48eSPawel Jakub Dawidek rc = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_vaddr,
403d303b48eSPawel Jakub Dawidek dma->dma_size, glxsb_dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
404a51aa5d1SPhilip Paeps if (rc != 0) {
405a51aa5d1SPhilip Paeps device_printf(sc->sc_dev,
406a51aa5d1SPhilip Paeps "cannot load DMA memory for %d bytes (%d)\n",
407a51aa5d1SPhilip Paeps dma->dma_size, rc);
408d303b48eSPawel Jakub Dawidek goto fail1;
409a51aa5d1SPhilip Paeps }
410a51aa5d1SPhilip Paeps
411a51aa5d1SPhilip Paeps return (0);
412a51aa5d1SPhilip Paeps
413a51aa5d1SPhilip Paeps fail1:
414d303b48eSPawel Jakub Dawidek bus_dmamem_free(sc->sc_dmat, dma->dma_vaddr, dma->dma_map);
415a51aa5d1SPhilip Paeps fail0:
416d303b48eSPawel Jakub Dawidek bus_dma_tag_destroy(sc->sc_dmat);
417a51aa5d1SPhilip Paeps return (rc);
418a51aa5d1SPhilip Paeps }
419a51aa5d1SPhilip Paeps
420a51aa5d1SPhilip Paeps static void
glxsb_dma_pre_op(struct glxsb_softc * sc,struct glxsb_dma_map * dma)421a51aa5d1SPhilip Paeps glxsb_dma_pre_op(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
422a51aa5d1SPhilip Paeps {
423a51aa5d1SPhilip Paeps
424a51aa5d1SPhilip Paeps bus_dmamap_sync(sc->sc_dmat, dma->dma_map,
425a51aa5d1SPhilip Paeps BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
426a51aa5d1SPhilip Paeps }
427a51aa5d1SPhilip Paeps
428a51aa5d1SPhilip Paeps static void
glxsb_dma_post_op(struct glxsb_softc * sc,struct glxsb_dma_map * dma)429a51aa5d1SPhilip Paeps glxsb_dma_post_op(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
430a51aa5d1SPhilip Paeps {
431a51aa5d1SPhilip Paeps
432a51aa5d1SPhilip Paeps bus_dmamap_sync(sc->sc_dmat, dma->dma_map,
433a51aa5d1SPhilip Paeps BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
434a51aa5d1SPhilip Paeps }
435a51aa5d1SPhilip Paeps
436a51aa5d1SPhilip Paeps static void
glxsb_dma_free(struct glxsb_softc * sc,struct glxsb_dma_map * dma)437a51aa5d1SPhilip Paeps glxsb_dma_free(struct glxsb_softc *sc, struct glxsb_dma_map *dma)
438a51aa5d1SPhilip Paeps {
439a51aa5d1SPhilip Paeps
440a51aa5d1SPhilip Paeps bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
441a51aa5d1SPhilip Paeps bus_dmamem_free(sc->sc_dmat, dma->dma_vaddr, dma->dma_map);
442a51aa5d1SPhilip Paeps bus_dma_tag_destroy(sc->sc_dmat);
443a51aa5d1SPhilip Paeps }
444a51aa5d1SPhilip Paeps
445a51aa5d1SPhilip Paeps static void
glxsb_rnd(void * v)446a51aa5d1SPhilip Paeps glxsb_rnd(void *v)
447a51aa5d1SPhilip Paeps {
448a51aa5d1SPhilip Paeps struct glxsb_softc *sc = v;
449a51aa5d1SPhilip Paeps uint32_t status, value;
450a51aa5d1SPhilip Paeps
451a51aa5d1SPhilip Paeps status = bus_read_4(sc->sc_sr, SB_RANDOM_NUM_STATUS);
452a51aa5d1SPhilip Paeps if (status & SB_RNS_TRNG_VALID) {
453a51aa5d1SPhilip Paeps value = bus_read_4(sc->sc_sr, SB_RANDOM_NUM);
454a51aa5d1SPhilip Paeps /* feed with one uint32 */
455d1b06863SMark Murray /* MarkM: FIX!! Check that this does not swamp the harvester! */
45619fa89e9SMark Murray random_harvest_queue(&value, sizeof(value), RANDOM_PURE_GLXSB);
457a51aa5d1SPhilip Paeps }
458a51aa5d1SPhilip Paeps
459a51aa5d1SPhilip Paeps callout_reset(&sc->sc_rngco, sc->sc_rnghz, glxsb_rnd, sc);
460a51aa5d1SPhilip Paeps }
461a51aa5d1SPhilip Paeps
462a51aa5d1SPhilip Paeps static int
glxsb_crypto_setup(struct glxsb_softc * sc)463a51aa5d1SPhilip Paeps glxsb_crypto_setup(struct glxsb_softc *sc)
464a51aa5d1SPhilip Paeps {
465a51aa5d1SPhilip Paeps
466a2d5cc8fSConrad Meyer sc->sc_cid = crypto_get_driverid(sc->sc_dev,
467a2d5cc8fSConrad Meyer sizeof(struct glxsb_session), CRYPTOCAP_F_HARDWARE);
468a51aa5d1SPhilip Paeps
469a51aa5d1SPhilip Paeps if (sc->sc_cid < 0) {
470a51aa5d1SPhilip Paeps device_printf(sc->sc_dev, "cannot get crypto driver id\n");
471a51aa5d1SPhilip Paeps return (ENOMEM);
472a51aa5d1SPhilip Paeps }
473a51aa5d1SPhilip Paeps
474a51aa5d1SPhilip Paeps mtx_init(&sc->sc_task_mtx, "glxsb_crypto_mtx", NULL, MTX_DEF);
475a51aa5d1SPhilip Paeps
476a51aa5d1SPhilip Paeps return (0);
477a51aa5d1SPhilip Paeps }
478a51aa5d1SPhilip Paeps
479a51aa5d1SPhilip Paeps static int
glxsb_crypto_probesession(device_t dev,const struct crypto_session_params * csp)480c0341432SJohn Baldwin glxsb_crypto_probesession(device_t dev, const struct crypto_session_params *csp)
481a51aa5d1SPhilip Paeps {
482a51aa5d1SPhilip Paeps
483c0341432SJohn Baldwin if (csp->csp_flags != 0)
484a51aa5d1SPhilip Paeps return (EINVAL);
485a51aa5d1SPhilip Paeps
486c0341432SJohn Baldwin /*
487c0341432SJohn Baldwin * We only support HMAC algorithms to be able to work with
488c0341432SJohn Baldwin * ipsec(4), so if we are asked only for authentication without
489c0341432SJohn Baldwin * encryption, don't pretend we can accelerate it.
490c0341432SJohn Baldwin */
491c0341432SJohn Baldwin switch (csp->csp_mode) {
492c0341432SJohn Baldwin case CSP_MODE_ETA:
493c0341432SJohn Baldwin switch (csp->csp_auth_alg) {
494a51aa5d1SPhilip Paeps case CRYPTO_NULL_HMAC:
495a51aa5d1SPhilip Paeps case CRYPTO_SHA1_HMAC:
496a51aa5d1SPhilip Paeps case CRYPTO_RIPEMD160_HMAC:
497a51aa5d1SPhilip Paeps case CRYPTO_SHA2_256_HMAC:
498a51aa5d1SPhilip Paeps case CRYPTO_SHA2_384_HMAC:
499a51aa5d1SPhilip Paeps case CRYPTO_SHA2_512_HMAC:
500a51aa5d1SPhilip Paeps break;
501a51aa5d1SPhilip Paeps default:
502a51aa5d1SPhilip Paeps return (EINVAL);
503a51aa5d1SPhilip Paeps }
504c0341432SJohn Baldwin /* FALLTHROUGH */
505c0341432SJohn Baldwin case CSP_MODE_CIPHER:
506c0341432SJohn Baldwin switch (csp->csp_cipher_alg) {
507c0341432SJohn Baldwin case CRYPTO_AES_CBC:
508c0341432SJohn Baldwin if (csp->csp_cipher_klen * 8 != 128)
509c0341432SJohn Baldwin return (EINVAL);
510c0341432SJohn Baldwin break;
511c0341432SJohn Baldwin default:
512c0341432SJohn Baldwin return (EINVAL);
513c0341432SJohn Baldwin }
514c0341432SJohn Baldwin default:
515c0341432SJohn Baldwin return (EINVAL);
516c0341432SJohn Baldwin }
517c0341432SJohn Baldwin return (CRYPTODEV_PROBE_HARDWARE);
518a51aa5d1SPhilip Paeps }
519a51aa5d1SPhilip Paeps
520c0341432SJohn Baldwin static int
glxsb_crypto_newsession(device_t dev,crypto_session_t cses,const struct crypto_session_params * csp)521c0341432SJohn Baldwin glxsb_crypto_newsession(device_t dev, crypto_session_t cses,
522c0341432SJohn Baldwin const struct crypto_session_params *csp)
523c0341432SJohn Baldwin {
524c0341432SJohn Baldwin struct glxsb_softc *sc = device_get_softc(dev);
525c0341432SJohn Baldwin struct glxsb_session *ses;
526c0341432SJohn Baldwin int error;
527a51aa5d1SPhilip Paeps
528a2d5cc8fSConrad Meyer ses = crypto_get_driver_session(cses);
529a51aa5d1SPhilip Paeps
530a51aa5d1SPhilip Paeps /* Copy the key (Geode LX wants the primary key only) */
531c0341432SJohn Baldwin if (csp->csp_cipher_key != NULL)
532c0341432SJohn Baldwin bcopy(csp->csp_cipher_key, ses->ses_key, sizeof(ses->ses_key));
533a51aa5d1SPhilip Paeps
534c0341432SJohn Baldwin if (csp->csp_auth_alg != 0) {
535c0341432SJohn Baldwin error = glxsb_hash_setup(ses, csp);
536a51aa5d1SPhilip Paeps if (error != 0) {
537a2d5cc8fSConrad Meyer glxsb_crypto_freesession(sc->sc_dev, cses);
538a51aa5d1SPhilip Paeps return (error);
539a51aa5d1SPhilip Paeps }
540a51aa5d1SPhilip Paeps }
541a51aa5d1SPhilip Paeps
542a51aa5d1SPhilip Paeps return (0);
543a51aa5d1SPhilip Paeps }
544a51aa5d1SPhilip Paeps
545a2d5cc8fSConrad Meyer static void
glxsb_crypto_freesession(device_t dev,crypto_session_t cses)546a2d5cc8fSConrad Meyer glxsb_crypto_freesession(device_t dev, crypto_session_t cses)
547a51aa5d1SPhilip Paeps {
548a2d5cc8fSConrad Meyer struct glxsb_session *ses;
549a51aa5d1SPhilip Paeps
550a2d5cc8fSConrad Meyer ses = crypto_get_driver_session(cses);
551a51aa5d1SPhilip Paeps glxsb_hash_free(ses);
552a51aa5d1SPhilip Paeps }
553a51aa5d1SPhilip Paeps
554a51aa5d1SPhilip Paeps static int
glxsb_aes(struct glxsb_softc * sc,uint32_t control,uint32_t psrc,uint32_t pdst,const void * key,int len,const void * iv)555a51aa5d1SPhilip Paeps glxsb_aes(struct glxsb_softc *sc, uint32_t control, uint32_t psrc,
556c0341432SJohn Baldwin uint32_t pdst, const void *key, int len, const void *iv)
557a51aa5d1SPhilip Paeps {
558a51aa5d1SPhilip Paeps uint32_t status;
559a51aa5d1SPhilip Paeps int i;
560a51aa5d1SPhilip Paeps
561a51aa5d1SPhilip Paeps if (len & 0xF) {
562a51aa5d1SPhilip Paeps device_printf(sc->sc_dev,
563a51aa5d1SPhilip Paeps "len must be a multiple of 16 (not %d)\n", len);
564a51aa5d1SPhilip Paeps return (EINVAL);
565a51aa5d1SPhilip Paeps }
566a51aa5d1SPhilip Paeps
567a51aa5d1SPhilip Paeps /* Set the source */
568a51aa5d1SPhilip Paeps bus_write_4(sc->sc_sr, SB_SOURCE_A, psrc);
569a51aa5d1SPhilip Paeps
570a51aa5d1SPhilip Paeps /* Set the destination address */
571a51aa5d1SPhilip Paeps bus_write_4(sc->sc_sr, SB_DEST_A, pdst);
572a51aa5d1SPhilip Paeps
573a51aa5d1SPhilip Paeps /* Set the data length */
574a51aa5d1SPhilip Paeps bus_write_4(sc->sc_sr, SB_LENGTH_A, len);
575a51aa5d1SPhilip Paeps
576a51aa5d1SPhilip Paeps /* Set the IV */
577a51aa5d1SPhilip Paeps if (iv != NULL) {
578a51aa5d1SPhilip Paeps bus_write_region_4(sc->sc_sr, SB_CBC_IV, iv, 4);
579a51aa5d1SPhilip Paeps control |= SB_CTL_CBC;
580a51aa5d1SPhilip Paeps }
581a51aa5d1SPhilip Paeps
582a51aa5d1SPhilip Paeps /* Set the key */
583a51aa5d1SPhilip Paeps bus_write_region_4(sc->sc_sr, SB_WKEY, key, 4);
584a51aa5d1SPhilip Paeps
585a51aa5d1SPhilip Paeps /* Ask the security block to do it */
586a51aa5d1SPhilip Paeps bus_write_4(sc->sc_sr, SB_CTL_A,
587a51aa5d1SPhilip Paeps control | SB_CTL_WK | SB_CTL_DC | SB_CTL_SC | SB_CTL_ST);
588a51aa5d1SPhilip Paeps
589a51aa5d1SPhilip Paeps /*
590a51aa5d1SPhilip Paeps * Now wait until it is done.
591a51aa5d1SPhilip Paeps *
592a51aa5d1SPhilip Paeps * We do a busy wait. Obviously the number of iterations of
593a51aa5d1SPhilip Paeps * the loop required to perform the AES operation depends upon
594a51aa5d1SPhilip Paeps * the number of bytes to process.
595a51aa5d1SPhilip Paeps *
596a51aa5d1SPhilip Paeps * On a 500 MHz Geode LX we see
597a51aa5d1SPhilip Paeps *
598a51aa5d1SPhilip Paeps * length (bytes) typical max iterations
599a51aa5d1SPhilip Paeps * 16 12
600a51aa5d1SPhilip Paeps * 64 22
601a51aa5d1SPhilip Paeps * 256 59
602a51aa5d1SPhilip Paeps * 1024 212
603a51aa5d1SPhilip Paeps * 8192 1,537
604a51aa5d1SPhilip Paeps *
605a51aa5d1SPhilip Paeps * Since we have a maximum size of operation defined in
606a51aa5d1SPhilip Paeps * GLXSB_MAX_AES_LEN, we use this constant to decide how long
607a51aa5d1SPhilip Paeps * to wait. Allow an order of magnitude longer than it should
608a51aa5d1SPhilip Paeps * really take, just in case.
609a51aa5d1SPhilip Paeps */
610a51aa5d1SPhilip Paeps
611a51aa5d1SPhilip Paeps for (i = 0; i < GLXSB_MAX_AES_LEN * 10; i++) {
612a51aa5d1SPhilip Paeps status = bus_read_4(sc->sc_sr, SB_CTL_A);
613a51aa5d1SPhilip Paeps if ((status & SB_CTL_ST) == 0) /* Done */
614a51aa5d1SPhilip Paeps return (0);
615a51aa5d1SPhilip Paeps }
616a51aa5d1SPhilip Paeps
617a51aa5d1SPhilip Paeps device_printf(sc->sc_dev, "operation failed to complete\n");
618a51aa5d1SPhilip Paeps return (EIO);
619a51aa5d1SPhilip Paeps }
620a51aa5d1SPhilip Paeps
621a51aa5d1SPhilip Paeps static int
glxsb_crypto_encdec(struct cryptop * crp,struct glxsb_session * ses,struct glxsb_softc * sc)622c0341432SJohn Baldwin glxsb_crypto_encdec(struct cryptop *crp, struct glxsb_session *ses,
623c0341432SJohn Baldwin struct glxsb_softc *sc)
624a51aa5d1SPhilip Paeps {
625a51aa5d1SPhilip Paeps char *op_src, *op_dst;
626c0341432SJohn Baldwin const void *key;
627a51aa5d1SPhilip Paeps uint32_t op_psrc, op_pdst;
628c0341432SJohn Baldwin uint8_t op_iv[SB_AES_BLOCK_SIZE];
629d303b48eSPawel Jakub Dawidek int error;
630a51aa5d1SPhilip Paeps int len, tlen, xlen;
631a51aa5d1SPhilip Paeps int offset;
632a51aa5d1SPhilip Paeps uint32_t control;
633a51aa5d1SPhilip Paeps
634c0341432SJohn Baldwin if ((crp->crp_payload_length % SB_AES_BLOCK_SIZE) != 0)
635d303b48eSPawel Jakub Dawidek return (EINVAL);
636a51aa5d1SPhilip Paeps
637a51aa5d1SPhilip Paeps /* How much of our buffer will we need to use? */
638c0341432SJohn Baldwin xlen = crp->crp_payload_length > GLXSB_MAX_AES_LEN ?
639c0341432SJohn Baldwin GLXSB_MAX_AES_LEN : crp->crp_payload_length;
640a51aa5d1SPhilip Paeps
641a51aa5d1SPhilip Paeps /*
642a51aa5d1SPhilip Paeps * XXX Check if we can have input == output on Geode LX.
643a51aa5d1SPhilip Paeps * XXX In the meantime, use two separate (adjacent) buffers.
644a51aa5d1SPhilip Paeps */
645a51aa5d1SPhilip Paeps op_src = sc->sc_dma.dma_vaddr;
646a51aa5d1SPhilip Paeps op_dst = (char *)sc->sc_dma.dma_vaddr + xlen;
647a51aa5d1SPhilip Paeps
648a51aa5d1SPhilip Paeps op_psrc = sc->sc_dma.dma_paddr;
649a51aa5d1SPhilip Paeps op_pdst = sc->sc_dma.dma_paddr + xlen;
650a51aa5d1SPhilip Paeps
651c0341432SJohn Baldwin if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
652a51aa5d1SPhilip Paeps control = SB_CTL_ENC;
653a51aa5d1SPhilip Paeps else
654a51aa5d1SPhilip Paeps control = SB_CTL_DEC;
655c0341432SJohn Baldwin
65629fe41ddSJohn Baldwin crypto_read_iv(crp, op_iv);
657a51aa5d1SPhilip Paeps
658a51aa5d1SPhilip Paeps offset = 0;
659c0341432SJohn Baldwin tlen = crp->crp_payload_length;
660c0341432SJohn Baldwin
661c0341432SJohn Baldwin if (crp->crp_cipher_key != NULL)
662c0341432SJohn Baldwin key = crp->crp_cipher_key;
663c0341432SJohn Baldwin else
664c0341432SJohn Baldwin key = ses->ses_key;
665a51aa5d1SPhilip Paeps
666a51aa5d1SPhilip Paeps /* Process the data in GLXSB_MAX_AES_LEN chunks */
667a51aa5d1SPhilip Paeps while (tlen > 0) {
668a51aa5d1SPhilip Paeps len = (tlen > GLXSB_MAX_AES_LEN) ? GLXSB_MAX_AES_LEN : tlen;
669c0341432SJohn Baldwin crypto_copydata(crp, crp->crp_payload_start + offset, len,
670c0341432SJohn Baldwin op_src);
671a51aa5d1SPhilip Paeps
672a51aa5d1SPhilip Paeps glxsb_dma_pre_op(sc, &sc->sc_dma);
673a51aa5d1SPhilip Paeps
674c0341432SJohn Baldwin error = glxsb_aes(sc, control, op_psrc, op_pdst, key, len,
675c0341432SJohn Baldwin op_iv);
676a51aa5d1SPhilip Paeps
677a51aa5d1SPhilip Paeps glxsb_dma_post_op(sc, &sc->sc_dma);
678d303b48eSPawel Jakub Dawidek if (error != 0)
679d303b48eSPawel Jakub Dawidek return (error);
680a51aa5d1SPhilip Paeps
681c0341432SJohn Baldwin crypto_copyback(crp, crp->crp_payload_start + offset, len,
682c0341432SJohn Baldwin op_dst);
683a51aa5d1SPhilip Paeps
684a51aa5d1SPhilip Paeps offset += len;
685a51aa5d1SPhilip Paeps tlen -= len;
686a51aa5d1SPhilip Paeps
687a51aa5d1SPhilip Paeps /*
688c0341432SJohn Baldwin * Copy out last block for use as next iteration IV.
689a51aa5d1SPhilip Paeps */
690c0341432SJohn Baldwin if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
691c0341432SJohn Baldwin bcopy(op_dst + len - sizeof(op_iv), op_iv,
692d303b48eSPawel Jakub Dawidek sizeof(op_iv));
693c0341432SJohn Baldwin else
694c0341432SJohn Baldwin bcopy(op_src + len - sizeof(op_iv), op_iv,
695c0341432SJohn Baldwin sizeof(op_iv));
696a51aa5d1SPhilip Paeps } /* while */
697a51aa5d1SPhilip Paeps
698a51aa5d1SPhilip Paeps /* All AES processing has now been done. */
699a51aa5d1SPhilip Paeps bzero(sc->sc_dma.dma_vaddr, xlen * 2);
700a51aa5d1SPhilip Paeps
701d303b48eSPawel Jakub Dawidek return (0);
702a51aa5d1SPhilip Paeps }
703a51aa5d1SPhilip Paeps
704a51aa5d1SPhilip Paeps static void
glxsb_crypto_task(void * arg,int pending)705a51aa5d1SPhilip Paeps glxsb_crypto_task(void *arg, int pending)
706a51aa5d1SPhilip Paeps {
707a51aa5d1SPhilip Paeps struct glxsb_softc *sc = arg;
708c0341432SJohn Baldwin const struct crypto_session_params *csp;
709a51aa5d1SPhilip Paeps struct glxsb_session *ses;
710a51aa5d1SPhilip Paeps struct cryptop *crp;
711d303b48eSPawel Jakub Dawidek int error;
712a51aa5d1SPhilip Paeps
713a51aa5d1SPhilip Paeps crp = sc->sc_to.to_crp;
714a51aa5d1SPhilip Paeps ses = sc->sc_to.to_ses;
715c0341432SJohn Baldwin csp = crypto_get_params(crp->crp_session);
716a51aa5d1SPhilip Paeps
717a51aa5d1SPhilip Paeps /* Perform data authentication if requested before encryption */
718c0341432SJohn Baldwin if (csp->csp_mode == CSP_MODE_ETA &&
719c0341432SJohn Baldwin !CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
720c0341432SJohn Baldwin error = glxsb_hash_process(ses, csp, crp);
721a51aa5d1SPhilip Paeps if (error != 0)
722a51aa5d1SPhilip Paeps goto out;
723a51aa5d1SPhilip Paeps }
724a51aa5d1SPhilip Paeps
725c0341432SJohn Baldwin error = glxsb_crypto_encdec(crp, ses, sc);
726a51aa5d1SPhilip Paeps if (error != 0)
727a51aa5d1SPhilip Paeps goto out;
728a51aa5d1SPhilip Paeps
729a51aa5d1SPhilip Paeps /* Perform data authentication if requested after encryption */
730c0341432SJohn Baldwin if (csp->csp_mode == CSP_MODE_ETA &&
731c0341432SJohn Baldwin CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
732c0341432SJohn Baldwin error = glxsb_hash_process(ses, csp, crp);
733a51aa5d1SPhilip Paeps if (error != 0)
734a51aa5d1SPhilip Paeps goto out;
735a51aa5d1SPhilip Paeps }
736a51aa5d1SPhilip Paeps out:
737a51aa5d1SPhilip Paeps mtx_lock(&sc->sc_task_mtx);
738a51aa5d1SPhilip Paeps sc->sc_task_count--;
739a51aa5d1SPhilip Paeps mtx_unlock(&sc->sc_task_mtx);
740a51aa5d1SPhilip Paeps
741a51aa5d1SPhilip Paeps crp->crp_etype = error;
742a51aa5d1SPhilip Paeps crypto_unblock(sc->sc_cid, CRYPTO_SYMQ);
743a51aa5d1SPhilip Paeps crypto_done(crp);
744a51aa5d1SPhilip Paeps }
745a51aa5d1SPhilip Paeps
746a51aa5d1SPhilip Paeps static int
glxsb_crypto_process(device_t dev,struct cryptop * crp,int hint)747a51aa5d1SPhilip Paeps glxsb_crypto_process(device_t dev, struct cryptop *crp, int hint)
748a51aa5d1SPhilip Paeps {
749a51aa5d1SPhilip Paeps struct glxsb_softc *sc = device_get_softc(dev);
750a51aa5d1SPhilip Paeps struct glxsb_session *ses;
751a51aa5d1SPhilip Paeps
752a2d5cc8fSConrad Meyer ses = crypto_get_driver_session(crp->crp_session);
753a51aa5d1SPhilip Paeps
754a51aa5d1SPhilip Paeps mtx_lock(&sc->sc_task_mtx);
755a51aa5d1SPhilip Paeps if (sc->sc_task_count != 0) {
756a51aa5d1SPhilip Paeps mtx_unlock(&sc->sc_task_mtx);
757a51aa5d1SPhilip Paeps return (ERESTART);
758a51aa5d1SPhilip Paeps }
759a51aa5d1SPhilip Paeps sc->sc_task_count++;
760a51aa5d1SPhilip Paeps
761a51aa5d1SPhilip Paeps sc->sc_to.to_crp = crp;
762a51aa5d1SPhilip Paeps sc->sc_to.to_ses = ses;
763a51aa5d1SPhilip Paeps mtx_unlock(&sc->sc_task_mtx);
764a51aa5d1SPhilip Paeps
765a51aa5d1SPhilip Paeps taskqueue_enqueue(sc->sc_tq, &sc->sc_cryptotask);
766a51aa5d1SPhilip Paeps return(0);
767a51aa5d1SPhilip Paeps }
768