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 <linux/uaccess.h>
40 #include <linux/hw_random.h>
41 #include <linux/debugfs.h>
42 #include <asm/debug.h>
44 #define CREATE_TRACE_POINTS
45 #include <asm/trace/zcrypt.h>
47 #include "zcrypt_api.h"
48 #include "zcrypt_debug.h"
50 #include "zcrypt_msgtype6.h"
51 #include "zcrypt_msgtype50.h"
56 MODULE_AUTHOR("IBM Corporation");
57 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
58 "Copyright IBM Corp. 2001, 2012");
59 MODULE_LICENSE("GPL");
62 * zcrypt tracepoint functions
64 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
65 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
67 static int zcrypt_hwrng_seed = 1;
68 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, S_IRUSR|S_IRGRP);
69 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
71 DEFINE_SPINLOCK(zcrypt_list_lock);
72 LIST_HEAD(zcrypt_card_list);
73 int zcrypt_device_count;
75 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
76 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
78 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
79 EXPORT_SYMBOL(zcrypt_rescan_req);
81 static LIST_HEAD(zcrypt_ops_list);
83 /* Zcrypt related debug feature stuff. */
84 debug_info_t *zcrypt_dbf_info;
87 * Process a rescan of the transport layer.
89 * Returns 1, if the rescan has been processed, otherwise 0.
91 static inline int zcrypt_process_rescan(void)
93 if (atomic_read(&zcrypt_rescan_req)) {
94 atomic_set(&zcrypt_rescan_req, 0);
95 atomic_inc(&zcrypt_rescan_count);
96 ap_bus_force_rescan();
97 ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
98 atomic_inc_return(&zcrypt_rescan_count));
104 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
106 list_add_tail(&zops->list, &zcrypt_ops_list);
109 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
111 list_del_init(&zops->list);
114 struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
116 struct zcrypt_ops *zops;
118 list_for_each_entry(zops, &zcrypt_ops_list, list)
119 if ((zops->variant == variant) &&
120 (!strncmp(zops->name, name, sizeof(zops->name))))
124 EXPORT_SYMBOL(zcrypt_msgtype);
127 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
129 * This function is not supported beyond zcrypt 1.3.1.
131 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
132 size_t count, loff_t *f_pos)
138 * zcrypt_write(): Not allowed.
140 * Write is is not allowed
142 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
143 size_t count, loff_t *f_pos)
149 * zcrypt_open(): Count number of users.
151 * Device open function to count number of users.
153 static int zcrypt_open(struct inode *inode, struct file *filp)
155 atomic_inc(&zcrypt_open_count);
156 return nonseekable_open(inode, filp);
160 * zcrypt_release(): Count number of users.
162 * Device close function to count number of users.
164 static int zcrypt_release(struct inode *inode, struct file *filp)
166 atomic_dec(&zcrypt_open_count);
170 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
171 struct zcrypt_queue *zq,
174 if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
176 zcrypt_queue_get(zq);
177 get_device(&zq->queue->ap_dev.device);
178 atomic_add(weight, &zc->load);
179 atomic_add(weight, &zq->load);
184 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
185 struct zcrypt_queue *zq,
188 struct module *mod = zq->queue->ap_dev.drv->driver.owner;
191 atomic_sub(weight, &zc->load);
192 atomic_sub(weight, &zq->load);
193 put_device(&zq->queue->ap_dev.device);
194 zcrypt_queue_put(zq);
198 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
199 struct zcrypt_card *pref_zc,
200 unsigned weight, unsigned pref_weight)
204 weight += atomic_read(&zc->load);
205 pref_weight += atomic_read(&pref_zc->load);
206 if (weight == pref_weight)
207 return atomic_read(&zc->card->total_request_count) >
208 atomic_read(&pref_zc->card->total_request_count);
209 return weight > pref_weight;
212 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
213 struct zcrypt_queue *pref_zq,
214 unsigned weight, unsigned pref_weight)
218 weight += atomic_read(&zq->load);
219 pref_weight += atomic_read(&pref_zq->load);
220 if (weight == pref_weight)
221 return zq->queue->total_request_count >
222 pref_zq->queue->total_request_count;
223 return weight > pref_weight;
229 static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
231 struct zcrypt_card *zc, *pref_zc;
232 struct zcrypt_queue *zq, *pref_zq;
233 unsigned int weight, pref_weight;
234 unsigned int func_code;
235 int qid = 0, rc = -ENODEV;
237 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
239 if (mex->outputdatalength < mex->inputdatalength) {
246 * As long as outputdatalength is big enough, we can set the
247 * outputdatalength equal to the inputdatalength, since that is the
248 * number of bytes we will copy in any case
250 mex->outputdatalength = mex->inputdatalength;
252 rc = get_rsa_modex_fc(mex, &func_code);
258 spin_lock(&zcrypt_list_lock);
259 for_each_zcrypt_card(zc) {
260 /* Check for online accelarator and CCA cards */
261 if (!zc->online || !(zc->card->functions & 0x18000000))
263 /* Check for size limits */
264 if (zc->min_mod_size > mex->inputdatalength ||
265 zc->max_mod_size < mex->inputdatalength)
267 /* get weight index of the card device */
268 weight = zc->speed_rating[func_code];
269 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
271 for_each_zcrypt_queue(zq, zc) {
272 /* check if device is online and eligible */
273 if (!zq->online || !zq->ops->rsa_modexpo)
275 if (zcrypt_queue_compare(zq, pref_zq,
276 weight, pref_weight))
280 pref_weight = weight;
283 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
284 spin_unlock(&zcrypt_list_lock);
291 qid = pref_zq->queue->qid;
292 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
294 spin_lock(&zcrypt_list_lock);
295 zcrypt_drop_queue(pref_zc, pref_zq, weight);
296 spin_unlock(&zcrypt_list_lock);
299 trace_s390_zcrypt_rep(mex, func_code, rc,
300 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
304 static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
306 struct zcrypt_card *zc, *pref_zc;
307 struct zcrypt_queue *zq, *pref_zq;
308 unsigned int weight, pref_weight;
309 unsigned int func_code;
310 int qid = 0, rc = -ENODEV;
312 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
314 if (crt->outputdatalength < crt->inputdatalength) {
321 * As long as outputdatalength is big enough, we can set the
322 * outputdatalength equal to the inputdatalength, since that is the
323 * number of bytes we will copy in any case
325 crt->outputdatalength = crt->inputdatalength;
327 rc = get_rsa_crt_fc(crt, &func_code);
333 spin_lock(&zcrypt_list_lock);
334 for_each_zcrypt_card(zc) {
335 /* Check for online accelarator and CCA cards */
336 if (!zc->online || !(zc->card->functions & 0x18000000))
338 /* Check for size limits */
339 if (zc->min_mod_size > crt->inputdatalength ||
340 zc->max_mod_size < crt->inputdatalength)
342 /* get weight index of the card device */
343 weight = zc->speed_rating[func_code];
344 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
346 for_each_zcrypt_queue(zq, zc) {
347 /* check if device is online and eligible */
348 if (!zq->online || !zq->ops->rsa_modexpo_crt)
350 if (zcrypt_queue_compare(zq, pref_zq,
351 weight, pref_weight))
355 pref_weight = weight;
358 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
359 spin_unlock(&zcrypt_list_lock);
366 qid = pref_zq->queue->qid;
367 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
369 spin_lock(&zcrypt_list_lock);
370 zcrypt_drop_queue(pref_zc, pref_zq, weight);
371 spin_unlock(&zcrypt_list_lock);
374 trace_s390_zcrypt_rep(crt, func_code, rc,
375 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
379 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
381 struct zcrypt_card *zc, *pref_zc;
382 struct zcrypt_queue *zq, *pref_zq;
383 struct ap_message ap_msg;
384 unsigned int weight, pref_weight;
385 unsigned int func_code;
386 unsigned short *domain;
387 int qid = 0, rc = -ENODEV;
389 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
391 rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
397 spin_lock(&zcrypt_list_lock);
398 for_each_zcrypt_card(zc) {
399 /* Check for online CCA cards */
400 if (!zc->online || !(zc->card->functions & 0x10000000))
402 /* Check for user selected CCA card */
403 if (xcRB->user_defined != AUTOSELECT &&
404 xcRB->user_defined != zc->card->id)
406 /* get weight index of the card device */
407 weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
408 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
410 for_each_zcrypt_queue(zq, zc) {
411 /* check if device is online and eligible */
413 !zq->ops->send_cprb ||
414 ((*domain != (unsigned short) AUTOSELECT) &&
415 (*domain != AP_QID_QUEUE(zq->queue->qid))))
417 if (zcrypt_queue_compare(zq, pref_zq,
418 weight, pref_weight))
422 pref_weight = weight;
425 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
426 spin_unlock(&zcrypt_list_lock);
433 /* in case of auto select, provide the correct domain */
434 qid = pref_zq->queue->qid;
435 if (*domain == (unsigned short) AUTOSELECT)
436 *domain = AP_QID_QUEUE(qid);
438 rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
440 spin_lock(&zcrypt_list_lock);
441 zcrypt_drop_queue(pref_zc, pref_zq, weight);
442 spin_unlock(&zcrypt_list_lock);
445 trace_s390_zcrypt_rep(xcRB, func_code, rc,
446 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
449 EXPORT_SYMBOL(zcrypt_send_cprb);
451 static bool is_desired_ep11_card(unsigned int dev_id,
452 unsigned short target_num,
453 struct ep11_target_dev *targets)
455 while (target_num-- > 0) {
456 if (dev_id == targets->ap_id)
463 static bool is_desired_ep11_queue(unsigned int dev_qid,
464 unsigned short target_num,
465 struct ep11_target_dev *targets)
467 while (target_num-- > 0) {
468 if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
475 static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
477 struct zcrypt_card *zc, *pref_zc;
478 struct zcrypt_queue *zq, *pref_zq;
479 struct ep11_target_dev *targets;
480 unsigned short target_num;
481 unsigned int weight, pref_weight;
482 unsigned int func_code;
483 struct ap_message ap_msg;
484 int qid = 0, rc = -ENODEV;
486 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
488 target_num = (unsigned short) xcrb->targets_num;
490 /* empty list indicates autoselect (all available targets) */
492 if (target_num != 0) {
493 struct ep11_target_dev __user *uptr;
495 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
502 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
503 if (copy_from_user(targets, uptr,
504 target_num * sizeof(*targets))) {
511 rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
517 spin_lock(&zcrypt_list_lock);
518 for_each_zcrypt_card(zc) {
519 /* Check for online EP11 cards */
520 if (!zc->online || !(zc->card->functions & 0x04000000))
522 /* Check for user selected EP11 card */
524 !is_desired_ep11_card(zc->card->id, target_num, targets))
526 /* get weight index of the card device */
527 weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
528 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
530 for_each_zcrypt_queue(zq, zc) {
531 /* check if device is online and eligible */
533 !zq->ops->send_ep11_cprb ||
535 !is_desired_ep11_queue(zq->queue->qid,
536 target_num, targets)))
538 if (zcrypt_queue_compare(zq, pref_zq,
539 weight, pref_weight))
543 pref_weight = weight;
546 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
547 spin_unlock(&zcrypt_list_lock);
554 qid = pref_zq->queue->qid;
555 rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
557 spin_lock(&zcrypt_list_lock);
558 zcrypt_drop_queue(pref_zc, pref_zq, weight);
559 spin_unlock(&zcrypt_list_lock);
564 trace_s390_zcrypt_rep(xcrb, func_code, rc,
565 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
569 static long zcrypt_rng(char *buffer)
571 struct zcrypt_card *zc, *pref_zc;
572 struct zcrypt_queue *zq, *pref_zq;
573 unsigned int weight, pref_weight;
574 unsigned int func_code;
575 struct ap_message ap_msg;
577 int qid = 0, rc = -ENODEV;
579 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
581 rc = get_rng_fc(&ap_msg, &func_code, &domain);
587 spin_lock(&zcrypt_list_lock);
588 for_each_zcrypt_card(zc) {
589 /* Check for online CCA cards */
590 if (!zc->online || !(zc->card->functions & 0x10000000))
592 /* get weight index of the card device */
593 weight = zc->speed_rating[func_code];
594 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
596 for_each_zcrypt_queue(zq, zc) {
597 /* check if device is online and eligible */
598 if (!zq->online || !zq->ops->rng)
600 if (zcrypt_queue_compare(zq, pref_zq,
601 weight, pref_weight))
605 pref_weight = weight;
608 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, weight);
609 spin_unlock(&zcrypt_list_lock);
614 qid = pref_zq->queue->qid;
615 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
617 spin_lock(&zcrypt_list_lock);
618 zcrypt_drop_queue(pref_zc, pref_zq, weight);
619 spin_unlock(&zcrypt_list_lock);
622 trace_s390_zcrypt_rep(buffer, func_code, rc,
623 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
627 void zcrypt_device_status_mask(struct zcrypt_device_matrix *matrix)
629 struct zcrypt_card *zc;
630 struct zcrypt_queue *zq;
631 struct zcrypt_device_status *stat;
633 memset(matrix, 0, sizeof(*matrix));
634 spin_lock(&zcrypt_list_lock);
635 for_each_zcrypt_card(zc) {
636 for_each_zcrypt_queue(zq, zc) {
637 stat = matrix->device;
638 stat += AP_QID_CARD(zq->queue->qid) * MAX_ZDEV_DOMAINS;
639 stat += AP_QID_QUEUE(zq->queue->qid);
640 stat->hwtype = zc->card->ap_dev.device_type;
641 stat->functions = zc->card->functions >> 26;
642 stat->qid = zq->queue->qid;
643 stat->online = zq->online ? 0x01 : 0x00;
646 spin_unlock(&zcrypt_list_lock);
648 EXPORT_SYMBOL(zcrypt_device_status_mask);
650 static void zcrypt_status_mask(char status[AP_DEVICES])
652 struct zcrypt_card *zc;
653 struct zcrypt_queue *zq;
655 memset(status, 0, sizeof(char) * AP_DEVICES);
656 spin_lock(&zcrypt_list_lock);
657 for_each_zcrypt_card(zc) {
658 for_each_zcrypt_queue(zq, zc) {
659 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
661 status[AP_QID_CARD(zq->queue->qid)] =
662 zc->online ? zc->user_space_type : 0x0d;
665 spin_unlock(&zcrypt_list_lock);
668 static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
670 struct zcrypt_card *zc;
671 struct zcrypt_queue *zq;
673 memset(qdepth, 0, sizeof(char) * AP_DEVICES);
674 spin_lock(&zcrypt_list_lock);
676 for_each_zcrypt_card(zc) {
677 for_each_zcrypt_queue(zq, zc) {
678 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
680 spin_lock(&zq->queue->lock);
681 qdepth[AP_QID_CARD(zq->queue->qid)] =
682 zq->queue->pendingq_count +
683 zq->queue->requestq_count;
684 spin_unlock(&zq->queue->lock);
688 spin_unlock(&zcrypt_list_lock);
691 static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
693 struct zcrypt_card *zc;
694 struct zcrypt_queue *zq;
696 memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
697 spin_lock(&zcrypt_list_lock);
699 for_each_zcrypt_card(zc) {
700 for_each_zcrypt_queue(zq, zc) {
701 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
703 spin_lock(&zq->queue->lock);
704 reqcnt[AP_QID_CARD(zq->queue->qid)] =
705 zq->queue->total_request_count;
706 spin_unlock(&zq->queue->lock);
710 spin_unlock(&zcrypt_list_lock);
713 static int zcrypt_pendingq_count(void)
715 struct zcrypt_card *zc;
716 struct zcrypt_queue *zq;
720 spin_lock(&zcrypt_list_lock);
722 for_each_zcrypt_card(zc) {
723 for_each_zcrypt_queue(zq, zc) {
724 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
726 spin_lock(&zq->queue->lock);
727 pendingq_count += zq->queue->pendingq_count;
728 spin_unlock(&zq->queue->lock);
732 spin_unlock(&zcrypt_list_lock);
733 return pendingq_count;
736 static int zcrypt_requestq_count(void)
738 struct zcrypt_card *zc;
739 struct zcrypt_queue *zq;
743 spin_lock(&zcrypt_list_lock);
745 for_each_zcrypt_card(zc) {
746 for_each_zcrypt_queue(zq, zc) {
747 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
749 spin_lock(&zq->queue->lock);
750 requestq_count += zq->queue->requestq_count;
751 spin_unlock(&zq->queue->lock);
755 spin_unlock(&zcrypt_list_lock);
756 return requestq_count;
759 static int zcrypt_count_type(int type)
761 struct zcrypt_card *zc;
762 struct zcrypt_queue *zq;
766 spin_lock(&zcrypt_list_lock);
767 for_each_zcrypt_card(zc) {
768 if (zc->card->id != type)
770 for_each_zcrypt_queue(zq, zc) {
771 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
776 spin_unlock(&zcrypt_list_lock);
781 * zcrypt_ica_status(): Old, depracted combi status call.
783 * Old, deprecated combi status call.
785 static long zcrypt_ica_status(struct file *filp, unsigned long arg)
787 struct ica_z90_status *pstat;
790 pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
793 pstat->totalcount = zcrypt_device_count;
794 pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
795 pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
796 pstat->requestqWaitCount = zcrypt_requestq_count();
797 pstat->pendingqWaitCount = zcrypt_pendingq_count();
798 pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
799 pstat->cryptoDomain = ap_domain_index;
800 zcrypt_status_mask(pstat->status);
801 zcrypt_qdepth_mask(pstat->qdepth);
803 if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
809 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
815 case ICARSAMODEXPO: {
816 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
817 struct ica_rsa_modexpo mex;
818 if (copy_from_user(&mex, umex, sizeof(mex)))
821 rc = zcrypt_rsa_modexpo(&mex);
822 } while (rc == -EAGAIN);
823 /* on failure: retry once again after a requested rescan */
824 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
826 rc = zcrypt_rsa_modexpo(&mex);
827 } while (rc == -EAGAIN);
829 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
832 return put_user(mex.outputdatalength, &umex->outputdatalength);
835 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
836 struct ica_rsa_modexpo_crt crt;
837 if (copy_from_user(&crt, ucrt, sizeof(crt)))
840 rc = zcrypt_rsa_crt(&crt);
841 } while (rc == -EAGAIN);
842 /* on failure: retry once again after a requested rescan */
843 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
845 rc = zcrypt_rsa_crt(&crt);
846 } while (rc == -EAGAIN);
848 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
851 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
854 struct ica_xcRB __user *uxcRB = (void __user *) arg;
855 struct ica_xcRB xcRB;
856 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
859 rc = zcrypt_send_cprb(&xcRB);
860 } while (rc == -EAGAIN);
861 /* on failure: retry once again after a requested rescan */
862 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
864 rc = zcrypt_send_cprb(&xcRB);
865 } while (rc == -EAGAIN);
867 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d\n", rc);
868 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
872 case ZSENDEP11CPRB: {
873 struct ep11_urb __user *uxcrb = (void __user *)arg;
874 struct ep11_urb xcrb;
875 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
878 rc = zcrypt_send_ep11_cprb(&xcrb);
879 } while (rc == -EAGAIN);
880 /* on failure: retry once again after a requested rescan */
881 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
883 rc = zcrypt_send_ep11_cprb(&xcrb);
884 } while (rc == -EAGAIN);
886 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
887 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
891 case ZDEVICESTATUS: {
892 struct zcrypt_device_matrix *device_status;
894 device_status = kzalloc(sizeof(struct zcrypt_device_matrix),
899 zcrypt_device_status_mask(device_status);
901 if (copy_to_user((char __user *) arg, device_status,
902 sizeof(struct zcrypt_device_matrix))) {
903 kfree(device_status);
907 kfree(device_status);
910 case Z90STAT_STATUS_MASK: {
911 char status[AP_DEVICES];
912 zcrypt_status_mask(status);
913 if (copy_to_user((char __user *) arg, status,
914 sizeof(char) * AP_DEVICES))
918 case Z90STAT_QDEPTH_MASK: {
919 char qdepth[AP_DEVICES];
920 zcrypt_qdepth_mask(qdepth);
921 if (copy_to_user((char __user *) arg, qdepth,
922 sizeof(char) * AP_DEVICES))
926 case Z90STAT_PERDEV_REQCNT: {
927 int reqcnt[AP_DEVICES];
928 zcrypt_perdev_reqcnt(reqcnt);
929 if (copy_to_user((int __user *) arg, reqcnt,
930 sizeof(int) * AP_DEVICES))
934 case Z90STAT_REQUESTQ_COUNT:
935 return put_user(zcrypt_requestq_count(), (int __user *) arg);
936 case Z90STAT_PENDINGQ_COUNT:
937 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
938 case Z90STAT_TOTALOPEN_COUNT:
939 return put_user(atomic_read(&zcrypt_open_count),
941 case Z90STAT_DOMAIN_INDEX:
942 return put_user(ap_domain_index, (int __user *) arg);
944 * Deprecated ioctls. Don't add another device count ioctl,
945 * you can count them yourself in the user space with the
946 * output of the Z90STAT_STATUS_MASK ioctl.
949 return zcrypt_ica_status(filp, arg);
950 case Z90STAT_TOTALCOUNT:
951 return put_user(zcrypt_device_count, (int __user *) arg);
952 case Z90STAT_PCICACOUNT:
953 return put_user(zcrypt_count_type(ZCRYPT_PCICA),
955 case Z90STAT_PCICCCOUNT:
956 return put_user(zcrypt_count_type(ZCRYPT_PCICC),
958 case Z90STAT_PCIXCCMCL2COUNT:
959 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
961 case Z90STAT_PCIXCCMCL3COUNT:
962 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
964 case Z90STAT_PCIXCCCOUNT:
965 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
966 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
968 case Z90STAT_CEX2CCOUNT:
969 return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
971 case Z90STAT_CEX2ACOUNT:
972 return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
975 /* unknown ioctl number */
982 * ioctl32 conversion routines
984 struct compat_ica_rsa_modexpo {
985 compat_uptr_t inputdata;
986 unsigned int inputdatalength;
987 compat_uptr_t outputdata;
988 unsigned int outputdatalength;
990 compat_uptr_t n_modulus;
993 static long trans_modexpo32(struct file *filp, unsigned int cmd,
996 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
997 struct compat_ica_rsa_modexpo mex32;
998 struct ica_rsa_modexpo mex64;
1001 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1003 mex64.inputdata = compat_ptr(mex32.inputdata);
1004 mex64.inputdatalength = mex32.inputdatalength;
1005 mex64.outputdata = compat_ptr(mex32.outputdata);
1006 mex64.outputdatalength = mex32.outputdatalength;
1007 mex64.b_key = compat_ptr(mex32.b_key);
1008 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1010 rc = zcrypt_rsa_modexpo(&mex64);
1011 } while (rc == -EAGAIN);
1012 /* on failure: retry once again after a requested rescan */
1013 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1015 rc = zcrypt_rsa_modexpo(&mex64);
1016 } while (rc == -EAGAIN);
1019 return put_user(mex64.outputdatalength,
1020 &umex32->outputdatalength);
1023 struct compat_ica_rsa_modexpo_crt {
1024 compat_uptr_t inputdata;
1025 unsigned int inputdatalength;
1026 compat_uptr_t outputdata;
1027 unsigned int outputdatalength;
1028 compat_uptr_t bp_key;
1029 compat_uptr_t bq_key;
1030 compat_uptr_t np_prime;
1031 compat_uptr_t nq_prime;
1032 compat_uptr_t u_mult_inv;
1035 static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
1038 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1039 struct compat_ica_rsa_modexpo_crt crt32;
1040 struct ica_rsa_modexpo_crt crt64;
1043 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1045 crt64.inputdata = compat_ptr(crt32.inputdata);
1046 crt64.inputdatalength = crt32.inputdatalength;
1047 crt64.outputdata= compat_ptr(crt32.outputdata);
1048 crt64.outputdatalength = crt32.outputdatalength;
1049 crt64.bp_key = compat_ptr(crt32.bp_key);
1050 crt64.bq_key = compat_ptr(crt32.bq_key);
1051 crt64.np_prime = compat_ptr(crt32.np_prime);
1052 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1053 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1055 rc = zcrypt_rsa_crt(&crt64);
1056 } while (rc == -EAGAIN);
1057 /* on failure: retry once again after a requested rescan */
1058 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1060 rc = zcrypt_rsa_crt(&crt64);
1061 } while (rc == -EAGAIN);
1064 return put_user(crt64.outputdatalength,
1065 &ucrt32->outputdatalength);
1068 struct compat_ica_xcRB {
1069 unsigned short agent_ID;
1070 unsigned int user_defined;
1071 unsigned short request_ID;
1072 unsigned int request_control_blk_length;
1073 unsigned char padding1[16 - sizeof (compat_uptr_t)];
1074 compat_uptr_t request_control_blk_addr;
1075 unsigned int request_data_length;
1076 char padding2[16 - sizeof (compat_uptr_t)];
1077 compat_uptr_t request_data_address;
1078 unsigned int reply_control_blk_length;
1079 char padding3[16 - sizeof (compat_uptr_t)];
1080 compat_uptr_t reply_control_blk_addr;
1081 unsigned int reply_data_length;
1082 char padding4[16 - sizeof (compat_uptr_t)];
1083 compat_uptr_t reply_data_addr;
1084 unsigned short priority_window;
1085 unsigned int status;
1086 } __attribute__((packed));
1088 static long trans_xcRB32(struct file *filp, unsigned int cmd,
1091 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1092 struct compat_ica_xcRB xcRB32;
1093 struct ica_xcRB xcRB64;
1096 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1098 xcRB64.agent_ID = xcRB32.agent_ID;
1099 xcRB64.user_defined = xcRB32.user_defined;
1100 xcRB64.request_ID = xcRB32.request_ID;
1101 xcRB64.request_control_blk_length =
1102 xcRB32.request_control_blk_length;
1103 xcRB64.request_control_blk_addr =
1104 compat_ptr(xcRB32.request_control_blk_addr);
1105 xcRB64.request_data_length =
1106 xcRB32.request_data_length;
1107 xcRB64.request_data_address =
1108 compat_ptr(xcRB32.request_data_address);
1109 xcRB64.reply_control_blk_length =
1110 xcRB32.reply_control_blk_length;
1111 xcRB64.reply_control_blk_addr =
1112 compat_ptr(xcRB32.reply_control_blk_addr);
1113 xcRB64.reply_data_length = xcRB32.reply_data_length;
1114 xcRB64.reply_data_addr =
1115 compat_ptr(xcRB32.reply_data_addr);
1116 xcRB64.priority_window = xcRB32.priority_window;
1117 xcRB64.status = xcRB32.status;
1119 rc = zcrypt_send_cprb(&xcRB64);
1120 } while (rc == -EAGAIN);
1121 /* on failure: retry once again after a requested rescan */
1122 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1124 rc = zcrypt_send_cprb(&xcRB64);
1125 } while (rc == -EAGAIN);
1126 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1127 xcRB32.reply_data_length = xcRB64.reply_data_length;
1128 xcRB32.status = xcRB64.status;
1129 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1134 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1137 if (cmd == ICARSAMODEXPO)
1138 return trans_modexpo32(filp, cmd, arg);
1139 if (cmd == ICARSACRT)
1140 return trans_modexpo_crt32(filp, cmd, arg);
1141 if (cmd == ZSECSENDCPRB)
1142 return trans_xcRB32(filp, cmd, arg);
1143 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1148 * Misc device file operations.
1150 static const struct file_operations zcrypt_fops = {
1151 .owner = THIS_MODULE,
1152 .read = zcrypt_read,
1153 .write = zcrypt_write,
1154 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1155 #ifdef CONFIG_COMPAT
1156 .compat_ioctl = zcrypt_compat_ioctl,
1158 .open = zcrypt_open,
1159 .release = zcrypt_release,
1160 .llseek = no_llseek,
1166 static struct miscdevice zcrypt_misc_device = {
1167 .minor = MISC_DYNAMIC_MINOR,
1169 .fops = &zcrypt_fops,
1173 * Deprecated /proc entry support.
1175 static struct proc_dir_entry *zcrypt_entry;
1177 static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
1181 for (i = 0; i < len; i++)
1182 seq_printf(m, "%01x", (unsigned int) addr[i]);
1186 static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
1192 for (c = 0; c < (len / 16); c++) {
1193 sprintcl(m, addr+inl, 16);
1198 sprintcl(m, addr+inl, cx);
1204 static void sprinthx(unsigned char *title, struct seq_file *m,
1205 unsigned char *addr, unsigned int len)
1209 seq_printf(m, "\n%s\n", title);
1211 for (r = 0; r < (len / 64); r++) {
1212 sprintrw(m, addr+inl, 64);
1217 sprintrw(m, addr+inl, rx);
1223 static void sprinthx4(unsigned char *title, struct seq_file *m,
1224 unsigned int *array, unsigned int len)
1226 seq_printf(m, "\n%s\n", title);
1227 seq_hex_dump(m, " ", DUMP_PREFIX_NONE, 32, 4, array, len, false);
1231 static int zcrypt_proc_show(struct seq_file *m, void *v)
1233 char workarea[sizeof(int) * AP_DEVICES];
1235 seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
1236 ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
1237 seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
1238 seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
1239 seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
1240 seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
1241 seq_printf(m, "PCIXCC MCL2 count: %d\n",
1242 zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
1243 seq_printf(m, "PCIXCC MCL3 count: %d\n",
1244 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1245 seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
1246 seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
1247 seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
1248 seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
1249 seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
1250 seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
1251 seq_printf(m, "Total open handles: %d\n\n",
1252 atomic_read(&zcrypt_open_count));
1253 zcrypt_status_mask(workarea);
1254 sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1255 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1256 m, workarea, AP_DEVICES);
1257 zcrypt_qdepth_mask(workarea);
1258 sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
1259 zcrypt_perdev_reqcnt((int *) workarea);
1260 sprinthx4("Per-device successfully completed request counts",
1261 m, (unsigned int *) workarea, AP_DEVICES);
1265 static int zcrypt_proc_open(struct inode *inode, struct file *file)
1267 return single_open(file, zcrypt_proc_show, NULL);
1270 static void zcrypt_disable_card(int index)
1272 struct zcrypt_card *zc;
1273 struct zcrypt_queue *zq;
1275 spin_lock(&zcrypt_list_lock);
1276 for_each_zcrypt_card(zc) {
1277 for_each_zcrypt_queue(zq, zc) {
1278 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1281 ap_flush_queue(zq->queue);
1284 spin_unlock(&zcrypt_list_lock);
1287 static void zcrypt_enable_card(int index)
1289 struct zcrypt_card *zc;
1290 struct zcrypt_queue *zq;
1292 spin_lock(&zcrypt_list_lock);
1293 for_each_zcrypt_card(zc) {
1294 for_each_zcrypt_queue(zq, zc) {
1295 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1298 ap_flush_queue(zq->queue);
1301 spin_unlock(&zcrypt_list_lock);
1304 static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
1305 size_t count, loff_t *pos)
1307 unsigned char *lbuf, *ptr;
1314 #define LBUFSIZE 1200UL
1315 lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1319 local_count = min(LBUFSIZE - 1, count);
1320 if (copy_from_user(lbuf, buffer, local_count) != 0) {
1324 lbuf[local_count] = '\0';
1326 ptr = strstr(lbuf, "Online devices");
1329 ptr = strstr(ptr, "\n");
1334 if (strstr(ptr, "Waiting work element counts") == NULL)
1337 for (j = 0; j < 64 && *ptr; ptr++) {
1339 * '0' for no device, '1' for PCICA, '2' for PCICC,
1340 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1341 * '5' for CEX2C and '6' for CEX2A'
1342 * '7' for CEX3C and '8' for CEX3A
1344 if (*ptr >= '0' && *ptr <= '8')
1346 else if (*ptr == 'd' || *ptr == 'D')
1347 zcrypt_disable_card(j++);
1348 else if (*ptr == 'e' || *ptr == 'E')
1349 zcrypt_enable_card(j++);
1350 else if (*ptr != ' ' && *ptr != '\t')
1358 static const struct file_operations zcrypt_proc_fops = {
1359 .owner = THIS_MODULE,
1360 .open = zcrypt_proc_open,
1362 .llseek = seq_lseek,
1363 .release = single_release,
1364 .write = zcrypt_proc_write,
1367 static int zcrypt_rng_device_count;
1368 static u32 *zcrypt_rng_buffer;
1369 static int zcrypt_rng_buffer_index;
1370 static DEFINE_MUTEX(zcrypt_rng_mutex);
1372 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1377 * We don't need locking here because the RNG API guarantees serialized
1378 * read method calls.
1380 if (zcrypt_rng_buffer_index == 0) {
1381 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1382 /* on failure: retry once again after a requested rescan */
1383 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1384 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1387 zcrypt_rng_buffer_index = rc / sizeof *data;
1389 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1390 return sizeof *data;
1393 static struct hwrng zcrypt_rng_dev = {
1395 .data_read = zcrypt_rng_data_read,
1399 int zcrypt_rng_device_add(void)
1403 mutex_lock(&zcrypt_rng_mutex);
1404 if (zcrypt_rng_device_count == 0) {
1405 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1406 if (!zcrypt_rng_buffer) {
1410 zcrypt_rng_buffer_index = 0;
1411 if (!zcrypt_hwrng_seed)
1412 zcrypt_rng_dev.quality = 0;
1413 rc = hwrng_register(&zcrypt_rng_dev);
1416 zcrypt_rng_device_count = 1;
1418 zcrypt_rng_device_count++;
1419 mutex_unlock(&zcrypt_rng_mutex);
1423 free_page((unsigned long) zcrypt_rng_buffer);
1425 mutex_unlock(&zcrypt_rng_mutex);
1429 void zcrypt_rng_device_remove(void)
1431 mutex_lock(&zcrypt_rng_mutex);
1432 zcrypt_rng_device_count--;
1433 if (zcrypt_rng_device_count == 0) {
1434 hwrng_unregister(&zcrypt_rng_dev);
1435 free_page((unsigned long) zcrypt_rng_buffer);
1437 mutex_unlock(&zcrypt_rng_mutex);
1440 int __init zcrypt_debug_init(void)
1442 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1443 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1444 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1445 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1450 void zcrypt_debug_exit(void)
1452 debug_unregister(zcrypt_dbf_info);
1456 * zcrypt_api_init(): Module initialization.
1458 * The module initialization code.
1460 int __init zcrypt_api_init(void)
1464 rc = zcrypt_debug_init();
1468 atomic_set(&zcrypt_rescan_req, 0);
1470 /* Register the request sprayer. */
1471 rc = misc_register(&zcrypt_misc_device);
1475 /* Set up the proc file system */
1476 zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL,
1478 if (!zcrypt_entry) {
1483 zcrypt_msgtype6_init();
1484 zcrypt_msgtype50_init();
1488 misc_deregister(&zcrypt_misc_device);
1494 * zcrypt_api_exit(): Module termination.
1496 * The module termination code.
1498 void __exit zcrypt_api_exit(void)
1500 remove_proc_entry("driver/z90crypt", NULL);
1501 misc_deregister(&zcrypt_misc_device);
1502 zcrypt_msgtype6_exit();
1503 zcrypt_msgtype50_exit();
1504 zcrypt_debug_exit();
1507 module_init(zcrypt_api_init);
1508 module_exit(zcrypt_api_exit);