1 /* $OpenBSD: crypto.c,v 1.38 2002/06/11 11:14:29 beck Exp $ */
3 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
5 * This code was written by Angelos D. Keromytis in Athens, Greece, in
6 * February 2000. Network Security Technologies Inc. (NSTI) kindly
7 * supported the development of this code.
9 * Copyright (c) 2000, 2001 Angelos D. Keromytis
11 * Permission to use, copy, and modify this software with or without fee
12 * is hereby granted, provided that this entire notice is included in
13 * all source code copies of any software which is or includes a copy or
14 * modification of this software.
16 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
18 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
19 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
26 #define CRYPTO_TIMING /* enable timing support */
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/eventhandler.h>
31 #include <sys/kernel.h>
32 #include <sys/kthread.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/malloc.h>
38 #include <sys/sysctl.h>
41 #include <opencrypto/cryptodev.h>
42 #include <opencrypto/xform.h> /* XXX for M_XDATA */
45 * Crypto drivers register themselves by allocating a slot in the
46 * crypto_drivers table with crypto_get_driverid() and then registering
47 * each algorithm they support with crypto_register() and crypto_kregister().
49 static struct mtx crypto_drivers_mtx; /* lock on driver table */
50 #define CRYPTO_DRIVER_LOCK() mtx_lock(&crypto_drivers_mtx)
51 #define CRYPTO_DRIVER_UNLOCK() mtx_unlock(&crypto_drivers_mtx)
52 static struct cryptocap *crypto_drivers = NULL;
53 static int crypto_drivers_num = 0;
56 * There are two queues for crypto requests; one for symmetric (e.g.
57 * cipher) operations and one for asymmetric (e.g. MOD)operations.
58 * A single mutex is used to lock access to both queues. We could
59 * have one per-queue but having one simplifies handling of block/unblock
62 static int crp_sleep = 0;
63 static TAILQ_HEAD(,cryptop) crp_q; /* request queues */
64 static TAILQ_HEAD(,cryptkop) crp_kq;
65 static struct mtx crypto_q_mtx;
66 #define CRYPTO_Q_LOCK() mtx_lock(&crypto_q_mtx)
67 #define CRYPTO_Q_UNLOCK() mtx_unlock(&crypto_q_mtx)
70 * There are two queues for processing completed crypto requests; one
71 * for the symmetric and one for the asymmetric ops. We only need one
72 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
73 * mutex is used to lock access to both queues. Note that this lock
74 * must be separate from the lock on request queues to insure driver
75 * callbacks don't generate lock order reversals.
77 static TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queues */
78 static TAILQ_HEAD(,cryptkop) crp_ret_kq;
79 static struct mtx crypto_ret_q_mtx;
80 #define CRYPTO_RETQ_LOCK() mtx_lock(&crypto_ret_q_mtx)
81 #define CRYPTO_RETQ_UNLOCK() mtx_unlock(&crypto_ret_q_mtx)
82 #define CRYPTO_RETQ_EMPTY() (TAILQ_EMPTY(&crp_ret_q) && TAILQ_EMPTY(&crp_ret_kq))
84 static uma_zone_t cryptop_zone;
85 static uma_zone_t cryptodesc_zone;
87 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
88 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
89 &crypto_userasymcrypto, 0,
90 "Enable/disable user-mode access to asymmetric crypto support");
91 int crypto_devallowsoft = 0; /* only use hardware crypto for asym */
92 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
93 &crypto_devallowsoft, 0,
94 "Enable/disable use of software asym crypto support");
96 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
98 static void crypto_proc(void);
99 static struct proc *cryptoproc;
100 static void crypto_ret_proc(void);
101 static struct proc *cryptoretproc;
102 static void crypto_destroy(void);
103 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
104 static int crypto_kinvoke(struct cryptkop *krp);
106 static struct cryptostats cryptostats;
107 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
108 cryptostats, "Crypto system statistics");
111 static int crypto_timing = 0;
112 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
113 &crypto_timing, 0, "Enable/disable crypto timing support");
121 mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table",
126 mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF);
128 TAILQ_INIT(&crp_ret_q);
129 TAILQ_INIT(&crp_ret_kq);
130 mtx_init(&crypto_ret_q_mtx, "crypto", "crypto return queues", MTX_DEF);
132 cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop),
134 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
135 cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc),
137 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
138 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
139 printf("crypto_init: cannot setup crypto zones\n");
144 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
145 crypto_drivers = malloc(crypto_drivers_num *
146 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
147 if (crypto_drivers == NULL) {
148 printf("crypto_init: cannot setup crypto drivers\n");
153 error = kthread_create((void (*)(void *)) crypto_proc, NULL,
154 &cryptoproc, 0, 0, "crypto");
156 printf("crypto_init: cannot start crypto thread; error %d",
161 error = kthread_create((void (*)(void *)) crypto_ret_proc, NULL,
162 &cryptoretproc, 0, 0, "crypto returns");
164 printf("crypto_init: cannot start cryptoret thread; error %d",
175 * Signal a crypto thread to terminate. We use the driver
176 * table lock to synchronize the sleep/wakeups so that we
177 * are sure the threads have terminated before we release
178 * the data structures they use. See crypto_finis below
179 * for the other half of this song-and-dance.
182 crypto_terminate(struct proc **pp, void *q)
186 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
191 PROC_LOCK(p); /* NB: insure we don't miss wakeup */
192 CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */
193 msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0);
195 CRYPTO_DRIVER_LOCK();
203 * Terminate any crypto threads.
205 CRYPTO_DRIVER_LOCK();
206 crypto_terminate(&cryptoproc, &crp_q);
207 crypto_terminate(&cryptoretproc, &crp_ret_q);
208 CRYPTO_DRIVER_UNLOCK();
210 /* XXX flush queues??? */
213 * Reclaim dynamically allocated resources.
215 if (crypto_drivers != NULL)
216 free(crypto_drivers, M_CRYPTO_DATA);
218 if (cryptodesc_zone != NULL)
219 uma_zdestroy(cryptodesc_zone);
220 if (cryptop_zone != NULL)
221 uma_zdestroy(cryptop_zone);
222 mtx_destroy(&crypto_q_mtx);
223 mtx_destroy(&crypto_ret_q_mtx);
224 mtx_destroy(&crypto_drivers_mtx);
228 * Initialization code, both for static and dynamic loading.
231 crypto_modevent(module_t mod, int type, void *unused)
237 error = crypto_init();
238 if (error == 0 && bootverbose)
239 printf("crypto: <crypto core>\n");
242 /*XXX disallow if active sessions */
250 static moduledata_t crypto_mod = {
255 MODULE_VERSION(crypto, 1);
256 DECLARE_MODULE(crypto, crypto_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
257 MODULE_DEPEND(crypto, zlib, 1, 1, 1);
260 * Create a new session.
263 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
265 struct cryptocap *cap = NULL;
266 struct cryptoini *cr;
267 u_int32_t hid = 0, lid;
270 CRYPTO_DRIVER_LOCK();
272 if (crypto_drivers == NULL)
276 * The algorithm we use here is pretty stupid; just use the
277 * first driver that supports all the algorithms we need.
279 * XXX We need more smarts here (in real life too, but that's
280 * XXX another story altogether).
284 * First try to find hardware crypto.
287 for (hid = 0; hid < crypto_drivers_num; hid++) {
288 cap = &crypto_drivers[hid];
290 * If it's not initialized or has remaining sessions
291 * referencing it, skip.
293 if (cap->cc_newsession == NULL ||
294 (cap->cc_flags & CRYPTOCAP_F_CLEANUP))
297 /* Hardware required -- ignore software drivers. */
298 if (cap->cc_flags & CRYPTOCAP_F_SOFTWARE)
301 /* See if all the algorithms are supported. */
302 for (cr = cri; cr; cr = cr->cri_next)
303 if (cap->cc_alg[cr->cri_alg] == 0)
306 /* Ok, all algorithms are supported. */
310 if (hid == crypto_drivers_num)
314 * If no hardware crypto, look for software crypto.
316 if (cap == NULL && hard <= 0) {
317 for (hid = 0; hid < crypto_drivers_num; hid++) {
318 cap = &crypto_drivers[hid];
320 * If it's not initialized or has remaining sessions
321 * referencing it, skip.
323 if (cap->cc_newsession == NULL ||
324 (cap->cc_flags & CRYPTOCAP_F_CLEANUP))
327 /* Software required -- ignore hardware drivers. */
328 if (!(cap->cc_flags & CRYPTOCAP_F_SOFTWARE))
331 /* See if all the algorithms are supported. */
332 for (cr = cri; cr; cr = cr->cri_next)
333 if (cap->cc_alg[cr->cri_alg] == 0)
336 /* Ok, all algorithms are supported. */
340 if (hid == crypto_drivers_num)
346 * Can't do everything in one session.
348 * XXX Fix this. We need to inject a "virtual" session layer right
352 /* Call the driver initialization routine. */
353 lid = hid; /* Pass the driver ID. */
354 err = (*cap->cc_newsession)(cap->cc_arg, &lid, cri);
356 /* XXX assert (hid &~ 0xffffff) == 0 */
357 /* XXX assert (cap->cc_flags &~ 0xff) == 0 */
358 (*sid) = ((cap->cc_flags & 0xff) << 24) | hid;
360 (*sid) |= (lid & 0xffffffff);
365 CRYPTO_DRIVER_UNLOCK();
370 crypto_remove(struct cryptocap *cap)
373 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
374 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
375 bzero(cap, sizeof(*cap));
379 * Delete an existing session (or a reserved session on an unregistered
383 crypto_freesession(u_int64_t sid)
385 struct cryptocap *cap;
389 CRYPTO_DRIVER_LOCK();
391 if (crypto_drivers == NULL) {
396 /* Determine two IDs. */
397 hid = CRYPTO_SESID2HID(sid);
399 if (hid >= crypto_drivers_num) {
403 cap = &crypto_drivers[hid];
405 if (cap->cc_sessions)
408 /* Call the driver cleanup routine, if available. */
409 if (cap->cc_freesession)
410 err = cap->cc_freesession(cap->cc_arg, sid);
414 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
418 CRYPTO_DRIVER_UNLOCK();
423 * Return an unused driver id. Used by drivers prior to registering
424 * support for the algorithms they handle.
427 crypto_get_driverid(u_int32_t flags)
429 struct cryptocap *newdrv;
432 CRYPTO_DRIVER_LOCK();
434 for (i = 0; i < crypto_drivers_num; i++) {
435 if (crypto_drivers[i].cc_process == NULL &&
436 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
441 /* Out of entries, allocate some more. */
442 if (i == crypto_drivers_num) {
443 /* Be careful about wrap-around. */
444 if (2 * crypto_drivers_num <= crypto_drivers_num) {
445 CRYPTO_DRIVER_UNLOCK();
446 printf("crypto: driver count wraparound!\n");
450 newdrv = malloc(2 * crypto_drivers_num *
451 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
452 if (newdrv == NULL) {
453 CRYPTO_DRIVER_UNLOCK();
454 printf("crypto: no space to expand driver table!\n");
458 bcopy(crypto_drivers, newdrv,
459 crypto_drivers_num * sizeof(struct cryptocap));
461 crypto_drivers_num *= 2;
463 free(crypto_drivers, M_CRYPTO_DATA);
464 crypto_drivers = newdrv;
467 /* NB: state is zero'd on free */
468 crypto_drivers[i].cc_sessions = 1; /* Mark */
469 crypto_drivers[i].cc_flags = flags;
471 printf("crypto: assign driver %u, flags %u\n", i, flags);
473 CRYPTO_DRIVER_UNLOCK();
478 static struct cryptocap *
479 crypto_checkdriver(u_int32_t hid)
481 if (crypto_drivers == NULL)
483 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
487 * Register support for a key-related algorithm. This routine
488 * is called once for each algorithm supported a driver.
491 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags,
492 int (*kprocess)(void*, struct cryptkop *, int),
495 struct cryptocap *cap;
498 CRYPTO_DRIVER_LOCK();
500 cap = crypto_checkdriver(driverid);
502 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
504 * XXX Do some performance testing to determine placing.
505 * XXX We probably need an auxiliary data structure that
506 * XXX describes relative performances.
509 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
511 printf("crypto: driver %u registers key alg %u flags %u\n"
517 if (cap->cc_kprocess == NULL) {
519 cap->cc_kprocess = kprocess;
525 CRYPTO_DRIVER_UNLOCK();
530 * Register support for a non-key-related algorithm. This routine
531 * is called once for each such algorithm supported by a driver.
534 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
536 int (*newses)(void*, u_int32_t*, struct cryptoini*),
537 int (*freeses)(void*, u_int64_t),
538 int (*process)(void*, struct cryptop *, int),
541 struct cryptocap *cap;
544 CRYPTO_DRIVER_LOCK();
546 cap = crypto_checkdriver(driverid);
547 /* NB: algorithms are in the range [1..max] */
549 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
551 * XXX Do some performance testing to determine placing.
552 * XXX We probably need an auxiliary data structure that
553 * XXX describes relative performances.
556 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
557 cap->cc_max_op_len[alg] = maxoplen;
559 printf("crypto: driver %u registers alg %u flags %u maxoplen %u\n"
566 if (cap->cc_process == NULL) {
568 cap->cc_newsession = newses;
569 cap->cc_process = process;
570 cap->cc_freesession = freeses;
571 cap->cc_sessions = 0; /* Unmark */
577 CRYPTO_DRIVER_UNLOCK();
582 * Unregister a crypto driver. If there are pending sessions using it,
583 * leave enough information around so that subsequent calls using those
584 * sessions will correctly detect the driver has been unregistered and
588 crypto_unregister(u_int32_t driverid, int alg)
590 struct cryptocap *cap;
594 CRYPTO_DRIVER_LOCK();
596 cap = crypto_checkdriver(driverid);
598 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
599 cap->cc_alg[alg] != 0) {
600 cap->cc_alg[alg] = 0;
601 cap->cc_max_op_len[alg] = 0;
603 /* Was this the last algorithm ? */
604 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
605 if (cap->cc_alg[i] != 0)
608 if (i == CRYPTO_ALGORITHM_MAX + 1) {
609 ses = cap->cc_sessions;
610 kops = cap->cc_koperations;
611 bzero(cap, sizeof(*cap));
612 if (ses != 0 || kops != 0) {
614 * If there are pending sessions, just mark as invalid.
616 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
617 cap->cc_sessions = ses;
618 cap->cc_koperations = kops;
625 CRYPTO_DRIVER_UNLOCK();
630 * Unregister all algorithms associated with a crypto driver.
631 * If there are pending sessions using it, leave enough information
632 * around so that subsequent calls using those sessions will
633 * correctly detect the driver has been unregistered and reroute
637 crypto_unregister_all(u_int32_t driverid)
639 struct cryptocap *cap;
643 CRYPTO_DRIVER_LOCK();
645 cap = crypto_checkdriver(driverid);
647 for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
649 cap->cc_max_op_len[i] = 0;
651 ses = cap->cc_sessions;
652 kops = cap->cc_koperations;
653 bzero(cap, sizeof(*cap));
654 if (ses != 0 || kops != 0) {
656 * If there are pending sessions, just mark as invalid.
658 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
659 cap->cc_sessions = ses;
660 cap->cc_koperations = kops;
666 CRYPTO_DRIVER_UNLOCK();
671 * Clear blockage on a driver. The what parameter indicates whether
672 * the driver is now ready for cryptop's and/or cryptokop's.
675 crypto_unblock(u_int32_t driverid, int what)
677 struct cryptocap *cap;
681 cap = crypto_checkdriver(driverid);
683 if (what & CRYPTO_SYMQ)
684 cap->cc_qblocked = 0;
685 if (what & CRYPTO_ASYMQ)
686 cap->cc_kqblocked = 0;
698 * Add a crypto request to a queue, to be processed by the kernel thread.
701 crypto_dispatch(struct cryptop *crp)
703 struct cryptocap *cap;
707 cryptostats.cs_ops++;
711 binuptime(&crp->crp_tstamp);
714 hid = CRYPTO_SESID2HID(crp->crp_sid);
716 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
718 * Caller marked the request to be processed
719 * immediately; dispatch it directly to the
720 * driver unless the driver is currently blocked.
722 cap = crypto_checkdriver(hid);
723 /* Driver cannot disappeared when there is an active session. */
724 KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
725 if (!cap->cc_qblocked) {
726 result = crypto_invoke(cap, crp, 0);
727 if (result != ERESTART)
730 * The driver ran out of resources, put the request on
736 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
744 * Add an asymetric crypto request to a queue,
745 * to be processed by the kernel thread.
748 crypto_kdispatch(struct cryptkop *krp)
752 cryptostats.cs_kops++;
754 result = crypto_kinvoke(krp);
755 if (result != ERESTART)
758 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
767 * Dispatch an assymetric crypto request to the appropriate crypto devices.
770 crypto_kinvoke(struct cryptkop *krp)
772 struct cryptocap *cap = NULL;
776 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
777 KASSERT(krp->krp_callback != NULL,
778 ("%s: krp->crp_callback == NULL", __func__));
780 CRYPTO_DRIVER_LOCK();
781 for (hid = 0; hid < crypto_drivers_num; hid++) {
782 cap = &crypto_drivers[hid];
785 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
786 !crypto_devallowsoft) {
789 if (cap->cc_kprocess == NULL)
791 if (!(cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED))
793 if (cap->cc_kqblocked) {
801 if (hid < crypto_drivers_num) {
802 cap->cc_koperations++;
803 CRYPTO_DRIVER_UNLOCK();
804 error = cap->cc_kprocess(cap->cc_karg, krp, 0);
805 CRYPTO_DRIVER_LOCK();
806 if (error == ERESTART) {
807 cap->cc_koperations--;
808 CRYPTO_DRIVER_UNLOCK();
814 CRYPTO_DRIVER_UNLOCK();
817 krp->krp_status = error;
825 crypto_tstat(struct cryptotstat *ts, struct bintime *bt)
827 struct bintime now, delta;
833 delta.frac = now.frac - bt->frac;
834 delta.sec = now.sec - bt->sec;
837 bintime2timespec(&delta, &t);
838 timespecadd(&ts->acc, &t);
839 if (timespeccmp(&t, &ts->min, <))
841 if (timespeccmp(&t, &ts->max, >))
850 * Dispatch a crypto request to the appropriate crypto devices.
853 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
856 KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
857 KASSERT(crp->crp_callback != NULL,
858 ("%s: crp->crp_callback == NULL", __func__));
859 KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
863 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
865 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
866 struct cryptodesc *crd;
870 * Driver has unregistered; migrate the session and return
871 * an error to the caller so they'll resubmit the op.
873 * XXX: What if there are more already queued requests for this
876 crypto_freesession(crp->crp_sid);
878 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
879 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
881 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 0) == 0)
884 crp->crp_etype = EAGAIN;
889 * Invoke the driver to process the request.
891 return cap->cc_process(cap->cc_arg, crp, hint);
896 * Release a set of crypto descriptors.
899 crypto_freereq(struct cryptop *crp)
901 struct cryptodesc *crd;
908 struct cryptop *crp2;
911 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
913 ("Freeing cryptop from the crypto queue (%p).",
918 TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
920 ("Freeing cryptop from the return queue (%p).",
923 CRYPTO_RETQ_UNLOCK();
927 while ((crd = crp->crp_desc) != NULL) {
928 crp->crp_desc = crd->crd_next;
929 uma_zfree(cryptodesc_zone, crd);
932 uma_zfree(cryptop_zone, crp);
936 * Acquire a set of crypto descriptors.
939 crypto_getreq(int num)
941 struct cryptodesc *crd;
944 crp = uma_zalloc(cryptop_zone, M_NOWAIT|M_ZERO);
947 crd = uma_zalloc(cryptodesc_zone, M_NOWAIT|M_ZERO);
953 crd->crd_next = crp->crp_desc;
961 * Invoke the callback on behalf of the driver.
964 crypto_done(struct cryptop *crp)
966 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
967 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
968 crp->crp_flags |= CRYPTO_F_DONE;
969 if (crp->crp_etype != 0)
970 cryptostats.cs_errs++;
973 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
976 * CBIMM means unconditionally do the callback immediately;
977 * CBIFSYNC means do the callback immediately only if the
978 * operation was done synchronously. Both are used to avoid
979 * doing extraneous context switches; the latter is mostly
980 * used with the software crypto driver.
982 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
983 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
984 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
986 * Do the callback directly. This is ok when the
987 * callback routine does very little (e.g. the
988 * /dev/crypto callback method just does a wakeup).
993 * NB: We must copy the timestamp before
994 * doing the callback as the cryptop is
995 * likely to be reclaimed.
997 struct bintime t = crp->crp_tstamp;
998 crypto_tstat(&cryptostats.cs_cb, &t);
999 crp->crp_callback(crp);
1000 crypto_tstat(&cryptostats.cs_finis, &t);
1003 crp->crp_callback(crp);
1006 * Normal case; queue the callback for the thread.
1009 if (CRYPTO_RETQ_EMPTY())
1010 wakeup_one(&crp_ret_q); /* shared wait channel */
1011 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1012 CRYPTO_RETQ_UNLOCK();
1017 * Invoke the callback on behalf of the driver.
1020 crypto_kdone(struct cryptkop *krp)
1022 struct cryptocap *cap;
1024 if (krp->krp_status != 0)
1025 cryptostats.cs_kerrs++;
1026 CRYPTO_DRIVER_LOCK();
1027 /* XXX: What if driver is loaded in the meantime? */
1028 if (krp->krp_hid < crypto_drivers_num) {
1029 cap = &crypto_drivers[krp->krp_hid];
1030 cap->cc_koperations--;
1031 KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
1032 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1035 CRYPTO_DRIVER_UNLOCK();
1037 if (CRYPTO_RETQ_EMPTY())
1038 wakeup_one(&crp_ret_q); /* shared wait channel */
1039 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1040 CRYPTO_RETQ_UNLOCK();
1044 crypto_getfeat(int *featp)
1046 int hid, kalg, feat = 0;
1048 if (!crypto_userasymcrypto)
1051 CRYPTO_DRIVER_LOCK();
1052 for (hid = 0; hid < crypto_drivers_num; hid++) {
1053 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1054 !crypto_devallowsoft) {
1057 if (crypto_drivers[hid].cc_kprocess == NULL)
1059 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1060 if ((crypto_drivers[hid].cc_kalg[kalg] &
1061 CRYPTO_ALG_FLAG_SUPPORTED) != 0)
1064 CRYPTO_DRIVER_UNLOCK();
1071 * Terminate a thread at module unload. The process that
1072 * initiated this is waiting for us to signal that we're gone;
1073 * wake it up and exit. We use the driver table lock to insure
1074 * we don't do the wakeup before they're waiting. There is no
1075 * race here because the waiter sleeps on the proc lock for the
1076 * thread so it gets notified at the right time because of an
1077 * extra wakeup that's done in exit1().
1080 crypto_finis(void *chan)
1082 CRYPTO_DRIVER_LOCK();
1084 CRYPTO_DRIVER_UNLOCK();
1089 * Crypto thread, dispatches crypto requests.
1094 struct cryptop *crp, *submit;
1095 struct cryptkop *krp;
1096 struct cryptocap *cap;
1103 * Find the first element in the queue that can be
1104 * processed and look-ahead to see if multiple ops
1105 * are ready for the same driver.
1109 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1110 hid = CRYPTO_SESID2HID(crp->crp_sid);
1111 cap = crypto_checkdriver(hid);
1113 * Driver cannot disappeared when there is an active
1116 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1117 __func__, __LINE__));
1118 if (cap == NULL || cap->cc_process == NULL) {
1119 /* Op needs to be migrated, process it. */
1124 if (!cap->cc_qblocked) {
1125 if (submit != NULL) {
1127 * We stop on finding another op,
1128 * regardless whether its for the same
1129 * driver or not. We could keep
1130 * searching the queue but it might be
1131 * better to just use a per-driver
1134 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1135 hint = CRYPTO_HINT_MORE;
1139 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1141 /* keep scanning for more are q'd */
1145 if (submit != NULL) {
1146 TAILQ_REMOVE(&crp_q, submit, crp_next);
1147 hid = CRYPTO_SESID2HID(submit->crp_sid);
1148 cap = crypto_checkdriver(hid);
1149 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1150 __func__, __LINE__));
1151 result = crypto_invoke(cap, submit, hint);
1152 if (result == ERESTART) {
1154 * The driver ran out of resources, mark the
1155 * driver ``blocked'' for cryptop's and put
1156 * the request back in the queue. It would
1157 * best to put the request back where we got
1158 * it but that's hard so for now we put it
1159 * at the front. This should be ok; putting
1160 * it at the end does not work.
1162 /* XXX validate sid again? */
1163 crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1164 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1165 cryptostats.cs_blocks++;
1169 /* As above, but for key ops */
1170 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1171 cap = crypto_checkdriver(krp->krp_hid);
1172 if (cap == NULL || cap->cc_kprocess == NULL) {
1173 /* Op needs to be migrated, process it. */
1176 if (!cap->cc_kqblocked)
1180 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1181 result = crypto_kinvoke(krp);
1182 if (result == ERESTART) {
1184 * The driver ran out of resources, mark the
1185 * driver ``blocked'' for cryptkop's and put
1186 * the request back in the queue. It would
1187 * best to put the request back where we got
1188 * it but that's hard so for now we put it
1189 * at the front. This should be ok; putting
1190 * it at the end does not work.
1192 /* XXX validate sid again? */
1193 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1194 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1195 cryptostats.cs_kblocks++;
1199 if (submit == NULL && krp == NULL) {
1201 * Nothing more to be processed. Sleep until we're
1202 * woken because there are more ops to process.
1203 * This happens either by submission or by a driver
1204 * becoming unblocked and notifying us through
1205 * crypto_unblock. Note that when we wakeup we
1206 * start processing each queue again from the
1207 * front. It's not clear that it's important to
1208 * preserve this ordering since ops may finish
1209 * out of order if dispatched to different devices
1210 * and some become blocked while others do not.
1213 msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0);
1215 if (cryptoproc == NULL)
1217 cryptostats.cs_intrs++;
1222 crypto_finis(&crp_q);
1226 * Crypto returns thread, does callbacks for processed crypto requests.
1227 * Callbacks are done here, rather than in the crypto drivers, because
1228 * callbacks typically are expensive and would slow interrupt handling.
1231 crypto_ret_proc(void)
1233 struct cryptop *crpt;
1234 struct cryptkop *krpt;
1238 /* Harvest return q's for completed ops */
1239 crpt = TAILQ_FIRST(&crp_ret_q);
1241 TAILQ_REMOVE(&crp_ret_q, crpt, crp_next);
1243 krpt = TAILQ_FIRST(&crp_ret_kq);
1245 TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next);
1247 if (crpt != NULL || krpt != NULL) {
1248 CRYPTO_RETQ_UNLOCK();
1250 * Run callbacks unlocked.
1253 #ifdef CRYPTO_TIMING
1254 if (crypto_timing) {
1256 * NB: We must copy the timestamp before
1257 * doing the callback as the cryptop is
1258 * likely to be reclaimed.
1260 struct bintime t = crpt->crp_tstamp;
1261 crypto_tstat(&cryptostats.cs_cb, &t);
1262 crpt->crp_callback(crpt);
1263 crypto_tstat(&cryptostats.cs_finis, &t);
1266 crpt->crp_callback(crpt);
1269 krpt->krp_callback(krpt);
1273 * Nothing more to be processed. Sleep until we're
1274 * woken because there are more returns to process.
1276 msleep(&crp_ret_q, &crypto_ret_q_mtx, PWAIT,
1277 "crypto_ret_wait", 0);
1278 if (cryptoretproc == NULL)
1280 cryptostats.cs_rets++;
1283 CRYPTO_RETQ_UNLOCK();
1285 crypto_finis(&crp_ret_q);