kernel: fix cryptodev with kernel >= 2.6.35
[openwrt/svn-archive/archive.git] / target / linux / generic / files / crypto / ocf / crypto.c
1 /*-
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.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * Copyright (c) 2002-2006 Sam Leffler. All rights reserved.
9 *
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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.
28 */
29
30 #if 0
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.27 2007/03/21 03:42:51 sam Exp $");
33 #endif
34
35 /*
36 * Cryptographic Subsystem.
37 *
38 * This code is derived from the Openbsd Cryptographic Framework (OCF)
39 * that has the copyright shown below. Very little of the original
40 * code remains.
41 */
42 /*-
43 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
44 *
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.
48 *
49 * Copyright (c) 2000, 2001 Angelos D. Keromytis
50 *
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.
55 *
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
60 * PURPOSE.
61 *
62 __FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.16 2005/01/07 02:29:16 imp Exp $");
63 */
64
65
66 #ifndef AUTOCONF_INCLUDED
67 #include <linux/config.h>
68 #endif
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>
78
79 /*
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
82 * us
83 */
84 static int crypto_initted = 0;
85
86 /*
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().
90 */
91
92 /*
93 * lock on driver table
94 * we track its state as spin_is_locked does not do anything on non-SMP boxes
95 */
96 static spinlock_t crypto_drivers_lock;
97 static int crypto_drivers_locked; /* for non-SMP boxes */
98
99 #define CRYPTO_DRIVER_LOCK() \
100 ({ \
101 spin_lock_irqsave(&crypto_drivers_lock, d_flags); \
102 crypto_drivers_locked = 1; \
103 dprintk("%s,%d: DRIVER_LOCK()\n", __FILE__, __LINE__); \
104 })
105 #define CRYPTO_DRIVER_UNLOCK() \
106 ({ \
107 dprintk("%s,%d: DRIVER_UNLOCK()\n", __FILE__, __LINE__); \
108 crypto_drivers_locked = 0; \
109 spin_unlock_irqrestore(&crypto_drivers_lock, d_flags); \
110 })
111 #define CRYPTO_DRIVER_ASSERT() \
112 ({ \
113 if (!crypto_drivers_locked) { \
114 dprintk("%s,%d: DRIVER_ASSERT!\n", __FILE__, __LINE__); \
115 } \
116 })
117
118 /*
119 * Crypto device/driver capabilities structure.
120 *
121 * Synchronization:
122 * (d) - protected by CRYPTO_DRIVER_LOCK()
123 * (q) - protected by CRYPTO_Q_LOCK()
124 * Not tagged fields are read-only.
125 */
126 struct cryptocap {
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 */
130 /*
131 * Largest possible operator length (in bits) for each type of
132 * encryption algorithm. XXX not used
133 */
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];
137
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 */
142
143 int cc_unqblocked; /* (q) symmetric q blocked */
144 int cc_unkqblocked; /* (q) asymmetric q blocked */
145 };
146 static struct cryptocap *crypto_drivers = NULL;
147 static int crypto_drivers_num = 0;
148
149 /*
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
154 * operations.
155 */
156 static int crp_sleep = 0;
157 static LIST_HEAD(crp_q); /* request queues */
158 static LIST_HEAD(crp_kq);
159
160 static spinlock_t crypto_q_lock;
161
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");
165
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");
169
170 #define CRYPTO_Q_LOCK() \
171 ({ \
172 spin_lock_irqsave(&crypto_q_lock, q_flags); \
173 dprintk("%s,%d: Q_LOCK()\n", __FILE__, __LINE__); \
174 })
175 #define CRYPTO_Q_UNLOCK() \
176 ({ \
177 dprintk("%s,%d: Q_UNLOCK()\n", __FILE__, __LINE__); \
178 spin_unlock_irqrestore(&crypto_q_lock, q_flags); \
179 })
180
181 /*
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.
188 */
189 static LIST_HEAD(crp_ret_q); /* callback queues */
190 static LIST_HEAD(crp_ret_kq);
191
192 static spinlock_t crypto_ret_q_lock;
193 #define CRYPTO_RETQ_LOCK() \
194 ({ \
195 spin_lock_irqsave(&crypto_ret_q_lock, r_flags); \
196 dprintk("%s,%d: RETQ_LOCK\n", __FILE__, __LINE__); \
197 })
198 #define CRYPTO_RETQ_UNLOCK() \
199 ({ \
200 dprintk("%s,%d: RETQ_UNLOCK\n", __FILE__, __LINE__); \
201 spin_unlock_irqrestore(&crypto_ret_q_lock, r_flags); \
202 })
203 #define CRYPTO_RETQ_EMPTY() (list_empty(&crp_ret_q) && list_empty(&crp_ret_kq))
204
205 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
206 static kmem_cache_t *cryptop_zone;
207 static kmem_cache_t *cryptodesc_zone;
208 #else
209 static struct kmem_cache *cryptop_zone;
210 static struct kmem_cache *cryptodesc_zone;
211 #endif
212
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)
216 #endif
217
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);
223
224 /*
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.
230 *
231 * We cannot print errors when this condition occurs, we are already too
232 * slow, printing anything will just kill us
233 */
234
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");
239
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");
244
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");
250
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");
255
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");
260
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");
265
266 /*
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.
272 */
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");
277
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);
284
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);
291
292 static struct cryptostats cryptostats;
293
294 static struct cryptocap *
295 crypto_checkdriver(u_int32_t hid)
296 {
297 if (crypto_drivers == NULL)
298 return NULL;
299 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
300 }
301
302 /*
303 * Compare a driver's list of supported algorithms against another
304 * list; return non-zero if all algorithms are supported.
305 */
306 static int
307 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
308 {
309 const struct cryptoini *cr;
310
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)
314 return 0;
315 return 1;
316 }
317
318 /*
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.
325 *
326 * XXX We need more smarts here (in real life too, but that's
327 * XXX another story altogether).
328 */
329 static struct cryptocap *
330 crypto_select_driver(const struct cryptoini *cri, int flags)
331 {
332 struct cryptocap *cap, *best;
333 int match, hid;
334
335 CRYPTO_DRIVER_ASSERT();
336
337 /*
338 * Look first for hardware crypto devices if permitted.
339 */
340 if (flags & CRYPTOCAP_F_HARDWARE)
341 match = CRYPTOCAP_F_HARDWARE;
342 else
343 match = CRYPTOCAP_F_SOFTWARE;
344 best = NULL;
345 again:
346 for (hid = 0; hid < crypto_drivers_num; hid++) {
347 cap = &crypto_drivers[hid];
348 /*
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.
352 */
353 if (cap->cc_dev == NULL ||
354 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
355 (cap->cc_flags & match) == 0)
356 continue;
357
358 /* verify all the algorithms are supported. */
359 if (driver_suitable(cap, cri)) {
360 if (best == NULL ||
361 cap->cc_sessions < best->cc_sessions)
362 best = cap;
363 }
364 }
365 if (best != NULL)
366 return best;
367 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
368 /* sort of an Algol 68-style for loop */
369 match = CRYPTOCAP_F_SOFTWARE;
370 goto again;
371 }
372 return best;
373 }
374
375 /*
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.
380 */
381 int
382 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
383 {
384 struct cryptocap *cap;
385 u_int32_t hid, lid;
386 int err;
387 unsigned long d_flags;
388
389 CRYPTO_DRIVER_LOCK();
390 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
391 /*
392 * Use specified driver; verify it is capable.
393 */
394 cap = crypto_checkdriver(crid);
395 if (cap != NULL && !driver_suitable(cap, cri))
396 cap = NULL;
397 } else {
398 /*
399 * No requested driver; select based on crid flags.
400 */
401 cap = crypto_select_driver(cri, crid);
402 /*
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.
406 */
407 }
408 if (cap != NULL) {
409 /* Call the driver initialization routine. */
410 hid = cap - crypto_drivers;
411 lid = hid; /* Pass the driver ID. */
412 cap->cc_sessions++;
413 CRYPTO_DRIVER_UNLOCK();
414 err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
415 CRYPTO_DRIVER_LOCK();
416 if (err == 0) {
417 (*sid) = (cap->cc_flags & 0xff000000)
418 | (hid & 0x00ffffff);
419 (*sid) <<= 32;
420 (*sid) |= (lid & 0xffffffff);
421 } else
422 cap->cc_sessions--;
423 } else
424 err = EINVAL;
425 CRYPTO_DRIVER_UNLOCK();
426 return err;
427 }
428
429 static void
430 crypto_remove(struct cryptocap *cap)
431 {
432 CRYPTO_DRIVER_ASSERT();
433 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
434 bzero(cap, sizeof(*cap));
435 }
436
437 /*
438 * Delete an existing session (or a reserved session on an unregistered
439 * driver).
440 */
441 int
442 crypto_freesession(u_int64_t sid)
443 {
444 struct cryptocap *cap;
445 u_int32_t hid;
446 int err = 0;
447 unsigned long d_flags;
448
449 dprintk("%s()\n", __FUNCTION__);
450 CRYPTO_DRIVER_LOCK();
451
452 if (crypto_drivers == NULL) {
453 err = EINVAL;
454 goto done;
455 }
456
457 /* Determine two IDs. */
458 hid = CRYPTO_SESID2HID(sid);
459
460 if (hid >= crypto_drivers_num) {
461 dprintk("%s - INVALID DRIVER NUM %d\n", __FUNCTION__, hid);
462 err = ENOENT;
463 goto done;
464 }
465 cap = &crypto_drivers[hid];
466
467 if (cap->cc_dev) {
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();
472 }
473
474 if (cap->cc_sessions)
475 cap->cc_sessions--;
476
477 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
478 crypto_remove(cap);
479
480 done:
481 CRYPTO_DRIVER_UNLOCK();
482 return err;
483 }
484
485 /*
486 * Return an unused driver id. Used by drivers prior to registering
487 * support for the algorithms they handle.
488 */
489 int32_t
490 crypto_get_driverid(device_t dev, int flags)
491 {
492 struct cryptocap *newdrv;
493 int i;
494 unsigned long d_flags;
495
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));
499 return -1;
500 }
501
502 CRYPTO_DRIVER_LOCK();
503
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) {
507 break;
508 }
509 }
510
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");
517 return -1;
518 }
519
520 newdrv = kmalloc(2 * crypto_drivers_num * sizeof(struct cryptocap),
521 GFP_KERNEL);
522 if (newdrv == NULL) {
523 CRYPTO_DRIVER_UNLOCK();
524 printk("crypto: no space to expand driver table!\n");
525 return -1;
526 }
527
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));
532
533 crypto_drivers_num *= 2;
534
535 kfree(crypto_drivers);
536 crypto_drivers = newdrv;
537 }
538
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;
543 if (bootverbose)
544 printf("crypto: assign %s driver id %u, flags %u\n",
545 device_get_nameunit(dev), i, flags);
546
547 CRYPTO_DRIVER_UNLOCK();
548
549 return i;
550 }
551
552 /*
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.
557 */
558 int
559 crypto_find_driver(const char *match)
560 {
561 int i, len = strlen(match);
562 unsigned long d_flags;
563
564 CRYPTO_DRIVER_LOCK();
565 for (i = 0; i < crypto_drivers_num; i++) {
566 device_t dev = crypto_drivers[i].cc_dev;
567 if (dev == NULL ||
568 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
569 continue;
570 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
571 strncmp(match, device_get_name(dev), len) == 0)
572 break;
573 }
574 CRYPTO_DRIVER_UNLOCK();
575 return i < crypto_drivers_num ? i : -1;
576 }
577
578 /*
579 * Return the device_t for the specified driver or NULL
580 * if the driver identifier is invalid.
581 */
582 device_t
583 crypto_find_device_byhid(int hid)
584 {
585 struct cryptocap *cap = crypto_checkdriver(hid);
586 return cap != NULL ? cap->cc_dev : NULL;
587 }
588
589 /*
590 * Return the device/driver capabilities.
591 */
592 int
593 crypto_getcaps(int hid)
594 {
595 struct cryptocap *cap = crypto_checkdriver(hid);
596 return cap != NULL ? cap->cc_flags : 0;
597 }
598
599 /*
600 * Register support for a key-related algorithm. This routine
601 * is called once for each algorithm supported a driver.
602 */
603 int
604 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
605 {
606 struct cryptocap *cap;
607 int err;
608 unsigned long d_flags;
609
610 dprintk("%s()\n", __FUNCTION__);
611 CRYPTO_DRIVER_LOCK();
612
613 cap = crypto_checkdriver(driverid);
614 if (cap != NULL &&
615 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
616 /*
617 * XXX Do some performance testing to determine placing.
618 * XXX We probably need an auxiliary data structure that
619 * XXX describes relative performances.
620 */
621
622 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
623 if (bootverbose)
624 printf("crypto: %s registers key alg %u flags %u\n"
625 , device_get_nameunit(cap->cc_dev)
626 , kalg
627 , flags
628 );
629 err = 0;
630 } else
631 err = EINVAL;
632
633 CRYPTO_DRIVER_UNLOCK();
634 return err;
635 }
636
637 /*
638 * Register support for a non-key-related algorithm. This routine
639 * is called once for each such algorithm supported by a driver.
640 */
641 int
642 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
643 u_int32_t flags)
644 {
645 struct cryptocap *cap;
646 int err;
647 unsigned long d_flags;
648
649 dprintk("%s(id=0x%x, alg=%d, maxoplen=%d, flags=0x%x)\n", __FUNCTION__,
650 driverid, alg, maxoplen, flags);
651
652 CRYPTO_DRIVER_LOCK();
653
654 cap = crypto_checkdriver(driverid);
655 /* NB: algorithms are in the range [1..max] */
656 if (cap != NULL &&
657 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
658 /*
659 * XXX Do some performance testing to determine placing.
660 * XXX We probably need an auxiliary data structure that
661 * XXX describes relative performances.
662 */
663
664 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
665 cap->cc_max_op_len[alg] = maxoplen;
666 if (bootverbose)
667 printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
668 , device_get_nameunit(cap->cc_dev)
669 , alg
670 , flags
671 , maxoplen
672 );
673 cap->cc_sessions = 0; /* Unmark */
674 err = 0;
675 } else
676 err = EINVAL;
677
678 CRYPTO_DRIVER_UNLOCK();
679 return err;
680 }
681
682 static void
683 driver_finis(struct cryptocap *cap)
684 {
685 u_int32_t ses, kops;
686
687 CRYPTO_DRIVER_ASSERT();
688
689 ses = cap->cc_sessions;
690 kops = cap->cc_koperations;
691 bzero(cap, sizeof(*cap));
692 if (ses != 0 || kops != 0) {
693 /*
694 * If there are pending sessions,
695 * just mark as invalid.
696 */
697 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
698 cap->cc_sessions = ses;
699 cap->cc_koperations = kops;
700 }
701 }
702
703 /*
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
707 * reroute requests.
708 */
709 int
710 crypto_unregister(u_int32_t driverid, int alg)
711 {
712 struct cryptocap *cap;
713 int i, err;
714 unsigned long d_flags;
715
716 dprintk("%s()\n", __FUNCTION__);
717 CRYPTO_DRIVER_LOCK();
718
719 cap = crypto_checkdriver(driverid);
720 if (cap != NULL &&
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;
725
726 /* Was this the last algorithm ? */
727 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
728 if (cap->cc_alg[i] != 0)
729 break;
730
731 if (i == CRYPTO_ALGORITHM_MAX + 1)
732 driver_finis(cap);
733 err = 0;
734 } else
735 err = EINVAL;
736 CRYPTO_DRIVER_UNLOCK();
737 return err;
738 }
739
740 /*
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
745 * requests.
746 */
747 int
748 crypto_unregister_all(u_int32_t driverid)
749 {
750 struct cryptocap *cap;
751 int err;
752 unsigned long d_flags;
753
754 dprintk("%s()\n", __FUNCTION__);
755 CRYPTO_DRIVER_LOCK();
756 cap = crypto_checkdriver(driverid);
757 if (cap != NULL) {
758 driver_finis(cap);
759 err = 0;
760 } else
761 err = EINVAL;
762 CRYPTO_DRIVER_UNLOCK();
763
764 return err;
765 }
766
767 /*
768 * Clear blockage on a driver. The what parameter indicates whether
769 * the driver is now ready for cryptop's and/or cryptokop's.
770 */
771 int
772 crypto_unblock(u_int32_t driverid, int what)
773 {
774 struct cryptocap *cap;
775 int err;
776 unsigned long q_flags;
777
778 CRYPTO_Q_LOCK();
779 cap = crypto_checkdriver(driverid);
780 if (cap != NULL) {
781 if (what & CRYPTO_SYMQ) {
782 cap->cc_qblocked = 0;
783 cap->cc_unqblocked = 0;
784 crypto_all_qblocked = 0;
785 }
786 if (what & CRYPTO_ASYMQ) {
787 cap->cc_kqblocked = 0;
788 cap->cc_unkqblocked = 0;
789 crypto_all_kqblocked = 0;
790 }
791 if (crp_sleep)
792 wake_up_interruptible(&cryptoproc_wait);
793 err = 0;
794 } else
795 err = EINVAL;
796 CRYPTO_Q_UNLOCK(); //DAVIDM should this be a driver lock
797
798 return err;
799 }
800
801 /*
802 * Add a crypto request to a queue, to be processed by the kernel thread.
803 */
804 int
805 crypto_dispatch(struct cryptop *crp)
806 {
807 struct cryptocap *cap;
808 int result = -1;
809 unsigned long q_flags;
810
811 dprintk("%s()\n", __FUNCTION__);
812
813 cryptostats.cs_ops++;
814
815 CRYPTO_Q_LOCK();
816 if (crypto_q_cnt >= crypto_q_max) {
817 CRYPTO_Q_UNLOCK();
818 cryptostats.cs_drops++;
819 return ENOMEM;
820 }
821 crypto_q_cnt++;
822
823 /* make sure we are starting a fresh run on this crp. */
824 crp->crp_flags &= ~CRYPTO_F_DONE;
825 crp->crp_etype = 0;
826
827 /*
828 * Caller marked the request to be processed immediately; dispatch
829 * it directly to the driver unless the driver is currently blocked.
830 */
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;
839 CRYPTO_Q_UNLOCK();
840 result = crypto_invoke(cap, crp, 0);
841 CRYPTO_Q_LOCK();
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;
846 }
847 }
848 if (result == ERESTART) {
849 /*
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.
857 */
858 list_add(&crp->crp_next, &crp_q);
859 cryptostats.cs_blocks++;
860 result = 0;
861 } else if (result == -1) {
862 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
863 result = 0;
864 }
865 if (crp_sleep)
866 wake_up_interruptible(&cryptoproc_wait);
867 CRYPTO_Q_UNLOCK();
868 return result;
869 }
870
871 /*
872 * Add an asymetric crypto request to a queue,
873 * to be processed by the kernel thread.
874 */
875 int
876 crypto_kdispatch(struct cryptkop *krp)
877 {
878 int error;
879 unsigned long q_flags;
880
881 cryptostats.cs_kops++;
882
883 error = crypto_kinvoke(krp, krp->krp_crid);
884 if (error == ERESTART) {
885 CRYPTO_Q_LOCK();
886 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
887 if (crp_sleep)
888 wake_up_interruptible(&cryptoproc_wait);
889 CRYPTO_Q_UNLOCK();
890 error = 0;
891 }
892 return error;
893 }
894
895 /*
896 * Verify a driver is suitable for the specified operation.
897 */
898 static __inline int
899 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
900 {
901 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
902 }
903
904 /*
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.
913 */
914 static struct cryptocap *
915 crypto_select_kdriver(const struct cryptkop *krp, int flags)
916 {
917 struct cryptocap *cap, *best, *blocked;
918 int match, hid;
919
920 CRYPTO_DRIVER_ASSERT();
921
922 /*
923 * Look first for hardware crypto devices if permitted.
924 */
925 if (flags & CRYPTOCAP_F_HARDWARE)
926 match = CRYPTOCAP_F_HARDWARE;
927 else
928 match = CRYPTOCAP_F_SOFTWARE;
929 best = NULL;
930 blocked = NULL;
931 again:
932 for (hid = 0; hid < crypto_drivers_num; hid++) {
933 cap = &crypto_drivers[hid];
934 /*
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.
938 */
939 if (cap->cc_dev == NULL ||
940 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
941 (cap->cc_flags & match) == 0)
942 continue;
943
944 /* verify all the algorithms are supported. */
945 if (kdriver_suitable(cap, krp)) {
946 if (best == NULL ||
947 cap->cc_koperations < best->cc_koperations)
948 best = cap;
949 }
950 }
951 if (best != NULL)
952 return best;
953 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
954 /* sort of an Algol 68-style for loop */
955 match = CRYPTOCAP_F_SOFTWARE;
956 goto again;
957 }
958 return best;
959 }
960
961 /*
962 * Dispatch an assymetric crypto request.
963 */
964 static int
965 crypto_kinvoke(struct cryptkop *krp, int crid)
966 {
967 struct cryptocap *cap = NULL;
968 int error;
969 unsigned long d_flags;
970
971 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
972 KASSERT(krp->krp_callback != NULL,
973 ("%s: krp->crp_callback == NULL", __func__));
974
975 CRYPTO_DRIVER_LOCK();
976 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
977 cap = crypto_checkdriver(crid);
978 if (cap != NULL) {
979 /*
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.
983 */
984 if (!kdriver_suitable(cap, krp) ||
985 (!crypto_devallowsoft &&
986 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
987 cap = NULL;
988 }
989 } else {
990 /*
991 * No requested driver; select based on crid flags.
992 */
993 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
994 crid &= ~CRYPTOCAP_F_SOFTWARE;
995 cap = crypto_select_kdriver(krp, crid);
996 }
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();
1006 return (error);
1007 }
1008 /* return the actual device used */
1009 krp->krp_crid = krp->krp_hid;
1010 } else {
1011 /*
1012 * NB: cap is !NULL if device is blocked; in
1013 * that case return ERESTART so the operation
1014 * is resubmitted if possible.
1015 */
1016 error = (cap == NULL) ? ENODEV : ERESTART;
1017 }
1018 CRYPTO_DRIVER_UNLOCK();
1019
1020 if (error) {
1021 krp->krp_status = error;
1022 crypto_kdone(krp);
1023 }
1024 return 0;
1025 }
1026
1027
1028 /*
1029 * Dispatch a crypto request to the appropriate crypto devices.
1030 */
1031 static int
1032 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1033 {
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__));
1038
1039 dprintk("%s()\n", __FUNCTION__);
1040
1041 #ifdef CRYPTO_TIMING
1042 if (crypto_timing)
1043 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1044 #endif
1045 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1046 struct cryptodesc *crd;
1047 u_int64_t nid;
1048
1049 /*
1050 * Driver has unregistered; migrate the session and return
1051 * an error to the caller so they'll resubmit the op.
1052 *
1053 * XXX: What if there are more already queued requests for this
1054 * session?
1055 */
1056 crypto_freesession(crp->crp_sid);
1057
1058 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1059 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1060
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)
1064 crp->crp_sid = nid;
1065
1066 crp->crp_etype = EAGAIN;
1067 crypto_done(crp);
1068 return 0;
1069 } else {
1070 /*
1071 * Invoke the driver to process the request.
1072 */
1073 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1074 }
1075 }
1076
1077 /*
1078 * Release a set of crypto descriptors.
1079 */
1080 void
1081 crypto_freereq(struct cryptop *crp)
1082 {
1083 struct cryptodesc *crd;
1084
1085 if (crp == NULL)
1086 return;
1087
1088 #ifdef DIAGNOSTIC
1089 {
1090 struct cryptop *crp2;
1091 unsigned long q_flags;
1092
1093 CRYPTO_Q_LOCK();
1094 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1095 KASSERT(crp2 != crp,
1096 ("Freeing cryptop from the crypto queue (%p).",
1097 crp));
1098 }
1099 CRYPTO_Q_UNLOCK();
1100 CRYPTO_RETQ_LOCK();
1101 TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
1102 KASSERT(crp2 != crp,
1103 ("Freeing cryptop from the return queue (%p).",
1104 crp));
1105 }
1106 CRYPTO_RETQ_UNLOCK();
1107 }
1108 #endif
1109
1110 while ((crd = crp->crp_desc) != NULL) {
1111 crp->crp_desc = crd->crd_next;
1112 kmem_cache_free(cryptodesc_zone, crd);
1113 }
1114 kmem_cache_free(cryptop_zone, crp);
1115 }
1116
1117 /*
1118 * Acquire a set of crypto descriptors.
1119 */
1120 struct cryptop *
1121 crypto_getreq(int num)
1122 {
1123 struct cryptodesc *crd;
1124 struct cryptop *crp;
1125
1126 crp = kmem_cache_alloc(cryptop_zone, SLAB_ATOMIC);
1127 if (crp != NULL) {
1128 memset(crp, 0, sizeof(*crp));
1129 INIT_LIST_HEAD(&crp->crp_next);
1130 init_waitqueue_head(&crp->crp_waitq);
1131 while (num--) {
1132 crd = kmem_cache_alloc(cryptodesc_zone, SLAB_ATOMIC);
1133 if (crd == NULL) {
1134 crypto_freereq(crp);
1135 return NULL;
1136 }
1137 memset(crd, 0, sizeof(*crd));
1138 crd->crd_next = crp->crp_desc;
1139 crp->crp_desc = crd;
1140 }
1141 }
1142 return crp;
1143 }
1144
1145 /*
1146 * Invoke the callback on behalf of the driver.
1147 */
1148 void
1149 crypto_done(struct cryptop *crp)
1150 {
1151 unsigned long q_flags;
1152
1153 dprintk("%s()\n", __FUNCTION__);
1154 if ((crp->crp_flags & CRYPTO_F_DONE) == 0) {
1155 crp->crp_flags |= CRYPTO_F_DONE;
1156 CRYPTO_Q_LOCK();
1157 crypto_q_cnt--;
1158 CRYPTO_Q_UNLOCK();
1159 } else
1160 printk("crypto: crypto_done op already done, flags 0x%x",
1161 crp->crp_flags);
1162 if (crp->crp_etype != 0)
1163 cryptostats.cs_errs++;
1164 /*
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.
1170 */
1171 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1172 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1173 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
1174 /*
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).
1178 */
1179 crp->crp_callback(crp);
1180 } else {
1181 unsigned long r_flags;
1182 /*
1183 * Normal case; queue the callback for the thread.
1184 */
1185 CRYPTO_RETQ_LOCK();
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();
1190 }
1191 }
1192
1193 /*
1194 * Invoke the callback on behalf of the driver.
1195 */
1196 void
1197 crypto_kdone(struct cryptkop *krp)
1198 {
1199 struct cryptocap *cap;
1200 unsigned long d_flags;
1201
1202 if ((krp->krp_flags & CRYPTO_KF_DONE) != 0)
1203 printk("crypto: crypto_kdone op already done, flags 0x%x",
1204 krp->krp_flags);
1205 krp->krp_flags |= CRYPTO_KF_DONE;
1206 if (krp->krp_status != 0)
1207 cryptostats.cs_kerrs++;
1208
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)
1216 crypto_remove(cap);
1217 }
1218 CRYPTO_DRIVER_UNLOCK();
1219
1220 /*
1221 * CBIMM means unconditionally do the callback immediately;
1222 * This is used to avoid doing extraneous context switches
1223 */
1224 if ((krp->krp_flags & CRYPTO_KF_CBIMM)) {
1225 /*
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).
1229 */
1230 krp->krp_callback(krp);
1231 } else {
1232 unsigned long r_flags;
1233 /*
1234 * Normal case; queue the callback for the thread.
1235 */
1236 CRYPTO_RETQ_LOCK();
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();
1241 }
1242 }
1243
1244 int
1245 crypto_getfeat(int *featp)
1246 {
1247 int hid, kalg, feat = 0;
1248 unsigned long d_flags;
1249
1250 CRYPTO_DRIVER_LOCK();
1251 for (hid = 0; hid < crypto_drivers_num; hid++) {
1252 const struct cryptocap *cap = &crypto_drivers[hid];
1253
1254 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1255 !crypto_devallowsoft) {
1256 continue;
1257 }
1258 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1259 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1260 feat |= 1 << kalg;
1261 }
1262 CRYPTO_DRIVER_UNLOCK();
1263 *featp = feat;
1264 return (0);
1265 }
1266
1267 /*
1268 * Crypto thread, dispatches crypto requests.
1269 */
1270 static int
1271 crypto_proc(void *arg)
1272 {
1273 struct cryptop *crp, *submit;
1274 struct cryptkop *krp, *krpp;
1275 struct cryptocap *cap;
1276 u_int32_t hid;
1277 int result, hint;
1278 unsigned long q_flags;
1279 int loopcount = 0;
1280
1281 ocf_daemonize("crypto");
1282
1283 CRYPTO_Q_LOCK();
1284 for (;;) {
1285 /*
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.
1290 */
1291 crypto_all_qblocked = !list_empty(&crp_q);
1292
1293 /*
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.
1297 */
1298 submit = NULL;
1299 hint = 0;
1300 list_for_each_entry(crp, &crp_q, crp_next) {
1301 hid = CRYPTO_SESID2HID(crp->crp_sid);
1302 cap = crypto_checkdriver(hid);
1303 /*
1304 * Driver cannot disappear when there is an active
1305 * session.
1306 */
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. */
1311 if (submit == NULL)
1312 submit = crp;
1313 break;
1314 }
1315 if (!cap->cc_qblocked) {
1316 if (submit != NULL) {
1317 /*
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
1323 * queue instead.
1324 */
1325 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1326 hint = CRYPTO_HINT_MORE;
1327 break;
1328 } else {
1329 submit = crp;
1330 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1331 break;
1332 /* keep scanning for more are q'd */
1333 }
1334 }
1335 }
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);
1342 CRYPTO_Q_UNLOCK();
1343 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1344 __func__, __LINE__));
1345 result = crypto_invoke(cap, submit, hint);
1346 CRYPTO_Q_LOCK();
1347 if (result == ERESTART) {
1348 /*
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.
1356 */
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;
1363 }
1364 crypto_drivers[hid].cc_unqblocked = 0;
1365 }
1366
1367 crypto_all_kqblocked = !list_empty(&crp_kq);
1368
1369 /* As above, but for key ops */
1370 krp = NULL;
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) {
1374 /*
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.
1379 */
1380 krp->krp_hid = krp->krp_crid &
1381 (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1382 if (krp->krp_hid == 0)
1383 krp->krp_hid =
1384 CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1385 break;
1386 }
1387 if (!cap->cc_kqblocked) {
1388 krp = krpp;
1389 break;
1390 }
1391 }
1392 if (krp != NULL) {
1393 crypto_all_kqblocked = 0;
1394 list_del(&krp->krp_next);
1395 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1396 CRYPTO_Q_UNLOCK();
1397 result = crypto_kinvoke(krp, krp->krp_hid);
1398 CRYPTO_Q_LOCK();
1399 if (result == ERESTART) {
1400 /*
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.
1408 */
1409 /* XXX validate sid again? */
1410 list_add(&krp->krp_next, &crp_kq);
1411 cryptostats.cs_kblocks++;
1412 } else
1413 crypto_drivers[krp->krp_hid].cc_kqblocked = 0;
1414 }
1415
1416 if (submit == NULL && krp == NULL) {
1417 /*
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.
1428 */
1429 dprintk("%s - sleeping (qe=%d qb=%d kqe=%d kqb=%d)\n",
1430 __FUNCTION__,
1431 list_empty(&crp_q), crypto_all_qblocked,
1432 list_empty(&crp_kq), crypto_all_kqblocked);
1433 loopcount = 0;
1434 CRYPTO_Q_UNLOCK();
1435 crp_sleep = 1;
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);
1440 crp_sleep = 0;
1441 if (signal_pending (current)) {
1442 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1443 spin_lock_irq(&current->sigmask_lock);
1444 #endif
1445 flush_signals(current);
1446 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1447 spin_unlock_irq(&current->sigmask_lock);
1448 #endif
1449 }
1450 CRYPTO_Q_LOCK();
1451 dprintk("%s - awake\n", __FUNCTION__);
1452 if (cryptoproc == (pid_t) -1)
1453 break;
1454 cryptostats.cs_intrs++;
1455 } else if (loopcount > crypto_max_loopcount) {
1456 /*
1457 * Give other processes a chance to run if we've
1458 * been using the CPU exclusively for a while.
1459 */
1460 loopcount = 0;
1461 schedule();
1462 }
1463 loopcount++;
1464 }
1465 CRYPTO_Q_UNLOCK();
1466 complete_and_exit(&cryptoproc_exited, 0);
1467 }
1468
1469 /*
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.
1473 */
1474 static int
1475 crypto_ret_proc(void *arg)
1476 {
1477 struct cryptop *crpt;
1478 struct cryptkop *krpt;
1479 unsigned long r_flags;
1480
1481 ocf_daemonize("crypto_ret");
1482
1483 CRYPTO_RETQ_LOCK();
1484 for (;;) {
1485 /* Harvest return q's for completed ops */
1486 crpt = NULL;
1487 if (!list_empty(&crp_ret_q))
1488 crpt = list_entry(crp_ret_q.next, typeof(*crpt), crp_next);
1489 if (crpt != NULL)
1490 list_del(&crpt->crp_next);
1491
1492 krpt = NULL;
1493 if (!list_empty(&crp_ret_kq))
1494 krpt = list_entry(crp_ret_kq.next, typeof(*krpt), krp_next);
1495 if (krpt != NULL)
1496 list_del(&krpt->krp_next);
1497
1498 if (crpt != NULL || krpt != NULL) {
1499 CRYPTO_RETQ_UNLOCK();
1500 /*
1501 * Run callbacks unlocked.
1502 */
1503 if (crpt != NULL)
1504 crpt->crp_callback(crpt);
1505 if (krpt != NULL)
1506 krpt->krp_callback(krpt);
1507 CRYPTO_RETQ_LOCK();
1508 } else {
1509 /*
1510 * Nothing more to be processed. Sleep until we're
1511 * woken because there are more returns to process.
1512 */
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(&current->sigmask_lock);
1522 #endif
1523 flush_signals(current);
1524 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1525 spin_unlock_irq(&current->sigmask_lock);
1526 #endif
1527 }
1528 CRYPTO_RETQ_LOCK();
1529 dprintk("%s - awake\n", __FUNCTION__);
1530 if (cryptoretproc == (pid_t) -1) {
1531 dprintk("%s - EXITING!\n", __FUNCTION__);
1532 break;
1533 }
1534 cryptostats.cs_rets++;
1535 }
1536 }
1537 CRYPTO_RETQ_UNLOCK();
1538 complete_and_exit(&cryptoretproc_exited, 0);
1539 }
1540
1541
1542 #if 0 /* should put this into /proc or something */
1543 static void
1544 db_show_drivers(void)
1545 {
1546 int hid;
1547
1548 db_printf("%12s %4s %4s %8s %2s %2s\n"
1549 , "Device"
1550 , "Ses"
1551 , "Kops"
1552 , "Flags"
1553 , "QB"
1554 , "KB"
1555 );
1556 for (hid = 0; hid < crypto_drivers_num; hid++) {
1557 const struct cryptocap *cap = &crypto_drivers[hid];
1558 if (cap->cc_dev == NULL)
1559 continue;
1560 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1561 , device_get_nameunit(cap->cc_dev)
1562 , cap->cc_sessions
1563 , cap->cc_koperations
1564 , cap->cc_flags
1565 , cap->cc_qblocked
1566 , cap->cc_kqblocked
1567 );
1568 }
1569 }
1570
1571 DB_SHOW_COMMAND(crypto, db_show_crypto)
1572 {
1573 struct cryptop *crp;
1574
1575 db_show_drivers();
1576 db_printf("\n");
1577
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
1586 , crp->crp_etype
1587 , crp->crp_flags
1588 , crp->crp_desc
1589 , crp->crp_callback
1590 );
1591 }
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)
1598 , crp->crp_etype
1599 , crp->crp_flags
1600 , crp->crp_callback
1601 );
1602 }
1603 }
1604 }
1605
1606 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1607 {
1608 struct cryptkop *krp;
1609
1610 db_show_drivers();
1611 db_printf("\n");
1612
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"
1617 , krp->krp_op
1618 , krp->krp_status
1619 , krp->krp_iparams, krp->krp_oparams
1620 , krp->krp_crid, krp->krp_hid
1621 , krp->krp_callback
1622 );
1623 }
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"
1629 , krp->krp_op
1630 , krp->krp_status
1631 , krp->krp_crid, krp->krp_hid
1632 , krp->krp_callback
1633 );
1634 }
1635 }
1636 }
1637 #endif
1638
1639
1640 static int
1641 crypto_init(void)
1642 {
1643 int error;
1644
1645 dprintk("%s(%p)\n", __FUNCTION__, (void *) crypto_init);
1646
1647 if (crypto_initted)
1648 return 0;
1649 crypto_initted = 1;
1650
1651 spin_lock_init(&crypto_drivers_lock);
1652 spin_lock_init(&crypto_q_lock);
1653 spin_lock_init(&crypto_ret_q_lock);
1654
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)
1658 , NULL
1659 #endif
1660 );
1661
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)
1665 , NULL
1666 #endif
1667 );
1668
1669 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
1670 printk("crypto: crypto_init cannot setup crypto zones\n");
1671 error = ENOMEM;
1672 goto bad;
1673 }
1674
1675 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
1676 crypto_drivers = kmalloc(crypto_drivers_num * sizeof(struct cryptocap),
1677 GFP_KERNEL);
1678 if (crypto_drivers == NULL) {
1679 printk("crypto: crypto_init cannot setup crypto drivers\n");
1680 error = ENOMEM;
1681 goto bad;
1682 }
1683
1684 memset(crypto_drivers, 0, crypto_drivers_num * sizeof(struct cryptocap));
1685
1686 init_completion(&cryptoproc_exited);
1687 init_completion(&cryptoretproc_exited);
1688
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) {
1692 error = cryptoproc;
1693 printk("crypto: crypto_init cannot start crypto thread; error %d",
1694 error);
1695 goto bad;
1696 }
1697
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",
1703 error);
1704 goto bad;
1705 }
1706
1707 return 0;
1708 bad:
1709 crypto_exit();
1710 return error;
1711 }
1712
1713
1714 static void
1715 crypto_exit(void)
1716 {
1717 pid_t p;
1718 unsigned long d_flags;
1719
1720 dprintk("%s()\n", __FUNCTION__);
1721
1722 /*
1723 * Terminate any crypto threads.
1724 */
1725
1726 CRYPTO_DRIVER_LOCK();
1727 p = cryptoproc;
1728 cryptoproc = (pid_t) -1;
1729 kill_proc(p, SIGTERM, 1);
1730 wake_up_interruptible(&cryptoproc_wait);
1731 CRYPTO_DRIVER_UNLOCK();
1732
1733 wait_for_completion(&cryptoproc_exited);
1734
1735 CRYPTO_DRIVER_LOCK();
1736 p = cryptoretproc;
1737 cryptoretproc = (pid_t) -1;
1738 kill_proc(p, SIGTERM, 1);
1739 wake_up_interruptible(&cryptoretproc_wait);
1740 CRYPTO_DRIVER_UNLOCK();
1741
1742 wait_for_completion(&cryptoretproc_exited);
1743
1744 /* XXX flush queues??? */
1745
1746 /*
1747 * Reclaim dynamically allocated resources.
1748 */
1749 if (crypto_drivers != NULL)
1750 kfree(crypto_drivers);
1751
1752 if (cryptodesc_zone != NULL)
1753 kmem_cache_destroy(cryptodesc_zone);
1754 if (cryptop_zone != NULL)
1755 kmem_cache_destroy(cryptop_zone);
1756 }
1757
1758
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);
1778
1779 module_init(crypto_init);
1780 module_exit(crypto_exit);
1781
1782 MODULE_LICENSE("BSD");
1783 MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
1784 MODULE_DESCRIPTION("OCF (OpenBSD Cryptographic Framework)");