zcrypt_api.c 59 KB

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