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