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