blob: e2031739aa29fda4a188d4ac35101bc9f7439ee0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * IBM ASM Service Processor Device Driver
4 *
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.
9 *
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.
14 *
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.
18 *
19 * Copyright (C) IBM Corporation, 2004
20 *
Dmitry Torokhov3110dc72007-07-17 04:03:58 -070021 * Author: Max Asböck <amax@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
23 */
24
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040025#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "ibmasm.h"
Max Asbock88187602005-06-21 17:16:36 -070027#include "lowlevel.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static void exec_next_command(struct service_processor *sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Max Asbock88187602005-06-21 17:16:36 -070031static atomic_t command_count = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Max Asbock88187602005-06-21 17:16:36 -070033struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct command *cmd;
36
37 if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE)
38 return NULL;
39
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070040 cmd = kzalloc(sizeof(struct command), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (cmd == NULL)
42 return NULL;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070045 cmd->buffer = kzalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if (cmd->buffer == NULL) {
47 kfree(cmd);
48 return NULL;
49 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 cmd->buffer_size = buffer_size;
51
Greg Kroah-Hartmana0451712007-12-03 21:16:20 -070052 kref_init(&cmd->kref);
Max Asbock88187602005-06-21 17:16:36 -070053 cmd->lock = &sp->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 cmd->status = IBMASM_CMD_PENDING;
56 init_waitqueue_head(&cmd->wait);
57 INIT_LIST_HEAD(&cmd->queue_node);
58
Max Asbock88187602005-06-21 17:16:36 -070059 atomic_inc(&command_count);
60 dbg("command count: %d\n", atomic_read(&command_count));
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return cmd;
63}
64
Greg Kroah-Hartmana0451712007-12-03 21:16:20 -070065void ibmasm_free_command(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Greg Kroah-Hartmana0451712007-12-03 21:16:20 -070067 struct command *cmd = to_command(kref);
Dmitry Torokhov3110dc72007-07-17 04:03:58 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 list_del(&cmd->queue_node);
Max Asbock88187602005-06-21 17:16:36 -070070 atomic_dec(&command_count);
71 dbg("command count: %d\n", atomic_read(&command_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 kfree(cmd->buffer);
73 kfree(cmd);
74}
75
76static void enqueue_command(struct service_processor *sp, struct command *cmd)
77{
78 list_add_tail(&cmd->queue_node, &sp->command_queue);
79}
80
81static struct command *dequeue_command(struct service_processor *sp)
82{
83 struct command *cmd;
84 struct list_head *next;
85
86 if (list_empty(&sp->command_queue))
87 return NULL;
88
89 next = sp->command_queue.next;
90 list_del_init(next);
91 cmd = list_entry(next, struct command, queue_node);
92
93 return cmd;
94}
95
96static inline void do_exec_command(struct service_processor *sp)
97{
Max Asbock88187602005-06-21 17:16:36 -070098 char tsbuf[32];
99
Harvey Harrison6e574192008-04-29 00:59:20 -0700100 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
Max Asbock88187602005-06-21 17:16:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (ibmasm_send_i2o_message(sp)) {
103 sp->current_command->status = IBMASM_CMD_FAILED;
Max Asbock88187602005-06-21 17:16:36 -0700104 wake_up(&sp->current_command->wait);
105 command_put(sp->current_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 exec_next_command(sp);
107 }
108}
Dmitry Torokhov3110dc72007-07-17 04:03:58 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/**
111 * exec_command
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
Dmitry Torokhov3110dc72007-07-17 04:03:58 -0700116 * the current commands buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118void ibmasm_exec_command(struct service_processor *sp, struct command *cmd)
119{
120 unsigned long flags;
Max Asbock88187602005-06-21 17:16:36 -0700121 char tsbuf[32];
122
Harvey Harrison6e574192008-04-29 00:59:20 -0700123 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 spin_lock_irqsave(&sp->lock, flags);
126
127 if (!sp->current_command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 sp->current_command = cmd;
Max Asbock88187602005-06-21 17:16:36 -0700129 command_get(sp->current_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 spin_unlock_irqrestore(&sp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 do_exec_command(sp);
132 } else {
133 enqueue_command(sp, cmd);
134 spin_unlock_irqrestore(&sp->lock, flags);
135 }
136}
137
138static void exec_next_command(struct service_processor *sp)
139{
140 unsigned long flags;
Max Asbock88187602005-06-21 17:16:36 -0700141 char tsbuf[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Harvey Harrison6e574192008-04-29 00:59:20 -0700143 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
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);
150 do_exec_command(sp);
151 } else {
152 spin_unlock_irqrestore(&sp->lock, flags);
153 }
154}
155
Dmitry Torokhov3110dc72007-07-17 04:03:58 -0700156/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * 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).
160 */
161void ibmasm_wait_for_response(struct command *cmd, int timeout)
162{
163 wait_event_interruptible_timeout(cmd->wait,
164 cmd->status == IBMASM_CMD_COMPLETE ||
165 cmd->status == IBMASM_CMD_FAILED,
166 timeout * HZ);
167}
168
169/**
170 * receive_command_response
171 * called by the interrupt handler when a dot command of type command_response
172 * was received.
173 */
174void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size)
175{
176 struct command *cmd = sp->current_command;
177
Dmitry Torokhov3110dc72007-07-17 04:03:58 -0700178 if (!sp->current_command)
179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Max Asbock88187602005-06-21 17:16:36 -0700181 memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 cmd->status = IBMASM_CMD_COMPLETE;
Max Asbock88187602005-06-21 17:16:36 -0700183 wake_up(&sp->current_command->wait);
184 command_put(sp->current_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 exec_next_command(sp);
186}