zcrypt_api.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * zcrypt 2.2.0
  4. *
  5. * Copyright IBM Corp. 2001, 2018
  6. * Author(s): Robert Burroughs
  7. * Eric Rossman (edrossma@us.ibm.com)
  8. * Cornelia Huck <cornelia.huck@de.ibm.com>
  9. *
  10. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  11. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  12. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  13. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  14. * Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
  15. */
  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 <asm/debug.h>
  30. #define CREATE_TRACE_POINTS
  31. #include <asm/trace/zcrypt.h>
  32. #include "zcrypt_api.h"
  33. #include "zcrypt_debug.h"
  34. #include "zcrypt_msgtype6.h"
  35. #include "zcrypt_msgtype50.h"
  36. /*
  37. * Module description.
  38. */
  39. MODULE_AUTHOR("IBM Corporation");
  40. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  41. "Copyright IBM Corp. 2001, 2012");
  42. MODULE_LICENSE("GPL");
  43. /*
  44. * zcrypt tracepoint functions
  45. */
  46. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
  47. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
  48. static int zcrypt_hwrng_seed = 1;
  49. module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
  50. MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
  51. DEFINE_SPINLOCK(zcrypt_list_lock);
  52. LIST_HEAD(zcrypt_card_list);
  53. int zcrypt_device_count;
  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(DBF_INFO, "rescan count=%07d\n",
  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. /* helper function, matches the name for find_zcdndev_by_name() */
  113. static int __match_zcdn_name(struct device *dev, const void *data)
  114. {
  115. return strcmp(dev_name(dev), (const char *)data) == 0;
  116. }
  117. /* helper function, matches the devt value for find_zcdndev_by_devt() */
  118. static int __match_zcdn_devt(struct device *dev, const void *data)
  119. {
  120. return dev->devt == *((dev_t *) data);
  121. }
  122. /*
  123. * Find zcdn device by name.
  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_name(const char *name)
  128. {
  129. struct device *dev =
  130. class_find_device(zcrypt_class, NULL,
  131. (void *) name,
  132. __match_zcdn_name);
  133. return dev ? to_zcdn_dev(dev) : NULL;
  134. }
  135. /*
  136. * Find zcdn device by devt value.
  137. * Returns reference to the zcdn device which needs to be released
  138. * with put_device() after use.
  139. */
  140. static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
  141. {
  142. struct device *dev =
  143. class_find_device(zcrypt_class, NULL,
  144. (void *) &devt,
  145. __match_zcdn_devt);
  146. return dev ? to_zcdn_dev(dev) : NULL;
  147. }
  148. static ssize_t ioctlmask_show(struct device *dev,
  149. struct device_attribute *attr,
  150. char *buf)
  151. {
  152. int i, rc;
  153. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  154. if (mutex_lock_interruptible(&ap_perms_mutex))
  155. return -ERESTARTSYS;
  156. buf[0] = '0';
  157. buf[1] = 'x';
  158. for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
  159. snprintf(buf + 2 + 2 * i * sizeof(long),
  160. PAGE_SIZE - 2 - 2 * i * sizeof(long),
  161. "%016lx", zcdndev->perms.ioctlm[i]);
  162. buf[2 + 2 * i * sizeof(long)] = '\n';
  163. buf[2 + 2 * i * sizeof(long) + 1] = '\0';
  164. rc = 2 + 2 * i * sizeof(long) + 1;
  165. mutex_unlock(&ap_perms_mutex);
  166. return rc;
  167. }
  168. static ssize_t ioctlmask_store(struct device *dev,
  169. struct device_attribute *attr,
  170. const char *buf, size_t count)
  171. {
  172. int rc;
  173. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  174. rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
  175. AP_IOCTLS, &ap_perms_mutex);
  176. if (rc)
  177. return rc;
  178. return count;
  179. }
  180. static DEVICE_ATTR_RW(ioctlmask);
  181. static ssize_t apmask_show(struct device *dev,
  182. struct device_attribute *attr,
  183. char *buf)
  184. {
  185. int i, rc;
  186. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  187. if (mutex_lock_interruptible(&ap_perms_mutex))
  188. return -ERESTARTSYS;
  189. buf[0] = '0';
  190. buf[1] = 'x';
  191. for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
  192. snprintf(buf + 2 + 2 * i * sizeof(long),
  193. PAGE_SIZE - 2 - 2 * i * sizeof(long),
  194. "%016lx", zcdndev->perms.apm[i]);
  195. buf[2 + 2 * i * sizeof(long)] = '\n';
  196. buf[2 + 2 * i * sizeof(long) + 1] = '\0';
  197. rc = 2 + 2 * i * sizeof(long) + 1;
  198. mutex_unlock(&ap_perms_mutex);
  199. return rc;
  200. }
  201. static ssize_t apmask_store(struct device *dev,
  202. struct device_attribute *attr,
  203. const char *buf, size_t count)
  204. {
  205. int rc;
  206. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  207. rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
  208. AP_DEVICES, &ap_perms_mutex);
  209. if (rc)
  210. return rc;
  211. return count;
  212. }
  213. static DEVICE_ATTR_RW(apmask);
  214. static ssize_t aqmask_show(struct device *dev,
  215. struct device_attribute *attr,
  216. char *buf)
  217. {
  218. int i, rc;
  219. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  220. if (mutex_lock_interruptible(&ap_perms_mutex))
  221. return -ERESTARTSYS;
  222. buf[0] = '0';
  223. buf[1] = 'x';
  224. for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
  225. snprintf(buf + 2 + 2 * i * sizeof(long),
  226. PAGE_SIZE - 2 - 2 * i * sizeof(long),
  227. "%016lx", zcdndev->perms.aqm[i]);
  228. buf[2 + 2 * i * sizeof(long)] = '\n';
  229. buf[2 + 2 * i * sizeof(long) + 1] = '\0';
  230. rc = 2 + 2 * i * sizeof(long) + 1;
  231. mutex_unlock(&ap_perms_mutex);
  232. return rc;
  233. }
  234. static ssize_t aqmask_store(struct device *dev,
  235. struct device_attribute *attr,
  236. const char *buf, size_t count)
  237. {
  238. int rc;
  239. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  240. rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
  241. AP_DOMAINS, &ap_perms_mutex);
  242. if (rc)
  243. return rc;
  244. return count;
  245. }
  246. static DEVICE_ATTR_RW(aqmask);
  247. static struct attribute *zcdn_dev_attrs[] = {
  248. &dev_attr_ioctlmask.attr,
  249. &dev_attr_apmask.attr,
  250. &dev_attr_aqmask.attr,
  251. NULL
  252. };
  253. static struct attribute_group zcdn_dev_attr_group = {
  254. .attrs = zcdn_dev_attrs
  255. };
  256. static const struct attribute_group *zcdn_dev_attr_groups[] = {
  257. &zcdn_dev_attr_group,
  258. NULL
  259. };
  260. static ssize_t zcdn_create_store(struct class *class,
  261. struct class_attribute *attr,
  262. const char *buf, size_t count)
  263. {
  264. int rc;
  265. char name[ZCDN_MAX_NAME];
  266. strncpy(name, skip_spaces(buf), sizeof(name));
  267. name[sizeof(name) - 1] = '\0';
  268. rc = zcdn_create(strim(name));
  269. return rc ? rc : count;
  270. }
  271. static const struct class_attribute class_attr_zcdn_create =
  272. __ATTR(create, 0600, NULL, zcdn_create_store);
  273. static ssize_t zcdn_destroy_store(struct class *class,
  274. struct class_attribute *attr,
  275. const char *buf, size_t count)
  276. {
  277. int rc;
  278. char name[ZCDN_MAX_NAME];
  279. strncpy(name, skip_spaces(buf), sizeof(name));
  280. name[sizeof(name) - 1] = '\0';
  281. rc = zcdn_destroy(strim(name));
  282. return rc ? rc : count;
  283. }
  284. static const struct class_attribute class_attr_zcdn_destroy =
  285. __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
  286. static void zcdn_device_release(struct device *dev)
  287. {
  288. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  289. ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
  290. MAJOR(dev->devt), MINOR(dev->devt));
  291. kfree(zcdndev);
  292. }
  293. static int zcdn_create(const char *name)
  294. {
  295. dev_t devt;
  296. int i, rc = 0;
  297. char nodename[ZCDN_MAX_NAME];
  298. struct zcdn_device *zcdndev;
  299. if (mutex_lock_interruptible(&ap_perms_mutex))
  300. return -ERESTARTSYS;
  301. /* check if device node with this name already exists */
  302. if (name[0]) {
  303. zcdndev = find_zcdndev_by_name(name);
  304. if (zcdndev) {
  305. put_device(&zcdndev->device);
  306. rc = -EEXIST;
  307. goto unlockout;
  308. }
  309. }
  310. /* find an unused minor number */
  311. for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
  312. devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
  313. zcdndev = find_zcdndev_by_devt(devt);
  314. if (zcdndev)
  315. put_device(&zcdndev->device);
  316. else
  317. break;
  318. }
  319. if (i == ZCRYPT_MAX_MINOR_NODES) {
  320. rc = -ENOSPC;
  321. goto unlockout;
  322. }
  323. /* alloc and prepare a new zcdn device */
  324. zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
  325. if (!zcdndev) {
  326. rc = -ENOMEM;
  327. goto unlockout;
  328. }
  329. zcdndev->device.release = zcdn_device_release;
  330. zcdndev->device.class = zcrypt_class;
  331. zcdndev->device.devt = devt;
  332. zcdndev->device.groups = zcdn_dev_attr_groups;
  333. if (name[0])
  334. strncpy(nodename, name, sizeof(nodename));
  335. else
  336. snprintf(nodename, sizeof(nodename),
  337. ZCRYPT_NAME "_%d", (int) MINOR(devt));
  338. nodename[sizeof(nodename)-1] = '\0';
  339. if (dev_set_name(&zcdndev->device, nodename)) {
  340. rc = -EINVAL;
  341. goto unlockout;
  342. }
  343. rc = device_register(&zcdndev->device);
  344. if (rc) {
  345. put_device(&zcdndev->device);
  346. goto unlockout;
  347. }
  348. ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
  349. MAJOR(devt), MINOR(devt));
  350. unlockout:
  351. mutex_unlock(&ap_perms_mutex);
  352. return rc;
  353. }
  354. static int zcdn_destroy(const char *name)
  355. {
  356. int rc = 0;
  357. struct zcdn_device *zcdndev;
  358. if (mutex_lock_interruptible(&ap_perms_mutex))
  359. return -ERESTARTSYS;
  360. /* try to find this zcdn device */
  361. zcdndev = find_zcdndev_by_name(name);
  362. if (!zcdndev) {
  363. rc = -ENOENT;
  364. goto unlockout;
  365. }
  366. /*
  367. * The zcdn device is not hard destroyed. It is subject to
  368. * reference counting and thus just needs to be unregistered.
  369. */
  370. put_device(&zcdndev->device);
  371. device_unregister(&zcdndev->device);
  372. unlockout:
  373. mutex_unlock(&ap_perms_mutex);
  374. return rc;
  375. }
  376. static void zcdn_destroy_all(void)
  377. {
  378. int i;
  379. dev_t devt;
  380. struct zcdn_device *zcdndev;
  381. mutex_lock(&ap_perms_mutex);
  382. for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
  383. devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
  384. zcdndev = find_zcdndev_by_devt(devt);
  385. if (zcdndev) {
  386. put_device(&zcdndev->device);
  387. device_unregister(&zcdndev->device);
  388. }
  389. }
  390. mutex_unlock(&ap_perms_mutex);
  391. }
  392. #endif
  393. /**
  394. * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
  395. *
  396. * This function is not supported beyond zcrypt 1.3.1.
  397. */
  398. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  399. size_t count, loff_t *f_pos)
  400. {
  401. return -EPERM;
  402. }
  403. /**
  404. * zcrypt_write(): Not allowed.
  405. *
  406. * Write is is not allowed
  407. */
  408. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  409. size_t count, loff_t *f_pos)
  410. {
  411. return -EPERM;
  412. }
  413. /**
  414. * zcrypt_open(): Count number of users.
  415. *
  416. * Device open function to count number of users.
  417. */
  418. static int zcrypt_open(struct inode *inode, struct file *filp)
  419. {
  420. struct ap_perms *perms = &ap_perms;
  421. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  422. if (filp->f_inode->i_cdev == &zcrypt_cdev) {
  423. struct zcdn_device *zcdndev;
  424. if (mutex_lock_interruptible(&ap_perms_mutex))
  425. return -ERESTARTSYS;
  426. zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
  427. /* find returns a reference, no get_device() needed */
  428. mutex_unlock(&ap_perms_mutex);
  429. if (zcdndev)
  430. perms = &zcdndev->perms;
  431. }
  432. #endif
  433. filp->private_data = (void *) perms;
  434. atomic_inc(&zcrypt_open_count);
  435. return nonseekable_open(inode, filp);
  436. }
  437. /**
  438. * zcrypt_release(): Count number of users.
  439. *
  440. * Device close function to count number of users.
  441. */
  442. static int zcrypt_release(struct inode *inode, struct file *filp)
  443. {
  444. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  445. if (filp->f_inode->i_cdev == &zcrypt_cdev) {
  446. struct zcdn_device *zcdndev;
  447. if (mutex_lock_interruptible(&ap_perms_mutex))
  448. return -ERESTARTSYS;
  449. zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
  450. mutex_unlock(&ap_perms_mutex);
  451. if (zcdndev) {
  452. /* 2 puts here: one for find, one for open */
  453. put_device(&zcdndev->device);
  454. put_device(&zcdndev->device);
  455. }
  456. }
  457. #endif
  458. atomic_dec(&zcrypt_open_count);
  459. return 0;
  460. }
  461. static inline int zcrypt_check_ioctl(struct ap_perms *perms,
  462. unsigned int cmd)
  463. {
  464. int rc = -EPERM;
  465. int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
  466. if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
  467. if (test_bit_inv(ioctlnr, perms->ioctlm))
  468. rc = 0;
  469. }
  470. if (rc)
  471. ZCRYPT_DBF(DBF_WARN,
  472. "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
  473. ioctlnr, rc);
  474. return rc;
  475. }
  476. static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
  477. {
  478. return test_bit_inv(card, perms->apm) ? true : false;
  479. }
  480. static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
  481. {
  482. return test_bit_inv(queue, perms->aqm) ? true : false;
  483. }
  484. static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
  485. struct zcrypt_queue *zq,
  486. unsigned int weight)
  487. {
  488. if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
  489. return NULL;
  490. zcrypt_queue_get(zq);
  491. get_device(&zq->queue->ap_dev.device);
  492. atomic_add(weight, &zc->load);
  493. atomic_add(weight, &zq->load);
  494. zq->request_count++;
  495. return zq;
  496. }
  497. static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
  498. struct zcrypt_queue *zq,
  499. unsigned int weight)
  500. {
  501. struct module *mod = zq->queue->ap_dev.drv->driver.owner;
  502. zq->request_count--;
  503. atomic_sub(weight, &zc->load);
  504. atomic_sub(weight, &zq->load);
  505. put_device(&zq->queue->ap_dev.device);
  506. zcrypt_queue_put(zq);
  507. module_put(mod);
  508. }
  509. static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
  510. struct zcrypt_card *pref_zc,
  511. unsigned int weight,
  512. unsigned int pref_weight)
  513. {
  514. if (!pref_zc)
  515. return false;
  516. weight += atomic_read(&zc->load);
  517. pref_weight += atomic_read(&pref_zc->load);
  518. if (weight == pref_weight)
  519. return atomic_read(&zc->card->total_request_count) >
  520. atomic_read(&pref_zc->card->total_request_count);
  521. return weight > pref_weight;
  522. }
  523. static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
  524. struct zcrypt_queue *pref_zq,
  525. unsigned int weight,
  526. unsigned int pref_weight)
  527. {
  528. if (!pref_zq)
  529. return false;
  530. weight += atomic_read(&zq->load);
  531. pref_weight += atomic_read(&pref_zq->load);
  532. if (weight == pref_weight)
  533. return zq->queue->total_request_count >
  534. pref_zq->queue->total_request_count;
  535. return weight > pref_weight;
  536. }
  537. /*
  538. * zcrypt ioctls.
  539. */
  540. static long zcrypt_rsa_modexpo(struct ap_perms *perms,
  541. struct ica_rsa_modexpo *mex)
  542. {
  543. struct zcrypt_card *zc, *pref_zc;
  544. struct zcrypt_queue *zq, *pref_zq;
  545. unsigned int weight, pref_weight;
  546. unsigned int func_code;
  547. int qid = 0, rc = -ENODEV;
  548. trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
  549. if (mex->outputdatalength < mex->inputdatalength) {
  550. rc = -EINVAL;
  551. goto out;
  552. }
  553. /*
  554. * As long as outputdatalength is big enough, we can set the
  555. * outputdatalength equal to the inputdatalength, since that is the
  556. * number of bytes we will copy in any case
  557. */
  558. mex->outputdatalength = mex->inputdatalength;
  559. rc = get_rsa_modex_fc(mex, &func_code);
  560. if (rc)
  561. goto out;
  562. pref_zc = NULL;
  563. pref_zq = NULL;
  564. spin_lock(&zcrypt_list_lock);
  565. for_each_zcrypt_card(zc) {
  566. /* Check for online accelarator and CCA cards */
  567. if (!zc->online || !(zc->card->functions & 0x18000000))
  568. continue;
  569. /* Check for size limits */
  570. if (zc->min_mod_size > mex->inputdatalength ||
  571. zc->max_mod_size < mex->inputdatalength)
  572. continue;
  573. /* check if device node has admission for this card */
  574. if (!zcrypt_check_card(perms, zc->card->id))
  575. continue;
  576. /* get weight index of the card device */
  577. weight = zc->speed_rating[func_code];
  578. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  579. continue;
  580. for_each_zcrypt_queue(zq, zc) {
  581. /* check if device is online and eligible */
  582. if (!zq->online || !zq->ops->rsa_modexpo)
  583. continue;
  584. /* check if device node has admission for this queue */
  585. if (!zcrypt_check_queue(perms,
  586. AP_QID_QUEUE(zq->queue->qid)))
  587. continue;
  588. if (zcrypt_queue_compare(zq, pref_zq,
  589. weight, pref_weight))
  590. continue;
  591. pref_zc = zc;
  592. pref_zq = zq;
  593. pref_weight = weight;
  594. }
  595. }
  596. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  597. spin_unlock(&zcrypt_list_lock);
  598. if (!pref_zq) {
  599. rc = -ENODEV;
  600. goto out;
  601. }
  602. qid = pref_zq->queue->qid;
  603. rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
  604. spin_lock(&zcrypt_list_lock);
  605. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  606. spin_unlock(&zcrypt_list_lock);
  607. out:
  608. trace_s390_zcrypt_rep(mex, func_code, rc,
  609. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  610. return rc;
  611. }
  612. static long zcrypt_rsa_crt(struct ap_perms *perms,
  613. struct ica_rsa_modexpo_crt *crt)
  614. {
  615. struct zcrypt_card *zc, *pref_zc;
  616. struct zcrypt_queue *zq, *pref_zq;
  617. unsigned int weight, pref_weight;
  618. unsigned int func_code;
  619. int qid = 0, rc = -ENODEV;
  620. trace_s390_zcrypt_req(crt, TP_ICARSACRT);
  621. if (crt->outputdatalength < crt->inputdatalength) {
  622. rc = -EINVAL;
  623. goto out;
  624. }
  625. /*
  626. * As long as outputdatalength is big enough, we can set the
  627. * outputdatalength equal to the inputdatalength, since that is the
  628. * number of bytes we will copy in any case
  629. */
  630. crt->outputdatalength = crt->inputdatalength;
  631. rc = get_rsa_crt_fc(crt, &func_code);
  632. if (rc)
  633. goto out;
  634. pref_zc = NULL;
  635. pref_zq = NULL;
  636. spin_lock(&zcrypt_list_lock);
  637. for_each_zcrypt_card(zc) {
  638. /* Check for online accelarator and CCA cards */
  639. if (!zc->online || !(zc->card->functions & 0x18000000))
  640. continue;
  641. /* Check for size limits */
  642. if (zc->min_mod_size > crt->inputdatalength ||
  643. zc->max_mod_size < crt->inputdatalength)
  644. continue;
  645. /* check if device node has admission for this card */
  646. if (!zcrypt_check_card(perms, zc->card->id))
  647. continue;
  648. /* get weight index of the card device */
  649. weight = zc->speed_rating[func_code];
  650. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  651. continue;
  652. for_each_zcrypt_queue(zq, zc) {
  653. /* check if device is online and eligible */
  654. if (!zq->online || !zq->ops->rsa_modexpo_crt)
  655. continue;
  656. /* check if device node has admission for this queue */
  657. if (!zcrypt_check_queue(perms,
  658. AP_QID_QUEUE(zq->queue->qid)))
  659. continue;
  660. if (zcrypt_queue_compare(zq, pref_zq,
  661. weight, pref_weight))
  662. continue;
  663. pref_zc = zc;
  664. pref_zq = zq;
  665. pref_weight = weight;
  666. }
  667. }
  668. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  669. spin_unlock(&zcrypt_list_lock);
  670. if (!pref_zq) {
  671. rc = -ENODEV;
  672. goto out;
  673. }
  674. qid = pref_zq->queue->qid;
  675. rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
  676. spin_lock(&zcrypt_list_lock);
  677. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  678. spin_unlock(&zcrypt_list_lock);
  679. out:
  680. trace_s390_zcrypt_rep(crt, func_code, rc,
  681. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  682. return rc;
  683. }
  684. static long _zcrypt_send_cprb(struct ap_perms *perms,
  685. struct ica_xcRB *xcRB)
  686. {
  687. struct zcrypt_card *zc, *pref_zc;
  688. struct zcrypt_queue *zq, *pref_zq;
  689. struct ap_message ap_msg;
  690. unsigned int weight, pref_weight;
  691. unsigned int func_code;
  692. unsigned short *domain;
  693. int qid = 0, rc = -ENODEV;
  694. trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
  695. ap_init_message(&ap_msg);
  696. rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
  697. if (rc)
  698. goto out;
  699. pref_zc = NULL;
  700. pref_zq = NULL;
  701. spin_lock(&zcrypt_list_lock);
  702. for_each_zcrypt_card(zc) {
  703. /* Check for online CCA cards */
  704. if (!zc->online || !(zc->card->functions & 0x10000000))
  705. continue;
  706. /* Check for user selected CCA card */
  707. if (xcRB->user_defined != AUTOSELECT &&
  708. xcRB->user_defined != zc->card->id)
  709. continue;
  710. /* check if device node has admission for this card */
  711. if (!zcrypt_check_card(perms, zc->card->id))
  712. continue;
  713. /* get weight index of the card device */
  714. weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
  715. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  716. continue;
  717. for_each_zcrypt_queue(zq, zc) {
  718. /* check if device is online and eligible */
  719. if (!zq->online ||
  720. !zq->ops->send_cprb ||
  721. ((*domain != (unsigned short) AUTOSELECT) &&
  722. (*domain != AP_QID_QUEUE(zq->queue->qid))))
  723. continue;
  724. /* check if device node has admission for this queue */
  725. if (!zcrypt_check_queue(perms,
  726. AP_QID_QUEUE(zq->queue->qid)))
  727. continue;
  728. if (zcrypt_queue_compare(zq, pref_zq,
  729. weight, pref_weight))
  730. continue;
  731. pref_zc = zc;
  732. pref_zq = zq;
  733. pref_weight = weight;
  734. }
  735. }
  736. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  737. spin_unlock(&zcrypt_list_lock);
  738. if (!pref_zq) {
  739. rc = -ENODEV;
  740. goto out;
  741. }
  742. /* in case of auto select, provide the correct domain */
  743. qid = pref_zq->queue->qid;
  744. if (*domain == (unsigned short) AUTOSELECT)
  745. *domain = AP_QID_QUEUE(qid);
  746. rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
  747. spin_lock(&zcrypt_list_lock);
  748. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  749. spin_unlock(&zcrypt_list_lock);
  750. out:
  751. ap_release_message(&ap_msg);
  752. trace_s390_zcrypt_rep(xcRB, func_code, rc,
  753. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  754. return rc;
  755. }
  756. long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  757. {
  758. return _zcrypt_send_cprb(NULL, xcRB);
  759. }
  760. EXPORT_SYMBOL(zcrypt_send_cprb);
  761. static bool is_desired_ep11_card(unsigned int dev_id,
  762. unsigned short target_num,
  763. struct ep11_target_dev *targets)
  764. {
  765. while (target_num-- > 0) {
  766. if (dev_id == targets->ap_id)
  767. return true;
  768. targets++;
  769. }
  770. return false;
  771. }
  772. static bool is_desired_ep11_queue(unsigned int dev_qid,
  773. unsigned short target_num,
  774. struct ep11_target_dev *targets)
  775. {
  776. while (target_num-- > 0) {
  777. if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
  778. return true;
  779. targets++;
  780. }
  781. return false;
  782. }
  783. static long zcrypt_send_ep11_cprb(struct ap_perms *perms,
  784. struct ep11_urb *xcrb)
  785. {
  786. struct zcrypt_card *zc, *pref_zc;
  787. struct zcrypt_queue *zq, *pref_zq;
  788. struct ep11_target_dev *targets;
  789. unsigned short target_num;
  790. unsigned int weight, pref_weight;
  791. unsigned int func_code;
  792. struct ap_message ap_msg;
  793. int qid = 0, rc = -ENODEV;
  794. trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
  795. ap_init_message(&ap_msg);
  796. target_num = (unsigned short) xcrb->targets_num;
  797. /* empty list indicates autoselect (all available targets) */
  798. targets = NULL;
  799. if (target_num != 0) {
  800. struct ep11_target_dev __user *uptr;
  801. targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
  802. if (!targets) {
  803. rc = -ENOMEM;
  804. goto out;
  805. }
  806. uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
  807. if (copy_from_user(targets, uptr,
  808. target_num * sizeof(*targets))) {
  809. rc = -EFAULT;
  810. goto out_free;
  811. }
  812. }
  813. rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
  814. if (rc)
  815. goto out_free;
  816. pref_zc = NULL;
  817. pref_zq = NULL;
  818. spin_lock(&zcrypt_list_lock);
  819. for_each_zcrypt_card(zc) {
  820. /* Check for online EP11 cards */
  821. if (!zc->online || !(zc->card->functions & 0x04000000))
  822. continue;
  823. /* Check for user selected EP11 card */
  824. if (targets &&
  825. !is_desired_ep11_card(zc->card->id, target_num, targets))
  826. continue;
  827. /* check if device node has admission for this card */
  828. if (!zcrypt_check_card(perms, zc->card->id))
  829. continue;
  830. /* get weight index of the card device */
  831. weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
  832. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  833. continue;
  834. for_each_zcrypt_queue(zq, zc) {
  835. /* check if device is online and eligible */
  836. if (!zq->online ||
  837. !zq->ops->send_ep11_cprb ||
  838. (targets &&
  839. !is_desired_ep11_queue(zq->queue->qid,
  840. target_num, targets)))
  841. continue;
  842. /* check if device node has admission for this queue */
  843. if (!zcrypt_check_queue(perms,
  844. AP_QID_QUEUE(zq->queue->qid)))
  845. continue;
  846. if (zcrypt_queue_compare(zq, pref_zq,
  847. weight, pref_weight))
  848. continue;
  849. pref_zc = zc;
  850. pref_zq = zq;
  851. pref_weight = weight;
  852. }
  853. }
  854. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  855. spin_unlock(&zcrypt_list_lock);
  856. if (!pref_zq) {
  857. rc = -ENODEV;
  858. goto out_free;
  859. }
  860. qid = pref_zq->queue->qid;
  861. rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
  862. spin_lock(&zcrypt_list_lock);
  863. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  864. spin_unlock(&zcrypt_list_lock);
  865. out_free:
  866. kfree(targets);
  867. out:
  868. ap_release_message(&ap_msg);
  869. trace_s390_zcrypt_rep(xcrb, func_code, rc,
  870. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  871. return rc;
  872. }
  873. static long zcrypt_rng(char *buffer)
  874. {
  875. struct zcrypt_card *zc, *pref_zc;
  876. struct zcrypt_queue *zq, *pref_zq;
  877. unsigned int weight, pref_weight;
  878. unsigned int func_code;
  879. struct ap_message ap_msg;
  880. unsigned int domain;
  881. int qid = 0, rc = -ENODEV;
  882. trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
  883. ap_init_message(&ap_msg);
  884. rc = get_rng_fc(&ap_msg, &func_code, &domain);
  885. if (rc)
  886. goto out;
  887. pref_zc = NULL;
  888. pref_zq = NULL;
  889. spin_lock(&zcrypt_list_lock);
  890. for_each_zcrypt_card(zc) {
  891. /* Check for online CCA cards */
  892. if (!zc->online || !(zc->card->functions & 0x10000000))
  893. continue;
  894. /* get weight index of the card device */
  895. weight = zc->speed_rating[func_code];
  896. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  897. continue;
  898. for_each_zcrypt_queue(zq, zc) {
  899. /* check if device is online and eligible */
  900. if (!zq->online || !zq->ops->rng)
  901. continue;
  902. if (zcrypt_queue_compare(zq, pref_zq,
  903. weight, pref_weight))
  904. continue;
  905. pref_zc = zc;
  906. pref_zq = zq;
  907. pref_weight = weight;
  908. }
  909. }
  910. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  911. spin_unlock(&zcrypt_list_lock);
  912. if (!pref_zq) {
  913. rc = -ENODEV;
  914. goto out;
  915. }
  916. qid = pref_zq->queue->qid;
  917. rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
  918. spin_lock(&zcrypt_list_lock);
  919. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  920. spin_unlock(&zcrypt_list_lock);
  921. out:
  922. ap_release_message(&ap_msg);
  923. trace_s390_zcrypt_rep(buffer, func_code, rc,
  924. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  925. return rc;
  926. }
  927. static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
  928. {
  929. struct zcrypt_card *zc;
  930. struct zcrypt_queue *zq;
  931. struct zcrypt_device_status *stat;
  932. int card, queue;
  933. memset(devstatus, 0, MAX_ZDEV_ENTRIES
  934. * sizeof(struct zcrypt_device_status));
  935. spin_lock(&zcrypt_list_lock);
  936. for_each_zcrypt_card(zc) {
  937. for_each_zcrypt_queue(zq, zc) {
  938. card = AP_QID_CARD(zq->queue->qid);
  939. if (card >= MAX_ZDEV_CARDIDS)
  940. continue;
  941. queue = AP_QID_QUEUE(zq->queue->qid);
  942. stat = &devstatus[card * AP_DOMAINS + queue];
  943. stat->hwtype = zc->card->ap_dev.device_type;
  944. stat->functions = zc->card->functions >> 26;
  945. stat->qid = zq->queue->qid;
  946. stat->online = zq->online ? 0x01 : 0x00;
  947. }
  948. }
  949. spin_unlock(&zcrypt_list_lock);
  950. }
  951. void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
  952. {
  953. struct zcrypt_card *zc;
  954. struct zcrypt_queue *zq;
  955. struct zcrypt_device_status_ext *stat;
  956. int card, queue;
  957. memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
  958. * sizeof(struct zcrypt_device_status_ext));
  959. spin_lock(&zcrypt_list_lock);
  960. for_each_zcrypt_card(zc) {
  961. for_each_zcrypt_queue(zq, zc) {
  962. card = AP_QID_CARD(zq->queue->qid);
  963. queue = AP_QID_QUEUE(zq->queue->qid);
  964. stat = &devstatus[card * AP_DOMAINS + queue];
  965. stat->hwtype = zc->card->ap_dev.device_type;
  966. stat->functions = zc->card->functions >> 26;
  967. stat->qid = zq->queue->qid;
  968. stat->online = zq->online ? 0x01 : 0x00;
  969. }
  970. }
  971. spin_unlock(&zcrypt_list_lock);
  972. }
  973. EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
  974. static void zcrypt_status_mask(char status[], size_t max_adapters)
  975. {
  976. struct zcrypt_card *zc;
  977. struct zcrypt_queue *zq;
  978. int card;
  979. memset(status, 0, max_adapters);
  980. spin_lock(&zcrypt_list_lock);
  981. for_each_zcrypt_card(zc) {
  982. for_each_zcrypt_queue(zq, zc) {
  983. card = AP_QID_CARD(zq->queue->qid);
  984. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
  985. || card >= max_adapters)
  986. continue;
  987. status[card] = zc->online ? zc->user_space_type : 0x0d;
  988. }
  989. }
  990. spin_unlock(&zcrypt_list_lock);
  991. }
  992. static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
  993. {
  994. struct zcrypt_card *zc;
  995. struct zcrypt_queue *zq;
  996. int card;
  997. memset(qdepth, 0, max_adapters);
  998. spin_lock(&zcrypt_list_lock);
  999. local_bh_disable();
  1000. for_each_zcrypt_card(zc) {
  1001. for_each_zcrypt_queue(zq, zc) {
  1002. card = AP_QID_CARD(zq->queue->qid);
  1003. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
  1004. || card >= max_adapters)
  1005. continue;
  1006. spin_lock(&zq->queue->lock);
  1007. qdepth[card] =
  1008. zq->queue->pendingq_count +
  1009. zq->queue->requestq_count;
  1010. spin_unlock(&zq->queue->lock);
  1011. }
  1012. }
  1013. local_bh_enable();
  1014. spin_unlock(&zcrypt_list_lock);
  1015. }
  1016. static void zcrypt_perdev_reqcnt(int reqcnt[], size_t max_adapters)
  1017. {
  1018. struct zcrypt_card *zc;
  1019. struct zcrypt_queue *zq;
  1020. int card;
  1021. memset(reqcnt, 0, sizeof(int) * max_adapters);
  1022. spin_lock(&zcrypt_list_lock);
  1023. local_bh_disable();
  1024. for_each_zcrypt_card(zc) {
  1025. for_each_zcrypt_queue(zq, zc) {
  1026. card = AP_QID_CARD(zq->queue->qid);
  1027. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
  1028. || card >= max_adapters)
  1029. continue;
  1030. spin_lock(&zq->queue->lock);
  1031. reqcnt[card] = zq->queue->total_request_count;
  1032. spin_unlock(&zq->queue->lock);
  1033. }
  1034. }
  1035. local_bh_enable();
  1036. spin_unlock(&zcrypt_list_lock);
  1037. }
  1038. static int zcrypt_pendingq_count(void)
  1039. {
  1040. struct zcrypt_card *zc;
  1041. struct zcrypt_queue *zq;
  1042. int pendingq_count;
  1043. pendingq_count = 0;
  1044. spin_lock(&zcrypt_list_lock);
  1045. local_bh_disable();
  1046. for_each_zcrypt_card(zc) {
  1047. for_each_zcrypt_queue(zq, zc) {
  1048. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1049. continue;
  1050. spin_lock(&zq->queue->lock);
  1051. pendingq_count += zq->queue->pendingq_count;
  1052. spin_unlock(&zq->queue->lock);
  1053. }
  1054. }
  1055. local_bh_enable();
  1056. spin_unlock(&zcrypt_list_lock);
  1057. return pendingq_count;
  1058. }
  1059. static int zcrypt_requestq_count(void)
  1060. {
  1061. struct zcrypt_card *zc;
  1062. struct zcrypt_queue *zq;
  1063. int requestq_count;
  1064. requestq_count = 0;
  1065. spin_lock(&zcrypt_list_lock);
  1066. local_bh_disable();
  1067. for_each_zcrypt_card(zc) {
  1068. for_each_zcrypt_queue(zq, zc) {
  1069. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1070. continue;
  1071. spin_lock(&zq->queue->lock);
  1072. requestq_count += zq->queue->requestq_count;
  1073. spin_unlock(&zq->queue->lock);
  1074. }
  1075. }
  1076. local_bh_enable();
  1077. spin_unlock(&zcrypt_list_lock);
  1078. return requestq_count;
  1079. }
  1080. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  1081. unsigned long arg)
  1082. {
  1083. int rc;
  1084. struct ap_perms *perms =
  1085. (struct ap_perms *) filp->private_data;
  1086. rc = zcrypt_check_ioctl(perms, cmd);
  1087. if (rc)
  1088. return rc;
  1089. switch (cmd) {
  1090. case ICARSAMODEXPO: {
  1091. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  1092. struct ica_rsa_modexpo mex;
  1093. if (copy_from_user(&mex, umex, sizeof(mex)))
  1094. return -EFAULT;
  1095. do {
  1096. rc = zcrypt_rsa_modexpo(perms, &mex);
  1097. } while (rc == -EAGAIN);
  1098. /* on failure: retry once again after a requested rescan */
  1099. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1100. do {
  1101. rc = zcrypt_rsa_modexpo(perms, &mex);
  1102. } while (rc == -EAGAIN);
  1103. if (rc) {
  1104. ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
  1105. return rc;
  1106. }
  1107. return put_user(mex.outputdatalength, &umex->outputdatalength);
  1108. }
  1109. case ICARSACRT: {
  1110. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  1111. struct ica_rsa_modexpo_crt crt;
  1112. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  1113. return -EFAULT;
  1114. do {
  1115. rc = zcrypt_rsa_crt(perms, &crt);
  1116. } while (rc == -EAGAIN);
  1117. /* on failure: retry once again after a requested rescan */
  1118. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1119. do {
  1120. rc = zcrypt_rsa_crt(perms, &crt);
  1121. } while (rc == -EAGAIN);
  1122. if (rc) {
  1123. ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
  1124. return rc;
  1125. }
  1126. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  1127. }
  1128. case ZSECSENDCPRB: {
  1129. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  1130. struct ica_xcRB xcRB;
  1131. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  1132. return -EFAULT;
  1133. do {
  1134. rc = _zcrypt_send_cprb(perms, &xcRB);
  1135. } while (rc == -EAGAIN);
  1136. /* on failure: retry once again after a requested rescan */
  1137. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1138. do {
  1139. rc = _zcrypt_send_cprb(perms, &xcRB);
  1140. } while (rc == -EAGAIN);
  1141. if (rc)
  1142. ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d\n", rc);
  1143. if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
  1144. return -EFAULT;
  1145. return rc;
  1146. }
  1147. case ZSENDEP11CPRB: {
  1148. struct ep11_urb __user *uxcrb = (void __user *)arg;
  1149. struct ep11_urb xcrb;
  1150. if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
  1151. return -EFAULT;
  1152. do {
  1153. rc = zcrypt_send_ep11_cprb(perms, &xcrb);
  1154. } while (rc == -EAGAIN);
  1155. /* on failure: retry once again after a requested rescan */
  1156. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1157. do {
  1158. rc = zcrypt_send_ep11_cprb(perms, &xcrb);
  1159. } while (rc == -EAGAIN);
  1160. if (rc)
  1161. ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
  1162. if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
  1163. return -EFAULT;
  1164. return rc;
  1165. }
  1166. case ZCRYPT_DEVICE_STATUS: {
  1167. struct zcrypt_device_status_ext *device_status;
  1168. size_t total_size = MAX_ZDEV_ENTRIES_EXT
  1169. * sizeof(struct zcrypt_device_status_ext);
  1170. device_status = kzalloc(total_size, GFP_KERNEL);
  1171. if (!device_status)
  1172. return -ENOMEM;
  1173. zcrypt_device_status_mask_ext(device_status);
  1174. if (copy_to_user((char __user *) arg, device_status,
  1175. total_size))
  1176. rc = -EFAULT;
  1177. kfree(device_status);
  1178. return rc;
  1179. }
  1180. case ZCRYPT_STATUS_MASK: {
  1181. char status[AP_DEVICES];
  1182. zcrypt_status_mask(status, AP_DEVICES);
  1183. if (copy_to_user((char __user *) arg, status, sizeof(status)))
  1184. return -EFAULT;
  1185. return 0;
  1186. }
  1187. case ZCRYPT_QDEPTH_MASK: {
  1188. char qdepth[AP_DEVICES];
  1189. zcrypt_qdepth_mask(qdepth, AP_DEVICES);
  1190. if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
  1191. return -EFAULT;
  1192. return 0;
  1193. }
  1194. case ZCRYPT_PERDEV_REQCNT: {
  1195. int *reqcnt;
  1196. reqcnt = kcalloc(AP_DEVICES, sizeof(int), GFP_KERNEL);
  1197. if (!reqcnt)
  1198. return -ENOMEM;
  1199. zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
  1200. if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
  1201. rc = -EFAULT;
  1202. kfree(reqcnt);
  1203. return rc;
  1204. }
  1205. case Z90STAT_REQUESTQ_COUNT:
  1206. return put_user(zcrypt_requestq_count(), (int __user *) arg);
  1207. case Z90STAT_PENDINGQ_COUNT:
  1208. return put_user(zcrypt_pendingq_count(), (int __user *) arg);
  1209. case Z90STAT_TOTALOPEN_COUNT:
  1210. return put_user(atomic_read(&zcrypt_open_count),
  1211. (int __user *) arg);
  1212. case Z90STAT_DOMAIN_INDEX:
  1213. return put_user(ap_domain_index, (int __user *) arg);
  1214. /*
  1215. * Deprecated ioctls
  1216. */
  1217. case ZDEVICESTATUS: {
  1218. /* the old ioctl supports only 64 adapters */
  1219. struct zcrypt_device_status *device_status;
  1220. size_t total_size = MAX_ZDEV_ENTRIES
  1221. * sizeof(struct zcrypt_device_status);
  1222. device_status = kzalloc(total_size, GFP_KERNEL);
  1223. if (!device_status)
  1224. return -ENOMEM;
  1225. zcrypt_device_status_mask(device_status);
  1226. if (copy_to_user((char __user *) arg, device_status,
  1227. total_size))
  1228. rc = -EFAULT;
  1229. kfree(device_status);
  1230. return rc;
  1231. }
  1232. case Z90STAT_STATUS_MASK: {
  1233. /* the old ioctl supports only 64 adapters */
  1234. char status[MAX_ZDEV_CARDIDS];
  1235. zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
  1236. if (copy_to_user((char __user *) arg, status, sizeof(status)))
  1237. return -EFAULT;
  1238. return 0;
  1239. }
  1240. case Z90STAT_QDEPTH_MASK: {
  1241. /* the old ioctl supports only 64 adapters */
  1242. char qdepth[MAX_ZDEV_CARDIDS];
  1243. zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
  1244. if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
  1245. return -EFAULT;
  1246. return 0;
  1247. }
  1248. case Z90STAT_PERDEV_REQCNT: {
  1249. /* the old ioctl supports only 64 adapters */
  1250. int reqcnt[MAX_ZDEV_CARDIDS];
  1251. zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
  1252. if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
  1253. return -EFAULT;
  1254. return 0;
  1255. }
  1256. /* unknown ioctl number */
  1257. default:
  1258. ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
  1259. return -ENOIOCTLCMD;
  1260. }
  1261. }
  1262. #ifdef CONFIG_COMPAT
  1263. /*
  1264. * ioctl32 conversion routines
  1265. */
  1266. struct compat_ica_rsa_modexpo {
  1267. compat_uptr_t inputdata;
  1268. unsigned int inputdatalength;
  1269. compat_uptr_t outputdata;
  1270. unsigned int outputdatalength;
  1271. compat_uptr_t b_key;
  1272. compat_uptr_t n_modulus;
  1273. };
  1274. static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
  1275. unsigned int cmd, unsigned long arg)
  1276. {
  1277. struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
  1278. struct compat_ica_rsa_modexpo mex32;
  1279. struct ica_rsa_modexpo mex64;
  1280. long rc;
  1281. if (copy_from_user(&mex32, umex32, sizeof(mex32)))
  1282. return -EFAULT;
  1283. mex64.inputdata = compat_ptr(mex32.inputdata);
  1284. mex64.inputdatalength = mex32.inputdatalength;
  1285. mex64.outputdata = compat_ptr(mex32.outputdata);
  1286. mex64.outputdatalength = mex32.outputdatalength;
  1287. mex64.b_key = compat_ptr(mex32.b_key);
  1288. mex64.n_modulus = compat_ptr(mex32.n_modulus);
  1289. do {
  1290. rc = zcrypt_rsa_modexpo(perms, &mex64);
  1291. } while (rc == -EAGAIN);
  1292. /* on failure: retry once again after a requested rescan */
  1293. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1294. do {
  1295. rc = zcrypt_rsa_modexpo(perms, &mex64);
  1296. } while (rc == -EAGAIN);
  1297. if (rc)
  1298. return rc;
  1299. return put_user(mex64.outputdatalength,
  1300. &umex32->outputdatalength);
  1301. }
  1302. struct compat_ica_rsa_modexpo_crt {
  1303. compat_uptr_t inputdata;
  1304. unsigned int inputdatalength;
  1305. compat_uptr_t outputdata;
  1306. unsigned int outputdatalength;
  1307. compat_uptr_t bp_key;
  1308. compat_uptr_t bq_key;
  1309. compat_uptr_t np_prime;
  1310. compat_uptr_t nq_prime;
  1311. compat_uptr_t u_mult_inv;
  1312. };
  1313. static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
  1314. unsigned int cmd, unsigned long arg)
  1315. {
  1316. struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
  1317. struct compat_ica_rsa_modexpo_crt crt32;
  1318. struct ica_rsa_modexpo_crt crt64;
  1319. long rc;
  1320. if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
  1321. return -EFAULT;
  1322. crt64.inputdata = compat_ptr(crt32.inputdata);
  1323. crt64.inputdatalength = crt32.inputdatalength;
  1324. crt64.outputdata = compat_ptr(crt32.outputdata);
  1325. crt64.outputdatalength = crt32.outputdatalength;
  1326. crt64.bp_key = compat_ptr(crt32.bp_key);
  1327. crt64.bq_key = compat_ptr(crt32.bq_key);
  1328. crt64.np_prime = compat_ptr(crt32.np_prime);
  1329. crt64.nq_prime = compat_ptr(crt32.nq_prime);
  1330. crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
  1331. do {
  1332. rc = zcrypt_rsa_crt(perms, &crt64);
  1333. } while (rc == -EAGAIN);
  1334. /* on failure: retry once again after a requested rescan */
  1335. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1336. do {
  1337. rc = zcrypt_rsa_crt(perms, &crt64);
  1338. } while (rc == -EAGAIN);
  1339. if (rc)
  1340. return rc;
  1341. return put_user(crt64.outputdatalength,
  1342. &ucrt32->outputdatalength);
  1343. }
  1344. struct compat_ica_xcRB {
  1345. unsigned short agent_ID;
  1346. unsigned int user_defined;
  1347. unsigned short request_ID;
  1348. unsigned int request_control_blk_length;
  1349. unsigned char padding1[16 - sizeof(compat_uptr_t)];
  1350. compat_uptr_t request_control_blk_addr;
  1351. unsigned int request_data_length;
  1352. char padding2[16 - sizeof(compat_uptr_t)];
  1353. compat_uptr_t request_data_address;
  1354. unsigned int reply_control_blk_length;
  1355. char padding3[16 - sizeof(compat_uptr_t)];
  1356. compat_uptr_t reply_control_blk_addr;
  1357. unsigned int reply_data_length;
  1358. char padding4[16 - sizeof(compat_uptr_t)];
  1359. compat_uptr_t reply_data_addr;
  1360. unsigned short priority_window;
  1361. unsigned int status;
  1362. } __packed;
  1363. static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
  1364. unsigned int cmd, unsigned long arg)
  1365. {
  1366. struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
  1367. struct compat_ica_xcRB xcRB32;
  1368. struct ica_xcRB xcRB64;
  1369. long rc;
  1370. if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
  1371. return -EFAULT;
  1372. xcRB64.agent_ID = xcRB32.agent_ID;
  1373. xcRB64.user_defined = xcRB32.user_defined;
  1374. xcRB64.request_ID = xcRB32.request_ID;
  1375. xcRB64.request_control_blk_length =
  1376. xcRB32.request_control_blk_length;
  1377. xcRB64.request_control_blk_addr =
  1378. compat_ptr(xcRB32.request_control_blk_addr);
  1379. xcRB64.request_data_length =
  1380. xcRB32.request_data_length;
  1381. xcRB64.request_data_address =
  1382. compat_ptr(xcRB32.request_data_address);
  1383. xcRB64.reply_control_blk_length =
  1384. xcRB32.reply_control_blk_length;
  1385. xcRB64.reply_control_blk_addr =
  1386. compat_ptr(xcRB32.reply_control_blk_addr);
  1387. xcRB64.reply_data_length = xcRB32.reply_data_length;
  1388. xcRB64.reply_data_addr =
  1389. compat_ptr(xcRB32.reply_data_addr);
  1390. xcRB64.priority_window = xcRB32.priority_window;
  1391. xcRB64.status = xcRB32.status;
  1392. do {
  1393. rc = _zcrypt_send_cprb(perms, &xcRB64);
  1394. } while (rc == -EAGAIN);
  1395. /* on failure: retry once again after a requested rescan */
  1396. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1397. do {
  1398. rc = _zcrypt_send_cprb(perms, &xcRB64);
  1399. } while (rc == -EAGAIN);
  1400. xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
  1401. xcRB32.reply_data_length = xcRB64.reply_data_length;
  1402. xcRB32.status = xcRB64.status;
  1403. if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
  1404. return -EFAULT;
  1405. return rc;
  1406. }
  1407. static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
  1408. unsigned long arg)
  1409. {
  1410. int rc;
  1411. struct ap_perms *perms =
  1412. (struct ap_perms *) filp->private_data;
  1413. rc = zcrypt_check_ioctl(perms, cmd);
  1414. if (rc)
  1415. return rc;
  1416. if (cmd == ICARSAMODEXPO)
  1417. return trans_modexpo32(perms, filp, cmd, arg);
  1418. if (cmd == ICARSACRT)
  1419. return trans_modexpo_crt32(perms, filp, cmd, arg);
  1420. if (cmd == ZSECSENDCPRB)
  1421. return trans_xcRB32(perms, filp, cmd, arg);
  1422. return zcrypt_unlocked_ioctl(filp, cmd, arg);
  1423. }
  1424. #endif
  1425. /*
  1426. * Misc device file operations.
  1427. */
  1428. static const struct file_operations zcrypt_fops = {
  1429. .owner = THIS_MODULE,
  1430. .read = zcrypt_read,
  1431. .write = zcrypt_write,
  1432. .unlocked_ioctl = zcrypt_unlocked_ioctl,
  1433. #ifdef CONFIG_COMPAT
  1434. .compat_ioctl = zcrypt_compat_ioctl,
  1435. #endif
  1436. .open = zcrypt_open,
  1437. .release = zcrypt_release,
  1438. .llseek = no_llseek,
  1439. };
  1440. /*
  1441. * Misc device.
  1442. */
  1443. static struct miscdevice zcrypt_misc_device = {
  1444. .minor = MISC_DYNAMIC_MINOR,
  1445. .name = "z90crypt",
  1446. .fops = &zcrypt_fops,
  1447. };
  1448. static int zcrypt_rng_device_count;
  1449. static u32 *zcrypt_rng_buffer;
  1450. static int zcrypt_rng_buffer_index;
  1451. static DEFINE_MUTEX(zcrypt_rng_mutex);
  1452. static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
  1453. {
  1454. int rc;
  1455. /*
  1456. * We don't need locking here because the RNG API guarantees serialized
  1457. * read method calls.
  1458. */
  1459. if (zcrypt_rng_buffer_index == 0) {
  1460. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1461. /* on failure: retry once again after a requested rescan */
  1462. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1463. rc = zcrypt_rng((char *) zcrypt_rng_buffer);
  1464. if (rc < 0)
  1465. return -EIO;
  1466. zcrypt_rng_buffer_index = rc / sizeof(*data);
  1467. }
  1468. *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
  1469. return sizeof(*data);
  1470. }
  1471. static struct hwrng zcrypt_rng_dev = {
  1472. .name = "zcrypt",
  1473. .data_read = zcrypt_rng_data_read,
  1474. .quality = 990,
  1475. };
  1476. int zcrypt_rng_device_add(void)
  1477. {
  1478. int rc = 0;
  1479. mutex_lock(&zcrypt_rng_mutex);
  1480. if (zcrypt_rng_device_count == 0) {
  1481. zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
  1482. if (!zcrypt_rng_buffer) {
  1483. rc = -ENOMEM;
  1484. goto out;
  1485. }
  1486. zcrypt_rng_buffer_index = 0;
  1487. if (!zcrypt_hwrng_seed)
  1488. zcrypt_rng_dev.quality = 0;
  1489. rc = hwrng_register(&zcrypt_rng_dev);
  1490. if (rc)
  1491. goto out_free;
  1492. zcrypt_rng_device_count = 1;
  1493. } else
  1494. zcrypt_rng_device_count++;
  1495. mutex_unlock(&zcrypt_rng_mutex);
  1496. return 0;
  1497. out_free:
  1498. free_page((unsigned long) zcrypt_rng_buffer);
  1499. out:
  1500. mutex_unlock(&zcrypt_rng_mutex);
  1501. return rc;
  1502. }
  1503. void zcrypt_rng_device_remove(void)
  1504. {
  1505. mutex_lock(&zcrypt_rng_mutex);
  1506. zcrypt_rng_device_count--;
  1507. if (zcrypt_rng_device_count == 0) {
  1508. hwrng_unregister(&zcrypt_rng_dev);
  1509. free_page((unsigned long) zcrypt_rng_buffer);
  1510. }
  1511. mutex_unlock(&zcrypt_rng_mutex);
  1512. }
  1513. int __init zcrypt_debug_init(void)
  1514. {
  1515. zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
  1516. DBF_MAX_SPRINTF_ARGS * sizeof(long));
  1517. debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
  1518. debug_set_level(zcrypt_dbf_info, DBF_ERR);
  1519. return 0;
  1520. }
  1521. void zcrypt_debug_exit(void)
  1522. {
  1523. debug_unregister(zcrypt_dbf_info);
  1524. }
  1525. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  1526. static int __init zcdn_init(void)
  1527. {
  1528. int rc;
  1529. /* create a new class 'zcrypt' */
  1530. zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
  1531. if (IS_ERR(zcrypt_class)) {
  1532. rc = PTR_ERR(zcrypt_class);
  1533. goto out_class_create_failed;
  1534. }
  1535. zcrypt_class->dev_release = zcdn_device_release;
  1536. /* alloc device minor range */
  1537. rc = alloc_chrdev_region(&zcrypt_devt,
  1538. 0, ZCRYPT_MAX_MINOR_NODES,
  1539. ZCRYPT_NAME);
  1540. if (rc)
  1541. goto out_alloc_chrdev_failed;
  1542. cdev_init(&zcrypt_cdev, &zcrypt_fops);
  1543. zcrypt_cdev.owner = THIS_MODULE;
  1544. rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
  1545. if (rc)
  1546. goto out_cdev_add_failed;
  1547. /* need some class specific sysfs attributes */
  1548. rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
  1549. if (rc)
  1550. goto out_class_create_file_1_failed;
  1551. rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
  1552. if (rc)
  1553. goto out_class_create_file_2_failed;
  1554. return 0;
  1555. out_class_create_file_2_failed:
  1556. class_remove_file(zcrypt_class, &class_attr_zcdn_create);
  1557. out_class_create_file_1_failed:
  1558. cdev_del(&zcrypt_cdev);
  1559. out_cdev_add_failed:
  1560. unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
  1561. out_alloc_chrdev_failed:
  1562. class_destroy(zcrypt_class);
  1563. out_class_create_failed:
  1564. return rc;
  1565. }
  1566. static void zcdn_exit(void)
  1567. {
  1568. class_remove_file(zcrypt_class, &class_attr_zcdn_create);
  1569. class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
  1570. zcdn_destroy_all();
  1571. cdev_del(&zcrypt_cdev);
  1572. unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
  1573. class_destroy(zcrypt_class);
  1574. }
  1575. #endif
  1576. /**
  1577. * zcrypt_api_init(): Module initialization.
  1578. *
  1579. * The module initialization code.
  1580. */
  1581. int __init zcrypt_api_init(void)
  1582. {
  1583. int rc;
  1584. rc = zcrypt_debug_init();
  1585. if (rc)
  1586. goto out;
  1587. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  1588. rc = zcdn_init();
  1589. if (rc)
  1590. goto out;
  1591. #endif
  1592. /* Register the request sprayer. */
  1593. rc = misc_register(&zcrypt_misc_device);
  1594. if (rc < 0)
  1595. goto out_misc_register_failed;
  1596. zcrypt_msgtype6_init();
  1597. zcrypt_msgtype50_init();
  1598. return 0;
  1599. out_misc_register_failed:
  1600. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  1601. zcdn_exit();
  1602. #endif
  1603. zcrypt_debug_exit();
  1604. out:
  1605. return rc;
  1606. }
  1607. /**
  1608. * zcrypt_api_exit(): Module termination.
  1609. *
  1610. * The module termination code.
  1611. */
  1612. void __exit zcrypt_api_exit(void)
  1613. {
  1614. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  1615. zcdn_exit();
  1616. #endif
  1617. misc_deregister(&zcrypt_misc_device);
  1618. zcrypt_msgtype6_exit();
  1619. zcrypt_msgtype50_exit();
  1620. zcrypt_debug_exit();
  1621. }
  1622. module_init(zcrypt_api_init);
  1623. module_exit(zcrypt_api_exit);