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