zcrypt_cex2a.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * zcrypt 2.1.0
  3. *
  4. * Copyright IBM Corp. 2001, 2012
  5. * Author(s): Robert Burroughs
  6. * Eric Rossman (edrossma@us.ibm.com)
  7. *
  8. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  9. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  10. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  11. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2, or (at your option)
  16. * any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/err.h>
  31. #include <linux/atomic.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/mod_devicetable.h>
  34. #include "ap_bus.h"
  35. #include "zcrypt_api.h"
  36. #include "zcrypt_error.h"
  37. #include "zcrypt_cex2a.h"
  38. #include "zcrypt_msgtype50.h"
  39. #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
  40. #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
  41. #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
  42. #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
  43. #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
  44. #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
  45. #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
  46. * (max outputdatalength) +
  47. * type80_hdr*/
  48. #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
  49. #define CEX2A_CLEANUP_TIME (15*HZ)
  50. #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
  51. MODULE_AUTHOR("IBM Corporation");
  52. MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \
  53. "Copyright IBM Corp. 2001, 2012");
  54. MODULE_LICENSE("GPL");
  55. static struct ap_device_id zcrypt_cex2a_card_ids[] = {
  56. { .dev_type = AP_DEVICE_TYPE_CEX2A,
  57. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  58. { .dev_type = AP_DEVICE_TYPE_CEX3A,
  59. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  60. { /* end of list */ },
  61. };
  62. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids);
  63. static struct ap_device_id zcrypt_cex2a_queue_ids[] = {
  64. { .dev_type = AP_DEVICE_TYPE_CEX2A,
  65. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  66. { .dev_type = AP_DEVICE_TYPE_CEX3A,
  67. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  68. { /* end of list */ },
  69. };
  70. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids);
  71. /**
  72. * Probe function for CEX2A card devices. It always accepts the AP device
  73. * since the bus_match already checked the card type.
  74. * @ap_dev: pointer to the AP device.
  75. */
  76. static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev)
  77. {
  78. /*
  79. * Normalized speed ratings per crypto adapter
  80. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  81. */
  82. static const int CEX2A_SPEED_IDX[] = {
  83. 800, 1000, 2000, 900, 1200, 2400, 0, 0};
  84. static const int CEX3A_SPEED_IDX[] = {
  85. 400, 500, 1000, 450, 550, 1200, 0, 0};
  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 (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A) {
  95. zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
  96. zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
  97. memcpy(zc->speed_rating, CEX2A_SPEED_IDX,
  98. sizeof(CEX2A_SPEED_IDX));
  99. zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  100. zc->type_string = "CEX2A";
  101. zc->user_space_type = ZCRYPT_CEX2A;
  102. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX3A) {
  103. zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
  104. zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
  105. zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  106. if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
  107. ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
  108. zc->max_mod_size = CEX3A_MAX_MOD_SIZE;
  109. zc->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
  110. }
  111. memcpy(zc->speed_rating, CEX3A_SPEED_IDX,
  112. sizeof(CEX3A_SPEED_IDX));
  113. zc->type_string = "CEX3A";
  114. zc->user_space_type = ZCRYPT_CEX3A;
  115. } else {
  116. zcrypt_card_free(zc);
  117. return -ENODEV;
  118. }
  119. zc->online = 1;
  120. rc = zcrypt_card_register(zc);
  121. if (rc) {
  122. ac->private = NULL;
  123. zcrypt_card_free(zc);
  124. }
  125. return rc;
  126. }
  127. /**
  128. * This is called to remove the CEX2A card driver information
  129. * if an AP card device is removed.
  130. */
  131. static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev)
  132. {
  133. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  134. if (zc)
  135. zcrypt_card_unregister(zc);
  136. }
  137. static struct ap_driver zcrypt_cex2a_card_driver = {
  138. .probe = zcrypt_cex2a_card_probe,
  139. .remove = zcrypt_cex2a_card_remove,
  140. .ids = zcrypt_cex2a_card_ids,
  141. };
  142. /**
  143. * Probe function for CEX2A queue devices. It always accepts the AP device
  144. * since the bus_match already checked the queue type.
  145. * @ap_dev: pointer to the AP device.
  146. */
  147. static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev)
  148. {
  149. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  150. struct zcrypt_queue *zq = NULL;
  151. int rc;
  152. switch (ap_dev->device_type) {
  153. case AP_DEVICE_TYPE_CEX2A:
  154. zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE);
  155. if (!zq)
  156. return -ENOMEM;
  157. break;
  158. case AP_DEVICE_TYPE_CEX3A:
  159. zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE);
  160. if (!zq)
  161. return -ENOMEM;
  162. break;
  163. }
  164. if (!zq)
  165. return -ENODEV;
  166. zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT);
  167. zq->queue = aq;
  168. zq->online = 1;
  169. atomic_set(&zq->load, 0);
  170. ap_queue_init_reply(aq, &zq->reply);
  171. aq->request_timeout = CEX2A_CLEANUP_TIME,
  172. aq->private = zq;
  173. rc = zcrypt_queue_register(zq);
  174. if (rc) {
  175. aq->private = NULL;
  176. zcrypt_queue_free(zq);
  177. }
  178. return rc;
  179. }
  180. /**
  181. * This is called to remove the CEX2A queue driver information
  182. * if an AP queue device is removed.
  183. */
  184. static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev)
  185. {
  186. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  187. struct zcrypt_queue *zq = aq->private;
  188. ap_queue_remove(aq);
  189. if (zq)
  190. zcrypt_queue_unregister(zq);
  191. }
  192. static struct ap_driver zcrypt_cex2a_queue_driver = {
  193. .probe = zcrypt_cex2a_queue_probe,
  194. .remove = zcrypt_cex2a_queue_remove,
  195. .suspend = ap_queue_suspend,
  196. .resume = ap_queue_resume,
  197. .ids = zcrypt_cex2a_queue_ids,
  198. };
  199. int __init zcrypt_cex2a_init(void)
  200. {
  201. int rc;
  202. rc = ap_driver_register(&zcrypt_cex2a_card_driver,
  203. THIS_MODULE, "cex2acard");
  204. if (rc)
  205. return rc;
  206. rc = ap_driver_register(&zcrypt_cex2a_queue_driver,
  207. THIS_MODULE, "cex2aqueue");
  208. if (rc)
  209. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  210. return rc;
  211. }
  212. void __exit zcrypt_cex2a_exit(void)
  213. {
  214. ap_driver_unregister(&zcrypt_cex2a_queue_driver);
  215. ap_driver_unregister(&zcrypt_cex2a_card_driver);
  216. }
  217. module_init(zcrypt_cex2a_init);
  218. module_exit(zcrypt_cex2a_exit);