zcrypt_cex2c.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #define CEX2C_MIN_MOD_SIZE 16 /* 128 bits */
  27. #define CEX2C_MAX_MOD_SIZE 256 /* 2048 bits */
  28. #define CEX3C_MIN_MOD_SIZE 16 /* 128 bits */
  29. #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
  30. #define CEX2C_MAX_XCRB_MESSAGE_SIZE (12*1024)
  31. #define CEX2C_CLEANUP_TIME (15*HZ)
  32. MODULE_AUTHOR("IBM Corporation");
  33. MODULE_DESCRIPTION("CEX2C/CEX3C Cryptographic Coprocessor device driver, " \
  34. "Copyright IBM Corp. 2001, 2018");
  35. MODULE_LICENSE("GPL");
  36. static struct ap_device_id zcrypt_cex2c_card_ids[] = {
  37. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  38. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  39. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  40. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  41. { /* end of list */ },
  42. };
  43. MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_card_ids);
  44. static struct ap_device_id zcrypt_cex2c_queue_ids[] = {
  45. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  46. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  47. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  48. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  49. { /* end of list */ },
  50. };
  51. MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_queue_ids);
  52. /**
  53. * Large random number detection function. Its sends a message to a CEX2C/CEX3C
  54. * card to find out if large random numbers are supported.
  55. * @ap_dev: pointer to the AP device.
  56. *
  57. * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  58. */
  59. static int zcrypt_cex2c_rng_supported(struct ap_queue *aq)
  60. {
  61. struct ap_message ap_msg;
  62. unsigned long long psmid;
  63. unsigned int domain;
  64. struct {
  65. struct type86_hdr hdr;
  66. struct type86_fmt2_ext fmt2;
  67. struct CPRBX cprbx;
  68. } __packed *reply;
  69. struct {
  70. struct type6_hdr hdr;
  71. struct CPRBX cprbx;
  72. char function_code[2];
  73. short int rule_length;
  74. char rule[8];
  75. short int verb_length;
  76. short int key_length;
  77. } __packed *msg;
  78. int rc, i;
  79. ap_init_message(&ap_msg);
  80. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  81. if (!ap_msg.message)
  82. return -ENOMEM;
  83. rng_type6CPRB_msgX(&ap_msg, 4, &domain);
  84. msg = ap_msg.message;
  85. msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
  86. rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.message,
  87. ap_msg.length);
  88. if (rc)
  89. goto out_free;
  90. /* Wait for the test message to complete. */
  91. for (i = 0; i < 2 * HZ; i++) {
  92. msleep(1000 / HZ);
  93. rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
  94. if (rc == 0 && psmid == 0x0102030405060708ULL)
  95. break;
  96. }
  97. if (i >= 2 * HZ) {
  98. /* Got no answer. */
  99. rc = -ENODEV;
  100. goto out_free;
  101. }
  102. reply = ap_msg.message;
  103. if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
  104. rc = 1;
  105. else
  106. rc = 0;
  107. out_free:
  108. free_page((unsigned long) ap_msg.message);
  109. return rc;
  110. }
  111. /**
  112. * Probe function for CEX2C/CEX3C card devices. It always accepts the
  113. * AP device since the bus_match already checked the hardware type.
  114. * @ap_dev: pointer to the AP card device.
  115. */
  116. static int zcrypt_cex2c_card_probe(struct ap_device *ap_dev)
  117. {
  118. /*
  119. * Normalized speed ratings per crypto adapter
  120. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  121. */
  122. static const int CEX2C_SPEED_IDX[] = {
  123. 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
  124. static const int CEX3C_SPEED_IDX[] = {
  125. 500, 700, 1400, 550, 800, 1500, 80, 10};
  126. struct ap_card *ac = to_ap_card(&ap_dev->device);
  127. struct zcrypt_card *zc;
  128. int rc = 0;
  129. zc = zcrypt_card_alloc();
  130. if (!zc)
  131. return -ENOMEM;
  132. zc->card = ac;
  133. ac->private = zc;
  134. switch (ac->ap_dev.device_type) {
  135. case AP_DEVICE_TYPE_CEX2C:
  136. zc->user_space_type = ZCRYPT_CEX2C;
  137. zc->type_string = "CEX2C";
  138. memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
  139. sizeof(CEX2C_SPEED_IDX));
  140. zc->min_mod_size = CEX2C_MIN_MOD_SIZE;
  141. zc->max_mod_size = CEX2C_MAX_MOD_SIZE;
  142. zc->max_exp_bit_length = CEX2C_MAX_MOD_SIZE;
  143. break;
  144. case AP_DEVICE_TYPE_CEX3C:
  145. zc->user_space_type = ZCRYPT_CEX3C;
  146. zc->type_string = "CEX3C";
  147. memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
  148. sizeof(CEX3C_SPEED_IDX));
  149. zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
  150. zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
  151. zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
  152. break;
  153. default:
  154. zcrypt_card_free(zc);
  155. return -ENODEV;
  156. }
  157. zc->online = 1;
  158. rc = zcrypt_card_register(zc);
  159. if (rc) {
  160. ac->private = NULL;
  161. zcrypt_card_free(zc);
  162. }
  163. return rc;
  164. }
  165. /**
  166. * This is called to remove the CEX2C/CEX3C card driver information
  167. * if an AP card device is removed.
  168. */
  169. static void zcrypt_cex2c_card_remove(struct ap_device *ap_dev)
  170. {
  171. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  172. if (zc)
  173. zcrypt_card_unregister(zc);
  174. }
  175. static struct ap_driver zcrypt_cex2c_card_driver = {
  176. .probe = zcrypt_cex2c_card_probe,
  177. .remove = zcrypt_cex2c_card_remove,
  178. .ids = zcrypt_cex2c_card_ids,
  179. .flags = AP_DRIVER_FLAG_DEFAULT,
  180. };
  181. /**
  182. * Probe function for CEX2C/CEX3C queue devices. It always accepts the
  183. * AP device since the bus_match already checked the hardware type.
  184. * @ap_dev: pointer to the AP card device.
  185. */
  186. static int zcrypt_cex2c_queue_probe(struct ap_device *ap_dev)
  187. {
  188. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  189. struct zcrypt_queue *zq;
  190. int rc;
  191. zq = zcrypt_queue_alloc(CEX2C_MAX_XCRB_MESSAGE_SIZE);
  192. if (!zq)
  193. return -ENOMEM;
  194. zq->queue = aq;
  195. zq->online = 1;
  196. atomic_set(&zq->load, 0);
  197. rc = zcrypt_cex2c_rng_supported(aq);
  198. if (rc < 0) {
  199. zcrypt_queue_free(zq);
  200. return rc;
  201. }
  202. if (rc)
  203. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  204. MSGTYPE06_VARIANT_DEFAULT);
  205. else
  206. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  207. MSGTYPE06_VARIANT_NORNG);
  208. ap_queue_init_reply(aq, &zq->reply);
  209. aq->request_timeout = CEX2C_CLEANUP_TIME;
  210. aq->private = zq;
  211. rc = zcrypt_queue_register(zq);
  212. if (rc) {
  213. aq->private = NULL;
  214. zcrypt_queue_free(zq);
  215. }
  216. return rc;
  217. }
  218. /**
  219. * This is called to remove the CEX2C/CEX3C queue driver information
  220. * if an AP queue device is removed.
  221. */
  222. static void zcrypt_cex2c_queue_remove(struct ap_device *ap_dev)
  223. {
  224. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  225. struct zcrypt_queue *zq = aq->private;
  226. if (zq)
  227. zcrypt_queue_unregister(zq);
  228. }
  229. static struct ap_driver zcrypt_cex2c_queue_driver = {
  230. .probe = zcrypt_cex2c_queue_probe,
  231. .remove = zcrypt_cex2c_queue_remove,
  232. .suspend = ap_queue_suspend,
  233. .resume = ap_queue_resume,
  234. .ids = zcrypt_cex2c_queue_ids,
  235. .flags = AP_DRIVER_FLAG_DEFAULT,
  236. };
  237. int __init zcrypt_cex2c_init(void)
  238. {
  239. int rc;
  240. rc = ap_driver_register(&zcrypt_cex2c_card_driver,
  241. THIS_MODULE, "cex2card");
  242. if (rc)
  243. return rc;
  244. rc = ap_driver_register(&zcrypt_cex2c_queue_driver,
  245. THIS_MODULE, "cex2cqueue");
  246. if (rc)
  247. ap_driver_unregister(&zcrypt_cex2c_card_driver);
  248. return rc;
  249. }
  250. void zcrypt_cex2c_exit(void)
  251. {
  252. ap_driver_unregister(&zcrypt_cex2c_queue_driver);
  253. ap_driver_unregister(&zcrypt_cex2c_card_driver);
  254. }
  255. module_init(zcrypt_cex2c_init);
  256. module_exit(zcrypt_cex2c_exit);