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