zcrypt_api.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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",
  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. return rc;
  731. return put_user(mex.outputdatalength, &umex->outputdatalength);
  732. }
  733. case ICARSACRT: {
  734. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  735. struct ica_rsa_modexpo_crt crt;
  736. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  737. return -EFAULT;
  738. do {
  739. rc = zcrypt_rsa_crt(&crt);
  740. } while (rc == -EAGAIN);
  741. /* on failure: retry once again after a requested rescan */
  742. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  743. do {
  744. rc = zcrypt_rsa_crt(&crt);
  745. } while (rc == -EAGAIN);
  746. if (rc)
  747. return rc;
  748. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  749. }
  750. case ZSECSENDCPRB: {
  751. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  752. struct ica_xcRB xcRB;
  753. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  754. return -EFAULT;
  755. do {
  756. rc = zcrypt_send_cprb(&xcRB);
  757. } while (rc == -EAGAIN);
  758. /* on failure: retry once again after a requested rescan */
  759. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  760. do {
  761. rc = zcrypt_send_cprb(&xcRB);
  762. } while (rc == -EAGAIN);
  763. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  764. return -EFAULT;
  765. return rc;
  766. }
  767. case ZSENDEP11CPRB: {
  768. struct ep11_urb __user *uxcrb = (void __user *)arg;
  769. struct ep11_urb xcrb;
  770. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  771. return -EFAULT;
  772. do {
  773. rc = zcrypt_send_ep11_cprb(&xcrb);
  774. } while (rc == -EAGAIN);
  775. /* on failure: retry once again after a requested rescan */
  776. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  777. do {
  778. rc = zcrypt_send_ep11_cprb(&xcrb);
  779. } while (rc == -EAGAIN);
  780. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  781. return -EFAULT;
  782. return rc;
  783. }
  784. case ZDEVICESTATUS: {
  785. struct zcrypt_device_matrix *device_status;
  786. device_status = kzalloc(sizeof(struct zcrypt_device_matrix),
  787. GFP_KERNEL);
  788. if (!device_status)
  789. return -ENOMEM;
  790. zcrypt_device_status_mask(device_status);
  791. if (copy_to_user((char __user *) arg, device_status,
  792. sizeof(struct zcrypt_device_matrix))) {
  793. kfree(device_status);
  794. return -EFAULT;
  795. }
  796. kfree(device_status);
  797. return 0;
  798. }
  799. case Z90STAT_STATUS_MASK: {
  800. char status[AP_DEVICES];
  801. zcrypt_status_mask(status);
  802. if (copy_to_user((char __user *) arg, status,
  803. sizeof(char) * AP_DEVICES))
  804. return -EFAULT;
  805. return 0;
  806. }
  807. case Z90STAT_QDEPTH_MASK: {
  808. char qdepth[AP_DEVICES];
  809. zcrypt_qdepth_mask(qdepth);
  810. if (copy_to_user((char __user *) arg, qdepth,
  811. sizeof(char) * AP_DEVICES))
  812. return -EFAULT;
  813. return 0;
  814. }
  815. case Z90STAT_PERDEV_REQCNT: {
  816. int reqcnt[AP_DEVICES];
  817. zcrypt_perdev_reqcnt(reqcnt);
  818. if (copy_to_user((int __user *) arg, reqcnt,
  819. sizeof(int) * AP_DEVICES))
  820. return -EFAULT;
  821. return 0;
  822. }
  823. case Z90STAT_REQUESTQ_COUNT:
  824. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  825. case Z90STAT_PENDINGQ_COUNT:
  826. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  827. case Z90STAT_TOTALOPEN_COUNT:
  828. return put_user(atomic_read(&zcrypt_open_count),
  829. (int __user *) arg);
  830. case Z90STAT_DOMAIN_INDEX:
  831. return put_user(ap_domain_index, (int __user *) arg);
  832. /*
  833. * Deprecated ioctls. Don't add another device count ioctl,
  834. * you can count them yourself in the user space with the
  835. * output of the Z90STAT_STATUS_MASK ioctl.
  836. */
  837. case ICAZ90STATUS:
  838. return zcrypt_ica_status(filp, arg);
  839. case Z90STAT_TOTALCOUNT:
  840. return put_user(zcrypt_device_count, (int __user *) arg);
  841. case Z90STAT_PCICACOUNT:
  842. return put_user(zcrypt_count_type(ZCRYPT_PCICA),
  843. (int __user *) arg);
  844. case Z90STAT_PCICCCOUNT:
  845. return put_user(zcrypt_count_type(ZCRYPT_PCICC),
  846. (int __user *) arg);
  847. case Z90STAT_PCIXCCMCL2COUNT:
  848. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
  849. (int __user *) arg);
  850. case Z90STAT_PCIXCCMCL3COUNT:
  851. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  852. (int __user *) arg);
  853. case Z90STAT_PCIXCCCOUNT:
  854. return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
  855. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
  856. (int __user *) arg);
  857. case Z90STAT_CEX2CCOUNT:
  858. return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
  859. (int __user *) arg);
  860. case Z90STAT_CEX2ACOUNT:
  861. return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
  862. (int __user *) arg);
  863. default:
  864. /* unknown ioctl number */
  865. return -ENOIOCTLCMD;
  866. }
  867. }
  868. #ifdef CONFIG_COMPAT
  869. /*
  870. * ioctl32 conversion routines
  871. */
  872. struct compat_ica_rsa_modexpo {
  873. compat_uptr_t inputdata;
  874. unsigned int inputdatalength;
  875. compat_uptr_t outputdata;
  876. unsigned int outputdatalength;
  877. compat_uptr_t b_key;
  878. compat_uptr_t n_modulus;
  879. };
  880. static long trans_modexpo32(struct file *filp, unsigned int cmd,
  881. unsigned long arg)
  882. {
  883. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  884. struct compat_ica_rsa_modexpo mex32;
  885. struct ica_rsa_modexpo mex64;
  886. long rc;
  887. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  888. return -EFAULT;
  889. mex64.inputdata = compat_ptr(mex32.inputdata);
  890. mex64.inputdatalength = mex32.inputdatalength;
  891. mex64.outputdata = compat_ptr(mex32.outputdata);
  892. mex64.outputdatalength = mex32.outputdatalength;
  893. mex64.b_key = compat_ptr(mex32.b_key);
  894. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  895. do {
  896. rc = zcrypt_rsa_modexpo(&mex64);
  897. } while (rc == -EAGAIN);
  898. /* on failure: retry once again after a requested rescan */
  899. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  900. do {
  901. rc = zcrypt_rsa_modexpo(&mex64);
  902. } while (rc == -EAGAIN);
  903. if (rc)
  904. return rc;
  905. return put_user(mex64.outputdatalength,
  906. &umex32->outputdatalength);
  907. }
  908. struct compat_ica_rsa_modexpo_crt {
  909. compat_uptr_t inputdata;
  910. unsigned int inputdatalength;
  911. compat_uptr_t outputdata;
  912. unsigned int outputdatalength;
  913. compat_uptr_t bp_key;
  914. compat_uptr_t bq_key;
  915. compat_uptr_t np_prime;
  916. compat_uptr_t nq_prime;
  917. compat_uptr_t u_mult_inv;
  918. };
  919. static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
  920. unsigned long arg)
  921. {
  922. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  923. struct compat_ica_rsa_modexpo_crt crt32;
  924. struct ica_rsa_modexpo_crt crt64;
  925. long rc;
  926. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  927. return -EFAULT;
  928. crt64.inputdata = compat_ptr(crt32.inputdata);
  929. crt64.inputdatalength = crt32.inputdatalength;
  930. crt64.outputdata= compat_ptr(crt32.outputdata);
  931. crt64.outputdatalength = crt32.outputdatalength;
  932. crt64.bp_key = compat_ptr(crt32.bp_key);
  933. crt64.bq_key = compat_ptr(crt32.bq_key);
  934. crt64.np_prime = compat_ptr(crt32.np_prime);
  935. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  936. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  937. do {
  938. rc = zcrypt_rsa_crt(&crt64);
  939. } while (rc == -EAGAIN);
  940. /* on failure: retry once again after a requested rescan */
  941. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  942. do {
  943. rc = zcrypt_rsa_crt(&crt64);
  944. } while (rc == -EAGAIN);
  945. if (rc)
  946. return rc;
  947. return put_user(crt64.outputdatalength,
  948. &ucrt32->outputdatalength);
  949. }
  950. struct compat_ica_xcRB {
  951. unsigned short agent_ID;
  952. unsigned int user_defined;
  953. unsigned short request_ID;
  954. unsigned int request_control_blk_length;
  955. unsigned char padding1[16 - sizeof (compat_uptr_t)];
  956. compat_uptr_t request_control_blk_addr;
  957. unsigned int request_data_length;
  958. char padding2[16 - sizeof (compat_uptr_t)];
  959. compat_uptr_t request_data_address;
  960. unsigned int reply_control_blk_length;
  961. char padding3[16 - sizeof (compat_uptr_t)];
  962. compat_uptr_t reply_control_blk_addr;
  963. unsigned int reply_data_length;
  964. char padding4[16 - sizeof (compat_uptr_t)];
  965. compat_uptr_t reply_data_addr;
  966. unsigned short priority_window;
  967. unsigned int status;
  968. } __attribute__((packed));
  969. static long trans_xcRB32(struct file *filp, unsigned int cmd,
  970. unsigned long arg)
  971. {
  972. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  973. struct compat_ica_xcRB xcRB32;
  974. struct ica_xcRB xcRB64;
  975. long rc;
  976. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  977. return -EFAULT;
  978. xcRB64.agent_ID = xcRB32.agent_ID;
  979. xcRB64.user_defined = xcRB32.user_defined;
  980. xcRB64.request_ID = xcRB32.request_ID;
  981. xcRB64.request_control_blk_length =
  982. xcRB32.request_control_blk_length;
  983. xcRB64.request_control_blk_addr =
  984. compat_ptr(xcRB32.request_control_blk_addr);
  985. xcRB64.request_data_length =
  986. xcRB32.request_data_length;
  987. xcRB64.request_data_address =
  988. compat_ptr(xcRB32.request_data_address);
  989. xcRB64.reply_control_blk_length =
  990. xcRB32.reply_control_blk_length;
  991. xcRB64.reply_control_blk_addr =
  992. compat_ptr(xcRB32.reply_control_blk_addr);
  993. xcRB64.reply_data_length = xcRB32.reply_data_length;
  994. xcRB64.reply_data_addr =
  995. compat_ptr(xcRB32.reply_data_addr);
  996. xcRB64.priority_window = xcRB32.priority_window;
  997. xcRB64.status = xcRB32.status;
  998. do {
  999. rc = zcrypt_send_cprb(&xcRB64);
  1000. } while (rc == -EAGAIN);
  1001. /* on failure: retry once again after a requested rescan */
  1002. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1003. do {
  1004. rc = zcrypt_send_cprb(&xcRB64);
  1005. } while (rc == -EAGAIN);
  1006. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  1007. xcRB32.reply_data_length = xcRB64.reply_data_length;
  1008. xcRB32.status = xcRB64.status;
  1009. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  1010. return -EFAULT;
  1011. return rc;
  1012. }
  1013. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  1014. unsigned long arg)
  1015. {
  1016. if (cmd == ICARSAMODEXPO)
  1017. return trans_modexpo32(filp, cmd, arg);
  1018. if (cmd == ICARSACRT)
  1019. return trans_modexpo_crt32(filp, cmd, arg);
  1020. if (cmd == ZSECSENDCPRB)
  1021. return trans_xcRB32(filp, cmd, arg);
  1022. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  1023. }
  1024. #endif
  1025. /*
  1026. * Misc device file operations.
  1027. */
  1028. static const struct file_operations zcrypt_fops = {
  1029. .owner = THIS_MODULE,
  1030. .read = zcrypt_read,
  1031. .write = zcrypt_write,
  1032. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  1033. #ifdef CONFIG_COMPAT
  1034. .compat_ioctl = zcrypt_compat_ioctl,
  1035. #endif
  1036. .open = zcrypt_open,
  1037. .release = zcrypt_release,
  1038. .llseek = no_llseek,
  1039. };
  1040. /*
  1041. * Misc device.
  1042. */
  1043. static struct miscdevice zcrypt_misc_device = {
  1044. .minor = MISC_DYNAMIC_MINOR,
  1045. .name = "z90crypt",
  1046. .fops = &zcrypt_fops,
  1047. };
  1048. /*
  1049. * Deprecated /proc entry support.
  1050. */
  1051. static struct proc_dir_entry *zcrypt_entry;
  1052. static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
  1053. {
  1054. int i;
  1055. for (i = 0; i < len; i++)
  1056. seq_printf(m, "%01x", (unsigned int) addr[i]);
  1057. seq_putc(m, ' ');
  1058. }
  1059. static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
  1060. {
  1061. int inl, c, cx;
  1062. seq_printf(m, " ");
  1063. inl = 0;
  1064. for (c = 0; c < (len / 16); c++) {
  1065. sprintcl(m, addr+inl, 16);
  1066. inl += 16;
  1067. }
  1068. cx = len%16;
  1069. if (cx) {
  1070. sprintcl(m, addr+inl, cx);
  1071. inl += cx;
  1072. }
  1073. seq_putc(m, '\n');
  1074. }
  1075. static void sprinthx(unsigned char *title, struct seq_file *m,
  1076. unsigned char *addr, unsigned int len)
  1077. {
  1078. int inl, r, rx;
  1079. seq_printf(m, "\n%s\n", title);
  1080. inl = 0;
  1081. for (r = 0; r < (len / 64); r++) {
  1082. sprintrw(m, addr+inl, 64);
  1083. inl += 64;
  1084. }
  1085. rx = len % 64;
  1086. if (rx) {
  1087. sprintrw(m, addr+inl, rx);
  1088. inl += rx;
  1089. }
  1090. seq_putc(m, '\n');
  1091. }
  1092. static void sprinthx4(unsigned char *title, struct seq_file *m,
  1093. unsigned int *array, unsigned int len)
  1094. {
  1095. seq_printf(m, "\n%s\n", title);
  1096. seq_hex_dump(m, " ", DUMP_PREFIX_NONE, 32, 4, array, len, false);
  1097. seq_putc(m, '\n');
  1098. }
  1099. static int zcrypt_proc_show(struct seq_file *m, void *v)
  1100. {
  1101. char workarea[sizeof(int) * AP_DEVICES];
  1102. seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
  1103. ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
  1104. seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
  1105. seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
  1106. seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
  1107. seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
  1108. seq_printf(m, "PCIXCC MCL2 count: %d\n",
  1109. zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
  1110. seq_printf(m, "PCIXCC MCL3 count: %d\n",
  1111. zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
  1112. seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
  1113. seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
  1114. seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
  1115. seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
  1116. seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
  1117. seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
  1118. seq_printf(m, "Total open handles: %d\n\n",
  1119. atomic_read(&zcrypt_open_count));
  1120. zcrypt_status_mask(workarea);
  1121. sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
  1122. "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
  1123. m, workarea, AP_DEVICES);
  1124. zcrypt_qdepth_mask(workarea);
  1125. sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
  1126. zcrypt_perdev_reqcnt((int *) workarea);
  1127. sprinthx4("Per-device successfully completed request counts",
  1128. m, (unsigned int *) workarea, AP_DEVICES);
  1129. return 0;
  1130. }
  1131. static int zcrypt_proc_open(struct inode *inode, struct file *file)
  1132. {
  1133. return single_open(file, zcrypt_proc_show, NULL);
  1134. }
  1135. static void zcrypt_disable_card(int index)
  1136. {
  1137. struct zcrypt_card *zc;
  1138. struct zcrypt_queue *zq;
  1139. spin_lock(&zcrypt_list_lock);
  1140. for_each_zcrypt_card(zc) {
  1141. for_each_zcrypt_queue(zq, zc) {
  1142. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1143. continue;
  1144. zq->online = 0;
  1145. ap_flush_queue(zq->queue);
  1146. }
  1147. }
  1148. spin_unlock(&zcrypt_list_lock);
  1149. }
  1150. static void zcrypt_enable_card(int index)
  1151. {
  1152. struct zcrypt_card *zc;
  1153. struct zcrypt_queue *zq;
  1154. spin_lock(&zcrypt_list_lock);
  1155. for_each_zcrypt_card(zc) {
  1156. for_each_zcrypt_queue(zq, zc) {
  1157. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1158. continue;
  1159. zq->online = 1;
  1160. ap_flush_queue(zq->queue);
  1161. }
  1162. }
  1163. spin_unlock(&zcrypt_list_lock);
  1164. }
  1165. static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
  1166. size_t count, loff_t *pos)
  1167. {
  1168. unsigned char *lbuf, *ptr;
  1169. size_t local_count;
  1170. int j;
  1171. if (count <= 0)
  1172. return 0;
  1173. #define LBUFSIZE 1200UL
  1174. lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
  1175. if (!lbuf)
  1176. return 0;
  1177. local_count = min(LBUFSIZE - 1, count);
  1178. if (copy_from_user(lbuf, buffer, local_count) != 0) {
  1179. kfree(lbuf);
  1180. return -EFAULT;
  1181. }
  1182. lbuf[local_count] = '\0';
  1183. ptr = strstr(lbuf, "Online devices");
  1184. if (!ptr)
  1185. goto out;
  1186. ptr = strstr(ptr, "\n");
  1187. if (!ptr)
  1188. goto out;
  1189. ptr++;
  1190. if (strstr(ptr, "Waiting work element counts") == NULL)
  1191. goto out;
  1192. for (j = 0; j < 64 && *ptr; ptr++) {
  1193. /*
  1194. * '0' for no device, '1' for PCICA, '2' for PCICC,
  1195. * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
  1196. * '5' for CEX2C and '6' for CEX2A'
  1197. * '7' for CEX3C and '8' for CEX3A
  1198. */
  1199. if (*ptr >= '0' && *ptr <= '8')
  1200. j++;
  1201. else if (*ptr == 'd' || *ptr == 'D')
  1202. zcrypt_disable_card(j++);
  1203. else if (*ptr == 'e' || *ptr == 'E')
  1204. zcrypt_enable_card(j++);
  1205. else if (*ptr != ' ' && *ptr != '\t')
  1206. break;
  1207. }
  1208. out:
  1209. kfree(lbuf);
  1210. return count;
  1211. }
  1212. static const struct file_operations zcrypt_proc_fops = {
  1213. .owner = THIS_MODULE,
  1214. .open = zcrypt_proc_open,
  1215. .read = seq_read,
  1216. .llseek = seq_lseek,
  1217. .release = single_release,
  1218. .write = zcrypt_proc_write,
  1219. };
  1220. static int zcrypt_rng_device_count;
  1221. static u32 *zcrypt_rng_buffer;
  1222. static int zcrypt_rng_buffer_index;
  1223. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1224. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1225. {
  1226. int rc;
  1227. /*
  1228. * We don't need locking here because the RNG API guarantees serialized
  1229. * read method calls.
  1230. */
  1231. if (zcrypt_rng_buffer_index == 0) {
  1232. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1233. /* on failure: retry once again after a requested rescan */
  1234. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1235. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1236. if (rc < 0)
  1237. return -EIO;
  1238. zcrypt_rng_buffer_index = rc / sizeof *data;
  1239. }
  1240. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1241. return sizeof *data;
  1242. }
  1243. static struct hwrng zcrypt_rng_dev = {
  1244. .name = "zcrypt",
  1245. .data_read = zcrypt_rng_data_read,
  1246. .quality = 990,
  1247. };
  1248. int zcrypt_rng_device_add(void)
  1249. {
  1250. int rc = 0;
  1251. mutex_lock(&zcrypt_rng_mutex);
  1252. if (zcrypt_rng_device_count == 0) {
  1253. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1254. if (!zcrypt_rng_buffer) {
  1255. rc = -ENOMEM;
  1256. goto out;
  1257. }
  1258. zcrypt_rng_buffer_index = 0;
  1259. if (!zcrypt_hwrng_seed)
  1260. zcrypt_rng_dev.quality = 0;
  1261. rc = hwrng_register(&zcrypt_rng_dev);
  1262. if (rc)
  1263. goto out_free;
  1264. zcrypt_rng_device_count = 1;
  1265. } else
  1266. zcrypt_rng_device_count++;
  1267. mutex_unlock(&zcrypt_rng_mutex);
  1268. return 0;
  1269. out_free:
  1270. free_page((unsigned long) zcrypt_rng_buffer);
  1271. out:
  1272. mutex_unlock(&zcrypt_rng_mutex);
  1273. return rc;
  1274. }
  1275. void zcrypt_rng_device_remove(void)
  1276. {
  1277. mutex_lock(&zcrypt_rng_mutex);
  1278. zcrypt_rng_device_count--;
  1279. if (zcrypt_rng_device_count == 0) {
  1280. hwrng_unregister(&zcrypt_rng_dev);
  1281. free_page((unsigned long) zcrypt_rng_buffer);
  1282. }
  1283. mutex_unlock(&zcrypt_rng_mutex);
  1284. }
  1285. int __init zcrypt_debug_init(void)
  1286. {
  1287. zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
  1288. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1289. debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
  1290. debug_set_level(zcrypt_dbf_info, DBF_ERR);
  1291. return 0;
  1292. }
  1293. void zcrypt_debug_exit(void)
  1294. {
  1295. debug_unregister(zcrypt_dbf_info);
  1296. }
  1297. /**
  1298. * zcrypt_api_init(): Module initialization.
  1299. *
  1300. * The module initialization code.
  1301. */
  1302. int __init zcrypt_api_init(void)
  1303. {
  1304. int rc;
  1305. rc = zcrypt_debug_init();
  1306. if (rc)
  1307. goto out;
  1308. atomic_set(&zcrypt_rescan_req, 0);
  1309. /* Register the request sprayer. */
  1310. rc = misc_register(&zcrypt_misc_device);
  1311. if (rc < 0)
  1312. goto out;
  1313. /* Set up the proc file system */
  1314. zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL,
  1315. &zcrypt_proc_fops);
  1316. if (!zcrypt_entry) {
  1317. rc = -ENOMEM;
  1318. goto out_misc;
  1319. }
  1320. zcrypt_msgtype6_init();
  1321. zcrypt_msgtype50_init();
  1322. return 0;
  1323. out_misc:
  1324. misc_deregister(&zcrypt_misc_device);
  1325. out:
  1326. return rc;
  1327. }
  1328. /**
  1329. * zcrypt_api_exit(): Module termination.
  1330. *
  1331. * The module termination code.
  1332. */
  1333. void __exit zcrypt_api_exit(void)
  1334. {
  1335. remove_proc_entry("driver/z90crypt", NULL);
  1336. misc_deregister(&zcrypt_misc_device);
  1337. zcrypt_msgtype6_exit();
  1338. zcrypt_msgtype50_exit();
  1339. zcrypt_debug_exit();
  1340. }
  1341. module_init(zcrypt_api_init);
  1342. module_exit(zcrypt_api_exit);