zcrypt_cex2a.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2001, 2012
  4. * Author(s): Robert Burroughs
  5. * Eric Rossman ([email protected])
  6. *
  7. * Hotplug & misc device support: Jochen Roehrig ([email protected])
  8. * Major cleanup & driver split: Martin Schwidefsky <[email protected]>
  9. * Ralph Wuerthner <[email protected]>
  10. * MSGTYPE restruct: Holger Dengler <[email protected]>
  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. */
  34. #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
  35. #define CEX2A_CLEANUP_TIME (15 * HZ)
  36. #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
  37. MODULE_AUTHOR("IBM Corporation");
  38. MODULE_DESCRIPTION("CEX2A/CEX3A Cryptographic Coprocessor device driver, " \
  39. "Copyright IBM Corp. 2001, 2018");
  40. MODULE_LICENSE("GPL");
  41. static struct ap_device_id zcrypt_cex2a_card_ids[] = {
  42. { .dev_type = AP_DEVICE_TYPE_CEX2A,
  43. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  44. { .dev_type = AP_DEVICE_TYPE_CEX3A,
  45. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  46. { /* end of list */ },
  47. };
  48. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids);
  49. static struct ap_device_id zcrypt_cex2a_queue_ids[] = {
  50. { .dev_type = AP_DEVICE_TYPE_CEX2A,
  51. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  52. { .dev_type = AP_DEVICE_TYPE_CEX3A,
  53. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  54. { /* end of list */ },
  55. };
  56. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids);
  57. /*
  58. * Probe function for CEX2A card devices. It always accepts the AP device
  59. * since the bus_match already checked the card type.
  60. * @ap_dev: pointer to the AP device.
  61. */
  62. static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev)
  63. {
  64. /*
  65. * Normalized speed ratings per crypto adapter
  66. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  67. */
  68. static const int CEX2A_SPEED_IDX[] = {
  69. 800, 1000, 2000, 900, 1200, 2400, 0, 0};
  70. static const int CEX3A_SPEED_IDX[] = {
  71. 400, 500, 1000, 450, 550, 1200, 0, 0};
  72. struct ap_card *ac = to_ap_card(&ap_dev->device);
  73. struct zcrypt_card *zc;
  74. int rc = 0;
  75. zc = zcrypt_card_alloc();
  76. if (!zc)
  77. return -ENOMEM;
  78. zc->card = ac;
  79. dev_set_drvdata(&ap_dev->device, zc);
  80. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A) {
  81. zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
  82. zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
  83. zc->speed_rating = 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. zc->speed_rating = CEX3A_SPEED_IDX;
  97. zc->type_string = "CEX3A";
  98. zc->user_space_type = ZCRYPT_CEX3A;
  99. } else {
  100. zcrypt_card_free(zc);
  101. return -ENODEV;
  102. }
  103. zc->online = 1;
  104. rc = zcrypt_card_register(zc);
  105. if (rc)
  106. zcrypt_card_free(zc);
  107. return rc;
  108. }
  109. /*
  110. * This is called to remove the CEX2A card driver information
  111. * if an AP card device is removed.
  112. */
  113. static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev)
  114. {
  115. struct zcrypt_card *zc = dev_get_drvdata(&ap_dev->device);
  116. zcrypt_card_unregister(zc);
  117. }
  118. static struct ap_driver zcrypt_cex2a_card_driver = {
  119. .probe = zcrypt_cex2a_card_probe,
  120. .remove = zcrypt_cex2a_card_remove,
  121. .ids = zcrypt_cex2a_card_ids,
  122. .flags = AP_DRIVER_FLAG_DEFAULT,
  123. };
  124. /*
  125. * Probe function for CEX2A queue devices. It always accepts the AP device
  126. * since the bus_match already checked the queue type.
  127. * @ap_dev: pointer to the AP device.
  128. */
  129. static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev)
  130. {
  131. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  132. struct zcrypt_queue *zq = NULL;
  133. int rc;
  134. switch (ap_dev->device_type) {
  135. case AP_DEVICE_TYPE_CEX2A:
  136. zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE);
  137. if (!zq)
  138. return -ENOMEM;
  139. break;
  140. case AP_DEVICE_TYPE_CEX3A:
  141. zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE);
  142. if (!zq)
  143. return -ENOMEM;
  144. break;
  145. }
  146. if (!zq)
  147. return -ENODEV;
  148. zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT);
  149. zq->queue = aq;
  150. zq->online = 1;
  151. atomic_set(&zq->load, 0);
  152. ap_queue_init_state(aq);
  153. ap_queue_init_reply(aq, &zq->reply);
  154. aq->request_timeout = CEX2A_CLEANUP_TIME;
  155. dev_set_drvdata(&ap_dev->device, zq);
  156. rc = zcrypt_queue_register(zq);
  157. if (rc)
  158. zcrypt_queue_free(zq);
  159. return rc;
  160. }
  161. /*
  162. * This is called to remove the CEX2A queue driver information
  163. * if an AP queue device is removed.
  164. */
  165. static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev)
  166. {
  167. struct zcrypt_queue *zq = dev_get_drvdata(&ap_dev->device);
  168. zcrypt_queue_unregister(zq);
  169. }
  170. static struct ap_driver zcrypt_cex2a_queue_driver = {
  171. .probe = zcrypt_cex2a_queue_probe,
  172. .remove = zcrypt_cex2a_queue_remove,
  173. .ids = zcrypt_cex2a_queue_ids,
  174. .flags = AP_DRIVER_FLAG_DEFAULT,
  175. };
  176. int __init zcrypt_cex2a_init(void)
  177. {
  178. int rc;
  179. rc = ap_driver_register(&zcrypt_cex2a_card_driver,
  180. THIS_MODULE, "cex2acard");
  181. if (rc)
  182. return rc;
  183. rc = ap_driver_register(&zcrypt_cex2a_queue_driver,
  184. THIS_MODULE, "cex2aqueue");
  185. if (rc)
  186. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  187. return rc;
  188. }
  189. void __exit zcrypt_cex2a_exit(void)
  190. {
  191. ap_driver_unregister(&zcrypt_cex2a_queue_driver);
  192. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  193. }
  194. module_init(zcrypt_cex2a_init);
  195. module_exit(zcrypt_cex2a_exit);