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 <asm/atomic.h>
38 #include <asm/uaccess.h>
40 #include "zcrypt_api.h"
45 MODULE_AUTHOR("IBM Corporation");
46 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, "
47 "Copyright 2001, 2006 IBM Corporation");
48 MODULE_LICENSE("GPL");
50 static DEFINE_SPINLOCK(zcrypt_device_lock);
51 static LIST_HEAD(zcrypt_device_list);
52 static int zcrypt_device_count = 0;
53 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
56 * Device attributes common for all crypto devices.
58 static ssize_t zcrypt_type_show(struct device *dev,
59 struct device_attribute *attr, char *buf)
61 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
62 return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
65 static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
67 static ssize_t zcrypt_online_show(struct device *dev,
68 struct device_attribute *attr, char *buf)
70 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
71 return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
74 static ssize_t zcrypt_online_store(struct device *dev,
75 struct device_attribute *attr,
76 const char *buf, size_t count)
78 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
81 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
83 zdev->online = online;
85 ap_flush_queue(zdev->ap_dev);
89 static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
91 static struct attribute * zcrypt_device_attrs[] = {
93 &dev_attr_online.attr,
97 static struct attribute_group zcrypt_device_attr_group = {
98 .attrs = zcrypt_device_attrs,
102 * Move the device towards the head of the device list.
103 * Need to be called while holding the zcrypt device list lock.
104 * Note: cards with speed_rating of 0 are kept at the end of the list.
106 static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
108 struct zcrypt_device *tmp;
111 if (zdev->speed_rating == 0)
113 for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
114 tmp = list_entry(l, struct zcrypt_device, list);
115 if ((tmp->request_count + 1) * tmp->speed_rating <=
116 (zdev->request_count + 1) * zdev->speed_rating &&
117 tmp->speed_rating != 0)
120 if (l == zdev->list.prev)
122 /* Move zdev behind l */
123 list_del(&zdev->list);
124 list_add(&zdev->list, l);
128 * Move the device towards the tail of the device list.
129 * Need to be called while holding the zcrypt device list lock.
130 * Note: cards with speed_rating of 0 are kept at the end of the list.
132 static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
134 struct zcrypt_device *tmp;
137 if (zdev->speed_rating == 0)
139 for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
140 tmp = list_entry(l, struct zcrypt_device, list);
141 if ((tmp->request_count + 1) * tmp->speed_rating >
142 (zdev->request_count + 1) * zdev->speed_rating ||
143 tmp->speed_rating == 0)
146 if (l == zdev->list.next)
148 /* Move zdev before l */
149 list_del(&zdev->list);
150 list_add_tail(&zdev->list, l);
153 static void zcrypt_device_release(struct kref *kref)
155 struct zcrypt_device *zdev =
156 container_of(kref, struct zcrypt_device, refcount);
157 zcrypt_device_free(zdev);
160 void zcrypt_device_get(struct zcrypt_device *zdev)
162 kref_get(&zdev->refcount);
164 EXPORT_SYMBOL(zcrypt_device_get);
166 int zcrypt_device_put(struct zcrypt_device *zdev)
168 return kref_put(&zdev->refcount, zcrypt_device_release);
170 EXPORT_SYMBOL(zcrypt_device_put);
172 struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
174 struct zcrypt_device *zdev;
176 zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
179 zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
180 if (!zdev->reply.message)
182 zdev->reply.length = max_response_size;
183 spin_lock_init(&zdev->lock);
184 INIT_LIST_HEAD(&zdev->list);
191 EXPORT_SYMBOL(zcrypt_device_alloc);
193 void zcrypt_device_free(struct zcrypt_device *zdev)
195 kfree(zdev->reply.message);
198 EXPORT_SYMBOL(zcrypt_device_free);
201 * Register a crypto device.
203 int zcrypt_device_register(struct zcrypt_device *zdev)
207 rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
208 &zcrypt_device_attr_group);
211 get_device(&zdev->ap_dev->device);
212 kref_init(&zdev->refcount);
213 spin_lock_bh(&zcrypt_device_lock);
214 zdev->online = 1; /* New devices are online by default. */
215 list_add_tail(&zdev->list, &zcrypt_device_list);
216 __zcrypt_increase_preference(zdev);
217 zcrypt_device_count++;
218 spin_unlock_bh(&zcrypt_device_lock);
222 EXPORT_SYMBOL(zcrypt_device_register);
225 * Unregister a crypto device.
227 void zcrypt_device_unregister(struct zcrypt_device *zdev)
229 spin_lock_bh(&zcrypt_device_lock);
230 zcrypt_device_count--;
231 list_del_init(&zdev->list);
232 spin_unlock_bh(&zcrypt_device_lock);
233 sysfs_remove_group(&zdev->ap_dev->device.kobj,
234 &zcrypt_device_attr_group);
235 put_device(&zdev->ap_dev->device);
236 zcrypt_device_put(zdev);
238 EXPORT_SYMBOL(zcrypt_device_unregister);
241 * zcrypt_read is not be supported beyond zcrypt 1.3.1
243 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
244 size_t count, loff_t *f_pos)
250 * Write is is not allowed
252 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
253 size_t count, loff_t *f_pos)
259 * Device open/close functions to count number of users.
261 static int zcrypt_open(struct inode *inode, struct file *filp)
263 atomic_inc(&zcrypt_open_count);
267 static int zcrypt_release(struct inode *inode, struct file *filp)
269 atomic_dec(&zcrypt_open_count);
276 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
278 struct zcrypt_device *zdev;
281 if (mex->outputdatalength < mex->inputdatalength)
284 * As long as outputdatalength is big enough, we can set the
285 * outputdatalength equal to the inputdatalength, since that is the
286 * number of bytes we will copy in any case
288 mex->outputdatalength = mex->inputdatalength;
290 spin_lock_bh(&zcrypt_device_lock);
291 list_for_each_entry(zdev, &zcrypt_device_list, list) {
293 !zdev->ops->rsa_modexpo ||
294 zdev->min_mod_size > mex->inputdatalength ||
295 zdev->max_mod_size < mex->inputdatalength)
297 zcrypt_device_get(zdev);
298 get_device(&zdev->ap_dev->device);
299 zdev->request_count++;
300 __zcrypt_decrease_preference(zdev);
301 spin_unlock_bh(&zcrypt_device_lock);
302 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
303 rc = zdev->ops->rsa_modexpo(zdev, mex);
304 module_put(zdev->ap_dev->drv->driver.owner);
308 spin_lock_bh(&zcrypt_device_lock);
309 zdev->request_count--;
310 __zcrypt_increase_preference(zdev);
311 put_device(&zdev->ap_dev->device);
312 zcrypt_device_put(zdev);
313 spin_unlock_bh(&zcrypt_device_lock);
316 spin_unlock_bh(&zcrypt_device_lock);
320 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
322 struct zcrypt_device *zdev;
323 unsigned long long z1, z2, z3;
326 if (crt->outputdatalength < crt->inputdatalength ||
327 (crt->inputdatalength & 1))
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 crt->outputdatalength = crt->inputdatalength;
338 spin_lock_bh(&zcrypt_device_lock);
339 list_for_each_entry(zdev, &zcrypt_device_list, list) {
341 !zdev->ops->rsa_modexpo_crt ||
342 zdev->min_mod_size > crt->inputdatalength ||
343 zdev->max_mod_size < crt->inputdatalength)
345 if (zdev->short_crt && crt->inputdatalength > 240) {
347 * Check inputdata for leading zeros for cards
348 * that can't handle np_prime, bp_key, or
349 * u_mult_inv > 128 bytes.
353 spin_unlock_bh(&zcrypt_device_lock);
354 /* len is max 256 / 2 - 120 = 8 */
355 len = crt->inputdatalength / 2 - 120;
357 if (copy_from_user(&z1, crt->np_prime, len) ||
358 copy_from_user(&z2, crt->bp_key, len) ||
359 copy_from_user(&z3, crt->u_mult_inv, len))
363 * We have to restart device lookup -
364 * the device list may have changed by now.
368 if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
369 /* The device can't handle this request. */
372 zcrypt_device_get(zdev);
373 get_device(&zdev->ap_dev->device);
374 zdev->request_count++;
375 __zcrypt_decrease_preference(zdev);
376 spin_unlock_bh(&zcrypt_device_lock);
377 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
378 rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
379 module_put(zdev->ap_dev->drv->driver.owner);
383 spin_lock_bh(&zcrypt_device_lock);
384 zdev->request_count--;
385 __zcrypt_increase_preference(zdev);
386 put_device(&zdev->ap_dev->device);
387 zcrypt_device_put(zdev);
388 spin_unlock_bh(&zcrypt_device_lock);
391 spin_unlock_bh(&zcrypt_device_lock);
395 static void zcrypt_status_mask(char status[AP_DEVICES])
397 struct zcrypt_device *zdev;
399 memset(status, 0, sizeof(char) * AP_DEVICES);
400 spin_lock_bh(&zcrypt_device_lock);
401 list_for_each_entry(zdev, &zcrypt_device_list, list)
402 status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
403 zdev->online ? zdev->user_space_type : 0x0d;
404 spin_unlock_bh(&zcrypt_device_lock);
407 static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
409 struct zcrypt_device *zdev;
411 memset(qdepth, 0, sizeof(char) * AP_DEVICES);
412 spin_lock_bh(&zcrypt_device_lock);
413 list_for_each_entry(zdev, &zcrypt_device_list, list) {
414 spin_lock(&zdev->ap_dev->lock);
415 qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
416 zdev->ap_dev->pendingq_count +
417 zdev->ap_dev->requestq_count;
418 spin_unlock(&zdev->ap_dev->lock);
420 spin_unlock_bh(&zcrypt_device_lock);
423 static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
425 struct zcrypt_device *zdev;
427 memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
428 spin_lock_bh(&zcrypt_device_lock);
429 list_for_each_entry(zdev, &zcrypt_device_list, list) {
430 spin_lock(&zdev->ap_dev->lock);
431 reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
432 zdev->ap_dev->total_request_count;
433 spin_unlock(&zdev->ap_dev->lock);
435 spin_unlock_bh(&zcrypt_device_lock);
438 static int zcrypt_pendingq_count(void)
440 struct zcrypt_device *zdev;
441 int pendingq_count = 0;
443 spin_lock_bh(&zcrypt_device_lock);
444 list_for_each_entry(zdev, &zcrypt_device_list, list) {
445 spin_lock(&zdev->ap_dev->lock);
446 pendingq_count += zdev->ap_dev->pendingq_count;
447 spin_unlock(&zdev->ap_dev->lock);
449 spin_unlock_bh(&zcrypt_device_lock);
450 return pendingq_count;
453 static int zcrypt_requestq_count(void)
455 struct zcrypt_device *zdev;
456 int requestq_count = 0;
458 spin_lock_bh(&zcrypt_device_lock);
459 list_for_each_entry(zdev, &zcrypt_device_list, list) {
460 spin_lock(&zdev->ap_dev->lock);
461 requestq_count += zdev->ap_dev->requestq_count;
462 spin_unlock(&zdev->ap_dev->lock);
464 spin_unlock_bh(&zcrypt_device_lock);
465 return requestq_count;
468 static int zcrypt_count_type(int type)
470 struct zcrypt_device *zdev;
471 int device_count = 0;
473 spin_lock_bh(&zcrypt_device_lock);
474 list_for_each_entry(zdev, &zcrypt_device_list, list)
475 if (zdev->user_space_type == type)
477 spin_unlock_bh(&zcrypt_device_lock);
482 * Old, deprecated combi status call.
484 static long zcrypt_ica_status(struct file *filp, unsigned long arg)
486 struct ica_z90_status *pstat;
489 pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
492 pstat->totalcount = zcrypt_device_count;
493 pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
494 pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
495 pstat->requestqWaitCount = zcrypt_requestq_count();
496 pstat->pendingqWaitCount = zcrypt_pendingq_count();
497 pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
498 pstat->cryptoDomain = ap_domain_index;
499 zcrypt_status_mask(pstat->status);
500 zcrypt_qdepth_mask(pstat->qdepth);
502 if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
508 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
514 case ICARSAMODEXPO: {
515 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
516 struct ica_rsa_modexpo mex;
517 if (copy_from_user(&mex, umex, sizeof(mex)))
520 rc = zcrypt_rsa_modexpo(&mex);
521 } while (rc == -EAGAIN);
524 return put_user(mex.outputdatalength, &umex->outputdatalength);
527 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
528 struct ica_rsa_modexpo_crt crt;
529 if (copy_from_user(&crt, ucrt, sizeof(crt)))
532 rc = zcrypt_rsa_crt(&crt);
533 } while (rc == -EAGAIN);
536 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
538 case Z90STAT_STATUS_MASK: {
539 char status[AP_DEVICES];
540 zcrypt_status_mask(status);
541 if (copy_to_user((char __user *) arg, status,
542 sizeof(char) * AP_DEVICES))
546 case Z90STAT_QDEPTH_MASK: {
547 char qdepth[AP_DEVICES];
548 zcrypt_qdepth_mask(qdepth);
549 if (copy_to_user((char __user *) arg, qdepth,
550 sizeof(char) * AP_DEVICES))
554 case Z90STAT_PERDEV_REQCNT: {
555 int reqcnt[AP_DEVICES];
556 zcrypt_perdev_reqcnt(reqcnt);
557 if (copy_to_user((int __user *) arg, reqcnt,
558 sizeof(int) * AP_DEVICES))
562 case Z90STAT_REQUESTQ_COUNT:
563 return put_user(zcrypt_requestq_count(), (int __user *) arg);
564 case Z90STAT_PENDINGQ_COUNT:
565 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
566 case Z90STAT_TOTALOPEN_COUNT:
567 return put_user(atomic_read(&zcrypt_open_count),
569 case Z90STAT_DOMAIN_INDEX:
570 return put_user(ap_domain_index, (int __user *) arg);
572 * Deprecated ioctls. Don't add another device count ioctl,
573 * you can count them yourself in the user space with the
574 * output of the Z90STAT_STATUS_MASK ioctl.
577 return zcrypt_ica_status(filp, arg);
578 case Z90STAT_TOTALCOUNT:
579 return put_user(zcrypt_device_count, (int __user *) arg);
580 case Z90STAT_PCICACOUNT:
581 return put_user(zcrypt_count_type(ZCRYPT_PCICA),
583 case Z90STAT_PCICCCOUNT:
584 return put_user(zcrypt_count_type(ZCRYPT_PCICC),
586 case Z90STAT_PCIXCCMCL2COUNT:
587 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
589 case Z90STAT_PCIXCCMCL3COUNT:
590 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
592 case Z90STAT_PCIXCCCOUNT:
593 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
594 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
596 case Z90STAT_CEX2CCOUNT:
597 return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
599 case Z90STAT_CEX2ACOUNT:
600 return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
603 /* unknown ioctl number */
610 * ioctl32 conversion routines
612 struct compat_ica_rsa_modexpo {
613 compat_uptr_t inputdata;
614 unsigned int inputdatalength;
615 compat_uptr_t outputdata;
616 unsigned int outputdatalength;
618 compat_uptr_t n_modulus;
621 static long trans_modexpo32(struct file *filp, unsigned int cmd,
624 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
625 struct compat_ica_rsa_modexpo mex32;
626 struct ica_rsa_modexpo mex64;
629 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
631 mex64.inputdata = compat_ptr(mex32.inputdata);
632 mex64.inputdatalength = mex32.inputdatalength;
633 mex64.outputdata = compat_ptr(mex32.outputdata);
634 mex64.outputdatalength = mex32.outputdatalength;
635 mex64.b_key = compat_ptr(mex32.b_key);
636 mex64.n_modulus = compat_ptr(mex32.n_modulus);
638 rc = zcrypt_rsa_modexpo(&mex64);
639 } while (rc == -EAGAIN);
641 rc = put_user(mex64.outputdatalength,
642 &umex32->outputdatalength);
646 struct compat_ica_rsa_modexpo_crt {
647 compat_uptr_t inputdata;
648 unsigned int inputdatalength;
649 compat_uptr_t outputdata;
650 unsigned int outputdatalength;
651 compat_uptr_t bp_key;
652 compat_uptr_t bq_key;
653 compat_uptr_t np_prime;
654 compat_uptr_t nq_prime;
655 compat_uptr_t u_mult_inv;
658 static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
661 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
662 struct compat_ica_rsa_modexpo_crt crt32;
663 struct ica_rsa_modexpo_crt crt64;
666 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
668 crt64.inputdata = compat_ptr(crt32.inputdata);
669 crt64.inputdatalength = crt32.inputdatalength;
670 crt64.outputdata= compat_ptr(crt32.outputdata);
671 crt64.outputdatalength = crt32.outputdatalength;
672 crt64.bp_key = compat_ptr(crt32.bp_key);
673 crt64.bq_key = compat_ptr(crt32.bq_key);
674 crt64.np_prime = compat_ptr(crt32.np_prime);
675 crt64.nq_prime = compat_ptr(crt32.nq_prime);
676 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
678 rc = zcrypt_rsa_crt(&crt64);
679 } while (rc == -EAGAIN);
681 rc = put_user(crt64.outputdatalength,
682 &ucrt32->outputdatalength);
686 long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
689 if (cmd == ICARSAMODEXPO)
690 return trans_modexpo32(filp, cmd, arg);
691 if (cmd == ICARSACRT)
692 return trans_modexpo_crt32(filp, cmd, arg);
693 return zcrypt_unlocked_ioctl(filp, cmd, arg);
698 * Misc device file operations.
700 static struct file_operations zcrypt_fops = {
701 .owner = THIS_MODULE,
703 .write = zcrypt_write,
704 .unlocked_ioctl = zcrypt_unlocked_ioctl,
706 .compat_ioctl = zcrypt_compat_ioctl,
709 .release = zcrypt_release
715 static struct miscdevice zcrypt_misc_device = {
716 .minor = MISC_DYNAMIC_MINOR,
718 .fops = &zcrypt_fops,
722 * Deprecated /proc entry support.
724 static struct proc_dir_entry *zcrypt_entry;
726 static inline int sprintcl(unsigned char *outaddr, unsigned char *addr,
732 for (i = 0; i < len; i++)
733 hl += sprintf(outaddr+hl, "%01x", (unsigned int) addr[i]);
734 hl += sprintf(outaddr+hl, " ");
738 static inline int sprintrw(unsigned char *outaddr, unsigned char *addr,
743 hl = sprintf(outaddr, " ");
745 for (c = 0; c < (len / 16); c++) {
746 hl += sprintcl(outaddr+hl, addr+inl, 16);
751 hl += sprintcl(outaddr+hl, addr+inl, cx);
754 hl += sprintf(outaddr+hl, "\n");
758 static inline int sprinthx(unsigned char *title, unsigned char *outaddr,
759 unsigned char *addr, unsigned int len)
763 hl = sprintf(outaddr, "\n%s\n", title);
765 for (r = 0; r < (len / 64); r++) {
766 hl += sprintrw(outaddr+hl, addr+inl, 64);
771 hl += sprintrw(outaddr+hl, addr+inl, rx);
774 hl += sprintf(outaddr+hl, "\n");
778 static inline int sprinthx4(unsigned char *title, unsigned char *outaddr,
779 unsigned int *array, unsigned int len)
783 hl = sprintf(outaddr, "\n%s\n", title);
784 for (r = 0; r < len; r++) {
786 hl += sprintf(outaddr+hl, " ");
787 hl += sprintf(outaddr+hl, "%08X ", array[r]);
789 hl += sprintf(outaddr+hl, "\n");
791 hl += sprintf(outaddr+hl, "\n");
795 static int zcrypt_status_read(char *resp_buff, char **start, off_t offset,
796 int count, int *eof, void *data)
798 unsigned char *workarea;
803 /* resp_buff is a page. Use the right half for a work area */
804 workarea = resp_buff + 2000;
805 len += sprintf(resp_buff + len, "\nzcrypt version: %d.%d.%d\n",
806 ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
807 len += sprintf(resp_buff + len, "Cryptographic domain: %d\n",
809 len += sprintf(resp_buff + len, "Total device count: %d\n",
810 zcrypt_device_count);
811 len += sprintf(resp_buff + len, "PCICA count: %d\n",
812 zcrypt_count_type(ZCRYPT_PCICA));
813 len += sprintf(resp_buff + len, "PCICC count: %d\n",
814 zcrypt_count_type(ZCRYPT_PCICC));
815 len += sprintf(resp_buff + len, "PCIXCC MCL2 count: %d\n",
816 zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
817 len += sprintf(resp_buff + len, "PCIXCC MCL3 count: %d\n",
818 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
819 len += sprintf(resp_buff + len, "CEX2C count: %d\n",
820 zcrypt_count_type(ZCRYPT_CEX2C));
821 len += sprintf(resp_buff + len, "CEX2A count: %d\n",
822 zcrypt_count_type(ZCRYPT_CEX2A));
823 len += sprintf(resp_buff + len, "requestq count: %d\n",
824 zcrypt_requestq_count());
825 len += sprintf(resp_buff + len, "pendingq count: %d\n",
826 zcrypt_pendingq_count());
827 len += sprintf(resp_buff + len, "Total open handles: %d\n\n",
828 atomic_read(&zcrypt_open_count));
829 zcrypt_status_mask(workarea);
830 len += sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
831 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A",
832 resp_buff+len, workarea, AP_DEVICES);
833 zcrypt_qdepth_mask(workarea);
834 len += sprinthx("Waiting work element counts",
835 resp_buff+len, workarea, AP_DEVICES);
836 zcrypt_perdev_reqcnt((unsigned int *) workarea);
837 len += sprinthx4("Per-device successfully completed request counts",
838 resp_buff+len,(unsigned int *) workarea, AP_DEVICES);
840 memset((void *) workarea, 0x00, AP_DEVICES * sizeof(unsigned int));
844 static void zcrypt_disable_card(int index)
846 struct zcrypt_device *zdev;
848 spin_lock_bh(&zcrypt_device_lock);
849 list_for_each_entry(zdev, &zcrypt_device_list, list)
850 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
852 ap_flush_queue(zdev->ap_dev);
855 spin_unlock_bh(&zcrypt_device_lock);
858 static void zcrypt_enable_card(int index)
860 struct zcrypt_device *zdev;
862 spin_lock_bh(&zcrypt_device_lock);
863 list_for_each_entry(zdev, &zcrypt_device_list, list)
864 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
868 spin_unlock_bh(&zcrypt_device_lock);
871 static int zcrypt_status_write(struct file *file, const char __user *buffer,
872 unsigned long count, void *data)
874 unsigned char *lbuf, *ptr;
875 unsigned long local_count;
881 #define LBUFSIZE 1200UL
882 lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
884 PRINTK("kmalloc failed!\n");
888 local_count = min(LBUFSIZE - 1, count);
889 if (copy_from_user(lbuf, buffer, local_count) != 0) {
893 lbuf[local_count] = '\0';
895 ptr = strstr(lbuf, "Online devices");
897 PRINTK("Unable to parse data (missing \"Online devices\")\n");
900 ptr = strstr(ptr, "\n");
902 PRINTK("Unable to parse data (missing newline "
903 "after \"Online devices\")\n");
908 if (strstr(ptr, "Waiting work element counts") == NULL) {
909 PRINTK("Unable to parse data (missing "
910 "\"Waiting work element counts\")\n");
914 for (j = 0; j < 64 && *ptr; ptr++) {
916 * '0' for no device, '1' for PCICA, '2' for PCICC,
917 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
918 * '5' for CEX2C and '6' for CEX2A'
920 if (*ptr >= '0' && *ptr <= '6')
922 else if (*ptr == 'd' || *ptr == 'D')
923 zcrypt_disable_card(j++);
924 else if (*ptr == 'e' || *ptr == 'E')
925 zcrypt_enable_card(j++);
926 else if (*ptr != ' ' && *ptr != '\t')
935 * The module initialization code.
937 int __init zcrypt_api_init(void)
941 /* Register the request sprayer. */
942 rc = misc_register(&zcrypt_misc_device);
944 PRINTKW(KERN_ERR "misc_register (minor %d) failed with %d\n",
945 zcrypt_misc_device.minor, rc);
949 /* Set up the proc file system */
950 zcrypt_entry = create_proc_entry("driver/z90crypt", 0644, NULL);
952 PRINTK("Couldn't create z90crypt proc entry\n");
956 zcrypt_entry->nlink = 1;
957 zcrypt_entry->data = NULL;
958 zcrypt_entry->read_proc = zcrypt_status_read;
959 zcrypt_entry->write_proc = zcrypt_status_write;
964 misc_deregister(&zcrypt_misc_device);
970 * The module termination code.
972 void zcrypt_api_exit(void)
974 remove_proc_entry("driver/z90crypt", NULL);
975 misc_deregister(&zcrypt_misc_device);
978 #ifndef CONFIG_ZCRYPT_MONOLITHIC
979 module_init(zcrypt_api_init);
980 module_exit(zcrypt_api_exit);