Linux 4.2.1
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_api.c
blob01bf1f5cf2e95a7f40f722f51e4df87ea257e724
1 /*
2 * zcrypt 2.1.0
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)
17 * any later version.
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>
33 #include <linux/fs.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"
50 * Module description.
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;
104 int online;
106 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
107 return -EINVAL;
108 zdev->online = online;
109 ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dman", zdev->ap_dev->qid,
110 zdev->online);
111 if (!online)
112 ap_flush_queue(zdev->ap_dev);
113 return count;
116 static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
118 static struct attribute * zcrypt_device_attrs[] = {
119 &dev_attr_type.attr,
120 &dev_attr_online.attr,
121 NULL,
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));
141 return 1;
143 return 0;
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;
157 struct list_head *l;
159 if (zdev->speed_rating == 0)
160 return;
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)
166 break;
168 if (l == zdev->list.prev)
169 return;
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;
185 struct list_head *l;
187 if (zdev->speed_rating == 0)
188 return;
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)
194 break;
196 if (l == zdev->list.next)
197 return;
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);
226 if (!zdev)
227 return NULL;
228 zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
229 if (!zdev->reply.message)
230 goto out_free;
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;
235 return zdev;
237 out_free:
238 kfree(zdev);
239 return NULL;
241 EXPORT_SYMBOL(zcrypt_device_alloc);
243 void zcrypt_device_free(struct zcrypt_device *zdev)
245 kfree(zdev->reply.message);
246 kfree(zdev);
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)
258 int rc;
260 if (!zdev->ops)
261 return -ENODEV;
262 rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
263 &zcrypt_device_attr_group);
264 if (rc)
265 goto out;
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,
271 zdev->online);
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();
278 if (rc)
279 goto out_unregister;
281 return 0;
283 out_unregister:
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);
292 out:
293 return rc;
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)
305 if (zdev->ops->rng)
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)
320 if (zops->owner) {
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);
336 static inline
337 struct zcrypt_ops *__ops_lookup(unsigned char *name, int variant)
339 struct zcrypt_ops *zops;
340 int found = 0;
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))) {
346 found = 1;
347 break;
350 if (!found || !try_module_get(zops->owner))
351 zops = NULL;
353 spin_unlock_bh(&zcrypt_ops_list_lock);
355 return zops;
358 struct zcrypt_ops *zcrypt_msgtype_request(unsigned char *name, int variant)
360 struct zcrypt_ops *zops = NULL;
362 zops = __ops_lookup(name, variant);
363 if (!zops) {
364 request_module("%s", name);
365 zops = __ops_lookup(name, variant);
367 return zops;
369 EXPORT_SYMBOL(zcrypt_msgtype_request);
371 void zcrypt_msgtype_release(struct zcrypt_ops *zops)
373 if (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)
386 return -EPERM;
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)
397 return -EPERM;
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);
419 return 0;
423 * zcrypt ioctls.
425 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
427 struct zcrypt_device *zdev;
428 int rc;
430 if (mex->outputdatalength < mex->inputdatalength)
431 return -EINVAL;
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) {
441 if (!zdev->online ||
442 !zdev->ops->rsa_modexpo ||
443 zdev->min_mod_size > mex->inputdatalength ||
444 zdev->max_mod_size < mex->inputdatalength)
445 continue;
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);
456 else
457 rc = -EAGAIN;
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);
463 return rc;
465 spin_unlock_bh(&zcrypt_device_lock);
466 return -ENODEV;
469 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
471 struct zcrypt_device *zdev;
472 unsigned long long z1, z2, z3;
473 int rc, copied;
475 if (crt->outputdatalength < crt->inputdatalength ||
476 (crt->inputdatalength & 1))
477 return -EINVAL;
479 * As long as outputdatalength is big enough, we can set the
480 * outputdatalength equal to the inputdatalength, since that is the
481 * number of bytes we will copy in any case
483 crt->outputdatalength = crt->inputdatalength;
485 copied = 0;
486 restart:
487 spin_lock_bh(&zcrypt_device_lock);
488 list_for_each_entry(zdev, &zcrypt_device_list, list) {
489 if (!zdev->online ||
490 !zdev->ops->rsa_modexpo_crt ||
491 zdev->min_mod_size > crt->inputdatalength ||
492 zdev->max_mod_size < crt->inputdatalength)
493 continue;
494 if (zdev->short_crt && crt->inputdatalength > 240) {
496 * Check inputdata for leading zeros for cards
497 * that can't handle np_prime, bp_key, or
498 * u_mult_inv > 128 bytes.
500 if (copied == 0) {
501 unsigned int len;
502 spin_unlock_bh(&zcrypt_device_lock);
503 /* len is max 256 / 2 - 120 = 8
504 * For bigger device just assume len of leading
505 * 0s is 8 as stated in the requirements for
506 * ica_rsa_modexpo_crt struct in zcrypt.h.
508 if (crt->inputdatalength <= 256)
509 len = crt->inputdatalength / 2 - 120;
510 else
511 len = 8;
512 if (len > sizeof(z1))
513 return -EFAULT;
514 z1 = z2 = z3 = 0;
515 if (copy_from_user(&z1, crt->np_prime, len) ||
516 copy_from_user(&z2, crt->bp_key, len) ||
517 copy_from_user(&z3, crt->u_mult_inv, len))
518 return -EFAULT;
519 z1 = z2 = z3 = 0;
520 copied = 1;
522 * We have to restart device lookup -
523 * the device list may have changed by now.
525 goto restart;
527 if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
528 /* The device can't handle this request. */
529 continue;
531 zcrypt_device_get(zdev);
532 get_device(&zdev->ap_dev->device);
533 zdev->request_count++;
534 __zcrypt_decrease_preference(zdev);
535 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
536 spin_unlock_bh(&zcrypt_device_lock);
537 rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
538 spin_lock_bh(&zcrypt_device_lock);
539 module_put(zdev->ap_dev->drv->driver.owner);
541 else
542 rc = -EAGAIN;
543 zdev->request_count--;
544 __zcrypt_increase_preference(zdev);
545 put_device(&zdev->ap_dev->device);
546 zcrypt_device_put(zdev);
547 spin_unlock_bh(&zcrypt_device_lock);
548 return rc;
550 spin_unlock_bh(&zcrypt_device_lock);
551 return -ENODEV;
554 static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
556 struct zcrypt_device *zdev;
557 int rc;
559 spin_lock_bh(&zcrypt_device_lock);
560 list_for_each_entry(zdev, &zcrypt_device_list, list) {
561 if (!zdev->online || !zdev->ops->send_cprb ||
562 (zdev->ops->variant == MSGTYPE06_VARIANT_EP11) ||
563 (xcRB->user_defined != AUTOSELECT &&
564 AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined))
565 continue;
566 zcrypt_device_get(zdev);
567 get_device(&zdev->ap_dev->device);
568 zdev->request_count++;
569 __zcrypt_decrease_preference(zdev);
570 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
571 spin_unlock_bh(&zcrypt_device_lock);
572 rc = zdev->ops->send_cprb(zdev, xcRB);
573 spin_lock_bh(&zcrypt_device_lock);
574 module_put(zdev->ap_dev->drv->driver.owner);
576 else
577 rc = -EAGAIN;
578 zdev->request_count--;
579 __zcrypt_increase_preference(zdev);
580 put_device(&zdev->ap_dev->device);
581 zcrypt_device_put(zdev);
582 spin_unlock_bh(&zcrypt_device_lock);
583 return rc;
585 spin_unlock_bh(&zcrypt_device_lock);
586 return -ENODEV;
589 struct ep11_target_dev_list {
590 unsigned short targets_num;
591 struct ep11_target_dev *targets;
594 static bool is_desired_ep11dev(unsigned int dev_qid,
595 struct ep11_target_dev_list dev_list)
597 int n;
599 for (n = 0; n < dev_list.targets_num; n++, dev_list.targets++) {
600 if ((AP_QID_DEVICE(dev_qid) == dev_list.targets->ap_id) &&
601 (AP_QID_QUEUE(dev_qid) == dev_list.targets->dom_id)) {
602 return true;
605 return false;
608 static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
610 struct zcrypt_device *zdev;
611 bool autoselect = false;
612 int rc;
613 struct ep11_target_dev_list ep11_dev_list = {
614 .targets_num = 0x00,
615 .targets = NULL,
618 ep11_dev_list.targets_num = (unsigned short) xcrb->targets_num;
620 /* empty list indicates autoselect (all available targets) */
621 if (ep11_dev_list.targets_num == 0)
622 autoselect = true;
623 else {
624 ep11_dev_list.targets = kcalloc((unsigned short)
625 xcrb->targets_num,
626 sizeof(struct ep11_target_dev),
627 GFP_KERNEL);
628 if (!ep11_dev_list.targets)
629 return -ENOMEM;
631 if (copy_from_user(ep11_dev_list.targets,
632 (struct ep11_target_dev __force __user *)
633 xcrb->targets, xcrb->targets_num *
634 sizeof(struct ep11_target_dev)))
635 return -EFAULT;
638 spin_lock_bh(&zcrypt_device_lock);
639 list_for_each_entry(zdev, &zcrypt_device_list, list) {
640 /* check if device is eligible */
641 if (!zdev->online ||
642 zdev->ops->variant != MSGTYPE06_VARIANT_EP11)
643 continue;
645 /* check if device is selected as valid target */
646 if (!is_desired_ep11dev(zdev->ap_dev->qid, ep11_dev_list) &&
647 !autoselect)
648 continue;
650 zcrypt_device_get(zdev);
651 get_device(&zdev->ap_dev->device);
652 zdev->request_count++;
653 __zcrypt_decrease_preference(zdev);
654 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
655 spin_unlock_bh(&zcrypt_device_lock);
656 rc = zdev->ops->send_ep11_cprb(zdev, xcrb);
657 spin_lock_bh(&zcrypt_device_lock);
658 module_put(zdev->ap_dev->drv->driver.owner);
659 } else {
660 rc = -EAGAIN;
662 zdev->request_count--;
663 __zcrypt_increase_preference(zdev);
664 put_device(&zdev->ap_dev->device);
665 zcrypt_device_put(zdev);
666 spin_unlock_bh(&zcrypt_device_lock);
667 return rc;
669 spin_unlock_bh(&zcrypt_device_lock);
670 return -ENODEV;
673 static long zcrypt_rng(char *buffer)
675 struct zcrypt_device *zdev;
676 int rc;
678 spin_lock_bh(&zcrypt_device_lock);
679 list_for_each_entry(zdev, &zcrypt_device_list, list) {
680 if (!zdev->online || !zdev->ops->rng)
681 continue;
682 zcrypt_device_get(zdev);
683 get_device(&zdev->ap_dev->device);
684 zdev->request_count++;
685 __zcrypt_decrease_preference(zdev);
686 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
687 spin_unlock_bh(&zcrypt_device_lock);
688 rc = zdev->ops->rng(zdev, buffer);
689 spin_lock_bh(&zcrypt_device_lock);
690 module_put(zdev->ap_dev->drv->driver.owner);
691 } else
692 rc = -EAGAIN;
693 zdev->request_count--;
694 __zcrypt_increase_preference(zdev);
695 put_device(&zdev->ap_dev->device);
696 zcrypt_device_put(zdev);
697 spin_unlock_bh(&zcrypt_device_lock);
698 return rc;
700 spin_unlock_bh(&zcrypt_device_lock);
701 return -ENODEV;
704 static void zcrypt_status_mask(char status[AP_DEVICES])
706 struct zcrypt_device *zdev;
708 memset(status, 0, sizeof(char) * AP_DEVICES);
709 spin_lock_bh(&zcrypt_device_lock);
710 list_for_each_entry(zdev, &zcrypt_device_list, list)
711 status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
712 zdev->online ? zdev->user_space_type : 0x0d;
713 spin_unlock_bh(&zcrypt_device_lock);
716 static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
718 struct zcrypt_device *zdev;
720 memset(qdepth, 0, sizeof(char) * AP_DEVICES);
721 spin_lock_bh(&zcrypt_device_lock);
722 list_for_each_entry(zdev, &zcrypt_device_list, list) {
723 spin_lock(&zdev->ap_dev->lock);
724 qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
725 zdev->ap_dev->pendingq_count +
726 zdev->ap_dev->requestq_count;
727 spin_unlock(&zdev->ap_dev->lock);
729 spin_unlock_bh(&zcrypt_device_lock);
732 static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
734 struct zcrypt_device *zdev;
736 memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
737 spin_lock_bh(&zcrypt_device_lock);
738 list_for_each_entry(zdev, &zcrypt_device_list, list) {
739 spin_lock(&zdev->ap_dev->lock);
740 reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
741 zdev->ap_dev->total_request_count;
742 spin_unlock(&zdev->ap_dev->lock);
744 spin_unlock_bh(&zcrypt_device_lock);
747 static int zcrypt_pendingq_count(void)
749 struct zcrypt_device *zdev;
750 int pendingq_count = 0;
752 spin_lock_bh(&zcrypt_device_lock);
753 list_for_each_entry(zdev, &zcrypt_device_list, list) {
754 spin_lock(&zdev->ap_dev->lock);
755 pendingq_count += zdev->ap_dev->pendingq_count;
756 spin_unlock(&zdev->ap_dev->lock);
758 spin_unlock_bh(&zcrypt_device_lock);
759 return pendingq_count;
762 static int zcrypt_requestq_count(void)
764 struct zcrypt_device *zdev;
765 int requestq_count = 0;
767 spin_lock_bh(&zcrypt_device_lock);
768 list_for_each_entry(zdev, &zcrypt_device_list, list) {
769 spin_lock(&zdev->ap_dev->lock);
770 requestq_count += zdev->ap_dev->requestq_count;
771 spin_unlock(&zdev->ap_dev->lock);
773 spin_unlock_bh(&zcrypt_device_lock);
774 return requestq_count;
777 static int zcrypt_count_type(int type)
779 struct zcrypt_device *zdev;
780 int device_count = 0;
782 spin_lock_bh(&zcrypt_device_lock);
783 list_for_each_entry(zdev, &zcrypt_device_list, list)
784 if (zdev->user_space_type == type)
785 device_count++;
786 spin_unlock_bh(&zcrypt_device_lock);
787 return device_count;
791 * zcrypt_ica_status(): Old, depracted combi status call.
793 * Old, deprecated combi status call.
795 static long zcrypt_ica_status(struct file *filp, unsigned long arg)
797 struct ica_z90_status *pstat;
798 int ret;
800 pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
801 if (!pstat)
802 return -ENOMEM;
803 pstat->totalcount = zcrypt_device_count;
804 pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
805 pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
806 pstat->requestqWaitCount = zcrypt_requestq_count();
807 pstat->pendingqWaitCount = zcrypt_pendingq_count();
808 pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
809 pstat->cryptoDomain = ap_domain_index;
810 zcrypt_status_mask(pstat->status);
811 zcrypt_qdepth_mask(pstat->qdepth);
812 ret = 0;
813 if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
814 ret = -EFAULT;
815 kfree(pstat);
816 return ret;
819 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
820 unsigned long arg)
822 int rc;
824 switch (cmd) {
825 case ICARSAMODEXPO: {
826 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
827 struct ica_rsa_modexpo mex;
828 if (copy_from_user(&mex, umex, sizeof(mex)))
829 return -EFAULT;
830 do {
831 rc = zcrypt_rsa_modexpo(&mex);
832 } while (rc == -EAGAIN);
833 /* on failure: retry once again after a requested rescan */
834 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
835 do {
836 rc = zcrypt_rsa_modexpo(&mex);
837 } while (rc == -EAGAIN);
838 if (rc)
839 return rc;
840 return put_user(mex.outputdatalength, &umex->outputdatalength);
842 case ICARSACRT: {
843 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
844 struct ica_rsa_modexpo_crt crt;
845 if (copy_from_user(&crt, ucrt, sizeof(crt)))
846 return -EFAULT;
847 do {
848 rc = zcrypt_rsa_crt(&crt);
849 } while (rc == -EAGAIN);
850 /* on failure: retry once again after a requested rescan */
851 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
852 do {
853 rc = zcrypt_rsa_crt(&crt);
854 } while (rc == -EAGAIN);
855 if (rc)
856 return rc;
857 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
859 case ZSECSENDCPRB: {
860 struct ica_xcRB __user *uxcRB = (void __user *) arg;
861 struct ica_xcRB xcRB;
862 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
863 return -EFAULT;
864 do {
865 rc = zcrypt_send_cprb(&xcRB);
866 } while (rc == -EAGAIN);
867 /* on failure: retry once again after a requested rescan */
868 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
869 do {
870 rc = zcrypt_send_cprb(&xcRB);
871 } while (rc == -EAGAIN);
872 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
873 return -EFAULT;
874 return rc;
876 case ZSENDEP11CPRB: {
877 struct ep11_urb __user *uxcrb = (void __user *)arg;
878 struct ep11_urb xcrb;
879 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
880 return -EFAULT;
881 do {
882 rc = zcrypt_send_ep11_cprb(&xcrb);
883 } while (rc == -EAGAIN);
884 /* on failure: retry once again after a requested rescan */
885 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
886 do {
887 rc = zcrypt_send_ep11_cprb(&xcrb);
888 } while (rc == -EAGAIN);
889 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
890 return -EFAULT;
891 return rc;
893 case Z90STAT_STATUS_MASK: {
894 char status[AP_DEVICES];
895 zcrypt_status_mask(status);
896 if (copy_to_user((char __user *) arg, status,
897 sizeof(char) * AP_DEVICES))
898 return -EFAULT;
899 return 0;
901 case Z90STAT_QDEPTH_MASK: {
902 char qdepth[AP_DEVICES];
903 zcrypt_qdepth_mask(qdepth);
904 if (copy_to_user((char __user *) arg, qdepth,
905 sizeof(char) * AP_DEVICES))
906 return -EFAULT;
907 return 0;
909 case Z90STAT_PERDEV_REQCNT: {
910 int reqcnt[AP_DEVICES];
911 zcrypt_perdev_reqcnt(reqcnt);
912 if (copy_to_user((int __user *) arg, reqcnt,
913 sizeof(int) * AP_DEVICES))
914 return -EFAULT;
915 return 0;
917 case Z90STAT_REQUESTQ_COUNT:
918 return put_user(zcrypt_requestq_count(), (int __user *) arg);
919 case Z90STAT_PENDINGQ_COUNT:
920 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
921 case Z90STAT_TOTALOPEN_COUNT:
922 return put_user(atomic_read(&zcrypt_open_count),
923 (int __user *) arg);
924 case Z90STAT_DOMAIN_INDEX:
925 return put_user(ap_domain_index, (int __user *) arg);
927 * Deprecated ioctls. Don't add another device count ioctl,
928 * you can count them yourself in the user space with the
929 * output of the Z90STAT_STATUS_MASK ioctl.
931 case ICAZ90STATUS:
932 return zcrypt_ica_status(filp, arg);
933 case Z90STAT_TOTALCOUNT:
934 return put_user(zcrypt_device_count, (int __user *) arg);
935 case Z90STAT_PCICACOUNT:
936 return put_user(zcrypt_count_type(ZCRYPT_PCICA),
937 (int __user *) arg);
938 case Z90STAT_PCICCCOUNT:
939 return put_user(zcrypt_count_type(ZCRYPT_PCICC),
940 (int __user *) arg);
941 case Z90STAT_PCIXCCMCL2COUNT:
942 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
943 (int __user *) arg);
944 case Z90STAT_PCIXCCMCL3COUNT:
945 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
946 (int __user *) arg);
947 case Z90STAT_PCIXCCCOUNT:
948 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
949 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
950 (int __user *) arg);
951 case Z90STAT_CEX2CCOUNT:
952 return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
953 (int __user *) arg);
954 case Z90STAT_CEX2ACOUNT:
955 return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
956 (int __user *) arg);
957 default:
958 /* unknown ioctl number */
959 return -ENOIOCTLCMD;
963 #ifdef CONFIG_COMPAT
965 * ioctl32 conversion routines
967 struct compat_ica_rsa_modexpo {
968 compat_uptr_t inputdata;
969 unsigned int inputdatalength;
970 compat_uptr_t outputdata;
971 unsigned int outputdatalength;
972 compat_uptr_t b_key;
973 compat_uptr_t n_modulus;
976 static long trans_modexpo32(struct file *filp, unsigned int cmd,
977 unsigned long arg)
979 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
980 struct compat_ica_rsa_modexpo mex32;
981 struct ica_rsa_modexpo mex64;
982 long rc;
984 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
985 return -EFAULT;
986 mex64.inputdata = compat_ptr(mex32.inputdata);
987 mex64.inputdatalength = mex32.inputdatalength;
988 mex64.outputdata = compat_ptr(mex32.outputdata);
989 mex64.outputdatalength = mex32.outputdatalength;
990 mex64.b_key = compat_ptr(mex32.b_key);
991 mex64.n_modulus = compat_ptr(mex32.n_modulus);
992 do {
993 rc = zcrypt_rsa_modexpo(&mex64);
994 } while (rc == -EAGAIN);
995 /* on failure: retry once again after a requested rescan */
996 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
997 do {
998 rc = zcrypt_rsa_modexpo(&mex64);
999 } while (rc == -EAGAIN);
1000 if (rc)
1001 return rc;
1002 return put_user(mex64.outputdatalength,
1003 &umex32->outputdatalength);
1006 struct compat_ica_rsa_modexpo_crt {
1007 compat_uptr_t inputdata;
1008 unsigned int inputdatalength;
1009 compat_uptr_t outputdata;
1010 unsigned int outputdatalength;
1011 compat_uptr_t bp_key;
1012 compat_uptr_t bq_key;
1013 compat_uptr_t np_prime;
1014 compat_uptr_t nq_prime;
1015 compat_uptr_t u_mult_inv;
1018 static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
1019 unsigned long arg)
1021 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1022 struct compat_ica_rsa_modexpo_crt crt32;
1023 struct ica_rsa_modexpo_crt crt64;
1024 long rc;
1026 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1027 return -EFAULT;
1028 crt64.inputdata = compat_ptr(crt32.inputdata);
1029 crt64.inputdatalength = crt32.inputdatalength;
1030 crt64.outputdata= compat_ptr(crt32.outputdata);
1031 crt64.outputdatalength = crt32.outputdatalength;
1032 crt64.bp_key = compat_ptr(crt32.bp_key);
1033 crt64.bq_key = compat_ptr(crt32.bq_key);
1034 crt64.np_prime = compat_ptr(crt32.np_prime);
1035 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1036 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1037 do {
1038 rc = zcrypt_rsa_crt(&crt64);
1039 } while (rc == -EAGAIN);
1040 /* on failure: retry once again after a requested rescan */
1041 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1042 do {
1043 rc = zcrypt_rsa_crt(&crt64);
1044 } while (rc == -EAGAIN);
1045 if (rc)
1046 return rc;
1047 return put_user(crt64.outputdatalength,
1048 &ucrt32->outputdatalength);
1051 struct compat_ica_xcRB {
1052 unsigned short agent_ID;
1053 unsigned int user_defined;
1054 unsigned short request_ID;
1055 unsigned int request_control_blk_length;
1056 unsigned char padding1[16 - sizeof (compat_uptr_t)];
1057 compat_uptr_t request_control_blk_addr;
1058 unsigned int request_data_length;
1059 char padding2[16 - sizeof (compat_uptr_t)];
1060 compat_uptr_t request_data_address;
1061 unsigned int reply_control_blk_length;
1062 char padding3[16 - sizeof (compat_uptr_t)];
1063 compat_uptr_t reply_control_blk_addr;
1064 unsigned int reply_data_length;
1065 char padding4[16 - sizeof (compat_uptr_t)];
1066 compat_uptr_t reply_data_addr;
1067 unsigned short priority_window;
1068 unsigned int status;
1069 } __attribute__((packed));
1071 static long trans_xcRB32(struct file *filp, unsigned int cmd,
1072 unsigned long arg)
1074 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1075 struct compat_ica_xcRB xcRB32;
1076 struct ica_xcRB xcRB64;
1077 long rc;
1079 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1080 return -EFAULT;
1081 xcRB64.agent_ID = xcRB32.agent_ID;
1082 xcRB64.user_defined = xcRB32.user_defined;
1083 xcRB64.request_ID = xcRB32.request_ID;
1084 xcRB64.request_control_blk_length =
1085 xcRB32.request_control_blk_length;
1086 xcRB64.request_control_blk_addr =
1087 compat_ptr(xcRB32.request_control_blk_addr);
1088 xcRB64.request_data_length =
1089 xcRB32.request_data_length;
1090 xcRB64.request_data_address =
1091 compat_ptr(xcRB32.request_data_address);
1092 xcRB64.reply_control_blk_length =
1093 xcRB32.reply_control_blk_length;
1094 xcRB64.reply_control_blk_addr =
1095 compat_ptr(xcRB32.reply_control_blk_addr);
1096 xcRB64.reply_data_length = xcRB32.reply_data_length;
1097 xcRB64.reply_data_addr =
1098 compat_ptr(xcRB32.reply_data_addr);
1099 xcRB64.priority_window = xcRB32.priority_window;
1100 xcRB64.status = xcRB32.status;
1101 do {
1102 rc = zcrypt_send_cprb(&xcRB64);
1103 } while (rc == -EAGAIN);
1104 /* on failure: retry once again after a requested rescan */
1105 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1106 do {
1107 rc = zcrypt_send_cprb(&xcRB64);
1108 } while (rc == -EAGAIN);
1109 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1110 xcRB32.reply_data_length = xcRB64.reply_data_length;
1111 xcRB32.status = xcRB64.status;
1112 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1113 return -EFAULT;
1114 return rc;
1117 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1118 unsigned long arg)
1120 if (cmd == ICARSAMODEXPO)
1121 return trans_modexpo32(filp, cmd, arg);
1122 if (cmd == ICARSACRT)
1123 return trans_modexpo_crt32(filp, cmd, arg);
1124 if (cmd == ZSECSENDCPRB)
1125 return trans_xcRB32(filp, cmd, arg);
1126 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1128 #endif
1131 * Misc device file operations.
1133 static const struct file_operations zcrypt_fops = {
1134 .owner = THIS_MODULE,
1135 .read = zcrypt_read,
1136 .write = zcrypt_write,
1137 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1138 #ifdef CONFIG_COMPAT
1139 .compat_ioctl = zcrypt_compat_ioctl,
1140 #endif
1141 .open = zcrypt_open,
1142 .release = zcrypt_release,
1143 .llseek = no_llseek,
1147 * Misc device.
1149 static struct miscdevice zcrypt_misc_device = {
1150 .minor = MISC_DYNAMIC_MINOR,
1151 .name = "z90crypt",
1152 .fops = &zcrypt_fops,
1156 * Deprecated /proc entry support.
1158 static struct proc_dir_entry *zcrypt_entry;
1160 static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
1162 int i;
1164 for (i = 0; i < len; i++)
1165 seq_printf(m, "%01x", (unsigned int) addr[i]);
1166 seq_putc(m, ' ');
1169 static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
1171 int inl, c, cx;
1173 seq_printf(m, " ");
1174 inl = 0;
1175 for (c = 0; c < (len / 16); c++) {
1176 sprintcl(m, addr+inl, 16);
1177 inl += 16;
1179 cx = len%16;
1180 if (cx) {
1181 sprintcl(m, addr+inl, cx);
1182 inl += cx;
1184 seq_putc(m, '\n');
1187 static void sprinthx(unsigned char *title, struct seq_file *m,
1188 unsigned char *addr, unsigned int len)
1190 int inl, r, rx;
1192 seq_printf(m, "\n%s\n", title);
1193 inl = 0;
1194 for (r = 0; r < (len / 64); r++) {
1195 sprintrw(m, addr+inl, 64);
1196 inl += 64;
1198 rx = len % 64;
1199 if (rx) {
1200 sprintrw(m, addr+inl, rx);
1201 inl += rx;
1203 seq_putc(m, '\n');
1206 static void sprinthx4(unsigned char *title, struct seq_file *m,
1207 unsigned int *array, unsigned int len)
1209 int r;
1211 seq_printf(m, "\n%s\n", title);
1212 for (r = 0; r < len; r++) {
1213 if ((r % 8) == 0)
1214 seq_printf(m, " ");
1215 seq_printf(m, "%08X ", array[r]);
1216 if ((r % 8) == 7)
1217 seq_putc(m, '\n');
1219 seq_putc(m, '\n');
1222 static int zcrypt_proc_show(struct seq_file *m, void *v)
1224 char workarea[sizeof(int) * AP_DEVICES];
1226 seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
1227 ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
1228 seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
1229 seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
1230 seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
1231 seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
1232 seq_printf(m, "PCIXCC MCL2 count: %d\n",
1233 zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
1234 seq_printf(m, "PCIXCC MCL3 count: %d\n",
1235 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1236 seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
1237 seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
1238 seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
1239 seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
1240 seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
1241 seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
1242 seq_printf(m, "Total open handles: %d\n\n",
1243 atomic_read(&zcrypt_open_count));
1244 zcrypt_status_mask(workarea);
1245 sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1246 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1247 m, workarea, AP_DEVICES);
1248 zcrypt_qdepth_mask(workarea);
1249 sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
1250 zcrypt_perdev_reqcnt((int *) workarea);
1251 sprinthx4("Per-device successfully completed request counts",
1252 m, (unsigned int *) workarea, AP_DEVICES);
1253 return 0;
1256 static int zcrypt_proc_open(struct inode *inode, struct file *file)
1258 return single_open(file, zcrypt_proc_show, NULL);
1261 static void zcrypt_disable_card(int index)
1263 struct zcrypt_device *zdev;
1265 spin_lock_bh(&zcrypt_device_lock);
1266 list_for_each_entry(zdev, &zcrypt_device_list, list)
1267 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1268 zdev->online = 0;
1269 ap_flush_queue(zdev->ap_dev);
1270 break;
1272 spin_unlock_bh(&zcrypt_device_lock);
1275 static void zcrypt_enable_card(int index)
1277 struct zcrypt_device *zdev;
1279 spin_lock_bh(&zcrypt_device_lock);
1280 list_for_each_entry(zdev, &zcrypt_device_list, list)
1281 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1282 zdev->online = 1;
1283 break;
1285 spin_unlock_bh(&zcrypt_device_lock);
1288 static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
1289 size_t count, loff_t *pos)
1291 unsigned char *lbuf, *ptr;
1292 size_t local_count;
1293 int j;
1295 if (count <= 0)
1296 return 0;
1298 #define LBUFSIZE 1200UL
1299 lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1300 if (!lbuf)
1301 return 0;
1303 local_count = min(LBUFSIZE - 1, count);
1304 if (copy_from_user(lbuf, buffer, local_count) != 0) {
1305 kfree(lbuf);
1306 return -EFAULT;
1308 lbuf[local_count] = '\0';
1310 ptr = strstr(lbuf, "Online devices");
1311 if (!ptr)
1312 goto out;
1313 ptr = strstr(ptr, "\n");
1314 if (!ptr)
1315 goto out;
1316 ptr++;
1318 if (strstr(ptr, "Waiting work element counts") == NULL)
1319 goto out;
1321 for (j = 0; j < 64 && *ptr; ptr++) {
1323 * '0' for no device, '1' for PCICA, '2' for PCICC,
1324 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1325 * '5' for CEX2C and '6' for CEX2A'
1326 * '7' for CEX3C and '8' for CEX3A
1328 if (*ptr >= '0' && *ptr <= '8')
1329 j++;
1330 else if (*ptr == 'd' || *ptr == 'D')
1331 zcrypt_disable_card(j++);
1332 else if (*ptr == 'e' || *ptr == 'E')
1333 zcrypt_enable_card(j++);
1334 else if (*ptr != ' ' && *ptr != '\t')
1335 break;
1337 out:
1338 kfree(lbuf);
1339 return count;
1342 static const struct file_operations zcrypt_proc_fops = {
1343 .owner = THIS_MODULE,
1344 .open = zcrypt_proc_open,
1345 .read = seq_read,
1346 .llseek = seq_lseek,
1347 .release = single_release,
1348 .write = zcrypt_proc_write,
1351 static int zcrypt_rng_device_count;
1352 static u32 *zcrypt_rng_buffer;
1353 static int zcrypt_rng_buffer_index;
1354 static DEFINE_MUTEX(zcrypt_rng_mutex);
1356 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1358 int rc;
1361 * We don't need locking here because the RNG API guarantees serialized
1362 * read method calls.
1364 if (zcrypt_rng_buffer_index == 0) {
1365 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1366 /* on failure: retry once again after a requested rescan */
1367 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1368 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1369 if (rc < 0)
1370 return -EIO;
1371 zcrypt_rng_buffer_index = rc / sizeof *data;
1373 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1374 return sizeof *data;
1377 static struct hwrng zcrypt_rng_dev = {
1378 .name = "zcrypt",
1379 .data_read = zcrypt_rng_data_read,
1380 .quality = 990,
1383 static int zcrypt_rng_device_add(void)
1385 int rc = 0;
1387 mutex_lock(&zcrypt_rng_mutex);
1388 if (zcrypt_rng_device_count == 0) {
1389 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1390 if (!zcrypt_rng_buffer) {
1391 rc = -ENOMEM;
1392 goto out;
1394 zcrypt_rng_buffer_index = 0;
1395 if (!zcrypt_hwrng_seed)
1396 zcrypt_rng_dev.quality = 0;
1397 rc = hwrng_register(&zcrypt_rng_dev);
1398 if (rc)
1399 goto out_free;
1400 zcrypt_rng_device_count = 1;
1401 } else
1402 zcrypt_rng_device_count++;
1403 mutex_unlock(&zcrypt_rng_mutex);
1404 return 0;
1406 out_free:
1407 free_page((unsigned long) zcrypt_rng_buffer);
1408 out:
1409 mutex_unlock(&zcrypt_rng_mutex);
1410 return rc;
1413 static void zcrypt_rng_device_remove(void)
1415 mutex_lock(&zcrypt_rng_mutex);
1416 zcrypt_rng_device_count--;
1417 if (zcrypt_rng_device_count == 0) {
1418 hwrng_unregister(&zcrypt_rng_dev);
1419 free_page((unsigned long) zcrypt_rng_buffer);
1421 mutex_unlock(&zcrypt_rng_mutex);
1424 int __init zcrypt_debug_init(void)
1426 debugfs_root = debugfs_create_dir("zcrypt", NULL);
1428 zcrypt_dbf_common = debug_register("zcrypt_common", 1, 1, 16);
1429 debug_register_view(zcrypt_dbf_common, &debug_hex_ascii_view);
1430 debug_set_level(zcrypt_dbf_common, DBF_ERR);
1432 zcrypt_dbf_devices = debug_register("zcrypt_devices", 1, 1, 16);
1433 debug_register_view(zcrypt_dbf_devices, &debug_hex_ascii_view);
1434 debug_set_level(zcrypt_dbf_devices, DBF_ERR);
1436 return 0;
1439 void zcrypt_debug_exit(void)
1441 debugfs_remove(debugfs_root);
1442 if (zcrypt_dbf_common)
1443 debug_unregister(zcrypt_dbf_common);
1444 if (zcrypt_dbf_devices)
1445 debug_unregister(zcrypt_dbf_devices);
1449 * zcrypt_api_init(): Module initialization.
1451 * The module initialization code.
1453 int __init zcrypt_api_init(void)
1455 int rc;
1457 rc = zcrypt_debug_init();
1458 if (rc)
1459 goto out;
1461 atomic_set(&zcrypt_rescan_req, 0);
1463 /* Register the request sprayer. */
1464 rc = misc_register(&zcrypt_misc_device);
1465 if (rc < 0)
1466 goto out;
1468 /* Set up the proc file system */
1469 zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
1470 if (!zcrypt_entry) {
1471 rc = -ENOMEM;
1472 goto out_misc;
1475 return 0;
1477 out_misc:
1478 misc_deregister(&zcrypt_misc_device);
1479 out:
1480 return rc;
1484 * zcrypt_api_exit(): Module termination.
1486 * The module termination code.
1488 void zcrypt_api_exit(void)
1490 remove_proc_entry("driver/z90crypt", NULL);
1491 misc_deregister(&zcrypt_misc_device);
1492 zcrypt_debug_exit();
1495 module_init(zcrypt_api_init);
1496 module_exit(zcrypt_api_exit);