xref: /freebsd/sys/dev/safe/safe.c (revision 8b54d874c70d9fb3ed2e059f65c91b3e0eaadbd7)
1b7e3f244SSam Leffler /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4b7e3f244SSam Leffler  * Copyright (c) 2003 Sam Leffler, Errno Consulting
5b7e3f244SSam Leffler  * Copyright (c) 2003 Global Technology Associates, Inc.
6b7e3f244SSam Leffler  * All rights reserved.
7b7e3f244SSam Leffler  *
8b7e3f244SSam Leffler  * Redistribution and use in source and binary forms, with or without
9b7e3f244SSam Leffler  * modification, are permitted provided that the following conditions
10b7e3f244SSam Leffler  * are met:
11b7e3f244SSam Leffler  * 1. Redistributions of source code must retain the above copyright
12b7e3f244SSam Leffler  *    notice, this list of conditions and the following disclaimer.
13b7e3f244SSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
14b7e3f244SSam Leffler  *    notice, this list of conditions and the following disclaimer in the
15b7e3f244SSam Leffler  *    documentation and/or other materials provided with the distribution.
16b7e3f244SSam Leffler  *
17b7e3f244SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18b7e3f244SSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19b7e3f244SSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b7e3f244SSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21b7e3f244SSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22b7e3f244SSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23b7e3f244SSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24b7e3f244SSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25b7e3f244SSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26b7e3f244SSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27b7e3f244SSam Leffler  * SUCH DAMAGE.
28b7e3f244SSam Leffler  */
29b7e3f244SSam Leffler 
30b7e3f244SSam Leffler #include <sys/cdefs.h>
31b7e3f244SSam Leffler /*
32b7e3f244SSam Leffler  * SafeNet SafeXcel-1141 hardware crypto accelerator
33b7e3f244SSam Leffler  */
34b7e3f244SSam Leffler #include "opt_safe.h"
35b7e3f244SSam Leffler 
36b7e3f244SSam Leffler #include <sys/param.h>
37b7e3f244SSam Leffler #include <sys/systm.h>
38b7e3f244SSam Leffler #include <sys/proc.h>
39b7e3f244SSam Leffler #include <sys/errno.h>
40b7e3f244SSam Leffler #include <sys/malloc.h>
41b7e3f244SSam Leffler #include <sys/kernel.h>
42b7e3f244SSam Leffler #include <sys/mbuf.h>
43fe12f24bSPoul-Henning Kamp #include <sys/module.h>
44b7e3f244SSam Leffler #include <sys/lock.h>
45b7e3f244SSam Leffler #include <sys/mutex.h>
46b7e3f244SSam Leffler #include <sys/sysctl.h>
47b7e3f244SSam Leffler #include <sys/endian.h>
48c0341432SJohn Baldwin #include <sys/uio.h>
49b7e3f244SSam Leffler 
50b7e3f244SSam Leffler #include <vm/vm.h>
51b7e3f244SSam Leffler #include <vm/pmap.h>
52b7e3f244SSam Leffler 
53b7e3f244SSam Leffler #include <machine/bus.h>
54b7e3f244SSam Leffler #include <machine/resource.h>
55b7e3f244SSam Leffler #include <sys/bus.h>
56b7e3f244SSam Leffler #include <sys/rman.h>
57b7e3f244SSam Leffler 
58b7e3f244SSam Leffler #include <opencrypto/cryptodev.h>
59c0341432SJohn Baldwin #include <opencrypto/xform_auth.h>
60b7e3f244SSam Leffler #include <sys/random.h>
616810ad6fSSam Leffler #include <sys/kobj.h>
626810ad6fSSam Leffler 
636810ad6fSSam Leffler #include "cryptodev_if.h"
64b7e3f244SSam Leffler 
6590cf0136SWarner Losh #include <dev/pci/pcivar.h>
6690cf0136SWarner Losh #include <dev/pci/pcireg.h>
67b7e3f244SSam Leffler 
68b7e3f244SSam Leffler #ifdef SAFE_RNDTEST
69b7e3f244SSam Leffler #include <dev/rndtest/rndtest.h>
70b7e3f244SSam Leffler #endif
71b7e3f244SSam Leffler #include <dev/safe/safereg.h>
72b7e3f244SSam Leffler #include <dev/safe/safevar.h>
73b7e3f244SSam Leffler 
74b7e3f244SSam Leffler #ifndef bswap32
75b7e3f244SSam Leffler #define	bswap32	NTOHL
76b7e3f244SSam Leffler #endif
77b7e3f244SSam Leffler 
78b7e3f244SSam Leffler /*
79b7e3f244SSam Leffler  * Prototypes and count for the pci_device structure
80b7e3f244SSam Leffler  */
81b7e3f244SSam Leffler static	int safe_probe(device_t);
82b7e3f244SSam Leffler static	int safe_attach(device_t);
83b7e3f244SSam Leffler static	int safe_detach(device_t);
84b7e3f244SSam Leffler static	int safe_suspend(device_t);
85b7e3f244SSam Leffler static	int safe_resume(device_t);
86a6340ec8SWarner Losh static	int safe_shutdown(device_t);
87b7e3f244SSam Leffler 
88c0341432SJohn Baldwin static	int safe_probesession(device_t, const struct crypto_session_params *);
89c0341432SJohn Baldwin static	int safe_newsession(device_t, crypto_session_t,
90c0341432SJohn Baldwin 	    const struct crypto_session_params *);
916810ad6fSSam Leffler static	int safe_process(device_t, struct cryptop *, int);
926810ad6fSSam Leffler 
93b7e3f244SSam Leffler static device_method_t safe_methods[] = {
94b7e3f244SSam Leffler 	/* Device interface */
95b7e3f244SSam Leffler 	DEVMETHOD(device_probe,		safe_probe),
96b7e3f244SSam Leffler 	DEVMETHOD(device_attach,	safe_attach),
97b7e3f244SSam Leffler 	DEVMETHOD(device_detach,	safe_detach),
98b7e3f244SSam Leffler 	DEVMETHOD(device_suspend,	safe_suspend),
99b7e3f244SSam Leffler 	DEVMETHOD(device_resume,	safe_resume),
100b7e3f244SSam Leffler 	DEVMETHOD(device_shutdown,	safe_shutdown),
101b7e3f244SSam Leffler 
1026810ad6fSSam Leffler 	/* crypto device methods */
103c0341432SJohn Baldwin 	DEVMETHOD(cryptodev_probesession, safe_probesession),
1046810ad6fSSam Leffler 	DEVMETHOD(cryptodev_newsession,	safe_newsession),
1056810ad6fSSam Leffler 	DEVMETHOD(cryptodev_process,	safe_process),
1066810ad6fSSam Leffler 
1074b7ec270SMarius Strobl 	DEVMETHOD_END
108b7e3f244SSam Leffler };
109a15fbf0cSJohn Baldwin 
110b7e3f244SSam Leffler static driver_t safe_driver = {
111b7e3f244SSam Leffler 	"safe",
112b7e3f244SSam Leffler 	safe_methods,
113b7e3f244SSam Leffler 	sizeof (struct safe_softc)
114b7e3f244SSam Leffler };
115b7e3f244SSam Leffler 
116a15fbf0cSJohn Baldwin DRIVER_MODULE(safe, pci, safe_driver, 0, 0);
117b7e3f244SSam Leffler MODULE_DEPEND(safe, crypto, 1, 1, 1);
118b7e3f244SSam Leffler #ifdef SAFE_RNDTEST
119b7e3f244SSam Leffler MODULE_DEPEND(safe, rndtest, 1, 1, 1);
120b7e3f244SSam Leffler #endif
121b7e3f244SSam Leffler 
122b7e3f244SSam Leffler static	void safe_intr(void *);
123b7e3f244SSam Leffler static	void safe_callback(struct safe_softc *, struct safe_ringentry *);
124b7e3f244SSam Leffler static	void safe_feed(struct safe_softc *, struct safe_ringentry *);
125b7e3f244SSam Leffler static	void safe_mcopy(struct mbuf *, struct mbuf *, u_int);
126b7e3f244SSam Leffler #ifndef SAFE_NO_RNG
127b7e3f244SSam Leffler static	void safe_rng_init(struct safe_softc *);
128b7e3f244SSam Leffler static	void safe_rng(void *);
129b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */
130b7e3f244SSam Leffler static	int safe_dma_malloc(struct safe_softc *, bus_size_t,
131b7e3f244SSam Leffler 	        struct safe_dma_alloc *, int);
132b7e3f244SSam Leffler #define	safe_dma_sync(_dma, _flags) \
133b7e3f244SSam Leffler 	bus_dmamap_sync((_dma)->dma_tag, (_dma)->dma_map, (_flags))
134b7e3f244SSam Leffler static	void safe_dma_free(struct safe_softc *, struct safe_dma_alloc *);
135b7e3f244SSam Leffler static	int safe_dmamap_aligned(const struct safe_operand *);
136b7e3f244SSam Leffler static	int safe_dmamap_uniform(const struct safe_operand *);
137b7e3f244SSam Leffler 
138b7e3f244SSam Leffler static	void safe_reset_board(struct safe_softc *);
139b7e3f244SSam Leffler static	void safe_init_board(struct safe_softc *);
140b7e3f244SSam Leffler static	void safe_init_pciregs(device_t dev);
141b7e3f244SSam Leffler static	void safe_cleanchip(struct safe_softc *);
142b7e3f244SSam Leffler static	void safe_totalreset(struct safe_softc *);
143b7e3f244SSam Leffler 
144b7e3f244SSam Leffler static	int safe_free_entry(struct safe_softc *, struct safe_ringentry *);
145b7e3f244SSam Leffler 
1467029da5cSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, safe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1476472ac3dSEd Schouten     "SafeNet driver parameters");
148b7e3f244SSam Leffler 
149b7e3f244SSam Leffler #ifdef SAFE_DEBUG
150b7e3f244SSam Leffler static	void safe_dump_dmastatus(struct safe_softc *, const char *);
151b7e3f244SSam Leffler static	void safe_dump_ringstate(struct safe_softc *, const char *);
152b7e3f244SSam Leffler static	void safe_dump_intrstate(struct safe_softc *, const char *);
153b7e3f244SSam Leffler static	void safe_dump_request(struct safe_softc *, const char *,
154b7e3f244SSam Leffler 		struct safe_ringentry *);
155b7e3f244SSam Leffler 
156b7e3f244SSam Leffler static	struct safe_softc *safec;		/* for use by hw.safe.dump */
157b7e3f244SSam Leffler 
158b7e3f244SSam Leffler static	int safe_debug = 0;
159b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, debug, CTLFLAG_RW, &safe_debug,
160b7e3f244SSam Leffler 	    0, "control debugging msgs");
161b7e3f244SSam Leffler #define	DPRINTF(_x)	if (safe_debug) printf _x
162b7e3f244SSam Leffler #else
163b7e3f244SSam Leffler #define	DPRINTF(_x)
164b7e3f244SSam Leffler #endif
165b7e3f244SSam Leffler 
166b7e3f244SSam Leffler #define	READ_REG(sc,r) \
167b7e3f244SSam Leffler 	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (r))
168b7e3f244SSam Leffler 
169b7e3f244SSam Leffler #define WRITE_REG(sc,reg,val) \
170b7e3f244SSam Leffler 	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, reg, val)
171b7e3f244SSam Leffler 
172b7e3f244SSam Leffler struct safe_stats safestats;
173b7e3f244SSam Leffler SYSCTL_STRUCT(_hw_safe, OID_AUTO, stats, CTLFLAG_RD, &safestats,
174b7e3f244SSam Leffler 	    safe_stats, "driver statistics");
175b7e3f244SSam Leffler #ifndef SAFE_NO_RNG
176b7e3f244SSam Leffler static	int safe_rnginterval = 1;		/* poll once a second */
177b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, rnginterval, CTLFLAG_RW, &safe_rnginterval,
178b7e3f244SSam Leffler 	    0, "RNG polling interval (secs)");
179b7e3f244SSam Leffler static	int safe_rngbufsize = 16;		/* 64 bytes each poll  */
180b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, rngbufsize, CTLFLAG_RW, &safe_rngbufsize,
181b7e3f244SSam Leffler 	    0, "RNG polling buffer size (32-bit words)");
182b7e3f244SSam Leffler static	int safe_rngmaxalarm = 8;		/* max alarms before reset */
183b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, rngmaxalarm, CTLFLAG_RW, &safe_rngmaxalarm,
184b7e3f244SSam Leffler 	    0, "RNG max alarms before reset");
185b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */
186b7e3f244SSam Leffler 
187b7e3f244SSam Leffler static int
safe_probe(device_t dev)188b7e3f244SSam Leffler safe_probe(device_t dev)
189b7e3f244SSam Leffler {
190b7e3f244SSam Leffler 	if (pci_get_vendor(dev) == PCI_VENDOR_SAFENET &&
191b7e3f244SSam Leffler 	    pci_get_device(dev) == PCI_PRODUCT_SAFEXCEL)
192d2b677bbSWarner Losh 		return (BUS_PROBE_DEFAULT);
193b7e3f244SSam Leffler 	return (ENXIO);
194b7e3f244SSam Leffler }
195b7e3f244SSam Leffler 
196b7e3f244SSam Leffler static const char*
safe_partname(struct safe_softc * sc)197b7e3f244SSam Leffler safe_partname(struct safe_softc *sc)
198b7e3f244SSam Leffler {
199b7e3f244SSam Leffler 	/* XXX sprintf numbers when not decoded */
200b7e3f244SSam Leffler 	switch (pci_get_vendor(sc->sc_dev)) {
201b7e3f244SSam Leffler 	case PCI_VENDOR_SAFENET:
202b7e3f244SSam Leffler 		switch (pci_get_device(sc->sc_dev)) {
203b7e3f244SSam Leffler 		case PCI_PRODUCT_SAFEXCEL: return "SafeNet SafeXcel-1141";
204b7e3f244SSam Leffler 		}
205b7e3f244SSam Leffler 		return "SafeNet unknown-part";
206b7e3f244SSam Leffler 	}
207b7e3f244SSam Leffler 	return "Unknown-vendor unknown-part";
208b7e3f244SSam Leffler }
209b7e3f244SSam Leffler 
210b7e3f244SSam Leffler #ifndef SAFE_NO_RNG
211b7e3f244SSam Leffler static void
default_harvest(struct rndtest_state * rsp,void * buf,u_int count)212b7e3f244SSam Leffler default_harvest(struct rndtest_state *rsp, void *buf, u_int count)
213b7e3f244SSam Leffler {
214d1b06863SMark Murray 	/* MarkM: FIX!! Check that this does not swamp the harvester! */
21519fa89e9SMark Murray 	random_harvest_queue(buf, count, RANDOM_PURE_SAFE);
216b7e3f244SSam Leffler }
217b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */
218b7e3f244SSam Leffler 
219b7e3f244SSam Leffler static int
safe_attach(device_t dev)220b7e3f244SSam Leffler safe_attach(device_t dev)
221b7e3f244SSam Leffler {
222b7e3f244SSam Leffler 	struct safe_softc *sc = device_get_softc(dev);
223b7e3f244SSam Leffler 	u_int32_t raddr;
224c0341432SJohn Baldwin 	u_int32_t i;
225b7e3f244SSam Leffler 	int rid;
226b7e3f244SSam Leffler 
227b7e3f244SSam Leffler 	bzero(sc, sizeof (*sc));
228b7e3f244SSam Leffler 	sc->sc_dev = dev;
229b7e3f244SSam Leffler 
230b7e3f244SSam Leffler 	/* XXX handle power management */
231b7e3f244SSam Leffler 
232c68534f1SScott Long 	pci_enable_busmaster(dev);
233b7e3f244SSam Leffler 
234b7e3f244SSam Leffler 	/*
235b7e3f244SSam Leffler 	 * Setup memory-mapping of PCI registers.
236b7e3f244SSam Leffler 	 */
237b7e3f244SSam Leffler 	rid = BS_BAR;
2385f96beb9SNate Lawson 	sc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2395f96beb9SNate Lawson 					   RF_ACTIVE);
240b7e3f244SSam Leffler 	if (sc->sc_sr == NULL) {
241b7e3f244SSam Leffler 		device_printf(dev, "cannot map register space\n");
242b7e3f244SSam Leffler 		goto bad;
243b7e3f244SSam Leffler 	}
244b7e3f244SSam Leffler 	sc->sc_st = rman_get_bustag(sc->sc_sr);
245b7e3f244SSam Leffler 	sc->sc_sh = rman_get_bushandle(sc->sc_sr);
246b7e3f244SSam Leffler 
247b7e3f244SSam Leffler 	/*
248b7e3f244SSam Leffler 	 * Arrange interrupt line.
249b7e3f244SSam Leffler 	 */
250b7e3f244SSam Leffler 	rid = 0;
2515f96beb9SNate Lawson 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2525f96beb9SNate Lawson 					    RF_SHAREABLE|RF_ACTIVE);
253b7e3f244SSam Leffler 	if (sc->sc_irq == NULL) {
254b7e3f244SSam Leffler 		device_printf(dev, "could not map interrupt\n");
255b7e3f244SSam Leffler 		goto bad1;
256b7e3f244SSam Leffler 	}
257b7e3f244SSam Leffler 	/*
258b7e3f244SSam Leffler 	 * NB: Network code assumes we are blocked with splimp()
259b7e3f244SSam Leffler 	 *     so make sure the IRQ is mapped appropriately.
260b7e3f244SSam Leffler 	 */
261b7e3f244SSam Leffler 	if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_NET | INTR_MPSAFE,
262ef544f63SPaolo Pisati 			   NULL, safe_intr, sc, &sc->sc_ih)) {
263b7e3f244SSam Leffler 		device_printf(dev, "could not establish interrupt\n");
264b7e3f244SSam Leffler 		goto bad2;
265b7e3f244SSam Leffler 	}
266b7e3f244SSam Leffler 
2671b0909d5SConrad Meyer 	sc->sc_cid = crypto_get_driverid(dev, sizeof(struct safe_session),
2681b0909d5SConrad Meyer 	    CRYPTOCAP_F_HARDWARE);
269b7e3f244SSam Leffler 	if (sc->sc_cid < 0) {
270b7e3f244SSam Leffler 		device_printf(dev, "could not get crypto driver id\n");
271b7e3f244SSam Leffler 		goto bad3;
272b7e3f244SSam Leffler 	}
273b7e3f244SSam Leffler 
274b7e3f244SSam Leffler 	sc->sc_chiprev = READ_REG(sc, SAFE_DEVINFO) &
275b7e3f244SSam Leffler 		(SAFE_DEVINFO_REV_MAJ | SAFE_DEVINFO_REV_MIN);
276b7e3f244SSam Leffler 
277b7e3f244SSam Leffler 	/*
278b7e3f244SSam Leffler 	 * Setup DMA descriptor area.
279b7e3f244SSam Leffler 	 */
28062ce43ccSScott Long 	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
281b7e3f244SSam Leffler 			       1,			/* alignment */
282b7e3f244SSam Leffler 			       SAFE_DMA_BOUNDARY,	/* boundary */
283b7e3f244SSam Leffler 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
284b7e3f244SSam Leffler 			       BUS_SPACE_MAXADDR,	/* highaddr */
285b7e3f244SSam Leffler 			       NULL, NULL,		/* filter, filterarg */
286b7e3f244SSam Leffler 			       SAFE_MAX_DMA,		/* maxsize */
287b7e3f244SSam Leffler 			       SAFE_MAX_PART,		/* nsegments */
288b7e3f244SSam Leffler 			       SAFE_MAX_SSIZE,		/* maxsegsize */
289b7e3f244SSam Leffler 			       BUS_DMA_ALLOCNOW,	/* flags */
290b7e3f244SSam Leffler 			       NULL, NULL,		/* locking */
291b7e3f244SSam Leffler 			       &sc->sc_srcdmat)) {
292b7e3f244SSam Leffler 		device_printf(dev, "cannot allocate DMA tag\n");
293b7e3f244SSam Leffler 		goto bad4;
294b7e3f244SSam Leffler 	}
29562ce43ccSScott Long 	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
29688bba874SSam Leffler 			       1,			/* alignment */
297b7e3f244SSam Leffler 			       SAFE_MAX_DSIZE,		/* boundary */
298b7e3f244SSam Leffler 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
299b7e3f244SSam Leffler 			       BUS_SPACE_MAXADDR,	/* highaddr */
300b7e3f244SSam Leffler 			       NULL, NULL,		/* filter, filterarg */
301b7e3f244SSam Leffler 			       SAFE_MAX_DMA,		/* maxsize */
302b7e3f244SSam Leffler 			       SAFE_MAX_PART,		/* nsegments */
303b7e3f244SSam Leffler 			       SAFE_MAX_DSIZE,		/* maxsegsize */
304b7e3f244SSam Leffler 			       BUS_DMA_ALLOCNOW,	/* flags */
305b7e3f244SSam Leffler 			       NULL, NULL,		/* locking */
306b7e3f244SSam Leffler 			       &sc->sc_dstdmat)) {
307b7e3f244SSam Leffler 		device_printf(dev, "cannot allocate DMA tag\n");
308b7e3f244SSam Leffler 		goto bad4;
309b7e3f244SSam Leffler 	}
310b7e3f244SSam Leffler 
311b7e3f244SSam Leffler 	/*
312b7e3f244SSam Leffler 	 * Allocate packet engine descriptors.
313b7e3f244SSam Leffler 	 */
314b7e3f244SSam Leffler 	if (safe_dma_malloc(sc,
315b7e3f244SSam Leffler 	    SAFE_MAX_NQUEUE * sizeof (struct safe_ringentry),
316b7e3f244SSam Leffler 	    &sc->sc_ringalloc, 0)) {
317b7e3f244SSam Leffler 		device_printf(dev, "cannot allocate PE descriptor ring\n");
318b7e3f244SSam Leffler 		bus_dma_tag_destroy(sc->sc_srcdmat);
319b7e3f244SSam Leffler 		goto bad4;
320b7e3f244SSam Leffler 	}
321b7e3f244SSam Leffler 	/*
322b7e3f244SSam Leffler 	 * Hookup the static portion of all our data structures.
323b7e3f244SSam Leffler 	 */
324b7e3f244SSam Leffler 	sc->sc_ring = (struct safe_ringentry *) sc->sc_ringalloc.dma_vaddr;
325b7e3f244SSam Leffler 	sc->sc_ringtop = sc->sc_ring + SAFE_MAX_NQUEUE;
326b7e3f244SSam Leffler 	sc->sc_front = sc->sc_ring;
327b7e3f244SSam Leffler 	sc->sc_back = sc->sc_ring;
328b7e3f244SSam Leffler 	raddr = sc->sc_ringalloc.dma_paddr;
329b7e3f244SSam Leffler 	bzero(sc->sc_ring, SAFE_MAX_NQUEUE * sizeof(struct safe_ringentry));
330b7e3f244SSam Leffler 	for (i = 0; i < SAFE_MAX_NQUEUE; i++) {
331b7e3f244SSam Leffler 		struct safe_ringentry *re = &sc->sc_ring[i];
332b7e3f244SSam Leffler 
333b7e3f244SSam Leffler 		re->re_desc.d_sa = raddr +
334b7e3f244SSam Leffler 			offsetof(struct safe_ringentry, re_sa);
335b7e3f244SSam Leffler 		re->re_sa.sa_staterec = raddr +
336b7e3f244SSam Leffler 			offsetof(struct safe_ringentry, re_sastate);
337b7e3f244SSam Leffler 
338b7e3f244SSam Leffler 		raddr += sizeof (struct safe_ringentry);
339b7e3f244SSam Leffler 	}
340b7e3f244SSam Leffler 	mtx_init(&sc->sc_ringmtx, device_get_nameunit(dev),
341b7e3f244SSam Leffler 		"packet engine ring", MTX_DEF);
342b7e3f244SSam Leffler 
343b7e3f244SSam Leffler 	/*
344b7e3f244SSam Leffler 	 * Allocate scatter and gather particle descriptors.
345b7e3f244SSam Leffler 	 */
346b7e3f244SSam Leffler 	if (safe_dma_malloc(sc, SAFE_TOTAL_SPART * sizeof (struct safe_pdesc),
347b7e3f244SSam Leffler 	    &sc->sc_spalloc, 0)) {
348b7e3f244SSam Leffler 		device_printf(dev, "cannot allocate source particle "
349b7e3f244SSam Leffler 			"descriptor ring\n");
350b7e3f244SSam Leffler 		mtx_destroy(&sc->sc_ringmtx);
351b7e3f244SSam Leffler 		safe_dma_free(sc, &sc->sc_ringalloc);
352b7e3f244SSam Leffler 		bus_dma_tag_destroy(sc->sc_srcdmat);
353b7e3f244SSam Leffler 		goto bad4;
354b7e3f244SSam Leffler 	}
355b7e3f244SSam Leffler 	sc->sc_spring = (struct safe_pdesc *) sc->sc_spalloc.dma_vaddr;
356b7e3f244SSam Leffler 	sc->sc_springtop = sc->sc_spring + SAFE_TOTAL_SPART;
357b7e3f244SSam Leffler 	sc->sc_spfree = sc->sc_spring;
358b7e3f244SSam Leffler 	bzero(sc->sc_spring, SAFE_TOTAL_SPART * sizeof(struct safe_pdesc));
359b7e3f244SSam Leffler 
360b7e3f244SSam Leffler 	if (safe_dma_malloc(sc, SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
361b7e3f244SSam Leffler 	    &sc->sc_dpalloc, 0)) {
362b7e3f244SSam Leffler 		device_printf(dev, "cannot allocate destination particle "
363b7e3f244SSam Leffler 			"descriptor ring\n");
364b7e3f244SSam Leffler 		mtx_destroy(&sc->sc_ringmtx);
365b7e3f244SSam Leffler 		safe_dma_free(sc, &sc->sc_spalloc);
366b7e3f244SSam Leffler 		safe_dma_free(sc, &sc->sc_ringalloc);
367b7e3f244SSam Leffler 		bus_dma_tag_destroy(sc->sc_dstdmat);
368b7e3f244SSam Leffler 		goto bad4;
369b7e3f244SSam Leffler 	}
370b7e3f244SSam Leffler 	sc->sc_dpring = (struct safe_pdesc *) sc->sc_dpalloc.dma_vaddr;
371b7e3f244SSam Leffler 	sc->sc_dpringtop = sc->sc_dpring + SAFE_TOTAL_DPART;
372b7e3f244SSam Leffler 	sc->sc_dpfree = sc->sc_dpring;
373b7e3f244SSam Leffler 	bzero(sc->sc_dpring, SAFE_TOTAL_DPART * sizeof(struct safe_pdesc));
374b7e3f244SSam Leffler 
375b7e3f244SSam Leffler 	device_printf(sc->sc_dev, "%s", safe_partname(sc));
376b7e3f244SSam Leffler 
377c0341432SJohn Baldwin 	sc->sc_devinfo = READ_REG(sc, SAFE_DEVINFO);
378c0341432SJohn Baldwin 	if (sc->sc_devinfo & SAFE_DEVINFO_RNG) {
379b7e3f244SSam Leffler 		sc->sc_flags |= SAFE_FLAGS_RNG;
380b7e3f244SSam Leffler 		printf(" rng");
381b7e3f244SSam Leffler 	}
382c0341432SJohn Baldwin 	if (sc->sc_devinfo & SAFE_DEVINFO_PKEY) {
383b7e3f244SSam Leffler #if 0
384b7e3f244SSam Leffler 		printf(" key");
385b7e3f244SSam Leffler 		sc->sc_flags |= SAFE_FLAGS_KEY;
386b7e3f244SSam Leffler #endif
387b7e3f244SSam Leffler 	}
388c0341432SJohn Baldwin 	if (sc->sc_devinfo & SAFE_DEVINFO_DES) {
389b7e3f244SSam Leffler 		printf(" des/3des");
390b7e3f244SSam Leffler 	}
391c0341432SJohn Baldwin 	if (sc->sc_devinfo & SAFE_DEVINFO_AES) {
392b7e3f244SSam Leffler 		printf(" aes");
393b7e3f244SSam Leffler 	}
394c0341432SJohn Baldwin 	if (sc->sc_devinfo & SAFE_DEVINFO_MD5) {
395b7e3f244SSam Leffler 		printf(" md5");
396b7e3f244SSam Leffler 	}
397c0341432SJohn Baldwin 	if (sc->sc_devinfo & SAFE_DEVINFO_SHA1) {
398b7e3f244SSam Leffler 		printf(" sha1");
399b7e3f244SSam Leffler 	}
400b7e3f244SSam Leffler 	/* XXX other supported algorithms */
401b7e3f244SSam Leffler 	printf("\n");
402b7e3f244SSam Leffler 
403b7e3f244SSam Leffler 	safe_reset_board(sc);		/* reset h/w */
404b7e3f244SSam Leffler 	safe_init_pciregs(dev);		/* init pci settings */
405b7e3f244SSam Leffler 	safe_init_board(sc);		/* init h/w */
406b7e3f244SSam Leffler 
407b7e3f244SSam Leffler #ifndef SAFE_NO_RNG
408b7e3f244SSam Leffler 	if (sc->sc_flags & SAFE_FLAGS_RNG) {
409b7e3f244SSam Leffler #ifdef SAFE_RNDTEST
410b7e3f244SSam Leffler 		sc->sc_rndtest = rndtest_attach(dev);
411b7e3f244SSam Leffler 		if (sc->sc_rndtest)
412b7e3f244SSam Leffler 			sc->sc_harvest = rndtest_harvest;
413b7e3f244SSam Leffler 		else
414b7e3f244SSam Leffler 			sc->sc_harvest = default_harvest;
415b7e3f244SSam Leffler #else
416b7e3f244SSam Leffler 		sc->sc_harvest = default_harvest;
417b7e3f244SSam Leffler #endif
418b7e3f244SSam Leffler 		safe_rng_init(sc);
419b7e3f244SSam Leffler 
420fd90e2edSJung-uk Kim 		callout_init(&sc->sc_rngto, 1);
421b7e3f244SSam Leffler 		callout_reset(&sc->sc_rngto, hz*safe_rnginterval, safe_rng, sc);
422b7e3f244SSam Leffler 	}
423b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */
424b7e3f244SSam Leffler #ifdef SAFE_DEBUG
425b7e3f244SSam Leffler 	safec = sc;			/* for use by hw.safe.dump */
426b7e3f244SSam Leffler #endif
427b7e3f244SSam Leffler 	return (0);
428b7e3f244SSam Leffler bad4:
429b7e3f244SSam Leffler 	crypto_unregister_all(sc->sc_cid);
430b7e3f244SSam Leffler bad3:
431b7e3f244SSam Leffler 	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
432b7e3f244SSam Leffler bad2:
433b7e3f244SSam Leffler 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
434b7e3f244SSam Leffler bad1:
435b7e3f244SSam Leffler 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
436b7e3f244SSam Leffler bad:
437b7e3f244SSam Leffler 	return (ENXIO);
438b7e3f244SSam Leffler }
439b7e3f244SSam Leffler 
440b7e3f244SSam Leffler /*
441b7e3f244SSam Leffler  * Detach a device that successfully probed.
442b7e3f244SSam Leffler  */
443b7e3f244SSam Leffler static int
safe_detach(device_t dev)444b7e3f244SSam Leffler safe_detach(device_t dev)
445b7e3f244SSam Leffler {
446b7e3f244SSam Leffler 	struct safe_softc *sc = device_get_softc(dev);
447b7e3f244SSam Leffler 
448b7e3f244SSam Leffler 	/* XXX wait/abort active ops */
449b7e3f244SSam Leffler 
450b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_HI_MASK, 0);		/* disable interrupts */
451b7e3f244SSam Leffler 
452b7e3f244SSam Leffler 	callout_stop(&sc->sc_rngto);
453b7e3f244SSam Leffler 
454b7e3f244SSam Leffler 	crypto_unregister_all(sc->sc_cid);
455b7e3f244SSam Leffler 
456b7e3f244SSam Leffler #ifdef SAFE_RNDTEST
457b7e3f244SSam Leffler 	if (sc->sc_rndtest)
458b7e3f244SSam Leffler 		rndtest_detach(sc->sc_rndtest);
459b7e3f244SSam Leffler #endif
460b7e3f244SSam Leffler 
461b7e3f244SSam Leffler 	safe_cleanchip(sc);
462b7e3f244SSam Leffler 	safe_dma_free(sc, &sc->sc_dpalloc);
463b7e3f244SSam Leffler 	safe_dma_free(sc, &sc->sc_spalloc);
464b7e3f244SSam Leffler 	mtx_destroy(&sc->sc_ringmtx);
465b7e3f244SSam Leffler 	safe_dma_free(sc, &sc->sc_ringalloc);
466b7e3f244SSam Leffler 
467b7e3f244SSam Leffler 	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
468b7e3f244SSam Leffler 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
469b7e3f244SSam Leffler 
470b7e3f244SSam Leffler 	bus_dma_tag_destroy(sc->sc_srcdmat);
471b7e3f244SSam Leffler 	bus_dma_tag_destroy(sc->sc_dstdmat);
472b7e3f244SSam Leffler 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
473b7e3f244SSam Leffler 
474b7e3f244SSam Leffler 	return (0);
475b7e3f244SSam Leffler }
476b7e3f244SSam Leffler 
477b7e3f244SSam Leffler /*
478b7e3f244SSam Leffler  * Stop all chip i/o so that the kernel's probe routines don't
479b7e3f244SSam Leffler  * get confused by errant DMAs when rebooting.
480b7e3f244SSam Leffler  */
481a6340ec8SWarner Losh static int
safe_shutdown(device_t dev)482b7e3f244SSam Leffler safe_shutdown(device_t dev)
483b7e3f244SSam Leffler {
484b7e3f244SSam Leffler #ifdef notyet
485b7e3f244SSam Leffler 	safe_stop(device_get_softc(dev));
486b7e3f244SSam Leffler #endif
487a6340ec8SWarner Losh 	return (0);
488b7e3f244SSam Leffler }
489b7e3f244SSam Leffler 
490b7e3f244SSam Leffler /*
491b7e3f244SSam Leffler  * Device suspend routine.
492b7e3f244SSam Leffler  */
493b7e3f244SSam Leffler static int
safe_suspend(device_t dev)494b7e3f244SSam Leffler safe_suspend(device_t dev)
495b7e3f244SSam Leffler {
496b7e3f244SSam Leffler 	struct safe_softc *sc = device_get_softc(dev);
497b7e3f244SSam Leffler 
498b7e3f244SSam Leffler #ifdef notyet
499b7e3f244SSam Leffler 	/* XXX stop the device and save PCI settings */
500b7e3f244SSam Leffler #endif
501b7e3f244SSam Leffler 	sc->sc_suspended = 1;
502b7e3f244SSam Leffler 
503b7e3f244SSam Leffler 	return (0);
504b7e3f244SSam Leffler }
505b7e3f244SSam Leffler 
506b7e3f244SSam Leffler static int
safe_resume(device_t dev)507b7e3f244SSam Leffler safe_resume(device_t dev)
508b7e3f244SSam Leffler {
509b7e3f244SSam Leffler 	struct safe_softc *sc = device_get_softc(dev);
510b7e3f244SSam Leffler 
511b7e3f244SSam Leffler #ifdef notyet
512b7e3f244SSam Leffler 	/* XXX retore PCI settings and start the device */
513b7e3f244SSam Leffler #endif
514b7e3f244SSam Leffler 	sc->sc_suspended = 0;
515b7e3f244SSam Leffler 	return (0);
516b7e3f244SSam Leffler }
517b7e3f244SSam Leffler 
518b7e3f244SSam Leffler /*
519b7e3f244SSam Leffler  * SafeXcel Interrupt routine
520b7e3f244SSam Leffler  */
521b7e3f244SSam Leffler static void
safe_intr(void * arg)522b7e3f244SSam Leffler safe_intr(void *arg)
523b7e3f244SSam Leffler {
524b7e3f244SSam Leffler 	struct safe_softc *sc = arg;
525b7e3f244SSam Leffler 	volatile u_int32_t stat;
526b7e3f244SSam Leffler 
527b7e3f244SSam Leffler 	stat = READ_REG(sc, SAFE_HM_STAT);
528b7e3f244SSam Leffler 	if (stat == 0)			/* shared irq, not for us */
529b7e3f244SSam Leffler 		return;
530b7e3f244SSam Leffler 
531b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_HI_CLR, stat);	/* IACK */
532b7e3f244SSam Leffler 
533b7e3f244SSam Leffler 	if ((stat & SAFE_INT_PE_DDONE)) {
534b7e3f244SSam Leffler 		/*
535b7e3f244SSam Leffler 		 * Descriptor(s) done; scan the ring and
536b7e3f244SSam Leffler 		 * process completed operations.
537b7e3f244SSam Leffler 		 */
538b7e3f244SSam Leffler 		mtx_lock(&sc->sc_ringmtx);
539b7e3f244SSam Leffler 		while (sc->sc_back != sc->sc_front) {
540b7e3f244SSam Leffler 			struct safe_ringentry *re = sc->sc_back;
541b7e3f244SSam Leffler #ifdef SAFE_DEBUG
542b7e3f244SSam Leffler 			if (safe_debug) {
543b7e3f244SSam Leffler 				safe_dump_ringstate(sc, __func__);
544b7e3f244SSam Leffler 				safe_dump_request(sc, __func__, re);
545b7e3f244SSam Leffler 			}
546b7e3f244SSam Leffler #endif
547b7e3f244SSam Leffler 			/*
548b7e3f244SSam Leffler 			 * safe_process marks ring entries that were allocated
549b7e3f244SSam Leffler 			 * but not used with a csr of zero.  This insures the
550b7e3f244SSam Leffler 			 * ring front pointer never needs to be set backwards
551b7e3f244SSam Leffler 			 * in the event that an entry is allocated but not used
552b7e3f244SSam Leffler 			 * because of a setup error.
553b7e3f244SSam Leffler 			 */
554b7e3f244SSam Leffler 			if (re->re_desc.d_csr != 0) {
555b7e3f244SSam Leffler 				if (!SAFE_PE_CSR_IS_DONE(re->re_desc.d_csr))
556b7e3f244SSam Leffler 					break;
557b7e3f244SSam Leffler 				if (!SAFE_PE_LEN_IS_DONE(re->re_desc.d_len))
558b7e3f244SSam Leffler 					break;
559b7e3f244SSam Leffler 				sc->sc_nqchip--;
560b7e3f244SSam Leffler 				safe_callback(sc, re);
561b7e3f244SSam Leffler 			}
562b7e3f244SSam Leffler 			if (++(sc->sc_back) == sc->sc_ringtop)
563b7e3f244SSam Leffler 				sc->sc_back = sc->sc_ring;
564b7e3f244SSam Leffler 		}
565b7e3f244SSam Leffler 		mtx_unlock(&sc->sc_ringmtx);
566b7e3f244SSam Leffler 	}
567b7e3f244SSam Leffler 
568b7e3f244SSam Leffler 	/*
569b7e3f244SSam Leffler 	 * Check to see if we got any DMA Error
570b7e3f244SSam Leffler 	 */
571b7e3f244SSam Leffler 	if (stat & SAFE_INT_PE_ERROR) {
572b7e3f244SSam Leffler 		DPRINTF(("dmaerr dmastat %08x\n",
573b7e3f244SSam Leffler 			READ_REG(sc, SAFE_PE_DMASTAT)));
574b7e3f244SSam Leffler 		safestats.st_dmaerr++;
575b7e3f244SSam Leffler 		safe_totalreset(sc);
576b7e3f244SSam Leffler #if 0
577b7e3f244SSam Leffler 		safe_feed(sc);
578b7e3f244SSam Leffler #endif
579b7e3f244SSam Leffler 	}
580b7e3f244SSam Leffler 
581b7e3f244SSam Leffler 	if (sc->sc_needwakeup) {		/* XXX check high watermark */
58276681661SJohn Baldwin 		int wakeup = sc->sc_needwakeup & CRYPTO_SYMQ;
583b7e3f244SSam Leffler 		DPRINTF(("%s: wakeup crypto %x\n", __func__,
584b7e3f244SSam Leffler 			sc->sc_needwakeup));
585b7e3f244SSam Leffler 		sc->sc_needwakeup &= ~wakeup;
586b7e3f244SSam Leffler 		crypto_unblock(sc->sc_cid, wakeup);
587b7e3f244SSam Leffler 	}
588b7e3f244SSam Leffler }
589b7e3f244SSam Leffler 
590b7e3f244SSam Leffler /*
591b7e3f244SSam Leffler  * safe_feed() - post a request to chip
592b7e3f244SSam Leffler  */
593b7e3f244SSam Leffler static void
safe_feed(struct safe_softc * sc,struct safe_ringentry * re)594b7e3f244SSam Leffler safe_feed(struct safe_softc *sc, struct safe_ringentry *re)
595b7e3f244SSam Leffler {
596b7e3f244SSam Leffler 	bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_PREWRITE);
597b7e3f244SSam Leffler 	if (re->re_dst_map != NULL)
598b7e3f244SSam Leffler 		bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map,
599b7e3f244SSam Leffler 			BUS_DMASYNC_PREREAD);
600b7e3f244SSam Leffler 	/* XXX have no smaller granularity */
601b7e3f244SSam Leffler 	safe_dma_sync(&sc->sc_ringalloc,
602b7e3f244SSam Leffler 		BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
603b7e3f244SSam Leffler 	safe_dma_sync(&sc->sc_spalloc, BUS_DMASYNC_PREWRITE);
604b7e3f244SSam Leffler 	safe_dma_sync(&sc->sc_dpalloc, BUS_DMASYNC_PREWRITE);
605b7e3f244SSam Leffler 
606b7e3f244SSam Leffler #ifdef SAFE_DEBUG
607b7e3f244SSam Leffler 	if (safe_debug) {
608b7e3f244SSam Leffler 		safe_dump_ringstate(sc, __func__);
609b7e3f244SSam Leffler 		safe_dump_request(sc, __func__, re);
610b7e3f244SSam Leffler 	}
611b7e3f244SSam Leffler #endif
612b7e3f244SSam Leffler 	sc->sc_nqchip++;
613b7e3f244SSam Leffler 	if (sc->sc_nqchip > safestats.st_maxqchip)
614b7e3f244SSam Leffler 		safestats.st_maxqchip = sc->sc_nqchip;
615b7e3f244SSam Leffler 	/* poke h/w to check descriptor ring, any value can be written */
616b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_HI_RD_DESCR, 0);
617b7e3f244SSam Leffler }
618b7e3f244SSam Leffler 
6199a2f6061SPawel Jakub Dawidek #define	N(a)	(sizeof(a) / sizeof (a[0]))
6209a2f6061SPawel Jakub Dawidek static void
safe_setup_enckey(struct safe_session * ses,const void * key)621c0341432SJohn Baldwin safe_setup_enckey(struct safe_session *ses, const void *key)
6229a2f6061SPawel Jakub Dawidek {
6239a2f6061SPawel Jakub Dawidek 	int i;
6249a2f6061SPawel Jakub Dawidek 
625c0341432SJohn Baldwin 	bcopy(key, ses->ses_key, ses->ses_klen);
6269a2f6061SPawel Jakub Dawidek 
6279a2f6061SPawel Jakub Dawidek 	/* PE is little-endian, insure proper byte order */
6289a2f6061SPawel Jakub Dawidek 	for (i = 0; i < N(ses->ses_key); i++)
6299a2f6061SPawel Jakub Dawidek 		ses->ses_key[i] = htole32(ses->ses_key[i]);
6309a2f6061SPawel Jakub Dawidek }
6319a2f6061SPawel Jakub Dawidek 
6329a2f6061SPawel Jakub Dawidek static void
safe_setup_mackey(struct safe_session * ses,int algo,const uint8_t * key,int klen)633c0341432SJohn Baldwin safe_setup_mackey(struct safe_session *ses, int algo, const uint8_t *key,
634c0341432SJohn Baldwin     int klen)
6359a2f6061SPawel Jakub Dawidek {
6369a2f6061SPawel Jakub Dawidek 	SHA1_CTX sha1ctx;
6379a2f6061SPawel Jakub Dawidek 	int i;
6389a2f6061SPawel Jakub Dawidek 
639c0341432SJohn Baldwin 	hmac_init_ipad(&auth_hash_hmac_sha1, key, klen, &sha1ctx);
640c0341432SJohn Baldwin 	bcopy(sha1ctx.h.b32, ses->ses_hminner, sizeof(sha1ctx.h.b32));
641c0341432SJohn Baldwin 
642c0341432SJohn Baldwin 	hmac_init_opad(&auth_hash_hmac_sha1, key, klen, &sha1ctx);
643c0341432SJohn Baldwin 	bcopy(sha1ctx.h.b32, ses->ses_hmouter, sizeof(sha1ctx.h.b32));
644c0341432SJohn Baldwin 
645c0341432SJohn Baldwin 	explicit_bzero(&sha1ctx, sizeof(sha1ctx));
6469a2f6061SPawel Jakub Dawidek 
6479a2f6061SPawel Jakub Dawidek 	/* PE is little-endian, insure proper byte order */
6489a2f6061SPawel Jakub Dawidek 	for (i = 0; i < N(ses->ses_hminner); i++) {
6499a2f6061SPawel Jakub Dawidek 		ses->ses_hminner[i] = htole32(ses->ses_hminner[i]);
6509a2f6061SPawel Jakub Dawidek 		ses->ses_hmouter[i] = htole32(ses->ses_hmouter[i]);
6519a2f6061SPawel Jakub Dawidek 	}
6529a2f6061SPawel Jakub Dawidek }
6539a2f6061SPawel Jakub Dawidek #undef N
6549a2f6061SPawel Jakub Dawidek 
655c0341432SJohn Baldwin static bool
safe_auth_supported(struct safe_softc * sc,const struct crypto_session_params * csp)656c0341432SJohn Baldwin safe_auth_supported(struct safe_softc *sc,
657c0341432SJohn Baldwin     const struct crypto_session_params *csp)
658b7e3f244SSam Leffler {
659b7e3f244SSam Leffler 
660c0341432SJohn Baldwin 	switch (csp->csp_auth_alg) {
661c0341432SJohn Baldwin 	case CRYPTO_SHA1_HMAC:
662c0341432SJohn Baldwin 		if ((sc->sc_devinfo & SAFE_DEVINFO_SHA1) == 0)
663c0341432SJohn Baldwin 			return (false);
664c0341432SJohn Baldwin 		break;
665c0341432SJohn Baldwin 	default:
666c0341432SJohn Baldwin 		return (false);
667c0341432SJohn Baldwin 	}
668c0341432SJohn Baldwin 	return (true);
669c0341432SJohn Baldwin }
670c0341432SJohn Baldwin 
671c0341432SJohn Baldwin static bool
safe_cipher_supported(struct safe_softc * sc,const struct crypto_session_params * csp)672c0341432SJohn Baldwin safe_cipher_supported(struct safe_softc *sc,
673c0341432SJohn Baldwin     const struct crypto_session_params *csp)
674c0341432SJohn Baldwin {
675c0341432SJohn Baldwin 
676c0341432SJohn Baldwin 	switch (csp->csp_cipher_alg) {
677b7e3f244SSam Leffler 	case CRYPTO_AES_CBC:
678c0341432SJohn Baldwin 		if ((sc->sc_devinfo & SAFE_DEVINFO_AES) == 0)
679c0341432SJohn Baldwin 			return (false);
680c0341432SJohn Baldwin 		if (csp->csp_ivlen != 16)
681c0341432SJohn Baldwin 			return (false);
682c0341432SJohn Baldwin 		if (csp->csp_cipher_klen != 16 &&
683c0341432SJohn Baldwin 		    csp->csp_cipher_klen != 24 &&
684c0341432SJohn Baldwin 		    csp->csp_cipher_klen != 32)
685c0341432SJohn Baldwin 			return (false);
686b7e3f244SSam Leffler 		break;
687b7e3f244SSam Leffler 	}
688c0341432SJohn Baldwin 	return (true);
689b7e3f244SSam Leffler }
690b7e3f244SSam Leffler 
691c0341432SJohn Baldwin static int
safe_probesession(device_t dev,const struct crypto_session_params * csp)692c0341432SJohn Baldwin safe_probesession(device_t dev, const struct crypto_session_params *csp)
693c0341432SJohn Baldwin {
694c0341432SJohn Baldwin 	struct safe_softc *sc = device_get_softc(dev);
695c0341432SJohn Baldwin 
696c0341432SJohn Baldwin 	if (csp->csp_flags != 0)
697c0341432SJohn Baldwin 		return (EINVAL);
698c0341432SJohn Baldwin 	switch (csp->csp_mode) {
699c0341432SJohn Baldwin 	case CSP_MODE_DIGEST:
700c0341432SJohn Baldwin 		if (!safe_auth_supported(sc, csp))
701c0341432SJohn Baldwin 			return (EINVAL);
702c0341432SJohn Baldwin 		break;
703c0341432SJohn Baldwin 	case CSP_MODE_CIPHER:
704c0341432SJohn Baldwin 		if (!safe_cipher_supported(sc, csp))
705c0341432SJohn Baldwin 			return (EINVAL);
706c0341432SJohn Baldwin 		break;
707c0341432SJohn Baldwin 	case CSP_MODE_ETA:
708c0341432SJohn Baldwin 		if (!safe_auth_supported(sc, csp) ||
709c0341432SJohn Baldwin 		    !safe_cipher_supported(sc, csp))
710c0341432SJohn Baldwin 			return (EINVAL);
711c0341432SJohn Baldwin 		break;
712c0341432SJohn Baldwin 	default:
713c0341432SJohn Baldwin 		return (EINVAL);
714c0341432SJohn Baldwin 	}
715c0341432SJohn Baldwin 
716c0341432SJohn Baldwin 	return (CRYPTODEV_PROBE_HARDWARE);
717c0341432SJohn Baldwin }
718c0341432SJohn Baldwin 
719c0341432SJohn Baldwin /*
720c0341432SJohn Baldwin  * Allocate a new 'session'.
721c0341432SJohn Baldwin  */
722c0341432SJohn Baldwin static int
safe_newsession(device_t dev,crypto_session_t cses,const struct crypto_session_params * csp)723c0341432SJohn Baldwin safe_newsession(device_t dev, crypto_session_t cses,
724c0341432SJohn Baldwin     const struct crypto_session_params *csp)
725c0341432SJohn Baldwin {
726c0341432SJohn Baldwin 	struct safe_session *ses;
727c0341432SJohn Baldwin 
7281b0909d5SConrad Meyer 	ses = crypto_get_driver_session(cses);
729c0341432SJohn Baldwin 	if (csp->csp_cipher_alg != 0) {
730c0341432SJohn Baldwin 		ses->ses_klen = csp->csp_cipher_klen;
731c0341432SJohn Baldwin 		if (csp->csp_cipher_key != NULL)
732c0341432SJohn Baldwin 			safe_setup_enckey(ses, csp->csp_cipher_key);
733b7e3f244SSam Leffler 	}
734b7e3f244SSam Leffler 
735c0341432SJohn Baldwin 	if (csp->csp_auth_alg != 0) {
736c0341432SJohn Baldwin 		ses->ses_mlen = csp->csp_auth_mlen;
737af65c53aSPawel Jakub Dawidek 		if (ses->ses_mlen == 0) {
7381dc8d404SPawel Jakub Dawidek 			ses->ses_mlen = SHA1_HASH_LEN;
739af65c53aSPawel Jakub Dawidek 		}
740af65c53aSPawel Jakub Dawidek 
741c0341432SJohn Baldwin 		if (csp->csp_auth_key != NULL) {
742c0341432SJohn Baldwin 			safe_setup_mackey(ses, csp->csp_auth_alg,
743c0341432SJohn Baldwin 			    csp->csp_auth_key, csp->csp_auth_klen);
744b7e3f244SSam Leffler 		}
745b7e3f244SSam Leffler 	}
746b7e3f244SSam Leffler 
747b7e3f244SSam Leffler 	return (0);
748b7e3f244SSam Leffler }
749b7e3f244SSam Leffler 
750b7e3f244SSam Leffler static void
safe_op_cb(void * arg,bus_dma_segment_t * seg,int nsegs,int error)751c0341432SJohn Baldwin safe_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, int error)
752b7e3f244SSam Leffler {
753b7e3f244SSam Leffler 	struct safe_operand *op = arg;
754b7e3f244SSam Leffler 
755c0341432SJohn Baldwin 	DPRINTF(("%s: nsegs %d error %d\n", __func__,
756c0341432SJohn Baldwin 		nsegs, error));
757b7e3f244SSam Leffler 	if (error != 0)
758b7e3f244SSam Leffler 		return;
759b7e3f244SSam Leffler 	op->nsegs = nsegs;
760b7e3f244SSam Leffler 	bcopy(seg, op->segs, nsegs * sizeof (seg[0]));
761b7e3f244SSam Leffler }
762b7e3f244SSam Leffler 
763b7e3f244SSam Leffler static int
safe_process(device_t dev,struct cryptop * crp,int hint)7646810ad6fSSam Leffler safe_process(device_t dev, struct cryptop *crp, int hint)
765b7e3f244SSam Leffler {
7666810ad6fSSam Leffler 	struct safe_softc *sc = device_get_softc(dev);
767c0341432SJohn Baldwin 	const struct crypto_session_params *csp;
768b7e3f244SSam Leffler 	int err = 0, i, nicealign, uniform;
769c0341432SJohn Baldwin 	int bypass, oplen;
770b7e3f244SSam Leffler 	int16_t coffset;
771b7e3f244SSam Leffler 	struct safe_session *ses;
772b7e3f244SSam Leffler 	struct safe_ringentry *re;
773b7e3f244SSam Leffler 	struct safe_sarec *sa;
774b7e3f244SSam Leffler 	struct safe_pdesc *pd;
775b7e3f244SSam Leffler 	u_int32_t cmd0, cmd1, staterec;
776b7e3f244SSam Leffler 
777b7e3f244SSam Leffler 	mtx_lock(&sc->sc_ringmtx);
778b7e3f244SSam Leffler 	if (sc->sc_front == sc->sc_back && sc->sc_nqchip != 0) {
779b7e3f244SSam Leffler 		safestats.st_ringfull++;
780b7e3f244SSam Leffler 		sc->sc_needwakeup |= CRYPTO_SYMQ;
781b7e3f244SSam Leffler 		mtx_unlock(&sc->sc_ringmtx);
782b7e3f244SSam Leffler 		return (ERESTART);
783b7e3f244SSam Leffler 	}
784b7e3f244SSam Leffler 	re = sc->sc_front;
785b7e3f244SSam Leffler 
786b7e3f244SSam Leffler 	staterec = re->re_sa.sa_staterec;	/* save */
787b7e3f244SSam Leffler 	/* NB: zero everything but the PE descriptor */
788b7e3f244SSam Leffler 	bzero(&re->re_sa, sizeof(struct safe_ringentry) - sizeof(re->re_desc));
789b7e3f244SSam Leffler 	re->re_sa.sa_staterec = staterec;	/* restore */
790b7e3f244SSam Leffler 
791b7e3f244SSam Leffler 	re->re_crp = crp;
792b7e3f244SSam Leffler 
793b7e3f244SSam Leffler 	sa = &re->re_sa;
7941b0909d5SConrad Meyer 	ses = crypto_get_driver_session(crp->crp_session);
795c0341432SJohn Baldwin 	csp = crypto_get_params(crp->crp_session);
796b7e3f244SSam Leffler 
797b7e3f244SSam Leffler 	cmd0 = SAFE_SA_CMD0_BASIC;		/* basic group operation */
798b7e3f244SSam Leffler 	cmd1 = 0;
799c0341432SJohn Baldwin 	switch (csp->csp_mode) {
800c0341432SJohn Baldwin 	case CSP_MODE_DIGEST:
801b7e3f244SSam Leffler 		cmd0 |= SAFE_SA_CMD0_OP_HASH;
802c0341432SJohn Baldwin 		break;
803c0341432SJohn Baldwin 	case CSP_MODE_CIPHER:
804b7e3f244SSam Leffler 		cmd0 |= SAFE_SA_CMD0_OP_CRYPT;
805c0341432SJohn Baldwin 		break;
806c0341432SJohn Baldwin 	case CSP_MODE_ETA:
807b7e3f244SSam Leffler 		cmd0 |= SAFE_SA_CMD0_OP_BOTH;
808c0341432SJohn Baldwin 		break;
809b7e3f244SSam Leffler 	}
810b7e3f244SSam Leffler 
811c0341432SJohn Baldwin 	if (csp->csp_cipher_alg != 0) {
812c0341432SJohn Baldwin 		if (crp->crp_cipher_key != NULL)
813c0341432SJohn Baldwin 			safe_setup_enckey(ses, crp->crp_cipher_key);
8149a2f6061SPawel Jakub Dawidek 
815c0341432SJohn Baldwin 		switch (csp->csp_cipher_alg) {
816c0341432SJohn Baldwin 		case CRYPTO_AES_CBC:
817b7e3f244SSam Leffler 			cmd0 |= SAFE_SA_CMD0_AES;
818b7e3f244SSam Leffler 			cmd1 |= SAFE_SA_CMD1_CBC;
819c0341432SJohn Baldwin 			if (ses->ses_klen * 8 == 128)
820b7e3f244SSam Leffler 			     cmd1 |=  SAFE_SA_CMD1_AES128;
821c0341432SJohn Baldwin 			else if (ses->ses_klen * 8 == 192)
822b7e3f244SSam Leffler 			     cmd1 |=  SAFE_SA_CMD1_AES192;
823b7e3f244SSam Leffler 			else
824b7e3f244SSam Leffler 			     cmd1 |=  SAFE_SA_CMD1_AES256;
825b7e3f244SSam Leffler 		}
826b7e3f244SSam Leffler 
827b7e3f244SSam Leffler 		/*
828b7e3f244SSam Leffler 		 * Setup encrypt/decrypt state.  When using basic ops
829b7e3f244SSam Leffler 		 * we can't use an inline IV because hash/crypt offset
830b7e3f244SSam Leffler 		 * must be from the end of the IV to the start of the
831b7e3f244SSam Leffler 		 * crypt data and this leaves out the preceding header
832b7e3f244SSam Leffler 		 * from the hash calculation.  Instead we place the IV
833b7e3f244SSam Leffler 		 * in the state record and set the hash/crypt offset to
834b7e3f244SSam Leffler 		 * copy both the header+IV.
835b7e3f244SSam Leffler 		 */
83629fe41ddSJohn Baldwin 		crypto_read_iv(crp, re->re_sastate.sa_saved_iv);
837c0341432SJohn Baldwin 		cmd0 |= SAFE_SA_CMD0_IVLD_STATE;
838c0341432SJohn Baldwin 
839c0341432SJohn Baldwin 		if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
840b7e3f244SSam Leffler 			cmd0 |= SAFE_SA_CMD0_OUTBOUND;
841b7e3f244SSam Leffler 
842c0341432SJohn Baldwin 			/*
843c0341432SJohn Baldwin 			 * XXX: I suspect we don't need this since we
844c0341432SJohn Baldwin 			 * don't save the returned IV.
845c0341432SJohn Baldwin 			 */
846c0341432SJohn Baldwin 			cmd0 |= SAFE_SA_CMD0_SAVEIV;
847b7e3f244SSam Leffler 		} else {
848b7e3f244SSam Leffler 			cmd0 |= SAFE_SA_CMD0_INBOUND;
849b7e3f244SSam Leffler 		}
850b7e3f244SSam Leffler 		/*
851b7e3f244SSam Leffler 		 * For basic encryption use the zero pad algorithm.
852b7e3f244SSam Leffler 		 * This pads results to an 8-byte boundary and
853b7e3f244SSam Leffler 		 * suppresses padding verification for inbound (i.e.
854b7e3f244SSam Leffler 		 * decrypt) operations.
855b7e3f244SSam Leffler 		 *
856b7e3f244SSam Leffler 		 * NB: Not sure if the 8-byte pad boundary is a problem.
857b7e3f244SSam Leffler 		 */
858b7e3f244SSam Leffler 		cmd0 |= SAFE_SA_CMD0_PAD_ZERO;
859b7e3f244SSam Leffler 
860b7e3f244SSam Leffler 		/* XXX assert key bufs have the same size */
861b7e3f244SSam Leffler 		bcopy(ses->ses_key, sa->sa_key, sizeof(sa->sa_key));
862b7e3f244SSam Leffler 	}
863b7e3f244SSam Leffler 
864c0341432SJohn Baldwin 	if (csp->csp_auth_alg != 0) {
865c0341432SJohn Baldwin 		if (crp->crp_auth_key != NULL) {
866c0341432SJohn Baldwin 			safe_setup_mackey(ses, csp->csp_auth_alg,
867c0341432SJohn Baldwin 			    crp->crp_auth_key, csp->csp_auth_klen);
8689a2f6061SPawel Jakub Dawidek 		}
8699a2f6061SPawel Jakub Dawidek 
870c0341432SJohn Baldwin 		switch (csp->csp_auth_alg) {
871c0341432SJohn Baldwin 		case CRYPTO_SHA1_HMAC:
872b7e3f244SSam Leffler 			cmd0 |= SAFE_SA_CMD0_SHA1;
873b7e3f244SSam Leffler 			cmd1 |= SAFE_SA_CMD1_HMAC;	/* NB: enable HMAC */
874c0341432SJohn Baldwin 			break;
875b7e3f244SSam Leffler 		}
876c0341432SJohn Baldwin 
877b7e3f244SSam Leffler 		/*
878b7e3f244SSam Leffler 		 * Digest data is loaded from the SA and the hash
879b7e3f244SSam Leffler 		 * result is saved to the state block where we
880b7e3f244SSam Leffler 		 * retrieve it for return to the caller.
881b7e3f244SSam Leffler 		 */
882b7e3f244SSam Leffler 		/* XXX assert digest bufs have the same size */
883b7e3f244SSam Leffler 		bcopy(ses->ses_hminner, sa->sa_indigest,
884b7e3f244SSam Leffler 			sizeof(sa->sa_indigest));
885b7e3f244SSam Leffler 		bcopy(ses->ses_hmouter, sa->sa_outdigest,
886b7e3f244SSam Leffler 			sizeof(sa->sa_outdigest));
887b7e3f244SSam Leffler 
888b7e3f244SSam Leffler 		cmd0 |= SAFE_SA_CMD0_HSLD_SA | SAFE_SA_CMD0_SAVEHASH;
889b7e3f244SSam Leffler 		re->re_flags |= SAFE_QFLAGS_COPYOUTICV;
890b7e3f244SSam Leffler 	}
891b7e3f244SSam Leffler 
892c0341432SJohn Baldwin 	if (csp->csp_mode == CSP_MODE_ETA) {
893b7e3f244SSam Leffler 		/*
894c0341432SJohn Baldwin 		 * The driver only supports ETA requests where there
895c0341432SJohn Baldwin 		 * is no gap between the AAD and payload.
896b7e3f244SSam Leffler 		 */
897c0341432SJohn Baldwin 		if (crp->crp_aad_length != 0 &&
898c0341432SJohn Baldwin 		    crp->crp_aad_start + crp->crp_aad_length !=
899c0341432SJohn Baldwin 		    crp->crp_payload_start) {
900b7e3f244SSam Leffler 			safestats.st_lenmismatch++;
901b7e3f244SSam Leffler 			err = EINVAL;
902b7e3f244SSam Leffler 			goto errout;
903b7e3f244SSam Leffler 		}
904c0341432SJohn Baldwin 		if (crp->crp_aad_length != 0)
905c0341432SJohn Baldwin 			bypass = crp->crp_aad_start;
906c0341432SJohn Baldwin 		else
907c0341432SJohn Baldwin 			bypass = crp->crp_payload_start;
908c0341432SJohn Baldwin 		coffset = crp->crp_aad_length;
909c0341432SJohn Baldwin 		oplen = crp->crp_payload_start + crp->crp_payload_length;
910b7e3f244SSam Leffler #ifdef SAFE_DEBUG
911b7e3f244SSam Leffler 		if (safe_debug) {
912c0341432SJohn Baldwin 			printf("AAD: skip %d, len %d, digest %d\n",
913c0341432SJohn Baldwin 			    crp->crp_aad_start, crp->crp_aad_length,
914c0341432SJohn Baldwin 			    crp->crp_digest_start);
915c0341432SJohn Baldwin 			printf("payload: skip %d, len %d, IV %d\n",
916c0341432SJohn Baldwin 			    crp->crp_payload_start, crp->crp_payload_length,
917c0341432SJohn Baldwin 			    crp->crp_iv_start);
918b7e3f244SSam Leffler 			printf("bypass %d coffset %d oplen %d\n",
919b7e3f244SSam Leffler 				bypass, coffset, oplen);
920b7e3f244SSam Leffler 		}
921b7e3f244SSam Leffler #endif
922b7e3f244SSam Leffler 		if (coffset & 3) {	/* offset must be 32-bit aligned */
923b7e3f244SSam Leffler 			DPRINTF(("%s: coffset %u misaligned\n",
924b7e3f244SSam Leffler 				__func__, coffset));
925b7e3f244SSam Leffler 			safestats.st_coffmisaligned++;
926b7e3f244SSam Leffler 			err = EINVAL;
927b7e3f244SSam Leffler 			goto errout;
928b7e3f244SSam Leffler 		}
929b7e3f244SSam Leffler 		coffset >>= 2;
930b7e3f244SSam Leffler 		if (coffset > 255) {	/* offset must be <256 dwords */
931b7e3f244SSam Leffler 			DPRINTF(("%s: coffset %u too big\n",
932b7e3f244SSam Leffler 				__func__, coffset));
933b7e3f244SSam Leffler 			safestats.st_cofftoobig++;
934b7e3f244SSam Leffler 			err = EINVAL;
935b7e3f244SSam Leffler 			goto errout;
936b7e3f244SSam Leffler 		}
937b7e3f244SSam Leffler 		/*
938b7e3f244SSam Leffler 		 * Tell the hardware to copy the header to the output.
939b7e3f244SSam Leffler 		 * The header is defined as the data from the end of
940b7e3f244SSam Leffler 		 * the bypass to the start of data to be encrypted.
941b7e3f244SSam Leffler 		 * Typically this is the inline IV.  Note that you need
942b7e3f244SSam Leffler 		 * to do this even if src+dst are the same; it appears
943b7e3f244SSam Leffler 		 * that w/o this bit the crypted data is written
944b7e3f244SSam Leffler 		 * immediately after the bypass data.
945b7e3f244SSam Leffler 		 */
946b7e3f244SSam Leffler 		cmd1 |= SAFE_SA_CMD1_HDRCOPY;
947b7e3f244SSam Leffler 		/*
948b7e3f244SSam Leffler 		 * Disable IP header mutable bit handling.  This is
949b7e3f244SSam Leffler 		 * needed to get correct HMAC calculations.
950b7e3f244SSam Leffler 		 */
951b7e3f244SSam Leffler 		cmd1 |= SAFE_SA_CMD1_MUTABLE;
952b7e3f244SSam Leffler 	} else {
953c0341432SJohn Baldwin 		bypass = crp->crp_payload_start;
954c0341432SJohn Baldwin 		oplen = bypass + crp->crp_payload_length;
955b7e3f244SSam Leffler 		coffset = 0;
956b7e3f244SSam Leffler 	}
957b7e3f244SSam Leffler 	/* XXX verify multiple of 4 when using s/g */
958b7e3f244SSam Leffler 	if (bypass > 96) {		/* bypass offset must be <= 96 bytes */
959b7e3f244SSam Leffler 		DPRINTF(("%s: bypass %u too big\n", __func__, bypass));
960b7e3f244SSam Leffler 		safestats.st_bypasstoobig++;
961b7e3f244SSam Leffler 		err = EINVAL;
962b7e3f244SSam Leffler 		goto errout;
963b7e3f244SSam Leffler 	}
964b7e3f244SSam Leffler 
965b7e3f244SSam Leffler 	if (bus_dmamap_create(sc->sc_srcdmat, BUS_DMA_NOWAIT, &re->re_src_map)) {
966b7e3f244SSam Leffler 		safestats.st_nomap++;
967b7e3f244SSam Leffler 		err = ENOMEM;
968b7e3f244SSam Leffler 		goto errout;
969b7e3f244SSam Leffler 	}
970c0341432SJohn Baldwin 	if (bus_dmamap_load_crp(sc->sc_srcdmat, re->re_src_map, crp, safe_op_cb,
971b7e3f244SSam Leffler 	    &re->re_src, BUS_DMA_NOWAIT) != 0) {
972b7e3f244SSam Leffler 		bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
973b7e3f244SSam Leffler 		re->re_src_map = NULL;
974b7e3f244SSam Leffler 		safestats.st_noload++;
975b7e3f244SSam Leffler 		err = ENOMEM;
976b7e3f244SSam Leffler 		goto errout;
977b7e3f244SSam Leffler 	}
9789c0e3d3aSJohn Baldwin 	re->re_src_mapsize = crypto_buffer_len(&crp->crp_buf);
979b7e3f244SSam Leffler 	nicealign = safe_dmamap_aligned(&re->re_src);
980b7e3f244SSam Leffler 	uniform = safe_dmamap_uniform(&re->re_src);
981b7e3f244SSam Leffler 
982b7e3f244SSam Leffler 	DPRINTF(("src nicealign %u uniform %u nsegs %u\n",
983b7e3f244SSam Leffler 		nicealign, uniform, re->re_src.nsegs));
984b7e3f244SSam Leffler 	if (re->re_src.nsegs > 1) {
985b7e3f244SSam Leffler 		re->re_desc.d_src = sc->sc_spalloc.dma_paddr +
986b7e3f244SSam Leffler 			((caddr_t) sc->sc_spfree - (caddr_t) sc->sc_spring);
987b7e3f244SSam Leffler 		for (i = 0; i < re->re_src_nsegs; i++) {
988b7e3f244SSam Leffler 			/* NB: no need to check if there's space */
989b7e3f244SSam Leffler 			pd = sc->sc_spfree;
990b7e3f244SSam Leffler 			if (++(sc->sc_spfree) == sc->sc_springtop)
991b7e3f244SSam Leffler 				sc->sc_spfree = sc->sc_spring;
992b7e3f244SSam Leffler 
993b7e3f244SSam Leffler 			KASSERT((pd->pd_flags&3) == 0 ||
994b7e3f244SSam Leffler 				(pd->pd_flags&3) == SAFE_PD_DONE,
995b7e3f244SSam Leffler 				("bogus source particle descriptor; flags %x",
996b7e3f244SSam Leffler 				pd->pd_flags));
997b7e3f244SSam Leffler 			pd->pd_addr = re->re_src_segs[i].ds_addr;
998b7e3f244SSam Leffler 			pd->pd_size = re->re_src_segs[i].ds_len;
999b7e3f244SSam Leffler 			pd->pd_flags = SAFE_PD_READY;
1000b7e3f244SSam Leffler 		}
1001b7e3f244SSam Leffler 		cmd0 |= SAFE_SA_CMD0_IGATHER;
1002b7e3f244SSam Leffler 	} else {
1003b7e3f244SSam Leffler 		/*
1004b7e3f244SSam Leffler 		 * No need for gather, reference the operand directly.
1005b7e3f244SSam Leffler 		 */
1006b7e3f244SSam Leffler 		re->re_desc.d_src = re->re_src_segs[0].ds_addr;
1007b7e3f244SSam Leffler 	}
1008b7e3f244SSam Leffler 
1009c0341432SJohn Baldwin 	if (csp->csp_mode == CSP_MODE_DIGEST) {
1010b7e3f244SSam Leffler 		/*
1011b7e3f244SSam Leffler 		 * Hash op; no destination needed.
1012b7e3f244SSam Leffler 		 */
1013b7e3f244SSam Leffler 	} else {
1014b7e3f244SSam Leffler 		if (nicealign && uniform == 1) {
1015b7e3f244SSam Leffler 			/*
1016b7e3f244SSam Leffler 			 * Source layout is suitable for direct
1017b7e3f244SSam Leffler 			 * sharing of the DMA map and segment list.
1018b7e3f244SSam Leffler 			 */
1019b7e3f244SSam Leffler 			re->re_dst = re->re_src;
1020b7e3f244SSam Leffler 		} else if (nicealign && uniform == 2) {
1021b7e3f244SSam Leffler 			/*
1022b7e3f244SSam Leffler 			 * The source is properly aligned but requires a
1023b7e3f244SSam Leffler 			 * different particle list to handle DMA of the
1024b7e3f244SSam Leffler 			 * result.  Create a new map and do the load to
1025b7e3f244SSam Leffler 			 * create the segment list.  The particle
1026b7e3f244SSam Leffler 			 * descriptor setup code below will handle the
1027b7e3f244SSam Leffler 			 * rest.
1028b7e3f244SSam Leffler 			 */
1029c0341432SJohn Baldwin 			if (bus_dmamap_create(sc->sc_dstdmat, BUS_DMA_NOWAIT,
1030c0341432SJohn Baldwin 			    &re->re_dst_map)) {
1031b7e3f244SSam Leffler 				safestats.st_nomap++;
1032b7e3f244SSam Leffler 				err = ENOMEM;
1033b7e3f244SSam Leffler 				goto errout;
1034b7e3f244SSam Leffler 			}
1035c0341432SJohn Baldwin 			if (bus_dmamap_load_crp(sc->sc_dstdmat, re->re_dst_map,
1036c0341432SJohn Baldwin 			    crp, safe_op_cb, &re->re_dst, BUS_DMA_NOWAIT) !=
1037c0341432SJohn Baldwin 			    0) {
1038b7e3f244SSam Leffler 				bus_dmamap_destroy(sc->sc_dstdmat,
1039b7e3f244SSam Leffler 				    re->re_dst_map);
1040b7e3f244SSam Leffler 				re->re_dst_map = NULL;
1041b7e3f244SSam Leffler 				safestats.st_noload++;
1042b7e3f244SSam Leffler 				err = ENOMEM;
1043b7e3f244SSam Leffler 				goto errout;
1044b7e3f244SSam Leffler 			}
10459c0e3d3aSJohn Baldwin 		} else if (crp->crp_buf.cb_type == CRYPTO_BUF_MBUF) {
1046b7e3f244SSam Leffler 			int totlen, len;
1047b7e3f244SSam Leffler 			struct mbuf *m, *top, **mp;
1048b7e3f244SSam Leffler 
1049b7e3f244SSam Leffler 			/*
1050b7e3f244SSam Leffler 			 * DMA constraints require that we allocate a
1051b7e3f244SSam Leffler 			 * new mbuf chain for the destination.  We
1052b7e3f244SSam Leffler 			 * allocate an entire new set of mbufs of
1053b7e3f244SSam Leffler 			 * optimal/required size and then tell the
1054b7e3f244SSam Leffler 			 * hardware to copy any bits that are not
1055b7e3f244SSam Leffler 			 * created as a byproduct of the operation.
1056b7e3f244SSam Leffler 			 */
1057b7e3f244SSam Leffler 			if (!nicealign)
1058b7e3f244SSam Leffler 				safestats.st_unaligned++;
1059b7e3f244SSam Leffler 			if (!uniform)
1060b7e3f244SSam Leffler 				safestats.st_notuniform++;
1061b7e3f244SSam Leffler 			totlen = re->re_src_mapsize;
10629c0e3d3aSJohn Baldwin 			if (crp->crp_buf.cb_mbuf->m_flags & M_PKTHDR) {
1063b7e3f244SSam Leffler 				len = MHLEN;
1064c6499eccSGleb Smirnoff 				MGETHDR(m, M_NOWAIT, MT_DATA);
10659c0e3d3aSJohn Baldwin 				if (m && !m_dup_pkthdr(m, crp->crp_buf.cb_mbuf,
1066c6499eccSGleb Smirnoff 				    M_NOWAIT)) {
1067b7e3f244SSam Leffler 					m_free(m);
1068b7e3f244SSam Leffler 					m = NULL;
1069b7e3f244SSam Leffler 				}
1070b7e3f244SSam Leffler 			} else {
1071b7e3f244SSam Leffler 				len = MLEN;
1072c6499eccSGleb Smirnoff 				MGET(m, M_NOWAIT, MT_DATA);
1073b7e3f244SSam Leffler 			}
1074b7e3f244SSam Leffler 			if (m == NULL) {
1075b7e3f244SSam Leffler 				safestats.st_nombuf++;
1076b7e3f244SSam Leffler 				err = sc->sc_nqchip ? ERESTART : ENOMEM;
1077b7e3f244SSam Leffler 				goto errout;
1078b7e3f244SSam Leffler 			}
1079b7e3f244SSam Leffler 			if (totlen >= MINCLSIZE) {
10802a8c860fSRobert Watson 				if (!(MCLGET(m, M_NOWAIT))) {
1081b7e3f244SSam Leffler 					m_free(m);
1082b7e3f244SSam Leffler 					safestats.st_nomcl++;
1083b7e3f244SSam Leffler 					err = sc->sc_nqchip ?
1084b7e3f244SSam Leffler 					    ERESTART : ENOMEM;
1085b7e3f244SSam Leffler 					goto errout;
1086b7e3f244SSam Leffler 				}
1087b7e3f244SSam Leffler 				len = MCLBYTES;
1088b7e3f244SSam Leffler 			}
1089b7e3f244SSam Leffler 			m->m_len = len;
1090b7e3f244SSam Leffler 			top = NULL;
1091b7e3f244SSam Leffler 			mp = &top;
1092b7e3f244SSam Leffler 
1093b7e3f244SSam Leffler 			while (totlen > 0) {
1094b7e3f244SSam Leffler 				if (top) {
1095c6499eccSGleb Smirnoff 					MGET(m, M_NOWAIT, MT_DATA);
1096b7e3f244SSam Leffler 					if (m == NULL) {
1097b7e3f244SSam Leffler 						m_freem(top);
1098b7e3f244SSam Leffler 						safestats.st_nombuf++;
1099b7e3f244SSam Leffler 						err = sc->sc_nqchip ?
1100b7e3f244SSam Leffler 						    ERESTART : ENOMEM;
1101b7e3f244SSam Leffler 						goto errout;
1102b7e3f244SSam Leffler 					}
1103b7e3f244SSam Leffler 					len = MLEN;
1104b7e3f244SSam Leffler 				}
1105b7e3f244SSam Leffler 				if (top && totlen >= MINCLSIZE) {
11062a8c860fSRobert Watson 					if (!(MCLGET(m, M_NOWAIT))) {
1107b7e3f244SSam Leffler 						*mp = m;
1108b7e3f244SSam Leffler 						m_freem(top);
1109b7e3f244SSam Leffler 						safestats.st_nomcl++;
1110b7e3f244SSam Leffler 						err = sc->sc_nqchip ?
1111b7e3f244SSam Leffler 						    ERESTART : ENOMEM;
1112b7e3f244SSam Leffler 						goto errout;
1113b7e3f244SSam Leffler 					}
1114b7e3f244SSam Leffler 					len = MCLBYTES;
1115b7e3f244SSam Leffler 				}
1116b7e3f244SSam Leffler 				m->m_len = len = min(totlen, len);
1117b7e3f244SSam Leffler 				totlen -= len;
1118b7e3f244SSam Leffler 				*mp = m;
1119b7e3f244SSam Leffler 				mp = &m->m_next;
1120b7e3f244SSam Leffler 			}
1121b7e3f244SSam Leffler 			re->re_dst_m = top;
1122b7e3f244SSam Leffler 			if (bus_dmamap_create(sc->sc_dstdmat,
1123b7e3f244SSam Leffler 			    BUS_DMA_NOWAIT, &re->re_dst_map) != 0) {
1124b7e3f244SSam Leffler 				safestats.st_nomap++;
1125b7e3f244SSam Leffler 				err = ENOMEM;
1126b7e3f244SSam Leffler 				goto errout;
1127b7e3f244SSam Leffler 			}
1128c0341432SJohn Baldwin 			if (bus_dmamap_load_mbuf_sg(sc->sc_dstdmat,
1129c0341432SJohn Baldwin 			    re->re_dst_map, top, re->re_dst_segs,
1130c0341432SJohn Baldwin 			    &re->re_dst_nsegs, 0) != 0) {
1131b7e3f244SSam Leffler 				bus_dmamap_destroy(sc->sc_dstdmat,
1132b7e3f244SSam Leffler 				    re->re_dst_map);
1133b7e3f244SSam Leffler 				re->re_dst_map = NULL;
1134b7e3f244SSam Leffler 				safestats.st_noload++;
1135b7e3f244SSam Leffler 				err = ENOMEM;
1136b7e3f244SSam Leffler 				goto errout;
1137b7e3f244SSam Leffler 			}
1138c0341432SJohn Baldwin 			re->re_dst_mapsize = re->re_src_mapsize;
1139b7e3f244SSam Leffler 			if (re->re_src.mapsize > oplen) {
1140b7e3f244SSam Leffler 				/*
1141b7e3f244SSam Leffler 				 * There's data following what the
1142b7e3f244SSam Leffler 				 * hardware will copy for us.  If this
1143b7e3f244SSam Leffler 				 * isn't just the ICV (that's going to
1144b7e3f244SSam Leffler 				 * be written on completion), copy it
1145b7e3f244SSam Leffler 				 * to the new mbufs
1146b7e3f244SSam Leffler 				 */
1147c0341432SJohn Baldwin 				if (!(csp->csp_mode == CSP_MODE_ETA &&
1148c0341432SJohn Baldwin 				    (re->re_src.mapsize-oplen) == ses->ses_mlen &&
1149c0341432SJohn Baldwin 				    crp->crp_digest_start == oplen))
11509c0e3d3aSJohn Baldwin 					safe_mcopy(crp->crp_buf.cb_mbuf,
11519c0e3d3aSJohn Baldwin 					    re->re_dst_m, oplen);
1152b7e3f244SSam Leffler 				else
1153b7e3f244SSam Leffler 					safestats.st_noicvcopy++;
1154b7e3f244SSam Leffler 			}
1155b7e3f244SSam Leffler 		} else {
1156c0341432SJohn Baldwin 			if (!nicealign) {
1157c0341432SJohn Baldwin 				safestats.st_iovmisaligned++;
1158b7e3f244SSam Leffler 				err = EINVAL;
1159b7e3f244SSam Leffler 				goto errout;
1160c0341432SJohn Baldwin 			} else {
1161c0341432SJohn Baldwin 				/*
1162c0341432SJohn Baldwin 				 * There's no way to handle the DMA
1163c0341432SJohn Baldwin 				 * requirements with this uio.  We
1164c0341432SJohn Baldwin 				 * could create a separate DMA area for
1165c0341432SJohn Baldwin 				 * the result and then copy it back,
1166c0341432SJohn Baldwin 				 * but for now we just bail and return
1167c0341432SJohn Baldwin 				 * an error.  Note that uio requests
1168c0341432SJohn Baldwin 				 * > SAFE_MAX_DSIZE are handled because
1169c0341432SJohn Baldwin 				 * the DMA map and segment list for the
1170c0341432SJohn Baldwin 				 * destination wil result in a
1171c0341432SJohn Baldwin 				 * destination particle list that does
1172c0341432SJohn Baldwin 				 * the necessary scatter DMA.
1173c0341432SJohn Baldwin 				 */
1174c0341432SJohn Baldwin 				safestats.st_iovnotuniform++;
1175c0341432SJohn Baldwin 				err = EINVAL;
1176c0341432SJohn Baldwin 				goto errout;
1177c0341432SJohn Baldwin 			}
1178b7e3f244SSam Leffler 		}
1179b7e3f244SSam Leffler 
1180b7e3f244SSam Leffler 		if (re->re_dst.nsegs > 1) {
1181b7e3f244SSam Leffler 			re->re_desc.d_dst = sc->sc_dpalloc.dma_paddr +
1182b7e3f244SSam Leffler 			    ((caddr_t) sc->sc_dpfree - (caddr_t) sc->sc_dpring);
1183b7e3f244SSam Leffler 			for (i = 0; i < re->re_dst_nsegs; i++) {
1184b7e3f244SSam Leffler 				pd = sc->sc_dpfree;
1185b7e3f244SSam Leffler 				KASSERT((pd->pd_flags&3) == 0 ||
1186b7e3f244SSam Leffler 					(pd->pd_flags&3) == SAFE_PD_DONE,
1187b7e3f244SSam Leffler 					("bogus dest particle descriptor; flags %x",
1188b7e3f244SSam Leffler 						pd->pd_flags));
1189b7e3f244SSam Leffler 				if (++(sc->sc_dpfree) == sc->sc_dpringtop)
1190b7e3f244SSam Leffler 					sc->sc_dpfree = sc->sc_dpring;
1191b7e3f244SSam Leffler 				pd->pd_addr = re->re_dst_segs[i].ds_addr;
1192b7e3f244SSam Leffler 				pd->pd_flags = SAFE_PD_READY;
1193b7e3f244SSam Leffler 			}
1194b7e3f244SSam Leffler 			cmd0 |= SAFE_SA_CMD0_OSCATTER;
1195b7e3f244SSam Leffler 		} else {
1196b7e3f244SSam Leffler 			/*
1197b7e3f244SSam Leffler 			 * No need for scatter, reference the operand directly.
1198b7e3f244SSam Leffler 			 */
1199b7e3f244SSam Leffler 			re->re_desc.d_dst = re->re_dst_segs[0].ds_addr;
1200b7e3f244SSam Leffler 		}
1201b7e3f244SSam Leffler 	}
1202b7e3f244SSam Leffler 
1203b7e3f244SSam Leffler 	/*
1204b7e3f244SSam Leffler 	 * All done with setup; fillin the SA command words
1205b7e3f244SSam Leffler 	 * and the packet engine descriptor.  The operation
1206b7e3f244SSam Leffler 	 * is now ready for submission to the hardware.
1207b7e3f244SSam Leffler 	 */
1208b7e3f244SSam Leffler 	sa->sa_cmd0 = cmd0 | SAFE_SA_CMD0_IPCI | SAFE_SA_CMD0_OPCI;
1209b7e3f244SSam Leffler 	sa->sa_cmd1 = cmd1
1210b7e3f244SSam Leffler 		    | (coffset << SAFE_SA_CMD1_OFFSET_S)
1211b7e3f244SSam Leffler 		    | SAFE_SA_CMD1_SAREV1	/* Rev 1 SA data structure */
1212b7e3f244SSam Leffler 		    | SAFE_SA_CMD1_SRPCI
1213b7e3f244SSam Leffler 		    ;
1214b7e3f244SSam Leffler 	/*
1215b7e3f244SSam Leffler 	 * NB: the order of writes is important here.  In case the
1216b7e3f244SSam Leffler 	 * chip is scanning the ring because of an outstanding request
1217b7e3f244SSam Leffler 	 * it might nab this one too.  In that case we need to make
1218b7e3f244SSam Leffler 	 * sure the setup is complete before we write the length
1219b7e3f244SSam Leffler 	 * field of the descriptor as it signals the descriptor is
1220b7e3f244SSam Leffler 	 * ready for processing.
1221b7e3f244SSam Leffler 	 */
1222b7e3f244SSam Leffler 	re->re_desc.d_csr = SAFE_PE_CSR_READY | SAFE_PE_CSR_SAPCI;
1223c0341432SJohn Baldwin 	if (csp->csp_auth_alg != 0)
1224b7e3f244SSam Leffler 		re->re_desc.d_csr |= SAFE_PE_CSR_LOADSA | SAFE_PE_CSR_HASHFINAL;
1225b7e3f244SSam Leffler 	re->re_desc.d_len = oplen
1226b7e3f244SSam Leffler 			  | SAFE_PE_LEN_READY
1227b7e3f244SSam Leffler 			  | (bypass << SAFE_PE_LEN_BYPASS_S)
1228b7e3f244SSam Leffler 			  ;
1229b7e3f244SSam Leffler 
1230b7e3f244SSam Leffler 	safestats.st_ipackets++;
1231b7e3f244SSam Leffler 	safestats.st_ibytes += oplen;
1232b7e3f244SSam Leffler 
1233b7e3f244SSam Leffler 	if (++(sc->sc_front) == sc->sc_ringtop)
1234b7e3f244SSam Leffler 		sc->sc_front = sc->sc_ring;
1235b7e3f244SSam Leffler 
1236b7e3f244SSam Leffler 	/* XXX honor batching */
1237b7e3f244SSam Leffler 	safe_feed(sc, re);
1238b7e3f244SSam Leffler 	mtx_unlock(&sc->sc_ringmtx);
1239b7e3f244SSam Leffler 	return (0);
1240b7e3f244SSam Leffler 
1241b7e3f244SSam Leffler errout:
1242c0341432SJohn Baldwin 	if (re->re_dst_m != NULL)
1243b7e3f244SSam Leffler 		m_freem(re->re_dst_m);
1244b7e3f244SSam Leffler 
1245b7e3f244SSam Leffler 	if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) {
1246b7e3f244SSam Leffler 		bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map);
1247b7e3f244SSam Leffler 		bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map);
1248b7e3f244SSam Leffler 	}
1249b7e3f244SSam Leffler 	if (re->re_src_map != NULL) {
1250b7e3f244SSam Leffler 		bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map);
1251b7e3f244SSam Leffler 		bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
1252b7e3f244SSam Leffler 	}
1253b7e3f244SSam Leffler 	mtx_unlock(&sc->sc_ringmtx);
1254b7e3f244SSam Leffler 	if (err != ERESTART) {
1255b7e3f244SSam Leffler 		crp->crp_etype = err;
1256b7e3f244SSam Leffler 		crypto_done(crp);
1257895c98ccSJohn Baldwin 		err = 0;
1258b7e3f244SSam Leffler 	} else {
1259b7e3f244SSam Leffler 		sc->sc_needwakeup |= CRYPTO_SYMQ;
1260b7e3f244SSam Leffler 	}
1261b7e3f244SSam Leffler 	return (err);
1262b7e3f244SSam Leffler }
1263b7e3f244SSam Leffler 
1264b7e3f244SSam Leffler static void
safe_callback(struct safe_softc * sc,struct safe_ringentry * re)1265b7e3f244SSam Leffler safe_callback(struct safe_softc *sc, struct safe_ringentry *re)
1266b7e3f244SSam Leffler {
1267c0341432SJohn Baldwin 	const struct crypto_session_params *csp;
1268b7e3f244SSam Leffler 	struct cryptop *crp = (struct cryptop *)re->re_crp;
12691b0909d5SConrad Meyer 	struct safe_session *ses;
1270c0341432SJohn Baldwin 	uint8_t hash[HASH_MAX_LEN];
1271b7e3f244SSam Leffler 
12721b0909d5SConrad Meyer 	ses = crypto_get_driver_session(crp->crp_session);
1273c0341432SJohn Baldwin 	csp = crypto_get_params(crp->crp_session);
12741b0909d5SConrad Meyer 
1275b7e3f244SSam Leffler 	safestats.st_opackets++;
1276b7e3f244SSam Leffler 	safestats.st_obytes += re->re_dst.mapsize;
1277b7e3f244SSam Leffler 
1278b7e3f244SSam Leffler 	safe_dma_sync(&sc->sc_ringalloc,
1279b7e3f244SSam Leffler 		BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1280b7e3f244SSam Leffler 	if (re->re_desc.d_csr & SAFE_PE_CSR_STATUS) {
1281b7e3f244SSam Leffler 		device_printf(sc->sc_dev, "csr 0x%x cmd0 0x%x cmd1 0x%x\n",
1282b7e3f244SSam Leffler 			re->re_desc.d_csr,
1283b7e3f244SSam Leffler 			re->re_sa.sa_cmd0, re->re_sa.sa_cmd1);
1284b7e3f244SSam Leffler 		safestats.st_peoperr++;
1285b7e3f244SSam Leffler 		crp->crp_etype = EIO;		/* something more meaningful? */
1286b7e3f244SSam Leffler 	}
1287c0341432SJohn Baldwin 
12889c0e3d3aSJohn Baldwin 	/*
12899c0e3d3aSJohn Baldwin 	 * XXX: Should crp_buf.cb_mbuf be updated to re->re_dst_m if
12909c0e3d3aSJohn Baldwin 	 * it is non-NULL?
12919c0e3d3aSJohn Baldwin 	 */
1292c0341432SJohn Baldwin 
1293b7e3f244SSam Leffler 	if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) {
1294b7e3f244SSam Leffler 		bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map,
1295b7e3f244SSam Leffler 		    BUS_DMASYNC_POSTREAD);
1296b7e3f244SSam Leffler 		bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map);
1297b7e3f244SSam Leffler 		bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map);
1298b7e3f244SSam Leffler 	}
1299b7e3f244SSam Leffler 	bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_POSTWRITE);
1300b7e3f244SSam Leffler 	bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map);
1301b7e3f244SSam Leffler 	bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
1302b7e3f244SSam Leffler 
1303b7e3f244SSam Leffler 	if (re->re_flags & SAFE_QFLAGS_COPYOUTICV) {
1304c0341432SJohn Baldwin 		if (csp->csp_auth_alg == CRYPTO_SHA1_HMAC) {
1305b7e3f244SSam Leffler 			/*
1306b7e3f244SSam Leffler 			 * SHA-1 ICV's are byte-swapped; fix 'em up
1307c0341432SJohn Baldwin 			 * before copying them to their destination.
1308b7e3f244SSam Leffler 			 */
1309357a26abSXin LI 			re->re_sastate.sa_saved_indigest[0] =
1310b7e3f244SSam Leffler 			    bswap32(re->re_sastate.sa_saved_indigest[0]);
1311357a26abSXin LI 			re->re_sastate.sa_saved_indigest[1] =
1312b7e3f244SSam Leffler 			    bswap32(re->re_sastate.sa_saved_indigest[1]);
1313357a26abSXin LI 			re->re_sastate.sa_saved_indigest[2] =
1314b7e3f244SSam Leffler 			    bswap32(re->re_sastate.sa_saved_indigest[2]);
1315b7e3f244SSam Leffler 		}
1316c0341432SJohn Baldwin 
1317c0341432SJohn Baldwin 		if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1318c0341432SJohn Baldwin 			crypto_copydata(crp, crp->crp_digest_start,
1319c0341432SJohn Baldwin 			    ses->ses_mlen, hash);
1320c0341432SJohn Baldwin 			if (timingsafe_bcmp(re->re_sastate.sa_saved_indigest,
1321c0341432SJohn Baldwin 			    hash, ses->ses_mlen) != 0)
1322c0341432SJohn Baldwin 				crp->crp_etype = EBADMSG;
1323c0341432SJohn Baldwin 		} else
1324c0341432SJohn Baldwin 			crypto_copyback(crp, crp->crp_digest_start,
1325c0341432SJohn Baldwin 			    ses->ses_mlen, re->re_sastate.sa_saved_indigest);
1326b7e3f244SSam Leffler 	}
1327b7e3f244SSam Leffler 	crypto_done(crp);
1328b7e3f244SSam Leffler }
1329b7e3f244SSam Leffler 
1330b7e3f244SSam Leffler /*
1331b7e3f244SSam Leffler  * Copy all data past offset from srcm to dstm.
1332b7e3f244SSam Leffler  */
1333b7e3f244SSam Leffler static void
safe_mcopy(struct mbuf * srcm,struct mbuf * dstm,u_int offset)1334b7e3f244SSam Leffler safe_mcopy(struct mbuf *srcm, struct mbuf *dstm, u_int offset)
1335b7e3f244SSam Leffler {
1336b7e3f244SSam Leffler 	u_int j, dlen, slen;
1337b7e3f244SSam Leffler 	caddr_t dptr, sptr;
1338b7e3f244SSam Leffler 
1339b7e3f244SSam Leffler 	/*
1340b7e3f244SSam Leffler 	 * Advance src and dst to offset.
1341b7e3f244SSam Leffler 	 */
1342b7e3f244SSam Leffler 	j = offset;
134319a9c3dfSRyan Libby 	while (j >= srcm->m_len) {
1344b7e3f244SSam Leffler 		j -= srcm->m_len;
1345b7e3f244SSam Leffler 		srcm = srcm->m_next;
1346b7e3f244SSam Leffler 		if (srcm == NULL)
1347b7e3f244SSam Leffler 			return;
1348b7e3f244SSam Leffler 	}
1349b7e3f244SSam Leffler 	sptr = mtod(srcm, caddr_t) + j;
1350b7e3f244SSam Leffler 	slen = srcm->m_len - j;
1351b7e3f244SSam Leffler 
1352b7e3f244SSam Leffler 	j = offset;
135319a9c3dfSRyan Libby 	while (j >= dstm->m_len) {
1354b7e3f244SSam Leffler 		j -= dstm->m_len;
1355b7e3f244SSam Leffler 		dstm = dstm->m_next;
1356b7e3f244SSam Leffler 		if (dstm == NULL)
1357b7e3f244SSam Leffler 			return;
1358b7e3f244SSam Leffler 	}
1359b7e3f244SSam Leffler 	dptr = mtod(dstm, caddr_t) + j;
1360b7e3f244SSam Leffler 	dlen = dstm->m_len - j;
1361b7e3f244SSam Leffler 
1362b7e3f244SSam Leffler 	/*
1363b7e3f244SSam Leffler 	 * Copy everything that remains.
1364b7e3f244SSam Leffler 	 */
1365b7e3f244SSam Leffler 	for (;;) {
1366b7e3f244SSam Leffler 		j = min(slen, dlen);
1367b7e3f244SSam Leffler 		bcopy(sptr, dptr, j);
1368b7e3f244SSam Leffler 		if (slen == j) {
1369b7e3f244SSam Leffler 			srcm = srcm->m_next;
1370b7e3f244SSam Leffler 			if (srcm == NULL)
1371b7e3f244SSam Leffler 				return;
1372b7e3f244SSam Leffler 			sptr = srcm->m_data;
1373b7e3f244SSam Leffler 			slen = srcm->m_len;
1374b7e3f244SSam Leffler 		} else
1375b7e3f244SSam Leffler 			sptr += j, slen -= j;
1376b7e3f244SSam Leffler 		if (dlen == j) {
1377b7e3f244SSam Leffler 			dstm = dstm->m_next;
1378b7e3f244SSam Leffler 			if (dstm == NULL)
1379b7e3f244SSam Leffler 				return;
1380b7e3f244SSam Leffler 			dptr = dstm->m_data;
1381b7e3f244SSam Leffler 			dlen = dstm->m_len;
1382b7e3f244SSam Leffler 		} else
1383b7e3f244SSam Leffler 			dptr += j, dlen -= j;
1384b7e3f244SSam Leffler 	}
1385b7e3f244SSam Leffler }
1386b7e3f244SSam Leffler 
1387b7e3f244SSam Leffler #ifndef SAFE_NO_RNG
1388b7e3f244SSam Leffler #define	SAFE_RNG_MAXWAIT	1000
1389b7e3f244SSam Leffler 
1390b7e3f244SSam Leffler static void
safe_rng_init(struct safe_softc * sc)1391b7e3f244SSam Leffler safe_rng_init(struct safe_softc *sc)
1392b7e3f244SSam Leffler {
1393b7e3f244SSam Leffler 	u_int32_t w, v;
1394b7e3f244SSam Leffler 	int i;
1395b7e3f244SSam Leffler 
1396b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_RNG_CTRL, 0);
1397b7e3f244SSam Leffler 	/* use default value according to the manual */
1398b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_RNG_CNFG, 0x834);	/* magic from SafeNet */
1399b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
1400b7e3f244SSam Leffler 
1401b7e3f244SSam Leffler 	/*
1402b7e3f244SSam Leffler 	 * There is a bug in rev 1.0 of the 1140 that when the RNG
1403b7e3f244SSam Leffler 	 * is brought out of reset the ready status flag does not
1404b7e3f244SSam Leffler 	 * work until the RNG has finished its internal initialization.
1405b7e3f244SSam Leffler 	 *
1406b7e3f244SSam Leffler 	 * So in order to determine the device is through its
1407b7e3f244SSam Leffler 	 * initialization we must read the data register, using the
1408b7e3f244SSam Leffler 	 * status reg in the read in case it is initialized.  Then read
1409b7e3f244SSam Leffler 	 * the data register until it changes from the first read.
1410b7e3f244SSam Leffler 	 * Once it changes read the data register until it changes
1411b7e3f244SSam Leffler 	 * again.  At this time the RNG is considered initialized.
1412b7e3f244SSam Leffler 	 * This could take between 750ms - 1000ms in time.
1413b7e3f244SSam Leffler 	 */
1414b7e3f244SSam Leffler 	i = 0;
1415b7e3f244SSam Leffler 	w = READ_REG(sc, SAFE_RNG_OUT);
1416b7e3f244SSam Leffler 	do {
1417b7e3f244SSam Leffler 		v = READ_REG(sc, SAFE_RNG_OUT);
1418b7e3f244SSam Leffler 		if (v != w) {
1419b7e3f244SSam Leffler 			w = v;
1420b7e3f244SSam Leffler 			break;
1421b7e3f244SSam Leffler 		}
1422b7e3f244SSam Leffler 		DELAY(10);
1423b7e3f244SSam Leffler 	} while (++i < SAFE_RNG_MAXWAIT);
1424b7e3f244SSam Leffler 
1425b7e3f244SSam Leffler 	/* Wait Until data changes again */
1426b7e3f244SSam Leffler 	i = 0;
1427b7e3f244SSam Leffler 	do {
1428b7e3f244SSam Leffler 		v = READ_REG(sc, SAFE_RNG_OUT);
1429b7e3f244SSam Leffler 		if (v != w)
1430b7e3f244SSam Leffler 			break;
1431b7e3f244SSam Leffler 		DELAY(10);
1432b7e3f244SSam Leffler 	} while (++i < SAFE_RNG_MAXWAIT);
1433b7e3f244SSam Leffler }
1434b7e3f244SSam Leffler 
1435b7e3f244SSam Leffler static __inline void
safe_rng_disable_short_cycle(struct safe_softc * sc)1436b7e3f244SSam Leffler safe_rng_disable_short_cycle(struct safe_softc *sc)
1437b7e3f244SSam Leffler {
1438b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_RNG_CTRL,
1439b7e3f244SSam Leffler 		READ_REG(sc, SAFE_RNG_CTRL) &~ SAFE_RNG_CTRL_SHORTEN);
1440b7e3f244SSam Leffler }
1441b7e3f244SSam Leffler 
1442b7e3f244SSam Leffler static __inline void
safe_rng_enable_short_cycle(struct safe_softc * sc)1443b7e3f244SSam Leffler safe_rng_enable_short_cycle(struct safe_softc *sc)
1444b7e3f244SSam Leffler {
1445b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_RNG_CTRL,
1446b7e3f244SSam Leffler 		READ_REG(sc, SAFE_RNG_CTRL) | SAFE_RNG_CTRL_SHORTEN);
1447b7e3f244SSam Leffler }
1448b7e3f244SSam Leffler 
1449b7e3f244SSam Leffler static __inline u_int32_t
safe_rng_read(struct safe_softc * sc)1450b7e3f244SSam Leffler safe_rng_read(struct safe_softc *sc)
1451b7e3f244SSam Leffler {
1452b7e3f244SSam Leffler 	int i;
1453b7e3f244SSam Leffler 
1454b7e3f244SSam Leffler 	i = 0;
1455b7e3f244SSam Leffler 	while (READ_REG(sc, SAFE_RNG_STAT) != 0 && ++i < SAFE_RNG_MAXWAIT)
1456b7e3f244SSam Leffler 		;
1457b7e3f244SSam Leffler 	return READ_REG(sc, SAFE_RNG_OUT);
1458b7e3f244SSam Leffler }
1459b7e3f244SSam Leffler 
1460b7e3f244SSam Leffler static void
safe_rng(void * arg)1461b7e3f244SSam Leffler safe_rng(void *arg)
1462b7e3f244SSam Leffler {
1463b7e3f244SSam Leffler 	struct safe_softc *sc = arg;
1464b7e3f244SSam Leffler 	u_int32_t buf[SAFE_RNG_MAXBUFSIZ];	/* NB: maybe move to softc */
1465b7e3f244SSam Leffler 	u_int maxwords;
1466b7e3f244SSam Leffler 	int i;
1467b7e3f244SSam Leffler 
1468b7e3f244SSam Leffler 	safestats.st_rng++;
1469b7e3f244SSam Leffler 	/*
1470b7e3f244SSam Leffler 	 * Fetch the next block of data.
1471b7e3f244SSam Leffler 	 */
1472b7e3f244SSam Leffler 	maxwords = safe_rngbufsize;
1473b7e3f244SSam Leffler 	if (maxwords > SAFE_RNG_MAXBUFSIZ)
1474b7e3f244SSam Leffler 		maxwords = SAFE_RNG_MAXBUFSIZ;
1475b7e3f244SSam Leffler retry:
1476b7e3f244SSam Leffler 	for (i = 0; i < maxwords; i++)
1477b7e3f244SSam Leffler 		buf[i] = safe_rng_read(sc);
1478b7e3f244SSam Leffler 	/*
1479b7e3f244SSam Leffler 	 * Check the comparator alarm count and reset the h/w if
1480b7e3f244SSam Leffler 	 * it exceeds our threshold.  This guards against the
1481b7e3f244SSam Leffler 	 * hardware oscillators resonating with external signals.
1482b7e3f244SSam Leffler 	 */
1483b7e3f244SSam Leffler 	if (READ_REG(sc, SAFE_RNG_ALM_CNT) > safe_rngmaxalarm) {
1484b7e3f244SSam Leffler 		u_int32_t freq_inc, w;
1485b7e3f244SSam Leffler 
1486b7e3f244SSam Leffler 		DPRINTF(("%s: alarm count %u exceeds threshold %u\n", __func__,
1487b7e3f244SSam Leffler 			READ_REG(sc, SAFE_RNG_ALM_CNT), safe_rngmaxalarm));
1488b7e3f244SSam Leffler 		safestats.st_rngalarm++;
1489b7e3f244SSam Leffler 		safe_rng_enable_short_cycle(sc);
1490b7e3f244SSam Leffler 		freq_inc = 18;
1491b7e3f244SSam Leffler 		for (i = 0; i < 64; i++) {
1492b7e3f244SSam Leffler 			w = READ_REG(sc, SAFE_RNG_CNFG);
1493b7e3f244SSam Leffler 			freq_inc = ((w + freq_inc) & 0x3fL);
1494b7e3f244SSam Leffler 			w = ((w & ~0x3fL) | freq_inc);
1495b7e3f244SSam Leffler 			WRITE_REG(sc, SAFE_RNG_CNFG, w);
1496b7e3f244SSam Leffler 
1497b7e3f244SSam Leffler 			WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
1498b7e3f244SSam Leffler 
1499b7e3f244SSam Leffler 			(void) safe_rng_read(sc);
1500b7e3f244SSam Leffler 			DELAY(25);
1501b7e3f244SSam Leffler 
1502b7e3f244SSam Leffler 			if (READ_REG(sc, SAFE_RNG_ALM_CNT) == 0) {
1503b7e3f244SSam Leffler 				safe_rng_disable_short_cycle(sc);
1504b7e3f244SSam Leffler 				goto retry;
1505b7e3f244SSam Leffler 			}
1506b7e3f244SSam Leffler 			freq_inc = 1;
1507b7e3f244SSam Leffler 		}
1508b7e3f244SSam Leffler 		safe_rng_disable_short_cycle(sc);
1509b7e3f244SSam Leffler 	} else
1510b7e3f244SSam Leffler 		WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
1511b7e3f244SSam Leffler 
1512b7e3f244SSam Leffler 	(*sc->sc_harvest)(sc->sc_rndtest, buf, maxwords*sizeof (u_int32_t));
1513b7e3f244SSam Leffler 	callout_reset(&sc->sc_rngto,
1514b7e3f244SSam Leffler 		hz * (safe_rnginterval ? safe_rnginterval : 1), safe_rng, sc);
1515b7e3f244SSam Leffler }
1516b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */
1517b7e3f244SSam Leffler 
1518b7e3f244SSam Leffler static void
safe_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)1519b7e3f244SSam Leffler safe_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1520b7e3f244SSam Leffler {
1521b7e3f244SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
1522b7e3f244SSam Leffler 	*paddr = segs->ds_addr;
1523b7e3f244SSam Leffler }
1524b7e3f244SSam Leffler 
1525b7e3f244SSam Leffler static int
safe_dma_malloc(struct safe_softc * sc,bus_size_t size,struct safe_dma_alloc * dma,int mapflags)1526b7e3f244SSam Leffler safe_dma_malloc(
1527b7e3f244SSam Leffler 	struct safe_softc *sc,
1528b7e3f244SSam Leffler 	bus_size_t size,
1529b7e3f244SSam Leffler 	struct safe_dma_alloc *dma,
1530b7e3f244SSam Leffler 	int mapflags
1531b7e3f244SSam Leffler )
1532b7e3f244SSam Leffler {
1533b7e3f244SSam Leffler 	int r;
1534b7e3f244SSam Leffler 
153562ce43ccSScott Long 	r = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
1536b7e3f244SSam Leffler 			       sizeof(u_int32_t), 0,	/* alignment, bounds */
1537b7e3f244SSam Leffler 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1538b7e3f244SSam Leffler 			       BUS_SPACE_MAXADDR,	/* highaddr */
1539b7e3f244SSam Leffler 			       NULL, NULL,		/* filter, filterarg */
1540b7e3f244SSam Leffler 			       size,			/* maxsize */
1541b7e3f244SSam Leffler 			       1,			/* nsegments */
1542b7e3f244SSam Leffler 			       size,			/* maxsegsize */
1543b7e3f244SSam Leffler 			       BUS_DMA_ALLOCNOW,	/* flags */
1544b7e3f244SSam Leffler 			       NULL, NULL,		/* locking */
1545b7e3f244SSam Leffler 			       &dma->dma_tag);
1546b7e3f244SSam Leffler 	if (r != 0) {
1547b7e3f244SSam Leffler 		device_printf(sc->sc_dev, "safe_dma_malloc: "
1548b7e3f244SSam Leffler 			"bus_dma_tag_create failed; error %u\n", r);
1549b7e3f244SSam Leffler 		goto fail_0;
1550b7e3f244SSam Leffler 	}
1551b7e3f244SSam Leffler 
1552b7e3f244SSam Leffler 	r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
1553b7e3f244SSam Leffler 			     BUS_DMA_NOWAIT, &dma->dma_map);
1554b7e3f244SSam Leffler 	if (r != 0) {
1555b7e3f244SSam Leffler 		device_printf(sc->sc_dev, "safe_dma_malloc: "
1556d14c4346SJohn-Mark Gurney 			"bus_dmammem_alloc failed; size %ju, error %u\n",
1557d14c4346SJohn-Mark Gurney 			(uintmax_t)size, r);
1558f07894dbSJohn Baldwin 		goto fail_1;
1559b7e3f244SSam Leffler 	}
1560b7e3f244SSam Leffler 
1561b7e3f244SSam Leffler 	r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
1562b7e3f244SSam Leffler 		            size,
1563b7e3f244SSam Leffler 			    safe_dmamap_cb,
1564b7e3f244SSam Leffler 			    &dma->dma_paddr,
1565b7e3f244SSam Leffler 			    mapflags | BUS_DMA_NOWAIT);
1566b7e3f244SSam Leffler 	if (r != 0) {
1567b7e3f244SSam Leffler 		device_printf(sc->sc_dev, "safe_dma_malloc: "
1568b7e3f244SSam Leffler 			"bus_dmamap_load failed; error %u\n", r);
1569f07894dbSJohn Baldwin 		goto fail_2;
1570b7e3f244SSam Leffler 	}
1571b7e3f244SSam Leffler 
1572b7e3f244SSam Leffler 	dma->dma_size = size;
1573b7e3f244SSam Leffler 	return (0);
1574b7e3f244SSam Leffler 
1575b7e3f244SSam Leffler 	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1576b7e3f244SSam Leffler fail_2:
1577b7e3f244SSam Leffler 	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1578b7e3f244SSam Leffler fail_1:
1579b7e3f244SSam Leffler 	bus_dma_tag_destroy(dma->dma_tag);
1580b7e3f244SSam Leffler fail_0:
1581b7e3f244SSam Leffler 	dma->dma_tag = NULL;
1582b7e3f244SSam Leffler 	return (r);
1583b7e3f244SSam Leffler }
1584b7e3f244SSam Leffler 
1585b7e3f244SSam Leffler static void
safe_dma_free(struct safe_softc * sc,struct safe_dma_alloc * dma)1586b7e3f244SSam Leffler safe_dma_free(struct safe_softc *sc, struct safe_dma_alloc *dma)
1587b7e3f244SSam Leffler {
1588b7e3f244SSam Leffler 	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
1589b7e3f244SSam Leffler 	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
1590b7e3f244SSam Leffler 	bus_dma_tag_destroy(dma->dma_tag);
1591b7e3f244SSam Leffler }
1592b7e3f244SSam Leffler 
1593b7e3f244SSam Leffler /*
1594b7e3f244SSam Leffler  * Resets the board.  Values in the regesters are left as is
1595b7e3f244SSam Leffler  * from the reset (i.e. initial values are assigned elsewhere).
1596b7e3f244SSam Leffler  */
1597b7e3f244SSam Leffler static void
safe_reset_board(struct safe_softc * sc)1598b7e3f244SSam Leffler safe_reset_board(struct safe_softc *sc)
1599b7e3f244SSam Leffler {
1600b7e3f244SSam Leffler 	u_int32_t v;
1601b7e3f244SSam Leffler 	/*
1602b7e3f244SSam Leffler 	 * Reset the device.  The manual says no delay
1603b7e3f244SSam Leffler 	 * is needed between marking and clearing reset.
1604b7e3f244SSam Leffler 	 */
1605b7e3f244SSam Leffler 	v = READ_REG(sc, SAFE_PE_DMACFG) &~
1606b7e3f244SSam Leffler 		(SAFE_PE_DMACFG_PERESET | SAFE_PE_DMACFG_PDRRESET |
1607b7e3f244SSam Leffler 		 SAFE_PE_DMACFG_SGRESET);
1608b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_DMACFG, v
1609b7e3f244SSam Leffler 				    | SAFE_PE_DMACFG_PERESET
1610b7e3f244SSam Leffler 				    | SAFE_PE_DMACFG_PDRRESET
1611b7e3f244SSam Leffler 				    | SAFE_PE_DMACFG_SGRESET);
1612b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_DMACFG, v);
1613b7e3f244SSam Leffler }
1614b7e3f244SSam Leffler 
1615b7e3f244SSam Leffler /*
1616b7e3f244SSam Leffler  * Initialize registers we need to touch only once.
1617b7e3f244SSam Leffler  */
1618b7e3f244SSam Leffler static void
safe_init_board(struct safe_softc * sc)1619b7e3f244SSam Leffler safe_init_board(struct safe_softc *sc)
1620b7e3f244SSam Leffler {
1621b7e3f244SSam Leffler 	u_int32_t v, dwords;
1622b7e3f244SSam Leffler 
1623c2ede4b3SMartin Blapp 	v = READ_REG(sc, SAFE_PE_DMACFG);
1624b7e3f244SSam Leffler 	v &=~ SAFE_PE_DMACFG_PEMODE;
1625b7e3f244SSam Leffler 	v |= SAFE_PE_DMACFG_FSENA		/* failsafe enable */
1626b7e3f244SSam Leffler 	  |  SAFE_PE_DMACFG_GPRPCI		/* gather ring on PCI */
1627b7e3f244SSam Leffler 	  |  SAFE_PE_DMACFG_SPRPCI		/* scatter ring on PCI */
1628b7e3f244SSam Leffler 	  |  SAFE_PE_DMACFG_ESDESC		/* endian-swap descriptors */
1629b7e3f244SSam Leffler 	  |  SAFE_PE_DMACFG_ESSA		/* endian-swap SA's */
1630b7e3f244SSam Leffler 	  |  SAFE_PE_DMACFG_ESPDESC		/* endian-swap part. desc's */
1631b7e3f244SSam Leffler 	  ;
1632b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_DMACFG, v);
1633b7e3f244SSam Leffler #if 0
1634b7e3f244SSam Leffler 	/* XXX select byte swap based on host byte order */
1635b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_ENDIAN, 0x1b);
1636b7e3f244SSam Leffler #endif
1637b7e3f244SSam Leffler 	if (sc->sc_chiprev == SAFE_REV(1,0)) {
1638b7e3f244SSam Leffler 		/*
1639b7e3f244SSam Leffler 		 * Avoid large PCI DMA transfers.  Rev 1.0 has a bug where
1640b7e3f244SSam Leffler 		 * "target mode transfers" done while the chip is DMA'ing
1641b7e3f244SSam Leffler 		 * >1020 bytes cause the hardware to lockup.  To avoid this
1642b7e3f244SSam Leffler 		 * we reduce the max PCI transfer size and use small source
1643b7e3f244SSam Leffler 		 * particle descriptors (<= 256 bytes).
1644b7e3f244SSam Leffler 		 */
1645b7e3f244SSam Leffler 		WRITE_REG(sc, SAFE_DMA_CFG, 256);
1646b7e3f244SSam Leffler 		device_printf(sc->sc_dev,
1647b7e3f244SSam Leffler 			"Reduce max DMA size to %u words for rev %u.%u WAR\n",
1648b7e3f244SSam Leffler 			(READ_REG(sc, SAFE_DMA_CFG)>>2) & 0xff,
1649b7e3f244SSam Leffler 			SAFE_REV_MAJ(sc->sc_chiprev),
1650b7e3f244SSam Leffler 			SAFE_REV_MIN(sc->sc_chiprev));
1651b7e3f244SSam Leffler 	}
1652b7e3f244SSam Leffler 
1653b7e3f244SSam Leffler 	/* NB: operands+results are overlaid */
1654b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_PDRBASE, sc->sc_ringalloc.dma_paddr);
1655b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_RDRBASE, sc->sc_ringalloc.dma_paddr);
1656b7e3f244SSam Leffler 	/*
1657b7e3f244SSam Leffler 	 * Configure ring entry size and number of items in the ring.
1658b7e3f244SSam Leffler 	 */
1659b7e3f244SSam Leffler 	KASSERT((sizeof(struct safe_ringentry) % sizeof(u_int32_t)) == 0,
1660b7e3f244SSam Leffler 		("PE ring entry not 32-bit aligned!"));
1661b7e3f244SSam Leffler 	dwords = sizeof(struct safe_ringentry) / sizeof(u_int32_t);
1662b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_RINGCFG,
1663b7e3f244SSam Leffler 		(dwords << SAFE_PE_RINGCFG_OFFSET_S) | SAFE_MAX_NQUEUE);
1664b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_RINGPOLL, 0);	/* disable polling */
1665b7e3f244SSam Leffler 
1666b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_GRNGBASE, sc->sc_spalloc.dma_paddr);
1667b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_SRNGBASE, sc->sc_dpalloc.dma_paddr);
1668b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_PARTSIZE,
1669b7e3f244SSam Leffler 		(SAFE_TOTAL_DPART<<16) | SAFE_TOTAL_SPART);
1670b7e3f244SSam Leffler 	/*
1671b7e3f244SSam Leffler 	 * NB: destination particles are fixed size.  We use
1672b7e3f244SSam Leffler 	 *     an mbuf cluster and require all results go to
1673b7e3f244SSam Leffler 	 *     clusters or smaller.
1674b7e3f244SSam Leffler 	 */
1675b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_PARTCFG, SAFE_MAX_DSIZE);
1676b7e3f244SSam Leffler 
1677b7e3f244SSam Leffler 	/* it's now safe to enable PE mode, do it */
1678b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_PE_DMACFG, v | SAFE_PE_DMACFG_PEMODE);
1679b7e3f244SSam Leffler 
1680b7e3f244SSam Leffler 	/*
1681b7e3f244SSam Leffler 	 * Configure hardware to use level-triggered interrupts and
1682b7e3f244SSam Leffler 	 * to interrupt after each descriptor is processed.
1683b7e3f244SSam Leffler 	 */
1684b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_HI_CFG, SAFE_HI_CFG_LEVEL);
1685b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_HI_DESC_CNT, 1);
1686b7e3f244SSam Leffler 	WRITE_REG(sc, SAFE_HI_MASK, SAFE_INT_PE_DDONE | SAFE_INT_PE_ERROR);
1687b7e3f244SSam Leffler }
1688b7e3f244SSam Leffler 
1689b7e3f244SSam Leffler /*
1690b7e3f244SSam Leffler  * Init PCI registers
1691b7e3f244SSam Leffler  */
1692b7e3f244SSam Leffler static void
safe_init_pciregs(device_t dev)1693b7e3f244SSam Leffler safe_init_pciregs(device_t dev)
1694b7e3f244SSam Leffler {
1695b7e3f244SSam Leffler }
1696b7e3f244SSam Leffler 
1697b7e3f244SSam Leffler /*
1698b7e3f244SSam Leffler  * Clean up after a chip crash.
1699b7e3f244SSam Leffler  * It is assumed that the caller in splimp()
1700b7e3f244SSam Leffler  */
1701b7e3f244SSam Leffler static void
safe_cleanchip(struct safe_softc * sc)1702b7e3f244SSam Leffler safe_cleanchip(struct safe_softc *sc)
1703b7e3f244SSam Leffler {
1704b7e3f244SSam Leffler 
1705b7e3f244SSam Leffler 	if (sc->sc_nqchip != 0) {
1706b7e3f244SSam Leffler 		struct safe_ringentry *re = sc->sc_back;
1707b7e3f244SSam Leffler 
1708b7e3f244SSam Leffler 		while (re != sc->sc_front) {
1709b7e3f244SSam Leffler 			if (re->re_desc.d_csr != 0)
1710b7e3f244SSam Leffler 				safe_free_entry(sc, re);
1711b7e3f244SSam Leffler 			if (++re == sc->sc_ringtop)
1712b7e3f244SSam Leffler 				re = sc->sc_ring;
1713b7e3f244SSam Leffler 		}
1714b7e3f244SSam Leffler 		sc->sc_back = re;
1715b7e3f244SSam Leffler 		sc->sc_nqchip = 0;
1716b7e3f244SSam Leffler 	}
1717b7e3f244SSam Leffler }
1718b7e3f244SSam Leffler 
1719b7e3f244SSam Leffler /*
1720b7e3f244SSam Leffler  * free a safe_q
1721b7e3f244SSam Leffler  * It is assumed that the caller is within splimp().
1722b7e3f244SSam Leffler  */
1723b7e3f244SSam Leffler static int
safe_free_entry(struct safe_softc * sc,struct safe_ringentry * re)1724b7e3f244SSam Leffler safe_free_entry(struct safe_softc *sc, struct safe_ringentry *re)
1725b7e3f244SSam Leffler {
1726b7e3f244SSam Leffler 	struct cryptop *crp;
1727b7e3f244SSam Leffler 
1728b7e3f244SSam Leffler 	/*
1729b7e3f244SSam Leffler 	 * Free header MCR
1730b7e3f244SSam Leffler 	 */
1731c0341432SJohn Baldwin 	if (re->re_dst_m != NULL)
1732b7e3f244SSam Leffler 		m_freem(re->re_dst_m);
1733b7e3f244SSam Leffler 
1734b7e3f244SSam Leffler 	crp = (struct cryptop *)re->re_crp;
1735b7e3f244SSam Leffler 
1736b7e3f244SSam Leffler 	re->re_desc.d_csr = 0;
1737b7e3f244SSam Leffler 
1738b7e3f244SSam Leffler 	crp->crp_etype = EFAULT;
1739b7e3f244SSam Leffler 	crypto_done(crp);
1740b7e3f244SSam Leffler 	return(0);
1741b7e3f244SSam Leffler }
1742b7e3f244SSam Leffler 
1743b7e3f244SSam Leffler /*
1744b7e3f244SSam Leffler  * Routine to reset the chip and clean up.
1745b7e3f244SSam Leffler  * It is assumed that the caller is in splimp()
1746b7e3f244SSam Leffler  */
1747b7e3f244SSam Leffler static void
safe_totalreset(struct safe_softc * sc)1748b7e3f244SSam Leffler safe_totalreset(struct safe_softc *sc)
1749b7e3f244SSam Leffler {
1750b7e3f244SSam Leffler 	safe_reset_board(sc);
1751b7e3f244SSam Leffler 	safe_init_board(sc);
1752b7e3f244SSam Leffler 	safe_cleanchip(sc);
1753b7e3f244SSam Leffler }
1754b7e3f244SSam Leffler 
1755b7e3f244SSam Leffler /*
1756b7e3f244SSam Leffler  * Is the operand suitable aligned for direct DMA.  Each
1757b7e3f244SSam Leffler  * segment must be aligned on a 32-bit boundary and all
1758b7e3f244SSam Leffler  * but the last segment must be a multiple of 4 bytes.
1759b7e3f244SSam Leffler  */
1760b7e3f244SSam Leffler static int
safe_dmamap_aligned(const struct safe_operand * op)1761b7e3f244SSam Leffler safe_dmamap_aligned(const struct safe_operand *op)
1762b7e3f244SSam Leffler {
1763b7e3f244SSam Leffler 	int i;
1764b7e3f244SSam Leffler 
1765b7e3f244SSam Leffler 	for (i = 0; i < op->nsegs; i++) {
1766b7e3f244SSam Leffler 		if (op->segs[i].ds_addr & 3)
1767b7e3f244SSam Leffler 			return (0);
1768b7e3f244SSam Leffler 		if (i != (op->nsegs - 1) && (op->segs[i].ds_len & 3))
1769b7e3f244SSam Leffler 			return (0);
1770b7e3f244SSam Leffler 	}
1771b7e3f244SSam Leffler 	return (1);
1772b7e3f244SSam Leffler }
1773b7e3f244SSam Leffler 
1774b7e3f244SSam Leffler /*
1775b7e3f244SSam Leffler  * Is the operand suitable for direct DMA as the destination
1776b7e3f244SSam Leffler  * of an operation.  The hardware requires that each ``particle''
1777b7e3f244SSam Leffler  * but the last in an operation result have the same size.  We
1778b7e3f244SSam Leffler  * fix that size at SAFE_MAX_DSIZE bytes.  This routine returns
1779*1bbdcf62SGordon Bergling  * 0 if some segment is not a multiple of this size, 1 if all
1780b7e3f244SSam Leffler  * segments are exactly this size, or 2 if segments are at worst
178181d4309fSGordon Bergling  * a multiple of this size.
1782b7e3f244SSam Leffler  */
1783b7e3f244SSam Leffler static int
safe_dmamap_uniform(const struct safe_operand * op)1784b7e3f244SSam Leffler safe_dmamap_uniform(const struct safe_operand *op)
1785b7e3f244SSam Leffler {
1786b7e3f244SSam Leffler 	int result = 1;
1787b7e3f244SSam Leffler 
1788b7e3f244SSam Leffler 	if (op->nsegs > 0) {
1789b7e3f244SSam Leffler 		int i;
1790b7e3f244SSam Leffler 
1791900017e8SSam Leffler 		for (i = 0; i < op->nsegs-1; i++) {
1792b7e3f244SSam Leffler 			if (op->segs[i].ds_len % SAFE_MAX_DSIZE)
1793b7e3f244SSam Leffler 				return (0);
1794b7e3f244SSam Leffler 			if (op->segs[i].ds_len != SAFE_MAX_DSIZE)
1795b7e3f244SSam Leffler 				result = 2;
1796b7e3f244SSam Leffler 		}
1797900017e8SSam Leffler 	}
1798b7e3f244SSam Leffler 	return (result);
1799b7e3f244SSam Leffler }
1800b7e3f244SSam Leffler 
1801b7e3f244SSam Leffler #ifdef SAFE_DEBUG
1802b7e3f244SSam Leffler static void
safe_dump_dmastatus(struct safe_softc * sc,const char * tag)1803b7e3f244SSam Leffler safe_dump_dmastatus(struct safe_softc *sc, const char *tag)
1804b7e3f244SSam Leffler {
1805b7e3f244SSam Leffler 	printf("%s: ENDIAN 0x%x SRC 0x%x DST 0x%x STAT 0x%x\n"
1806b7e3f244SSam Leffler 		, tag
1807b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_DMA_ENDIAN)
1808b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_DMA_SRCADDR)
1809b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_DMA_DSTADDR)
1810b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_DMA_STAT)
1811b7e3f244SSam Leffler 	);
1812b7e3f244SSam Leffler }
1813b7e3f244SSam Leffler 
1814b7e3f244SSam Leffler static void
safe_dump_intrstate(struct safe_softc * sc,const char * tag)1815b7e3f244SSam Leffler safe_dump_intrstate(struct safe_softc *sc, const char *tag)
1816b7e3f244SSam Leffler {
1817b7e3f244SSam Leffler 	printf("%s: HI_CFG 0x%x HI_MASK 0x%x HI_DESC_CNT 0x%x HU_STAT 0x%x HM_STAT 0x%x\n"
1818b7e3f244SSam Leffler 		, tag
1819b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_HI_CFG)
1820b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_HI_MASK)
1821b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_HI_DESC_CNT)
1822b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_HU_STAT)
1823b7e3f244SSam Leffler 		, READ_REG(sc, SAFE_HM_STAT)
1824b7e3f244SSam Leffler 	);
1825b7e3f244SSam Leffler }
1826b7e3f244SSam Leffler 
1827b7e3f244SSam Leffler static void
safe_dump_ringstate(struct safe_softc * sc,const char * tag)1828b7e3f244SSam Leffler safe_dump_ringstate(struct safe_softc *sc, const char *tag)
1829b7e3f244SSam Leffler {
1830b7e3f244SSam Leffler 	u_int32_t estat = READ_REG(sc, SAFE_PE_ERNGSTAT);
1831b7e3f244SSam Leffler 
1832b7e3f244SSam Leffler 	/* NB: assume caller has lock on ring */
1833668329e9SPeter Wemm 	printf("%s: ERNGSTAT %x (next %u) back %lu front %lu\n",
1834b7e3f244SSam Leffler 		tag,
1835b7e3f244SSam Leffler 		estat, (estat >> SAFE_PE_ERNGSTAT_NEXT_S),
1836668329e9SPeter Wemm 		(unsigned long)(sc->sc_back - sc->sc_ring),
1837668329e9SPeter Wemm 		(unsigned long)(sc->sc_front - sc->sc_ring));
1838b7e3f244SSam Leffler }
1839b7e3f244SSam Leffler 
1840b7e3f244SSam Leffler static void
safe_dump_request(struct safe_softc * sc,const char * tag,struct safe_ringentry * re)1841b7e3f244SSam Leffler safe_dump_request(struct safe_softc *sc, const char* tag, struct safe_ringentry *re)
1842b7e3f244SSam Leffler {
1843b7e3f244SSam Leffler 	int ix, nsegs;
1844b7e3f244SSam Leffler 
1845b7e3f244SSam Leffler 	ix = re - sc->sc_ring;
1846b7e3f244SSam Leffler 	printf("%s: %p (%u): csr %x src %x dst %x sa %x len %x\n"
1847b7e3f244SSam Leffler 		, tag
1848b7e3f244SSam Leffler 		, re, ix
1849b7e3f244SSam Leffler 		, re->re_desc.d_csr
1850b7e3f244SSam Leffler 		, re->re_desc.d_src
1851b7e3f244SSam Leffler 		, re->re_desc.d_dst
1852b7e3f244SSam Leffler 		, re->re_desc.d_sa
1853b7e3f244SSam Leffler 		, re->re_desc.d_len
1854b7e3f244SSam Leffler 	);
1855b7e3f244SSam Leffler 	if (re->re_src.nsegs > 1) {
1856b7e3f244SSam Leffler 		ix = (re->re_desc.d_src - sc->sc_spalloc.dma_paddr) /
1857b7e3f244SSam Leffler 			sizeof(struct safe_pdesc);
1858b7e3f244SSam Leffler 		for (nsegs = re->re_src.nsegs; nsegs; nsegs--) {
1859b7e3f244SSam Leffler 			printf(" spd[%u] %p: %p size %u flags %x"
1860b7e3f244SSam Leffler 				, ix, &sc->sc_spring[ix]
1861668329e9SPeter Wemm 				, (caddr_t)(uintptr_t) sc->sc_spring[ix].pd_addr
1862b7e3f244SSam Leffler 				, sc->sc_spring[ix].pd_size
1863b7e3f244SSam Leffler 				, sc->sc_spring[ix].pd_flags
1864b7e3f244SSam Leffler 			);
1865b7e3f244SSam Leffler 			if (sc->sc_spring[ix].pd_size == 0)
1866b7e3f244SSam Leffler 				printf(" (zero!)");
1867b7e3f244SSam Leffler 			printf("\n");
1868b7e3f244SSam Leffler 			if (++ix == SAFE_TOTAL_SPART)
1869b7e3f244SSam Leffler 				ix = 0;
1870b7e3f244SSam Leffler 		}
1871b7e3f244SSam Leffler 	}
1872b7e3f244SSam Leffler 	if (re->re_dst.nsegs > 1) {
1873b7e3f244SSam Leffler 		ix = (re->re_desc.d_dst - sc->sc_dpalloc.dma_paddr) /
1874b7e3f244SSam Leffler 			sizeof(struct safe_pdesc);
1875b7e3f244SSam Leffler 		for (nsegs = re->re_dst.nsegs; nsegs; nsegs--) {
1876b7e3f244SSam Leffler 			printf(" dpd[%u] %p: %p flags %x\n"
1877b7e3f244SSam Leffler 				, ix, &sc->sc_dpring[ix]
1878668329e9SPeter Wemm 				, (caddr_t)(uintptr_t) sc->sc_dpring[ix].pd_addr
1879b7e3f244SSam Leffler 				, sc->sc_dpring[ix].pd_flags
1880b7e3f244SSam Leffler 			);
1881b7e3f244SSam Leffler 			if (++ix == SAFE_TOTAL_DPART)
1882b7e3f244SSam Leffler 				ix = 0;
1883b7e3f244SSam Leffler 		}
1884b7e3f244SSam Leffler 	}
1885b7e3f244SSam Leffler 	printf("sa: cmd0 %08x cmd1 %08x staterec %x\n",
1886b7e3f244SSam Leffler 		re->re_sa.sa_cmd0, re->re_sa.sa_cmd1, re->re_sa.sa_staterec);
1887b7e3f244SSam Leffler 	printf("sa: key %x %x %x %x %x %x %x %x\n"
1888b7e3f244SSam Leffler 		, re->re_sa.sa_key[0]
1889b7e3f244SSam Leffler 		, re->re_sa.sa_key[1]
1890b7e3f244SSam Leffler 		, re->re_sa.sa_key[2]
1891b7e3f244SSam Leffler 		, re->re_sa.sa_key[3]
1892b7e3f244SSam Leffler 		, re->re_sa.sa_key[4]
1893b7e3f244SSam Leffler 		, re->re_sa.sa_key[5]
1894b7e3f244SSam Leffler 		, re->re_sa.sa_key[6]
1895b7e3f244SSam Leffler 		, re->re_sa.sa_key[7]
1896b7e3f244SSam Leffler 	);
1897b7e3f244SSam Leffler 	printf("sa: indigest %x %x %x %x %x\n"
1898b7e3f244SSam Leffler 		, re->re_sa.sa_indigest[0]
1899b7e3f244SSam Leffler 		, re->re_sa.sa_indigest[1]
1900b7e3f244SSam Leffler 		, re->re_sa.sa_indigest[2]
1901b7e3f244SSam Leffler 		, re->re_sa.sa_indigest[3]
1902b7e3f244SSam Leffler 		, re->re_sa.sa_indigest[4]
1903b7e3f244SSam Leffler 	);
1904b7e3f244SSam Leffler 	printf("sa: outdigest %x %x %x %x %x\n"
1905b7e3f244SSam Leffler 		, re->re_sa.sa_outdigest[0]
1906b7e3f244SSam Leffler 		, re->re_sa.sa_outdigest[1]
1907b7e3f244SSam Leffler 		, re->re_sa.sa_outdigest[2]
1908b7e3f244SSam Leffler 		, re->re_sa.sa_outdigest[3]
1909b7e3f244SSam Leffler 		, re->re_sa.sa_outdigest[4]
1910b7e3f244SSam Leffler 	);
1911b7e3f244SSam Leffler 	printf("sr: iv %x %x %x %x\n"
1912b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_iv[0]
1913b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_iv[1]
1914b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_iv[2]
1915b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_iv[3]
1916b7e3f244SSam Leffler 	);
1917b7e3f244SSam Leffler 	printf("sr: hashbc %u indigest %x %x %x %x %x\n"
1918b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_hashbc
1919b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_indigest[0]
1920b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_indigest[1]
1921b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_indigest[2]
1922b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_indigest[3]
1923b7e3f244SSam Leffler 		, re->re_sastate.sa_saved_indigest[4]
1924b7e3f244SSam Leffler 	);
1925b7e3f244SSam Leffler }
1926b7e3f244SSam Leffler 
1927b7e3f244SSam Leffler static void
safe_dump_ring(struct safe_softc * sc,const char * tag)1928b7e3f244SSam Leffler safe_dump_ring(struct safe_softc *sc, const char *tag)
1929b7e3f244SSam Leffler {
1930b7e3f244SSam Leffler 	mtx_lock(&sc->sc_ringmtx);
1931b7e3f244SSam Leffler 	printf("\nSafeNet Ring State:\n");
1932b7e3f244SSam Leffler 	safe_dump_intrstate(sc, tag);
1933b7e3f244SSam Leffler 	safe_dump_dmastatus(sc, tag);
1934b7e3f244SSam Leffler 	safe_dump_ringstate(sc, tag);
1935b7e3f244SSam Leffler 	if (sc->sc_nqchip) {
1936b7e3f244SSam Leffler 		struct safe_ringentry *re = sc->sc_back;
1937b7e3f244SSam Leffler 		do {
1938b7e3f244SSam Leffler 			safe_dump_request(sc, tag, re);
1939b7e3f244SSam Leffler 			if (++re == sc->sc_ringtop)
1940b7e3f244SSam Leffler 				re = sc->sc_ring;
1941b7e3f244SSam Leffler 		} while (re != sc->sc_front);
1942b7e3f244SSam Leffler 	}
1943b7e3f244SSam Leffler 	mtx_unlock(&sc->sc_ringmtx);
1944b7e3f244SSam Leffler }
1945b7e3f244SSam Leffler 
1946b7e3f244SSam Leffler static int
sysctl_hw_safe_dump(SYSCTL_HANDLER_ARGS)1947b7e3f244SSam Leffler sysctl_hw_safe_dump(SYSCTL_HANDLER_ARGS)
1948b7e3f244SSam Leffler {
1949b7e3f244SSam Leffler 	char dmode[64];
1950b7e3f244SSam Leffler 	int error;
1951b7e3f244SSam Leffler 
1952b7e3f244SSam Leffler 	strncpy(dmode, "", sizeof(dmode) - 1);
1953b7e3f244SSam Leffler 	dmode[sizeof(dmode) - 1] = '\0';
1954b7e3f244SSam Leffler 	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
1955b7e3f244SSam Leffler 
1956b7e3f244SSam Leffler 	if (error == 0 && req->newptr != NULL) {
1957b7e3f244SSam Leffler 		struct safe_softc *sc = safec;
1958b7e3f244SSam Leffler 
1959b7e3f244SSam Leffler 		if (!sc)
1960b7e3f244SSam Leffler 			return EINVAL;
1961b7e3f244SSam Leffler 		if (strncmp(dmode, "dma", 3) == 0)
1962b7e3f244SSam Leffler 			safe_dump_dmastatus(sc, "safe0");
1963b7e3f244SSam Leffler 		else if (strncmp(dmode, "int", 3) == 0)
1964b7e3f244SSam Leffler 			safe_dump_intrstate(sc, "safe0");
1965b7e3f244SSam Leffler 		else if (strncmp(dmode, "ring", 4) == 0)
1966b7e3f244SSam Leffler 			safe_dump_ring(sc, "safe0");
1967b7e3f244SSam Leffler 		else
1968b7e3f244SSam Leffler 			return EINVAL;
1969b7e3f244SSam Leffler 	}
1970b7e3f244SSam Leffler 	return error;
1971b7e3f244SSam Leffler }
19727029da5cSPawel Biernacki SYSCTL_PROC(_hw_safe, OID_AUTO, dump,
19737029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0,
19747029da5cSPawel Biernacki     sysctl_hw_safe_dump, "A",
19757029da5cSPawel Biernacki     "Dump driver state");
1976b7e3f244SSam Leffler #endif /* SAFE_DEBUG */
1977