zcrypt_pcixcc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * zcrypt 2.1.0
  4. *
  5. * Copyright IBM Corp. 2001, 2012
  6. * Author(s): Robert Burroughs
  7. * Eric Rossman (edrossma@us.ibm.com)
  8. *
  9. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  10. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  11. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  12. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <linux/atomic.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/mod_devicetable.h>
  22. #include "ap_bus.h"
  23. #include "zcrypt_api.h"
  24. #include "zcrypt_error.h"
  25. #include "zcrypt_msgtype6.h"
  26. #include "zcrypt_pcixcc.h"
  27. #include "zcrypt_cca_key.h"
  28. #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
  29. #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
  30. #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
  31. #define CEX3C_MIN_MOD_SIZE PCIXCC_MIN_MOD_SIZE
  32. #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
  33. #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
  34. #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
  35. #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
  36. #define PCIXCC_CLEANUP_TIME (15*HZ)
  37. #define CEIL4(x) ((((x)+3)/4)*4)
  38. struct response_type {
  39. struct completion work;
  40. int type;
  41. };
  42. #define PCIXCC_RESPONSE_TYPE_ICA 0
  43. #define PCIXCC_RESPONSE_TYPE_XCRB 1
  44. MODULE_AUTHOR("IBM Corporation");
  45. MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
  46. "Copyright IBM Corp. 2001, 2012");
  47. MODULE_LICENSE("GPL");
  48. static struct ap_device_id zcrypt_pcixcc_card_ids[] = {
  49. { .dev_type = AP_DEVICE_TYPE_PCIXCC,
  50. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  51. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  52. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  53. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  54. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  55. { /* end of list */ },
  56. };
  57. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_card_ids);
  58. static struct ap_device_id zcrypt_pcixcc_queue_ids[] = {
  59. { .dev_type = AP_DEVICE_TYPE_PCIXCC,
  60. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  61. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  62. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  63. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  64. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  65. { /* end of list */ },
  66. };
  67. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_queue_ids);
  68. /**
  69. * Large random number detection function. Its sends a message to a pcixcc
  70. * card to find out if large random numbers are supported.
  71. * @ap_dev: pointer to the AP device.
  72. *
  73. * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  74. */
  75. static int zcrypt_pcixcc_rng_supported(struct ap_queue *aq)
  76. {
  77. struct ap_message ap_msg;
  78. unsigned long long psmid;
  79. unsigned int domain;
  80. struct {
  81. struct type86_hdr hdr;
  82. struct type86_fmt2_ext fmt2;
  83. struct CPRBX cprbx;
  84. } __packed *reply;
  85. struct {
  86. struct type6_hdr hdr;
  87. struct CPRBX cprbx;
  88. char function_code[2];
  89. short int rule_length;
  90. char rule[8];
  91. short int verb_length;
  92. short int key_length;
  93. } __packed *msg;
  94. int rc, i;
  95. ap_init_message(&ap_msg);
  96. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  97. if (!ap_msg.message)
  98. return -ENOMEM;
  99. rng_type6CPRB_msgX(&ap_msg, 4, &domain);
  100. msg = ap_msg.message;
  101. msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
  102. rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.message,
  103. ap_msg.length);
  104. if (rc)
  105. goto out_free;
  106. /* Wait for the test message to complete. */
  107. for (i = 0; i < 2 * HZ; i++) {
  108. msleep(1000 / HZ);
  109. rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
  110. if (rc == 0 && psmid == 0x0102030405060708ULL)
  111. break;
  112. }
  113. if (i >= 2 * HZ) {
  114. /* Got no answer. */
  115. rc = -ENODEV;
  116. goto out_free;
  117. }
  118. reply = ap_msg.message;
  119. if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
  120. rc = 1;
  121. else
  122. rc = 0;
  123. out_free:
  124. free_page((unsigned long) ap_msg.message);
  125. return rc;
  126. }
  127. /**
  128. * Probe function for PCIXCC/CEX2C card devices. It always accepts the
  129. * AP device since the bus_match already checked the hardware type. The
  130. * PCIXCC cards come in two flavours: micro code level 2 and micro code
  131. * level 3. This is checked by sending a test message to the device.
  132. * @ap_dev: pointer to the AP card device.
  133. */
  134. static int zcrypt_pcixcc_card_probe(struct ap_device *ap_dev)
  135. {
  136. /*
  137. * Normalized speed ratings per crypto adapter
  138. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  139. */
  140. static const int CEX2C_SPEED_IDX[] = {
  141. 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
  142. static const int CEX3C_SPEED_IDX[] = {
  143. 500, 700, 1400, 550, 800, 1500, 80, 10};
  144. struct ap_card *ac = to_ap_card(&ap_dev->device);
  145. struct zcrypt_card *zc;
  146. int rc = 0;
  147. zc = zcrypt_card_alloc();
  148. if (!zc)
  149. return -ENOMEM;
  150. zc->card = ac;
  151. ac->private = zc;
  152. switch (ac->ap_dev.device_type) {
  153. case AP_DEVICE_TYPE_CEX2C:
  154. zc->user_space_type = ZCRYPT_CEX2C;
  155. zc->type_string = "CEX2C";
  156. memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
  157. sizeof(CEX2C_SPEED_IDX));
  158. zc->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  159. zc->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  160. zc->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  161. break;
  162. case AP_DEVICE_TYPE_CEX3C:
  163. zc->user_space_type = ZCRYPT_CEX3C;
  164. zc->type_string = "CEX3C";
  165. memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
  166. sizeof(CEX3C_SPEED_IDX));
  167. zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
  168. zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
  169. zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
  170. break;
  171. default:
  172. zcrypt_card_free(zc);
  173. return -ENODEV;
  174. }
  175. zc->online = 1;
  176. rc = zcrypt_card_register(zc);
  177. if (rc) {
  178. ac->private = NULL;
  179. zcrypt_card_free(zc);
  180. }
  181. return rc;
  182. }
  183. /**
  184. * This is called to remove the PCIXCC/CEX2C card driver information
  185. * if an AP card device is removed.
  186. */
  187. static void zcrypt_pcixcc_card_remove(struct ap_device *ap_dev)
  188. {
  189. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  190. if (zc)
  191. zcrypt_card_unregister(zc);
  192. }
  193. static struct ap_driver zcrypt_pcixcc_card_driver = {
  194. .probe = zcrypt_pcixcc_card_probe,
  195. .remove = zcrypt_pcixcc_card_remove,
  196. .ids = zcrypt_pcixcc_card_ids,
  197. .flags = AP_DRIVER_FLAG_DEFAULT,
  198. };
  199. /**
  200. * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
  201. * AP device since the bus_match already checked the hardware type. The
  202. * PCIXCC cards come in two flavours: micro code level 2 and micro code
  203. * level 3. This is checked by sending a test message to the device.
  204. * @ap_dev: pointer to the AP card device.
  205. */
  206. static int zcrypt_pcixcc_queue_probe(struct ap_device *ap_dev)
  207. {
  208. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  209. struct zcrypt_queue *zq;
  210. int rc;
  211. zq = zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE);
  212. if (!zq)
  213. return -ENOMEM;
  214. zq->queue = aq;
  215. zq->online = 1;
  216. atomic_set(&zq->load, 0);
  217. rc = zcrypt_pcixcc_rng_supported(aq);
  218. if (rc < 0) {
  219. zcrypt_queue_free(zq);
  220. return rc;
  221. }
  222. if (rc)
  223. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  224. MSGTYPE06_VARIANT_DEFAULT);
  225. else
  226. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  227. MSGTYPE06_VARIANT_NORNG);
  228. ap_queue_init_reply(aq, &zq->reply);
  229. aq->request_timeout = PCIXCC_CLEANUP_TIME,
  230. aq->private = zq;
  231. rc = zcrypt_queue_register(zq);
  232. if (rc) {
  233. aq->private = NULL;
  234. zcrypt_queue_free(zq);
  235. }
  236. return rc;
  237. }
  238. /**
  239. * This is called to remove the PCIXCC/CEX2C queue driver information
  240. * if an AP queue device is removed.
  241. */
  242. static void zcrypt_pcixcc_queue_remove(struct ap_device *ap_dev)
  243. {
  244. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  245. struct zcrypt_queue *zq = aq->private;
  246. if (zq)
  247. zcrypt_queue_unregister(zq);
  248. }
  249. static struct ap_driver zcrypt_pcixcc_queue_driver = {
  250. .probe = zcrypt_pcixcc_queue_probe,
  251. .remove = zcrypt_pcixcc_queue_remove,
  252. .suspend = ap_queue_suspend,
  253. .resume = ap_queue_resume,
  254. .ids = zcrypt_pcixcc_queue_ids,
  255. .flags = AP_DRIVER_FLAG_DEFAULT,
  256. };
  257. int __init zcrypt_pcixcc_init(void)
  258. {
  259. int rc;
  260. rc = ap_driver_register(&zcrypt_pcixcc_card_driver,
  261. THIS_MODULE, "pcixcccard");
  262. if (rc)
  263. return rc;
  264. rc = ap_driver_register(&zcrypt_pcixcc_queue_driver,
  265. THIS_MODULE, "pcixccqueue");
  266. if (rc)
  267. ap_driver_unregister(&zcrypt_pcixcc_card_driver);
  268. return rc;
  269. }
  270. void zcrypt_pcixcc_exit(void)
  271. {
  272. ap_driver_unregister(&zcrypt_pcixcc_queue_driver);
  273. ap_driver_unregister(&zcrypt_pcixcc_card_driver);
  274. }
  275. module_init(zcrypt_pcixcc_init);
  276. module_exit(zcrypt_pcixcc_exit);