zcrypt_pcicc.c 18 KB

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