zcrypt_api.c 36 KB

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