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