1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/sched.h>
26#include "ibmasm.h"
27#include "lowlevel.h"
28
29static void exec_next_command(struct service_processor *sp);
30
31static atomic_t command_count = ATOMIC_INIT(0);
32
33struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size)
34{
35 struct command *cmd;
36
37 if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE)
38 return NULL;
39
40 cmd = kzalloc(sizeof(struct command), GFP_KERNEL);
41 if (cmd == NULL)
42 return NULL;
43
44
45 cmd->buffer = kzalloc(buffer_size, GFP_KERNEL);
46 if (cmd->buffer == NULL) {
47 kfree(cmd);
48 return NULL;
49 }
50 cmd->buffer_size = buffer_size;
51
52 kref_init(&cmd->kref);
53 cmd->lock = &sp->lock;
54
55 cmd->status = IBMASM_CMD_PENDING;
56 init_waitqueue_head(&cmd->wait);
57 INIT_LIST_HEAD(&cmd->queue_node);
58
59 atomic_inc(&command_count);
60 dbg("command count: %d\n", atomic_read(&command_count));
61
62 return cmd;
63}
64
65void ibmasm_free_command(struct kref *kref)
66{
67 struct command *cmd = to_command(kref);
68
69 list_del(&cmd->queue_node);
70 atomic_dec(&command_count);
71 dbg("command count: %d\n", atomic_read(&command_count));
72 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{
98 char tsbuf[32];
99
100 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
101
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);
107 }
108}
109
110
111
112
113
114
115
116
117
118void ibmasm_exec_command(struct service_processor *sp, struct command *cmd)
119{
120 unsigned long flags;
121 char tsbuf[32];
122
123 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
124
125 spin_lock_irqsave(&sp->lock, flags);
126
127 if (!sp->current_command) {
128 sp->current_command = cmd;
129 command_get(sp->current_command);
130 spin_unlock_irqrestore(&sp->lock, flags);
131 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;
141 char tsbuf[32];
142
143 dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf));
144
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
156
157
158
159
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
171
172
173
174void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size)
175{
176 struct command *cmd = sp->current_command;
177
178 if (!sp->current_command)
179 return;
180
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);
186}
187