zcrypt_pcicc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * zcrypt 2.1.0
  3. *
  4. * Copyright IBM Corp. 2001, 2006
  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. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #define KMSG_COMPONENT "zcrypt"
  27. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/gfp.h>
  31. #include <linux/err.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_pcicc.h"
  38. #include "zcrypt_cca_key.h"
  39. #define PCICC_MIN_MOD_SIZE 64 /* 512 bits */
  40. #define PCICC_MAX_MOD_SIZE_OLD 128 /* 1024 bits */
  41. #define PCICC_MAX_MOD_SIZE 256 /* 2048 bits */
  42. /*
  43. * PCICC cards need a speed rating of 0. This keeps them at the end of
  44. * the zcrypt device list (see zcrypt_api.c). PCICC cards are only
  45. * used if no other cards are present because they are slow and can only
  46. * cope with PKCS12 padded requests. The logic is queer. PKCS11 padded
  47. * requests are rejected. The modexpo function encrypts PKCS12 padded data
  48. * and decrypts any non-PKCS12 padded data (except PKCS11) in the assumption
  49. * that it's encrypted PKCS12 data. The modexpo_crt function always decrypts
  50. * the data in the assumption that its PKCS12 encrypted data.
  51. */
  52. #define PCICC_SPEED_RATING 0
  53. #define PCICC_MAX_MESSAGE_SIZE 0x710 /* max size type6 v1 crt message */
  54. #define PCICC_MAX_RESPONSE_SIZE 0x710 /* max size type86 v1 reply */
  55. #define PCICC_CLEANUP_TIME (15*HZ)
  56. static struct ap_device_id zcrypt_pcicc_ids[] = {
  57. { AP_DEVICE(AP_DEVICE_TYPE_PCICC) },
  58. { /* end of list */ },
  59. };
  60. MODULE_DEVICE_TABLE(ap, zcrypt_pcicc_ids);
  61. MODULE_AUTHOR("IBM Corporation");
  62. MODULE_DESCRIPTION("PCICC Cryptographic Coprocessor device driver, "
  63. "Copyright IBM Corp. 2001, 2006");
  64. MODULE_LICENSE("GPL");
  65. static int zcrypt_pcicc_probe(struct ap_device *ap_dev);
  66. static void zcrypt_pcicc_remove(struct ap_device *ap_dev);
  67. static void zcrypt_pcicc_receive(struct ap_device *, struct ap_message *,
  68. struct ap_message *);
  69. static struct ap_driver zcrypt_pcicc_driver = {
  70. .probe = zcrypt_pcicc_probe,
  71. .remove = zcrypt_pcicc_remove,
  72. .ids = zcrypt_pcicc_ids,
  73. .request_timeout = PCICC_CLEANUP_TIME,
  74. };
  75. /**
  76. * The following is used to initialize the CPRB passed to the PCICC card
  77. * in a type6 message. The 3 fields that must be filled in at execution
  78. * time are req_parml, rpl_parml and usage_domain. Note that all three
  79. * fields are *little*-endian. Actually, everything about this interface
  80. * is ascii/little-endian, since the device has 'Intel inside'.
  81. *
  82. * The CPRB is followed immediately by the parm block.
  83. * The parm block contains:
  84. * - function code ('PD' 0x5044 or 'PK' 0x504B)
  85. * - rule block (0x0A00 'PKCS-1.2' or 0x0A00 'ZERO-PAD')
  86. * - VUD block
  87. */
  88. static struct CPRB static_cprb = {
  89. .cprb_len = cpu_to_le16(0x0070),
  90. .cprb_ver_id = 0x41,
  91. .func_id = {0x54,0x32},
  92. .checkpoint_flag= 0x01,
  93. .svr_namel = cpu_to_le16(0x0008),
  94. .svr_name = {'I','C','S','F',' ',' ',' ',' '}
  95. };
  96. /**
  97. * Check the message for PKCS11 padding.
  98. */
  99. static inline int is_PKCS11_padded(unsigned char *buffer, int length)
  100. {
  101. int i;
  102. if ((buffer[0] != 0x00) || (buffer[1] != 0x01))
  103. return 0;
  104. for (i = 2; i < length; i++)
  105. if (buffer[i] != 0xFF)
  106. break;
  107. if (i < 10 || i == length)
  108. return 0;
  109. if (buffer[i] != 0x00)
  110. return 0;
  111. return 1;
  112. }
  113. /**
  114. * Check the message for PKCS12 padding.
  115. */
  116. static inline int is_PKCS12_padded(unsigned char *buffer, int length)
  117. {
  118. int i;
  119. if ((buffer[0] != 0x00) || (buffer[1] != 0x02))
  120. return 0;
  121. for (i = 2; i < length; i++)
  122. if (buffer[i] == 0x00)
  123. break;
  124. if ((i < 10) || (i == length))
  125. return 0;
  126. if (buffer[i] != 0x00)
  127. return 0;
  128. return 1;
  129. }
  130. /**
  131. * Convert a ICAMEX message to a type6 MEX message.
  132. *
  133. * @zdev: crypto device pointer
  134. * @zreq: crypto request pointer
  135. * @mex: pointer to user input data
  136. *
  137. * Returns 0 on success or -EFAULT.
  138. */
  139. static int ICAMEX_msg_to_type6MEX_msg(struct zcrypt_device *zdev,
  140. struct ap_message *ap_msg,
  141. struct ica_rsa_modexpo *mex)
  142. {
  143. static struct type6_hdr static_type6_hdr = {
  144. .type = 0x06,
  145. .offset1 = 0x00000058,
  146. .agent_id = {0x01,0x00,0x43,0x43,0x41,0x2D,0x41,0x50,
  147. 0x50,0x4C,0x20,0x20,0x20,0x01,0x01,0x01},
  148. .function_code = {'P','K'},
  149. };
  150. static struct function_and_rules_block static_pke_function_and_rules ={
  151. .function_code = {'P','K'},
  152. .ulen = cpu_to_le16(10),
  153. .only_rule = {'P','K','C','S','-','1','.','2'}
  154. };
  155. struct {
  156. struct type6_hdr hdr;
  157. struct CPRB cprb;
  158. struct function_and_rules_block fr;
  159. unsigned short length;
  160. char text[0];
  161. } __attribute__((packed)) *msg = ap_msg->message;
  162. int vud_len, pad_len, size;
  163. /* VUD.ciphertext */
  164. if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
  165. return -EFAULT;
  166. if (is_PKCS11_padded(msg->text, mex->inputdatalength))
  167. return -EINVAL;
  168. /* static message header and f&r */
  169. msg->hdr = static_type6_hdr;
  170. msg->fr = static_pke_function_and_rules;
  171. if (is_PKCS12_padded(msg->text, mex->inputdatalength)) {
  172. /* strip the padding and adjust the data length */
  173. pad_len = strnlen(msg->text + 2, mex->inputdatalength - 2) + 3;
  174. if (pad_len <= 9 || pad_len >= mex->inputdatalength)
  175. return -ENODEV;
  176. vud_len = mex->inputdatalength - pad_len;
  177. memmove(msg->text, msg->text + pad_len, vud_len);
  178. msg->length = cpu_to_le16(vud_len + 2);
  179. /* Set up key after the variable length text. */
  180. size = zcrypt_type6_mex_key_en(mex, msg->text + vud_len, 0);
  181. if (size < 0)
  182. return size;
  183. size += sizeof(*msg) + vud_len; /* total size of msg */
  184. } else {
  185. vud_len = mex->inputdatalength;
  186. msg->length = cpu_to_le16(2 + vud_len);
  187. msg->hdr.function_code[1] = 'D';
  188. msg->fr.function_code[1] = 'D';
  189. /* Set up key after the variable length text. */
  190. size = zcrypt_type6_mex_key_de(mex, msg->text + vud_len, 0);
  191. if (size < 0)
  192. return size;
  193. size += sizeof(*msg) + vud_len; /* total size of msg */
  194. }
  195. /* message header, cprb and f&r */
  196. msg->hdr.ToCardLen1 = (size - sizeof(msg->hdr) + 3) & -4;
  197. msg->hdr.FromCardLen1 = PCICC_MAX_RESPONSE_SIZE - sizeof(msg->hdr);
  198. msg->cprb = static_cprb;
  199. msg->cprb.usage_domain[0]= AP_QID_QUEUE(zdev->ap_dev->qid);
  200. msg->cprb.req_parml = cpu_to_le16(size - sizeof(msg->hdr) -
  201. sizeof(msg->cprb));
  202. msg->cprb.rpl_parml = cpu_to_le16(msg->hdr.FromCardLen1);
  203. ap_msg->length = (size + 3) & -4;
  204. return 0;
  205. }
  206. /**
  207. * Convert a ICACRT message to a type6 CRT message.
  208. *
  209. * @zdev: crypto device pointer
  210. * @zreq: crypto request pointer
  211. * @crt: pointer to user input data
  212. *
  213. * Returns 0 on success or -EFAULT.
  214. */
  215. static int ICACRT_msg_to_type6CRT_msg(struct zcrypt_device *zdev,
  216. struct ap_message *ap_msg,
  217. struct ica_rsa_modexpo_crt *crt)
  218. {
  219. static struct type6_hdr static_type6_hdr = {
  220. .type = 0x06,
  221. .offset1 = 0x00000058,
  222. .agent_id = {0x01,0x00,0x43,0x43,0x41,0x2D,0x41,0x50,
  223. 0x50,0x4C,0x20,0x20,0x20,0x01,0x01,0x01},
  224. .function_code = {'P','D'},
  225. };
  226. static struct function_and_rules_block static_pkd_function_and_rules ={
  227. .function_code = {'P','D'},
  228. .ulen = cpu_to_le16(10),
  229. .only_rule = {'P','K','C','S','-','1','.','2'}
  230. };
  231. struct {
  232. struct type6_hdr hdr;
  233. struct CPRB cprb;
  234. struct function_and_rules_block fr;
  235. unsigned short length;
  236. char text[0];
  237. } __attribute__((packed)) *msg = ap_msg->message;
  238. int size;
  239. /* VUD.ciphertext */
  240. msg->length = cpu_to_le16(2 + crt->inputdatalength);
  241. if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
  242. return -EFAULT;
  243. if (is_PKCS11_padded(msg->text, crt->inputdatalength))
  244. return -EINVAL;
  245. /* Set up key after the variable length text. */
  246. size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength, 0);
  247. if (size < 0)
  248. return size;
  249. size += sizeof(*msg) + crt->inputdatalength; /* total size of msg */
  250. /* message header, cprb and f&r */
  251. msg->hdr = static_type6_hdr;
  252. msg->hdr.ToCardLen1 = (size - sizeof(msg->hdr) + 3) & -4;
  253. msg->hdr.FromCardLen1 = PCICC_MAX_RESPONSE_SIZE - sizeof(msg->hdr);
  254. msg->cprb = static_cprb;
  255. msg->cprb.usage_domain[0] = AP_QID_QUEUE(zdev->ap_dev->qid);
  256. msg->cprb.req_parml = msg->cprb.rpl_parml =
  257. cpu_to_le16(size - sizeof(msg->hdr) - sizeof(msg->cprb));
  258. msg->fr = static_pkd_function_and_rules;
  259. ap_msg->length = (size + 3) & -4;
  260. return 0;
  261. }
  262. /**
  263. * Copy results from a type 86 reply message back to user space.
  264. *
  265. * @zdev: crypto device pointer
  266. * @reply: reply AP message.
  267. * @data: pointer to user output data
  268. * @length: size of user output data
  269. *
  270. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  271. */
  272. struct type86_reply {
  273. struct type86_hdr hdr;
  274. struct type86_fmt2_ext fmt2;
  275. struct CPRB cprb;
  276. unsigned char pad[4]; /* 4 byte function code/rules block ? */
  277. unsigned short length;
  278. char text[0];
  279. } __attribute__((packed));
  280. static int convert_type86(struct zcrypt_device *zdev,
  281. struct ap_message *reply,
  282. char __user *outputdata,
  283. unsigned int outputdatalength)
  284. {
  285. static unsigned char static_pad[] = {
  286. 0x00,0x02,
  287. 0x1B,0x7B,0x5D,0xB5,0x75,0x01,0x3D,0xFD,
  288. 0x8D,0xD1,0xC7,0x03,0x2D,0x09,0x23,0x57,
  289. 0x89,0x49,0xB9,0x3F,0xBB,0x99,0x41,0x5B,
  290. 0x75,0x21,0x7B,0x9D,0x3B,0x6B,0x51,0x39,
  291. 0xBB,0x0D,0x35,0xB9,0x89,0x0F,0x93,0xA5,
  292. 0x0B,0x47,0xF1,0xD3,0xBB,0xCB,0xF1,0x9D,
  293. 0x23,0x73,0x71,0xFF,0xF3,0xF5,0x45,0xFB,
  294. 0x61,0x29,0x23,0xFD,0xF1,0x29,0x3F,0x7F,
  295. 0x17,0xB7,0x1B,0xA9,0x19,0xBD,0x57,0xA9,
  296. 0xD7,0x95,0xA3,0xCB,0xED,0x1D,0xDB,0x45,
  297. 0x7D,0x11,0xD1,0x51,0x1B,0xED,0x71,0xE9,
  298. 0xB1,0xD1,0xAB,0xAB,0x21,0x2B,0x1B,0x9F,
  299. 0x3B,0x9F,0xF7,0xF7,0xBD,0x63,0xEB,0xAD,
  300. 0xDF,0xB3,0x6F,0x5B,0xDB,0x8D,0xA9,0x5D,
  301. 0xE3,0x7D,0x77,0x49,0x47,0xF5,0xA7,0xFD,
  302. 0xAB,0x2F,0x27,0x35,0x77,0xD3,0x49,0xC9,
  303. 0x09,0xEB,0xB1,0xF9,0xBF,0x4B,0xCB,0x2B,
  304. 0xEB,0xEB,0x05,0xFF,0x7D,0xC7,0x91,0x8B,
  305. 0x09,0x83,0xB9,0xB9,0x69,0x33,0x39,0x6B,
  306. 0x79,0x75,0x19,0xBF,0xBB,0x07,0x1D,0xBD,
  307. 0x29,0xBF,0x39,0x95,0x93,0x1D,0x35,0xC7,
  308. 0xC9,0x4D,0xE5,0x97,0x0B,0x43,0x9B,0xF1,
  309. 0x16,0x93,0x03,0x1F,0xA5,0xFB,0xDB,0xF3,
  310. 0x27,0x4F,0x27,0x61,0x05,0x1F,0xB9,0x23,
  311. 0x2F,0xC3,0x81,0xA9,0x23,0x71,0x55,0x55,
  312. 0xEB,0xED,0x41,0xE5,0xF3,0x11,0xF1,0x43,
  313. 0x69,0x03,0xBD,0x0B,0x37,0x0F,0x51,0x8F,
  314. 0x0B,0xB5,0x89,0x5B,0x67,0xA9,0xD9,0x4F,
  315. 0x01,0xF9,0x21,0x77,0x37,0x73,0x79,0xC5,
  316. 0x7F,0x51,0xC1,0xCF,0x97,0xA1,0x75,0xAD,
  317. 0x35,0x9D,0xD3,0xD3,0xA7,0x9D,0x5D,0x41,
  318. 0x6F,0x65,0x1B,0xCF,0xA9,0x87,0x91,0x09
  319. };
  320. struct type86_reply *msg = reply->message;
  321. unsigned short service_rc, service_rs;
  322. unsigned int reply_len, pad_len;
  323. char *data;
  324. service_rc = le16_to_cpu(msg->cprb.ccp_rtcode);
  325. if (unlikely(service_rc != 0)) {
  326. service_rs = le16_to_cpu(msg->cprb.ccp_rscode);
  327. if (service_rc == 8 && service_rs == 66)
  328. return -EINVAL;
  329. if (service_rc == 8 && service_rs == 65)
  330. return -EINVAL;
  331. if (service_rc == 8 && service_rs == 770) {
  332. zdev->max_mod_size = PCICC_MAX_MOD_SIZE_OLD;
  333. return -EAGAIN;
  334. }
  335. if (service_rc == 8 && service_rs == 783) {
  336. zdev->max_mod_size = PCICC_MAX_MOD_SIZE_OLD;
  337. return -EAGAIN;
  338. }
  339. if (service_rc == 8 && service_rs == 72)
  340. return -EINVAL;
  341. zdev->online = 0;
  342. pr_err("Cryptographic device %x failed and was set offline\n",
  343. zdev->ap_dev->qid);
  344. ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%drc%d",
  345. zdev->ap_dev->qid, zdev->online,
  346. msg->hdr.reply_code);
  347. return -EAGAIN; /* repeat the request on a different device. */
  348. }
  349. data = msg->text;
  350. reply_len = le16_to_cpu(msg->length) - 2;
  351. if (reply_len > outputdatalength)
  352. return -EINVAL;
  353. /*
  354. * For all encipher requests, the length of the ciphertext (reply_len)
  355. * will always equal the modulus length. For MEX decipher requests
  356. * the output needs to get padded. Minimum pad size is 10.
  357. *
  358. * Currently, the cases where padding will be added is for:
  359. * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
  360. * ZERO-PAD and CRT is only supported for PKD requests)
  361. * - PCICC, always
  362. */
  363. pad_len = outputdatalength - reply_len;
  364. if (pad_len > 0) {
  365. if (pad_len < 10)
  366. return -EINVAL;
  367. /* 'restore' padding left in the PCICC/PCIXCC card. */
  368. if (copy_to_user(outputdata, static_pad, pad_len - 1))
  369. return -EFAULT;
  370. if (put_user(0, outputdata + pad_len - 1))
  371. return -EFAULT;
  372. }
  373. /* Copy the crypto response to user space. */
  374. if (copy_to_user(outputdata + pad_len, data, reply_len))
  375. return -EFAULT;
  376. return 0;
  377. }
  378. static int convert_response(struct zcrypt_device *zdev,
  379. struct ap_message *reply,
  380. char __user *outputdata,
  381. unsigned int outputdatalength)
  382. {
  383. struct type86_reply *msg = reply->message;
  384. /* Response type byte is the second byte in the response. */
  385. switch (msg->hdr.type) {
  386. case TYPE82_RSP_CODE:
  387. case TYPE88_RSP_CODE:
  388. return convert_error(zdev, reply);
  389. case TYPE86_RSP_CODE:
  390. if (msg->hdr.reply_code)
  391. return convert_error(zdev, reply);
  392. if (msg->cprb.cprb_ver_id == 0x01)
  393. return convert_type86(zdev, reply,
  394. outputdata, outputdatalength);
  395. /* no break, incorrect cprb version is an unknown response */
  396. default: /* Unknown response type, this should NEVER EVER happen */
  397. zdev->online = 0;
  398. pr_err("Cryptographic device %x failed and was set offline\n",
  399. zdev->ap_dev->qid);
  400. ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%dfail",
  401. zdev->ap_dev->qid, zdev->online);
  402. return -EAGAIN; /* repeat the request on a different device. */
  403. }
  404. }
  405. /**
  406. * This function is called from the AP bus code after a crypto request
  407. * "msg" has finished with the reply message "reply".
  408. * It is called from tasklet context.
  409. * @ap_dev: pointer to the AP device
  410. * @msg: pointer to the AP message
  411. * @reply: pointer to the AP reply message
  412. */
  413. static void zcrypt_pcicc_receive(struct ap_device *ap_dev,
  414. struct ap_message *msg,
  415. struct ap_message *reply)
  416. {
  417. static struct error_hdr error_reply = {
  418. .type = TYPE82_RSP_CODE,
  419. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  420. };
  421. struct type86_reply *t86r;
  422. int length;
  423. /* Copy the reply message to the request message buffer. */
  424. if (IS_ERR(reply)) {
  425. memcpy(msg->message, &error_reply, sizeof(error_reply));
  426. goto out;
  427. }
  428. t86r = reply->message;
  429. if (t86r->hdr.type == TYPE86_RSP_CODE &&
  430. t86r->cprb.cprb_ver_id == 0x01) {
  431. length = sizeof(struct type86_reply) + t86r->length - 2;
  432. length = min(PCICC_MAX_RESPONSE_SIZE, length);
  433. memcpy(msg->message, reply->message, length);
  434. } else
  435. memcpy(msg->message, reply->message, sizeof error_reply);
  436. out:
  437. complete((struct completion *) msg->private);
  438. }
  439. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  440. /**
  441. * The request distributor calls this function if it picked the PCICC
  442. * device to handle a modexpo request.
  443. * @zdev: pointer to zcrypt_device structure that identifies the
  444. * PCICC device to the request distributor
  445. * @mex: pointer to the modexpo request buffer
  446. */
  447. static long zcrypt_pcicc_modexpo(struct zcrypt_device *zdev,
  448. struct ica_rsa_modexpo *mex)
  449. {
  450. struct ap_message ap_msg;
  451. struct completion work;
  452. int rc;
  453. ap_init_message(&ap_msg);
  454. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  455. if (!ap_msg.message)
  456. return -ENOMEM;
  457. ap_msg.receive = zcrypt_pcicc_receive;
  458. ap_msg.length = PAGE_SIZE;
  459. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  460. atomic_inc_return(&zcrypt_step);
  461. ap_msg.private = &work;
  462. rc = ICAMEX_msg_to_type6MEX_msg(zdev, &ap_msg, mex);
  463. if (rc)
  464. goto out_free;
  465. init_completion(&work);
  466. ap_queue_message(zdev->ap_dev, &ap_msg);
  467. rc = wait_for_completion_interruptible(&work);
  468. if (rc == 0)
  469. rc = convert_response(zdev, &ap_msg, mex->outputdata,
  470. mex->outputdatalength);
  471. else
  472. /* Signal pending. */
  473. ap_cancel_message(zdev->ap_dev, &ap_msg);
  474. out_free:
  475. free_page((unsigned long) ap_msg.message);
  476. return rc;
  477. }
  478. /**
  479. * The request distributor calls this function if it picked the PCICC
  480. * device to handle a modexpo_crt request.
  481. * @zdev: pointer to zcrypt_device structure that identifies the
  482. * PCICC device to the request distributor
  483. * @crt: pointer to the modexpoc_crt request buffer
  484. */
  485. static long zcrypt_pcicc_modexpo_crt(struct zcrypt_device *zdev,
  486. struct ica_rsa_modexpo_crt *crt)
  487. {
  488. struct ap_message ap_msg;
  489. struct completion work;
  490. int rc;
  491. ap_init_message(&ap_msg);
  492. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  493. if (!ap_msg.message)
  494. return -ENOMEM;
  495. ap_msg.receive = zcrypt_pcicc_receive;
  496. ap_msg.length = PAGE_SIZE;
  497. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  498. atomic_inc_return(&zcrypt_step);
  499. ap_msg.private = &work;
  500. rc = ICACRT_msg_to_type6CRT_msg(zdev, &ap_msg, crt);
  501. if (rc)
  502. goto out_free;
  503. init_completion(&work);
  504. ap_queue_message(zdev->ap_dev, &ap_msg);
  505. rc = wait_for_completion_interruptible(&work);
  506. if (rc == 0)
  507. rc = convert_response(zdev, &ap_msg, crt->outputdata,
  508. crt->outputdatalength);
  509. else
  510. /* Signal pending. */
  511. ap_cancel_message(zdev->ap_dev, &ap_msg);
  512. out_free:
  513. free_page((unsigned long) ap_msg.message);
  514. return rc;
  515. }
  516. /**
  517. * The crypto operations for a PCICC card.
  518. */
  519. static struct zcrypt_ops zcrypt_pcicc_ops = {
  520. .rsa_modexpo = zcrypt_pcicc_modexpo,
  521. .rsa_modexpo_crt = zcrypt_pcicc_modexpo_crt,
  522. };
  523. /**
  524. * Probe function for PCICC cards. It always accepts the AP device
  525. * since the bus_match already checked the hardware type.
  526. * @ap_dev: pointer to the AP device.
  527. */
  528. static int zcrypt_pcicc_probe(struct ap_device *ap_dev)
  529. {
  530. struct zcrypt_device *zdev;
  531. int rc;
  532. zdev = zcrypt_device_alloc(PCICC_MAX_RESPONSE_SIZE);
  533. if (!zdev)
  534. return -ENOMEM;
  535. zdev->ap_dev = ap_dev;
  536. zdev->ops = &zcrypt_pcicc_ops;
  537. zdev->online = 1;
  538. zdev->user_space_type = ZCRYPT_PCICC;
  539. zdev->type_string = "PCICC";
  540. zdev->min_mod_size = PCICC_MIN_MOD_SIZE;
  541. zdev->max_mod_size = PCICC_MAX_MOD_SIZE;
  542. zdev->speed_rating = PCICC_SPEED_RATING;
  543. zdev->max_exp_bit_length = PCICC_MAX_MOD_SIZE;
  544. ap_dev->reply = &zdev->reply;
  545. ap_dev->private = zdev;
  546. rc = zcrypt_device_register(zdev);
  547. if (rc)
  548. goto out_free;
  549. return 0;
  550. out_free:
  551. ap_dev->private = NULL;
  552. zcrypt_device_free(zdev);
  553. return rc;
  554. }
  555. /**
  556. * This is called to remove the extended PCICC driver information
  557. * if an AP device is removed.
  558. */
  559. static void zcrypt_pcicc_remove(struct ap_device *ap_dev)
  560. {
  561. struct zcrypt_device *zdev = ap_dev->private;
  562. zcrypt_device_unregister(zdev);
  563. }
  564. int __init zcrypt_pcicc_init(void)
  565. {
  566. return ap_driver_register(&zcrypt_pcicc_driver, THIS_MODULE, "pcicc");
  567. }
  568. void zcrypt_pcicc_exit(void)
  569. {
  570. ap_driver_unregister(&zcrypt_pcicc_driver);
  571. }
  572. module_init(zcrypt_pcicc_init);
  573. module_exit(zcrypt_pcicc_exit);