1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2001, 2018
4 * Author(s): Robert Burroughs
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);
138 * Find zcdn device by name.
139 * Returns reference to the zcdn device which needs to be released
140 * with put_device() after use.
142 static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
144 struct device *dev = class_find_device_by_name(zcrypt_class, name);
146 return dev ? to_zcdn_dev(dev) : NULL;
150 * Find zcdn device by devt value.
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_devt(dev_t devt)
156 struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
158 return dev ? to_zcdn_dev(dev) : NULL;
161 static ssize_t ioctlmask_show(struct device *dev,
162 struct device_attribute *attr,
166 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
168 if (mutex_lock_interruptible(&ap_perms_mutex))
173 for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
174 snprintf(buf + 2 + 2 * i * sizeof(long),
175 PAGE_SIZE - 2 - 2 * i * sizeof(long),
176 "%016lx", zcdndev->perms.ioctlm[i]);
177 buf[2 + 2 * i * sizeof(long)] = '\n';
178 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
179 rc = 2 + 2 * i * sizeof(long) + 1;
181 mutex_unlock(&ap_perms_mutex);
186 static ssize_t ioctlmask_store(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
191 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
193 rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
194 AP_IOCTLS, &ap_perms_mutex);
201 static DEVICE_ATTR_RW(ioctlmask);
203 static ssize_t apmask_show(struct device *dev,
204 struct device_attribute *attr,
208 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
210 if (mutex_lock_interruptible(&ap_perms_mutex))
215 for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
216 snprintf(buf + 2 + 2 * i * sizeof(long),
217 PAGE_SIZE - 2 - 2 * i * sizeof(long),
218 "%016lx", zcdndev->perms.apm[i]);
219 buf[2 + 2 * i * sizeof(long)] = '\n';
220 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
221 rc = 2 + 2 * i * sizeof(long) + 1;
223 mutex_unlock(&ap_perms_mutex);
228 static ssize_t apmask_store(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t count)
233 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
235 rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
236 AP_DEVICES, &ap_perms_mutex);
243 static DEVICE_ATTR_RW(apmask);
245 static ssize_t aqmask_show(struct device *dev,
246 struct device_attribute *attr,
250 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
252 if (mutex_lock_interruptible(&ap_perms_mutex))
257 for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
258 snprintf(buf + 2 + 2 * i * sizeof(long),
259 PAGE_SIZE - 2 - 2 * i * sizeof(long),
260 "%016lx", zcdndev->perms.aqm[i]);
261 buf[2 + 2 * i * sizeof(long)] = '\n';
262 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
263 rc = 2 + 2 * i * sizeof(long) + 1;
265 mutex_unlock(&ap_perms_mutex);
270 static ssize_t aqmask_store(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf, size_t count)
275 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
277 rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
278 AP_DOMAINS, &ap_perms_mutex);
285 static DEVICE_ATTR_RW(aqmask);
287 static struct attribute *zcdn_dev_attrs[] = {
288 &dev_attr_ioctlmask.attr,
289 &dev_attr_apmask.attr,
290 &dev_attr_aqmask.attr,
294 static struct attribute_group zcdn_dev_attr_group = {
295 .attrs = zcdn_dev_attrs
298 static const struct attribute_group *zcdn_dev_attr_groups[] = {
299 &zcdn_dev_attr_group,
303 static ssize_t zcdn_create_store(struct class *class,
304 struct class_attribute *attr,
305 const char *buf, size_t count)
308 char name[ZCDN_MAX_NAME];
310 strncpy(name, skip_spaces(buf), sizeof(name));
311 name[sizeof(name) - 1] = '\0';
313 rc = zcdn_create(strim(name));
315 return rc ? rc : count;
318 static const struct class_attribute class_attr_zcdn_create =
319 __ATTR(create, 0600, NULL, zcdn_create_store);
321 static ssize_t zcdn_destroy_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_destroy(strim(name));
333 return rc ? rc : count;
336 static const struct class_attribute class_attr_zcdn_destroy =
337 __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
339 static void zcdn_device_release(struct device *dev)
341 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
343 ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
344 MAJOR(dev->devt), MINOR(dev->devt));
349 static int zcdn_create(const char *name)
353 char nodename[ZCDN_MAX_NAME];
354 struct zcdn_device *zcdndev;
356 if (mutex_lock_interruptible(&ap_perms_mutex))
359 /* check if device node with this name already exists */
361 zcdndev = find_zcdndev_by_name(name);
363 put_device(&zcdndev->device);
369 /* find an unused minor number */
370 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
371 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
372 zcdndev = find_zcdndev_by_devt(devt);
374 put_device(&zcdndev->device);
378 if (i == ZCRYPT_MAX_MINOR_NODES) {
383 /* alloc and prepare a new zcdn device */
384 zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
389 zcdndev->device.release = zcdn_device_release;
390 zcdndev->device.class = zcrypt_class;
391 zcdndev->device.devt = devt;
392 zcdndev->device.groups = zcdn_dev_attr_groups;
394 strncpy(nodename, name, sizeof(nodename));
396 snprintf(nodename, sizeof(nodename),
397 ZCRYPT_NAME "_%d", (int) MINOR(devt));
398 nodename[sizeof(nodename)-1] = '\0';
399 if (dev_set_name(&zcdndev->device, nodename)) {
403 rc = device_register(&zcdndev->device);
405 put_device(&zcdndev->device);
409 ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
410 MAJOR(devt), MINOR(devt));
413 mutex_unlock(&ap_perms_mutex);
417 static int zcdn_destroy(const char *name)
420 struct zcdn_device *zcdndev;
422 if (mutex_lock_interruptible(&ap_perms_mutex))
425 /* try to find this zcdn device */
426 zcdndev = find_zcdndev_by_name(name);
433 * The zcdn device is not hard destroyed. It is subject to
434 * reference counting and thus just needs to be unregistered.
436 put_device(&zcdndev->device);
437 device_unregister(&zcdndev->device);
440 mutex_unlock(&ap_perms_mutex);
444 static void zcdn_destroy_all(void)
448 struct zcdn_device *zcdndev;
450 mutex_lock(&ap_perms_mutex);
451 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
452 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
453 zcdndev = find_zcdndev_by_devt(devt);
455 put_device(&zcdndev->device);
456 device_unregister(&zcdndev->device);
459 mutex_unlock(&ap_perms_mutex);
465 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
467 * This function is not supported beyond zcrypt 1.3.1.
469 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
470 size_t count, loff_t *f_pos)
476 * zcrypt_write(): Not allowed.
478 * Write is is not allowed
480 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
481 size_t count, loff_t *f_pos)
487 * zcrypt_open(): Count number of users.
489 * Device open function to count number of users.
491 static int zcrypt_open(struct inode *inode, struct file *filp)
493 struct ap_perms *perms = &ap_perms;
495 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
496 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
497 struct zcdn_device *zcdndev;
499 if (mutex_lock_interruptible(&ap_perms_mutex))
501 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
502 /* find returns a reference, no get_device() needed */
503 mutex_unlock(&ap_perms_mutex);
505 perms = &zcdndev->perms;
508 filp->private_data = (void *) perms;
510 atomic_inc(&zcrypt_open_count);
511 return stream_open(inode, filp);
515 * zcrypt_release(): Count number of users.
517 * Device close function to count number of users.
519 static int zcrypt_release(struct inode *inode, struct file *filp)
521 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
522 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
523 struct zcdn_device *zcdndev;
525 if (mutex_lock_interruptible(&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 atomic_read(&zc->card->total_request_count) >
610 atomic_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 ica_rsa_modexpo *mex)
635 struct zcrypt_card *zc, *pref_zc;
636 struct zcrypt_queue *zq, *pref_zq;
637 unsigned int weight, pref_weight;
638 unsigned int func_code;
639 int qid = 0, rc = -ENODEV;
642 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
644 if (mex->outputdatalength < mex->inputdatalength) {
651 * As long as outputdatalength is big enough, we can set the
652 * outputdatalength equal to the inputdatalength, since that is the
653 * number of bytes we will copy in any case
655 mex->outputdatalength = mex->inputdatalength;
657 rc = get_rsa_modex_fc(mex, &func_code);
663 spin_lock(&zcrypt_list_lock);
664 for_each_zcrypt_card(zc) {
665 /* Check for online accelarator and CCA cards */
666 if (!zc->online || !(zc->card->functions & 0x18000000))
668 /* Check for size limits */
669 if (zc->min_mod_size > mex->inputdatalength ||
670 zc->max_mod_size < mex->inputdatalength)
672 /* check if device node has admission for this card */
673 if (!zcrypt_check_card(perms, zc->card->id))
675 /* get weight index of the card device */
676 weight = zc->speed_rating[func_code];
677 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
679 for_each_zcrypt_queue(zq, zc) {
680 /* check if device is online and eligible */
681 if (!zq->online || !zq->ops->rsa_modexpo)
683 /* check if device node has admission for this queue */
684 if (!zcrypt_check_queue(perms,
685 AP_QID_QUEUE(zq->queue->qid)))
687 if (zcrypt_queue_compare(zq, pref_zq,
688 weight, pref_weight))
692 pref_weight = weight;
695 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
696 spin_unlock(&zcrypt_list_lock);
703 qid = pref_zq->queue->qid;
704 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
706 spin_lock(&zcrypt_list_lock);
707 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
708 spin_unlock(&zcrypt_list_lock);
711 trace_s390_zcrypt_rep(mex, func_code, rc,
712 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
716 static long zcrypt_rsa_crt(struct ap_perms *perms,
717 struct ica_rsa_modexpo_crt *crt)
719 struct zcrypt_card *zc, *pref_zc;
720 struct zcrypt_queue *zq, *pref_zq;
721 unsigned int weight, pref_weight;
722 unsigned int func_code;
723 int qid = 0, rc = -ENODEV;
726 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
728 if (crt->outputdatalength < crt->inputdatalength) {
735 * As long as outputdatalength is big enough, we can set the
736 * outputdatalength equal to the inputdatalength, since that is the
737 * number of bytes we will copy in any case
739 crt->outputdatalength = crt->inputdatalength;
741 rc = get_rsa_crt_fc(crt, &func_code);
747 spin_lock(&zcrypt_list_lock);
748 for_each_zcrypt_card(zc) {
749 /* Check for online accelarator and CCA cards */
750 if (!zc->online || !(zc->card->functions & 0x18000000))
752 /* Check for size limits */
753 if (zc->min_mod_size > crt->inputdatalength ||
754 zc->max_mod_size < crt->inputdatalength)
756 /* check if device node has admission for this card */
757 if (!zcrypt_check_card(perms, zc->card->id))
759 /* get weight index of the card device */
760 weight = zc->speed_rating[func_code];
761 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
763 for_each_zcrypt_queue(zq, zc) {
764 /* check if device is online and eligible */
765 if (!zq->online || !zq->ops->rsa_modexpo_crt)
767 /* check if device node has admission for this queue */
768 if (!zcrypt_check_queue(perms,
769 AP_QID_QUEUE(zq->queue->qid)))
771 if (zcrypt_queue_compare(zq, pref_zq,
772 weight, pref_weight))
776 pref_weight = weight;
779 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
780 spin_unlock(&zcrypt_list_lock);
787 qid = pref_zq->queue->qid;
788 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
790 spin_lock(&zcrypt_list_lock);
791 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
792 spin_unlock(&zcrypt_list_lock);
795 trace_s390_zcrypt_rep(crt, func_code, rc,
796 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
800 static long _zcrypt_send_cprb(struct ap_perms *perms,
801 struct ica_xcRB *xcRB)
803 struct zcrypt_card *zc, *pref_zc;
804 struct zcrypt_queue *zq, *pref_zq;
805 struct ap_message ap_msg;
806 unsigned int weight, pref_weight;
807 unsigned int func_code;
808 unsigned short *domain, tdom;
809 int qid = 0, rc = -ENODEV;
812 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
815 ap_init_message(&ap_msg);
816 rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
821 * If a valid target domain is set and this domain is NOT a usage
822 * domain but a control only domain, use the default domain as target.
825 if (tdom >= 0 && tdom < AP_DOMAINS &&
826 !ap_test_config_usage_domain(tdom) &&
827 ap_test_config_ctrl_domain(tdom) &&
828 ap_domain_index >= 0)
829 tdom = ap_domain_index;
833 spin_lock(&zcrypt_list_lock);
834 for_each_zcrypt_card(zc) {
835 /* Check for online CCA cards */
836 if (!zc->online || !(zc->card->functions & 0x10000000))
838 /* Check for user selected CCA card */
839 if (xcRB->user_defined != AUTOSELECT &&
840 xcRB->user_defined != zc->card->id)
842 /* check if device node has admission for this card */
843 if (!zcrypt_check_card(perms, zc->card->id))
845 /* get weight index of the card device */
846 weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
847 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
849 for_each_zcrypt_queue(zq, zc) {
850 /* check if device is online and eligible */
852 !zq->ops->send_cprb ||
853 (tdom != (unsigned short) AUTOSELECT &&
854 tdom != AP_QID_QUEUE(zq->queue->qid)))
856 /* check if device node has admission for this queue */
857 if (!zcrypt_check_queue(perms,
858 AP_QID_QUEUE(zq->queue->qid)))
860 if (zcrypt_queue_compare(zq, pref_zq,
861 weight, pref_weight))
865 pref_weight = weight;
868 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
869 spin_unlock(&zcrypt_list_lock);
876 /* in case of auto select, provide the correct domain */
877 qid = pref_zq->queue->qid;
878 if (*domain == (unsigned short) AUTOSELECT)
879 *domain = AP_QID_QUEUE(qid);
881 rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
883 spin_lock(&zcrypt_list_lock);
884 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
885 spin_unlock(&zcrypt_list_lock);
888 ap_release_message(&ap_msg);
889 trace_s390_zcrypt_rep(xcRB, func_code, rc,
890 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
894 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
896 return _zcrypt_send_cprb(&ap_perms, xcRB);
898 EXPORT_SYMBOL(zcrypt_send_cprb);
900 static bool is_desired_ep11_card(unsigned int dev_id,
901 unsigned short target_num,
902 struct ep11_target_dev *targets)
904 while (target_num-- > 0) {
905 if (dev_id == targets->ap_id)
912 static bool is_desired_ep11_queue(unsigned int dev_qid,
913 unsigned short target_num,
914 struct ep11_target_dev *targets)
916 while (target_num-- > 0) {
917 if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
924 static long zcrypt_send_ep11_cprb(struct ap_perms *perms,
925 struct ep11_urb *xcrb)
927 struct zcrypt_card *zc, *pref_zc;
928 struct zcrypt_queue *zq, *pref_zq;
929 struct ep11_target_dev *targets;
930 unsigned short target_num;
931 unsigned int weight, pref_weight;
932 unsigned int func_code;
933 struct ap_message ap_msg;
934 int qid = 0, rc = -ENODEV;
937 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
939 ap_init_message(&ap_msg);
941 target_num = (unsigned short) xcrb->targets_num;
943 /* empty list indicates autoselect (all available targets) */
945 if (target_num != 0) {
946 struct ep11_target_dev __user *uptr;
948 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
955 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
956 if (copy_from_user(targets, uptr,
957 target_num * sizeof(*targets))) {
964 rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
970 spin_lock(&zcrypt_list_lock);
971 for_each_zcrypt_card(zc) {
972 /* Check for online EP11 cards */
973 if (!zc->online || !(zc->card->functions & 0x04000000))
975 /* Check for user selected EP11 card */
977 !is_desired_ep11_card(zc->card->id, target_num, targets))
979 /* check if device node has admission for this card */
980 if (!zcrypt_check_card(perms, zc->card->id))
982 /* get weight index of the card device */
983 weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
984 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
986 for_each_zcrypt_queue(zq, zc) {
987 /* check if device is online and eligible */
989 !zq->ops->send_ep11_cprb ||
991 !is_desired_ep11_queue(zq->queue->qid,
992 target_num, targets)))
994 /* check if device node has admission for this queue */
995 if (!zcrypt_check_queue(perms,
996 AP_QID_QUEUE(zq->queue->qid)))
998 if (zcrypt_queue_compare(zq, pref_zq,
999 weight, pref_weight))
1003 pref_weight = weight;
1006 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1007 spin_unlock(&zcrypt_list_lock);
1014 qid = pref_zq->queue->qid;
1015 rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
1017 spin_lock(&zcrypt_list_lock);
1018 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1019 spin_unlock(&zcrypt_list_lock);
1024 ap_release_message(&ap_msg);
1025 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1026 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1030 static long zcrypt_rng(char *buffer)
1032 struct zcrypt_card *zc, *pref_zc;
1033 struct zcrypt_queue *zq, *pref_zq;
1034 unsigned int weight, pref_weight;
1035 unsigned int func_code;
1036 struct ap_message ap_msg;
1037 unsigned int domain;
1038 int qid = 0, rc = -ENODEV;
1041 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1043 ap_init_message(&ap_msg);
1044 rc = get_rng_fc(&ap_msg, &func_code, &domain);
1050 spin_lock(&zcrypt_list_lock);
1051 for_each_zcrypt_card(zc) {
1052 /* Check for online CCA cards */
1053 if (!zc->online || !(zc->card->functions & 0x10000000))
1055 /* get weight index of the card device */
1056 weight = zc->speed_rating[func_code];
1057 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1059 for_each_zcrypt_queue(zq, zc) {
1060 /* check if device is online and eligible */
1061 if (!zq->online || !zq->ops->rng)
1063 if (zcrypt_queue_compare(zq, pref_zq,
1064 weight, pref_weight))
1068 pref_weight = weight;
1071 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1072 spin_unlock(&zcrypt_list_lock);
1079 qid = pref_zq->queue->qid;
1080 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1082 spin_lock(&zcrypt_list_lock);
1083 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1084 spin_unlock(&zcrypt_list_lock);
1087 ap_release_message(&ap_msg);
1088 trace_s390_zcrypt_rep(buffer, func_code, rc,
1089 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1093 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1095 struct zcrypt_card *zc;
1096 struct zcrypt_queue *zq;
1097 struct zcrypt_device_status *stat;
1100 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1101 * sizeof(struct zcrypt_device_status));
1103 spin_lock(&zcrypt_list_lock);
1104 for_each_zcrypt_card(zc) {
1105 for_each_zcrypt_queue(zq, zc) {
1106 card = AP_QID_CARD(zq->queue->qid);
1107 if (card >= MAX_ZDEV_CARDIDS)
1109 queue = AP_QID_QUEUE(zq->queue->qid);
1110 stat = &devstatus[card * AP_DOMAINS + queue];
1111 stat->hwtype = zc->card->ap_dev.device_type;
1112 stat->functions = zc->card->functions >> 26;
1113 stat->qid = zq->queue->qid;
1114 stat->online = zq->online ? 0x01 : 0x00;
1117 spin_unlock(&zcrypt_list_lock);
1120 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1122 struct zcrypt_card *zc;
1123 struct zcrypt_queue *zq;
1124 struct zcrypt_device_status_ext *stat;
1127 memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1128 * sizeof(struct zcrypt_device_status_ext));
1130 spin_lock(&zcrypt_list_lock);
1131 for_each_zcrypt_card(zc) {
1132 for_each_zcrypt_queue(zq, zc) {
1133 card = AP_QID_CARD(zq->queue->qid);
1134 queue = AP_QID_QUEUE(zq->queue->qid);
1135 stat = &devstatus[card * AP_DOMAINS + queue];
1136 stat->hwtype = zc->card->ap_dev.device_type;
1137 stat->functions = zc->card->functions >> 26;
1138 stat->qid = zq->queue->qid;
1139 stat->online = zq->online ? 0x01 : 0x00;
1142 spin_unlock(&zcrypt_list_lock);
1144 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1146 int zcrypt_device_status_ext(int card, int queue,
1147 struct zcrypt_device_status_ext *devstat)
1149 struct zcrypt_card *zc;
1150 struct zcrypt_queue *zq;
1152 memset(devstat, 0, sizeof(*devstat));
1154 spin_lock(&zcrypt_list_lock);
1155 for_each_zcrypt_card(zc) {
1156 for_each_zcrypt_queue(zq, zc) {
1157 if (card == AP_QID_CARD(zq->queue->qid) &&
1158 queue == AP_QID_QUEUE(zq->queue->qid)) {
1159 devstat->hwtype = zc->card->ap_dev.device_type;
1160 devstat->functions = zc->card->functions >> 26;
1161 devstat->qid = zq->queue->qid;
1162 devstat->online = zq->online ? 0x01 : 0x00;
1163 spin_unlock(&zcrypt_list_lock);
1168 spin_unlock(&zcrypt_list_lock);
1172 EXPORT_SYMBOL(zcrypt_device_status_ext);
1174 static void zcrypt_status_mask(char status[], size_t max_adapters)
1176 struct zcrypt_card *zc;
1177 struct zcrypt_queue *zq;
1180 memset(status, 0, max_adapters);
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 (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1186 || card >= max_adapters)
1188 status[card] = zc->online ? zc->user_space_type : 0x0d;
1191 spin_unlock(&zcrypt_list_lock);
1194 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1196 struct zcrypt_card *zc;
1197 struct zcrypt_queue *zq;
1200 memset(qdepth, 0, max_adapters);
1201 spin_lock(&zcrypt_list_lock);
1203 for_each_zcrypt_card(zc) {
1204 for_each_zcrypt_queue(zq, zc) {
1205 card = AP_QID_CARD(zq->queue->qid);
1206 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1207 || card >= max_adapters)
1209 spin_lock(&zq->queue->lock);
1211 zq->queue->pendingq_count +
1212 zq->queue->requestq_count;
1213 spin_unlock(&zq->queue->lock);
1217 spin_unlock(&zcrypt_list_lock);
1220 static void zcrypt_perdev_reqcnt(int reqcnt[], size_t max_adapters)
1222 struct zcrypt_card *zc;
1223 struct zcrypt_queue *zq;
1226 memset(reqcnt, 0, sizeof(int) * max_adapters);
1227 spin_lock(&zcrypt_list_lock);
1229 for_each_zcrypt_card(zc) {
1230 for_each_zcrypt_queue(zq, zc) {
1231 card = AP_QID_CARD(zq->queue->qid);
1232 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1233 || card >= max_adapters)
1235 spin_lock(&zq->queue->lock);
1236 reqcnt[card] = zq->queue->total_request_count;
1237 spin_unlock(&zq->queue->lock);
1241 spin_unlock(&zcrypt_list_lock);
1244 static int zcrypt_pendingq_count(void)
1246 struct zcrypt_card *zc;
1247 struct zcrypt_queue *zq;
1251 spin_lock(&zcrypt_list_lock);
1253 for_each_zcrypt_card(zc) {
1254 for_each_zcrypt_queue(zq, zc) {
1255 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1257 spin_lock(&zq->queue->lock);
1258 pendingq_count += zq->queue->pendingq_count;
1259 spin_unlock(&zq->queue->lock);
1263 spin_unlock(&zcrypt_list_lock);
1264 return pendingq_count;
1267 static int zcrypt_requestq_count(void)
1269 struct zcrypt_card *zc;
1270 struct zcrypt_queue *zq;
1274 spin_lock(&zcrypt_list_lock);
1276 for_each_zcrypt_card(zc) {
1277 for_each_zcrypt_queue(zq, zc) {
1278 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1280 spin_lock(&zq->queue->lock);
1281 requestq_count += zq->queue->requestq_count;
1282 spin_unlock(&zq->queue->lock);
1286 spin_unlock(&zcrypt_list_lock);
1287 return requestq_count;
1290 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1294 struct ap_perms *perms =
1295 (struct ap_perms *) filp->private_data;
1297 rc = zcrypt_check_ioctl(perms, cmd);
1302 case ICARSAMODEXPO: {
1303 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1304 struct ica_rsa_modexpo mex;
1306 if (copy_from_user(&mex, umex, sizeof(mex)))
1309 rc = zcrypt_rsa_modexpo(perms, &mex);
1310 } while (rc == -EAGAIN);
1311 /* on failure: retry once again after a requested rescan */
1312 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1314 rc = zcrypt_rsa_modexpo(perms, &mex);
1315 } while (rc == -EAGAIN);
1317 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1320 return put_user(mex.outputdatalength, &umex->outputdatalength);
1323 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1324 struct ica_rsa_modexpo_crt crt;
1326 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1329 rc = zcrypt_rsa_crt(perms, &crt);
1330 } while (rc == -EAGAIN);
1331 /* on failure: retry once again after a requested rescan */
1332 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1334 rc = zcrypt_rsa_crt(perms, &crt);
1335 } while (rc == -EAGAIN);
1337 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1340 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1342 case ZSECSENDCPRB: {
1343 struct ica_xcRB __user *uxcRB = (void __user *) arg;
1344 struct ica_xcRB xcRB;
1346 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1349 rc = _zcrypt_send_cprb(perms, &xcRB);
1350 } while (rc == -EAGAIN);
1351 /* on failure: retry once again after a requested rescan */
1352 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1354 rc = _zcrypt_send_cprb(perms, &xcRB);
1355 } while (rc == -EAGAIN);
1357 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1359 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1363 case ZSENDEP11CPRB: {
1364 struct ep11_urb __user *uxcrb = (void __user *)arg;
1365 struct ep11_urb xcrb;
1367 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1370 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1371 } while (rc == -EAGAIN);
1372 /* on failure: retry once again after a requested rescan */
1373 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1375 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1376 } while (rc == -EAGAIN);
1378 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1379 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1383 case ZCRYPT_DEVICE_STATUS: {
1384 struct zcrypt_device_status_ext *device_status;
1385 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1386 * sizeof(struct zcrypt_device_status_ext);
1388 device_status = kzalloc(total_size, GFP_KERNEL);
1391 zcrypt_device_status_mask_ext(device_status);
1392 if (copy_to_user((char __user *) arg, device_status,
1395 kfree(device_status);
1398 case ZCRYPT_STATUS_MASK: {
1399 char status[AP_DEVICES];
1401 zcrypt_status_mask(status, AP_DEVICES);
1402 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1406 case ZCRYPT_QDEPTH_MASK: {
1407 char qdepth[AP_DEVICES];
1409 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1410 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1414 case ZCRYPT_PERDEV_REQCNT: {
1417 reqcnt = kcalloc(AP_DEVICES, sizeof(int), GFP_KERNEL);
1420 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1421 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1426 case Z90STAT_REQUESTQ_COUNT:
1427 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1428 case Z90STAT_PENDINGQ_COUNT:
1429 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1430 case Z90STAT_TOTALOPEN_COUNT:
1431 return put_user(atomic_read(&zcrypt_open_count),
1432 (int __user *) arg);
1433 case Z90STAT_DOMAIN_INDEX:
1434 return put_user(ap_domain_index, (int __user *) arg);
1438 case ZDEVICESTATUS: {
1439 /* the old ioctl supports only 64 adapters */
1440 struct zcrypt_device_status *device_status;
1441 size_t total_size = MAX_ZDEV_ENTRIES
1442 * sizeof(struct zcrypt_device_status);
1444 device_status = kzalloc(total_size, GFP_KERNEL);
1447 zcrypt_device_status_mask(device_status);
1448 if (copy_to_user((char __user *) arg, device_status,
1451 kfree(device_status);
1454 case Z90STAT_STATUS_MASK: {
1455 /* the old ioctl supports only 64 adapters */
1456 char status[MAX_ZDEV_CARDIDS];
1458 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1459 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1463 case Z90STAT_QDEPTH_MASK: {
1464 /* the old ioctl supports only 64 adapters */
1465 char qdepth[MAX_ZDEV_CARDIDS];
1467 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1468 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1472 case Z90STAT_PERDEV_REQCNT: {
1473 /* the old ioctl supports only 64 adapters */
1474 int reqcnt[MAX_ZDEV_CARDIDS];
1476 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1477 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1481 /* unknown ioctl number */
1483 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1484 return -ENOIOCTLCMD;
1488 #ifdef CONFIG_COMPAT
1490 * ioctl32 conversion routines
1492 struct compat_ica_rsa_modexpo {
1493 compat_uptr_t inputdata;
1494 unsigned int inputdatalength;
1495 compat_uptr_t outputdata;
1496 unsigned int outputdatalength;
1497 compat_uptr_t b_key;
1498 compat_uptr_t n_modulus;
1501 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1502 unsigned int cmd, unsigned long arg)
1504 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1505 struct compat_ica_rsa_modexpo mex32;
1506 struct ica_rsa_modexpo mex64;
1509 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1511 mex64.inputdata = compat_ptr(mex32.inputdata);
1512 mex64.inputdatalength = mex32.inputdatalength;
1513 mex64.outputdata = compat_ptr(mex32.outputdata);
1514 mex64.outputdatalength = mex32.outputdatalength;
1515 mex64.b_key = compat_ptr(mex32.b_key);
1516 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1518 rc = zcrypt_rsa_modexpo(perms, &mex64);
1519 } while (rc == -EAGAIN);
1520 /* on failure: retry once again after a requested rescan */
1521 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1523 rc = zcrypt_rsa_modexpo(perms, &mex64);
1524 } while (rc == -EAGAIN);
1527 return put_user(mex64.outputdatalength,
1528 &umex32->outputdatalength);
1531 struct compat_ica_rsa_modexpo_crt {
1532 compat_uptr_t inputdata;
1533 unsigned int inputdatalength;
1534 compat_uptr_t outputdata;
1535 unsigned int outputdatalength;
1536 compat_uptr_t bp_key;
1537 compat_uptr_t bq_key;
1538 compat_uptr_t np_prime;
1539 compat_uptr_t nq_prime;
1540 compat_uptr_t u_mult_inv;
1543 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1544 unsigned int cmd, unsigned long arg)
1546 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1547 struct compat_ica_rsa_modexpo_crt crt32;
1548 struct ica_rsa_modexpo_crt crt64;
1551 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1553 crt64.inputdata = compat_ptr(crt32.inputdata);
1554 crt64.inputdatalength = crt32.inputdatalength;
1555 crt64.outputdata = compat_ptr(crt32.outputdata);
1556 crt64.outputdatalength = crt32.outputdatalength;
1557 crt64.bp_key = compat_ptr(crt32.bp_key);
1558 crt64.bq_key = compat_ptr(crt32.bq_key);
1559 crt64.np_prime = compat_ptr(crt32.np_prime);
1560 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1561 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1563 rc = zcrypt_rsa_crt(perms, &crt64);
1564 } while (rc == -EAGAIN);
1565 /* on failure: retry once again after a requested rescan */
1566 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1568 rc = zcrypt_rsa_crt(perms, &crt64);
1569 } while (rc == -EAGAIN);
1572 return put_user(crt64.outputdatalength,
1573 &ucrt32->outputdatalength);
1576 struct compat_ica_xcRB {
1577 unsigned short agent_ID;
1578 unsigned int user_defined;
1579 unsigned short request_ID;
1580 unsigned int request_control_blk_length;
1581 unsigned char padding1[16 - sizeof(compat_uptr_t)];
1582 compat_uptr_t request_control_blk_addr;
1583 unsigned int request_data_length;
1584 char padding2[16 - sizeof(compat_uptr_t)];
1585 compat_uptr_t request_data_address;
1586 unsigned int reply_control_blk_length;
1587 char padding3[16 - sizeof(compat_uptr_t)];
1588 compat_uptr_t reply_control_blk_addr;
1589 unsigned int reply_data_length;
1590 char padding4[16 - sizeof(compat_uptr_t)];
1591 compat_uptr_t reply_data_addr;
1592 unsigned short priority_window;
1593 unsigned int status;
1596 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1597 unsigned int cmd, unsigned long arg)
1599 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1600 struct compat_ica_xcRB xcRB32;
1601 struct ica_xcRB xcRB64;
1604 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1606 xcRB64.agent_ID = xcRB32.agent_ID;
1607 xcRB64.user_defined = xcRB32.user_defined;
1608 xcRB64.request_ID = xcRB32.request_ID;
1609 xcRB64.request_control_blk_length =
1610 xcRB32.request_control_blk_length;
1611 xcRB64.request_control_blk_addr =
1612 compat_ptr(xcRB32.request_control_blk_addr);
1613 xcRB64.request_data_length =
1614 xcRB32.request_data_length;
1615 xcRB64.request_data_address =
1616 compat_ptr(xcRB32.request_data_address);
1617 xcRB64.reply_control_blk_length =
1618 xcRB32.reply_control_blk_length;
1619 xcRB64.reply_control_blk_addr =
1620 compat_ptr(xcRB32.reply_control_blk_addr);
1621 xcRB64.reply_data_length = xcRB32.reply_data_length;
1622 xcRB64.reply_data_addr =
1623 compat_ptr(xcRB32.reply_data_addr);
1624 xcRB64.priority_window = xcRB32.priority_window;
1625 xcRB64.status = xcRB32.status;
1627 rc = _zcrypt_send_cprb(perms, &xcRB64);
1628 } while (rc == -EAGAIN);
1629 /* on failure: retry once again after a requested rescan */
1630 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1632 rc = _zcrypt_send_cprb(perms, &xcRB64);
1633 } while (rc == -EAGAIN);
1634 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1635 xcRB32.reply_data_length = xcRB64.reply_data_length;
1636 xcRB32.status = xcRB64.status;
1637 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1642 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1646 struct ap_perms *perms =
1647 (struct ap_perms *) filp->private_data;
1649 rc = zcrypt_check_ioctl(perms, cmd);
1653 if (cmd == ICARSAMODEXPO)
1654 return trans_modexpo32(perms, filp, cmd, arg);
1655 if (cmd == ICARSACRT)
1656 return trans_modexpo_crt32(perms, filp, cmd, arg);
1657 if (cmd == ZSECSENDCPRB)
1658 return trans_xcRB32(perms, filp, cmd, arg);
1659 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1664 * Misc device file operations.
1666 static const struct file_operations zcrypt_fops = {
1667 .owner = THIS_MODULE,
1668 .read = zcrypt_read,
1669 .write = zcrypt_write,
1670 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1671 #ifdef CONFIG_COMPAT
1672 .compat_ioctl = zcrypt_compat_ioctl,
1674 .open = zcrypt_open,
1675 .release = zcrypt_release,
1676 .llseek = no_llseek,
1682 static struct miscdevice zcrypt_misc_device = {
1683 .minor = MISC_DYNAMIC_MINOR,
1685 .fops = &zcrypt_fops,
1688 static int zcrypt_rng_device_count;
1689 static u32 *zcrypt_rng_buffer;
1690 static int zcrypt_rng_buffer_index;
1691 static DEFINE_MUTEX(zcrypt_rng_mutex);
1693 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1698 * We don't need locking here because the RNG API guarantees serialized
1699 * read method calls.
1701 if (zcrypt_rng_buffer_index == 0) {
1702 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1703 /* on failure: retry once again after a requested rescan */
1704 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1705 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1708 zcrypt_rng_buffer_index = rc / sizeof(*data);
1710 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1711 return sizeof(*data);
1714 static struct hwrng zcrypt_rng_dev = {
1716 .data_read = zcrypt_rng_data_read,
1720 int zcrypt_rng_device_add(void)
1724 mutex_lock(&zcrypt_rng_mutex);
1725 if (zcrypt_rng_device_count == 0) {
1726 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1727 if (!zcrypt_rng_buffer) {
1731 zcrypt_rng_buffer_index = 0;
1732 if (!zcrypt_hwrng_seed)
1733 zcrypt_rng_dev.quality = 0;
1734 rc = hwrng_register(&zcrypt_rng_dev);
1737 zcrypt_rng_device_count = 1;
1739 zcrypt_rng_device_count++;
1740 mutex_unlock(&zcrypt_rng_mutex);
1744 free_page((unsigned long) zcrypt_rng_buffer);
1746 mutex_unlock(&zcrypt_rng_mutex);
1750 void zcrypt_rng_device_remove(void)
1752 mutex_lock(&zcrypt_rng_mutex);
1753 zcrypt_rng_device_count--;
1754 if (zcrypt_rng_device_count == 0) {
1755 hwrng_unregister(&zcrypt_rng_dev);
1756 free_page((unsigned long) zcrypt_rng_buffer);
1758 mutex_unlock(&zcrypt_rng_mutex);
1761 int __init zcrypt_debug_init(void)
1763 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1764 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1765 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1766 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1771 void zcrypt_debug_exit(void)
1773 debug_unregister(zcrypt_dbf_info);
1776 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1778 static int __init zcdn_init(void)
1782 /* create a new class 'zcrypt' */
1783 zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1784 if (IS_ERR(zcrypt_class)) {
1785 rc = PTR_ERR(zcrypt_class);
1786 goto out_class_create_failed;
1788 zcrypt_class->dev_release = zcdn_device_release;
1790 /* alloc device minor range */
1791 rc = alloc_chrdev_region(&zcrypt_devt,
1792 0, ZCRYPT_MAX_MINOR_NODES,
1795 goto out_alloc_chrdev_failed;
1797 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1798 zcrypt_cdev.owner = THIS_MODULE;
1799 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1801 goto out_cdev_add_failed;
1803 /* need some class specific sysfs attributes */
1804 rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1806 goto out_class_create_file_1_failed;
1807 rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1809 goto out_class_create_file_2_failed;
1813 out_class_create_file_2_failed:
1814 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1815 out_class_create_file_1_failed:
1816 cdev_del(&zcrypt_cdev);
1817 out_cdev_add_failed:
1818 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1819 out_alloc_chrdev_failed:
1820 class_destroy(zcrypt_class);
1821 out_class_create_failed:
1825 static void zcdn_exit(void)
1827 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1828 class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1830 cdev_del(&zcrypt_cdev);
1831 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1832 class_destroy(zcrypt_class);
1838 * zcrypt_api_init(): Module initialization.
1840 * The module initialization code.
1842 int __init zcrypt_api_init(void)
1846 rc = zcrypt_debug_init();
1850 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1856 /* Register the request sprayer. */
1857 rc = misc_register(&zcrypt_misc_device);
1859 goto out_misc_register_failed;
1861 zcrypt_msgtype6_init();
1862 zcrypt_msgtype50_init();
1866 out_misc_register_failed:
1867 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1870 zcrypt_debug_exit();
1876 * zcrypt_api_exit(): Module termination.
1878 * The module termination code.
1880 void __exit zcrypt_api_exit(void)
1882 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1885 misc_deregister(&zcrypt_misc_device);
1886 zcrypt_msgtype6_exit();
1887 zcrypt_msgtype50_exit();
1888 zcrypt_ccamisc_exit();
1889 zcrypt_debug_exit();
1892 module_init(zcrypt_api_init);
1893 module_exit(zcrypt_api_exit);