zcrypt_api.c 33 KB

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