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