Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   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>
   7 *
   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>
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/init.h>
  17#include <linux/interrupt.h>
  18#include <linux/miscdevice.h>
  19#include <linux/fs.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 <linux/capability.h>
  29#include <asm/debug.h>
  30
  31#define CREATE_TRACE_POINTS
  32#include <asm/trace/zcrypt.h>
  33
  34#include "zcrypt_api.h"
  35#include "zcrypt_debug.h"
  36
  37#include "zcrypt_msgtype6.h"
  38#include "zcrypt_msgtype50.h"
  39#include "zcrypt_ccamisc.h"
  40#include "zcrypt_ep11misc.h"
  41
  42/*
  43 * Module description.
  44 */
  45MODULE_AUTHOR("IBM Corporation");
  46MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  47		   "Copyright IBM Corp. 2001, 2012");
  48MODULE_LICENSE("GPL");
  49
  50/*
  51 * zcrypt tracepoint functions
  52 */
  53EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
  54EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
  55
 
 
 
 
  56DEFINE_SPINLOCK(zcrypt_list_lock);
  57LIST_HEAD(zcrypt_card_list);
 
  58
  59static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  60static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
  61
  62atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
  63EXPORT_SYMBOL(zcrypt_rescan_req);
  64
  65static LIST_HEAD(zcrypt_ops_list);
  66
  67/* Zcrypt related debug feature stuff. */
  68debug_info_t *zcrypt_dbf_info;
  69
  70/*
  71 * Process a rescan of the transport layer.
  72 *
  73 * Returns 1, if the rescan has been processed, otherwise 0.
  74 */
  75static inline int zcrypt_process_rescan(void)
  76{
  77	if (atomic_read(&zcrypt_rescan_req)) {
  78		atomic_set(&zcrypt_rescan_req, 0);
  79		atomic_inc(&zcrypt_rescan_count);
  80		ap_bus_force_rescan();
  81		ZCRYPT_DBF_INFO("%s rescan count=%07d\n", __func__,
  82				atomic_inc_return(&zcrypt_rescan_count));
  83		return 1;
  84	}
  85	return 0;
  86}
  87
  88void zcrypt_msgtype_register(struct zcrypt_ops *zops)
  89{
  90	list_add_tail(&zops->list, &zcrypt_ops_list);
  91}
  92
  93void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  94{
  95	list_del_init(&zops->list);
  96}
  97
  98struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
  99{
 100	struct zcrypt_ops *zops;
 101
 102	list_for_each_entry(zops, &zcrypt_ops_list, list)
 103		if (zops->variant == variant &&
 104		    (!strncmp(zops->name, name, sizeof(zops->name))))
 105			return zops;
 106	return NULL;
 107}
 108EXPORT_SYMBOL(zcrypt_msgtype);
 109
 110/*
 111 * Multi device nodes extension functions.
 112 */
 113
 114#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
 115
 116struct zcdn_device;
 117
 118static struct class *zcrypt_class;
 119static dev_t zcrypt_devt;
 120static struct cdev zcrypt_cdev;
 121
 122struct zcdn_device {
 123	struct device device;
 124	struct ap_perms perms;
 125};
 126
 127#define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
 128
 129#define ZCDN_MAX_NAME 32
 130
 131static int zcdn_create(const char *name);
 132static int zcdn_destroy(const char *name);
 133
 134/*
 135 * Find zcdn device by name.
 136 * Returns reference to the zcdn device which needs to be released
 137 * with put_device() after use.
 138 */
 139static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
 140{
 141	struct device *dev = class_find_device_by_name(zcrypt_class, name);
 142
 143	return dev ? to_zcdn_dev(dev) : NULL;
 144}
 145
 146/*
 147 * Find zcdn device by devt value.
 148 * Returns reference to the zcdn device which needs to be released
 149 * with put_device() after use.
 150 */
 151static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
 152{
 153	struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
 154
 155	return dev ? to_zcdn_dev(dev) : NULL;
 156}
 157
 158static ssize_t ioctlmask_show(struct device *dev,
 159			      struct device_attribute *attr,
 160			      char *buf)
 161{
 162	int i, rc;
 163	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 164
 165	if (mutex_lock_interruptible(&ap_perms_mutex))
 166		return -ERESTARTSYS;
 167
 168	buf[0] = '0';
 169	buf[1] = 'x';
 170	for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
 171		snprintf(buf + 2 + 2 * i * sizeof(long),
 172			 PAGE_SIZE - 2 - 2 * i * sizeof(long),
 173			 "%016lx", zcdndev->perms.ioctlm[i]);
 174	buf[2 + 2 * i * sizeof(long)] = '\n';
 175	buf[2 + 2 * i * sizeof(long) + 1] = '\0';
 176	rc = 2 + 2 * i * sizeof(long) + 1;
 177
 178	mutex_unlock(&ap_perms_mutex);
 179
 180	return rc;
 181}
 182
 183static ssize_t ioctlmask_store(struct device *dev,
 184			       struct device_attribute *attr,
 185			       const char *buf, size_t count)
 186{
 187	int rc;
 188	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 189
 190	rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
 191			       AP_IOCTLS, &ap_perms_mutex);
 192	if (rc)
 193		return rc;
 194
 195	return count;
 196}
 197
 198static DEVICE_ATTR_RW(ioctlmask);
 199
 200static ssize_t apmask_show(struct device *dev,
 201			   struct device_attribute *attr,
 202			   char *buf)
 203{
 204	int i, rc;
 205	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 206
 207	if (mutex_lock_interruptible(&ap_perms_mutex))
 208		return -ERESTARTSYS;
 209
 210	buf[0] = '0';
 211	buf[1] = 'x';
 212	for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
 213		snprintf(buf + 2 + 2 * i * sizeof(long),
 214			 PAGE_SIZE - 2 - 2 * i * sizeof(long),
 215			 "%016lx", zcdndev->perms.apm[i]);
 216	buf[2 + 2 * i * sizeof(long)] = '\n';
 217	buf[2 + 2 * i * sizeof(long) + 1] = '\0';
 218	rc = 2 + 2 * i * sizeof(long) + 1;
 219
 220	mutex_unlock(&ap_perms_mutex);
 221
 222	return rc;
 223}
 224
 225static ssize_t apmask_store(struct device *dev,
 226			    struct device_attribute *attr,
 227			    const char *buf, size_t count)
 228{
 229	int rc;
 230	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 231
 232	rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
 233			       AP_DEVICES, &ap_perms_mutex);
 234	if (rc)
 235		return rc;
 236
 237	return count;
 238}
 239
 240static DEVICE_ATTR_RW(apmask);
 241
 242static ssize_t aqmask_show(struct device *dev,
 243			   struct device_attribute *attr,
 244			   char *buf)
 245{
 246	int i, rc;
 247	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 248
 249	if (mutex_lock_interruptible(&ap_perms_mutex))
 250		return -ERESTARTSYS;
 251
 252	buf[0] = '0';
 253	buf[1] = 'x';
 254	for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
 255		snprintf(buf + 2 + 2 * i * sizeof(long),
 256			 PAGE_SIZE - 2 - 2 * i * sizeof(long),
 257			 "%016lx", zcdndev->perms.aqm[i]);
 258	buf[2 + 2 * i * sizeof(long)] = '\n';
 259	buf[2 + 2 * i * sizeof(long) + 1] = '\0';
 260	rc = 2 + 2 * i * sizeof(long) + 1;
 261
 262	mutex_unlock(&ap_perms_mutex);
 263
 264	return rc;
 265}
 266
 267static ssize_t aqmask_store(struct device *dev,
 268			    struct device_attribute *attr,
 269			    const char *buf, size_t count)
 270{
 271	int rc;
 272	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 273
 274	rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
 275			       AP_DOMAINS, &ap_perms_mutex);
 276	if (rc)
 277		return rc;
 278
 279	return count;
 280}
 281
 282static DEVICE_ATTR_RW(aqmask);
 283
 284static ssize_t admask_show(struct device *dev,
 285			   struct device_attribute *attr,
 286			   char *buf)
 287{
 288	int i, rc;
 289	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 290
 291	if (mutex_lock_interruptible(&ap_perms_mutex))
 292		return -ERESTARTSYS;
 293
 294	buf[0] = '0';
 295	buf[1] = 'x';
 296	for (i = 0; i < sizeof(zcdndev->perms.adm) / sizeof(long); i++)
 297		snprintf(buf + 2 + 2 * i * sizeof(long),
 298			 PAGE_SIZE - 2 - 2 * i * sizeof(long),
 299			 "%016lx", zcdndev->perms.adm[i]);
 300	buf[2 + 2 * i * sizeof(long)] = '\n';
 301	buf[2 + 2 * i * sizeof(long) + 1] = '\0';
 302	rc = 2 + 2 * i * sizeof(long) + 1;
 303
 304	mutex_unlock(&ap_perms_mutex);
 305
 306	return rc;
 307}
 308
 309static ssize_t admask_store(struct device *dev,
 310			    struct device_attribute *attr,
 311			    const char *buf, size_t count)
 312{
 313	int rc;
 314	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 315
 316	rc = ap_parse_mask_str(buf, zcdndev->perms.adm,
 317			       AP_DOMAINS, &ap_perms_mutex);
 318	if (rc)
 319		return rc;
 320
 321	return count;
 322}
 323
 324static DEVICE_ATTR_RW(admask);
 325
 326static struct attribute *zcdn_dev_attrs[] = {
 327	&dev_attr_ioctlmask.attr,
 328	&dev_attr_apmask.attr,
 329	&dev_attr_aqmask.attr,
 330	&dev_attr_admask.attr,
 331	NULL
 332};
 333
 334static struct attribute_group zcdn_dev_attr_group = {
 335	.attrs = zcdn_dev_attrs
 336};
 337
 338static const struct attribute_group *zcdn_dev_attr_groups[] = {
 339	&zcdn_dev_attr_group,
 340	NULL
 341};
 342
 343static ssize_t zcdn_create_store(struct class *class,
 344				 struct class_attribute *attr,
 345				 const char *buf, size_t count)
 346{
 347	int rc;
 348	char name[ZCDN_MAX_NAME];
 349
 350	strncpy(name, skip_spaces(buf), sizeof(name));
 351	name[sizeof(name) - 1] = '\0';
 352
 353	rc = zcdn_create(strim(name));
 354
 355	return rc ? rc : count;
 356}
 357
 358static const struct class_attribute class_attr_zcdn_create =
 359	__ATTR(create, 0600, NULL, zcdn_create_store);
 360
 361static ssize_t zcdn_destroy_store(struct class *class,
 362				  struct class_attribute *attr,
 363				  const char *buf, size_t count)
 364{
 365	int rc;
 366	char name[ZCDN_MAX_NAME];
 367
 368	strncpy(name, skip_spaces(buf), sizeof(name));
 369	name[sizeof(name) - 1] = '\0';
 370
 371	rc = zcdn_destroy(strim(name));
 372
 373	return rc ? rc : count;
 374}
 375
 376static const struct class_attribute class_attr_zcdn_destroy =
 377	__ATTR(destroy, 0600, NULL, zcdn_destroy_store);
 378
 379static void zcdn_device_release(struct device *dev)
 380{
 381	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 382
 383	ZCRYPT_DBF_INFO("%s releasing zcdn device %d:%d\n",
 384			__func__, MAJOR(dev->devt), MINOR(dev->devt));
 385
 386	kfree(zcdndev);
 387}
 388
 389static int zcdn_create(const char *name)
 390{
 391	dev_t devt;
 392	int i, rc = 0;
 393	char nodename[ZCDN_MAX_NAME];
 394	struct zcdn_device *zcdndev;
 395
 396	if (mutex_lock_interruptible(&ap_perms_mutex))
 397		return -ERESTARTSYS;
 398
 399	/* check if device node with this name already exists */
 400	if (name[0]) {
 401		zcdndev = find_zcdndev_by_name(name);
 402		if (zcdndev) {
 403			put_device(&zcdndev->device);
 404			rc = -EEXIST;
 405			goto unlockout;
 406		}
 407	}
 408
 409	/* find an unused minor number */
 410	for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
 411		devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
 412		zcdndev = find_zcdndev_by_devt(devt);
 413		if (zcdndev)
 414			put_device(&zcdndev->device);
 415		else
 416			break;
 417	}
 418	if (i == ZCRYPT_MAX_MINOR_NODES) {
 419		rc = -ENOSPC;
 420		goto unlockout;
 421	}
 422
 423	/* alloc and prepare a new zcdn device */
 424	zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
 425	if (!zcdndev) {
 426		rc = -ENOMEM;
 427		goto unlockout;
 428	}
 429	zcdndev->device.release = zcdn_device_release;
 430	zcdndev->device.class = zcrypt_class;
 431	zcdndev->device.devt = devt;
 432	zcdndev->device.groups = zcdn_dev_attr_groups;
 433	if (name[0])
 434		strncpy(nodename, name, sizeof(nodename));
 435	else
 436		snprintf(nodename, sizeof(nodename),
 437			 ZCRYPT_NAME "_%d", (int)MINOR(devt));
 438	nodename[sizeof(nodename) - 1] = '\0';
 439	if (dev_set_name(&zcdndev->device, nodename)) {
 440		rc = -EINVAL;
 441		goto unlockout;
 442	}
 443	rc = device_register(&zcdndev->device);
 444	if (rc) {
 445		put_device(&zcdndev->device);
 446		goto unlockout;
 447	}
 448
 449	ZCRYPT_DBF_INFO("%s created zcdn device %d:%d\n",
 450			__func__, MAJOR(devt), MINOR(devt));
 451
 452unlockout:
 453	mutex_unlock(&ap_perms_mutex);
 454	return rc;
 455}
 456
 457static int zcdn_destroy(const char *name)
 458{
 459	int rc = 0;
 460	struct zcdn_device *zcdndev;
 461
 462	if (mutex_lock_interruptible(&ap_perms_mutex))
 463		return -ERESTARTSYS;
 464
 465	/* try to find this zcdn device */
 466	zcdndev = find_zcdndev_by_name(name);
 467	if (!zcdndev) {
 468		rc = -ENOENT;
 469		goto unlockout;
 470	}
 471
 472	/*
 473	 * The zcdn device is not hard destroyed. It is subject to
 474	 * reference counting and thus just needs to be unregistered.
 475	 */
 476	put_device(&zcdndev->device);
 477	device_unregister(&zcdndev->device);
 478
 479unlockout:
 480	mutex_unlock(&ap_perms_mutex);
 481	return rc;
 482}
 483
 484static void zcdn_destroy_all(void)
 485{
 486	int i;
 487	dev_t devt;
 488	struct zcdn_device *zcdndev;
 489
 490	mutex_lock(&ap_perms_mutex);
 491	for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
 492		devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
 493		zcdndev = find_zcdndev_by_devt(devt);
 494		if (zcdndev) {
 495			put_device(&zcdndev->device);
 496			device_unregister(&zcdndev->device);
 497		}
 498	}
 499	mutex_unlock(&ap_perms_mutex);
 500}
 501
 502#endif
 503
 504/*
 505 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
 506 *
 507 * This function is not supported beyond zcrypt 1.3.1.
 508 */
 509static ssize_t zcrypt_read(struct file *filp, char __user *buf,
 510			   size_t count, loff_t *f_pos)
 511{
 512	return -EPERM;
 513}
 514
 515/*
 516 * zcrypt_write(): Not allowed.
 517 *
 518 * Write is not allowed
 519 */
 520static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
 521			    size_t count, loff_t *f_pos)
 522{
 523	return -EPERM;
 524}
 525
 526/*
 527 * zcrypt_open(): Count number of users.
 528 *
 529 * Device open function to count number of users.
 530 */
 531static int zcrypt_open(struct inode *inode, struct file *filp)
 532{
 533	struct ap_perms *perms = &ap_perms;
 534
 535#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
 536	if (filp->f_inode->i_cdev == &zcrypt_cdev) {
 537		struct zcdn_device *zcdndev;
 538
 539		if (mutex_lock_interruptible(&ap_perms_mutex))
 540			return -ERESTARTSYS;
 541		zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
 542		/* find returns a reference, no get_device() needed */
 543		mutex_unlock(&ap_perms_mutex);
 544		if (zcdndev)
 545			perms = &zcdndev->perms;
 546	}
 547#endif
 548	filp->private_data = (void *)perms;
 549
 550	atomic_inc(&zcrypt_open_count);
 551	return stream_open(inode, filp);
 552}
 553
 554/*
 555 * zcrypt_release(): Count number of users.
 556 *
 557 * Device close function to count number of users.
 558 */
 559static int zcrypt_release(struct inode *inode, struct file *filp)
 560{
 561#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
 562	if (filp->f_inode->i_cdev == &zcrypt_cdev) {
 563		struct zcdn_device *zcdndev;
 564
 565		mutex_lock(&ap_perms_mutex);
 566		zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
 567		mutex_unlock(&ap_perms_mutex);
 568		if (zcdndev) {
 569			/* 2 puts here: one for find, one for open */
 570			put_device(&zcdndev->device);
 571			put_device(&zcdndev->device);
 572		}
 573	}
 574#endif
 575
 576	atomic_dec(&zcrypt_open_count);
 577	return 0;
 578}
 579
 580static inline int zcrypt_check_ioctl(struct ap_perms *perms,
 581				     unsigned int cmd)
 582{
 583	int rc = -EPERM;
 584	int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
 585
 586	if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
 587		if (test_bit_inv(ioctlnr, perms->ioctlm))
 588			rc = 0;
 589	}
 590
 591	if (rc)
 592		ZCRYPT_DBF_WARN("%s ioctl check failed: ioctlnr=0x%04x rc=%d\n",
 593				__func__, ioctlnr, rc);
 
 594
 595	return rc;
 596}
 597
 598static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
 599{
 600	return test_bit_inv(card, perms->apm) ? true : false;
 601}
 602
 603static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
 604{
 605	return test_bit_inv(queue, perms->aqm) ? true : false;
 606}
 607
 608static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
 609						     struct zcrypt_queue *zq,
 610						     struct module **pmod,
 611						     unsigned int weight)
 612{
 613	if (!zq || !try_module_get(zq->queue->ap_dev.device.driver->owner))
 614		return NULL;
 615	zcrypt_queue_get(zq);
 616	get_device(&zq->queue->ap_dev.device);
 617	atomic_add(weight, &zc->load);
 618	atomic_add(weight, &zq->load);
 619	zq->request_count++;
 620	*pmod = zq->queue->ap_dev.device.driver->owner;
 621	return zq;
 622}
 623
 624static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
 625				     struct zcrypt_queue *zq,
 626				     struct module *mod,
 627				     unsigned int weight)
 628{
 629	zq->request_count--;
 630	atomic_sub(weight, &zc->load);
 631	atomic_sub(weight, &zq->load);
 632	put_device(&zq->queue->ap_dev.device);
 633	zcrypt_queue_put(zq);
 634	module_put(mod);
 635}
 636
 637static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
 638				       struct zcrypt_card *pref_zc,
 639				       unsigned int weight,
 640				       unsigned int pref_weight)
 641{
 642	if (!pref_zc)
 643		return true;
 644	weight += atomic_read(&zc->load);
 645	pref_weight += atomic_read(&pref_zc->load);
 646	if (weight == pref_weight)
 647		return atomic64_read(&zc->card->total_request_count) <
 648			atomic64_read(&pref_zc->card->total_request_count);
 649	return weight < pref_weight;
 650}
 651
 652static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
 653					struct zcrypt_queue *pref_zq,
 654					unsigned int weight,
 655					unsigned int pref_weight)
 656{
 657	if (!pref_zq)
 658		return true;
 659	weight += atomic_read(&zq->load);
 660	pref_weight += atomic_read(&pref_zq->load);
 661	if (weight == pref_weight)
 662		return zq->queue->total_request_count <
 663			pref_zq->queue->total_request_count;
 664	return weight < pref_weight;
 665}
 666
 667/*
 668 * zcrypt ioctls.
 669 */
 670static long zcrypt_rsa_modexpo(struct ap_perms *perms,
 671			       struct zcrypt_track *tr,
 672			       struct ica_rsa_modexpo *mex)
 673{
 674	struct zcrypt_card *zc, *pref_zc;
 675	struct zcrypt_queue *zq, *pref_zq;
 676	struct ap_message ap_msg;
 677	unsigned int wgt = 0, pref_wgt = 0;
 678	unsigned int func_code;
 679	int cpen, qpen, qid = 0, rc = -ENODEV;
 680	struct module *mod;
 681
 682	trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
 683
 684	ap_init_message(&ap_msg);
 685
 686#ifdef CONFIG_ZCRYPT_DEBUG
 687	if (tr && tr->fi.cmd)
 688		ap_msg.fi.cmd = tr->fi.cmd;
 689#endif
 690
 691	if (mex->outputdatalength < mex->inputdatalength) {
 692		func_code = 0;
 693		rc = -EINVAL;
 694		goto out;
 695	}
 696
 697	/*
 698	 * As long as outputdatalength is big enough, we can set the
 699	 * outputdatalength equal to the inputdatalength, since that is the
 700	 * number of bytes we will copy in any case
 701	 */
 702	mex->outputdatalength = mex->inputdatalength;
 703
 704	rc = get_rsa_modex_fc(mex, &func_code);
 705	if (rc)
 706		goto out;
 707
 708	pref_zc = NULL;
 709	pref_zq = NULL;
 710	spin_lock(&zcrypt_list_lock);
 711	for_each_zcrypt_card(zc) {
 712		/* Check for usable accelarator or CCA card */
 713		if (!zc->online || !zc->card->config || zc->card->chkstop ||
 714		    !(zc->card->functions & 0x18000000))
 715			continue;
 716		/* Check for size limits */
 717		if (zc->min_mod_size > mex->inputdatalength ||
 718		    zc->max_mod_size < mex->inputdatalength)
 719			continue;
 720		/* check if device node has admission for this card */
 721		if (!zcrypt_check_card(perms, zc->card->id))
 722			continue;
 723		/* get weight index of the card device	*/
 724		wgt = zc->speed_rating[func_code];
 725		/* penalty if this msg was previously sent via this card */
 726		cpen = (tr && tr->again_counter && tr->last_qid &&
 727			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
 728			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
 729		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
 730			continue;
 731		for_each_zcrypt_queue(zq, zc) {
 732			/* check if device is usable and eligible */
 733			if (!zq->online || !zq->ops->rsa_modexpo ||
 734			    !zq->queue->config || zq->queue->chkstop)
 735				continue;
 736			/* check if device node has admission for this queue */
 737			if (!zcrypt_check_queue(perms,
 738						AP_QID_QUEUE(zq->queue->qid)))
 739				continue;
 740			/* penalty if the msg was previously sent at this qid */
 741			qpen = (tr && tr->again_counter && tr->last_qid &&
 742				tr->last_qid == zq->queue->qid) ?
 743				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
 744			if (!zcrypt_queue_compare(zq, pref_zq,
 745						  wgt + cpen + qpen, pref_wgt))
 746				continue;
 747			pref_zc = zc;
 748			pref_zq = zq;
 749			pref_wgt = wgt + cpen + qpen;
 750		}
 751	}
 752	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
 753	spin_unlock(&zcrypt_list_lock);
 754
 755	if (!pref_zq) {
 756		ZCRYPT_DBF_DBG("%s no matching queue found => ENODEV\n",
 757			       __func__);
 758		rc = -ENODEV;
 759		goto out;
 760	}
 761
 762	qid = pref_zq->queue->qid;
 763	rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
 764
 765	spin_lock(&zcrypt_list_lock);
 766	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
 767	spin_unlock(&zcrypt_list_lock);
 768
 769out:
 770	ap_release_message(&ap_msg);
 771	if (tr) {
 772		tr->last_rc = rc;
 773		tr->last_qid = qid;
 774	}
 775	trace_s390_zcrypt_rep(mex, func_code, rc,
 776			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
 777	return rc;
 778}
 779
 780static long zcrypt_rsa_crt(struct ap_perms *perms,
 781			   struct zcrypt_track *tr,
 782			   struct ica_rsa_modexpo_crt *crt)
 783{
 784	struct zcrypt_card *zc, *pref_zc;
 785	struct zcrypt_queue *zq, *pref_zq;
 786	struct ap_message ap_msg;
 787	unsigned int wgt = 0, pref_wgt = 0;
 788	unsigned int func_code;
 789	int cpen, qpen, qid = 0, rc = -ENODEV;
 790	struct module *mod;
 791
 792	trace_s390_zcrypt_req(crt, TP_ICARSACRT);
 793
 794	ap_init_message(&ap_msg);
 795
 796#ifdef CONFIG_ZCRYPT_DEBUG
 797	if (tr && tr->fi.cmd)
 798		ap_msg.fi.cmd = tr->fi.cmd;
 799#endif
 800
 801	if (crt->outputdatalength < crt->inputdatalength) {
 802		func_code = 0;
 803		rc = -EINVAL;
 804		goto out;
 805	}
 806
 807	/*
 808	 * As long as outputdatalength is big enough, we can set the
 809	 * outputdatalength equal to the inputdatalength, since that is the
 810	 * number of bytes we will copy in any case
 811	 */
 812	crt->outputdatalength = crt->inputdatalength;
 813
 814	rc = get_rsa_crt_fc(crt, &func_code);
 815	if (rc)
 816		goto out;
 817
 818	pref_zc = NULL;
 819	pref_zq = NULL;
 820	spin_lock(&zcrypt_list_lock);
 821	for_each_zcrypt_card(zc) {
 822		/* Check for usable accelarator or CCA card */
 823		if (!zc->online || !zc->card->config || zc->card->chkstop ||
 824		    !(zc->card->functions & 0x18000000))
 825			continue;
 826		/* Check for size limits */
 827		if (zc->min_mod_size > crt->inputdatalength ||
 828		    zc->max_mod_size < crt->inputdatalength)
 829			continue;
 830		/* check if device node has admission for this card */
 831		if (!zcrypt_check_card(perms, zc->card->id))
 832			continue;
 833		/* get weight index of the card device	*/
 834		wgt = zc->speed_rating[func_code];
 835		/* penalty if this msg was previously sent via this card */
 836		cpen = (tr && tr->again_counter && tr->last_qid &&
 837			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
 838			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
 839		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
 840			continue;
 841		for_each_zcrypt_queue(zq, zc) {
 842			/* check if device is usable and eligible */
 843			if (!zq->online || !zq->ops->rsa_modexpo_crt ||
 844			    !zq->queue->config || zq->queue->chkstop)
 845				continue;
 846			/* check if device node has admission for this queue */
 847			if (!zcrypt_check_queue(perms,
 848						AP_QID_QUEUE(zq->queue->qid)))
 849				continue;
 850			/* penalty if the msg was previously sent at this qid */
 851			qpen = (tr && tr->again_counter && tr->last_qid &&
 852				tr->last_qid == zq->queue->qid) ?
 853				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
 854			if (!zcrypt_queue_compare(zq, pref_zq,
 855						  wgt + cpen + qpen, pref_wgt))
 856				continue;
 857			pref_zc = zc;
 858			pref_zq = zq;
 859			pref_wgt = wgt + cpen + qpen;
 860		}
 861	}
 862	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
 863	spin_unlock(&zcrypt_list_lock);
 864
 865	if (!pref_zq) {
 866		ZCRYPT_DBF_DBG("%s no matching queue found => ENODEV\n",
 867			       __func__);
 868		rc = -ENODEV;
 869		goto out;
 870	}
 871
 872	qid = pref_zq->queue->qid;
 873	rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
 874
 875	spin_lock(&zcrypt_list_lock);
 876	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
 877	spin_unlock(&zcrypt_list_lock);
 878
 879out:
 880	ap_release_message(&ap_msg);
 881	if (tr) {
 882		tr->last_rc = rc;
 883		tr->last_qid = qid;
 884	}
 885	trace_s390_zcrypt_rep(crt, func_code, rc,
 886			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
 887	return rc;
 888}
 889
 890static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
 891			      struct zcrypt_track *tr,
 892			      struct ica_xcRB *xcrb)
 893{
 894	struct zcrypt_card *zc, *pref_zc;
 895	struct zcrypt_queue *zq, *pref_zq;
 896	struct ap_message ap_msg;
 897	unsigned int wgt = 0, pref_wgt = 0;
 898	unsigned int func_code;
 899	unsigned short *domain, tdom;
 900	int cpen, qpen, qid = 0, rc = -ENODEV;
 901	struct module *mod;
 902
 903	trace_s390_zcrypt_req(xcrb, TB_ZSECSENDCPRB);
 904
 905	xcrb->status = 0;
 906	ap_init_message(&ap_msg);
 907
 908#ifdef CONFIG_ZCRYPT_DEBUG
 909	if (tr && tr->fi.cmd)
 910		ap_msg.fi.cmd = tr->fi.cmd;
 911	if (tr && tr->fi.action == AP_FI_ACTION_CCA_AGENT_FF) {
 912		ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid agent_ID 'FF'\n",
 913				__func__, tr->fi.cmd);
 914		xcrb->agent_ID = 0x4646;
 915	}
 916#endif
 917
 918	rc = prep_cca_ap_msg(userspace, xcrb, &ap_msg, &func_code, &domain);
 919	if (rc)
 920		goto out;
 921
 922	tdom = *domain;
 923	if (perms != &ap_perms && tdom < AP_DOMAINS) {
 924		if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
 925			if (!test_bit_inv(tdom, perms->adm)) {
 926				rc = -ENODEV;
 927				goto out;
 928			}
 929		} else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
 930			rc = -EOPNOTSUPP;
 931			goto out;
 932		}
 933	}
 934	/*
 935	 * If a valid target domain is set and this domain is NOT a usage
 936	 * domain but a control only domain, autoselect target domain.
 937	 */
 
 938	if (tdom < AP_DOMAINS &&
 939	    !ap_test_config_usage_domain(tdom) &&
 940	    ap_test_config_ctrl_domain(tdom))
 941		tdom = AUTOSEL_DOM;
 
 942
 943	pref_zc = NULL;
 944	pref_zq = NULL;
 945	spin_lock(&zcrypt_list_lock);
 946	for_each_zcrypt_card(zc) {
 947		/* Check for usable CCA card */
 948		if (!zc->online || !zc->card->config || zc->card->chkstop ||
 949		    !(zc->card->functions & 0x10000000))
 950			continue;
 951		/* Check for user selected CCA card */
 952		if (xcrb->user_defined != AUTOSELECT &&
 953		    xcrb->user_defined != zc->card->id)
 954			continue;
 955		/* check if request size exceeds card max msg size */
 956		if (ap_msg.len > zc->card->maxmsgsize)
 957			continue;
 958		/* check if device node has admission for this card */
 959		if (!zcrypt_check_card(perms, zc->card->id))
 960			continue;
 961		/* get weight index of the card device	*/
 962		wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
 963		/* penalty if this msg was previously sent via this card */
 964		cpen = (tr && tr->again_counter && tr->last_qid &&
 965			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
 966			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
 967		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
 968			continue;
 969		for_each_zcrypt_queue(zq, zc) {
 970			/* check for device usable and eligible */
 971			if (!zq->online || !zq->ops->send_cprb ||
 972			    !zq->queue->config || zq->queue->chkstop ||
 973			    (tdom != AUTOSEL_DOM &&
 974			     tdom != AP_QID_QUEUE(zq->queue->qid)))
 975				continue;
 976			/* check if device node has admission for this queue */
 977			if (!zcrypt_check_queue(perms,
 978						AP_QID_QUEUE(zq->queue->qid)))
 979				continue;
 980			/* penalty if the msg was previously sent at this qid */
 981			qpen = (tr && tr->again_counter && tr->last_qid &&
 982				tr->last_qid == zq->queue->qid) ?
 983				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
 984			if (!zcrypt_queue_compare(zq, pref_zq,
 985						  wgt + cpen + qpen, pref_wgt))
 986				continue;
 987			pref_zc = zc;
 988			pref_zq = zq;
 989			pref_wgt = wgt + cpen + qpen;
 990		}
 991	}
 992	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
 993	spin_unlock(&zcrypt_list_lock);
 994
 995	if (!pref_zq) {
 996		ZCRYPT_DBF_DBG("%s no match for address %02x.%04x => ENODEV\n",
 997			       __func__, xcrb->user_defined, *domain);
 998		rc = -ENODEV;
 999		goto out;
1000	}
1001
1002	/* in case of auto select, provide the correct domain */
1003	qid = pref_zq->queue->qid;
1004	if (*domain == AUTOSEL_DOM)
1005		*domain = AP_QID_QUEUE(qid);
1006
1007#ifdef CONFIG_ZCRYPT_DEBUG
1008	if (tr && tr->fi.action == AP_FI_ACTION_CCA_DOM_INVAL) {
1009		ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid domain\n",
1010				__func__, tr->fi.cmd);
1011		*domain = 99;
1012	}
1013#endif
1014
1015	rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcrb, &ap_msg);
1016
1017	spin_lock(&zcrypt_list_lock);
1018	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1019	spin_unlock(&zcrypt_list_lock);
1020
1021out:
1022	ap_release_message(&ap_msg);
1023	if (tr) {
1024		tr->last_rc = rc;
1025		tr->last_qid = qid;
1026	}
1027	trace_s390_zcrypt_rep(xcrb, func_code, rc,
1028			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1029	return rc;
1030}
1031
1032long zcrypt_send_cprb(struct ica_xcRB *xcrb)
1033{
1034	return _zcrypt_send_cprb(false, &ap_perms, NULL, xcrb);
1035}
1036EXPORT_SYMBOL(zcrypt_send_cprb);
1037
1038static bool is_desired_ep11_card(unsigned int dev_id,
1039				 unsigned short target_num,
1040				 struct ep11_target_dev *targets)
1041{
1042	while (target_num-- > 0) {
1043		if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
1044			return true;
1045		targets++;
1046	}
1047	return false;
1048}
1049
1050static bool is_desired_ep11_queue(unsigned int dev_qid,
1051				  unsigned short target_num,
1052				  struct ep11_target_dev *targets)
1053{
1054	int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
1055
1056	while (target_num-- > 0) {
1057		if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
1058		    (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
1059			return true;
1060		targets++;
1061	}
1062	return false;
1063}
1064
1065static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
1066				   struct zcrypt_track *tr,
1067				   struct ep11_urb *xcrb)
1068{
1069	struct zcrypt_card *zc, *pref_zc;
1070	struct zcrypt_queue *zq, *pref_zq;
1071	struct ep11_target_dev *targets;
1072	unsigned short target_num;
1073	unsigned int wgt = 0, pref_wgt = 0;
1074	unsigned int func_code, domain;
1075	struct ap_message ap_msg;
1076	int cpen, qpen, qid = 0, rc = -ENODEV;
1077	struct module *mod;
1078
1079	trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
1080
1081	ap_init_message(&ap_msg);
1082
1083#ifdef CONFIG_ZCRYPT_DEBUG
1084	if (tr && tr->fi.cmd)
1085		ap_msg.fi.cmd = tr->fi.cmd;
1086#endif
1087
1088	target_num = (unsigned short)xcrb->targets_num;
1089
1090	/* empty list indicates autoselect (all available targets) */
1091	targets = NULL;
1092	if (target_num != 0) {
1093		struct ep11_target_dev __user *uptr;
1094
1095		targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
1096		if (!targets) {
1097			func_code = 0;
1098			rc = -ENOMEM;
1099			goto out;
1100		}
1101
1102		uptr = (struct ep11_target_dev __force __user *)xcrb->targets;
1103		if (z_copy_from_user(userspace, targets, uptr,
1104				     target_num * sizeof(*targets))) {
1105			func_code = 0;
1106			rc = -EFAULT;
1107			goto out_free;
1108		}
1109	}
1110
1111	rc = prep_ep11_ap_msg(userspace, xcrb, &ap_msg, &func_code, &domain);
1112	if (rc)
1113		goto out_free;
1114
1115	if (perms != &ap_perms && domain < AUTOSEL_DOM) {
1116		if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
1117			if (!test_bit_inv(domain, perms->adm)) {
1118				rc = -ENODEV;
1119				goto out_free;
1120			}
1121		} else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
1122			rc = -EOPNOTSUPP;
1123			goto out_free;
1124		}
1125	}
1126
1127	pref_zc = NULL;
1128	pref_zq = NULL;
1129	spin_lock(&zcrypt_list_lock);
1130	for_each_zcrypt_card(zc) {
1131		/* Check for usable EP11 card */
1132		if (!zc->online || !zc->card->config || zc->card->chkstop ||
1133		    !(zc->card->functions & 0x04000000))
1134			continue;
1135		/* Check for user selected EP11 card */
1136		if (targets &&
1137		    !is_desired_ep11_card(zc->card->id, target_num, targets))
1138			continue;
1139		/* check if request size exceeds card max msg size */
1140		if (ap_msg.len > zc->card->maxmsgsize)
1141			continue;
1142		/* check if device node has admission for this card */
1143		if (!zcrypt_check_card(perms, zc->card->id))
1144			continue;
1145		/* get weight index of the card device	*/
1146		wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1147		/* penalty if this msg was previously sent via this card */
1148		cpen = (tr && tr->again_counter && tr->last_qid &&
1149			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
1150			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
1151		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
1152			continue;
1153		for_each_zcrypt_queue(zq, zc) {
1154			/* check if device is usable and eligible */
1155			if (!zq->online || !zq->ops->send_ep11_cprb ||
1156			    !zq->queue->config || zq->queue->chkstop ||
1157			    (targets &&
1158			     !is_desired_ep11_queue(zq->queue->qid,
1159						    target_num, targets)))
1160				continue;
1161			/* check if device node has admission for this queue */
1162			if (!zcrypt_check_queue(perms,
1163						AP_QID_QUEUE(zq->queue->qid)))
1164				continue;
1165			/* penalty if the msg was previously sent at this qid */
1166			qpen = (tr && tr->again_counter && tr->last_qid &&
1167				tr->last_qid == zq->queue->qid) ?
1168				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
1169			if (!zcrypt_queue_compare(zq, pref_zq,
1170						  wgt + cpen + qpen, pref_wgt))
1171				continue;
1172			pref_zc = zc;
1173			pref_zq = zq;
1174			pref_wgt = wgt + cpen + qpen;
1175		}
1176	}
1177	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1178	spin_unlock(&zcrypt_list_lock);
1179
1180	if (!pref_zq) {
1181		if (targets && target_num == 1) {
1182			ZCRYPT_DBF_DBG("%s no match for address %02x.%04x => ENODEV\n",
1183				       __func__, (int)targets->ap_id,
1184				       (int)targets->dom_id);
1185		} else if (targets) {
1186			ZCRYPT_DBF_DBG("%s no match for %d target addrs => ENODEV\n",
1187				       __func__, (int)target_num);
1188		} else {
1189			ZCRYPT_DBF_DBG("%s no match for address ff.ffff => ENODEV\n",
1190				       __func__);
1191		}
1192		rc = -ENODEV;
1193		goto out_free;
1194	}
1195
1196	qid = pref_zq->queue->qid;
1197	rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
1198
1199	spin_lock(&zcrypt_list_lock);
1200	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1201	spin_unlock(&zcrypt_list_lock);
1202
1203out_free:
1204	kfree(targets);
1205out:
1206	ap_release_message(&ap_msg);
1207	if (tr) {
1208		tr->last_rc = rc;
1209		tr->last_qid = qid;
1210	}
1211	trace_s390_zcrypt_rep(xcrb, func_code, rc,
1212			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1213	return rc;
1214}
1215
1216long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
1217{
1218	return _zcrypt_send_ep11_cprb(false, &ap_perms, NULL, xcrb);
1219}
1220EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1221
1222static long zcrypt_rng(char *buffer)
1223{
1224	struct zcrypt_card *zc, *pref_zc;
1225	struct zcrypt_queue *zq, *pref_zq;
1226	unsigned int wgt = 0, pref_wgt = 0;
1227	unsigned int func_code;
1228	struct ap_message ap_msg;
1229	unsigned int domain;
1230	int qid = 0, rc = -ENODEV;
1231	struct module *mod;
1232
1233	trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1234
1235	ap_init_message(&ap_msg);
1236	rc = prep_rng_ap_msg(&ap_msg, &func_code, &domain);
1237	if (rc)
1238		goto out;
1239
1240	pref_zc = NULL;
1241	pref_zq = NULL;
1242	spin_lock(&zcrypt_list_lock);
1243	for_each_zcrypt_card(zc) {
1244		/* Check for usable CCA card */
1245		if (!zc->online || !zc->card->config || zc->card->chkstop ||
1246		    !(zc->card->functions & 0x10000000))
1247			continue;
1248		/* get weight index of the card device	*/
1249		wgt = zc->speed_rating[func_code];
1250		if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
1251			continue;
1252		for_each_zcrypt_queue(zq, zc) {
1253			/* check if device is usable and eligible */
1254			if (!zq->online || !zq->ops->rng ||
1255			    !zq->queue->config || zq->queue->chkstop)
1256				continue;
1257			if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
 
1258				continue;
1259			pref_zc = zc;
1260			pref_zq = zq;
1261			pref_wgt = wgt;
1262		}
1263	}
1264	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1265	spin_unlock(&zcrypt_list_lock);
1266
1267	if (!pref_zq) {
1268		ZCRYPT_DBF_DBG("%s no matching queue found => ENODEV\n",
1269			       __func__);
1270		rc = -ENODEV;
1271		goto out;
1272	}
1273
1274	qid = pref_zq->queue->qid;
1275	rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1276
1277	spin_lock(&zcrypt_list_lock);
1278	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1279	spin_unlock(&zcrypt_list_lock);
1280
1281out:
1282	ap_release_message(&ap_msg);
1283	trace_s390_zcrypt_rep(buffer, func_code, rc,
1284			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1285	return rc;
1286}
1287
1288static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1289{
1290	struct zcrypt_card *zc;
1291	struct zcrypt_queue *zq;
1292	struct zcrypt_device_status *stat;
1293	int card, queue;
1294
1295	memset(devstatus, 0, MAX_ZDEV_ENTRIES
1296	       * sizeof(struct zcrypt_device_status));
1297
1298	spin_lock(&zcrypt_list_lock);
1299	for_each_zcrypt_card(zc) {
1300		for_each_zcrypt_queue(zq, zc) {
1301			card = AP_QID_CARD(zq->queue->qid);
1302			if (card >= MAX_ZDEV_CARDIDS)
1303				continue;
1304			queue = AP_QID_QUEUE(zq->queue->qid);
1305			stat = &devstatus[card * AP_DOMAINS + queue];
1306			stat->hwtype = zc->card->ap_dev.device_type;
1307			stat->functions = zc->card->functions >> 26;
1308			stat->qid = zq->queue->qid;
1309			stat->online = zq->online ? 0x01 : 0x00;
1310		}
1311	}
1312	spin_unlock(&zcrypt_list_lock);
1313}
1314
1315void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1316{
1317	struct zcrypt_card *zc;
1318	struct zcrypt_queue *zq;
1319	struct zcrypt_device_status_ext *stat;
1320	int card, queue;
1321
1322	memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1323	       * sizeof(struct zcrypt_device_status_ext));
1324
1325	spin_lock(&zcrypt_list_lock);
1326	for_each_zcrypt_card(zc) {
1327		for_each_zcrypt_queue(zq, zc) {
1328			card = AP_QID_CARD(zq->queue->qid);
1329			queue = AP_QID_QUEUE(zq->queue->qid);
1330			stat = &devstatus[card * AP_DOMAINS + queue];
1331			stat->hwtype = zc->card->ap_dev.device_type;
1332			stat->functions = zc->card->functions >> 26;
1333			stat->qid = zq->queue->qid;
1334			stat->online = zq->online ? 0x01 : 0x00;
1335		}
1336	}
1337	spin_unlock(&zcrypt_list_lock);
1338}
1339EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1340
1341int zcrypt_device_status_ext(int card, int queue,
1342			     struct zcrypt_device_status_ext *devstat)
1343{
1344	struct zcrypt_card *zc;
1345	struct zcrypt_queue *zq;
1346
1347	memset(devstat, 0, sizeof(*devstat));
1348
1349	spin_lock(&zcrypt_list_lock);
1350	for_each_zcrypt_card(zc) {
1351		for_each_zcrypt_queue(zq, zc) {
1352			if (card == AP_QID_CARD(zq->queue->qid) &&
1353			    queue == AP_QID_QUEUE(zq->queue->qid)) {
1354				devstat->hwtype = zc->card->ap_dev.device_type;
1355				devstat->functions = zc->card->functions >> 26;
1356				devstat->qid = zq->queue->qid;
1357				devstat->online = zq->online ? 0x01 : 0x00;
1358				spin_unlock(&zcrypt_list_lock);
1359				return 0;
1360			}
1361		}
1362	}
1363	spin_unlock(&zcrypt_list_lock);
1364
1365	return -ENODEV;
1366}
1367EXPORT_SYMBOL(zcrypt_device_status_ext);
1368
1369static void zcrypt_status_mask(char status[], size_t max_adapters)
1370{
1371	struct zcrypt_card *zc;
1372	struct zcrypt_queue *zq;
1373	int card;
1374
1375	memset(status, 0, max_adapters);
1376	spin_lock(&zcrypt_list_lock);
1377	for_each_zcrypt_card(zc) {
1378		for_each_zcrypt_queue(zq, zc) {
1379			card = AP_QID_CARD(zq->queue->qid);
1380			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1381			    card >= max_adapters)
1382				continue;
1383			status[card] = zc->online ? zc->user_space_type : 0x0d;
1384		}
1385	}
1386	spin_unlock(&zcrypt_list_lock);
1387}
1388
1389static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1390{
1391	struct zcrypt_card *zc;
1392	struct zcrypt_queue *zq;
1393	int card;
1394
1395	memset(qdepth, 0, max_adapters);
1396	spin_lock(&zcrypt_list_lock);
1397	local_bh_disable();
1398	for_each_zcrypt_card(zc) {
1399		for_each_zcrypt_queue(zq, zc) {
1400			card = AP_QID_CARD(zq->queue->qid);
1401			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1402			    card >= max_adapters)
1403				continue;
1404			spin_lock(&zq->queue->lock);
1405			qdepth[card] =
1406				zq->queue->pendingq_count +
1407				zq->queue->requestq_count;
1408			spin_unlock(&zq->queue->lock);
1409		}
1410	}
1411	local_bh_enable();
1412	spin_unlock(&zcrypt_list_lock);
1413}
1414
1415static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1416{
1417	struct zcrypt_card *zc;
1418	struct zcrypt_queue *zq;
1419	int card;
1420	u64 cnt;
1421
1422	memset(reqcnt, 0, sizeof(int) * max_adapters);
1423	spin_lock(&zcrypt_list_lock);
1424	local_bh_disable();
1425	for_each_zcrypt_card(zc) {
1426		for_each_zcrypt_queue(zq, zc) {
1427			card = AP_QID_CARD(zq->queue->qid);
1428			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1429			    card >= max_adapters)
1430				continue;
1431			spin_lock(&zq->queue->lock);
1432			cnt = zq->queue->total_request_count;
1433			spin_unlock(&zq->queue->lock);
1434			reqcnt[card] = (cnt < UINT_MAX) ? (u32)cnt : UINT_MAX;
1435		}
1436	}
1437	local_bh_enable();
1438	spin_unlock(&zcrypt_list_lock);
1439}
1440
1441static int zcrypt_pendingq_count(void)
1442{
1443	struct zcrypt_card *zc;
1444	struct zcrypt_queue *zq;
1445	int pendingq_count;
1446
1447	pendingq_count = 0;
1448	spin_lock(&zcrypt_list_lock);
1449	local_bh_disable();
1450	for_each_zcrypt_card(zc) {
1451		for_each_zcrypt_queue(zq, zc) {
1452			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1453				continue;
1454			spin_lock(&zq->queue->lock);
1455			pendingq_count += zq->queue->pendingq_count;
1456			spin_unlock(&zq->queue->lock);
1457		}
1458	}
1459	local_bh_enable();
1460	spin_unlock(&zcrypt_list_lock);
1461	return pendingq_count;
1462}
1463
1464static int zcrypt_requestq_count(void)
1465{
1466	struct zcrypt_card *zc;
1467	struct zcrypt_queue *zq;
1468	int requestq_count;
1469
1470	requestq_count = 0;
1471	spin_lock(&zcrypt_list_lock);
1472	local_bh_disable();
1473	for_each_zcrypt_card(zc) {
1474		for_each_zcrypt_queue(zq, zc) {
1475			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1476				continue;
1477			spin_lock(&zq->queue->lock);
1478			requestq_count += zq->queue->requestq_count;
1479			spin_unlock(&zq->queue->lock);
1480		}
1481	}
1482	local_bh_enable();
1483	spin_unlock(&zcrypt_list_lock);
1484	return requestq_count;
1485}
1486
1487static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1488{
1489	int rc;
1490	struct zcrypt_track tr;
1491	struct ica_rsa_modexpo mex;
1492	struct ica_rsa_modexpo __user *umex = (void __user *)arg;
1493
1494	memset(&tr, 0, sizeof(tr));
1495	if (copy_from_user(&mex, umex, sizeof(mex)))
1496		return -EFAULT;
1497
1498#ifdef CONFIG_ZCRYPT_DEBUG
1499	if (mex.inputdatalength & (1U << 31)) {
1500		if (!capable(CAP_SYS_ADMIN))
1501			return -EPERM;
1502		tr.fi.cmd = (u16)(mex.inputdatalength >> 16);
1503	}
1504	mex.inputdatalength &= 0x0000FFFF;
1505#endif
1506
1507	do {
1508		rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1509		if (rc == -EAGAIN)
1510			tr.again_counter++;
1511#ifdef CONFIG_ZCRYPT_DEBUG
1512		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1513			break;
1514#endif
1515	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1516	/* on failure: retry once again after a requested rescan */
1517	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1518		do {
1519			rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1520			if (rc == -EAGAIN)
1521				tr.again_counter++;
1522		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1523	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1524		rc = -EIO;
1525	if (rc) {
1526		ZCRYPT_DBF_DBG("ioctl ICARSAMODEXPO rc=%d\n", rc);
1527		return rc;
1528	}
1529	return put_user(mex.outputdatalength, &umex->outputdatalength);
1530}
1531
1532static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1533{
1534	int rc;
1535	struct zcrypt_track tr;
1536	struct ica_rsa_modexpo_crt crt;
1537	struct ica_rsa_modexpo_crt __user *ucrt = (void __user *)arg;
1538
1539	memset(&tr, 0, sizeof(tr));
1540	if (copy_from_user(&crt, ucrt, sizeof(crt)))
1541		return -EFAULT;
1542
1543#ifdef CONFIG_ZCRYPT_DEBUG
1544	if (crt.inputdatalength & (1U << 31)) {
1545		if (!capable(CAP_SYS_ADMIN))
1546			return -EPERM;
1547		tr.fi.cmd = (u16)(crt.inputdatalength >> 16);
1548	}
1549	crt.inputdatalength &= 0x0000FFFF;
1550#endif
1551
1552	do {
1553		rc = zcrypt_rsa_crt(perms, &tr, &crt);
1554		if (rc == -EAGAIN)
1555			tr.again_counter++;
1556#ifdef CONFIG_ZCRYPT_DEBUG
1557		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1558			break;
1559#endif
1560	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1561	/* on failure: retry once again after a requested rescan */
1562	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1563		do {
1564			rc = zcrypt_rsa_crt(perms, &tr, &crt);
1565			if (rc == -EAGAIN)
1566				tr.again_counter++;
1567		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1568	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1569		rc = -EIO;
1570	if (rc) {
1571		ZCRYPT_DBF_DBG("ioctl ICARSACRT rc=%d\n", rc);
1572		return rc;
1573	}
1574	return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1575}
1576
1577static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1578{
1579	int rc;
1580	struct ica_xcRB xcrb;
1581	struct zcrypt_track tr;
1582	struct ica_xcRB __user *uxcrb = (void __user *)arg;
1583
1584	memset(&tr, 0, sizeof(tr));
1585	if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1586		return -EFAULT;
1587
1588#ifdef CONFIG_ZCRYPT_DEBUG
1589	if ((xcrb.status & 0x8000FFFF) == 0x80004649 /* 'FI' */) {
1590		if (!capable(CAP_SYS_ADMIN))
1591			return -EPERM;
1592		tr.fi.cmd = (u16)(xcrb.status >> 16);
1593	}
1594	xcrb.status = 0;
1595#endif
1596
1597	do {
1598		rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb);
1599		if (rc == -EAGAIN)
1600			tr.again_counter++;
1601#ifdef CONFIG_ZCRYPT_DEBUG
1602		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1603			break;
1604#endif
1605	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1606	/* on failure: retry once again after a requested rescan */
1607	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1608		do {
1609			rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb);
1610			if (rc == -EAGAIN)
1611				tr.again_counter++;
1612		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1613	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1614		rc = -EIO;
1615	if (rc)
1616		ZCRYPT_DBF_DBG("ioctl ZSENDCPRB rc=%d status=0x%x\n",
1617			       rc, xcrb.status);
1618	if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1619		return -EFAULT;
1620	return rc;
1621}
1622
1623static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1624{
1625	int rc;
1626	struct ep11_urb xcrb;
1627	struct zcrypt_track tr;
1628	struct ep11_urb __user *uxcrb = (void __user *)arg;
1629
1630	memset(&tr, 0, sizeof(tr));
1631	if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1632		return -EFAULT;
1633
1634#ifdef CONFIG_ZCRYPT_DEBUG
1635	if (xcrb.req_len & (1ULL << 63)) {
1636		if (!capable(CAP_SYS_ADMIN))
1637			return -EPERM;
1638		tr.fi.cmd = (u16)(xcrb.req_len >> 48);
1639	}
1640	xcrb.req_len &= 0x0000FFFFFFFFFFFFULL;
1641#endif
1642
1643	do {
1644		rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1645		if (rc == -EAGAIN)
1646			tr.again_counter++;
1647#ifdef CONFIG_ZCRYPT_DEBUG
1648		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1649			break;
1650#endif
1651	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1652	/* on failure: retry once again after a requested rescan */
1653	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1654		do {
1655			rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1656			if (rc == -EAGAIN)
1657				tr.again_counter++;
1658		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1659	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1660		rc = -EIO;
1661	if (rc)
1662		ZCRYPT_DBF_DBG("ioctl ZSENDEP11CPRB rc=%d\n", rc);
1663	if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1664		return -EFAULT;
1665	return rc;
1666}
1667
1668static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1669				  unsigned long arg)
1670{
1671	int rc;
1672	struct ap_perms *perms =
1673		(struct ap_perms *)filp->private_data;
1674
1675	rc = zcrypt_check_ioctl(perms, cmd);
1676	if (rc)
1677		return rc;
1678
1679	switch (cmd) {
1680	case ICARSAMODEXPO:
1681		return icarsamodexpo_ioctl(perms, arg);
1682	case ICARSACRT:
1683		return icarsacrt_ioctl(perms, arg);
1684	case ZSECSENDCPRB:
1685		return zsecsendcprb_ioctl(perms, arg);
1686	case ZSENDEP11CPRB:
1687		return zsendep11cprb_ioctl(perms, arg);
1688	case ZCRYPT_DEVICE_STATUS: {
1689		struct zcrypt_device_status_ext *device_status;
1690		size_t total_size = MAX_ZDEV_ENTRIES_EXT
1691			* sizeof(struct zcrypt_device_status_ext);
1692
1693		device_status = kzalloc(total_size, GFP_KERNEL);
1694		if (!device_status)
1695			return -ENOMEM;
1696		zcrypt_device_status_mask_ext(device_status);
1697		if (copy_to_user((char __user *)arg, device_status,
1698				 total_size))
1699			rc = -EFAULT;
1700		kfree(device_status);
1701		return rc;
1702	}
1703	case ZCRYPT_STATUS_MASK: {
1704		char status[AP_DEVICES];
1705
1706		zcrypt_status_mask(status, AP_DEVICES);
1707		if (copy_to_user((char __user *)arg, status, sizeof(status)))
1708			return -EFAULT;
1709		return 0;
1710	}
1711	case ZCRYPT_QDEPTH_MASK: {
1712		char qdepth[AP_DEVICES];
1713
1714		zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1715		if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
1716			return -EFAULT;
1717		return 0;
1718	}
1719	case ZCRYPT_PERDEV_REQCNT: {
1720		u32 *reqcnt;
1721
1722		reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1723		if (!reqcnt)
1724			return -ENOMEM;
1725		zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1726		if (copy_to_user((int __user *)arg, reqcnt,
1727				 sizeof(u32) * AP_DEVICES))
1728			rc = -EFAULT;
1729		kfree(reqcnt);
1730		return rc;
1731	}
1732	case Z90STAT_REQUESTQ_COUNT:
1733		return put_user(zcrypt_requestq_count(), (int __user *)arg);
1734	case Z90STAT_PENDINGQ_COUNT:
1735		return put_user(zcrypt_pendingq_count(), (int __user *)arg);
1736	case Z90STAT_TOTALOPEN_COUNT:
1737		return put_user(atomic_read(&zcrypt_open_count),
1738				(int __user *)arg);
1739	case Z90STAT_DOMAIN_INDEX:
1740		return put_user(ap_domain_index, (int __user *)arg);
1741	/*
1742	 * Deprecated ioctls
1743	 */
1744	case ZDEVICESTATUS: {
1745		/* the old ioctl supports only 64 adapters */
1746		struct zcrypt_device_status *device_status;
1747		size_t total_size = MAX_ZDEV_ENTRIES
1748			* sizeof(struct zcrypt_device_status);
1749
1750		device_status = kzalloc(total_size, GFP_KERNEL);
1751		if (!device_status)
1752			return -ENOMEM;
1753		zcrypt_device_status_mask(device_status);
1754		if (copy_to_user((char __user *)arg, device_status,
1755				 total_size))
1756			rc = -EFAULT;
1757		kfree(device_status);
1758		return rc;
1759	}
1760	case Z90STAT_STATUS_MASK: {
1761		/* the old ioctl supports only 64 adapters */
1762		char status[MAX_ZDEV_CARDIDS];
1763
1764		zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1765		if (copy_to_user((char __user *)arg, status, sizeof(status)))
1766			return -EFAULT;
1767		return 0;
1768	}
1769	case Z90STAT_QDEPTH_MASK: {
1770		/* the old ioctl supports only 64 adapters */
1771		char qdepth[MAX_ZDEV_CARDIDS];
1772
1773		zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1774		if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
1775			return -EFAULT;
1776		return 0;
1777	}
1778	case Z90STAT_PERDEV_REQCNT: {
1779		/* the old ioctl supports only 64 adapters */
1780		u32 reqcnt[MAX_ZDEV_CARDIDS];
1781
1782		zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1783		if (copy_to_user((int __user *)arg, reqcnt, sizeof(reqcnt)))
1784			return -EFAULT;
1785		return 0;
1786	}
1787	/* unknown ioctl number */
1788	default:
1789		ZCRYPT_DBF_DBG("unknown ioctl 0x%08x\n", cmd);
1790		return -ENOIOCTLCMD;
1791	}
1792}
1793
1794#ifdef CONFIG_COMPAT
1795/*
1796 * ioctl32 conversion routines
1797 */
1798struct compat_ica_rsa_modexpo {
1799	compat_uptr_t	inputdata;
1800	unsigned int	inputdatalength;
1801	compat_uptr_t	outputdata;
1802	unsigned int	outputdatalength;
1803	compat_uptr_t	b_key;
1804	compat_uptr_t	n_modulus;
1805};
1806
1807static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1808			    unsigned int cmd, unsigned long arg)
1809{
1810	struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1811	struct compat_ica_rsa_modexpo mex32;
1812	struct ica_rsa_modexpo mex64;
1813	struct zcrypt_track tr;
1814	long rc;
1815
1816	memset(&tr, 0, sizeof(tr));
1817	if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1818		return -EFAULT;
1819	mex64.inputdata = compat_ptr(mex32.inputdata);
1820	mex64.inputdatalength = mex32.inputdatalength;
1821	mex64.outputdata = compat_ptr(mex32.outputdata);
1822	mex64.outputdatalength = mex32.outputdatalength;
1823	mex64.b_key = compat_ptr(mex32.b_key);
1824	mex64.n_modulus = compat_ptr(mex32.n_modulus);
1825	do {
1826		rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1827		if (rc == -EAGAIN)
1828			tr.again_counter++;
1829	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1830	/* on failure: retry once again after a requested rescan */
1831	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1832		do {
1833			rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1834			if (rc == -EAGAIN)
1835				tr.again_counter++;
1836		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1837	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1838		rc = -EIO;
1839	if (rc)
1840		return rc;
1841	return put_user(mex64.outputdatalength,
1842			&umex32->outputdatalength);
1843}
1844
1845struct compat_ica_rsa_modexpo_crt {
1846	compat_uptr_t	inputdata;
1847	unsigned int	inputdatalength;
1848	compat_uptr_t	outputdata;
1849	unsigned int	outputdatalength;
1850	compat_uptr_t	bp_key;
1851	compat_uptr_t	bq_key;
1852	compat_uptr_t	np_prime;
1853	compat_uptr_t	nq_prime;
1854	compat_uptr_t	u_mult_inv;
1855};
1856
1857static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1858				unsigned int cmd, unsigned long arg)
1859{
1860	struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1861	struct compat_ica_rsa_modexpo_crt crt32;
1862	struct ica_rsa_modexpo_crt crt64;
1863	struct zcrypt_track tr;
1864	long rc;
1865
1866	memset(&tr, 0, sizeof(tr));
1867	if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1868		return -EFAULT;
1869	crt64.inputdata = compat_ptr(crt32.inputdata);
1870	crt64.inputdatalength = crt32.inputdatalength;
1871	crt64.outputdata = compat_ptr(crt32.outputdata);
1872	crt64.outputdatalength = crt32.outputdatalength;
1873	crt64.bp_key = compat_ptr(crt32.bp_key);
1874	crt64.bq_key = compat_ptr(crt32.bq_key);
1875	crt64.np_prime = compat_ptr(crt32.np_prime);
1876	crt64.nq_prime = compat_ptr(crt32.nq_prime);
1877	crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1878	do {
1879		rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1880		if (rc == -EAGAIN)
1881			tr.again_counter++;
1882	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1883	/* on failure: retry once again after a requested rescan */
1884	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1885		do {
1886			rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1887			if (rc == -EAGAIN)
1888				tr.again_counter++;
1889		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1890	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1891		rc = -EIO;
1892	if (rc)
1893		return rc;
1894	return put_user(crt64.outputdatalength,
1895			&ucrt32->outputdatalength);
1896}
1897
1898struct compat_ica_xcrb {
1899	unsigned short	agent_ID;
1900	unsigned int	user_defined;
1901	unsigned short	request_ID;
1902	unsigned int	request_control_blk_length;
1903	unsigned char	padding1[16 - sizeof(compat_uptr_t)];
1904	compat_uptr_t	request_control_blk_addr;
1905	unsigned int	request_data_length;
1906	char		padding2[16 - sizeof(compat_uptr_t)];
1907	compat_uptr_t	request_data_address;
1908	unsigned int	reply_control_blk_length;
1909	char		padding3[16 - sizeof(compat_uptr_t)];
1910	compat_uptr_t	reply_control_blk_addr;
1911	unsigned int	reply_data_length;
1912	char		padding4[16 - sizeof(compat_uptr_t)];
1913	compat_uptr_t	reply_data_addr;
1914	unsigned short	priority_window;
1915	unsigned int	status;
1916} __packed;
1917
1918static long trans_xcrb32(struct ap_perms *perms, struct file *filp,
1919			 unsigned int cmd, unsigned long arg)
1920{
1921	struct compat_ica_xcrb __user *uxcrb32 = compat_ptr(arg);
1922	struct compat_ica_xcrb xcrb32;
1923	struct zcrypt_track tr;
1924	struct ica_xcRB xcrb64;
1925	long rc;
1926
1927	memset(&tr, 0, sizeof(tr));
1928	if (copy_from_user(&xcrb32, uxcrb32, sizeof(xcrb32)))
1929		return -EFAULT;
1930	xcrb64.agent_ID = xcrb32.agent_ID;
1931	xcrb64.user_defined = xcrb32.user_defined;
1932	xcrb64.request_ID = xcrb32.request_ID;
1933	xcrb64.request_control_blk_length =
1934		xcrb32.request_control_blk_length;
1935	xcrb64.request_control_blk_addr =
1936		compat_ptr(xcrb32.request_control_blk_addr);
1937	xcrb64.request_data_length =
1938		xcrb32.request_data_length;
1939	xcrb64.request_data_address =
1940		compat_ptr(xcrb32.request_data_address);
1941	xcrb64.reply_control_blk_length =
1942		xcrb32.reply_control_blk_length;
1943	xcrb64.reply_control_blk_addr =
1944		compat_ptr(xcrb32.reply_control_blk_addr);
1945	xcrb64.reply_data_length = xcrb32.reply_data_length;
1946	xcrb64.reply_data_addr =
1947		compat_ptr(xcrb32.reply_data_addr);
1948	xcrb64.priority_window = xcrb32.priority_window;
1949	xcrb64.status = xcrb32.status;
1950	do {
1951		rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb64);
1952		if (rc == -EAGAIN)
1953			tr.again_counter++;
1954	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1955	/* on failure: retry once again after a requested rescan */
1956	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1957		do {
1958			rc = _zcrypt_send_cprb(true, perms, &tr, &xcrb64);
1959			if (rc == -EAGAIN)
1960				tr.again_counter++;
1961		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1962	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1963		rc = -EIO;
1964	xcrb32.reply_control_blk_length = xcrb64.reply_control_blk_length;
1965	xcrb32.reply_data_length = xcrb64.reply_data_length;
1966	xcrb32.status = xcrb64.status;
1967	if (copy_to_user(uxcrb32, &xcrb32, sizeof(xcrb32)))
1968		return -EFAULT;
1969	return rc;
1970}
1971
1972static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1973				unsigned long arg)
1974{
1975	int rc;
1976	struct ap_perms *perms =
1977		(struct ap_perms *)filp->private_data;
1978
1979	rc = zcrypt_check_ioctl(perms, cmd);
1980	if (rc)
1981		return rc;
1982
1983	if (cmd == ICARSAMODEXPO)
1984		return trans_modexpo32(perms, filp, cmd, arg);
1985	if (cmd == ICARSACRT)
1986		return trans_modexpo_crt32(perms, filp, cmd, arg);
1987	if (cmd == ZSECSENDCPRB)
1988		return trans_xcrb32(perms, filp, cmd, arg);
1989	return zcrypt_unlocked_ioctl(filp, cmd, arg);
1990}
1991#endif
1992
1993/*
1994 * Misc device file operations.
1995 */
1996static const struct file_operations zcrypt_fops = {
1997	.owner		= THIS_MODULE,
1998	.read		= zcrypt_read,
1999	.write		= zcrypt_write,
2000	.unlocked_ioctl	= zcrypt_unlocked_ioctl,
2001#ifdef CONFIG_COMPAT
2002	.compat_ioctl	= zcrypt_compat_ioctl,
2003#endif
2004	.open		= zcrypt_open,
2005	.release	= zcrypt_release,
2006	.llseek		= no_llseek,
2007};
2008
2009/*
2010 * Misc device.
2011 */
2012static struct miscdevice zcrypt_misc_device = {
2013	.minor	    = MISC_DYNAMIC_MINOR,
2014	.name	    = "z90crypt",
2015	.fops	    = &zcrypt_fops,
2016};
2017
2018static int zcrypt_rng_device_count;
2019static u32 *zcrypt_rng_buffer;
2020static int zcrypt_rng_buffer_index;
2021static DEFINE_MUTEX(zcrypt_rng_mutex);
2022
2023static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
2024{
2025	int rc;
2026
2027	/*
2028	 * We don't need locking here because the RNG API guarantees serialized
2029	 * read method calls.
2030	 */
2031	if (zcrypt_rng_buffer_index == 0) {
2032		rc = zcrypt_rng((char *)zcrypt_rng_buffer);
2033		/* on failure: retry once again after a requested rescan */
2034		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
2035			rc = zcrypt_rng((char *)zcrypt_rng_buffer);
2036		if (rc < 0)
2037			return -EIO;
2038		zcrypt_rng_buffer_index = rc / sizeof(*data);
2039	}
2040	*data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
2041	return sizeof(*data);
2042}
2043
2044static struct hwrng zcrypt_rng_dev = {
2045	.name		= "zcrypt",
2046	.data_read	= zcrypt_rng_data_read,
2047	.quality	= 990,
2048};
2049
2050int zcrypt_rng_device_add(void)
2051{
2052	int rc = 0;
2053
2054	mutex_lock(&zcrypt_rng_mutex);
2055	if (zcrypt_rng_device_count == 0) {
2056		zcrypt_rng_buffer = (u32 *)get_zeroed_page(GFP_KERNEL);
2057		if (!zcrypt_rng_buffer) {
2058			rc = -ENOMEM;
2059			goto out;
2060		}
2061		zcrypt_rng_buffer_index = 0;
 
 
2062		rc = hwrng_register(&zcrypt_rng_dev);
2063		if (rc)
2064			goto out_free;
2065		zcrypt_rng_device_count = 1;
2066	} else {
2067		zcrypt_rng_device_count++;
2068	}
2069	mutex_unlock(&zcrypt_rng_mutex);
2070	return 0;
2071
2072out_free:
2073	free_page((unsigned long)zcrypt_rng_buffer);
2074out:
2075	mutex_unlock(&zcrypt_rng_mutex);
2076	return rc;
2077}
2078
2079void zcrypt_rng_device_remove(void)
2080{
2081	mutex_lock(&zcrypt_rng_mutex);
2082	zcrypt_rng_device_count--;
2083	if (zcrypt_rng_device_count == 0) {
2084		hwrng_unregister(&zcrypt_rng_dev);
2085		free_page((unsigned long)zcrypt_rng_buffer);
2086	}
2087	mutex_unlock(&zcrypt_rng_mutex);
2088}
2089
2090/*
2091 * Wait until the zcrypt api is operational.
2092 * The AP bus scan and the binding of ap devices to device drivers is
2093 * an asynchronous job. This function waits until these initial jobs
2094 * are done and so the zcrypt api should be ready to serve crypto
2095 * requests - if there are resources available. The function uses an
2096 * internal timeout of 60s. The very first caller will either wait for
2097 * ap bus bindings complete or the timeout happens. This state will be
2098 * remembered for further callers which will only be blocked until a
2099 * decision is made (timeout or bindings complete).
2100 * On timeout -ETIME is returned, on success the return value is 0.
2101 */
2102int zcrypt_wait_api_operational(void)
2103{
2104	static DEFINE_MUTEX(zcrypt_wait_api_lock);
2105	static int zcrypt_wait_api_state;
2106	int rc;
2107
2108	rc = mutex_lock_interruptible(&zcrypt_wait_api_lock);
2109	if (rc)
2110		return rc;
2111
2112	switch (zcrypt_wait_api_state) {
2113	case 0:
2114		/* initial state, invoke wait for the ap bus complete */
2115		rc = ap_wait_init_apqn_bindings_complete(
2116			msecs_to_jiffies(60 * 1000));
2117		switch (rc) {
2118		case 0:
2119			/* ap bus bindings are complete */
2120			zcrypt_wait_api_state = 1;
2121			break;
2122		case -EINTR:
2123			/* interrupted, go back to caller */
2124			break;
2125		case -ETIME:
2126			/* timeout */
2127			ZCRYPT_DBF_WARN("%s ap_wait_init_apqn_bindings_complete()=ETIME\n",
2128					__func__);
2129			zcrypt_wait_api_state = -ETIME;
2130			break;
2131		default:
2132			/* other failure */
2133			ZCRYPT_DBF_DBG("%s ap_wait_init_apqn_bindings_complete()=%d\n",
2134				       __func__, rc);
2135			break;
2136		}
2137		break;
2138	case 1:
2139		/* a previous caller already found ap bus bindings complete */
2140		rc = 0;
2141		break;
2142	default:
2143		/* a previous caller had timeout or other failure */
2144		rc = zcrypt_wait_api_state;
2145		break;
2146	}
2147
2148	mutex_unlock(&zcrypt_wait_api_lock);
2149
2150	return rc;
2151}
2152EXPORT_SYMBOL(zcrypt_wait_api_operational);
2153
2154int __init zcrypt_debug_init(void)
2155{
2156	zcrypt_dbf_info = debug_register("zcrypt", 2, 1,
2157					 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2158	debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
2159	debug_set_level(zcrypt_dbf_info, DBF_ERR);
2160
2161	return 0;
2162}
2163
2164void zcrypt_debug_exit(void)
2165{
2166	debug_unregister(zcrypt_dbf_info);
2167}
2168
2169#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2170
2171static int __init zcdn_init(void)
2172{
2173	int rc;
2174
2175	/* create a new class 'zcrypt' */
2176	zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
2177	if (IS_ERR(zcrypt_class)) {
2178		rc = PTR_ERR(zcrypt_class);
2179		goto out_class_create_failed;
2180	}
2181	zcrypt_class->dev_release = zcdn_device_release;
2182
2183	/* alloc device minor range */
2184	rc = alloc_chrdev_region(&zcrypt_devt,
2185				 0, ZCRYPT_MAX_MINOR_NODES,
2186				 ZCRYPT_NAME);
2187	if (rc)
2188		goto out_alloc_chrdev_failed;
2189
2190	cdev_init(&zcrypt_cdev, &zcrypt_fops);
2191	zcrypt_cdev.owner = THIS_MODULE;
2192	rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2193	if (rc)
2194		goto out_cdev_add_failed;
2195
2196	/* need some class specific sysfs attributes */
2197	rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
2198	if (rc)
2199		goto out_class_create_file_1_failed;
2200	rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
2201	if (rc)
2202		goto out_class_create_file_2_failed;
2203
2204	return 0;
2205
2206out_class_create_file_2_failed:
2207	class_remove_file(zcrypt_class, &class_attr_zcdn_create);
2208out_class_create_file_1_failed:
2209	cdev_del(&zcrypt_cdev);
2210out_cdev_add_failed:
2211	unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2212out_alloc_chrdev_failed:
2213	class_destroy(zcrypt_class);
2214out_class_create_failed:
2215	return rc;
2216}
2217
2218static void zcdn_exit(void)
2219{
2220	class_remove_file(zcrypt_class, &class_attr_zcdn_create);
2221	class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
2222	zcdn_destroy_all();
2223	cdev_del(&zcrypt_cdev);
2224	unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2225	class_destroy(zcrypt_class);
2226}
2227
2228#endif
2229
2230/*
2231 * zcrypt_api_init(): Module initialization.
2232 *
2233 * The module initialization code.
2234 */
2235int __init zcrypt_api_init(void)
2236{
2237	int rc;
2238
2239	rc = zcrypt_debug_init();
2240	if (rc)
2241		goto out;
2242
2243#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2244	rc = zcdn_init();
2245	if (rc)
2246		goto out;
2247#endif
2248
2249	/* Register the request sprayer. */
2250	rc = misc_register(&zcrypt_misc_device);
2251	if (rc < 0)
2252		goto out_misc_register_failed;
2253
2254	zcrypt_msgtype6_init();
2255	zcrypt_msgtype50_init();
2256
2257	return 0;
2258
2259out_misc_register_failed:
2260#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2261	zcdn_exit();
2262#endif
2263	zcrypt_debug_exit();
2264out:
2265	return rc;
2266}
2267
2268/*
2269 * zcrypt_api_exit(): Module termination.
2270 *
2271 * The module termination code.
2272 */
2273void __exit zcrypt_api_exit(void)
2274{
2275#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2276	zcdn_exit();
2277#endif
2278	misc_deregister(&zcrypt_misc_device);
2279	zcrypt_msgtype6_exit();
2280	zcrypt_msgtype50_exit();
2281	zcrypt_ccamisc_exit();
2282	zcrypt_ep11misc_exit();
2283	zcrypt_debug_exit();
2284}
2285
2286module_init(zcrypt_api_init);
2287module_exit(zcrypt_api_exit);
v5.9
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   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>
   7 *
   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>
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/init.h>
  17#include <linux/interrupt.h>
  18#include <linux/miscdevice.h>
  19#include <linux/fs.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>
  29
  30#define CREATE_TRACE_POINTS
  31#include <asm/trace/zcrypt.h>
  32
  33#include "zcrypt_api.h"
  34#include "zcrypt_debug.h"
  35
  36#include "zcrypt_msgtype6.h"
  37#include "zcrypt_msgtype50.h"
  38#include "zcrypt_ccamisc.h"
  39#include "zcrypt_ep11misc.h"
  40
  41/*
  42 * Module description.
  43 */
  44MODULE_AUTHOR("IBM Corporation");
  45MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  46		   "Copyright IBM Corp. 2001, 2012");
  47MODULE_LICENSE("GPL");
  48
  49/*
  50 * zcrypt tracepoint functions
  51 */
  52EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
  53EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
  54
  55static int zcrypt_hwrng_seed = 1;
  56module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
  57MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
  58
  59DEFINE_SPINLOCK(zcrypt_list_lock);
  60LIST_HEAD(zcrypt_card_list);
  61int zcrypt_device_count;
  62
  63static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  64static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
  65
  66atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
  67EXPORT_SYMBOL(zcrypt_rescan_req);
  68
  69static LIST_HEAD(zcrypt_ops_list);
  70
  71/* Zcrypt related debug feature stuff. */
  72debug_info_t *zcrypt_dbf_info;
  73
  74/**
  75 * Process a rescan of the transport layer.
  76 *
  77 * Returns 1, if the rescan has been processed, otherwise 0.
  78 */
  79static inline int zcrypt_process_rescan(void)
  80{
  81	if (atomic_read(&zcrypt_rescan_req)) {
  82		atomic_set(&zcrypt_rescan_req, 0);
  83		atomic_inc(&zcrypt_rescan_count);
  84		ap_bus_force_rescan();
  85		ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
  86			   atomic_inc_return(&zcrypt_rescan_count));
  87		return 1;
  88	}
  89	return 0;
  90}
  91
  92void zcrypt_msgtype_register(struct zcrypt_ops *zops)
  93{
  94	list_add_tail(&zops->list, &zcrypt_ops_list);
  95}
  96
  97void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
  98{
  99	list_del_init(&zops->list);
 100}
 101
 102struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
 103{
 104	struct zcrypt_ops *zops;
 105
 106	list_for_each_entry(zops, &zcrypt_ops_list, list)
 107		if ((zops->variant == variant) &&
 108		    (!strncmp(zops->name, name, sizeof(zops->name))))
 109			return zops;
 110	return NULL;
 111}
 112EXPORT_SYMBOL(zcrypt_msgtype);
 113
 114/*
 115 * Multi device nodes extension functions.
 116 */
 117
 118#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
 119
 120struct zcdn_device;
 121
 122static struct class *zcrypt_class;
 123static dev_t zcrypt_devt;
 124static struct cdev zcrypt_cdev;
 125
 126struct zcdn_device {
 127	struct device device;
 128	struct ap_perms perms;
 129};
 130
 131#define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
 132
 133#define ZCDN_MAX_NAME 32
 134
 135static int zcdn_create(const char *name);
 136static int zcdn_destroy(const char *name);
 137
 138/*
 139 * Find zcdn device by name.
 140 * Returns reference to the zcdn device which needs to be released
 141 * with put_device() after use.
 142 */
 143static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
 144{
 145	struct device *dev = class_find_device_by_name(zcrypt_class, name);
 146
 147	return dev ? to_zcdn_dev(dev) : NULL;
 148}
 149
 150/*
 151 * Find zcdn device by devt value.
 152 * Returns reference to the zcdn device which needs to be released
 153 * with put_device() after use.
 154 */
 155static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
 156{
 157	struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
 158
 159	return dev ? to_zcdn_dev(dev) : NULL;
 160}
 161
 162static ssize_t ioctlmask_show(struct device *dev,
 163			      struct device_attribute *attr,
 164			      char *buf)
 165{
 166	int i, rc;
 167	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 168
 169	if (mutex_lock_interruptible(&ap_perms_mutex))
 170		return -ERESTARTSYS;
 171
 172	buf[0] = '0';
 173	buf[1] = 'x';
 174	for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
 175		snprintf(buf + 2 + 2 * i * sizeof(long),
 176			 PAGE_SIZE - 2 - 2 * i * sizeof(long),
 177			 "%016lx", zcdndev->perms.ioctlm[i]);
 178	buf[2 + 2 * i * sizeof(long)] = '\n';
 179	buf[2 + 2 * i * sizeof(long) + 1] = '\0';
 180	rc = 2 + 2 * i * sizeof(long) + 1;
 181
 182	mutex_unlock(&ap_perms_mutex);
 183
 184	return rc;
 185}
 186
 187static ssize_t ioctlmask_store(struct device *dev,
 188			       struct device_attribute *attr,
 189			       const char *buf, size_t count)
 190{
 191	int rc;
 192	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 193
 194	rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
 195			       AP_IOCTLS, &ap_perms_mutex);
 196	if (rc)
 197		return rc;
 198
 199	return count;
 200}
 201
 202static DEVICE_ATTR_RW(ioctlmask);
 203
 204static ssize_t apmask_show(struct device *dev,
 205			   struct device_attribute *attr,
 206			   char *buf)
 207{
 208	int i, rc;
 209	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 210
 211	if (mutex_lock_interruptible(&ap_perms_mutex))
 212		return -ERESTARTSYS;
 213
 214	buf[0] = '0';
 215	buf[1] = 'x';
 216	for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
 217		snprintf(buf + 2 + 2 * i * sizeof(long),
 218			 PAGE_SIZE - 2 - 2 * i * sizeof(long),
 219			 "%016lx", zcdndev->perms.apm[i]);
 220	buf[2 + 2 * i * sizeof(long)] = '\n';
 221	buf[2 + 2 * i * sizeof(long) + 1] = '\0';
 222	rc = 2 + 2 * i * sizeof(long) + 1;
 223
 224	mutex_unlock(&ap_perms_mutex);
 225
 226	return rc;
 227}
 228
 229static ssize_t apmask_store(struct device *dev,
 230			    struct device_attribute *attr,
 231			    const char *buf, size_t count)
 232{
 233	int rc;
 234	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 235
 236	rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
 237			       AP_DEVICES, &ap_perms_mutex);
 238	if (rc)
 239		return rc;
 240
 241	return count;
 242}
 243
 244static DEVICE_ATTR_RW(apmask);
 245
 246static ssize_t aqmask_show(struct device *dev,
 247			   struct device_attribute *attr,
 248			   char *buf)
 249{
 250	int i, rc;
 251	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 252
 253	if (mutex_lock_interruptible(&ap_perms_mutex))
 254		return -ERESTARTSYS;
 255
 256	buf[0] = '0';
 257	buf[1] = 'x';
 258	for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
 259		snprintf(buf + 2 + 2 * i * sizeof(long),
 260			 PAGE_SIZE - 2 - 2 * i * sizeof(long),
 261			 "%016lx", zcdndev->perms.aqm[i]);
 262	buf[2 + 2 * i * sizeof(long)] = '\n';
 263	buf[2 + 2 * i * sizeof(long) + 1] = '\0';
 264	rc = 2 + 2 * i * sizeof(long) + 1;
 265
 266	mutex_unlock(&ap_perms_mutex);
 267
 268	return rc;
 269}
 270
 271static ssize_t aqmask_store(struct device *dev,
 272			    struct device_attribute *attr,
 273			    const char *buf, size_t count)
 274{
 275	int rc;
 276	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 277
 278	rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
 279			       AP_DOMAINS, &ap_perms_mutex);
 280	if (rc)
 281		return rc;
 282
 283	return count;
 284}
 285
 286static DEVICE_ATTR_RW(aqmask);
 287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 288static struct attribute *zcdn_dev_attrs[] = {
 289	&dev_attr_ioctlmask.attr,
 290	&dev_attr_apmask.attr,
 291	&dev_attr_aqmask.attr,
 
 292	NULL
 293};
 294
 295static struct attribute_group zcdn_dev_attr_group = {
 296	.attrs = zcdn_dev_attrs
 297};
 298
 299static const struct attribute_group *zcdn_dev_attr_groups[] = {
 300	&zcdn_dev_attr_group,
 301	NULL
 302};
 303
 304static ssize_t zcdn_create_store(struct class *class,
 305				 struct class_attribute *attr,
 306				 const char *buf, size_t count)
 307{
 308	int rc;
 309	char name[ZCDN_MAX_NAME];
 310
 311	strncpy(name, skip_spaces(buf), sizeof(name));
 312	name[sizeof(name) - 1] = '\0';
 313
 314	rc = zcdn_create(strim(name));
 315
 316	return rc ? rc : count;
 317}
 318
 319static const struct class_attribute class_attr_zcdn_create =
 320	__ATTR(create, 0600, NULL, zcdn_create_store);
 321
 322static ssize_t zcdn_destroy_store(struct class *class,
 323				  struct class_attribute *attr,
 324				  const char *buf, size_t count)
 325{
 326	int rc;
 327	char name[ZCDN_MAX_NAME];
 328
 329	strncpy(name, skip_spaces(buf), sizeof(name));
 330	name[sizeof(name) - 1] = '\0';
 331
 332	rc = zcdn_destroy(strim(name));
 333
 334	return rc ? rc : count;
 335}
 336
 337static const struct class_attribute class_attr_zcdn_destroy =
 338	__ATTR(destroy, 0600, NULL, zcdn_destroy_store);
 339
 340static void zcdn_device_release(struct device *dev)
 341{
 342	struct zcdn_device *zcdndev = to_zcdn_dev(dev);
 343
 344	ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
 345		   MAJOR(dev->devt), MINOR(dev->devt));
 346
 347	kfree(zcdndev);
 348}
 349
 350static int zcdn_create(const char *name)
 351{
 352	dev_t devt;
 353	int i, rc = 0;
 354	char nodename[ZCDN_MAX_NAME];
 355	struct zcdn_device *zcdndev;
 356
 357	if (mutex_lock_interruptible(&ap_perms_mutex))
 358		return -ERESTARTSYS;
 359
 360	/* check if device node with this name already exists */
 361	if (name[0]) {
 362		zcdndev = find_zcdndev_by_name(name);
 363		if (zcdndev) {
 364			put_device(&zcdndev->device);
 365			rc = -EEXIST;
 366			goto unlockout;
 367		}
 368	}
 369
 370	/* find an unused minor number */
 371	for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
 372		devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
 373		zcdndev = find_zcdndev_by_devt(devt);
 374		if (zcdndev)
 375			put_device(&zcdndev->device);
 376		else
 377			break;
 378	}
 379	if (i == ZCRYPT_MAX_MINOR_NODES) {
 380		rc = -ENOSPC;
 381		goto unlockout;
 382	}
 383
 384	/* alloc and prepare a new zcdn device */
 385	zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
 386	if (!zcdndev) {
 387		rc = -ENOMEM;
 388		goto unlockout;
 389	}
 390	zcdndev->device.release = zcdn_device_release;
 391	zcdndev->device.class = zcrypt_class;
 392	zcdndev->device.devt = devt;
 393	zcdndev->device.groups = zcdn_dev_attr_groups;
 394	if (name[0])
 395		strncpy(nodename, name, sizeof(nodename));
 396	else
 397		snprintf(nodename, sizeof(nodename),
 398			 ZCRYPT_NAME "_%d", (int) MINOR(devt));
 399	nodename[sizeof(nodename)-1] = '\0';
 400	if (dev_set_name(&zcdndev->device, nodename)) {
 401		rc = -EINVAL;
 402		goto unlockout;
 403	}
 404	rc = device_register(&zcdndev->device);
 405	if (rc) {
 406		put_device(&zcdndev->device);
 407		goto unlockout;
 408	}
 409
 410	ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
 411		   MAJOR(devt), MINOR(devt));
 412
 413unlockout:
 414	mutex_unlock(&ap_perms_mutex);
 415	return rc;
 416}
 417
 418static int zcdn_destroy(const char *name)
 419{
 420	int rc = 0;
 421	struct zcdn_device *zcdndev;
 422
 423	if (mutex_lock_interruptible(&ap_perms_mutex))
 424		return -ERESTARTSYS;
 425
 426	/* try to find this zcdn device */
 427	zcdndev = find_zcdndev_by_name(name);
 428	if (!zcdndev) {
 429		rc = -ENOENT;
 430		goto unlockout;
 431	}
 432
 433	/*
 434	 * The zcdn device is not hard destroyed. It is subject to
 435	 * reference counting and thus just needs to be unregistered.
 436	 */
 437	put_device(&zcdndev->device);
 438	device_unregister(&zcdndev->device);
 439
 440unlockout:
 441	mutex_unlock(&ap_perms_mutex);
 442	return rc;
 443}
 444
 445static void zcdn_destroy_all(void)
 446{
 447	int i;
 448	dev_t devt;
 449	struct zcdn_device *zcdndev;
 450
 451	mutex_lock(&ap_perms_mutex);
 452	for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
 453		devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
 454		zcdndev = find_zcdndev_by_devt(devt);
 455		if (zcdndev) {
 456			put_device(&zcdndev->device);
 457			device_unregister(&zcdndev->device);
 458		}
 459	}
 460	mutex_unlock(&ap_perms_mutex);
 461}
 462
 463#endif
 464
 465/**
 466 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
 467 *
 468 * This function is not supported beyond zcrypt 1.3.1.
 469 */
 470static ssize_t zcrypt_read(struct file *filp, char __user *buf,
 471			   size_t count, loff_t *f_pos)
 472{
 473	return -EPERM;
 474}
 475
 476/**
 477 * zcrypt_write(): Not allowed.
 478 *
 479 * Write is is not allowed
 480 */
 481static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
 482			    size_t count, loff_t *f_pos)
 483{
 484	return -EPERM;
 485}
 486
 487/**
 488 * zcrypt_open(): Count number of users.
 489 *
 490 * Device open function to count number of users.
 491 */
 492static int zcrypt_open(struct inode *inode, struct file *filp)
 493{
 494	struct ap_perms *perms = &ap_perms;
 495
 496#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
 497	if (filp->f_inode->i_cdev == &zcrypt_cdev) {
 498		struct zcdn_device *zcdndev;
 499
 500		if (mutex_lock_interruptible(&ap_perms_mutex))
 501			return -ERESTARTSYS;
 502		zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
 503		/* find returns a reference, no get_device() needed */
 504		mutex_unlock(&ap_perms_mutex);
 505		if (zcdndev)
 506			perms = &zcdndev->perms;
 507	}
 508#endif
 509	filp->private_data = (void *) perms;
 510
 511	atomic_inc(&zcrypt_open_count);
 512	return stream_open(inode, filp);
 513}
 514
 515/**
 516 * zcrypt_release(): Count number of users.
 517 *
 518 * Device close function to count number of users.
 519 */
 520static int zcrypt_release(struct inode *inode, struct file *filp)
 521{
 522#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
 523	if (filp->f_inode->i_cdev == &zcrypt_cdev) {
 524		struct zcdn_device *zcdndev;
 525
 526		mutex_lock(&ap_perms_mutex);
 527		zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
 528		mutex_unlock(&ap_perms_mutex);
 529		if (zcdndev) {
 530			/* 2 puts here: one for find, one for open */
 531			put_device(&zcdndev->device);
 532			put_device(&zcdndev->device);
 533		}
 534	}
 535#endif
 536
 537	atomic_dec(&zcrypt_open_count);
 538	return 0;
 539}
 540
 541static inline int zcrypt_check_ioctl(struct ap_perms *perms,
 542				     unsigned int cmd)
 543{
 544	int rc = -EPERM;
 545	int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
 546
 547	if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
 548		if (test_bit_inv(ioctlnr, perms->ioctlm))
 549			rc = 0;
 550	}
 551
 552	if (rc)
 553		ZCRYPT_DBF(DBF_WARN,
 554			   "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
 555			   ioctlnr, rc);
 556
 557	return rc;
 558}
 559
 560static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
 561{
 562	return test_bit_inv(card, perms->apm) ? true : false;
 563}
 564
 565static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
 566{
 567	return test_bit_inv(queue, perms->aqm) ? true : false;
 568}
 569
 570static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
 571						     struct zcrypt_queue *zq,
 572						     struct module **pmod,
 573						     unsigned int weight)
 574{
 575	if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
 576		return NULL;
 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);
 581	zq->request_count++;
 582	*pmod = zq->queue->ap_dev.drv->driver.owner;
 583	return zq;
 584}
 585
 586static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
 587				     struct zcrypt_queue *zq,
 588				     struct module *mod,
 589				     unsigned int weight)
 590{
 591	zq->request_count--;
 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);
 596	module_put(mod);
 597}
 598
 599static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
 600				       struct zcrypt_card *pref_zc,
 601				       unsigned int weight,
 602				       unsigned int pref_weight)
 603{
 604	if (!pref_zc)
 605		return false;
 606	weight += atomic_read(&zc->load);
 607	pref_weight += atomic_read(&pref_zc->load);
 608	if (weight == pref_weight)
 609		return atomic64_read(&zc->card->total_request_count) >
 610			atomic64_read(&pref_zc->card->total_request_count);
 611	return weight > pref_weight;
 612}
 613
 614static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
 615					struct zcrypt_queue *pref_zq,
 616					unsigned int weight,
 617					unsigned int pref_weight)
 618{
 619	if (!pref_zq)
 620		return false;
 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;
 627}
 628
 629/*
 630 * zcrypt ioctls.
 631 */
 632static long zcrypt_rsa_modexpo(struct ap_perms *perms,
 
 633			       struct ica_rsa_modexpo *mex)
 634{
 635	struct zcrypt_card *zc, *pref_zc;
 636	struct zcrypt_queue *zq, *pref_zq;
 637	unsigned int weight = 0, pref_weight = 0;
 
 638	unsigned int func_code;
 639	int qid = 0, rc = -ENODEV;
 640	struct module *mod;
 641
 642	trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
 643
 
 
 
 
 
 
 
 644	if (mex->outputdatalength < mex->inputdatalength) {
 645		func_code = 0;
 646		rc = -EINVAL;
 647		goto out;
 648	}
 649
 650	/*
 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
 654	 */
 655	mex->outputdatalength = mex->inputdatalength;
 656
 657	rc = get_rsa_modex_fc(mex, &func_code);
 658	if (rc)
 659		goto out;
 660
 661	pref_zc = NULL;
 662	pref_zq = NULL;
 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))
 
 667			continue;
 668		/* Check for size limits */
 669		if (zc->min_mod_size > mex->inputdatalength ||
 670		    zc->max_mod_size < mex->inputdatalength)
 671			continue;
 672		/* check if device node has admission for this card */
 673		if (!zcrypt_check_card(perms, zc->card->id))
 674			continue;
 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))
 
 
 
 
 678			continue;
 679		for_each_zcrypt_queue(zq, zc) {
 680			/* check if device is online and eligible */
 681			if (!zq->online || !zq->ops->rsa_modexpo)
 
 682				continue;
 683			/* check if device node has admission for this queue */
 684			if (!zcrypt_check_queue(perms,
 685						AP_QID_QUEUE(zq->queue->qid)))
 686				continue;
 687			if (zcrypt_queue_compare(zq, pref_zq,
 688						 weight, pref_weight))
 
 
 
 
 689				continue;
 690			pref_zc = zc;
 691			pref_zq = zq;
 692			pref_weight = weight;
 693		}
 694	}
 695	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
 696	spin_unlock(&zcrypt_list_lock);
 697
 698	if (!pref_zq) {
 
 
 699		rc = -ENODEV;
 700		goto out;
 701	}
 702
 703	qid = pref_zq->queue->qid;
 704	rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
 705
 706	spin_lock(&zcrypt_list_lock);
 707	zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
 708	spin_unlock(&zcrypt_list_lock);
 709
 710out:
 
 
 
 
 
 711	trace_s390_zcrypt_rep(mex, func_code, rc,
 712			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
 713	return rc;
 714}
 715
 716static long zcrypt_rsa_crt(struct ap_perms *perms,
 
 717			   struct ica_rsa_modexpo_crt *crt)
 718{
 719	struct zcrypt_card *zc, *pref_zc;
 720	struct zcrypt_queue *zq, *pref_zq;
 721	unsigned int weight = 0, pref_weight = 0;
 
 722	unsigned int func_code;
 723	int qid = 0, rc = -ENODEV;
 724	struct module *mod;
 725
 726	trace_s390_zcrypt_req(crt, TP_ICARSACRT);
 727
 
 
 
 
 
 
 
 728	if (crt->outputdatalength < crt->inputdatalength) {
 729		func_code = 0;
 730		rc = -EINVAL;
 731		goto out;
 732	}
 733
 734	/*
 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
 738	 */
 739	crt->outputdatalength = crt->inputdatalength;
 740
 741	rc = get_rsa_crt_fc(crt, &func_code);
 742	if (rc)
 743		goto out;
 744
 745	pref_zc = NULL;
 746	pref_zq = NULL;
 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))
 
 751			continue;
 752		/* Check for size limits */
 753		if (zc->min_mod_size > crt->inputdatalength ||
 754		    zc->max_mod_size < crt->inputdatalength)
 755			continue;
 756		/* check if device node has admission for this card */
 757		if (!zcrypt_check_card(perms, zc->card->id))
 758			continue;
 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))
 
 
 
 
 762			continue;
 763		for_each_zcrypt_queue(zq, zc) {
 764			/* check if device is online and eligible */
 765			if (!zq->online || !zq->ops->rsa_modexpo_crt)
 
 766				continue;
 767			/* check if device node has admission for this queue */
 768			if (!zcrypt_check_queue(perms,
 769						AP_QID_QUEUE(zq->queue->qid)))
 770				continue;
 771			if (zcrypt_queue_compare(zq, pref_zq,
 772						 weight, pref_weight))
 
 
 
 
 773				continue;
 774			pref_zc = zc;
 775			pref_zq = zq;
 776			pref_weight = weight;
 777		}
 778	}
 779	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
 780	spin_unlock(&zcrypt_list_lock);
 781
 782	if (!pref_zq) {
 
 
 783		rc = -ENODEV;
 784		goto out;
 785	}
 786
 787	qid = pref_zq->queue->qid;
 788	rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
 789
 790	spin_lock(&zcrypt_list_lock);
 791	zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
 792	spin_unlock(&zcrypt_list_lock);
 793
 794out:
 
 
 
 
 
 795	trace_s390_zcrypt_rep(crt, func_code, rc,
 796			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
 797	return rc;
 798}
 799
 800static long _zcrypt_send_cprb(struct ap_perms *perms,
 801			      struct ica_xcRB *xcRB)
 
 802{
 803	struct zcrypt_card *zc, *pref_zc;
 804	struct zcrypt_queue *zq, *pref_zq;
 805	struct ap_message ap_msg;
 806	unsigned int weight = 0, pref_weight = 0;
 807	unsigned int func_code;
 808	unsigned short *domain, tdom;
 809	int qid = 0, rc = -ENODEV;
 810	struct module *mod;
 811
 812	trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
 813
 814	xcRB->status = 0;
 815	ap_init_message(&ap_msg);
 816	rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
 
 
 
 
 
 
 
 
 
 
 
 817	if (rc)
 818		goto out;
 819
 
 
 
 
 
 
 
 
 
 
 
 
 820	/*
 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.
 823	 */
 824	tdom = *domain;
 825	if (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;
 830
 831	pref_zc = NULL;
 832	pref_zq = NULL;
 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))
 
 837			continue;
 838		/* Check for user selected CCA card */
 839		if (xcRB->user_defined != AUTOSELECT &&
 840		    xcRB->user_defined != zc->card->id)
 
 
 
 841			continue;
 842		/* check if device node has admission for this card */
 843		if (!zcrypt_check_card(perms, zc->card->id))
 844			continue;
 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))
 
 
 
 
 848			continue;
 849		for_each_zcrypt_queue(zq, zc) {
 850			/* check if device is online and eligible */
 851			if (!zq->online ||
 852			    !zq->ops->send_cprb ||
 853			    (tdom != AUTOSEL_DOM &&
 854			     tdom != AP_QID_QUEUE(zq->queue->qid)))
 855				continue;
 856			/* check if device node has admission for this queue */
 857			if (!zcrypt_check_queue(perms,
 858						AP_QID_QUEUE(zq->queue->qid)))
 859				continue;
 860			if (zcrypt_queue_compare(zq, pref_zq,
 861						 weight, pref_weight))
 
 
 
 
 862				continue;
 863			pref_zc = zc;
 864			pref_zq = zq;
 865			pref_weight = weight;
 866		}
 867	}
 868	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
 869	spin_unlock(&zcrypt_list_lock);
 870
 871	if (!pref_zq) {
 
 
 872		rc = -ENODEV;
 873		goto out;
 874	}
 875
 876	/* in case of auto select, provide the correct domain */
 877	qid = pref_zq->queue->qid;
 878	if (*domain == AUTOSEL_DOM)
 879		*domain = AP_QID_QUEUE(qid);
 880
 881	rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
 
 
 
 
 
 
 
 
 882
 883	spin_lock(&zcrypt_list_lock);
 884	zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
 885	spin_unlock(&zcrypt_list_lock);
 886
 887out:
 888	ap_release_message(&ap_msg);
 889	trace_s390_zcrypt_rep(xcRB, func_code, rc,
 
 
 
 
 890			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
 891	return rc;
 892}
 893
 894long zcrypt_send_cprb(struct ica_xcRB *xcRB)
 895{
 896	return _zcrypt_send_cprb(&ap_perms, xcRB);
 897}
 898EXPORT_SYMBOL(zcrypt_send_cprb);
 899
 900static bool is_desired_ep11_card(unsigned int dev_id,
 901				 unsigned short target_num,
 902				 struct ep11_target_dev *targets)
 903{
 904	while (target_num-- > 0) {
 905		if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
 906			return true;
 907		targets++;
 908	}
 909	return false;
 910}
 911
 912static bool is_desired_ep11_queue(unsigned int dev_qid,
 913				  unsigned short target_num,
 914				  struct ep11_target_dev *targets)
 915{
 916	int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
 917
 918	while (target_num-- > 0) {
 919		if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
 920		    (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
 921			return true;
 922		targets++;
 923	}
 924	return false;
 925}
 926
 927static long _zcrypt_send_ep11_cprb(struct ap_perms *perms,
 
 928				   struct ep11_urb *xcrb)
 929{
 930	struct zcrypt_card *zc, *pref_zc;
 931	struct zcrypt_queue *zq, *pref_zq;
 932	struct ep11_target_dev *targets;
 933	unsigned short target_num;
 934	unsigned int weight = 0, pref_weight = 0;
 935	unsigned int func_code;
 936	struct ap_message ap_msg;
 937	int qid = 0, rc = -ENODEV;
 938	struct module *mod;
 939
 940	trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
 941
 942	ap_init_message(&ap_msg);
 943
 944	target_num = (unsigned short) xcrb->targets_num;
 
 
 
 
 
 945
 946	/* empty list indicates autoselect (all available targets) */
 947	targets = NULL;
 948	if (target_num != 0) {
 949		struct ep11_target_dev __user *uptr;
 950
 951		targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
 952		if (!targets) {
 953			func_code = 0;
 954			rc = -ENOMEM;
 955			goto out;
 956		}
 957
 958		uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
 959		if (copy_from_user(targets, uptr,
 960				   target_num * sizeof(*targets))) {
 961			func_code = 0;
 962			rc = -EFAULT;
 963			goto out_free;
 964		}
 965	}
 966
 967	rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
 968	if (rc)
 969		goto out_free;
 970
 
 
 
 
 
 
 
 
 
 
 
 
 971	pref_zc = NULL;
 972	pref_zq = NULL;
 973	spin_lock(&zcrypt_list_lock);
 974	for_each_zcrypt_card(zc) {
 975		/* Check for online EP11 cards */
 976		if (!zc->online || !(zc->card->functions & 0x04000000))
 
 977			continue;
 978		/* Check for user selected EP11 card */
 979		if (targets &&
 980		    !is_desired_ep11_card(zc->card->id, target_num, targets))
 981			continue;
 
 
 
 982		/* check if device node has admission for this card */
 983		if (!zcrypt_check_card(perms, zc->card->id))
 984			continue;
 985		/* get weight index of the card device	*/
 986		weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
 987		if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
 
 
 
 
 988			continue;
 989		for_each_zcrypt_queue(zq, zc) {
 990			/* check if device is online and eligible */
 991			if (!zq->online ||
 992			    !zq->ops->send_ep11_cprb ||
 993			    (targets &&
 994			     !is_desired_ep11_queue(zq->queue->qid,
 995						    target_num, targets)))
 996				continue;
 997			/* check if device node has admission for this queue */
 998			if (!zcrypt_check_queue(perms,
 999						AP_QID_QUEUE(zq->queue->qid)))
1000				continue;
1001			if (zcrypt_queue_compare(zq, pref_zq,
1002						 weight, pref_weight))
 
 
 
 
1003				continue;
1004			pref_zc = zc;
1005			pref_zq = zq;
1006			pref_weight = weight;
1007		}
1008	}
1009	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1010	spin_unlock(&zcrypt_list_lock);
1011
1012	if (!pref_zq) {
 
 
 
 
 
 
 
 
 
 
 
1013		rc = -ENODEV;
1014		goto out_free;
1015	}
1016
1017	qid = pref_zq->queue->qid;
1018	rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
1019
1020	spin_lock(&zcrypt_list_lock);
1021	zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1022	spin_unlock(&zcrypt_list_lock);
1023
1024out_free:
1025	kfree(targets);
1026out:
1027	ap_release_message(&ap_msg);
 
 
 
 
1028	trace_s390_zcrypt_rep(xcrb, func_code, rc,
1029			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1030	return rc;
1031}
1032
1033long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
1034{
1035	return _zcrypt_send_ep11_cprb(&ap_perms, xcrb);
1036}
1037EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1038
1039static long zcrypt_rng(char *buffer)
1040{
1041	struct zcrypt_card *zc, *pref_zc;
1042	struct zcrypt_queue *zq, *pref_zq;
1043	unsigned int weight = 0, pref_weight = 0;
1044	unsigned int func_code;
1045	struct ap_message ap_msg;
1046	unsigned int domain;
1047	int qid = 0, rc = -ENODEV;
1048	struct module *mod;
1049
1050	trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1051
1052	ap_init_message(&ap_msg);
1053	rc = get_rng_fc(&ap_msg, &func_code, &domain);
1054	if (rc)
1055		goto out;
1056
1057	pref_zc = NULL;
1058	pref_zq = NULL;
1059	spin_lock(&zcrypt_list_lock);
1060	for_each_zcrypt_card(zc) {
1061		/* Check for online CCA cards */
1062		if (!zc->online || !(zc->card->functions & 0x10000000))
 
1063			continue;
1064		/* get weight index of the card device	*/
1065		weight = zc->speed_rating[func_code];
1066		if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1067			continue;
1068		for_each_zcrypt_queue(zq, zc) {
1069			/* check if device is online and eligible */
1070			if (!zq->online || !zq->ops->rng)
 
1071				continue;
1072			if (zcrypt_queue_compare(zq, pref_zq,
1073						 weight, pref_weight))
1074				continue;
1075			pref_zc = zc;
1076			pref_zq = zq;
1077			pref_weight = weight;
1078		}
1079	}
1080	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1081	spin_unlock(&zcrypt_list_lock);
1082
1083	if (!pref_zq) {
 
 
1084		rc = -ENODEV;
1085		goto out;
1086	}
1087
1088	qid = pref_zq->queue->qid;
1089	rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1090
1091	spin_lock(&zcrypt_list_lock);
1092	zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1093	spin_unlock(&zcrypt_list_lock);
1094
1095out:
1096	ap_release_message(&ap_msg);
1097	trace_s390_zcrypt_rep(buffer, func_code, rc,
1098			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1099	return rc;
1100}
1101
1102static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1103{
1104	struct zcrypt_card *zc;
1105	struct zcrypt_queue *zq;
1106	struct zcrypt_device_status *stat;
1107	int card, queue;
1108
1109	memset(devstatus, 0, MAX_ZDEV_ENTRIES
1110	       * sizeof(struct zcrypt_device_status));
1111
1112	spin_lock(&zcrypt_list_lock);
1113	for_each_zcrypt_card(zc) {
1114		for_each_zcrypt_queue(zq, zc) {
1115			card = AP_QID_CARD(zq->queue->qid);
1116			if (card >= MAX_ZDEV_CARDIDS)
1117				continue;
1118			queue = AP_QID_QUEUE(zq->queue->qid);
1119			stat = &devstatus[card * AP_DOMAINS + queue];
1120			stat->hwtype = zc->card->ap_dev.device_type;
1121			stat->functions = zc->card->functions >> 26;
1122			stat->qid = zq->queue->qid;
1123			stat->online = zq->online ? 0x01 : 0x00;
1124		}
1125	}
1126	spin_unlock(&zcrypt_list_lock);
1127}
1128
1129void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1130{
1131	struct zcrypt_card *zc;
1132	struct zcrypt_queue *zq;
1133	struct zcrypt_device_status_ext *stat;
1134	int card, queue;
1135
1136	memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1137	       * sizeof(struct zcrypt_device_status_ext));
1138
1139	spin_lock(&zcrypt_list_lock);
1140	for_each_zcrypt_card(zc) {
1141		for_each_zcrypt_queue(zq, zc) {
1142			card = AP_QID_CARD(zq->queue->qid);
1143			queue = AP_QID_QUEUE(zq->queue->qid);
1144			stat = &devstatus[card * AP_DOMAINS + queue];
1145			stat->hwtype = zc->card->ap_dev.device_type;
1146			stat->functions = zc->card->functions >> 26;
1147			stat->qid = zq->queue->qid;
1148			stat->online = zq->online ? 0x01 : 0x00;
1149		}
1150	}
1151	spin_unlock(&zcrypt_list_lock);
1152}
1153EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1154
1155int zcrypt_device_status_ext(int card, int queue,
1156			     struct zcrypt_device_status_ext *devstat)
1157{
1158	struct zcrypt_card *zc;
1159	struct zcrypt_queue *zq;
1160
1161	memset(devstat, 0, sizeof(*devstat));
1162
1163	spin_lock(&zcrypt_list_lock);
1164	for_each_zcrypt_card(zc) {
1165		for_each_zcrypt_queue(zq, zc) {
1166			if (card == AP_QID_CARD(zq->queue->qid) &&
1167			    queue == AP_QID_QUEUE(zq->queue->qid)) {
1168				devstat->hwtype = zc->card->ap_dev.device_type;
1169				devstat->functions = zc->card->functions >> 26;
1170				devstat->qid = zq->queue->qid;
1171				devstat->online = zq->online ? 0x01 : 0x00;
1172				spin_unlock(&zcrypt_list_lock);
1173				return 0;
1174			}
1175		}
1176	}
1177	spin_unlock(&zcrypt_list_lock);
1178
1179	return -ENODEV;
1180}
1181EXPORT_SYMBOL(zcrypt_device_status_ext);
1182
1183static void zcrypt_status_mask(char status[], size_t max_adapters)
1184{
1185	struct zcrypt_card *zc;
1186	struct zcrypt_queue *zq;
1187	int card;
1188
1189	memset(status, 0, max_adapters);
1190	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)
1196				continue;
1197			status[card] = zc->online ? zc->user_space_type : 0x0d;
1198		}
1199	}
1200	spin_unlock(&zcrypt_list_lock);
1201}
1202
1203static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1204{
1205	struct zcrypt_card *zc;
1206	struct zcrypt_queue *zq;
1207	int card;
1208
1209	memset(qdepth, 0, max_adapters);
1210	spin_lock(&zcrypt_list_lock);
1211	local_bh_disable();
1212	for_each_zcrypt_card(zc) {
1213		for_each_zcrypt_queue(zq, zc) {
1214			card = AP_QID_CARD(zq->queue->qid);
1215			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1216			    || card >= max_adapters)
1217				continue;
1218			spin_lock(&zq->queue->lock);
1219			qdepth[card] =
1220				zq->queue->pendingq_count +
1221				zq->queue->requestq_count;
1222			spin_unlock(&zq->queue->lock);
1223		}
1224	}
1225	local_bh_enable();
1226	spin_unlock(&zcrypt_list_lock);
1227}
1228
1229static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1230{
1231	struct zcrypt_card *zc;
1232	struct zcrypt_queue *zq;
1233	int card;
1234	u64 cnt;
1235
1236	memset(reqcnt, 0, sizeof(int) * max_adapters);
1237	spin_lock(&zcrypt_list_lock);
1238	local_bh_disable();
1239	for_each_zcrypt_card(zc) {
1240		for_each_zcrypt_queue(zq, zc) {
1241			card = AP_QID_CARD(zq->queue->qid);
1242			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1243			    || card >= max_adapters)
1244				continue;
1245			spin_lock(&zq->queue->lock);
1246			cnt = zq->queue->total_request_count;
1247			spin_unlock(&zq->queue->lock);
1248			reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX;
1249		}
1250	}
1251	local_bh_enable();
1252	spin_unlock(&zcrypt_list_lock);
1253}
1254
1255static int zcrypt_pendingq_count(void)
1256{
1257	struct zcrypt_card *zc;
1258	struct zcrypt_queue *zq;
1259	int pendingq_count;
1260
1261	pendingq_count = 0;
1262	spin_lock(&zcrypt_list_lock);
1263	local_bh_disable();
1264	for_each_zcrypt_card(zc) {
1265		for_each_zcrypt_queue(zq, zc) {
1266			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1267				continue;
1268			spin_lock(&zq->queue->lock);
1269			pendingq_count += zq->queue->pendingq_count;
1270			spin_unlock(&zq->queue->lock);
1271		}
1272	}
1273	local_bh_enable();
1274	spin_unlock(&zcrypt_list_lock);
1275	return pendingq_count;
1276}
1277
1278static int zcrypt_requestq_count(void)
1279{
1280	struct zcrypt_card *zc;
1281	struct zcrypt_queue *zq;
1282	int requestq_count;
1283
1284	requestq_count = 0;
1285	spin_lock(&zcrypt_list_lock);
1286	local_bh_disable();
1287	for_each_zcrypt_card(zc) {
1288		for_each_zcrypt_queue(zq, zc) {
1289			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1290				continue;
1291			spin_lock(&zq->queue->lock);
1292			requestq_count += zq->queue->requestq_count;
1293			spin_unlock(&zq->queue->lock);
1294		}
1295	}
1296	local_bh_enable();
1297	spin_unlock(&zcrypt_list_lock);
1298	return requestq_count;
1299}
1300
1301static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1302{
1303	int rc;
 
1304	struct ica_rsa_modexpo mex;
1305	struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1306
 
1307	if (copy_from_user(&mex, umex, sizeof(mex)))
1308		return -EFAULT;
 
 
 
 
 
 
 
 
 
 
1309	do {
1310		rc = zcrypt_rsa_modexpo(perms, &mex);
1311	} while (rc == -EAGAIN);
 
 
 
 
 
 
1312	/* on failure: retry once again after a requested rescan */
1313	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1314		do {
1315			rc = zcrypt_rsa_modexpo(perms, &mex);
1316		} while (rc == -EAGAIN);
 
 
 
 
1317	if (rc) {
1318		ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1319		return rc;
1320	}
1321	return put_user(mex.outputdatalength, &umex->outputdatalength);
1322}
1323
1324static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1325{
1326	int rc;
 
1327	struct ica_rsa_modexpo_crt crt;
1328	struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1329
 
1330	if (copy_from_user(&crt, ucrt, sizeof(crt)))
1331		return -EFAULT;
 
 
 
 
 
 
 
 
 
 
1332	do {
1333		rc = zcrypt_rsa_crt(perms, &crt);
1334	} while (rc == -EAGAIN);
 
 
 
 
 
 
1335	/* on failure: retry once again after a requested rescan */
1336	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1337		do {
1338			rc = zcrypt_rsa_crt(perms, &crt);
1339		} while (rc == -EAGAIN);
 
 
 
 
1340	if (rc) {
1341		ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1342		return rc;
1343	}
1344	return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1345}
1346
1347static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1348{
1349	int rc;
1350	struct ica_xcRB xcRB;
1351	struct ica_xcRB __user *uxcRB = (void __user *) arg;
 
1352
1353	if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
 
1354		return -EFAULT;
 
 
 
 
 
 
 
 
 
 
1355	do {
1356		rc = _zcrypt_send_cprb(perms, &xcRB);
1357	} while (rc == -EAGAIN);
 
 
 
 
 
 
1358	/* on failure: retry once again after a requested rescan */
1359	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1360		do {
1361			rc = _zcrypt_send_cprb(perms, &xcRB);
1362		} while (rc == -EAGAIN);
 
 
 
 
1363	if (rc)
1364		ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1365			   rc, xcRB.status);
1366	if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1367		return -EFAULT;
1368	return rc;
1369}
1370
1371static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1372{
1373	int rc;
1374	struct ep11_urb xcrb;
 
1375	struct ep11_urb __user *uxcrb = (void __user *)arg;
1376
 
1377	if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1378		return -EFAULT;
 
 
 
 
 
 
 
 
 
 
1379	do {
1380		rc = _zcrypt_send_ep11_cprb(perms, &xcrb);
1381	} while (rc == -EAGAIN);
 
 
 
 
 
 
1382	/* on failure: retry once again after a requested rescan */
1383	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1384		do {
1385			rc = _zcrypt_send_ep11_cprb(perms, &xcrb);
1386		} while (rc == -EAGAIN);
 
 
 
 
1387	if (rc)
1388		ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1389	if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1390		return -EFAULT;
1391	return rc;
1392}
1393
1394static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1395				  unsigned long arg)
1396{
1397	int rc;
1398	struct ap_perms *perms =
1399		(struct ap_perms *) filp->private_data;
1400
1401	rc = zcrypt_check_ioctl(perms, cmd);
1402	if (rc)
1403		return rc;
1404
1405	switch (cmd) {
1406	case ICARSAMODEXPO:
1407		return icarsamodexpo_ioctl(perms, arg);
1408	case ICARSACRT:
1409		return icarsacrt_ioctl(perms, arg);
1410	case ZSECSENDCPRB:
1411		return zsecsendcprb_ioctl(perms, arg);
1412	case ZSENDEP11CPRB:
1413		return zsendep11cprb_ioctl(perms, arg);
1414	case ZCRYPT_DEVICE_STATUS: {
1415		struct zcrypt_device_status_ext *device_status;
1416		size_t total_size = MAX_ZDEV_ENTRIES_EXT
1417			* sizeof(struct zcrypt_device_status_ext);
1418
1419		device_status = kzalloc(total_size, GFP_KERNEL);
1420		if (!device_status)
1421			return -ENOMEM;
1422		zcrypt_device_status_mask_ext(device_status);
1423		if (copy_to_user((char __user *) arg, device_status,
1424				 total_size))
1425			rc = -EFAULT;
1426		kfree(device_status);
1427		return rc;
1428	}
1429	case ZCRYPT_STATUS_MASK: {
1430		char status[AP_DEVICES];
1431
1432		zcrypt_status_mask(status, AP_DEVICES);
1433		if (copy_to_user((char __user *) arg, status, sizeof(status)))
1434			return -EFAULT;
1435		return 0;
1436	}
1437	case ZCRYPT_QDEPTH_MASK: {
1438		char qdepth[AP_DEVICES];
1439
1440		zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1441		if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1442			return -EFAULT;
1443		return 0;
1444	}
1445	case ZCRYPT_PERDEV_REQCNT: {
1446		u32 *reqcnt;
1447
1448		reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1449		if (!reqcnt)
1450			return -ENOMEM;
1451		zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1452		if (copy_to_user((int __user *) arg, reqcnt,
1453				 sizeof(u32) * AP_DEVICES))
1454			rc = -EFAULT;
1455		kfree(reqcnt);
1456		return rc;
1457	}
1458	case Z90STAT_REQUESTQ_COUNT:
1459		return put_user(zcrypt_requestq_count(), (int __user *) arg);
1460	case Z90STAT_PENDINGQ_COUNT:
1461		return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1462	case Z90STAT_TOTALOPEN_COUNT:
1463		return put_user(atomic_read(&zcrypt_open_count),
1464				(int __user *) arg);
1465	case Z90STAT_DOMAIN_INDEX:
1466		return put_user(ap_domain_index, (int __user *) arg);
1467	/*
1468	 * Deprecated ioctls
1469	 */
1470	case ZDEVICESTATUS: {
1471		/* the old ioctl supports only 64 adapters */
1472		struct zcrypt_device_status *device_status;
1473		size_t total_size = MAX_ZDEV_ENTRIES
1474			* sizeof(struct zcrypt_device_status);
1475
1476		device_status = kzalloc(total_size, GFP_KERNEL);
1477		if (!device_status)
1478			return -ENOMEM;
1479		zcrypt_device_status_mask(device_status);
1480		if (copy_to_user((char __user *) arg, device_status,
1481				 total_size))
1482			rc = -EFAULT;
1483		kfree(device_status);
1484		return rc;
1485	}
1486	case Z90STAT_STATUS_MASK: {
1487		/* the old ioctl supports only 64 adapters */
1488		char status[MAX_ZDEV_CARDIDS];
1489
1490		zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1491		if (copy_to_user((char __user *) arg, status, sizeof(status)))
1492			return -EFAULT;
1493		return 0;
1494	}
1495	case Z90STAT_QDEPTH_MASK: {
1496		/* the old ioctl supports only 64 adapters */
1497		char qdepth[MAX_ZDEV_CARDIDS];
1498
1499		zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1500		if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1501			return -EFAULT;
1502		return 0;
1503	}
1504	case Z90STAT_PERDEV_REQCNT: {
1505		/* the old ioctl supports only 64 adapters */
1506		u32 reqcnt[MAX_ZDEV_CARDIDS];
1507
1508		zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1509		if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1510			return -EFAULT;
1511		return 0;
1512	}
1513	/* unknown ioctl number */
1514	default:
1515		ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1516		return -ENOIOCTLCMD;
1517	}
1518}
1519
1520#ifdef CONFIG_COMPAT
1521/*
1522 * ioctl32 conversion routines
1523 */
1524struct compat_ica_rsa_modexpo {
1525	compat_uptr_t	inputdata;
1526	unsigned int	inputdatalength;
1527	compat_uptr_t	outputdata;
1528	unsigned int	outputdatalength;
1529	compat_uptr_t	b_key;
1530	compat_uptr_t	n_modulus;
1531};
1532
1533static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1534			    unsigned int cmd, unsigned long arg)
1535{
1536	struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1537	struct compat_ica_rsa_modexpo mex32;
1538	struct ica_rsa_modexpo mex64;
 
1539	long rc;
1540
 
1541	if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1542		return -EFAULT;
1543	mex64.inputdata = compat_ptr(mex32.inputdata);
1544	mex64.inputdatalength = mex32.inputdatalength;
1545	mex64.outputdata = compat_ptr(mex32.outputdata);
1546	mex64.outputdatalength = mex32.outputdatalength;
1547	mex64.b_key = compat_ptr(mex32.b_key);
1548	mex64.n_modulus = compat_ptr(mex32.n_modulus);
1549	do {
1550		rc = zcrypt_rsa_modexpo(perms, &mex64);
1551	} while (rc == -EAGAIN);
 
 
1552	/* on failure: retry once again after a requested rescan */
1553	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1554		do {
1555			rc = zcrypt_rsa_modexpo(perms, &mex64);
1556		} while (rc == -EAGAIN);
 
 
 
 
1557	if (rc)
1558		return rc;
1559	return put_user(mex64.outputdatalength,
1560			&umex32->outputdatalength);
1561}
1562
1563struct compat_ica_rsa_modexpo_crt {
1564	compat_uptr_t	inputdata;
1565	unsigned int	inputdatalength;
1566	compat_uptr_t	outputdata;
1567	unsigned int	outputdatalength;
1568	compat_uptr_t	bp_key;
1569	compat_uptr_t	bq_key;
1570	compat_uptr_t	np_prime;
1571	compat_uptr_t	nq_prime;
1572	compat_uptr_t	u_mult_inv;
1573};
1574
1575static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1576				unsigned int cmd, unsigned long arg)
1577{
1578	struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1579	struct compat_ica_rsa_modexpo_crt crt32;
1580	struct ica_rsa_modexpo_crt crt64;
 
1581	long rc;
1582
 
1583	if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1584		return -EFAULT;
1585	crt64.inputdata = compat_ptr(crt32.inputdata);
1586	crt64.inputdatalength = crt32.inputdatalength;
1587	crt64.outputdata = compat_ptr(crt32.outputdata);
1588	crt64.outputdatalength = crt32.outputdatalength;
1589	crt64.bp_key = compat_ptr(crt32.bp_key);
1590	crt64.bq_key = compat_ptr(crt32.bq_key);
1591	crt64.np_prime = compat_ptr(crt32.np_prime);
1592	crt64.nq_prime = compat_ptr(crt32.nq_prime);
1593	crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1594	do {
1595		rc = zcrypt_rsa_crt(perms, &crt64);
1596	} while (rc == -EAGAIN);
 
 
1597	/* on failure: retry once again after a requested rescan */
1598	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1599		do {
1600			rc = zcrypt_rsa_crt(perms, &crt64);
1601		} while (rc == -EAGAIN);
 
 
 
 
1602	if (rc)
1603		return rc;
1604	return put_user(crt64.outputdatalength,
1605			&ucrt32->outputdatalength);
1606}
1607
1608struct compat_ica_xcRB {
1609	unsigned short	agent_ID;
1610	unsigned int	user_defined;
1611	unsigned short	request_ID;
1612	unsigned int	request_control_blk_length;
1613	unsigned char	padding1[16 - sizeof(compat_uptr_t)];
1614	compat_uptr_t	request_control_blk_addr;
1615	unsigned int	request_data_length;
1616	char		padding2[16 - sizeof(compat_uptr_t)];
1617	compat_uptr_t	request_data_address;
1618	unsigned int	reply_control_blk_length;
1619	char		padding3[16 - sizeof(compat_uptr_t)];
1620	compat_uptr_t	reply_control_blk_addr;
1621	unsigned int	reply_data_length;
1622	char		padding4[16 - sizeof(compat_uptr_t)];
1623	compat_uptr_t	reply_data_addr;
1624	unsigned short	priority_window;
1625	unsigned int	status;
1626} __packed;
1627
1628static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1629			 unsigned int cmd, unsigned long arg)
1630{
1631	struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1632	struct compat_ica_xcRB xcRB32;
1633	struct ica_xcRB xcRB64;
 
1634	long rc;
1635
1636	if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
 
1637		return -EFAULT;
1638	xcRB64.agent_ID = xcRB32.agent_ID;
1639	xcRB64.user_defined = xcRB32.user_defined;
1640	xcRB64.request_ID = xcRB32.request_ID;
1641	xcRB64.request_control_blk_length =
1642		xcRB32.request_control_blk_length;
1643	xcRB64.request_control_blk_addr =
1644		compat_ptr(xcRB32.request_control_blk_addr);
1645	xcRB64.request_data_length =
1646		xcRB32.request_data_length;
1647	xcRB64.request_data_address =
1648		compat_ptr(xcRB32.request_data_address);
1649	xcRB64.reply_control_blk_length =
1650		xcRB32.reply_control_blk_length;
1651	xcRB64.reply_control_blk_addr =
1652		compat_ptr(xcRB32.reply_control_blk_addr);
1653	xcRB64.reply_data_length = xcRB32.reply_data_length;
1654	xcRB64.reply_data_addr =
1655		compat_ptr(xcRB32.reply_data_addr);
1656	xcRB64.priority_window = xcRB32.priority_window;
1657	xcRB64.status = xcRB32.status;
1658	do {
1659		rc = _zcrypt_send_cprb(perms, &xcRB64);
1660	} while (rc == -EAGAIN);
 
 
1661	/* on failure: retry once again after a requested rescan */
1662	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1663		do {
1664			rc = _zcrypt_send_cprb(perms, &xcRB64);
1665		} while (rc == -EAGAIN);
1666	xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1667	xcRB32.reply_data_length = xcRB64.reply_data_length;
1668	xcRB32.status = xcRB64.status;
1669	if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
 
 
 
 
1670		return -EFAULT;
1671	return rc;
1672}
1673
1674static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1675			 unsigned long arg)
1676{
1677	int rc;
1678	struct ap_perms *perms =
1679		(struct ap_perms *) filp->private_data;
1680
1681	rc = zcrypt_check_ioctl(perms, cmd);
1682	if (rc)
1683		return rc;
1684
1685	if (cmd == ICARSAMODEXPO)
1686		return trans_modexpo32(perms, filp, cmd, arg);
1687	if (cmd == ICARSACRT)
1688		return trans_modexpo_crt32(perms, filp, cmd, arg);
1689	if (cmd == ZSECSENDCPRB)
1690		return trans_xcRB32(perms, filp, cmd, arg);
1691	return zcrypt_unlocked_ioctl(filp, cmd, arg);
1692}
1693#endif
1694
1695/*
1696 * Misc device file operations.
1697 */
1698static const struct file_operations zcrypt_fops = {
1699	.owner		= THIS_MODULE,
1700	.read		= zcrypt_read,
1701	.write		= zcrypt_write,
1702	.unlocked_ioctl	= zcrypt_unlocked_ioctl,
1703#ifdef CONFIG_COMPAT
1704	.compat_ioctl	= zcrypt_compat_ioctl,
1705#endif
1706	.open		= zcrypt_open,
1707	.release	= zcrypt_release,
1708	.llseek		= no_llseek,
1709};
1710
1711/*
1712 * Misc device.
1713 */
1714static struct miscdevice zcrypt_misc_device = {
1715	.minor	    = MISC_DYNAMIC_MINOR,
1716	.name	    = "z90crypt",
1717	.fops	    = &zcrypt_fops,
1718};
1719
1720static int zcrypt_rng_device_count;
1721static u32 *zcrypt_rng_buffer;
1722static int zcrypt_rng_buffer_index;
1723static DEFINE_MUTEX(zcrypt_rng_mutex);
1724
1725static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1726{
1727	int rc;
1728
1729	/*
1730	 * We don't need locking here because the RNG API guarantees serialized
1731	 * read method calls.
1732	 */
1733	if (zcrypt_rng_buffer_index == 0) {
1734		rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1735		/* on failure: retry once again after a requested rescan */
1736		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1737			rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1738		if (rc < 0)
1739			return -EIO;
1740		zcrypt_rng_buffer_index = rc / sizeof(*data);
1741	}
1742	*data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1743	return sizeof(*data);
1744}
1745
1746static struct hwrng zcrypt_rng_dev = {
1747	.name		= "zcrypt",
1748	.data_read	= zcrypt_rng_data_read,
1749	.quality	= 990,
1750};
1751
1752int zcrypt_rng_device_add(void)
1753{
1754	int rc = 0;
1755
1756	mutex_lock(&zcrypt_rng_mutex);
1757	if (zcrypt_rng_device_count == 0) {
1758		zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1759		if (!zcrypt_rng_buffer) {
1760			rc = -ENOMEM;
1761			goto out;
1762		}
1763		zcrypt_rng_buffer_index = 0;
1764		if (!zcrypt_hwrng_seed)
1765			zcrypt_rng_dev.quality = 0;
1766		rc = hwrng_register(&zcrypt_rng_dev);
1767		if (rc)
1768			goto out_free;
1769		zcrypt_rng_device_count = 1;
1770	} else
1771		zcrypt_rng_device_count++;
 
1772	mutex_unlock(&zcrypt_rng_mutex);
1773	return 0;
1774
1775out_free:
1776	free_page((unsigned long) zcrypt_rng_buffer);
1777out:
1778	mutex_unlock(&zcrypt_rng_mutex);
1779	return rc;
1780}
1781
1782void zcrypt_rng_device_remove(void)
1783{
1784	mutex_lock(&zcrypt_rng_mutex);
1785	zcrypt_rng_device_count--;
1786	if (zcrypt_rng_device_count == 0) {
1787		hwrng_unregister(&zcrypt_rng_dev);
1788		free_page((unsigned long) zcrypt_rng_buffer);
1789	}
1790	mutex_unlock(&zcrypt_rng_mutex);
1791}
1792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1793int __init zcrypt_debug_init(void)
1794{
1795	zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1796					 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1797	debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1798	debug_set_level(zcrypt_dbf_info, DBF_ERR);
1799
1800	return 0;
1801}
1802
1803void zcrypt_debug_exit(void)
1804{
1805	debug_unregister(zcrypt_dbf_info);
1806}
1807
1808#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1809
1810static int __init zcdn_init(void)
1811{
1812	int rc;
1813
1814	/* create a new class 'zcrypt' */
1815	zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1816	if (IS_ERR(zcrypt_class)) {
1817		rc = PTR_ERR(zcrypt_class);
1818		goto out_class_create_failed;
1819	}
1820	zcrypt_class->dev_release = zcdn_device_release;
1821
1822	/* alloc device minor range */
1823	rc = alloc_chrdev_region(&zcrypt_devt,
1824				 0, ZCRYPT_MAX_MINOR_NODES,
1825				 ZCRYPT_NAME);
1826	if (rc)
1827		goto out_alloc_chrdev_failed;
1828
1829	cdev_init(&zcrypt_cdev, &zcrypt_fops);
1830	zcrypt_cdev.owner = THIS_MODULE;
1831	rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1832	if (rc)
1833		goto out_cdev_add_failed;
1834
1835	/* need some class specific sysfs attributes */
1836	rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1837	if (rc)
1838		goto out_class_create_file_1_failed;
1839	rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1840	if (rc)
1841		goto out_class_create_file_2_failed;
1842
1843	return 0;
1844
1845out_class_create_file_2_failed:
1846	class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1847out_class_create_file_1_failed:
1848	cdev_del(&zcrypt_cdev);
1849out_cdev_add_failed:
1850	unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1851out_alloc_chrdev_failed:
1852	class_destroy(zcrypt_class);
1853out_class_create_failed:
1854	return rc;
1855}
1856
1857static void zcdn_exit(void)
1858{
1859	class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1860	class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1861	zcdn_destroy_all();
1862	cdev_del(&zcrypt_cdev);
1863	unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1864	class_destroy(zcrypt_class);
1865}
1866
1867#endif
1868
1869/**
1870 * zcrypt_api_init(): Module initialization.
1871 *
1872 * The module initialization code.
1873 */
1874int __init zcrypt_api_init(void)
1875{
1876	int rc;
1877
1878	rc = zcrypt_debug_init();
1879	if (rc)
1880		goto out;
1881
1882#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1883	rc = zcdn_init();
1884	if (rc)
1885		goto out;
1886#endif
1887
1888	/* Register the request sprayer. */
1889	rc = misc_register(&zcrypt_misc_device);
1890	if (rc < 0)
1891		goto out_misc_register_failed;
1892
1893	zcrypt_msgtype6_init();
1894	zcrypt_msgtype50_init();
1895
1896	return 0;
1897
1898out_misc_register_failed:
1899#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1900	zcdn_exit();
1901#endif
1902	zcrypt_debug_exit();
1903out:
1904	return rc;
1905}
1906
1907/**
1908 * zcrypt_api_exit(): Module termination.
1909 *
1910 * The module termination code.
1911 */
1912void __exit zcrypt_api_exit(void)
1913{
1914#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1915	zcdn_exit();
1916#endif
1917	misc_deregister(&zcrypt_misc_device);
1918	zcrypt_msgtype6_exit();
1919	zcrypt_msgtype50_exit();
1920	zcrypt_ccamisc_exit();
1921	zcrypt_ep11misc_exit();
1922	zcrypt_debug_exit();
1923}
1924
1925module_init(zcrypt_api_init);
1926module_exit(zcrypt_api_exit);