zcrypt_pcixcc.c 33 KB

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