1 /* $NetBSD: crypto.c,v 1.33 2009/03/25 01:26:13 darran Exp $ */
2 /* $FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $ */
3 /* $OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $ */
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Coyote Point Systems, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
35 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
37 * This code was written by Angelos D. Keromytis in Athens, Greece, in
38 * February 2000. Network Security Technologies Inc. (NSTI) kindly
39 * supported the development of this code.
41 * Copyright (c) 2000, 2001 Angelos D. Keromytis
43 * Permission to use, copy, and modify this software with or without fee
44 * is hereby granted, provided that this entire notice is included in
45 * all source code copies of any software which is or includes a copy or
46 * modification of this software.
48 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
49 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
50 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
51 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
55 #include <sys/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.33 2009/03/25 01:26:13 darran Exp $");
58 #include <sys/param.h>
59 #include <sys/reboot.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
64 #include <sys/kthread.h>
66 #include <sys/sysctl.h>
70 #include <opencrypto/cryptodev.h>
71 #include <opencrypto/xform.h> /* XXX for M_XDATA */
73 kcondvar_t cryptoret_cv
;
76 /* below are kludges for residual code wrtitten to FreeBSD interfaces */
78 #define register_swi(lvl, fn) \
79 softint_establish(SOFTINT_NET, (void (*)(void*))fn, NULL)
80 #define unregister_swi(lvl, fn) softint_disestablish(softintr_cookie)
81 #define setsoftcrypto(x) softint_schedule(x)
83 #define SESID2HID(sid) (((sid) >> 32) & 0xffffffff)
85 int crypto_ret_q_check(struct cryptop
*);
88 * Crypto drivers register themselves by allocating a slot in the
89 * crypto_drivers table with crypto_get_driverid() and then registering
90 * each algorithm they support with crypto_register() and crypto_kregister().
92 static struct cryptocap
*crypto_drivers
;
93 static int crypto_drivers_num
;
94 static void* softintr_cookie
;
97 * There are two queues for crypto requests; one for symmetric (e.g.
98 * cipher) operations and one for asymmetric (e.g. MOD) operations.
99 * See below for how synchronization is handled.
101 static TAILQ_HEAD(,cryptop
) crp_q
= /* request queues */
102 TAILQ_HEAD_INITIALIZER(crp_q
);
103 static TAILQ_HEAD(,cryptkop
) crp_kq
=
104 TAILQ_HEAD_INITIALIZER(crp_kq
);
107 * There are two queues for processing completed crypto requests; one
108 * for the symmetric and one for the asymmetric ops. We only need one
109 * but have two to avoid type futzing (cryptop vs. cryptkop). See below
110 * for how synchronization is handled.
112 static TAILQ_HEAD(crprethead
, cryptop
) crp_ret_q
= /* callback queues */
113 TAILQ_HEAD_INITIALIZER(crp_ret_q
);
114 static TAILQ_HEAD(krprethead
, cryptkop
) crp_ret_kq
=
115 TAILQ_HEAD_INITIALIZER(crp_ret_kq
);
118 * XXX these functions are ghastly hacks for when the submission
119 * XXX routines discover a request that was not CBIMM is already
120 * XXX done, and must be yanked from the retq (where _done) put it
121 * XXX as cryptoret won't get the chance. The queue is walked backwards
122 * XXX as the request is generally the last one queued.
124 * call with the lock held, or else.
127 crypto_ret_q_remove(struct cryptop
*crp
)
129 struct cryptop
* acrp
, *next
;
131 TAILQ_FOREACH_REVERSE_SAFE(acrp
, &crp_ret_q
, crprethead
, crp_next
, next
) {
133 TAILQ_REMOVE(&crp_ret_q
, crp
, crp_next
);
134 crp
->crp_flags
&= (~CRYPTO_F_ONRETQ
);
142 crypto_ret_kq_remove(struct cryptkop
*krp
)
144 struct cryptkop
* akrp
, *next
;
146 TAILQ_FOREACH_REVERSE_SAFE(akrp
, &crp_ret_kq
, krprethead
, krp_next
, next
) {
148 TAILQ_REMOVE(&crp_ret_kq
, krp
, krp_next
);
149 krp
->krp_flags
&= (~CRYPTO_F_ONRETQ
);
157 * Crypto op and desciptor data structures are allocated
158 * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
160 struct pool cryptop_pool
;
161 struct pool cryptodesc_pool
;
162 struct pool cryptkop_pool
;
164 int crypto_usercrypto
= 1; /* userland may open /dev/crypto */
165 int crypto_userasymcrypto
= 1; /* userland may do asym crypto reqs */
167 * cryptodevallowsoft is (intended to be) sysctl'able, controlling
168 * access to hardware versus software transforms as below:
170 * crypto_devallowsoft < 0: Force userlevel requests to use software
172 * crypto_devallowsoft = 0: Use hardware if present, grant userlevel
173 * requests for non-accelerated transforms
174 * (handling the latter in software)
175 * crypto_devallowsoft > 0: Allow user requests only for transforms which
176 * are hardware-accelerated.
178 int crypto_devallowsoft
= 1; /* only use hardware crypto */
180 SYSCTL_SETUP(sysctl_opencrypto_setup
, "sysctl opencrypto subtree setup")
182 sysctl_createv(clog
, 0, NULL
, NULL
,
184 CTLTYPE_NODE
, "kern", NULL
,
187 sysctl_createv(clog
, 0, NULL
, NULL
,
188 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
189 CTLTYPE_INT
, "usercrypto",
190 SYSCTL_DESCR("Enable/disable user-mode access to "
192 NULL
, 0, &crypto_usercrypto
, 0,
193 CTL_KERN
, CTL_CREATE
, CTL_EOL
);
194 sysctl_createv(clog
, 0, NULL
, NULL
,
195 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
196 CTLTYPE_INT
, "userasymcrypto",
197 SYSCTL_DESCR("Enable/disable user-mode access to "
198 "asymmetric crypto support"),
199 NULL
, 0, &crypto_userasymcrypto
, 0,
200 CTL_KERN
, CTL_CREATE
, CTL_EOL
);
201 sysctl_createv(clog
, 0, NULL
, NULL
,
202 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
203 CTLTYPE_INT
, "cryptodevallowsoft",
204 SYSCTL_DESCR("Enable/disable use of software "
205 "asymmetric crypto support"),
206 NULL
, 0, &crypto_devallowsoft
, 0,
207 CTL_KERN
, CTL_CREATE
, CTL_EOL
);
210 MALLOC_DEFINE(M_CRYPTO_DATA
, "crypto", "crypto session records");
213 * Synchronization: read carefully, this is non-trivial.
215 * Crypto requests are submitted via crypto_dispatch. Typically
216 * these come in from network protocols at spl0 (output path) or
217 * spl[,soft]net (input path).
219 * Requests are typically passed on the driver directly, but they
220 * may also be queued for processing by a software interrupt thread,
221 * cryptointr, that runs at splsoftcrypto. This thread dispatches
222 * the requests to crypto drivers (h/w or s/w) who call crypto_done
223 * when a request is complete. Hardware crypto drivers are assumed
224 * to register their IRQ's as network devices so their interrupt handlers
225 * and subsequent "done callbacks" happen at spl[imp,net].
227 * Completed crypto ops are queued for a separate kernel thread that
228 * handles the callbacks at spl0. This decoupling insures the crypto
229 * driver interrupt service routine is not delayed while the callback
230 * takes place and that callbacks are delivered after a context switch
231 * (as opposed to a software interrupt that clients must block).
233 * This scheme is not intended for SMP machines.
235 static void cryptointr(void); /* swi thread to dispatch ops */
236 static void cryptoret(void); /* kernel thread for callbacks*/
237 static struct lwp
*cryptothread
;
238 static void crypto_destroy(void);
239 static int crypto_invoke(struct cryptop
*crp
, int hint
);
240 static int crypto_kinvoke(struct cryptkop
*krp
, int hint
);
242 static struct cryptostats cryptostats
;
244 static int crypto_timing
= 0;
252 mutex_init(&crypto_mtx
, MUTEX_DEFAULT
, IPL_NET
);
253 cv_init(&cryptoret_cv
, "crypto_wait");
254 pool_init(&cryptop_pool
, sizeof(struct cryptop
), 0, 0,
255 0, "cryptop", NULL
, IPL_NET
);
256 pool_init(&cryptodesc_pool
, sizeof(struct cryptodesc
), 0, 0,
257 0, "cryptodesc", NULL
, IPL_NET
);
258 pool_init(&cryptkop_pool
, sizeof(struct cryptkop
), 0, 0,
259 0, "cryptkop", NULL
, IPL_NET
);
261 crypto_drivers
= malloc(CRYPTO_DRIVERS_INITIAL
*
262 sizeof(struct cryptocap
), M_CRYPTO_DATA
, M_NOWAIT
| M_ZERO
);
263 if (crypto_drivers
== NULL
) {
264 printf("crypto_init: cannot malloc driver table\n");
267 crypto_drivers_num
= CRYPTO_DRIVERS_INITIAL
;
269 softintr_cookie
= register_swi(SWI_CRYPTO
, cryptointr
);
270 error
= kthread_create(PRI_NONE
, KTHREAD_MPSAFE
, NULL
,
271 (void (*)(void*))cryptoret
, NULL
, &cryptothread
, "cryptoret");
273 printf("crypto_init: cannot start cryptoret thread; error %d",
284 static ONCE_DECL(crypto_init_once
);
286 RUN_ONCE(&crypto_init_once
, crypto_init0
);
292 /* XXX no wait to reclaim zones */
293 if (crypto_drivers
!= NULL
)
294 free(crypto_drivers
, M_CRYPTO_DATA
);
295 unregister_swi(SWI_CRYPTO
, cryptointr
);
299 * Create a new session. Must be called with crypto_mtx held.
302 crypto_newsession(u_int64_t
*sid
, struct cryptoini
*cri
, int hard
)
304 struct cryptoini
*cr
;
308 KASSERT(mutex_owned(&crypto_mtx
));
310 if (crypto_drivers
== NULL
)
314 * The algorithm we use here is pretty stupid; just use the
315 * first driver that supports all the algorithms we need.
317 * XXX We need more smarts here (in real life too, but that's
318 * XXX another story altogether).
321 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
323 * If it's not initialized or has remaining sessions
324 * referencing it, skip.
326 if (crypto_drivers
[hid
].cc_newsession
== NULL
||
327 (crypto_drivers
[hid
].cc_flags
& CRYPTOCAP_F_CLEANUP
))
330 /* Hardware required -- ignore software drivers. */
332 (crypto_drivers
[hid
].cc_flags
& CRYPTOCAP_F_SOFTWARE
))
334 /* Software required -- ignore hardware drivers. */
336 (crypto_drivers
[hid
].cc_flags
& CRYPTOCAP_F_SOFTWARE
) == 0)
339 /* See if all the algorithms are supported. */
340 for (cr
= cri
; cr
; cr
= cr
->cri_next
)
341 if (crypto_drivers
[hid
].cc_alg
[cr
->cri_alg
] == 0) {
342 DPRINTF(("crypto_newsession: alg %d not supported\n", cr
->cri_alg
));
347 /* Ok, all algorithms are supported. */
350 * Can't do everything in one session.
352 * XXX Fix this. We need to inject a "virtual" session layer right
356 /* Call the driver initialization routine. */
357 lid
= hid
; /* Pass the driver ID. */
358 err
= crypto_drivers
[hid
].cc_newsession(
359 crypto_drivers
[hid
].cc_arg
, &lid
, cri
);
363 (*sid
) |= (lid
& 0xffffffff);
364 crypto_drivers
[hid
].cc_sessions
++;
375 * Delete an existing session (or a reserved session on an unregistered
376 * driver). Must be called with crypto_mtx mutex held.
379 crypto_freesession(u_int64_t sid
)
384 KASSERT(mutex_owned(&crypto_mtx
));
386 if (crypto_drivers
== NULL
) {
391 /* Determine two IDs. */
392 hid
= SESID2HID(sid
);
394 if (hid
>= crypto_drivers_num
) {
399 if (crypto_drivers
[hid
].cc_sessions
)
400 crypto_drivers
[hid
].cc_sessions
--;
402 /* Call the driver cleanup routine, if available. */
403 if (crypto_drivers
[hid
].cc_freesession
) {
404 err
= crypto_drivers
[hid
].cc_freesession(
405 crypto_drivers
[hid
].cc_arg
, sid
);
411 * If this was the last session of a driver marked as invalid,
412 * make the entry available for reuse.
414 if ((crypto_drivers
[hid
].cc_flags
& CRYPTOCAP_F_CLEANUP
) &&
415 crypto_drivers
[hid
].cc_sessions
== 0)
416 memset(&crypto_drivers
[hid
], 0, sizeof(struct cryptocap
));
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_init(); /* XXX oh, this is foul! */
434 mutex_spin_enter(&crypto_mtx
);
435 for (i
= 0; i
< crypto_drivers_num
; i
++)
436 if (crypto_drivers
[i
].cc_process
== NULL
&&
437 (crypto_drivers
[i
].cc_flags
& CRYPTOCAP_F_CLEANUP
) == 0 &&
438 crypto_drivers
[i
].cc_sessions
== 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 mutex_spin_exit(&crypto_mtx
);
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 mutex_spin_exit(&crypto_mtx
);
454 printf("crypto: no space to expand driver table!\n");
458 memcpy(newdrv
, crypto_drivers
,
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
;
472 printf("crypto: assign driver %u, flags %u\n", i
, flags
);
474 mutex_spin_exit(&crypto_mtx
);
479 static struct cryptocap
*
480 crypto_checkdriver(u_int32_t hid
)
482 if (crypto_drivers
== NULL
)
484 return (hid
>= crypto_drivers_num
? NULL
: &crypto_drivers
[hid
]);
488 * Register support for a key-related algorithm. This routine
489 * is called once for each algorithm supported a driver.
492 crypto_kregister(u_int32_t driverid
, int kalg
, u_int32_t flags
,
493 int (*kprocess
)(void*, struct cryptkop
*, int),
496 struct cryptocap
*cap
;
499 mutex_spin_enter(&crypto_mtx
);
501 cap
= crypto_checkdriver(driverid
);
503 (CRK_ALGORITM_MIN
<= kalg
&& kalg
<= CRK_ALGORITHM_MAX
)) {
505 * XXX Do some performance testing to determine placing.
506 * XXX We probably need an auxiliary data structure that
507 * XXX describes relative performances.
510 cap
->cc_kalg
[kalg
] = flags
| CRYPTO_ALG_FLAG_SUPPORTED
;
512 printf("crypto: driver %u registers key alg %u "
520 if (cap
->cc_kprocess
== NULL
) {
522 cap
->cc_kprocess
= kprocess
;
528 mutex_spin_exit(&crypto_mtx
);
533 * Register support for a non-key-related algorithm. This routine
534 * is called once for each such algorithm supported by a driver.
537 crypto_register(u_int32_t driverid
, int alg
, u_int16_t maxoplen
,
539 int (*newses
)(void*, u_int32_t
*, struct cryptoini
*),
540 int (*freeses
)(void*, u_int64_t
),
541 int (*process
)(void*, struct cryptop
*, int),
544 struct cryptocap
*cap
;
547 mutex_spin_enter(&crypto_mtx
);
549 cap
= crypto_checkdriver(driverid
);
550 /* NB: algorithms are in the range [1..max] */
552 (CRYPTO_ALGORITHM_MIN
<= alg
&& alg
<= CRYPTO_ALGORITHM_MAX
)) {
554 * XXX Do some performance testing to determine placing.
555 * XXX We probably need an auxiliary data structure that
556 * XXX describes relative performances.
559 cap
->cc_alg
[alg
] = flags
| CRYPTO_ALG_FLAG_SUPPORTED
;
560 cap
->cc_max_op_len
[alg
] = maxoplen
;
562 printf("crypto: driver %u registers alg %u "
563 "flags %u maxoplen %u\n",
571 if (cap
->cc_process
== NULL
) {
573 cap
->cc_newsession
= newses
;
574 cap
->cc_process
= process
;
575 cap
->cc_freesession
= freeses
;
576 cap
->cc_sessions
= 0; /* Unmark */
582 mutex_spin_exit(&crypto_mtx
);
587 * Unregister a crypto driver. If there are pending sessions using it,
588 * leave enough information around so that subsequent calls using those
589 * sessions will correctly detect the driver has been unregistered and
593 crypto_unregister(u_int32_t driverid
, int alg
)
597 struct cryptocap
*cap
;
599 mutex_spin_enter(&crypto_mtx
);
601 cap
= crypto_checkdriver(driverid
);
603 (CRYPTO_ALGORITHM_MIN
<= alg
&& alg
<= CRYPTO_ALGORITHM_MAX
) &&
604 cap
->cc_alg
[alg
] != 0) {
605 cap
->cc_alg
[alg
] = 0;
606 cap
->cc_max_op_len
[alg
] = 0;
608 /* Was this the last algorithm ? */
609 for (i
= 1; i
<= CRYPTO_ALGORITHM_MAX
; i
++)
610 if (cap
->cc_alg
[i
] != 0)
613 if (i
== CRYPTO_ALGORITHM_MAX
+ 1) {
614 ses
= cap
->cc_sessions
;
615 memset(cap
, 0, sizeof(struct cryptocap
));
618 * If there are pending sessions, just mark as invalid.
620 cap
->cc_flags
|= CRYPTOCAP_F_CLEANUP
;
621 cap
->cc_sessions
= ses
;
628 mutex_spin_exit(&crypto_mtx
);
633 * Unregister all algorithms associated with a crypto driver.
634 * If there are pending sessions using it, leave enough information
635 * around so that subsequent calls using those sessions will
636 * correctly detect the driver has been unregistered and reroute
639 * XXX careful. Don't change this to call crypto_unregister() for each
640 * XXX registered algorithm unless you drop the mutex across the calls;
641 * XXX you can't take it recursively.
644 crypto_unregister_all(u_int32_t driverid
)
648 struct cryptocap
*cap
;
650 mutex_spin_enter(&crypto_mtx
);
651 cap
= crypto_checkdriver(driverid
);
653 for (i
= CRYPTO_ALGORITHM_MIN
; i
<= CRYPTO_ALGORITHM_MAX
; i
++) {
655 cap
->cc_max_op_len
[i
] = 0;
657 ses
= cap
->cc_sessions
;
658 memset(cap
, 0, sizeof(struct cryptocap
));
661 * If there are pending sessions, just mark as invalid.
663 cap
->cc_flags
|= CRYPTOCAP_F_CLEANUP
;
664 cap
->cc_sessions
= ses
;
670 mutex_spin_exit(&crypto_mtx
);
675 * Clear blockage on a driver. The what parameter indicates whether
676 * the driver is now ready for cryptop's and/or cryptokop's.
679 crypto_unblock(u_int32_t driverid
, int what
)
681 struct cryptocap
*cap
;
684 mutex_spin_enter(&crypto_mtx
);
685 cap
= crypto_checkdriver(driverid
);
688 if (what
& CRYPTO_SYMQ
) {
689 needwakeup
|= cap
->cc_qblocked
;
690 cap
->cc_qblocked
= 0;
692 if (what
& CRYPTO_ASYMQ
) {
693 needwakeup
|= cap
->cc_kqblocked
;
694 cap
->cc_kqblocked
= 0;
697 mutex_spin_exit(&crypto_mtx
);
699 setsoftcrypto(softintr_cookie
);
702 mutex_spin_exit(&crypto_mtx
);
709 * Dispatch a crypto request to a driver or queue
710 * it, to be processed by the kernel thread.
713 crypto_dispatch(struct cryptop
*crp
)
715 u_int32_t hid
= SESID2HID(crp
->crp_sid
);
718 mutex_spin_enter(&crypto_mtx
);
719 DPRINTF(("crypto_dispatch: crp %08x, reqid 0x%x, alg %d\n",
722 crp
->crp_desc
->crd_alg
));
724 cryptostats
.cs_ops
++;
728 nanouptime(&crp
->crp_tstamp
);
730 if ((crp
->crp_flags
& CRYPTO_F_BATCH
) == 0) {
731 struct cryptocap
*cap
;
733 * Caller marked the request to be processed
734 * immediately; dispatch it directly to the
735 * driver unless the driver is currently blocked.
737 cap
= crypto_checkdriver(hid
);
738 if (cap
&& !cap
->cc_qblocked
) {
739 mutex_spin_exit(&crypto_mtx
);
740 result
= crypto_invoke(crp
, 0);
741 if (result
== ERESTART
) {
743 * The driver ran out of resources, mark the
744 * driver ``blocked'' for cryptop's and put
745 * the op on the queue.
747 mutex_spin_enter(&crypto_mtx
);
748 crypto_drivers
[hid
].cc_qblocked
= 1;
749 TAILQ_INSERT_HEAD(&crp_q
, crp
, crp_next
);
750 cryptostats
.cs_blocks
++;
751 mutex_spin_exit(&crypto_mtx
);
756 * The driver is blocked, just queue the op until
757 * it unblocks and the swi thread gets kicked.
759 TAILQ_INSERT_TAIL(&crp_q
, crp
, crp_next
);
763 int wasempty
= TAILQ_EMPTY(&crp_q
);
765 * Caller marked the request as ``ok to delay'';
766 * queue it for the swi thread. This is desirable
767 * when the operation is low priority and/or suitable
770 TAILQ_INSERT_TAIL(&crp_q
, crp
, crp_next
);
772 mutex_spin_exit(&crypto_mtx
);
773 setsoftcrypto(softintr_cookie
);
781 mutex_spin_exit(&crypto_mtx
);
787 * Add an asymetric crypto request to a queue,
788 * to be processed by the kernel thread.
791 crypto_kdispatch(struct cryptkop
*krp
)
793 struct cryptocap
*cap
;
796 mutex_spin_enter(&crypto_mtx
);
797 cryptostats
.cs_kops
++;
799 cap
= crypto_checkdriver(krp
->krp_hid
);
800 if (cap
&& !cap
->cc_kqblocked
) {
801 mutex_spin_exit(&crypto_mtx
);
802 result
= crypto_kinvoke(krp
, 0);
803 if (result
== ERESTART
) {
805 * The driver ran out of resources, mark the
806 * driver ``blocked'' for cryptop's and put
807 * the op on the queue.
809 mutex_spin_enter(&crypto_mtx
);
810 crypto_drivers
[krp
->krp_hid
].cc_kqblocked
= 1;
811 TAILQ_INSERT_HEAD(&crp_kq
, krp
, krp_next
);
812 cryptostats
.cs_kblocks
++;
813 mutex_spin_exit(&crypto_mtx
);
817 * The driver is blocked, just queue the op until
818 * it unblocks and the swi thread gets kicked.
820 TAILQ_INSERT_TAIL(&crp_kq
, krp
, krp_next
);
822 mutex_spin_exit(&crypto_mtx
);
829 * Dispatch an assymetric crypto request to the appropriate crypto devices.
832 crypto_kinvoke(struct cryptkop
*krp
, int hint
)
840 if (krp
->krp_callback
== NULL
) {
841 cv_destroy(&krp
->krp_cv
);
842 pool_put(&cryptkop_pool
, krp
);
846 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
847 if ((crypto_drivers
[hid
].cc_flags
& CRYPTOCAP_F_SOFTWARE
) &&
848 crypto_devallowsoft
== 0)
850 if (crypto_drivers
[hid
].cc_kprocess
== NULL
)
852 if ((crypto_drivers
[hid
].cc_kalg
[krp
->krp_op
] &
853 CRYPTO_ALG_FLAG_SUPPORTED
) == 0)
857 if (hid
< crypto_drivers_num
) {
859 error
= crypto_drivers
[hid
].cc_kprocess(
860 crypto_drivers
[hid
].cc_karg
, krp
, hint
);
866 krp
->krp_status
= error
;
874 crypto_tstat(struct cryptotstat
*ts
, struct timespec
*tv
)
876 struct timespec now
, t
;
879 t
.tv_sec
= now
.tv_sec
- tv
->tv_sec
;
880 t
.tv_nsec
= now
.tv_nsec
- tv
->tv_nsec
;
883 t
.tv_nsec
+= 1000000000;
885 timespecadd(&ts
->acc
, &t
, &t
);
886 if (timespeccmp(&t
, &ts
->min
, <))
888 if (timespeccmp(&t
, &ts
->max
, >))
897 * Dispatch a crypto request to the appropriate crypto devices.
900 crypto_invoke(struct cryptop
*crp
, int hint
)
903 int (*process
)(void*, struct cryptop
*, int);
907 crypto_tstat(&cryptostats
.cs_invoke
, &crp
->crp_tstamp
);
912 if (crp
->crp_callback
== NULL
) {
915 if (crp
->crp_desc
== NULL
) {
916 crp
->crp_etype
= EINVAL
;
921 hid
= SESID2HID(crp
->crp_sid
);
922 if (hid
< crypto_drivers_num
) {
923 mutex_spin_enter(&crypto_mtx
);
924 if (crypto_drivers
[hid
].cc_flags
& CRYPTOCAP_F_CLEANUP
)
925 crypto_freesession(crp
->crp_sid
);
926 process
= crypto_drivers
[hid
].cc_process
;
927 mutex_spin_exit(&crypto_mtx
);
932 if (process
== NULL
) {
933 struct cryptodesc
*crd
;
937 * Driver has unregistered; migrate the session and return
938 * an error to the caller so they'll resubmit the op.
940 mutex_spin_enter(&crypto_mtx
);
941 for (crd
= crp
->crp_desc
; crd
->crd_next
; crd
= crd
->crd_next
)
942 crd
->CRD_INI
.cri_next
= &(crd
->crd_next
->CRD_INI
);
944 if (crypto_newsession(&nid
, &(crp
->crp_desc
->CRD_INI
), 0) == 0)
947 crp
->crp_etype
= EAGAIN
;
948 mutex_spin_exit(&crypto_mtx
);
954 * Invoke the driver to process the request.
956 DPRINTF(("calling process for %08x\n", (uint32_t)crp
));
957 return (*process
)(crypto_drivers
[hid
].cc_arg
, crp
, hint
);
962 * Release a set of crypto descriptors.
965 crypto_freereq(struct cryptop
*crp
)
967 struct cryptodesc
*crd
;
971 DPRINTF(("crypto_freereq[%d]: crp %p\n",
972 (uint32_t)crp
->crp_sid
, crp
));
975 if (crp
->crp_flags
& CRYPTO_F_ONRETQ
) {
976 panic("crypto_freereq() freeing crp on RETQ\n");
979 while ((crd
= crp
->crp_desc
) != NULL
) {
980 crp
->crp_desc
= crd
->crd_next
;
981 pool_put(&cryptodesc_pool
, crd
);
983 cv_destroy(&crp
->crp_cv
);
984 pool_put(&cryptop_pool
, crp
);
988 * Acquire a set of crypto descriptors.
991 crypto_getreq(int num
)
993 struct cryptodesc
*crd
;
996 crp
= pool_get(&cryptop_pool
, 0);
1000 memset(crp
, 0, sizeof(struct cryptop
));
1001 cv_init(&crp
->crp_cv
, "crydev");
1004 crd
= pool_get(&cryptodesc_pool
, 0);
1006 crypto_freereq(crp
);
1010 memset(crd
, 0, sizeof(struct cryptodesc
));
1011 crd
->crd_next
= crp
->crp_desc
;
1012 crp
->crp_desc
= crd
;
1019 * Invoke the callback on behalf of the driver.
1022 crypto_done(struct cryptop
*crp
)
1026 if (crp
->crp_etype
!= 0)
1027 cryptostats
.cs_errs
++;
1028 #ifdef CRYPTO_TIMING
1030 crypto_tstat(&cryptostats
.cs_done
, &crp
->crp_tstamp
);
1032 DPRINTF(("crypto_done[%d]: crp %08x\n",
1033 (uint32_t)crp
->crp_sid
, (uint32_t)crp
));
1036 * Normal case; queue the callback for the thread.
1038 * The return queue is manipulated by the swi thread
1039 * and, potentially, by crypto device drivers calling
1040 * back to mark operations completed. Thus we need
1041 * to mask both while manipulating the return queue.
1043 if (crp
->crp_flags
& CRYPTO_F_CBIMM
) {
1045 * Do the callback directly. This is ok when the
1046 * callback routine does very little (e.g. the
1047 * /dev/crypto callback method just does a wakeup).
1049 mutex_spin_enter(&crypto_mtx
);
1050 crp
->crp_flags
|= CRYPTO_F_DONE
;
1051 mutex_spin_exit(&crypto_mtx
);
1053 #ifdef CRYPTO_TIMING
1054 if (crypto_timing
) {
1056 * NB: We must copy the timestamp before
1057 * doing the callback as the cryptop is
1058 * likely to be reclaimed.
1060 struct timespec t
= crp
->crp_tstamp
;
1061 crypto_tstat(&cryptostats
.cs_cb
, &t
);
1062 crp
->crp_callback(crp
);
1063 crypto_tstat(&cryptostats
.cs_finis
, &t
);
1066 crp
->crp_callback(crp
);
1068 mutex_spin_enter(&crypto_mtx
);
1069 crp
->crp_flags
|= CRYPTO_F_DONE
;
1071 if (crp
->crp_flags
& CRYPTO_F_USER
) {
1072 /* the request has completed while
1073 * running in the user context
1074 * so don't queue it - the user
1075 * thread won't sleep when it sees
1076 * the CRYPTO_F_DONE flag.
1077 * This is an optimization to avoid
1078 * unecessary context switches.
1080 DPRINTF(("crypto_done[%d]: crp %08x CRYPTO_F_USER\n",
1081 (uint32_t)crp
->crp_sid
, (uint32_t)crp
));
1083 wasempty
= TAILQ_EMPTY(&crp_ret_q
);
1084 DPRINTF(("crypto_done[%d]: queueing %08x\n",
1085 (uint32_t)crp
->crp_sid
, (uint32_t)crp
));
1086 crp
->crp_flags
|= CRYPTO_F_ONRETQ
;
1087 TAILQ_INSERT_TAIL(&crp_ret_q
, crp
, crp_next
);
1089 DPRINTF(("crypto_done[%d]: waking cryptoret, crp %08x " \
1090 "hit empty queue\n.",
1091 (uint32_t)crp
->crp_sid
, (uint32_t)crp
));
1092 cv_signal(&cryptoret_cv
);
1095 mutex_spin_exit(&crypto_mtx
);
1100 * Invoke the callback on behalf of the driver.
1103 crypto_kdone(struct cryptkop
*krp
)
1107 if (krp
->krp_status
!= 0)
1108 cryptostats
.cs_kerrs
++;
1110 krp
->krp_flags
|= CRYPTO_F_DONE
;
1113 * The return queue is manipulated by the swi thread
1114 * and, potentially, by crypto device drivers calling
1115 * back to mark operations completed. Thus we need
1116 * to mask both while manipulating the return queue.
1118 if (krp
->krp_flags
& CRYPTO_F_CBIMM
) {
1119 krp
->krp_callback(krp
);
1121 mutex_spin_enter(&crypto_mtx
);
1122 wasempty
= TAILQ_EMPTY(&crp_ret_kq
);
1123 krp
->krp_flags
|= CRYPTO_F_ONRETQ
;
1124 TAILQ_INSERT_TAIL(&crp_ret_kq
, krp
, krp_next
);
1126 cv_signal(&cryptoret_cv
);
1127 mutex_spin_exit(&crypto_mtx
);
1132 crypto_getfeat(int *featp
)
1134 int hid
, kalg
, feat
= 0;
1136 mutex_spin_enter(&crypto_mtx
);
1138 if (crypto_userasymcrypto
== 0)
1141 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
1142 if ((crypto_drivers
[hid
].cc_flags
& CRYPTOCAP_F_SOFTWARE
) &&
1143 crypto_devallowsoft
== 0) {
1146 if (crypto_drivers
[hid
].cc_kprocess
== NULL
)
1148 for (kalg
= 0; kalg
< CRK_ALGORITHM_MAX
; kalg
++)
1149 if ((crypto_drivers
[hid
].cc_kalg
[kalg
] &
1150 CRYPTO_ALG_FLAG_SUPPORTED
) != 0)
1154 mutex_spin_exit(&crypto_mtx
);
1160 * Software interrupt thread to dispatch crypto requests.
1165 struct cryptop
*crp
, *submit
, *cnext
;
1166 struct cryptkop
*krp
, *knext
;
1167 struct cryptocap
*cap
;
1170 cryptostats
.cs_intrs
++;
1171 mutex_spin_enter(&crypto_mtx
);
1174 * Find the first element in the queue that can be
1175 * processed and look-ahead to see if multiple ops
1176 * are ready for the same driver.
1180 TAILQ_FOREACH_SAFE(crp
, &crp_q
, crp_next
, cnext
) {
1181 u_int32_t hid
= SESID2HID(crp
->crp_sid
);
1182 cap
= crypto_checkdriver(hid
);
1183 if (cap
== NULL
|| cap
->cc_process
== NULL
) {
1184 /* Op needs to be migrated, process it. */
1189 if (!cap
->cc_qblocked
) {
1190 if (submit
!= NULL
) {
1192 * We stop on finding another op,
1193 * regardless whether its for the same
1194 * driver or not. We could keep
1195 * searching the queue but it might be
1196 * better to just use a per-driver
1199 if (SESID2HID(submit
->crp_sid
) == hid
)
1200 hint
= CRYPTO_HINT_MORE
;
1204 if ((submit
->crp_flags
& CRYPTO_F_BATCH
) == 0)
1206 /* keep scanning for more are q'd */
1210 if (submit
!= NULL
) {
1211 TAILQ_REMOVE(&crp_q
, submit
, crp_next
);
1212 mutex_spin_exit(&crypto_mtx
);
1213 result
= crypto_invoke(submit
, hint
);
1214 /* we must take here as the TAILQ op or kinvoke
1215 may need this mutex below. sigh. */
1216 mutex_spin_enter(&crypto_mtx
);
1217 if (result
== ERESTART
) {
1219 * The driver ran out of resources, mark the
1220 * driver ``blocked'' for cryptop's and put
1221 * the request back in the queue. It would
1222 * best to put the request back where we got
1223 * it but that's hard so for now we put it
1224 * at the front. This should be ok; putting
1225 * it at the end does not work.
1227 /* XXX validate sid again? */
1228 crypto_drivers
[SESID2HID(submit
->crp_sid
)].cc_qblocked
= 1;
1229 TAILQ_INSERT_HEAD(&crp_q
, submit
, crp_next
);
1230 cryptostats
.cs_blocks
++;
1234 /* As above, but for key ops */
1235 TAILQ_FOREACH_SAFE(krp
, &crp_kq
, krp_next
, knext
) {
1236 cap
= crypto_checkdriver(krp
->krp_hid
);
1237 if (cap
== NULL
|| cap
->cc_kprocess
== NULL
) {
1238 /* Op needs to be migrated, process it. */
1241 if (!cap
->cc_kqblocked
)
1245 TAILQ_REMOVE(&crp_kq
, krp
, krp_next
);
1246 mutex_spin_exit(&crypto_mtx
);
1247 result
= crypto_kinvoke(krp
, 0);
1248 /* the next iteration will want the mutex. :-/ */
1249 mutex_spin_enter(&crypto_mtx
);
1250 if (result
== ERESTART
) {
1252 * The driver ran out of resources, mark the
1253 * driver ``blocked'' for cryptkop's and put
1254 * the request back in the queue. It would
1255 * best to put the request back where we got
1256 * it but that's hard so for now we put it
1257 * at the front. This should be ok; putting
1258 * it at the end does not work.
1260 /* XXX validate sid again? */
1261 crypto_drivers
[krp
->krp_hid
].cc_kqblocked
= 1;
1262 TAILQ_INSERT_HEAD(&crp_kq
, krp
, krp_next
);
1263 cryptostats
.cs_kblocks
++;
1266 } while (submit
!= NULL
|| krp
!= NULL
);
1267 mutex_spin_exit(&crypto_mtx
);
1271 * Kernel thread to do callbacks.
1276 struct cryptop
*crp
;
1277 struct cryptkop
*krp
;
1279 mutex_spin_enter(&crypto_mtx
);
1281 crp
= TAILQ_FIRST(&crp_ret_q
);
1283 TAILQ_REMOVE(&crp_ret_q
, crp
, crp_next
);
1284 crp
->crp_flags
&= ~CRYPTO_F_ONRETQ
;
1286 krp
= TAILQ_FIRST(&crp_ret_kq
);
1288 TAILQ_REMOVE(&crp_ret_kq
, krp
, krp_next
);
1289 krp
->krp_flags
&= ~CRYPTO_F_ONRETQ
;
1292 /* drop before calling any callbacks. */
1293 if (crp
== NULL
&& krp
== NULL
) {
1294 cryptostats
.cs_rets
++;
1295 cv_wait(&cryptoret_cv
, &crypto_mtx
);
1299 mutex_spin_exit(&crypto_mtx
);
1302 #ifdef CRYPTO_TIMING
1303 if (crypto_timing
) {
1305 * NB: We must copy the timestamp before
1306 * doing the callback as the cryptop is
1307 * likely to be reclaimed.
1309 struct timespec t
= crp
->crp_tstamp
;
1310 crypto_tstat(&cryptostats
.cs_cb
, &t
);
1311 crp
->crp_callback(crp
);
1312 crypto_tstat(&cryptostats
.cs_finis
, &t
);
1316 crp
->crp_callback(crp
);
1320 krp
->krp_callback(krp
);
1322 mutex_spin_enter(&crypto_mtx
);