zcrypt_api.c 32 KB

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