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"
42 MODULE_AUTHOR("IBM Corporation");
43 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
44 "Copyright IBM Corp. 2001, 2012");
45 MODULE_LICENSE("GPL");
48 * zcrypt tracepoint functions
50 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
51 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
53 static int zcrypt_hwrng_seed = 1;
54 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
55 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
57 DEFINE_SPINLOCK(zcrypt_list_lock);
58 LIST_HEAD(zcrypt_card_list);
59 int zcrypt_device_count;
61 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
62 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
64 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
65 EXPORT_SYMBOL(zcrypt_rescan_req);
67 static LIST_HEAD(zcrypt_ops_list);
69 /* Zcrypt related debug feature stuff. */
70 debug_info_t *zcrypt_dbf_info;
73 * Process a rescan of the transport layer.
75 * Returns 1, if the rescan has been processed, otherwise 0.
77 static inline int zcrypt_process_rescan(void)
79 if (atomic_read(&zcrypt_rescan_req)) {
80 atomic_set(&zcrypt_rescan_req, 0);
81 atomic_inc(&zcrypt_rescan_count);
82 ap_bus_force_rescan();
83 ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
84 atomic_inc_return(&zcrypt_rescan_count));
90 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
92 list_add_tail(&zops->list, &zcrypt_ops_list);
95 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
97 list_del_init(&zops->list);
100 struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
102 struct zcrypt_ops *zops;
104 list_for_each_entry(zops, &zcrypt_ops_list, list)
105 if ((zops->variant == variant) &&
106 (!strncmp(zops->name, name, sizeof(zops->name))))
110 EXPORT_SYMBOL(zcrypt_msgtype);
113 * Multi device nodes extension functions.
116 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
120 static struct class *zcrypt_class;
121 static dev_t zcrypt_devt;
122 static struct cdev zcrypt_cdev;
125 struct device device;
126 struct ap_perms perms;
129 #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
131 #define ZCDN_MAX_NAME 32
133 static int zcdn_create(const char *name);
134 static int zcdn_destroy(const char *name);
136 /* helper function, matches the name for find_zcdndev_by_name() */
137 static int __match_zcdn_name(struct device *dev, const void *data)
139 return strcmp(dev_name(dev), (const char *)data) == 0;
142 /* helper function, matches the devt value for find_zcdndev_by_devt() */
143 static int __match_zcdn_devt(struct device *dev, const void *data)
145 return dev->devt == *((dev_t *) data);
149 * Find zcdn device by name.
150 * Returns reference to the zcdn device which needs to be released
151 * with put_device() after use.
153 static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
156 class_find_device(zcrypt_class, NULL,
160 return dev ? to_zcdn_dev(dev) : NULL;
164 * Find zcdn device by devt value.
165 * Returns reference to the zcdn device which needs to be released
166 * with put_device() after use.
168 static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
171 class_find_device(zcrypt_class, NULL,
175 return dev ? to_zcdn_dev(dev) : NULL;
178 static ssize_t ioctlmask_show(struct device *dev,
179 struct device_attribute *attr,
183 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
185 if (mutex_lock_interruptible(&ap_perms_mutex))
190 for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
191 snprintf(buf + 2 + 2 * i * sizeof(long),
192 PAGE_SIZE - 2 - 2 * i * sizeof(long),
193 "%016lx", zcdndev->perms.ioctlm[i]);
194 buf[2 + 2 * i * sizeof(long)] = '\n';
195 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
196 rc = 2 + 2 * i * sizeof(long) + 1;
198 mutex_unlock(&ap_perms_mutex);
203 static ssize_t ioctlmask_store(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf, size_t count)
208 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
210 rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
211 AP_IOCTLS, &ap_perms_mutex);
218 static DEVICE_ATTR_RW(ioctlmask);
220 static ssize_t apmask_show(struct device *dev,
221 struct device_attribute *attr,
225 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
227 if (mutex_lock_interruptible(&ap_perms_mutex))
232 for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
233 snprintf(buf + 2 + 2 * i * sizeof(long),
234 PAGE_SIZE - 2 - 2 * i * sizeof(long),
235 "%016lx", zcdndev->perms.apm[i]);
236 buf[2 + 2 * i * sizeof(long)] = '\n';
237 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
238 rc = 2 + 2 * i * sizeof(long) + 1;
240 mutex_unlock(&ap_perms_mutex);
245 static ssize_t apmask_store(struct device *dev,
246 struct device_attribute *attr,
247 const char *buf, size_t count)
250 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
252 rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
253 AP_DEVICES, &ap_perms_mutex);
260 static DEVICE_ATTR_RW(apmask);
262 static ssize_t aqmask_show(struct device *dev,
263 struct device_attribute *attr,
267 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
269 if (mutex_lock_interruptible(&ap_perms_mutex))
274 for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
275 snprintf(buf + 2 + 2 * i * sizeof(long),
276 PAGE_SIZE - 2 - 2 * i * sizeof(long),
277 "%016lx", zcdndev->perms.aqm[i]);
278 buf[2 + 2 * i * sizeof(long)] = '\n';
279 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
280 rc = 2 + 2 * i * sizeof(long) + 1;
282 mutex_unlock(&ap_perms_mutex);
287 static ssize_t aqmask_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
292 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
294 rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
295 AP_DOMAINS, &ap_perms_mutex);
302 static DEVICE_ATTR_RW(aqmask);
304 static struct attribute *zcdn_dev_attrs[] = {
305 &dev_attr_ioctlmask.attr,
306 &dev_attr_apmask.attr,
307 &dev_attr_aqmask.attr,
311 static struct attribute_group zcdn_dev_attr_group = {
312 .attrs = zcdn_dev_attrs
315 static const struct attribute_group *zcdn_dev_attr_groups[] = {
316 &zcdn_dev_attr_group,
320 static ssize_t zcdn_create_store(struct class *class,
321 struct class_attribute *attr,
322 const char *buf, size_t count)
325 char name[ZCDN_MAX_NAME];
327 strncpy(name, skip_spaces(buf), sizeof(name));
328 name[sizeof(name) - 1] = '\0';
330 rc = zcdn_create(strim(name));
332 return rc ? rc : count;
335 static const struct class_attribute class_attr_zcdn_create =
336 __ATTR(create, 0600, NULL, zcdn_create_store);
338 static ssize_t zcdn_destroy_store(struct class *class,
339 struct class_attribute *attr,
340 const char *buf, size_t count)
343 char name[ZCDN_MAX_NAME];
345 strncpy(name, skip_spaces(buf), sizeof(name));
346 name[sizeof(name) - 1] = '\0';
348 rc = zcdn_destroy(strim(name));
350 return rc ? rc : count;
353 static const struct class_attribute class_attr_zcdn_destroy =
354 __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
356 static void zcdn_device_release(struct device *dev)
358 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
360 ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
361 MAJOR(dev->devt), MINOR(dev->devt));
366 static int zcdn_create(const char *name)
370 char nodename[ZCDN_MAX_NAME];
371 struct zcdn_device *zcdndev;
373 if (mutex_lock_interruptible(&ap_perms_mutex))
376 /* check if device node with this name already exists */
378 zcdndev = find_zcdndev_by_name(name);
380 put_device(&zcdndev->device);
386 /* find an unused minor number */
387 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
388 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
389 zcdndev = find_zcdndev_by_devt(devt);
391 put_device(&zcdndev->device);
395 if (i == ZCRYPT_MAX_MINOR_NODES) {
400 /* alloc and prepare a new zcdn device */
401 zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
406 zcdndev->device.release = zcdn_device_release;
407 zcdndev->device.class = zcrypt_class;
408 zcdndev->device.devt = devt;
409 zcdndev->device.groups = zcdn_dev_attr_groups;
411 strncpy(nodename, name, sizeof(nodename));
413 snprintf(nodename, sizeof(nodename),
414 ZCRYPT_NAME "_%d", (int) MINOR(devt));
415 nodename[sizeof(nodename)-1] = '\0';
416 if (dev_set_name(&zcdndev->device, nodename)) {
420 rc = device_register(&zcdndev->device);
422 put_device(&zcdndev->device);
426 ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
427 MAJOR(devt), MINOR(devt));
430 mutex_unlock(&ap_perms_mutex);
434 static int zcdn_destroy(const char *name)
437 struct zcdn_device *zcdndev;
439 if (mutex_lock_interruptible(&ap_perms_mutex))
442 /* try to find this zcdn device */
443 zcdndev = find_zcdndev_by_name(name);
450 * The zcdn device is not hard destroyed. It is subject to
451 * reference counting and thus just needs to be unregistered.
453 put_device(&zcdndev->device);
454 device_unregister(&zcdndev->device);
457 mutex_unlock(&ap_perms_mutex);
461 static void zcdn_destroy_all(void)
465 struct zcdn_device *zcdndev;
467 mutex_lock(&ap_perms_mutex);
468 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
469 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
470 zcdndev = find_zcdndev_by_devt(devt);
472 put_device(&zcdndev->device);
473 device_unregister(&zcdndev->device);
476 mutex_unlock(&ap_perms_mutex);
482 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
484 * This function is not supported beyond zcrypt 1.3.1.
486 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
487 size_t count, loff_t *f_pos)
493 * zcrypt_write(): Not allowed.
495 * Write is is not allowed
497 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
498 size_t count, loff_t *f_pos)
504 * zcrypt_open(): Count number of users.
506 * Device open function to count number of users.
508 static int zcrypt_open(struct inode *inode, struct file *filp)
510 struct ap_perms *perms = &ap_perms;
512 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
513 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
514 struct zcdn_device *zcdndev;
516 if (mutex_lock_interruptible(&ap_perms_mutex))
518 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
519 /* find returns a reference, no get_device() needed */
520 mutex_unlock(&ap_perms_mutex);
522 perms = &zcdndev->perms;
525 filp->private_data = (void *) perms;
527 atomic_inc(&zcrypt_open_count);
528 return stream_open(inode, filp);
532 * zcrypt_release(): Count number of users.
534 * Device close function to count number of users.
536 static int zcrypt_release(struct inode *inode, struct file *filp)
538 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
539 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
540 struct zcdn_device *zcdndev;
542 mutex_lock(&ap_perms_mutex);
543 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
544 mutex_unlock(&ap_perms_mutex);
546 /* 2 puts here: one for find, one for open */
547 put_device(&zcdndev->device);
548 put_device(&zcdndev->device);
553 atomic_dec(&zcrypt_open_count);
557 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
561 int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
563 if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
564 if (test_bit_inv(ioctlnr, perms->ioctlm))
570 "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
576 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
578 return test_bit_inv(card, perms->apm) ? true : false;
581 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
583 return test_bit_inv(queue, perms->aqm) ? true : false;
586 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
587 struct zcrypt_queue *zq,
588 struct module **pmod,
591 if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
593 zcrypt_queue_get(zq);
594 get_device(&zq->queue->ap_dev.device);
595 atomic_add(weight, &zc->load);
596 atomic_add(weight, &zq->load);
598 *pmod = zq->queue->ap_dev.drv->driver.owner;
602 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
603 struct zcrypt_queue *zq,
608 atomic_sub(weight, &zc->load);
609 atomic_sub(weight, &zq->load);
610 put_device(&zq->queue->ap_dev.device);
611 zcrypt_queue_put(zq);
615 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
616 struct zcrypt_card *pref_zc,
618 unsigned int pref_weight)
622 weight += atomic_read(&zc->load);
623 pref_weight += atomic_read(&pref_zc->load);
624 if (weight == pref_weight)
625 return atomic_read(&zc->card->total_request_count) >
626 atomic_read(&pref_zc->card->total_request_count);
627 return weight > pref_weight;
630 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
631 struct zcrypt_queue *pref_zq,
633 unsigned int pref_weight)
637 weight += atomic_read(&zq->load);
638 pref_weight += atomic_read(&pref_zq->load);
639 if (weight == pref_weight)
640 return zq->queue->total_request_count >
641 pref_zq->queue->total_request_count;
642 return weight > pref_weight;
648 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
649 struct ica_rsa_modexpo *mex)
651 struct zcrypt_card *zc, *pref_zc;
652 struct zcrypt_queue *zq, *pref_zq;
653 unsigned int weight, pref_weight;
654 unsigned int func_code;
655 int qid = 0, rc = -ENODEV;
658 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
660 if (mex->outputdatalength < mex->inputdatalength) {
667 * As long as outputdatalength is big enough, we can set the
668 * outputdatalength equal to the inputdatalength, since that is the
669 * number of bytes we will copy in any case
671 mex->outputdatalength = mex->inputdatalength;
673 rc = get_rsa_modex_fc(mex, &func_code);
679 spin_lock(&zcrypt_list_lock);
680 for_each_zcrypt_card(zc) {
681 /* Check for online accelarator and CCA cards */
682 if (!zc->online || !(zc->card->functions & 0x18000000))
684 /* Check for size limits */
685 if (zc->min_mod_size > mex->inputdatalength ||
686 zc->max_mod_size < mex->inputdatalength)
688 /* check if device node has admission for this card */
689 if (!zcrypt_check_card(perms, zc->card->id))
691 /* get weight index of the card device */
692 weight = zc->speed_rating[func_code];
693 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
695 for_each_zcrypt_queue(zq, zc) {
696 /* check if device is online and eligible */
697 if (!zq->online || !zq->ops->rsa_modexpo)
699 /* check if device node has admission for this queue */
700 if (!zcrypt_check_queue(perms,
701 AP_QID_QUEUE(zq->queue->qid)))
703 if (zcrypt_queue_compare(zq, pref_zq,
704 weight, pref_weight))
708 pref_weight = weight;
711 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
712 spin_unlock(&zcrypt_list_lock);
719 qid = pref_zq->queue->qid;
720 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
722 spin_lock(&zcrypt_list_lock);
723 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
724 spin_unlock(&zcrypt_list_lock);
727 trace_s390_zcrypt_rep(mex, func_code, rc,
728 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
732 static long zcrypt_rsa_crt(struct ap_perms *perms,
733 struct ica_rsa_modexpo_crt *crt)
735 struct zcrypt_card *zc, *pref_zc;
736 struct zcrypt_queue *zq, *pref_zq;
737 unsigned int weight, pref_weight;
738 unsigned int func_code;
739 int qid = 0, rc = -ENODEV;
742 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
744 if (crt->outputdatalength < crt->inputdatalength) {
751 * As long as outputdatalength is big enough, we can set the
752 * outputdatalength equal to the inputdatalength, since that is the
753 * number of bytes we will copy in any case
755 crt->outputdatalength = crt->inputdatalength;
757 rc = get_rsa_crt_fc(crt, &func_code);
763 spin_lock(&zcrypt_list_lock);
764 for_each_zcrypt_card(zc) {
765 /* Check for online accelarator and CCA cards */
766 if (!zc->online || !(zc->card->functions & 0x18000000))
768 /* Check for size limits */
769 if (zc->min_mod_size > crt->inputdatalength ||
770 zc->max_mod_size < crt->inputdatalength)
772 /* check if device node has admission for this card */
773 if (!zcrypt_check_card(perms, zc->card->id))
775 /* get weight index of the card device */
776 weight = zc->speed_rating[func_code];
777 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
779 for_each_zcrypt_queue(zq, zc) {
780 /* check if device is online and eligible */
781 if (!zq->online || !zq->ops->rsa_modexpo_crt)
783 /* check if device node has admission for this queue */
784 if (!zcrypt_check_queue(perms,
785 AP_QID_QUEUE(zq->queue->qid)))
787 if (zcrypt_queue_compare(zq, pref_zq,
788 weight, pref_weight))
792 pref_weight = weight;
795 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
796 spin_unlock(&zcrypt_list_lock);
803 qid = pref_zq->queue->qid;
804 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
806 spin_lock(&zcrypt_list_lock);
807 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
808 spin_unlock(&zcrypt_list_lock);
811 trace_s390_zcrypt_rep(crt, func_code, rc,
812 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
816 static long _zcrypt_send_cprb(struct ap_perms *perms,
817 struct ica_xcRB *xcRB)
819 struct zcrypt_card *zc, *pref_zc;
820 struct zcrypt_queue *zq, *pref_zq;
821 struct ap_message ap_msg;
822 unsigned int weight, pref_weight;
823 unsigned int func_code;
824 unsigned short *domain, tdom;
825 int qid = 0, rc = -ENODEV;
828 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
831 ap_init_message(&ap_msg);
832 rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
837 * If a valid target domain is set and this domain is NOT a usage
838 * domain but a control only domain, use the default domain as target.
841 if (tdom >= 0 && tdom < AP_DOMAINS &&
842 !ap_test_config_usage_domain(tdom) &&
843 ap_test_config_ctrl_domain(tdom) &&
844 ap_domain_index >= 0)
845 tdom = ap_domain_index;
849 spin_lock(&zcrypt_list_lock);
850 for_each_zcrypt_card(zc) {
851 /* Check for online CCA cards */
852 if (!zc->online || !(zc->card->functions & 0x10000000))
854 /* Check for user selected CCA card */
855 if (xcRB->user_defined != AUTOSELECT &&
856 xcRB->user_defined != zc->card->id)
858 /* check if device node has admission for this card */
859 if (!zcrypt_check_card(perms, zc->card->id))
861 /* get weight index of the card device */
862 weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
863 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
865 for_each_zcrypt_queue(zq, zc) {
866 /* check if device is online and eligible */
868 !zq->ops->send_cprb ||
869 (tdom != (unsigned short) AUTOSELECT &&
870 tdom != AP_QID_QUEUE(zq->queue->qid)))
872 /* check if device node has admission for this queue */
873 if (!zcrypt_check_queue(perms,
874 AP_QID_QUEUE(zq->queue->qid)))
876 if (zcrypt_queue_compare(zq, pref_zq,
877 weight, pref_weight))
881 pref_weight = weight;
884 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
885 spin_unlock(&zcrypt_list_lock);
892 /* in case of auto select, provide the correct domain */
893 qid = pref_zq->queue->qid;
894 if (*domain == (unsigned short) AUTOSELECT)
895 *domain = AP_QID_QUEUE(qid);
897 rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
899 spin_lock(&zcrypt_list_lock);
900 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
901 spin_unlock(&zcrypt_list_lock);
904 ap_release_message(&ap_msg);
905 trace_s390_zcrypt_rep(xcRB, func_code, rc,
906 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
910 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
912 return _zcrypt_send_cprb(&ap_perms, xcRB);
914 EXPORT_SYMBOL(zcrypt_send_cprb);
916 static bool is_desired_ep11_card(unsigned int dev_id,
917 unsigned short target_num,
918 struct ep11_target_dev *targets)
920 while (target_num-- > 0) {
921 if (dev_id == targets->ap_id)
928 static bool is_desired_ep11_queue(unsigned int dev_qid,
929 unsigned short target_num,
930 struct ep11_target_dev *targets)
932 while (target_num-- > 0) {
933 if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
940 static long zcrypt_send_ep11_cprb(struct ap_perms *perms,
941 struct ep11_urb *xcrb)
943 struct zcrypt_card *zc, *pref_zc;
944 struct zcrypt_queue *zq, *pref_zq;
945 struct ep11_target_dev *targets;
946 unsigned short target_num;
947 unsigned int weight, pref_weight;
948 unsigned int func_code;
949 struct ap_message ap_msg;
950 int qid = 0, rc = -ENODEV;
953 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
955 ap_init_message(&ap_msg);
957 target_num = (unsigned short) xcrb->targets_num;
959 /* empty list indicates autoselect (all available targets) */
961 if (target_num != 0) {
962 struct ep11_target_dev __user *uptr;
964 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
971 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
972 if (copy_from_user(targets, uptr,
973 target_num * sizeof(*targets))) {
980 rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
986 spin_lock(&zcrypt_list_lock);
987 for_each_zcrypt_card(zc) {
988 /* Check for online EP11 cards */
989 if (!zc->online || !(zc->card->functions & 0x04000000))
991 /* Check for user selected EP11 card */
993 !is_desired_ep11_card(zc->card->id, target_num, targets))
995 /* check if device node has admission for this card */
996 if (!zcrypt_check_card(perms, zc->card->id))
998 /* get weight index of the card device */
999 weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1000 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1002 for_each_zcrypt_queue(zq, zc) {
1003 /* check if device is online and eligible */
1005 !zq->ops->send_ep11_cprb ||
1007 !is_desired_ep11_queue(zq->queue->qid,
1008 target_num, targets)))
1010 /* check if device node has admission for this queue */
1011 if (!zcrypt_check_queue(perms,
1012 AP_QID_QUEUE(zq->queue->qid)))
1014 if (zcrypt_queue_compare(zq, pref_zq,
1015 weight, pref_weight))
1019 pref_weight = weight;
1022 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1023 spin_unlock(&zcrypt_list_lock);
1030 qid = pref_zq->queue->qid;
1031 rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
1033 spin_lock(&zcrypt_list_lock);
1034 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1035 spin_unlock(&zcrypt_list_lock);
1040 ap_release_message(&ap_msg);
1041 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1042 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1046 static long zcrypt_rng(char *buffer)
1048 struct zcrypt_card *zc, *pref_zc;
1049 struct zcrypt_queue *zq, *pref_zq;
1050 unsigned int weight, pref_weight;
1051 unsigned int func_code;
1052 struct ap_message ap_msg;
1053 unsigned int domain;
1054 int qid = 0, rc = -ENODEV;
1057 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1059 ap_init_message(&ap_msg);
1060 rc = get_rng_fc(&ap_msg, &func_code, &domain);
1066 spin_lock(&zcrypt_list_lock);
1067 for_each_zcrypt_card(zc) {
1068 /* Check for online CCA cards */
1069 if (!zc->online || !(zc->card->functions & 0x10000000))
1071 /* get weight index of the card device */
1072 weight = zc->speed_rating[func_code];
1073 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1075 for_each_zcrypt_queue(zq, zc) {
1076 /* check if device is online and eligible */
1077 if (!zq->online || !zq->ops->rng)
1079 if (zcrypt_queue_compare(zq, pref_zq,
1080 weight, pref_weight))
1084 pref_weight = weight;
1087 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1088 spin_unlock(&zcrypt_list_lock);
1095 qid = pref_zq->queue->qid;
1096 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1098 spin_lock(&zcrypt_list_lock);
1099 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1100 spin_unlock(&zcrypt_list_lock);
1103 ap_release_message(&ap_msg);
1104 trace_s390_zcrypt_rep(buffer, func_code, rc,
1105 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1109 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1111 struct zcrypt_card *zc;
1112 struct zcrypt_queue *zq;
1113 struct zcrypt_device_status *stat;
1116 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1117 * sizeof(struct zcrypt_device_status));
1119 spin_lock(&zcrypt_list_lock);
1120 for_each_zcrypt_card(zc) {
1121 for_each_zcrypt_queue(zq, zc) {
1122 card = AP_QID_CARD(zq->queue->qid);
1123 if (card >= MAX_ZDEV_CARDIDS)
1125 queue = AP_QID_QUEUE(zq->queue->qid);
1126 stat = &devstatus[card * AP_DOMAINS + queue];
1127 stat->hwtype = zc->card->ap_dev.device_type;
1128 stat->functions = zc->card->functions >> 26;
1129 stat->qid = zq->queue->qid;
1130 stat->online = zq->online ? 0x01 : 0x00;
1133 spin_unlock(&zcrypt_list_lock);
1136 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1138 struct zcrypt_card *zc;
1139 struct zcrypt_queue *zq;
1140 struct zcrypt_device_status_ext *stat;
1143 memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1144 * sizeof(struct zcrypt_device_status_ext));
1146 spin_lock(&zcrypt_list_lock);
1147 for_each_zcrypt_card(zc) {
1148 for_each_zcrypt_queue(zq, zc) {
1149 card = AP_QID_CARD(zq->queue->qid);
1150 queue = AP_QID_QUEUE(zq->queue->qid);
1151 stat = &devstatus[card * AP_DOMAINS + queue];
1152 stat->hwtype = zc->card->ap_dev.device_type;
1153 stat->functions = zc->card->functions >> 26;
1154 stat->qid = zq->queue->qid;
1155 stat->online = zq->online ? 0x01 : 0x00;
1158 spin_unlock(&zcrypt_list_lock);
1160 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1162 static void zcrypt_status_mask(char status[], size_t max_adapters)
1164 struct zcrypt_card *zc;
1165 struct zcrypt_queue *zq;
1168 memset(status, 0, max_adapters);
1169 spin_lock(&zcrypt_list_lock);
1170 for_each_zcrypt_card(zc) {
1171 for_each_zcrypt_queue(zq, zc) {
1172 card = AP_QID_CARD(zq->queue->qid);
1173 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1174 || card >= max_adapters)
1176 status[card] = zc->online ? zc->user_space_type : 0x0d;
1179 spin_unlock(&zcrypt_list_lock);
1182 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1184 struct zcrypt_card *zc;
1185 struct zcrypt_queue *zq;
1188 memset(qdepth, 0, max_adapters);
1189 spin_lock(&zcrypt_list_lock);
1191 for_each_zcrypt_card(zc) {
1192 for_each_zcrypt_queue(zq, zc) {
1193 card = AP_QID_CARD(zq->queue->qid);
1194 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1195 || card >= max_adapters)
1197 spin_lock(&zq->queue->lock);
1199 zq->queue->pendingq_count +
1200 zq->queue->requestq_count;
1201 spin_unlock(&zq->queue->lock);
1205 spin_unlock(&zcrypt_list_lock);
1208 static void zcrypt_perdev_reqcnt(int reqcnt[], size_t max_adapters)
1210 struct zcrypt_card *zc;
1211 struct zcrypt_queue *zq;
1214 memset(reqcnt, 0, sizeof(int) * max_adapters);
1215 spin_lock(&zcrypt_list_lock);
1217 for_each_zcrypt_card(zc) {
1218 for_each_zcrypt_queue(zq, zc) {
1219 card = AP_QID_CARD(zq->queue->qid);
1220 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1221 || card >= max_adapters)
1223 spin_lock(&zq->queue->lock);
1224 reqcnt[card] = zq->queue->total_request_count;
1225 spin_unlock(&zq->queue->lock);
1229 spin_unlock(&zcrypt_list_lock);
1232 static int zcrypt_pendingq_count(void)
1234 struct zcrypt_card *zc;
1235 struct zcrypt_queue *zq;
1239 spin_lock(&zcrypt_list_lock);
1241 for_each_zcrypt_card(zc) {
1242 for_each_zcrypt_queue(zq, zc) {
1243 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1245 spin_lock(&zq->queue->lock);
1246 pendingq_count += zq->queue->pendingq_count;
1247 spin_unlock(&zq->queue->lock);
1251 spin_unlock(&zcrypt_list_lock);
1252 return pendingq_count;
1255 static int zcrypt_requestq_count(void)
1257 struct zcrypt_card *zc;
1258 struct zcrypt_queue *zq;
1262 spin_lock(&zcrypt_list_lock);
1264 for_each_zcrypt_card(zc) {
1265 for_each_zcrypt_queue(zq, zc) {
1266 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1268 spin_lock(&zq->queue->lock);
1269 requestq_count += zq->queue->requestq_count;
1270 spin_unlock(&zq->queue->lock);
1274 spin_unlock(&zcrypt_list_lock);
1275 return requestq_count;
1278 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1282 struct ap_perms *perms =
1283 (struct ap_perms *) filp->private_data;
1285 rc = zcrypt_check_ioctl(perms, cmd);
1290 case ICARSAMODEXPO: {
1291 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1292 struct ica_rsa_modexpo mex;
1294 if (copy_from_user(&mex, umex, sizeof(mex)))
1297 rc = zcrypt_rsa_modexpo(perms, &mex);
1298 } while (rc == -EAGAIN);
1299 /* on failure: retry once again after a requested rescan */
1300 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1302 rc = zcrypt_rsa_modexpo(perms, &mex);
1303 } while (rc == -EAGAIN);
1305 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1308 return put_user(mex.outputdatalength, &umex->outputdatalength);
1311 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1312 struct ica_rsa_modexpo_crt crt;
1314 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1317 rc = zcrypt_rsa_crt(perms, &crt);
1318 } while (rc == -EAGAIN);
1319 /* on failure: retry once again after a requested rescan */
1320 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1322 rc = zcrypt_rsa_crt(perms, &crt);
1323 } while (rc == -EAGAIN);
1325 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1328 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1330 case ZSECSENDCPRB: {
1331 struct ica_xcRB __user *uxcRB = (void __user *) arg;
1332 struct ica_xcRB xcRB;
1334 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1337 rc = _zcrypt_send_cprb(perms, &xcRB);
1338 } while (rc == -EAGAIN);
1339 /* on failure: retry once again after a requested rescan */
1340 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1342 rc = _zcrypt_send_cprb(perms, &xcRB);
1343 } while (rc == -EAGAIN);
1345 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1347 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1351 case ZSENDEP11CPRB: {
1352 struct ep11_urb __user *uxcrb = (void __user *)arg;
1353 struct ep11_urb xcrb;
1355 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1358 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1359 } while (rc == -EAGAIN);
1360 /* on failure: retry once again after a requested rescan */
1361 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1363 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1364 } while (rc == -EAGAIN);
1366 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1367 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1371 case ZCRYPT_DEVICE_STATUS: {
1372 struct zcrypt_device_status_ext *device_status;
1373 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1374 * sizeof(struct zcrypt_device_status_ext);
1376 device_status = kzalloc(total_size, GFP_KERNEL);
1379 zcrypt_device_status_mask_ext(device_status);
1380 if (copy_to_user((char __user *) arg, device_status,
1383 kfree(device_status);
1386 case ZCRYPT_STATUS_MASK: {
1387 char status[AP_DEVICES];
1389 zcrypt_status_mask(status, AP_DEVICES);
1390 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1394 case ZCRYPT_QDEPTH_MASK: {
1395 char qdepth[AP_DEVICES];
1397 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1398 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1402 case ZCRYPT_PERDEV_REQCNT: {
1405 reqcnt = kcalloc(AP_DEVICES, sizeof(int), GFP_KERNEL);
1408 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1409 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1414 case Z90STAT_REQUESTQ_COUNT:
1415 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1416 case Z90STAT_PENDINGQ_COUNT:
1417 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1418 case Z90STAT_TOTALOPEN_COUNT:
1419 return put_user(atomic_read(&zcrypt_open_count),
1420 (int __user *) arg);
1421 case Z90STAT_DOMAIN_INDEX:
1422 return put_user(ap_domain_index, (int __user *) arg);
1426 case ZDEVICESTATUS: {
1427 /* the old ioctl supports only 64 adapters */
1428 struct zcrypt_device_status *device_status;
1429 size_t total_size = MAX_ZDEV_ENTRIES
1430 * sizeof(struct zcrypt_device_status);
1432 device_status = kzalloc(total_size, GFP_KERNEL);
1435 zcrypt_device_status_mask(device_status);
1436 if (copy_to_user((char __user *) arg, device_status,
1439 kfree(device_status);
1442 case Z90STAT_STATUS_MASK: {
1443 /* the old ioctl supports only 64 adapters */
1444 char status[MAX_ZDEV_CARDIDS];
1446 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1447 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1451 case Z90STAT_QDEPTH_MASK: {
1452 /* the old ioctl supports only 64 adapters */
1453 char qdepth[MAX_ZDEV_CARDIDS];
1455 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1456 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1460 case Z90STAT_PERDEV_REQCNT: {
1461 /* the old ioctl supports only 64 adapters */
1462 int reqcnt[MAX_ZDEV_CARDIDS];
1464 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1465 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1469 /* unknown ioctl number */
1471 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1472 return -ENOIOCTLCMD;
1476 #ifdef CONFIG_COMPAT
1478 * ioctl32 conversion routines
1480 struct compat_ica_rsa_modexpo {
1481 compat_uptr_t inputdata;
1482 unsigned int inputdatalength;
1483 compat_uptr_t outputdata;
1484 unsigned int outputdatalength;
1485 compat_uptr_t b_key;
1486 compat_uptr_t n_modulus;
1489 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1490 unsigned int cmd, unsigned long arg)
1492 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1493 struct compat_ica_rsa_modexpo mex32;
1494 struct ica_rsa_modexpo mex64;
1497 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1499 mex64.inputdata = compat_ptr(mex32.inputdata);
1500 mex64.inputdatalength = mex32.inputdatalength;
1501 mex64.outputdata = compat_ptr(mex32.outputdata);
1502 mex64.outputdatalength = mex32.outputdatalength;
1503 mex64.b_key = compat_ptr(mex32.b_key);
1504 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1506 rc = zcrypt_rsa_modexpo(perms, &mex64);
1507 } while (rc == -EAGAIN);
1508 /* on failure: retry once again after a requested rescan */
1509 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1511 rc = zcrypt_rsa_modexpo(perms, &mex64);
1512 } while (rc == -EAGAIN);
1515 return put_user(mex64.outputdatalength,
1516 &umex32->outputdatalength);
1519 struct compat_ica_rsa_modexpo_crt {
1520 compat_uptr_t inputdata;
1521 unsigned int inputdatalength;
1522 compat_uptr_t outputdata;
1523 unsigned int outputdatalength;
1524 compat_uptr_t bp_key;
1525 compat_uptr_t bq_key;
1526 compat_uptr_t np_prime;
1527 compat_uptr_t nq_prime;
1528 compat_uptr_t u_mult_inv;
1531 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1532 unsigned int cmd, unsigned long arg)
1534 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1535 struct compat_ica_rsa_modexpo_crt crt32;
1536 struct ica_rsa_modexpo_crt crt64;
1539 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1541 crt64.inputdata = compat_ptr(crt32.inputdata);
1542 crt64.inputdatalength = crt32.inputdatalength;
1543 crt64.outputdata = compat_ptr(crt32.outputdata);
1544 crt64.outputdatalength = crt32.outputdatalength;
1545 crt64.bp_key = compat_ptr(crt32.bp_key);
1546 crt64.bq_key = compat_ptr(crt32.bq_key);
1547 crt64.np_prime = compat_ptr(crt32.np_prime);
1548 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1549 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1551 rc = zcrypt_rsa_crt(perms, &crt64);
1552 } while (rc == -EAGAIN);
1553 /* on failure: retry once again after a requested rescan */
1554 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1556 rc = zcrypt_rsa_crt(perms, &crt64);
1557 } while (rc == -EAGAIN);
1560 return put_user(crt64.outputdatalength,
1561 &ucrt32->outputdatalength);
1564 struct compat_ica_xcRB {
1565 unsigned short agent_ID;
1566 unsigned int user_defined;
1567 unsigned short request_ID;
1568 unsigned int request_control_blk_length;
1569 unsigned char padding1[16 - sizeof(compat_uptr_t)];
1570 compat_uptr_t request_control_blk_addr;
1571 unsigned int request_data_length;
1572 char padding2[16 - sizeof(compat_uptr_t)];
1573 compat_uptr_t request_data_address;
1574 unsigned int reply_control_blk_length;
1575 char padding3[16 - sizeof(compat_uptr_t)];
1576 compat_uptr_t reply_control_blk_addr;
1577 unsigned int reply_data_length;
1578 char padding4[16 - sizeof(compat_uptr_t)];
1579 compat_uptr_t reply_data_addr;
1580 unsigned short priority_window;
1581 unsigned int status;
1584 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1585 unsigned int cmd, unsigned long arg)
1587 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1588 struct compat_ica_xcRB xcRB32;
1589 struct ica_xcRB xcRB64;
1592 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1594 xcRB64.agent_ID = xcRB32.agent_ID;
1595 xcRB64.user_defined = xcRB32.user_defined;
1596 xcRB64.request_ID = xcRB32.request_ID;
1597 xcRB64.request_control_blk_length =
1598 xcRB32.request_control_blk_length;
1599 xcRB64.request_control_blk_addr =
1600 compat_ptr(xcRB32.request_control_blk_addr);
1601 xcRB64.request_data_length =
1602 xcRB32.request_data_length;
1603 xcRB64.request_data_address =
1604 compat_ptr(xcRB32.request_data_address);
1605 xcRB64.reply_control_blk_length =
1606 xcRB32.reply_control_blk_length;
1607 xcRB64.reply_control_blk_addr =
1608 compat_ptr(xcRB32.reply_control_blk_addr);
1609 xcRB64.reply_data_length = xcRB32.reply_data_length;
1610 xcRB64.reply_data_addr =
1611 compat_ptr(xcRB32.reply_data_addr);
1612 xcRB64.priority_window = xcRB32.priority_window;
1613 xcRB64.status = xcRB32.status;
1615 rc = _zcrypt_send_cprb(perms, &xcRB64);
1616 } while (rc == -EAGAIN);
1617 /* on failure: retry once again after a requested rescan */
1618 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1620 rc = _zcrypt_send_cprb(perms, &xcRB64);
1621 } while (rc == -EAGAIN);
1622 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1623 xcRB32.reply_data_length = xcRB64.reply_data_length;
1624 xcRB32.status = xcRB64.status;
1625 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1630 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1634 struct ap_perms *perms =
1635 (struct ap_perms *) filp->private_data;
1637 rc = zcrypt_check_ioctl(perms, cmd);
1641 if (cmd == ICARSAMODEXPO)
1642 return trans_modexpo32(perms, filp, cmd, arg);
1643 if (cmd == ICARSACRT)
1644 return trans_modexpo_crt32(perms, filp, cmd, arg);
1645 if (cmd == ZSECSENDCPRB)
1646 return trans_xcRB32(perms, filp, cmd, arg);
1647 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1652 * Misc device file operations.
1654 static const struct file_operations zcrypt_fops = {
1655 .owner = THIS_MODULE,
1656 .read = zcrypt_read,
1657 .write = zcrypt_write,
1658 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1659 #ifdef CONFIG_COMPAT
1660 .compat_ioctl = zcrypt_compat_ioctl,
1662 .open = zcrypt_open,
1663 .release = zcrypt_release,
1664 .llseek = no_llseek,
1670 static struct miscdevice zcrypt_misc_device = {
1671 .minor = MISC_DYNAMIC_MINOR,
1673 .fops = &zcrypt_fops,
1676 static int zcrypt_rng_device_count;
1677 static u32 *zcrypt_rng_buffer;
1678 static int zcrypt_rng_buffer_index;
1679 static DEFINE_MUTEX(zcrypt_rng_mutex);
1681 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1686 * We don't need locking here because the RNG API guarantees serialized
1687 * read method calls.
1689 if (zcrypt_rng_buffer_index == 0) {
1690 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1691 /* on failure: retry once again after a requested rescan */
1692 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1693 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1696 zcrypt_rng_buffer_index = rc / sizeof(*data);
1698 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1699 return sizeof(*data);
1702 static struct hwrng zcrypt_rng_dev = {
1704 .data_read = zcrypt_rng_data_read,
1708 int zcrypt_rng_device_add(void)
1712 mutex_lock(&zcrypt_rng_mutex);
1713 if (zcrypt_rng_device_count == 0) {
1714 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1715 if (!zcrypt_rng_buffer) {
1719 zcrypt_rng_buffer_index = 0;
1720 if (!zcrypt_hwrng_seed)
1721 zcrypt_rng_dev.quality = 0;
1722 rc = hwrng_register(&zcrypt_rng_dev);
1725 zcrypt_rng_device_count = 1;
1727 zcrypt_rng_device_count++;
1728 mutex_unlock(&zcrypt_rng_mutex);
1732 free_page((unsigned long) zcrypt_rng_buffer);
1734 mutex_unlock(&zcrypt_rng_mutex);
1738 void zcrypt_rng_device_remove(void)
1740 mutex_lock(&zcrypt_rng_mutex);
1741 zcrypt_rng_device_count--;
1742 if (zcrypt_rng_device_count == 0) {
1743 hwrng_unregister(&zcrypt_rng_dev);
1744 free_page((unsigned long) zcrypt_rng_buffer);
1746 mutex_unlock(&zcrypt_rng_mutex);
1749 int __init zcrypt_debug_init(void)
1751 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1752 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1753 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1754 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1759 void zcrypt_debug_exit(void)
1761 debug_unregister(zcrypt_dbf_info);
1764 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1766 static int __init zcdn_init(void)
1770 /* create a new class 'zcrypt' */
1771 zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1772 if (IS_ERR(zcrypt_class)) {
1773 rc = PTR_ERR(zcrypt_class);
1774 goto out_class_create_failed;
1776 zcrypt_class->dev_release = zcdn_device_release;
1778 /* alloc device minor range */
1779 rc = alloc_chrdev_region(&zcrypt_devt,
1780 0, ZCRYPT_MAX_MINOR_NODES,
1783 goto out_alloc_chrdev_failed;
1785 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1786 zcrypt_cdev.owner = THIS_MODULE;
1787 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1789 goto out_cdev_add_failed;
1791 /* need some class specific sysfs attributes */
1792 rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1794 goto out_class_create_file_1_failed;
1795 rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1797 goto out_class_create_file_2_failed;
1801 out_class_create_file_2_failed:
1802 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1803 out_class_create_file_1_failed:
1804 cdev_del(&zcrypt_cdev);
1805 out_cdev_add_failed:
1806 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1807 out_alloc_chrdev_failed:
1808 class_destroy(zcrypt_class);
1809 out_class_create_failed:
1813 static void zcdn_exit(void)
1815 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1816 class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1818 cdev_del(&zcrypt_cdev);
1819 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1820 class_destroy(zcrypt_class);
1826 * zcrypt_api_init(): Module initialization.
1828 * The module initialization code.
1830 int __init zcrypt_api_init(void)
1834 rc = zcrypt_debug_init();
1838 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1844 /* Register the request sprayer. */
1845 rc = misc_register(&zcrypt_misc_device);
1847 goto out_misc_register_failed;
1849 zcrypt_msgtype6_init();
1850 zcrypt_msgtype50_init();
1854 out_misc_register_failed:
1855 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1858 zcrypt_debug_exit();
1864 * zcrypt_api_exit(): Module termination.
1866 * The module termination code.
1868 void __exit zcrypt_api_exit(void)
1870 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1873 misc_deregister(&zcrypt_misc_device);
1874 zcrypt_msgtype6_exit();
1875 zcrypt_msgtype50_exit();
1876 zcrypt_debug_exit();
1879 module_init(zcrypt_api_init);
1880 module_exit(zcrypt_api_exit);