zcrypt_cex2a.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2001, 2012
  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/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/atomic.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/mod_devicetable.h>
  19. #include "ap_bus.h"
  20. #include "zcrypt_api.h"
  21. #include "zcrypt_error.h"
  22. #include "zcrypt_cex2a.h"
  23. #include "zcrypt_msgtype50.h"
  24. #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
  25. #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
  26. #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
  27. #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
  28. #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
  29. #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
  30. #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
  31. * (max outputdatalength) +
  32. * type80_hdr*/
  33. #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
  34. #define CEX2A_CLEANUP_TIME (15*HZ)
  35. #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
  36. MODULE_AUTHOR("IBM Corporation");
  37. MODULE_DESCRIPTION("CEX2A/CEX3A Cryptographic Coprocessor device driver, " \
  38. "Copyright IBM Corp. 2001, 2018");
  39. MODULE_LICENSE("GPL");
  40. static struct ap_device_id zcrypt_cex2a_card_ids[] = {
  41. { .dev_type = AP_DEVICE_TYPE_CEX2A,
  42. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  43. { .dev_type = AP_DEVICE_TYPE_CEX3A,
  44. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  45. { /* end of list */ },
  46. };
  47. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids);
  48. static struct ap_device_id zcrypt_cex2a_queue_ids[] = {
  49. { .dev_type = AP_DEVICE_TYPE_CEX2A,
  50. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  51. { .dev_type = AP_DEVICE_TYPE_CEX3A,
  52. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  53. { /* end of list */ },
  54. };
  55. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids);
  56. /**
  57. * Probe function for CEX2A card devices. It always accepts the AP device
  58. * since the bus_match already checked the card type.
  59. * @ap_dev: pointer to the AP device.
  60. */
  61. static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev)
  62. {
  63. /*
  64. * Normalized speed ratings per crypto adapter
  65. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  66. */
  67. static const int CEX2A_SPEED_IDX[] = {
  68. 800, 1000, 2000, 900, 1200, 2400, 0, 0};
  69. static const int CEX3A_SPEED_IDX[] = {
  70. 400, 500, 1000, 450, 550, 1200, 0, 0};
  71. struct ap_card *ac = to_ap_card(&ap_dev->device);
  72. struct zcrypt_card *zc;
  73. int rc = 0;
  74. zc = zcrypt_card_alloc();
  75. if (!zc)
  76. return -ENOMEM;
  77. zc->card = ac;
  78. ac->private = zc;
  79. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A) {
  80. zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
  81. zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
  82. memcpy(zc->speed_rating, CEX2A_SPEED_IDX,
  83. sizeof(CEX2A_SPEED_IDX));
  84. zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  85. zc->type_string = "CEX2A";
  86. zc->user_space_type = ZCRYPT_CEX2A;
  87. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX3A) {
  88. zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
  89. zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
  90. zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  91. if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
  92. ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
  93. zc->max_mod_size = CEX3A_MAX_MOD_SIZE;
  94. zc->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
  95. }
  96. memcpy(zc->speed_rating, CEX3A_SPEED_IDX,
  97. sizeof(CEX3A_SPEED_IDX));
  98. zc->type_string = "CEX3A";
  99. zc->user_space_type = ZCRYPT_CEX3A;
  100. } else {
  101. zcrypt_card_free(zc);
  102. return -ENODEV;
  103. }
  104. zc->online = 1;
  105. rc = zcrypt_card_register(zc);
  106. if (rc) {
  107. ac->private = NULL;
  108. zcrypt_card_free(zc);
  109. }
  110. return rc;
  111. }
  112. /**
  113. * This is called to remove the CEX2A card driver information
  114. * if an AP card device is removed.
  115. */
  116. static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev)
  117. {
  118. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  119. if (zc)
  120. zcrypt_card_unregister(zc);
  121. }
  122. static struct ap_driver zcrypt_cex2a_card_driver = {
  123. .probe = zcrypt_cex2a_card_probe,
  124. .remove = zcrypt_cex2a_card_remove,
  125. .ids = zcrypt_cex2a_card_ids,
  126. .flags = AP_DRIVER_FLAG_DEFAULT,
  127. };
  128. /**
  129. * Probe function for CEX2A queue devices. It always accepts the AP device
  130. * since the bus_match already checked the queue type.
  131. * @ap_dev: pointer to the AP device.
  132. */
  133. static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev)
  134. {
  135. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  136. struct zcrypt_queue *zq = NULL;
  137. int rc;
  138. switch (ap_dev->device_type) {
  139. case AP_DEVICE_TYPE_CEX2A:
  140. zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE);
  141. if (!zq)
  142. return -ENOMEM;
  143. break;
  144. case AP_DEVICE_TYPE_CEX3A:
  145. zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE);
  146. if (!zq)
  147. return -ENOMEM;
  148. break;
  149. }
  150. if (!zq)
  151. return -ENODEV;
  152. zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT);
  153. zq->queue = aq;
  154. zq->online = 1;
  155. atomic_set(&zq->load, 0);
  156. ap_queue_init_reply(aq, &zq->reply);
  157. aq->request_timeout = CEX2A_CLEANUP_TIME,
  158. aq->private = zq;
  159. rc = zcrypt_queue_register(zq);
  160. if (rc) {
  161. aq->private = NULL;
  162. zcrypt_queue_free(zq);
  163. }
  164. return rc;
  165. }
  166. /**
  167. * This is called to remove the CEX2A queue driver information
  168. * if an AP queue device is removed.
  169. */
  170. static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev)
  171. {
  172. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  173. struct zcrypt_queue *zq = aq->private;
  174. if (zq)
  175. zcrypt_queue_unregister(zq);
  176. }
  177. static struct ap_driver zcrypt_cex2a_queue_driver = {
  178. .probe = zcrypt_cex2a_queue_probe,
  179. .remove = zcrypt_cex2a_queue_remove,
  180. .suspend = ap_queue_suspend,
  181. .resume = ap_queue_resume,
  182. .ids = zcrypt_cex2a_queue_ids,
  183. .flags = AP_DRIVER_FLAG_DEFAULT,
  184. };
  185. int __init zcrypt_cex2a_init(void)
  186. {
  187. int rc;
  188. rc = ap_driver_register(&zcrypt_cex2a_card_driver,
  189. THIS_MODULE, "cex2acard");
  190. if (rc)
  191. return rc;
  192. rc = ap_driver_register(&zcrypt_cex2a_queue_driver,
  193. THIS_MODULE, "cex2aqueue");
  194. if (rc)
  195. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  196. return rc;
  197. }
  198. void __exit zcrypt_cex2a_exit(void)
  199. {
  200. ap_driver_unregister(&zcrypt_cex2a_queue_driver);
  201. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  202. }
  203. module_init(zcrypt_cex2a_init);
  204. module_exit(zcrypt_cex2a_exit);