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 <linux/sched.h>
29 static void exec_next_command(struct service_processor
*sp
);
31 static atomic_t command_count
= ATOMIC_INIT(0);
33 struct command
*ibmasm_new_command(struct service_processor
*sp
, size_t buffer_size
)
37 if (buffer_size
> IBMASM_CMD_MAX_BUFFER_SIZE
)
40 cmd
= kzalloc(sizeof(struct command
), GFP_KERNEL
);
45 cmd
->buffer
= kzalloc(buffer_size
, GFP_KERNEL
);
46 if (cmd
->buffer
== NULL
) {
50 cmd
->buffer_size
= buffer_size
;
52 kref_init(&cmd
->kref
);
53 cmd
->lock
= &sp
->lock
;
55 cmd
->status
= IBMASM_CMD_PENDING
;
56 init_waitqueue_head(&cmd
->wait
);
57 INIT_LIST_HEAD(&cmd
->queue_node
);
59 atomic_inc(&command_count
);
60 dbg("command count: %d\n", atomic_read(&command_count
));
65 void ibmasm_free_command(struct kref
*kref
)
67 struct command
*cmd
= to_command(kref
);
69 list_del(&cmd
->queue_node
);
70 atomic_dec(&command_count
);
71 dbg("command count: %d\n", atomic_read(&command_count
));
76 static void enqueue_command(struct service_processor
*sp
, struct command
*cmd
)
78 list_add_tail(&cmd
->queue_node
, &sp
->command_queue
);
81 static struct command
*dequeue_command(struct service_processor
*sp
)
84 struct list_head
*next
;
86 if (list_empty(&sp
->command_queue
))
89 next
= sp
->command_queue
.next
;
91 cmd
= list_entry(next
, struct command
, queue_node
);
96 static inline void do_exec_command(struct service_processor
*sp
)
100 dbg("%s:%d at %s\n", __func__
, __LINE__
, get_timestamp(tsbuf
));
102 if (ibmasm_send_i2o_message(sp
)) {
103 sp
->current_command
->status
= IBMASM_CMD_FAILED
;
104 wake_up(&sp
->current_command
->wait
);
105 command_put(sp
->current_command
);
106 exec_next_command(sp
);
112 * send a command to a service processor
113 * Commands are executed sequentially. One command (sp->current_command)
114 * is sent to the service processor. Once the interrupt handler gets a
115 * message of type command_response, the message is copied into
116 * the current commands buffer,
118 void ibmasm_exec_command(struct service_processor
*sp
, struct command
*cmd
)
123 dbg("%s:%d at %s\n", __func__
, __LINE__
, get_timestamp(tsbuf
));
125 spin_lock_irqsave(&sp
->lock
, flags
);
127 if (!sp
->current_command
) {
128 sp
->current_command
= cmd
;
129 command_get(sp
->current_command
);
130 spin_unlock_irqrestore(&sp
->lock
, flags
);
133 enqueue_command(sp
, cmd
);
134 spin_unlock_irqrestore(&sp
->lock
, flags
);
138 static void exec_next_command(struct service_processor
*sp
)
143 dbg("%s:%d at %s\n", __func__
, __LINE__
, get_timestamp(tsbuf
));
145 spin_lock_irqsave(&sp
->lock
, flags
);
146 sp
->current_command
= dequeue_command(sp
);
147 if (sp
->current_command
) {
148 command_get(sp
->current_command
);
149 spin_unlock_irqrestore(&sp
->lock
, flags
);
152 spin_unlock_irqrestore(&sp
->lock
, flags
);
157 * Sleep until a command has failed or a response has been received
158 * and the command status been updated by the interrupt handler.
159 * (see receive_response).
161 void ibmasm_wait_for_response(struct command
*cmd
, int timeout
)
163 wait_event_interruptible_timeout(cmd
->wait
,
164 cmd
->status
== IBMASM_CMD_COMPLETE
||
165 cmd
->status
== IBMASM_CMD_FAILED
,
170 * receive_command_response
171 * called by the interrupt handler when a dot command of type command_response
174 void ibmasm_receive_command_response(struct service_processor
*sp
, void *response
, size_t size
)
176 struct command
*cmd
= sp
->current_command
;
178 if (!sp
->current_command
)
181 memcpy_fromio(cmd
->buffer
, response
, min(size
, cmd
->buffer_size
));
182 cmd
->status
= IBMASM_CMD_COMPLETE
;
183 wake_up(&sp
->current_command
->wait
);
184 command_put(sp
->current_command
);
185 exec_next_command(sp
);