zcrypt_api.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2001, 2018
  4. * Author(s): Robert Burroughs
  5. * Eric Rossman (edrossma@us.ibm.com)
  6. * Cornelia Huck <cornelia.huck@de.ibm.com>
  7. *
  8. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  9. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  10. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  11. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  12. * Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
  13. */
  14. #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 <asm/debug.h>
  28. #define CREATE_TRACE_POINTS
  29. #include <asm/trace/zcrypt.h>
  30. #include "zcrypt_api.h"
  31. #include "zcrypt_debug.h"
  32. #include "zcrypt_msgtype6.h"
  33. #include "zcrypt_msgtype50.h"
  34. /*
  35. * Module description.
  36. */
  37. MODULE_AUTHOR("IBM Corporation");
  38. MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  39. "Copyright IBM Corp. 2001, 2012");
  40. MODULE_LICENSE("GPL");
  41. /*
  42. * zcrypt tracepoint functions
  43. */
  44. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
  45. EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
  46. static int zcrypt_hwrng_seed = 1;
  47. module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
  48. MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
  49. DEFINE_SPINLOCK(zcrypt_list_lock);
  50. LIST_HEAD(zcrypt_card_list);
  51. int zcrypt_device_count;
  52. static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  53. static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
  54. atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
  55. EXPORT_SYMBOL(zcrypt_rescan_req);
  56. static LIST_HEAD(zcrypt_ops_list);
  57. /* Zcrypt related debug feature stuff. */
  58. debug_info_t *zcrypt_dbf_info;
  59. /**
  60. * Process a rescan of the transport layer.
  61. *
  62. * Returns 1, if the rescan has been processed, otherwise 0.
  63. */
  64. static inline int zcrypt_process_rescan(void)
  65. {
  66. if (atomic_read(&zcrypt_rescan_req)) {
  67. atomic_set(&zcrypt_rescan_req, 0);
  68. atomic_inc(&zcrypt_rescan_count);
  69. ap_bus_force_rescan();
  70. ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
  71. atomic_inc_return(&zcrypt_rescan_count));
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. void zcrypt_msgtype_register(struct zcrypt_ops *zops)
  77. {
  78. list_add_tail(&zops->list, &zcrypt_ops_list);
  79. }
  80. void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  81. {
  82. list_del_init(&zops->list);
  83. }
  84. struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
  85. {
  86. struct zcrypt_ops *zops;
  87. list_for_each_entry(zops, &zcrypt_ops_list, list)
  88. if ((zops->variant == variant) &&
  89. (!strncmp(zops->name, name, sizeof(zops->name))))
  90. return zops;
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL(zcrypt_msgtype);
  94. /*
  95. * Multi device nodes extension functions.
  96. */
  97. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  98. struct zcdn_device;
  99. static struct class *zcrypt_class;
  100. static dev_t zcrypt_devt;
  101. static struct cdev zcrypt_cdev;
  102. struct zcdn_device {
  103. struct device device;
  104. struct ap_perms perms;
  105. };
  106. #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
  107. #define ZCDN_MAX_NAME 32
  108. static int zcdn_create(const char *name);
  109. static int zcdn_destroy(const char *name);
  110. /* helper function, matches the name for find_zcdndev_by_name() */
  111. static int __match_zcdn_name(struct device *dev, const void *data)
  112. {
  113. return strcmp(dev_name(dev), (const char *)data) == 0;
  114. }
  115. /* helper function, matches the devt value for find_zcdndev_by_devt() */
  116. static int __match_zcdn_devt(struct device *dev, const void *data)
  117. {
  118. return dev->devt == *((dev_t *) data);
  119. }
  120. /*
  121. * Find zcdn device by name.
  122. * Returns reference to the zcdn device which needs to be released
  123. * with put_device() after use.
  124. */
  125. static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
  126. {
  127. struct device *dev =
  128. class_find_device(zcrypt_class, NULL,
  129. (void *) name,
  130. __match_zcdn_name);
  131. return dev ? to_zcdn_dev(dev) : NULL;
  132. }
  133. /*
  134. * Find zcdn device by devt value.
  135. * Returns reference to the zcdn device which needs to be released
  136. * with put_device() after use.
  137. */
  138. static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
  139. {
  140. struct device *dev =
  141. class_find_device(zcrypt_class, NULL,
  142. (void *) &devt,
  143. __match_zcdn_devt);
  144. return dev ? to_zcdn_dev(dev) : NULL;
  145. }
  146. static ssize_t ioctlmask_show(struct device *dev,
  147. struct device_attribute *attr,
  148. char *buf)
  149. {
  150. int i, rc;
  151. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  152. if (mutex_lock_interruptible(&ap_perms_mutex))
  153. return -ERESTARTSYS;
  154. buf[0] = '0';
  155. buf[1] = 'x';
  156. for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
  157. snprintf(buf + 2 + 2 * i * sizeof(long),
  158. PAGE_SIZE - 2 - 2 * i * sizeof(long),
  159. "%016lx", zcdndev->perms.ioctlm[i]);
  160. buf[2 + 2 * i * sizeof(long)] = '\n';
  161. buf[2 + 2 * i * sizeof(long) + 1] = '\0';
  162. rc = 2 + 2 * i * sizeof(long) + 1;
  163. mutex_unlock(&ap_perms_mutex);
  164. return rc;
  165. }
  166. static ssize_t ioctlmask_store(struct device *dev,
  167. struct device_attribute *attr,
  168. const char *buf, size_t count)
  169. {
  170. int rc;
  171. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  172. rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
  173. AP_IOCTLS, &ap_perms_mutex);
  174. if (rc)
  175. return rc;
  176. return count;
  177. }
  178. static DEVICE_ATTR_RW(ioctlmask);
  179. static ssize_t apmask_show(struct device *dev,
  180. struct device_attribute *attr,
  181. char *buf)
  182. {
  183. int i, rc;
  184. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  185. if (mutex_lock_interruptible(&ap_perms_mutex))
  186. return -ERESTARTSYS;
  187. buf[0] = '0';
  188. buf[1] = 'x';
  189. for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
  190. snprintf(buf + 2 + 2 * i * sizeof(long),
  191. PAGE_SIZE - 2 - 2 * i * sizeof(long),
  192. "%016lx", zcdndev->perms.apm[i]);
  193. buf[2 + 2 * i * sizeof(long)] = '\n';
  194. buf[2 + 2 * i * sizeof(long) + 1] = '\0';
  195. rc = 2 + 2 * i * sizeof(long) + 1;
  196. mutex_unlock(&ap_perms_mutex);
  197. return rc;
  198. }
  199. static ssize_t apmask_store(struct device *dev,
  200. struct device_attribute *attr,
  201. const char *buf, size_t count)
  202. {
  203. int rc;
  204. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  205. rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
  206. AP_DEVICES, &ap_perms_mutex);
  207. if (rc)
  208. return rc;
  209. return count;
  210. }
  211. static DEVICE_ATTR_RW(apmask);
  212. static ssize_t aqmask_show(struct device *dev,
  213. struct device_attribute *attr,
  214. char *buf)
  215. {
  216. int i, rc;
  217. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  218. if (mutex_lock_interruptible(&ap_perms_mutex))
  219. return -ERESTARTSYS;
  220. buf[0] = '0';
  221. buf[1] = 'x';
  222. for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
  223. snprintf(buf + 2 + 2 * i * sizeof(long),
  224. PAGE_SIZE - 2 - 2 * i * sizeof(long),
  225. "%016lx", zcdndev->perms.aqm[i]);
  226. buf[2 + 2 * i * sizeof(long)] = '\n';
  227. buf[2 + 2 * i * sizeof(long) + 1] = '\0';
  228. rc = 2 + 2 * i * sizeof(long) + 1;
  229. mutex_unlock(&ap_perms_mutex);
  230. return rc;
  231. }
  232. static ssize_t aqmask_store(struct device *dev,
  233. struct device_attribute *attr,
  234. const char *buf, size_t count)
  235. {
  236. int rc;
  237. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  238. rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
  239. AP_DOMAINS, &ap_perms_mutex);
  240. if (rc)
  241. return rc;
  242. return count;
  243. }
  244. static DEVICE_ATTR_RW(aqmask);
  245. static struct attribute *zcdn_dev_attrs[] = {
  246. &dev_attr_ioctlmask.attr,
  247. &dev_attr_apmask.attr,
  248. &dev_attr_aqmask.attr,
  249. NULL
  250. };
  251. static struct attribute_group zcdn_dev_attr_group = {
  252. .attrs = zcdn_dev_attrs
  253. };
  254. static const struct attribute_group *zcdn_dev_attr_groups[] = {
  255. &zcdn_dev_attr_group,
  256. NULL
  257. };
  258. static ssize_t zcdn_create_store(struct class *class,
  259. struct class_attribute *attr,
  260. const char *buf, size_t count)
  261. {
  262. int rc;
  263. char name[ZCDN_MAX_NAME];
  264. strncpy(name, skip_spaces(buf), sizeof(name));
  265. name[sizeof(name) - 1] = '\0';
  266. rc = zcdn_create(strim(name));
  267. return rc ? rc : count;
  268. }
  269. static const struct class_attribute class_attr_zcdn_create =
  270. __ATTR(create, 0600, NULL, zcdn_create_store);
  271. static ssize_t zcdn_destroy_store(struct class *class,
  272. struct class_attribute *attr,
  273. const char *buf, size_t count)
  274. {
  275. int rc;
  276. char name[ZCDN_MAX_NAME];
  277. strncpy(name, skip_spaces(buf), sizeof(name));
  278. name[sizeof(name) - 1] = '\0';
  279. rc = zcdn_destroy(strim(name));
  280. return rc ? rc : count;
  281. }
  282. static const struct class_attribute class_attr_zcdn_destroy =
  283. __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
  284. static void zcdn_device_release(struct device *dev)
  285. {
  286. struct zcdn_device *zcdndev = to_zcdn_dev(dev);
  287. ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
  288. MAJOR(dev->devt), MINOR(dev->devt));
  289. kfree(zcdndev);
  290. }
  291. static int zcdn_create(const char *name)
  292. {
  293. dev_t devt;
  294. int i, rc = 0;
  295. char nodename[ZCDN_MAX_NAME];
  296. struct zcdn_device *zcdndev;
  297. if (mutex_lock_interruptible(&ap_perms_mutex))
  298. return -ERESTARTSYS;
  299. /* check if device node with this name already exists */
  300. if (name[0]) {
  301. zcdndev = find_zcdndev_by_name(name);
  302. if (zcdndev) {
  303. put_device(&zcdndev->device);
  304. rc = -EEXIST;
  305. goto unlockout;
  306. }
  307. }
  308. /* find an unused minor number */
  309. for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
  310. devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
  311. zcdndev = find_zcdndev_by_devt(devt);
  312. if (zcdndev)
  313. put_device(&zcdndev->device);
  314. else
  315. break;
  316. }
  317. if (i == ZCRYPT_MAX_MINOR_NODES) {
  318. rc = -ENOSPC;
  319. goto unlockout;
  320. }
  321. /* alloc and prepare a new zcdn device */
  322. zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
  323. if (!zcdndev) {
  324. rc = -ENOMEM;
  325. goto unlockout;
  326. }
  327. zcdndev->device.release = zcdn_device_release;
  328. zcdndev->device.class = zcrypt_class;
  329. zcdndev->device.devt = devt;
  330. zcdndev->device.groups = zcdn_dev_attr_groups;
  331. if (name[0])
  332. strncpy(nodename, name, sizeof(nodename));
  333. else
  334. snprintf(nodename, sizeof(nodename),
  335. ZCRYPT_NAME "_%d", (int) MINOR(devt));
  336. nodename[sizeof(nodename)-1] = '\0';
  337. if (dev_set_name(&zcdndev->device, nodename)) {
  338. rc = -EINVAL;
  339. goto unlockout;
  340. }
  341. rc = device_register(&zcdndev->device);
  342. if (rc) {
  343. put_device(&zcdndev->device);
  344. goto unlockout;
  345. }
  346. ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
  347. MAJOR(devt), MINOR(devt));
  348. unlockout:
  349. mutex_unlock(&ap_perms_mutex);
  350. return rc;
  351. }
  352. static int zcdn_destroy(const char *name)
  353. {
  354. int rc = 0;
  355. struct zcdn_device *zcdndev;
  356. if (mutex_lock_interruptible(&ap_perms_mutex))
  357. return -ERESTARTSYS;
  358. /* try to find this zcdn device */
  359. zcdndev = find_zcdndev_by_name(name);
  360. if (!zcdndev) {
  361. rc = -ENOENT;
  362. goto unlockout;
  363. }
  364. /*
  365. * The zcdn device is not hard destroyed. It is subject to
  366. * reference counting and thus just needs to be unregistered.
  367. */
  368. put_device(&zcdndev->device);
  369. device_unregister(&zcdndev->device);
  370. unlockout:
  371. mutex_unlock(&ap_perms_mutex);
  372. return rc;
  373. }
  374. static void zcdn_destroy_all(void)
  375. {
  376. int i;
  377. dev_t devt;
  378. struct zcdn_device *zcdndev;
  379. mutex_lock(&ap_perms_mutex);
  380. for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
  381. devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
  382. zcdndev = find_zcdndev_by_devt(devt);
  383. if (zcdndev) {
  384. put_device(&zcdndev->device);
  385. device_unregister(&zcdndev->device);
  386. }
  387. }
  388. mutex_unlock(&ap_perms_mutex);
  389. }
  390. #endif
  391. /**
  392. * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
  393. *
  394. * This function is not supported beyond zcrypt 1.3.1.
  395. */
  396. static ssize_t zcrypt_read(struct file *filp, char __user *buf,
  397. size_t count, loff_t *f_pos)
  398. {
  399. return -EPERM;
  400. }
  401. /**
  402. * zcrypt_write(): Not allowed.
  403. *
  404. * Write is is not allowed
  405. */
  406. static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
  407. size_t count, loff_t *f_pos)
  408. {
  409. return -EPERM;
  410. }
  411. /**
  412. * zcrypt_open(): Count number of users.
  413. *
  414. * Device open function to count number of users.
  415. */
  416. static int zcrypt_open(struct inode *inode, struct file *filp)
  417. {
  418. struct ap_perms *perms = &ap_perms;
  419. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  420. if (filp->f_inode->i_cdev == &zcrypt_cdev) {
  421. struct zcdn_device *zcdndev;
  422. if (mutex_lock_interruptible(&ap_perms_mutex))
  423. return -ERESTARTSYS;
  424. zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
  425. /* find returns a reference, no get_device() needed */
  426. mutex_unlock(&ap_perms_mutex);
  427. if (zcdndev)
  428. perms = &zcdndev->perms;
  429. }
  430. #endif
  431. filp->private_data = (void *) perms;
  432. atomic_inc(&zcrypt_open_count);
  433. return nonseekable_open(inode, filp);
  434. }
  435. /**
  436. * zcrypt_release(): Count number of users.
  437. *
  438. * Device close function to count number of users.
  439. */
  440. static int zcrypt_release(struct inode *inode, struct file *filp)
  441. {
  442. #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
  443. if (filp->f_inode->i_cdev == &zcrypt_cdev) {
  444. struct zcdn_device *zcdndev;
  445. if (mutex_lock_interruptible(&ap_perms_mutex))
  446. return -ERESTARTSYS;
  447. zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
  448. mutex_unlock(&ap_perms_mutex);
  449. if (zcdndev) {
  450. /* 2 puts here: one for find, one for open */
  451. put_device(&zcdndev->device);
  452. put_device(&zcdndev->device);
  453. }
  454. }
  455. #endif
  456. atomic_dec(&zcrypt_open_count);
  457. return 0;
  458. }
  459. static inline int zcrypt_check_ioctl(struct ap_perms *perms,
  460. unsigned int cmd)
  461. {
  462. int rc = -EPERM;
  463. int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
  464. if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
  465. if (test_bit_inv(ioctlnr, perms->ioctlm))
  466. rc = 0;
  467. }
  468. if (rc)
  469. ZCRYPT_DBF(DBF_WARN,
  470. "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
  471. ioctlnr, rc);
  472. return rc;
  473. }
  474. static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
  475. {
  476. return test_bit_inv(card, perms->apm) ? true : false;
  477. }
  478. static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
  479. {
  480. return test_bit_inv(queue, perms->aqm) ? true : false;
  481. }
  482. static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
  483. struct zcrypt_queue *zq,
  484. unsigned int weight)
  485. {
  486. if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
  487. return NULL;
  488. zcrypt_queue_get(zq);
  489. get_device(&zq->queue->ap_dev.device);
  490. atomic_add(weight, &zc->load);
  491. atomic_add(weight, &zq->load);
  492. zq->request_count++;
  493. return zq;
  494. }
  495. static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
  496. struct zcrypt_queue *zq,
  497. unsigned int weight)
  498. {
  499. struct module *mod = zq->queue->ap_dev.drv->driver.owner;
  500. zq->request_count--;
  501. atomic_sub(weight, &zc->load);
  502. atomic_sub(weight, &zq->load);
  503. put_device(&zq->queue->ap_dev.device);
  504. zcrypt_queue_put(zq);
  505. module_put(mod);
  506. }
  507. static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
  508. struct zcrypt_card *pref_zc,
  509. unsigned int weight,
  510. unsigned int pref_weight)
  511. {
  512. if (!pref_zc)
  513. return false;
  514. weight += atomic_read(&zc->load);
  515. pref_weight += atomic_read(&pref_zc->load);
  516. if (weight == pref_weight)
  517. return atomic_read(&zc->card->total_request_count) >
  518. atomic_read(&pref_zc->card->total_request_count);
  519. return weight > pref_weight;
  520. }
  521. static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
  522. struct zcrypt_queue *pref_zq,
  523. unsigned int weight,
  524. unsigned int pref_weight)
  525. {
  526. if (!pref_zq)
  527. return false;
  528. weight += atomic_read(&zq->load);
  529. pref_weight += atomic_read(&pref_zq->load);
  530. if (weight == pref_weight)
  531. return zq->queue->total_request_count >
  532. pref_zq->queue->total_request_count;
  533. return weight > pref_weight;
  534. }
  535. /*
  536. * zcrypt ioctls.
  537. */
  538. static long zcrypt_rsa_modexpo(struct ap_perms *perms,
  539. struct ica_rsa_modexpo *mex)
  540. {
  541. struct zcrypt_card *zc, *pref_zc;
  542. struct zcrypt_queue *zq, *pref_zq;
  543. unsigned int weight, pref_weight;
  544. unsigned int func_code;
  545. int qid = 0, rc = -ENODEV;
  546. trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
  547. if (mex->outputdatalength < mex->inputdatalength) {
  548. rc = -EINVAL;
  549. goto out;
  550. }
  551. /*
  552. * As long as outputdatalength is big enough, we can set the
  553. * outputdatalength equal to the inputdatalength, since that is the
  554. * number of bytes we will copy in any case
  555. */
  556. mex->outputdatalength = mex->inputdatalength;
  557. rc = get_rsa_modex_fc(mex, &func_code);
  558. if (rc)
  559. goto out;
  560. pref_zc = NULL;
  561. pref_zq = NULL;
  562. spin_lock(&zcrypt_list_lock);
  563. for_each_zcrypt_card(zc) {
  564. /* Check for online accelarator and CCA cards */
  565. if (!zc->online || !(zc->card->functions & 0x18000000))
  566. continue;
  567. /* Check for size limits */
  568. if (zc->min_mod_size > mex->inputdatalength ||
  569. zc->max_mod_size < mex->inputdatalength)
  570. continue;
  571. /* check if device node has admission for this card */
  572. if (!zcrypt_check_card(perms, zc->card->id))
  573. continue;
  574. /* get weight index of the card device */
  575. weight = zc->speed_rating[func_code];
  576. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  577. continue;
  578. for_each_zcrypt_queue(zq, zc) {
  579. /* check if device is online and eligible */
  580. if (!zq->online || !zq->ops->rsa_modexpo)
  581. continue;
  582. /* check if device node has admission for this queue */
  583. if (!zcrypt_check_queue(perms,
  584. AP_QID_QUEUE(zq->queue->qid)))
  585. continue;
  586. if (zcrypt_queue_compare(zq, pref_zq,
  587. weight, pref_weight))
  588. continue;
  589. pref_zc = zc;
  590. pref_zq = zq;
  591. pref_weight = weight;
  592. }
  593. }
  594. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  595. spin_unlock(&zcrypt_list_lock);
  596. if (!pref_zq) {
  597. rc = -ENODEV;
  598. goto out;
  599. }
  600. qid = pref_zq->queue->qid;
  601. rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
  602. spin_lock(&zcrypt_list_lock);
  603. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  604. spin_unlock(&zcrypt_list_lock);
  605. out:
  606. trace_s390_zcrypt_rep(mex, func_code, rc,
  607. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  608. return rc;
  609. }
  610. static long zcrypt_rsa_crt(struct ap_perms *perms,
  611. struct ica_rsa_modexpo_crt *crt)
  612. {
  613. struct zcrypt_card *zc, *pref_zc;
  614. struct zcrypt_queue *zq, *pref_zq;
  615. unsigned int weight, pref_weight;
  616. unsigned int func_code;
  617. int qid = 0, rc = -ENODEV;
  618. trace_s390_zcrypt_req(crt, TP_ICARSACRT);
  619. if (crt->outputdatalength < crt->inputdatalength) {
  620. rc = -EINVAL;
  621. goto out;
  622. }
  623. /*
  624. * As long as outputdatalength is big enough, we can set the
  625. * outputdatalength equal to the inputdatalength, since that is the
  626. * number of bytes we will copy in any case
  627. */
  628. crt->outputdatalength = crt->inputdatalength;
  629. rc = get_rsa_crt_fc(crt, &func_code);
  630. if (rc)
  631. goto out;
  632. pref_zc = NULL;
  633. pref_zq = NULL;
  634. spin_lock(&zcrypt_list_lock);
  635. for_each_zcrypt_card(zc) {
  636. /* Check for online accelarator and CCA cards */
  637. if (!zc->online || !(zc->card->functions & 0x18000000))
  638. continue;
  639. /* Check for size limits */
  640. if (zc->min_mod_size > crt->inputdatalength ||
  641. zc->max_mod_size < crt->inputdatalength)
  642. continue;
  643. /* check if device node has admission for this card */
  644. if (!zcrypt_check_card(perms, zc->card->id))
  645. continue;
  646. /* get weight index of the card device */
  647. weight = zc->speed_rating[func_code];
  648. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  649. continue;
  650. for_each_zcrypt_queue(zq, zc) {
  651. /* check if device is online and eligible */
  652. if (!zq->online || !zq->ops->rsa_modexpo_crt)
  653. continue;
  654. /* check if device node has admission for this queue */
  655. if (!zcrypt_check_queue(perms,
  656. AP_QID_QUEUE(zq->queue->qid)))
  657. continue;
  658. if (zcrypt_queue_compare(zq, pref_zq,
  659. weight, pref_weight))
  660. continue;
  661. pref_zc = zc;
  662. pref_zq = zq;
  663. pref_weight = weight;
  664. }
  665. }
  666. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  667. spin_unlock(&zcrypt_list_lock);
  668. if (!pref_zq) {
  669. rc = -ENODEV;
  670. goto out;
  671. }
  672. qid = pref_zq->queue->qid;
  673. rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
  674. spin_lock(&zcrypt_list_lock);
  675. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  676. spin_unlock(&zcrypt_list_lock);
  677. out:
  678. trace_s390_zcrypt_rep(crt, func_code, rc,
  679. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  680. return rc;
  681. }
  682. static long _zcrypt_send_cprb(struct ap_perms *perms,
  683. struct ica_xcRB *xcRB)
  684. {
  685. struct zcrypt_card *zc, *pref_zc;
  686. struct zcrypt_queue *zq, *pref_zq;
  687. struct ap_message ap_msg;
  688. unsigned int weight, pref_weight;
  689. unsigned int func_code;
  690. unsigned short *domain;
  691. int qid = 0, rc = -ENODEV;
  692. trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
  693. xcRB->status = 0;
  694. ap_init_message(&ap_msg);
  695. rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
  696. if (rc)
  697. goto out;
  698. pref_zc = NULL;
  699. pref_zq = NULL;
  700. spin_lock(&zcrypt_list_lock);
  701. for_each_zcrypt_card(zc) {
  702. /* Check for online CCA cards */
  703. if (!zc->online || !(zc->card->functions & 0x10000000))
  704. continue;
  705. /* Check for user selected CCA card */
  706. if (xcRB->user_defined != AUTOSELECT &&
  707. xcRB->user_defined != zc->card->id)
  708. continue;
  709. /* check if device node has admission for this card */
  710. if (!zcrypt_check_card(perms, zc->card->id))
  711. continue;
  712. /* get weight index of the card device */
  713. weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
  714. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  715. continue;
  716. for_each_zcrypt_queue(zq, zc) {
  717. /* check if device is online and eligible */
  718. if (!zq->online ||
  719. !zq->ops->send_cprb ||
  720. ((*domain != (unsigned short) AUTOSELECT) &&
  721. (*domain != AP_QID_QUEUE(zq->queue->qid))))
  722. continue;
  723. /* check if device node has admission for this queue */
  724. if (!zcrypt_check_queue(perms,
  725. AP_QID_QUEUE(zq->queue->qid)))
  726. continue;
  727. if (zcrypt_queue_compare(zq, pref_zq,
  728. weight, pref_weight))
  729. continue;
  730. pref_zc = zc;
  731. pref_zq = zq;
  732. pref_weight = weight;
  733. }
  734. }
  735. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  736. spin_unlock(&zcrypt_list_lock);
  737. if (!pref_zq) {
  738. rc = -ENODEV;
  739. goto out;
  740. }
  741. /* in case of auto select, provide the correct domain */
  742. qid = pref_zq->queue->qid;
  743. if (*domain == (unsigned short) AUTOSELECT)
  744. *domain = AP_QID_QUEUE(qid);
  745. rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
  746. spin_lock(&zcrypt_list_lock);
  747. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  748. spin_unlock(&zcrypt_list_lock);
  749. out:
  750. ap_release_message(&ap_msg);
  751. trace_s390_zcrypt_rep(xcRB, func_code, rc,
  752. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  753. return rc;
  754. }
  755. long zcrypt_send_cprb(struct ica_xcRB *xcRB)
  756. {
  757. return _zcrypt_send_cprb(&ap_perms, xcRB);
  758. }
  759. EXPORT_SYMBOL(zcrypt_send_cprb);
  760. static bool is_desired_ep11_card(unsigned int dev_id,
  761. unsigned short target_num,
  762. struct ep11_target_dev *targets)
  763. {
  764. while (target_num-- > 0) {
  765. if (dev_id == targets->ap_id)
  766. return true;
  767. targets++;
  768. }
  769. return false;
  770. }
  771. static bool is_desired_ep11_queue(unsigned int dev_qid,
  772. unsigned short target_num,
  773. struct ep11_target_dev *targets)
  774. {
  775. while (target_num-- > 0) {
  776. if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
  777. return true;
  778. targets++;
  779. }
  780. return false;
  781. }
  782. static long zcrypt_send_ep11_cprb(struct ap_perms *perms,
  783. struct ep11_urb *xcrb)
  784. {
  785. struct zcrypt_card *zc, *pref_zc;
  786. struct zcrypt_queue *zq, *pref_zq;
  787. struct ep11_target_dev *targets;
  788. unsigned short target_num;
  789. unsigned int weight, pref_weight;
  790. unsigned int func_code;
  791. struct ap_message ap_msg;
  792. int qid = 0, rc = -ENODEV;
  793. trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
  794. ap_init_message(&ap_msg);
  795. target_num = (unsigned short) xcrb->targets_num;
  796. /* empty list indicates autoselect (all available targets) */
  797. targets = NULL;
  798. if (target_num != 0) {
  799. struct ep11_target_dev __user *uptr;
  800. targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
  801. if (!targets) {
  802. rc = -ENOMEM;
  803. goto out;
  804. }
  805. uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
  806. if (copy_from_user(targets, uptr,
  807. target_num * sizeof(*targets))) {
  808. rc = -EFAULT;
  809. goto out_free;
  810. }
  811. }
  812. rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
  813. if (rc)
  814. goto out_free;
  815. pref_zc = NULL;
  816. pref_zq = NULL;
  817. spin_lock(&zcrypt_list_lock);
  818. for_each_zcrypt_card(zc) {
  819. /* Check for online EP11 cards */
  820. if (!zc->online || !(zc->card->functions & 0x04000000))
  821. continue;
  822. /* Check for user selected EP11 card */
  823. if (targets &&
  824. !is_desired_ep11_card(zc->card->id, target_num, targets))
  825. continue;
  826. /* check if device node has admission for this card */
  827. if (!zcrypt_check_card(perms, zc->card->id))
  828. continue;
  829. /* get weight index of the card device */
  830. weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
  831. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  832. continue;
  833. for_each_zcrypt_queue(zq, zc) {
  834. /* check if device is online and eligible */
  835. if (!zq->online ||
  836. !zq->ops->send_ep11_cprb ||
  837. (targets &&
  838. !is_desired_ep11_queue(zq->queue->qid,
  839. target_num, targets)))
  840. continue;
  841. /* check if device node has admission for this queue */
  842. if (!zcrypt_check_queue(perms,
  843. AP_QID_QUEUE(zq->queue->qid)))
  844. continue;
  845. if (zcrypt_queue_compare(zq, pref_zq,
  846. weight, pref_weight))
  847. continue;
  848. pref_zc = zc;
  849. pref_zq = zq;
  850. pref_weight = weight;
  851. }
  852. }
  853. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  854. spin_unlock(&zcrypt_list_lock);
  855. if (!pref_zq) {
  856. rc = -ENODEV;
  857. goto out_free;
  858. }
  859. qid = pref_zq->queue->qid;
  860. rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
  861. spin_lock(&zcrypt_list_lock);
  862. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  863. spin_unlock(&zcrypt_list_lock);
  864. out_free:
  865. kfree(targets);
  866. out:
  867. ap_release_message(&ap_msg);
  868. trace_s390_zcrypt_rep(xcrb, func_code, rc,
  869. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  870. return rc;
  871. }
  872. static long zcrypt_rng(char *buffer)
  873. {
  874. struct zcrypt_card *zc, *pref_zc;
  875. struct zcrypt_queue *zq, *pref_zq;
  876. unsigned int weight, pref_weight;
  877. unsigned int func_code;
  878. struct ap_message ap_msg;
  879. unsigned int domain;
  880. int qid = 0, rc = -ENODEV;
  881. trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
  882. ap_init_message(&ap_msg);
  883. rc = get_rng_fc(&ap_msg, &func_code, &domain);
  884. if (rc)
  885. goto out;
  886. pref_zc = NULL;
  887. pref_zq = NULL;
  888. spin_lock(&zcrypt_list_lock);
  889. for_each_zcrypt_card(zc) {
  890. /* Check for online CCA cards */
  891. if (!zc->online || !(zc->card->functions & 0x10000000))
  892. continue;
  893. /* get weight index of the card device */
  894. weight = zc->speed_rating[func_code];
  895. if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
  896. continue;
  897. for_each_zcrypt_queue(zq, zc) {
  898. /* check if device is online and eligible */
  899. if (!zq->online || !zq->ops->rng)
  900. continue;
  901. if (zcrypt_queue_compare(zq, pref_zq,
  902. weight, pref_weight))
  903. continue;
  904. pref_zc = zc;
  905. pref_zq = zq;
  906. pref_weight = weight;
  907. }
  908. }
  909. pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
  910. spin_unlock(&zcrypt_list_lock);
  911. if (!pref_zq) {
  912. rc = -ENODEV;
  913. goto out;
  914. }
  915. qid = pref_zq->queue->qid;
  916. rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
  917. spin_lock(&zcrypt_list_lock);
  918. zcrypt_drop_queue(pref_zc, pref_zq, weight);
  919. spin_unlock(&zcrypt_list_lock);
  920. out:
  921. ap_release_message(&ap_msg);
  922. trace_s390_zcrypt_rep(buffer, func_code, rc,
  923. AP_QID_CARD(qid), AP_QID_QUEUE(qid));
  924. return rc;
  925. }
  926. static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
  927. {
  928. struct zcrypt_card *zc;
  929. struct zcrypt_queue *zq;
  930. struct zcrypt_device_status *stat;
  931. int card, queue;
  932. memset(devstatus, 0, MAX_ZDEV_ENTRIES
  933. * sizeof(struct zcrypt_device_status));
  934. spin_lock(&zcrypt_list_lock);
  935. for_each_zcrypt_card(zc) {
  936. for_each_zcrypt_queue(zq, zc) {
  937. card = AP_QID_CARD(zq->queue->qid);
  938. if (card >= MAX_ZDEV_CARDIDS)
  939. continue;
  940. queue = AP_QID_QUEUE(zq->queue->qid);
  941. stat = &devstatus[card * AP_DOMAINS + queue];
  942. stat->hwtype = zc->card->ap_dev.device_type;
  943. stat->functions = zc->card->functions >> 26;
  944. stat->qid = zq->queue->qid;
  945. stat->online = zq->online ? 0x01 : 0x00;
  946. }
  947. }
  948. spin_unlock(&zcrypt_list_lock);
  949. }
  950. void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
  951. {
  952. struct zcrypt_card *zc;
  953. struct zcrypt_queue *zq;
  954. struct zcrypt_device_status_ext *stat;
  955. int card, queue;
  956. memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
  957. * sizeof(struct zcrypt_device_status_ext));
  958. spin_lock(&zcrypt_list_lock);
  959. for_each_zcrypt_card(zc) {
  960. for_each_zcrypt_queue(zq, zc) {
  961. card = AP_QID_CARD(zq->queue->qid);
  962. queue = AP_QID_QUEUE(zq->queue->qid);
  963. stat = &devstatus[card * AP_DOMAINS + queue];
  964. stat->hwtype = zc->card->ap_dev.device_type;
  965. stat->functions = zc->card->functions >> 26;
  966. stat->qid = zq->queue->qid;
  967. stat->online = zq->online ? 0x01 : 0x00;
  968. }
  969. }
  970. spin_unlock(&zcrypt_list_lock);
  971. }
  972. EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
  973. static void zcrypt_status_mask(char status[], size_t max_adapters)
  974. {
  975. struct zcrypt_card *zc;
  976. struct zcrypt_queue *zq;
  977. int card;
  978. memset(status, 0, max_adapters);
  979. spin_lock(&zcrypt_list_lock);
  980. for_each_zcrypt_card(zc) {
  981. for_each_zcrypt_queue(zq, zc) {
  982. card = AP_QID_CARD(zq->queue->qid);
  983. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
  984. || card >= max_adapters)
  985. continue;
  986. status[card] = zc->online ? zc->user_space_type : 0x0d;
  987. }
  988. }
  989. spin_unlock(&zcrypt_list_lock);
  990. }
  991. static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
  992. {
  993. struct zcrypt_card *zc;
  994. struct zcrypt_queue *zq;
  995. int card;
  996. memset(qdepth, 0, max_adapters);
  997. spin_lock(&zcrypt_list_lock);
  998. local_bh_disable();
  999. for_each_zcrypt_card(zc) {
  1000. for_each_zcrypt_queue(zq, zc) {
  1001. card = AP_QID_CARD(zq->queue->qid);
  1002. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
  1003. || card >= max_adapters)
  1004. continue;
  1005. spin_lock(&zq->queue->lock);
  1006. qdepth[card] =
  1007. zq->queue->pendingq_count +
  1008. zq->queue->requestq_count;
  1009. spin_unlock(&zq->queue->lock);
  1010. }
  1011. }
  1012. local_bh_enable();
  1013. spin_unlock(&zcrypt_list_lock);
  1014. }
  1015. static void zcrypt_perdev_reqcnt(int reqcnt[], size_t max_adapters)
  1016. {
  1017. struct zcrypt_card *zc;
  1018. struct zcrypt_queue *zq;
  1019. int card;
  1020. memset(reqcnt, 0, sizeof(int) * max_adapters);
  1021. spin_lock(&zcrypt_list_lock);
  1022. local_bh_disable();
  1023. for_each_zcrypt_card(zc) {
  1024. for_each_zcrypt_queue(zq, zc) {
  1025. card = AP_QID_CARD(zq->queue->qid);
  1026. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
  1027. || card >= max_adapters)
  1028. continue;
  1029. spin_lock(&zq->queue->lock);
  1030. reqcnt[card] = zq->queue->total_request_count;
  1031. spin_unlock(&zq->queue->lock);
  1032. }
  1033. }
  1034. local_bh_enable();
  1035. spin_unlock(&zcrypt_list_lock);
  1036. }
  1037. static int zcrypt_pendingq_count(void)
  1038. {
  1039. struct zcrypt_card *zc;
  1040. struct zcrypt_queue *zq;
  1041. int pendingq_count;
  1042. pendingq_count = 0;
  1043. spin_lock(&zcrypt_list_lock);
  1044. local_bh_disable();
  1045. for_each_zcrypt_card(zc) {
  1046. for_each_zcrypt_queue(zq, zc) {
  1047. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1048. continue;
  1049. spin_lock(&zq->queue->lock);
  1050. pendingq_count += zq->queue->pendingq_count;
  1051. spin_unlock(&zq->queue->lock);
  1052. }
  1053. }
  1054. local_bh_enable();
  1055. spin_unlock(&zcrypt_list_lock);
  1056. return pendingq_count;
  1057. }
  1058. static int zcrypt_requestq_count(void)
  1059. {
  1060. struct zcrypt_card *zc;
  1061. struct zcrypt_queue *zq;
  1062. int requestq_count;
  1063. requestq_count = 0;
  1064. spin_lock(&zcrypt_list_lock);
  1065. local_bh_disable();
  1066. for_each_zcrypt_card(zc) {
  1067. for_each_zcrypt_queue(zq, zc) {
  1068. if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
  1069. continue;
  1070. spin_lock(&zq->queue->lock);
  1071. requestq_count += zq->queue->requestq_count;
  1072. spin_unlock(&zq->queue->lock);
  1073. }
  1074. }
  1075. local_bh_enable();
  1076. spin_unlock(&zcrypt_list_lock);
  1077. return requestq_count;
  1078. }
  1079. static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
  1080. unsigned long arg)
  1081. {
  1082. int rc;
  1083. struct ap_perms *perms =
  1084. (struct ap_perms *) filp->private_data;
  1085. rc = zcrypt_check_ioctl(perms, cmd);
  1086. if (rc)
  1087. return rc;
  1088. switch (cmd) {
  1089. case ICARSAMODEXPO: {
  1090. struct ica_rsa_modexpo __user *umex = (void __user *) arg;
  1091. struct ica_rsa_modexpo mex;
  1092. if (copy_from_user(&mex, umex, sizeof(mex)))
  1093. return -EFAULT;
  1094. do {
  1095. rc = zcrypt_rsa_modexpo(perms, &mex);
  1096. } while (rc == -EAGAIN);
  1097. /* on failure: retry once again after a requested rescan */
  1098. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1099. do {
  1100. rc = zcrypt_rsa_modexpo(perms, &mex);
  1101. } while (rc == -EAGAIN);
  1102. if (rc) {
  1103. ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
  1104. return rc;
  1105. }
  1106. return put_user(mex.outputdatalength, &umex->outputdatalength);
  1107. }
  1108. case ICARSACRT: {
  1109. struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
  1110. struct ica_rsa_modexpo_crt crt;
  1111. if (copy_from_user(&crt, ucrt, sizeof(crt)))
  1112. return -EFAULT;
  1113. do {
  1114. rc = zcrypt_rsa_crt(perms, &crt);
  1115. } while (rc == -EAGAIN);
  1116. /* on failure: retry once again after a requested rescan */
  1117. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1118. do {
  1119. rc = zcrypt_rsa_crt(perms, &crt);
  1120. } while (rc == -EAGAIN);
  1121. if (rc) {
  1122. ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
  1123. return rc;
  1124. }
  1125. return put_user(crt.outputdatalength, &ucrt->outputdatalength);
  1126. }
  1127. case ZSECSENDCPRB: {
  1128. struct ica_xcRB __user *uxcRB = (void __user *) arg;
  1129. struct ica_xcRB xcRB;
  1130. if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
  1131. return -EFAULT;
  1132. do {
  1133. rc = _zcrypt_send_cprb(perms, &xcRB);
  1134. } while (rc == -EAGAIN);
  1135. /* on failure: retry once again after a requested rescan */
  1136. if ((rc == -ENODEV) && (zcrypt_process_rescan()))
  1137. do {
  1138. rc = _zcrypt_send_cprb(perms, &xcRB);
  1139. } while (rc == -EAGAIN);
  1140. if (rc)
  1141. ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
  1142. rc, xcRB.status);
  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);