zcrypt_api.c 32 KB

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