zcrypt_api.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. spin_lock_bh(&zcrypt_ops_list_lock);
  286. list_add_tail(&zops->list, &zcrypt_ops_list);
  287. spin_unlock_bh(&zcrypt_ops_list_lock);
  288. }
  289. EXPORT_SYMBOL(zcrypt_msgtype_register);
  290. void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  291. {
  292. spin_lock_bh(&zcrypt_ops_list_lock);
  293. list_del_init(&zops->list);
  294. spin_unlock_bh(&zcrypt_ops_list_lock);
  295. }
  296. EXPORT_SYMBOL(zcrypt_msgtype_unregister);
  297. static inline
  298. struct zcrypt_ops *__ops_lookup(unsigned char *name, int variant)
  299. {
  300. struct zcrypt_ops *zops;
  301. int found = 0;
  302. spin_lock_bh(&zcrypt_ops_list_lock);
  303. list_for_each_entry(zops, &zcrypt_ops_list, list) {
  304. if ((zops->variant == variant) &&
  305. (!strncmp(zops->name, name, sizeof(zops->name)))) {
  306. found = 1;
  307. break;
  308. }
  309. }
  310. if (!found || !try_module_get(zops->owner))
  311. zops = NULL;
  312. spin_unlock_bh(&zcrypt_ops_list_lock);
  313. return zops;
  314. }
  315. struct zcrypt_ops *zcrypt_msgtype_request(unsigned char *name, int variant)
  316. {
  317. struct zcrypt_ops *zops = NULL;
  318. zops = __ops_lookup(name, variant);
  319. if (!zops) {
  320. request_module("%s", name);
  321. zops = __ops_lookup(name, variant);
  322. }
  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. return -EINVAL;
  423. /*
  424. * As long as outputdatalength is big enough, we can set the
  425. * outputdatalength equal to the inputdatalength, since that is the
  426. * number of bytes we will copy in any case
  427. */
  428. crt->outputdatalength = crt->inputdatalength;
  429. copied = 0;
  430. restart:
  431. spin_lock_bh(&zcrypt_device_lock);
  432. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  433. if (!zdev->online ||
  434. !zdev->ops->rsa_modexpo_crt ||
  435. zdev->min_mod_size > crt->inputdatalength ||
  436. zdev->max_mod_size < crt->inputdatalength)
  437. continue;
  438. if (zdev->short_crt && crt->inputdatalength > 240) {
  439. /*
  440. * Check inputdata for leading zeros for cards
  441. * that can't handle np_prime, bp_key, or
  442. * u_mult_inv > 128 bytes.
  443. */
  444. if (copied == 0) {
  445. unsigned int len;
  446. spin_unlock_bh(&zcrypt_device_lock);
  447. /* len is max 256 / 2 - 120 = 8
  448. * For bigger device just assume len of leading
  449. * 0s is 8 as stated in the requirements for
  450. * ica_rsa_modexpo_crt struct in zcrypt.h.
  451. */
  452. if (crt->inputdatalength <= 256)
  453. len = crt->inputdatalength / 2 - 120;
  454. else
  455. len = 8;
  456. if (len > sizeof(z1))
  457. return -EFAULT;
  458. z1 = z2 = z3 = 0;
  459. if (copy_from_user(&z1, crt->np_prime, len) ||
  460. copy_from_user(&z2, crt->bp_key, len) ||
  461. copy_from_user(&z3, crt->u_mult_inv, len))
  462. return -EFAULT;
  463. z1 = z2 = z3 = 0;
  464. copied = 1;
  465. /*
  466. * We have to restart device lookup -
  467. * the device list may have changed by now.
  468. */
  469. goto restart;
  470. }
  471. if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
  472. /* The device can't handle this request. */
  473. continue;
  474. }
  475. zcrypt_device_get(zdev);
  476. get_device(&zdev->ap_dev->device);
  477. zdev->request_count++;
  478. __zcrypt_decrease_preference(zdev);
  479. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  480. spin_unlock_bh(&zcrypt_device_lock);
  481. rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
  482. spin_lock_bh(&zcrypt_device_lock);
  483. module_put(zdev->ap_dev->drv->driver.owner);
  484. }
  485. else
  486. rc = -EAGAIN;
  487. zdev->request_count--;
  488. __zcrypt_increase_preference(zdev);
  489. put_device(&zdev->ap_dev->device);
  490. zcrypt_device_put(zdev);
  491. spin_unlock_bh(&zcrypt_device_lock);
  492. return rc;
  493. }
  494. spin_unlock_bh(&zcrypt_device_lock);
  495. return -ENODEV;
  496. }
  497. static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  498. {
  499. struct zcrypt_device *zdev;
  500. int rc;
  501. spin_lock_bh(&zcrypt_device_lock);
  502. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  503. if (!zdev->online || !zdev->ops->send_cprb ||
  504. (zdev->ops->variant == MSGTYPE06_VARIANT_EP11) ||
  505. (xcRB->user_defined != AUTOSELECT &&
  506. AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined))
  507. continue;
  508. zcrypt_device_get(zdev);
  509. get_device(&zdev->ap_dev->device);
  510. zdev->request_count++;
  511. __zcrypt_decrease_preference(zdev);
  512. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  513. spin_unlock_bh(&zcrypt_device_lock);
  514. rc = zdev->ops->send_cprb(zdev, xcRB);
  515. spin_lock_bh(&zcrypt_device_lock);
  516. module_put(zdev->ap_dev->drv->driver.owner);
  517. }
  518. else
  519. rc = -EAGAIN;
  520. zdev->request_count--;
  521. __zcrypt_increase_preference(zdev);
  522. put_device(&zdev->ap_dev->device);
  523. zcrypt_device_put(zdev);
  524. spin_unlock_bh(&zcrypt_device_lock);
  525. return rc;
  526. }
  527. spin_unlock_bh(&zcrypt_device_lock);
  528. return -ENODEV;
  529. }
  530. struct ep11_target_dev_list {
  531. unsigned short targets_num;
  532. struct ep11_target_dev *targets;
  533. };
  534. static bool is_desired_ep11dev(unsigned int dev_qid,
  535. struct ep11_target_dev_list dev_list)
  536. {
  537. int n;
  538. for (n = 0; n < dev_list.targets_num; n++, dev_list.targets++) {
  539. if ((AP_QID_DEVICE(dev_qid) == dev_list.targets->ap_id) &&
  540. (AP_QID_QUEUE(dev_qid) == dev_list.targets->dom_id)) {
  541. return true;
  542. }
  543. }
  544. return false;
  545. }
  546. static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
  547. {
  548. struct zcrypt_device *zdev;
  549. bool autoselect = false;
  550. int rc;
  551. struct ep11_target_dev_list ep11_dev_list = {
  552. .targets_num = 0x00,
  553. .targets = NULL,
  554. };
  555. ep11_dev_list.targets_num = (unsigned short) xcrb->targets_num;
  556. /* empty list indicates autoselect (all available targets) */
  557. if (ep11_dev_list.targets_num == 0)
  558. autoselect = true;
  559. else {
  560. ep11_dev_list.targets = kcalloc((unsigned short)
  561. xcrb->targets_num,
  562. sizeof(struct ep11_target_dev),
  563. GFP_KERNEL);
  564. if (!ep11_dev_list.targets)
  565. return -ENOMEM;
  566. if (copy_from_user(ep11_dev_list.targets,
  567. (struct ep11_target_dev __force __user *)
  568. xcrb->targets, xcrb->targets_num *
  569. sizeof(struct ep11_target_dev)))
  570. return -EFAULT;
  571. }
  572. spin_lock_bh(&zcrypt_device_lock);
  573. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  574. /* check if device is eligible */
  575. if (!zdev->online ||
  576. zdev->ops->variant != MSGTYPE06_VARIANT_EP11)
  577. continue;
  578. /* check if device is selected as valid target */
  579. if (!is_desired_ep11dev(zdev->ap_dev->qid, ep11_dev_list) &&
  580. !autoselect)
  581. continue;
  582. zcrypt_device_get(zdev);
  583. get_device(&zdev->ap_dev->device);
  584. zdev->request_count++;
  585. __zcrypt_decrease_preference(zdev);
  586. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  587. spin_unlock_bh(&zcrypt_device_lock);
  588. rc = zdev->ops->send_ep11_cprb(zdev, xcrb);
  589. spin_lock_bh(&zcrypt_device_lock);
  590. module_put(zdev->ap_dev->drv->driver.owner);
  591. } else {
  592. rc = -EAGAIN;
  593. }
  594. zdev->request_count--;
  595. __zcrypt_increase_preference(zdev);
  596. put_device(&zdev->ap_dev->device);
  597. zcrypt_device_put(zdev);
  598. spin_unlock_bh(&zcrypt_device_lock);
  599. return rc;
  600. }
  601. spin_unlock_bh(&zcrypt_device_lock);
  602. return -ENODEV;
  603. }
  604. static long zcrypt_rng(char *buffer)
  605. {
  606. struct zcrypt_device *zdev;
  607. int rc;
  608. spin_lock_bh(&zcrypt_device_lock);
  609. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  610. if (!zdev->online || !zdev->ops->rng)
  611. continue;
  612. zcrypt_device_get(zdev);
  613. get_device(&zdev->ap_dev->device);
  614. zdev->request_count++;
  615. __zcrypt_decrease_preference(zdev);
  616. if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
  617. spin_unlock_bh(&zcrypt_device_lock);
  618. rc = zdev->ops->rng(zdev, buffer);
  619. spin_lock_bh(&zcrypt_device_lock);
  620. module_put(zdev->ap_dev->drv->driver.owner);
  621. } else
  622. rc = -EAGAIN;
  623. zdev->request_count--;
  624. __zcrypt_increase_preference(zdev);
  625. put_device(&zdev->ap_dev->device);
  626. zcrypt_device_put(zdev);
  627. spin_unlock_bh(&zcrypt_device_lock);
  628. return rc;
  629. }
  630. spin_unlock_bh(&zcrypt_device_lock);
  631. return -ENODEV;
  632. }
  633. static void zcrypt_status_mask(char status[AP_DEVICES])
  634. {
  635. struct zcrypt_device *zdev;
  636. memset(status, 0, sizeof(char) * AP_DEVICES);
  637. spin_lock_bh(&zcrypt_device_lock);
  638. list_for_each_entry(zdev, &zcrypt_device_list, list)
  639. status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  640. zdev->online ? zdev->user_space_type : 0x0d;
  641. spin_unlock_bh(&zcrypt_device_lock);
  642. }
  643. static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
  644. {
  645. struct zcrypt_device *zdev;
  646. memset(qdepth, 0, sizeof(char) * AP_DEVICES);
  647. spin_lock_bh(&zcrypt_device_lock);
  648. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  649. spin_lock(&zdev->ap_dev->lock);
  650. qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  651. zdev->ap_dev->pendingq_count +
  652. zdev->ap_dev->requestq_count;
  653. spin_unlock(&zdev->ap_dev->lock);
  654. }
  655. spin_unlock_bh(&zcrypt_device_lock);
  656. }
  657. static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
  658. {
  659. struct zcrypt_device *zdev;
  660. memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
  661. spin_lock_bh(&zcrypt_device_lock);
  662. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  663. spin_lock(&zdev->ap_dev->lock);
  664. reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
  665. zdev->ap_dev->total_request_count;
  666. spin_unlock(&zdev->ap_dev->lock);
  667. }
  668. spin_unlock_bh(&zcrypt_device_lock);
  669. }
  670. static int zcrypt_pendingq_count(void)
  671. {
  672. struct zcrypt_device *zdev;
  673. int pendingq_count = 0;
  674. spin_lock_bh(&zcrypt_device_lock);
  675. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  676. spin_lock(&zdev->ap_dev->lock);
  677. pendingq_count += zdev->ap_dev->pendingq_count;
  678. spin_unlock(&zdev->ap_dev->lock);
  679. }
  680. spin_unlock_bh(&zcrypt_device_lock);
  681. return pendingq_count;
  682. }
  683. static int zcrypt_requestq_count(void)
  684. {
  685. struct zcrypt_device *zdev;
  686. int requestq_count = 0;
  687. spin_lock_bh(&zcrypt_device_lock);
  688. list_for_each_entry(zdev, &zcrypt_device_list, list) {
  689. spin_lock(&zdev->ap_dev->lock);
  690. requestq_count += zdev->ap_dev->requestq_count;
  691. spin_unlock(&zdev->ap_dev->lock);
  692. }
  693. spin_unlock_bh(&zcrypt_device_lock);
  694. return requestq_count;
  695. }
  696. static int zcrypt_count_type(int type)
  697. {
  698. struct zcrypt_device *zdev;
  699. int device_count = 0;
  700. spin_lock_bh(&zcrypt_device_lock);
  701. list_for_each_entry(zdev, &zcrypt_device_list, list)
  702. if (zdev->user_space_type == type)
  703. device_count++;
  704. spin_unlock_bh(&zcrypt_device_lock);
  705. return device_count;
  706. }
  707. /**
  708. * zcrypt_ica_status(): Old, depracted combi status call.
  709. *
  710. * Old, deprecated combi status call.
  711. */
  712. static long zcrypt_ica_status(struct file *filp, unsigned long arg)
  713. {
  714. struct ica_z90_status *pstat;
  715. int ret;
  716. pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
  717. if (!pstat)
  718. return -ENOMEM;
  719. pstat->totalcount = zcrypt_device_count;
  720. pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
  721. pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
  722. pstat->requestqWaitCount = zcrypt_requestq_count();
  723. pstat->pendingqWaitCount = zcrypt_pendingq_count();
  724. pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
  725. pstat->cryptoDomain = ap_domain_index;
  726. zcrypt_status_mask(pstat->status);
  727. zcrypt_qdepth_mask(pstat->qdepth);
  728. ret = 0;
  729. if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
  730. ret = -EFAULT;
  731. kfree(pstat);
  732. return ret;
  733. }
  734. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  735. unsigned long arg)
  736. {
  737. int rc;
  738. switch (cmd) {
  739. case ICARSAMODEXPO: {
  740. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  741. struct ica_rsa_modexpo mex;
  742. if (copy_from_user(&mex, umex, sizeof(mex)))
  743. return -EFAULT;
  744. do {
  745. rc = zcrypt_rsa_modexpo(&mex);
  746. } while (rc == -EAGAIN);
  747. /* on failure: retry once again after a requested rescan */
  748. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  749. do {
  750. rc = zcrypt_rsa_modexpo(&mex);
  751. } while (rc == -EAGAIN);
  752. if (rc)
  753. return rc;
  754. return put_user(mex.outputdatalength, &umex->outputdatalength);
  755. }
  756. case ICARSACRT: {
  757. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  758. struct ica_rsa_modexpo_crt crt;
  759. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  760. return -EFAULT;
  761. do {
  762. rc = zcrypt_rsa_crt(&crt);
  763. } while (rc == -EAGAIN);
  764. /* on failure: retry once again after a requested rescan */
  765. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  766. do {
  767. rc = zcrypt_rsa_crt(&crt);
  768. } while (rc == -EAGAIN);
  769. if (rc)
  770. return rc;
  771. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  772. }
  773. case ZSECSENDCPRB: {
  774. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  775. struct ica_xcRB xcRB;
  776. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  777. return -EFAULT;
  778. do {
  779. rc = zcrypt_send_cprb(&xcRB);
  780. } while (rc == -EAGAIN);
  781. /* on failure: retry once again after a requested rescan */
  782. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  783. do {
  784. rc = zcrypt_send_cprb(&xcRB);
  785. } while (rc == -EAGAIN);
  786. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  787. return -EFAULT;
  788. return rc;
  789. }
  790. case ZSENDEP11CPRB: {
  791. struct ep11_urb __user *uxcrb = (void __user *)arg;
  792. struct ep11_urb xcrb;
  793. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  794. return -EFAULT;
  795. do {
  796. rc = zcrypt_send_ep11_cprb(&xcrb);
  797. } while (rc == -EAGAIN);
  798. /* on failure: retry once again after a requested rescan */
  799. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  800. do {
  801. rc = zcrypt_send_ep11_cprb(&xcrb);
  802. } while (rc == -EAGAIN);
  803. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  804. return -EFAULT;
  805. return rc;
  806. }
  807. case Z90STAT_STATUS_MASK: {
  808. char status[AP_DEVICES];
  809. zcrypt_status_mask(status);
  810. if (copy_to_user((char __user *) arg, status,
  811. sizeof(char) * AP_DEVICES))
  812. return -EFAULT;
  813. return 0;
  814. }
  815. case Z90STAT_QDEPTH_MASK: {
  816. char qdepth[AP_DEVICES];
  817. zcrypt_qdepth_mask(qdepth);
  818. if (copy_to_user((char __user *) arg, qdepth,
  819. sizeof(char) * AP_DEVICES))
  820. return -EFAULT;
  821. return 0;
  822. }
  823. case Z90STAT_PERDEV_REQCNT: {
  824. int reqcnt[AP_DEVICES];
  825. zcrypt_perdev_reqcnt(reqcnt);
  826. if (copy_to_user((int __user *) arg, reqcnt,
  827. sizeof(int) * AP_DEVICES))
  828. return -EFAULT;
  829. return 0;
  830. }
  831. case Z90STAT_REQUESTQ_COUNT:
  832. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  833. case Z90STAT_PENDINGQ_COUNT:
  834. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  835. case Z90STAT_TOTALOPEN_COUNT:
  836. return put_user(atomic_read(&zcrypt_open_count),
  837. (int __user *) arg);
  838. case Z90STAT_DOMAIN_INDEX:
  839. return put_user(ap_domain_index, (int __user *) arg);
  840. /*
  841. * Deprecated ioctls. Don't add another device count ioctl,
  842. * you can count them yourself in the user space with the
  843. * output of the Z90STAT_STATUS_MASK ioctl.
  844. */
  845. case ICAZ90STATUS:
  846. return zcrypt_ica_status(filp, arg);
  847. case Z90STAT_TOTALCOUNT:
  848. return put_user(zcrypt_device_count, (int __user *) arg);
  849. case Z90STAT_PCICACOUNT:
  850. return put_user(zcrypt_count_type(ZCRYPT_PCICA),
  851. (int __user *) arg);
  852. case Z90STAT_PCICCCOUNT:
  853. return put_user(zcrypt_count_type(ZCRYPT_PCICC),
  854. (int __user *) arg);
  855. case Z90STAT_PCIXCCMCL2COUNT:
  856. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
  857. (int __user *) arg);
  858. case Z90STAT_PCIXCCMCL3COUNT:
  859. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  860. (int __user *) arg);
  861. case Z90STAT_PCIXCCCOUNT:
  862. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
  863. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  864. (int __user *) arg);
  865. case Z90STAT_CEX2CCOUNT:
  866. return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
  867. (int __user *) arg);
  868. case Z90STAT_CEX2ACOUNT:
  869. return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
  870. (int __user *) arg);
  871. default:
  872. /* unknown ioctl number */
  873. return -ENOIOCTLCMD;
  874. }
  875. }
  876. #ifdef CONFIG_COMPAT
  877. /*
  878. * ioctl32 conversion routines
  879. */
  880. struct compat_ica_rsa_modexpo {
  881. compat_uptr_t inputdata;
  882. unsigned int inputdatalength;
  883. compat_uptr_t outputdata;
  884. unsigned int outputdatalength;
  885. compat_uptr_t b_key;
  886. compat_uptr_t n_modulus;
  887. };
  888. static long trans_modexpo32(struct file *filp, unsigned int cmd,
  889. unsigned long arg)
  890. {
  891. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  892. struct compat_ica_rsa_modexpo mex32;
  893. struct ica_rsa_modexpo mex64;
  894. long rc;
  895. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  896. return -EFAULT;
  897. mex64.inputdata = compat_ptr(mex32.inputdata);
  898. mex64.inputdatalength = mex32.inputdatalength;
  899. mex64.outputdata = compat_ptr(mex32.outputdata);
  900. mex64.outputdatalength = mex32.outputdatalength;
  901. mex64.b_key = compat_ptr(mex32.b_key);
  902. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  903. do {
  904. rc = zcrypt_rsa_modexpo(&mex64);
  905. } while (rc == -EAGAIN);
  906. /* on failure: retry once again after a requested rescan */
  907. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  908. do {
  909. rc = zcrypt_rsa_modexpo(&mex64);
  910. } while (rc == -EAGAIN);
  911. if (rc)
  912. return rc;
  913. return put_user(mex64.outputdatalength,
  914. &umex32->outputdatalength);
  915. }
  916. struct compat_ica_rsa_modexpo_crt {
  917. compat_uptr_t inputdata;
  918. unsigned int inputdatalength;
  919. compat_uptr_t outputdata;
  920. unsigned int outputdatalength;
  921. compat_uptr_t bp_key;
  922. compat_uptr_t bq_key;
  923. compat_uptr_t np_prime;
  924. compat_uptr_t nq_prime;
  925. compat_uptr_t u_mult_inv;
  926. };
  927. static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
  928. unsigned long arg)
  929. {
  930. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  931. struct compat_ica_rsa_modexpo_crt crt32;
  932. struct ica_rsa_modexpo_crt crt64;
  933. long rc;
  934. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  935. return -EFAULT;
  936. crt64.inputdata = compat_ptr(crt32.inputdata);
  937. crt64.inputdatalength = crt32.inputdatalength;
  938. crt64.outputdata= compat_ptr(crt32.outputdata);
  939. crt64.outputdatalength = crt32.outputdatalength;
  940. crt64.bp_key = compat_ptr(crt32.bp_key);
  941. crt64.bq_key = compat_ptr(crt32.bq_key);
  942. crt64.np_prime = compat_ptr(crt32.np_prime);
  943. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  944. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  945. do {
  946. rc = zcrypt_rsa_crt(&crt64);
  947. } while (rc == -EAGAIN);
  948. /* on failure: retry once again after a requested rescan */
  949. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  950. do {
  951. rc = zcrypt_rsa_crt(&crt64);
  952. } while (rc == -EAGAIN);
  953. if (rc)
  954. return rc;
  955. return put_user(crt64.outputdatalength,
  956. &ucrt32->outputdatalength);
  957. }
  958. struct compat_ica_xcRB {
  959. unsigned short agent_ID;
  960. unsigned int user_defined;
  961. unsigned short request_ID;
  962. unsigned int request_control_blk_length;
  963. unsigned char padding1[16 - sizeof (compat_uptr_t)];
  964. compat_uptr_t request_control_blk_addr;
  965. unsigned int request_data_length;
  966. char padding2[16 - sizeof (compat_uptr_t)];
  967. compat_uptr_t request_data_address;
  968. unsigned int reply_control_blk_length;
  969. char padding3[16 - sizeof (compat_uptr_t)];
  970. compat_uptr_t reply_control_blk_addr;
  971. unsigned int reply_data_length;
  972. char padding4[16 - sizeof (compat_uptr_t)];
  973. compat_uptr_t reply_data_addr;
  974. unsigned short priority_window;
  975. unsigned int status;
  976. } __attribute__((packed));
  977. static long trans_xcRB32(struct file *filp, unsigned int cmd,
  978. unsigned long arg)
  979. {
  980. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  981. struct compat_ica_xcRB xcRB32;
  982. struct ica_xcRB xcRB64;
  983. long rc;
  984. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  985. return -EFAULT;
  986. xcRB64.agent_ID = xcRB32.agent_ID;
  987. xcRB64.user_defined = xcRB32.user_defined;
  988. xcRB64.request_ID = xcRB32.request_ID;
  989. xcRB64.request_control_blk_length =
  990. xcRB32.request_control_blk_length;
  991. xcRB64.request_control_blk_addr =
  992. compat_ptr(xcRB32.request_control_blk_addr);
  993. xcRB64.request_data_length =
  994. xcRB32.request_data_length;
  995. xcRB64.request_data_address =
  996. compat_ptr(xcRB32.request_data_address);
  997. xcRB64.reply_control_blk_length =
  998. xcRB32.reply_control_blk_length;
  999. xcRB64.reply_control_blk_addr =
  1000. compat_ptr(xcRB32.reply_control_blk_addr);
  1001. xcRB64.reply_data_length = xcRB32.reply_data_length;
  1002. xcRB64.reply_data_addr =
  1003. compat_ptr(xcRB32.reply_data_addr);
  1004. xcRB64.priority_window = xcRB32.priority_window;
  1005. xcRB64.status = xcRB32.status;
  1006. do {
  1007. rc = zcrypt_send_cprb(&xcRB64);
  1008. } while (rc == -EAGAIN);
  1009. /* on failure: retry once again after a requested rescan */
  1010. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1011. do {
  1012. rc = zcrypt_send_cprb(&xcRB64);
  1013. } while (rc == -EAGAIN);
  1014. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  1015. xcRB32.reply_data_length = xcRB64.reply_data_length;
  1016. xcRB32.status = xcRB64.status;
  1017. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  1018. return -EFAULT;
  1019. return rc;
  1020. }
  1021. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  1022. unsigned long arg)
  1023. {
  1024. if (cmd == ICARSAMODEXPO)
  1025. return trans_modexpo32(filp, cmd, arg);
  1026. if (cmd == ICARSACRT)
  1027. return trans_modexpo_crt32(filp, cmd, arg);
  1028. if (cmd == ZSECSENDCPRB)
  1029. return trans_xcRB32(filp, cmd, arg);
  1030. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  1031. }
  1032. #endif
  1033. /*
  1034. * Misc device file operations.
  1035. */
  1036. static const struct file_operations zcrypt_fops = {
  1037. .owner = THIS_MODULE,
  1038. .read = zcrypt_read,
  1039. .write = zcrypt_write,
  1040. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  1041. #ifdef CONFIG_COMPAT
  1042. .compat_ioctl = zcrypt_compat_ioctl,
  1043. #endif
  1044. .open = zcrypt_open,
  1045. .release = zcrypt_release,
  1046. .llseek = no_llseek,
  1047. };
  1048. /*
  1049. * Misc device.
  1050. */
  1051. static struct miscdevice zcrypt_misc_device = {
  1052. .minor = MISC_DYNAMIC_MINOR,
  1053. .name = "z90crypt",
  1054. .fops = &zcrypt_fops,
  1055. };
  1056. /*
  1057. * Deprecated /proc entry support.
  1058. */
  1059. static struct proc_dir_entry *zcrypt_entry;
  1060. static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
  1061. {
  1062. int i;
  1063. for (i = 0; i < len; i++)
  1064. seq_printf(m, "%01x", (unsigned int) addr[i]);
  1065. seq_putc(m, ' ');
  1066. }
  1067. static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
  1068. {
  1069. int inl, c, cx;
  1070. seq_printf(m, " ");
  1071. inl = 0;
  1072. for (c = 0; c < (len / 16); c++) {
  1073. sprintcl(m, addr+inl, 16);
  1074. inl += 16;
  1075. }
  1076. cx = len%16;
  1077. if (cx) {
  1078. sprintcl(m, addr+inl, cx);
  1079. inl += cx;
  1080. }
  1081. seq_putc(m, '\n');
  1082. }
  1083. static void sprinthx(unsigned char *title, struct seq_file *m,
  1084. unsigned char *addr, unsigned int len)
  1085. {
  1086. int inl, r, rx;
  1087. seq_printf(m, "\n%s\n", title);
  1088. inl = 0;
  1089. for (r = 0; r < (len / 64); r++) {
  1090. sprintrw(m, addr+inl, 64);
  1091. inl += 64;
  1092. }
  1093. rx = len % 64;
  1094. if (rx) {
  1095. sprintrw(m, addr+inl, rx);
  1096. inl += rx;
  1097. }
  1098. seq_putc(m, '\n');
  1099. }
  1100. static void sprinthx4(unsigned char *title, struct seq_file *m,
  1101. unsigned int *array, unsigned int len)
  1102. {
  1103. seq_printf(m, "\n%s\n", title);
  1104. seq_hex_dump(m, " ", DUMP_PREFIX_NONE, 32, 4, array, len, false);
  1105. seq_putc(m, '\n');
  1106. }
  1107. static int zcrypt_proc_show(struct seq_file *m, void *v)
  1108. {
  1109. char workarea[sizeof(int) * AP_DEVICES];
  1110. seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
  1111. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  1112. seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
  1113. seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
  1114. seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
  1115. seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
  1116. seq_printf(m, "PCIXCC MCL2 count: %d\n",
  1117. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  1118. seq_printf(m, "PCIXCC MCL3 count: %d\n",
  1119. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  1120. seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
  1121. seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
  1122. seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
  1123. seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
  1124. seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
  1125. seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
  1126. seq_printf(m, "Total open handles: %d\n\n",
  1127. atomic_read(&zcrypt_open_count));
  1128. zcrypt_status_mask(workarea);
  1129. sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  1130. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
  1131. m, workarea, AP_DEVICES);
  1132. zcrypt_qdepth_mask(workarea);
  1133. sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
  1134. zcrypt_perdev_reqcnt((int *) workarea);
  1135. sprinthx4("Per-device successfully completed request counts",
  1136. m, (unsigned int *) workarea, AP_DEVICES);
  1137. return 0;
  1138. }
  1139. static int zcrypt_proc_open(struct inode *inode, struct file *file)
  1140. {
  1141. return single_open(file, zcrypt_proc_show, NULL);
  1142. }
  1143. static void zcrypt_disable_card(int index)
  1144. {
  1145. struct zcrypt_device *zdev;
  1146. spin_lock_bh(&zcrypt_device_lock);
  1147. list_for_each_entry(zdev, &zcrypt_device_list, list)
  1148. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  1149. zdev->online = 0;
  1150. ap_flush_queue(zdev->ap_dev);
  1151. break;
  1152. }
  1153. spin_unlock_bh(&zcrypt_device_lock);
  1154. }
  1155. static void zcrypt_enable_card(int index)
  1156. {
  1157. struct zcrypt_device *zdev;
  1158. spin_lock_bh(&zcrypt_device_lock);
  1159. list_for_each_entry(zdev, &zcrypt_device_list, list)
  1160. if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
  1161. zdev->online = 1;
  1162. break;
  1163. }
  1164. spin_unlock_bh(&zcrypt_device_lock);
  1165. }
  1166. static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
  1167. size_t count, loff_t *pos)
  1168. {
  1169. unsigned char *lbuf, *ptr;
  1170. size_t local_count;
  1171. int j;
  1172. if (count <= 0)
  1173. return 0;
  1174. #define LBUFSIZE 1200UL
  1175. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  1176. if (!lbuf)
  1177. return 0;
  1178. local_count = min(LBUFSIZE - 1, count);
  1179. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  1180. kfree(lbuf);
  1181. return -EFAULT;
  1182. }
  1183. lbuf[local_count] = '\0';
  1184. ptr = strstr(lbuf, "Online devices");
  1185. if (!ptr)
  1186. goto out;
  1187. ptr = strstr(ptr, "\n");
  1188. if (!ptr)
  1189. goto out;
  1190. ptr++;
  1191. if (strstr(ptr, "Waiting work element counts") == NULL)
  1192. goto out;
  1193. for (j = 0; j < 64 && *ptr; ptr++) {
  1194. /*
  1195. * '0' for no device, '1' for PCICA, '2' for PCICC,
  1196. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  1197. * '5' for CEX2C and '6' for CEX2A'
  1198. * '7' for CEX3C and '8' for CEX3A
  1199. */
  1200. if (*ptr >= '0' && *ptr <= '8')
  1201. j++;
  1202. else if (*ptr == 'd' || *ptr == 'D')
  1203. zcrypt_disable_card(j++);
  1204. else if (*ptr == 'e' || *ptr == 'E')
  1205. zcrypt_enable_card(j++);
  1206. else if (*ptr != ' ' && *ptr != '\t')
  1207. break;
  1208. }
  1209. out:
  1210. kfree(lbuf);
  1211. return count;
  1212. }
  1213. static const struct file_operations zcrypt_proc_fops = {
  1214. .owner = THIS_MODULE,
  1215. .open = zcrypt_proc_open,
  1216. .read = seq_read,
  1217. .llseek = seq_lseek,
  1218. .release = single_release,
  1219. .write = zcrypt_proc_write,
  1220. };
  1221. static int zcrypt_rng_device_count;
  1222. static u32 *zcrypt_rng_buffer;
  1223. static int zcrypt_rng_buffer_index;
  1224. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1225. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1226. {
  1227. int rc;
  1228. /*
  1229. * We don't need locking here because the RNG API guarantees serialized
  1230. * read method calls.
  1231. */
  1232. if (zcrypt_rng_buffer_index == 0) {
  1233. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1234. /* on failure: retry once again after a requested rescan */
  1235. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1236. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1237. if (rc < 0)
  1238. return -EIO;
  1239. zcrypt_rng_buffer_index = rc / sizeof *data;
  1240. }
  1241. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1242. return sizeof *data;
  1243. }
  1244. static struct hwrng zcrypt_rng_dev = {
  1245. .name = "zcrypt",
  1246. .data_read = zcrypt_rng_data_read,
  1247. .quality = 990,
  1248. };
  1249. static int zcrypt_rng_device_add(void)
  1250. {
  1251. int rc = 0;
  1252. mutex_lock(&zcrypt_rng_mutex);
  1253. if (zcrypt_rng_device_count == 0) {
  1254. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1255. if (!zcrypt_rng_buffer) {
  1256. rc = -ENOMEM;
  1257. goto out;
  1258. }
  1259. zcrypt_rng_buffer_index = 0;
  1260. if (!zcrypt_hwrng_seed)
  1261. zcrypt_rng_dev.quality = 0;
  1262. rc = hwrng_register(&zcrypt_rng_dev);
  1263. if (rc)
  1264. goto out_free;
  1265. zcrypt_rng_device_count = 1;
  1266. } else
  1267. zcrypt_rng_device_count++;
  1268. mutex_unlock(&zcrypt_rng_mutex);
  1269. return 0;
  1270. out_free:
  1271. free_page((unsigned long) zcrypt_rng_buffer);
  1272. out:
  1273. mutex_unlock(&zcrypt_rng_mutex);
  1274. return rc;
  1275. }
  1276. static void zcrypt_rng_device_remove(void)
  1277. {
  1278. mutex_lock(&zcrypt_rng_mutex);
  1279. zcrypt_rng_device_count--;
  1280. if (zcrypt_rng_device_count == 0) {
  1281. hwrng_unregister(&zcrypt_rng_dev);
  1282. free_page((unsigned long) zcrypt_rng_buffer);
  1283. }
  1284. mutex_unlock(&zcrypt_rng_mutex);
  1285. }
  1286. int __init zcrypt_debug_init(void)
  1287. {
  1288. debugfs_root = debugfs_create_dir("zcrypt", NULL);
  1289. zcrypt_dbf_common = debug_register("zcrypt_common", 1, 1, 16);
  1290. debug_register_view(zcrypt_dbf_common, &debug_hex_ascii_view);
  1291. debug_set_level(zcrypt_dbf_common, DBF_ERR);
  1292. zcrypt_dbf_devices = debug_register("zcrypt_devices", 1, 1, 16);
  1293. debug_register_view(zcrypt_dbf_devices, &debug_hex_ascii_view);
  1294. debug_set_level(zcrypt_dbf_devices, DBF_ERR);
  1295. return 0;
  1296. }
  1297. void zcrypt_debug_exit(void)
  1298. {
  1299. debugfs_remove(debugfs_root);
  1300. if (zcrypt_dbf_common)
  1301. debug_unregister(zcrypt_dbf_common);
  1302. if (zcrypt_dbf_devices)
  1303. debug_unregister(zcrypt_dbf_devices);
  1304. }
  1305. /**
  1306. * zcrypt_api_init(): Module initialization.
  1307. *
  1308. * The module initialization code.
  1309. */
  1310. int __init zcrypt_api_init(void)
  1311. {
  1312. int rc;
  1313. rc = zcrypt_debug_init();
  1314. if (rc)
  1315. goto out;
  1316. atomic_set(&zcrypt_rescan_req, 0);
  1317. /* Register the request sprayer. */
  1318. rc = misc_register(&zcrypt_misc_device);
  1319. if (rc < 0)
  1320. goto out;
  1321. /* Set up the proc file system */
  1322. zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
  1323. if (!zcrypt_entry) {
  1324. rc = -ENOMEM;
  1325. goto out_misc;
  1326. }
  1327. return 0;
  1328. out_misc:
  1329. misc_deregister(&zcrypt_misc_device);
  1330. out:
  1331. return rc;
  1332. }
  1333. /**
  1334. * zcrypt_api_exit(): Module termination.
  1335. *
  1336. * The module termination code.
  1337. */
  1338. void zcrypt_api_exit(void)
  1339. {
  1340. remove_proc_entry("driver/z90crypt", NULL);
  1341. misc_deregister(&zcrypt_misc_device);
  1342. zcrypt_debug_exit();
  1343. }
  1344. module_init(zcrypt_api_init);
  1345. module_exit(zcrypt_api_exit);