zcrypt_api.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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 <linux/uaccess.h>
  39. #include <linux/hw_random.h>
  40. #include <linux/debugfs.h>
  41. #include <asm/debug.h>
  42. #define CREATE_TRACE_POINTS
  43. #include <asm/trace/zcrypt.h>
  44. #include "zcrypt_api.h"
  45. #include "zcrypt_debug.h"
  46. #include "zcrypt_msgtype6.h"
  47. #include "zcrypt_msgtype50.h"
  48. /*
  49. * Module description.
  50. */
  51. MODULE_AUTHOR("IBM Corporation");
  52. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  53. "Copyright IBM Corp. 2001, 2012");
  54. MODULE_LICENSE("GPL");
  55. /*
  56. * zcrypt tracepoint functions
  57. */
  58. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
  59. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
  60. static int zcrypt_hwrng_seed = 1;
  61. module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, S_IRUSR|S_IRGRP);
  62. MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
  63. DEFINE_SPINLOCK(zcrypt_list_lock);
  64. LIST_HEAD(zcrypt_card_list);
  65. int zcrypt_device_count;
  66. static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  67. static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
  68. atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
  69. EXPORT_SYMBOL(zcrypt_rescan_req);
  70. static LIST_HEAD(zcrypt_ops_list);
  71. /* Zcrypt related debug feature stuff. */
  72. debug_info_t *zcrypt_dbf_info;
  73. /**
  74. * Process a rescan of the transport layer.
  75. *
  76. * Returns 1, if the rescan has been processed, otherwise 0.
  77. */
  78. static inline int zcrypt_process_rescan(void)
  79. {
  80. if (atomic_read(&zcrypt_rescan_req)) {
  81. atomic_set(&zcrypt_rescan_req, 0);
  82. atomic_inc(&zcrypt_rescan_count);
  83. ap_bus_force_rescan();
  84. ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
  85. atomic_inc_return(&zcrypt_rescan_count));
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. void zcrypt_msgtype_register(struct zcrypt_ops *zops)
  91. {
  92. list_add_tail(&zops->list, &zcrypt_ops_list);
  93. }
  94. void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  95. {
  96. list_del_init(&zops->list);
  97. }
  98. struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
  99. {
  100. struct zcrypt_ops *zops;
  101. list_for_each_entry(zops, &zcrypt_ops_list, list)
  102. if ((zops->variant == variant) &&
  103. (!strncmp(zops->name, name, sizeof(zops->name))))
  104. return zops;
  105. return NULL;
  106. }
  107. EXPORT_SYMBOL(zcrypt_msgtype);
  108. /**
  109. * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
  110. *
  111. * This function is not supported beyond zcrypt 1.3.1.
  112. */
  113. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  114. size_t count, loff_t *f_pos)
  115. {
  116. return -EPERM;
  117. }
  118. /**
  119. * zcrypt_write(): Not allowed.
  120. *
  121. * Write is is not allowed
  122. */
  123. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  124. size_t count, loff_t *f_pos)
  125. {
  126. return -EPERM;
  127. }
  128. /**
  129. * zcrypt_open(): Count number of users.
  130. *
  131. * Device open function to count number of users.
  132. */
  133. static int zcrypt_open(struct inode *inode, struct file *filp)
  134. {
  135. atomic_inc(&zcrypt_open_count);
  136. return nonseekable_open(inode, filp);
  137. }
  138. /**
  139. * zcrypt_release(): Count number of users.
  140. *
  141. * Device close function to count number of users.
  142. */
  143. static int zcrypt_release(struct inode *inode, struct file *filp)
  144. {
  145. atomic_dec(&zcrypt_open_count);
  146. return 0;
  147. }
  148. static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
  149. struct zcrypt_queue *zq,
  150. unsigned int weight)
  151. {
  152. if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
  153. return NULL;
  154. zcrypt_queue_get(zq);
  155. get_device(&zq->queue->ap_dev.device);
  156. atomic_add(weight, &zc->load);
  157. atomic_add(weight, &zq->load);
  158. zq->request_count++;
  159. return zq;
  160. }
  161. static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
  162. struct zcrypt_queue *zq,
  163. unsigned int weight)
  164. {
  165. struct module *mod = zq->queue->ap_dev.drv->driver.owner;
  166. zq->request_count--;
  167. atomic_sub(weight, &zc->load);
  168. atomic_sub(weight, &zq->load);
  169. put_device(&zq->queue->ap_dev.device);
  170. zcrypt_queue_put(zq);
  171. module_put(mod);
  172. }
  173. static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
  174. struct zcrypt_card *pref_zc,
  175. unsigned weight, unsigned pref_weight)
  176. {
  177. if (!pref_zc)
  178. return false;
  179. weight += atomic_read(&zc->load);
  180. pref_weight += atomic_read(&pref_zc->load);
  181. if (weight == pref_weight)
  182. return atomic_read(&zc->card->total_request_count) >
  183. atomic_read(&pref_zc->card->total_request_count);
  184. return weight > pref_weight;
  185. }
  186. static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
  187. struct zcrypt_queue *pref_zq,
  188. unsigned weight, unsigned pref_weight)
  189. {
  190. if (!pref_zq)
  191. return false;
  192. weight += atomic_read(&zq->load);
  193. pref_weight += atomic_read(&pref_zq->load);
  194. if (weight == pref_weight)
  195. return &zq->queue->total_request_count >
  196. &pref_zq->queue->total_request_count;
  197. return weight > pref_weight;
  198. }
  199. /*
  200. * zcrypt ioctls.
  201. */
  202. static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
  203. {
  204. struct zcrypt_card *zc, *pref_zc;
  205. struct zcrypt_queue *zq, *pref_zq;
  206. unsigned int weight, pref_weight;
  207. unsigned int func_code;
  208. int qid = 0, rc = -ENODEV;
  209. trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
  210. if (mex->outputdatalength < mex->inputdatalength) {
  211. rc = -EINVAL;
  212. goto out;
  213. }
  214. /*
  215. * As long as outputdatalength is big enough, we can set the
  216. * outputdatalength equal to the inputdatalength, since that is the
  217. * number of bytes we will copy in any case
  218. */
  219. mex->outputdatalength = mex->inputdatalength;
  220. rc = get_rsa_modex_fc(mex, &func_code);
  221. if (rc)
  222. goto out;
  223. pref_zc = NULL;
  224. pref_zq = NULL;
  225. spin_lock(&zcrypt_list_lock);
  226. for_each_zcrypt_card(zc) {
  227. /* Check for online accelarator and CCA cards */
  228. if (!zc->online || !(zc->card->functions & 0x18000000))
  229. continue;
  230. /* Check for size limits */
  231. if (zc->min_mod_size > mex->inputdatalength ||
  232. zc->max_mod_size < mex->inputdatalength)
  233. continue;
  234. /* get weight index of the card device */
  235. weight = zc->speed_rating[func_code];
  236. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  237. continue;
  238. for_each_zcrypt_queue(zq, zc) {
  239. /* check if device is online and eligible */
  240. if (!zq->online || !zq->ops->rsa_modexpo)
  241. continue;
  242. if (zcrypt_queue_compare(zq, pref_zq,
  243. weight, pref_weight))
  244. continue;
  245. pref_zc = zc;
  246. pref_zq = zq;
  247. pref_weight = weight;
  248. }
  249. }
  250. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  251. spin_unlock(&zcrypt_list_lock);
  252. if (!pref_zq) {
  253. rc = -ENODEV;
  254. goto out;
  255. }
  256. qid = pref_zq->queue->qid;
  257. rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
  258. spin_lock(&zcrypt_list_lock);
  259. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  260. spin_unlock(&zcrypt_list_lock);
  261. out:
  262. trace_s390_zcrypt_rep(mex, func_code, rc,
  263. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  264. return rc;
  265. }
  266. static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
  267. {
  268. struct zcrypt_card *zc, *pref_zc;
  269. struct zcrypt_queue *zq, *pref_zq;
  270. unsigned int weight, pref_weight;
  271. unsigned int func_code;
  272. int qid = 0, rc = -ENODEV;
  273. trace_s390_zcrypt_req(crt, TP_ICARSACRT);
  274. if (crt->outputdatalength < crt->inputdatalength) {
  275. rc = -EINVAL;
  276. goto out;
  277. }
  278. /*
  279. * As long as outputdatalength is big enough, we can set the
  280. * outputdatalength equal to the inputdatalength, since that is the
  281. * number of bytes we will copy in any case
  282. */
  283. crt->outputdatalength = crt->inputdatalength;
  284. rc = get_rsa_crt_fc(crt, &func_code);
  285. if (rc)
  286. goto out;
  287. pref_zc = NULL;
  288. pref_zq = NULL;
  289. spin_lock(&zcrypt_list_lock);
  290. for_each_zcrypt_card(zc) {
  291. /* Check for online accelarator and CCA cards */
  292. if (!zc->online || !(zc->card->functions & 0x18000000))
  293. continue;
  294. /* Check for size limits */
  295. if (zc->min_mod_size > crt->inputdatalength ||
  296. zc->max_mod_size < crt->inputdatalength)
  297. continue;
  298. /* get weight index of the card device */
  299. weight = zc->speed_rating[func_code];
  300. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  301. continue;
  302. for_each_zcrypt_queue(zq, zc) {
  303. /* check if device is online and eligible */
  304. if (!zq->online || !zq->ops->rsa_modexpo_crt)
  305. continue;
  306. if (zcrypt_queue_compare(zq, pref_zq,
  307. weight, pref_weight))
  308. continue;
  309. pref_zc = zc;
  310. pref_zq = zq;
  311. pref_weight = weight;
  312. }
  313. }
  314. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  315. spin_unlock(&zcrypt_list_lock);
  316. if (!pref_zq) {
  317. rc = -ENODEV;
  318. goto out;
  319. }
  320. qid = pref_zq->queue->qid;
  321. rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
  322. spin_lock(&zcrypt_list_lock);
  323. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  324. spin_unlock(&zcrypt_list_lock);
  325. out:
  326. trace_s390_zcrypt_rep(crt, func_code, rc,
  327. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  328. return rc;
  329. }
  330. long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  331. {
  332. struct zcrypt_card *zc, *pref_zc;
  333. struct zcrypt_queue *zq, *pref_zq;
  334. struct ap_message ap_msg;
  335. unsigned int weight, pref_weight;
  336. unsigned int func_code;
  337. unsigned short *domain;
  338. int qid = 0, rc = -ENODEV;
  339. trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
  340. rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
  341. if (rc)
  342. goto out;
  343. pref_zc = NULL;
  344. pref_zq = NULL;
  345. spin_lock(&zcrypt_list_lock);
  346. for_each_zcrypt_card(zc) {
  347. /* Check for online CCA cards */
  348. if (!zc->online || !(zc->card->functions & 0x10000000))
  349. continue;
  350. /* Check for user selected CCA card */
  351. if (xcRB->user_defined != AUTOSELECT &&
  352. xcRB->user_defined != zc->card->id)
  353. continue;
  354. /* get weight index of the card device */
  355. weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
  356. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  357. continue;
  358. for_each_zcrypt_queue(zq, zc) {
  359. /* check if device is online and eligible */
  360. if (!zq->online ||
  361. !zq->ops->send_cprb ||
  362. ((*domain != (unsigned short) AUTOSELECT) &&
  363. (*domain != AP_QID_QUEUE(zq->queue->qid))))
  364. continue;
  365. if (zcrypt_queue_compare(zq, pref_zq,
  366. weight, pref_weight))
  367. continue;
  368. pref_zc = zc;
  369. pref_zq = zq;
  370. pref_weight = weight;
  371. }
  372. }
  373. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  374. spin_unlock(&zcrypt_list_lock);
  375. if (!pref_zq) {
  376. rc = -ENODEV;
  377. goto out;
  378. }
  379. /* in case of auto select, provide the correct domain */
  380. qid = pref_zq->queue->qid;
  381. if (*domain == (unsigned short) AUTOSELECT)
  382. *domain = AP_QID_QUEUE(qid);
  383. rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
  384. spin_lock(&zcrypt_list_lock);
  385. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  386. spin_unlock(&zcrypt_list_lock);
  387. out:
  388. trace_s390_zcrypt_rep(xcRB, func_code, rc,
  389. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  390. return rc;
  391. }
  392. EXPORT_SYMBOL(zcrypt_send_cprb);
  393. static bool is_desired_ep11_card(unsigned int dev_id,
  394. unsigned short target_num,
  395. struct ep11_target_dev *targets)
  396. {
  397. while (target_num-- > 0) {
  398. if (dev_id == targets->ap_id)
  399. return true;
  400. targets++;
  401. }
  402. return false;
  403. }
  404. static bool is_desired_ep11_queue(unsigned int dev_qid,
  405. unsigned short target_num,
  406. struct ep11_target_dev *targets)
  407. {
  408. while (target_num-- > 0) {
  409. if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
  410. return true;
  411. targets++;
  412. }
  413. return false;
  414. }
  415. static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
  416. {
  417. struct zcrypt_card *zc, *pref_zc;
  418. struct zcrypt_queue *zq, *pref_zq;
  419. struct ep11_target_dev *targets;
  420. unsigned short target_num;
  421. unsigned int weight, pref_weight;
  422. unsigned int func_code;
  423. struct ap_message ap_msg;
  424. int qid = 0, rc = -ENODEV;
  425. trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
  426. target_num = (unsigned short) xcrb->targets_num;
  427. /* empty list indicates autoselect (all available targets) */
  428. targets = NULL;
  429. if (target_num != 0) {
  430. struct ep11_target_dev __user *uptr;
  431. targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
  432. if (!targets) {
  433. rc = -ENOMEM;
  434. goto out;
  435. }
  436. uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
  437. if (copy_from_user(targets, uptr,
  438. target_num * sizeof(*targets))) {
  439. rc = -EFAULT;
  440. goto out;
  441. }
  442. }
  443. rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
  444. if (rc)
  445. goto out_free;
  446. pref_zc = NULL;
  447. pref_zq = NULL;
  448. spin_lock(&zcrypt_list_lock);
  449. for_each_zcrypt_card(zc) {
  450. /* Check for online EP11 cards */
  451. if (!zc->online || !(zc->card->functions & 0x04000000))
  452. continue;
  453. /* Check for user selected EP11 card */
  454. if (targets &&
  455. !is_desired_ep11_card(zc->card->id, target_num, targets))
  456. continue;
  457. /* get weight index of the card device */
  458. weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
  459. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  460. continue;
  461. for_each_zcrypt_queue(zq, zc) {
  462. /* check if device is online and eligible */
  463. if (!zq->online ||
  464. !zq->ops->send_ep11_cprb ||
  465. (targets &&
  466. !is_desired_ep11_queue(zq->queue->qid,
  467. target_num, targets)))
  468. continue;
  469. if (zcrypt_queue_compare(zq, pref_zq,
  470. weight, pref_weight))
  471. continue;
  472. pref_zc = zc;
  473. pref_zq = zq;
  474. pref_weight = weight;
  475. }
  476. }
  477. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  478. spin_unlock(&zcrypt_list_lock);
  479. if (!pref_zq) {
  480. rc = -ENODEV;
  481. goto out_free;
  482. }
  483. qid = pref_zq->queue->qid;
  484. rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
  485. spin_lock(&zcrypt_list_lock);
  486. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  487. spin_unlock(&zcrypt_list_lock);
  488. out_free:
  489. kfree(targets);
  490. out:
  491. trace_s390_zcrypt_rep(xcrb, func_code, rc,
  492. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  493. return rc;
  494. }
  495. static long zcrypt_rng(char *buffer)
  496. {
  497. struct zcrypt_card *zc, *pref_zc;
  498. struct zcrypt_queue *zq, *pref_zq;
  499. unsigned int weight, pref_weight;
  500. unsigned int func_code;
  501. struct ap_message ap_msg;
  502. unsigned int domain;
  503. int qid = 0, rc = -ENODEV;
  504. trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
  505. rc = get_rng_fc(&ap_msg, &func_code, &domain);
  506. if (rc)
  507. goto out;
  508. pref_zc = NULL;
  509. pref_zq = NULL;
  510. spin_lock(&zcrypt_list_lock);
  511. for_each_zcrypt_card(zc) {
  512. /* Check for online CCA cards */
  513. if (!zc->online || !(zc->card->functions & 0x10000000))
  514. continue;
  515. /* get weight index of the card device */
  516. weight = zc->speed_rating[func_code];
  517. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  518. continue;
  519. for_each_zcrypt_queue(zq, zc) {
  520. /* check if device is online and eligible */
  521. if (!zq->online || !zq->ops->rng)
  522. continue;
  523. if (zcrypt_queue_compare(zq, pref_zq,
  524. weight, pref_weight))
  525. continue;
  526. pref_zc = zc;
  527. pref_zq = zq;
  528. pref_weight = weight;
  529. }
  530. }
  531. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  532. spin_unlock(&zcrypt_list_lock);
  533. if (!pref_zq)
  534. return -ENODEV;
  535. qid = pref_zq->queue->qid;
  536. rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
  537. spin_lock(&zcrypt_list_lock);
  538. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  539. spin_unlock(&zcrypt_list_lock);
  540. out:
  541. trace_s390_zcrypt_rep(buffer, func_code, rc,
  542. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  543. return rc;
  544. }
  545. void zcrypt_device_status_mask(struct zcrypt_device_matrix *matrix)
  546. {
  547. struct zcrypt_card *zc;
  548. struct zcrypt_queue *zq;
  549. struct zcrypt_device_status *stat;
  550. memset(matrix, 0, sizeof(*matrix));
  551. spin_lock(&zcrypt_list_lock);
  552. for_each_zcrypt_card(zc) {
  553. for_each_zcrypt_queue(zq, zc) {
  554. stat = matrix->device;
  555. stat += AP_QID_CARD(zq->queue->qid) * MAX_ZDEV_DOMAINS;
  556. stat += AP_QID_QUEUE(zq->queue->qid);
  557. stat->hwtype = zc->card->ap_dev.device_type;
  558. stat->functions = zc->card->functions >> 26;
  559. stat->qid = zq->queue->qid;
  560. stat->online = zq->online ? 0x01 : 0x00;
  561. }
  562. }
  563. spin_unlock(&zcrypt_list_lock);
  564. }
  565. EXPORT_SYMBOL(zcrypt_device_status_mask);
  566. static void zcrypt_status_mask(char status[AP_DEVICES])
  567. {
  568. struct zcrypt_card *zc;
  569. struct zcrypt_queue *zq;
  570. memset(status, 0, sizeof(char) * AP_DEVICES);
  571. spin_lock(&zcrypt_list_lock);
  572. for_each_zcrypt_card(zc) {
  573. for_each_zcrypt_queue(zq, zc) {
  574. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  575. continue;
  576. status[AP_QID_CARD(zq->queue->qid)] =
  577. zc->online ? zc->user_space_type : 0x0d;
  578. }
  579. }
  580. spin_unlock(&zcrypt_list_lock);
  581. }
  582. static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
  583. {
  584. struct zcrypt_card *zc;
  585. struct zcrypt_queue *zq;
  586. memset(qdepth, 0, sizeof(char) * AP_DEVICES);
  587. spin_lock(&zcrypt_list_lock);
  588. local_bh_disable();
  589. for_each_zcrypt_card(zc) {
  590. for_each_zcrypt_queue(zq, zc) {
  591. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  592. continue;
  593. spin_lock(&zq->queue->lock);
  594. qdepth[AP_QID_CARD(zq->queue->qid)] =
  595. zq->queue->pendingq_count +
  596. zq->queue->requestq_count;
  597. spin_unlock(&zq->queue->lock);
  598. }
  599. }
  600. local_bh_enable();
  601. spin_unlock(&zcrypt_list_lock);
  602. }
  603. static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
  604. {
  605. struct zcrypt_card *zc;
  606. struct zcrypt_queue *zq;
  607. memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
  608. spin_lock(&zcrypt_list_lock);
  609. local_bh_disable();
  610. for_each_zcrypt_card(zc) {
  611. for_each_zcrypt_queue(zq, zc) {
  612. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  613. continue;
  614. spin_lock(&zq->queue->lock);
  615. reqcnt[AP_QID_CARD(zq->queue->qid)] =
  616. zq->queue->total_request_count;
  617. spin_unlock(&zq->queue->lock);
  618. }
  619. }
  620. local_bh_enable();
  621. spin_unlock(&zcrypt_list_lock);
  622. }
  623. static int zcrypt_pendingq_count(void)
  624. {
  625. struct zcrypt_card *zc;
  626. struct zcrypt_queue *zq;
  627. int pendingq_count;
  628. pendingq_count = 0;
  629. spin_lock(&zcrypt_list_lock);
  630. local_bh_disable();
  631. for_each_zcrypt_card(zc) {
  632. for_each_zcrypt_queue(zq, zc) {
  633. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  634. continue;
  635. spin_lock(&zq->queue->lock);
  636. pendingq_count += zq->queue->pendingq_count;
  637. spin_unlock(&zq->queue->lock);
  638. }
  639. }
  640. local_bh_enable();
  641. spin_unlock(&zcrypt_list_lock);
  642. return pendingq_count;
  643. }
  644. static int zcrypt_requestq_count(void)
  645. {
  646. struct zcrypt_card *zc;
  647. struct zcrypt_queue *zq;
  648. int requestq_count;
  649. requestq_count = 0;
  650. spin_lock(&zcrypt_list_lock);
  651. local_bh_disable();
  652. for_each_zcrypt_card(zc) {
  653. for_each_zcrypt_queue(zq, zc) {
  654. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  655. continue;
  656. spin_lock(&zq->queue->lock);
  657. requestq_count += zq->queue->requestq_count;
  658. spin_unlock(&zq->queue->lock);
  659. }
  660. }
  661. local_bh_enable();
  662. spin_unlock(&zcrypt_list_lock);
  663. return requestq_count;
  664. }
  665. static int zcrypt_count_type(int type)
  666. {
  667. struct zcrypt_card *zc;
  668. struct zcrypt_queue *zq;
  669. int device_count;
  670. device_count = 0;
  671. spin_lock(&zcrypt_list_lock);
  672. for_each_zcrypt_card(zc) {
  673. if (zc->card->id != type)
  674. continue;
  675. for_each_zcrypt_queue(zq, zc) {
  676. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  677. continue;
  678. device_count++;
  679. }
  680. }
  681. spin_unlock(&zcrypt_list_lock);
  682. return device_count;
  683. }
  684. /**
  685. * zcrypt_ica_status(): Old, depracted combi status call.
  686. *
  687. * Old, deprecated combi status call.
  688. */
  689. static long zcrypt_ica_status(struct file *filp, unsigned long arg)
  690. {
  691. struct ica_z90_status *pstat;
  692. int ret;
  693. pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
  694. if (!pstat)
  695. return -ENOMEM;
  696. pstat->totalcount = zcrypt_device_count;
  697. pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
  698. pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
  699. pstat->requestqWaitCount = zcrypt_requestq_count();
  700. pstat->pendingqWaitCount = zcrypt_pendingq_count();
  701. pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
  702. pstat->cryptoDomain = ap_domain_index;
  703. zcrypt_status_mask(pstat->status);
  704. zcrypt_qdepth_mask(pstat->qdepth);
  705. ret = 0;
  706. if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
  707. ret = -EFAULT;
  708. kfree(pstat);
  709. return ret;
  710. }
  711. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  712. unsigned long arg)
  713. {
  714. int rc;
  715. switch (cmd) {
  716. case ICARSAMODEXPO: {
  717. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  718. struct ica_rsa_modexpo mex;
  719. if (copy_from_user(&mex, umex, sizeof(mex)))
  720. return -EFAULT;
  721. do {
  722. rc = zcrypt_rsa_modexpo(&mex);
  723. } while (rc == -EAGAIN);
  724. /* on failure: retry once again after a requested rescan */
  725. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  726. do {
  727. rc = zcrypt_rsa_modexpo(&mex);
  728. } while (rc == -EAGAIN);
  729. if (rc) {
  730. ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
  731. return rc;
  732. }
  733. return put_user(mex.outputdatalength, &umex->outputdatalength);
  734. }
  735. case ICARSACRT: {
  736. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  737. struct ica_rsa_modexpo_crt crt;
  738. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  739. return -EFAULT;
  740. do {
  741. rc = zcrypt_rsa_crt(&crt);
  742. } while (rc == -EAGAIN);
  743. /* on failure: retry once again after a requested rescan */
  744. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  745. do {
  746. rc = zcrypt_rsa_crt(&crt);
  747. } while (rc == -EAGAIN);
  748. if (rc) {
  749. ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
  750. return rc;
  751. }
  752. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  753. }
  754. case ZSECSENDCPRB: {
  755. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  756. struct ica_xcRB xcRB;
  757. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  758. return -EFAULT;
  759. do {
  760. rc = zcrypt_send_cprb(&xcRB);
  761. } while (rc == -EAGAIN);
  762. /* on failure: retry once again after a requested rescan */
  763. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  764. do {
  765. rc = zcrypt_send_cprb(&xcRB);
  766. } while (rc == -EAGAIN);
  767. if (rc)
  768. ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d\n", rc);
  769. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  770. return -EFAULT;
  771. return rc;
  772. }
  773. case ZSENDEP11CPRB: {
  774. struct ep11_urb __user *uxcrb = (void __user *)arg;
  775. struct ep11_urb xcrb;
  776. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  777. return -EFAULT;
  778. do {
  779. rc = zcrypt_send_ep11_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_ep11_cprb(&xcrb);
  785. } while (rc == -EAGAIN);
  786. if (rc)
  787. ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
  788. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  789. return -EFAULT;
  790. return rc;
  791. }
  792. case ZDEVICESTATUS: {
  793. struct zcrypt_device_matrix *device_status;
  794. device_status = kzalloc(sizeof(struct zcrypt_device_matrix),
  795. GFP_KERNEL);
  796. if (!device_status)
  797. return -ENOMEM;
  798. zcrypt_device_status_mask(device_status);
  799. if (copy_to_user((char __user *) arg, device_status,
  800. sizeof(struct zcrypt_device_matrix))) {
  801. kfree(device_status);
  802. return -EFAULT;
  803. }
  804. kfree(device_status);
  805. return 0;
  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_card *zc;
  1146. struct zcrypt_queue *zq;
  1147. spin_lock(&zcrypt_list_lock);
  1148. for_each_zcrypt_card(zc) {
  1149. for_each_zcrypt_queue(zq, zc) {
  1150. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1151. continue;
  1152. zq->online = 0;
  1153. ap_flush_queue(zq->queue);
  1154. }
  1155. }
  1156. spin_unlock(&zcrypt_list_lock);
  1157. }
  1158. static void zcrypt_enable_card(int index)
  1159. {
  1160. struct zcrypt_card *zc;
  1161. struct zcrypt_queue *zq;
  1162. spin_lock(&zcrypt_list_lock);
  1163. for_each_zcrypt_card(zc) {
  1164. for_each_zcrypt_queue(zq, zc) {
  1165. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1166. continue;
  1167. zq->online = 1;
  1168. ap_flush_queue(zq->queue);
  1169. }
  1170. }
  1171. spin_unlock(&zcrypt_list_lock);
  1172. }
  1173. static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
  1174. size_t count, loff_t *pos)
  1175. {
  1176. unsigned char *lbuf, *ptr;
  1177. size_t local_count;
  1178. int j;
  1179. if (count <= 0)
  1180. return 0;
  1181. #define LBUFSIZE 1200UL
  1182. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  1183. if (!lbuf)
  1184. return 0;
  1185. local_count = min(LBUFSIZE - 1, count);
  1186. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  1187. kfree(lbuf);
  1188. return -EFAULT;
  1189. }
  1190. lbuf[local_count] = '\0';
  1191. ptr = strstr(lbuf, "Online devices");
  1192. if (!ptr)
  1193. goto out;
  1194. ptr = strstr(ptr, "\n");
  1195. if (!ptr)
  1196. goto out;
  1197. ptr++;
  1198. if (strstr(ptr, "Waiting work element counts") == NULL)
  1199. goto out;
  1200. for (j = 0; j < 64 && *ptr; ptr++) {
  1201. /*
  1202. * '0' for no device, '1' for PCICA, '2' for PCICC,
  1203. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  1204. * '5' for CEX2C and '6' for CEX2A'
  1205. * '7' for CEX3C and '8' for CEX3A
  1206. */
  1207. if (*ptr >= '0' && *ptr <= '8')
  1208. j++;
  1209. else if (*ptr == 'd' || *ptr == 'D')
  1210. zcrypt_disable_card(j++);
  1211. else if (*ptr == 'e' || *ptr == 'E')
  1212. zcrypt_enable_card(j++);
  1213. else if (*ptr != ' ' && *ptr != '\t')
  1214. break;
  1215. }
  1216. out:
  1217. kfree(lbuf);
  1218. return count;
  1219. }
  1220. static const struct file_operations zcrypt_proc_fops = {
  1221. .owner = THIS_MODULE,
  1222. .open = zcrypt_proc_open,
  1223. .read = seq_read,
  1224. .llseek = seq_lseek,
  1225. .release = single_release,
  1226. .write = zcrypt_proc_write,
  1227. };
  1228. static int zcrypt_rng_device_count;
  1229. static u32 *zcrypt_rng_buffer;
  1230. static int zcrypt_rng_buffer_index;
  1231. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1232. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1233. {
  1234. int rc;
  1235. /*
  1236. * We don't need locking here because the RNG API guarantees serialized
  1237. * read method calls.
  1238. */
  1239. if (zcrypt_rng_buffer_index == 0) {
  1240. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1241. /* on failure: retry once again after a requested rescan */
  1242. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1243. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1244. if (rc < 0)
  1245. return -EIO;
  1246. zcrypt_rng_buffer_index = rc / sizeof *data;
  1247. }
  1248. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1249. return sizeof *data;
  1250. }
  1251. static struct hwrng zcrypt_rng_dev = {
  1252. .name = "zcrypt",
  1253. .data_read = zcrypt_rng_data_read,
  1254. .quality = 990,
  1255. };
  1256. int zcrypt_rng_device_add(void)
  1257. {
  1258. int rc = 0;
  1259. mutex_lock(&zcrypt_rng_mutex);
  1260. if (zcrypt_rng_device_count == 0) {
  1261. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1262. if (!zcrypt_rng_buffer) {
  1263. rc = -ENOMEM;
  1264. goto out;
  1265. }
  1266. zcrypt_rng_buffer_index = 0;
  1267. if (!zcrypt_hwrng_seed)
  1268. zcrypt_rng_dev.quality = 0;
  1269. rc = hwrng_register(&zcrypt_rng_dev);
  1270. if (rc)
  1271. goto out_free;
  1272. zcrypt_rng_device_count = 1;
  1273. } else
  1274. zcrypt_rng_device_count++;
  1275. mutex_unlock(&zcrypt_rng_mutex);
  1276. return 0;
  1277. out_free:
  1278. free_page((unsigned long) zcrypt_rng_buffer);
  1279. out:
  1280. mutex_unlock(&zcrypt_rng_mutex);
  1281. return rc;
  1282. }
  1283. void zcrypt_rng_device_remove(void)
  1284. {
  1285. mutex_lock(&zcrypt_rng_mutex);
  1286. zcrypt_rng_device_count--;
  1287. if (zcrypt_rng_device_count == 0) {
  1288. hwrng_unregister(&zcrypt_rng_dev);
  1289. free_page((unsigned long) zcrypt_rng_buffer);
  1290. }
  1291. mutex_unlock(&zcrypt_rng_mutex);
  1292. }
  1293. int __init zcrypt_debug_init(void)
  1294. {
  1295. zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
  1296. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1297. debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
  1298. debug_set_level(zcrypt_dbf_info, DBF_ERR);
  1299. return 0;
  1300. }
  1301. void zcrypt_debug_exit(void)
  1302. {
  1303. debug_unregister(zcrypt_dbf_info);
  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,
  1323. &zcrypt_proc_fops);
  1324. if (!zcrypt_entry) {
  1325. rc = -ENOMEM;
  1326. goto out_misc;
  1327. }
  1328. zcrypt_msgtype6_init();
  1329. zcrypt_msgtype50_init();
  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 __exit zcrypt_api_exit(void)
  1342. {
  1343. remove_proc_entry("driver/z90crypt", NULL);
  1344. misc_deregister(&zcrypt_misc_device);
  1345. zcrypt_msgtype6_exit();
  1346. zcrypt_msgtype50_exit();
  1347. zcrypt_debug_exit();
  1348. }
  1349. module_init(zcrypt_api_init);
  1350. module_exit(zcrypt_api_exit);