zcrypt_pcixcc.c 34 KB

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