2 * Linux port done by David McCullough <david_mccullough@mcafee.com>
3 * Copyright (C) 2006-2010 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 int cc_unqblocked; /* (q) symmetric q blocked */
144 int cc_unkqblocked; /* (q) asymmetric q blocked */
146 static struct cryptocap *crypto_drivers = NULL;
147 static int crypto_drivers_num = 0;
150 * There are two queues for crypto requests; one for symmetric (e.g.
151 * cipher) operations and one for asymmetric (e.g. MOD)operations.
152 * A single mutex is used to lock access to both queues. We could
153 * have one per-queue but having one simplifies handling of block/unblock
156 static int crp_sleep = 0;
157 static LIST_HEAD(crp_q); /* request queues */
158 static LIST_HEAD(crp_kq);
160 static spinlock_t crypto_q_lock;
162 int crypto_all_qblocked = 0; /* protect with Q_LOCK */
163 module_param(crypto_all_qblocked, int, 0444);
164 MODULE_PARM_DESC(crypto_all_qblocked, "Are all crypto queues blocked");
166 int crypto_all_kqblocked = 0; /* protect with Q_LOCK */
167 module_param(crypto_all_kqblocked, int, 0444);
168 MODULE_PARM_DESC(crypto_all_kqblocked, "Are all asym crypto queues blocked");
170 #define CRYPTO_Q_LOCK() \
172 spin_lock_irqsave(&crypto_q_lock, q_flags); \
173 dprintk("%s,%d: Q_LOCK()\n", __FILE__, __LINE__); \
175 #define CRYPTO_Q_UNLOCK() \
177 dprintk("%s,%d: Q_UNLOCK()\n", __FILE__, __LINE__); \
178 spin_unlock_irqrestore(&crypto_q_lock, q_flags); \
182 * There are two queues for processing completed crypto requests; one
183 * for the symmetric and one for the asymmetric ops. We only need one
184 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
185 * mutex is used to lock access to both queues. Note that this lock
186 * must be separate from the lock on request queues to insure driver
187 * callbacks don't generate lock order reversals.
189 static LIST_HEAD(crp_ret_q); /* callback queues */
190 static LIST_HEAD(crp_ret_kq);
192 static spinlock_t crypto_ret_q_lock;
193 #define CRYPTO_RETQ_LOCK() \
195 spin_lock_irqsave(&crypto_ret_q_lock, r_flags); \
196 dprintk("%s,%d: RETQ_LOCK\n", __FILE__, __LINE__); \
198 #define CRYPTO_RETQ_UNLOCK() \
200 dprintk("%s,%d: RETQ_UNLOCK\n", __FILE__, __LINE__); \
201 spin_unlock_irqrestore(&crypto_ret_q_lock, r_flags); \
203 #define CRYPTO_RETQ_EMPTY() (list_empty(&crp_ret_q) && list_empty(&crp_ret_kq))
205 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
206 static kmem_cache_t *cryptop_zone;
207 static kmem_cache_t *cryptodesc_zone;
209 static struct kmem_cache *cryptop_zone;
210 static struct kmem_cache *cryptodesc_zone;
213 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
214 #include <linux/sched.h>
215 #define kill_proc(p,s,v) send_sig(s,find_task_by_vpid(p),0)
218 #define debug crypto_debug
219 int crypto_debug = 0;
220 module_param(crypto_debug, int, 0644);
221 MODULE_PARM_DESC(crypto_debug, "Enable debug");
222 EXPORT_SYMBOL(crypto_debug);
225 * Maximum number of outstanding crypto requests before we start
226 * failing requests. We need this to prevent DOS when too many
227 * requests are arriving for us to keep up. Otherwise we will
228 * run the system out of memory. Since crypto is slow, we are
229 * usually the bottleneck that needs to say, enough is enough.
231 * We cannot print errors when this condition occurs, we are already too
232 * slow, printing anything will just kill us
235 static int crypto_q_cnt = 0;
236 module_param(crypto_q_cnt, int, 0444);
237 MODULE_PARM_DESC(crypto_q_cnt,
238 "Current number of outstanding crypto requests");
240 static int crypto_q_max = 1000;
241 module_param(crypto_q_max, int, 0644);
242 MODULE_PARM_DESC(crypto_q_max,
243 "Maximum number of outstanding crypto requests");
245 #define bootverbose crypto_verbose
246 static int crypto_verbose = 0;
247 module_param(crypto_verbose, int, 0644);
248 MODULE_PARM_DESC(crypto_verbose,
249 "Enable verbose crypto startup");
251 int crypto_usercrypto = 1; /* userland may do crypto reqs */
252 module_param(crypto_usercrypto, int, 0644);
253 MODULE_PARM_DESC(crypto_usercrypto,
254 "Enable/disable user-mode access to crypto support");
256 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
257 module_param(crypto_userasymcrypto, int, 0644);
258 MODULE_PARM_DESC(crypto_userasymcrypto,
259 "Enable/disable user-mode access to asymmetric crypto support");
261 int crypto_devallowsoft = 0; /* only use hardware crypto */
262 module_param(crypto_devallowsoft, int, 0644);
263 MODULE_PARM_DESC(crypto_devallowsoft,
264 "Enable/disable use of software crypto support");
267 * This parameter controls the maximum number of crypto operations to
268 * do consecutively in the crypto kernel thread before scheduling to allow
269 * other processes to run. Without it, it is possible to get into a
270 * situation where the crypto thread never allows any other processes to run.
271 * Default to 1000 which should be less than one second.
273 static int crypto_max_loopcount = 1000;
274 module_param(crypto_max_loopcount, int, 0644);
275 MODULE_PARM_DESC(crypto_max_loopcount,
276 "Maximum number of crypto ops to do before yielding to other processes");
278 static pid_t cryptoproc = (pid_t) -1;
279 static struct completion cryptoproc_exited;
280 static DECLARE_WAIT_QUEUE_HEAD(cryptoproc_wait);
281 static pid_t cryptoretproc = (pid_t) -1;
282 static struct completion cryptoretproc_exited;
283 static DECLARE_WAIT_QUEUE_HEAD(cryptoretproc_wait);
285 static int crypto_proc(void *arg);
286 static int crypto_ret_proc(void *arg);
287 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
288 static int crypto_kinvoke(struct cryptkop *krp, int flags);
289 static void crypto_exit(void);
290 static int crypto_init(void);
292 static struct cryptostats cryptostats;
294 static struct cryptocap *
295 crypto_checkdriver(u_int32_t hid)
297 if (crypto_drivers == NULL)
299 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
303 * Compare a driver's list of supported algorithms against another
304 * list; return non-zero if all algorithms are supported.
307 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
309 const struct cryptoini *cr;
311 /* See if all the algorithms are supported. */
312 for (cr = cri; cr; cr = cr->cri_next)
313 if (cap->cc_alg[cr->cri_alg] == 0)
319 * Select a driver for a new session that supports the specified
320 * algorithms and, optionally, is constrained according to the flags.
321 * The algorithm we use here is pretty stupid; just use the
322 * first driver that supports all the algorithms we need. If there
323 * are multiple drivers we choose the driver with the fewest active
324 * sessions. We prefer hardware-backed drivers to software ones.
326 * XXX We need more smarts here (in real life too, but that's
327 * XXX another story altogether).
329 static struct cryptocap *
330 crypto_select_driver(const struct cryptoini *cri, int flags)
332 struct cryptocap *cap, *best;
335 CRYPTO_DRIVER_ASSERT();
338 * Look first for hardware crypto devices if permitted.
340 if (flags & CRYPTOCAP_F_HARDWARE)
341 match = CRYPTOCAP_F_HARDWARE;
343 match = CRYPTOCAP_F_SOFTWARE;
346 for (hid = 0; hid < crypto_drivers_num; hid++) {
347 cap = &crypto_drivers[hid];
349 * If it's not initialized, is in the process of
350 * going away, or is not appropriate (hardware
351 * or software based on match), then skip.
353 if (cap->cc_dev == NULL ||
354 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
355 (cap->cc_flags & match) == 0)
358 /* verify all the algorithms are supported. */
359 if (driver_suitable(cap, cri)) {
361 cap->cc_sessions < best->cc_sessions)
367 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
368 /* sort of an Algol 68-style for loop */
369 match = CRYPTOCAP_F_SOFTWARE;
376 * Create a new session. The crid argument specifies a crypto
377 * driver to use or constraints on a driver to select (hardware
378 * only, software only, either). Whatever driver is selected
379 * must be capable of the requested crypto algorithms.
382 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
384 struct cryptocap *cap;
387 unsigned long d_flags;
389 CRYPTO_DRIVER_LOCK();
390 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
392 * Use specified driver; verify it is capable.
394 cap = crypto_checkdriver(crid);
395 if (cap != NULL && !driver_suitable(cap, cri))
399 * No requested driver; select based on crid flags.
401 cap = crypto_select_driver(cri, crid);
403 * if NULL then can't do everything in one session.
404 * XXX Fix this. We need to inject a "virtual" session
405 * XXX layer right about here.
409 /* Call the driver initialization routine. */
410 hid = cap - crypto_drivers;
411 lid = hid; /* Pass the driver ID. */
413 CRYPTO_DRIVER_UNLOCK();
414 err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
415 CRYPTO_DRIVER_LOCK();
417 (*sid) = (cap->cc_flags & 0xff000000)
418 | (hid & 0x00ffffff);
420 (*sid) |= (lid & 0xffffffff);
425 CRYPTO_DRIVER_UNLOCK();
430 crypto_remove(struct cryptocap *cap)
432 CRYPTO_DRIVER_ASSERT();
433 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
434 bzero(cap, sizeof(*cap));
438 * Delete an existing session (or a reserved session on an unregistered
442 crypto_freesession(u_int64_t sid)
444 struct cryptocap *cap;
447 unsigned long d_flags;
449 dprintk("%s()\n", __FUNCTION__);
450 CRYPTO_DRIVER_LOCK();
452 if (crypto_drivers == NULL) {
457 /* Determine two IDs. */
458 hid = CRYPTO_SESID2HID(sid);
460 if (hid >= crypto_drivers_num) {
461 dprintk("%s - INVALID DRIVER NUM %d\n", __FUNCTION__, hid);
465 cap = &crypto_drivers[hid];
468 CRYPTO_DRIVER_UNLOCK();
469 /* Call the driver cleanup routine, if available, unlocked. */
470 err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
471 CRYPTO_DRIVER_LOCK();
474 if (cap->cc_sessions)
477 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
481 CRYPTO_DRIVER_UNLOCK();
486 * Return an unused driver id. Used by drivers prior to registering
487 * support for the algorithms they handle.
490 crypto_get_driverid(device_t dev, int flags)
492 struct cryptocap *newdrv;
494 unsigned long d_flags;
496 if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
497 printf("%s: no flags specified when registering driver\n",
498 device_get_nameunit(dev));
502 CRYPTO_DRIVER_LOCK();
504 for (i = 0; i < crypto_drivers_num; i++) {
505 if (crypto_drivers[i].cc_dev == NULL &&
506 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
511 /* Out of entries, allocate some more. */
512 if (i == crypto_drivers_num) {
513 /* Be careful about wrap-around. */
514 if (2 * crypto_drivers_num <= crypto_drivers_num) {
515 CRYPTO_DRIVER_UNLOCK();
516 printk("crypto: driver count wraparound!\n");
520 newdrv = kmalloc(2 * crypto_drivers_num * sizeof(struct cryptocap),
522 if (newdrv == NULL) {
523 CRYPTO_DRIVER_UNLOCK();
524 printk("crypto: no space to expand driver table!\n");
528 memcpy(newdrv, crypto_drivers,
529 crypto_drivers_num * sizeof(struct cryptocap));
530 memset(&newdrv[crypto_drivers_num], 0,
531 crypto_drivers_num * sizeof(struct cryptocap));
533 crypto_drivers_num *= 2;
535 kfree(crypto_drivers);
536 crypto_drivers = newdrv;
539 /* NB: state is zero'd on free */
540 crypto_drivers[i].cc_sessions = 1; /* Mark */
541 crypto_drivers[i].cc_dev = dev;
542 crypto_drivers[i].cc_flags = flags;
544 printf("crypto: assign %s driver id %u, flags %u\n",
545 device_get_nameunit(dev), i, flags);
547 CRYPTO_DRIVER_UNLOCK();
553 * Lookup a driver by name. We match against the full device
554 * name and unit, and against just the name. The latter gives
555 * us a simple widlcarding by device name. On success return the
556 * driver/hardware identifier; otherwise return -1.
559 crypto_find_driver(const char *match)
561 int i, len = strlen(match);
562 unsigned long d_flags;
564 CRYPTO_DRIVER_LOCK();
565 for (i = 0; i < crypto_drivers_num; i++) {
566 device_t dev = crypto_drivers[i].cc_dev;
568 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
570 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
571 strncmp(match, device_get_name(dev), len) == 0)
574 CRYPTO_DRIVER_UNLOCK();
575 return i < crypto_drivers_num ? i : -1;
579 * Return the device_t for the specified driver or NULL
580 * if the driver identifier is invalid.
583 crypto_find_device_byhid(int hid)
585 struct cryptocap *cap = crypto_checkdriver(hid);
586 return cap != NULL ? cap->cc_dev : NULL;
590 * Return the device/driver capabilities.
593 crypto_getcaps(int hid)
595 struct cryptocap *cap = crypto_checkdriver(hid);
596 return cap != NULL ? cap->cc_flags : 0;
600 * Register support for a key-related algorithm. This routine
601 * is called once for each algorithm supported a driver.
604 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
606 struct cryptocap *cap;
608 unsigned long d_flags;
610 dprintk("%s()\n", __FUNCTION__);
611 CRYPTO_DRIVER_LOCK();
613 cap = crypto_checkdriver(driverid);
615 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
617 * XXX Do some performance testing to determine placing.
618 * XXX We probably need an auxiliary data structure that
619 * XXX describes relative performances.
622 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
624 printf("crypto: %s registers key alg %u flags %u\n"
625 , device_get_nameunit(cap->cc_dev)
633 CRYPTO_DRIVER_UNLOCK();
638 * Register support for a non-key-related algorithm. This routine
639 * is called once for each such algorithm supported by a driver.
642 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
645 struct cryptocap *cap;
647 unsigned long d_flags;
649 dprintk("%s(id=0x%x, alg=%d, maxoplen=%d, flags=0x%x)\n", __FUNCTION__,
650 driverid, alg, maxoplen, flags);
652 CRYPTO_DRIVER_LOCK();
654 cap = crypto_checkdriver(driverid);
655 /* NB: algorithms are in the range [1..max] */
657 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
659 * XXX Do some performance testing to determine placing.
660 * XXX We probably need an auxiliary data structure that
661 * XXX describes relative performances.
664 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
665 cap->cc_max_op_len[alg] = maxoplen;
667 printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
668 , device_get_nameunit(cap->cc_dev)
673 cap->cc_sessions = 0; /* Unmark */
678 CRYPTO_DRIVER_UNLOCK();
683 driver_finis(struct cryptocap *cap)
687 CRYPTO_DRIVER_ASSERT();
689 ses = cap->cc_sessions;
690 kops = cap->cc_koperations;
691 bzero(cap, sizeof(*cap));
692 if (ses != 0 || kops != 0) {
694 * If there are pending sessions,
695 * just mark as invalid.
697 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
698 cap->cc_sessions = ses;
699 cap->cc_koperations = kops;
704 * Unregister a crypto driver. If there are pending sessions using it,
705 * leave enough information around so that subsequent calls using those
706 * sessions will correctly detect the driver has been unregistered and
710 crypto_unregister(u_int32_t driverid, int alg)
712 struct cryptocap *cap;
714 unsigned long d_flags;
716 dprintk("%s()\n", __FUNCTION__);
717 CRYPTO_DRIVER_LOCK();
719 cap = crypto_checkdriver(driverid);
721 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
722 cap->cc_alg[alg] != 0) {
723 cap->cc_alg[alg] = 0;
724 cap->cc_max_op_len[alg] = 0;
726 /* Was this the last algorithm ? */
727 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
728 if (cap->cc_alg[i] != 0)
731 if (i == CRYPTO_ALGORITHM_MAX + 1)
736 CRYPTO_DRIVER_UNLOCK();
741 * Unregister all algorithms associated with a crypto driver.
742 * If there are pending sessions using it, leave enough information
743 * around so that subsequent calls using those sessions will
744 * correctly detect the driver has been unregistered and reroute
748 crypto_unregister_all(u_int32_t driverid)
750 struct cryptocap *cap;
752 unsigned long d_flags;
754 dprintk("%s()\n", __FUNCTION__);
755 CRYPTO_DRIVER_LOCK();
756 cap = crypto_checkdriver(driverid);
762 CRYPTO_DRIVER_UNLOCK();
768 * Clear blockage on a driver. The what parameter indicates whether
769 * the driver is now ready for cryptop's and/or cryptokop's.
772 crypto_unblock(u_int32_t driverid, int what)
774 struct cryptocap *cap;
776 unsigned long q_flags;
779 cap = crypto_checkdriver(driverid);
781 if (what & CRYPTO_SYMQ) {
782 cap->cc_qblocked = 0;
783 cap->cc_unqblocked = 0;
784 crypto_all_qblocked = 0;
786 if (what & CRYPTO_ASYMQ) {
787 cap->cc_kqblocked = 0;
788 cap->cc_unkqblocked = 0;
789 crypto_all_kqblocked = 0;
792 wake_up_interruptible(&cryptoproc_wait);
796 CRYPTO_Q_UNLOCK(); //DAVIDM should this be a driver lock
802 * Add a crypto request to a queue, to be processed by the kernel thread.
805 crypto_dispatch(struct cryptop *crp)
807 struct cryptocap *cap;
809 unsigned long q_flags;
811 dprintk("%s()\n", __FUNCTION__);
813 cryptostats.cs_ops++;
816 if (crypto_q_cnt >= crypto_q_max) {
818 cryptostats.cs_drops++;
823 /* make sure we are starting a fresh run on this crp. */
824 crp->crp_flags &= ~CRYPTO_F_DONE;
828 * Caller marked the request to be processed immediately; dispatch
829 * it directly to the driver unless the driver is currently blocked.
831 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
832 int hid = CRYPTO_SESID2HID(crp->crp_sid);
833 cap = crypto_checkdriver(hid);
834 /* Driver cannot disappear when there is an active session. */
835 KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
836 if (!cap->cc_qblocked) {
837 crypto_all_qblocked = 0;
838 crypto_drivers[hid].cc_unqblocked = 1;
840 result = crypto_invoke(cap, crp, 0);
842 if (result == ERESTART)
843 if (crypto_drivers[hid].cc_unqblocked)
844 crypto_drivers[hid].cc_qblocked = 1;
845 crypto_drivers[hid].cc_unqblocked = 0;
848 if (result == ERESTART) {
850 * The driver ran out of resources, mark the
851 * driver ``blocked'' for cryptop's and put
852 * the request back in the queue. It would
853 * best to put the request back where we got
854 * it but that's hard so for now we put it
855 * at the front. This should be ok; putting
856 * it at the end does not work.
858 list_add(&crp->crp_next, &crp_q);
859 cryptostats.cs_blocks++;
861 } else if (result == -1) {
862 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
866 wake_up_interruptible(&cryptoproc_wait);
872 * Add an asymetric crypto request to a queue,
873 * to be processed by the kernel thread.
876 crypto_kdispatch(struct cryptkop *krp)
879 unsigned long q_flags;
881 cryptostats.cs_kops++;
883 error = crypto_kinvoke(krp, krp->krp_crid);
884 if (error == ERESTART) {
886 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
888 wake_up_interruptible(&cryptoproc_wait);
896 * Verify a driver is suitable for the specified operation.
899 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
901 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
905 * Select a driver for an asym operation. The driver must
906 * support the necessary algorithm. The caller can constrain
907 * which device is selected with the flags parameter. The
908 * algorithm we use here is pretty stupid; just use the first
909 * driver that supports the algorithms we need. If there are
910 * multiple suitable drivers we choose the driver with the
911 * fewest active operations. We prefer hardware-backed
912 * drivers to software ones when either may be used.
914 static struct cryptocap *
915 crypto_select_kdriver(const struct cryptkop *krp, int flags)
917 struct cryptocap *cap, *best, *blocked;
920 CRYPTO_DRIVER_ASSERT();
923 * Look first for hardware crypto devices if permitted.
925 if (flags & CRYPTOCAP_F_HARDWARE)
926 match = CRYPTOCAP_F_HARDWARE;
928 match = CRYPTOCAP_F_SOFTWARE;
932 for (hid = 0; hid < crypto_drivers_num; hid++) {
933 cap = &crypto_drivers[hid];
935 * If it's not initialized, is in the process of
936 * going away, or is not appropriate (hardware
937 * or software based on match), then skip.
939 if (cap->cc_dev == NULL ||
940 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
941 (cap->cc_flags & match) == 0)
944 /* verify all the algorithms are supported. */
945 if (kdriver_suitable(cap, krp)) {
947 cap->cc_koperations < best->cc_koperations)
953 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
954 /* sort of an Algol 68-style for loop */
955 match = CRYPTOCAP_F_SOFTWARE;
962 * Dispatch an assymetric crypto request.
965 crypto_kinvoke(struct cryptkop *krp, int crid)
967 struct cryptocap *cap = NULL;
969 unsigned long d_flags;
971 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
972 KASSERT(krp->krp_callback != NULL,
973 ("%s: krp->crp_callback == NULL", __func__));
975 CRYPTO_DRIVER_LOCK();
976 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
977 cap = crypto_checkdriver(crid);
980 * Driver present, it must support the necessary
981 * algorithm and, if s/w drivers are excluded,
982 * it must be registered as hardware-backed.
984 if (!kdriver_suitable(cap, krp) ||
985 (!crypto_devallowsoft &&
986 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
991 * No requested driver; select based on crid flags.
993 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
994 crid &= ~CRYPTOCAP_F_SOFTWARE;
995 cap = crypto_select_kdriver(krp, crid);
997 if (cap != NULL && !cap->cc_kqblocked) {
998 krp->krp_hid = cap - crypto_drivers;
999 cap->cc_koperations++;
1000 CRYPTO_DRIVER_UNLOCK();
1001 error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
1002 CRYPTO_DRIVER_LOCK();
1003 if (error == ERESTART) {
1004 cap->cc_koperations--;
1005 CRYPTO_DRIVER_UNLOCK();
1008 /* return the actual device used */
1009 krp->krp_crid = krp->krp_hid;
1012 * NB: cap is !NULL if device is blocked; in
1013 * that case return ERESTART so the operation
1014 * is resubmitted if possible.
1016 error = (cap == NULL) ? ENODEV : ERESTART;
1018 CRYPTO_DRIVER_UNLOCK();
1021 krp->krp_status = error;
1029 * Dispatch a crypto request to the appropriate crypto devices.
1032 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1034 KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
1035 KASSERT(crp->crp_callback != NULL,
1036 ("%s: crp->crp_callback == NULL", __func__));
1037 KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
1039 dprintk("%s()\n", __FUNCTION__);
1041 #ifdef CRYPTO_TIMING
1043 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1045 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1046 struct cryptodesc *crd;
1050 * Driver has unregistered; migrate the session and return
1051 * an error to the caller so they'll resubmit the op.
1053 * XXX: What if there are more already queued requests for this
1056 crypto_freesession(crp->crp_sid);
1058 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1059 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1061 /* XXX propagate flags from initial session? */
1062 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
1063 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
1066 crp->crp_etype = EAGAIN;
1071 * Invoke the driver to process the request.
1073 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1078 * Release a set of crypto descriptors.
1081 crypto_freereq(struct cryptop *crp)
1083 struct cryptodesc *crd;
1090 struct cryptop *crp2;
1091 unsigned long q_flags;
1094 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1095 KASSERT(crp2 != crp,
1096 ("Freeing cryptop from the crypto queue (%p).",
1101 TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
1102 KASSERT(crp2 != crp,
1103 ("Freeing cryptop from the return queue (%p).",
1106 CRYPTO_RETQ_UNLOCK();
1110 while ((crd = crp->crp_desc) != NULL) {
1111 crp->crp_desc = crd->crd_next;
1112 kmem_cache_free(cryptodesc_zone, crd);
1114 kmem_cache_free(cryptop_zone, crp);
1118 * Acquire a set of crypto descriptors.
1121 crypto_getreq(int num)
1123 struct cryptodesc *crd;
1124 struct cryptop *crp;
1126 crp = kmem_cache_alloc(cryptop_zone, SLAB_ATOMIC);
1128 memset(crp, 0, sizeof(*crp));
1129 INIT_LIST_HEAD(&crp->crp_next);
1130 init_waitqueue_head(&crp->crp_waitq);
1132 crd = kmem_cache_alloc(cryptodesc_zone, SLAB_ATOMIC);
1134 crypto_freereq(crp);
1137 memset(crd, 0, sizeof(*crd));
1138 crd->crd_next = crp->crp_desc;
1139 crp->crp_desc = crd;
1146 * Invoke the callback on behalf of the driver.
1149 crypto_done(struct cryptop *crp)
1151 unsigned long q_flags;
1153 dprintk("%s()\n", __FUNCTION__);
1154 if ((crp->crp_flags & CRYPTO_F_DONE) == 0) {
1155 crp->crp_flags |= CRYPTO_F_DONE;
1160 printk("crypto: crypto_done op already done, flags 0x%x",
1162 if (crp->crp_etype != 0)
1163 cryptostats.cs_errs++;
1165 * CBIMM means unconditionally do the callback immediately;
1166 * CBIFSYNC means do the callback immediately only if the
1167 * operation was done synchronously. Both are used to avoid
1168 * doing extraneous context switches; the latter is mostly
1169 * used with the software crypto driver.
1171 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1172 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1173 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
1175 * Do the callback directly. This is ok when the
1176 * callback routine does very little (e.g. the
1177 * /dev/crypto callback method just does a wakeup).
1179 crp->crp_callback(crp);
1181 unsigned long r_flags;
1183 * Normal case; queue the callback for the thread.
1186 if (CRYPTO_RETQ_EMPTY())
1187 wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
1188 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1189 CRYPTO_RETQ_UNLOCK();
1194 * Invoke the callback on behalf of the driver.
1197 crypto_kdone(struct cryptkop *krp)
1199 struct cryptocap *cap;
1200 unsigned long d_flags;
1202 if ((krp->krp_flags & CRYPTO_KF_DONE) != 0)
1203 printk("crypto: crypto_kdone op already done, flags 0x%x",
1205 krp->krp_flags |= CRYPTO_KF_DONE;
1206 if (krp->krp_status != 0)
1207 cryptostats.cs_kerrs++;
1209 CRYPTO_DRIVER_LOCK();
1210 /* XXX: What if driver is loaded in the meantime? */
1211 if (krp->krp_hid < crypto_drivers_num) {
1212 cap = &crypto_drivers[krp->krp_hid];
1213 cap->cc_koperations--;
1214 KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
1215 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1218 CRYPTO_DRIVER_UNLOCK();
1221 * CBIMM means unconditionally do the callback immediately;
1222 * This is used to avoid doing extraneous context switches
1224 if ((krp->krp_flags & CRYPTO_KF_CBIMM)) {
1226 * Do the callback directly. This is ok when the
1227 * callback routine does very little (e.g. the
1228 * /dev/crypto callback method just does a wakeup).
1230 krp->krp_callback(krp);
1232 unsigned long r_flags;
1234 * Normal case; queue the callback for the thread.
1237 if (CRYPTO_RETQ_EMPTY())
1238 wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
1239 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1240 CRYPTO_RETQ_UNLOCK();
1245 crypto_getfeat(int *featp)
1247 int hid, kalg, feat = 0;
1248 unsigned long d_flags;
1250 CRYPTO_DRIVER_LOCK();
1251 for (hid = 0; hid < crypto_drivers_num; hid++) {
1252 const struct cryptocap *cap = &crypto_drivers[hid];
1254 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1255 !crypto_devallowsoft) {
1258 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1259 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1262 CRYPTO_DRIVER_UNLOCK();
1268 * Crypto thread, dispatches crypto requests.
1271 crypto_proc(void *arg)
1273 struct cryptop *crp, *submit;
1274 struct cryptkop *krp, *krpp;
1275 struct cryptocap *cap;
1278 unsigned long q_flags;
1281 ocf_daemonize("crypto");
1286 * we need to make sure we don't get into a busy loop with nothing
1287 * to do, the two crypto_all_*blocked vars help us find out when
1288 * we are all full and can do nothing on any driver or Q. If so we
1289 * wait for an unblock.
1291 crypto_all_qblocked = !list_empty(&crp_q);
1294 * Find the first element in the queue that can be
1295 * processed and look-ahead to see if multiple ops
1296 * are ready for the same driver.
1300 list_for_each_entry(crp, &crp_q, crp_next) {
1301 hid = CRYPTO_SESID2HID(crp->crp_sid);
1302 cap = crypto_checkdriver(hid);
1304 * Driver cannot disappear when there is an active
1307 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1308 __func__, __LINE__));
1309 if (cap == NULL || cap->cc_dev == NULL) {
1310 /* Op needs to be migrated, process it. */
1315 if (!cap->cc_qblocked) {
1316 if (submit != NULL) {
1318 * We stop on finding another op,
1319 * regardless whether its for the same
1320 * driver or not. We could keep
1321 * searching the queue but it might be
1322 * better to just use a per-driver
1325 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1326 hint = CRYPTO_HINT_MORE;
1330 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1332 /* keep scanning for more are q'd */
1336 if (submit != NULL) {
1337 hid = CRYPTO_SESID2HID(submit->crp_sid);
1338 crypto_all_qblocked = 0;
1339 list_del(&submit->crp_next);
1340 crypto_drivers[hid].cc_unqblocked = 1;
1341 cap = crypto_checkdriver(hid);
1343 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1344 __func__, __LINE__));
1345 result = crypto_invoke(cap, submit, hint);
1347 if (result == ERESTART) {
1349 * The driver ran out of resources, mark the
1350 * driver ``blocked'' for cryptop's and put
1351 * the request back in the queue. It would
1352 * best to put the request back where we got
1353 * it but that's hard so for now we put it
1354 * at the front. This should be ok; putting
1355 * it at the end does not work.
1357 /* XXX validate sid again? */
1358 list_add(&submit->crp_next, &crp_q);
1359 cryptostats.cs_blocks++;
1360 if (crypto_drivers[hid].cc_unqblocked)
1361 crypto_drivers[hid].cc_qblocked=0;
1362 crypto_drivers[hid].cc_unqblocked=0;
1364 crypto_drivers[hid].cc_unqblocked = 0;
1367 crypto_all_kqblocked = !list_empty(&crp_kq);
1369 /* As above, but for key ops */
1371 list_for_each_entry(krpp, &crp_kq, krp_next) {
1372 cap = crypto_checkdriver(krpp->krp_hid);
1373 if (cap == NULL || cap->cc_dev == NULL) {
1375 * Operation needs to be migrated, invalidate
1376 * the assigned device so it will reselect a
1377 * new one below. Propagate the original
1378 * crid selection flags if supplied.
1380 krp->krp_hid = krp->krp_crid &
1381 (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1382 if (krp->krp_hid == 0)
1384 CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1387 if (!cap->cc_kqblocked) {
1393 crypto_all_kqblocked = 0;
1394 list_del(&krp->krp_next);
1395 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1397 result = crypto_kinvoke(krp, krp->krp_hid);
1399 if (result == ERESTART) {
1401 * The driver ran out of resources, mark the
1402 * driver ``blocked'' for cryptkop's and put
1403 * the request back in the queue. It would
1404 * best to put the request back where we got
1405 * it but that's hard so for now we put it
1406 * at the front. This should be ok; putting
1407 * it at the end does not work.
1409 /* XXX validate sid again? */
1410 list_add(&krp->krp_next, &crp_kq);
1411 cryptostats.cs_kblocks++;
1413 crypto_drivers[krp->krp_hid].cc_kqblocked = 0;
1416 if (submit == NULL && krp == NULL) {
1418 * Nothing more to be processed. Sleep until we're
1419 * woken because there are more ops to process.
1420 * This happens either by submission or by a driver
1421 * becoming unblocked and notifying us through
1422 * crypto_unblock. Note that when we wakeup we
1423 * start processing each queue again from the
1424 * front. It's not clear that it's important to
1425 * preserve this ordering since ops may finish
1426 * out of order if dispatched to different devices
1427 * and some become blocked while others do not.
1429 dprintk("%s - sleeping (qe=%d qb=%d kqe=%d kqb=%d)\n",
1431 list_empty(&crp_q), crypto_all_qblocked,
1432 list_empty(&crp_kq), crypto_all_kqblocked);
1436 wait_event_interruptible(cryptoproc_wait,
1437 !(list_empty(&crp_q) || crypto_all_qblocked) ||
1438 !(list_empty(&crp_kq) || crypto_all_kqblocked) ||
1439 cryptoproc == (pid_t) -1);
1441 if (signal_pending (current)) {
1442 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1443 spin_lock_irq(¤t->sigmask_lock);
1445 flush_signals(current);
1446 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1447 spin_unlock_irq(¤t->sigmask_lock);
1451 dprintk("%s - awake\n", __FUNCTION__);
1452 if (cryptoproc == (pid_t) -1)
1454 cryptostats.cs_intrs++;
1455 } else if (loopcount > crypto_max_loopcount) {
1457 * Give other processes a chance to run if we've
1458 * been using the CPU exclusively for a while.
1466 complete_and_exit(&cryptoproc_exited, 0);
1470 * Crypto returns thread, does callbacks for processed crypto requests.
1471 * Callbacks are done here, rather than in the crypto drivers, because
1472 * callbacks typically are expensive and would slow interrupt handling.
1475 crypto_ret_proc(void *arg)
1477 struct cryptop *crpt;
1478 struct cryptkop *krpt;
1479 unsigned long r_flags;
1481 ocf_daemonize("crypto_ret");
1485 /* Harvest return q's for completed ops */
1487 if (!list_empty(&crp_ret_q))
1488 crpt = list_entry(crp_ret_q.next, typeof(*crpt), crp_next);
1490 list_del(&crpt->crp_next);
1493 if (!list_empty(&crp_ret_kq))
1494 krpt = list_entry(crp_ret_kq.next, typeof(*krpt), krp_next);
1496 list_del(&krpt->krp_next);
1498 if (crpt != NULL || krpt != NULL) {
1499 CRYPTO_RETQ_UNLOCK();
1501 * Run callbacks unlocked.
1504 crpt->crp_callback(crpt);
1506 krpt->krp_callback(krpt);
1510 * Nothing more to be processed. Sleep until we're
1511 * woken because there are more returns to process.
1513 dprintk("%s - sleeping\n", __FUNCTION__);
1514 CRYPTO_RETQ_UNLOCK();
1515 wait_event_interruptible(cryptoretproc_wait,
1516 cryptoretproc == (pid_t) -1 ||
1517 !list_empty(&crp_ret_q) ||
1518 !list_empty(&crp_ret_kq));
1519 if (signal_pending (current)) {
1520 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1521 spin_lock_irq(¤t->sigmask_lock);
1523 flush_signals(current);
1524 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1525 spin_unlock_irq(¤t->sigmask_lock);
1529 dprintk("%s - awake\n", __FUNCTION__);
1530 if (cryptoretproc == (pid_t) -1) {
1531 dprintk("%s - EXITING!\n", __FUNCTION__);
1534 cryptostats.cs_rets++;
1537 CRYPTO_RETQ_UNLOCK();
1538 complete_and_exit(&cryptoretproc_exited, 0);
1542 #if 0 /* should put this into /proc or something */
1544 db_show_drivers(void)
1548 db_printf("%12s %4s %4s %8s %2s %2s\n"
1556 for (hid = 0; hid < crypto_drivers_num; hid++) {
1557 const struct cryptocap *cap = &crypto_drivers[hid];
1558 if (cap->cc_dev == NULL)
1560 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1561 , device_get_nameunit(cap->cc_dev)
1563 , cap->cc_koperations
1571 DB_SHOW_COMMAND(crypto, db_show_crypto)
1573 struct cryptop *crp;
1578 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1579 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1580 "Desc", "Callback");
1581 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1582 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1583 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1584 , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
1585 , crp->crp_ilen, crp->crp_olen
1592 if (!TAILQ_EMPTY(&crp_ret_q)) {
1593 db_printf("\n%4s %4s %4s %8s\n",
1594 "HID", "Etype", "Flags", "Callback");
1595 TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
1596 db_printf("%4u %4u %04x %8p\n"
1597 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1606 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1608 struct cryptkop *krp;
1613 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1614 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1615 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1616 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1619 , krp->krp_iparams, krp->krp_oparams
1620 , krp->krp_crid, krp->krp_hid
1624 if (!TAILQ_EMPTY(&crp_ret_q)) {
1625 db_printf("%4s %5s %8s %4s %8s\n",
1626 "Op", "Status", "CRID", "HID", "Callback");
1627 TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
1628 db_printf("%4u %5u %08x %4u %8p\n"
1631 , krp->krp_crid, krp->krp_hid
1645 dprintk("%s(%p)\n", __FUNCTION__, (void *) crypto_init);
1651 spin_lock_init(&crypto_drivers_lock);
1652 spin_lock_init(&crypto_q_lock);
1653 spin_lock_init(&crypto_ret_q_lock);
1655 cryptop_zone = kmem_cache_create("cryptop", sizeof(struct cryptop),
1656 0, SLAB_HWCACHE_ALIGN, NULL
1657 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1662 cryptodesc_zone = kmem_cache_create("cryptodesc", sizeof(struct cryptodesc),
1663 0, SLAB_HWCACHE_ALIGN, NULL
1664 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1669 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
1670 printk("crypto: crypto_init cannot setup crypto zones\n");
1675 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
1676 crypto_drivers = kmalloc(crypto_drivers_num * sizeof(struct cryptocap),
1678 if (crypto_drivers == NULL) {
1679 printk("crypto: crypto_init cannot setup crypto drivers\n");
1684 memset(crypto_drivers, 0, crypto_drivers_num * sizeof(struct cryptocap));
1686 init_completion(&cryptoproc_exited);
1687 init_completion(&cryptoretproc_exited);
1689 cryptoproc = 0; /* to avoid race condition where proc runs first */
1690 cryptoproc = kernel_thread(crypto_proc, NULL, CLONE_FS|CLONE_FILES);
1691 if (cryptoproc < 0) {
1693 printk("crypto: crypto_init cannot start crypto thread; error %d",
1698 cryptoretproc = 0; /* to avoid race condition where proc runs first */
1699 cryptoretproc = kernel_thread(crypto_ret_proc, NULL, CLONE_FS|CLONE_FILES);
1700 if (cryptoretproc < 0) {
1701 error = cryptoretproc;
1702 printk("crypto: crypto_init cannot start cryptoret thread; error %d",
1718 unsigned long d_flags;
1720 dprintk("%s()\n", __FUNCTION__);
1723 * Terminate any crypto threads.
1726 CRYPTO_DRIVER_LOCK();
1728 cryptoproc = (pid_t) -1;
1729 kill_proc(p, SIGTERM, 1);
1730 wake_up_interruptible(&cryptoproc_wait);
1731 CRYPTO_DRIVER_UNLOCK();
1733 wait_for_completion(&cryptoproc_exited);
1735 CRYPTO_DRIVER_LOCK();
1737 cryptoretproc = (pid_t) -1;
1738 kill_proc(p, SIGTERM, 1);
1739 wake_up_interruptible(&cryptoretproc_wait);
1740 CRYPTO_DRIVER_UNLOCK();
1742 wait_for_completion(&cryptoretproc_exited);
1744 /* XXX flush queues??? */
1747 * Reclaim dynamically allocated resources.
1749 if (crypto_drivers != NULL)
1750 kfree(crypto_drivers);
1752 if (cryptodesc_zone != NULL)
1753 kmem_cache_destroy(cryptodesc_zone);
1754 if (cryptop_zone != NULL)
1755 kmem_cache_destroy(cryptop_zone);
1759 EXPORT_SYMBOL(crypto_newsession);
1760 EXPORT_SYMBOL(crypto_freesession);
1761 EXPORT_SYMBOL(crypto_get_driverid);
1762 EXPORT_SYMBOL(crypto_kregister);
1763 EXPORT_SYMBOL(crypto_register);
1764 EXPORT_SYMBOL(crypto_unregister);
1765 EXPORT_SYMBOL(crypto_unregister_all);
1766 EXPORT_SYMBOL(crypto_unblock);
1767 EXPORT_SYMBOL(crypto_dispatch);
1768 EXPORT_SYMBOL(crypto_kdispatch);
1769 EXPORT_SYMBOL(crypto_freereq);
1770 EXPORT_SYMBOL(crypto_getreq);
1771 EXPORT_SYMBOL(crypto_done);
1772 EXPORT_SYMBOL(crypto_kdone);
1773 EXPORT_SYMBOL(crypto_getfeat);
1774 EXPORT_SYMBOL(crypto_userasymcrypto);
1775 EXPORT_SYMBOL(crypto_getcaps);
1776 EXPORT_SYMBOL(crypto_find_driver);
1777 EXPORT_SYMBOL(crypto_find_device_byhid);
1779 module_init(crypto_init);
1780 module_exit(crypto_exit);
1782 MODULE_LICENSE("BSD");
1783 MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
1784 MODULE_DESCRIPTION("OCF (OpenBSD Cryptographic Framework)");