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