xref: /openbmc/linux/drivers/misc/ibmasm/command.c (revision dd00cc486ab1c17049a535413d1751ef3482141c)
11da177e4SLinus Torvalds 
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * IBM ASM Service Processor Device Driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  * (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  * GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Copyright (C) IBM Corporation, 2004
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Author: Max Asb�ck <amax@us.ibm.com>
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include "ibmasm.h"
2688187605SMax Asbock #include "lowlevel.h"
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds static void exec_next_command(struct service_processor *sp);
291da177e4SLinus Torvalds static void free_command(struct kobject *kobj);
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds static struct kobj_type ibmasm_cmd_kobj_type = {
321da177e4SLinus Torvalds 	.release = free_command,
331da177e4SLinus Torvalds };
341da177e4SLinus Torvalds 
3588187605SMax Asbock static atomic_t command_count = ATOMIC_INIT(0);
361da177e4SLinus Torvalds 
3788187605SMax Asbock struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size)
381da177e4SLinus Torvalds {
391da177e4SLinus Torvalds 	struct command *cmd;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE)
421da177e4SLinus Torvalds 		return NULL;
431da177e4SLinus Torvalds 
44*dd00cc48SYoann Padioleau 	cmd = kzalloc(sizeof(struct command), GFP_KERNEL);
451da177e4SLinus Torvalds 	if (cmd == NULL)
461da177e4SLinus Torvalds 		return NULL;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 
49*dd00cc48SYoann Padioleau 	cmd->buffer = kzalloc(buffer_size, GFP_KERNEL);
501da177e4SLinus Torvalds 	if (cmd->buffer == NULL) {
511da177e4SLinus Torvalds 		kfree(cmd);
521da177e4SLinus Torvalds 		return NULL;
531da177e4SLinus Torvalds 	}
541da177e4SLinus Torvalds 	cmd->buffer_size = buffer_size;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	kobject_init(&cmd->kobj);
571da177e4SLinus Torvalds 	cmd->kobj.ktype = &ibmasm_cmd_kobj_type;
5888187605SMax Asbock 	cmd->lock = &sp->lock;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	cmd->status = IBMASM_CMD_PENDING;
611da177e4SLinus Torvalds 	init_waitqueue_head(&cmd->wait);
621da177e4SLinus Torvalds 	INIT_LIST_HEAD(&cmd->queue_node);
631da177e4SLinus Torvalds 
6488187605SMax Asbock 	atomic_inc(&command_count);
6588187605SMax Asbock 	dbg("command count: %d\n", atomic_read(&command_count));
6688187605SMax Asbock 
671da177e4SLinus Torvalds 	return cmd;
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static void free_command(struct kobject *kobj)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct command *cmd = to_command(kobj);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	list_del(&cmd->queue_node);
7588187605SMax Asbock 	atomic_dec(&command_count);
7688187605SMax Asbock 	dbg("command count: %d\n", atomic_read(&command_count));
771da177e4SLinus Torvalds 	kfree(cmd->buffer);
781da177e4SLinus Torvalds 	kfree(cmd);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static void enqueue_command(struct service_processor *sp, struct command *cmd)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	list_add_tail(&cmd->queue_node, &sp->command_queue);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static struct command *dequeue_command(struct service_processor *sp)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	struct command *cmd;
891da177e4SLinus Torvalds 	struct list_head *next;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	if (list_empty(&sp->command_queue))
921da177e4SLinus Torvalds 		return NULL;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	next = sp->command_queue.next;
951da177e4SLinus Torvalds 	list_del_init(next);
961da177e4SLinus Torvalds 	cmd = list_entry(next, struct command, queue_node);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	return cmd;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds static inline void do_exec_command(struct service_processor *sp)
1021da177e4SLinus Torvalds {
10388187605SMax Asbock 	char tsbuf[32];
10488187605SMax Asbock 
10588187605SMax Asbock 	dbg("%s:%d at %s\n", __FUNCTION__, __LINE__, get_timestamp(tsbuf));
10688187605SMax Asbock 
1071da177e4SLinus Torvalds 	if (ibmasm_send_i2o_message(sp)) {
1081da177e4SLinus Torvalds 		sp->current_command->status = IBMASM_CMD_FAILED;
10988187605SMax Asbock 		wake_up(&sp->current_command->wait);
11088187605SMax Asbock 		command_put(sp->current_command);
1111da177e4SLinus Torvalds 		exec_next_command(sp);
1121da177e4SLinus Torvalds 	}
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /**
1161da177e4SLinus Torvalds  * exec_command
1171da177e4SLinus Torvalds  * send a command to a service processor
1181da177e4SLinus Torvalds  * Commands are executed sequentially. One command (sp->current_command)
1191da177e4SLinus Torvalds  * is sent to the service processor. Once the interrupt handler gets a
1201da177e4SLinus Torvalds  * message of type command_response, the message is copied into
1211da177e4SLinus Torvalds  * the current commands buffer,
1221da177e4SLinus Torvalds  */
1231da177e4SLinus Torvalds void ibmasm_exec_command(struct service_processor *sp, struct command *cmd)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	unsigned long flags;
12688187605SMax Asbock 	char tsbuf[32];
12788187605SMax Asbock 
12888187605SMax Asbock 	dbg("%s:%d at %s\n", __FUNCTION__, __LINE__, get_timestamp(tsbuf));
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	if (!sp->current_command) {
1331da177e4SLinus Torvalds 		sp->current_command = cmd;
13488187605SMax Asbock 		command_get(sp->current_command);
1351da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sp->lock, flags);
1361da177e4SLinus Torvalds 		do_exec_command(sp);
1371da177e4SLinus Torvalds 	} else {
1381da177e4SLinus Torvalds 		enqueue_command(sp, cmd);
1391da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sp->lock, flags);
1401da177e4SLinus Torvalds 	}
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static void exec_next_command(struct service_processor *sp)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	unsigned long flags;
14688187605SMax Asbock 	char tsbuf[32];
1471da177e4SLinus Torvalds 
14888187605SMax Asbock 	dbg("%s:%d at %s\n", __FUNCTION__, __LINE__, get_timestamp(tsbuf));
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	spin_lock_irqsave(&sp->lock, flags);
1511da177e4SLinus Torvalds 	sp->current_command = dequeue_command(sp);
1521da177e4SLinus Torvalds 	if (sp->current_command) {
1531da177e4SLinus Torvalds 		command_get(sp->current_command);
1541da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sp->lock, flags);
1551da177e4SLinus Torvalds 		do_exec_command(sp);
1561da177e4SLinus Torvalds 	} else {
1571da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sp->lock, flags);
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /**
1621da177e4SLinus Torvalds  * Sleep until a command has failed or a response has been received
1631da177e4SLinus Torvalds  * and the command status been updated by the interrupt handler.
1641da177e4SLinus Torvalds  * (see receive_response).
1651da177e4SLinus Torvalds  */
1661da177e4SLinus Torvalds void ibmasm_wait_for_response(struct command *cmd, int timeout)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	wait_event_interruptible_timeout(cmd->wait,
1691da177e4SLinus Torvalds 				cmd->status == IBMASM_CMD_COMPLETE ||
1701da177e4SLinus Torvalds 				cmd->status == IBMASM_CMD_FAILED,
1711da177e4SLinus Torvalds 				timeout * HZ);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds /**
1751da177e4SLinus Torvalds  * receive_command_response
1761da177e4SLinus Torvalds  * called by the interrupt handler when a dot command of type command_response
1771da177e4SLinus Torvalds  * was received.
1781da177e4SLinus Torvalds  */
1791da177e4SLinus Torvalds void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	struct command *cmd = sp->current_command;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	if (!sp->current_command)
1841da177e4SLinus Torvalds 		return;
1851da177e4SLinus Torvalds 
18688187605SMax Asbock 	memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size));
1871da177e4SLinus Torvalds 	cmd->status = IBMASM_CMD_COMPLETE;
18888187605SMax Asbock 	wake_up(&sp->current_command->wait);
18988187605SMax Asbock 	command_put(sp->current_command);
1901da177e4SLinus Torvalds 	exec_next_command(sp);
1911da177e4SLinus Torvalds }
192