crypto.c revision 199884
1139825Simp/*- 2167755Ssam * Copyright (c) 2002-2006 Sam Leffler. All rights reserved. 3167755Ssam * 4167755Ssam * Redistribution and use in source and binary forms, with or without 5167755Ssam * modification, are permitted provided that the following conditions 6167755Ssam * are met: 7167755Ssam * 1. Redistributions of source code must retain the above copyright 8167755Ssam * notice, this list of conditions and the following disclaimer. 9167755Ssam * 2. Redistributions in binary form must reproduce the above copyright 10167755Ssam * notice, this list of conditions and the following disclaimer in the 11167755Ssam * documentation and/or other materials provided with the distribution. 12167755Ssam * 13167755Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14167755Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15167755Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16167755Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17167755Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18167755Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19167755Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20167755Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21167755Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22167755Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23167755Ssam */ 24167755Ssam 25167755Ssam#include <sys/cdefs.h> 26167755Ssam__FBSDID("$FreeBSD: head/sys/opencrypto/crypto.c 199884 2009-11-28 16:54:18Z bz $"); 27167755Ssam 28167755Ssam/* 29167755Ssam * Cryptographic Subsystem. 30167755Ssam * 31167755Ssam * This code is derived from the Openbsd Cryptographic Framework (OCF) 32167755Ssam * that has the copyright shown below. Very little of the original 33167755Ssam * code remains. 34167755Ssam */ 35167755Ssam 36167755Ssam/*- 37104476Ssam * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) 38104476Ssam * 39104476Ssam * This code was written by Angelos D. Keromytis in Athens, Greece, in 40104476Ssam * February 2000. Network Security Technologies Inc. (NSTI) kindly 41104476Ssam * supported the development of this code. 42104476Ssam * 43104476Ssam * Copyright (c) 2000, 2001 Angelos D. Keromytis 44104476Ssam * 45104476Ssam * Permission to use, copy, and modify this software with or without fee 46104476Ssam * is hereby granted, provided that this entire notice is included in 47104476Ssam * all source code copies of any software which is or includes a copy or 48104476Ssam * modification of this software. 49104476Ssam * 50104476Ssam * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 51104476Ssam * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 52104476Ssam * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 53104476Ssam * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 54104476Ssam * PURPOSE. 55104476Ssam */ 56116191Sobrien 57108587Ssam#define CRYPTO_TIMING /* enable timing support */ 58104476Ssam 59167755Ssam#include "opt_ddb.h" 60199884Sbz#include "opt_kdtrace.h" 61167755Ssam 62104476Ssam#include <sys/param.h> 63104476Ssam#include <sys/systm.h> 64104476Ssam#include <sys/eventhandler.h> 65104476Ssam#include <sys/kernel.h> 66104476Ssam#include <sys/kthread.h> 67104476Ssam#include <sys/lock.h> 68129880Sphk#include <sys/module.h> 69104476Ssam#include <sys/mutex.h> 70104476Ssam#include <sys/malloc.h> 71104476Ssam#include <sys/proc.h> 72199884Sbz#include <sys/sdt.h> 73104476Ssam#include <sys/sysctl.h> 74104476Ssam 75167755Ssam#include <ddb/ddb.h> 76167755Ssam 77104476Ssam#include <vm/uma.h> 78104476Ssam#include <opencrypto/cryptodev.h> 79104628Ssam#include <opencrypto/xform.h> /* XXX for M_XDATA */ 80104476Ssam 81167755Ssam#include <sys/kobj.h> 82167755Ssam#include <sys/bus.h> 83167755Ssam#include "cryptodev_if.h" 84167755Ssam 85199884SbzSDT_PROVIDER_DEFINE(opencrypto); 86199884Sbz 87104476Ssam/* 88104476Ssam * Crypto drivers register themselves by allocating a slot in the 89104476Ssam * crypto_drivers table with crypto_get_driverid() and then registering 90104476Ssam * each algorithm they support with crypto_register() and crypto_kregister(). 91104476Ssam */ 92104476Ssamstatic struct mtx crypto_drivers_mtx; /* lock on driver table */ 93104476Ssam#define CRYPTO_DRIVER_LOCK() mtx_lock(&crypto_drivers_mtx) 94104476Ssam#define CRYPTO_DRIVER_UNLOCK() mtx_unlock(&crypto_drivers_mtx) 95167755Ssam#define CRYPTO_DRIVER_ASSERT() mtx_assert(&crypto_drivers_mtx, MA_OWNED) 96167755Ssam 97167755Ssam/* 98167755Ssam * Crypto device/driver capabilities structure. 99167755Ssam * 100167755Ssam * Synchronization: 101167755Ssam * (d) - protected by CRYPTO_DRIVER_LOCK() 102167755Ssam * (q) - protected by CRYPTO_Q_LOCK() 103167755Ssam * Not tagged fields are read-only. 104167755Ssam */ 105167755Ssamstruct cryptocap { 106167755Ssam device_t cc_dev; /* (d) device/driver */ 107167755Ssam u_int32_t cc_sessions; /* (d) # of sessions */ 108167755Ssam u_int32_t cc_koperations; /* (d) # os asym operations */ 109167755Ssam /* 110167755Ssam * Largest possible operator length (in bits) for each type of 111167755Ssam * encryption algorithm. XXX not used 112167755Ssam */ 113167755Ssam u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1]; 114167755Ssam u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1]; 115167755Ssam u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1]; 116167755Ssam 117167755Ssam int cc_flags; /* (d) flags */ 118167755Ssam#define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */ 119167755Ssam int cc_qblocked; /* (q) symmetric q blocked */ 120167755Ssam int cc_kqblocked; /* (q) asymmetric q blocked */ 121167755Ssam}; 122104476Ssamstatic struct cryptocap *crypto_drivers = NULL; 123104476Ssamstatic int crypto_drivers_num = 0; 124104476Ssam 125104476Ssam/* 126104476Ssam * There are two queues for crypto requests; one for symmetric (e.g. 127104476Ssam * cipher) operations and one for asymmetric (e.g. MOD)operations. 128104476Ssam * A single mutex is used to lock access to both queues. We could 129104476Ssam * have one per-queue but having one simplifies handling of block/unblock 130104476Ssam * operations. 131104476Ssam */ 132158827Spjdstatic int crp_sleep = 0; 133104476Ssamstatic TAILQ_HEAD(,cryptop) crp_q; /* request queues */ 134104476Ssamstatic TAILQ_HEAD(,cryptkop) crp_kq; 135104476Ssamstatic struct mtx crypto_q_mtx; 136104476Ssam#define CRYPTO_Q_LOCK() mtx_lock(&crypto_q_mtx) 137104476Ssam#define CRYPTO_Q_UNLOCK() mtx_unlock(&crypto_q_mtx) 138104476Ssam 139104476Ssam/* 140104476Ssam * There are two queues for processing completed crypto requests; one 141104476Ssam * for the symmetric and one for the asymmetric ops. We only need one 142104476Ssam * but have two to avoid type futzing (cryptop vs. cryptkop). A single 143104476Ssam * mutex is used to lock access to both queues. Note that this lock 144104476Ssam * must be separate from the lock on request queues to insure driver 145104476Ssam * callbacks don't generate lock order reversals. 146104476Ssam */ 147104476Ssamstatic TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queues */ 148104476Ssamstatic TAILQ_HEAD(,cryptkop) crp_ret_kq; 149104476Ssamstatic struct mtx crypto_ret_q_mtx; 150104476Ssam#define CRYPTO_RETQ_LOCK() mtx_lock(&crypto_ret_q_mtx) 151104476Ssam#define CRYPTO_RETQ_UNLOCK() mtx_unlock(&crypto_ret_q_mtx) 152158826Spjd#define CRYPTO_RETQ_EMPTY() (TAILQ_EMPTY(&crp_ret_q) && TAILQ_EMPTY(&crp_ret_kq)) 153104476Ssam 154104476Ssamstatic uma_zone_t cryptop_zone; 155104476Ssamstatic uma_zone_t cryptodesc_zone; 156104476Ssam 157104476Ssamint crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */ 158104476SsamSYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW, 159104476Ssam &crypto_userasymcrypto, 0, 160104476Ssam "Enable/disable user-mode access to asymmetric crypto support"); 161104476Ssamint crypto_devallowsoft = 0; /* only use hardware crypto for asym */ 162104476SsamSYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW, 163104476Ssam &crypto_devallowsoft, 0, 164104476Ssam "Enable/disable use of software asym crypto support"); 165104476Ssam 166104476SsamMALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records"); 167104476Ssam 168108588Ssamstatic void crypto_proc(void); 169108588Ssamstatic struct proc *cryptoproc; 170108588Ssamstatic void crypto_ret_proc(void); 171108588Ssamstatic struct proc *cryptoretproc; 172108588Ssamstatic void crypto_destroy(void); 173158702Spjdstatic int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint); 174167755Ssamstatic int crypto_kinvoke(struct cryptkop *krp, int flags); 175108588Ssam 176108587Ssamstatic struct cryptostats cryptostats; 177108587SsamSYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats, 178108587Ssam cryptostats, "Crypto system statistics"); 179108587Ssam 180108587Ssam#ifdef CRYPTO_TIMING 181108587Ssamstatic int crypto_timing = 0; 182108587SsamSYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW, 183108587Ssam &crypto_timing, 0, "Enable/disable crypto timing support"); 184108587Ssam#endif 185108587Ssam 186108588Ssamstatic int 187104476Ssamcrypto_init(void) 188104476Ssam{ 189108588Ssam int error; 190108588Ssam 191115746Ssam mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table", 192115746Ssam MTX_DEF|MTX_QUIET); 193108588Ssam 194108588Ssam TAILQ_INIT(&crp_q); 195108588Ssam TAILQ_INIT(&crp_kq); 196115746Ssam mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF); 197108588Ssam 198108588Ssam TAILQ_INIT(&crp_ret_q); 199108588Ssam TAILQ_INIT(&crp_ret_kq); 200115746Ssam mtx_init(&crypto_ret_q_mtx, "crypto", "crypto return queues", MTX_DEF); 201108588Ssam 202104476Ssam cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop), 203104476Ssam 0, 0, 0, 0, 204104476Ssam UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 205104476Ssam cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc), 206104476Ssam 0, 0, 0, 0, 207104476Ssam UMA_ALIGN_PTR, UMA_ZONE_ZINIT); 208108588Ssam if (cryptodesc_zone == NULL || cryptop_zone == NULL) { 209108588Ssam printf("crypto_init: cannot setup crypto zones\n"); 210108588Ssam error = ENOMEM; 211108588Ssam goto bad; 212108588Ssam } 213104476Ssam 214104476Ssam crypto_drivers_num = CRYPTO_DRIVERS_INITIAL; 215104476Ssam crypto_drivers = malloc(crypto_drivers_num * 216104476Ssam sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO); 217108588Ssam if (crypto_drivers == NULL) { 218108588Ssam printf("crypto_init: cannot setup crypto drivers\n"); 219108588Ssam error = ENOMEM; 220108588Ssam goto bad; 221108588Ssam } 222104476Ssam 223172836Sjulian error = kproc_create((void (*)(void *)) crypto_proc, NULL, 224108588Ssam &cryptoproc, 0, 0, "crypto"); 225108588Ssam if (error) { 226108588Ssam printf("crypto_init: cannot start crypto thread; error %d", 227108588Ssam error); 228108588Ssam goto bad; 229108588Ssam } 230104476Ssam 231172836Sjulian error = kproc_create((void (*)(void *)) crypto_ret_proc, NULL, 232108588Ssam &cryptoretproc, 0, 0, "crypto returns"); 233108588Ssam if (error) { 234108588Ssam printf("crypto_init: cannot start cryptoret thread; error %d", 235108588Ssam error); 236108588Ssam goto bad; 237108588Ssam } 238108588Ssam return 0; 239108588Ssambad: 240108588Ssam crypto_destroy(); 241108588Ssam return error; 242104476Ssam} 243104476Ssam 244104476Ssam/* 245108588Ssam * Signal a crypto thread to terminate. We use the driver 246108588Ssam * table lock to synchronize the sleep/wakeups so that we 247108588Ssam * are sure the threads have terminated before we release 248108588Ssam * the data structures they use. See crypto_finis below 249108588Ssam * for the other half of this song-and-dance. 250108588Ssam */ 251108588Ssamstatic void 252108588Ssamcrypto_terminate(struct proc **pp, void *q) 253108588Ssam{ 254108588Ssam struct proc *p; 255108588Ssam 256108588Ssam mtx_assert(&crypto_drivers_mtx, MA_OWNED); 257108588Ssam p = *pp; 258108588Ssam *pp = NULL; 259108588Ssam if (p) { 260108588Ssam wakeup_one(q); 261108588Ssam PROC_LOCK(p); /* NB: insure we don't miss wakeup */ 262108588Ssam CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */ 263108588Ssam msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0); 264108588Ssam PROC_UNLOCK(p); 265108588Ssam CRYPTO_DRIVER_LOCK(); 266108588Ssam } 267108588Ssam} 268108588Ssam 269108588Ssamstatic void 270108588Ssamcrypto_destroy(void) 271108588Ssam{ 272108588Ssam /* 273108588Ssam * Terminate any crypto threads. 274108588Ssam */ 275108588Ssam CRYPTO_DRIVER_LOCK(); 276108588Ssam crypto_terminate(&cryptoproc, &crp_q); 277108588Ssam crypto_terminate(&cryptoretproc, &crp_ret_q); 278108588Ssam CRYPTO_DRIVER_UNLOCK(); 279108588Ssam 280108588Ssam /* XXX flush queues??? */ 281108588Ssam 282108588Ssam /* 283108588Ssam * Reclaim dynamically allocated resources. 284108588Ssam */ 285108588Ssam if (crypto_drivers != NULL) 286108588Ssam free(crypto_drivers, M_CRYPTO_DATA); 287108588Ssam 288108588Ssam if (cryptodesc_zone != NULL) 289108588Ssam uma_zdestroy(cryptodesc_zone); 290108588Ssam if (cryptop_zone != NULL) 291108588Ssam uma_zdestroy(cryptop_zone); 292108588Ssam mtx_destroy(&crypto_q_mtx); 293108588Ssam mtx_destroy(&crypto_ret_q_mtx); 294108588Ssam mtx_destroy(&crypto_drivers_mtx); 295108588Ssam} 296108588Ssam 297167755Ssamstatic struct cryptocap * 298167755Ssamcrypto_checkdriver(u_int32_t hid) 299167755Ssam{ 300167755Ssam if (crypto_drivers == NULL) 301167755Ssam return NULL; 302167755Ssam return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]); 303167755Ssam} 304167755Ssam 305108588Ssam/* 306167755Ssam * Compare a driver's list of supported algorithms against another 307167755Ssam * list; return non-zero if all algorithms are supported. 308105251Smarkm */ 309105251Smarkmstatic int 310167755Ssamdriver_suitable(const struct cryptocap *cap, const struct cryptoini *cri) 311105251Smarkm{ 312167755Ssam const struct cryptoini *cr; 313108588Ssam 314167755Ssam /* See if all the algorithms are supported. */ 315167755Ssam for (cr = cri; cr; cr = cr->cri_next) 316167755Ssam if (cap->cc_alg[cr->cri_alg] == 0) 317167755Ssam return 0; 318167755Ssam return 1; 319105251Smarkm} 320105251Smarkm 321105251Smarkm/* 322167755Ssam * Select a driver for a new session that supports the specified 323167755Ssam * algorithms and, optionally, is constrained according to the flags. 324167755Ssam * The algorithm we use here is pretty stupid; just use the 325167755Ssam * first driver that supports all the algorithms we need. If there 326167755Ssam * are multiple drivers we choose the driver with the fewest active 327167755Ssam * sessions. We prefer hardware-backed drivers to software ones. 328167755Ssam * 329167755Ssam * XXX We need more smarts here (in real life too, but that's 330167755Ssam * XXX another story altogether). 331104476Ssam */ 332167755Ssamstatic struct cryptocap * 333167755Ssamcrypto_select_driver(const struct cryptoini *cri, int flags) 334104476Ssam{ 335167755Ssam struct cryptocap *cap, *best; 336167755Ssam int match, hid; 337104476Ssam 338167755Ssam CRYPTO_DRIVER_ASSERT(); 339104476Ssam 340104476Ssam /* 341167755Ssam * Look first for hardware crypto devices if permitted. 342104476Ssam */ 343167755Ssam if (flags & CRYPTOCAP_F_HARDWARE) 344167755Ssam match = CRYPTOCAP_F_HARDWARE; 345167755Ssam else 346167755Ssam match = CRYPTOCAP_F_SOFTWARE; 347167755Ssam best = NULL; 348167755Ssamagain: 349167755Ssam for (hid = 0; hid < crypto_drivers_num; hid++) { 350167755Ssam cap = &crypto_drivers[hid]; 351167755Ssam /* 352167755Ssam * If it's not initialized, is in the process of 353167755Ssam * going away, or is not appropriate (hardware 354167755Ssam * or software based on match), then skip. 355167755Ssam */ 356167755Ssam if (cap->cc_dev == NULL || 357167755Ssam (cap->cc_flags & CRYPTOCAP_F_CLEANUP) || 358167755Ssam (cap->cc_flags & match) == 0) 359167755Ssam continue; 360104476Ssam 361167755Ssam /* verify all the algorithms are supported. */ 362167755Ssam if (driver_suitable(cap, cri)) { 363167755Ssam if (best == NULL || 364167755Ssam cap->cc_sessions < best->cc_sessions) 365167755Ssam best = cap; 366159240Spjd } 367159240Spjd } 368167755Ssam if (best != NULL) 369167755Ssam return best; 370167755Ssam if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) { 371167755Ssam /* sort of an Algol 68-style for loop */ 372167755Ssam match = CRYPTOCAP_F_SOFTWARE; 373167755Ssam goto again; 374167755Ssam } 375167755Ssam return best; 376167755Ssam} 377104476Ssam 378167755Ssam/* 379167755Ssam * Create a new session. The crid argument specifies a crypto 380167755Ssam * driver to use or constraints on a driver to select (hardware 381167755Ssam * only, software only, either). Whatever driver is selected 382167755Ssam * must be capable of the requested crypto algorithms. 383167755Ssam */ 384167755Ssamint 385167755Ssamcrypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid) 386167755Ssam{ 387167755Ssam struct cryptocap *cap; 388167755Ssam u_int32_t hid, lid; 389167755Ssam int err; 390159240Spjd 391167755Ssam CRYPTO_DRIVER_LOCK(); 392167755Ssam if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) { 393167755Ssam /* 394167755Ssam * Use specified driver; verify it is capable. 395167755Ssam */ 396167755Ssam cap = crypto_checkdriver(crid); 397167755Ssam if (cap != NULL && !driver_suitable(cap, cri)) 398159240Spjd cap = NULL; 399167755Ssam } else { 400167755Ssam /* 401167755Ssam * No requested driver; select based on crid flags. 402167755Ssam */ 403167755Ssam cap = crypto_select_driver(cri, crid); 404167755Ssam /* 405167755Ssam * if NULL then can't do everything in one session. 406167755Ssam * XXX Fix this. We need to inject a "virtual" session 407167755Ssam * XXX layer right about here. 408167755Ssam */ 409104476Ssam } 410159240Spjd if (cap != NULL) { 411159240Spjd /* Call the driver initialization routine. */ 412167755Ssam hid = cap - crypto_drivers; 413159240Spjd lid = hid; /* Pass the driver ID. */ 414167755Ssam err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri); 415159240Spjd if (err == 0) { 416167755Ssam (*sid) = (cap->cc_flags & 0xff000000) 417167755Ssam | (hid & 0x00ffffff); 418159240Spjd (*sid) <<= 32; 419159240Spjd (*sid) |= (lid & 0xffffffff); 420159240Spjd cap->cc_sessions++; 421159240Spjd } 422167755Ssam } else 423167755Ssam err = EINVAL; 424104476Ssam CRYPTO_DRIVER_UNLOCK(); 425104476Ssam return err; 426104476Ssam} 427104476Ssam 428158702Spjdstatic void 429158702Spjdcrypto_remove(struct cryptocap *cap) 430158702Spjd{ 431158702Spjd 432158702Spjd mtx_assert(&crypto_drivers_mtx, MA_OWNED); 433158702Spjd if (cap->cc_sessions == 0 && cap->cc_koperations == 0) 434158702Spjd bzero(cap, sizeof(*cap)); 435158702Spjd} 436158702Spjd 437104476Ssam/* 438104476Ssam * Delete an existing session (or a reserved session on an unregistered 439104476Ssam * driver). 440104476Ssam */ 441104476Ssamint 442104476Ssamcrypto_freesession(u_int64_t sid) 443104476Ssam{ 444158702Spjd struct cryptocap *cap; 445104476Ssam u_int32_t hid; 446104476Ssam int err; 447104476Ssam 448104476Ssam CRYPTO_DRIVER_LOCK(); 449104476Ssam 450104476Ssam if (crypto_drivers == NULL) { 451104476Ssam err = EINVAL; 452104476Ssam goto done; 453104476Ssam } 454104476Ssam 455104476Ssam /* Determine two IDs. */ 456116924Ssam hid = CRYPTO_SESID2HID(sid); 457104476Ssam 458104476Ssam if (hid >= crypto_drivers_num) { 459104476Ssam err = ENOENT; 460104476Ssam goto done; 461104476Ssam } 462158702Spjd cap = &crypto_drivers[hid]; 463104476Ssam 464158702Spjd if (cap->cc_sessions) 465158702Spjd cap->cc_sessions--; 466104476Ssam 467104476Ssam /* Call the driver cleanup routine, if available. */ 468167755Ssam err = CRYPTODEV_FREESESSION(cap->cc_dev, sid); 469104476Ssam 470158702Spjd if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) 471158702Spjd crypto_remove(cap); 472104476Ssam 473104476Ssamdone: 474104476Ssam CRYPTO_DRIVER_UNLOCK(); 475104476Ssam return err; 476104476Ssam} 477104476Ssam 478104476Ssam/* 479104476Ssam * Return an unused driver id. Used by drivers prior to registering 480104476Ssam * support for the algorithms they handle. 481104476Ssam */ 482104476Ssamint32_t 483167755Ssamcrypto_get_driverid(device_t dev, int flags) 484104476Ssam{ 485104476Ssam struct cryptocap *newdrv; 486104476Ssam int i; 487104476Ssam 488167755Ssam if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) { 489167755Ssam printf("%s: no flags specified when registering driver\n", 490167755Ssam device_get_nameunit(dev)); 491167755Ssam return -1; 492167755Ssam } 493167755Ssam 494104476Ssam CRYPTO_DRIVER_LOCK(); 495104476Ssam 496158702Spjd for (i = 0; i < crypto_drivers_num; i++) { 497167755Ssam if (crypto_drivers[i].cc_dev == NULL && 498158702Spjd (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) { 499104476Ssam break; 500158702Spjd } 501158702Spjd } 502104476Ssam 503104476Ssam /* Out of entries, allocate some more. */ 504104476Ssam if (i == crypto_drivers_num) { 505104476Ssam /* Be careful about wrap-around. */ 506104476Ssam if (2 * crypto_drivers_num <= crypto_drivers_num) { 507104476Ssam CRYPTO_DRIVER_UNLOCK(); 508104476Ssam printf("crypto: driver count wraparound!\n"); 509104476Ssam return -1; 510104476Ssam } 511104476Ssam 512104476Ssam newdrv = malloc(2 * crypto_drivers_num * 513104476Ssam sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO); 514104476Ssam if (newdrv == NULL) { 515104476Ssam CRYPTO_DRIVER_UNLOCK(); 516104476Ssam printf("crypto: no space to expand driver table!\n"); 517104476Ssam return -1; 518104476Ssam } 519104476Ssam 520104476Ssam bcopy(crypto_drivers, newdrv, 521104476Ssam crypto_drivers_num * sizeof(struct cryptocap)); 522104476Ssam 523104476Ssam crypto_drivers_num *= 2; 524104476Ssam 525104476Ssam free(crypto_drivers, M_CRYPTO_DATA); 526104476Ssam crypto_drivers = newdrv; 527104476Ssam } 528104476Ssam 529104476Ssam /* NB: state is zero'd on free */ 530104476Ssam crypto_drivers[i].cc_sessions = 1; /* Mark */ 531167755Ssam crypto_drivers[i].cc_dev = dev; 532104476Ssam crypto_drivers[i].cc_flags = flags; 533104476Ssam if (bootverbose) 534167755Ssam printf("crypto: assign %s driver id %u, flags %u\n", 535167755Ssam device_get_nameunit(dev), i, flags); 536104476Ssam 537104476Ssam CRYPTO_DRIVER_UNLOCK(); 538104476Ssam 539104476Ssam return i; 540104476Ssam} 541104476Ssam 542167755Ssam/* 543167755Ssam * Lookup a driver by name. We match against the full device 544167755Ssam * name and unit, and against just the name. The latter gives 545167755Ssam * us a simple widlcarding by device name. On success return the 546167755Ssam * driver/hardware identifier; otherwise return -1. 547167755Ssam */ 548167755Ssamint 549167755Ssamcrypto_find_driver(const char *match) 550104476Ssam{ 551167755Ssam int i, len = strlen(match); 552167755Ssam 553167755Ssam CRYPTO_DRIVER_LOCK(); 554167755Ssam for (i = 0; i < crypto_drivers_num; i++) { 555167755Ssam device_t dev = crypto_drivers[i].cc_dev; 556167755Ssam if (dev == NULL || 557167755Ssam (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP)) 558167755Ssam continue; 559167755Ssam if (strncmp(match, device_get_nameunit(dev), len) == 0 || 560167755Ssam strncmp(match, device_get_name(dev), len) == 0) 561167755Ssam break; 562167755Ssam } 563167755Ssam CRYPTO_DRIVER_UNLOCK(); 564167755Ssam return i < crypto_drivers_num ? i : -1; 565104476Ssam} 566104476Ssam 567104476Ssam/* 568167755Ssam * Return the device_t for the specified driver or NULL 569167755Ssam * if the driver identifier is invalid. 570167755Ssam */ 571167755Ssamdevice_t 572167755Ssamcrypto_find_device_byhid(int hid) 573167755Ssam{ 574167755Ssam struct cryptocap *cap = crypto_checkdriver(hid); 575167755Ssam return cap != NULL ? cap->cc_dev : NULL; 576167755Ssam} 577167755Ssam 578167755Ssam/* 579167755Ssam * Return the device/driver capabilities. 580167755Ssam */ 581167755Ssamint 582167755Ssamcrypto_getcaps(int hid) 583167755Ssam{ 584167755Ssam struct cryptocap *cap = crypto_checkdriver(hid); 585167755Ssam return cap != NULL ? cap->cc_flags : 0; 586167755Ssam} 587167755Ssam 588167755Ssam/* 589104476Ssam * Register support for a key-related algorithm. This routine 590104476Ssam * is called once for each algorithm supported a driver. 591104476Ssam */ 592104476Ssamint 593167755Ssamcrypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags) 594104476Ssam{ 595104476Ssam struct cryptocap *cap; 596104476Ssam int err; 597104476Ssam 598104476Ssam CRYPTO_DRIVER_LOCK(); 599104476Ssam 600104476Ssam cap = crypto_checkdriver(driverid); 601104476Ssam if (cap != NULL && 602104476Ssam (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) { 603104476Ssam /* 604104476Ssam * XXX Do some performance testing to determine placing. 605104476Ssam * XXX We probably need an auxiliary data structure that 606104476Ssam * XXX describes relative performances. 607104476Ssam */ 608104476Ssam 609104476Ssam cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED; 610104476Ssam if (bootverbose) 611167755Ssam printf("crypto: %s registers key alg %u flags %u\n" 612167755Ssam , device_get_nameunit(cap->cc_dev) 613104476Ssam , kalg 614104476Ssam , flags 615104476Ssam ); 616104476Ssam err = 0; 617104476Ssam } else 618104476Ssam err = EINVAL; 619104476Ssam 620104476Ssam CRYPTO_DRIVER_UNLOCK(); 621104476Ssam return err; 622104476Ssam} 623104476Ssam 624104476Ssam/* 625104476Ssam * Register support for a non-key-related algorithm. This routine 626104476Ssam * is called once for each such algorithm supported by a driver. 627104476Ssam */ 628104476Ssamint 629104476Ssamcrypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen, 630167755Ssam u_int32_t flags) 631104476Ssam{ 632104476Ssam struct cryptocap *cap; 633104476Ssam int err; 634104476Ssam 635104476Ssam CRYPTO_DRIVER_LOCK(); 636104476Ssam 637104476Ssam cap = crypto_checkdriver(driverid); 638104476Ssam /* NB: algorithms are in the range [1..max] */ 639104476Ssam if (cap != NULL && 640104476Ssam (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) { 641104476Ssam /* 642104476Ssam * XXX Do some performance testing to determine placing. 643104476Ssam * XXX We probably need an auxiliary data structure that 644104476Ssam * XXX describes relative performances. 645104476Ssam */ 646104476Ssam 647104476Ssam cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED; 648104476Ssam cap->cc_max_op_len[alg] = maxoplen; 649104476Ssam if (bootverbose) 650167755Ssam printf("crypto: %s registers alg %u flags %u maxoplen %u\n" 651167755Ssam , device_get_nameunit(cap->cc_dev) 652104476Ssam , alg 653104476Ssam , flags 654104476Ssam , maxoplen 655104476Ssam ); 656167755Ssam cap->cc_sessions = 0; /* Unmark */ 657104476Ssam err = 0; 658104476Ssam } else 659104476Ssam err = EINVAL; 660104476Ssam 661104476Ssam CRYPTO_DRIVER_UNLOCK(); 662104476Ssam return err; 663104476Ssam} 664104476Ssam 665167755Ssamstatic void 666167755Ssamdriver_finis(struct cryptocap *cap) 667167755Ssam{ 668167755Ssam u_int32_t ses, kops; 669167755Ssam 670167755Ssam CRYPTO_DRIVER_ASSERT(); 671167755Ssam 672167755Ssam ses = cap->cc_sessions; 673167755Ssam kops = cap->cc_koperations; 674167755Ssam bzero(cap, sizeof(*cap)); 675167755Ssam if (ses != 0 || kops != 0) { 676167755Ssam /* 677167755Ssam * If there are pending sessions, 678167755Ssam * just mark as invalid. 679167755Ssam */ 680167755Ssam cap->cc_flags |= CRYPTOCAP_F_CLEANUP; 681167755Ssam cap->cc_sessions = ses; 682167755Ssam cap->cc_koperations = kops; 683167755Ssam } 684167755Ssam} 685167755Ssam 686104476Ssam/* 687104476Ssam * Unregister a crypto driver. If there are pending sessions using it, 688104476Ssam * leave enough information around so that subsequent calls using those 689104476Ssam * sessions will correctly detect the driver has been unregistered and 690104476Ssam * reroute requests. 691104476Ssam */ 692104476Ssamint 693104476Ssamcrypto_unregister(u_int32_t driverid, int alg) 694104476Ssam{ 695158702Spjd struct cryptocap *cap; 696104476Ssam int i, err; 697104476Ssam 698104476Ssam CRYPTO_DRIVER_LOCK(); 699104476Ssam cap = crypto_checkdriver(driverid); 700104476Ssam if (cap != NULL && 701104476Ssam (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) && 702104476Ssam cap->cc_alg[alg] != 0) { 703104476Ssam cap->cc_alg[alg] = 0; 704104476Ssam cap->cc_max_op_len[alg] = 0; 705104476Ssam 706104476Ssam /* Was this the last algorithm ? */ 707104476Ssam for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++) 708104476Ssam if (cap->cc_alg[i] != 0) 709104476Ssam break; 710104476Ssam 711167755Ssam if (i == CRYPTO_ALGORITHM_MAX + 1) 712167755Ssam driver_finis(cap); 713104476Ssam err = 0; 714104476Ssam } else 715104476Ssam err = EINVAL; 716167755Ssam CRYPTO_DRIVER_UNLOCK(); 717104476Ssam 718104476Ssam return err; 719104476Ssam} 720104476Ssam 721104476Ssam/* 722104476Ssam * Unregister all algorithms associated with a crypto driver. 723104476Ssam * If there are pending sessions using it, leave enough information 724104476Ssam * around so that subsequent calls using those sessions will 725104476Ssam * correctly detect the driver has been unregistered and reroute 726104476Ssam * requests. 727104476Ssam */ 728104476Ssamint 729104476Ssamcrypto_unregister_all(u_int32_t driverid) 730104476Ssam{ 731158702Spjd struct cryptocap *cap; 732167755Ssam int err; 733104476Ssam 734104476Ssam CRYPTO_DRIVER_LOCK(); 735104476Ssam cap = crypto_checkdriver(driverid); 736104476Ssam if (cap != NULL) { 737167755Ssam driver_finis(cap); 738104476Ssam err = 0; 739104476Ssam } else 740104476Ssam err = EINVAL; 741167755Ssam CRYPTO_DRIVER_UNLOCK(); 742104476Ssam 743104476Ssam return err; 744104476Ssam} 745104476Ssam 746104476Ssam/* 747104476Ssam * Clear blockage on a driver. The what parameter indicates whether 748104476Ssam * the driver is now ready for cryptop's and/or cryptokop's. 749104476Ssam */ 750104476Ssamint 751104476Ssamcrypto_unblock(u_int32_t driverid, int what) 752104476Ssam{ 753104476Ssam struct cryptocap *cap; 754158827Spjd int err; 755104476Ssam 756104476Ssam CRYPTO_Q_LOCK(); 757104476Ssam cap = crypto_checkdriver(driverid); 758104476Ssam if (cap != NULL) { 759158827Spjd if (what & CRYPTO_SYMQ) 760104476Ssam cap->cc_qblocked = 0; 761158827Spjd if (what & CRYPTO_ASYMQ) 762104476Ssam cap->cc_kqblocked = 0; 763158827Spjd if (crp_sleep) 764104628Ssam wakeup_one(&crp_q); 765104476Ssam err = 0; 766104476Ssam } else 767104476Ssam err = EINVAL; 768104476Ssam CRYPTO_Q_UNLOCK(); 769104476Ssam 770104476Ssam return err; 771104476Ssam} 772104476Ssam 773104476Ssam/* 774104476Ssam * Add a crypto request to a queue, to be processed by the kernel thread. 775104476Ssam */ 776104476Ssamint 777104476Ssamcrypto_dispatch(struct cryptop *crp) 778104476Ssam{ 779158702Spjd struct cryptocap *cap; 780158702Spjd u_int32_t hid; 781158702Spjd int result; 782104476Ssam 783108587Ssam cryptostats.cs_ops++; 784108587Ssam 785108587Ssam#ifdef CRYPTO_TIMING 786108587Ssam if (crypto_timing) 787108587Ssam binuptime(&crp->crp_tstamp); 788108587Ssam#endif 789108587Ssam 790158702Spjd hid = CRYPTO_SESID2HID(crp->crp_sid); 791158702Spjd 792111297Ssam if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) { 793111297Ssam /* 794111297Ssam * Caller marked the request to be processed 795111297Ssam * immediately; dispatch it directly to the 796111297Ssam * driver unless the driver is currently blocked. 797111297Ssam */ 798111297Ssam cap = crypto_checkdriver(hid); 799158702Spjd /* Driver cannot disappeared when there is an active session. */ 800158702Spjd KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__)); 801158702Spjd if (!cap->cc_qblocked) { 802158702Spjd result = crypto_invoke(cap, crp, 0); 803158702Spjd if (result != ERESTART) 804158702Spjd return (result); 805158823Spjd /* 806158823Spjd * The driver ran out of resources, put the request on 807158823Spjd * the queue. 808158823Spjd */ 809108990Ssam } 810108990Ssam } 811158702Spjd CRYPTO_Q_LOCK(); 812158827Spjd TAILQ_INSERT_TAIL(&crp_q, crp, crp_next); 813158827Spjd if (crp_sleep) 814157665Spjd wakeup_one(&crp_q); 815115746Ssam CRYPTO_Q_UNLOCK(); 816158702Spjd return 0; 817104476Ssam} 818104476Ssam 819104476Ssam/* 820104476Ssam * Add an asymetric crypto request to a queue, 821104476Ssam * to be processed by the kernel thread. 822104476Ssam */ 823104476Ssamint 824104476Ssamcrypto_kdispatch(struct cryptkop *krp) 825104476Ssam{ 826167755Ssam int error; 827104476Ssam 828108587Ssam cryptostats.cs_kops++; 829108587Ssam 830167755Ssam error = crypto_kinvoke(krp, krp->krp_crid); 831167755Ssam if (error == ERESTART) { 832167755Ssam CRYPTO_Q_LOCK(); 833167755Ssam TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next); 834167755Ssam if (crp_sleep) 835167755Ssam wakeup_one(&crp_q); 836167755Ssam CRYPTO_Q_UNLOCK(); 837167755Ssam error = 0; 838167755Ssam } 839167755Ssam return error; 840167755Ssam} 841104476Ssam 842167755Ssam/* 843167755Ssam * Verify a driver is suitable for the specified operation. 844167755Ssam */ 845167755Ssamstatic __inline int 846167755Ssamkdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp) 847167755Ssam{ 848167755Ssam return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0; 849104476Ssam} 850104476Ssam 851104476Ssam/* 852167755Ssam * Select a driver for an asym operation. The driver must 853167755Ssam * support the necessary algorithm. The caller can constrain 854167755Ssam * which device is selected with the flags parameter. The 855167755Ssam * algorithm we use here is pretty stupid; just use the first 856167755Ssam * driver that supports the algorithms we need. If there are 857167755Ssam * multiple suitable drivers we choose the driver with the 858167755Ssam * fewest active operations. We prefer hardware-backed 859167755Ssam * drivers to software ones when either may be used. 860104476Ssam */ 861167755Ssamstatic struct cryptocap * 862167755Ssamcrypto_select_kdriver(const struct cryptkop *krp, int flags) 863167755Ssam{ 864167755Ssam struct cryptocap *cap, *best, *blocked; 865167755Ssam int match, hid; 866167755Ssam 867167755Ssam CRYPTO_DRIVER_ASSERT(); 868167755Ssam 869167755Ssam /* 870167755Ssam * Look first for hardware crypto devices if permitted. 871167755Ssam */ 872167755Ssam if (flags & CRYPTOCAP_F_HARDWARE) 873167755Ssam match = CRYPTOCAP_F_HARDWARE; 874167755Ssam else 875167755Ssam match = CRYPTOCAP_F_SOFTWARE; 876167755Ssam best = NULL; 877167755Ssam blocked = NULL; 878167755Ssamagain: 879167755Ssam for (hid = 0; hid < crypto_drivers_num; hid++) { 880167755Ssam cap = &crypto_drivers[hid]; 881167755Ssam /* 882167755Ssam * If it's not initialized, is in the process of 883167755Ssam * going away, or is not appropriate (hardware 884167755Ssam * or software based on match), then skip. 885167755Ssam */ 886167755Ssam if (cap->cc_dev == NULL || 887167755Ssam (cap->cc_flags & CRYPTOCAP_F_CLEANUP) || 888167755Ssam (cap->cc_flags & match) == 0) 889167755Ssam continue; 890167755Ssam 891167755Ssam /* verify all the algorithms are supported. */ 892167755Ssam if (kdriver_suitable(cap, krp)) { 893167755Ssam if (best == NULL || 894167755Ssam cap->cc_koperations < best->cc_koperations) 895167755Ssam best = cap; 896167755Ssam } 897167755Ssam } 898167755Ssam if (best != NULL) 899167755Ssam return best; 900167755Ssam if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) { 901167755Ssam /* sort of an Algol 68-style for loop */ 902167755Ssam match = CRYPTOCAP_F_SOFTWARE; 903167755Ssam goto again; 904167755Ssam } 905167755Ssam return best; 906167755Ssam} 907167755Ssam 908167755Ssam/* 909167755Ssam * Dispatch an assymetric crypto request. 910167755Ssam */ 911104476Ssamstatic int 912167755Ssamcrypto_kinvoke(struct cryptkop *krp, int crid) 913104476Ssam{ 914158702Spjd struct cryptocap *cap = NULL; 915167755Ssam int error; 916104476Ssam 917158702Spjd KASSERT(krp != NULL, ("%s: krp == NULL", __func__)); 918158702Spjd KASSERT(krp->krp_callback != NULL, 919158702Spjd ("%s: krp->crp_callback == NULL", __func__)); 920104476Ssam 921158702Spjd CRYPTO_DRIVER_LOCK(); 922167755Ssam if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) { 923167755Ssam cap = crypto_checkdriver(crid); 924167755Ssam if (cap != NULL) { 925167755Ssam /* 926167755Ssam * Driver present, it must support the necessary 927167755Ssam * algorithm and, if s/w drivers are excluded, 928167755Ssam * it must be registered as hardware-backed. 929167755Ssam */ 930167755Ssam if (!kdriver_suitable(cap, krp) || 931167755Ssam (!crypto_devallowsoft && 932167755Ssam (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0)) 933167755Ssam cap = NULL; 934158702Spjd } 935167755Ssam } else { 936167755Ssam /* 937167755Ssam * No requested driver; select based on crid flags. 938167755Ssam */ 939167755Ssam if (!crypto_devallowsoft) /* NB: disallow s/w drivers */ 940167755Ssam crid &= ~CRYPTOCAP_F_SOFTWARE; 941167755Ssam cap = crypto_select_kdriver(krp, crid); 942104476Ssam } 943167755Ssam if (cap != NULL && !cap->cc_kqblocked) { 944167755Ssam krp->krp_hid = cap - crypto_drivers; 945158702Spjd cap->cc_koperations++; 946158702Spjd CRYPTO_DRIVER_UNLOCK(); 947167755Ssam error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0); 948158702Spjd CRYPTO_DRIVER_LOCK(); 949158702Spjd if (error == ERESTART) { 950158702Spjd cap->cc_koperations--; 951158702Spjd CRYPTO_DRIVER_UNLOCK(); 952158702Spjd return (error); 953158702Spjd } 954158702Spjd } else { 955167755Ssam /* 956167755Ssam * NB: cap is !NULL if device is blocked; in 957167755Ssam * that case return ERESTART so the operation 958167755Ssam * is resubmitted if possible. 959167755Ssam */ 960167755Ssam error = (cap == NULL) ? ENODEV : ERESTART; 961158702Spjd } 962158702Spjd CRYPTO_DRIVER_UNLOCK(); 963104476Ssam 964104476Ssam if (error) { 965104476Ssam krp->krp_status = error; 966104628Ssam crypto_kdone(krp); 967104476Ssam } 968104476Ssam return 0; 969104476Ssam} 970104476Ssam 971108587Ssam#ifdef CRYPTO_TIMING 972108587Ssamstatic void 973108587Ssamcrypto_tstat(struct cryptotstat *ts, struct bintime *bt) 974108587Ssam{ 975108587Ssam struct bintime now, delta; 976108587Ssam struct timespec t; 977108587Ssam uint64_t u; 978108587Ssam 979108587Ssam binuptime(&now); 980108587Ssam u = now.frac; 981108587Ssam delta.frac = now.frac - bt->frac; 982108587Ssam delta.sec = now.sec - bt->sec; 983108587Ssam if (u < delta.frac) 984108587Ssam delta.sec--; 985108587Ssam bintime2timespec(&delta, &t); 986108587Ssam timespecadd(&ts->acc, &t); 987108587Ssam if (timespeccmp(&t, &ts->min, <)) 988108587Ssam ts->min = t; 989108587Ssam if (timespeccmp(&t, &ts->max, >)) 990108587Ssam ts->max = t; 991108587Ssam ts->count++; 992108587Ssam 993108587Ssam *bt = now; 994108587Ssam} 995108587Ssam#endif 996108587Ssam 997104476Ssam/* 998104476Ssam * Dispatch a crypto request to the appropriate crypto devices. 999104476Ssam */ 1000104476Ssamstatic int 1001158702Spjdcrypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint) 1002104476Ssam{ 1003104476Ssam 1004158702Spjd KASSERT(crp != NULL, ("%s: crp == NULL", __func__)); 1005158702Spjd KASSERT(crp->crp_callback != NULL, 1006158702Spjd ("%s: crp->crp_callback == NULL", __func__)); 1007158702Spjd KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__)); 1008158702Spjd 1009108587Ssam#ifdef CRYPTO_TIMING 1010108587Ssam if (crypto_timing) 1011108587Ssam crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp); 1012108587Ssam#endif 1013158702Spjd if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) { 1014104476Ssam struct cryptodesc *crd; 1015104476Ssam u_int64_t nid; 1016104476Ssam 1017104476Ssam /* 1018104476Ssam * Driver has unregistered; migrate the session and return 1019104476Ssam * an error to the caller so they'll resubmit the op. 1020158702Spjd * 1021158702Spjd * XXX: What if there are more already queued requests for this 1022158702Spjd * session? 1023104476Ssam */ 1024158702Spjd crypto_freesession(crp->crp_sid); 1025158702Spjd 1026104476Ssam for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next) 1027104476Ssam crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI); 1028104476Ssam 1029167755Ssam /* XXX propagate flags from initial session? */ 1030167755Ssam if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 1031167755Ssam CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0) 1032104476Ssam crp->crp_sid = nid; 1033104476Ssam 1034104476Ssam crp->crp_etype = EAGAIN; 1035104628Ssam crypto_done(crp); 1036104476Ssam return 0; 1037104476Ssam } else { 1038104476Ssam /* 1039104476Ssam * Invoke the driver to process the request. 1040104476Ssam */ 1041167755Ssam return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint); 1042104476Ssam } 1043104476Ssam} 1044104476Ssam 1045104476Ssam/* 1046104476Ssam * Release a set of crypto descriptors. 1047104476Ssam */ 1048104476Ssamvoid 1049104476Ssamcrypto_freereq(struct cryptop *crp) 1050104476Ssam{ 1051104476Ssam struct cryptodesc *crd; 1052104476Ssam 1053104476Ssam if (crp == NULL) 1054104476Ssam return; 1055104476Ssam 1056159346Spjd#ifdef DIAGNOSTIC 1057159346Spjd { 1058159346Spjd struct cryptop *crp2; 1059159346Spjd 1060159346Spjd CRYPTO_Q_LOCK(); 1061159346Spjd TAILQ_FOREACH(crp2, &crp_q, crp_next) { 1062159346Spjd KASSERT(crp2 != crp, 1063159346Spjd ("Freeing cryptop from the crypto queue (%p).", 1064159346Spjd crp)); 1065159346Spjd } 1066159346Spjd CRYPTO_Q_UNLOCK(); 1067159346Spjd CRYPTO_RETQ_LOCK(); 1068159346Spjd TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) { 1069159346Spjd KASSERT(crp2 != crp, 1070159346Spjd ("Freeing cryptop from the return queue (%p).", 1071159346Spjd crp)); 1072159346Spjd } 1073159346Spjd CRYPTO_RETQ_UNLOCK(); 1074159346Spjd } 1075159346Spjd#endif 1076159346Spjd 1077104476Ssam while ((crd = crp->crp_desc) != NULL) { 1078104476Ssam crp->crp_desc = crd->crd_next; 1079104476Ssam uma_zfree(cryptodesc_zone, crd); 1080104476Ssam } 1081104476Ssam uma_zfree(cryptop_zone, crp); 1082104476Ssam} 1083104476Ssam 1084104476Ssam/* 1085104476Ssam * Acquire a set of crypto descriptors. 1086104476Ssam */ 1087104476Ssamstruct cryptop * 1088104476Ssamcrypto_getreq(int num) 1089104476Ssam{ 1090104476Ssam struct cryptodesc *crd; 1091104476Ssam struct cryptop *crp; 1092104476Ssam 1093108813Ssam crp = uma_zalloc(cryptop_zone, M_NOWAIT|M_ZERO); 1094104476Ssam if (crp != NULL) { 1095104476Ssam while (num--) { 1096108813Ssam crd = uma_zalloc(cryptodesc_zone, M_NOWAIT|M_ZERO); 1097104476Ssam if (crd == NULL) { 1098104476Ssam crypto_freereq(crp); 1099104476Ssam return NULL; 1100104476Ssam } 1101104476Ssam 1102104476Ssam crd->crd_next = crp->crp_desc; 1103104476Ssam crp->crp_desc = crd; 1104104476Ssam } 1105104476Ssam } 1106104476Ssam return crp; 1107104476Ssam} 1108104476Ssam 1109104476Ssam/* 1110104476Ssam * Invoke the callback on behalf of the driver. 1111104476Ssam */ 1112104476Ssamvoid 1113104476Ssamcrypto_done(struct cryptop *crp) 1114104476Ssam{ 1115115746Ssam KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0, 1116115746Ssam ("crypto_done: op already done, flags 0x%x", crp->crp_flags)); 1117115746Ssam crp->crp_flags |= CRYPTO_F_DONE; 1118108587Ssam if (crp->crp_etype != 0) 1119108587Ssam cryptostats.cs_errs++; 1120108587Ssam#ifdef CRYPTO_TIMING 1121108587Ssam if (crypto_timing) 1122108587Ssam crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp); 1123108587Ssam#endif 1124117058Ssam /* 1125117058Ssam * CBIMM means unconditionally do the callback immediately; 1126117058Ssam * CBIFSYNC means do the callback immediately only if the 1127117058Ssam * operation was done synchronously. Both are used to avoid 1128117058Ssam * doing extraneous context switches; the latter is mostly 1129117058Ssam * used with the software crypto driver. 1130117058Ssam */ 1131117058Ssam if ((crp->crp_flags & CRYPTO_F_CBIMM) || 1132117058Ssam ((crp->crp_flags & CRYPTO_F_CBIFSYNC) && 1133117058Ssam (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) { 1134111297Ssam /* 1135111297Ssam * Do the callback directly. This is ok when the 1136111297Ssam * callback routine does very little (e.g. the 1137111297Ssam * /dev/crypto callback method just does a wakeup). 1138111297Ssam */ 1139111297Ssam#ifdef CRYPTO_TIMING 1140111297Ssam if (crypto_timing) { 1141111297Ssam /* 1142111297Ssam * NB: We must copy the timestamp before 1143111297Ssam * doing the callback as the cryptop is 1144111297Ssam * likely to be reclaimed. 1145111297Ssam */ 1146111297Ssam struct bintime t = crp->crp_tstamp; 1147111297Ssam crypto_tstat(&cryptostats.cs_cb, &t); 1148111297Ssam crp->crp_callback(crp); 1149111297Ssam crypto_tstat(&cryptostats.cs_finis, &t); 1150111297Ssam } else 1151111297Ssam#endif 1152111297Ssam crp->crp_callback(crp); 1153111297Ssam } else { 1154111297Ssam /* 1155111297Ssam * Normal case; queue the callback for the thread. 1156111297Ssam */ 1157111297Ssam CRYPTO_RETQ_LOCK(); 1158158826Spjd if (CRYPTO_RETQ_EMPTY()) 1159158702Spjd wakeup_one(&crp_ret_q); /* shared wait channel */ 1160111297Ssam TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next); 1161111297Ssam CRYPTO_RETQ_UNLOCK(); 1162111297Ssam } 1163104476Ssam} 1164104476Ssam 1165104476Ssam/* 1166104476Ssam * Invoke the callback on behalf of the driver. 1167104476Ssam */ 1168104476Ssamvoid 1169104476Ssamcrypto_kdone(struct cryptkop *krp) 1170104476Ssam{ 1171158702Spjd struct cryptocap *cap; 1172104476Ssam 1173108587Ssam if (krp->krp_status != 0) 1174108587Ssam cryptostats.cs_kerrs++; 1175158702Spjd CRYPTO_DRIVER_LOCK(); 1176158702Spjd /* XXX: What if driver is loaded in the meantime? */ 1177158702Spjd if (krp->krp_hid < crypto_drivers_num) { 1178158702Spjd cap = &crypto_drivers[krp->krp_hid]; 1179158702Spjd cap->cc_koperations--; 1180158702Spjd KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0")); 1181158702Spjd if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) 1182158702Spjd crypto_remove(cap); 1183158702Spjd } 1184158702Spjd CRYPTO_DRIVER_UNLOCK(); 1185104476Ssam CRYPTO_RETQ_LOCK(); 1186158826Spjd if (CRYPTO_RETQ_EMPTY()) 1187158702Spjd wakeup_one(&crp_ret_q); /* shared wait channel */ 1188104476Ssam TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next); 1189104628Ssam CRYPTO_RETQ_UNLOCK(); 1190104476Ssam} 1191104476Ssam 1192104476Ssamint 1193104476Ssamcrypto_getfeat(int *featp) 1194104476Ssam{ 1195104476Ssam int hid, kalg, feat = 0; 1196104476Ssam 1197104476Ssam CRYPTO_DRIVER_LOCK(); 1198104476Ssam for (hid = 0; hid < crypto_drivers_num; hid++) { 1199167755Ssam const struct cryptocap *cap = &crypto_drivers[hid]; 1200167755Ssam 1201167755Ssam if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) && 1202104476Ssam !crypto_devallowsoft) { 1203104476Ssam continue; 1204104476Ssam } 1205104476Ssam for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++) 1206167755Ssam if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED) 1207104476Ssam feat |= 1 << kalg; 1208104476Ssam } 1209104476Ssam CRYPTO_DRIVER_UNLOCK(); 1210104476Ssam *featp = feat; 1211104476Ssam return (0); 1212104476Ssam} 1213104476Ssam 1214108588Ssam/* 1215108588Ssam * Terminate a thread at module unload. The process that 1216108588Ssam * initiated this is waiting for us to signal that we're gone; 1217108588Ssam * wake it up and exit. We use the driver table lock to insure 1218108588Ssam * we don't do the wakeup before they're waiting. There is no 1219108588Ssam * race here because the waiter sleeps on the proc lock for the 1220108588Ssam * thread so it gets notified at the right time because of an 1221108588Ssam * extra wakeup that's done in exit1(). 1222108588Ssam */ 1223104476Ssamstatic void 1224108588Ssamcrypto_finis(void *chan) 1225104476Ssam{ 1226108588Ssam CRYPTO_DRIVER_LOCK(); 1227108588Ssam wakeup_one(chan); 1228108588Ssam CRYPTO_DRIVER_UNLOCK(); 1229172836Sjulian kproc_exit(0); 1230104476Ssam} 1231104476Ssam 1232104476Ssam/* 1233104628Ssam * Crypto thread, dispatches crypto requests. 1234104476Ssam */ 1235104476Ssamstatic void 1236104476Ssamcrypto_proc(void) 1237104476Ssam{ 1238104628Ssam struct cryptop *crp, *submit; 1239104628Ssam struct cryptkop *krp; 1240104476Ssam struct cryptocap *cap; 1241158702Spjd u_int32_t hid; 1242104476Ssam int result, hint; 1243104476Ssam 1244104628Ssam CRYPTO_Q_LOCK(); 1245104476Ssam for (;;) { 1246104476Ssam /* 1247104476Ssam * Find the first element in the queue that can be 1248104476Ssam * processed and look-ahead to see if multiple ops 1249104476Ssam * are ready for the same driver. 1250104476Ssam */ 1251104476Ssam submit = NULL; 1252104476Ssam hint = 0; 1253104476Ssam TAILQ_FOREACH(crp, &crp_q, crp_next) { 1254158702Spjd hid = CRYPTO_SESID2HID(crp->crp_sid); 1255104476Ssam cap = crypto_checkdriver(hid); 1256158702Spjd /* 1257158702Spjd * Driver cannot disappeared when there is an active 1258158702Spjd * session. 1259158702Spjd */ 1260158716Spjd KASSERT(cap != NULL, ("%s:%u Driver disappeared.", 1261158716Spjd __func__, __LINE__)); 1262167755Ssam if (cap == NULL || cap->cc_dev == NULL) { 1263104476Ssam /* Op needs to be migrated, process it. */ 1264104476Ssam if (submit == NULL) 1265104476Ssam submit = crp; 1266104476Ssam break; 1267104476Ssam } 1268104476Ssam if (!cap->cc_qblocked) { 1269104476Ssam if (submit != NULL) { 1270104476Ssam /* 1271104476Ssam * We stop on finding another op, 1272104476Ssam * regardless whether its for the same 1273104476Ssam * driver or not. We could keep 1274104476Ssam * searching the queue but it might be 1275104476Ssam * better to just use a per-driver 1276104476Ssam * queue instead. 1277104476Ssam */ 1278116924Ssam if (CRYPTO_SESID2HID(submit->crp_sid) == hid) 1279104476Ssam hint = CRYPTO_HINT_MORE; 1280104476Ssam break; 1281104476Ssam } else { 1282104476Ssam submit = crp; 1283111297Ssam if ((submit->crp_flags & CRYPTO_F_BATCH) == 0) 1284104476Ssam break; 1285104476Ssam /* keep scanning for more are q'd */ 1286104476Ssam } 1287104476Ssam } 1288104476Ssam } 1289104476Ssam if (submit != NULL) { 1290104476Ssam TAILQ_REMOVE(&crp_q, submit, crp_next); 1291158702Spjd hid = CRYPTO_SESID2HID(submit->crp_sid); 1292158702Spjd cap = crypto_checkdriver(hid); 1293158716Spjd KASSERT(cap != NULL, ("%s:%u Driver disappeared.", 1294158716Spjd __func__, __LINE__)); 1295158702Spjd result = crypto_invoke(cap, submit, hint); 1296104476Ssam if (result == ERESTART) { 1297104476Ssam /* 1298104476Ssam * The driver ran out of resources, mark the 1299104476Ssam * driver ``blocked'' for cryptop's and put 1300104476Ssam * the request back in the queue. It would 1301104476Ssam * best to put the request back where we got 1302104476Ssam * it but that's hard so for now we put it 1303104476Ssam * at the front. This should be ok; putting 1304104476Ssam * it at the end does not work. 1305104476Ssam */ 1306104476Ssam /* XXX validate sid again? */ 1307116924Ssam crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1; 1308104476Ssam TAILQ_INSERT_HEAD(&crp_q, submit, crp_next); 1309108587Ssam cryptostats.cs_blocks++; 1310104476Ssam } 1311104476Ssam } 1312104476Ssam 1313104476Ssam /* As above, but for key ops */ 1314104476Ssam TAILQ_FOREACH(krp, &crp_kq, krp_next) { 1315104476Ssam cap = crypto_checkdriver(krp->krp_hid); 1316167755Ssam if (cap == NULL || cap->cc_dev == NULL) { 1317167755Ssam /* 1318167755Ssam * Operation needs to be migrated, invalidate 1319167755Ssam * the assigned device so it will reselect a 1320167755Ssam * new one below. Propagate the original 1321167755Ssam * crid selection flags if supplied. 1322167755Ssam */ 1323167755Ssam krp->krp_hid = krp->krp_crid & 1324167755Ssam (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE); 1325167755Ssam if (krp->krp_hid == 0) 1326167755Ssam krp->krp_hid = 1327167755Ssam CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE; 1328104476Ssam break; 1329104476Ssam } 1330104476Ssam if (!cap->cc_kqblocked) 1331104476Ssam break; 1332104476Ssam } 1333104476Ssam if (krp != NULL) { 1334104476Ssam TAILQ_REMOVE(&crp_kq, krp, krp_next); 1335167755Ssam result = crypto_kinvoke(krp, krp->krp_hid); 1336104476Ssam if (result == ERESTART) { 1337104476Ssam /* 1338104476Ssam * The driver ran out of resources, mark the 1339104476Ssam * driver ``blocked'' for cryptkop's and put 1340104476Ssam * the request back in the queue. It would 1341104476Ssam * best to put the request back where we got 1342104476Ssam * it but that's hard so for now we put it 1343104476Ssam * at the front. This should be ok; putting 1344104476Ssam * it at the end does not work. 1345104476Ssam */ 1346104476Ssam /* XXX validate sid again? */ 1347104476Ssam crypto_drivers[krp->krp_hid].cc_kqblocked = 1; 1348104476Ssam TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next); 1349108587Ssam cryptostats.cs_kblocks++; 1350104476Ssam } 1351104476Ssam } 1352104476Ssam 1353104628Ssam if (submit == NULL && krp == NULL) { 1354104476Ssam /* 1355104476Ssam * Nothing more to be processed. Sleep until we're 1356104476Ssam * woken because there are more ops to process. 1357104476Ssam * This happens either by submission or by a driver 1358104476Ssam * becoming unblocked and notifying us through 1359104476Ssam * crypto_unblock. Note that when we wakeup we 1360104476Ssam * start processing each queue again from the 1361104476Ssam * front. It's not clear that it's important to 1362104476Ssam * preserve this ordering since ops may finish 1363104476Ssam * out of order if dispatched to different devices 1364104476Ssam * and some become blocked while others do not. 1365104476Ssam */ 1366158827Spjd crp_sleep = 1; 1367104628Ssam msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0); 1368158827Spjd crp_sleep = 0; 1369108588Ssam if (cryptoproc == NULL) 1370108588Ssam break; 1371108587Ssam cryptostats.cs_intrs++; 1372104476Ssam } 1373104476Ssam } 1374108588Ssam CRYPTO_Q_UNLOCK(); 1375104628Ssam 1376108588Ssam crypto_finis(&crp_q); 1377104628Ssam} 1378104628Ssam 1379104628Ssam/* 1380104628Ssam * Crypto returns thread, does callbacks for processed crypto requests. 1381104628Ssam * Callbacks are done here, rather than in the crypto drivers, because 1382104628Ssam * callbacks typically are expensive and would slow interrupt handling. 1383104628Ssam */ 1384104628Ssamstatic void 1385104628Ssamcrypto_ret_proc(void) 1386104628Ssam{ 1387104628Ssam struct cryptop *crpt; 1388104628Ssam struct cryptkop *krpt; 1389104628Ssam 1390104628Ssam CRYPTO_RETQ_LOCK(); 1391104628Ssam for (;;) { 1392104628Ssam /* Harvest return q's for completed ops */ 1393104628Ssam crpt = TAILQ_FIRST(&crp_ret_q); 1394104628Ssam if (crpt != NULL) 1395104628Ssam TAILQ_REMOVE(&crp_ret_q, crpt, crp_next); 1396104628Ssam 1397104628Ssam krpt = TAILQ_FIRST(&crp_ret_kq); 1398104628Ssam if (krpt != NULL) 1399104628Ssam TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next); 1400104628Ssam 1401104628Ssam if (crpt != NULL || krpt != NULL) { 1402104628Ssam CRYPTO_RETQ_UNLOCK(); 1403104628Ssam /* 1404104628Ssam * Run callbacks unlocked. 1405104628Ssam */ 1406108587Ssam if (crpt != NULL) { 1407108587Ssam#ifdef CRYPTO_TIMING 1408108587Ssam if (crypto_timing) { 1409108587Ssam /* 1410108587Ssam * NB: We must copy the timestamp before 1411108587Ssam * doing the callback as the cryptop is 1412108587Ssam * likely to be reclaimed. 1413108587Ssam */ 1414108587Ssam struct bintime t = crpt->crp_tstamp; 1415108587Ssam crypto_tstat(&cryptostats.cs_cb, &t); 1416108587Ssam crpt->crp_callback(crpt); 1417108587Ssam crypto_tstat(&cryptostats.cs_finis, &t); 1418108587Ssam } else 1419108587Ssam#endif 1420108587Ssam crpt->crp_callback(crpt); 1421108587Ssam } 1422104628Ssam if (krpt != NULL) 1423104628Ssam krpt->krp_callback(krpt); 1424104628Ssam CRYPTO_RETQ_LOCK(); 1425104628Ssam } else { 1426104628Ssam /* 1427104628Ssam * Nothing more to be processed. Sleep until we're 1428104628Ssam * woken because there are more returns to process. 1429104628Ssam */ 1430104628Ssam msleep(&crp_ret_q, &crypto_ret_q_mtx, PWAIT, 1431104628Ssam "crypto_ret_wait", 0); 1432108588Ssam if (cryptoretproc == NULL) 1433108588Ssam break; 1434108587Ssam cryptostats.cs_rets++; 1435104628Ssam } 1436104628Ssam } 1437108588Ssam CRYPTO_RETQ_UNLOCK(); 1438108588Ssam 1439108588Ssam crypto_finis(&crp_ret_q); 1440104628Ssam} 1441167755Ssam 1442167755Ssam#ifdef DDB 1443167755Ssamstatic void 1444167755Ssamdb_show_drivers(void) 1445167755Ssam{ 1446167755Ssam int hid; 1447167755Ssam 1448167755Ssam db_printf("%12s %4s %4s %8s %2s %2s\n" 1449167755Ssam , "Device" 1450167755Ssam , "Ses" 1451167755Ssam , "Kops" 1452167755Ssam , "Flags" 1453167755Ssam , "QB" 1454167755Ssam , "KB" 1455167755Ssam ); 1456167755Ssam for (hid = 0; hid < crypto_drivers_num; hid++) { 1457167755Ssam const struct cryptocap *cap = &crypto_drivers[hid]; 1458167755Ssam if (cap->cc_dev == NULL) 1459167755Ssam continue; 1460167755Ssam db_printf("%-12s %4u %4u %08x %2u %2u\n" 1461167755Ssam , device_get_nameunit(cap->cc_dev) 1462167755Ssam , cap->cc_sessions 1463167755Ssam , cap->cc_koperations 1464167755Ssam , cap->cc_flags 1465167755Ssam , cap->cc_qblocked 1466167755Ssam , cap->cc_kqblocked 1467167755Ssam ); 1468167755Ssam } 1469167755Ssam} 1470167755Ssam 1471167755SsamDB_SHOW_COMMAND(crypto, db_show_crypto) 1472167755Ssam{ 1473167755Ssam struct cryptop *crp; 1474167755Ssam 1475167755Ssam db_show_drivers(); 1476167755Ssam db_printf("\n"); 1477167755Ssam 1478167755Ssam db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n", 1479167755Ssam "HID", "Caps", "Ilen", "Olen", "Etype", "Flags", 1480167755Ssam "Desc", "Callback"); 1481167755Ssam TAILQ_FOREACH(crp, &crp_q, crp_next) { 1482167755Ssam db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n" 1483167755Ssam , (int) CRYPTO_SESID2HID(crp->crp_sid) 1484167755Ssam , (int) CRYPTO_SESID2CAPS(crp->crp_sid) 1485167755Ssam , crp->crp_ilen, crp->crp_olen 1486167755Ssam , crp->crp_etype 1487167755Ssam , crp->crp_flags 1488167755Ssam , crp->crp_desc 1489167755Ssam , crp->crp_callback 1490167755Ssam ); 1491167755Ssam } 1492167755Ssam if (!TAILQ_EMPTY(&crp_ret_q)) { 1493167755Ssam db_printf("\n%4s %4s %4s %8s\n", 1494167755Ssam "HID", "Etype", "Flags", "Callback"); 1495167755Ssam TAILQ_FOREACH(crp, &crp_ret_q, crp_next) { 1496167755Ssam db_printf("%4u %4u %04x %8p\n" 1497167755Ssam , (int) CRYPTO_SESID2HID(crp->crp_sid) 1498167755Ssam , crp->crp_etype 1499167755Ssam , crp->crp_flags 1500167755Ssam , crp->crp_callback 1501167755Ssam ); 1502167755Ssam } 1503167755Ssam } 1504167755Ssam} 1505167755Ssam 1506167755SsamDB_SHOW_COMMAND(kcrypto, db_show_kcrypto) 1507167755Ssam{ 1508167755Ssam struct cryptkop *krp; 1509167755Ssam 1510167755Ssam db_show_drivers(); 1511167755Ssam db_printf("\n"); 1512167755Ssam 1513167755Ssam db_printf("%4s %5s %4s %4s %8s %4s %8s\n", 1514167755Ssam "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback"); 1515167755Ssam TAILQ_FOREACH(krp, &crp_kq, krp_next) { 1516167755Ssam db_printf("%4u %5u %4u %4u %08x %4u %8p\n" 1517167755Ssam , krp->krp_op 1518167755Ssam , krp->krp_status 1519167755Ssam , krp->krp_iparams, krp->krp_oparams 1520167755Ssam , krp->krp_crid, krp->krp_hid 1521167755Ssam , krp->krp_callback 1522167755Ssam ); 1523167755Ssam } 1524167755Ssam if (!TAILQ_EMPTY(&crp_ret_q)) { 1525167755Ssam db_printf("%4s %5s %8s %4s %8s\n", 1526167755Ssam "Op", "Status", "CRID", "HID", "Callback"); 1527167755Ssam TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) { 1528167755Ssam db_printf("%4u %5u %08x %4u %8p\n" 1529167755Ssam , krp->krp_op 1530167755Ssam , krp->krp_status 1531167755Ssam , krp->krp_crid, krp->krp_hid 1532167755Ssam , krp->krp_callback 1533167755Ssam ); 1534167755Ssam } 1535167755Ssam } 1536167755Ssam} 1537167755Ssam#endif 1538167755Ssam 1539167755Ssamint crypto_modevent(module_t mod, int type, void *unused); 1540167755Ssam 1541167755Ssam/* 1542167755Ssam * Initialization code, both for static and dynamic loading. 1543167755Ssam * Note this is not invoked with the usual MODULE_DECLARE 1544167755Ssam * mechanism but instead is listed as a dependency by the 1545167755Ssam * cryptosoft driver. This guarantees proper ordering of 1546167755Ssam * calls on module load/unload. 1547167755Ssam */ 1548167755Ssamint 1549167755Ssamcrypto_modevent(module_t mod, int type, void *unused) 1550167755Ssam{ 1551167755Ssam int error = EINVAL; 1552167755Ssam 1553167755Ssam switch (type) { 1554167755Ssam case MOD_LOAD: 1555167755Ssam error = crypto_init(); 1556167755Ssam if (error == 0 && bootverbose) 1557167755Ssam printf("crypto: <crypto core>\n"); 1558167755Ssam break; 1559167755Ssam case MOD_UNLOAD: 1560167755Ssam /*XXX disallow if active sessions */ 1561167755Ssam error = 0; 1562167755Ssam crypto_destroy(); 1563167755Ssam return 0; 1564167755Ssam } 1565167755Ssam return error; 1566167755Ssam} 1567167755SsamMODULE_VERSION(crypto, 1); 1568167755SsamMODULE_DEPEND(crypto, zlib, 1, 1, 1); 1569