Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[firefly-linux-kernel-4.4.55.git] / drivers / s390 / crypto / zcrypt_api.c
1 /*
2  *  zcrypt 2.1.0
3  *
4  *  Copyright IBM Corp. 2001, 2012
5  *  Author(s): Robert Burroughs
6  *             Eric Rossman (edrossma@us.ibm.com)
7  *             Cornelia Huck <cornelia.huck@de.ibm.com>
8  *
9  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
10  *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
11  *                                Ralph Wuerthner <rwuerthn@de.ibm.com>
12  *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.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/interrupt.h>
32 #include <linux/miscdevice.h>
33 #include <linux/fs.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/compat.h>
37 #include <linux/slab.h>
38 #include <linux/atomic.h>
39 #include <asm/uaccess.h>
40 #include <linux/hw_random.h>
41 #include <linux/debugfs.h>
42 #include <asm/debug.h>
43
44 #include "zcrypt_debug.h"
45 #include "zcrypt_api.h"
46
47 #include "zcrypt_msgtype6.h"
48
49 /*
50  * Module description.
51  */
52 MODULE_AUTHOR("IBM Corporation");
53 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
54                    "Copyright IBM Corp. 2001, 2012");
55 MODULE_LICENSE("GPL");
56
57 static int zcrypt_hwrng_seed = 1;
58 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, S_IRUSR|S_IRGRP);
59 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
60
61 static DEFINE_SPINLOCK(zcrypt_device_lock);
62 static LIST_HEAD(zcrypt_device_list);
63 static int zcrypt_device_count = 0;
64 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
65 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
66
67 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
68 EXPORT_SYMBOL(zcrypt_rescan_req);
69
70 static int zcrypt_rng_device_add(void);
71 static void zcrypt_rng_device_remove(void);
72
73 static DEFINE_SPINLOCK(zcrypt_ops_list_lock);
74 static LIST_HEAD(zcrypt_ops_list);
75
76 static debug_info_t *zcrypt_dbf_common;
77 static debug_info_t *zcrypt_dbf_devices;
78 static struct dentry *debugfs_root;
79
80 /*
81  * Device attributes common for all crypto devices.
82  */
83 static ssize_t zcrypt_type_show(struct device *dev,
84                                 struct device_attribute *attr, char *buf)
85 {
86         struct zcrypt_device *zdev = to_ap_dev(dev)->private;
87         return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
88 }
89
90 static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
91
92 static ssize_t zcrypt_online_show(struct device *dev,
93                                   struct device_attribute *attr, char *buf)
94 {
95         struct zcrypt_device *zdev = to_ap_dev(dev)->private;
96         return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
97 }
98
99 static ssize_t zcrypt_online_store(struct device *dev,
100                                    struct device_attribute *attr,
101                                    const char *buf, size_t count)
102 {
103         struct zcrypt_device *zdev = to_ap_dev(dev)->private;
104         int online;
105
106         if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
107                 return -EINVAL;
108         zdev->online = online;
109         ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dman", zdev->ap_dev->qid,
110                        zdev->online);
111         if (!online)
112                 ap_flush_queue(zdev->ap_dev);
113         return count;
114 }
115
116 static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
117
118 static struct attribute * zcrypt_device_attrs[] = {
119         &dev_attr_type.attr,
120         &dev_attr_online.attr,
121         NULL,
122 };
123
124 static struct attribute_group zcrypt_device_attr_group = {
125         .attrs = zcrypt_device_attrs,
126 };
127
128 /**
129  * Process a rescan of the transport layer.
130  *
131  * Returns 1, if the rescan has been processed, otherwise 0.
132  */
133 static inline int zcrypt_process_rescan(void)
134 {
135         if (atomic_read(&zcrypt_rescan_req)) {
136                 atomic_set(&zcrypt_rescan_req, 0);
137                 atomic_inc(&zcrypt_rescan_count);
138                 ap_bus_force_rescan();
139                 ZCRYPT_DBF_COMMON(DBF_INFO, "rescan%07d",
140                                   atomic_inc_return(&zcrypt_rescan_count));
141                 return 1;
142         }
143         return 0;
144 }
145
146 /**
147  * __zcrypt_increase_preference(): Increase preference of a crypto device.
148  * @zdev: Pointer the crypto device
149  *
150  * Move the device towards the head of the device list.
151  * Need to be called while holding the zcrypt device list lock.
152  * Note: cards with speed_rating of 0 are kept at the end of the list.
153  */
154 static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
155 {
156         struct zcrypt_device *tmp;
157         struct list_head *l;
158
159         if (zdev->speed_rating == 0)
160                 return;
161         for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
162                 tmp = list_entry(l, struct zcrypt_device, list);
163                 if ((tmp->request_count + 1) * tmp->speed_rating <=
164                     (zdev->request_count + 1) * zdev->speed_rating &&
165                     tmp->speed_rating != 0)
166                         break;
167         }
168         if (l == zdev->list.prev)
169                 return;
170         /* Move zdev behind l */
171         list_move(&zdev->list, l);
172 }
173
174 /**
175  * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
176  * @zdev: Pointer to a crypto device.
177  *
178  * Move the device towards the tail of the device list.
179  * Need to be called while holding the zcrypt device list lock.
180  * Note: cards with speed_rating of 0 are kept at the end of the list.
181  */
182 static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
183 {
184         struct zcrypt_device *tmp;
185         struct list_head *l;
186
187         if (zdev->speed_rating == 0)
188                 return;
189         for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
190                 tmp = list_entry(l, struct zcrypt_device, list);
191                 if ((tmp->request_count + 1) * tmp->speed_rating >
192                     (zdev->request_count + 1) * zdev->speed_rating ||
193                     tmp->speed_rating == 0)
194                         break;
195         }
196         if (l == zdev->list.next)
197                 return;
198         /* Move zdev before l */
199         list_move_tail(&zdev->list, l);
200 }
201
202 static void zcrypt_device_release(struct kref *kref)
203 {
204         struct zcrypt_device *zdev =
205                 container_of(kref, struct zcrypt_device, refcount);
206         zcrypt_device_free(zdev);
207 }
208
209 void zcrypt_device_get(struct zcrypt_device *zdev)
210 {
211         kref_get(&zdev->refcount);
212 }
213 EXPORT_SYMBOL(zcrypt_device_get);
214
215 int zcrypt_device_put(struct zcrypt_device *zdev)
216 {
217         return kref_put(&zdev->refcount, zcrypt_device_release);
218 }
219 EXPORT_SYMBOL(zcrypt_device_put);
220
221 struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
222 {
223         struct zcrypt_device *zdev;
224
225         zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
226         if (!zdev)
227                 return NULL;
228         zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
229         if (!zdev->reply.message)
230                 goto out_free;
231         zdev->reply.length = max_response_size;
232         spin_lock_init(&zdev->lock);
233         INIT_LIST_HEAD(&zdev->list);
234         zdev->dbf_area = zcrypt_dbf_devices;
235         return zdev;
236
237 out_free:
238         kfree(zdev);
239         return NULL;
240 }
241 EXPORT_SYMBOL(zcrypt_device_alloc);
242
243 void zcrypt_device_free(struct zcrypt_device *zdev)
244 {
245         kfree(zdev->reply.message);
246         kfree(zdev);
247 }
248 EXPORT_SYMBOL(zcrypt_device_free);
249
250 /**
251  * zcrypt_device_register() - Register a crypto device.
252  * @zdev: Pointer to a crypto device
253  *
254  * Register a crypto device. Returns 0 if successful.
255  */
256 int zcrypt_device_register(struct zcrypt_device *zdev)
257 {
258         int rc;
259
260         if (!zdev->ops)
261                 return -ENODEV;
262         rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
263                                 &zcrypt_device_attr_group);
264         if (rc)
265                 goto out;
266         get_device(&zdev->ap_dev->device);
267         kref_init(&zdev->refcount);
268         spin_lock_bh(&zcrypt_device_lock);
269         zdev->online = 1;       /* New devices are online by default. */
270         ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dreg", zdev->ap_dev->qid,
271                        zdev->online);
272         list_add_tail(&zdev->list, &zcrypt_device_list);
273         __zcrypt_increase_preference(zdev);
274         zcrypt_device_count++;
275         spin_unlock_bh(&zcrypt_device_lock);
276         if (zdev->ops->rng) {
277                 rc = zcrypt_rng_device_add();
278                 if (rc)
279                         goto out_unregister;
280         }
281         return 0;
282
283 out_unregister:
284         spin_lock_bh(&zcrypt_device_lock);
285         zcrypt_device_count--;
286         list_del_init(&zdev->list);
287         spin_unlock_bh(&zcrypt_device_lock);
288         sysfs_remove_group(&zdev->ap_dev->device.kobj,
289                            &zcrypt_device_attr_group);
290         put_device(&zdev->ap_dev->device);
291         zcrypt_device_put(zdev);
292 out:
293         return rc;
294 }
295 EXPORT_SYMBOL(zcrypt_device_register);
296
297 /**
298  * zcrypt_device_unregister(): Unregister a crypto device.
299  * @zdev: Pointer to crypto device
300  *
301  * Unregister a crypto device.
302  */
303 void zcrypt_device_unregister(struct zcrypt_device *zdev)
304 {
305         if (zdev->ops->rng)
306                 zcrypt_rng_device_remove();
307         spin_lock_bh(&zcrypt_device_lock);
308         zcrypt_device_count--;
309         list_del_init(&zdev->list);
310         spin_unlock_bh(&zcrypt_device_lock);
311         sysfs_remove_group(&zdev->ap_dev->device.kobj,
312                            &zcrypt_device_attr_group);
313         put_device(&zdev->ap_dev->device);
314         zcrypt_device_put(zdev);
315 }
316 EXPORT_SYMBOL(zcrypt_device_unregister);
317
318 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
319 {
320         if (zops->owner) {
321                 spin_lock_bh(&zcrypt_ops_list_lock);
322                 list_add_tail(&zops->list, &zcrypt_ops_list);
323                 spin_unlock_bh(&zcrypt_ops_list_lock);
324         }
325 }
326 EXPORT_SYMBOL(zcrypt_msgtype_register);
327
328 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
329 {
330         spin_lock_bh(&zcrypt_ops_list_lock);
331         list_del_init(&zops->list);
332         spin_unlock_bh(&zcrypt_ops_list_lock);
333 }
334 EXPORT_SYMBOL(zcrypt_msgtype_unregister);
335
336 static inline
337 struct zcrypt_ops *__ops_lookup(unsigned char *name, int variant)
338 {
339         struct zcrypt_ops *zops;
340         int found = 0;
341
342         spin_lock_bh(&zcrypt_ops_list_lock);
343         list_for_each_entry(zops, &zcrypt_ops_list, list) {
344                 if ((zops->variant == variant) &&
345                     (!strncmp(zops->owner->name, name, MODULE_NAME_LEN))) {
346                         found = 1;
347                         break;
348                 }
349         }
350         if (!found || !try_module_get(zops->owner))
351                 zops = NULL;
352
353         spin_unlock_bh(&zcrypt_ops_list_lock);
354
355         return zops;
356 }
357
358 struct zcrypt_ops *zcrypt_msgtype_request(unsigned char *name, int variant)
359 {
360         struct zcrypt_ops *zops = NULL;
361
362         zops = __ops_lookup(name, variant);
363         if (!zops) {
364                 request_module("%s", name);
365                 zops = __ops_lookup(name, variant);
366         }
367         return zops;
368 }
369 EXPORT_SYMBOL(zcrypt_msgtype_request);
370
371 void zcrypt_msgtype_release(struct zcrypt_ops *zops)
372 {
373         if (zops)
374                 module_put(zops->owner);
375 }
376 EXPORT_SYMBOL(zcrypt_msgtype_release);
377
378 /**
379  * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
380  *
381  * This function is not supported beyond zcrypt 1.3.1.
382  */
383 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
384                            size_t count, loff_t *f_pos)
385 {
386         return -EPERM;
387 }
388
389 /**
390  * zcrypt_write(): Not allowed.
391  *
392  * Write is is not allowed
393  */
394 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
395                             size_t count, loff_t *f_pos)
396 {
397         return -EPERM;
398 }
399
400 /**
401  * zcrypt_open(): Count number of users.
402  *
403  * Device open function to count number of users.
404  */
405 static int zcrypt_open(struct inode *inode, struct file *filp)
406 {
407         atomic_inc(&zcrypt_open_count);
408         return nonseekable_open(inode, filp);
409 }
410
411 /**
412  * zcrypt_release(): Count number of users.
413  *
414  * Device close function to count number of users.
415  */
416 static int zcrypt_release(struct inode *inode, struct file *filp)
417 {
418         atomic_dec(&zcrypt_open_count);
419         return 0;
420 }
421
422 /*
423  * zcrypt ioctls.
424  */
425 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
426 {
427         struct zcrypt_device *zdev;
428         int rc;
429
430         if (mex->outputdatalength < mex->inputdatalength)
431                 return -EINVAL;
432         /*
433          * As long as outputdatalength is big enough, we can set the
434          * outputdatalength equal to the inputdatalength, since that is the
435          * number of bytes we will copy in any case
436          */
437         mex->outputdatalength = mex->inputdatalength;
438
439         spin_lock_bh(&zcrypt_device_lock);
440         list_for_each_entry(zdev, &zcrypt_device_list, list) {
441                 if (!zdev->online ||
442                     !zdev->ops->rsa_modexpo ||
443                     zdev->min_mod_size > mex->inputdatalength ||
444                     zdev->max_mod_size < mex->inputdatalength)
445                         continue;
446                 zcrypt_device_get(zdev);
447                 get_device(&zdev->ap_dev->device);
448                 zdev->request_count++;
449                 __zcrypt_decrease_preference(zdev);
450                 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
451                         spin_unlock_bh(&zcrypt_device_lock);
452                         rc = zdev->ops->rsa_modexpo(zdev, mex);
453                         spin_lock_bh(&zcrypt_device_lock);
454                         module_put(zdev->ap_dev->drv->driver.owner);
455                 }
456                 else
457                         rc = -EAGAIN;
458                 zdev->request_count--;
459                 __zcrypt_increase_preference(zdev);
460                 put_device(&zdev->ap_dev->device);
461                 zcrypt_device_put(zdev);
462                 spin_unlock_bh(&zcrypt_device_lock);
463                 return rc;
464         }
465         spin_unlock_bh(&zcrypt_device_lock);
466         return -ENODEV;
467 }
468
469 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
470 {
471         struct zcrypt_device *zdev;
472         unsigned long long z1, z2, z3;
473         int rc, copied;
474
475         if (crt->outputdatalength < crt->inputdatalength)
476                 return -EINVAL;
477         /*
478          * As long as outputdatalength is big enough, we can set the
479          * outputdatalength equal to the inputdatalength, since that is the
480          * number of bytes we will copy in any case
481          */
482         crt->outputdatalength = crt->inputdatalength;
483
484         copied = 0;
485  restart:
486         spin_lock_bh(&zcrypt_device_lock);
487         list_for_each_entry(zdev, &zcrypt_device_list, list) {
488                 if (!zdev->online ||
489                     !zdev->ops->rsa_modexpo_crt ||
490                     zdev->min_mod_size > crt->inputdatalength ||
491                     zdev->max_mod_size < crt->inputdatalength)
492                         continue;
493                 if (zdev->short_crt && crt->inputdatalength > 240) {
494                         /*
495                          * Check inputdata for leading zeros for cards
496                          * that can't handle np_prime, bp_key, or
497                          * u_mult_inv > 128 bytes.
498                          */
499                         if (copied == 0) {
500                                 unsigned int len;
501                                 spin_unlock_bh(&zcrypt_device_lock);
502                                 /* len is max 256 / 2 - 120 = 8
503                                  * For bigger device just assume len of leading
504                                  * 0s is 8 as stated in the requirements for
505                                  * ica_rsa_modexpo_crt struct in zcrypt.h.
506                                  */
507                                 if (crt->inputdatalength <= 256)
508                                         len = crt->inputdatalength / 2 - 120;
509                                 else
510                                         len = 8;
511                                 if (len > sizeof(z1))
512                                         return -EFAULT;
513                                 z1 = z2 = z3 = 0;
514                                 if (copy_from_user(&z1, crt->np_prime, len) ||
515                                     copy_from_user(&z2, crt->bp_key, len) ||
516                                     copy_from_user(&z3, crt->u_mult_inv, len))
517                                         return -EFAULT;
518                                 z1 = z2 = z3 = 0;
519                                 copied = 1;
520                                 /*
521                                  * We have to restart device lookup -
522                                  * the device list may have changed by now.
523                                  */
524                                 goto restart;
525                         }
526                         if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
527                                 /* The device can't handle this request. */
528                                 continue;
529                 }
530                 zcrypt_device_get(zdev);
531                 get_device(&zdev->ap_dev->device);
532                 zdev->request_count++;
533                 __zcrypt_decrease_preference(zdev);
534                 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
535                         spin_unlock_bh(&zcrypt_device_lock);
536                         rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
537                         spin_lock_bh(&zcrypt_device_lock);
538                         module_put(zdev->ap_dev->drv->driver.owner);
539                 }
540                 else
541                         rc = -EAGAIN;
542                 zdev->request_count--;
543                 __zcrypt_increase_preference(zdev);
544                 put_device(&zdev->ap_dev->device);
545                 zcrypt_device_put(zdev);
546                 spin_unlock_bh(&zcrypt_device_lock);
547                 return rc;
548         }
549         spin_unlock_bh(&zcrypt_device_lock);
550         return -ENODEV;
551 }
552
553 static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
554 {
555         struct zcrypt_device *zdev;
556         int rc;
557
558         spin_lock_bh(&zcrypt_device_lock);
559         list_for_each_entry(zdev, &zcrypt_device_list, list) {
560                 if (!zdev->online || !zdev->ops->send_cprb ||
561                    (zdev->ops->variant == MSGTYPE06_VARIANT_EP11) ||
562                    (xcRB->user_defined != AUTOSELECT &&
563                     AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined))
564                         continue;
565                 zcrypt_device_get(zdev);
566                 get_device(&zdev->ap_dev->device);
567                 zdev->request_count++;
568                 __zcrypt_decrease_preference(zdev);
569                 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
570                         spin_unlock_bh(&zcrypt_device_lock);
571                         rc = zdev->ops->send_cprb(zdev, xcRB);
572                         spin_lock_bh(&zcrypt_device_lock);
573                         module_put(zdev->ap_dev->drv->driver.owner);
574                 }
575                 else
576                         rc = -EAGAIN;
577                 zdev->request_count--;
578                 __zcrypt_increase_preference(zdev);
579                 put_device(&zdev->ap_dev->device);
580                 zcrypt_device_put(zdev);
581                 spin_unlock_bh(&zcrypt_device_lock);
582                 return rc;
583         }
584         spin_unlock_bh(&zcrypt_device_lock);
585         return -ENODEV;
586 }
587
588 struct ep11_target_dev_list {
589         unsigned short          targets_num;
590         struct ep11_target_dev  *targets;
591 };
592
593 static bool is_desired_ep11dev(unsigned int dev_qid,
594                                struct ep11_target_dev_list dev_list)
595 {
596         int n;
597
598         for (n = 0; n < dev_list.targets_num; n++, dev_list.targets++) {
599                 if ((AP_QID_DEVICE(dev_qid) == dev_list.targets->ap_id) &&
600                     (AP_QID_QUEUE(dev_qid) == dev_list.targets->dom_id)) {
601                         return true;
602                 }
603         }
604         return false;
605 }
606
607 static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
608 {
609         struct zcrypt_device *zdev;
610         bool autoselect = false;
611         int rc;
612         struct ep11_target_dev_list ep11_dev_list = {
613                 .targets_num    =  0x00,
614                 .targets        =  NULL,
615         };
616
617         ep11_dev_list.targets_num = (unsigned short) xcrb->targets_num;
618
619         /* empty list indicates autoselect (all available targets) */
620         if (ep11_dev_list.targets_num == 0)
621                 autoselect = true;
622         else {
623                 ep11_dev_list.targets = kcalloc((unsigned short)
624                                                 xcrb->targets_num,
625                                                 sizeof(struct ep11_target_dev),
626                                                 GFP_KERNEL);
627                 if (!ep11_dev_list.targets)
628                         return -ENOMEM;
629
630                 if (copy_from_user(ep11_dev_list.targets,
631                                    (struct ep11_target_dev __force __user *)
632                                    xcrb->targets, xcrb->targets_num *
633                                    sizeof(struct ep11_target_dev)))
634                         return -EFAULT;
635         }
636
637         spin_lock_bh(&zcrypt_device_lock);
638         list_for_each_entry(zdev, &zcrypt_device_list, list) {
639                 /* check if device is eligible */
640                 if (!zdev->online ||
641                     zdev->ops->variant != MSGTYPE06_VARIANT_EP11)
642                         continue;
643
644                 /* check if device is selected as valid target */
645                 if (!is_desired_ep11dev(zdev->ap_dev->qid, ep11_dev_list) &&
646                     !autoselect)
647                         continue;
648
649                 zcrypt_device_get(zdev);
650                 get_device(&zdev->ap_dev->device);
651                 zdev->request_count++;
652                 __zcrypt_decrease_preference(zdev);
653                 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
654                         spin_unlock_bh(&zcrypt_device_lock);
655                         rc = zdev->ops->send_ep11_cprb(zdev, xcrb);
656                         spin_lock_bh(&zcrypt_device_lock);
657                         module_put(zdev->ap_dev->drv->driver.owner);
658                 } else {
659                         rc = -EAGAIN;
660                   }
661                 zdev->request_count--;
662                 __zcrypt_increase_preference(zdev);
663                 put_device(&zdev->ap_dev->device);
664                 zcrypt_device_put(zdev);
665                 spin_unlock_bh(&zcrypt_device_lock);
666                 return rc;
667         }
668         spin_unlock_bh(&zcrypt_device_lock);
669         return -ENODEV;
670 }
671
672 static long zcrypt_rng(char *buffer)
673 {
674         struct zcrypt_device *zdev;
675         int rc;
676
677         spin_lock_bh(&zcrypt_device_lock);
678         list_for_each_entry(zdev, &zcrypt_device_list, list) {
679                 if (!zdev->online || !zdev->ops->rng)
680                         continue;
681                 zcrypt_device_get(zdev);
682                 get_device(&zdev->ap_dev->device);
683                 zdev->request_count++;
684                 __zcrypt_decrease_preference(zdev);
685                 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
686                         spin_unlock_bh(&zcrypt_device_lock);
687                         rc = zdev->ops->rng(zdev, buffer);
688                         spin_lock_bh(&zcrypt_device_lock);
689                         module_put(zdev->ap_dev->drv->driver.owner);
690                 } else
691                         rc = -EAGAIN;
692                 zdev->request_count--;
693                 __zcrypt_increase_preference(zdev);
694                 put_device(&zdev->ap_dev->device);
695                 zcrypt_device_put(zdev);
696                 spin_unlock_bh(&zcrypt_device_lock);
697                 return rc;
698         }
699         spin_unlock_bh(&zcrypt_device_lock);
700         return -ENODEV;
701 }
702
703 static void zcrypt_status_mask(char status[AP_DEVICES])
704 {
705         struct zcrypt_device *zdev;
706
707         memset(status, 0, sizeof(char) * AP_DEVICES);
708         spin_lock_bh(&zcrypt_device_lock);
709         list_for_each_entry(zdev, &zcrypt_device_list, list)
710                 status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
711                         zdev->online ? zdev->user_space_type : 0x0d;
712         spin_unlock_bh(&zcrypt_device_lock);
713 }
714
715 static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
716 {
717         struct zcrypt_device *zdev;
718
719         memset(qdepth, 0, sizeof(char)  * AP_DEVICES);
720         spin_lock_bh(&zcrypt_device_lock);
721         list_for_each_entry(zdev, &zcrypt_device_list, list) {
722                 spin_lock(&zdev->ap_dev->lock);
723                 qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
724                         zdev->ap_dev->pendingq_count +
725                         zdev->ap_dev->requestq_count;
726                 spin_unlock(&zdev->ap_dev->lock);
727         }
728         spin_unlock_bh(&zcrypt_device_lock);
729 }
730
731 static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
732 {
733         struct zcrypt_device *zdev;
734
735         memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
736         spin_lock_bh(&zcrypt_device_lock);
737         list_for_each_entry(zdev, &zcrypt_device_list, list) {
738                 spin_lock(&zdev->ap_dev->lock);
739                 reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
740                         zdev->ap_dev->total_request_count;
741                 spin_unlock(&zdev->ap_dev->lock);
742         }
743         spin_unlock_bh(&zcrypt_device_lock);
744 }
745
746 static int zcrypt_pendingq_count(void)
747 {
748         struct zcrypt_device *zdev;
749         int pendingq_count = 0;
750
751         spin_lock_bh(&zcrypt_device_lock);
752         list_for_each_entry(zdev, &zcrypt_device_list, list) {
753                 spin_lock(&zdev->ap_dev->lock);
754                 pendingq_count += zdev->ap_dev->pendingq_count;
755                 spin_unlock(&zdev->ap_dev->lock);
756         }
757         spin_unlock_bh(&zcrypt_device_lock);
758         return pendingq_count;
759 }
760
761 static int zcrypt_requestq_count(void)
762 {
763         struct zcrypt_device *zdev;
764         int requestq_count = 0;
765
766         spin_lock_bh(&zcrypt_device_lock);
767         list_for_each_entry(zdev, &zcrypt_device_list, list) {
768                 spin_lock(&zdev->ap_dev->lock);
769                 requestq_count += zdev->ap_dev->requestq_count;
770                 spin_unlock(&zdev->ap_dev->lock);
771         }
772         spin_unlock_bh(&zcrypt_device_lock);
773         return requestq_count;
774 }
775
776 static int zcrypt_count_type(int type)
777 {
778         struct zcrypt_device *zdev;
779         int device_count = 0;
780
781         spin_lock_bh(&zcrypt_device_lock);
782         list_for_each_entry(zdev, &zcrypt_device_list, list)
783                 if (zdev->user_space_type == type)
784                         device_count++;
785         spin_unlock_bh(&zcrypt_device_lock);
786         return device_count;
787 }
788
789 /**
790  * zcrypt_ica_status(): Old, depracted combi status call.
791  *
792  * Old, deprecated combi status call.
793  */
794 static long zcrypt_ica_status(struct file *filp, unsigned long arg)
795 {
796         struct ica_z90_status *pstat;
797         int ret;
798
799         pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
800         if (!pstat)
801                 return -ENOMEM;
802         pstat->totalcount = zcrypt_device_count;
803         pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
804         pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
805         pstat->requestqWaitCount = zcrypt_requestq_count();
806         pstat->pendingqWaitCount = zcrypt_pendingq_count();
807         pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
808         pstat->cryptoDomain = ap_domain_index;
809         zcrypt_status_mask(pstat->status);
810         zcrypt_qdepth_mask(pstat->qdepth);
811         ret = 0;
812         if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
813                 ret = -EFAULT;
814         kfree(pstat);
815         return ret;
816 }
817
818 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
819                                   unsigned long arg)
820 {
821         int rc;
822
823         switch (cmd) {
824         case ICARSAMODEXPO: {
825                 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
826                 struct ica_rsa_modexpo mex;
827                 if (copy_from_user(&mex, umex, sizeof(mex)))
828                         return -EFAULT;
829                 do {
830                         rc = zcrypt_rsa_modexpo(&mex);
831                 } while (rc == -EAGAIN);
832                 /* on failure: retry once again after a requested rescan */
833                 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
834                         do {
835                                 rc = zcrypt_rsa_modexpo(&mex);
836                         } while (rc == -EAGAIN);
837                 if (rc)
838                         return rc;
839                 return put_user(mex.outputdatalength, &umex->outputdatalength);
840         }
841         case ICARSACRT: {
842                 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
843                 struct ica_rsa_modexpo_crt crt;
844                 if (copy_from_user(&crt, ucrt, sizeof(crt)))
845                         return -EFAULT;
846                 do {
847                         rc = zcrypt_rsa_crt(&crt);
848                 } while (rc == -EAGAIN);
849                 /* on failure: retry once again after a requested rescan */
850                 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
851                         do {
852                                 rc = zcrypt_rsa_crt(&crt);
853                         } while (rc == -EAGAIN);
854                 if (rc)
855                         return rc;
856                 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
857         }
858         case ZSECSENDCPRB: {
859                 struct ica_xcRB __user *uxcRB = (void __user *) arg;
860                 struct ica_xcRB xcRB;
861                 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
862                         return -EFAULT;
863                 do {
864                         rc = zcrypt_send_cprb(&xcRB);
865                 } while (rc == -EAGAIN);
866                 /* on failure: retry once again after a requested rescan */
867                 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
868                         do {
869                                 rc = zcrypt_send_cprb(&xcRB);
870                         } while (rc == -EAGAIN);
871                 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
872                         return -EFAULT;
873                 return rc;
874         }
875         case ZSENDEP11CPRB: {
876                 struct ep11_urb __user *uxcrb = (void __user *)arg;
877                 struct ep11_urb xcrb;
878                 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
879                         return -EFAULT;
880                 do {
881                         rc = zcrypt_send_ep11_cprb(&xcrb);
882                 } while (rc == -EAGAIN);
883                 /* on failure: retry once again after a requested rescan */
884                 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
885                         do {
886                                 rc = zcrypt_send_ep11_cprb(&xcrb);
887                         } while (rc == -EAGAIN);
888                 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
889                         return -EFAULT;
890                 return rc;
891         }
892         case Z90STAT_STATUS_MASK: {
893                 char status[AP_DEVICES];
894                 zcrypt_status_mask(status);
895                 if (copy_to_user((char __user *) arg, status,
896                                  sizeof(char) * AP_DEVICES))
897                         return -EFAULT;
898                 return 0;
899         }
900         case Z90STAT_QDEPTH_MASK: {
901                 char qdepth[AP_DEVICES];
902                 zcrypt_qdepth_mask(qdepth);
903                 if (copy_to_user((char __user *) arg, qdepth,
904                                  sizeof(char) * AP_DEVICES))
905                         return -EFAULT;
906                 return 0;
907         }
908         case Z90STAT_PERDEV_REQCNT: {
909                 int reqcnt[AP_DEVICES];
910                 zcrypt_perdev_reqcnt(reqcnt);
911                 if (copy_to_user((int __user *) arg, reqcnt,
912                                  sizeof(int) * AP_DEVICES))
913                         return -EFAULT;
914                 return 0;
915         }
916         case Z90STAT_REQUESTQ_COUNT:
917                 return put_user(zcrypt_requestq_count(), (int __user *) arg);
918         case Z90STAT_PENDINGQ_COUNT:
919                 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
920         case Z90STAT_TOTALOPEN_COUNT:
921                 return put_user(atomic_read(&zcrypt_open_count),
922                                 (int __user *) arg);
923         case Z90STAT_DOMAIN_INDEX:
924                 return put_user(ap_domain_index, (int __user *) arg);
925         /*
926          * Deprecated ioctls. Don't add another device count ioctl,
927          * you can count them yourself in the user space with the
928          * output of the Z90STAT_STATUS_MASK ioctl.
929          */
930         case ICAZ90STATUS:
931                 return zcrypt_ica_status(filp, arg);
932         case Z90STAT_TOTALCOUNT:
933                 return put_user(zcrypt_device_count, (int __user *) arg);
934         case Z90STAT_PCICACOUNT:
935                 return put_user(zcrypt_count_type(ZCRYPT_PCICA),
936                                 (int __user *) arg);
937         case Z90STAT_PCICCCOUNT:
938                 return put_user(zcrypt_count_type(ZCRYPT_PCICC),
939                                 (int __user *) arg);
940         case Z90STAT_PCIXCCMCL2COUNT:
941                 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
942                                 (int __user *) arg);
943         case Z90STAT_PCIXCCMCL3COUNT:
944                 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
945                                 (int __user *) arg);
946         case Z90STAT_PCIXCCCOUNT:
947                 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
948                                 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
949                                 (int __user *) arg);
950         case Z90STAT_CEX2CCOUNT:
951                 return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
952                                 (int __user *) arg);
953         case Z90STAT_CEX2ACOUNT:
954                 return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
955                                 (int __user *) arg);
956         default:
957                 /* unknown ioctl number */
958                 return -ENOIOCTLCMD;
959         }
960 }
961
962 #ifdef CONFIG_COMPAT
963 /*
964  * ioctl32 conversion routines
965  */
966 struct compat_ica_rsa_modexpo {
967         compat_uptr_t   inputdata;
968         unsigned int    inputdatalength;
969         compat_uptr_t   outputdata;
970         unsigned int    outputdatalength;
971         compat_uptr_t   b_key;
972         compat_uptr_t   n_modulus;
973 };
974
975 static long trans_modexpo32(struct file *filp, unsigned int cmd,
976                             unsigned long arg)
977 {
978         struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
979         struct compat_ica_rsa_modexpo mex32;
980         struct ica_rsa_modexpo mex64;
981         long rc;
982
983         if (copy_from_user(&mex32, umex32, sizeof(mex32)))
984                 return -EFAULT;
985         mex64.inputdata = compat_ptr(mex32.inputdata);
986         mex64.inputdatalength = mex32.inputdatalength;
987         mex64.outputdata = compat_ptr(mex32.outputdata);
988         mex64.outputdatalength = mex32.outputdatalength;
989         mex64.b_key = compat_ptr(mex32.b_key);
990         mex64.n_modulus = compat_ptr(mex32.n_modulus);
991         do {
992                 rc = zcrypt_rsa_modexpo(&mex64);
993         } while (rc == -EAGAIN);
994         /* on failure: retry once again after a requested rescan */
995         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
996                 do {
997                         rc = zcrypt_rsa_modexpo(&mex64);
998                 } while (rc == -EAGAIN);
999         if (rc)
1000                 return rc;
1001         return put_user(mex64.outputdatalength,
1002                         &umex32->outputdatalength);
1003 }
1004
1005 struct compat_ica_rsa_modexpo_crt {
1006         compat_uptr_t   inputdata;
1007         unsigned int    inputdatalength;
1008         compat_uptr_t   outputdata;
1009         unsigned int    outputdatalength;
1010         compat_uptr_t   bp_key;
1011         compat_uptr_t   bq_key;
1012         compat_uptr_t   np_prime;
1013         compat_uptr_t   nq_prime;
1014         compat_uptr_t   u_mult_inv;
1015 };
1016
1017 static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
1018                                 unsigned long arg)
1019 {
1020         struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1021         struct compat_ica_rsa_modexpo_crt crt32;
1022         struct ica_rsa_modexpo_crt crt64;
1023         long rc;
1024
1025         if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1026                 return -EFAULT;
1027         crt64.inputdata = compat_ptr(crt32.inputdata);
1028         crt64.inputdatalength = crt32.inputdatalength;
1029         crt64.outputdata=  compat_ptr(crt32.outputdata);
1030         crt64.outputdatalength = crt32.outputdatalength;
1031         crt64.bp_key = compat_ptr(crt32.bp_key);
1032         crt64.bq_key = compat_ptr(crt32.bq_key);
1033         crt64.np_prime = compat_ptr(crt32.np_prime);
1034         crt64.nq_prime = compat_ptr(crt32.nq_prime);
1035         crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1036         do {
1037                 rc = zcrypt_rsa_crt(&crt64);
1038         } while (rc == -EAGAIN);
1039         /* on failure: retry once again after a requested rescan */
1040         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1041                 do {
1042                         rc = zcrypt_rsa_crt(&crt64);
1043                 } while (rc == -EAGAIN);
1044         if (rc)
1045                 return rc;
1046         return put_user(crt64.outputdatalength,
1047                         &ucrt32->outputdatalength);
1048 }
1049
1050 struct compat_ica_xcRB {
1051         unsigned short  agent_ID;
1052         unsigned int    user_defined;
1053         unsigned short  request_ID;
1054         unsigned int    request_control_blk_length;
1055         unsigned char   padding1[16 - sizeof (compat_uptr_t)];
1056         compat_uptr_t   request_control_blk_addr;
1057         unsigned int    request_data_length;
1058         char            padding2[16 - sizeof (compat_uptr_t)];
1059         compat_uptr_t   request_data_address;
1060         unsigned int    reply_control_blk_length;
1061         char            padding3[16 - sizeof (compat_uptr_t)];
1062         compat_uptr_t   reply_control_blk_addr;
1063         unsigned int    reply_data_length;
1064         char            padding4[16 - sizeof (compat_uptr_t)];
1065         compat_uptr_t   reply_data_addr;
1066         unsigned short  priority_window;
1067         unsigned int    status;
1068 } __attribute__((packed));
1069
1070 static long trans_xcRB32(struct file *filp, unsigned int cmd,
1071                          unsigned long arg)
1072 {
1073         struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1074         struct compat_ica_xcRB xcRB32;
1075         struct ica_xcRB xcRB64;
1076         long rc;
1077
1078         if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1079                 return -EFAULT;
1080         xcRB64.agent_ID = xcRB32.agent_ID;
1081         xcRB64.user_defined = xcRB32.user_defined;
1082         xcRB64.request_ID = xcRB32.request_ID;
1083         xcRB64.request_control_blk_length =
1084                 xcRB32.request_control_blk_length;
1085         xcRB64.request_control_blk_addr =
1086                 compat_ptr(xcRB32.request_control_blk_addr);
1087         xcRB64.request_data_length =
1088                 xcRB32.request_data_length;
1089         xcRB64.request_data_address =
1090                 compat_ptr(xcRB32.request_data_address);
1091         xcRB64.reply_control_blk_length =
1092                 xcRB32.reply_control_blk_length;
1093         xcRB64.reply_control_blk_addr =
1094                 compat_ptr(xcRB32.reply_control_blk_addr);
1095         xcRB64.reply_data_length = xcRB32.reply_data_length;
1096         xcRB64.reply_data_addr =
1097                 compat_ptr(xcRB32.reply_data_addr);
1098         xcRB64.priority_window = xcRB32.priority_window;
1099         xcRB64.status = xcRB32.status;
1100         do {
1101                 rc = zcrypt_send_cprb(&xcRB64);
1102         } while (rc == -EAGAIN);
1103         /* on failure: retry once again after a requested rescan */
1104         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1105                 do {
1106                         rc = zcrypt_send_cprb(&xcRB64);
1107                 } while (rc == -EAGAIN);
1108         xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1109         xcRB32.reply_data_length = xcRB64.reply_data_length;
1110         xcRB32.status = xcRB64.status;
1111         if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1112                         return -EFAULT;
1113         return rc;
1114 }
1115
1116 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1117                          unsigned long arg)
1118 {
1119         if (cmd == ICARSAMODEXPO)
1120                 return trans_modexpo32(filp, cmd, arg);
1121         if (cmd == ICARSACRT)
1122                 return trans_modexpo_crt32(filp, cmd, arg);
1123         if (cmd == ZSECSENDCPRB)
1124                 return trans_xcRB32(filp, cmd, arg);
1125         return zcrypt_unlocked_ioctl(filp, cmd, arg);
1126 }
1127 #endif
1128
1129 /*
1130  * Misc device file operations.
1131  */
1132 static const struct file_operations zcrypt_fops = {
1133         .owner          = THIS_MODULE,
1134         .read           = zcrypt_read,
1135         .write          = zcrypt_write,
1136         .unlocked_ioctl = zcrypt_unlocked_ioctl,
1137 #ifdef CONFIG_COMPAT
1138         .compat_ioctl   = zcrypt_compat_ioctl,
1139 #endif
1140         .open           = zcrypt_open,
1141         .release        = zcrypt_release,
1142         .llseek         = no_llseek,
1143 };
1144
1145 /*
1146  * Misc device.
1147  */
1148 static struct miscdevice zcrypt_misc_device = {
1149         .minor      = MISC_DYNAMIC_MINOR,
1150         .name       = "z90crypt",
1151         .fops       = &zcrypt_fops,
1152 };
1153
1154 /*
1155  * Deprecated /proc entry support.
1156  */
1157 static struct proc_dir_entry *zcrypt_entry;
1158
1159 static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
1160 {
1161         int i;
1162
1163         for (i = 0; i < len; i++)
1164                 seq_printf(m, "%01x", (unsigned int) addr[i]);
1165         seq_putc(m, ' ');
1166 }
1167
1168 static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
1169 {
1170         int inl, c, cx;
1171
1172         seq_printf(m, "    ");
1173         inl = 0;
1174         for (c = 0; c < (len / 16); c++) {
1175                 sprintcl(m, addr+inl, 16);
1176                 inl += 16;
1177         }
1178         cx = len%16;
1179         if (cx) {
1180                 sprintcl(m, addr+inl, cx);
1181                 inl += cx;
1182         }
1183         seq_putc(m, '\n');
1184 }
1185
1186 static void sprinthx(unsigned char *title, struct seq_file *m,
1187                      unsigned char *addr, unsigned int len)
1188 {
1189         int inl, r, rx;
1190
1191         seq_printf(m, "\n%s\n", title);
1192         inl = 0;
1193         for (r = 0; r < (len / 64); r++) {
1194                 sprintrw(m, addr+inl, 64);
1195                 inl += 64;
1196         }
1197         rx = len % 64;
1198         if (rx) {
1199                 sprintrw(m, addr+inl, rx);
1200                 inl += rx;
1201         }
1202         seq_putc(m, '\n');
1203 }
1204
1205 static void sprinthx4(unsigned char *title, struct seq_file *m,
1206                       unsigned int *array, unsigned int len)
1207 {
1208         seq_printf(m, "\n%s\n", title);
1209         seq_hex_dump(m, "    ", DUMP_PREFIX_NONE, 32, 4, array, len, false);
1210         seq_putc(m, '\n');
1211 }
1212
1213 static int zcrypt_proc_show(struct seq_file *m, void *v)
1214 {
1215         char workarea[sizeof(int) * AP_DEVICES];
1216
1217         seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
1218                    ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
1219         seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
1220         seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
1221         seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
1222         seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
1223         seq_printf(m, "PCIXCC MCL2 count: %d\n",
1224                    zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
1225         seq_printf(m, "PCIXCC MCL3 count: %d\n",
1226                    zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1227         seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
1228         seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
1229         seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
1230         seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
1231         seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
1232         seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
1233         seq_printf(m, "Total open handles: %d\n\n",
1234                    atomic_read(&zcrypt_open_count));
1235         zcrypt_status_mask(workarea);
1236         sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1237                  "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1238                  m, workarea, AP_DEVICES);
1239         zcrypt_qdepth_mask(workarea);
1240         sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
1241         zcrypt_perdev_reqcnt((int *) workarea);
1242         sprinthx4("Per-device successfully completed request counts",
1243                   m, (unsigned int *) workarea, AP_DEVICES);
1244         return 0;
1245 }
1246
1247 static int zcrypt_proc_open(struct inode *inode, struct file *file)
1248 {
1249         return single_open(file, zcrypt_proc_show, NULL);
1250 }
1251
1252 static void zcrypt_disable_card(int index)
1253 {
1254         struct zcrypt_device *zdev;
1255
1256         spin_lock_bh(&zcrypt_device_lock);
1257         list_for_each_entry(zdev, &zcrypt_device_list, list)
1258                 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1259                         zdev->online = 0;
1260                         ap_flush_queue(zdev->ap_dev);
1261                         break;
1262                 }
1263         spin_unlock_bh(&zcrypt_device_lock);
1264 }
1265
1266 static void zcrypt_enable_card(int index)
1267 {
1268         struct zcrypt_device *zdev;
1269
1270         spin_lock_bh(&zcrypt_device_lock);
1271         list_for_each_entry(zdev, &zcrypt_device_list, list)
1272                 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1273                         zdev->online = 1;
1274                         break;
1275                 }
1276         spin_unlock_bh(&zcrypt_device_lock);
1277 }
1278
1279 static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
1280                                  size_t count, loff_t *pos)
1281 {
1282         unsigned char *lbuf, *ptr;
1283         size_t local_count;
1284         int j;
1285
1286         if (count <= 0)
1287                 return 0;
1288
1289 #define LBUFSIZE 1200UL
1290         lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1291         if (!lbuf)
1292                 return 0;
1293
1294         local_count = min(LBUFSIZE - 1, count);
1295         if (copy_from_user(lbuf, buffer, local_count) != 0) {
1296                 kfree(lbuf);
1297                 return -EFAULT;
1298         }
1299         lbuf[local_count] = '\0';
1300
1301         ptr = strstr(lbuf, "Online devices");
1302         if (!ptr)
1303                 goto out;
1304         ptr = strstr(ptr, "\n");
1305         if (!ptr)
1306                 goto out;
1307         ptr++;
1308
1309         if (strstr(ptr, "Waiting work element counts") == NULL)
1310                 goto out;
1311
1312         for (j = 0; j < 64 && *ptr; ptr++) {
1313                 /*
1314                  * '0' for no device, '1' for PCICA, '2' for PCICC,
1315                  * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1316                  * '5' for CEX2C and '6' for CEX2A'
1317                  * '7' for CEX3C and '8' for CEX3A
1318                  */
1319                 if (*ptr >= '0' && *ptr <= '8')
1320                         j++;
1321                 else if (*ptr == 'd' || *ptr == 'D')
1322                         zcrypt_disable_card(j++);
1323                 else if (*ptr == 'e' || *ptr == 'E')
1324                         zcrypt_enable_card(j++);
1325                 else if (*ptr != ' ' && *ptr != '\t')
1326                         break;
1327         }
1328 out:
1329         kfree(lbuf);
1330         return count;
1331 }
1332
1333 static const struct file_operations zcrypt_proc_fops = {
1334         .owner          = THIS_MODULE,
1335         .open           = zcrypt_proc_open,
1336         .read           = seq_read,
1337         .llseek         = seq_lseek,
1338         .release        = single_release,
1339         .write          = zcrypt_proc_write,
1340 };
1341
1342 static int zcrypt_rng_device_count;
1343 static u32 *zcrypt_rng_buffer;
1344 static int zcrypt_rng_buffer_index;
1345 static DEFINE_MUTEX(zcrypt_rng_mutex);
1346
1347 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1348 {
1349         int rc;
1350
1351         /*
1352          * We don't need locking here because the RNG API guarantees serialized
1353          * read method calls.
1354          */
1355         if (zcrypt_rng_buffer_index == 0) {
1356                 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1357                 /* on failure: retry once again after a requested rescan */
1358                 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1359                         rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1360                 if (rc < 0)
1361                         return -EIO;
1362                 zcrypt_rng_buffer_index = rc / sizeof *data;
1363         }
1364         *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1365         return sizeof *data;
1366 }
1367
1368 static struct hwrng zcrypt_rng_dev = {
1369         .name           = "zcrypt",
1370         .data_read      = zcrypt_rng_data_read,
1371         .quality        = 990,
1372 };
1373
1374 static int zcrypt_rng_device_add(void)
1375 {
1376         int rc = 0;
1377
1378         mutex_lock(&zcrypt_rng_mutex);
1379         if (zcrypt_rng_device_count == 0) {
1380                 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1381                 if (!zcrypt_rng_buffer) {
1382                         rc = -ENOMEM;
1383                         goto out;
1384                 }
1385                 zcrypt_rng_buffer_index = 0;
1386                 if (!zcrypt_hwrng_seed)
1387                         zcrypt_rng_dev.quality = 0;
1388                 rc = hwrng_register(&zcrypt_rng_dev);
1389                 if (rc)
1390                         goto out_free;
1391                 zcrypt_rng_device_count = 1;
1392         } else
1393                 zcrypt_rng_device_count++;
1394         mutex_unlock(&zcrypt_rng_mutex);
1395         return 0;
1396
1397 out_free:
1398         free_page((unsigned long) zcrypt_rng_buffer);
1399 out:
1400         mutex_unlock(&zcrypt_rng_mutex);
1401         return rc;
1402 }
1403
1404 static void zcrypt_rng_device_remove(void)
1405 {
1406         mutex_lock(&zcrypt_rng_mutex);
1407         zcrypt_rng_device_count--;
1408         if (zcrypt_rng_device_count == 0) {
1409                 hwrng_unregister(&zcrypt_rng_dev);
1410                 free_page((unsigned long) zcrypt_rng_buffer);
1411         }
1412         mutex_unlock(&zcrypt_rng_mutex);
1413 }
1414
1415 int __init zcrypt_debug_init(void)
1416 {
1417         debugfs_root = debugfs_create_dir("zcrypt", NULL);
1418
1419         zcrypt_dbf_common = debug_register("zcrypt_common", 1, 1, 16);
1420         debug_register_view(zcrypt_dbf_common, &debug_hex_ascii_view);
1421         debug_set_level(zcrypt_dbf_common, DBF_ERR);
1422
1423         zcrypt_dbf_devices = debug_register("zcrypt_devices", 1, 1, 16);
1424         debug_register_view(zcrypt_dbf_devices, &debug_hex_ascii_view);
1425         debug_set_level(zcrypt_dbf_devices, DBF_ERR);
1426
1427         return 0;
1428 }
1429
1430 void zcrypt_debug_exit(void)
1431 {
1432         debugfs_remove(debugfs_root);
1433         if (zcrypt_dbf_common)
1434                 debug_unregister(zcrypt_dbf_common);
1435         if (zcrypt_dbf_devices)
1436                 debug_unregister(zcrypt_dbf_devices);
1437 }
1438
1439 /**
1440  * zcrypt_api_init(): Module initialization.
1441  *
1442  * The module initialization code.
1443  */
1444 int __init zcrypt_api_init(void)
1445 {
1446         int rc;
1447
1448         rc = zcrypt_debug_init();
1449         if (rc)
1450                 goto out;
1451
1452         atomic_set(&zcrypt_rescan_req, 0);
1453
1454         /* Register the request sprayer. */
1455         rc = misc_register(&zcrypt_misc_device);
1456         if (rc < 0)
1457                 goto out;
1458
1459         /* Set up the proc file system */
1460         zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
1461         if (!zcrypt_entry) {
1462                 rc = -ENOMEM;
1463                 goto out_misc;
1464         }
1465
1466         return 0;
1467
1468 out_misc:
1469         misc_deregister(&zcrypt_misc_device);
1470 out:
1471         return rc;
1472 }
1473
1474 /**
1475  * zcrypt_api_exit(): Module termination.
1476  *
1477  * The module termination code.
1478  */
1479 void zcrypt_api_exit(void)
1480 {
1481         remove_proc_entry("driver/z90crypt", NULL);
1482         misc_deregister(&zcrypt_misc_device);
1483         zcrypt_debug_exit();
1484 }
1485
1486 module_init(zcrypt_api_init);
1487 module_exit(zcrypt_api_exit);