zcrypt_pcixcc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <asm/uaccess.h>
  34. #include "ap_bus.h"
  35. #include "zcrypt_api.h"
  36. #include "zcrypt_error.h"
  37. #include "zcrypt_msgtype6.h"
  38. #include "zcrypt_pcixcc.h"
  39. #include "zcrypt_cca_key.h"
  40. #include "zcrypt_msgtype6.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_MCL2_SPEED_RATING 7870
  47. #define PCIXCC_MCL3_SPEED_RATING 7870
  48. #define CEX2C_SPEED_RATING 7000
  49. #define CEX3C_SPEED_RATING 6500
  50. #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
  51. #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
  52. #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
  53. #define PCIXCC_CLEANUP_TIME (15*HZ)
  54. #define CEIL4(x) ((((x)+3)/4)*4)
  55. struct response_type {
  56. struct completion work;
  57. int type;
  58. };
  59. #define PCIXCC_RESPONSE_TYPE_ICA 0
  60. #define PCIXCC_RESPONSE_TYPE_XCRB 1
  61. static struct ap_device_id zcrypt_pcixcc_ids[] = {
  62. { AP_DEVICE(AP_DEVICE_TYPE_PCIXCC) },
  63. { AP_DEVICE(AP_DEVICE_TYPE_CEX2C) },
  64. { AP_DEVICE(AP_DEVICE_TYPE_CEX3C) },
  65. { /* end of list */ },
  66. };
  67. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_ids);
  68. MODULE_AUTHOR("IBM Corporation");
  69. MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
  70. "Copyright IBM Corp. 2001, 2012");
  71. MODULE_LICENSE("GPL");
  72. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev);
  73. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev);
  74. static struct ap_driver zcrypt_pcixcc_driver = {
  75. .probe = zcrypt_pcixcc_probe,
  76. .remove = zcrypt_pcixcc_remove,
  77. .ids = zcrypt_pcixcc_ids,
  78. .request_timeout = PCIXCC_CLEANUP_TIME,
  79. };
  80. /**
  81. * Micro-code detection function. Its sends a message to a pcixcc card
  82. * to find out the microcode level.
  83. * @ap_dev: pointer to the AP device.
  84. */
  85. static int zcrypt_pcixcc_mcl(struct ap_device *ap_dev)
  86. {
  87. static unsigned char msg[] = {
  88. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  89. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  90. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  91. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  92. 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
  93. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  94. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00,
  95. 0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00,
  96. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  97. 0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00,
  98. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  99. 0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32,
  100. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8,
  101. 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,
  102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  103. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  104. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  105. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  106. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  107. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  108. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  109. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  110. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  111. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  113. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  115. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  116. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  117. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  118. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  119. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  120. 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
  121. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  122. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  123. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  124. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  125. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  126. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x0A,
  127. 0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20,
  128. 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05,
  129. 0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
  130. 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55,
  131. 0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,
  132. 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,
  133. 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,
  134. 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,
  135. 0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54,
  136. 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00,
  137. 0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00,
  138. 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40,
  139. 0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C,
  140. 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF,
  141. 0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9,
  142. 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63,
  143. 0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5,
  144. 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A,
  145. 0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01,
  146. 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28,
  147. 0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91,
  148. 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5,
  149. 0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C,
  150. 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98,
  151. 0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96,
  152. 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19,
  153. 0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47,
  154. 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36,
  155. 0xF1,0x3D,0x93,0x53
  156. };
  157. unsigned long long psmid;
  158. struct CPRBX *cprbx;
  159. char *reply;
  160. int rc, i;
  161. reply = (void *) get_zeroed_page(GFP_KERNEL);
  162. if (!reply)
  163. return -ENOMEM;
  164. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, msg, sizeof(msg));
  165. if (rc)
  166. goto out_free;
  167. /* Wait for the test message to complete. */
  168. for (i = 0; i < 6; i++) {
  169. mdelay(300);
  170. rc = ap_recv(ap_dev->qid, &psmid, reply, 4096);
  171. if (rc == 0 && psmid == 0x0102030405060708ULL)
  172. break;
  173. }
  174. if (i >= 6) {
  175. /* Got no answer. */
  176. rc = -ENODEV;
  177. goto out_free;
  178. }
  179. cprbx = (struct CPRBX *) (reply + 48);
  180. if (cprbx->ccp_rtcode == 8 && cprbx->ccp_rscode == 33)
  181. rc = ZCRYPT_PCIXCC_MCL2;
  182. else
  183. rc = ZCRYPT_PCIXCC_MCL3;
  184. out_free:
  185. free_page((unsigned long) reply);
  186. return rc;
  187. }
  188. /**
  189. * Large random number detection function. Its sends a message to a pcixcc
  190. * card to find out if large random numbers are supported.
  191. * @ap_dev: pointer to the AP device.
  192. *
  193. * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  194. */
  195. static int zcrypt_pcixcc_rng_supported(struct ap_device *ap_dev)
  196. {
  197. struct ap_message ap_msg;
  198. unsigned long long psmid;
  199. struct {
  200. struct type86_hdr hdr;
  201. struct type86_fmt2_ext fmt2;
  202. struct CPRBX cprbx;
  203. } __attribute__((packed)) *reply;
  204. int rc, i;
  205. ap_init_message(&ap_msg);
  206. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  207. if (!ap_msg.message)
  208. return -ENOMEM;
  209. rng_type6CPRB_msgX(ap_dev, &ap_msg, 4);
  210. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, ap_msg.message,
  211. ap_msg.length);
  212. if (rc)
  213. goto out_free;
  214. /* Wait for the test message to complete. */
  215. for (i = 0; i < 2 * HZ; i++) {
  216. msleep(1000 / HZ);
  217. rc = ap_recv(ap_dev->qid, &psmid, ap_msg.message, 4096);
  218. if (rc == 0 && psmid == 0x0102030405060708ULL)
  219. break;
  220. }
  221. if (i >= 2 * HZ) {
  222. /* Got no answer. */
  223. rc = -ENODEV;
  224. goto out_free;
  225. }
  226. reply = ap_msg.message;
  227. if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
  228. rc = 1;
  229. else
  230. rc = 0;
  231. out_free:
  232. free_page((unsigned long) ap_msg.message);
  233. return rc;
  234. }
  235. /**
  236. * Probe function for PCIXCC/CEX2C cards. It always accepts the AP device
  237. * since the bus_match already checked the hardware type. The PCIXCC
  238. * cards come in two flavours: micro code level 2 and micro code level 3.
  239. * This is checked by sending a test message to the device.
  240. * @ap_dev: pointer to the AP device.
  241. */
  242. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev)
  243. {
  244. struct zcrypt_device *zdev;
  245. int rc = 0;
  246. zdev = zcrypt_device_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE);
  247. if (!zdev)
  248. return -ENOMEM;
  249. zdev->ap_dev = ap_dev;
  250. zdev->online = 1;
  251. switch (ap_dev->device_type) {
  252. case AP_DEVICE_TYPE_PCIXCC:
  253. rc = zcrypt_pcixcc_mcl(ap_dev);
  254. if (rc < 0) {
  255. zcrypt_device_free(zdev);
  256. return rc;
  257. }
  258. zdev->user_space_type = rc;
  259. if (rc == ZCRYPT_PCIXCC_MCL2) {
  260. zdev->type_string = "PCIXCC_MCL2";
  261. zdev->speed_rating = PCIXCC_MCL2_SPEED_RATING;
  262. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  263. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  264. zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  265. } else {
  266. zdev->type_string = "PCIXCC_MCL3";
  267. zdev->speed_rating = PCIXCC_MCL3_SPEED_RATING;
  268. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  269. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  270. zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  271. }
  272. break;
  273. case AP_DEVICE_TYPE_CEX2C:
  274. zdev->user_space_type = ZCRYPT_CEX2C;
  275. zdev->type_string = "CEX2C";
  276. zdev->speed_rating = CEX2C_SPEED_RATING;
  277. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  278. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  279. zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  280. break;
  281. case AP_DEVICE_TYPE_CEX3C:
  282. zdev->user_space_type = ZCRYPT_CEX3C;
  283. zdev->type_string = "CEX3C";
  284. zdev->speed_rating = CEX3C_SPEED_RATING;
  285. zdev->min_mod_size = CEX3C_MIN_MOD_SIZE;
  286. zdev->max_mod_size = CEX3C_MAX_MOD_SIZE;
  287. zdev->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
  288. break;
  289. default:
  290. goto out_free;
  291. }
  292. rc = zcrypt_pcixcc_rng_supported(ap_dev);
  293. if (rc < 0) {
  294. zcrypt_device_free(zdev);
  295. return rc;
  296. }
  297. if (rc)
  298. zdev->ops = zcrypt_msgtype_request(MSGTYPE06_NAME,
  299. MSGTYPE06_VARIANT_DEFAULT);
  300. else
  301. zdev->ops = zcrypt_msgtype_request(MSGTYPE06_NAME,
  302. MSGTYPE06_VARIANT_NORNG);
  303. ap_dev->reply = &zdev->reply;
  304. ap_dev->private = zdev;
  305. rc = zcrypt_device_register(zdev);
  306. if (rc)
  307. goto out_free;
  308. return 0;
  309. out_free:
  310. ap_dev->private = NULL;
  311. zcrypt_msgtype_release(zdev->ops);
  312. zcrypt_device_free(zdev);
  313. return rc;
  314. }
  315. /**
  316. * This is called to remove the extended PCIXCC/CEX2C driver information
  317. * if an AP device is removed.
  318. */
  319. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev)
  320. {
  321. struct zcrypt_device *zdev = ap_dev->private;
  322. struct zcrypt_ops *zops = zdev->ops;
  323. zcrypt_device_unregister(zdev);
  324. zcrypt_msgtype_release(zops);
  325. }
  326. int __init zcrypt_pcixcc_init(void)
  327. {
  328. return ap_driver_register(&zcrypt_pcixcc_driver, THIS_MODULE, "pcixcc");
  329. }
  330. void zcrypt_pcixcc_exit(void)
  331. {
  332. ap_driver_unregister(&zcrypt_pcixcc_driver);
  333. }
  334. module_init(zcrypt_pcixcc_init);
  335. module_exit(zcrypt_pcixcc_exit);