zcrypt_pcixcc.c 33 KB

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