zcrypt_api.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * linux/drivers/s390/crypto/zcrypt_api.c
  3. *
  4. * zcrypt 2.1.0
  5. *
  6. * Copyright (C) 2001, 2006 IBM Corporation
  7. * Author(s): Robert Burroughs
  8. * Eric Rossman (edrossma@us.ibm.com)
  9. * Cornelia Huck <cornelia.huck@de.ibm.com>
  10. *
  11. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  12. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  13. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2, or (at your option)
  18. * any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/fs.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/compat.h>
  36. #include <asm/atomic.h>
  37. #include <asm/uaccess.h>
  38. #include <linux/hw_random.h>
  39. #include "zcrypt_api.h"
  40. /**
  41. * Module description.
  42. */
  43. MODULE_AUTHOR("IBM Corporation");
  44. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, "
  45. "Copyright 2001, 2006 IBM Corporation");
  46. MODULE_LICENSE("GPL");
  47. static DEFINE_SPINLOCK(zcrypt_device_lock);
  48. static LIST_HEAD(zcrypt_device_list);
  49. static int zcrypt_device_count = 0;
  50. static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  51. static int zcrypt_rng_device_add(void);
  52. static void zcrypt_rng_device_remove(void);
  53. /**
  54. * Device attributes common for all crypto devices.
  55. */
  56. static ssize_t zcrypt_type_show(struct device *dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  60. return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
  61. }
  62. static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
  63. static ssize_t zcrypt_online_show(struct device *dev,
  64. struct device_attribute *attr, char *buf)
  65. {
  66. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  67. return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
  68. }
  69. static ssize_t zcrypt_online_store(struct device *dev,
  70. struct device_attribute *attr,
  71. const char *buf, size_t count)
  72. {
  73. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  74. int online;
  75. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  76. return -EINVAL;
  77. zdev->online = online;
  78. if (!online)
  79. ap_flush_queue(zdev->ap_dev);
  80. return count;
  81. }
  82. static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
  83. static struct attribute * zcrypt_device_attrs[] = {
  84. &dev_attr_type.attr,
  85. &dev_attr_online.attr,
  86. NULL,
  87. };
  88. static struct attribute_group zcrypt_device_attr_group = {
  89. .attrs = zcrypt_device_attrs,
  90. };
  91. /**
  92. * Move the device towards the head of the device list.
  93. * Need to be called while holding the zcrypt device list lock.
  94. * Note: cards with speed_rating of 0 are kept at the end of the list.
  95. */
  96. static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
  97. {
  98. struct zcrypt_device *tmp;
  99. struct list_head *l;
  100. if (zdev->speed_rating == 0)
  101. return;
  102. for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
  103. tmp = list_entry(l, struct zcrypt_device, list);
  104. if ((tmp->request_count + 1) * tmp->speed_rating <=
  105. (zdev->request_count + 1) * zdev->speed_rating &&
  106. tmp->speed_rating != 0)
  107. break;
  108. }
  109. if (l == zdev->list.prev)
  110. return;
  111. /* Move zdev behind l */
  112. list_del(&zdev->list);
  113. list_add(&zdev->list, l);
  114. }
  115. /**
  116. * Move the device towards the tail of the device list.
  117. * Need to be called while holding the zcrypt device list lock.
  118. * Note: cards with speed_rating of 0 are kept at the end of the list.
  119. */
  120. static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
  121. {
  122. struct zcrypt_device *tmp;
  123. struct list_head *l;
  124. if (zdev->speed_rating == 0)
  125. return;
  126. for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
  127. tmp = list_entry(l, struct zcrypt_device, list);
  128. if ((tmp->request_count + 1) * tmp->speed_rating >
  129. (zdev->request_count + 1) * zdev->speed_rating ||
  130. tmp->speed_rating == 0)
  131. break;
  132. }
  133. if (l == zdev->list.next)
  134. return;
  135. /* Move zdev before l */
  136. list_del(&zdev->list);
  137. list_add_tail(&zdev->list, l);
  138. }
  139. static void zcrypt_device_release(struct kref *kref)
  140. {
  141. struct zcrypt_device *zdev =
  142. container_of(kref, struct zcrypt_device, refcount);
  143. zcrypt_device_free(zdev);
  144. }
  145. void zcrypt_device_get(struct zcrypt_device *zdev)
  146. {
  147. kref_get(&zdev->refcount);
  148. }
  149. EXPORT_SYMBOL(zcrypt_device_get);
  150. int zcrypt_device_put(struct zcrypt_device *zdev)
  151. {
  152. return kref_put(&zdev->refcount, zcrypt_device_release);
  153. }
  154. EXPORT_SYMBOL(zcrypt_device_put);
  155. struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
  156. {
  157. struct zcrypt_device *zdev;
  158. zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
  159. if (!zdev)
  160. return NULL;
  161. zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
  162. if (!zdev->reply.message)
  163. goto out_free;
  164. zdev->reply.length = max_response_size;
  165. spin_lock_init(&zdev->lock);
  166. INIT_LIST_HEAD(&zdev->list);
  167. return zdev;
  168. out_free:
  169. kfree(zdev);
  170. return NULL;
  171. }
  172. EXPORT_SYMBOL(zcrypt_device_alloc);
  173. void zcrypt_device_free(struct zcrypt_device *zdev)
  174. {
  175. kfree(zdev->reply.message);
  176. kfree(zdev);
  177. }
  178. EXPORT_SYMBOL(zcrypt_device_free);
  179. /**
  180. * Register a crypto device.
  181. */
  182. int zcrypt_device_register(struct zcrypt_device *zdev)
  183. {
  184. int rc;
  185. rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
  186. &zcrypt_device_attr_group);
  187. if (rc)
  188. goto out;
  189. get_device(&zdev->ap_dev->device);
  190. kref_init(&zdev->refcount);
  191. spin_lock_bh(&zcrypt_device_lock);
  192. zdev->online = 1; /* New devices are online by default. */
  193. list_add_tail(&zdev->list, &zcrypt_device_list);
  194. __zcrypt_increase_preference(zdev);
  195. zcrypt_device_count++;
  196. spin_unlock_bh(&zcrypt_device_lock);
  197. if (zdev->ops->rng) {
  198. rc = zcrypt_rng_device_add();
  199. if (rc)
  200. goto out_unregister;
  201. }
  202. return 0;
  203. out_unregister:
  204. spin_lock_bh(&zcrypt_device_lock);
  205. zcrypt_device_count--;
  206. list_del_init(&zdev->list);
  207. spin_unlock_bh(&zcrypt_device_lock);
  208. sysfs_remove_group(&zdev->ap_dev->device.kobj,
  209. &zcrypt_device_attr_group);
  210. put_device(&zdev->ap_dev->device);
  211. zcrypt_device_put(zdev);
  212. out:
  213. return rc;
  214. }
  215. EXPORT_SYMBOL(zcrypt_device_register);
  216. /**
  217. * Unregister a crypto device.
  218. */
  219. void zcrypt_device_unregister(struct zcrypt_device *zdev)
  220. {
  221. if (zdev->ops->rng)
  222. zcrypt_rng_device_remove();
  223. spin_lock_bh(&zcrypt_device_lock);
  224. zcrypt_device_count--;
  225. list_del_init(&zdev->list);
  226. spin_unlock_bh(&zcrypt_device_lock);
  227. sysfs_remove_group(&zdev->ap_dev->device.kobj,
  228. &zcrypt_device_attr_group);
  229. put_device(&zdev->ap_dev->device);
  230. zcrypt_device_put(zdev);
  231. }
  232. EXPORT_SYMBOL(zcrypt_device_unregister);
  233. /**
  234. * zcrypt_read is not be supported beyond zcrypt 1.3.1
  235. */
  236. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  237. size_t count, loff_t *f_pos)
  238. {
  239. return -EPERM;
  240. }
  241. /**
  242. * Write is is not allowed
  243. */
  244. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  245. size_t count, loff_t *f_pos)
  246. {
  247. return -EPERM;
  248. }
  249. /**
  250. * Device open/close functions to count number of users.
  251. */
  252. static int zcrypt_open(struct inode *inode, struct file *filp)
  253. {
  254. atomic_inc(&zcrypt_open_count);
  255. return 0;
  256. }
  257. static int zcrypt_release(struct inode *inode, struct file *filp)
  258. {
  259. atomic_dec(&zcrypt_open_count);
  260. return 0;
  261. }
  262. /**
  263. * zcrypt ioctls.
  264. */
  265. static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
  266. {
  267. struct zcrypt_device *zdev;
  268. int rc;
  269. if (mex->outputdatalength < mex->inputdatalength)
  270. return -EINVAL;
  271. /**
  272. * As long as outputdatalength is big enough, we can set the
  273. * outputdatalength equal to the inputdatalength, since that is the
  274. * number of bytes we will copy in any case
  275. */
  276. mex->outputdatalength = mex->inputdatalength;
  277. spin_lock_bh(&zcrypt_device_lock);
  278. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  279. if (!zdev->online ||
  280. !zdev->ops->rsa_modexpo ||
  281. zdev->min_mod_size > mex->inputdatalength ||
  282. zdev->max_mod_size < mex->inputdatalength)
  283. continue;
  284. zcrypt_device_get(zdev);
  285. get_device(&zdev->ap_dev->device);
  286. zdev->request_count++;
  287. __zcrypt_decrease_preference(zdev);
  288. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  289. spin_unlock_bh(&zcrypt_device_lock);
  290. rc = zdev->ops->rsa_modexpo(zdev, mex);
  291. spin_lock_bh(&zcrypt_device_lock);
  292. module_put(zdev->ap_dev->drv->driver.owner);
  293. }
  294. else
  295. rc = -EAGAIN;
  296. zdev->request_count--;
  297. __zcrypt_increase_preference(zdev);
  298. put_device(&zdev->ap_dev->device);
  299. zcrypt_device_put(zdev);
  300. spin_unlock_bh(&zcrypt_device_lock);
  301. return rc;
  302. }
  303. spin_unlock_bh(&zcrypt_device_lock);
  304. return -ENODEV;
  305. }
  306. static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
  307. {
  308. struct zcrypt_device *zdev;
  309. unsigned long long z1, z2, z3;
  310. int rc, copied;
  311. if (crt->outputdatalength < crt->inputdatalength ||
  312. (crt->inputdatalength & 1))
  313. return -EINVAL;
  314. /**
  315. * As long as outputdatalength is big enough, we can set the
  316. * outputdatalength equal to the inputdatalength, since that is the
  317. * number of bytes we will copy in any case
  318. */
  319. crt->outputdatalength = crt->inputdatalength;
  320. copied = 0;
  321. restart:
  322. spin_lock_bh(&zcrypt_device_lock);
  323. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  324. if (!zdev->online ||
  325. !zdev->ops->rsa_modexpo_crt ||
  326. zdev->min_mod_size > crt->inputdatalength ||
  327. zdev->max_mod_size < crt->inputdatalength)
  328. continue;
  329. if (zdev->short_crt && crt->inputdatalength > 240) {
  330. /**
  331. * Check inputdata for leading zeros for cards
  332. * that can't handle np_prime, bp_key, or
  333. * u_mult_inv > 128 bytes.
  334. */
  335. if (copied == 0) {
  336. int len;
  337. spin_unlock_bh(&zcrypt_device_lock);
  338. /* len is max 256 / 2 - 120 = 8 */
  339. len = crt->inputdatalength / 2 - 120;
  340. z1 = z2 = z3 = 0;
  341. if (copy_from_user(&z1, crt->np_prime, len) ||
  342. copy_from_user(&z2, crt->bp_key, len) ||
  343. copy_from_user(&z3, crt->u_mult_inv, len))
  344. return -EFAULT;
  345. copied = 1;
  346. /**
  347. * We have to restart device lookup -
  348. * the device list may have changed by now.
  349. */
  350. goto restart;
  351. }
  352. if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
  353. /* The device can't handle this request. */
  354. continue;
  355. }
  356. zcrypt_device_get(zdev);
  357. get_device(&zdev->ap_dev->device);
  358. zdev->request_count++;
  359. __zcrypt_decrease_preference(zdev);
  360. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  361. spin_unlock_bh(&zcrypt_device_lock);
  362. rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
  363. spin_lock_bh(&zcrypt_device_lock);
  364. module_put(zdev->ap_dev->drv->driver.owner);
  365. }
  366. else
  367. rc = -EAGAIN;
  368. zdev->request_count--;
  369. __zcrypt_increase_preference(zdev);
  370. put_device(&zdev->ap_dev->device);
  371. zcrypt_device_put(zdev);
  372. spin_unlock_bh(&zcrypt_device_lock);
  373. return rc;
  374. }
  375. spin_unlock_bh(&zcrypt_device_lock);
  376. return -ENODEV;
  377. }
  378. static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  379. {
  380. struct zcrypt_device *zdev;
  381. int rc;
  382. spin_lock_bh(&zcrypt_device_lock);
  383. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  384. if (!zdev->online || !zdev->ops->send_cprb ||
  385. (xcRB->user_defined != AUTOSELECT &&
  386. AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined)
  387. )
  388. continue;
  389. zcrypt_device_get(zdev);
  390. get_device(&zdev->ap_dev->device);
  391. zdev->request_count++;
  392. __zcrypt_decrease_preference(zdev);
  393. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  394. spin_unlock_bh(&zcrypt_device_lock);
  395. rc = zdev->ops->send_cprb(zdev, xcRB);
  396. spin_lock_bh(&zcrypt_device_lock);
  397. module_put(zdev->ap_dev->drv->driver.owner);
  398. }
  399. else
  400. rc = -EAGAIN;
  401. zdev->request_count--;
  402. __zcrypt_increase_preference(zdev);
  403. put_device(&zdev->ap_dev->device);
  404. zcrypt_device_put(zdev);
  405. spin_unlock_bh(&zcrypt_device_lock);
  406. return rc;
  407. }
  408. spin_unlock_bh(&zcrypt_device_lock);
  409. return -ENODEV;
  410. }
  411. static long zcrypt_rng(char *buffer)
  412. {
  413. struct zcrypt_device *zdev;
  414. int rc;
  415. spin_lock_bh(&zcrypt_device_lock);
  416. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  417. if (!zdev->online || !zdev->ops->rng)
  418. continue;
  419. zcrypt_device_get(zdev);
  420. get_device(&zdev->ap_dev->device);
  421. zdev->request_count++;
  422. __zcrypt_decrease_preference(zdev);
  423. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  424. spin_unlock_bh(&zcrypt_device_lock);
  425. rc = zdev->ops->rng(zdev, buffer);
  426. spin_lock_bh(&zcrypt_device_lock);
  427. module_put(zdev->ap_dev->drv->driver.owner);
  428. } else
  429. rc = -EAGAIN;
  430. zdev->request_count--;
  431. __zcrypt_increase_preference(zdev);
  432. put_device(&zdev->ap_dev->device);
  433. zcrypt_device_put(zdev);
  434. spin_unlock_bh(&zcrypt_device_lock);
  435. return rc;
  436. }
  437. spin_unlock_bh(&zcrypt_device_lock);
  438. return -ENODEV;
  439. }
  440. static void zcrypt_status_mask(char status[AP_DEVICES])
  441. {
  442. struct zcrypt_device *zdev;
  443. memset(status, 0, sizeof(char) * AP_DEVICES);
  444. spin_lock_bh(&zcrypt_device_lock);
  445. list_for_each_entry(zdev, &zcrypt_device_list, list)
  446. status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  447. zdev->online ? zdev->user_space_type : 0x0d;
  448. spin_unlock_bh(&zcrypt_device_lock);
  449. }
  450. static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
  451. {
  452. struct zcrypt_device *zdev;
  453. memset(qdepth, 0, sizeof(char) * AP_DEVICES);
  454. spin_lock_bh(&zcrypt_device_lock);
  455. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  456. spin_lock(&zdev->ap_dev->lock);
  457. qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  458. zdev->ap_dev->pendingq_count +
  459. zdev->ap_dev->requestq_count;
  460. spin_unlock(&zdev->ap_dev->lock);
  461. }
  462. spin_unlock_bh(&zcrypt_device_lock);
  463. }
  464. static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
  465. {
  466. struct zcrypt_device *zdev;
  467. memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
  468. spin_lock_bh(&zcrypt_device_lock);
  469. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  470. spin_lock(&zdev->ap_dev->lock);
  471. reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  472. zdev->ap_dev->total_request_count;
  473. spin_unlock(&zdev->ap_dev->lock);
  474. }
  475. spin_unlock_bh(&zcrypt_device_lock);
  476. }
  477. static int zcrypt_pendingq_count(void)
  478. {
  479. struct zcrypt_device *zdev;
  480. int pendingq_count = 0;
  481. spin_lock_bh(&zcrypt_device_lock);
  482. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  483. spin_lock(&zdev->ap_dev->lock);
  484. pendingq_count += zdev->ap_dev->pendingq_count;
  485. spin_unlock(&zdev->ap_dev->lock);
  486. }
  487. spin_unlock_bh(&zcrypt_device_lock);
  488. return pendingq_count;
  489. }
  490. static int zcrypt_requestq_count(void)
  491. {
  492. struct zcrypt_device *zdev;
  493. int requestq_count = 0;
  494. spin_lock_bh(&zcrypt_device_lock);
  495. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  496. spin_lock(&zdev->ap_dev->lock);
  497. requestq_count += zdev->ap_dev->requestq_count;
  498. spin_unlock(&zdev->ap_dev->lock);
  499. }
  500. spin_unlock_bh(&zcrypt_device_lock);
  501. return requestq_count;
  502. }
  503. static int zcrypt_count_type(int type)
  504. {
  505. struct zcrypt_device *zdev;
  506. int device_count = 0;
  507. spin_lock_bh(&zcrypt_device_lock);
  508. list_for_each_entry(zdev, &zcrypt_device_list, list)
  509. if (zdev->user_space_type == type)
  510. device_count++;
  511. spin_unlock_bh(&zcrypt_device_lock);
  512. return device_count;
  513. }
  514. /**
  515. * Old, deprecated combi status call.
  516. */
  517. static long zcrypt_ica_status(struct file *filp, unsigned long arg)
  518. {
  519. struct ica_z90_status *pstat;
  520. int ret;
  521. pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
  522. if (!pstat)
  523. return -ENOMEM;
  524. pstat->totalcount = zcrypt_device_count;
  525. pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
  526. pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
  527. pstat->requestqWaitCount = zcrypt_requestq_count();
  528. pstat->pendingqWaitCount = zcrypt_pendingq_count();
  529. pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
  530. pstat->cryptoDomain = ap_domain_index;
  531. zcrypt_status_mask(pstat->status);
  532. zcrypt_qdepth_mask(pstat->qdepth);
  533. ret = 0;
  534. if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
  535. ret = -EFAULT;
  536. kfree(pstat);
  537. return ret;
  538. }
  539. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  540. unsigned long arg)
  541. {
  542. int rc;
  543. switch (cmd) {
  544. case ICARSAMODEXPO: {
  545. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  546. struct ica_rsa_modexpo mex;
  547. if (copy_from_user(&mex, umex, sizeof(mex)))
  548. return -EFAULT;
  549. do {
  550. rc = zcrypt_rsa_modexpo(&mex);
  551. } while (rc == -EAGAIN);
  552. if (rc)
  553. return rc;
  554. return put_user(mex.outputdatalength, &umex->outputdatalength);
  555. }
  556. case ICARSACRT: {
  557. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  558. struct ica_rsa_modexpo_crt crt;
  559. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  560. return -EFAULT;
  561. do {
  562. rc = zcrypt_rsa_crt(&crt);
  563. } while (rc == -EAGAIN);
  564. if (rc)
  565. return rc;
  566. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  567. }
  568. case ZSECSENDCPRB: {
  569. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  570. struct ica_xcRB xcRB;
  571. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  572. return -EFAULT;
  573. do {
  574. rc = zcrypt_send_cprb(&xcRB);
  575. } while (rc == -EAGAIN);
  576. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  577. return -EFAULT;
  578. return rc;
  579. }
  580. case Z90STAT_STATUS_MASK: {
  581. char status[AP_DEVICES];
  582. zcrypt_status_mask(status);
  583. if (copy_to_user((char __user *) arg, status,
  584. sizeof(char) * AP_DEVICES))
  585. return -EFAULT;
  586. return 0;
  587. }
  588. case Z90STAT_QDEPTH_MASK: {
  589. char qdepth[AP_DEVICES];
  590. zcrypt_qdepth_mask(qdepth);
  591. if (copy_to_user((char __user *) arg, qdepth,
  592. sizeof(char) * AP_DEVICES))
  593. return -EFAULT;
  594. return 0;
  595. }
  596. case Z90STAT_PERDEV_REQCNT: {
  597. int reqcnt[AP_DEVICES];
  598. zcrypt_perdev_reqcnt(reqcnt);
  599. if (copy_to_user((int __user *) arg, reqcnt,
  600. sizeof(int) * AP_DEVICES))
  601. return -EFAULT;
  602. return 0;
  603. }
  604. case Z90STAT_REQUESTQ_COUNT:
  605. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  606. case Z90STAT_PENDINGQ_COUNT:
  607. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  608. case Z90STAT_TOTALOPEN_COUNT:
  609. return put_user(atomic_read(&zcrypt_open_count),
  610. (int __user *) arg);
  611. case Z90STAT_DOMAIN_INDEX:
  612. return put_user(ap_domain_index, (int __user *) arg);
  613. /**
  614. * Deprecated ioctls. Don't add another device count ioctl,
  615. * you can count them yourself in the user space with the
  616. * output of the Z90STAT_STATUS_MASK ioctl.
  617. */
  618. case ICAZ90STATUS:
  619. return zcrypt_ica_status(filp, arg);
  620. case Z90STAT_TOTALCOUNT:
  621. return put_user(zcrypt_device_count, (int __user *) arg);
  622. case Z90STAT_PCICACOUNT:
  623. return put_user(zcrypt_count_type(ZCRYPT_PCICA),
  624. (int __user *) arg);
  625. case Z90STAT_PCICCCOUNT:
  626. return put_user(zcrypt_count_type(ZCRYPT_PCICC),
  627. (int __user *) arg);
  628. case Z90STAT_PCIXCCMCL2COUNT:
  629. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
  630. (int __user *) arg);
  631. case Z90STAT_PCIXCCMCL3COUNT:
  632. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  633. (int __user *) arg);
  634. case Z90STAT_PCIXCCCOUNT:
  635. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
  636. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  637. (int __user *) arg);
  638. case Z90STAT_CEX2CCOUNT:
  639. return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
  640. (int __user *) arg);
  641. case Z90STAT_CEX2ACOUNT:
  642. return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
  643. (int __user *) arg);
  644. default:
  645. /* unknown ioctl number */
  646. return -ENOIOCTLCMD;
  647. }
  648. }
  649. #ifdef CONFIG_COMPAT
  650. /**
  651. * ioctl32 conversion routines
  652. */
  653. struct compat_ica_rsa_modexpo {
  654. compat_uptr_t inputdata;
  655. unsigned int inputdatalength;
  656. compat_uptr_t outputdata;
  657. unsigned int outputdatalength;
  658. compat_uptr_t b_key;
  659. compat_uptr_t n_modulus;
  660. };
  661. static long trans_modexpo32(struct file *filp, unsigned int cmd,
  662. unsigned long arg)
  663. {
  664. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  665. struct compat_ica_rsa_modexpo mex32;
  666. struct ica_rsa_modexpo mex64;
  667. long rc;
  668. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  669. return -EFAULT;
  670. mex64.inputdata = compat_ptr(mex32.inputdata);
  671. mex64.inputdatalength = mex32.inputdatalength;
  672. mex64.outputdata = compat_ptr(mex32.outputdata);
  673. mex64.outputdatalength = mex32.outputdatalength;
  674. mex64.b_key = compat_ptr(mex32.b_key);
  675. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  676. do {
  677. rc = zcrypt_rsa_modexpo(&mex64);
  678. } while (rc == -EAGAIN);
  679. if (!rc)
  680. rc = put_user(mex64.outputdatalength,
  681. &umex32->outputdatalength);
  682. return rc;
  683. }
  684. struct compat_ica_rsa_modexpo_crt {
  685. compat_uptr_t inputdata;
  686. unsigned int inputdatalength;
  687. compat_uptr_t outputdata;
  688. unsigned int outputdatalength;
  689. compat_uptr_t bp_key;
  690. compat_uptr_t bq_key;
  691. compat_uptr_t np_prime;
  692. compat_uptr_t nq_prime;
  693. compat_uptr_t u_mult_inv;
  694. };
  695. static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
  696. unsigned long arg)
  697. {
  698. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  699. struct compat_ica_rsa_modexpo_crt crt32;
  700. struct ica_rsa_modexpo_crt crt64;
  701. long rc;
  702. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  703. return -EFAULT;
  704. crt64.inputdata = compat_ptr(crt32.inputdata);
  705. crt64.inputdatalength = crt32.inputdatalength;
  706. crt64.outputdata= compat_ptr(crt32.outputdata);
  707. crt64.outputdatalength = crt32.outputdatalength;
  708. crt64.bp_key = compat_ptr(crt32.bp_key);
  709. crt64.bq_key = compat_ptr(crt32.bq_key);
  710. crt64.np_prime = compat_ptr(crt32.np_prime);
  711. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  712. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  713. do {
  714. rc = zcrypt_rsa_crt(&crt64);
  715. } while (rc == -EAGAIN);
  716. if (!rc)
  717. rc = put_user(crt64.outputdatalength,
  718. &ucrt32->outputdatalength);
  719. return rc;
  720. }
  721. struct compat_ica_xcRB {
  722. unsigned short agent_ID;
  723. unsigned int user_defined;
  724. unsigned short request_ID;
  725. unsigned int request_control_blk_length;
  726. unsigned char padding1[16 - sizeof (compat_uptr_t)];
  727. compat_uptr_t request_control_blk_addr;
  728. unsigned int request_data_length;
  729. char padding2[16 - sizeof (compat_uptr_t)];
  730. compat_uptr_t request_data_address;
  731. unsigned int reply_control_blk_length;
  732. char padding3[16 - sizeof (compat_uptr_t)];
  733. compat_uptr_t reply_control_blk_addr;
  734. unsigned int reply_data_length;
  735. char padding4[16 - sizeof (compat_uptr_t)];
  736. compat_uptr_t reply_data_addr;
  737. unsigned short priority_window;
  738. unsigned int status;
  739. } __attribute__((packed));
  740. static long trans_xcRB32(struct file *filp, unsigned int cmd,
  741. unsigned long arg)
  742. {
  743. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  744. struct compat_ica_xcRB xcRB32;
  745. struct ica_xcRB xcRB64;
  746. long rc;
  747. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  748. return -EFAULT;
  749. xcRB64.agent_ID = xcRB32.agent_ID;
  750. xcRB64.user_defined = xcRB32.user_defined;
  751. xcRB64.request_ID = xcRB32.request_ID;
  752. xcRB64.request_control_blk_length =
  753. xcRB32.request_control_blk_length;
  754. xcRB64.request_control_blk_addr =
  755. compat_ptr(xcRB32.request_control_blk_addr);
  756. xcRB64.request_data_length =
  757. xcRB32.request_data_length;
  758. xcRB64.request_data_address =
  759. compat_ptr(xcRB32.request_data_address);
  760. xcRB64.reply_control_blk_length =
  761. xcRB32.reply_control_blk_length;
  762. xcRB64.reply_control_blk_addr =
  763. compat_ptr(xcRB32.reply_control_blk_addr);
  764. xcRB64.reply_data_length = xcRB32.reply_data_length;
  765. xcRB64.reply_data_addr =
  766. compat_ptr(xcRB32.reply_data_addr);
  767. xcRB64.priority_window = xcRB32.priority_window;
  768. xcRB64.status = xcRB32.status;
  769. do {
  770. rc = zcrypt_send_cprb(&xcRB64);
  771. } while (rc == -EAGAIN);
  772. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  773. xcRB32.reply_data_length = xcRB64.reply_data_length;
  774. xcRB32.status = xcRB64.status;
  775. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  776. return -EFAULT;
  777. return rc;
  778. }
  779. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  780. unsigned long arg)
  781. {
  782. if (cmd == ICARSAMODEXPO)
  783. return trans_modexpo32(filp, cmd, arg);
  784. if (cmd == ICARSACRT)
  785. return trans_modexpo_crt32(filp, cmd, arg);
  786. if (cmd == ZSECSENDCPRB)
  787. return trans_xcRB32(filp, cmd, arg);
  788. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  789. }
  790. #endif
  791. /**
  792. * Misc device file operations.
  793. */
  794. static const struct file_operations zcrypt_fops = {
  795. .owner = THIS_MODULE,
  796. .read = zcrypt_read,
  797. .write = zcrypt_write,
  798. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  799. #ifdef CONFIG_COMPAT
  800. .compat_ioctl = zcrypt_compat_ioctl,
  801. #endif
  802. .open = zcrypt_open,
  803. .release = zcrypt_release
  804. };
  805. /**
  806. * Misc device.
  807. */
  808. static struct miscdevice zcrypt_misc_device = {
  809. .minor = MISC_DYNAMIC_MINOR,
  810. .name = "z90crypt",
  811. .fops = &zcrypt_fops,
  812. };
  813. /**
  814. * Deprecated /proc entry support.
  815. */
  816. static struct proc_dir_entry *zcrypt_entry;
  817. static int sprintcl(unsigned char *outaddr, unsigned char *addr,
  818. unsigned int len)
  819. {
  820. int hl, i;
  821. hl = 0;
  822. for (i = 0; i < len; i++)
  823. hl += sprintf(outaddr+hl, "%01x", (unsigned int) addr[i]);
  824. hl += sprintf(outaddr+hl, " ");
  825. return hl;
  826. }
  827. static int sprintrw(unsigned char *outaddr, unsigned char *addr,
  828. unsigned int len)
  829. {
  830. int hl, inl, c, cx;
  831. hl = sprintf(outaddr, " ");
  832. inl = 0;
  833. for (c = 0; c < (len / 16); c++) {
  834. hl += sprintcl(outaddr+hl, addr+inl, 16);
  835. inl += 16;
  836. }
  837. cx = len%16;
  838. if (cx) {
  839. hl += sprintcl(outaddr+hl, addr+inl, cx);
  840. inl += cx;
  841. }
  842. hl += sprintf(outaddr+hl, "\n");
  843. return hl;
  844. }
  845. static int sprinthx(unsigned char *title, unsigned char *outaddr,
  846. unsigned char *addr, unsigned int len)
  847. {
  848. int hl, inl, r, rx;
  849. hl = sprintf(outaddr, "\n%s\n", title);
  850. inl = 0;
  851. for (r = 0; r < (len / 64); r++) {
  852. hl += sprintrw(outaddr+hl, addr+inl, 64);
  853. inl += 64;
  854. }
  855. rx = len % 64;
  856. if (rx) {
  857. hl += sprintrw(outaddr+hl, addr+inl, rx);
  858. inl += rx;
  859. }
  860. hl += sprintf(outaddr+hl, "\n");
  861. return hl;
  862. }
  863. static int sprinthx4(unsigned char *title, unsigned char *outaddr,
  864. unsigned int *array, unsigned int len)
  865. {
  866. int hl, r;
  867. hl = sprintf(outaddr, "\n%s\n", title);
  868. for (r = 0; r < len; r++) {
  869. if ((r % 8) == 0)
  870. hl += sprintf(outaddr+hl, " ");
  871. hl += sprintf(outaddr+hl, "%08X ", array[r]);
  872. if ((r % 8) == 7)
  873. hl += sprintf(outaddr+hl, "\n");
  874. }
  875. hl += sprintf(outaddr+hl, "\n");
  876. return hl;
  877. }
  878. static int zcrypt_status_read(char *resp_buff, char **start, off_t offset,
  879. int count, int *eof, void *data)
  880. {
  881. unsigned char *workarea;
  882. int len;
  883. len = 0;
  884. /* resp_buff is a page. Use the right half for a work area */
  885. workarea = resp_buff + 2000;
  886. len += sprintf(resp_buff + len, "\nzcrypt version: %d.%d.%d\n",
  887. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  888. len += sprintf(resp_buff + len, "Cryptographic domain: %d\n",
  889. ap_domain_index);
  890. len += sprintf(resp_buff + len, "Total device count: %d\n",
  891. zcrypt_device_count);
  892. len += sprintf(resp_buff + len, "PCICA count: %d\n",
  893. zcrypt_count_type(ZCRYPT_PCICA));
  894. len += sprintf(resp_buff + len, "PCICC count: %d\n",
  895. zcrypt_count_type(ZCRYPT_PCICC));
  896. len += sprintf(resp_buff + len, "PCIXCC MCL2 count: %d\n",
  897. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  898. len += sprintf(resp_buff + len, "PCIXCC MCL3 count: %d\n",
  899. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  900. len += sprintf(resp_buff + len, "CEX2C count: %d\n",
  901. zcrypt_count_type(ZCRYPT_CEX2C));
  902. len += sprintf(resp_buff + len, "CEX2A count: %d\n",
  903. zcrypt_count_type(ZCRYPT_CEX2A));
  904. len += sprintf(resp_buff + len, "requestq count: %d\n",
  905. zcrypt_requestq_count());
  906. len += sprintf(resp_buff + len, "pendingq count: %d\n",
  907. zcrypt_pendingq_count());
  908. len += sprintf(resp_buff + len, "Total open handles: %d\n\n",
  909. atomic_read(&zcrypt_open_count));
  910. zcrypt_status_mask(workarea);
  911. len += sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  912. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A",
  913. resp_buff+len, workarea, AP_DEVICES);
  914. zcrypt_qdepth_mask(workarea);
  915. len += sprinthx("Waiting work element counts",
  916. resp_buff+len, workarea, AP_DEVICES);
  917. zcrypt_perdev_reqcnt((int *) workarea);
  918. len += sprinthx4("Per-device successfully completed request counts",
  919. resp_buff+len,(unsigned int *) workarea, AP_DEVICES);
  920. *eof = 1;
  921. memset((void *) workarea, 0x00, AP_DEVICES * sizeof(unsigned int));
  922. return len;
  923. }
  924. static void zcrypt_disable_card(int index)
  925. {
  926. struct zcrypt_device *zdev;
  927. spin_lock_bh(&zcrypt_device_lock);
  928. list_for_each_entry(zdev, &zcrypt_device_list, list)
  929. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  930. zdev->online = 0;
  931. ap_flush_queue(zdev->ap_dev);
  932. break;
  933. }
  934. spin_unlock_bh(&zcrypt_device_lock);
  935. }
  936. static void zcrypt_enable_card(int index)
  937. {
  938. struct zcrypt_device *zdev;
  939. spin_lock_bh(&zcrypt_device_lock);
  940. list_for_each_entry(zdev, &zcrypt_device_list, list)
  941. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  942. zdev->online = 1;
  943. break;
  944. }
  945. spin_unlock_bh(&zcrypt_device_lock);
  946. }
  947. static int zcrypt_status_write(struct file *file, const char __user *buffer,
  948. unsigned long count, void *data)
  949. {
  950. unsigned char *lbuf, *ptr;
  951. unsigned long local_count;
  952. int j;
  953. if (count <= 0)
  954. return 0;
  955. #define LBUFSIZE 1200UL
  956. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  957. if (!lbuf) {
  958. PRINTK("kmalloc failed!\n");
  959. return 0;
  960. }
  961. local_count = min(LBUFSIZE - 1, count);
  962. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  963. kfree(lbuf);
  964. return -EFAULT;
  965. }
  966. lbuf[local_count] = '\0';
  967. ptr = strstr(lbuf, "Online devices");
  968. if (!ptr) {
  969. PRINTK("Unable to parse data (missing \"Online devices\")\n");
  970. goto out;
  971. }
  972. ptr = strstr(ptr, "\n");
  973. if (!ptr) {
  974. PRINTK("Unable to parse data (missing newline "
  975. "after \"Online devices\")\n");
  976. goto out;
  977. }
  978. ptr++;
  979. if (strstr(ptr, "Waiting work element counts") == NULL) {
  980. PRINTK("Unable to parse data (missing "
  981. "\"Waiting work element counts\")\n");
  982. goto out;
  983. }
  984. for (j = 0; j < 64 && *ptr; ptr++) {
  985. /**
  986. * '0' for no device, '1' for PCICA, '2' for PCICC,
  987. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  988. * '5' for CEX2C and '6' for CEX2A'
  989. */
  990. if (*ptr >= '0' && *ptr <= '6')
  991. j++;
  992. else if (*ptr == 'd' || *ptr == 'D')
  993. zcrypt_disable_card(j++);
  994. else if (*ptr == 'e' || *ptr == 'E')
  995. zcrypt_enable_card(j++);
  996. else if (*ptr != ' ' && *ptr != '\t')
  997. break;
  998. }
  999. out:
  1000. kfree(lbuf);
  1001. return count;
  1002. }
  1003. static int zcrypt_rng_device_count;
  1004. static u32 *zcrypt_rng_buffer;
  1005. static int zcrypt_rng_buffer_index;
  1006. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1007. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1008. {
  1009. int rc;
  1010. /**
  1011. * We don't need locking here because the RNG API guarantees serialized
  1012. * read method calls.
  1013. */
  1014. if (zcrypt_rng_buffer_index == 0) {
  1015. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1016. if (rc < 0)
  1017. return -EIO;
  1018. zcrypt_rng_buffer_index = rc / sizeof *data;
  1019. }
  1020. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1021. return sizeof *data;
  1022. }
  1023. static struct hwrng zcrypt_rng_dev = {
  1024. .name = "zcrypt",
  1025. .data_read = zcrypt_rng_data_read,
  1026. };
  1027. static int zcrypt_rng_device_add(void)
  1028. {
  1029. int rc = 0;
  1030. mutex_lock(&zcrypt_rng_mutex);
  1031. if (zcrypt_rng_device_count == 0) {
  1032. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1033. if (!zcrypt_rng_buffer) {
  1034. rc = -ENOMEM;
  1035. goto out;
  1036. }
  1037. zcrypt_rng_buffer_index = 0;
  1038. rc = hwrng_register(&zcrypt_rng_dev);
  1039. if (rc)
  1040. goto out_free;
  1041. zcrypt_rng_device_count = 1;
  1042. } else
  1043. zcrypt_rng_device_count++;
  1044. mutex_unlock(&zcrypt_rng_mutex);
  1045. return 0;
  1046. out_free:
  1047. free_page((unsigned long) zcrypt_rng_buffer);
  1048. out:
  1049. mutex_unlock(&zcrypt_rng_mutex);
  1050. return rc;
  1051. }
  1052. static void zcrypt_rng_device_remove(void)
  1053. {
  1054. mutex_lock(&zcrypt_rng_mutex);
  1055. zcrypt_rng_device_count--;
  1056. if (zcrypt_rng_device_count == 0) {
  1057. hwrng_unregister(&zcrypt_rng_dev);
  1058. free_page((unsigned long) zcrypt_rng_buffer);
  1059. }
  1060. mutex_unlock(&zcrypt_rng_mutex);
  1061. }
  1062. /**
  1063. * The module initialization code.
  1064. */
  1065. int __init zcrypt_api_init(void)
  1066. {
  1067. int rc;
  1068. /* Register the request sprayer. */
  1069. rc = misc_register(&zcrypt_misc_device);
  1070. if (rc < 0) {
  1071. PRINTKW(KERN_ERR "misc_register (minor %d) failed with %d\n",
  1072. zcrypt_misc_device.minor, rc);
  1073. goto out;
  1074. }
  1075. /* Set up the proc file system */
  1076. zcrypt_entry = create_proc_entry("driver/z90crypt", 0644, NULL);
  1077. if (!zcrypt_entry) {
  1078. PRINTK("Couldn't create z90crypt proc entry\n");
  1079. rc = -ENOMEM;
  1080. goto out_misc;
  1081. }
  1082. zcrypt_entry->data = NULL;
  1083. zcrypt_entry->read_proc = zcrypt_status_read;
  1084. zcrypt_entry->write_proc = zcrypt_status_write;
  1085. return 0;
  1086. out_misc:
  1087. misc_deregister(&zcrypt_misc_device);
  1088. out:
  1089. return rc;
  1090. }
  1091. /**
  1092. * The module termination code.
  1093. */
  1094. void zcrypt_api_exit(void)
  1095. {
  1096. remove_proc_entry("driver/z90crypt", NULL);
  1097. misc_deregister(&zcrypt_misc_device);
  1098. }
  1099. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  1100. module_init(zcrypt_api_init);
  1101. module_exit(zcrypt_api_exit);
  1102. #endif