zcrypt_cex4.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/CEX5/CEX6 Cryptographic Card device driver, " \
  34. "Copyright IBM Corp. 2018");
  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/CEX5/CEX6 card device. It always
  58. * accepts the AP device since the bus_match already checked
  59. * the hardware type.
  60. * @ap_dev: pointer to the AP device.
  61. */
  62. static int zcrypt_cex4_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 CEX4A_SPEED_IDX[] = {
  69. 14, 19, 249, 42, 228, 1458, 0, 0};
  70. static const int CEX5A_SPEED_IDX[] = {
  71. 8, 9, 20, 18, 66, 458, 0, 0};
  72. static const int CEX6A_SPEED_IDX[] = {
  73. 6, 9, 20, 17, 65, 438, 0, 0};
  74. static const int CEX4C_SPEED_IDX[] = {
  75. 59, 69, 308, 83, 278, 2204, 209, 40};
  76. static const int CEX5C_SPEED_IDX[] = {
  77. 24, 31, 50, 37, 90, 479, 27, 10};
  78. static const int CEX6C_SPEED_IDX[] = {
  79. 16, 20, 32, 27, 77, 455, 23, 9};
  80. static const int CEX4P_SPEED_IDX[] = {
  81. 224, 313, 3560, 359, 605, 2827, 0, 50};
  82. static const int CEX5P_SPEED_IDX[] = {
  83. 63, 84, 156, 83, 142, 533, 0, 10};
  84. static const int CEX6P_SPEED_IDX[] = {
  85. 55, 70, 121, 73, 129, 522, 0, 9};
  86. struct ap_card *ac = to_ap_card(&ap_dev->device);
  87. struct zcrypt_card *zc;
  88. int rc = 0;
  89. zc = zcrypt_card_alloc();
  90. if (!zc)
  91. return -ENOMEM;
  92. zc->card = ac;
  93. ac->private = zc;
  94. if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL)) {
  95. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  96. zc->type_string = "CEX4A";
  97. zc->user_space_type = ZCRYPT_CEX4;
  98. memcpy(zc->speed_rating, CEX4A_SPEED_IDX,
  99. sizeof(CEX4A_SPEED_IDX));
  100. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
  101. zc->type_string = "CEX5A";
  102. zc->user_space_type = ZCRYPT_CEX5;
  103. memcpy(zc->speed_rating, CEX5A_SPEED_IDX,
  104. sizeof(CEX5A_SPEED_IDX));
  105. } else {
  106. zc->type_string = "CEX6A";
  107. zc->user_space_type = ZCRYPT_CEX6;
  108. memcpy(zc->speed_rating, CEX6A_SPEED_IDX,
  109. sizeof(CEX6A_SPEED_IDX));
  110. }
  111. zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
  112. if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
  113. ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
  114. zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
  115. zc->max_exp_bit_length =
  116. CEX4A_MAX_MOD_SIZE_4K;
  117. } else {
  118. zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
  119. zc->max_exp_bit_length =
  120. CEX4A_MAX_MOD_SIZE_2K;
  121. }
  122. } else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
  123. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  124. zc->type_string = "CEX4C";
  125. /* wrong user space type, must be CEX4
  126. * just keep it for cca compatibility
  127. */
  128. zc->user_space_type = ZCRYPT_CEX3C;
  129. memcpy(zc->speed_rating, CEX4C_SPEED_IDX,
  130. sizeof(CEX4C_SPEED_IDX));
  131. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
  132. zc->type_string = "CEX5C";
  133. /* wrong user space type, must be CEX5
  134. * just keep it for cca compatibility
  135. */
  136. zc->user_space_type = ZCRYPT_CEX3C;
  137. memcpy(zc->speed_rating, CEX5C_SPEED_IDX,
  138. sizeof(CEX5C_SPEED_IDX));
  139. } else {
  140. zc->type_string = "CEX6C";
  141. /* wrong user space type, must be CEX6
  142. * just keep it for cca compatibility
  143. */
  144. zc->user_space_type = ZCRYPT_CEX3C;
  145. memcpy(zc->speed_rating, CEX6C_SPEED_IDX,
  146. sizeof(CEX6C_SPEED_IDX));
  147. }
  148. zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
  149. zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
  150. zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  151. } else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
  152. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  153. zc->type_string = "CEX4P";
  154. zc->user_space_type = ZCRYPT_CEX4;
  155. memcpy(zc->speed_rating, CEX4P_SPEED_IDX,
  156. sizeof(CEX4P_SPEED_IDX));
  157. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
  158. zc->type_string = "CEX5P";
  159. zc->user_space_type = ZCRYPT_CEX5;
  160. memcpy(zc->speed_rating, CEX5P_SPEED_IDX,
  161. sizeof(CEX5P_SPEED_IDX));
  162. } else {
  163. zc->type_string = "CEX6P";
  164. zc->user_space_type = ZCRYPT_CEX6;
  165. memcpy(zc->speed_rating, CEX6P_SPEED_IDX,
  166. sizeof(CEX6P_SPEED_IDX));
  167. }
  168. zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
  169. zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
  170. zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  171. } else {
  172. zcrypt_card_free(zc);
  173. return -ENODEV;
  174. }
  175. zc->online = 1;
  176. rc = zcrypt_card_register(zc);
  177. if (rc) {
  178. ac->private = NULL;
  179. zcrypt_card_free(zc);
  180. }
  181. return rc;
  182. }
  183. /**
  184. * This is called to remove the CEX4/CEX5/CEX6 card driver information
  185. * if an AP card device is removed.
  186. */
  187. static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
  188. {
  189. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  190. if (zc)
  191. zcrypt_card_unregister(zc);
  192. }
  193. static struct ap_driver zcrypt_cex4_card_driver = {
  194. .probe = zcrypt_cex4_card_probe,
  195. .remove = zcrypt_cex4_card_remove,
  196. .ids = zcrypt_cex4_card_ids,
  197. .flags = AP_DRIVER_FLAG_DEFAULT,
  198. };
  199. /**
  200. * Probe function for CEX4/CEX5/CEX6 queue device. It always
  201. * accepts the AP device since the bus_match already checked
  202. * the hardware type.
  203. * @ap_dev: pointer to the AP device.
  204. */
  205. static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
  206. {
  207. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  208. struct zcrypt_queue *zq;
  209. int rc;
  210. if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) {
  211. zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE);
  212. if (!zq)
  213. return -ENOMEM;
  214. zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
  215. MSGTYPE50_VARIANT_DEFAULT);
  216. } else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
  217. zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
  218. if (!zq)
  219. return -ENOMEM;
  220. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  221. MSGTYPE06_VARIANT_DEFAULT);
  222. } else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
  223. zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
  224. if (!zq)
  225. return -ENOMEM;
  226. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  227. MSGTYPE06_VARIANT_EP11);
  228. } else {
  229. return -ENODEV;
  230. }
  231. zq->queue = aq;
  232. zq->online = 1;
  233. atomic_set(&zq->load, 0);
  234. ap_queue_init_reply(aq, &zq->reply);
  235. aq->request_timeout = CEX4_CLEANUP_TIME,
  236. aq->private = zq;
  237. rc = zcrypt_queue_register(zq);
  238. if (rc) {
  239. aq->private = NULL;
  240. zcrypt_queue_free(zq);
  241. }
  242. return rc;
  243. }
  244. /**
  245. * This is called to remove the CEX4/CEX5/CEX6 queue driver
  246. * information if an AP queue device is removed.
  247. */
  248. static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
  249. {
  250. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  251. struct zcrypt_queue *zq = aq->private;
  252. if (zq)
  253. zcrypt_queue_unregister(zq);
  254. }
  255. static struct ap_driver zcrypt_cex4_queue_driver = {
  256. .probe = zcrypt_cex4_queue_probe,
  257. .remove = zcrypt_cex4_queue_remove,
  258. .suspend = ap_queue_suspend,
  259. .resume = ap_queue_resume,
  260. .ids = zcrypt_cex4_queue_ids,
  261. .flags = AP_DRIVER_FLAG_DEFAULT,
  262. };
  263. int __init zcrypt_cex4_init(void)
  264. {
  265. int rc;
  266. rc = ap_driver_register(&zcrypt_cex4_card_driver,
  267. THIS_MODULE, "cex4card");
  268. if (rc)
  269. return rc;
  270. rc = ap_driver_register(&zcrypt_cex4_queue_driver,
  271. THIS_MODULE, "cex4queue");
  272. if (rc)
  273. ap_driver_unregister(&zcrypt_cex4_card_driver);
  274. return rc;
  275. }
  276. void __exit zcrypt_cex4_exit(void)
  277. {
  278. ap_driver_unregister(&zcrypt_cex4_queue_driver);
  279. ap_driver_unregister(&zcrypt_cex4_card_driver);
  280. }
  281. module_init(zcrypt_cex4_init);
  282. module_exit(zcrypt_cex4_exit);