1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2001, 2018
4 * Author(s): Robert Burroughs
5 * Eric Rossman (edrossma@us.ibm.com)
6 * Cornelia Huck <cornelia.huck@de.ibm.com>
8 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Ralph Wuerthner <rwuerthn@de.ibm.com>
11 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
12 * Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/miscdevice.h>
20 #include <linux/compat.h>
21 #include <linux/slab.h>
22 #include <linux/atomic.h>
23 #include <linux/uaccess.h>
24 #include <linux/hw_random.h>
25 #include <linux/debugfs.h>
26 #include <linux/cdev.h>
27 #include <linux/ctype.h>
28 #include <asm/debug.h>
30 #define CREATE_TRACE_POINTS
31 #include <asm/trace/zcrypt.h>
33 #include "zcrypt_api.h"
34 #include "zcrypt_debug.h"
36 #include "zcrypt_msgtype6.h"
37 #include "zcrypt_msgtype50.h"
38 #include "zcrypt_ccamisc.h"
39 #include "zcrypt_ep11misc.h"
44 MODULE_AUTHOR("IBM Corporation");
45 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
46 "Copyright IBM Corp. 2001, 2012");
47 MODULE_LICENSE("GPL");
50 * zcrypt tracepoint functions
52 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
53 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
55 static int zcrypt_hwrng_seed = 1;
56 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
57 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
59 DEFINE_SPINLOCK(zcrypt_list_lock);
60 LIST_HEAD(zcrypt_card_list);
61 int zcrypt_device_count;
63 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
64 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
66 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
67 EXPORT_SYMBOL(zcrypt_rescan_req);
69 static LIST_HEAD(zcrypt_ops_list);
71 /* Zcrypt related debug feature stuff. */
72 debug_info_t *zcrypt_dbf_info;
75 * Process a rescan of the transport layer.
77 * Returns 1, if the rescan has been processed, otherwise 0.
79 static inline int zcrypt_process_rescan(void)
81 if (atomic_read(&zcrypt_rescan_req)) {
82 atomic_set(&zcrypt_rescan_req, 0);
83 atomic_inc(&zcrypt_rescan_count);
84 ap_bus_force_rescan();
85 ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
86 atomic_inc_return(&zcrypt_rescan_count));
92 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
94 list_add_tail(&zops->list, &zcrypt_ops_list);
97 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
99 list_del_init(&zops->list);
102 struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
104 struct zcrypt_ops *zops;
106 list_for_each_entry(zops, &zcrypt_ops_list, list)
107 if ((zops->variant == variant) &&
108 (!strncmp(zops->name, name, sizeof(zops->name))))
112 EXPORT_SYMBOL(zcrypt_msgtype);
115 * Multi device nodes extension functions.
118 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
122 static struct class *zcrypt_class;
123 static dev_t zcrypt_devt;
124 static struct cdev zcrypt_cdev;
127 struct device device;
128 struct ap_perms perms;
131 #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
133 #define ZCDN_MAX_NAME 32
135 static int zcdn_create(const char *name);
136 static int zcdn_destroy(const char *name);
139 * Find zcdn device by name.
140 * Returns reference to the zcdn device which needs to be released
141 * with put_device() after use.
143 static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
145 struct device *dev = class_find_device_by_name(zcrypt_class, name);
147 return dev ? to_zcdn_dev(dev) : NULL;
151 * Find zcdn device by devt value.
152 * Returns reference to the zcdn device which needs to be released
153 * with put_device() after use.
155 static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
157 struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
159 return dev ? to_zcdn_dev(dev) : NULL;
162 static ssize_t ioctlmask_show(struct device *dev,
163 struct device_attribute *attr,
167 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
169 if (mutex_lock_interruptible(&ap_perms_mutex))
174 for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
175 snprintf(buf + 2 + 2 * i * sizeof(long),
176 PAGE_SIZE - 2 - 2 * i * sizeof(long),
177 "%016lx", zcdndev->perms.ioctlm[i]);
178 buf[2 + 2 * i * sizeof(long)] = '\n';
179 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
180 rc = 2 + 2 * i * sizeof(long) + 1;
182 mutex_unlock(&ap_perms_mutex);
187 static ssize_t ioctlmask_store(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
192 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
194 rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
195 AP_IOCTLS, &ap_perms_mutex);
202 static DEVICE_ATTR_RW(ioctlmask);
204 static ssize_t apmask_show(struct device *dev,
205 struct device_attribute *attr,
209 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
211 if (mutex_lock_interruptible(&ap_perms_mutex))
216 for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
217 snprintf(buf + 2 + 2 * i * sizeof(long),
218 PAGE_SIZE - 2 - 2 * i * sizeof(long),
219 "%016lx", zcdndev->perms.apm[i]);
220 buf[2 + 2 * i * sizeof(long)] = '\n';
221 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
222 rc = 2 + 2 * i * sizeof(long) + 1;
224 mutex_unlock(&ap_perms_mutex);
229 static ssize_t apmask_store(struct device *dev,
230 struct device_attribute *attr,
231 const char *buf, size_t count)
234 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
236 rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
237 AP_DEVICES, &ap_perms_mutex);
244 static DEVICE_ATTR_RW(apmask);
246 static ssize_t aqmask_show(struct device *dev,
247 struct device_attribute *attr,
251 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
253 if (mutex_lock_interruptible(&ap_perms_mutex))
258 for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
259 snprintf(buf + 2 + 2 * i * sizeof(long),
260 PAGE_SIZE - 2 - 2 * i * sizeof(long),
261 "%016lx", zcdndev->perms.aqm[i]);
262 buf[2 + 2 * i * sizeof(long)] = '\n';
263 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
264 rc = 2 + 2 * i * sizeof(long) + 1;
266 mutex_unlock(&ap_perms_mutex);
271 static ssize_t aqmask_store(struct device *dev,
272 struct device_attribute *attr,
273 const char *buf, size_t count)
276 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
278 rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
279 AP_DOMAINS, &ap_perms_mutex);
286 static DEVICE_ATTR_RW(aqmask);
288 static struct attribute *zcdn_dev_attrs[] = {
289 &dev_attr_ioctlmask.attr,
290 &dev_attr_apmask.attr,
291 &dev_attr_aqmask.attr,
295 static struct attribute_group zcdn_dev_attr_group = {
296 .attrs = zcdn_dev_attrs
299 static const struct attribute_group *zcdn_dev_attr_groups[] = {
300 &zcdn_dev_attr_group,
304 static ssize_t zcdn_create_store(struct class *class,
305 struct class_attribute *attr,
306 const char *buf, size_t count)
309 char name[ZCDN_MAX_NAME];
311 strncpy(name, skip_spaces(buf), sizeof(name));
312 name[sizeof(name) - 1] = '\0';
314 rc = zcdn_create(strim(name));
316 return rc ? rc : count;
319 static const struct class_attribute class_attr_zcdn_create =
320 __ATTR(create, 0600, NULL, zcdn_create_store);
322 static ssize_t zcdn_destroy_store(struct class *class,
323 struct class_attribute *attr,
324 const char *buf, size_t count)
327 char name[ZCDN_MAX_NAME];
329 strncpy(name, skip_spaces(buf), sizeof(name));
330 name[sizeof(name) - 1] = '\0';
332 rc = zcdn_destroy(strim(name));
334 return rc ? rc : count;
337 static const struct class_attribute class_attr_zcdn_destroy =
338 __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
340 static void zcdn_device_release(struct device *dev)
342 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
344 ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
345 MAJOR(dev->devt), MINOR(dev->devt));
350 static int zcdn_create(const char *name)
354 char nodename[ZCDN_MAX_NAME];
355 struct zcdn_device *zcdndev;
357 if (mutex_lock_interruptible(&ap_perms_mutex))
360 /* check if device node with this name already exists */
362 zcdndev = find_zcdndev_by_name(name);
364 put_device(&zcdndev->device);
370 /* find an unused minor number */
371 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
372 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
373 zcdndev = find_zcdndev_by_devt(devt);
375 put_device(&zcdndev->device);
379 if (i == ZCRYPT_MAX_MINOR_NODES) {
384 /* alloc and prepare a new zcdn device */
385 zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
390 zcdndev->device.release = zcdn_device_release;
391 zcdndev->device.class = zcrypt_class;
392 zcdndev->device.devt = devt;
393 zcdndev->device.groups = zcdn_dev_attr_groups;
395 strncpy(nodename, name, sizeof(nodename));
397 snprintf(nodename, sizeof(nodename),
398 ZCRYPT_NAME "_%d", (int) MINOR(devt));
399 nodename[sizeof(nodename)-1] = '\0';
400 if (dev_set_name(&zcdndev->device, nodename)) {
404 rc = device_register(&zcdndev->device);
406 put_device(&zcdndev->device);
410 ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
411 MAJOR(devt), MINOR(devt));
414 mutex_unlock(&ap_perms_mutex);
418 static int zcdn_destroy(const char *name)
421 struct zcdn_device *zcdndev;
423 if (mutex_lock_interruptible(&ap_perms_mutex))
426 /* try to find this zcdn device */
427 zcdndev = find_zcdndev_by_name(name);
434 * The zcdn device is not hard destroyed. It is subject to
435 * reference counting and thus just needs to be unregistered.
437 put_device(&zcdndev->device);
438 device_unregister(&zcdndev->device);
441 mutex_unlock(&ap_perms_mutex);
445 static void zcdn_destroy_all(void)
449 struct zcdn_device *zcdndev;
451 mutex_lock(&ap_perms_mutex);
452 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
453 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
454 zcdndev = find_zcdndev_by_devt(devt);
456 put_device(&zcdndev->device);
457 device_unregister(&zcdndev->device);
460 mutex_unlock(&ap_perms_mutex);
466 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
468 * This function is not supported beyond zcrypt 1.3.1.
470 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
471 size_t count, loff_t *f_pos)
477 * zcrypt_write(): Not allowed.
479 * Write is is not allowed
481 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
482 size_t count, loff_t *f_pos)
488 * zcrypt_open(): Count number of users.
490 * Device open function to count number of users.
492 static int zcrypt_open(struct inode *inode, struct file *filp)
494 struct ap_perms *perms = &ap_perms;
496 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
497 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
498 struct zcdn_device *zcdndev;
500 if (mutex_lock_interruptible(&ap_perms_mutex))
502 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
503 /* find returns a reference, no get_device() needed */
504 mutex_unlock(&ap_perms_mutex);
506 perms = &zcdndev->perms;
509 filp->private_data = (void *) perms;
511 atomic_inc(&zcrypt_open_count);
512 return stream_open(inode, filp);
516 * zcrypt_release(): Count number of users.
518 * Device close function to count number of users.
520 static int zcrypt_release(struct inode *inode, struct file *filp)
522 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
523 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
524 struct zcdn_device *zcdndev;
526 mutex_lock(&ap_perms_mutex);
527 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
528 mutex_unlock(&ap_perms_mutex);
530 /* 2 puts here: one for find, one for open */
531 put_device(&zcdndev->device);
532 put_device(&zcdndev->device);
537 atomic_dec(&zcrypt_open_count);
541 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
545 int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
547 if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
548 if (test_bit_inv(ioctlnr, perms->ioctlm))
554 "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
560 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
562 return test_bit_inv(card, perms->apm) ? true : false;
565 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
567 return test_bit_inv(queue, perms->aqm) ? true : false;
570 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
571 struct zcrypt_queue *zq,
572 struct module **pmod,
575 if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
577 zcrypt_queue_get(zq);
578 get_device(&zq->queue->ap_dev.device);
579 atomic_add(weight, &zc->load);
580 atomic_add(weight, &zq->load);
582 *pmod = zq->queue->ap_dev.drv->driver.owner;
586 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
587 struct zcrypt_queue *zq,
592 atomic_sub(weight, &zc->load);
593 atomic_sub(weight, &zq->load);
594 put_device(&zq->queue->ap_dev.device);
595 zcrypt_queue_put(zq);
599 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
600 struct zcrypt_card *pref_zc,
602 unsigned int pref_weight)
606 weight += atomic_read(&zc->load);
607 pref_weight += atomic_read(&pref_zc->load);
608 if (weight == pref_weight)
609 return atomic64_read(&zc->card->total_request_count) <
610 atomic64_read(&pref_zc->card->total_request_count);
611 return weight < pref_weight;
614 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
615 struct zcrypt_queue *pref_zq,
617 unsigned int pref_weight)
621 weight += atomic_read(&zq->load);
622 pref_weight += atomic_read(&pref_zq->load);
623 if (weight == pref_weight)
624 return zq->queue->total_request_count <
625 pref_zq->queue->total_request_count;
626 return weight < pref_weight;
632 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
633 struct zcrypt_track *tr,
634 struct ica_rsa_modexpo *mex)
636 struct zcrypt_card *zc, *pref_zc;
637 struct zcrypt_queue *zq, *pref_zq;
638 struct ap_message ap_msg;
639 unsigned int wgt = 0, pref_wgt = 0;
640 unsigned int func_code;
641 int cpen, qpen, qid = 0, rc = -ENODEV;
644 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
646 ap_init_message(&ap_msg);
648 if (mex->outputdatalength < mex->inputdatalength) {
655 * As long as outputdatalength is big enough, we can set the
656 * outputdatalength equal to the inputdatalength, since that is the
657 * number of bytes we will copy in any case
659 mex->outputdatalength = mex->inputdatalength;
661 rc = get_rsa_modex_fc(mex, &func_code);
667 spin_lock(&zcrypt_list_lock);
668 for_each_zcrypt_card(zc) {
669 /* Check for useable accelarator or CCA card */
670 if (!zc->online || !zc->card->config ||
671 !(zc->card->functions & 0x18000000))
673 /* Check for size limits */
674 if (zc->min_mod_size > mex->inputdatalength ||
675 zc->max_mod_size < mex->inputdatalength)
677 /* check if device node has admission for this card */
678 if (!zcrypt_check_card(perms, zc->card->id))
680 /* get weight index of the card device */
681 wgt = zc->speed_rating[func_code];
682 /* penalty if this msg was previously sent via this card */
683 cpen = (tr && tr->again_counter && tr->last_qid &&
684 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
685 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
686 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
688 for_each_zcrypt_queue(zq, zc) {
689 /* check if device is useable and eligible */
690 if (!zq->online || !zq->ops->rsa_modexpo ||
693 /* check if device node has admission for this queue */
694 if (!zcrypt_check_queue(perms,
695 AP_QID_QUEUE(zq->queue->qid)))
697 /* penalty if the msg was previously sent at this qid */
698 qpen = (tr && tr->again_counter && tr->last_qid &&
699 tr->last_qid == zq->queue->qid) ?
700 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
701 if (!zcrypt_queue_compare(zq, pref_zq,
702 wgt + cpen + qpen, pref_wgt))
706 pref_wgt = wgt + cpen + qpen;
709 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
710 spin_unlock(&zcrypt_list_lock);
717 qid = pref_zq->queue->qid;
718 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
720 spin_lock(&zcrypt_list_lock);
721 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
722 spin_unlock(&zcrypt_list_lock);
725 ap_release_message(&ap_msg);
730 trace_s390_zcrypt_rep(mex, func_code, rc,
731 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
735 static long zcrypt_rsa_crt(struct ap_perms *perms,
736 struct zcrypt_track *tr,
737 struct ica_rsa_modexpo_crt *crt)
739 struct zcrypt_card *zc, *pref_zc;
740 struct zcrypt_queue *zq, *pref_zq;
741 struct ap_message ap_msg;
742 unsigned int wgt = 0, pref_wgt = 0;
743 unsigned int func_code;
744 int cpen, qpen, qid = 0, rc = -ENODEV;
747 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
749 ap_init_message(&ap_msg);
751 if (crt->outputdatalength < crt->inputdatalength) {
758 * As long as outputdatalength is big enough, we can set the
759 * outputdatalength equal to the inputdatalength, since that is the
760 * number of bytes we will copy in any case
762 crt->outputdatalength = crt->inputdatalength;
764 rc = get_rsa_crt_fc(crt, &func_code);
770 spin_lock(&zcrypt_list_lock);
771 for_each_zcrypt_card(zc) {
772 /* Check for useable accelarator or CCA card */
773 if (!zc->online || !zc->card->config ||
774 !(zc->card->functions & 0x18000000))
776 /* Check for size limits */
777 if (zc->min_mod_size > crt->inputdatalength ||
778 zc->max_mod_size < crt->inputdatalength)
780 /* check if device node has admission for this card */
781 if (!zcrypt_check_card(perms, zc->card->id))
783 /* get weight index of the card device */
784 wgt = zc->speed_rating[func_code];
785 /* penalty if this msg was previously sent via this card */
786 cpen = (tr && tr->again_counter && tr->last_qid &&
787 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
788 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
789 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
791 for_each_zcrypt_queue(zq, zc) {
792 /* check if device is useable and eligible */
793 if (!zq->online || !zq->ops->rsa_modexpo_crt ||
796 /* check if device node has admission for this queue */
797 if (!zcrypt_check_queue(perms,
798 AP_QID_QUEUE(zq->queue->qid)))
800 /* penalty if the msg was previously sent at this qid */
801 qpen = (tr && tr->again_counter && tr->last_qid &&
802 tr->last_qid == zq->queue->qid) ?
803 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
804 if (!zcrypt_queue_compare(zq, pref_zq,
805 wgt + cpen + qpen, pref_wgt))
809 pref_wgt = wgt + cpen + qpen;
812 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
813 spin_unlock(&zcrypt_list_lock);
820 qid = pref_zq->queue->qid;
821 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
823 spin_lock(&zcrypt_list_lock);
824 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
825 spin_unlock(&zcrypt_list_lock);
828 ap_release_message(&ap_msg);
833 trace_s390_zcrypt_rep(crt, func_code, rc,
834 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
838 static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
839 struct zcrypt_track *tr,
840 struct ica_xcRB *xcRB)
842 struct zcrypt_card *zc, *pref_zc;
843 struct zcrypt_queue *zq, *pref_zq;
844 struct ap_message ap_msg;
845 unsigned int wgt = 0, pref_wgt = 0;
846 unsigned int func_code;
847 unsigned short *domain, tdom;
848 int cpen, qpen, qid = 0, rc = -ENODEV;
851 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
854 ap_init_message(&ap_msg);
855 rc = get_cprb_fc(userspace, xcRB, &ap_msg, &func_code, &domain);
860 * If a valid target domain is set and this domain is NOT a usage
861 * domain but a control only domain, use the default domain as target.
864 if (tdom < AP_DOMAINS &&
865 !ap_test_config_usage_domain(tdom) &&
866 ap_test_config_ctrl_domain(tdom) &&
867 ap_domain_index >= 0)
868 tdom = ap_domain_index;
872 spin_lock(&zcrypt_list_lock);
873 for_each_zcrypt_card(zc) {
874 /* Check for useable CCA card */
875 if (!zc->online || !zc->card->config ||
876 !(zc->card->functions & 0x10000000))
878 /* Check for user selected CCA card */
879 if (xcRB->user_defined != AUTOSELECT &&
880 xcRB->user_defined != zc->card->id)
882 /* check if device node has admission for this card */
883 if (!zcrypt_check_card(perms, zc->card->id))
885 /* get weight index of the card device */
886 wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
887 /* penalty if this msg was previously sent via this card */
888 cpen = (tr && tr->again_counter && tr->last_qid &&
889 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
890 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
891 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
893 for_each_zcrypt_queue(zq, zc) {
894 /* check for device useable and eligible */
896 !zq->ops->send_cprb ||
897 !zq->queue->config ||
898 (tdom != AUTOSEL_DOM &&
899 tdom != AP_QID_QUEUE(zq->queue->qid)))
901 /* check if device node has admission for this queue */
902 if (!zcrypt_check_queue(perms,
903 AP_QID_QUEUE(zq->queue->qid)))
905 /* penalty if the msg was previously sent at this qid */
906 qpen = (tr && tr->again_counter && tr->last_qid &&
907 tr->last_qid == zq->queue->qid) ?
908 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
909 if (!zcrypt_queue_compare(zq, pref_zq,
910 wgt + cpen + qpen, pref_wgt))
914 pref_wgt = wgt + cpen + qpen;
917 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
918 spin_unlock(&zcrypt_list_lock);
925 /* in case of auto select, provide the correct domain */
926 qid = pref_zq->queue->qid;
927 if (*domain == AUTOSEL_DOM)
928 *domain = AP_QID_QUEUE(qid);
930 rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcRB, &ap_msg);
932 spin_lock(&zcrypt_list_lock);
933 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
934 spin_unlock(&zcrypt_list_lock);
937 ap_release_message(&ap_msg);
942 trace_s390_zcrypt_rep(xcRB, func_code, rc,
943 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
947 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
949 return _zcrypt_send_cprb(false, &ap_perms, NULL, xcRB);
951 EXPORT_SYMBOL(zcrypt_send_cprb);
953 static bool is_desired_ep11_card(unsigned int dev_id,
954 unsigned short target_num,
955 struct ep11_target_dev *targets)
957 while (target_num-- > 0) {
958 if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
965 static bool is_desired_ep11_queue(unsigned int dev_qid,
966 unsigned short target_num,
967 struct ep11_target_dev *targets)
969 int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
971 while (target_num-- > 0) {
972 if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
973 (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
980 static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
981 struct zcrypt_track *tr,
982 struct ep11_urb *xcrb)
984 struct zcrypt_card *zc, *pref_zc;
985 struct zcrypt_queue *zq, *pref_zq;
986 struct ep11_target_dev *targets;
987 unsigned short target_num;
988 unsigned int wgt = 0, pref_wgt = 0;
989 unsigned int func_code;
990 struct ap_message ap_msg;
991 int cpen, qpen, qid = 0, rc = -ENODEV;
994 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
996 ap_init_message(&ap_msg);
998 target_num = (unsigned short) xcrb->targets_num;
1000 /* empty list indicates autoselect (all available targets) */
1002 if (target_num != 0) {
1003 struct ep11_target_dev __user *uptr;
1005 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
1012 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
1013 if (z_copy_from_user(userspace, targets, uptr,
1014 target_num * sizeof(*targets))) {
1021 rc = get_ep11cprb_fc(userspace, xcrb, &ap_msg, &func_code);
1027 spin_lock(&zcrypt_list_lock);
1028 for_each_zcrypt_card(zc) {
1029 /* Check for useable EP11 card */
1030 if (!zc->online || !zc->card->config ||
1031 !(zc->card->functions & 0x04000000))
1033 /* Check for user selected EP11 card */
1035 !is_desired_ep11_card(zc->card->id, target_num, targets))
1037 /* check if device node has admission for this card */
1038 if (!zcrypt_check_card(perms, zc->card->id))
1040 /* get weight index of the card device */
1041 wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1042 /* penalty if this msg was previously sent via this card */
1043 cpen = (tr && tr->again_counter && tr->last_qid &&
1044 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
1045 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
1046 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
1048 for_each_zcrypt_queue(zq, zc) {
1049 /* check if device is useable and eligible */
1051 !zq->ops->send_ep11_cprb ||
1052 !zq->queue->config ||
1054 !is_desired_ep11_queue(zq->queue->qid,
1055 target_num, targets)))
1057 /* check if device node has admission for this queue */
1058 if (!zcrypt_check_queue(perms,
1059 AP_QID_QUEUE(zq->queue->qid)))
1061 /* penalty if the msg was previously sent at this qid */
1062 qpen = (tr && tr->again_counter && tr->last_qid &&
1063 tr->last_qid == zq->queue->qid) ?
1064 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
1065 if (!zcrypt_queue_compare(zq, pref_zq,
1066 wgt + cpen + qpen, pref_wgt))
1070 pref_wgt = wgt + cpen + qpen;
1073 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1074 spin_unlock(&zcrypt_list_lock);
1081 qid = pref_zq->queue->qid;
1082 rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
1084 spin_lock(&zcrypt_list_lock);
1085 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1086 spin_unlock(&zcrypt_list_lock);
1091 ap_release_message(&ap_msg);
1096 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1097 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1101 long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
1103 return _zcrypt_send_ep11_cprb(false, &ap_perms, NULL, xcrb);
1105 EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1107 static long zcrypt_rng(char *buffer)
1109 struct zcrypt_card *zc, *pref_zc;
1110 struct zcrypt_queue *zq, *pref_zq;
1111 unsigned int wgt = 0, pref_wgt = 0;
1112 unsigned int func_code;
1113 struct ap_message ap_msg;
1114 unsigned int domain;
1115 int qid = 0, rc = -ENODEV;
1118 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1120 ap_init_message(&ap_msg);
1121 rc = get_rng_fc(&ap_msg, &func_code, &domain);
1127 spin_lock(&zcrypt_list_lock);
1128 for_each_zcrypt_card(zc) {
1129 /* Check for useable CCA card */
1130 if (!zc->online || !zc->card->config ||
1131 !(zc->card->functions & 0x10000000))
1133 /* get weight index of the card device */
1134 wgt = zc->speed_rating[func_code];
1135 if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
1137 for_each_zcrypt_queue(zq, zc) {
1138 /* check if device is useable and eligible */
1139 if (!zq->online || !zq->ops->rng ||
1142 if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
1149 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1150 spin_unlock(&zcrypt_list_lock);
1157 qid = pref_zq->queue->qid;
1158 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1160 spin_lock(&zcrypt_list_lock);
1161 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1162 spin_unlock(&zcrypt_list_lock);
1165 ap_release_message(&ap_msg);
1166 trace_s390_zcrypt_rep(buffer, func_code, rc,
1167 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1171 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1173 struct zcrypt_card *zc;
1174 struct zcrypt_queue *zq;
1175 struct zcrypt_device_status *stat;
1178 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1179 * sizeof(struct zcrypt_device_status));
1181 spin_lock(&zcrypt_list_lock);
1182 for_each_zcrypt_card(zc) {
1183 for_each_zcrypt_queue(zq, zc) {
1184 card = AP_QID_CARD(zq->queue->qid);
1185 if (card >= MAX_ZDEV_CARDIDS)
1187 queue = AP_QID_QUEUE(zq->queue->qid);
1188 stat = &devstatus[card * AP_DOMAINS + queue];
1189 stat->hwtype = zc->card->ap_dev.device_type;
1190 stat->functions = zc->card->functions >> 26;
1191 stat->qid = zq->queue->qid;
1192 stat->online = zq->online ? 0x01 : 0x00;
1195 spin_unlock(&zcrypt_list_lock);
1198 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1200 struct zcrypt_card *zc;
1201 struct zcrypt_queue *zq;
1202 struct zcrypt_device_status_ext *stat;
1205 memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1206 * sizeof(struct zcrypt_device_status_ext));
1208 spin_lock(&zcrypt_list_lock);
1209 for_each_zcrypt_card(zc) {
1210 for_each_zcrypt_queue(zq, zc) {
1211 card = AP_QID_CARD(zq->queue->qid);
1212 queue = AP_QID_QUEUE(zq->queue->qid);
1213 stat = &devstatus[card * AP_DOMAINS + queue];
1214 stat->hwtype = zc->card->ap_dev.device_type;
1215 stat->functions = zc->card->functions >> 26;
1216 stat->qid = zq->queue->qid;
1217 stat->online = zq->online ? 0x01 : 0x00;
1220 spin_unlock(&zcrypt_list_lock);
1222 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1224 int zcrypt_device_status_ext(int card, int queue,
1225 struct zcrypt_device_status_ext *devstat)
1227 struct zcrypt_card *zc;
1228 struct zcrypt_queue *zq;
1230 memset(devstat, 0, sizeof(*devstat));
1232 spin_lock(&zcrypt_list_lock);
1233 for_each_zcrypt_card(zc) {
1234 for_each_zcrypt_queue(zq, zc) {
1235 if (card == AP_QID_CARD(zq->queue->qid) &&
1236 queue == AP_QID_QUEUE(zq->queue->qid)) {
1237 devstat->hwtype = zc->card->ap_dev.device_type;
1238 devstat->functions = zc->card->functions >> 26;
1239 devstat->qid = zq->queue->qid;
1240 devstat->online = zq->online ? 0x01 : 0x00;
1241 spin_unlock(&zcrypt_list_lock);
1246 spin_unlock(&zcrypt_list_lock);
1250 EXPORT_SYMBOL(zcrypt_device_status_ext);
1252 static void zcrypt_status_mask(char status[], size_t max_adapters)
1254 struct zcrypt_card *zc;
1255 struct zcrypt_queue *zq;
1258 memset(status, 0, max_adapters);
1259 spin_lock(&zcrypt_list_lock);
1260 for_each_zcrypt_card(zc) {
1261 for_each_zcrypt_queue(zq, zc) {
1262 card = AP_QID_CARD(zq->queue->qid);
1263 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1264 || card >= max_adapters)
1266 status[card] = zc->online ? zc->user_space_type : 0x0d;
1269 spin_unlock(&zcrypt_list_lock);
1272 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1274 struct zcrypt_card *zc;
1275 struct zcrypt_queue *zq;
1278 memset(qdepth, 0, max_adapters);
1279 spin_lock(&zcrypt_list_lock);
1281 for_each_zcrypt_card(zc) {
1282 for_each_zcrypt_queue(zq, zc) {
1283 card = AP_QID_CARD(zq->queue->qid);
1284 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1285 || card >= max_adapters)
1287 spin_lock(&zq->queue->lock);
1289 zq->queue->pendingq_count +
1290 zq->queue->requestq_count;
1291 spin_unlock(&zq->queue->lock);
1295 spin_unlock(&zcrypt_list_lock);
1298 static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1300 struct zcrypt_card *zc;
1301 struct zcrypt_queue *zq;
1305 memset(reqcnt, 0, sizeof(int) * max_adapters);
1306 spin_lock(&zcrypt_list_lock);
1308 for_each_zcrypt_card(zc) {
1309 for_each_zcrypt_queue(zq, zc) {
1310 card = AP_QID_CARD(zq->queue->qid);
1311 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1312 || card >= max_adapters)
1314 spin_lock(&zq->queue->lock);
1315 cnt = zq->queue->total_request_count;
1316 spin_unlock(&zq->queue->lock);
1317 reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX;
1321 spin_unlock(&zcrypt_list_lock);
1324 static int zcrypt_pendingq_count(void)
1326 struct zcrypt_card *zc;
1327 struct zcrypt_queue *zq;
1331 spin_lock(&zcrypt_list_lock);
1333 for_each_zcrypt_card(zc) {
1334 for_each_zcrypt_queue(zq, zc) {
1335 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1337 spin_lock(&zq->queue->lock);
1338 pendingq_count += zq->queue->pendingq_count;
1339 spin_unlock(&zq->queue->lock);
1343 spin_unlock(&zcrypt_list_lock);
1344 return pendingq_count;
1347 static int zcrypt_requestq_count(void)
1349 struct zcrypt_card *zc;
1350 struct zcrypt_queue *zq;
1354 spin_lock(&zcrypt_list_lock);
1356 for_each_zcrypt_card(zc) {
1357 for_each_zcrypt_queue(zq, zc) {
1358 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1360 spin_lock(&zq->queue->lock);
1361 requestq_count += zq->queue->requestq_count;
1362 spin_unlock(&zq->queue->lock);
1366 spin_unlock(&zcrypt_list_lock);
1367 return requestq_count;
1370 static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1373 struct zcrypt_track tr;
1374 struct ica_rsa_modexpo mex;
1375 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1377 memset(&tr, 0, sizeof(tr));
1378 if (copy_from_user(&mex, umex, sizeof(mex)))
1381 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1384 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1385 /* on failure: retry once again after a requested rescan */
1386 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1388 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1391 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1393 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1396 return put_user(mex.outputdatalength, &umex->outputdatalength);
1399 static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1402 struct zcrypt_track tr;
1403 struct ica_rsa_modexpo_crt crt;
1404 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1406 memset(&tr, 0, sizeof(tr));
1407 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1410 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1413 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1414 /* on failure: retry once again after a requested rescan */
1415 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1417 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1420 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1422 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1425 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1428 static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1431 struct ica_xcRB xcRB;
1432 struct zcrypt_track tr;
1433 struct ica_xcRB __user *uxcRB = (void __user *) arg;
1435 memset(&tr, 0, sizeof(tr));
1436 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1439 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1442 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1443 /* on failure: retry once again after a requested rescan */
1444 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1446 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1449 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1451 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1453 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1458 static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1461 struct ep11_urb xcrb;
1462 struct zcrypt_track tr;
1463 struct ep11_urb __user *uxcrb = (void __user *)arg;
1465 memset(&tr, 0, sizeof(tr));
1466 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1469 rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1472 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1473 /* on failure: retry once again after a requested rescan */
1474 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1476 rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1479 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1481 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1482 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1487 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1491 struct ap_perms *perms =
1492 (struct ap_perms *) filp->private_data;
1494 rc = zcrypt_check_ioctl(perms, cmd);
1500 return icarsamodexpo_ioctl(perms, arg);
1502 return icarsacrt_ioctl(perms, arg);
1504 return zsecsendcprb_ioctl(perms, arg);
1506 return zsendep11cprb_ioctl(perms, arg);
1507 case ZCRYPT_DEVICE_STATUS: {
1508 struct zcrypt_device_status_ext *device_status;
1509 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1510 * sizeof(struct zcrypt_device_status_ext);
1512 device_status = kzalloc(total_size, GFP_KERNEL);
1515 zcrypt_device_status_mask_ext(device_status);
1516 if (copy_to_user((char __user *) arg, device_status,
1519 kfree(device_status);
1522 case ZCRYPT_STATUS_MASK: {
1523 char status[AP_DEVICES];
1525 zcrypt_status_mask(status, AP_DEVICES);
1526 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1530 case ZCRYPT_QDEPTH_MASK: {
1531 char qdepth[AP_DEVICES];
1533 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1534 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1538 case ZCRYPT_PERDEV_REQCNT: {
1541 reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1544 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1545 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1550 case Z90STAT_REQUESTQ_COUNT:
1551 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1552 case Z90STAT_PENDINGQ_COUNT:
1553 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1554 case Z90STAT_TOTALOPEN_COUNT:
1555 return put_user(atomic_read(&zcrypt_open_count),
1556 (int __user *) arg);
1557 case Z90STAT_DOMAIN_INDEX:
1558 return put_user(ap_domain_index, (int __user *) arg);
1562 case ZDEVICESTATUS: {
1563 /* the old ioctl supports only 64 adapters */
1564 struct zcrypt_device_status *device_status;
1565 size_t total_size = MAX_ZDEV_ENTRIES
1566 * sizeof(struct zcrypt_device_status);
1568 device_status = kzalloc(total_size, GFP_KERNEL);
1571 zcrypt_device_status_mask(device_status);
1572 if (copy_to_user((char __user *) arg, device_status,
1575 kfree(device_status);
1578 case Z90STAT_STATUS_MASK: {
1579 /* the old ioctl supports only 64 adapters */
1580 char status[MAX_ZDEV_CARDIDS];
1582 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1583 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1587 case Z90STAT_QDEPTH_MASK: {
1588 /* the old ioctl supports only 64 adapters */
1589 char qdepth[MAX_ZDEV_CARDIDS];
1591 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1592 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1596 case Z90STAT_PERDEV_REQCNT: {
1597 /* the old ioctl supports only 64 adapters */
1598 u32 reqcnt[MAX_ZDEV_CARDIDS];
1600 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1601 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1605 /* unknown ioctl number */
1607 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1608 return -ENOIOCTLCMD;
1612 #ifdef CONFIG_COMPAT
1614 * ioctl32 conversion routines
1616 struct compat_ica_rsa_modexpo {
1617 compat_uptr_t inputdata;
1618 unsigned int inputdatalength;
1619 compat_uptr_t outputdata;
1620 unsigned int outputdatalength;
1621 compat_uptr_t b_key;
1622 compat_uptr_t n_modulus;
1625 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1626 unsigned int cmd, unsigned long arg)
1628 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1629 struct compat_ica_rsa_modexpo mex32;
1630 struct ica_rsa_modexpo mex64;
1631 struct zcrypt_track tr;
1634 memset(&tr, 0, sizeof(tr));
1635 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1637 mex64.inputdata = compat_ptr(mex32.inputdata);
1638 mex64.inputdatalength = mex32.inputdatalength;
1639 mex64.outputdata = compat_ptr(mex32.outputdata);
1640 mex64.outputdatalength = mex32.outputdatalength;
1641 mex64.b_key = compat_ptr(mex32.b_key);
1642 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1644 rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1647 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1648 /* on failure: retry once again after a requested rescan */
1649 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1651 rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1654 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1657 return put_user(mex64.outputdatalength,
1658 &umex32->outputdatalength);
1661 struct compat_ica_rsa_modexpo_crt {
1662 compat_uptr_t inputdata;
1663 unsigned int inputdatalength;
1664 compat_uptr_t outputdata;
1665 unsigned int outputdatalength;
1666 compat_uptr_t bp_key;
1667 compat_uptr_t bq_key;
1668 compat_uptr_t np_prime;
1669 compat_uptr_t nq_prime;
1670 compat_uptr_t u_mult_inv;
1673 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1674 unsigned int cmd, unsigned long arg)
1676 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1677 struct compat_ica_rsa_modexpo_crt crt32;
1678 struct ica_rsa_modexpo_crt crt64;
1679 struct zcrypt_track tr;
1682 memset(&tr, 0, sizeof(tr));
1683 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1685 crt64.inputdata = compat_ptr(crt32.inputdata);
1686 crt64.inputdatalength = crt32.inputdatalength;
1687 crt64.outputdata = compat_ptr(crt32.outputdata);
1688 crt64.outputdatalength = crt32.outputdatalength;
1689 crt64.bp_key = compat_ptr(crt32.bp_key);
1690 crt64.bq_key = compat_ptr(crt32.bq_key);
1691 crt64.np_prime = compat_ptr(crt32.np_prime);
1692 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1693 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1695 rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1698 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1699 /* on failure: retry once again after a requested rescan */
1700 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1702 rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1705 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1708 return put_user(crt64.outputdatalength,
1709 &ucrt32->outputdatalength);
1712 struct compat_ica_xcRB {
1713 unsigned short agent_ID;
1714 unsigned int user_defined;
1715 unsigned short request_ID;
1716 unsigned int request_control_blk_length;
1717 unsigned char padding1[16 - sizeof(compat_uptr_t)];
1718 compat_uptr_t request_control_blk_addr;
1719 unsigned int request_data_length;
1720 char padding2[16 - sizeof(compat_uptr_t)];
1721 compat_uptr_t request_data_address;
1722 unsigned int reply_control_blk_length;
1723 char padding3[16 - sizeof(compat_uptr_t)];
1724 compat_uptr_t reply_control_blk_addr;
1725 unsigned int reply_data_length;
1726 char padding4[16 - sizeof(compat_uptr_t)];
1727 compat_uptr_t reply_data_addr;
1728 unsigned short priority_window;
1729 unsigned int status;
1732 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1733 unsigned int cmd, unsigned long arg)
1735 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1736 struct compat_ica_xcRB xcRB32;
1737 struct zcrypt_track tr;
1738 struct ica_xcRB xcRB64;
1741 memset(&tr, 0, sizeof(tr));
1742 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1744 xcRB64.agent_ID = xcRB32.agent_ID;
1745 xcRB64.user_defined = xcRB32.user_defined;
1746 xcRB64.request_ID = xcRB32.request_ID;
1747 xcRB64.request_control_blk_length =
1748 xcRB32.request_control_blk_length;
1749 xcRB64.request_control_blk_addr =
1750 compat_ptr(xcRB32.request_control_blk_addr);
1751 xcRB64.request_data_length =
1752 xcRB32.request_data_length;
1753 xcRB64.request_data_address =
1754 compat_ptr(xcRB32.request_data_address);
1755 xcRB64.reply_control_blk_length =
1756 xcRB32.reply_control_blk_length;
1757 xcRB64.reply_control_blk_addr =
1758 compat_ptr(xcRB32.reply_control_blk_addr);
1759 xcRB64.reply_data_length = xcRB32.reply_data_length;
1760 xcRB64.reply_data_addr =
1761 compat_ptr(xcRB32.reply_data_addr);
1762 xcRB64.priority_window = xcRB32.priority_window;
1763 xcRB64.status = xcRB32.status;
1765 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1768 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1769 /* on failure: retry once again after a requested rescan */
1770 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1772 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1775 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1776 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1777 xcRB32.reply_data_length = xcRB64.reply_data_length;
1778 xcRB32.status = xcRB64.status;
1779 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1784 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1788 struct ap_perms *perms =
1789 (struct ap_perms *) filp->private_data;
1791 rc = zcrypt_check_ioctl(perms, cmd);
1795 if (cmd == ICARSAMODEXPO)
1796 return trans_modexpo32(perms, filp, cmd, arg);
1797 if (cmd == ICARSACRT)
1798 return trans_modexpo_crt32(perms, filp, cmd, arg);
1799 if (cmd == ZSECSENDCPRB)
1800 return trans_xcRB32(perms, filp, cmd, arg);
1801 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1806 * Misc device file operations.
1808 static const struct file_operations zcrypt_fops = {
1809 .owner = THIS_MODULE,
1810 .read = zcrypt_read,
1811 .write = zcrypt_write,
1812 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1813 #ifdef CONFIG_COMPAT
1814 .compat_ioctl = zcrypt_compat_ioctl,
1816 .open = zcrypt_open,
1817 .release = zcrypt_release,
1818 .llseek = no_llseek,
1824 static struct miscdevice zcrypt_misc_device = {
1825 .minor = MISC_DYNAMIC_MINOR,
1827 .fops = &zcrypt_fops,
1830 static int zcrypt_rng_device_count;
1831 static u32 *zcrypt_rng_buffer;
1832 static int zcrypt_rng_buffer_index;
1833 static DEFINE_MUTEX(zcrypt_rng_mutex);
1835 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1840 * We don't need locking here because the RNG API guarantees serialized
1841 * read method calls.
1843 if (zcrypt_rng_buffer_index == 0) {
1844 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1845 /* on failure: retry once again after a requested rescan */
1846 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1847 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1850 zcrypt_rng_buffer_index = rc / sizeof(*data);
1852 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1853 return sizeof(*data);
1856 static struct hwrng zcrypt_rng_dev = {
1858 .data_read = zcrypt_rng_data_read,
1862 int zcrypt_rng_device_add(void)
1866 mutex_lock(&zcrypt_rng_mutex);
1867 if (zcrypt_rng_device_count == 0) {
1868 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1869 if (!zcrypt_rng_buffer) {
1873 zcrypt_rng_buffer_index = 0;
1874 if (!zcrypt_hwrng_seed)
1875 zcrypt_rng_dev.quality = 0;
1876 rc = hwrng_register(&zcrypt_rng_dev);
1879 zcrypt_rng_device_count = 1;
1881 zcrypt_rng_device_count++;
1882 mutex_unlock(&zcrypt_rng_mutex);
1886 free_page((unsigned long) zcrypt_rng_buffer);
1888 mutex_unlock(&zcrypt_rng_mutex);
1892 void zcrypt_rng_device_remove(void)
1894 mutex_lock(&zcrypt_rng_mutex);
1895 zcrypt_rng_device_count--;
1896 if (zcrypt_rng_device_count == 0) {
1897 hwrng_unregister(&zcrypt_rng_dev);
1898 free_page((unsigned long) zcrypt_rng_buffer);
1900 mutex_unlock(&zcrypt_rng_mutex);
1903 int __init zcrypt_debug_init(void)
1905 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1906 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1907 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1908 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1913 void zcrypt_debug_exit(void)
1915 debug_unregister(zcrypt_dbf_info);
1918 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1920 static int __init zcdn_init(void)
1924 /* create a new class 'zcrypt' */
1925 zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1926 if (IS_ERR(zcrypt_class)) {
1927 rc = PTR_ERR(zcrypt_class);
1928 goto out_class_create_failed;
1930 zcrypt_class->dev_release = zcdn_device_release;
1932 /* alloc device minor range */
1933 rc = alloc_chrdev_region(&zcrypt_devt,
1934 0, ZCRYPT_MAX_MINOR_NODES,
1937 goto out_alloc_chrdev_failed;
1939 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1940 zcrypt_cdev.owner = THIS_MODULE;
1941 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1943 goto out_cdev_add_failed;
1945 /* need some class specific sysfs attributes */
1946 rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1948 goto out_class_create_file_1_failed;
1949 rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1951 goto out_class_create_file_2_failed;
1955 out_class_create_file_2_failed:
1956 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1957 out_class_create_file_1_failed:
1958 cdev_del(&zcrypt_cdev);
1959 out_cdev_add_failed:
1960 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1961 out_alloc_chrdev_failed:
1962 class_destroy(zcrypt_class);
1963 out_class_create_failed:
1967 static void zcdn_exit(void)
1969 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1970 class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1972 cdev_del(&zcrypt_cdev);
1973 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1974 class_destroy(zcrypt_class);
1980 * zcrypt_api_init(): Module initialization.
1982 * The module initialization code.
1984 int __init zcrypt_api_init(void)
1988 rc = zcrypt_debug_init();
1992 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1998 /* Register the request sprayer. */
1999 rc = misc_register(&zcrypt_misc_device);
2001 goto out_misc_register_failed;
2003 zcrypt_msgtype6_init();
2004 zcrypt_msgtype50_init();
2008 out_misc_register_failed:
2009 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2012 zcrypt_debug_exit();
2018 * zcrypt_api_exit(): Module termination.
2020 * The module termination code.
2022 void __exit zcrypt_api_exit(void)
2024 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2027 misc_deregister(&zcrypt_misc_device);
2028 zcrypt_msgtype6_exit();
2029 zcrypt_msgtype50_exit();
2030 zcrypt_ccamisc_exit();
2031 zcrypt_ep11misc_exit();
2032 zcrypt_debug_exit();
2035 module_init(zcrypt_api_init);
2036 module_exit(zcrypt_api_exit);