zcrypt_api.c 33 KB

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