package/grub: only disable graphics on host grub
[openwrt.git] / target / linux / generic-2.6 / files / crypto / ocf / crypto.c
1 /*-
2 * Linux port done by David McCullough <david_mccullough@securecomputing.com>
3 * Copyright (C) 2006-2007 David McCullough
4 * Copyright (C) 2004-2005 Intel Corporation.
5 * The license and original author are listed below.
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 static struct cryptocap *crypto_drivers = NULL;
144 static int crypto_drivers_num = 0;
145
146 /*
147 * There are two queues for crypto requests; one for symmetric (e.g.
148 * cipher) operations and one for asymmetric (e.g. MOD)operations.
149 * A single mutex is used to lock access to both queues. We could
150 * have one per-queue but having one simplifies handling of block/unblock
151 * operations.
152 */
153 static int crp_sleep = 0;
154 static LIST_HEAD(crp_q); /* request queues */
155 static LIST_HEAD(crp_kq);
156
157 static spinlock_t crypto_q_lock;
158
159 int crypto_all_qblocked = 0; /* protect with Q_LOCK */
160 module_param(crypto_all_qblocked, int, 0444);
161 MODULE_PARM_DESC(crypto_all_qblocked, "Are all crypto queues blocked");
162
163 int crypto_all_kqblocked = 0; /* protect with Q_LOCK */
164 module_param(crypto_all_kqblocked, int, 0444);
165 MODULE_PARM_DESC(crypto_all_kqblocked, "Are all asym crypto queues blocked");
166
167 #define CRYPTO_Q_LOCK() \
168 ({ \
169 spin_lock_irqsave(&crypto_q_lock, q_flags); \
170 dprintk("%s,%d: Q_LOCK()\n", __FILE__, __LINE__); \
171 })
172 #define CRYPTO_Q_UNLOCK() \
173 ({ \
174 dprintk("%s,%d: Q_UNLOCK()\n", __FILE__, __LINE__); \
175 spin_unlock_irqrestore(&crypto_q_lock, q_flags); \
176 })
177
178 /*
179 * There are two queues for processing completed crypto requests; one
180 * for the symmetric and one for the asymmetric ops. We only need one
181 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
182 * mutex is used to lock access to both queues. Note that this lock
183 * must be separate from the lock on request queues to insure driver
184 * callbacks don't generate lock order reversals.
185 */
186 static LIST_HEAD(crp_ret_q); /* callback queues */
187 static LIST_HEAD(crp_ret_kq);
188
189 static spinlock_t crypto_ret_q_lock;
190 #define CRYPTO_RETQ_LOCK() \
191 ({ \
192 spin_lock_irqsave(&crypto_ret_q_lock, r_flags); \
193 dprintk("%s,%d: RETQ_LOCK\n", __FILE__, __LINE__); \
194 })
195 #define CRYPTO_RETQ_UNLOCK() \
196 ({ \
197 dprintk("%s,%d: RETQ_UNLOCK\n", __FILE__, __LINE__); \
198 spin_unlock_irqrestore(&crypto_ret_q_lock, r_flags); \
199 })
200 #define CRYPTO_RETQ_EMPTY() (list_empty(&crp_ret_q) && list_empty(&crp_ret_kq))
201
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
203 static kmem_cache_t *cryptop_zone;
204 static kmem_cache_t *cryptodesc_zone;
205 #else
206 static struct kmem_cache *cryptop_zone;
207 static struct kmem_cache *cryptodesc_zone;
208 #endif
209
210 #define debug crypto_debug
211 int crypto_debug = 0;
212 module_param(crypto_debug, int, 0644);
213 MODULE_PARM_DESC(crypto_debug, "Enable debug");
214 EXPORT_SYMBOL(crypto_debug);
215
216 /*
217 * Maximum number of outstanding crypto requests before we start
218 * failing requests. We need this to prevent DOS when too many
219 * requests are arriving for us to keep up. Otherwise we will
220 * run the system out of memory. Since crypto is slow, we are
221 * usually the bottleneck that needs to say, enough is enough.
222 *
223 * We cannot print errors when this condition occurs, we are already too
224 * slow, printing anything will just kill us
225 */
226
227 static int crypto_q_cnt = 0;
228 module_param(crypto_q_cnt, int, 0444);
229 MODULE_PARM_DESC(crypto_q_cnt,
230 "Current number of outstanding crypto requests");
231
232 static int crypto_q_max = 1000;
233 module_param(crypto_q_max, int, 0644);
234 MODULE_PARM_DESC(crypto_q_max,
235 "Maximum number of outstanding crypto requests");
236
237 #define bootverbose crypto_verbose
238 static int crypto_verbose = 0;
239 module_param(crypto_verbose, int, 0644);
240 MODULE_PARM_DESC(crypto_verbose,
241 "Enable verbose crypto startup");
242
243 int crypto_usercrypto = 1; /* userland may do crypto reqs */
244 module_param(crypto_usercrypto, int, 0644);
245 MODULE_PARM_DESC(crypto_usercrypto,
246 "Enable/disable user-mode access to crypto support");
247
248 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
249 module_param(crypto_userasymcrypto, int, 0644);
250 MODULE_PARM_DESC(crypto_userasymcrypto,
251 "Enable/disable user-mode access to asymmetric crypto support");
252
253 int crypto_devallowsoft = 0; /* only use hardware crypto */
254 module_param(crypto_devallowsoft, int, 0644);
255 MODULE_PARM_DESC(crypto_devallowsoft,
256 "Enable/disable use of software crypto support");
257
258 static pid_t cryptoproc = (pid_t) -1;
259 static struct completion cryptoproc_exited;
260 static DECLARE_WAIT_QUEUE_HEAD(cryptoproc_wait);
261 static pid_t cryptoretproc = (pid_t) -1;
262 static struct completion cryptoretproc_exited;
263 static DECLARE_WAIT_QUEUE_HEAD(cryptoretproc_wait);
264
265 static int crypto_proc(void *arg);
266 static int crypto_ret_proc(void *arg);
267 static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
268 static int crypto_kinvoke(struct cryptkop *krp, int flags);
269 static void crypto_exit(void);
270 static int crypto_init(void);
271
272 static struct cryptostats cryptostats;
273
274 static struct cryptocap *
275 crypto_checkdriver(u_int32_t hid)
276 {
277 if (crypto_drivers == NULL)
278 return NULL;
279 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
280 }
281
282 /*
283 * Compare a driver's list of supported algorithms against another
284 * list; return non-zero if all algorithms are supported.
285 */
286 static int
287 driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
288 {
289 const struct cryptoini *cr;
290
291 /* See if all the algorithms are supported. */
292 for (cr = cri; cr; cr = cr->cri_next)
293 if (cap->cc_alg[cr->cri_alg] == 0)
294 return 0;
295 return 1;
296 }
297
298 /*
299 * Select a driver for a new session that supports the specified
300 * algorithms and, optionally, is constrained according to the flags.
301 * The algorithm we use here is pretty stupid; just use the
302 * first driver that supports all the algorithms we need. If there
303 * are multiple drivers we choose the driver with the fewest active
304 * sessions. We prefer hardware-backed drivers to software ones.
305 *
306 * XXX We need more smarts here (in real life too, but that's
307 * XXX another story altogether).
308 */
309 static struct cryptocap *
310 crypto_select_driver(const struct cryptoini *cri, int flags)
311 {
312 struct cryptocap *cap, *best;
313 int match, hid;
314
315 CRYPTO_DRIVER_ASSERT();
316
317 /*
318 * Look first for hardware crypto devices if permitted.
319 */
320 if (flags & CRYPTOCAP_F_HARDWARE)
321 match = CRYPTOCAP_F_HARDWARE;
322 else
323 match = CRYPTOCAP_F_SOFTWARE;
324 best = NULL;
325 again:
326 for (hid = 0; hid < crypto_drivers_num; hid++) {
327 cap = &crypto_drivers[hid];
328 /*
329 * If it's not initialized, is in the process of
330 * going away, or is not appropriate (hardware
331 * or software based on match), then skip.
332 */
333 if (cap->cc_dev == NULL ||
334 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
335 (cap->cc_flags & match) == 0)
336 continue;
337
338 /* verify all the algorithms are supported. */
339 if (driver_suitable(cap, cri)) {
340 if (best == NULL ||
341 cap->cc_sessions < best->cc_sessions)
342 best = cap;
343 }
344 }
345 if (best != NULL)
346 return best;
347 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
348 /* sort of an Algol 68-style for loop */
349 match = CRYPTOCAP_F_SOFTWARE;
350 goto again;
351 }
352 return best;
353 }
354
355 /*
356 * Create a new session. The crid argument specifies a crypto
357 * driver to use or constraints on a driver to select (hardware
358 * only, software only, either). Whatever driver is selected
359 * must be capable of the requested crypto algorithms.
360 */
361 int
362 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
363 {
364 struct cryptocap *cap;
365 u_int32_t hid, lid;
366 int err;
367 unsigned long d_flags;
368
369 CRYPTO_DRIVER_LOCK();
370 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
371 /*
372 * Use specified driver; verify it is capable.
373 */
374 cap = crypto_checkdriver(crid);
375 if (cap != NULL && !driver_suitable(cap, cri))
376 cap = NULL;
377 } else {
378 /*
379 * No requested driver; select based on crid flags.
380 */
381 cap = crypto_select_driver(cri, crid);
382 /*
383 * if NULL then can't do everything in one session.
384 * XXX Fix this. We need to inject a "virtual" session
385 * XXX layer right about here.
386 */
387 }
388 if (cap != NULL) {
389 /* Call the driver initialization routine. */
390 hid = cap - crypto_drivers;
391 lid = hid; /* Pass the driver ID. */
392 cap->cc_sessions++;
393 CRYPTO_DRIVER_UNLOCK();
394 err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
395 CRYPTO_DRIVER_LOCK();
396 if (err == 0) {
397 (*sid) = (cap->cc_flags & 0xff000000)
398 | (hid & 0x00ffffff);
399 (*sid) <<= 32;
400 (*sid) |= (lid & 0xffffffff);
401 } else
402 cap->cc_sessions--;
403 } else
404 err = EINVAL;
405 CRYPTO_DRIVER_UNLOCK();
406 return err;
407 }
408
409 static void
410 crypto_remove(struct cryptocap *cap)
411 {
412 CRYPTO_DRIVER_ASSERT();
413 if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
414 bzero(cap, sizeof(*cap));
415 }
416
417 /*
418 * Delete an existing session (or a reserved session on an unregistered
419 * driver).
420 */
421 int
422 crypto_freesession(u_int64_t sid)
423 {
424 struct cryptocap *cap;
425 u_int32_t hid;
426 int err = 0;
427 unsigned long d_flags;
428
429 dprintk("%s()\n", __FUNCTION__);
430 CRYPTO_DRIVER_LOCK();
431
432 if (crypto_drivers == NULL) {
433 err = EINVAL;
434 goto done;
435 }
436
437 /* Determine two IDs. */
438 hid = CRYPTO_SESID2HID(sid);
439
440 if (hid >= crypto_drivers_num) {
441 dprintk("%s - INVALID DRIVER NUM %d\n", __FUNCTION__, hid);
442 err = ENOENT;
443 goto done;
444 }
445 cap = &crypto_drivers[hid];
446
447 if (cap->cc_dev) {
448 CRYPTO_DRIVER_UNLOCK();
449 /* Call the driver cleanup routine, if available, unlocked. */
450 err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
451 CRYPTO_DRIVER_LOCK();
452 }
453
454 if (cap->cc_sessions)
455 cap->cc_sessions--;
456
457 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
458 crypto_remove(cap);
459
460 done:
461 CRYPTO_DRIVER_UNLOCK();
462 return err;
463 }
464
465 /*
466 * Return an unused driver id. Used by drivers prior to registering
467 * support for the algorithms they handle.
468 */
469 int32_t
470 crypto_get_driverid(device_t dev, int flags)
471 {
472 struct cryptocap *newdrv;
473 int i;
474 unsigned long d_flags;
475
476 if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
477 printf("%s: no flags specified when registering driver\n",
478 device_get_nameunit(dev));
479 return -1;
480 }
481
482 CRYPTO_DRIVER_LOCK();
483
484 for (i = 0; i < crypto_drivers_num; i++) {
485 if (crypto_drivers[i].cc_dev == NULL &&
486 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
487 break;
488 }
489 }
490
491 /* Out of entries, allocate some more. */
492 if (i == crypto_drivers_num) {
493 /* Be careful about wrap-around. */
494 if (2 * crypto_drivers_num <= crypto_drivers_num) {
495 CRYPTO_DRIVER_UNLOCK();
496 printk("crypto: driver count wraparound!\n");
497 return -1;
498 }
499
500 newdrv = kmalloc(2 * crypto_drivers_num * sizeof(struct cryptocap),
501 GFP_KERNEL);
502 if (newdrv == NULL) {
503 CRYPTO_DRIVER_UNLOCK();
504 printk("crypto: no space to expand driver table!\n");
505 return -1;
506 }
507
508 memcpy(newdrv, crypto_drivers,
509 crypto_drivers_num * sizeof(struct cryptocap));
510 memset(&newdrv[crypto_drivers_num], 0,
511 crypto_drivers_num * sizeof(struct cryptocap));
512
513 crypto_drivers_num *= 2;
514
515 kfree(crypto_drivers);
516 crypto_drivers = newdrv;
517 }
518
519 /* NB: state is zero'd on free */
520 crypto_drivers[i].cc_sessions = 1; /* Mark */
521 crypto_drivers[i].cc_dev = dev;
522 crypto_drivers[i].cc_flags = flags;
523 if (bootverbose)
524 printf("crypto: assign %s driver id %u, flags %u\n",
525 device_get_nameunit(dev), i, flags);
526
527 CRYPTO_DRIVER_UNLOCK();
528
529 return i;
530 }
531
532 /*
533 * Lookup a driver by name. We match against the full device
534 * name and unit, and against just the name. The latter gives
535 * us a simple widlcarding by device name. On success return the
536 * driver/hardware identifier; otherwise return -1.
537 */
538 int
539 crypto_find_driver(const char *match)
540 {
541 int i, len = strlen(match);
542 unsigned long d_flags;
543
544 CRYPTO_DRIVER_LOCK();
545 for (i = 0; i < crypto_drivers_num; i++) {
546 device_t dev = crypto_drivers[i].cc_dev;
547 if (dev == NULL ||
548 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
549 continue;
550 if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
551 strncmp(match, device_get_name(dev), len) == 0)
552 break;
553 }
554 CRYPTO_DRIVER_UNLOCK();
555 return i < crypto_drivers_num ? i : -1;
556 }
557
558 /*
559 * Return the device_t for the specified driver or NULL
560 * if the driver identifier is invalid.
561 */
562 device_t
563 crypto_find_device_byhid(int hid)
564 {
565 struct cryptocap *cap = crypto_checkdriver(hid);
566 return cap != NULL ? cap->cc_dev : NULL;
567 }
568
569 /*
570 * Return the device/driver capabilities.
571 */
572 int
573 crypto_getcaps(int hid)
574 {
575 struct cryptocap *cap = crypto_checkdriver(hid);
576 return cap != NULL ? cap->cc_flags : 0;
577 }
578
579 /*
580 * Register support for a key-related algorithm. This routine
581 * is called once for each algorithm supported a driver.
582 */
583 int
584 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
585 {
586 struct cryptocap *cap;
587 int err;
588 unsigned long d_flags;
589
590 dprintk("%s()\n", __FUNCTION__);
591 CRYPTO_DRIVER_LOCK();
592
593 cap = crypto_checkdriver(driverid);
594 if (cap != NULL &&
595 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
596 /*
597 * XXX Do some performance testing to determine placing.
598 * XXX We probably need an auxiliary data structure that
599 * XXX describes relative performances.
600 */
601
602 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
603 if (bootverbose)
604 printf("crypto: %s registers key alg %u flags %u\n"
605 , device_get_nameunit(cap->cc_dev)
606 , kalg
607 , flags
608 );
609 err = 0;
610 } else
611 err = EINVAL;
612
613 CRYPTO_DRIVER_UNLOCK();
614 return err;
615 }
616
617 /*
618 * Register support for a non-key-related algorithm. This routine
619 * is called once for each such algorithm supported by a driver.
620 */
621 int
622 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
623 u_int32_t flags)
624 {
625 struct cryptocap *cap;
626 int err;
627 unsigned long d_flags;
628
629 dprintk("%s(id=0x%x, alg=%d, maxoplen=%d, flags=0x%x)\n", __FUNCTION__,
630 driverid, alg, maxoplen, flags);
631
632 CRYPTO_DRIVER_LOCK();
633
634 cap = crypto_checkdriver(driverid);
635 /* NB: algorithms are in the range [1..max] */
636 if (cap != NULL &&
637 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
638 /*
639 * XXX Do some performance testing to determine placing.
640 * XXX We probably need an auxiliary data structure that
641 * XXX describes relative performances.
642 */
643
644 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
645 cap->cc_max_op_len[alg] = maxoplen;
646 if (bootverbose)
647 printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
648 , device_get_nameunit(cap->cc_dev)
649 , alg
650 , flags
651 , maxoplen
652 );
653 cap->cc_sessions = 0; /* Unmark */
654 err = 0;
655 } else
656 err = EINVAL;
657
658 CRYPTO_DRIVER_UNLOCK();
659 return err;
660 }
661
662 static void
663 driver_finis(struct cryptocap *cap)
664 {
665 u_int32_t ses, kops;
666
667 CRYPTO_DRIVER_ASSERT();
668
669 ses = cap->cc_sessions;
670 kops = cap->cc_koperations;
671 bzero(cap, sizeof(*cap));
672 if (ses != 0 || kops != 0) {
673 /*
674 * If there are pending sessions,
675 * just mark as invalid.
676 */
677 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
678 cap->cc_sessions = ses;
679 cap->cc_koperations = kops;
680 }
681 }
682
683 /*
684 * Unregister a crypto driver. If there are pending sessions using it,
685 * leave enough information around so that subsequent calls using those
686 * sessions will correctly detect the driver has been unregistered and
687 * reroute requests.
688 */
689 int
690 crypto_unregister(u_int32_t driverid, int alg)
691 {
692 struct cryptocap *cap;
693 int i, err;
694 unsigned long d_flags;
695
696 dprintk("%s()\n", __FUNCTION__);
697 CRYPTO_DRIVER_LOCK();
698
699 cap = crypto_checkdriver(driverid);
700 if (cap != NULL &&
701 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
702 cap->cc_alg[alg] != 0) {
703 cap->cc_alg[alg] = 0;
704 cap->cc_max_op_len[alg] = 0;
705
706 /* Was this the last algorithm ? */
707 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
708 if (cap->cc_alg[i] != 0)
709 break;
710
711 if (i == CRYPTO_ALGORITHM_MAX + 1)
712 driver_finis(cap);
713 err = 0;
714 } else
715 err = EINVAL;
716 CRYPTO_DRIVER_UNLOCK();
717 return err;
718 }
719
720 /*
721 * Unregister all algorithms associated with a crypto driver.
722 * If there are pending sessions using it, leave enough information
723 * around so that subsequent calls using those sessions will
724 * correctly detect the driver has been unregistered and reroute
725 * requests.
726 */
727 int
728 crypto_unregister_all(u_int32_t driverid)
729 {
730 struct cryptocap *cap;
731 int err;
732 unsigned long d_flags;
733
734 dprintk("%s()\n", __FUNCTION__);
735 CRYPTO_DRIVER_LOCK();
736 cap = crypto_checkdriver(driverid);
737 if (cap != NULL) {
738 driver_finis(cap);
739 err = 0;
740 } else
741 err = EINVAL;
742 CRYPTO_DRIVER_UNLOCK();
743
744 return err;
745 }
746
747 /*
748 * Clear blockage on a driver. The what parameter indicates whether
749 * the driver is now ready for cryptop's and/or cryptokop's.
750 */
751 int
752 crypto_unblock(u_int32_t driverid, int what)
753 {
754 struct cryptocap *cap;
755 int err;
756 unsigned long q_flags;
757
758 CRYPTO_Q_LOCK();
759 cap = crypto_checkdriver(driverid);
760 if (cap != NULL) {
761 if (what & CRYPTO_SYMQ) {
762 cap->cc_qblocked = 0;
763 crypto_all_qblocked = 0;
764 }
765 if (what & CRYPTO_ASYMQ) {
766 cap->cc_kqblocked = 0;
767 crypto_all_kqblocked = 0;
768 }
769 if (crp_sleep)
770 wake_up_interruptible(&cryptoproc_wait);
771 err = 0;
772 } else
773 err = EINVAL;
774 CRYPTO_Q_UNLOCK(); //DAVIDM should this be a driver lock
775
776 return err;
777 }
778
779 /*
780 * Add a crypto request to a queue, to be processed by the kernel thread.
781 */
782 int
783 crypto_dispatch(struct cryptop *crp)
784 {
785 struct cryptocap *cap;
786 int result = -1;
787 unsigned long q_flags;
788
789 dprintk("%s()\n", __FUNCTION__);
790
791 cryptostats.cs_ops++;
792
793 CRYPTO_Q_LOCK();
794 if (crypto_q_cnt >= crypto_q_max) {
795 CRYPTO_Q_UNLOCK();
796 cryptostats.cs_drops++;
797 return ENOMEM;
798 }
799 crypto_q_cnt++;
800
801 /*
802 * Caller marked the request to be processed immediately; dispatch
803 * it directly to the driver unless the driver is currently blocked.
804 */
805 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
806 int hid = CRYPTO_SESID2HID(crp->crp_sid);
807 cap = crypto_checkdriver(hid);
808 /* Driver cannot disappear when there is an active session. */
809 KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
810 if (!cap->cc_qblocked) {
811 crypto_all_qblocked = 0;
812 crypto_drivers[hid].cc_qblocked = 1;
813 CRYPTO_Q_UNLOCK();
814 result = crypto_invoke(cap, crp, 0);
815 CRYPTO_Q_LOCK();
816 if (result != ERESTART)
817 crypto_drivers[hid].cc_qblocked = 0;
818 }
819 }
820 if (result == ERESTART) {
821 /*
822 * The driver ran out of resources, mark the
823 * driver ``blocked'' for cryptop's and put
824 * the request back in the queue. It would
825 * best to put the request back where we got
826 * it but that's hard so for now we put it
827 * at the front. This should be ok; putting
828 * it at the end does not work.
829 */
830 list_add(&crp->crp_next, &crp_q);
831 cryptostats.cs_blocks++;
832 } else if (result == -1) {
833 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
834 }
835 if (crp_sleep)
836 wake_up_interruptible(&cryptoproc_wait);
837 CRYPTO_Q_UNLOCK();
838 return 0;
839 }
840
841 /*
842 * Add an asymetric crypto request to a queue,
843 * to be processed by the kernel thread.
844 */
845 int
846 crypto_kdispatch(struct cryptkop *krp)
847 {
848 int error;
849 unsigned long q_flags;
850
851 cryptostats.cs_kops++;
852
853 error = crypto_kinvoke(krp, krp->krp_crid);
854 if (error == ERESTART) {
855 CRYPTO_Q_LOCK();
856 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
857 if (crp_sleep)
858 wake_up_interruptible(&cryptoproc_wait);
859 CRYPTO_Q_UNLOCK();
860 error = 0;
861 }
862 return error;
863 }
864
865 /*
866 * Verify a driver is suitable for the specified operation.
867 */
868 static __inline int
869 kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
870 {
871 return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
872 }
873
874 /*
875 * Select a driver for an asym operation. The driver must
876 * support the necessary algorithm. The caller can constrain
877 * which device is selected with the flags parameter. The
878 * algorithm we use here is pretty stupid; just use the first
879 * driver that supports the algorithms we need. If there are
880 * multiple suitable drivers we choose the driver with the
881 * fewest active operations. We prefer hardware-backed
882 * drivers to software ones when either may be used.
883 */
884 static struct cryptocap *
885 crypto_select_kdriver(const struct cryptkop *krp, int flags)
886 {
887 struct cryptocap *cap, *best, *blocked;
888 int match, hid;
889
890 CRYPTO_DRIVER_ASSERT();
891
892 /*
893 * Look first for hardware crypto devices if permitted.
894 */
895 if (flags & CRYPTOCAP_F_HARDWARE)
896 match = CRYPTOCAP_F_HARDWARE;
897 else
898 match = CRYPTOCAP_F_SOFTWARE;
899 best = NULL;
900 blocked = NULL;
901 again:
902 for (hid = 0; hid < crypto_drivers_num; hid++) {
903 cap = &crypto_drivers[hid];
904 /*
905 * If it's not initialized, is in the process of
906 * going away, or is not appropriate (hardware
907 * or software based on match), then skip.
908 */
909 if (cap->cc_dev == NULL ||
910 (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
911 (cap->cc_flags & match) == 0)
912 continue;
913
914 /* verify all the algorithms are supported. */
915 if (kdriver_suitable(cap, krp)) {
916 if (best == NULL ||
917 cap->cc_koperations < best->cc_koperations)
918 best = cap;
919 }
920 }
921 if (best != NULL)
922 return best;
923 if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
924 /* sort of an Algol 68-style for loop */
925 match = CRYPTOCAP_F_SOFTWARE;
926 goto again;
927 }
928 return best;
929 }
930
931 /*
932 * Dispatch an assymetric crypto request.
933 */
934 static int
935 crypto_kinvoke(struct cryptkop *krp, int crid)
936 {
937 struct cryptocap *cap = NULL;
938 int error;
939 unsigned long d_flags;
940
941 KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
942 KASSERT(krp->krp_callback != NULL,
943 ("%s: krp->crp_callback == NULL", __func__));
944
945 CRYPTO_DRIVER_LOCK();
946 if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
947 cap = crypto_checkdriver(crid);
948 if (cap != NULL) {
949 /*
950 * Driver present, it must support the necessary
951 * algorithm and, if s/w drivers are excluded,
952 * it must be registered as hardware-backed.
953 */
954 if (!kdriver_suitable(cap, krp) ||
955 (!crypto_devallowsoft &&
956 (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
957 cap = NULL;
958 }
959 } else {
960 /*
961 * No requested driver; select based on crid flags.
962 */
963 if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
964 crid &= ~CRYPTOCAP_F_SOFTWARE;
965 cap = crypto_select_kdriver(krp, crid);
966 }
967 if (cap != NULL && !cap->cc_kqblocked) {
968 krp->krp_hid = cap - crypto_drivers;
969 cap->cc_koperations++;
970 CRYPTO_DRIVER_UNLOCK();
971 error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
972 CRYPTO_DRIVER_LOCK();
973 if (error == ERESTART) {
974 cap->cc_koperations--;
975 CRYPTO_DRIVER_UNLOCK();
976 return (error);
977 }
978 /* return the actual device used */
979 krp->krp_crid = krp->krp_hid;
980 } else {
981 /*
982 * NB: cap is !NULL if device is blocked; in
983 * that case return ERESTART so the operation
984 * is resubmitted if possible.
985 */
986 error = (cap == NULL) ? ENODEV : ERESTART;
987 }
988 CRYPTO_DRIVER_UNLOCK();
989
990 if (error) {
991 krp->krp_status = error;
992 crypto_kdone(krp);
993 }
994 return 0;
995 }
996
997
998 /*
999 * Dispatch a crypto request to the appropriate crypto devices.
1000 */
1001 static int
1002 crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
1003 {
1004 KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
1005 KASSERT(crp->crp_callback != NULL,
1006 ("%s: crp->crp_callback == NULL", __func__));
1007 KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
1008
1009 dprintk("%s()\n", __FUNCTION__);
1010
1011 #ifdef CRYPTO_TIMING
1012 if (crypto_timing)
1013 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
1014 #endif
1015 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
1016 struct cryptodesc *crd;
1017 u_int64_t nid;
1018
1019 /*
1020 * Driver has unregistered; migrate the session and return
1021 * an error to the caller so they'll resubmit the op.
1022 *
1023 * XXX: What if there are more already queued requests for this
1024 * session?
1025 */
1026 crypto_freesession(crp->crp_sid);
1027
1028 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
1029 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
1030
1031 /* XXX propagate flags from initial session? */
1032 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
1033 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
1034 crp->crp_sid = nid;
1035
1036 crp->crp_etype = EAGAIN;
1037 crypto_done(crp);
1038 return 0;
1039 } else {
1040 /*
1041 * Invoke the driver to process the request.
1042 */
1043 return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
1044 }
1045 }
1046
1047 /*
1048 * Release a set of crypto descriptors.
1049 */
1050 void
1051 crypto_freereq(struct cryptop *crp)
1052 {
1053 struct cryptodesc *crd;
1054
1055 if (crp == NULL)
1056 return;
1057
1058 #ifdef DIAGNOSTIC
1059 {
1060 struct cryptop *crp2;
1061 unsigned long q_flags;
1062
1063 CRYPTO_Q_LOCK();
1064 TAILQ_FOREACH(crp2, &crp_q, crp_next) {
1065 KASSERT(crp2 != crp,
1066 ("Freeing cryptop from the crypto queue (%p).",
1067 crp));
1068 }
1069 CRYPTO_Q_UNLOCK();
1070 CRYPTO_RETQ_LOCK();
1071 TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
1072 KASSERT(crp2 != crp,
1073 ("Freeing cryptop from the return queue (%p).",
1074 crp));
1075 }
1076 CRYPTO_RETQ_UNLOCK();
1077 }
1078 #endif
1079
1080 while ((crd = crp->crp_desc) != NULL) {
1081 crp->crp_desc = crd->crd_next;
1082 kmem_cache_free(cryptodesc_zone, crd);
1083 }
1084 kmem_cache_free(cryptop_zone, crp);
1085 }
1086
1087 /*
1088 * Acquire a set of crypto descriptors.
1089 */
1090 struct cryptop *
1091 crypto_getreq(int num)
1092 {
1093 struct cryptodesc *crd;
1094 struct cryptop *crp;
1095
1096 crp = kmem_cache_alloc(cryptop_zone, SLAB_ATOMIC);
1097 if (crp != NULL) {
1098 memset(crp, 0, sizeof(*crp));
1099 INIT_LIST_HEAD(&crp->crp_next);
1100 init_waitqueue_head(&crp->crp_waitq);
1101 while (num--) {
1102 crd = kmem_cache_alloc(cryptodesc_zone, SLAB_ATOMIC);
1103 if (crd == NULL) {
1104 crypto_freereq(crp);
1105 return NULL;
1106 }
1107 memset(crd, 0, sizeof(*crd));
1108 crd->crd_next = crp->crp_desc;
1109 crp->crp_desc = crd;
1110 }
1111 }
1112 return crp;
1113 }
1114
1115 /*
1116 * Invoke the callback on behalf of the driver.
1117 */
1118 void
1119 crypto_done(struct cryptop *crp)
1120 {
1121 unsigned long q_flags;
1122
1123 dprintk("%s()\n", __FUNCTION__);
1124 if ((crp->crp_flags & CRYPTO_F_DONE) == 0) {
1125 crp->crp_flags |= CRYPTO_F_DONE;
1126 CRYPTO_Q_LOCK();
1127 crypto_q_cnt--;
1128 CRYPTO_Q_UNLOCK();
1129 } else
1130 printk("crypto: crypto_done op already done, flags 0x%x",
1131 crp->crp_flags);
1132 if (crp->crp_etype != 0)
1133 cryptostats.cs_errs++;
1134 /*
1135 * CBIMM means unconditionally do the callback immediately;
1136 * CBIFSYNC means do the callback immediately only if the
1137 * operation was done synchronously. Both are used to avoid
1138 * doing extraneous context switches; the latter is mostly
1139 * used with the software crypto driver.
1140 */
1141 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
1142 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
1143 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
1144 /*
1145 * Do the callback directly. This is ok when the
1146 * callback routine does very little (e.g. the
1147 * /dev/crypto callback method just does a wakeup).
1148 */
1149 crp->crp_callback(crp);
1150 } else {
1151 unsigned long r_flags;
1152 /*
1153 * Normal case; queue the callback for the thread.
1154 */
1155 CRYPTO_RETQ_LOCK();
1156 if (CRYPTO_RETQ_EMPTY())
1157 wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
1158 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1159 CRYPTO_RETQ_UNLOCK();
1160 }
1161 }
1162
1163 /*
1164 * Invoke the callback on behalf of the driver.
1165 */
1166 void
1167 crypto_kdone(struct cryptkop *krp)
1168 {
1169 struct cryptocap *cap;
1170 unsigned long d_flags;
1171
1172 if ((krp->krp_flags & CRYPTO_KF_DONE) != 0)
1173 printk("crypto: crypto_kdone op already done, flags 0x%x",
1174 krp->krp_flags);
1175 krp->krp_flags |= CRYPTO_KF_DONE;
1176 if (krp->krp_status != 0)
1177 cryptostats.cs_kerrs++;
1178
1179 CRYPTO_DRIVER_LOCK();
1180 /* XXX: What if driver is loaded in the meantime? */
1181 if (krp->krp_hid < crypto_drivers_num) {
1182 cap = &crypto_drivers[krp->krp_hid];
1183 cap->cc_koperations--;
1184 KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
1185 if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
1186 crypto_remove(cap);
1187 }
1188 CRYPTO_DRIVER_UNLOCK();
1189
1190 /*
1191 * CBIMM means unconditionally do the callback immediately;
1192 * This is used to avoid doing extraneous context switches
1193 */
1194 if ((krp->krp_flags & CRYPTO_KF_CBIMM)) {
1195 /*
1196 * Do the callback directly. This is ok when the
1197 * callback routine does very little (e.g. the
1198 * /dev/crypto callback method just does a wakeup).
1199 */
1200 krp->krp_callback(krp);
1201 } else {
1202 unsigned long r_flags;
1203 /*
1204 * Normal case; queue the callback for the thread.
1205 */
1206 CRYPTO_RETQ_LOCK();
1207 if (CRYPTO_RETQ_EMPTY())
1208 wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
1209 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1210 CRYPTO_RETQ_UNLOCK();
1211 }
1212 }
1213
1214 int
1215 crypto_getfeat(int *featp)
1216 {
1217 int hid, kalg, feat = 0;
1218 unsigned long d_flags;
1219
1220 CRYPTO_DRIVER_LOCK();
1221 for (hid = 0; hid < crypto_drivers_num; hid++) {
1222 const struct cryptocap *cap = &crypto_drivers[hid];
1223
1224 if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1225 !crypto_devallowsoft) {
1226 continue;
1227 }
1228 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1229 if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
1230 feat |= 1 << kalg;
1231 }
1232 CRYPTO_DRIVER_UNLOCK();
1233 *featp = feat;
1234 return (0);
1235 }
1236
1237 /*
1238 * Crypto thread, dispatches crypto requests.
1239 */
1240 static int
1241 crypto_proc(void *arg)
1242 {
1243 struct cryptop *crp, *submit;
1244 struct cryptkop *krp, *krpp;
1245 struct cryptocap *cap;
1246 u_int32_t hid;
1247 int result, hint;
1248 unsigned long q_flags;
1249
1250 ocf_daemonize("crypto");
1251
1252 CRYPTO_Q_LOCK();
1253 for (;;) {
1254 /*
1255 * we need to make sure we don't get into a busy loop with nothing
1256 * to do, the two crypto_all_*blocked vars help us find out when
1257 * we are all full and can do nothing on any driver or Q. If so we
1258 * wait for an unblock.
1259 */
1260 crypto_all_qblocked = !list_empty(&crp_q);
1261
1262 /*
1263 * Find the first element in the queue that can be
1264 * processed and look-ahead to see if multiple ops
1265 * are ready for the same driver.
1266 */
1267 submit = NULL;
1268 hint = 0;
1269 list_for_each_entry(crp, &crp_q, crp_next) {
1270 hid = CRYPTO_SESID2HID(crp->crp_sid);
1271 cap = crypto_checkdriver(hid);
1272 /*
1273 * Driver cannot disappear when there is an active
1274 * session.
1275 */
1276 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1277 __func__, __LINE__));
1278 if (cap == NULL || cap->cc_dev == NULL) {
1279 /* Op needs to be migrated, process it. */
1280 if (submit == NULL)
1281 submit = crp;
1282 break;
1283 }
1284 if (!cap->cc_qblocked) {
1285 if (submit != NULL) {
1286 /*
1287 * We stop on finding another op,
1288 * regardless whether its for the same
1289 * driver or not. We could keep
1290 * searching the queue but it might be
1291 * better to just use a per-driver
1292 * queue instead.
1293 */
1294 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1295 hint = CRYPTO_HINT_MORE;
1296 break;
1297 } else {
1298 submit = crp;
1299 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1300 break;
1301 /* keep scanning for more are q'd */
1302 }
1303 }
1304 }
1305 if (submit != NULL) {
1306 hid = CRYPTO_SESID2HID(submit->crp_sid);
1307 crypto_all_qblocked = 0;
1308 list_del(&submit->crp_next);
1309 crypto_drivers[hid].cc_qblocked = 1;
1310 cap = crypto_checkdriver(hid);
1311 CRYPTO_Q_UNLOCK();
1312 KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
1313 __func__, __LINE__));
1314 result = crypto_invoke(cap, submit, hint);
1315 CRYPTO_Q_LOCK();
1316 if (result == ERESTART) {
1317 /*
1318 * The driver ran out of resources, mark the
1319 * driver ``blocked'' for cryptop's and put
1320 * the request back in the queue. It would
1321 * best to put the request back where we got
1322 * it but that's hard so for now we put it
1323 * at the front. This should be ok; putting
1324 * it at the end does not work.
1325 */
1326 /* XXX validate sid again? */
1327 list_add(&submit->crp_next, &crp_q);
1328 cryptostats.cs_blocks++;
1329 } else
1330 crypto_drivers[hid].cc_qblocked=0;
1331 }
1332
1333 crypto_all_kqblocked = !list_empty(&crp_kq);
1334
1335 /* As above, but for key ops */
1336 krp = NULL;
1337 list_for_each_entry(krpp, &crp_kq, krp_next) {
1338 cap = crypto_checkdriver(krpp->krp_hid);
1339 if (cap == NULL || cap->cc_dev == NULL) {
1340 /*
1341 * Operation needs to be migrated, invalidate
1342 * the assigned device so it will reselect a
1343 * new one below. Propagate the original
1344 * crid selection flags if supplied.
1345 */
1346 krp->krp_hid = krp->krp_crid &
1347 (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
1348 if (krp->krp_hid == 0)
1349 krp->krp_hid =
1350 CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
1351 break;
1352 }
1353 if (!cap->cc_kqblocked) {
1354 krp = krpp;
1355 break;
1356 }
1357 }
1358 if (krp != NULL) {
1359 crypto_all_kqblocked = 0;
1360 list_del(&krp->krp_next);
1361 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1362 CRYPTO_Q_UNLOCK();
1363 result = crypto_kinvoke(krp, krp->krp_hid);
1364 CRYPTO_Q_LOCK();
1365 if (result == ERESTART) {
1366 /*
1367 * The driver ran out of resources, mark the
1368 * driver ``blocked'' for cryptkop's and put
1369 * the request back in the queue. It would
1370 * best to put the request back where we got
1371 * it but that's hard so for now we put it
1372 * at the front. This should be ok; putting
1373 * it at the end does not work.
1374 */
1375 /* XXX validate sid again? */
1376 list_add(&krp->krp_next, &crp_kq);
1377 cryptostats.cs_kblocks++;
1378 } else
1379 crypto_drivers[krp->krp_hid].cc_kqblocked = 0;
1380 }
1381
1382 if (submit == NULL && krp == NULL) {
1383 /*
1384 * Nothing more to be processed. Sleep until we're
1385 * woken because there are more ops to process.
1386 * This happens either by submission or by a driver
1387 * becoming unblocked and notifying us through
1388 * crypto_unblock. Note that when we wakeup we
1389 * start processing each queue again from the
1390 * front. It's not clear that it's important to
1391 * preserve this ordering since ops may finish
1392 * out of order if dispatched to different devices
1393 * and some become blocked while others do not.
1394 */
1395 dprintk("%s - sleeping (qe=%d qb=%d kqe=%d kqb=%d)\n",
1396 __FUNCTION__,
1397 list_empty(&crp_q), crypto_all_qblocked,
1398 list_empty(&crp_kq), crypto_all_kqblocked);
1399 CRYPTO_Q_UNLOCK();
1400 crp_sleep = 1;
1401 wait_event_interruptible(cryptoproc_wait,
1402 !(list_empty(&crp_q) || crypto_all_qblocked) ||
1403 !(list_empty(&crp_kq) || crypto_all_kqblocked) ||
1404 cryptoproc == (pid_t) -1);
1405 crp_sleep = 0;
1406 if (signal_pending (current)) {
1407 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1408 spin_lock_irq(&current->sigmask_lock);
1409 #endif
1410 flush_signals(current);
1411 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1412 spin_unlock_irq(&current->sigmask_lock);
1413 #endif
1414 }
1415 CRYPTO_Q_LOCK();
1416 dprintk("%s - awake\n", __FUNCTION__);
1417 if (cryptoproc == (pid_t) -1)
1418 break;
1419 cryptostats.cs_intrs++;
1420 }
1421 }
1422 CRYPTO_Q_UNLOCK();
1423 complete_and_exit(&cryptoproc_exited, 0);
1424 }
1425
1426 /*
1427 * Crypto returns thread, does callbacks for processed crypto requests.
1428 * Callbacks are done here, rather than in the crypto drivers, because
1429 * callbacks typically are expensive and would slow interrupt handling.
1430 */
1431 static int
1432 crypto_ret_proc(void *arg)
1433 {
1434 struct cryptop *crpt;
1435 struct cryptkop *krpt;
1436 unsigned long r_flags;
1437
1438 ocf_daemonize("crypto_ret");
1439
1440 CRYPTO_RETQ_LOCK();
1441 for (;;) {
1442 /* Harvest return q's for completed ops */
1443 crpt = NULL;
1444 if (!list_empty(&crp_ret_q))
1445 crpt = list_entry(crp_ret_q.next, typeof(*crpt), crp_next);
1446 if (crpt != NULL)
1447 list_del(&crpt->crp_next);
1448
1449 krpt = NULL;
1450 if (!list_empty(&crp_ret_kq))
1451 krpt = list_entry(crp_ret_kq.next, typeof(*krpt), krp_next);
1452 if (krpt != NULL)
1453 list_del(&krpt->krp_next);
1454
1455 if (crpt != NULL || krpt != NULL) {
1456 CRYPTO_RETQ_UNLOCK();
1457 /*
1458 * Run callbacks unlocked.
1459 */
1460 if (crpt != NULL)
1461 crpt->crp_callback(crpt);
1462 if (krpt != NULL)
1463 krpt->krp_callback(krpt);
1464 CRYPTO_RETQ_LOCK();
1465 } else {
1466 /*
1467 * Nothing more to be processed. Sleep until we're
1468 * woken because there are more returns to process.
1469 */
1470 dprintk("%s - sleeping\n", __FUNCTION__);
1471 CRYPTO_RETQ_UNLOCK();
1472 wait_event_interruptible(cryptoretproc_wait,
1473 cryptoretproc == (pid_t) -1 ||
1474 !list_empty(&crp_ret_q) ||
1475 !list_empty(&crp_ret_kq));
1476 if (signal_pending (current)) {
1477 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1478 spin_lock_irq(&current->sigmask_lock);
1479 #endif
1480 flush_signals(current);
1481 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1482 spin_unlock_irq(&current->sigmask_lock);
1483 #endif
1484 }
1485 CRYPTO_RETQ_LOCK();
1486 dprintk("%s - awake\n", __FUNCTION__);
1487 if (cryptoretproc == (pid_t) -1) {
1488 dprintk("%s - EXITING!\n", __FUNCTION__);
1489 break;
1490 }
1491 cryptostats.cs_rets++;
1492 }
1493 }
1494 CRYPTO_RETQ_UNLOCK();
1495 complete_and_exit(&cryptoretproc_exited, 0);
1496 }
1497
1498
1499 #if 0 /* should put this into /proc or something */
1500 static void
1501 db_show_drivers(void)
1502 {
1503 int hid;
1504
1505 db_printf("%12s %4s %4s %8s %2s %2s\n"
1506 , "Device"
1507 , "Ses"
1508 , "Kops"
1509 , "Flags"
1510 , "QB"
1511 , "KB"
1512 );
1513 for (hid = 0; hid < crypto_drivers_num; hid++) {
1514 const struct cryptocap *cap = &crypto_drivers[hid];
1515 if (cap->cc_dev == NULL)
1516 continue;
1517 db_printf("%-12s %4u %4u %08x %2u %2u\n"
1518 , device_get_nameunit(cap->cc_dev)
1519 , cap->cc_sessions
1520 , cap->cc_koperations
1521 , cap->cc_flags
1522 , cap->cc_qblocked
1523 , cap->cc_kqblocked
1524 );
1525 }
1526 }
1527
1528 DB_SHOW_COMMAND(crypto, db_show_crypto)
1529 {
1530 struct cryptop *crp;
1531
1532 db_show_drivers();
1533 db_printf("\n");
1534
1535 db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
1536 "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
1537 "Desc", "Callback");
1538 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1539 db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
1540 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1541 , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
1542 , crp->crp_ilen, crp->crp_olen
1543 , crp->crp_etype
1544 , crp->crp_flags
1545 , crp->crp_desc
1546 , crp->crp_callback
1547 );
1548 }
1549 if (!TAILQ_EMPTY(&crp_ret_q)) {
1550 db_printf("\n%4s %4s %4s %8s\n",
1551 "HID", "Etype", "Flags", "Callback");
1552 TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
1553 db_printf("%4u %4u %04x %8p\n"
1554 , (int) CRYPTO_SESID2HID(crp->crp_sid)
1555 , crp->crp_etype
1556 , crp->crp_flags
1557 , crp->crp_callback
1558 );
1559 }
1560 }
1561 }
1562
1563 DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
1564 {
1565 struct cryptkop *krp;
1566
1567 db_show_drivers();
1568 db_printf("\n");
1569
1570 db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
1571 "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
1572 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1573 db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
1574 , krp->krp_op
1575 , krp->krp_status
1576 , krp->krp_iparams, krp->krp_oparams
1577 , krp->krp_crid, krp->krp_hid
1578 , krp->krp_callback
1579 );
1580 }
1581 if (!TAILQ_EMPTY(&crp_ret_q)) {
1582 db_printf("%4s %5s %8s %4s %8s\n",
1583 "Op", "Status", "CRID", "HID", "Callback");
1584 TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
1585 db_printf("%4u %5u %08x %4u %8p\n"
1586 , krp->krp_op
1587 , krp->krp_status
1588 , krp->krp_crid, krp->krp_hid
1589 , krp->krp_callback
1590 );
1591 }
1592 }
1593 }
1594 #endif
1595
1596
1597 static int
1598 crypto_init(void)
1599 {
1600 int error;
1601
1602 dprintk("%s(0x%x)\n", __FUNCTION__, (int) crypto_init);
1603
1604 if (crypto_initted)
1605 return 0;
1606 crypto_initted = 1;
1607
1608 spin_lock_init(&crypto_drivers_lock);
1609 spin_lock_init(&crypto_q_lock);
1610 spin_lock_init(&crypto_ret_q_lock);
1611
1612 cryptop_zone = kmem_cache_create("cryptop", sizeof(struct cryptop),
1613 0, SLAB_HWCACHE_ALIGN, NULL
1614 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1615 , NULL
1616 #endif
1617 );
1618
1619 cryptodesc_zone = kmem_cache_create("cryptodesc", sizeof(struct cryptodesc),
1620 0, SLAB_HWCACHE_ALIGN, NULL
1621 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1622 , NULL
1623 #endif
1624 );
1625
1626 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
1627 printk("crypto: crypto_init cannot setup crypto zones\n");
1628 error = ENOMEM;
1629 goto bad;
1630 }
1631
1632 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
1633 crypto_drivers = kmalloc(crypto_drivers_num * sizeof(struct cryptocap),
1634 GFP_KERNEL);
1635 if (crypto_drivers == NULL) {
1636 printk("crypto: crypto_init cannot setup crypto drivers\n");
1637 error = ENOMEM;
1638 goto bad;
1639 }
1640
1641 memset(crypto_drivers, 0, crypto_drivers_num * sizeof(struct cryptocap));
1642
1643 init_completion(&cryptoproc_exited);
1644 init_completion(&cryptoretproc_exited);
1645
1646 cryptoproc = 0; /* to avoid race condition where proc runs first */
1647 cryptoproc = kernel_thread(crypto_proc, NULL, CLONE_FS|CLONE_FILES);
1648 if (cryptoproc < 0) {
1649 error = cryptoproc;
1650 printk("crypto: crypto_init cannot start crypto thread; error %d",
1651 error);
1652 goto bad;
1653 }
1654
1655 cryptoretproc = 0; /* to avoid race condition where proc runs first */
1656 cryptoretproc = kernel_thread(crypto_ret_proc, NULL, CLONE_FS|CLONE_FILES);
1657 if (cryptoretproc < 0) {
1658 error = cryptoretproc;
1659 printk("crypto: crypto_init cannot start cryptoret thread; error %d",
1660 error);
1661 goto bad;
1662 }
1663
1664 return 0;
1665 bad:
1666 crypto_exit();
1667 return error;
1668 }
1669
1670
1671 static void
1672 crypto_exit(void)
1673 {
1674 pid_t p;
1675 unsigned long d_flags;
1676
1677 dprintk("%s()\n", __FUNCTION__);
1678
1679 /*
1680 * Terminate any crypto threads.
1681 */
1682
1683 CRYPTO_DRIVER_LOCK();
1684 p = cryptoproc;
1685 cryptoproc = (pid_t) -1;
1686 kill_proc(p, SIGTERM, 1);
1687 wake_up_interruptible(&cryptoproc_wait);
1688 CRYPTO_DRIVER_UNLOCK();
1689
1690 wait_for_completion(&cryptoproc_exited);
1691
1692 CRYPTO_DRIVER_LOCK();
1693 p = cryptoretproc;
1694 cryptoretproc = (pid_t) -1;
1695 kill_proc(p, SIGTERM, 1);
1696 wake_up_interruptible(&cryptoretproc_wait);
1697 CRYPTO_DRIVER_UNLOCK();
1698
1699 wait_for_completion(&cryptoretproc_exited);
1700
1701 /* XXX flush queues??? */
1702
1703 /*
1704 * Reclaim dynamically allocated resources.
1705 */
1706 if (crypto_drivers != NULL)
1707 kfree(crypto_drivers);
1708
1709 if (cryptodesc_zone != NULL)
1710 kmem_cache_destroy(cryptodesc_zone);
1711 if (cryptop_zone != NULL)
1712 kmem_cache_destroy(cryptop_zone);
1713 }
1714
1715
1716 EXPORT_SYMBOL(crypto_newsession);
1717 EXPORT_SYMBOL(crypto_freesession);
1718 EXPORT_SYMBOL(crypto_get_driverid);
1719 EXPORT_SYMBOL(crypto_kregister);
1720 EXPORT_SYMBOL(crypto_register);
1721 EXPORT_SYMBOL(crypto_unregister);
1722 EXPORT_SYMBOL(crypto_unregister_all);
1723 EXPORT_SYMBOL(crypto_unblock);
1724 EXPORT_SYMBOL(crypto_dispatch);
1725 EXPORT_SYMBOL(crypto_kdispatch);
1726 EXPORT_SYMBOL(crypto_freereq);
1727 EXPORT_SYMBOL(crypto_getreq);
1728 EXPORT_SYMBOL(crypto_done);
1729 EXPORT_SYMBOL(crypto_kdone);
1730 EXPORT_SYMBOL(crypto_getfeat);
1731 EXPORT_SYMBOL(crypto_userasymcrypto);
1732 EXPORT_SYMBOL(crypto_getcaps);
1733 EXPORT_SYMBOL(crypto_find_driver);
1734 EXPORT_SYMBOL(crypto_find_device_byhid);
1735
1736 module_init(crypto_init);
1737 module_exit(crypto_exit);
1738
1739 MODULE_LICENSE("BSD");
1740 MODULE_AUTHOR("David McCullough <david_mccullough@securecomputing.com>");
1741 MODULE_DESCRIPTION("OCF (OpenBSD Cryptographic Framework)");
This page took 0.118761 seconds and 5 git commands to generate.