zcrypt_pcixcc.c 34 KB

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