/*++
"This software program is available to you under a choice of one of two
licenses.  You may choose to be licensed under either the GNU General Public
License (GPL) Version 2, June 1991, available at
http://www.fsf.org/copyleft/gpl.html, or the Intel BSD + Patent License,
the text of which follows:

"Recipient" has requested a license and Intel Corporation ("Intel") is willing
to grant a license for the software entitled InfiniBand(tm) System Software
(the "Software") being provided by Intel Corporation.

The following definitions apply to this License:

"Licensed Patents" means patent claims licensable by Intel Corporation which
are necessarily infringed by the use or sale of the Software alone or when
combined with the operating system referred to below.

"Recipient" means the party to whom Intel delivers this Software.
"Licensee" means Recipient and those third parties that receive a license to
any operating system available under the GNU Public License version 2.0 or
later.

Copyright (c) 1996-2003 Intel Corporation. All rights reserved.

The license is provided to Recipient and Recipient's Licensees under the
following terms.

Redistribution and use in source and binary forms of the Software, with or
without modification, are permitted provided that the following conditions are
met:
Redistributions of source code of the Software may retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form of the Software may reproduce the above
copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

Neither the name of Intel Corporation nor the names of its contributors shall
be used to endorse or promote products derived from this Software without
specific prior written permission.

Intel hereby grants Recipient and Licensees a non-exclusive, worldwide,
royalty-free patent license under Licensed Patents to make, use, sell, offer
to sell, import and otherwise transfer the Software, if any, in source code
and object code form. This license shall include changes to the Software that
are error corrections or other minor changes to the Software that do not add
functionality or features when the Software is incorporated in any version of
a operating system that has been distributed under the GNU General Public
License 2.0 or later.  This patent license shall apply to the combination of
the Software and any operating system licensed under the GNU Public License
version 2.0 or later if, at the time Intel provides the Software to Recipient,
such addition of the Software to the then publicly available versions of such
operating system available under the GNU Public License version 2.0 or later
(whether in gold, beta or alpha form) causes such combination to be covered by
the Licensed Patents. The patent license shall not apply to any other
combinations which include the Software. No hardware per se is licensed
hereunder.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
--*/

/*
 * Abstract:
 *	This module defines
 *
 * Environment:
 *	Linux Kernel Mode
 *
 * $Revision: 1.1 $
 */

#include <complib/cl_types.h>
#include <complib/cl_memory.h>
#include <complib/cl_debug.h>

#include <complib/cl_device.h>
#include <complib/cl_waitobj.h>
#include <complib/cl_syshelper.h>

#include <linux/fs.h>
#include <linux/file.h>

/*
 * System Helper Globals
 */
cl_dev_handle_t g_syshelper_dev_handle;
static boolean_t syshelper_device_created = FALSE;

/*
 * Syshelper functions
 */
cl_status_t
_syshelper_open(IN void *p_device_context, OUT void **pp_open_context);

void
 _syshelper_ioctl(IN void *p_device_context,
		  IN void *p_open_context,
		  IN cl_ioctl_info_t * p_kernel_ioctl_info);

cl_status_t
_syshelper_close(IN void *p_device_context, IN void *p_open_context);

/*
 * To create a system helper device, populate a
 * cl_dev_info_t structure and register with the
 * device framework
 */
static cl_dev_info_t g_syshelper_dev_info = {
      name:SYSHELP_DEVICE_NAME,
      p_device_context:NULL,	/* for now */
      pfn_open:_syshelper_open,
      pfn_close:_syshelper_close,
      pfn_ioctl:_syshelper_ioctl,
      pfn_mmap:NULL,
      max_use_count:0		/* no limits ! */
};

cl_status_t
_syshelper_open(IN void *p_device_context, OUT void **pp_open_context)
{
	cl_wait_obj_handle_t *p_wait_obj_handle = NULL;
	/*
	 * Allocate a Wait Object handle and assign that to the
	 * open_context.  This handle will be populated in the
	 * create_wait_object ioctl.
	 */
	p_wait_obj_handle = cl_zalloc(sizeof (cl_wait_obj_handle_t));
	if (!p_wait_obj_handle) {
		cl_dbg_out
		    ("_syshelper_open: Memory allocation failed for wait object handle\n");
		return CL_INSUFFICIENT_MEMORY;
	}

	*p_wait_obj_handle = NULL;
	*pp_open_context = p_wait_obj_handle;

	return CL_SUCCESS;
}

