zcrypt_cex2a.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. zc->speed_rating = CEX2A_SPEED_IDX;
  83. zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  84. zc->type_string = "CEX2A";
  85. zc->user_space_type = ZCRYPT_CEX2A;
  86. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX3A) {
  87. zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
  88. zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
  89. zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  90. if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
  91. ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
  92. zc->max_mod_size = CEX3A_MAX_MOD_SIZE;
  93. zc->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
  94. }
  95. zc->speed_rating = CEX3A_SPEED_IDX;
  96. zc->type_string = "CEX3A";
  97. zc->user_space_type = ZCRYPT_CEX3A;
  98. } else {
  99. zcrypt_card_free(zc);
  100. return -ENODEV;
  101. }
  102. zc->online = 1;
  103. rc = zcrypt_card_register(zc);
  104. if (rc) {
  105. ac->private = NULL;
  106. zcrypt_card_free(zc);
  107. }
  108. return rc;
  109. }
  110. /**
  111. * This is called to remove the CEX2A card driver information
  112. * if an AP card device is removed.
  113. */
  114. static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev)
  115. {
  116. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  117. if (zc)
  118. zcrypt_card_unregister(zc);
  119. }
  120. static struct ap_driver zcrypt_cex2a_card_driver = {
  121. .probe = zcrypt_cex2a_card_probe,
  122. .remove = zcrypt_cex2a_card_remove,
  123. .ids = zcrypt_cex2a_card_ids,
  124. .flags = AP_DRIVER_FLAG_DEFAULT,
  125. };
  126. /**
  127. * Probe function for CEX2A queue devices. It always accepts the AP device
  128. * since the bus_match already checked the queue type.
  129. * @ap_dev: pointer to the AP device.
  130. */
  131. static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev)
  132. {
  133. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  134. struct zcrypt_queue *zq = NULL;
  135. int rc;
  136. switch (ap_dev->device_type) {
  137. case AP_DEVICE_TYPE_CEX2A:
  138. zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE);
  139. if (!zq)
  140. return -ENOMEM;
  141. break;
  142. case AP_DEVICE_TYPE_CEX3A:
  143. zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE);
  144. if (!zq)
  145. return -ENOMEM;
  146. break;
  147. }
  148. if (!zq)
  149. return -ENODEV;
  150. zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT);
  151. zq->queue = aq;
  152. zq->online = 1;
  153. atomic_set(&zq->load, 0);
  154. ap_queue_init_state(aq);
  155. ap_queue_init_reply(aq, &zq->reply);
  156. aq->request_timeout = CEX2A_CLEANUP_TIME,
  157. aq->private = zq;
  158. rc = zcrypt_queue_register(zq);
  159. if (rc) {
  160. aq->private = NULL;
  161. zcrypt_queue_free(zq);
  162. }
  163. return rc;
  164. }
  165. /**
  166. * This is called to remove the CEX2A queue driver information
  167. * if an AP queue device is removed.
  168. */
  169. static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev)
  170. {
  171. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  172. struct zcrypt_queue *zq = aq->private;
  173. if (zq)
  174. zcrypt_queue_unregister(zq);
  175. }
  176. static struct ap_driver zcrypt_cex2a_queue_driver = {
  177. .probe = zcrypt_cex2a_queue_probe,
  178. .remove = zcrypt_cex2a_queue_remove,
  179. .ids = zcrypt_cex2a_queue_ids,
  180. .flags = AP_DRIVER_FLAG_DEFAULT,
  181. };
  182. int __init zcrypt_cex2a_init(void)
  183. {
  184. int rc;
  185. rc = ap_driver_register(&zcrypt_cex2a_card_driver,
  186. THIS_MODULE, "cex2acard");
  187. if (rc)
  188. return rc;
  189. rc = ap_driver_register(&zcrypt_cex2a_queue_driver,
  190. THIS_MODULE, "cex2aqueue");
  191. if (rc)
  192. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  193. return rc;
  194. }
  195. void __exit zcrypt_cex2a_exit(void)
  196. {
  197. ap_driver_unregister(&zcrypt_cex2a_queue_driver);
  198. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  199. }
  200. module_init(zcrypt_cex2a_init);
  201. module_exit(zcrypt_cex2a_exit);