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