Linux-2.6.12-rc2
[linux-2.6/kvm.git] / drivers / misc / ibmasm / command.c
blob245b0058381ddd9e2494e4eb2550e215f021abf5
2 /*
3 * IBM ASM Service Processor Device Driver
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) IBM Corporation, 2004
21 * Author: Max Asböck <amax@us.ibm.com>
25 #include "ibmasm.h"
27 static void exec_next_command(struct service_processor *sp);
28 static void free_command(struct kobject *kobj);
30 static struct kobj_type ibmasm_cmd_kobj_type = {
31 .release = free_command,
35 struct command *ibmasm_new_command(size_t buffer_size)
37 struct command *cmd;
39 if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE)
40 return NULL;
42 cmd = kmalloc(sizeof(struct command), GFP_KERNEL);
43 if (cmd == NULL)
44 return NULL;
46 memset(cmd, 0, sizeof(*cmd));
48 cmd->buffer = kmalloc(buffer_size, GFP_KERNEL);
49 if (cmd->buffer == NULL) {
50 kfree(cmd);
51 return NULL;
53 memset(cmd->buffer, 0, buffer_size);
54 cmd->buffer_size = buffer_size;
56 kobject_init(&cmd->kobj);
57 cmd->kobj.ktype = &ibmasm_cmd_kobj_type;
59 cmd->status = IBMASM_CMD_PENDING;
60 init_waitqueue_head(&cmd->wait);
61 INIT_LIST_HEAD(&cmd->queue_node);
63 return cmd;
66 static void free_command(struct kobject *kobj)
68 struct command *cmd = to_command(kobj);
70 list_del(&cmd->queue_node);
71 kfree(cmd->buffer);
72 kfree(cmd);
75 static void enqueue_command(struct service_processor *sp, struct command *cmd)
77 list_add_tail(&cmd->queue_node, &sp->command_queue);
80 static struct command *dequeue_command(struct service_processor *sp)
82 struct command *cmd;
83 struct list_head *next;
85 if (list_empty(&sp->command_queue))
86 return NULL;
88 next = sp->command_queue.next;
89 list_del_init(next);
90 cmd = list_entry(next, struct command, queue_node);
92 return cmd;
95 static inline void do_exec_command(struct service_processor *sp)
97 if (ibmasm_send_i2o_message(sp)) {
98 sp->current_command->status = IBMASM_CMD_FAILED;
99 exec_next_command(sp);
104 * exec_command
105 * send a command to a service processor
106 * Commands are executed sequentially. One command (sp->current_command)
107 * is sent to the service processor. Once the interrupt handler gets a
108 * message of type command_response, the message is copied into
109 * the current commands buffer,
111 void ibmasm_exec_command(struct service_processor *sp, struct command *cmd)
113 unsigned long flags;
115 spin_lock_irqsave(&sp->lock, flags);
117 if (!sp->current_command) {
118 command_get(cmd);
119 sp->current_command = cmd;
120 spin_unlock_irqrestore(&sp->lock, flags);
122 do_exec_command(sp);
123 } else {
124 enqueue_command(sp, cmd);
125 spin_unlock_irqrestore(&sp->lock, flags);
129 static void exec_next_command(struct service_processor *sp)
131 unsigned long flags;
133 wake_up(&sp->current_command->wait);
134 command_put(sp->current_command);
136 spin_lock_irqsave(&sp->lock, flags);
137 sp->current_command = dequeue_command(sp);
138 if (sp->current_command) {
139 command_get(sp->current_command);
140 spin_unlock_irqrestore(&sp->lock, flags);
141 do_exec_command(sp);
142 } else {
143 spin_unlock_irqrestore(&sp->lock, flags);
147 /**
148 * Sleep until a command has failed or a response has been received
149 * and the command status been updated by the interrupt handler.
150 * (see receive_response).
152 void ibmasm_wait_for_response(struct command *cmd, int timeout)
154 wait_event_interruptible_timeout(cmd->wait,
155 cmd->status == IBMASM_CMD_COMPLETE ||
156 cmd->status == IBMASM_CMD_FAILED,
157 timeout * HZ);
161 * receive_command_response
162 * called by the interrupt handler when a dot command of type command_response
163 * was received.
165 void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size)
167 struct command *cmd = sp->current_command;
169 if (!sp->current_command)
170 return;
172 memcpy(cmd->buffer, response, min(size, cmd->buffer_size));
173 cmd->status = IBMASM_CMD_COMPLETE;
174 exec_next_command(sp);