Linux Audio

Check our new training course

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
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);