zcrypt_pcixcc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * linux/drivers/s390/crypto/zcrypt_pcixcc.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 <linux/delay.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_pcixcc.h"
  39. #include "zcrypt_cca_key.h"
  40. #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
  41. #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
  42. #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
  43. #define PCIXCC_MCL2_SPEED_RATING 7870 /* FIXME: needs finetuning */
  44. #define PCIXCC_MCL3_SPEED_RATING 7870
  45. #define CEX2C_SPEED_RATING 8540
  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_MAX_XCRB_RESPONSE_SIZE PCIXCC_MAX_XCRB_MESSAGE_SIZE
  50. #define PCIXCC_MAX_XCRB_DATA_SIZE (11*1024)
  51. #define PCIXCC_MAX_XCRB_REPLY_SIZE (5*1024)
  52. #define PCIXCC_MAX_RESPONSE_SIZE PCIXCC_MAX_XCRB_RESPONSE_SIZE
  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. { /* end of list */ },
  65. };
  66. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  67. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_ids);
  68. MODULE_AUTHOR("IBM Corporation");
  69. MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, "
  70. "Copyright 2001, 2006 IBM Corporation");
  71. MODULE_LICENSE("GPL");
  72. #endif
  73. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev);
  74. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev);
  75. static void zcrypt_pcixcc_receive(struct ap_device *, struct ap_message *,
  76. struct ap_message *);
  77. static struct ap_driver zcrypt_pcixcc_driver = {
  78. .probe = zcrypt_pcixcc_probe,
  79. .remove = zcrypt_pcixcc_remove,
  80. .receive = zcrypt_pcixcc_receive,
  81. .ids = zcrypt_pcixcc_ids,
  82. };
  83. /**
  84. * The following is used to initialize the CPRBX passed to the PCIXCC/CEX2C
  85. * card in a type6 message. The 3 fields that must be filled in at execution
  86. * time are req_parml, rpl_parml and usage_domain.
  87. * Everything about this interface is ascii/big-endian, since the
  88. * device does *not* have 'Intel inside'.
  89. *
  90. * The CPRBX is followed immediately by the parm block.
  91. * The parm block contains:
  92. * - function code ('PD' 0x5044 or 'PK' 0x504B)
  93. * - rule block (one of:)
  94. * + 0x000A 'PKCS-1.2' (MCL2 'PD')
  95. * + 0x000A 'ZERO-PAD' (MCL2 'PK')
  96. * + 0x000A 'ZERO-PAD' (MCL3 'PD' or CEX2C 'PD')
  97. * + 0x000A 'MRP ' (MCL3 'PK' or CEX2C 'PK')
  98. * - VUD block
  99. */
  100. static struct CPRBX static_cprbx = {
  101. .cprb_len = 0x00DC,
  102. .cprb_ver_id = 0x02,
  103. .func_id = {0x54,0x32},
  104. };
  105. /**
  106. * Convert a ICAMEX message to a type6 MEX message.
  107. *
  108. * @zdev: crypto device pointer
  109. * @ap_msg: pointer to AP message
  110. * @mex: pointer to user input data
  111. *
  112. * Returns 0 on success or -EFAULT.
  113. */
  114. static int ICAMEX_msg_to_type6MEX_msgX(struct zcrypt_device *zdev,
  115. struct ap_message *ap_msg,
  116. struct ica_rsa_modexpo *mex)
  117. {
  118. static struct type6_hdr static_type6_hdrX = {
  119. .type = 0x06,
  120. .offset1 = 0x00000058,
  121. .agent_id = {'C','A',},
  122. .function_code = {'P','K'},
  123. };
  124. static struct function_and_rules_block static_pke_fnr = {
  125. .function_code = {'P','K'},
  126. .ulen = 10,
  127. .only_rule = {'M','R','P',' ',' ',' ',' ',' '}
  128. };
  129. static struct function_and_rules_block static_pke_fnr_MCL2 = {
  130. .function_code = {'P','K'},
  131. .ulen = 10,
  132. .only_rule = {'Z','E','R','O','-','P','A','D'}
  133. };
  134. struct {
  135. struct type6_hdr hdr;
  136. struct CPRBX cprbx;
  137. struct function_and_rules_block fr;
  138. unsigned short length;
  139. char text[0];
  140. } __attribute__((packed)) *msg = ap_msg->message;
  141. int size;
  142. /* VUD.ciphertext */
  143. msg->length = mex->inputdatalength + 2;
  144. if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
  145. return -EFAULT;
  146. /* Set up key which is located after the variable length text. */
  147. size = zcrypt_type6_mex_key_en(mex, msg->text+mex->inputdatalength, 1);
  148. if (size < 0)
  149. return size;
  150. size += sizeof(*msg) + mex->inputdatalength;
  151. /* message header, cprbx and f&r */
  152. msg->hdr = static_type6_hdrX;
  153. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  154. msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  155. msg->cprbx = static_cprbx;
  156. msg->cprbx.domain = AP_QID_QUEUE(zdev->ap_dev->qid);
  157. msg->cprbx.rpl_msgbl = msg->hdr.FromCardLen1;
  158. msg->fr = (zdev->user_space_type == ZCRYPT_PCIXCC_MCL2) ?
  159. static_pke_fnr_MCL2 : static_pke_fnr;
  160. msg->cprbx.req_parml = size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  161. ap_msg->length = size;
  162. return 0;
  163. }
  164. /**
  165. * Convert a ICACRT message to a type6 CRT message.
  166. *
  167. * @zdev: crypto device pointer
  168. * @ap_msg: pointer to AP message
  169. * @crt: pointer to user input data
  170. *
  171. * Returns 0 on success or -EFAULT.
  172. */
  173. static int ICACRT_msg_to_type6CRT_msgX(struct zcrypt_device *zdev,
  174. struct ap_message *ap_msg,
  175. struct ica_rsa_modexpo_crt *crt)
  176. {
  177. static struct type6_hdr static_type6_hdrX = {
  178. .type = 0x06,
  179. .offset1 = 0x00000058,
  180. .agent_id = {'C','A',},
  181. .function_code = {'P','D'},
  182. };
  183. static struct function_and_rules_block static_pkd_fnr = {
  184. .function_code = {'P','D'},
  185. .ulen = 10,
  186. .only_rule = {'Z','E','R','O','-','P','A','D'}
  187. };
  188. static struct function_and_rules_block static_pkd_fnr_MCL2 = {
  189. .function_code = {'P','D'},
  190. .ulen = 10,
  191. .only_rule = {'P','K','C','S','-','1','.','2'}
  192. };
  193. struct {
  194. struct type6_hdr hdr;
  195. struct CPRBX cprbx;
  196. struct function_and_rules_block fr;
  197. unsigned short length;
  198. char text[0];
  199. } __attribute__((packed)) *msg = ap_msg->message;
  200. int size;
  201. /* VUD.ciphertext */
  202. msg->length = crt->inputdatalength + 2;
  203. if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
  204. return -EFAULT;
  205. /* Set up key which is located after the variable length text. */
  206. size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength, 1);
  207. if (size < 0)
  208. return size;
  209. size += sizeof(*msg) + crt->inputdatalength; /* total size of msg */
  210. /* message header, cprbx and f&r */
  211. msg->hdr = static_type6_hdrX;
  212. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  213. msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  214. msg->cprbx = static_cprbx;
  215. msg->cprbx.domain = AP_QID_QUEUE(zdev->ap_dev->qid);
  216. msg->cprbx.req_parml = msg->cprbx.rpl_msgbl =
  217. size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  218. msg->fr = (zdev->user_space_type == ZCRYPT_PCIXCC_MCL2) ?
  219. static_pkd_fnr_MCL2 : static_pkd_fnr;
  220. ap_msg->length = size;
  221. return 0;
  222. }
  223. /**
  224. * Convert a XCRB message to a type6 CPRB message.
  225. *
  226. * @zdev: crypto device pointer
  227. * @ap_msg: pointer to AP message
  228. * @xcRB: pointer to user input data
  229. *
  230. * Returns 0 on success or -EFAULT.
  231. */
  232. struct type86_fmt2_msg {
  233. struct type86_hdr hdr;
  234. struct type86_fmt2_ext fmt2;
  235. } __attribute__((packed));
  236. static int XCRB_msg_to_type6CPRB_msgX(struct zcrypt_device *zdev,
  237. struct ap_message *ap_msg,
  238. struct ica_xcRB *xcRB)
  239. {
  240. static struct type6_hdr static_type6_hdrX = {
  241. .type = 0x06,
  242. .offset1 = 0x00000058,
  243. };
  244. struct {
  245. struct type6_hdr hdr;
  246. struct ica_CPRBX cprbx;
  247. } __attribute__((packed)) *msg = ap_msg->message;
  248. int rcblen = CEIL4(xcRB->request_control_blk_length);
  249. int replylen;
  250. char *req_data = ap_msg->message + sizeof(struct type6_hdr) + rcblen;
  251. char *function_code;
  252. /* length checks */
  253. ap_msg->length = sizeof(struct type6_hdr) +
  254. CEIL4(xcRB->request_control_blk_length) +
  255. xcRB->request_data_length;
  256. if (ap_msg->length > PCIXCC_MAX_XCRB_MESSAGE_SIZE) {
  257. PRINTK("Combined message is too large (%ld/%d/%d).\n",
  258. sizeof(struct type6_hdr),
  259. xcRB->request_control_blk_length,
  260. xcRB->request_data_length);
  261. return -EFAULT;
  262. }
  263. if (CEIL4(xcRB->reply_control_blk_length) >
  264. PCIXCC_MAX_XCRB_REPLY_SIZE) {
  265. PDEBUG("Reply CPRB length is too large (%d).\n",
  266. xcRB->request_control_blk_length);
  267. return -EFAULT;
  268. }
  269. if (CEIL4(xcRB->reply_data_length) > PCIXCC_MAX_XCRB_DATA_SIZE) {
  270. PDEBUG("Reply data block length is too large (%d).\n",
  271. xcRB->reply_data_length);
  272. return -EFAULT;
  273. }
  274. replylen = CEIL4(xcRB->reply_control_blk_length) +
  275. CEIL4(xcRB->reply_data_length) +
  276. sizeof(struct type86_fmt2_msg);
  277. if (replylen > PCIXCC_MAX_XCRB_RESPONSE_SIZE) {
  278. PDEBUG("Reply CPRB + data block > PCIXCC_MAX_XCRB_RESPONSE_SIZE"
  279. " (%d/%d/%d).\n",
  280. sizeof(struct type86_fmt2_msg),
  281. xcRB->reply_control_blk_length,
  282. xcRB->reply_data_length);
  283. xcRB->reply_control_blk_length = PCIXCC_MAX_XCRB_RESPONSE_SIZE -
  284. (sizeof(struct type86_fmt2_msg) +
  285. CEIL4(xcRB->reply_data_length));
  286. PDEBUG("Capping Reply CPRB length at %d\n",
  287. xcRB->reply_control_blk_length);
  288. }
  289. /* prepare type6 header */
  290. msg->hdr = static_type6_hdrX;
  291. memcpy(msg->hdr.agent_id , &(xcRB->agent_ID), sizeof(xcRB->agent_ID));
  292. msg->hdr.ToCardLen1 = xcRB->request_control_blk_length;
  293. if (xcRB->request_data_length) {
  294. msg->hdr.offset2 = msg->hdr.offset1 + rcblen;
  295. msg->hdr.ToCardLen2 = xcRB->request_data_length;
  296. }
  297. msg->hdr.FromCardLen1 = xcRB->reply_control_blk_length;
  298. msg->hdr.FromCardLen2 = xcRB->reply_data_length;
  299. /* prepare CPRB */
  300. if (copy_from_user(&(msg->cprbx), xcRB->request_control_blk_addr,
  301. xcRB->request_control_blk_length))
  302. return -EFAULT;
  303. if (msg->cprbx.cprb_len + sizeof(msg->hdr.function_code) >
  304. xcRB->request_control_blk_length) {
  305. PDEBUG("cprb_len too large (%d/%d)\n", msg->cprbx.cprb_len,
  306. xcRB->request_control_blk_length);
  307. return -EFAULT;
  308. }
  309. function_code = ((unsigned char *)&msg->cprbx) + msg->cprbx.cprb_len;
  310. memcpy(msg->hdr.function_code, function_code, sizeof(msg->hdr.function_code));
  311. /* copy data block */
  312. if (xcRB->request_data_length &&
  313. copy_from_user(req_data, xcRB->request_data_address,
  314. xcRB->request_data_length))
  315. return -EFAULT;
  316. return 0;
  317. }
  318. /**
  319. * Copy results from a type 86 ICA reply message back to user space.
  320. *
  321. * @zdev: crypto device pointer
  322. * @reply: reply AP message.
  323. * @data: pointer to user output data
  324. * @length: size of user output data
  325. *
  326. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  327. */
  328. struct type86x_reply {
  329. struct type86_hdr hdr;
  330. struct type86_fmt2_ext fmt2;
  331. struct CPRBX cprbx;
  332. unsigned char pad[4]; /* 4 byte function code/rules block ? */
  333. unsigned short length;
  334. char text[0];
  335. } __attribute__((packed));
  336. static int convert_type86_ica(struct zcrypt_device *zdev,
  337. struct ap_message *reply,
  338. char __user *outputdata,
  339. unsigned int outputdatalength)
  340. {
  341. static unsigned char static_pad[] = {
  342. 0x00,0x02,
  343. 0x1B,0x7B,0x5D,0xB5,0x75,0x01,0x3D,0xFD,
  344. 0x8D,0xD1,0xC7,0x03,0x2D,0x09,0x23,0x57,
  345. 0x89,0x49,0xB9,0x3F,0xBB,0x99,0x41,0x5B,
  346. 0x75,0x21,0x7B,0x9D,0x3B,0x6B,0x51,0x39,
  347. 0xBB,0x0D,0x35,0xB9,0x89,0x0F,0x93,0xA5,
  348. 0x0B,0x47,0xF1,0xD3,0xBB,0xCB,0xF1,0x9D,
  349. 0x23,0x73,0x71,0xFF,0xF3,0xF5,0x45,0xFB,
  350. 0x61,0x29,0x23,0xFD,0xF1,0x29,0x3F,0x7F,
  351. 0x17,0xB7,0x1B,0xA9,0x19,0xBD,0x57,0xA9,
  352. 0xD7,0x95,0xA3,0xCB,0xED,0x1D,0xDB,0x45,
  353. 0x7D,0x11,0xD1,0x51,0x1B,0xED,0x71,0xE9,
  354. 0xB1,0xD1,0xAB,0xAB,0x21,0x2B,0x1B,0x9F,
  355. 0x3B,0x9F,0xF7,0xF7,0xBD,0x63,0xEB,0xAD,
  356. 0xDF,0xB3,0x6F,0x5B,0xDB,0x8D,0xA9,0x5D,
  357. 0xE3,0x7D,0x77,0x49,0x47,0xF5,0xA7,0xFD,
  358. 0xAB,0x2F,0x27,0x35,0x77,0xD3,0x49,0xC9,
  359. 0x09,0xEB,0xB1,0xF9,0xBF,0x4B,0xCB,0x2B,
  360. 0xEB,0xEB,0x05,0xFF,0x7D,0xC7,0x91,0x8B,
  361. 0x09,0x83,0xB9,0xB9,0x69,0x33,0x39,0x6B,
  362. 0x79,0x75,0x19,0xBF,0xBB,0x07,0x1D,0xBD,
  363. 0x29,0xBF,0x39,0x95,0x93,0x1D,0x35,0xC7,
  364. 0xC9,0x4D,0xE5,0x97,0x0B,0x43,0x9B,0xF1,
  365. 0x16,0x93,0x03,0x1F,0xA5,0xFB,0xDB,0xF3,
  366. 0x27,0x4F,0x27,0x61,0x05,0x1F,0xB9,0x23,
  367. 0x2F,0xC3,0x81,0xA9,0x23,0x71,0x55,0x55,
  368. 0xEB,0xED,0x41,0xE5,0xF3,0x11,0xF1,0x43,
  369. 0x69,0x03,0xBD,0x0B,0x37,0x0F,0x51,0x8F,
  370. 0x0B,0xB5,0x89,0x5B,0x67,0xA9,0xD9,0x4F,
  371. 0x01,0xF9,0x21,0x77,0x37,0x73,0x79,0xC5,
  372. 0x7F,0x51,0xC1,0xCF,0x97,0xA1,0x75,0xAD,
  373. 0x35,0x9D,0xD3,0xD3,0xA7,0x9D,0x5D,0x41,
  374. 0x6F,0x65,0x1B,0xCF,0xA9,0x87,0x91,0x09
  375. };
  376. struct type86x_reply *msg = reply->message;
  377. unsigned short service_rc, service_rs;
  378. unsigned int reply_len, pad_len;
  379. char *data;
  380. service_rc = msg->cprbx.ccp_rtcode;
  381. if (unlikely(service_rc != 0)) {
  382. service_rs = msg->cprbx.ccp_rscode;
  383. if (service_rc == 8 && service_rs == 66) {
  384. PDEBUG("Bad block format on PCIXCC/CEX2C\n");
  385. return -EINVAL;
  386. }
  387. if (service_rc == 8 && service_rs == 65) {
  388. PDEBUG("Probably an even modulus on PCIXCC/CEX2C\n");
  389. return -EINVAL;
  390. }
  391. if (service_rc == 8 && service_rs == 770) {
  392. PDEBUG("Invalid key length on PCIXCC/CEX2C\n");
  393. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  394. return -EAGAIN;
  395. }
  396. if (service_rc == 8 && service_rs == 783) {
  397. PDEBUG("Extended bitlengths not enabled on PCIXCC/CEX2C\n");
  398. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  399. return -EAGAIN;
  400. }
  401. PRINTK("Unknown service rc/rs (PCIXCC/CEX2C): %d/%d\n",
  402. service_rc, service_rs);
  403. zdev->online = 0;
  404. return -EAGAIN; /* repeat the request on a different device. */
  405. }
  406. data = msg->text;
  407. reply_len = msg->length - 2;
  408. if (reply_len > outputdatalength)
  409. return -EINVAL;
  410. /**
  411. * For all encipher requests, the length of the ciphertext (reply_len)
  412. * will always equal the modulus length. For MEX decipher requests
  413. * the output needs to get padded. Minimum pad size is 10.
  414. *
  415. * Currently, the cases where padding will be added is for:
  416. * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
  417. * ZERO-PAD and CRT is only supported for PKD requests)
  418. * - PCICC, always
  419. */
  420. pad_len = outputdatalength - reply_len;
  421. if (pad_len > 0) {
  422. if (pad_len < 10)
  423. return -EINVAL;
  424. /* 'restore' padding left in the PCICC/PCIXCC card. */
  425. if (copy_to_user(outputdata, static_pad, pad_len - 1))
  426. return -EFAULT;
  427. if (put_user(0, outputdata + pad_len - 1))
  428. return -EFAULT;
  429. }
  430. /* Copy the crypto response to user space. */
  431. if (copy_to_user(outputdata + pad_len, data, reply_len))
  432. return -EFAULT;
  433. return 0;
  434. }
  435. /**
  436. * Copy results from a type 86 XCRB reply message back to user space.
  437. *
  438. * @zdev: crypto device pointer
  439. * @reply: reply AP message.
  440. * @xcRB: pointer to XCRB
  441. *
  442. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  443. */
  444. static int convert_type86_xcrb(struct zcrypt_device *zdev,
  445. struct ap_message *reply,
  446. struct ica_xcRB *xcRB)
  447. {
  448. struct type86_fmt2_msg *msg = reply->message;
  449. char *data = reply->message;
  450. /* Copy CPRB to user */
  451. if (copy_to_user(xcRB->reply_control_blk_addr,
  452. data + msg->fmt2.offset1, msg->fmt2.count1))
  453. return -EFAULT;
  454. xcRB->reply_control_blk_length = msg->fmt2.count1;
  455. /* Copy data buffer to user */
  456. if (msg->fmt2.count2)
  457. if (copy_to_user(xcRB->reply_data_addr,
  458. data + msg->fmt2.offset2, msg->fmt2.count2))
  459. return -EFAULT;
  460. xcRB->reply_data_length = msg->fmt2.count2;
  461. return 0;
  462. }
  463. static int convert_response_ica(struct zcrypt_device *zdev,
  464. struct ap_message *reply,
  465. char __user *outputdata,
  466. unsigned int outputdatalength)
  467. {
  468. struct type86x_reply *msg = reply->message;
  469. /* Response type byte is the second byte in the response. */
  470. switch (((unsigned char *) reply->message)[1]) {
  471. case TYPE82_RSP_CODE:
  472. case TYPE88_RSP_CODE:
  473. return convert_error(zdev, reply);
  474. case TYPE86_RSP_CODE:
  475. if (msg->hdr.reply_code)
  476. return convert_error(zdev, reply);
  477. if (msg->cprbx.cprb_ver_id == 0x02)
  478. return convert_type86_ica(zdev, reply,
  479. outputdata, outputdatalength);
  480. /* no break, incorrect cprb version is an unknown response */
  481. default: /* Unknown response type, this should NEVER EVER happen */
  482. PRINTK("Unrecognized Message Header: %08x%08x\n",
  483. *(unsigned int *) reply->message,
  484. *(unsigned int *) (reply->message+4));
  485. zdev->online = 0;
  486. return -EAGAIN; /* repeat the request on a different device. */
  487. }
  488. }
  489. static int convert_response_xcrb(struct zcrypt_device *zdev,
  490. struct ap_message *reply,
  491. struct ica_xcRB *xcRB)
  492. {
  493. struct type86x_reply *msg = reply->message;
  494. /* Response type byte is the second byte in the response. */
  495. switch (((unsigned char *) reply->message)[1]) {
  496. case TYPE82_RSP_CODE:
  497. case TYPE88_RSP_CODE:
  498. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  499. return convert_error(zdev, reply);
  500. case TYPE86_RSP_CODE:
  501. if (msg->hdr.reply_code) {
  502. memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
  503. return convert_error(zdev, reply);
  504. }
  505. if (msg->cprbx.cprb_ver_id == 0x02)
  506. return convert_type86_xcrb(zdev, reply, xcRB);
  507. /* no break, incorrect cprb version is an unknown response */
  508. default: /* Unknown response type, this should NEVER EVER happen */
  509. PRINTK("Unrecognized Message Header: %08x%08x\n",
  510. *(unsigned int *) reply->message,
  511. *(unsigned int *) (reply->message+4));
  512. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  513. zdev->online = 0;
  514. return -EAGAIN; /* repeat the request on a different device. */
  515. }
  516. }
  517. /**
  518. * This function is called from the AP bus code after a crypto request
  519. * "msg" has finished with the reply message "reply".
  520. * It is called from tasklet context.
  521. * @ap_dev: pointer to the AP device
  522. * @msg: pointer to the AP message
  523. * @reply: pointer to the AP reply message
  524. */
  525. static void zcrypt_pcixcc_receive(struct ap_device *ap_dev,
  526. struct ap_message *msg,
  527. struct ap_message *reply)
  528. {
  529. static struct error_hdr error_reply = {
  530. .type = TYPE82_RSP_CODE,
  531. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  532. };
  533. struct response_type *resp_type =
  534. (struct response_type *) msg->private;
  535. struct type86x_reply *t86r = reply->message;
  536. int length;
  537. /* Copy the reply message to the request message buffer. */
  538. if (IS_ERR(reply))
  539. memcpy(msg->message, &error_reply, sizeof(error_reply));
  540. else if (t86r->hdr.type == TYPE86_RSP_CODE &&
  541. t86r->cprbx.cprb_ver_id == 0x02) {
  542. switch (resp_type->type) {
  543. case PCIXCC_RESPONSE_TYPE_ICA:
  544. length = sizeof(struct type86x_reply)
  545. + t86r->length - 2;
  546. length = min(PCIXCC_MAX_ICA_RESPONSE_SIZE, length);
  547. memcpy(msg->message, reply->message, length);
  548. break;
  549. case PCIXCC_RESPONSE_TYPE_XCRB:
  550. length = t86r->fmt2.offset2 + t86r->fmt2.count2;
  551. length = min(PCIXCC_MAX_XCRB_RESPONSE_SIZE, length);
  552. memcpy(msg->message, reply->message, length);
  553. break;
  554. default:
  555. PRINTK("Invalid internal response type: %i\n",
  556. resp_type->type);
  557. memcpy(msg->message, &error_reply,
  558. sizeof error_reply);
  559. }
  560. } else
  561. memcpy(msg->message, reply->message, sizeof error_reply);
  562. complete(&(resp_type->work));
  563. }
  564. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  565. /**
  566. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  567. * device to handle a modexpo request.
  568. * @zdev: pointer to zcrypt_device structure that identifies the
  569. * PCIXCC/CEX2C device to the request distributor
  570. * @mex: pointer to the modexpo request buffer
  571. */
  572. static long zcrypt_pcixcc_modexpo(struct zcrypt_device *zdev,
  573. struct ica_rsa_modexpo *mex)
  574. {
  575. struct ap_message ap_msg;
  576. struct response_type resp_type = {
  577. .type = PCIXCC_RESPONSE_TYPE_ICA,
  578. };
  579. int rc;
  580. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  581. if (!ap_msg.message)
  582. return -ENOMEM;
  583. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  584. atomic_inc_return(&zcrypt_step);
  585. ap_msg.private = &resp_type;
  586. rc = ICAMEX_msg_to_type6MEX_msgX(zdev, &ap_msg, mex);
  587. if (rc)
  588. goto out_free;
  589. init_completion(&resp_type.work);
  590. ap_queue_message(zdev->ap_dev, &ap_msg);
  591. rc = wait_for_completion_interruptible_timeout(
  592. &resp_type.work, PCIXCC_CLEANUP_TIME);
  593. if (rc > 0)
  594. rc = convert_response_ica(zdev, &ap_msg, mex->outputdata,
  595. mex->outputdatalength);
  596. else {
  597. /* Signal pending or message timed out. */
  598. ap_cancel_message(zdev->ap_dev, &ap_msg);
  599. if (rc == 0)
  600. /* Message timed out. */
  601. rc = -ETIME;
  602. }
  603. out_free:
  604. free_page((unsigned long) ap_msg.message);
  605. return rc;
  606. }
  607. /**
  608. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  609. * device to handle a modexpo_crt request.
  610. * @zdev: pointer to zcrypt_device structure that identifies the
  611. * PCIXCC/CEX2C device to the request distributor
  612. * @crt: pointer to the modexpoc_crt request buffer
  613. */
  614. static long zcrypt_pcixcc_modexpo_crt(struct zcrypt_device *zdev,
  615. struct ica_rsa_modexpo_crt *crt)
  616. {
  617. struct ap_message ap_msg;
  618. struct response_type resp_type = {
  619. .type = PCIXCC_RESPONSE_TYPE_ICA,
  620. };
  621. int rc;
  622. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  623. if (!ap_msg.message)
  624. return -ENOMEM;
  625. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  626. atomic_inc_return(&zcrypt_step);
  627. ap_msg.private = &resp_type;
  628. rc = ICACRT_msg_to_type6CRT_msgX(zdev, &ap_msg, crt);
  629. if (rc)
  630. goto out_free;
  631. init_completion(&resp_type.work);
  632. ap_queue_message(zdev->ap_dev, &ap_msg);
  633. rc = wait_for_completion_interruptible_timeout(
  634. &resp_type.work, PCIXCC_CLEANUP_TIME);
  635. if (rc > 0)
  636. rc = convert_response_ica(zdev, &ap_msg, crt->outputdata,
  637. crt->outputdatalength);
  638. else {
  639. /* Signal pending or message timed out. */
  640. ap_cancel_message(zdev->ap_dev, &ap_msg);
  641. if (rc == 0)
  642. /* Message timed out. */
  643. rc = -ETIME;
  644. }
  645. out_free:
  646. free_page((unsigned long) ap_msg.message);
  647. return rc;
  648. }
  649. /**
  650. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  651. * device to handle a send_cprb request.
  652. * @zdev: pointer to zcrypt_device structure that identifies the
  653. * PCIXCC/CEX2C device to the request distributor
  654. * @xcRB: pointer to the send_cprb request buffer
  655. */
  656. long zcrypt_pcixcc_send_cprb(struct zcrypt_device *zdev, struct ica_xcRB *xcRB)
  657. {
  658. struct ap_message ap_msg;
  659. struct response_type resp_type = {
  660. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  661. };
  662. int rc;
  663. ap_msg.message = (void *) kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  664. if (!ap_msg.message)
  665. return -ENOMEM;
  666. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  667. atomic_inc_return(&zcrypt_step);
  668. ap_msg.private = &resp_type;
  669. rc = XCRB_msg_to_type6CPRB_msgX(zdev, &ap_msg, xcRB);
  670. if (rc)
  671. goto out_free;
  672. init_completion(&resp_type.work);
  673. ap_queue_message(zdev->ap_dev, &ap_msg);
  674. rc = wait_for_completion_interruptible_timeout(
  675. &resp_type.work, PCIXCC_CLEANUP_TIME);
  676. if (rc > 0)
  677. rc = convert_response_xcrb(zdev, &ap_msg, xcRB);
  678. else {
  679. /* Signal pending or message timed out. */
  680. ap_cancel_message(zdev->ap_dev, &ap_msg);
  681. if (rc == 0)
  682. /* Message timed out. */
  683. rc = -ETIME;
  684. }
  685. out_free:
  686. memset(ap_msg.message, 0x0, ap_msg.length);
  687. kfree(ap_msg.message);
  688. return rc;
  689. }
  690. /**
  691. * The crypto operations for a PCIXCC/CEX2C card.
  692. */
  693. static struct zcrypt_ops zcrypt_pcixcc_ops = {
  694. .rsa_modexpo = zcrypt_pcixcc_modexpo,
  695. .rsa_modexpo_crt = zcrypt_pcixcc_modexpo_crt,
  696. .send_cprb = zcrypt_pcixcc_send_cprb,
  697. };
  698. /**
  699. * Micro-code detection function. Its sends a message to a pcixcc card
  700. * to find out the microcode level.
  701. * @ap_dev: pointer to the AP device.
  702. */
  703. static int zcrypt_pcixcc_mcl(struct ap_device *ap_dev)
  704. {
  705. static unsigned char msg[] = {
  706. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  707. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  708. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  709. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  710. 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
  711. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  712. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00,
  713. 0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00,
  714. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  715. 0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00,
  716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  717. 0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32,
  718. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8,
  719. 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,
  720. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  721. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  722. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  723. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  724. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  725. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  726. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  727. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  728. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  729. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  730. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  731. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  732. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  733. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  734. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  735. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  736. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  737. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  738. 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
  739. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  740. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  741. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  742. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  743. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  744. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x0A,
  745. 0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20,
  746. 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05,
  747. 0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
  748. 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55,
  749. 0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,
  750. 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,
  751. 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,
  752. 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,
  753. 0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54,
  754. 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00,
  755. 0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00,
  756. 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40,
  757. 0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C,
  758. 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF,
  759. 0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9,
  760. 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63,
  761. 0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5,
  762. 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A,
  763. 0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01,
  764. 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28,
  765. 0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91,
  766. 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5,
  767. 0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C,
  768. 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98,
  769. 0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96,
  770. 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19,
  771. 0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47,
  772. 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36,
  773. 0xF1,0x3D,0x93,0x53
  774. };
  775. unsigned long long psmid;
  776. struct CPRBX *cprbx;
  777. char *reply;
  778. int rc, i;
  779. reply = (void *) get_zeroed_page(GFP_KERNEL);
  780. if (!reply)
  781. return -ENOMEM;
  782. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, msg, sizeof(msg));
  783. if (rc)
  784. goto out_free;
  785. /* Wait for the test message to complete. */
  786. for (i = 0; i < 6; i++) {
  787. mdelay(300);
  788. rc = ap_recv(ap_dev->qid, &psmid, reply, 4096);
  789. if (rc == 0 && psmid == 0x0102030405060708ULL)
  790. break;
  791. }
  792. if (i >= 6) {
  793. /* Got no answer. */
  794. rc = -ENODEV;
  795. goto out_free;
  796. }
  797. cprbx = (struct CPRBX *) (reply + 48);
  798. if (cprbx->ccp_rtcode == 8 && cprbx->ccp_rscode == 33)
  799. rc = ZCRYPT_PCIXCC_MCL2;
  800. else
  801. rc = ZCRYPT_PCIXCC_MCL3;
  802. out_free:
  803. free_page((unsigned long) reply);
  804. return rc;
  805. }
  806. /**
  807. * Probe function for PCIXCC/CEX2C cards. It always accepts the AP device
  808. * since the bus_match already checked the hardware type. The PCIXCC
  809. * cards come in two flavours: micro code level 2 and micro code level 3.
  810. * This is checked by sending a test message to the device.
  811. * @ap_dev: pointer to the AP device.
  812. */
  813. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev)
  814. {
  815. struct zcrypt_device *zdev;
  816. int rc;
  817. zdev = zcrypt_device_alloc(PCIXCC_MAX_RESPONSE_SIZE);
  818. if (!zdev)
  819. return -ENOMEM;
  820. zdev->ap_dev = ap_dev;
  821. zdev->ops = &zcrypt_pcixcc_ops;
  822. zdev->online = 1;
  823. if (ap_dev->device_type == AP_DEVICE_TYPE_PCIXCC) {
  824. rc = zcrypt_pcixcc_mcl(ap_dev);
  825. if (rc < 0) {
  826. zcrypt_device_free(zdev);
  827. return rc;
  828. }
  829. zdev->user_space_type = rc;
  830. if (rc == ZCRYPT_PCIXCC_MCL2) {
  831. zdev->type_string = "PCIXCC_MCL2";
  832. zdev->speed_rating = PCIXCC_MCL2_SPEED_RATING;
  833. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  834. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  835. } else {
  836. zdev->type_string = "PCIXCC_MCL3";
  837. zdev->speed_rating = PCIXCC_MCL3_SPEED_RATING;
  838. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  839. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  840. }
  841. } else {
  842. zdev->user_space_type = ZCRYPT_CEX2C;
  843. zdev->type_string = "CEX2C";
  844. zdev->speed_rating = CEX2C_SPEED_RATING;
  845. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  846. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  847. }
  848. ap_dev->reply = &zdev->reply;
  849. ap_dev->private = zdev;
  850. rc = zcrypt_device_register(zdev);
  851. if (rc)
  852. goto out_free;
  853. return 0;
  854. out_free:
  855. ap_dev->private = NULL;
  856. zcrypt_device_free(zdev);
  857. return rc;
  858. }
  859. /**
  860. * This is called to remove the extended PCIXCC/CEX2C driver information
  861. * if an AP device is removed.
  862. */
  863. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev)
  864. {
  865. struct zcrypt_device *zdev = ap_dev->private;
  866. zcrypt_device_unregister(zdev);
  867. }
  868. int __init zcrypt_pcixcc_init(void)
  869. {
  870. return ap_driver_register(&zcrypt_pcixcc_driver, THIS_MODULE, "pcixcc");
  871. }
  872. void zcrypt_pcixcc_exit(void)
  873. {
  874. ap_driver_unregister(&zcrypt_pcixcc_driver);
  875. }
  876. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  877. module_init(zcrypt_pcixcc_init);
  878. module_exit(zcrypt_pcixcc_exit);
  879. #endif