zcrypt_api.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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/seq_file.h>
  36. #include <linux/compat.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/slab.h>
  39. #include <asm/atomic.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/hw_random.h>
  42. #include "zcrypt_api.h"
  43. /*
  44. * Module description.
  45. */
  46. MODULE_AUTHOR("IBM Corporation");
  47. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, "
  48. "Copyright 2001, 2006 IBM Corporation");
  49. MODULE_LICENSE("GPL");
  50. static DEFINE_SPINLOCK(zcrypt_device_lock);
  51. static LIST_HEAD(zcrypt_device_list);
  52. static int zcrypt_device_count = 0;
  53. static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  54. static int zcrypt_rng_device_add(void);
  55. static void zcrypt_rng_device_remove(void);
  56. /*
  57. * Device attributes common for all crypto devices.
  58. */
  59. static ssize_t zcrypt_type_show(struct device *dev,
  60. struct device_attribute *attr, char *buf)
  61. {
  62. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  63. return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
  64. }
  65. static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
  66. static ssize_t zcrypt_online_show(struct device *dev,
  67. struct device_attribute *attr, char *buf)
  68. {
  69. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  70. return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
  71. }
  72. static ssize_t zcrypt_online_store(struct device *dev,
  73. struct device_attribute *attr,
  74. const char *buf, size_t count)
  75. {
  76. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  77. int online;
  78. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  79. return -EINVAL;
  80. zdev->online = online;
  81. if (!online)
  82. ap_flush_queue(zdev->ap_dev);
  83. return count;
  84. }
  85. static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
  86. static struct attribute * zcrypt_device_attrs[] = {
  87. &dev_attr_type.attr,
  88. &dev_attr_online.attr,
  89. NULL,
  90. };
  91. static struct attribute_group zcrypt_device_attr_group = {
  92. .attrs = zcrypt_device_attrs,
  93. };
  94. /**
  95. * __zcrypt_increase_preference(): Increase preference of a crypto device.
  96. * @zdev: Pointer the crypto device
  97. *
  98. * Move the device towards the head of the device list.
  99. * Need to be called while holding the zcrypt device list lock.
  100. * Note: cards with speed_rating of 0 are kept at the end of the list.
  101. */
  102. static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
  103. {
  104. struct zcrypt_device *tmp;
  105. struct list_head *l;
  106. if (zdev->speed_rating == 0)
  107. return;
  108. for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
  109. tmp = list_entry(l, struct zcrypt_device, list);
  110. if ((tmp->request_count + 1) * tmp->speed_rating <=
  111. (zdev->request_count + 1) * zdev->speed_rating &&
  112. tmp->speed_rating != 0)
  113. break;
  114. }
  115. if (l == zdev->list.prev)
  116. return;
  117. /* Move zdev behind l */
  118. list_move(&zdev->list, l);
  119. }
  120. /**
  121. * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
  122. * @zdev: Pointer to a crypto device.
  123. *
  124. * Move the device towards the tail of the device list.
  125. * Need to be called while holding the zcrypt device list lock.
  126. * Note: cards with speed_rating of 0 are kept at the end of the list.
  127. */
  128. static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
  129. {
  130. struct zcrypt_device *tmp;
  131. struct list_head *l;
  132. if (zdev->speed_rating == 0)
  133. return;
  134. for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
  135. tmp = list_entry(l, struct zcrypt_device, list);
  136. if ((tmp->request_count + 1) * tmp->speed_rating >
  137. (zdev->request_count + 1) * zdev->speed_rating ||
  138. tmp->speed_rating == 0)
  139. break;
  140. }
  141. if (l == zdev->list.next)
  142. return;
  143. /* Move zdev before l */
  144. list_move_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. atomic_inc(&zcrypt_open_count);
  274. return nonseekable_open(inode, filp);
  275. }
  276. /**
  277. * zcrypt_release(): Count number of users.
  278. *
  279. * Device close function to count number of users.
  280. */
  281. static int zcrypt_release(struct inode *inode, struct file *filp)
  282. {
  283. atomic_dec(&zcrypt_open_count);
  284. return 0;
  285. }
  286. /*
  287. * zcrypt ioctls.
  288. */
  289. static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
  290. {
  291. struct zcrypt_device *zdev;
  292. int rc;
  293. if (mex->outputdatalength < mex->inputdatalength)
  294. return -EINVAL;
  295. /*
  296. * As long as outputdatalength is big enough, we can set the
  297. * outputdatalength equal to the inputdatalength, since that is the
  298. * number of bytes we will copy in any case
  299. */
  300. mex->outputdatalength = mex->inputdatalength;
  301. spin_lock_bh(&zcrypt_device_lock);
  302. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  303. if (!zdev->online ||
  304. !zdev->ops->rsa_modexpo ||
  305. zdev->min_mod_size > mex->inputdatalength ||
  306. zdev->max_mod_size < mex->inputdatalength)
  307. continue;
  308. zcrypt_device_get(zdev);
  309. get_device(&zdev->ap_dev->device);
  310. zdev->request_count++;
  311. __zcrypt_decrease_preference(zdev);
  312. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  313. spin_unlock_bh(&zcrypt_device_lock);
  314. rc = zdev->ops->rsa_modexpo(zdev, mex);
  315. spin_lock_bh(&zcrypt_device_lock);
  316. module_put(zdev->ap_dev->drv->driver.owner);
  317. }
  318. else
  319. rc = -EAGAIN;
  320. zdev->request_count--;
  321. __zcrypt_increase_preference(zdev);
  322. put_device(&zdev->ap_dev->device);
  323. zcrypt_device_put(zdev);
  324. spin_unlock_bh(&zcrypt_device_lock);
  325. return rc;
  326. }
  327. spin_unlock_bh(&zcrypt_device_lock);
  328. return -ENODEV;
  329. }
  330. static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
  331. {
  332. struct zcrypt_device *zdev;
  333. unsigned long long z1, z2, z3;
  334. int rc, copied;
  335. if (crt->outputdatalength < crt->inputdatalength ||
  336. (crt->inputdatalength & 1))
  337. return -EINVAL;
  338. /*
  339. * As long as outputdatalength is big enough, we can set the
  340. * outputdatalength equal to the inputdatalength, since that is the
  341. * number of bytes we will copy in any case
  342. */
  343. crt->outputdatalength = crt->inputdatalength;
  344. copied = 0;
  345. restart:
  346. spin_lock_bh(&zcrypt_device_lock);
  347. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  348. if (!zdev->online ||
  349. !zdev->ops->rsa_modexpo_crt ||
  350. zdev->min_mod_size > crt->inputdatalength ||
  351. zdev->max_mod_size < crt->inputdatalength)
  352. continue;
  353. if (zdev->short_crt && crt->inputdatalength > 240) {
  354. /*
  355. * Check inputdata for leading zeros for cards
  356. * that can't handle np_prime, bp_key, or
  357. * u_mult_inv > 128 bytes.
  358. */
  359. if (copied == 0) {
  360. unsigned int len;
  361. spin_unlock_bh(&zcrypt_device_lock);
  362. /* len is max 256 / 2 - 120 = 8 */
  363. len = crt->inputdatalength / 2 - 120;
  364. if (len > sizeof(z1))
  365. return -EFAULT;
  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. .llseek = no_llseek,
  833. };
  834. /*
  835. * Misc device.
  836. */
  837. static struct miscdevice zcrypt_misc_device = {
  838. .minor = MISC_DYNAMIC_MINOR,
  839. .name = "z90crypt",
  840. .fops = &zcrypt_fops,
  841. };
  842. /*
  843. * Deprecated /proc entry support.
  844. */
  845. static struct proc_dir_entry *zcrypt_entry;
  846. static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
  847. {
  848. int i;
  849. for (i = 0; i < len; i++)
  850. seq_printf(m, "%01x", (unsigned int) addr[i]);
  851. seq_putc(m, ' ');
  852. }
  853. static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
  854. {
  855. int inl, c, cx;
  856. seq_printf(m, " ");
  857. inl = 0;
  858. for (c = 0; c < (len / 16); c++) {
  859. sprintcl(m, addr+inl, 16);
  860. inl += 16;
  861. }
  862. cx = len%16;
  863. if (cx) {
  864. sprintcl(m, addr+inl, cx);
  865. inl += cx;
  866. }
  867. seq_putc(m, '\n');
  868. }
  869. static void sprinthx(unsigned char *title, struct seq_file *m,
  870. unsigned char *addr, unsigned int len)
  871. {
  872. int inl, r, rx;
  873. seq_printf(m, "\n%s\n", title);
  874. inl = 0;
  875. for (r = 0; r < (len / 64); r++) {
  876. sprintrw(m, addr+inl, 64);
  877. inl += 64;
  878. }
  879. rx = len % 64;
  880. if (rx) {
  881. sprintrw(m, addr+inl, rx);
  882. inl += rx;
  883. }
  884. seq_putc(m, '\n');
  885. }
  886. static void sprinthx4(unsigned char *title, struct seq_file *m,
  887. unsigned int *array, unsigned int len)
  888. {
  889. int r;
  890. seq_printf(m, "\n%s\n", title);
  891. for (r = 0; r < len; r++) {
  892. if ((r % 8) == 0)
  893. seq_printf(m, " ");
  894. seq_printf(m, "%08X ", array[r]);
  895. if ((r % 8) == 7)
  896. seq_putc(m, '\n');
  897. }
  898. seq_putc(m, '\n');
  899. }
  900. static int zcrypt_proc_show(struct seq_file *m, void *v)
  901. {
  902. char workarea[sizeof(int) * AP_DEVICES];
  903. seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
  904. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  905. seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
  906. seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
  907. seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
  908. seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
  909. seq_printf(m, "PCIXCC MCL2 count: %d\n",
  910. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  911. seq_printf(m, "PCIXCC MCL3 count: %d\n",
  912. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  913. seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
  914. seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
  915. seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
  916. seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
  917. seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
  918. seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
  919. seq_printf(m, "Total open handles: %d\n\n",
  920. atomic_read(&zcrypt_open_count));
  921. zcrypt_status_mask(workarea);
  922. sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  923. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
  924. m, workarea, AP_DEVICES);
  925. zcrypt_qdepth_mask(workarea);
  926. sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
  927. zcrypt_perdev_reqcnt((int *) workarea);
  928. sprinthx4("Per-device successfully completed request counts",
  929. m, (unsigned int *) workarea, AP_DEVICES);
  930. return 0;
  931. }
  932. static int zcrypt_proc_open(struct inode *inode, struct file *file)
  933. {
  934. return single_open(file, zcrypt_proc_show, NULL);
  935. }
  936. static void zcrypt_disable_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 = 0;
  943. ap_flush_queue(zdev->ap_dev);
  944. break;
  945. }
  946. spin_unlock_bh(&zcrypt_device_lock);
  947. }
  948. static void zcrypt_enable_card(int index)
  949. {
  950. struct zcrypt_device *zdev;
  951. spin_lock_bh(&zcrypt_device_lock);
  952. list_for_each_entry(zdev, &zcrypt_device_list, list)
  953. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  954. zdev->online = 1;
  955. break;
  956. }
  957. spin_unlock_bh(&zcrypt_device_lock);
  958. }
  959. static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
  960. size_t count, loff_t *pos)
  961. {
  962. unsigned char *lbuf, *ptr;
  963. size_t local_count;
  964. int j;
  965. if (count <= 0)
  966. return 0;
  967. #define LBUFSIZE 1200UL
  968. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  969. if (!lbuf)
  970. return 0;
  971. local_count = min(LBUFSIZE - 1, count);
  972. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  973. kfree(lbuf);
  974. return -EFAULT;
  975. }
  976. lbuf[local_count] = '\0';
  977. ptr = strstr(lbuf, "Online devices");
  978. if (!ptr)
  979. goto out;
  980. ptr = strstr(ptr, "\n");
  981. if (!ptr)
  982. goto out;
  983. ptr++;
  984. if (strstr(ptr, "Waiting work element counts") == NULL)
  985. goto out;
  986. for (j = 0; j < 64 && *ptr; ptr++) {
  987. /*
  988. * '0' for no device, '1' for PCICA, '2' for PCICC,
  989. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  990. * '5' for CEX2C and '6' for CEX2A'
  991. * '7' for CEX3C and '8' for CEX3A
  992. */
  993. if (*ptr >= '0' && *ptr <= '8')
  994. j++;
  995. else if (*ptr == 'd' || *ptr == 'D')
  996. zcrypt_disable_card(j++);
  997. else if (*ptr == 'e' || *ptr == 'E')
  998. zcrypt_enable_card(j++);
  999. else if (*ptr != ' ' && *ptr != '\t')
  1000. break;
  1001. }
  1002. out:
  1003. kfree(lbuf);
  1004. return count;
  1005. }
  1006. static const struct file_operations zcrypt_proc_fops = {
  1007. .owner = THIS_MODULE,
  1008. .open = zcrypt_proc_open,
  1009. .read = seq_read,
  1010. .llseek = seq_lseek,
  1011. .release = single_release,
  1012. .write = zcrypt_proc_write,
  1013. };
  1014. static int zcrypt_rng_device_count;
  1015. static u32 *zcrypt_rng_buffer;
  1016. static int zcrypt_rng_buffer_index;
  1017. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1018. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1019. {
  1020. int rc;
  1021. /*
  1022. * We don't need locking here because the RNG API guarantees serialized
  1023. * read method calls.
  1024. */
  1025. if (zcrypt_rng_buffer_index == 0) {
  1026. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1027. if (rc < 0)
  1028. return -EIO;
  1029. zcrypt_rng_buffer_index = rc / sizeof *data;
  1030. }
  1031. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1032. return sizeof *data;
  1033. }
  1034. static struct hwrng zcrypt_rng_dev = {
  1035. .name = "zcrypt",
  1036. .data_read = zcrypt_rng_data_read,
  1037. };
  1038. static int zcrypt_rng_device_add(void)
  1039. {
  1040. int rc = 0;
  1041. mutex_lock(&zcrypt_rng_mutex);
  1042. if (zcrypt_rng_device_count == 0) {
  1043. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1044. if (!zcrypt_rng_buffer) {
  1045. rc = -ENOMEM;
  1046. goto out;
  1047. }
  1048. zcrypt_rng_buffer_index = 0;
  1049. rc = hwrng_register(&zcrypt_rng_dev);
  1050. if (rc)
  1051. goto out_free;
  1052. zcrypt_rng_device_count = 1;
  1053. } else
  1054. zcrypt_rng_device_count++;
  1055. mutex_unlock(&zcrypt_rng_mutex);
  1056. return 0;
  1057. out_free:
  1058. free_page((unsigned long) zcrypt_rng_buffer);
  1059. out:
  1060. mutex_unlock(&zcrypt_rng_mutex);
  1061. return rc;
  1062. }
  1063. static void zcrypt_rng_device_remove(void)
  1064. {
  1065. mutex_lock(&zcrypt_rng_mutex);
  1066. zcrypt_rng_device_count--;
  1067. if (zcrypt_rng_device_count == 0) {
  1068. hwrng_unregister(&zcrypt_rng_dev);
  1069. free_page((unsigned long) zcrypt_rng_buffer);
  1070. }
  1071. mutex_unlock(&zcrypt_rng_mutex);
  1072. }
  1073. /**
  1074. * zcrypt_api_init(): Module initialization.
  1075. *
  1076. * The module initialization code.
  1077. */
  1078. int __init zcrypt_api_init(void)
  1079. {
  1080. int rc;
  1081. /* Register the request sprayer. */
  1082. rc = misc_register(&zcrypt_misc_device);
  1083. if (rc < 0)
  1084. goto out;
  1085. /* Set up the proc file system */
  1086. zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
  1087. if (!zcrypt_entry) {
  1088. rc = -ENOMEM;
  1089. goto out_misc;
  1090. }
  1091. return 0;
  1092. out_misc:
  1093. misc_deregister(&zcrypt_misc_device);
  1094. out:
  1095. return rc;
  1096. }
  1097. /**
  1098. * zcrypt_api_exit(): Module termination.
  1099. *
  1100. * The module termination code.
  1101. */
  1102. void zcrypt_api_exit(void)
  1103. {
  1104. remove_proc_entry("driver/z90crypt", NULL);
  1105. misc_deregister(&zcrypt_misc_device);
  1106. }
  1107. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  1108. module_init(zcrypt_api_init);
  1109. module_exit(zcrypt_api_exit);
  1110. #endif