zcrypt_pcixcc.c 33 KB

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