zcrypt_pcixcc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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 local_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 = local_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. /* Fall through, no break, incorrect cprb version is an unknown
  515. * response */
  516. default: /* Unknown response type, this should NEVER EVER happen */
  517. zdev->online = 0;
  518. return -EAGAIN; /* repeat the request on a different device. */
  519. }
  520. }
  521. static int convert_response_xcrb(struct zcrypt_device *zdev,
  522. struct ap_message *reply,
  523. struct ica_xcRB *xcRB)
  524. {
  525. struct type86x_reply *msg = reply->message;
  526. /* Response type byte is the second byte in the response. */
  527. switch (((unsigned char *) reply->message)[1]) {
  528. case TYPE82_RSP_CODE:
  529. case TYPE88_RSP_CODE:
  530. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  531. return convert_error(zdev, reply);
  532. case TYPE86_RSP_CODE:
  533. if (msg->hdr.reply_code) {
  534. memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
  535. return convert_error(zdev, reply);
  536. }
  537. if (msg->cprbx.cprb_ver_id == 0x02)
  538. return convert_type86_xcrb(zdev, reply, xcRB);
  539. /* Fall through, no break, incorrect cprb version is an unknown
  540. * response */
  541. default: /* Unknown response type, this should NEVER EVER happen */
  542. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  543. zdev->online = 0;
  544. return -EAGAIN; /* repeat the request on a different device. */
  545. }
  546. }
  547. static int convert_response_rng(struct zcrypt_device *zdev,
  548. struct ap_message *reply,
  549. char *data)
  550. {
  551. struct type86x_reply *msg = reply->message;
  552. switch (msg->hdr.type) {
  553. case TYPE82_RSP_CODE:
  554. case TYPE88_RSP_CODE:
  555. return -EINVAL;
  556. case TYPE86_RSP_CODE:
  557. if (msg->hdr.reply_code)
  558. return -EINVAL;
  559. if (msg->cprbx.cprb_ver_id == 0x02)
  560. return convert_type86_rng(zdev, reply, data);
  561. /* Fall through, no break, incorrect cprb version is an unknown
  562. * response */
  563. default: /* Unknown response type, this should NEVER EVER happen */
  564. zdev->online = 0;
  565. return -EAGAIN; /* repeat the request on a different device. */
  566. }
  567. }
  568. /**
  569. * This function is called from the AP bus code after a crypto request
  570. * "msg" has finished with the reply message "reply".
  571. * It is called from tasklet context.
  572. * @ap_dev: pointer to the AP device
  573. * @msg: pointer to the AP message
  574. * @reply: pointer to the AP reply message
  575. */
  576. static void zcrypt_pcixcc_receive(struct ap_device *ap_dev,
  577. struct ap_message *msg,
  578. struct ap_message *reply)
  579. {
  580. static struct error_hdr error_reply = {
  581. .type = TYPE82_RSP_CODE,
  582. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  583. };
  584. struct response_type *resp_type =
  585. (struct response_type *) msg->private;
  586. struct type86x_reply *t86r;
  587. int length;
  588. /* Copy the reply message to the request message buffer. */
  589. if (IS_ERR(reply)) {
  590. memcpy(msg->message, &error_reply, sizeof(error_reply));
  591. goto out;
  592. }
  593. t86r = reply->message;
  594. if (t86r->hdr.type == TYPE86_RSP_CODE &&
  595. t86r->cprbx.cprb_ver_id == 0x02) {
  596. switch (resp_type->type) {
  597. case PCIXCC_RESPONSE_TYPE_ICA:
  598. length = sizeof(struct type86x_reply)
  599. + t86r->length - 2;
  600. length = min(PCIXCC_MAX_ICA_RESPONSE_SIZE, length);
  601. memcpy(msg->message, reply->message, length);
  602. break;
  603. case PCIXCC_RESPONSE_TYPE_XCRB:
  604. length = t86r->fmt2.offset2 + t86r->fmt2.count2;
  605. length = min(PCIXCC_MAX_XCRB_RESPONSE_SIZE, length);
  606. memcpy(msg->message, reply->message, length);
  607. break;
  608. default:
  609. memcpy(msg->message, &error_reply, sizeof error_reply);
  610. }
  611. } else
  612. memcpy(msg->message, reply->message, sizeof error_reply);
  613. out:
  614. complete(&(resp_type->work));
  615. }
  616. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  617. /**
  618. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  619. * device to handle a modexpo request.
  620. * @zdev: pointer to zcrypt_device structure that identifies the
  621. * PCIXCC/CEX2C device to the request distributor
  622. * @mex: pointer to the modexpo request buffer
  623. */
  624. static long zcrypt_pcixcc_modexpo(struct zcrypt_device *zdev,
  625. struct ica_rsa_modexpo *mex)
  626. {
  627. struct ap_message ap_msg;
  628. struct response_type resp_type = {
  629. .type = PCIXCC_RESPONSE_TYPE_ICA,
  630. };
  631. int rc;
  632. ap_init_message(&ap_msg);
  633. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  634. if (!ap_msg.message)
  635. return -ENOMEM;
  636. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  637. atomic_inc_return(&zcrypt_step);
  638. ap_msg.private = &resp_type;
  639. rc = ICAMEX_msg_to_type6MEX_msgX(zdev, &ap_msg, mex);
  640. if (rc)
  641. goto out_free;
  642. init_completion(&resp_type.work);
  643. ap_queue_message(zdev->ap_dev, &ap_msg);
  644. rc = wait_for_completion_interruptible(&resp_type.work);
  645. if (rc == 0)
  646. rc = convert_response_ica(zdev, &ap_msg, mex->outputdata,
  647. mex->outputdatalength);
  648. else
  649. /* Signal pending. */
  650. ap_cancel_message(zdev->ap_dev, &ap_msg);
  651. out_free:
  652. free_page((unsigned long) ap_msg.message);
  653. return rc;
  654. }
  655. /**
  656. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  657. * device to handle a modexpo_crt request.
  658. * @zdev: pointer to zcrypt_device structure that identifies the
  659. * PCIXCC/CEX2C device to the request distributor
  660. * @crt: pointer to the modexpoc_crt request buffer
  661. */
  662. static long zcrypt_pcixcc_modexpo_crt(struct zcrypt_device *zdev,
  663. struct ica_rsa_modexpo_crt *crt)
  664. {
  665. struct ap_message ap_msg;
  666. struct response_type resp_type = {
  667. .type = PCIXCC_RESPONSE_TYPE_ICA,
  668. };
  669. int rc;
  670. ap_init_message(&ap_msg);
  671. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  672. if (!ap_msg.message)
  673. return -ENOMEM;
  674. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  675. atomic_inc_return(&zcrypt_step);
  676. ap_msg.private = &resp_type;
  677. rc = ICACRT_msg_to_type6CRT_msgX(zdev, &ap_msg, crt);
  678. if (rc)
  679. goto out_free;
  680. init_completion(&resp_type.work);
  681. ap_queue_message(zdev->ap_dev, &ap_msg);
  682. rc = wait_for_completion_interruptible(&resp_type.work);
  683. if (rc == 0)
  684. rc = convert_response_ica(zdev, &ap_msg, crt->outputdata,
  685. crt->outputdatalength);
  686. else
  687. /* Signal pending. */
  688. ap_cancel_message(zdev->ap_dev, &ap_msg);
  689. out_free:
  690. free_page((unsigned long) ap_msg.message);
  691. return rc;
  692. }
  693. /**
  694. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  695. * device to handle a send_cprb request.
  696. * @zdev: pointer to zcrypt_device structure that identifies the
  697. * PCIXCC/CEX2C device to the request distributor
  698. * @xcRB: pointer to the send_cprb request buffer
  699. */
  700. static long zcrypt_pcixcc_send_cprb(struct zcrypt_device *zdev,
  701. struct ica_xcRB *xcRB)
  702. {
  703. struct ap_message ap_msg;
  704. struct response_type resp_type = {
  705. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  706. };
  707. int rc;
  708. ap_init_message(&ap_msg);
  709. ap_msg.message = kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  710. if (!ap_msg.message)
  711. return -ENOMEM;
  712. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  713. atomic_inc_return(&zcrypt_step);
  714. ap_msg.private = &resp_type;
  715. rc = XCRB_msg_to_type6CPRB_msgX(zdev, &ap_msg, xcRB);
  716. if (rc)
  717. goto out_free;
  718. init_completion(&resp_type.work);
  719. ap_queue_message(zdev->ap_dev, &ap_msg);
  720. rc = wait_for_completion_interruptible(&resp_type.work);
  721. if (rc == 0)
  722. rc = convert_response_xcrb(zdev, &ap_msg, xcRB);
  723. else
  724. /* Signal pending. */
  725. ap_cancel_message(zdev->ap_dev, &ap_msg);
  726. out_free:
  727. kzfree(ap_msg.message);
  728. return rc;
  729. }
  730. /**
  731. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  732. * device to generate random data.
  733. * @zdev: pointer to zcrypt_device structure that identifies the
  734. * PCIXCC/CEX2C device to the request distributor
  735. * @buffer: pointer to a memory page to return random data
  736. */
  737. static long zcrypt_pcixcc_rng(struct zcrypt_device *zdev,
  738. char *buffer)
  739. {
  740. struct ap_message ap_msg;
  741. struct response_type resp_type = {
  742. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  743. };
  744. int rc;
  745. ap_init_message(&ap_msg);
  746. ap_msg.message = kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  747. if (!ap_msg.message)
  748. return -ENOMEM;
  749. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  750. atomic_inc_return(&zcrypt_step);
  751. ap_msg.private = &resp_type;
  752. rng_type6CPRB_msgX(zdev->ap_dev, &ap_msg, ZCRYPT_RNG_BUFFER_SIZE);
  753. init_completion(&resp_type.work);
  754. ap_queue_message(zdev->ap_dev, &ap_msg);
  755. rc = wait_for_completion_interruptible(&resp_type.work);
  756. if (rc == 0)
  757. rc = convert_response_rng(zdev, &ap_msg, buffer);
  758. else
  759. /* Signal pending. */
  760. ap_cancel_message(zdev->ap_dev, &ap_msg);
  761. kfree(ap_msg.message);
  762. return rc;
  763. }
  764. /**
  765. * The crypto operations for a PCIXCC/CEX2C card.
  766. */
  767. static struct zcrypt_ops zcrypt_pcixcc_ops = {
  768. .rsa_modexpo = zcrypt_pcixcc_modexpo,
  769. .rsa_modexpo_crt = zcrypt_pcixcc_modexpo_crt,
  770. .send_cprb = zcrypt_pcixcc_send_cprb,
  771. };
  772. static struct zcrypt_ops zcrypt_pcixcc_with_rng_ops = {
  773. .rsa_modexpo = zcrypt_pcixcc_modexpo,
  774. .rsa_modexpo_crt = zcrypt_pcixcc_modexpo_crt,
  775. .send_cprb = zcrypt_pcixcc_send_cprb,
  776. .rng = zcrypt_pcixcc_rng,
  777. };
  778. /**
  779. * Micro-code detection function. Its sends a message to a pcixcc card
  780. * to find out the microcode level.
  781. * @ap_dev: pointer to the AP device.
  782. */
  783. static int zcrypt_pcixcc_mcl(struct ap_device *ap_dev)
  784. {
  785. static unsigned char msg[] = {
  786. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  787. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  788. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  789. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  790. 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
  791. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  792. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00,
  793. 0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00,
  794. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  795. 0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00,
  796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  797. 0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32,
  798. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8,
  799. 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,
  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,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,0x04,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,0x50,0x4B,0x00,0x0A,
  825. 0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20,
  826. 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05,
  827. 0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
  828. 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55,
  829. 0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,
  830. 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,
  831. 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,
  832. 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,
  833. 0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54,
  834. 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00,
  835. 0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00,
  836. 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40,
  837. 0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C,
  838. 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF,
  839. 0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9,
  840. 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63,
  841. 0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5,
  842. 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A,
  843. 0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01,
  844. 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28,
  845. 0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91,
  846. 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5,
  847. 0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C,
  848. 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98,
  849. 0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96,
  850. 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19,
  851. 0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47,
  852. 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36,
  853. 0xF1,0x3D,0x93,0x53
  854. };
  855. unsigned long long psmid;
  856. struct CPRBX *cprbx;
  857. char *reply;
  858. int rc, i;
  859. reply = (void *) get_zeroed_page(GFP_KERNEL);
  860. if (!reply)
  861. return -ENOMEM;
  862. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, msg, sizeof(msg));
  863. if (rc)
  864. goto out_free;
  865. /* Wait for the test message to complete. */
  866. for (i = 0; i < 6; i++) {
  867. mdelay(300);
  868. rc = ap_recv(ap_dev->qid, &psmid, reply, 4096);
  869. if (rc == 0 && psmid == 0x0102030405060708ULL)
  870. break;
  871. }
  872. if (i >= 6) {
  873. /* Got no answer. */
  874. rc = -ENODEV;
  875. goto out_free;
  876. }
  877. cprbx = (struct CPRBX *) (reply + 48);
  878. if (cprbx->ccp_rtcode == 8 && cprbx->ccp_rscode == 33)
  879. rc = ZCRYPT_PCIXCC_MCL2;
  880. else
  881. rc = ZCRYPT_PCIXCC_MCL3;
  882. out_free:
  883. free_page((unsigned long) reply);
  884. return rc;
  885. }
  886. /**
  887. * Large random number detection function. Its sends a message to a pcixcc
  888. * card to find out if large random numbers are supported.
  889. * @ap_dev: pointer to the AP device.
  890. *
  891. * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
  892. */
  893. static int zcrypt_pcixcc_rng_supported(struct ap_device *ap_dev)
  894. {
  895. struct ap_message ap_msg;
  896. unsigned long long psmid;
  897. struct {
  898. struct type86_hdr hdr;
  899. struct type86_fmt2_ext fmt2;
  900. struct CPRBX cprbx;
  901. } __attribute__((packed)) *reply;
  902. int rc, i;
  903. ap_init_message(&ap_msg);
  904. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  905. if (!ap_msg.message)
  906. return -ENOMEM;
  907. rng_type6CPRB_msgX(ap_dev, &ap_msg, 4);
  908. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, ap_msg.message,
  909. ap_msg.length);
  910. if (rc)
  911. goto out_free;
  912. /* Wait for the test message to complete. */
  913. for (i = 0; i < 2 * HZ; i++) {
  914. msleep(1000 / HZ);
  915. rc = ap_recv(ap_dev->qid, &psmid, ap_msg.message, 4096);
  916. if (rc == 0 && psmid == 0x0102030405060708ULL)
  917. break;
  918. }
  919. if (i >= 2 * HZ) {
  920. /* Got no answer. */
  921. rc = -ENODEV;
  922. goto out_free;
  923. }
  924. reply = ap_msg.message;
  925. if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
  926. rc = 1;
  927. else
  928. rc = 0;
  929. out_free:
  930. free_page((unsigned long) ap_msg.message);
  931. return rc;
  932. }
  933. /**
  934. * Probe function for PCIXCC/CEX2C cards. It always accepts the AP device
  935. * since the bus_match already checked the hardware type. The PCIXCC
  936. * cards come in two flavours: micro code level 2 and micro code level 3.
  937. * This is checked by sending a test message to the device.
  938. * @ap_dev: pointer to the AP device.
  939. */
  940. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev)
  941. {
  942. struct zcrypt_device *zdev;
  943. int rc;
  944. zdev = zcrypt_device_alloc(PCIXCC_MAX_RESPONSE_SIZE);
  945. if (!zdev)
  946. return -ENOMEM;
  947. zdev->ap_dev = ap_dev;
  948. zdev->online = 1;
  949. if (ap_dev->device_type == AP_DEVICE_TYPE_PCIXCC) {
  950. rc = zcrypt_pcixcc_mcl(ap_dev);
  951. if (rc < 0) {
  952. zcrypt_device_free(zdev);
  953. return rc;
  954. }
  955. zdev->user_space_type = rc;
  956. if (rc == ZCRYPT_PCIXCC_MCL2) {
  957. zdev->type_string = "PCIXCC_MCL2";
  958. zdev->speed_rating = PCIXCC_MCL2_SPEED_RATING;
  959. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  960. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  961. } else {
  962. zdev->type_string = "PCIXCC_MCL3";
  963. zdev->speed_rating = PCIXCC_MCL3_SPEED_RATING;
  964. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  965. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  966. }
  967. } else {
  968. zdev->user_space_type = ZCRYPT_CEX2C;
  969. zdev->type_string = "CEX2C";
  970. zdev->speed_rating = CEX2C_SPEED_RATING;
  971. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  972. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  973. }
  974. rc = zcrypt_pcixcc_rng_supported(ap_dev);
  975. if (rc < 0) {
  976. zcrypt_device_free(zdev);
  977. return rc;
  978. }
  979. if (rc)
  980. zdev->ops = &zcrypt_pcixcc_with_rng_ops;
  981. else
  982. zdev->ops = &zcrypt_pcixcc_ops;
  983. ap_dev->reply = &zdev->reply;
  984. ap_dev->private = zdev;
  985. rc = zcrypt_device_register(zdev);
  986. if (rc)
  987. goto out_free;
  988. return 0;
  989. out_free:
  990. ap_dev->private = NULL;
  991. zcrypt_device_free(zdev);
  992. return rc;
  993. }
  994. /**
  995. * This is called to remove the extended PCIXCC/CEX2C driver information
  996. * if an AP device is removed.
  997. */
  998. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev)
  999. {
  1000. struct zcrypt_device *zdev = ap_dev->private;
  1001. zcrypt_device_unregister(zdev);
  1002. }
  1003. int __init zcrypt_pcixcc_init(void)
  1004. {
  1005. return ap_driver_register(&zcrypt_pcixcc_driver, THIS_MODULE, "pcixcc");
  1006. }
  1007. void zcrypt_pcixcc_exit(void)
  1008. {
  1009. ap_driver_unregister(&zcrypt_pcixcc_driver);
  1010. }
  1011. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  1012. module_init(zcrypt_pcixcc_init);
  1013. module_exit(zcrypt_pcixcc_exit);
  1014. #endif