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 #include <linux/version.h>
67 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
68 #include <linux/config.h>
70 #include <linux/module.h>
71 #include <linux/init.h>
72 #include <linux/list.h>
73 #include <linux/slab.h>
74 #include <linux/wait.h>
75 #include <linux/sched.h>
76 #include <linux/spinlock.h>
77 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
78 #include <linux/kthread.h>
80 #include <cryptodev.h>
83 * keep track of whether or not we have been initialised, a big
84 * issue if we are linked into the kernel and a driver gets started before
87 static int crypto_initted
= 0;
90 * Crypto drivers register themselves by allocating a slot in the
91 * crypto_drivers table with crypto_get_driverid() and then registering
92 * each algorithm they support with crypto_register() and crypto_kregister().
96 * lock on driver table
97 * we track its state as spin_is_locked does not do anything on non-SMP boxes
99 static spinlock_t crypto_drivers_lock
;
100 static int crypto_drivers_locked
; /* for non-SMP boxes */
102 #define CRYPTO_DRIVER_LOCK() \
104 spin_lock_irqsave(&crypto_drivers_lock, d_flags); \
105 crypto_drivers_locked = 1; \
106 dprintk("%s,%d: DRIVER_LOCK()\n", __FILE__, __LINE__); \
108 #define CRYPTO_DRIVER_UNLOCK() \
110 dprintk("%s,%d: DRIVER_UNLOCK()\n", __FILE__, __LINE__); \
111 crypto_drivers_locked = 0; \
112 spin_unlock_irqrestore(&crypto_drivers_lock, d_flags); \
114 #define CRYPTO_DRIVER_ASSERT() \
116 if (!crypto_drivers_locked) { \
117 dprintk("%s,%d: DRIVER_ASSERT!\n", __FILE__, __LINE__); \
122 * Crypto device/driver capabilities structure.
125 * (d) - protected by CRYPTO_DRIVER_LOCK()
126 * (q) - protected by CRYPTO_Q_LOCK()
127 * Not tagged fields are read-only.
130 device_t cc_dev
; /* (d) device/driver */
131 u_int32_t cc_sessions
; /* (d) # of sessions */
132 u_int32_t cc_koperations
; /* (d) # os asym operations */
134 * Largest possible operator length (in bits) for each type of
135 * encryption algorithm. XXX not used
137 u_int16_t cc_max_op_len
[CRYPTO_ALGORITHM_MAX
+ 1];
138 u_int8_t cc_alg
[CRYPTO_ALGORITHM_MAX
+ 1];
139 u_int8_t cc_kalg
[CRK_ALGORITHM_MAX
+ 1];
141 int cc_flags
; /* (d) flags */
142 #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
143 int cc_qblocked
; /* (q) symmetric q blocked */
144 int cc_kqblocked
; /* (q) asymmetric q blocked */
146 int cc_unqblocked
; /* (q) symmetric q blocked */
147 int cc_unkqblocked
; /* (q) asymmetric q blocked */
149 static struct cryptocap
*crypto_drivers
= NULL
;
150 static int crypto_drivers_num
= 0;
153 * There are two queues for crypto requests; one for symmetric (e.g.
154 * cipher) operations and one for asymmetric (e.g. MOD)operations.
155 * A single mutex is used to lock access to both queues. We could
156 * have one per-queue but having one simplifies handling of block/unblock
159 static LIST_HEAD(crp_q
); /* crypto request queue */
160 static LIST_HEAD(crp_kq
); /* asym request queue */
162 static spinlock_t crypto_q_lock
;
164 int crypto_all_qblocked
= 0; /* protect with Q_LOCK */
165 module_param(crypto_all_qblocked
, int, 0444);
166 MODULE_PARM_DESC(crypto_all_qblocked
, "Are all crypto queues blocked");
168 int crypto_all_kqblocked
= 0; /* protect with Q_LOCK */
169 module_param(crypto_all_kqblocked
, int, 0444);
170 MODULE_PARM_DESC(crypto_all_kqblocked
, "Are all asym crypto queues blocked");
172 #define CRYPTO_Q_LOCK() \
174 spin_lock_irqsave(&crypto_q_lock, q_flags); \
175 dprintk("%s,%d: Q_LOCK()\n", __FILE__, __LINE__); \
177 #define CRYPTO_Q_UNLOCK() \
179 dprintk("%s,%d: Q_UNLOCK()\n", __FILE__, __LINE__); \
180 spin_unlock_irqrestore(&crypto_q_lock, q_flags); \
184 * There are two queues for processing completed crypto requests; one
185 * for the symmetric and one for the asymmetric ops. We only need one
186 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
187 * mutex is used to lock access to both queues. Note that this lock
188 * must be separate from the lock on request queues to insure driver
189 * callbacks don't generate lock order reversals.
191 static LIST_HEAD(crp_ret_q
); /* callback queues */
192 static LIST_HEAD(crp_ret_kq
);
194 static spinlock_t crypto_ret_q_lock
;
195 #define CRYPTO_RETQ_LOCK() \
197 spin_lock_irqsave(&crypto_ret_q_lock, r_flags); \
198 dprintk("%s,%d: RETQ_LOCK\n", __FILE__, __LINE__); \
200 #define CRYPTO_RETQ_UNLOCK() \
202 dprintk("%s,%d: RETQ_UNLOCK\n", __FILE__, __LINE__); \
203 spin_unlock_irqrestore(&crypto_ret_q_lock, r_flags); \
205 #define CRYPTO_RETQ_EMPTY() (list_empty(&crp_ret_q) && list_empty(&crp_ret_kq))
207 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
208 static kmem_cache_t
*cryptop_zone
;
209 static kmem_cache_t
*cryptodesc_zone
;
211 static struct kmem_cache
*cryptop_zone
;
212 static struct kmem_cache
*cryptodesc_zone
;
215 #define debug crypto_debug
216 int crypto_debug
= 0;
217 module_param(crypto_debug
, int, 0644);
218 MODULE_PARM_DESC(crypto_debug
, "Enable debug");
219 EXPORT_SYMBOL(crypto_debug
);
222 * Maximum number of outstanding crypto requests before we start
223 * failing requests. We need this to prevent DOS when too many
224 * requests are arriving for us to keep up. Otherwise we will
225 * run the system out of memory. Since crypto is slow, we are
226 * usually the bottleneck that needs to say, enough is enough.
228 * We cannot print errors when this condition occurs, we are already too
229 * slow, printing anything will just kill us
232 static int crypto_q_cnt
= 0;
233 module_param(crypto_q_cnt
, int, 0444);
234 MODULE_PARM_DESC(crypto_q_cnt
,
235 "Current number of outstanding crypto requests");
237 static int crypto_q_max
= 1000;
238 module_param(crypto_q_max
, int, 0644);
239 MODULE_PARM_DESC(crypto_q_max
,
240 "Maximum number of outstanding crypto requests");
242 #define bootverbose crypto_verbose
243 static int crypto_verbose
= 0;
244 module_param(crypto_verbose
, int, 0644);
245 MODULE_PARM_DESC(crypto_verbose
,
246 "Enable verbose crypto startup");
248 int crypto_usercrypto
= 1; /* userland may do crypto reqs */
249 module_param(crypto_usercrypto
, int, 0644);
250 MODULE_PARM_DESC(crypto_usercrypto
,
251 "Enable/disable user-mode access to crypto support");
253 int crypto_userasymcrypto
= 1; /* userland may do asym crypto reqs */
254 module_param(crypto_userasymcrypto
, int, 0644);
255 MODULE_PARM_DESC(crypto_userasymcrypto
,
256 "Enable/disable user-mode access to asymmetric crypto support");
258 int crypto_devallowsoft
= 0; /* only use hardware crypto */
259 module_param(crypto_devallowsoft
, int, 0644);
260 MODULE_PARM_DESC(crypto_devallowsoft
,
261 "Enable/disable use of software crypto support");
264 * This parameter controls the maximum number of crypto operations to
265 * do consecutively in the crypto kernel thread before scheduling to allow
266 * other processes to run. Without it, it is possible to get into a
267 * situation where the crypto thread never allows any other processes to run.
268 * Default to 1000 which should be less than one second.
270 static int crypto_max_loopcount
= 1000;
271 module_param(crypto_max_loopcount
, int, 0644);
272 MODULE_PARM_DESC(crypto_max_loopcount
,
273 "Maximum number of crypto ops to do before yielding to other processes");
275 #ifndef CONFIG_NR_CPUS
276 #define CONFIG_NR_CPUS 1
279 static struct task_struct
*cryptoproc
[CONFIG_NR_CPUS
];
280 static struct task_struct
*cryptoretproc
[CONFIG_NR_CPUS
];
281 static DECLARE_WAIT_QUEUE_HEAD(cryptoproc_wait
);
282 static DECLARE_WAIT_QUEUE_HEAD(cryptoretproc_wait
);
284 static int crypto_proc(void *arg
);
285 static int crypto_ret_proc(void *arg
);
286 static int crypto_invoke(struct cryptocap
*cap
, struct cryptop
*crp
, int hint
);
287 static int crypto_kinvoke(struct cryptkop
*krp
, int flags
);
288 static void crypto_exit(void);
289 static int crypto_init(void);
291 static struct cryptostats cryptostats
;
293 static struct cryptocap
*
294 crypto_checkdriver(u_int32_t hid
)
296 if (crypto_drivers
== NULL
)
298 return (hid
>= crypto_drivers_num
? NULL
: &crypto_drivers
[hid
]);
302 * Compare a driver's list of supported algorithms against another
303 * list; return non-zero if all algorithms are supported.
306 driver_suitable(const struct cryptocap
*cap
, const struct cryptoini
*cri
)
308 const struct cryptoini
*cr
;
310 /* See if all the algorithms are supported. */
311 for (cr
= cri
; cr
; cr
= cr
->cri_next
)
312 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;
791 wake_up_interruptible(&cryptoproc_wait
);
795 CRYPTO_Q_UNLOCK(); //DAVIDM should this be a driver lock
801 * Add a crypto request to a queue, to be processed by the kernel thread.
804 crypto_dispatch(struct cryptop
*crp
)
806 struct cryptocap
*cap
;
808 unsigned long q_flags
;
810 dprintk("%s()\n", __FUNCTION__
);
812 cryptostats
.cs_ops
++;
815 if (crypto_q_cnt
>= crypto_q_max
) {
816 cryptostats
.cs_drops
++;
822 /* make sure we are starting a fresh run on this crp. */
823 crp
->crp_flags
&= ~CRYPTO_F_DONE
;
827 * Caller marked the request to be processed immediately; dispatch
828 * it directly to the driver unless the driver is currently blocked.
830 if ((crp
->crp_flags
& CRYPTO_F_BATCH
) == 0) {
831 int hid
= CRYPTO_SESID2HID(crp
->crp_sid
);
832 cap
= crypto_checkdriver(hid
);
833 /* Driver cannot disappear when there is an active session. */
834 KASSERT(cap
!= NULL
, ("%s: Driver disappeared.", __func__
));
835 if (!cap
->cc_qblocked
) {
836 crypto_all_qblocked
= 0;
837 crypto_drivers
[hid
].cc_unqblocked
= 1;
839 result
= crypto_invoke(cap
, crp
, 0);
841 if (result
== ERESTART
)
842 if (crypto_drivers
[hid
].cc_unqblocked
)
843 crypto_drivers
[hid
].cc_qblocked
= 1;
844 crypto_drivers
[hid
].cc_unqblocked
= 0;
847 if (result
== ERESTART
) {
849 * The driver ran out of resources, mark the
850 * driver ``blocked'' for cryptop's and put
851 * the request back in the queue. It would
852 * best to put the request back where we got
853 * it but that's hard so for now we put it
854 * at the front. This should be ok; putting
855 * it at the end does not work.
857 list_add(&crp
->crp_next
, &crp_q
);
858 cryptostats
.cs_blocks
++;
860 } else if (result
== -1) {
861 TAILQ_INSERT_TAIL(&crp_q
, crp
, crp_next
);
864 wake_up_interruptible(&cryptoproc_wait
);
870 * Add an asymetric crypto request to a queue,
871 * to be processed by the kernel thread.
874 crypto_kdispatch(struct cryptkop
*krp
)
877 unsigned long q_flags
;
879 cryptostats
.cs_kops
++;
881 error
= crypto_kinvoke(krp
, krp
->krp_crid
);
882 if (error
== ERESTART
) {
884 TAILQ_INSERT_TAIL(&crp_kq
, krp
, krp_next
);
885 wake_up_interruptible(&cryptoproc_wait
);
893 * Verify a driver is suitable for the specified operation.
896 kdriver_suitable(const struct cryptocap
*cap
, const struct cryptkop
*krp
)
898 return (cap
->cc_kalg
[krp
->krp_op
] & CRYPTO_ALG_FLAG_SUPPORTED
) != 0;
902 * Select a driver for an asym operation. The driver must
903 * support the necessary algorithm. The caller can constrain
904 * which device is selected with the flags parameter. The
905 * algorithm we use here is pretty stupid; just use the first
906 * driver that supports the algorithms we need. If there are
907 * multiple suitable drivers we choose the driver with the
908 * fewest active operations. We prefer hardware-backed
909 * drivers to software ones when either may be used.
911 static struct cryptocap
*
912 crypto_select_kdriver(const struct cryptkop
*krp
, int flags
)
914 struct cryptocap
*cap
, *best
, *blocked
;
917 CRYPTO_DRIVER_ASSERT();
920 * Look first for hardware crypto devices if permitted.
922 if (flags
& CRYPTOCAP_F_HARDWARE
)
923 match
= CRYPTOCAP_F_HARDWARE
;
925 match
= CRYPTOCAP_F_SOFTWARE
;
929 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
930 cap
= &crypto_drivers
[hid
];
932 * If it's not initialized, is in the process of
933 * going away, or is not appropriate (hardware
934 * or software based on match), then skip.
936 if (cap
->cc_dev
== NULL
||
937 (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
) ||
938 (cap
->cc_flags
& match
) == 0)
941 /* verify all the algorithms are supported. */
942 if (kdriver_suitable(cap
, krp
)) {
944 cap
->cc_koperations
< best
->cc_koperations
)
950 if (match
== CRYPTOCAP_F_HARDWARE
&& (flags
& CRYPTOCAP_F_SOFTWARE
)) {
951 /* sort of an Algol 68-style for loop */
952 match
= CRYPTOCAP_F_SOFTWARE
;
959 * Dispatch an assymetric crypto request.
962 crypto_kinvoke(struct cryptkop
*krp
, int crid
)
964 struct cryptocap
*cap
= NULL
;
966 unsigned long d_flags
;
968 KASSERT(krp
!= NULL
, ("%s: krp == NULL", __func__
));
969 KASSERT(krp
->krp_callback
!= NULL
,
970 ("%s: krp->crp_callback == NULL", __func__
));
972 CRYPTO_DRIVER_LOCK();
973 if ((crid
& (CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
)) == 0) {
974 cap
= crypto_checkdriver(crid
);
977 * Driver present, it must support the necessary
978 * algorithm and, if s/w drivers are excluded,
979 * it must be registered as hardware-backed.
981 if (!kdriver_suitable(cap
, krp
) ||
982 (!crypto_devallowsoft
&&
983 (cap
->cc_flags
& CRYPTOCAP_F_HARDWARE
) == 0))
988 * No requested driver; select based on crid flags.
990 if (!crypto_devallowsoft
) /* NB: disallow s/w drivers */
991 crid
&= ~CRYPTOCAP_F_SOFTWARE
;
992 cap
= crypto_select_kdriver(krp
, crid
);
994 if (cap
!= NULL
&& !cap
->cc_kqblocked
) {
995 krp
->krp_hid
= cap
- crypto_drivers
;
996 cap
->cc_koperations
++;
997 CRYPTO_DRIVER_UNLOCK();
998 error
= CRYPTODEV_KPROCESS(cap
->cc_dev
, krp
, 0);
999 CRYPTO_DRIVER_LOCK();
1000 if (error
== ERESTART
) {
1001 cap
->cc_koperations
--;
1002 CRYPTO_DRIVER_UNLOCK();
1005 /* return the actual device used */
1006 krp
->krp_crid
= krp
->krp_hid
;
1009 * NB: cap is !NULL if device is blocked; in
1010 * that case return ERESTART so the operation
1011 * is resubmitted if possible.
1013 error
= (cap
== NULL
) ? ENODEV
: ERESTART
;
1015 CRYPTO_DRIVER_UNLOCK();
1018 krp
->krp_status
= error
;
1026 * Dispatch a crypto request to the appropriate crypto devices.
1029 crypto_invoke(struct cryptocap
*cap
, struct cryptop
*crp
, int hint
)
1031 KASSERT(crp
!= NULL
, ("%s: crp == NULL", __func__
));
1032 KASSERT(crp
->crp_callback
!= NULL
,
1033 ("%s: crp->crp_callback == NULL", __func__
));
1034 KASSERT(crp
->crp_desc
!= NULL
, ("%s: crp->crp_desc == NULL", __func__
));
1036 dprintk("%s()\n", __FUNCTION__
);
1038 #ifdef CRYPTO_TIMING
1040 crypto_tstat(&cryptostats
.cs_invoke
, &crp
->crp_tstamp
);
1042 if (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
) {
1043 struct cryptodesc
*crd
;
1047 * Driver has unregistered; migrate the session and return
1048 * an error to the caller so they'll resubmit the op.
1050 * XXX: What if there are more already queued requests for this
1053 crypto_freesession(crp
->crp_sid
);
1055 for (crd
= crp
->crp_desc
; crd
->crd_next
; crd
= crd
->crd_next
)
1056 crd
->CRD_INI
.cri_next
= &(crd
->crd_next
->CRD_INI
);
1058 /* XXX propagate flags from initial session? */
1059 if (crypto_newsession(&nid
, &(crp
->crp_desc
->CRD_INI
),
1060 CRYPTOCAP_F_HARDWARE
| CRYPTOCAP_F_SOFTWARE
) == 0)
1063 crp
->crp_etype
= EAGAIN
;
1068 * Invoke the driver to process the request.
1070 return CRYPTODEV_PROCESS(cap
->cc_dev
, crp
, hint
);
1075 * Release a set of crypto descriptors.
1078 crypto_freereq(struct cryptop
*crp
)
1080 struct cryptodesc
*crd
;
1087 struct cryptop
*crp2
;
1088 unsigned long q_flags
;
1091 TAILQ_FOREACH(crp2
, &crp_q
, crp_next
) {
1092 KASSERT(crp2
!= crp
,
1093 ("Freeing cryptop from the crypto queue (%p).",
1098 TAILQ_FOREACH(crp2
, &crp_ret_q
, crp_next
) {
1099 KASSERT(crp2
!= crp
,
1100 ("Freeing cryptop from the return queue (%p).",
1103 CRYPTO_RETQ_UNLOCK();
1107 while ((crd
= crp
->crp_desc
) != NULL
) {
1108 crp
->crp_desc
= crd
->crd_next
;
1109 kmem_cache_free(cryptodesc_zone
, crd
);
1111 kmem_cache_free(cryptop_zone
, crp
);
1115 * Acquire a set of crypto descriptors.
1118 crypto_getreq(int num
)
1120 struct cryptodesc
*crd
;
1121 struct cryptop
*crp
;
1123 crp
= kmem_cache_alloc(cryptop_zone
, SLAB_ATOMIC
);
1125 memset(crp
, 0, sizeof(*crp
));
1126 INIT_LIST_HEAD(&crp
->crp_next
);
1127 init_waitqueue_head(&crp
->crp_waitq
);
1129 crd
= kmem_cache_alloc(cryptodesc_zone
, SLAB_ATOMIC
);
1131 crypto_freereq(crp
);
1134 memset(crd
, 0, sizeof(*crd
));
1135 crd
->crd_next
= crp
->crp_desc
;
1136 crp
->crp_desc
= crd
;
1143 * Invoke the callback on behalf of the driver.
1146 crypto_done(struct cryptop
*crp
)
1148 unsigned long q_flags
;
1150 dprintk("%s()\n", __FUNCTION__
);
1151 if ((crp
->crp_flags
& CRYPTO_F_DONE
) == 0) {
1152 crp
->crp_flags
|= CRYPTO_F_DONE
;
1157 printk("crypto: crypto_done op already done, flags 0x%x",
1159 if (crp
->crp_etype
!= 0)
1160 cryptostats
.cs_errs
++;
1162 * CBIMM means unconditionally do the callback immediately;
1163 * CBIFSYNC means do the callback immediately only if the
1164 * operation was done synchronously. Both are used to avoid
1165 * doing extraneous context switches; the latter is mostly
1166 * used with the software crypto driver.
1168 if ((crp
->crp_flags
& CRYPTO_F_CBIMM
) ||
1169 ((crp
->crp_flags
& CRYPTO_F_CBIFSYNC
) &&
1170 (CRYPTO_SESID2CAPS(crp
->crp_sid
) & CRYPTOCAP_F_SYNC
))) {
1172 * Do the callback directly. This is ok when the
1173 * callback routine does very little (e.g. the
1174 * /dev/crypto callback method just does a wakeup).
1176 crp
->crp_callback(crp
);
1178 unsigned long r_flags
;
1180 * Normal case; queue the callback for the thread.
1183 wake_up_interruptible(&cryptoretproc_wait
);/* shared wait channel */
1184 TAILQ_INSERT_TAIL(&crp_ret_q
, crp
, crp_next
);
1185 CRYPTO_RETQ_UNLOCK();
1190 * Invoke the callback on behalf of the driver.
1193 crypto_kdone(struct cryptkop
*krp
)
1195 struct cryptocap
*cap
;
1196 unsigned long d_flags
;
1198 if ((krp
->krp_flags
& CRYPTO_KF_DONE
) != 0)
1199 printk("crypto: crypto_kdone op already done, flags 0x%x",
1201 krp
->krp_flags
|= CRYPTO_KF_DONE
;
1202 if (krp
->krp_status
!= 0)
1203 cryptostats
.cs_kerrs
++;
1205 CRYPTO_DRIVER_LOCK();
1206 /* XXX: What if driver is loaded in the meantime? */
1207 if (krp
->krp_hid
< crypto_drivers_num
) {
1208 cap
= &crypto_drivers
[krp
->krp_hid
];
1209 cap
->cc_koperations
--;
1210 KASSERT(cap
->cc_koperations
>= 0, ("cc_koperations < 0"));
1211 if (cap
->cc_flags
& CRYPTOCAP_F_CLEANUP
)
1214 CRYPTO_DRIVER_UNLOCK();
1217 * CBIMM means unconditionally do the callback immediately;
1218 * This is used to avoid doing extraneous context switches
1220 if ((krp
->krp_flags
& CRYPTO_KF_CBIMM
)) {
1222 * Do the callback directly. This is ok when the
1223 * callback routine does very little (e.g. the
1224 * /dev/crypto callback method just does a wakeup).
1226 krp
->krp_callback(krp
);
1228 unsigned long r_flags
;
1230 * Normal case; queue the callback for the thread.
1233 wake_up_interruptible(&cryptoretproc_wait
);/* shared wait channel */
1234 TAILQ_INSERT_TAIL(&crp_ret_kq
, krp
, krp_next
);
1235 CRYPTO_RETQ_UNLOCK();
1240 crypto_getfeat(int *featp
)
1242 int hid
, kalg
, feat
= 0;
1243 unsigned long d_flags
;
1245 CRYPTO_DRIVER_LOCK();
1246 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
1247 const struct cryptocap
*cap
= &crypto_drivers
[hid
];
1249 if ((cap
->cc_flags
& CRYPTOCAP_F_SOFTWARE
) &&
1250 !crypto_devallowsoft
) {
1253 for (kalg
= 0; kalg
< CRK_ALGORITHM_MAX
; kalg
++)
1254 if (cap
->cc_kalg
[kalg
] & CRYPTO_ALG_FLAG_SUPPORTED
)
1257 CRYPTO_DRIVER_UNLOCK();
1263 * Crypto thread, dispatches crypto requests.
1266 crypto_proc(void *arg
)
1268 struct cryptop
*crp
, *submit
;
1269 struct cryptkop
*krp
, *krpp
;
1270 struct cryptocap
*cap
;
1273 unsigned long q_flags
;
1276 set_current_state(TASK_INTERRUPTIBLE
);
1281 * we need to make sure we don't get into a busy loop with nothing
1282 * to do, the two crypto_all_*blocked vars help us find out when
1283 * we are all full and can do nothing on any driver or Q. If so we
1284 * wait for an unblock.
1286 crypto_all_qblocked
= !list_empty(&crp_q
);
1289 * Find the first element in the queue that can be
1290 * processed and look-ahead to see if multiple ops
1291 * are ready for the same driver.
1295 list_for_each_entry(crp
, &crp_q
, crp_next
) {
1296 hid
= CRYPTO_SESID2HID(crp
->crp_sid
);
1297 cap
= crypto_checkdriver(hid
);
1299 * Driver cannot disappear when there is an active
1302 KASSERT(cap
!= NULL
, ("%s:%u Driver disappeared.",
1303 __func__
, __LINE__
));
1304 if (cap
== NULL
|| cap
->cc_dev
== NULL
) {
1305 /* Op needs to be migrated, process it. */
1310 if (!cap
->cc_qblocked
) {
1311 if (submit
!= NULL
) {
1313 * We stop on finding another op,
1314 * regardless whether its for the same
1315 * driver or not. We could keep
1316 * searching the queue but it might be
1317 * better to just use a per-driver
1320 if (CRYPTO_SESID2HID(submit
->crp_sid
) == hid
)
1321 hint
= CRYPTO_HINT_MORE
;
1325 if ((submit
->crp_flags
& CRYPTO_F_BATCH
) == 0)
1327 /* keep scanning for more are q'd */
1331 if (submit
!= NULL
) {
1332 hid
= CRYPTO_SESID2HID(submit
->crp_sid
);
1333 crypto_all_qblocked
= 0;
1334 list_del(&submit
->crp_next
);
1335 crypto_drivers
[hid
].cc_unqblocked
= 1;
1336 cap
= crypto_checkdriver(hid
);
1338 KASSERT(cap
!= NULL
, ("%s:%u Driver disappeared.",
1339 __func__
, __LINE__
));
1340 result
= crypto_invoke(cap
, submit
, hint
);
1342 if (result
== ERESTART
) {
1344 * The driver ran out of resources, mark the
1345 * driver ``blocked'' for cryptop's and put
1346 * the request back in the queue. It would
1347 * best to put the request back where we got
1348 * it but that's hard so for now we put it
1349 * at the front. This should be ok; putting
1350 * it at the end does not work.
1352 /* XXX validate sid again? */
1353 list_add(&submit
->crp_next
, &crp_q
);
1354 cryptostats
.cs_blocks
++;
1355 if (crypto_drivers
[hid
].cc_unqblocked
)
1356 crypto_drivers
[hid
].cc_qblocked
=0;
1357 crypto_drivers
[hid
].cc_unqblocked
=0;
1359 crypto_drivers
[hid
].cc_unqblocked
= 0;
1362 crypto_all_kqblocked
= !list_empty(&crp_kq
);
1364 /* As above, but for key ops */
1366 list_for_each_entry(krpp
, &crp_kq
, krp_next
) {
1367 cap
= crypto_checkdriver(krpp
->krp_hid
);
1368 if (cap
== NULL
|| cap
->cc_dev
== NULL
) {
1370 * Operation needs to be migrated, invalidate
1371 * the assigned device so it will reselect a
1372 * new one below. Propagate the original
1373 * crid selection flags if supplied.
1375 krp
->krp_hid
= krp
->krp_crid
&
1376 (CRYPTOCAP_F_SOFTWARE
|CRYPTOCAP_F_HARDWARE
);
1377 if (krp
->krp_hid
== 0)
1379 CRYPTOCAP_F_SOFTWARE
|CRYPTOCAP_F_HARDWARE
;
1382 if (!cap
->cc_kqblocked
) {
1388 crypto_all_kqblocked
= 0;
1389 list_del(&krp
->krp_next
);
1390 crypto_drivers
[krp
->krp_hid
].cc_kqblocked
= 1;
1392 result
= crypto_kinvoke(krp
, krp
->krp_hid
);
1394 if (result
== ERESTART
) {
1396 * The driver ran out of resources, mark the
1397 * driver ``blocked'' for cryptkop's and put
1398 * the request back in the queue. It would
1399 * best to put the request back where we got
1400 * it but that's hard so for now we put it
1401 * at the front. This should be ok; putting
1402 * it at the end does not work.
1404 /* XXX validate sid again? */
1405 list_add(&krp
->krp_next
, &crp_kq
);
1406 cryptostats
.cs_kblocks
++;
1408 crypto_drivers
[krp
->krp_hid
].cc_kqblocked
= 0;
1411 if (submit
== NULL
&& krp
== NULL
) {
1413 * Nothing more to be processed. Sleep until we're
1414 * woken because there are more ops to process.
1415 * This happens either by submission or by a driver
1416 * becoming unblocked and notifying us through
1417 * crypto_unblock. Note that when we wakeup we
1418 * start processing each queue again from the
1419 * front. It's not clear that it's important to
1420 * preserve this ordering since ops may finish
1421 * out of order if dispatched to different devices
1422 * and some become blocked while others do not.
1424 dprintk("%s - sleeping (qe=%d qb=%d kqe=%d kqb=%d)\n",
1426 list_empty(&crp_q
), crypto_all_qblocked
,
1427 list_empty(&crp_kq
), crypto_all_kqblocked
);
1430 wait_event_interruptible(cryptoproc_wait
,
1431 !(list_empty(&crp_q
) || crypto_all_qblocked
) ||
1432 !(list_empty(&crp_kq
) || crypto_all_kqblocked
) ||
1433 kthread_should_stop());
1434 if (signal_pending (current
)) {
1435 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1436 spin_lock_irq(¤t
->sigmask_lock
);
1438 flush_signals(current
);
1439 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1440 spin_unlock_irq(¤t
->sigmask_lock
);
1444 dprintk("%s - awake\n", __FUNCTION__
);
1445 if (kthread_should_stop())
1447 cryptostats
.cs_intrs
++;
1448 } else if (loopcount
> crypto_max_loopcount
) {
1450 * Give other processes a chance to run if we've
1451 * been using the CPU exclusively for a while.
1465 * Crypto returns thread, does callbacks for processed crypto requests.
1466 * Callbacks are done here, rather than in the crypto drivers, because
1467 * callbacks typically are expensive and would slow interrupt handling.
1470 crypto_ret_proc(void *arg
)
1472 struct cryptop
*crpt
;
1473 struct cryptkop
*krpt
;
1474 unsigned long r_flags
;
1476 set_current_state(TASK_INTERRUPTIBLE
);
1480 /* Harvest return q's for completed ops */
1482 if (!list_empty(&crp_ret_q
))
1483 crpt
= list_entry(crp_ret_q
.next
, typeof(*crpt
), crp_next
);
1485 list_del(&crpt
->crp_next
);
1488 if (!list_empty(&crp_ret_kq
))
1489 krpt
= list_entry(crp_ret_kq
.next
, typeof(*krpt
), krp_next
);
1491 list_del(&krpt
->krp_next
);
1493 if (crpt
!= NULL
|| krpt
!= NULL
) {
1494 CRYPTO_RETQ_UNLOCK();
1496 * Run callbacks unlocked.
1499 crpt
->crp_callback(crpt
);
1501 krpt
->krp_callback(krpt
);
1505 * Nothing more to be processed. Sleep until we're
1506 * woken because there are more returns to process.
1508 dprintk("%s - sleeping\n", __FUNCTION__
);
1509 CRYPTO_RETQ_UNLOCK();
1510 wait_event_interruptible(cryptoretproc_wait
,
1511 !list_empty(&crp_ret_q
) ||
1512 !list_empty(&crp_ret_kq
) ||
1513 kthread_should_stop());
1514 if (signal_pending (current
)) {
1515 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1516 spin_lock_irq(¤t
->sigmask_lock
);
1518 flush_signals(current
);
1519 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1520 spin_unlock_irq(¤t
->sigmask_lock
);
1524 dprintk("%s - awake\n", __FUNCTION__
);
1525 if (kthread_should_stop()) {
1526 dprintk("%s - EXITING!\n", __FUNCTION__
);
1529 cryptostats
.cs_rets
++;
1532 CRYPTO_RETQ_UNLOCK();
1537 #if 0 /* should put this into /proc or something */
1539 db_show_drivers(void)
1543 db_printf("%12s %4s %4s %8s %2s %2s\n"
1551 for (hid
= 0; hid
< crypto_drivers_num
; hid
++) {
1552 const struct cryptocap
*cap
= &crypto_drivers
[hid
];
1553 if (cap
->cc_dev
== NULL
)
1555 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1556 , device_get_nameunit(cap
->cc_dev
)
1558 , cap
->cc_koperations
1566 DB_SHOW_COMMAND(crypto
, db_show_crypto
)
1568 struct cryptop
*crp
;
1573 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1574 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1575 "Desc", "Callback");
1576 TAILQ_FOREACH(crp
, &crp_q
, crp_next
) {
1577 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1578 , (int) CRYPTO_SESID2HID(crp
->crp_sid
)
1579 , (int) CRYPTO_SESID2CAPS(crp
->crp_sid
)
1580 , crp
->crp_ilen
, crp
->crp_olen
1587 if (!TAILQ_EMPTY(&crp_ret_q
)) {
1588 db_printf("\n%4s %4s %4s %8s\n",
1589 "HID", "Etype", "Flags", "Callback");
1590 TAILQ_FOREACH(crp
, &crp_ret_q
, crp_next
) {
1591 db_printf("%4u %4u %04x %8p\n"
1592 , (int) CRYPTO_SESID2HID(crp
->crp_sid
)
1601 DB_SHOW_COMMAND(kcrypto
, db_show_kcrypto
)
1603 struct cryptkop
*krp
;
1608 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1609 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1610 TAILQ_FOREACH(krp
, &crp_kq
, krp_next
) {
1611 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1614 , krp
->krp_iparams
, krp
->krp_oparams
1615 , krp
->krp_crid
, krp
->krp_hid
1619 if (!TAILQ_EMPTY(&crp_ret_q
)) {
1620 db_printf("%4s %5s %8s %4s %8s\n",
1621 "Op", "Status", "CRID", "HID", "Callback");
1622 TAILQ_FOREACH(krp
, &crp_ret_kq
, krp_next
) {
1623 db_printf("%4u %5u %08x %4u %8p\n"
1626 , krp
->krp_crid
, krp
->krp_hid
1641 dprintk("%s(%p)\n", __FUNCTION__
, (void *) crypto_init
);
1647 spin_lock_init(&crypto_drivers_lock
);
1648 spin_lock_init(&crypto_q_lock
);
1649 spin_lock_init(&crypto_ret_q_lock
);
1651 cryptop_zone
= kmem_cache_create("cryptop", sizeof(struct cryptop
),
1652 0, SLAB_HWCACHE_ALIGN
, NULL
1653 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1658 cryptodesc_zone
= kmem_cache_create("cryptodesc", sizeof(struct cryptodesc
),
1659 0, SLAB_HWCACHE_ALIGN
, NULL
1660 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1665 if (cryptodesc_zone
== NULL
|| cryptop_zone
== NULL
) {
1666 printk("crypto: crypto_init cannot setup crypto zones\n");
1671 crypto_drivers_num
= CRYPTO_DRIVERS_INITIAL
;
1672 crypto_drivers
= kmalloc(crypto_drivers_num
* sizeof(struct cryptocap
),
1674 if (crypto_drivers
== NULL
) {
1675 printk("crypto: crypto_init cannot setup crypto drivers\n");
1680 memset(crypto_drivers
, 0, crypto_drivers_num
* sizeof(struct cryptocap
));
1682 ocf_for_each_cpu(cpu
) {
1683 cryptoproc
[cpu
] = kthread_create(crypto_proc
, (void *) cpu
,
1684 "ocf_%d", (int) cpu
);
1685 if (IS_ERR(cryptoproc
[cpu
])) {
1686 error
= PTR_ERR(cryptoproc
[cpu
]);
1687 printk("crypto: crypto_init cannot start crypto thread; error %d",
1691 kthread_bind(cryptoproc
[cpu
], cpu
);
1692 wake_up_process(cryptoproc
[cpu
]);
1694 cryptoretproc
[cpu
] = kthread_create(crypto_ret_proc
, (void *) cpu
,
1695 "ocf_ret_%d", (int) cpu
);
1696 if (IS_ERR(cryptoretproc
[cpu
])) {
1697 error
= PTR_ERR(cryptoretproc
[cpu
]);
1698 printk("crypto: crypto_init cannot start cryptoret thread; error %d",
1702 kthread_bind(cryptoretproc
[cpu
], cpu
);
1703 wake_up_process(cryptoretproc
[cpu
]);
1718 dprintk("%s()\n", __FUNCTION__
);
1721 * Terminate any crypto threads.
1723 ocf_for_each_cpu(cpu
) {
1724 kthread_stop(cryptoproc
[cpu
]);
1725 kthread_stop(cryptoretproc
[cpu
]);
1729 * Reclaim dynamically allocated resources.
1731 if (crypto_drivers
!= NULL
)
1732 kfree(crypto_drivers
);
1734 if (cryptodesc_zone
!= NULL
)
1735 kmem_cache_destroy(cryptodesc_zone
);
1736 if (cryptop_zone
!= NULL
)
1737 kmem_cache_destroy(cryptop_zone
);
1741 EXPORT_SYMBOL(crypto_newsession
);
1742 EXPORT_SYMBOL(crypto_freesession
);
1743 EXPORT_SYMBOL(crypto_get_driverid
);
1744 EXPORT_SYMBOL(crypto_kregister
);
1745 EXPORT_SYMBOL(crypto_register
);
1746 EXPORT_SYMBOL(crypto_unregister
);
1747 EXPORT_SYMBOL(crypto_unregister_all
);
1748 EXPORT_SYMBOL(crypto_unblock
);
1749 EXPORT_SYMBOL(crypto_dispatch
);
1750 EXPORT_SYMBOL(crypto_kdispatch
);
1751 EXPORT_SYMBOL(crypto_freereq
);
1752 EXPORT_SYMBOL(crypto_getreq
);
1753 EXPORT_SYMBOL(crypto_done
);
1754 EXPORT_SYMBOL(crypto_kdone
);
1755 EXPORT_SYMBOL(crypto_getfeat
);
1756 EXPORT_SYMBOL(crypto_userasymcrypto
);
1757 EXPORT_SYMBOL(crypto_getcaps
);
1758 EXPORT_SYMBOL(crypto_find_driver
);
1759 EXPORT_SYMBOL(crypto_find_device_byhid
);
1761 module_init(crypto_init
);
1762 module_exit(crypto_exit
);
1764 MODULE_LICENSE("BSD");
1765 MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
1766 MODULE_DESCRIPTION("OCF (OpenBSD Cryptographic Framework)");