zcrypt_pcixcc.c 33 KB

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