zcrypt_pcixcc.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * zcrypt 2.1.0
  3. *
  4. * Copyright IBM Corp. 2001, 2006
  5. * Author(s): Robert Burroughs
  6. * Eric Rossman (edrossma@us.ibm.com)
  7. *
  8. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  9. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  10. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/err.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/atomic.h>
  32. #include <asm/uaccess.h>
  33. #include "ap_bus.h"
  34. #include "zcrypt_api.h"
  35. #include "zcrypt_error.h"
  36. #include "zcrypt_pcicc.h"
  37. #include "zcrypt_pcixcc.h"
  38. #include "zcrypt_cca_key.h"
  39. #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
  40. #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
  41. #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
  42. #define CEX3C_MIN_MOD_SIZE PCIXCC_MIN_MOD_SIZE
  43. #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
  44. #define PCIXCC_MCL2_SPEED_RATING 7870
  45. #define PCIXCC_MCL3_SPEED_RATING 7870
  46. #define CEX2C_SPEED_RATING 7000
  47. #define CEX3C_SPEED_RATING 6500
  48. #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
  49. #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
  50. #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
  51. #define PCIXCC_CLEANUP_TIME (15*HZ)
  52. #define CEIL4(x) ((((x)+3)/4)*4)
  53. struct response_type {
  54. struct completion work;
  55. int type;
  56. };
  57. #define PCIXCC_RESPONSE_TYPE_ICA 0
  58. #define PCIXCC_RESPONSE_TYPE_XCRB 1
  59. static struct ap_device_id zcrypt_pcixcc_ids[] = {
  60. { AP_DEVICE(AP_DEVICE_TYPE_PCIXCC) },
  61. { AP_DEVICE(AP_DEVICE_TYPE_CEX2C) },
  62. { AP_DEVICE(AP_DEVICE_TYPE_CEX3C) },
  63. { /* end of list */ },
  64. };
  65. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_ids);
  66. MODULE_AUTHOR("IBM Corporation");
  67. MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, "
  68. "Copyright IBM Corp. 2001, 2006");
  69. MODULE_LICENSE("GPL");
  70. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev);
  71. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev);
  72. static void zcrypt_pcixcc_receive(struct ap_device *, struct ap_message *,
  73. struct ap_message *);
  74. static struct ap_driver zcrypt_pcixcc_driver = {
  75. .probe = zcrypt_pcixcc_probe,
  76. .remove = zcrypt_pcixcc_remove,
  77. .ids = zcrypt_pcixcc_ids,
  78. .request_timeout = PCIXCC_CLEANUP_TIME,
  79. };
  80. /**
  81. * The following is used to initialize the CPRBX passed to the PCIXCC/CEX2C
  82. * card in a type6 message. The 3 fields that must be filled in at execution
  83. * time are req_parml, rpl_parml and usage_domain.
  84. * Everything about this interface is ascii/big-endian, since the
  85. * device does *not* have 'Intel inside'.
  86. *
  87. * The CPRBX is followed immediately by the parm block.
  88. * The parm block contains:
  89. * - function code ('PD' 0x5044 or 'PK' 0x504B)
  90. * - rule block (one of:)
  91. * + 0x000A 'PKCS-1.2' (MCL2 'PD')
  92. * + 0x000A 'ZERO-PAD' (MCL2 'PK')
  93. * + 0x000A 'ZERO-PAD' (MCL3 'PD' or CEX2C 'PD')
  94. * + 0x000A 'MRP ' (MCL3 'PK' or CEX2C 'PK')
  95. * - VUD block
  96. */
  97. static struct CPRBX static_cprbx = {
  98. .cprb_len = 0x00DC,
  99. .cprb_ver_id = 0x02,
  100. .func_id = {0x54,0x32},
  101. };
  102. /**
  103. * Convert a ICAMEX message to a type6 MEX message.
  104. *
  105. * @zdev: crypto device pointer
  106. * @ap_msg: pointer to AP message
  107. * @mex: pointer to user input data
  108. *
  109. * Returns 0 on success or -EFAULT.
  110. */
  111. static int ICAMEX_msg_to_type6MEX_msgX(struct zcrypt_device *zdev,
  112. struct ap_message *ap_msg,
  113. struct ica_rsa_modexpo *mex)
  114. {
  115. static struct type6_hdr static_type6_hdrX = {
  116. .type = 0x06,
  117. .offset1 = 0x00000058,
  118. .agent_id = {'C','A',},
  119. .function_code = {'P','K'},
  120. };
  121. static struct function_and_rules_block static_pke_fnr = {
  122. .function_code = {'P','K'},
  123. .ulen = 10,
  124. .only_rule = {'M','R','P',' ',' ',' ',' ',' '}
  125. };
  126. static struct function_and_rules_block static_pke_fnr_MCL2 = {
  127. .function_code = {'P','K'},
  128. .ulen = 10,
  129. .only_rule = {'Z','E','R','O','-','P','A','D'}
  130. };
  131. struct {
  132. struct type6_hdr hdr;
  133. struct CPRBX cprbx;
  134. struct function_and_rules_block fr;
  135. unsigned short length;
  136. char text[0];
  137. } __attribute__((packed)) *msg = ap_msg->message;
  138. int size;
  139. /* VUD.ciphertext */
  140. msg->length = mex->inputdatalength + 2;
  141. if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
  142. return -EFAULT;
  143. /* Set up key which is located after the variable length text. */
  144. size = zcrypt_type6_mex_key_en(mex, msg->text+mex->inputdatalength, 1);
  145. if (size < 0)
  146. return size;
  147. size += sizeof(*msg) + mex->inputdatalength;
  148. /* message header, cprbx and f&r */
  149. msg->hdr = static_type6_hdrX;
  150. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  151. msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  152. msg->cprbx = static_cprbx;
  153. msg->cprbx.domain = AP_QID_QUEUE(zdev->ap_dev->qid);
  154. msg->cprbx.rpl_msgbl = msg->hdr.FromCardLen1;
  155. msg->fr = (zdev->user_space_type == ZCRYPT_PCIXCC_MCL2) ?
  156. static_pke_fnr_MCL2 : static_pke_fnr;
  157. msg->cprbx.req_parml = size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  158. ap_msg->length = size;
  159. return 0;
  160. }
  161. /**
  162. * Convert a ICACRT message to a type6 CRT message.
  163. *
  164. * @zdev: crypto device pointer
  165. * @ap_msg: pointer to AP message
  166. * @crt: pointer to user input data
  167. *
  168. * Returns 0 on success or -EFAULT.
  169. */
  170. static int ICACRT_msg_to_type6CRT_msgX(struct zcrypt_device *zdev,
  171. struct ap_message *ap_msg,
  172. struct ica_rsa_modexpo_crt *crt)
  173. {
  174. static struct type6_hdr static_type6_hdrX = {
  175. .type = 0x06,
  176. .offset1 = 0x00000058,
  177. .agent_id = {'C','A',},
  178. .function_code = {'P','D'},
  179. };
  180. static struct function_and_rules_block static_pkd_fnr = {
  181. .function_code = {'P','D'},
  182. .ulen = 10,
  183. .only_rule = {'Z','E','R','O','-','P','A','D'}
  184. };
  185. static struct function_and_rules_block static_pkd_fnr_MCL2 = {
  186. .function_code = {'P','D'},
  187. .ulen = 10,
  188. .only_rule = {'P','K','C','S','-','1','.','2'}
  189. };
  190. struct {
  191. struct type6_hdr hdr;
  192. struct CPRBX cprbx;
  193. struct function_and_rules_block fr;
  194. unsigned short length;
  195. char text[0];
  196. } __attribute__((packed)) *msg = ap_msg->message;
  197. int size;
  198. /* VUD.ciphertext */
  199. msg->length = crt->inputdatalength + 2;
  200. if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
  201. return -EFAULT;
  202. /* Set up key which is located after the variable length text. */
  203. size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength, 1);
  204. if (size < 0)
  205. return size;
  206. size += sizeof(*msg) + crt->inputdatalength; /* total size of msg */
  207. /* message header, cprbx and f&r */
  208. msg->hdr = static_type6_hdrX;
  209. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  210. msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  211. msg->cprbx = static_cprbx;
  212. msg->cprbx.domain = AP_QID_QUEUE(zdev->ap_dev->qid);
  213. msg->cprbx.req_parml = msg->cprbx.rpl_msgbl =
  214. size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  215. msg->fr = (zdev->user_space_type == ZCRYPT_PCIXCC_MCL2) ?
  216. static_pkd_fnr_MCL2 : static_pkd_fnr;
  217. ap_msg->length = size;
  218. return 0;
  219. }
  220. /**
  221. * Convert a XCRB message to a type6 CPRB message.
  222. *
  223. * @zdev: crypto device pointer
  224. * @ap_msg: pointer to AP message
  225. * @xcRB: pointer to user input data
  226. *
  227. * Returns 0 on success or -EFAULT, -EINVAL.
  228. */
  229. struct type86_fmt2_msg {
  230. struct type86_hdr hdr;
  231. struct type86_fmt2_ext fmt2;
  232. } __attribute__((packed));
  233. static int XCRB_msg_to_type6CPRB_msgX(struct zcrypt_device *zdev,
  234. struct ap_message *ap_msg,
  235. struct ica_xcRB *xcRB)
  236. {
  237. static struct type6_hdr static_type6_hdrX = {
  238. .type = 0x06,
  239. .offset1 = 0x00000058,
  240. };
  241. struct {
  242. struct type6_hdr hdr;
  243. struct CPRBX cprbx;
  244. } __attribute__((packed)) *msg = ap_msg->message;
  245. int rcblen = CEIL4(xcRB->request_control_blk_length);
  246. int replylen;
  247. char *req_data = ap_msg->message + sizeof(struct type6_hdr) + rcblen;
  248. char *function_code;
  249. /* length checks */
  250. ap_msg->length = sizeof(struct type6_hdr) +
  251. CEIL4(xcRB->request_control_blk_length) +
  252. xcRB->request_data_length;
  253. if (ap_msg->length > PCIXCC_MAX_XCRB_MESSAGE_SIZE)
  254. return -EINVAL;
  255. replylen = sizeof(struct type86_fmt2_msg) +
  256. CEIL4(xcRB->reply_control_blk_length) +
  257. xcRB->reply_data_length;
  258. if (replylen > PCIXCC_MAX_XCRB_MESSAGE_SIZE)
  259. return -EINVAL;
  260. /* prepare type6 header */
  261. msg->hdr = static_type6_hdrX;
  262. memcpy(msg->hdr.agent_id , &(xcRB->agent_ID), sizeof(xcRB->agent_ID));
  263. msg->hdr.ToCardLen1 = xcRB->request_control_blk_length;
  264. if (xcRB->request_data_length) {
  265. msg->hdr.offset2 = msg->hdr.offset1 + rcblen;
  266. msg->hdr.ToCardLen2 = xcRB->request_data_length;
  267. }
  268. msg->hdr.FromCardLen1 = xcRB->reply_control_blk_length;
  269. msg->hdr.FromCardLen2 = xcRB->reply_data_length;
  270. /* prepare CPRB */
  271. if (copy_from_user(&(msg->cprbx), xcRB->request_control_blk_addr,
  272. xcRB->request_control_blk_length))
  273. return -EFAULT;
  274. if (msg->cprbx.cprb_len + sizeof(msg->hdr.function_code) >
  275. xcRB->request_control_blk_length)
  276. return -EINVAL;
  277. function_code = ((unsigned char *)&msg->cprbx) + msg->cprbx.cprb_len;
  278. memcpy(msg->hdr.function_code, function_code, sizeof(msg->hdr.function_code));
  279. if (memcmp(function_code, "US", 2) == 0)
  280. ap_msg->special = 1;
  281. else
  282. ap_msg->special = 0;
  283. /* copy data block */
  284. if (xcRB->request_data_length &&
  285. copy_from_user(req_data, xcRB->request_data_address,
  286. xcRB->request_data_length))
  287. return -EFAULT;
  288. return 0;
  289. }
  290. /**
  291. * Prepare a type6 CPRB message for random number generation
  292. *
  293. * @ap_dev: AP device pointer
  294. * @ap_msg: pointer to AP message
  295. */
  296. static void rng_type6CPRB_msgX(struct ap_device *ap_dev,
  297. struct ap_message *ap_msg,
  298. unsigned random_number_length)
  299. {
  300. struct {
  301. struct type6_hdr hdr;
  302. struct CPRBX cprbx;
  303. char function_code[2];
  304. short int rule_length;
  305. char rule[8];
  306. short int verb_length;
  307. short int key_length;
  308. } __attribute__((packed)) *msg = ap_msg->message;
  309. static struct type6_hdr static_type6_hdrX = {
  310. .type = 0x06,
  311. .offset1 = 0x00000058,
  312. .agent_id = {'C', 'A'},
  313. .function_code = {'R', 'L'},
  314. .ToCardLen1 = sizeof *msg - sizeof(msg->hdr),
  315. .FromCardLen1 = sizeof *msg - sizeof(msg->hdr),
  316. };
  317. static struct CPRBX local_cprbx = {
  318. .cprb_len = 0x00dc,
  319. .cprb_ver_id = 0x02,
  320. .func_id = {0x54, 0x32},
  321. .req_parml = sizeof *msg - sizeof(msg->hdr) -
  322. sizeof(msg->cprbx),
  323. .rpl_msgbl = sizeof *msg - sizeof(msg->hdr),
  324. };
  325. msg->hdr = static_type6_hdrX;
  326. msg->hdr.FromCardLen2 = random_number_length,
  327. msg->cprbx = local_cprbx;
  328. msg->cprbx.rpl_datal = random_number_length,
  329. msg->cprbx.domain = AP_QID_QUEUE(ap_dev->qid);
  330. memcpy(msg->function_code, msg->hdr.function_code, 0x02);
  331. msg->rule_length = 0x0a;
  332. memcpy(msg->rule, "RANDOM ", 8);
  333. msg->verb_length = 0x02;
  334. msg->key_length = 0x02;
  335. ap_msg->length = sizeof *msg;
  336. }
  337. /**
  338. * Copy results from a type 86 ICA reply message back to user space.
  339. *
  340. * @zdev: crypto device pointer
  341. * @reply: reply AP message.
  342. * @data: pointer to user output data
  343. * @length: size of user output data
  344. *
  345. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  346. */
  347. struct type86x_reply {
  348. struct type86_hdr hdr;
  349. struct type86_fmt2_ext fmt2;
  350. struct CPRBX cprbx;
  351. unsigned char pad[4]; /* 4 byte function code/rules block ? */
  352. unsigned short length;
  353. char text[0];
  354. } __attribute__((packed));
  355. static int convert_type86_ica(struct zcrypt_device *zdev,
  356. struct ap_message *reply,
  357. char __user *outputdata,
  358. unsigned int outputdatalength)
  359. {
  360. static unsigned char static_pad[] = {
  361. 0x00,0x02,
  362. 0x1B,0x7B,0x5D,0xB5,0x75,0x01,0x3D,0xFD,
  363. 0x8D,0xD1,0xC7,0x03,0x2D,0x09,0x23,0x57,
  364. 0x89,0x49,0xB9,0x3F,0xBB,0x99,0x41,0x5B,
  365. 0x75,0x21,0x7B,0x9D,0x3B,0x6B,0x51,0x39,
  366. 0xBB,0x0D,0x35,0xB9,0x89,0x0F,0x93,0xA5,
  367. 0x0B,0x47,0xF1,0xD3,0xBB,0xCB,0xF1,0x9D,
  368. 0x23,0x73,0x71,0xFF,0xF3,0xF5,0x45,0xFB,
  369. 0x61,0x29,0x23,0xFD,0xF1,0x29,0x3F,0x7F,
  370. 0x17,0xB7,0x1B,0xA9,0x19,0xBD,0x57,0xA9,
  371. 0xD7,0x95,0xA3,0xCB,0xED,0x1D,0xDB,0x45,
  372. 0x7D,0x11,0xD1,0x51,0x1B,0xED,0x71,0xE9,
  373. 0xB1,0xD1,0xAB,0xAB,0x21,0x2B,0x1B,0x9F,
  374. 0x3B,0x9F,0xF7,0xF7,0xBD,0x63,0xEB,0xAD,
  375. 0xDF,0xB3,0x6F,0x5B,0xDB,0x8D,0xA9,0x5D,
  376. 0xE3,0x7D,0x77,0x49,0x47,0xF5,0xA7,0xFD,
  377. 0xAB,0x2F,0x27,0x35,0x77,0xD3,0x49,0xC9,
  378. 0x09,0xEB,0xB1,0xF9,0xBF,0x4B,0xCB,0x2B,
  379. 0xEB,0xEB,0x05,0xFF,0x7D,0xC7,0x91,0x8B,
  380. 0x09,0x83,0xB9,0xB9,0x69,0x33,0x39,0x6B,
  381. 0x79,0x75,0x19,0xBF,0xBB,0x07,0x1D,0xBD,
  382. 0x29,0xBF,0x39,0x95,0x93,0x1D,0x35,0xC7,
  383. 0xC9,0x4D,0xE5,0x97,0x0B,0x43,0x9B,0xF1,
  384. 0x16,0x93,0x03,0x1F,0xA5,0xFB,0xDB,0xF3,
  385. 0x27,0x4F,0x27,0x61,0x05,0x1F,0xB9,0x23,
  386. 0x2F,0xC3,0x81,0xA9,0x23,0x71,0x55,0x55,
  387. 0xEB,0xED,0x41,0xE5,0xF3,0x11,0xF1,0x43,
  388. 0x69,0x03,0xBD,0x0B,0x37,0x0F,0x51,0x8F,
  389. 0x0B,0xB5,0x89,0x5B,0x67,0xA9,0xD9,0x4F,
  390. 0x01,0xF9,0x21,0x77,0x37,0x73,0x79,0xC5,
  391. 0x7F,0x51,0xC1,0xCF,0x97,0xA1,0x75,0xAD,
  392. 0x35,0x9D,0xD3,0xD3,0xA7,0x9D,0x5D,0x41,
  393. 0x6F,0x65,0x1B,0xCF,0xA9,0x87,0x91,0x09
  394. };
  395. struct type86x_reply *msg = reply->message;
  396. unsigned short service_rc, service_rs;
  397. unsigned int reply_len, pad_len;
  398. char *data;
  399. service_rc = msg->cprbx.ccp_rtcode;
  400. if (unlikely(service_rc != 0)) {
  401. service_rs = msg->cprbx.ccp_rscode;
  402. if (service_rc == 8 && service_rs == 66)
  403. return -EINVAL;
  404. if (service_rc == 8 && service_rs == 65)
  405. return -EINVAL;
  406. if (service_rc == 8 && service_rs == 770)
  407. return -EINVAL;
  408. if (service_rc == 8 && service_rs == 783) {
  409. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  410. return -EAGAIN;
  411. }
  412. if (service_rc == 12 && service_rs == 769)
  413. return -EINVAL;
  414. if (service_rc == 8 && service_rs == 72)
  415. return -EINVAL;
  416. zdev->online = 0;
  417. return -EAGAIN; /* repeat the request on a different device. */
  418. }
  419. data = msg->text;
  420. reply_len = msg->length - 2;
  421. if (reply_len > outputdatalength)
  422. return -EINVAL;
  423. /*
  424. * For all encipher requests, the length of the ciphertext (reply_len)
  425. * will always equal the modulus length. For MEX decipher requests
  426. * the output needs to get padded. Minimum pad size is 10.
  427. *
  428. * Currently, the cases where padding will be added is for:
  429. * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
  430. * ZERO-PAD and CRT is only supported for PKD requests)
  431. * - PCICC, always
  432. */
  433. pad_len = outputdatalength - reply_len;
  434. if (pad_len > 0) {
  435. if (pad_len < 10)
  436. return -EINVAL;
  437. /* 'restore' padding left in the PCICC/PCIXCC card. */
  438. if (copy_to_user(outputdata, static_pad, pad_len - 1))
  439. return -EFAULT;
  440. if (put_user(0, outputdata + pad_len - 1))
  441. return -EFAULT;
  442. }
  443. /* Copy the crypto response to user space. */
  444. if (copy_to_user(outputdata + pad_len, data, reply_len))
  445. return -EFAULT;
  446. return 0;
  447. }
  448. /**
  449. * Copy results from a type 86 XCRB reply message back to user space.
  450. *
  451. * @zdev: crypto device pointer
  452. * @reply: reply AP message.
  453. * @xcRB: pointer to XCRB
  454. *
  455. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  456. */
  457. static int convert_type86_xcrb(struct zcrypt_device *zdev,
  458. struct ap_message *reply,
  459. struct ica_xcRB *xcRB)
  460. {
  461. struct type86_fmt2_msg *msg = reply->message;
  462. char *data = reply->message;
  463. /* Copy CPRB to user */
  464. if (copy_to_user(xcRB->reply_control_blk_addr,
  465. data + msg->fmt2.offset1, msg->fmt2.count1))
  466. return -EFAULT;
  467. xcRB->reply_control_blk_length = msg->fmt2.count1;
  468. /* Copy data buffer to user */
  469. if (msg->fmt2.count2)
  470. if (copy_to_user(xcRB->reply_data_addr,
  471. data + msg->fmt2.offset2, msg->fmt2.count2))
  472. return -EFAULT;
  473. xcRB->reply_data_length = msg->fmt2.count2;
  474. return 0;
  475. }
  476. static int convert_type86_rng(struct zcrypt_device *zdev,
  477. struct ap_message *reply,
  478. char *buffer)
  479. {
  480. struct {
  481. struct type86_hdr hdr;
  482. struct type86_fmt2_ext fmt2;
  483. struct CPRBX cprbx;
  484. } __attribute__((packed)) *msg = reply->message;
  485. char *data = reply->message;
  486. if (msg->cprbx.ccp_rtcode != 0 || msg->cprbx.ccp_rscode != 0)
  487. return -EINVAL;
  488. memcpy(buffer, data + msg->fmt2.offset2, msg->fmt2.count2);
  489. return msg->fmt2.count2;
  490. }
  491. static int convert_response_ica(struct zcrypt_device *zdev,
  492. struct ap_message *reply,
  493. char __user *outputdata,
  494. unsigned int outputdatalength)
  495. {
  496. struct type86x_reply *msg = reply->message;
  497. /* Response type byte is the second byte in the response. */
  498. switch (((unsigned char *) reply->message)[1]) {
  499. case TYPE82_RSP_CODE:
  500. case TYPE88_RSP_CODE:
  501. return convert_error(zdev, reply);
  502. case TYPE86_RSP_CODE:
  503. if (msg->cprbx.ccp_rtcode &&
  504. (msg->cprbx.ccp_rscode == 0x14f) &&
  505. (outputdatalength > 256)) {
  506. if (zdev->max_exp_bit_length <= 17) {
  507. zdev->max_exp_bit_length = 17;
  508. return -EAGAIN;
  509. } else
  510. return -EINVAL;
  511. }
  512. if (msg->hdr.reply_code)
  513. return convert_error(zdev, reply);
  514. if (msg->cprbx.cprb_ver_id == 0x02)
  515. return convert_type86_ica(zdev, reply,
  516. outputdata, outputdatalength);
  517. /* Fall through, no break, incorrect cprb version is an unknown
  518. * response */
  519. default: /* Unknown response type, this should NEVER EVER happen */
  520. zdev->online = 0;
  521. return -EAGAIN; /* repeat the request on a different device. */
  522. }
  523. }
  524. static int convert_response_xcrb(struct zcrypt_device *zdev,
  525. struct ap_message *reply,
  526. struct ica_xcRB *xcRB)
  527. {
  528. struct type86x_reply *msg = reply->message;
  529. /* Response type byte is the second byte in the response. */
  530. switch (((unsigned char *) reply->message)[1]) {
  531. case TYPE82_RSP_CODE:
  532. case TYPE88_RSP_CODE:
  533. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  534. return convert_error(zdev, reply);
  535. case TYPE86_RSP_CODE:
  536. if (msg->hdr.reply_code) {
  537. memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
  538. return convert_error(zdev, reply);
  539. }
  540. if (msg->cprbx.cprb_ver_id == 0x02)
  541. return convert_type86_xcrb(zdev, reply, xcRB);
  542. /* Fall through, no break, incorrect cprb version is an unknown
  543. * response */
  544. default: /* Unknown response type, this should NEVER EVER happen */
  545. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  546. zdev->online = 0;
  547. return -EAGAIN; /* repeat the request on a different device. */
  548. }
  549. }
  550. static int convert_response_rng(struct zcrypt_device *zdev,
  551. struct ap_message *reply,
  552. char *data)
  553. {
  554. struct type86x_reply *msg = reply->message;
  555. switch (msg->hdr.type) {
  556. case TYPE82_RSP_CODE:
  557. case TYPE88_RSP_CODE:
  558. return -EINVAL;
  559. case TYPE86_RSP_CODE:
  560. if (msg->hdr.reply_code)
  561. return -EINVAL;
  562. if (msg->cprbx.cprb_ver_id == 0x02)
  563. return convert_type86_rng(zdev, reply, data);
  564. /* Fall through, no break, incorrect cprb version is an unknown
  565. * response */
  566. default: /* Unknown response type, this should NEVER EVER happen */
  567. zdev->online = 0;
  568. return -EAGAIN; /* repeat the request on a different device. */
  569. }
  570. }
  571. /**
  572. * This function is called from the AP bus code after a crypto request
  573. * "msg" has finished with the reply message "reply".
  574. * It is called from tasklet context.
  575. * @ap_dev: pointer to the AP device
  576. * @msg: pointer to the AP message
  577. * @reply: pointer to the AP reply message
  578. */
  579. static void zcrypt_pcixcc_receive(struct ap_device *ap_dev,
  580. struct ap_message *msg,
  581. struct ap_message *reply)
  582. {
  583. static struct error_hdr error_reply = {
  584. .type = TYPE82_RSP_CODE,
  585. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  586. };
  587. struct response_type *resp_type =
  588. (struct response_type *) msg->private;
  589. struct type86x_reply *t86r;
  590. int length;
  591. /* Copy the reply message to the request message buffer. */
  592. if (IS_ERR(reply)) {
  593. memcpy(msg->message, &error_reply, sizeof(error_reply));
  594. goto out;
  595. }
  596. t86r = reply->message;
  597. if (t86r->hdr.type == TYPE86_RSP_CODE &&
  598. t86r->cprbx.cprb_ver_id == 0x02) {
  599. switch (resp_type->type) {
  600. case PCIXCC_RESPONSE_TYPE_ICA:
  601. length = sizeof(struct type86x_reply)
  602. + t86r->length - 2;
  603. length = min(PCIXCC_MAX_ICA_RESPONSE_SIZE, length);
  604. memcpy(msg->message, reply->message, length);
  605. break;
  606. case PCIXCC_RESPONSE_TYPE_XCRB:
  607. length = t86r->fmt2.offset2 + t86r->fmt2.count2;
  608. length = min(PCIXCC_MAX_XCRB_MESSAGE_SIZE, length);
  609. memcpy(msg->message, reply->message, length);
  610. break;
  611. default:
  612. memcpy(msg->message, &error_reply, sizeof error_reply);
  613. }
  614. } else
  615. memcpy(msg->message, reply->message, sizeof error_reply);
  616. out:
  617. complete(&(resp_type->work));
  618. }
  619. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  620. /**
  621. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  622. * device to handle a modexpo request.
  623. * @zdev: pointer to zcrypt_device structure that identifies the
  624. * PCIXCC/CEX2C device to the request distributor
  625. * @mex: pointer to the modexpo request buffer
  626. */
  627. static long zcrypt_pcixcc_modexpo(struct zcrypt_device *zdev,
  628. struct ica_rsa_modexpo *mex)
  629. {
  630. struct ap_message ap_msg;
  631. struct response_type resp_type = {
  632. .type = PCIXCC_RESPONSE_TYPE_ICA,
  633. };
  634. int rc;
  635. ap_init_message(&ap_msg);
  636. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  637. if (!ap_msg.message)
  638. return -ENOMEM;
  639. ap_msg.receive = zcrypt_pcixcc_receive;
  640. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  641. atomic_inc_return(&zcrypt_step);
  642. ap_msg.private = &resp_type;
  643. rc = ICAMEX_msg_to_type6MEX_msgX(zdev, &ap_msg, mex);
  644. if (rc)
  645. goto out_free;
  646. init_completion(&resp_type.work);
  647. ap_queue_message(zdev->ap_dev, &ap_msg);
  648. rc = wait_for_completion_interruptible(&resp_type.work);
  649. if (rc == 0)
  650. rc = convert_response_ica(zdev, &ap_msg, mex->outputdata,
  651. mex->outputdatalength);
  652. else
  653. /* Signal pending. */
  654. ap_cancel_message(zdev->ap_dev, &ap_msg);
  655. out_free:
  656. free_page((unsigned long) ap_msg.message);
  657. return rc;
  658. }
  659. /**
  660. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  661. * device to handle a modexpo_crt request.
  662. * @zdev: pointer to zcrypt_device structure that identifies the
  663. * PCIXCC/CEX2C device to the request distributor
  664. * @crt: pointer to the modexpoc_crt request buffer
  665. */
  666. static long zcrypt_pcixcc_modexpo_crt(struct zcrypt_device *zdev,
  667. struct ica_rsa_modexpo_crt *crt)
  668. {
  669. struct ap_message ap_msg;
  670. struct response_type resp_type = {
  671. .type = PCIXCC_RESPONSE_TYPE_ICA,
  672. };
  673. int rc;
  674. ap_init_message(&ap_msg);
  675. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  676. if (!ap_msg.message)
  677. return -ENOMEM;
  678. ap_msg.receive = zcrypt_pcixcc_receive;
  679. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  680. atomic_inc_return(&zcrypt_step);
  681. ap_msg.private = &resp_type;
  682. rc = ICACRT_msg_to_type6CRT_msgX(zdev, &ap_msg, crt);
  683. if (rc)
  684. goto out_free;
  685. init_completion(&resp_type.work);
  686. ap_queue_message(zdev->ap_dev, &ap_msg);
  687. rc = wait_for_completion_interruptible(&resp_type.work);
  688. if (rc == 0)
  689. rc = convert_response_ica(zdev, &ap_msg, crt->outputdata,
  690. crt->outputdatalength);
  691. else
  692. /* Signal pending. */
  693. ap_cancel_message(zdev->ap_dev, &ap_msg);
  694. out_free:
  695. free_page((unsigned long) ap_msg.message);
  696. return rc;
  697. }
  698. /**
  699. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  700. * device to handle a send_cprb request.
  701. * @zdev: pointer to zcrypt_device structure that identifies the
  702. * PCIXCC/CEX2C device to the request distributor
  703. * @xcRB: pointer to the send_cprb request buffer
  704. */
  705. static long zcrypt_pcixcc_send_cprb(struct zcrypt_device *zdev,
  706. struct ica_xcRB *xcRB)
  707. {
  708. struct ap_message ap_msg;
  709. struct response_type resp_type = {
  710. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  711. };
  712. int rc;
  713. ap_init_message(&ap_msg);
  714. ap_msg.message = kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  715. if (!ap_msg.message)
  716. return -ENOMEM;
  717. ap_msg.receive = zcrypt_pcixcc_receive;
  718. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  719. atomic_inc_return(&zcrypt_step);
  720. ap_msg.private = &resp_type;
  721. rc = XCRB_msg_to_type6CPRB_msgX(zdev, &ap_msg, xcRB);
  722. if (rc)
  723. goto out_free;
  724. init_completion(&resp_type.work);
  725. ap_queue_message(zdev->ap_dev, &ap_msg);
  726. rc = wait_for_completion_interruptible(&resp_type.work);
  727. if (rc == 0)
  728. rc = convert_response_xcrb(zdev, &ap_msg, xcRB);
  729. else
  730. /* Signal pending. */
  731. ap_cancel_message(zdev->ap_dev, &ap_msg);
  732. out_free:
  733. kzfree(ap_msg.message);
  734. return rc;
  735. }
  736. /**
  737. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  738. * device to generate random data.
  739. * @zdev: pointer to zcrypt_device structure that identifies the
  740. * PCIXCC/CEX2C device to the request distributor
  741. * @buffer: pointer to a memory page to return random data
  742. */
  743. static long zcrypt_pcixcc_rng(struct zcrypt_device *zdev,
  744. char *buffer)
  745. {
  746. struct ap_message ap_msg;
  747. struct response_type resp_type = {
  748. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  749. };
  750. int rc;
  751. ap_init_message(&ap_msg);
  752. ap_msg.message = kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  753. if (!ap_msg.message)
  754. return -ENOMEM;
  755. ap_msg.receive = zcrypt_pcixcc_receive;
  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_XCRB_MESSAGE_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. zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  970. } else {
  971. zdev->type_string = "PCIXCC_MCL3";
  972. zdev->speed_rating = PCIXCC_MCL3_SPEED_RATING;
  973. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  974. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  975. zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  976. }
  977. break;
  978. case AP_DEVICE_TYPE_CEX2C:
  979. zdev->user_space_type = ZCRYPT_CEX2C;
  980. zdev->type_string = "CEX2C";
  981. zdev->speed_rating = CEX2C_SPEED_RATING;
  982. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  983. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  984. zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
  985. break;
  986. case AP_DEVICE_TYPE_CEX3C:
  987. zdev->user_space_type = ZCRYPT_CEX3C;
  988. zdev->type_string = "CEX3C";
  989. zdev->speed_rating = CEX3C_SPEED_RATING;
  990. zdev->min_mod_size = CEX3C_MIN_MOD_SIZE;
  991. zdev->max_mod_size = CEX3C_MAX_MOD_SIZE;
  992. zdev->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
  993. break;
  994. default:
  995. goto out_free;
  996. }
  997. rc = zcrypt_pcixcc_rng_supported(ap_dev);
  998. if (rc < 0) {
  999. zcrypt_device_free(zdev);
  1000. return rc;
  1001. }
  1002. if (rc)
  1003. zdev->ops = &zcrypt_pcixcc_with_rng_ops;
  1004. else
  1005. zdev->ops = &zcrypt_pcixcc_ops;
  1006. ap_dev->reply = &zdev->reply;
  1007. ap_dev->private = zdev;
  1008. rc = zcrypt_device_register(zdev);
  1009. if (rc)
  1010. goto out_free;
  1011. return 0;
  1012. out_free:
  1013. ap_dev->private = NULL;
  1014. zcrypt_device_free(zdev);
  1015. return rc;
  1016. }
  1017. /**
  1018. * This is called to remove the extended PCIXCC/CEX2C driver information
  1019. * if an AP device is removed.
  1020. */
  1021. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev)
  1022. {
  1023. struct zcrypt_device *zdev = ap_dev->private;
  1024. zcrypt_device_unregister(zdev);
  1025. }
  1026. int __init zcrypt_pcixcc_init(void)
  1027. {
  1028. return ap_driver_register(&zcrypt_pcixcc_driver, THIS_MODULE, "pcixcc");
  1029. }
  1030. void zcrypt_pcixcc_exit(void)
  1031. {
  1032. ap_driver_unregister(&zcrypt_pcixcc_driver);
  1033. }
  1034. module_init(zcrypt_pcixcc_init);
  1035. module_exit(zcrypt_pcixcc_exit);