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