summaryrefslogtreecommitdiff
path: root/sys/dev/pci/nofn.c
blob: e8b21a6aba428cffc4d2e710e2d70dd6f06706d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*	$OpenBSD: nofn.c,v 1.17 2010/04/06 22:28:07 tedu Exp $	*/

/*
 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Effort sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
 *
 */

/*
 * Driver for the Hifn 7814/7851/7854 HIPP1 processor.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/timeout.h>

#include <crypto/cryptodev.h>
#include <crypto/cryptosoft.h>
#include <dev/rndvar.h>
#include <crypto/md5.h>
#include <crypto/sha1.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#include <dev/pci/nofnreg.h>
#include <dev/pci/nofnvar.h>

int nofn_match(struct device *, void *, void *);
void nofn_attach(struct device *, struct device *, void *);
int nofn_intr(void *);

void nofn_rng_enable(struct nofn_softc *);
void nofn_rng_disable(struct nofn_softc *);
void nofn_rng_tick(void *);
int nofn_rng_intr(struct nofn_softc *);
int nofn_rng_read(struct nofn_softc *);

int nofn_pk_process(struct cryptkop *);
void nofn_pk_enable(struct nofn_softc *);
void nofn_pk_feed(struct nofn_softc *);
struct nofn_softc *nofn_pk_find(struct cryptkop *);
void nofn_pk_write_reg(struct nofn_softc *, int, union nofn_pk_reg *);
void nofn_pk_read_reg(struct nofn_softc *, int, union nofn_pk_reg *);
void nofn_pk_zero_reg(struct nofn_softc *, int);
int nofn_modexp_start(struct nofn_softc *, struct nofn_pk_q *);
void nofn_modexp_finish(struct nofn_softc *, struct nofn_pk_q *);
int nofn_pk_sigbits(const u_int8_t *, u_int);

struct cfattach nofn_ca = {
	sizeof(struct nofn_softc), nofn_match, nofn_attach
};

struct cfdriver nofn_cd = {
	0, "nofn", DV_DULL
};

int
nofn_match(parent, match, aux)
	struct device *parent;
	void *match, *aux;
{
	struct pci_attach_args *pa = (struct pci_attach_args *)aux;

	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_HIFN &&
	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_HIFN_78XX)
		return (1);
	return (0);
}

void
nofn_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct nofn_softc *sc = (struct nofn_softc *)self;
	struct pci_attach_args *pa = aux;
	pci_chipset_tag_t pc = pa->pa_pc;
	pci_intr_handle_t ih;
	const char *intrstr = NULL;
	bus_size_t bar0size = 0, bar3size = 0;

	sc->sc_dmat = pa->pa_dmat;

	if (pci_mapreg_map(pa, NOFN_BAR0_REGS, PCI_MAPREG_TYPE_MEM, 0,
	    &sc->sc_st, &sc->sc_sh, NULL, &bar0size, 0)) {
		printf(": can't map bar0 regs\n");
		goto fail;
	}

	if (pci_intr_map(pa, &ih)) {
		printf(": couldn't map interrupt\n");
		bus_space_unmap(sc->sc_st, sc->sc_sh, bar0size);
		goto fail;
	}

	intrstr = pci_intr_string(pc, ih);
	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, nofn_intr, sc,
	    self->dv_xname);
	if (sc->sc_ih == NULL) {
		printf(": couldn't establish interrupt");
		if (intrstr != NULL)
			printf(" at %s", intrstr);
		printf("\n");
		goto fail;
	}

	sc->sc_revid = REG_READ_4(sc, NOFN_REVID);

	switch (sc->sc_revid) {
	case REVID_7814_7854_1:
	case REVID_8154_1:/* XXX ? */
	case REVID_8065_1:/* XXX ? */
	case REVID_8165_1:/* XXX ? */
		if (pci_mapreg_map(pa, NOFN_BAR3_PK, PCI_MAPREG_TYPE_MEM, 0,
		    &sc->sc_pk_t, &sc->sc_pk_h, NULL, &bar3size, 0)) {
			printf(": can't map bar3 regs\n");
			goto fail;
		}
		nofn_rng_enable(sc);
		nofn_pk_enable(sc);
		break;
	case REVID_7851_1:
	case REVID_7851_2:
		break;
	default:
		printf(": unknown revid %x\n", sc->sc_revid);
		break;
	}

	printf(":");
	if (sc->sc_flags & NOFN_FLAGS_PK)
		printf(" PK");
	if (sc->sc_flags & NOFN_FLAGS_RNG)
		printf(" RNG");
	printf(", %s\n", intrstr);

	REG_WRITE_4(sc, NOFN_PCI_INT_MASK, sc->sc_intrmask);

	return;

