zcrypt_api.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. static struct dentry *zcrypt_dbf_root;
  73. debug_info_t *zcrypt_dbf_info;
  74. /**
  75. * Process a rescan of the transport layer.
  76. *
  77. * Returns 1, if the rescan has been processed, otherwise 0.
  78. */
  79. static inline int zcrypt_process_rescan(void)
  80. {
  81. if (atomic_read(&zcrypt_rescan_req)) {
  82. atomic_set(&zcrypt_rescan_req, 0);
  83. atomic_inc(&zcrypt_rescan_count);
  84. ap_bus_force_rescan();
  85. ZCRYPT_DBF(DBF_INFO, "rescan count=%07d",
  86. atomic_inc_return(&zcrypt_rescan_count));
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. void zcrypt_msgtype_register(struct zcrypt_ops *zops)
  92. {
  93. list_add_tail(&zops->list, &zcrypt_ops_list);
  94. }
  95. void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  96. {
  97. list_del_init(&zops->list);
  98. }
  99. struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
  100. {
  101. struct zcrypt_ops *zops;
  102. list_for_each_entry(zops, &zcrypt_ops_list, list)
  103. if ((zops->variant == variant) &&
  104. (!strncmp(zops->name, name, sizeof(zops->name))))
  105. return zops;
  106. return NULL;
  107. }
  108. EXPORT_SYMBOL(zcrypt_msgtype);
  109. /**
  110. * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
  111. *
  112. * This function is not supported beyond zcrypt 1.3.1.
  113. */
  114. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  115. size_t count, loff_t *f_pos)
  116. {
  117. return -EPERM;
  118. }
  119. /**
  120. * zcrypt_write(): Not allowed.
  121. *
  122. * Write is is not allowed
  123. */
  124. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  125. size_t count, loff_t *f_pos)
  126. {
  127. return -EPERM;
  128. }
  129. /**
  130. * zcrypt_open(): Count number of users.
  131. *
  132. * Device open function to count number of users.
  133. */
  134. static int zcrypt_open(struct inode *inode, struct file *filp)
  135. {
  136. atomic_inc(&zcrypt_open_count);
  137. return nonseekable_open(inode, filp);
  138. }
  139. /**
  140. * zcrypt_release(): Count number of users.
  141. *
  142. * Device close function to count number of users.
  143. */
  144. static int zcrypt_release(struct inode *inode, struct file *filp)
  145. {
  146. atomic_dec(&zcrypt_open_count);
  147. return 0;
  148. }
  149. static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
  150. struct zcrypt_queue *zq,
  151. unsigned int weight)
  152. {
  153. if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
  154. return NULL;
  155. zcrypt_queue_get(zq);
  156. get_device(&zq->queue->ap_dev.device);
  157. atomic_add(weight, &zc->load);
  158. atomic_add(weight, &zq->load);
  159. zq->request_count++;
  160. return zq;
  161. }
  162. static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
  163. struct zcrypt_queue *zq,
  164. unsigned int weight)
  165. {
  166. struct module *mod = zq->queue->ap_dev.drv->driver.owner;
  167. zq->request_count--;
  168. atomic_sub(weight, &zc->load);
  169. atomic_sub(weight, &zq->load);
  170. put_device(&zq->queue->ap_dev.device);
  171. zcrypt_queue_put(zq);
  172. module_put(mod);
  173. }
  174. static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
  175. struct zcrypt_card *pref_zc,
  176. unsigned weight, unsigned pref_weight)
  177. {
  178. if (!pref_zc)
  179. return 0;
  180. weight += atomic_read(&zc->load);
  181. pref_weight += atomic_read(&pref_zc->load);
  182. if (weight == pref_weight)
  183. return atomic_read(&zc->card->total_request_count) >
  184. atomic_read(&pref_zc->card->total_request_count);
  185. return weight > pref_weight;
  186. }
  187. static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
  188. struct zcrypt_queue *pref_zq,
  189. unsigned weight, unsigned pref_weight)
  190. {
  191. if (!pref_zq)
  192. return 0;
  193. weight += atomic_read(&zq->load);
  194. pref_weight += atomic_read(&pref_zq->load);
  195. if (weight == pref_weight)
  196. return &zq->queue->total_request_count >
  197. &pref_zq->queue->total_request_count;
  198. return weight > pref_weight;
  199. }
  200. /*
  201. * zcrypt ioctls.
  202. */
  203. static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
  204. {
  205. struct zcrypt_card *zc, *pref_zc;
  206. struct zcrypt_queue *zq, *pref_zq;
  207. unsigned int weight, pref_weight;
  208. unsigned int func_code;
  209. int qid = 0, rc = -ENODEV;
  210. trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
  211. if (mex->outputdatalength < mex->inputdatalength) {
  212. rc = -EINVAL;
  213. goto out;
  214. }
  215. /*
  216. * As long as outputdatalength is big enough, we can set the
  217. * outputdatalength equal to the inputdatalength, since that is the
  218. * number of bytes we will copy in any case
  219. */
  220. mex->outputdatalength = mex->inputdatalength;
  221. rc = get_rsa_modex_fc(mex, &func_code);
  222. if (rc)
  223. goto out;
  224. pref_zc = NULL;
  225. pref_zq = NULL;
  226. spin_lock(&zcrypt_list_lock);
  227. for_each_zcrypt_card(zc) {
  228. /* Check for online accelarator and CCA cards */
  229. if (!zc->online || !(zc->card->functions & 0x18000000))
  230. continue;
  231. /* Check for size limits */
  232. if (zc->min_mod_size > mex->inputdatalength ||
  233. zc->max_mod_size < mex->inputdatalength)
  234. continue;
  235. /* get weight index of the card device */
  236. weight = zc->speed_rating[func_code];
  237. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  238. continue;
  239. for_each_zcrypt_queue(zq, zc) {
  240. /* check if device is online and eligible */
  241. if (!zq->online || !zq->ops->rsa_modexpo)
  242. continue;
  243. if (zcrypt_queue_compare(zq, pref_zq,
  244. weight, pref_weight))
  245. continue;
  246. pref_zc = zc;
  247. pref_zq = zq;
  248. pref_weight = weight;
  249. }
  250. }
  251. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  252. spin_unlock(&zcrypt_list_lock);
  253. if (!pref_zq) {
  254. rc = -ENODEV;
  255. goto out;
  256. }
  257. qid = pref_zq->queue->qid;
  258. rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
  259. spin_lock(&zcrypt_list_lock);
  260. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  261. spin_unlock(&zcrypt_list_lock);
  262. out:
  263. trace_s390_zcrypt_rep(mex, func_code, rc,
  264. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  265. return rc;
  266. }
  267. static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
  268. {
  269. struct zcrypt_card *zc, *pref_zc;
  270. struct zcrypt_queue *zq, *pref_zq;
  271. unsigned int weight, pref_weight;
  272. unsigned int func_code;
  273. int qid = 0, rc = -ENODEV;
  274. trace_s390_zcrypt_req(crt, TP_ICARSACRT);
  275. if (crt->outputdatalength < crt->inputdatalength) {
  276. rc = -EINVAL;
  277. goto out;
  278. }
  279. /*
  280. * As long as outputdatalength is big enough, we can set the
  281. * outputdatalength equal to the inputdatalength, since that is the
  282. * number of bytes we will copy in any case
  283. */
  284. crt->outputdatalength = crt->inputdatalength;
  285. rc = get_rsa_crt_fc(crt, &func_code);
  286. if (rc)
  287. goto out;
  288. pref_zc = NULL;
  289. pref_zq = NULL;
  290. spin_lock(&zcrypt_list_lock);
  291. for_each_zcrypt_card(zc) {
  292. /* Check for online accelarator and CCA cards */
  293. if (!zc->online || !(zc->card->functions & 0x18000000))
  294. continue;
  295. /* Check for size limits */
  296. if (zc->min_mod_size > crt->inputdatalength ||
  297. zc->max_mod_size < crt->inputdatalength)
  298. continue;
  299. /* get weight index of the card device */
  300. weight = zc->speed_rating[func_code];
  301. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  302. continue;
  303. for_each_zcrypt_queue(zq, zc) {
  304. /* check if device is online and eligible */
  305. if (!zq->online || !zq->ops->rsa_modexpo_crt)
  306. continue;
  307. if (zcrypt_queue_compare(zq, pref_zq,
  308. weight, pref_weight))
  309. continue;
  310. pref_zc = zc;
  311. pref_zq = zq;
  312. pref_weight = weight;
  313. }
  314. }
  315. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  316. spin_unlock(&zcrypt_list_lock);
  317. if (!pref_zq) {
  318. rc = -ENODEV;
  319. goto out;
  320. }
  321. qid = pref_zq->queue->qid;
  322. rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
  323. spin_lock(&zcrypt_list_lock);
  324. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  325. spin_unlock(&zcrypt_list_lock);
  326. out:
  327. trace_s390_zcrypt_rep(crt, func_code, rc,
  328. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  329. return rc;
  330. }
  331. static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  332. {
  333. struct zcrypt_card *zc, *pref_zc;
  334. struct zcrypt_queue *zq, *pref_zq;
  335. struct ap_message ap_msg;
  336. unsigned int weight, pref_weight;
  337. unsigned int func_code;
  338. unsigned short *domain;
  339. int qid = 0, rc = -ENODEV;
  340. trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
  341. rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
  342. if (rc)
  343. goto out;
  344. pref_zc = NULL;
  345. pref_zq = NULL;
  346. spin_lock(&zcrypt_list_lock);
  347. for_each_zcrypt_card(zc) {
  348. /* Check for online CCA cards */
  349. if (!zc->online || !(zc->card->functions & 0x10000000))
  350. continue;
  351. /* Check for user selected CCA card */
  352. if (xcRB->user_defined != AUTOSELECT &&
  353. xcRB->user_defined != zc->card->id)
  354. continue;
  355. /* get weight index of the card device */
  356. weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
  357. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  358. continue;
  359. for_each_zcrypt_queue(zq, zc) {
  360. /* check if device is online and eligible */
  361. if (!zq->online ||
  362. !zq->ops->send_cprb ||
  363. ((*domain != (unsigned short) AUTOSELECT) &&
  364. (*domain != AP_QID_QUEUE(zq->queue->qid))))
  365. continue;
  366. if (zcrypt_queue_compare(zq, pref_zq,
  367. weight, pref_weight))
  368. continue;
  369. pref_zc = zc;
  370. pref_zq = zq;
  371. pref_weight = weight;
  372. }
  373. }
  374. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  375. spin_unlock(&zcrypt_list_lock);
  376. if (!pref_zq) {
  377. rc = -ENODEV;
  378. goto out;
  379. }
  380. /* in case of auto select, provide the correct domain */
  381. qid = pref_zq->queue->qid;
  382. if (*domain == (unsigned short) AUTOSELECT)
  383. *domain = AP_QID_QUEUE(qid);
  384. rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
  385. spin_lock(&zcrypt_list_lock);
  386. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  387. spin_unlock(&zcrypt_list_lock);
  388. out:
  389. trace_s390_zcrypt_rep(xcRB, func_code, rc,
  390. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  391. return rc;
  392. }
  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. static 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. for_each_zcrypt_card(zc) {
  589. for_each_zcrypt_queue(zq, zc) {
  590. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  591. continue;
  592. spin_lock(&zq->queue->lock);
  593. qdepth[AP_QID_CARD(zq->queue->qid)] =
  594. zq->queue->pendingq_count +
  595. zq->queue->requestq_count;
  596. spin_unlock(&zq->queue->lock);
  597. }
  598. }
  599. spin_unlock(&zcrypt_list_lock);
  600. }
  601. static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
  602. {
  603. struct zcrypt_card *zc;
  604. struct zcrypt_queue *zq;
  605. memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
  606. spin_lock(&zcrypt_list_lock);
  607. for_each_zcrypt_card(zc) {
  608. for_each_zcrypt_queue(zq, zc) {
  609. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  610. continue;
  611. spin_lock(&zq->queue->lock);
  612. reqcnt[AP_QID_CARD(zq->queue->qid)] =
  613. zq->queue->total_request_count;
  614. spin_unlock(&zq->queue->lock);
  615. }
  616. }
  617. spin_unlock(&zcrypt_list_lock);
  618. }
  619. static int zcrypt_pendingq_count(void)
  620. {
  621. struct zcrypt_card *zc;
  622. struct zcrypt_queue *zq;
  623. int pendingq_count;
  624. pendingq_count = 0;
  625. spin_lock(&zcrypt_list_lock);
  626. for_each_zcrypt_card(zc) {
  627. for_each_zcrypt_queue(zq, zc) {
  628. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  629. continue;
  630. spin_lock(&zq->queue->lock);
  631. pendingq_count += zq->queue->pendingq_count;
  632. spin_unlock(&zq->queue->lock);
  633. }
  634. }
  635. spin_unlock(&zcrypt_list_lock);
  636. return pendingq_count;
  637. }
  638. static int zcrypt_requestq_count(void)
  639. {
  640. struct zcrypt_card *zc;
  641. struct zcrypt_queue *zq;
  642. int requestq_count;
  643. requestq_count = 0;
  644. spin_lock(&zcrypt_list_lock);
  645. for_each_zcrypt_card(zc) {
  646. for_each_zcrypt_queue(zq, zc) {
  647. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  648. continue;
  649. spin_lock(&zq->queue->lock);
  650. requestq_count += zq->queue->requestq_count;
  651. spin_unlock(&zq->queue->lock);
  652. }
  653. }
  654. spin_unlock(&zcrypt_list_lock);
  655. return requestq_count;
  656. }
  657. static int zcrypt_count_type(int type)
  658. {
  659. struct zcrypt_card *zc;
  660. struct zcrypt_queue *zq;
  661. int device_count;
  662. device_count = 0;
  663. spin_lock(&zcrypt_list_lock);
  664. for_each_zcrypt_card(zc) {
  665. if (zc->card->id != type)
  666. continue;
  667. for_each_zcrypt_queue(zq, zc) {
  668. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  669. continue;
  670. device_count++;
  671. }
  672. }
  673. spin_unlock(&zcrypt_list_lock);
  674. return device_count;
  675. }
  676. /**
  677. * zcrypt_ica_status(): Old, depracted combi status call.
  678. *
  679. * Old, deprecated combi status call.
  680. */
  681. static long zcrypt_ica_status(struct file *filp, unsigned long arg)
  682. {
  683. struct ica_z90_status *pstat;
  684. int ret;
  685. pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
  686. if (!pstat)
  687. return -ENOMEM;
  688. pstat->totalcount = zcrypt_device_count;
  689. pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
  690. pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
  691. pstat->requestqWaitCount = zcrypt_requestq_count();
  692. pstat->pendingqWaitCount = zcrypt_pendingq_count();
  693. pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
  694. pstat->cryptoDomain = ap_domain_index;
  695. zcrypt_status_mask(pstat->status);
  696. zcrypt_qdepth_mask(pstat->qdepth);
  697. ret = 0;
  698. if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
  699. ret = -EFAULT;
  700. kfree(pstat);
  701. return ret;
  702. }
  703. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  704. unsigned long arg)
  705. {
  706. int rc;
  707. switch (cmd) {
  708. case ICARSAMODEXPO: {
  709. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  710. struct ica_rsa_modexpo mex;
  711. if (copy_from_user(&mex, umex, sizeof(mex)))
  712. return -EFAULT;
  713. do {
  714. rc = zcrypt_rsa_modexpo(&mex);
  715. } while (rc == -EAGAIN);
  716. /* on failure: retry once again after a requested rescan */
  717. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  718. do {
  719. rc = zcrypt_rsa_modexpo(&mex);
  720. } while (rc == -EAGAIN);
  721. if (rc)
  722. return rc;
  723. return put_user(mex.outputdatalength, &umex->outputdatalength);
  724. }
  725. case ICARSACRT: {
  726. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  727. struct ica_rsa_modexpo_crt crt;
  728. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  729. return -EFAULT;
  730. do {
  731. rc = zcrypt_rsa_crt(&crt);
  732. } while (rc == -EAGAIN);
  733. /* on failure: retry once again after a requested rescan */
  734. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  735. do {
  736. rc = zcrypt_rsa_crt(&crt);
  737. } while (rc == -EAGAIN);
  738. if (rc)
  739. return rc;
  740. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  741. }
  742. case ZSECSENDCPRB: {
  743. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  744. struct ica_xcRB xcRB;
  745. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  746. return -EFAULT;
  747. do {
  748. rc = zcrypt_send_cprb(&xcRB);
  749. } while (rc == -EAGAIN);
  750. /* on failure: retry once again after a requested rescan */
  751. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  752. do {
  753. rc = zcrypt_send_cprb(&xcRB);
  754. } while (rc == -EAGAIN);
  755. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  756. return -EFAULT;
  757. return rc;
  758. }
  759. case ZSENDEP11CPRB: {
  760. struct ep11_urb __user *uxcrb = (void __user *)arg;
  761. struct ep11_urb xcrb;
  762. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  763. return -EFAULT;
  764. do {
  765. rc = zcrypt_send_ep11_cprb(&xcrb);
  766. } while (rc == -EAGAIN);
  767. /* on failure: retry once again after a requested rescan */
  768. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  769. do {
  770. rc = zcrypt_send_ep11_cprb(&xcrb);
  771. } while (rc == -EAGAIN);
  772. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  773. return -EFAULT;
  774. return rc;
  775. }
  776. case ZDEVICESTATUS: {
  777. struct zcrypt_device_matrix *device_status;
  778. device_status = kzalloc(sizeof(struct zcrypt_device_matrix),
  779. GFP_KERNEL);
  780. if (!device_status)
  781. return -ENOMEM;
  782. zcrypt_device_status_mask(device_status);
  783. if (copy_to_user((char __user *) arg, device_status,
  784. sizeof(struct zcrypt_device_matrix))) {
  785. kfree(device_status);
  786. return -EFAULT;
  787. }
  788. kfree(device_status);
  789. return 0;
  790. }
  791. case Z90STAT_STATUS_MASK: {
  792. char status[AP_DEVICES];
  793. zcrypt_status_mask(status);
  794. if (copy_to_user((char __user *) arg, status,
  795. sizeof(char) * AP_DEVICES))
  796. return -EFAULT;
  797. return 0;
  798. }
  799. case Z90STAT_QDEPTH_MASK: {
  800. char qdepth[AP_DEVICES];
  801. zcrypt_qdepth_mask(qdepth);
  802. if (copy_to_user((char __user *) arg, qdepth,
  803. sizeof(char) * AP_DEVICES))
  804. return -EFAULT;
  805. return 0;
  806. }
  807. case Z90STAT_PERDEV_REQCNT: {
  808. int reqcnt[AP_DEVICES];
  809. zcrypt_perdev_reqcnt(reqcnt);
  810. if (copy_to_user((int __user *) arg, reqcnt,
  811. sizeof(int) * AP_DEVICES))
  812. return -EFAULT;
  813. return 0;
  814. }
  815. case Z90STAT_REQUESTQ_COUNT:
  816. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  817. case Z90STAT_PENDINGQ_COUNT:
  818. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  819. case Z90STAT_TOTALOPEN_COUNT:
  820. return put_user(atomic_read(&zcrypt_open_count),
  821. (int __user *) arg);
  822. case Z90STAT_DOMAIN_INDEX:
  823. return put_user(ap_domain_index, (int __user *) arg);
  824. /*
  825. * Deprecated ioctls. Don't add another device count ioctl,
  826. * you can count them yourself in the user space with the
  827. * output of the Z90STAT_STATUS_MASK ioctl.
  828. */
  829. case ICAZ90STATUS:
  830. return zcrypt_ica_status(filp, arg);
  831. case Z90STAT_TOTALCOUNT:
  832. return put_user(zcrypt_device_count, (int __user *) arg);
  833. case Z90STAT_PCICACOUNT:
  834. return put_user(zcrypt_count_type(ZCRYPT_PCICA),
  835. (int __user *) arg);
  836. case Z90STAT_PCICCCOUNT:
  837. return put_user(zcrypt_count_type(ZCRYPT_PCICC),
  838. (int __user *) arg);
  839. case Z90STAT_PCIXCCMCL2COUNT:
  840. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
  841. (int __user *) arg);
  842. case Z90STAT_PCIXCCMCL3COUNT:
  843. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  844. (int __user *) arg);
  845. case Z90STAT_PCIXCCCOUNT:
  846. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
  847. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  848. (int __user *) arg);
  849. case Z90STAT_CEX2CCOUNT:
  850. return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
  851. (int __user *) arg);
  852. case Z90STAT_CEX2ACOUNT:
  853. return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
  854. (int __user *) arg);
  855. default:
  856. /* unknown ioctl number */
  857. return -ENOIOCTLCMD;
  858. }
  859. }
  860. #ifdef CONFIG_COMPAT
  861. /*
  862. * ioctl32 conversion routines
  863. */
  864. struct compat_ica_rsa_modexpo {
  865. compat_uptr_t inputdata;
  866. unsigned int inputdatalength;
  867. compat_uptr_t outputdata;
  868. unsigned int outputdatalength;
  869. compat_uptr_t b_key;
  870. compat_uptr_t n_modulus;
  871. };
  872. static long trans_modexpo32(struct file *filp, unsigned int cmd,
  873. unsigned long arg)
  874. {
  875. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  876. struct compat_ica_rsa_modexpo mex32;
  877. struct ica_rsa_modexpo mex64;
  878. long rc;
  879. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  880. return -EFAULT;
  881. mex64.inputdata = compat_ptr(mex32.inputdata);
  882. mex64.inputdatalength = mex32.inputdatalength;
  883. mex64.outputdata = compat_ptr(mex32.outputdata);
  884. mex64.outputdatalength = mex32.outputdatalength;
  885. mex64.b_key = compat_ptr(mex32.b_key);
  886. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  887. do {
  888. rc = zcrypt_rsa_modexpo(&mex64);
  889. } while (rc == -EAGAIN);
  890. /* on failure: retry once again after a requested rescan */
  891. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  892. do {
  893. rc = zcrypt_rsa_modexpo(&mex64);
  894. } while (rc == -EAGAIN);
  895. if (rc)
  896. return rc;
  897. return put_user(mex64.outputdatalength,
  898. &umex32->outputdatalength);
  899. }
  900. struct compat_ica_rsa_modexpo_crt {
  901. compat_uptr_t inputdata;
  902. unsigned int inputdatalength;
  903. compat_uptr_t outputdata;
  904. unsigned int outputdatalength;
  905. compat_uptr_t bp_key;
  906. compat_uptr_t bq_key;
  907. compat_uptr_t np_prime;
  908. compat_uptr_t nq_prime;
  909. compat_uptr_t u_mult_inv;
  910. };
  911. static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
  912. unsigned long arg)
  913. {
  914. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  915. struct compat_ica_rsa_modexpo_crt crt32;
  916. struct ica_rsa_modexpo_crt crt64;
  917. long rc;
  918. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  919. return -EFAULT;
  920. crt64.inputdata = compat_ptr(crt32.inputdata);
  921. crt64.inputdatalength = crt32.inputdatalength;
  922. crt64.outputdata= compat_ptr(crt32.outputdata);
  923. crt64.outputdatalength = crt32.outputdatalength;
  924. crt64.bp_key = compat_ptr(crt32.bp_key);
  925. crt64.bq_key = compat_ptr(crt32.bq_key);
  926. crt64.np_prime = compat_ptr(crt32.np_prime);
  927. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  928. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  929. do {
  930. rc = zcrypt_rsa_crt(&crt64);
  931. } while (rc == -EAGAIN);
  932. /* on failure: retry once again after a requested rescan */
  933. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  934. do {
  935. rc = zcrypt_rsa_crt(&crt64);
  936. } while (rc == -EAGAIN);
  937. if (rc)
  938. return rc;
  939. return put_user(crt64.outputdatalength,
  940. &ucrt32->outputdatalength);
  941. }
  942. struct compat_ica_xcRB {
  943. unsigned short agent_ID;
  944. unsigned int user_defined;
  945. unsigned short request_ID;
  946. unsigned int request_control_blk_length;
  947. unsigned char padding1[16 - sizeof (compat_uptr_t)];
  948. compat_uptr_t request_control_blk_addr;
  949. unsigned int request_data_length;
  950. char padding2[16 - sizeof (compat_uptr_t)];
  951. compat_uptr_t request_data_address;
  952. unsigned int reply_control_blk_length;
  953. char padding3[16 - sizeof (compat_uptr_t)];
  954. compat_uptr_t reply_control_blk_addr;
  955. unsigned int reply_data_length;
  956. char padding4[16 - sizeof (compat_uptr_t)];
  957. compat_uptr_t reply_data_addr;
  958. unsigned short priority_window;
  959. unsigned int status;
  960. } __attribute__((packed));
  961. static long trans_xcRB32(struct file *filp, unsigned int cmd,
  962. unsigned long arg)
  963. {
  964. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  965. struct compat_ica_xcRB xcRB32;
  966. struct ica_xcRB xcRB64;
  967. long rc;
  968. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  969. return -EFAULT;
  970. xcRB64.agent_ID = xcRB32.agent_ID;
  971. xcRB64.user_defined = xcRB32.user_defined;
  972. xcRB64.request_ID = xcRB32.request_ID;
  973. xcRB64.request_control_blk_length =
  974. xcRB32.request_control_blk_length;
  975. xcRB64.request_control_blk_addr =
  976. compat_ptr(xcRB32.request_control_blk_addr);
  977. xcRB64.request_data_length =
  978. xcRB32.request_data_length;
  979. xcRB64.request_data_address =
  980. compat_ptr(xcRB32.request_data_address);
  981. xcRB64.reply_control_blk_length =
  982. xcRB32.reply_control_blk_length;
  983. xcRB64.reply_control_blk_addr =
  984. compat_ptr(xcRB32.reply_control_blk_addr);
  985. xcRB64.reply_data_length = xcRB32.reply_data_length;
  986. xcRB64.reply_data_addr =
  987. compat_ptr(xcRB32.reply_data_addr);
  988. xcRB64.priority_window = xcRB32.priority_window;
  989. xcRB64.status = xcRB32.status;
  990. do {
  991. rc = zcrypt_send_cprb(&xcRB64);
  992. } while (rc == -EAGAIN);
  993. /* on failure: retry once again after a requested rescan */
  994. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  995. do {
  996. rc = zcrypt_send_cprb(&xcRB64);
  997. } while (rc == -EAGAIN);
  998. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  999. xcRB32.reply_data_length = xcRB64.reply_data_length;
  1000. xcRB32.status = xcRB64.status;
  1001. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  1002. return -EFAULT;
  1003. return rc;
  1004. }
  1005. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  1006. unsigned long arg)
  1007. {
  1008. if (cmd == ICARSAMODEXPO)
  1009. return trans_modexpo32(filp, cmd, arg);
  1010. if (cmd == ICARSACRT)
  1011. return trans_modexpo_crt32(filp, cmd, arg);
  1012. if (cmd == ZSECSENDCPRB)
  1013. return trans_xcRB32(filp, cmd, arg);
  1014. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  1015. }
  1016. #endif
  1017. /*
  1018. * Misc device file operations.
  1019. */
  1020. static const struct file_operations zcrypt_fops = {
  1021. .owner = THIS_MODULE,
  1022. .read = zcrypt_read,
  1023. .write = zcrypt_write,
  1024. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  1025. #ifdef CONFIG_COMPAT
  1026. .compat_ioctl = zcrypt_compat_ioctl,
  1027. #endif
  1028. .open = zcrypt_open,
  1029. .release = zcrypt_release,
  1030. .llseek = no_llseek,
  1031. };
  1032. /*
  1033. * Misc device.
  1034. */
  1035. static struct miscdevice zcrypt_misc_device = {
  1036. .minor = MISC_DYNAMIC_MINOR,
  1037. .name = "z90crypt",
  1038. .fops = &zcrypt_fops,
  1039. };
  1040. /*
  1041. * Deprecated /proc entry support.
  1042. */
  1043. static struct proc_dir_entry *zcrypt_entry;
  1044. static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
  1045. {
  1046. int i;
  1047. for (i = 0; i < len; i++)
  1048. seq_printf(m, "%01x", (unsigned int) addr[i]);
  1049. seq_putc(m, ' ');
  1050. }
  1051. static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
  1052. {
  1053. int inl, c, cx;
  1054. seq_printf(m, " ");
  1055. inl = 0;
  1056. for (c = 0; c < (len / 16); c++) {
  1057. sprintcl(m, addr+inl, 16);
  1058. inl += 16;
  1059. }
  1060. cx = len%16;
  1061. if (cx) {
  1062. sprintcl(m, addr+inl, cx);
  1063. inl += cx;
  1064. }
  1065. seq_putc(m, '\n');
  1066. }
  1067. static void sprinthx(unsigned char *title, struct seq_file *m,
  1068. unsigned char *addr, unsigned int len)
  1069. {
  1070. int inl, r, rx;
  1071. seq_printf(m, "\n%s\n", title);
  1072. inl = 0;
  1073. for (r = 0; r < (len / 64); r++) {
  1074. sprintrw(m, addr+inl, 64);
  1075. inl += 64;
  1076. }
  1077. rx = len % 64;
  1078. if (rx) {
  1079. sprintrw(m, addr+inl, rx);
  1080. inl += rx;
  1081. }
  1082. seq_putc(m, '\n');
  1083. }
  1084. static void sprinthx4(unsigned char *title, struct seq_file *m,
  1085. unsigned int *array, unsigned int len)
  1086. {
  1087. seq_printf(m, "\n%s\n", title);
  1088. seq_hex_dump(m, " ", DUMP_PREFIX_NONE, 32, 4, array, len, false);
  1089. seq_putc(m, '\n');
  1090. }
  1091. static int zcrypt_proc_show(struct seq_file *m, void *v)
  1092. {
  1093. char workarea[sizeof(int) * AP_DEVICES];
  1094. seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
  1095. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  1096. seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
  1097. seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
  1098. seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
  1099. seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
  1100. seq_printf(m, "PCIXCC MCL2 count: %d\n",
  1101. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  1102. seq_printf(m, "PCIXCC MCL3 count: %d\n",
  1103. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  1104. seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
  1105. seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
  1106. seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
  1107. seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
  1108. seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
  1109. seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
  1110. seq_printf(m, "Total open handles: %d\n\n",
  1111. atomic_read(&zcrypt_open_count));
  1112. zcrypt_status_mask(workarea);
  1113. sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  1114. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
  1115. m, workarea, AP_DEVICES);
  1116. zcrypt_qdepth_mask(workarea);
  1117. sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
  1118. zcrypt_perdev_reqcnt((int *) workarea);
  1119. sprinthx4("Per-device successfully completed request counts",
  1120. m, (unsigned int *) workarea, AP_DEVICES);
  1121. return 0;
  1122. }
  1123. static int zcrypt_proc_open(struct inode *inode, struct file *file)
  1124. {
  1125. return single_open(file, zcrypt_proc_show, NULL);
  1126. }
  1127. static void zcrypt_disable_card(int index)
  1128. {
  1129. struct zcrypt_card *zc;
  1130. struct zcrypt_queue *zq;
  1131. spin_lock(&zcrypt_list_lock);
  1132. for_each_zcrypt_card(zc) {
  1133. for_each_zcrypt_queue(zq, zc) {
  1134. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1135. continue;
  1136. zq->online = 0;
  1137. ap_flush_queue(zq->queue);
  1138. }
  1139. }
  1140. spin_unlock(&zcrypt_list_lock);
  1141. }
  1142. static void zcrypt_enable_card(int index)
  1143. {
  1144. struct zcrypt_card *zc;
  1145. struct zcrypt_queue *zq;
  1146. spin_lock(&zcrypt_list_lock);
  1147. for_each_zcrypt_card(zc) {
  1148. for_each_zcrypt_queue(zq, zc) {
  1149. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1150. continue;
  1151. zq->online = 1;
  1152. ap_flush_queue(zq->queue);
  1153. }
  1154. }
  1155. spin_unlock(&zcrypt_list_lock);
  1156. }
  1157. static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
  1158. size_t count, loff_t *pos)
  1159. {
  1160. unsigned char *lbuf, *ptr;
  1161. size_t local_count;
  1162. int j;
  1163. if (count <= 0)
  1164. return 0;
  1165. #define LBUFSIZE 1200UL
  1166. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  1167. if (!lbuf)
  1168. return 0;
  1169. local_count = min(LBUFSIZE - 1, count);
  1170. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  1171. kfree(lbuf);
  1172. return -EFAULT;
  1173. }
  1174. lbuf[local_count] = '\0';
  1175. ptr = strstr(lbuf, "Online devices");
  1176. if (!ptr)
  1177. goto out;
  1178. ptr = strstr(ptr, "\n");
  1179. if (!ptr)
  1180. goto out;
  1181. ptr++;
  1182. if (strstr(ptr, "Waiting work element counts") == NULL)
  1183. goto out;
  1184. for (j = 0; j < 64 && *ptr; ptr++) {
  1185. /*
  1186. * '0' for no device, '1' for PCICA, '2' for PCICC,
  1187. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  1188. * '5' for CEX2C and '6' for CEX2A'
  1189. * '7' for CEX3C and '8' for CEX3A
  1190. */
  1191. if (*ptr >= '0' && *ptr <= '8')
  1192. j++;
  1193. else if (*ptr == 'd' || *ptr == 'D')
  1194. zcrypt_disable_card(j++);
  1195. else if (*ptr == 'e' || *ptr == 'E')
  1196. zcrypt_enable_card(j++);
  1197. else if (*ptr != ' ' && *ptr != '\t')
  1198. break;
  1199. }
  1200. out:
  1201. kfree(lbuf);
  1202. return count;
  1203. }
  1204. static const struct file_operations zcrypt_proc_fops = {
  1205. .owner = THIS_MODULE,
  1206. .open = zcrypt_proc_open,
  1207. .read = seq_read,
  1208. .llseek = seq_lseek,
  1209. .release = single_release,
  1210. .write = zcrypt_proc_write,
  1211. };
  1212. static int zcrypt_rng_device_count;
  1213. static u32 *zcrypt_rng_buffer;
  1214. static int zcrypt_rng_buffer_index;
  1215. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1216. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1217. {
  1218. int rc;
  1219. /*
  1220. * We don't need locking here because the RNG API guarantees serialized
  1221. * read method calls.
  1222. */
  1223. if (zcrypt_rng_buffer_index == 0) {
  1224. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1225. /* on failure: retry once again after a requested rescan */
  1226. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1227. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1228. if (rc < 0)
  1229. return -EIO;
  1230. zcrypt_rng_buffer_index = rc / sizeof *data;
  1231. }
  1232. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1233. return sizeof *data;
  1234. }
  1235. static struct hwrng zcrypt_rng_dev = {
  1236. .name = "zcrypt",
  1237. .data_read = zcrypt_rng_data_read,
  1238. .quality = 990,
  1239. };
  1240. int zcrypt_rng_device_add(void)
  1241. {
  1242. int rc = 0;
  1243. mutex_lock(&zcrypt_rng_mutex);
  1244. if (zcrypt_rng_device_count == 0) {
  1245. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1246. if (!zcrypt_rng_buffer) {
  1247. rc = -ENOMEM;
  1248. goto out;
  1249. }
  1250. zcrypt_rng_buffer_index = 0;
  1251. if (!zcrypt_hwrng_seed)
  1252. zcrypt_rng_dev.quality = 0;
  1253. rc = hwrng_register(&zcrypt_rng_dev);
  1254. if (rc)
  1255. goto out_free;
  1256. zcrypt_rng_device_count = 1;
  1257. } else
  1258. zcrypt_rng_device_count++;
  1259. mutex_unlock(&zcrypt_rng_mutex);
  1260. return 0;
  1261. out_free:
  1262. free_page((unsigned long) zcrypt_rng_buffer);
  1263. out:
  1264. mutex_unlock(&zcrypt_rng_mutex);
  1265. return rc;
  1266. }
  1267. void zcrypt_rng_device_remove(void)
  1268. {
  1269. mutex_lock(&zcrypt_rng_mutex);
  1270. zcrypt_rng_device_count--;
  1271. if (zcrypt_rng_device_count == 0) {
  1272. hwrng_unregister(&zcrypt_rng_dev);
  1273. free_page((unsigned long) zcrypt_rng_buffer);
  1274. }
  1275. mutex_unlock(&zcrypt_rng_mutex);
  1276. }
  1277. int __init zcrypt_debug_init(void)
  1278. {
  1279. zcrypt_dbf_root = debugfs_create_dir("zcrypt", NULL);
  1280. zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
  1281. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1282. debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
  1283. debug_set_level(zcrypt_dbf_info, DBF_ERR);
  1284. return 0;
  1285. }
  1286. void zcrypt_debug_exit(void)
  1287. {
  1288. debugfs_remove(zcrypt_dbf_root);
  1289. debug_unregister(zcrypt_dbf_info);
  1290. }
  1291. /**
  1292. * zcrypt_api_init(): Module initialization.
  1293. *
  1294. * The module initialization code.
  1295. */
  1296. int __init zcrypt_api_init(void)
  1297. {
  1298. int rc;
  1299. rc = zcrypt_debug_init();
  1300. if (rc)
  1301. goto out;
  1302. atomic_set(&zcrypt_rescan_req, 0);
  1303. /* Register the request sprayer. */
  1304. rc = misc_register(&zcrypt_misc_device);
  1305. if (rc < 0)
  1306. goto out;
  1307. /* Set up the proc file system */
  1308. zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL,
  1309. &zcrypt_proc_fops);
  1310. if (!zcrypt_entry) {
  1311. rc = -ENOMEM;
  1312. goto out_misc;
  1313. }
  1314. zcrypt_msgtype6_init();
  1315. zcrypt_msgtype50_init();
  1316. return 0;
  1317. out_misc:
  1318. misc_deregister(&zcrypt_misc_device);
  1319. out:
  1320. return rc;
  1321. }
  1322. /**
  1323. * zcrypt_api_exit(): Module termination.
  1324. *
  1325. * The module termination code.
  1326. */
  1327. void __exit zcrypt_api_exit(void)
  1328. {
  1329. remove_proc_entry("driver/z90crypt", NULL);
  1330. misc_deregister(&zcrypt_misc_device);
  1331. zcrypt_msgtype6_exit();
  1332. zcrypt_msgtype50_exit();
  1333. zcrypt_debug_exit();
  1334. }
  1335. module_init(zcrypt_api_init);
  1336. module_exit(zcrypt_api_exit);