4 * Copyright IBM Corp. 2001, 2012
5 * Author(s): Robert Burroughs
6 * Eric Rossman (edrossma@us.ibm.com)
7 * Cornelia Huck <cornelia.huck@de.ibm.com>
9 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
10 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
11 * Ralph Wuerthner <rwuerthn@de.ibm.com>
12 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
32 #include <linux/miscdevice.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/compat.h>
37 #include <linux/slab.h>
38 #include <linux/atomic.h>
39 #include <asm/uaccess.h>
40 #include <linux/hw_random.h>
41 #include <linux/debugfs.h>
42 #include <asm/debug.h>
44 #include "zcrypt_debug.h"
45 #include "zcrypt_api.h"
47 #include "zcrypt_msgtype6.h"
52 MODULE_AUTHOR("IBM Corporation");
53 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
54 "Copyright IBM Corp. 2001, 2012");
55 MODULE_LICENSE("GPL");
57 static int zcrypt_hwrng_seed = 1;
58 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, S_IRUSR|S_IRGRP);
59 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
61 static DEFINE_SPINLOCK(zcrypt_device_lock);
62 static LIST_HEAD(zcrypt_device_list);
63 static int zcrypt_device_count = 0;
64 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
65 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
67 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
68 EXPORT_SYMBOL(zcrypt_rescan_req);
70 static int zcrypt_rng_device_add(void);
71 static void zcrypt_rng_device_remove(void);
73 static DEFINE_SPINLOCK(zcrypt_ops_list_lock);
74 static LIST_HEAD(zcrypt_ops_list);
76 static debug_info_t *zcrypt_dbf_common;
77 static debug_info_t *zcrypt_dbf_devices;
78 static struct dentry *debugfs_root;
81 * Device attributes common for all crypto devices.
83 static ssize_t zcrypt_type_show(struct device *dev,
84 struct device_attribute *attr, char *buf)
86 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
87 return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
90 static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
92 static ssize_t zcrypt_online_show(struct device *dev,
93 struct device_attribute *attr, char *buf)
95 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
96 return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
99 static ssize_t zcrypt_online_store(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
103 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
106 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
108 zdev->online = online;
109 ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dman", zdev->ap_dev->qid,
112 ap_flush_queue(zdev->ap_dev);
116 static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
118 static struct attribute * zcrypt_device_attrs[] = {
120 &dev_attr_online.attr,
124 static struct attribute_group zcrypt_device_attr_group = {
125 .attrs = zcrypt_device_attrs,
129 * Process a rescan of the transport layer.
131 * Returns 1, if the rescan has been processed, otherwise 0.
133 static inline int zcrypt_process_rescan(void)
135 if (atomic_read(&zcrypt_rescan_req)) {
136 atomic_set(&zcrypt_rescan_req, 0);
137 atomic_inc(&zcrypt_rescan_count);
138 ap_bus_force_rescan();
139 ZCRYPT_DBF_COMMON(DBF_INFO, "rescan%07d",
140 atomic_inc_return(&zcrypt_rescan_count));
147 * __zcrypt_increase_preference(): Increase preference of a crypto device.
148 * @zdev: Pointer the crypto device
150 * Move the device towards the head of the device list.
151 * Need to be called while holding the zcrypt device list lock.
152 * Note: cards with speed_rating of 0 are kept at the end of the list.
154 static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
156 struct zcrypt_device *tmp;
159 if (zdev->speed_rating == 0)
161 for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
162 tmp = list_entry(l, struct zcrypt_device, list);
163 if ((tmp->request_count + 1) * tmp->speed_rating <=
164 (zdev->request_count + 1) * zdev->speed_rating &&
165 tmp->speed_rating != 0)
168 if (l == zdev->list.prev)
170 /* Move zdev behind l */
171 list_move(&zdev->list, l);
175 * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
176 * @zdev: Pointer to a crypto device.
178 * Move the device towards the tail of the device list.
179 * Need to be called while holding the zcrypt device list lock.
180 * Note: cards with speed_rating of 0 are kept at the end of the list.
182 static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
184 struct zcrypt_device *tmp;
187 if (zdev->speed_rating == 0)
189 for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
190 tmp = list_entry(l, struct zcrypt_device, list);
191 if ((tmp->request_count + 1) * tmp->speed_rating >
192 (zdev->request_count + 1) * zdev->speed_rating ||
193 tmp->speed_rating == 0)
196 if (l == zdev->list.next)
198 /* Move zdev before l */
199 list_move_tail(&zdev->list, l);
202 static void zcrypt_device_release(struct kref *kref)
204 struct zcrypt_device *zdev =
205 container_of(kref, struct zcrypt_device, refcount);
206 zcrypt_device_free(zdev);
209 void zcrypt_device_get(struct zcrypt_device *zdev)
211 kref_get(&zdev->refcount);
213 EXPORT_SYMBOL(zcrypt_device_get);
215 int zcrypt_device_put(struct zcrypt_device *zdev)
217 return kref_put(&zdev->refcount, zcrypt_device_release);
219 EXPORT_SYMBOL(zcrypt_device_put);
221 struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
223 struct zcrypt_device *zdev;
225 zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
228 zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
229 if (!zdev->reply.message)
231 zdev->reply.length = max_response_size;
232 spin_lock_init(&zdev->lock);
233 INIT_LIST_HEAD(&zdev->list);
234 zdev->dbf_area = zcrypt_dbf_devices;
241 EXPORT_SYMBOL(zcrypt_device_alloc);
243 void zcrypt_device_free(struct zcrypt_device *zdev)
245 kfree(zdev->reply.message);
248 EXPORT_SYMBOL(zcrypt_device_free);
251 * zcrypt_device_register() - Register a crypto device.
252 * @zdev: Pointer to a crypto device
254 * Register a crypto device. Returns 0 if successful.
256 int zcrypt_device_register(struct zcrypt_device *zdev)
262 rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
263 &zcrypt_device_attr_group);
266 get_device(&zdev->ap_dev->device);
267 kref_init(&zdev->refcount);
268 spin_lock_bh(&zcrypt_device_lock);
269 zdev->online = 1; /* New devices are online by default. */
270 ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dreg", zdev->ap_dev->qid,
272 list_add_tail(&zdev->list, &zcrypt_device_list);
273 __zcrypt_increase_preference(zdev);
274 zcrypt_device_count++;
275 spin_unlock_bh(&zcrypt_device_lock);
276 if (zdev->ops->rng) {
277 rc = zcrypt_rng_device_add();
284 spin_lock_bh(&zcrypt_device_lock);
285 zcrypt_device_count--;
286 list_del_init(&zdev->list);
287 spin_unlock_bh(&zcrypt_device_lock);
288 sysfs_remove_group(&zdev->ap_dev->device.kobj,
289 &zcrypt_device_attr_group);
290 put_device(&zdev->ap_dev->device);
291 zcrypt_device_put(zdev);
295 EXPORT_SYMBOL(zcrypt_device_register);
298 * zcrypt_device_unregister(): Unregister a crypto device.
299 * @zdev: Pointer to crypto device
301 * Unregister a crypto device.
303 void zcrypt_device_unregister(struct zcrypt_device *zdev)
306 zcrypt_rng_device_remove();
307 spin_lock_bh(&zcrypt_device_lock);
308 zcrypt_device_count--;
309 list_del_init(&zdev->list);
310 spin_unlock_bh(&zcrypt_device_lock);
311 sysfs_remove_group(&zdev->ap_dev->device.kobj,
312 &zcrypt_device_attr_group);
313 put_device(&zdev->ap_dev->device);
314 zcrypt_device_put(zdev);
316 EXPORT_SYMBOL(zcrypt_device_unregister);
318 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
321 spin_lock_bh(&zcrypt_ops_list_lock);
322 list_add_tail(&zops->list, &zcrypt_ops_list);
323 spin_unlock_bh(&zcrypt_ops_list_lock);
326 EXPORT_SYMBOL(zcrypt_msgtype_register);
328 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
330 spin_lock_bh(&zcrypt_ops_list_lock);
331 list_del_init(&zops->list);
332 spin_unlock_bh(&zcrypt_ops_list_lock);
334 EXPORT_SYMBOL(zcrypt_msgtype_unregister);
337 struct zcrypt_ops *__ops_lookup(unsigned char *name, int variant)
339 struct zcrypt_ops *zops;
342 spin_lock_bh(&zcrypt_ops_list_lock);
343 list_for_each_entry(zops, &zcrypt_ops_list, list) {
344 if ((zops->variant == variant) &&
345 (!strncmp(zops->owner->name, name, MODULE_NAME_LEN))) {
350 if (!found || !try_module_get(zops->owner))
353 spin_unlock_bh(&zcrypt_ops_list_lock);
358 struct zcrypt_ops *zcrypt_msgtype_request(unsigned char *name, int variant)
360 struct zcrypt_ops *zops = NULL;
362 zops = __ops_lookup(name, variant);
364 request_module("%s", name);
365 zops = __ops_lookup(name, variant);
369 EXPORT_SYMBOL(zcrypt_msgtype_request);
371 void zcrypt_msgtype_release(struct zcrypt_ops *zops)
374 module_put(zops->owner);
376 EXPORT_SYMBOL(zcrypt_msgtype_release);
379 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
381 * This function is not supported beyond zcrypt 1.3.1.
383 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
384 size_t count, loff_t *f_pos)
390 * zcrypt_write(): Not allowed.
392 * Write is is not allowed
394 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
395 size_t count, loff_t *f_pos)
401 * zcrypt_open(): Count number of users.
403 * Device open function to count number of users.
405 static int zcrypt_open(struct inode *inode, struct file *filp)
407 atomic_inc(&zcrypt_open_count);
408 return nonseekable_open(inode, filp);
412 * zcrypt_release(): Count number of users.
414 * Device close function to count number of users.
416 static int zcrypt_release(struct inode *inode, struct file *filp)
418 atomic_dec(&zcrypt_open_count);
425 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
427 struct zcrypt_device *zdev;
430 if (mex->outputdatalength < mex->inputdatalength)
433 * As long as outputdatalength is big enough, we can set the
434 * outputdatalength equal to the inputdatalength, since that is the
435 * number of bytes we will copy in any case
437 mex->outputdatalength = mex->inputdatalength;
439 spin_lock_bh(&zcrypt_device_lock);
440 list_for_each_entry(zdev, &zcrypt_device_list, list) {
442 !zdev->ops->rsa_modexpo ||
443 zdev->min_mod_size > mex->inputdatalength ||
444 zdev->max_mod_size < mex->inputdatalength)
446 zcrypt_device_get(zdev);
447 get_device(&zdev->ap_dev->device);
448 zdev->request_count++;
449 __zcrypt_decrease_preference(zdev);
450 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
451 spin_unlock_bh(&zcrypt_device_lock);
452 rc = zdev->ops->rsa_modexpo(zdev, mex);
453 spin_lock_bh(&zcrypt_device_lock);
454 module_put(zdev->ap_dev->drv->driver.owner);
458 zdev->request_count--;
459 __zcrypt_increase_preference(zdev);
460 put_device(&zdev->ap_dev->device);
461 zcrypt_device_put(zdev);
462 spin_unlock_bh(&zcrypt_device_lock);
465 spin_unlock_bh(&zcrypt_device_lock);
469 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
471 struct zcrypt_device *zdev;
472 unsigned long long z1, z2, z3;
475 if (crt->outputdatalength < crt->inputdatalength)
478 * As long as outputdatalength is big enough, we can set the
479 * outputdatalength equal to the inputdatalength, since that is the
480 * number of bytes we will copy in any case
482 crt->outputdatalength = crt->inputdatalength;
486 spin_lock_bh(&zcrypt_device_lock);
487 list_for_each_entry(zdev, &zcrypt_device_list, list) {
489 !zdev->ops->rsa_modexpo_crt ||
490 zdev->min_mod_size > crt->inputdatalength ||
491 zdev->max_mod_size < crt->inputdatalength)
493 if (zdev->short_crt && crt->inputdatalength > 240) {
495 * Check inputdata for leading zeros for cards
496 * that can't handle np_prime, bp_key, or
497 * u_mult_inv > 128 bytes.
501 spin_unlock_bh(&zcrypt_device_lock);
502 /* len is max 256 / 2 - 120 = 8
503 * For bigger device just assume len of leading
504 * 0s is 8 as stated in the requirements for
505 * ica_rsa_modexpo_crt struct in zcrypt.h.
507 if (crt->inputdatalength <= 256)
508 len = crt->inputdatalength / 2 - 120;
511 if (len > sizeof(z1))
514 if (copy_from_user(&z1, crt->np_prime, len) ||
515 copy_from_user(&z2, crt->bp_key, len) ||
516 copy_from_user(&z3, crt->u_mult_inv, len))
521 * We have to restart device lookup -
522 * the device list may have changed by now.
526 if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
527 /* The device can't handle this request. */
530 zcrypt_device_get(zdev);
531 get_device(&zdev->ap_dev->device);
532 zdev->request_count++;
533 __zcrypt_decrease_preference(zdev);
534 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
535 spin_unlock_bh(&zcrypt_device_lock);
536 rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
537 spin_lock_bh(&zcrypt_device_lock);
538 module_put(zdev->ap_dev->drv->driver.owner);
542 zdev->request_count--;
543 __zcrypt_increase_preference(zdev);
544 put_device(&zdev->ap_dev->device);
545 zcrypt_device_put(zdev);
546 spin_unlock_bh(&zcrypt_device_lock);
549 spin_unlock_bh(&zcrypt_device_lock);
553 static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
555 struct zcrypt_device *zdev;
558 spin_lock_bh(&zcrypt_device_lock);
559 list_for_each_entry(zdev, &zcrypt_device_list, list) {
560 if (!zdev->online || !zdev->ops->send_cprb ||
561 (zdev->ops->variant == MSGTYPE06_VARIANT_EP11) ||
562 (xcRB->user_defined != AUTOSELECT &&
563 AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined))
565 zcrypt_device_get(zdev);
566 get_device(&zdev->ap_dev->device);
567 zdev->request_count++;
568 __zcrypt_decrease_preference(zdev);
569 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
570 spin_unlock_bh(&zcrypt_device_lock);
571 rc = zdev->ops->send_cprb(zdev, xcRB);
572 spin_lock_bh(&zcrypt_device_lock);
573 module_put(zdev->ap_dev->drv->driver.owner);
577 zdev->request_count--;
578 __zcrypt_increase_preference(zdev);
579 put_device(&zdev->ap_dev->device);
580 zcrypt_device_put(zdev);
581 spin_unlock_bh(&zcrypt_device_lock);
584 spin_unlock_bh(&zcrypt_device_lock);
588 struct ep11_target_dev_list {
589 unsigned short targets_num;
590 struct ep11_target_dev *targets;
593 static bool is_desired_ep11dev(unsigned int dev_qid,
594 struct ep11_target_dev_list dev_list)
598 for (n = 0; n < dev_list.targets_num; n++, dev_list.targets++) {
599 if ((AP_QID_DEVICE(dev_qid) == dev_list.targets->ap_id) &&
600 (AP_QID_QUEUE(dev_qid) == dev_list.targets->dom_id)) {
607 static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
609 struct zcrypt_device *zdev;
610 bool autoselect = false;
612 struct ep11_target_dev_list ep11_dev_list = {
617 ep11_dev_list.targets_num = (unsigned short) xcrb->targets_num;
619 /* empty list indicates autoselect (all available targets) */
620 if (ep11_dev_list.targets_num == 0)
623 ep11_dev_list.targets = kcalloc((unsigned short)
625 sizeof(struct ep11_target_dev),
627 if (!ep11_dev_list.targets)
630 if (copy_from_user(ep11_dev_list.targets,
631 (struct ep11_target_dev __force __user *)
632 xcrb->targets, xcrb->targets_num *
633 sizeof(struct ep11_target_dev)))
637 spin_lock_bh(&zcrypt_device_lock);
638 list_for_each_entry(zdev, &zcrypt_device_list, list) {
639 /* check if device is eligible */
641 zdev->ops->variant != MSGTYPE06_VARIANT_EP11)
644 /* check if device is selected as valid target */
645 if (!is_desired_ep11dev(zdev->ap_dev->qid, ep11_dev_list) &&
649 zcrypt_device_get(zdev);
650 get_device(&zdev->ap_dev->device);
651 zdev->request_count++;
652 __zcrypt_decrease_preference(zdev);
653 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
654 spin_unlock_bh(&zcrypt_device_lock);
655 rc = zdev->ops->send_ep11_cprb(zdev, xcrb);
656 spin_lock_bh(&zcrypt_device_lock);
657 module_put(zdev->ap_dev->drv->driver.owner);
661 zdev->request_count--;
662 __zcrypt_increase_preference(zdev);
663 put_device(&zdev->ap_dev->device);
664 zcrypt_device_put(zdev);
665 spin_unlock_bh(&zcrypt_device_lock);
668 spin_unlock_bh(&zcrypt_device_lock);
672 static long zcrypt_rng(char *buffer)
674 struct zcrypt_device *zdev;
677 spin_lock_bh(&zcrypt_device_lock);
678 list_for_each_entry(zdev, &zcrypt_device_list, list) {
679 if (!zdev->online || !zdev->ops->rng)
681 zcrypt_device_get(zdev);
682 get_device(&zdev->ap_dev->device);
683 zdev->request_count++;
684 __zcrypt_decrease_preference(zdev);
685 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
686 spin_unlock_bh(&zcrypt_device_lock);
687 rc = zdev->ops->rng(zdev, buffer);
688 spin_lock_bh(&zcrypt_device_lock);
689 module_put(zdev->ap_dev->drv->driver.owner);
692 zdev->request_count--;
693 __zcrypt_increase_preference(zdev);
694 put_device(&zdev->ap_dev->device);
695 zcrypt_device_put(zdev);
696 spin_unlock_bh(&zcrypt_device_lock);
699 spin_unlock_bh(&zcrypt_device_lock);
703 static void zcrypt_status_mask(char status[AP_DEVICES])
705 struct zcrypt_device *zdev;
707 memset(status, 0, sizeof(char) * AP_DEVICES);
708 spin_lock_bh(&zcrypt_device_lock);
709 list_for_each_entry(zdev, &zcrypt_device_list, list)
710 status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
711 zdev->online ? zdev->user_space_type : 0x0d;
712 spin_unlock_bh(&zcrypt_device_lock);
715 static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
717 struct zcrypt_device *zdev;
719 memset(qdepth, 0, sizeof(char) * AP_DEVICES);
720 spin_lock_bh(&zcrypt_device_lock);
721 list_for_each_entry(zdev, &zcrypt_device_list, list) {
722 spin_lock(&zdev->ap_dev->lock);
723 qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
724 zdev->ap_dev->pendingq_count +
725 zdev->ap_dev->requestq_count;
726 spin_unlock(&zdev->ap_dev->lock);
728 spin_unlock_bh(&zcrypt_device_lock);
731 static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
733 struct zcrypt_device *zdev;
735 memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
736 spin_lock_bh(&zcrypt_device_lock);
737 list_for_each_entry(zdev, &zcrypt_device_list, list) {
738 spin_lock(&zdev->ap_dev->lock);
739 reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
740 zdev->ap_dev->total_request_count;
741 spin_unlock(&zdev->ap_dev->lock);
743 spin_unlock_bh(&zcrypt_device_lock);
746 static int zcrypt_pendingq_count(void)
748 struct zcrypt_device *zdev;
749 int pendingq_count = 0;
751 spin_lock_bh(&zcrypt_device_lock);
752 list_for_each_entry(zdev, &zcrypt_device_list, list) {
753 spin_lock(&zdev->ap_dev->lock);
754 pendingq_count += zdev->ap_dev->pendingq_count;
755 spin_unlock(&zdev->ap_dev->lock);
757 spin_unlock_bh(&zcrypt_device_lock);
758 return pendingq_count;
761 static int zcrypt_requestq_count(void)
763 struct zcrypt_device *zdev;
764 int requestq_count = 0;
766 spin_lock_bh(&zcrypt_device_lock);
767 list_for_each_entry(zdev, &zcrypt_device_list, list) {
768 spin_lock(&zdev->ap_dev->lock);
769 requestq_count += zdev->ap_dev->requestq_count;
770 spin_unlock(&zdev->ap_dev->lock);
772 spin_unlock_bh(&zcrypt_device_lock);
773 return requestq_count;
776 static int zcrypt_count_type(int type)
778 struct zcrypt_device *zdev;
779 int device_count = 0;
781 spin_lock_bh(&zcrypt_device_lock);
782 list_for_each_entry(zdev, &zcrypt_device_list, list)
783 if (zdev->user_space_type == type)
785 spin_unlock_bh(&zcrypt_device_lock);
790 * zcrypt_ica_status(): Old, depracted combi status call.
792 * Old, deprecated combi status call.
794 static long zcrypt_ica_status(struct file *filp, unsigned long arg)
796 struct ica_z90_status *pstat;
799 pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
802 pstat->totalcount = zcrypt_device_count;
803 pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
804 pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
805 pstat->requestqWaitCount = zcrypt_requestq_count();
806 pstat->pendingqWaitCount = zcrypt_pendingq_count();
807 pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
808 pstat->cryptoDomain = ap_domain_index;
809 zcrypt_status_mask(pstat->status);
810 zcrypt_qdepth_mask(pstat->qdepth);
812 if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
818 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
824 case ICARSAMODEXPO: {
825 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
826 struct ica_rsa_modexpo mex;
827 if (copy_from_user(&mex, umex, sizeof(mex)))
830 rc = zcrypt_rsa_modexpo(&mex);
831 } while (rc == -EAGAIN);
832 /* on failure: retry once again after a requested rescan */
833 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
835 rc = zcrypt_rsa_modexpo(&mex);
836 } while (rc == -EAGAIN);
839 return put_user(mex.outputdatalength, &umex->outputdatalength);
842 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
843 struct ica_rsa_modexpo_crt crt;
844 if (copy_from_user(&crt, ucrt, sizeof(crt)))
847 rc = zcrypt_rsa_crt(&crt);
848 } while (rc == -EAGAIN);
849 /* on failure: retry once again after a requested rescan */
850 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
852 rc = zcrypt_rsa_crt(&crt);
853 } while (rc == -EAGAIN);
856 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
859 struct ica_xcRB __user *uxcRB = (void __user *) arg;
860 struct ica_xcRB xcRB;
861 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
864 rc = zcrypt_send_cprb(&xcRB);
865 } while (rc == -EAGAIN);
866 /* on failure: retry once again after a requested rescan */
867 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
869 rc = zcrypt_send_cprb(&xcRB);
870 } while (rc == -EAGAIN);
871 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
875 case ZSENDEP11CPRB: {
876 struct ep11_urb __user *uxcrb = (void __user *)arg;
877 struct ep11_urb xcrb;
878 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
881 rc = zcrypt_send_ep11_cprb(&xcrb);
882 } while (rc == -EAGAIN);
883 /* on failure: retry once again after a requested rescan */
884 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
886 rc = zcrypt_send_ep11_cprb(&xcrb);
887 } while (rc == -EAGAIN);
888 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
892 case Z90STAT_STATUS_MASK: {
893 char status[AP_DEVICES];
894 zcrypt_status_mask(status);
895 if (copy_to_user((char __user *) arg, status,
896 sizeof(char) * AP_DEVICES))
900 case Z90STAT_QDEPTH_MASK: {
901 char qdepth[AP_DEVICES];
902 zcrypt_qdepth_mask(qdepth);
903 if (copy_to_user((char __user *) arg, qdepth,
904 sizeof(char) * AP_DEVICES))
908 case Z90STAT_PERDEV_REQCNT: {
909 int reqcnt[AP_DEVICES];
910 zcrypt_perdev_reqcnt(reqcnt);
911 if (copy_to_user((int __user *) arg, reqcnt,
912 sizeof(int) * AP_DEVICES))
916 case Z90STAT_REQUESTQ_COUNT:
917 return put_user(zcrypt_requestq_count(), (int __user *) arg);
918 case Z90STAT_PENDINGQ_COUNT:
919 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
920 case Z90STAT_TOTALOPEN_COUNT:
921 return put_user(atomic_read(&zcrypt_open_count),
923 case Z90STAT_DOMAIN_INDEX:
924 return put_user(ap_domain_index, (int __user *) arg);
926 * Deprecated ioctls. Don't add another device count ioctl,
927 * you can count them yourself in the user space with the
928 * output of the Z90STAT_STATUS_MASK ioctl.
931 return zcrypt_ica_status(filp, arg);
932 case Z90STAT_TOTALCOUNT:
933 return put_user(zcrypt_device_count, (int __user *) arg);
934 case Z90STAT_PCICACOUNT:
935 return put_user(zcrypt_count_type(ZCRYPT_PCICA),
937 case Z90STAT_PCICCCOUNT:
938 return put_user(zcrypt_count_type(ZCRYPT_PCICC),
940 case Z90STAT_PCIXCCMCL2COUNT:
941 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
943 case Z90STAT_PCIXCCMCL3COUNT:
944 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
946 case Z90STAT_PCIXCCCOUNT:
947 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
948 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
950 case Z90STAT_CEX2CCOUNT:
951 return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
953 case Z90STAT_CEX2ACOUNT:
954 return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
957 /* unknown ioctl number */
964 * ioctl32 conversion routines
966 struct compat_ica_rsa_modexpo {
967 compat_uptr_t inputdata;
968 unsigned int inputdatalength;
969 compat_uptr_t outputdata;
970 unsigned int outputdatalength;
972 compat_uptr_t n_modulus;
975 static long trans_modexpo32(struct file *filp, unsigned int cmd,
978 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
979 struct compat_ica_rsa_modexpo mex32;
980 struct ica_rsa_modexpo mex64;
983 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
985 mex64.inputdata = compat_ptr(mex32.inputdata);
986 mex64.inputdatalength = mex32.inputdatalength;
987 mex64.outputdata = compat_ptr(mex32.outputdata);
988 mex64.outputdatalength = mex32.outputdatalength;
989 mex64.b_key = compat_ptr(mex32.b_key);
990 mex64.n_modulus = compat_ptr(mex32.n_modulus);
992 rc = zcrypt_rsa_modexpo(&mex64);
993 } while (rc == -EAGAIN);
994 /* on failure: retry once again after a requested rescan */
995 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
997 rc = zcrypt_rsa_modexpo(&mex64);
998 } while (rc == -EAGAIN);
1001 return put_user(mex64.outputdatalength,
1002 &umex32->outputdatalength);
1005 struct compat_ica_rsa_modexpo_crt {
1006 compat_uptr_t inputdata;
1007 unsigned int inputdatalength;
1008 compat_uptr_t outputdata;
1009 unsigned int outputdatalength;
1010 compat_uptr_t bp_key;
1011 compat_uptr_t bq_key;
1012 compat_uptr_t np_prime;
1013 compat_uptr_t nq_prime;
1014 compat_uptr_t u_mult_inv;
1017 static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
1020 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1021 struct compat_ica_rsa_modexpo_crt crt32;
1022 struct ica_rsa_modexpo_crt crt64;
1025 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1027 crt64.inputdata = compat_ptr(crt32.inputdata);
1028 crt64.inputdatalength = crt32.inputdatalength;
1029 crt64.outputdata= compat_ptr(crt32.outputdata);
1030 crt64.outputdatalength = crt32.outputdatalength;
1031 crt64.bp_key = compat_ptr(crt32.bp_key);
1032 crt64.bq_key = compat_ptr(crt32.bq_key);
1033 crt64.np_prime = compat_ptr(crt32.np_prime);
1034 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1035 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1037 rc = zcrypt_rsa_crt(&crt64);
1038 } while (rc == -EAGAIN);
1039 /* on failure: retry once again after a requested rescan */
1040 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1042 rc = zcrypt_rsa_crt(&crt64);
1043 } while (rc == -EAGAIN);
1046 return put_user(crt64.outputdatalength,
1047 &ucrt32->outputdatalength);
1050 struct compat_ica_xcRB {
1051 unsigned short agent_ID;
1052 unsigned int user_defined;
1053 unsigned short request_ID;
1054 unsigned int request_control_blk_length;
1055 unsigned char padding1[16 - sizeof (compat_uptr_t)];
1056 compat_uptr_t request_control_blk_addr;
1057 unsigned int request_data_length;
1058 char padding2[16 - sizeof (compat_uptr_t)];
1059 compat_uptr_t request_data_address;
1060 unsigned int reply_control_blk_length;
1061 char padding3[16 - sizeof (compat_uptr_t)];
1062 compat_uptr_t reply_control_blk_addr;
1063 unsigned int reply_data_length;
1064 char padding4[16 - sizeof (compat_uptr_t)];
1065 compat_uptr_t reply_data_addr;
1066 unsigned short priority_window;
1067 unsigned int status;
1068 } __attribute__((packed));
1070 static long trans_xcRB32(struct file *filp, unsigned int cmd,
1073 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1074 struct compat_ica_xcRB xcRB32;
1075 struct ica_xcRB xcRB64;
1078 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1080 xcRB64.agent_ID = xcRB32.agent_ID;
1081 xcRB64.user_defined = xcRB32.user_defined;
1082 xcRB64.request_ID = xcRB32.request_ID;
1083 xcRB64.request_control_blk_length =
1084 xcRB32.request_control_blk_length;
1085 xcRB64.request_control_blk_addr =
1086 compat_ptr(xcRB32.request_control_blk_addr);
1087 xcRB64.request_data_length =
1088 xcRB32.request_data_length;
1089 xcRB64.request_data_address =
1090 compat_ptr(xcRB32.request_data_address);
1091 xcRB64.reply_control_blk_length =
1092 xcRB32.reply_control_blk_length;
1093 xcRB64.reply_control_blk_addr =
1094 compat_ptr(xcRB32.reply_control_blk_addr);
1095 xcRB64.reply_data_length = xcRB32.reply_data_length;
1096 xcRB64.reply_data_addr =
1097 compat_ptr(xcRB32.reply_data_addr);
1098 xcRB64.priority_window = xcRB32.priority_window;
1099 xcRB64.status = xcRB32.status;
1101 rc = zcrypt_send_cprb(&xcRB64);
1102 } while (rc == -EAGAIN);
1103 /* on failure: retry once again after a requested rescan */
1104 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1106 rc = zcrypt_send_cprb(&xcRB64);
1107 } while (rc == -EAGAIN);
1108 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1109 xcRB32.reply_data_length = xcRB64.reply_data_length;
1110 xcRB32.status = xcRB64.status;
1111 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1116 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1119 if (cmd == ICARSAMODEXPO)
1120 return trans_modexpo32(filp, cmd, arg);
1121 if (cmd == ICARSACRT)
1122 return trans_modexpo_crt32(filp, cmd, arg);
1123 if (cmd == ZSECSENDCPRB)
1124 return trans_xcRB32(filp, cmd, arg);
1125 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1130 * Misc device file operations.
1132 static const struct file_operations zcrypt_fops = {
1133 .owner = THIS_MODULE,
1134 .read = zcrypt_read,
1135 .write = zcrypt_write,
1136 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1137 #ifdef CONFIG_COMPAT
1138 .compat_ioctl = zcrypt_compat_ioctl,
1140 .open = zcrypt_open,
1141 .release = zcrypt_release,
1142 .llseek = no_llseek,
1148 static struct miscdevice zcrypt_misc_device = {
1149 .minor = MISC_DYNAMIC_MINOR,
1151 .fops = &zcrypt_fops,
1155 * Deprecated /proc entry support.
1157 static struct proc_dir_entry *zcrypt_entry;
1159 static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
1163 for (i = 0; i < len; i++)
1164 seq_printf(m, "%01x", (unsigned int) addr[i]);
1168 static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
1174 for (c = 0; c < (len / 16); c++) {
1175 sprintcl(m, addr+inl, 16);
1180 sprintcl(m, addr+inl, cx);
1186 static void sprinthx(unsigned char *title, struct seq_file *m,
1187 unsigned char *addr, unsigned int len)
1191 seq_printf(m, "\n%s\n", title);
1193 for (r = 0; r < (len / 64); r++) {
1194 sprintrw(m, addr+inl, 64);
1199 sprintrw(m, addr+inl, rx);
1205 static void sprinthx4(unsigned char *title, struct seq_file *m,
1206 unsigned int *array, unsigned int len)
1208 seq_printf(m, "\n%s\n", title);
1209 seq_hex_dump(m, " ", DUMP_PREFIX_NONE, 32, 4, array, len, false);
1213 static int zcrypt_proc_show(struct seq_file *m, void *v)
1215 char workarea[sizeof(int) * AP_DEVICES];
1217 seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
1218 ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
1219 seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
1220 seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
1221 seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
1222 seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
1223 seq_printf(m, "PCIXCC MCL2 count: %d\n",
1224 zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
1225 seq_printf(m, "PCIXCC MCL3 count: %d\n",
1226 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1227 seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
1228 seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
1229 seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
1230 seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
1231 seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
1232 seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
1233 seq_printf(m, "Total open handles: %d\n\n",
1234 atomic_read(&zcrypt_open_count));
1235 zcrypt_status_mask(workarea);
1236 sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1237 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1238 m, workarea, AP_DEVICES);
1239 zcrypt_qdepth_mask(workarea);
1240 sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
1241 zcrypt_perdev_reqcnt((int *) workarea);
1242 sprinthx4("Per-device successfully completed request counts",
1243 m, (unsigned int *) workarea, AP_DEVICES);
1247 static int zcrypt_proc_open(struct inode *inode, struct file *file)
1249 return single_open(file, zcrypt_proc_show, NULL);
1252 static void zcrypt_disable_card(int index)
1254 struct zcrypt_device *zdev;
1256 spin_lock_bh(&zcrypt_device_lock);
1257 list_for_each_entry(zdev, &zcrypt_device_list, list)
1258 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1260 ap_flush_queue(zdev->ap_dev);
1263 spin_unlock_bh(&zcrypt_device_lock);
1266 static void zcrypt_enable_card(int index)
1268 struct zcrypt_device *zdev;
1270 spin_lock_bh(&zcrypt_device_lock);
1271 list_for_each_entry(zdev, &zcrypt_device_list, list)
1272 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1276 spin_unlock_bh(&zcrypt_device_lock);
1279 static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
1280 size_t count, loff_t *pos)
1282 unsigned char *lbuf, *ptr;
1289 #define LBUFSIZE 1200UL
1290 lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1294 local_count = min(LBUFSIZE - 1, count);
1295 if (copy_from_user(lbuf, buffer, local_count) != 0) {
1299 lbuf[local_count] = '\0';
1301 ptr = strstr(lbuf, "Online devices");
1304 ptr = strstr(ptr, "\n");
1309 if (strstr(ptr, "Waiting work element counts") == NULL)
1312 for (j = 0; j < 64 && *ptr; ptr++) {
1314 * '0' for no device, '1' for PCICA, '2' for PCICC,
1315 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1316 * '5' for CEX2C and '6' for CEX2A'
1317 * '7' for CEX3C and '8' for CEX3A
1319 if (*ptr >= '0' && *ptr <= '8')
1321 else if (*ptr == 'd' || *ptr == 'D')
1322 zcrypt_disable_card(j++);
1323 else if (*ptr == 'e' || *ptr == 'E')
1324 zcrypt_enable_card(j++);
1325 else if (*ptr != ' ' && *ptr != '\t')
1333 static const struct file_operations zcrypt_proc_fops = {
1334 .owner = THIS_MODULE,
1335 .open = zcrypt_proc_open,
1337 .llseek = seq_lseek,
1338 .release = single_release,
1339 .write = zcrypt_proc_write,
1342 static int zcrypt_rng_device_count;
1343 static u32 *zcrypt_rng_buffer;
1344 static int zcrypt_rng_buffer_index;
1345 static DEFINE_MUTEX(zcrypt_rng_mutex);
1347 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1352 * We don't need locking here because the RNG API guarantees serialized
1353 * read method calls.
1355 if (zcrypt_rng_buffer_index == 0) {
1356 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1357 /* on failure: retry once again after a requested rescan */
1358 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1359 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1362 zcrypt_rng_buffer_index = rc / sizeof *data;
1364 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1365 return sizeof *data;
1368 static struct hwrng zcrypt_rng_dev = {
1370 .data_read = zcrypt_rng_data_read,
1374 static int zcrypt_rng_device_add(void)
1378 mutex_lock(&zcrypt_rng_mutex);
1379 if (zcrypt_rng_device_count == 0) {
1380 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1381 if (!zcrypt_rng_buffer) {
1385 zcrypt_rng_buffer_index = 0;
1386 if (!zcrypt_hwrng_seed)
1387 zcrypt_rng_dev.quality = 0;
1388 rc = hwrng_register(&zcrypt_rng_dev);
1391 zcrypt_rng_device_count = 1;
1393 zcrypt_rng_device_count++;
1394 mutex_unlock(&zcrypt_rng_mutex);
1398 free_page((unsigned long) zcrypt_rng_buffer);
1400 mutex_unlock(&zcrypt_rng_mutex);
1404 static void zcrypt_rng_device_remove(void)
1406 mutex_lock(&zcrypt_rng_mutex);
1407 zcrypt_rng_device_count--;
1408 if (zcrypt_rng_device_count == 0) {
1409 hwrng_unregister(&zcrypt_rng_dev);
1410 free_page((unsigned long) zcrypt_rng_buffer);
1412 mutex_unlock(&zcrypt_rng_mutex);
1415 int __init zcrypt_debug_init(void)
1417 debugfs_root = debugfs_create_dir("zcrypt", NULL);
1419 zcrypt_dbf_common = debug_register("zcrypt_common", 1, 1, 16);
1420 debug_register_view(zcrypt_dbf_common, &debug_hex_ascii_view);
1421 debug_set_level(zcrypt_dbf_common, DBF_ERR);
1423 zcrypt_dbf_devices = debug_register("zcrypt_devices", 1, 1, 16);
1424 debug_register_view(zcrypt_dbf_devices, &debug_hex_ascii_view);
1425 debug_set_level(zcrypt_dbf_devices, DBF_ERR);
1430 void zcrypt_debug_exit(void)
1432 debugfs_remove(debugfs_root);
1433 if (zcrypt_dbf_common)
1434 debug_unregister(zcrypt_dbf_common);
1435 if (zcrypt_dbf_devices)
1436 debug_unregister(zcrypt_dbf_devices);
1440 * zcrypt_api_init(): Module initialization.
1442 * The module initialization code.
1444 int __init zcrypt_api_init(void)
1448 rc = zcrypt_debug_init();
1452 atomic_set(&zcrypt_rescan_req, 0);
1454 /* Register the request sprayer. */
1455 rc = misc_register(&zcrypt_misc_device);
1459 /* Set up the proc file system */
1460 zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
1461 if (!zcrypt_entry) {
1469 misc_deregister(&zcrypt_misc_device);
1475 * zcrypt_api_exit(): Module termination.
1477 * The module termination code.
1479 void zcrypt_api_exit(void)
1481 remove_proc_entry("driver/z90crypt", NULL);
1482 misc_deregister(&zcrypt_misc_device);
1483 zcrypt_debug_exit();
1486 module_init(zcrypt_api_init);
1487 module_exit(zcrypt_api_exit);