zcrypt_cex2a.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <asm/uaccess.h>
  33. #include "ap_bus.h"
  34. #include "zcrypt_api.h"
  35. #include "zcrypt_error.h"
  36. #include "zcrypt_cex2a.h"
  37. #include "zcrypt_msgtype50.h"
  38. #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
  39. #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
  40. #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
  41. #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
  42. #define CEX2A_SPEED_RATING 970
  43. #define CEX3A_SPEED_RATING 900 /* Fixme: Needs finetuning */
  44. #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
  45. #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
  46. #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
  47. * (max outputdatalength) +
  48. * type80_hdr*/
  49. #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
  50. #define CEX2A_CLEANUP_TIME (15*HZ)
  51. #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
  52. static struct ap_device_id zcrypt_cex2a_ids[] = {
  53. { AP_DEVICE(AP_DEVICE_TYPE_CEX2A) },
  54. { AP_DEVICE(AP_DEVICE_TYPE_CEX3A) },
  55. { /* end of list */ },
  56. };
  57. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
  58. MODULE_AUTHOR("IBM Corporation");
  59. MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \
  60. "Copyright IBM Corp. 2001, 2012");
  61. MODULE_LICENSE("GPL");
  62. static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
  63. static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
  64. static struct ap_driver zcrypt_cex2a_driver = {
  65. .probe = zcrypt_cex2a_probe,
  66. .remove = zcrypt_cex2a_remove,
  67. .ids = zcrypt_cex2a_ids,
  68. .request_timeout = CEX2A_CLEANUP_TIME,
  69. };
  70. /**
  71. * Probe function for CEX2A cards. It always accepts the AP device
  72. * since the bus_match already checked the hardware type.
  73. * @ap_dev: pointer to the AP device.
  74. */
  75. static int zcrypt_cex2a_probe(struct ap_device *ap_dev)
  76. {
  77. struct zcrypt_device *zdev = NULL;
  78. int rc = 0;
  79. switch (ap_dev->device_type) {
  80. case AP_DEVICE_TYPE_CEX2A:
  81. zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
  82. if (!zdev)
  83. return -ENOMEM;
  84. zdev->user_space_type = ZCRYPT_CEX2A;
  85. zdev->type_string = "CEX2A";
  86. zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
  87. zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
  88. zdev->short_crt = 1;
  89. zdev->speed_rating = CEX2A_SPEED_RATING;
  90. zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  91. break;
  92. case AP_DEVICE_TYPE_CEX3A:
  93. zdev = zcrypt_device_alloc(CEX3A_MAX_RESPONSE_SIZE);
  94. if (!zdev)
  95. return -ENOMEM;
  96. zdev->user_space_type = ZCRYPT_CEX3A;
  97. zdev->type_string = "CEX3A";
  98. zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
  99. zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
  100. zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  101. if (ap_test_bit(&ap_dev->functions, AP_FUNC_MEX4K) &&
  102. ap_test_bit(&ap_dev->functions, AP_FUNC_CRT4K)) {
  103. zdev->max_mod_size = CEX3A_MAX_MOD_SIZE;
  104. zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
  105. }
  106. zdev->short_crt = 1;
  107. zdev->speed_rating = CEX3A_SPEED_RATING;
  108. break;
  109. }
  110. if (!zdev)
  111. return -ENODEV;
  112. zdev->ops = zcrypt_msgtype_request(MSGTYPE50_NAME,
  113. MSGTYPE50_VARIANT_DEFAULT);
  114. zdev->ap_dev = ap_dev;
  115. zdev->online = 1;
  116. ap_dev->reply = &zdev->reply;
  117. ap_dev->private = zdev;
  118. rc = zcrypt_device_register(zdev);
  119. if (rc) {
  120. ap_dev->private = NULL;
  121. zcrypt_msgtype_release(zdev->ops);
  122. zcrypt_device_free(zdev);
  123. }
  124. return rc;
  125. }
  126. /**
  127. * This is called to remove the extended CEX2A driver information
  128. * if an AP device is removed.
  129. */
  130. static void zcrypt_cex2a_remove(struct ap_device *ap_dev)
  131. {
  132. struct zcrypt_device *zdev = ap_dev->private;
  133. struct zcrypt_ops *zops = zdev->ops;
  134. zcrypt_device_unregister(zdev);
  135. zcrypt_msgtype_release(zops);
  136. }
  137. int __init zcrypt_cex2a_init(void)
  138. {
  139. return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
  140. }
  141. void __exit zcrypt_cex2a_exit(void)
  142. {
  143. ap_driver_unregister(&zcrypt_cex2a_driver);
  144. }
  145. module_init(zcrypt_cex2a_init);
  146. module_exit(zcrypt_cex2a_exit);