fail:
	if (bar3size != 0)
		bus_space_unmap(sc->sc_pk_t, sc->sc_pk_h, bar3size);
	if (bar0size != 0)
		bus_space_unmap(sc->sc_st, sc->sc_sh, bar0size);
}

int
nofn_intr(vsc)
	void *vsc;
{
	struct nofn_softc *sc = vsc;
	u_int32_t stat;
	int r = 0;

	stat = REG_READ_4(sc, NOFN_PCI_INT_STAT) & sc->sc_intrmask;

	if (stat & PCIINTSTAT_RNGRDY)
		r |= nofn_rng_intr(sc);

	if (stat & PCIINTSTAT_PK) {
		struct nofn_pk_q *q;
		u_int32_t sr;

		r = 1;
		sr = PK_READ_4(sc, NOFN_PK_SR);
		if (sr & PK_SR_DONE && sc->sc_pk_current != NULL) {
			q = sc->sc_pk_current;
			sc->sc_pk_current = NULL;
			q->q_finish(sc, q);
			free(q, M_DEVBUF);
			nofn_pk_feed(sc);
		}
	}

	return (r);
}

int
nofn_rng_read(sc)
	struct nofn_softc *sc;
{
	u_int32_t buf[8], reg;
	int ret = 0, i;

	for (;;) {
		reg = PK_READ_4(sc, NOFN_PK_SR);
		if (reg & PK_SR_UFLOW) {
			ret = -1;
			printf("%s: rng underflow, disabling.\n",
			    sc->sc_dev.dv_xname);
			nofn_rng_disable(sc);
			break;
		}

		if ((reg & PK_SR_RRDY) == 0)
			break;

		ret = 1;
		bus_space_read_region_4(sc->sc_pk_t, sc->sc_pk_h,
		    NOFN_PK_RNGFIFO_BEGIN, buf, 8);
		if (sc->sc_rngskip > 0)
			sc->sc_rngskip -= 8;
		else
			for (i = 0; i < 8; i++)
				add_true_randomness(buf[i]);
	}

	return (ret);
}

int
nofn_rng_intr(sc)
	struct nofn_softc *sc;
{
	int r;

	r = nofn_rng_read(sc);
	if (r == 0)
		return (0);
	return (1);
}

void
nofn_rng_tick(vsc)
	void *vsc;
{
	struct nofn_softc *sc = vsc;
	int s, r;

	s = splnet();
	r = nofn_rng_read(sc);
	if (r != -1)
		timeout_add(&sc->sc_rngto, sc->sc_rngtick);
	splx(s);
}

void
nofn_rng_disable(sc)
	struct nofn_softc *sc;
{
	u_int32_t r;

	/* disable rng unit */
	r = PK_READ_4(sc, NOFN_PK_CFG2);
	r &= PK_CFG2_ALU_ENA; /* preserve */
	PK_WRITE_4(sc, NOFN_PK_CFG2, r);

	switch (sc->sc_revid) {
	case REVID_7814_7854_1:
		timeout_del(&sc->sc_rngto);
		break;
	case REVID_8154_1:
	case REVID_8065_1:
	case REVID_8165_1:
		/* disable rng interrupts */
		r = PK_READ_4(sc, NOFN_PK_IER);
		r &= PK_IER_DONE; /* preserve */
		PK_WRITE_4(sc, NOFN_PK_IER, r);

		sc->sc_intrmask &= ~PCIINTMASK_RNGRDY;
		REG_WRITE_4(sc, NOFN_PCI_INT_MASK, sc->sc_intrmask);
		break;
	default:
		printf("%s: nofn_rng_disable: unknown rev %x\n", 
		    sc->sc_dev.dv_xname, sc->sc_revid);
		break;
	}

	sc->sc_flags &= ~NOFN_FLAGS_RNG;
}

void
nofn_rng_enable(sc)
	struct nofn_softc *sc;
{
	u_int32_t r;

	/* setup scalar */
	PK_WRITE_4(sc, NOFN_PK_RNC, PK_RNC_SCALER);

	/* enable rng unit */
	r = PK_READ_4(sc, NOFN_PK_CFG2);
	r &= PK_CFG2_ALU_ENA; /* preserve */
	r |= PK_CFG2_RNG_ENA;
	PK_WRITE_4(sc, NOFN_PK_CFG2, r);

