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