1 /* $FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.7 2003/06/03 00:09:02 sam Exp $ */
2 /* $DragonFly: src/sys/opencrypto/crypto.c,v 1.12 2006/09/05 03:48:13 dillon Exp $ */
3 /* $OpenBSD: crypto.c,v 1.38 2002/06/11 11:14:29 beck Exp $ */
5 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
7 * This code was written by Angelos D. Keromytis in Athens, Greece, in
8 * February 2000. Network Security Technologies Inc. (NSTI) kindly
9 * supported the development of this code.
11 * Copyright (c) 2000, 2001 Angelos D. Keromytis
13 * Permission to use, copy, and modify this software with or without fee
14 * is hereby granted, provided that this entire notice is included in
15 * all source code copies of any software which is or includes a copy or
16 * modification of this software.
18 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
20 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
21 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
25 #define CRYPTO_TIMING /* enable cryptop timing stuff */
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/eventhandler.h>
30 #include <sys/kernel.h>
31 #include <sys/kthread.h>
32 #include <sys/malloc.h>
34 #include <sys/sysctl.h>
35 #include <sys/interrupt.h>
36 #include <sys/thread2.h>
37 #include <machine/ipl.h>
39 #include <vm/vm_zone.h>
40 #include <opencrypto/cryptodev.h>
41 #include <opencrypto/xform.h> /* XXX for M_XDATA */
43 #define SESID2HID(sid) (((sid) >> 32) & 0xffffffff)
46 * Crypto drivers register themselves by allocating a slot in the
47 * crypto_drivers table with crypto_get_driverid() and then registering
48 * each algorithm they support with crypto_register() and crypto_kregister().
50 static struct cryptocap *crypto_drivers = NULL;
51 static int crypto_drivers_num = 0;
54 * There are two queues for crypto requests; one for symmetric (e.g.
55 * cipher) operations and one for asymmetric (e.g. MOD) operations.
56 * See below for how synchronization is handled.
58 static TAILQ_HEAD(,cryptop) crp_q; /* request queues */
59 static TAILQ_HEAD(,cryptkop) crp_kq;
62 * There are two queues for processing completed crypto requests; one
63 * for the symmetric and one for the asymmetric ops. We only need one
64 * but have two to avoid type futzing (cryptop vs. cryptkop). See below
65 * for how synchronization is handled.
67 static TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queues */
68 static TAILQ_HEAD(,cryptkop) crp_ret_kq;
71 * Crypto op and desciptor data structures are allocated
72 * from separate private zones.
74 static vm_zone_t cryptop_zone;
75 static vm_zone_t cryptodesc_zone;
77 int crypto_usercrypto = 1; /* userland may open /dev/crypto */
78 SYSCTL_INT(_kern, OID_AUTO, usercrypto, CTLFLAG_RW,
79 &crypto_usercrypto, 0,
80 "Enable/disable user-mode access to crypto support");
81 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
82 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
83 &crypto_userasymcrypto, 0,
84 "Enable/disable user-mode access to asymmetric crypto support");
85 int crypto_devallowsoft = 0; /* only use hardware crypto for asym */
86 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
87 &crypto_devallowsoft, 0,
88 "Enable/disable use of software asym crypto support");
90 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
93 * Synchronization: read carefully, this is non-trivial.
95 * Crypto requests are submitted via crypto_dispatch. No critical
96 * section or lock/interlock guarentees are made on entry.
98 * Requests are typically passed on the driver directly, but they
99 * may also be queued for processing by a software interrupt thread,
100 * cryptointr, that runs in a critical section. This thread dispatches
101 * the requests to crypto drivers (h/w or s/w) who call crypto_done
102 * when a request is complete. Hardware crypto drivers are assumed
103 * to register their IRQ's as network devices so their interrupt handlers
104 * and subsequent "done callbacks" happen at appropriate protection levels.
106 * Completed crypto ops are queued for a separate kernel thread that
107 * handles the callbacks with no critical section or lock/interlock
108 * guarentees. This decoupling insures the crypto driver interrupt service
109 * routine is not delayed while the callback takes place and that callbacks
110 * are delivered after a context switch (as opposed to a software interrupt
111 * that clients must block).
113 * This scheme is not intended for SMP machines.
115 static inthand2_t cryptointr;
116 static void cryptoret(void); /* kernel thread for callbacks*/
117 static struct thread *cryptothread;
118 static void crypto_destroy(void);
119 static int crypto_invoke(struct cryptop *crp, int hint);
120 static int crypto_kinvoke(struct cryptkop *krp, int hint);
122 static struct cryptostats cryptostats;
123 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
124 cryptostats, "Crypto system statistics");
127 static int crypto_timing = 0;
128 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
129 &crypto_timing, 0, "Enable/disable crypto timing support");
132 static void *crypto_int_id;
139 cryptop_zone = zinit("cryptop", sizeof (struct cryptop), 0, 0, 1);
140 cryptodesc_zone = zinit("cryptodesc", sizeof (struct cryptodesc),
142 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
143 printf("crypto_init: cannot setup crypto zones\n");
147 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
148 crypto_drivers = kmalloc(crypto_drivers_num *
149 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
150 if (crypto_drivers == NULL) {
151 printf("crypto_init: cannot malloc driver table\n");
158 TAILQ_INIT(&crp_ret_q);
159 TAILQ_INIT(&crp_ret_kq);
161 crypto_int_id = register_swi(SWI_CRYPTO, cryptointr, NULL,
163 error = kthread_create((void (*)(void *)) cryptoret, NULL,
164 &cryptothread, "cryptoret");
166 printf("crypto_init: cannot start cryptoret thread; error %d",
176 /* XXX no wait to reclaim zones */
177 if (crypto_drivers != NULL)
178 kfree(crypto_drivers, M_CRYPTO_DATA);
179 unregister_swi(crypto_int_id);
183 * Initialization code, both for static and dynamic loading.
186 crypto_modevent(module_t mod, int type, void *unused)
192 error = crypto_init();
193 if (error == 0 && bootverbose)
194 printf("crypto: <crypto core>\n");
197 /*XXX disallow if active sessions */
205 static moduledata_t crypto_mod = {
210 MODULE_VERSION(crypto, 1);
211 DECLARE_MODULE(crypto, crypto_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
214 * Create a new session.
217 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
219 struct cryptoini *cr;
225 if (crypto_drivers == NULL)
229 * The algorithm we use here is pretty stupid; just use the
230 * first driver that supports all the algorithms we need.
232 * XXX We need more smarts here (in real life too, but that's
233 * XXX another story altogether).
236 for (hid = 0; hid < crypto_drivers_num; hid++) {
238 * If it's not initialized or has remaining sessions
239 * referencing it, skip.
241 if (crypto_drivers[hid].cc_newsession == NULL ||
242 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP))
245 /* Hardware required -- ignore software drivers. */
247 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE))
249 /* Software required -- ignore hardware drivers. */
251 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
254 /* See if all the algorithms are supported. */
255 for (cr = cri; cr; cr = cr->cri_next)
256 if (crypto_drivers[hid].cc_alg[cr->cri_alg] == 0)
260 /* Ok, all algorithms are supported. */
263 * Can't do everything in one session.
265 * XXX Fix this. We need to inject a "virtual" session layer right
269 /* Call the driver initialization routine. */
270 lid = hid; /* Pass the driver ID. */
271 err = crypto_drivers[hid].cc_newsession(
272 crypto_drivers[hid].cc_arg, &lid, cri);
276 (*sid) |= (lid & 0xffffffff);
277 crypto_drivers[hid].cc_sessions++;
288 * Delete an existing session (or a reserved session on an unregistered
292 crypto_freesession(u_int64_t sid)
299 if (crypto_drivers == NULL) {
304 /* Determine two IDs. */
305 hid = SESID2HID(sid);
307 if (hid >= crypto_drivers_num) {
312 if (crypto_drivers[hid].cc_sessions)
313 crypto_drivers[hid].cc_sessions--;
315 /* Call the driver cleanup routine, if available. */
316 if (crypto_drivers[hid].cc_freesession)
317 err = crypto_drivers[hid].cc_freesession(
318 crypto_drivers[hid].cc_arg, sid);
323 * If this was the last session of a driver marked as invalid,
324 * make the entry available for reuse.
326 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP) &&
327 crypto_drivers[hid].cc_sessions == 0)
328 bzero(&crypto_drivers[hid], sizeof(struct cryptocap));
336 * Return an unused driver id. Used by drivers prior to registering
337 * support for the algorithms they handle.
340 crypto_get_driverid(u_int32_t flags)
342 struct cryptocap *newdrv;
346 for (i = 0; i < crypto_drivers_num; i++)
347 if (crypto_drivers[i].cc_process == NULL &&
348 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0 &&
349 crypto_drivers[i].cc_sessions == 0)
352 /* Out of entries, allocate some more. */
353 if (i == crypto_drivers_num) {
354 /* Be careful about wrap-around. */
355 if (2 * crypto_drivers_num <= crypto_drivers_num) {
357 printf("crypto: driver count wraparound!\n");
361 newdrv = kmalloc(2 * crypto_drivers_num *
362 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
363 if (newdrv == NULL) {
365 printf("crypto: no space to expand driver table!\n");
369 bcopy(crypto_drivers, newdrv,
370 crypto_drivers_num * sizeof(struct cryptocap));
372 crypto_drivers_num *= 2;
374 kfree(crypto_drivers, M_CRYPTO_DATA);
375 crypto_drivers = newdrv;
378 /* NB: state is zero'd on free */
379 crypto_drivers[i].cc_sessions = 1; /* Mark */
380 crypto_drivers[i].cc_flags = flags;
382 printf("crypto: assign driver %u, flags %u\n", i, flags);
389 static struct cryptocap *
390 crypto_checkdriver(u_int32_t hid)
392 if (crypto_drivers == NULL)
394 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
398 * Register support for a key-related algorithm. This routine
399 * is called once for each algorithm supported a driver.
402 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags,
403 int (*kprocess)(void*, struct cryptkop *, int),
406 struct cryptocap *cap;
411 cap = crypto_checkdriver(driverid);
413 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
415 * XXX Do some performance testing to determine placing.
416 * XXX We probably need an auxiliary data structure that
417 * XXX describes relative performances.
420 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
422 printf("crypto: driver %u registers key alg %u flags %u\n"
428 if (cap->cc_kprocess == NULL) {
430 cap->cc_kprocess = kprocess;
441 * Register support for a non-key-related algorithm. This routine
442 * is called once for each such algorithm supported by a driver.
445 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
447 int (*newses)(void*, u_int32_t*, struct cryptoini*),
448 int (*freeses)(void*, u_int64_t),
449 int (*process)(void*, struct cryptop *, int),
452 struct cryptocap *cap;
457 cap = crypto_checkdriver(driverid);
458 /* NB: algorithms are in the range [1..max] */
460 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
462 * XXX Do some performance testing to determine placing.
463 * XXX We probably need an auxiliary data structure that
464 * XXX describes relative performances.
467 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
468 cap->cc_max_op_len[alg] = maxoplen;
470 printf("crypto: driver %u registers alg %u flags %u maxoplen %u\n"
477 if (cap->cc_process == NULL) {
479 cap->cc_newsession = newses;
480 cap->cc_process = process;
481 cap->cc_freesession = freeses;
482 cap->cc_sessions = 0; /* Unmark */
493 * Unregister a crypto driver. If there are pending sessions using it,
494 * leave enough information around so that subsequent calls using those
495 * sessions will correctly detect the driver has been unregistered and
499 crypto_unregister(u_int32_t driverid, int alg)
503 struct cryptocap *cap;
507 cap = crypto_checkdriver(driverid);
509 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
510 cap->cc_alg[alg] != 0) {
511 cap->cc_alg[alg] = 0;
512 cap->cc_max_op_len[alg] = 0;
514 /* Was this the last algorithm ? */
515 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
516 if (cap->cc_alg[i] != 0)
519 if (i == CRYPTO_ALGORITHM_MAX + 1) {
520 ses = cap->cc_sessions;
521 bzero(cap, sizeof(struct cryptocap));
524 * If there are pending sessions, just mark as invalid.
526 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
527 cap->cc_sessions = ses;
539 * Unregister all algorithms associated with a crypto driver.
540 * If there are pending sessions using it, leave enough information
541 * around so that subsequent calls using those sessions will
542 * correctly detect the driver has been unregistered and reroute
546 crypto_unregister_all(u_int32_t driverid)
550 struct cryptocap *cap;
553 cap = crypto_checkdriver(driverid);
555 for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
557 cap->cc_max_op_len[i] = 0;
559 ses = cap->cc_sessions;
560 bzero(cap, sizeof(struct cryptocap));
563 * If there are pending sessions, just mark as invalid.
565 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
566 cap->cc_sessions = ses;
577 * Clear blockage on a driver. The what parameter indicates whether
578 * the driver is now ready for cryptop's and/or cryptokop's.
581 crypto_unblock(u_int32_t driverid, int what)
583 struct cryptocap *cap;
587 cap = crypto_checkdriver(driverid);
590 if (what & CRYPTO_SYMQ) {
591 needwakeup |= cap->cc_qblocked;
592 cap->cc_qblocked = 0;
594 if (what & CRYPTO_ASYMQ) {
595 needwakeup |= cap->cc_kqblocked;
596 cap->cc_kqblocked = 0;
609 * Dispatch a crypto request to a driver or queue
610 * it, to be processed by the kernel thread.
613 crypto_dispatch(struct cryptop *crp)
615 u_int32_t hid = SESID2HID(crp->crp_sid);
618 cryptostats.cs_ops++;
622 nanouptime(&crp->crp_tstamp);
625 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
626 struct cryptocap *cap;
628 * Caller marked the request to be processed
629 * immediately; dispatch it directly to the
630 * driver unless the driver is currently blocked.
632 cap = crypto_checkdriver(hid);
633 if (cap && !cap->cc_qblocked) {
634 result = crypto_invoke(crp, 0);
635 if (result == ERESTART) {
637 * The driver ran out of resources, mark the
638 * driver ``blocked'' for cryptop's and put
639 * the op on the queue.
641 crypto_drivers[hid].cc_qblocked = 1;
642 TAILQ_INSERT_HEAD(&crp_q, crp, crp_next);
643 cryptostats.cs_blocks++;
648 * The driver is blocked, just queue the op until
649 * it unblocks and the swi thread gets kicked.
651 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
655 int wasempty = TAILQ_EMPTY(&crp_q);
657 * Caller marked the request as ``ok to delay'';
658 * queue it for the swi thread. This is desirable
659 * when the operation is low priority and/or suitable
662 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
673 * Add an asymetric crypto request to a queue,
674 * to be processed by the kernel thread.
677 crypto_kdispatch(struct cryptkop *krp)
679 struct cryptocap *cap;
682 cryptostats.cs_kops++;
685 cap = crypto_checkdriver(krp->krp_hid);
686 if (cap && !cap->cc_kqblocked) {
687 result = crypto_kinvoke(krp, 0);
688 if (result == ERESTART) {
690 * The driver ran out of resources, mark the
691 * driver ``blocked'' for cryptop's and put
692 * the op on the queue.
694 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
695 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
696 cryptostats.cs_kblocks++;
700 * The driver is blocked, just queue the op until
701 * it unblocks and the swi thread gets kicked.
703 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
712 * Dispatch an assymetric crypto request to the appropriate crypto devices.
715 crypto_kinvoke(struct cryptkop *krp, int hint)
723 if (krp->krp_callback == NULL) {
724 kfree(krp, M_XDATA); /* XXX allocated in cryptodev */
728 for (hid = 0; hid < crypto_drivers_num; hid++) {
729 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
730 !crypto_devallowsoft)
732 if (crypto_drivers[hid].cc_kprocess == NULL)
734 if ((crypto_drivers[hid].cc_kalg[krp->krp_op] &
735 CRYPTO_ALG_FLAG_SUPPORTED) == 0)
739 if (hid < crypto_drivers_num) {
741 error = crypto_drivers[hid].cc_kprocess(
742 crypto_drivers[hid].cc_karg, krp, hint);
747 krp->krp_status = error;
755 crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
757 struct timespec now, t;
760 t.tv_sec = now.tv_sec - tv->tv_sec;
761 t.tv_nsec = now.tv_nsec - tv->tv_nsec;
764 t.tv_nsec += 1000000000;
766 timespecadd(&ts->acc, &t);
767 if (timespeccmp(&t, &ts->min, <))
769 if (timespeccmp(&t, &ts->max, >))
778 * Dispatch a crypto request to the appropriate crypto devices.
781 crypto_invoke(struct cryptop *crp, int hint)
784 int (*process)(void*, struct cryptop *, int);
788 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
793 if (crp->crp_callback == NULL) {
797 if (crp->crp_desc == NULL) {
798 crp->crp_etype = EINVAL;
803 hid = SESID2HID(crp->crp_sid);
804 if (hid < crypto_drivers_num) {
805 if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP)
806 crypto_freesession(crp->crp_sid);
807 process = crypto_drivers[hid].cc_process;
812 if (process == NULL) {
813 struct cryptodesc *crd;
817 * Driver has unregistered; migrate the session and return
818 * an error to the caller so they'll resubmit the op.
820 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
821 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
823 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 0) == 0)
826 crp->crp_etype = EAGAIN;
831 * Invoke the driver to process the request.
833 return (*process)(crypto_drivers[hid].cc_arg, crp, hint);
838 * Release a set of crypto descriptors.
841 crypto_freereq(struct cryptop *crp)
843 struct cryptodesc *crd;
846 while ((crd = crp->crp_desc) != NULL) {
847 crp->crp_desc = crd->crd_next;
848 zfree(cryptodesc_zone, crd);
850 zfree(cryptop_zone, crp);
855 * Acquire a set of crypto descriptors. The descriptors are self contained
856 * so no special lock/interlock protection is necessary.
859 crypto_getreq(int num)
861 struct cryptodesc *crd;
864 crp = zalloc(cryptop_zone);
866 bzero(crp, sizeof (*crp));
868 crd = zalloc(cryptodesc_zone);
874 bzero(crd, sizeof (*crd));
875 crd->crd_next = crp->crp_desc;
883 * Invoke the callback on behalf of the driver.
886 crypto_done(struct cryptop *crp)
888 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
889 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
890 crp->crp_flags |= CRYPTO_F_DONE;
891 if (crp->crp_etype != 0)
892 cryptostats.cs_errs++;
895 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
897 if (crp->crp_flags & CRYPTO_F_CBIMM) {
899 * Do the callback directly. This is ok when the
900 * callback routine does very little (e.g. the
901 * /dev/crypto callback method just does a wakeup).
906 * NB: We must copy the timestamp before
907 * doing the callback as the cryptop is
908 * likely to be reclaimed.
910 struct timespec t = crp->crp_tstamp;
911 crypto_tstat(&cryptostats.cs_cb, &t);
912 crp->crp_callback(crp);
913 crypto_tstat(&cryptostats.cs_finis, &t);
916 crp->crp_callback(crp);
920 * Normal case; queue the callback for the thread.
922 * The return queue is manipulated by the swi thread
923 * and, potentially, by crypto device drivers calling
924 * back to mark operations completed. Thus we need
925 * to mask both while manipulating the return queue.
928 wasempty = TAILQ_EMPTY(&crp_ret_q);
929 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
931 wakeup_one(&crp_ret_q);
937 * Invoke the callback on behalf of the driver.
940 crypto_kdone(struct cryptkop *krp)
944 if (krp->krp_status != 0)
945 cryptostats.cs_kerrs++;
947 * The return queue is manipulated by the swi thread
948 * and, potentially, by crypto device drivers calling
949 * back to mark operations completed. Thus we need
950 * to mask both while manipulating the return queue.
953 wasempty = TAILQ_EMPTY(&crp_ret_kq);
954 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
956 wakeup_one(&crp_ret_q);
961 crypto_getfeat(int *featp)
963 int hid, kalg, feat = 0;
966 if (!crypto_userasymcrypto)
969 for (hid = 0; hid < crypto_drivers_num; hid++) {
970 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
971 !crypto_devallowsoft) {
974 if (crypto_drivers[hid].cc_kprocess == NULL)
976 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
977 if ((crypto_drivers[hid].cc_kalg[kalg] &
978 CRYPTO_ALG_FLAG_SUPPORTED) != 0)
988 * Software interrupt thread to dispatch crypto requests.
991 cryptointr(void *dummy, void *frame)
993 struct cryptop *crp, *submit;
994 struct cryptkop *krp;
995 struct cryptocap *cap;
998 cryptostats.cs_intrs++;
1002 * Find the first element in the queue that can be
1003 * processed and look-ahead to see if multiple ops
1004 * are ready for the same driver.
1008 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1009 u_int32_t hid = SESID2HID(crp->crp_sid);
1010 cap = crypto_checkdriver(hid);
1011 if (cap == NULL || cap->cc_process == NULL) {
1012 /* Op needs to be migrated, process it. */
1017 if (!cap->cc_qblocked) {
1018 if (submit != NULL) {
1020 * We stop on finding another op,
1021 * regardless whether its for the same
1022 * driver or not. We could keep
1023 * searching the queue but it might be
1024 * better to just use a per-driver
1027 if (SESID2HID(submit->crp_sid) == hid)
1028 hint = CRYPTO_HINT_MORE;
1032 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1034 /* keep scanning for more are q'd */
1038 if (submit != NULL) {
1039 TAILQ_REMOVE(&crp_q, submit, crp_next);
1040 result = crypto_invoke(submit, hint);
1041 if (result == ERESTART) {
1043 * The driver ran out of resources, mark the
1044 * driver ``blocked'' for cryptop's and put
1045 * the request back in the queue. It would
1046 * best to put the request back where we got
1047 * it but that's hard so for now we put it
1048 * at the front. This should be ok; putting
1049 * it at the end does not work.
1051 /* XXX validate sid again? */
1052 crypto_drivers[SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1053 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1054 cryptostats.cs_blocks++;
1058 /* As above, but for key ops */
1059 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1060 cap = crypto_checkdriver(krp->krp_hid);
1061 if (cap == NULL || cap->cc_kprocess == NULL) {
1062 /* Op needs to be migrated, process it. */
1065 if (!cap->cc_kqblocked)
1069 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1070 result = crypto_kinvoke(krp, 0);
1071 if (result == ERESTART) {
1073 * The driver ran out of resources, mark the
1074 * driver ``blocked'' for cryptkop's and put
1075 * the request back in the queue. It would
1076 * best to put the request back where we got
1077 * it but that's hard so for now we put it
1078 * at the front. This should be ok; putting
1079 * it at the end does not work.
1081 /* XXX validate sid again? */
1082 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1083 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1084 cryptostats.cs_kblocks++;
1087 } while (submit != NULL || krp != NULL);
1092 * Kernel thread to do callbacks.
1097 struct cryptop *crp;
1098 struct cryptkop *krp;
1102 crp = TAILQ_FIRST(&crp_ret_q);
1104 TAILQ_REMOVE(&crp_ret_q, crp, crp_next);
1105 krp = TAILQ_FIRST(&crp_ret_kq);
1107 TAILQ_REMOVE(&crp_ret_kq, krp, krp_next);
1109 if (crp != NULL || krp != NULL) {
1110 crit_exit(); /* lower ipl for callbacks */
1112 #ifdef CRYPTO_TIMING
1113 if (crypto_timing) {
1115 * NB: We must copy the timestamp before
1116 * doing the callback as the cryptop is
1117 * likely to be reclaimed.
1119 struct timespec t = crp->crp_tstamp;
1120 crypto_tstat(&cryptostats.cs_cb, &t);
1121 crp->crp_callback(crp);
1122 crypto_tstat(&cryptostats.cs_finis, &t);
1125 crp->crp_callback(crp);
1128 krp->krp_callback(krp);
1131 (void) tsleep(&crp_ret_q, 0, "crypto_wait", 0);
1132 cryptostats.cs_rets++;
1135 /* CODE NOT REACHED (crit_exit() would go here otherwise) */