zcrypt_api.c 32 KB

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