1139825Simp/*-
2167755Ssam * Copyright (c) 2002-2006 Sam Leffler.  All rights reserved.
3167755Ssam *
4167755Ssam * Redistribution and use in source and binary forms, with or without
5167755Ssam * modification, are permitted provided that the following conditions
6167755Ssam * are met:
7167755Ssam * 1. Redistributions of source code must retain the above copyright
8167755Ssam *    notice, this list of conditions and the following disclaimer.
9167755Ssam * 2. Redistributions in binary form must reproduce the above copyright
10167755Ssam *    notice, this list of conditions and the following disclaimer in the
11167755Ssam *    documentation and/or other materials provided with the distribution.
12167755Ssam *
13167755Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14167755Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15167755Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16167755Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17167755Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18167755Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19167755Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20167755Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21167755Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22167755Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23167755Ssam */
24167755Ssam
25167755Ssam#include <sys/cdefs.h>
26167755Ssam__FBSDID("$FreeBSD$");
27167755Ssam
28167755Ssam/*
29167755Ssam * Cryptographic Subsystem.
30167755Ssam *
31167755Ssam * This code is derived from the Openbsd Cryptographic Framework (OCF)
32167755Ssam * that has the copyright shown below.  Very little of the original
33167755Ssam * code remains.
34167755Ssam */
35167755Ssam
36167755Ssam/*-
37104476Ssam * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
38104476Ssam *
39104476Ssam * This code was written by Angelos D. Keromytis in Athens, Greece, in
40104476Ssam * February 2000. Network Security Technologies Inc. (NSTI) kindly
41104476Ssam * supported the development of this code.
42104476Ssam *
43104476Ssam * Copyright (c) 2000, 2001 Angelos D. Keromytis
44104476Ssam *
45104476Ssam * Permission to use, copy, and modify this software with or without fee
46104476Ssam * is hereby granted, provided that this entire notice is included in
47104476Ssam * all source code copies of any software which is or includes a copy or
48104476Ssam * modification of this software.
49104476Ssam *
50104476Ssam * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
51104476Ssam * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
52104476Ssam * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
53104476Ssam * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
54104476Ssam * PURPOSE.
55104476Ssam */
56116191Sobrien
57108587Ssam#define	CRYPTO_TIMING				/* enable timing support */
58104476Ssam
59167755Ssam#include "opt_ddb.h"
60199884Sbz#include "opt_kdtrace.h"
61167755Ssam
62104476Ssam#include <sys/param.h>
63104476Ssam#include <sys/systm.h>
64104476Ssam#include <sys/eventhandler.h>
65104476Ssam#include <sys/kernel.h>
66104476Ssam#include <sys/kthread.h>
67104476Ssam#include <sys/lock.h>
68129880Sphk#include <sys/module.h>
69104476Ssam#include <sys/mutex.h>
70104476Ssam#include <sys/malloc.h>
71104476Ssam#include <sys/proc.h>
72199884Sbz#include <sys/sdt.h>
73104476Ssam#include <sys/sysctl.h>
74104476Ssam
75167755Ssam#include <ddb/ddb.h>
76167755Ssam
77104476Ssam#include <vm/uma.h>
78104476Ssam#include <opencrypto/cryptodev.h>
79104628Ssam#include <opencrypto/xform.h>			/* XXX for M_XDATA */
80104476Ssam
81167755Ssam#include <sys/kobj.h>
82167755Ssam#include <sys/bus.h>
83167755Ssam#include "cryptodev_if.h"
84167755Ssam
85208834Skib#if defined(__i386__) || defined(__amd64__)
86208834Skib#include <machine/pcb.h>
87208834Skib#endif
88208834Skib
89199884SbzSDT_PROVIDER_DEFINE(opencrypto);
90199884Sbz
91104476Ssam/*
92104476Ssam * Crypto drivers register themselves by allocating a slot in the
93104476Ssam * crypto_drivers table with crypto_get_driverid() and then registering
94104476Ssam * each algorithm they support with crypto_register() and crypto_kregister().
95104476Ssam */
96104476Ssamstatic	struct mtx crypto_drivers_mtx;		/* lock on driver table */
97104476Ssam#define	CRYPTO_DRIVER_LOCK()	mtx_lock(&crypto_drivers_mtx)
98104476Ssam#define	CRYPTO_DRIVER_UNLOCK()	mtx_unlock(&crypto_drivers_mtx)
99167755Ssam#define	CRYPTO_DRIVER_ASSERT()	mtx_assert(&crypto_drivers_mtx, MA_OWNED)
100167755Ssam
101167755Ssam/*
102167755Ssam * Crypto device/driver capabilities structure.
103167755Ssam *
104167755Ssam * Synchronization:
105167755Ssam * (d) - protected by CRYPTO_DRIVER_LOCK()
106167755Ssam * (q) - protected by CRYPTO_Q_LOCK()
107167755Ssam * Not tagged fields are read-only.
108167755Ssam */
109167755Ssamstruct cryptocap {
110167755Ssam	device_t	cc_dev;			/* (d) device/driver */
111167755Ssam	u_int32_t	cc_sessions;		/* (d) # of sessions */
112167755Ssam	u_int32_t	cc_koperations;		/* (d) # os asym operations */
113167755Ssam	/*
114167755Ssam	 * Largest possible operator length (in bits) for each type of
115167755Ssam	 * encryption algorithm. XXX not used
116167755Ssam	 */
117167755Ssam	u_int16_t	cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
118167755Ssam	u_int8_t	cc_alg[CRYPTO_ALGORITHM_MAX + 1];
119167755Ssam	u_int8_t	cc_kalg[CRK_ALGORITHM_MAX + 1];
120167755Ssam
121167755Ssam	int		cc_flags;		/* (d) flags */
122167755Ssam#define CRYPTOCAP_F_CLEANUP	0x80000000	/* needs resource cleanup */
123167755Ssam	int		cc_qblocked;		/* (q) symmetric q blocked */
124167755Ssam	int		cc_kqblocked;		/* (q) asymmetric q blocked */
125167755Ssam};
126104476Ssamstatic	struct cryptocap *crypto_drivers = NULL;
127104476Ssamstatic	int crypto_drivers_num = 0;
128104476Ssam
129104476Ssam/*
130104476Ssam * There are two queues for crypto requests; one for symmetric (e.g.
131104476Ssam * cipher) operations and one for asymmetric (e.g. MOD)operations.
132104476Ssam * A single mutex is used to lock access to both queues.  We could
133104476Ssam * have one per-queue but having one simplifies handling of block/unblock
134104476Ssam * operations.
135104476Ssam */
136158827Spjdstatic	int crp_sleep = 0;
137104476Ssamstatic	TAILQ_HEAD(,cryptop) crp_q;		/* request queues */
138104476Ssamstatic	TAILQ_HEAD(,cryptkop) crp_kq;
139104476Ssamstatic	struct mtx crypto_q_mtx;
140104476Ssam#define	CRYPTO_Q_LOCK()		mtx_lock(&crypto_q_mtx)
141104476Ssam#define	CRYPTO_Q_UNLOCK()	mtx_unlock(&crypto_q_mtx)
142104476Ssam
143104476Ssam/*
144104476Ssam * There are two queues for processing completed crypto requests; one
145104476Ssam * for the symmetric and one for the asymmetric ops.  We only need one
146104476Ssam * but have two to avoid type futzing (cryptop vs. cryptkop).  A single
147104476Ssam * mutex is used to lock access to both queues.  Note that this lock
148104476Ssam * must be separate from the lock on request queues to insure driver
149104476Ssam * callbacks don't generate lock order reversals.
150104476Ssam */
151104476Ssamstatic	TAILQ_HEAD(,cryptop) crp_ret_q;		/* callback queues */
152104476Ssamstatic	TAILQ_HEAD(,cryptkop) crp_ret_kq;
153104476Ssamstatic	struct mtx crypto_ret_q_mtx;
154104476Ssam#define	CRYPTO_RETQ_LOCK()	mtx_lock(&crypto_ret_q_mtx)
155104476Ssam#define	CRYPTO_RETQ_UNLOCK()	mtx_unlock(&crypto_ret_q_mtx)
156158826Spjd#define	CRYPTO_RETQ_EMPTY()	(TAILQ_EMPTY(&crp_ret_q) && TAILQ_EMPTY(&crp_ret_kq))
157104476Ssam
158104476Ssamstatic	uma_zone_t cryptop_zone;
159104476Ssamstatic	uma_zone_t cryptodesc_zone;
160104476Ssam
161104476Ssamint	crypto_userasymcrypto = 1;	/* userland may do asym crypto reqs */
162104476SsamSYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
163104476Ssam	   &crypto_userasymcrypto, 0,
164104476Ssam	   "Enable/disable user-mode access to asymmetric crypto support");
165104476Ssamint	crypto_devallowsoft = 0;	/* only use hardware crypto for asym */
166104476SsamSYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
167104476Ssam	   &crypto_devallowsoft, 0,
168104476Ssam	   "Enable/disable use of software asym crypto support");
169104476Ssam
170104476SsamMALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
171104476Ssam
172108588Ssamstatic	void crypto_proc(void);
173108588Ssamstatic	struct proc *cryptoproc;
174108588Ssamstatic	void crypto_ret_proc(void);
175108588Ssamstatic	struct proc *cryptoretproc;
176108588Ssamstatic	void crypto_destroy(void);
177158702Spjdstatic	int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
178167755Ssamstatic	int crypto_kinvoke(struct cryptkop *krp, int flags);
179108588Ssam
180108587Ssamstatic	struct cryptostats cryptostats;
181108587SsamSYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
182108587Ssam	    cryptostats, "Crypto system statistics");
183108587Ssam
184108587Ssam#ifdef CRYPTO_TIMING
185108587Ssamstatic	int crypto_timing = 0;
186108587SsamSYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
187108587Ssam	   &crypto_timing, 0, "Enable/disable crypto timing support");
188108587Ssam#endif
189108587Ssam
190108588Ssamstatic int
191104476Ssamcrypto_init(void)
192104476Ssam{
193108588Ssam	int error;
194108588Ssam
195115746Ssam	mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table",
196115746Ssam		MTX_DEF|MTX_QUIET);
197108588Ssam
198108588Ssam	TAILQ_INIT(&crp_q);
199108588Ssam	TAILQ_INIT(&crp_kq);
200115746Ssam	mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF);
201108588Ssam
202108588Ssam	TAILQ_INIT(&crp_ret_q);
203108588Ssam	TAILQ_INIT(&crp_ret_kq);
204115746Ssam	mtx_init(&crypto_ret_q_mtx, "crypto", "crypto return queues", MTX_DEF);
205108588Ssam
206104476Ssam	cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop),
207104476Ssam				    0, 0, 0, 0,
208104476Ssam				    UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
209104476Ssam	cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc),
210104476Ssam				    0, 0, 0, 0,
211104476Ssam				    UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
212108588Ssam	if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
213108588Ssam		printf("crypto_init: cannot setup crypto zones\n");
214108588Ssam		error = ENOMEM;
215108588Ssam		goto bad;
216108588Ssam	}
217104476Ssam
218104476Ssam	crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
219104476Ssam	crypto_drivers = malloc(crypto_drivers_num *
220104476Ssam	    sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
221108588Ssam	if (crypto_drivers == NULL) {
222108588Ssam		printf("crypto_init: cannot setup crypto drivers\n");
223108588Ssam		error = ENOMEM;
224108588Ssam		goto bad;
225108588Ssam	}
226104476Ssam
227172836Sjulian	error = kproc_create((void (*)(void *)) crypto_proc, NULL,
228108588Ssam		    &cryptoproc, 0, 0, "crypto");
229108588Ssam	if (error) {
230108588Ssam		printf("crypto_init: cannot start crypto thread; error %d",
231108588Ssam			error);
232108588Ssam		goto bad;
233108588Ssam	}
234104476Ssam
235172836Sjulian	error = kproc_create((void (*)(void *)) crypto_ret_proc, NULL,
236108588Ssam		    &cryptoretproc, 0, 0, "crypto returns");
237108588Ssam	if (error) {
238108588Ssam		printf("crypto_init: cannot start cryptoret thread; error %d",
239108588Ssam			error);
240108588Ssam		goto bad;
241108588Ssam	}
242108588Ssam	return 0;
243108588Ssambad:
244108588Ssam	crypto_destroy();
245108588Ssam	return error;
246104476Ssam}
247104476Ssam
248104476Ssam/*
249108588Ssam * Signal a crypto thread to terminate.  We use the driver
250108588Ssam * table lock to synchronize the sleep/wakeups so that we
251108588Ssam * are sure the threads have terminated before we release
252108588Ssam * the data structures they use.  See crypto_finis below
253108588Ssam * for the other half of this song-and-dance.
254108588Ssam */
255108588Ssamstatic void
256108588Ssamcrypto_terminate(struct proc **pp, void *q)
257108588Ssam{
258108588Ssam	struct proc *p;
259108588Ssam
260108588Ssam	mtx_assert(&crypto_drivers_mtx, MA_OWNED);
261108588Ssam	p = *pp;
262108588Ssam	*pp = NULL;
263108588Ssam	if (p) {
264108588Ssam		wakeup_one(q);
265108588Ssam		PROC_LOCK(p);		/* NB: insure we don't miss wakeup */
266108588Ssam		CRYPTO_DRIVER_UNLOCK();	/* let crypto_finis progress */
267108588Ssam		msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0);
268108588Ssam		PROC_UNLOCK(p);
269108588Ssam		CRYPTO_DRIVER_LOCK();
270108588Ssam	}
271108588Ssam}
272108588Ssam
273108588Ssamstatic void
274108588Ssamcrypto_destroy(void)
275108588Ssam{
276108588Ssam	/*
277108588Ssam	 * Terminate any crypto threads.
278108588Ssam	 */
279108588Ssam	CRYPTO_DRIVER_LOCK();
280108588Ssam	crypto_terminate(&cryptoproc, &crp_q);
281108588Ssam	crypto_terminate(&cryptoretproc, &crp_ret_q);
282108588Ssam	CRYPTO_DRIVER_UNLOCK();
283108588Ssam
284108588Ssam	/* XXX flush queues??? */
285108588Ssam
286108588Ssam	/*
287108588Ssam	 * Reclaim dynamically allocated resources.
288108588Ssam	 */
289108588Ssam	if (crypto_drivers != NULL)
290108588Ssam		free(crypto_drivers, M_CRYPTO_DATA);
291108588Ssam
292108588Ssam	if (cryptodesc_zone != NULL)
293108588Ssam		uma_zdestroy(cryptodesc_zone);
294108588Ssam	if (cryptop_zone != NULL)
295108588Ssam		uma_zdestroy(cryptop_zone);
296108588Ssam	mtx_destroy(&crypto_q_mtx);
297108588Ssam	mtx_destroy(&crypto_ret_q_mtx);
298108588Ssam	mtx_destroy(&crypto_drivers_mtx);
299108588Ssam}
300108588Ssam
301167755Ssamstatic struct cryptocap *
302167755Ssamcrypto_checkdriver(u_int32_t hid)
303167755Ssam{
304167755Ssam	if (crypto_drivers == NULL)
305167755Ssam		return NULL;
306167755Ssam	return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
307167755Ssam}
308167755Ssam
309108588Ssam/*
310167755Ssam * Compare a driver's list of supported algorithms against another
311167755Ssam * list; return non-zero if all algorithms are supported.
312105251Smarkm */
313105251Smarkmstatic int
314167755Ssamdriver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
315105251Smarkm{
316167755Ssam	const struct cryptoini *cr;
317108588Ssam
318167755Ssam	/* See if all the algorithms are supported. */
319167755Ssam	for (cr = cri; cr; cr = cr->cri_next)
320167755Ssam		if (cap->cc_alg[cr->cri_alg] == 0)
321167755Ssam			return 0;
322167755Ssam	return 1;
323105251Smarkm}
324105251Smarkm
325105251Smarkm/*
326167755Ssam * Select a driver for a new session that supports the specified
327167755Ssam * algorithms and, optionally, is constrained according to the flags.
328167755Ssam * The algorithm we use here is pretty stupid; just use the
329167755Ssam * first driver that supports all the algorithms we need. If there
330167755Ssam * are multiple drivers we choose the driver with the fewest active
331167755Ssam * sessions.  We prefer hardware-backed drivers to software ones.
332167755Ssam *
333167755Ssam * XXX We need more smarts here (in real life too, but that's
334167755Ssam * XXX another story altogether).
335104476Ssam */
336167755Ssamstatic struct cryptocap *
337167755Ssamcrypto_select_driver(const struct cryptoini *cri, int flags)
338104476Ssam{
339167755Ssam	struct cryptocap *cap, *best;
340167755Ssam	int match, hid;
341104476Ssam
342167755Ssam	CRYPTO_DRIVER_ASSERT();
343104476Ssam
344104476Ssam	/*
345167755Ssam	 * Look first for hardware crypto devices if permitted.
346104476Ssam	 */
347167755Ssam	if (flags & CRYPTOCAP_F_HARDWARE)
348167755Ssam		match = CRYPTOCAP_F_HARDWARE;
349167755Ssam	else
350167755Ssam		match = CRYPTOCAP_F_SOFTWARE;
351167755Ssam	best = NULL;
352167755Ssamagain:
353167755Ssam	for (hid = 0; hid < crypto_drivers_num; hid++) {
354167755Ssam		cap = &crypto_drivers[hid];
355167755Ssam		/*
356167755Ssam		 * If it's not initialized, is in the process of
357167755Ssam		 * going away, or is not appropriate (hardware
358167755Ssam		 * or software based on match), then skip.
359167755Ssam		 */
360167755Ssam		if (cap->cc_dev == NULL ||
361167755Ssam		    (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
362167755Ssam		    (cap->cc_flags & match) == 0)
363167755Ssam			continue;
364104476Ssam
365167755Ssam		/* verify all the algorithms are supported. */
366167755Ssam		if (driver_suitable(cap, cri)) {
367167755Ssam			if (best == NULL ||
368167755Ssam			    cap->cc_sessions < best->cc_sessions)
369167755Ssam				best = cap;
370159240Spjd		}
371159240Spjd	}
372167755Ssam	if (best != NULL)
373167755Ssam		return best;
374167755Ssam	if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
375167755Ssam		/* sort of an Algol 68-style for loop */
376167755Ssam		match = CRYPTOCAP_F_SOFTWARE;
377167755Ssam		goto again;
378167755Ssam	}
379167755Ssam	return best;
380167755Ssam}
381104476Ssam
382167755Ssam/*
383167755Ssam * Create a new session.  The crid argument specifies a crypto
384167755Ssam * driver to use or constraints on a driver to select (hardware
385167755Ssam * only, software only, either).  Whatever driver is selected
386167755Ssam * must be capable of the requested crypto algorithms.
387167755Ssam */
388167755Ssamint
389167755Ssamcrypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
390167755Ssam{
391167755Ssam	struct cryptocap *cap;
392167755Ssam	u_int32_t hid, lid;
393167755Ssam	int err;
394159240Spjd
395167755Ssam	CRYPTO_DRIVER_LOCK();
396167755Ssam	if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
397167755Ssam		/*
398167755Ssam		 * Use specified driver; verify it is capable.
399167755Ssam		 */
400167755Ssam		cap = crypto_checkdriver(crid);
401167755Ssam		if (cap != NULL && !driver_suitable(cap, cri))
402159240Spjd			cap = NULL;
403167755Ssam	} else {
404167755Ssam		/*
405167755Ssam		 * No requested driver; select based on crid flags.
406167755Ssam		 */
407167755Ssam		cap = crypto_select_driver(cri, crid);
408167755Ssam		/*
409167755Ssam		 * if NULL then can't do everything in one session.
410167755Ssam		 * XXX Fix this. We need to inject a "virtual" session
411167755Ssam		 * XXX layer right about here.
412167755Ssam		 */
413104476Ssam	}
414159240Spjd	if (cap != NULL) {
415159240Spjd		/* Call the driver initialization routine. */
416167755Ssam		hid = cap - crypto_drivers;
417159240Spjd		lid = hid;		/* Pass the driver ID. */
418167755Ssam		err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
419159240Spjd		if (err == 0) {
420167755Ssam			(*sid) = (cap->cc_flags & 0xff000000)
421167755Ssam			       | (hid & 0x00ffffff);
422159240Spjd			(*sid) <<= 32;
423159240Spjd			(*sid) |= (lid & 0xffffffff);
424159240Spjd			cap->cc_sessions++;
425159240Spjd		}
426167755Ssam	} else
427167755Ssam		err = EINVAL;
428104476Ssam	CRYPTO_DRIVER_UNLOCK();
429104476Ssam	return err;
430104476Ssam}
431104476Ssam
432158702Spjdstatic void
433158702Spjdcrypto_remove(struct cryptocap *cap)
434158702Spjd{
435158702Spjd
436158702Spjd	mtx_assert(&crypto_drivers_mtx, MA_OWNED);
437158702Spjd	if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
438158702Spjd		bzero(cap, sizeof(*cap));
439158702Spjd}
440158702Spjd
441104476Ssam/*
442104476Ssam * Delete an existing session (or a reserved session on an unregistered
443104476Ssam * driver).
444104476Ssam */
445104476Ssamint
446104476Ssamcrypto_freesession(u_int64_t sid)
447104476Ssam{
448158702Spjd	struct cryptocap *cap;
449104476Ssam	u_int32_t hid;
450104476Ssam	int err;
451104476Ssam
452104476Ssam	CRYPTO_DRIVER_LOCK();
453104476Ssam
454104476Ssam	if (crypto_drivers == NULL) {
455104476Ssam		err = EINVAL;
456104476Ssam		goto done;
457104476Ssam	}
458104476Ssam
459104476Ssam	/* Determine two IDs. */
460116924Ssam	hid = CRYPTO_SESID2HID(sid);
461104476Ssam
462104476Ssam	if (hid >= crypto_drivers_num) {
463104476Ssam		err = ENOENT;
464104476Ssam		goto done;
465104476Ssam	}
466158702Spjd	cap = &crypto_drivers[hid];
467104476Ssam
468158702Spjd	if (cap->cc_sessions)
469158702Spjd		cap->cc_sessions--;
470104476Ssam
471104476Ssam	/* Call the driver cleanup routine, if available. */
472167755Ssam	err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
473104476Ssam
474158702Spjd	if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
475158702Spjd		crypto_remove(cap);
476104476Ssam
477104476Ssamdone:
478104476Ssam	CRYPTO_DRIVER_UNLOCK();
479104476Ssam	return err;
480104476Ssam}
481104476Ssam
482104476Ssam/*
483104476Ssam * Return an unused driver id.  Used by drivers prior to registering
484104476Ssam * support for the algorithms they handle.
485104476Ssam */
486104476Ssamint32_t
487167755Ssamcrypto_get_driverid(device_t dev, int flags)
488104476Ssam{
489104476Ssam	struct cryptocap *newdrv;
490104476Ssam	int i;
491104476Ssam
492167755Ssam	if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
493167755Ssam		printf("%s: no flags specified when registering driver\n",
494167755Ssam		    device_get_nameunit(dev));
495167755Ssam		return -1;
496167755Ssam	}
497167755Ssam
498104476Ssam	CRYPTO_DRIVER_LOCK();
499104476Ssam
500158702Spjd	for (i = 0; i < crypto_drivers_num; i++) {
501167755Ssam		if (crypto_drivers[i].cc_dev == NULL &&
502158702Spjd		    (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
503104476Ssam			break;
504158702Spjd		}
505158702Spjd	}
506104476Ssam
507104476Ssam	/* Out of entries, allocate some more. */
508104476Ssam	if (i == crypto_drivers_num) {
509104476Ssam		/* Be careful about wrap-around. */
510104476Ssam		if (2 * crypto_drivers_num <= crypto_drivers_num) {
511104476Ssam			CRYPTO_DRIVER_UNLOCK();
512104476Ssam			printf("crypto: driver count wraparound!\n");
513104476Ssam			return -1;
514104476Ssam		}
515104476Ssam
516104476Ssam		newdrv = malloc(2 * crypto_drivers_num *
517104476Ssam		    sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
518104476Ssam		if (newdrv == NULL) {
519104476Ssam			CRYPTO_DRIVER_UNLOCK();
520104476Ssam			printf("crypto: no space to expand driver table!\n");
521104476Ssam			return -1;
522104476Ssam		}
523104476Ssam
524104476Ssam		bcopy(crypto_drivers, newdrv,
525104476Ssam		    crypto_drivers_num * sizeof(struct cryptocap));
526104476Ssam
527104476Ssam		crypto_drivers_num *= 2;
528104476Ssam
529104476Ssam		free(crypto_drivers, M_CRYPTO_DATA);
530104476Ssam		crypto_drivers = newdrv;
531104476Ssam	}
532104476Ssam
533104476Ssam	/* NB: state is zero'd on free */
534104476Ssam	crypto_drivers[i].cc_sessions = 1;	/* Mark */
535167755Ssam	crypto_drivers[i].cc_dev = dev;
536104476Ssam	crypto_drivers[i].cc_flags = flags;
537104476Ssam	if (bootverbose)
538167755Ssam		printf("crypto: assign %s driver id %u, flags %u\n",
539167755Ssam		    device_get_nameunit(dev), i, flags);
540104476Ssam
541104476Ssam	CRYPTO_DRIVER_UNLOCK();
542104476Ssam
543104476Ssam	return i;
544104476Ssam}
545104476Ssam
546167755Ssam/*
547167755Ssam * Lookup a driver by name.  We match against the full device
548167755Ssam * name and unit, and against just the name.  The latter gives
549167755Ssam * us a simple widlcarding by device name.  On success return the
550167755Ssam * driver/hardware identifier; otherwise return -1.
551167755Ssam */
552167755Ssamint
553167755Ssamcrypto_find_driver(const char *match)
554104476Ssam{
555167755Ssam	int i, len = strlen(match);
556167755Ssam
557167755Ssam	CRYPTO_DRIVER_LOCK();
558167755Ssam	for (i = 0; i < crypto_drivers_num; i++) {
559167755Ssam		device_t dev = crypto_drivers[i].cc_dev;
560167755Ssam		if (dev == NULL ||
561167755Ssam		    (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
562167755Ssam			continue;
563167755Ssam		if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
564167755Ssam		    strncmp(match, device_get_name(dev), len) == 0)
565167755Ssam			break;
566167755Ssam	}
567167755Ssam	CRYPTO_DRIVER_UNLOCK();
568167755Ssam	return i < crypto_drivers_num ? i : -1;
569104476Ssam}
570104476Ssam
571104476Ssam/*
572167755Ssam * Return the device_t for the specified driver or NULL
573167755Ssam * if the driver identifier is invalid.
574167755Ssam */
575167755Ssamdevice_t
576167755Ssamcrypto_find_device_byhid(int hid)
577167755Ssam{
578167755Ssam	struct cryptocap *cap = crypto_checkdriver(hid);
579167755Ssam	return cap != NULL ? cap->cc_dev : NULL;
580167755Ssam}
581167755Ssam
582167755Ssam/*
583167755Ssam * Return the device/driver capabilities.
584167755Ssam */
585167755Ssamint
586167755Ssamcrypto_getcaps(int hid)
587167755Ssam{
588167755Ssam	struct cryptocap *cap = crypto_checkdriver(hid);
589167755Ssam	return cap != NULL ? cap->cc_flags : 0;
590167755Ssam}
591167755Ssam
592167755Ssam/*
593104476Ssam * Register support for a key-related algorithm.  This routine
594104476Ssam * is called once for each algorithm supported a driver.
595104476Ssam */
596104476Ssamint
597167755Ssamcrypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
598104476Ssam{
599104476Ssam	struct cryptocap *cap;
600104476Ssam	int err;
601104476Ssam
602104476Ssam	CRYPTO_DRIVER_LOCK();
603104476Ssam
604104476Ssam	cap = crypto_checkdriver(driverid);
605104476Ssam	if (cap != NULL &&
606104476Ssam	    (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
607104476Ssam		/*
608104476Ssam		 * XXX Do some performance testing to determine placing.
609104476Ssam		 * XXX We probably need an auxiliary data structure that
610104476Ssam		 * XXX describes relative performances.
611104476Ssam		 */
612104476Ssam
613104476Ssam		cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
614104476Ssam		if (bootverbose)
615167755Ssam			printf("crypto: %s registers key alg %u flags %u\n"
616167755Ssam				, device_get_nameunit(cap->cc_dev)
617104476Ssam				, kalg
618104476Ssam				, flags
619104476Ssam			);
620104476Ssam		err = 0;
621104476Ssam	} else
622104476Ssam		err = EINVAL;
623104476Ssam
624104476Ssam	CRYPTO_DRIVER_UNLOCK();
625104476Ssam	return err;
626104476Ssam}
627104476Ssam
628104476Ssam/*
629104476Ssam * Register support for a non-key-related algorithm.  This routine
630104476Ssam * is called once for each such algorithm supported by a driver.
631104476Ssam */
632104476Ssamint
633104476Ssamcrypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
634167755Ssam    u_int32_t flags)
635104476Ssam{
636104476Ssam	struct cryptocap *cap;
637104476Ssam	int err;
638104476Ssam
639104476Ssam	CRYPTO_DRIVER_LOCK();
640104476Ssam
641104476Ssam	cap = crypto_checkdriver(driverid);
642104476Ssam	/* NB: algorithms are in the range [1..max] */
643104476Ssam	if (cap != NULL &&
644104476Ssam	    (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
645104476Ssam		/*
646104476Ssam		 * XXX Do some performance testing to determine placing.
647104476Ssam		 * XXX We probably need an auxiliary data structure that
648104476Ssam		 * XXX describes relative performances.
649104476Ssam		 */
650104476Ssam
651104476Ssam		cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
652104476Ssam		cap->cc_max_op_len[alg] = maxoplen;
653104476Ssam		if (bootverbose)
654167755Ssam			printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
655167755Ssam				, device_get_nameunit(cap->cc_dev)
656104476Ssam				, alg
657104476Ssam				, flags
658104476Ssam				, maxoplen
659104476Ssam			);
660167755Ssam		cap->cc_sessions = 0;		/* Unmark */
661104476Ssam		err = 0;
662104476Ssam	} else
663104476Ssam		err = EINVAL;
664104476Ssam
665104476Ssam	CRYPTO_DRIVER_UNLOCK();
666104476Ssam	return err;
667104476Ssam}
668104476Ssam
669167755Ssamstatic void
670167755Ssamdriver_finis(struct cryptocap *cap)
671167755Ssam{
672167755Ssam	u_int32_t ses, kops;
673167755Ssam
674167755Ssam	CRYPTO_DRIVER_ASSERT();
675167755Ssam
676167755Ssam	ses = cap->cc_sessions;
677167755Ssam	kops = cap->cc_koperations;
678167755Ssam	bzero(cap, sizeof(*cap));
679167755Ssam	if (ses != 0 || kops != 0) {
680167755Ssam		/*
681167755Ssam		 * If there are pending sessions,
682167755Ssam		 * just mark as invalid.
683167755Ssam		 */
684167755Ssam		cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
685167755Ssam		cap->cc_sessions = ses;
686167755Ssam		cap->cc_koperations = kops;
687167755Ssam	}
688167755Ssam}
689167755Ssam
690104476Ssam/*
691104476Ssam * Unregister a crypto driver. If there are pending sessions using it,
692104476Ssam * leave enough information around so that subsequent calls using those
693104476Ssam * sessions will correctly detect the driver has been unregistered and
694104476Ssam * reroute requests.
695104476Ssam */
696104476Ssamint
697104476Ssamcrypto_unregister(u_int32_t driverid, int alg)
698104476Ssam{
699158702Spjd	struct cryptocap *cap;
700104476Ssam	int i, err;
701104476Ssam
702104476Ssam	CRYPTO_DRIVER_LOCK();
703104476Ssam	cap = crypto_checkdriver(driverid);
704104476Ssam	if (cap != NULL &&
705104476Ssam	    (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
706104476Ssam	    cap->cc_alg[alg] != 0) {
707104476Ssam		cap->cc_alg[alg] = 0;
708104476Ssam		cap->cc_max_op_len[alg] = 0;
709104476Ssam
710104476Ssam		/* Was this the last algorithm ? */
711104476Ssam		for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
712104476Ssam			if (cap->cc_alg[i] != 0)
713104476Ssam				break;
714104476Ssam
715167755Ssam		if (i == CRYPTO_ALGORITHM_MAX + 1)
716167755Ssam			driver_finis(cap);
717104476Ssam		err = 0;
718104476Ssam	} else
719104476Ssam		err = EINVAL;
720167755Ssam	CRYPTO_DRIVER_UNLOCK();
721104476Ssam
722104476Ssam	return err;
723104476Ssam}
724104476Ssam
725104476Ssam/*
726104476Ssam * Unregister all algorithms associated with a crypto driver.
727104476Ssam * If there are pending sessions using it, leave enough information
728104476Ssam * around so that subsequent calls using those sessions will
729104476Ssam * correctly detect the driver has been unregistered and reroute
730104476Ssam * requests.
731104476Ssam */
732104476Ssamint
733104476Ssamcrypto_unregister_all(u_int32_t driverid)
734104476Ssam{
735158702Spjd	struct cryptocap *cap;
736167755Ssam	int err;
737104476Ssam
738104476Ssam	CRYPTO_DRIVER_LOCK();
739104476Ssam	cap = crypto_checkdriver(driverid);
740104476Ssam	if (cap != NULL) {
741167755Ssam		driver_finis(cap);
742104476Ssam		err = 0;
743104476Ssam	} else
744104476Ssam		err = EINVAL;
745167755Ssam	CRYPTO_DRIVER_UNLOCK();
746104476Ssam
747104476Ssam	return err;
748104476Ssam}
749104476Ssam
750104476Ssam/*
751104476Ssam * Clear blockage on a driver.  The what parameter indicates whether
752104476Ssam * the driver is now ready for cryptop's and/or cryptokop's.
753104476Ssam */
754104476Ssamint
755104476Ssamcrypto_unblock(u_int32_t driverid, int what)
756104476Ssam{
757104476Ssam	struct cryptocap *cap;
758158827Spjd	int err;
759104476Ssam
760104476Ssam	CRYPTO_Q_LOCK();
761104476Ssam	cap = crypto_checkdriver(driverid);
762104476Ssam	if (cap != NULL) {
763158827Spjd		if (what & CRYPTO_SYMQ)
764104476Ssam			cap->cc_qblocked = 0;
765158827Spjd		if (what & CRYPTO_ASYMQ)
766104476Ssam			cap->cc_kqblocked = 0;
767158827Spjd		if (crp_sleep)
768104628Ssam			wakeup_one(&crp_q);
769104476Ssam		err = 0;
770104476Ssam	} else
771104476Ssam		err = EINVAL;
772104476Ssam	CRYPTO_Q_UNLOCK();
773104476Ssam
774104476Ssam	return err;
775104476Ssam}
776104476Ssam
777104476Ssam/*
778104476Ssam * Add a crypto request to a queue, to be processed by the kernel thread.
779104476Ssam */
780104476Ssamint
781104476Ssamcrypto_dispatch(struct cryptop *crp)
782104476Ssam{
783158702Spjd	struct cryptocap *cap;
784158702Spjd	u_int32_t hid;
785158702Spjd	int result;
786104476Ssam
787108587Ssam	cryptostats.cs_ops++;
788108587Ssam
789108587Ssam#ifdef CRYPTO_TIMING
790108587Ssam	if (crypto_timing)
791108587Ssam		binuptime(&crp->crp_tstamp);
792108587Ssam#endif
793108587Ssam
794158702Spjd	hid = CRYPTO_SESID2HID(crp->crp_sid);
795158702Spjd
796111297Ssam	if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
797111297Ssam		/*
798111297Ssam		 * Caller marked the request to be processed
799111297Ssam		 * immediately; dispatch it directly to the
800111297Ssam		 * driver unless the driver is currently blocked.
801111297Ssam		 */
802111297Ssam		cap = crypto_checkdriver(hid);
803158702Spjd		/* Driver cannot disappeared when there is an active session. */
804158702Spjd		KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
805158702Spjd		if (!cap->cc_qblocked) {
806158702Spjd			result = crypto_invoke(cap, crp, 0);
807158702Spjd			if (result != ERESTART)
808158702Spjd				return (result);
809158823Spjd			/*
810158823Spjd			 * The driver ran out of resources, put the request on
811158823Spjd			 * the queue.
812158823Spjd			 */
813108990Ssam		}
814108990Ssam	}
815158702Spjd	CRYPTO_Q_LOCK();
816158827Spjd	TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
817158827Spjd	if (crp_sleep)
818157665Spjd		wakeup_one(&crp_q);
819115746Ssam	CRYPTO_Q_UNLOCK();
820158702Spjd	return 0;
821104476Ssam}
822104476Ssam
823104476Ssam/*
824104476Ssam * Add an asymetric crypto request to a queue,
825104476Ssam * to be processed by the kernel thread.
826104476Ssam */
827104476Ssamint
828104476Ssamcrypto_kdispatch(struct cryptkop *krp)
829104476Ssam{
830167755Ssam	int error;
831104476Ssam
832108587Ssam	cryptostats.cs_kops++;
833108587Ssam
834167755Ssam	error = crypto_kinvoke(krp, krp->krp_crid);
835167755Ssam	if (error == ERESTART) {
836167755Ssam		CRYPTO_Q_LOCK();
837167755Ssam		TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
838167755Ssam		if (crp_sleep)
839167755Ssam			wakeup_one(&crp_q);
840167755Ssam		CRYPTO_Q_UNLOCK();
841167755Ssam		error = 0;
842167755Ssam	}
843167755Ssam	return error;
844167755Ssam}
845104476Ssam
846167755Ssam/*
847167755Ssam * Verify a driver is suitable for the specified operation.
848167755Ssam */
849167755Ssamstatic __inline int
850167755Ssamkdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
851167755Ssam{
852167755Ssam	return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
853104476Ssam}
854104476Ssam
855104476Ssam/*
856167755Ssam * Select a driver for an asym operation.  The driver must
857167755Ssam * support the necessary algorithm.  The caller can constrain
858167755Ssam * which device is selected with the flags parameter.  The
859167755Ssam * algorithm we use here is pretty stupid; just use the first
860167755Ssam * driver that supports the algorithms we need. If there are
861167755Ssam * multiple suitable drivers we choose the driver with the
862167755Ssam * fewest active operations.  We prefer hardware-backed
863167755Ssam * drivers to software ones when either may be used.
864104476Ssam */
865167755Ssamstatic struct cryptocap *
866167755Ssamcrypto_select_kdriver(const struct cryptkop *krp, int flags)
867167755Ssam{
868167755Ssam	struct cryptocap *cap, *best, *blocked;
869167755Ssam	int match, hid;
870167755Ssam
871167755Ssam	CRYPTO_DRIVER_ASSERT();
872167755Ssam
873167755Ssam	/*
874167755Ssam	 * Look first for hardware crypto devices if permitted.
875167755Ssam	 */
876167755Ssam	if (flags & CRYPTOCAP_F_HARDWARE)
877167755Ssam		match = CRYPTOCAP_F_HARDWARE;
878167755Ssam	else
879167755Ssam		match = CRYPTOCAP_F_SOFTWARE;
880167755Ssam	best = NULL;
881167755Ssam	blocked = NULL;
882167755Ssamagain:
883167755Ssam	for (hid = 0; hid < crypto_drivers_num; hid++) {
884167755Ssam		cap = &crypto_drivers[hid];
885167755Ssam		/*
886167755Ssam		 * If it's not initialized, is in the process of
887167755Ssam		 * going away, or is not appropriate (hardware
888167755Ssam		 * or software based on match), then skip.
889167755Ssam		 */
890167755Ssam		if (cap->cc_dev == NULL ||
891167755Ssam		    (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
892167755Ssam		    (cap->cc_flags & match) == 0)
893167755Ssam			continue;
894167755Ssam
895167755Ssam		/* verify all the algorithms are supported. */
896167755Ssam		if (kdriver_suitable(cap, krp)) {
897167755Ssam			if (best == NULL ||
898167755Ssam			    cap->cc_koperations < best->cc_koperations)
899167755Ssam				best = cap;
900167755Ssam		}
901167755Ssam	}
902167755Ssam	if (best != NULL)
903167755Ssam		return best;
904167755Ssam	if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
905167755Ssam		/* sort of an Algol 68-style for loop */
906167755Ssam		match = CRYPTOCAP_F_SOFTWARE;
907167755Ssam		goto again;
908167755Ssam	}
909167755Ssam	return best;
910167755Ssam}
911167755Ssam
912167755Ssam/*
913167755Ssam * Dispatch an assymetric crypto request.
914167755Ssam */
915104476Ssamstatic int
916167755Ssamcrypto_kinvoke(struct cryptkop *krp, int crid)
917104476Ssam{
918158702Spjd	struct cryptocap *cap = NULL;
919167755Ssam	int error;
920104476Ssam
921158702Spjd	KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
922158702Spjd	KASSERT(krp->krp_callback != NULL,
923158702Spjd	    ("%s: krp->crp_callback == NULL", __func__));
924104476Ssam
925158702Spjd	CRYPTO_DRIVER_LOCK();
926167755Ssam	if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
927167755Ssam		cap = crypto_checkdriver(crid);
928167755Ssam		if (cap != NULL) {
929167755Ssam			/*
930167755Ssam			 * Driver present, it must support the necessary
931167755Ssam			 * algorithm and, if s/w drivers are excluded,
932167755Ssam			 * it must be registered as hardware-backed.
933167755Ssam			 */
934167755Ssam			if (!kdriver_suitable(cap, krp) ||
935167755Ssam			    (!crypto_devallowsoft &&
936167755Ssam			     (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
937167755Ssam				cap = NULL;
938158702Spjd		}
939167755Ssam	} else {
940167755Ssam		/*
941167755Ssam		 * No requested driver; select based on crid flags.
942167755Ssam		 */
943167755Ssam		if (!crypto_devallowsoft)	/* NB: disallow s/w drivers */
944167755Ssam			crid &= ~CRYPTOCAP_F_SOFTWARE;
945167755Ssam		cap = crypto_select_kdriver(krp, crid);
946104476Ssam	}
947167755Ssam	if (cap != NULL && !cap->cc_kqblocked) {
948167755Ssam		krp->krp_hid = cap - crypto_drivers;
949158702Spjd		cap->cc_koperations++;
950158702Spjd		CRYPTO_DRIVER_UNLOCK();
951167755Ssam		error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
952158702Spjd		CRYPTO_DRIVER_LOCK();
953158702Spjd		if (error == ERESTART) {
954158702Spjd			cap->cc_koperations--;
955158702Spjd			CRYPTO_DRIVER_UNLOCK();
956158702Spjd			return (error);
957158702Spjd		}
958158702Spjd	} else {
959167755Ssam		/*
960167755Ssam		 * NB: cap is !NULL if device is blocked; in
961167755Ssam		 *     that case return ERESTART so the operation
962167755Ssam		 *     is resubmitted if possible.
963167755Ssam		 */
964167755Ssam		error = (cap == NULL) ? ENODEV : ERESTART;
965158702Spjd	}
966158702Spjd	CRYPTO_DRIVER_UNLOCK();
967104476Ssam
968104476Ssam	if (error) {
969104476Ssam		krp->krp_status = error;
970104628Ssam		crypto_kdone(krp);
971104476Ssam	}
972104476Ssam	return 0;
973104476Ssam}
974104476Ssam
975108587Ssam#ifdef CRYPTO_TIMING
976108587Ssamstatic void
977108587Ssamcrypto_tstat(struct cryptotstat *ts, struct bintime *bt)
978108587Ssam{
979108587Ssam	struct bintime now, delta;
980108587Ssam	struct timespec t;
981108587Ssam	uint64_t u;
982108587Ssam
983108587Ssam	binuptime(&now);
984108587Ssam	u = now.frac;
985108587Ssam	delta.frac = now.frac - bt->frac;
986108587Ssam	delta.sec = now.sec - bt->sec;
987108587Ssam	if (u < delta.frac)
988108587Ssam		delta.sec--;
989108587Ssam	bintime2timespec(&delta, &t);
990108587Ssam	timespecadd(&ts->acc, &t);
991108587Ssam	if (timespeccmp(&t, &ts->min, <))
992108587Ssam		ts->min = t;
993108587Ssam	if (timespeccmp(&t, &ts->max, >))
994108587Ssam		ts->max = t;
995108587Ssam	ts->count++;
996108587Ssam
997108587Ssam	*bt = now;
998108587Ssam}
999108587Ssam#endif
1000108587Ssam
1001104476Ssam/*
1002104476Ssam * Dispatch a crypto request to the appropriate crypto devices.
1003104476Ssam */
1004104476Ssamstatic int
1005158702Spjdcrypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1006104476Ssam{
1007104476Ssam
1008158702Spjd	KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
1009158702Spjd	KASSERT(crp->crp_callback != NULL,
1010158702Spjd	    ("%s: crp->crp_callback == NULL", __func__));
1011158702Spjd	KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
1012158702Spjd
1013108587Ssam#ifdef CRYPTO_TIMING
1014108587Ssam	if (crypto_timing)
1015108587Ssam		crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1016108587Ssam#endif
1017158702Spjd	if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1018104476Ssam		struct cryptodesc *crd;
1019104476Ssam		u_int64_t nid;
1020104476Ssam
1021104476Ssam		/*
1022104476Ssam		 * Driver has unregistered; migrate the session and return
1023104476Ssam		 * an error to the caller so they'll resubmit the op.
1024158702Spjd		 *
1025158702Spjd		 * XXX: What if there are more already queued requests for this
1026158702Spjd		 *      session?
1027104476Ssam		 */
1028158702Spjd		crypto_freesession(crp->crp_sid);
1029158702Spjd
1030104476Ssam		for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1031104476Ssam			crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1032104476Ssam
1033167755Ssam		/* XXX propagate flags from initial session? */
1034167755Ssam		if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
1035167755Ssam		    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
1036104476Ssam			crp->crp_sid = nid;
1037104476Ssam
1038104476Ssam		crp->crp_etype = EAGAIN;
1039104628Ssam		crypto_done(crp);
1040104476Ssam		return 0;
1041104476Ssam	} else {
1042104476Ssam		/*
1043104476Ssam		 * Invoke the driver to process the request.
1044104476Ssam		 */
1045167755Ssam		return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1046104476Ssam	}
1047104476Ssam}
1048104476Ssam
1049104476Ssam/*
1050104476Ssam * Release a set of crypto descriptors.
1051104476Ssam */
1052104476Ssamvoid
1053104476Ssamcrypto_freereq(struct cryptop *crp)
1054104476Ssam{
1055104476Ssam	struct cryptodesc *crd;
1056104476Ssam
1057104476Ssam	if (crp == NULL)
1058104476Ssam		return;
1059104476Ssam
1060159346Spjd#ifdef DIAGNOSTIC
1061159346Spjd	{
1062159346Spjd		struct cryptop *crp2;
1063159346Spjd
1064159346Spjd		CRYPTO_Q_LOCK();
1065159346Spjd		TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1066159346Spjd			KASSERT(crp2 != crp,
1067159346Spjd			    ("Freeing cryptop from the crypto queue (%p).",
1068159346Spjd			    crp));
1069159346Spjd		}
1070159346Spjd		CRYPTO_Q_UNLOCK();
1071159346Spjd		CRYPTO_RETQ_LOCK();
1072159346Spjd		TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
1073159346Spjd			KASSERT(crp2 != crp,
1074159346Spjd			    ("Freeing cryptop from the return queue (%p).",
1075159346Spjd			    crp));
1076159346Spjd		}
1077159346Spjd		CRYPTO_RETQ_UNLOCK();
1078159346Spjd	}
1079159346Spjd#endif
1080159346Spjd
1081104476Ssam	while ((crd = crp->crp_desc) != NULL) {
1082104476Ssam		crp->crp_desc = crd->crd_next;
1083104476Ssam		uma_zfree(cryptodesc_zone, crd);
1084104476Ssam	}
1085104476Ssam	uma_zfree(cryptop_zone, crp);
1086104476Ssam}
1087104476Ssam
1088104476Ssam/*
1089104476Ssam * Acquire a set of crypto descriptors.
1090104476Ssam */
1091104476Ssamstruct cryptop *
1092104476Ssamcrypto_getreq(int num)
1093104476Ssam{
1094104476Ssam	struct cryptodesc *crd;
1095104476Ssam	struct cryptop *crp;
1096104476Ssam
1097108813Ssam	crp = uma_zalloc(cryptop_zone, M_NOWAIT|M_ZERO);
1098104476Ssam	if (crp != NULL) {
1099104476Ssam		while (num--) {
1100108813Ssam			crd = uma_zalloc(cryptodesc_zone, M_NOWAIT|M_ZERO);
1101104476Ssam			if (crd == NULL) {
1102104476Ssam				crypto_freereq(crp);
1103104476Ssam				return NULL;
1104104476Ssam			}
1105104476Ssam
1106104476Ssam			crd->crd_next = crp->crp_desc;
1107104476Ssam			crp->crp_desc = crd;
1108104476Ssam		}
1109104476Ssam	}
1110104476Ssam	return crp;
1111104476Ssam}
1112104476Ssam
1113104476Ssam/*
1114104476Ssam * Invoke the callback on behalf of the driver.
1115104476Ssam */
1116104476Ssamvoid
1117104476Ssamcrypto_done(struct cryptop *crp)
1118104476Ssam{
1119115746Ssam	KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
1120115746Ssam		("crypto_done: op already done, flags 0x%x", crp->crp_flags));
1121115746Ssam	crp->crp_flags |= CRYPTO_F_DONE;
1122108587Ssam	if (crp->crp_etype != 0)
1123108587Ssam		cryptostats.cs_errs++;
1124108587Ssam#ifdef CRYPTO_TIMING
1125108587Ssam	if (crypto_timing)
1126108587Ssam		crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
1127108587Ssam#endif
1128117058Ssam	/*
1129117058Ssam	 * CBIMM means unconditionally do the callback immediately;
1130117058Ssam	 * CBIFSYNC means do the callback immediately only if the
1131117058Ssam	 * operation was done synchronously.  Both are used to avoid
1132117058Ssam	 * doing extraneous context switches; the latter is mostly
1133117058Ssam	 * used with the software crypto driver.
1134117058Ssam	 */
1135117058Ssam	if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1136117058Ssam	    ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1137117058Ssam	     (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
1138111297Ssam		/*
1139111297Ssam		 * Do the callback directly.  This is ok when the
1140111297Ssam		 * callback routine does very little (e.g. the
1141111297Ssam		 * /dev/crypto callback method just does a wakeup).
1142111297Ssam		 */
1143111297Ssam#ifdef CRYPTO_TIMING
1144111297Ssam		if (crypto_timing) {
1145111297Ssam			/*
1146111297Ssam			 * NB: We must copy the timestamp before
1147111297Ssam			 * doing the callback as the cryptop is
1148111297Ssam			 * likely to be reclaimed.
1149111297Ssam			 */
1150111297Ssam			struct bintime t = crp->crp_tstamp;
1151111297Ssam			crypto_tstat(&cryptostats.cs_cb, &t);
1152111297Ssam			crp->crp_callback(crp);
1153111297Ssam			crypto_tstat(&cryptostats.cs_finis, &t);
1154111297Ssam		} else
1155111297Ssam#endif
1156111297Ssam			crp->crp_callback(crp);
1157111297Ssam	} else {
1158111297Ssam		/*
1159111297Ssam		 * Normal case; queue the callback for the thread.
1160111297Ssam		 */
1161111297Ssam		CRYPTO_RETQ_LOCK();
1162158826Spjd		if (CRYPTO_RETQ_EMPTY())
1163158702Spjd			wakeup_one(&crp_ret_q);	/* shared wait channel */
1164111297Ssam		TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1165111297Ssam		CRYPTO_RETQ_UNLOCK();
1166111297Ssam	}
1167104476Ssam}
1168104476Ssam
1169104476Ssam/*
1170104476Ssam * Invoke the callback on behalf of the driver.
1171104476Ssam */
1172104476Ssamvoid
1173104476Ssamcrypto_kdone(struct cryptkop *krp)
1174104476Ssam{
1175158702Spjd	struct cryptocap *cap;
1176104476Ssam
1177108587Ssam	if (krp->krp_status != 0)
1178108587Ssam		cryptostats.cs_kerrs++;
1179158702Spjd	CRYPTO_DRIVER_LOCK();
1180158702Spjd	/* XXX: What if driver is loaded in the meantime? */
1181158702Spjd	if (krp->krp_hid < crypto_drivers_num) {
1182158702Spjd		cap = &crypto_drivers[krp->krp_hid];
1183158702Spjd		cap->cc_koperations--;
1184158702Spjd		KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
1185158702Spjd		if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1186158702Spjd			crypto_remove(cap);
1187158702Spjd	}
1188158702Spjd	CRYPTO_DRIVER_UNLOCK();
1189104476Ssam	CRYPTO_RETQ_LOCK();
1190158826Spjd	if (CRYPTO_RETQ_EMPTY())
1191158702Spjd		wakeup_one(&crp_ret_q);		/* shared wait channel */
1192104476Ssam	TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1193104628Ssam	CRYPTO_RETQ_UNLOCK();
1194104476Ssam}
1195104476Ssam
1196104476Ssamint
1197104476Ssamcrypto_getfeat(int *featp)
1198104476Ssam{
1199104476Ssam	int hid, kalg, feat = 0;
1200104476Ssam
1201104476Ssam	CRYPTO_DRIVER_LOCK();
1202104476Ssam	for (hid = 0; hid < crypto_drivers_num; hid++) {
1203167755Ssam		const struct cryptocap *cap = &crypto_drivers[hid];
1204167755Ssam
1205167755Ssam		if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1206104476Ssam		    !crypto_devallowsoft) {
1207104476Ssam			continue;
1208104476Ssam		}
1209104476Ssam		for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1210167755Ssam			if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1211104476Ssam				feat |=  1 << kalg;
1212104476Ssam	}
1213104476Ssam	CRYPTO_DRIVER_UNLOCK();
1214104476Ssam	*featp = feat;
1215104476Ssam	return (0);
1216104476Ssam}
1217104476Ssam
1218108588Ssam/*
1219108588Ssam * Terminate a thread at module unload.  The process that
1220108588Ssam * initiated this is waiting for us to signal that we're gone;
1221108588Ssam * wake it up and exit.  We use the driver table lock to insure
1222108588Ssam * we don't do the wakeup before they're waiting.  There is no
1223108588Ssam * race here because the waiter sleeps on the proc lock for the
1224108588Ssam * thread so it gets notified at the right time because of an
1225108588Ssam * extra wakeup that's done in exit1().
1226108588Ssam */
1227104476Ssamstatic void
1228108588Ssamcrypto_finis(void *chan)
1229104476Ssam{
1230108588Ssam	CRYPTO_DRIVER_LOCK();
1231108588Ssam	wakeup_one(chan);
1232108588Ssam	CRYPTO_DRIVER_UNLOCK();
1233172836Sjulian	kproc_exit(0);
1234104476Ssam}
1235104476Ssam
1236104476Ssam/*
1237104628Ssam * Crypto thread, dispatches crypto requests.
1238104476Ssam */
1239104476Ssamstatic void
1240104476Ssamcrypto_proc(void)
1241104476Ssam{
1242104628Ssam	struct cryptop *crp, *submit;
1243104628Ssam	struct cryptkop *krp;
1244104476Ssam	struct cryptocap *cap;
1245158702Spjd	u_int32_t hid;
1246104476Ssam	int result, hint;
1247104476Ssam
1248208834Skib#if defined(__i386__) || defined(__amd64__)
1249208834Skib	fpu_kern_thread(FPU_KERN_NORMAL);
1250208834Skib#endif
1251208834Skib
1252104628Ssam	CRYPTO_Q_LOCK();
1253104476Ssam	for (;;) {
1254104476Ssam		/*
1255104476Ssam		 * Find the first element in the queue that can be
1256104476Ssam		 * processed and look-ahead to see if multiple ops
1257104476Ssam		 * are ready for the same driver.
1258104476Ssam		 */
1259104476Ssam		submit = NULL;
1260104476Ssam		hint = 0;
1261104476Ssam		TAILQ_FOREACH(crp, &crp_q, crp_next) {
1262158702Spjd			hid = CRYPTO_SESID2HID(crp->crp_sid);
1263104476Ssam			cap = crypto_checkdriver(hid);
1264158702Spjd			/*
1265158702Spjd			 * Driver cannot disappeared when there is an active
1266158702Spjd			 * session.
1267158702Spjd			 */
1268158716Spjd			KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1269158716Spjd			    __func__, __LINE__));
1270167755Ssam			if (cap == NULL || cap->cc_dev == NULL) {
1271104476Ssam				/* Op needs to be migrated, process it. */
1272104476Ssam				if (submit == NULL)
1273104476Ssam					submit = crp;
1274104476Ssam				break;
1275104476Ssam			}
1276104476Ssam			if (!cap->cc_qblocked) {
1277104476Ssam				if (submit != NULL) {
1278104476Ssam					/*
1279104476Ssam					 * We stop on finding another op,
1280104476Ssam					 * regardless whether its for the same
1281104476Ssam					 * driver or not.  We could keep
1282104476Ssam					 * searching the queue but it might be
1283104476Ssam					 * better to just use a per-driver
1284104476Ssam					 * queue instead.
1285104476Ssam					 */
1286116924Ssam					if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1287104476Ssam						hint = CRYPTO_HINT_MORE;
1288104476Ssam					break;
1289104476Ssam				} else {
1290104476Ssam					submit = crp;
1291111297Ssam					if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1292104476Ssam						break;
1293104476Ssam					/* keep scanning for more are q'd */
1294104476Ssam				}
1295104476Ssam			}
1296104476Ssam		}
1297104476Ssam		if (submit != NULL) {
1298104476Ssam			TAILQ_REMOVE(&crp_q, submit, crp_next);
1299158702Spjd			hid = CRYPTO_SESID2HID(submit->crp_sid);
1300158702Spjd			cap = crypto_checkdriver(hid);
1301158716Spjd			KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1302158716Spjd			    __func__, __LINE__));
1303158702Spjd			result = crypto_invoke(cap, submit, hint);
1304104476Ssam			if (result == ERESTART) {
1305104476Ssam				/*
1306104476Ssam				 * The driver ran out of resources, mark the
1307104476Ssam				 * driver ``blocked'' for cryptop's and put
1308104476Ssam				 * the request back in the queue.  It would
1309104476Ssam				 * best to put the request back where we got
1310104476Ssam				 * it but that's hard so for now we put it
1311104476Ssam				 * at the front.  This should be ok; putting
1312104476Ssam				 * it at the end does not work.
1313104476Ssam				 */
1314104476Ssam				/* XXX validate sid again? */
1315116924Ssam				crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1316104476Ssam				TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1317108587Ssam				cryptostats.cs_blocks++;
1318104476Ssam			}
1319104476Ssam		}
1320104476Ssam
1321104476Ssam		/* As above, but for key ops */
1322104476Ssam		TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1323104476Ssam			cap = crypto_checkdriver(krp->krp_hid);
1324167755Ssam			if (cap == NULL || cap->cc_dev == NULL) {
1325167755Ssam				/*
1326167755Ssam				 * Operation needs to be migrated, invalidate
1327167755Ssam				 * the assigned device so it will reselect a
1328167755Ssam				 * new one below.  Propagate the original
1329167755Ssam				 * crid selection flags if supplied.
1330167755Ssam				 */
1331167755Ssam				krp->krp_hid = krp->krp_crid &
1332167755Ssam				    (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1333167755Ssam				if (krp->krp_hid == 0)
1334167755Ssam					krp->krp_hid =
1335167755Ssam				    CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1336104476Ssam				break;
1337104476Ssam			}
1338104476Ssam			if (!cap->cc_kqblocked)
1339104476Ssam				break;
1340104476Ssam		}
1341104476Ssam		if (krp != NULL) {
1342104476Ssam			TAILQ_REMOVE(&crp_kq, krp, krp_next);
1343167755Ssam			result = crypto_kinvoke(krp, krp->krp_hid);
1344104476Ssam			if (result == ERESTART) {
1345104476Ssam				/*
1346104476Ssam				 * The driver ran out of resources, mark the
1347104476Ssam				 * driver ``blocked'' for cryptkop's and put
1348104476Ssam				 * the request back in the queue.  It would
1349104476Ssam				 * best to put the request back where we got
1350104476Ssam				 * it but that's hard so for now we put it
1351104476Ssam				 * at the front.  This should be ok; putting
1352104476Ssam				 * it at the end does not work.
1353104476Ssam				 */
1354104476Ssam				/* XXX validate sid again? */
1355104476Ssam				crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1356104476Ssam				TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1357108587Ssam				cryptostats.cs_kblocks++;
1358104476Ssam			}
1359104476Ssam		}
1360104476Ssam
1361104628Ssam		if (submit == NULL && krp == NULL) {
1362104476Ssam			/*
1363104476Ssam			 * Nothing more to be processed.  Sleep until we're
1364104476Ssam			 * woken because there are more ops to process.
1365104476Ssam			 * This happens either by submission or by a driver
1366104476Ssam			 * becoming unblocked and notifying us through
1367104476Ssam			 * crypto_unblock.  Note that when we wakeup we
1368104476Ssam			 * start processing each queue again from the
1369104476Ssam			 * front. It's not clear that it's important to
1370104476Ssam			 * preserve this ordering since ops may finish
1371104476Ssam			 * out of order if dispatched to different devices
1372104476Ssam			 * and some become blocked while others do not.
1373104476Ssam			 */
1374158827Spjd			crp_sleep = 1;
1375104628Ssam			msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0);
1376158827Spjd			crp_sleep = 0;
1377108588Ssam			if (cryptoproc == NULL)
1378108588Ssam				break;
1379108587Ssam			cryptostats.cs_intrs++;
1380104476Ssam		}
1381104476Ssam	}
1382108588Ssam	CRYPTO_Q_UNLOCK();
1383104628Ssam
1384108588Ssam	crypto_finis(&crp_q);
1385104628Ssam}
1386104628Ssam
1387104628Ssam/*
1388104628Ssam * Crypto returns thread, does callbacks for processed crypto requests.
1389104628Ssam * Callbacks are done here, rather than in the crypto drivers, because
1390104628Ssam * callbacks typically are expensive and would slow interrupt handling.
1391104628Ssam */
1392104628Ssamstatic void
1393104628Ssamcrypto_ret_proc(void)
1394104628Ssam{
1395104628Ssam	struct cryptop *crpt;
1396104628Ssam	struct cryptkop *krpt;
1397104628Ssam
1398104628Ssam	CRYPTO_RETQ_LOCK();
1399104628Ssam	for (;;) {
1400104628Ssam		/* Harvest return q's for completed ops */
1401104628Ssam		crpt = TAILQ_FIRST(&crp_ret_q);
1402104628Ssam		if (crpt != NULL)
1403104628Ssam			TAILQ_REMOVE(&crp_ret_q, crpt, crp_next);
1404104628Ssam
1405104628Ssam		krpt = TAILQ_FIRST(&crp_ret_kq);
1406104628Ssam		if (krpt != NULL)
1407104628Ssam			TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next);
1408104628Ssam
1409104628Ssam		if (crpt != NULL || krpt != NULL) {
1410104628Ssam			CRYPTO_RETQ_UNLOCK();
1411104628Ssam			/*
1412104628Ssam			 * Run callbacks unlocked.
1413104628Ssam			 */
1414108587Ssam			if (crpt != NULL) {
1415108587Ssam#ifdef CRYPTO_TIMING
1416108587Ssam				if (crypto_timing) {
1417108587Ssam					/*
1418108587Ssam					 * NB: We must copy the timestamp before
1419108587Ssam					 * doing the callback as the cryptop is
1420108587Ssam					 * likely to be reclaimed.
1421108587Ssam					 */
1422108587Ssam					struct bintime t = crpt->crp_tstamp;
1423108587Ssam					crypto_tstat(&cryptostats.cs_cb, &t);
1424108587Ssam					crpt->crp_callback(crpt);
1425108587Ssam					crypto_tstat(&cryptostats.cs_finis, &t);
1426108587Ssam				} else
1427108587Ssam#endif
1428108587Ssam					crpt->crp_callback(crpt);
1429108587Ssam			}
1430104628Ssam			if (krpt != NULL)
1431104628Ssam				krpt->krp_callback(krpt);
1432104628Ssam			CRYPTO_RETQ_LOCK();
1433104628Ssam		} else {
1434104628Ssam			/*
1435104628Ssam			 * Nothing more to be processed.  Sleep until we're
1436104628Ssam			 * woken because there are more returns to process.
1437104628Ssam			 */
1438104628Ssam			msleep(&crp_ret_q, &crypto_ret_q_mtx, PWAIT,
1439104628Ssam				"crypto_ret_wait", 0);
1440108588Ssam			if (cryptoretproc == NULL)
1441108588Ssam				break;
1442108587Ssam			cryptostats.cs_rets++;
1443104628Ssam		}
1444104628Ssam	}
1445108588Ssam	CRYPTO_RETQ_UNLOCK();
1446108588Ssam
1447108588Ssam	crypto_finis(&crp_ret_q);
1448104628Ssam}
1449167755Ssam
1450167755Ssam#ifdef DDB
1451167755Ssamstatic void
1452167755Ssamdb_show_drivers(void)
1453167755Ssam{
1454167755Ssam	int hid;
1455167755Ssam
1456167755Ssam	db_printf("%12s %4s %4s %8s %2s %2s\n"
1457167755Ssam		, "Device"
1458167755Ssam		, "Ses"
1459167755Ssam		, "Kops"
1460167755Ssam		, "Flags"
1461167755Ssam		, "QB"
1462167755Ssam		, "KB"
1463167755Ssam	);
1464167755Ssam	for (hid = 0; hid < crypto_drivers_num; hid++) {
1465167755Ssam		const struct cryptocap *cap = &crypto_drivers[hid];
1466167755Ssam		if (cap->cc_dev == NULL)
1467167755Ssam			continue;
1468167755Ssam		db_printf("%-12s %4u %4u %08x %2u %2u\n"
1469167755Ssam		    , device_get_nameunit(cap->cc_dev)
1470167755Ssam		    , cap->cc_sessions
1471167755Ssam		    , cap->cc_koperations
1472167755Ssam		    , cap->cc_flags
1473167755Ssam		    , cap->cc_qblocked
1474167755Ssam		    , cap->cc_kqblocked
1475167755Ssam		);
1476167755Ssam	}
1477167755Ssam}
1478167755Ssam
1479167755SsamDB_SHOW_COMMAND(crypto, db_show_crypto)
1480167755Ssam{
1481167755Ssam	struct cryptop *crp;
1482167755Ssam
1483167755Ssam	db_show_drivers();
1484167755Ssam	db_printf("\n");
1485167755Ssam
1486167755Ssam	db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1487167755Ssam	    "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1488167755Ssam	    "Desc", "Callback");
1489167755Ssam	TAILQ_FOREACH(crp, &crp_q, crp_next) {
1490167755Ssam		db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1491167755Ssam		    , (int) CRYPTO_SESID2HID(crp->crp_sid)
1492167755Ssam		    , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
1493167755Ssam		    , crp->crp_ilen, crp->crp_olen
1494167755Ssam		    , crp->crp_etype
1495167755Ssam		    , crp->crp_flags
1496167755Ssam		    , crp->crp_desc
1497167755Ssam		    , crp->crp_callback
1498167755Ssam		);
1499167755Ssam	}
1500167755Ssam	if (!TAILQ_EMPTY(&crp_ret_q)) {
1501167755Ssam		db_printf("\n%4s %4s %4s %8s\n",
1502167755Ssam		    "HID", "Etype", "Flags", "Callback");
1503167755Ssam		TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
1504167755Ssam			db_printf("%4u %4u %04x %8p\n"
1505167755Ssam			    , (int) CRYPTO_SESID2HID(crp->crp_sid)
1506167755Ssam			    , crp->crp_etype
1507167755Ssam			    , crp->crp_flags
1508167755Ssam			    , crp->crp_callback
1509167755Ssam			);
1510167755Ssam		}
1511167755Ssam	}
1512167755Ssam}
1513167755Ssam
1514167755SsamDB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1515167755Ssam{
1516167755Ssam	struct cryptkop *krp;
1517167755Ssam
1518167755Ssam	db_show_drivers();
1519167755Ssam	db_printf("\n");
1520167755Ssam
1521167755Ssam	db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1522167755Ssam	    "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1523167755Ssam	TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1524167755Ssam		db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1525167755Ssam		    , krp->krp_op
1526167755Ssam		    , krp->krp_status
1527167755Ssam		    , krp->krp_iparams, krp->krp_oparams
1528167755Ssam		    , krp->krp_crid, krp->krp_hid
1529167755Ssam		    , krp->krp_callback
1530167755Ssam		);
1531167755Ssam	}
1532167755Ssam	if (!TAILQ_EMPTY(&crp_ret_q)) {
1533167755Ssam		db_printf("%4s %5s %8s %4s %8s\n",
1534167755Ssam		    "Op", "Status", "CRID", "HID", "Callback");
1535167755Ssam		TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
1536167755Ssam			db_printf("%4u %5u %08x %4u %8p\n"
1537167755Ssam			    , krp->krp_op
1538167755Ssam			    , krp->krp_status
1539167755Ssam			    , krp->krp_crid, krp->krp_hid
1540167755Ssam			    , krp->krp_callback
1541167755Ssam			);
1542167755Ssam		}
1543167755Ssam	}
1544167755Ssam}
1545167755Ssam#endif
1546167755Ssam
1547167755Ssamint crypto_modevent(module_t mod, int type, void *unused);
1548167755Ssam
1549167755Ssam/*
1550167755Ssam * Initialization code, both for static and dynamic loading.
1551167755Ssam * Note this is not invoked with the usual MODULE_DECLARE
1552167755Ssam * mechanism but instead is listed as a dependency by the
1553167755Ssam * cryptosoft driver.  This guarantees proper ordering of
1554167755Ssam * calls on module load/unload.
1555167755Ssam */
1556167755Ssamint
1557167755Ssamcrypto_modevent(module_t mod, int type, void *unused)
1558167755Ssam{
1559167755Ssam	int error = EINVAL;
1560167755Ssam
1561167755Ssam	switch (type) {
1562167755Ssam	case MOD_LOAD:
1563167755Ssam		error = crypto_init();
1564167755Ssam		if (error == 0 && bootverbose)
1565167755Ssam			printf("crypto: <crypto core>\n");
1566167755Ssam		break;
1567167755Ssam	case MOD_UNLOAD:
1568167755Ssam		/*XXX disallow if active sessions */
1569167755Ssam		error = 0;
1570167755Ssam		crypto_destroy();
1571167755Ssam		return 0;
1572167755Ssam	}
1573167755Ssam	return error;
1574167755Ssam}
1575167755SsamMODULE_VERSION(crypto, 1);
1576167755SsamMODULE_DEPEND(crypto, zlib, 1, 1, 1);
1577