zcrypt_cex4.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "ap_bus.h"
  12. #include "zcrypt_api.h"
  13. #include "zcrypt_msgtype6.h"
  14. #include "zcrypt_msgtype50.h"
  15. #include "zcrypt_error.h"
  16. #include "zcrypt_cex4.h"
  17. #define CEX4A_MIN_MOD_SIZE 1 /* 8 bits */
  18. #define CEX4A_MAX_MOD_SIZE_2K 256 /* 2048 bits */
  19. #define CEX4A_MAX_MOD_SIZE_4K 512 /* 4096 bits */
  20. #define CEX4C_MIN_MOD_SIZE 16 /* 256 bits */
  21. #define CEX4C_MAX_MOD_SIZE 512 /* 4096 bits */
  22. #define CEX4A_SPEED_RATING 900 /* TODO new card, new speed rating */
  23. #define CEX4C_SPEED_RATING 6500 /* TODO new card, new speed rating */
  24. #define CEX4P_SPEED_RATING 7000 /* TODO new card, new speed rating */
  25. #define CEX5A_SPEED_RATING 450 /* TODO new card, new speed rating */
  26. #define CEX5C_SPEED_RATING 3250 /* TODO new card, new speed rating */
  27. #define CEX5P_SPEED_RATING 3500 /* TODO new card, new speed rating */
  28. #define CEX4A_MAX_MESSAGE_SIZE MSGTYPE50_CRB3_MAX_MSG_SIZE
  29. #define CEX4C_MAX_MESSAGE_SIZE MSGTYPE06_MAX_MSG_SIZE
  30. /* Waiting time for requests to be processed.
  31. * Currently there are some types of request which are not deterministic.
  32. * But the maximum time limit managed by the stomper code is set to 60sec.
  33. * Hence we have to wait at least that time period.
  34. */
  35. #define CEX4_CLEANUP_TIME (900*HZ)
  36. static struct ap_device_id zcrypt_cex4_ids[] = {
  37. { AP_DEVICE(AP_DEVICE_TYPE_CEX4) },
  38. { AP_DEVICE(AP_DEVICE_TYPE_CEX5) },
  39. { /* end of list */ },
  40. };
  41. MODULE_DEVICE_TABLE(ap, zcrypt_cex4_ids);
  42. MODULE_AUTHOR("IBM Corporation");
  43. MODULE_DESCRIPTION("CEX4 Cryptographic Card device driver, " \
  44. "Copyright IBM Corp. 2012");
  45. MODULE_LICENSE("GPL");
  46. static int zcrypt_cex4_probe(struct ap_device *ap_dev);
  47. static void zcrypt_cex4_remove(struct ap_device *ap_dev);
  48. static struct ap_driver zcrypt_cex4_driver = {
  49. .probe = zcrypt_cex4_probe,
  50. .remove = zcrypt_cex4_remove,
  51. .ids = zcrypt_cex4_ids,
  52. .request_timeout = CEX4_CLEANUP_TIME,
  53. };
  54. /**
  55. * Probe function for CEX4 cards. It always accepts the AP device
  56. * since the bus_match already checked the hardware type.
  57. * @ap_dev: pointer to the AP device.
  58. */
  59. static int zcrypt_cex4_probe(struct ap_device *ap_dev)
  60. {
  61. struct zcrypt_device *zdev = NULL;
  62. int rc = 0;
  63. switch (ap_dev->device_type) {
  64. case AP_DEVICE_TYPE_CEX4:
  65. case AP_DEVICE_TYPE_CEX5:
  66. if (ap_test_bit(&ap_dev->functions, AP_FUNC_ACCEL)) {
  67. zdev = zcrypt_device_alloc(CEX4A_MAX_MESSAGE_SIZE);
  68. if (!zdev)
  69. return -ENOMEM;
  70. if (ap_dev->device_type == AP_DEVICE_TYPE_CEX4) {
  71. zdev->type_string = "CEX4A";
  72. zdev->speed_rating = CEX4A_SPEED_RATING;
  73. } else {
  74. zdev->type_string = "CEX5A";
  75. zdev->speed_rating = CEX5A_SPEED_RATING;
  76. }
  77. zdev->user_space_type = ZCRYPT_CEX3A;
  78. zdev->min_mod_size = CEX4A_MIN_MOD_SIZE;
  79. if (ap_test_bit(&ap_dev->functions, AP_FUNC_MEX4K) &&
  80. ap_test_bit(&ap_dev->functions, AP_FUNC_CRT4K)) {
  81. zdev->max_mod_size =
  82. CEX4A_MAX_MOD_SIZE_4K;
  83. zdev->max_exp_bit_length =
  84. CEX4A_MAX_MOD_SIZE_4K;
  85. } else {
  86. zdev->max_mod_size =
  87. CEX4A_MAX_MOD_SIZE_2K;
  88. zdev->max_exp_bit_length =
  89. CEX4A_MAX_MOD_SIZE_2K;
  90. }
  91. zdev->short_crt = 1;
  92. zdev->ops = zcrypt_msgtype_request(MSGTYPE50_NAME,
  93. MSGTYPE50_VARIANT_DEFAULT);
  94. } else if (ap_test_bit(&ap_dev->functions, AP_FUNC_COPRO)) {
  95. zdev = zcrypt_device_alloc(CEX4C_MAX_MESSAGE_SIZE);
  96. if (!zdev)
  97. return -ENOMEM;
  98. if (ap_dev->device_type == AP_DEVICE_TYPE_CEX4) {
  99. zdev->type_string = "CEX4C";
  100. zdev->speed_rating = CEX4C_SPEED_RATING;
  101. } else {
  102. zdev->type_string = "CEX5C";
  103. zdev->speed_rating = CEX5C_SPEED_RATING;
  104. }
  105. zdev->user_space_type = ZCRYPT_CEX3C;
  106. zdev->min_mod_size = CEX4C_MIN_MOD_SIZE;
  107. zdev->max_mod_size = CEX4C_MAX_MOD_SIZE;
  108. zdev->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  109. zdev->short_crt = 0;
  110. zdev->ops = zcrypt_msgtype_request(MSGTYPE06_NAME,
  111. MSGTYPE06_VARIANT_DEFAULT);
  112. } else if (ap_test_bit(&ap_dev->functions, AP_FUNC_EP11)) {
  113. zdev = zcrypt_device_alloc(CEX4C_MAX_MESSAGE_SIZE);
  114. if (!zdev)
  115. return -ENOMEM;
  116. if (ap_dev->device_type == AP_DEVICE_TYPE_CEX4) {
  117. zdev->type_string = "CEX4P";
  118. zdev->speed_rating = CEX4P_SPEED_RATING;
  119. } else {
  120. zdev->type_string = "CEX5P";
  121. zdev->speed_rating = CEX5P_SPEED_RATING;
  122. }
  123. zdev->user_space_type = ZCRYPT_CEX4;
  124. zdev->min_mod_size = CEX4C_MIN_MOD_SIZE;
  125. zdev->max_mod_size = CEX4C_MAX_MOD_SIZE;
  126. zdev->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  127. zdev->short_crt = 0;
  128. zdev->ops = zcrypt_msgtype_request(MSGTYPE06_NAME,
  129. MSGTYPE06_VARIANT_EP11);
  130. }
  131. break;
  132. }
  133. if (!zdev)
  134. return -ENODEV;
  135. zdev->ap_dev = ap_dev;
  136. zdev->online = 1;
  137. ap_device_init_reply(ap_dev, &zdev->reply);
  138. ap_dev->private = zdev;
  139. rc = zcrypt_device_register(zdev);
  140. if (rc) {
  141. zcrypt_msgtype_release(zdev->ops);
  142. ap_dev->private = NULL;
  143. zcrypt_device_free(zdev);
  144. }
  145. return rc;
  146. }
  147. /**
  148. * This is called to remove the extended CEX4 driver information
  149. * if an AP device is removed.
  150. */
  151. static void zcrypt_cex4_remove(struct ap_device *ap_dev)
  152. {
  153. struct zcrypt_device *zdev = ap_dev->private;
  154. struct zcrypt_ops *zops;
  155. if (zdev) {
  156. zops = zdev->ops;
  157. zcrypt_device_unregister(zdev);
  158. zcrypt_msgtype_release(zops);
  159. }
  160. }
  161. int __init zcrypt_cex4_init(void)
  162. {
  163. return ap_driver_register(&zcrypt_cex4_driver, THIS_MODULE, "cex4");
  164. }
  165. void __exit zcrypt_cex4_exit(void)
  166. {
  167. ap_driver_unregister(&zcrypt_cex4_driver);
  168. }
  169. module_init(zcrypt_cex4_init);
  170. module_exit(zcrypt_cex4_exit);