zcrypt_cex4.c 8.9 KB

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