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 nonseekable_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 if (mutex_lock_interruptible(&ap_perms_mutex))
544 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
545 mutex_unlock(&ap_perms_mutex);
547 /* 2 puts here: one for find, one for open */
548 put_device(&zcdndev->device);
549 put_device(&zcdndev->device);
554 atomic_dec(&zcrypt_open_count);
558 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
562 int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
564 if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
565 if (test_bit_inv(ioctlnr, perms->ioctlm))
571 "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
577 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
579 return test_bit_inv(card, perms->apm) ? true : false;
582 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
584 return test_bit_inv(queue, perms->aqm) ? true : false;
587 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
588 struct zcrypt_queue *zq,
589 struct module **pmod,
592 if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
594 zcrypt_queue_get(zq);
595 get_device(&zq->queue->ap_dev.device);
596 atomic_add(weight, &zc->load);
597 atomic_add(weight, &zq->load);
599 *pmod = zq->queue->ap_dev.drv->driver.owner;
603 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
604 struct zcrypt_queue *zq,
609 atomic_sub(weight, &zc->load);
610 atomic_sub(weight, &zq->load);
611 put_device(&zq->queue->ap_dev.device);
612 zcrypt_queue_put(zq);
616 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
617 struct zcrypt_card *pref_zc,
619 unsigned int pref_weight)
623 weight += atomic_read(&zc->load);
624 pref_weight += atomic_read(&pref_zc->load);
625 if (weight == pref_weight)
626 return atomic_read(&zc->card->total_request_count) >
627 atomic_read(&pref_zc->card->total_request_count);
628 return weight > pref_weight;
631 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
632 struct zcrypt_queue *pref_zq,
634 unsigned int pref_weight)
638 weight += atomic_read(&zq->load);
639 pref_weight += atomic_read(&pref_zq->load);
640 if (weight == pref_weight)
641 return zq->queue->total_request_count >
642 pref_zq->queue->total_request_count;
643 return weight > pref_weight;
649 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
650 struct ica_rsa_modexpo *mex)
652 struct zcrypt_card *zc, *pref_zc;
653 struct zcrypt_queue *zq, *pref_zq;
654 unsigned int weight, pref_weight;
655 unsigned int func_code;
656 int qid = 0, rc = -ENODEV;
659 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
661 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) {
750 * As long as outputdatalength is big enough, we can set the
751 * outputdatalength equal to the inputdatalength, since that is the
752 * number of bytes we will copy in any case
754 crt->outputdatalength = crt->inputdatalength;
756 rc = get_rsa_crt_fc(crt, &func_code);
762 spin_lock(&zcrypt_list_lock);
763 for_each_zcrypt_card(zc) {
764 /* Check for online accelarator and CCA cards */
765 if (!zc->online || !(zc->card->functions & 0x18000000))
767 /* Check for size limits */
768 if (zc->min_mod_size > crt->inputdatalength ||
769 zc->max_mod_size < crt->inputdatalength)
771 /* check if device node has admission for this card */
772 if (!zcrypt_check_card(perms, zc->card->id))
774 /* get weight index of the card device */
775 weight = zc->speed_rating[func_code];
776 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
778 for_each_zcrypt_queue(zq, zc) {
779 /* check if device is online and eligible */
780 if (!zq->online || !zq->ops->rsa_modexpo_crt)
782 /* check if device node has admission for this queue */
783 if (!zcrypt_check_queue(perms,
784 AP_QID_QUEUE(zq->queue->qid)))
786 if (zcrypt_queue_compare(zq, pref_zq,
787 weight, pref_weight))
791 pref_weight = weight;
794 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
795 spin_unlock(&zcrypt_list_lock);
802 qid = pref_zq->queue->qid;
803 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
805 spin_lock(&zcrypt_list_lock);
806 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
807 spin_unlock(&zcrypt_list_lock);
810 trace_s390_zcrypt_rep(crt, func_code, rc,
811 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
815 static long _zcrypt_send_cprb(struct ap_perms *perms,
816 struct ica_xcRB *xcRB)
818 struct zcrypt_card *zc, *pref_zc;
819 struct zcrypt_queue *zq, *pref_zq;
820 struct ap_message ap_msg;
821 unsigned int weight, pref_weight;
822 unsigned int func_code;
823 unsigned short *domain;
824 int qid = 0, rc = -ENODEV;
827 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
830 ap_init_message(&ap_msg);
831 rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
837 spin_lock(&zcrypt_list_lock);
838 for_each_zcrypt_card(zc) {
839 /* Check for online CCA cards */
840 if (!zc->online || !(zc->card->functions & 0x10000000))
842 /* Check for user selected CCA card */
843 if (xcRB->user_defined != AUTOSELECT &&
844 xcRB->user_defined != zc->card->id)
846 /* check if device node has admission for this card */
847 if (!zcrypt_check_card(perms, zc->card->id))
849 /* get weight index of the card device */
850 weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
851 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
853 for_each_zcrypt_queue(zq, zc) {
854 /* check if device is online and eligible */
856 !zq->ops->send_cprb ||
857 ((*domain != (unsigned short) AUTOSELECT) &&
858 (*domain != AP_QID_QUEUE(zq->queue->qid))))
860 /* check if device node has admission for this queue */
861 if (!zcrypt_check_queue(perms,
862 AP_QID_QUEUE(zq->queue->qid)))
864 if (zcrypt_queue_compare(zq, pref_zq,
865 weight, pref_weight))
869 pref_weight = weight;
872 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
873 spin_unlock(&zcrypt_list_lock);
880 /* in case of auto select, provide the correct domain */
881 qid = pref_zq->queue->qid;
882 if (*domain == (unsigned short) AUTOSELECT)
883 *domain = AP_QID_QUEUE(qid);
885 rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
887 spin_lock(&zcrypt_list_lock);
888 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
889 spin_unlock(&zcrypt_list_lock);
892 ap_release_message(&ap_msg);
893 trace_s390_zcrypt_rep(xcRB, func_code, rc,
894 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
898 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
900 return _zcrypt_send_cprb(&ap_perms, xcRB);
902 EXPORT_SYMBOL(zcrypt_send_cprb);
904 static bool is_desired_ep11_card(unsigned int dev_id,
905 unsigned short target_num,
906 struct ep11_target_dev *targets)
908 while (target_num-- > 0) {
909 if (dev_id == targets->ap_id)
916 static bool is_desired_ep11_queue(unsigned int dev_qid,
917 unsigned short target_num,
918 struct ep11_target_dev *targets)
920 while (target_num-- > 0) {
921 if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
928 static long zcrypt_send_ep11_cprb(struct ap_perms *perms,
929 struct ep11_urb *xcrb)
931 struct zcrypt_card *zc, *pref_zc;
932 struct zcrypt_queue *zq, *pref_zq;
933 struct ep11_target_dev *targets;
934 unsigned short target_num;
935 unsigned int weight, pref_weight;
936 unsigned int func_code;
937 struct ap_message ap_msg;
938 int qid = 0, rc = -ENODEV;
941 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
943 ap_init_message(&ap_msg);
945 target_num = (unsigned short) xcrb->targets_num;
947 /* empty list indicates autoselect (all available targets) */
949 if (target_num != 0) {
950 struct ep11_target_dev __user *uptr;
952 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
958 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
959 if (copy_from_user(targets, uptr,
960 target_num * sizeof(*targets))) {
966 rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
972 spin_lock(&zcrypt_list_lock);
973 for_each_zcrypt_card(zc) {
974 /* Check for online EP11 cards */
975 if (!zc->online || !(zc->card->functions & 0x04000000))
977 /* Check for user selected EP11 card */
979 !is_desired_ep11_card(zc->card->id, target_num, targets))
981 /* check if device node has admission for this card */
982 if (!zcrypt_check_card(perms, zc->card->id))
984 /* get weight index of the card device */
985 weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
986 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
988 for_each_zcrypt_queue(zq, zc) {
989 /* check if device is online and eligible */
991 !zq->ops->send_ep11_cprb ||
993 !is_desired_ep11_queue(zq->queue->qid,
994 target_num, targets)))
996 /* check if device node has admission for this queue */
997 if (!zcrypt_check_queue(perms,
998 AP_QID_QUEUE(zq->queue->qid)))
1000 if (zcrypt_queue_compare(zq, pref_zq,
1001 weight, pref_weight))
1005 pref_weight = weight;
1008 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1009 spin_unlock(&zcrypt_list_lock);
1016 qid = pref_zq->queue->qid;
1017 rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
1019 spin_lock(&zcrypt_list_lock);
1020 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1021 spin_unlock(&zcrypt_list_lock);
1026 ap_release_message(&ap_msg);
1027 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1028 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1032 static long zcrypt_rng(char *buffer)
1034 struct zcrypt_card *zc, *pref_zc;
1035 struct zcrypt_queue *zq, *pref_zq;
1036 unsigned int weight, pref_weight;
1037 unsigned int func_code;
1038 struct ap_message ap_msg;
1039 unsigned int domain;
1040 int qid = 0, rc = -ENODEV;
1043 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1045 ap_init_message(&ap_msg);
1046 rc = get_rng_fc(&ap_msg, &func_code, &domain);
1052 spin_lock(&zcrypt_list_lock);
1053 for_each_zcrypt_card(zc) {
1054 /* Check for online CCA cards */
1055 if (!zc->online || !(zc->card->functions & 0x10000000))
1057 /* get weight index of the card device */
1058 weight = zc->speed_rating[func_code];
1059 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1061 for_each_zcrypt_queue(zq, zc) {
1062 /* check if device is online and eligible */
1063 if (!zq->online || !zq->ops->rng)
1065 if (zcrypt_queue_compare(zq, pref_zq,
1066 weight, pref_weight))
1070 pref_weight = weight;
1073 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1074 spin_unlock(&zcrypt_list_lock);
1081 qid = pref_zq->queue->qid;
1082 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1084 spin_lock(&zcrypt_list_lock);
1085 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1086 spin_unlock(&zcrypt_list_lock);
1089 ap_release_message(&ap_msg);
1090 trace_s390_zcrypt_rep(buffer, func_code, rc,
1091 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1095 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1097 struct zcrypt_card *zc;
1098 struct zcrypt_queue *zq;
1099 struct zcrypt_device_status *stat;
1102 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1103 * sizeof(struct zcrypt_device_status));
1105 spin_lock(&zcrypt_list_lock);
1106 for_each_zcrypt_card(zc) {
1107 for_each_zcrypt_queue(zq, zc) {
1108 card = AP_QID_CARD(zq->queue->qid);
1109 if (card >= MAX_ZDEV_CARDIDS)
1111 queue = AP_QID_QUEUE(zq->queue->qid);
1112 stat = &devstatus[card * AP_DOMAINS + queue];
1113 stat->hwtype = zc->card->ap_dev.device_type;
1114 stat->functions = zc->card->functions >> 26;
1115 stat->qid = zq->queue->qid;
1116 stat->online = zq->online ? 0x01 : 0x00;
1119 spin_unlock(&zcrypt_list_lock);
1122 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1124 struct zcrypt_card *zc;
1125 struct zcrypt_queue *zq;
1126 struct zcrypt_device_status_ext *stat;
1129 memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1130 * sizeof(struct zcrypt_device_status_ext));
1132 spin_lock(&zcrypt_list_lock);
1133 for_each_zcrypt_card(zc) {
1134 for_each_zcrypt_queue(zq, zc) {
1135 card = AP_QID_CARD(zq->queue->qid);
1136 queue = AP_QID_QUEUE(zq->queue->qid);
1137 stat = &devstatus[card * AP_DOMAINS + queue];
1138 stat->hwtype = zc->card->ap_dev.device_type;
1139 stat->functions = zc->card->functions >> 26;
1140 stat->qid = zq->queue->qid;
1141 stat->online = zq->online ? 0x01 : 0x00;
1144 spin_unlock(&zcrypt_list_lock);
1146 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1148 static void zcrypt_status_mask(char status[], size_t max_adapters)
1150 struct zcrypt_card *zc;
1151 struct zcrypt_queue *zq;
1154 memset(status, 0, max_adapters);
1155 spin_lock(&zcrypt_list_lock);
1156 for_each_zcrypt_card(zc) {
1157 for_each_zcrypt_queue(zq, zc) {
1158 card = AP_QID_CARD(zq->queue->qid);
1159 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1160 || card >= max_adapters)
1162 status[card] = zc->online ? zc->user_space_type : 0x0d;
1165 spin_unlock(&zcrypt_list_lock);
1168 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1170 struct zcrypt_card *zc;
1171 struct zcrypt_queue *zq;
1174 memset(qdepth, 0, max_adapters);
1175 spin_lock(&zcrypt_list_lock);
1177 for_each_zcrypt_card(zc) {
1178 for_each_zcrypt_queue(zq, zc) {
1179 card = AP_QID_CARD(zq->queue->qid);
1180 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1181 || card >= max_adapters)
1183 spin_lock(&zq->queue->lock);
1185 zq->queue->pendingq_count +
1186 zq->queue->requestq_count;
1187 spin_unlock(&zq->queue->lock);
1191 spin_unlock(&zcrypt_list_lock);
1194 static void zcrypt_perdev_reqcnt(int reqcnt[], size_t max_adapters)
1196 struct zcrypt_card *zc;
1197 struct zcrypt_queue *zq;
1200 memset(reqcnt, 0, sizeof(int) * 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);
1210 reqcnt[card] = zq->queue->total_request_count;
1211 spin_unlock(&zq->queue->lock);
1215 spin_unlock(&zcrypt_list_lock);
1218 static int zcrypt_pendingq_count(void)
1220 struct zcrypt_card *zc;
1221 struct zcrypt_queue *zq;
1225 spin_lock(&zcrypt_list_lock);
1227 for_each_zcrypt_card(zc) {
1228 for_each_zcrypt_queue(zq, zc) {
1229 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1231 spin_lock(&zq->queue->lock);
1232 pendingq_count += zq->queue->pendingq_count;
1233 spin_unlock(&zq->queue->lock);
1237 spin_unlock(&zcrypt_list_lock);
1238 return pendingq_count;
1241 static int zcrypt_requestq_count(void)
1243 struct zcrypt_card *zc;
1244 struct zcrypt_queue *zq;
1248 spin_lock(&zcrypt_list_lock);
1250 for_each_zcrypt_card(zc) {
1251 for_each_zcrypt_queue(zq, zc) {
1252 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1254 spin_lock(&zq->queue->lock);
1255 requestq_count += zq->queue->requestq_count;
1256 spin_unlock(&zq->queue->lock);
1260 spin_unlock(&zcrypt_list_lock);
1261 return requestq_count;
1264 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1268 struct ap_perms *perms =
1269 (struct ap_perms *) filp->private_data;
1271 rc = zcrypt_check_ioctl(perms, cmd);
1276 case ICARSAMODEXPO: {
1277 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1278 struct ica_rsa_modexpo mex;
1280 if (copy_from_user(&mex, umex, sizeof(mex)))
1283 rc = zcrypt_rsa_modexpo(perms, &mex);
1284 } while (rc == -EAGAIN);
1285 /* on failure: retry once again after a requested rescan */
1286 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1288 rc = zcrypt_rsa_modexpo(perms, &mex);
1289 } while (rc == -EAGAIN);
1291 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1294 return put_user(mex.outputdatalength, &umex->outputdatalength);
1297 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1298 struct ica_rsa_modexpo_crt crt;
1300 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1303 rc = zcrypt_rsa_crt(perms, &crt);
1304 } while (rc == -EAGAIN);
1305 /* on failure: retry once again after a requested rescan */
1306 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1308 rc = zcrypt_rsa_crt(perms, &crt);
1309 } while (rc == -EAGAIN);
1311 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1314 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1316 case ZSECSENDCPRB: {
1317 struct ica_xcRB __user *uxcRB = (void __user *) arg;
1318 struct ica_xcRB xcRB;
1320 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1323 rc = _zcrypt_send_cprb(perms, &xcRB);
1324 } while (rc == -EAGAIN);
1325 /* on failure: retry once again after a requested rescan */
1326 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1328 rc = _zcrypt_send_cprb(perms, &xcRB);
1329 } while (rc == -EAGAIN);
1331 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1333 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1337 case ZSENDEP11CPRB: {
1338 struct ep11_urb __user *uxcrb = (void __user *)arg;
1339 struct ep11_urb xcrb;
1341 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1344 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1345 } while (rc == -EAGAIN);
1346 /* on failure: retry once again after a requested rescan */
1347 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1349 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1350 } while (rc == -EAGAIN);
1352 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1353 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1357 case ZCRYPT_DEVICE_STATUS: {
1358 struct zcrypt_device_status_ext *device_status;
1359 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1360 * sizeof(struct zcrypt_device_status_ext);
1362 device_status = kzalloc(total_size, GFP_KERNEL);
1365 zcrypt_device_status_mask_ext(device_status);
1366 if (copy_to_user((char __user *) arg, device_status,
1369 kfree(device_status);
1372 case ZCRYPT_STATUS_MASK: {
1373 char status[AP_DEVICES];
1375 zcrypt_status_mask(status, AP_DEVICES);
1376 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1380 case ZCRYPT_QDEPTH_MASK: {
1381 char qdepth[AP_DEVICES];
1383 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1384 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1388 case ZCRYPT_PERDEV_REQCNT: {
1391 reqcnt = kcalloc(AP_DEVICES, sizeof(int), GFP_KERNEL);
1394 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1395 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1400 case Z90STAT_REQUESTQ_COUNT:
1401 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1402 case Z90STAT_PENDINGQ_COUNT:
1403 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1404 case Z90STAT_TOTALOPEN_COUNT:
1405 return put_user(atomic_read(&zcrypt_open_count),
1406 (int __user *) arg);
1407 case Z90STAT_DOMAIN_INDEX:
1408 return put_user(ap_domain_index, (int __user *) arg);
1412 case ZDEVICESTATUS: {
1413 /* the old ioctl supports only 64 adapters */
1414 struct zcrypt_device_status *device_status;
1415 size_t total_size = MAX_ZDEV_ENTRIES
1416 * sizeof(struct zcrypt_device_status);
1418 device_status = kzalloc(total_size, GFP_KERNEL);
1421 zcrypt_device_status_mask(device_status);
1422 if (copy_to_user((char __user *) arg, device_status,
1425 kfree(device_status);
1428 case Z90STAT_STATUS_MASK: {
1429 /* the old ioctl supports only 64 adapters */
1430 char status[MAX_ZDEV_CARDIDS];
1432 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1433 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1437 case Z90STAT_QDEPTH_MASK: {
1438 /* the old ioctl supports only 64 adapters */
1439 char qdepth[MAX_ZDEV_CARDIDS];
1441 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1442 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1446 case Z90STAT_PERDEV_REQCNT: {
1447 /* the old ioctl supports only 64 adapters */
1448 int reqcnt[MAX_ZDEV_CARDIDS];
1450 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1451 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1455 /* unknown ioctl number */
1457 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1458 return -ENOIOCTLCMD;
1462 #ifdef CONFIG_COMPAT
1464 * ioctl32 conversion routines
1466 struct compat_ica_rsa_modexpo {
1467 compat_uptr_t inputdata;
1468 unsigned int inputdatalength;
1469 compat_uptr_t outputdata;
1470 unsigned int outputdatalength;
1471 compat_uptr_t b_key;
1472 compat_uptr_t n_modulus;
1475 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1476 unsigned int cmd, unsigned long arg)
1478 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1479 struct compat_ica_rsa_modexpo mex32;
1480 struct ica_rsa_modexpo mex64;
1483 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1485 mex64.inputdata = compat_ptr(mex32.inputdata);
1486 mex64.inputdatalength = mex32.inputdatalength;
1487 mex64.outputdata = compat_ptr(mex32.outputdata);
1488 mex64.outputdatalength = mex32.outputdatalength;
1489 mex64.b_key = compat_ptr(mex32.b_key);
1490 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1492 rc = zcrypt_rsa_modexpo(perms, &mex64);
1493 } while (rc == -EAGAIN);
1494 /* on failure: retry once again after a requested rescan */
1495 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1497 rc = zcrypt_rsa_modexpo(perms, &mex64);
1498 } while (rc == -EAGAIN);
1501 return put_user(mex64.outputdatalength,
1502 &umex32->outputdatalength);
1505 struct compat_ica_rsa_modexpo_crt {
1506 compat_uptr_t inputdata;
1507 unsigned int inputdatalength;
1508 compat_uptr_t outputdata;
1509 unsigned int outputdatalength;
1510 compat_uptr_t bp_key;
1511 compat_uptr_t bq_key;
1512 compat_uptr_t np_prime;
1513 compat_uptr_t nq_prime;
1514 compat_uptr_t u_mult_inv;
1517 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1518 unsigned int cmd, unsigned long arg)
1520 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1521 struct compat_ica_rsa_modexpo_crt crt32;
1522 struct ica_rsa_modexpo_crt crt64;
1525 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1527 crt64.inputdata = compat_ptr(crt32.inputdata);
1528 crt64.inputdatalength = crt32.inputdatalength;
1529 crt64.outputdata = compat_ptr(crt32.outputdata);
1530 crt64.outputdatalength = crt32.outputdatalength;
1531 crt64.bp_key = compat_ptr(crt32.bp_key);
1532 crt64.bq_key = compat_ptr(crt32.bq_key);
1533 crt64.np_prime = compat_ptr(crt32.np_prime);
1534 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1535 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1537 rc = zcrypt_rsa_crt(perms, &crt64);
1538 } while (rc == -EAGAIN);
1539 /* on failure: retry once again after a requested rescan */
1540 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1542 rc = zcrypt_rsa_crt(perms, &crt64);
1543 } while (rc == -EAGAIN);
1546 return put_user(crt64.outputdatalength,
1547 &ucrt32->outputdatalength);
1550 struct compat_ica_xcRB {
1551 unsigned short agent_ID;
1552 unsigned int user_defined;
1553 unsigned short request_ID;
1554 unsigned int request_control_blk_length;
1555 unsigned char padding1[16 - sizeof(compat_uptr_t)];
1556 compat_uptr_t request_control_blk_addr;
1557 unsigned int request_data_length;
1558 char padding2[16 - sizeof(compat_uptr_t)];
1559 compat_uptr_t request_data_address;
1560 unsigned int reply_control_blk_length;
1561 char padding3[16 - sizeof(compat_uptr_t)];
1562 compat_uptr_t reply_control_blk_addr;
1563 unsigned int reply_data_length;
1564 char padding4[16 - sizeof(compat_uptr_t)];
1565 compat_uptr_t reply_data_addr;
1566 unsigned short priority_window;
1567 unsigned int status;
1570 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1571 unsigned int cmd, unsigned long arg)
1573 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1574 struct compat_ica_xcRB xcRB32;
1575 struct ica_xcRB xcRB64;
1578 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1580 xcRB64.agent_ID = xcRB32.agent_ID;
1581 xcRB64.user_defined = xcRB32.user_defined;
1582 xcRB64.request_ID = xcRB32.request_ID;
1583 xcRB64.request_control_blk_length =
1584 xcRB32.request_control_blk_length;
1585 xcRB64.request_control_blk_addr =
1586 compat_ptr(xcRB32.request_control_blk_addr);
1587 xcRB64.request_data_length =
1588 xcRB32.request_data_length;
1589 xcRB64.request_data_address =
1590 compat_ptr(xcRB32.request_data_address);
1591 xcRB64.reply_control_blk_length =
1592 xcRB32.reply_control_blk_length;
1593 xcRB64.reply_control_blk_addr =
1594 compat_ptr(xcRB32.reply_control_blk_addr);
1595 xcRB64.reply_data_length = xcRB32.reply_data_length;
1596 xcRB64.reply_data_addr =
1597 compat_ptr(xcRB32.reply_data_addr);
1598 xcRB64.priority_window = xcRB32.priority_window;
1599 xcRB64.status = xcRB32.status;
1601 rc = _zcrypt_send_cprb(perms, &xcRB64);
1602 } while (rc == -EAGAIN);
1603 /* on failure: retry once again after a requested rescan */
1604 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1606 rc = _zcrypt_send_cprb(perms, &xcRB64);
1607 } while (rc == -EAGAIN);
1608 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1609 xcRB32.reply_data_length = xcRB64.reply_data_length;
1610 xcRB32.status = xcRB64.status;
1611 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1616 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1620 struct ap_perms *perms =
1621 (struct ap_perms *) filp->private_data;
1623 rc = zcrypt_check_ioctl(perms, cmd);
1627 if (cmd == ICARSAMODEXPO)
1628 return trans_modexpo32(perms, filp, cmd, arg);
1629 if (cmd == ICARSACRT)
1630 return trans_modexpo_crt32(perms, filp, cmd, arg);
1631 if (cmd == ZSECSENDCPRB)
1632 return trans_xcRB32(perms, filp, cmd, arg);
1633 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1638 * Misc device file operations.
1640 static const struct file_operations zcrypt_fops = {
1641 .owner = THIS_MODULE,
1642 .read = zcrypt_read,
1643 .write = zcrypt_write,
1644 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1645 #ifdef CONFIG_COMPAT
1646 .compat_ioctl = zcrypt_compat_ioctl,
1648 .open = zcrypt_open,
1649 .release = zcrypt_release,
1650 .llseek = no_llseek,
1656 static struct miscdevice zcrypt_misc_device = {
1657 .minor = MISC_DYNAMIC_MINOR,
1659 .fops = &zcrypt_fops,
1662 static int zcrypt_rng_device_count;
1663 static u32 *zcrypt_rng_buffer;
1664 static int zcrypt_rng_buffer_index;
1665 static DEFINE_MUTEX(zcrypt_rng_mutex);
1667 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1672 * We don't need locking here because the RNG API guarantees serialized
1673 * read method calls.
1675 if (zcrypt_rng_buffer_index == 0) {
1676 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1677 /* on failure: retry once again after a requested rescan */
1678 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1679 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1682 zcrypt_rng_buffer_index = rc / sizeof(*data);
1684 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1685 return sizeof(*data);
1688 static struct hwrng zcrypt_rng_dev = {
1690 .data_read = zcrypt_rng_data_read,
1694 int zcrypt_rng_device_add(void)
1698 mutex_lock(&zcrypt_rng_mutex);
1699 if (zcrypt_rng_device_count == 0) {
1700 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1701 if (!zcrypt_rng_buffer) {
1705 zcrypt_rng_buffer_index = 0;
1706 if (!zcrypt_hwrng_seed)
1707 zcrypt_rng_dev.quality = 0;
1708 rc = hwrng_register(&zcrypt_rng_dev);
1711 zcrypt_rng_device_count = 1;
1713 zcrypt_rng_device_count++;
1714 mutex_unlock(&zcrypt_rng_mutex);
1718 free_page((unsigned long) zcrypt_rng_buffer);
1720 mutex_unlock(&zcrypt_rng_mutex);
1724 void zcrypt_rng_device_remove(void)
1726 mutex_lock(&zcrypt_rng_mutex);
1727 zcrypt_rng_device_count--;
1728 if (zcrypt_rng_device_count == 0) {
1729 hwrng_unregister(&zcrypt_rng_dev);
1730 free_page((unsigned long) zcrypt_rng_buffer);
1732 mutex_unlock(&zcrypt_rng_mutex);
1735 int __init zcrypt_debug_init(void)
1737 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1738 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1739 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1740 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1745 void zcrypt_debug_exit(void)
1747 debug_unregister(zcrypt_dbf_info);
1750 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1752 static int __init zcdn_init(void)
1756 /* create a new class 'zcrypt' */
1757 zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1758 if (IS_ERR(zcrypt_class)) {
1759 rc = PTR_ERR(zcrypt_class);
1760 goto out_class_create_failed;
1762 zcrypt_class->dev_release = zcdn_device_release;
1764 /* alloc device minor range */
1765 rc = alloc_chrdev_region(&zcrypt_devt,
1766 0, ZCRYPT_MAX_MINOR_NODES,
1769 goto out_alloc_chrdev_failed;
1771 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1772 zcrypt_cdev.owner = THIS_MODULE;
1773 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1775 goto out_cdev_add_failed;
1777 /* need some class specific sysfs attributes */
1778 rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1780 goto out_class_create_file_1_failed;
1781 rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1783 goto out_class_create_file_2_failed;
1787 out_class_create_file_2_failed:
1788 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1789 out_class_create_file_1_failed:
1790 cdev_del(&zcrypt_cdev);
1791 out_cdev_add_failed:
1792 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1793 out_alloc_chrdev_failed:
1794 class_destroy(zcrypt_class);
1795 out_class_create_failed:
1799 static void zcdn_exit(void)
1801 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1802 class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1804 cdev_del(&zcrypt_cdev);
1805 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1806 class_destroy(zcrypt_class);
1812 * zcrypt_api_init(): Module initialization.
1814 * The module initialization code.
1816 int __init zcrypt_api_init(void)
1820 rc = zcrypt_debug_init();
1824 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1830 /* Register the request sprayer. */
1831 rc = misc_register(&zcrypt_misc_device);
1833 goto out_misc_register_failed;
1835 zcrypt_msgtype6_init();
1836 zcrypt_msgtype50_init();
1840 out_misc_register_failed:
1841 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1844 zcrypt_debug_exit();
1850 * zcrypt_api_exit(): Module termination.
1852 * The module termination code.
1854 void __exit zcrypt_api_exit(void)
1856 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1859 misc_deregister(&zcrypt_misc_device);
1860 zcrypt_msgtype6_exit();
1861 zcrypt_msgtype50_exit();
1862 zcrypt_debug_exit();
1865 module_init(zcrypt_api_init);
1866 module_exit(zcrypt_api_exit);