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