s390/zcrypt: move ap_msg param one level up the call chain
[linux-2.6-microblaze.git] / drivers / s390 / crypto / zcrypt_api.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright IBM Corp. 2001, 2018
4  *  Author(s): Robert Burroughs
5  *             Eric Rossman (edrossma@us.ibm.com)
6  *             Cornelia Huck <cornelia.huck@de.ibm.com>
7  *
8  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9  *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10  *                                Ralph Wuerthner <rwuerthn@de.ibm.com>
11  *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.ibm.com>
12  *  Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
13  */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/miscdevice.h>
19 #include <linux/fs.h>
20 #include <linux/compat.h>
21 #include <linux/slab.h>
22 #include <linux/atomic.h>
23 #include <linux/uaccess.h>
24 #include <linux/hw_random.h>
25 #include <linux/debugfs.h>
26 #include <linux/cdev.h>
27 #include <linux/ctype.h>
28 #include <asm/debug.h>
29
30 #define CREATE_TRACE_POINTS
31 #include <asm/trace/zcrypt.h>
32
33 #include "zcrypt_api.h"
34 #include "zcrypt_debug.h"
35
36 #include "zcrypt_msgtype6.h"
37 #include "zcrypt_msgtype50.h"
38 #include "zcrypt_ccamisc.h"
39 #include "zcrypt_ep11misc.h"
40
41 /*
42  * Module description.
43  */
44 MODULE_AUTHOR("IBM Corporation");
45 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
46                    "Copyright IBM Corp. 2001, 2012");
47 MODULE_LICENSE("GPL");
48
49 /*
50  * zcrypt tracepoint functions
51  */
52 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
53 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
54
55 static int zcrypt_hwrng_seed = 1;
56 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
57 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
58
59 DEFINE_SPINLOCK(zcrypt_list_lock);
60 LIST_HEAD(zcrypt_card_list);
61 int zcrypt_device_count;
62
63 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
64 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
65
66 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
67 EXPORT_SYMBOL(zcrypt_rescan_req);
68
69 static LIST_HEAD(zcrypt_ops_list);
70
71 /* Zcrypt related debug feature stuff. */
72 debug_info_t *zcrypt_dbf_info;
73
74 /**
75  * Process a rescan of the transport layer.
76  *
77  * Returns 1, if the rescan has been processed, otherwise 0.
78  */
79 static inline int zcrypt_process_rescan(void)
80 {
81         if (atomic_read(&zcrypt_rescan_req)) {
82                 atomic_set(&zcrypt_rescan_req, 0);
83                 atomic_inc(&zcrypt_rescan_count);
84                 ap_bus_force_rescan();
85                 ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
86                            atomic_inc_return(&zcrypt_rescan_count));
87                 return 1;
88         }
89         return 0;
90 }
91
92 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
93 {
94         list_add_tail(&zops->list, &zcrypt_ops_list);
95 }
96
97 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
98 {
99         list_del_init(&zops->list);
100 }
101
102 struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
103 {
104         struct zcrypt_ops *zops;
105
106         list_for_each_entry(zops, &zcrypt_ops_list, list)
107                 if ((zops->variant == variant) &&
108                     (!strncmp(zops->name, name, sizeof(zops->name))))
109                         return zops;
110         return NULL;
111 }
112 EXPORT_SYMBOL(zcrypt_msgtype);
113
114 /*
115  * Multi device nodes extension functions.
116  */
117
118 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
119
120 struct zcdn_device;
121
122 static struct class *zcrypt_class;
123 static dev_t zcrypt_devt;
124 static struct cdev zcrypt_cdev;
125
126 struct zcdn_device {
127         struct device device;
128         struct ap_perms perms;
129 };
130
131 #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
132
133 #define ZCDN_MAX_NAME 32
134
135 static int zcdn_create(const char *name);
136 static int zcdn_destroy(const char *name);
137
138 /*
139  * Find zcdn device by name.
140  * Returns reference to the zcdn device which needs to be released
141  * with put_device() after use.
142  */
143 static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
144 {
145         struct device *dev = class_find_device_by_name(zcrypt_class, name);
146
147         return dev ? to_zcdn_dev(dev) : NULL;
148 }
149
150 /*
151  * Find zcdn device by devt value.
152  * Returns reference to the zcdn device which needs to be released
153  * with put_device() after use.
154  */
155 static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
156 {
157         struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
158
159         return dev ? to_zcdn_dev(dev) : NULL;
160 }
161
162 static ssize_t ioctlmask_show(struct device *dev,
163                               struct device_attribute *attr,
164                               char *buf)
165 {
166         int i, rc;
167         struct zcdn_device *zcdndev = to_zcdn_dev(dev);
168
169         if (mutex_lock_interruptible(&ap_perms_mutex))
170                 return -ERESTARTSYS;
171
172         buf[0] = '0';
173         buf[1] = 'x';
174         for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
175                 snprintf(buf + 2 + 2 * i * sizeof(long),
176                          PAGE_SIZE - 2 - 2 * i * sizeof(long),
177                          "%016lx", zcdndev->perms.ioctlm[i]);
178         buf[2 + 2 * i * sizeof(long)] = '\n';
179         buf[2 + 2 * i * sizeof(long) + 1] = '\0';
180         rc = 2 + 2 * i * sizeof(long) + 1;
181
182         mutex_unlock(&ap_perms_mutex);
183
184         return rc;
185 }
186
187 static ssize_t ioctlmask_store(struct device *dev,
188                                struct device_attribute *attr,
189                                const char *buf, size_t count)
190 {
191         int rc;
192         struct zcdn_device *zcdndev = to_zcdn_dev(dev);
193
194         rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
195                                AP_IOCTLS, &ap_perms_mutex);
196         if (rc)
197                 return rc;
198
199         return count;
200 }
201
202 static DEVICE_ATTR_RW(ioctlmask);
203
204 static ssize_t apmask_show(struct device *dev,
205                            struct device_attribute *attr,
206                            char *buf)
207 {
208         int i, rc;
209         struct zcdn_device *zcdndev = to_zcdn_dev(dev);
210
211         if (mutex_lock_interruptible(&ap_perms_mutex))
212                 return -ERESTARTSYS;
213
214         buf[0] = '0';
215         buf[1] = 'x';
216         for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
217                 snprintf(buf + 2 + 2 * i * sizeof(long),
218                          PAGE_SIZE - 2 - 2 * i * sizeof(long),
219                          "%016lx", zcdndev->perms.apm[i]);
220         buf[2 + 2 * i * sizeof(long)] = '\n';
221         buf[2 + 2 * i * sizeof(long) + 1] = '\0';
222         rc = 2 + 2 * i * sizeof(long) + 1;
223
224         mutex_unlock(&ap_perms_mutex);
225
226         return rc;
227 }
228
229 static ssize_t apmask_store(struct device *dev,
230                             struct device_attribute *attr,
231                             const char *buf, size_t count)
232 {
233         int rc;
234         struct zcdn_device *zcdndev = to_zcdn_dev(dev);
235
236         rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
237                                AP_DEVICES, &ap_perms_mutex);
238         if (rc)
239                 return rc;
240
241         return count;
242 }
243
244 static DEVICE_ATTR_RW(apmask);
245
246 static ssize_t aqmask_show(struct device *dev,
247                            struct device_attribute *attr,
248                            char *buf)
249 {
250         int i, rc;
251         struct zcdn_device *zcdndev = to_zcdn_dev(dev);
252
253         if (mutex_lock_interruptible(&ap_perms_mutex))
254                 return -ERESTARTSYS;
255
256         buf[0] = '0';
257         buf[1] = 'x';
258         for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
259                 snprintf(buf + 2 + 2 * i * sizeof(long),
260                          PAGE_SIZE - 2 - 2 * i * sizeof(long),
261                          "%016lx", zcdndev->perms.aqm[i]);
262         buf[2 + 2 * i * sizeof(long)] = '\n';
263         buf[2 + 2 * i * sizeof(long) + 1] = '\0';
264         rc = 2 + 2 * i * sizeof(long) + 1;
265
266         mutex_unlock(&ap_perms_mutex);
267
268         return rc;
269 }
270
271 static ssize_t aqmask_store(struct device *dev,
272                             struct device_attribute *attr,
273                             const char *buf, size_t count)
274 {
275         int rc;
276         struct zcdn_device *zcdndev = to_zcdn_dev(dev);
277
278         rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
279                                AP_DOMAINS, &ap_perms_mutex);
280         if (rc)
281                 return rc;
282
283         return count;
284 }
285
286 static DEVICE_ATTR_RW(aqmask);
287
288 static struct attribute *zcdn_dev_attrs[] = {
289         &dev_attr_ioctlmask.attr,
290         &dev_attr_apmask.attr,
291         &dev_attr_aqmask.attr,
292         NULL
293 };
294
295 static struct attribute_group zcdn_dev_attr_group = {
296         .attrs = zcdn_dev_attrs
297 };
298
299 static const struct attribute_group *zcdn_dev_attr_groups[] = {
300         &zcdn_dev_attr_group,
301         NULL
302 };
303
304 static ssize_t zcdn_create_store(struct class *class,
305                                  struct class_attribute *attr,
306                                  const char *buf, size_t count)
307 {
308         int rc;
309         char name[ZCDN_MAX_NAME];
310
311         strncpy(name, skip_spaces(buf), sizeof(name));
312         name[sizeof(name) - 1] = '\0';
313
314         rc = zcdn_create(strim(name));
315
316         return rc ? rc : count;
317 }
318
319 static const struct class_attribute class_attr_zcdn_create =
320         __ATTR(create, 0600, NULL, zcdn_create_store);
321
322 static ssize_t zcdn_destroy_store(struct class *class,
323                                   struct class_attribute *attr,
324                                   const char *buf, size_t count)
325 {
326         int rc;
327         char name[ZCDN_MAX_NAME];
328
329         strncpy(name, skip_spaces(buf), sizeof(name));
330         name[sizeof(name) - 1] = '\0';
331
332         rc = zcdn_destroy(strim(name));
333
334         return rc ? rc : count;
335 }
336
337 static const struct class_attribute class_attr_zcdn_destroy =
338         __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
339
340 static void zcdn_device_release(struct device *dev)
341 {
342         struct zcdn_device *zcdndev = to_zcdn_dev(dev);
343
344         ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
345                    MAJOR(dev->devt), MINOR(dev->devt));
346
347         kfree(zcdndev);
348 }
349
350 static int zcdn_create(const char *name)
351 {
352         dev_t devt;
353         int i, rc = 0;
354         char nodename[ZCDN_MAX_NAME];
355         struct zcdn_device *zcdndev;
356
357         if (mutex_lock_interruptible(&ap_perms_mutex))
358                 return -ERESTARTSYS;
359
360         /* check if device node with this name already exists */
361         if (name[0]) {
362                 zcdndev = find_zcdndev_by_name(name);
363                 if (zcdndev) {
364                         put_device(&zcdndev->device);
365                         rc = -EEXIST;
366                         goto unlockout;
367                 }
368         }
369
370         /* find an unused minor number */
371         for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
372                 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
373                 zcdndev = find_zcdndev_by_devt(devt);
374                 if (zcdndev)
375                         put_device(&zcdndev->device);
376                 else
377                         break;
378         }
379         if (i == ZCRYPT_MAX_MINOR_NODES) {
380                 rc = -ENOSPC;
381                 goto unlockout;
382         }
383
384         /* alloc and prepare a new zcdn device */
385         zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
386         if (!zcdndev) {
387                 rc = -ENOMEM;
388                 goto unlockout;
389         }
390         zcdndev->device.release = zcdn_device_release;
391         zcdndev->device.class = zcrypt_class;
392         zcdndev->device.devt = devt;
393         zcdndev->device.groups = zcdn_dev_attr_groups;
394         if (name[0])
395                 strncpy(nodename, name, sizeof(nodename));
396         else
397                 snprintf(nodename, sizeof(nodename),
398                          ZCRYPT_NAME "_%d", (int) MINOR(devt));
399         nodename[sizeof(nodename)-1] = '\0';
400         if (dev_set_name(&zcdndev->device, nodename)) {
401                 rc = -EINVAL;
402                 goto unlockout;
403         }
404         rc = device_register(&zcdndev->device);
405         if (rc) {
406                 put_device(&zcdndev->device);
407                 goto unlockout;
408         }
409
410         ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
411                    MAJOR(devt), MINOR(devt));
412
413 unlockout:
414         mutex_unlock(&ap_perms_mutex);
415         return rc;
416 }
417
418 static int zcdn_destroy(const char *name)
419 {
420         int rc = 0;
421         struct zcdn_device *zcdndev;
422
423         if (mutex_lock_interruptible(&ap_perms_mutex))
424                 return -ERESTARTSYS;
425
426         /* try to find this zcdn device */
427         zcdndev = find_zcdndev_by_name(name);
428         if (!zcdndev) {
429                 rc = -ENOENT;
430                 goto unlockout;
431         }
432
433         /*
434          * The zcdn device is not hard destroyed. It is subject to
435          * reference counting and thus just needs to be unregistered.
436          */
437         put_device(&zcdndev->device);
438         device_unregister(&zcdndev->device);
439
440 unlockout:
441         mutex_unlock(&ap_perms_mutex);
442         return rc;
443 }
444
445 static void zcdn_destroy_all(void)
446 {
447         int i;
448         dev_t devt;
449         struct zcdn_device *zcdndev;
450
451         mutex_lock(&ap_perms_mutex);
452         for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
453                 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
454                 zcdndev = find_zcdndev_by_devt(devt);
455                 if (zcdndev) {
456                         put_device(&zcdndev->device);
457                         device_unregister(&zcdndev->device);
458                 }
459         }
460         mutex_unlock(&ap_perms_mutex);
461 }
462
463 #endif
464
465 /**
466  * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
467  *
468  * This function is not supported beyond zcrypt 1.3.1.
469  */
470 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
471                            size_t count, loff_t *f_pos)
472 {
473         return -EPERM;
474 }
475
476 /**
477  * zcrypt_write(): Not allowed.
478  *
479  * Write is is not allowed
480  */
481 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
482                             size_t count, loff_t *f_pos)
483 {
484         return -EPERM;
485 }
486
487 /**
488  * zcrypt_open(): Count number of users.
489  *
490  * Device open function to count number of users.
491  */
492 static int zcrypt_open(struct inode *inode, struct file *filp)
493 {
494         struct ap_perms *perms = &ap_perms;
495
496 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
497         if (filp->f_inode->i_cdev == &zcrypt_cdev) {
498                 struct zcdn_device *zcdndev;
499
500                 if (mutex_lock_interruptible(&ap_perms_mutex))
501                         return -ERESTARTSYS;
502                 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
503                 /* find returns a reference, no get_device() needed */
504                 mutex_unlock(&ap_perms_mutex);
505                 if (zcdndev)
506                         perms = &zcdndev->perms;
507         }
508 #endif
509         filp->private_data = (void *) perms;
510
511         atomic_inc(&zcrypt_open_count);
512         return stream_open(inode, filp);
513 }
514
515 /**
516  * zcrypt_release(): Count number of users.
517  *
518  * Device close function to count number of users.
519  */
520 static int zcrypt_release(struct inode *inode, struct file *filp)
521 {
522 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
523         if (filp->f_inode->i_cdev == &zcrypt_cdev) {
524                 struct zcdn_device *zcdndev;
525
526                 mutex_lock(&ap_perms_mutex);
527                 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
528                 mutex_unlock(&ap_perms_mutex);
529                 if (zcdndev) {
530                         /* 2 puts here: one for find, one for open */
531                         put_device(&zcdndev->device);
532                         put_device(&zcdndev->device);
533                 }
534         }
535 #endif
536
537         atomic_dec(&zcrypt_open_count);
538         return 0;
539 }
540
541 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
542                                      unsigned int cmd)
543 {
544         int rc = -EPERM;
545         int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
546
547         if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
548                 if (test_bit_inv(ioctlnr, perms->ioctlm))
549                         rc = 0;
550         }
551
552         if (rc)
553                 ZCRYPT_DBF(DBF_WARN,
554                            "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
555                            ioctlnr, rc);
556
557         return rc;
558 }
559
560 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
561 {
562         return test_bit_inv(card, perms->apm) ? true : false;
563 }
564
565 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
566 {
567         return test_bit_inv(queue, perms->aqm) ? true : false;
568 }
569
570 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
571                                                      struct zcrypt_queue *zq,
572                                                      struct module **pmod,
573                                                      unsigned int weight)
574 {
575         if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
576                 return NULL;
577         zcrypt_queue_get(zq);
578         get_device(&zq->queue->ap_dev.device);
579         atomic_add(weight, &zc->load);
580         atomic_add(weight, &zq->load);
581         zq->request_count++;
582         *pmod = zq->queue->ap_dev.drv->driver.owner;
583         return zq;
584 }
585
586 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
587                                      struct zcrypt_queue *zq,
588                                      struct module *mod,
589                                      unsigned int weight)
590 {
591         zq->request_count--;
592         atomic_sub(weight, &zc->load);
593         atomic_sub(weight, &zq->load);
594         put_device(&zq->queue->ap_dev.device);
595         zcrypt_queue_put(zq);
596         module_put(mod);
597 }
598
599 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
600                                        struct zcrypt_card *pref_zc,
601                                        unsigned int weight,
602                                        unsigned int pref_weight)
603 {
604         if (!pref_zc)
605                 return true;
606         weight += atomic_read(&zc->load);
607         pref_weight += atomic_read(&pref_zc->load);
608         if (weight == pref_weight)
609                 return atomic64_read(&zc->card->total_request_count) <
610                         atomic64_read(&pref_zc->card->total_request_count);
611         return weight < pref_weight;
612 }
613
614 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
615                                         struct zcrypt_queue *pref_zq,
616                                         unsigned int weight,
617                                         unsigned int pref_weight)
618 {
619         if (!pref_zq)
620                 return true;
621         weight += atomic_read(&zq->load);
622         pref_weight += atomic_read(&pref_zq->load);
623         if (weight == pref_weight)
624                 return zq->queue->total_request_count <
625                         pref_zq->queue->total_request_count;
626         return weight < pref_weight;
627 }
628
629 /*
630  * zcrypt ioctls.
631  */
632 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
633                                struct zcrypt_track *tr,
634                                struct ica_rsa_modexpo *mex)
635 {
636         struct zcrypt_card *zc, *pref_zc;
637         struct zcrypt_queue *zq, *pref_zq;
638         struct ap_message ap_msg;
639         unsigned int wgt = 0, pref_wgt = 0;
640         unsigned int func_code;
641         int cpen, qpen, qid = 0, rc = -ENODEV;
642         struct module *mod;
643
644         trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
645
646         ap_init_message(&ap_msg);
647
648         if (mex->outputdatalength < mex->inputdatalength) {
649                 func_code = 0;
650                 rc = -EINVAL;
651                 goto out;
652         }
653
654         /*
655          * As long as outputdatalength is big enough, we can set the
656          * outputdatalength equal to the inputdatalength, since that is the
657          * number of bytes we will copy in any case
658          */
659         mex->outputdatalength = mex->inputdatalength;
660
661         rc = get_rsa_modex_fc(mex, &func_code);
662         if (rc)
663                 goto out;
664
665         pref_zc = NULL;
666         pref_zq = NULL;
667         spin_lock(&zcrypt_list_lock);
668         for_each_zcrypt_card(zc) {
669                 /* Check for useable accelarator or CCA card */
670                 if (!zc->online || !zc->card->config ||
671                     !(zc->card->functions & 0x18000000))
672                         continue;
673                 /* Check for size limits */
674                 if (zc->min_mod_size > mex->inputdatalength ||
675                     zc->max_mod_size < mex->inputdatalength)
676                         continue;
677                 /* check if device node has admission for this card */
678                 if (!zcrypt_check_card(perms, zc->card->id))
679                         continue;
680                 /* get weight index of the card device  */
681                 wgt = zc->speed_rating[func_code];
682                 /* penalty if this msg was previously sent via this card */
683                 cpen = (tr && tr->again_counter && tr->last_qid &&
684                         AP_QID_CARD(tr->last_qid) == zc->card->id) ?
685                         TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
686                 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
687                         continue;
688                 for_each_zcrypt_queue(zq, zc) {
689                         /* check if device is useable and eligible */
690                         if (!zq->online || !zq->ops->rsa_modexpo ||
691                             !zq->queue->config)
692                                 continue;
693                         /* check if device node has admission for this queue */
694                         if (!zcrypt_check_queue(perms,
695                                                 AP_QID_QUEUE(zq->queue->qid)))
696                                 continue;
697                         /* penalty if the msg was previously sent at this qid */
698                         qpen = (tr && tr->again_counter && tr->last_qid &&
699                                 tr->last_qid == zq->queue->qid) ?
700                                 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
701                         if (!zcrypt_queue_compare(zq, pref_zq,
702                                                   wgt + cpen + qpen, pref_wgt))
703                                 continue;
704                         pref_zc = zc;
705                         pref_zq = zq;
706                         pref_wgt = wgt + cpen + qpen;
707                 }
708         }
709         pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
710         spin_unlock(&zcrypt_list_lock);
711
712         if (!pref_zq) {
713                 rc = -ENODEV;
714                 goto out;
715         }
716
717         qid = pref_zq->queue->qid;
718         rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
719
720         spin_lock(&zcrypt_list_lock);
721         zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
722         spin_unlock(&zcrypt_list_lock);
723
724 out:
725         ap_release_message(&ap_msg);
726         if (tr) {
727                 tr->last_rc = rc;
728                 tr->last_qid = qid;
729         }
730         trace_s390_zcrypt_rep(mex, func_code, rc,
731                               AP_QID_CARD(qid), AP_QID_QUEUE(qid));
732         return rc;
733 }
734
735 static long zcrypt_rsa_crt(struct ap_perms *perms,
736                            struct zcrypt_track *tr,
737                            struct ica_rsa_modexpo_crt *crt)
738 {
739         struct zcrypt_card *zc, *pref_zc;
740         struct zcrypt_queue *zq, *pref_zq;
741         struct ap_message ap_msg;
742         unsigned int wgt = 0, pref_wgt = 0;
743         unsigned int func_code;
744         int cpen, qpen, qid = 0, rc = -ENODEV;
745         struct module *mod;
746
747         trace_s390_zcrypt_req(crt, TP_ICARSACRT);
748
749         ap_init_message(&ap_msg);
750
751         if (crt->outputdatalength < crt->inputdatalength) {
752                 func_code = 0;
753                 rc = -EINVAL;
754                 goto out;
755         }
756
757         /*
758          * As long as outputdatalength is big enough, we can set the
759          * outputdatalength equal to the inputdatalength, since that is the
760          * number of bytes we will copy in any case
761          */
762         crt->outputdatalength = crt->inputdatalength;
763
764         rc = get_rsa_crt_fc(crt, &func_code);
765         if (rc)
766                 goto out;
767
768         pref_zc = NULL;
769         pref_zq = NULL;
770         spin_lock(&zcrypt_list_lock);
771         for_each_zcrypt_card(zc) {
772                 /* Check for useable accelarator or CCA card */
773                 if (!zc->online || !zc->card->config ||
774                     !(zc->card->functions & 0x18000000))
775                         continue;
776                 /* Check for size limits */
777                 if (zc->min_mod_size > crt->inputdatalength ||
778                     zc->max_mod_size < crt->inputdatalength)
779                         continue;
780                 /* check if device node has admission for this card */
781                 if (!zcrypt_check_card(perms, zc->card->id))
782                         continue;
783                 /* get weight index of the card device  */
784                 wgt = zc->speed_rating[func_code];
785                 /* penalty if this msg was previously sent via this card */
786                 cpen = (tr && tr->again_counter && tr->last_qid &&
787                         AP_QID_CARD(tr->last_qid) == zc->card->id) ?
788                         TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
789                 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
790                         continue;
791                 for_each_zcrypt_queue(zq, zc) {
792                         /* check if device is useable and eligible */
793                         if (!zq->online || !zq->ops->rsa_modexpo_crt ||
794                             !zq->queue->config)
795                                 continue;
796                         /* check if device node has admission for this queue */
797                         if (!zcrypt_check_queue(perms,
798                                                 AP_QID_QUEUE(zq->queue->qid)))
799                                 continue;
800                         /* penalty if the msg was previously sent at this qid */
801                         qpen = (tr && tr->again_counter && tr->last_qid &&
802                                 tr->last_qid == zq->queue->qid) ?
803                                 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
804                         if (!zcrypt_queue_compare(zq, pref_zq,
805                                                   wgt + cpen + qpen, pref_wgt))
806                                 continue;
807                         pref_zc = zc;
808                         pref_zq = zq;
809                         pref_wgt = wgt + cpen + qpen;
810                 }
811         }
812         pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
813         spin_unlock(&zcrypt_list_lock);
814
815         if (!pref_zq) {
816                 rc = -ENODEV;
817                 goto out;
818         }
819
820         qid = pref_zq->queue->qid;
821         rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
822
823         spin_lock(&zcrypt_list_lock);
824         zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
825         spin_unlock(&zcrypt_list_lock);
826
827 out:
828         ap_release_message(&ap_msg);
829         if (tr) {
830                 tr->last_rc = rc;
831                 tr->last_qid = qid;
832         }
833         trace_s390_zcrypt_rep(crt, func_code, rc,
834                               AP_QID_CARD(qid), AP_QID_QUEUE(qid));
835         return rc;
836 }
837
838 static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
839                               struct zcrypt_track *tr,
840                               struct ica_xcRB *xcRB)
841 {
842         struct zcrypt_card *zc, *pref_zc;
843         struct zcrypt_queue *zq, *pref_zq;
844         struct ap_message ap_msg;
845         unsigned int wgt = 0, pref_wgt = 0;
846         unsigned int func_code;
847         unsigned short *domain, tdom;
848         int cpen, qpen, qid = 0, rc = -ENODEV;
849         struct module *mod;
850
851         trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
852
853         xcRB->status = 0;
854         ap_init_message(&ap_msg);
855         rc = get_cprb_fc(userspace, xcRB, &ap_msg, &func_code, &domain);
856         if (rc)
857                 goto out;
858
859         /*
860          * If a valid target domain is set and this domain is NOT a usage
861          * domain but a control only domain, use the default domain as target.
862          */
863         tdom = *domain;
864         if (tdom < AP_DOMAINS &&
865             !ap_test_config_usage_domain(tdom) &&
866             ap_test_config_ctrl_domain(tdom) &&
867             ap_domain_index >= 0)
868                 tdom = ap_domain_index;
869
870         pref_zc = NULL;
871         pref_zq = NULL;
872         spin_lock(&zcrypt_list_lock);
873         for_each_zcrypt_card(zc) {
874                 /* Check for useable CCA card */
875                 if (!zc->online || !zc->card->config ||
876                     !(zc->card->functions & 0x10000000))
877                         continue;
878                 /* Check for user selected CCA card */
879                 if (xcRB->user_defined != AUTOSELECT &&
880                     xcRB->user_defined != zc->card->id)
881                         continue;
882                 /* check if device node has admission for this card */
883                 if (!zcrypt_check_card(perms, zc->card->id))
884                         continue;
885                 /* get weight index of the card device  */
886                 wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
887                 /* penalty if this msg was previously sent via this card */
888                 cpen = (tr && tr->again_counter && tr->last_qid &&
889                         AP_QID_CARD(tr->last_qid) == zc->card->id) ?
890                         TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
891                 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
892                         continue;
893                 for_each_zcrypt_queue(zq, zc) {
894                         /* check for device useable and eligible */
895                         if (!zq->online ||
896                             !zq->ops->send_cprb ||
897                             !zq->queue->config ||
898                             (tdom != AUTOSEL_DOM &&
899                              tdom != AP_QID_QUEUE(zq->queue->qid)))
900                                 continue;
901                         /* check if device node has admission for this queue */
902                         if (!zcrypt_check_queue(perms,
903                                                 AP_QID_QUEUE(zq->queue->qid)))
904                                 continue;
905                         /* penalty if the msg was previously sent at this qid */
906                         qpen = (tr && tr->again_counter && tr->last_qid &&
907                                 tr->last_qid == zq->queue->qid) ?
908                                 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
909                         if (!zcrypt_queue_compare(zq, pref_zq,
910                                                   wgt + cpen + qpen, pref_wgt))
911                                 continue;
912                         pref_zc = zc;
913                         pref_zq = zq;
914                         pref_wgt = wgt + cpen + qpen;
915                 }
916         }
917         pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
918         spin_unlock(&zcrypt_list_lock);
919
920         if (!pref_zq) {
921                 rc = -ENODEV;
922                 goto out;
923         }
924
925         /* in case of auto select, provide the correct domain */
926         qid = pref_zq->queue->qid;
927         if (*domain == AUTOSEL_DOM)
928                 *domain = AP_QID_QUEUE(qid);
929
930         rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcRB, &ap_msg);
931
932         spin_lock(&zcrypt_list_lock);
933         zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
934         spin_unlock(&zcrypt_list_lock);
935
936 out:
937         ap_release_message(&ap_msg);
938         if (tr) {
939                 tr->last_rc = rc;
940                 tr->last_qid = qid;
941         }
942         trace_s390_zcrypt_rep(xcRB, func_code, rc,
943                               AP_QID_CARD(qid), AP_QID_QUEUE(qid));
944         return rc;
945 }
946
947 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
948 {
949         return _zcrypt_send_cprb(false, &ap_perms, NULL, xcRB);
950 }
951 EXPORT_SYMBOL(zcrypt_send_cprb);
952
953 static bool is_desired_ep11_card(unsigned int dev_id,
954                                  unsigned short target_num,
955                                  struct ep11_target_dev *targets)
956 {
957         while (target_num-- > 0) {
958                 if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
959                         return true;
960                 targets++;
961         }
962         return false;
963 }
964
965 static bool is_desired_ep11_queue(unsigned int dev_qid,
966                                   unsigned short target_num,
967                                   struct ep11_target_dev *targets)
968 {
969         int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
970
971         while (target_num-- > 0) {
972                 if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
973                     (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
974                         return true;
975                 targets++;
976         }
977         return false;
978 }
979
980 static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
981                                    struct zcrypt_track *tr,
982                                    struct ep11_urb *xcrb)
983 {
984         struct zcrypt_card *zc, *pref_zc;
985         struct zcrypt_queue *zq, *pref_zq;
986         struct ep11_target_dev *targets;
987         unsigned short target_num;
988         unsigned int wgt = 0, pref_wgt = 0;
989         unsigned int func_code;
990         struct ap_message ap_msg;
991         int cpen, qpen, qid = 0, rc = -ENODEV;
992         struct module *mod;
993
994         trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
995
996         ap_init_message(&ap_msg);
997
998         target_num = (unsigned short) xcrb->targets_num;
999
1000         /* empty list indicates autoselect (all available targets) */
1001         targets = NULL;
1002         if (target_num != 0) {
1003                 struct ep11_target_dev __user *uptr;
1004
1005                 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
1006                 if (!targets) {
1007                         func_code = 0;
1008                         rc = -ENOMEM;
1009                         goto out;
1010                 }
1011
1012                 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
1013                 if (z_copy_from_user(userspace, targets, uptr,
1014                                    target_num * sizeof(*targets))) {
1015                         func_code = 0;
1016                         rc = -EFAULT;
1017                         goto out_free;
1018                 }
1019         }
1020
1021         rc = get_ep11cprb_fc(userspace, xcrb, &ap_msg, &func_code);
1022         if (rc)
1023                 goto out_free;
1024
1025         pref_zc = NULL;
1026         pref_zq = NULL;
1027         spin_lock(&zcrypt_list_lock);
1028         for_each_zcrypt_card(zc) {
1029                 /* Check for useable EP11 card */
1030                 if (!zc->online || !zc->card->config ||
1031                     !(zc->card->functions & 0x04000000))
1032                         continue;
1033                 /* Check for user selected EP11 card */
1034                 if (targets &&
1035                     !is_desired_ep11_card(zc->card->id, target_num, targets))
1036                         continue;
1037                 /* check if device node has admission for this card */
1038                 if (!zcrypt_check_card(perms, zc->card->id))
1039                         continue;
1040                 /* get weight index of the card device  */
1041                 wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1042                 /* penalty if this msg was previously sent via this card */
1043                 cpen = (tr && tr->again_counter && tr->last_qid &&
1044                         AP_QID_CARD(tr->last_qid) == zc->card->id) ?
1045                         TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
1046                 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
1047                         continue;
1048                 for_each_zcrypt_queue(zq, zc) {
1049                         /* check if device is useable and eligible */
1050                         if (!zq->online ||
1051                             !zq->ops->send_ep11_cprb ||
1052                             !zq->queue->config ||
1053                             (targets &&
1054                              !is_desired_ep11_queue(zq->queue->qid,
1055                                                     target_num, targets)))
1056                                 continue;
1057                         /* check if device node has admission for this queue */
1058                         if (!zcrypt_check_queue(perms,
1059                                                 AP_QID_QUEUE(zq->queue->qid)))
1060                                 continue;
1061                         /* penalty if the msg was previously sent at this qid */
1062                         qpen = (tr && tr->again_counter && tr->last_qid &&
1063                                 tr->last_qid == zq->queue->qid) ?
1064                                 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
1065                         if (!zcrypt_queue_compare(zq, pref_zq,
1066                                                   wgt + cpen + qpen, pref_wgt))
1067                                 continue;
1068                         pref_zc = zc;
1069                         pref_zq = zq;
1070                         pref_wgt = wgt + cpen + qpen;
1071                 }
1072         }
1073         pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1074         spin_unlock(&zcrypt_list_lock);
1075
1076         if (!pref_zq) {
1077                 rc = -ENODEV;
1078                 goto out_free;
1079         }
1080
1081         qid = pref_zq->queue->qid;
1082         rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
1083
1084         spin_lock(&zcrypt_list_lock);
1085         zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1086         spin_unlock(&zcrypt_list_lock);
1087
1088 out_free:
1089         kfree(targets);
1090 out:
1091         ap_release_message(&ap_msg);
1092         if (tr) {
1093                 tr->last_rc = rc;
1094                 tr->last_qid = qid;
1095         }
1096         trace_s390_zcrypt_rep(xcrb, func_code, rc,
1097                               AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1098         return rc;
1099 }
1100
1101 long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
1102 {
1103         return _zcrypt_send_ep11_cprb(false, &ap_perms, NULL, xcrb);
1104 }
1105 EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1106
1107 static long zcrypt_rng(char *buffer)
1108 {
1109         struct zcrypt_card *zc, *pref_zc;
1110         struct zcrypt_queue *zq, *pref_zq;
1111         unsigned int wgt = 0, pref_wgt = 0;
1112         unsigned int func_code;
1113         struct ap_message ap_msg;
1114         unsigned int domain;
1115         int qid = 0, rc = -ENODEV;
1116         struct module *mod;
1117
1118         trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1119
1120         ap_init_message(&ap_msg);
1121         rc = get_rng_fc(&ap_msg, &func_code, &domain);
1122         if (rc)
1123                 goto out;
1124
1125         pref_zc = NULL;
1126         pref_zq = NULL;
1127         spin_lock(&zcrypt_list_lock);
1128         for_each_zcrypt_card(zc) {
1129                 /* Check for useable CCA card */
1130                 if (!zc->online || !zc->card->config ||
1131                     !(zc->card->functions & 0x10000000))
1132                         continue;
1133                 /* get weight index of the card device  */
1134                 wgt = zc->speed_rating[func_code];
1135                 if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
1136                         continue;
1137                 for_each_zcrypt_queue(zq, zc) {
1138                         /* check if device is useable and eligible */
1139                         if (!zq->online || !zq->ops->rng ||
1140                             !zq->queue->config)
1141                                 continue;
1142                         if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
1143                                 continue;
1144                         pref_zc = zc;
1145                         pref_zq = zq;
1146                         pref_wgt = wgt;
1147                 }
1148         }
1149         pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1150         spin_unlock(&zcrypt_list_lock);
1151
1152         if (!pref_zq) {
1153                 rc = -ENODEV;
1154                 goto out;
1155         }
1156
1157         qid = pref_zq->queue->qid;
1158         rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1159
1160         spin_lock(&zcrypt_list_lock);
1161         zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1162         spin_unlock(&zcrypt_list_lock);
1163
1164 out:
1165         ap_release_message(&ap_msg);
1166         trace_s390_zcrypt_rep(buffer, func_code, rc,
1167                               AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1168         return rc;
1169 }
1170
1171 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1172 {
1173         struct zcrypt_card *zc;
1174         struct zcrypt_queue *zq;
1175         struct zcrypt_device_status *stat;
1176         int card, queue;
1177
1178         memset(devstatus, 0, MAX_ZDEV_ENTRIES
1179                * sizeof(struct zcrypt_device_status));
1180
1181         spin_lock(&zcrypt_list_lock);
1182         for_each_zcrypt_card(zc) {
1183                 for_each_zcrypt_queue(zq, zc) {
1184                         card = AP_QID_CARD(zq->queue->qid);
1185                         if (card >= MAX_ZDEV_CARDIDS)
1186                                 continue;
1187                         queue = AP_QID_QUEUE(zq->queue->qid);
1188                         stat = &devstatus[card * AP_DOMAINS + queue];
1189                         stat->hwtype = zc->card->ap_dev.device_type;
1190                         stat->functions = zc->card->functions >> 26;
1191                         stat->qid = zq->queue->qid;
1192                         stat->online = zq->online ? 0x01 : 0x00;
1193                 }
1194         }
1195         spin_unlock(&zcrypt_list_lock);
1196 }
1197
1198 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1199 {
1200         struct zcrypt_card *zc;
1201         struct zcrypt_queue *zq;
1202         struct zcrypt_device_status_ext *stat;
1203         int card, queue;
1204
1205         memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1206                * sizeof(struct zcrypt_device_status_ext));
1207
1208         spin_lock(&zcrypt_list_lock);
1209         for_each_zcrypt_card(zc) {
1210                 for_each_zcrypt_queue(zq, zc) {
1211                         card = AP_QID_CARD(zq->queue->qid);
1212                         queue = AP_QID_QUEUE(zq->queue->qid);
1213                         stat = &devstatus[card * AP_DOMAINS + queue];
1214                         stat->hwtype = zc->card->ap_dev.device_type;
1215                         stat->functions = zc->card->functions >> 26;
1216                         stat->qid = zq->queue->qid;
1217                         stat->online = zq->online ? 0x01 : 0x00;
1218                 }
1219         }
1220         spin_unlock(&zcrypt_list_lock);
1221 }
1222 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1223
1224 int zcrypt_device_status_ext(int card, int queue,
1225                              struct zcrypt_device_status_ext *devstat)
1226 {
1227         struct zcrypt_card *zc;
1228         struct zcrypt_queue *zq;
1229
1230         memset(devstat, 0, sizeof(*devstat));
1231
1232         spin_lock(&zcrypt_list_lock);
1233         for_each_zcrypt_card(zc) {
1234                 for_each_zcrypt_queue(zq, zc) {
1235                         if (card == AP_QID_CARD(zq->queue->qid) &&
1236                             queue == AP_QID_QUEUE(zq->queue->qid)) {
1237                                 devstat->hwtype = zc->card->ap_dev.device_type;
1238                                 devstat->functions = zc->card->functions >> 26;
1239                                 devstat->qid = zq->queue->qid;
1240                                 devstat->online = zq->online ? 0x01 : 0x00;
1241                                 spin_unlock(&zcrypt_list_lock);
1242                                 return 0;
1243                         }
1244                 }
1245         }
1246         spin_unlock(&zcrypt_list_lock);
1247
1248         return -ENODEV;
1249 }
1250 EXPORT_SYMBOL(zcrypt_device_status_ext);
1251
1252 static void zcrypt_status_mask(char status[], size_t max_adapters)
1253 {
1254         struct zcrypt_card *zc;
1255         struct zcrypt_queue *zq;
1256         int card;
1257
1258         memset(status, 0, max_adapters);
1259         spin_lock(&zcrypt_list_lock);
1260         for_each_zcrypt_card(zc) {
1261                 for_each_zcrypt_queue(zq, zc) {
1262                         card = AP_QID_CARD(zq->queue->qid);
1263                         if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1264                             || card >= max_adapters)
1265                                 continue;
1266                         status[card] = zc->online ? zc->user_space_type : 0x0d;
1267                 }
1268         }
1269         spin_unlock(&zcrypt_list_lock);
1270 }
1271
1272 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1273 {
1274         struct zcrypt_card *zc;
1275         struct zcrypt_queue *zq;
1276         int card;
1277
1278         memset(qdepth, 0, max_adapters);
1279         spin_lock(&zcrypt_list_lock);
1280         local_bh_disable();
1281         for_each_zcrypt_card(zc) {
1282                 for_each_zcrypt_queue(zq, zc) {
1283                         card = AP_QID_CARD(zq->queue->qid);
1284                         if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1285                             || card >= max_adapters)
1286                                 continue;
1287                         spin_lock(&zq->queue->lock);
1288                         qdepth[card] =
1289                                 zq->queue->pendingq_count +
1290                                 zq->queue->requestq_count;
1291                         spin_unlock(&zq->queue->lock);
1292                 }
1293         }
1294         local_bh_enable();
1295         spin_unlock(&zcrypt_list_lock);
1296 }
1297
1298 static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1299 {
1300         struct zcrypt_card *zc;
1301         struct zcrypt_queue *zq;
1302         int card;
1303         u64 cnt;
1304
1305         memset(reqcnt, 0, sizeof(int) * max_adapters);
1306         spin_lock(&zcrypt_list_lock);
1307         local_bh_disable();
1308         for_each_zcrypt_card(zc) {
1309                 for_each_zcrypt_queue(zq, zc) {
1310                         card = AP_QID_CARD(zq->queue->qid);
1311                         if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1312                             || card >= max_adapters)
1313                                 continue;
1314                         spin_lock(&zq->queue->lock);
1315                         cnt = zq->queue->total_request_count;
1316                         spin_unlock(&zq->queue->lock);
1317                         reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX;
1318                 }
1319         }
1320         local_bh_enable();
1321         spin_unlock(&zcrypt_list_lock);
1322 }
1323
1324 static int zcrypt_pendingq_count(void)
1325 {
1326         struct zcrypt_card *zc;
1327         struct zcrypt_queue *zq;
1328         int pendingq_count;
1329
1330         pendingq_count = 0;
1331         spin_lock(&zcrypt_list_lock);
1332         local_bh_disable();
1333         for_each_zcrypt_card(zc) {
1334                 for_each_zcrypt_queue(zq, zc) {
1335                         if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1336                                 continue;
1337                         spin_lock(&zq->queue->lock);
1338                         pendingq_count += zq->queue->pendingq_count;
1339                         spin_unlock(&zq->queue->lock);
1340                 }
1341         }
1342         local_bh_enable();
1343         spin_unlock(&zcrypt_list_lock);
1344         return pendingq_count;
1345 }
1346
1347 static int zcrypt_requestq_count(void)
1348 {
1349         struct zcrypt_card *zc;
1350         struct zcrypt_queue *zq;
1351         int requestq_count;
1352
1353         requestq_count = 0;
1354         spin_lock(&zcrypt_list_lock);
1355         local_bh_disable();
1356         for_each_zcrypt_card(zc) {
1357                 for_each_zcrypt_queue(zq, zc) {
1358                         if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1359                                 continue;
1360                         spin_lock(&zq->queue->lock);
1361                         requestq_count += zq->queue->requestq_count;
1362                         spin_unlock(&zq->queue->lock);
1363                 }
1364         }
1365         local_bh_enable();
1366         spin_unlock(&zcrypt_list_lock);
1367         return requestq_count;
1368 }
1369
1370 static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1371 {
1372         int rc;
1373         struct zcrypt_track tr;
1374         struct ica_rsa_modexpo mex;
1375         struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1376
1377         memset(&tr, 0, sizeof(tr));
1378         if (copy_from_user(&mex, umex, sizeof(mex)))
1379                 return -EFAULT;
1380         do {
1381                 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1382                 if (rc == -EAGAIN)
1383                         tr.again_counter++;
1384         } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1385         /* on failure: retry once again after a requested rescan */
1386         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1387                 do {
1388                         rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1389                         if (rc == -EAGAIN)
1390                                 tr.again_counter++;
1391                 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1392         if (rc) {
1393                 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1394                 return rc;
1395         }
1396         return put_user(mex.outputdatalength, &umex->outputdatalength);
1397 }
1398
1399 static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1400 {
1401         int rc;
1402         struct zcrypt_track tr;
1403         struct ica_rsa_modexpo_crt crt;
1404         struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1405
1406         memset(&tr, 0, sizeof(tr));
1407         if (copy_from_user(&crt, ucrt, sizeof(crt)))
1408                 return -EFAULT;
1409         do {
1410                 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1411                 if (rc == -EAGAIN)
1412                         tr.again_counter++;
1413         } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1414         /* on failure: retry once again after a requested rescan */
1415         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1416                 do {
1417                         rc = zcrypt_rsa_crt(perms, &tr, &crt);
1418                         if (rc == -EAGAIN)
1419                                 tr.again_counter++;
1420                 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1421         if (rc) {
1422                 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1423                 return rc;
1424         }
1425         return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1426 }
1427
1428 static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1429 {
1430         int rc;
1431         struct ica_xcRB xcRB;
1432         struct zcrypt_track tr;
1433         struct ica_xcRB __user *uxcRB = (void __user *) arg;
1434
1435         memset(&tr, 0, sizeof(tr));
1436         if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1437                 return -EFAULT;
1438         do {
1439                 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1440                 if (rc == -EAGAIN)
1441                         tr.again_counter++;
1442         } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1443         /* on failure: retry once again after a requested rescan */
1444         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1445                 do {
1446                         rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1447                         if (rc == -EAGAIN)
1448                                 tr.again_counter++;
1449                 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1450         if (rc)
1451                 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1452                            rc, xcRB.status);
1453         if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1454                 return -EFAULT;
1455         return rc;
1456 }
1457
1458 static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1459 {
1460         int rc;
1461         struct ep11_urb xcrb;
1462         struct zcrypt_track tr;
1463         struct ep11_urb __user *uxcrb = (void __user *)arg;
1464
1465         memset(&tr, 0, sizeof(tr));
1466         if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1467                 return -EFAULT;
1468         do {
1469                 rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1470                 if (rc == -EAGAIN)
1471                         tr.again_counter++;
1472         } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1473         /* on failure: retry once again after a requested rescan */
1474         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1475                 do {
1476                         rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1477                         if (rc == -EAGAIN)
1478                                 tr.again_counter++;
1479                 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1480         if (rc)
1481                 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1482         if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1483                 return -EFAULT;
1484         return rc;
1485 }
1486
1487 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1488                                   unsigned long arg)
1489 {
1490         int rc;
1491         struct ap_perms *perms =
1492                 (struct ap_perms *) filp->private_data;
1493
1494         rc = zcrypt_check_ioctl(perms, cmd);
1495         if (rc)
1496                 return rc;
1497
1498         switch (cmd) {
1499         case ICARSAMODEXPO:
1500                 return icarsamodexpo_ioctl(perms, arg);
1501         case ICARSACRT:
1502                 return icarsacrt_ioctl(perms, arg);
1503         case ZSECSENDCPRB:
1504                 return zsecsendcprb_ioctl(perms, arg);
1505         case ZSENDEP11CPRB:
1506                 return zsendep11cprb_ioctl(perms, arg);
1507         case ZCRYPT_DEVICE_STATUS: {
1508                 struct zcrypt_device_status_ext *device_status;
1509                 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1510                         * sizeof(struct zcrypt_device_status_ext);
1511
1512                 device_status = kzalloc(total_size, GFP_KERNEL);
1513                 if (!device_status)
1514                         return -ENOMEM;
1515                 zcrypt_device_status_mask_ext(device_status);
1516                 if (copy_to_user((char __user *) arg, device_status,
1517                                  total_size))
1518                         rc = -EFAULT;
1519                 kfree(device_status);
1520                 return rc;
1521         }
1522         case ZCRYPT_STATUS_MASK: {
1523                 char status[AP_DEVICES];
1524
1525                 zcrypt_status_mask(status, AP_DEVICES);
1526                 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1527                         return -EFAULT;
1528                 return 0;
1529         }
1530         case ZCRYPT_QDEPTH_MASK: {
1531                 char qdepth[AP_DEVICES];
1532
1533                 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1534                 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1535                         return -EFAULT;
1536                 return 0;
1537         }
1538         case ZCRYPT_PERDEV_REQCNT: {
1539                 u32 *reqcnt;
1540
1541                 reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1542                 if (!reqcnt)
1543                         return -ENOMEM;
1544                 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1545                 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1546                         rc = -EFAULT;
1547                 kfree(reqcnt);
1548                 return rc;
1549         }
1550         case Z90STAT_REQUESTQ_COUNT:
1551                 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1552         case Z90STAT_PENDINGQ_COUNT:
1553                 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1554         case Z90STAT_TOTALOPEN_COUNT:
1555                 return put_user(atomic_read(&zcrypt_open_count),
1556                                 (int __user *) arg);
1557         case Z90STAT_DOMAIN_INDEX:
1558                 return put_user(ap_domain_index, (int __user *) arg);
1559         /*
1560          * Deprecated ioctls
1561          */
1562         case ZDEVICESTATUS: {
1563                 /* the old ioctl supports only 64 adapters */
1564                 struct zcrypt_device_status *device_status;
1565                 size_t total_size = MAX_ZDEV_ENTRIES
1566                         * sizeof(struct zcrypt_device_status);
1567
1568                 device_status = kzalloc(total_size, GFP_KERNEL);
1569                 if (!device_status)
1570                         return -ENOMEM;
1571                 zcrypt_device_status_mask(device_status);
1572                 if (copy_to_user((char __user *) arg, device_status,
1573                                  total_size))
1574                         rc = -EFAULT;
1575                 kfree(device_status);
1576                 return rc;
1577         }
1578         case Z90STAT_STATUS_MASK: {
1579                 /* the old ioctl supports only 64 adapters */
1580                 char status[MAX_ZDEV_CARDIDS];
1581
1582                 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1583                 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1584                         return -EFAULT;
1585                 return 0;
1586         }
1587         case Z90STAT_QDEPTH_MASK: {
1588                 /* the old ioctl supports only 64 adapters */
1589                 char qdepth[MAX_ZDEV_CARDIDS];
1590
1591                 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1592                 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1593                         return -EFAULT;
1594                 return 0;
1595         }
1596         case Z90STAT_PERDEV_REQCNT: {
1597                 /* the old ioctl supports only 64 adapters */
1598                 u32 reqcnt[MAX_ZDEV_CARDIDS];
1599
1600                 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1601                 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1602                         return -EFAULT;
1603                 return 0;
1604         }
1605         /* unknown ioctl number */
1606         default:
1607                 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1608                 return -ENOIOCTLCMD;
1609         }
1610 }
1611
1612 #ifdef CONFIG_COMPAT
1613 /*
1614  * ioctl32 conversion routines
1615  */
1616 struct compat_ica_rsa_modexpo {
1617         compat_uptr_t   inputdata;
1618         unsigned int    inputdatalength;
1619         compat_uptr_t   outputdata;
1620         unsigned int    outputdatalength;
1621         compat_uptr_t   b_key;
1622         compat_uptr_t   n_modulus;
1623 };
1624
1625 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1626                             unsigned int cmd, unsigned long arg)
1627 {
1628         struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1629         struct compat_ica_rsa_modexpo mex32;
1630         struct ica_rsa_modexpo mex64;
1631         struct zcrypt_track tr;
1632         long rc;
1633
1634         memset(&tr, 0, sizeof(tr));
1635         if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1636                 return -EFAULT;
1637         mex64.inputdata = compat_ptr(mex32.inputdata);
1638         mex64.inputdatalength = mex32.inputdatalength;
1639         mex64.outputdata = compat_ptr(mex32.outputdata);
1640         mex64.outputdatalength = mex32.outputdatalength;
1641         mex64.b_key = compat_ptr(mex32.b_key);
1642         mex64.n_modulus = compat_ptr(mex32.n_modulus);
1643         do {
1644                 rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1645                 if (rc == -EAGAIN)
1646                         tr.again_counter++;
1647         } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1648         /* on failure: retry once again after a requested rescan */
1649         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1650                 do {
1651                         rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1652                         if (rc == -EAGAIN)
1653                                 tr.again_counter++;
1654                 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1655         if (rc)
1656                 return rc;
1657         return put_user(mex64.outputdatalength,
1658                         &umex32->outputdatalength);
1659 }
1660
1661 struct compat_ica_rsa_modexpo_crt {
1662         compat_uptr_t   inputdata;
1663         unsigned int    inputdatalength;
1664         compat_uptr_t   outputdata;
1665         unsigned int    outputdatalength;
1666         compat_uptr_t   bp_key;
1667         compat_uptr_t   bq_key;
1668         compat_uptr_t   np_prime;
1669         compat_uptr_t   nq_prime;
1670         compat_uptr_t   u_mult_inv;
1671 };
1672
1673 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1674                                 unsigned int cmd, unsigned long arg)
1675 {
1676         struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1677         struct compat_ica_rsa_modexpo_crt crt32;
1678         struct ica_rsa_modexpo_crt crt64;
1679         struct zcrypt_track tr;
1680         long rc;
1681
1682         memset(&tr, 0, sizeof(tr));
1683         if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1684                 return -EFAULT;
1685         crt64.inputdata = compat_ptr(crt32.inputdata);
1686         crt64.inputdatalength = crt32.inputdatalength;
1687         crt64.outputdata = compat_ptr(crt32.outputdata);
1688         crt64.outputdatalength = crt32.outputdatalength;
1689         crt64.bp_key = compat_ptr(crt32.bp_key);
1690         crt64.bq_key = compat_ptr(crt32.bq_key);
1691         crt64.np_prime = compat_ptr(crt32.np_prime);
1692         crt64.nq_prime = compat_ptr(crt32.nq_prime);
1693         crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1694         do {
1695                 rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1696                 if (rc == -EAGAIN)
1697                         tr.again_counter++;
1698         } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1699         /* on failure: retry once again after a requested rescan */
1700         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1701                 do {
1702                         rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1703                         if (rc == -EAGAIN)
1704                                 tr.again_counter++;
1705                 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1706         if (rc)
1707                 return rc;
1708         return put_user(crt64.outputdatalength,
1709                         &ucrt32->outputdatalength);
1710 }
1711
1712 struct compat_ica_xcRB {
1713         unsigned short  agent_ID;
1714         unsigned int    user_defined;
1715         unsigned short  request_ID;
1716         unsigned int    request_control_blk_length;
1717         unsigned char   padding1[16 - sizeof(compat_uptr_t)];
1718         compat_uptr_t   request_control_blk_addr;
1719         unsigned int    request_data_length;
1720         char            padding2[16 - sizeof(compat_uptr_t)];
1721         compat_uptr_t   request_data_address;
1722         unsigned int    reply_control_blk_length;
1723         char            padding3[16 - sizeof(compat_uptr_t)];
1724         compat_uptr_t   reply_control_blk_addr;
1725         unsigned int    reply_data_length;
1726         char            padding4[16 - sizeof(compat_uptr_t)];
1727         compat_uptr_t   reply_data_addr;
1728         unsigned short  priority_window;
1729         unsigned int    status;
1730 } __packed;
1731
1732 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1733                          unsigned int cmd, unsigned long arg)
1734 {
1735         struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1736         struct compat_ica_xcRB xcRB32;
1737         struct zcrypt_track tr;
1738         struct ica_xcRB xcRB64;
1739         long rc;
1740
1741         memset(&tr, 0, sizeof(tr));
1742         if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1743                 return -EFAULT;
1744         xcRB64.agent_ID = xcRB32.agent_ID;
1745         xcRB64.user_defined = xcRB32.user_defined;
1746         xcRB64.request_ID = xcRB32.request_ID;
1747         xcRB64.request_control_blk_length =
1748                 xcRB32.request_control_blk_length;
1749         xcRB64.request_control_blk_addr =
1750                 compat_ptr(xcRB32.request_control_blk_addr);
1751         xcRB64.request_data_length =
1752                 xcRB32.request_data_length;
1753         xcRB64.request_data_address =
1754                 compat_ptr(xcRB32.request_data_address);
1755         xcRB64.reply_control_blk_length =
1756                 xcRB32.reply_control_blk_length;
1757         xcRB64.reply_control_blk_addr =
1758                 compat_ptr(xcRB32.reply_control_blk_addr);
1759         xcRB64.reply_data_length = xcRB32.reply_data_length;
1760         xcRB64.reply_data_addr =
1761                 compat_ptr(xcRB32.reply_data_addr);
1762         xcRB64.priority_window = xcRB32.priority_window;
1763         xcRB64.status = xcRB32.status;
1764         do {
1765                 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1766                 if (rc == -EAGAIN)
1767                         tr.again_counter++;
1768         } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1769         /* on failure: retry once again after a requested rescan */
1770         if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1771                 do {
1772                         rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1773                         if (rc == -EAGAIN)
1774                                 tr.again_counter++;
1775                 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1776         xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1777         xcRB32.reply_data_length = xcRB64.reply_data_length;
1778         xcRB32.status = xcRB64.status;
1779         if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1780                 return -EFAULT;
1781         return rc;
1782 }
1783
1784 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1785                          unsigned long arg)
1786 {
1787         int rc;
1788         struct ap_perms *perms =
1789                 (struct ap_perms *) filp->private_data;
1790
1791         rc = zcrypt_check_ioctl(perms, cmd);
1792         if (rc)
1793                 return rc;
1794
1795         if (cmd == ICARSAMODEXPO)
1796                 return trans_modexpo32(perms, filp, cmd, arg);
1797         if (cmd == ICARSACRT)
1798                 return trans_modexpo_crt32(perms, filp, cmd, arg);
1799         if (cmd == ZSECSENDCPRB)
1800                 return trans_xcRB32(perms, filp, cmd, arg);
1801         return zcrypt_unlocked_ioctl(filp, cmd, arg);
1802 }
1803 #endif
1804
1805 /*
1806  * Misc device file operations.
1807  */
1808 static const struct file_operations zcrypt_fops = {
1809         .owner          = THIS_MODULE,
1810         .read           = zcrypt_read,
1811         .write          = zcrypt_write,
1812         .unlocked_ioctl = zcrypt_unlocked_ioctl,
1813 #ifdef CONFIG_COMPAT
1814         .compat_ioctl   = zcrypt_compat_ioctl,
1815 #endif
1816         .open           = zcrypt_open,
1817         .release        = zcrypt_release,
1818         .llseek         = no_llseek,
1819 };
1820
1821 /*
1822  * Misc device.
1823  */
1824 static struct miscdevice zcrypt_misc_device = {
1825         .minor      = MISC_DYNAMIC_MINOR,
1826         .name       = "z90crypt",
1827         .fops       = &zcrypt_fops,
1828 };
1829
1830 static int zcrypt_rng_device_count;
1831 static u32 *zcrypt_rng_buffer;
1832 static int zcrypt_rng_buffer_index;
1833 static DEFINE_MUTEX(zcrypt_rng_mutex);
1834
1835 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1836 {
1837         int rc;
1838
1839         /*
1840          * We don't need locking here because the RNG API guarantees serialized
1841          * read method calls.
1842          */
1843         if (zcrypt_rng_buffer_index == 0) {
1844                 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1845                 /* on failure: retry once again after a requested rescan */
1846                 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1847                         rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1848                 if (rc < 0)
1849                         return -EIO;
1850                 zcrypt_rng_buffer_index = rc / sizeof(*data);
1851         }
1852         *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1853         return sizeof(*data);
1854 }
1855
1856 static struct hwrng zcrypt_rng_dev = {
1857         .name           = "zcrypt",
1858         .data_read      = zcrypt_rng_data_read,
1859         .quality        = 990,
1860 };
1861
1862 int zcrypt_rng_device_add(void)
1863 {
1864         int rc = 0;
1865
1866         mutex_lock(&zcrypt_rng_mutex);
1867         if (zcrypt_rng_device_count == 0) {
1868                 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1869                 if (!zcrypt_rng_buffer) {
1870                         rc = -ENOMEM;
1871                         goto out;
1872                 }
1873                 zcrypt_rng_buffer_index = 0;
1874                 if (!zcrypt_hwrng_seed)
1875                         zcrypt_rng_dev.quality = 0;
1876                 rc = hwrng_register(&zcrypt_rng_dev);
1877                 if (rc)
1878                         goto out_free;
1879                 zcrypt_rng_device_count = 1;
1880         } else
1881                 zcrypt_rng_device_count++;
1882         mutex_unlock(&zcrypt_rng_mutex);
1883         return 0;
1884
1885 out_free:
1886         free_page((unsigned long) zcrypt_rng_buffer);
1887 out:
1888         mutex_unlock(&zcrypt_rng_mutex);
1889         return rc;
1890 }
1891
1892 void zcrypt_rng_device_remove(void)
1893 {
1894         mutex_lock(&zcrypt_rng_mutex);
1895         zcrypt_rng_device_count--;
1896         if (zcrypt_rng_device_count == 0) {
1897                 hwrng_unregister(&zcrypt_rng_dev);
1898                 free_page((unsigned long) zcrypt_rng_buffer);
1899         }
1900         mutex_unlock(&zcrypt_rng_mutex);
1901 }
1902
1903 int __init zcrypt_debug_init(void)
1904 {
1905         zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1906                                          DBF_MAX_SPRINTF_ARGS * sizeof(long));
1907         debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1908         debug_set_level(zcrypt_dbf_info, DBF_ERR);
1909
1910         return 0;
1911 }
1912
1913 void zcrypt_debug_exit(void)
1914 {
1915         debug_unregister(zcrypt_dbf_info);
1916 }
1917
1918 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1919
1920 static int __init zcdn_init(void)
1921 {
1922         int rc;
1923
1924         /* create a new class 'zcrypt' */
1925         zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1926         if (IS_ERR(zcrypt_class)) {
1927                 rc = PTR_ERR(zcrypt_class);
1928                 goto out_class_create_failed;
1929         }
1930         zcrypt_class->dev_release = zcdn_device_release;
1931
1932         /* alloc device minor range */
1933         rc = alloc_chrdev_region(&zcrypt_devt,
1934                                  0, ZCRYPT_MAX_MINOR_NODES,
1935                                  ZCRYPT_NAME);
1936         if (rc)
1937                 goto out_alloc_chrdev_failed;
1938
1939         cdev_init(&zcrypt_cdev, &zcrypt_fops);
1940         zcrypt_cdev.owner = THIS_MODULE;
1941         rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1942         if (rc)
1943                 goto out_cdev_add_failed;
1944
1945         /* need some class specific sysfs attributes */
1946         rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1947         if (rc)
1948                 goto out_class_create_file_1_failed;
1949         rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1950         if (rc)
1951                 goto out_class_create_file_2_failed;
1952
1953         return 0;
1954
1955 out_class_create_file_2_failed:
1956         class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1957 out_class_create_file_1_failed:
1958         cdev_del(&zcrypt_cdev);
1959 out_cdev_add_failed:
1960         unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1961 out_alloc_chrdev_failed:
1962         class_destroy(zcrypt_class);
1963 out_class_create_failed:
1964         return rc;
1965 }
1966
1967 static void zcdn_exit(void)
1968 {
1969         class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1970         class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1971         zcdn_destroy_all();
1972         cdev_del(&zcrypt_cdev);
1973         unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1974         class_destroy(zcrypt_class);
1975 }
1976
1977 #endif
1978
1979 /**
1980  * zcrypt_api_init(): Module initialization.
1981  *
1982  * The module initialization code.
1983  */
1984 int __init zcrypt_api_init(void)
1985 {
1986         int rc;
1987
1988         rc = zcrypt_debug_init();
1989         if (rc)
1990                 goto out;
1991
1992 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1993         rc = zcdn_init();
1994         if (rc)
1995                 goto out;
1996 #endif
1997
1998         /* Register the request sprayer. */
1999         rc = misc_register(&zcrypt_misc_device);
2000         if (rc < 0)
2001                 goto out_misc_register_failed;
2002
2003         zcrypt_msgtype6_init();
2004         zcrypt_msgtype50_init();
2005
2006         return 0;
2007
2008 out_misc_register_failed:
2009 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2010         zcdn_exit();
2011 #endif
2012         zcrypt_debug_exit();
2013 out:
2014         return rc;
2015 }
2016
2017 /**
2018  * zcrypt_api_exit(): Module termination.
2019  *
2020  * The module termination code.
2021  */
2022 void __exit zcrypt_api_exit(void)
2023 {
2024 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2025         zcdn_exit();
2026 #endif
2027         misc_deregister(&zcrypt_misc_device);
2028         zcrypt_msgtype6_exit();
2029         zcrypt_msgtype50_exit();
2030         zcrypt_ccamisc_exit();
2031         zcrypt_ep11misc_exit();
2032         zcrypt_debug_exit();
2033 }
2034
2035 module_init(zcrypt_api_init);
2036 module_exit(zcrypt_api_exit);