void
_syshelper_ioctl(IN void *p_device_context,
		 IN void *p_open_context,
		 IN cl_ioctl_info_t * p_kernel_ioctl_info)
{
	cl_status_t ioctl_status = CL_SUCCESS;
	cl_status_t status = CL_ERROR;
	cl_wait_obj_handle_t *p_wait_obj_handle;
	cl_wait_ioctl_params_t *ioctl_params = NULL;
	uint32_t wait_u_sec = 0;
	cl_create_wait_obj_params_t *create_ioctl_buf = NULL;
	boolean_t auto_reset = FALSE;

	p_wait_obj_handle = p_open_context;
	/*
	 * So what are we trying to do here ? Find out the command
	 */
	switch (p_kernel_ioctl_info->command) {
	case CREATE_WAIT_OBJ:
		{
			if (*p_wait_obj_handle != NULL) {
				ioctl_status = CL_ERROR;
				break;
			}

			create_ioctl_buf =
			    (cl_create_wait_obj_params_t
			     *) (p_kernel_ioctl_info->p_buf);
			auto_reset = create_ioctl_buf->auto_reset;

			status = __cl_create_wait_object(auto_reset,
							 p_wait_obj_handle);
			/*
			 * Return the status to user mode.  The handle is the fd
			 * handle of the open_device
			 */
			create_ioctl_buf->status = status;
			p_kernel_ioctl_info->num_bytes_ret =
			    sizeof (cl_create_wait_obj_params_t);
			break;
		}

	case WAITON_WAIT_OBJ:
		{
			ioctl_params =
			    (cl_wait_ioctl_params_t *) (p_kernel_ioctl_info->
							p_buf);
			//wait_obj_handle = ioctl_params->wait_obj_handle;
			wait_u_sec = ioctl_params->wait_u_sec;

			status = __cl_wait_on_wait_object(*p_wait_obj_handle,
							  wait_u_sec);

			/* Return the status of WaitObj in the output buffer */
			ioctl_params->wait_status = status;
			p_kernel_ioctl_info->num_bytes_ret =
			    sizeof (cl_wait_ioctl_params_t);
			break;
		}

	case TRIGGER_WAIT_OBJ:
		{
			__cl_signal_wait_object(*p_wait_obj_handle);
			break;
		}

	case RESET_WAIT_OBJ:
		{
			__cl_clear_wait_object(*p_wait_obj_handle);
			break;
		}

	default:
		cl_msg_out("_syshelper_ioctl: Unsupported command (0x%x) \n",
			   (int) p_kernel_ioctl_info->command);

		ioctl_status = CL_ERROR;

	}
	p_kernel_ioctl_info->io_status = ioctl_status;
}

cl_status_t
_syshelper_close(IN void *p_device_context, IN void *p_open_context)
{
	cl_wait_obj_handle_t *p_wait_obj_handle = p_open_context;

	if (*p_wait_obj_handle) {
		__cl_destroy_wait_object(*p_wait_obj_handle);
	}

	cl_free(p_wait_obj_handle);

	return (CL_SUCCESS);
}

/*
 * Initialize the system helper data structures and prepare
 * for use
 */
cl_status_t
__cl_syshelper_init(void)
{
	if (cl_dev_create(&g_syshelper_dev_info,
			  &g_syshelper_dev_handle) == CL_ERROR) {
		cl_msg_out
		    ("failed to create system device (%s) for syshelper\n",
		     SYSHELP_DEVICE_NAME);
		return CL_ERROR;
	}

	cl_dbg_out("__cl_syshelper_init: Created Device (%s) \n",
		   ((cl_dev_info_t *) g_syshelper_dev_handle)->name);

	syshelper_device_created = TRUE;

	return CL_SUCCESS;
}

/*
 * Cleanup system helper data structures.
 */
void
__cl_syshelper_exit(void)
{
	if (syshelper_device_created) {
		cl_dev_destroy(g_syshelper_dev_handle);
	}

	cl_dbg_out("__cl_syshelper_exit: system helper device deleted !\n");
}

/*
 * This function validates a user wait object handle and returns
 * the corresponding kernel wait object handle.  Only kernel
 * clients call this function.
 */
cl_wait_obj_handle_t
cl_get_kernel_wait_object(IN cl_wait_obj_handle_t user_mode_handle)
{
	int fd = (int) (uintn_t) user_mode_handle;
	cl_dev_info_t *p_dev_info = NULL;
	void *p_open_context = NULL;
	cl_status_t status;

	status = cl_get_open_context(fd, &p_open_context, &p_dev_info);
	if (status != CL_SUCCESS) {
		cl_dbg_out
		    ("cl_get_kernel_wait_object: Could not validate handle\n");
		return NULL;
	}

	if (!p_dev_info || !p_open_context) {
		cl_dbg_out
		    ("Wrong device info (%p) or context (%p) returned by complib\n",
		     p_dev_info, p_open_context);
		return NULL;
	}

	/* Now verify the owner and make sure that it is syshelper */
	if (strcmp(p_dev_info->name, SYSHELP_DEVICE_NAME) != 0) {
		cl_dbg_out("Wrong owner (%s) for the file handle\n",
			   p_dev_info->name);
		return NULL;
	}

	return (*((cl_wait_obj_handle_t *) p_open_context));
}
