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 DEFINE_SPINLOCK(zcrypt_device_lock
);
58 static LIST_HEAD(zcrypt_device_list
);
59 static int zcrypt_device_count
= 0;
60 static atomic_t zcrypt_open_count
= ATOMIC_INIT(0);
61 static atomic_t zcrypt_rescan_count
= ATOMIC_INIT(0);
63 atomic_t zcrypt_rescan_req
= ATOMIC_INIT(0);
64 EXPORT_SYMBOL(zcrypt_rescan_req
);
66 static int zcrypt_rng_device_add(void);
67 static void zcrypt_rng_device_remove(void);
69 static DEFINE_SPINLOCK(zcrypt_ops_list_lock
);
70 static LIST_HEAD(zcrypt_ops_list
);
72 static debug_info_t
*zcrypt_dbf_common
;
73 static debug_info_t
*zcrypt_dbf_devices
;
74 static struct dentry
*debugfs_root
;
77 * Device attributes common for all crypto devices.
79 static ssize_t
zcrypt_type_show(struct device
*dev
,
80 struct device_attribute
*attr
, char *buf
)
82 struct zcrypt_device
*zdev
= to_ap_dev(dev
)->private;
83 return snprintf(buf
, PAGE_SIZE
, "%s\n", zdev
->type_string
);
86 static DEVICE_ATTR(type
, 0444, zcrypt_type_show
, NULL
);
88 static ssize_t
zcrypt_online_show(struct device
*dev
,
89 struct device_attribute
*attr
, char *buf
)
91 struct zcrypt_device
*zdev
= to_ap_dev(dev
)->private;
92 return snprintf(buf
, PAGE_SIZE
, "%d\n", zdev
->online
);
95 static ssize_t
zcrypt_online_store(struct device
*dev
,
96 struct device_attribute
*attr
,
97 const char *buf
, size_t count
)
99 struct zcrypt_device
*zdev
= to_ap_dev(dev
)->private;
102 if (sscanf(buf
, "%d\n", &online
) != 1 || online
< 0 || online
> 1)
104 zdev
->online
= online
;
105 ZCRYPT_DBF_DEV(DBF_INFO
, zdev
, "dev%04xo%dman", zdev
->ap_dev
->qid
,
108 ap_flush_queue(zdev
->ap_dev
);
112 static DEVICE_ATTR(online
, 0644, zcrypt_online_show
, zcrypt_online_store
);
114 static struct attribute
* zcrypt_device_attrs
[] = {
116 &dev_attr_online
.attr
,
120 static struct attribute_group zcrypt_device_attr_group
= {
121 .attrs
= zcrypt_device_attrs
,
125 * Process a rescan of the transport layer.
127 * Returns 1, if the rescan has been processed, otherwise 0.
129 static inline int zcrypt_process_rescan(void)
131 if (atomic_read(&zcrypt_rescan_req
)) {
132 atomic_set(&zcrypt_rescan_req
, 0);
133 atomic_inc(&zcrypt_rescan_count
);
134 ap_bus_force_rescan();
135 ZCRYPT_DBF_COMMON(DBF_INFO
, "rescan%07d",
136 atomic_inc_return(&zcrypt_rescan_count
));
143 * __zcrypt_increase_preference(): Increase preference of a crypto device.
144 * @zdev: Pointer the crypto device
146 * Move the device towards the head of the device list.
147 * Need to be called while holding the zcrypt device list lock.
148 * Note: cards with speed_rating of 0 are kept at the end of the list.
150 static void __zcrypt_increase_preference(struct zcrypt_device
*zdev
)
152 struct zcrypt_device
*tmp
;
155 if (zdev
->speed_rating
== 0)
157 for (l
= zdev
->list
.prev
; l
!= &zcrypt_device_list
; l
= l
->prev
) {
158 tmp
= list_entry(l
, struct zcrypt_device
, list
);
159 if ((tmp
->request_count
+ 1) * tmp
->speed_rating
<=
160 (zdev
->request_count
+ 1) * zdev
->speed_rating
&&
161 tmp
->speed_rating
!= 0)
164 if (l
== zdev
->list
.prev
)
166 /* Move zdev behind l */
167 list_move(&zdev
->list
, l
);
171 * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
172 * @zdev: Pointer to a crypto device.
174 * Move the device towards the tail of the device list.
175 * Need to be called while holding the zcrypt device list lock.
176 * Note: cards with speed_rating of 0 are kept at the end of the list.
178 static void __zcrypt_decrease_preference(struct zcrypt_device
*zdev
)
180 struct zcrypt_device
*tmp
;
183 if (zdev
->speed_rating
== 0)
185 for (l
= zdev
->list
.next
; l
!= &zcrypt_device_list
; l
= l
->next
) {
186 tmp
= list_entry(l
, struct zcrypt_device
, list
);
187 if ((tmp
->request_count
+ 1) * tmp
->speed_rating
>
188 (zdev
->request_count
+ 1) * zdev
->speed_rating
||
189 tmp
->speed_rating
== 0)
192 if (l
== zdev
->list
.next
)
194 /* Move zdev before l */
195 list_move_tail(&zdev
->list
, l
);
198 static void zcrypt_device_release(struct kref
*kref
)
200 struct zcrypt_device
*zdev
=
201 container_of(kref
, struct zcrypt_device
, refcount
);
202 zcrypt_device_free(zdev
);
205 void zcrypt_device_get(struct zcrypt_device
*zdev
)
207 kref_get(&zdev
->refcount
);
209 EXPORT_SYMBOL(zcrypt_device_get
);
211 int zcrypt_device_put(struct zcrypt_device
*zdev
)
213 return kref_put(&zdev
->refcount
, zcrypt_device_release
);
215 EXPORT_SYMBOL(zcrypt_device_put
);
217 struct zcrypt_device
*zcrypt_device_alloc(size_t max_response_size
)
219 struct zcrypt_device
*zdev
;
221 zdev
= kzalloc(sizeof(struct zcrypt_device
), GFP_KERNEL
);
224 zdev
->reply
.message
= kmalloc(max_response_size
, GFP_KERNEL
);
225 if (!zdev
->reply
.message
)
227 zdev
->reply
.length
= max_response_size
;
228 spin_lock_init(&zdev
->lock
);
229 INIT_LIST_HEAD(&zdev
->list
);
230 zdev
->dbf_area
= zcrypt_dbf_devices
;
237 EXPORT_SYMBOL(zcrypt_device_alloc
);
239 void zcrypt_device_free(struct zcrypt_device
*zdev
)
241 kfree(zdev
->reply
.message
);
244 EXPORT_SYMBOL(zcrypt_device_free
);
247 * zcrypt_device_register() - Register a crypto device.
248 * @zdev: Pointer to a crypto device
250 * Register a crypto device. Returns 0 if successful.
252 int zcrypt_device_register(struct zcrypt_device
*zdev
)
258 rc
= sysfs_create_group(&zdev
->ap_dev
->device
.kobj
,
259 &zcrypt_device_attr_group
);
262 get_device(&zdev
->ap_dev
->device
);
263 kref_init(&zdev
->refcount
);
264 spin_lock_bh(&zcrypt_device_lock
);
265 zdev
->online
= 1; /* New devices are online by default. */
266 ZCRYPT_DBF_DEV(DBF_INFO
, zdev
, "dev%04xo%dreg", zdev
->ap_dev
->qid
,
268 list_add_tail(&zdev
->list
, &zcrypt_device_list
);
269 __zcrypt_increase_preference(zdev
);
270 zcrypt_device_count
++;
271 spin_unlock_bh(&zcrypt_device_lock
);
272 if (zdev
->ops
->rng
) {
273 rc
= zcrypt_rng_device_add();
280 spin_lock_bh(&zcrypt_device_lock
);
281 zcrypt_device_count
--;
282 list_del_init(&zdev
->list
);
283 spin_unlock_bh(&zcrypt_device_lock
);
284 sysfs_remove_group(&zdev
->ap_dev
->device
.kobj
,
285 &zcrypt_device_attr_group
);
286 put_device(&zdev
->ap_dev
->device
);
287 zcrypt_device_put(zdev
);
291 EXPORT_SYMBOL(zcrypt_device_register
);
294 * zcrypt_device_unregister(): Unregister a crypto device.
295 * @zdev: Pointer to crypto device
297 * Unregister a crypto device.
299 void zcrypt_device_unregister(struct zcrypt_device
*zdev
)
302 zcrypt_rng_device_remove();
303 spin_lock_bh(&zcrypt_device_lock
);
304 zcrypt_device_count
--;
305 list_del_init(&zdev
->list
);
306 spin_unlock_bh(&zcrypt_device_lock
);
307 sysfs_remove_group(&zdev
->ap_dev
->device
.kobj
,
308 &zcrypt_device_attr_group
);
309 put_device(&zdev
->ap_dev
->device
);
310 zcrypt_device_put(zdev
);
312 EXPORT_SYMBOL(zcrypt_device_unregister
);
314 void zcrypt_msgtype_register(struct zcrypt_ops
*zops
)
317 spin_lock_bh(&zcrypt_ops_list_lock
);
318 list_add_tail(&zops
->list
, &zcrypt_ops_list
);
319 spin_unlock_bh(&zcrypt_ops_list_lock
);
322 EXPORT_SYMBOL(zcrypt_msgtype_register
);
324 void zcrypt_msgtype_unregister(struct zcrypt_ops
*zops
)
326 spin_lock_bh(&zcrypt_ops_list_lock
);
327 list_del_init(&zops
->list
);
328 spin_unlock_bh(&zcrypt_ops_list_lock
);
330 EXPORT_SYMBOL(zcrypt_msgtype_unregister
);
333 struct zcrypt_ops
*__ops_lookup(unsigned char *name
, int variant
)
335 struct zcrypt_ops
*zops
;
338 spin_lock_bh(&zcrypt_ops_list_lock
);
339 list_for_each_entry(zops
, &zcrypt_ops_list
, list
) {
340 if ((zops
->variant
== variant
) &&
341 (!strncmp(zops
->owner
->name
, name
, MODULE_NAME_LEN
))) {
346 spin_unlock_bh(&zcrypt_ops_list_lock
);
353 struct zcrypt_ops
*zcrypt_msgtype_request(unsigned char *name
, int variant
)
355 struct zcrypt_ops
*zops
= NULL
;
357 zops
= __ops_lookup(name
, variant
);
359 request_module(name
);
360 zops
= __ops_lookup(name
, variant
);
362 if ((!zops
) || (!try_module_get(zops
->owner
)))
366 EXPORT_SYMBOL(zcrypt_msgtype_request
);
368 void zcrypt_msgtype_release(struct zcrypt_ops
*zops
)
371 module_put(zops
->owner
);
373 EXPORT_SYMBOL(zcrypt_msgtype_release
);
376 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
378 * This function is not supported beyond zcrypt 1.3.1.
380 static ssize_t
zcrypt_read(struct file
*filp
, char __user
*buf
,
381 size_t count
, loff_t
*f_pos
)
387 * zcrypt_write(): Not allowed.
389 * Write is is not allowed
391 static ssize_t
zcrypt_write(struct file
*filp
, const char __user
*buf
,
392 size_t count
, loff_t
*f_pos
)
398 * zcrypt_open(): Count number of users.
400 * Device open function to count number of users.
402 static int zcrypt_open(struct inode
*inode
, struct file
*filp
)
404 atomic_inc(&zcrypt_open_count
);
405 return nonseekable_open(inode
, filp
);
409 * zcrypt_release(): Count number of users.
411 * Device close function to count number of users.
413 static int zcrypt_release(struct inode
*inode
, struct file
*filp
)
415 atomic_dec(&zcrypt_open_count
);
422 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo
*mex
)
424 struct zcrypt_device
*zdev
;
427 if (mex
->outputdatalength
< mex
->inputdatalength
)
430 * As long as outputdatalength is big enough, we can set the
431 * outputdatalength equal to the inputdatalength, since that is the
432 * number of bytes we will copy in any case
434 mex
->outputdatalength
= mex
->inputdatalength
;
436 spin_lock_bh(&zcrypt_device_lock
);
437 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
439 !zdev
->ops
->rsa_modexpo
||
440 zdev
->min_mod_size
> mex
->inputdatalength
||
441 zdev
->max_mod_size
< mex
->inputdatalength
)
443 zcrypt_device_get(zdev
);
444 get_device(&zdev
->ap_dev
->device
);
445 zdev
->request_count
++;
446 __zcrypt_decrease_preference(zdev
);
447 if (try_module_get(zdev
->ap_dev
->drv
->driver
.owner
)) {
448 spin_unlock_bh(&zcrypt_device_lock
);
449 rc
= zdev
->ops
->rsa_modexpo(zdev
, mex
);
450 spin_lock_bh(&zcrypt_device_lock
);
451 module_put(zdev
->ap_dev
->drv
->driver
.owner
);
455 zdev
->request_count
--;
456 __zcrypt_increase_preference(zdev
);
457 put_device(&zdev
->ap_dev
->device
);
458 zcrypt_device_put(zdev
);
459 spin_unlock_bh(&zcrypt_device_lock
);
462 spin_unlock_bh(&zcrypt_device_lock
);
466 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt
*crt
)
468 struct zcrypt_device
*zdev
;
469 unsigned long long z1
, z2
, z3
;
472 if (crt
->outputdatalength
< crt
->inputdatalength
||
473 (crt
->inputdatalength
& 1))
476 * As long as outputdatalength is big enough, we can set the
477 * outputdatalength equal to the inputdatalength, since that is the
478 * number of bytes we will copy in any case
480 crt
->outputdatalength
= crt
->inputdatalength
;
484 spin_lock_bh(&zcrypt_device_lock
);
485 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
487 !zdev
->ops
->rsa_modexpo_crt
||
488 zdev
->min_mod_size
> crt
->inputdatalength
||
489 zdev
->max_mod_size
< crt
->inputdatalength
)
491 if (zdev
->short_crt
&& crt
->inputdatalength
> 240) {
493 * Check inputdata for leading zeros for cards
494 * that can't handle np_prime, bp_key, or
495 * u_mult_inv > 128 bytes.
499 spin_unlock_bh(&zcrypt_device_lock
);
500 /* len is max 256 / 2 - 120 = 8
501 * For bigger device just assume len of leading
502 * 0s is 8 as stated in the requirements for
503 * ica_rsa_modexpo_crt struct in zcrypt.h.
505 if (crt
->inputdatalength
<= 256)
506 len
= crt
->inputdatalength
/ 2 - 120;
509 if (len
> sizeof(z1
))
512 if (copy_from_user(&z1
, crt
->np_prime
, len
) ||
513 copy_from_user(&z2
, crt
->bp_key
, len
) ||
514 copy_from_user(&z3
, crt
->u_mult_inv
, len
))
519 * We have to restart device lookup -
520 * the device list may have changed by now.
524 if (z1
!= 0ULL || z2
!= 0ULL || z3
!= 0ULL)
525 /* The device can't handle this request. */
528 zcrypt_device_get(zdev
);
529 get_device(&zdev
->ap_dev
->device
);
530 zdev
->request_count
++;
531 __zcrypt_decrease_preference(zdev
);
532 if (try_module_get(zdev
->ap_dev
->drv
->driver
.owner
)) {
533 spin_unlock_bh(&zcrypt_device_lock
);
534 rc
= zdev
->ops
->rsa_modexpo_crt(zdev
, crt
);
535 spin_lock_bh(&zcrypt_device_lock
);
536 module_put(zdev
->ap_dev
->drv
->driver
.owner
);
540 zdev
->request_count
--;
541 __zcrypt_increase_preference(zdev
);
542 put_device(&zdev
->ap_dev
->device
);
543 zcrypt_device_put(zdev
);
544 spin_unlock_bh(&zcrypt_device_lock
);
547 spin_unlock_bh(&zcrypt_device_lock
);
551 static long zcrypt_send_cprb(struct ica_xcRB
*xcRB
)
553 struct zcrypt_device
*zdev
;
556 spin_lock_bh(&zcrypt_device_lock
);
557 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
558 if (!zdev
->online
|| !zdev
->ops
->send_cprb
||
559 (zdev
->ops
->variant
== MSGTYPE06_VARIANT_EP11
) ||
560 (xcRB
->user_defined
!= AUTOSELECT
&&
561 AP_QID_DEVICE(zdev
->ap_dev
->qid
) != xcRB
->user_defined
))
563 zcrypt_device_get(zdev
);
564 get_device(&zdev
->ap_dev
->device
);
565 zdev
->request_count
++;
566 __zcrypt_decrease_preference(zdev
);
567 if (try_module_get(zdev
->ap_dev
->drv
->driver
.owner
)) {
568 spin_unlock_bh(&zcrypt_device_lock
);
569 rc
= zdev
->ops
->send_cprb(zdev
, xcRB
);
570 spin_lock_bh(&zcrypt_device_lock
);
571 module_put(zdev
->ap_dev
->drv
->driver
.owner
);
575 zdev
->request_count
--;
576 __zcrypt_increase_preference(zdev
);
577 put_device(&zdev
->ap_dev
->device
);
578 zcrypt_device_put(zdev
);
579 spin_unlock_bh(&zcrypt_device_lock
);
582 spin_unlock_bh(&zcrypt_device_lock
);
586 struct ep11_target_dev_list
{
587 unsigned short targets_num
;
588 struct ep11_target_dev
*targets
;
591 static bool is_desired_ep11dev(unsigned int dev_qid
,
592 struct ep11_target_dev_list dev_list
)
596 for (n
= 0; n
< dev_list
.targets_num
; n
++, dev_list
.targets
++) {
597 if ((AP_QID_DEVICE(dev_qid
) == dev_list
.targets
->ap_id
) &&
598 (AP_QID_QUEUE(dev_qid
) == dev_list
.targets
->dom_id
)) {
605 static long zcrypt_send_ep11_cprb(struct ep11_urb
*xcrb
)
607 struct zcrypt_device
*zdev
;
608 bool autoselect
= false;
610 struct ep11_target_dev_list ep11_dev_list
= {
615 ep11_dev_list
.targets_num
= (unsigned short) xcrb
->targets_num
;
617 /* empty list indicates autoselect (all available targets) */
618 if (ep11_dev_list
.targets_num
== 0)
621 ep11_dev_list
.targets
= kcalloc((unsigned short)
623 sizeof(struct ep11_target_dev
),
625 if (!ep11_dev_list
.targets
)
628 if (copy_from_user(ep11_dev_list
.targets
,
629 (struct ep11_target_dev
*)xcrb
->targets
,
631 sizeof(struct ep11_target_dev
)))
635 spin_lock_bh(&zcrypt_device_lock
);
636 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
637 /* check if device is eligible */
639 zdev
->ops
->variant
!= MSGTYPE06_VARIANT_EP11
)
642 /* check if device is selected as valid target */
643 if (!is_desired_ep11dev(zdev
->ap_dev
->qid
, ep11_dev_list
) &&
647 zcrypt_device_get(zdev
);
648 get_device(&zdev
->ap_dev
->device
);
649 zdev
->request_count
++;
650 __zcrypt_decrease_preference(zdev
);
651 if (try_module_get(zdev
->ap_dev
->drv
->driver
.owner
)) {
652 spin_unlock_bh(&zcrypt_device_lock
);
653 rc
= zdev
->ops
->send_ep11_cprb(zdev
, xcrb
);
654 spin_lock_bh(&zcrypt_device_lock
);
655 module_put(zdev
->ap_dev
->drv
->driver
.owner
);
659 zdev
->request_count
--;
660 __zcrypt_increase_preference(zdev
);
661 put_device(&zdev
->ap_dev
->device
);
662 zcrypt_device_put(zdev
);
663 spin_unlock_bh(&zcrypt_device_lock
);
666 spin_unlock_bh(&zcrypt_device_lock
);
670 static long zcrypt_rng(char *buffer
)
672 struct zcrypt_device
*zdev
;
675 spin_lock_bh(&zcrypt_device_lock
);
676 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
677 if (!zdev
->online
|| !zdev
->ops
->rng
)
679 zcrypt_device_get(zdev
);
680 get_device(&zdev
->ap_dev
->device
);
681 zdev
->request_count
++;
682 __zcrypt_decrease_preference(zdev
);
683 if (try_module_get(zdev
->ap_dev
->drv
->driver
.owner
)) {
684 spin_unlock_bh(&zcrypt_device_lock
);
685 rc
= zdev
->ops
->rng(zdev
, buffer
);
686 spin_lock_bh(&zcrypt_device_lock
);
687 module_put(zdev
->ap_dev
->drv
->driver
.owner
);
690 zdev
->request_count
--;
691 __zcrypt_increase_preference(zdev
);
692 put_device(&zdev
->ap_dev
->device
);
693 zcrypt_device_put(zdev
);
694 spin_unlock_bh(&zcrypt_device_lock
);
697 spin_unlock_bh(&zcrypt_device_lock
);
701 static void zcrypt_status_mask(char status
[AP_DEVICES
])
703 struct zcrypt_device
*zdev
;
705 memset(status
, 0, sizeof(char) * AP_DEVICES
);
706 spin_lock_bh(&zcrypt_device_lock
);
707 list_for_each_entry(zdev
, &zcrypt_device_list
, list
)
708 status
[AP_QID_DEVICE(zdev
->ap_dev
->qid
)] =
709 zdev
->online
? zdev
->user_space_type
: 0x0d;
710 spin_unlock_bh(&zcrypt_device_lock
);
713 static void zcrypt_qdepth_mask(char qdepth
[AP_DEVICES
])
715 struct zcrypt_device
*zdev
;
717 memset(qdepth
, 0, sizeof(char) * AP_DEVICES
);
718 spin_lock_bh(&zcrypt_device_lock
);
719 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
720 spin_lock(&zdev
->ap_dev
->lock
);
721 qdepth
[AP_QID_DEVICE(zdev
->ap_dev
->qid
)] =
722 zdev
->ap_dev
->pendingq_count
+
723 zdev
->ap_dev
->requestq_count
;
724 spin_unlock(&zdev
->ap_dev
->lock
);
726 spin_unlock_bh(&zcrypt_device_lock
);
729 static void zcrypt_perdev_reqcnt(int reqcnt
[AP_DEVICES
])
731 struct zcrypt_device
*zdev
;
733 memset(reqcnt
, 0, sizeof(int) * AP_DEVICES
);
734 spin_lock_bh(&zcrypt_device_lock
);
735 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
736 spin_lock(&zdev
->ap_dev
->lock
);
737 reqcnt
[AP_QID_DEVICE(zdev
->ap_dev
->qid
)] =
738 zdev
->ap_dev
->total_request_count
;
739 spin_unlock(&zdev
->ap_dev
->lock
);
741 spin_unlock_bh(&zcrypt_device_lock
);
744 static int zcrypt_pendingq_count(void)
746 struct zcrypt_device
*zdev
;
747 int pendingq_count
= 0;
749 spin_lock_bh(&zcrypt_device_lock
);
750 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
751 spin_lock(&zdev
->ap_dev
->lock
);
752 pendingq_count
+= zdev
->ap_dev
->pendingq_count
;
753 spin_unlock(&zdev
->ap_dev
->lock
);
755 spin_unlock_bh(&zcrypt_device_lock
);
756 return pendingq_count
;
759 static int zcrypt_requestq_count(void)
761 struct zcrypt_device
*zdev
;
762 int requestq_count
= 0;
764 spin_lock_bh(&zcrypt_device_lock
);
765 list_for_each_entry(zdev
, &zcrypt_device_list
, list
) {
766 spin_lock(&zdev
->ap_dev
->lock
);
767 requestq_count
+= zdev
->ap_dev
->requestq_count
;
768 spin_unlock(&zdev
->ap_dev
->lock
);
770 spin_unlock_bh(&zcrypt_device_lock
);
771 return requestq_count
;
774 static int zcrypt_count_type(int type
)
776 struct zcrypt_device
*zdev
;
777 int device_count
= 0;
779 spin_lock_bh(&zcrypt_device_lock
);
780 list_for_each_entry(zdev
, &zcrypt_device_list
, list
)
781 if (zdev
->user_space_type
== type
)
783 spin_unlock_bh(&zcrypt_device_lock
);
788 * zcrypt_ica_status(): Old, depracted combi status call.
790 * Old, deprecated combi status call.
792 static long zcrypt_ica_status(struct file
*filp
, unsigned long arg
)
794 struct ica_z90_status
*pstat
;
797 pstat
= kzalloc(sizeof(*pstat
), GFP_KERNEL
);
800 pstat
->totalcount
= zcrypt_device_count
;
801 pstat
->leedslitecount
= zcrypt_count_type(ZCRYPT_PCICA
);
802 pstat
->leeds2count
= zcrypt_count_type(ZCRYPT_PCICC
);
803 pstat
->requestqWaitCount
= zcrypt_requestq_count();
804 pstat
->pendingqWaitCount
= zcrypt_pendingq_count();
805 pstat
->totalOpenCount
= atomic_read(&zcrypt_open_count
);
806 pstat
->cryptoDomain
= ap_domain_index
;
807 zcrypt_status_mask(pstat
->status
);
808 zcrypt_qdepth_mask(pstat
->qdepth
);
810 if (copy_to_user((void __user
*) arg
, pstat
, sizeof(*pstat
)))
816 static long zcrypt_unlocked_ioctl(struct file
*filp
, unsigned int cmd
,
822 case ICARSAMODEXPO
: {
823 struct ica_rsa_modexpo __user
*umex
= (void __user
*) arg
;
824 struct ica_rsa_modexpo mex
;
825 if (copy_from_user(&mex
, umex
, sizeof(mex
)))
828 rc
= zcrypt_rsa_modexpo(&mex
);
829 } while (rc
== -EAGAIN
);
830 /* on failure: retry once again after a requested rescan */
831 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
833 rc
= zcrypt_rsa_modexpo(&mex
);
834 } while (rc
== -EAGAIN
);
837 return put_user(mex
.outputdatalength
, &umex
->outputdatalength
);
840 struct ica_rsa_modexpo_crt __user
*ucrt
= (void __user
*) arg
;
841 struct ica_rsa_modexpo_crt crt
;
842 if (copy_from_user(&crt
, ucrt
, sizeof(crt
)))
845 rc
= zcrypt_rsa_crt(&crt
);
846 } while (rc
== -EAGAIN
);
847 /* on failure: retry once again after a requested rescan */
848 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
850 rc
= zcrypt_rsa_crt(&crt
);
851 } while (rc
== -EAGAIN
);
854 return put_user(crt
.outputdatalength
, &ucrt
->outputdatalength
);
857 struct ica_xcRB __user
*uxcRB
= (void __user
*) arg
;
858 struct ica_xcRB xcRB
;
859 if (copy_from_user(&xcRB
, uxcRB
, sizeof(xcRB
)))
862 rc
= zcrypt_send_cprb(&xcRB
);
863 } while (rc
== -EAGAIN
);
864 /* on failure: retry once again after a requested rescan */
865 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
867 rc
= zcrypt_send_cprb(&xcRB
);
868 } while (rc
== -EAGAIN
);
869 if (copy_to_user(uxcRB
, &xcRB
, sizeof(xcRB
)))
873 case ZSENDEP11CPRB
: {
874 struct ep11_urb __user
*uxcrb
= (void __user
*)arg
;
875 struct ep11_urb xcrb
;
876 if (copy_from_user(&xcrb
, uxcrb
, sizeof(xcrb
)))
879 rc
= zcrypt_send_ep11_cprb(&xcrb
);
880 } while (rc
== -EAGAIN
);
881 /* on failure: retry once again after a requested rescan */
882 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
884 rc
= zcrypt_send_ep11_cprb(&xcrb
);
885 } while (rc
== -EAGAIN
);
886 if (copy_to_user(uxcrb
, &xcrb
, sizeof(xcrb
)))
890 case Z90STAT_STATUS_MASK
: {
891 char status
[AP_DEVICES
];
892 zcrypt_status_mask(status
);
893 if (copy_to_user((char __user
*) arg
, status
,
894 sizeof(char) * AP_DEVICES
))
898 case Z90STAT_QDEPTH_MASK
: {
899 char qdepth
[AP_DEVICES
];
900 zcrypt_qdepth_mask(qdepth
);
901 if (copy_to_user((char __user
*) arg
, qdepth
,
902 sizeof(char) * AP_DEVICES
))
906 case Z90STAT_PERDEV_REQCNT
: {
907 int reqcnt
[AP_DEVICES
];
908 zcrypt_perdev_reqcnt(reqcnt
);
909 if (copy_to_user((int __user
*) arg
, reqcnt
,
910 sizeof(int) * AP_DEVICES
))
914 case Z90STAT_REQUESTQ_COUNT
:
915 return put_user(zcrypt_requestq_count(), (int __user
*) arg
);
916 case Z90STAT_PENDINGQ_COUNT
:
917 return put_user(zcrypt_pendingq_count(), (int __user
*) arg
);
918 case Z90STAT_TOTALOPEN_COUNT
:
919 return put_user(atomic_read(&zcrypt_open_count
),
921 case Z90STAT_DOMAIN_INDEX
:
922 return put_user(ap_domain_index
, (int __user
*) arg
);
924 * Deprecated ioctls. Don't add another device count ioctl,
925 * you can count them yourself in the user space with the
926 * output of the Z90STAT_STATUS_MASK ioctl.
929 return zcrypt_ica_status(filp
, arg
);
930 case Z90STAT_TOTALCOUNT
:
931 return put_user(zcrypt_device_count
, (int __user
*) arg
);
932 case Z90STAT_PCICACOUNT
:
933 return put_user(zcrypt_count_type(ZCRYPT_PCICA
),
935 case Z90STAT_PCICCCOUNT
:
936 return put_user(zcrypt_count_type(ZCRYPT_PCICC
),
938 case Z90STAT_PCIXCCMCL2COUNT
:
939 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2
),
941 case Z90STAT_PCIXCCMCL3COUNT
:
942 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3
),
944 case Z90STAT_PCIXCCCOUNT
:
945 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2
) +
946 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3
),
948 case Z90STAT_CEX2CCOUNT
:
949 return put_user(zcrypt_count_type(ZCRYPT_CEX2C
),
951 case Z90STAT_CEX2ACOUNT
:
952 return put_user(zcrypt_count_type(ZCRYPT_CEX2A
),
955 /* unknown ioctl number */
962 * ioctl32 conversion routines
964 struct compat_ica_rsa_modexpo
{
965 compat_uptr_t inputdata
;
966 unsigned int inputdatalength
;
967 compat_uptr_t outputdata
;
968 unsigned int outputdatalength
;
970 compat_uptr_t n_modulus
;
973 static long trans_modexpo32(struct file
*filp
, unsigned int cmd
,
976 struct compat_ica_rsa_modexpo __user
*umex32
= compat_ptr(arg
);
977 struct compat_ica_rsa_modexpo mex32
;
978 struct ica_rsa_modexpo mex64
;
981 if (copy_from_user(&mex32
, umex32
, sizeof(mex32
)))
983 mex64
.inputdata
= compat_ptr(mex32
.inputdata
);
984 mex64
.inputdatalength
= mex32
.inputdatalength
;
985 mex64
.outputdata
= compat_ptr(mex32
.outputdata
);
986 mex64
.outputdatalength
= mex32
.outputdatalength
;
987 mex64
.b_key
= compat_ptr(mex32
.b_key
);
988 mex64
.n_modulus
= compat_ptr(mex32
.n_modulus
);
990 rc
= zcrypt_rsa_modexpo(&mex64
);
991 } while (rc
== -EAGAIN
);
992 /* on failure: retry once again after a requested rescan */
993 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
995 rc
= zcrypt_rsa_modexpo(&mex64
);
996 } while (rc
== -EAGAIN
);
999 return put_user(mex64
.outputdatalength
,
1000 &umex32
->outputdatalength
);
1003 struct compat_ica_rsa_modexpo_crt
{
1004 compat_uptr_t inputdata
;
1005 unsigned int inputdatalength
;
1006 compat_uptr_t outputdata
;
1007 unsigned int outputdatalength
;
1008 compat_uptr_t bp_key
;
1009 compat_uptr_t bq_key
;
1010 compat_uptr_t np_prime
;
1011 compat_uptr_t nq_prime
;
1012 compat_uptr_t u_mult_inv
;
1015 static long trans_modexpo_crt32(struct file
*filp
, unsigned int cmd
,
1018 struct compat_ica_rsa_modexpo_crt __user
*ucrt32
= compat_ptr(arg
);
1019 struct compat_ica_rsa_modexpo_crt crt32
;
1020 struct ica_rsa_modexpo_crt crt64
;
1023 if (copy_from_user(&crt32
, ucrt32
, sizeof(crt32
)))
1025 crt64
.inputdata
= compat_ptr(crt32
.inputdata
);
1026 crt64
.inputdatalength
= crt32
.inputdatalength
;
1027 crt64
.outputdata
= compat_ptr(crt32
.outputdata
);
1028 crt64
.outputdatalength
= crt32
.outputdatalength
;
1029 crt64
.bp_key
= compat_ptr(crt32
.bp_key
);
1030 crt64
.bq_key
= compat_ptr(crt32
.bq_key
);
1031 crt64
.np_prime
= compat_ptr(crt32
.np_prime
);
1032 crt64
.nq_prime
= compat_ptr(crt32
.nq_prime
);
1033 crt64
.u_mult_inv
= compat_ptr(crt32
.u_mult_inv
);
1035 rc
= zcrypt_rsa_crt(&crt64
);
1036 } while (rc
== -EAGAIN
);
1037 /* on failure: retry once again after a requested rescan */
1038 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
1040 rc
= zcrypt_rsa_crt(&crt64
);
1041 } while (rc
== -EAGAIN
);
1044 return put_user(crt64
.outputdatalength
,
1045 &ucrt32
->outputdatalength
);
1048 struct compat_ica_xcRB
{
1049 unsigned short agent_ID
;
1050 unsigned int user_defined
;
1051 unsigned short request_ID
;
1052 unsigned int request_control_blk_length
;
1053 unsigned char padding1
[16 - sizeof (compat_uptr_t
)];
1054 compat_uptr_t request_control_blk_addr
;
1055 unsigned int request_data_length
;
1056 char padding2
[16 - sizeof (compat_uptr_t
)];
1057 compat_uptr_t request_data_address
;
1058 unsigned int reply_control_blk_length
;
1059 char padding3
[16 - sizeof (compat_uptr_t
)];
1060 compat_uptr_t reply_control_blk_addr
;
1061 unsigned int reply_data_length
;
1062 char padding4
[16 - sizeof (compat_uptr_t
)];
1063 compat_uptr_t reply_data_addr
;
1064 unsigned short priority_window
;
1065 unsigned int status
;
1066 } __attribute__((packed
));
1068 static long trans_xcRB32(struct file
*filp
, unsigned int cmd
,
1071 struct compat_ica_xcRB __user
*uxcRB32
= compat_ptr(arg
);
1072 struct compat_ica_xcRB xcRB32
;
1073 struct ica_xcRB xcRB64
;
1076 if (copy_from_user(&xcRB32
, uxcRB32
, sizeof(xcRB32
)))
1078 xcRB64
.agent_ID
= xcRB32
.agent_ID
;
1079 xcRB64
.user_defined
= xcRB32
.user_defined
;
1080 xcRB64
.request_ID
= xcRB32
.request_ID
;
1081 xcRB64
.request_control_blk_length
=
1082 xcRB32
.request_control_blk_length
;
1083 xcRB64
.request_control_blk_addr
=
1084 compat_ptr(xcRB32
.request_control_blk_addr
);
1085 xcRB64
.request_data_length
=
1086 xcRB32
.request_data_length
;
1087 xcRB64
.request_data_address
=
1088 compat_ptr(xcRB32
.request_data_address
);
1089 xcRB64
.reply_control_blk_length
=
1090 xcRB32
.reply_control_blk_length
;
1091 xcRB64
.reply_control_blk_addr
=
1092 compat_ptr(xcRB32
.reply_control_blk_addr
);
1093 xcRB64
.reply_data_length
= xcRB32
.reply_data_length
;
1094 xcRB64
.reply_data_addr
=
1095 compat_ptr(xcRB32
.reply_data_addr
);
1096 xcRB64
.priority_window
= xcRB32
.priority_window
;
1097 xcRB64
.status
= xcRB32
.status
;
1099 rc
= zcrypt_send_cprb(&xcRB64
);
1100 } while (rc
== -EAGAIN
);
1101 /* on failure: retry once again after a requested rescan */
1102 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
1104 rc
= zcrypt_send_cprb(&xcRB64
);
1105 } while (rc
== -EAGAIN
);
1106 xcRB32
.reply_control_blk_length
= xcRB64
.reply_control_blk_length
;
1107 xcRB32
.reply_data_length
= xcRB64
.reply_data_length
;
1108 xcRB32
.status
= xcRB64
.status
;
1109 if (copy_to_user(uxcRB32
, &xcRB32
, sizeof(xcRB32
)))
1114 static long zcrypt_compat_ioctl(struct file
*filp
, unsigned int cmd
,
1117 if (cmd
== ICARSAMODEXPO
)
1118 return trans_modexpo32(filp
, cmd
, arg
);
1119 if (cmd
== ICARSACRT
)
1120 return trans_modexpo_crt32(filp
, cmd
, arg
);
1121 if (cmd
== ZSECSENDCPRB
)
1122 return trans_xcRB32(filp
, cmd
, arg
);
1123 return zcrypt_unlocked_ioctl(filp
, cmd
, arg
);
1128 * Misc device file operations.
1130 static const struct file_operations zcrypt_fops
= {
1131 .owner
= THIS_MODULE
,
1132 .read
= zcrypt_read
,
1133 .write
= zcrypt_write
,
1134 .unlocked_ioctl
= zcrypt_unlocked_ioctl
,
1135 #ifdef CONFIG_COMPAT
1136 .compat_ioctl
= zcrypt_compat_ioctl
,
1138 .open
= zcrypt_open
,
1139 .release
= zcrypt_release
,
1140 .llseek
= no_llseek
,
1146 static struct miscdevice zcrypt_misc_device
= {
1147 .minor
= MISC_DYNAMIC_MINOR
,
1149 .fops
= &zcrypt_fops
,
1153 * Deprecated /proc entry support.
1155 static struct proc_dir_entry
*zcrypt_entry
;
1157 static void sprintcl(struct seq_file
*m
, unsigned char *addr
, unsigned int len
)
1161 for (i
= 0; i
< len
; i
++)
1162 seq_printf(m
, "%01x", (unsigned int) addr
[i
]);
1166 static void sprintrw(struct seq_file
*m
, unsigned char *addr
, unsigned int len
)
1172 for (c
= 0; c
< (len
/ 16); c
++) {
1173 sprintcl(m
, addr
+inl
, 16);
1178 sprintcl(m
, addr
+inl
, cx
);
1184 static void sprinthx(unsigned char *title
, struct seq_file
*m
,
1185 unsigned char *addr
, unsigned int len
)
1189 seq_printf(m
, "\n%s\n", title
);
1191 for (r
= 0; r
< (len
/ 64); r
++) {
1192 sprintrw(m
, addr
+inl
, 64);
1197 sprintrw(m
, addr
+inl
, rx
);
1203 static void sprinthx4(unsigned char *title
, struct seq_file
*m
,
1204 unsigned int *array
, unsigned int len
)
1208 seq_printf(m
, "\n%s\n", title
);
1209 for (r
= 0; r
< len
; r
++) {
1212 seq_printf(m
, "%08X ", array
[r
]);
1219 static int zcrypt_proc_show(struct seq_file
*m
, void *v
)
1221 char workarea
[sizeof(int) * AP_DEVICES
];
1223 seq_printf(m
, "\nzcrypt version: %d.%d.%d\n",
1224 ZCRYPT_VERSION
, ZCRYPT_RELEASE
, ZCRYPT_VARIANT
);
1225 seq_printf(m
, "Cryptographic domain: %d\n", ap_domain_index
);
1226 seq_printf(m
, "Total device count: %d\n", zcrypt_device_count
);
1227 seq_printf(m
, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA
));
1228 seq_printf(m
, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC
));
1229 seq_printf(m
, "PCIXCC MCL2 count: %d\n",
1230 zcrypt_count_type(ZCRYPT_PCIXCC_MCL2
));
1231 seq_printf(m
, "PCIXCC MCL3 count: %d\n",
1232 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3
));
1233 seq_printf(m
, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C
));
1234 seq_printf(m
, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A
));
1235 seq_printf(m
, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C
));
1236 seq_printf(m
, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A
));
1237 seq_printf(m
, "requestq count: %d\n", zcrypt_requestq_count());
1238 seq_printf(m
, "pendingq count: %d\n", zcrypt_pendingq_count());
1239 seq_printf(m
, "Total open handles: %d\n\n",
1240 atomic_read(&zcrypt_open_count
));
1241 zcrypt_status_mask(workarea
);
1242 sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1243 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1244 m
, workarea
, AP_DEVICES
);
1245 zcrypt_qdepth_mask(workarea
);
1246 sprinthx("Waiting work element counts", m
, workarea
, AP_DEVICES
);
1247 zcrypt_perdev_reqcnt((int *) workarea
);
1248 sprinthx4("Per-device successfully completed request counts",
1249 m
, (unsigned int *) workarea
, AP_DEVICES
);
1253 static int zcrypt_proc_open(struct inode
*inode
, struct file
*file
)
1255 return single_open(file
, zcrypt_proc_show
, NULL
);
1258 static void zcrypt_disable_card(int index
)
1260 struct zcrypt_device
*zdev
;
1262 spin_lock_bh(&zcrypt_device_lock
);
1263 list_for_each_entry(zdev
, &zcrypt_device_list
, list
)
1264 if (AP_QID_DEVICE(zdev
->ap_dev
->qid
) == index
) {
1266 ap_flush_queue(zdev
->ap_dev
);
1269 spin_unlock_bh(&zcrypt_device_lock
);
1272 static void zcrypt_enable_card(int index
)
1274 struct zcrypt_device
*zdev
;
1276 spin_lock_bh(&zcrypt_device_lock
);
1277 list_for_each_entry(zdev
, &zcrypt_device_list
, list
)
1278 if (AP_QID_DEVICE(zdev
->ap_dev
->qid
) == index
) {
1282 spin_unlock_bh(&zcrypt_device_lock
);
1285 static ssize_t
zcrypt_proc_write(struct file
*file
, const char __user
*buffer
,
1286 size_t count
, loff_t
*pos
)
1288 unsigned char *lbuf
, *ptr
;
1295 #define LBUFSIZE 1200UL
1296 lbuf
= kmalloc(LBUFSIZE
, GFP_KERNEL
);
1300 local_count
= min(LBUFSIZE
- 1, count
);
1301 if (copy_from_user(lbuf
, buffer
, local_count
) != 0) {
1305 lbuf
[local_count
] = '\0';
1307 ptr
= strstr(lbuf
, "Online devices");
1310 ptr
= strstr(ptr
, "\n");
1315 if (strstr(ptr
, "Waiting work element counts") == NULL
)
1318 for (j
= 0; j
< 64 && *ptr
; ptr
++) {
1320 * '0' for no device, '1' for PCICA, '2' for PCICC,
1321 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1322 * '5' for CEX2C and '6' for CEX2A'
1323 * '7' for CEX3C and '8' for CEX3A
1325 if (*ptr
>= '0' && *ptr
<= '8')
1327 else if (*ptr
== 'd' || *ptr
== 'D')
1328 zcrypt_disable_card(j
++);
1329 else if (*ptr
== 'e' || *ptr
== 'E')
1330 zcrypt_enable_card(j
++);
1331 else if (*ptr
!= ' ' && *ptr
!= '\t')
1339 static const struct file_operations zcrypt_proc_fops
= {
1340 .owner
= THIS_MODULE
,
1341 .open
= zcrypt_proc_open
,
1343 .llseek
= seq_lseek
,
1344 .release
= single_release
,
1345 .write
= zcrypt_proc_write
,
1348 static int zcrypt_rng_device_count
;
1349 static u32
*zcrypt_rng_buffer
;
1350 static int zcrypt_rng_buffer_index
;
1351 static DEFINE_MUTEX(zcrypt_rng_mutex
);
1353 static int zcrypt_rng_data_read(struct hwrng
*rng
, u32
*data
)
1358 * We don't need locking here because the RNG API guarantees serialized
1359 * read method calls.
1361 if (zcrypt_rng_buffer_index
== 0) {
1362 rc
= zcrypt_rng((char *) zcrypt_rng_buffer
);
1363 /* on failure: retry once again after a requested rescan */
1364 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
1365 rc
= zcrypt_rng((char *) zcrypt_rng_buffer
);
1368 zcrypt_rng_buffer_index
= rc
/ sizeof *data
;
1370 *data
= zcrypt_rng_buffer
[--zcrypt_rng_buffer_index
];
1371 return sizeof *data
;
1374 static struct hwrng zcrypt_rng_dev
= {
1376 .data_read
= zcrypt_rng_data_read
,
1379 static int zcrypt_rng_device_add(void)
1383 mutex_lock(&zcrypt_rng_mutex
);
1384 if (zcrypt_rng_device_count
== 0) {
1385 zcrypt_rng_buffer
= (u32
*) get_zeroed_page(GFP_KERNEL
);
1386 if (!zcrypt_rng_buffer
) {
1390 zcrypt_rng_buffer_index
= 0;
1391 rc
= hwrng_register(&zcrypt_rng_dev
);
1394 zcrypt_rng_device_count
= 1;
1396 zcrypt_rng_device_count
++;
1397 mutex_unlock(&zcrypt_rng_mutex
);
1401 free_page((unsigned long) zcrypt_rng_buffer
);
1403 mutex_unlock(&zcrypt_rng_mutex
);
1407 static void zcrypt_rng_device_remove(void)
1409 mutex_lock(&zcrypt_rng_mutex
);
1410 zcrypt_rng_device_count
--;
1411 if (zcrypt_rng_device_count
== 0) {
1412 hwrng_unregister(&zcrypt_rng_dev
);
1413 free_page((unsigned long) zcrypt_rng_buffer
);
1415 mutex_unlock(&zcrypt_rng_mutex
);
1418 int __init
zcrypt_debug_init(void)
1420 debugfs_root
= debugfs_create_dir("zcrypt", NULL
);
1422 zcrypt_dbf_common
= debug_register("zcrypt_common", 1, 1, 16);
1423 debug_register_view(zcrypt_dbf_common
, &debug_hex_ascii_view
);
1424 debug_set_level(zcrypt_dbf_common
, DBF_ERR
);
1426 zcrypt_dbf_devices
= debug_register("zcrypt_devices", 1, 1, 16);
1427 debug_register_view(zcrypt_dbf_devices
, &debug_hex_ascii_view
);
1428 debug_set_level(zcrypt_dbf_devices
, DBF_ERR
);
1433 void zcrypt_debug_exit(void)
1435 debugfs_remove(debugfs_root
);
1436 if (zcrypt_dbf_common
)
1437 debug_unregister(zcrypt_dbf_common
);
1438 if (zcrypt_dbf_devices
)
1439 debug_unregister(zcrypt_dbf_devices
);
1443 * zcrypt_api_init(): Module initialization.
1445 * The module initialization code.
1447 int __init
zcrypt_api_init(void)
1451 rc
= zcrypt_debug_init();
1455 atomic_set(&zcrypt_rescan_req
, 0);
1457 /* Register the request sprayer. */
1458 rc
= misc_register(&zcrypt_misc_device
);
1462 /* Set up the proc file system */
1463 zcrypt_entry
= proc_create("driver/z90crypt", 0644, NULL
, &zcrypt_proc_fops
);
1464 if (!zcrypt_entry
) {
1472 misc_deregister(&zcrypt_misc_device
);
1478 * zcrypt_api_exit(): Module termination.
1480 * The module termination code.
1482 void zcrypt_api_exit(void)
1484 remove_proc_entry("driver/z90crypt", NULL
);
1485 misc_deregister(&zcrypt_misc_device
);
1486 zcrypt_debug_exit();
1489 module_init(zcrypt_api_init
);
1490 module_exit(zcrypt_api_exit
);