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/slab.h>
  38. #include <asm/atomic.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/hw_random.h>
  41. #include "zcrypt_api.h"
  42. /*
  43. * Module description.
  44. */
  45. MODULE_AUTHOR("IBM Corporation");
  46. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, "
  47. "Copyright 2001, 2006 IBM Corporation");
  48. MODULE_LICENSE("GPL");
  49. static DEFINE_SPINLOCK(zcrypt_device_lock);
  50. static LIST_HEAD(zcrypt_device_list);
  51. static int zcrypt_device_count = 0;
  52. static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  53. static int zcrypt_rng_device_add(void);
  54. static void zcrypt_rng_device_remove(void);
  55. /*
  56. * Device attributes common for all crypto devices.
  57. */
  58. static ssize_t zcrypt_type_show(struct device *dev,
  59. struct device_attribute *attr, char *buf)
  60. {
  61. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  62. return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
  63. }
  64. static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
  65. static ssize_t zcrypt_online_show(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  69. return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
  70. }
  71. static ssize_t zcrypt_online_store(struct device *dev,
  72. struct device_attribute *attr,
  73. const char *buf, size_t count)
  74. {
  75. struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  76. int online;
  77. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  78. return -EINVAL;
  79. zdev->online = online;
  80. if (!online)
  81. ap_flush_queue(zdev->ap_dev);
  82. return count;
  83. }
  84. static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
  85. static struct attribute * zcrypt_device_attrs[] = {
  86. &dev_attr_type.attr,
  87. &dev_attr_online.attr,
  88. NULL,
  89. };
  90. static struct attribute_group zcrypt_device_attr_group = {
  91. .attrs = zcrypt_device_attrs,
  92. };
  93. /**
  94. * __zcrypt_increase_preference(): Increase preference of a crypto device.
  95. * @zdev: Pointer the crypto device
  96. *
  97. * Move the device towards the head of the device list.
  98. * Need to be called while holding the zcrypt device list lock.
  99. * Note: cards with speed_rating of 0 are kept at the end of the list.
  100. */
  101. static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
  102. {
  103. struct zcrypt_device *tmp;
  104. struct list_head *l;
  105. if (zdev->speed_rating == 0)
  106. return;
  107. for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
  108. tmp = list_entry(l, struct zcrypt_device, list);
  109. if ((tmp->request_count + 1) * tmp->speed_rating <=
  110. (zdev->request_count + 1) * zdev->speed_rating &&
  111. tmp->speed_rating != 0)
  112. break;
  113. }
  114. if (l == zdev->list.prev)
  115. return;
  116. /* Move zdev behind l */
  117. list_move(&zdev->list, l);
  118. }
  119. /**
  120. * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
  121. * @zdev: Pointer to a crypto device.
  122. *
  123. * Move the device towards the tail of the device list.
  124. * Need to be called while holding the zcrypt device list lock.
  125. * Note: cards with speed_rating of 0 are kept at the end of the list.
  126. */
  127. static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
  128. {
  129. struct zcrypt_device *tmp;
  130. struct list_head *l;
  131. if (zdev->speed_rating == 0)
  132. return;
  133. for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
  134. tmp = list_entry(l, struct zcrypt_device, list);
  135. if ((tmp->request_count + 1) * tmp->speed_rating >
  136. (zdev->request_count + 1) * zdev->speed_rating ||
  137. tmp->speed_rating == 0)
  138. break;
  139. }
  140. if (l == zdev->list.next)
  141. return;
  142. /* Move zdev before l */
  143. list_move_tail(&zdev->list, l);
  144. }
  145. static void zcrypt_device_release(struct kref *kref)
  146. {
  147. struct zcrypt_device *zdev =
  148. container_of(kref, struct zcrypt_device, refcount);
  149. zcrypt_device_free(zdev);
  150. }
  151. void zcrypt_device_get(struct zcrypt_device *zdev)
  152. {
  153. kref_get(&zdev->refcount);
  154. }
  155. EXPORT_SYMBOL(zcrypt_device_get);
  156. int zcrypt_device_put(struct zcrypt_device *zdev)
  157. {
  158. return kref_put(&zdev->refcount, zcrypt_device_release);
  159. }
  160. EXPORT_SYMBOL(zcrypt_device_put);
  161. struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
  162. {
  163. struct zcrypt_device *zdev;
  164. zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
  165. if (!zdev)
  166. return NULL;
  167. zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
  168. if (!zdev->reply.message)
  169. goto out_free;
  170. zdev->reply.length = max_response_size;
  171. spin_lock_init(&zdev->lock);
  172. INIT_LIST_HEAD(&zdev->list);
  173. return zdev;
  174. out_free:
  175. kfree(zdev);
  176. return NULL;
  177. }
  178. EXPORT_SYMBOL(zcrypt_device_alloc);
  179. void zcrypt_device_free(struct zcrypt_device *zdev)
  180. {
  181. kfree(zdev->reply.message);
  182. kfree(zdev);
  183. }
  184. EXPORT_SYMBOL(zcrypt_device_free);
  185. /**
  186. * zcrypt_device_register() - Register a crypto device.
  187. * @zdev: Pointer to a crypto device
  188. *
  189. * Register a crypto device. Returns 0 if successful.
  190. */
  191. int zcrypt_device_register(struct zcrypt_device *zdev)
  192. {
  193. int rc;
  194. rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
  195. &zcrypt_device_attr_group);
  196. if (rc)
  197. goto out;
  198. get_device(&zdev->ap_dev->device);
  199. kref_init(&zdev->refcount);
  200. spin_lock_bh(&zcrypt_device_lock);
  201. zdev->online = 1; /* New devices are online by default. */
  202. list_add_tail(&zdev->list, &zcrypt_device_list);
  203. __zcrypt_increase_preference(zdev);
  204. zcrypt_device_count++;
  205. spin_unlock_bh(&zcrypt_device_lock);
  206. if (zdev->ops->rng) {
  207. rc = zcrypt_rng_device_add();
  208. if (rc)
  209. goto out_unregister;
  210. }
  211. return 0;
  212. out_unregister:
  213. spin_lock_bh(&zcrypt_device_lock);
  214. zcrypt_device_count--;
  215. list_del_init(&zdev->list);
  216. spin_unlock_bh(&zcrypt_device_lock);
  217. sysfs_remove_group(&zdev->ap_dev->device.kobj,
  218. &zcrypt_device_attr_group);
  219. put_device(&zdev->ap_dev->device);
  220. zcrypt_device_put(zdev);
  221. out:
  222. return rc;
  223. }
  224. EXPORT_SYMBOL(zcrypt_device_register);
  225. /**
  226. * zcrypt_device_unregister(): Unregister a crypto device.
  227. * @zdev: Pointer to crypto device
  228. *
  229. * Unregister a crypto device.
  230. */
  231. void zcrypt_device_unregister(struct zcrypt_device *zdev)
  232. {
  233. if (zdev->ops->rng)
  234. zcrypt_rng_device_remove();
  235. spin_lock_bh(&zcrypt_device_lock);
  236. zcrypt_device_count--;
  237. list_del_init(&zdev->list);
  238. spin_unlock_bh(&zcrypt_device_lock);
  239. sysfs_remove_group(&zdev->ap_dev->device.kobj,
  240. &zcrypt_device_attr_group);
  241. put_device(&zdev->ap_dev->device);
  242. zcrypt_device_put(zdev);
  243. }
  244. EXPORT_SYMBOL(zcrypt_device_unregister);
  245. /**
  246. * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
  247. *
  248. * This function is not supported beyond zcrypt 1.3.1.
  249. */
  250. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  251. size_t count, loff_t *f_pos)
  252. {
  253. return -EPERM;
  254. }
  255. /**
  256. * zcrypt_write(): Not allowed.
  257. *
  258. * Write is is not allowed
  259. */
  260. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  261. size_t count, loff_t *f_pos)
  262. {
  263. return -EPERM;
  264. }
  265. /**
  266. * zcrypt_open(): Count number of users.
  267. *
  268. * Device open function to count number of users.
  269. */
  270. static int zcrypt_open(struct inode *inode, struct file *filp)
  271. {
  272. atomic_inc(&zcrypt_open_count);
  273. return nonseekable_open(inode, filp);
  274. }
  275. /**
  276. * zcrypt_release(): Count number of users.
  277. *
  278. * Device close function to count number of users.
  279. */
  280. static int zcrypt_release(struct inode *inode, struct file *filp)
  281. {
  282. atomic_dec(&zcrypt_open_count);
  283. return 0;
  284. }
  285. /*
  286. * zcrypt ioctls.
  287. */
  288. static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
  289. {
  290. struct zcrypt_device *zdev;
  291. int rc;
  292. if (mex->outputdatalength < mex->inputdatalength)
  293. return -EINVAL;
  294. /*
  295. * As long as outputdatalength is big enough, we can set the
  296. * outputdatalength equal to the inputdatalength, since that is the
  297. * number of bytes we will copy in any case
  298. */
  299. mex->outputdatalength = mex->inputdatalength;
  300. spin_lock_bh(&zcrypt_device_lock);
  301. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  302. if (!zdev->online ||
  303. !zdev->ops->rsa_modexpo ||
  304. zdev->min_mod_size > mex->inputdatalength ||
  305. zdev->max_mod_size < mex->inputdatalength)
  306. continue;
  307. zcrypt_device_get(zdev);
  308. get_device(&zdev->ap_dev->device);
  309. zdev->request_count++;
  310. __zcrypt_decrease_preference(zdev);
  311. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  312. spin_unlock_bh(&zcrypt_device_lock);
  313. rc = zdev->ops->rsa_modexpo(zdev, mex);
  314. spin_lock_bh(&zcrypt_device_lock);
  315. module_put(zdev->ap_dev->drv->driver.owner);
  316. }
  317. else
  318. rc = -EAGAIN;
  319. zdev->request_count--;
  320. __zcrypt_increase_preference(zdev);
  321. put_device(&zdev->ap_dev->device);
  322. zcrypt_device_put(zdev);
  323. spin_unlock_bh(&zcrypt_device_lock);
  324. return rc;
  325. }
  326. spin_unlock_bh(&zcrypt_device_lock);
  327. return -ENODEV;
  328. }
  329. static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
  330. {
  331. struct zcrypt_device *zdev;
  332. unsigned long long z1, z2, z3;
  333. int rc, copied;
  334. if (crt->outputdatalength < crt->inputdatalength ||
  335. (crt->inputdatalength & 1))
  336. return -EINVAL;
  337. /*
  338. * As long as outputdatalength is big enough, we can set the
  339. * outputdatalength equal to the inputdatalength, since that is the
  340. * number of bytes we will copy in any case
  341. */
  342. crt->outputdatalength = crt->inputdatalength;
  343. copied = 0;
  344. restart:
  345. spin_lock_bh(&zcrypt_device_lock);
  346. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  347. if (!zdev->online ||
  348. !zdev->ops->rsa_modexpo_crt ||
  349. zdev->min_mod_size > crt->inputdatalength ||
  350. zdev->max_mod_size < crt->inputdatalength)
  351. continue;
  352. if (zdev->short_crt && crt->inputdatalength > 240) {
  353. /*
  354. * Check inputdata for leading zeros for cards
  355. * that can't handle np_prime, bp_key, or
  356. * u_mult_inv > 128 bytes.
  357. */
  358. if (copied == 0) {
  359. unsigned int len;
  360. spin_unlock_bh(&zcrypt_device_lock);
  361. /* len is max 256 / 2 - 120 = 8 */
  362. len = crt->inputdatalength / 2 - 120;
  363. if (len > sizeof(z1))
  364. return -EFAULT;
  365. z1 = z2 = z3 = 0;
  366. if (copy_from_user(&z1, crt->np_prime, len) ||
  367. copy_from_user(&z2, crt->bp_key, len) ||
  368. copy_from_user(&z3, crt->u_mult_inv, len))
  369. return -EFAULT;
  370. copied = 1;
  371. /*
  372. * We have to restart device lookup -
  373. * the device list may have changed by now.
  374. */
  375. goto restart;
  376. }
  377. if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
  378. /* The device can't handle this request. */
  379. continue;
  380. }
  381. zcrypt_device_get(zdev);
  382. get_device(&zdev->ap_dev->device);
  383. zdev->request_count++;
  384. __zcrypt_decrease_preference(zdev);
  385. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  386. spin_unlock_bh(&zcrypt_device_lock);
  387. rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
  388. spin_lock_bh(&zcrypt_device_lock);
  389. module_put(zdev->ap_dev->drv->driver.owner);
  390. }
  391. else
  392. rc = -EAGAIN;
  393. zdev->request_count--;
  394. __zcrypt_increase_preference(zdev);
  395. put_device(&zdev->ap_dev->device);
  396. zcrypt_device_put(zdev);
  397. spin_unlock_bh(&zcrypt_device_lock);
  398. return rc;
  399. }
  400. spin_unlock_bh(&zcrypt_device_lock);
  401. return -ENODEV;
  402. }
  403. static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  404. {
  405. struct zcrypt_device *zdev;
  406. int rc;
  407. spin_lock_bh(&zcrypt_device_lock);
  408. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  409. if (!zdev->online || !zdev->ops->send_cprb ||
  410. (xcRB->user_defined != AUTOSELECT &&
  411. AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined)
  412. )
  413. continue;
  414. zcrypt_device_get(zdev);
  415. get_device(&zdev->ap_dev->device);
  416. zdev->request_count++;
  417. __zcrypt_decrease_preference(zdev);
  418. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  419. spin_unlock_bh(&zcrypt_device_lock);
  420. rc = zdev->ops->send_cprb(zdev, xcRB);
  421. spin_lock_bh(&zcrypt_device_lock);
  422. module_put(zdev->ap_dev->drv->driver.owner);
  423. }
  424. else
  425. rc = -EAGAIN;
  426. zdev->request_count--;
  427. __zcrypt_increase_preference(zdev);
  428. put_device(&zdev->ap_dev->device);
  429. zcrypt_device_put(zdev);
  430. spin_unlock_bh(&zcrypt_device_lock);
  431. return rc;
  432. }
  433. spin_unlock_bh(&zcrypt_device_lock);
  434. return -ENODEV;
  435. }
  436. static long zcrypt_rng(char *buffer)
  437. {
  438. struct zcrypt_device *zdev;
  439. int rc;
  440. spin_lock_bh(&zcrypt_device_lock);
  441. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  442. if (!zdev->online || !zdev->ops->rng)
  443. continue;
  444. zcrypt_device_get(zdev);
  445. get_device(&zdev->ap_dev->device);
  446. zdev->request_count++;
  447. __zcrypt_decrease_preference(zdev);
  448. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  449. spin_unlock_bh(&zcrypt_device_lock);
  450. rc = zdev->ops->rng(zdev, buffer);
  451. spin_lock_bh(&zcrypt_device_lock);
  452. module_put(zdev->ap_dev->drv->driver.owner);
  453. } else
  454. rc = -EAGAIN;
  455. zdev->request_count--;
  456. __zcrypt_increase_preference(zdev);
  457. put_device(&zdev->ap_dev->device);
  458. zcrypt_device_put(zdev);
  459. spin_unlock_bh(&zcrypt_device_lock);
  460. return rc;
  461. }
  462. spin_unlock_bh(&zcrypt_device_lock);
  463. return -ENODEV;
  464. }
  465. static void zcrypt_status_mask(char status[AP_DEVICES])
  466. {
  467. struct zcrypt_device *zdev;
  468. memset(status, 0, sizeof(char) * AP_DEVICES);
  469. spin_lock_bh(&zcrypt_device_lock);
  470. list_for_each_entry(zdev, &zcrypt_device_list, list)
  471. status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  472. zdev->online ? zdev->user_space_type : 0x0d;
  473. spin_unlock_bh(&zcrypt_device_lock);
  474. }
  475. static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
  476. {
  477. struct zcrypt_device *zdev;
  478. memset(qdepth, 0, sizeof(char) * AP_DEVICES);
  479. spin_lock_bh(&zcrypt_device_lock);
  480. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  481. spin_lock(&zdev->ap_dev->lock);
  482. qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  483. zdev->ap_dev->pendingq_count +
  484. zdev->ap_dev->requestq_count;
  485. spin_unlock(&zdev->ap_dev->lock);
  486. }
  487. spin_unlock_bh(&zcrypt_device_lock);
  488. }
  489. static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
  490. {
  491. struct zcrypt_device *zdev;
  492. memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
  493. spin_lock_bh(&zcrypt_device_lock);
  494. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  495. spin_lock(&zdev->ap_dev->lock);
  496. reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  497. zdev->ap_dev->total_request_count;
  498. spin_unlock(&zdev->ap_dev->lock);
  499. }
  500. spin_unlock_bh(&zcrypt_device_lock);
  501. }
  502. static int zcrypt_pendingq_count(void)
  503. {
  504. struct zcrypt_device *zdev;
  505. int pendingq_count = 0;
  506. spin_lock_bh(&zcrypt_device_lock);
  507. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  508. spin_lock(&zdev->ap_dev->lock);
  509. pendingq_count += zdev->ap_dev->pendingq_count;
  510. spin_unlock(&zdev->ap_dev->lock);
  511. }
  512. spin_unlock_bh(&zcrypt_device_lock);
  513. return pendingq_count;
  514. }
  515. static int zcrypt_requestq_count(void)
  516. {
  517. struct zcrypt_device *zdev;
  518. int requestq_count = 0;
  519. spin_lock_bh(&zcrypt_device_lock);
  520. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  521. spin_lock(&zdev->ap_dev->lock);
  522. requestq_count += zdev->ap_dev->requestq_count;
  523. spin_unlock(&zdev->ap_dev->lock);
  524. }
  525. spin_unlock_bh(&zcrypt_device_lock);
  526. return requestq_count;
  527. }
  528. static int zcrypt_count_type(int type)
  529. {
  530. struct zcrypt_device *zdev;
  531. int device_count = 0;
  532. spin_lock_bh(&zcrypt_device_lock);
  533. list_for_each_entry(zdev, &zcrypt_device_list, list)
  534. if (zdev->user_space_type == type)
  535. device_count++;
  536. spin_unlock_bh(&zcrypt_device_lock);
  537. return device_count;
  538. }
  539. /**
  540. * zcrypt_ica_status(): Old, depracted combi status call.
  541. *
  542. * Old, deprecated combi status call.
  543. */
  544. static long zcrypt_ica_status(struct file *filp, unsigned long arg)
  545. {
  546. struct ica_z90_status *pstat;
  547. int ret;
  548. pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
  549. if (!pstat)
  550. return -ENOMEM;
  551. pstat->totalcount = zcrypt_device_count;
  552. pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
  553. pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
  554. pstat->requestqWaitCount = zcrypt_requestq_count();
  555. pstat->pendingqWaitCount = zcrypt_pendingq_count();
  556. pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
  557. pstat->cryptoDomain = ap_domain_index;
  558. zcrypt_status_mask(pstat->status);
  559. zcrypt_qdepth_mask(pstat->qdepth);
  560. ret = 0;
  561. if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
  562. ret = -EFAULT;
  563. kfree(pstat);
  564. return ret;
  565. }
  566. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  567. unsigned long arg)
  568. {
  569. int rc;
  570. switch (cmd) {
  571. case ICARSAMODEXPO: {
  572. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  573. struct ica_rsa_modexpo mex;
  574. if (copy_from_user(&mex, umex, sizeof(mex)))
  575. return -EFAULT;
  576. do {
  577. rc = zcrypt_rsa_modexpo(&mex);
  578. } while (rc == -EAGAIN);
  579. if (rc)
  580. return rc;
  581. return put_user(mex.outputdatalength, &umex->outputdatalength);
  582. }
  583. case ICARSACRT: {
  584. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  585. struct ica_rsa_modexpo_crt crt;
  586. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  587. return -EFAULT;
  588. do {
  589. rc = zcrypt_rsa_crt(&crt);
  590. } while (rc == -EAGAIN);
  591. if (rc)
  592. return rc;
  593. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  594. }
  595. case ZSECSENDCPRB: {
  596. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  597. struct ica_xcRB xcRB;
  598. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  599. return -EFAULT;
  600. do {
  601. rc = zcrypt_send_cprb(&xcRB);
  602. } while (rc == -EAGAIN);
  603. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  604. return -EFAULT;
  605. return rc;
  606. }
  607. case Z90STAT_STATUS_MASK: {
  608. char status[AP_DEVICES];
  609. zcrypt_status_mask(status);
  610. if (copy_to_user((char __user *) arg, status,
  611. sizeof(char) * AP_DEVICES))
  612. return -EFAULT;
  613. return 0;
  614. }
  615. case Z90STAT_QDEPTH_MASK: {
  616. char qdepth[AP_DEVICES];
  617. zcrypt_qdepth_mask(qdepth);
  618. if (copy_to_user((char __user *) arg, qdepth,
  619. sizeof(char) * AP_DEVICES))
  620. return -EFAULT;
  621. return 0;
  622. }
  623. case Z90STAT_PERDEV_REQCNT: {
  624. int reqcnt[AP_DEVICES];
  625. zcrypt_perdev_reqcnt(reqcnt);
  626. if (copy_to_user((int __user *) arg, reqcnt,
  627. sizeof(int) * AP_DEVICES))
  628. return -EFAULT;
  629. return 0;
  630. }
  631. case Z90STAT_REQUESTQ_COUNT:
  632. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  633. case Z90STAT_PENDINGQ_COUNT:
  634. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  635. case Z90STAT_TOTALOPEN_COUNT:
  636. return put_user(atomic_read(&zcrypt_open_count),
  637. (int __user *) arg);
  638. case Z90STAT_DOMAIN_INDEX:
  639. return put_user(ap_domain_index, (int __user *) arg);
  640. /*
  641. * Deprecated ioctls. Don't add another device count ioctl,
  642. * you can count them yourself in the user space with the
  643. * output of the Z90STAT_STATUS_MASK ioctl.
  644. */
  645. case ICAZ90STATUS:
  646. return zcrypt_ica_status(filp, arg);
  647. case Z90STAT_TOTALCOUNT:
  648. return put_user(zcrypt_device_count, (int __user *) arg);
  649. case Z90STAT_PCICACOUNT:
  650. return put_user(zcrypt_count_type(ZCRYPT_PCICA),
  651. (int __user *) arg);
  652. case Z90STAT_PCICCCOUNT:
  653. return put_user(zcrypt_count_type(ZCRYPT_PCICC),
  654. (int __user *) arg);
  655. case Z90STAT_PCIXCCMCL2COUNT:
  656. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
  657. (int __user *) arg);
  658. case Z90STAT_PCIXCCMCL3COUNT:
  659. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  660. (int __user *) arg);
  661. case Z90STAT_PCIXCCCOUNT:
  662. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
  663. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  664. (int __user *) arg);
  665. case Z90STAT_CEX2CCOUNT:
  666. return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
  667. (int __user *) arg);
  668. case Z90STAT_CEX2ACOUNT:
  669. return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
  670. (int __user *) arg);
  671. default:
  672. /* unknown ioctl number */
  673. return -ENOIOCTLCMD;
  674. }
  675. }
  676. #ifdef CONFIG_COMPAT
  677. /*
  678. * ioctl32 conversion routines
  679. */
  680. struct compat_ica_rsa_modexpo {
  681. compat_uptr_t inputdata;
  682. unsigned int inputdatalength;
  683. compat_uptr_t outputdata;
  684. unsigned int outputdatalength;
  685. compat_uptr_t b_key;
  686. compat_uptr_t n_modulus;
  687. };
  688. static long trans_modexpo32(struct file *filp, unsigned int cmd,
  689. unsigned long arg)
  690. {
  691. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  692. struct compat_ica_rsa_modexpo mex32;
  693. struct ica_rsa_modexpo mex64;
  694. long rc;
  695. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  696. return -EFAULT;
  697. mex64.inputdata = compat_ptr(mex32.inputdata);
  698. mex64.inputdatalength = mex32.inputdatalength;
  699. mex64.outputdata = compat_ptr(mex32.outputdata);
  700. mex64.outputdatalength = mex32.outputdatalength;
  701. mex64.b_key = compat_ptr(mex32.b_key);
  702. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  703. do {
  704. rc = zcrypt_rsa_modexpo(&mex64);
  705. } while (rc == -EAGAIN);
  706. if (!rc)
  707. rc = put_user(mex64.outputdatalength,
  708. &umex32->outputdatalength);
  709. return rc;
  710. }
  711. struct compat_ica_rsa_modexpo_crt {
  712. compat_uptr_t inputdata;
  713. unsigned int inputdatalength;
  714. compat_uptr_t outputdata;
  715. unsigned int outputdatalength;
  716. compat_uptr_t bp_key;
  717. compat_uptr_t bq_key;
  718. compat_uptr_t np_prime;
  719. compat_uptr_t nq_prime;
  720. compat_uptr_t u_mult_inv;
  721. };
  722. static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
  723. unsigned long arg)
  724. {
  725. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  726. struct compat_ica_rsa_modexpo_crt crt32;
  727. struct ica_rsa_modexpo_crt crt64;
  728. long rc;
  729. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  730. return -EFAULT;
  731. crt64.inputdata = compat_ptr(crt32.inputdata);
  732. crt64.inputdatalength = crt32.inputdatalength;
  733. crt64.outputdata= compat_ptr(crt32.outputdata);
  734. crt64.outputdatalength = crt32.outputdatalength;
  735. crt64.bp_key = compat_ptr(crt32.bp_key);
  736. crt64.bq_key = compat_ptr(crt32.bq_key);
  737. crt64.np_prime = compat_ptr(crt32.np_prime);
  738. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  739. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  740. do {
  741. rc = zcrypt_rsa_crt(&crt64);
  742. } while (rc == -EAGAIN);
  743. if (!rc)
  744. rc = put_user(crt64.outputdatalength,
  745. &ucrt32->outputdatalength);
  746. return rc;
  747. }
  748. struct compat_ica_xcRB {
  749. unsigned short agent_ID;
  750. unsigned int user_defined;
  751. unsigned short request_ID;
  752. unsigned int request_control_blk_length;
  753. unsigned char padding1[16 - sizeof (compat_uptr_t)];
  754. compat_uptr_t request_control_blk_addr;
  755. unsigned int request_data_length;
  756. char padding2[16 - sizeof (compat_uptr_t)];
  757. compat_uptr_t request_data_address;
  758. unsigned int reply_control_blk_length;
  759. char padding3[16 - sizeof (compat_uptr_t)];
  760. compat_uptr_t reply_control_blk_addr;
  761. unsigned int reply_data_length;
  762. char padding4[16 - sizeof (compat_uptr_t)];
  763. compat_uptr_t reply_data_addr;
  764. unsigned short priority_window;
  765. unsigned int status;
  766. } __attribute__((packed));
  767. static long trans_xcRB32(struct file *filp, unsigned int cmd,
  768. unsigned long arg)
  769. {
  770. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  771. struct compat_ica_xcRB xcRB32;
  772. struct ica_xcRB xcRB64;
  773. long rc;
  774. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  775. return -EFAULT;
  776. xcRB64.agent_ID = xcRB32.agent_ID;
  777. xcRB64.user_defined = xcRB32.user_defined;
  778. xcRB64.request_ID = xcRB32.request_ID;
  779. xcRB64.request_control_blk_length =
  780. xcRB32.request_control_blk_length;
  781. xcRB64.request_control_blk_addr =
  782. compat_ptr(xcRB32.request_control_blk_addr);
  783. xcRB64.request_data_length =
  784. xcRB32.request_data_length;
  785. xcRB64.request_data_address =
  786. compat_ptr(xcRB32.request_data_address);
  787. xcRB64.reply_control_blk_length =
  788. xcRB32.reply_control_blk_length;
  789. xcRB64.reply_control_blk_addr =
  790. compat_ptr(xcRB32.reply_control_blk_addr);
  791. xcRB64.reply_data_length = xcRB32.reply_data_length;
  792. xcRB64.reply_data_addr =
  793. compat_ptr(xcRB32.reply_data_addr);
  794. xcRB64.priority_window = xcRB32.priority_window;
  795. xcRB64.status = xcRB32.status;
  796. do {
  797. rc = zcrypt_send_cprb(&xcRB64);
  798. } while (rc == -EAGAIN);
  799. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  800. xcRB32.reply_data_length = xcRB64.reply_data_length;
  801. xcRB32.status = xcRB64.status;
  802. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  803. return -EFAULT;
  804. return rc;
  805. }
  806. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  807. unsigned long arg)
  808. {
  809. if (cmd == ICARSAMODEXPO)
  810. return trans_modexpo32(filp, cmd, arg);
  811. if (cmd == ICARSACRT)
  812. return trans_modexpo_crt32(filp, cmd, arg);
  813. if (cmd == ZSECSENDCPRB)
  814. return trans_xcRB32(filp, cmd, arg);
  815. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  816. }
  817. #endif
  818. /*
  819. * Misc device file operations.
  820. */
  821. static const struct file_operations zcrypt_fops = {
  822. .owner = THIS_MODULE,
  823. .read = zcrypt_read,
  824. .write = zcrypt_write,
  825. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  826. #ifdef CONFIG_COMPAT
  827. .compat_ioctl = zcrypt_compat_ioctl,
  828. #endif
  829. .open = zcrypt_open,
  830. .release = zcrypt_release,
  831. .llseek = no_llseek,
  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