	/* 78xx chips cannot use interrupts to gather rng's */
	switch (sc->sc_revid) {
	case REVID_7814_7854_1:
		timeout_set(&sc->sc_rngto, nofn_rng_tick, sc);
		if (hz < 100)
			sc->sc_rngtick = 1;
		else
			sc->sc_rngtick = hz / 100;
		timeout_add(&sc->sc_rngto, sc->sc_rngtick);
		break;
	case REVID_8154_1:
	case REVID_8065_1:
	case REVID_8165_1:
		/* enable rng interrupts */
		r = PK_READ_4(sc, NOFN_PK_IER);
		r &= PK_IER_DONE; /* preserve */
		r |= PK_IER_RRDY;
		PK_WRITE_4(sc, NOFN_PK_IER, r);
		sc->sc_intrmask |= PCIINTMASK_RNGRDY;
		break;
	default:
		printf("%s: nofn_rng_enable: unknown rev %x\n", 
		    sc->sc_dev.dv_xname, sc->sc_revid);
		break;
	}

	sc->sc_flags |= NOFN_FLAGS_RNG;
}

void
nofn_pk_enable(sc)
	struct nofn_softc *sc;
{
	u_int32_t r;
	int algs[CRK_ALGORITHM_MAX + 1];

	if ((sc->sc_cid = crypto_get_driverid(0)) < 0) {
		printf(": failed to register cid\n");
		return;
	}

	SIMPLEQ_INIT(&sc->sc_pk_queue);
	sc->sc_pk_current = NULL;

	bzero(algs, sizeof(algs));
	algs[CRK_MOD_EXP] = CRYPTO_ALG_FLAG_SUPPORTED;
	crypto_kregister(sc->sc_cid, algs, nofn_pk_process);

	/* enable ALU */
	r = PK_READ_4(sc, NOFN_PK_CFG2);
	r &= PK_CFG2_RNG_ENA; /* preserve */
	r |= PK_CFG2_ALU_ENA;
	PK_WRITE_4(sc, NOFN_PK_CFG2, r);

	sc->sc_intrmask |= PCIINTMASK_PK;
	sc->sc_flags |= NOFN_FLAGS_PK;
}

void
nofn_pk_feed(sc)
	struct nofn_softc *sc;
{
	struct nofn_pk_q *q;
	u_int32_t r;

	/* Queue is empty and nothing being processed, turn off interrupt */
	if (SIMPLEQ_EMPTY(&sc->sc_pk_queue) &&
	    sc->sc_pk_current == NULL) {
		r = PK_READ_4(sc, NOFN_PK_IER);
		r &= PK_IER_RRDY; /* preserve */
		PK_WRITE_4(sc, NOFN_PK_IER, r);
		return;
	}

	/* Operation already pending, wait. */
	if (sc->sc_pk_current != NULL)
		return;

	while (!SIMPLEQ_EMPTY(&sc->sc_pk_queue)) {
		q = SIMPLEQ_FIRST(&sc->sc_pk_queue);
		if (q->q_start(sc, q) == 0) {
			sc->sc_pk_current = q;
			SIMPLEQ_REMOVE_HEAD(&sc->sc_pk_queue, q_next);

			r = PK_READ_4(sc, NOFN_PK_IER);
			r &= PK_IER_RRDY; /* preserve */
			r |= PK_IER_DONE;
			PK_WRITE_4(sc, NOFN_PK_IER, r);
			break;
		} else {
			SIMPLEQ_REMOVE_HEAD(&sc->sc_pk_queue, q_next);
			free(q, M_DEVBUF);
		}
	}
}

int
nofn_pk_process(krp)
	struct cryptkop *krp;
{
	struct nofn_softc *sc;
	struct nofn_pk_q *q;
	int s;

	if (krp == NULL || krp->krp_callback == NULL)
		return (EINVAL);
	if ((sc = nofn_pk_find(krp)) == NULL) {
		krp->krp_status = EINVAL;
		crypto_kdone(krp);
		return (0);
	}

	q = (struct nofn_pk_q *)malloc(sizeof(*q), M_DEVBUF, M_NOWAIT);
	if (q == NULL) {
		krp->krp_status = ENOMEM;
		crypto_kdone(krp);
		return (0);
	}

	switch (krp->krp_op) {
	case CRK_MOD_EXP:
		q->q_start = nofn_modexp_start;
		q->q_finish = nofn_modexp_finish;
		q->q_krp = krp;
		s = splnet();
		SIMPLEQ_INSERT_TAIL(&sc->sc_pk_queue, q, q_next);
		nofn_pk_feed(sc);
		splx(s);
		return (0);
	default:
		printf("%s: kprocess: invalid op 0x%x\n",
		    sc->sc_dev.dv_xname, krp->krp_op);
		krp->krp_status = EOPNOTSUPP;
		crypto_kdone(krp);
		free(q, M_DEVBUF);
		return (0);
	}
}

