[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / char / ipmi / ipmi_kcs_sm.c
blob48cce24329bea516b62a0f3578ea162a48b73556
1 /*
2 * ipmi_kcs_sm.c
4 * State machine for handling IPMI KCS interfaces.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
8 * source@mvista.com
10 * Copyright 2002 MontaVista Software Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 * This state machine is taken from the state machine in the IPMI spec,
36 * pretty much verbatim. If you have questions about the states, see
37 * that document.
40 #include <linux/kernel.h> /* For printk. */
41 #include <linux/string.h>
42 #include <linux/ipmi_msgdefs.h> /* for completion codes */
43 #include "ipmi_si_sm.h"
45 #define IPMI_KCS_VERSION "v33"
47 /* Set this if you want a printout of why the state machine was hosed
48 when it gets hosed. */
49 #define DEBUG_HOSED_REASON
51 /* Print the state machine state on entry every time. */
52 #undef DEBUG_STATE
54 /* The states the KCS driver may be in. */
55 enum kcs_states {
56 KCS_IDLE, /* The KCS interface is currently
57 doing nothing. */
58 KCS_START_OP, /* We are starting an operation. The
59 data is in the output buffer, but
60 nothing has been done to the
61 interface yet. This was added to
62 the state machine in the spec to
63 wait for the initial IBF. */
64 KCS_WAIT_WRITE_START, /* We have written a write cmd to the
65 interface. */
66 KCS_WAIT_WRITE, /* We are writing bytes to the
67 interface. */
68 KCS_WAIT_WRITE_END, /* We have written the write end cmd
69 to the interface, and still need to
70 write the last byte. */
71 KCS_WAIT_READ, /* We are waiting to read data from
72 the interface. */
73 KCS_ERROR0, /* State to transition to the error
74 handler, this was added to the
75 state machine in the spec to be
76 sure IBF was there. */
77 KCS_ERROR1, /* First stage error handler, wait for
78 the interface to respond. */
79 KCS_ERROR2, /* The abort cmd has been written,
80 wait for the interface to
81 respond. */
82 KCS_ERROR3, /* We wrote some data to the
83 interface, wait for it to switch to
84 read mode. */
85 KCS_HOSED /* The hardware failed to follow the
86 state machine. */
89 #define MAX_KCS_READ_SIZE 80
90 #define MAX_KCS_WRITE_SIZE 80
92 /* Timeouts in microseconds. */
93 #define IBF_RETRY_TIMEOUT 1000000
94 #define OBF_RETRY_TIMEOUT 1000000
95 #define MAX_ERROR_RETRIES 10
97 struct si_sm_data
99 enum kcs_states state;
100 struct si_sm_io *io;
101 unsigned char write_data[MAX_KCS_WRITE_SIZE];
102 int write_pos;
103 int write_count;
104 int orig_write_count;
105 unsigned char read_data[MAX_KCS_READ_SIZE];
106 int read_pos;
107 int truncated;
109 unsigned int error_retries;
110 long ibf_timeout;
111 long obf_timeout;
114 static unsigned int init_kcs_data(struct si_sm_data *kcs,
115 struct si_sm_io *io)
117 kcs->state = KCS_IDLE;
118 kcs->io = io;
119 kcs->write_pos = 0;
120 kcs->write_count = 0;
121 kcs->orig_write_count = 0;
122 kcs->read_pos = 0;
123 kcs->error_retries = 0;
124 kcs->truncated = 0;
125 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
126 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
128 /* Reserve 2 I/O bytes. */
129 return 2;
132 static inline unsigned char read_status(struct si_sm_data *kcs)
134 return kcs->io->inputb(kcs->io, 1);
137 static inline unsigned char read_data(struct si_sm_data *kcs)
139 return kcs->io->inputb(kcs->io, 0);
142 static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
144 kcs->io->outputb(kcs->io, 1, data);
147 static inline void write_data(struct si_sm_data *kcs, unsigned char data)
149 kcs->io->outputb(kcs->io, 0, data);
152 /* Control codes. */
153 #define KCS_GET_STATUS_ABORT 0x60
154 #define KCS_WRITE_START 0x61
155 #define KCS_WRITE_END 0x62
156 #define KCS_READ_BYTE 0x68
158 /* Status bits. */
159 #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
160 #define KCS_IDLE_STATE 0
161 #define KCS_READ_STATE 1
162 #define KCS_WRITE_STATE 2
163 #define KCS_ERROR_STATE 3
164 #define GET_STATUS_ATN(status) ((status) & 0x04)
165 #define GET_STATUS_IBF(status) ((status) & 0x02)
166 #define GET_STATUS_OBF(status) ((status) & 0x01)
169 static inline void write_next_byte(struct si_sm_data *kcs)
171 write_data(kcs, kcs->write_data[kcs->write_pos]);
172 (kcs->write_pos)++;
173 (kcs->write_count)--;
176 static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
178 (kcs->error_retries)++;
179 if (kcs->error_retries > MAX_ERROR_RETRIES) {
180 #ifdef DEBUG_HOSED_REASON
181 printk("ipmi_kcs_sm: kcs hosed: %s\n", reason);
182 #endif
183 kcs->state = KCS_HOSED;
184 } else {
185 kcs->state = KCS_ERROR0;
189 static inline void read_next_byte(struct si_sm_data *kcs)
191 if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
192 /* Throw the data away and mark it truncated. */
193 read_data(kcs);
194 kcs->truncated = 1;
195 } else {
196 kcs->read_data[kcs->read_pos] = read_data(kcs);
197 (kcs->read_pos)++;
199 write_data(kcs, KCS_READ_BYTE);
202 static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
203 long time)
205 if (GET_STATUS_IBF(status)) {
206 kcs->ibf_timeout -= time;
207 if (kcs->ibf_timeout < 0) {
208 start_error_recovery(kcs, "IBF not ready in time");
209 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
210 return 1;
212 return 0;
214 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
215 return 1;
218 static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
219 long time)
221 if (! GET_STATUS_OBF(status)) {
222 kcs->obf_timeout -= time;
223 if (kcs->obf_timeout < 0) {
224 start_error_recovery(kcs, "OBF not ready in time");
225 return 1;
227 return 0;
229 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
230 return 1;
233 static void clear_obf(struct si_sm_data *kcs, unsigned char status)
235 if (GET_STATUS_OBF(status))
236 read_data(kcs);
239 static void restart_kcs_transaction(struct si_sm_data *kcs)
241 kcs->write_count = kcs->orig_write_count;
242 kcs->write_pos = 0;
243 kcs->read_pos = 0;
244 kcs->state = KCS_WAIT_WRITE_START;
245 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
246 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
247 write_cmd(kcs, KCS_WRITE_START);
250 static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
251 unsigned int size)
253 if ((size < 2) || (size > MAX_KCS_WRITE_SIZE)) {
254 return -1;
257 if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) {
258 return -2;
261 kcs->error_retries = 0;
262 memcpy(kcs->write_data, data, size);
263 kcs->write_count = size;
264 kcs->orig_write_count = size;
265 kcs->write_pos = 0;
266 kcs->read_pos = 0;
267 kcs->state = KCS_START_OP;
268 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
269 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
270 return 0;
273 static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
274 unsigned int length)
276 if (length < kcs->read_pos) {
277 kcs->read_pos = length;
278 kcs->truncated = 1;
281 memcpy(data, kcs->read_data, kcs->read_pos);
283 if ((length >= 3) && (kcs->read_pos < 3)) {
284 /* Guarantee that we return at least 3 bytes, with an
285 error in the third byte if it is too short. */
286 data[2] = IPMI_ERR_UNSPECIFIED;
287 kcs->read_pos = 3;
289 if (kcs->truncated) {
290 /* Report a truncated error. We might overwrite
291 another error, but that's too bad, the user needs
292 to know it was truncated. */
293 data[2] = IPMI_ERR_MSG_TRUNCATED;
294 kcs->truncated = 0;
297 return kcs->read_pos;
300 /* This implements the state machine defined in the IPMI manual, see
301 that for details on how this works. Divide that flowchart into
302 sections delimited by "Wait for IBF" and this will become clear. */
303 static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
305 unsigned char status;
306 unsigned char state;
308 status = read_status(kcs);
310 #ifdef DEBUG_STATE
311 printk(" State = %d, %x\n", kcs->state, status);
312 #endif
313 /* All states wait for ibf, so just do it here. */
314 if (!check_ibf(kcs, status, time))
315 return SI_SM_CALL_WITH_DELAY;
317 /* Just about everything looks at the KCS state, so grab that, too. */
318 state = GET_STATUS_STATE(status);
320 switch (kcs->state) {
321 case KCS_IDLE:
322 /* If there's and interrupt source, turn it off. */
323 clear_obf(kcs, status);
325 if (GET_STATUS_ATN(status))
326 return SI_SM_ATTN;
327 else
328 return SI_SM_IDLE;
330 case KCS_START_OP:
331 if (state != KCS_IDLE) {
332 start_error_recovery(kcs,
333 "State machine not idle at start");
334 break;
337 clear_obf(kcs, status);
338 write_cmd(kcs, KCS_WRITE_START);
339 kcs->state = KCS_WAIT_WRITE_START;
340 break;
342 case KCS_WAIT_WRITE_START:
343 if (state != KCS_WRITE_STATE) {
344 start_error_recovery(
345 kcs,
346 "Not in write state at write start");
347 break;
349 read_data(kcs);
350 if (kcs->write_count == 1) {
351 write_cmd(kcs, KCS_WRITE_END);
352 kcs->state = KCS_WAIT_WRITE_END;
353 } else {
354 write_next_byte(kcs);
355 kcs->state = KCS_WAIT_WRITE;
357 break;
359 case KCS_WAIT_WRITE:
360 if (state != KCS_WRITE_STATE) {
361 start_error_recovery(kcs,
362 "Not in write state for write");
363 break;
365 clear_obf(kcs, status);
366 if (kcs->write_count == 1) {
367 write_cmd(kcs, KCS_WRITE_END);
368 kcs->state = KCS_WAIT_WRITE_END;
369 } else {
370 write_next_byte(kcs);
372 break;
374 case KCS_WAIT_WRITE_END:
375 if (state != KCS_WRITE_STATE) {
376 start_error_recovery(kcs,
377 "Not in write state for write end");
378 break;
380 clear_obf(kcs, status);
381 write_next_byte(kcs);
382 kcs->state = KCS_WAIT_READ;
383 break;
385 case KCS_WAIT_READ:
386 if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
387 start_error_recovery(
388 kcs,
389 "Not in read or idle in read state");
390 break;
393 if (state == KCS_READ_STATE) {
394 if (! check_obf(kcs, status, time))
395 return SI_SM_CALL_WITH_DELAY;
396 read_next_byte(kcs);
397 } else {
398 /* We don't implement this exactly like the state
399 machine in the spec. Some broken hardware
400 does not write the final dummy byte to the
401 read register. Thus obf will never go high
402 here. We just go straight to idle, and we
403 handle clearing out obf in idle state if it
404 happens to come in. */
405 clear_obf(kcs, status);
406 kcs->orig_write_count = 0;
407 kcs->state = KCS_IDLE;
408 return SI_SM_TRANSACTION_COMPLETE;
410 break;
412 case KCS_ERROR0:
413 clear_obf(kcs, status);
414 write_cmd(kcs, KCS_GET_STATUS_ABORT);
415 kcs->state = KCS_ERROR1;
416 break;
418 case KCS_ERROR1:
419 clear_obf(kcs, status);
420 write_data(kcs, 0);
421 kcs->state = KCS_ERROR2;
422 break;
424 case KCS_ERROR2:
425 if (state != KCS_READ_STATE) {
426 start_error_recovery(kcs,
427 "Not in read state for error2");
428 break;
430 if (! check_obf(kcs, status, time))
431 return SI_SM_CALL_WITH_DELAY;
433 clear_obf(kcs, status);
434 write_data(kcs, KCS_READ_BYTE);
435 kcs->state = KCS_ERROR3;
436 break;
438 case KCS_ERROR3:
439 if (state != KCS_IDLE_STATE) {
440 start_error_recovery(kcs,
441 "Not in idle state for error3");
442 break;
445 if (! check_obf(kcs, status, time))
446 return SI_SM_CALL_WITH_DELAY;
448 clear_obf(kcs, status);
449 if (kcs->orig_write_count) {
450 restart_kcs_transaction(kcs);
451 } else {
452 kcs->state = KCS_IDLE;
453 return SI_SM_TRANSACTION_COMPLETE;
455 break;
457 case KCS_HOSED:
458 break;
461 if (kcs->state == KCS_HOSED) {
462 init_kcs_data(kcs, kcs->io);
463 return SI_SM_HOSED;
466 return SI_SM_CALL_WITHOUT_DELAY;
469 static int kcs_size(void)
471 return sizeof(struct si_sm_data);
474 static int kcs_detect(struct si_sm_data *kcs)
476 /* It's impossible for the KCS status register to be all 1's,
477 (assuming a properly functioning, self-initialized BMC)
478 but that's what you get from reading a bogus address, so we
479 test that first. */
480 if (read_status(kcs) == 0xff)
481 return 1;
483 return 0;
486 static void kcs_cleanup(struct si_sm_data *kcs)
490 struct si_sm_handlers kcs_smi_handlers =
492 .version = IPMI_KCS_VERSION,
493 .init_data = init_kcs_data,
494 .start_transaction = start_kcs_transaction,
495 .get_result = get_kcs_result,
496 .event = kcs_event,
497 .detect = kcs_detect,
498 .cleanup = kcs_cleanup,
499 .size = kcs_size,