zcrypt_pcixcc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/init.h>
  29. #include <linux/err.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/atomic.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/mod_devicetable.h>
  35. #include "ap_bus.h"
  36. #include "zcrypt_api.h"
  37. #include "zcrypt_error.h"
  38. #include "zcrypt_msgtype6.h"
  39. #include "zcrypt_pcixcc.h"
  40. #include "zcrypt_cca_key.h"
  41. #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
  42. #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
  43. #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
  44. #define CEX3C_MIN_MOD_SIZE PCIXCC_MIN_MOD_SIZE
  45. #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
  46. #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
  47. #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
  48. #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
  49. #define PCIXCC_CLEANUP_TIME (15*HZ)
  50. #define CEIL4(x) ((((x)+3)/4)*4)
  51. struct response_type {
  52. struct completion work;
  53. int type;
  54. };
  55. #define PCIXCC_RESPONSE_TYPE_ICA 0
  56. #define PCIXCC_RESPONSE_TYPE_XCRB 1
  57. MODULE_AUTHOR("IBM Corporation");
  58. MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
  59. "Copyright IBM Corp. 2001, 2012");
  60. MODULE_LICENSE("GPL");
  61. static struct ap_device_id zcrypt_pcixcc_card_ids[] = {
  62. { .dev_type = AP_DEVICE_TYPE_PCIXCC,
  63. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  64. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  65. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  66. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  67. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  68. { /* end of list */ },
  69. };
  70. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_card_ids);
  71. static struct ap_device_id zcrypt_pcixcc_queue_ids[] = {
  72. { .dev_type = AP_DEVICE_TYPE_PCIXCC,
  73. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  74. { .dev_type = AP_DEVICE_TYPE_CEX2C,
  75. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  76. { .dev_type = AP_DEVICE_TYPE_CEX3C,
  77. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  78. { /* end of list */ },
  79. };
  80. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_queue_ids);
  81. /**
  82. * Large random number detection function. Its sends a message to a pcixcc
  83. * card to find out if large random numbers are supported.
  84. * @ap_dev: pointer to the AP device.
  85. *
  86. * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  87. */
  88. static int zcrypt_pcixcc_rng_supported(struct ap_queue *aq)
  89. {
  90. struct ap_message ap_msg;
  91. unsigned long long psmid;
  92. unsigned int domain;
  93. struct {
  94. struct type86_hdr hdr;
  95. struct type86_fmt2_ext fmt2;
  96. struct CPRBX cprbx;
  97. } __attribute__((packed)) *reply;
  98. struct {
  99. struct type6_hdr hdr;
  100. struct CPRBX cprbx;
  101. char function_code[2];
  102. short int rule_length;
  103. char rule[8];
  104. short int verb_length;
  105. short int key_length;
  106. } __packed * msg;
  107. int rc, i;
  108. ap_init_message(&ap_msg);
  109. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  110. if (!ap_msg.message)
  111. return -ENOMEM;
  112. rng_type6CPRB_msgX(&ap_msg, 4, &domain);
  113. msg = ap_msg.message;
  114. msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
  115. rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.message,
  116. ap_msg.length);
  117. if (rc)
  118. goto out_free;
  119. /* Wait for the test message to complete. */
  120. for (i = 0; i < 2 * HZ; i++) {
  121. msleep(1000 / HZ);
  122. rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
  123. if (rc == 0 && psmid == 0x0102030405060708ULL)
  124. break;
  125. }
  126. if (i >= 2 * HZ) {
  127. /* Got no answer. */
  128. rc = -ENODEV;
  129. goto out_free;
  130. }
  131. reply = ap_msg.message;
  132. if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
  133. rc = 1;
  134. else
  135. rc = 0;
  136. out_free:
  137. free_page((unsigned long) ap_msg.message);
  138. return rc;
  139. }
  140. /**
  141. * Probe function for PCIXCC/CEX2C card devices. It always accepts the
  142. * AP device since the bus_match already checked the hardware type. The
  143. * PCIXCC cards come in two flavours: micro code level 2 and micro code
  144. * level 3. This is checked by sending a test message to the device.
  145. * @ap_dev: pointer to the AP card device.
  146. */
  147. static int zcrypt_pcixcc_card_probe(struct ap_device *ap_dev)
  148. {
  149. /*
  150. * Normalized speed ratings per crypto adapter
  151. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  152. */
  153. static const int CEX2C_SPEED_IDX[] = {
  154. 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
  155. static const int CEX3C_SPEED_IDX[] = {
  156. 500, 700, 1400, 550, 800, 1500, 80, 10};
  157. struct ap_card *ac = to_ap_card(&ap_dev->device);
  158. struct zcrypt_card *zc;
  159. int rc = 0;
  160. zc = zcrypt_card_alloc();
  161. if (!zc)
  162. return -ENOMEM;
  163. zc->card = ac;
  164. ac->private = zc;
  165. switch (ac->ap_dev.device_type) {
  166. case AP_DEVICE_TYPE_CEX2C:
  167. zc->user_space_type = ZCRYPT_CEX2C;
  168. zc->type_string = "CEX2C";
  169. memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
  170. sizeof(CEX2C_SPEED_IDX));
  171. zc->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  172. zc->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  173. zc->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  174. break;
  175. case AP_DEVICE_TYPE_CEX3C:
  176. zc->user_space_type = ZCRYPT_CEX3C;
  177. zc->type_string = "CEX3C";
  178. memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
  179. sizeof(CEX3C_SPEED_IDX));
  180. zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
  181. zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
  182. zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
  183. break;
  184. default:
  185. zcrypt_card_free(zc);
  186. return -ENODEV;
  187. }
  188. zc->online = 1;
  189. rc = zcrypt_card_register(zc);
  190. if (rc) {
  191. ac->private = NULL;
  192. zcrypt_card_free(zc);
  193. }
  194. return rc;
  195. }
  196. /**
  197. * This is called to remove the PCIXCC/CEX2C card driver information
  198. * if an AP card device is removed.
  199. */
  200. static void zcrypt_pcixcc_card_remove(struct ap_device *ap_dev)
  201. {
  202. struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
  203. if (zc)
  204. zcrypt_card_unregister(zc);
  205. }
  206. static struct ap_driver zcrypt_pcixcc_card_driver = {
  207. .probe = zcrypt_pcixcc_card_probe,
  208. .remove = zcrypt_pcixcc_card_remove,
  209. .ids = zcrypt_pcixcc_card_ids,
  210. };
  211. /**
  212. * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
  213. * AP device since the bus_match already checked the hardware type. The
  214. * PCIXCC cards come in two flavours: micro code level 2 and micro code
  215. * level 3. This is checked by sending a test message to the device.
  216. * @ap_dev: pointer to the AP card device.
  217. */
  218. static int zcrypt_pcixcc_queue_probe(struct ap_device *ap_dev)
  219. {
  220. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  221. struct zcrypt_queue *zq;
  222. int rc;
  223. zq = zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE);
  224. if (!zq)
  225. return -ENOMEM;
  226. zq->queue = aq;
  227. zq->online = 1;
  228. atomic_set(&zq->load, 0);
  229. rc = zcrypt_pcixcc_rng_supported(aq);
  230. if (rc < 0) {
  231. zcrypt_queue_free(zq);
  232. return rc;
  233. }
  234. if (rc)
  235. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  236. MSGTYPE06_VARIANT_DEFAULT);
  237. else
  238. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  239. MSGTYPE06_VARIANT_NORNG);
  240. ap_queue_init_reply(aq, &zq->reply);
  241. aq->request_timeout = PCIXCC_CLEANUP_TIME,
  242. aq->private = zq;
  243. rc = zcrypt_queue_register(zq);
  244. if (rc) {
  245. aq->private = NULL;
  246. zcrypt_queue_free(zq);
  247. }
  248. return rc;
  249. }
  250. /**
  251. * This is called to remove the PCIXCC/CEX2C queue driver information
  252. * if an AP queue device is removed.
  253. */
  254. static void zcrypt_pcixcc_queue_remove(struct ap_device *ap_dev)
  255. {
  256. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  257. struct zcrypt_queue *zq = aq->private;
  258. ap_queue_remove(aq);
  259. if (zq)
  260. zcrypt_queue_unregister(zq);
  261. }
  262. static struct ap_driver zcrypt_pcixcc_queue_driver = {
  263. .probe = zcrypt_pcixcc_queue_probe,
  264. .remove = zcrypt_pcixcc_queue_remove,
  265. .suspend = ap_queue_suspend,
  266. .resume = ap_queue_resume,
  267. .ids = zcrypt_pcixcc_queue_ids,
  268. };
  269. int __init zcrypt_pcixcc_init(void)
  270. {
  271. int rc;
  272. rc = ap_driver_register(&zcrypt_pcixcc_card_driver,
  273. THIS_MODULE, "pcixcccard");
  274. if (rc)
  275. return rc;
  276. rc = ap_driver_register(&zcrypt_pcixcc_queue_driver,
  277. THIS_MODULE, "pcixccqueue");
  278. if (rc)
  279. ap_driver_unregister(&zcrypt_pcixcc_card_driver);
  280. return rc;
  281. }
  282. void zcrypt_pcixcc_exit(void)
  283. {
  284. ap_driver_unregister(&zcrypt_pcixcc_queue_driver);
  285. ap_driver_unregister(&zcrypt_pcixcc_card_driver);
  286. }
  287. module_init(zcrypt_pcixcc_init);
  288. module_exit(zcrypt_pcixcc_exit);