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