zcrypt_cex4.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright IBM Corp. 2012
  3. * Author(s): Holger Dengler <hd@linux.vnet.ibm.com>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/init.h>
  8. #include <linux/err.h>
  9. #include <linux/atomic.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/mod_devicetable.h>
  12. #include "ap_bus.h"
  13. #include "zcrypt_api.h"
  14. #include "zcrypt_msgtype6.h"
  15. #include "zcrypt_msgtype50.h"
  16. #include "zcrypt_error.h"
  17. #include "zcrypt_cex4.h"
  18. #define CEX4A_MIN_MOD_SIZE 1 /* 8 bits */
  19. #define CEX4A_MAX_MOD_SIZE_2K 256 /* 2048 bits */
  20. #define CEX4A_MAX_MOD_SIZE_4K 512 /* 4096 bits */
  21. #define CEX4C_MIN_MOD_SIZE 16 /* 256 bits */
  22. #define CEX4C_MAX_MOD_SIZE 512 /* 4096 bits */
  23. #define CEX4A_MAX_MESSAGE_SIZE MSGTYPE50_CRB3_MAX_MSG_SIZE
  24. #define CEX4C_MAX_MESSAGE_SIZE MSGTYPE06_MAX_MSG_SIZE
  25. /* Waiting time for requests to be processed.
  26. * Currently there are some types of request which are not deterministic.
  27. * But the maximum time limit managed by the stomper code is set to 60sec.
  28. * Hence we have to wait at least that time period.
  29. */
  30. #define CEX4_CLEANUP_TIME (900*HZ)
  31. MODULE_AUTHOR("IBM Corporation");
  32. MODULE_DESCRIPTION("CEX4 Cryptographic Card device driver, " \
  33. "Copyright IBM Corp. 2012");
  34. MODULE_LICENSE("GPL");
  35. static struct ap_device_id zcrypt_cex4_card_ids[] = {
  36. { .dev_type = AP_DEVICE_TYPE_CEX4,
  37. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  38. { .dev_type = AP_DEVICE_TYPE_CEX5,
  39. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  40. { /* end of list */ },
  41. };
  42. MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids);
  43. static struct ap_device_id zcrypt_cex4_queue_ids[] = {
  44. { .dev_type = AP_DEVICE_TYPE_CEX4,
  45. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  46. { .dev_type = AP_DEVICE_TYPE_CEX5,
  47. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  48. { /* end of list */ },
  49. };
  50. MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids);
  51. /**
  52. * Probe function for CEX4 card device. It always accepts the AP device
  53. * since the bus_match already checked the hardware type.
  54. * @ap_dev: pointer to the AP device.
  55. */
  56. static int zcrypt_cex4_card_probe(struct ap_device *ap_dev)
  57. {
  58. /*
  59. * Normalized speed ratings per crypto adapter
  60. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  61. */
  62. static const int CEX4A_SPEED_IDX[] = {
  63. 5, 6, 59, 20, 115, 581, 0, 0};
  64. static const int CEX5A_SPEED_IDX[] = {
  65. 3, 3, 6, 8, 32, 218, 0, 0};
  66. static const int CEX4C_SPEED_IDX[] = {
  67. 24, 25, 82, 41, 138, 1111, 79, 8};
  68. static const int CEX5C_SPEED_IDX[] = {
  69. 10, 14, 23, 17, 45, 242, 63, 4};
  70. static const int CEX4P_SPEED_IDX[] = {
  71. 142, 198, 1852, 203, 331, 1563, 0, 8};
  72. static const int CEX5P_SPEED_IDX[] = {
  73. 49, 67, 131, 52, 85, 287, 0, 4};
  74. struct ap_card *ac = to_ap_card(&ap_dev->device);
  75. struct zcrypt_card *zc;
  76. int rc = 0;
  77. zc = zcrypt_card_alloc();
  78. if (!zc)
  79. return -ENOMEM;
  80. zc->card = ac;
  81. ac->private = zc;
  82. if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL)) {
  83. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  84. zc->type_string = "CEX4A";
  85. zc->user_space_type = ZCRYPT_CEX4;
  86. memcpy(zc->speed_rating, CEX4A_SPEED_IDX,
  87. sizeof(CEX4A_SPEED_IDX));
  88. } else {
  89. zc->type_string = "CEX5A";
  90. zc->user_space_type = ZCRYPT_CEX5;
  91. memcpy(zc->speed_rating, CEX5A_SPEED_IDX,
  92. sizeof(CEX5A_SPEED_IDX));
  93. }
  94. zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
  95. if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
  96. ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
  97. zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
  98. zc->max_exp_bit_length =
  99. CEX4A_MAX_MOD_SIZE_4K;
  100. } else {
  101. zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
  102. zc->max_exp_bit_length =
  103. CEX4A_MAX_MOD_SIZE_2K;
  104. }
  105. } else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
  106. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  107. zc->type_string = "CEX4C";
  108. /* wrong user space type, must be CEX4
  109. * just keep it for cca compatibility
  110. */
  111. zc->user_space_type = ZCRYPT_CEX3C;
  112. memcpy(zc->speed_rating, CEX4C_SPEED_IDX,
  113. sizeof(CEX4C_SPEED_IDX));
  114. } else {
  115. zc->type_string = "CEX5C";
  116. /* wrong user space type, must be CEX5
  117. * just keep it for cca compatibility
  118. */
  119. zc->user_space_type = ZCRYPT_CEX3C;
  120. memcpy(zc->speed_rating, CEX5C_SPEED_IDX,
  121. sizeof(CEX5C_SPEED_IDX));
  122. }
  123. zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
  124. zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
  125. zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  126. } else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
  127. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  128. zc->type_string = "CEX4P";
  129. zc->user_space_type = ZCRYPT_CEX4;
  130. memcpy(zc->speed_rating, CEX4P_SPEED_IDX,
  131. sizeof(CEX4P_SPEED_IDX));
  132. } else {
  133. zc->type_string = "CEX5P";
  134. zc->user_space_type = ZCRYPT_CEX5;
  135. memcpy(zc->speed_rating, CEX5P_SPEED_IDX,
  136. sizeof(CEX5P_SPEED_IDX));
  137. }
  138. zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
  139. zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
  140. zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  141. } else {
  142. zcrypt_card_free(zc);
  143. return -ENODEV;
  144. }
  145. zc->online = 1;
  146. rc = zcrypt_card_register(zc);
  147. if (rc) {
  148. ac->private = NULL;
  149. zcrypt_card_free(zc);
  150. }
  151. return rc;
  152. }
  153. /**
  154. * This is called to remove the CEX4 card driver information
  155. * if an AP card device is removed.
  156. */
  157. static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
  158. {
  159. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  160. if (zc)
  161. zcrypt_card_unregister(zc);
  162. }
  163. static struct ap_driver zcrypt_cex4_card_driver = {
  164. .probe = zcrypt_cex4_card_probe,
  165. .remove = zcrypt_cex4_card_remove,
  166. .ids = zcrypt_cex4_card_ids,
  167. };
  168. /**
  169. * Probe function for CEX4 queue device. It always accepts the AP device
  170. * since the bus_match already checked the hardware type.
  171. * @ap_dev: pointer to the AP device.
  172. */
  173. static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
  174. {
  175. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  176. struct zcrypt_queue *zq;
  177. int rc;
  178. if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) {
  179. zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE);
  180. if (!zq)
  181. return -ENOMEM;
  182. zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
  183. MSGTYPE50_VARIANT_DEFAULT);
  184. } else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
  185. zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
  186. if (!zq)
  187. return -ENOMEM;
  188. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  189. MSGTYPE06_VARIANT_DEFAULT);
  190. } else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
  191. zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
  192. if (!zq)
  193. return -ENOMEM;
  194. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  195. MSGTYPE06_VARIANT_EP11);
  196. } else {
  197. return -ENODEV;
  198. }
  199. zq->queue = aq;
  200. zq->online = 1;
  201. atomic_set(&zq->load, 0);
  202. ap_queue_init_reply(aq, &zq->reply);
  203. aq->request_timeout = CEX4_CLEANUP_TIME,
  204. aq->private = zq;
  205. rc = zcrypt_queue_register(zq);
  206. if (rc) {
  207. aq->private = NULL;
  208. zcrypt_queue_free(zq);
  209. }
  210. return rc;
  211. }
  212. /**
  213. * This is called to remove the CEX4 queue driver information
  214. * if an AP queue device is removed.
  215. */
  216. static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
  217. {
  218. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  219. struct zcrypt_queue *zq = aq->private;
  220. ap_queue_remove(aq);
  221. if (zq)
  222. zcrypt_queue_unregister(zq);
  223. }
  224. static struct ap_driver zcrypt_cex4_queue_driver = {
  225. .probe = zcrypt_cex4_queue_probe,
  226. .remove = zcrypt_cex4_queue_remove,
  227. .suspend = ap_queue_suspend,
  228. .resume = ap_queue_resume,
  229. .ids = zcrypt_cex4_queue_ids,
  230. };
  231. int __init zcrypt_cex4_init(void)
  232. {
  233. int rc;
  234. rc = ap_driver_register(&zcrypt_cex4_card_driver,
  235. THIS_MODULE, "cex4card");
  236. if (rc)
  237. return rc;
  238. rc = ap_driver_register(&zcrypt_cex4_queue_driver,
  239. THIS_MODULE, "cex4queue");
  240. if (rc)
  241. ap_driver_unregister(&zcrypt_cex4_card_driver);
  242. return rc;
  243. }
  244. void __exit zcrypt_cex4_exit(void)
  245. {
  246. ap_driver_unregister(&zcrypt_cex4_queue_driver);
  247. ap_driver_unregister(&zcrypt_cex4_card_driver);
  248. }
  249. module_init(zcrypt_cex4_init);
  250. module_exit(zcrypt_cex4_exit);