crypto.c 46 KB

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