zcrypt_api.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2001, 2018
  4. * Author(s): Robert Burroughs
  5. * Eric Rossman (edrossma@us.ibm.com)
  6. * Cornelia Huck <cornelia.huck@de.ibm.com>
  7. *
  8. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  9. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  10. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  11. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  12. * Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
  13. */
  14. #define KMSG_COMPONENT "zcrypt"
  15. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/fs.h>
  21. #include <linux/compat.h>
  22. #include <linux/slab.h>
  23. #include <linux/atomic.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/hw_random.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/cdev.h>
  28. #include <linux/ctype.h>
  29. #include <linux/capability.h>
  30. #include <asm/debug.h>
  31. #define CREATE_TRACE_POINTS
  32. #include <asm/trace/zcrypt.h>
  33. #include "zcrypt_api.h"
  34. #include "zcrypt_debug.h"
  35. #include "zcrypt_msgtype6.h"
  36. #include "zcrypt_msgtype50.h"
  37. #include "zcrypt_ccamisc.h"
  38. #include "zcrypt_ep11misc.h"
  39. /*
  40. * Module description.
  41. */
  42. MODULE_AUTHOR("IBM Corporation");
  43. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  44. "Copyright IBM Corp. 2001, 2012");
  45. MODULE_LICENSE("GPL");
  46. /*
  47. * zcrypt tracepoint functions
  48. */
  49. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
  50. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
  51. DEFINE_SPINLOCK(zcrypt_list_lock);
  52. LIST_HEAD(zcrypt_card_list);
  53. static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  54. static LIST_HEAD(zcrypt_ops_list);
  55. /* Zcrypt related debug feature stuff. */
  56. debug_info_t *zcrypt_dbf_info;
  57. /*
  58. * Process a rescan of the transport layer.
  59. * Runs a synchronous AP bus rescan.
  60. * Returns true if something has changed (for example the
  61. * bus scan has found and build up new devices) and it is
  62. * worth to do a retry. Otherwise false is returned meaning
  63. * no changes on the AP bus level.
  64. */
  65. static inline bool zcrypt_process_rescan(void)
  66. {
  67. return ap_bus_force_rescan();
  68. }
  69. void zcrypt_msgtype_register(struct zcrypt_ops *zops)
  70. {
  71. list_add_tail(&zops->list, &zcrypt_ops_list);
  72. }
  73. void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  74. {
  75. list_del_init(&zops->list);
  76. }
  77. struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
  78. {
  79. struct zcrypt_ops *zops;
  80. list_for_each_entry(zops, &zcrypt_ops_list, list)
  81. if (zops->variant == variant &&
  82. (!strncmp(zops->name, name, sizeof(zops->name))))
  83. return zops;
  84. return NULL;
  85. }
  86. EXPORT_SYMBOL(zcrypt_msgtype);
  87. /*
  88. * Multi device nodes extension functions.
  89. */
  90. struct zcdn_device;
  91. static void zcdn_device_release(struct device *dev);
  92. static const struct class zcrypt_class = {
  93. .name = ZCRYPT_NAME,
  94. .dev_release = zcdn_device_release,
  95. };
  96. static dev_t zcrypt_devt;
  97. static struct cdev zcrypt_cdev;
  98. struct zcdn_device {
  99. struct device device;
  100. struct ap_perms perms;
  101. };
  102. #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
  103. #define ZCDN_MAX_NAME 32
  104. static int zcdn_create(const char *name);
  105. static int zcdn_destroy(const char *name);
  106. /*
  107. * Find zcdn device by name.
  108. * Returns reference to the zcdn device which needs to be released
  109. * with put_device() after use.
  110. */
  111. static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
  112. {
  113. struct device *dev = class_find_device_by_name(&zcrypt_class, name);
  114. return dev ? to_zcdn_dev(dev) : NULL;
  115. }
  116. /*
  117. * Find zcdn device by devt value.
  118. * Returns reference to the zcdn device which needs to be released
  119. * with put_device() after use.
  120. */
  121. static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
  122. {
  123. struct device *dev = class_find_device_by_devt(&zcrypt_class, devt);
  124. return dev ? to_zcdn_dev(dev) : NULL;
  125. }
  126. static ssize_t ioctlmask_show(struct device *dev,
  127. struct device_attribute *attr,
  128. char *buf)
  129. {
  130. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  131. int i, n;
  132. if (mutex_lock_interruptible(&ap_perms_mutex))
  133. return -ERESTARTSYS;
  134. n = sysfs_emit(buf, "0x");
  135. for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
  136. n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.ioctlm[i]);
  137. n += sysfs_emit_at(buf, n, "\n");
  138. mutex_unlock(&ap_perms_mutex);
  139. return n;
  140. }
  141. static ssize_t ioctlmask_store(struct device *dev,
  142. struct device_attribute *attr,
  143. const char *buf, size_t count)
  144. {
  145. int rc;
  146. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  147. rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
  148. AP_IOCTLS, &ap_perms_mutex);
  149. if (rc)
  150. return rc;
  151. return count;
  152. }
  153. static DEVICE_ATTR_RW(ioctlmask);
  154. static ssize_t apmask_show(struct device *dev,
  155. struct device_attribute *attr,
  156. char *buf)
  157. {
  158. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  159. int i, n;
  160. if (mutex_lock_interruptible(&ap_perms_mutex))
  161. return -ERESTARTSYS;
  162. n = sysfs_emit(buf, "0x");
  163. for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
  164. n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.apm[i]);
  165. n += sysfs_emit_at(buf, n, "\n");
  166. mutex_unlock(&ap_perms_mutex);
  167. return n;
  168. }
  169. static ssize_t apmask_store(struct device *dev,
  170. struct device_attribute *attr,
  171. const char *buf, size_t count)
  172. {
  173. int rc;
  174. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  175. rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
  176. AP_DEVICES, &ap_perms_mutex);
  177. if (rc)
  178. return rc;
  179. return count;
  180. }
  181. static DEVICE_ATTR_RW(apmask);
  182. static ssize_t aqmask_show(struct device *dev,
  183. struct device_attribute *attr,
  184. char *buf)
  185. {
  186. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  187. int i, n;
  188. if (mutex_lock_interruptible(&ap_perms_mutex))
  189. return -ERESTARTSYS;
  190. n = sysfs_emit(buf, "0x");
  191. for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
  192. n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.aqm[i]);
  193. n += sysfs_emit_at(buf, n, "\n");
  194. mutex_unlock(&ap_perms_mutex);
  195. return n;
  196. }
  197. static ssize_t aqmask_store(struct device *dev,
  198. struct device_attribute *attr,
  199. const char *buf, size_t count)
  200. {
  201. int rc;
  202. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  203. rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
  204. AP_DOMAINS, &ap_perms_mutex);
  205. if (rc)
  206. return rc;
  207. return count;
  208. }
  209. static DEVICE_ATTR_RW(aqmask);
  210. static ssize_t admask_show(struct device *dev,
  211. struct device_attribute *attr,
  212. char *buf)
  213. {
  214. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  215. int i, n;
  216. if (mutex_lock_interruptible(&ap_perms_mutex))
  217. return -ERESTARTSYS;
  218. n = sysfs_emit(buf, "0x");
  219. for (i = 0; i < sizeof(zcdndev->perms.adm) / sizeof(long); i++)
  220. n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.adm[i]);
  221. n += sysfs_emit_at(buf, n, "\n");
  222. mutex_unlock(&ap_perms_mutex);
  223. return n;
  224. }
  225. static ssize_t admask_store(struct device *dev,
  226. struct device_attribute *attr,
  227. const char *buf, size_t count)
  228. {
  229. int rc;
  230. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  231. rc = ap_parse_mask_str(buf, zcdndev->perms.adm,
  232. AP_DOMAINS, &ap_perms_mutex);
  233. if (rc)
  234. return rc;
  235. return count;
  236. }
  237. static DEVICE_ATTR_RW(admask);
  238. static struct attribute *zcdn_dev_attrs[] = {
  239. &dev_attr_ioctlmask.attr,
  240. &dev_attr_apmask.attr,
  241. &dev_attr_aqmask.attr,
  242. &dev_attr_admask.attr,
  243. NULL
  244. };
  245. static struct attribute_group zcdn_dev_attr_group = {
  246. .attrs = zcdn_dev_attrs
  247. };
  248. static const struct attribute_group *zcdn_dev_attr_groups[] = {
  249. &zcdn_dev_attr_group,
  250. NULL
  251. };
  252. static ssize_t zcdn_create_store(const struct class *class,
  253. const struct class_attribute *attr,
  254. const char *buf, size_t count)
  255. {
  256. int rc;
  257. char name[ZCDN_MAX_NAME];
  258. strscpy(name, skip_spaces(buf), sizeof(name));
  259. rc = zcdn_create(strim(name));
  260. return rc ? rc : count;
  261. }
  262. static const struct class_attribute class_attr_zcdn_create =
  263. __ATTR(create, 0600, NULL, zcdn_create_store);
  264. static ssize_t zcdn_destroy_store(const struct class *class,
  265. const struct class_attribute *attr,
  266. const char *buf, size_t count)
  267. {
  268. int rc;
  269. char name[ZCDN_MAX_NAME];
  270. strscpy(name, skip_spaces(buf), sizeof(name));
  271. rc = zcdn_destroy(strim(name));
  272. return rc ? rc : count;
  273. }
  274. static const struct class_attribute class_attr_zcdn_destroy =
  275. __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
  276. static void zcdn_device_release(struct device *dev)
  277. {
  278. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  279. ZCRYPT_DBF_INFO("%s releasing zcdn device %d:%d\n",
  280. __func__, MAJOR(dev->devt), MINOR(dev->devt));
  281. kfree(zcdndev);
  282. }
  283. static int zcdn_create(const char *name)
  284. {
  285. dev_t devt;
  286. int i, rc = 0;
  287. struct zcdn_device *zcdndev;
  288. if (mutex_lock_interruptible(&ap_perms_mutex))
  289. return -ERESTARTSYS;
  290. /* check if device node with this name already exists */
  291. if (name[0]) {
  292. zcdndev = find_zcdndev_by_name(name);
  293. if (zcdndev) {
  294. put_device(&zcdndev->device);
  295. rc = -EEXIST;
  296. goto unlockout;
  297. }
  298. }
  299. /* find an unused minor number */
  300. for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
  301. devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
  302. zcdndev = find_zcdndev_by_devt(devt);
  303. if (zcdndev)
  304. put_device(&zcdndev->device);
  305. else
  306. break;
  307. }
  308. if (i == ZCRYPT_MAX_MINOR_NODES) {
  309. rc = -ENOSPC;
  310. goto unlockout;
  311. }
  312. /* alloc and prepare a new zcdn device */
  313. zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
  314. if (!zcdndev) {
  315. rc = -ENOMEM;
  316. goto unlockout;
  317. }
  318. zcdndev->device.release = zcdn_device_release;
  319. zcdndev->device.class = &zcrypt_class;
  320. zcdndev->device.devt = devt;
  321. zcdndev->device.groups = zcdn_dev_attr_groups;
  322. if (name[0])
  323. rc = dev_set_name(&zcdndev->device, "%s", name);
  324. else
  325. rc = dev_set_name(&zcdndev->device, ZCRYPT_NAME "_%d", (int)MINOR(devt));
  326. if (rc) {
  327. kfree(zcdndev);
  328. goto unlockout;
  329. }
  330. rc = device_register(&zcdndev->device);
  331. if (rc) {
  332. put_device(&zcdndev->device);
  333. goto unlockout;
  334. }
  335. ZCRYPT_DBF_INFO("%s created zcdn device %d:%d\n",
  336. __func__, MAJOR(devt), MINOR(devt));
  337. unlockout:
  338. mutex_unlock(&ap_perms_mutex);
  339. return rc;
  340. }
  341. static int zcdn_destroy(const char *name)
  342. {
  343. int rc = 0;
  344. struct zcdn_device *zcdndev;
  345. if (mutex_lock_interruptible(&ap_perms_mutex))
  346. return -ERESTARTSYS;
  347. /* try to find this zcdn device */
  348. zcdndev = find_zcdndev_by_name(name);
  349. if (!zcdndev) {
  350. rc = -ENOENT;
  351. goto unlockout;
  352. }
  353. /*
  354. * The zcdn device is not hard destroyed. It is subject to
  355. * reference counting and thus just needs to be unregistered.
  356. */
  357. put_device(&zcdndev->device);
  358. device_unregister(&zcdndev->device);
  359. unlockout:
  360. mutex_unlock(&ap_perms_mutex);
  361. return rc;
  362. }
  363. static void zcdn_destroy_all(void)
  364. {
  365. int i;
  366. dev_t devt;
  367. struct zcdn_device *zcdndev;
  368. mutex_lock(&ap_perms_mutex);
  369. for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
  370. devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
  371. zcdndev = find_zcdndev_by_devt(devt);
  372. if (zcdndev) {
  373. put_device(&zcdndev->device);
  374. device_unregister(&zcdndev->device);
  375. }
  376. }
  377. mutex_unlock(&ap_perms_mutex);
  378. }
  379. /*
  380. * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
  381. *
  382. * This function is not supported beyond zcrypt 1.3.1.
  383. */
  384. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  385. size_t count, loff_t *f_pos)
  386. {
  387. return -EPERM;
  388. }
  389. /*
  390. * zcrypt_write(): Not allowed.
  391. *
  392. * Write is not allowed
  393. */
  394. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  395. size_t count, loff_t *f_pos)
  396. {
  397. return -EPERM;
  398. }
  399. /*
  400. * zcrypt_open(): Count number of users.
  401. *
  402. * Device open function to count number of users.
  403. */
  404. static int zcrypt_open(struct inode *inode, struct file *filp)
  405. {
  406. struct ap_perms *perms = &ap_perms;
  407. if (filp->f_inode->i_cdev == &zcrypt_cdev) {
  408. struct zcdn_device *zcdndev;
  409. if (mutex_lock_interruptible(&ap_perms_mutex))
  410. return -ERESTARTSYS;
  411. zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
  412. /* find returns a reference, no get_device() needed */
  413. mutex_unlock(&ap_perms_mutex);
  414. if (zcdndev)
  415. perms = &zcdndev->perms;
  416. }
  417. filp->private_data = (void *)perms;
  418. atomic_inc(&zcrypt_open_count);
  419. return stream_open(inode, filp);
  420. }
  421. /*
  422. * zcrypt_release(): Count number of users.
  423. *
  424. * Device close function to count number of users.
  425. */
  426. static int zcrypt_release(struct inode *inode, struct file *filp)
  427. {
  428. if (filp->f_inode->i_cdev == &zcrypt_cdev) {
  429. struct zcdn_device *zcdndev;
  430. mutex_lock(&ap_perms_mutex);
  431. zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
  432. mutex_unlock(&ap_perms_mutex);
  433. if (zcdndev) {
  434. /* 2 puts here: one for find, one for open */
  435. put_device(&zcdndev->device);
  436. put_device(&zcdndev->device);
  437. }
  438. }
  439. atomic_dec(&zcrypt_open_count);
  440. return 0;
  441. }
  442. static inline int zcrypt_check_ioctl(struct ap_perms *perms,
  443. unsigned int cmd)
  444. {
  445. int rc = -EPERM;
  446. int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
  447. if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
  448. if (test_bit_inv(ioctlnr, perms->ioctlm))
  449. rc = 0;
  450. }
  451. if (rc)
  452. ZCRYPT_DBF_WARN("%s ioctl check failed: ioctlnr=0x%04x rc=%d\n",
  453. __func__, ioctlnr, rc);
  454. return rc;
  455. }
  456. static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
  457. {
  458. return test_bit_inv(card, perms->apm) ? true : false;
  459. }
  460. static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
  461. {
  462. return test_bit_inv(queue, perms->aqm) ? true : false;
  463. }
  464. static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
  465. struct zcrypt_queue *zq,
  466. struct module **pmod,
  467. unsigned int weight)
  468. {
  469. if (!zq || !try_module_get(zq->queue->ap_dev.device.driver->owner))
  470. return NULL;
  471. zcrypt_card_get(zc);
  472. zcrypt_queue_get(zq);
  473. get_device(&zq->queue->ap_dev.device);
  474. atomic_add(weight, &zc->load);
  475. atomic_add(weight, &zq->load);
  476. zq->request_count++;
  477. *pmod = zq->queue->ap_dev.device.driver->owner;
  478. return zq;
  479. }
  480. static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
  481. struct zcrypt_queue *zq,
  482. struct module *mod,
  483. unsigned int weight)
  484. {
  485. zq->request_count--;
  486. atomic_sub(weight, &zc->load);
  487. atomic_sub(weight, &zq->load);
  488. put_device(&zq->queue->ap_dev.device);
  489. zcrypt_queue_put(zq);
  490. zcrypt_card_put(zc);
  491. module_put(mod);
  492. }
  493. static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
  494. struct zcrypt_card *pref_zc,
  495. unsigned int weight,
  496. unsigned int pref_weight)
  497. {
  498. if (!pref_zc)
  499. return true;
  500. weight += atomic_read(&zc->load);
  501. pref_weight += atomic_read(&pref_zc->load);
  502. if (weight == pref_weight)
  503. return atomic64_read(&zc->card->total_request_count) <
  504. atomic64_read(&pref_zc->card->total_request_count);
  505. return weight < pref_weight;
  506. }
  507. static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
  508. struct zcrypt_queue *pref_zq,
  509. unsigned int weight,
  510. unsigned int pref_weight)
  511. {
  512. if (!pref_zq)
  513. return true;
  514. weight += atomic_read(&zq->load);
  515. pref_weight += atomic_read(&pref_zq->load);
  516. if (weight == pref_weight)
  517. return zq->queue->total_request_count <
  518. pref_zq->queue->total_request_count;
  519. return weight < pref_weight;
  520. }
  521. /*
  522. * zcrypt ioctls.
  523. */
  524. static long zcrypt_rsa_modexpo(struct ap_perms *perms,
  525. struct zcrypt_track *tr,
  526. struct ica_rsa_modexpo *mex)
  527. {
  528. struct zcrypt_card *zc, *pref_zc;
  529. struct zcrypt_queue *zq, *pref_zq;
  530. struct ap_message ap_msg;
  531. unsigned int wgt = 0, pref_wgt = 0;
  532. unsigned int func_code;
  533. int cpen, qpen, qid = 0, rc = -ENODEV;
  534. struct module *mod;
  535. trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
  536. ap_init_message(&ap_msg);
  537. if (mex->outputdatalength < mex->inputdatalength) {
  538. func_code = 0;
  539. rc = -EINVAL;
  540. goto out;
  541. }
  542. /*
  543. * As long as outputdatalength is big enough, we can set the
  544. * outputdatalength equal to the inputdatalength, since that is the
  545. * number of bytes we will copy in any case
  546. */
  547. mex->outputdatalength = mex->inputdatalength;
  548. rc = get_rsa_modex_fc(mex, &func_code);
  549. if (rc)
  550. goto out;
  551. pref_zc = NULL;
  552. pref_zq = NULL;
  553. spin_lock(&zcrypt_list_lock);
  554. for_each_zcrypt_card(zc) {
  555. /* Check for usable accelerator or CCA card */
  556. if (!zc->online || !zc->card->config || zc->card->chkstop ||
  557. !(zc->card->hwinfo.accel || zc->card->hwinfo.cca))
  558. continue;
  559. /* Check for size limits */
  560. if (zc->min_mod_size > mex->inputdatalength ||
  561. zc->max_mod_size < mex->inputdatalength)
  562. continue;
  563. /* check if device node has admission for this card */
  564. if (!zcrypt_check_card(perms, zc->card->id))
  565. continue;
  566. /* get weight index of the card device */
  567. wgt = zc->speed_rating[func_code];
  568. /* penalty if this msg was previously sent via this card */
  569. cpen = (tr && tr->again_counter && tr->last_qid &&
  570. AP_QID_CARD(tr->last_qid) == zc->card->id) ?
  571. TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
  572. if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
  573. continue;
  574. for_each_zcrypt_queue(zq, zc) {
  575. /* check if device is usable and eligible */
  576. if (!zq->online || !zq->ops->rsa_modexpo ||
  577. !ap_queue_usable(zq->queue))
  578. continue;
  579. /* check if device node has admission for this queue */
  580. if (!zcrypt_check_queue(perms,
  581. AP_QID_QUEUE(zq->queue->qid)))
  582. continue;
  583. /* penalty if the msg was previously sent at this qid */
  584. qpen = (tr && tr->again_counter && tr->last_qid &&
  585. tr->last_qid == zq->queue->qid) ?
  586. TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
  587. if (!zcrypt_queue_compare(zq, pref_zq,
  588. wgt + cpen + qpen, pref_wgt))
  589. continue;
  590. pref_zc = zc;
  591. pref_zq = zq;
  592. pref_wgt = wgt + cpen + qpen;
  593. }
  594. }
  595. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
  596. spin_unlock(&zcrypt_list_lock);
  597. if (!pref_zq) {
  598. pr_debug("no matching queue found => ENODEV\n");
  599. rc = -ENODEV;
  600. goto out;
  601. }
  602. qid = pref_zq->queue->qid;
  603. rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
  604. spin_lock(&zcrypt_list_lock);
  605. zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
  606. spin_unlock(&zcrypt_list_lock);
  607. out:
  608. ap_release_message(&ap_msg);
  609. if (tr) {
  610. tr->last_rc = rc;
  611. tr->last_qid = qid;
  612. }
  613. trace_s390_zcrypt_rep(mex, func_code, rc,
  614. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  615. return rc;
  616. }
  617. static long zcrypt_rsa_crt(struct ap_perms *perms,
  618. struct zcrypt_track *tr,
  619. struct ica_rsa_modexpo_crt *crt)
  620. {
  621. struct zcrypt_card *zc, *pref_zc;
  622. struct zcrypt_queue *zq, *pref_zq;
  623. struct ap_message ap_msg;
  624. unsigned int wgt = 0, pref_wgt = 0;
  625. unsigned int func_code;
  626. int cpen, qpen, qid = 0, rc = -ENODEV;
  627. struct module *mod;
  628. trace_s390_zcrypt_req(crt, TP_ICARSACRT);
  629. ap_init_message(&ap_msg);
  630. if (crt->outputdatalength < crt->inputdatalength) {
  631. func_code = 0;
  632. rc = -EINVAL;
  633. goto out;
  634. }
  635. /*
  636. * As long as outputdatalength is big enough, we can set the
  637. * outputdatalength equal to the inputdatalength, since that is the
  638. * number of bytes we will copy in any case
  639. */
  640. crt->outputdatalength = crt->inputdatalength;
  641. rc = get_rsa_crt_fc(crt, &func_code);
  642. if (rc)
  643. goto out;
  644. pref_zc = NULL;
  645. pref_zq = NULL;
  646. spin_lock(&zcrypt_list_lock);
  647. for_each_zcrypt_card(zc) {
  648. /* Check for usable accelerator or CCA card */
  649. if (!zc->online || !zc->card->config || zc->card->chkstop ||
  650. !(zc->card->hwinfo.accel || zc->card->hwinfo.cca))
  651. continue;
  652. /* Check for size limits */
  653. if (zc->min_mod_size > crt->inputdatalength ||
  654. zc->max_mod_size < crt->inputdatalength)
  655. continue;
  656. /* check if device node has admission for this card */
  657. if (!zcrypt_check_card(perms, zc->card->id))
  658. continue;
  659. /* get weight index of the card device */
  660. wgt = zc->speed_rating[func_code];
  661. /* penalty if this msg was previously sent via this card */
  662. cpen = (tr && tr->again_counter && tr->last_qid &&
  663. AP_QID_CARD(tr->last_qid) == zc->card->id) ?
  664. TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
  665. if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
  666. continue;
  667. for_each_zcrypt_queue(zq, zc) {
  668. /* check if device is usable and eligible */
  669. if (!zq->online || !zq->ops->rsa_modexpo_crt ||
  670. !ap_queue_usable(zq->queue))
  671. continue;
  672. /* check if device node has admission for this queue */
  673. if (!zcrypt_check_queue(perms,
  674. AP_QID_QUEUE(zq->queue->qid)))
  675. continue;
  676. /* penalty if the msg was previously sent at this qid */
  677. qpen = (tr && tr->again_counter && tr->last_qid &&
  678. tr->last_qid == zq->queue->qid) ?
  679. TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
  680. if (!zcrypt_queue_compare(zq, pref_zq,
  681. wgt + cpen + qpen, pref_wgt))
  682. continue;
  683. pref_zc = zc;
  684. pref_zq = zq;
  685. pref_wgt = wgt + cpen + qpen;
  686. }
  687. }
  688. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
  689. spin_unlock(&zcrypt_list_lock);
  690. if (!pref_zq) {
  691. pr_debug("no matching queue found => ENODEV\n");
  692. rc = -ENODEV;
  693. goto out;
  694. }
  695. qid = pref_zq->queue->qid;
  696. rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
  697. spin_lock(&zcrypt_list_lock);
  698. zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
  699. spin_unlock(&zcrypt_list_lock);
  700. out:
  701. ap_release_message(&ap_msg);
  702. if (tr) {
  703. tr->last_rc = rc;
  704. tr->last_qid = qid;
  705. }
  706. trace_s390_zcrypt_rep(crt, func_code, rc,
  707. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  708. return rc;
  709. }
  710. static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
  711. struct zcrypt_track *tr,
  712. struct ica_xcRB *xcrb)
  713. {
  714. struct zcrypt_card *zc, *pref_zc;
  715. struct zcrypt_queue *zq, *pref_zq;
  716. struct ap_message ap_msg;
  717. unsigned int wgt = 0, pref_wgt = 0;
  718. unsigned int func_code;
  719. unsigned short *domain, tdom;
  720. int cpen, qpen, qid = 0, rc = -ENODEV;
  721. struct module *mod;
  722. trace_s390_zcrypt_req(xcrb, TB_ZSECSENDCPRB);
  723. xcrb->status = 0;
  724. ap_init_message(&ap_msg);
  725. rc = prep_cca_ap_msg(userspace, xcrb, &ap_msg, &func_code, &domain);
  726. if (rc)
  727. goto out;
  728. print_hex_dump_debug("ccareq: ", DUMP_PREFIX_ADDRESS, 16, 1,
  729. ap_msg.msg, ap_msg.len, false);
  730. tdom = *domain;
  731. if (perms != &ap_perms && tdom < AP_DOMAINS) {
  732. if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
  733. if (!test_bit_inv(tdom, perms->adm)) {
  734. rc = -ENODEV;
  735. goto out;
  736. }
  737. } else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
  738. rc = -EOPNOTSUPP;
  739. goto out;
  740. }
  741. }
  742. /*
  743. * If a valid target domain is set and this domain is NOT a usage
  744. * domain but a control only domain, autoselect target domain.
  745. */
  746. if (tdom < AP_DOMAINS &&
  747. !ap_test_config_usage_domain(tdom) &&
  748. ap_test_config_ctrl_domain(tdom))
  749. tdom = AUTOSEL_DOM;
  750. pref_zc = NULL;
  751. pref_zq = NULL;
  752. spin_lock(&zcrypt_list_lock);
  753. for_each_zcrypt_card(zc) {
  754. /* Check for usable CCA card */
  755. if (!zc->online || !zc->card->config || zc->card->chkstop ||
  756. !zc->card->hwinfo.cca)
  757. continue;
  758. /* Check for user selected CCA card */
  759. if (xcrb->user_defined != AUTOSELECT &&
  760. xcrb->user_defined != zc->card->id)
  761. continue;
  762. /* check if request size exceeds card max msg size */
  763. if (ap_msg.len > zc->card->maxmsgsize)
  764. continue;
  765. /* check if device node has admission for this card */
  766. if (!zcrypt_check_card(perms, zc->card->id))
  767. continue;
  768. /* get weight index of the card device */
  769. wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
  770. /* penalty if this msg was previously sent via this card */
  771. cpen = (tr && tr->again_counter && tr->last_qid &&
  772. AP_QID_CARD(tr->last_qid) == zc->card->id) ?
  773. TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
  774. if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
  775. continue;
  776. for_each_zcrypt_queue(zq, zc) {
  777. /* check for device usable and eligible */
  778. if (!zq->online || !zq->ops->send_cprb ||
  779. !ap_queue_usable(zq->queue) ||
  780. (tdom != AUTOSEL_DOM &&
  781. tdom != AP_QID_QUEUE(zq->queue->qid)))
  782. continue;
  783. /* check if device node has admission for this queue */
  784. if (!zcrypt_check_queue(perms,
  785. AP_QID_QUEUE(zq->queue->qid)))
  786. continue;
  787. /* penalty if the msg was previously sent at this qid */
  788. qpen = (tr && tr->again_counter && tr->last_qid &&
  789. tr->last_qid == zq->queue->qid) ?
  790. TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
  791. if (!zcrypt_queue_compare(zq, pref_zq,
  792. wgt + cpen + qpen, pref_wgt))
  793. continue;
  794. pref_zc = zc;
  795. pref_zq = zq;
  796. pref_wgt = wgt + cpen + qpen;
  797. }
  798. }
  799. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
  800. spin_unlock(&zcrypt_list_lock);
  801. if (!pref_zq) {
  802. pr_debug("no match for address %02x.%04x => ENODEV\n",
  803. xcrb->user_defined, *domain);
  804. rc = -ENODEV;
  805. goto out;
  806. }
  807. /* in case of auto select, provide the correct domain */
  808. qid = pref_zq->queue->qid;
  809. if (*domain == AUTOSEL_DOM)
  810. *domain = AP_QID_QUEUE(qid);
  811. rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcrb, &ap_msg);
  812. if (!rc) {
  813. print_hex_dump_debug("ccarpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
  814. ap_msg.msg, ap_msg.len, false);
  815. }
  816. spin_lock(&zcrypt_list_lock);
  817. zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
  818. spin_unlock(&zcrypt_list_lock);
  819. out:
  820. ap_release_message(&ap_msg);
  821. if (tr) {
  822. tr->last_rc = rc;
  823. tr->last_qid = qid;
  824. }
  825. trace_s390_zcrypt_rep(xcrb, func_code, rc,
  826. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  827. return rc;
  828. }
  829. long zcrypt_send_cprb(struct ica_xcRB *xcrb)
  830. {
  831. struct zcrypt_track tr;
  832. int rc;
  833. memset(&tr, 0, sizeof(tr));
  834. do {
  835. rc = _zcrypt_send_cprb(false, &ap_perms, &tr, xcrb);
  836. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  837. /* on ENODEV failure: retry once again after a requested rescan */
  838. if (rc == -ENODEV && zcrypt_process_rescan())
  839. do {
  840. rc = _zcrypt_send_cprb(false, &ap_perms, &tr, xcrb);
  841. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  842. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  843. rc = -EIO;
  844. if (rc)
  845. pr_debug("rc=%d\n", rc);
  846. return rc;
  847. }
  848. EXPORT_SYMBOL(zcrypt_send_cprb);
  849. static bool is_desired_ep11_card(unsigned int dev_id,
  850. unsigned short target_num,
  851. struct ep11_target_dev *targets)
  852. {
  853. while (target_num-- > 0) {
  854. if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
  855. return true;
  856. targets++;
  857. }
  858. return false;
  859. }
  860. static bool is_desired_ep11_queue(unsigned int dev_qid,
  861. unsigned short target_num,
  862. struct ep11_target_dev *targets)
  863. {
  864. int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
  865. while (target_num-- > 0) {
  866. if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
  867. (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
  868. return true;
  869. targets++;
  870. }
  871. return false;
  872. }
  873. static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
  874. struct zcrypt_track *tr,
  875. struct ep11_urb *xcrb)
  876. {
  877. struct zcrypt_card *zc, *pref_zc;
  878. struct zcrypt_queue *zq, *pref_zq;
  879. struct ep11_target_dev *targets;
  880. unsigned short target_num;
  881. unsigned int wgt = 0, pref_wgt = 0;
  882. unsigned int func_code, domain;
  883. struct ap_message ap_msg;
  884. int cpen, qpen, qid = 0, rc = -ENODEV;
  885. struct module *mod;
  886. trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
  887. ap_init_message(&ap_msg);
  888. target_num = (unsigned short)xcrb->targets_num;
  889. /* empty list indicates autoselect (all available targets) */
  890. targets = NULL;
  891. if (target_num != 0) {
  892. struct ep11_target_dev __user *uptr;
  893. targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
  894. if (!targets) {
  895. func_code = 0;
  896. rc = -ENOMEM;
  897. goto out;
  898. }
  899. uptr = (struct ep11_target_dev __force __user *)xcrb->targets;
  900. if (z_copy_from_user(userspace, targets, uptr,
  901. target_num * sizeof(*targets))) {
  902. func_code = 0;
  903. rc = -EFAULT;
  904. goto out_free;
  905. }
  906. }
  907. rc = prep_ep11_ap_msg(userspace, xcrb, &ap_msg, &func_code, &domain);
  908. if (rc)
  909. goto out_free;
  910. print_hex_dump_debug("ep11req: ", DUMP_PREFIX_ADDRESS, 16, 1,
  911. ap_msg.msg, ap_msg.len, false);
  912. if (perms != &ap_perms && domain < AUTOSEL_DOM) {
  913. if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
  914. if (!test_bit_inv(domain, perms->adm)) {
  915. rc = -ENODEV;
  916. goto out_free;
  917. }
  918. } else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
  919. rc = -EOPNOTSUPP;
  920. goto out_free;
  921. }
  922. }
  923. pref_zc = NULL;
  924. pref_zq = NULL;
  925. spin_lock(&zcrypt_list_lock);
  926. for_each_zcrypt_card(zc) {
  927. /* Check for usable EP11 card */
  928. if (!zc->online || !zc->card->config || zc->card->chkstop ||
  929. !zc->card->hwinfo.ep11)
  930. continue;
  931. /* Check for user selected EP11 card */
  932. if (targets &&
  933. !is_desired_ep11_card(zc->card->id, target_num, targets))
  934. continue;
  935. /* check if request size exceeds card max msg size */
  936. if (ap_msg.len > zc->card->maxmsgsize)
  937. continue;
  938. /* check if device node has admission for this card */
  939. if (!zcrypt_check_card(perms, zc->card->id))
  940. continue;
  941. /* get weight index of the card device */
  942. wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
  943. /* penalty if this msg was previously sent via this card */
  944. cpen = (tr && tr->again_counter && tr->last_qid &&
  945. AP_QID_CARD(tr->last_qid) == zc->card->id) ?
  946. TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
  947. if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
  948. continue;
  949. for_each_zcrypt_queue(zq, zc) {
  950. /* check if device is usable and eligible */
  951. if (!zq->online || !zq->ops->send_ep11_cprb ||
  952. !ap_queue_usable(zq->queue) ||
  953. (targets &&
  954. !is_desired_ep11_queue(zq->queue->qid,
  955. target_num, targets)))
  956. continue;
  957. /* check if device node has admission for this queue */
  958. if (!zcrypt_check_queue(perms,
  959. AP_QID_QUEUE(zq->queue->qid)))
  960. continue;
  961. /* penalty if the msg was previously sent at this qid */
  962. qpen = (tr && tr->again_counter && tr->last_qid &&
  963. tr->last_qid == zq->queue->qid) ?
  964. TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
  965. if (!zcrypt_queue_compare(zq, pref_zq,
  966. wgt + cpen + qpen, pref_wgt))
  967. continue;
  968. pref_zc = zc;
  969. pref_zq = zq;
  970. pref_wgt = wgt + cpen + qpen;
  971. }
  972. }
  973. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
  974. spin_unlock(&zcrypt_list_lock);
  975. if (!pref_zq) {
  976. if (targets && target_num == 1) {
  977. pr_debug("no match for address %02x.%04x => ENODEV\n",
  978. (int)targets->ap_id, (int)targets->dom_id);
  979. } else if (targets) {
  980. pr_debug("no match for %d target addrs => ENODEV\n",
  981. (int)target_num);
  982. } else {
  983. pr_debug("no match for address ff.ffff => ENODEV\n");
  984. }
  985. rc = -ENODEV;
  986. goto out_free;
  987. }
  988. qid = pref_zq->queue->qid;
  989. rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
  990. if (!rc) {
  991. print_hex_dump_debug("ep11rpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
  992. ap_msg.msg, ap_msg.len, false);
  993. }
  994. spin_lock(&zcrypt_list_lock);
  995. zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
  996. spin_unlock(&zcrypt_list_lock);
  997. out_free:
  998. kfree(targets);
  999. out:
  1000. ap_release_message(&ap_msg);
  1001. if (tr) {
  1002. tr->last_rc = rc;
  1003. tr->last_qid = qid;
  1004. }
  1005. trace_s390_zcrypt_rep(xcrb, func_code, rc,
  1006. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  1007. return rc;
  1008. }
  1009. long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
  1010. {
  1011. struct zcrypt_track tr;
  1012. int rc;
  1013. memset(&tr, 0, sizeof(tr));
  1014. do {
  1015. rc = _zcrypt_send_ep11_cprb(false, &ap_perms, &tr, xcrb);
  1016. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1017. /* on ENODEV failure: retry once again after a requested rescan */
  1018. if (rc == -ENODEV && zcrypt_process_rescan())
  1019. do {
  1020. rc = _zcrypt_send_ep11_cprb(false, &ap_perms, &tr, xcrb);
  1021. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1022. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1023. rc = -EIO;
  1024. if (rc)
  1025. pr_debug("rc=%d\n", rc);
  1026. return rc;
  1027. }
  1028. EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
  1029. static long zcrypt_rng(char *buffer)
  1030. {
  1031. struct zcrypt_card *zc, *pref_zc;
  1032. struct zcrypt_queue *zq, *pref_zq;
  1033. unsigned int wgt = 0, pref_wgt = 0;
  1034. unsigned int func_code;
  1035. struct ap_message ap_msg;
  1036. unsigned int domain;
  1037. int qid = 0, rc = -ENODEV;
  1038. struct module *mod;
  1039. trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
  1040. ap_init_message(&ap_msg);
  1041. rc = prep_rng_ap_msg(&ap_msg, &func_code, &domain);
  1042. if (rc)
  1043. goto out;
  1044. pref_zc = NULL;
  1045. pref_zq = NULL;
  1046. spin_lock(&zcrypt_list_lock);
  1047. for_each_zcrypt_card(zc) {
  1048. /* Check for usable CCA card */
  1049. if (!zc->online || !zc->card->config || zc->card->chkstop ||
  1050. !zc->card->hwinfo.cca)
  1051. continue;
  1052. /* get weight index of the card device */
  1053. wgt = zc->speed_rating[func_code];
  1054. if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
  1055. continue;
  1056. for_each_zcrypt_queue(zq, zc) {
  1057. /* check if device is usable and eligible */
  1058. if (!zq->online || !zq->ops->rng ||
  1059. !ap_queue_usable(zq->queue))
  1060. continue;
  1061. if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
  1062. continue;
  1063. pref_zc = zc;
  1064. pref_zq = zq;
  1065. pref_wgt = wgt;
  1066. }
  1067. }
  1068. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
  1069. spin_unlock(&zcrypt_list_lock);
  1070. if (!pref_zq) {
  1071. pr_debug("no matching queue found => ENODEV\n");
  1072. rc = -ENODEV;
  1073. goto out;
  1074. }
  1075. qid = pref_zq->queue->qid;
  1076. rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
  1077. spin_lock(&zcrypt_list_lock);
  1078. zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
  1079. spin_unlock(&zcrypt_list_lock);
  1080. out:
  1081. ap_release_message(&ap_msg);
  1082. trace_s390_zcrypt_rep(buffer, func_code, rc,
  1083. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  1084. return rc;
  1085. }
  1086. static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
  1087. {
  1088. struct zcrypt_card *zc;
  1089. struct zcrypt_queue *zq;
  1090. struct zcrypt_device_status *stat;
  1091. int card, queue;
  1092. memset(devstatus, 0, MAX_ZDEV_ENTRIES
  1093. * sizeof(struct zcrypt_device_status));
  1094. spin_lock(&zcrypt_list_lock);
  1095. for_each_zcrypt_card(zc) {
  1096. for_each_zcrypt_queue(zq, zc) {
  1097. card = AP_QID_CARD(zq->queue->qid);
  1098. if (card >= MAX_ZDEV_CARDIDS)
  1099. continue;
  1100. queue = AP_QID_QUEUE(zq->queue->qid);
  1101. stat = &devstatus[card * AP_DOMAINS + queue];
  1102. stat->hwtype = zc->card->ap_dev.device_type;
  1103. stat->functions = zc->card->hwinfo.fac >> 26;
  1104. stat->qid = zq->queue->qid;
  1105. stat->online = zq->online ? 0x01 : 0x00;
  1106. }
  1107. }
  1108. spin_unlock(&zcrypt_list_lock);
  1109. }
  1110. void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
  1111. {
  1112. struct zcrypt_card *zc;
  1113. struct zcrypt_queue *zq;
  1114. struct zcrypt_device_status_ext *stat;
  1115. int card, queue;
  1116. spin_lock(&zcrypt_list_lock);
  1117. for_each_zcrypt_card(zc) {
  1118. for_each_zcrypt_queue(zq, zc) {
  1119. card = AP_QID_CARD(zq->queue->qid);
  1120. queue = AP_QID_QUEUE(zq->queue->qid);
  1121. stat = &devstatus[card * AP_DOMAINS + queue];
  1122. stat->hwtype = zc->card->ap_dev.device_type;
  1123. stat->functions = zc->card->hwinfo.fac >> 26;
  1124. stat->qid = zq->queue->qid;
  1125. stat->online = zq->online ? 0x01 : 0x00;
  1126. }
  1127. }
  1128. spin_unlock(&zcrypt_list_lock);
  1129. }
  1130. EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
  1131. int zcrypt_device_status_ext(int card, int queue,
  1132. struct zcrypt_device_status_ext *devstat)
  1133. {
  1134. struct zcrypt_card *zc;
  1135. struct zcrypt_queue *zq;
  1136. memset(devstat, 0, sizeof(*devstat));
  1137. spin_lock(&zcrypt_list_lock);
  1138. for_each_zcrypt_card(zc) {
  1139. for_each_zcrypt_queue(zq, zc) {
  1140. if (card == AP_QID_CARD(zq->queue->qid) &&
  1141. queue == AP_QID_QUEUE(zq->queue->qid)) {
  1142. devstat->hwtype = zc->card->ap_dev.device_type;
  1143. devstat->functions = zc->card->hwinfo.fac >> 26;
  1144. devstat->qid = zq->queue->qid;
  1145. devstat->online = zq->online ? 0x01 : 0x00;
  1146. spin_unlock(&zcrypt_list_lock);
  1147. return 0;
  1148. }
  1149. }
  1150. }
  1151. spin_unlock(&zcrypt_list_lock);
  1152. return -ENODEV;
  1153. }
  1154. EXPORT_SYMBOL(zcrypt_device_status_ext);
  1155. static void zcrypt_status_mask(char status[], size_t max_adapters)
  1156. {
  1157. struct zcrypt_card *zc;
  1158. struct zcrypt_queue *zq;
  1159. int card;
  1160. memset(status, 0, max_adapters);
  1161. spin_lock(&zcrypt_list_lock);
  1162. for_each_zcrypt_card(zc) {
  1163. for_each_zcrypt_queue(zq, zc) {
  1164. card = AP_QID_CARD(zq->queue->qid);
  1165. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
  1166. card >= max_adapters)
  1167. continue;
  1168. status[card] = zc->online ? zc->user_space_type : 0x0d;
  1169. }
  1170. }
  1171. spin_unlock(&zcrypt_list_lock);
  1172. }
  1173. static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
  1174. {
  1175. struct zcrypt_card *zc;
  1176. struct zcrypt_queue *zq;
  1177. int card;
  1178. memset(qdepth, 0, max_adapters);
  1179. spin_lock(&zcrypt_list_lock);
  1180. local_bh_disable();
  1181. for_each_zcrypt_card(zc) {
  1182. for_each_zcrypt_queue(zq, zc) {
  1183. card = AP_QID_CARD(zq->queue->qid);
  1184. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
  1185. card >= max_adapters)
  1186. continue;
  1187. spin_lock(&zq->queue->lock);
  1188. qdepth[card] =
  1189. zq->queue->pendingq_count +
  1190. zq->queue->requestq_count;
  1191. spin_unlock(&zq->queue->lock);
  1192. }
  1193. }
  1194. local_bh_enable();
  1195. spin_unlock(&zcrypt_list_lock);
  1196. }
  1197. static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
  1198. {
  1199. struct zcrypt_card *zc;
  1200. struct zcrypt_queue *zq;
  1201. int card;
  1202. u64 cnt;
  1203. memset(reqcnt, 0, sizeof(int) * max_adapters);
  1204. spin_lock(&zcrypt_list_lock);
  1205. local_bh_disable();
  1206. for_each_zcrypt_card(zc) {
  1207. for_each_zcrypt_queue(zq, zc) {
  1208. card = AP_QID_CARD(zq->queue->qid);
  1209. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
  1210. card >= max_adapters)
  1211. continue;
  1212. spin_lock(&zq->queue->lock);
  1213. cnt = zq->queue->total_request_count;
  1214. spin_unlock(&zq->queue->lock);
  1215. reqcnt[card] = (cnt < UINT_MAX) ? (u32)cnt : UINT_MAX;
  1216. }
  1217. }
  1218. local_bh_enable();
  1219. spin_unlock(&zcrypt_list_lock);
  1220. }
  1221. static int zcrypt_pendingq_count(void)
  1222. {
  1223. struct zcrypt_card *zc;
  1224. struct zcrypt_queue *zq;
  1225. int pendingq_count;
  1226. pendingq_count = 0;
  1227. spin_lock(&zcrypt_list_lock);
  1228. local_bh_disable();
  1229. for_each_zcrypt_card(zc) {
  1230. for_each_zcrypt_queue(zq, zc) {
  1231. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1232. continue;
  1233. spin_lock(&zq->queue->lock);
  1234. pendingq_count += zq->queue->pendingq_count;
  1235. spin_unlock(&zq->queue->lock);
  1236. }
  1237. }
  1238. local_bh_enable();
  1239. spin_unlock(&zcrypt_list_lock);
  1240. return pendingq_count;
  1241. }
  1242. static int zcrypt_requestq_count(void)
  1243. {
  1244. struct zcrypt_card *zc;
  1245. struct zcrypt_queue *zq;
  1246. int requestq_count;
  1247. requestq_count = 0;
  1248. spin_lock(&zcrypt_list_lock);
  1249. local_bh_disable();
  1250. for_each_zcrypt_card(zc) {
  1251. for_each_zcrypt_queue(zq, zc) {
  1252. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1253. continue;
  1254. spin_lock(&zq->queue->lock);
  1255. requestq_count += zq->queue->requestq_count;
  1256. spin_unlock(&zq->queue->lock);
  1257. }
  1258. }
  1259. local_bh_enable();
  1260. spin_unlock(&zcrypt_list_lock);
  1261. return requestq_count;
  1262. }
  1263. static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
  1264. {
  1265. int rc;
  1266. struct zcrypt_track tr;
  1267. struct ica_rsa_modexpo mex;
  1268. struct ica_rsa_modexpo __user *umex = (void __user *)arg;
  1269. memset(&tr, 0, sizeof(tr));
  1270. if (copy_from_user(&mex, umex, sizeof(mex)))
  1271. return -EFAULT;
  1272. do {
  1273. rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
  1274. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1275. /* on ENODEV failure: retry once again after a requested rescan */
  1276. if (rc == -ENODEV && zcrypt_process_rescan())
  1277. do {
  1278. rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
  1279. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1280. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1281. rc = -EIO;
  1282. if (rc) {
  1283. pr_debug("ioctl ICARSAMODEXPO rc=%d\n", rc);
  1284. return rc;
  1285. }
  1286. return put_user(mex.outputdatalength, &umex->outputdatalength);
  1287. }
  1288. static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
  1289. {
  1290. int rc;
  1291. struct zcrypt_track tr;
  1292. struct ica_rsa_modexpo_crt crt;
  1293. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *)arg;
  1294. memset(&tr, 0, sizeof(tr));
  1295. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  1296. return -EFAULT;
  1297. do {
  1298. rc = zcrypt_rsa_crt(perms, &tr, &crt);
  1299. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1300. /* on ENODEV failure: retry once again after a requested rescan */
  1301. if (rc == -ENODEV && zcrypt_process_rescan())
  1302. do {
  1303. rc = zcrypt_rsa_crt(perms, &tr, &crt);
  1304. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1305. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1306. rc = -EIO;
  1307. if (rc) {
  1308. pr_debug("ioctl ICARSACRT rc=%d\n", rc);
  1309. return rc;
  1310. }
  1311. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  1312. }
  1313. static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
  1314. {
  1315. int rc;
  1316. struct ica_xcRB xcrb;
  1317. struct zcrypt_track tr;
  1318. struct ica_xcRB __user *uxcrb = (void __user *)arg;
  1319. memset(&tr, 0, sizeof(tr));
  1320. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  1321. return -EFAULT;
  1322. do {
  1323. rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb);
  1324. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1325. /* on ENODEV failure: retry once again after a requested rescan */
  1326. if (rc == -ENODEV && zcrypt_process_rescan())
  1327. do {
  1328. rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb);
  1329. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1330. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1331. rc = -EIO;
  1332. if (rc)
  1333. pr_debug("ioctl ZSENDCPRB rc=%d status=0x%x\n",
  1334. rc, xcrb.status);
  1335. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  1336. return -EFAULT;
  1337. return rc;
  1338. }
  1339. static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
  1340. {
  1341. int rc;
  1342. struct ep11_urb xcrb;
  1343. struct zcrypt_track tr;
  1344. struct ep11_urb __user *uxcrb = (void __user *)arg;
  1345. memset(&tr, 0, sizeof(tr));
  1346. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  1347. return -EFAULT;
  1348. do {
  1349. rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
  1350. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1351. /* on ENODEV failure: retry once again after a requested rescan */
  1352. if (rc == -ENODEV && zcrypt_process_rescan())
  1353. do {
  1354. rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
  1355. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1356. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1357. rc = -EIO;
  1358. if (rc)
  1359. pr_debug("ioctl ZSENDEP11CPRB rc=%d\n", rc);
  1360. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  1361. return -EFAULT;
  1362. return rc;
  1363. }
  1364. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  1365. unsigned long arg)
  1366. {
  1367. int rc;
  1368. struct ap_perms *perms =
  1369. (struct ap_perms *)filp->private_data;
  1370. rc = zcrypt_check_ioctl(perms, cmd);
  1371. if (rc)
  1372. return rc;
  1373. switch (cmd) {
  1374. case ICARSAMODEXPO:
  1375. return icarsamodexpo_ioctl(perms, arg);
  1376. case ICARSACRT:
  1377. return icarsacrt_ioctl(perms, arg);
  1378. case ZSECSENDCPRB:
  1379. return zsecsendcprb_ioctl(perms, arg);
  1380. case ZSENDEP11CPRB:
  1381. return zsendep11cprb_ioctl(perms, arg);
  1382. case ZCRYPT_DEVICE_STATUS: {
  1383. struct zcrypt_device_status_ext *device_status;
  1384. size_t total_size = MAX_ZDEV_ENTRIES_EXT
  1385. * sizeof(struct zcrypt_device_status_ext);
  1386. device_status = kvcalloc(MAX_ZDEV_ENTRIES_EXT,
  1387. sizeof(struct zcrypt_device_status_ext),
  1388. GFP_KERNEL);
  1389. if (!device_status)
  1390. return -ENOMEM;
  1391. zcrypt_device_status_mask_ext(device_status);
  1392. if (copy_to_user((char __user *)arg, device_status,
  1393. total_size))
  1394. rc = -EFAULT;
  1395. kvfree(device_status);
  1396. return rc;
  1397. }
  1398. case ZCRYPT_STATUS_MASK: {
  1399. char status[AP_DEVICES];
  1400. zcrypt_status_mask(status, AP_DEVICES);
  1401. if (copy_to_user((char __user *)arg, status, sizeof(status)))
  1402. return -EFAULT;
  1403. return 0;
  1404. }
  1405. case ZCRYPT_QDEPTH_MASK: {
  1406. char qdepth[AP_DEVICES];
  1407. zcrypt_qdepth_mask(qdepth, AP_DEVICES);
  1408. if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
  1409. return -EFAULT;
  1410. return 0;
  1411. }
  1412. case ZCRYPT_PERDEV_REQCNT: {
  1413. u32 *reqcnt;
  1414. reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
  1415. if (!reqcnt)
  1416. return -ENOMEM;
  1417. zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
  1418. if (copy_to_user((int __user *)arg, reqcnt,
  1419. sizeof(u32) * AP_DEVICES))
  1420. rc = -EFAULT;
  1421. kfree(reqcnt);
  1422. return rc;
  1423. }
  1424. case Z90STAT_REQUESTQ_COUNT:
  1425. return put_user(zcrypt_requestq_count(), (int __user *)arg);
  1426. case Z90STAT_PENDINGQ_COUNT:
  1427. return put_user(zcrypt_pendingq_count(), (int __user *)arg);
  1428. case Z90STAT_TOTALOPEN_COUNT:
  1429. return put_user(atomic_read(&zcrypt_open_count),
  1430. (int __user *)arg);
  1431. case Z90STAT_DOMAIN_INDEX:
  1432. return put_user(ap_domain_index, (int __user *)arg);
  1433. /*
  1434. * Deprecated ioctls
  1435. */
  1436. case ZDEVICESTATUS: {
  1437. /* the old ioctl supports only 64 adapters */
  1438. struct zcrypt_device_status *device_status;
  1439. size_t total_size = MAX_ZDEV_ENTRIES
  1440. * sizeof(struct zcrypt_device_status);
  1441. device_status = kzalloc(total_size, GFP_KERNEL);
  1442. if (!device_status)
  1443. return -ENOMEM;
  1444. zcrypt_device_status_mask(device_status);
  1445. if (copy_to_user((char __user *)arg, device_status,
  1446. total_size))
  1447. rc = -EFAULT;
  1448. kfree(device_status);
  1449. return rc;
  1450. }
  1451. case Z90STAT_STATUS_MASK: {
  1452. /* the old ioctl supports only 64 adapters */
  1453. char status[MAX_ZDEV_CARDIDS];
  1454. zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
  1455. if (copy_to_user((char __user *)arg, status, sizeof(status)))
  1456. return -EFAULT;
  1457. return 0;
  1458. }
  1459. case Z90STAT_QDEPTH_MASK: {
  1460. /* the old ioctl supports only 64 adapters */
  1461. char qdepth[MAX_ZDEV_CARDIDS];
  1462. zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
  1463. if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
  1464. return -EFAULT;
  1465. return 0;
  1466. }
  1467. case Z90STAT_PERDEV_REQCNT: {
  1468. /* the old ioctl supports only 64 adapters */
  1469. u32 reqcnt[MAX_ZDEV_CARDIDS];
  1470. zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
  1471. if (copy_to_user((int __user *)arg, reqcnt, sizeof(reqcnt)))
  1472. return -EFAULT;
  1473. return 0;
  1474. }
  1475. /* unknown ioctl number */
  1476. default:
  1477. pr_debug("unknown ioctl 0x%08x\n", cmd);
  1478. return -ENOIOCTLCMD;
  1479. }
  1480. }
  1481. #ifdef CONFIG_COMPAT
  1482. /*
  1483. * ioctl32 conversion routines
  1484. */
  1485. struct compat_ica_rsa_modexpo {
  1486. compat_uptr_t inputdata;
  1487. unsigned int inputdatalength;
  1488. compat_uptr_t outputdata;
  1489. unsigned int outputdatalength;
  1490. compat_uptr_t b_key;
  1491. compat_uptr_t n_modulus;
  1492. };
  1493. static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
  1494. unsigned int cmd, unsigned long arg)
  1495. {
  1496. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  1497. struct compat_ica_rsa_modexpo mex32;
  1498. struct ica_rsa_modexpo mex64;
  1499. struct zcrypt_track tr;
  1500. long rc;
  1501. memset(&tr, 0, sizeof(tr));
  1502. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  1503. return -EFAULT;
  1504. mex64.inputdata = compat_ptr(mex32.inputdata);
  1505. mex64.inputdatalength = mex32.inputdatalength;
  1506. mex64.outputdata = compat_ptr(mex32.outputdata);
  1507. mex64.outputdatalength = mex32.outputdatalength;
  1508. mex64.b_key = compat_ptr(mex32.b_key);
  1509. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  1510. do {
  1511. rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
  1512. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1513. /* on ENODEV failure: retry once again after a requested rescan */
  1514. if (rc == -ENODEV && zcrypt_process_rescan())
  1515. do {
  1516. rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
  1517. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1518. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1519. rc = -EIO;
  1520. if (rc)
  1521. return rc;
  1522. return put_user(mex64.outputdatalength,
  1523. &umex32->outputdatalength);
  1524. }
  1525. struct compat_ica_rsa_modexpo_crt {
  1526. compat_uptr_t inputdata;
  1527. unsigned int inputdatalength;
  1528. compat_uptr_t outputdata;
  1529. unsigned int outputdatalength;
  1530. compat_uptr_t bp_key;
  1531. compat_uptr_t bq_key;
  1532. compat_uptr_t np_prime;
  1533. compat_uptr_t nq_prime;
  1534. compat_uptr_t u_mult_inv;
  1535. };
  1536. static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
  1537. unsigned int cmd, unsigned long arg)
  1538. {
  1539. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  1540. struct compat_ica_rsa_modexpo_crt crt32;
  1541. struct ica_rsa_modexpo_crt crt64;
  1542. struct zcrypt_track tr;
  1543. long rc;
  1544. memset(&tr, 0, sizeof(tr));
  1545. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  1546. return -EFAULT;
  1547. crt64.inputdata = compat_ptr(crt32.inputdata);
  1548. crt64.inputdatalength = crt32.inputdatalength;
  1549. crt64.outputdata = compat_ptr(crt32.outputdata);
  1550. crt64.outputdatalength = crt32.outputdatalength;
  1551. crt64.bp_key = compat_ptr(crt32.bp_key);
  1552. crt64.bq_key = compat_ptr(crt32.bq_key);
  1553. crt64.np_prime = compat_ptr(crt32.np_prime);
  1554. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  1555. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  1556. do {
  1557. rc = zcrypt_rsa_crt(perms, &tr, &crt64);
  1558. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1559. /* on ENODEV failure: retry once again after a requested rescan */
  1560. if (rc == -ENODEV && zcrypt_process_rescan())
  1561. do {
  1562. rc = zcrypt_rsa_crt(perms, &tr, &crt64);
  1563. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1564. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1565. rc = -EIO;
  1566. if (rc)
  1567. return rc;
  1568. return put_user(crt64.outputdatalength,
  1569. &ucrt32->outputdatalength);
  1570. }
  1571. struct compat_ica_xcrb {
  1572. unsigned short agent_ID;
  1573. unsigned int user_defined;
  1574. unsigned short request_ID;
  1575. unsigned int request_control_blk_length;
  1576. unsigned char padding1[16 - sizeof(compat_uptr_t)];
  1577. compat_uptr_t request_control_blk_addr;
  1578. unsigned int request_data_length;
  1579. char padding2[16 - sizeof(compat_uptr_t)];
  1580. compat_uptr_t request_data_address;
  1581. unsigned int reply_control_blk_length;
  1582. char padding3[16 - sizeof(compat_uptr_t)];
  1583. compat_uptr_t reply_control_blk_addr;
  1584. unsigned int reply_data_length;
  1585. char padding4[16 - sizeof(compat_uptr_t)];
  1586. compat_uptr_t reply_data_addr;
  1587. unsigned short priority_window;
  1588. unsigned int status;
  1589. } __packed;
  1590. static long trans_xcrb32(struct ap_perms *perms, struct file *filp,
  1591. unsigned int cmd, unsigned long arg)
  1592. {
  1593. struct compat_ica_xcrb __user *uxcrb32 = compat_ptr(arg);
  1594. struct compat_ica_xcrb xcrb32;
  1595. struct zcrypt_track tr;
  1596. struct ica_xcRB xcrb64;
  1597. long rc;
  1598. memset(&tr, 0, sizeof(tr));
  1599. if (copy_from_user(&xcrb32, uxcrb32, sizeof(xcrb32)))
  1600. return -EFAULT;
  1601. xcrb64.agent_ID = xcrb32.agent_ID;
  1602. xcrb64.user_defined = xcrb32.user_defined;
  1603. xcrb64.request_ID = xcrb32.request_ID;
  1604. xcrb64.request_control_blk_length =
  1605. xcrb32.request_control_blk_length;
  1606. xcrb64.request_control_blk_addr =
  1607. compat_ptr(xcrb32.request_control_blk_addr);
  1608. xcrb64.request_data_length =
  1609. xcrb32.request_data_length;
  1610. xcrb64.request_data_address =
  1611. compat_ptr(xcrb32.request_data_address);
  1612. xcrb64.reply_control_blk_length =
  1613. xcrb32.reply_control_blk_length;
  1614. xcrb64.reply_control_blk_addr =
  1615. compat_ptr(xcrb32.reply_control_blk_addr);
  1616. xcrb64.reply_data_length = xcrb32.reply_data_length;
  1617. xcrb64.reply_data_addr =
  1618. compat_ptr(xcrb32.reply_data_addr);
  1619. xcrb64.priority_window = xcrb32.priority_window;
  1620. xcrb64.status = xcrb32.status;
  1621. do {
  1622. rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb64);
  1623. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1624. /* on ENODEV failure: retry once again after a requested rescan */
  1625. if (rc == -ENODEV && zcrypt_process_rescan())
  1626. do {
  1627. rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb64);
  1628. } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
  1629. if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
  1630. rc = -EIO;
  1631. xcrb32.reply_control_blk_length = xcrb64.reply_control_blk_length;
  1632. xcrb32.reply_data_length = xcrb64.reply_data_length;
  1633. xcrb32.status = xcrb64.status;
  1634. if (copy_to_user(uxcrb32, &xcrb32, sizeof(xcrb32)))
  1635. return -EFAULT;
  1636. return rc;
  1637. }
  1638. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  1639. unsigned long arg)
  1640. {
  1641. int rc;
  1642. struct ap_perms *perms =
  1643. (struct ap_perms *)filp->private_data;
  1644. rc = zcrypt_check_ioctl(perms, cmd);
  1645. if (rc)
  1646. return rc;
  1647. if (cmd == ICARSAMODEXPO)
  1648. return trans_modexpo32(perms, filp, cmd, arg);
  1649. if (cmd == ICARSACRT)
  1650. return trans_modexpo_crt32(perms, filp, cmd, arg);
  1651. if (cmd == ZSECSENDCPRB)
  1652. return trans_xcrb32(perms, filp, cmd, arg);
  1653. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  1654. }
  1655. #endif
  1656. /*
  1657. * Misc device file operations.
  1658. */
  1659. static const struct file_operations zcrypt_fops = {
  1660. .owner = THIS_MODULE,
  1661. .read = zcrypt_read,
  1662. .write = zcrypt_write,
  1663. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  1664. #ifdef CONFIG_COMPAT
  1665. .compat_ioctl = zcrypt_compat_ioctl,
  1666. #endif
  1667. .open = zcrypt_open,
  1668. .release = zcrypt_release,
  1669. };
  1670. /*
  1671. * Misc device.
  1672. */
  1673. static struct miscdevice zcrypt_misc_device = {
  1674. .minor = MISC_DYNAMIC_MINOR,
  1675. .name = "z90crypt",
  1676. .fops = &zcrypt_fops,
  1677. };
  1678. static int zcrypt_rng_device_count;
  1679. static u32 *zcrypt_rng_buffer;
  1680. static int zcrypt_rng_buffer_index;
  1681. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1682. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1683. {
  1684. int rc;
  1685. /*
  1686. * We don't need locking here because the RNG API guarantees serialized
  1687. * read method calls.
  1688. */
  1689. if (zcrypt_rng_buffer_index == 0) {
  1690. rc = zcrypt_rng((char *)zcrypt_rng_buffer);
  1691. /* on ENODEV failure: retry once again after an AP bus rescan */
  1692. if (rc == -ENODEV && zcrypt_process_rescan())
  1693. rc = zcrypt_rng((char *)zcrypt_rng_buffer);
  1694. if (rc < 0)
  1695. return -EIO;
  1696. zcrypt_rng_buffer_index = rc / sizeof(*data);
  1697. }
  1698. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1699. return sizeof(*data);
  1700. }
  1701. static struct hwrng zcrypt_rng_dev = {
  1702. .name = "zcrypt",
  1703. .data_read = zcrypt_rng_data_read,
  1704. .quality = 990,
  1705. };
  1706. int zcrypt_rng_device_add(void)
  1707. {
  1708. int rc = 0;
  1709. mutex_lock(&zcrypt_rng_mutex);
  1710. if (zcrypt_rng_device_count == 0) {
  1711. zcrypt_rng_buffer = (u32 *)get_zeroed_page(GFP_KERNEL);
  1712. if (!zcrypt_rng_buffer) {
  1713. rc = -ENOMEM;
  1714. goto out;
  1715. }
  1716. zcrypt_rng_buffer_index = 0;
  1717. rc = hwrng_register(&zcrypt_rng_dev);
  1718. if (rc)
  1719. goto out_free;
  1720. zcrypt_rng_device_count = 1;
  1721. } else {
  1722. zcrypt_rng_device_count++;
  1723. }
  1724. mutex_unlock(&zcrypt_rng_mutex);
  1725. return 0;
  1726. out_free:
  1727. free_page((unsigned long)zcrypt_rng_buffer);
  1728. out:
  1729. mutex_unlock(&zcrypt_rng_mutex);
  1730. return rc;
  1731. }
  1732. void zcrypt_rng_device_remove(void)
  1733. {
  1734. mutex_lock(&zcrypt_rng_mutex);
  1735. zcrypt_rng_device_count--;
  1736. if (zcrypt_rng_device_count == 0) {
  1737. hwrng_unregister(&zcrypt_rng_dev);
  1738. free_page((unsigned long)zcrypt_rng_buffer);
  1739. }
  1740. mutex_unlock(&zcrypt_rng_mutex);
  1741. }
  1742. /*
  1743. * Wait until the zcrypt api is operational.
  1744. * The AP bus scan and the binding of ap devices to device drivers is
  1745. * an asynchronous job. This function waits until these initial jobs
  1746. * are done and so the zcrypt api should be ready to serve crypto
  1747. * requests - if there are resources available. The function uses an
  1748. * internal timeout of 30s. The very first caller will either wait for
  1749. * ap bus bindings complete or the timeout happens. This state will be
  1750. * remembered for further callers which will only be blocked until a
  1751. * decision is made (timeout or bindings complete).
  1752. * On timeout -ETIME is returned, on success the return value is 0.
  1753. */
  1754. int zcrypt_wait_api_operational(void)
  1755. {
  1756. static DEFINE_MUTEX(zcrypt_wait_api_lock);
  1757. static int zcrypt_wait_api_state;
  1758. int rc;
  1759. rc = mutex_lock_interruptible(&zcrypt_wait_api_lock);
  1760. if (rc)
  1761. return rc;
  1762. switch (zcrypt_wait_api_state) {
  1763. case 0:
  1764. /* initial state, invoke wait for the ap bus complete */
  1765. rc = ap_wait_apqn_bindings_complete(
  1766. msecs_to_jiffies(ZCRYPT_WAIT_BINDINGS_COMPLETE_MS));
  1767. switch (rc) {
  1768. case 0:
  1769. /* ap bus bindings are complete */
  1770. zcrypt_wait_api_state = 1;
  1771. break;
  1772. case -EINTR:
  1773. /* interrupted, go back to caller */
  1774. break;
  1775. case -ETIME:
  1776. /* timeout */
  1777. ZCRYPT_DBF_WARN("%s ap_wait_init_apqn_bindings_complete()=ETIME\n",
  1778. __func__);
  1779. zcrypt_wait_api_state = -ETIME;
  1780. break;
  1781. default:
  1782. /* other failure */
  1783. pr_debug("ap_wait_init_apqn_bindings_complete()=%d\n", rc);
  1784. break;
  1785. }
  1786. break;
  1787. case 1:
  1788. /* a previous caller already found ap bus bindings complete */
  1789. rc = 0;
  1790. break;
  1791. default:
  1792. /* a previous caller had timeout or other failure */
  1793. rc = zcrypt_wait_api_state;
  1794. break;
  1795. }
  1796. mutex_unlock(&zcrypt_wait_api_lock);
  1797. return rc;
  1798. }
  1799. EXPORT_SYMBOL(zcrypt_wait_api_operational);
  1800. int __init zcrypt_debug_init(void)
  1801. {
  1802. zcrypt_dbf_info = debug_register("zcrypt", 2, 1,
  1803. ZCRYPT_DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1804. debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
  1805. debug_set_level(zcrypt_dbf_info, DBF_ERR);
  1806. return 0;
  1807. }
  1808. void zcrypt_debug_exit(void)
  1809. {
  1810. debug_unregister(zcrypt_dbf_info);
  1811. }
  1812. static int __init zcdn_init(void)
  1813. {
  1814. int rc;
  1815. /* create a new class 'zcrypt' */
  1816. rc = class_register(&zcrypt_class);
  1817. if (rc)
  1818. goto out_class_register_failed;
  1819. /* alloc device minor range */
  1820. rc = alloc_chrdev_region(&zcrypt_devt,
  1821. 0, ZCRYPT_MAX_MINOR_NODES,
  1822. ZCRYPT_NAME);
  1823. if (rc)
  1824. goto out_alloc_chrdev_failed;
  1825. cdev_init(&zcrypt_cdev, &zcrypt_fops);
  1826. zcrypt_cdev.owner = THIS_MODULE;
  1827. rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
  1828. if (rc)
  1829. goto out_cdev_add_failed;
  1830. /* need some class specific sysfs attributes */
  1831. rc = class_create_file(&zcrypt_class, &class_attr_zcdn_create);
  1832. if (rc)
  1833. goto out_class_create_file_1_failed;
  1834. rc = class_create_file(&zcrypt_class, &class_attr_zcdn_destroy);
  1835. if (rc)
  1836. goto out_class_create_file_2_failed;
  1837. return 0;
  1838. out_class_create_file_2_failed:
  1839. class_remove_file(&zcrypt_class, &class_attr_zcdn_create);
  1840. out_class_create_file_1_failed:
  1841. cdev_del(&zcrypt_cdev);
  1842. out_cdev_add_failed:
  1843. unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
  1844. out_alloc_chrdev_failed:
  1845. class_unregister(&zcrypt_class);
  1846. out_class_register_failed:
  1847. return rc;
  1848. }
  1849. static void zcdn_exit(void)
  1850. {
  1851. class_remove_file(&zcrypt_class, &class_attr_zcdn_create);
  1852. class_remove_file(&zcrypt_class, &class_attr_zcdn_destroy);
  1853. zcdn_destroy_all();
  1854. cdev_del(&zcrypt_cdev);
  1855. unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
  1856. class_unregister(&zcrypt_class);
  1857. }
  1858. /*
  1859. * zcrypt_api_init(): Module initialization.
  1860. *
  1861. * The module initialization code.
  1862. */
  1863. int __init zcrypt_api_init(void)
  1864. {
  1865. int rc;
  1866. rc = zcrypt_debug_init();
  1867. if (rc)
  1868. goto out;
  1869. rc = zcdn_init();
  1870. if (rc)
  1871. goto out;
  1872. /* Register the request sprayer. */
  1873. rc = misc_register(&zcrypt_misc_device);
  1874. if (rc < 0)
  1875. goto out_misc_register_failed;
  1876. zcrypt_msgtype6_init();
  1877. zcrypt_msgtype50_init();
  1878. return 0;
  1879. out_misc_register_failed:
  1880. zcdn_exit();
  1881. zcrypt_debug_exit();
  1882. out:
  1883. return rc;
  1884. }
  1885. /*
  1886. * zcrypt_api_exit(): Module termination.
  1887. *
  1888. * The module termination code.
  1889. */
  1890. void __exit zcrypt_api_exit(void)
  1891. {
  1892. zcdn_exit();
  1893. misc_deregister(&zcrypt_misc_device);
  1894. zcrypt_msgtype6_exit();
  1895. zcrypt_msgtype50_exit();
  1896. zcrypt_ccamisc_exit();
  1897. zcrypt_ep11misc_exit();
  1898. zcrypt_debug_exit();
  1899. }
  1900. module_init(zcrypt_api_init);
  1901. module_exit(zcrypt_api_exit);