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.7 2005/02/01 22:41:27 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>
36 #include <sys/interrupt.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. Typically
96 * these come in from network protocols at spl0 (output path) or
97 * splnet (input path).
99 * Requests are typically passed on the driver directly, but they
100 * may also be queued for processing by a software interrupt thread,
101 * cryptointr, that runs at splsoftcrypto. This thread dispatches
102 * the requests to crypto drivers (h/w or s/w) who call crypto_done
103 * when a request is complete. Hardware crypto drivers are assumed
104 * to register their IRQ's as network devices so their interrupt handlers
105 * and subsequent "done callbacks" happen at splimp.
107 * Completed crypto ops are queued for a separate kernel thread that
108 * handles the callbacks at spl0. This decoupling insures the crypto
109 * driver interrupt service routine is not delayed while the callback
110 * takes place and that callbacks are delivered after a context switch
111 * (as opposed to a software interrupt that clients must block).
113 * This scheme is not intended for SMP machines.
115 static void cryptointr(void *dummy); /* swi thread to dispatch ops */
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");
137 cryptop_zone = zinit("cryptop", sizeof (struct cryptop), 0, 0, 1);
138 cryptodesc_zone = zinit("cryptodesc", sizeof (struct cryptodesc),
140 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
141 printf("crypto_init: cannot setup crypto zones\n");
145 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
146 crypto_drivers = malloc(crypto_drivers_num *
147 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
148 if (crypto_drivers == NULL) {
149 printf("crypto_init: cannot malloc driver table\n");
156 TAILQ_INIT(&crp_ret_q);
157 TAILQ_INIT(&crp_ret_kq);
159 register_swi(SWI_CRYPTO, cryptointr, NULL, "swi_crypto", NULL);
160 error = kthread_create((void (*)(void *)) cryptoret, NULL,
161 &cryptothread, "cryptoret");
163 printf("crypto_init: cannot start cryptoret thread; error %d",
173 /* XXX no wait to reclaim zones */
174 if (crypto_drivers != NULL)
175 free(crypto_drivers, M_CRYPTO_DATA);
176 unregister_swi(SWI_CRYPTO, cryptointr);
180 * Initialization code, both for static and dynamic loading.
183 crypto_modevent(module_t mod, int type, void *unused)
189 error = crypto_init();
190 if (error == 0 && bootverbose)
191 printf("crypto: <crypto core>\n");
194 /*XXX disallow if active sessions */
202 static moduledata_t crypto_mod = {
207 MODULE_VERSION(crypto, 1);
208 DECLARE_MODULE(crypto, crypto_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
211 * Create a new session.
214 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
216 struct cryptoini *cr;
223 if (crypto_drivers == NULL)
227 * The algorithm we use here is pretty stupid; just use the
228 * first driver that supports all the algorithms we need.
230 * XXX We need more smarts here (in real life too, but that's
231 * XXX another story altogether).
234 for (hid = 0; hid < crypto_drivers_num; hid++) {
236 * If it's not initialized or has remaining sessions
237 * referencing it, skip.
239 if (crypto_drivers[hid].cc_newsession == NULL ||
240 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP))
243 /* Hardware required -- ignore software drivers. */
245 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE))
247 /* Software required -- ignore hardware drivers. */
249 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
252 /* See if all the algorithms are supported. */
253 for (cr = cri; cr; cr = cr->cri_next)
254 if (crypto_drivers[hid].cc_alg[cr->cri_alg] == 0)
258 /* Ok, all algorithms are supported. */
261 * Can't do everything in one session.
263 * XXX Fix this. We need to inject a "virtual" session layer right
267 /* Call the driver initialization routine. */
268 lid = hid; /* Pass the driver ID. */
269 err = crypto_drivers[hid].cc_newsession(
270 crypto_drivers[hid].cc_arg, &lid, cri);
274 (*sid) |= (lid & 0xffffffff);
275 crypto_drivers[hid].cc_sessions++;
286 * Delete an existing session (or a reserved session on an unregistered
290 crypto_freesession(u_int64_t sid)
297 if (crypto_drivers == NULL) {
302 /* Determine two IDs. */
303 hid = SESID2HID(sid);
305 if (hid >= crypto_drivers_num) {
310 if (crypto_drivers[hid].cc_sessions)
311 crypto_drivers[hid].cc_sessions--;
313 /* Call the driver cleanup routine, if available. */
314 if (crypto_drivers[hid].cc_freesession)
315 err = crypto_drivers[hid].cc_freesession(
316 crypto_drivers[hid].cc_arg, sid);
321 * If this was the last session of a driver marked as invalid,
322 * make the entry available for reuse.
324 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP) &&
325 crypto_drivers[hid].cc_sessions == 0)
326 bzero(&crypto_drivers[hid], sizeof(struct cryptocap));
334 * Return an unused driver id. Used by drivers prior to registering
335 * support for the algorithms they handle.
338 crypto_get_driverid(u_int32_t flags)
340 struct cryptocap *newdrv;
344 for (i = 0; i < crypto_drivers_num; i++)
345 if (crypto_drivers[i].cc_process == NULL &&
346 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0 &&
347 crypto_drivers[i].cc_sessions == 0)
350 /* Out of entries, allocate some more. */
351 if (i == crypto_drivers_num) {
352 /* Be careful about wrap-around. */
353 if (2 * crypto_drivers_num <= crypto_drivers_num) {
355 printf("crypto: driver count wraparound!\n");
359 newdrv = malloc(2 * crypto_drivers_num *
360 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
361 if (newdrv == NULL) {
363 printf("crypto: no space to expand driver table!\n");
367 bcopy(crypto_drivers, newdrv,
368 crypto_drivers_num * sizeof(struct cryptocap));
370 crypto_drivers_num *= 2;
372 free(crypto_drivers, M_CRYPTO_DATA);
373 crypto_drivers = newdrv;
376 /* NB: state is zero'd on free */
377 crypto_drivers[i].cc_sessions = 1; /* Mark */
378 crypto_drivers[i].cc_flags = flags;
380 printf("crypto: assign driver %u, flags %u\n", i, flags);
387 static struct cryptocap *
388 crypto_checkdriver(u_int32_t hid)
390 if (crypto_drivers == NULL)
392 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
396 * Register support for a key-related algorithm. This routine
397 * is called once for each algorithm supported a driver.
400 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags,
401 int (*kprocess)(void*, struct cryptkop *, int),
405 struct cryptocap *cap;
410 cap = crypto_checkdriver(driverid);
412 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
414 * XXX Do some performance testing to determine placing.
415 * XXX We probably need an auxiliary data structure that
416 * XXX describes relative performances.
419 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
421 printf("crypto: driver %u registers key alg %u flags %u\n"
427 if (cap->cc_kprocess == NULL) {
429 cap->cc_kprocess = kprocess;
440 * Register support for a non-key-related algorithm. This routine
441 * is called once for each such algorithm supported by a driver.
444 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
446 int (*newses)(void*, u_int32_t*, struct cryptoini*),
447 int (*freeses)(void*, u_int64_t),
448 int (*process)(void*, struct cryptop *, int),
451 struct cryptocap *cap;
456 cap = crypto_checkdriver(driverid);
457 /* NB: algorithms are in the range [1..max] */
459 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
461 * XXX Do some performance testing to determine placing.
462 * XXX We probably need an auxiliary data structure that
463 * XXX describes relative performances.
466 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
467 cap->cc_max_op_len[alg] = maxoplen;
469 printf("crypto: driver %u registers alg %u flags %u maxoplen %u\n"
476 if (cap->cc_process == NULL) {
478 cap->cc_newsession = newses;
479 cap->cc_process = process;
480 cap->cc_freesession = freeses;
481 cap->cc_sessions = 0; /* Unmark */
492 * Unregister a crypto driver. If there are pending sessions using it,
493 * leave enough information around so that subsequent calls using those
494 * sessions will correctly detect the driver has been unregistered and
498 crypto_unregister(u_int32_t driverid, int alg)
500 int i, err, s = splcrypto();
502 struct cryptocap *cap;
504 cap = crypto_checkdriver(driverid);
506 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
507 cap->cc_alg[alg] != 0) {
508 cap->cc_alg[alg] = 0;
509 cap->cc_max_op_len[alg] = 0;
511 /* Was this the last algorithm ? */
512 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
513 if (cap->cc_alg[i] != 0)
516 if (i == CRYPTO_ALGORITHM_MAX + 1) {
517 ses = cap->cc_sessions;
518 bzero(cap, sizeof(struct cryptocap));
521 * If there are pending sessions, just mark as invalid.
523 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
524 cap->cc_sessions = ses;
536 * Unregister all algorithms associated with a crypto driver.
537 * If there are pending sessions using it, leave enough information
538 * around so that subsequent calls using those sessions will
539 * correctly detect the driver has been unregistered and reroute
543 crypto_unregister_all(u_int32_t driverid)
545 int i, err, s = splcrypto();
547 struct cryptocap *cap;
549 cap = crypto_checkdriver(driverid);
551 for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
553 cap->cc_max_op_len[i] = 0;
555 ses = cap->cc_sessions;
556 bzero(cap, sizeof(struct cryptocap));
559 * If there are pending sessions, just mark as invalid.
561 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
562 cap->cc_sessions = ses;
573 * Clear blockage on a driver. The what parameter indicates whether
574 * the driver is now ready for cryptop's and/or cryptokop's.
577 crypto_unblock(u_int32_t driverid, int what)
579 struct cryptocap *cap;
580 int needwakeup, err, s;
583 cap = crypto_checkdriver(driverid);
586 if (what & CRYPTO_SYMQ) {
587 needwakeup |= cap->cc_qblocked;
588 cap->cc_qblocked = 0;
590 if (what & CRYPTO_ASYMQ) {
591 needwakeup |= cap->cc_kqblocked;
592 cap->cc_kqblocked = 0;
605 * Dispatch a crypto request to a driver or queue
606 * it, to be processed by the kernel thread.
609 crypto_dispatch(struct cryptop *crp)
611 u_int32_t hid = SESID2HID(crp->crp_sid);
614 cryptostats.cs_ops++;
618 nanouptime(&crp->crp_tstamp);
621 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
622 struct cryptocap *cap;
624 * Caller marked the request to be processed
625 * immediately; dispatch it directly to the
626 * driver unless the driver is currently blocked.
628 cap = crypto_checkdriver(hid);
629 if (cap && !cap->cc_qblocked) {
630 result = crypto_invoke(crp, 0);
631 if (result == ERESTART) {
633 * The driver ran out of resources, mark the
634 * driver ``blocked'' for cryptop's and put
635 * the op on the queue.
637 crypto_drivers[hid].cc_qblocked = 1;
638 TAILQ_INSERT_HEAD(&crp_q, crp, crp_next);
639 cryptostats.cs_blocks++;
644 * The driver is blocked, just queue the op until
645 * it unblocks and the swi thread gets kicked.
647 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
651 int wasempty = TAILQ_EMPTY(&crp_q);
653 * Caller marked the request as ``ok to delay'';
654 * queue it for the swi thread. This is desirable
655 * when the operation is low priority and/or suitable
658 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
669 * Add an asymetric crypto request to a queue,
670 * to be processed by the kernel thread.
673 crypto_kdispatch(struct cryptkop *krp)
675 struct cryptocap *cap;
678 cryptostats.cs_kops++;
681 cap = crypto_checkdriver(krp->krp_hid);
682 if (cap && !cap->cc_kqblocked) {
683 result = crypto_kinvoke(krp, 0);
684 if (result == ERESTART) {
686 * The driver ran out of resources, mark the
687 * driver ``blocked'' for cryptop's and put
688 * the op on the queue.
690 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
691 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
692 cryptostats.cs_kblocks++;
696 * The driver is blocked, just queue the op until
697 * it unblocks and the swi thread gets kicked.
699 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
708 * Dispatch an assymetric crypto request to the appropriate crypto devices.
711 crypto_kinvoke(struct cryptkop *krp, int hint)
719 if (krp->krp_callback == NULL) {
720 free(krp, M_XDATA); /* XXX allocated in cryptodev */
724 for (hid = 0; hid < crypto_drivers_num; hid++) {
725 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
726 !crypto_devallowsoft)
728 if (crypto_drivers[hid].cc_kprocess == NULL)
730 if ((crypto_drivers[hid].cc_kalg[krp->krp_op] &
731 CRYPTO_ALG_FLAG_SUPPORTED) == 0)
735 if (hid < crypto_drivers_num) {
737 error = crypto_drivers[hid].cc_kprocess(
738 crypto_drivers[hid].cc_karg, krp, hint);
743 krp->krp_status = error;
751 crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
753 struct timespec now, t;
756 t.tv_sec = now.tv_sec - tv->tv_sec;
757 t.tv_nsec = now.tv_nsec - tv->tv_nsec;
760 t.tv_nsec += 1000000000;
762 timespecadd(&ts->acc, &t);
763 if (timespeccmp(&t, &ts->min, <))
765 if (timespeccmp(&t, &ts->max, >))
774 * Dispatch a crypto request to the appropriate crypto devices.
777 crypto_invoke(struct cryptop *crp, int hint)
780 int (*process)(void*, struct cryptop *, int);
784 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
789 if (crp->crp_callback == NULL) {
793 if (crp->crp_desc == NULL) {
794 crp->crp_etype = EINVAL;
799 hid = SESID2HID(crp->crp_sid);
800 if (hid < crypto_drivers_num) {
801 if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP)
802 crypto_freesession(crp->crp_sid);
803 process = crypto_drivers[hid].cc_process;
808 if (process == NULL) {
809 struct cryptodesc *crd;
813 * Driver has unregistered; migrate the session and return
814 * an error to the caller so they'll resubmit the op.
816 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
817 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
819 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 0) == 0)
822 crp->crp_etype = EAGAIN;
827 * Invoke the driver to process the request.
829 return (*process)(crypto_drivers[hid].cc_arg, crp, hint);
834 * Release a set of crypto descriptors.
837 crypto_freereq(struct cryptop *crp)
839 struct cryptodesc *crd;
842 while ((crd = crp->crp_desc) != NULL) {
843 crp->crp_desc = crd->crd_next;
844 zfree(cryptodesc_zone, crd);
846 zfree(cryptop_zone, crp);
851 * Acquire a set of crypto descriptors. The descriptors are self contained
852 * so no special spl protection is necessary.
855 crypto_getreq(int num)
857 struct cryptodesc *crd;
860 crp = zalloc(cryptop_zone);
862 bzero(crp, sizeof (*crp));
864 crd = zalloc(cryptodesc_zone);
870 bzero(crd, sizeof (*crd));
871 crd->crd_next = crp->crp_desc;
879 * Invoke the callback on behalf of the driver.
882 crypto_done(struct cryptop *crp)
884 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
885 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
886 crp->crp_flags |= CRYPTO_F_DONE;
887 if (crp->crp_etype != 0)
888 cryptostats.cs_errs++;
891 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
893 if (crp->crp_flags & CRYPTO_F_CBIMM) {
895 * Do the callback directly. This is ok when the
896 * callback routine does very little (e.g. the
897 * /dev/crypto callback method just does a wakeup).
902 * NB: We must copy the timestamp before
903 * doing the callback as the cryptop is
904 * likely to be reclaimed.
906 struct timespec t = crp->crp_tstamp;
907 crypto_tstat(&cryptostats.cs_cb, &t);
908 crp->crp_callback(crp);
909 crypto_tstat(&cryptostats.cs_finis, &t);
912 crp->crp_callback(crp);
916 * Normal case; queue the callback for the thread.
918 * The return queue is manipulated by the swi thread
919 * and, potentially, by crypto device drivers calling
920 * back to mark operations completed. Thus we need
921 * to mask both while manipulating the return queue.
924 wasempty = TAILQ_EMPTY(&crp_ret_q);
925 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
927 wakeup_one(&crp_ret_q);
933 * Invoke the callback on behalf of the driver.
936 crypto_kdone(struct cryptkop *krp)
940 if (krp->krp_status != 0)
941 cryptostats.cs_kerrs++;
943 * The return queue is manipulated by the swi thread
944 * and, potentially, by crypto device drivers calling
945 * back to mark operations completed. Thus we need
946 * to mask both while manipulating the return queue.
949 wasempty = TAILQ_EMPTY(&crp_ret_kq);
950 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
952 wakeup_one(&crp_ret_q);
957 crypto_getfeat(int *featp)
959 int hid, kalg, feat = 0;
962 if (!crypto_userasymcrypto)
965 for (hid = 0; hid < crypto_drivers_num; hid++) {
966 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
967 !crypto_devallowsoft) {
970 if (crypto_drivers[hid].cc_kprocess == NULL)
972 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
973 if ((crypto_drivers[hid].cc_kalg[kalg] &
974 CRYPTO_ALG_FLAG_SUPPORTED) != 0)
984 * Software interrupt thread to dispatch crypto requests.
987 cryptointr(void *dummy)
989 struct cryptop *crp, *submit;
990 struct cryptkop *krp;
991 struct cryptocap *cap;
994 cryptostats.cs_intrs++;
998 * Find the first element in the queue that can be
999 * processed and look-ahead to see if multiple ops
1000 * are ready for the same driver.
1004 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1005 u_int32_t hid = SESID2HID(crp->crp_sid);
1006 cap = crypto_checkdriver(hid);
1007 if (cap == NULL || cap->cc_process == NULL) {
1008 /* Op needs to be migrated, process it. */
1013 if (!cap->cc_qblocked) {
1014 if (submit != NULL) {
1016 * We stop on finding another op,
1017 * regardless whether its for the same
1018 * driver or not. We could keep
1019 * searching the queue but it might be
1020 * better to just use a per-driver
1023 if (SESID2HID(submit->crp_sid) == hid)
1024 hint = CRYPTO_HINT_MORE;
1028 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1030 /* keep scanning for more are q'd */
1034 if (submit != NULL) {
1035 TAILQ_REMOVE(&crp_q, submit, crp_next);
1036 result = crypto_invoke(submit, hint);
1037 if (result == ERESTART) {
1039 * The driver ran out of resources, mark the
1040 * driver ``blocked'' for cryptop's and put
1041 * the request back in the queue. It would
1042 * best to put the request back where we got
1043 * it but that's hard so for now we put it
1044 * at the front. This should be ok; putting
1045 * it at the end does not work.
1047 /* XXX validate sid again? */
1048 crypto_drivers[SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1049 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1050 cryptostats.cs_blocks++;
1054 /* As above, but for key ops */
1055 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1056 cap = crypto_checkdriver(krp->krp_hid);
1057 if (cap == NULL || cap->cc_kprocess == NULL) {
1058 /* Op needs to be migrated, process it. */
1061 if (!cap->cc_kqblocked)
1065 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1066 result = crypto_kinvoke(krp, 0);
1067 if (result == ERESTART) {
1069 * The driver ran out of resources, mark the
1070 * driver ``blocked'' for cryptkop's and put
1071 * the request back in the queue. It would
1072 * best to put the request back where we got
1073 * it but that's hard so for now we put it
1074 * at the front. This should be ok; putting
1075 * it at the end does not work.
1077 /* XXX validate sid again? */
1078 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1079 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1080 cryptostats.cs_kblocks++;
1083 } while (submit != NULL || krp != NULL);
1088 * Kernel thread to do callbacks.
1093 struct cryptop *crp;
1094 struct cryptkop *krp;
1099 crp = TAILQ_FIRST(&crp_ret_q);
1101 TAILQ_REMOVE(&crp_ret_q, crp, crp_next);
1102 krp = TAILQ_FIRST(&crp_ret_kq);
1104 TAILQ_REMOVE(&crp_ret_kq, krp, krp_next);
1106 if (crp != NULL || krp != NULL) {
1107 splx(s); /* lower ipl for callbacks */
1109 #ifdef CRYPTO_TIMING
1110 if (crypto_timing) {
1112 * NB: We must copy the timestamp before
1113 * doing the callback as the cryptop is
1114 * likely to be reclaimed.
1116 struct timespec t = crp->crp_tstamp;
1117 crypto_tstat(&cryptostats.cs_cb, &t);
1118 crp->crp_callback(crp);
1119 crypto_tstat(&cryptostats.cs_finis, &t);
1122 crp->crp_callback(crp);
1125 krp->krp_callback(krp);
1128 (void) tsleep(&crp_ret_q, 0, "crypto_wait", 0);
1129 cryptostats.cs_rets++;