zcrypt_pcixcc.c 33 KB

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