Home | History | Annotate | Line # | Download | only in opencrypto
crypto.c revision 1.20.8.2
      1  1.20.8.2      matt /*	crypto.c,v 1.20.8.1 2007/11/06 23:34:26 matt Exp */
      2       1.1  jonathan /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
      3       1.1  jonathan /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
      4       1.1  jonathan 
      5       1.1  jonathan /*
      6       1.1  jonathan  * The author of this code is Angelos D. Keromytis (angelos (at) cis.upenn.edu)
      7       1.1  jonathan  *
      8       1.1  jonathan  * This code was written by Angelos D. Keromytis in Athens, Greece, in
      9       1.1  jonathan  * February 2000. Network Security Technologies Inc. (NSTI) kindly
     10       1.1  jonathan  * supported the development of this code.
     11       1.1  jonathan  *
     12       1.1  jonathan  * Copyright (c) 2000, 2001 Angelos D. Keromytis
     13       1.1  jonathan  *
     14       1.1  jonathan  * Permission to use, copy, and modify this software with or without fee
     15       1.1  jonathan  * is hereby granted, provided that this entire notice is included in
     16       1.1  jonathan  * all source code copies of any software which is or includes a copy or
     17       1.1  jonathan  * modification of this software.
     18       1.1  jonathan  *
     19       1.1  jonathan  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     20       1.1  jonathan  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     21       1.1  jonathan  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     22       1.1  jonathan  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     23       1.1  jonathan  * PURPOSE.
     24       1.1  jonathan  */
     25       1.1  jonathan 
     26       1.1  jonathan #include <sys/cdefs.h>
     27  1.20.8.2      matt __KERNEL_RCSID(0, "crypto.c,v 1.20.8.1 2007/11/06 23:34:26 matt Exp");
     28       1.1  jonathan 
     29       1.1  jonathan #include <sys/param.h>
     30       1.1  jonathan #include <sys/reboot.h>
     31       1.1  jonathan #include <sys/systm.h>
     32       1.1  jonathan #include <sys/malloc.h>
     33       1.1  jonathan #include <sys/proc.h>
     34       1.1  jonathan #include <sys/pool.h>
     35       1.1  jonathan #include <sys/kthread.h>
     36      1.11   thorpej #include <sys/once.h>
     37      1.13  christos #include <sys/sysctl.h>
     38  1.20.8.1      matt #include <sys/intr.h>
     39       1.1  jonathan 
     40  1.20.8.2      matt #include "opt_ocf.h"
     41  1.20.8.1      matt #include <opencrypto/cryptodev.h>
     42       1.1  jonathan #include <opencrypto/xform.h>			/* XXX for M_XDATA */
     43       1.1  jonathan 
     44  1.20.8.2      matt kcondvar_t cryptoret_cv;
     45  1.20.8.2      matt kmutex_t crypto_mtx;
     46  1.20.8.2      matt 
     47  1.20.8.2      matt /* below are kludges for residual code wrtitten to FreeBSD interfaces */
     48       1.1  jonathan   #define SWI_CRYPTO 17
     49       1.1  jonathan   #define register_swi(lvl, fn)  \
     50  1.20.8.1      matt   softint_establish(SOFTINT_NET, (void (*)(void*))fn, NULL)
     51  1.20.8.1      matt   #define unregister_swi(lvl, fn)  softint_disestablish(softintr_cookie)
     52  1.20.8.1      matt   #define setsoftcrypto(x) softint_schedule(x)
     53       1.1  jonathan 
     54       1.1  jonathan #define	SESID2HID(sid)	(((sid) >> 32) & 0xffffffff)
     55       1.1  jonathan 
     56       1.1  jonathan /*
     57       1.1  jonathan  * Crypto drivers register themselves by allocating a slot in the
     58       1.1  jonathan  * crypto_drivers table with crypto_get_driverid() and then registering
     59       1.1  jonathan  * each algorithm they support with crypto_register() and crypto_kregister().
     60       1.1  jonathan  */
     61      1.11   thorpej static	struct cryptocap *crypto_drivers;
     62      1.11   thorpej static	int crypto_drivers_num;
     63       1.1  jonathan static	void* softintr_cookie;
     64       1.1  jonathan 
     65       1.1  jonathan /*
     66       1.1  jonathan  * There are two queues for crypto requests; one for symmetric (e.g.
     67       1.1  jonathan  * cipher) operations and one for asymmetric (e.g. MOD) operations.
     68       1.1  jonathan  * See below for how synchronization is handled.
     69       1.1  jonathan  */
     70      1.11   thorpej static	TAILQ_HEAD(,cryptop) crp_q =		/* request queues */
     71      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_q);
     72      1.11   thorpej static	TAILQ_HEAD(,cryptkop) crp_kq =
     73      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_kq);
     74       1.1  jonathan 
     75       1.1  jonathan /*
     76       1.1  jonathan  * There are two queues for processing completed crypto requests; one
     77       1.1  jonathan  * for the symmetric and one for the asymmetric ops.  We only need one
     78       1.1  jonathan  * but have two to avoid type futzing (cryptop vs. cryptkop).  See below
     79       1.1  jonathan  * for how synchronization is handled.
     80       1.1  jonathan  */
     81  1.20.8.2      matt static	TAILQ_HEAD(crprethead, cryptop) crp_ret_q =	/* callback queues */
     82      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_ret_q);
     83  1.20.8.2      matt static	TAILQ_HEAD(krprethead, cryptkop) crp_ret_kq =
     84      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_ret_kq);
     85       1.1  jonathan 
     86       1.1  jonathan /*
     87  1.20.8.2      matt  * XXX these functions are ghastly hacks for when the submission
     88  1.20.8.2      matt  * XXX routines discover a request that was not CBIMM is already
     89  1.20.8.2      matt  * XXX done, and must be yanked from the retq (where _done) put it
     90  1.20.8.2      matt  * XXX as cryptoret won't get the chance.  The queue is walked backwards
     91  1.20.8.2      matt  * XXX as the request is generally the last one queued.
     92  1.20.8.2      matt  *
     93  1.20.8.2      matt  *	 call with the lock held, or else.
     94  1.20.8.2      matt  */
     95  1.20.8.2      matt int
     96  1.20.8.2      matt crypto_ret_q_remove(struct cryptop *crp)
     97  1.20.8.2      matt {
     98  1.20.8.2      matt 	struct cryptop * acrp;
     99  1.20.8.2      matt 
    100  1.20.8.2      matt 	TAILQ_FOREACH_REVERSE(acrp, &crp_ret_q, crprethead, crp_next) {
    101  1.20.8.2      matt 		if (acrp == crp) {
    102  1.20.8.2      matt 			TAILQ_REMOVE(&crp_ret_q, crp, crp_next);
    103  1.20.8.2      matt 			crp->crp_flags &= (~CRYPTO_F_ONRETQ);
    104  1.20.8.2      matt 			return 1;
    105  1.20.8.2      matt 		}
    106  1.20.8.2      matt 	}
    107  1.20.8.2      matt 	return 0;
    108  1.20.8.2      matt }
    109  1.20.8.2      matt 
    110  1.20.8.2      matt int
    111  1.20.8.2      matt crypto_ret_kq_remove(struct cryptkop *krp)
    112  1.20.8.2      matt {
    113  1.20.8.2      matt 	struct cryptkop * akrp;
    114  1.20.8.2      matt 
    115  1.20.8.2      matt 	TAILQ_FOREACH_REVERSE(akrp, &crp_ret_kq, krprethead, krp_next) {
    116  1.20.8.2      matt 		if (akrp == krp) {
    117  1.20.8.2      matt 			TAILQ_REMOVE(&crp_ret_kq, krp, krp_next);
    118  1.20.8.2      matt 			krp->krp_flags &= (~CRYPTO_F_ONRETQ);
    119  1.20.8.2      matt 			return 1;
    120  1.20.8.2      matt 		}
    121  1.20.8.2      matt 	}
    122  1.20.8.2      matt 	return 0;
    123  1.20.8.2      matt }
    124  1.20.8.2      matt 
    125  1.20.8.2      matt /*
    126       1.1  jonathan  * Crypto op and desciptor data structures are allocated
    127       1.1  jonathan  * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
    128       1.1  jonathan  */
    129       1.1  jonathan struct pool cryptop_pool;
    130       1.1  jonathan struct pool cryptodesc_pool;
    131  1.20.8.2      matt struct pool cryptkop_pool;
    132       1.1  jonathan 
    133       1.1  jonathan int	crypto_usercrypto = 1;		/* userland may open /dev/crypto */
    134       1.1  jonathan int	crypto_userasymcrypto = 1;	/* userland may do asym crypto reqs */
    135      1.10     perry /*
    136       1.6  jonathan  * cryptodevallowsoft is (intended to be) sysctl'able, controlling
    137       1.6  jonathan  * access to hardware versus software transforms as below:
    138       1.6  jonathan  *
    139       1.6  jonathan  * crypto_devallowsoft < 0:  Force userlevel requests to use software
    140       1.6  jonathan  *                              transforms, always
    141       1.6  jonathan  * crypto_devallowsoft = 0:  Use hardware if present, grant userlevel
    142       1.6  jonathan  *                              requests for non-accelerated transforms
    143       1.6  jonathan  *                              (handling the latter in software)
    144       1.6  jonathan  * crypto_devallowsoft > 0:  Allow user requests only for transforms which
    145       1.6  jonathan  *                               are hardware-accelerated.
    146       1.6  jonathan  */
    147       1.9  jonathan int	crypto_devallowsoft = 1;	/* only use hardware crypto */
    148       1.6  jonathan 
    149      1.13  christos SYSCTL_SETUP(sysctl_opencrypto_setup, "sysctl opencrypto subtree setup")
    150      1.13  christos {
    151      1.13  christos 	sysctl_createv(clog, 0, NULL, NULL,
    152      1.13  christos 		       CTLFLAG_PERMANENT,
    153      1.13  christos 		       CTLTYPE_NODE, "kern", NULL,
    154      1.13  christos 		       NULL, 0, NULL, 0,
    155      1.13  christos 		       CTL_KERN, CTL_EOL);
    156      1.13  christos 	sysctl_createv(clog, 0, NULL, NULL,
    157      1.13  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    158      1.13  christos 		       CTLTYPE_INT, "usercrypto",
    159      1.13  christos 		       SYSCTL_DESCR("Enable/disable user-mode access to "
    160      1.13  christos 			   "crypto support"),
    161      1.13  christos 		       NULL, 0, &crypto_usercrypto, 0,
    162      1.13  christos 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    163      1.13  christos 	sysctl_createv(clog, 0, NULL, NULL,
    164      1.13  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    165      1.13  christos 		       CTLTYPE_INT, "userasymcrypto",
    166      1.13  christos 		       SYSCTL_DESCR("Enable/disable user-mode access to "
    167      1.13  christos 			   "asymmetric crypto support"),
    168      1.13  christos 		       NULL, 0, &crypto_userasymcrypto, 0,
    169      1.13  christos 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    170      1.13  christos 	sysctl_createv(clog, 0, NULL, NULL,
    171      1.13  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    172      1.13  christos 		       CTLTYPE_INT, "cryptodevallowsoft",
    173      1.13  christos 		       SYSCTL_DESCR("Enable/disable use of software "
    174      1.13  christos 			   "asymmetric crypto support"),
    175      1.13  christos 		       NULL, 0, &crypto_devallowsoft, 0,
    176      1.13  christos 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    177      1.13  christos }
    178       1.1  jonathan 
    179       1.1  jonathan MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
    180       1.1  jonathan 
    181       1.1  jonathan /*
    182       1.1  jonathan  * Synchronization: read carefully, this is non-trivial.
    183       1.1  jonathan  *
    184       1.1  jonathan  * Crypto requests are submitted via crypto_dispatch.  Typically
    185       1.1  jonathan  * these come in from network protocols at spl0 (output path) or
    186       1.1  jonathan  * spl[,soft]net (input path).
    187       1.1  jonathan  *
    188       1.1  jonathan  * Requests are typically passed on the driver directly, but they
    189       1.1  jonathan  * may also be queued for processing by a software interrupt thread,
    190      1.10     perry  * cryptointr, that runs at splsoftcrypto.  This thread dispatches
    191       1.1  jonathan  * the requests to crypto drivers (h/w or s/w) who call crypto_done
    192       1.1  jonathan  * when a request is complete.  Hardware crypto drivers are assumed
    193       1.1  jonathan  * to register their IRQ's as network devices so their interrupt handlers
    194       1.1  jonathan  * and subsequent "done callbacks" happen at spl[imp,net].
    195       1.1  jonathan  *
    196       1.1  jonathan  * Completed crypto ops are queued for a separate kernel thread that
    197       1.1  jonathan  * handles the callbacks at spl0.  This decoupling insures the crypto
    198       1.1  jonathan  * driver interrupt service routine is not delayed while the callback
    199       1.1  jonathan  * takes place and that callbacks are delivered after a context switch
    200       1.1  jonathan  * (as opposed to a software interrupt that clients must block).
    201       1.1  jonathan  *
    202       1.1  jonathan  * This scheme is not intended for SMP machines.
    203      1.10     perry  */
    204       1.1  jonathan static	void cryptointr(void);		/* swi thread to dispatch ops */
    205       1.1  jonathan static	void cryptoret(void);		/* kernel thread for callbacks*/
    206      1.20        ad static	struct lwp *cryptothread;
    207       1.1  jonathan static	void crypto_destroy(void);
    208       1.1  jonathan static	int crypto_invoke(struct cryptop *crp, int hint);
    209       1.1  jonathan static	int crypto_kinvoke(struct cryptkop *krp, int hint);
    210       1.1  jonathan 
    211       1.1  jonathan static struct cryptostats cryptostats;
    212  1.20.8.2      matt #ifdef CRYPTO_TIMING
    213       1.1  jonathan static	int crypto_timing = 0;
    214  1.20.8.2      matt #endif
    215       1.1  jonathan 
    216      1.12      yamt static int
    217      1.11   thorpej crypto_init0(void)
    218       1.1  jonathan {
    219       1.1  jonathan 	int error;
    220       1.1  jonathan 
    221  1.20.8.2      matt 	mutex_init(&crypto_mtx, MUTEX_DEFAULT, IPL_NET);
    222  1.20.8.2      matt 	cv_init(&cryptoret_cv, "crypto_wait");
    223  1.20.8.2      matt 	pool_init(&cryptop_pool, sizeof(struct cryptop), 0, 0,
    224  1.20.8.2      matt 		  0, "cryptop", NULL, IPL_NET);
    225  1.20.8.2      matt 	pool_init(&cryptodesc_pool, sizeof(struct cryptodesc), 0, 0,
    226  1.20.8.2      matt 		  0, "cryptodesc", NULL, IPL_NET);
    227  1.20.8.2      matt 	pool_init(&cryptkop_pool, sizeof(struct cryptkop), 0, 0,
    228  1.20.8.2      matt 		  0, "cryptkop", NULL, IPL_NET);
    229       1.1  jonathan 
    230      1.11   thorpej 	crypto_drivers = malloc(CRYPTO_DRIVERS_INITIAL *
    231       1.1  jonathan 	    sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
    232       1.1  jonathan 	if (crypto_drivers == NULL) {
    233       1.1  jonathan 		printf("crypto_init: cannot malloc driver table\n");
    234      1.12      yamt 		return 0;
    235       1.1  jonathan 	}
    236      1.11   thorpej 	crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
    237       1.1  jonathan 
    238       1.1  jonathan 	softintr_cookie = register_swi(SWI_CRYPTO, cryptointr);
    239  1.20.8.2      matt 	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    240  1.20.8.2      matt 	    (void (*)(void*))cryptoret, NULL, &cryptothread, "cryptoret");
    241       1.1  jonathan 	if (error) {
    242       1.1  jonathan 		printf("crypto_init: cannot start cryptoret thread; error %d",
    243       1.1  jonathan 			error);
    244       1.1  jonathan 		crypto_destroy();
    245       1.1  jonathan 	}
    246      1.20        ad 
    247      1.12      yamt 	return 0;
    248      1.11   thorpej }
    249      1.11   thorpej 
    250      1.11   thorpej void
    251      1.11   thorpej crypto_init(void)
    252      1.11   thorpej {
    253      1.18    daniel 	static ONCE_DECL(crypto_init_once);
    254      1.11   thorpej 
    255      1.11   thorpej 	RUN_ONCE(&crypto_init_once, crypto_init0);
    256       1.1  jonathan }
    257       1.1  jonathan 
    258       1.1  jonathan static void
    259       1.1  jonathan crypto_destroy(void)
    260       1.1  jonathan {
    261       1.1  jonathan 	/* XXX no wait to reclaim zones */
    262       1.1  jonathan 	if (crypto_drivers != NULL)
    263       1.1  jonathan 		free(crypto_drivers, M_CRYPTO_DATA);
    264       1.1  jonathan 	unregister_swi(SWI_CRYPTO, cryptointr);
    265       1.1  jonathan }
    266       1.1  jonathan 
    267       1.1  jonathan /*
    268  1.20.8.2      matt  * Create a new session.  Must be called with crypto_mtx held.
    269       1.1  jonathan  */
    270       1.1  jonathan int
    271       1.1  jonathan crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
    272       1.1  jonathan {
    273       1.1  jonathan 	struct cryptoini *cr;
    274       1.1  jonathan 	u_int32_t hid, lid;
    275       1.1  jonathan 	int err = EINVAL;
    276       1.1  jonathan 
    277  1.20.8.2      matt 	KASSERT(mutex_owned(&crypto_mtx));
    278       1.1  jonathan 
    279       1.1  jonathan 	if (crypto_drivers == NULL)
    280       1.1  jonathan 		goto done;
    281       1.1  jonathan 
    282       1.1  jonathan 	/*
    283       1.1  jonathan 	 * The algorithm we use here is pretty stupid; just use the
    284       1.1  jonathan 	 * first driver that supports all the algorithms we need.
    285       1.1  jonathan 	 *
    286       1.1  jonathan 	 * XXX We need more smarts here (in real life too, but that's
    287       1.1  jonathan 	 * XXX another story altogether).
    288       1.1  jonathan 	 */
    289       1.1  jonathan 
    290       1.1  jonathan 	for (hid = 0; hid < crypto_drivers_num; hid++) {
    291       1.1  jonathan 		/*
    292       1.1  jonathan 		 * If it's not initialized or has remaining sessions
    293       1.1  jonathan 		 * referencing it, skip.
    294       1.1  jonathan 		 */
    295       1.1  jonathan 		if (crypto_drivers[hid].cc_newsession == NULL ||
    296       1.1  jonathan 		    (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP))
    297       1.1  jonathan 			continue;
    298       1.1  jonathan 
    299       1.1  jonathan 		/* Hardware required -- ignore software drivers. */
    300       1.1  jonathan 		if (hard > 0 &&
    301       1.1  jonathan 		    (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE))
    302       1.1  jonathan 			continue;
    303       1.1  jonathan 		/* Software required -- ignore hardware drivers. */
    304       1.1  jonathan 		if (hard < 0 &&
    305       1.1  jonathan 		    (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
    306       1.1  jonathan 			continue;
    307       1.1  jonathan 
    308       1.1  jonathan 		/* See if all the algorithms are supported. */
    309       1.1  jonathan 		for (cr = cri; cr; cr = cr->cri_next)
    310       1.1  jonathan 			if (crypto_drivers[hid].cc_alg[cr->cri_alg] == 0)
    311       1.1  jonathan 				break;
    312       1.1  jonathan 
    313       1.1  jonathan 		if (cr == NULL) {
    314       1.1  jonathan 			/* Ok, all algorithms are supported. */
    315       1.1  jonathan 
    316       1.1  jonathan 			/*
    317       1.1  jonathan 			 * Can't do everything in one session.
    318       1.1  jonathan 			 *
    319       1.1  jonathan 			 * XXX Fix this. We need to inject a "virtual" session layer right
    320       1.1  jonathan 			 * XXX about here.
    321       1.1  jonathan 			 */
    322       1.1  jonathan 
    323       1.1  jonathan 			/* Call the driver initialization routine. */
    324       1.1  jonathan 			lid = hid;		/* Pass the driver ID. */
    325       1.1  jonathan 			err = crypto_drivers[hid].cc_newsession(
    326       1.1  jonathan 					crypto_drivers[hid].cc_arg, &lid, cri);
    327       1.1  jonathan 			if (err == 0) {
    328       1.1  jonathan 				(*sid) = hid;
    329       1.1  jonathan 				(*sid) <<= 32;
    330       1.1  jonathan 				(*sid) |= (lid & 0xffffffff);
    331       1.1  jonathan 				crypto_drivers[hid].cc_sessions++;
    332       1.1  jonathan 			}
    333       1.1  jonathan 			goto done;
    334       1.1  jonathan 			/*break;*/
    335       1.1  jonathan 		}
    336       1.1  jonathan 	}
    337       1.1  jonathan done:
    338       1.1  jonathan 	return err;
    339       1.1  jonathan }
    340       1.1  jonathan 
    341       1.1  jonathan /*
    342       1.1  jonathan  * Delete an existing session (or a reserved session on an unregistered
    343  1.20.8.2      matt  * driver).  Must be called with crypto_mtx mutex held.
    344       1.1  jonathan  */
    345       1.1  jonathan int
    346       1.1  jonathan crypto_freesession(u_int64_t sid)
    347       1.1  jonathan {
    348       1.1  jonathan 	u_int32_t hid;
    349       1.1  jonathan 	int err = 0;
    350       1.1  jonathan 
    351  1.20.8.2      matt 	KASSERT(mutex_owned(&crypto_mtx));
    352       1.1  jonathan 
    353       1.1  jonathan 	if (crypto_drivers == NULL) {
    354       1.1  jonathan 		err = EINVAL;
    355       1.1  jonathan 		goto done;
    356       1.1  jonathan 	}
    357       1.1  jonathan 
    358       1.1  jonathan 	/* Determine two IDs. */
    359       1.1  jonathan 	hid = SESID2HID(sid);
    360       1.1  jonathan 
    361       1.1  jonathan 	if (hid >= crypto_drivers_num) {
    362       1.1  jonathan 		err = ENOENT;
    363       1.1  jonathan 		goto done;
    364       1.1  jonathan 	}
    365       1.1  jonathan 
    366       1.1  jonathan 	if (crypto_drivers[hid].cc_sessions)
    367       1.1  jonathan 		crypto_drivers[hid].cc_sessions--;
    368       1.1  jonathan 
    369       1.1  jonathan 	/* Call the driver cleanup routine, if available. */
    370  1.20.8.2      matt 	if (crypto_drivers[hid].cc_freesession) {
    371       1.1  jonathan 		err = crypto_drivers[hid].cc_freesession(
    372       1.1  jonathan 				crypto_drivers[hid].cc_arg, sid);
    373  1.20.8.2      matt 	}
    374       1.1  jonathan 	else
    375       1.1  jonathan 		err = 0;
    376       1.1  jonathan 
    377       1.1  jonathan 	/*
    378       1.1  jonathan 	 * If this was the last session of a driver marked as invalid,
    379       1.1  jonathan 	 * make the entry available for reuse.
    380       1.1  jonathan 	 */
    381       1.1  jonathan 	if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP) &&
    382       1.1  jonathan 	    crypto_drivers[hid].cc_sessions == 0)
    383       1.1  jonathan 		bzero(&crypto_drivers[hid], sizeof(struct cryptocap));
    384       1.1  jonathan 
    385       1.1  jonathan done:
    386       1.1  jonathan 	return err;
    387       1.1  jonathan }
    388       1.1  jonathan 
    389       1.1  jonathan /*
    390       1.1  jonathan  * Return an unused driver id.  Used by drivers prior to registering
    391       1.1  jonathan  * support for the algorithms they handle.
    392       1.1  jonathan  */
    393       1.1  jonathan int32_t
    394       1.1  jonathan crypto_get_driverid(u_int32_t flags)
    395       1.1  jonathan {
    396       1.1  jonathan 	struct cryptocap *newdrv;
    397  1.20.8.2      matt 	int i;
    398       1.1  jonathan 
    399  1.20.8.2      matt 	crypto_init();		/* XXX oh, this is foul! */
    400      1.11   thorpej 
    401  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    402       1.1  jonathan 	for (i = 0; i < crypto_drivers_num; i++)
    403       1.1  jonathan 		if (crypto_drivers[i].cc_process == NULL &&
    404       1.1  jonathan 		    (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0 &&
    405       1.1  jonathan 		    crypto_drivers[i].cc_sessions == 0)
    406       1.1  jonathan 			break;
    407       1.1  jonathan 
    408       1.1  jonathan 	/* Out of entries, allocate some more. */
    409       1.1  jonathan 	if (i == crypto_drivers_num) {
    410       1.1  jonathan 		/* Be careful about wrap-around. */
    411       1.1  jonathan 		if (2 * crypto_drivers_num <= crypto_drivers_num) {
    412  1.20.8.2      matt 			mutex_spin_exit(&crypto_mtx);
    413       1.1  jonathan 			printf("crypto: driver count wraparound!\n");
    414       1.1  jonathan 			return -1;
    415       1.1  jonathan 		}
    416       1.1  jonathan 
    417       1.1  jonathan 		newdrv = malloc(2 * crypto_drivers_num *
    418       1.1  jonathan 		    sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
    419       1.1  jonathan 		if (newdrv == NULL) {
    420  1.20.8.2      matt 			mutex_spin_exit(&crypto_mtx);
    421       1.1  jonathan 			printf("crypto: no space to expand driver table!\n");
    422       1.1  jonathan 			return -1;
    423       1.1  jonathan 		}
    424       1.1  jonathan 
    425       1.1  jonathan 		bcopy(crypto_drivers, newdrv,
    426       1.1  jonathan 		    crypto_drivers_num * sizeof(struct cryptocap));
    427       1.1  jonathan 
    428       1.1  jonathan 		crypto_drivers_num *= 2;
    429       1.1  jonathan 
    430       1.1  jonathan 		free(crypto_drivers, M_CRYPTO_DATA);
    431       1.1  jonathan 		crypto_drivers = newdrv;
    432       1.1  jonathan 	}
    433       1.1  jonathan 
    434       1.1  jonathan 	/* NB: state is zero'd on free */
    435       1.1  jonathan 	crypto_drivers[i].cc_sessions = 1;	/* Mark */
    436       1.1  jonathan 	crypto_drivers[i].cc_flags = flags;
    437       1.1  jonathan 
    438       1.1  jonathan 	if (bootverbose)
    439       1.1  jonathan 		printf("crypto: assign driver %u, flags %u\n", i, flags);
    440       1.1  jonathan 
    441  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
    442       1.1  jonathan 
    443       1.1  jonathan 	return i;
    444       1.1  jonathan }
    445       1.1  jonathan 
    446       1.1  jonathan static struct cryptocap *
    447       1.1  jonathan crypto_checkdriver(u_int32_t hid)
    448       1.1  jonathan {
    449       1.1  jonathan 	if (crypto_drivers == NULL)
    450       1.1  jonathan 		return NULL;
    451       1.1  jonathan 	return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
    452       1.1  jonathan }
    453       1.1  jonathan 
    454       1.1  jonathan /*
    455       1.1  jonathan  * Register support for a key-related algorithm.  This routine
    456       1.1  jonathan  * is called once for each algorithm supported a driver.
    457       1.1  jonathan  */
    458       1.1  jonathan int
    459       1.1  jonathan crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags,
    460       1.1  jonathan     int (*kprocess)(void*, struct cryptkop *, int),
    461       1.1  jonathan     void *karg)
    462       1.1  jonathan {
    463       1.1  jonathan 	struct cryptocap *cap;
    464       1.1  jonathan 	int err;
    465       1.1  jonathan 
    466  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    467       1.1  jonathan 
    468       1.1  jonathan 	cap = crypto_checkdriver(driverid);
    469       1.1  jonathan 	if (cap != NULL &&
    470       1.1  jonathan 	    (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
    471       1.1  jonathan 		/*
    472       1.1  jonathan 		 * XXX Do some performance testing to determine placing.
    473       1.1  jonathan 		 * XXX We probably need an auxiliary data structure that
    474       1.1  jonathan 		 * XXX describes relative performances.
    475       1.1  jonathan 		 */
    476       1.1  jonathan 
    477       1.1  jonathan 		cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
    478  1.20.8.2      matt 		if (bootverbose) {
    479  1.20.8.2      matt 			printf("crypto: driver %u registers key alg %u "
    480  1.20.8.2      matt 			       " flags %u\n",
    481  1.20.8.2      matt 				driverid,
    482  1.20.8.2      matt 				kalg,
    483  1.20.8.2      matt 				flags
    484       1.1  jonathan 			);
    485  1.20.8.2      matt 		}
    486       1.1  jonathan 
    487       1.1  jonathan 		if (cap->cc_kprocess == NULL) {
    488       1.1  jonathan 			cap->cc_karg = karg;
    489       1.1  jonathan 			cap->cc_kprocess = kprocess;
    490       1.1  jonathan 		}
    491       1.1  jonathan 		err = 0;
    492       1.1  jonathan 	} else
    493       1.1  jonathan 		err = EINVAL;
    494       1.1  jonathan 
    495  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
    496       1.1  jonathan 	return err;
    497       1.1  jonathan }
    498       1.1  jonathan 
    499       1.1  jonathan /*
    500       1.1  jonathan  * Register support for a non-key-related algorithm.  This routine
    501       1.1  jonathan  * is called once for each such algorithm supported by a driver.
    502       1.1  jonathan  */
    503       1.1  jonathan int
    504       1.1  jonathan crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
    505       1.1  jonathan     u_int32_t flags,
    506       1.1  jonathan     int (*newses)(void*, u_int32_t*, struct cryptoini*),
    507       1.1  jonathan     int (*freeses)(void*, u_int64_t),
    508       1.1  jonathan     int (*process)(void*, struct cryptop *, int),
    509       1.1  jonathan     void *arg)
    510       1.1  jonathan {
    511       1.1  jonathan 	struct cryptocap *cap;
    512  1.20.8.2      matt 	int err;
    513       1.1  jonathan 
    514  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    515       1.1  jonathan 
    516       1.1  jonathan 	cap = crypto_checkdriver(driverid);
    517       1.1  jonathan 	/* NB: algorithms are in the range [1..max] */
    518       1.1  jonathan 	if (cap != NULL &&
    519       1.1  jonathan 	    (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
    520       1.1  jonathan 		/*
    521       1.1  jonathan 		 * XXX Do some performance testing to determine placing.
    522       1.1  jonathan 		 * XXX We probably need an auxiliary data structure that
    523       1.1  jonathan 		 * XXX describes relative performances.
    524       1.1  jonathan 		 */
    525       1.1  jonathan 
    526       1.1  jonathan 		cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
    527       1.1  jonathan 		cap->cc_max_op_len[alg] = maxoplen;
    528  1.20.8.2      matt 		if (bootverbose) {
    529  1.20.8.2      matt 			printf("crypto: driver %u registers alg %u "
    530  1.20.8.2      matt 				"flags %u maxoplen %u\n",
    531  1.20.8.2      matt 				driverid,
    532  1.20.8.2      matt 				alg,
    533  1.20.8.2      matt 				flags,
    534  1.20.8.2      matt 				maxoplen
    535       1.1  jonathan 			);
    536  1.20.8.2      matt 		}
    537       1.1  jonathan 
    538       1.1  jonathan 		if (cap->cc_process == NULL) {
    539       1.1  jonathan 			cap->cc_arg = arg;
    540       1.1  jonathan 			cap->cc_newsession = newses;
    541       1.1  jonathan 			cap->cc_process = process;
    542       1.1  jonathan 			cap->cc_freesession = freeses;
    543       1.1  jonathan 			cap->cc_sessions = 0;		/* Unmark */
    544       1.1  jonathan 		}
    545       1.1  jonathan 		err = 0;
    546       1.1  jonathan 	} else
    547       1.1  jonathan 		err = EINVAL;
    548       1.1  jonathan 
    549  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
    550       1.1  jonathan 	return err;
    551       1.1  jonathan }
    552       1.1  jonathan 
    553       1.1  jonathan /*
    554       1.1  jonathan  * Unregister a crypto driver. If there are pending sessions using it,
    555       1.1  jonathan  * leave enough information around so that subsequent calls using those
    556       1.1  jonathan  * sessions will correctly detect the driver has been unregistered and
    557       1.1  jonathan  * reroute requests.
    558       1.1  jonathan  */
    559       1.1  jonathan int
    560       1.1  jonathan crypto_unregister(u_int32_t driverid, int alg)
    561       1.1  jonathan {
    562  1.20.8.2      matt 	int i, err;
    563       1.1  jonathan 	u_int32_t ses;
    564       1.1  jonathan 	struct cryptocap *cap;
    565       1.1  jonathan 
    566  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    567       1.1  jonathan 
    568       1.1  jonathan 	cap = crypto_checkdriver(driverid);
    569       1.1  jonathan 	if (cap != NULL &&
    570       1.1  jonathan 	    (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
    571       1.1  jonathan 	    cap->cc_alg[alg] != 0) {
    572       1.1  jonathan 		cap->cc_alg[alg] = 0;
    573       1.1  jonathan 		cap->cc_max_op_len[alg] = 0;
    574       1.1  jonathan 
    575       1.1  jonathan 		/* Was this the last algorithm ? */
    576       1.1  jonathan 		for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
    577       1.1  jonathan 			if (cap->cc_alg[i] != 0)
    578       1.1  jonathan 				break;
    579       1.1  jonathan 
    580       1.1  jonathan 		if (i == CRYPTO_ALGORITHM_MAX + 1) {
    581       1.1  jonathan 			ses = cap->cc_sessions;
    582       1.1  jonathan 			bzero(cap, sizeof(struct cryptocap));
    583       1.1  jonathan 			if (ses != 0) {
    584       1.1  jonathan 				/*
    585       1.1  jonathan 				 * If there are pending sessions, just mark as invalid.
    586       1.1  jonathan 				 */
    587       1.1  jonathan 				cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
    588       1.1  jonathan 				cap->cc_sessions = ses;
    589       1.1  jonathan 			}
    590       1.1  jonathan 		}
    591       1.1  jonathan 		err = 0;
    592       1.1  jonathan 	} else
    593       1.1  jonathan 		err = EINVAL;
    594       1.1  jonathan 
    595  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
    596       1.1  jonathan 	return err;
    597       1.1  jonathan }
    598       1.1  jonathan 
    599       1.1  jonathan /*
    600       1.1  jonathan  * Unregister all algorithms associated with a crypto driver.
    601       1.1  jonathan  * If there are pending sessions using it, leave enough information
    602       1.1  jonathan  * around so that subsequent calls using those sessions will
    603       1.1  jonathan  * correctly detect the driver has been unregistered and reroute
    604       1.1  jonathan  * requests.
    605  1.20.8.2      matt  *
    606  1.20.8.2      matt  * XXX careful.  Don't change this to call crypto_unregister() for each
    607  1.20.8.2      matt  * XXX registered algorithm unless you drop the mutex across the calls;
    608  1.20.8.2      matt  * XXX you can't take it recursively.
    609       1.1  jonathan  */
    610       1.1  jonathan int
    611       1.1  jonathan crypto_unregister_all(u_int32_t driverid)
    612       1.1  jonathan {
    613  1.20.8.2      matt 	int i, err;
    614       1.1  jonathan 	u_int32_t ses;
    615       1.1  jonathan 	struct cryptocap *cap;
    616       1.1  jonathan 
    617  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    618       1.1  jonathan 	cap = crypto_checkdriver(driverid);
    619       1.1  jonathan 	if (cap != NULL) {
    620       1.1  jonathan 		for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
    621       1.1  jonathan 			cap->cc_alg[i] = 0;
    622       1.1  jonathan 			cap->cc_max_op_len[i] = 0;
    623       1.1  jonathan 		}
    624       1.1  jonathan 		ses = cap->cc_sessions;
    625       1.1  jonathan 		bzero(cap, sizeof(struct cryptocap));
    626       1.1  jonathan 		if (ses != 0) {
    627       1.1  jonathan 			/*
    628       1.1  jonathan 			 * If there are pending sessions, just mark as invalid.
    629       1.1  jonathan 			 */
    630       1.1  jonathan 			cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
    631       1.1  jonathan 			cap->cc_sessions = ses;
    632       1.1  jonathan 		}
    633       1.1  jonathan 		err = 0;
    634       1.1  jonathan 	} else
    635       1.1  jonathan 		err = EINVAL;
    636       1.1  jonathan 
    637  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
    638       1.1  jonathan 	return err;
    639       1.1  jonathan }
    640       1.1  jonathan 
    641       1.1  jonathan /*
    642       1.1  jonathan  * Clear blockage on a driver.  The what parameter indicates whether
    643       1.1  jonathan  * the driver is now ready for cryptop's and/or cryptokop's.
    644       1.1  jonathan  */
    645       1.1  jonathan int
    646       1.1  jonathan crypto_unblock(u_int32_t driverid, int what)
    647       1.1  jonathan {
    648       1.1  jonathan 	struct cryptocap *cap;
    649  1.20.8.2      matt 	int needwakeup, err;
    650       1.1  jonathan 
    651  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    652       1.1  jonathan 	cap = crypto_checkdriver(driverid);
    653       1.1  jonathan 	if (cap != NULL) {
    654       1.1  jonathan 		needwakeup = 0;
    655       1.1  jonathan 		if (what & CRYPTO_SYMQ) {
    656       1.1  jonathan 			needwakeup |= cap->cc_qblocked;
    657       1.1  jonathan 			cap->cc_qblocked = 0;
    658       1.1  jonathan 		}
    659       1.1  jonathan 		if (what & CRYPTO_ASYMQ) {
    660       1.1  jonathan 			needwakeup |= cap->cc_kqblocked;
    661       1.1  jonathan 			cap->cc_kqblocked = 0;
    662       1.1  jonathan 		}
    663       1.1  jonathan 		err = 0;
    664  1.20.8.2      matt 		mutex_spin_exit(&crypto_mtx);
    665  1.20.8.2      matt 		if (needwakeup)
    666  1.20.8.2      matt 			setsoftcrypto(softintr_cookie);
    667  1.20.8.2      matt 	} else {
    668       1.1  jonathan 		err = EINVAL;
    669  1.20.8.2      matt 		mutex_spin_exit(&crypto_mtx);
    670  1.20.8.2      matt 	}
    671       1.1  jonathan 
    672       1.1  jonathan 	return err;
    673       1.1  jonathan }
    674       1.1  jonathan 
    675       1.1  jonathan /*
    676       1.1  jonathan  * Dispatch a crypto request to a driver or queue
    677       1.1  jonathan  * it, to be processed by the kernel thread.
    678       1.1  jonathan  */
    679       1.1  jonathan int
    680       1.1  jonathan crypto_dispatch(struct cryptop *crp)
    681       1.1  jonathan {
    682       1.1  jonathan 	u_int32_t hid = SESID2HID(crp->crp_sid);
    683  1.20.8.2      matt 	int result;
    684       1.1  jonathan 
    685  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    686       1.1  jonathan 
    687       1.1  jonathan 	cryptostats.cs_ops++;
    688       1.1  jonathan 
    689       1.1  jonathan #ifdef CRYPTO_TIMING
    690       1.1  jonathan 	if (crypto_timing)
    691       1.1  jonathan 		nanouptime(&crp->crp_tstamp);
    692       1.1  jonathan #endif
    693       1.1  jonathan 	if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
    694       1.1  jonathan 		struct cryptocap *cap;
    695       1.1  jonathan 		/*
    696       1.1  jonathan 		 * Caller marked the request to be processed
    697       1.1  jonathan 		 * immediately; dispatch it directly to the
    698       1.1  jonathan 		 * driver unless the driver is currently blocked.
    699       1.1  jonathan 		 */
    700       1.1  jonathan 		cap = crypto_checkdriver(hid);
    701       1.1  jonathan 		if (cap && !cap->cc_qblocked) {
    702  1.20.8.2      matt 			mutex_spin_exit(&crypto_mtx);
    703       1.1  jonathan 			result = crypto_invoke(crp, 0);
    704       1.1  jonathan 			if (result == ERESTART) {
    705       1.1  jonathan 				/*
    706       1.1  jonathan 				 * The driver ran out of resources, mark the
    707       1.1  jonathan 				 * driver ``blocked'' for cryptop's and put
    708       1.1  jonathan 				 * the op on the queue.
    709       1.1  jonathan 				 */
    710  1.20.8.2      matt 				mutex_spin_enter(&crypto_mtx);
    711       1.1  jonathan 				crypto_drivers[hid].cc_qblocked = 1;
    712       1.1  jonathan 				TAILQ_INSERT_HEAD(&crp_q, crp, crp_next);
    713       1.1  jonathan 				cryptostats.cs_blocks++;
    714  1.20.8.2      matt 				mutex_spin_exit(&crypto_mtx);
    715       1.1  jonathan 			}
    716  1.20.8.2      matt 			goto out_released;
    717       1.1  jonathan 		} else {
    718       1.1  jonathan 			/*
    719       1.1  jonathan 			 * The driver is blocked, just queue the op until
    720       1.1  jonathan 			 * it unblocks and the swi thread gets kicked.
    721       1.1  jonathan 			 */
    722       1.1  jonathan 			TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
    723       1.1  jonathan 			result = 0;
    724       1.1  jonathan 		}
    725       1.1  jonathan 	} else {
    726       1.1  jonathan 		int wasempty = TAILQ_EMPTY(&crp_q);
    727       1.1  jonathan 		/*
    728       1.1  jonathan 		 * Caller marked the request as ``ok to delay'';
    729       1.1  jonathan 		 * queue it for the swi thread.  This is desirable
    730       1.1  jonathan 		 * when the operation is low priority and/or suitable
    731       1.1  jonathan 		 * for batching.
    732       1.1  jonathan 		 */
    733       1.1  jonathan 		TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
    734       1.1  jonathan 		if (wasempty) {
    735  1.20.8.2      matt 			mutex_spin_exit(&crypto_mtx);
    736       1.1  jonathan 			setsoftcrypto(softintr_cookie);
    737  1.20.8.2      matt 			result = 0;
    738  1.20.8.2      matt 			goto out_released;
    739       1.1  jonathan 		}
    740       1.1  jonathan 
    741       1.1  jonathan 		result = 0;
    742       1.1  jonathan 	}
    743       1.1  jonathan 
    744  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
    745  1.20.8.2      matt out_released:
    746       1.1  jonathan 	return result;
    747       1.1  jonathan }
    748       1.1  jonathan 
    749       1.1  jonathan /*
    750       1.1  jonathan  * Add an asymetric crypto request to a queue,
    751       1.1  jonathan  * to be processed by the kernel thread.
    752       1.1  jonathan  */
    753       1.1  jonathan int
    754       1.1  jonathan crypto_kdispatch(struct cryptkop *krp)
    755       1.1  jonathan {
    756       1.1  jonathan 	struct cryptocap *cap;
    757  1.20.8.2      matt 	int result;
    758       1.1  jonathan 
    759  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    760       1.1  jonathan 	cryptostats.cs_kops++;
    761       1.1  jonathan 
    762       1.1  jonathan 	cap = crypto_checkdriver(krp->krp_hid);
    763       1.1  jonathan 	if (cap && !cap->cc_kqblocked) {
    764  1.20.8.2      matt 		mutex_spin_exit(&crypto_mtx);
    765       1.1  jonathan 		result = crypto_kinvoke(krp, 0);
    766       1.1  jonathan 		if (result == ERESTART) {
    767       1.1  jonathan 			/*
    768       1.1  jonathan 			 * The driver ran out of resources, mark the
    769       1.1  jonathan 			 * driver ``blocked'' for cryptop's and put
    770       1.1  jonathan 			 * the op on the queue.
    771       1.1  jonathan 			 */
    772  1.20.8.2      matt 			mutex_spin_enter(&crypto_mtx);
    773       1.1  jonathan 			crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
    774       1.1  jonathan 			TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
    775       1.1  jonathan 			cryptostats.cs_kblocks++;
    776  1.20.8.2      matt 			mutex_spin_exit(&crypto_mtx);
    777       1.1  jonathan 		}
    778       1.1  jonathan 	} else {
    779       1.1  jonathan 		/*
    780       1.1  jonathan 		 * The driver is blocked, just queue the op until
    781       1.1  jonathan 		 * it unblocks and the swi thread gets kicked.
    782       1.1  jonathan 		 */
    783       1.1  jonathan 		TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
    784       1.1  jonathan 		result = 0;
    785  1.20.8.2      matt 		mutex_spin_exit(&crypto_mtx);
    786       1.1  jonathan 	}
    787       1.1  jonathan 
    788       1.1  jonathan 	return result;
    789       1.1  jonathan }
    790       1.1  jonathan 
    791       1.1  jonathan /*
    792       1.1  jonathan  * Dispatch an assymetric crypto request to the appropriate crypto devices.
    793       1.1  jonathan  */
    794       1.1  jonathan static int
    795       1.1  jonathan crypto_kinvoke(struct cryptkop *krp, int hint)
    796       1.1  jonathan {
    797       1.1  jonathan 	u_int32_t hid;
    798       1.1  jonathan 	int error;
    799       1.1  jonathan 
    800       1.1  jonathan 	/* Sanity checks. */
    801       1.1  jonathan 	if (krp == NULL)
    802       1.1  jonathan 		return EINVAL;
    803       1.1  jonathan 	if (krp->krp_callback == NULL) {
    804  1.20.8.2      matt 		pool_put(&cryptkop_pool, krp);
    805       1.1  jonathan 		return EINVAL;
    806       1.1  jonathan 	}
    807       1.1  jonathan 
    808       1.1  jonathan 	for (hid = 0; hid < crypto_drivers_num; hid++) {
    809       1.1  jonathan 		if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
    810       1.1  jonathan 		    crypto_devallowsoft == 0)
    811       1.1  jonathan 			continue;
    812       1.1  jonathan 		if (crypto_drivers[hid].cc_kprocess == NULL)
    813       1.1  jonathan 			continue;
    814       1.1  jonathan 		if ((crypto_drivers[hid].cc_kalg[krp->krp_op] &
    815       1.1  jonathan 		    CRYPTO_ALG_FLAG_SUPPORTED) == 0)
    816       1.1  jonathan 			continue;
    817       1.1  jonathan 		break;
    818       1.1  jonathan 	}
    819       1.1  jonathan 	if (hid < crypto_drivers_num) {
    820       1.1  jonathan 		krp->krp_hid = hid;
    821       1.1  jonathan 		error = crypto_drivers[hid].cc_kprocess(
    822       1.1  jonathan 				crypto_drivers[hid].cc_karg, krp, hint);
    823       1.1  jonathan 	} else {
    824       1.1  jonathan 		error = ENODEV;
    825       1.1  jonathan 	}
    826       1.1  jonathan 
    827       1.1  jonathan 	if (error) {
    828       1.1  jonathan 		krp->krp_status = error;
    829       1.1  jonathan 		crypto_kdone(krp);
    830       1.1  jonathan 	}
    831       1.1  jonathan 	return 0;
    832       1.1  jonathan }
    833       1.1  jonathan 
    834       1.1  jonathan #ifdef CRYPTO_TIMING
    835       1.1  jonathan static void
    836       1.1  jonathan crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
    837       1.1  jonathan {
    838       1.1  jonathan 	struct timespec now, t;
    839       1.1  jonathan 
    840       1.1  jonathan 	nanouptime(&now);
    841       1.1  jonathan 	t.tv_sec = now.tv_sec - tv->tv_sec;
    842       1.1  jonathan 	t.tv_nsec = now.tv_nsec - tv->tv_nsec;
    843       1.1  jonathan 	if (t.tv_nsec < 0) {
    844       1.1  jonathan 		t.tv_sec--;
    845       1.1  jonathan 		t.tv_nsec += 1000000000;
    846       1.1  jonathan 	}
    847       1.1  jonathan 	timespecadd(&ts->acc, &t, &t);
    848       1.1  jonathan 	if (timespeccmp(&t, &ts->min, <))
    849       1.1  jonathan 		ts->min = t;
    850       1.1  jonathan 	if (timespeccmp(&t, &ts->max, >))
    851       1.1  jonathan 		ts->max = t;
    852       1.1  jonathan 	ts->count++;
    853       1.1  jonathan 
    854       1.1  jonathan 	*tv = now;
    855       1.1  jonathan }
    856       1.1  jonathan #endif
    857       1.1  jonathan 
    858       1.1  jonathan /*
    859       1.1  jonathan  * Dispatch a crypto request to the appropriate crypto devices.
    860       1.1  jonathan  */
    861       1.1  jonathan static int
    862       1.1  jonathan crypto_invoke(struct cryptop *crp, int hint)
    863       1.1  jonathan {
    864       1.1  jonathan 	u_int32_t hid;
    865       1.1  jonathan 	int (*process)(void*, struct cryptop *, int);
    866       1.1  jonathan 
    867       1.1  jonathan #ifdef CRYPTO_TIMING
    868       1.1  jonathan 	if (crypto_timing)
    869       1.1  jonathan 		crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
    870       1.1  jonathan #endif
    871       1.1  jonathan 	/* Sanity checks. */
    872       1.1  jonathan 	if (crp == NULL)
    873       1.1  jonathan 		return EINVAL;
    874       1.1  jonathan 	if (crp->crp_callback == NULL) {
    875       1.1  jonathan 		crypto_freereq(crp);
    876       1.1  jonathan 		return EINVAL;
    877       1.1  jonathan 	}
    878       1.1  jonathan 	if (crp->crp_desc == NULL) {
    879       1.1  jonathan 		crp->crp_etype = EINVAL;
    880       1.1  jonathan 		crypto_done(crp);
    881       1.1  jonathan 		return 0;
    882       1.1  jonathan 	}
    883       1.1  jonathan 
    884       1.1  jonathan 	hid = SESID2HID(crp->crp_sid);
    885       1.1  jonathan 	if (hid < crypto_drivers_num) {
    886  1.20.8.2      matt 		mutex_enter(&crypto_mtx);
    887       1.1  jonathan 		if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP)
    888       1.1  jonathan 			crypto_freesession(crp->crp_sid);
    889       1.1  jonathan 		process = crypto_drivers[hid].cc_process;
    890  1.20.8.2      matt 		mutex_exit(&crypto_mtx);
    891       1.1  jonathan 	} else {
    892       1.1  jonathan 		process = NULL;
    893       1.1  jonathan 	}
    894       1.1  jonathan 
    895       1.1  jonathan 	if (process == NULL) {
    896       1.1  jonathan 		struct cryptodesc *crd;
    897      1.16       mrg 		u_int64_t nid = 0;
    898       1.1  jonathan 
    899       1.1  jonathan 		/*
    900       1.1  jonathan 		 * Driver has unregistered; migrate the session and return
    901       1.1  jonathan 		 * an error to the caller so they'll resubmit the op.
    902       1.1  jonathan 		 */
    903  1.20.8.2      matt 		mutex_enter(&crypto_mtx);
    904       1.1  jonathan 		for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
    905       1.1  jonathan 			crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
    906       1.1  jonathan 
    907       1.1  jonathan 		if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 0) == 0)
    908       1.1  jonathan 			crp->crp_sid = nid;
    909       1.1  jonathan 
    910       1.1  jonathan 		crp->crp_etype = EAGAIN;
    911  1.20.8.2      matt 		mutex_exit(&crypto_mtx);
    912  1.20.8.2      matt 
    913       1.1  jonathan 		crypto_done(crp);
    914       1.1  jonathan 		return 0;
    915       1.1  jonathan 	} else {
    916       1.1  jonathan 		/*
    917       1.1  jonathan 		 * Invoke the driver to process the request.
    918       1.1  jonathan 		 */
    919  1.20.8.2      matt 		DPRINTF(("calling process for %08x\n", (uint32_t)crp));
    920       1.1  jonathan 		return (*process)(crypto_drivers[hid].cc_arg, crp, hint);
    921       1.1  jonathan 	}
    922       1.1  jonathan }
    923       1.1  jonathan 
    924       1.1  jonathan /*
    925       1.1  jonathan  * Release a set of crypto descriptors.
    926       1.1  jonathan  */
    927       1.1  jonathan void
    928       1.1  jonathan crypto_freereq(struct cryptop *crp)
    929       1.1  jonathan {
    930       1.1  jonathan 	struct cryptodesc *crd;
    931       1.1  jonathan 
    932       1.1  jonathan 	if (crp == NULL)
    933       1.1  jonathan 		return;
    934       1.1  jonathan 
    935       1.1  jonathan 	while ((crd = crp->crp_desc) != NULL) {
    936       1.1  jonathan 		crp->crp_desc = crd->crd_next;
    937       1.1  jonathan 		pool_put(&cryptodesc_pool, crd);
    938       1.1  jonathan 	}
    939       1.1  jonathan 	pool_put(&cryptop_pool, crp);
    940       1.1  jonathan }
    941       1.1  jonathan 
    942       1.1  jonathan /*
    943       1.1  jonathan  * Acquire a set of crypto descriptors.
    944       1.1  jonathan  */
    945       1.1  jonathan struct cryptop *
    946       1.1  jonathan crypto_getreq(int num)
    947       1.1  jonathan {
    948       1.1  jonathan 	struct cryptodesc *crd;
    949       1.1  jonathan 	struct cryptop *crp;
    950       1.1  jonathan 
    951       1.1  jonathan 	crp = pool_get(&cryptop_pool, 0);
    952       1.1  jonathan 	if (crp == NULL) {
    953       1.1  jonathan 		return NULL;
    954       1.1  jonathan 	}
    955       1.1  jonathan 	bzero(crp, sizeof(struct cryptop));
    956  1.20.8.2      matt 	cv_init(&crp->crp_cv, "crydev");
    957       1.1  jonathan 
    958       1.1  jonathan 	while (num--) {
    959       1.1  jonathan 		crd = pool_get(&cryptodesc_pool, 0);
    960       1.1  jonathan 		if (crd == NULL) {
    961       1.1  jonathan 			crypto_freereq(crp);
    962       1.1  jonathan 			return NULL;
    963       1.1  jonathan 		}
    964       1.1  jonathan 
    965       1.1  jonathan 		bzero(crd, sizeof(struct cryptodesc));
    966       1.1  jonathan 		crd->crd_next = crp->crp_desc;
    967       1.1  jonathan 		crp->crp_desc = crd;
    968       1.1  jonathan 	}
    969       1.1  jonathan 
    970       1.1  jonathan 	return crp;
    971       1.1  jonathan }
    972       1.1  jonathan 
    973       1.1  jonathan /*
    974       1.1  jonathan  * Invoke the callback on behalf of the driver.
    975       1.1  jonathan  */
    976       1.1  jonathan void
    977       1.1  jonathan crypto_done(struct cryptop *crp)
    978       1.1  jonathan {
    979  1.20.8.2      matt 	int wasempty;
    980  1.20.8.2      matt 
    981       1.1  jonathan 	if (crp->crp_etype != 0)
    982       1.1  jonathan 		cryptostats.cs_errs++;
    983       1.1  jonathan #ifdef CRYPTO_TIMING
    984       1.1  jonathan 	if (crypto_timing)
    985       1.1  jonathan 		crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
    986       1.1  jonathan #endif
    987       1.1  jonathan 	/*
    988  1.20.8.2      matt 	 * Normal case; queue the callback for the thread.
    989  1.20.8.2      matt 	 *
    990  1.20.8.2      matt 	 * The return queue is manipulated by the swi thread
    991  1.20.8.2      matt 	 * and, potentially, by crypto device drivers calling
    992  1.20.8.2      matt 	 * back to mark operations completed.  Thus we need
    993  1.20.8.2      matt 	 * to mask both while manipulating the return queue.
    994       1.1  jonathan 	 */
    995  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
    996  1.20.8.2      matt 	wasempty = TAILQ_EMPTY(&crp_ret_q);
    997  1.20.8.2      matt 	DPRINTF(("crypto_done: queueing %08x\n", (uint32_t)crp));
    998  1.20.8.2      matt 	crp->crp_flags |= CRYPTO_F_ONRETQ|CRYPTO_F_DONE;
    999  1.20.8.2      matt 	TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
   1000  1.20.8.2      matt 	if (wasempty) {
   1001  1.20.8.2      matt 		DPRINTF(("crypto_done: waking cryptoret, %08x " \
   1002  1.20.8.2      matt 			"hit empty queue\n.", (uint32_t)crp));
   1003  1.20.8.2      matt 		cv_signal(&cryptoret_cv);
   1004       1.1  jonathan 	}
   1005  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
   1006       1.1  jonathan }
   1007       1.1  jonathan 
   1008       1.1  jonathan /*
   1009       1.1  jonathan  * Invoke the callback on behalf of the driver.
   1010       1.1  jonathan  */
   1011       1.1  jonathan void
   1012       1.1  jonathan crypto_kdone(struct cryptkop *krp)
   1013       1.1  jonathan {
   1014  1.20.8.2      matt 	int wasempty;
   1015       1.1  jonathan 
   1016       1.1  jonathan 	if (krp->krp_status != 0)
   1017       1.1  jonathan 		cryptostats.cs_kerrs++;
   1018       1.1  jonathan 	/*
   1019       1.1  jonathan 	 * The return queue is manipulated by the swi thread
   1020       1.1  jonathan 	 * and, potentially, by crypto device drivers calling
   1021       1.1  jonathan 	 * back to mark operations completed.  Thus we need
   1022       1.1  jonathan 	 * to mask both while manipulating the return queue.
   1023       1.1  jonathan 	 */
   1024  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
   1025       1.1  jonathan 	wasempty = TAILQ_EMPTY(&crp_ret_kq);
   1026  1.20.8.2      matt 	krp->krp_flags |= CRYPTO_F_ONRETQ|CRYPTO_F_DONE;
   1027       1.1  jonathan 	TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
   1028       1.1  jonathan 	if (wasempty)
   1029  1.20.8.2      matt 		cv_signal(&cryptoret_cv);
   1030  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
   1031       1.1  jonathan }
   1032       1.1  jonathan 
   1033       1.1  jonathan int
   1034       1.1  jonathan crypto_getfeat(int *featp)
   1035       1.1  jonathan {
   1036       1.1  jonathan 	int hid, kalg, feat = 0;
   1037       1.1  jonathan 
   1038  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
   1039       1.1  jonathan 
   1040       1.1  jonathan 	if (crypto_userasymcrypto == 0)
   1041      1.10     perry 		goto out;
   1042       1.1  jonathan 
   1043       1.1  jonathan 	for (hid = 0; hid < crypto_drivers_num; hid++) {
   1044       1.1  jonathan 		if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
   1045       1.7  jonathan 		    crypto_devallowsoft == 0) {
   1046       1.1  jonathan 			continue;
   1047       1.1  jonathan 		}
   1048       1.1  jonathan 		if (crypto_drivers[hid].cc_kprocess == NULL)
   1049       1.1  jonathan 			continue;
   1050       1.1  jonathan 		for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
   1051       1.1  jonathan 			if ((crypto_drivers[hid].cc_kalg[kalg] &
   1052       1.1  jonathan 			    CRYPTO_ALG_FLAG_SUPPORTED) != 0)
   1053       1.1  jonathan 				feat |=  1 << kalg;
   1054       1.1  jonathan 	}
   1055       1.1  jonathan out:
   1056  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
   1057       1.1  jonathan 	*featp = feat;
   1058       1.1  jonathan 	return (0);
   1059       1.1  jonathan }
   1060       1.1  jonathan 
   1061       1.1  jonathan /*
   1062       1.1  jonathan  * Software interrupt thread to dispatch crypto requests.
   1063       1.1  jonathan  */
   1064       1.1  jonathan static void
   1065       1.1  jonathan cryptointr(void)
   1066       1.1  jonathan {
   1067       1.1  jonathan 	struct cryptop *crp, *submit;
   1068       1.1  jonathan 	struct cryptkop *krp;
   1069       1.1  jonathan 	struct cryptocap *cap;
   1070  1.20.8.2      matt 	int result, hint;
   1071       1.1  jonathan 
   1072       1.1  jonathan 	printf("crypto softint\n");
   1073       1.1  jonathan 	cryptostats.cs_intrs++;
   1074  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
   1075       1.1  jonathan 	do {
   1076       1.1  jonathan 		/*
   1077       1.1  jonathan 		 * Find the first element in the queue that can be
   1078       1.1  jonathan 		 * processed and look-ahead to see if multiple ops
   1079       1.1  jonathan 		 * are ready for the same driver.
   1080       1.1  jonathan 		 */
   1081       1.1  jonathan 		submit = NULL;
   1082       1.1  jonathan 		hint = 0;
   1083       1.1  jonathan 		TAILQ_FOREACH(crp, &crp_q, crp_next) {
   1084       1.1  jonathan 			u_int32_t hid = SESID2HID(crp->crp_sid);
   1085       1.1  jonathan 			cap = crypto_checkdriver(hid);
   1086       1.1  jonathan 			if (cap == NULL || cap->cc_process == NULL) {
   1087       1.1  jonathan 				/* Op needs to be migrated, process it. */
   1088       1.1  jonathan 				if (submit == NULL)
   1089       1.1  jonathan 					submit = crp;
   1090       1.1  jonathan 				break;
   1091       1.1  jonathan 			}
   1092       1.1  jonathan 			if (!cap->cc_qblocked) {
   1093       1.1  jonathan 				if (submit != NULL) {
   1094       1.1  jonathan 					/*
   1095       1.1  jonathan 					 * We stop on finding another op,
   1096       1.1  jonathan 					 * regardless whether its for the same
   1097       1.1  jonathan 					 * driver or not.  We could keep
   1098       1.1  jonathan 					 * searching the queue but it might be
   1099       1.1  jonathan 					 * better to just use a per-driver
   1100       1.1  jonathan 					 * queue instead.
   1101       1.1  jonathan 					 */
   1102       1.1  jonathan 					if (SESID2HID(submit->crp_sid) == hid)
   1103       1.1  jonathan 						hint = CRYPTO_HINT_MORE;
   1104       1.1  jonathan 					break;
   1105       1.1  jonathan 				} else {
   1106       1.1  jonathan 					submit = crp;
   1107       1.1  jonathan 					if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
   1108       1.1  jonathan 						break;
   1109       1.1  jonathan 					/* keep scanning for more are q'd */
   1110       1.1  jonathan 				}
   1111       1.1  jonathan 			}
   1112       1.1  jonathan 		}
   1113       1.1  jonathan 		if (submit != NULL) {
   1114       1.1  jonathan 			TAILQ_REMOVE(&crp_q, submit, crp_next);
   1115  1.20.8.2      matt 			mutex_spin_exit(&crypto_mtx);
   1116       1.1  jonathan 			result = crypto_invoke(submit, hint);
   1117  1.20.8.2      matt 			/* we must take here as the TAILQ op or kinvoke
   1118  1.20.8.2      matt 			   may need this mutex below.  sigh. */
   1119  1.20.8.2      matt 			mutex_spin_enter(&crypto_mtx);
   1120       1.1  jonathan 			if (result == ERESTART) {
   1121       1.1  jonathan 				/*
   1122       1.1  jonathan 				 * The driver ran out of resources, mark the
   1123       1.1  jonathan 				 * driver ``blocked'' for cryptop's and put
   1124       1.1  jonathan 				 * the request back in the queue.  It would
   1125       1.1  jonathan 				 * best to put the request back where we got
   1126       1.1  jonathan 				 * it but that's hard so for now we put it
   1127       1.1  jonathan 				 * at the front.  This should be ok; putting
   1128       1.1  jonathan 				 * it at the end does not work.
   1129       1.1  jonathan 				 */
   1130       1.1  jonathan 				/* XXX validate sid again? */
   1131       1.1  jonathan 				crypto_drivers[SESID2HID(submit->crp_sid)].cc_qblocked = 1;
   1132       1.1  jonathan 				TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
   1133       1.1  jonathan 				cryptostats.cs_blocks++;
   1134       1.1  jonathan 			}
   1135       1.1  jonathan 		}
   1136       1.1  jonathan 
   1137       1.1  jonathan 		/* As above, but for key ops */
   1138       1.1  jonathan 		TAILQ_FOREACH(krp, &crp_kq, krp_next) {
   1139       1.1  jonathan 			cap = crypto_checkdriver(krp->krp_hid);
   1140       1.1  jonathan 			if (cap == NULL || cap->cc_kprocess == NULL) {
   1141       1.1  jonathan 				/* Op needs to be migrated, process it. */
   1142       1.1  jonathan 				break;
   1143       1.1  jonathan 			}
   1144       1.1  jonathan 			if (!cap->cc_kqblocked)
   1145       1.1  jonathan 				break;
   1146       1.1  jonathan 		}
   1147       1.1  jonathan 		if (krp != NULL) {
   1148       1.1  jonathan 			TAILQ_REMOVE(&crp_kq, krp, krp_next);
   1149  1.20.8.2      matt 			mutex_spin_exit(&crypto_mtx);
   1150       1.1  jonathan 			result = crypto_kinvoke(krp, 0);
   1151  1.20.8.2      matt 			/* the next iteration will want the mutex. :-/ */
   1152  1.20.8.2      matt 			mutex_spin_enter(&crypto_mtx);
   1153       1.1  jonathan 			if (result == ERESTART) {
   1154       1.1  jonathan 				/*
   1155       1.1  jonathan 				 * The driver ran out of resources, mark the
   1156       1.1  jonathan 				 * driver ``blocked'' for cryptkop's and put
   1157       1.1  jonathan 				 * the request back in the queue.  It would
   1158       1.1  jonathan 				 * best to put the request back where we got
   1159       1.1  jonathan 				 * it but that's hard so for now we put it
   1160       1.1  jonathan 				 * at the front.  This should be ok; putting
   1161       1.1  jonathan 				 * it at the end does not work.
   1162       1.1  jonathan 				 */
   1163       1.1  jonathan 				/* XXX validate sid again? */
   1164       1.1  jonathan 				crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
   1165       1.1  jonathan 				TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
   1166       1.1  jonathan 				cryptostats.cs_kblocks++;
   1167       1.1  jonathan 			}
   1168       1.1  jonathan 		}
   1169       1.1  jonathan 	} while (submit != NULL || krp != NULL);
   1170  1.20.8.2      matt 	mutex_spin_exit(&crypto_mtx);
   1171       1.1  jonathan }
   1172       1.1  jonathan 
   1173       1.1  jonathan /*
   1174       1.1  jonathan  * Kernel thread to do callbacks.
   1175       1.1  jonathan  */
   1176       1.1  jonathan static void
   1177       1.1  jonathan cryptoret(void)
   1178       1.1  jonathan {
   1179       1.1  jonathan 	struct cryptop *crp;
   1180       1.1  jonathan 	struct cryptkop *krp;
   1181       1.1  jonathan 
   1182  1.20.8.2      matt 	mutex_spin_enter(&crypto_mtx);
   1183       1.1  jonathan 	for (;;) {
   1184       1.1  jonathan 		crp = TAILQ_FIRST(&crp_ret_q);
   1185  1.20.8.2      matt 		if (crp != NULL) {
   1186       1.1  jonathan 			TAILQ_REMOVE(&crp_ret_q, crp, crp_next);
   1187  1.20.8.2      matt 			crp->crp_flags &= ~CRYPTO_F_ONRETQ;
   1188  1.20.8.2      matt 		}
   1189       1.1  jonathan 		krp = TAILQ_FIRST(&crp_ret_kq);
   1190  1.20.8.2      matt 		if (krp != NULL) {
   1191       1.1  jonathan 			TAILQ_REMOVE(&crp_ret_kq, krp, krp_next);
   1192  1.20.8.2      matt 			krp->krp_flags &= ~CRYPTO_F_ONRETQ;
   1193  1.20.8.2      matt 		}
   1194       1.1  jonathan 
   1195  1.20.8.2      matt 		/* drop before calling any callbacks. */
   1196  1.20.8.2      matt 		if (crp == NULL && krp == NULL) {
   1197  1.20.8.2      matt 			cryptostats.cs_rets++;
   1198  1.20.8.2      matt 			cv_wait(&cryptoret_cv, &crypto_mtx);
   1199  1.20.8.2      matt 			continue;
   1200  1.20.8.2      matt 		}
   1201  1.20.8.2      matt 
   1202  1.20.8.2      matt 		mutex_spin_exit(&crypto_mtx);
   1203  1.20.8.2      matt 
   1204  1.20.8.2      matt 		if (crp != NULL) {
   1205       1.1  jonathan #ifdef CRYPTO_TIMING
   1206  1.20.8.2      matt 			if (crypto_timing) {
   1207  1.20.8.2      matt 				/*
   1208  1.20.8.2      matt 				 * NB: We must copy the timestamp before
   1209  1.20.8.2      matt 				 * doing the callback as the cryptop is
   1210  1.20.8.2      matt 				 * likely to be reclaimed.
   1211  1.20.8.2      matt 				 */
   1212  1.20.8.2      matt 				struct timespec t = crp->crp_tstamp;
   1213  1.20.8.2      matt 				crypto_tstat(&cryptostats.cs_cb, &t);
   1214  1.20.8.2      matt 				crp->crp_callback(crp);
   1215  1.20.8.2      matt 				crypto_tstat(&cryptostats.cs_finis, &t);
   1216  1.20.8.2      matt 			} else
   1217       1.1  jonathan #endif
   1218  1.20.8.2      matt 			{
   1219  1.20.8.2      matt 				crp->crp_callback(crp);
   1220       1.1  jonathan 			}
   1221       1.1  jonathan 		}
   1222  1.20.8.2      matt 		if (krp != NULL)
   1223  1.20.8.2      matt 			krp->krp_callback(krp);
   1224       1.1  jonathan 
   1225  1.20.8.2      matt 		mutex_spin_enter(&crypto_mtx);
   1226       1.1  jonathan 	}
   1227       1.1  jonathan }
   1228