ubsec.c revision 1.30 1 /* $NetBSD: ubsec.c,v 1.30 2013/11/17 16:54:02 bad Exp $ */
2 /* $FreeBSD: src/sys/dev/ubsec/ubsec.c,v 1.6.2.6 2003/01/23 21:06:43 sam Exp $ */
3 /* $OpenBSD: ubsec.c,v 1.127 2003/06/04 14:04:58 jason Exp $ */
4
5 /*
6 * Copyright (c) 2000 Jason L. Wright (jason (at) thought.net)
7 * Copyright (c) 2000 Theo de Raadt (deraadt (at) openbsd.org)
8 * Copyright (c) 2001 Patrik Lindergren (patrik (at) ipunplugged.com)
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Effort sponsored in part by the Defense Advanced Research Projects
32 * Agency (DARPA) and Air Force Research Laboratory, Air Force
33 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
34 *
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ubsec.c,v 1.30 2013/11/17 16:54:02 bad Exp $");
39
40 #undef UBSEC_DEBUG
41
42 /*
43 * uBsec 5[56]01, bcm580xx, bcm582x hardware crypto accelerator
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/endian.h>
50 #ifdef __NetBSD__
51 #define UBSEC_NO_RNG /* hangs on attach */
52 #define letoh16 htole16
53 #define letoh32 htole32
54 #endif
55 #include <sys/errno.h>
56 #include <sys/malloc.h>
57 #include <sys/kernel.h>
58 #include <sys/mbuf.h>
59 #include <sys/device.h>
60 #include <sys/queue.h>
61
62 #include <opencrypto/cryptodev.h>
63 #include <opencrypto/xform.h>
64 #ifdef __OpenBSD__
65 #include <dev/rndvar.h>
66 #include <sys/md5k.h>
67 #else
68 #include <sys/cprng.h>
69 #include <sys/md5.h>
70 #endif
71 #include <sys/sha1.h>
72
73 #include <dev/pci/pcireg.h>
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcidevs.h>
76
77 #include <dev/pci/ubsecreg.h>
78 #include <dev/pci/ubsecvar.h>
79
80 /*
81 * Prototypes and count for the pci_device structure
82 */
83 static int ubsec_probe(device_t, cfdata_t, void *);
84 static void ubsec_attach(device_t, device_t, void *);
85 static void ubsec_reset_board(struct ubsec_softc *);
86 static void ubsec_init_board(struct ubsec_softc *);
87 static void ubsec_init_pciregs(struct pci_attach_args *pa);
88 static void ubsec_cleanchip(struct ubsec_softc *);
89 static void ubsec_totalreset(struct ubsec_softc *);
90 static int ubsec_free_q(struct ubsec_softc*, struct ubsec_q *);
91
92 #ifdef __OpenBSD__
93 struct cfattach ubsec_ca = {
94 sizeof(struct ubsec_softc), ubsec_probe, ubsec_attach,
95 };
96
97 struct cfdriver ubsec_cd = {
98 0, "ubsec", DV_DULL
99 };
100 #else
101 CFATTACH_DECL_NEW(ubsec, sizeof(struct ubsec_softc), ubsec_probe, ubsec_attach,
102 NULL, NULL);
103 extern struct cfdriver ubsec_cd;
104 #endif
105
106 /* patchable */
107 #ifdef UBSEC_DEBUG
108 extern int ubsec_debug;
109 int ubsec_debug=1;
110 #endif
111
112 static int ubsec_intr(void *);
113 static int ubsec_newsession(void*, u_int32_t *, struct cryptoini *);
114 static int ubsec_freesession(void*, u_int64_t);
115 static int ubsec_process(void*, struct cryptop *, int hint);
116 static void ubsec_callback(struct ubsec_softc *, struct ubsec_q *);
117 static void ubsec_feed(struct ubsec_softc *);
118 static void ubsec_mcopy(struct mbuf *, struct mbuf *, int, int);
119 static void ubsec_callback2(struct ubsec_softc *, struct ubsec_q2 *);
120 static void ubsec_feed2(struct ubsec_softc *);
121 #ifndef UBSEC_NO_RNG
122 static void ubsec_rng(void *);
123 static void ubsec_rng_locked(void *);
124 static void ubsec_rng_get(size_t, void *);
125 #endif /* UBSEC_NO_RNG */
126 static int ubsec_dma_malloc(struct ubsec_softc *, bus_size_t,
127 struct ubsec_dma_alloc *, int);
128 static void ubsec_dma_free(struct ubsec_softc *, struct ubsec_dma_alloc *);
129 static int ubsec_dmamap_aligned(bus_dmamap_t);
130
131 static int ubsec_kprocess(void*, struct cryptkop *, int);
132 static int ubsec_kprocess_modexp_sw(struct ubsec_softc *,
133 struct cryptkop *, int);
134 static int ubsec_kprocess_modexp_hw(struct ubsec_softc *,
135 struct cryptkop *, int);
136 static int ubsec_kprocess_rsapriv(struct ubsec_softc *,
137 struct cryptkop *, int);
138 static void ubsec_kfree(struct ubsec_softc *, struct ubsec_q2 *);
139 static int ubsec_ksigbits(struct crparam *);
140 static void ubsec_kshift_r(u_int, u_int8_t *, u_int, u_int8_t *, u_int);
141 static void ubsec_kshift_l(u_int, u_int8_t *, u_int, u_int8_t *, u_int);
142
143 #ifdef UBSEC_DEBUG
144 static void ubsec_dump_pb(volatile struct ubsec_pktbuf *);
145 static void ubsec_dump_mcr(struct ubsec_mcr *);
146 static void ubsec_dump_ctx2(volatile struct ubsec_ctx_keyop *);
147 #endif
148
149 #define READ_REG(sc,r) \
150 bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (r))
151
152 #define WRITE_REG(sc,reg,val) \
153 bus_space_write_4((sc)->sc_st, (sc)->sc_sh, reg, val)
154
155 #define SWAP32(x) (x) = htole32(ntohl((x)))
156 #ifndef HTOLE32
157 #define HTOLE32(x) (x) = htole32(x)
158 #endif
159
160 struct ubsec_stats ubsecstats;
161
162 /*
163 * ubsec_maxbatch controls the number of crypto ops to voluntarily
164 * collect into one submission to the hardware. This batching happens
165 * when ops are dispatched from the crypto subsystem with a hint that
166 * more are to follow immediately. These ops must also not be marked
167 * with a ``no delay'' flag.
168 */
169 static int ubsec_maxbatch = 1;
170 #ifdef SYSCTL_INT
171 SYSCTL_INT(_kern, OID_AUTO, ubsec_maxbatch, CTLFLAG_RW, &ubsec_maxbatch,
172 0, "Broadcom driver: max ops to batch w/o interrupt");
173 #endif
174
175 /*
176 * ubsec_maxaggr controls the number of crypto ops to submit to the
177 * hardware as a unit. This aggregation reduces the number of interrupts
178 * to the host at the expense of increased latency (for all but the last
179 * operation). For network traffic setting this to one yields the highest
180 * performance but at the expense of more interrupt processing.
181 */
182 static int ubsec_maxaggr = 1;
183 #ifdef SYSCTL_INT
184 SYSCTL_INT(_kern, OID_AUTO, ubsec_maxaggr, CTLFLAG_RW, &ubsec_maxaggr,
185 0, "Broadcom driver: max ops to aggregate under one interrupt");
186 #endif
187
188 static const struct ubsec_product {
189 pci_vendor_id_t ubsec_vendor;
190 pci_product_id_t ubsec_product;
191 int ubsec_flags;
192 int ubsec_statmask;
193 const char *ubsec_name;
194 } ubsec_products[] = {
195 { PCI_VENDOR_BLUESTEEL, PCI_PRODUCT_BLUESTEEL_5501,
196 0,
197 BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
198 "Bluesteel 5501"
199 },
200 { PCI_VENDOR_BLUESTEEL, PCI_PRODUCT_BLUESTEEL_5601,
201 UBS_FLAGS_KEY | UBS_FLAGS_RNG,
202 BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
203 "Bluesteel 5601"
204 },
205
206 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_5801,
207 0,
208 BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
209 "Broadcom BCM5801"
210 },
211
212 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_5802,
213 UBS_FLAGS_KEY | UBS_FLAGS_RNG,
214 BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
215 "Broadcom BCM5802"
216 },
217
218 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_5805,
219 UBS_FLAGS_KEY | UBS_FLAGS_RNG,
220 BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
221 "Broadcom BCM5805"
222 },
223
224 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_5820,
225 UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
226 UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
227 BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
228 "Broadcom BCM5820"
229 },
230
231 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_5821,
232 UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
233 UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
234 BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
235 BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
236 "Broadcom BCM5821"
237 },
238 { PCI_VENDOR_SUN, PCI_PRODUCT_SUN_SCA1K,
239 UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
240 UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
241 BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
242 BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
243 "Sun Crypto Accelerator 1000"
244 },
245 { PCI_VENDOR_SUN, PCI_PRODUCT_SUN_5821,
246 UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
247 UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
248 BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
249 BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
250 "Broadcom BCM5821 (Sun)"
251 },
252
253 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_5822,
254 UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
255 UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
256 BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
257 BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
258 "Broadcom BCM5822"
259 },
260
261 { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_5823,
262 UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
263 UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
264 BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
265 BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
266 "Broadcom BCM5823"
267 },
268
269 { 0, 0,
270 0,
271 0,
272 NULL
273 }
274 };
275
276 static const struct ubsec_product *
277 ubsec_lookup(const struct pci_attach_args *pa)
278 {
279 const struct ubsec_product *up;
280
281 for (up = ubsec_products; up->ubsec_name != NULL; up++) {
282 if (PCI_VENDOR(pa->pa_id) == up->ubsec_vendor &&
283 PCI_PRODUCT(pa->pa_id) == up->ubsec_product)
284 return (up);
285 }
286 return (NULL);
287 }
288
289 static int
290 ubsec_probe(device_t parent, cfdata_t match, void *aux)
291 {
292 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
293
294 if (ubsec_lookup(pa) != NULL)
295 return (1);
296
297 return (0);
298 }
299
300 static void
301 ubsec_attach(device_t parent, device_t self, void *aux)
302 {
303 struct ubsec_softc *sc = device_private(self);
304 struct pci_attach_args *pa = aux;
305 const struct ubsec_product *up;
306 pci_chipset_tag_t pc = pa->pa_pc;
307 pci_intr_handle_t ih;
308 const char *intrstr = NULL;
309 struct ubsec_dma *dmap;
310 u_int32_t cmd, i;
311
312 sc->sc_dev = self;
313 up = ubsec_lookup(pa);
314 if (up == NULL) {
315 printf("\n");
316 panic("ubsec_attach: impossible");
317 }
318
319 pci_aprint_devinfo_fancy(pa, "Crypto processor", up->ubsec_name, 1);
320
321 SIMPLEQ_INIT(&sc->sc_queue);
322 SIMPLEQ_INIT(&sc->sc_qchip);
323 SIMPLEQ_INIT(&sc->sc_queue2);
324 SIMPLEQ_INIT(&sc->sc_qchip2);
325 SIMPLEQ_INIT(&sc->sc_q2free);
326
327 sc->sc_flags = up->ubsec_flags;
328 sc->sc_statmask = up->ubsec_statmask;
329
330 cmd = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
331 cmd |= PCI_COMMAND_MASTER_ENABLE;
332 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, cmd);
333
334 if (pci_mapreg_map(pa, BS_BAR, PCI_MAPREG_TYPE_MEM, 0,
335 &sc->sc_st, &sc->sc_sh, NULL, NULL)) {
336 aprint_error_dev(self, "can't find mem space");
337 return;
338 }
339
340 sc->sc_dmat = pa->pa_dmat;
341
342 if (pci_intr_map(pa, &ih)) {
343 aprint_error_dev(self, "couldn't map interrupt\n");
344 return;
345 }
346 intrstr = pci_intr_string(pc, ih);
347 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ubsec_intr, sc);
348 if (sc->sc_ih == NULL) {
349 aprint_error_dev(self, "couldn't establish interrupt");
350 if (intrstr != NULL)
351 aprint_error(" at %s", intrstr);
352 aprint_error("\n");
353 return;
354 }
355 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
356
357 sc->sc_cid = crypto_get_driverid(0);
358 if (sc->sc_cid < 0) {
359 aprint_error_dev(self, "couldn't get crypto driver id\n");
360 pci_intr_disestablish(pc, sc->sc_ih);
361 return;
362 }
363
364 sc->sc_rng_need = RND_POOLBITS / NBBY;
365 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
366
367 SIMPLEQ_INIT(&sc->sc_freequeue);
368 dmap = sc->sc_dmaa;
369 for (i = 0; i < UBS_MAX_NQUEUE; i++, dmap++) {
370 struct ubsec_q *q;
371
372 q = (struct ubsec_q *)malloc(sizeof(struct ubsec_q),
373 M_DEVBUF, M_NOWAIT);
374 if (q == NULL) {
375 aprint_error_dev(self, "can't allocate queue buffers\n");
376 break;
377 }
378
379 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_dmachunk),
380 &dmap->d_alloc, 0)) {
381 aprint_error_dev(self, "can't allocate dma buffers\n");
382 free(q, M_DEVBUF);
383 break;
384 }
385 dmap->d_dma = (struct ubsec_dmachunk *)dmap->d_alloc.dma_vaddr;
386
387 q->q_dma = dmap;
388 sc->sc_queuea[i] = q;
389
390 SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
391 }
392
393 crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0,
394 ubsec_newsession, ubsec_freesession, ubsec_process, sc);
395 crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0,
396 ubsec_newsession, ubsec_freesession, ubsec_process, sc);
397 crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC_96, 0, 0,
398 ubsec_newsession, ubsec_freesession, ubsec_process, sc);
399 crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC_96, 0, 0,
400 ubsec_newsession, ubsec_freesession, ubsec_process, sc);
401
402 /*
403 * Reset Broadcom chip
404 */
405 ubsec_reset_board(sc);
406
407 /*
408 * Init Broadcom specific PCI settings
409 */
410 ubsec_init_pciregs(pa);
411
412 /*
413 * Init Broadcom chip
414 */
415 ubsec_init_board(sc);
416
417 #ifndef UBSEC_NO_RNG
418 if (sc->sc_flags & UBS_FLAGS_RNG) {
419 sc->sc_statmask |= BS_STAT_MCR2_DONE;
420
421 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
422 &sc->sc_rng.rng_q.q_mcr, 0))
423 goto skip_rng;
424
425 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_rngbypass),
426 &sc->sc_rng.rng_q.q_ctx, 0)) {
427 ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_mcr);
428 goto skip_rng;
429 }
430
431 if (ubsec_dma_malloc(sc, sizeof(u_int32_t) *
432 UBSEC_RNG_BUFSIZ, &sc->sc_rng.rng_buf, 0)) {
433 ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_ctx);
434 ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_mcr);
435 goto skip_rng;
436 }
437
438 rndsource_setcb(&sc->sc_rnd_source, ubsec_rng_get, sc);
439 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
440 RND_TYPE_RNG,
441 RND_FLAG_NO_ESTIMATE|RND_FLAG_HASCB);
442 if (hz >= 100)
443 sc->sc_rnghz = hz / 100;
444 else
445 sc->sc_rnghz = 1;
446 #ifdef __OpenBSD__
447 timeout_set(&sc->sc_rngto, ubsec_rng, sc);
448 timeout_add(&sc->sc_rngto, sc->sc_rnghz);
449 #else
450 callout_init(&sc->sc_rngto, 0);
451 callout_setfunc(&sc->sc_rngto, ubsec_rng, sc);
452 callout_schedule(&sc->sc_rngto, sc->sc_rnghz);
453 #endif
454 skip_rng:
455 if (sc->sc_rnghz)
456 aprint_normal_dev(self, "random number generator enabled\n");
457 else
458 aprint_error_dev(self, "WARNING: random number generator "
459 "disabled\n");
460 }
461 #endif /* UBSEC_NO_RNG */
462
463 if (sc->sc_flags & UBS_FLAGS_KEY) {
464 sc->sc_statmask |= BS_STAT_MCR2_DONE;
465
466 crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0,
467 ubsec_kprocess, sc);
468 #if 0
469 crypto_kregister(sc->sc_cid, CRK_MOD_EXP_CRT, 0,
470 ubsec_kprocess, sc);
471 #endif
472 }
473 }
474
475 /*
476 * UBSEC Interrupt routine
477 */
478 static int
479 ubsec_intr(void *arg)
480 {
481 struct ubsec_softc *sc = arg;
482 volatile u_int32_t stat;
483 struct ubsec_q *q;
484 struct ubsec_dma *dmap;
485 int npkts = 0, i;
486
487 mutex_spin_enter(&sc->sc_mtx);
488 stat = READ_REG(sc, BS_STAT);
489 stat &= sc->sc_statmask;
490 if (stat == 0) {
491 mutex_spin_exit(&sc->sc_mtx);
492 return (0);
493 }
494
495 WRITE_REG(sc, BS_STAT, stat); /* IACK */
496
497 /*
498 * Check to see if we have any packets waiting for us
499 */
500 if ((stat & BS_STAT_MCR1_DONE)) {
501 while (!SIMPLEQ_EMPTY(&sc->sc_qchip)) {
502 q = SIMPLEQ_FIRST(&sc->sc_qchip);
503 dmap = q->q_dma;
504
505 if ((dmap->d_dma->d_mcr.mcr_flags & htole16(UBS_MCR_DONE)) == 0)
506 break;
507
508 q = SIMPLEQ_FIRST(&sc->sc_qchip);
509 SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip, /*q,*/ q_next);
510
511 npkts = q->q_nstacked_mcrs;
512 sc->sc_nqchip -= 1+npkts;
513 /*
514 * search for further sc_qchip ubsec_q's that share
515 * the same MCR, and complete them too, they must be
516 * at the top.
517 */
518 for (i = 0; i < npkts; i++) {
519 if(q->q_stacked_mcr[i])
520 ubsec_callback(sc, q->q_stacked_mcr[i]);
521 else
522 break;
523 }
524 ubsec_callback(sc, q);
525 }
526
527 /*
528 * Don't send any more packet to chip if there has been
529 * a DMAERR.
530 */
531 if (!(stat & BS_STAT_DMAERR))
532 ubsec_feed(sc);
533 }
534
535 /*
536 * Check to see if we have any key setups/rng's waiting for us
537 */
538 if ((sc->sc_flags & (UBS_FLAGS_KEY|UBS_FLAGS_RNG)) &&
539 (stat & BS_STAT_MCR2_DONE)) {
540 struct ubsec_q2 *q2;
541 struct ubsec_mcr *mcr;
542
543 while (!SIMPLEQ_EMPTY(&sc->sc_qchip2)) {
544 q2 = SIMPLEQ_FIRST(&sc->sc_qchip2);
545
546 bus_dmamap_sync(sc->sc_dmat, q2->q_mcr.dma_map,
547 0, q2->q_mcr.dma_map->dm_mapsize,
548 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
549
550 mcr = (struct ubsec_mcr *)q2->q_mcr.dma_vaddr;
551 if ((mcr->mcr_flags & htole16(UBS_MCR_DONE)) == 0) {
552 bus_dmamap_sync(sc->sc_dmat,
553 q2->q_mcr.dma_map, 0,
554 q2->q_mcr.dma_map->dm_mapsize,
555 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
556 break;
557 }
558 q2 = SIMPLEQ_FIRST(&sc->sc_qchip2);
559 SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip2, /*q2,*/ q_next);
560 ubsec_callback2(sc, q2);
561 /*
562 * Don't send any more packet to chip if there has been
563 * a DMAERR.
564 */
565 if (!(stat & BS_STAT_DMAERR))
566 ubsec_feed2(sc);
567 }
568 }
569
570 /*
571 * Check to see if we got any DMA Error
572 */
573 if (stat & BS_STAT_DMAERR) {
574 #ifdef UBSEC_DEBUG
575 if (ubsec_debug) {
576 volatile u_int32_t a = READ_REG(sc, BS_ERR);
577
578 printf("%s: dmaerr %s@%08x\n", device_xname(sc->sc_dev),
579 (a & BS_ERR_READ) ? "read" : "write",
580 a & BS_ERR_ADDR);
581 }
582 #endif /* UBSEC_DEBUG */
583 ubsecstats.hst_dmaerr++;
584 ubsec_totalreset(sc);
585 ubsec_feed(sc);
586 }
587
588 if (sc->sc_needwakeup) { /* XXX check high watermark */
589 int wkeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
590 #ifdef UBSEC_DEBUG
591 if (ubsec_debug)
592 printf("%s: wakeup crypto (%x)\n", device_xname(sc->sc_dev),
593 sc->sc_needwakeup);
594 #endif /* UBSEC_DEBUG */
595 sc->sc_needwakeup &= ~wkeup;
596 crypto_unblock(sc->sc_cid, wkeup);
597 }
598 mutex_spin_exit(&sc->sc_mtx);
599 return (1);
600 }
601
602 /*
603 * ubsec_feed() - aggregate and post requests to chip
604 * OpenBSD comments:
605 * It is assumed that the caller set splnet()
606 */
607 static void
608 ubsec_feed(struct ubsec_softc *sc)
609 {
610 struct ubsec_q *q, *q2;
611 int npkts, i;
612 void *v;
613 u_int32_t stat;
614 #ifdef UBSEC_DEBUG
615 static int max;
616 #endif /* UBSEC_DEBUG */
617
618 npkts = sc->sc_nqueue;
619 if (npkts > ubsecstats.hst_maxqueue)
620 ubsecstats.hst_maxqueue = npkts;
621 if (npkts < 2)
622 goto feed1;
623
624 /*
625 * Decide how many ops to combine in a single MCR. We cannot
626 * aggregate more than UBS_MAX_AGGR because this is the number
627 * of slots defined in the data structure. Otherwise we clamp
628 * based on the tunable parameter ubsec_maxaggr. Note that
629 * aggregation can happen in two ways: either by batching ops
630 * from above or because the h/w backs up and throttles us.
631 * Aggregating ops reduces the number of interrupts to the host
632 * but also (potentially) increases the latency for processing
633 * completed ops as we only get an interrupt when all aggregated
634 * ops have completed.
635 */
636 if (npkts > UBS_MAX_AGGR)
637 npkts = UBS_MAX_AGGR;
638 if (npkts > ubsec_maxaggr)
639 npkts = ubsec_maxaggr;
640 if (npkts > ubsecstats.hst_maxbatch)
641 ubsecstats.hst_maxbatch = npkts;
642 if (npkts < 2)
643 goto feed1;
644 ubsecstats.hst_totbatch += npkts-1;
645
646 if ((stat = READ_REG(sc, BS_STAT)) & (BS_STAT_MCR1_FULL | BS_STAT_DMAERR)) {
647 if (stat & BS_STAT_DMAERR) {
648 ubsec_totalreset(sc);
649 ubsecstats.hst_dmaerr++;
650 } else {
651 ubsecstats.hst_mcr1full++;
652 }
653 return;
654 }
655
656 #ifdef UBSEC_DEBUG
657 if (ubsec_debug)
658 printf("merging %d records\n", npkts);
659 /* XXX temporary aggregation statistics reporting code */
660 if (max < npkts) {
661 max = npkts;
662 printf("%s: new max aggregate %d\n", device_xname(sc->sc_dev), max);
663 }
664 #endif /* UBSEC_DEBUG */
665
666 q = SIMPLEQ_FIRST(&sc->sc_queue);
667 SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, /*q,*/ q_next);
668 --sc->sc_nqueue;
669
670 bus_dmamap_sync(sc->sc_dmat, q->q_src_map,
671 0, q->q_src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
672 if (q->q_dst_map != NULL)
673 bus_dmamap_sync(sc->sc_dmat, q->q_dst_map,
674 0, q->q_dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
675
676 q->q_nstacked_mcrs = npkts - 1; /* Number of packets stacked */
677
678 for (i = 0; i < q->q_nstacked_mcrs; i++) {
679 q2 = SIMPLEQ_FIRST(&sc->sc_queue);
680 bus_dmamap_sync(sc->sc_dmat, q2->q_src_map,
681 0, q2->q_src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
682 if (q2->q_dst_map != NULL)
683 bus_dmamap_sync(sc->sc_dmat, q2->q_dst_map,
684 0, q2->q_dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
685 q2= SIMPLEQ_FIRST(&sc->sc_queue);
686 SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, /*q2,*/ q_next);
687 --sc->sc_nqueue;
688
689 v = ((void *)&q2->q_dma->d_dma->d_mcr);
690 v = (char*)v + (sizeof(struct ubsec_mcr) -
691 sizeof(struct ubsec_mcr_add));
692 memcpy(&q->q_dma->d_dma->d_mcradd[i], v, sizeof(struct ubsec_mcr_add));
693 q->q_stacked_mcr[i] = q2;
694 }
695 q->q_dma->d_dma->d_mcr.mcr_pkts = htole16(npkts);
696 SIMPLEQ_INSERT_TAIL(&sc->sc_qchip, q, q_next);
697 sc->sc_nqchip += npkts;
698 if (sc->sc_nqchip > ubsecstats.hst_maxqchip)
699 ubsecstats.hst_maxqchip = sc->sc_nqchip;
700 bus_dmamap_sync(sc->sc_dmat, q->q_dma->d_alloc.dma_map,
701 0, q->q_dma->d_alloc.dma_map->dm_mapsize,
702 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
703 WRITE_REG(sc, BS_MCR1, q->q_dma->d_alloc.dma_paddr +
704 offsetof(struct ubsec_dmachunk, d_mcr));
705 return;
706
707 feed1:
708 while (!SIMPLEQ_EMPTY(&sc->sc_queue)) {
709 if ((stat = READ_REG(sc, BS_STAT)) & (BS_STAT_MCR1_FULL | BS_STAT_DMAERR)) {
710 if (stat & BS_STAT_DMAERR) {
711 ubsec_totalreset(sc);
712 ubsecstats.hst_dmaerr++;
713 } else {
714 ubsecstats.hst_mcr1full++;
715 }
716 break;
717 }
718
719 q = SIMPLEQ_FIRST(&sc->sc_queue);
720
721 bus_dmamap_sync(sc->sc_dmat, q->q_src_map,
722 0, q->q_src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
723 if (q->q_dst_map != NULL)
724 bus_dmamap_sync(sc->sc_dmat, q->q_dst_map,
725 0, q->q_dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
726 bus_dmamap_sync(sc->sc_dmat, q->q_dma->d_alloc.dma_map,
727 0, q->q_dma->d_alloc.dma_map->dm_mapsize,
728 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
729
730 WRITE_REG(sc, BS_MCR1, q->q_dma->d_alloc.dma_paddr +
731 offsetof(struct ubsec_dmachunk, d_mcr));
732 #ifdef UBSEC_DEBUG
733 if (ubsec_debug)
734 printf("feed: q->chip %p %08x stat %08x\n",
735 q, (u_int32_t)q->q_dma->d_alloc.dma_paddr,
736 stat);
737 #endif /* UBSEC_DEBUG */
738 q = SIMPLEQ_FIRST(&sc->sc_queue);
739 SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, /*q,*/ q_next);
740 --sc->sc_nqueue;
741 SIMPLEQ_INSERT_TAIL(&sc->sc_qchip, q, q_next);
742 sc->sc_nqchip++;
743 }
744 if (sc->sc_nqchip > ubsecstats.hst_maxqchip)
745 ubsecstats.hst_maxqchip = sc->sc_nqchip;
746 }
747
748 /*
749 * Allocate a new 'session' and return an encoded session id. 'sidp'
750 * contains our registration id, and should contain an encoded session
751 * id on successful allocation.
752 */
753 static int
754 ubsec_newsession(void *arg, u_int32_t *sidp, struct cryptoini *cri)
755 {
756 struct cryptoini *c, *encini = NULL, *macini = NULL;
757 struct ubsec_softc *sc;
758 struct ubsec_session *ses = NULL;
759 MD5_CTX md5ctx;
760 SHA1_CTX sha1ctx;
761 int i, sesn;
762
763 sc = arg;
764 KASSERT(sc != NULL /*, ("ubsec_newsession: null softc")*/);
765
766 if (sidp == NULL || cri == NULL || sc == NULL)
767 return (EINVAL);
768
769 for (c = cri; c != NULL; c = c->cri_next) {
770 if (c->cri_alg == CRYPTO_MD5_HMAC_96 ||
771 c->cri_alg == CRYPTO_SHA1_HMAC_96) {
772 if (macini)
773 return (EINVAL);
774 macini = c;
775 } else if (c->cri_alg == CRYPTO_DES_CBC ||
776 c->cri_alg == CRYPTO_3DES_CBC) {
777 if (encini)
778 return (EINVAL);
779 encini = c;
780 } else
781 return (EINVAL);
782 }
783 if (encini == NULL && macini == NULL)
784 return (EINVAL);
785
786 if (sc->sc_sessions == NULL) {
787 ses = sc->sc_sessions = (struct ubsec_session *)malloc(
788 sizeof(struct ubsec_session), M_DEVBUF, M_NOWAIT);
789 if (ses == NULL)
790 return (ENOMEM);
791 sesn = 0;
792 sc->sc_nsessions = 1;
793 } else {
794 for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
795 if (sc->sc_sessions[sesn].ses_used == 0) {
796 ses = &sc->sc_sessions[sesn];
797 break;
798 }
799 }
800
801 if (ses == NULL) {
802 sesn = sc->sc_nsessions;
803 ses = (struct ubsec_session *)malloc((sesn + 1) *
804 sizeof(struct ubsec_session), M_DEVBUF, M_NOWAIT);
805 if (ses == NULL)
806 return (ENOMEM);
807 memcpy(ses, sc->sc_sessions, sesn *
808 sizeof(struct ubsec_session));
809 memset(sc->sc_sessions, 0, sesn *
810 sizeof(struct ubsec_session));
811 free(sc->sc_sessions, M_DEVBUF);
812 sc->sc_sessions = ses;
813 ses = &sc->sc_sessions[sesn];
814 sc->sc_nsessions++;
815 }
816 }
817
818 memset(ses, 0, sizeof(struct ubsec_session));
819 ses->ses_used = 1;
820 if (encini) {
821 /* get an IV, network byte order */
822 #ifdef __NetBSD__
823 cprng_fast(ses->ses_iv, sizeof(ses->ses_iv));
824 #else
825 get_random_bytes(ses->ses_iv, sizeof(ses->ses_iv));
826 #endif
827
828 /* Go ahead and compute key in ubsec's byte order */
829 if (encini->cri_alg == CRYPTO_DES_CBC) {
830 memcpy(&ses->ses_deskey[0], encini->cri_key, 8);
831 memcpy(&ses->ses_deskey[2], encini->cri_key, 8);
832 memcpy(&ses->ses_deskey[4], encini->cri_key, 8);
833 } else
834 memcpy(ses->ses_deskey, encini->cri_key, 24);
835
836 SWAP32(ses->ses_deskey[0]);
837 SWAP32(ses->ses_deskey[1]);
838 SWAP32(ses->ses_deskey[2]);
839 SWAP32(ses->ses_deskey[3]);
840 SWAP32(ses->ses_deskey[4]);
841 SWAP32(ses->ses_deskey[5]);
842 }
843
844 if (macini) {
845 for (i = 0; i < macini->cri_klen / 8; i++)
846 macini->cri_key[i] ^= HMAC_IPAD_VAL;
847
848 if (macini->cri_alg == CRYPTO_MD5_HMAC_96) {
849 MD5Init(&md5ctx);
850 MD5Update(&md5ctx, macini->cri_key,
851 macini->cri_klen / 8);
852 MD5Update(&md5ctx, hmac_ipad_buffer,
853 HMAC_BLOCK_LEN - (macini->cri_klen / 8));
854 memcpy(ses->ses_hminner, md5ctx.state,
855 sizeof(md5ctx.state));
856 } else {
857 SHA1Init(&sha1ctx);
858 SHA1Update(&sha1ctx, macini->cri_key,
859 macini->cri_klen / 8);
860 SHA1Update(&sha1ctx, hmac_ipad_buffer,
861 HMAC_BLOCK_LEN - (macini->cri_klen / 8));
862 memcpy(ses->ses_hminner, sha1ctx.state,
863 sizeof(sha1ctx.state));
864 }
865
866 for (i = 0; i < macini->cri_klen / 8; i++)
867 macini->cri_key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
868
869 if (macini->cri_alg == CRYPTO_MD5_HMAC_96) {
870 MD5Init(&md5ctx);
871 MD5Update(&md5ctx, macini->cri_key,
872 macini->cri_klen / 8);
873 MD5Update(&md5ctx, hmac_opad_buffer,
874 HMAC_BLOCK_LEN - (macini->cri_klen / 8));
875 memcpy(ses->ses_hmouter, md5ctx.state,
876 sizeof(md5ctx.state));
877 } else {
878 SHA1Init(&sha1ctx);
879 SHA1Update(&sha1ctx, macini->cri_key,
880 macini->cri_klen / 8);
881 SHA1Update(&sha1ctx, hmac_opad_buffer,
882 HMAC_BLOCK_LEN - (macini->cri_klen / 8));
883 memcpy(ses->ses_hmouter, sha1ctx.state,
884 sizeof(sha1ctx.state));
885 }
886
887 for (i = 0; i < macini->cri_klen / 8; i++)
888 macini->cri_key[i] ^= HMAC_OPAD_VAL;
889 }
890
891 *sidp = UBSEC_SID(device_unit(sc->sc_dev), sesn);
892 return (0);
893 }
894
895 /*
896 * Deallocate a session.
897 */
898 static int
899 ubsec_freesession(void *arg, u_int64_t tid)
900 {
901 struct ubsec_softc *sc;
902 int session;
903 u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
904
905 sc = arg;
906 KASSERT(sc != NULL /*, ("ubsec_freesession: null softc")*/);
907
908 session = UBSEC_SESSION(sid);
909 if (session >= sc->sc_nsessions)
910 return (EINVAL);
911
912 memset(&sc->sc_sessions[session], 0, sizeof(sc->sc_sessions[session]));
913 return (0);
914 }
915
916 #ifdef __FreeBSD__ /* Ugly gratuitous changes to bus_dma */
917 static void
918 ubsec_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, bus_size_t mapsize, int error)
919 {
920 struct ubsec_operand *op = arg;
921
922 KASSERT(nsegs <= UBS_MAX_SCATTER
923 /*, ("Too many DMA segments returned when mapping operand")*/);
924 #ifdef UBSEC_DEBUG
925 if (ubsec_debug)
926 printf("ubsec_op_cb: mapsize %u nsegs %d\n",
927 (u_int) mapsize, nsegs);
928 #endif
929 op->mapsize = mapsize;
930 op->nsegs = nsegs;
931 memcpy(op->segs, seg, nsegs * sizeof (seg[0]));
932 }
933 #endif
934
935 static int
936 ubsec_process(void *arg, struct cryptop *crp, int hint)
937 {
938 struct ubsec_q *q = NULL;
939 #ifdef __OpenBSD__
940 int card;
941 #endif
942 int err = 0, i, j, nicealign;
943 struct ubsec_softc *sc;
944 struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
945 int encoffset = 0, macoffset = 0, cpskip, cpoffset;
946 int sskip, dskip, stheend, dtheend;
947 int16_t coffset;
948 struct ubsec_session *ses;
949 struct ubsec_pktctx ctx;
950 struct ubsec_dma *dmap = NULL;
951
952 sc = arg;
953 KASSERT(sc != NULL /*, ("ubsec_process: null softc")*/);
954
955 if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
956 ubsecstats.hst_invalid++;
957 return (EINVAL);
958 }
959 if (UBSEC_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
960 ubsecstats.hst_badsession++;
961 return (EINVAL);
962 }
963
964 mutex_spin_enter(&sc->sc_mtx);
965
966 if (SIMPLEQ_EMPTY(&sc->sc_freequeue)) {
967 ubsecstats.hst_queuefull++;
968 sc->sc_needwakeup |= CRYPTO_SYMQ;
969 mutex_spin_exit(&sc->sc_mtx);
970 return(ERESTART);
971 }
972
973 q = SIMPLEQ_FIRST(&sc->sc_freequeue);
974 SIMPLEQ_REMOVE_HEAD(&sc->sc_freequeue, /*q,*/ q_next);
975 mutex_spin_exit(&sc->sc_mtx);
976
977 dmap = q->q_dma; /* Save dma pointer */
978 memset(q, 0, sizeof(struct ubsec_q));
979 memset(&ctx, 0, sizeof(ctx));
980
981 q->q_sesn = UBSEC_SESSION(crp->crp_sid);
982 q->q_dma = dmap;
983 ses = &sc->sc_sessions[q->q_sesn];
984
985 if (crp->crp_flags & CRYPTO_F_IMBUF) {
986 q->q_src_m = (struct mbuf *)crp->crp_buf;
987 q->q_dst_m = (struct mbuf *)crp->crp_buf;
988 } else if (crp->crp_flags & CRYPTO_F_IOV) {
989 q->q_src_io = (struct uio *)crp->crp_buf;
990 q->q_dst_io = (struct uio *)crp->crp_buf;
991 } else {
992 ubsecstats.hst_badflags++;
993 err = EINVAL;
994 goto errout; /* XXX we don't handle contiguous blocks! */
995 }
996
997 memset(&dmap->d_dma->d_mcr, 0, sizeof(struct ubsec_mcr));
998
999 dmap->d_dma->d_mcr.mcr_pkts = htole16(1);
1000 dmap->d_dma->d_mcr.mcr_flags = 0;
1001 q->q_crp = crp;
1002
1003 crd1 = crp->crp_desc;
1004 if (crd1 == NULL) {
1005 ubsecstats.hst_nodesc++;
1006 err = EINVAL;
1007 goto errout;
1008 }
1009 crd2 = crd1->crd_next;
1010
1011 if (crd2 == NULL) {
1012 if (crd1->crd_alg == CRYPTO_MD5_HMAC_96 ||
1013 crd1->crd_alg == CRYPTO_SHA1_HMAC_96) {
1014 maccrd = crd1;
1015 enccrd = NULL;
1016 } else if (crd1->crd_alg == CRYPTO_DES_CBC ||
1017 crd1->crd_alg == CRYPTO_3DES_CBC) {
1018 maccrd = NULL;
1019 enccrd = crd1;
1020 } else {
1021 ubsecstats.hst_badalg++;
1022 err = EINVAL;
1023 goto errout;
1024 }
1025 } else {
1026 if ((crd1->crd_alg == CRYPTO_MD5_HMAC_96 ||
1027 crd1->crd_alg == CRYPTO_SHA1_HMAC_96) &&
1028 (crd2->crd_alg == CRYPTO_DES_CBC ||
1029 crd2->crd_alg == CRYPTO_3DES_CBC) &&
1030 ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
1031 maccrd = crd1;
1032 enccrd = crd2;
1033 } else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
1034 crd1->crd_alg == CRYPTO_3DES_CBC) &&
1035 (crd2->crd_alg == CRYPTO_MD5_HMAC_96 ||
1036 crd2->crd_alg == CRYPTO_SHA1_HMAC_96) &&
1037 (crd1->crd_flags & CRD_F_ENCRYPT)) {
1038 enccrd = crd1;
1039 maccrd = crd2;
1040 } else {
1041 /*
1042 * We cannot order the ubsec as requested
1043 */
1044 ubsecstats.hst_badalg++;
1045 err = EINVAL;
1046 goto errout;
1047 }
1048 }
1049
1050 if (enccrd) {
1051 encoffset = enccrd->crd_skip;
1052 ctx.pc_flags |= htole16(UBS_PKTCTX_ENC_3DES);
1053
1054 if (enccrd->crd_flags & CRD_F_ENCRYPT) {
1055 q->q_flags |= UBSEC_QFLAGS_COPYOUTIV;
1056
1057 if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
1058 memcpy(ctx.pc_iv, enccrd->crd_iv, 8);
1059 else {
1060 ctx.pc_iv[0] = ses->ses_iv[0];
1061 ctx.pc_iv[1] = ses->ses_iv[1];
1062 }
1063
1064 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
1065 if (crp->crp_flags & CRYPTO_F_IMBUF)
1066 m_copyback(q->q_src_m,
1067 enccrd->crd_inject,
1068 8, (void *)ctx.pc_iv);
1069 else if (crp->crp_flags & CRYPTO_F_IOV)
1070 cuio_copyback(q->q_src_io,
1071 enccrd->crd_inject,
1072 8, (void *)ctx.pc_iv);
1073 }
1074 } else {
1075 ctx.pc_flags |= htole16(UBS_PKTCTX_INBOUND);
1076
1077 if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
1078 memcpy(ctx.pc_iv, enccrd->crd_iv, 8);
1079 else if (crp->crp_flags & CRYPTO_F_IMBUF)
1080 m_copydata(q->q_src_m, enccrd->crd_inject,
1081 8, (void *)ctx.pc_iv);
1082 else if (crp->crp_flags & CRYPTO_F_IOV)
1083 cuio_copydata(q->q_src_io,
1084 enccrd->crd_inject, 8,
1085 (void *)ctx.pc_iv);
1086 }
1087
1088 ctx.pc_deskey[0] = ses->ses_deskey[0];
1089 ctx.pc_deskey[1] = ses->ses_deskey[1];
1090 ctx.pc_deskey[2] = ses->ses_deskey[2];
1091 ctx.pc_deskey[3] = ses->ses_deskey[3];
1092 ctx.pc_deskey[4] = ses->ses_deskey[4];
1093 ctx.pc_deskey[5] = ses->ses_deskey[5];
1094 SWAP32(ctx.pc_iv[0]);
1095 SWAP32(ctx.pc_iv[1]);
1096 }
1097
1098 if (maccrd) {
1099 macoffset = maccrd->crd_skip;
1100
1101 if (maccrd->crd_alg == CRYPTO_MD5_HMAC_96)
1102 ctx.pc_flags |= htole16(UBS_PKTCTX_AUTH_MD5);
1103 else
1104 ctx.pc_flags |= htole16(UBS_PKTCTX_AUTH_SHA1);
1105
1106 for (i = 0; i < 5; i++) {
1107 ctx.pc_hminner[i] = ses->ses_hminner[i];
1108 ctx.pc_hmouter[i] = ses->ses_hmouter[i];
1109
1110 HTOLE32(ctx.pc_hminner[i]);
1111 HTOLE32(ctx.pc_hmouter[i]);
1112 }
1113 }
1114
1115 if (enccrd && maccrd) {
1116 /*
1117 * ubsec cannot handle packets where the end of encryption
1118 * and authentication are not the same, or where the
1119 * encrypted part begins before the authenticated part.
1120 */
1121 if ((encoffset + enccrd->crd_len) !=
1122 (macoffset + maccrd->crd_len)) {
1123 ubsecstats.hst_lenmismatch++;
1124 err = EINVAL;
1125 goto errout;
1126 }
1127 if (enccrd->crd_skip < maccrd->crd_skip) {
1128 ubsecstats.hst_skipmismatch++;
1129 err = EINVAL;
1130 goto errout;
1131 }
1132 sskip = maccrd->crd_skip;
1133 cpskip = dskip = enccrd->crd_skip;
1134 stheend = maccrd->crd_len;
1135 dtheend = enccrd->crd_len;
1136 coffset = enccrd->crd_skip - maccrd->crd_skip;
1137 cpoffset = cpskip + dtheend;
1138 #ifdef UBSEC_DEBUG
1139 if (ubsec_debug) {
1140 printf("mac: skip %d, len %d, inject %d\n",
1141 maccrd->crd_skip, maccrd->crd_len, maccrd->crd_inject);
1142 printf("enc: skip %d, len %d, inject %d\n",
1143 enccrd->crd_skip, enccrd->crd_len, enccrd->crd_inject);
1144 printf("src: skip %d, len %d\n", sskip, stheend);
1145 printf("dst: skip %d, len %d\n", dskip, dtheend);
1146 printf("ubs: coffset %d, pktlen %d, cpskip %d, cpoffset %d\n",
1147 coffset, stheend, cpskip, cpoffset);
1148 }
1149 #endif
1150 } else {
1151 cpskip = dskip = sskip = macoffset + encoffset;
1152 dtheend = stheend = (enccrd)?enccrd->crd_len:maccrd->crd_len;
1153 cpoffset = cpskip + dtheend;
1154 coffset = 0;
1155 }
1156 ctx.pc_offset = htole16(coffset >> 2);
1157
1158 /* XXX FIXME: jonathan asks, what the heck's that 0xfff0? */
1159 if (bus_dmamap_create(sc->sc_dmat, 0xfff0, UBS_MAX_SCATTER,
1160 0xfff0, 0, BUS_DMA_NOWAIT, &q->q_src_map) != 0) {
1161 err = ENOMEM;
1162 goto errout;
1163 }
1164 if (crp->crp_flags & CRYPTO_F_IMBUF) {
1165 if (bus_dmamap_load_mbuf(sc->sc_dmat, q->q_src_map,
1166 q->q_src_m, BUS_DMA_NOWAIT) != 0) {
1167 bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
1168 q->q_src_map = NULL;
1169 ubsecstats.hst_noload++;
1170 err = ENOMEM;
1171 goto errout;
1172 }
1173 } else if (crp->crp_flags & CRYPTO_F_IOV) {
1174 if (bus_dmamap_load_uio(sc->sc_dmat, q->q_src_map,
1175 q->q_src_io, BUS_DMA_NOWAIT) != 0) {
1176 bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
1177 q->q_src_map = NULL;
1178 ubsecstats.hst_noload++;
1179 err = ENOMEM;
1180 goto errout;
1181 }
1182 }
1183 nicealign = ubsec_dmamap_aligned(q->q_src_map);
1184
1185 dmap->d_dma->d_mcr.mcr_pktlen = htole16(stheend);
1186
1187 #ifdef UBSEC_DEBUG
1188 if (ubsec_debug)
1189 printf("src skip: %d nicealign: %u\n", sskip, nicealign);
1190 #endif
1191 for (i = j = 0; i < q->q_src_map->dm_nsegs; i++) {
1192 struct ubsec_pktbuf *pb;
1193 bus_size_t packl = q->q_src_map->dm_segs[i].ds_len;
1194 bus_addr_t packp = q->q_src_map->dm_segs[i].ds_addr;
1195
1196 if (sskip >= packl) {
1197 sskip -= packl;
1198 continue;
1199 }
1200
1201 packl -= sskip;
1202 packp += sskip;
1203 sskip = 0;
1204
1205 if (packl > 0xfffc) {
1206 err = EIO;
1207 goto errout;
1208 }
1209
1210 if (j == 0)
1211 pb = &dmap->d_dma->d_mcr.mcr_ipktbuf;
1212 else
1213 pb = &dmap->d_dma->d_sbuf[j - 1];
1214
1215 pb->pb_addr = htole32(packp);
1216
1217 if (stheend) {
1218 if (packl > stheend) {
1219 pb->pb_len = htole32(stheend);
1220 stheend = 0;
1221 } else {
1222 pb->pb_len = htole32(packl);
1223 stheend -= packl;
1224 }
1225 } else
1226 pb->pb_len = htole32(packl);
1227
1228 if ((i + 1) == q->q_src_map->dm_nsegs)
1229 pb->pb_next = 0;
1230 else
1231 pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
1232 offsetof(struct ubsec_dmachunk, d_sbuf[j]));
1233 j++;
1234 }
1235
1236 if (enccrd == NULL && maccrd != NULL) {
1237 dmap->d_dma->d_mcr.mcr_opktbuf.pb_addr = 0;
1238 dmap->d_dma->d_mcr.mcr_opktbuf.pb_len = 0;
1239 dmap->d_dma->d_mcr.mcr_opktbuf.pb_next = htole32(dmap->d_alloc.dma_paddr +
1240 offsetof(struct ubsec_dmachunk, d_macbuf[0]));
1241 #ifdef UBSEC_DEBUG
1242 if (ubsec_debug)
1243 printf("opkt: %x %x %x\n",
1244 dmap->d_dma->d_mcr.mcr_opktbuf.pb_addr,
1245 dmap->d_dma->d_mcr.mcr_opktbuf.pb_len,
1246 dmap->d_dma->d_mcr.mcr_opktbuf.pb_next);
1247
1248 #endif
1249 } else {
1250 if (crp->crp_flags & CRYPTO_F_IOV) {
1251 if (!nicealign) {
1252 ubsecstats.hst_iovmisaligned++;
1253 err = EINVAL;
1254 goto errout;
1255 }
1256 /* XXX: ``what the heck's that'' 0xfff0? */
1257 if (bus_dmamap_create(sc->sc_dmat, 0xfff0,
1258 UBS_MAX_SCATTER, 0xfff0, 0, BUS_DMA_NOWAIT,
1259 &q->q_dst_map) != 0) {
1260 ubsecstats.hst_nomap++;
1261 err = ENOMEM;
1262 goto errout;
1263 }
1264 if (bus_dmamap_load_uio(sc->sc_dmat, q->q_dst_map,
1265 q->q_dst_io, BUS_DMA_NOWAIT) != 0) {
1266 bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
1267 q->q_dst_map = NULL;
1268 ubsecstats.hst_noload++;
1269 err = ENOMEM;
1270 goto errout;
1271 }
1272 } else if (crp->crp_flags & CRYPTO_F_IMBUF) {
1273 if (nicealign) {
1274 q->q_dst_m = q->q_src_m;
1275 q->q_dst_map = q->q_src_map;
1276 } else {
1277 int totlen, len;
1278 struct mbuf *m, *top, **mp;
1279
1280 ubsecstats.hst_unaligned++;
1281 totlen = q->q_src_map->dm_mapsize;
1282 if (q->q_src_m->m_flags & M_PKTHDR) {
1283 len = MHLEN;
1284 MGETHDR(m, M_DONTWAIT, MT_DATA);
1285 /*XXX FIXME: m_dup_pkthdr */
1286 if (m && 1 /*!m_dup_pkthdr(m, q->q_src_m, M_DONTWAIT)*/) {
1287 m_free(m);
1288 m = NULL;
1289 }
1290 } else {
1291 len = MLEN;
1292 MGET(m, M_DONTWAIT, MT_DATA);
1293 }
1294 if (m == NULL) {
1295 ubsecstats.hst_nombuf++;
1296 err = sc->sc_nqueue ? ERESTART : ENOMEM;
1297 goto errout;
1298 }
1299 if (len == MHLEN)
1300 /*XXX was M_DUP_PKTHDR*/
1301 M_COPY_PKTHDR(m, q->q_src_m);
1302 if (totlen >= MINCLSIZE) {
1303 MCLGET(m, M_DONTWAIT);
1304 if ((m->m_flags & M_EXT) == 0) {
1305 m_free(m);
1306 ubsecstats.hst_nomcl++;
1307 err = sc->sc_nqueue ? ERESTART : ENOMEM;
1308 goto errout;
1309 }
1310 len = MCLBYTES;
1311 }
1312 m->m_len = len;
1313 top = NULL;
1314 mp = ⊤
1315
1316 while (totlen > 0) {
1317 if (top) {
1318 MGET(m, M_DONTWAIT, MT_DATA);
1319 if (m == NULL) {
1320 m_freem(top);
1321 ubsecstats.hst_nombuf++;
1322 err = sc->sc_nqueue ? ERESTART : ENOMEM;
1323 goto errout;
1324 }
1325 len = MLEN;
1326 }
1327 if (top && totlen >= MINCLSIZE) {
1328 MCLGET(m, M_DONTWAIT);
1329 if ((m->m_flags & M_EXT) == 0) {
1330 *mp = m;
1331 m_freem(top);
1332 ubsecstats.hst_nomcl++;
1333 err = sc->sc_nqueue ? ERESTART : ENOMEM;
1334 goto errout;
1335 }
1336 len = MCLBYTES;
1337 }
1338 m->m_len = len = min(totlen, len);
1339 totlen -= len;
1340 *mp = m;
1341 mp = &m->m_next;
1342 }
1343 q->q_dst_m = top;
1344 ubsec_mcopy(q->q_src_m, q->q_dst_m,
1345 cpskip, cpoffset);
1346 /* XXX again, what the heck is that 0xfff0? */
1347 if (bus_dmamap_create(sc->sc_dmat, 0xfff0,
1348 UBS_MAX_SCATTER, 0xfff0, 0, BUS_DMA_NOWAIT,
1349 &q->q_dst_map) != 0) {
1350 ubsecstats.hst_nomap++;
1351 err = ENOMEM;
1352 goto errout;
1353 }
1354 if (bus_dmamap_load_mbuf(sc->sc_dmat,
1355 q->q_dst_map, q->q_dst_m,
1356 BUS_DMA_NOWAIT) != 0) {
1357 bus_dmamap_destroy(sc->sc_dmat,
1358 q->q_dst_map);
1359 q->q_dst_map = NULL;
1360 ubsecstats.hst_noload++;
1361 err = ENOMEM;
1362 goto errout;
1363 }
1364 }
1365 } else {
1366 ubsecstats.hst_badflags++;
1367 err = EINVAL;
1368 goto errout;
1369 }
1370
1371 #ifdef UBSEC_DEBUG
1372 if (ubsec_debug)
1373 printf("dst skip: %d\n", dskip);
1374 #endif
1375 for (i = j = 0; i < q->q_dst_map->dm_nsegs; i++) {
1376 struct ubsec_pktbuf *pb;
1377 bus_size_t packl = q->q_dst_map->dm_segs[i].ds_len;
1378 bus_addr_t packp = q->q_dst_map->dm_segs[i].ds_addr;
1379
1380 if (dskip >= packl) {
1381 dskip -= packl;
1382 continue;
1383 }
1384
1385 packl -= dskip;
1386 packp += dskip;
1387 dskip = 0;
1388
1389 if (packl > 0xfffc) {
1390 err = EIO;
1391 goto errout;
1392 }
1393
1394 if (j == 0)
1395 pb = &dmap->d_dma->d_mcr.mcr_opktbuf;
1396 else
1397 pb = &dmap->d_dma->d_dbuf[j - 1];
1398
1399 pb->pb_addr = htole32(packp);
1400
1401 if (dtheend) {
1402 if (packl > dtheend) {
1403 pb->pb_len = htole32(dtheend);
1404 dtheend = 0;
1405 } else {
1406 pb->pb_len = htole32(packl);
1407 dtheend -= packl;
1408 }
1409 } else
1410 pb->pb_len = htole32(packl);
1411
1412 if ((i + 1) == q->q_dst_map->dm_nsegs) {
1413 if (maccrd)
1414 pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
1415 offsetof(struct ubsec_dmachunk, d_macbuf[0]));
1416 else
1417 pb->pb_next = 0;
1418 } else
1419 pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
1420 offsetof(struct ubsec_dmachunk, d_dbuf[j]));
1421 j++;
1422 }
1423 }
1424
1425 dmap->d_dma->d_mcr.mcr_cmdctxp = htole32(dmap->d_alloc.dma_paddr +
1426 offsetof(struct ubsec_dmachunk, d_ctx));
1427
1428 if (sc->sc_flags & UBS_FLAGS_LONGCTX) {
1429 struct ubsec_pktctx_long *ctxl;
1430
1431 ctxl = (struct ubsec_pktctx_long *)((char *)dmap->d_alloc.dma_vaddr +
1432 offsetof(struct ubsec_dmachunk, d_ctx));
1433
1434 /* transform small context into long context */
1435 ctxl->pc_len = htole16(sizeof(struct ubsec_pktctx_long));
1436 ctxl->pc_type = htole16(UBS_PKTCTX_TYPE_IPSEC);
1437 ctxl->pc_flags = ctx.pc_flags;
1438 ctxl->pc_offset = ctx.pc_offset;
1439 for (i = 0; i < 6; i++)
1440 ctxl->pc_deskey[i] = ctx.pc_deskey[i];
1441 for (i = 0; i < 5; i++)
1442 ctxl->pc_hminner[i] = ctx.pc_hminner[i];
1443 for (i = 0; i < 5; i++)
1444 ctxl->pc_hmouter[i] = ctx.pc_hmouter[i];
1445 ctxl->pc_iv[0] = ctx.pc_iv[0];
1446 ctxl->pc_iv[1] = ctx.pc_iv[1];
1447 } else
1448 memcpy((char *)dmap->d_alloc.dma_vaddr +
1449 offsetof(struct ubsec_dmachunk, d_ctx), &ctx,
1450 sizeof(struct ubsec_pktctx));
1451
1452 mutex_spin_enter(&sc->sc_mtx);
1453 SIMPLEQ_INSERT_TAIL(&sc->sc_queue, q, q_next);
1454 sc->sc_nqueue++;
1455 ubsecstats.hst_ipackets++;
1456 ubsecstats.hst_ibytes += dmap->d_alloc.dma_map->dm_mapsize;
1457 if ((hint & CRYPTO_HINT_MORE) == 0 || sc->sc_nqueue >= ubsec_maxbatch)
1458 ubsec_feed(sc);
1459 mutex_spin_exit(&sc->sc_mtx);
1460 return (0);
1461
1462 errout:
1463 if (q != NULL) {
1464 if ((q->q_dst_m != NULL) && (q->q_src_m != q->q_dst_m))
1465 m_freem(q->q_dst_m);
1466
1467 if (q->q_dst_map != NULL && q->q_dst_map != q->q_src_map) {
1468 bus_dmamap_unload(sc->sc_dmat, q->q_dst_map);
1469 bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
1470 }
1471 if (q->q_src_map != NULL) {
1472 bus_dmamap_unload(sc->sc_dmat, q->q_src_map);
1473 bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
1474 }
1475
1476 mutex_spin_enter(&sc->sc_mtx);
1477 SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
1478 mutex_spin_exit(&sc->sc_mtx);
1479 }
1480 #if 0 /* jonathan says: this openbsd code seems to be subsumed elsewhere */
1481 if (err == EINVAL)
1482 ubsecstats.hst_invalid++;
1483 else
1484 ubsecstats.hst_nomem++;
1485 #endif
1486 if (err != ERESTART) {
1487 crp->crp_etype = err;
1488 crypto_done(crp);
1489 } else {
1490 sc->sc_needwakeup |= CRYPTO_SYMQ;
1491 }
1492 return (err);
1493 }
1494
1495 static void
1496 ubsec_callback(struct ubsec_softc *sc, struct ubsec_q *q)
1497 {
1498 struct cryptop *crp = (struct cryptop *)q->q_crp;
1499 struct cryptodesc *crd;
1500 struct ubsec_dma *dmap = q->q_dma;
1501
1502 ubsecstats.hst_opackets++;
1503 ubsecstats.hst_obytes += dmap->d_alloc.dma_size;
1504
1505 bus_dmamap_sync(sc->sc_dmat, dmap->d_alloc.dma_map, 0,
1506 dmap->d_alloc.dma_map->dm_mapsize,
1507 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1508 if (q->q_dst_map != NULL && q->q_dst_map != q->q_src_map) {
1509 bus_dmamap_sync(sc->sc_dmat, q->q_dst_map,
1510 0, q->q_dst_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1511 bus_dmamap_unload(sc->sc_dmat, q->q_dst_map);
1512 bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
1513 }
1514 bus_dmamap_sync(sc->sc_dmat, q->q_src_map,
1515 0, q->q_src_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1516 bus_dmamap_unload(sc->sc_dmat, q->q_src_map);
1517 bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
1518
1519 if ((crp->crp_flags & CRYPTO_F_IMBUF) && (q->q_src_m != q->q_dst_m)) {
1520 m_freem(q->q_src_m);
1521 crp->crp_buf = (void *)q->q_dst_m;
1522 }
1523
1524 /* copy out IV for future use */
1525 if (q->q_flags & UBSEC_QFLAGS_COPYOUTIV) {
1526 for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
1527 if (crd->crd_alg != CRYPTO_DES_CBC &&
1528 crd->crd_alg != CRYPTO_3DES_CBC)
1529 continue;
1530 if (crp->crp_flags & CRYPTO_F_IMBUF)
1531 m_copydata((struct mbuf *)crp->crp_buf,
1532 crd->crd_skip + crd->crd_len - 8, 8,
1533 (void *)sc->sc_sessions[q->q_sesn].ses_iv);
1534 else if (crp->crp_flags & CRYPTO_F_IOV) {
1535 cuio_copydata((struct uio *)crp->crp_buf,
1536 crd->crd_skip + crd->crd_len - 8, 8,
1537 (void *)sc->sc_sessions[q->q_sesn].ses_iv);
1538 }
1539 break;
1540 }
1541 }
1542
1543 for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
1544 if (crd->crd_alg != CRYPTO_MD5_HMAC_96 &&
1545 crd->crd_alg != CRYPTO_SHA1_HMAC_96)
1546 continue;
1547 if (crp->crp_flags & CRYPTO_F_IMBUF)
1548 m_copyback((struct mbuf *)crp->crp_buf,
1549 crd->crd_inject, 12,
1550 (void *)dmap->d_dma->d_macbuf);
1551 else if (crp->crp_flags & CRYPTO_F_IOV && crp->crp_mac)
1552 bcopy((void *)dmap->d_dma->d_macbuf,
1553 crp->crp_mac, 12);
1554 break;
1555 }
1556 SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
1557 crypto_done(crp);
1558 }
1559
1560 static void
1561 ubsec_mcopy(struct mbuf *srcm, struct mbuf *dstm, int hoffset, int toffset)
1562 {
1563 int i, j, dlen, slen;
1564 char *dptr, *sptr;
1565
1566 j = 0;
1567 sptr = srcm->m_data;
1568 slen = srcm->m_len;
1569 dptr = dstm->m_data;
1570 dlen = dstm->m_len;
1571
1572 while (1) {
1573 for (i = 0; i < min(slen, dlen); i++) {
1574 if (j < hoffset || j >= toffset)
1575 *dptr++ = *sptr++;
1576 slen--;
1577 dlen--;
1578 j++;
1579 }
1580 if (slen == 0) {
1581 srcm = srcm->m_next;
1582 if (srcm == NULL)
1583 return;
1584 sptr = srcm->m_data;
1585 slen = srcm->m_len;
1586 }
1587 if (dlen == 0) {
1588 dstm = dstm->m_next;
1589 if (dstm == NULL)
1590 return;
1591 dptr = dstm->m_data;
1592 dlen = dstm->m_len;
1593 }
1594 }
1595 }
1596
1597 /*
1598 * feed the key generator, must be called at splnet() or higher.
1599 */
1600 static void
1601 ubsec_feed2(struct ubsec_softc *sc)
1602 {
1603 struct ubsec_q2 *q;
1604
1605 while (!SIMPLEQ_EMPTY(&sc->sc_queue2)) {
1606 if (READ_REG(sc, BS_STAT) & BS_STAT_MCR2_FULL)
1607 break;
1608 q = SIMPLEQ_FIRST(&sc->sc_queue2);
1609
1610 bus_dmamap_sync(sc->sc_dmat, q->q_mcr.dma_map, 0,
1611 q->q_mcr.dma_map->dm_mapsize,
1612 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1613 bus_dmamap_sync(sc->sc_dmat, q->q_ctx.dma_map, 0,
1614 q->q_ctx.dma_map->dm_mapsize,
1615 BUS_DMASYNC_PREWRITE);
1616
1617 WRITE_REG(sc, BS_MCR2, q->q_mcr.dma_paddr);
1618 q = SIMPLEQ_FIRST(&sc->sc_queue2);
1619 SIMPLEQ_REMOVE_HEAD(&sc->sc_queue2, /*q,*/ q_next);
1620 --sc->sc_nqueue2;
1621 SIMPLEQ_INSERT_TAIL(&sc->sc_qchip2, q, q_next);
1622 }
1623 }
1624
1625 /*
1626 * Callback for handling random numbers
1627 */
1628 static void
1629 ubsec_callback2(struct ubsec_softc *sc, struct ubsec_q2 *q)
1630 {
1631 struct cryptkop *krp;
1632 struct ubsec_ctx_keyop *ctx;
1633
1634 ctx = (struct ubsec_ctx_keyop *)q->q_ctx.dma_vaddr;
1635 bus_dmamap_sync(sc->sc_dmat, q->q_ctx.dma_map, 0,
1636 q->q_ctx.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1637
1638 switch (q->q_type) {
1639 #ifndef UBSEC_NO_RNG
1640 case UBS_CTXOP_RNGSHA1:
1641 case UBS_CTXOP_RNGBYPASS: {
1642 struct ubsec_q2_rng *rng = (struct ubsec_q2_rng *)q;
1643 u_int32_t *p;
1644 int i;
1645
1646 bus_dmamap_sync(sc->sc_dmat, rng->rng_buf.dma_map, 0,
1647 rng->rng_buf.dma_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1648 p = (u_int32_t *)rng->rng_buf.dma_vaddr;
1649 #ifndef __NetBSD__
1650 for (i = 0; i < UBSEC_RNG_BUFSIZ; p++, i++)
1651 add_true_randomness(letoh32(*p));
1652 rng->rng_used = 0;
1653 #else
1654 i = UBSEC_RNG_BUFSIZ * sizeof(u_int32_t);
1655 rnd_add_data(&sc->sc_rnd_source, (char *)p, i, i * NBBY);
1656 sc->sc_rng_need -= i;
1657 rng->rng_used = 0;
1658 #endif
1659 #ifdef __OpenBSD__
1660 timeout_add(&sc->sc_rngto, sc->sc_rnghz);
1661 #else
1662 if (sc->sc_rng_need > 0) {
1663 callout_schedule(&sc->sc_rngto, sc->sc_rnghz);
1664 }
1665 #endif
1666 break;
1667 }
1668 #endif
1669 case UBS_CTXOP_MODEXP: {
1670 struct ubsec_q2_modexp *me = (struct ubsec_q2_modexp *)q;
1671 u_int rlen, clen;
1672
1673 krp = me->me_krp;
1674 rlen = (me->me_modbits + 7) / 8;
1675 clen = (krp->krp_param[krp->krp_iparams].crp_nbits + 7) / 8;
1676
1677 bus_dmamap_sync(sc->sc_dmat, me->me_M.dma_map,
1678 0, me->me_M.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1679 bus_dmamap_sync(sc->sc_dmat, me->me_E.dma_map,
1680 0, me->me_E.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1681 bus_dmamap_sync(sc->sc_dmat, me->me_C.dma_map,
1682 0, me->me_C.dma_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1683 bus_dmamap_sync(sc->sc_dmat, me->me_epb.dma_map,
1684 0, me->me_epb.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1685
1686 if (clen < rlen)
1687 krp->krp_status = E2BIG;
1688 else {
1689 if (sc->sc_flags & UBS_FLAGS_HWNORM) {
1690 memset(krp->krp_param[krp->krp_iparams].crp_p, 0,
1691 (krp->krp_param[krp->krp_iparams].crp_nbits
1692 + 7) / 8);
1693 bcopy(me->me_C.dma_vaddr,
1694 krp->krp_param[krp->krp_iparams].crp_p,
1695 (me->me_modbits + 7) / 8);
1696 } else
1697 ubsec_kshift_l(me->me_shiftbits,
1698 me->me_C.dma_vaddr, me->me_normbits,
1699 krp->krp_param[krp->krp_iparams].crp_p,
1700 krp->krp_param[krp->krp_iparams].crp_nbits);
1701 }
1702
1703 crypto_kdone(krp);
1704
1705 /* bzero all potentially sensitive data */
1706 memset(me->me_E.dma_vaddr, 0, me->me_E.dma_size);
1707 memset(me->me_M.dma_vaddr, 0, me->me_M.dma_size);
1708 memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
1709 memset(me->me_q.q_ctx.dma_vaddr, 0, me->me_q.q_ctx.dma_size);
1710
1711 /* Can't free here, so put us on the free list. */
1712 SIMPLEQ_INSERT_TAIL(&sc->sc_q2free, &me->me_q, q_next);
1713 break;
1714 }
1715 case UBS_CTXOP_RSAPRIV: {
1716 struct ubsec_q2_rsapriv *rp = (struct ubsec_q2_rsapriv *)q;
1717 u_int len;
1718
1719 krp = rp->rpr_krp;
1720 bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgin.dma_map, 0,
1721 rp->rpr_msgin.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1722 bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgout.dma_map, 0,
1723 rp->rpr_msgout.dma_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1724
1725 len = (krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT].crp_nbits + 7) / 8;
1726 bcopy(rp->rpr_msgout.dma_vaddr,
1727 krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT].crp_p, len);
1728
1729 crypto_kdone(krp);
1730
1731 memset(rp->rpr_msgin.dma_vaddr, 0, rp->rpr_msgin.dma_size);
1732 memset(rp->rpr_msgout.dma_vaddr, 0, rp->rpr_msgout.dma_size);
1733 memset(rp->rpr_q.q_ctx.dma_vaddr, 0, rp->rpr_q.q_ctx.dma_size);
1734
1735 /* Can't free here, so put us on the free list. */
1736 SIMPLEQ_INSERT_TAIL(&sc->sc_q2free, &rp->rpr_q, q_next);
1737 break;
1738 }
1739 default:
1740 printf("%s: unknown ctx op: %x\n", device_xname(sc->sc_dev),
1741 letoh16(ctx->ctx_op));
1742 break;
1743 }
1744 }
1745
1746 #ifndef UBSEC_NO_RNG
1747
1748 static void
1749 ubsec_rng_get(size_t bytes, void *vsc)
1750 {
1751 struct ubsec_softc *sc = vsc;
1752
1753 mutex_spin_enter(&sc->sc_mtx);
1754 sc->sc_rng_need = bytes;
1755 ubsec_rng_locked(sc);
1756 mutex_spin_exit(&sc->sc_mtx);
1757
1758 }
1759
1760 static void
1761 ubsec_rng(void *vsc)
1762 {
1763 struct ubsec_softc *sc = vsc;
1764 mutex_spin_enter(&sc->sc_mtx);
1765 ubsec_rng_locked(sc);
1766 mutex_spin_exit(&sc->sc_mtx);
1767 }
1768
1769 static void
1770 ubsec_rng_locked(void *vsc)
1771 {
1772 struct ubsec_softc *sc = vsc;
1773 struct ubsec_q2_rng *rng = &sc->sc_rng;
1774 struct ubsec_mcr *mcr;
1775 struct ubsec_ctx_rngbypass *ctx;
1776
1777 mutex_spin_enter(&sc->sc_mtx);
1778 if (rng->rng_used) {
1779 mutex_spin_exit(&sc->sc_mtx);
1780 return;
1781 }
1782
1783 if (sc->sc_rng_need < 1) {
1784 callout_stop(&sc->sc_rngto);
1785 mutex_spin_exit(&sc->sc_mtx);
1786 return;
1787 }
1788
1789 sc->sc_nqueue2++;
1790 if (sc->sc_nqueue2 >= UBS_MAX_NQUEUE)
1791 goto out;
1792
1793 mcr = (struct ubsec_mcr *)rng->rng_q.q_mcr.dma_vaddr;
1794 ctx = (struct ubsec_ctx_rngbypass *)rng->rng_q.q_ctx.dma_vaddr;
1795
1796 mcr->mcr_pkts = htole16(1);
1797 mcr->mcr_flags = 0;
1798 mcr->mcr_cmdctxp = htole32(rng->rng_q.q_ctx.dma_paddr);
1799 mcr->mcr_ipktbuf.pb_addr = mcr->mcr_ipktbuf.pb_next = 0;
1800 mcr->mcr_ipktbuf.pb_len = 0;
1801 mcr->mcr_reserved = mcr->mcr_pktlen = 0;
1802 mcr->mcr_opktbuf.pb_addr = htole32(rng->rng_buf.dma_paddr);
1803 mcr->mcr_opktbuf.pb_len = htole32(((sizeof(u_int32_t) * UBSEC_RNG_BUFSIZ)) &
1804 UBS_PKTBUF_LEN);
1805 mcr->mcr_opktbuf.pb_next = 0;
1806
1807 ctx->rbp_len = htole16(sizeof(struct ubsec_ctx_rngbypass));
1808 ctx->rbp_op = htole16(UBS_CTXOP_RNGSHA1);
1809 rng->rng_q.q_type = UBS_CTXOP_RNGSHA1;
1810
1811 bus_dmamap_sync(sc->sc_dmat, rng->rng_buf.dma_map, 0,
1812 rng->rng_buf.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
1813
1814 SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &rng->rng_q, q_next);
1815 rng->rng_used = 1;
1816 ubsec_feed2(sc);
1817 ubsecstats.hst_rng++;
1818 mutex_spin_exit(&sc->sc_mtx);
1819
1820 return;
1821
1822 out:
1823 /*
1824 * Something weird happened, generate our own call back.
1825 */
1826 sc->sc_nqueue2--;
1827 mutex_spin_exit(&sc->sc_mtx);
1828 #ifdef __OpenBSD__
1829 timeout_add(&sc->sc_rngto, sc->sc_rnghz);
1830 #else
1831 callout_schedule(&sc->sc_rngto, sc->sc_rnghz);
1832 #endif
1833 }
1834 #endif /* UBSEC_NO_RNG */
1835
1836 static int
1837 ubsec_dma_malloc(struct ubsec_softc *sc, bus_size_t size,
1838 struct ubsec_dma_alloc *dma,int mapflags)
1839 {
1840 int r;
1841
1842 if ((r = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0,
1843 &dma->dma_seg, 1, &dma->dma_nseg, BUS_DMA_NOWAIT)) != 0)
1844 goto fail_0;
1845
1846 if ((r = bus_dmamem_map(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg,
1847 size, &dma->dma_vaddr, mapflags | BUS_DMA_NOWAIT)) != 0)
1848 goto fail_1;
1849
1850 if ((r = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1851 BUS_DMA_NOWAIT, &dma->dma_map)) != 0)
1852 goto fail_2;
1853
1854 if ((r = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_vaddr,
1855 size, NULL, BUS_DMA_NOWAIT)) != 0)
1856 goto fail_3;
1857
1858 dma->dma_paddr = dma->dma_map->dm_segs[0].ds_addr;
1859 dma->dma_size = size;
1860 return (0);
1861
1862 fail_3:
1863 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
1864 fail_2:
1865 bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, size);
1866 fail_1:
1867 bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
1868 fail_0:
1869 dma->dma_map = NULL;
1870 return (r);
1871 }
1872
1873 static void
1874 ubsec_dma_free(struct ubsec_softc *sc, struct ubsec_dma_alloc *dma)
1875 {
1876 bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
1877 bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, dma->dma_size);
1878 bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
1879 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
1880 }
1881
1882 /*
1883 * Resets the board. Values in the regesters are left as is
1884 * from the reset (i.e. initial values are assigned elsewhere).
1885 */
1886 static void
1887 ubsec_reset_board(struct ubsec_softc *sc)
1888 {
1889 volatile u_int32_t ctrl;
1890
1891 ctrl = READ_REG(sc, BS_CTRL);
1892 ctrl |= BS_CTRL_RESET;
1893 WRITE_REG(sc, BS_CTRL, ctrl);
1894
1895 /*
1896 * Wait aprox. 30 PCI clocks = 900 ns = 0.9 us
1897 */
1898 DELAY(10);
1899 }
1900
1901 /*
1902 * Init Broadcom registers
1903 */
1904 static void
1905 ubsec_init_board(struct ubsec_softc *sc)
1906 {
1907 u_int32_t ctrl;
1908
1909 ctrl = READ_REG(sc, BS_CTRL);
1910 ctrl &= ~(BS_CTRL_BE32 | BS_CTRL_BE64);
1911 ctrl |= BS_CTRL_LITTLE_ENDIAN | BS_CTRL_MCR1INT;
1912
1913 /*
1914 * XXX: Sam Leffler's code has (UBS_FLAGS_KEY|UBS_FLAGS_RNG)).
1915 * anyone got hw docs?
1916 */
1917 if (sc->sc_flags & UBS_FLAGS_KEY)
1918 ctrl |= BS_CTRL_MCR2INT;
1919 else
1920 ctrl &= ~BS_CTRL_MCR2INT;
1921
1922 if (sc->sc_flags & UBS_FLAGS_HWNORM)
1923 ctrl &= ~BS_CTRL_SWNORM;
1924
1925 WRITE_REG(sc, BS_CTRL, ctrl);
1926 }
1927
1928 /*
1929 * Init Broadcom PCI registers
1930 */
1931 static void
1932 ubsec_init_pciregs(struct pci_attach_args *pa)
1933 {
1934 pci_chipset_tag_t pc = pa->pa_pc;
1935 u_int32_t misc;
1936
1937 /*
1938 * This will set the cache line size to 1, this will
1939 * force the BCM58xx chip just to do burst read/writes.
1940 * Cache line read/writes are to slow
1941 */
1942 misc = pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
1943 misc = (misc & ~(PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT))
1944 | ((UBS_DEF_CACHELINE & 0xff) << PCI_CACHELINE_SHIFT);
1945 pci_conf_write(pc, pa->pa_tag, PCI_BHLC_REG, misc);
1946 }
1947
1948 /*
1949 * Clean up after a chip crash.
1950 * It is assumed that the caller in splnet()
1951 */
1952 static void
1953 ubsec_cleanchip(struct ubsec_softc *sc)
1954 {
1955 struct ubsec_q *q;
1956
1957 while (!SIMPLEQ_EMPTY(&sc->sc_qchip)) {
1958 q = SIMPLEQ_FIRST(&sc->sc_qchip);
1959 SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip, /*q,*/ q_next);
1960 ubsec_free_q(sc, q);
1961 }
1962 sc->sc_nqchip = 0;
1963 }
1964
1965 /*
1966 * free a ubsec_q
1967 * It is assumed that the caller is within splnet()
1968 */
1969 static int
1970 ubsec_free_q(struct ubsec_softc *sc, struct ubsec_q *q)
1971 {
1972 struct ubsec_q *q2;
1973 struct cryptop *crp;
1974 int npkts;
1975 int i;
1976
1977 npkts = q->q_nstacked_mcrs;
1978
1979 for (i = 0; i < npkts; i++) {
1980 if(q->q_stacked_mcr[i]) {
1981 q2 = q->q_stacked_mcr[i];
1982
1983 if ((q2->q_dst_m != NULL) && (q2->q_src_m != q2->q_dst_m))
1984 m_freem(q2->q_dst_m);
1985
1986 crp = (struct cryptop *)q2->q_crp;
1987
1988 SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q2, q_next);
1989
1990 crp->crp_etype = EFAULT;
1991 crypto_done(crp);
1992 } else {
1993 break;
1994 }
1995 }
1996
1997 /*
1998 * Free header MCR
1999 */
2000 if ((q->q_dst_m != NULL) && (q->q_src_m != q->q_dst_m))
2001 m_freem(q->q_dst_m);
2002
2003 crp = (struct cryptop *)q->q_crp;
2004
2005 SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
2006
2007 crp->crp_etype = EFAULT;
2008 crypto_done(crp);
2009 return(0);
2010 }
2011
2012 /*
2013 * Routine to reset the chip and clean up.
2014 * It is assumed that the caller is in splnet()
2015 */
2016 static void
2017 ubsec_totalreset(struct ubsec_softc *sc)
2018 {
2019 ubsec_reset_board(sc);
2020 ubsec_init_board(sc);
2021 ubsec_cleanchip(sc);
2022 }
2023
2024 static int
2025 ubsec_dmamap_aligned(bus_dmamap_t map)
2026 {
2027 int i;
2028
2029 for (i = 0; i < map->dm_nsegs; i++) {
2030 if (map->dm_segs[i].ds_addr & 3)
2031 return (0);
2032 if ((i != (map->dm_nsegs - 1)) &&
2033 (map->dm_segs[i].ds_len & 3))
2034 return (0);
2035 }
2036 return (1);
2037 }
2038
2039 #ifdef __OpenBSD__
2040 struct ubsec_softc *
2041 ubsec_kfind(struct cryptkop *krp)
2042 {
2043 struct ubsec_softc *sc;
2044 int i;
2045
2046 for (i = 0; i < ubsec_cd.cd_ndevs; i++) {
2047 sc = ubsec_cd.cd_devs[i];
2048 if (sc == NULL)
2049 continue;
2050 if (sc->sc_cid == krp->krp_hid)
2051 return (sc);
2052 }
2053 return (NULL);
2054 }
2055 #endif
2056
2057 static void
2058 ubsec_kfree(struct ubsec_softc *sc, struct ubsec_q2 *q)
2059 {
2060 switch (q->q_type) {
2061 case UBS_CTXOP_MODEXP: {
2062 struct ubsec_q2_modexp *me = (struct ubsec_q2_modexp *)q;
2063
2064 ubsec_dma_free(sc, &me->me_q.q_mcr);
2065 ubsec_dma_free(sc, &me->me_q.q_ctx);
2066 ubsec_dma_free(sc, &me->me_M);
2067 ubsec_dma_free(sc, &me->me_E);
2068 ubsec_dma_free(sc, &me->me_C);
2069 ubsec_dma_free(sc, &me->me_epb);
2070 free(me, M_DEVBUF);
2071 break;
2072 }
2073 case UBS_CTXOP_RSAPRIV: {
2074 struct ubsec_q2_rsapriv *rp = (struct ubsec_q2_rsapriv *)q;
2075
2076 ubsec_dma_free(sc, &rp->rpr_q.q_mcr);
2077 ubsec_dma_free(sc, &rp->rpr_q.q_ctx);
2078 ubsec_dma_free(sc, &rp->rpr_msgin);
2079 ubsec_dma_free(sc, &rp->rpr_msgout);
2080 free(rp, M_DEVBUF);
2081 break;
2082 }
2083 default:
2084 printf("%s: invalid kfree 0x%x\n", device_xname(sc->sc_dev),
2085 q->q_type);
2086 break;
2087 }
2088 }
2089
2090 static int
2091 ubsec_kprocess(void *arg, struct cryptkop *krp, int hint)
2092 {
2093 struct ubsec_softc *sc;
2094 int r;
2095
2096 if (krp == NULL || krp->krp_callback == NULL)
2097 return (EINVAL);
2098 #ifdef __OpenBSD__
2099 if ((sc = ubsec_kfind(krp)) == NULL)
2100 return (EINVAL);
2101 #else
2102 sc = arg;
2103 KASSERT(sc != NULL /*, ("ubsec_kprocess: null softc")*/);
2104 #endif
2105
2106 while (!SIMPLEQ_EMPTY(&sc->sc_q2free)) {
2107 struct ubsec_q2 *q;
2108
2109 q = SIMPLEQ_FIRST(&sc->sc_q2free);
2110 SIMPLEQ_REMOVE_HEAD(&sc->sc_q2free, /*q,*/ q_next);
2111 ubsec_kfree(sc, q);
2112 }
2113
2114 switch (krp->krp_op) {
2115 case CRK_MOD_EXP:
2116 if (sc->sc_flags & UBS_FLAGS_HWNORM)
2117 r = ubsec_kprocess_modexp_hw(sc, krp, hint);
2118 else
2119 r = ubsec_kprocess_modexp_sw(sc, krp, hint);
2120 break;
2121 case CRK_MOD_EXP_CRT:
2122 r = ubsec_kprocess_rsapriv(sc, krp, hint);
2123 break;
2124 default:
2125 printf("%s: kprocess: invalid op 0x%x\n",
2126 device_xname(sc->sc_dev), krp->krp_op);
2127 krp->krp_status = EOPNOTSUPP;
2128 crypto_kdone(krp);
2129 r = 0;
2130 }
2131 return (r);
2132 }
2133
2134 /*
2135 * Start computation of cr[C] = (cr[M] ^ cr[E]) mod cr[N] (sw normalization)
2136 */
2137 static int
2138 ubsec_kprocess_modexp_sw(struct ubsec_softc *sc, struct cryptkop *krp,
2139 int hint)
2140 {
2141 struct ubsec_q2_modexp *me;
2142 struct ubsec_mcr *mcr;
2143 struct ubsec_ctx_modexp *ctx;
2144 struct ubsec_pktbuf *epb;
2145 int err = 0;
2146 u_int nbits, normbits, mbits, shiftbits, ebits;
2147
2148 me = (struct ubsec_q2_modexp *)malloc(sizeof *me, M_DEVBUF, M_NOWAIT);
2149 if (me == NULL) {
2150 err = ENOMEM;
2151 goto errout;
2152 }
2153 memset(me, 0, sizeof *me);
2154 me->me_krp = krp;
2155 me->me_q.q_type = UBS_CTXOP_MODEXP;
2156
2157 nbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_N]);
2158 if (nbits <= 512)
2159 normbits = 512;
2160 else if (nbits <= 768)
2161 normbits = 768;
2162 else if (nbits <= 1024)
2163 normbits = 1024;
2164 else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 1536)
2165 normbits = 1536;
2166 else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 2048)
2167 normbits = 2048;
2168 else {
2169 err = E2BIG;
2170 goto errout;
2171 }
2172
2173 shiftbits = normbits - nbits;
2174
2175 me->me_modbits = nbits;
2176 me->me_shiftbits = shiftbits;
2177 me->me_normbits = normbits;
2178
2179 /* Sanity check: result bits must be >= true modulus bits. */
2180 if (krp->krp_param[krp->krp_iparams].crp_nbits < nbits) {
2181 err = ERANGE;
2182 goto errout;
2183 }
2184
2185 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
2186 &me->me_q.q_mcr, 0)) {
2187 err = ENOMEM;
2188 goto errout;
2189 }
2190 mcr = (struct ubsec_mcr *)me->me_q.q_mcr.dma_vaddr;
2191
2192 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_modexp),
2193 &me->me_q.q_ctx, 0)) {
2194 err = ENOMEM;
2195 goto errout;
2196 }
2197
2198 mbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_M]);
2199 if (mbits > nbits) {
2200 err = E2BIG;
2201 goto errout;
2202 }
2203 if (ubsec_dma_malloc(sc, normbits / 8, &me->me_M, 0)) {
2204 err = ENOMEM;
2205 goto errout;
2206 }
2207 ubsec_kshift_r(shiftbits,
2208 krp->krp_param[UBS_MODEXP_PAR_M].crp_p, mbits,
2209 me->me_M.dma_vaddr, normbits);
2210
2211 if (ubsec_dma_malloc(sc, normbits / 8, &me->me_C, 0)) {
2212 err = ENOMEM;
2213 goto errout;
2214 }
2215 memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
2216
2217 ebits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_E]);
2218 if (ebits > nbits) {
2219 err = E2BIG;
2220 goto errout;
2221 }
2222 if (ubsec_dma_malloc(sc, normbits / 8, &me->me_E, 0)) {
2223 err = ENOMEM;
2224 goto errout;
2225 }
2226 ubsec_kshift_r(shiftbits,
2227 krp->krp_param[UBS_MODEXP_PAR_E].crp_p, ebits,
2228 me->me_E.dma_vaddr, normbits);
2229
2230 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_pktbuf),
2231 &me->me_epb, 0)) {
2232 err = ENOMEM;
2233 goto errout;
2234 }
2235 epb = (struct ubsec_pktbuf *)me->me_epb.dma_vaddr;
2236 epb->pb_addr = htole32(me->me_E.dma_paddr);
2237 epb->pb_next = 0;
2238 epb->pb_len = htole32(normbits / 8);
2239
2240 #ifdef UBSEC_DEBUG
2241 if (ubsec_debug) {
2242 printf("Epb ");
2243 ubsec_dump_pb(epb);
2244 }
2245 #endif
2246
2247 mcr->mcr_pkts = htole16(1);
2248 mcr->mcr_flags = 0;
2249 mcr->mcr_cmdctxp = htole32(me->me_q.q_ctx.dma_paddr);
2250 mcr->mcr_reserved = 0;
2251 mcr->mcr_pktlen = 0;
2252
2253 mcr->mcr_ipktbuf.pb_addr = htole32(me->me_M.dma_paddr);
2254 mcr->mcr_ipktbuf.pb_len = htole32(normbits / 8);
2255 mcr->mcr_ipktbuf.pb_next = htole32(me->me_epb.dma_paddr);
2256
2257 mcr->mcr_opktbuf.pb_addr = htole32(me->me_C.dma_paddr);
2258 mcr->mcr_opktbuf.pb_next = 0;
2259 mcr->mcr_opktbuf.pb_len = htole32(normbits / 8);
2260
2261 #ifdef DIAGNOSTIC
2262 /* Misaligned output buffer will hang the chip. */
2263 if ((letoh32(mcr->mcr_opktbuf.pb_addr) & 3) != 0)
2264 panic("%s: modexp invalid addr 0x%x",
2265 device_xname(sc->sc_dev), letoh32(mcr->mcr_opktbuf.pb_addr));
2266 if ((letoh32(mcr->mcr_opktbuf.pb_len) & 3) != 0)
2267 panic("%s: modexp invalid len 0x%x",
2268 device_xname(sc->sc_dev), letoh32(mcr->mcr_opktbuf.pb_len));
2269 #endif
2270
2271 ctx = (struct ubsec_ctx_modexp *)me->me_q.q_ctx.dma_vaddr;
2272 memset(ctx, 0, sizeof(*ctx));
2273 ubsec_kshift_r(shiftbits,
2274 krp->krp_param[UBS_MODEXP_PAR_N].crp_p, nbits,
2275 ctx->me_N, normbits);
2276 ctx->me_len = htole16((normbits / 8) + (4 * sizeof(u_int16_t)));
2277 ctx->me_op = htole16(UBS_CTXOP_MODEXP);
2278 ctx->me_E_len = htole16(nbits);
2279 ctx->me_N_len = htole16(nbits);
2280
2281 #ifdef UBSEC_DEBUG
2282 if (ubsec_debug) {
2283 ubsec_dump_mcr(mcr);
2284 ubsec_dump_ctx2((struct ubsec_ctx_keyop *)ctx);
2285 }
2286 #endif
2287
2288 /*
2289 * ubsec_feed2 will sync mcr and ctx, we just need to sync
2290 * everything else.
2291 */
2292 bus_dmamap_sync(sc->sc_dmat, me->me_M.dma_map,
2293 0, me->me_M.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2294 bus_dmamap_sync(sc->sc_dmat, me->me_E.dma_map,
2295 0, me->me_E.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2296 bus_dmamap_sync(sc->sc_dmat, me->me_C.dma_map,
2297 0, me->me_C.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
2298 bus_dmamap_sync(sc->sc_dmat, me->me_epb.dma_map,
2299 0, me->me_epb.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2300
2301 /* Enqueue and we're done... */
2302 mutex_spin_enter(&sc->sc_mtx);
2303 SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &me->me_q, q_next);
2304 ubsec_feed2(sc);
2305 ubsecstats.hst_modexp++;
2306 mutex_spin_exit(&sc->sc_mtx);
2307
2308 return (0);
2309
2310 errout:
2311 if (me != NULL) {
2312 if (me->me_q.q_mcr.dma_map != NULL)
2313 ubsec_dma_free(sc, &me->me_q.q_mcr);
2314 if (me->me_q.q_ctx.dma_map != NULL) {
2315 memset(me->me_q.q_ctx.dma_vaddr, 0, me->me_q.q_ctx.dma_size);
2316 ubsec_dma_free(sc, &me->me_q.q_ctx);
2317 }
2318 if (me->me_M.dma_map != NULL) {
2319 memset(me->me_M.dma_vaddr, 0, me->me_M.dma_size);
2320 ubsec_dma_free(sc, &me->me_M);
2321 }
2322 if (me->me_E.dma_map != NULL) {
2323 memset(me->me_E.dma_vaddr, 0, me->me_E.dma_size);
2324 ubsec_dma_free(sc, &me->me_E);
2325 }
2326 if (me->me_C.dma_map != NULL) {
2327 memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
2328 ubsec_dma_free(sc, &me->me_C);
2329 }
2330 if (me->me_epb.dma_map != NULL)
2331 ubsec_dma_free(sc, &me->me_epb);
2332 free(me, M_DEVBUF);
2333 }
2334 krp->krp_status = err;
2335 crypto_kdone(krp);
2336 return (0);
2337 }
2338
2339 /*
2340 * Start computation of cr[C] = (cr[M] ^ cr[E]) mod cr[N] (hw normalization)
2341 */
2342 static int
2343 ubsec_kprocess_modexp_hw(struct ubsec_softc *sc, struct cryptkop *krp,
2344 int hint)
2345 {
2346 struct ubsec_q2_modexp *me;
2347 struct ubsec_mcr *mcr;
2348 struct ubsec_ctx_modexp *ctx;
2349 struct ubsec_pktbuf *epb;
2350 int err = 0;
2351 u_int nbits, normbits, mbits, shiftbits, ebits;
2352
2353 me = (struct ubsec_q2_modexp *)malloc(sizeof *me, M_DEVBUF, M_NOWAIT);
2354 if (me == NULL) {
2355 err = ENOMEM;
2356 goto errout;
2357 }
2358 memset(me, 0, sizeof *me);
2359 me->me_krp = krp;
2360 me->me_q.q_type = UBS_CTXOP_MODEXP;
2361
2362 nbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_N]);
2363 if (nbits <= 512)
2364 normbits = 512;
2365 else if (nbits <= 768)
2366 normbits = 768;
2367 else if (nbits <= 1024)
2368 normbits = 1024;
2369 else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 1536)
2370 normbits = 1536;
2371 else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 2048)
2372 normbits = 2048;
2373 else {
2374 err = E2BIG;
2375 goto errout;
2376 }
2377
2378 shiftbits = normbits - nbits;
2379
2380 /* XXX ??? */
2381 me->me_modbits = nbits;
2382 me->me_shiftbits = shiftbits;
2383 me->me_normbits = normbits;
2384
2385 /* Sanity check: result bits must be >= true modulus bits. */
2386 if (krp->krp_param[krp->krp_iparams].crp_nbits < nbits) {
2387 err = ERANGE;
2388 goto errout;
2389 }
2390
2391 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
2392 &me->me_q.q_mcr, 0)) {
2393 err = ENOMEM;
2394 goto errout;
2395 }
2396 mcr = (struct ubsec_mcr *)me->me_q.q_mcr.dma_vaddr;
2397
2398 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_modexp),
2399 &me->me_q.q_ctx, 0)) {
2400 err = ENOMEM;
2401 goto errout;
2402 }
2403
2404 mbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_M]);
2405 if (mbits > nbits) {
2406 err = E2BIG;
2407 goto errout;
2408 }
2409 if (ubsec_dma_malloc(sc, normbits / 8, &me->me_M, 0)) {
2410 err = ENOMEM;
2411 goto errout;
2412 }
2413 memset(me->me_M.dma_vaddr, 0, normbits / 8);
2414 bcopy(krp->krp_param[UBS_MODEXP_PAR_M].crp_p,
2415 me->me_M.dma_vaddr, (mbits + 7) / 8);
2416
2417 if (ubsec_dma_malloc(sc, normbits / 8, &me->me_C, 0)) {
2418 err = ENOMEM;
2419 goto errout;
2420 }
2421 memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
2422
2423 ebits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_E]);
2424 if (ebits > nbits) {
2425 err = E2BIG;
2426 goto errout;
2427 }
2428 if (ubsec_dma_malloc(sc, normbits / 8, &me->me_E, 0)) {
2429 err = ENOMEM;
2430 goto errout;
2431 }
2432 memset(me->me_E.dma_vaddr, 0, normbits / 8);
2433 bcopy(krp->krp_param[UBS_MODEXP_PAR_E].crp_p,
2434 me->me_E.dma_vaddr, (ebits + 7) / 8);
2435
2436 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_pktbuf),
2437 &me->me_epb, 0)) {
2438 err = ENOMEM;
2439 goto errout;
2440 }
2441 epb = (struct ubsec_pktbuf *)me->me_epb.dma_vaddr;
2442 epb->pb_addr = htole32(me->me_E.dma_paddr);
2443 epb->pb_next = 0;
2444 epb->pb_len = htole32((ebits + 7) / 8);
2445
2446 #ifdef UBSEC_DEBUG
2447 if (ubsec_debug) {
2448 printf("Epb ");
2449 ubsec_dump_pb(epb);
2450 }
2451 #endif
2452
2453 mcr->mcr_pkts = htole16(1);
2454 mcr->mcr_flags = 0;
2455 mcr->mcr_cmdctxp = htole32(me->me_q.q_ctx.dma_paddr);
2456 mcr->mcr_reserved = 0;
2457 mcr->mcr_pktlen = 0;
2458
2459 mcr->mcr_ipktbuf.pb_addr = htole32(me->me_M.dma_paddr);
2460 mcr->mcr_ipktbuf.pb_len = htole32(normbits / 8);
2461 mcr->mcr_ipktbuf.pb_next = htole32(me->me_epb.dma_paddr);
2462
2463 mcr->mcr_opktbuf.pb_addr = htole32(me->me_C.dma_paddr);
2464 mcr->mcr_opktbuf.pb_next = 0;
2465 mcr->mcr_opktbuf.pb_len = htole32(normbits / 8);
2466
2467 #ifdef DIAGNOSTIC
2468 /* Misaligned output buffer will hang the chip. */
2469 if ((letoh32(mcr->mcr_opktbuf.pb_addr) & 3) != 0)
2470 panic("%s: modexp invalid addr 0x%x",
2471 device_xname(sc->sc_dev), letoh32(mcr->mcr_opktbuf.pb_addr));
2472 if ((letoh32(mcr->mcr_opktbuf.pb_len) & 3) != 0)
2473 panic("%s: modexp invalid len 0x%x",
2474 device_xname(sc->sc_dev), letoh32(mcr->mcr_opktbuf.pb_len));
2475 #endif
2476
2477 ctx = (struct ubsec_ctx_modexp *)me->me_q.q_ctx.dma_vaddr;
2478 memset(ctx, 0, sizeof(*ctx));
2479 memcpy(ctx->me_N, krp->krp_param[UBS_MODEXP_PAR_N].crp_p,
2480 (nbits + 7) / 8);
2481 ctx->me_len = htole16((normbits / 8) + (4 * sizeof(u_int16_t)));
2482 ctx->me_op = htole16(UBS_CTXOP_MODEXP);
2483 ctx->me_E_len = htole16(ebits);
2484 ctx->me_N_len = htole16(nbits);
2485
2486 #ifdef UBSEC_DEBUG
2487 if (ubsec_debug) {
2488 ubsec_dump_mcr(mcr);
2489 ubsec_dump_ctx2((struct ubsec_ctx_keyop *)ctx);
2490 }
2491 #endif
2492
2493 /*
2494 * ubsec_feed2 will sync mcr and ctx, we just need to sync
2495 * everything else.
2496 */
2497 bus_dmamap_sync(sc->sc_dmat, me->me_M.dma_map,
2498 0, me->me_M.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2499 bus_dmamap_sync(sc->sc_dmat, me->me_E.dma_map,
2500 0, me->me_E.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2501 bus_dmamap_sync(sc->sc_dmat, me->me_C.dma_map,
2502 0, me->me_C.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
2503 bus_dmamap_sync(sc->sc_dmat, me->me_epb.dma_map,
2504 0, me->me_epb.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2505
2506 /* Enqueue and we're done... */
2507 mutex_spin_enter(&sc->sc_mtx);
2508 SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &me->me_q, q_next);
2509 ubsec_feed2(sc);
2510 mutex_spin_exit(&sc->sc_mtx);
2511
2512 return (0);
2513
2514 errout:
2515 if (me != NULL) {
2516 if (me->me_q.q_mcr.dma_map != NULL)
2517 ubsec_dma_free(sc, &me->me_q.q_mcr);
2518 if (me->me_q.q_ctx.dma_map != NULL) {
2519 memset(me->me_q.q_ctx.dma_vaddr, 0, me->me_q.q_ctx.dma_size);
2520 ubsec_dma_free(sc, &me->me_q.q_ctx);
2521 }
2522 if (me->me_M.dma_map != NULL) {
2523 memset(me->me_M.dma_vaddr, 0, me->me_M.dma_size);
2524 ubsec_dma_free(sc, &me->me_M);
2525 }
2526 if (me->me_E.dma_map != NULL) {
2527 memset(me->me_E.dma_vaddr, 0, me->me_E.dma_size);
2528 ubsec_dma_free(sc, &me->me_E);
2529 }
2530 if (me->me_C.dma_map != NULL) {
2531 memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
2532 ubsec_dma_free(sc, &me->me_C);
2533 }
2534 if (me->me_epb.dma_map != NULL)
2535 ubsec_dma_free(sc, &me->me_epb);
2536 free(me, M_DEVBUF);
2537 }
2538 krp->krp_status = err;
2539 crypto_kdone(krp);
2540 return (0);
2541 }
2542
2543 static int
2544 ubsec_kprocess_rsapriv(struct ubsec_softc *sc, struct cryptkop *krp,
2545 int hint)
2546 {
2547 struct ubsec_q2_rsapriv *rp = NULL;
2548 struct ubsec_mcr *mcr;
2549 struct ubsec_ctx_rsapriv *ctx;
2550 int err = 0;
2551 u_int padlen, msglen;
2552
2553 msglen = ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_P]);
2554 padlen = ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_Q]);
2555 if (msglen > padlen)
2556 padlen = msglen;
2557
2558 if (padlen <= 256)
2559 padlen = 256;
2560 else if (padlen <= 384)
2561 padlen = 384;
2562 else if (padlen <= 512)
2563 padlen = 512;
2564 else if (sc->sc_flags & UBS_FLAGS_BIGKEY && padlen <= 768)
2565 padlen = 768;
2566 else if (sc->sc_flags & UBS_FLAGS_BIGKEY && padlen <= 1024)
2567 padlen = 1024;
2568 else {
2569 err = E2BIG;
2570 goto errout;
2571 }
2572
2573 if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_DP]) > padlen) {
2574 err = E2BIG;
2575 goto errout;
2576 }
2577
2578 if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_DQ]) > padlen) {
2579 err = E2BIG;
2580 goto errout;
2581 }
2582
2583 if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_PINV]) > padlen) {
2584 err = E2BIG;
2585 goto errout;
2586 }
2587
2588 rp = malloc(sizeof *rp, M_DEVBUF, M_NOWAIT|M_ZERO);
2589 if (rp == NULL)
2590 return (ENOMEM);
2591 rp->rpr_krp = krp;
2592 rp->rpr_q.q_type = UBS_CTXOP_RSAPRIV;
2593
2594 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
2595 &rp->rpr_q.q_mcr, 0)) {
2596 err = ENOMEM;
2597 goto errout;
2598 }
2599 mcr = (struct ubsec_mcr *)rp->rpr_q.q_mcr.dma_vaddr;
2600
2601 if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_rsapriv),
2602 &rp->rpr_q.q_ctx, 0)) {
2603 err = ENOMEM;
2604 goto errout;
2605 }
2606 ctx = (struct ubsec_ctx_rsapriv *)rp->rpr_q.q_ctx.dma_vaddr;
2607 memset(ctx, 0, sizeof *ctx);
2608
2609 /* Copy in p */
2610 bcopy(krp->krp_param[UBS_RSAPRIV_PAR_P].crp_p,
2611 &ctx->rpr_buf[0 * (padlen / 8)],
2612 (krp->krp_param[UBS_RSAPRIV_PAR_P].crp_nbits + 7) / 8);
2613
2614 /* Copy in q */
2615 bcopy(krp->krp_param[UBS_RSAPRIV_PAR_Q].crp_p,
2616 &ctx->rpr_buf[1 * (padlen / 8)],
2617 (krp->krp_param[UBS_RSAPRIV_PAR_Q].crp_nbits + 7) / 8);
2618
2619 /* Copy in dp */
2620 bcopy(krp->krp_param[UBS_RSAPRIV_PAR_DP].crp_p,
2621 &ctx->rpr_buf[2 * (padlen / 8)],
2622 (krp->krp_param[UBS_RSAPRIV_PAR_DP].crp_nbits + 7) / 8);
2623
2624 /* Copy in dq */
2625 bcopy(krp->krp_param[UBS_RSAPRIV_PAR_DQ].crp_p,
2626 &ctx->rpr_buf[3 * (padlen / 8)],
2627 (krp->krp_param[UBS_RSAPRIV_PAR_DQ].crp_nbits + 7) / 8);
2628
2629 /* Copy in pinv */
2630 bcopy(krp->krp_param[UBS_RSAPRIV_PAR_PINV].crp_p,
2631 &ctx->rpr_buf[4 * (padlen / 8)],
2632 (krp->krp_param[UBS_RSAPRIV_PAR_PINV].crp_nbits + 7) / 8);
2633
2634 msglen = padlen * 2;
2635
2636 /* Copy in input message (aligned buffer/length). */
2637 if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_MSGIN]) > msglen) {
2638 /* Is this likely? */
2639 err = E2BIG;
2640 goto errout;
2641 }
2642 if (ubsec_dma_malloc(sc, (msglen + 7) / 8, &rp->rpr_msgin, 0)) {
2643 err = ENOMEM;
2644 goto errout;
2645 }
2646 memset(rp->rpr_msgin.dma_vaddr, 0, (msglen + 7) / 8);
2647 bcopy(krp->krp_param[UBS_RSAPRIV_PAR_MSGIN].crp_p,
2648 rp->rpr_msgin.dma_vaddr,
2649 (krp->krp_param[UBS_RSAPRIV_PAR_MSGIN].crp_nbits + 7) / 8);
2650
2651 /* Prepare space for output message (aligned buffer/length). */
2652 if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT]) < msglen) {
2653 /* Is this likely? */
2654 err = E2BIG;
2655 goto errout;
2656 }
2657 if (ubsec_dma_malloc(sc, (msglen + 7) / 8, &rp->rpr_msgout, 0)) {
2658 err = ENOMEM;
2659 goto errout;
2660 }
2661 memset(rp->rpr_msgout.dma_vaddr, 0, (msglen + 7) / 8);
2662
2663 mcr->mcr_pkts = htole16(1);
2664 mcr->mcr_flags = 0;
2665 mcr->mcr_cmdctxp = htole32(rp->rpr_q.q_ctx.dma_paddr);
2666 mcr->mcr_ipktbuf.pb_addr = htole32(rp->rpr_msgin.dma_paddr);
2667 mcr->mcr_ipktbuf.pb_next = 0;
2668 mcr->mcr_ipktbuf.pb_len = htole32(rp->rpr_msgin.dma_size);
2669 mcr->mcr_reserved = 0;
2670 mcr->mcr_pktlen = htole16(msglen);
2671 mcr->mcr_opktbuf.pb_addr = htole32(rp->rpr_msgout.dma_paddr);
2672 mcr->mcr_opktbuf.pb_next = 0;
2673 mcr->mcr_opktbuf.pb_len = htole32(rp->rpr_msgout.dma_size);
2674
2675 #ifdef DIAGNOSTIC
2676 if (rp->rpr_msgin.dma_paddr & 3 || rp->rpr_msgin.dma_size & 3) {
2677 panic("%s: rsapriv: invalid msgin 0x%lx(0x%lx)",
2678 device_xname(sc->sc_dev), (u_long) rp->rpr_msgin.dma_paddr,
2679 (u_long) rp->rpr_msgin.dma_size);
2680 }
2681 if (rp->rpr_msgout.dma_paddr & 3 || rp->rpr_msgout.dma_size & 3) {
2682 panic("%s: rsapriv: invalid msgout 0x%lx(0x%lx)",
2683 device_xname(sc->sc_dev), (u_long) rp->rpr_msgout.dma_paddr,
2684 (u_long) rp->rpr_msgout.dma_size);
2685 }
2686 #endif
2687
2688 ctx->rpr_len = (sizeof(u_int16_t) * 4) + (5 * (padlen / 8));
2689 ctx->rpr_op = htole16(UBS_CTXOP_RSAPRIV);
2690 ctx->rpr_q_len = htole16(padlen);
2691 ctx->rpr_p_len = htole16(padlen);
2692
2693 /*
2694 * ubsec_feed2 will sync mcr and ctx, we just need to sync
2695 * everything else.
2696 */
2697 bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgin.dma_map,
2698 0, rp->rpr_msgin.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2699 bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgout.dma_map,
2700 0, rp->rpr_msgout.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
2701
2702 /* Enqueue and we're done... */
2703 mutex_spin_enter(&sc->sc_mtx);
2704 SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &rp->rpr_q, q_next);
2705 ubsec_feed2(sc);
2706 ubsecstats.hst_modexpcrt++;
2707 mutex_spin_exit(&sc->sc_mtx);
2708 return (0);
2709
2710 errout:
2711 if (rp != NULL) {
2712 if (rp->rpr_q.q_mcr.dma_map != NULL)
2713 ubsec_dma_free(sc, &rp->rpr_q.q_mcr);
2714 if (rp->rpr_msgin.dma_map != NULL) {
2715 memset(rp->rpr_msgin.dma_vaddr, 0, rp->rpr_msgin.dma_size);
2716 ubsec_dma_free(sc, &rp->rpr_msgin);
2717 }
2718 if (rp->rpr_msgout.dma_map != NULL) {
2719 memset(rp->rpr_msgout.dma_vaddr, 0, rp->rpr_msgout.dma_size);
2720 ubsec_dma_free(sc, &rp->rpr_msgout);
2721 }
2722 free(rp, M_DEVBUF);
2723 }
2724 krp->krp_status = err;
2725 crypto_kdone(krp);
2726 return (0);
2727 }
2728
2729 #ifdef UBSEC_DEBUG
2730 static void
2731 ubsec_dump_pb(volatile struct ubsec_pktbuf *pb)
2732 {
2733 printf("addr 0x%x (0x%x) next 0x%x\n",
2734 pb->pb_addr, pb->pb_len, pb->pb_next);
2735 }
2736
2737 static void
2738 ubsec_dump_ctx2(volatile struct ubsec_ctx_keyop *c)
2739 {
2740 printf("CTX (0x%x):\n", c->ctx_len);
2741 switch (letoh16(c->ctx_op)) {
2742 case UBS_CTXOP_RNGBYPASS:
2743 case UBS_CTXOP_RNGSHA1:
2744 break;
2745 case UBS_CTXOP_MODEXP:
2746 {
2747 struct ubsec_ctx_modexp *cx = (void *)c;
2748 int i, len;
2749
2750 printf(" Elen %u, Nlen %u\n",
2751 letoh16(cx->me_E_len), letoh16(cx->me_N_len));
2752 len = (cx->me_N_len + 7)/8;
2753 for (i = 0; i < len; i++)
2754 printf("%s%02x", (i == 0) ? " N: " : ":", cx->me_N[i]);
2755 printf("\n");
2756 break;
2757 }
2758 default:
2759 printf("unknown context: %x\n", c->ctx_op);
2760 }
2761 printf("END CTX\n");
2762 }
2763
2764 static void
2765 ubsec_dump_mcr(struct ubsec_mcr *mcr)
2766 {
2767 volatile struct ubsec_mcr_add *ma;
2768 int i;
2769
2770 printf("MCR:\n");
2771 printf(" pkts: %u, flags 0x%x\n",
2772 letoh16(mcr->mcr_pkts), letoh16(mcr->mcr_flags));
2773 ma = (volatile struct ubsec_mcr_add *)&mcr->mcr_cmdctxp;
2774 for (i = 0; i < letoh16(mcr->mcr_pkts); i++) {
2775 printf(" %d: ctx 0x%x len 0x%x rsvd 0x%x\n", i,
2776 letoh32(ma->mcr_cmdctxp), letoh16(ma->mcr_pktlen),
2777 letoh16(ma->mcr_reserved));
2778 printf(" %d: ipkt ", i);
2779 ubsec_dump_pb(&ma->mcr_ipktbuf);
2780 printf(" %d: opkt ", i);
2781 ubsec_dump_pb(&ma->mcr_opktbuf);
2782 ma++;
2783 }
2784 printf("END MCR\n");
2785 }
2786 #endif /* UBSEC_DEBUG */
2787
2788 /*
2789 * Return the number of significant bits of a big number.
2790 */
2791 static int
2792 ubsec_ksigbits(struct crparam *cr)
2793 {
2794 u_int plen = (cr->crp_nbits + 7) / 8;
2795 int i, sig = plen * 8;
2796 u_int8_t c, *p = cr->crp_p;
2797
2798 for (i = plen - 1; i >= 0; i--) {
2799 c = p[i];
2800 if (c != 0) {
2801 while ((c & 0x80) == 0) {
2802 sig--;
2803 c <<= 1;
2804 }
2805 break;
2806 }
2807 sig -= 8;
2808 }
2809 return (sig);
2810 }
2811
2812 static void
2813 ubsec_kshift_r(u_int shiftbits, u_int8_t *src, u_int srcbits,
2814 u_int8_t *dst, u_int dstbits)
2815 {
2816 u_int slen, dlen;
2817 int i, si, di, n;
2818
2819 slen = (srcbits + 7) / 8;
2820 dlen = (dstbits + 7) / 8;
2821
2822 for (i = 0; i < slen; i++)
2823 dst[i] = src[i];
2824 for (i = 0; i < dlen - slen; i++)
2825 dst[slen + i] = 0;
2826
2827 n = shiftbits / 8;
2828 if (n != 0) {
2829 si = dlen - n - 1;
2830 di = dlen - 1;
2831 while (si >= 0)
2832 dst[di--] = dst[si--];
2833 while (di >= 0)
2834 dst[di--] = 0;
2835 }
2836
2837 n = shiftbits % 8;
2838 if (n != 0) {
2839 for (i = dlen - 1; i > 0; i--)
2840 dst[i] = (dst[i] << n) |
2841 (dst[i - 1] >> (8 - n));
2842 dst[0] = dst[0] << n;
2843 }
2844 }
2845
2846 static void
2847 ubsec_kshift_l(u_int shiftbits, u_int8_t *src, u_int srcbits,
2848 u_int8_t *dst, u_int dstbits)
2849 {
2850 int slen, dlen, i, n;
2851
2852 slen = (srcbits + 7) / 8;
2853 dlen = (dstbits + 7) / 8;
2854
2855 n = shiftbits / 8;
2856 for (i = 0; i < slen; i++)
2857 dst[i] = src[i + n];
2858 for (i = 0; i < dlen - slen; i++)
2859 dst[slen + i] = 0;
2860
2861 n = shiftbits % 8;
2862 if (n != 0) {
2863 for (i = 0; i < (dlen - 1); i++)
2864 dst[i] = (dst[i] >> n) | (dst[i + 1] << (8 - n));
2865 dst[dlen - 1] = dst[dlen - 1] >> n;
2866 }
2867 }
2868