struct nofn_softc *
nofn_pk_find(krp)
	struct cryptkop *krp;
{
	struct nofn_softc *sc;
	int i;

	for (i = 0; i < nofn_cd.cd_ndevs; i++) {
		sc = nofn_cd.cd_devs[i];
		if (sc == NULL)
			continue;
		if (sc->sc_cid == krp->krp_hid)
			return (sc);
	}
	return (NULL);
}

void
nofn_pk_read_reg(sc, ridx, rp)
	struct nofn_softc *sc;
	int ridx;
	union nofn_pk_reg *rp;
{
#if BYTE_ORDER == BIG_ENDIAN
	bus_space_read_region_4(sc->sc_pk_t, sc->sc_pk_h,
	    NOFN_PK_REGADDR(NOFN_PK_WIN_0, ridx, 0), rp->w, 1024/32);
#else
	bus_space_read_region_4(sc->sc_pk_t, sc->sc_pk_h,
	    NOFN_PK_REGADDR(NOFN_PK_WIN_2, ridx, 0), rp->w, 1024/32);
#endif
}

void
nofn_pk_write_reg(sc, ridx, rp)
	struct nofn_softc *sc;
	int ridx;
	union nofn_pk_reg *rp;
{
#if BYTE_ORDER == BIG_ENDIAN
	bus_space_write_region_4(sc->sc_pk_t, sc->sc_pk_h,
	    NOFN_PK_REGADDR(NOFN_PK_WIN_0, ridx, 0), rp->w, 1024/32);
#else
	bus_space_write_region_4(sc->sc_pk_t, sc->sc_pk_h,
	    NOFN_PK_REGADDR(NOFN_PK_WIN_2, ridx, 0), rp->w, 1024/32);
#endif
}

void
nofn_pk_zero_reg(sc, ridx)
	struct nofn_softc *sc;
	int ridx;
{
	nofn_pk_write_reg(sc, ridx, &sc->sc_pk_zero);
}

int
nofn_modexp_start(sc, q)
	struct nofn_softc *sc;
	struct nofn_pk_q *q;
{
	struct cryptkop *krp = q->q_krp;
	int ip = 0, err = 0;
	int mshift, eshift, nshift;
	int mbits, ebits, nbits;

	if (krp->krp_param[NOFN_MODEXP_PAR_M].crp_nbits > 1024) {
		err = ERANGE;
		goto errout;
	}

	/* Zero out registers. */
	nofn_pk_zero_reg(sc, 0);
	nofn_pk_zero_reg(sc, 1);
	nofn_pk_zero_reg(sc, 2);
	nofn_pk_zero_reg(sc, 3);

	/* Write out N... */
	nbits = nofn_pk_sigbits(krp->krp_param[NOFN_MODEXP_PAR_N].crp_p,
	    krp->krp_param[NOFN_MODEXP_PAR_N].crp_nbits);
	if (nbits > 1024) {
		err = E2BIG;
		goto errout;
	}
	if (nbits < 5) {
		err = ERANGE;
		goto errout;
	}
	bzero(&sc->sc_pk_tmp, sizeof(sc->sc_pk_tmp));
	bcopy(krp->krp_param[NOFN_MODEXP_PAR_N].crp_p, &sc->sc_pk_tmp,
	    (nbits + 7) / 8);
	nofn_pk_write_reg(sc, 2, &sc->sc_pk_tmp);

