zcrypt_pcixcc.c 33 KB

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