4 * State machine for handling IPMI KCS interfaces.
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@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
40 #include <linux/kernel.h> /* For printk. */
41 #include <linux/module.h>
42 #include <linux/moduleparam.h>
43 #include <linux/string.h>
44 #include <linux/ipmi_msgdefs.h> /* for completion codes */
45 #include "ipmi_si_sm.h"
47 /* kcs_debug is a bit-field
48 * KCS_DEBUG_ENABLE - turned on for now
49 * KCS_DEBUG_MSG - commands and their responses
50 * KCS_DEBUG_STATES - state machine
52 #define KCS_DEBUG_STATES 4
53 #define KCS_DEBUG_MSG 2
54 #define KCS_DEBUG_ENABLE 1
57 module_param(kcs_debug, int, 0644);
58 MODULE_PARM_DESC(kcs_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
60 /* The states the KCS driver may be in. */
62 KCS_IDLE, /* The KCS interface is currently
64 KCS_START_OP, /* We are starting an operation. The
65 data is in the output buffer, but
66 nothing has been done to the
67 interface yet. This was added to
68 the state machine in the spec to
69 wait for the initial IBF. */
70 KCS_WAIT_WRITE_START, /* We have written a write cmd to the
72 KCS_WAIT_WRITE, /* We are writing bytes to the
74 KCS_WAIT_WRITE_END, /* We have written the write end cmd
75 to the interface, and still need to
76 write the last byte. */
77 KCS_WAIT_READ, /* We are waiting to read data from
79 KCS_ERROR0, /* State to transition to the error
80 handler, this was added to the
81 state machine in the spec to be
82 sure IBF was there. */
83 KCS_ERROR1, /* First stage error handler, wait for
84 the interface to respond. */
85 KCS_ERROR2, /* The abort cmd has been written,
86 wait for the interface to
88 KCS_ERROR3, /* We wrote some data to the
89 interface, wait for it to switch to
91 KCS_HOSED /* The hardware failed to follow the
95 #define MAX_KCS_READ_SIZE 80
96 #define MAX_KCS_WRITE_SIZE 80
98 /* Timeouts in microseconds. */
99 #define IBF_RETRY_TIMEOUT 1000000
100 #define OBF_RETRY_TIMEOUT 1000000
101 #define MAX_ERROR_RETRIES 10
105 enum kcs_states state;
107 unsigned char write_data[MAX_KCS_WRITE_SIZE];
110 int orig_write_count;
111 unsigned char read_data[MAX_KCS_READ_SIZE];
115 unsigned int error_retries;
120 static unsigned int init_kcs_data(struct si_sm_data *kcs,
123 kcs->state = KCS_IDLE;
126 kcs->write_count = 0;
127 kcs->orig_write_count = 0;
129 kcs->error_retries = 0;
131 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
132 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
134 /* Reserve 2 I/O bytes. */
138 static inline unsigned char read_status(struct si_sm_data *kcs)
140 return kcs->io->inputb(kcs->io, 1);
143 static inline unsigned char read_data(struct si_sm_data *kcs)
145 return kcs->io->inputb(kcs->io, 0);
148 static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
150 kcs->io->outputb(kcs->io, 1, data);
153 static inline void write_data(struct si_sm_data *kcs, unsigned char data)
155 kcs->io->outputb(kcs->io, 0, data);
159 #define KCS_GET_STATUS_ABORT 0x60
160 #define KCS_WRITE_START 0x61
161 #define KCS_WRITE_END 0x62
162 #define KCS_READ_BYTE 0x68
165 #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
166 #define KCS_IDLE_STATE 0
167 #define KCS_READ_STATE 1
168 #define KCS_WRITE_STATE 2
169 #define KCS_ERROR_STATE 3
170 #define GET_STATUS_ATN(status) ((status) & 0x04)
171 #define GET_STATUS_IBF(status) ((status) & 0x02)
172 #define GET_STATUS_OBF(status) ((status) & 0x01)
175 static inline void write_next_byte(struct si_sm_data *kcs)
177 write_data(kcs, kcs->write_data[kcs->write_pos]);
179 (kcs->write_count)--;
182 static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
184 (kcs->error_retries)++;
185 if (kcs->error_retries > MAX_ERROR_RETRIES) {
186 if (kcs_debug & KCS_DEBUG_ENABLE)
187 printk(KERN_DEBUG "ipmi_kcs_sm: kcs hosed: %s\n", reason);
188 kcs->state = KCS_HOSED;
190 kcs->state = KCS_ERROR0;
194 static inline void read_next_byte(struct si_sm_data *kcs)
196 if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
197 /* Throw the data away and mark it truncated. */
201 kcs->read_data[kcs->read_pos] = read_data(kcs);
204 write_data(kcs, KCS_READ_BYTE);
207 static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
210 if (GET_STATUS_IBF(status)) {
211 kcs->ibf_timeout -= time;
212 if (kcs->ibf_timeout < 0) {
213 start_error_recovery(kcs, "IBF not ready in time");
214 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
219 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
223 static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
226 if (! GET_STATUS_OBF(status)) {
227 kcs->obf_timeout -= time;
228 if (kcs->obf_timeout < 0) {
229 start_error_recovery(kcs, "OBF not ready in time");
234 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
238 static void clear_obf(struct si_sm_data *kcs, unsigned char status)
240 if (GET_STATUS_OBF(status))
244 static void restart_kcs_transaction(struct si_sm_data *kcs)
246 kcs->write_count = kcs->orig_write_count;
249 kcs->state = KCS_WAIT_WRITE_START;
250 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
251 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
252 write_cmd(kcs, KCS_WRITE_START);
255 static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
260 if ((size < 2) || (size > MAX_KCS_WRITE_SIZE)) {
263 if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) {
266 if (kcs_debug & KCS_DEBUG_MSG) {
267 printk(KERN_DEBUG "start_kcs_transaction -");
268 for (i = 0; i < size; i ++) {
269 printk(" %02x", (unsigned char) (data [i]));
273 kcs->error_retries = 0;
274 memcpy(kcs->write_data, data, size);
275 kcs->write_count = size;
276 kcs->orig_write_count = size;
279 kcs->state = KCS_START_OP;
280 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
281 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
285 static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
288 if (length < kcs->read_pos) {
289 kcs->read_pos = length;
293 memcpy(data, kcs->read_data, kcs->read_pos);
295 if ((length >= 3) && (kcs->read_pos < 3)) {
296 /* Guarantee that we return at least 3 bytes, with an
297 error in the third byte if it is too short. */
298 data[2] = IPMI_ERR_UNSPECIFIED;
301 if (kcs->truncated) {
302 /* Report a truncated error. We might overwrite
303 another error, but that's too bad, the user needs
304 to know it was truncated. */
305 data[2] = IPMI_ERR_MSG_TRUNCATED;
309 return kcs->read_pos;
312 /* This implements the state machine defined in the IPMI manual, see
313 that for details on how this works. Divide that flowchart into
314 sections delimited by "Wait for IBF" and this will become clear. */
315 static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
317 unsigned char status;
320 status = read_status(kcs);
322 if (kcs_debug & KCS_DEBUG_STATES)
323 printk(KERN_DEBUG "KCS: State = %d, %x\n", kcs->state, status);
325 /* All states wait for ibf, so just do it here. */
326 if (!check_ibf(kcs, status, time))
327 return SI_SM_CALL_WITH_DELAY;
329 /* Just about everything looks at the KCS state, so grab that, too. */
330 state = GET_STATUS_STATE(status);
332 switch (kcs->state) {
334 /* If there's and interrupt source, turn it off. */
335 clear_obf(kcs, status);
337 if (GET_STATUS_ATN(status))
343 if (state != KCS_IDLE) {
344 start_error_recovery(kcs,
345 "State machine not idle at start");
349 clear_obf(kcs, status);
350 write_cmd(kcs, KCS_WRITE_START);
351 kcs->state = KCS_WAIT_WRITE_START;
354 case KCS_WAIT_WRITE_START:
355 if (state != KCS_WRITE_STATE) {
356 start_error_recovery(
358 "Not in write state at write start");
362 if (kcs->write_count == 1) {
363 write_cmd(kcs, KCS_WRITE_END);
364 kcs->state = KCS_WAIT_WRITE_END;
366 write_next_byte(kcs);
367 kcs->state = KCS_WAIT_WRITE;
372 if (state != KCS_WRITE_STATE) {
373 start_error_recovery(kcs,
374 "Not in write state for write");
377 clear_obf(kcs, status);
378 if (kcs->write_count == 1) {
379 write_cmd(kcs, KCS_WRITE_END);
380 kcs->state = KCS_WAIT_WRITE_END;
382 write_next_byte(kcs);
386 case KCS_WAIT_WRITE_END:
387 if (state != KCS_WRITE_STATE) {
388 start_error_recovery(kcs,
389 "Not in write state for write end");
392 clear_obf(kcs, status);
393 write_next_byte(kcs);
394 kcs->state = KCS_WAIT_READ;
398 if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
399 start_error_recovery(
401 "Not in read or idle in read state");
405 if (state == KCS_READ_STATE) {
406 if (! check_obf(kcs, status, time))
407 return SI_SM_CALL_WITH_DELAY;
410 /* We don't implement this exactly like the state
411 machine in the spec. Some broken hardware
412 does not write the final dummy byte to the
413 read register. Thus obf will never go high
414 here. We just go straight to idle, and we
415 handle clearing out obf in idle state if it
416 happens to come in. */
417 clear_obf(kcs, status);
418 kcs->orig_write_count = 0;
419 kcs->state = KCS_IDLE;
420 return SI_SM_TRANSACTION_COMPLETE;
425 clear_obf(kcs, status);
426 write_cmd(kcs, KCS_GET_STATUS_ABORT);
427 kcs->state = KCS_ERROR1;
431 clear_obf(kcs, status);
433 kcs->state = KCS_ERROR2;
437 if (state != KCS_READ_STATE) {
438 start_error_recovery(kcs,
439 "Not in read state for error2");
442 if (! check_obf(kcs, status, time))
443 return SI_SM_CALL_WITH_DELAY;
445 clear_obf(kcs, status);
446 write_data(kcs, KCS_READ_BYTE);
447 kcs->state = KCS_ERROR3;
451 if (state != KCS_IDLE_STATE) {
452 start_error_recovery(kcs,
453 "Not in idle state for error3");
457 if (! check_obf(kcs, status, time))
458 return SI_SM_CALL_WITH_DELAY;
460 clear_obf(kcs, status);
461 if (kcs->orig_write_count) {
462 restart_kcs_transaction(kcs);
464 kcs->state = KCS_IDLE;
465 return SI_SM_TRANSACTION_COMPLETE;
473 if (kcs->state == KCS_HOSED) {
474 init_kcs_data(kcs, kcs->io);
478 return SI_SM_CALL_WITHOUT_DELAY;
481 static int kcs_size(void)
483 return sizeof(struct si_sm_data);
486 static int kcs_detect(struct si_sm_data *kcs)
488 /* It's impossible for the KCS status register to be all 1's,
489 (assuming a properly functioning, self-initialized BMC)
490 but that's what you get from reading a bogus address, so we
492 if (read_status(kcs) == 0xff)
498 static void kcs_cleanup(struct si_sm_data *kcs)
502 struct si_sm_handlers kcs_smi_handlers =
504 .init_data = init_kcs_data,
505 .start_transaction = start_kcs_transaction,
506 .get_result = get_kcs_result,
508 .detect = kcs_detect,
509 .cleanup = kcs_cleanup,