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