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"
43 MODULE_AUTHOR("IBM Corporation");
44 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
45 "Copyright IBM Corp. 2001, 2012");
46 MODULE_LICENSE("GPL");
49 * zcrypt tracepoint functions
51 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
52 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
54 static int zcrypt_hwrng_seed = 1;
55 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
56 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
58 DEFINE_SPINLOCK(zcrypt_list_lock);
59 LIST_HEAD(zcrypt_card_list);
60 int zcrypt_device_count;
62 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
63 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
65 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
66 EXPORT_SYMBOL(zcrypt_rescan_req);
68 static LIST_HEAD(zcrypt_ops_list);
70 /* Zcrypt related debug feature stuff. */
71 debug_info_t *zcrypt_dbf_info;
74 * Process a rescan of the transport layer.
76 * Returns 1, if the rescan has been processed, otherwise 0.
78 static inline int zcrypt_process_rescan(void)
80 if (atomic_read(&zcrypt_rescan_req)) {
81 atomic_set(&zcrypt_rescan_req, 0);
82 atomic_inc(&zcrypt_rescan_count);
83 ap_bus_force_rescan();
84 ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
85 atomic_inc_return(&zcrypt_rescan_count));
91 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
93 list_add_tail(&zops->list, &zcrypt_ops_list);
96 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
98 list_del_init(&zops->list);
101 struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
103 struct zcrypt_ops *zops;
105 list_for_each_entry(zops, &zcrypt_ops_list, list)
106 if ((zops->variant == variant) &&
107 (!strncmp(zops->name, name, sizeof(zops->name))))
111 EXPORT_SYMBOL(zcrypt_msgtype);
114 * Multi device nodes extension functions.
117 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
121 static struct class *zcrypt_class;
122 static dev_t zcrypt_devt;
123 static struct cdev zcrypt_cdev;
126 struct device device;
127 struct ap_perms perms;
130 #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
132 #define ZCDN_MAX_NAME 32
134 static int zcdn_create(const char *name);
135 static int zcdn_destroy(const char *name);
137 /* helper function, matches the name for find_zcdndev_by_name() */
138 static int __match_zcdn_name(struct device *dev, const void *data)
140 return strcmp(dev_name(dev), (const char *)data) == 0;
143 /* helper function, matches the devt value for find_zcdndev_by_devt() */
144 static int __match_zcdn_devt(struct device *dev, const void *data)
146 return dev->devt == *((dev_t *) data);
150 * Find zcdn device by name.
151 * Returns reference to the zcdn device which needs to be released
152 * with put_device() after use.
154 static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
157 class_find_device(zcrypt_class, NULL,
161 return dev ? to_zcdn_dev(dev) : NULL;
165 * Find zcdn device by devt value.
166 * Returns reference to the zcdn device which needs to be released
167 * with put_device() after use.
169 static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
172 class_find_device(zcrypt_class, NULL,
176 return dev ? to_zcdn_dev(dev) : NULL;
179 static ssize_t ioctlmask_show(struct device *dev,
180 struct device_attribute *attr,
184 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
186 if (mutex_lock_interruptible(&ap_perms_mutex))
191 for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
192 snprintf(buf + 2 + 2 * i * sizeof(long),
193 PAGE_SIZE - 2 - 2 * i * sizeof(long),
194 "%016lx", zcdndev->perms.ioctlm[i]);
195 buf[2 + 2 * i * sizeof(long)] = '\n';
196 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
197 rc = 2 + 2 * i * sizeof(long) + 1;
199 mutex_unlock(&ap_perms_mutex);
204 static ssize_t ioctlmask_store(struct device *dev,
205 struct device_attribute *attr,
206 const char *buf, size_t count)
209 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
211 rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
212 AP_IOCTLS, &ap_perms_mutex);
219 static DEVICE_ATTR_RW(ioctlmask);
221 static ssize_t apmask_show(struct device *dev,
222 struct device_attribute *attr,
226 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
228 if (mutex_lock_interruptible(&ap_perms_mutex))
233 for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
234 snprintf(buf + 2 + 2 * i * sizeof(long),
235 PAGE_SIZE - 2 - 2 * i * sizeof(long),
236 "%016lx", zcdndev->perms.apm[i]);
237 buf[2 + 2 * i * sizeof(long)] = '\n';
238 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
239 rc = 2 + 2 * i * sizeof(long) + 1;
241 mutex_unlock(&ap_perms_mutex);
246 static ssize_t apmask_store(struct device *dev,
247 struct device_attribute *attr,
248 const char *buf, size_t count)
251 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
253 rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
254 AP_DEVICES, &ap_perms_mutex);
261 static DEVICE_ATTR_RW(apmask);
263 static ssize_t aqmask_show(struct device *dev,
264 struct device_attribute *attr,
268 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
270 if (mutex_lock_interruptible(&ap_perms_mutex))
275 for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
276 snprintf(buf + 2 + 2 * i * sizeof(long),
277 PAGE_SIZE - 2 - 2 * i * sizeof(long),
278 "%016lx", zcdndev->perms.aqm[i]);
279 buf[2 + 2 * i * sizeof(long)] = '\n';
280 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
281 rc = 2 + 2 * i * sizeof(long) + 1;
283 mutex_unlock(&ap_perms_mutex);
288 static ssize_t aqmask_store(struct device *dev,
289 struct device_attribute *attr,
290 const char *buf, size_t count)
293 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
295 rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
296 AP_DOMAINS, &ap_perms_mutex);
303 static DEVICE_ATTR_RW(aqmask);
305 static struct attribute *zcdn_dev_attrs[] = {
306 &dev_attr_ioctlmask.attr,
307 &dev_attr_apmask.attr,
308 &dev_attr_aqmask.attr,
312 static struct attribute_group zcdn_dev_attr_group = {
313 .attrs = zcdn_dev_attrs
316 static const struct attribute_group *zcdn_dev_attr_groups[] = {
317 &zcdn_dev_attr_group,
321 static ssize_t zcdn_create_store(struct class *class,
322 struct class_attribute *attr,
323 const char *buf, size_t count)
326 char name[ZCDN_MAX_NAME];
328 strncpy(name, skip_spaces(buf), sizeof(name));
329 name[sizeof(name) - 1] = '\0';
331 rc = zcdn_create(strim(name));
333 return rc ? rc : count;
336 static const struct class_attribute class_attr_zcdn_create =
337 __ATTR(create, 0600, NULL, zcdn_create_store);
339 static ssize_t zcdn_destroy_store(struct class *class,
340 struct class_attribute *attr,
341 const char *buf, size_t count)
344 char name[ZCDN_MAX_NAME];
346 strncpy(name, skip_spaces(buf), sizeof(name));
347 name[sizeof(name) - 1] = '\0';
349 rc = zcdn_destroy(strim(name));
351 return rc ? rc : count;
354 static const struct class_attribute class_attr_zcdn_destroy =
355 __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
357 static void zcdn_device_release(struct device *dev)
359 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
361 ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
362 MAJOR(dev->devt), MINOR(dev->devt));
367 static int zcdn_create(const char *name)
371 char nodename[ZCDN_MAX_NAME];
372 struct zcdn_device *zcdndev;
374 if (mutex_lock_interruptible(&ap_perms_mutex))
377 /* check if device node with this name already exists */
379 zcdndev = find_zcdndev_by_name(name);
381 put_device(&zcdndev->device);
387 /* find an unused minor number */
388 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
389 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
390 zcdndev = find_zcdndev_by_devt(devt);
392 put_device(&zcdndev->device);
396 if (i == ZCRYPT_MAX_MINOR_NODES) {
401 /* alloc and prepare a new zcdn device */
402 zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
407 zcdndev->device.release = zcdn_device_release;
408 zcdndev->device.class = zcrypt_class;
409 zcdndev->device.devt = devt;
410 zcdndev->device.groups = zcdn_dev_attr_groups;
412 strncpy(nodename, name, sizeof(nodename));
414 snprintf(nodename, sizeof(nodename),
415 ZCRYPT_NAME "_%d", (int) MINOR(devt));
416 nodename[sizeof(nodename)-1] = '\0';
417 if (dev_set_name(&zcdndev->device, nodename)) {
421 rc = device_register(&zcdndev->device);
423 put_device(&zcdndev->device);
427 ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
428 MAJOR(devt), MINOR(devt));
431 mutex_unlock(&ap_perms_mutex);
435 static int zcdn_destroy(const char *name)
438 struct zcdn_device *zcdndev;
440 if (mutex_lock_interruptible(&ap_perms_mutex))
443 /* try to find this zcdn device */
444 zcdndev = find_zcdndev_by_name(name);
451 * The zcdn device is not hard destroyed. It is subject to
452 * reference counting and thus just needs to be unregistered.
454 put_device(&zcdndev->device);
455 device_unregister(&zcdndev->device);
458 mutex_unlock(&ap_perms_mutex);
462 static void zcdn_destroy_all(void)
466 struct zcdn_device *zcdndev;
468 mutex_lock(&ap_perms_mutex);
469 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
470 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
471 zcdndev = find_zcdndev_by_devt(devt);
473 put_device(&zcdndev->device);
474 device_unregister(&zcdndev->device);
477 mutex_unlock(&ap_perms_mutex);
483 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
485 * This function is not supported beyond zcrypt 1.3.1.
487 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
488 size_t count, loff_t *f_pos)
494 * zcrypt_write(): Not allowed.
496 * Write is is not allowed
498 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
499 size_t count, loff_t *f_pos)
505 * zcrypt_open(): Count number of users.
507 * Device open function to count number of users.
509 static int zcrypt_open(struct inode *inode, struct file *filp)
511 struct ap_perms *perms = &ap_perms;
513 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
514 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
515 struct zcdn_device *zcdndev;
517 if (mutex_lock_interruptible(&ap_perms_mutex))
519 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
520 /* find returns a reference, no get_device() needed */
521 mutex_unlock(&ap_perms_mutex);
523 perms = &zcdndev->perms;
526 filp->private_data = (void *) perms;
528 atomic_inc(&zcrypt_open_count);
529 return stream_open(inode, filp);
533 * zcrypt_release(): Count number of users.
535 * Device close function to count number of users.
537 static int zcrypt_release(struct inode *inode, struct file *filp)
539 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
540 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
541 struct zcdn_device *zcdndev;
543 if (mutex_lock_interruptible(&ap_perms_mutex))
545 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
546 mutex_unlock(&ap_perms_mutex);
548 /* 2 puts here: one for find, one for open */
549 put_device(&zcdndev->device);
550 put_device(&zcdndev->device);
555 atomic_dec(&zcrypt_open_count);
559 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
563 int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
565 if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
566 if (test_bit_inv(ioctlnr, perms->ioctlm))
572 "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
578 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
580 return test_bit_inv(card, perms->apm) ? true : false;
583 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
585 return test_bit_inv(queue, perms->aqm) ? true : false;
588 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
589 struct zcrypt_queue *zq,
590 struct module **pmod,
593 if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
595 zcrypt_queue_get(zq);
596 get_device(&zq->queue->ap_dev.device);
597 atomic_add(weight, &zc->load);
598 atomic_add(weight, &zq->load);
600 *pmod = zq->queue->ap_dev.drv->driver.owner;
604 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
605 struct zcrypt_queue *zq,
610 atomic_sub(weight, &zc->load);
611 atomic_sub(weight, &zq->load);
612 put_device(&zq->queue->ap_dev.device);
613 zcrypt_queue_put(zq);
617 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
618 struct zcrypt_card *pref_zc,
620 unsigned int pref_weight)
624 weight += atomic_read(&zc->load);
625 pref_weight += atomic_read(&pref_zc->load);
626 if (weight == pref_weight)
627 return atomic_read(&zc->card->total_request_count) >
628 atomic_read(&pref_zc->card->total_request_count);
629 return weight > pref_weight;
632 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
633 struct zcrypt_queue *pref_zq,
635 unsigned int pref_weight)
639 weight += atomic_read(&zq->load);
640 pref_weight += atomic_read(&pref_zq->load);
641 if (weight == pref_weight)
642 return zq->queue->total_request_count >
643 pref_zq->queue->total_request_count;
644 return weight > pref_weight;
650 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
651 struct ica_rsa_modexpo *mex)
653 struct zcrypt_card *zc, *pref_zc;
654 struct zcrypt_queue *zq, *pref_zq;
655 unsigned int weight, pref_weight;
656 unsigned int func_code;
657 int qid = 0, rc = -ENODEV;
660 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
662 if (mex->outputdatalength < mex->inputdatalength) {
669 * As long as outputdatalength is big enough, we can set the
670 * outputdatalength equal to the inputdatalength, since that is the
671 * number of bytes we will copy in any case
673 mex->outputdatalength = mex->inputdatalength;
675 rc = get_rsa_modex_fc(mex, &func_code);
681 spin_lock(&zcrypt_list_lock);
682 for_each_zcrypt_card(zc) {
683 /* Check for online accelarator and CCA cards */
684 if (!zc->online || !(zc->card->functions & 0x18000000))
686 /* Check for size limits */
687 if (zc->min_mod_size > mex->inputdatalength ||
688 zc->max_mod_size < mex->inputdatalength)
690 /* check if device node has admission for this card */
691 if (!zcrypt_check_card(perms, zc->card->id))
693 /* get weight index of the card device */
694 weight = zc->speed_rating[func_code];
695 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
697 for_each_zcrypt_queue(zq, zc) {
698 /* check if device is online and eligible */
699 if (!zq->online || !zq->ops->rsa_modexpo)
701 /* check if device node has admission for this queue */
702 if (!zcrypt_check_queue(perms,
703 AP_QID_QUEUE(zq->queue->qid)))
705 if (zcrypt_queue_compare(zq, pref_zq,
706 weight, pref_weight))
710 pref_weight = weight;
713 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
714 spin_unlock(&zcrypt_list_lock);
721 qid = pref_zq->queue->qid;
722 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
724 spin_lock(&zcrypt_list_lock);
725 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
726 spin_unlock(&zcrypt_list_lock);
729 trace_s390_zcrypt_rep(mex, func_code, rc,
730 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
734 static long zcrypt_rsa_crt(struct ap_perms *perms,
735 struct ica_rsa_modexpo_crt *crt)
737 struct zcrypt_card *zc, *pref_zc;
738 struct zcrypt_queue *zq, *pref_zq;
739 unsigned int weight, pref_weight;
740 unsigned int func_code;
741 int qid = 0, rc = -ENODEV;
744 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
746 if (crt->outputdatalength < crt->inputdatalength) {
753 * As long as outputdatalength is big enough, we can set the
754 * outputdatalength equal to the inputdatalength, since that is the
755 * number of bytes we will copy in any case
757 crt->outputdatalength = crt->inputdatalength;
759 rc = get_rsa_crt_fc(crt, &func_code);
765 spin_lock(&zcrypt_list_lock);
766 for_each_zcrypt_card(zc) {
767 /* Check for online accelarator and CCA cards */
768 if (!zc->online || !(zc->card->functions & 0x18000000))
770 /* Check for size limits */
771 if (zc->min_mod_size > crt->inputdatalength ||
772 zc->max_mod_size < crt->inputdatalength)
774 /* check if device node has admission for this card */
775 if (!zcrypt_check_card(perms, zc->card->id))
777 /* get weight index of the card device */
778 weight = zc->speed_rating[func_code];
779 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
781 for_each_zcrypt_queue(zq, zc) {
782 /* check if device is online and eligible */
783 if (!zq->online || !zq->ops->rsa_modexpo_crt)
785 /* check if device node has admission for this queue */
786 if (!zcrypt_check_queue(perms,
787 AP_QID_QUEUE(zq->queue->qid)))
789 if (zcrypt_queue_compare(zq, pref_zq,
790 weight, pref_weight))
794 pref_weight = weight;
797 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
798 spin_unlock(&zcrypt_list_lock);
805 qid = pref_zq->queue->qid;
806 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
808 spin_lock(&zcrypt_list_lock);
809 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
810 spin_unlock(&zcrypt_list_lock);
813 trace_s390_zcrypt_rep(crt, func_code, rc,
814 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
818 static long _zcrypt_send_cprb(struct ap_perms *perms,
819 struct ica_xcRB *xcRB)
821 struct zcrypt_card *zc, *pref_zc;
822 struct zcrypt_queue *zq, *pref_zq;
823 struct ap_message ap_msg;
824 unsigned int weight, pref_weight;
825 unsigned int func_code;
826 unsigned short *domain, tdom;
827 int qid = 0, rc = -ENODEV;
830 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
833 ap_init_message(&ap_msg);
834 rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
839 * If a valid target domain is set and this domain is NOT a usage
840 * domain but a control only domain, use the default domain as target.
843 if (tdom >= 0 && tdom < AP_DOMAINS &&
844 !ap_test_config_usage_domain(tdom) &&
845 ap_test_config_ctrl_domain(tdom) &&
846 ap_domain_index >= 0)
847 tdom = ap_domain_index;
851 spin_lock(&zcrypt_list_lock);
852 for_each_zcrypt_card(zc) {
853 /* Check for online CCA cards */
854 if (!zc->online || !(zc->card->functions & 0x10000000))
856 /* Check for user selected CCA card */
857 if (xcRB->user_defined != AUTOSELECT &&
858 xcRB->user_defined != zc->card->id)
860 /* check if device node has admission for this card */
861 if (!zcrypt_check_card(perms, zc->card->id))
863 /* get weight index of the card device */
864 weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
865 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
867 for_each_zcrypt_queue(zq, zc) {
868 /* check if device is online and eligible */
870 !zq->ops->send_cprb ||
871 (tdom != (unsigned short) AUTOSELECT &&
872 tdom != AP_QID_QUEUE(zq->queue->qid)))
874 /* check if device node has admission for this queue */
875 if (!zcrypt_check_queue(perms,
876 AP_QID_QUEUE(zq->queue->qid)))
878 if (zcrypt_queue_compare(zq, pref_zq,
879 weight, pref_weight))
883 pref_weight = weight;
886 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
887 spin_unlock(&zcrypt_list_lock);
894 /* in case of auto select, provide the correct domain */
895 qid = pref_zq->queue->qid;
896 if (*domain == (unsigned short) AUTOSELECT)
897 *domain = AP_QID_QUEUE(qid);
899 rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
901 spin_lock(&zcrypt_list_lock);
902 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
903 spin_unlock(&zcrypt_list_lock);
906 ap_release_message(&ap_msg);
907 trace_s390_zcrypt_rep(xcRB, func_code, rc,
908 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
912 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
914 return _zcrypt_send_cprb(&ap_perms, xcRB);
916 EXPORT_SYMBOL(zcrypt_send_cprb);
918 static bool is_desired_ep11_card(unsigned int dev_id,
919 unsigned short target_num,
920 struct ep11_target_dev *targets)
922 while (target_num-- > 0) {
923 if (dev_id == targets->ap_id)
930 static bool is_desired_ep11_queue(unsigned int dev_qid,
931 unsigned short target_num,
932 struct ep11_target_dev *targets)
934 while (target_num-- > 0) {
935 if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
942 static long zcrypt_send_ep11_cprb(struct ap_perms *perms,
943 struct ep11_urb *xcrb)
945 struct zcrypt_card *zc, *pref_zc;
946 struct zcrypt_queue *zq, *pref_zq;
947 struct ep11_target_dev *targets;
948 unsigned short target_num;
949 unsigned int weight, pref_weight;
950 unsigned int func_code;
951 struct ap_message ap_msg;
952 int qid = 0, rc = -ENODEV;
955 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
957 ap_init_message(&ap_msg);
959 target_num = (unsigned short) xcrb->targets_num;
961 /* empty list indicates autoselect (all available targets) */
963 if (target_num != 0) {
964 struct ep11_target_dev __user *uptr;
966 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
973 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
974 if (copy_from_user(targets, uptr,
975 target_num * sizeof(*targets))) {
982 rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
988 spin_lock(&zcrypt_list_lock);
989 for_each_zcrypt_card(zc) {
990 /* Check for online EP11 cards */
991 if (!zc->online || !(zc->card->functions & 0x04000000))
993 /* Check for user selected EP11 card */
995 !is_desired_ep11_card(zc->card->id, target_num, targets))
997 /* check if device node has admission for this card */
998 if (!zcrypt_check_card(perms, zc->card->id))
1000 /* get weight index of the card device */
1001 weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1002 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1004 for_each_zcrypt_queue(zq, zc) {
1005 /* check if device is online and eligible */
1007 !zq->ops->send_ep11_cprb ||
1009 !is_desired_ep11_queue(zq->queue->qid,
1010 target_num, targets)))
1012 /* check if device node has admission for this queue */
1013 if (!zcrypt_check_queue(perms,
1014 AP_QID_QUEUE(zq->queue->qid)))
1016 if (zcrypt_queue_compare(zq, pref_zq,
1017 weight, pref_weight))
1021 pref_weight = weight;
1024 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1025 spin_unlock(&zcrypt_list_lock);
1032 qid = pref_zq->queue->qid;
1033 rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
1035 spin_lock(&zcrypt_list_lock);
1036 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1037 spin_unlock(&zcrypt_list_lock);
1042 ap_release_message(&ap_msg);
1043 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1044 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1048 static long zcrypt_rng(char *buffer)
1050 struct zcrypt_card *zc, *pref_zc;
1051 struct zcrypt_queue *zq, *pref_zq;
1052 unsigned int weight, pref_weight;
1053 unsigned int func_code;
1054 struct ap_message ap_msg;
1055 unsigned int domain;
1056 int qid = 0, rc = -ENODEV;
1059 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1061 ap_init_message(&ap_msg);
1062 rc = get_rng_fc(&ap_msg, &func_code, &domain);
1068 spin_lock(&zcrypt_list_lock);
1069 for_each_zcrypt_card(zc) {
1070 /* Check for online CCA cards */
1071 if (!zc->online || !(zc->card->functions & 0x10000000))
1073 /* get weight index of the card device */
1074 weight = zc->speed_rating[func_code];
1075 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1077 for_each_zcrypt_queue(zq, zc) {
1078 /* check if device is online and eligible */
1079 if (!zq->online || !zq->ops->rng)
1081 if (zcrypt_queue_compare(zq, pref_zq,
1082 weight, pref_weight))
1086 pref_weight = weight;
1089 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1090 spin_unlock(&zcrypt_list_lock);
1097 qid = pref_zq->queue->qid;
1098 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1100 spin_lock(&zcrypt_list_lock);
1101 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1102 spin_unlock(&zcrypt_list_lock);
1105 ap_release_message(&ap_msg);
1106 trace_s390_zcrypt_rep(buffer, func_code, rc,
1107 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1111 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1113 struct zcrypt_card *zc;
1114 struct zcrypt_queue *zq;
1115 struct zcrypt_device_status *stat;
1118 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1119 * sizeof(struct zcrypt_device_status));
1121 spin_lock(&zcrypt_list_lock);
1122 for_each_zcrypt_card(zc) {
1123 for_each_zcrypt_queue(zq, zc) {
1124 card = AP_QID_CARD(zq->queue->qid);
1125 if (card >= MAX_ZDEV_CARDIDS)
1127 queue = AP_QID_QUEUE(zq->queue->qid);
1128 stat = &devstatus[card * AP_DOMAINS + queue];
1129 stat->hwtype = zc->card->ap_dev.device_type;
1130 stat->functions = zc->card->functions >> 26;
1131 stat->qid = zq->queue->qid;
1132 stat->online = zq->online ? 0x01 : 0x00;
1135 spin_unlock(&zcrypt_list_lock);
1138 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1140 struct zcrypt_card *zc;
1141 struct zcrypt_queue *zq;
1142 struct zcrypt_device_status_ext *stat;
1145 memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1146 * sizeof(struct zcrypt_device_status_ext));
1148 spin_lock(&zcrypt_list_lock);
1149 for_each_zcrypt_card(zc) {
1150 for_each_zcrypt_queue(zq, zc) {
1151 card = AP_QID_CARD(zq->queue->qid);
1152 queue = AP_QID_QUEUE(zq->queue->qid);
1153 stat = &devstatus[card * AP_DOMAINS + queue];
1154 stat->hwtype = zc->card->ap_dev.device_type;
1155 stat->functions = zc->card->functions >> 26;
1156 stat->qid = zq->queue->qid;
1157 stat->online = zq->online ? 0x01 : 0x00;
1160 spin_unlock(&zcrypt_list_lock);
1162 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1164 static void zcrypt_status_mask(char status[], size_t max_adapters)
1166 struct zcrypt_card *zc;
1167 struct zcrypt_queue *zq;
1170 memset(status, 0, max_adapters);
1171 spin_lock(&zcrypt_list_lock);
1172 for_each_zcrypt_card(zc) {
1173 for_each_zcrypt_queue(zq, zc) {
1174 card = AP_QID_CARD(zq->queue->qid);
1175 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1176 || card >= max_adapters)
1178 status[card] = zc->online ? zc->user_space_type : 0x0d;
1181 spin_unlock(&zcrypt_list_lock);
1184 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1186 struct zcrypt_card *zc;
1187 struct zcrypt_queue *zq;
1190 memset(qdepth, 0, max_adapters);
1191 spin_lock(&zcrypt_list_lock);
1193 for_each_zcrypt_card(zc) {
1194 for_each_zcrypt_queue(zq, zc) {
1195 card = AP_QID_CARD(zq->queue->qid);
1196 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1197 || card >= max_adapters)
1199 spin_lock(&zq->queue->lock);
1201 zq->queue->pendingq_count +
1202 zq->queue->requestq_count;
1203 spin_unlock(&zq->queue->lock);
1207 spin_unlock(&zcrypt_list_lock);
1210 static void zcrypt_perdev_reqcnt(int reqcnt[], size_t max_adapters)
1212 struct zcrypt_card *zc;
1213 struct zcrypt_queue *zq;
1216 memset(reqcnt, 0, sizeof(int) * max_adapters);
1217 spin_lock(&zcrypt_list_lock);
1219 for_each_zcrypt_card(zc) {
1220 for_each_zcrypt_queue(zq, zc) {
1221 card = AP_QID_CARD(zq->queue->qid);
1222 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1223 || card >= max_adapters)
1225 spin_lock(&zq->queue->lock);
1226 reqcnt[card] = zq->queue->total_request_count;
1227 spin_unlock(&zq->queue->lock);
1231 spin_unlock(&zcrypt_list_lock);
1234 static int zcrypt_pendingq_count(void)
1236 struct zcrypt_card *zc;
1237 struct zcrypt_queue *zq;
1241 spin_lock(&zcrypt_list_lock);
1243 for_each_zcrypt_card(zc) {
1244 for_each_zcrypt_queue(zq, zc) {
1245 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1247 spin_lock(&zq->queue->lock);
1248 pendingq_count += zq->queue->pendingq_count;
1249 spin_unlock(&zq->queue->lock);
1253 spin_unlock(&zcrypt_list_lock);
1254 return pendingq_count;
1257 static int zcrypt_requestq_count(void)
1259 struct zcrypt_card *zc;
1260 struct zcrypt_queue *zq;
1264 spin_lock(&zcrypt_list_lock);
1266 for_each_zcrypt_card(zc) {
1267 for_each_zcrypt_queue(zq, zc) {
1268 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1270 spin_lock(&zq->queue->lock);
1271 requestq_count += zq->queue->requestq_count;
1272 spin_unlock(&zq->queue->lock);
1276 spin_unlock(&zcrypt_list_lock);
1277 return requestq_count;
1280 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1284 struct ap_perms *perms =
1285 (struct ap_perms *) filp->private_data;
1287 rc = zcrypt_check_ioctl(perms, cmd);
1292 case ICARSAMODEXPO: {
1293 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1294 struct ica_rsa_modexpo mex;
1296 if (copy_from_user(&mex, umex, sizeof(mex)))
1299 rc = zcrypt_rsa_modexpo(perms, &mex);
1300 } while (rc == -EAGAIN);
1301 /* on failure: retry once again after a requested rescan */
1302 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1304 rc = zcrypt_rsa_modexpo(perms, &mex);
1305 } while (rc == -EAGAIN);
1307 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1310 return put_user(mex.outputdatalength, &umex->outputdatalength);
1313 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1314 struct ica_rsa_modexpo_crt crt;
1316 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1319 rc = zcrypt_rsa_crt(perms, &crt);
1320 } while (rc == -EAGAIN);
1321 /* on failure: retry once again after a requested rescan */
1322 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1324 rc = zcrypt_rsa_crt(perms, &crt);
1325 } while (rc == -EAGAIN);
1327 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1330 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1332 case ZSECSENDCPRB: {
1333 struct ica_xcRB __user *uxcRB = (void __user *) arg;
1334 struct ica_xcRB xcRB;
1336 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1339 rc = _zcrypt_send_cprb(perms, &xcRB);
1340 } while (rc == -EAGAIN);
1341 /* on failure: retry once again after a requested rescan */
1342 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1344 rc = _zcrypt_send_cprb(perms, &xcRB);
1345 } while (rc == -EAGAIN);
1347 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1349 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1353 case ZSENDEP11CPRB: {
1354 struct ep11_urb __user *uxcrb = (void __user *)arg;
1355 struct ep11_urb xcrb;
1357 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1360 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1361 } while (rc == -EAGAIN);
1362 /* on failure: retry once again after a requested rescan */
1363 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1365 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1366 } while (rc == -EAGAIN);
1368 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1369 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1373 case ZCRYPT_DEVICE_STATUS: {
1374 struct zcrypt_device_status_ext *device_status;
1375 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1376 * sizeof(struct zcrypt_device_status_ext);
1378 device_status = kzalloc(total_size, GFP_KERNEL);
1381 zcrypt_device_status_mask_ext(device_status);
1382 if (copy_to_user((char __user *) arg, device_status,
1385 kfree(device_status);
1388 case ZCRYPT_STATUS_MASK: {
1389 char status[AP_DEVICES];
1391 zcrypt_status_mask(status, AP_DEVICES);
1392 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1396 case ZCRYPT_QDEPTH_MASK: {
1397 char qdepth[AP_DEVICES];
1399 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1400 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1404 case ZCRYPT_PERDEV_REQCNT: {
1407 reqcnt = kcalloc(AP_DEVICES, sizeof(int), GFP_KERNEL);
1410 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1411 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1416 case Z90STAT_REQUESTQ_COUNT:
1417 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1418 case Z90STAT_PENDINGQ_COUNT:
1419 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1420 case Z90STAT_TOTALOPEN_COUNT:
1421 return put_user(atomic_read(&zcrypt_open_count),
1422 (int __user *) arg);
1423 case Z90STAT_DOMAIN_INDEX:
1424 return put_user(ap_domain_index, (int __user *) arg);
1428 case ZDEVICESTATUS: {
1429 /* the old ioctl supports only 64 adapters */
1430 struct zcrypt_device_status *device_status;
1431 size_t total_size = MAX_ZDEV_ENTRIES
1432 * sizeof(struct zcrypt_device_status);
1434 device_status = kzalloc(total_size, GFP_KERNEL);
1437 zcrypt_device_status_mask(device_status);
1438 if (copy_to_user((char __user *) arg, device_status,
1441 kfree(device_status);
1444 case Z90STAT_STATUS_MASK: {
1445 /* the old ioctl supports only 64 adapters */
1446 char status[MAX_ZDEV_CARDIDS];
1448 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1449 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1453 case Z90STAT_QDEPTH_MASK: {
1454 /* the old ioctl supports only 64 adapters */
1455 char qdepth[MAX_ZDEV_CARDIDS];
1457 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1458 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1462 case Z90STAT_PERDEV_REQCNT: {
1463 /* the old ioctl supports only 64 adapters */
1464 int reqcnt[MAX_ZDEV_CARDIDS];
1466 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1467 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1471 /* unknown ioctl number */
1473 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1474 return -ENOIOCTLCMD;
1478 #ifdef CONFIG_COMPAT
1480 * ioctl32 conversion routines
1482 struct compat_ica_rsa_modexpo {
1483 compat_uptr_t inputdata;
1484 unsigned int inputdatalength;
1485 compat_uptr_t outputdata;
1486 unsigned int outputdatalength;
1487 compat_uptr_t b_key;
1488 compat_uptr_t n_modulus;
1491 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1492 unsigned int cmd, unsigned long arg)
1494 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1495 struct compat_ica_rsa_modexpo mex32;
1496 struct ica_rsa_modexpo mex64;
1499 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1501 mex64.inputdata = compat_ptr(mex32.inputdata);
1502 mex64.inputdatalength = mex32.inputdatalength;
1503 mex64.outputdata = compat_ptr(mex32.outputdata);
1504 mex64.outputdatalength = mex32.outputdatalength;
1505 mex64.b_key = compat_ptr(mex32.b_key);
1506 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1508 rc = zcrypt_rsa_modexpo(perms, &mex64);
1509 } while (rc == -EAGAIN);
1510 /* on failure: retry once again after a requested rescan */
1511 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1513 rc = zcrypt_rsa_modexpo(perms, &mex64);
1514 } while (rc == -EAGAIN);
1517 return put_user(mex64.outputdatalength,
1518 &umex32->outputdatalength);
1521 struct compat_ica_rsa_modexpo_crt {
1522 compat_uptr_t inputdata;
1523 unsigned int inputdatalength;
1524 compat_uptr_t outputdata;
1525 unsigned int outputdatalength;
1526 compat_uptr_t bp_key;
1527 compat_uptr_t bq_key;
1528 compat_uptr_t np_prime;
1529 compat_uptr_t nq_prime;
1530 compat_uptr_t u_mult_inv;
1533 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1534 unsigned int cmd, unsigned long arg)
1536 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1537 struct compat_ica_rsa_modexpo_crt crt32;
1538 struct ica_rsa_modexpo_crt crt64;
1541 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1543 crt64.inputdata = compat_ptr(crt32.inputdata);
1544 crt64.inputdatalength = crt32.inputdatalength;
1545 crt64.outputdata = compat_ptr(crt32.outputdata);
1546 crt64.outputdatalength = crt32.outputdatalength;
1547 crt64.bp_key = compat_ptr(crt32.bp_key);
1548 crt64.bq_key = compat_ptr(crt32.bq_key);
1549 crt64.np_prime = compat_ptr(crt32.np_prime);
1550 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1551 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1553 rc = zcrypt_rsa_crt(perms, &crt64);
1554 } while (rc == -EAGAIN);
1555 /* on failure: retry once again after a requested rescan */
1556 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1558 rc = zcrypt_rsa_crt(perms, &crt64);
1559 } while (rc == -EAGAIN);
1562 return put_user(crt64.outputdatalength,
1563 &ucrt32->outputdatalength);
1566 struct compat_ica_xcRB {
1567 unsigned short agent_ID;
1568 unsigned int user_defined;
1569 unsigned short request_ID;
1570 unsigned int request_control_blk_length;
1571 unsigned char padding1[16 - sizeof(compat_uptr_t)];
1572 compat_uptr_t request_control_blk_addr;
1573 unsigned int request_data_length;
1574 char padding2[16 - sizeof(compat_uptr_t)];
1575 compat_uptr_t request_data_address;
1576 unsigned int reply_control_blk_length;
1577 char padding3[16 - sizeof(compat_uptr_t)];
1578 compat_uptr_t reply_control_blk_addr;
1579 unsigned int reply_data_length;
1580 char padding4[16 - sizeof(compat_uptr_t)];
1581 compat_uptr_t reply_data_addr;
1582 unsigned short priority_window;
1583 unsigned int status;
1586 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1587 unsigned int cmd, unsigned long arg)
1589 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1590 struct compat_ica_xcRB xcRB32;
1591 struct ica_xcRB xcRB64;
1594 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1596 xcRB64.agent_ID = xcRB32.agent_ID;
1597 xcRB64.user_defined = xcRB32.user_defined;
1598 xcRB64.request_ID = xcRB32.request_ID;
1599 xcRB64.request_control_blk_length =
1600 xcRB32.request_control_blk_length;
1601 xcRB64.request_control_blk_addr =
1602 compat_ptr(xcRB32.request_control_blk_addr);
1603 xcRB64.request_data_length =
1604 xcRB32.request_data_length;
1605 xcRB64.request_data_address =
1606 compat_ptr(xcRB32.request_data_address);
1607 xcRB64.reply_control_blk_length =
1608 xcRB32.reply_control_blk_length;
1609 xcRB64.reply_control_blk_addr =
1610 compat_ptr(xcRB32.reply_control_blk_addr);
1611 xcRB64.reply_data_length = xcRB32.reply_data_length;
1612 xcRB64.reply_data_addr =
1613 compat_ptr(xcRB32.reply_data_addr);
1614 xcRB64.priority_window = xcRB32.priority_window;
1615 xcRB64.status = xcRB32.status;
1617 rc = _zcrypt_send_cprb(perms, &xcRB64);
1618 } while (rc == -EAGAIN);
1619 /* on failure: retry once again after a requested rescan */
1620 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1622 rc = _zcrypt_send_cprb(perms, &xcRB64);
1623 } while (rc == -EAGAIN);
1624 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1625 xcRB32.reply_data_length = xcRB64.reply_data_length;
1626 xcRB32.status = xcRB64.status;
1627 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1632 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1636 struct ap_perms *perms =
1637 (struct ap_perms *) filp->private_data;
1639 rc = zcrypt_check_ioctl(perms, cmd);
1643 if (cmd == ICARSAMODEXPO)
1644 return trans_modexpo32(perms, filp, cmd, arg);
1645 if (cmd == ICARSACRT)
1646 return trans_modexpo_crt32(perms, filp, cmd, arg);
1647 if (cmd == ZSECSENDCPRB)
1648 return trans_xcRB32(perms, filp, cmd, arg);
1649 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1654 * Misc device file operations.
1656 static const struct file_operations zcrypt_fops = {
1657 .owner = THIS_MODULE,
1658 .read = zcrypt_read,
1659 .write = zcrypt_write,
1660 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1661 #ifdef CONFIG_COMPAT
1662 .compat_ioctl = zcrypt_compat_ioctl,
1664 .open = zcrypt_open,
1665 .release = zcrypt_release,
1666 .llseek = no_llseek,
1672 static struct miscdevice zcrypt_misc_device = {
1673 .minor = MISC_DYNAMIC_MINOR,
1675 .fops = &zcrypt_fops,
1678 static int zcrypt_rng_device_count;
1679 static u32 *zcrypt_rng_buffer;
1680 static int zcrypt_rng_buffer_index;
1681 static DEFINE_MUTEX(zcrypt_rng_mutex);
1683 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1688 * We don't need locking here because the RNG API guarantees serialized
1689 * read method calls.
1691 if (zcrypt_rng_buffer_index == 0) {
1692 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1693 /* on failure: retry once again after a requested rescan */
1694 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1695 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1698 zcrypt_rng_buffer_index = rc / sizeof(*data);
1700 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1701 return sizeof(*data);
1704 static struct hwrng zcrypt_rng_dev = {
1706 .data_read = zcrypt_rng_data_read,
1710 int zcrypt_rng_device_add(void)
1714 mutex_lock(&zcrypt_rng_mutex);
1715 if (zcrypt_rng_device_count == 0) {
1716 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1717 if (!zcrypt_rng_buffer) {
1721 zcrypt_rng_buffer_index = 0;
1722 if (!zcrypt_hwrng_seed)
1723 zcrypt_rng_dev.quality = 0;
1724 rc = hwrng_register(&zcrypt_rng_dev);
1727 zcrypt_rng_device_count = 1;
1729 zcrypt_rng_device_count++;
1730 mutex_unlock(&zcrypt_rng_mutex);
1734 free_page((unsigned long) zcrypt_rng_buffer);
1736 mutex_unlock(&zcrypt_rng_mutex);
1740 void zcrypt_rng_device_remove(void)
1742 mutex_lock(&zcrypt_rng_mutex);
1743 zcrypt_rng_device_count--;
1744 if (zcrypt_rng_device_count == 0) {
1745 hwrng_unregister(&zcrypt_rng_dev);
1746 free_page((unsigned long) zcrypt_rng_buffer);
1748 mutex_unlock(&zcrypt_rng_mutex);
1751 int __init zcrypt_debug_init(void)
1753 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1754 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1755 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1756 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1761 void zcrypt_debug_exit(void)
1763 debug_unregister(zcrypt_dbf_info);
1766 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1768 static int __init zcdn_init(void)
1772 /* create a new class 'zcrypt' */
1773 zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1774 if (IS_ERR(zcrypt_class)) {
1775 rc = PTR_ERR(zcrypt_class);
1776 goto out_class_create_failed;
1778 zcrypt_class->dev_release = zcdn_device_release;
1780 /* alloc device minor range */
1781 rc = alloc_chrdev_region(&zcrypt_devt,
1782 0, ZCRYPT_MAX_MINOR_NODES,
1785 goto out_alloc_chrdev_failed;
1787 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1788 zcrypt_cdev.owner = THIS_MODULE;
1789 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1791 goto out_cdev_add_failed;
1793 /* need some class specific sysfs attributes */
1794 rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1796 goto out_class_create_file_1_failed;
1797 rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1799 goto out_class_create_file_2_failed;
1803 out_class_create_file_2_failed:
1804 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1805 out_class_create_file_1_failed:
1806 cdev_del(&zcrypt_cdev);
1807 out_cdev_add_failed:
1808 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1809 out_alloc_chrdev_failed:
1810 class_destroy(zcrypt_class);
1811 out_class_create_failed:
1815 static void zcdn_exit(void)
1817 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1818 class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1820 cdev_del(&zcrypt_cdev);
1821 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1822 class_destroy(zcrypt_class);
1828 * zcrypt_api_init(): Module initialization.
1830 * The module initialization code.
1832 int __init zcrypt_api_init(void)
1836 rc = zcrypt_debug_init();
1840 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1846 /* Register the request sprayer. */
1847 rc = misc_register(&zcrypt_misc_device);
1849 goto out_misc_register_failed;
1851 zcrypt_msgtype6_init();
1852 zcrypt_msgtype50_init();
1856 out_misc_register_failed:
1857 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1860 zcrypt_debug_exit();
1866 * zcrypt_api_exit(): Module termination.
1868 * The module termination code.
1870 void __exit zcrypt_api_exit(void)
1872 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1875 misc_deregister(&zcrypt_misc_device);
1876 zcrypt_msgtype6_exit();
1877 zcrypt_msgtype50_exit();
1878 zcrypt_ccamisc_exit();
1879 zcrypt_debug_exit();
1882 module_init(zcrypt_api_init);
1883 module_exit(zcrypt_api_exit);