| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 1 | /* | 
 | 2 |  *  linux/drivers/s390/crypto/zcrypt_cex2a.c | 
 | 3 |  * | 
| Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 4 |  *  zcrypt 2.1.0 | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 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 |  | 
 | 29 | #include <linux/module.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 31 | #include <linux/init.h> | 
 | 32 | #include <linux/err.h> | 
 | 33 | #include <asm/atomic.h> | 
 | 34 | #include <asm/uaccess.h> | 
 | 35 |  | 
 | 36 | #include "ap_bus.h" | 
 | 37 | #include "zcrypt_api.h" | 
 | 38 | #include "zcrypt_error.h" | 
 | 39 | #include "zcrypt_cex2a.h" | 
 | 40 |  | 
 | 41 | #define CEX2A_MIN_MOD_SIZE	  1	/*    8 bits	*/ | 
 | 42 | #define CEX2A_MAX_MOD_SIZE	256	/* 2048 bits	*/ | 
| Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 43 | #define CEX3A_MIN_MOD_SIZE	CEX2A_MIN_MOD_SIZE | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 44 | #define CEX3A_MAX_MOD_SIZE	512	/* 4096 bits	*/ | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 45 |  | 
 | 46 | #define CEX2A_SPEED_RATING	970 | 
| Felix Beck | 7a6f5cd | 2009-12-07 12:51:59 +0100 | [diff] [blame] | 47 | #define CEX3A_SPEED_RATING	900 /* Fixme: Needs finetuning */ | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 48 |  | 
 | 49 | #define CEX2A_MAX_MESSAGE_SIZE	0x390	/* sizeof(struct type50_crb2_msg)    */ | 
 | 50 | #define CEX2A_MAX_RESPONSE_SIZE 0x110	/* max outputdatalength + type80_hdr */ | 
 | 51 |  | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 52 | #define CEX3A_MAX_RESPONSE_SIZE	0x210	/* 512 bit modulus | 
 | 53 | 					 * (max outputdatalength) + | 
 | 54 | 					 * type80_hdr*/ | 
 | 55 | #define CEX3A_MAX_MESSAGE_SIZE	sizeof(struct type50_crb3_msg) | 
| Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 56 |  | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 57 | #define CEX2A_CLEANUP_TIME	(15*HZ) | 
| Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 58 | #define CEX3A_CLEANUP_TIME	CEX2A_CLEANUP_TIME | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 59 |  | 
 | 60 | static struct ap_device_id zcrypt_cex2a_ids[] = { | 
 | 61 | 	{ AP_DEVICE(AP_DEVICE_TYPE_CEX2A) }, | 
| Felix Beck | ffda4f7 | 2009-12-07 12:51:56 +0100 | [diff] [blame] | 62 | 	{ AP_DEVICE(AP_DEVICE_TYPE_CEX3A) }, | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 63 | 	{ /* end of list */ }, | 
 | 64 | }; | 
 | 65 |  | 
 | 66 | #ifndef CONFIG_ZCRYPT_MONOLITHIC | 
 | 67 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids); | 
 | 68 | MODULE_AUTHOR("IBM Corporation"); | 
 | 69 | MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " | 
 | 70 | 		   "Copyright 2001, 2006 IBM Corporation"); | 
 | 71 | MODULE_LICENSE("GPL"); | 
 | 72 | #endif | 
 | 73 |  | 
 | 74 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev); | 
 | 75 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev); | 
 | 76 | static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *, | 
 | 77 | 				 struct ap_message *); | 
 | 78 |  | 
 | 79 | static struct ap_driver zcrypt_cex2a_driver = { | 
 | 80 | 	.probe = zcrypt_cex2a_probe, | 
 | 81 | 	.remove = zcrypt_cex2a_remove, | 
 | 82 | 	.receive = zcrypt_cex2a_receive, | 
 | 83 | 	.ids = zcrypt_cex2a_ids, | 
| Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 84 | 	.request_timeout = CEX2A_CLEANUP_TIME, | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 85 | }; | 
 | 86 |  | 
 | 87 | /** | 
 | 88 |  * Convert a ICAMEX message to a type50 MEX message. | 
 | 89 |  * | 
 | 90 |  * @zdev: crypto device pointer | 
 | 91 |  * @zreq: crypto request pointer | 
 | 92 |  * @mex: pointer to user input data | 
 | 93 |  * | 
 | 94 |  * Returns 0 on success or -EFAULT. | 
 | 95 |  */ | 
 | 96 | static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev, | 
 | 97 | 				       struct ap_message *ap_msg, | 
 | 98 | 				       struct ica_rsa_modexpo *mex) | 
 | 99 | { | 
 | 100 | 	unsigned char *mod, *exp, *inp; | 
 | 101 | 	int mod_len; | 
 | 102 |  | 
 | 103 | 	mod_len = mex->inputdatalength; | 
 | 104 |  | 
 | 105 | 	if (mod_len <= 128) { | 
 | 106 | 		struct type50_meb1_msg *meb1 = ap_msg->message; | 
 | 107 | 		memset(meb1, 0, sizeof(*meb1)); | 
 | 108 | 		ap_msg->length = sizeof(*meb1); | 
 | 109 | 		meb1->header.msg_type_code = TYPE50_TYPE_CODE; | 
 | 110 | 		meb1->header.msg_len = sizeof(*meb1); | 
 | 111 | 		meb1->keyblock_type = TYPE50_MEB1_FMT; | 
 | 112 | 		mod = meb1->modulus + sizeof(meb1->modulus) - mod_len; | 
 | 113 | 		exp = meb1->exponent + sizeof(meb1->exponent) - mod_len; | 
 | 114 | 		inp = meb1->message + sizeof(meb1->message) - mod_len; | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 115 | 	} else if (mod_len <= 256) { | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 116 | 		struct type50_meb2_msg *meb2 = ap_msg->message; | 
 | 117 | 		memset(meb2, 0, sizeof(*meb2)); | 
 | 118 | 		ap_msg->length = sizeof(*meb2); | 
 | 119 | 		meb2->header.msg_type_code = TYPE50_TYPE_CODE; | 
 | 120 | 		meb2->header.msg_len = sizeof(*meb2); | 
 | 121 | 		meb2->keyblock_type = TYPE50_MEB2_FMT; | 
 | 122 | 		mod = meb2->modulus + sizeof(meb2->modulus) - mod_len; | 
 | 123 | 		exp = meb2->exponent + sizeof(meb2->exponent) - mod_len; | 
 | 124 | 		inp = meb2->message + sizeof(meb2->message) - mod_len; | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 125 | 	} else { | 
 | 126 | 		/* mod_len > 256 = 4096 bit RSA Key */ | 
 | 127 | 		struct type50_meb3_msg *meb3 = ap_msg->message; | 
 | 128 | 		memset(meb3, 0, sizeof(*meb3)); | 
 | 129 | 		ap_msg->length = sizeof(*meb3); | 
 | 130 | 		meb3->header.msg_type_code = TYPE50_TYPE_CODE; | 
 | 131 | 		meb3->header.msg_len = sizeof(*meb3); | 
 | 132 | 		meb3->keyblock_type = TYPE50_MEB3_FMT; | 
 | 133 | 		mod = meb3->modulus + sizeof(meb3->modulus) - mod_len; | 
 | 134 | 		exp = meb3->exponent + sizeof(meb3->exponent) - mod_len; | 
 | 135 | 		inp = meb3->message + sizeof(meb3->message) - mod_len; | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 136 | 	} | 
 | 137 |  | 
 | 138 | 	if (copy_from_user(mod, mex->n_modulus, mod_len) || | 
 | 139 | 	    copy_from_user(exp, mex->b_key, mod_len) || | 
 | 140 | 	    copy_from_user(inp, mex->inputdata, mod_len)) | 
 | 141 | 		return -EFAULT; | 
 | 142 | 	return 0; | 
 | 143 | } | 
 | 144 |  | 
 | 145 | /** | 
 | 146 |  * Convert a ICACRT message to a type50 CRT message. | 
 | 147 |  * | 
 | 148 |  * @zdev: crypto device pointer | 
 | 149 |  * @zreq: crypto request pointer | 
 | 150 |  * @crt: pointer to user input data | 
 | 151 |  * | 
 | 152 |  * Returns 0 on success or -EFAULT. | 
 | 153 |  */ | 
 | 154 | static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev, | 
 | 155 | 				       struct ap_message *ap_msg, | 
 | 156 | 				       struct ica_rsa_modexpo_crt *crt) | 
 | 157 | { | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 158 | 	int mod_len, short_len, long_len, long_offset, limit; | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 159 | 	unsigned char *p, *q, *dp, *dq, *u, *inp; | 
 | 160 |  | 
 | 161 | 	mod_len = crt->inputdatalength; | 
 | 162 | 	short_len = mod_len / 2; | 
 | 163 | 	long_len = mod_len / 2 + 8; | 
 | 164 |  | 
 | 165 | 	/* | 
 | 166 | 	 * CEX2A cannot handle p, dp, or U > 128 bytes. | 
 | 167 | 	 * If we have one of these, we need to do extra checking. | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 168 | 	 * For CEX3A the limit is 256 bytes. | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 169 | 	 */ | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 170 | 	if (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE) | 
 | 171 | 		limit = 256; | 
 | 172 | 	else | 
 | 173 | 		limit = 128; | 
 | 174 |  | 
 | 175 | 	if (long_len > limit) { | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 176 | 		/* | 
 | 177 | 		 * zcrypt_rsa_crt already checked for the leading | 
 | 178 | 		 * zeroes of np_prime, bp_key and u_mult_inc. | 
 | 179 | 		 */ | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 180 | 		long_offset = long_len - limit; | 
 | 181 | 		long_len = limit; | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 182 | 	} else | 
 | 183 | 		long_offset = 0; | 
 | 184 |  | 
 | 185 | 	/* | 
 | 186 | 	 * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use | 
 | 187 | 	 * the larger message structure. | 
 | 188 | 	 */ | 
 | 189 | 	if (long_len <= 64) { | 
 | 190 | 		struct type50_crb1_msg *crb1 = ap_msg->message; | 
 | 191 | 		memset(crb1, 0, sizeof(*crb1)); | 
 | 192 | 		ap_msg->length = sizeof(*crb1); | 
 | 193 | 		crb1->header.msg_type_code = TYPE50_TYPE_CODE; | 
 | 194 | 		crb1->header.msg_len = sizeof(*crb1); | 
 | 195 | 		crb1->keyblock_type = TYPE50_CRB1_FMT; | 
 | 196 | 		p = crb1->p + sizeof(crb1->p) - long_len; | 
 | 197 | 		q = crb1->q + sizeof(crb1->q) - short_len; | 
 | 198 | 		dp = crb1->dp + sizeof(crb1->dp) - long_len; | 
 | 199 | 		dq = crb1->dq + sizeof(crb1->dq) - short_len; | 
 | 200 | 		u = crb1->u + sizeof(crb1->u) - long_len; | 
 | 201 | 		inp = crb1->message + sizeof(crb1->message) - mod_len; | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 202 | 	} else if (long_len <= 128) { | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 203 | 		struct type50_crb2_msg *crb2 = ap_msg->message; | 
 | 204 | 		memset(crb2, 0, sizeof(*crb2)); | 
 | 205 | 		ap_msg->length = sizeof(*crb2); | 
 | 206 | 		crb2->header.msg_type_code = TYPE50_TYPE_CODE; | 
 | 207 | 		crb2->header.msg_len = sizeof(*crb2); | 
 | 208 | 		crb2->keyblock_type = TYPE50_CRB2_FMT; | 
 | 209 | 		p = crb2->p + sizeof(crb2->p) - long_len; | 
 | 210 | 		q = crb2->q + sizeof(crb2->q) - short_len; | 
 | 211 | 		dp = crb2->dp + sizeof(crb2->dp) - long_len; | 
 | 212 | 		dq = crb2->dq + sizeof(crb2->dq) - short_len; | 
 | 213 | 		u = crb2->u + sizeof(crb2->u) - long_len; | 
 | 214 | 		inp = crb2->message + sizeof(crb2->message) - mod_len; | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 215 | 	} else { | 
 | 216 | 		/* long_len >= 256 */ | 
 | 217 | 		struct type50_crb3_msg *crb3 = ap_msg->message; | 
 | 218 | 		memset(crb3, 0, sizeof(*crb3)); | 
 | 219 | 		ap_msg->length = sizeof(*crb3); | 
 | 220 | 		crb3->header.msg_type_code = TYPE50_TYPE_CODE; | 
 | 221 | 		crb3->header.msg_len = sizeof(*crb3); | 
 | 222 | 		crb3->keyblock_type = TYPE50_CRB3_FMT; | 
 | 223 | 		p = crb3->p + sizeof(crb3->p) - long_len; | 
 | 224 | 		q = crb3->q + sizeof(crb3->q) - short_len; | 
 | 225 | 		dp = crb3->dp + sizeof(crb3->dp) - long_len; | 
 | 226 | 		dq = crb3->dq + sizeof(crb3->dq) - short_len; | 
 | 227 | 		u = crb3->u + sizeof(crb3->u) - long_len; | 
 | 228 | 		inp = crb3->message + sizeof(crb3->message) - mod_len; | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 229 | 	} | 
 | 230 |  | 
 | 231 | 	if (copy_from_user(p, crt->np_prime + long_offset, long_len) || | 
 | 232 | 	    copy_from_user(q, crt->nq_prime, short_len) || | 
 | 233 | 	    copy_from_user(dp, crt->bp_key + long_offset, long_len) || | 
 | 234 | 	    copy_from_user(dq, crt->bq_key, short_len) || | 
 | 235 | 	    copy_from_user(u, crt->u_mult_inv + long_offset, long_len) || | 
 | 236 | 	    copy_from_user(inp, crt->inputdata, mod_len)) | 
 | 237 | 		return -EFAULT; | 
 | 238 |  | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 239 | 	return 0; | 
 | 240 | } | 
 | 241 |  | 
 | 242 | /** | 
 | 243 |  * Copy results from a type 80 reply message back to user space. | 
 | 244 |  * | 
 | 245 |  * @zdev: crypto device pointer | 
 | 246 |  * @reply: reply AP message. | 
 | 247 |  * @data: pointer to user output data | 
 | 248 |  * @length: size of user output data | 
 | 249 |  * | 
 | 250 |  * Returns 0 on success or -EFAULT. | 
 | 251 |  */ | 
 | 252 | static int convert_type80(struct zcrypt_device *zdev, | 
 | 253 | 			  struct ap_message *reply, | 
 | 254 | 			  char __user *outputdata, | 
 | 255 | 			  unsigned int outputdatalength) | 
 | 256 | { | 
 | 257 | 	struct type80_hdr *t80h = reply->message; | 
 | 258 | 	unsigned char *data; | 
 | 259 |  | 
 | 260 | 	if (t80h->len < sizeof(*t80h) + outputdatalength) { | 
 | 261 | 		/* The result is too short, the CEX2A card may not do that.. */ | 
 | 262 | 		zdev->online = 0; | 
 | 263 | 		return -EAGAIN;	/* repeat the request on a different device. */ | 
 | 264 | 	} | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 265 | 	if (zdev->user_space_type == ZCRYPT_CEX2A) | 
 | 266 | 		BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE); | 
 | 267 | 	else | 
 | 268 | 		BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE); | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 269 | 	data = reply->message + t80h->len - outputdatalength; | 
 | 270 | 	if (copy_to_user(outputdata, data, outputdatalength)) | 
 | 271 | 		return -EFAULT; | 
 | 272 | 	return 0; | 
 | 273 | } | 
 | 274 |  | 
 | 275 | static int convert_response(struct zcrypt_device *zdev, | 
 | 276 | 			    struct ap_message *reply, | 
 | 277 | 			    char __user *outputdata, | 
 | 278 | 			    unsigned int outputdatalength) | 
 | 279 | { | 
 | 280 | 	/* Response type byte is the second byte in the response. */ | 
 | 281 | 	switch (((unsigned char *) reply->message)[1]) { | 
 | 282 | 	case TYPE82_RSP_CODE: | 
 | 283 | 	case TYPE88_RSP_CODE: | 
 | 284 | 		return convert_error(zdev, reply); | 
 | 285 | 	case TYPE80_RSP_CODE: | 
 | 286 | 		return convert_type80(zdev, reply, | 
 | 287 | 				      outputdata, outputdatalength); | 
 | 288 | 	default: /* Unknown response type, this should NEVER EVER happen */ | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 289 | 		zdev->online = 0; | 
 | 290 | 		return -EAGAIN;	/* repeat the request on a different device. */ | 
 | 291 | 	} | 
 | 292 | } | 
 | 293 |  | 
 | 294 | /** | 
 | 295 |  * This function is called from the AP bus code after a crypto request | 
 | 296 |  * "msg" has finished with the reply message "reply". | 
 | 297 |  * It is called from tasklet context. | 
 | 298 |  * @ap_dev: pointer to the AP device | 
 | 299 |  * @msg: pointer to the AP message | 
 | 300 |  * @reply: pointer to the AP reply message | 
 | 301 |  */ | 
 | 302 | static void zcrypt_cex2a_receive(struct ap_device *ap_dev, | 
 | 303 | 				 struct ap_message *msg, | 
 | 304 | 				 struct ap_message *reply) | 
 | 305 | { | 
 | 306 | 	static struct error_hdr error_reply = { | 
 | 307 | 		.type = TYPE82_RSP_CODE, | 
 | 308 | 		.reply_code = REP82_ERROR_MACHINE_FAILURE, | 
 | 309 | 	}; | 
| Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 310 | 	struct type80_hdr *t80h; | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 311 | 	int length; | 
 | 312 |  | 
 | 313 | 	/* Copy the reply message to the request message buffer. */ | 
| Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 314 | 	if (IS_ERR(reply)) { | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 315 | 		memcpy(msg->message, &error_reply, sizeof(error_reply)); | 
| Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 316 | 		goto out; | 
 | 317 | 	} | 
 | 318 | 	t80h = reply->message; | 
 | 319 | 	if (t80h->type == TYPE80_RSP_CODE) { | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 320 | 		if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A) | 
 | 321 | 			length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len); | 
 | 322 | 		else | 
 | 323 | 			length = min(CEX3A_MAX_RESPONSE_SIZE, (int) t80h->len); | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 324 | 		memcpy(msg->message, reply->message, length); | 
 | 325 | 	} else | 
 | 326 | 		memcpy(msg->message, reply->message, sizeof error_reply); | 
| Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 327 | out: | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 328 | 	complete((struct completion *) msg->private); | 
 | 329 | } | 
 | 330 |  | 
 | 331 | static atomic_t zcrypt_step = ATOMIC_INIT(0); | 
 | 332 |  | 
 | 333 | /** | 
 | 334 |  * The request distributor calls this function if it picked the CEX2A | 
 | 335 |  * device to handle a modexpo request. | 
 | 336 |  * @zdev: pointer to zcrypt_device structure that identifies the | 
 | 337 |  *	  CEX2A device to the request distributor | 
 | 338 |  * @mex: pointer to the modexpo request buffer | 
 | 339 |  */ | 
 | 340 | static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev, | 
 | 341 | 				 struct ica_rsa_modexpo *mex) | 
 | 342 | { | 
 | 343 | 	struct ap_message ap_msg; | 
 | 344 | 	struct completion work; | 
 | 345 | 	int rc; | 
 | 346 |  | 
| Felix Beck | 468ffdd | 2009-12-07 12:51:54 +0100 | [diff] [blame] | 347 | 	ap_init_message(&ap_msg); | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 348 | 	if (zdev->user_space_type == ZCRYPT_CEX2A) | 
 | 349 | 		ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL); | 
 | 350 | 	else | 
 | 351 | 		ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL); | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 352 | 	if (!ap_msg.message) | 
 | 353 | 		return -ENOMEM; | 
 | 354 | 	ap_msg.psmid = (((unsigned long long) current->pid) << 32) + | 
 | 355 | 				atomic_inc_return(&zcrypt_step); | 
 | 356 | 	ap_msg.private = &work; | 
 | 357 | 	rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex); | 
 | 358 | 	if (rc) | 
 | 359 | 		goto out_free; | 
 | 360 | 	init_completion(&work); | 
 | 361 | 	ap_queue_message(zdev->ap_dev, &ap_msg); | 
| Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 362 | 	rc = wait_for_completion_interruptible(&work); | 
 | 363 | 	if (rc == 0) | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 364 | 		rc = convert_response(zdev, &ap_msg, mex->outputdata, | 
 | 365 | 				      mex->outputdatalength); | 
| Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 366 | 	else | 
 | 367 | 		/* Signal pending. */ | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 368 | 		ap_cancel_message(zdev->ap_dev, &ap_msg); | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 369 | out_free: | 
 | 370 | 	kfree(ap_msg.message); | 
 | 371 | 	return rc; | 
 | 372 | } | 
 | 373 |  | 
 | 374 | /** | 
 | 375 |  * The request distributor calls this function if it picked the CEX2A | 
 | 376 |  * device to handle a modexpo_crt request. | 
 | 377 |  * @zdev: pointer to zcrypt_device structure that identifies the | 
 | 378 |  *	  CEX2A device to the request distributor | 
 | 379 |  * @crt: pointer to the modexpoc_crt request buffer | 
 | 380 |  */ | 
 | 381 | static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev, | 
 | 382 | 				     struct ica_rsa_modexpo_crt *crt) | 
 | 383 | { | 
 | 384 | 	struct ap_message ap_msg; | 
 | 385 | 	struct completion work; | 
 | 386 | 	int rc; | 
 | 387 |  | 
| Felix Beck | 468ffdd | 2009-12-07 12:51:54 +0100 | [diff] [blame] | 388 | 	ap_init_message(&ap_msg); | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 389 | 	if (zdev->user_space_type == ZCRYPT_CEX2A) | 
 | 390 | 		ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL); | 
 | 391 | 	else | 
 | 392 | 		ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL); | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 393 | 	if (!ap_msg.message) | 
 | 394 | 		return -ENOMEM; | 
 | 395 | 	ap_msg.psmid = (((unsigned long long) current->pid) << 32) + | 
 | 396 | 				atomic_inc_return(&zcrypt_step); | 
 | 397 | 	ap_msg.private = &work; | 
 | 398 | 	rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt); | 
 | 399 | 	if (rc) | 
 | 400 | 		goto out_free; | 
 | 401 | 	init_completion(&work); | 
 | 402 | 	ap_queue_message(zdev->ap_dev, &ap_msg); | 
| Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 403 | 	rc = wait_for_completion_interruptible(&work); | 
 | 404 | 	if (rc == 0) | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 405 | 		rc = convert_response(zdev, &ap_msg, crt->outputdata, | 
 | 406 | 				      crt->outputdatalength); | 
| Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 407 | 	else | 
 | 408 | 		/* Signal pending. */ | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 409 | 		ap_cancel_message(zdev->ap_dev, &ap_msg); | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 410 | out_free: | 
 | 411 | 	kfree(ap_msg.message); | 
 | 412 | 	return rc; | 
 | 413 | } | 
 | 414 |  | 
 | 415 | /** | 
 | 416 |  * The crypto operations for a CEX2A card. | 
 | 417 |  */ | 
 | 418 | static struct zcrypt_ops zcrypt_cex2a_ops = { | 
 | 419 | 	.rsa_modexpo = zcrypt_cex2a_modexpo, | 
 | 420 | 	.rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt, | 
 | 421 | }; | 
 | 422 |  | 
 | 423 | /** | 
 | 424 |  * Probe function for CEX2A cards. It always accepts the AP device | 
 | 425 |  * since the bus_match already checked the hardware type. | 
 | 426 |  * @ap_dev: pointer to the AP device. | 
 | 427 |  */ | 
 | 428 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev) | 
 | 429 | { | 
| Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 430 | 	struct zcrypt_device *zdev = NULL; | 
 | 431 | 	int rc = 0; | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 432 |  | 
| Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 433 | 	switch (ap_dev->device_type) { | 
 | 434 | 	case AP_DEVICE_TYPE_CEX2A: | 
 | 435 | 		zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE); | 
 | 436 | 		if (!zdev) | 
 | 437 | 			return -ENOMEM; | 
 | 438 | 		zdev->user_space_type = ZCRYPT_CEX2A; | 
 | 439 | 		zdev->type_string = "CEX2A"; | 
 | 440 | 		zdev->min_mod_size = CEX2A_MIN_MOD_SIZE; | 
 | 441 | 		zdev->max_mod_size = CEX2A_MAX_MOD_SIZE; | 
 | 442 | 		zdev->short_crt = 1; | 
 | 443 | 		zdev->speed_rating = CEX2A_SPEED_RATING; | 
| Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 444 | 		zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; | 
| Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 445 | 		break; | 
 | 446 | 	case AP_DEVICE_TYPE_CEX3A: | 
 | 447 | 		zdev = zcrypt_device_alloc(CEX3A_MAX_RESPONSE_SIZE); | 
 | 448 | 		if (!zdev) | 
 | 449 | 			return -ENOMEM; | 
 | 450 | 		zdev->user_space_type = ZCRYPT_CEX3A; | 
 | 451 | 		zdev->type_string = "CEX3A"; | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 452 | 		zdev->min_mod_size = CEX2A_MIN_MOD_SIZE; | 
 | 453 | 		zdev->max_mod_size = CEX2A_MAX_MOD_SIZE; | 
| Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 454 | 		zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; | 
 | 455 | 		if (ap_4096_commands_available(ap_dev->qid)) { | 
| Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 456 | 			zdev->max_mod_size = CEX3A_MAX_MOD_SIZE; | 
| Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 457 | 			zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE; | 
 | 458 | 		} | 
| Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 459 | 		zdev->short_crt = 1; | 
 | 460 | 		zdev->speed_rating = CEX3A_SPEED_RATING; | 
 | 461 | 		break; | 
 | 462 | 	} | 
 | 463 | 	if (zdev != NULL) { | 
 | 464 | 		zdev->ap_dev = ap_dev; | 
 | 465 | 		zdev->ops = &zcrypt_cex2a_ops; | 
 | 466 | 		zdev->online = 1; | 
 | 467 | 		ap_dev->reply = &zdev->reply; | 
 | 468 | 		ap_dev->private = zdev; | 
 | 469 | 		rc = zcrypt_device_register(zdev); | 
 | 470 | 	} | 
 | 471 | 	if (rc) { | 
 | 472 | 		ap_dev->private = NULL; | 
 | 473 | 		zcrypt_device_free(zdev); | 
 | 474 | 	} | 
| Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 475 | 	return rc; | 
 | 476 | } | 
 | 477 |  | 
 | 478 | /** | 
 | 479 |  * This is called to remove the extended CEX2A driver information | 
 | 480 |  * if an AP device is removed. | 
 | 481 |  */ | 
 | 482 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev) | 
 | 483 | { | 
 | 484 | 	struct zcrypt_device *zdev = ap_dev->private; | 
 | 485 |  | 
 | 486 | 	zcrypt_device_unregister(zdev); | 
 | 487 | } | 
 | 488 |  | 
 | 489 | int __init zcrypt_cex2a_init(void) | 
 | 490 | { | 
 | 491 | 	return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a"); | 
 | 492 | } | 
 | 493 |  | 
 | 494 | void __exit zcrypt_cex2a_exit(void) | 
 | 495 | { | 
 | 496 | 	ap_driver_unregister(&zcrypt_cex2a_driver); | 
 | 497 | } | 
 | 498 |  | 
 | 499 | #ifndef CONFIG_ZCRYPT_MONOLITHIC | 
 | 500 | module_init(zcrypt_cex2a_init); | 
 | 501 | module_exit(zcrypt_cex2a_exit); | 
 | 502 | #endif |