zcrypt_api.c 29 KB

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