2 * Linux port done by David McCullough <david_mccullough@securecomputing.com>
3 * Copyright (C) 2006-2007 David McCullough
4 * Copyright (C) 2004-2005 Intel Corporation.
5 * The license and original author are listed below.
7 * Redistribution and use in source and binary forms, with or without
8 * Copyright (c) 2002-2006 Sam Leffler. All rights reserved.
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.27 2007/03/21 03:42:51 sam Exp $");
36 * Cryptographic Subsystem.
38 * This code is derived from the Openbsd Cryptographic Framework (OCF)
39 * that has the copyright shown below. Very little of the original
43 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
45 * This code was written by Angelos D. Keromytis in Athens, Greece, in
46 * February 2000. Network Security Technologies Inc. (NSTI) kindly
47 * supported the development of this code.
49 * Copyright (c) 2000, 2001 Angelos D. Keromytis
51 * Permission to use, copy, and modify this software with or without fee
52 * is hereby granted, provided that this entire notice is included in
53 * all source code copies of any software which is or includes a copy or
54 * modification of this software.
56 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
57 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
58 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
59 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
62 __FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.16 2005/01/07 02:29:16 imp Exp $");
66 #ifndef AUTOCONF_INCLUDED
67 #include <linux/config.h>
69 #include <linux/module.h>
70 #include <linux/init.h>
71 #include <linux/list.h>
72 #include <linux/slab.h>
73 #include <linux/wait.h>
74 #include <linux/sched.h>
75 #include <linux/spinlock.h>
76 #include <linux/version.h>
77 #include <cryptodev.h>
80 * keep track of whether or not we have been initialised, a big
81 * issue if we are linked into the kernel and a driver gets started before
84 static int crypto_initted
= 0;
87 * Crypto drivers register themselves by allocating a slot in the
88 * crypto_drivers table with crypto_get_driverid() and then registering
89 * each algorithm they support with crypto_register() and crypto_kregister().
93 * lock on driver table
94 * we track its state as spin_is_locked does not do anything on non-SMP boxes
96 static spinlock_t crypto_drivers_lock
;
97 static int crypto_drivers_locked
; /* for non-SMP boxes */
99 #define CRYPTO_DRIVER_LOCK() \
101 spin_lock_irqsave(&crypto_drivers_lock, d_flags); \
102 crypto_drivers_locked = 1; \
103 dprintk("%s,%d: DRIVER_LOCK()\n", __FILE__, __LINE__); \
105 #define CRYPTO_DRIVER_UNLOCK() \
107 dprintk("%s,%d: DRIVER_UNLOCK()\n", __FILE__, __LINE__); \
108 crypto_drivers_locked = 0; \
109 spin_unlock_irqrestore(&crypto_drivers_lock, d_flags); \
111 #define CRYPTO_DRIVER_ASSERT() \
113 if (!crypto_drivers_locked) { \
114 dprintk("%s,%d: DRIVER_ASSERT!\n", __FILE__, __LINE__); \
119 * Crypto device/driver capabilities structure.
122 * (d) - protected by CRYPTO_DRIVER_LOCK()
123 * (q) - protected by CRYPTO_Q_LOCK()
124 * Not tagged fields are read-only.
127 device_t cc_dev
; /* (d) device/driver */
128 u_int32_t cc_sessions
; /* (d) # of sessions */
129 u_int32_t cc_koperations
; /* (d) # os asym operations */
131 * Largest possible operator length (in bits) for each type of
132 * encryption algorithm. XXX not used
134 u_int16_t cc_max_op_len
[CRYPTO_ALGORITHM_MAX
+ 1];
135 u_int8_t cc_alg
[CRYPTO_ALGORITHM_MAX
+ 1];
136 u_int8_t cc_kalg
[CRK_ALGORITHM_MAX
+ 1];
138 int cc_flags
; /* (d) flags */
139 #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
140 int cc_qblocked
; /* (q) symmetric q blocked */
141 int cc_kqblocked
; /* (q) asymmetric q blocked */
143 static struct cryptocap
*crypto_drivers
= NULL
;
144 static int crypto_drivers_num
= 0;
147 * There are two queues for crypto requests; one for symmetric (e.g.
148 * cipher) operations and one for asymmetric (e.g. MOD)operations.
149 * A single mutex is used to lock access to both queues. We could
150 * have one per-queue but having one simplifies handling of block/unblock
153 static int crp_sleep
= 0;
154 static LIST_HEAD(crp_q
); /* request queues */
155 static LIST_HEAD(crp_kq
);
157 static spinlock_t crypto_q_lock
;
159 int crypto_all_qblocked
= 0; /* protect with Q_LOCK */
160 module_param(crypto_all_qblocked
, int, 0444);
161 MODULE_PARM_DESC(crypto_all_qblocked
, "Are all crypto queues blocked");
163 int crypto_all_kqblocked
= 0; /* protect with Q_LOCK */
164 module_param(crypto_all_kqblocked
, int, 0444);
165 MODULE_PARM_DESC(crypto_all_kqblocked
, "Are all asym crypto queues blocked");
167 #define CRYPTO_Q_LOCK() \
169 spin_lock_irqsave(&crypto_q_lock, q_flags); \
170 dprintk("%s,%d: Q_LOCK()\n", __FILE__, __LINE__); \
172 #define CRYPTO_Q_UNLOCK() \
174 dprintk("%s,%d: Q_UNLOCK()\n", __FILE__, __LINE__); \
175 spin_unlock_irqrestore(&crypto_q_lock, q_flags); \
179 * There are two queues for processing completed crypto requests; one
180 * for the symmetric and one for the asymmetric ops. We only need one
181 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
182 * mutex is used to lock access to both queues. Note that this lock
183 * must be separate from the lock on request queues to insure driver
184 * callbacks don't generate lock order reversals.
186 static LIST_HEAD(crp_ret_q
); /* callback queues */
187 static LIST_HEAD(crp_ret_kq
);
189 static spinlock_t crypto_ret_q_lock
;
190 #define CRYPTO_RETQ_LOCK() \
192 spin_lock_irqsave(&crypto_ret_q_lock, r_flags); \
193 dprintk("%s,%d: RETQ_LOCK\n", __FILE__, __LINE__); \
195 #define CRYPTO_RETQ_UNLOCK() \
197 dprintk("%s,%d: RETQ_UNLOCK\n", __FILE__, __LINE__); \
198 spin_unlock_irqrestore(&crypto_ret_q_lock, r_flags); \
200 #define CRYPTO_RETQ_EMPTY() (list_empty(&crp_ret_q) && list_empty(&crp_ret_kq))
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
203 static kmem_cache_t
*cryptop_zone
;
204 static kmem_cache_t
*cryptodesc_zone
;
206 static struct kmem_cache
*cryptop_zone
;
207 static struct kmem_cache
*cryptodesc_zone
;
210 #define debug crypto_debug
211 int crypto_debug
= 0;
212 module_param(crypto_debug
, int, 0644);
213 MODULE_PARM_DESC(crypto_debug
, "Enable debug");
214 EXPORT_SYMBOL(crypto_debug
);
217 * Maximum number of outstanding crypto requests before we start
218 * failing requests. We need this to prevent DOS when too many
219 * requests are arriving for us to keep up. Otherwise we will
220 * run the system out of memory. Since crypto is slow, we are
221 * usually the bottleneck that needs to say, enough is enough.
223 * We cannot print errors when this condition occurs, we are already too
224 * slow, printing anything will just kill us
227 static int crypto_q_cnt
= 0;
228 module_param(crypto_q_cnt
, int, 0444);
229 MODULE_PARM_DESC(crypto_q_cnt
,
230 "Current number of outstanding crypto requests");
232 static int crypto_q_max
= 1000;
233 module_param(crypto_q_max
, int, 0644);
234 MODULE_PARM_DESC(crypto_q_max
,
235 "Maximum number of outstanding crypto requests");
237 #define bootverbose crypto_verbose
238 static int crypto_verbose
= 0;
239 module_param(crypto_verbose
, int, 0644);
240 MODULE_PARM_DESC(crypto_verbose
,
241 "Enable verbose crypto startup");
243 int crypto_usercrypto
= 1; /* userland may do crypto reqs */
244 module_param(crypto_usercrypto
, int, 0644);
245 MODULE_PARM_DESC(crypto_usercrypto
,
246 "Enable/disable user-mode access to crypto support");
248 int crypto_userasymcrypto
= 1; /* userland may do asym crypto reqs */
249 module_param(crypto_userasymcrypto
, int, 0644);
250 MODULE_PARM_DESC(crypto_userasymcrypto
,
251 "Enable/disable user-mode access to asymmetric crypto support");
253 int crypto_devallowsoft
= 0; /* only use hardware crypto */
254 module_param(crypto_devallowsoft
, int, 0644);
255 MODULE_PARM_DESC(crypto_devallowsoft
,
256 "Enable/disable use of software crypto support");
258 static pid_t cryptoproc
= (pid_t
) -1;
259 static struct completion cryptoproc_exited
;
260 static DECLARE_WAIT_QUEUE_HEAD(cryptoproc_wait
);
261 static pid_t cryptoretproc
= (pid_t
) -1;
262 static struct completion cryptoretproc_exited
;
263 static DECLARE_WAIT_QUEUE_HEAD(cryptoretproc_wait
);
265 static int crypto_proc(void *arg
);
266 static int crypto_ret_proc(void *arg
);
267 static int crypto_invoke(struct cryptocap
*cap
, struct cryptop
*crp
, int hint
);
268 static int crypto_kinvoke(struct cryptkop
*krp
, int flags
);
269 static void crypto_exit(void);
270 static int crypto_init(void);
272 static struct cryptostats cryptostats
;
274 static struct cryptocap
*
275 crypto_checkdriver(u_int32_t hid
)
277 if (crypto_drivers
== NULL
)
279 return (hid
>= crypto_drivers_num
? NULL
: &crypto_drivers
[hid
]);
283 * Compare a driver's list of supported algorithms against another
284 * list; return non-zero if all algorithms are supported.
287 driver_suitable(const struct cryptocap
*cap
, const struct cryptoini
*cri
)
289 const struct cryptoini
*cr
;
291 /* See if all the algorithms are supported. */
292 for (cr
= cri
; cr
; cr
= cr
->cri_next
)
293 if (cap
->cc_alg
[cr
->cri_alg
] == 0)
299 * Select a driver for a new session that supports the specified
300 * algorithms and, optionally, is constrained according to the flags.
301 * The algorithm we use here is pretty stupid; just use the
302 * first driver that supports all the algorithms we need. If there
303 * are multiple drivers we choose the driver with the fewest active
304 * sessions. We prefer hardware-backed drivers to software ones.
306 * XXX We need more smarts here (in real life too, but that's
307 * XXX another story altogether).
309 static struct cryptocap
*
310 crypto_select_driver(const struct cryptoini
*cri
, int flags
)
312 struct cryptocap
*cap
, *best
;
315 CRYPTO_DRIVER_ASSERT();
318 * Look first for hardware crypto devices if permitted.
320 if (flags
& CRYPTOCAP_F_HARDWARE
)
321 match
= CRYPTOCAP_F_HARDWARE
;
323 match
= CRYPTOCAP_F_SOFTWARE
;
326 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
327 cap
= &crypto_drivers
[hid
];
329 * If it's not initialized, is in the process of
330 * going away, or is not appropriate (hardware
331 * or software based on match), then skip.
333 if (cap
->cc_dev
== NULL
||
334 (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
) ||
335 (cap
->cc_flags
& match
) == 0)
338 /* verify all the algorithms are supported. */
339 if (driver_suitable(cap
, cri
)) {
341 cap
->cc_sessions
< best
->cc_sessions
)
347 if (match
== CRYPTOCAP_F_HARDWARE
&& (flags
& CRYPTOCAP_F_SOFTWARE
)) {
348 /* sort of an Algol 68-style for loop */
349 match
= CRYPTOCAP_F_SOFTWARE
;
356 * Create a new session. The crid argument specifies a crypto
357 * driver to use or constraints on a driver to select (hardware
358 * only, software only, either). Whatever driver is selected
359 * must be capable of the requested crypto algorithms.
362 crypto_newsession(u_int64_t
*sid
, struct cryptoini
*cri
, int crid
)
364 struct cryptocap
*cap
;
367 unsigned long d_flags
;
369 CRYPTO_DRIVER_LOCK();
370 if ((crid
& (CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
)) == 0) {
372 * Use specified driver; verify it is capable.
374 cap
= crypto_checkdriver(crid
);
375 if (cap
!= NULL
&& !driver_suitable(cap
, cri
))
379 * No requested driver; select based on crid flags.
381 cap
= crypto_select_driver(cri
, crid
);
383 * if NULL then can't do everything in one session.
384 * XXX Fix this. We need to inject a "virtual" session
385 * XXX layer right about here.
389 /* Call the driver initialization routine. */
390 hid
= cap
- crypto_drivers
;
391 lid
= hid
; /* Pass the driver ID. */
393 CRYPTO_DRIVER_UNLOCK();
394 err
= CRYPTODEV_NEWSESSION(cap
->cc_dev
, &lid
, cri
);
395 CRYPTO_DRIVER_LOCK();
397 (*sid
) = (cap
->cc_flags
& 0xff000000)
398 | (hid
& 0x00ffffff);
400 (*sid
) |= (lid
& 0xffffffff);
405 CRYPTO_DRIVER_UNLOCK();
410 crypto_remove(struct cryptocap
*cap
)
412 CRYPTO_DRIVER_ASSERT();
413 if (cap
->cc_sessions
== 0 && cap
->cc_koperations
== 0)
414 bzero(cap
, sizeof(*cap
));
418 * Delete an existing session (or a reserved session on an unregistered
422 crypto_freesession(u_int64_t sid
)
424 struct cryptocap
*cap
;
427 unsigned long d_flags
;
429 dprintk("%s()\n", __FUNCTION__
);
430 CRYPTO_DRIVER_LOCK();
432 if (crypto_drivers
== NULL
) {
437 /* Determine two IDs. */
438 hid
= CRYPTO_SESID2HID(sid
);
440 if (hid
>= crypto_drivers_num
) {
441 dprintk("%s - INVALID DRIVER NUM %d\n", __FUNCTION__
, hid
);
445 cap
= &crypto_drivers
[hid
];
448 CRYPTO_DRIVER_UNLOCK();
449 /* Call the driver cleanup routine, if available, unlocked. */
450 err
= CRYPTODEV_FREESESSION(cap
->cc_dev
, sid
);
451 CRYPTO_DRIVER_LOCK();
454 if (cap
->cc_sessions
)
457 if (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
)
461 CRYPTO_DRIVER_UNLOCK();
466 * Return an unused driver id. Used by drivers prior to registering
467 * support for the algorithms they handle.
470 crypto_get_driverid(device_t dev
, int flags
)
472 struct cryptocap
*newdrv
;
474 unsigned long d_flags
;
476 if ((flags
& (CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
)) == 0) {
477 printf("%s: no flags specified when registering driver\n",
478 device_get_nameunit(dev
));
482 CRYPTO_DRIVER_LOCK();
484 for (i
= 0; i
< crypto_drivers_num
; i
++) {
485 if (crypto_drivers
[i
].cc_dev
== NULL
&&
486 (crypto_drivers
[i
].cc_flags
& CRYPTOCAP_F_CLEANUP
) == 0) {
491 /* Out of entries, allocate some more. */
492 if (i
== crypto_drivers_num
) {
493 /* Be careful about wrap-around. */
494 if (2 * crypto_drivers_num
<= crypto_drivers_num
) {
495 CRYPTO_DRIVER_UNLOCK();
496 printk("crypto: driver count wraparound!\n");
500 newdrv
= kmalloc(2 * crypto_drivers_num
* sizeof(struct cryptocap
),
502 if (newdrv
== NULL
) {
503 CRYPTO_DRIVER_UNLOCK();
504 printk("crypto: no space to expand driver table!\n");
508 memcpy(newdrv
, crypto_drivers
,
509 crypto_drivers_num
* sizeof(struct cryptocap
));
510 memset(&newdrv
[crypto_drivers_num
], 0,
511 crypto_drivers_num
* sizeof(struct cryptocap
));
513 crypto_drivers_num
*= 2;
515 kfree(crypto_drivers
);
516 crypto_drivers
= newdrv
;
519 /* NB: state is zero'd on free */
520 crypto_drivers
[i
].cc_sessions
= 1; /* Mark */
521 crypto_drivers
[i
].cc_dev
= dev
;
522 crypto_drivers
[i
].cc_flags
= flags
;
524 printf("crypto: assign %s driver id %u, flags %u\n",
525 device_get_nameunit(dev
), i
, flags
);
527 CRYPTO_DRIVER_UNLOCK();
533 * Lookup a driver by name. We match against the full device
534 * name and unit, and against just the name. The latter gives
535 * us a simple widlcarding by device name. On success return the
536 * driver/hardware identifier; otherwise return -1.
539 crypto_find_driver(const char *match
)
541 int i
, len
= strlen(match
);
542 unsigned long d_flags
;
544 CRYPTO_DRIVER_LOCK();
545 for (i
= 0; i
< crypto_drivers_num
; i
++) {
546 device_t dev
= crypto_drivers
[i
].cc_dev
;
548 (crypto_drivers
[i
].cc_flags
& CRYPTOCAP_F_CLEANUP
))
550 if (strncmp(match
, device_get_nameunit(dev
), len
) == 0 ||
551 strncmp(match
, device_get_name(dev
), len
) == 0)
554 CRYPTO_DRIVER_UNLOCK();
555 return i
< crypto_drivers_num
? i
: -1;
559 * Return the device_t for the specified driver or NULL
560 * if the driver identifier is invalid.
563 crypto_find_device_byhid(int hid
)
565 struct cryptocap
*cap
= crypto_checkdriver(hid
);
566 return cap
!= NULL
? cap
->cc_dev
: NULL
;
570 * Return the device/driver capabilities.
573 crypto_getcaps(int hid
)
575 struct cryptocap
*cap
= crypto_checkdriver(hid
);
576 return cap
!= NULL
? cap
->cc_flags
: 0;
580 * Register support for a key-related algorithm. This routine
581 * is called once for each algorithm supported a driver.
584 crypto_kregister(u_int32_t driverid
, int kalg
, u_int32_t flags
)
586 struct cryptocap
*cap
;
588 unsigned long d_flags
;
590 dprintk("%s()\n", __FUNCTION__
);
591 CRYPTO_DRIVER_LOCK();
593 cap
= crypto_checkdriver(driverid
);
595 (CRK_ALGORITM_MIN
<= kalg
&& kalg
<= CRK_ALGORITHM_MAX
)) {
597 * XXX Do some performance testing to determine placing.
598 * XXX We probably need an auxiliary data structure that
599 * XXX describes relative performances.
602 cap
->cc_kalg
[kalg
] = flags
| CRYPTO_ALG_FLAG_SUPPORTED
;
604 printf("crypto: %s registers key alg %u flags %u\n"
605 , device_get_nameunit(cap
->cc_dev
)
613 CRYPTO_DRIVER_UNLOCK();
618 * Register support for a non-key-related algorithm. This routine
619 * is called once for each such algorithm supported by a driver.
622 crypto_register(u_int32_t driverid
, int alg
, u_int16_t maxoplen
,
625 struct cryptocap
*cap
;
627 unsigned long d_flags
;
629 dprintk("%s(id=0x%x, alg=%d, maxoplen=%d, flags=0x%x)\n", __FUNCTION__
,
630 driverid
, alg
, maxoplen
, flags
);
632 CRYPTO_DRIVER_LOCK();
634 cap
= crypto_checkdriver(driverid
);
635 /* NB: algorithms are in the range [1..max] */
637 (CRYPTO_ALGORITHM_MIN
<= alg
&& alg
<= CRYPTO_ALGORITHM_MAX
)) {
639 * XXX Do some performance testing to determine placing.
640 * XXX We probably need an auxiliary data structure that
641 * XXX describes relative performances.
644 cap
->cc_alg
[alg
] = flags
| CRYPTO_ALG_FLAG_SUPPORTED
;
645 cap
->cc_max_op_len
[alg
] = maxoplen
;
647 printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
648 , device_get_nameunit(cap
->cc_dev
)
653 cap
->cc_sessions
= 0; /* Unmark */
658 CRYPTO_DRIVER_UNLOCK();
663 driver_finis(struct cryptocap
*cap
)
667 CRYPTO_DRIVER_ASSERT();
669 ses
= cap
->cc_sessions
;
670 kops
= cap
->cc_koperations
;
671 bzero(cap
, sizeof(*cap
));
672 if (ses
!= 0 || kops
!= 0) {
674 * If there are pending sessions,
675 * just mark as invalid.
677 cap
->cc_flags
|= CRYPTOCAP_F_CLEANUP
;
678 cap
->cc_sessions
= ses
;
679 cap
->cc_koperations
= kops
;
684 * Unregister a crypto driver. If there are pending sessions using it,
685 * leave enough information around so that subsequent calls using those
686 * sessions will correctly detect the driver has been unregistered and
690 crypto_unregister(u_int32_t driverid
, int alg
)
692 struct cryptocap
*cap
;
694 unsigned long d_flags
;
696 dprintk("%s()\n", __FUNCTION__
);
697 CRYPTO_DRIVER_LOCK();
699 cap
= crypto_checkdriver(driverid
);
701 (CRYPTO_ALGORITHM_MIN
<= alg
&& alg
<= CRYPTO_ALGORITHM_MAX
) &&
702 cap
->cc_alg
[alg
] != 0) {
703 cap
->cc_alg
[alg
] = 0;
704 cap
->cc_max_op_len
[alg
] = 0;
706 /* Was this the last algorithm ? */
707 for (i
= 1; i
<= CRYPTO_ALGORITHM_MAX
; i
++)
708 if (cap
->cc_alg
[i
] != 0)
711 if (i
== CRYPTO_ALGORITHM_MAX
+ 1)
716 CRYPTO_DRIVER_UNLOCK();
721 * Unregister all algorithms associated with a crypto driver.
722 * If there are pending sessions using it, leave enough information
723 * around so that subsequent calls using those sessions will
724 * correctly detect the driver has been unregistered and reroute
728 crypto_unregister_all(u_int32_t driverid
)
730 struct cryptocap
*cap
;
732 unsigned long d_flags
;
734 dprintk("%s()\n", __FUNCTION__
);
735 CRYPTO_DRIVER_LOCK();
736 cap
= crypto_checkdriver(driverid
);
742 CRYPTO_DRIVER_UNLOCK();
748 * Clear blockage on a driver. The what parameter indicates whether
749 * the driver is now ready for cryptop's and/or cryptokop's.
752 crypto_unblock(u_int32_t driverid
, int what
)
754 struct cryptocap
*cap
;
756 unsigned long q_flags
;
759 cap
= crypto_checkdriver(driverid
);
761 if (what
& CRYPTO_SYMQ
) {
762 cap
->cc_qblocked
= 0;
763 crypto_all_qblocked
= 0;
765 if (what
& CRYPTO_ASYMQ
) {
766 cap
->cc_kqblocked
= 0;
767 crypto_all_kqblocked
= 0;
770 wake_up_interruptible(&cryptoproc_wait
);
774 CRYPTO_Q_UNLOCK(); //DAVIDM should this be a driver lock
780 * Add a crypto request to a queue, to be processed by the kernel thread.
783 crypto_dispatch(struct cryptop
*crp
)
785 struct cryptocap
*cap
;
787 unsigned long q_flags
;
789 dprintk("%s()\n", __FUNCTION__
);
791 cryptostats
.cs_ops
++;
794 if (crypto_q_cnt
>= crypto_q_max
) {
796 cryptostats
.cs_drops
++;
802 * Caller marked the request to be processed immediately; dispatch
803 * it directly to the driver unless the driver is currently blocked.
805 if ((crp
->crp_flags
& CRYPTO_F_BATCH
) == 0) {
806 int hid
= CRYPTO_SESID2HID(crp
->crp_sid
);
807 cap
= crypto_checkdriver(hid
);
808 /* Driver cannot disappear when there is an active session. */
809 KASSERT(cap
!= NULL
, ("%s: Driver disappeared.", __func__
));
810 if (!cap
->cc_qblocked
) {
811 crypto_all_qblocked
= 0;
812 crypto_drivers
[hid
].cc_qblocked
= 1;
814 result
= crypto_invoke(cap
, crp
, 0);
816 if (result
!= ERESTART
)
817 crypto_drivers
[hid
].cc_qblocked
= 0;
820 if (result
== ERESTART
) {
822 * The driver ran out of resources, mark the
823 * driver ``blocked'' for cryptop's and put
824 * the request back in the queue. It would
825 * best to put the request back where we got
826 * it but that's hard so for now we put it
827 * at the front. This should be ok; putting
828 * it at the end does not work.
830 list_add(&crp
->crp_next
, &crp_q
);
831 cryptostats
.cs_blocks
++;
832 } else if (result
== -1) {
833 TAILQ_INSERT_TAIL(&crp_q
, crp
, crp_next
);
836 wake_up_interruptible(&cryptoproc_wait
);
842 * Add an asymetric crypto request to a queue,
843 * to be processed by the kernel thread.
846 crypto_kdispatch(struct cryptkop
*krp
)
849 unsigned long q_flags
;
851 cryptostats
.cs_kops
++;
853 error
= crypto_kinvoke(krp
, krp
->krp_crid
);
854 if (error
== ERESTART
) {
856 TAILQ_INSERT_TAIL(&crp_kq
, krp
, krp_next
);
858 wake_up_interruptible(&cryptoproc_wait
);
866 * Verify a driver is suitable for the specified operation.
869 kdriver_suitable(const struct cryptocap
*cap
, const struct cryptkop
*krp
)
871 return (cap
->cc_kalg
[krp
->krp_op
] & CRYPTO_ALG_FLAG_SUPPORTED
) != 0;
875 * Select a driver for an asym operation. The driver must
876 * support the necessary algorithm. The caller can constrain
877 * which device is selected with the flags parameter. The
878 * algorithm we use here is pretty stupid; just use the first
879 * driver that supports the algorithms we need. If there are
880 * multiple suitable drivers we choose the driver with the
881 * fewest active operations. We prefer hardware-backed
882 * drivers to software ones when either may be used.
884 static struct cryptocap
*
885 crypto_select_kdriver(const struct cryptkop
*krp
, int flags
)
887 struct cryptocap
*cap
, *best
, *blocked
;
890 CRYPTO_DRIVER_ASSERT();
893 * Look first for hardware crypto devices if permitted.
895 if (flags
& CRYPTOCAP_F_HARDWARE
)
896 match
= CRYPTOCAP_F_HARDWARE
;
898 match
= CRYPTOCAP_F_SOFTWARE
;
902 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
903 cap
= &crypto_drivers
[hid
];
905 * If it's not initialized, is in the process of
906 * going away, or is not appropriate (hardware
907 * or software based on match), then skip.
909 if (cap
->cc_dev
== NULL
||
910 (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
) ||
911 (cap
->cc_flags
& match
) == 0)
914 /* verify all the algorithms are supported. */
915 if (kdriver_suitable(cap
, krp
)) {
917 cap
->cc_koperations
< best
->cc_koperations
)
923 if (match
== CRYPTOCAP_F_HARDWARE
&& (flags
& CRYPTOCAP_F_SOFTWARE
)) {
924 /* sort of an Algol 68-style for loop */
925 match
= CRYPTOCAP_F_SOFTWARE
;
932 * Dispatch an assymetric crypto request.
935 crypto_kinvoke(struct cryptkop
*krp
, int crid
)
937 struct cryptocap
*cap
= NULL
;
939 unsigned long d_flags
;
941 KASSERT(krp
!= NULL
, ("%s: krp == NULL", __func__
));
942 KASSERT(krp
->krp_callback
!= NULL
,
943 ("%s: krp->crp_callback == NULL", __func__
));
945 CRYPTO_DRIVER_LOCK();
946 if ((crid
& (CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
)) == 0) {
947 cap
= crypto_checkdriver(crid
);
950 * Driver present, it must support the necessary
951 * algorithm and, if s/w drivers are excluded,
952 * it must be registered as hardware-backed.
954 if (!kdriver_suitable(cap
, krp
) ||
955 (!crypto_devallowsoft
&&
956 (cap
->cc_flags
& CRYPTOCAP_F_HARDWARE
) == 0))
961 * No requested driver; select based on crid flags.
963 if (!crypto_devallowsoft
) /* NB: disallow s/w drivers */
964 crid
&= ~CRYPTOCAP_F_SOFTWARE
;
965 cap
= crypto_select_kdriver(krp
, crid
);
967 if (cap
!= NULL
&& !cap
->cc_kqblocked
) {
968 krp
->krp_hid
= cap
- crypto_drivers
;
969 cap
->cc_koperations
++;
970 CRYPTO_DRIVER_UNLOCK();
971 error
= CRYPTODEV_KPROCESS(cap
->cc_dev
, krp
, 0);
972 CRYPTO_DRIVER_LOCK();
973 if (error
== ERESTART
) {
974 cap
->cc_koperations
--;
975 CRYPTO_DRIVER_UNLOCK();
978 /* return the actual device used */
979 krp
->krp_crid
= krp
->krp_hid
;
982 * NB: cap is !NULL if device is blocked; in
983 * that case return ERESTART so the operation
984 * is resubmitted if possible.
986 error
= (cap
== NULL
) ? ENODEV
: ERESTART
;
988 CRYPTO_DRIVER_UNLOCK();
991 krp
->krp_status
= error
;
999 * Dispatch a crypto request to the appropriate crypto devices.
1002 crypto_invoke(struct cryptocap
*cap
, struct cryptop
*crp
, int hint
)
1004 KASSERT(crp
!= NULL
, ("%s: crp == NULL", __func__
));
1005 KASSERT(crp
->crp_callback
!= NULL
,
1006 ("%s: crp->crp_callback == NULL", __func__
));
1007 KASSERT(crp
->crp_desc
!= NULL
, ("%s: crp->crp_desc == NULL", __func__
));
1009 dprintk("%s()\n", __FUNCTION__
);
1011 #ifdef CRYPTO_TIMING
1013 crypto_tstat(&cryptostats
.cs_invoke
, &crp
->crp_tstamp
);
1015 if (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
) {
1016 struct cryptodesc
*crd
;
1020 * Driver has unregistered; migrate the session and return
1021 * an error to the caller so they'll resubmit the op.
1023 * XXX: What if there are more already queued requests for this
1026 crypto_freesession(crp
->crp_sid
);
1028 for (crd
= crp
->crp_desc
; crd
->crd_next
; crd
= crd
->crd_next
)
1029 crd
->CRD_INI
.cri_next
= &(crd
->crd_next
->CRD_INI
);
1031 /* XXX propagate flags from initial session? */
1032 if (crypto_newsession(&nid
, &(crp
->crp_desc
->CRD_INI
),
1033 CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
) == 0)
1036 crp
->crp_etype
= EAGAIN
;
1041 * Invoke the driver to process the request.
1043 return CRYPTODEV_PROCESS(cap
->cc_dev
, crp
, hint
);
1048 * Release a set of crypto descriptors.
1051 crypto_freereq(struct cryptop
*crp
)
1053 struct cryptodesc
*crd
;
1060 struct cryptop
*crp2
;
1061 unsigned long q_flags
;
1064 TAILQ_FOREACH(crp2
, &crp_q
, crp_next
) {
1065 KASSERT(crp2
!= crp
,
1066 ("Freeing cryptop from the crypto queue (%p).",
1071 TAILQ_FOREACH(crp2
, &crp_ret_q
, crp_next
) {
1072 KASSERT(crp2
!= crp
,
1073 ("Freeing cryptop from the return queue (%p).",
1076 CRYPTO_RETQ_UNLOCK();
1080 while ((crd
= crp
->crp_desc
) != NULL
) {
1081 crp
->crp_desc
= crd
->crd_next
;
1082 kmem_cache_free(cryptodesc_zone
, crd
);
1084 kmem_cache_free(cryptop_zone
, crp
);
1088 * Acquire a set of crypto descriptors.
1091 crypto_getreq(int num
)
1093 struct cryptodesc
*crd
;
1094 struct cryptop
*crp
;
1096 crp
= kmem_cache_alloc(cryptop_zone
, SLAB_ATOMIC
);
1098 memset(crp
, 0, sizeof(*crp
));
1099 INIT_LIST_HEAD(&crp
->crp_next
);
1100 init_waitqueue_head(&crp
->crp_waitq
);
1102 crd
= kmem_cache_alloc(cryptodesc_zone
, SLAB_ATOMIC
);
1104 crypto_freereq(crp
);
1107 memset(crd
, 0, sizeof(*crd
));
1108 crd
->crd_next
= crp
->crp_desc
;
1109 crp
->crp_desc
= crd
;
1116 * Invoke the callback on behalf of the driver.
1119 crypto_done(struct cryptop
*crp
)
1121 unsigned long q_flags
;
1123 dprintk("%s()\n", __FUNCTION__
);
1124 if ((crp
->crp_flags
& CRYPTO_F_DONE
) == 0) {
1125 crp
->crp_flags
|= CRYPTO_F_DONE
;
1130 printk("crypto: crypto_done op already done, flags 0x%x",
1132 if (crp
->crp_etype
!= 0)
1133 cryptostats
.cs_errs
++;
1135 * CBIMM means unconditionally do the callback immediately;
1136 * CBIFSYNC means do the callback immediately only if the
1137 * operation was done synchronously. Both are used to avoid
1138 * doing extraneous context switches; the latter is mostly
1139 * used with the software crypto driver.
1141 if ((crp
->crp_flags
& CRYPTO_F_CBIMM
) ||
1142 ((crp
->crp_flags
& CRYPTO_F_CBIFSYNC
) &&
1143 (CRYPTO_SESID2CAPS(crp
->crp_sid
) & CRYPTOCAP_F_SYNC
))) {
1145 * Do the callback directly. This is ok when the
1146 * callback routine does very little (e.g. the
1147 * /dev/crypto callback method just does a wakeup).
1149 crp
->crp_callback(crp
);
1151 unsigned long r_flags
;
1153 * Normal case; queue the callback for the thread.
1156 if (CRYPTO_RETQ_EMPTY())
1157 wake_up_interruptible(&cryptoretproc_wait
);/* shared wait channel */
1158 TAILQ_INSERT_TAIL(&crp_ret_q
, crp
, crp_next
);
1159 CRYPTO_RETQ_UNLOCK();
1164 * Invoke the callback on behalf of the driver.
1167 crypto_kdone(struct cryptkop
*krp
)
1169 struct cryptocap
*cap
;
1170 unsigned long d_flags
;
1172 if ((krp
->krp_flags
& CRYPTO_KF_DONE
) != 0)
1173 printk("crypto: crypto_kdone op already done, flags 0x%x",
1175 krp
->krp_flags
|= CRYPTO_KF_DONE
;
1176 if (krp
->krp_status
!= 0)
1177 cryptostats
.cs_kerrs
++;
1179 CRYPTO_DRIVER_LOCK();
1180 /* XXX: What if driver is loaded in the meantime? */
1181 if (krp
->krp_hid
< crypto_drivers_num
) {
1182 cap
= &crypto_drivers
[krp
->krp_hid
];
1183 cap
->cc_koperations
--;
1184 KASSERT(cap
->cc_koperations
>= 0, ("cc_koperations < 0"));
1185 if (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
)
1188 CRYPTO_DRIVER_UNLOCK();
1191 * CBIMM means unconditionally do the callback immediately;
1192 * This is used to avoid doing extraneous context switches
1194 if ((krp
->krp_flags
& CRYPTO_KF_CBIMM
)) {
1196 * Do the callback directly. This is ok when the
1197 * callback routine does very little (e.g. the
1198 * /dev/crypto callback method just does a wakeup).
1200 krp
->krp_callback(krp
);
1202 unsigned long r_flags
;
1204 * Normal case; queue the callback for the thread.
1207 if (CRYPTO_RETQ_EMPTY())
1208 wake_up_interruptible(&cryptoretproc_wait
);/* shared wait channel */
1209 TAILQ_INSERT_TAIL(&crp_ret_kq
, krp
, krp_next
);
1210 CRYPTO_RETQ_UNLOCK();
1215 crypto_getfeat(int *featp
)
1217 int hid
, kalg
, feat
= 0;
1218 unsigned long d_flags
;
1220 CRYPTO_DRIVER_LOCK();
1221 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
1222 const struct cryptocap
*cap
= &crypto_drivers
[hid
];
1224 if ((cap
->cc_flags
& CRYPTOCAP_F_SOFTWARE
) &&
1225 !crypto_devallowsoft
) {
1228 for (kalg
= 0; kalg
< CRK_ALGORITHM_MAX
; kalg
++)
1229 if (cap
->cc_kalg
[kalg
] & CRYPTO_ALG_FLAG_SUPPORTED
)
1232 CRYPTO_DRIVER_UNLOCK();
1238 * Crypto thread, dispatches crypto requests.
1241 crypto_proc(void *arg
)
1243 struct cryptop
*crp
, *submit
;
1244 struct cryptkop
*krp
, *krpp
;
1245 struct cryptocap
*cap
;
1248 unsigned long q_flags
;
1250 ocf_daemonize("crypto");
1255 * we need to make sure we don't get into a busy loop with nothing
1256 * to do, the two crypto_all_*blocked vars help us find out when
1257 * we are all full and can do nothing on any driver or Q. If so we
1258 * wait for an unblock.
1260 crypto_all_qblocked
= !list_empty(&crp_q
);
1263 * Find the first element in the queue that can be
1264 * processed and look-ahead to see if multiple ops
1265 * are ready for the same driver.
1269 list_for_each_entry(crp
, &crp_q
, crp_next
) {
1270 hid
= CRYPTO_SESID2HID(crp
->crp_sid
);
1271 cap
= crypto_checkdriver(hid
);
1273 * Driver cannot disappear when there is an active
1276 KASSERT(cap
!= NULL
, ("%s:%u Driver disappeared.",
1277 __func__
, __LINE__
));
1278 if (cap
== NULL
|| cap
->cc_dev
== NULL
) {
1279 /* Op needs to be migrated, process it. */
1284 if (!cap
->cc_qblocked
) {
1285 if (submit
!= NULL
) {
1287 * We stop on finding another op,
1288 * regardless whether its for the same
1289 * driver or not. We could keep
1290 * searching the queue but it might be
1291 * better to just use a per-driver
1294 if (CRYPTO_SESID2HID(submit
->crp_sid
) == hid
)
1295 hint
= CRYPTO_HINT_MORE
;
1299 if ((submit
->crp_flags
& CRYPTO_F_BATCH
) == 0)
1301 /* keep scanning for more are q'd */
1305 if (submit
!= NULL
) {
1306 hid
= CRYPTO_SESID2HID(submit
->crp_sid
);
1307 crypto_all_qblocked
= 0;
1308 list_del(&submit
->crp_next
);
1309 crypto_drivers
[hid
].cc_qblocked
= 1;
1310 cap
= crypto_checkdriver(hid
);
1312 KASSERT(cap
!= NULL
, ("%s:%u Driver disappeared.",
1313 __func__
, __LINE__
));
1314 result
= crypto_invoke(cap
, submit
, hint
);
1316 if (result
== ERESTART
) {
1318 * The driver ran out of resources, mark the
1319 * driver ``blocked'' for cryptop's and put
1320 * the request back in the queue. It would
1321 * best to put the request back where we got
1322 * it but that's hard so for now we put it
1323 * at the front. This should be ok; putting
1324 * it at the end does not work.
1326 /* XXX validate sid again? */
1327 list_add(&submit
->crp_next
, &crp_q
);
1328 cryptostats
.cs_blocks
++;
1330 crypto_drivers
[hid
].cc_qblocked
=0;
1333 crypto_all_kqblocked
= !list_empty(&crp_kq
);
1335 /* As above, but for key ops */
1337 list_for_each_entry(krpp
, &crp_kq
, krp_next
) {
1338 cap
= crypto_checkdriver(krpp
->krp_hid
);
1339 if (cap
== NULL
|| cap
->cc_dev
== NULL
) {
1341 * Operation needs to be migrated, invalidate
1342 * the assigned device so it will reselect a
1343 * new one below. Propagate the original
1344 * crid selection flags if supplied.
1346 krp
->krp_hid
= krp
->krp_crid
&
1347 (CRYPTOCAP_F_SOFTWARE
|CRYPTOCAP_F_HARDWARE
);
1348 if (krp
->krp_hid
== 0)
1350 CRYPTOCAP_F_SOFTWARE
|CRYPTOCAP_F_HARDWARE
;
1353 if (!cap
->cc_kqblocked
) {
1359 crypto_all_kqblocked
= 0;
1360 list_del(&krp
->krp_next
);
1361 crypto_drivers
[krp
->krp_hid
].cc_kqblocked
= 1;
1363 result
= crypto_kinvoke(krp
, krp
->krp_hid
);
1365 if (result
== ERESTART
) {
1367 * The driver ran out of resources, mark the
1368 * driver ``blocked'' for cryptkop's and put
1369 * the request back in the queue. It would
1370 * best to put the request back where we got
1371 * it but that's hard so for now we put it
1372 * at the front. This should be ok; putting
1373 * it at the end does not work.
1375 /* XXX validate sid again? */
1376 list_add(&krp
->krp_next
, &crp_kq
);
1377 cryptostats
.cs_kblocks
++;
1379 crypto_drivers
[krp
->krp_hid
].cc_kqblocked
= 0;
1382 if (submit
== NULL
&& krp
== NULL
) {
1384 * Nothing more to be processed. Sleep until we're
1385 * woken because there are more ops to process.
1386 * This happens either by submission or by a driver
1387 * becoming unblocked and notifying us through
1388 * crypto_unblock. Note that when we wakeup we
1389 * start processing each queue again from the
1390 * front. It's not clear that it's important to
1391 * preserve this ordering since ops may finish
1392 * out of order if dispatched to different devices
1393 * and some become blocked while others do not.
1395 dprintk("%s - sleeping (qe=%d qb=%d kqe=%d kqb=%d)\n",
1397 list_empty(&crp_q
), crypto_all_qblocked
,
1398 list_empty(&crp_kq
), crypto_all_kqblocked
);
1401 wait_event_interruptible(cryptoproc_wait
,
1402 !(list_empty(&crp_q
) || crypto_all_qblocked
) ||
1403 !(list_empty(&crp_kq
) || crypto_all_kqblocked
) ||
1404 cryptoproc
== (pid_t
) -1);
1406 if (signal_pending (current
)) {
1407 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1408 spin_lock_irq(¤t
->sigmask_lock
);
1410 flush_signals(current
);
1411 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1412 spin_unlock_irq(¤t
->sigmask_lock
);
1416 dprintk("%s - awake\n", __FUNCTION__
);
1417 if (cryptoproc
== (pid_t
) -1)
1419 cryptostats
.cs_intrs
++;
1423 complete_and_exit(&cryptoproc_exited
, 0);
1427 * Crypto returns thread, does callbacks for processed crypto requests.
1428 * Callbacks are done here, rather than in the crypto drivers, because
1429 * callbacks typically are expensive and would slow interrupt handling.
1432 crypto_ret_proc(void *arg
)
1434 struct cryptop
*crpt
;
1435 struct cryptkop
*krpt
;
1436 unsigned long r_flags
;
1438 ocf_daemonize("crypto_ret");
1442 /* Harvest return q's for completed ops */
1444 if (!list_empty(&crp_ret_q
))
1445 crpt
= list_entry(crp_ret_q
.next
, typeof(*crpt
), crp_next
);
1447 list_del(&crpt
->crp_next
);
1450 if (!list_empty(&crp_ret_kq
))
1451 krpt
= list_entry(crp_ret_kq
.next
, typeof(*krpt
), krp_next
);
1453 list_del(&krpt
->krp_next
);
1455 if (crpt
!= NULL
|| krpt
!= NULL
) {
1456 CRYPTO_RETQ_UNLOCK();
1458 * Run callbacks unlocked.
1461 crpt
->crp_callback(crpt
);
1463 krpt
->krp_callback(krpt
);
1467 * Nothing more to be processed. Sleep until we're
1468 * woken because there are more returns to process.
1470 dprintk("%s - sleeping\n", __FUNCTION__
);
1471 CRYPTO_RETQ_UNLOCK();
1472 wait_event_interruptible(cryptoretproc_wait
,
1473 cryptoretproc
== (pid_t
) -1 ||
1474 !list_empty(&crp_ret_q
) ||
1475 !list_empty(&crp_ret_kq
));
1476 if (signal_pending (current
)) {
1477 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1478 spin_lock_irq(¤t
->sigmask_lock
);
1480 flush_signals(current
);
1481 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1482 spin_unlock_irq(¤t
->sigmask_lock
);
1486 dprintk("%s - awake\n", __FUNCTION__
);
1487 if (cryptoretproc
== (pid_t
) -1) {
1488 dprintk("%s - EXITING!\n", __FUNCTION__
);
1491 cryptostats
.cs_rets
++;
1494 CRYPTO_RETQ_UNLOCK();
1495 complete_and_exit(&cryptoretproc_exited
, 0);
1499 #if 0 /* should put this into /proc or something */
1501 db_show_drivers(void)
1505 db_printf("%12s %4s %4s %8s %2s %2s\n"
1513 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
1514 const struct cryptocap
*cap
= &crypto_drivers
[hid
];
1515 if (cap
->cc_dev
== NULL
)
1517 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1518 , device_get_nameunit(cap
->cc_dev
)
1520 , cap
->cc_koperations
1528 DB_SHOW_COMMAND(crypto
, db_show_crypto
)
1530 struct cryptop
*crp
;
1535 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1536 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1537 "Desc", "Callback");
1538 TAILQ_FOREACH(crp
, &crp_q
, crp_next
) {
1539 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1540 , (int) CRYPTO_SESID2HID(crp
->crp_sid
)
1541 , (int) CRYPTO_SESID2CAPS(crp
->crp_sid
)
1542 , crp
->crp_ilen
, crp
->crp_olen
1549 if (!TAILQ_EMPTY(&crp_ret_q
)) {
1550 db_printf("\n%4s %4s %4s %8s\n",
1551 "HID", "Etype", "Flags", "Callback");
1552 TAILQ_FOREACH(crp
, &crp_ret_q
, crp_next
) {
1553 db_printf("%4u %4u %04x %8p\n"
1554 , (int) CRYPTO_SESID2HID(crp
->crp_sid
)
1563 DB_SHOW_COMMAND(kcrypto
, db_show_kcrypto
)
1565 struct cryptkop
*krp
;
1570 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1571 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1572 TAILQ_FOREACH(krp
, &crp_kq
, krp_next
) {
1573 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1576 , krp
->krp_iparams
, krp
->krp_oparams
1577 , krp
->krp_crid
, krp
->krp_hid
1581 if (!TAILQ_EMPTY(&crp_ret_q
)) {
1582 db_printf("%4s %5s %8s %4s %8s\n",
1583 "Op", "Status", "CRID", "HID", "Callback");
1584 TAILQ_FOREACH(krp
, &crp_ret_kq
, krp_next
) {
1585 db_printf("%4u %5u %08x %4u %8p\n"
1588 , krp
->krp_crid
, krp
->krp_hid
1602 dprintk("%s(0x%x)\n", __FUNCTION__
, (int) crypto_init
);
1608 spin_lock_init(&crypto_drivers_lock
);
1609 spin_lock_init(&crypto_q_lock
);
1610 spin_lock_init(&crypto_ret_q_lock
);
1612 cryptop_zone
= kmem_cache_create("cryptop", sizeof(struct cryptop
),
1613 0, SLAB_HWCACHE_ALIGN
, NULL
1614 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1619 cryptodesc_zone
= kmem_cache_create("cryptodesc", sizeof(struct cryptodesc
),
1620 0, SLAB_HWCACHE_ALIGN
, NULL
1621 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1626 if (cryptodesc_zone
== NULL
|| cryptop_zone
== NULL
) {
1627 printk("crypto: crypto_init cannot setup crypto zones\n");
1632 crypto_drivers_num
= CRYPTO_DRIVERS_INITIAL
;
1633 crypto_drivers
= kmalloc(crypto_drivers_num
* sizeof(struct cryptocap
),
1635 if (crypto_drivers
== NULL
) {
1636 printk("crypto: crypto_init cannot setup crypto drivers\n");
1641 memset(crypto_drivers
, 0, crypto_drivers_num
* sizeof(struct cryptocap
));
1643 init_completion(&cryptoproc_exited
);
1644 init_completion(&cryptoretproc_exited
);
1646 cryptoproc
= 0; /* to avoid race condition where proc runs first */
1647 cryptoproc
= kernel_thread(crypto_proc
, NULL
, CLONE_FS
|CLONE_FILES
);
1648 if (cryptoproc
< 0) {
1650 printk("crypto: crypto_init cannot start crypto thread; error %d",
1655 cryptoretproc
= 0; /* to avoid race condition where proc runs first */
1656 cryptoretproc
= kernel_thread(crypto_ret_proc
, NULL
, CLONE_FS
|CLONE_FILES
);
1657 if (cryptoretproc
< 0) {
1658 error
= cryptoretproc
;
1659 printk("crypto: crypto_init cannot start cryptoret thread; error %d",
1675 unsigned long d_flags
;
1677 dprintk("%s()\n", __FUNCTION__
);
1680 * Terminate any crypto threads.
1683 CRYPTO_DRIVER_LOCK();
1685 cryptoproc
= (pid_t
) -1;
1686 kill_proc(p
, SIGTERM
, 1);
1687 wake_up_interruptible(&cryptoproc_wait
);
1688 CRYPTO_DRIVER_UNLOCK();
1690 wait_for_completion(&cryptoproc_exited
);
1692 CRYPTO_DRIVER_LOCK();
1694 cryptoretproc
= (pid_t
) -1;
1695 kill_proc(p
, SIGTERM
, 1);
1696 wake_up_interruptible(&cryptoretproc_wait
);
1697 CRYPTO_DRIVER_UNLOCK();
1699 wait_for_completion(&cryptoretproc_exited
);
1701 /* XXX flush queues??? */
1704 * Reclaim dynamically allocated resources.
1706 if (crypto_drivers
!= NULL
)
1707 kfree(crypto_drivers
);
1709 if (cryptodesc_zone
!= NULL
)
1710 kmem_cache_destroy(cryptodesc_zone
);
1711 if (cryptop_zone
!= NULL
)
1712 kmem_cache_destroy(cryptop_zone
);
1716 EXPORT_SYMBOL(crypto_newsession
);
1717 EXPORT_SYMBOL(crypto_freesession
);
1718 EXPORT_SYMBOL(crypto_get_driverid
);
1719 EXPORT_SYMBOL(crypto_kregister
);
1720 EXPORT_SYMBOL(crypto_register
);
1721 EXPORT_SYMBOL(crypto_unregister
);
1722 EXPORT_SYMBOL(crypto_unregister_all
);
1723 EXPORT_SYMBOL(crypto_unblock
);
1724 EXPORT_SYMBOL(crypto_dispatch
);
1725 EXPORT_SYMBOL(crypto_kdispatch
);
1726 EXPORT_SYMBOL(crypto_freereq
);
1727 EXPORT_SYMBOL(crypto_getreq
);
1728 EXPORT_SYMBOL(crypto_done
);
1729 EXPORT_SYMBOL(crypto_kdone
);
1730 EXPORT_SYMBOL(crypto_getfeat
);
1731 EXPORT_SYMBOL(crypto_userasymcrypto
);
1732 EXPORT_SYMBOL(crypto_getcaps
);
1733 EXPORT_SYMBOL(crypto_find_driver
);
1734 EXPORT_SYMBOL(crypto_find_device_byhid
);
1736 module_init(crypto_init
);
1737 module_exit(crypto_exit
);
1739 MODULE_LICENSE("BSD");
1740 MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
1741 MODULE_DESCRIPTION("OCF (OpenBSD Cryptographic Framework)");