/*
 * linux/drivers/s390/block/dasd_cmb.c ($Revision: 1.7.6.2 $)
 *
 * Linux on zSeries Channel Measurement Facility support
 *  (dasd device driver interface)
 *
 * Copyright 2000,2003 IBM Corporation
 *
 * Author: Arnd Bergmann <arndb@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 <asm/cmb.h>
#include <asm/ioctl32.h>

#include "dasd_int.h"

/* This mutex protects us from a race between enable and disable for
 * a single device. Making it global instead of per device reduces
 * the memory requirement and makes it possible to use a single
 * completion handler and return value */
static DECLARE_MUTEX(cmf_setup_mutex);
static DECLARE_COMPLETION(cmf_setup_completion);
static int cmf_setup_return;

static void
dasd_cmf_enable_callback(struct cmf_device *cdev)
{
	cdev->callback = NULL;
	cmf_setup_return = set_cmf(cdev, 2);
	complete(&cmf_setup_completion);
}

static void
dasd_cmf_disable_callback(struct cmf_device *cdev)
{
	cdev->callback = NULL;
	cmf_setup_return = set_cmf(cdev, 0);
	complete(&cmf_setup_completion);
}

static inline int
dasd_cmf_device_busy(struct dasd_device_t *device)
{
	ccw_req_t *cqr;
	for (cqr = device->queue.head; cqr; cqr = cqr->next) {
		if (cqr->status == CQR_STATUS_IN_IO)
			return 1;
	}
	return 0;
}

static int
dasd_ioctl_cmf_enable(void *inp, int no, long args)
{
	struct dasd_device_t *device;
	int ret;

	device = dasd_device_from_kdev (((struct inode*)inp)->i_rdev);
	if (!device)
		return -EINVAL;
	
	if (down_interruptible(&cmf_setup_mutex))
		return -ERESTARTSYS;

	/* the device may already be enabled, in this case
	   we just reset the cmb to 0 */
	if (!list_empty(&device->cdev.cmb_list)) {
		ret = 0;
		goto out_reset;
	}
		
	ret = enable_cmf(&device->cdev);
	if (ret)
		goto out;

	MOD_INC_USE_COUNT;

	spin_lock_irq(device->cdev.ccwlock);
	if (!dasd_cmf_device_busy(device)) {
		ret = set_cmf(&device->cdev, 2);
		spin_unlock_irq(device->cdev.ccwlock);
	} else {
		device->cdev.callback = &dasd_cmf_enable_callback;
		spin_unlock_irq(device->cdev.ccwlock);
		wait_for_completion(&cmf_setup_completion);
		ret = cmf_setup_return;
	}

	if (ret) {
		disable_cmf(&device->cdev);
		MOD_DEC_USE_COUNT;
	}

out_reset:
	cmf_reset(&device->cdev);
out:
	up(&cmf_setup_mutex);
	return ret;
}

static int
dasd_ioctl_cmf_disable(void *inp, int no, long args)
{
	struct dasd_device_t *device;
	int ret;

	device = dasd_device_from_kdev (((struct inode*)inp)->i_rdev);
	if (!device)
		return -EINVAL;

	if (down_interruptible(&cmf_setup_mutex))
		return -ERESTARTSYS;

	spin_lock_irq(device->cdev.ccwlock);

	if (!dasd_cmf_device_busy(device)) {
		ret = set_cmf(&device->cdev, 0);
		spin_unlock_irq(device->cdev.ccwlock);
	} else {
		device->cdev.callback = &dasd_cmf_disable_callback;
		spin_unlock_irq(device->cdev.ccwlock);
		wait_for_completion(&cmf_setup_completion);
		ret = cmf_setup_return;
	}

	if(!ret) {
		disable_cmf(&device->cdev);
		MOD_DEC_USE_COUNT;
	}
	up(&cmf_setup_mutex);
	return ret;

}

static int
dasd_ioctl_readall_cmb(void *inp, int no, long args)
{
	struct dasd_device_t *device;
	struct cmbdata * udata;
	struct cmbdata data;
	size_t size;
	int ret;

	device = dasd_device_from_kdev (((struct inode*)inp)->i_rdev);
	if (!device)
		return -EINVAL;
	udata = (void *) args;
	size = _IOC_SIZE(no);

	if (!access_ok(VERIFY_WRITE, udata, size))
		return -EFAULT;
	ret = cmf_readall(&device->cdev, &data);
	if (ret)
		return ret;
	if (copy_to_user(udata, &data, min(size, sizeof(*udata))))
		return -EFAULT;
	return 0;
}

/* module initialization below here. dasd already provides a mechanism
 * to dynamically register ioctl functions, so we simply use this.
 * FIXME: register ioctl32 functions as well. */
static inline int
ioctl_reg(unsigned int no, dasd_ioctl_fn_t handler)
{
	int ret;
	ret = dasd_ioctl_no_register(THIS_MODULE, no, handler);
	if (ret)
		return ret;

	ret = register_ioctl32_conversion(no, sys_ioctl);
	if (ret)
		dasd_ioctl_no_unregister(THIS_MODULE, no, handler);

	return ret;
}

static inline void
ioctl_unreg(unsigned int no, dasd_ioctl_fn_t handler)
{
	dasd_ioctl_no_unregister(THIS_MODULE, no, handler);
	unregister_ioctl32_conversion(no);

}

static void
dasd_cmf_exit(void)
{
	ioctl_unreg(BIODASDCMFENABLE,  dasd_ioctl_cmf_enable);
	ioctl_unreg(BIODASDCMFDISABLE, dasd_ioctl_cmf_disable);
	ioctl_unreg(BIODASDREADALLCMB, dasd_ioctl_readall_cmb);
}

static int __init
dasd_cmf_init(void)
{
	int ret;
	ret = ioctl_reg (BIODASDCMFENABLE, dasd_ioctl_cmf_enable);
	if (ret)
		goto err;
	ret = ioctl_reg (BIODASDCMFDISABLE, dasd_ioctl_cmf_disable);
	if (ret)
		goto err;
	ret = ioctl_reg (BIODASDREADALLCMB, dasd_ioctl_readall_cmb);
	if (ret)
		goto err;

	return 0;
err:
	dasd_cmf_exit();

	return ret;
}

module_init(dasd_cmf_init);
module_exit(dasd_cmf_exit);

MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("channel measurement facility interface for dasd\n"
		   "Copyright 2003 IBM Corporation\n");
