zcrypt_pcixcc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. if (memcmp(function_code, "US", 2) == 0)
  292. ap_msg->special = 1;
  293. else
  294. ap_msg->special = 0;
  295. /* copy data block */
  296. if (xcRB->request_data_length &&
  297. copy_from_user(req_data, xcRB->request_data_address,
  298. xcRB->request_data_length))
  299. return -EFAULT;
  300. return 0;
  301. }
  302. /**
  303. * Prepare a type6 CPRB message for random number generation
  304. *
  305. * @ap_dev: AP device pointer
  306. * @ap_msg: pointer to AP message
  307. */
  308. static void rng_type6CPRB_msgX(struct ap_device *ap_dev,
  309. struct ap_message *ap_msg,
  310. unsigned random_number_length)
  311. {
  312. struct {
  313. struct type6_hdr hdr;
  314. struct CPRBX cprbx;
  315. char function_code[2];
  316. short int rule_length;
  317. char rule[8];
  318. short int verb_length;
  319. short int key_length;
  320. } __attribute__((packed)) *msg = ap_msg->message;
  321. static struct type6_hdr static_type6_hdrX = {
  322. .type = 0x06,
  323. .offset1 = 0x00000058,
  324. .agent_id = {'C', 'A'},
  325. .function_code = {'R', 'L'},
  326. .ToCardLen1 = sizeof *msg - sizeof(msg->hdr),
  327. .FromCardLen1 = sizeof *msg - sizeof(msg->hdr),
  328. };
  329. static struct CPRBX local_cprbx = {
  330. .cprb_len = 0x00dc,
  331. .cprb_ver_id = 0x02,
  332. .func_id = {0x54, 0x32},
  333. .req_parml = sizeof *msg - sizeof(msg->hdr) -
  334. sizeof(msg->cprbx),
  335. .rpl_msgbl = sizeof *msg - sizeof(msg->hdr),
  336. };
  337. msg->hdr = static_type6_hdrX;
  338. msg->hdr.FromCardLen2 = random_number_length,
  339. msg->cprbx = local_cprbx;
  340. msg->cprbx.rpl_datal = random_number_length,
  341. msg->cprbx.domain = AP_QID_QUEUE(ap_dev->qid);
  342. memcpy(msg->function_code, msg->hdr.function_code, 0x02);
  343. msg->rule_length = 0x0a;
  344. memcpy(msg->rule, "RANDOM ", 8);
  345. msg->verb_length = 0x02;
  346. msg->key_length = 0x02;
  347. ap_msg->length = sizeof *msg;
  348. }
  349. /**
  350. * Copy results from a type 86 ICA reply message back to user space.
  351. *
  352. * @zdev: crypto device pointer
  353. * @reply: reply AP message.
  354. * @data: pointer to user output data
  355. * @length: size of user output data
  356. *
  357. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  358. */
  359. struct type86x_reply {
  360. struct type86_hdr hdr;
  361. struct type86_fmt2_ext fmt2;
  362. struct CPRBX cprbx;
  363. unsigned char pad[4]; /* 4 byte function code/rules block ? */
  364. unsigned short length;
  365. char text[0];
  366. } __attribute__((packed));
  367. static int convert_type86_ica(struct zcrypt_device *zdev,
  368. struct ap_message *reply,
  369. char __user *outputdata,
  370. unsigned int outputdatalength)
  371. {
  372. static unsigned char static_pad[] = {
  373. 0x00,0x02,
  374. 0x1B,0x7B,0x5D,0xB5,0x75,0x01,0x3D,0xFD,
  375. 0x8D,0xD1,0xC7,0x03,0x2D,0x09,0x23,0x57,
  376. 0x89,0x49,0xB9,0x3F,0xBB,0x99,0x41,0x5B,
  377. 0x75,0x21,0x7B,0x9D,0x3B,0x6B,0x51,0x39,
  378. 0xBB,0x0D,0x35,0xB9,0x89,0x0F,0x93,0xA5,
  379. 0x0B,0x47,0xF1,0xD3,0xBB,0xCB,0xF1,0x9D,
  380. 0x23,0x73,0x71,0xFF,0xF3,0xF5,0x45,0xFB,
  381. 0x61,0x29,0x23,0xFD,0xF1,0x29,0x3F,0x7F,
  382. 0x17,0xB7,0x1B,0xA9,0x19,0xBD,0x57,0xA9,
  383. 0xD7,0x95,0xA3,0xCB,0xED,0x1D,0xDB,0x45,
  384. 0x7D,0x11,0xD1,0x51,0x1B,0xED,0x71,0xE9,
  385. 0xB1,0xD1,0xAB,0xAB,0x21,0x2B,0x1B,0x9F,
  386. 0x3B,0x9F,0xF7,0xF7,0xBD,0x63,0xEB,0xAD,
  387. 0xDF,0xB3,0x6F,0x5B,0xDB,0x8D,0xA9,0x5D,
  388. 0xE3,0x7D,0x77,0x49,0x47,0xF5,0xA7,0xFD,
  389. 0xAB,0x2F,0x27,0x35,0x77,0xD3,0x49,0xC9,
  390. 0x09,0xEB,0xB1,0xF9,0xBF,0x4B,0xCB,0x2B,
  391. 0xEB,0xEB,0x05,0xFF,0x7D,0xC7,0x91,0x8B,
  392. 0x09,0x83,0xB9,0xB9,0x69,0x33,0x39,0x6B,
  393. 0x79,0x75,0x19,0xBF,0xBB,0x07,0x1D,0xBD,
  394. 0x29,0xBF,0x39,0x95,0x93,0x1D,0x35,0xC7,
  395. 0xC9,0x4D,0xE5,0x97,0x0B,0x43,0x9B,0xF1,
  396. 0x16,0x93,0x03,0x1F,0xA5,0xFB,0xDB,0xF3,
  397. 0x27,0x4F,0x27,0x61,0x05,0x1F,0xB9,0x23,
  398. 0x2F,0xC3,0x81,0xA9,0x23,0x71,0x55,0x55,
  399. 0xEB,0xED,0x41,0xE5,0xF3,0x11,0xF1,0x43,
  400. 0x69,0x03,0xBD,0x0B,0x37,0x0F,0x51,0x8F,
  401. 0x0B,0xB5,0x89,0x5B,0x67,0xA9,0xD9,0x4F,
  402. 0x01,0xF9,0x21,0x77,0x37,0x73,0x79,0xC5,
  403. 0x7F,0x51,0xC1,0xCF,0x97,0xA1,0x75,0xAD,
  404. 0x35,0x9D,0xD3,0xD3,0xA7,0x9D,0x5D,0x41,
  405. 0x6F,0x65,0x1B,0xCF,0xA9,0x87,0x91,0x09
  406. };
  407. struct type86x_reply *msg = reply->message;
  408. unsigned short service_rc, service_rs;
  409. unsigned int reply_len, pad_len;
  410. char *data;
  411. service_rc = msg->cprbx.ccp_rtcode;
  412. if (unlikely(service_rc != 0)) {
  413. service_rs = msg->cprbx.ccp_rscode;
  414. if (service_rc == 8 && service_rs == 66)
  415. return -EINVAL;
  416. if (service_rc == 8 && service_rs == 65)
  417. return -EINVAL;
  418. if (service_rc == 8 && service_rs == 770)
  419. return -EINVAL;
  420. if (service_rc == 8 && service_rs == 783) {
  421. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  422. return -EAGAIN;
  423. }
  424. if (service_rc == 12 && service_rs == 769)
  425. return -EINVAL;
  426. zdev->online = 0;
  427. return -EAGAIN; /* repeat the request on a different device. */
  428. }
  429. data = msg->text;
  430. reply_len = msg->length - 2;
  431. if (reply_len > outputdatalength)
  432. return -EINVAL;
  433. /*
  434. * For all encipher requests, the length of the ciphertext (reply_len)
  435. * will always equal the modulus length. For MEX decipher requests
  436. * the output needs to get padded. Minimum pad size is 10.
  437. *
  438. * Currently, the cases where padding will be added is for:
  439. * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
  440. * ZERO-PAD and CRT is only supported for PKD requests)
  441. * - PCICC, always
  442. */
  443. pad_len = outputdatalength - reply_len;
  444. if (pad_len > 0) {
  445. if (pad_len < 10)
  446. return -EINVAL;
  447. /* 'restore' padding left in the PCICC/PCIXCC card. */
  448. if (copy_to_user(outputdata, static_pad, pad_len - 1))
  449. return -EFAULT;
  450. if (put_user(0, outputdata + pad_len - 1))
  451. return -EFAULT;
  452. }
  453. /* Copy the crypto response to user space. */
  454. if (copy_to_user(outputdata + pad_len, data, reply_len))
  455. return -EFAULT;
  456. return 0;
  457. }
  458. /**
  459. * Copy results from a type 86 XCRB reply message back to user space.
  460. *
  461. * @zdev: crypto device pointer
  462. * @reply: reply AP message.
  463. * @xcRB: pointer to XCRB
  464. *
  465. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  466. */
  467. static int convert_type86_xcrb(struct zcrypt_device *zdev,
  468. struct ap_message *reply,
  469. struct ica_xcRB *xcRB)
  470. {
  471. struct type86_fmt2_msg *msg = reply->message;
  472. char *data = reply->message;
  473. /* Copy CPRB to user */
  474. if (copy_to_user(xcRB->reply_control_blk_addr,
  475. data + msg->fmt2.offset1, msg->fmt2.count1))
  476. return -EFAULT;
  477. xcRB->reply_control_blk_length = msg->fmt2.count1;
  478. /* Copy data buffer to user */
  479. if (msg->fmt2.count2)
  480. if (copy_to_user(xcRB->reply_data_addr,
  481. data + msg->fmt2.offset2, msg->fmt2.count2))
  482. return -EFAULT;
  483. xcRB->reply_data_length = msg->fmt2.count2;
  484. return 0;
  485. }
  486. static int convert_type86_rng(struct zcrypt_device *zdev,
  487. struct ap_message *reply,
  488. char *buffer)
  489. {
  490. struct {
  491. struct type86_hdr hdr;
  492. struct type86_fmt2_ext fmt2;
  493. struct CPRBX cprbx;
  494. } __attribute__((packed)) *msg = reply->message;
  495. char *data = reply->message;
  496. if (msg->cprbx.ccp_rtcode != 0 || msg->cprbx.ccp_rscode != 0)
  497. return -EINVAL;
  498. memcpy(buffer, data + msg->fmt2.offset2, msg->fmt2.count2);
  499. return msg->fmt2.count2;
  500. }
  501. static int convert_response_ica(struct zcrypt_device *zdev,
  502. struct ap_message *reply,
  503. char __user *outputdata,
  504. unsigned int outputdatalength)
  505. {
  506. struct type86x_reply *msg = reply->message;
  507. /* Response type byte is the second byte in the response. */
  508. switch (((unsigned char *) reply->message)[1]) {
  509. case TYPE82_RSP_CODE:
  510. case TYPE88_RSP_CODE:
  511. return convert_error(zdev, reply);
  512. case TYPE86_RSP_CODE:
  513. if (msg->hdr.reply_code)
  514. return convert_error(zdev, reply);
  515. if (msg->cprbx.cprb_ver_id == 0x02)
  516. return convert_type86_ica(zdev, reply,
  517. outputdata, outputdatalength);
  518. /* Fall through, no break, incorrect cprb version is an unknown
  519. * response */
  520. default: /* Unknown response type, this should NEVER EVER happen */
  521. zdev->online = 0;
  522. return -EAGAIN; /* repeat the request on a different device. */
  523. }
  524. }
  525. static int convert_response_xcrb(struct zcrypt_device *zdev,
  526. struct ap_message *reply,
  527. struct ica_xcRB *xcRB)
  528. {
  529. struct type86x_reply *msg = reply->message;
  530. /* Response type byte is the second byte in the response. */
  531. switch (((unsigned char *) reply->message)[1]) {
  532. case TYPE82_RSP_CODE:
  533. case TYPE88_RSP_CODE:
  534. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  535. return convert_error(zdev, reply);
  536. case TYPE86_RSP_CODE:
  537. if (msg->hdr.reply_code) {
  538. memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
  539. return convert_error(zdev, reply);
  540. }
  541. if (msg->cprbx.cprb_ver_id == 0x02)
  542. return convert_type86_xcrb(zdev, reply, xcRB);
  543. /* Fall through, no break, incorrect cprb version is an unknown
  544. * response */
  545. default: /* Unknown response type, this should NEVER EVER happen */
  546. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  547. zdev->online = 0;
  548. return -EAGAIN; /* repeat the request on a different device. */
  549. }
  550. }
  551. static int convert_response_rng(struct zcrypt_device *zdev,
  552. struct ap_message *reply,
  553. char *data)
  554. {
  555. struct type86x_reply *msg = reply->message;
  556. switch (msg->hdr.type) {
  557. case TYPE82_RSP_CODE:
  558. case TYPE88_RSP_CODE:
  559. return -EINVAL;
  560. case TYPE86_RSP_CODE:
  561. if (msg->hdr.reply_code)
  562. return -EINVAL;
  563. if (msg->cprbx.cprb_ver_id == 0x02)
  564. return convert_type86_rng(zdev, reply, data);
  565. /* Fall through, no break, incorrect cprb version is an unknown
  566. * response */
  567. default: /* Unknown response type, this should NEVER EVER happen */
  568. zdev->online = 0;
  569. return -EAGAIN; /* repeat the request on a different device. */
  570. }
  571. }
  572. /**
  573. * This function is called from the AP bus code after a crypto request
  574. * "msg" has finished with the reply message "reply".
  575. * It is called from tasklet context.
  576. * @ap_dev: pointer to the AP device
  577. * @msg: pointer to the AP message
  578. * @reply: pointer to the AP reply message
  579. */
  580. static void zcrypt_pcixcc_receive(struct ap_device *ap_dev,
  581. struct ap_message *msg,
  582. struct ap_message *reply)
  583. {
  584. static struct error_hdr error_reply = {
  585. .type = TYPE82_RSP_CODE,
  586. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  587. };
  588. struct response_type *resp_type =
  589. (struct response_type *) msg->private;
  590. struct type86x_reply *t86r;
  591. int length;
  592. /* Copy the reply message to the request message buffer. */
  593. if (IS_ERR(reply)) {
  594. memcpy(msg->message, &error_reply, sizeof(error_reply));
  595. goto out;
  596. }
  597. t86r = reply->message;
  598. if (t86r->hdr.type == TYPE86_RSP_CODE &&
  599. t86r->cprbx.cprb_ver_id == 0x02) {
  600. switch (resp_type->type) {
  601. case PCIXCC_RESPONSE_TYPE_ICA:
  602. length = sizeof(struct type86x_reply)
  603. + t86r->length - 2;
  604. length = min(PCIXCC_MAX_ICA_RESPONSE_SIZE, length);
  605. memcpy(msg->message, reply->message, length);
  606. break;
  607. case PCIXCC_RESPONSE_TYPE_XCRB:
  608. length = t86r->fmt2.offset2 + t86r->fmt2.count2;
  609. length = min(PCIXCC_MAX_XCRB_RESPONSE_SIZE, length);
  610. memcpy(msg->message, reply->message, length);
  611. break;
  612. default:
  613. memcpy(msg->message, &error_reply, sizeof error_reply);
  614. }
  615. } else
  616. memcpy(msg->message, reply->message, sizeof error_reply);
  617. out:
  618. complete(&(resp_type->work));
  619. }
  620. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  621. /**
  622. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  623. * device to handle a modexpo request.
  624. * @zdev: pointer to zcrypt_device structure that identifies the
  625. * PCIXCC/CEX2C device to the request distributor
  626. * @mex: pointer to the modexpo request buffer
  627. */
  628. static long zcrypt_pcixcc_modexpo(struct zcrypt_device *zdev,
  629. struct ica_rsa_modexpo *mex)
  630. {
  631. struct ap_message ap_msg;
  632. struct response_type resp_type = {
  633. .type = PCIXCC_RESPONSE_TYPE_ICA,
  634. };
  635. int rc;
  636. ap_init_message(&ap_msg);
  637. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  638. if (!ap_msg.message)
  639. return -ENOMEM;
  640. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  641. atomic_inc_return(&zcrypt_step);
  642. ap_msg.private = &resp_type;
  643. rc = ICAMEX_msg_to_type6MEX_msgX(zdev, &ap_msg, mex);
  644. if (rc)
  645. goto out_free;
  646. init_completion(&resp_type.work);
  647. ap_queue_message(zdev->ap_dev, &ap_msg);
  648. rc = wait_for_completion_interruptible(&resp_type.work);
  649. if (rc == 0)
  650. rc = convert_response_ica(zdev, &ap_msg, mex->outputdata,
  651. mex->outputdatalength);
  652. else
  653. /* Signal pending. */
  654. ap_cancel_message(zdev->ap_dev, &ap_msg);
  655. out_free:
  656. free_page((unsigned long) ap_msg.message);
  657. return rc;
  658. }
  659. /**
  660. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  661. * device to handle a modexpo_crt request.
  662. * @zdev: pointer to zcrypt_device structure that identifies the
  663. * PCIXCC/CEX2C device to the request distributor
  664. * @crt: pointer to the modexpoc_crt request buffer
  665. */
  666. static long zcrypt_pcixcc_modexpo_crt(struct zcrypt_device *zdev,
  667. struct ica_rsa_modexpo_crt *crt)
  668. {
  669. struct ap_message ap_msg;
  670. struct response_type resp_type = {
  671. .type = PCIXCC_RESPONSE_TYPE_ICA,
  672. };
  673. int rc;
  674. ap_init_message(&ap_msg);
  675. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  676. if (!ap_msg.message)
  677. return -ENOMEM;
  678. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  679. atomic_inc_return(&zcrypt_step);
  680. ap_msg.private = &resp_type;
  681. rc = ICACRT_msg_to_type6CRT_msgX(zdev, &ap_msg, crt);
  682. if (rc)
  683. goto out_free;
  684. init_completion(&resp_type.work);
  685. ap_queue_message(zdev->ap_dev, &ap_msg);
  686. rc = wait_for_completion_interruptible(&resp_type.work);
  687. if (rc == 0)
  688. rc = convert_response_ica(zdev, &ap_msg, crt->outputdata,
  689. crt->outputdatalength);
  690. else
  691. /* Signal pending. */
  692. ap_cancel_message(zdev->ap_dev, &ap_msg);
  693. out_free:
  694. free_page((unsigned long) ap_msg.message);
  695. return rc;
  696. }
  697. /**
  698. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  699. * device to handle a send_cprb request.
  700. * @zdev: pointer to zcrypt_device structure that identifies the
  701. * PCIXCC/CEX2C device to the request distributor
  702. * @xcRB: pointer to the send_cprb request buffer
  703. */
  704. static long zcrypt_pcixcc_send_cprb(struct zcrypt_device *zdev,
  705. struct ica_xcRB *xcRB)
  706. {
  707. struct ap_message ap_msg;
  708. struct response_type resp_type = {
  709. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  710. };
  711. int rc;
  712. ap_init_message(&ap_msg);
  713. ap_msg.message = kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  714. if (!ap_msg.message)
  715. return -ENOMEM;
  716. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  717. atomic_inc_return(&zcrypt_step);
  718. ap_msg.private = &resp_type;
  719. rc = XCRB_msg_to_type6CPRB_msgX(zdev, &ap_msg, xcRB);
  720. if (rc)
  721. goto out_free;
  722. init_completion(&resp_type.work);
  723. ap_queue_message(zdev->ap_dev, &ap_msg);
  724. rc = wait_for_completion_interruptible(&resp_type.work);
  725. if (rc == 0)
  726. rc = convert_response_xcrb(zdev, &ap_msg, xcRB);
  727. else
  728. /* Signal pending. */
  729. ap_cancel_message(zdev->ap_dev, &ap_msg);
  730. out_free:
  731. kzfree(ap_msg.message);
  732. return rc;
  733. }
  734. /**
  735. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  736. * device to generate random data.
  737. * @zdev: pointer to zcrypt_device structure that identifies the
  738. * PCIXCC/CEX2C device to the request distributor
  739. * @buffer: pointer to a memory page to return random data
  740. */
  741. static long zcrypt_pcixcc_rng(struct zcrypt_device *zdev,
  742. char *buffer)
  743. {
  744. struct ap_message ap_msg;
  745. struct response_type resp_type = {
  746. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  747. };
  748. int rc;
  749. ap_init_message(&ap_msg);
  750. ap_msg.message = kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  751. if (!ap_msg.message)
  752. return -ENOMEM;
  753. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  754. atomic_inc_return(&zcrypt_step);
  755. ap_msg.private = &resp_type;
  756. rng_type6CPRB_msgX(zdev->ap_dev, &ap_msg, ZCRYPT_RNG_BUFFER_SIZE);
  757. init_completion(&resp_type.work);
  758. ap_queue_message(zdev->ap_dev, &ap_msg);
  759. rc = wait_for_completion_interruptible(&resp_type.work);
  760. if (rc == 0)
  761. rc = convert_response_rng(zdev, &ap_msg, buffer);
  762. else
  763. /* Signal pending. */
  764. ap_cancel_message(zdev->ap_dev, &ap_msg);
  765. kfree(ap_msg.message);
  766. return rc;
  767. }
  768. /**
  769. * The crypto operations for a PCIXCC/CEX2C card.
  770. */
  771. static struct zcrypt_ops zcrypt_pcixcc_ops = {
  772. .rsa_modexpo = zcrypt_pcixcc_modexpo,
  773. .rsa_modexpo_crt = zcrypt_pcixcc_modexpo_crt,
  774. .send_cprb = zcrypt_pcixcc_send_cprb,
  775. };
  776. static struct zcrypt_ops zcrypt_pcixcc_with_rng_ops = {
  777. .rsa_modexpo = zcrypt_pcixcc_modexpo,
  778. .rsa_modexpo_crt = zcrypt_pcixcc_modexpo_crt,
  779. .send_cprb = zcrypt_pcixcc_send_cprb,
  780. .rng = zcrypt_pcixcc_rng,
  781. };
  782. /**
  783. * Micro-code detection function. Its sends a message to a pcixcc card
  784. * to find out the microcode level.
  785. * @ap_dev: pointer to the AP device.
  786. */
  787. static int zcrypt_pcixcc_mcl(struct ap_device *ap_dev)
  788. {
  789. static unsigned char msg[] = {
  790. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  791. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  792. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  793. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  794. 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
  795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  796. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00,
  797. 0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00,
  798. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  799. 0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00,
  800. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  801. 0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32,
  802. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8,
  803. 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,
  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,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,0x04,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,0x00,0x00,0x00,0x00,0x00,
  828. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x0A,
  829. 0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20,
  830. 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05,
  831. 0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
  832. 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55,
  833. 0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,
  834. 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,
  835. 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,
  836. 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,
  837. 0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54,
  838. 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00,
  839. 0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00,
  840. 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40,
  841. 0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C,
  842. 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF,
  843. 0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9,
  844. 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63,
  845. 0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5,
  846. 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A,
  847. 0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01,
  848. 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28,
  849. 0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91,
  850. 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5,
  851. 0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C,
  852. 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98,
  853. 0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96,
  854. 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19,
  855. 0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47,
  856. 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36,
  857. 0xF1,0x3D,0x93,0x53
  858. };
  859. unsigned long long psmid;
  860. struct CPRBX *cprbx;
  861. char *reply;
  862. int rc, i;
  863. reply = (void *) get_zeroed_page(GFP_KERNEL);
  864. if (!reply)
  865. return -ENOMEM;
  866. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, msg, sizeof(msg));
  867. if (rc)
  868. goto out_free;
  869. /* Wait for the test message to complete. */
  870. for (i = 0; i < 6; i++) {
  871. mdelay(300);
  872. rc = ap_recv(ap_dev->qid, &psmid, reply, 4096);
  873. if (rc == 0 && psmid == 0x0102030405060708ULL)
  874. break;
  875. }
  876. if (i >= 6) {
  877. /* Got no answer. */
  878. rc = -ENODEV;
  879. goto out_free;
  880. }
  881. cprbx = (struct CPRBX *) (reply + 48);
  882. if (cprbx->ccp_rtcode == 8 && cprbx->ccp_rscode == 33)
  883. rc = ZCRYPT_PCIXCC_MCL2;
  884. else
  885. rc = ZCRYPT_PCIXCC_MCL3;
  886. out_free:
  887. free_page((unsigned long) reply);
  888. return rc;
  889. }
  890. /**
  891. * Large random number detection function. Its sends a message to a pcixcc
  892. * card to find out if large random numbers are supported.
  893. * @ap_dev: pointer to the AP device.
  894. *
  895. * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  896. */
  897. static int zcrypt_pcixcc_rng_supported(struct ap_device *ap_dev)
  898. {
  899. struct ap_message ap_msg;
  900. unsigned long long psmid;
  901. struct {
  902. struct type86_hdr hdr;
  903. struct type86_fmt2_ext fmt2;
  904. struct CPRBX cprbx;
  905. } __attribute__((packed)) *reply;
  906. int rc, i;
  907. ap_init_message(&ap_msg);
  908. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  909. if (!ap_msg.message)
  910. return -ENOMEM;
  911. rng_type6CPRB_msgX(ap_dev, &ap_msg, 4);
  912. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, ap_msg.message,
  913. ap_msg.length);
  914. if (rc)
  915. goto out_free;
  916. /* Wait for the test message to complete. */
  917. for (i = 0; i < 2 * HZ; i++) {
  918. msleep(1000 / HZ);
  919. rc = ap_recv(ap_dev->qid, &psmid, ap_msg.message, 4096);
  920. if (rc == 0 && psmid == 0x0102030405060708ULL)
  921. break;
  922. }
  923. if (i >= 2 * HZ) {
  924. /* Got no answer. */
  925. rc = -ENODEV;
  926. goto out_free;
  927. }
  928. reply = ap_msg.message;
  929. if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
  930. rc = 1;
  931. else
  932. rc = 0;
  933. out_free:
  934. free_page((unsigned long) ap_msg.message);
  935. return rc;
  936. }
  937. /**
  938. * Probe function for PCIXCC/CEX2C cards. It always accepts the AP device
  939. * since the bus_match already checked the hardware type. The PCIXCC
  940. * cards come in two flavours: micro code level 2 and micro code level 3.
  941. * This is checked by sending a test message to the device.
  942. * @ap_dev: pointer to the AP device.
  943. */
  944. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev)
  945. {
  946. struct zcrypt_device *zdev;
  947. int rc;
  948. zdev = zcrypt_device_alloc(PCIXCC_MAX_RESPONSE_SIZE);
  949. if (!zdev)
  950. return -ENOMEM;
  951. zdev->ap_dev = ap_dev;
  952. zdev->online = 1;
  953. if (ap_dev->device_type == AP_DEVICE_TYPE_PCIXCC) {
  954. rc = zcrypt_pcixcc_mcl(ap_dev);
  955. if (rc < 0) {
  956. zcrypt_device_free(zdev);
  957. return rc;
  958. }
  959. zdev->user_space_type = rc;
  960. if (rc == ZCRYPT_PCIXCC_MCL2) {
  961. zdev->type_string = "PCIXCC_MCL2";
  962. zdev->speed_rating = PCIXCC_MCL2_SPEED_RATING;
  963. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  964. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  965. } else {
  966. zdev->type_string = "PCIXCC_MCL3";
  967. zdev->speed_rating = PCIXCC_MCL3_SPEED_RATING;
  968. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  969. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  970. }
  971. } else {
  972. zdev->user_space_type = ZCRYPT_CEX2C;
  973. zdev->type_string = "CEX2C";
  974. zdev->speed_rating = CEX2C_SPEED_RATING;
  975. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  976. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  977. }
  978. rc = zcrypt_pcixcc_rng_supported(ap_dev);
  979. if (rc < 0) {
  980. zcrypt_device_free(zdev);
  981. return rc;
  982. }
  983. if (rc)
  984. zdev->ops = &zcrypt_pcixcc_with_rng_ops;
  985. else
  986. zdev->ops = &zcrypt_pcixcc_ops;
  987. ap_dev->reply = &zdev->reply;
  988. ap_dev->private = zdev;
  989. rc = zcrypt_device_register(zdev);
  990. if (rc)
  991. goto out_free;
  992. return 0;
  993. out_free:
  994. ap_dev->private = NULL;
  995. zcrypt_device_free(zdev);
  996. return rc;
  997. }
  998. /**
  999. * This is called to remove the extended PCIXCC/CEX2C driver information
  1000. * if an AP device is removed.
  1001. */
  1002. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev)
  1003. {
  1004. struct zcrypt_device *zdev = ap_dev->private;
  1005. zcrypt_device_unregister(zdev);
  1006. }
  1007. int __init zcrypt_pcixcc_init(void)
  1008. {
  1009. return ap_driver_register(&zcrypt_pcixcc_driver, THIS_MODULE, "pcixcc");
  1010. }
  1011. void zcrypt_pcixcc_exit(void)
  1012. {
  1013. ap_driver_unregister(&zcrypt_pcixcc_driver);
  1014. }
  1015. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  1016. module_init(zcrypt_pcixcc_init);
  1017. module_exit(zcrypt_pcixcc_exit);
  1018. #endif