zcrypt_cex2a.c 6.3 KB

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