2 * Copyright (c) 2002-2006 Sam Leffler. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
29 * Cryptographic Subsystem.
31 * This code is derived from the Openbsd Cryptographic Framework (OCF)
32 * that has the copyright shown below. Very little of the original
37 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
39 * This code was written by Angelos D. Keromytis in Athens, Greece, in
40 * February 2000. Network Security Technologies Inc. (NSTI) kindly
41 * supported the development of this code.
43 * Copyright (c) 2000, 2001 Angelos D. Keromytis
45 * Permission to use, copy, and modify this software with or without fee
46 * is hereby granted, provided that this entire notice is included in
47 * all source code copies of any software which is or includes a copy or
48 * modification of this software.
50 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
51 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
52 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
53 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
57 #define CRYPTO_TIMING /* enable timing support */
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/eventhandler.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
66 #include <sys/linker.h>
68 #include <sys/module.h>
69 #include <sys/mutex.h>
70 #include <sys/malloc.h>
74 #include <sys/sysctl.h>
75 #include <sys/taskqueue.h>
80 #include <crypto/intake.h>
81 #include <opencrypto/cryptodev.h>
82 #include <opencrypto/xform.h> /* XXX for M_XDATA */
86 #include "cryptodev_if.h"
88 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
89 #include <machine/pcb.h>
92 struct crypto_session {
96 uint32_t capabilities;
99 SDT_PROVIDER_DEFINE(opencrypto);
102 * Crypto drivers register themselves by allocating a slot in the
103 * crypto_drivers table with crypto_get_driverid() and then registering
104 * each algorithm they support with crypto_register() and crypto_kregister().
106 static struct mtx crypto_drivers_mtx; /* lock on driver table */
107 #define CRYPTO_DRIVER_LOCK() mtx_lock(&crypto_drivers_mtx)
108 #define CRYPTO_DRIVER_UNLOCK() mtx_unlock(&crypto_drivers_mtx)
109 #define CRYPTO_DRIVER_ASSERT() mtx_assert(&crypto_drivers_mtx, MA_OWNED)
112 * Crypto device/driver capabilities structure.
115 * (d) - protected by CRYPTO_DRIVER_LOCK()
116 * (q) - protected by CRYPTO_Q_LOCK()
117 * Not tagged fields are read-only.
120 device_t cc_dev; /* (d) device/driver */
121 u_int32_t cc_sessions; /* (d) # of sessions */
122 u_int32_t cc_koperations; /* (d) # os asym operations */
124 * Largest possible operator length (in bits) for each type of
125 * encryption algorithm. XXX not used
127 u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
128 u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
129 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
131 int cc_flags; /* (d) flags */
132 #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
133 int cc_qblocked; /* (q) symmetric q blocked */
134 int cc_kqblocked; /* (q) asymmetric q blocked */
135 size_t cc_session_size;
137 static struct cryptocap *crypto_drivers = NULL;
138 static int crypto_drivers_num = 0;
141 * There are two queues for crypto requests; one for symmetric (e.g.
142 * cipher) operations and one for asymmetric (e.g. MOD)operations.
143 * A single mutex is used to lock access to both queues. We could
144 * have one per-queue but having one simplifies handling of block/unblock
147 static int crp_sleep = 0;
148 static TAILQ_HEAD(cryptop_q ,cryptop) crp_q; /* request queues */
149 static TAILQ_HEAD(,cryptkop) crp_kq;
150 static struct mtx crypto_q_mtx;
151 #define CRYPTO_Q_LOCK() mtx_lock(&crypto_q_mtx)
152 #define CRYPTO_Q_UNLOCK() mtx_unlock(&crypto_q_mtx)
155 * Taskqueue used to dispatch the crypto requests
156 * that have the CRYPTO_F_ASYNC flag
158 static struct taskqueue *crypto_tq;
161 * Crypto seq numbers are operated on with modular arithmetic
163 #define CRYPTO_SEQ_GT(a,b) ((int)((a)-(b)) > 0)
165 struct crypto_ret_worker {
166 struct mtx crypto_ret_mtx;
168 TAILQ_HEAD(,cryptop) crp_ordered_ret_q; /* ordered callback queue for symetric jobs */
169 TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queue for symetric jobs */
170 TAILQ_HEAD(,cryptkop) crp_ret_kq; /* callback queue for asym jobs */
172 u_int32_t reorder_ops; /* total ordered sym jobs received */
173 u_int32_t reorder_cur_seq; /* current sym job dispatched */
175 struct proc *cryptoretproc;
177 static struct crypto_ret_worker *crypto_ret_workers = NULL;
179 #define CRYPTO_RETW(i) (&crypto_ret_workers[i])
180 #define CRYPTO_RETW_ID(w) ((w) - crypto_ret_workers)
181 #define FOREACH_CRYPTO_RETW(w) \
182 for (w = crypto_ret_workers; w < crypto_ret_workers + crypto_workers_num; ++w)
184 #define CRYPTO_RETW_LOCK(w) mtx_lock(&w->crypto_ret_mtx)
185 #define CRYPTO_RETW_UNLOCK(w) mtx_unlock(&w->crypto_ret_mtx)
186 #define CRYPTO_RETW_EMPTY(w) \
187 (TAILQ_EMPTY(&w->crp_ret_q) && TAILQ_EMPTY(&w->crp_ret_kq) && TAILQ_EMPTY(&w->crp_ordered_ret_q))
189 static int crypto_workers_num = 0;
190 SYSCTL_INT(_kern, OID_AUTO, crypto_workers_num, CTLFLAG_RDTUN,
191 &crypto_workers_num, 0,
192 "Number of crypto workers used to dispatch crypto jobs");
194 static uma_zone_t cryptop_zone;
195 static uma_zone_t cryptodesc_zone;
196 static uma_zone_t cryptoses_zone;
198 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
199 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
200 &crypto_userasymcrypto, 0,
201 "Enable/disable user-mode access to asymmetric crypto support");
202 int crypto_devallowsoft = 0; /* only use hardware crypto */
203 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
204 &crypto_devallowsoft, 0,
205 "Enable/disable use of software crypto by /dev/crypto");
207 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
209 static void crypto_proc(void);
210 static struct proc *cryptoproc;
211 static void crypto_ret_proc(struct crypto_ret_worker *ret_worker);
212 static void crypto_destroy(void);
213 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
214 static int crypto_kinvoke(struct cryptkop *krp, int flags);
215 static void crypto_remove(struct cryptocap *cap);
216 static void crypto_task_invoke(void *ctx, int pending);
217 static void crypto_batch_enqueue(struct cryptop *crp);
219 static struct cryptostats cryptostats;
220 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
221 cryptostats, "Crypto system statistics");
224 static int crypto_timing = 0;
225 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
226 &crypto_timing, 0, "Enable/disable crypto timing support");
229 /* Try to avoid directly exposing the key buffer as a symbol */
230 static struct keybuf *keybuf;
232 static struct keybuf empty_keybuf = {
236 /* Obtain the key buffer from boot metadata */
242 kmdp = preload_search_by_type("elf kernel");
245 kmdp = preload_search_by_type("elf64 kernel");
247 keybuf = (struct keybuf *)preload_search_info(kmdp,
248 MODINFO_METADATA | MODINFOMD_KEYBUF);
251 keybuf = &empty_keybuf;
254 /* It'd be nice if we could store these in some kind of secure memory... */
255 struct keybuf * get_keybuf(void) {
263 struct crypto_ret_worker *ret_worker;
266 mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table",
271 mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF);
273 cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop),
275 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
276 cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc),
278 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
279 cryptoses_zone = uma_zcreate("crypto_session",
280 sizeof(struct crypto_session), NULL, NULL, NULL, NULL,
281 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
283 if (cryptodesc_zone == NULL || cryptop_zone == NULL ||
284 cryptoses_zone == NULL) {
285 printf("crypto_init: cannot setup crypto zones\n");
290 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
291 crypto_drivers = malloc(crypto_drivers_num *
292 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
293 if (crypto_drivers == NULL) {
294 printf("crypto_init: cannot setup crypto drivers\n");
299 if (crypto_workers_num < 1 || crypto_workers_num > mp_ncpus)
300 crypto_workers_num = mp_ncpus;
302 crypto_tq = taskqueue_create("crypto", M_WAITOK|M_ZERO,
303 taskqueue_thread_enqueue, &crypto_tq);
304 if (crypto_tq == NULL) {
305 printf("crypto init: cannot setup crypto taskqueue\n");
310 taskqueue_start_threads(&crypto_tq, crypto_workers_num, PRI_MIN_KERN,
313 error = kproc_create((void (*)(void *)) crypto_proc, NULL,
314 &cryptoproc, 0, 0, "crypto");
316 printf("crypto_init: cannot start crypto thread; error %d",
321 crypto_ret_workers = malloc(crypto_workers_num * sizeof(struct crypto_ret_worker),
322 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
323 if (crypto_ret_workers == NULL) {
325 printf("crypto_init: cannot allocate ret workers\n");
330 FOREACH_CRYPTO_RETW(ret_worker) {
331 TAILQ_INIT(&ret_worker->crp_ordered_ret_q);
332 TAILQ_INIT(&ret_worker->crp_ret_q);
333 TAILQ_INIT(&ret_worker->crp_ret_kq);
335 ret_worker->reorder_ops = 0;
336 ret_worker->reorder_cur_seq = 0;
338 mtx_init(&ret_worker->crypto_ret_mtx, "crypto", "crypto return queues", MTX_DEF);
340 error = kproc_create((void (*)(void *)) crypto_ret_proc, ret_worker,
341 &ret_worker->cryptoretproc, 0, 0, "crypto returns %td", CRYPTO_RETW_ID(ret_worker));
343 printf("crypto_init: cannot start cryptoret thread; error %d",
358 * Signal a crypto thread to terminate. We use the driver
359 * table lock to synchronize the sleep/wakeups so that we
360 * are sure the threads have terminated before we release
361 * the data structures they use. See crypto_finis below
362 * for the other half of this song-and-dance.
365 crypto_terminate(struct proc **pp, void *q)
369 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
374 PROC_LOCK(p); /* NB: insure we don't miss wakeup */
375 CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */
376 msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0);
378 CRYPTO_DRIVER_LOCK();
385 struct crypto_ret_worker *ret_worker;
388 * Terminate any crypto threads.
390 if (crypto_tq != NULL)
391 taskqueue_drain_all(crypto_tq);
392 CRYPTO_DRIVER_LOCK();
393 crypto_terminate(&cryptoproc, &crp_q);
394 FOREACH_CRYPTO_RETW(ret_worker)
395 crypto_terminate(&ret_worker->cryptoretproc, &ret_worker->crp_ret_q);
396 CRYPTO_DRIVER_UNLOCK();
398 /* XXX flush queues??? */
401 * Reclaim dynamically allocated resources.
403 if (crypto_drivers != NULL)
404 free(crypto_drivers, M_CRYPTO_DATA);
406 if (cryptoses_zone != NULL)
407 uma_zdestroy(cryptoses_zone);
408 if (cryptodesc_zone != NULL)
409 uma_zdestroy(cryptodesc_zone);
410 if (cryptop_zone != NULL)
411 uma_zdestroy(cryptop_zone);
412 mtx_destroy(&crypto_q_mtx);
413 FOREACH_CRYPTO_RETW(ret_worker)
414 mtx_destroy(&ret_worker->crypto_ret_mtx);
415 free(crypto_ret_workers, M_CRYPTO_DATA);
416 if (crypto_tq != NULL)
417 taskqueue_free(crypto_tq);
418 mtx_destroy(&crypto_drivers_mtx);
422 crypto_ses2hid(crypto_session_t crypto_session)
424 return (crypto_session->hid);
428 crypto_ses2caps(crypto_session_t crypto_session)
430 return (crypto_session->capabilities);
434 crypto_get_driver_session(crypto_session_t crypto_session)
436 return (crypto_session->softc);
439 static struct cryptocap *
440 crypto_checkdriver(u_int32_t hid)
442 if (crypto_drivers == NULL)
444 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
448 * Compare a driver's list of supported algorithms against another
449 * list; return non-zero if all algorithms are supported.
452 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
454 const struct cryptoini *cr;
456 /* See if all the algorithms are supported. */
457 for (cr = cri; cr; cr = cr->cri_next)
458 if (cap->cc_alg[cr->cri_alg] == 0)
464 * Select a driver for a new session that supports the specified
465 * algorithms and, optionally, is constrained according to the flags.
466 * The algorithm we use here is pretty stupid; just use the
467 * first driver that supports all the algorithms we need. If there
468 * are multiple drivers we choose the driver with the fewest active
469 * sessions. We prefer hardware-backed drivers to software ones.
471 * XXX We need more smarts here (in real life too, but that's
472 * XXX another story altogether).
474 static struct cryptocap *
475 crypto_select_driver(const struct cryptoini *cri, int flags)
477 struct cryptocap *cap, *best;
480 CRYPTO_DRIVER_ASSERT();
483 * Look first for hardware crypto devices if permitted.
485 if (flags & CRYPTOCAP_F_HARDWARE)
486 match = CRYPTOCAP_F_HARDWARE;
488 match = CRYPTOCAP_F_SOFTWARE;
491 for (hid = 0; hid < crypto_drivers_num; hid++) {
492 cap = &crypto_drivers[hid];
494 * If it's not initialized, is in the process of
495 * going away, or is not appropriate (hardware
496 * or software based on match), then skip.
498 if (cap->cc_dev == NULL ||
499 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
500 (cap->cc_flags & match) == 0)
503 /* verify all the algorithms are supported. */
504 if (driver_suitable(cap, cri)) {
506 cap->cc_sessions < best->cc_sessions)
510 if (best == NULL && match == CRYPTOCAP_F_HARDWARE &&
511 (flags & CRYPTOCAP_F_SOFTWARE)) {
512 /* sort of an Algol 68-style for loop */
513 match = CRYPTOCAP_F_SOFTWARE;
520 * Create a new session. The crid argument specifies a crypto
521 * driver to use or constraints on a driver to select (hardware
522 * only, software only, either). Whatever driver is selected
523 * must be capable of the requested crypto algorithms.
526 crypto_newsession(crypto_session_t *cses, struct cryptoini *cri, int crid)
528 crypto_session_t res;
530 struct cryptocap *cap;
539 CRYPTO_DRIVER_LOCK();
540 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
542 * Use specified driver; verify it is capable.
544 cap = crypto_checkdriver(crid);
545 if (cap != NULL && !driver_suitable(cap, cri))
549 * No requested driver; select based on crid flags.
551 cap = crypto_select_driver(cri, crid);
553 * if NULL then can't do everything in one session.
554 * XXX Fix this. We need to inject a "virtual" session
555 * XXX layer right about here.
559 CRYPTDEB("no driver");
564 softc_size = cap->cc_session_size;
565 hid = cap - crypto_drivers;
567 CRYPTO_DRIVER_UNLOCK();
569 softc_mem = malloc(softc_size, M_CRYPTO_DATA, M_WAITOK | M_ZERO);
570 res = uma_zalloc(cryptoses_zone, M_WAITOK | M_ZERO);
571 res->softc = softc_mem;
573 CRYPTO_DRIVER_LOCK();
574 cap = crypto_checkdriver(hid);
575 if (cap != NULL && (cap->cc_flags & CRYPTOCAP_F_CLEANUP) != 0) {
581 free(softc_mem, M_CRYPTO_DATA);
582 uma_zfree(cryptoses_zone, res);
583 CRYPTO_DRIVER_UNLOCK();
587 /* Call the driver initialization routine. */
588 err = CRYPTODEV_NEWSESSION(cap->cc_dev, res, cri);
590 CRYPTDEB("dev newsession failed: %d", err);
594 res->capabilities = cap->cc_flags & 0xff000000;
599 CRYPTO_DRIVER_UNLOCK();
601 free(softc_mem, M_CRYPTO_DATA);
603 uma_zfree(cryptoses_zone, res);
609 crypto_remove(struct cryptocap *cap)
612 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
613 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
614 bzero(cap, sizeof(*cap));
618 * Delete an existing session (or a reserved session on an unregistered
622 crypto_freesession(crypto_session_t cses)
624 struct cryptocap *cap;
632 CRYPTO_DRIVER_LOCK();
634 hid = crypto_ses2hid(cses);
635 KASSERT(hid < crypto_drivers_num,
636 ("bogus crypto_session %p hid %u", cses, hid));
637 cap = &crypto_drivers[hid];
640 ses_size = cap->cc_session_size;
642 if (cap->cc_sessions)
645 /* Call the driver cleanup routine, if available. */
646 CRYPTODEV_FREESESSION(cap->cc_dev, cses);
648 explicit_bzero(ses, ses_size);
649 free(ses, M_CRYPTO_DATA);
650 uma_zfree(cryptoses_zone, cses);
652 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
655 CRYPTO_DRIVER_UNLOCK();
659 * Return an unused driver id. Used by drivers prior to registering
660 * support for the algorithms they handle.
663 crypto_get_driverid(device_t dev, size_t sessionsize, int flags)
665 struct cryptocap *newdrv;
668 if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
669 printf("%s: no flags specified when registering driver\n",
670 device_get_nameunit(dev));
674 CRYPTO_DRIVER_LOCK();
676 for (i = 0; i < crypto_drivers_num; i++) {
677 if (crypto_drivers[i].cc_dev == NULL &&
678 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
683 /* Out of entries, allocate some more. */
684 if (i == crypto_drivers_num) {
685 /* Be careful about wrap-around. */
686 if (2 * crypto_drivers_num <= crypto_drivers_num) {
687 CRYPTO_DRIVER_UNLOCK();
688 printf("crypto: driver count wraparound!\n");
692 newdrv = malloc(2 * crypto_drivers_num *
693 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
694 if (newdrv == NULL) {
695 CRYPTO_DRIVER_UNLOCK();
696 printf("crypto: no space to expand driver table!\n");
700 bcopy(crypto_drivers, newdrv,
701 crypto_drivers_num * sizeof(struct cryptocap));
703 crypto_drivers_num *= 2;
705 free(crypto_drivers, M_CRYPTO_DATA);
706 crypto_drivers = newdrv;
709 /* NB: state is zero'd on free */
710 crypto_drivers[i].cc_sessions = 1; /* Mark */
711 crypto_drivers[i].cc_dev = dev;
712 crypto_drivers[i].cc_flags = flags;
713 crypto_drivers[i].cc_session_size = sessionsize;
715 printf("crypto: assign %s driver id %u, flags 0x%x\n",
716 device_get_nameunit(dev), i, flags);
718 CRYPTO_DRIVER_UNLOCK();
724 * Lookup a driver by name. We match against the full device
725 * name and unit, and against just the name. The latter gives
726 * us a simple widlcarding by device name. On success return the
727 * driver/hardware identifier; otherwise return -1.
730 crypto_find_driver(const char *match)
732 int i, len = strlen(match);
734 CRYPTO_DRIVER_LOCK();
735 for (i = 0; i < crypto_drivers_num; i++) {
736 device_t dev = crypto_drivers[i].cc_dev;
738 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
740 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
741 strncmp(match, device_get_name(dev), len) == 0)
744 CRYPTO_DRIVER_UNLOCK();
745 return i < crypto_drivers_num ? i : -1;
749 * Return the device_t for the specified driver or NULL
750 * if the driver identifier is invalid.
753 crypto_find_device_byhid(int hid)
755 struct cryptocap *cap = crypto_checkdriver(hid);
756 return cap != NULL ? cap->cc_dev : NULL;
760 * Return the device/driver capabilities.
763 crypto_getcaps(int hid)
765 struct cryptocap *cap = crypto_checkdriver(hid);
766 return cap != NULL ? cap->cc_flags : 0;
770 * Register support for a key-related algorithm. This routine
771 * is called once for each algorithm supported a driver.
774 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
776 struct cryptocap *cap;
779 CRYPTO_DRIVER_LOCK();
781 cap = crypto_checkdriver(driverid);
783 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
785 * XXX Do some performance testing to determine placing.
786 * XXX We probably need an auxiliary data structure that
787 * XXX describes relative performances.
790 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
792 printf("crypto: %s registers key alg %u flags %u\n"
793 , device_get_nameunit(cap->cc_dev)
801 CRYPTO_DRIVER_UNLOCK();
806 * Register support for a non-key-related algorithm. This routine
807 * is called once for each such algorithm supported by a driver.
810 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
813 struct cryptocap *cap;
816 CRYPTO_DRIVER_LOCK();
818 cap = crypto_checkdriver(driverid);
819 /* NB: algorithms are in the range [1..max] */
821 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
823 * XXX Do some performance testing to determine placing.
824 * XXX We probably need an auxiliary data structure that
825 * XXX describes relative performances.
828 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
829 cap->cc_max_op_len[alg] = maxoplen;
831 printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
832 , device_get_nameunit(cap->cc_dev)
837 cap->cc_sessions = 0; /* Unmark */
842 CRYPTO_DRIVER_UNLOCK();
847 driver_finis(struct cryptocap *cap)
851 CRYPTO_DRIVER_ASSERT();
853 ses = cap->cc_sessions;
854 kops = cap->cc_koperations;
855 bzero(cap, sizeof(*cap));
856 if (ses != 0 || kops != 0) {
858 * If there are pending sessions,
859 * just mark as invalid.
861 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
862 cap->cc_sessions = ses;
863 cap->cc_koperations = kops;
868 * Unregister a crypto driver. If there are pending sessions using it,
869 * leave enough information around so that subsequent calls using those
870 * sessions will correctly detect the driver has been unregistered and
874 crypto_unregister(u_int32_t driverid, int alg)
876 struct cryptocap *cap;
879 CRYPTO_DRIVER_LOCK();
880 cap = crypto_checkdriver(driverid);
882 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
883 cap->cc_alg[alg] != 0) {
884 cap->cc_alg[alg] = 0;
885 cap->cc_max_op_len[alg] = 0;
887 /* Was this the last algorithm ? */
888 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
889 if (cap->cc_alg[i] != 0)
892 if (i == CRYPTO_ALGORITHM_MAX + 1)
897 CRYPTO_DRIVER_UNLOCK();
903 * Unregister all algorithms associated with a crypto driver.
904 * If there are pending sessions using it, leave enough information
905 * around so that subsequent calls using those sessions will
906 * correctly detect the driver has been unregistered and reroute
910 crypto_unregister_all(u_int32_t driverid)
912 struct cryptocap *cap;
915 CRYPTO_DRIVER_LOCK();
916 cap = crypto_checkdriver(driverid);
922 CRYPTO_DRIVER_UNLOCK();
928 * Clear blockage on a driver. The what parameter indicates whether
929 * the driver is now ready for cryptop's and/or cryptokop's.
932 crypto_unblock(u_int32_t driverid, int what)
934 struct cryptocap *cap;
938 cap = crypto_checkdriver(driverid);
940 if (what & CRYPTO_SYMQ)
941 cap->cc_qblocked = 0;
942 if (what & CRYPTO_ASYMQ)
943 cap->cc_kqblocked = 0;
955 * Add a crypto request to a queue, to be processed by the kernel thread.
958 crypto_dispatch(struct cryptop *crp)
960 struct cryptocap *cap;
964 cryptostats.cs_ops++;
968 binuptime(&crp->crp_tstamp);
971 crp->crp_retw_id = ((uintptr_t)crp->crp_session) % crypto_workers_num;
973 if (CRYPTOP_ASYNC(crp)) {
974 if (crp->crp_flags & CRYPTO_F_ASYNC_KEEPORDER) {
975 struct crypto_ret_worker *ret_worker;
977 ret_worker = CRYPTO_RETW(crp->crp_retw_id);
979 CRYPTO_RETW_LOCK(ret_worker);
980 crp->crp_seq = ret_worker->reorder_ops++;
981 CRYPTO_RETW_UNLOCK(ret_worker);
984 TASK_INIT(&crp->crp_task, 0, crypto_task_invoke, crp);
985 taskqueue_enqueue(crypto_tq, &crp->crp_task);
989 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
990 hid = crypto_ses2hid(crp->crp_session);
993 * Caller marked the request to be processed
994 * immediately; dispatch it directly to the
995 * driver unless the driver is currently blocked.
997 cap = crypto_checkdriver(hid);
998 /* Driver cannot disappeared when there is an active session. */
999 KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
1000 if (!cap->cc_qblocked) {
1001 result = crypto_invoke(cap, crp, 0);
1002 if (result != ERESTART)
1005 * The driver ran out of resources, put the request on
1010 crypto_batch_enqueue(crp);
1015 crypto_batch_enqueue(struct cryptop *crp)
1019 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
1026 * Add an asymetric crypto request to a queue,
1027 * to be processed by the kernel thread.
1030 crypto_kdispatch(struct cryptkop *krp)
1034 cryptostats.cs_kops++;
1036 error = crypto_kinvoke(krp, krp->krp_crid);
1037 if (error == ERESTART) {
1039 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
1049 * Verify a driver is suitable for the specified operation.
1052 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
1054 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
1058 * Select a driver for an asym operation. The driver must
1059 * support the necessary algorithm. The caller can constrain
1060 * which device is selected with the flags parameter. The
1061 * algorithm we use here is pretty stupid; just use the first
1062 * driver that supports the algorithms we need. If there are
1063 * multiple suitable drivers we choose the driver with the
1064 * fewest active operations. We prefer hardware-backed
1065 * drivers to software ones when either may be used.
1067 static struct cryptocap *
1068 crypto_select_kdriver(const struct cryptkop *krp, int flags)
1070 struct cryptocap *cap, *best;
1073 CRYPTO_DRIVER_ASSERT();
1076 * Look first for hardware crypto devices if permitted.
1078 if (flags & CRYPTOCAP_F_HARDWARE)
1079 match = CRYPTOCAP_F_HARDWARE;
1081 match = CRYPTOCAP_F_SOFTWARE;
1084 for (hid = 0; hid < crypto_drivers_num; hid++) {
1085 cap = &crypto_drivers[hid];
1087 * If it's not initialized, is in the process of
1088 * going away, or is not appropriate (hardware
1089 * or software based on match), then skip.
1091 if (cap->cc_dev == NULL ||
1092 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
1093 (cap->cc_flags & match) == 0)
1096 /* verify all the algorithms are supported. */
1097 if (kdriver_suitable(cap, krp)) {
1099 cap->cc_koperations < best->cc_koperations)
1105 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
1106 /* sort of an Algol 68-style for loop */
1107 match = CRYPTOCAP_F_SOFTWARE;
1114 * Dispatch an asymmetric crypto request.
1117 crypto_kinvoke(struct cryptkop *krp, int crid)
1119 struct cryptocap *cap = NULL;
1122 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
1123 KASSERT(krp->krp_callback != NULL,
1124 ("%s: krp->crp_callback == NULL", __func__));
1126 CRYPTO_DRIVER_LOCK();
1127 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
1128 cap = crypto_checkdriver(crid);
1131 * Driver present, it must support the necessary
1132 * algorithm and, if s/w drivers are excluded,
1133 * it must be registered as hardware-backed.
1135 if (!kdriver_suitable(cap, krp) ||
1136 (!crypto_devallowsoft &&
1137 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
1142 * No requested driver; select based on crid flags.
1144 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
1145 crid &= ~CRYPTOCAP_F_SOFTWARE;
1146 cap = crypto_select_kdriver(krp, crid);
1148 if (cap != NULL && !cap->cc_kqblocked) {
1149 krp->krp_hid = cap - crypto_drivers;
1150 cap->cc_koperations++;
1151 CRYPTO_DRIVER_UNLOCK();
1152 error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
1153 CRYPTO_DRIVER_LOCK();
1154 if (error == ERESTART) {
1155 cap->cc_koperations--;
1156 CRYPTO_DRIVER_UNLOCK();
1161 * NB: cap is !NULL if device is blocked; in
1162 * that case return ERESTART so the operation
1163 * is resubmitted if possible.
1165 error = (cap == NULL) ? ENODEV : ERESTART;
1167 CRYPTO_DRIVER_UNLOCK();
1170 krp->krp_status = error;
1176 #ifdef CRYPTO_TIMING
1178 crypto_tstat(struct cryptotstat *ts, struct bintime *bt)
1180 struct bintime now, delta;
1186 delta.frac = now.frac - bt->frac;
1187 delta.sec = now.sec - bt->sec;
1190 bintime2timespec(&delta, &t);
1191 timespecadd(&ts->acc, &t, &ts->acc);
1192 if (timespeccmp(&t, &ts->min, <))
1194 if (timespeccmp(&t, &ts->max, >))
1203 crypto_task_invoke(void *ctx, int pending)
1205 struct cryptocap *cap;
1206 struct cryptop *crp;
1209 crp = (struct cryptop *)ctx;
1211 hid = crypto_ses2hid(crp->crp_session);
1212 cap = crypto_checkdriver(hid);
1214 result = crypto_invoke(cap, crp, 0);
1215 if (result == ERESTART)
1216 crypto_batch_enqueue(crp);
1220 * Dispatch a crypto request to the appropriate crypto devices.
1223 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1226 KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
1227 KASSERT(crp->crp_callback != NULL,
1228 ("%s: crp->crp_callback == NULL", __func__));
1229 KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
1231 #ifdef CRYPTO_TIMING
1233 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1235 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1236 struct cryptodesc *crd;
1237 crypto_session_t nses;
1240 * Driver has unregistered; migrate the session and return
1241 * an error to the caller so they'll resubmit the op.
1243 * XXX: What if there are more already queued requests for this
1246 crypto_freesession(crp->crp_session);
1248 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1249 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1251 /* XXX propagate flags from initial session? */
1252 if (crypto_newsession(&nses, &(crp->crp_desc->CRD_INI),
1253 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
1254 crp->crp_session = nses;
1256 crp->crp_etype = EAGAIN;
1261 * Invoke the driver to process the request.
1263 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1268 * Release a set of crypto descriptors.
1271 crypto_freereq(struct cryptop *crp)
1273 struct cryptodesc *crd;
1280 struct cryptop *crp2;
1281 struct crypto_ret_worker *ret_worker;
1284 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1285 KASSERT(crp2 != crp,
1286 ("Freeing cryptop from the crypto queue (%p).",
1291 FOREACH_CRYPTO_RETW(ret_worker) {
1292 CRYPTO_RETW_LOCK(ret_worker);
1293 TAILQ_FOREACH(crp2, &ret_worker->crp_ret_q, crp_next) {
1294 KASSERT(crp2 != crp,
1295 ("Freeing cryptop from the return queue (%p).",
1298 CRYPTO_RETW_UNLOCK(ret_worker);
1303 while ((crd = crp->crp_desc) != NULL) {
1304 crp->crp_desc = crd->crd_next;
1305 uma_zfree(cryptodesc_zone, crd);
1307 uma_zfree(cryptop_zone, crp);
1311 * Acquire a set of crypto descriptors.
1314 crypto_getreq(int num)
1316 struct cryptodesc *crd;
1317 struct cryptop *crp;
1319 crp = uma_zalloc(cryptop_zone, M_NOWAIT|M_ZERO);
1322 crd = uma_zalloc(cryptodesc_zone, M_NOWAIT|M_ZERO);
1324 crypto_freereq(crp);
1328 crd->crd_next = crp->crp_desc;
1329 crp->crp_desc = crd;
1336 * Invoke the callback on behalf of the driver.
1339 crypto_done(struct cryptop *crp)
1341 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
1342 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
1343 crp->crp_flags |= CRYPTO_F_DONE;
1344 if (crp->crp_etype != 0)
1345 cryptostats.cs_errs++;
1346 #ifdef CRYPTO_TIMING
1348 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
1351 * CBIMM means unconditionally do the callback immediately;
1352 * CBIFSYNC means do the callback immediately only if the
1353 * operation was done synchronously. Both are used to avoid
1354 * doing extraneous context switches; the latter is mostly
1355 * used with the software crypto driver.
1357 if (!CRYPTOP_ASYNC_KEEPORDER(crp) &&
1358 ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1359 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1360 (crypto_ses2caps(crp->crp_session) & CRYPTOCAP_F_SYNC)))) {
1362 * Do the callback directly. This is ok when the
1363 * callback routine does very little (e.g. the
1364 * /dev/crypto callback method just does a wakeup).
1366 #ifdef CRYPTO_TIMING
1367 if (crypto_timing) {
1369 * NB: We must copy the timestamp before
1370 * doing the callback as the cryptop is
1371 * likely to be reclaimed.
1373 struct bintime t = crp->crp_tstamp;
1374 crypto_tstat(&cryptostats.cs_cb, &t);
1375 crp->crp_callback(crp);
1376 crypto_tstat(&cryptostats.cs_finis, &t);
1379 crp->crp_callback(crp);
1381 struct crypto_ret_worker *ret_worker;
1384 ret_worker = CRYPTO_RETW(crp->crp_retw_id);
1388 * Normal case; queue the callback for the thread.
1390 CRYPTO_RETW_LOCK(ret_worker);
1391 if (CRYPTOP_ASYNC_KEEPORDER(crp)) {
1392 struct cryptop *tmp;
1394 TAILQ_FOREACH_REVERSE(tmp, &ret_worker->crp_ordered_ret_q,
1395 cryptop_q, crp_next) {
1396 if (CRYPTO_SEQ_GT(crp->crp_seq, tmp->crp_seq)) {
1397 TAILQ_INSERT_AFTER(&ret_worker->crp_ordered_ret_q,
1398 tmp, crp, crp_next);
1403 TAILQ_INSERT_HEAD(&ret_worker->crp_ordered_ret_q,
1407 if (crp->crp_seq == ret_worker->reorder_cur_seq)
1411 if (CRYPTO_RETW_EMPTY(ret_worker))
1414 TAILQ_INSERT_TAIL(&ret_worker->crp_ret_q, crp, crp_next);
1418 wakeup_one(&ret_worker->crp_ret_q); /* shared wait channel */
1419 CRYPTO_RETW_UNLOCK(ret_worker);
1424 * Invoke the callback on behalf of the driver.
1427 crypto_kdone(struct cryptkop *krp)
1429 struct crypto_ret_worker *ret_worker;
1430 struct cryptocap *cap;
1432 if (krp->krp_status != 0)
1433 cryptostats.cs_kerrs++;
1434 CRYPTO_DRIVER_LOCK();
1435 /* XXX: What if driver is loaded in the meantime? */
1436 if (krp->krp_hid < crypto_drivers_num) {
1437 cap = &crypto_drivers[krp->krp_hid];
1438 KASSERT(cap->cc_koperations > 0, ("cc_koperations == 0"));
1439 cap->cc_koperations--;
1440 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1443 CRYPTO_DRIVER_UNLOCK();
1445 ret_worker = CRYPTO_RETW(0);
1447 CRYPTO_RETW_LOCK(ret_worker);
1448 if (CRYPTO_RETW_EMPTY(ret_worker))
1449 wakeup_one(&ret_worker->crp_ret_q); /* shared wait channel */
1450 TAILQ_INSERT_TAIL(&ret_worker->crp_ret_kq, krp, krp_next);
1451 CRYPTO_RETW_UNLOCK(ret_worker);
1455 crypto_getfeat(int *featp)
1457 int hid, kalg, feat = 0;
1459 CRYPTO_DRIVER_LOCK();
1460 for (hid = 0; hid < crypto_drivers_num; hid++) {
1461 const struct cryptocap *cap = &crypto_drivers[hid];
1463 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1464 !crypto_devallowsoft) {
1467 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1468 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1471 CRYPTO_DRIVER_UNLOCK();
1477 * Terminate a thread at module unload. The process that
1478 * initiated this is waiting for us to signal that we're gone;
1479 * wake it up and exit. We use the driver table lock to insure
1480 * we don't do the wakeup before they're waiting. There is no
1481 * race here because the waiter sleeps on the proc lock for the
1482 * thread so it gets notified at the right time because of an
1483 * extra wakeup that's done in exit1().
1486 crypto_finis(void *chan)
1488 CRYPTO_DRIVER_LOCK();
1490 CRYPTO_DRIVER_UNLOCK();
1495 * Crypto thread, dispatches crypto requests.
1500 struct cryptop *crp, *submit;
1501 struct cryptkop *krp;
1502 struct cryptocap *cap;
1506 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1507 fpu_kern_thread(FPU_KERN_NORMAL);
1513 * Find the first element in the queue that can be
1514 * processed and look-ahead to see if multiple ops
1515 * are ready for the same driver.
1519 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1520 hid = crypto_ses2hid(crp->crp_session);
1521 cap = crypto_checkdriver(hid);
1523 * Driver cannot disappeared when there is an active
1526 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1527 __func__, __LINE__));
1528 if (cap == NULL || cap->cc_dev == NULL) {
1529 /* Op needs to be migrated, process it. */
1534 if (!cap->cc_qblocked) {
1535 if (submit != NULL) {
1537 * We stop on finding another op,
1538 * regardless whether its for the same
1539 * driver or not. We could keep
1540 * searching the queue but it might be
1541 * better to just use a per-driver
1544 if (crypto_ses2hid(submit->crp_session) == hid)
1545 hint = CRYPTO_HINT_MORE;
1549 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1551 /* keep scanning for more are q'd */
1555 if (submit != NULL) {
1556 TAILQ_REMOVE(&crp_q, submit, crp_next);
1557 hid = crypto_ses2hid(submit->crp_session);
1558 cap = crypto_checkdriver(hid);
1559 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1560 __func__, __LINE__));
1561 result = crypto_invoke(cap, submit, hint);
1562 if (result == ERESTART) {
1564 * The driver ran out of resources, mark the
1565 * driver ``blocked'' for cryptop's and put
1566 * the request back in the queue. It would
1567 * best to put the request back where we got
1568 * it but that's hard so for now we put it
1569 * at the front. This should be ok; putting
1570 * it at the end does not work.
1572 /* XXX validate sid again? */
1573 crypto_drivers[crypto_ses2hid(submit->crp_session)].cc_qblocked = 1;
1574 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1575 cryptostats.cs_blocks++;
1579 /* As above, but for key ops */
1580 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1581 cap = crypto_checkdriver(krp->krp_hid);
1582 if (cap == NULL || cap->cc_dev == NULL) {
1584 * Operation needs to be migrated, invalidate
1585 * the assigned device so it will reselect a
1586 * new one below. Propagate the original
1587 * crid selection flags if supplied.
1589 krp->krp_hid = krp->krp_crid &
1590 (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1591 if (krp->krp_hid == 0)
1593 CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1596 if (!cap->cc_kqblocked)
1600 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1601 result = crypto_kinvoke(krp, krp->krp_hid);
1602 if (result == ERESTART) {
1604 * The driver ran out of resources, mark the
1605 * driver ``blocked'' for cryptkop's and put
1606 * the request back in the queue. It would
1607 * best to put the request back where we got
1608 * it but that's hard so for now we put it
1609 * at the front. This should be ok; putting
1610 * it at the end does not work.
1612 /* XXX validate sid again? */
1613 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1614 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1615 cryptostats.cs_kblocks++;
1619 if (submit == NULL && krp == NULL) {
1621 * Nothing more to be processed. Sleep until we're
1622 * woken because there are more ops to process.
1623 * This happens either by submission or by a driver
1624 * becoming unblocked and notifying us through
1625 * crypto_unblock. Note that when we wakeup we
1626 * start processing each queue again from the
1627 * front. It's not clear that it's important to
1628 * preserve this ordering since ops may finish
1629 * out of order if dispatched to different devices
1630 * and some become blocked while others do not.
1633 msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0);
1635 if (cryptoproc == NULL)
1637 cryptostats.cs_intrs++;
1642 crypto_finis(&crp_q);
1646 * Crypto returns thread, does callbacks for processed crypto requests.
1647 * Callbacks are done here, rather than in the crypto drivers, because
1648 * callbacks typically are expensive and would slow interrupt handling.
1651 crypto_ret_proc(struct crypto_ret_worker *ret_worker)
1653 struct cryptop *crpt;
1654 struct cryptkop *krpt;
1656 CRYPTO_RETW_LOCK(ret_worker);
1658 /* Harvest return q's for completed ops */
1659 crpt = TAILQ_FIRST(&ret_worker->crp_ordered_ret_q);
1661 if (crpt->crp_seq == ret_worker->reorder_cur_seq) {
1662 TAILQ_REMOVE(&ret_worker->crp_ordered_ret_q, crpt, crp_next);
1663 ret_worker->reorder_cur_seq++;
1670 crpt = TAILQ_FIRST(&ret_worker->crp_ret_q);
1672 TAILQ_REMOVE(&ret_worker->crp_ret_q, crpt, crp_next);
1675 krpt = TAILQ_FIRST(&ret_worker->crp_ret_kq);
1677 TAILQ_REMOVE(&ret_worker->crp_ret_kq, krpt, krp_next);
1679 if (crpt != NULL || krpt != NULL) {
1680 CRYPTO_RETW_UNLOCK(ret_worker);
1682 * Run callbacks unlocked.
1685 #ifdef CRYPTO_TIMING
1686 if (crypto_timing) {
1688 * NB: We must copy the timestamp before
1689 * doing the callback as the cryptop is
1690 * likely to be reclaimed.
1692 struct bintime t = crpt->crp_tstamp;
1693 crypto_tstat(&cryptostats.cs_cb, &t);
1694 crpt->crp_callback(crpt);
1695 crypto_tstat(&cryptostats.cs_finis, &t);
1698 crpt->crp_callback(crpt);
1701 krpt->krp_callback(krpt);
1702 CRYPTO_RETW_LOCK(ret_worker);
1705 * Nothing more to be processed. Sleep until we're
1706 * woken because there are more returns to process.
1708 msleep(&ret_worker->crp_ret_q, &ret_worker->crypto_ret_mtx, PWAIT,
1709 "crypto_ret_wait", 0);
1710 if (ret_worker->cryptoretproc == NULL)
1712 cryptostats.cs_rets++;
1715 CRYPTO_RETW_UNLOCK(ret_worker);
1717 crypto_finis(&ret_worker->crp_ret_q);
1722 db_show_drivers(void)
1726 db_printf("%12s %4s %4s %8s %2s %2s\n"
1734 for (hid = 0; hid < crypto_drivers_num; hid++) {
1735 const struct cryptocap *cap = &crypto_drivers[hid];
1736 if (cap->cc_dev == NULL)
1738 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1739 , device_get_nameunit(cap->cc_dev)
1741 , cap->cc_koperations
1749 DB_SHOW_COMMAND(crypto, db_show_crypto)
1751 struct cryptop *crp;
1752 struct crypto_ret_worker *ret_worker;
1757 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1758 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1759 "Desc", "Callback");
1760 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1761 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1762 , (int) crypto_ses2hid(crp->crp_session)
1763 , (int) crypto_ses2caps(crp->crp_session)
1764 , crp->crp_ilen, crp->crp_olen
1771 FOREACH_CRYPTO_RETW(ret_worker) {
1772 db_printf("\n%8s %4s %4s %4s %8s\n",
1773 "ret_worker", "HID", "Etype", "Flags", "Callback");
1774 if (!TAILQ_EMPTY(&ret_worker->crp_ret_q)) {
1775 TAILQ_FOREACH(crp, &ret_worker->crp_ret_q, crp_next) {
1776 db_printf("%8td %4u %4u %04x %8p\n"
1777 , CRYPTO_RETW_ID(ret_worker)
1778 , (int) crypto_ses2hid(crp->crp_session)
1788 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1790 struct cryptkop *krp;
1791 struct crypto_ret_worker *ret_worker;
1796 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1797 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1798 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1799 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1802 , krp->krp_iparams, krp->krp_oparams
1803 , krp->krp_crid, krp->krp_hid
1808 ret_worker = CRYPTO_RETW(0);
1809 if (!TAILQ_EMPTY(&ret_worker->crp_ret_q)) {
1810 db_printf("%4s %5s %8s %4s %8s\n",
1811 "Op", "Status", "CRID", "HID", "Callback");
1812 TAILQ_FOREACH(krp, &ret_worker->crp_ret_kq, krp_next) {
1813 db_printf("%4u %5u %08x %4u %8p\n"
1816 , krp->krp_crid, krp->krp_hid
1824 int crypto_modevent(module_t mod, int type, void *unused);
1827 * Initialization code, both for static and dynamic loading.
1828 * Note this is not invoked with the usual MODULE_DECLARE
1829 * mechanism but instead is listed as a dependency by the
1830 * cryptosoft driver. This guarantees proper ordering of
1831 * calls on module load/unload.
1834 crypto_modevent(module_t mod, int type, void *unused)
1840 error = crypto_init();
1841 if (error == 0 && bootverbose)
1842 printf("crypto: <crypto core>\n");
1845 /*XXX disallow if active sessions */
1852 MODULE_VERSION(crypto, 1);
1853 MODULE_DEPEND(crypto, zlib, 1, 1, 1);