Sync acpiconf with FreeBSD 7.2.
[dragonfly.git] / sys / opencrypto / crypto.c
blob31fa1e2643615cd2bebe54067010ac50563f22cf
1 /* $FreeBSD: src/sys/opencrypto/crypto.c,v 1.28 2007/10/20 23:23:22 julian Exp $ */
2 /*-
3 * Copyright (c) 2002-2006 Sam Leffler. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * Cryptographic Subsystem.
29 * This code is derived from the Openbsd Cryptographic Framework (OCF)
30 * that has the copyright shown below. Very little of the original
31 * code remains.
34 /*-
35 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
37 * This code was written by Angelos D. Keromytis in Athens, Greece, in
38 * February 2000. Network Security Technologies Inc. (NSTI) kindly
39 * supported the development of this code.
41 * Copyright (c) 2000, 2001 Angelos D. Keromytis
43 * Permission to use, copy, and modify this software with or without fee
44 * is hereby granted, provided that this entire notice is included in
45 * all source code copies of any software which is or includes a copy or
46 * modification of this software.
48 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
49 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
50 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
51 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
52 * PURPOSE.
55 #define CRYPTO_TIMING /* enable timing support */
57 #include "opt_ddb.h"
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/eventhandler.h>
62 #include <sys/kernel.h>
63 #include <sys/kthread.h>
64 #include <sys/lock.h>
65 #include <sys/module.h>
66 #include <sys/malloc.h>
67 #include <sys/proc.h>
68 #include <sys/sysctl.h>
69 #include <sys/thread2.h>
71 #include <vm/vm_zone.h>
73 #include <ddb/ddb.h>
75 #include <opencrypto/cryptodev.h>
76 #include <opencrypto/xform.h> /* XXX for M_XDATA */
78 #include <sys/kobj.h>
79 #include <sys/bus.h>
80 #include "cryptodev_if.h"
83 * Crypto drivers register themselves by allocating a slot in the
84 * crypto_drivers table with crypto_get_driverid() and then registering
85 * each algorithm they support with crypto_register() and crypto_kregister().
87 static struct lock crypto_drivers_lock; /* lock on driver table */
88 #define CRYPTO_DRIVER_LOCK() lockmgr(&crypto_drivers_lock, LK_EXCLUSIVE)
89 #define CRYPTO_DRIVER_UNLOCK() lockmgr(&crypto_drivers_lock, LK_RELEASE)
90 #define CRYPTO_DRIVER_ASSERT() KKASSERT(lockstatus(&crypto_drivers_lock, curthread) != 0)
93 * Crypto device/driver capabilities structure.
95 * Synchronization:
96 * (d) - protected by CRYPTO_DRIVER_LOCK()
97 * (q) - protected by CRYPTO_Q_LOCK()
98 * Not tagged fields are read-only.
100 struct cryptocap {
101 device_t cc_dev; /* (d) device/driver */
102 u_int32_t cc_sessions; /* (d) # of sessions */
103 u_int32_t cc_koperations; /* (d) # os asym operations */
105 * Largest possible operator length (in bits) for each type of
106 * encryption algorithm. XXX not used
108 u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
109 u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
110 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
112 int cc_flags; /* (d) flags */
113 #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
114 int cc_qblocked; /* (q) symmetric q blocked */
115 int cc_kqblocked; /* (q) asymmetric q blocked */
117 static struct cryptocap *crypto_drivers = NULL;
118 static int crypto_drivers_num = 0;
121 * There are two queues for crypto requests; one for symmetric (e.g.
122 * cipher) operations and one for asymmetric (e.g. MOD) operations.
123 * See below for how synchronization is handled.
124 * A single lock is used to lock access to both queues. We could
125 * have one per-queue but having one simplifies handling of block/unblock
126 * operations.
128 static int crp_sleep = 0;
129 static TAILQ_HEAD(,cryptop) crp_q; /* request queues */
130 static TAILQ_HEAD(,cryptkop) crp_kq;
131 static struct lock crypto_q_lock;
132 #define CRYPTO_Q_LOCK() lockmgr(&crypto_q_lock, LK_EXCLUSIVE)
133 #define CRYPTO_Q_UNLOCK() lockmgr(&crypto_q_lock, LK_RELEASE)
136 * There are two queues for processing completed crypto requests; one
137 * for the symmetric and one for the asymmetric ops. We only need one
138 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
139 * lock is used to lock access to both queues. Note that this lock
140 * must be separate from the lock on request queues to insure driver
141 * callbacks don't generate lock order reversals.
143 static TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queues */
144 static TAILQ_HEAD(,cryptkop) crp_ret_kq;
145 static struct lock crypto_ret_q_lock;
146 #define CRYPTO_RETQ_LOCK() lockmgr(&crypto_ret_q_lock, LK_EXCLUSIVE)
147 #define CRYPTO_RETQ_UNLOCK() lockmgr(&crypto_ret_q_lock, LK_RELEASE)
148 #define CRYPTO_RETQ_EMPTY() (TAILQ_EMPTY(&crp_ret_q) && TAILQ_EMPTY(&crp_ret_kq))
151 * Crypto op and desciptor data structures are allocated
152 * from separate private zones.
154 static vm_zone_t cryptop_zone;
155 static vm_zone_t cryptodesc_zone;
157 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
158 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
159 &crypto_userasymcrypto, 0,
160 "Enable/disable user-mode access to asymmetric crypto support");
161 int crypto_devallowsoft = 0; /* only use hardware crypto for asym */
162 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
163 &crypto_devallowsoft, 0,
164 "Enable/disable use of software asym crypto support");
166 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
168 static void crypto_proc(void);
169 static struct thread *cryptothread;
170 static void crypto_ret_proc(void);
171 static struct thread *cryptoretthread;
172 static void crypto_destroy(void);
173 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
174 static int crypto_kinvoke(struct cryptkop *krp, int flags);
176 static struct cryptostats cryptostats;
177 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
178 cryptostats, "Crypto system statistics");
180 #ifdef CRYPTO_TIMING
181 static int crypto_timing = 0;
182 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
183 &crypto_timing, 0, "Enable/disable crypto timing support");
184 #endif
186 static int
187 crypto_init(void)
189 int error;
191 lockinit(&crypto_drivers_lock, "crypto driver table", 0, LK_CANRECURSE);
193 TAILQ_INIT(&crp_q);
194 TAILQ_INIT(&crp_kq);
195 lockinit(&crypto_q_lock, "crypto op queues", 0, LK_CANRECURSE);
197 TAILQ_INIT(&crp_ret_q);
198 TAILQ_INIT(&crp_ret_kq);
199 lockinit(&crypto_ret_q_lock, "crypto return queues", 0, LK_CANRECURSE);
201 cryptop_zone = zinit("cryptop", sizeof (struct cryptop), 0, 0, 1);
202 cryptodesc_zone = zinit("cryptodesc", sizeof (struct cryptodesc),
203 0, 0, 1);
204 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
205 kprintf("crypto_init: cannot setup crypto zones\n");
206 error = ENOMEM;
207 goto bad;
210 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
211 crypto_drivers = kmalloc(crypto_drivers_num *
212 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
213 if (crypto_drivers == NULL) {
214 kprintf("crypto_init: cannot malloc driver table\n");
215 error = ENOMEM;
216 goto bad;
219 error = kthread_create((void (*)(void *)) crypto_proc, NULL,
220 &cryptothread, "crypto");
221 if (error) {
222 kprintf("crypto_init: cannot start crypto thread; error %d",
223 error);
224 goto bad;
227 error = kthread_create((void (*)(void *)) crypto_ret_proc, NULL,
228 &cryptoretthread, "crypto returns");
229 if (error) {
230 kprintf("crypto_init: cannot start cryptoret thread; error %d",
231 error);
232 goto bad;
234 return 0;
235 bad:
236 crypto_destroy();
237 return error;
241 * Signal a crypto thread to terminate. We use the driver
242 * table lock to synchronize the sleep/wakeups so that we
243 * are sure the threads have terminated before we release
244 * the data structures they use. See crypto_finis below
245 * for the other half of this song-and-dance.
247 static void
248 crypto_terminate(struct thread **tp, void *q)
250 struct thread *t;
252 KKASSERT(lockstatus(&crypto_drivers_lock, curthread) != 0);
253 t = *tp;
254 *tp = NULL;
255 if (t) {
256 kprintf("crypto_terminate: start\n");
257 wakeup_one(q);
258 crit_enter();
259 tsleep_interlock(t, 0);
260 CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */
261 crit_exit();
262 tsleep(t, PINTERLOCKED, "crypto_destroy", 0);
263 CRYPTO_DRIVER_LOCK();
264 kprintf("crypto_terminate: end\n");
268 static void
269 crypto_destroy(void)
272 * Terminate any crypto threads.
274 CRYPTO_DRIVER_LOCK();
275 crypto_terminate(&cryptothread, &crp_q);
276 crypto_terminate(&cryptoretthread, &crp_ret_q);
277 CRYPTO_DRIVER_UNLOCK();
279 /* XXX flush queues??? */
282 * Reclaim dynamically allocated resources.
284 if (crypto_drivers != NULL)
285 kfree(crypto_drivers, M_CRYPTO_DATA);
287 if (cryptodesc_zone != NULL)
288 zdestroy(cryptodesc_zone);
289 if (cryptop_zone != NULL)
290 zdestroy(cryptop_zone);
291 lockuninit(&crypto_q_lock);
292 lockuninit(&crypto_ret_q_lock);
293 lockuninit(&crypto_drivers_lock);
296 static struct cryptocap *
297 crypto_checkdriver(u_int32_t hid)
299 if (crypto_drivers == NULL)
300 return NULL;
301 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
305 * Compare a driver's list of supported algorithms against another
306 * list; return non-zero if all algorithms are supported.
308 static int
309 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
311 const struct cryptoini *cr;
313 /* See if all the algorithms are supported. */
314 for (cr = cri; cr; cr = cr->cri_next)
315 if (cap->cc_alg[cr->cri_alg] == 0)
316 return 0;
317 return 1;
321 * Select a driver for a new session that supports the specified
322 * algorithms and, optionally, is constrained according to the flags.
323 * The algorithm we use here is pretty stupid; just use the
324 * first driver that supports all the algorithms we need. If there
325 * are multiple drivers we choose the driver with the fewest active
326 * sessions. We prefer hardware-backed drivers to software ones.
328 * XXX We need more smarts here (in real life too, but that's
329 * XXX another story altogether).
331 static struct cryptocap *
332 crypto_select_driver(const struct cryptoini *cri, int flags)
334 struct cryptocap *cap, *best;
335 int match, hid;
337 CRYPTO_DRIVER_ASSERT();
340 * Look first for hardware crypto devices if permitted.
342 if (flags & CRYPTOCAP_F_HARDWARE)
343 match = CRYPTOCAP_F_HARDWARE;
344 else
345 match = CRYPTOCAP_F_SOFTWARE;
346 best = NULL;
347 again:
348 for (hid = 0; hid < crypto_drivers_num; hid++) {
349 cap = &crypto_drivers[hid];
351 * If it's not initialized, is in the process of
352 * going away, or is not appropriate (hardware
353 * or software based on match), then skip.
355 if (cap->cc_dev == NULL ||
356 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
357 (cap->cc_flags & match) == 0)
358 continue;
360 /* verify all the algorithms are supported. */
361 if (driver_suitable(cap, cri)) {
362 if (best == NULL ||
363 cap->cc_sessions < best->cc_sessions)
364 best = cap;
367 if (best != NULL)
368 return best;
369 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
370 /* sort of an Algol 68-style for loop */
371 match = CRYPTOCAP_F_SOFTWARE;
372 goto again;
374 return best;
378 * Create a new session. The crid argument specifies a crypto
379 * driver to use or constraints on a driver to select (hardware
380 * only, software only, either). Whatever driver is selected
381 * must be capable of the requested crypto algorithms.
384 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
386 struct cryptocap *cap;
387 u_int32_t hid, lid;
388 int err;
390 CRYPTO_DRIVER_LOCK();
391 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
393 * Use specified driver; verify it is capable.
395 cap = crypto_checkdriver(crid);
396 if (cap != NULL && !driver_suitable(cap, cri))
397 cap = NULL;
398 } else {
400 * No requested driver; select based on crid flags.
402 cap = crypto_select_driver(cri, crid);
404 * if NULL then can't do everything in one session.
405 * XXX Fix this. We need to inject a "virtual" session
406 * XXX layer right about here.
409 if (cap != NULL) {
410 /* Call the driver initialization routine. */
411 hid = cap - crypto_drivers;
412 lid = hid; /* Pass the driver ID. */
413 err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
414 if (err == 0) {
415 (*sid) = (cap->cc_flags & 0xff000000)
416 | (hid & 0x00ffffff);
417 (*sid) <<= 32;
418 (*sid) |= (lid & 0xffffffff);
419 cap->cc_sessions++;
421 } else
422 err = EINVAL;
423 CRYPTO_DRIVER_UNLOCK();
424 return err;
427 static void
428 crypto_remove(struct cryptocap *cap)
431 KKASSERT(lockstatus(&crypto_drivers_lock, curthread) != 0);
432 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
433 bzero(cap, sizeof(*cap));
437 * Delete an existing session (or a reserved session on an unregistered
438 * driver).
441 crypto_freesession(u_int64_t sid)
443 struct cryptocap *cap;
444 u_int32_t hid;
445 int err;
447 CRYPTO_DRIVER_LOCK();
449 if (crypto_drivers == NULL) {
450 err = EINVAL;
451 goto done;
454 /* Determine two IDs. */
455 hid = CRYPTO_SESID2HID(sid);
457 if (hid >= crypto_drivers_num) {
458 err = ENOENT;
459 goto done;
461 cap = &crypto_drivers[hid];
463 if (cap->cc_sessions)
464 cap->cc_sessions--;
466 /* Call the driver cleanup routine, if available. */
467 err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
469 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
470 crypto_remove(cap);
472 done:
473 CRYPTO_DRIVER_UNLOCK();
474 return err;
478 * Return an unused driver id. Used by drivers prior to registering
479 * support for the algorithms they handle.
481 int32_t
482 crypto_get_driverid(device_t dev, int flags)
484 struct cryptocap *newdrv;
485 int i;
487 if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
488 kprintf("%s: no flags specified when registering driver\n",
489 device_get_nameunit(dev));
490 return -1;
493 CRYPTO_DRIVER_LOCK();
495 for (i = 0; i < crypto_drivers_num; i++) {
496 if (crypto_drivers[i].cc_dev == NULL &&
497 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
498 break;
502 /* Out of entries, allocate some more. */
503 if (i == crypto_drivers_num) {
504 /* Be careful about wrap-around. */
505 if (2 * crypto_drivers_num <= crypto_drivers_num) {
506 CRYPTO_DRIVER_UNLOCK();
507 kprintf("crypto: driver count wraparound!\n");
508 return -1;
511 newdrv = kmalloc(2 * crypto_drivers_num *
512 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
513 if (newdrv == NULL) {
514 CRYPTO_DRIVER_UNLOCK();
515 kprintf("crypto: no space to expand driver table!\n");
516 return -1;
519 bcopy(crypto_drivers, newdrv,
520 crypto_drivers_num * sizeof(struct cryptocap));
522 crypto_drivers_num *= 2;
524 kfree(crypto_drivers, M_CRYPTO_DATA);
525 crypto_drivers = newdrv;
528 /* NB: state is zero'd on free */
529 crypto_drivers[i].cc_sessions = 1; /* Mark */
530 crypto_drivers[i].cc_dev = dev;
531 crypto_drivers[i].cc_flags = flags;
532 if (bootverbose)
533 kprintf("crypto: assign %s driver id %u, flags %u\n",
534 device_get_nameunit(dev), i, flags);
536 CRYPTO_DRIVER_UNLOCK();
538 return i;
542 * Lookup a driver by name. We match against the full device
543 * name and unit, and against just the name. The latter gives
544 * us a simple widlcarding by device name. On success return the
545 * driver/hardware identifier; otherwise return -1.
548 crypto_find_driver(const char *match)
550 int i, len = strlen(match);
552 CRYPTO_DRIVER_LOCK();
553 for (i = 0; i < crypto_drivers_num; i++) {
554 device_t dev = crypto_drivers[i].cc_dev;
555 if (dev == NULL ||
556 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
557 continue;
558 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
559 strncmp(match, device_get_name(dev), len) == 0)
560 break;
562 CRYPTO_DRIVER_UNLOCK();
563 return i < crypto_drivers_num ? i : -1;
567 * Return the device_t for the specified driver or NULL
568 * if the driver identifier is invalid.
570 device_t
571 crypto_find_device_byhid(int hid)
573 struct cryptocap *cap = crypto_checkdriver(hid);
574 return cap != NULL ? cap->cc_dev : NULL;
578 * Return the device/driver capabilities.
581 crypto_getcaps(int hid)
583 struct cryptocap *cap = crypto_checkdriver(hid);
584 return cap != NULL ? cap->cc_flags : 0;
588 * Register support for a key-related algorithm. This routine
589 * is called once for each algorithm supported a driver.
592 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
594 struct cryptocap *cap;
595 int err;
597 CRYPTO_DRIVER_LOCK();
599 cap = crypto_checkdriver(driverid);
600 if (cap != NULL &&
601 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
603 * XXX Do some performance testing to determine placing.
604 * XXX We probably need an auxiliary data structure that
605 * XXX describes relative performances.
608 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
609 if (bootverbose)
610 kprintf("crypto: %s registers key alg %u flags %u\n"
611 , device_get_nameunit(cap->cc_dev)
612 , kalg
613 , flags
616 err = 0;
617 } else
618 err = EINVAL;
620 CRYPTO_DRIVER_UNLOCK();
621 return err;
625 * Register support for a non-key-related algorithm. This routine
626 * is called once for each such algorithm supported by a driver.
629 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
630 u_int32_t flags)
632 struct cryptocap *cap;
633 int err;
635 CRYPTO_DRIVER_LOCK();
637 cap = crypto_checkdriver(driverid);
638 /* NB: algorithms are in the range [1..max] */
639 if (cap != NULL &&
640 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
642 * XXX Do some performance testing to determine placing.
643 * XXX We probably need an auxiliary data structure that
644 * XXX describes relative performances.
647 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
648 cap->cc_max_op_len[alg] = maxoplen;
649 if (bootverbose)
650 kprintf("crypto: %s registers alg %u flags %u maxoplen %u\n"
651 , device_get_nameunit(cap->cc_dev)
652 , alg
653 , flags
654 , maxoplen
656 cap->cc_sessions = 0; /* Unmark */
657 err = 0;
658 } else
659 err = EINVAL;
661 CRYPTO_DRIVER_UNLOCK();
662 return err;
665 static void
666 driver_finis(struct cryptocap *cap)
668 u_int32_t ses, kops;
670 CRYPTO_DRIVER_ASSERT();
672 ses = cap->cc_sessions;
673 kops = cap->cc_koperations;
674 bzero(cap, sizeof(*cap));
675 if (ses != 0 || kops != 0) {
677 * If there are pending sessions,
678 * just mark as invalid.
680 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
681 cap->cc_sessions = ses;
682 cap->cc_koperations = kops;
687 * Unregister a crypto driver. If there are pending sessions using it,
688 * leave enough information around so that subsequent calls using those
689 * sessions will correctly detect the driver has been unregistered and
690 * reroute requests.
693 crypto_unregister(u_int32_t driverid, int alg)
695 struct cryptocap *cap;
696 int i, err;
698 CRYPTO_DRIVER_LOCK();
699 cap = crypto_checkdriver(driverid);
700 if (cap != NULL &&
701 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
702 cap->cc_alg[alg] != 0) {
703 cap->cc_alg[alg] = 0;
704 cap->cc_max_op_len[alg] = 0;
706 /* Was this the last algorithm ? */
707 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
708 if (cap->cc_alg[i] != 0)
709 break;
711 if (i == CRYPTO_ALGORITHM_MAX + 1)
712 driver_finis(cap);
713 err = 0;
714 } else
715 err = EINVAL;
716 CRYPTO_DRIVER_UNLOCK();
718 return err;
722 * Unregister all algorithms associated with a crypto driver.
723 * If there are pending sessions using it, leave enough information
724 * around so that subsequent calls using those sessions will
725 * correctly detect the driver has been unregistered and reroute
726 * requests.
729 crypto_unregister_all(u_int32_t driverid)
731 struct cryptocap *cap;
732 int err;
734 CRYPTO_DRIVER_LOCK();
735 cap = crypto_checkdriver(driverid);
736 if (cap != NULL) {
737 driver_finis(cap);
738 err = 0;
739 } else
740 err = EINVAL;
741 CRYPTO_DRIVER_UNLOCK();
743 return err;
747 * Clear blockage on a driver. The what parameter indicates whether
748 * the driver is now ready for cryptop's and/or cryptokop's.
751 crypto_unblock(u_int32_t driverid, int what)
753 struct cryptocap *cap;
754 int err;
756 CRYPTO_Q_LOCK();
757 cap = crypto_checkdriver(driverid);
758 if (cap != NULL) {
759 if (what & CRYPTO_SYMQ)
760 cap->cc_qblocked = 0;
761 if (what & CRYPTO_ASYMQ)
762 cap->cc_kqblocked = 0;
763 if (crp_sleep)
764 wakeup_one(&crp_q);
765 err = 0;
766 } else
767 err = EINVAL;
768 CRYPTO_Q_UNLOCK();
770 return err;
774 * Add a crypto request to a queue, to be processed by the kernel thread.
777 crypto_dispatch(struct cryptop *crp)
779 struct cryptocap *cap;
780 u_int32_t hid;
781 int result;
783 cryptostats.cs_ops++;
785 #ifdef CRYPTO_TIMING
786 if (crypto_timing)
787 nanouptime(&crp->crp_tstamp);
788 #endif
790 hid = CRYPTO_SESID2HID(crp->crp_sid);
792 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
794 * Caller marked the request to be processed
795 * immediately; dispatch it directly to the
796 * driver unless the driver is currently blocked.
798 cap = crypto_checkdriver(hid);
799 /* Driver cannot disappeared when there is an active session. */
800 KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
801 if (!cap->cc_qblocked) {
802 result = crypto_invoke(cap, crp, 0);
803 if (result != ERESTART)
804 return (result);
806 * The driver ran out of resources, put the request on
807 * the queue.
811 CRYPTO_Q_LOCK();
812 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
813 if (crp_sleep)
814 wakeup_one(&crp_q);
815 CRYPTO_Q_UNLOCK();
816 return 0;
820 * Add an asymetric crypto request to a queue,
821 * to be processed by the kernel thread.
824 crypto_kdispatch(struct cryptkop *krp)
826 int error;
828 cryptostats.cs_kops++;
830 error = crypto_kinvoke(krp, krp->krp_crid);
831 if (error == ERESTART) {
832 CRYPTO_Q_LOCK();
833 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
834 if (crp_sleep)
835 wakeup_one(&crp_q);
836 CRYPTO_Q_UNLOCK();
837 error = 0;
839 return error;
843 * Verify a driver is suitable for the specified operation.
845 static __inline int
846 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
848 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
852 * Select a driver for an asym operation. The driver must
853 * support the necessary algorithm. The caller can constrain
854 * which device is selected with the flags parameter. The
855 * algorithm we use here is pretty stupid; just use the first
856 * driver that supports the algorithms we need. If there are
857 * multiple suitable drivers we choose the driver with the
858 * fewest active operations. We prefer hardware-backed
859 * drivers to software ones when either may be used.
861 static struct cryptocap *
862 crypto_select_kdriver(const struct cryptkop *krp, int flags)
864 struct cryptocap *cap, *best, *blocked;
865 int match, hid;
867 CRYPTO_DRIVER_ASSERT();
870 * Look first for hardware crypto devices if permitted.
872 if (flags & CRYPTOCAP_F_HARDWARE)
873 match = CRYPTOCAP_F_HARDWARE;
874 else
875 match = CRYPTOCAP_F_SOFTWARE;
876 best = NULL;
877 blocked = NULL;
878 again:
879 for (hid = 0; hid < crypto_drivers_num; hid++) {
880 cap = &crypto_drivers[hid];
882 * If it's not initialized, is in the process of
883 * going away, or is not appropriate (hardware
884 * or software based on match), then skip.
886 if (cap->cc_dev == NULL ||
887 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
888 (cap->cc_flags & match) == 0)
889 continue;
891 /* verify all the algorithms are supported. */
892 if (kdriver_suitable(cap, krp)) {
893 if (best == NULL ||
894 cap->cc_koperations < best->cc_koperations)
895 best = cap;
898 if (best != NULL)
899 return best;
900 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
901 /* sort of an Algol 68-style for loop */
902 match = CRYPTOCAP_F_SOFTWARE;
903 goto again;
905 return best;
909 * Dispatch an assymetric crypto request.
911 static int
912 crypto_kinvoke(struct cryptkop *krp, int crid)
914 struct cryptocap *cap = NULL;
915 int error;
917 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
918 KASSERT(krp->krp_callback != NULL,
919 ("%s: krp->crp_callback == NULL", __func__));
921 CRYPTO_DRIVER_LOCK();
922 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
923 cap = crypto_checkdriver(crid);
924 if (cap != NULL) {
926 * Driver present, it must support the necessary
927 * algorithm and, if s/w drivers are excluded,
928 * it must be registered as hardware-backed.
930 if (!kdriver_suitable(cap, krp) ||
931 (!crypto_devallowsoft &&
932 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
933 cap = NULL;
935 } else {
937 * No requested driver; select based on crid flags.
939 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
940 crid &= ~CRYPTOCAP_F_SOFTWARE;
941 cap = crypto_select_kdriver(krp, crid);
943 if (cap != NULL && !cap->cc_kqblocked) {
944 krp->krp_hid = cap - crypto_drivers;
945 cap->cc_koperations++;
946 CRYPTO_DRIVER_UNLOCK();
947 error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
948 CRYPTO_DRIVER_LOCK();
949 if (error == ERESTART) {
950 cap->cc_koperations--;
951 CRYPTO_DRIVER_UNLOCK();
952 return (error);
954 } else {
956 * NB: cap is !NULL if device is blocked; in
957 * that case return ERESTART so the operation
958 * is resubmitted if possible.
960 error = (cap == NULL) ? ENODEV : ERESTART;
962 CRYPTO_DRIVER_UNLOCK();
964 if (error) {
965 krp->krp_status = error;
966 crypto_kdone(krp);
968 return 0;
971 #ifdef CRYPTO_TIMING
972 static void
973 crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
975 struct timespec now, t;
977 nanouptime(&now);
978 t.tv_sec = now.tv_sec - tv->tv_sec;
979 t.tv_nsec = now.tv_nsec - tv->tv_nsec;
980 if (t.tv_nsec < 0) {
981 t.tv_sec--;
982 t.tv_nsec += 1000000000;
984 timespecadd(&ts->acc, &t);
985 if (timespeccmp(&t, &ts->min, <))
986 ts->min = t;
987 if (timespeccmp(&t, &ts->max, >))
988 ts->max = t;
989 ts->count++;
991 *tv = now;
993 #endif
996 * Dispatch a crypto request to the appropriate crypto devices.
998 static int
999 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1002 KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
1003 KASSERT(crp->crp_callback != NULL,
1004 ("%s: crp->crp_callback == NULL", __func__));
1005 KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
1007 #ifdef CRYPTO_TIMING
1008 if (crypto_timing)
1009 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1010 #endif
1011 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1012 struct cryptodesc *crd;
1013 u_int64_t nid;
1016 * Driver has unregistered; migrate the session and return
1017 * an error to the caller so they'll resubmit the op.
1019 * XXX: What if there are more already queued requests for this
1020 * session?
1022 crypto_freesession(crp->crp_sid);
1024 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1025 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1027 /* XXX propagate flags from initial session? */
1028 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
1029 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
1030 crp->crp_sid = nid;
1032 crp->crp_etype = EAGAIN;
1033 crypto_done(crp);
1034 return 0;
1035 } else {
1037 * Invoke the driver to process the request.
1039 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1044 * Release a set of crypto descriptors.
1046 void
1047 crypto_freereq(struct cryptop *crp)
1049 struct cryptodesc *crd;
1051 if (crp == NULL)
1052 return;
1054 #ifdef DIAGNOSTIC
1056 struct cryptop *crp2;
1058 CRYPTO_Q_LOCK();
1059 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1060 KASSERT(crp2 != crp,
1061 ("Freeing cryptop from the crypto queue (%p).",
1062 crp));
1064 CRYPTO_Q_UNLOCK();
1065 CRYPTO_RETQ_LOCK();
1066 TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
1067 KASSERT(crp2 != crp,
1068 ("Freeing cryptop from the return queue (%p).",
1069 crp));
1071 CRYPTO_RETQ_UNLOCK();
1073 #endif
1075 while ((crd = crp->crp_desc) != NULL) {
1076 crp->crp_desc = crd->crd_next;
1077 zfree(cryptodesc_zone, crd);
1079 zfree(cryptop_zone, crp);
1083 * Acquire a set of crypto descriptors.
1085 struct cryptop *
1086 crypto_getreq(int num)
1088 struct cryptodesc *crd;
1089 struct cryptop *crp;
1091 crp = zalloc(cryptop_zone);
1092 if (crp != NULL) {
1093 bzero(crp, sizeof (*crp));
1094 while (num--) {
1095 crd = zalloc(cryptodesc_zone);
1096 if (crd == NULL) {
1097 crypto_freereq(crp);
1098 return NULL;
1100 bzero(crd, sizeof (*crd));
1102 crd->crd_next = crp->crp_desc;
1103 crp->crp_desc = crd;
1106 return crp;
1110 * Invoke the callback on behalf of the driver.
1112 void
1113 crypto_done(struct cryptop *crp)
1115 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
1116 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
1117 crp->crp_flags |= CRYPTO_F_DONE;
1118 if (crp->crp_etype != 0)
1119 cryptostats.cs_errs++;
1120 #ifdef CRYPTO_TIMING
1121 if (crypto_timing)
1122 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
1123 #endif
1125 * CBIMM means unconditionally do the callback immediately;
1126 * CBIFSYNC means do the callback immediately only if the
1127 * operation was done synchronously. Both are used to avoid
1128 * doing extraneous context switches; the latter is mostly
1129 * used with the software crypto driver.
1131 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1132 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1133 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
1135 * Do the callback directly. This is ok when the
1136 * callback routine does very little (e.g. the
1137 * /dev/crypto callback method just does a wakeup).
1139 #ifdef CRYPTO_TIMING
1140 if (crypto_timing) {
1142 * NB: We must copy the timestamp before
1143 * doing the callback as the cryptop is
1144 * likely to be reclaimed.
1146 struct timespec t = crp->crp_tstamp;
1147 crypto_tstat(&cryptostats.cs_cb, &t);
1148 crp->crp_callback(crp);
1149 crypto_tstat(&cryptostats.cs_finis, &t);
1150 } else
1151 #endif
1152 crp->crp_callback(crp);
1153 } else {
1155 * Normal case; queue the callback for the thread.
1157 CRYPTO_RETQ_LOCK();
1158 if (CRYPTO_RETQ_EMPTY())
1159 wakeup_one(&crp_ret_q); /* shared wait channel */
1160 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1161 CRYPTO_RETQ_UNLOCK();
1166 * Invoke the callback on behalf of the driver.
1168 void
1169 crypto_kdone(struct cryptkop *krp)
1171 struct cryptocap *cap;
1173 if (krp->krp_status != 0)
1174 cryptostats.cs_kerrs++;
1175 CRYPTO_DRIVER_LOCK();
1176 /* XXX: What if driver is loaded in the meantime? */
1177 if (krp->krp_hid < crypto_drivers_num) {
1178 cap = &crypto_drivers[krp->krp_hid];
1179 cap->cc_koperations--;
1180 KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
1181 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1182 crypto_remove(cap);
1184 CRYPTO_DRIVER_UNLOCK();
1185 CRYPTO_RETQ_LOCK();
1186 if (CRYPTO_RETQ_EMPTY())
1187 wakeup_one(&crp_ret_q); /* shared wait channel */
1188 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1189 CRYPTO_RETQ_UNLOCK();
1193 crypto_getfeat(int *featp)
1195 int hid, kalg, feat = 0;
1197 CRYPTO_DRIVER_LOCK();
1198 for (hid = 0; hid < crypto_drivers_num; hid++) {
1199 const struct cryptocap *cap = &crypto_drivers[hid];
1201 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1202 !crypto_devallowsoft) {
1203 continue;
1205 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1206 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1207 feat |= 1 << kalg;
1209 CRYPTO_DRIVER_UNLOCK();
1210 *featp = feat;
1211 return (0);
1215 * Terminate a thread at module unload. The process that
1216 * initiated this is waiting for us to signal that we're gone;
1217 * wake it up and exit. We use the driver table lock to insure
1218 * we don't do the wakeup before they're waiting. There is no
1219 * race here because the waiter sleeps on the proc lock for the
1220 * thread so it gets notified at the right time because of an
1221 * extra wakeup that's done in exit1().
1223 static void
1224 crypto_finis(void *chan)
1226 CRYPTO_DRIVER_LOCK();
1227 wakeup_one(chan);
1228 CRYPTO_DRIVER_UNLOCK();
1229 kthread_exit();
1233 * Crypto thread, dispatches crypto requests.
1235 static void
1236 crypto_proc(void)
1238 struct cryptop *crp, *submit;
1239 struct cryptkop *krp;
1240 struct cryptocap *cap;
1241 u_int32_t hid;
1242 int result, hint;
1244 CRYPTO_Q_LOCK();
1245 for (;;) {
1247 * Find the first element in the queue that can be
1248 * processed and look-ahead to see if multiple ops
1249 * are ready for the same driver.
1251 submit = NULL;
1252 hint = 0;
1253 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1254 hid = CRYPTO_SESID2HID(crp->crp_sid);
1255 cap = crypto_checkdriver(hid);
1257 * Driver cannot disappeared when there is an active
1258 * session.
1260 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1261 __func__, __LINE__));
1262 if (cap == NULL || cap->cc_dev == NULL) {
1263 /* Op needs to be migrated, process it. */
1264 if (submit == NULL)
1265 submit = crp;
1266 break;
1268 if (!cap->cc_qblocked) {
1269 if (submit != NULL) {
1271 * We stop on finding another op,
1272 * regardless whether its for the same
1273 * driver or not. We could keep
1274 * searching the queue but it might be
1275 * better to just use a per-driver
1276 * queue instead.
1278 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1279 hint = CRYPTO_HINT_MORE;
1280 break;
1281 } else {
1282 submit = crp;
1283 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1284 break;
1285 /* keep scanning for more are q'd */
1289 if (submit != NULL) {
1290 TAILQ_REMOVE(&crp_q, submit, crp_next);
1291 hid = CRYPTO_SESID2HID(submit->crp_sid);
1292 cap = crypto_checkdriver(hid);
1293 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1294 __func__, __LINE__));
1295 result = crypto_invoke(cap, submit, hint);
1296 if (result == ERESTART) {
1298 * The driver ran out of resources, mark the
1299 * driver ``blocked'' for cryptop's and put
1300 * the request back in the queue. It would
1301 * best to put the request back where we got
1302 * it but that's hard so for now we put it
1303 * at the front. This should be ok; putting
1304 * it at the end does not work.
1306 /* XXX validate sid again? */
1307 crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1308 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1309 cryptostats.cs_blocks++;
1313 /* As above, but for key ops */
1314 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1315 cap = crypto_checkdriver(krp->krp_hid);
1316 if (cap == NULL || cap->cc_dev == NULL) {
1318 * Operation needs to be migrated, invalidate
1319 * the assigned device so it will reselect a
1320 * new one below. Propagate the original
1321 * crid selection flags if supplied.
1323 krp->krp_hid = krp->krp_crid &
1324 (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1325 if (krp->krp_hid == 0)
1326 krp->krp_hid =
1327 CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1328 break;
1330 if (!cap->cc_kqblocked)
1331 break;
1333 if (krp != NULL) {
1334 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1335 result = crypto_kinvoke(krp, krp->krp_hid);
1336 if (result == ERESTART) {
1338 * The driver ran out of resources, mark the
1339 * driver ``blocked'' for cryptkop's and put
1340 * the request back in the queue. It would
1341 * best to put the request back where we got
1342 * it but that's hard so for now we put it
1343 * at the front. This should be ok; putting
1344 * it at the end does not work.
1346 /* XXX validate sid again? */
1347 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1348 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1349 cryptostats.cs_kblocks++;
1353 if (submit == NULL && krp == NULL) {
1355 * Nothing more to be processed. Sleep until we're
1356 * woken because there are more ops to process.
1357 * This happens either by submission or by a driver
1358 * becoming unblocked and notifying us through
1359 * crypto_unblock. Note that when we wakeup we
1360 * start processing each queue again from the
1361 * front. It's not clear that it's important to
1362 * preserve this ordering since ops may finish
1363 * out of order if dispatched to different devices
1364 * and some become blocked while others do not.
1366 crp_sleep = 1;
1367 lksleep (&crp_q, &crypto_q_lock, 0, "crypto_wait", 0);
1368 crp_sleep = 0;
1369 if (cryptothread == NULL)
1370 break;
1371 cryptostats.cs_intrs++;
1374 CRYPTO_Q_UNLOCK();
1376 crypto_finis(&crp_q);
1380 * Crypto returns thread, does callbacks for processed crypto requests.
1381 * Callbacks are done here, rather than in the crypto drivers, because
1382 * callbacks typically are expensive and would slow interrupt handling.
1384 static void
1385 crypto_ret_proc(void)
1387 struct cryptop *crpt;
1388 struct cryptkop *krpt;
1390 CRYPTO_RETQ_LOCK();
1391 for (;;) {
1392 /* Harvest return q's for completed ops */
1393 crpt = TAILQ_FIRST(&crp_ret_q);
1394 if (crpt != NULL)
1395 TAILQ_REMOVE(&crp_ret_q, crpt, crp_next);
1397 krpt = TAILQ_FIRST(&crp_ret_kq);
1398 if (krpt != NULL)
1399 TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next);
1401 if (crpt != NULL || krpt != NULL) {
1402 CRYPTO_RETQ_UNLOCK();
1404 * Run callbacks unlocked.
1406 if (crpt != NULL) {
1407 #ifdef CRYPTO_TIMING
1408 if (crypto_timing) {
1410 * NB: We must copy the timestamp before
1411 * doing the callback as the cryptop is
1412 * likely to be reclaimed.
1414 struct timespec t = crpt->crp_tstamp;
1415 crypto_tstat(&cryptostats.cs_cb, &t);
1416 crpt->crp_callback(crpt);
1417 crypto_tstat(&cryptostats.cs_finis, &t);
1418 } else
1419 #endif
1420 crpt->crp_callback(crpt);
1422 if (krpt != NULL)
1423 krpt->krp_callback(krpt);
1424 CRYPTO_RETQ_LOCK();
1425 } else {
1427 * Nothing more to be processed. Sleep until we're
1428 * woken because there are more returns to process.
1430 lksleep (&crp_ret_q, &crypto_ret_q_lock, 0, "crypto_ret_wait", 0);
1431 if (cryptoretthread == NULL) {
1432 break;
1434 cryptostats.cs_rets++;
1437 CRYPTO_RETQ_UNLOCK();
1439 crypto_finis(&crp_ret_q);
1442 #ifdef DDB
1443 static void
1444 db_show_drivers(void)
1446 int hid;
1448 db_printf("%12s %4s %4s %8s %2s %2s\n"
1449 , "Device"
1450 , "Ses"
1451 , "Kops"
1452 , "Flags"
1453 , "QB"
1454 , "KB"
1456 for (hid = 0; hid < crypto_drivers_num; hid++) {
1457 const struct cryptocap *cap = &crypto_drivers[hid];
1458 if (cap->cc_dev == NULL)
1459 continue;
1460 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1461 , device_get_nameunit(cap->cc_dev)
1462 , cap->cc_sessions
1463 , cap->cc_koperations
1464 , cap->cc_flags
1465 , cap->cc_qblocked
1466 , cap->cc_kqblocked
1471 DB_SHOW_COMMAND(crypto, db_show_crypto)
1473 struct cryptop *crp;
1475 db_show_drivers();
1476 db_printf("\n");
1478 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1479 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1480 "Desc", "Callback");
1481 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1482 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1483 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1484 , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
1485 , crp->crp_ilen, crp->crp_olen
1486 , crp->crp_etype
1487 , crp->crp_flags
1488 , crp->crp_desc
1489 , crp->crp_callback
1492 if (!TAILQ_EMPTY(&crp_ret_q)) {
1493 db_printf("\n%4s %4s %4s %8s\n",
1494 "HID", "Etype", "Flags", "Callback");
1495 TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
1496 db_printf("%4u %4u %04x %8p\n"
1497 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1498 , crp->crp_etype
1499 , crp->crp_flags
1500 , crp->crp_callback
1506 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1508 struct cryptkop *krp;
1510 db_show_drivers();
1511 db_printf("\n");
1513 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1514 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1515 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1516 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1517 , krp->krp_op
1518 , krp->krp_status
1519 , krp->krp_iparams, krp->krp_oparams
1520 , krp->krp_crid, krp->krp_hid
1521 , krp->krp_callback
1524 if (!TAILQ_EMPTY(&crp_ret_q)) {
1525 db_printf("%4s %5s %8s %4s %8s\n",
1526 "Op", "Status", "CRID", "HID", "Callback");
1527 TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
1528 db_printf("%4u %5u %08x %4u %8p\n"
1529 , krp->krp_op
1530 , krp->krp_status
1531 , krp->krp_crid, krp->krp_hid
1532 , krp->krp_callback
1537 #endif
1539 int crypto_modevent(module_t mod, int type, void *unused);
1542 * Initialization code, both for static and dynamic loading.
1543 * Note this is not invoked with the usual MODULE_DECLARE
1544 * mechanism but instead is listed as a dependency by the
1545 * cryptosoft driver. This guarantees proper ordering of
1546 * calls on module load/unload.
1549 crypto_modevent(module_t mod, int type, void *unused)
1551 int error = EINVAL;
1553 switch (type) {
1554 case MOD_LOAD:
1555 error = crypto_init();
1556 if (error == 0 && bootverbose)
1557 kprintf("crypto: <crypto core>\n");
1558 break;
1559 case MOD_UNLOAD:
1560 /*XXX disallow if active sessions */
1561 error = 0;
1562 crypto_destroy();
1563 return 0;
1565 return error;
1567 MODULE_VERSION(crypto, 1);
1568 MODULE_DEPEND(crypto, zlib, 1, 1, 1);