1117845Ssam/*-
2117845Ssam * Copyright (c) 2003 Sam Leffler, Errno Consulting
3117845Ssam * Copyright (c) 2003 Global Technology Associates, Inc.
4117845Ssam * All rights reserved.
5117845Ssam *
6117845Ssam * Redistribution and use in source and binary forms, with or without
7117845Ssam * modification, are permitted provided that the following conditions
8117845Ssam * are met:
9117845Ssam * 1. Redistributions of source code must retain the above copyright
10117845Ssam *    notice, this list of conditions and the following disclaimer.
11117845Ssam * 2. Redistributions in binary form must reproduce the above copyright
12117845Ssam *    notice, this list of conditions and the following disclaimer in the
13117845Ssam *    documentation and/or other materials provided with the distribution.
14117845Ssam *
15117845Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16117845Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17117845Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18117845Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19117845Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20117845Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21117845Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22117845Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23117845Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24117845Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25117845Ssam * SUCH DAMAGE.
26117845Ssam */
27117845Ssam
28117845Ssam#include <sys/cdefs.h>
29117845Ssam__FBSDID("$FreeBSD: stable/10/sys/dev/safe/safe.c 314667 2017-03-04 13:03:31Z avg $");
30117845Ssam
31117845Ssam/*
32117845Ssam * SafeNet SafeXcel-1141 hardware crypto accelerator
33117845Ssam */
34117845Ssam#include "opt_safe.h"
35117845Ssam
36117845Ssam#include <sys/param.h>
37117845Ssam#include <sys/systm.h>
38117845Ssam#include <sys/proc.h>
39117845Ssam#include <sys/errno.h>
40117845Ssam#include <sys/malloc.h>
41117845Ssam#include <sys/kernel.h>
42117845Ssam#include <sys/mbuf.h>
43129879Sphk#include <sys/module.h>
44117845Ssam#include <sys/lock.h>
45117845Ssam#include <sys/mutex.h>
46117845Ssam#include <sys/sysctl.h>
47117845Ssam#include <sys/endian.h>
48117845Ssam
49117845Ssam#include <vm/vm.h>
50117845Ssam#include <vm/pmap.h>
51117845Ssam
52117845Ssam#include <machine/bus.h>
53117845Ssam#include <machine/resource.h>
54117845Ssam#include <sys/bus.h>
55117845Ssam#include <sys/rman.h>
56117845Ssam
57117845Ssam#include <crypto/sha1.h>
58117845Ssam#include <opencrypto/cryptodev.h>
59117845Ssam#include <opencrypto/cryptosoft.h>
60117845Ssam#include <sys/md5.h>
61117845Ssam#include <sys/random.h>
62167755Ssam#include <sys/kobj.h>
63117845Ssam
64167755Ssam#include "cryptodev_if.h"
65167755Ssam
66119287Simp#include <dev/pci/pcivar.h>
67119287Simp#include <dev/pci/pcireg.h>
68117845Ssam
69117845Ssam#ifdef SAFE_RNDTEST
70117845Ssam#include <dev/rndtest/rndtest.h>
71117845Ssam#endif
72117845Ssam#include <dev/safe/safereg.h>
73117845Ssam#include <dev/safe/safevar.h>
74117845Ssam
75117845Ssam#ifndef bswap32
76117845Ssam#define	bswap32	NTOHL
77117845Ssam#endif
78117845Ssam
79117845Ssam/*
80117845Ssam * Prototypes and count for the pci_device structure
81117845Ssam */
82117845Ssamstatic	int safe_probe(device_t);
83117845Ssamstatic	int safe_attach(device_t);
84117845Ssamstatic	int safe_detach(device_t);
85117845Ssamstatic	int safe_suspend(device_t);
86117845Ssamstatic	int safe_resume(device_t);
87188178Simpstatic	int safe_shutdown(device_t);
88117845Ssam
89167755Ssamstatic	int safe_newsession(device_t, u_int32_t *, struct cryptoini *);
90167755Ssamstatic	int safe_freesession(device_t, u_int64_t);
91167755Ssamstatic	int safe_process(device_t, struct cryptop *, int);
92167755Ssam
93117845Ssamstatic device_method_t safe_methods[] = {
94117845Ssam	/* Device interface */
95117845Ssam	DEVMETHOD(device_probe,		safe_probe),
96117845Ssam	DEVMETHOD(device_attach,	safe_attach),
97117845Ssam	DEVMETHOD(device_detach,	safe_detach),
98117845Ssam	DEVMETHOD(device_suspend,	safe_suspend),
99117845Ssam	DEVMETHOD(device_resume,	safe_resume),
100117845Ssam	DEVMETHOD(device_shutdown,	safe_shutdown),
101117845Ssam
102167755Ssam	/* crypto device methods */
103167755Ssam	DEVMETHOD(cryptodev_newsession,	safe_newsession),
104167755Ssam	DEVMETHOD(cryptodev_freesession,safe_freesession),
105167755Ssam	DEVMETHOD(cryptodev_process,	safe_process),
106167755Ssam
107227843Smarius	DEVMETHOD_END
108117845Ssam};
109117845Ssamstatic driver_t safe_driver = {
110117845Ssam	"safe",
111117845Ssam	safe_methods,
112117845Ssam	sizeof (struct safe_softc)
113117845Ssam};
114117845Ssamstatic devclass_t safe_devclass;
115117845Ssam
116117845SsamDRIVER_MODULE(safe, pci, safe_driver, safe_devclass, 0, 0);
117117845SsamMODULE_DEPEND(safe, crypto, 1, 1, 1);
118117845Ssam#ifdef SAFE_RNDTEST
119117845SsamMODULE_DEPEND(safe, rndtest, 1, 1, 1);
120117845Ssam#endif
121117845Ssam
122117845Ssamstatic	void safe_intr(void *);
123117845Ssamstatic	void safe_callback(struct safe_softc *, struct safe_ringentry *);
124117845Ssamstatic	void safe_feed(struct safe_softc *, struct safe_ringentry *);
125117845Ssamstatic	void safe_mcopy(struct mbuf *, struct mbuf *, u_int);
126117845Ssam#ifndef SAFE_NO_RNG
127117845Ssamstatic	void safe_rng_init(struct safe_softc *);
128117845Ssamstatic	void safe_rng(void *);
129117845Ssam#endif /* SAFE_NO_RNG */
130117845Ssamstatic	int safe_dma_malloc(struct safe_softc *, bus_size_t,
131117845Ssam	        struct safe_dma_alloc *, int);
132117845Ssam#define	safe_dma_sync(_dma, _flags) \
133117845Ssam	bus_dmamap_sync((_dma)->dma_tag, (_dma)->dma_map, (_flags))
134117845Ssamstatic	void safe_dma_free(struct safe_softc *, struct safe_dma_alloc *);
135117845Ssamstatic	int safe_dmamap_aligned(const struct safe_operand *);
136117845Ssamstatic	int safe_dmamap_uniform(const struct safe_operand *);
137117845Ssam
138117845Ssamstatic	void safe_reset_board(struct safe_softc *);
139117845Ssamstatic	void safe_init_board(struct safe_softc *);
140117845Ssamstatic	void safe_init_pciregs(device_t dev);
141117845Ssamstatic	void safe_cleanchip(struct safe_softc *);
142117845Ssamstatic	void safe_totalreset(struct safe_softc *);
143117845Ssam
144117845Ssamstatic	int safe_free_entry(struct safe_softc *, struct safe_ringentry *);
145117845Ssam
146227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, safe, CTLFLAG_RD, 0,
147227309Sed    "SafeNet driver parameters");
148117845Ssam
149117845Ssam#ifdef SAFE_DEBUG
150117845Ssamstatic	void safe_dump_dmastatus(struct safe_softc *, const char *);
151117845Ssamstatic	void safe_dump_ringstate(struct safe_softc *, const char *);
152117845Ssamstatic	void safe_dump_intrstate(struct safe_softc *, const char *);
153117845Ssamstatic	void safe_dump_request(struct safe_softc *, const char *,
154117845Ssam		struct safe_ringentry *);
155117845Ssam
156117845Ssamstatic	struct safe_softc *safec;		/* for use by hw.safe.dump */
157117845Ssam
158117845Ssamstatic	int safe_debug = 0;
159117845SsamSYSCTL_INT(_hw_safe, OID_AUTO, debug, CTLFLAG_RW, &safe_debug,
160117845Ssam	    0, "control debugging msgs");
161117845Ssam#define	DPRINTF(_x)	if (safe_debug) printf _x
162117845Ssam#else
163117845Ssam#define	DPRINTF(_x)
164117845Ssam#endif
165117845Ssam
166117845Ssam#define	READ_REG(sc,r) \
167117845Ssam	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (r))
168117845Ssam
169117845Ssam#define WRITE_REG(sc,reg,val) \
170117845Ssam	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, reg, val)
171117845Ssam
172117845Ssamstruct safe_stats safestats;
173117845SsamSYSCTL_STRUCT(_hw_safe, OID_AUTO, stats, CTLFLAG_RD, &safestats,
174117845Ssam	    safe_stats, "driver statistics");
175117845Ssam#ifndef SAFE_NO_RNG
176117845Ssamstatic	int safe_rnginterval = 1;		/* poll once a second */
177117845SsamSYSCTL_INT(_hw_safe, OID_AUTO, rnginterval, CTLFLAG_RW, &safe_rnginterval,
178117845Ssam	    0, "RNG polling interval (secs)");
179117845Ssamstatic	int safe_rngbufsize = 16;		/* 64 bytes each poll  */
180117845SsamSYSCTL_INT(_hw_safe, OID_AUTO, rngbufsize, CTLFLAG_RW, &safe_rngbufsize,
181117845Ssam	    0, "RNG polling buffer size (32-bit words)");
182117845Ssamstatic	int safe_rngmaxalarm = 8;		/* max alarms before reset */
183117845SsamSYSCTL_INT(_hw_safe, OID_AUTO, rngmaxalarm, CTLFLAG_RW, &safe_rngmaxalarm,
184117845Ssam	    0, "RNG max alarms before reset");
185117845Ssam#endif /* SAFE_NO_RNG */
186117845Ssam
187117845Ssamstatic int
188117845Ssamsafe_probe(device_t dev)
189117845Ssam{
190117845Ssam	if (pci_get_vendor(dev) == PCI_VENDOR_SAFENET &&
191117845Ssam	    pci_get_device(dev) == PCI_PRODUCT_SAFEXCEL)
192142890Simp		return (BUS_PROBE_DEFAULT);
193117845Ssam	return (ENXIO);
194117845Ssam}
195117845Ssam
196117845Ssamstatic const char*
197117845Ssamsafe_partname(struct safe_softc *sc)
198117845Ssam{
199117845Ssam	/* XXX sprintf numbers when not decoded */
200117845Ssam	switch (pci_get_vendor(sc->sc_dev)) {
201117845Ssam	case PCI_VENDOR_SAFENET:
202117845Ssam		switch (pci_get_device(sc->sc_dev)) {
203117845Ssam		case PCI_PRODUCT_SAFEXCEL: return "SafeNet SafeXcel-1141";
204117845Ssam		}
205117845Ssam		return "SafeNet unknown-part";
206117845Ssam	}
207117845Ssam	return "Unknown-vendor unknown-part";
208117845Ssam}
209117845Ssam
210117845Ssam#ifndef SAFE_NO_RNG
211117845Ssamstatic void
212117845Ssamdefault_harvest(struct rndtest_state *rsp, void *buf, u_int count)
213117845Ssam{
214256381Smarkm	random_harvest(buf, count, count*NBBY/2, RANDOM_PURE_SAFE);
215117845Ssam}
216117845Ssam#endif /* SAFE_NO_RNG */
217117845Ssam
218117845Ssamstatic int
219117845Ssamsafe_attach(device_t dev)
220117845Ssam{
221117845Ssam	struct safe_softc *sc = device_get_softc(dev);
222117845Ssam	u_int32_t raddr;
223254263Sscottl	u_int32_t i, devinfo;
224117845Ssam	int rid;
225117845Ssam
226117845Ssam	bzero(sc, sizeof (*sc));
227117845Ssam	sc->sc_dev = dev;
228117845Ssam
229117845Ssam	/* XXX handle power management */
230117845Ssam
231254263Sscottl	pci_enable_busmaster(dev);
232117845Ssam
233117845Ssam	/*
234117845Ssam	 * Setup memory-mapping of PCI registers.
235117845Ssam	 */
236117845Ssam	rid = BS_BAR;
237127135Snjl	sc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
238127135Snjl					   RF_ACTIVE);
239117845Ssam	if (sc->sc_sr == NULL) {
240117845Ssam		device_printf(dev, "cannot map register space\n");
241117845Ssam		goto bad;
242117845Ssam	}
243117845Ssam	sc->sc_st = rman_get_bustag(sc->sc_sr);
244117845Ssam	sc->sc_sh = rman_get_bushandle(sc->sc_sr);
245117845Ssam
246117845Ssam	/*
247117845Ssam	 * Arrange interrupt line.
248117845Ssam	 */
249117845Ssam	rid = 0;
250127135Snjl	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
251127135Snjl					    RF_SHAREABLE|RF_ACTIVE);
252117845Ssam	if (sc->sc_irq == NULL) {
253117845Ssam		device_printf(dev, "could not map interrupt\n");
254117845Ssam		goto bad1;
255117845Ssam	}
256117845Ssam	/*
257117845Ssam	 * NB: Network code assumes we are blocked with splimp()
258117845Ssam	 *     so make sure the IRQ is mapped appropriately.
259117845Ssam	 */
260117845Ssam	if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_NET | INTR_MPSAFE,
261166901Spiso			   NULL, safe_intr, sc, &sc->sc_ih)) {
262117845Ssam		device_printf(dev, "could not establish interrupt\n");
263117845Ssam		goto bad2;
264117845Ssam	}
265117845Ssam
266167755Ssam	sc->sc_cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
267117845Ssam	if (sc->sc_cid < 0) {
268117845Ssam		device_printf(dev, "could not get crypto driver id\n");
269117845Ssam		goto bad3;
270117845Ssam	}
271117845Ssam
272117845Ssam	sc->sc_chiprev = READ_REG(sc, SAFE_DEVINFO) &
273117845Ssam		(SAFE_DEVINFO_REV_MAJ | SAFE_DEVINFO_REV_MIN);
274117845Ssam
275117845Ssam	/*
276117845Ssam	 * Setup DMA descriptor area.
277117845Ssam	 */
278232874Sscottl	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
279117845Ssam			       1,			/* alignment */
280117845Ssam			       SAFE_DMA_BOUNDARY,	/* boundary */
281117845Ssam			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
282117845Ssam			       BUS_SPACE_MAXADDR,	/* highaddr */
283117845Ssam			       NULL, NULL,		/* filter, filterarg */
284117845Ssam			       SAFE_MAX_DMA,		/* maxsize */
285117845Ssam			       SAFE_MAX_PART,		/* nsegments */
286117845Ssam			       SAFE_MAX_SSIZE,		/* maxsegsize */
287117845Ssam			       BUS_DMA_ALLOCNOW,	/* flags */
288117845Ssam			       NULL, NULL,		/* locking */
289117845Ssam			       &sc->sc_srcdmat)) {
290117845Ssam		device_printf(dev, "cannot allocate DMA tag\n");
291117845Ssam		goto bad4;
292117845Ssam	}
293232874Sscottl	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
294173307Ssam			       1,			/* alignment */
295117845Ssam			       SAFE_MAX_DSIZE,		/* boundary */
296117845Ssam			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
297117845Ssam			       BUS_SPACE_MAXADDR,	/* highaddr */
298117845Ssam			       NULL, NULL,		/* filter, filterarg */
299117845Ssam			       SAFE_MAX_DMA,		/* maxsize */
300117845Ssam			       SAFE_MAX_PART,		/* nsegments */
301117845Ssam			       SAFE_MAX_DSIZE,		/* maxsegsize */
302117845Ssam			       BUS_DMA_ALLOCNOW,	/* flags */
303117845Ssam			       NULL, NULL,		/* locking */
304117845Ssam			       &sc->sc_dstdmat)) {
305117845Ssam		device_printf(dev, "cannot allocate DMA tag\n");
306117845Ssam		goto bad4;
307117845Ssam	}
308117845Ssam
309117845Ssam	/*
310117845Ssam	 * Allocate packet engine descriptors.
311117845Ssam	 */
312117845Ssam	if (safe_dma_malloc(sc,
313117845Ssam	    SAFE_MAX_NQUEUE * sizeof (struct safe_ringentry),
314117845Ssam	    &sc->sc_ringalloc, 0)) {
315117845Ssam		device_printf(dev, "cannot allocate PE descriptor ring\n");
316117845Ssam		bus_dma_tag_destroy(sc->sc_srcdmat);
317117845Ssam		goto bad4;
318117845Ssam	}
319117845Ssam	/*
320117845Ssam	 * Hookup the static portion of all our data structures.
321117845Ssam	 */
322117845Ssam	sc->sc_ring = (struct safe_ringentry *) sc->sc_ringalloc.dma_vaddr;
323117845Ssam	sc->sc_ringtop = sc->sc_ring + SAFE_MAX_NQUEUE;
324117845Ssam	sc->sc_front = sc->sc_ring;
325117845Ssam	sc->sc_back = sc->sc_ring;
326117845Ssam	raddr = sc->sc_ringalloc.dma_paddr;
327117845Ssam	bzero(sc->sc_ring, SAFE_MAX_NQUEUE * sizeof(struct safe_ringentry));
328117845Ssam	for (i = 0; i < SAFE_MAX_NQUEUE; i++) {
329117845Ssam		struct safe_ringentry *re = &sc->sc_ring[i];
330117845Ssam
331117845Ssam		re->re_desc.d_sa = raddr +
332117845Ssam			offsetof(struct safe_ringentry, re_sa);
333117845Ssam		re->re_sa.sa_staterec = raddr +
334117845Ssam			offsetof(struct safe_ringentry, re_sastate);
335117845Ssam
336117845Ssam		raddr += sizeof (struct safe_ringentry);
337117845Ssam	}
338117845Ssam	mtx_init(&sc->sc_ringmtx, device_get_nameunit(dev),
339117845Ssam		"packet engine ring", MTX_DEF);
340117845Ssam
341117845Ssam	/*
342117845Ssam	 * Allocate scatter and gather particle descriptors.
343117845Ssam	 */
344117845Ssam	if (safe_dma_malloc(sc, SAFE_TOTAL_SPART * sizeof (struct safe_pdesc),
345117845Ssam	    &sc->sc_spalloc, 0)) {
346117845Ssam		device_printf(dev, "cannot allocate source particle "
347117845Ssam			"descriptor ring\n");
348117845Ssam		mtx_destroy(&sc->sc_ringmtx);
349117845Ssam		safe_dma_free(sc, &sc->sc_ringalloc);
350117845Ssam		bus_dma_tag_destroy(sc->sc_srcdmat);
351117845Ssam		goto bad4;
352117845Ssam	}
353117845Ssam	sc->sc_spring = (struct safe_pdesc *) sc->sc_spalloc.dma_vaddr;
354117845Ssam	sc->sc_springtop = sc->sc_spring + SAFE_TOTAL_SPART;
355117845Ssam	sc->sc_spfree = sc->sc_spring;
356117845Ssam	bzero(sc->sc_spring, SAFE_TOTAL_SPART * sizeof(struct safe_pdesc));
357117845Ssam
358117845Ssam	if (safe_dma_malloc(sc, SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
359117845Ssam	    &sc->sc_dpalloc, 0)) {
360117845Ssam		device_printf(dev, "cannot allocate destination particle "
361117845Ssam			"descriptor ring\n");
362117845Ssam		mtx_destroy(&sc->sc_ringmtx);
363117845Ssam		safe_dma_free(sc, &sc->sc_spalloc);
364117845Ssam		safe_dma_free(sc, &sc->sc_ringalloc);
365117845Ssam		bus_dma_tag_destroy(sc->sc_dstdmat);
366117845Ssam		goto bad4;
367117845Ssam	}
368117845Ssam	sc->sc_dpring = (struct safe_pdesc *) sc->sc_dpalloc.dma_vaddr;
369117845Ssam	sc->sc_dpringtop = sc->sc_dpring + SAFE_TOTAL_DPART;
370117845Ssam	sc->sc_dpfree = sc->sc_dpring;
371117845Ssam	bzero(sc->sc_dpring, SAFE_TOTAL_DPART * sizeof(struct safe_pdesc));
372117845Ssam
373117845Ssam	device_printf(sc->sc_dev, "%s", safe_partname(sc));
374117845Ssam
375117845Ssam	devinfo = READ_REG(sc, SAFE_DEVINFO);
376117845Ssam	if (devinfo & SAFE_DEVINFO_RNG) {
377117845Ssam		sc->sc_flags |= SAFE_FLAGS_RNG;
378117845Ssam		printf(" rng");
379117845Ssam	}
380117845Ssam	if (devinfo & SAFE_DEVINFO_PKEY) {
381117845Ssam#if 0
382117845Ssam		printf(" key");
383117845Ssam		sc->sc_flags |= SAFE_FLAGS_KEY;
384167755Ssam		crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0);
385167755Ssam		crypto_kregister(sc->sc_cid, CRK_MOD_EXP_CRT, 0);
386117845Ssam#endif
387117845Ssam	}
388117845Ssam	if (devinfo & SAFE_DEVINFO_DES) {
389117845Ssam		printf(" des/3des");
390167755Ssam		crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
391167755Ssam		crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
392117845Ssam	}
393117845Ssam	if (devinfo & SAFE_DEVINFO_AES) {
394117845Ssam		printf(" aes");
395167755Ssam		crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
396117845Ssam	}
397117845Ssam	if (devinfo & SAFE_DEVINFO_MD5) {
398117845Ssam		printf(" md5");
399167755Ssam		crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
400117845Ssam	}
401117845Ssam	if (devinfo & SAFE_DEVINFO_SHA1) {
402117845Ssam		printf(" sha1");
403167755Ssam		crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
404117845Ssam	}
405117845Ssam	printf(" null");
406167755Ssam	crypto_register(sc->sc_cid, CRYPTO_NULL_CBC, 0, 0);
407167755Ssam	crypto_register(sc->sc_cid, CRYPTO_NULL_HMAC, 0, 0);
408117845Ssam	/* XXX other supported algorithms */
409117845Ssam	printf("\n");
410117845Ssam
411117845Ssam	safe_reset_board(sc);		/* reset h/w */
412117845Ssam	safe_init_pciregs(dev);		/* init pci settings */
413117845Ssam	safe_init_board(sc);		/* init h/w */
414117845Ssam
415117845Ssam#ifndef SAFE_NO_RNG
416117845Ssam	if (sc->sc_flags & SAFE_FLAGS_RNG) {
417117845Ssam#ifdef SAFE_RNDTEST
418117845Ssam		sc->sc_rndtest = rndtest_attach(dev);
419117845Ssam		if (sc->sc_rndtest)
420117845Ssam			sc->sc_harvest = rndtest_harvest;
421117845Ssam		else
422117845Ssam			sc->sc_harvest = default_harvest;
423117845Ssam#else
424117845Ssam		sc->sc_harvest = default_harvest;
425117845Ssam#endif
426117845Ssam		safe_rng_init(sc);
427117845Ssam
428314667Savg		callout_init(&sc->sc_rngto, 1);
429117845Ssam		callout_reset(&sc->sc_rngto, hz*safe_rnginterval, safe_rng, sc);
430117845Ssam	}
431117845Ssam#endif /* SAFE_NO_RNG */
432117845Ssam#ifdef SAFE_DEBUG
433117845Ssam	safec = sc;			/* for use by hw.safe.dump */
434117845Ssam#endif
435117845Ssam	return (0);
436117845Ssambad4:
437117845Ssam	crypto_unregister_all(sc->sc_cid);
438117845Ssambad3:
439117845Ssam	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
440117845Ssambad2:
441117845Ssam	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
442117845Ssambad1:
443117845Ssam	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
444117845Ssambad:
445117845Ssam	return (ENXIO);
446117845Ssam}
447117845Ssam
448117845Ssam/*
449117845Ssam * Detach a device that successfully probed.
450117845Ssam */
451117845Ssamstatic int
452117845Ssamsafe_detach(device_t dev)
453117845Ssam{
454117845Ssam	struct safe_softc *sc = device_get_softc(dev);
455117845Ssam
456117845Ssam	/* XXX wait/abort active ops */
457117845Ssam
458117845Ssam	WRITE_REG(sc, SAFE_HI_MASK, 0);		/* disable interrupts */
459117845Ssam
460117845Ssam	callout_stop(&sc->sc_rngto);
461117845Ssam
462117845Ssam	crypto_unregister_all(sc->sc_cid);
463117845Ssam
464117845Ssam#ifdef SAFE_RNDTEST
465117845Ssam	if (sc->sc_rndtest)
466117845Ssam		rndtest_detach(sc->sc_rndtest);
467117845Ssam#endif
468117845Ssam
469117845Ssam	safe_cleanchip(sc);
470117845Ssam	safe_dma_free(sc, &sc->sc_dpalloc);
471117845Ssam	safe_dma_free(sc, &sc->sc_spalloc);
472117845Ssam	mtx_destroy(&sc->sc_ringmtx);
473117845Ssam	safe_dma_free(sc, &sc->sc_ringalloc);
474117845Ssam
475117845Ssam	bus_generic_detach(dev);
476117845Ssam	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
477117845Ssam	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
478117845Ssam
479117845Ssam	bus_dma_tag_destroy(sc->sc_srcdmat);
480117845Ssam	bus_dma_tag_destroy(sc->sc_dstdmat);
481117845Ssam	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
482117845Ssam
483117845Ssam	return (0);
484117845Ssam}
485117845Ssam
486117845Ssam/*
487117845Ssam * Stop all chip i/o so that the kernel's probe routines don't
488117845Ssam * get confused by errant DMAs when rebooting.
489117845Ssam */
490188178Simpstatic int
491117845Ssamsafe_shutdown(device_t dev)
492117845Ssam{
493117845Ssam#ifdef notyet
494117845Ssam	safe_stop(device_get_softc(dev));
495117845Ssam#endif
496188178Simp	return (0);
497117845Ssam}
498117845Ssam
499117845Ssam/*
500117845Ssam * Device suspend routine.
501117845Ssam */
502117845Ssamstatic int
503117845Ssamsafe_suspend(device_t dev)
504117845Ssam{
505117845Ssam	struct safe_softc *sc = device_get_softc(dev);
506117845Ssam
507117845Ssam#ifdef notyet
508117845Ssam	/* XXX stop the device and save PCI settings */
509117845Ssam#endif
510117845Ssam	sc->sc_suspended = 1;
511117845Ssam
512117845Ssam	return (0);
513117845Ssam}
514117845Ssam
515117845Ssamstatic int
516117845Ssamsafe_resume(device_t dev)
517117845Ssam{
518117845Ssam	struct safe_softc *sc = device_get_softc(dev);
519117845Ssam
520117845Ssam#ifdef notyet
521117845Ssam	/* XXX retore PCI settings and start the device */
522117845Ssam#endif
523117845Ssam	sc->sc_suspended = 0;
524117845Ssam	return (0);
525117845Ssam}
526117845Ssam
527117845Ssam/*
528117845Ssam * SafeXcel Interrupt routine
529117845Ssam */
530117845Ssamstatic void
531117845Ssamsafe_intr(void *arg)
532117845Ssam{
533117845Ssam	struct safe_softc *sc = arg;
534117845Ssam	volatile u_int32_t stat;
535117845Ssam
536117845Ssam	stat = READ_REG(sc, SAFE_HM_STAT);
537117845Ssam	if (stat == 0)			/* shared irq, not for us */
538117845Ssam		return;
539117845Ssam
540117845Ssam	WRITE_REG(sc, SAFE_HI_CLR, stat);	/* IACK */
541117845Ssam
542117845Ssam	if ((stat & SAFE_INT_PE_DDONE)) {
543117845Ssam		/*
544117845Ssam		 * Descriptor(s) done; scan the ring and
545117845Ssam		 * process completed operations.
546117845Ssam		 */
547117845Ssam		mtx_lock(&sc->sc_ringmtx);
548117845Ssam		while (sc->sc_back != sc->sc_front) {
549117845Ssam			struct safe_ringentry *re = sc->sc_back;
550117845Ssam#ifdef SAFE_DEBUG
551117845Ssam			if (safe_debug) {
552117845Ssam				safe_dump_ringstate(sc, __func__);
553117845Ssam				safe_dump_request(sc, __func__, re);
554117845Ssam			}
555117845Ssam#endif
556117845Ssam			/*
557117845Ssam			 * safe_process marks ring entries that were allocated
558117845Ssam			 * but not used with a csr of zero.  This insures the
559117845Ssam			 * ring front pointer never needs to be set backwards
560117845Ssam			 * in the event that an entry is allocated but not used
561117845Ssam			 * because of a setup error.
562117845Ssam			 */
563117845Ssam			if (re->re_desc.d_csr != 0) {
564117845Ssam				if (!SAFE_PE_CSR_IS_DONE(re->re_desc.d_csr))
565117845Ssam					break;
566117845Ssam				if (!SAFE_PE_LEN_IS_DONE(re->re_desc.d_len))
567117845Ssam					break;
568117845Ssam				sc->sc_nqchip--;
569117845Ssam				safe_callback(sc, re);
570117845Ssam			}
571117845Ssam			if (++(sc->sc_back) == sc->sc_ringtop)
572117845Ssam				sc->sc_back = sc->sc_ring;
573117845Ssam		}
574117845Ssam		mtx_unlock(&sc->sc_ringmtx);
575117845Ssam	}
576117845Ssam
577117845Ssam	/*
578117845Ssam	 * Check to see if we got any DMA Error
579117845Ssam	 */
580117845Ssam	if (stat & SAFE_INT_PE_ERROR) {
581117845Ssam		DPRINTF(("dmaerr dmastat %08x\n",
582117845Ssam			READ_REG(sc, SAFE_PE_DMASTAT)));
583117845Ssam		safestats.st_dmaerr++;
584117845Ssam		safe_totalreset(sc);
585117845Ssam#if 0
586117845Ssam		safe_feed(sc);
587117845Ssam#endif
588117845Ssam	}
589117845Ssam
590117845Ssam	if (sc->sc_needwakeup) {		/* XXX check high watermark */
591117845Ssam		int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
592117845Ssam		DPRINTF(("%s: wakeup crypto %x\n", __func__,
593117845Ssam			sc->sc_needwakeup));
594117845Ssam		sc->sc_needwakeup &= ~wakeup;
595117845Ssam		crypto_unblock(sc->sc_cid, wakeup);
596117845Ssam	}
597117845Ssam}
598117845Ssam
599117845Ssam/*
600117845Ssam * safe_feed() - post a request to chip
601117845Ssam */
602117845Ssamstatic void
603117845Ssamsafe_feed(struct safe_softc *sc, struct safe_ringentry *re)
604117845Ssam{
605117845Ssam	bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_PREWRITE);
606117845Ssam	if (re->re_dst_map != NULL)
607117845Ssam		bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map,
608117845Ssam			BUS_DMASYNC_PREREAD);
609117845Ssam	/* XXX have no smaller granularity */
610117845Ssam	safe_dma_sync(&sc->sc_ringalloc,
611117845Ssam		BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
612117845Ssam	safe_dma_sync(&sc->sc_spalloc, BUS_DMASYNC_PREWRITE);
613117845Ssam	safe_dma_sync(&sc->sc_dpalloc, BUS_DMASYNC_PREWRITE);
614117845Ssam
615117845Ssam#ifdef SAFE_DEBUG
616117845Ssam	if (safe_debug) {
617117845Ssam		safe_dump_ringstate(sc, __func__);
618117845Ssam		safe_dump_request(sc, __func__, re);
619117845Ssam	}
620117845Ssam#endif
621117845Ssam	sc->sc_nqchip++;
622117845Ssam	if (sc->sc_nqchip > safestats.st_maxqchip)
623117845Ssam		safestats.st_maxqchip = sc->sc_nqchip;
624117845Ssam	/* poke h/w to check descriptor ring, any value can be written */
625117845Ssam	WRITE_REG(sc, SAFE_HI_RD_DESCR, 0);
626117845Ssam}
627117845Ssam
628159226Spjd#define	N(a)	(sizeof(a) / sizeof (a[0]))
629159226Spjdstatic void
630159226Spjdsafe_setup_enckey(struct safe_session *ses, caddr_t key)
631159226Spjd{
632159226Spjd	int i;
633159226Spjd
634159226Spjd	bcopy(key, ses->ses_key, ses->ses_klen / 8);
635159226Spjd
636159226Spjd	/* PE is little-endian, insure proper byte order */
637159226Spjd	for (i = 0; i < N(ses->ses_key); i++)
638159226Spjd		ses->ses_key[i] = htole32(ses->ses_key[i]);
639159226Spjd}
640159226Spjd
641159226Spjdstatic void
642159226Spjdsafe_setup_mackey(struct safe_session *ses, int algo, caddr_t key, int klen)
643159226Spjd{
644159226Spjd	MD5_CTX md5ctx;
645159226Spjd	SHA1_CTX sha1ctx;
646159226Spjd	int i;
647159226Spjd
648159226Spjd
649159226Spjd	for (i = 0; i < klen; i++)
650159226Spjd		key[i] ^= HMAC_IPAD_VAL;
651159226Spjd
652159226Spjd	if (algo == CRYPTO_MD5_HMAC) {
653159226Spjd		MD5Init(&md5ctx);
654159226Spjd		MD5Update(&md5ctx, key, klen);
655159232Spjd		MD5Update(&md5ctx, hmac_ipad_buffer, MD5_HMAC_BLOCK_LEN - klen);
656159226Spjd		bcopy(md5ctx.state, ses->ses_hminner, sizeof(md5ctx.state));
657159226Spjd	} else {
658159226Spjd		SHA1Init(&sha1ctx);
659159226Spjd		SHA1Update(&sha1ctx, key, klen);
660159232Spjd		SHA1Update(&sha1ctx, hmac_ipad_buffer,
661159232Spjd		    SHA1_HMAC_BLOCK_LEN - klen);
662159226Spjd		bcopy(sha1ctx.h.b32, ses->ses_hminner, sizeof(sha1ctx.h.b32));
663159226Spjd	}
664159226Spjd
665159226Spjd	for (i = 0; i < klen; i++)
666159226Spjd		key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
667159226Spjd
668159226Spjd	if (algo == CRYPTO_MD5_HMAC) {
669159226Spjd		MD5Init(&md5ctx);
670159226Spjd		MD5Update(&md5ctx, key, klen);
671159232Spjd		MD5Update(&md5ctx, hmac_opad_buffer, MD5_HMAC_BLOCK_LEN - klen);
672159226Spjd		bcopy(md5ctx.state, ses->ses_hmouter, sizeof(md5ctx.state));
673159226Spjd	} else {
674159226Spjd		SHA1Init(&sha1ctx);
675159226Spjd		SHA1Update(&sha1ctx, key, klen);
676159232Spjd		SHA1Update(&sha1ctx, hmac_opad_buffer,
677159232Spjd		    SHA1_HMAC_BLOCK_LEN - klen);
678159226Spjd		bcopy(sha1ctx.h.b32, ses->ses_hmouter, sizeof(sha1ctx.h.b32));
679159226Spjd	}
680159226Spjd
681159226Spjd	for (i = 0; i < klen; i++)
682159226Spjd		key[i] ^= HMAC_OPAD_VAL;
683159226Spjd
684159226Spjd	/* PE is little-endian, insure proper byte order */
685159226Spjd	for (i = 0; i < N(ses->ses_hminner); i++) {
686159226Spjd		ses->ses_hminner[i] = htole32(ses->ses_hminner[i]);
687159226Spjd		ses->ses_hmouter[i] = htole32(ses->ses_hmouter[i]);
688159226Spjd	}
689159226Spjd}
690159226Spjd#undef N
691159226Spjd
692117845Ssam/*
693117845Ssam * Allocate a new 'session' and return an encoded session id.  'sidp'
694117845Ssam * contains our registration id, and should contain an encoded session
695117845Ssam * id on successful allocation.
696117845Ssam */
697117845Ssamstatic int
698167755Ssamsafe_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
699117845Ssam{
700167755Ssam	struct safe_softc *sc = device_get_softc(dev);
701117845Ssam	struct cryptoini *c, *encini = NULL, *macini = NULL;
702117845Ssam	struct safe_session *ses = NULL;
703159226Spjd	int sesn;
704117845Ssam
705117845Ssam	if (sidp == NULL || cri == NULL || sc == NULL)
706117845Ssam		return (EINVAL);
707117845Ssam
708117845Ssam	for (c = cri; c != NULL; c = c->cri_next) {
709117845Ssam		if (c->cri_alg == CRYPTO_MD5_HMAC ||
710117845Ssam		    c->cri_alg == CRYPTO_SHA1_HMAC ||
711117845Ssam		    c->cri_alg == CRYPTO_NULL_HMAC) {
712117845Ssam			if (macini)
713117845Ssam				return (EINVAL);
714117845Ssam			macini = c;
715117845Ssam		} else if (c->cri_alg == CRYPTO_DES_CBC ||
716117845Ssam		    c->cri_alg == CRYPTO_3DES_CBC ||
717117845Ssam		    c->cri_alg == CRYPTO_AES_CBC ||
718117845Ssam		    c->cri_alg == CRYPTO_NULL_CBC) {
719117845Ssam			if (encini)
720117845Ssam				return (EINVAL);
721117845Ssam			encini = c;
722117845Ssam		} else
723117845Ssam			return (EINVAL);
724117845Ssam	}
725117845Ssam	if (encini == NULL && macini == NULL)
726117845Ssam		return (EINVAL);
727117845Ssam	if (encini) {			/* validate key length */
728117845Ssam		switch (encini->cri_alg) {
729117845Ssam		case CRYPTO_DES_CBC:
730117845Ssam			if (encini->cri_klen != 64)
731117845Ssam				return (EINVAL);
732117845Ssam			break;
733117845Ssam		case CRYPTO_3DES_CBC:
734117845Ssam			if (encini->cri_klen != 192)
735117845Ssam				return (EINVAL);
736117845Ssam			break;
737117845Ssam		case CRYPTO_AES_CBC:
738117845Ssam			if (encini->cri_klen != 128 &&
739117845Ssam			    encini->cri_klen != 192 &&
740117845Ssam			    encini->cri_klen != 256)
741117845Ssam				return (EINVAL);
742117845Ssam			break;
743117845Ssam		}
744117845Ssam	}
745117845Ssam
746117845Ssam	if (sc->sc_sessions == NULL) {
747117845Ssam		ses = sc->sc_sessions = (struct safe_session *)malloc(
748117845Ssam		    sizeof(struct safe_session), M_DEVBUF, M_NOWAIT);
749117845Ssam		if (ses == NULL)
750117845Ssam			return (ENOMEM);
751117845Ssam		sesn = 0;
752117845Ssam		sc->sc_nsessions = 1;
753117845Ssam	} else {
754117845Ssam		for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
755117845Ssam			if (sc->sc_sessions[sesn].ses_used == 0) {
756117845Ssam				ses = &sc->sc_sessions[sesn];
757117845Ssam				break;
758117845Ssam			}
759117845Ssam		}
760117845Ssam
761117845Ssam		if (ses == NULL) {
762117845Ssam			sesn = sc->sc_nsessions;
763117845Ssam			ses = (struct safe_session *)malloc((sesn + 1) *
764117845Ssam			    sizeof(struct safe_session), M_DEVBUF, M_NOWAIT);
765117845Ssam			if (ses == NULL)
766117845Ssam				return (ENOMEM);
767117845Ssam			bcopy(sc->sc_sessions, ses, sesn *
768117845Ssam			    sizeof(struct safe_session));
769117845Ssam			bzero(sc->sc_sessions, sesn *
770117845Ssam			    sizeof(struct safe_session));
771117845Ssam			free(sc->sc_sessions, M_DEVBUF);
772117845Ssam			sc->sc_sessions = ses;
773117845Ssam			ses = &sc->sc_sessions[sesn];
774117845Ssam			sc->sc_nsessions++;
775117845Ssam		}
776117845Ssam	}
777117845Ssam
778117845Ssam	bzero(ses, sizeof(struct safe_session));
779117845Ssam	ses->ses_used = 1;
780117845Ssam
781117845Ssam	if (encini) {
782117845Ssam		/* get an IV */
783117845Ssam		/* XXX may read fewer than requested */
784117845Ssam		read_random(ses->ses_iv, sizeof(ses->ses_iv));
785117845Ssam
786117845Ssam		ses->ses_klen = encini->cri_klen;
787159226Spjd		if (encini->cri_key != NULL)
788159226Spjd			safe_setup_enckey(ses, encini->cri_key);
789117845Ssam	}
790117845Ssam
791117845Ssam	if (macini) {
792158705Spjd		ses->ses_mlen = macini->cri_mlen;
793158705Spjd		if (ses->ses_mlen == 0) {
794158705Spjd			if (macini->cri_alg == CRYPTO_MD5_HMAC)
795159233Spjd				ses->ses_mlen = MD5_HASH_LEN;
796158705Spjd			else
797159233Spjd				ses->ses_mlen = SHA1_HASH_LEN;
798158705Spjd		}
799158705Spjd
800159226Spjd		if (macini->cri_key != NULL) {
801159226Spjd			safe_setup_mackey(ses, macini->cri_alg, macini->cri_key,
802117845Ssam			    macini->cri_klen / 8);
803117845Ssam		}
804117845Ssam	}
805117845Ssam
806117845Ssam	*sidp = SAFE_SID(device_get_unit(sc->sc_dev), sesn);
807117845Ssam	return (0);
808117845Ssam}
809117845Ssam
810117845Ssam/*
811117845Ssam * Deallocate a session.
812117845Ssam */
813117845Ssamstatic int
814167755Ssamsafe_freesession(device_t dev, u_int64_t tid)
815117845Ssam{
816167755Ssam	struct safe_softc *sc = device_get_softc(dev);
817117845Ssam	int session, ret;
818117845Ssam	u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
819117845Ssam
820117845Ssam	if (sc == NULL)
821117845Ssam		return (EINVAL);
822117845Ssam
823117845Ssam	session = SAFE_SESSION(sid);
824117845Ssam	if (session < sc->sc_nsessions) {
825117845Ssam		bzero(&sc->sc_sessions[session], sizeof(sc->sc_sessions[session]));
826117845Ssam		ret = 0;
827117845Ssam	} else
828117845Ssam		ret = EINVAL;
829117845Ssam	return (ret);
830117845Ssam}
831117845Ssam
832117845Ssamstatic void
833117845Ssamsafe_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, bus_size_t mapsize, int error)
834117845Ssam{
835117845Ssam	struct safe_operand *op = arg;
836117845Ssam
837117845Ssam	DPRINTF(("%s: mapsize %u nsegs %d error %d\n", __func__,
838117845Ssam		(u_int) mapsize, nsegs, error));
839117845Ssam	if (error != 0)
840117845Ssam		return;
841117845Ssam	op->mapsize = mapsize;
842117845Ssam	op->nsegs = nsegs;
843117845Ssam	bcopy(seg, op->segs, nsegs * sizeof (seg[0]));
844117845Ssam}
845117845Ssam
846117845Ssamstatic int
847167755Ssamsafe_process(device_t dev, struct cryptop *crp, int hint)
848117845Ssam{
849167755Ssam	struct safe_softc *sc = device_get_softc(dev);
850117845Ssam	int err = 0, i, nicealign, uniform;
851117845Ssam	struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
852117845Ssam	int bypass, oplen, ivsize;
853117845Ssam	caddr_t iv;
854117845Ssam	int16_t coffset;
855117845Ssam	struct safe_session *ses;
856117845Ssam	struct safe_ringentry *re;
857117845Ssam	struct safe_sarec *sa;
858117845Ssam	struct safe_pdesc *pd;
859117845Ssam	u_int32_t cmd0, cmd1, staterec;
860117845Ssam
861117845Ssam	if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
862117845Ssam		safestats.st_invalid++;
863117845Ssam		return (EINVAL);
864117845Ssam	}
865117845Ssam	if (SAFE_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
866117845Ssam		safestats.st_badsession++;
867117845Ssam		return (EINVAL);
868117845Ssam	}
869117845Ssam
870117845Ssam	mtx_lock(&sc->sc_ringmtx);
871117845Ssam	if (sc->sc_front == sc->sc_back && sc->sc_nqchip != 0) {
872117845Ssam		safestats.st_ringfull++;
873117845Ssam		sc->sc_needwakeup |= CRYPTO_SYMQ;
874117845Ssam		mtx_unlock(&sc->sc_ringmtx);
875117845Ssam		return (ERESTART);
876117845Ssam	}
877117845Ssam	re = sc->sc_front;
878117845Ssam
879117845Ssam	staterec = re->re_sa.sa_staterec;	/* save */
880117845Ssam	/* NB: zero everything but the PE descriptor */
881117845Ssam	bzero(&re->re_sa, sizeof(struct safe_ringentry) - sizeof(re->re_desc));
882117845Ssam	re->re_sa.sa_staterec = staterec;	/* restore */
883117845Ssam
884117845Ssam	re->re_crp = crp;
885117845Ssam	re->re_sesn = SAFE_SESSION(crp->crp_sid);
886117845Ssam
887117845Ssam	if (crp->crp_flags & CRYPTO_F_IMBUF) {
888117845Ssam		re->re_src_m = (struct mbuf *)crp->crp_buf;
889117845Ssam		re->re_dst_m = (struct mbuf *)crp->crp_buf;
890117845Ssam	} else if (crp->crp_flags & CRYPTO_F_IOV) {
891117845Ssam		re->re_src_io = (struct uio *)crp->crp_buf;
892117845Ssam		re->re_dst_io = (struct uio *)crp->crp_buf;
893117845Ssam	} else {
894117845Ssam		safestats.st_badflags++;
895117845Ssam		err = EINVAL;
896117845Ssam		goto errout;	/* XXX we don't handle contiguous blocks! */
897117845Ssam	}
898117845Ssam
899117845Ssam	sa = &re->re_sa;
900117845Ssam	ses = &sc->sc_sessions[re->re_sesn];
901117845Ssam
902117845Ssam	crd1 = crp->crp_desc;
903117845Ssam	if (crd1 == NULL) {
904117845Ssam		safestats.st_nodesc++;
905117845Ssam		err = EINVAL;
906117845Ssam		goto errout;
907117845Ssam	}
908117845Ssam	crd2 = crd1->crd_next;
909117845Ssam
910117845Ssam	cmd0 = SAFE_SA_CMD0_BASIC;		/* basic group operation */
911117845Ssam	cmd1 = 0;
912117845Ssam	if (crd2 == NULL) {
913117845Ssam		if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
914117845Ssam		    crd1->crd_alg == CRYPTO_SHA1_HMAC ||
915117845Ssam		    crd1->crd_alg == CRYPTO_NULL_HMAC) {
916117845Ssam			maccrd = crd1;
917117845Ssam			enccrd = NULL;
918117845Ssam			cmd0 |= SAFE_SA_CMD0_OP_HASH;
919117845Ssam		} else if (crd1->crd_alg == CRYPTO_DES_CBC ||
920117845Ssam		    crd1->crd_alg == CRYPTO_3DES_CBC ||
921117845Ssam		    crd1->crd_alg == CRYPTO_AES_CBC ||
922117845Ssam		    crd1->crd_alg == CRYPTO_NULL_CBC) {
923117845Ssam			maccrd = NULL;
924117845Ssam			enccrd = crd1;
925117845Ssam			cmd0 |= SAFE_SA_CMD0_OP_CRYPT;
926117845Ssam		} else {
927117845Ssam			safestats.st_badalg++;
928117845Ssam			err = EINVAL;
929117845Ssam			goto errout;
930117845Ssam		}
931117845Ssam	} else {
932117845Ssam		if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
933117845Ssam		    crd1->crd_alg == CRYPTO_SHA1_HMAC ||
934117845Ssam		    crd1->crd_alg == CRYPTO_NULL_HMAC) &&
935117845Ssam		    (crd2->crd_alg == CRYPTO_DES_CBC ||
936117845Ssam			crd2->crd_alg == CRYPTO_3DES_CBC ||
937117845Ssam		        crd2->crd_alg == CRYPTO_AES_CBC ||
938117845Ssam		        crd2->crd_alg == CRYPTO_NULL_CBC) &&
939117845Ssam		    ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
940117845Ssam			maccrd = crd1;
941117845Ssam			enccrd = crd2;
942117845Ssam		} else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
943117845Ssam		    crd1->crd_alg == CRYPTO_3DES_CBC ||
944117845Ssam		    crd1->crd_alg == CRYPTO_AES_CBC ||
945117845Ssam		    crd1->crd_alg == CRYPTO_NULL_CBC) &&
946117845Ssam		    (crd2->crd_alg == CRYPTO_MD5_HMAC ||
947117845Ssam			crd2->crd_alg == CRYPTO_SHA1_HMAC ||
948117845Ssam			crd2->crd_alg == CRYPTO_NULL_HMAC) &&
949117845Ssam		    (crd1->crd_flags & CRD_F_ENCRYPT)) {
950117845Ssam			enccrd = crd1;
951117845Ssam			maccrd = crd2;
952117845Ssam		} else {
953117845Ssam			safestats.st_badalg++;
954117845Ssam			err = EINVAL;
955117845Ssam			goto errout;
956117845Ssam		}
957117845Ssam		cmd0 |= SAFE_SA_CMD0_OP_BOTH;
958117845Ssam	}
959117845Ssam
960117845Ssam	if (enccrd) {
961159226Spjd		if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT)
962159226Spjd			safe_setup_enckey(ses, enccrd->crd_key);
963159226Spjd
964117845Ssam		if (enccrd->crd_alg == CRYPTO_DES_CBC) {
965117845Ssam			cmd0 |= SAFE_SA_CMD0_DES;
966117845Ssam			cmd1 |= SAFE_SA_CMD1_CBC;
967117845Ssam			ivsize = 2*sizeof(u_int32_t);
968117845Ssam		} else if (enccrd->crd_alg == CRYPTO_3DES_CBC) {
969117845Ssam			cmd0 |= SAFE_SA_CMD0_3DES;
970117845Ssam			cmd1 |= SAFE_SA_CMD1_CBC;
971117845Ssam			ivsize = 2*sizeof(u_int32_t);
972117845Ssam		} else if (enccrd->crd_alg == CRYPTO_AES_CBC) {
973117845Ssam			cmd0 |= SAFE_SA_CMD0_AES;
974117845Ssam			cmd1 |= SAFE_SA_CMD1_CBC;
975117845Ssam			if (ses->ses_klen == 128)
976117845Ssam			     cmd1 |=  SAFE_SA_CMD1_AES128;
977117845Ssam			else if (ses->ses_klen == 192)
978117845Ssam			     cmd1 |=  SAFE_SA_CMD1_AES192;
979117845Ssam			else
980117845Ssam			     cmd1 |=  SAFE_SA_CMD1_AES256;
981117845Ssam			ivsize = 4*sizeof(u_int32_t);
982117845Ssam		} else {
983117845Ssam			cmd0 |= SAFE_SA_CMD0_CRYPT_NULL;
984117845Ssam			ivsize = 0;
985117845Ssam		}
986117845Ssam
987117845Ssam		/*
988117845Ssam		 * Setup encrypt/decrypt state.  When using basic ops
989117845Ssam		 * we can't use an inline IV because hash/crypt offset
990117845Ssam		 * must be from the end of the IV to the start of the
991117845Ssam		 * crypt data and this leaves out the preceding header
992117845Ssam		 * from the hash calculation.  Instead we place the IV
993117845Ssam		 * in the state record and set the hash/crypt offset to
994117845Ssam		 * copy both the header+IV.
995117845Ssam		 */
996117845Ssam		if (enccrd->crd_flags & CRD_F_ENCRYPT) {
997117845Ssam			cmd0 |= SAFE_SA_CMD0_OUTBOUND;
998117845Ssam
999117845Ssam			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
1000117845Ssam				iv = enccrd->crd_iv;
1001117845Ssam			else
1002117845Ssam				iv = (caddr_t) ses->ses_iv;
1003117845Ssam			if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
1004159242Spjd				crypto_copyback(crp->crp_flags, crp->crp_buf,
1005159242Spjd				    enccrd->crd_inject, ivsize, iv);
1006117845Ssam			}
1007117845Ssam			bcopy(iv, re->re_sastate.sa_saved_iv, ivsize);
1008117845Ssam			cmd0 |= SAFE_SA_CMD0_IVLD_STATE | SAFE_SA_CMD0_SAVEIV;
1009117845Ssam			re->re_flags |= SAFE_QFLAGS_COPYOUTIV;
1010117845Ssam		} else {
1011117845Ssam			cmd0 |= SAFE_SA_CMD0_INBOUND;
1012117845Ssam
1013159242Spjd			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
1014117845Ssam				bcopy(enccrd->crd_iv,
1015117845Ssam					re->re_sastate.sa_saved_iv, ivsize);
1016159242Spjd			} else {
1017159242Spjd				crypto_copydata(crp->crp_flags, crp->crp_buf,
1018159242Spjd				    enccrd->crd_inject, ivsize,
1019159242Spjd				    (caddr_t)re->re_sastate.sa_saved_iv);
1020159242Spjd			}
1021117845Ssam			cmd0 |= SAFE_SA_CMD0_IVLD_STATE;
1022117845Ssam		}
1023117845Ssam		/*
1024117845Ssam		 * For basic encryption use the zero pad algorithm.
1025117845Ssam		 * This pads results to an 8-byte boundary and
1026117845Ssam		 * suppresses padding verification for inbound (i.e.
1027117845Ssam		 * decrypt) operations.
1028117845Ssam		 *
1029117845Ssam		 * NB: Not sure if the 8-byte pad boundary is a problem.
1030117845Ssam		 */
1031117845Ssam		cmd0 |= SAFE_SA_CMD0_PAD_ZERO;
1032117845Ssam
1033117845Ssam		/* XXX assert key bufs have the same size */
1034117845Ssam		bcopy(ses->ses_key, sa->sa_key, sizeof(sa->sa_key));
1035117845Ssam	}
1036117845Ssam
1037117845Ssam	if (maccrd) {
1038159226Spjd		if (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
1039159226Spjd			safe_setup_mackey(ses, maccrd->crd_alg,
1040159226Spjd			    maccrd->crd_key, maccrd->crd_klen / 8);
1041159226Spjd		}
1042159226Spjd
1043117845Ssam		if (maccrd->crd_alg == CRYPTO_MD5_HMAC) {
1044117845Ssam			cmd0 |= SAFE_SA_CMD0_MD5;
1045117845Ssam			cmd1 |= SAFE_SA_CMD1_HMAC;	/* NB: enable HMAC */
1046117845Ssam		} else if (maccrd->crd_alg == CRYPTO_SHA1_HMAC) {
1047117845Ssam			cmd0 |= SAFE_SA_CMD0_SHA1;
1048117845Ssam			cmd1 |= SAFE_SA_CMD1_HMAC;	/* NB: enable HMAC */
1049117845Ssam		} else {
1050117845Ssam			cmd0 |= SAFE_SA_CMD0_HASH_NULL;
1051117845Ssam		}
1052117845Ssam		/*
1053117845Ssam		 * Digest data is loaded from the SA and the hash
1054117845Ssam		 * result is saved to the state block where we
1055117845Ssam		 * retrieve it for return to the caller.
1056117845Ssam		 */
1057117845Ssam		/* XXX assert digest bufs have the same size */
1058117845Ssam		bcopy(ses->ses_hminner, sa->sa_indigest,
1059117845Ssam			sizeof(sa->sa_indigest));
1060117845Ssam		bcopy(ses->ses_hmouter, sa->sa_outdigest,
1061117845Ssam			sizeof(sa->sa_outdigest));
1062117845Ssam
1063117845Ssam		cmd0 |= SAFE_SA_CMD0_HSLD_SA | SAFE_SA_CMD0_SAVEHASH;
1064117845Ssam		re->re_flags |= SAFE_QFLAGS_COPYOUTICV;
1065117845Ssam	}
1066117845Ssam
1067117845Ssam	if (enccrd && maccrd) {
1068117845Ssam		/*
1069117845Ssam		 * The offset from hash data to the start of
1070117845Ssam		 * crypt data is the difference in the skips.
1071117845Ssam		 */
1072117845Ssam		bypass = maccrd->crd_skip;
1073117845Ssam		coffset = enccrd->crd_skip - maccrd->crd_skip;
1074117845Ssam		if (coffset < 0) {
1075117845Ssam			DPRINTF(("%s: hash does not precede crypt; "
1076117845Ssam				"mac skip %u enc skip %u\n",
1077117845Ssam				__func__, maccrd->crd_skip, enccrd->crd_skip));
1078117845Ssam			safestats.st_skipmismatch++;
1079117845Ssam			err = EINVAL;
1080117845Ssam			goto errout;
1081117845Ssam		}
1082117845Ssam		oplen = enccrd->crd_skip + enccrd->crd_len;
1083117845Ssam		if (maccrd->crd_skip + maccrd->crd_len != oplen) {
1084117845Ssam			DPRINTF(("%s: hash amount %u != crypt amount %u\n",
1085117845Ssam				__func__, maccrd->crd_skip + maccrd->crd_len,
1086117845Ssam				oplen));
1087117845Ssam			safestats.st_lenmismatch++;
1088117845Ssam			err = EINVAL;
1089117845Ssam			goto errout;
1090117845Ssam		}
1091117845Ssam#ifdef SAFE_DEBUG
1092117845Ssam		if (safe_debug) {
1093117845Ssam			printf("mac: skip %d, len %d, inject %d\n",
1094117845Ssam			    maccrd->crd_skip, maccrd->crd_len,
1095117845Ssam			    maccrd->crd_inject);
1096117845Ssam			printf("enc: skip %d, len %d, inject %d\n",
1097117845Ssam			    enccrd->crd_skip, enccrd->crd_len,
1098117845Ssam			    enccrd->crd_inject);
1099117845Ssam			printf("bypass %d coffset %d oplen %d\n",
1100117845Ssam				bypass, coffset, oplen);
1101117845Ssam		}
1102117845Ssam#endif
1103117845Ssam		if (coffset & 3) {	/* offset must be 32-bit aligned */
1104117845Ssam			DPRINTF(("%s: coffset %u misaligned\n",
1105117845Ssam				__func__, coffset));
1106117845Ssam			safestats.st_coffmisaligned++;
1107117845Ssam			err = EINVAL;
1108117845Ssam			goto errout;
1109117845Ssam		}
1110117845Ssam		coffset >>= 2;
1111117845Ssam		if (coffset > 255) {	/* offset must be <256 dwords */
1112117845Ssam			DPRINTF(("%s: coffset %u too big\n",
1113117845Ssam				__func__, coffset));
1114117845Ssam			safestats.st_cofftoobig++;
1115117845Ssam			err = EINVAL;
1116117845Ssam			goto errout;
1117117845Ssam		}
1118117845Ssam		/*
1119117845Ssam		 * Tell the hardware to copy the header to the output.
1120117845Ssam		 * The header is defined as the data from the end of
1121117845Ssam		 * the bypass to the start of data to be encrypted.
1122117845Ssam		 * Typically this is the inline IV.  Note that you need
1123117845Ssam		 * to do this even if src+dst are the same; it appears
1124117845Ssam		 * that w/o this bit the crypted data is written
1125117845Ssam		 * immediately after the bypass data.
1126117845Ssam		 */
1127117845Ssam		cmd1 |= SAFE_SA_CMD1_HDRCOPY;
1128117845Ssam		/*
1129117845Ssam		 * Disable IP header mutable bit handling.  This is
1130117845Ssam		 * needed to get correct HMAC calculations.
1131117845Ssam		 */
1132117845Ssam		cmd1 |= SAFE_SA_CMD1_MUTABLE;
1133117845Ssam	} else {
1134117845Ssam		if (enccrd) {
1135117845Ssam			bypass = enccrd->crd_skip;
1136117845Ssam			oplen = bypass + enccrd->crd_len;
1137117845Ssam		} else {
1138117845Ssam			bypass = maccrd->crd_skip;
1139117845Ssam			oplen = bypass + maccrd->crd_len;
1140117845Ssam		}
1141117845Ssam		coffset = 0;
1142117845Ssam	}
1143117845Ssam	/* XXX verify multiple of 4 when using s/g */
1144117845Ssam	if (bypass > 96) {		/* bypass offset must be <= 96 bytes */
1145117845Ssam		DPRINTF(("%s: bypass %u too big\n", __func__, bypass));
1146117845Ssam		safestats.st_bypasstoobig++;
1147117845Ssam		err = EINVAL;
1148117845Ssam		goto errout;
1149117845Ssam	}
1150117845Ssam
1151117845Ssam	if (bus_dmamap_create(sc->sc_srcdmat, BUS_DMA_NOWAIT, &re->re_src_map)) {
1152117845Ssam		safestats.st_nomap++;
1153117845Ssam		err = ENOMEM;
1154117845Ssam		goto errout;
1155117845Ssam	}
1156117845Ssam	if (crp->crp_flags & CRYPTO_F_IMBUF) {
1157117845Ssam		if (bus_dmamap_load_mbuf(sc->sc_srcdmat, re->re_src_map,
1158117845Ssam		    re->re_src_m, safe_op_cb,
1159117845Ssam		    &re->re_src, BUS_DMA_NOWAIT) != 0) {
1160117845Ssam			bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
1161117845Ssam			re->re_src_map = NULL;
1162117845Ssam			safestats.st_noload++;
1163117845Ssam			err = ENOMEM;
1164117845Ssam			goto errout;
1165117845Ssam		}
1166117845Ssam	} else if (crp->crp_flags & CRYPTO_F_IOV) {
1167117845Ssam		if (bus_dmamap_load_uio(sc->sc_srcdmat, re->re_src_map,
1168117845Ssam		    re->re_src_io, safe_op_cb,
1169117845Ssam		    &re->re_src, BUS_DMA_NOWAIT) != 0) {
1170117845Ssam			bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
1171117845Ssam			re->re_src_map = NULL;
1172117845Ssam			safestats.st_noload++;
1173117845Ssam			err = ENOMEM;
1174117845Ssam			goto errout;
1175117845Ssam		}
1176117845Ssam	}
1177117845Ssam	nicealign = safe_dmamap_aligned(&re->re_src);
1178117845Ssam	uniform = safe_dmamap_uniform(&re->re_src);
1179117845Ssam
1180117845Ssam	DPRINTF(("src nicealign %u uniform %u nsegs %u\n",
1181117845Ssam		nicealign, uniform, re->re_src.nsegs));
1182117845Ssam	if (re->re_src.nsegs > 1) {
1183117845Ssam		re->re_desc.d_src = sc->sc_spalloc.dma_paddr +
1184117845Ssam			((caddr_t) sc->sc_spfree - (caddr_t) sc->sc_spring);
1185117845Ssam		for (i = 0; i < re->re_src_nsegs; i++) {
1186117845Ssam			/* NB: no need to check if there's space */
1187117845Ssam			pd = sc->sc_spfree;
1188117845Ssam			if (++(sc->sc_spfree) == sc->sc_springtop)
1189117845Ssam				sc->sc_spfree = sc->sc_spring;
1190117845Ssam
1191117845Ssam			KASSERT((pd->pd_flags&3) == 0 ||
1192117845Ssam				(pd->pd_flags&3) == SAFE_PD_DONE,
1193117845Ssam				("bogus source particle descriptor; flags %x",
1194117845Ssam				pd->pd_flags));
1195117845Ssam			pd->pd_addr = re->re_src_segs[i].ds_addr;
1196117845Ssam			pd->pd_size = re->re_src_segs[i].ds_len;
1197117845Ssam			pd->pd_flags = SAFE_PD_READY;
1198117845Ssam		}
1199117845Ssam		cmd0 |= SAFE_SA_CMD0_IGATHER;
1200117845Ssam	} else {
1201117845Ssam		/*
1202117845Ssam		 * No need for gather, reference the operand directly.
1203117845Ssam		 */
1204117845Ssam		re->re_desc.d_src = re->re_src_segs[0].ds_addr;
1205117845Ssam	}
1206117845Ssam
1207117845Ssam	if (enccrd == NULL && maccrd != NULL) {
1208117845Ssam		/*
1209117845Ssam		 * Hash op; no destination needed.
1210117845Ssam		 */
1211117845Ssam	} else {
1212117845Ssam		if (crp->crp_flags & CRYPTO_F_IOV) {
1213117845Ssam			if (!nicealign) {
1214117845Ssam				safestats.st_iovmisaligned++;
1215117845Ssam				err = EINVAL;
1216117845Ssam				goto errout;
1217117845Ssam			}
1218117845Ssam			if (uniform != 1) {
1219117845Ssam				/*
1220117845Ssam				 * Source is not suitable for direct use as
1221117845Ssam				 * the destination.  Create a new scatter/gather
1222117845Ssam				 * list based on the destination requirements
1223117845Ssam				 * and check if that's ok.
1224117845Ssam				 */
1225117845Ssam				if (bus_dmamap_create(sc->sc_dstdmat,
1226117845Ssam				    BUS_DMA_NOWAIT, &re->re_dst_map)) {
1227117845Ssam					safestats.st_nomap++;
1228117845Ssam					err = ENOMEM;
1229117845Ssam					goto errout;
1230117845Ssam				}
1231117845Ssam				if (bus_dmamap_load_uio(sc->sc_dstdmat,
1232117845Ssam				    re->re_dst_map, re->re_dst_io,
1233117845Ssam				    safe_op_cb, &re->re_dst,
1234117845Ssam				    BUS_DMA_NOWAIT) != 0) {
1235117845Ssam					bus_dmamap_destroy(sc->sc_dstdmat,
1236117845Ssam						re->re_dst_map);
1237117845Ssam					re->re_dst_map = NULL;
1238117845Ssam					safestats.st_noload++;
1239117845Ssam					err = ENOMEM;
1240117845Ssam					goto errout;
1241117845Ssam				}
1242117845Ssam				uniform = safe_dmamap_uniform(&re->re_dst);
1243117845Ssam				if (!uniform) {
1244117845Ssam					/*
1245117845Ssam					 * There's no way to handle the DMA
1246117845Ssam					 * requirements with this uio.  We
1247117845Ssam					 * could create a separate DMA area for
1248117845Ssam					 * the result and then copy it back,
1249117845Ssam					 * but for now we just bail and return
1250117845Ssam					 * an error.  Note that uio requests
1251117845Ssam					 * > SAFE_MAX_DSIZE are handled because
1252117845Ssam					 * the DMA map and segment list for the
1253117845Ssam					 * destination wil result in a
1254117845Ssam					 * destination particle list that does
1255117845Ssam					 * the necessary scatter DMA.
1256117845Ssam					 */
1257117845Ssam					safestats.st_iovnotuniform++;
1258117845Ssam					err = EINVAL;
1259117845Ssam					goto errout;
1260117845Ssam				}
1261118882Ssam			} else
1262118882Ssam				re->re_dst = re->re_src;
1263117845Ssam		} else if (crp->crp_flags & CRYPTO_F_IMBUF) {
1264117845Ssam			if (nicealign && uniform == 1) {
1265117845Ssam				/*
1266117845Ssam				 * Source layout is suitable for direct
1267117845Ssam				 * sharing of the DMA map and segment list.
1268117845Ssam				 */
1269117845Ssam				re->re_dst = re->re_src;
1270117845Ssam			} else if (nicealign && uniform == 2) {
1271117845Ssam				/*
1272117845Ssam				 * The source is properly aligned but requires a
1273117845Ssam				 * different particle list to handle DMA of the
1274117845Ssam				 * result.  Create a new map and do the load to
1275117845Ssam				 * create the segment list.  The particle
1276117845Ssam				 * descriptor setup code below will handle the
1277117845Ssam				 * rest.
1278117845Ssam				 */
1279117845Ssam				if (bus_dmamap_create(sc->sc_dstdmat,
1280117845Ssam				    BUS_DMA_NOWAIT, &re->re_dst_map)) {
1281117845Ssam					safestats.st_nomap++;
1282117845Ssam					err = ENOMEM;
1283117845Ssam					goto errout;
1284117845Ssam				}
1285117845Ssam				if (bus_dmamap_load_mbuf(sc->sc_dstdmat,
1286117845Ssam				    re->re_dst_map, re->re_dst_m,
1287117845Ssam				    safe_op_cb, &re->re_dst,
1288117845Ssam				    BUS_DMA_NOWAIT) != 0) {
1289117845Ssam					bus_dmamap_destroy(sc->sc_dstdmat,
1290117845Ssam						re->re_dst_map);
1291117845Ssam					re->re_dst_map = NULL;
1292117845Ssam					safestats.st_noload++;
1293117845Ssam					err = ENOMEM;
1294117845Ssam					goto errout;
1295117845Ssam				}
1296117845Ssam			} else {		/* !(aligned and/or uniform) */
1297117845Ssam				int totlen, len;
1298117845Ssam				struct mbuf *m, *top, **mp;
1299117845Ssam
1300117845Ssam				/*
1301117845Ssam				 * DMA constraints require that we allocate a
1302117845Ssam				 * new mbuf chain for the destination.  We
1303117845Ssam				 * allocate an entire new set of mbufs of
1304117845Ssam				 * optimal/required size and then tell the
1305117845Ssam				 * hardware to copy any bits that are not
1306117845Ssam				 * created as a byproduct of the operation.
1307117845Ssam				 */
1308117845Ssam				if (!nicealign)
1309117845Ssam					safestats.st_unaligned++;
1310117845Ssam				if (!uniform)
1311117845Ssam					safestats.st_notuniform++;
1312117845Ssam				totlen = re->re_src_mapsize;
1313117845Ssam				if (re->re_src_m->m_flags & M_PKTHDR) {
1314117845Ssam					len = MHLEN;
1315243857Sglebius					MGETHDR(m, M_NOWAIT, MT_DATA);
1316117845Ssam					if (m && !m_dup_pkthdr(m, re->re_src_m,
1317243857Sglebius					    M_NOWAIT)) {
1318117845Ssam						m_free(m);
1319117845Ssam						m = NULL;
1320117845Ssam					}
1321117845Ssam				} else {
1322117845Ssam					len = MLEN;
1323243857Sglebius					MGET(m, M_NOWAIT, MT_DATA);
1324117845Ssam				}
1325117845Ssam				if (m == NULL) {
1326117845Ssam					safestats.st_nombuf++;
1327117845Ssam					err = sc->sc_nqchip ? ERESTART : ENOMEM;
1328117845Ssam					goto errout;
1329117845Ssam				}
1330117845Ssam				if (totlen >= MINCLSIZE) {
1331243857Sglebius					MCLGET(m, M_NOWAIT);
1332117845Ssam					if ((m->m_flags & M_EXT) == 0) {
1333117845Ssam						m_free(m);
1334117845Ssam						safestats.st_nomcl++;
1335117845Ssam						err = sc->sc_nqchip ?
1336117845Ssam							ERESTART : ENOMEM;
1337117845Ssam						goto errout;
1338117845Ssam					}
1339117845Ssam					len = MCLBYTES;
1340117845Ssam				}
1341117845Ssam				m->m_len = len;
1342117845Ssam				top = NULL;
1343117845Ssam				mp = &top;
1344117845Ssam
1345117845Ssam				while (totlen > 0) {
1346117845Ssam					if (top) {
1347243857Sglebius						MGET(m, M_NOWAIT, MT_DATA);
1348117845Ssam						if (m == NULL) {
1349117845Ssam							m_freem(top);
1350117845Ssam							safestats.st_nombuf++;
1351117845Ssam							err = sc->sc_nqchip ?
1352117845Ssam							    ERESTART : ENOMEM;
1353117845Ssam							goto errout;
1354117845Ssam						}
1355117845Ssam						len = MLEN;
1356117845Ssam					}
1357117845Ssam					if (top && totlen >= MINCLSIZE) {
1358243857Sglebius						MCLGET(m, M_NOWAIT);
1359117845Ssam						if ((m->m_flags & M_EXT) == 0) {
1360117845Ssam							*mp = m;
1361117845Ssam							m_freem(top);
1362117845Ssam							safestats.st_nomcl++;
1363117845Ssam							err = sc->sc_nqchip ?
1364117845Ssam							    ERESTART : ENOMEM;
1365117845Ssam							goto errout;
1366117845Ssam						}
1367117845Ssam						len = MCLBYTES;
1368117845Ssam					}
1369117845Ssam					m->m_len = len = min(totlen, len);
1370117845Ssam					totlen -= len;
1371117845Ssam					*mp = m;
1372117845Ssam					mp = &m->m_next;
1373117845Ssam				}
1374117845Ssam				re->re_dst_m = top;
1375117845Ssam				if (bus_dmamap_create(sc->sc_dstdmat,
1376117845Ssam				    BUS_DMA_NOWAIT, &re->re_dst_map) != 0) {
1377117845Ssam					safestats.st_nomap++;
1378117845Ssam					err = ENOMEM;
1379117845Ssam					goto errout;
1380117845Ssam				}
1381117845Ssam				if (bus_dmamap_load_mbuf(sc->sc_dstdmat,
1382117845Ssam				    re->re_dst_map, re->re_dst_m,
1383117845Ssam				    safe_op_cb, &re->re_dst,
1384117845Ssam				    BUS_DMA_NOWAIT) != 0) {
1385117845Ssam					bus_dmamap_destroy(sc->sc_dstdmat,
1386117845Ssam					re->re_dst_map);
1387117845Ssam					re->re_dst_map = NULL;
1388117845Ssam					safestats.st_noload++;
1389117845Ssam					err = ENOMEM;
1390117845Ssam					goto errout;
1391117845Ssam				}
1392117845Ssam				if (re->re_src.mapsize > oplen) {
1393117845Ssam					/*
1394117845Ssam					 * There's data following what the
1395117845Ssam					 * hardware will copy for us.  If this
1396117845Ssam					 * isn't just the ICV (that's going to
1397117845Ssam					 * be written on completion), copy it
1398117845Ssam					 * to the new mbufs
1399117845Ssam					 */
1400117845Ssam					if (!(maccrd &&
1401117845Ssam					    (re->re_src.mapsize-oplen) == 12 &&
1402117845Ssam					    maccrd->crd_inject == oplen))
1403117845Ssam						safe_mcopy(re->re_src_m,
1404117845Ssam							   re->re_dst_m,
1405117845Ssam							   oplen);
1406117845Ssam					else
1407117845Ssam						safestats.st_noicvcopy++;
1408117845Ssam				}
1409117845Ssam			}
1410117845Ssam		} else {
1411117845Ssam			safestats.st_badflags++;
1412117845Ssam			err = EINVAL;
1413117845Ssam			goto errout;
1414117845Ssam		}
1415117845Ssam
1416117845Ssam		if (re->re_dst.nsegs > 1) {
1417117845Ssam			re->re_desc.d_dst = sc->sc_dpalloc.dma_paddr +
1418117845Ssam			    ((caddr_t) sc->sc_dpfree - (caddr_t) sc->sc_dpring);
1419117845Ssam			for (i = 0; i < re->re_dst_nsegs; i++) {
1420117845Ssam				pd = sc->sc_dpfree;
1421117845Ssam				KASSERT((pd->pd_flags&3) == 0 ||
1422117845Ssam					(pd->pd_flags&3) == SAFE_PD_DONE,
1423117845Ssam					("bogus dest particle descriptor; flags %x",
1424117845Ssam						pd->pd_flags));
1425117845Ssam				if (++(sc->sc_dpfree) == sc->sc_dpringtop)
1426117845Ssam					sc->sc_dpfree = sc->sc_dpring;
1427117845Ssam				pd->pd_addr = re->re_dst_segs[i].ds_addr;
1428117845Ssam				pd->pd_flags = SAFE_PD_READY;
1429117845Ssam			}
1430117845Ssam			cmd0 |= SAFE_SA_CMD0_OSCATTER;
1431117845Ssam		} else {
1432117845Ssam			/*
1433117845Ssam			 * No need for scatter, reference the operand directly.
1434117845Ssam			 */
1435117845Ssam			re->re_desc.d_dst = re->re_dst_segs[0].ds_addr;
1436117845Ssam		}
1437117845Ssam	}
1438117845Ssam
1439117845Ssam	/*
1440117845Ssam	 * All done with setup; fillin the SA command words
1441117845Ssam	 * and the packet engine descriptor.  The operation
1442117845Ssam	 * is now ready for submission to the hardware.
1443117845Ssam	 */
1444117845Ssam	sa->sa_cmd0 = cmd0 | SAFE_SA_CMD0_IPCI | SAFE_SA_CMD0_OPCI;
1445117845Ssam	sa->sa_cmd1 = cmd1
1446117845Ssam		    | (coffset << SAFE_SA_CMD1_OFFSET_S)
1447117845Ssam		    | SAFE_SA_CMD1_SAREV1	/* Rev 1 SA data structure */
1448117845Ssam		    | SAFE_SA_CMD1_SRPCI
1449117845Ssam		    ;
1450117845Ssam	/*
1451117845Ssam	 * NB: the order of writes is important here.  In case the
1452117845Ssam	 * chip is scanning the ring because of an outstanding request
1453117845Ssam	 * it might nab this one too.  In that case we need to make
1454117845Ssam	 * sure the setup is complete before we write the length
1455117845Ssam	 * field of the descriptor as it signals the descriptor is
1456117845Ssam	 * ready for processing.
1457117845Ssam	 */
1458117845Ssam	re->re_desc.d_csr = SAFE_PE_CSR_READY | SAFE_PE_CSR_SAPCI;
1459117845Ssam	if (maccrd)
1460117845Ssam		re->re_desc.d_csr |= SAFE_PE_CSR_LOADSA | SAFE_PE_CSR_HASHFINAL;
1461117845Ssam	re->re_desc.d_len = oplen
1462117845Ssam			  | SAFE_PE_LEN_READY
1463117845Ssam			  | (bypass << SAFE_PE_LEN_BYPASS_S)
1464117845Ssam			  ;
1465117845Ssam
1466117845Ssam	safestats.st_ipackets++;
1467117845Ssam	safestats.st_ibytes += oplen;
1468117845Ssam
1469117845Ssam	if (++(sc->sc_front) == sc->sc_ringtop)
1470117845Ssam		sc->sc_front = sc->sc_ring;
1471117845Ssam
1472117845Ssam	/* XXX honor batching */
1473117845Ssam	safe_feed(sc, re);
1474117845Ssam	mtx_unlock(&sc->sc_ringmtx);
1475117845Ssam	return (0);
1476117845Ssam
1477117845Ssamerrout:
1478117845Ssam	if ((re->re_dst_m != NULL) && (re->re_src_m != re->re_dst_m))
1479117845Ssam		m_freem(re->re_dst_m);
1480117845Ssam
1481117845Ssam	if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) {
1482117845Ssam		bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map);
1483117845Ssam		bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map);
1484117845Ssam	}
1485117845Ssam	if (re->re_src_map != NULL) {
1486117845Ssam		bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map);
1487117845Ssam		bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
1488117845Ssam	}
1489117845Ssam	mtx_unlock(&sc->sc_ringmtx);
1490117845Ssam	if (err != ERESTART) {
1491117845Ssam		crp->crp_etype = err;
1492117845Ssam		crypto_done(crp);
1493117845Ssam	} else {
1494117845Ssam		sc->sc_needwakeup |= CRYPTO_SYMQ;
1495117845Ssam	}
1496117845Ssam	return (err);
1497117845Ssam}
1498117845Ssam
1499117845Ssamstatic void
1500117845Ssamsafe_callback(struct safe_softc *sc, struct safe_ringentry *re)
1501117845Ssam{
1502117845Ssam	struct cryptop *crp = (struct cryptop *)re->re_crp;
1503117845Ssam	struct cryptodesc *crd;
1504117845Ssam
1505117845Ssam	safestats.st_opackets++;
1506117845Ssam	safestats.st_obytes += re->re_dst.mapsize;
1507117845Ssam
1508117845Ssam	safe_dma_sync(&sc->sc_ringalloc,
1509117845Ssam		BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1510117845Ssam	if (re->re_desc.d_csr & SAFE_PE_CSR_STATUS) {
1511117845Ssam		device_printf(sc->sc_dev, "csr 0x%x cmd0 0x%x cmd1 0x%x\n",
1512117845Ssam			re->re_desc.d_csr,
1513117845Ssam			re->re_sa.sa_cmd0, re->re_sa.sa_cmd1);
1514117845Ssam		safestats.st_peoperr++;
1515117845Ssam		crp->crp_etype = EIO;		/* something more meaningful? */
1516117845Ssam	}
1517117845Ssam	if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) {
1518117845Ssam		bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map,
1519117845Ssam		    BUS_DMASYNC_POSTREAD);
1520117845Ssam		bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map);
1521117845Ssam		bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map);
1522117845Ssam	}
1523117845Ssam	bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_POSTWRITE);
1524117845Ssam	bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map);
1525117845Ssam	bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
1526117845Ssam
1527117845Ssam	/*
1528117845Ssam	 * If result was written to a differet mbuf chain, swap
1529117845Ssam	 * it in as the return value and reclaim the original.
1530117845Ssam	 */
1531117845Ssam	if ((crp->crp_flags & CRYPTO_F_IMBUF) && re->re_src_m != re->re_dst_m) {
1532117845Ssam		m_freem(re->re_src_m);
1533117845Ssam		crp->crp_buf = (caddr_t)re->re_dst_m;
1534117845Ssam	}
1535117845Ssam
1536117845Ssam	if (re->re_flags & SAFE_QFLAGS_COPYOUTIV) {
1537117845Ssam		/* copy out IV for future use */
1538117845Ssam		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
1539117845Ssam			int ivsize;
1540117845Ssam
1541117845Ssam			if (crd->crd_alg == CRYPTO_DES_CBC ||
1542117845Ssam			    crd->crd_alg == CRYPTO_3DES_CBC) {
1543117845Ssam				ivsize = 2*sizeof(u_int32_t);
1544117845Ssam			} else if (crd->crd_alg == CRYPTO_AES_CBC) {
1545117845Ssam				ivsize = 4*sizeof(u_int32_t);
1546117845Ssam			} else
1547117845Ssam				continue;
1548159242Spjd			crypto_copydata(crp->crp_flags, crp->crp_buf,
1549159242Spjd			    crd->crd_skip + crd->crd_len - ivsize, ivsize,
1550159242Spjd			    (caddr_t)sc->sc_sessions[re->re_sesn].ses_iv);
1551117845Ssam			break;
1552117845Ssam		}
1553117845Ssam	}
1554117845Ssam
1555117845Ssam	if (re->re_flags & SAFE_QFLAGS_COPYOUTICV) {
1556117845Ssam		/* copy out ICV result */
1557117845Ssam		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
1558117845Ssam			if (!(crd->crd_alg == CRYPTO_MD5_HMAC ||
1559117845Ssam			    crd->crd_alg == CRYPTO_SHA1_HMAC ||
1560117845Ssam			    crd->crd_alg == CRYPTO_NULL_HMAC))
1561117845Ssam				continue;
1562117845Ssam			if (crd->crd_alg == CRYPTO_SHA1_HMAC) {
1563117845Ssam				/*
1564117845Ssam				 * SHA-1 ICV's are byte-swapped; fix 'em up
1565117845Ssam				 * before copy them to their destination.
1566117845Ssam				 */
1567223026Sdelphij				re->re_sastate.sa_saved_indigest[0] =
1568223026Sdelphij				    bswap32(re->re_sastate.sa_saved_indigest[0]);
1569223026Sdelphij				re->re_sastate.sa_saved_indigest[1] =
1570223026Sdelphij				    bswap32(re->re_sastate.sa_saved_indigest[1]);
1571223026Sdelphij				re->re_sastate.sa_saved_indigest[2] =
1572223026Sdelphij				    bswap32(re->re_sastate.sa_saved_indigest[2]);
1573117845Ssam			}
1574159242Spjd			crypto_copyback(crp->crp_flags, crp->crp_buf,
1575159242Spjd			    crd->crd_inject,
1576159242Spjd			    sc->sc_sessions[re->re_sesn].ses_mlen,
1577159242Spjd			    (caddr_t)re->re_sastate.sa_saved_indigest);
1578117845Ssam			break;
1579117845Ssam		}
1580117845Ssam	}
1581117845Ssam	crypto_done(crp);
1582117845Ssam}
1583117845Ssam
1584117845Ssam/*
1585117845Ssam * Copy all data past offset from srcm to dstm.
1586117845Ssam */
1587117845Ssamstatic void
1588117845Ssamsafe_mcopy(struct mbuf *srcm, struct mbuf *dstm, u_int offset)
1589117845Ssam{
1590117845Ssam	u_int j, dlen, slen;
1591117845Ssam	caddr_t dptr, sptr;
1592117845Ssam
1593117845Ssam	/*
1594117845Ssam	 * Advance src and dst to offset.
1595117845Ssam	 */
1596117845Ssam	j = offset;
1597117845Ssam	while (j >= 0) {
1598117845Ssam		if (srcm->m_len > j)
1599117845Ssam			break;
1600117845Ssam		j -= srcm->m_len;
1601117845Ssam		srcm = srcm->m_next;
1602117845Ssam		if (srcm == NULL)
1603117845Ssam			return;
1604117845Ssam	}
1605117845Ssam	sptr = mtod(srcm, caddr_t) + j;
1606117845Ssam	slen = srcm->m_len - j;
1607117845Ssam
1608117845Ssam	j = offset;
1609117845Ssam	while (j >= 0) {
1610117845Ssam		if (dstm->m_len > j)
1611117845Ssam			break;
1612117845Ssam		j -= dstm->m_len;
1613117845Ssam		dstm = dstm->m_next;
1614117845Ssam		if (dstm == NULL)
1615117845Ssam			return;
1616117845Ssam	}
1617117845Ssam	dptr = mtod(dstm, caddr_t) + j;
1618117845Ssam	dlen = dstm->m_len - j;
1619117845Ssam
1620117845Ssam	/*
1621117845Ssam	 * Copy everything that remains.
1622117845Ssam	 */
1623117845Ssam	for (;;) {
1624117845Ssam		j = min(slen, dlen);
1625117845Ssam		bcopy(sptr, dptr, j);
1626117845Ssam		if (slen == j) {
1627117845Ssam			srcm = srcm->m_next;
1628117845Ssam			if (srcm == NULL)
1629117845Ssam				return;
1630117845Ssam			sptr = srcm->m_data;
1631117845Ssam			slen = srcm->m_len;
1632117845Ssam		} else
1633117845Ssam			sptr += j, slen -= j;
1634117845Ssam		if (dlen == j) {
1635117845Ssam			dstm = dstm->m_next;
1636117845Ssam			if (dstm == NULL)
1637117845Ssam				return;
1638117845Ssam			dptr = dstm->m_data;
1639117845Ssam			dlen = dstm->m_len;
1640117845Ssam		} else
1641117845Ssam			dptr += j, dlen -= j;
1642117845Ssam	}
1643117845Ssam}
1644117845Ssam
1645117845Ssam#ifndef SAFE_NO_RNG
1646117845Ssam#define	SAFE_RNG_MAXWAIT	1000
1647117845Ssam
1648117845Ssamstatic void
1649117845Ssamsafe_rng_init(struct safe_softc *sc)
1650117845Ssam{
1651117845Ssam	u_int32_t w, v;
1652117845Ssam	int i;
1653117845Ssam
1654117845Ssam	WRITE_REG(sc, SAFE_RNG_CTRL, 0);
1655117845Ssam	/* use default value according to the manual */
1656117845Ssam	WRITE_REG(sc, SAFE_RNG_CNFG, 0x834);	/* magic from SafeNet */
1657117845Ssam	WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
1658117845Ssam
1659117845Ssam	/*
1660117845Ssam	 * There is a bug in rev 1.0 of the 1140 that when the RNG
1661117845Ssam	 * is brought out of reset the ready status flag does not
1662117845Ssam	 * work until the RNG has finished its internal initialization.
1663117845Ssam	 *
1664117845Ssam	 * So in order to determine the device is through its
1665117845Ssam	 * initialization we must read the data register, using the
1666117845Ssam	 * status reg in the read in case it is initialized.  Then read
1667117845Ssam	 * the data register until it changes from the first read.
1668117845Ssam	 * Once it changes read the data register until it changes
1669117845Ssam	 * again.  At this time the RNG is considered initialized.
1670117845Ssam	 * This could take between 750ms - 1000ms in time.
1671117845Ssam	 */
1672117845Ssam	i = 0;
1673117845Ssam	w = READ_REG(sc, SAFE_RNG_OUT);
1674117845Ssam	do {
1675117845Ssam		v = READ_REG(sc, SAFE_RNG_OUT);
1676117845Ssam		if (v != w) {
1677117845Ssam			w = v;
1678117845Ssam			break;
1679117845Ssam		}
1680117845Ssam		DELAY(10);
1681117845Ssam	} while (++i < SAFE_RNG_MAXWAIT);
1682117845Ssam
1683117845Ssam	/* Wait Until data changes again */
1684117845Ssam	i = 0;
1685117845Ssam	do {
1686117845Ssam		v = READ_REG(sc, SAFE_RNG_OUT);
1687117845Ssam		if (v != w)
1688117845Ssam			break;
1689117845Ssam		DELAY(10);
1690117845Ssam	} while (++i < SAFE_RNG_MAXWAIT);
1691117845Ssam}
1692117845Ssam
1693117845Ssamstatic __inline void
1694117845Ssamsafe_rng_disable_short_cycle(struct safe_softc *sc)
1695117845Ssam{
1696117845Ssam	WRITE_REG(sc, SAFE_RNG_CTRL,
1697117845Ssam		READ_REG(sc, SAFE_RNG_CTRL) &~ SAFE_RNG_CTRL_SHORTEN);
1698117845Ssam}
1699117845Ssam
1700117845Ssamstatic __inline void
1701117845Ssamsafe_rng_enable_short_cycle(struct safe_softc *sc)
1702117845Ssam{
1703117845Ssam	WRITE_REG(sc, SAFE_RNG_CTRL,
1704117845Ssam		READ_REG(sc, SAFE_RNG_CTRL) | SAFE_RNG_CTRL_SHORTEN);
1705117845Ssam}
1706117845Ssam
1707117845Ssamstatic __inline u_int32_t
1708117845Ssamsafe_rng_read(struct safe_softc *sc)
1709117845Ssam{
1710117845Ssam	int i;
1711117845Ssam
1712117845Ssam	i = 0;
1713117845Ssam	while (READ_REG(sc, SAFE_RNG_STAT) != 0 && ++i < SAFE_RNG_MAXWAIT)
1714117845Ssam		;
1715117845Ssam	return READ_REG(sc, SAFE_RNG_OUT);
1716117845Ssam}
1717117845Ssam
1718117845Ssamstatic void
1719117845Ssamsafe_rng(void *arg)
1720117845Ssam{
1721117845Ssam	struct safe_softc *sc = arg;
1722117845Ssam	u_int32_t buf[SAFE_RNG_MAXBUFSIZ];	/* NB: maybe move to softc */
1723117845Ssam	u_int maxwords;
1724117845Ssam	int i;
1725117845Ssam
1726117845Ssam	safestats.st_rng++;
1727117845Ssam	/*
1728117845Ssam	 * Fetch the next block of data.
1729117845Ssam	 */
1730117845Ssam	maxwords = safe_rngbufsize;
1731117845Ssam	if (maxwords > SAFE_RNG_MAXBUFSIZ)
1732117845Ssam		maxwords = SAFE_RNG_MAXBUFSIZ;
1733117845Ssamretry:
1734117845Ssam	for (i = 0; i < maxwords; i++)
1735117845Ssam		buf[i] = safe_rng_read(sc);
1736117845Ssam	/*
1737117845Ssam	 * Check the comparator alarm count and reset the h/w if
1738117845Ssam	 * it exceeds our threshold.  This guards against the
1739117845Ssam	 * hardware oscillators resonating with external signals.
1740117845Ssam	 */
1741117845Ssam	if (READ_REG(sc, SAFE_RNG_ALM_CNT) > safe_rngmaxalarm) {
1742117845Ssam		u_int32_t freq_inc, w;
1743117845Ssam
1744117845Ssam		DPRINTF(("%s: alarm count %u exceeds threshold %u\n", __func__,
1745117845Ssam			READ_REG(sc, SAFE_RNG_ALM_CNT), safe_rngmaxalarm));
1746117845Ssam		safestats.st_rngalarm++;
1747117845Ssam		safe_rng_enable_short_cycle(sc);
1748117845Ssam		freq_inc = 18;
1749117845Ssam		for (i = 0; i < 64; i++) {
1750117845Ssam			w = READ_REG(sc, SAFE_RNG_CNFG);
1751117845Ssam			freq_inc = ((w + freq_inc) & 0x3fL);
1752117845Ssam			w = ((w & ~0x3fL) | freq_inc);
1753117845Ssam			WRITE_REG(sc, SAFE_RNG_CNFG, w);
1754117845Ssam
1755117845Ssam			WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
1756117845Ssam
1757117845Ssam			(void) safe_rng_read(sc);
1758117845Ssam			DELAY(25);
1759117845Ssam
1760117845Ssam			if (READ_REG(sc, SAFE_RNG_ALM_CNT) == 0) {
1761117845Ssam				safe_rng_disable_short_cycle(sc);
1762117845Ssam				goto retry;
1763117845Ssam			}
1764117845Ssam			freq_inc = 1;
1765117845Ssam		}
1766117845Ssam		safe_rng_disable_short_cycle(sc);
1767117845Ssam	} else
1768117845Ssam		WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
1769117845Ssam
1770117845Ssam	(*sc->sc_harvest)(sc->sc_rndtest, buf, maxwords*sizeof (u_int32_t));
1771117845Ssam	callout_reset(&sc->sc_rngto,
1772117845Ssam		hz * (safe_rnginterval ? safe_rnginterval : 1), safe_rng, sc);
1773117845Ssam}
1774117845Ssam#endif /* SAFE_NO_RNG */
1775117845Ssam
1776117845Ssamstatic void
1777117845Ssamsafe_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1778117845Ssam{
1779117845Ssam	bus_addr_t *paddr = (bus_addr_t*) arg;
1780117845Ssam	*paddr = segs->ds_addr;
1781117845Ssam}
1782117845Ssam
1783117845Ssamstatic int
1784117845Ssamsafe_dma_malloc(
1785117845Ssam	struct safe_softc *sc,
1786117845Ssam	bus_size_t size,
1787117845Ssam	struct safe_dma_alloc *dma,
1788117845Ssam	int mapflags
1789117845Ssam)
1790117845Ssam{
1791117845Ssam	int r;
1792117845Ssam
1793232874Sscottl	r = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
1794117845Ssam			       sizeof(u_int32_t), 0,	/* alignment, bounds */
1795117845Ssam			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1796117845Ssam			       BUS_SPACE_MAXADDR,	/* highaddr */
1797117845Ssam			       NULL, NULL,		/* filter, filterarg */
1798117845Ssam			       size,			/* maxsize */
1799117845Ssam			       1,			/* nsegments */
1800117845Ssam			       size,			/* maxsegsize */
1801117845Ssam			       BUS_DMA_ALLOCNOW,	/* flags */
1802117845Ssam			       NULL, NULL,		/* locking */
1803117845Ssam			       &dma->dma_tag);
1804117845Ssam	if (r != 0) {
1805117845Ssam		device_printf(sc->sc_dev, "safe_dma_malloc: "
1806117845Ssam			"bus_dma_tag_create failed; error %u\n", r);
1807117845Ssam		goto fail_0;
1808117845Ssam	}
1809117845Ssam
1810117845Ssam	r = bus_dmamap_create(dma->dma_tag, BUS_DMA_NOWAIT, &dma->dma_map);
1811117845Ssam	if (r != 0) {
1812117845Ssam		device_printf(sc->sc_dev, "safe_dma_malloc: "
1813117845Ssam			"bus_dmamap_create failed; error %u\n", r);
1814117845Ssam		goto fail_1;
1815117845Ssam	}
1816117845Ssam
1817117845Ssam	r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
1818117845Ssam			     BUS_DMA_NOWAIT, &dma->dma_map);
1819117845Ssam	if (r != 0) {
1820117845Ssam		device_printf(sc->sc_dev, "safe_dma_malloc: "
1821117845Ssam			"bus_dmammem_alloc failed; size %zu, error %u\n",
1822117845Ssam			size, r);
1823117845Ssam		goto fail_2;
1824117845Ssam	}
1825117845Ssam
1826117845Ssam	r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
1827117845Ssam		            size,
1828117845Ssam			    safe_dmamap_cb,
1829117845Ssam			    &dma->dma_paddr,
1830117845Ssam			    mapflags | BUS_DMA_NOWAIT);
1831117845Ssam	if (r != 0) {
1832117845Ssam		device_printf(sc->sc_dev, "safe_dma_malloc: "
1833117845Ssam			"bus_dmamap_load failed; error %u\n", r);
1834117845Ssam		goto fail_3;
1835117845Ssam	}
1836117845Ssam
1837117845Ssam	dma->dma_size = size;
1838117845Ssam	return (0);
1839117845Ssam
1840117845Ssamfail_3:
1841117845Ssam	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1842117845Ssamfail_2:
1843117845Ssam	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1844117845Ssamfail_1:
1845117845Ssam	bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
1846117845Ssam	bus_dma_tag_destroy(dma->dma_tag);
1847117845Ssamfail_0:
1848117845Ssam	dma->dma_map = NULL;
1849117845Ssam	dma->dma_tag = NULL;
1850117845Ssam	return (r);
1851117845Ssam}
1852117845Ssam
1853117845Ssamstatic void
1854117845Ssamsafe_dma_free(struct safe_softc *sc, struct safe_dma_alloc *dma)
1855117845Ssam{
1856117845Ssam	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1857117845Ssam	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1858117845Ssam	bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
1859117845Ssam	bus_dma_tag_destroy(dma->dma_tag);
1860117845Ssam}
1861117845Ssam
1862117845Ssam/*
1863117845Ssam * Resets the board.  Values in the regesters are left as is
1864117845Ssam * from the reset (i.e. initial values are assigned elsewhere).
1865117845Ssam */
1866117845Ssamstatic void
1867117845Ssamsafe_reset_board(struct safe_softc *sc)
1868117845Ssam{
1869117845Ssam	u_int32_t v;
1870117845Ssam	/*
1871117845Ssam	 * Reset the device.  The manual says no delay
1872117845Ssam	 * is needed between marking and clearing reset.
1873117845Ssam	 */
1874117845Ssam	v = READ_REG(sc, SAFE_PE_DMACFG) &~
1875117845Ssam		(SAFE_PE_DMACFG_PERESET | SAFE_PE_DMACFG_PDRRESET |
1876117845Ssam		 SAFE_PE_DMACFG_SGRESET);
1877117845Ssam	WRITE_REG(sc, SAFE_PE_DMACFG, v
1878117845Ssam				    | SAFE_PE_DMACFG_PERESET
1879117845Ssam				    | SAFE_PE_DMACFG_PDRRESET
1880117845Ssam				    | SAFE_PE_DMACFG_SGRESET);
1881117845Ssam	WRITE_REG(sc, SAFE_PE_DMACFG, v);
1882117845Ssam}
1883117845Ssam
1884117845Ssam/*
1885117845Ssam * Initialize registers we need to touch only once.
1886117845Ssam */
1887117845Ssamstatic void
1888117845Ssamsafe_init_board(struct safe_softc *sc)
1889117845Ssam{
1890117845Ssam	u_int32_t v, dwords;
1891117845Ssam
1892201758Smbr	v = READ_REG(sc, SAFE_PE_DMACFG);
1893117845Ssam	v &=~ SAFE_PE_DMACFG_PEMODE;
1894117845Ssam	v |= SAFE_PE_DMACFG_FSENA		/* failsafe enable */
1895117845Ssam	  |  SAFE_PE_DMACFG_GPRPCI		/* gather ring on PCI */
1896117845Ssam	  |  SAFE_PE_DMACFG_SPRPCI		/* scatter ring on PCI */
1897117845Ssam	  |  SAFE_PE_DMACFG_ESDESC		/* endian-swap descriptors */
1898117845Ssam	  |  SAFE_PE_DMACFG_ESSA		/* endian-swap SA's */
1899117845Ssam	  |  SAFE_PE_DMACFG_ESPDESC		/* endian-swap part. desc's */
1900117845Ssam	  ;
1901117845Ssam	WRITE_REG(sc, SAFE_PE_DMACFG, v);
1902117845Ssam#if 0
1903117845Ssam	/* XXX select byte swap based on host byte order */
1904117845Ssam	WRITE_REG(sc, SAFE_ENDIAN, 0x1b);
1905117845Ssam#endif
1906117845Ssam	if (sc->sc_chiprev == SAFE_REV(1,0)) {
1907117845Ssam		/*
1908117845Ssam		 * Avoid large PCI DMA transfers.  Rev 1.0 has a bug where
1909117845Ssam		 * "target mode transfers" done while the chip is DMA'ing
1910117845Ssam		 * >1020 bytes cause the hardware to lockup.  To avoid this
1911117845Ssam		 * we reduce the max PCI transfer size and use small source
1912117845Ssam		 * particle descriptors (<= 256 bytes).
1913117845Ssam		 */
1914117845Ssam		WRITE_REG(sc, SAFE_DMA_CFG, 256);
1915117845Ssam		device_printf(sc->sc_dev,
1916117845Ssam			"Reduce max DMA size to %u words for rev %u.%u WAR\n",
1917117845Ssam			(READ_REG(sc, SAFE_DMA_CFG)>>2) & 0xff,
1918117845Ssam			SAFE_REV_MAJ(sc->sc_chiprev),
1919117845Ssam			SAFE_REV_MIN(sc->sc_chiprev));
1920117845Ssam	}
1921117845Ssam
1922117845Ssam	/* NB: operands+results are overlaid */
1923117845Ssam	WRITE_REG(sc, SAFE_PE_PDRBASE, sc->sc_ringalloc.dma_paddr);
1924117845Ssam	WRITE_REG(sc, SAFE_PE_RDRBASE, sc->sc_ringalloc.dma_paddr);
1925117845Ssam	/*
1926117845Ssam	 * Configure ring entry size and number of items in the ring.
1927117845Ssam	 */
1928117845Ssam	KASSERT((sizeof(struct safe_ringentry) % sizeof(u_int32_t)) == 0,
1929117845Ssam		("PE ring entry not 32-bit aligned!"));
1930117845Ssam	dwords = sizeof(struct safe_ringentry) / sizeof(u_int32_t);
1931117845Ssam	WRITE_REG(sc, SAFE_PE_RINGCFG,
1932117845Ssam		(dwords << SAFE_PE_RINGCFG_OFFSET_S) | SAFE_MAX_NQUEUE);
1933117845Ssam	WRITE_REG(sc, SAFE_PE_RINGPOLL, 0);	/* disable polling */
1934117845Ssam
1935117845Ssam	WRITE_REG(sc, SAFE_PE_GRNGBASE, sc->sc_spalloc.dma_paddr);
1936117845Ssam	WRITE_REG(sc, SAFE_PE_SRNGBASE, sc->sc_dpalloc.dma_paddr);
1937117845Ssam	WRITE_REG(sc, SAFE_PE_PARTSIZE,
1938117845Ssam		(SAFE_TOTAL_DPART<<16) | SAFE_TOTAL_SPART);
1939117845Ssam	/*
1940117845Ssam	 * NB: destination particles are fixed size.  We use
1941117845Ssam	 *     an mbuf cluster and require all results go to
1942117845Ssam	 *     clusters or smaller.
1943117845Ssam	 */
1944117845Ssam	WRITE_REG(sc, SAFE_PE_PARTCFG, SAFE_MAX_DSIZE);
1945117845Ssam
1946117845Ssam	/* it's now safe to enable PE mode, do it */
1947117845Ssam	WRITE_REG(sc, SAFE_PE_DMACFG, v | SAFE_PE_DMACFG_PEMODE);
1948117845Ssam
1949117845Ssam	/*
1950117845Ssam	 * Configure hardware to use level-triggered interrupts and
1951117845Ssam	 * to interrupt after each descriptor is processed.
1952117845Ssam	 */
1953117845Ssam	WRITE_REG(sc, SAFE_HI_CFG, SAFE_HI_CFG_LEVEL);
1954117845Ssam	WRITE_REG(sc, SAFE_HI_DESC_CNT, 1);
1955117845Ssam	WRITE_REG(sc, SAFE_HI_MASK, SAFE_INT_PE_DDONE | SAFE_INT_PE_ERROR);
1956117845Ssam}
1957117845Ssam
1958117845Ssam/*
1959117845Ssam * Init PCI registers
1960117845Ssam */
1961117845Ssamstatic void
1962117845Ssamsafe_init_pciregs(device_t dev)
1963117845Ssam{
1964117845Ssam}
1965117845Ssam
1966117845Ssam/*
1967117845Ssam * Clean up after a chip crash.
1968117845Ssam * It is assumed that the caller in splimp()
1969117845Ssam */
1970117845Ssamstatic void
1971117845Ssamsafe_cleanchip(struct safe_softc *sc)
1972117845Ssam{
1973117845Ssam
1974117845Ssam	if (sc->sc_nqchip != 0) {
1975117845Ssam		struct safe_ringentry *re = sc->sc_back;
1976117845Ssam
1977117845Ssam		while (re != sc->sc_front) {
1978117845Ssam			if (re->re_desc.d_csr != 0)
1979117845Ssam				safe_free_entry(sc, re);
1980117845Ssam			if (++re == sc->sc_ringtop)
1981117845Ssam				re = sc->sc_ring;
1982117845Ssam		}
1983117845Ssam		sc->sc_back = re;
1984117845Ssam		sc->sc_nqchip = 0;
1985117845Ssam	}
1986117845Ssam}
1987117845Ssam
1988117845Ssam/*
1989117845Ssam * free a safe_q
1990117845Ssam * It is assumed that the caller is within splimp().
1991117845Ssam */
1992117845Ssamstatic int
1993117845Ssamsafe_free_entry(struct safe_softc *sc, struct safe_ringentry *re)
1994117845Ssam{
1995117845Ssam	struct cryptop *crp;
1996117845Ssam
1997117845Ssam	/*
1998117845Ssam	 * Free header MCR
1999117845Ssam	 */
2000117845Ssam	if ((re->re_dst_m != NULL) && (re->re_src_m != re->re_dst_m))
2001117845Ssam		m_freem(re->re_dst_m);
2002117845Ssam
2003117845Ssam	crp = (struct cryptop *)re->re_crp;
2004117845Ssam
2005117845Ssam	re->re_desc.d_csr = 0;
2006117845Ssam
2007117845Ssam	crp->crp_etype = EFAULT;
2008117845Ssam	crypto_done(crp);
2009117845Ssam	return(0);
2010117845Ssam}
2011117845Ssam
2012117845Ssam/*
2013117845Ssam * Routine to reset the chip and clean up.
2014117845Ssam * It is assumed that the caller is in splimp()
2015117845Ssam */
2016117845Ssamstatic void
2017117845Ssamsafe_totalreset(struct safe_softc *sc)
2018117845Ssam{
2019117845Ssam	safe_reset_board(sc);
2020117845Ssam	safe_init_board(sc);
2021117845Ssam	safe_cleanchip(sc);
2022117845Ssam}
2023117845Ssam
2024117845Ssam/*
2025117845Ssam * Is the operand suitable aligned for direct DMA.  Each
2026117845Ssam * segment must be aligned on a 32-bit boundary and all
2027117845Ssam * but the last segment must be a multiple of 4 bytes.
2028117845Ssam */
2029117845Ssamstatic int
2030117845Ssamsafe_dmamap_aligned(const struct safe_operand *op)
2031117845Ssam{
2032117845Ssam	int i;
2033117845Ssam
2034117845Ssam	for (i = 0; i < op->nsegs; i++) {
2035117845Ssam		if (op->segs[i].ds_addr & 3)
2036117845Ssam			return (0);
2037117845Ssam		if (i != (op->nsegs - 1) && (op->segs[i].ds_len & 3))
2038117845Ssam			return (0);
2039117845Ssam	}
2040117845Ssam	return (1);
2041117845Ssam}
2042117845Ssam
2043117845Ssam/*
2044117845Ssam * Is the operand suitable for direct DMA as the destination
2045117845Ssam * of an operation.  The hardware requires that each ``particle''
2046117845Ssam * but the last in an operation result have the same size.  We
2047117845Ssam * fix that size at SAFE_MAX_DSIZE bytes.  This routine returns
2048117845Ssam * 0 if some segment is not a multiple of of this size, 1 if all
2049117845Ssam * segments are exactly this size, or 2 if segments are at worst
2050117845Ssam * a multple of this size.
2051117845Ssam */
2052117845Ssamstatic int
2053117845Ssamsafe_dmamap_uniform(const struct safe_operand *op)
2054117845Ssam{
2055117845Ssam	int result = 1;
2056117845Ssam
2057117845Ssam	if (op->nsegs > 0) {
2058117845Ssam		int i;
2059117845Ssam
2060118882Ssam		for (i = 0; i < op->nsegs-1; i++) {
2061117845Ssam			if (op->segs[i].ds_len % SAFE_MAX_DSIZE)
2062117845Ssam				return (0);
2063117845Ssam			if (op->segs[i].ds_len != SAFE_MAX_DSIZE)
2064117845Ssam				result = 2;
2065118882Ssam		}
2066117845Ssam	}
2067117845Ssam	return (result);
2068117845Ssam}
2069117845Ssam
2070117845Ssam#ifdef SAFE_DEBUG
2071117845Ssamstatic void
2072117845Ssamsafe_dump_dmastatus(struct safe_softc *sc, const char *tag)
2073117845Ssam{
2074117845Ssam	printf("%s: ENDIAN 0x%x SRC 0x%x DST 0x%x STAT 0x%x\n"
2075117845Ssam		, tag
2076117845Ssam		, READ_REG(sc, SAFE_DMA_ENDIAN)
2077117845Ssam		, READ_REG(sc, SAFE_DMA_SRCADDR)
2078117845Ssam		, READ_REG(sc, SAFE_DMA_DSTADDR)
2079117845Ssam		, READ_REG(sc, SAFE_DMA_STAT)
2080117845Ssam	);
2081117845Ssam}
2082117845Ssam
2083117845Ssamstatic void
2084117845Ssamsafe_dump_intrstate(struct safe_softc *sc, const char *tag)
2085117845Ssam{
2086117845Ssam	printf("%s: HI_CFG 0x%x HI_MASK 0x%x HI_DESC_CNT 0x%x HU_STAT 0x%x HM_STAT 0x%x\n"
2087117845Ssam		, tag
2088117845Ssam		, READ_REG(sc, SAFE_HI_CFG)
2089117845Ssam		, READ_REG(sc, SAFE_HI_MASK)
2090117845Ssam		, READ_REG(sc, SAFE_HI_DESC_CNT)
2091117845Ssam		, READ_REG(sc, SAFE_HU_STAT)
2092117845Ssam		, READ_REG(sc, SAFE_HM_STAT)
2093117845Ssam	);
2094117845Ssam}
2095117845Ssam
2096117845Ssamstatic void
2097117845Ssamsafe_dump_ringstate(struct safe_softc *sc, const char *tag)
2098117845Ssam{
2099117845Ssam	u_int32_t estat = READ_REG(sc, SAFE_PE_ERNGSTAT);
2100117845Ssam
2101117845Ssam	/* NB: assume caller has lock on ring */
2102125466Speter	printf("%s: ERNGSTAT %x (next %u) back %lu front %lu\n",
2103117845Ssam		tag,
2104117845Ssam		estat, (estat >> SAFE_PE_ERNGSTAT_NEXT_S),
2105125466Speter		(unsigned long)(sc->sc_back - sc->sc_ring),
2106125466Speter		(unsigned long)(sc->sc_front - sc->sc_ring));
2107117845Ssam}
2108117845Ssam
2109117845Ssamstatic void
2110117845Ssamsafe_dump_request(struct safe_softc *sc, const char* tag, struct safe_ringentry *re)
2111117845Ssam{
2112117845Ssam	int ix, nsegs;
2113117845Ssam
2114117845Ssam	ix = re - sc->sc_ring;
2115117845Ssam	printf("%s: %p (%u): csr %x src %x dst %x sa %x len %x\n"
2116117845Ssam		, tag
2117117845Ssam		, re, ix
2118117845Ssam		, re->re_desc.d_csr
2119117845Ssam		, re->re_desc.d_src
2120117845Ssam		, re->re_desc.d_dst
2121117845Ssam		, re->re_desc.d_sa
2122117845Ssam		, re->re_desc.d_len
2123117845Ssam	);
2124117845Ssam	if (re->re_src.nsegs > 1) {
2125117845Ssam		ix = (re->re_desc.d_src - sc->sc_spalloc.dma_paddr) /
2126117845Ssam			sizeof(struct safe_pdesc);
2127117845Ssam		for (nsegs = re->re_src.nsegs; nsegs; nsegs--) {
2128117845Ssam			printf(" spd[%u] %p: %p size %u flags %x"
2129117845Ssam				, ix, &sc->sc_spring[ix]
2130125466Speter				, (caddr_t)(uintptr_t) sc->sc_spring[ix].pd_addr
2131117845Ssam				, sc->sc_spring[ix].pd_size
2132117845Ssam				, sc->sc_spring[ix].pd_flags
2133117845Ssam			);
2134117845Ssam			if (sc->sc_spring[ix].pd_size == 0)
2135117845Ssam				printf(" (zero!)");
2136117845Ssam			printf("\n");
2137117845Ssam			if (++ix == SAFE_TOTAL_SPART)
2138117845Ssam				ix = 0;
2139117845Ssam		}
2140117845Ssam	}
2141117845Ssam	if (re->re_dst.nsegs > 1) {
2142117845Ssam		ix = (re->re_desc.d_dst - sc->sc_dpalloc.dma_paddr) /
2143117845Ssam			sizeof(struct safe_pdesc);
2144117845Ssam		for (nsegs = re->re_dst.nsegs; nsegs; nsegs--) {
2145117845Ssam			printf(" dpd[%u] %p: %p flags %x\n"
2146117845Ssam				, ix, &sc->sc_dpring[ix]
2147125466Speter				, (caddr_t)(uintptr_t) sc->sc_dpring[ix].pd_addr
2148117845Ssam				, sc->sc_dpring[ix].pd_flags
2149117845Ssam			);
2150117845Ssam			if (++ix == SAFE_TOTAL_DPART)
2151117845Ssam				ix = 0;
2152117845Ssam		}
2153117845Ssam	}
2154117845Ssam	printf("sa: cmd0 %08x cmd1 %08x staterec %x\n",
2155117845Ssam		re->re_sa.sa_cmd0, re->re_sa.sa_cmd1, re->re_sa.sa_staterec);
2156117845Ssam	printf("sa: key %x %x %x %x %x %x %x %x\n"
2157117845Ssam		, re->re_sa.sa_key[0]
2158117845Ssam		, re->re_sa.sa_key[1]
2159117845Ssam		, re->re_sa.sa_key[2]
2160117845Ssam		, re->re_sa.sa_key[3]
2161117845Ssam		, re->re_sa.sa_key[4]
2162117845Ssam		, re->re_sa.sa_key[5]
2163117845Ssam		, re->re_sa.sa_key[6]
2164117845Ssam		, re->re_sa.sa_key[7]
2165117845Ssam	);
2166117845Ssam	printf("sa: indigest %x %x %x %x %x\n"
2167117845Ssam		, re->re_sa.sa_indigest[0]
2168117845Ssam		, re->re_sa.sa_indigest[1]
2169117845Ssam		, re->re_sa.sa_indigest[2]
2170117845Ssam		, re->re_sa.sa_indigest[3]
2171117845Ssam		, re->re_sa.sa_indigest[4]
2172117845Ssam	);
2173117845Ssam	printf("sa: outdigest %x %x %x %x %x\n"
2174117845Ssam		, re->re_sa.sa_outdigest[0]
2175117845Ssam		, re->re_sa.sa_outdigest[1]
2176117845Ssam		, re->re_sa.sa_outdigest[2]
2177117845Ssam		, re->re_sa.sa_outdigest[3]
2178117845Ssam		, re->re_sa.sa_outdigest[4]
2179117845Ssam	);
2180117845Ssam	printf("sr: iv %x %x %x %x\n"
2181117845Ssam		, re->re_sastate.sa_saved_iv[0]
2182117845Ssam		, re->re_sastate.sa_saved_iv[1]
2183117845Ssam		, re->re_sastate.sa_saved_iv[2]
2184117845Ssam		, re->re_sastate.sa_saved_iv[3]
2185117845Ssam	);
2186117845Ssam	printf("sr: hashbc %u indigest %x %x %x %x %x\n"
2187117845Ssam		, re->re_sastate.sa_saved_hashbc
2188117845Ssam		, re->re_sastate.sa_saved_indigest[0]
2189117845Ssam		, re->re_sastate.sa_saved_indigest[1]
2190117845Ssam		, re->re_sastate.sa_saved_indigest[2]
2191117845Ssam		, re->re_sastate.sa_saved_indigest[3]
2192117845Ssam		, re->re_sastate.sa_saved_indigest[4]
2193117845Ssam	);
2194117845Ssam}
2195117845Ssam
2196117845Ssamstatic void
2197117845Ssamsafe_dump_ring(struct safe_softc *sc, const char *tag)
2198117845Ssam{
2199117845Ssam	mtx_lock(&sc->sc_ringmtx);
2200117845Ssam	printf("\nSafeNet Ring State:\n");
2201117845Ssam	safe_dump_intrstate(sc, tag);
2202117845Ssam	safe_dump_dmastatus(sc, tag);
2203117845Ssam	safe_dump_ringstate(sc, tag);
2204117845Ssam	if (sc->sc_nqchip) {
2205117845Ssam		struct safe_ringentry *re = sc->sc_back;
2206117845Ssam		do {
2207117845Ssam			safe_dump_request(sc, tag, re);
2208117845Ssam			if (++re == sc->sc_ringtop)
2209117845Ssam				re = sc->sc_ring;
2210117845Ssam		} while (re != sc->sc_front);
2211117845Ssam	}
2212117845Ssam	mtx_unlock(&sc->sc_ringmtx);
2213117845Ssam}
2214117845Ssam
2215117845Ssamstatic int
2216117845Ssamsysctl_hw_safe_dump(SYSCTL_HANDLER_ARGS)
2217117845Ssam{
2218117845Ssam	char dmode[64];
2219117845Ssam	int error;
2220117845Ssam
2221117845Ssam	strncpy(dmode, "", sizeof(dmode) - 1);
2222117845Ssam	dmode[sizeof(dmode) - 1] = '\0';
2223117845Ssam	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
2224117845Ssam
2225117845Ssam	if (error == 0 && req->newptr != NULL) {
2226117845Ssam		struct safe_softc *sc = safec;
2227117845Ssam
2228117845Ssam		if (!sc)
2229117845Ssam			return EINVAL;
2230117845Ssam		if (strncmp(dmode, "dma", 3) == 0)
2231117845Ssam			safe_dump_dmastatus(sc, "safe0");
2232117845Ssam		else if (strncmp(dmode, "int", 3) == 0)
2233117845Ssam			safe_dump_intrstate(sc, "safe0");
2234117845Ssam		else if (strncmp(dmode, "ring", 4) == 0)
2235117845Ssam			safe_dump_ring(sc, "safe0");
2236117845Ssam		else
2237117845Ssam			return EINVAL;
2238117845Ssam	}
2239117845Ssam	return error;
2240117845Ssam}
2241117845SsamSYSCTL_PROC(_hw_safe, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
2242117845Ssam	0, 0, sysctl_hw_safe_dump, "A", "Dump driver state");
2243117845Ssam#endif /* SAFE_DEBUG */
2244