1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright IBM Corp. 2001, 2012
6 * Author(s): Robert Burroughs
7 * Eric Rossman (edrossma@us.ibm.com)
8 * Cornelia Huck <cornelia.huck@de.ibm.com>
10 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
11 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
12 * Ralph Wuerthner <rwuerthn@de.ibm.com>
13 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/miscdevice.h>
21 #include <linux/compat.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/uaccess.h>
25 #include <linux/hw_random.h>
26 #include <linux/debugfs.h>
27 #include <asm/debug.h>
29 #define CREATE_TRACE_POINTS
30 #include <asm/trace/zcrypt.h>
32 #include "zcrypt_api.h"
33 #include "zcrypt_debug.h"
35 #include "zcrypt_msgtype6.h"
36 #include "zcrypt_msgtype50.h"
41 MODULE_AUTHOR("IBM Corporation");
42 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
43 "Copyright IBM Corp. 2001, 2012");
44 MODULE_LICENSE("GPL");
47 * zcrypt tracepoint functions
49 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req
);
50 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep
);
52 static int zcrypt_hwrng_seed
= 1;
53 module_param_named(hwrng_seed
, zcrypt_hwrng_seed
, int, 0440);
54 MODULE_PARM_DESC(hwrng_seed
, "Turn on/off hwrng auto seed, default is 1 (on).");
56 DEFINE_SPINLOCK(zcrypt_list_lock
);
57 LIST_HEAD(zcrypt_card_list
);
58 int zcrypt_device_count
;
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 LIST_HEAD(zcrypt_ops_list
);
68 /* Zcrypt related debug feature stuff. */
69 debug_info_t
*zcrypt_dbf_info
;
72 * Process a rescan of the transport layer.
74 * Returns 1, if the rescan has been processed, otherwise 0.
76 static inline int zcrypt_process_rescan(void)
78 if (atomic_read(&zcrypt_rescan_req
)) {
79 atomic_set(&zcrypt_rescan_req
, 0);
80 atomic_inc(&zcrypt_rescan_count
);
81 ap_bus_force_rescan();
82 ZCRYPT_DBF(DBF_INFO
, "rescan count=%07d\n",
83 atomic_inc_return(&zcrypt_rescan_count
));
89 void zcrypt_msgtype_register(struct zcrypt_ops
*zops
)
91 list_add_tail(&zops
->list
, &zcrypt_ops_list
);
94 void zcrypt_msgtype_unregister(struct zcrypt_ops
*zops
)
96 list_del_init(&zops
->list
);
99 struct zcrypt_ops
*zcrypt_msgtype(unsigned char *name
, int variant
)
101 struct zcrypt_ops
*zops
;
103 list_for_each_entry(zops
, &zcrypt_ops_list
, list
)
104 if ((zops
->variant
== variant
) &&
105 (!strncmp(zops
->name
, name
, sizeof(zops
->name
))))
109 EXPORT_SYMBOL(zcrypt_msgtype
);
112 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
114 * This function is not supported beyond zcrypt 1.3.1.
116 static ssize_t
zcrypt_read(struct file
*filp
, char __user
*buf
,
117 size_t count
, loff_t
*f_pos
)
123 * zcrypt_write(): Not allowed.
125 * Write is is not allowed
127 static ssize_t
zcrypt_write(struct file
*filp
, const char __user
*buf
,
128 size_t count
, loff_t
*f_pos
)
134 * zcrypt_open(): Count number of users.
136 * Device open function to count number of users.
138 static int zcrypt_open(struct inode
*inode
, struct file
*filp
)
140 atomic_inc(&zcrypt_open_count
);
141 return nonseekable_open(inode
, filp
);
145 * zcrypt_release(): Count number of users.
147 * Device close function to count number of users.
149 static int zcrypt_release(struct inode
*inode
, struct file
*filp
)
151 atomic_dec(&zcrypt_open_count
);
155 static inline struct zcrypt_queue
*zcrypt_pick_queue(struct zcrypt_card
*zc
,
156 struct zcrypt_queue
*zq
,
159 if (!zq
|| !try_module_get(zq
->queue
->ap_dev
.drv
->driver
.owner
))
161 zcrypt_queue_get(zq
);
162 get_device(&zq
->queue
->ap_dev
.device
);
163 atomic_add(weight
, &zc
->load
);
164 atomic_add(weight
, &zq
->load
);
169 static inline void zcrypt_drop_queue(struct zcrypt_card
*zc
,
170 struct zcrypt_queue
*zq
,
173 struct module
*mod
= zq
->queue
->ap_dev
.drv
->driver
.owner
;
176 atomic_sub(weight
, &zc
->load
);
177 atomic_sub(weight
, &zq
->load
);
178 put_device(&zq
->queue
->ap_dev
.device
);
179 zcrypt_queue_put(zq
);
183 static inline bool zcrypt_card_compare(struct zcrypt_card
*zc
,
184 struct zcrypt_card
*pref_zc
,
186 unsigned int pref_weight
)
190 weight
+= atomic_read(&zc
->load
);
191 pref_weight
+= atomic_read(&pref_zc
->load
);
192 if (weight
== pref_weight
)
193 return atomic64_read(&zc
->card
->total_request_count
) >
194 atomic64_read(&pref_zc
->card
->total_request_count
);
195 return weight
> pref_weight
;
198 static inline bool zcrypt_queue_compare(struct zcrypt_queue
*zq
,
199 struct zcrypt_queue
*pref_zq
,
201 unsigned int pref_weight
)
205 weight
+= atomic_read(&zq
->load
);
206 pref_weight
+= atomic_read(&pref_zq
->load
);
207 if (weight
== pref_weight
)
208 return zq
->queue
->total_request_count
>
209 pref_zq
->queue
->total_request_count
;
210 return weight
> pref_weight
;
216 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo
*mex
)
218 struct zcrypt_card
*zc
, *pref_zc
;
219 struct zcrypt_queue
*zq
, *pref_zq
;
220 unsigned int weight
, pref_weight
;
221 unsigned int func_code
;
222 int qid
= 0, rc
= -ENODEV
;
224 trace_s390_zcrypt_req(mex
, TP_ICARSAMODEXPO
);
226 if (mex
->outputdatalength
< mex
->inputdatalength
) {
233 * As long as outputdatalength is big enough, we can set the
234 * outputdatalength equal to the inputdatalength, since that is the
235 * number of bytes we will copy in any case
237 mex
->outputdatalength
= mex
->inputdatalength
;
239 rc
= get_rsa_modex_fc(mex
, &func_code
);
245 spin_lock(&zcrypt_list_lock
);
246 for_each_zcrypt_card(zc
) {
247 /* Check for online accelarator and CCA cards */
248 if (!zc
->online
|| !(zc
->card
->functions
& 0x18000000))
250 /* Check for size limits */
251 if (zc
->min_mod_size
> mex
->inputdatalength
||
252 zc
->max_mod_size
< mex
->inputdatalength
)
254 /* get weight index of the card device */
255 weight
= zc
->speed_rating
[func_code
];
256 if (zcrypt_card_compare(zc
, pref_zc
, weight
, pref_weight
))
258 for_each_zcrypt_queue(zq
, zc
) {
259 /* check if device is online and eligible */
260 if (!zq
->online
|| !zq
->ops
->rsa_modexpo
)
262 if (zcrypt_queue_compare(zq
, pref_zq
,
263 weight
, pref_weight
))
267 pref_weight
= weight
;
270 pref_zq
= zcrypt_pick_queue(pref_zc
, pref_zq
, weight
);
271 spin_unlock(&zcrypt_list_lock
);
278 qid
= pref_zq
->queue
->qid
;
279 rc
= pref_zq
->ops
->rsa_modexpo(pref_zq
, mex
);
281 spin_lock(&zcrypt_list_lock
);
282 zcrypt_drop_queue(pref_zc
, pref_zq
, weight
);
283 spin_unlock(&zcrypt_list_lock
);
286 trace_s390_zcrypt_rep(mex
, func_code
, rc
,
287 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
));
291 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt
*crt
)
293 struct zcrypt_card
*zc
, *pref_zc
;
294 struct zcrypt_queue
*zq
, *pref_zq
;
295 unsigned int weight
, pref_weight
;
296 unsigned int func_code
;
297 int qid
= 0, rc
= -ENODEV
;
299 trace_s390_zcrypt_req(crt
, TP_ICARSACRT
);
301 if (crt
->outputdatalength
< crt
->inputdatalength
) {
308 * As long as outputdatalength is big enough, we can set the
309 * outputdatalength equal to the inputdatalength, since that is the
310 * number of bytes we will copy in any case
312 crt
->outputdatalength
= crt
->inputdatalength
;
314 rc
= get_rsa_crt_fc(crt
, &func_code
);
320 spin_lock(&zcrypt_list_lock
);
321 for_each_zcrypt_card(zc
) {
322 /* Check for online accelarator and CCA cards */
323 if (!zc
->online
|| !(zc
->card
->functions
& 0x18000000))
325 /* Check for size limits */
326 if (zc
->min_mod_size
> crt
->inputdatalength
||
327 zc
->max_mod_size
< crt
->inputdatalength
)
329 /* get weight index of the card device */
330 weight
= zc
->speed_rating
[func_code
];
331 if (zcrypt_card_compare(zc
, pref_zc
, weight
, pref_weight
))
333 for_each_zcrypt_queue(zq
, zc
) {
334 /* check if device is online and eligible */
335 if (!zq
->online
|| !zq
->ops
->rsa_modexpo_crt
)
337 if (zcrypt_queue_compare(zq
, pref_zq
,
338 weight
, pref_weight
))
342 pref_weight
= weight
;
345 pref_zq
= zcrypt_pick_queue(pref_zc
, pref_zq
, weight
);
346 spin_unlock(&zcrypt_list_lock
);
353 qid
= pref_zq
->queue
->qid
;
354 rc
= pref_zq
->ops
->rsa_modexpo_crt(pref_zq
, crt
);
356 spin_lock(&zcrypt_list_lock
);
357 zcrypt_drop_queue(pref_zc
, pref_zq
, weight
);
358 spin_unlock(&zcrypt_list_lock
);
361 trace_s390_zcrypt_rep(crt
, func_code
, rc
,
362 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
));
366 long zcrypt_send_cprb(struct ica_xcRB
*xcRB
)
368 struct zcrypt_card
*zc
, *pref_zc
;
369 struct zcrypt_queue
*zq
, *pref_zq
;
370 struct ap_message ap_msg
;
371 unsigned int weight
, pref_weight
;
372 unsigned int func_code
;
373 unsigned short *domain
;
374 int qid
= 0, rc
= -ENODEV
;
376 trace_s390_zcrypt_req(xcRB
, TB_ZSECSENDCPRB
);
378 ap_init_message(&ap_msg
);
379 rc
= get_cprb_fc(xcRB
, &ap_msg
, &func_code
, &domain
);
385 spin_lock(&zcrypt_list_lock
);
386 for_each_zcrypt_card(zc
) {
387 /* Check for online CCA cards */
388 if (!zc
->online
|| !(zc
->card
->functions
& 0x10000000))
390 /* Check for user selected CCA card */
391 if (xcRB
->user_defined
!= AUTOSELECT
&&
392 xcRB
->user_defined
!= zc
->card
->id
)
394 /* get weight index of the card device */
395 weight
= speed_idx_cca(func_code
) * zc
->speed_rating
[SECKEY
];
396 if (zcrypt_card_compare(zc
, pref_zc
, weight
, pref_weight
))
398 for_each_zcrypt_queue(zq
, zc
) {
399 /* check if device is online and eligible */
401 !zq
->ops
->send_cprb
||
402 ((*domain
!= (unsigned short) AUTOSELECT
) &&
403 (*domain
!= AP_QID_QUEUE(zq
->queue
->qid
))))
405 if (zcrypt_queue_compare(zq
, pref_zq
,
406 weight
, pref_weight
))
410 pref_weight
= weight
;
413 pref_zq
= zcrypt_pick_queue(pref_zc
, pref_zq
, weight
);
414 spin_unlock(&zcrypt_list_lock
);
421 /* in case of auto select, provide the correct domain */
422 qid
= pref_zq
->queue
->qid
;
423 if (*domain
== (unsigned short) AUTOSELECT
)
424 *domain
= AP_QID_QUEUE(qid
);
426 rc
= pref_zq
->ops
->send_cprb(pref_zq
, xcRB
, &ap_msg
);
428 spin_lock(&zcrypt_list_lock
);
429 zcrypt_drop_queue(pref_zc
, pref_zq
, weight
);
430 spin_unlock(&zcrypt_list_lock
);
433 ap_release_message(&ap_msg
);
434 trace_s390_zcrypt_rep(xcRB
, func_code
, rc
,
435 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
));
438 EXPORT_SYMBOL(zcrypt_send_cprb
);
440 static bool is_desired_ep11_card(unsigned int dev_id
,
441 unsigned short target_num
,
442 struct ep11_target_dev
*targets
)
444 while (target_num
-- > 0) {
445 if (dev_id
== targets
->ap_id
)
452 static bool is_desired_ep11_queue(unsigned int dev_qid
,
453 unsigned short target_num
,
454 struct ep11_target_dev
*targets
)
456 while (target_num
-- > 0) {
457 if (AP_MKQID(targets
->ap_id
, targets
->dom_id
) == dev_qid
)
464 static long zcrypt_send_ep11_cprb(struct ep11_urb
*xcrb
)
466 struct zcrypt_card
*zc
, *pref_zc
;
467 struct zcrypt_queue
*zq
, *pref_zq
;
468 struct ep11_target_dev
*targets
;
469 unsigned short target_num
;
470 unsigned int weight
, pref_weight
;
471 unsigned int func_code
;
472 struct ap_message ap_msg
;
473 int qid
= 0, rc
= -ENODEV
;
475 trace_s390_zcrypt_req(xcrb
, TP_ZSENDEP11CPRB
);
477 ap_init_message(&ap_msg
);
479 target_num
= (unsigned short) xcrb
->targets_num
;
481 /* empty list indicates autoselect (all available targets) */
483 if (target_num
!= 0) {
484 struct ep11_target_dev __user
*uptr
;
486 targets
= kcalloc(target_num
, sizeof(*targets
), GFP_KERNEL
);
493 uptr
= (struct ep11_target_dev __force __user
*) xcrb
->targets
;
494 if (copy_from_user(targets
, uptr
,
495 target_num
* sizeof(*targets
))) {
502 rc
= get_ep11cprb_fc(xcrb
, &ap_msg
, &func_code
);
508 spin_lock(&zcrypt_list_lock
);
509 for_each_zcrypt_card(zc
) {
510 /* Check for online EP11 cards */
511 if (!zc
->online
|| !(zc
->card
->functions
& 0x04000000))
513 /* Check for user selected EP11 card */
515 !is_desired_ep11_card(zc
->card
->id
, target_num
, targets
))
517 /* get weight index of the card device */
518 weight
= speed_idx_ep11(func_code
) * zc
->speed_rating
[SECKEY
];
519 if (zcrypt_card_compare(zc
, pref_zc
, weight
, pref_weight
))
521 for_each_zcrypt_queue(zq
, zc
) {
522 /* check if device is online and eligible */
524 !zq
->ops
->send_ep11_cprb
||
526 !is_desired_ep11_queue(zq
->queue
->qid
,
527 target_num
, targets
)))
529 if (zcrypt_queue_compare(zq
, pref_zq
,
530 weight
, pref_weight
))
534 pref_weight
= weight
;
537 pref_zq
= zcrypt_pick_queue(pref_zc
, pref_zq
, weight
);
538 spin_unlock(&zcrypt_list_lock
);
545 qid
= pref_zq
->queue
->qid
;
546 rc
= pref_zq
->ops
->send_ep11_cprb(pref_zq
, xcrb
, &ap_msg
);
548 spin_lock(&zcrypt_list_lock
);
549 zcrypt_drop_queue(pref_zc
, pref_zq
, weight
);
550 spin_unlock(&zcrypt_list_lock
);
555 ap_release_message(&ap_msg
);
556 trace_s390_zcrypt_rep(xcrb
, func_code
, rc
,
557 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
));
561 static long zcrypt_rng(char *buffer
)
563 struct zcrypt_card
*zc
, *pref_zc
;
564 struct zcrypt_queue
*zq
, *pref_zq
;
565 unsigned int weight
, pref_weight
;
566 unsigned int func_code
;
567 struct ap_message ap_msg
;
569 int qid
= 0, rc
= -ENODEV
;
571 trace_s390_zcrypt_req(buffer
, TP_HWRNGCPRB
);
573 ap_init_message(&ap_msg
);
574 rc
= get_rng_fc(&ap_msg
, &func_code
, &domain
);
580 spin_lock(&zcrypt_list_lock
);
581 for_each_zcrypt_card(zc
) {
582 /* Check for online CCA cards */
583 if (!zc
->online
|| !(zc
->card
->functions
& 0x10000000))
585 /* get weight index of the card device */
586 weight
= zc
->speed_rating
[func_code
];
587 if (zcrypt_card_compare(zc
, pref_zc
, weight
, pref_weight
))
589 for_each_zcrypt_queue(zq
, zc
) {
590 /* check if device is online and eligible */
591 if (!zq
->online
|| !zq
->ops
->rng
)
593 if (zcrypt_queue_compare(zq
, pref_zq
,
594 weight
, pref_weight
))
598 pref_weight
= weight
;
601 pref_zq
= zcrypt_pick_queue(pref_zc
, pref_zq
, weight
);
602 spin_unlock(&zcrypt_list_lock
);
609 qid
= pref_zq
->queue
->qid
;
610 rc
= pref_zq
->ops
->rng(pref_zq
, buffer
, &ap_msg
);
612 spin_lock(&zcrypt_list_lock
);
613 zcrypt_drop_queue(pref_zc
, pref_zq
, weight
);
614 spin_unlock(&zcrypt_list_lock
);
617 ap_release_message(&ap_msg
);
618 trace_s390_zcrypt_rep(buffer
, func_code
, rc
,
619 AP_QID_CARD(qid
), AP_QID_QUEUE(qid
));
623 static void zcrypt_device_status_mask(struct zcrypt_device_status
*devstatus
)
625 struct zcrypt_card
*zc
;
626 struct zcrypt_queue
*zq
;
627 struct zcrypt_device_status
*stat
;
630 memset(devstatus
, 0, MAX_ZDEV_ENTRIES
631 * sizeof(struct zcrypt_device_status
));
633 spin_lock(&zcrypt_list_lock
);
634 for_each_zcrypt_card(zc
) {
635 for_each_zcrypt_queue(zq
, zc
) {
636 card
= AP_QID_CARD(zq
->queue
->qid
);
637 if (card
>= MAX_ZDEV_CARDIDS
)
639 queue
= AP_QID_QUEUE(zq
->queue
->qid
);
640 stat
= &devstatus
[card
* AP_DOMAINS
+ queue
];
641 stat
->hwtype
= zc
->card
->ap_dev
.device_type
;
642 stat
->functions
= zc
->card
->functions
>> 26;
643 stat
->qid
= zq
->queue
->qid
;
644 stat
->online
= zq
->online
? 0x01 : 0x00;
647 spin_unlock(&zcrypt_list_lock
);
650 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext
*devstatus
)
652 struct zcrypt_card
*zc
;
653 struct zcrypt_queue
*zq
;
654 struct zcrypt_device_status_ext
*stat
;
657 memset(devstatus
, 0, MAX_ZDEV_ENTRIES_EXT
658 * sizeof(struct zcrypt_device_status_ext
));
660 spin_lock(&zcrypt_list_lock
);
661 for_each_zcrypt_card(zc
) {
662 for_each_zcrypt_queue(zq
, zc
) {
663 card
= AP_QID_CARD(zq
->queue
->qid
);
664 queue
= AP_QID_QUEUE(zq
->queue
->qid
);
665 stat
= &devstatus
[card
* AP_DOMAINS
+ queue
];
666 stat
->hwtype
= zc
->card
->ap_dev
.device_type
;
667 stat
->functions
= zc
->card
->functions
>> 26;
668 stat
->qid
= zq
->queue
->qid
;
669 stat
->online
= zq
->online
? 0x01 : 0x00;
672 spin_unlock(&zcrypt_list_lock
);
674 EXPORT_SYMBOL(zcrypt_device_status_mask_ext
);
676 static void zcrypt_status_mask(char status
[], size_t max_adapters
)
678 struct zcrypt_card
*zc
;
679 struct zcrypt_queue
*zq
;
682 memset(status
, 0, max_adapters
);
683 spin_lock(&zcrypt_list_lock
);
684 for_each_zcrypt_card(zc
) {
685 for_each_zcrypt_queue(zq
, zc
) {
686 card
= AP_QID_CARD(zq
->queue
->qid
);
687 if (AP_QID_QUEUE(zq
->queue
->qid
) != ap_domain_index
688 || card
>= max_adapters
)
690 status
[card
] = zc
->online
? zc
->user_space_type
: 0x0d;
693 spin_unlock(&zcrypt_list_lock
);
696 static void zcrypt_qdepth_mask(char qdepth
[], size_t max_adapters
)
698 struct zcrypt_card
*zc
;
699 struct zcrypt_queue
*zq
;
702 memset(qdepth
, 0, max_adapters
);
703 spin_lock(&zcrypt_list_lock
);
705 for_each_zcrypt_card(zc
) {
706 for_each_zcrypt_queue(zq
, zc
) {
707 card
= AP_QID_CARD(zq
->queue
->qid
);
708 if (AP_QID_QUEUE(zq
->queue
->qid
) != ap_domain_index
709 || card
>= max_adapters
)
711 spin_lock(&zq
->queue
->lock
);
713 zq
->queue
->pendingq_count
+
714 zq
->queue
->requestq_count
;
715 spin_unlock(&zq
->queue
->lock
);
719 spin_unlock(&zcrypt_list_lock
);
722 static void zcrypt_perdev_reqcnt(u32 reqcnt
[], size_t max_adapters
)
724 struct zcrypt_card
*zc
;
725 struct zcrypt_queue
*zq
;
729 memset(reqcnt
, 0, sizeof(int) * max_adapters
);
730 spin_lock(&zcrypt_list_lock
);
732 for_each_zcrypt_card(zc
) {
733 for_each_zcrypt_queue(zq
, zc
) {
734 card
= AP_QID_CARD(zq
->queue
->qid
);
735 if (AP_QID_QUEUE(zq
->queue
->qid
) != ap_domain_index
736 || card
>= max_adapters
)
738 spin_lock(&zq
->queue
->lock
);
739 cnt
= zq
->queue
->total_request_count
;
740 spin_unlock(&zq
->queue
->lock
);
741 reqcnt
[card
] = (cnt
< UINT_MAX
) ? (u32
) cnt
: UINT_MAX
;
745 spin_unlock(&zcrypt_list_lock
);
748 static int zcrypt_pendingq_count(void)
750 struct zcrypt_card
*zc
;
751 struct zcrypt_queue
*zq
;
755 spin_lock(&zcrypt_list_lock
);
757 for_each_zcrypt_card(zc
) {
758 for_each_zcrypt_queue(zq
, zc
) {
759 if (AP_QID_QUEUE(zq
->queue
->qid
) != ap_domain_index
)
761 spin_lock(&zq
->queue
->lock
);
762 pendingq_count
+= zq
->queue
->pendingq_count
;
763 spin_unlock(&zq
->queue
->lock
);
767 spin_unlock(&zcrypt_list_lock
);
768 return pendingq_count
;
771 static int zcrypt_requestq_count(void)
773 struct zcrypt_card
*zc
;
774 struct zcrypt_queue
*zq
;
778 spin_lock(&zcrypt_list_lock
);
780 for_each_zcrypt_card(zc
) {
781 for_each_zcrypt_queue(zq
, zc
) {
782 if (AP_QID_QUEUE(zq
->queue
->qid
) != ap_domain_index
)
784 spin_lock(&zq
->queue
->lock
);
785 requestq_count
+= zq
->queue
->requestq_count
;
786 spin_unlock(&zq
->queue
->lock
);
790 spin_unlock(&zcrypt_list_lock
);
791 return requestq_count
;
794 static long zcrypt_unlocked_ioctl(struct file
*filp
, unsigned int cmd
,
800 case ICARSAMODEXPO
: {
801 struct ica_rsa_modexpo __user
*umex
= (void __user
*) arg
;
802 struct ica_rsa_modexpo mex
;
804 if (copy_from_user(&mex
, umex
, sizeof(mex
)))
807 rc
= zcrypt_rsa_modexpo(&mex
);
808 } while (rc
== -EAGAIN
);
809 /* on failure: retry once again after a requested rescan */
810 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
812 rc
= zcrypt_rsa_modexpo(&mex
);
813 } while (rc
== -EAGAIN
);
815 ZCRYPT_DBF(DBF_DEBUG
, "ioctl ICARSAMODEXPO rc=%d\n", rc
);
818 return put_user(mex
.outputdatalength
, &umex
->outputdatalength
);
821 struct ica_rsa_modexpo_crt __user
*ucrt
= (void __user
*) arg
;
822 struct ica_rsa_modexpo_crt crt
;
824 if (copy_from_user(&crt
, ucrt
, sizeof(crt
)))
827 rc
= zcrypt_rsa_crt(&crt
);
828 } while (rc
== -EAGAIN
);
829 /* on failure: retry once again after a requested rescan */
830 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
832 rc
= zcrypt_rsa_crt(&crt
);
833 } while (rc
== -EAGAIN
);
835 ZCRYPT_DBF(DBF_DEBUG
, "ioctl ICARSACRT rc=%d\n", rc
);
838 return put_user(crt
.outputdatalength
, &ucrt
->outputdatalength
);
841 struct ica_xcRB __user
*uxcRB
= (void __user
*) arg
;
842 struct ica_xcRB xcRB
;
844 if (copy_from_user(&xcRB
, uxcRB
, sizeof(xcRB
)))
847 rc
= zcrypt_send_cprb(&xcRB
);
848 } while (rc
== -EAGAIN
);
849 /* on failure: retry once again after a requested rescan */
850 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
852 rc
= zcrypt_send_cprb(&xcRB
);
853 } while (rc
== -EAGAIN
);
855 ZCRYPT_DBF(DBF_DEBUG
, "ioctl ZSENDCPRB rc=%d\n", rc
);
856 if (copy_to_user(uxcRB
, &xcRB
, sizeof(xcRB
)))
860 case ZSENDEP11CPRB
: {
861 struct ep11_urb __user
*uxcrb
= (void __user
*)arg
;
862 struct ep11_urb xcrb
;
864 if (copy_from_user(&xcrb
, uxcrb
, sizeof(xcrb
)))
867 rc
= zcrypt_send_ep11_cprb(&xcrb
);
868 } while (rc
== -EAGAIN
);
869 /* on failure: retry once again after a requested rescan */
870 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
872 rc
= zcrypt_send_ep11_cprb(&xcrb
);
873 } while (rc
== -EAGAIN
);
875 ZCRYPT_DBF(DBF_DEBUG
, "ioctl ZSENDEP11CPRB rc=%d\n", rc
);
876 if (copy_to_user(uxcrb
, &xcrb
, sizeof(xcrb
)))
880 case ZCRYPT_DEVICE_STATUS
: {
881 struct zcrypt_device_status_ext
*device_status
;
882 size_t total_size
= MAX_ZDEV_ENTRIES_EXT
883 * sizeof(struct zcrypt_device_status_ext
);
885 device_status
= kzalloc(total_size
, GFP_KERNEL
);
888 zcrypt_device_status_mask_ext(device_status
);
889 if (copy_to_user((char __user
*) arg
, device_status
,
892 kfree(device_status
);
895 case ZCRYPT_STATUS_MASK
: {
896 char status
[AP_DEVICES
];
898 zcrypt_status_mask(status
, AP_DEVICES
);
899 if (copy_to_user((char __user
*) arg
, status
, sizeof(status
)))
903 case ZCRYPT_QDEPTH_MASK
: {
904 char qdepth
[AP_DEVICES
];
906 zcrypt_qdepth_mask(qdepth
, AP_DEVICES
);
907 if (copy_to_user((char __user
*) arg
, qdepth
, sizeof(qdepth
)))
911 case ZCRYPT_PERDEV_REQCNT
: {
914 reqcnt
= kcalloc(AP_DEVICES
, sizeof(u32
), GFP_KERNEL
);
917 zcrypt_perdev_reqcnt(reqcnt
, AP_DEVICES
);
918 if (copy_to_user((int __user
*) arg
, reqcnt
, sizeof(reqcnt
)))
923 case Z90STAT_REQUESTQ_COUNT
:
924 return put_user(zcrypt_requestq_count(), (int __user
*) arg
);
925 case Z90STAT_PENDINGQ_COUNT
:
926 return put_user(zcrypt_pendingq_count(), (int __user
*) arg
);
927 case Z90STAT_TOTALOPEN_COUNT
:
928 return put_user(atomic_read(&zcrypt_open_count
),
930 case Z90STAT_DOMAIN_INDEX
:
931 return put_user(ap_domain_index
, (int __user
*) arg
);
935 case ZDEVICESTATUS
: {
936 /* the old ioctl supports only 64 adapters */
937 struct zcrypt_device_status
*device_status
;
938 size_t total_size
= MAX_ZDEV_ENTRIES
939 * sizeof(struct zcrypt_device_status
);
941 device_status
= kzalloc(total_size
, GFP_KERNEL
);
944 zcrypt_device_status_mask(device_status
);
945 if (copy_to_user((char __user
*) arg
, device_status
,
948 kfree(device_status
);
951 case Z90STAT_STATUS_MASK
: {
952 /* the old ioctl supports only 64 adapters */
953 char status
[MAX_ZDEV_CARDIDS
];
955 zcrypt_status_mask(status
, MAX_ZDEV_CARDIDS
);
956 if (copy_to_user((char __user
*) arg
, status
, sizeof(status
)))
960 case Z90STAT_QDEPTH_MASK
: {
961 /* the old ioctl supports only 64 adapters */
962 char qdepth
[MAX_ZDEV_CARDIDS
];
964 zcrypt_qdepth_mask(qdepth
, MAX_ZDEV_CARDIDS
);
965 if (copy_to_user((char __user
*) arg
, qdepth
, sizeof(qdepth
)))
969 case Z90STAT_PERDEV_REQCNT
: {
970 /* the old ioctl supports only 64 adapters */
971 u32 reqcnt
[MAX_ZDEV_CARDIDS
];
973 zcrypt_perdev_reqcnt(reqcnt
, MAX_ZDEV_CARDIDS
);
974 if (copy_to_user((int __user
*) arg
, reqcnt
, sizeof(reqcnt
)))
978 /* unknown ioctl number */
980 ZCRYPT_DBF(DBF_DEBUG
, "unknown ioctl 0x%08x\n", cmd
);
987 * ioctl32 conversion routines
989 struct compat_ica_rsa_modexpo
{
990 compat_uptr_t inputdata
;
991 unsigned int inputdatalength
;
992 compat_uptr_t outputdata
;
993 unsigned int outputdatalength
;
995 compat_uptr_t n_modulus
;
998 static long trans_modexpo32(struct file
*filp
, unsigned int cmd
,
1001 struct compat_ica_rsa_modexpo __user
*umex32
= compat_ptr(arg
);
1002 struct compat_ica_rsa_modexpo mex32
;
1003 struct ica_rsa_modexpo mex64
;
1006 if (copy_from_user(&mex32
, umex32
, sizeof(mex32
)))
1008 mex64
.inputdata
= compat_ptr(mex32
.inputdata
);
1009 mex64
.inputdatalength
= mex32
.inputdatalength
;
1010 mex64
.outputdata
= compat_ptr(mex32
.outputdata
);
1011 mex64
.outputdatalength
= mex32
.outputdatalength
;
1012 mex64
.b_key
= compat_ptr(mex32
.b_key
);
1013 mex64
.n_modulus
= compat_ptr(mex32
.n_modulus
);
1015 rc
= zcrypt_rsa_modexpo(&mex64
);
1016 } while (rc
== -EAGAIN
);
1017 /* on failure: retry once again after a requested rescan */
1018 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
1020 rc
= zcrypt_rsa_modexpo(&mex64
);
1021 } while (rc
== -EAGAIN
);
1024 return put_user(mex64
.outputdatalength
,
1025 &umex32
->outputdatalength
);
1028 struct compat_ica_rsa_modexpo_crt
{
1029 compat_uptr_t inputdata
;
1030 unsigned int inputdatalength
;
1031 compat_uptr_t outputdata
;
1032 unsigned int outputdatalength
;
1033 compat_uptr_t bp_key
;
1034 compat_uptr_t bq_key
;
1035 compat_uptr_t np_prime
;
1036 compat_uptr_t nq_prime
;
1037 compat_uptr_t u_mult_inv
;
1040 static long trans_modexpo_crt32(struct file
*filp
, unsigned int cmd
,
1043 struct compat_ica_rsa_modexpo_crt __user
*ucrt32
= compat_ptr(arg
);
1044 struct compat_ica_rsa_modexpo_crt crt32
;
1045 struct ica_rsa_modexpo_crt crt64
;
1048 if (copy_from_user(&crt32
, ucrt32
, sizeof(crt32
)))
1050 crt64
.inputdata
= compat_ptr(crt32
.inputdata
);
1051 crt64
.inputdatalength
= crt32
.inputdatalength
;
1052 crt64
.outputdata
= compat_ptr(crt32
.outputdata
);
1053 crt64
.outputdatalength
= crt32
.outputdatalength
;
1054 crt64
.bp_key
= compat_ptr(crt32
.bp_key
);
1055 crt64
.bq_key
= compat_ptr(crt32
.bq_key
);
1056 crt64
.np_prime
= compat_ptr(crt32
.np_prime
);
1057 crt64
.nq_prime
= compat_ptr(crt32
.nq_prime
);
1058 crt64
.u_mult_inv
= compat_ptr(crt32
.u_mult_inv
);
1060 rc
= zcrypt_rsa_crt(&crt64
);
1061 } while (rc
== -EAGAIN
);
1062 /* on failure: retry once again after a requested rescan */
1063 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
1065 rc
= zcrypt_rsa_crt(&crt64
);
1066 } while (rc
== -EAGAIN
);
1069 return put_user(crt64
.outputdatalength
,
1070 &ucrt32
->outputdatalength
);
1073 struct compat_ica_xcRB
{
1074 unsigned short agent_ID
;
1075 unsigned int user_defined
;
1076 unsigned short request_ID
;
1077 unsigned int request_control_blk_length
;
1078 unsigned char padding1
[16 - sizeof(compat_uptr_t
)];
1079 compat_uptr_t request_control_blk_addr
;
1080 unsigned int request_data_length
;
1081 char padding2
[16 - sizeof(compat_uptr_t
)];
1082 compat_uptr_t request_data_address
;
1083 unsigned int reply_control_blk_length
;
1084 char padding3
[16 - sizeof(compat_uptr_t
)];
1085 compat_uptr_t reply_control_blk_addr
;
1086 unsigned int reply_data_length
;
1087 char padding4
[16 - sizeof(compat_uptr_t
)];
1088 compat_uptr_t reply_data_addr
;
1089 unsigned short priority_window
;
1090 unsigned int status
;
1093 static long trans_xcRB32(struct file
*filp
, unsigned int cmd
,
1096 struct compat_ica_xcRB __user
*uxcRB32
= compat_ptr(arg
);
1097 struct compat_ica_xcRB xcRB32
;
1098 struct ica_xcRB xcRB64
;
1101 if (copy_from_user(&xcRB32
, uxcRB32
, sizeof(xcRB32
)))
1103 xcRB64
.agent_ID
= xcRB32
.agent_ID
;
1104 xcRB64
.user_defined
= xcRB32
.user_defined
;
1105 xcRB64
.request_ID
= xcRB32
.request_ID
;
1106 xcRB64
.request_control_blk_length
=
1107 xcRB32
.request_control_blk_length
;
1108 xcRB64
.request_control_blk_addr
=
1109 compat_ptr(xcRB32
.request_control_blk_addr
);
1110 xcRB64
.request_data_length
=
1111 xcRB32
.request_data_length
;
1112 xcRB64
.request_data_address
=
1113 compat_ptr(xcRB32
.request_data_address
);
1114 xcRB64
.reply_control_blk_length
=
1115 xcRB32
.reply_control_blk_length
;
1116 xcRB64
.reply_control_blk_addr
=
1117 compat_ptr(xcRB32
.reply_control_blk_addr
);
1118 xcRB64
.reply_data_length
= xcRB32
.reply_data_length
;
1119 xcRB64
.reply_data_addr
=
1120 compat_ptr(xcRB32
.reply_data_addr
);
1121 xcRB64
.priority_window
= xcRB32
.priority_window
;
1122 xcRB64
.status
= xcRB32
.status
;
1124 rc
= zcrypt_send_cprb(&xcRB64
);
1125 } while (rc
== -EAGAIN
);
1126 /* on failure: retry once again after a requested rescan */
1127 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
1129 rc
= zcrypt_send_cprb(&xcRB64
);
1130 } while (rc
== -EAGAIN
);
1131 xcRB32
.reply_control_blk_length
= xcRB64
.reply_control_blk_length
;
1132 xcRB32
.reply_data_length
= xcRB64
.reply_data_length
;
1133 xcRB32
.status
= xcRB64
.status
;
1134 if (copy_to_user(uxcRB32
, &xcRB32
, sizeof(xcRB32
)))
1139 static long zcrypt_compat_ioctl(struct file
*filp
, unsigned int cmd
,
1142 if (cmd
== ICARSAMODEXPO
)
1143 return trans_modexpo32(filp
, cmd
, arg
);
1144 if (cmd
== ICARSACRT
)
1145 return trans_modexpo_crt32(filp
, cmd
, arg
);
1146 if (cmd
== ZSECSENDCPRB
)
1147 return trans_xcRB32(filp
, cmd
, arg
);
1148 return zcrypt_unlocked_ioctl(filp
, cmd
, arg
);
1153 * Misc device file operations.
1155 static const struct file_operations zcrypt_fops
= {
1156 .owner
= THIS_MODULE
,
1157 .read
= zcrypt_read
,
1158 .write
= zcrypt_write
,
1159 .unlocked_ioctl
= zcrypt_unlocked_ioctl
,
1160 #ifdef CONFIG_COMPAT
1161 .compat_ioctl
= zcrypt_compat_ioctl
,
1163 .open
= zcrypt_open
,
1164 .release
= zcrypt_release
,
1165 .llseek
= no_llseek
,
1171 static struct miscdevice zcrypt_misc_device
= {
1172 .minor
= MISC_DYNAMIC_MINOR
,
1174 .fops
= &zcrypt_fops
,
1177 static int zcrypt_rng_device_count
;
1178 static u32
*zcrypt_rng_buffer
;
1179 static int zcrypt_rng_buffer_index
;
1180 static DEFINE_MUTEX(zcrypt_rng_mutex
);
1182 static int zcrypt_rng_data_read(struct hwrng
*rng
, u32
*data
)
1187 * We don't need locking here because the RNG API guarantees serialized
1188 * read method calls.
1190 if (zcrypt_rng_buffer_index
== 0) {
1191 rc
= zcrypt_rng((char *) zcrypt_rng_buffer
);
1192 /* on failure: retry once again after a requested rescan */
1193 if ((rc
== -ENODEV
) && (zcrypt_process_rescan()))
1194 rc
= zcrypt_rng((char *) zcrypt_rng_buffer
);
1197 zcrypt_rng_buffer_index
= rc
/ sizeof(*data
);
1199 *data
= zcrypt_rng_buffer
[--zcrypt_rng_buffer_index
];
1200 return sizeof(*data
);
1203 static struct hwrng zcrypt_rng_dev
= {
1205 .data_read
= zcrypt_rng_data_read
,
1209 int zcrypt_rng_device_add(void)
1213 mutex_lock(&zcrypt_rng_mutex
);
1214 if (zcrypt_rng_device_count
== 0) {
1215 zcrypt_rng_buffer
= (u32
*) get_zeroed_page(GFP_KERNEL
);
1216 if (!zcrypt_rng_buffer
) {
1220 zcrypt_rng_buffer_index
= 0;
1221 if (!zcrypt_hwrng_seed
)
1222 zcrypt_rng_dev
.quality
= 0;
1223 rc
= hwrng_register(&zcrypt_rng_dev
);
1226 zcrypt_rng_device_count
= 1;
1228 zcrypt_rng_device_count
++;
1229 mutex_unlock(&zcrypt_rng_mutex
);
1233 free_page((unsigned long) zcrypt_rng_buffer
);
1235 mutex_unlock(&zcrypt_rng_mutex
);
1239 void zcrypt_rng_device_remove(void)
1241 mutex_lock(&zcrypt_rng_mutex
);
1242 zcrypt_rng_device_count
--;
1243 if (zcrypt_rng_device_count
== 0) {
1244 hwrng_unregister(&zcrypt_rng_dev
);
1245 free_page((unsigned long) zcrypt_rng_buffer
);
1247 mutex_unlock(&zcrypt_rng_mutex
);
1250 int __init
zcrypt_debug_init(void)
1252 zcrypt_dbf_info
= debug_register("zcrypt", 1, 1,
1253 DBF_MAX_SPRINTF_ARGS
* sizeof(long));
1254 debug_register_view(zcrypt_dbf_info
, &debug_sprintf_view
);
1255 debug_set_level(zcrypt_dbf_info
, DBF_ERR
);
1260 void zcrypt_debug_exit(void)
1262 debug_unregister(zcrypt_dbf_info
);
1266 * zcrypt_api_init(): Module initialization.
1268 * The module initialization code.
1270 int __init
zcrypt_api_init(void)
1274 rc
= zcrypt_debug_init();
1278 /* Register the request sprayer. */
1279 rc
= misc_register(&zcrypt_misc_device
);
1283 zcrypt_msgtype6_init();
1284 zcrypt_msgtype50_init();
1292 * zcrypt_api_exit(): Module termination.
1294 * The module termination code.
1296 void __exit
zcrypt_api_exit(void)
1298 misc_deregister(&zcrypt_misc_device
);
1299 zcrypt_msgtype6_exit();
1300 zcrypt_msgtype50_exit();
1301 zcrypt_debug_exit();
1304 module_init(zcrypt_api_init
);
1305 module_exit(zcrypt_api_exit
);