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