zcrypt_pcicc.c 19 KB

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