2 * linux/drivers/s390/crypto/zcrypt_api.c
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>
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>
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)
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.
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.
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/interrupt.h>
33 #include <linux/miscdevice.h>
35 #include <linux/proc_fs.h>
36 #include <linux/compat.h>
37 #include <linux/smp_lock.h>
38 #include <asm/atomic.h>
39 #include <asm/uaccess.h>
40 #include <linux/hw_random.h>
42 #include "zcrypt_api.h"
47 MODULE_AUTHOR("IBM Corporation");
48 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, "
49 "Copyright 2001, 2006 IBM Corporation");
50 MODULE_LICENSE("GPL");
52 static DEFINE_SPINLOCK(zcrypt_device_lock);
53 static LIST_HEAD(zcrypt_device_list);
54 static int zcrypt_device_count = 0;
55 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
57 static int zcrypt_rng_device_add(void);
58 static void zcrypt_rng_device_remove(void);
61 * Device attributes common for all crypto devices.
63 static ssize_t zcrypt_type_show(struct device *dev,
64 struct device_attribute *attr, char *buf)
66 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
67 return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
70 static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
72 static ssize_t zcrypt_online_show(struct device *dev,
73 struct device_attribute *attr, char *buf)
75 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
76 return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
79 static ssize_t zcrypt_online_store(struct device *dev,
80 struct device_attribute *attr,
81 const char *buf, size_t count)
83 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
86 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
88 zdev->online = online;
90 ap_flush_queue(zdev->ap_dev);
94 static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
96 static struct attribute * zcrypt_device_attrs[] = {
98 &dev_attr_online.attr,
102 static struct attribute_group zcrypt_device_attr_group = {
103 .attrs = zcrypt_device_attrs,
107 * __zcrypt_increase_preference(): Increase preference of a crypto device.
108 * @zdev: Pointer the crypto device
110 * Move the device towards the head of the device list.
111 * Need to be called while holding the zcrypt device list lock.
112 * Note: cards with speed_rating of 0 are kept at the end of the list.
114 static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
116 struct zcrypt_device *tmp;
119 if (zdev->speed_rating == 0)
121 for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
122 tmp = list_entry(l, struct zcrypt_device, list);
123 if ((tmp->request_count + 1) * tmp->speed_rating <=
124 (zdev->request_count + 1) * zdev->speed_rating &&
125 tmp->speed_rating != 0)
128 if (l == zdev->list.prev)
130 /* Move zdev behind l */
131 list_move(&zdev->list, l);
135 * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
136 * @zdev: Pointer to a crypto device.
138 * Move the device towards the tail of the device list.
139 * Need to be called while holding the zcrypt device list lock.
140 * Note: cards with speed_rating of 0 are kept at the end of the list.
142 static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
144 struct zcrypt_device *tmp;
147 if (zdev->speed_rating == 0)
149 for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
150 tmp = list_entry(l, struct zcrypt_device, list);
151 if ((tmp->request_count + 1) * tmp->speed_rating >
152 (zdev->request_count + 1) * zdev->speed_rating ||
153 tmp->speed_rating == 0)
156 if (l == zdev->list.next)
158 /* Move zdev before l */
159 list_move_tail(&zdev->list, l);
162 static void zcrypt_device_release(struct kref *kref)
164 struct zcrypt_device *zdev =
165 container_of(kref, struct zcrypt_device, refcount);
166 zcrypt_device_free(zdev);
169 void zcrypt_device_get(struct zcrypt_device *zdev)
171 kref_get(&zdev->refcount);
173 EXPORT_SYMBOL(zcrypt_device_get);
175 int zcrypt_device_put(struct zcrypt_device *zdev)
177 return kref_put(&zdev->refcount, zcrypt_device_release);
179 EXPORT_SYMBOL(zcrypt_device_put);
181 struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
183 struct zcrypt_device *zdev;
185 zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
188 zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
189 if (!zdev->reply.message)
191 zdev->reply.length = max_response_size;
192 spin_lock_init(&zdev->lock);
193 INIT_LIST_HEAD(&zdev->list);
200 EXPORT_SYMBOL(zcrypt_device_alloc);
202 void zcrypt_device_free(struct zcrypt_device *zdev)
204 kfree(zdev->reply.message);
207 EXPORT_SYMBOL(zcrypt_device_free);
210 * zcrypt_device_register() - Register a crypto device.
211 * @zdev: Pointer to a crypto device
213 * Register a crypto device. Returns 0 if successful.
215 int zcrypt_device_register(struct zcrypt_device *zdev)
219 rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
220 &zcrypt_device_attr_group);
223 get_device(&zdev->ap_dev->device);
224 kref_init(&zdev->refcount);
225 spin_lock_bh(&zcrypt_device_lock);
226 zdev->online = 1; /* New devices are online by default. */
227 list_add_tail(&zdev->list, &zcrypt_device_list);
228 __zcrypt_increase_preference(zdev);
229 zcrypt_device_count++;
230 spin_unlock_bh(&zcrypt_device_lock);
231 if (zdev->ops->rng) {
232 rc = zcrypt_rng_device_add();
239 spin_lock_bh(&zcrypt_device_lock);
240 zcrypt_device_count--;
241 list_del_init(&zdev->list);
242 spin_unlock_bh(&zcrypt_device_lock);
243 sysfs_remove_group(&zdev->ap_dev->device.kobj,
244 &zcrypt_device_attr_group);
245 put_device(&zdev->ap_dev->device);
246 zcrypt_device_put(zdev);
250 EXPORT_SYMBOL(zcrypt_device_register);
253 * zcrypt_device_unregister(): Unregister a crypto device.
254 * @zdev: Pointer to crypto device
256 * Unregister a crypto device.
258 void zcrypt_device_unregister(struct zcrypt_device *zdev)
261 zcrypt_rng_device_remove();
262 spin_lock_bh(&zcrypt_device_lock);
263 zcrypt_device_count--;
264 list_del_init(&zdev->list);
265 spin_unlock_bh(&zcrypt_device_lock);
266 sysfs_remove_group(&zdev->ap_dev->device.kobj,
267 &zcrypt_device_attr_group);
268 put_device(&zdev->ap_dev->device);
269 zcrypt_device_put(zdev);
271 EXPORT_SYMBOL(zcrypt_device_unregister);
274 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
276 * This function is not supported beyond zcrypt 1.3.1.
278 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
279 size_t count, loff_t *f_pos)
285 * zcrypt_write(): Not allowed.
287 * Write is is not allowed
289 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
290 size_t count, loff_t *f_pos)
296 * zcrypt_open(): Count number of users.
298 * Device open function to count number of users.
300 static int zcrypt_open(struct inode *inode, struct file *filp)
303 atomic_inc(&zcrypt_open_count);
309 * zcrypt_release(): Count number of users.
311 * Device close function to count number of users.
313 static int zcrypt_release(struct inode *inode, struct file *filp)
315 atomic_dec(&zcrypt_open_count);
322 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
324 struct zcrypt_device *zdev;
327 if (mex->outputdatalength < mex->inputdatalength)
330 * As long as outputdatalength is big enough, we can set the
331 * outputdatalength equal to the inputdatalength, since that is the
332 * number of bytes we will copy in any case
334 mex->outputdatalength = mex->inputdatalength;
336 spin_lock_bh(&zcrypt_device_lock);
337 list_for_each_entry(zdev, &zcrypt_device_list, list) {
339 !zdev->ops->rsa_modexpo ||
340 zdev->min_mod_size > mex->inputdatalength ||
341 zdev->max_mod_size < mex->inputdatalength)
343 zcrypt_device_get(zdev);
344 get_device(&zdev->ap_dev->device);
345 zdev->request_count++;
346 __zcrypt_decrease_preference(zdev);
347 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
348 spin_unlock_bh(&zcrypt_device_lock);
349 rc = zdev->ops->rsa_modexpo(zdev, mex);
350 spin_lock_bh(&zcrypt_device_lock);
351 module_put(zdev->ap_dev->drv->driver.owner);
355 zdev->request_count--;
356 __zcrypt_increase_preference(zdev);
357 put_device(&zdev->ap_dev->device);
358 zcrypt_device_put(zdev);
359 spin_unlock_bh(&zcrypt_device_lock);
362 spin_unlock_bh(&zcrypt_device_lock);
366 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
368 struct zcrypt_device *zdev;
369 unsigned long long z1, z2, z3;
372 if (crt->outputdatalength < crt->inputdatalength ||
373 (crt->inputdatalength & 1))
376 * As long as outputdatalength is big enough, we can set the
377 * outputdatalength equal to the inputdatalength, since that is the
378 * number of bytes we will copy in any case
380 crt->outputdatalength = crt->inputdatalength;
384 spin_lock_bh(&zcrypt_device_lock);
385 list_for_each_entry(zdev, &zcrypt_device_list, list) {
387 !zdev->ops->rsa_modexpo_crt ||
388 zdev->min_mod_size > crt->inputdatalength ||
389 zdev->max_mod_size < crt->inputdatalength)
391 if (zdev->short_crt && crt->inputdatalength > 240) {
393 * Check inputdata for leading zeros for cards
394 * that can't handle np_prime, bp_key, or
395 * u_mult_inv > 128 bytes.
399 spin_unlock_bh(&zcrypt_device_lock);
400 /* len is max 256 / 2 - 120 = 8 */
401 len = crt->inputdatalength / 2 - 120;
403 if (copy_from_user(&z1, crt->np_prime, len) ||
404 copy_from_user(&z2, crt->bp_key, len) ||
405 copy_from_user(&z3, crt->u_mult_inv, len))
409 * We have to restart device lookup -
410 * the device list may have changed by now.
414 if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
415 /* The device can't handle this request. */
418 zcrypt_device_get(zdev);
419 get_device(&zdev->ap_dev->device);
420 zdev->request_count++;
421 __zcrypt_decrease_preference(zdev);
422 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
423 spin_unlock_bh(&zcrypt_device_lock);
424 rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
425 spin_lock_bh(&zcrypt_device_lock);
426 module_put(zdev->ap_dev->drv->driver.owner);
430 zdev->request_count--;
431 __zcrypt_increase_preference(zdev);
432 put_device(&zdev->ap_dev->device);
433 zcrypt_device_put(zdev);
434 spin_unlock_bh(&zcrypt_device_lock);
437 spin_unlock_bh(&zcrypt_device_lock);
441 static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
443 struct zcrypt_device *zdev;
446 spin_lock_bh(&zcrypt_device_lock);
447 list_for_each_entry(zdev, &zcrypt_device_list, list) {
448 if (!zdev->online || !zdev->ops->send_cprb ||
449 (xcRB->user_defined != AUTOSELECT &&
450 AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined)
453 zcrypt_device_get(zdev);
454 get_device(&zdev->ap_dev->device);
455 zdev->request_count++;
456 __zcrypt_decrease_preference(zdev);
457 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
458 spin_unlock_bh(&zcrypt_device_lock);
459 rc = zdev->ops->send_cprb(zdev, xcRB);
460 spin_lock_bh(&zcrypt_device_lock);
461 module_put(zdev->ap_dev->drv->driver.owner);
465 zdev->request_count--;
466 __zcrypt_increase_preference(zdev);
467 put_device(&zdev->ap_dev->device);
468 zcrypt_device_put(zdev);
469 spin_unlock_bh(&zcrypt_device_lock);
472 spin_unlock_bh(&zcrypt_device_lock);
476 static long zcrypt_rng(char *buffer)
478 struct zcrypt_device *zdev;
481 spin_lock_bh(&zcrypt_device_lock);
482 list_for_each_entry(zdev, &zcrypt_device_list, list) {
483 if (!zdev->online || !zdev->ops->rng)
485 zcrypt_device_get(zdev);
486 get_device(&zdev->ap_dev->device);
487 zdev->request_count++;
488 __zcrypt_decrease_preference(zdev);
489 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
490 spin_unlock_bh(&zcrypt_device_lock);
491 rc = zdev->ops->rng(zdev, buffer);
492 spin_lock_bh(&zcrypt_device_lock);
493 module_put(zdev->ap_dev->drv->driver.owner);
496 zdev->request_count--;
497 __zcrypt_increase_preference(zdev);
498 put_device(&zdev->ap_dev->device);
499 zcrypt_device_put(zdev);
500 spin_unlock_bh(&zcrypt_device_lock);
503 spin_unlock_bh(&zcrypt_device_lock);
507 static void zcrypt_status_mask(char status[AP_DEVICES])
509 struct zcrypt_device *zdev;
511 memset(status, 0, sizeof(char) * AP_DEVICES);
512 spin_lock_bh(&zcrypt_device_lock);
513 list_for_each_entry(zdev, &zcrypt_device_list, list)
514 status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
515 zdev->online ? zdev->user_space_type : 0x0d;
516 spin_unlock_bh(&zcrypt_device_lock);
519 static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
521 struct zcrypt_device *zdev;
523 memset(qdepth, 0, sizeof(char) * AP_DEVICES);
524 spin_lock_bh(&zcrypt_device_lock);
525 list_for_each_entry(zdev, &zcrypt_device_list, list) {
526 spin_lock(&zdev->ap_dev->lock);
527 qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
528 zdev->ap_dev->pendingq_count +
529 zdev->ap_dev->requestq_count;
530 spin_unlock(&zdev->ap_dev->lock);
532 spin_unlock_bh(&zcrypt_device_lock);
535 static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
537 struct zcrypt_device *zdev;
539 memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
540 spin_lock_bh(&zcrypt_device_lock);
541 list_for_each_entry(zdev, &zcrypt_device_list, list) {
542 spin_lock(&zdev->ap_dev->lock);
543 reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
544 zdev->ap_dev->total_request_count;
545 spin_unlock(&zdev->ap_dev->lock);
547 spin_unlock_bh(&zcrypt_device_lock);
550 static int zcrypt_pendingq_count(void)
552 struct zcrypt_device *zdev;
553 int pendingq_count = 0;
555 spin_lock_bh(&zcrypt_device_lock);
556 list_for_each_entry(zdev, &zcrypt_device_list, list) {
557 spin_lock(&zdev->ap_dev->lock);
558 pendingq_count += zdev->ap_dev->pendingq_count;
559 spin_unlock(&zdev->ap_dev->lock);
561 spin_unlock_bh(&zcrypt_device_lock);
562 return pendingq_count;
565 static int zcrypt_requestq_count(void)
567 struct zcrypt_device *zdev;
568 int requestq_count = 0;
570 spin_lock_bh(&zcrypt_device_lock);
571 list_for_each_entry(zdev, &zcrypt_device_list, list) {
572 spin_lock(&zdev->ap_dev->lock);
573 requestq_count += zdev->ap_dev->requestq_count;
574 spin_unlock(&zdev->ap_dev->lock);
576 spin_unlock_bh(&zcrypt_device_lock);
577 return requestq_count;
580 static int zcrypt_count_type(int type)
582 struct zcrypt_device *zdev;
583 int device_count = 0;
585 spin_lock_bh(&zcrypt_device_lock);
586 list_for_each_entry(zdev, &zcrypt_device_list, list)
587 if (zdev->user_space_type == type)
589 spin_unlock_bh(&zcrypt_device_lock);
594 * zcrypt_ica_status(): Old, depracted combi status call.
596 * Old, deprecated combi status call.
598 static long zcrypt_ica_status(struct file *filp, unsigned long arg)
600 struct ica_z90_status *pstat;
603 pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
606 pstat->totalcount = zcrypt_device_count;
607 pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
608 pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
609 pstat->requestqWaitCount = zcrypt_requestq_count();
610 pstat->pendingqWaitCount = zcrypt_pendingq_count();
611 pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
612 pstat->cryptoDomain = ap_domain_index;
613 zcrypt_status_mask(pstat->status);
614 zcrypt_qdepth_mask(pstat->qdepth);
616 if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
622 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
628 case ICARSAMODEXPO: {
629 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
630 struct ica_rsa_modexpo mex;
631 if (copy_from_user(&mex, umex, sizeof(mex)))
634 rc = zcrypt_rsa_modexpo(&mex);
635 } while (rc == -EAGAIN);
638 return put_user(mex.outputdatalength, &umex->outputdatalength);
641 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
642 struct ica_rsa_modexpo_crt crt;
643 if (copy_from_user(&crt, ucrt, sizeof(crt)))
646 rc = zcrypt_rsa_crt(&crt);
647 } while (rc == -EAGAIN);
650 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
653 struct ica_xcRB __user *uxcRB = (void __user *) arg;
654 struct ica_xcRB xcRB;
655 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
658 rc = zcrypt_send_cprb(&xcRB);
659 } while (rc == -EAGAIN);
660 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
664 case Z90STAT_STATUS_MASK: {
665 char status[AP_DEVICES];
666 zcrypt_status_mask(status);
667 if (copy_to_user((char __user *) arg, status,
668 sizeof(char) * AP_DEVICES))
672 case Z90STAT_QDEPTH_MASK: {
673 char qdepth[AP_DEVICES];
674 zcrypt_qdepth_mask(qdepth);
675 if (copy_to_user((char __user *) arg, qdepth,
676 sizeof(char) * AP_DEVICES))
680 case Z90STAT_PERDEV_REQCNT: {
681 int reqcnt[AP_DEVICES];
682 zcrypt_perdev_reqcnt(reqcnt);
683 if (copy_to_user((int __user *) arg, reqcnt,
684 sizeof(int) * AP_DEVICES))
688 case Z90STAT_REQUESTQ_COUNT:
689 return put_user(zcrypt_requestq_count(), (int __user *) arg);
690 case Z90STAT_PENDINGQ_COUNT:
691 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
692 case Z90STAT_TOTALOPEN_COUNT:
693 return put_user(atomic_read(&zcrypt_open_count),
695 case Z90STAT_DOMAIN_INDEX:
696 return put_user(ap_domain_index, (int __user *) arg);
698 * Deprecated ioctls. Don't add another device count ioctl,
699 * you can count them yourself in the user space with the
700 * output of the Z90STAT_STATUS_MASK ioctl.
703 return zcrypt_ica_status(filp, arg);
704 case Z90STAT_TOTALCOUNT:
705 return put_user(zcrypt_device_count, (int __user *) arg);
706 case Z90STAT_PCICACOUNT:
707 return put_user(zcrypt_count_type(ZCRYPT_PCICA),
709 case Z90STAT_PCICCCOUNT:
710 return put_user(zcrypt_count_type(ZCRYPT_PCICC),
712 case Z90STAT_PCIXCCMCL2COUNT:
713 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
715 case Z90STAT_PCIXCCMCL3COUNT:
716 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
718 case Z90STAT_PCIXCCCOUNT:
719 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
720 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
722 case Z90STAT_CEX2CCOUNT:
723 return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
725 case Z90STAT_CEX2ACOUNT:
726 return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
729 /* unknown ioctl number */
736 * ioctl32 conversion routines
738 struct compat_ica_rsa_modexpo {
739 compat_uptr_t inputdata;
740 unsigned int inputdatalength;
741 compat_uptr_t outputdata;
742 unsigned int outputdatalength;
744 compat_uptr_t n_modulus;
747 static long trans_modexpo32(struct file *filp, unsigned int cmd,
750 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
751 struct compat_ica_rsa_modexpo mex32;
752 struct ica_rsa_modexpo mex64;
755 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
757 mex64.inputdata = compat_ptr(mex32.inputdata);
758 mex64.inputdatalength = mex32.inputdatalength;
759 mex64.outputdata = compat_ptr(mex32.outputdata);
760 mex64.outputdatalength = mex32.outputdatalength;
761 mex64.b_key = compat_ptr(mex32.b_key);
762 mex64.n_modulus = compat_ptr(mex32.n_modulus);
764 rc = zcrypt_rsa_modexpo(&mex64);
765 } while (rc == -EAGAIN);
767 rc = put_user(mex64.outputdatalength,
768 &umex32->outputdatalength);
772 struct compat_ica_rsa_modexpo_crt {
773 compat_uptr_t inputdata;
774 unsigned int inputdatalength;
775 compat_uptr_t outputdata;
776 unsigned int outputdatalength;
777 compat_uptr_t bp_key;
778 compat_uptr_t bq_key;
779 compat_uptr_t np_prime;
780 compat_uptr_t nq_prime;
781 compat_uptr_t u_mult_inv;
784 static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
787 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
788 struct compat_ica_rsa_modexpo_crt crt32;
789 struct ica_rsa_modexpo_crt crt64;
792 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
794 crt64.inputdata = compat_ptr(crt32.inputdata);
795 crt64.inputdatalength = crt32.inputdatalength;
796 crt64.outputdata= compat_ptr(crt32.outputdata);
797 crt64.outputdatalength = crt32.outputdatalength;
798 crt64.bp_key = compat_ptr(crt32.bp_key);
799 crt64.bq_key = compat_ptr(crt32.bq_key);
800 crt64.np_prime = compat_ptr(crt32.np_prime);
801 crt64.nq_prime = compat_ptr(crt32.nq_prime);
802 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
804 rc = zcrypt_rsa_crt(&crt64);
805 } while (rc == -EAGAIN);
807 rc = put_user(crt64.outputdatalength,
808 &ucrt32->outputdatalength);
812 struct compat_ica_xcRB {
813 unsigned short agent_ID;
814 unsigned int user_defined;
815 unsigned short request_ID;
816 unsigned int request_control_blk_length;
817 unsigned char padding1[16 - sizeof (compat_uptr_t)];
818 compat_uptr_t request_control_blk_addr;
819 unsigned int request_data_length;
820 char padding2[16 - sizeof (compat_uptr_t)];
821 compat_uptr_t request_data_address;
822 unsigned int reply_control_blk_length;
823 char padding3[16 - sizeof (compat_uptr_t)];
824 compat_uptr_t reply_control_blk_addr;
825 unsigned int reply_data_length;
826 char padding4[16 - sizeof (compat_uptr_t)];
827 compat_uptr_t reply_data_addr;
828 unsigned short priority_window;
830 } __attribute__((packed));
832 static long trans_xcRB32(struct file *filp, unsigned int cmd,
835 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
836 struct compat_ica_xcRB xcRB32;
837 struct ica_xcRB xcRB64;
840 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
842 xcRB64.agent_ID = xcRB32.agent_ID;
843 xcRB64.user_defined = xcRB32.user_defined;
844 xcRB64.request_ID = xcRB32.request_ID;
845 xcRB64.request_control_blk_length =
846 xcRB32.request_control_blk_length;
847 xcRB64.request_control_blk_addr =
848 compat_ptr(xcRB32.request_control_blk_addr);
849 xcRB64.request_data_length =
850 xcRB32.request_data_length;
851 xcRB64.request_data_address =
852 compat_ptr(xcRB32.request_data_address);
853 xcRB64.reply_control_blk_length =
854 xcRB32.reply_control_blk_length;
855 xcRB64.reply_control_blk_addr =
856 compat_ptr(xcRB32.reply_control_blk_addr);
857 xcRB64.reply_data_length = xcRB32.reply_data_length;
858 xcRB64.reply_data_addr =
859 compat_ptr(xcRB32.reply_data_addr);
860 xcRB64.priority_window = xcRB32.priority_window;
861 xcRB64.status = xcRB32.status;
863 rc = zcrypt_send_cprb(&xcRB64);
864 } while (rc == -EAGAIN);
865 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
866 xcRB32.reply_data_length = xcRB64.reply_data_length;
867 xcRB32.status = xcRB64.status;
868 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
873 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
876 if (cmd == ICARSAMODEXPO)
877 return trans_modexpo32(filp, cmd, arg);
878 if (cmd == ICARSACRT)
879 return trans_modexpo_crt32(filp, cmd, arg);
880 if (cmd == ZSECSENDCPRB)
881 return trans_xcRB32(filp, cmd, arg);
882 return zcrypt_unlocked_ioctl(filp, cmd, arg);
887 * Misc device file operations.
889 static const struct file_operations zcrypt_fops = {
890 .owner = THIS_MODULE,
892 .write = zcrypt_write,
893 .unlocked_ioctl = zcrypt_unlocked_ioctl,
895 .compat_ioctl = zcrypt_compat_ioctl,
898 .release = zcrypt_release
904 static struct miscdevice zcrypt_misc_device = {
905 .minor = MISC_DYNAMIC_MINOR,
907 .fops = &zcrypt_fops,
911 * Deprecated /proc entry support.
913 static struct proc_dir_entry *zcrypt_entry;
915 static int sprintcl(unsigned char *outaddr, unsigned char *addr,
921 for (i = 0; i < len; i++)
922 hl += sprintf(outaddr+hl, "%01x", (unsigned int) addr[i]);
923 hl += sprintf(outaddr+hl, " ");
927 static int sprintrw(unsigned char *outaddr, unsigned char *addr,
932 hl = sprintf(outaddr, " ");
934 for (c = 0; c < (len / 16); c++) {
935 hl += sprintcl(outaddr+hl, addr+inl, 16);
940 hl += sprintcl(outaddr+hl, addr+inl, cx);
943 hl += sprintf(outaddr+hl, "\n");
947 static int sprinthx(unsigned char *title, unsigned char *outaddr,
948 unsigned char *addr, unsigned int len)
952 hl = sprintf(outaddr, "\n%s\n", title);
954 for (r = 0; r < (len / 64); r++) {
955 hl += sprintrw(outaddr+hl, addr+inl, 64);
960 hl += sprintrw(outaddr+hl, addr+inl, rx);
963 hl += sprintf(outaddr+hl, "\n");
967 static int sprinthx4(unsigned char *title, unsigned char *outaddr,
968 unsigned int *array, unsigned int len)
972 hl = sprintf(outaddr, "\n%s\n", title);
973 for (r = 0; r < len; r++) {
975 hl += sprintf(outaddr+hl, " ");
976 hl += sprintf(outaddr+hl, "%08X ", array[r]);
978 hl += sprintf(outaddr+hl, "\n");
980 hl += sprintf(outaddr+hl, "\n");
984 static int zcrypt_status_read(char *resp_buff, char **start, off_t offset,
985 int count, int *eof, void *data)
987 unsigned char *workarea;
992 /* resp_buff is a page. Use the right half for a work area */
993 workarea = resp_buff + 2000;
994 len += sprintf(resp_buff + len, "\nzcrypt version: %d.%d.%d\n",
995 ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
996 len += sprintf(resp_buff + len, "Cryptographic domain: %d\n",
998 len += sprintf(resp_buff + len, "Total device count: %d\n",
999 zcrypt_device_count);
1000 len += sprintf(resp_buff + len, "PCICA count: %d\n",
1001 zcrypt_count_type(ZCRYPT_PCICA));
1002 len += sprintf(resp_buff + len, "PCICC count: %d\n",
1003 zcrypt_count_type(ZCRYPT_PCICC));
1004 len += sprintf(resp_buff + len, "PCIXCC MCL2 count: %d\n",
1005 zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
1006 len += sprintf(resp_buff + len, "PCIXCC MCL3 count: %d\n",
1007 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1008 len += sprintf(resp_buff + len, "CEX2C count: %d\n",
1009 zcrypt_count_type(ZCRYPT_CEX2C));
1010 len += sprintf(resp_buff + len, "CEX2A count: %d\n",
1011 zcrypt_count_type(ZCRYPT_CEX2A));
1012 len += sprintf(resp_buff + len, "requestq count: %d\n",
1013 zcrypt_requestq_count());
1014 len += sprintf(resp_buff + len, "pendingq count: %d\n",
1015 zcrypt_pendingq_count());
1016 len += sprintf(resp_buff + len, "Total open handles: %d\n\n",
1017 atomic_read(&zcrypt_open_count));
1018 zcrypt_status_mask(workarea);
1019 len += sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1020 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A",
1021 resp_buff+len, workarea, AP_DEVICES);
1022 zcrypt_qdepth_mask(workarea);
1023 len += sprinthx("Waiting work element counts",
1024 resp_buff+len, workarea, AP_DEVICES);
1025 zcrypt_perdev_reqcnt((int *) workarea);
1026 len += sprinthx4("Per-device successfully completed request counts",
1027 resp_buff+len,(unsigned int *) workarea, AP_DEVICES);
1029 memset((void *) workarea, 0x00, AP_DEVICES * sizeof(unsigned int));
1033 static void zcrypt_disable_card(int index)
1035 struct zcrypt_device *zdev;
1037 spin_lock_bh(&zcrypt_device_lock);
1038 list_for_each_entry(zdev, &zcrypt_device_list, list)
1039 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1041 ap_flush_queue(zdev->ap_dev);
1044 spin_unlock_bh(&zcrypt_device_lock);
1047 static void zcrypt_enable_card(int index)
1049 struct zcrypt_device *zdev;
1051 spin_lock_bh(&zcrypt_device_lock);
1052 list_for_each_entry(zdev, &zcrypt_device_list, list)
1053 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1057 spin_unlock_bh(&zcrypt_device_lock);
1060 static int zcrypt_status_write(struct file *file, const char __user *buffer,
1061 unsigned long count, void *data)
1063 unsigned char *lbuf, *ptr;
1064 unsigned long local_count;
1070 #define LBUFSIZE 1200UL
1071 lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1075 local_count = min(LBUFSIZE - 1, count);
1076 if (copy_from_user(lbuf, buffer, local_count) != 0) {
1080 lbuf[local_count] = '\0';
1082 ptr = strstr(lbuf, "Online devices");
1085 ptr = strstr(ptr, "\n");
1090 if (strstr(ptr, "Waiting work element counts") == NULL)
1093 for (j = 0; j < 64 && *ptr; ptr++) {
1095 * '0' for no device, '1' for PCICA, '2' for PCICC,
1096 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1097 * '5' for CEX2C and '6' for CEX2A'
1099 if (*ptr >= '0' && *ptr <= '6')
1101 else if (*ptr == 'd' || *ptr == 'D')
1102 zcrypt_disable_card(j++);
1103 else if (*ptr == 'e' || *ptr == 'E')
1104 zcrypt_enable_card(j++);
1105 else if (*ptr != ' ' && *ptr != '\t')
1113 static int zcrypt_rng_device_count;
1114 static u32 *zcrypt_rng_buffer;
1115 static int zcrypt_rng_buffer_index;
1116 static DEFINE_MUTEX(zcrypt_rng_mutex);
1118 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1123 * We don't need locking here because the RNG API guarantees serialized
1124 * read method calls.
1126 if (zcrypt_rng_buffer_index == 0) {
1127 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1130 zcrypt_rng_buffer_index = rc / sizeof *data;
1132 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1133 return sizeof *data;
1136 static struct hwrng zcrypt_rng_dev = {
1138 .data_read = zcrypt_rng_data_read,
1141 static int zcrypt_rng_device_add(void)
1145 mutex_lock(&zcrypt_rng_mutex);
1146 if (zcrypt_rng_device_count == 0) {
1147 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1148 if (!zcrypt_rng_buffer) {
1152 zcrypt_rng_buffer_index = 0;
1153 rc = hwrng_register(&zcrypt_rng_dev);
1156 zcrypt_rng_device_count = 1;
1158 zcrypt_rng_device_count++;
1159 mutex_unlock(&zcrypt_rng_mutex);
1163 free_page((unsigned long) zcrypt_rng_buffer);
1165 mutex_unlock(&zcrypt_rng_mutex);
1169 static void zcrypt_rng_device_remove(void)
1171 mutex_lock(&zcrypt_rng_mutex);
1172 zcrypt_rng_device_count--;
1173 if (zcrypt_rng_device_count == 0) {
1174 hwrng_unregister(&zcrypt_rng_dev);
1175 free_page((unsigned long) zcrypt_rng_buffer);
1177 mutex_unlock(&zcrypt_rng_mutex);
1181 * zcrypt_api_init(): Module initialization.
1183 * The module initialization code.
1185 int __init zcrypt_api_init(void)
1189 /* Register the request sprayer. */
1190 rc = misc_register(&zcrypt_misc_device);
1194 /* Set up the proc file system */
1195 zcrypt_entry = create_proc_entry("driver/z90crypt", 0644, NULL);
1196 if (!zcrypt_entry) {
1200 zcrypt_entry->data = NULL;
1201 zcrypt_entry->read_proc = zcrypt_status_read;
1202 zcrypt_entry->write_proc = zcrypt_status_write;
1207 misc_deregister(&zcrypt_misc_device);
1213 * zcrypt_api_exit(): Module termination.
1215 * The module termination code.
1217 void zcrypt_api_exit(void)
1219 remove_proc_entry("driver/z90crypt", NULL);
1220 misc_deregister(&zcrypt_misc_device);
1223 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1224 module_init(zcrypt_api_init);
1225 module_exit(zcrypt_api_exit);