zcrypt_api.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /*
  2. * linux/drivers/s390/crypto/zcrypt_api.c
  3. *
  4. * zcrypt 2.1.0
  5. *
  6. * Copyright (C) 2001, 2006 IBM Corporation
  7. * Author(s): Robert Burroughs
  8. * Eric Rossman (edrossma@us.ibm.com)
  9. * Cornelia Huck <cornelia.huck@de.ibm.com>
  10. *
  11. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  12. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  13. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2, or (at your option)
  18. * any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/fs.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/compat.h>
  37. #include <linux/smp_lock.h>
  38. #include <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 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. 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. };
  832. /*
  833. * Misc device.
  834. */
  835. static struct miscdevice zcrypt_misc_device = {
  836. .minor = MISC_DYNAMIC_MINOR,
  837. .name = "z90crypt",
  838. .fops = &zcrypt_fops,
  839. };
  840. /*
  841. * Deprecated /proc entry support.
  842. */
  843. static struct proc_dir_entry *zcrypt_entry;
  844. static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
  845. {
  846. int i;
  847. for (i = 0; i < len; i++)
  848. seq_printf(m, "%01x", (unsigned int) addr[i]);
  849. seq_putc(m, ' ');
  850. }
  851. static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
  852. {
  853. int inl, c, cx;
  854. seq_printf(m, " ");
  855. inl = 0;
  856. for (c = 0; c < (len / 16); c++) {
  857. sprintcl(m, addr+inl, 16);
  858. inl += 16;
  859. }
  860. cx = len%16;
  861. if (cx) {
  862. sprintcl(m, addr+inl, cx);
  863. inl += cx;
  864. }
  865. seq_putc(m, '\n');
  866. }
  867. static void sprinthx(unsigned char *title, struct seq_file *m,
  868. unsigned char *addr, unsigned int len)
  869. {
  870. int inl, r, rx;
  871. seq_printf(m, "\n%s\n", title);
  872. inl = 0;
  873. for (r = 0; r < (len / 64); r++) {
  874. sprintrw(m, addr+inl, 64);
  875. inl += 64;
  876. }
  877. rx = len % 64;
  878. if (rx) {
  879. sprintrw(m, addr+inl, rx);
  880. inl += rx;
  881. }
  882. seq_putc(m, '\n');
  883. }
  884. static void sprinthx4(unsigned char *title, struct seq_file *m,
  885. unsigned int *array, unsigned int len)
  886. {
  887. int r;
  888. seq_printf(m, "\n%s\n", title);
  889. for (r = 0; r < len; r++) {
  890. if ((r % 8) == 0)
  891. seq_printf(m, " ");
  892. seq_printf(m, "%08X ", array[r]);
  893. if ((r % 8) == 7)
  894. seq_putc(m, '\n');
  895. }
  896. seq_putc(m, '\n');
  897. }
  898. static int zcrypt_proc_show(struct seq_file *m, void *v)
  899. {
  900. char workarea[sizeof(int) * AP_DEVICES];
  901. seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
  902. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  903. seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
  904. seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
  905. seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
  906. seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
  907. seq_printf(m, "PCIXCC MCL2 count: %d\n",
  908. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  909. seq_printf(m, "PCIXCC MCL3 count: %d\n",
  910. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  911. seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
  912. seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
  913. seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
  914. seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
  915. seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
  916. seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
  917. seq_printf(m, "Total open handles: %d\n\n",
  918. atomic_read(&zcrypt_open_count));
  919. zcrypt_status_mask(workarea);
  920. sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  921. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
  922. m, workarea, AP_DEVICES);
  923. zcrypt_qdepth_mask(workarea);
  924. sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
  925. zcrypt_perdev_reqcnt((int *) workarea);
  926. sprinthx4("Per-device successfully completed request counts",
  927. m, (unsigned int *) workarea, AP_DEVICES);
  928. return 0;
  929. }
  930. static int zcrypt_proc_open(struct inode *inode, struct file *file)
  931. {
  932. return single_open(file, zcrypt_proc_show, NULL);
  933. }
  934. static void zcrypt_disable_card(int index)
  935. {
  936. struct zcrypt_device *zdev;
  937. spin_lock_bh(&zcrypt_device_lock);
  938. list_for_each_entry(zdev, &zcrypt_device_list, list)
  939. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  940. zdev->online = 0;
  941. ap_flush_queue(zdev->ap_dev);
  942. break;
  943. }
  944. spin_unlock_bh(&zcrypt_device_lock);
  945. }
  946. static void zcrypt_enable_card(int index)
  947. {
  948. struct zcrypt_device *zdev;
  949. spin_lock_bh(&zcrypt_device_lock);
  950. list_for_each_entry(zdev, &zcrypt_device_list, list)
  951. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  952. zdev->online = 1;
  953. break;
  954. }
  955. spin_unlock_bh(&zcrypt_device_lock);
  956. }
  957. static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
  958. size_t count, loff_t *pos)
  959. {
  960. unsigned char *lbuf, *ptr;
  961. size_t local_count;
  962. int j;
  963. if (count <= 0)
  964. return 0;
  965. #define LBUFSIZE 1200UL
  966. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  967. if (!lbuf)
  968. return 0;
  969. local_count = min(LBUFSIZE - 1, count);
  970. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  971. kfree(lbuf);
  972. return -EFAULT;
  973. }
  974. lbuf[local_count] = '\0';
  975. ptr = strstr(lbuf, "Online devices");
  976. if (!ptr)
  977. goto out;
  978. ptr = strstr(ptr, "\n");
  979. if (!ptr)
  980. goto out;
  981. ptr++;
  982. if (strstr(ptr, "Waiting work element counts") == NULL)
  983. goto out;
  984. for (j = 0; j < 64 && *ptr; ptr++) {
  985. /*
  986. * '0' for no device, '1' for PCICA, '2' for PCICC,
  987. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  988. * '5' for CEX2C and '6' for CEX2A'
  989. * '7' for CEX3C and '8' for CEX3A
  990. */
  991. if (*ptr >= '0' && *ptr <= '8')
  992. j++;
  993. else if (*ptr == 'd' || *ptr == 'D')
  994. zcrypt_disable_card(j++);
  995. else if (*ptr == 'e' || *ptr == 'E')
  996. zcrypt_enable_card(j++);
  997. else if (*ptr != ' ' && *ptr != '\t')
  998. break;
  999. }
  1000. out:
  1001. kfree(lbuf);
  1002. return count;
  1003. }
  1004. static const struct file_operations zcrypt_proc_fops = {
  1005. .owner = THIS_MODULE,
  1006. .open = zcrypt_proc_open,
  1007. .read = seq_read,
  1008. .llseek = seq_lseek,
  1009. .release = single_release,
  1010. .write = zcrypt_proc_write,
  1011. };
  1012. static int zcrypt_rng_device_count;
  1013. static u32 *zcrypt_rng_buffer;
  1014. static int zcrypt_rng_buffer_index;
  1015. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1016. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1017. {
  1018. int rc;
  1019. /*
  1020. * We don't need locking here because the RNG API guarantees serialized
  1021. * read method calls.
  1022. */
  1023. if (zcrypt_rng_buffer_index == 0) {
  1024. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1025. if (rc < 0)
  1026. return -EIO;
  1027. zcrypt_rng_buffer_index = rc / sizeof *data;
  1028. }
  1029. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1030. return sizeof *data;
  1031. }
  1032. static struct hwrng zcrypt_rng_dev = {
  1033. .name = "zcrypt",
  1034. .data_read = zcrypt_rng_data_read,
  1035. };
  1036. static int zcrypt_rng_device_add(void)
  1037. {
  1038. int rc = 0;
  1039. mutex_lock(&zcrypt_rng_mutex);
  1040. if (zcrypt_rng_device_count == 0) {
  1041. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1042. if (!zcrypt_rng_buffer) {
  1043. rc = -ENOMEM;
  1044. goto out;
  1045. }
  1046. zcrypt_rng_buffer_index = 0;
  1047. rc = hwrng_register(&zcrypt_rng_dev);
  1048. if (rc)
  1049. goto out_free;
  1050. zcrypt_rng_device_count = 1;
  1051. } else
  1052. zcrypt_rng_device_count++;
  1053. mutex_unlock(&zcrypt_rng_mutex);
  1054. return 0;
  1055. out_free:
  1056. free_page((unsigned long) zcrypt_rng_buffer);
  1057. out:
  1058. mutex_unlock(&zcrypt_rng_mutex);
  1059. return rc;
  1060. }
  1061. static void zcrypt_rng_device_remove(void)
  1062. {
  1063. mutex_lock(&zcrypt_rng_mutex);
  1064. zcrypt_rng_device_count--;
  1065. if (zcrypt_rng_device_count == 0) {
  1066. hwrng_unregister(&zcrypt_rng_dev);
  1067. free_page((unsigned long) zcrypt_rng_buffer);
  1068. }
  1069. mutex_unlock(&zcrypt_rng_mutex);
  1070. }
  1071. /**
  1072. * zcrypt_api_init(): Module initialization.
  1073. *
  1074. * The module initialization code.
  1075. */
  1076. int __init zcrypt_api_init(void)
  1077. {
  1078. int rc;
  1079. /* Register the request sprayer. */
  1080. rc = misc_register(&zcrypt_misc_device);
  1081. if (rc < 0)
  1082. goto out;
  1083. /* Set up the proc file system */
  1084. zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
  1085. if (!zcrypt_entry) {
  1086. rc = -ENOMEM;
  1087. goto out_misc;
  1088. }
  1089. return 0;
  1090. out_misc:
  1091. misc_deregister(&zcrypt_misc_device);
  1092. out:
  1093. return rc;
  1094. }
  1095. /**
  1096. * zcrypt_api_exit(): Module termination.
  1097. *
  1098. * The module termination code.
  1099. */
  1100. void zcrypt_api_exit(void)
  1101. {
  1102. remove_proc_entry("driver/z90crypt", NULL);
  1103. misc_deregister(&zcrypt_misc_device);
  1104. }
  1105. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  1106. module_init(zcrypt_api_init);
  1107. module_exit(zcrypt_api_exit);
  1108. #endif