Home | History | Annotate | Line # | Download | only in opencrypto
crypto.c revision 1.78.2.1
      1  1.78.2.1       snj /*	$NetBSD: crypto.c,v 1.78.2.1 2017/06/22 05:36:41 snj 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.78.2.1       snj __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.78.2.1 2017/06/22 05:36:41 snj 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.77  knakahar /* Don't directly access crypto_drivers[i], use crypto_checkdriver(i). */
    103      1.11   thorpej static	struct cryptocap *crypto_drivers;
    104      1.11   thorpej static	int crypto_drivers_num;
    105      1.37  christos static	void *softintr_cookie;
    106      1.46  pgoyette static	int crypto_exit_flag;
    107       1.1  jonathan 
    108       1.1  jonathan /*
    109       1.1  jonathan  * There are two queues for crypto requests; one for symmetric (e.g.
    110       1.1  jonathan  * cipher) operations and one for asymmetric (e.g. MOD) operations.
    111       1.1  jonathan  * See below for how synchronization is handled.
    112       1.1  jonathan  */
    113      1.11   thorpej static	TAILQ_HEAD(,cryptop) crp_q =		/* request queues */
    114      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_q);
    115      1.11   thorpej static	TAILQ_HEAD(,cryptkop) crp_kq =
    116      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_kq);
    117       1.1  jonathan 
    118       1.1  jonathan /*
    119       1.1  jonathan  * There are two queues for processing completed crypto requests; one
    120       1.1  jonathan  * for the symmetric and one for the asymmetric ops.  We only need one
    121       1.1  jonathan  * but have two to avoid type futzing (cryptop vs. cryptkop).  See below
    122       1.1  jonathan  * for how synchronization is handled.
    123       1.1  jonathan  */
    124      1.23       tls static	TAILQ_HEAD(crprethead, cryptop) crp_ret_q =	/* callback queues */
    125      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_ret_q);
    126      1.23       tls static	TAILQ_HEAD(krprethead, cryptkop) crp_ret_kq =
    127      1.11   thorpej 		TAILQ_HEAD_INITIALIZER(crp_ret_kq);
    128       1.1  jonathan 
    129      1.73  knakahar #define DEFINIT_CRYPTO_Q_LEN(name)		\
    130      1.73  knakahar 	static int crypto_##name##_len = 0
    131      1.73  knakahar 
    132      1.73  knakahar #define DEFINIT_CRYPTO_Q_DROPS(name)		\
    133      1.73  knakahar 	static int crypto_##name##_drops = 0
    134      1.73  knakahar 
    135      1.75  knakahar #define DEFINIT_CRYPTO_Q_MAXLEN(name, defval)		\
    136      1.75  knakahar 	static int crypto_##name##_maxlen = defval
    137      1.73  knakahar 
    138      1.73  knakahar #define CRYPTO_Q_INC(name)			\
    139      1.73  knakahar 	do {					\
    140      1.73  knakahar 		crypto_##name##_len++;		\
    141      1.73  knakahar 	} while(0);
    142      1.73  knakahar 
    143      1.73  knakahar #define CRYPTO_Q_DEC(name)			\
    144      1.73  knakahar 	do {					\
    145      1.73  knakahar 		crypto_##name##_len--;		\
    146      1.73  knakahar 	} while(0);
    147      1.73  knakahar 
    148      1.74  knakahar #define CRYPTO_Q_INC_DROPS(name)		\
    149      1.74  knakahar 	do {					\
    150      1.74  knakahar 		crypto_##name##_drops++;	\
    151      1.74  knakahar 	} while(0);
    152      1.74  knakahar 
    153      1.74  knakahar #define CRYPTO_Q_IS_FULL(name)					\
    154      1.74  knakahar 	(crypto_##name##_maxlen > 0				\
    155      1.74  knakahar 	    && (crypto_##name##_len > crypto_##name##_maxlen))
    156      1.74  knakahar 
    157      1.73  knakahar /*
    158      1.73  knakahar  * current queue length.
    159      1.73  knakahar  */
    160      1.73  knakahar DEFINIT_CRYPTO_Q_LEN(crp_ret_q);
    161      1.73  knakahar DEFINIT_CRYPTO_Q_LEN(crp_ret_kq);
    162      1.73  knakahar 
    163      1.73  knakahar /*
    164      1.73  knakahar  * queue dropped count.
    165      1.73  knakahar  */
    166      1.73  knakahar DEFINIT_CRYPTO_Q_DROPS(crp_ret_q);
    167      1.73  knakahar DEFINIT_CRYPTO_Q_DROPS(crp_ret_kq);
    168      1.73  knakahar 
    169      1.75  knakahar #ifndef CRYPTO_RET_Q_MAXLEN
    170      1.75  knakahar #define CRYPTO_RET_Q_MAXLEN 0
    171      1.75  knakahar #endif
    172      1.75  knakahar #ifndef CRYPTO_RET_KQ_MAXLEN
    173      1.75  knakahar #define CRYPTO_RET_KQ_MAXLEN 0
    174      1.75  knakahar #endif
    175      1.73  knakahar /*
    176      1.73  knakahar  * queue length limit.
    177      1.73  knakahar  * default value is 0. <=0 means unlimited.
    178      1.73  knakahar  */
    179      1.75  knakahar DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_q, CRYPTO_RET_Q_MAXLEN);
    180      1.75  knakahar DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_kq, CRYPTO_RET_KQ_MAXLEN);
    181      1.73  knakahar 
    182      1.73  knakahar /*
    183      1.73  knakahar  * TODO:
    184      1.73  knakahar  * make percpu
    185      1.73  knakahar  */
    186      1.73  knakahar static int
    187      1.73  knakahar sysctl_opencrypto_q_len(SYSCTLFN_ARGS)
    188      1.73  knakahar {
    189      1.73  knakahar 	int error;
    190      1.73  knakahar 
    191      1.73  knakahar 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    192      1.73  knakahar 	if (error || newp == NULL)
    193      1.73  knakahar 		return error;
    194      1.73  knakahar 
    195      1.73  knakahar 	return 0;
    196      1.73  knakahar }
    197      1.73  knakahar 
    198      1.73  knakahar /*
    199      1.73  knakahar  * TODO:
    200      1.73  knakahar  * make percpu
    201      1.73  knakahar  */
    202      1.73  knakahar static int
    203      1.73  knakahar sysctl_opencrypto_q_drops(SYSCTLFN_ARGS)
    204      1.73  knakahar {
    205      1.73  knakahar 	int error;
    206      1.73  knakahar 
    207      1.73  knakahar 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    208      1.73  knakahar 	if (error || newp == NULL)
    209      1.73  knakahar 		return error;
    210      1.73  knakahar 
    211      1.73  knakahar 	return 0;
    212      1.73  knakahar }
    213      1.73  knakahar 
    214      1.73  knakahar /*
    215      1.73  knakahar  * need to make percpu?
    216      1.73  knakahar  */
    217      1.73  knakahar static int
    218      1.73  knakahar sysctl_opencrypto_q_maxlen(SYSCTLFN_ARGS)
    219      1.73  knakahar {
    220      1.73  knakahar 	int error;
    221      1.73  knakahar 
    222      1.73  knakahar 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    223      1.73  knakahar 	if (error || newp == NULL)
    224      1.73  knakahar 		return error;
    225      1.73  knakahar 
    226      1.73  knakahar 	return 0;
    227      1.73  knakahar }
    228      1.73  knakahar 
    229       1.1  jonathan /*
    230       1.1  jonathan  * Crypto op and desciptor data structures are allocated
    231       1.1  jonathan  * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
    232       1.1  jonathan  */
    233       1.1  jonathan struct pool cryptop_pool;
    234       1.1  jonathan struct pool cryptodesc_pool;
    235      1.23       tls struct pool cryptkop_pool;
    236       1.1  jonathan 
    237       1.1  jonathan int	crypto_usercrypto = 1;		/* userland may open /dev/crypto */
    238       1.1  jonathan int	crypto_userasymcrypto = 1;	/* userland may do asym crypto reqs */
    239      1.10     perry /*
    240       1.6  jonathan  * cryptodevallowsoft is (intended to be) sysctl'able, controlling
    241       1.6  jonathan  * access to hardware versus software transforms as below:
    242       1.6  jonathan  *
    243       1.6  jonathan  * crypto_devallowsoft < 0:  Force userlevel requests to use software
    244       1.6  jonathan  *                              transforms, always
    245       1.6  jonathan  * crypto_devallowsoft = 0:  Use hardware if present, grant userlevel
    246       1.6  jonathan  *                              requests for non-accelerated transforms
    247       1.6  jonathan  *                              (handling the latter in software)
    248       1.6  jonathan  * crypto_devallowsoft > 0:  Allow user requests only for transforms which
    249       1.6  jonathan  *                               are hardware-accelerated.
    250       1.6  jonathan  */
    251       1.9  jonathan int	crypto_devallowsoft = 1;	/* only use hardware crypto */
    252       1.6  jonathan 
    253      1.72  knakahar static void
    254      1.72  knakahar sysctl_opencrypto_setup(struct sysctllog **clog)
    255      1.13  christos {
    256      1.73  knakahar 	const struct sysctlnode *ocnode;
    257      1.73  knakahar 	const struct sysctlnode *retqnode, *retkqnode;
    258      1.45     pooka 
    259      1.13  christos 	sysctl_createv(clog, 0, NULL, NULL,
    260      1.13  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    261      1.13  christos 		       CTLTYPE_INT, "usercrypto",
    262      1.13  christos 		       SYSCTL_DESCR("Enable/disable user-mode access to "
    263      1.13  christos 			   "crypto support"),
    264      1.13  christos 		       NULL, 0, &crypto_usercrypto, 0,
    265      1.13  christos 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    266      1.13  christos 	sysctl_createv(clog, 0, NULL, NULL,
    267      1.13  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    268      1.13  christos 		       CTLTYPE_INT, "userasymcrypto",
    269      1.13  christos 		       SYSCTL_DESCR("Enable/disable user-mode access to "
    270      1.13  christos 			   "asymmetric crypto support"),
    271      1.13  christos 		       NULL, 0, &crypto_userasymcrypto, 0,
    272      1.13  christos 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    273      1.13  christos 	sysctl_createv(clog, 0, NULL, NULL,
    274      1.13  christos 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    275      1.13  christos 		       CTLTYPE_INT, "cryptodevallowsoft",
    276      1.13  christos 		       SYSCTL_DESCR("Enable/disable use of software "
    277      1.13  christos 			   "asymmetric crypto support"),
    278      1.13  christos 		       NULL, 0, &crypto_devallowsoft, 0,
    279      1.13  christos 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    280      1.73  knakahar 
    281      1.73  knakahar 	sysctl_createv(clog, 0, NULL, &ocnode,
    282      1.73  knakahar 		       CTLFLAG_PERMANENT,
    283      1.73  knakahar 		       CTLTYPE_NODE, "opencrypto",
    284      1.73  knakahar 		       SYSCTL_DESCR("opencrypto related entries"),
    285      1.73  knakahar 		       NULL, 0, NULL, 0,
    286      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    287      1.73  knakahar 
    288      1.73  knakahar 	sysctl_createv(clog, 0, &ocnode, &retqnode,
    289      1.73  knakahar 		       CTLFLAG_PERMANENT,
    290      1.73  knakahar 		       CTLTYPE_NODE, "crypto_ret_q",
    291      1.73  knakahar 		       SYSCTL_DESCR("crypto_ret_q related entries"),
    292      1.73  knakahar 		       NULL, 0, NULL, 0,
    293      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    294      1.73  knakahar 	sysctl_createv(clog, 0, &retqnode, NULL,
    295      1.73  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    296      1.73  knakahar 		       CTLTYPE_INT, "len",
    297      1.73  knakahar 		       SYSCTL_DESCR("Current queue length"),
    298      1.73  knakahar 		       sysctl_opencrypto_q_len, 0,
    299      1.73  knakahar 		       (void *)&crypto_crp_ret_q_len, 0,
    300      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    301      1.73  knakahar 	sysctl_createv(clog, 0, &retqnode, NULL,
    302      1.73  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    303      1.73  knakahar 		       CTLTYPE_INT, "drops",
    304      1.73  knakahar 		       SYSCTL_DESCR("Crypto requests dropped due to full ret queue"),
    305      1.73  knakahar 		       sysctl_opencrypto_q_drops, 0,
    306      1.73  knakahar 		       (void *)&crypto_crp_ret_q_drops, 0,
    307      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    308      1.73  knakahar 	sysctl_createv(clog, 0, &retqnode, NULL,
    309      1.73  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    310      1.73  knakahar 		       CTLTYPE_INT, "maxlen",
    311      1.73  knakahar 		       SYSCTL_DESCR("Maximum allowed queue length"),
    312      1.73  knakahar 		       sysctl_opencrypto_q_maxlen, 0,
    313      1.73  knakahar 		       (void *)&crypto_crp_ret_q_maxlen, 0,
    314      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    315      1.73  knakahar 
    316      1.73  knakahar 	sysctl_createv(clog, 0, &ocnode, &retkqnode,
    317      1.73  knakahar 		       CTLFLAG_PERMANENT,
    318      1.73  knakahar 		       CTLTYPE_NODE, "crypto_ret_kq",
    319      1.73  knakahar 		       SYSCTL_DESCR("crypto_ret_kq related entries"),
    320      1.73  knakahar 		       NULL, 0, NULL, 0,
    321      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    322      1.73  knakahar 	sysctl_createv(clog, 0, &retkqnode, NULL,
    323      1.73  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    324      1.73  knakahar 		       CTLTYPE_INT, "len",
    325      1.73  knakahar 		       SYSCTL_DESCR("Current queue length"),
    326      1.73  knakahar 		       sysctl_opencrypto_q_len, 0,
    327      1.73  knakahar 		       (void *)&crypto_crp_ret_kq_len, 0,
    328      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    329      1.73  knakahar 	sysctl_createv(clog, 0, &retkqnode, NULL,
    330      1.73  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    331      1.73  knakahar 		       CTLTYPE_INT, "drops",
    332      1.73  knakahar 		       SYSCTL_DESCR("Crypto requests dropped due to full ret queue"),
    333      1.73  knakahar 		       sysctl_opencrypto_q_drops, 0,
    334      1.73  knakahar 		       (void *)&crypto_crp_ret_kq_drops, 0,
    335      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    336      1.73  knakahar 	sysctl_createv(clog, 0, &retkqnode, NULL,
    337      1.73  knakahar 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    338      1.73  knakahar 		       CTLTYPE_INT, "maxlen",
    339      1.73  knakahar 		       SYSCTL_DESCR("Maximum allowed queue length"),
    340      1.73  knakahar 		       sysctl_opencrypto_q_maxlen, 0,
    341      1.73  knakahar 		       (void *)&crypto_crp_ret_kq_maxlen, 0,
    342      1.73  knakahar 		       CTL_CREATE, CTL_EOL);
    343      1.13  christos }
    344       1.1  jonathan 
    345       1.1  jonathan MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
    346       1.1  jonathan 
    347       1.1  jonathan /*
    348       1.1  jonathan  * Synchronization: read carefully, this is non-trivial.
    349       1.1  jonathan  *
    350       1.1  jonathan  * Crypto requests are submitted via crypto_dispatch.  Typically
    351       1.1  jonathan  * these come in from network protocols at spl0 (output path) or
    352       1.1  jonathan  * spl[,soft]net (input path).
    353       1.1  jonathan  *
    354       1.1  jonathan  * Requests are typically passed on the driver directly, but they
    355       1.1  jonathan  * may also be queued for processing by a software interrupt thread,
    356      1.10     perry  * cryptointr, that runs at splsoftcrypto.  This thread dispatches
    357       1.1  jonathan  * the requests to crypto drivers (h/w or s/w) who call crypto_done
    358       1.1  jonathan  * when a request is complete.  Hardware crypto drivers are assumed
    359       1.1  jonathan  * to register their IRQ's as network devices so their interrupt handlers
    360       1.1  jonathan  * and subsequent "done callbacks" happen at spl[imp,net].
    361       1.1  jonathan  *
    362       1.1  jonathan  * Completed crypto ops are queued for a separate kernel thread that
    363       1.1  jonathan  * handles the callbacks at spl0.  This decoupling insures the crypto
    364       1.1  jonathan  * driver interrupt service routine is not delayed while the callback
    365       1.1  jonathan  * takes place and that callbacks are delivered after a context switch
    366       1.1  jonathan  * (as opposed to a software interrupt that clients must block).
    367       1.1  jonathan  *
    368       1.1  jonathan  * This scheme is not intended for SMP machines.
    369      1.10     perry  */
    370       1.1  jonathan static	void cryptointr(void);		/* swi thread to dispatch ops */
    371       1.1  jonathan static	void cryptoret(void);		/* kernel thread for callbacks*/
    372      1.20        ad static	struct lwp *cryptothread;
    373      1.46  pgoyette static	int crypto_destroy(bool);
    374       1.1  jonathan static	int crypto_invoke(struct cryptop *crp, int hint);
    375       1.1  jonathan static	int crypto_kinvoke(struct cryptkop *krp, int hint);
    376       1.1  jonathan 
    377  1.78.2.1       snj static struct cryptocap *crypto_checkdriver_lock(u_int32_t);
    378  1.78.2.1       snj static struct cryptocap *crypto_checkdriver_uninit(u_int32_t);
    379      1.77  knakahar static struct cryptocap *crypto_checkdriver(u_int32_t);
    380  1.78.2.1       snj static void crypto_driver_lock(struct cryptocap *);
    381  1.78.2.1       snj static void crypto_driver_unlock(struct cryptocap *);
    382  1.78.2.1       snj static void crypto_driver_clear(struct cryptocap *);
    383      1.77  knakahar 
    384       1.1  jonathan static struct cryptostats cryptostats;
    385      1.23       tls #ifdef CRYPTO_TIMING
    386       1.1  jonathan static	int crypto_timing = 0;
    387      1.23       tls #endif
    388       1.1  jonathan 
    389      1.47  christos static struct sysctllog *sysctl_opencrypto_clog;
    390      1.44  pgoyette 
    391      1.12      yamt static int
    392      1.11   thorpej crypto_init0(void)
    393       1.1  jonathan {
    394       1.1  jonathan 	int error;
    395       1.1  jonathan 
    396      1.57  knakahar 	mutex_init(&crypto_drv_mtx, MUTEX_DEFAULT, IPL_NONE);
    397  1.78.2.1       snj 	mutex_init(&crypto_q_mtx, MUTEX_DEFAULT, IPL_NONE);
    398      1.40  drochner 	mutex_init(&crypto_ret_q_mtx, MUTEX_DEFAULT, IPL_NET);
    399      1.36  pgoyette 	cv_init(&cryptoret_cv, "crypto_w");
    400      1.23       tls 	pool_init(&cryptop_pool, sizeof(struct cryptop), 0, 0,
    401      1.48   msaitoh 		  0, "cryptop", NULL, IPL_NET);
    402      1.23       tls 	pool_init(&cryptodesc_pool, sizeof(struct cryptodesc), 0, 0,
    403      1.23       tls 		  0, "cryptodesc", NULL, IPL_NET);
    404      1.23       tls 	pool_init(&cryptkop_pool, sizeof(struct cryptkop), 0, 0,
    405      1.23       tls 		  0, "cryptkop", NULL, IPL_NET);
    406       1.1  jonathan 
    407      1.11   thorpej 	crypto_drivers = malloc(CRYPTO_DRIVERS_INITIAL *
    408       1.1  jonathan 	    sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
    409       1.1  jonathan 	if (crypto_drivers == NULL) {
    410       1.1  jonathan 		printf("crypto_init: cannot malloc driver table\n");
    411      1.46  pgoyette 		return ENOMEM;
    412       1.1  jonathan 	}
    413      1.11   thorpej 	crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
    414       1.1  jonathan 
    415       1.1  jonathan 	softintr_cookie = register_swi(SWI_CRYPTO, cryptointr);
    416      1.25       tls 	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    417      1.37  christos 	    (void (*)(void *))cryptoret, NULL, &cryptothread, "cryptoret");
    418       1.1  jonathan 	if (error) {
    419       1.1  jonathan 		printf("crypto_init: cannot start cryptoret thread; error %d",
    420       1.1  jonathan 			error);
    421      1.46  pgoyette 		return crypto_destroy(false);
    422       1.1  jonathan 	}
    423      1.20        ad 
    424      1.44  pgoyette 	sysctl_opencrypto_setup(&sysctl_opencrypto_clog);
    425      1.72  knakahar 
    426      1.12      yamt 	return 0;
    427      1.11   thorpej }
    428      1.11   thorpej 
    429      1.46  pgoyette int
    430      1.11   thorpej crypto_init(void)
    431      1.11   thorpej {
    432      1.18    daniel 	static ONCE_DECL(crypto_init_once);
    433      1.11   thorpej 
    434      1.46  pgoyette 	return RUN_ONCE(&crypto_init_once, crypto_init0);
    435       1.1  jonathan }
    436       1.1  jonathan 
    437      1.46  pgoyette static int
    438      1.46  pgoyette crypto_destroy(bool exit_kthread)
    439       1.1  jonathan {
    440      1.46  pgoyette 	int i;
    441      1.46  pgoyette 
    442      1.46  pgoyette 	if (exit_kthread) {
    443      1.77  knakahar 		struct cryptocap *cap = NULL;
    444      1.77  knakahar 
    445      1.46  pgoyette 		/* if we have any in-progress requests, don't unload */
    446  1.78.2.1       snj 		mutex_enter(&crypto_q_mtx);
    447      1.51  knakahar 		if (!TAILQ_EMPTY(&crp_q) || !TAILQ_EMPTY(&crp_kq)) {
    448  1.78.2.1       snj 			mutex_exit(&crypto_q_mtx);
    449      1.46  pgoyette 			return EBUSY;
    450      1.51  knakahar 		}
    451  1.78.2.1       snj 		mutex_exit(&crypto_q_mtx);
    452  1.78.2.1       snj 		/* FIXME:
    453  1.78.2.1       snj 		 * prohibit enqueue to crp_q and crp_kq after here.
    454  1.78.2.1       snj 		 */
    455      1.46  pgoyette 
    456  1.78.2.1       snj 		mutex_enter(&crypto_drv_mtx);
    457      1.77  knakahar 		for (i = 0; i < crypto_drivers_num; i++) {
    458      1.77  knakahar 			cap = crypto_checkdriver(i);
    459      1.77  knakahar 			if (cap == NULL)
    460      1.77  knakahar 				continue;
    461  1.78.2.1       snj 			if (cap->cc_sessions != 0) {
    462  1.78.2.1       snj 				mutex_exit(&crypto_drv_mtx);
    463  1.78.2.1       snj 				return EBUSY;
    464  1.78.2.1       snj 			}
    465      1.51  knakahar 		}
    466  1.78.2.1       snj 		mutex_exit(&crypto_drv_mtx);
    467  1.78.2.1       snj 		/* FIXME:
    468  1.78.2.1       snj 		 * prohibit touch crypto_drivers[] and each element after here.
    469  1.78.2.1       snj 		 */
    470      1.46  pgoyette 
    471  1.78.2.1       snj 		mutex_spin_enter(&crypto_ret_q_mtx);
    472      1.46  pgoyette 		/* kick the cryptoret thread and wait for it to exit */
    473      1.46  pgoyette 		crypto_exit_flag = 1;
    474      1.46  pgoyette 		cv_signal(&cryptoret_cv);
    475      1.46  pgoyette 
    476      1.46  pgoyette 		while (crypto_exit_flag != 0)
    477      1.46  pgoyette 			cv_wait(&cryptoret_cv, &crypto_ret_q_mtx);
    478      1.46  pgoyette 		mutex_spin_exit(&crypto_ret_q_mtx);
    479      1.46  pgoyette 	}
    480      1.46  pgoyette 
    481      1.46  pgoyette 	if (sysctl_opencrypto_clog != NULL)
    482      1.46  pgoyette 		sysctl_teardown(&sysctl_opencrypto_clog);
    483      1.46  pgoyette 
    484      1.46  pgoyette 	unregister_swi(SWI_CRYPTO, cryptointr);
    485      1.46  pgoyette 
    486      1.57  knakahar 	mutex_enter(&crypto_drv_mtx);
    487       1.1  jonathan 	if (crypto_drivers != NULL)
    488       1.1  jonathan 		free(crypto_drivers, M_CRYPTO_DATA);
    489      1.57  knakahar 	mutex_exit(&crypto_drv_mtx);
    490      1.46  pgoyette 
    491      1.46  pgoyette 	pool_destroy(&cryptop_pool);
    492      1.46  pgoyette 	pool_destroy(&cryptodesc_pool);
    493      1.46  pgoyette 	pool_destroy(&cryptkop_pool);
    494      1.46  pgoyette 
    495      1.46  pgoyette 	cv_destroy(&cryptoret_cv);
    496      1.46  pgoyette 
    497      1.46  pgoyette 	mutex_destroy(&crypto_ret_q_mtx);
    498      1.46  pgoyette 	mutex_destroy(&crypto_q_mtx);
    499      1.57  knakahar 	mutex_destroy(&crypto_drv_mtx);
    500      1.46  pgoyette 
    501      1.46  pgoyette 	return 0;
    502       1.1  jonathan }
    503       1.1  jonathan 
    504       1.1  jonathan /*
    505      1.57  knakahar  * Create a new session.
    506       1.1  jonathan  */
    507       1.1  jonathan int
    508       1.1  jonathan crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
    509       1.1  jonathan {
    510       1.1  jonathan 	struct cryptoini *cr;
    511      1.77  knakahar 	struct cryptocap *cap;
    512       1.1  jonathan 	u_int32_t hid, lid;
    513       1.1  jonathan 	int err = EINVAL;
    514       1.1  jonathan 
    515      1.57  knakahar 	mutex_enter(&crypto_drv_mtx);
    516       1.1  jonathan 
    517       1.1  jonathan 	/*
    518       1.1  jonathan 	 * The algorithm we use here is pretty stupid; just use the
    519       1.1  jonathan 	 * first driver that supports all the algorithms we need.
    520       1.1  jonathan 	 *
    521       1.1  jonathan 	 * XXX We need more smarts here (in real life too, but that's
    522       1.1  jonathan 	 * XXX another story altogether).
    523       1.1  jonathan 	 */
    524       1.1  jonathan 
    525       1.1  jonathan 	for (hid = 0; hid < crypto_drivers_num; hid++) {
    526      1.77  knakahar 		cap = crypto_checkdriver(hid);
    527      1.77  knakahar 		if (cap == NULL)
    528      1.77  knakahar 			continue;
    529      1.77  knakahar 
    530  1.78.2.1       snj 		crypto_driver_lock(cap);
    531  1.78.2.1       snj 
    532       1.1  jonathan 		/*
    533       1.1  jonathan 		 * If it's not initialized or has remaining sessions
    534       1.1  jonathan 		 * referencing it, skip.
    535       1.1  jonathan 		 */
    536      1.77  knakahar 		if (cap->cc_newsession == NULL ||
    537  1.78.2.1       snj 		    (cap->cc_flags & CRYPTOCAP_F_CLEANUP)) {
    538  1.78.2.1       snj 			crypto_driver_unlock(cap);
    539       1.1  jonathan 			continue;
    540  1.78.2.1       snj 		}
    541       1.1  jonathan 
    542       1.1  jonathan 		/* Hardware required -- ignore software drivers. */
    543  1.78.2.1       snj 		if (hard > 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE)) {
    544  1.78.2.1       snj 			crypto_driver_unlock(cap);
    545       1.1  jonathan 			continue;
    546  1.78.2.1       snj 		}
    547       1.1  jonathan 		/* Software required -- ignore hardware drivers. */
    548  1.78.2.1       snj 		if (hard < 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE) == 0) {
    549  1.78.2.1       snj 			crypto_driver_unlock(cap);
    550       1.1  jonathan 			continue;
    551  1.78.2.1       snj 		}
    552       1.1  jonathan 
    553       1.1  jonathan 		/* See if all the algorithms are supported. */
    554       1.1  jonathan 		for (cr = cri; cr; cr = cr->cri_next)
    555      1.77  knakahar 			if (cap->cc_alg[cr->cri_alg] == 0) {
    556      1.64  knakahar 				DPRINTF("alg %d not supported\n", cr->cri_alg);
    557       1.1  jonathan 				break;
    558      1.33    darran 			}
    559       1.1  jonathan 
    560       1.1  jonathan 		if (cr == NULL) {
    561       1.1  jonathan 			/* Ok, all algorithms are supported. */
    562       1.1  jonathan 
    563       1.1  jonathan 			/*
    564       1.1  jonathan 			 * Can't do everything in one session.
    565       1.1  jonathan 			 *
    566       1.1  jonathan 			 * XXX Fix this. We need to inject a "virtual" session layer right
    567       1.1  jonathan 			 * XXX about here.
    568       1.1  jonathan 			 */
    569       1.1  jonathan 
    570       1.1  jonathan 			/* Call the driver initialization routine. */
    571       1.1  jonathan 			lid = hid;		/* Pass the driver ID. */
    572      1.77  knakahar 			err = cap->cc_newsession(cap->cc_arg, &lid, cri);
    573       1.1  jonathan 			if (err == 0) {
    574       1.1  jonathan 				(*sid) = hid;
    575       1.1  jonathan 				(*sid) <<= 32;
    576       1.1  jonathan 				(*sid) |= (lid & 0xffffffff);
    577      1.77  knakahar 				(cap->cc_sessions)++;
    578      1.52  knakahar 			} else {
    579      1.64  knakahar 				DPRINTF("crypto_drivers[%d].cc_newsession() failed. error=%d\n",
    580      1.64  knakahar 					hid, err);
    581       1.1  jonathan 			}
    582  1.78.2.1       snj 			crypto_driver_unlock(cap);
    583       1.1  jonathan 			goto done;
    584       1.1  jonathan 			/*break;*/
    585       1.1  jonathan 		}
    586  1.78.2.1       snj 
    587  1.78.2.1       snj 		crypto_driver_unlock(cap);
    588       1.1  jonathan 	}
    589       1.1  jonathan done:
    590      1.57  knakahar 	mutex_exit(&crypto_drv_mtx);
    591       1.1  jonathan 	return err;
    592       1.1  jonathan }
    593       1.1  jonathan 
    594       1.1  jonathan /*
    595       1.1  jonathan  * Delete an existing session (or a reserved session on an unregistered
    596      1.57  knakahar  * driver).
    597       1.1  jonathan  */
    598       1.1  jonathan int
    599       1.1  jonathan crypto_freesession(u_int64_t sid)
    600       1.1  jonathan {
    601      1.77  knakahar 	struct cryptocap *cap;
    602       1.1  jonathan 	int err = 0;
    603       1.1  jonathan 
    604       1.1  jonathan 	/* Determine two IDs. */
    605  1.78.2.1       snj 	cap = crypto_checkdriver_lock(CRYPTO_SESID2HID(sid));
    606  1.78.2.1       snj 	if (cap == NULL)
    607  1.78.2.1       snj 		return ENOENT;
    608       1.1  jonathan 
    609      1.77  knakahar 	if (cap->cc_sessions)
    610      1.77  knakahar 		(cap->cc_sessions)--;
    611       1.1  jonathan 
    612       1.1  jonathan 	/* Call the driver cleanup routine, if available. */
    613      1.77  knakahar 	if (cap->cc_freesession)
    614      1.77  knakahar 		err = cap->cc_freesession(cap->cc_arg, sid);
    615       1.1  jonathan 	else
    616       1.1  jonathan 		err = 0;
    617       1.1  jonathan 
    618       1.1  jonathan 	/*
    619       1.1  jonathan 	 * If this was the last session of a driver marked as invalid,
    620       1.1  jonathan 	 * make the entry available for reuse.
    621       1.1  jonathan 	 */
    622      1.77  knakahar 	if ((cap->cc_flags & CRYPTOCAP_F_CLEANUP) && cap->cc_sessions == 0)
    623  1.78.2.1       snj 		crypto_driver_clear(cap);
    624       1.1  jonathan 
    625  1.78.2.1       snj 	crypto_driver_unlock(cap);
    626       1.1  jonathan 	return err;
    627       1.1  jonathan }
    628       1.1  jonathan 
    629  1.78.2.1       snj static bool
    630  1.78.2.1       snj crypto_checkdriver_initialized(const struct cryptocap *cap)
    631  1.78.2.1       snj {
    632  1.78.2.1       snj 
    633  1.78.2.1       snj 	return cap->cc_process != NULL ||
    634  1.78.2.1       snj 	    (cap->cc_flags & CRYPTOCAP_F_CLEANUP) != 0 ||
    635  1.78.2.1       snj 	    cap->cc_sessions != 0;
    636  1.78.2.1       snj }
    637  1.78.2.1       snj 
    638       1.1  jonathan /*
    639       1.1  jonathan  * Return an unused driver id.  Used by drivers prior to registering
    640       1.1  jonathan  * support for the algorithms they handle.
    641       1.1  jonathan  */
    642       1.1  jonathan int32_t
    643       1.1  jonathan crypto_get_driverid(u_int32_t flags)
    644       1.1  jonathan {
    645       1.1  jonathan 	struct cryptocap *newdrv;
    646      1.77  knakahar 	struct cryptocap *cap = NULL;
    647      1.23       tls 	int i;
    648       1.1  jonathan 
    649      1.46  pgoyette 	(void)crypto_init();		/* XXX oh, this is foul! */
    650      1.11   thorpej 
    651      1.57  knakahar 	mutex_enter(&crypto_drv_mtx);
    652      1.77  knakahar 	for (i = 0; i < crypto_drivers_num; i++) {
    653  1.78.2.1       snj 		cap = crypto_checkdriver_uninit(i);
    654  1.78.2.1       snj 		if (cap == NULL || crypto_checkdriver_initialized(cap))
    655      1.77  knakahar 			continue;
    656  1.78.2.1       snj 		break;
    657      1.77  knakahar 	}
    658       1.1  jonathan 
    659       1.1  jonathan 	/* Out of entries, allocate some more. */
    660      1.77  knakahar 	if (cap == NULL) {
    661       1.1  jonathan 		/* Be careful about wrap-around. */
    662       1.1  jonathan 		if (2 * crypto_drivers_num <= crypto_drivers_num) {
    663      1.57  knakahar 			mutex_exit(&crypto_drv_mtx);
    664       1.1  jonathan 			printf("crypto: driver count wraparound!\n");
    665       1.1  jonathan 			return -1;
    666       1.1  jonathan 		}
    667       1.1  jonathan 
    668       1.1  jonathan 		newdrv = malloc(2 * crypto_drivers_num *
    669       1.1  jonathan 		    sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
    670       1.1  jonathan 		if (newdrv == NULL) {
    671      1.57  knakahar 			mutex_exit(&crypto_drv_mtx);
    672       1.1  jonathan 			printf("crypto: no space to expand driver table!\n");
    673       1.1  jonathan 			return -1;
    674       1.1  jonathan 		}
    675       1.1  jonathan 
    676      1.34   tsutsui 		memcpy(newdrv, crypto_drivers,
    677       1.1  jonathan 		    crypto_drivers_num * sizeof(struct cryptocap));
    678       1.1  jonathan 
    679       1.1  jonathan 		crypto_drivers_num *= 2;
    680       1.1  jonathan 
    681       1.1  jonathan 		free(crypto_drivers, M_CRYPTO_DATA);
    682       1.1  jonathan 		crypto_drivers = newdrv;
    683      1.77  knakahar 
    684  1.78.2.1       snj 		cap = crypto_checkdriver_uninit(i);
    685      1.77  knakahar 		KASSERT(cap != NULL);
    686       1.1  jonathan 	}
    687       1.1  jonathan 
    688       1.1  jonathan 	/* NB: state is zero'd on free */
    689      1.77  knakahar 	cap->cc_sessions = 1;	/* Mark */
    690      1.77  knakahar 	cap->cc_flags = flags;
    691  1.78.2.1       snj 	mutex_init(&cap->cc_lock, MUTEX_DEFAULT, IPL_NET);
    692       1.1  jonathan 
    693       1.1  jonathan 	if (bootverbose)
    694       1.1  jonathan 		printf("crypto: assign driver %u, flags %u\n", i, flags);
    695       1.1  jonathan 
    696      1.57  knakahar 	mutex_exit(&crypto_drv_mtx);
    697       1.1  jonathan 
    698       1.1  jonathan 	return i;
    699       1.1  jonathan }
    700       1.1  jonathan 
    701       1.1  jonathan static struct cryptocap *
    702  1.78.2.1       snj crypto_checkdriver_lock(u_int32_t hid)
    703       1.1  jonathan {
    704  1.78.2.1       snj 	struct cryptocap *cap;
    705  1.78.2.1       snj 
    706  1.78.2.1       snj 	KASSERT(crypto_drivers != NULL);
    707  1.78.2.1       snj 
    708  1.78.2.1       snj 	if (hid >= crypto_drivers_num)
    709  1.78.2.1       snj 		return NULL;
    710  1.78.2.1       snj 
    711  1.78.2.1       snj 	cap = &crypto_drivers[hid];
    712  1.78.2.1       snj 	mutex_enter(&cap->cc_lock);
    713  1.78.2.1       snj 	return cap;
    714  1.78.2.1       snj }
    715  1.78.2.1       snj 
    716  1.78.2.1       snj /*
    717  1.78.2.1       snj  * Use crypto_checkdriver_uninit() instead of crypto_checkdriver() below two
    718  1.78.2.1       snj  * situations
    719  1.78.2.1       snj  *     - crypto_drivers[] may not be allocated
    720  1.78.2.1       snj  *     - crypto_drivers[hid] may not be initialized
    721  1.78.2.1       snj  */
    722  1.78.2.1       snj static struct cryptocap *
    723  1.78.2.1       snj crypto_checkdriver_uninit(u_int32_t hid)
    724  1.78.2.1       snj {
    725  1.78.2.1       snj 
    726  1.78.2.1       snj 	KASSERT(mutex_owned(&crypto_drv_mtx));
    727  1.78.2.1       snj 
    728       1.1  jonathan 	if (crypto_drivers == NULL)
    729       1.1  jonathan 		return NULL;
    730  1.78.2.1       snj 
    731       1.1  jonathan 	return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
    732       1.1  jonathan }
    733       1.1  jonathan 
    734       1.1  jonathan /*
    735  1.78.2.1       snj  * Use crypto_checkdriver_uninit() instead of crypto_checkdriver() below two
    736  1.78.2.1       snj  * situations
    737  1.78.2.1       snj  *     - crypto_drivers[] may not be allocated
    738  1.78.2.1       snj  *     - crypto_drivers[hid] may not be initialized
    739  1.78.2.1       snj  */
    740  1.78.2.1       snj static struct cryptocap *
    741  1.78.2.1       snj crypto_checkdriver(u_int32_t hid)
    742  1.78.2.1       snj {
    743  1.78.2.1       snj 
    744  1.78.2.1       snj 	KASSERT(mutex_owned(&crypto_drv_mtx));
    745  1.78.2.1       snj 
    746  1.78.2.1       snj 	if (crypto_drivers == NULL || hid >= crypto_drivers_num)
    747  1.78.2.1       snj 		return NULL;
    748  1.78.2.1       snj 
    749  1.78.2.1       snj 	struct cryptocap *cap = &crypto_drivers[hid];
    750  1.78.2.1       snj 	return crypto_checkdriver_initialized(cap) ? cap : NULL;
    751  1.78.2.1       snj }
    752  1.78.2.1       snj 
    753  1.78.2.1       snj static inline void
    754  1.78.2.1       snj crypto_driver_lock(struct cryptocap *cap)
    755  1.78.2.1       snj {
    756  1.78.2.1       snj 
    757  1.78.2.1       snj 	KASSERT(cap != NULL);
    758  1.78.2.1       snj 
    759  1.78.2.1       snj 	mutex_enter(&cap->cc_lock);
    760  1.78.2.1       snj }
    761  1.78.2.1       snj 
    762  1.78.2.1       snj static inline void
    763  1.78.2.1       snj crypto_driver_unlock(struct cryptocap *cap)
    764  1.78.2.1       snj {
    765  1.78.2.1       snj 
    766  1.78.2.1       snj 	KASSERT(cap != NULL);
    767  1.78.2.1       snj 
    768  1.78.2.1       snj 	mutex_exit(&cap->cc_lock);
    769  1.78.2.1       snj }
    770  1.78.2.1       snj 
    771  1.78.2.1       snj static void
    772  1.78.2.1       snj crypto_driver_clear(struct cryptocap *cap)
    773  1.78.2.1       snj {
    774  1.78.2.1       snj 
    775  1.78.2.1       snj 	if (cap == NULL)
    776  1.78.2.1       snj 		return;
    777  1.78.2.1       snj 
    778  1.78.2.1       snj 	KASSERT(mutex_owned(&cap->cc_lock));
    779  1.78.2.1       snj 
    780  1.78.2.1       snj 	cap->cc_sessions = 0;
    781  1.78.2.1       snj 	memset(&cap->cc_max_op_len, 0, sizeof(cap->cc_max_op_len));
    782  1.78.2.1       snj 	memset(&cap->cc_alg, 0, sizeof(cap->cc_alg));
    783  1.78.2.1       snj 	memset(&cap->cc_kalg, 0, sizeof(cap->cc_kalg));
    784  1.78.2.1       snj 	cap->cc_flags = 0;
    785  1.78.2.1       snj 	cap->cc_qblocked = 0;
    786  1.78.2.1       snj 	cap->cc_kqblocked = 0;
    787  1.78.2.1       snj 
    788  1.78.2.1       snj 	cap->cc_arg = NULL;
    789  1.78.2.1       snj 	cap->cc_newsession = NULL;
    790  1.78.2.1       snj 	cap->cc_process = NULL;
    791  1.78.2.1       snj 	cap->cc_freesession = NULL;
    792  1.78.2.1       snj 	cap->cc_kprocess = NULL;
    793  1.78.2.1       snj }
    794  1.78.2.1       snj 
    795  1.78.2.1       snj /*
    796       1.1  jonathan  * Register support for a key-related algorithm.  This routine
    797       1.1  jonathan  * is called once for each algorithm supported a driver.
    798       1.1  jonathan  */
    799       1.1  jonathan int
    800       1.1  jonathan crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags,
    801      1.37  christos     int (*kprocess)(void *, struct cryptkop *, int),
    802       1.1  jonathan     void *karg)
    803       1.1  jonathan {
    804       1.1  jonathan 	struct cryptocap *cap;
    805       1.1  jonathan 	int err;
    806       1.1  jonathan 
    807      1.57  knakahar 	mutex_enter(&crypto_drv_mtx);
    808       1.1  jonathan 
    809  1.78.2.1       snj 	cap = crypto_checkdriver_lock(driverid);
    810       1.1  jonathan 	if (cap != NULL &&
    811       1.1  jonathan 	    (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
    812       1.1  jonathan 		/*
    813       1.1  jonathan 		 * XXX Do some performance testing to determine placing.
    814       1.1  jonathan 		 * XXX We probably need an auxiliary data structure that
    815       1.1  jonathan 		 * XXX describes relative performances.
    816       1.1  jonathan 		 */
    817       1.1  jonathan 
    818       1.1  jonathan 		cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
    819      1.23       tls 		if (bootverbose) {
    820      1.23       tls 			printf("crypto: driver %u registers key alg %u "
    821      1.23       tls 			       " flags %u\n",
    822      1.23       tls 				driverid,
    823      1.23       tls 				kalg,
    824      1.23       tls 				flags
    825       1.1  jonathan 			);
    826      1.23       tls 		}
    827       1.1  jonathan 
    828       1.1  jonathan 		if (cap->cc_kprocess == NULL) {
    829       1.1  jonathan 			cap->cc_karg = karg;
    830       1.1  jonathan 			cap->cc_kprocess = kprocess;
    831       1.1  jonathan 		}
    832       1.1  jonathan 		err = 0;
    833       1.1  jonathan 	} else
    834       1.1  jonathan 		err = EINVAL;
    835       1.1  jonathan 
    836      1.57  knakahar 	mutex_exit(&crypto_drv_mtx);
    837       1.1  jonathan 	return err;
    838       1.1  jonathan }
    839       1.1  jonathan 
    840       1.1  jonathan /*
    841       1.1  jonathan  * Register support for a non-key-related algorithm.  This routine
    842       1.1  jonathan  * is called once for each such algorithm supported by a driver.
    843       1.1  jonathan  */
    844       1.1  jonathan int
    845       1.1  jonathan crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
    846       1.1  jonathan     u_int32_t flags,
    847      1.37  christos     int (*newses)(void *, u_int32_t*, struct cryptoini*),
    848      1.37  christos     int (*freeses)(void *, u_int64_t),
    849      1.37  christos     int (*process)(void *, struct cryptop *, int),
    850       1.1  jonathan     void *arg)
    851       1.1  jonathan {
    852       1.1  jonathan 	struct cryptocap *cap;
    853      1.23       tls 	int err;
    854       1.1  jonathan 
    855  1.78.2.1       snj 	cap = crypto_checkdriver_lock(driverid);
    856  1.78.2.1       snj 	if (cap == NULL)
    857  1.78.2.1       snj 		return EINVAL;
    858       1.1  jonathan 
    859       1.1  jonathan 	/* NB: algorithms are in the range [1..max] */
    860  1.78.2.1       snj 	if (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) {
    861       1.1  jonathan 		/*
    862       1.1  jonathan 		 * XXX Do some performance testing to determine placing.
    863       1.1  jonathan 		 * XXX We probably need an auxiliary data structure that
    864       1.1  jonathan 		 * XXX describes relative performances.
    865       1.1  jonathan 		 */
    866       1.1  jonathan 
    867       1.1  jonathan 		cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
    868       1.1  jonathan 		cap->cc_max_op_len[alg] = maxoplen;
    869      1.23       tls 		if (bootverbose) {
    870      1.23       tls 			printf("crypto: driver %u registers alg %u "
    871      1.23       tls 				"flags %u maxoplen %u\n",
    872      1.23       tls 				driverid,
    873      1.23       tls 				alg,
    874      1.23       tls 				flags,
    875      1.23       tls 				maxoplen
    876       1.1  jonathan 			);
    877      1.23       tls 		}
    878       1.1  jonathan 
    879       1.1  jonathan 		if (cap->cc_process == NULL) {
    880       1.1  jonathan 			cap->cc_arg = arg;
    881       1.1  jonathan 			cap->cc_newsession = newses;
    882       1.1  jonathan 			cap->cc_process = process;
    883       1.1  jonathan 			cap->cc_freesession = freeses;
    884       1.1  jonathan 			cap->cc_sessions = 0;		/* Unmark */
    885       1.1  jonathan 		}
    886       1.1  jonathan 		err = 0;
    887       1.1  jonathan 	} else
    888       1.1  jonathan 		err = EINVAL;
    889       1.1  jonathan 
    890  1.78.2.1       snj 	crypto_driver_unlock(cap);
    891  1.78.2.1       snj 
    892       1.1  jonathan 	return err;
    893       1.1  jonathan }
    894       1.1  jonathan 
    895      1.61  knakahar static int
    896  1.78.2.1       snj crypto_unregister_locked(struct cryptocap *cap, int alg, bool all)
    897      1.61  knakahar {
    898      1.61  knakahar 	int i;
    899      1.61  knakahar 	u_int32_t ses;
    900      1.61  knakahar 	bool lastalg = true;
    901      1.61  knakahar 
    902  1.78.2.1       snj 	KASSERT(cap != NULL);
    903  1.78.2.1       snj 	KASSERT(mutex_owned(&cap->cc_lock));
    904      1.61  knakahar 
    905      1.78  knakahar 	if (alg < CRYPTO_ALGORITHM_MIN || CRYPTO_ALGORITHM_MAX < alg)
    906      1.61  knakahar 		return EINVAL;
    907      1.61  knakahar 
    908  1.78.2.1       snj 	if (!all && cap->cc_alg[alg] == 0)
    909      1.61  knakahar 		return EINVAL;
    910      1.61  knakahar 
    911      1.61  knakahar 	cap->cc_alg[alg] = 0;
    912      1.61  knakahar 	cap->cc_max_op_len[alg] = 0;
    913      1.61  knakahar 
    914      1.62  knakahar 	if (all) {
    915      1.62  knakahar 		if (alg != CRYPTO_ALGORITHM_MAX)
    916      1.61  knakahar 			lastalg = false;
    917      1.62  knakahar 	} else {
    918      1.62  knakahar 		/* Was this the last algorithm ? */
    919      1.62  knakahar 		for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++)
    920      1.62  knakahar 			if (cap->cc_alg[i] != 0) {
    921      1.62  knakahar 				lastalg = false;
    922      1.62  knakahar 				break;
    923      1.62  knakahar 			}
    924      1.62  knakahar 	}
    925      1.61  knakahar 	if (lastalg) {
    926      1.61  knakahar 		ses = cap->cc_sessions;
    927  1.78.2.1       snj 		crypto_driver_clear(cap);
    928      1.61  knakahar 		if (ses != 0) {
    929      1.61  knakahar 			/*
    930      1.61  knakahar 			 * If there are pending sessions, just mark as invalid.
    931      1.61  knakahar 			 */
    932      1.61  knakahar 			cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
    933      1.61  knakahar 			cap->cc_sessions = ses;
    934      1.61  knakahar 		}
    935      1.61  knakahar 	}
    936      1.61  knakahar 
    937      1.61  knakahar 	return 0;
    938      1.61  knakahar }
    939      1.61  knakahar 
    940       1.1  jonathan /*
    941       1.1  jonathan  * Unregister a crypto driver. If there are pending sessions using it,
    942       1.1  jonathan  * leave enough information around so that subsequent calls using those
    943       1.1  jonathan  * sessions will correctly detect the driver has been unregistered and
    944       1.1  jonathan  * reroute requests.
    945       1.1  jonathan  */
    946       1.1  jonathan int
    947       1.1  jonathan crypto_unregister(u_int32_t driverid, int alg)
    948       1.1  jonathan {
    949      1.61  knakahar 	int err;
    950  1.78.2.1       snj 	struct cryptocap *cap;
    951       1.1  jonathan 
    952  1.78.2.1       snj 	cap = crypto_checkdriver_lock(driverid);
    953  1.78.2.1       snj 	err = crypto_unregister_locked(cap, alg, false);
    954  1.78.2.1       snj 	crypto_driver_unlock(cap);
    955       1.1  jonathan 
    956       1.1  jonathan 	return err;
    957       1.1  jonathan }
    958       1.1  jonathan 
    959       1.1  jonathan /*
    960       1.1  jonathan  * Unregister all algorithms associated with a crypto driver.
    961       1.1  jonathan  * If there are pending sessions using it, leave enough information
    962       1.1  jonathan  * around so that subsequent calls using those sessions will
    963       1.1  jonathan  * correctly detect the driver has been unregistered and reroute
    964       1.1  jonathan  * requests.
    965       1.1  jonathan  */
    966       1.1  jonathan int
    967       1.1  jonathan crypto_unregister_all(u_int32_t driverid)
    968       1.1  jonathan {
    969      1.62  knakahar 	int err, i;
    970  1.78.2.1       snj 	struct cryptocap *cap;
    971       1.1  jonathan 
    972  1.78.2.1       snj 	cap = crypto_checkdriver_lock(driverid);
    973      1.62  knakahar 	for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
    974  1.78.2.1       snj 		err = crypto_unregister_locked(cap, i, true);
    975      1.62  knakahar 		if (err)
    976      1.62  knakahar 			break;
    977      1.62  knakahar 	}
    978  1.78.2.1       snj 	crypto_driver_unlock(cap);
    979       1.1  jonathan 
    980       1.1  jonathan 	return err;
    981       1.1  jonathan }
    982       1.1  jonathan 
    983       1.1  jonathan /*
    984       1.1  jonathan  * Clear blockage on a driver.  The what parameter indicates whether
    985       1.1  jonathan  * the driver is now ready for cryptop's and/or cryptokop's.
    986       1.1  jonathan  */
    987       1.1  jonathan int
    988       1.1  jonathan crypto_unblock(u_int32_t driverid, int what)
    989       1.1  jonathan {
    990       1.1  jonathan 	struct cryptocap *cap;
    991      1.55  knakahar 	int needwakeup = 0;
    992       1.1  jonathan 
    993  1.78.2.1       snj 	cap = crypto_checkdriver_lock(driverid);
    994  1.78.2.1       snj 	if (cap == NULL)
    995      1.55  knakahar 		return EINVAL;
    996      1.55  knakahar 
    997      1.55  knakahar 	if (what & CRYPTO_SYMQ) {
    998      1.55  knakahar 		needwakeup |= cap->cc_qblocked;
    999      1.55  knakahar 		cap->cc_qblocked = 0;
   1000      1.55  knakahar 	}
   1001      1.55  knakahar 	if (what & CRYPTO_ASYMQ) {
   1002      1.55  knakahar 		needwakeup |= cap->cc_kqblocked;
   1003      1.55  knakahar 		cap->cc_kqblocked = 0;
   1004      1.24       tls 	}
   1005  1.78.2.1       snj 	crypto_driver_unlock(cap);
   1006      1.55  knakahar 	if (needwakeup)
   1007      1.55  knakahar 		setsoftcrypto(softintr_cookie);
   1008       1.1  jonathan 
   1009      1.55  knakahar 	return 0;
   1010       1.1  jonathan }
   1011       1.1  jonathan 
   1012       1.1  jonathan /*
   1013       1.1  jonathan  * Dispatch a crypto request to a driver or queue
   1014       1.1  jonathan  * it, to be processed by the kernel thread.
   1015       1.1  jonathan  */
   1016       1.1  jonathan int
   1017       1.1  jonathan crypto_dispatch(struct cryptop *crp)
   1018       1.1  jonathan {
   1019      1.23       tls 	int result;
   1020      1.65  knakahar 	struct cryptocap *cap;
   1021       1.1  jonathan 
   1022      1.59  knakahar 	KASSERT(crp != NULL);
   1023      1.59  knakahar 
   1024      1.64  knakahar 	DPRINTF("crp %p, alg %d\n", crp, crp->crp_desc->crd_alg);
   1025       1.1  jonathan 
   1026       1.1  jonathan 	cryptostats.cs_ops++;
   1027       1.1  jonathan 
   1028       1.1  jonathan #ifdef CRYPTO_TIMING
   1029       1.1  jonathan 	if (crypto_timing)
   1030       1.1  jonathan 		nanouptime(&crp->crp_tstamp);
   1031       1.1  jonathan #endif
   1032      1.58  knakahar 
   1033      1.65  knakahar 	if ((crp->crp_flags & CRYPTO_F_BATCH) != 0) {
   1034  1.78.2.1       snj 		int wasempty;
   1035       1.1  jonathan 		/*
   1036       1.1  jonathan 		 * Caller marked the request as ``ok to delay'';
   1037       1.1  jonathan 		 * queue it for the swi thread.  This is desirable
   1038       1.1  jonathan 		 * when the operation is low priority and/or suitable
   1039       1.1  jonathan 		 * for batching.
   1040  1.78.2.1       snj 		 *
   1041  1.78.2.1       snj 		 * don't care list order in batch job.
   1042       1.1  jonathan 		 */
   1043  1.78.2.1       snj 		mutex_enter(&crypto_q_mtx);
   1044  1.78.2.1       snj 		wasempty  = TAILQ_EMPTY(&crp_q);
   1045       1.1  jonathan 		TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
   1046  1.78.2.1       snj 		mutex_exit(&crypto_q_mtx);
   1047      1.65  knakahar 		if (wasempty)
   1048       1.1  jonathan 			setsoftcrypto(softintr_cookie);
   1049      1.65  knakahar 
   1050      1.65  knakahar 		return 0;
   1051      1.65  knakahar 	}
   1052      1.65  knakahar 
   1053  1.78.2.1       snj 	mutex_enter(&crypto_q_mtx);
   1054  1.78.2.1       snj 	cap = crypto_checkdriver_lock(CRYPTO_SESID2HID(crp->crp_sid));
   1055      1.66  knakahar 	/*
   1056      1.66  knakahar 	 * TODO:
   1057      1.66  knakahar 	 * If we can ensure the driver has been valid until the driver is
   1058      1.66  knakahar 	 * done crypto_unregister(), this migrate operation is not required.
   1059      1.66  knakahar 	 */
   1060      1.66  knakahar 	if (cap == NULL) {
   1061      1.66  knakahar 		/*
   1062      1.66  knakahar 		 * The driver must be detached, so this request will migrate
   1063      1.66  knakahar 		 * to other drivers in cryptointr() later.
   1064      1.66  knakahar 		 */
   1065      1.66  knakahar 		TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
   1066  1.78.2.1       snj 		mutex_exit(&crypto_q_mtx);
   1067      1.66  knakahar 		return 0;
   1068      1.66  knakahar 	}
   1069      1.66  knakahar 
   1070      1.67  knakahar 	if (cap->cc_qblocked != 0) {
   1071  1.78.2.1       snj 		crypto_driver_unlock(cap);
   1072      1.67  knakahar 		/*
   1073      1.67  knakahar 		 * The driver is blocked, just queue the op until
   1074      1.67  knakahar 		 * it unblocks and the swi thread gets kicked.
   1075      1.67  knakahar 		 */
   1076      1.67  knakahar 		TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
   1077  1.78.2.1       snj 		mutex_exit(&crypto_q_mtx);
   1078      1.67  knakahar 		return 0;
   1079      1.67  knakahar 	}
   1080      1.67  knakahar 
   1081      1.67  knakahar 	/*
   1082      1.65  knakahar 	 * Caller marked the request to be processed
   1083      1.65  knakahar 	 * immediately; dispatch it directly to the
   1084      1.65  knakahar 	 * driver unless the driver is currently blocked.
   1085      1.65  knakahar 	 */
   1086  1.78.2.1       snj 	crypto_driver_unlock(cap);
   1087      1.67  knakahar 	result = crypto_invoke(crp, 0);
   1088      1.67  knakahar 	if (result == ERESTART) {
   1089      1.67  knakahar 		/*
   1090      1.67  knakahar 		 * The driver ran out of resources, mark the
   1091      1.67  knakahar 		 * driver ``blocked'' for cryptop's and put
   1092      1.67  knakahar 		 * the op on the queue.
   1093      1.67  knakahar 		 */
   1094  1.78.2.1       snj 		crypto_driver_lock(cap);
   1095      1.77  knakahar 		cap->cc_qblocked = 1;
   1096  1.78.2.1       snj 		crypto_driver_unlock(cap);
   1097      1.67  knakahar 		TAILQ_INSERT_HEAD(&crp_q, crp, crp_next);
   1098      1.67  knakahar 		cryptostats.cs_blocks++;
   1099      1.65  knakahar 
   1100      1.65  knakahar 		/*
   1101      1.67  knakahar 		 * The crp is enqueued to crp_q, that is,
   1102      1.67  knakahar 		 * no error occurs. So, this function should
   1103      1.67  knakahar 		 * not return error.
   1104      1.65  knakahar 		 */
   1105       1.1  jonathan 		result = 0;
   1106       1.1  jonathan 	}
   1107       1.1  jonathan 
   1108  1.78.2.1       snj 	mutex_exit(&crypto_q_mtx);
   1109       1.1  jonathan 	return result;
   1110       1.1  jonathan }
   1111       1.1  jonathan 
   1112       1.1  jonathan /*
   1113       1.1  jonathan  * Add an asymetric crypto request to a queue,
   1114       1.1  jonathan  * to be processed by the kernel thread.
   1115       1.1  jonathan  */
   1116       1.1  jonathan int
   1117       1.1  jonathan crypto_kdispatch(struct cryptkop *krp)
   1118       1.1  jonathan {
   1119       1.1  jonathan 	struct cryptocap *cap;
   1120      1.23       tls 	int result;
   1121       1.1  jonathan 
   1122      1.59  knakahar 	KASSERT(krp != NULL);
   1123      1.59  knakahar 
   1124       1.1  jonathan 	cryptostats.cs_kops++;
   1125       1.1  jonathan 
   1126  1.78.2.1       snj 	mutex_enter(&crypto_q_mtx);
   1127  1.78.2.1       snj 	cap = crypto_checkdriver_lock(krp->krp_hid);
   1128      1.68  knakahar 	/*
   1129      1.68  knakahar 	 * TODO:
   1130      1.68  knakahar 	 * If we can ensure the driver has been valid until the driver is
   1131      1.68  knakahar 	 * done crypto_unregister(), this migrate operation is not required.
   1132      1.68  knakahar 	 */
   1133      1.68  knakahar 	if (cap == NULL) {
   1134      1.68  knakahar 		TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
   1135  1.78.2.1       snj 		mutex_exit(&crypto_q_mtx);
   1136      1.68  knakahar 		return 0;
   1137      1.68  knakahar 	}
   1138      1.68  knakahar 
   1139      1.68  knakahar 	if (cap->cc_kqblocked != 0) {
   1140  1.78.2.1       snj 		crypto_driver_unlock(cap);
   1141       1.1  jonathan 		/*
   1142       1.1  jonathan 		 * The driver is blocked, just queue the op until
   1143       1.1  jonathan 		 * it unblocks and the swi thread gets kicked.
   1144       1.1  jonathan 		 */
   1145       1.1  jonathan 		TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
   1146  1.78.2.1       snj 		mutex_exit(&crypto_q_mtx);
   1147      1.68  knakahar 		return 0;
   1148      1.68  knakahar 	}
   1149      1.68  knakahar 
   1150  1.78.2.1       snj 	crypto_driver_unlock(cap);
   1151      1.68  knakahar 	result = crypto_kinvoke(krp, 0);
   1152      1.68  knakahar 	if (result == ERESTART) {
   1153      1.68  knakahar 		/*
   1154      1.68  knakahar 		 * The driver ran out of resources, mark the
   1155      1.68  knakahar 		 * driver ``blocked'' for cryptop's and put
   1156      1.68  knakahar 		 * the op on the queue.
   1157      1.68  knakahar 		 */
   1158  1.78.2.1       snj 		crypto_driver_lock(cap);
   1159      1.77  knakahar 		cap->cc_kqblocked = 1;
   1160  1.78.2.1       snj 		crypto_driver_unlock(cap);
   1161      1.68  knakahar 		TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
   1162      1.68  knakahar 		cryptostats.cs_kblocks++;
   1163  1.78.2.1       snj 		mutex_exit(&crypto_q_mtx);
   1164      1.68  knakahar 
   1165      1.68  knakahar 		/*
   1166      1.68  knakahar 		 * The krp is enqueued to crp_kq, that is,
   1167      1.68  knakahar 		 * no error occurs. So, this function should
   1168      1.68  knakahar 		 * not return error.
   1169      1.68  knakahar 		 */
   1170       1.1  jonathan 		result = 0;
   1171       1.1  jonathan 	}
   1172       1.1  jonathan 
   1173       1.1  jonathan 	return result;
   1174       1.1  jonathan }
   1175       1.1  jonathan 
   1176       1.1  jonathan /*
   1177       1.1  jonathan  * Dispatch an assymetric crypto request to the appropriate crypto devices.
   1178       1.1  jonathan  */
   1179       1.1  jonathan static int
   1180       1.1  jonathan crypto_kinvoke(struct cryptkop *krp, int hint)
   1181       1.1  jonathan {
   1182      1.77  knakahar 	struct cryptocap *cap = NULL;
   1183       1.1  jonathan 	u_int32_t hid;
   1184       1.1  jonathan 	int error;
   1185       1.1  jonathan 
   1186      1.59  knakahar 	KASSERT(krp != NULL);
   1187      1.59  knakahar 
   1188       1.1  jonathan 	/* Sanity checks. */
   1189       1.1  jonathan 	if (krp->krp_callback == NULL) {
   1190      1.30    darran 		cv_destroy(&krp->krp_cv);
   1191      1.76  knakahar 		crypto_kfreereq(krp);
   1192       1.1  jonathan 		return EINVAL;
   1193       1.1  jonathan 	}
   1194       1.1  jonathan 
   1195      1.57  knakahar 	mutex_enter(&crypto_drv_mtx);
   1196       1.1  jonathan 	for (hid = 0; hid < crypto_drivers_num; hid++) {
   1197      1.77  knakahar 		cap = crypto_checkdriver(hid);
   1198      1.77  knakahar 		if (cap == NULL)
   1199      1.77  knakahar 			continue;
   1200  1.78.2.1       snj 		crypto_driver_lock(cap);
   1201      1.77  knakahar 		if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
   1202  1.78.2.1       snj 		    crypto_devallowsoft == 0) {
   1203  1.78.2.1       snj 			crypto_driver_unlock(cap);
   1204       1.1  jonathan 			continue;
   1205  1.78.2.1       snj 		}
   1206  1.78.2.1       snj 		if (cap->cc_kprocess == NULL) {
   1207  1.78.2.1       snj 			crypto_driver_unlock(cap);
   1208       1.1  jonathan 			continue;
   1209  1.78.2.1       snj 		}
   1210      1.77  knakahar 		if ((cap->cc_kalg[krp->krp_op] &
   1211  1.78.2.1       snj 			CRYPTO_ALG_FLAG_SUPPORTED) == 0) {
   1212  1.78.2.1       snj 			crypto_driver_unlock(cap);
   1213       1.1  jonathan 			continue;
   1214  1.78.2.1       snj 		}
   1215       1.1  jonathan 		break;
   1216       1.1  jonathan 	}
   1217  1.78.2.1       snj 	mutex_exit(&crypto_drv_mtx);
   1218      1.77  knakahar 	if (cap != NULL) {
   1219      1.37  christos 		int (*process)(void *, struct cryptkop *, int);
   1220      1.37  christos 		void *arg;
   1221      1.37  christos 
   1222      1.77  knakahar 		process = cap->cc_kprocess;
   1223      1.77  knakahar 		arg = cap->cc_karg;
   1224       1.1  jonathan 		krp->krp_hid = hid;
   1225  1.78.2.1       snj 		crypto_driver_unlock(cap);
   1226      1.37  christos 		error = (*process)(arg, krp, hint);
   1227       1.1  jonathan 	} else {
   1228       1.1  jonathan 		error = ENODEV;
   1229       1.1  jonathan 	}
   1230       1.1  jonathan 
   1231       1.1  jonathan 	if (error) {
   1232       1.1  jonathan 		krp->krp_status = error;
   1233       1.1  jonathan 		crypto_kdone(krp);
   1234       1.1  jonathan 	}
   1235       1.1  jonathan 	return 0;
   1236       1.1  jonathan }
   1237       1.1  jonathan 
   1238       1.1  jonathan #ifdef CRYPTO_TIMING
   1239       1.1  jonathan static void
   1240       1.1  jonathan crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
   1241       1.1  jonathan {
   1242       1.1  jonathan 	struct timespec now, t;
   1243       1.1  jonathan 
   1244       1.1  jonathan 	nanouptime(&now);
   1245       1.1  jonathan 	t.tv_sec = now.tv_sec - tv->tv_sec;
   1246       1.1  jonathan 	t.tv_nsec = now.tv_nsec - tv->tv_nsec;
   1247       1.1  jonathan 	if (t.tv_nsec < 0) {
   1248       1.1  jonathan 		t.tv_sec--;
   1249       1.1  jonathan 		t.tv_nsec += 1000000000;
   1250       1.1  jonathan 	}
   1251       1.1  jonathan 	timespecadd(&ts->acc, &t, &t);
   1252       1.1  jonathan 	if (timespeccmp(&t, &ts->min, <))
   1253       1.1  jonathan 		ts->min = t;
   1254       1.1  jonathan 	if (timespeccmp(&t, &ts->max, >))
   1255       1.1  jonathan 		ts->max = t;
   1256       1.1  jonathan 	ts->count++;
   1257       1.1  jonathan 
   1258       1.1  jonathan 	*tv = now;
   1259       1.1  jonathan }
   1260       1.1  jonathan #endif
   1261       1.1  jonathan 
   1262       1.1  jonathan /*
   1263       1.1  jonathan  * Dispatch a crypto request to the appropriate crypto devices.
   1264       1.1  jonathan  */
   1265       1.1  jonathan static int
   1266       1.1  jonathan crypto_invoke(struct cryptop *crp, int hint)
   1267       1.1  jonathan {
   1268      1.77  knakahar 	struct cryptocap *cap;
   1269       1.1  jonathan 
   1270      1.59  knakahar 	KASSERT(crp != NULL);
   1271      1.59  knakahar 
   1272       1.1  jonathan #ifdef CRYPTO_TIMING
   1273       1.1  jonathan 	if (crypto_timing)
   1274       1.1  jonathan 		crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
   1275       1.1  jonathan #endif
   1276       1.1  jonathan 	/* Sanity checks. */
   1277       1.1  jonathan 	if (crp->crp_callback == NULL) {
   1278       1.1  jonathan 		return EINVAL;
   1279       1.1  jonathan 	}
   1280       1.1  jonathan 	if (crp->crp_desc == NULL) {
   1281       1.1  jonathan 		crp->crp_etype = EINVAL;
   1282       1.1  jonathan 		crypto_done(crp);
   1283       1.1  jonathan 		return 0;
   1284       1.1  jonathan 	}
   1285       1.1  jonathan 
   1286  1.78.2.1       snj 	cap = crypto_checkdriver_lock(CRYPTO_SESID2HID(crp->crp_sid));
   1287      1.77  knakahar 	if (cap != NULL && (cap->cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
   1288      1.37  christos 		int (*process)(void *, struct cryptop *, int);
   1289      1.37  christos 		void *arg;
   1290      1.37  christos 
   1291      1.77  knakahar 		process = cap->cc_process;
   1292      1.77  knakahar 		arg = cap->cc_arg;
   1293      1.37  christos 
   1294      1.37  christos 		/*
   1295      1.37  christos 		 * Invoke the driver to process the request.
   1296      1.37  christos 		 */
   1297      1.64  knakahar 		DPRINTF("calling process for %p\n", crp);
   1298  1.78.2.1       snj 		crypto_driver_unlock(cap);
   1299      1.37  christos 		return (*process)(arg, crp, hint);
   1300       1.1  jonathan 	} else {
   1301       1.1  jonathan 		struct cryptodesc *crd;
   1302      1.16       mrg 		u_int64_t nid = 0;
   1303       1.1  jonathan 
   1304  1.78.2.1       snj 		if (cap != NULL)
   1305  1.78.2.1       snj 			crypto_driver_unlock(cap);
   1306  1.78.2.1       snj 
   1307       1.1  jonathan 		/*
   1308       1.1  jonathan 		 * Driver has unregistered; migrate the session and return
   1309       1.1  jonathan 		 * an error to the caller so they'll resubmit the op.
   1310       1.1  jonathan 		 */
   1311      1.63  knakahar 		crypto_freesession(crp->crp_sid);
   1312      1.63  knakahar 
   1313       1.1  jonathan 		for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
   1314       1.1  jonathan 			crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
   1315       1.1  jonathan 
   1316       1.1  jonathan 		if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 0) == 0)
   1317       1.1  jonathan 			crp->crp_sid = nid;
   1318       1.1  jonathan 
   1319       1.1  jonathan 		crp->crp_etype = EAGAIN;
   1320      1.23       tls 
   1321       1.1  jonathan 		crypto_done(crp);
   1322       1.1  jonathan 		return 0;
   1323       1.1  jonathan 	}
   1324       1.1  jonathan }
   1325       1.1  jonathan 
   1326       1.1  jonathan /*
   1327       1.1  jonathan  * Release a set of crypto descriptors.
   1328       1.1  jonathan  */
   1329       1.1  jonathan void
   1330       1.1  jonathan crypto_freereq(struct cryptop *crp)
   1331       1.1  jonathan {
   1332       1.1  jonathan 	struct cryptodesc *crd;
   1333       1.1  jonathan 
   1334       1.1  jonathan 	if (crp == NULL)
   1335       1.1  jonathan 		return;
   1336      1.64  knakahar 	DPRINTF("lid[%u]: crp %p\n", CRYPTO_SESID2LID(crp->crp_sid), crp);
   1337       1.1  jonathan 
   1338      1.30    darran 	/* sanity check */
   1339      1.30    darran 	if (crp->crp_flags & CRYPTO_F_ONRETQ) {
   1340      1.30    darran 		panic("crypto_freereq() freeing crp on RETQ\n");
   1341      1.30    darran 	}
   1342      1.30    darran 
   1343       1.1  jonathan 	while ((crd = crp->crp_desc) != NULL) {
   1344       1.1  jonathan 		crp->crp_desc = crd->crd_next;
   1345       1.1  jonathan 		pool_put(&cryptodesc_pool, crd);
   1346       1.1  jonathan 	}
   1347       1.1  jonathan 	pool_put(&cryptop_pool, crp);
   1348       1.1  jonathan }
   1349       1.1  jonathan 
   1350       1.1  jonathan /*
   1351       1.1  jonathan  * Acquire a set of crypto descriptors.
   1352       1.1  jonathan  */
   1353       1.1  jonathan struct cryptop *
   1354       1.1  jonathan crypto_getreq(int num)
   1355       1.1  jonathan {
   1356       1.1  jonathan 	struct cryptodesc *crd;
   1357       1.1  jonathan 	struct cryptop *crp;
   1358       1.1  jonathan 
   1359      1.74  knakahar 	/*
   1360      1.74  knakahar 	 * When crp_ret_q is full, we restrict here to avoid crp_ret_q overflow
   1361      1.74  knakahar 	 * by error callback.
   1362      1.74  knakahar 	 */
   1363      1.74  knakahar 	if (CRYPTO_Q_IS_FULL(crp_ret_q)) {
   1364      1.74  knakahar 		CRYPTO_Q_INC_DROPS(crp_ret_q);
   1365      1.74  knakahar 		return NULL;
   1366      1.74  knakahar 	}
   1367      1.74  knakahar 
   1368       1.1  jonathan 	crp = pool_get(&cryptop_pool, 0);
   1369       1.1  jonathan 	if (crp == NULL) {
   1370       1.1  jonathan 		return NULL;
   1371       1.1  jonathan 	}
   1372      1.31    cegger 	memset(crp, 0, sizeof(struct cryptop));
   1373       1.1  jonathan 
   1374       1.1  jonathan 	while (num--) {
   1375       1.1  jonathan 		crd = pool_get(&cryptodesc_pool, 0);
   1376       1.1  jonathan 		if (crd == NULL) {
   1377       1.1  jonathan 			crypto_freereq(crp);
   1378       1.1  jonathan 			return NULL;
   1379       1.1  jonathan 		}
   1380       1.1  jonathan 
   1381      1.31    cegger 		memset(crd, 0, sizeof(struct cryptodesc));
   1382       1.1  jonathan 		crd->crd_next = crp->crp_desc;
   1383       1.1  jonathan 		crp->crp_desc = crd;
   1384       1.1  jonathan 	}
   1385       1.1  jonathan 
   1386       1.1  jonathan 	return crp;
   1387       1.1  jonathan }
   1388       1.1  jonathan 
   1389       1.1  jonathan /*
   1390      1.76  knakahar  * Release a set of asymmetric crypto descriptors.
   1391      1.76  knakahar  * Currently, support one descriptor only.
   1392      1.76  knakahar  */
   1393      1.76  knakahar void
   1394      1.76  knakahar crypto_kfreereq(struct cryptkop *krp)
   1395      1.76  knakahar {
   1396      1.76  knakahar 
   1397      1.76  knakahar 	if (krp == NULL)
   1398      1.76  knakahar 		return;
   1399      1.76  knakahar 
   1400      1.76  knakahar 	DPRINTF("krp %p\n", krp);
   1401      1.76  knakahar 
   1402      1.76  knakahar 	/* sanity check */
   1403      1.76  knakahar 	if (krp->krp_flags & CRYPTO_F_ONRETQ) {
   1404      1.76  knakahar 		panic("crypto_kfreereq() freeing krp on RETQ\n");
   1405      1.76  knakahar 	}
   1406      1.76  knakahar 
   1407      1.76  knakahar 	pool_put(&cryptkop_pool, krp);
   1408      1.76  knakahar }
   1409      1.76  knakahar 
   1410      1.76  knakahar /*
   1411      1.76  knakahar  * Acquire a set of asymmetric crypto descriptors.
   1412      1.76  knakahar  * Currently, support one descriptor only.
   1413      1.76  knakahar  */
   1414      1.76  knakahar struct cryptkop *
   1415      1.76  knakahar crypto_kgetreq(int num __unused, int prflags)
   1416      1.76  knakahar {
   1417      1.76  knakahar 	struct cryptkop *krp;
   1418      1.76  knakahar 
   1419      1.76  knakahar 	/*
   1420      1.76  knakahar 	 * When crp_ret_kq is full, we restrict here to avoid crp_ret_kq
   1421      1.76  knakahar 	 * overflow by error callback.
   1422      1.76  knakahar 	 */
   1423      1.76  knakahar 	if (CRYPTO_Q_IS_FULL(crp_ret_kq)) {
   1424      1.76  knakahar 		CRYPTO_Q_INC_DROPS(crp_ret_kq);
   1425      1.76  knakahar 		return NULL;
   1426      1.76  knakahar 	}
   1427      1.76  knakahar 
   1428      1.76  knakahar 	krp = pool_get(&cryptkop_pool, prflags);
   1429      1.76  knakahar 	if (krp == NULL) {
   1430      1.76  knakahar 		return NULL;
   1431      1.76  knakahar 	}
   1432      1.76  knakahar 	memset(krp, 0, sizeof(struct cryptkop));
   1433      1.76  knakahar 
   1434      1.76  knakahar 	return krp;
   1435      1.76  knakahar }
   1436      1.76  knakahar 
   1437      1.76  knakahar /*
   1438       1.1  jonathan  * Invoke the callback on behalf of the driver.
   1439       1.1  jonathan  */
   1440       1.1  jonathan void
   1441       1.1  jonathan crypto_done(struct cryptop *crp)
   1442       1.1  jonathan {
   1443      1.23       tls 	int wasempty;
   1444      1.23       tls 
   1445      1.59  knakahar 	KASSERT(crp != NULL);
   1446      1.59  knakahar 
   1447       1.1  jonathan 	if (crp->crp_etype != 0)
   1448       1.1  jonathan 		cryptostats.cs_errs++;
   1449       1.1  jonathan #ifdef CRYPTO_TIMING
   1450       1.1  jonathan 	if (crypto_timing)
   1451       1.1  jonathan 		crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
   1452       1.1  jonathan #endif
   1453      1.64  knakahar 	DPRINTF("lid[%u]: crp %p\n", CRYPTO_SESID2LID(crp->crp_sid), crp);
   1454      1.27       tls 
   1455       1.1  jonathan 	/*
   1456      1.23       tls 	 * Normal case; queue the callback for the thread.
   1457      1.23       tls 	 *
   1458      1.23       tls 	 * The return queue is manipulated by the swi thread
   1459      1.23       tls 	 * and, potentially, by crypto device drivers calling
   1460      1.23       tls 	 * back to mark operations completed.  Thus we need
   1461      1.23       tls 	 * to mask both while manipulating the return queue.
   1462       1.1  jonathan 	 */
   1463      1.27       tls   	if (crp->crp_flags & CRYPTO_F_CBIMM) {
   1464      1.27       tls 		/*
   1465      1.27       tls 	 	* Do the callback directly.  This is ok when the
   1466      1.27       tls   	 	* callback routine does very little (e.g. the
   1467      1.27       tls 	 	* /dev/crypto callback method just does a wakeup).
   1468      1.27       tls 	 	*/
   1469      1.40  drochner 		mutex_spin_enter(&crypto_ret_q_mtx);
   1470      1.30    darran 		crp->crp_flags |= CRYPTO_F_DONE;
   1471      1.40  drochner 		mutex_spin_exit(&crypto_ret_q_mtx);
   1472      1.30    darran 
   1473      1.27       tls #ifdef CRYPTO_TIMING
   1474      1.27       tls 		if (crypto_timing) {
   1475      1.27       tls 			/*
   1476      1.27       tls 		 	* NB: We must copy the timestamp before
   1477      1.27       tls 		 	* doing the callback as the cryptop is
   1478      1.27       tls 		 	* likely to be reclaimed.
   1479      1.27       tls 		 	*/
   1480      1.27       tls 			struct timespec t = crp->crp_tstamp;
   1481      1.27       tls 			crypto_tstat(&cryptostats.cs_cb, &t);
   1482      1.27       tls 			crp->crp_callback(crp);
   1483      1.27       tls 			crypto_tstat(&cryptostats.cs_finis, &t);
   1484      1.27       tls 		} else
   1485      1.27       tls #endif
   1486      1.27       tls 		crp->crp_callback(crp);
   1487      1.27       tls 	} else {
   1488      1.40  drochner 		mutex_spin_enter(&crypto_ret_q_mtx);
   1489      1.30    darran 		crp->crp_flags |= CRYPTO_F_DONE;
   1490      1.52  knakahar #if 0
   1491      1.30    darran 		if (crp->crp_flags & CRYPTO_F_USER) {
   1492      1.52  knakahar 			/*
   1493      1.52  knakahar 			 * TODO:
   1494      1.52  knakahar 			 * If crp->crp_flags & CRYPTO_F_USER and the used
   1495      1.52  knakahar 			 * encryption driver does all the processing in
   1496      1.52  knakahar 			 * the same context, we can skip enqueueing crp_ret_q
   1497      1.52  knakahar 			 * and cv_signal(&cryptoret_cv).
   1498      1.30    darran 			 */
   1499      1.64  knakahar 			DPRINTF("lid[%u]: crp %p CRYPTO_F_USER\n",
   1500      1.64  knakahar 				CRYPTO_SESID2LID(crp->crp_sid), crp);
   1501      1.52  knakahar 		} else
   1502      1.52  knakahar #endif
   1503      1.52  knakahar 		{
   1504      1.30    darran 			wasempty = TAILQ_EMPTY(&crp_ret_q);
   1505      1.64  knakahar 			DPRINTF("lid[%u]: queueing %p\n",
   1506      1.64  knakahar 				CRYPTO_SESID2LID(crp->crp_sid), crp);
   1507      1.30    darran 			crp->crp_flags |= CRYPTO_F_ONRETQ;
   1508      1.30    darran 			TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
   1509      1.73  knakahar 			CRYPTO_Q_INC(crp_ret_q);
   1510      1.30    darran 			if (wasempty) {
   1511      1.64  knakahar 				DPRINTF("lid[%u]: waking cryptoret, "
   1512      1.35  jakllsch 					"crp %p hit empty queue\n.",
   1513      1.64  knakahar 					CRYPTO_SESID2LID(crp->crp_sid), crp);
   1514      1.30    darran 				cv_signal(&cryptoret_cv);
   1515      1.30    darran 			}
   1516      1.27       tls 		}
   1517      1.40  drochner 		mutex_spin_exit(&crypto_ret_q_mtx);
   1518       1.1  jonathan 	}
   1519       1.1  jonathan }
   1520       1.1  jonathan 
   1521       1.1  jonathan /*
   1522       1.1  jonathan  * Invoke the callback on behalf of the driver.
   1523       1.1  jonathan  */
   1524       1.1  jonathan void
   1525       1.1  jonathan crypto_kdone(struct cryptkop *krp)
   1526       1.1  jonathan {
   1527      1.23       tls 	int wasempty;
   1528       1.1  jonathan 
   1529      1.59  knakahar 	KASSERT(krp != NULL);
   1530      1.59  knakahar 
   1531       1.1  jonathan 	if (krp->krp_status != 0)
   1532       1.1  jonathan 		cryptostats.cs_kerrs++;
   1533      1.27       tls 
   1534      1.27       tls 	krp->krp_flags |= CRYPTO_F_DONE;
   1535      1.27       tls 
   1536       1.1  jonathan 	/*
   1537       1.1  jonathan 	 * The return queue is manipulated by the swi thread
   1538       1.1  jonathan 	 * and, potentially, by crypto device drivers calling
   1539       1.1  jonathan 	 * back to mark operations completed.  Thus we need
   1540       1.1  jonathan 	 * to mask both while manipulating the return queue.
   1541       1.1  jonathan 	 */
   1542      1.27       tls 	if (krp->krp_flags & CRYPTO_F_CBIMM) {
   1543      1.27       tls 		krp->krp_callback(krp);
   1544      1.27       tls 	} else {
   1545      1.40  drochner 		mutex_spin_enter(&crypto_ret_q_mtx);
   1546      1.27       tls 		wasempty = TAILQ_EMPTY(&crp_ret_kq);
   1547      1.27       tls 		krp->krp_flags |= CRYPTO_F_ONRETQ;
   1548      1.27       tls 		TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
   1549      1.73  knakahar 		CRYPTO_Q_INC(crp_ret_kq);
   1550      1.27       tls 		if (wasempty)
   1551      1.27       tls 			cv_signal(&cryptoret_cv);
   1552      1.40  drochner 		mutex_spin_exit(&crypto_ret_q_mtx);
   1553      1.27       tls 	}
   1554       1.1  jonathan }
   1555       1.1  jonathan 
   1556       1.1  jonathan int
   1557       1.1  jonathan crypto_getfeat(int *featp)
   1558       1.1  jonathan {
   1559       1.1  jonathan 
   1560  1.78.2.1       snj 	if (crypto_userasymcrypto == 0) {
   1561  1.78.2.1       snj 		*featp = 0;
   1562      1.57  knakahar 		return 0;
   1563  1.78.2.1       snj 	}
   1564       1.1  jonathan 
   1565      1.57  knakahar 	mutex_enter(&crypto_drv_mtx);
   1566       1.1  jonathan 
   1567  1.78.2.1       snj 	int feat = 0;
   1568  1.78.2.1       snj 	for (int hid = 0; hid < crypto_drivers_num; hid++) {
   1569      1.77  knakahar 		struct cryptocap *cap;
   1570      1.77  knakahar 		cap = crypto_checkdriver(hid);
   1571      1.77  knakahar 		if (cap == NULL)
   1572      1.77  knakahar 			continue;
   1573      1.77  knakahar 
   1574  1.78.2.1       snj 		crypto_driver_lock(cap);
   1575  1.78.2.1       snj 
   1576      1.77  knakahar 		if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
   1577  1.78.2.1       snj 		    crypto_devallowsoft == 0)
   1578  1.78.2.1       snj 			goto unlock;
   1579  1.78.2.1       snj 
   1580      1.77  knakahar 		if (cap->cc_kprocess == NULL)
   1581  1.78.2.1       snj 			goto unlock;
   1582  1.78.2.1       snj 
   1583  1.78.2.1       snj 		for (int kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
   1584      1.77  knakahar 			if ((cap->cc_kalg[kalg] &
   1585       1.1  jonathan 			    CRYPTO_ALG_FLAG_SUPPORTED) != 0)
   1586       1.1  jonathan 				feat |=  1 << kalg;
   1587  1.78.2.1       snj 
   1588  1.78.2.1       snj unlock:		crypto_driver_unlock(cap);
   1589       1.1  jonathan 	}
   1590      1.57  knakahar 
   1591      1.57  knakahar 	mutex_exit(&crypto_drv_mtx);
   1592       1.1  jonathan 	*featp = feat;
   1593       1.1  jonathan 	return (0);
   1594       1.1  jonathan }
   1595       1.1  jonathan 
   1596       1.1  jonathan /*
   1597       1.1  jonathan  * Software interrupt thread to dispatch crypto requests.
   1598       1.1  jonathan  */
   1599       1.1  jonathan static void
   1600       1.1  jonathan cryptointr(void)
   1601       1.1  jonathan {
   1602      1.30    darran 	struct cryptop *crp, *submit, *cnext;
   1603      1.30    darran 	struct cryptkop *krp, *knext;
   1604       1.1  jonathan 	struct cryptocap *cap;
   1605      1.23       tls 	int result, hint;
   1606       1.1  jonathan 
   1607       1.1  jonathan 	cryptostats.cs_intrs++;
   1608  1.78.2.1       snj 	mutex_enter(&crypto_q_mtx);
   1609       1.1  jonathan 	do {
   1610       1.1  jonathan 		/*
   1611       1.1  jonathan 		 * Find the first element in the queue that can be
   1612       1.1  jonathan 		 * processed and look-ahead to see if multiple ops
   1613       1.1  jonathan 		 * are ready for the same driver.
   1614       1.1  jonathan 		 */
   1615       1.1  jonathan 		submit = NULL;
   1616       1.1  jonathan 		hint = 0;
   1617      1.30    darran 		TAILQ_FOREACH_SAFE(crp, &crp_q, crp_next, cnext) {
   1618      1.35  jakllsch 			u_int32_t hid = CRYPTO_SESID2HID(crp->crp_sid);
   1619  1.78.2.1       snj 			cap = crypto_checkdriver_lock(hid);
   1620       1.1  jonathan 			if (cap == NULL || cap->cc_process == NULL) {
   1621  1.78.2.1       snj 				if (cap != NULL)
   1622  1.78.2.1       snj 					crypto_driver_unlock(cap);
   1623       1.1  jonathan 				/* Op needs to be migrated, process it. */
   1624      1.69  knakahar 				submit = crp;
   1625       1.1  jonathan 				break;
   1626       1.1  jonathan 			}
   1627      1.70  knakahar 
   1628      1.70  knakahar 			/*
   1629      1.70  knakahar 			 * skip blocked crp regardless of CRYPTO_F_BATCH
   1630      1.70  knakahar 			 */
   1631  1.78.2.1       snj 			if (cap->cc_qblocked != 0) {
   1632  1.78.2.1       snj 				crypto_driver_unlock(cap);
   1633      1.70  knakahar 				continue;
   1634  1.78.2.1       snj 			}
   1635  1.78.2.1       snj 			crypto_driver_unlock(cap);
   1636      1.70  knakahar 
   1637      1.71  knakahar 			/*
   1638      1.71  knakahar 			 * skip batch crp until the end of crp_q
   1639      1.71  knakahar 			 */
   1640      1.71  knakahar 			if ((crp->crp_flags & CRYPTO_F_BATCH) != 0) {
   1641      1.71  knakahar 				if (submit == NULL) {
   1642      1.71  knakahar 					submit = crp;
   1643      1.71  knakahar 				} else {
   1644      1.71  knakahar 					if (CRYPTO_SESID2HID(submit->crp_sid)
   1645      1.71  knakahar 					    == hid)
   1646      1.71  knakahar 						hint = CRYPTO_HINT_MORE;
   1647      1.71  knakahar 				}
   1648      1.71  knakahar 
   1649      1.71  knakahar 				continue;
   1650       1.1  jonathan 			}
   1651      1.71  knakahar 
   1652      1.71  knakahar 			/*
   1653      1.71  knakahar 			 * found first crp which is neither blocked nor batch.
   1654      1.71  knakahar 			 */
   1655      1.71  knakahar 			submit = crp;
   1656      1.71  knakahar 			/*
   1657      1.71  knakahar 			 * batch crp can be processed much later, so clear hint.
   1658      1.71  knakahar 			 */
   1659      1.71  knakahar 			hint = 0;
   1660      1.71  knakahar 			break;
   1661       1.1  jonathan 		}
   1662       1.1  jonathan 		if (submit != NULL) {
   1663       1.1  jonathan 			TAILQ_REMOVE(&crp_q, submit, crp_next);
   1664       1.1  jonathan 			result = crypto_invoke(submit, hint);
   1665      1.23       tls 			/* we must take here as the TAILQ op or kinvoke
   1666      1.23       tls 			   may need this mutex below.  sigh. */
   1667       1.1  jonathan 			if (result == ERESTART) {
   1668       1.1  jonathan 				/*
   1669       1.1  jonathan 				 * The driver ran out of resources, mark the
   1670       1.1  jonathan 				 * driver ``blocked'' for cryptop's and put
   1671       1.1  jonathan 				 * the request back in the queue.  It would
   1672       1.1  jonathan 				 * best to put the request back where we got
   1673       1.1  jonathan 				 * it but that's hard so for now we put it
   1674       1.1  jonathan 				 * at the front.  This should be ok; putting
   1675       1.1  jonathan 				 * it at the end does not work.
   1676       1.1  jonathan 				 */
   1677      1.77  knakahar 				/* validate sid again */
   1678  1.78.2.1       snj 				cap = crypto_checkdriver_lock(CRYPTO_SESID2HID(submit->crp_sid));
   1679      1.77  knakahar 				if (cap == NULL) {
   1680      1.77  knakahar 					/* migrate again, sigh... */
   1681      1.77  knakahar 					TAILQ_INSERT_TAIL(&crp_q, submit, crp_next);
   1682      1.77  knakahar 				} else {
   1683      1.77  knakahar 					cap->cc_qblocked = 1;
   1684  1.78.2.1       snj 					crypto_driver_unlock(cap);
   1685      1.77  knakahar 					TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
   1686      1.77  knakahar 					cryptostats.cs_blocks++;
   1687      1.77  knakahar 				}
   1688       1.1  jonathan 			}
   1689       1.1  jonathan 		}
   1690       1.1  jonathan 
   1691       1.1  jonathan 		/* As above, but for key ops */
   1692      1.30    darran 		TAILQ_FOREACH_SAFE(krp, &crp_kq, krp_next, knext) {
   1693  1.78.2.1       snj 			cap = crypto_checkdriver_lock(krp->krp_hid);
   1694       1.1  jonathan 			if (cap == NULL || cap->cc_kprocess == NULL) {
   1695  1.78.2.1       snj 				if (cap != NULL)
   1696  1.78.2.1       snj 					crypto_driver_unlock(cap);
   1697       1.1  jonathan 				/* Op needs to be migrated, process it. */
   1698       1.1  jonathan 				break;
   1699       1.1  jonathan 			}
   1700  1.78.2.1       snj 			if (!cap->cc_kqblocked) {
   1701  1.78.2.1       snj 				crypto_driver_unlock(cap);
   1702       1.1  jonathan 				break;
   1703  1.78.2.1       snj 			}
   1704  1.78.2.1       snj 			crypto_driver_unlock(cap);
   1705       1.1  jonathan 		}
   1706       1.1  jonathan 		if (krp != NULL) {
   1707       1.1  jonathan 			TAILQ_REMOVE(&crp_kq, krp, krp_next);
   1708       1.1  jonathan 			result = crypto_kinvoke(krp, 0);
   1709      1.23       tls 			/* the next iteration will want the mutex. :-/ */
   1710       1.1  jonathan 			if (result == ERESTART) {
   1711       1.1  jonathan 				/*
   1712       1.1  jonathan 				 * The driver ran out of resources, mark the
   1713       1.1  jonathan 				 * driver ``blocked'' for cryptkop's and put
   1714       1.1  jonathan 				 * the request back in the queue.  It would
   1715       1.1  jonathan 				 * best to put the request back where we got
   1716       1.1  jonathan 				 * it but that's hard so for now we put it
   1717       1.1  jonathan 				 * at the front.  This should be ok; putting
   1718       1.1  jonathan 				 * it at the end does not work.
   1719       1.1  jonathan 				 */
   1720      1.77  knakahar 				/* validate sid again */
   1721  1.78.2.1       snj 				cap = crypto_checkdriver_lock(krp->krp_hid);
   1722      1.77  knakahar 				if (cap == NULL) {
   1723      1.77  knakahar 					/* migrate again, sigh... */
   1724      1.77  knakahar 					TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
   1725      1.77  knakahar 				} else {
   1726      1.77  knakahar 					cap->cc_kqblocked = 1;
   1727  1.78.2.1       snj 					crypto_driver_unlock(cap);
   1728      1.77  knakahar 					TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
   1729      1.77  knakahar 					cryptostats.cs_kblocks++;
   1730      1.77  knakahar 				}
   1731       1.1  jonathan 			}
   1732       1.1  jonathan 		}
   1733       1.1  jonathan 	} while (submit != NULL || krp != NULL);
   1734  1.78.2.1       snj 	mutex_exit(&crypto_q_mtx);
   1735       1.1  jonathan }
   1736       1.1  jonathan 
   1737       1.1  jonathan /*
   1738       1.1  jonathan  * Kernel thread to do callbacks.
   1739       1.1  jonathan  */
   1740       1.1  jonathan static void
   1741       1.1  jonathan cryptoret(void)
   1742       1.1  jonathan {
   1743       1.1  jonathan 	struct cryptop *crp;
   1744       1.1  jonathan 	struct cryptkop *krp;
   1745       1.1  jonathan 
   1746      1.40  drochner 	mutex_spin_enter(&crypto_ret_q_mtx);
   1747       1.1  jonathan 	for (;;) {
   1748       1.1  jonathan 		crp = TAILQ_FIRST(&crp_ret_q);
   1749      1.23       tls 		if (crp != NULL) {
   1750       1.1  jonathan 			TAILQ_REMOVE(&crp_ret_q, crp, crp_next);
   1751      1.73  knakahar 			CRYPTO_Q_DEC(crp_ret_q);
   1752      1.23       tls 			crp->crp_flags &= ~CRYPTO_F_ONRETQ;
   1753      1.23       tls 		}
   1754       1.1  jonathan 		krp = TAILQ_FIRST(&crp_ret_kq);
   1755      1.23       tls 		if (krp != NULL) {
   1756       1.1  jonathan 			TAILQ_REMOVE(&crp_ret_kq, krp, krp_next);
   1757      1.73  knakahar 			CRYPTO_Q_DEC(crp_ret_kq);
   1758      1.23       tls 			krp->krp_flags &= ~CRYPTO_F_ONRETQ;
   1759      1.23       tls 		}
   1760       1.1  jonathan 
   1761      1.23       tls 		/* drop before calling any callbacks. */
   1762      1.26        ad 		if (crp == NULL && krp == NULL) {
   1763      1.46  pgoyette 
   1764      1.46  pgoyette                         /* Check for the exit condition. */
   1765      1.46  pgoyette 			if (crypto_exit_flag != 0) {
   1766      1.46  pgoyette 
   1767      1.46  pgoyette         			/* Time to die. */
   1768      1.46  pgoyette 				crypto_exit_flag = 0;
   1769      1.46  pgoyette         			cv_broadcast(&cryptoret_cv);
   1770      1.46  pgoyette 				mutex_spin_exit(&crypto_ret_q_mtx);
   1771      1.46  pgoyette         			kthread_exit(0);
   1772      1.46  pgoyette 			}
   1773      1.46  pgoyette 
   1774      1.26        ad 			cryptostats.cs_rets++;
   1775      1.40  drochner 			cv_wait(&cryptoret_cv, &crypto_ret_q_mtx);
   1776      1.26        ad 			continue;
   1777      1.26        ad 		}
   1778      1.26        ad 
   1779      1.40  drochner 		mutex_spin_exit(&crypto_ret_q_mtx);
   1780      1.26        ad 
   1781      1.26        ad 		if (crp != NULL) {
   1782       1.1  jonathan #ifdef CRYPTO_TIMING
   1783      1.26        ad 			if (crypto_timing) {
   1784      1.26        ad 				/*
   1785      1.26        ad 				 * NB: We must copy the timestamp before
   1786      1.26        ad 				 * doing the callback as the cryptop is
   1787      1.26        ad 				 * likely to be reclaimed.
   1788      1.26        ad 				 */
   1789      1.26        ad 				struct timespec t = crp->crp_tstamp;
   1790      1.26        ad 				crypto_tstat(&cryptostats.cs_cb, &t);
   1791      1.26        ad 				crp->crp_callback(crp);
   1792      1.26        ad 				crypto_tstat(&cryptostats.cs_finis, &t);
   1793      1.26        ad 			} else
   1794       1.1  jonathan #endif
   1795      1.26        ad 			{
   1796      1.26        ad 				crp->crp_callback(crp);
   1797       1.1  jonathan 			}
   1798       1.1  jonathan 		}
   1799      1.26        ad 		if (krp != NULL)
   1800      1.26        ad 			krp->krp_callback(krp);
   1801      1.26        ad 
   1802      1.40  drochner 		mutex_spin_enter(&crypto_ret_q_mtx);
   1803       1.1  jonathan 	}
   1804       1.1  jonathan }
   1805      1.42  pgoyette 
   1806      1.42  pgoyette /* NetBSD module interface */
   1807      1.42  pgoyette 
   1808      1.42  pgoyette MODULE(MODULE_CLASS_MISC, opencrypto, NULL);
   1809      1.42  pgoyette 
   1810      1.42  pgoyette static int
   1811      1.42  pgoyette opencrypto_modcmd(modcmd_t cmd, void *opaque)
   1812      1.42  pgoyette {
   1813      1.46  pgoyette 	int error = 0;
   1814      1.42  pgoyette 
   1815      1.42  pgoyette 	switch (cmd) {
   1816      1.42  pgoyette 	case MODULE_CMD_INIT:
   1817      1.43  pgoyette #ifdef _MODULE
   1818      1.46  pgoyette 		error = crypto_init();
   1819      1.43  pgoyette #endif
   1820      1.46  pgoyette 		break;
   1821      1.42  pgoyette 	case MODULE_CMD_FINI:
   1822      1.43  pgoyette #ifdef _MODULE
   1823      1.46  pgoyette 		error = crypto_destroy(true);
   1824      1.43  pgoyette #endif
   1825      1.46  pgoyette 		break;
   1826      1.42  pgoyette 	default:
   1827      1.46  pgoyette 		error = ENOTTY;
   1828      1.42  pgoyette 	}
   1829      1.46  pgoyette 	return error;
   1830      1.42  pgoyette }
   1831