zcrypt_cex2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. *
  7. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  8. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  10. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/atomic.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/mod_devicetable.h>
  20. #include "ap_bus.h"
  21. #include "zcrypt_api.h"
  22. #include "zcrypt_error.h"
  23. #include "zcrypt_msgtype6.h"
  24. #include "zcrypt_cex2c.h"
  25. #include "zcrypt_cca_key.h"
  26. #include "zcrypt_ccamisc.h"
  27. #define CEX2C_MIN_MOD_SIZE 16 /* 128 bits */
  28. #define CEX2C_MAX_MOD_SIZE 256 /* 2048 bits */
  29. #define CEX3C_MIN_MOD_SIZE 16 /* 128 bits */
  30. #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
  31. #define CEX2C_MAX_XCRB_MESSAGE_SIZE (12*1024)
  32. #define CEX2C_CLEANUP_TIME (15*HZ)
  33. MODULE_AUTHOR("IBM Corporation");
  34. MODULE_DESCRIPTION("CEX2C/CEX3C Cryptographic Coprocessor device driver, " \
  35. "Copyright IBM Corp. 2001, 2018");
  36. MODULE_LICENSE("GPL");
  37. static struct ap_device_id zcrypt_cex2c_card_ids[] = {
  38. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  39. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  40. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  41. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  42. { /* end of list */ },
  43. };
  44. MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_card_ids);
  45. static struct ap_device_id zcrypt_cex2c_queue_ids[] = {
  46. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  47. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  48. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  49. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  50. { /* end of list */ },
  51. };
  52. MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_queue_ids);
  53. /*
  54. * CCA card additional device attributes
  55. */
  56. static ssize_t cca_serialnr_show(struct device *dev,
  57. struct device_attribute *attr,
  58. char *buf)
  59. {
  60. struct cca_info ci;
  61. struct ap_card *ac = to_ap_card(dev);
  62. struct zcrypt_card *zc = ac->private;
  63. memset(&ci, 0, sizeof(ci));
  64. if (ap_domain_index >= 0)
  65. cca_get_info(ac->id, ap_domain_index, &ci, zc->online);
  66. return scnprintf(buf, PAGE_SIZE, "%s\n", ci.serial);
  67. }
  68. static struct device_attribute dev_attr_cca_serialnr =
  69. __ATTR(serialnr, 0444, cca_serialnr_show, NULL);
  70. static struct attribute *cca_card_attrs[] = {
  71. &dev_attr_cca_serialnr.attr,
  72. NULL,
  73. };
  74. static const struct attribute_group cca_card_attr_grp = {
  75. .attrs = cca_card_attrs,
  76. };
  77. /*
  78. * CCA queue additional device attributes
  79. */
  80. static ssize_t cca_mkvps_show(struct device *dev,
  81. struct device_attribute *attr,
  82. char *buf)
  83. {
  84. int n = 0;
  85. struct cca_info ci;
  86. struct zcrypt_queue *zq = to_ap_queue(dev)->private;
  87. static const char * const cao_state[] = { "invalid", "valid" };
  88. static const char * const new_state[] = { "empty", "partial", "full" };
  89. memset(&ci, 0, sizeof(ci));
  90. cca_get_info(AP_QID_CARD(zq->queue->qid),
  91. AP_QID_QUEUE(zq->queue->qid),
  92. &ci, zq->online);
  93. if (ci.new_aes_mk_state >= '1' && ci.new_aes_mk_state <= '3')
  94. n = scnprintf(buf, PAGE_SIZE, "AES NEW: %s 0x%016llx\n",
  95. new_state[ci.new_aes_mk_state - '1'],
  96. ci.new_aes_mkvp);
  97. else
  98. n = scnprintf(buf, PAGE_SIZE, "AES NEW: - -\n");
  99. if (ci.cur_aes_mk_state >= '1' && ci.cur_aes_mk_state <= '2')
  100. n += scnprintf(buf + n, PAGE_SIZE - n,
  101. "AES CUR: %s 0x%016llx\n",
  102. cao_state[ci.cur_aes_mk_state - '1'],
  103. ci.cur_aes_mkvp);
  104. else
  105. n += scnprintf(buf + n, PAGE_SIZE - n, "AES CUR: - -\n");
  106. if (ci.old_aes_mk_state >= '1' && ci.old_aes_mk_state <= '2')
  107. n += scnprintf(buf + n, PAGE_SIZE - n,
  108. "AES OLD: %s 0x%016llx\n",
  109. cao_state[ci.old_aes_mk_state - '1'],
  110. ci.old_aes_mkvp);
  111. else
  112. n += scnprintf(buf + n, PAGE_SIZE - n, "AES OLD: - -\n");
  113. if (ci.new_apka_mk_state >= '1' && ci.new_apka_mk_state <= '3')
  114. n += scnprintf(buf + n, PAGE_SIZE - n,
  115. "APKA NEW: %s 0x%016llx\n",
  116. new_state[ci.new_apka_mk_state - '1'],
  117. ci.new_apka_mkvp);
  118. else
  119. n += scnprintf(buf + n, PAGE_SIZE - n, "APKA NEW: - -\n");
  120. if (ci.cur_apka_mk_state >= '1' && ci.cur_apka_mk_state <= '2')
  121. n += scnprintf(buf + n, PAGE_SIZE - n,
  122. "APKA CUR: %s 0x%016llx\n",
  123. cao_state[ci.cur_apka_mk_state - '1'],
  124. ci.cur_apka_mkvp);
  125. else
  126. n += scnprintf(buf + n, PAGE_SIZE - n, "APKA CUR: - -\n");
  127. if (ci.old_apka_mk_state >= '1' && ci.old_apka_mk_state <= '2')
  128. n += scnprintf(buf + n, PAGE_SIZE - n,
  129. "APKA OLD: %s 0x%016llx\n",
  130. cao_state[ci.old_apka_mk_state - '1'],
  131. ci.old_apka_mkvp);
  132. else
  133. n += scnprintf(buf + n, PAGE_SIZE - n, "APKA OLD: - -\n");
  134. return n;
  135. }
  136. static struct device_attribute dev_attr_cca_mkvps =
  137. __ATTR(mkvps, 0444, cca_mkvps_show, NULL);
  138. static struct attribute *cca_queue_attrs[] = {
  139. &dev_attr_cca_mkvps.attr,
  140. NULL,
  141. };
  142. static const struct attribute_group cca_queue_attr_grp = {
  143. .attrs = cca_queue_attrs,
  144. };
  145. /**
  146. * Large random number detection function. Its sends a message to a CEX2C/CEX3C
  147. * card to find out if large random numbers are supported.
  148. * @ap_dev: pointer to the AP device.
  149. *
  150. * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  151. */
  152. static int zcrypt_cex2c_rng_supported(struct ap_queue *aq)
  153. {
  154. struct ap_message ap_msg;
  155. unsigned long long psmid;
  156. unsigned int domain;
  157. struct {
  158. struct type86_hdr hdr;
  159. struct type86_fmt2_ext fmt2;
  160. struct CPRBX cprbx;
  161. } __packed *reply;
  162. struct {
  163. struct type6_hdr hdr;
  164. struct CPRBX cprbx;
  165. char function_code[2];
  166. short int rule_length;
  167. char rule[8];
  168. short int verb_length;
  169. short int key_length;
  170. } __packed *msg;
  171. int rc, i;
  172. ap_init_message(&ap_msg);
  173. ap_msg.msg = (void *) get_zeroed_page(GFP_KERNEL);
  174. if (!ap_msg.msg)
  175. return -ENOMEM;
  176. rng_type6CPRB_msgX(&ap_msg, 4, &domain);
  177. msg = ap_msg.msg;
  178. msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
  179. rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.msg, ap_msg.len);
  180. if (rc)
  181. goto out_free;
  182. /* Wait for the test message to complete. */
  183. for (i = 0; i < 2 * HZ; i++) {
  184. msleep(1000 / HZ);
  185. rc = ap_recv(aq->qid, &psmid, ap_msg.msg, 4096);
  186. if (rc == 0 && psmid == 0x0102030405060708ULL)
  187. break;
  188. }
  189. if (i >= 2 * HZ) {
  190. /* Got no answer. */
  191. rc = -ENODEV;
  192. goto out_free;
  193. }
  194. reply = ap_msg.msg;
  195. if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
  196. rc = 1;
  197. else
  198. rc = 0;
  199. out_free:
  200. free_page((unsigned long) ap_msg.msg);
  201. return rc;
  202. }
  203. /**
  204. * Probe function for CEX2C/CEX3C card devices. It always accepts the
  205. * AP device since the bus_match already checked the hardware type.
  206. * @ap_dev: pointer to the AP card device.
  207. */
  208. static int zcrypt_cex2c_card_probe(struct ap_device *ap_dev)
  209. {
  210. /*
  211. * Normalized speed ratings per crypto adapter
  212. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  213. */
  214. static const int CEX2C_SPEED_IDX[] = {
  215. 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
  216. static const int CEX3C_SPEED_IDX[] = {
  217. 500, 700, 1400, 550, 800, 1500, 80, 10};
  218. struct ap_card *ac = to_ap_card(&ap_dev->device);
  219. struct zcrypt_card *zc;
  220. int rc = 0;
  221. zc = zcrypt_card_alloc();
  222. if (!zc)
  223. return -ENOMEM;
  224. zc->card = ac;
  225. ac->private = zc;
  226. switch (ac->ap_dev.device_type) {
  227. case AP_DEVICE_TYPE_CEX2C:
  228. zc->user_space_type = ZCRYPT_CEX2C;
  229. zc->type_string = "CEX2C";
  230. zc->speed_rating = CEX2C_SPEED_IDX;
  231. zc->min_mod_size = CEX2C_MIN_MOD_SIZE;
  232. zc->max_mod_size = CEX2C_MAX_MOD_SIZE;
  233. zc->max_exp_bit_length = CEX2C_MAX_MOD_SIZE;
  234. break;
  235. case AP_DEVICE_TYPE_CEX3C:
  236. zc->user_space_type = ZCRYPT_CEX3C;
  237. zc->type_string = "CEX3C";
  238. zc->speed_rating = CEX3C_SPEED_IDX;
  239. zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
  240. zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
  241. zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
  242. break;
  243. default:
  244. zcrypt_card_free(zc);
  245. return -ENODEV;
  246. }
  247. zc->online = 1;
  248. rc = zcrypt_card_register(zc);
  249. if (rc) {
  250. ac->private = NULL;
  251. zcrypt_card_free(zc);
  252. return rc;
  253. }
  254. if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
  255. rc = sysfs_create_group(&ap_dev->device.kobj,
  256. &cca_card_attr_grp);
  257. if (rc) {
  258. zcrypt_card_unregister(zc);
  259. ac->private = NULL;
  260. zcrypt_card_free(zc);
  261. }
  262. }
  263. return rc;
  264. }
  265. /**
  266. * This is called to remove the CEX2C/CEX3C card driver information
  267. * if an AP card device is removed.
  268. */
  269. static void zcrypt_cex2c_card_remove(struct ap_device *ap_dev)
  270. {
  271. struct ap_card *ac = to_ap_card(&ap_dev->device);
  272. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  273. if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
  274. sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_grp);
  275. if (zc)
  276. zcrypt_card_unregister(zc);
  277. }
  278. static struct ap_driver zcrypt_cex2c_card_driver = {
  279. .probe = zcrypt_cex2c_card_probe,
  280. .remove = zcrypt_cex2c_card_remove,
  281. .ids = zcrypt_cex2c_card_ids,
  282. .flags = AP_DRIVER_FLAG_DEFAULT,
  283. };
  284. /**
  285. * Probe function for CEX2C/CEX3C queue devices. It always accepts the
  286. * AP device since the bus_match already checked the hardware type.
  287. * @ap_dev: pointer to the AP card device.
  288. */
  289. static int zcrypt_cex2c_queue_probe(struct ap_device *ap_dev)
  290. {
  291. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  292. struct zcrypt_queue *zq;
  293. int rc;
  294. zq = zcrypt_queue_alloc(CEX2C_MAX_XCRB_MESSAGE_SIZE);
  295. if (!zq)
  296. return -ENOMEM;
  297. zq->queue = aq;
  298. zq->online = 1;
  299. atomic_set(&zq->load, 0);
  300. ap_rapq(aq->qid);
  301. rc = zcrypt_cex2c_rng_supported(aq);
  302. if (rc < 0) {
  303. zcrypt_queue_free(zq);
  304. return rc;
  305. }
  306. if (rc)
  307. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  308. MSGTYPE06_VARIANT_DEFAULT);
  309. else
  310. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  311. MSGTYPE06_VARIANT_NORNG);
  312. ap_queue_init_state(aq);
  313. ap_queue_init_reply(aq, &zq->reply);
  314. aq->request_timeout = CEX2C_CLEANUP_TIME;
  315. aq->private = zq;
  316. rc = zcrypt_queue_register(zq);
  317. if (rc) {
  318. aq->private = NULL;
  319. zcrypt_queue_free(zq);
  320. return rc;
  321. }
  322. if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
  323. rc = sysfs_create_group(&ap_dev->device.kobj,
  324. &cca_queue_attr_grp);
  325. if (rc) {
  326. zcrypt_queue_unregister(zq);
  327. aq->private = NULL;
  328. zcrypt_queue_free(zq);
  329. }
  330. }
  331. return rc;
  332. }
  333. /**
  334. * This is called to remove the CEX2C/CEX3C queue driver information
  335. * if an AP queue device is removed.
  336. */
  337. static void zcrypt_cex2c_queue_remove(struct ap_device *ap_dev)
  338. {
  339. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  340. struct zcrypt_queue *zq = aq->private;
  341. if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
  342. sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_grp);
  343. if (zq)
  344. zcrypt_queue_unregister(zq);
  345. }
  346. static struct ap_driver zcrypt_cex2c_queue_driver = {
  347. .probe = zcrypt_cex2c_queue_probe,
  348. .remove = zcrypt_cex2c_queue_remove,
  349. .ids = zcrypt_cex2c_queue_ids,
  350. .flags = AP_DRIVER_FLAG_DEFAULT,
  351. };
  352. int __init zcrypt_cex2c_init(void)
  353. {
  354. int rc;
  355. rc = ap_driver_register(&zcrypt_cex2c_card_driver,
  356. THIS_MODULE, "cex2card");
  357. if (rc)
  358. return rc;
  359. rc = ap_driver_register(&zcrypt_cex2c_queue_driver,
  360. THIS_MODULE, "cex2cqueue");
  361. if (rc)
  362. ap_driver_unregister(&zcrypt_cex2c_card_driver);
  363. return rc;
  364. }
  365. void zcrypt_cex2c_exit(void)
  366. {
  367. ap_driver_unregister(&zcrypt_cex2c_queue_driver);
  368. ap_driver_unregister(&zcrypt_cex2c_card_driver);
  369. }
  370. module_init(zcrypt_cex2c_init);
  371. module_exit(zcrypt_cex2c_exit);