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