zcrypt_api.c 32 KB

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