zcrypt_api.c 32 KB

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