zcrypt_api.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. atomic_inc(&zcrypt_open_count);
  272. return 0;
  273. }
  274. /**
  275. * zcrypt_release(): Count number of users.
  276. *
  277. * Device close function to count number of users.
  278. */
  279. static int zcrypt_release(struct inode *inode, struct file *filp)
  280. {
  281. atomic_dec(&zcrypt_open_count);
  282. return 0;
  283. }
  284. /*
  285. * zcrypt ioctls.
  286. */
  287. static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
  288. {
  289. struct zcrypt_device *zdev;
  290. int rc;
  291. if (mex->outputdatalength < mex->inputdatalength)
  292. return -EINVAL;
  293. /*
  294. * As long as outputdatalength is big enough, we can set the
  295. * outputdatalength equal to the inputdatalength, since that is the
  296. * number of bytes we will copy in any case
  297. */
  298. mex->outputdatalength = mex->inputdatalength;
  299. spin_lock_bh(&zcrypt_device_lock);
  300. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  301. if (!zdev->online ||
  302. !zdev->ops->rsa_modexpo ||
  303. zdev->min_mod_size > mex->inputdatalength ||
  304. zdev->max_mod_size < mex->inputdatalength)
  305. continue;
  306. zcrypt_device_get(zdev);
  307. get_device(&zdev->ap_dev->device);
  308. zdev->request_count++;
  309. __zcrypt_decrease_preference(zdev);
  310. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  311. spin_unlock_bh(&zcrypt_device_lock);
  312. rc = zdev->ops->rsa_modexpo(zdev, mex);
  313. spin_lock_bh(&zcrypt_device_lock);
  314. module_put(zdev->ap_dev->drv->driver.owner);
  315. }
  316. else
  317. rc = -EAGAIN;
  318. zdev->request_count--;
  319. __zcrypt_increase_preference(zdev);
  320. put_device(&zdev->ap_dev->device);
  321. zcrypt_device_put(zdev);
  322. spin_unlock_bh(&zcrypt_device_lock);
  323. return rc;
  324. }
  325. spin_unlock_bh(&zcrypt_device_lock);
  326. return -ENODEV;
  327. }
  328. static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
  329. {
  330. struct zcrypt_device *zdev;
  331. unsigned long long z1, z2, z3;
  332. int rc, copied;
  333. if (crt->outputdatalength < crt->inputdatalength ||
  334. (crt->inputdatalength & 1))
  335. return -EINVAL;
  336. /*
  337. * As long as outputdatalength is big enough, we can set the
  338. * outputdatalength equal to the inputdatalength, since that is the
  339. * number of bytes we will copy in any case
  340. */
  341. crt->outputdatalength = crt->inputdatalength;
  342. copied = 0;
  343. restart:
  344. spin_lock_bh(&zcrypt_device_lock);
  345. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  346. if (!zdev->online ||
  347. !zdev->ops->rsa_modexpo_crt ||
  348. zdev->min_mod_size > crt->inputdatalength ||
  349. zdev->max_mod_size < crt->inputdatalength)
  350. continue;
  351. if (zdev->short_crt && crt->inputdatalength > 240) {
  352. /*
  353. * Check inputdata for leading zeros for cards
  354. * that can't handle np_prime, bp_key, or
  355. * u_mult_inv > 128 bytes.
  356. */
  357. if (copied == 0) {
  358. int len;
  359. spin_unlock_bh(&zcrypt_device_lock);
  360. /* len is max 256 / 2 - 120 = 8 */
  361. len = crt->inputdatalength / 2 - 120;
  362. z1 = z2 = z3 = 0;
  363. if (copy_from_user(&z1, crt->np_prime, len) ||
  364. copy_from_user(&z2, crt->bp_key, len) ||
  365. copy_from_user(&z3, crt->u_mult_inv, len))
  366. return -EFAULT;
  367. copied = 1;
  368. /*
  369. * We have to restart device lookup -
  370. * the device list may have changed by now.
  371. */
  372. goto restart;
  373. }
  374. if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
  375. /* The device can't handle this request. */
  376. continue;
  377. }
  378. zcrypt_device_get(zdev);
  379. get_device(&zdev->ap_dev->device);
  380. zdev->request_count++;
  381. __zcrypt_decrease_preference(zdev);
  382. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  383. spin_unlock_bh(&zcrypt_device_lock);
  384. rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
  385. spin_lock_bh(&zcrypt_device_lock);
  386. module_put(zdev->ap_dev->drv->driver.owner);
  387. }
  388. else
  389. rc = -EAGAIN;
  390. zdev->request_count--;
  391. __zcrypt_increase_preference(zdev);
  392. put_device(&zdev->ap_dev->device);
  393. zcrypt_device_put(zdev);
  394. spin_unlock_bh(&zcrypt_device_lock);
  395. return rc;
  396. }
  397. spin_unlock_bh(&zcrypt_device_lock);
  398. return -ENODEV;
  399. }
  400. static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  401. {
  402. struct zcrypt_device *zdev;
  403. int rc;
  404. spin_lock_bh(&zcrypt_device_lock);
  405. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  406. if (!zdev->online || !zdev->ops->send_cprb ||
  407. (xcRB->user_defined != AUTOSELECT &&
  408. AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined)
  409. )
  410. continue;
  411. zcrypt_device_get(zdev);
  412. get_device(&zdev->ap_dev->device);
  413. zdev->request_count++;
  414. __zcrypt_decrease_preference(zdev);
  415. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  416. spin_unlock_bh(&zcrypt_device_lock);
  417. rc = zdev->ops->send_cprb(zdev, xcRB);
  418. spin_lock_bh(&zcrypt_device_lock);
  419. module_put(zdev->ap_dev->drv->driver.owner);
  420. }
  421. else
  422. rc = -EAGAIN;
  423. zdev->request_count--;
  424. __zcrypt_increase_preference(zdev);
  425. put_device(&zdev->ap_dev->device);
  426. zcrypt_device_put(zdev);
  427. spin_unlock_bh(&zcrypt_device_lock);
  428. return rc;
  429. }
  430. spin_unlock_bh(&zcrypt_device_lock);
  431. return -ENODEV;
  432. }
  433. static long zcrypt_rng(char *buffer)
  434. {
  435. struct zcrypt_device *zdev;
  436. int rc;
  437. spin_lock_bh(&zcrypt_device_lock);
  438. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  439. if (!zdev->online || !zdev->ops->rng)
  440. continue;
  441. zcrypt_device_get(zdev);
  442. get_device(&zdev->ap_dev->device);
  443. zdev->request_count++;
  444. __zcrypt_decrease_preference(zdev);
  445. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  446. spin_unlock_bh(&zcrypt_device_lock);
  447. rc = zdev->ops->rng(zdev, buffer);
  448. spin_lock_bh(&zcrypt_device_lock);
  449. module_put(zdev->ap_dev->drv->driver.owner);
  450. } else
  451. rc = -EAGAIN;
  452. zdev->request_count--;
  453. __zcrypt_increase_preference(zdev);
  454. put_device(&zdev->ap_dev->device);
  455. zcrypt_device_put(zdev);
  456. spin_unlock_bh(&zcrypt_device_lock);
  457. return rc;
  458. }
  459. spin_unlock_bh(&zcrypt_device_lock);
  460. return -ENODEV;
  461. }
  462. static void zcrypt_status_mask(char status[AP_DEVICES])
  463. {
  464. struct zcrypt_device *zdev;
  465. memset(status, 0, sizeof(char) * AP_DEVICES);
  466. spin_lock_bh(&zcrypt_device_lock);
  467. list_for_each_entry(zdev, &zcrypt_device_list, list)
  468. status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  469. zdev->online ? zdev->user_space_type : 0x0d;
  470. spin_unlock_bh(&zcrypt_device_lock);
  471. }
  472. static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
  473. {
  474. struct zcrypt_device *zdev;
  475. memset(qdepth, 0, sizeof(char) * AP_DEVICES);
  476. spin_lock_bh(&zcrypt_device_lock);
  477. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  478. spin_lock(&zdev->ap_dev->lock);
  479. qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  480. zdev->ap_dev->pendingq_count +
  481. zdev->ap_dev->requestq_count;
  482. spin_unlock(&zdev->ap_dev->lock);
  483. }
  484. spin_unlock_bh(&zcrypt_device_lock);
  485. }
  486. static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
  487. {
  488. struct zcrypt_device *zdev;
  489. memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
  490. spin_lock_bh(&zcrypt_device_lock);
  491. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  492. spin_lock(&zdev->ap_dev->lock);
  493. reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  494. zdev->ap_dev->total_request_count;
  495. spin_unlock(&zdev->ap_dev->lock);
  496. }
  497. spin_unlock_bh(&zcrypt_device_lock);
  498. }
  499. static int zcrypt_pendingq_count(void)
  500. {
  501. struct zcrypt_device *zdev;
  502. int pendingq_count = 0;
  503. spin_lock_bh(&zcrypt_device_lock);
  504. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  505. spin_lock(&zdev->ap_dev->lock);
  506. pendingq_count += zdev->ap_dev->pendingq_count;
  507. spin_unlock(&zdev->ap_dev->lock);
  508. }
  509. spin_unlock_bh(&zcrypt_device_lock);
  510. return pendingq_count;
  511. }
  512. static int zcrypt_requestq_count(void)
  513. {
  514. struct zcrypt_device *zdev;
  515. int requestq_count = 0;
  516. spin_lock_bh(&zcrypt_device_lock);
  517. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  518. spin_lock(&zdev->ap_dev->lock);
  519. requestq_count += zdev->ap_dev->requestq_count;
  520. spin_unlock(&zdev->ap_dev->lock);
  521. }
  522. spin_unlock_bh(&zcrypt_device_lock);
  523. return requestq_count;
  524. }
  525. static int zcrypt_count_type(int type)
  526. {
  527. struct zcrypt_device *zdev;
  528. int device_count = 0;
  529. spin_lock_bh(&zcrypt_device_lock);
  530. list_for_each_entry(zdev, &zcrypt_device_list, list)
  531. if (zdev->user_space_type == type)
  532. device_count++;
  533. spin_unlock_bh(&zcrypt_device_lock);
  534. return device_count;
  535. }
  536. /**
  537. * zcrypt_ica_status(): Old, depracted combi status call.
  538. *
  539. * Old, deprecated combi status call.
  540. */
  541. static long zcrypt_ica_status(struct file *filp, unsigned long arg)
  542. {
  543. struct ica_z90_status *pstat;
  544. int ret;
  545. pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
  546. if (!pstat)
  547. return -ENOMEM;
  548. pstat->totalcount = zcrypt_device_count;
  549. pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
  550. pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
  551. pstat->requestqWaitCount = zcrypt_requestq_count();
  552. pstat->pendingqWaitCount = zcrypt_pendingq_count();
  553. pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
  554. pstat->cryptoDomain = ap_domain_index;
  555. zcrypt_status_mask(pstat->status);
  556. zcrypt_qdepth_mask(pstat->qdepth);
  557. ret = 0;
  558. if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
  559. ret = -EFAULT;
  560. kfree(pstat);
  561. return ret;
  562. }
  563. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  564. unsigned long arg)
  565. {
  566. int rc;
  567. switch (cmd) {
  568. case ICARSAMODEXPO: {
  569. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  570. struct ica_rsa_modexpo mex;
  571. if (copy_from_user(&mex, umex, sizeof(mex)))
  572. return -EFAULT;
  573. do {
  574. rc = zcrypt_rsa_modexpo(&mex);
  575. } while (rc == -EAGAIN);
  576. if (rc)
  577. return rc;
  578. return put_user(mex.outputdatalength, &umex->outputdatalength);
  579. }
  580. case ICARSACRT: {
  581. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  582. struct ica_rsa_modexpo_crt crt;
  583. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  584. return -EFAULT;
  585. do {
  586. rc = zcrypt_rsa_crt(&crt);
  587. } while (rc == -EAGAIN);
  588. if (rc)
  589. return rc;
  590. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  591. }
  592. case ZSECSENDCPRB: {
  593. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  594. struct ica_xcRB xcRB;
  595. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  596. return -EFAULT;
  597. do {
  598. rc = zcrypt_send_cprb(&xcRB);
  599. } while (rc == -EAGAIN);
  600. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  601. return -EFAULT;
  602. return rc;
  603. }
  604. case Z90STAT_STATUS_MASK: {
  605. char status[AP_DEVICES];
  606. zcrypt_status_mask(status);
  607. if (copy_to_user((char __user *) arg, status,
  608. sizeof(char) * AP_DEVICES))
  609. return -EFAULT;
  610. return 0;
  611. }
  612. case Z90STAT_QDEPTH_MASK: {
  613. char qdepth[AP_DEVICES];
  614. zcrypt_qdepth_mask(qdepth);
  615. if (copy_to_user((char __user *) arg, qdepth,
  616. sizeof(char) * AP_DEVICES))
  617. return -EFAULT;
  618. return 0;
  619. }
  620. case Z90STAT_PERDEV_REQCNT: {
  621. int reqcnt[AP_DEVICES];
  622. zcrypt_perdev_reqcnt(reqcnt);
  623. if (copy_to_user((int __user *) arg, reqcnt,
  624. sizeof(int) * AP_DEVICES))
  625. return -EFAULT;
  626. return 0;
  627. }
  628. case Z90STAT_REQUESTQ_COUNT:
  629. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  630. case Z90STAT_PENDINGQ_COUNT:
  631. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  632. case Z90STAT_TOTALOPEN_COUNT:
  633. return put_user(atomic_read(&zcrypt_open_count),
  634. (int __user *) arg);
  635. case Z90STAT_DOMAIN_INDEX:
  636. return put_user(ap_domain_index, (int __user *) arg);
  637. /*
  638. * Deprecated ioctls. Don't add another device count ioctl,
  639. * you can count them yourself in the user space with the
  640. * output of the Z90STAT_STATUS_MASK ioctl.
  641. */
  642. case ICAZ90STATUS:
  643. return zcrypt_ica_status(filp, arg);
  644. case Z90STAT_TOTALCOUNT:
  645. return put_user(zcrypt_device_count, (int __user *) arg);
  646. case Z90STAT_PCICACOUNT:
  647. return put_user(zcrypt_count_type(ZCRYPT_PCICA),
  648. (int __user *) arg);
  649. case Z90STAT_PCICCCOUNT:
  650. return put_user(zcrypt_count_type(ZCRYPT_PCICC),
  651. (int __user *) arg);
  652. case Z90STAT_PCIXCCMCL2COUNT:
  653. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
  654. (int __user *) arg);
  655. case Z90STAT_PCIXCCMCL3COUNT:
  656. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  657. (int __user *) arg);
  658. case Z90STAT_PCIXCCCOUNT:
  659. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
  660. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  661. (int __user *) arg);
  662. case Z90STAT_CEX2CCOUNT:
  663. return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
  664. (int __user *) arg);
  665. case Z90STAT_CEX2ACOUNT:
  666. return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
  667. (int __user *) arg);
  668. default:
  669. /* unknown ioctl number */
  670. return -ENOIOCTLCMD;
  671. }
  672. }
  673. #ifdef CONFIG_COMPAT
  674. /*
  675. * ioctl32 conversion routines
  676. */
  677. struct compat_ica_rsa_modexpo {
  678. compat_uptr_t inputdata;
  679. unsigned int inputdatalength;
  680. compat_uptr_t outputdata;
  681. unsigned int outputdatalength;
  682. compat_uptr_t b_key;
  683. compat_uptr_t n_modulus;
  684. };
  685. static long trans_modexpo32(struct file *filp, unsigned int cmd,
  686. unsigned long arg)
  687. {
  688. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  689. struct compat_ica_rsa_modexpo mex32;
  690. struct ica_rsa_modexpo mex64;
  691. long rc;
  692. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  693. return -EFAULT;
  694. mex64.inputdata = compat_ptr(mex32.inputdata);
  695. mex64.inputdatalength = mex32.inputdatalength;
  696. mex64.outputdata = compat_ptr(mex32.outputdata);
  697. mex64.outputdatalength = mex32.outputdatalength;
  698. mex64.b_key = compat_ptr(mex32.b_key);
  699. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  700. do {
  701. rc = zcrypt_rsa_modexpo(&mex64);
  702. } while (rc == -EAGAIN);
  703. if (!rc)
  704. rc = put_user(mex64.outputdatalength,
  705. &umex32->outputdatalength);
  706. return rc;
  707. }
  708. struct compat_ica_rsa_modexpo_crt {
  709. compat_uptr_t inputdata;
  710. unsigned int inputdatalength;
  711. compat_uptr_t outputdata;
  712. unsigned int outputdatalength;
  713. compat_uptr_t bp_key;
  714. compat_uptr_t bq_key;
  715. compat_uptr_t np_prime;
  716. compat_uptr_t nq_prime;
  717. compat_uptr_t u_mult_inv;
  718. };
  719. static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
  720. unsigned long arg)
  721. {
  722. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  723. struct compat_ica_rsa_modexpo_crt crt32;
  724. struct ica_rsa_modexpo_crt crt64;
  725. long rc;
  726. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  727. return -EFAULT;
  728. crt64.inputdata = compat_ptr(crt32.inputdata);
  729. crt64.inputdatalength = crt32.inputdatalength;
  730. crt64.outputdata= compat_ptr(crt32.outputdata);
  731. crt64.outputdatalength = crt32.outputdatalength;
  732. crt64.bp_key = compat_ptr(crt32.bp_key);
  733. crt64.bq_key = compat_ptr(crt32.bq_key);
  734. crt64.np_prime = compat_ptr(crt32.np_prime);
  735. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  736. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  737. do {
  738. rc = zcrypt_rsa_crt(&crt64);
  739. } while (rc == -EAGAIN);
  740. if (!rc)
  741. rc = put_user(crt64.outputdatalength,
  742. &ucrt32->outputdatalength);
  743. return rc;
  744. }
  745. struct compat_ica_xcRB {
  746. unsigned short agent_ID;
  747. unsigned int user_defined;
  748. unsigned short request_ID;
  749. unsigned int request_control_blk_length;
  750. unsigned char padding1[16 - sizeof (compat_uptr_t)];
  751. compat_uptr_t request_control_blk_addr;
  752. unsigned int request_data_length;
  753. char padding2[16 - sizeof (compat_uptr_t)];
  754. compat_uptr_t request_data_address;
  755. unsigned int reply_control_blk_length;
  756. char padding3[16 - sizeof (compat_uptr_t)];
  757. compat_uptr_t reply_control_blk_addr;
  758. unsigned int reply_data_length;
  759. char padding4[16 - sizeof (compat_uptr_t)];
  760. compat_uptr_t reply_data_addr;
  761. unsigned short priority_window;
  762. unsigned int status;
  763. } __attribute__((packed));
  764. static long trans_xcRB32(struct file *filp, unsigned int cmd,
  765. unsigned long arg)
  766. {
  767. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  768. struct compat_ica_xcRB xcRB32;
  769. struct ica_xcRB xcRB64;
  770. long rc;
  771. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  772. return -EFAULT;
  773. xcRB64.agent_ID = xcRB32.agent_ID;
  774. xcRB64.user_defined = xcRB32.user_defined;
  775. xcRB64.request_ID = xcRB32.request_ID;
  776. xcRB64.request_control_blk_length =
  777. xcRB32.request_control_blk_length;
  778. xcRB64.request_control_blk_addr =
  779. compat_ptr(xcRB32.request_control_blk_addr);
  780. xcRB64.request_data_length =
  781. xcRB32.request_data_length;
  782. xcRB64.request_data_address =
  783. compat_ptr(xcRB32.request_data_address);
  784. xcRB64.reply_control_blk_length =
  785. xcRB32.reply_control_blk_length;
  786. xcRB64.reply_control_blk_addr =
  787. compat_ptr(xcRB32.reply_control_blk_addr);
  788. xcRB64.reply_data_length = xcRB32.reply_data_length;
  789. xcRB64.reply_data_addr =
  790. compat_ptr(xcRB32.reply_data_addr);
  791. xcRB64.priority_window = xcRB32.priority_window;
  792. xcRB64.status = xcRB32.status;
  793. do {
  794. rc = zcrypt_send_cprb(&xcRB64);
  795. } while (rc == -EAGAIN);
  796. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  797. xcRB32.reply_data_length = xcRB64.reply_data_length;
  798. xcRB32.status = xcRB64.status;
  799. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  800. return -EFAULT;
  801. return rc;
  802. }
  803. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  804. unsigned long arg)
  805. {
  806. if (cmd == ICARSAMODEXPO)
  807. return trans_modexpo32(filp, cmd, arg);
  808. if (cmd == ICARSACRT)
  809. return trans_modexpo_crt32(filp, cmd, arg);
  810. if (cmd == ZSECSENDCPRB)
  811. return trans_xcRB32(filp, cmd, arg);
  812. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  813. }
  814. #endif
  815. /*
  816. * Misc device file operations.
  817. */
  818. static const struct file_operations zcrypt_fops = {
  819. .owner = THIS_MODULE,
  820. .read = zcrypt_read,
  821. .write = zcrypt_write,
  822. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  823. #ifdef CONFIG_COMPAT
  824. .compat_ioctl = zcrypt_compat_ioctl,
  825. #endif
  826. .open = zcrypt_open,
  827. .release = zcrypt_release
  828. };
  829. /*
  830. * Misc device.
  831. */
  832. static struct miscdevice zcrypt_misc_device = {
  833. .minor = MISC_DYNAMIC_MINOR,
  834. .name = "z90crypt",
  835. .fops = &zcrypt_fops,
  836. };
  837. /*
  838. * Deprecated /proc entry support.
  839. */
  840. static struct proc_dir_entry *zcrypt_entry;
  841. static int sprintcl(unsigned char *outaddr, unsigned char *addr,
  842. unsigned int len)
  843. {
  844. int hl, i;
  845. hl = 0;
  846. for (i = 0; i < len; i++)
  847. hl += sprintf(outaddr+hl, "%01x", (unsigned int) addr[i]);
  848. hl += sprintf(outaddr+hl, " ");
  849. return hl;
  850. }
  851. static int sprintrw(unsigned char *outaddr, unsigned char *addr,
  852. unsigned int len)
  853. {
  854. int hl, inl, c, cx;
  855. hl = sprintf(outaddr, " ");
  856. inl = 0;
  857. for (c = 0; c < (len / 16); c++) {
  858. hl += sprintcl(outaddr+hl, addr+inl, 16);
  859. inl += 16;
  860. }
  861. cx = len%16;
  862. if (cx) {
  863. hl += sprintcl(outaddr+hl, addr+inl, cx);
  864. inl += cx;
  865. }
  866. hl += sprintf(outaddr+hl, "\n");
  867. return hl;
  868. }
  869. static int sprinthx(unsigned char *title, unsigned char *outaddr,
  870. unsigned char *addr, unsigned int len)
  871. {
  872. int hl, inl, r, rx;
  873. hl = sprintf(outaddr, "\n%s\n", title);
  874. inl = 0;
  875. for (r = 0; r < (len / 64); r++) {
  876. hl += sprintrw(outaddr+hl, addr+inl, 64);
  877. inl += 64;
  878. }
  879. rx = len % 64;
  880. if (rx) {
  881. hl += sprintrw(outaddr+hl, addr+inl, rx);
  882. inl += rx;
  883. }
  884. hl += sprintf(outaddr+hl, "\n");
  885. return hl;
  886. }
  887. static int sprinthx4(unsigned char *title, unsigned char *outaddr,
  888. unsigned int *array, unsigned int len)
  889. {
  890. int hl, r;
  891. hl = sprintf(outaddr, "\n%s\n", title);
  892. for (r = 0; r < len; r++) {
  893. if ((r % 8) == 0)
  894. hl += sprintf(outaddr+hl, " ");
  895. hl += sprintf(outaddr+hl, "%08X ", array[r]);
  896. if ((r % 8) == 7)
  897. hl += sprintf(outaddr+hl, "\n");
  898. }
  899. hl += sprintf(outaddr+hl, "\n");
  900. return hl;
  901. }
  902. static int zcrypt_status_read(char *resp_buff, char **start, off_t offset,
  903. int count, int *eof, void *data)
  904. {
  905. unsigned char *workarea;
  906. int len;
  907. len = 0;
  908. /* resp_buff is a page. Use the right half for a work area */
  909. workarea = resp_buff + 2000;
  910. len += sprintf(resp_buff + len, "\nzcrypt version: %d.%d.%d\n",
  911. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  912. len += sprintf(resp_buff + len, "Cryptographic domain: %d\n",
  913. ap_domain_index);
  914. len += sprintf(resp_buff + len, "Total device count: %d\n",
  915. zcrypt_device_count);
  916. len += sprintf(resp_buff + len, "PCICA count: %d\n",
  917. zcrypt_count_type(ZCRYPT_PCICA));
  918. len += sprintf(resp_buff + len, "PCICC count: %d\n",
  919. zcrypt_count_type(ZCRYPT_PCICC));
  920. len += sprintf(resp_buff + len, "PCIXCC MCL2 count: %d\n",
  921. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  922. len += sprintf(resp_buff + len, "PCIXCC MCL3 count: %d\n",
  923. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  924. len += sprintf(resp_buff + len, "CEX2C count: %d\n",
  925. zcrypt_count_type(ZCRYPT_CEX2C));
  926. len += sprintf(resp_buff + len, "CEX2A count: %d\n",
  927. zcrypt_count_type(ZCRYPT_CEX2A));
  928. len += sprintf(resp_buff + len, "CEX3C count: %d\n",
  929. zcrypt_count_type(ZCRYPT_CEX3C));
  930. len += sprintf(resp_buff + len, "CEX3A count: %d\n",
  931. zcrypt_count_type(ZCRYPT_CEX3A));
  932. len += sprintf(resp_buff + len, "requestq count: %d\n",
  933. zcrypt_requestq_count());
  934. len += sprintf(resp_buff + len, "pendingq count: %d\n",
  935. zcrypt_pendingq_count());
  936. len += sprintf(resp_buff + len, "Total open handles: %d\n\n",
  937. atomic_read(&zcrypt_open_count));
  938. zcrypt_status_mask(workarea);
  939. len += sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  940. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
  941. resp_buff+len, workarea, AP_DEVICES);
  942. zcrypt_qdepth_mask(workarea);
  943. len += sprinthx("Waiting work element counts",
  944. resp_buff+len, workarea, AP_DEVICES);
  945. zcrypt_perdev_reqcnt((int *) workarea);
  946. len += sprinthx4("Per-device successfully completed request counts",
  947. resp_buff+len,(unsigned int *) workarea, AP_DEVICES);
  948. *eof = 1;
  949. memset((void *) workarea, 0x00, AP_DEVICES * sizeof(unsigned int));
  950. return len;
  951. }
  952. static void zcrypt_disable_card(int index)
  953. {
  954. struct zcrypt_device *zdev;
  955. spin_lock_bh(&zcrypt_device_lock);
  956. list_for_each_entry(zdev, &zcrypt_device_list, list)
  957. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  958. zdev->online = 0;
  959. ap_flush_queue(zdev->ap_dev);
  960. break;
  961. }
  962. spin_unlock_bh(&zcrypt_device_lock);
  963. }
  964. static void zcrypt_enable_card(int index)
  965. {
  966. struct zcrypt_device *zdev;
  967. spin_lock_bh(&zcrypt_device_lock);
  968. list_for_each_entry(zdev, &zcrypt_device_list, list)
  969. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  970. zdev->online = 1;
  971. break;
  972. }
  973. spin_unlock_bh(&zcrypt_device_lock);
  974. }
  975. static int zcrypt_status_write(struct file *file, const char __user *buffer,
  976. unsigned long count, void *data)
  977. {
  978. unsigned char *lbuf, *ptr;
  979. unsigned long local_count;
  980. int j;
  981. if (count <= 0)
  982. return 0;
  983. #define LBUFSIZE 1200UL
  984. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  985. if (!lbuf)
  986. return 0;
  987. local_count = min(LBUFSIZE - 1, count);
  988. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  989. kfree(lbuf);
  990. return -EFAULT;
  991. }
  992. lbuf[local_count] = '\0';
  993. ptr = strstr(lbuf, "Online devices");
  994. if (!ptr)
  995. goto out;
  996. ptr = strstr(ptr, "\n");
  997. if (!ptr)
  998. goto out;
  999. ptr++;
  1000. if (strstr(ptr, "Waiting work element counts") == NULL)
  1001. goto out;
  1002. for (j = 0; j < 64 && *ptr; ptr++) {
  1003. /*
  1004. * '0' for no device, '1' for PCICA, '2' for PCICC,
  1005. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  1006. * '5' for CEX2C and '6' for CEX2A'
  1007. * '7' for CEX3C and '8' for CEX3A
  1008. */
  1009. if (*ptr >= '0' && *ptr <= '8')
  1010. j++;
  1011. else if (*ptr == 'd' || *ptr == 'D')
  1012. zcrypt_disable_card(j++);
  1013. else if (*ptr == 'e' || *ptr == 'E')
  1014. zcrypt_enable_card(j++);
  1015. else if (*ptr != ' ' && *ptr != '\t')
  1016. break;
  1017. }
  1018. out:
  1019. kfree(lbuf);
  1020. return count;
  1021. }
  1022. static int zcrypt_rng_device_count;
  1023. static u32 *zcrypt_rng_buffer;
  1024. static int zcrypt_rng_buffer_index;
  1025. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1026. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1027. {
  1028. int rc;
  1029. /*
  1030. * We don't need locking here because the RNG API guarantees serialized
  1031. * read method calls.
  1032. */
  1033. if (zcrypt_rng_buffer_index == 0) {
  1034. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1035. if (rc < 0)
  1036. return -EIO;
  1037. zcrypt_rng_buffer_index = rc / sizeof *data;
  1038. }
  1039. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1040. return sizeof *data;
  1041. }
  1042. static struct hwrng zcrypt_rng_dev = {
  1043. .name = "zcrypt",
  1044. .data_read = zcrypt_rng_data_read,
  1045. };
  1046. static int zcrypt_rng_device_add(void)
  1047. {
  1048. int rc = 0;
  1049. mutex_lock(&zcrypt_rng_mutex);
  1050. if (zcrypt_rng_device_count == 0) {
  1051. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1052. if (!zcrypt_rng_buffer) {
  1053. rc = -ENOMEM;
  1054. goto out;
  1055. }
  1056. zcrypt_rng_buffer_index = 0;
  1057. rc = hwrng_register(&zcrypt_rng_dev);
  1058. if (rc)
  1059. goto out_free;
  1060. zcrypt_rng_device_count = 1;
  1061. } else
  1062. zcrypt_rng_device_count++;
  1063. mutex_unlock(&zcrypt_rng_mutex);
  1064. return 0;
  1065. out_free:
  1066. free_page((unsigned long) zcrypt_rng_buffer);
  1067. out:
  1068. mutex_unlock(&zcrypt_rng_mutex);
  1069. return rc;
  1070. }
  1071. static void zcrypt_rng_device_remove(void)
  1072. {
  1073. mutex_lock(&zcrypt_rng_mutex);
  1074. zcrypt_rng_device_count--;
  1075. if (zcrypt_rng_device_count == 0) {
  1076. hwrng_unregister(&zcrypt_rng_dev);
  1077. free_page((unsigned long) zcrypt_rng_buffer);
  1078. }
  1079. mutex_unlock(&zcrypt_rng_mutex);
  1080. }
  1081. /**
  1082. * zcrypt_api_init(): Module initialization.
  1083. *
  1084. * The module initialization code.
  1085. */
  1086. int __init zcrypt_api_init(void)
  1087. {
  1088. int rc;
  1089. /* Register the request sprayer. */
  1090. rc = misc_register(&zcrypt_misc_device);
  1091. if (rc < 0)
  1092. goto out;
  1093. /* Set up the proc file system */
  1094. zcrypt_entry = create_proc_entry("driver/z90crypt", 0644, NULL);
  1095. if (!zcrypt_entry) {
  1096. rc = -ENOMEM;
  1097. goto out_misc;
  1098. }
  1099. zcrypt_entry->data = NULL;
  1100. zcrypt_entry->read_proc = zcrypt_status_read;
  1101. zcrypt_entry->write_proc = zcrypt_status_write;
  1102. return 0;
  1103. out_misc:
  1104. misc_deregister(&zcrypt_misc_device);
  1105. out:
  1106. return rc;
  1107. }
  1108. /**
  1109. * zcrypt_api_exit(): Module termination.
  1110. *
  1111. * The module termination code.
  1112. */
  1113. void zcrypt_api_exit(void)
  1114. {
  1115. remove_proc_entry("driver/z90crypt", NULL);
  1116. misc_deregister(&zcrypt_misc_device);
  1117. }
  1118. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  1119. module_init(zcrypt_api_init);
  1120. module_exit(zcrypt_api_exit);
  1121. #endif