907 lines · 19.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * ipmi_devintf.c * * Linux device interface for the IPMI message handler. * * Author: MontaVista Software, Inc. *         Corey Minyard <minyard@mvista.com> *         source@mvista.com * * Copyright 2002 MontaVista Software Inc. */ #include <linux/module.h>#include <linux/moduleparam.h>#include <linux/errno.h>#include <linux/poll.h>#include <linux/sched.h>#include <linux/spinlock.h>#include <linux/slab.h>#include <linux/ipmi.h>#include <linux/mutex.h>#include <linux/init.h>#include <linux/device.h>#include <linux/compat.h> struct ipmi_file_private{	struct ipmi_user     *user;	spinlock_t           recv_msg_lock;	struct list_head     recv_msgs;	struct fasync_struct *fasync_queue;	wait_queue_head_t    wait;	struct mutex	     recv_mutex;	int                  default_retries;	unsigned int         default_retry_time_ms;}; static void file_receive_handler(struct ipmi_recv_msg *msg,				 void                 *handler_data){	struct ipmi_file_private *priv = handler_data;	int                      was_empty;	unsigned long            flags; 	spin_lock_irqsave(&priv->recv_msg_lock, flags);	was_empty = list_empty(&priv->recv_msgs);	list_add_tail(&msg->link, &priv->recv_msgs);	spin_unlock_irqrestore(&priv->recv_msg_lock, flags); 	if (was_empty) {		wake_up_interruptible(&priv->wait);		kill_fasync(&priv->fasync_queue, SIGIO, POLL_IN);	}} static __poll_t ipmi_poll(struct file *file, poll_table *wait){	struct ipmi_file_private *priv = file->private_data;	__poll_t             mask = 0;	unsigned long            flags; 	poll_wait(file, &priv->wait, wait); 	spin_lock_irqsave(&priv->recv_msg_lock, flags); 	if (!list_empty(&priv->recv_msgs))		mask |= (EPOLLIN | EPOLLRDNORM); 	spin_unlock_irqrestore(&priv->recv_msg_lock, flags); 	return mask;} static int ipmi_fasync(int fd, struct file *file, int on){	struct ipmi_file_private *priv = file->private_data; 	return fasync_helper(fd, file, on, &priv->fasync_queue);} static const struct ipmi_user_hndl ipmi_hndlrs ={	.ipmi_recv_hndl	= file_receive_handler,}; static int ipmi_open(struct inode *inode, struct file *file){	int                      if_num = iminor(inode);	int                      rv;	struct ipmi_file_private *priv; 	priv = kmalloc_obj(*priv);	if (!priv)		return -ENOMEM; 	rv = ipmi_create_user(if_num,			      &ipmi_hndlrs,			      priv,			      &priv->user);	if (rv) {		kfree(priv);		goto out;	} 	file->private_data = priv; 	spin_lock_init(&priv->recv_msg_lock);	INIT_LIST_HEAD(&priv->recv_msgs);	init_waitqueue_head(&priv->wait);	priv->fasync_queue = NULL;	mutex_init(&priv->recv_mutex); 	/* Use the low-level defaults. */	priv->default_retries = -1;	priv->default_retry_time_ms = 0; out:	return rv;} static int ipmi_release(struct inode *inode, struct file *file){	struct ipmi_file_private *priv = file->private_data;	struct ipmi_recv_msg *msg, *next; 	ipmi_destroy_user(priv->user); 	list_for_each_entry_safe(msg, next, &priv->recv_msgs, link)		ipmi_free_recv_msg(msg); 	kfree(priv); 	return 0;} static int handle_send_req(struct ipmi_user *user,			   struct ipmi_req *req,			   int             retries,			   unsigned int    retry_time_ms){	int              rv;	struct ipmi_addr addr;	struct kernel_ipmi_msg msg; 	if (req->addr_len > sizeof(struct ipmi_addr))		return -EINVAL; 	if (copy_from_user(&addr, req->addr, req->addr_len))		return -EFAULT; 	msg.netfn = req->msg.netfn;	msg.cmd = req->msg.cmd;	msg.data_len = req->msg.data_len;	msg.data = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);	if (!msg.data)		return -ENOMEM; 	/* From here out we cannot return, we must jump to "out" for	   error exits to free msgdata. */ 	rv = ipmi_validate_addr(&addr, req->addr_len);	if (rv)		goto out; 	if (req->msg.data != NULL) {		if (req->msg.data_len > IPMI_MAX_MSG_LENGTH) {			rv = -EMSGSIZE;			goto out;		} 		if (copy_from_user(msg.data,				   req->msg.data,				   req->msg.data_len)) {			rv = -EFAULT;			goto out;		}	} else {		msg.data_len = 0;	} 	rv = ipmi_request_settime(user,				  &addr,				  req->msgid,				  &msg,				  NULL,				  0,				  retries,				  retry_time_ms); out:	kfree(msg.data);	return rv;} static int handle_recv(struct ipmi_file_private *priv,			bool trunc, struct ipmi_recv *rsp,			int (*copyout)(struct ipmi_recv *, void __user *),			void __user *to){	int              addr_len;	struct list_head *entry;	struct ipmi_recv_msg  *msg;	unsigned long    flags;	int rv = 0, rv2 = 0; 	/* We claim a mutex because we don't want two	   users getting something from the queue at a time.	   Since we have to release the spinlock before we can	   copy the data to the user, it's possible another	   user will grab something from the queue, too.  Then	   the messages might get out of order if something	   fails and the message gets put back onto the	   queue.  This mutex prevents that problem. */	mutex_lock(&priv->recv_mutex); 	/* Grab the message off the list. */	spin_lock_irqsave(&priv->recv_msg_lock, flags);	if (list_empty(&(priv->recv_msgs))) {		spin_unlock_irqrestore(&priv->recv_msg_lock, flags);		rv = -EAGAIN;		goto recv_err;	}	entry = priv->recv_msgs.next;	msg = list_entry(entry, struct ipmi_recv_msg, link);	list_del(entry);	spin_unlock_irqrestore(&priv->recv_msg_lock, flags); 	addr_len = ipmi_addr_length(msg->addr.addr_type);	if (rsp->addr_len < addr_len) {		rv = -EINVAL;		goto recv_putback_on_err;	} 	if (copy_to_user(rsp->addr, &msg->addr, addr_len)) {		rv = -EFAULT;		goto recv_putback_on_err;	}	rsp->addr_len = addr_len; 	rsp->recv_type = msg->recv_type;	rsp->msgid = msg->msgid;	rsp->msg.netfn = msg->msg.netfn;	rsp->msg.cmd = msg->msg.cmd; 	if (msg->msg.data_len > 0) {		if (rsp->msg.data_len < msg->msg.data_len) {			if (trunc) {				rv2 = -EMSGSIZE;				msg->msg.data_len = rsp->msg.data_len;			} else {				rv = -EMSGSIZE;				goto recv_putback_on_err;			}		} 		if (copy_to_user(rsp->msg.data,				 msg->msg.data,				 msg->msg.data_len)) {			rv = -EFAULT;			goto recv_putback_on_err;		}		rsp->msg.data_len = msg->msg.data_len;	} else {		rsp->msg.data_len = 0;	} 	rv = copyout(rsp, to);	if (rv)		goto recv_putback_on_err; 	mutex_unlock(&priv->recv_mutex);	ipmi_free_recv_msg(msg);	return rv2; recv_putback_on_err:	/* If we got an error, put the message back onto	   the head of the queue. */	spin_lock_irqsave(&priv->recv_msg_lock, flags);	list_add(entry, &priv->recv_msgs);	spin_unlock_irqrestore(&priv->recv_msg_lock, flags);recv_err:	mutex_unlock(&priv->recv_mutex);	return rv;} static int copyout_recv(struct ipmi_recv *rsp, void __user *to){	return copy_to_user(to, rsp, sizeof(struct ipmi_recv)) ? -EFAULT : 0;} static long ipmi_ioctl(struct file   *file,		       unsigned int  cmd,		       unsigned long data){	int                      rv = -EINVAL;	struct ipmi_file_private *priv = file->private_data;	void __user *arg = (void __user *)data; 	switch (cmd) 	{	case IPMICTL_SEND_COMMAND:	{		struct ipmi_req req;		int retries;		unsigned int retry_time_ms; 		if (copy_from_user(&req, arg, sizeof(req))) {			rv = -EFAULT;			break;		} 		mutex_lock(&priv->recv_mutex);		retries = priv->default_retries;		retry_time_ms = priv->default_retry_time_ms;		mutex_unlock(&priv->recv_mutex); 		rv = handle_send_req(priv->user, &req, retries, retry_time_ms);		break;	} 	case IPMICTL_SEND_COMMAND_SETTIME:	{		struct ipmi_req_settime req; 		if (copy_from_user(&req, arg, sizeof(req))) {			rv = -EFAULT;			break;		} 		rv = handle_send_req(priv->user,				     &req.req,				     req.retries,				     req.retry_time_ms);		break;	} 	case IPMICTL_RECEIVE_MSG:	case IPMICTL_RECEIVE_MSG_TRUNC:	{		struct ipmi_recv      rsp; 		if (copy_from_user(&rsp, arg, sizeof(rsp)))			rv = -EFAULT;		else			rv = handle_recv(priv, cmd == IPMICTL_RECEIVE_MSG_TRUNC,					 &rsp, copyout_recv, arg);		break;	} 	case IPMICTL_REGISTER_FOR_CMD:	{		struct ipmi_cmdspec val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		rv = ipmi_register_for_cmd(priv->user, val.netfn, val.cmd,					   IPMI_CHAN_ALL);		break;	} 	case IPMICTL_UNREGISTER_FOR_CMD:	{		struct ipmi_cmdspec   val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		rv = ipmi_unregister_for_cmd(priv->user, val.netfn, val.cmd,					     IPMI_CHAN_ALL);		break;	} 	case IPMICTL_REGISTER_FOR_CMD_CHANS:	{		struct ipmi_cmdspec_chans val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		rv = ipmi_register_for_cmd(priv->user, val.netfn, val.cmd,					   val.chans);		break;	} 	case IPMICTL_UNREGISTER_FOR_CMD_CHANS:	{		struct ipmi_cmdspec_chans val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		rv = ipmi_unregister_for_cmd(priv->user, val.netfn, val.cmd,					     val.chans);		break;	} 	case IPMICTL_SET_GETS_EVENTS_CMD:	{		int val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		rv = ipmi_set_gets_events(priv->user, val);		break;	} 	/* The next four are legacy, not per-channel. */	case IPMICTL_SET_MY_ADDRESS_CMD:	{		unsigned int val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		rv = ipmi_set_my_address(priv->user, 0, val);		break;	} 	case IPMICTL_GET_MY_ADDRESS_CMD:	{		unsigned int  val;		unsigned char rval; 		rv = ipmi_get_my_address(priv->user, 0, &rval);		if (rv)			break; 		val = rval; 		if (copy_to_user(arg, &val, sizeof(val))) {			rv = -EFAULT;			break;		}		break;	} 	case IPMICTL_SET_MY_LUN_CMD:	{		unsigned int val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		rv = ipmi_set_my_LUN(priv->user, 0, val);		break;	} 	case IPMICTL_GET_MY_LUN_CMD:	{		unsigned int  val;		unsigned char rval; 		rv = ipmi_get_my_LUN(priv->user, 0, &rval);		if (rv)			break; 		val = rval; 		if (copy_to_user(arg, &val, sizeof(val))) {			rv = -EFAULT;			break;		}		break;	} 	case IPMICTL_SET_MY_CHANNEL_ADDRESS_CMD:	{		struct ipmi_channel_lun_address_set val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;		} 		return ipmi_set_my_address(priv->user, val.channel, val.value);	} 	case IPMICTL_GET_MY_CHANNEL_ADDRESS_CMD:	{		struct ipmi_channel_lun_address_set val; 		if (copy_from_user(&val, arg, sizeof(val))) {			rv = -EFAULT;			break;

Lines 1–500 of 907.