/*
* linux/drivers/s390/crypto/zcrypt_api.c
*
* zcrypt 2.1.0
*
* Copyright (C) 2001, 2006 IBM Corporation
* Author(s): Robert Burroughs
* Eric Rossman (edrossma@us.ibm.com)
* Cornelia Huck <cornelia.huck@de.ibm.com>
*
* Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
* Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
* Ralph Wuerthner <rwuerthn@de.ibm.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/compat.h>
#include <asm/atomic.h>
#include <asm/uaccess.h>
#include "zcrypt_api.h"
/**
* Module description.
*/
MODULE_AUTHOR("IBM Corporation");
MODULE_DESCRIPTION("Cryptographic Coprocessor interface, "
"Copyright 2001, 2006 IBM Corporation");
MODULE_LICENSE("GPL");
static DEFINE_SPINLOCK(zcrypt_device_lock);
static LIST_HEAD(zcrypt_device_list);
static int zcrypt_device_count = 0;
static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
/**
* Device attributes common for all crypto devices.
*/
static ssize_t zcrypt_type_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct zcrypt_device *zdev = to_ap_dev(dev)->private;
return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
}
static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
static ssize_t zcrypt_online_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct zcrypt_device *zdev = to_ap_dev(dev)->private;
return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
}
static ssize_t zcrypt_online_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct zcrypt_device *zdev = to_ap_dev(dev)->private;
int online;
if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
return -EINVAL;
zdev->online = online;
if (!online)
ap_flush_queue(zdev->ap_dev);
return count;
}
static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
static struct attribute * zcrypt_device_attrs[] = {
&dev_attr_type.attr,
&dev_attr_online.attr,
NULL,
};
static struct attribute_group zcrypt_device_attr_group = {
.attrs = zcrypt_device_attrs,
};
/**
* Move the device towards the head of the device list.
* Need to be called while holding the zcrypt device list lock.
* Note: cards with speed_rating of 0 are kept at the end of the list.
*/
static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
{
struct zcrypt_device *tmp;
struct list_head *l;
if (zdev->speed_rating == 0)
return;
for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
tmp = list_entry(l, struct zcrypt_device, list);
if ((tmp->request_count + 1) * tmp->speed_rating <=
(zdev->request_count + 1) * zdev->speed_rating &&
tmp->speed_rating != 0)
break;
}
if (l == zdev->list.prev)
return;
/* Move zdev behind l */
list_del(&zdev->list);
list_add(&zdev->list, l);
}
/**
* Move the device towards the tail of the device list.
* Need to be called while holding the zcrypt device list lock.
* Note: cards with speed_rating of 0 are kept at the end of the list.
*/
static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
{
struct