Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v3.1
   1/*
   2 *  linux/drivers/s390/crypto/zcrypt_api.c
   3 *
   4 *  zcrypt 2.1.0
   5 *
   6 *  Copyright (C)  2001, 2006 IBM Corporation
   7 *  Author(s): Robert Burroughs
   8 *	       Eric Rossman (edrossma@us.ibm.com)
   9 *	       Cornelia Huck <cornelia.huck@de.ibm.com>
  10 *
  11 *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  12 *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  13 *				  Ralph Wuerthner <rwuerthn@de.ibm.com>
 
  14 *
  15 * This program is free software; you can redistribute it and/or modify
  16 * it under the terms of the GNU General Public License as published by
  17 * the Free Software Foundation; either version 2, or (at your option)
  18 * any later version.
  19 *
  20 * This program is distributed in the hope that it will be useful,
  21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23 * GNU General Public License for more details.
  24 *
  25 * You should have received a copy of the GNU General Public License
  26 * along with this program; if not, write to the Free Software
  27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28 */
  29
  30#include <linux/module.h>
  31#include <linux/init.h>
  32#include <linux/interrupt.h>
  33#include <linux/miscdevice.h>
  34#include <linux/fs.h>
  35#include <linux/proc_fs.h>
  36#include <linux/seq_file.h>
  37#include <linux/compat.h>
  38#include <linux/slab.h>
  39#include <linux/atomic.h>
  40#include <asm/uaccess.h>
  41#include <linux/hw_random.h>
 
 
  42
 
  43#include "zcrypt_api.h"
  44
 
 
  45/*
  46 * Module description.
  47 */
  48MODULE_AUTHOR("IBM Corporation");
  49MODULE_DESCRIPTION("Cryptographic Coprocessor interface, "
  50		   "Copyright 2001, 2006 IBM Corporation");
  51MODULE_LICENSE("GPL");
  52
  53static DEFINE_SPINLOCK(zcrypt_device_lock);
  54static LIST_HEAD(zcrypt_device_list);
  55static int zcrypt_device_count = 0;
  56static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
 
 
 
 
  57
  58static int zcrypt_rng_device_add(void);
  59static void zcrypt_rng_device_remove(void);
  60
 
 
 
 
 
 
 
  61/*
  62 * Device attributes common for all crypto devices.
  63 */
  64static ssize_t zcrypt_type_show(struct device *dev,
  65				struct device_attribute *attr, char *buf)
  66{
  67	struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  68	return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
  69}
  70
  71static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
  72
  73static ssize_t zcrypt_online_show(struct device *dev,
  74				  struct device_attribute *attr, char *buf)
  75{
  76	struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  77	return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
  78}
  79
  80static ssize_t zcrypt_online_store(struct device *dev,
  81				   struct device_attribute *attr,
  82				   const char *buf, size_t count)
  83{
  84	struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  85	int online;
  86
  87	if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  88		return -EINVAL;
  89	zdev->online = online;
 
 
  90	if (!online)
  91		ap_flush_queue(zdev->ap_dev);
  92	return count;
  93}
  94
  95static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
  96
  97static struct attribute * zcrypt_device_attrs[] = {
  98	&dev_attr_type.attr,
  99	&dev_attr_online.attr,
 100	NULL,
 101};
 102
 103static struct attribute_group zcrypt_device_attr_group = {
 104	.attrs = zcrypt_device_attrs,
 105};
 106
 107/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 108 * __zcrypt_increase_preference(): Increase preference of a crypto device.
 109 * @zdev: Pointer the crypto device
 110 *
 111 * Move the device towards the head of the device list.
 112 * Need to be called while holding the zcrypt device list lock.
 113 * Note: cards with speed_rating of 0 are kept at the end of the list.
 114 */
 115static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
 116{
 117	struct zcrypt_device *tmp;
 118	struct list_head *l;
 119
 120	if (zdev->speed_rating == 0)
 121		return;
 122	for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
 123		tmp = list_entry(l, struct zcrypt_device, list);
 124		if ((tmp->request_count + 1) * tmp->speed_rating <=
 125		    (zdev->request_count + 1) * zdev->speed_rating &&
 126		    tmp->speed_rating != 0)
 127			break;
 128	}
 129	if (l == zdev->list.prev)
 130		return;
 131	/* Move zdev behind l */
 132	list_move(&zdev->list, l);
 133}
 134
 135/**
 136 * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
 137 * @zdev: Pointer to a crypto device.
 138 *
 139 * Move the device towards the tail of the device list.
 140 * Need to be called while holding the zcrypt device list lock.
 141 * Note: cards with speed_rating of 0 are kept at the end of the list.
 142 */
 143static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
 144{
 145	struct zcrypt_device *tmp;
 146	struct list_head *l;
 147
 148	if (zdev->speed_rating == 0)
 149		return;
 150	for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
 151		tmp = list_entry(l, struct zcrypt_device, list);
 152		if ((tmp->request_count + 1) * tmp->speed_rating >
 153		    (zdev->request_count + 1) * zdev->speed_rating ||
 154		    tmp->speed_rating == 0)
 155			break;
 156	}
 157	if (l == zdev->list.next)
 158		return;
 159	/* Move zdev before l */
 160	list_move_tail(&zdev->list, l);
 161}
 162
 163static void zcrypt_device_release(struct kref *kref)
 164{
 165	struct zcrypt_device *zdev =
 166		container_of(kref, struct zcrypt_device, refcount);
 167	zcrypt_device_free(zdev);
 168}
 169
 170void zcrypt_device_get(struct zcrypt_device *zdev)
 171{
 172	kref_get(&zdev->refcount);
 173}
 174EXPORT_SYMBOL(zcrypt_device_get);
 175
 176int zcrypt_device_put(struct zcrypt_device *zdev)
 177{
 178	return kref_put(&zdev->refcount, zcrypt_device_release);
 179}
 180EXPORT_SYMBOL(zcrypt_device_put);
 181
 182struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
 183{
 184	struct zcrypt_device *zdev;
 185
 186	zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
 187	if (!zdev)
 188		return NULL;
 189	zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
 190	if (!zdev->reply.message)
 191		goto out_free;
 192	zdev->reply.length = max_response_size;
 193	spin_lock_init(&zdev->lock);
 194	INIT_LIST_HEAD(&zdev->list);
 
 195	return zdev;
 196
 197out_free:
 198	kfree(zdev);
 199	return NULL;
 200}
 201EXPORT_SYMBOL(zcrypt_device_alloc);
 202
 203void zcrypt_device_free(struct zcrypt_device *zdev)
 204{
 205	kfree(zdev->reply.message);
 206	kfree(zdev);
 207}
 208EXPORT_SYMBOL(zcrypt_device_free);
 209
 210/**
 211 * zcrypt_device_register() - Register a crypto device.
 212 * @zdev: Pointer to a crypto device
 213 *
 214 * Register a crypto device. Returns 0 if successful.
 215 */
 216int zcrypt_device_register(struct zcrypt_device *zdev)
 217{
 218	int rc;
 219
 
 
 220	rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
 221				&zcrypt_device_attr_group);
 222	if (rc)
 223		goto out;
 224	get_device(&zdev->ap_dev->device);
 225	kref_init(&zdev->refcount);
 226	spin_lock_bh(&zcrypt_device_lock);
 227	zdev->online = 1;	/* New devices are online by default. */
 
 
 228	list_add_tail(&zdev->list, &zcrypt_device_list);
 229	__zcrypt_increase_preference(zdev);
 230	zcrypt_device_count++;
 231	spin_unlock_bh(&zcrypt_device_lock);
 232	if (zdev->ops->rng) {
 233		rc = zcrypt_rng_device_add();
 234		if (rc)
 235			goto out_unregister;
 236	}
 237	return 0;
 238
 239out_unregister:
 240	spin_lock_bh(&zcrypt_device_lock);
 241	zcrypt_device_count--;
 242	list_del_init(&zdev->list);
 243	spin_unlock_bh(&zcrypt_device_lock);
 244	sysfs_remove_group(&zdev->ap_dev->device.kobj,
 245			   &zcrypt_device_attr_group);
 246	put_device(&zdev->ap_dev->device);
 247	zcrypt_device_put(zdev);
 248out:
 249	return rc;
 250}
 251EXPORT_SYMBOL(zcrypt_device_register);
 252
 253/**
 254 * zcrypt_device_unregister(): Unregister a crypto device.
 255 * @zdev: Pointer to crypto device
 256 *
 257 * Unregister a crypto device.
 258 */
 259void zcrypt_device_unregister(struct zcrypt_device *zdev)
 260{
 261	if (zdev->ops->rng)
 262		zcrypt_rng_device_remove();
 263	spin_lock_bh(&zcrypt_device_lock);
 264	zcrypt_device_count--;
 265	list_del_init(&zdev->list);
 266	spin_unlock_bh(&zcrypt_device_lock);
 267	sysfs_remove_group(&zdev->ap_dev->device.kobj,
 268			   &zcrypt_device_attr_group);
 269	put_device(&zdev->ap_dev->device);
 270	zcrypt_device_put(zdev);
 271}
 272EXPORT_SYMBOL(zcrypt_device_unregister);
 273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 274/**
 275 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
 276 *
 277 * This function is not supported beyond zcrypt 1.3.1.
 278 */
 279static ssize_t zcrypt_read(struct file *filp, char __user *buf,
 280			   size_t count, loff_t *f_pos)
 281{
 282	return -EPERM;
 283}
 284
 285/**
 286 * zcrypt_write(): Not allowed.
 287 *
 288 * Write is is not allowed
 289 */
 290static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
 291			    size_t count, loff_t *f_pos)
 292{
 293	return -EPERM;
 294}
 295
 296/**
 297 * zcrypt_open(): Count number of users.
 298 *
 299 * Device open function to count number of users.
 300 */
 301static int zcrypt_open(struct inode *inode, struct file *filp)
 302{
 303	atomic_inc(&zcrypt_open_count);
 304	return nonseekable_open(inode, filp);
 305}
 306
 307/**
 308 * zcrypt_release(): Count number of users.
 309 *
 310 * Device close function to count number of users.
 311 */
 312static int zcrypt_release(struct inode *inode, struct file *filp)
 313{
 314	atomic_dec(&zcrypt_open_count);
 315	return 0;
 316}
 317
 318/*
 319 * zcrypt ioctls.
 320 */
 321static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
 322{
 323	struct zcrypt_device *zdev;
 324	int rc;
 325
 326	if (mex->outputdatalength < mex->inputdatalength)
 327		return -EINVAL;
 328	/*
 329	 * As long as outputdatalength is big enough, we can set the
 330	 * outputdatalength equal to the inputdatalength, since that is the
 331	 * number of bytes we will copy in any case
 332	 */
 333	mex->outputdatalength = mex->inputdatalength;
 334
 335	spin_lock_bh(&zcrypt_device_lock);
 336	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 337		if (!zdev->online ||
 338		    !zdev->ops->rsa_modexpo ||
 339		    zdev->min_mod_size > mex->inputdatalength ||
 340		    zdev->max_mod_size < mex->inputdatalength)
 341			continue;
 342		zcrypt_device_get(zdev);
 343		get_device(&zdev->ap_dev->device);
 344		zdev->request_count++;
 345		__zcrypt_decrease_preference(zdev);
 346		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 347			spin_unlock_bh(&zcrypt_device_lock);
 348			rc = zdev->ops->rsa_modexpo(zdev, mex);
 349			spin_lock_bh(&zcrypt_device_lock);
 350			module_put(zdev->ap_dev->drv->driver.owner);
 351		}
 352		else
 353			rc = -EAGAIN;
 354		zdev->request_count--;
 355		__zcrypt_increase_preference(zdev);
 356		put_device(&zdev->ap_dev->device);
 357		zcrypt_device_put(zdev);
 358		spin_unlock_bh(&zcrypt_device_lock);
 359		return rc;
 360	}
 361	spin_unlock_bh(&zcrypt_device_lock);
 362	return -ENODEV;
 363}
 364
 365static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
 366{
 367	struct zcrypt_device *zdev;
 368	unsigned long long z1, z2, z3;
 369	int rc, copied;
 370
 371	if (crt->outputdatalength < crt->inputdatalength ||
 372	    (crt->inputdatalength & 1))
 373		return -EINVAL;
 374	/*
 375	 * As long as outputdatalength is big enough, we can set the
 376	 * outputdatalength equal to the inputdatalength, since that is the
 377	 * number of bytes we will copy in any case
 378	 */
 379	crt->outputdatalength = crt->inputdatalength;
 380
 381	copied = 0;
 382 restart:
 383	spin_lock_bh(&zcrypt_device_lock);
 384	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 385		if (!zdev->online ||
 386		    !zdev->ops->rsa_modexpo_crt ||
 387		    zdev->min_mod_size > crt->inputdatalength ||
 388		    zdev->max_mod_size < crt->inputdatalength)
 389			continue;
 390		if (zdev->short_crt && crt->inputdatalength > 240) {
 391			/*
 392			 * Check inputdata for leading zeros for cards
 393			 * that can't handle np_prime, bp_key, or
 394			 * u_mult_inv > 128 bytes.
 395			 */
 396			if (copied == 0) {
 397				unsigned int len;
 398				spin_unlock_bh(&zcrypt_device_lock);
 399				/* len is max 256 / 2 - 120 = 8
 400				 * For bigger device just assume len of leading
 401				 * 0s is 8 as stated in the requirements for
 402				 * ica_rsa_modexpo_crt struct in zcrypt.h.
 403				 */
 404				if (crt->inputdatalength <= 256)
 405					len = crt->inputdatalength / 2 - 120;
 406				else
 407					len = 8;
 408				if (len > sizeof(z1))
 409					return -EFAULT;
 410				z1 = z2 = z3 = 0;
 411				if (copy_from_user(&z1, crt->np_prime, len) ||
 412				    copy_from_user(&z2, crt->bp_key, len) ||
 413				    copy_from_user(&z3, crt->u_mult_inv, len))
 414					return -EFAULT;
 415				z1 = z2 = z3 = 0;
 416				copied = 1;
 417				/*
 418				 * We have to restart device lookup -
 419				 * the device list may have changed by now.
 420				 */
 421				goto restart;
 422			}
 423			if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
 424				/* The device can't handle this request. */
 425				continue;
 426		}
 427		zcrypt_device_get(zdev);
 428		get_device(&zdev->ap_dev->device);
 429		zdev->request_count++;
 430		__zcrypt_decrease_preference(zdev);
 431		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 432			spin_unlock_bh(&zcrypt_device_lock);
 433			rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
 434			spin_lock_bh(&zcrypt_device_lock);
 435			module_put(zdev->ap_dev->drv->driver.owner);
 436		}
 437		else
 438			rc = -EAGAIN;
 439		zdev->request_count--;
 440		__zcrypt_increase_preference(zdev);
 441		put_device(&zdev->ap_dev->device);
 442		zcrypt_device_put(zdev);
 443		spin_unlock_bh(&zcrypt_device_lock);
 444		return rc;
 445	}
 446	spin_unlock_bh(&zcrypt_device_lock);
 447	return -ENODEV;
 448}
 449
 450static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
 451{
 452	struct zcrypt_device *zdev;
 453	int rc;
 454
 455	spin_lock_bh(&zcrypt_device_lock);
 456	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 457		if (!zdev->online || !zdev->ops->send_cprb ||
 458		    (xcRB->user_defined != AUTOSELECT &&
 459			AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined)
 460		    )
 461			continue;
 462		zcrypt_device_get(zdev);
 463		get_device(&zdev->ap_dev->device);
 464		zdev->request_count++;
 465		__zcrypt_decrease_preference(zdev);
 466		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 467			spin_unlock_bh(&zcrypt_device_lock);
 468			rc = zdev->ops->send_cprb(zdev, xcRB);
 469			spin_lock_bh(&zcrypt_device_lock);
 470			module_put(zdev->ap_dev->drv->driver.owner);
 471		}
 472		else
 473			rc = -EAGAIN;
 474		zdev->request_count--;
 475		__zcrypt_increase_preference(zdev);
 476		put_device(&zdev->ap_dev->device);
 477		zcrypt_device_put(zdev);
 478		spin_unlock_bh(&zcrypt_device_lock);
 479		return rc;
 480	}
 481	spin_unlock_bh(&zcrypt_device_lock);
 482	return -ENODEV;
 483}
 484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 485static long zcrypt_rng(char *buffer)
 486{
 487	struct zcrypt_device *zdev;
 488	int rc;
 489
 490	spin_lock_bh(&zcrypt_device_lock);
 491	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 492		if (!zdev->online || !zdev->ops->rng)
 493			continue;
 494		zcrypt_device_get(zdev);
 495		get_device(&zdev->ap_dev->device);
 496		zdev->request_count++;
 497		__zcrypt_decrease_preference(zdev);
 498		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 499			spin_unlock_bh(&zcrypt_device_lock);
 500			rc = zdev->ops->rng(zdev, buffer);
 501			spin_lock_bh(&zcrypt_device_lock);
 502			module_put(zdev->ap_dev->drv->driver.owner);
 503		} else
 504			rc = -EAGAIN;
 505		zdev->request_count--;
 506		__zcrypt_increase_preference(zdev);
 507		put_device(&zdev->ap_dev->device);
 508		zcrypt_device_put(zdev);
 509		spin_unlock_bh(&zcrypt_device_lock);
 510		return rc;
 511	}
 512	spin_unlock_bh(&zcrypt_device_lock);
 513	return -ENODEV;
 514}
 515
 516static void zcrypt_status_mask(char status[AP_DEVICES])
 517{
 518	struct zcrypt_device *zdev;
 519
 520	memset(status, 0, sizeof(char) * AP_DEVICES);
 521	spin_lock_bh(&zcrypt_device_lock);
 522	list_for_each_entry(zdev, &zcrypt_device_list, list)
 523		status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
 524			zdev->online ? zdev->user_space_type : 0x0d;
 525	spin_unlock_bh(&zcrypt_device_lock);
 526}
 527
 528static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
 529{
 530	struct zcrypt_device *zdev;
 531
 532	memset(qdepth, 0, sizeof(char)	* AP_DEVICES);
 533	spin_lock_bh(&zcrypt_device_lock);
 534	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 535		spin_lock(&zdev->ap_dev->lock);
 536		qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
 537			zdev->ap_dev->pendingq_count +
 538			zdev->ap_dev->requestq_count;
 539		spin_unlock(&zdev->ap_dev->lock);
 540	}
 541	spin_unlock_bh(&zcrypt_device_lock);
 542}
 543
 544static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
 545{
 546	struct zcrypt_device *zdev;
 547
 548	memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
 549	spin_lock_bh(&zcrypt_device_lock);
 550	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 551		spin_lock(&zdev->ap_dev->lock);
 552		reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
 553			zdev->ap_dev->total_request_count;
 554		spin_unlock(&zdev->ap_dev->lock);
 555	}
 556	spin_unlock_bh(&zcrypt_device_lock);
 557}
 558
 559static int zcrypt_pendingq_count(void)
 560{
 561	struct zcrypt_device *zdev;
 562	int pendingq_count = 0;
 563
 564	spin_lock_bh(&zcrypt_device_lock);
 565	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 566		spin_lock(&zdev->ap_dev->lock);
 567		pendingq_count += zdev->ap_dev->pendingq_count;
 568		spin_unlock(&zdev->ap_dev->lock);
 569	}
 570	spin_unlock_bh(&zcrypt_device_lock);
 571	return pendingq_count;
 572}
 573
 574static int zcrypt_requestq_count(void)
 575{
 576	struct zcrypt_device *zdev;
 577	int requestq_count = 0;
 578
 579	spin_lock_bh(&zcrypt_device_lock);
 580	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 581		spin_lock(&zdev->ap_dev->lock);
 582		requestq_count += zdev->ap_dev->requestq_count;
 583		spin_unlock(&zdev->ap_dev->lock);
 584	}
 585	spin_unlock_bh(&zcrypt_device_lock);
 586	return requestq_count;
 587}
 588
 589static int zcrypt_count_type(int type)
 590{
 591	struct zcrypt_device *zdev;
 592	int device_count = 0;
 593
 594	spin_lock_bh(&zcrypt_device_lock);
 595	list_for_each_entry(zdev, &zcrypt_device_list, list)
 596		if (zdev->user_space_type == type)
 597			device_count++;
 598	spin_unlock_bh(&zcrypt_device_lock);
 599	return device_count;
 600}
 601
 602/**
 603 * zcrypt_ica_status(): Old, depracted combi status call.
 604 *
 605 * Old, deprecated combi status call.
 606 */
 607static long zcrypt_ica_status(struct file *filp, unsigned long arg)
 608{
 609	struct ica_z90_status *pstat;
 610	int ret;
 611
 612	pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
 613	if (!pstat)
 614		return -ENOMEM;
 615	pstat->totalcount = zcrypt_device_count;
 616	pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
 617	pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
 618	pstat->requestqWaitCount = zcrypt_requestq_count();
 619	pstat->pendingqWaitCount = zcrypt_pendingq_count();
 620	pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
 621	pstat->cryptoDomain = ap_domain_index;
 622	zcrypt_status_mask(pstat->status);
 623	zcrypt_qdepth_mask(pstat->qdepth);
 624	ret = 0;
 625	if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
 626		ret = -EFAULT;
 627	kfree(pstat);
 628	return ret;
 629}
 630
 631static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
 632				  unsigned long arg)
 633{
 634	int rc;
 635
 636	switch (cmd) {
 637	case ICARSAMODEXPO: {
 638		struct ica_rsa_modexpo __user *umex = (void __user *) arg;
 639		struct ica_rsa_modexpo mex;
 640		if (copy_from_user(&mex, umex, sizeof(mex)))
 641			return -EFAULT;
 642		do {
 643			rc = zcrypt_rsa_modexpo(&mex);
 644		} while (rc == -EAGAIN);
 
 
 
 
 
 645		if (rc)
 646			return rc;
 647		return put_user(mex.outputdatalength, &umex->outputdatalength);
 648	}
 649	case ICARSACRT: {
 650		struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
 651		struct ica_rsa_modexpo_crt crt;
 652		if (copy_from_user(&crt, ucrt, sizeof(crt)))
 653			return -EFAULT;
 654		do {
 655			rc = zcrypt_rsa_crt(&crt);
 656		} while (rc == -EAGAIN);
 
 
 
 
 
 657		if (rc)
 658			return rc;
 659		return put_user(crt.outputdatalength, &ucrt->outputdatalength);
 660	}
 661	case ZSECSENDCPRB: {
 662		struct ica_xcRB __user *uxcRB = (void __user *) arg;
 663		struct ica_xcRB xcRB;
 664		if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
 665			return -EFAULT;
 666		do {
 667			rc = zcrypt_send_cprb(&xcRB);
 668		} while (rc == -EAGAIN);
 
 
 
 
 
 669		if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
 670			return -EFAULT;
 671		return rc;
 672	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 673	case Z90STAT_STATUS_MASK: {
 674		char status[AP_DEVICES];
 675		zcrypt_status_mask(status);
 676		if (copy_to_user((char __user *) arg, status,
 677				 sizeof(char) * AP_DEVICES))
 678			return -EFAULT;
 679		return 0;
 680	}
 681	case Z90STAT_QDEPTH_MASK: {
 682		char qdepth[AP_DEVICES];
 683		zcrypt_qdepth_mask(qdepth);
 684		if (copy_to_user((char __user *) arg, qdepth,
 685				 sizeof(char) * AP_DEVICES))
 686			return -EFAULT;
 687		return 0;
 688	}
 689	case Z90STAT_PERDEV_REQCNT: {
 690		int reqcnt[AP_DEVICES];
 691		zcrypt_perdev_reqcnt(reqcnt);
 692		if (copy_to_user((int __user *) arg, reqcnt,
 693				 sizeof(int) * AP_DEVICES))
 694			return -EFAULT;
 695		return 0;
 696	}
 697	case Z90STAT_REQUESTQ_COUNT:
 698		return put_user(zcrypt_requestq_count(), (int __user *) arg);
 699	case Z90STAT_PENDINGQ_COUNT:
 700		return put_user(zcrypt_pendingq_count(), (int __user *) arg);
 701	case Z90STAT_TOTALOPEN_COUNT:
 702		return put_user(atomic_read(&zcrypt_open_count),
 703				(int __user *) arg);
 704	case Z90STAT_DOMAIN_INDEX:
 705		return put_user(ap_domain_index, (int __user *) arg);
 706	/*
 707	 * Deprecated ioctls. Don't add another device count ioctl,
 708	 * you can count them yourself in the user space with the
 709	 * output of the Z90STAT_STATUS_MASK ioctl.
 710	 */
 711	case ICAZ90STATUS:
 712		return zcrypt_ica_status(filp, arg);
 713	case Z90STAT_TOTALCOUNT:
 714		return put_user(zcrypt_device_count, (int __user *) arg);
 715	case Z90STAT_PCICACOUNT:
 716		return put_user(zcrypt_count_type(ZCRYPT_PCICA),
 717				(int __user *) arg);
 718	case Z90STAT_PCICCCOUNT:
 719		return put_user(zcrypt_count_type(ZCRYPT_PCICC),
 720				(int __user *) arg);
 721	case Z90STAT_PCIXCCMCL2COUNT:
 722		return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
 723				(int __user *) arg);
 724	case Z90STAT_PCIXCCMCL3COUNT:
 725		return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
 726				(int __user *) arg);
 727	case Z90STAT_PCIXCCCOUNT:
 728		return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
 729				zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
 730				(int __user *) arg);
 731	case Z90STAT_CEX2CCOUNT:
 732		return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
 733				(int __user *) arg);
 734	case Z90STAT_CEX2ACOUNT:
 735		return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
 736				(int __user *) arg);
 737	default:
 738		/* unknown ioctl number */
 739		return -ENOIOCTLCMD;
 740	}
 741}
 742
 743#ifdef CONFIG_COMPAT
 744/*
 745 * ioctl32 conversion routines
 746 */
 747struct compat_ica_rsa_modexpo {
 748	compat_uptr_t	inputdata;
 749	unsigned int	inputdatalength;
 750	compat_uptr_t	outputdata;
 751	unsigned int	outputdatalength;
 752	compat_uptr_t	b_key;
 753	compat_uptr_t	n_modulus;
 754};
 755
 756static long trans_modexpo32(struct file *filp, unsigned int cmd,
 757			    unsigned long arg)
 758{
 759	struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
 760	struct compat_ica_rsa_modexpo mex32;
 761	struct ica_rsa_modexpo mex64;
 762	long rc;
 763
 764	if (copy_from_user(&mex32, umex32, sizeof(mex32)))
 765		return -EFAULT;
 766	mex64.inputdata = compat_ptr(mex32.inputdata);
 767	mex64.inputdatalength = mex32.inputdatalength;
 768	mex64.outputdata = compat_ptr(mex32.outputdata);
 769	mex64.outputdatalength = mex32.outputdatalength;
 770	mex64.b_key = compat_ptr(mex32.b_key);
 771	mex64.n_modulus = compat_ptr(mex32.n_modulus);
 772	do {
 773		rc = zcrypt_rsa_modexpo(&mex64);
 774	} while (rc == -EAGAIN);
 775	if (!rc)
 776		rc = put_user(mex64.outputdatalength,
 777			      &umex32->outputdatalength);
 778	return rc;
 
 
 
 
 
 779}
 780
 781struct compat_ica_rsa_modexpo_crt {
 782	compat_uptr_t	inputdata;
 783	unsigned int	inputdatalength;
 784	compat_uptr_t	outputdata;
 785	unsigned int	outputdatalength;
 786	compat_uptr_t	bp_key;
 787	compat_uptr_t	bq_key;
 788	compat_uptr_t	np_prime;
 789	compat_uptr_t	nq_prime;
 790	compat_uptr_t	u_mult_inv;
 791};
 792
 793static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
 794				unsigned long arg)
 795{
 796	struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
 797	struct compat_ica_rsa_modexpo_crt crt32;
 798	struct ica_rsa_modexpo_crt crt64;
 799	long rc;
 800
 801	if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
 802		return -EFAULT;
 803	crt64.inputdata = compat_ptr(crt32.inputdata);
 804	crt64.inputdatalength = crt32.inputdatalength;
 805	crt64.outputdata=  compat_ptr(crt32.outputdata);
 806	crt64.outputdatalength = crt32.outputdatalength;
 807	crt64.bp_key = compat_ptr(crt32.bp_key);
 808	crt64.bq_key = compat_ptr(crt32.bq_key);
 809	crt64.np_prime = compat_ptr(crt32.np_prime);
 810	crt64.nq_prime = compat_ptr(crt32.nq_prime);
 811	crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
 812	do {
 813		rc = zcrypt_rsa_crt(&crt64);
 814	} while (rc == -EAGAIN);
 815	if (!rc)
 816		rc = put_user(crt64.outputdatalength,
 817			      &ucrt32->outputdatalength);
 818	return rc;
 
 
 
 
 
 819}
 820
 821struct compat_ica_xcRB {
 822	unsigned short	agent_ID;
 823	unsigned int	user_defined;
 824	unsigned short	request_ID;
 825	unsigned int	request_control_blk_length;
 826	unsigned char	padding1[16 - sizeof (compat_uptr_t)];
 827	compat_uptr_t	request_control_blk_addr;
 828	unsigned int	request_data_length;
 829	char		padding2[16 - sizeof (compat_uptr_t)];
 830	compat_uptr_t	request_data_address;
 831	unsigned int	reply_control_blk_length;
 832	char		padding3[16 - sizeof (compat_uptr_t)];
 833	compat_uptr_t	reply_control_blk_addr;
 834	unsigned int	reply_data_length;
 835	char		padding4[16 - sizeof (compat_uptr_t)];
 836	compat_uptr_t	reply_data_addr;
 837	unsigned short	priority_window;
 838	unsigned int	status;
 839} __attribute__((packed));
 840
 841static long trans_xcRB32(struct file *filp, unsigned int cmd,
 842			 unsigned long arg)
 843{
 844	struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
 845	struct compat_ica_xcRB xcRB32;
 846	struct ica_xcRB xcRB64;
 847	long rc;
 848
 849	if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
 850		return -EFAULT;
 851	xcRB64.agent_ID = xcRB32.agent_ID;
 852	xcRB64.user_defined = xcRB32.user_defined;
 853	xcRB64.request_ID = xcRB32.request_ID;
 854	xcRB64.request_control_blk_length =
 855		xcRB32.request_control_blk_length;
 856	xcRB64.request_control_blk_addr =
 857		compat_ptr(xcRB32.request_control_blk_addr);
 858	xcRB64.request_data_length =
 859		xcRB32.request_data_length;
 860	xcRB64.request_data_address =
 861		compat_ptr(xcRB32.request_data_address);
 862	xcRB64.reply_control_blk_length =
 863		xcRB32.reply_control_blk_length;
 864	xcRB64.reply_control_blk_addr =
 865		compat_ptr(xcRB32.reply_control_blk_addr);
 866	xcRB64.reply_data_length = xcRB32.reply_data_length;
 867	xcRB64.reply_data_addr =
 868		compat_ptr(xcRB32.reply_data_addr);
 869	xcRB64.priority_window = xcRB32.priority_window;
 870	xcRB64.status = xcRB32.status;
 871	do {
 872		rc = zcrypt_send_cprb(&xcRB64);
 873	} while (rc == -EAGAIN);
 
 
 
 
 
 874	xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
 875	xcRB32.reply_data_length = xcRB64.reply_data_length;
 876	xcRB32.status = xcRB64.status;
 877	if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
 878			return -EFAULT;
 879	return rc;
 880}
 881
 882static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
 883			 unsigned long arg)
 884{
 885	if (cmd == ICARSAMODEXPO)
 886		return trans_modexpo32(filp, cmd, arg);
 887	if (cmd == ICARSACRT)
 888		return trans_modexpo_crt32(filp, cmd, arg);
 889	if (cmd == ZSECSENDCPRB)
 890		return trans_xcRB32(filp, cmd, arg);
 891	return zcrypt_unlocked_ioctl(filp, cmd, arg);
 892}
 893#endif
 894
 895/*
 896 * Misc device file operations.
 897 */
 898static const struct file_operations zcrypt_fops = {
 899	.owner		= THIS_MODULE,
 900	.read		= zcrypt_read,
 901	.write		= zcrypt_write,
 902	.unlocked_ioctl	= zcrypt_unlocked_ioctl,
 903#ifdef CONFIG_COMPAT
 904	.compat_ioctl	= zcrypt_compat_ioctl,
 905#endif
 906	.open		= zcrypt_open,
 907	.release	= zcrypt_release,
 908	.llseek		= no_llseek,
 909};
 910
 911/*
 912 * Misc device.
 913 */
 914static struct miscdevice zcrypt_misc_device = {
 915	.minor	    = MISC_DYNAMIC_MINOR,
 916	.name	    = "z90crypt",
 917	.fops	    = &zcrypt_fops,
 918};
 919
 920/*
 921 * Deprecated /proc entry support.
 922 */
 923static struct proc_dir_entry *zcrypt_entry;
 924
 925static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
 926{
 927	int i;
 928
 929	for (i = 0; i < len; i++)
 930		seq_printf(m, "%01x", (unsigned int) addr[i]);
 931	seq_putc(m, ' ');
 932}
 933
 934static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
 935{
 936	int inl, c, cx;
 937
 938	seq_printf(m, "	   ");
 939	inl = 0;
 940	for (c = 0; c < (len / 16); c++) {
 941		sprintcl(m, addr+inl, 16);
 942		inl += 16;
 943	}
 944	cx = len%16;
 945	if (cx) {
 946		sprintcl(m, addr+inl, cx);
 947		inl += cx;
 948	}
 949	seq_putc(m, '\n');
 950}
 951
 952static void sprinthx(unsigned char *title, struct seq_file *m,
 953		     unsigned char *addr, unsigned int len)
 954{
 955	int inl, r, rx;
 956
 957	seq_printf(m, "\n%s\n", title);
 958	inl = 0;
 959	for (r = 0; r < (len / 64); r++) {
 960		sprintrw(m, addr+inl, 64);
 961		inl += 64;
 962	}
 963	rx = len % 64;
 964	if (rx) {
 965		sprintrw(m, addr+inl, rx);
 966		inl += rx;
 967	}
 968	seq_putc(m, '\n');
 969}
 970
 971static void sprinthx4(unsigned char *title, struct seq_file *m,
 972		      unsigned int *array, unsigned int len)
 973{
 974	int r;
 975
 976	seq_printf(m, "\n%s\n", title);
 977	for (r = 0; r < len; r++) {
 978		if ((r % 8) == 0)
 979			seq_printf(m, "    ");
 980		seq_printf(m, "%08X ", array[r]);
 981		if ((r % 8) == 7)
 982			seq_putc(m, '\n');
 983	}
 984	seq_putc(m, '\n');
 985}
 986
 987static int zcrypt_proc_show(struct seq_file *m, void *v)
 988{
 989	char workarea[sizeof(int) * AP_DEVICES];
 990
 991	seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
 992		   ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
 993	seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
 994	seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
 995	seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
 996	seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
 997	seq_printf(m, "PCIXCC MCL2 count: %d\n",
 998		   zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
 999	seq_printf(m, "PCIXCC MCL3 count: %d\n",
1000		   zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1001	seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
1002	seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
1003	seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
1004	seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
1005	seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
1006	seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
1007	seq_printf(m, "Total open handles: %d\n\n",
1008		   atomic_read(&zcrypt_open_count));
1009	zcrypt_status_mask(workarea);
1010	sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1011		 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1012		 m, workarea, AP_DEVICES);
1013	zcrypt_qdepth_mask(workarea);
1014	sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
1015	zcrypt_perdev_reqcnt((int *) workarea);
1016	sprinthx4("Per-device successfully completed request counts",
1017		  m, (unsigned int *) workarea, AP_DEVICES);
1018	return 0;
1019}
1020
1021static int zcrypt_proc_open(struct inode *inode, struct file *file)
1022{
1023	return single_open(file, zcrypt_proc_show, NULL);
1024}
1025
1026static void zcrypt_disable_card(int index)
1027{
1028	struct zcrypt_device *zdev;
1029
1030	spin_lock_bh(&zcrypt_device_lock);
1031	list_for_each_entry(zdev, &zcrypt_device_list, list)
1032		if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1033			zdev->online = 0;
1034			ap_flush_queue(zdev->ap_dev);
1035			break;
1036		}
1037	spin_unlock_bh(&zcrypt_device_lock);
1038}
1039
1040static void zcrypt_enable_card(int index)
1041{
1042	struct zcrypt_device *zdev;
1043
1044	spin_lock_bh(&zcrypt_device_lock);
1045	list_for_each_entry(zdev, &zcrypt_device_list, list)
1046		if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1047			zdev->online = 1;
1048			break;
1049		}
1050	spin_unlock_bh(&zcrypt_device_lock);
1051}
1052
1053static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
1054				 size_t count, loff_t *pos)
1055{
1056	unsigned char *lbuf, *ptr;
1057	size_t local_count;
1058	int j;
1059
1060	if (count <= 0)
1061		return 0;
1062
1063#define LBUFSIZE 1200UL
1064	lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1065	if (!lbuf)
1066		return 0;
1067
1068	local_count = min(LBUFSIZE - 1, count);
1069	if (copy_from_user(lbuf, buffer, local_count) != 0) {
1070		kfree(lbuf);
1071		return -EFAULT;
1072	}
1073	lbuf[local_count] = '\0';
1074
1075	ptr = strstr(lbuf, "Online devices");
1076	if (!ptr)
1077		goto out;
1078	ptr = strstr(ptr, "\n");
1079	if (!ptr)
1080		goto out;
1081	ptr++;
1082
1083	if (strstr(ptr, "Waiting work element counts") == NULL)
1084		goto out;
1085
1086	for (j = 0; j < 64 && *ptr; ptr++) {
1087		/*
1088		 * '0' for no device, '1' for PCICA, '2' for PCICC,
1089		 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1090		 * '5' for CEX2C and '6' for CEX2A'
1091		 * '7' for CEX3C and '8' for CEX3A
1092		 */
1093		if (*ptr >= '0' && *ptr <= '8')
1094			j++;
1095		else if (*ptr == 'd' || *ptr == 'D')
1096			zcrypt_disable_card(j++);
1097		else if (*ptr == 'e' || *ptr == 'E')
1098			zcrypt_enable_card(j++);
1099		else if (*ptr != ' ' && *ptr != '\t')
1100			break;
1101	}
1102out:
1103	kfree(lbuf);
1104	return count;
1105}
1106
1107static const struct file_operations zcrypt_proc_fops = {
1108	.owner		= THIS_MODULE,
1109	.open		= zcrypt_proc_open,
1110	.read		= seq_read,
1111	.llseek		= seq_lseek,
1112	.release	= single_release,
1113	.write		= zcrypt_proc_write,
1114};
1115
1116static int zcrypt_rng_device_count;
1117static u32 *zcrypt_rng_buffer;
1118static int zcrypt_rng_buffer_index;
1119static DEFINE_MUTEX(zcrypt_rng_mutex);
1120
1121static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1122{
1123	int rc;
1124
1125	/*
1126	 * We don't need locking here because the RNG API guarantees serialized
1127	 * read method calls.
1128	 */
1129	if (zcrypt_rng_buffer_index == 0) {
1130		rc = zcrypt_rng((char *) zcrypt_rng_buffer);
 
 
 
1131		if (rc < 0)
1132			return -EIO;
1133		zcrypt_rng_buffer_index = rc / sizeof *data;
1134	}
1135	*data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1136	return sizeof *data;
1137}
1138
1139static struct hwrng zcrypt_rng_dev = {
1140	.name		= "zcrypt",
1141	.data_read	= zcrypt_rng_data_read,
1142};
1143
1144static int zcrypt_rng_device_add(void)
1145{
1146	int rc = 0;
1147
1148	mutex_lock(&zcrypt_rng_mutex);
1149	if (zcrypt_rng_device_count == 0) {
1150		zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1151		if (!zcrypt_rng_buffer) {
1152			rc = -ENOMEM;
1153			goto out;
1154		}
1155		zcrypt_rng_buffer_index = 0;
1156		rc = hwrng_register(&zcrypt_rng_dev);
1157		if (rc)
1158			goto out_free;
1159		zcrypt_rng_device_count = 1;
1160	} else
1161		zcrypt_rng_device_count++;
1162	mutex_unlock(&zcrypt_rng_mutex);
1163	return 0;
1164
1165out_free:
1166	free_page((unsigned long) zcrypt_rng_buffer);
1167out:
1168	mutex_unlock(&zcrypt_rng_mutex);
1169	return rc;
1170}
1171
1172static void zcrypt_rng_device_remove(void)
1173{
1174	mutex_lock(&zcrypt_rng_mutex);
1175	zcrypt_rng_device_count--;
1176	if (zcrypt_rng_device_count == 0) {
1177		hwrng_unregister(&zcrypt_rng_dev);
1178		free_page((unsigned long) zcrypt_rng_buffer);
1179	}
1180	mutex_unlock(&zcrypt_rng_mutex);
1181}
1182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1183/**
1184 * zcrypt_api_init(): Module initialization.
1185 *
1186 * The module initialization code.
1187 */
1188int __init zcrypt_api_init(void)
1189{
1190	int rc;
1191
 
 
 
 
 
 
1192	/* Register the request sprayer. */
1193	rc = misc_register(&zcrypt_misc_device);
1194	if (rc < 0)
1195		goto out;
1196
1197	/* Set up the proc file system */
1198	zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
1199	if (!zcrypt_entry) {
1200		rc = -ENOMEM;
1201		goto out_misc;
1202	}
1203
1204	return 0;
1205
1206out_misc:
1207	misc_deregister(&zcrypt_misc_device);
1208out:
1209	return rc;
1210}
1211
1212/**
1213 * zcrypt_api_exit(): Module termination.
1214 *
1215 * The module termination code.
1216 */
1217void zcrypt_api_exit(void)
1218{
1219	remove_proc_entry("driver/z90crypt", NULL);
1220	misc_deregister(&zcrypt_misc_device);
 
1221}
1222
1223#ifndef CONFIG_ZCRYPT_MONOLITHIC
1224module_init(zcrypt_api_init);
1225module_exit(zcrypt_api_exit);
1226#endif
v3.15
   1/*
 
 
   2 *  zcrypt 2.1.0
   3 *
   4 *  Copyright IBM Corp. 2001, 2012
   5 *  Author(s): Robert Burroughs
   6 *	       Eric Rossman (edrossma@us.ibm.com)
   7 *	       Cornelia Huck <cornelia.huck@de.ibm.com>
   8 *
   9 *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  10 *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  11 *				  Ralph Wuerthner <rwuerthn@de.ibm.com>
  12 *  MSGTYPE restruct:		  Holger Dengler <hd@linux.vnet.ibm.com>
  13 *
  14 * This program is free software; you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License as published by
  16 * the Free Software Foundation; either version 2, or (at your option)
  17 * any later version.
  18 *
  19 * This program is distributed in the hope that it will be useful,
  20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22 * GNU General Public License for more details.
  23 *
  24 * You should have received a copy of the GNU General Public License
  25 * along with this program; if not, write to the Free Software
  26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27 */
  28
  29#include <linux/module.h>
  30#include <linux/init.h>
  31#include <linux/interrupt.h>
  32#include <linux/miscdevice.h>
  33#include <linux/fs.h>
  34#include <linux/proc_fs.h>
  35#include <linux/seq_file.h>
  36#include <linux/compat.h>
  37#include <linux/slab.h>
  38#include <linux/atomic.h>
  39#include <asm/uaccess.h>
  40#include <linux/hw_random.h>
  41#include <linux/debugfs.h>
  42#include <asm/debug.h>
  43
  44#include "zcrypt_debug.h"
  45#include "zcrypt_api.h"
  46
  47#include "zcrypt_msgtype6.h"
  48
  49/*
  50 * Module description.
  51 */
  52MODULE_AUTHOR("IBM Corporation");
  53MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
  54		   "Copyright IBM Corp. 2001, 2012");
  55MODULE_LICENSE("GPL");
  56
  57static DEFINE_SPINLOCK(zcrypt_device_lock);
  58static LIST_HEAD(zcrypt_device_list);
  59static int zcrypt_device_count = 0;
  60static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
  61static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
  62
  63atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
  64EXPORT_SYMBOL(zcrypt_rescan_req);
  65
  66static int zcrypt_rng_device_add(void);
  67static void zcrypt_rng_device_remove(void);
  68
  69static DEFINE_SPINLOCK(zcrypt_ops_list_lock);
  70static LIST_HEAD(zcrypt_ops_list);
  71
  72static debug_info_t *zcrypt_dbf_common;
  73static debug_info_t *zcrypt_dbf_devices;
  74static struct dentry *debugfs_root;
  75
  76/*
  77 * Device attributes common for all crypto devices.
  78 */
  79static ssize_t zcrypt_type_show(struct device *dev,
  80				struct device_attribute *attr, char *buf)
  81{
  82	struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  83	return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
  84}
  85
  86static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
  87
  88static ssize_t zcrypt_online_show(struct device *dev,
  89				  struct device_attribute *attr, char *buf)
  90{
  91	struct zcrypt_device *zdev = to_ap_dev(dev)->private;
  92	return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
  93}
  94
  95static ssize_t zcrypt_online_store(struct device *dev,
  96				   struct device_attribute *attr,
  97				   const char *buf, size_t count)
  98{
  99	struct zcrypt_device *zdev = to_ap_dev(dev)->private;
 100	int online;
 101
 102	if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
 103		return -EINVAL;
 104	zdev->online = online;
 105	ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dman", zdev->ap_dev->qid,
 106		       zdev->online);
 107	if (!online)
 108		ap_flush_queue(zdev->ap_dev);
 109	return count;
 110}
 111
 112static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
 113
 114static struct attribute * zcrypt_device_attrs[] = {
 115	&dev_attr_type.attr,
 116	&dev_attr_online.attr,
 117	NULL,
 118};
 119
 120static struct attribute_group zcrypt_device_attr_group = {
 121	.attrs = zcrypt_device_attrs,
 122};
 123
 124/**
 125 * Process a rescan of the transport layer.
 126 *
 127 * Returns 1, if the rescan has been processed, otherwise 0.
 128 */
 129static inline int zcrypt_process_rescan(void)
 130{
 131	if (atomic_read(&zcrypt_rescan_req)) {
 132		atomic_set(&zcrypt_rescan_req, 0);
 133		atomic_inc(&zcrypt_rescan_count);
 134		ap_bus_force_rescan();
 135		ZCRYPT_DBF_COMMON(DBF_INFO, "rescan%07d",
 136				  atomic_inc_return(&zcrypt_rescan_count));
 137		return 1;
 138	}
 139	return 0;
 140}
 141
 142/**
 143 * __zcrypt_increase_preference(): Increase preference of a crypto device.
 144 * @zdev: Pointer the crypto device
 145 *
 146 * Move the device towards the head of the device list.
 147 * Need to be called while holding the zcrypt device list lock.
 148 * Note: cards with speed_rating of 0 are kept at the end of the list.
 149 */
 150static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
 151{
 152	struct zcrypt_device *tmp;
 153	struct list_head *l;
 154
 155	if (zdev->speed_rating == 0)
 156		return;
 157	for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
 158		tmp = list_entry(l, struct zcrypt_device, list);
 159		if ((tmp->request_count + 1) * tmp->speed_rating <=
 160		    (zdev->request_count + 1) * zdev->speed_rating &&
 161		    tmp->speed_rating != 0)
 162			break;
 163	}
 164	if (l == zdev->list.prev)
 165		return;
 166	/* Move zdev behind l */
 167	list_move(&zdev->list, l);
 168}
 169
 170/**
 171 * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
 172 * @zdev: Pointer to a crypto device.
 173 *
 174 * Move the device towards the tail of the device list.
 175 * Need to be called while holding the zcrypt device list lock.
 176 * Note: cards with speed_rating of 0 are kept at the end of the list.
 177 */
 178static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
 179{
 180	struct zcrypt_device *tmp;
 181	struct list_head *l;
 182
 183	if (zdev->speed_rating == 0)
 184		return;
 185	for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
 186		tmp = list_entry(l, struct zcrypt_device, list);
 187		if ((tmp->request_count + 1) * tmp->speed_rating >
 188		    (zdev->request_count + 1) * zdev->speed_rating ||
 189		    tmp->speed_rating == 0)
 190			break;
 191	}
 192	if (l == zdev->list.next)
 193		return;
 194	/* Move zdev before l */
 195	list_move_tail(&zdev->list, l);
 196}
 197
 198static void zcrypt_device_release(struct kref *kref)
 199{
 200	struct zcrypt_device *zdev =
 201		container_of(kref, struct zcrypt_device, refcount);
 202	zcrypt_device_free(zdev);
 203}
 204
 205void zcrypt_device_get(struct zcrypt_device *zdev)
 206{
 207	kref_get(&zdev->refcount);
 208}
 209EXPORT_SYMBOL(zcrypt_device_get);
 210
 211int zcrypt_device_put(struct zcrypt_device *zdev)
 212{
 213	return kref_put(&zdev->refcount, zcrypt_device_release);
 214}
 215EXPORT_SYMBOL(zcrypt_device_put);
 216
 217struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
 218{
 219	struct zcrypt_device *zdev;
 220
 221	zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
 222	if (!zdev)
 223		return NULL;
 224	zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
 225	if (!zdev->reply.message)
 226		goto out_free;
 227	zdev->reply.length = max_response_size;
 228	spin_lock_init(&zdev->lock);
 229	INIT_LIST_HEAD(&zdev->list);
 230	zdev->dbf_area = zcrypt_dbf_devices;
 231	return zdev;
 232
 233out_free:
 234	kfree(zdev);
 235	return NULL;
 236}
 237EXPORT_SYMBOL(zcrypt_device_alloc);
 238
 239void zcrypt_device_free(struct zcrypt_device *zdev)
 240{
 241	kfree(zdev->reply.message);
 242	kfree(zdev);
 243}
 244EXPORT_SYMBOL(zcrypt_device_free);
 245
 246/**
 247 * zcrypt_device_register() - Register a crypto device.
 248 * @zdev: Pointer to a crypto device
 249 *
 250 * Register a crypto device. Returns 0 if successful.
 251 */
 252int zcrypt_device_register(struct zcrypt_device *zdev)
 253{
 254	int rc;
 255
 256	if (!zdev->ops)
 257		return -ENODEV;
 258	rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
 259				&zcrypt_device_attr_group);
 260	if (rc)
 261		goto out;
 262	get_device(&zdev->ap_dev->device);
 263	kref_init(&zdev->refcount);
 264	spin_lock_bh(&zcrypt_device_lock);
 265	zdev->online = 1;	/* New devices are online by default. */
 266	ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dreg", zdev->ap_dev->qid,
 267		       zdev->online);
 268	list_add_tail(&zdev->list, &zcrypt_device_list);
 269	__zcrypt_increase_preference(zdev);
 270	zcrypt_device_count++;
 271	spin_unlock_bh(&zcrypt_device_lock);
 272	if (zdev->ops->rng) {
 273		rc = zcrypt_rng_device_add();
 274		if (rc)
 275			goto out_unregister;
 276	}
 277	return 0;
 278
 279out_unregister:
 280	spin_lock_bh(&zcrypt_device_lock);
 281	zcrypt_device_count--;
 282	list_del_init(&zdev->list);
 283	spin_unlock_bh(&zcrypt_device_lock);
 284	sysfs_remove_group(&zdev->ap_dev->device.kobj,
 285			   &zcrypt_device_attr_group);
 286	put_device(&zdev->ap_dev->device);
 287	zcrypt_device_put(zdev);
 288out:
 289	return rc;
 290}
 291EXPORT_SYMBOL(zcrypt_device_register);
 292
 293/**
 294 * zcrypt_device_unregister(): Unregister a crypto device.
 295 * @zdev: Pointer to crypto device
 296 *
 297 * Unregister a crypto device.
 298 */
 299void zcrypt_device_unregister(struct zcrypt_device *zdev)
 300{
 301	if (zdev->ops->rng)
 302		zcrypt_rng_device_remove();
 303	spin_lock_bh(&zcrypt_device_lock);
 304	zcrypt_device_count--;
 305	list_del_init(&zdev->list);
 306	spin_unlock_bh(&zcrypt_device_lock);
 307	sysfs_remove_group(&zdev->ap_dev->device.kobj,
 308			   &zcrypt_device_attr_group);
 309	put_device(&zdev->ap_dev->device);
 310	zcrypt_device_put(zdev);
 311}
 312EXPORT_SYMBOL(zcrypt_device_unregister);
 313
 314void zcrypt_msgtype_register(struct zcrypt_ops *zops)
 315{
 316	if (zops->owner) {
 317		spin_lock_bh(&zcrypt_ops_list_lock);
 318		list_add_tail(&zops->list, &zcrypt_ops_list);
 319		spin_unlock_bh(&zcrypt_ops_list_lock);
 320	}
 321}
 322EXPORT_SYMBOL(zcrypt_msgtype_register);
 323
 324void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
 325{
 326	spin_lock_bh(&zcrypt_ops_list_lock);
 327	list_del_init(&zops->list);
 328	spin_unlock_bh(&zcrypt_ops_list_lock);
 329}
 330EXPORT_SYMBOL(zcrypt_msgtype_unregister);
 331
 332static inline
 333struct zcrypt_ops *__ops_lookup(unsigned char *name, int variant)
 334{
 335	struct zcrypt_ops *zops;
 336	int found = 0;
 337
 338	spin_lock_bh(&zcrypt_ops_list_lock);
 339	list_for_each_entry(zops, &zcrypt_ops_list, list) {
 340		if ((zops->variant == variant) &&
 341		    (!strncmp(zops->owner->name, name, MODULE_NAME_LEN))) {
 342			found = 1;
 343			break;
 344		}
 345	}
 346	spin_unlock_bh(&zcrypt_ops_list_lock);
 347
 348	if (!found)
 349		return NULL;
 350	return zops;
 351}
 352
 353struct zcrypt_ops *zcrypt_msgtype_request(unsigned char *name, int variant)
 354{
 355	struct zcrypt_ops *zops = NULL;
 356
 357	zops = __ops_lookup(name, variant);
 358	if (!zops) {
 359		request_module(name);
 360		zops = __ops_lookup(name, variant);
 361	}
 362	if ((!zops) || (!try_module_get(zops->owner)))
 363		return NULL;
 364	return zops;
 365}
 366EXPORT_SYMBOL(zcrypt_msgtype_request);
 367
 368void zcrypt_msgtype_release(struct zcrypt_ops *zops)
 369{
 370	if (zops)
 371		module_put(zops->owner);
 372}
 373EXPORT_SYMBOL(zcrypt_msgtype_release);
 374
 375/**
 376 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
 377 *
 378 * This function is not supported beyond zcrypt 1.3.1.
 379 */
 380static ssize_t zcrypt_read(struct file *filp, char __user *buf,
 381			   size_t count, loff_t *f_pos)
 382{
 383	return -EPERM;
 384}
 385
 386/**
 387 * zcrypt_write(): Not allowed.
 388 *
 389 * Write is is not allowed
 390 */
 391static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
 392			    size_t count, loff_t *f_pos)
 393{
 394	return -EPERM;
 395}
 396
 397/**
 398 * zcrypt_open(): Count number of users.
 399 *
 400 * Device open function to count number of users.
 401 */
 402static int zcrypt_open(struct inode *inode, struct file *filp)
 403{
 404	atomic_inc(&zcrypt_open_count);
 405	return nonseekable_open(inode, filp);
 406}
 407
 408/**
 409 * zcrypt_release(): Count number of users.
 410 *
 411 * Device close function to count number of users.
 412 */
 413static int zcrypt_release(struct inode *inode, struct file *filp)
 414{
 415	atomic_dec(&zcrypt_open_count);
 416	return 0;
 417}
 418
 419/*
 420 * zcrypt ioctls.
 421 */
 422static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
 423{
 424	struct zcrypt_device *zdev;
 425	int rc;
 426
 427	if (mex->outputdatalength < mex->inputdatalength)
 428		return -EINVAL;
 429	/*
 430	 * As long as outputdatalength is big enough, we can set the
 431	 * outputdatalength equal to the inputdatalength, since that is the
 432	 * number of bytes we will copy in any case
 433	 */
 434	mex->outputdatalength = mex->inputdatalength;
 435
 436	spin_lock_bh(&zcrypt_device_lock);
 437	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 438		if (!zdev->online ||
 439		    !zdev->ops->rsa_modexpo ||
 440		    zdev->min_mod_size > mex->inputdatalength ||
 441		    zdev->max_mod_size < mex->inputdatalength)
 442			continue;
 443		zcrypt_device_get(zdev);
 444		get_device(&zdev->ap_dev->device);
 445		zdev->request_count++;
 446		__zcrypt_decrease_preference(zdev);
 447		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 448			spin_unlock_bh(&zcrypt_device_lock);
 449			rc = zdev->ops->rsa_modexpo(zdev, mex);
 450			spin_lock_bh(&zcrypt_device_lock);
 451			module_put(zdev->ap_dev->drv->driver.owner);
 452		}
 453		else
 454			rc = -EAGAIN;
 455		zdev->request_count--;
 456		__zcrypt_increase_preference(zdev);
 457		put_device(&zdev->ap_dev->device);
 458		zcrypt_device_put(zdev);
 459		spin_unlock_bh(&zcrypt_device_lock);
 460		return rc;
 461	}
 462	spin_unlock_bh(&zcrypt_device_lock);
 463	return -ENODEV;
 464}
 465
 466static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
 467{
 468	struct zcrypt_device *zdev;
 469	unsigned long long z1, z2, z3;
 470	int rc, copied;
 471
 472	if (crt->outputdatalength < crt->inputdatalength ||
 473	    (crt->inputdatalength & 1))
 474		return -EINVAL;
 475	/*
 476	 * As long as outputdatalength is big enough, we can set the
 477	 * outputdatalength equal to the inputdatalength, since that is the
 478	 * number of bytes we will copy in any case
 479	 */
 480	crt->outputdatalength = crt->inputdatalength;
 481
 482	copied = 0;
 483 restart:
 484	spin_lock_bh(&zcrypt_device_lock);
 485	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 486		if (!zdev->online ||
 487		    !zdev->ops->rsa_modexpo_crt ||
 488		    zdev->min_mod_size > crt->inputdatalength ||
 489		    zdev->max_mod_size < crt->inputdatalength)
 490			continue;
 491		if (zdev->short_crt && crt->inputdatalength > 240) {
 492			/*
 493			 * Check inputdata for leading zeros for cards
 494			 * that can't handle np_prime, bp_key, or
 495			 * u_mult_inv > 128 bytes.
 496			 */
 497			if (copied == 0) {
 498				unsigned int len;
 499				spin_unlock_bh(&zcrypt_device_lock);
 500				/* len is max 256 / 2 - 120 = 8
 501				 * For bigger device just assume len of leading
 502				 * 0s is 8 as stated in the requirements for
 503				 * ica_rsa_modexpo_crt struct in zcrypt.h.
 504				 */
 505				if (crt->inputdatalength <= 256)
 506					len = crt->inputdatalength / 2 - 120;
 507				else
 508					len = 8;
 509				if (len > sizeof(z1))
 510					return -EFAULT;
 511				z1 = z2 = z3 = 0;
 512				if (copy_from_user(&z1, crt->np_prime, len) ||
 513				    copy_from_user(&z2, crt->bp_key, len) ||
 514				    copy_from_user(&z3, crt->u_mult_inv, len))
 515					return -EFAULT;
 516				z1 = z2 = z3 = 0;
 517				copied = 1;
 518				/*
 519				 * We have to restart device lookup -
 520				 * the device list may have changed by now.
 521				 */
 522				goto restart;
 523			}
 524			if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
 525				/* The device can't handle this request. */
 526				continue;
 527		}
 528		zcrypt_device_get(zdev);
 529		get_device(&zdev->ap_dev->device);
 530		zdev->request_count++;
 531		__zcrypt_decrease_preference(zdev);
 532		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 533			spin_unlock_bh(&zcrypt_device_lock);
 534			rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
 535			spin_lock_bh(&zcrypt_device_lock);
 536			module_put(zdev->ap_dev->drv->driver.owner);
 537		}
 538		else
 539			rc = -EAGAIN;
 540		zdev->request_count--;
 541		__zcrypt_increase_preference(zdev);
 542		put_device(&zdev->ap_dev->device);
 543		zcrypt_device_put(zdev);
 544		spin_unlock_bh(&zcrypt_device_lock);
 545		return rc;
 546	}
 547	spin_unlock_bh(&zcrypt_device_lock);
 548	return -ENODEV;
 549}
 550
 551static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
 552{
 553	struct zcrypt_device *zdev;
 554	int rc;
 555
 556	spin_lock_bh(&zcrypt_device_lock);
 557	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 558		if (!zdev->online || !zdev->ops->send_cprb ||
 559		   (zdev->ops->variant == MSGTYPE06_VARIANT_EP11) ||
 560		   (xcRB->user_defined != AUTOSELECT &&
 561		    AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined))
 562			continue;
 563		zcrypt_device_get(zdev);
 564		get_device(&zdev->ap_dev->device);
 565		zdev->request_count++;
 566		__zcrypt_decrease_preference(zdev);
 567		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 568			spin_unlock_bh(&zcrypt_device_lock);
 569			rc = zdev->ops->send_cprb(zdev, xcRB);
 570			spin_lock_bh(&zcrypt_device_lock);
 571			module_put(zdev->ap_dev->drv->driver.owner);
 572		}
 573		else
 574			rc = -EAGAIN;
 575		zdev->request_count--;
 576		__zcrypt_increase_preference(zdev);
 577		put_device(&zdev->ap_dev->device);
 578		zcrypt_device_put(zdev);
 579		spin_unlock_bh(&zcrypt_device_lock);
 580		return rc;
 581	}
 582	spin_unlock_bh(&zcrypt_device_lock);
 583	return -ENODEV;
 584}
 585
 586struct ep11_target_dev_list {
 587	unsigned short		targets_num;
 588	struct ep11_target_dev	*targets;
 589};
 590
 591static bool is_desired_ep11dev(unsigned int dev_qid,
 592			       struct ep11_target_dev_list dev_list)
 593{
 594	int n;
 595
 596	for (n = 0; n < dev_list.targets_num; n++, dev_list.targets++) {
 597		if ((AP_QID_DEVICE(dev_qid) == dev_list.targets->ap_id) &&
 598		    (AP_QID_QUEUE(dev_qid) == dev_list.targets->dom_id)) {
 599			return true;
 600		}
 601	}
 602	return false;
 603}
 604
 605static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
 606{
 607	struct zcrypt_device *zdev;
 608	bool autoselect = false;
 609	int rc;
 610	struct ep11_target_dev_list ep11_dev_list = {
 611		.targets_num	=  0x00,
 612		.targets	=  NULL,
 613	};
 614
 615	ep11_dev_list.targets_num = (unsigned short) xcrb->targets_num;
 616
 617	/* empty list indicates autoselect (all available targets) */
 618	if (ep11_dev_list.targets_num == 0)
 619		autoselect = true;
 620	else {
 621		ep11_dev_list.targets = kcalloc((unsigned short)
 622						xcrb->targets_num,
 623						sizeof(struct ep11_target_dev),
 624						GFP_KERNEL);
 625		if (!ep11_dev_list.targets)
 626			return -ENOMEM;
 627
 628		if (copy_from_user(ep11_dev_list.targets,
 629				   (struct ep11_target_dev __force __user *)
 630				   xcrb->targets, xcrb->targets_num *
 631				   sizeof(struct ep11_target_dev)))
 632			return -EFAULT;
 633	}
 634
 635	spin_lock_bh(&zcrypt_device_lock);
 636	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 637		/* check if device is eligible */
 638		if (!zdev->online ||
 639		    zdev->ops->variant != MSGTYPE06_VARIANT_EP11)
 640			continue;
 641
 642		/* check if device is selected as valid target */
 643		if (!is_desired_ep11dev(zdev->ap_dev->qid, ep11_dev_list) &&
 644		    !autoselect)
 645			continue;
 646
 647		zcrypt_device_get(zdev);
 648		get_device(&zdev->ap_dev->device);
 649		zdev->request_count++;
 650		__zcrypt_decrease_preference(zdev);
 651		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 652			spin_unlock_bh(&zcrypt_device_lock);
 653			rc = zdev->ops->send_ep11_cprb(zdev, xcrb);
 654			spin_lock_bh(&zcrypt_device_lock);
 655			module_put(zdev->ap_dev->drv->driver.owner);
 656		} else {
 657			rc = -EAGAIN;
 658		  }
 659		zdev->request_count--;
 660		__zcrypt_increase_preference(zdev);
 661		put_device(&zdev->ap_dev->device);
 662		zcrypt_device_put(zdev);
 663		spin_unlock_bh(&zcrypt_device_lock);
 664		return rc;
 665	}
 666	spin_unlock_bh(&zcrypt_device_lock);
 667	return -ENODEV;
 668}
 669
 670static long zcrypt_rng(char *buffer)
 671{
 672	struct zcrypt_device *zdev;
 673	int rc;
 674
 675	spin_lock_bh(&zcrypt_device_lock);
 676	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 677		if (!zdev->online || !zdev->ops->rng)
 678			continue;
 679		zcrypt_device_get(zdev);
 680		get_device(&zdev->ap_dev->device);
 681		zdev->request_count++;
 682		__zcrypt_decrease_preference(zdev);
 683		if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
 684			spin_unlock_bh(&zcrypt_device_lock);
 685			rc = zdev->ops->rng(zdev, buffer);
 686			spin_lock_bh(&zcrypt_device_lock);
 687			module_put(zdev->ap_dev->drv->driver.owner);
 688		} else
 689			rc = -EAGAIN;
 690		zdev->request_count--;
 691		__zcrypt_increase_preference(zdev);
 692		put_device(&zdev->ap_dev->device);
 693		zcrypt_device_put(zdev);
 694		spin_unlock_bh(&zcrypt_device_lock);
 695		return rc;
 696	}
 697	spin_unlock_bh(&zcrypt_device_lock);
 698	return -ENODEV;
 699}
 700
 701static void zcrypt_status_mask(char status[AP_DEVICES])
 702{
 703	struct zcrypt_device *zdev;
 704
 705	memset(status, 0, sizeof(char) * AP_DEVICES);
 706	spin_lock_bh(&zcrypt_device_lock);
 707	list_for_each_entry(zdev, &zcrypt_device_list, list)
 708		status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
 709			zdev->online ? zdev->user_space_type : 0x0d;
 710	spin_unlock_bh(&zcrypt_device_lock);
 711}
 712
 713static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
 714{
 715	struct zcrypt_device *zdev;
 716
 717	memset(qdepth, 0, sizeof(char)	* AP_DEVICES);
 718	spin_lock_bh(&zcrypt_device_lock);
 719	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 720		spin_lock(&zdev->ap_dev->lock);
 721		qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
 722			zdev->ap_dev->pendingq_count +
 723			zdev->ap_dev->requestq_count;
 724		spin_unlock(&zdev->ap_dev->lock);
 725	}
 726	spin_unlock_bh(&zcrypt_device_lock);
 727}
 728
 729static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
 730{
 731	struct zcrypt_device *zdev;
 732
 733	memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
 734	spin_lock_bh(&zcrypt_device_lock);
 735	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 736		spin_lock(&zdev->ap_dev->lock);
 737		reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
 738			zdev->ap_dev->total_request_count;
 739		spin_unlock(&zdev->ap_dev->lock);
 740	}
 741	spin_unlock_bh(&zcrypt_device_lock);
 742}
 743
 744static int zcrypt_pendingq_count(void)
 745{
 746	struct zcrypt_device *zdev;
 747	int pendingq_count = 0;
 748
 749	spin_lock_bh(&zcrypt_device_lock);
 750	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 751		spin_lock(&zdev->ap_dev->lock);
 752		pendingq_count += zdev->ap_dev->pendingq_count;
 753		spin_unlock(&zdev->ap_dev->lock);
 754	}
 755	spin_unlock_bh(&zcrypt_device_lock);
 756	return pendingq_count;
 757}
 758
 759static int zcrypt_requestq_count(void)
 760{
 761	struct zcrypt_device *zdev;
 762	int requestq_count = 0;
 763
 764	spin_lock_bh(&zcrypt_device_lock);
 765	list_for_each_entry(zdev, &zcrypt_device_list, list) {
 766		spin_lock(&zdev->ap_dev->lock);
 767		requestq_count += zdev->ap_dev->requestq_count;
 768		spin_unlock(&zdev->ap_dev->lock);
 769	}
 770	spin_unlock_bh(&zcrypt_device_lock);
 771	return requestq_count;
 772}
 773
 774static int zcrypt_count_type(int type)
 775{
 776	struct zcrypt_device *zdev;
 777	int device_count = 0;
 778
 779	spin_lock_bh(&zcrypt_device_lock);
 780	list_for_each_entry(zdev, &zcrypt_device_list, list)
 781		if (zdev->user_space_type == type)
 782			device_count++;
 783	spin_unlock_bh(&zcrypt_device_lock);
 784	return device_count;
 785}
 786
 787/**
 788 * zcrypt_ica_status(): Old, depracted combi status call.
 789 *
 790 * Old, deprecated combi status call.
 791 */
 792static long zcrypt_ica_status(struct file *filp, unsigned long arg)
 793{
 794	struct ica_z90_status *pstat;
 795	int ret;
 796
 797	pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
 798	if (!pstat)
 799		return -ENOMEM;
 800	pstat->totalcount = zcrypt_device_count;
 801	pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
 802	pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
 803	pstat->requestqWaitCount = zcrypt_requestq_count();
 804	pstat->pendingqWaitCount = zcrypt_pendingq_count();
 805	pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
 806	pstat->cryptoDomain = ap_domain_index;
 807	zcrypt_status_mask(pstat->status);
 808	zcrypt_qdepth_mask(pstat->qdepth);
 809	ret = 0;
 810	if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
 811		ret = -EFAULT;
 812	kfree(pstat);
 813	return ret;
 814}
 815
 816static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
 817				  unsigned long arg)
 818{
 819	int rc;
 820
 821	switch (cmd) {
 822	case ICARSAMODEXPO: {
 823		struct ica_rsa_modexpo __user *umex = (void __user *) arg;
 824		struct ica_rsa_modexpo mex;
 825		if (copy_from_user(&mex, umex, sizeof(mex)))
 826			return -EFAULT;
 827		do {
 828			rc = zcrypt_rsa_modexpo(&mex);
 829		} while (rc == -EAGAIN);
 830		/* on failure: retry once again after a requested rescan */
 831		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
 832			do {
 833				rc = zcrypt_rsa_modexpo(&mex);
 834			} while (rc == -EAGAIN);
 835		if (rc)
 836			return rc;
 837		return put_user(mex.outputdatalength, &umex->outputdatalength);
 838	}
 839	case ICARSACRT: {
 840		struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
 841		struct ica_rsa_modexpo_crt crt;
 842		if (copy_from_user(&crt, ucrt, sizeof(crt)))
 843			return -EFAULT;
 844		do {
 845			rc = zcrypt_rsa_crt(&crt);
 846		} while (rc == -EAGAIN);
 847		/* on failure: retry once again after a requested rescan */
 848		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
 849			do {
 850				rc = zcrypt_rsa_crt(&crt);
 851			} while (rc == -EAGAIN);
 852		if (rc)
 853			return rc;
 854		return put_user(crt.outputdatalength, &ucrt->outputdatalength);
 855	}
 856	case ZSECSENDCPRB: {
 857		struct ica_xcRB __user *uxcRB = (void __user *) arg;
 858		struct ica_xcRB xcRB;
 859		if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
 860			return -EFAULT;
 861		do {
 862			rc = zcrypt_send_cprb(&xcRB);
 863		} while (rc == -EAGAIN);
 864		/* on failure: retry once again after a requested rescan */
 865		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
 866			do {
 867				rc = zcrypt_send_cprb(&xcRB);
 868			} while (rc == -EAGAIN);
 869		if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
 870			return -EFAULT;
 871		return rc;
 872	}
 873	case ZSENDEP11CPRB: {
 874		struct ep11_urb __user *uxcrb = (void __user *)arg;
 875		struct ep11_urb xcrb;
 876		if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
 877			return -EFAULT;
 878		do {
 879			rc = zcrypt_send_ep11_cprb(&xcrb);
 880		} while (rc == -EAGAIN);
 881		/* on failure: retry once again after a requested rescan */
 882		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
 883			do {
 884				rc = zcrypt_send_ep11_cprb(&xcrb);
 885			} while (rc == -EAGAIN);
 886		if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
 887			return -EFAULT;
 888		return rc;
 889	}
 890	case Z90STAT_STATUS_MASK: {
 891		char status[AP_DEVICES];
 892		zcrypt_status_mask(status);
 893		if (copy_to_user((char __user *) arg, status,
 894				 sizeof(char) * AP_DEVICES))
 895			return -EFAULT;
 896		return 0;
 897	}
 898	case Z90STAT_QDEPTH_MASK: {
 899		char qdepth[AP_DEVICES];
 900		zcrypt_qdepth_mask(qdepth);
 901		if (copy_to_user((char __user *) arg, qdepth,
 902				 sizeof(char) * AP_DEVICES))
 903			return -EFAULT;
 904		return 0;
 905	}
 906	case Z90STAT_PERDEV_REQCNT: {
 907		int reqcnt[AP_DEVICES];
 908		zcrypt_perdev_reqcnt(reqcnt);
 909		if (copy_to_user((int __user *) arg, reqcnt,
 910				 sizeof(int) * AP_DEVICES))
 911			return -EFAULT;
 912		return 0;
 913	}
 914	case Z90STAT_REQUESTQ_COUNT:
 915		return put_user(zcrypt_requestq_count(), (int __user *) arg);
 916	case Z90STAT_PENDINGQ_COUNT:
 917		return put_user(zcrypt_pendingq_count(), (int __user *) arg);
 918	case Z90STAT_TOTALOPEN_COUNT:
 919		return put_user(atomic_read(&zcrypt_open_count),
 920				(int __user *) arg);
 921	case Z90STAT_DOMAIN_INDEX:
 922		return put_user(ap_domain_index, (int __user *) arg);
 923	/*
 924	 * Deprecated ioctls. Don't add another device count ioctl,
 925	 * you can count them yourself in the user space with the
 926	 * output of the Z90STAT_STATUS_MASK ioctl.
 927	 */
 928	case ICAZ90STATUS:
 929		return zcrypt_ica_status(filp, arg);
 930	case Z90STAT_TOTALCOUNT:
 931		return put_user(zcrypt_device_count, (int __user *) arg);
 932	case Z90STAT_PCICACOUNT:
 933		return put_user(zcrypt_count_type(ZCRYPT_PCICA),
 934				(int __user *) arg);
 935	case Z90STAT_PCICCCOUNT:
 936		return put_user(zcrypt_count_type(ZCRYPT_PCICC),
 937				(int __user *) arg);
 938	case Z90STAT_PCIXCCMCL2COUNT:
 939		return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
 940				(int __user *) arg);
 941	case Z90STAT_PCIXCCMCL3COUNT:
 942		return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
 943				(int __user *) arg);
 944	case Z90STAT_PCIXCCCOUNT:
 945		return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
 946				zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
 947				(int __user *) arg);
 948	case Z90STAT_CEX2CCOUNT:
 949		return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
 950				(int __user *) arg);
 951	case Z90STAT_CEX2ACOUNT:
 952		return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
 953				(int __user *) arg);
 954	default:
 955		/* unknown ioctl number */
 956		return -ENOIOCTLCMD;
 957	}
 958}
 959
 960#ifdef CONFIG_COMPAT
 961/*
 962 * ioctl32 conversion routines
 963 */
 964struct compat_ica_rsa_modexpo {
 965	compat_uptr_t	inputdata;
 966	unsigned int	inputdatalength;
 967	compat_uptr_t	outputdata;
 968	unsigned int	outputdatalength;
 969	compat_uptr_t	b_key;
 970	compat_uptr_t	n_modulus;
 971};
 972
 973static long trans_modexpo32(struct file *filp, unsigned int cmd,
 974			    unsigned long arg)
 975{
 976	struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
 977	struct compat_ica_rsa_modexpo mex32;
 978	struct ica_rsa_modexpo mex64;
 979	long rc;
 980
 981	if (copy_from_user(&mex32, umex32, sizeof(mex32)))
 982		return -EFAULT;
 983	mex64.inputdata = compat_ptr(mex32.inputdata);
 984	mex64.inputdatalength = mex32.inputdatalength;
 985	mex64.outputdata = compat_ptr(mex32.outputdata);
 986	mex64.outputdatalength = mex32.outputdatalength;
 987	mex64.b_key = compat_ptr(mex32.b_key);
 988	mex64.n_modulus = compat_ptr(mex32.n_modulus);
 989	do {
 990		rc = zcrypt_rsa_modexpo(&mex64);
 991	} while (rc == -EAGAIN);
 992	/* on failure: retry once again after a requested rescan */
 993	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
 994		do {
 995			rc = zcrypt_rsa_modexpo(&mex64);
 996		} while (rc == -EAGAIN);
 997	if (rc)
 998		return rc;
 999	return put_user(mex64.outputdatalength,
1000			&umex32->outputdatalength);
1001}
1002
1003struct compat_ica_rsa_modexpo_crt {
1004	compat_uptr_t	inputdata;
1005	unsigned int	inputdatalength;
1006	compat_uptr_t	outputdata;
1007	unsigned int	outputdatalength;
1008	compat_uptr_t	bp_key;
1009	compat_uptr_t	bq_key;
1010	compat_uptr_t	np_prime;
1011	compat_uptr_t	nq_prime;
1012	compat_uptr_t	u_mult_inv;
1013};
1014
1015static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
1016				unsigned long arg)
1017{
1018	struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1019	struct compat_ica_rsa_modexpo_crt crt32;
1020	struct ica_rsa_modexpo_crt crt64;
1021	long rc;
1022
1023	if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1024		return -EFAULT;
1025	crt64.inputdata = compat_ptr(crt32.inputdata);
1026	crt64.inputdatalength = crt32.inputdatalength;
1027	crt64.outputdata=  compat_ptr(crt32.outputdata);
1028	crt64.outputdatalength = crt32.outputdatalength;
1029	crt64.bp_key = compat_ptr(crt32.bp_key);
1030	crt64.bq_key = compat_ptr(crt32.bq_key);
1031	crt64.np_prime = compat_ptr(crt32.np_prime);
1032	crt64.nq_prime = compat_ptr(crt32.nq_prime);
1033	crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1034	do {
1035		rc = zcrypt_rsa_crt(&crt64);
1036	} while (rc == -EAGAIN);
1037	/* on failure: retry once again after a requested rescan */
1038	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1039		do {
1040			rc = zcrypt_rsa_crt(&crt64);
1041		} while (rc == -EAGAIN);
1042	if (rc)
1043		return rc;
1044	return put_user(crt64.outputdatalength,
1045			&ucrt32->outputdatalength);
1046}
1047
1048struct compat_ica_xcRB {
1049	unsigned short	agent_ID;
1050	unsigned int	user_defined;
1051	unsigned short	request_ID;
1052	unsigned int	request_control_blk_length;
1053	unsigned char	padding1[16 - sizeof (compat_uptr_t)];
1054	compat_uptr_t	request_control_blk_addr;
1055	unsigned int	request_data_length;
1056	char		padding2[16 - sizeof (compat_uptr_t)];
1057	compat_uptr_t	request_data_address;
1058	unsigned int	reply_control_blk_length;
1059	char		padding3[16 - sizeof (compat_uptr_t)];
1060	compat_uptr_t	reply_control_blk_addr;
1061	unsigned int	reply_data_length;
1062	char		padding4[16 - sizeof (compat_uptr_t)];
1063	compat_uptr_t	reply_data_addr;
1064	unsigned short	priority_window;
1065	unsigned int	status;
1066} __attribute__((packed));
1067
1068static long trans_xcRB32(struct file *filp, unsigned int cmd,
1069			 unsigned long arg)
1070{
1071	struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1072	struct compat_ica_xcRB xcRB32;
1073	struct ica_xcRB xcRB64;
1074	long rc;
1075
1076	if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1077		return -EFAULT;
1078	xcRB64.agent_ID = xcRB32.agent_ID;
1079	xcRB64.user_defined = xcRB32.user_defined;
1080	xcRB64.request_ID = xcRB32.request_ID;
1081	xcRB64.request_control_blk_length =
1082		xcRB32.request_control_blk_length;
1083	xcRB64.request_control_blk_addr =
1084		compat_ptr(xcRB32.request_control_blk_addr);
1085	xcRB64.request_data_length =
1086		xcRB32.request_data_length;
1087	xcRB64.request_data_address =
1088		compat_ptr(xcRB32.request_data_address);
1089	xcRB64.reply_control_blk_length =
1090		xcRB32.reply_control_blk_length;
1091	xcRB64.reply_control_blk_addr =
1092		compat_ptr(xcRB32.reply_control_blk_addr);
1093	xcRB64.reply_data_length = xcRB32.reply_data_length;
1094	xcRB64.reply_data_addr =
1095		compat_ptr(xcRB32.reply_data_addr);
1096	xcRB64.priority_window = xcRB32.priority_window;
1097	xcRB64.status = xcRB32.status;
1098	do {
1099		rc = zcrypt_send_cprb(&xcRB64);
1100	} while (rc == -EAGAIN);
1101	/* on failure: retry once again after a requested rescan */
1102	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1103		do {
1104			rc = zcrypt_send_cprb(&xcRB64);
1105		} while (rc == -EAGAIN);
1106	xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1107	xcRB32.reply_data_length = xcRB64.reply_data_length;
1108	xcRB32.status = xcRB64.status;
1109	if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1110			return -EFAULT;
1111	return rc;
1112}
1113
1114static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1115			 unsigned long arg)
1116{
1117	if (cmd == ICARSAMODEXPO)
1118		return trans_modexpo32(filp, cmd, arg);
1119	if (cmd == ICARSACRT)
1120		return trans_modexpo_crt32(filp, cmd, arg);
1121	if (cmd == ZSECSENDCPRB)
1122		return trans_xcRB32(filp, cmd, arg);
1123	return zcrypt_unlocked_ioctl(filp, cmd, arg);
1124}
1125#endif
1126
1127/*
1128 * Misc device file operations.
1129 */
1130static const struct file_operations zcrypt_fops = {
1131	.owner		= THIS_MODULE,
1132	.read		= zcrypt_read,
1133	.write		= zcrypt_write,
1134	.unlocked_ioctl	= zcrypt_unlocked_ioctl,
1135#ifdef CONFIG_COMPAT
1136	.compat_ioctl	= zcrypt_compat_ioctl,
1137#endif
1138	.open		= zcrypt_open,
1139	.release	= zcrypt_release,
1140	.llseek		= no_llseek,
1141};
1142
1143/*
1144 * Misc device.
1145 */
1146static struct miscdevice zcrypt_misc_device = {
1147	.minor	    = MISC_DYNAMIC_MINOR,
1148	.name	    = "z90crypt",
1149	.fops	    = &zcrypt_fops,
1150};
1151
1152/*
1153 * Deprecated /proc entry support.
1154 */
1155static struct proc_dir_entry *zcrypt_entry;
1156
1157static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
1158{
1159	int i;
1160
1161	for (i = 0; i < len; i++)
1162		seq_printf(m, "%01x", (unsigned int) addr[i]);
1163	seq_putc(m, ' ');
1164}
1165
1166static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
1167{
1168	int inl, c, cx;
1169
1170	seq_printf(m, "	   ");
1171	inl = 0;
1172	for (c = 0; c < (len / 16); c++) {
1173		sprintcl(m, addr+inl, 16);
1174		inl += 16;
1175	}
1176	cx = len%16;
1177	if (cx) {
1178		sprintcl(m, addr+inl, cx);
1179		inl += cx;
1180	}
1181	seq_putc(m, '\n');
1182}
1183
1184static void sprinthx(unsigned char *title, struct seq_file *m,
1185		     unsigned char *addr, unsigned int len)
1186{
1187	int inl, r, rx;
1188
1189	seq_printf(m, "\n%s\n", title);
1190	inl = 0;
1191	for (r = 0; r < (len / 64); r++) {
1192		sprintrw(m, addr+inl, 64);
1193		inl += 64;
1194	}
1195	rx = len % 64;
1196	if (rx) {
1197		sprintrw(m, addr+inl, rx);
1198		inl += rx;
1199	}
1200	seq_putc(m, '\n');
1201}
1202
1203static void sprinthx4(unsigned char *title, struct seq_file *m,
1204		      unsigned int *array, unsigned int len)
1205{
1206	int r;
1207
1208	seq_printf(m, "\n%s\n", title);
1209	for (r = 0; r < len; r++) {
1210		if ((r % 8) == 0)
1211			seq_printf(m, "    ");
1212		seq_printf(m, "%08X ", array[r]);
1213		if ((r % 8) == 7)
1214			seq_putc(m, '\n');
1215	}
1216	seq_putc(m, '\n');
1217}
1218
1219static int zcrypt_proc_show(struct seq_file *m, void *v)
1220{
1221	char workarea[sizeof(int) * AP_DEVICES];
1222
1223	seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
1224		   ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
1225	seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
1226	seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
1227	seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
1228	seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
1229	seq_printf(m, "PCIXCC MCL2 count: %d\n",
1230		   zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
1231	seq_printf(m, "PCIXCC MCL3 count: %d\n",
1232		   zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1233	seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
1234	seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
1235	seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
1236	seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
1237	seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
1238	seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
1239	seq_printf(m, "Total open handles: %d\n\n",
1240		   atomic_read(&zcrypt_open_count));
1241	zcrypt_status_mask(workarea);
1242	sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1243		 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1244		 m, workarea, AP_DEVICES);
1245	zcrypt_qdepth_mask(workarea);
1246	sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
1247	zcrypt_perdev_reqcnt((int *) workarea);
1248	sprinthx4("Per-device successfully completed request counts",
1249		  m, (unsigned int *) workarea, AP_DEVICES);
1250	return 0;
1251}
1252
1253static int zcrypt_proc_open(struct inode *inode, struct file *file)
1254{
1255	return single_open(file, zcrypt_proc_show, NULL);
1256}
1257
1258static void zcrypt_disable_card(int index)
1259{
1260	struct zcrypt_device *zdev;
1261
1262	spin_lock_bh(&zcrypt_device_lock);
1263	list_for_each_entry(zdev, &zcrypt_device_list, list)
1264		if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1265			zdev->online = 0;
1266			ap_flush_queue(zdev->ap_dev);
1267			break;
1268		}
1269	spin_unlock_bh(&zcrypt_device_lock);
1270}
1271
1272static void zcrypt_enable_card(int index)
1273{
1274	struct zcrypt_device *zdev;
1275
1276	spin_lock_bh(&zcrypt_device_lock);
1277	list_for_each_entry(zdev, &zcrypt_device_list, list)
1278		if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1279			zdev->online = 1;
1280			break;
1281		}
1282	spin_unlock_bh(&zcrypt_device_lock);
1283}
1284
1285static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
1286				 size_t count, loff_t *pos)
1287{
1288	unsigned char *lbuf, *ptr;
1289	size_t local_count;
1290	int j;
1291
1292	if (count <= 0)
1293		return 0;
1294
1295#define LBUFSIZE 1200UL
1296	lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1297	if (!lbuf)
1298		return 0;
1299
1300	local_count = min(LBUFSIZE - 1, count);
1301	if (copy_from_user(lbuf, buffer, local_count) != 0) {
1302		kfree(lbuf);
1303		return -EFAULT;
1304	}
1305	lbuf[local_count] = '\0';
1306
1307	ptr = strstr(lbuf, "Online devices");
1308	if (!ptr)
1309		goto out;
1310	ptr = strstr(ptr, "\n");
1311	if (!ptr)
1312		goto out;
1313	ptr++;
1314
1315	if (strstr(ptr, "Waiting work element counts") == NULL)
1316		goto out;
1317
1318	for (j = 0; j < 64 && *ptr; ptr++) {
1319		/*
1320		 * '0' for no device, '1' for PCICA, '2' for PCICC,
1321		 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1322		 * '5' for CEX2C and '6' for CEX2A'
1323		 * '7' for CEX3C and '8' for CEX3A
1324		 */
1325		if (*ptr >= '0' && *ptr <= '8')
1326			j++;
1327		else if (*ptr == 'd' || *ptr == 'D')
1328			zcrypt_disable_card(j++);
1329		else if (*ptr == 'e' || *ptr == 'E')
1330			zcrypt_enable_card(j++);
1331		else if (*ptr != ' ' && *ptr != '\t')
1332			break;
1333	}
1334out:
1335	kfree(lbuf);
1336	return count;
1337}
1338
1339static const struct file_operations zcrypt_proc_fops = {
1340	.owner		= THIS_MODULE,
1341	.open		= zcrypt_proc_open,
1342	.read		= seq_read,
1343	.llseek		= seq_lseek,
1344	.release	= single_release,
1345	.write		= zcrypt_proc_write,
1346};
1347
1348static int zcrypt_rng_device_count;
1349static u32 *zcrypt_rng_buffer;
1350static int zcrypt_rng_buffer_index;
1351static DEFINE_MUTEX(zcrypt_rng_mutex);
1352
1353static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1354{
1355	int rc;
1356
1357	/*
1358	 * We don't need locking here because the RNG API guarantees serialized
1359	 * read method calls.
1360	 */
1361	if (zcrypt_rng_buffer_index == 0) {
1362		rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1363		/* on failure: retry once again after a requested rescan */
1364		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1365			rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1366		if (rc < 0)
1367			return -EIO;
1368		zcrypt_rng_buffer_index = rc / sizeof *data;
1369	}
1370	*data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1371	return sizeof *data;
1372}
1373
1374static struct hwrng zcrypt_rng_dev = {
1375	.name		= "zcrypt",
1376	.data_read	= zcrypt_rng_data_read,
1377};
1378
1379static int zcrypt_rng_device_add(void)
1380{
1381	int rc = 0;
1382
1383	mutex_lock(&zcrypt_rng_mutex);
1384	if (zcrypt_rng_device_count == 0) {
1385		zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1386		if (!zcrypt_rng_buffer) {
1387			rc = -ENOMEM;
1388			goto out;
1389		}
1390		zcrypt_rng_buffer_index = 0;
1391		rc = hwrng_register(&zcrypt_rng_dev);
1392		if (rc)
1393			goto out_free;
1394		zcrypt_rng_device_count = 1;
1395	} else
1396		zcrypt_rng_device_count++;
1397	mutex_unlock(&zcrypt_rng_mutex);
1398	return 0;
1399
1400out_free:
1401	free_page((unsigned long) zcrypt_rng_buffer);
1402out:
1403	mutex_unlock(&zcrypt_rng_mutex);
1404	return rc;
1405}
1406
1407static void zcrypt_rng_device_remove(void)
1408{
1409	mutex_lock(&zcrypt_rng_mutex);
1410	zcrypt_rng_device_count--;
1411	if (zcrypt_rng_device_count == 0) {
1412		hwrng_unregister(&zcrypt_rng_dev);
1413		free_page((unsigned long) zcrypt_rng_buffer);
1414	}
1415	mutex_unlock(&zcrypt_rng_mutex);
1416}
1417
1418int __init zcrypt_debug_init(void)
1419{
1420	debugfs_root = debugfs_create_dir("zcrypt", NULL);
1421
1422	zcrypt_dbf_common = debug_register("zcrypt_common", 1, 1, 16);
1423	debug_register_view(zcrypt_dbf_common, &debug_hex_ascii_view);
1424	debug_set_level(zcrypt_dbf_common, DBF_ERR);
1425
1426	zcrypt_dbf_devices = debug_register("zcrypt_devices", 1, 1, 16);
1427	debug_register_view(zcrypt_dbf_devices, &debug_hex_ascii_view);
1428	debug_set_level(zcrypt_dbf_devices, DBF_ERR);
1429
1430	return 0;
1431}
1432
1433void zcrypt_debug_exit(void)
1434{
1435	debugfs_remove(debugfs_root);
1436	if (zcrypt_dbf_common)
1437		debug_unregister(zcrypt_dbf_common);
1438	if (zcrypt_dbf_devices)
1439		debug_unregister(zcrypt_dbf_devices);
1440}
1441
1442/**
1443 * zcrypt_api_init(): Module initialization.
1444 *
1445 * The module initialization code.
1446 */
1447int __init zcrypt_api_init(void)
1448{
1449	int rc;
1450
1451	rc = zcrypt_debug_init();
1452	if (rc)
1453		goto out;
1454
1455	atomic_set(&zcrypt_rescan_req, 0);
1456
1457	/* Register the request sprayer. */
1458	rc = misc_register(&zcrypt_misc_device);
1459	if (rc < 0)
1460		goto out;
1461
1462	/* Set up the proc file system */
1463	zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
1464	if (!zcrypt_entry) {
1465		rc = -ENOMEM;
1466		goto out_misc;
1467	}
1468
1469	return 0;
1470
1471out_misc:
1472	misc_deregister(&zcrypt_misc_device);
1473out:
1474	return rc;
1475}
1476
1477/**
1478 * zcrypt_api_exit(): Module termination.
1479 *
1480 * The module termination code.
1481 */
1482void zcrypt_api_exit(void)
1483{
1484	remove_proc_entry("driver/z90crypt", NULL);
1485	misc_deregister(&zcrypt_misc_device);
1486	zcrypt_debug_exit();
1487}
1488
 
1489module_init(zcrypt_api_init);
1490module_exit(zcrypt_api_exit);