	nshift = 1024 - nbits;
	PK_WRITE_4(sc, NOFN_PK_LENADDR(2), 1024);
	if (nshift != 0) {
		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(0, PK_OPCODE_SL, 2, 2, nshift));
		ip += 4;

		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(0, PK_OPCODE_TAG, 2, 2, nbits));
		ip += 4;
	}

	/* Write out M... */
	mbits = nofn_pk_sigbits(krp->krp_param[NOFN_MODEXP_PAR_M].crp_p,
	    krp->krp_param[NOFN_MODEXP_PAR_M].crp_nbits);
	if (mbits > 1024 || mbits > nbits) {
		err = E2BIG;
		goto errout;
	}
	bzero(&sc->sc_pk_tmp, sizeof(sc->sc_pk_tmp));
	bcopy(krp->krp_param[NOFN_MODEXP_PAR_M].crp_p, &sc->sc_pk_tmp,
	    (mbits + 7) / 8);
	nofn_pk_write_reg(sc, 0, &sc->sc_pk_tmp);

	mshift = 1024 - nbits;
	PK_WRITE_4(sc, NOFN_PK_LENADDR(0), 1024);
	if (mshift != 0) {
		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(0, PK_OPCODE_SL, 0, 0, mshift));
		ip += 4;

		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(0, PK_OPCODE_TAG, 0, 0, nbits));
		ip += 4;
	}

	/* Write out E... */
	ebits = nofn_pk_sigbits(krp->krp_param[NOFN_MODEXP_PAR_E].crp_p,
	    krp->krp_param[NOFN_MODEXP_PAR_E].crp_nbits);
	if (ebits > 1024 || ebits > nbits) {
		err = E2BIG;
		goto errout;
	}
	if (ebits < 1) {
		err = ERANGE;
		goto errout;
	}
	bzero(&sc->sc_pk_tmp, sizeof(sc->sc_pk_tmp));
	bcopy(krp->krp_param[NOFN_MODEXP_PAR_E].crp_p, &sc->sc_pk_tmp,
	    (ebits + 7) / 8);
	nofn_pk_write_reg(sc, 1, &sc->sc_pk_tmp);

	eshift = 1024 - nbits;
	PK_WRITE_4(sc, NOFN_PK_LENADDR(1), 1024);
	if (eshift != 0) {
		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(0, PK_OPCODE_SL, 1, 1, eshift));
		ip += 4;

		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(0, PK_OPCODE_TAG, 1, 1, nbits));
		ip += 4;
	}

	if (nshift == 0) {
		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR(PK_OP_DONE, PK_OPCODE_MODEXP, 3, 0, 1, 2));
		ip += 4;
	} else {
		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR(0, PK_OPCODE_MODEXP, 3, 0, 1, 2));
		ip += 4;

		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(0, PK_OPCODE_SR, 3, 3, nshift));
		ip += 4;

		PK_WRITE_4(sc, NOFN_PK_INSTR_BEGIN + ip,
		    NOFN_PK_INSTR2(PK_OP_DONE, PK_OPCODE_TAG, 3, 3, nbits));
		ip += 4;
	}

	/* Start microprogram */
	PK_WRITE_4(sc, NOFN_PK_CR, 0 << PK_CR_OFFSET_S);

	return (0);

errout:
	bzero(&sc->sc_pk_tmp, sizeof(sc->sc_pk_tmp));
	nofn_pk_zero_reg(sc, 0);
	nofn_pk_zero_reg(sc, 1);
	nofn_pk_zero_reg(sc, 2);
	nofn_pk_zero_reg(sc, 3);
	krp->krp_status = err;
	crypto_kdone(krp);
	return (1);
}

void
nofn_modexp_finish(sc, q)
	struct nofn_softc *sc;
	struct nofn_pk_q *q;
{
	struct cryptkop *krp = q->q_krp;
	int reglen, crplen;

	nofn_pk_read_reg(sc, 3, &sc->sc_pk_tmp);

	reglen = ((PK_READ_4(sc, NOFN_PK_LENADDR(3)) & NOFN_PK_LENMASK) + 7)
	    / 8;
	crplen = (krp->krp_param[krp->krp_iparams].crp_nbits + 7) / 8;

	if (crplen <= reglen)
		bcopy(sc->sc_pk_tmp.b, krp->krp_param[krp->krp_iparams].crp_p,
		    reglen);
	else {
		bcopy(sc->sc_pk_tmp.b, krp->krp_param[krp->krp_iparams].crp_p,
		    reglen);
		bzero(krp->krp_param[krp->krp_iparams].crp_p + reglen,
		    crplen - reglen);
	}
	bzero(&sc->sc_pk_tmp, sizeof(sc->sc_pk_tmp));
	nofn_pk_zero_reg(sc, 0);
	nofn_pk_zero_reg(sc, 1);
	nofn_pk_zero_reg(sc, 2);
	nofn_pk_zero_reg(sc, 3);
	crypto_kdone(krp);
}

/*
 * Return the number of significant bits of a little endian big number.
 */
int
nofn_pk_sigbits(p, pbits)
	const u_int8_t *p;
	u_int pbits;
{
	u_int plen = (pbits + 7) / 8;
	int i, sig = plen * 8;
	u_int8_t c;

	for (i = plen - 1; i >= 0; i--) {
		c = p[i];
		if (c != 0) {
			while ((c & 0x80) == 0) {
				sig--;
				c <<= 1;
			}
			break;
		}
		sig -= 8;
	}
	return (sig);
}