zcrypt_api.c 39 KB

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