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/string.h>
42 #include <linux/ipmi_msgdefs.h> /* for completion codes */
43 #include "ipmi_si_sm.h"
45 /* Set this if you want a printout of why the state machine was hosed
46 when it gets hosed. */
47 #define DEBUG_HOSED_REASON
49 /* Print the state machine state on entry every time. */
52 /* The states the KCS driver may be in. */
54 KCS_IDLE, /* The KCS interface is currently
56 KCS_START_OP, /* We are starting an operation. The
57 data is in the output buffer, but
58 nothing has been done to the
59 interface yet. This was added to
60 the state machine in the spec to
61 wait for the initial IBF. */
62 KCS_WAIT_WRITE_START, /* We have written a write cmd to the
64 KCS_WAIT_WRITE, /* We are writing bytes to the
66 KCS_WAIT_WRITE_END, /* We have written the write end cmd
67 to the interface, and still need to
68 write the last byte. */
69 KCS_WAIT_READ, /* We are waiting to read data from
71 KCS_ERROR0, /* State to transition to the error
72 handler, this was added to the
73 state machine in the spec to be
74 sure IBF was there. */
75 KCS_ERROR1, /* First stage error handler, wait for
76 the interface to respond. */
77 KCS_ERROR2, /* The abort cmd has been written,
78 wait for the interface to
80 KCS_ERROR3, /* We wrote some data to the
81 interface, wait for it to switch to
83 KCS_HOSED /* The hardware failed to follow the
87 #define MAX_KCS_READ_SIZE 80
88 #define MAX_KCS_WRITE_SIZE 80
90 /* Timeouts in microseconds. */
91 #define IBF_RETRY_TIMEOUT 1000000
92 #define OBF_RETRY_TIMEOUT 1000000
93 #define MAX_ERROR_RETRIES 10
97 enum kcs_states state;
99 unsigned char write_data[MAX_KCS_WRITE_SIZE];
102 int orig_write_count;
103 unsigned char read_data[MAX_KCS_READ_SIZE];
107 unsigned int error_retries;
112 static unsigned int init_kcs_data(struct si_sm_data *kcs,
115 kcs->state = KCS_IDLE;
118 kcs->write_count = 0;
119 kcs->orig_write_count = 0;
121 kcs->error_retries = 0;
123 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
124 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
126 /* Reserve 2 I/O bytes. */
130 static inline unsigned char read_status(struct si_sm_data *kcs)
132 return kcs->io->inputb(kcs->io, 1);
135 static inline unsigned char read_data(struct si_sm_data *kcs)
137 return kcs->io->inputb(kcs->io, 0);
140 static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
142 kcs->io->outputb(kcs->io, 1, data);
145 static inline void write_data(struct si_sm_data *kcs, unsigned char data)
147 kcs->io->outputb(kcs->io, 0, data);
151 #define KCS_GET_STATUS_ABORT 0x60
152 #define KCS_WRITE_START 0x61
153 #define KCS_WRITE_END 0x62
154 #define KCS_READ_BYTE 0x68
157 #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
158 #define KCS_IDLE_STATE 0
159 #define KCS_READ_STATE 1
160 #define KCS_WRITE_STATE 2
161 #define KCS_ERROR_STATE 3
162 #define GET_STATUS_ATN(status) ((status) & 0x04)
163 #define GET_STATUS_IBF(status) ((status) & 0x02)
164 #define GET_STATUS_OBF(status) ((status) & 0x01)
167 static inline void write_next_byte(struct si_sm_data *kcs)
169 write_data(kcs, kcs->write_data[kcs->write_pos]);
171 (kcs->write_count)--;
174 static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
176 (kcs->error_retries)++;
177 if (kcs->error_retries > MAX_ERROR_RETRIES) {
178 #ifdef DEBUG_HOSED_REASON
179 printk("ipmi_kcs_sm: kcs hosed: %s\n", reason);
181 kcs->state = KCS_HOSED;
183 kcs->state = KCS_ERROR0;
187 static inline void read_next_byte(struct si_sm_data *kcs)
189 if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
190 /* Throw the data away and mark it truncated. */
194 kcs->read_data[kcs->read_pos] = read_data(kcs);
197 write_data(kcs, KCS_READ_BYTE);
200 static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
203 if (GET_STATUS_IBF(status)) {
204 kcs->ibf_timeout -= time;
205 if (kcs->ibf_timeout < 0) {
206 start_error_recovery(kcs, "IBF not ready in time");
207 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
212 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
216 static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
219 if (! GET_STATUS_OBF(status)) {
220 kcs->obf_timeout -= time;
221 if (kcs->obf_timeout < 0) {
222 start_error_recovery(kcs, "OBF not ready in time");
227 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
231 static void clear_obf(struct si_sm_data *kcs, unsigned char status)
233 if (GET_STATUS_OBF(status))
237 static void restart_kcs_transaction(struct si_sm_data *kcs)
239 kcs->write_count = kcs->orig_write_count;
242 kcs->state = KCS_WAIT_WRITE_START;
243 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
244 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
245 write_cmd(kcs, KCS_WRITE_START);
248 static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
251 if ((size < 2) || (size > MAX_KCS_WRITE_SIZE)) {
255 if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) {
259 kcs->error_retries = 0;
260 memcpy(kcs->write_data, data, size);
261 kcs->write_count = size;
262 kcs->orig_write_count = size;
265 kcs->state = KCS_START_OP;
266 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
267 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
271 static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
274 if (length < kcs->read_pos) {
275 kcs->read_pos = length;
279 memcpy(data, kcs->read_data, kcs->read_pos);
281 if ((length >= 3) && (kcs->read_pos < 3)) {
282 /* Guarantee that we return at least 3 bytes, with an
283 error in the third byte if it is too short. */
284 data[2] = IPMI_ERR_UNSPECIFIED;
287 if (kcs->truncated) {
288 /* Report a truncated error. We might overwrite
289 another error, but that's too bad, the user needs
290 to know it was truncated. */
291 data[2] = IPMI_ERR_MSG_TRUNCATED;
295 return kcs->read_pos;
298 /* This implements the state machine defined in the IPMI manual, see
299 that for details on how this works. Divide that flowchart into
300 sections delimited by "Wait for IBF" and this will become clear. */
301 static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
303 unsigned char status;
306 status = read_status(kcs);
309 printk(" State = %d, %x\n", kcs->state, status);
311 /* All states wait for ibf, so just do it here. */
312 if (!check_ibf(kcs, status, time))
313 return SI_SM_CALL_WITH_DELAY;
315 /* Just about everything looks at the KCS state, so grab that, too. */
316 state = GET_STATUS_STATE(status);
318 switch (kcs->state) {
320 /* If there's and interrupt source, turn it off. */
321 clear_obf(kcs, status);
323 if (GET_STATUS_ATN(status))
329 if (state != KCS_IDLE) {
330 start_error_recovery(kcs,
331 "State machine not idle at start");
335 clear_obf(kcs, status);
336 write_cmd(kcs, KCS_WRITE_START);
337 kcs->state = KCS_WAIT_WRITE_START;
340 case KCS_WAIT_WRITE_START:
341 if (state != KCS_WRITE_STATE) {
342 start_error_recovery(
344 "Not in write state at write start");
348 if (kcs->write_count == 1) {
349 write_cmd(kcs, KCS_WRITE_END);
350 kcs->state = KCS_WAIT_WRITE_END;
352 write_next_byte(kcs);
353 kcs->state = KCS_WAIT_WRITE;
358 if (state != KCS_WRITE_STATE) {
359 start_error_recovery(kcs,
360 "Not in write state for write");
363 clear_obf(kcs, status);
364 if (kcs->write_count == 1) {
365 write_cmd(kcs, KCS_WRITE_END);
366 kcs->state = KCS_WAIT_WRITE_END;
368 write_next_byte(kcs);
372 case KCS_WAIT_WRITE_END:
373 if (state != KCS_WRITE_STATE) {
374 start_error_recovery(kcs,
375 "Not in write state for write end");
378 clear_obf(kcs, status);
379 write_next_byte(kcs);
380 kcs->state = KCS_WAIT_READ;
384 if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
385 start_error_recovery(
387 "Not in read or idle in read state");
391 if (state == KCS_READ_STATE) {
392 if (! check_obf(kcs, status, time))
393 return SI_SM_CALL_WITH_DELAY;
396 /* We don't implement this exactly like the state
397 machine in the spec. Some broken hardware
398 does not write the final dummy byte to the
399 read register. Thus obf will never go high
400 here. We just go straight to idle, and we
401 handle clearing out obf in idle state if it
402 happens to come in. */
403 clear_obf(kcs, status);
404 kcs->orig_write_count = 0;
405 kcs->state = KCS_IDLE;
406 return SI_SM_TRANSACTION_COMPLETE;
411 clear_obf(kcs, status);
412 write_cmd(kcs, KCS_GET_STATUS_ABORT);
413 kcs->state = KCS_ERROR1;
417 clear_obf(kcs, status);
419 kcs->state = KCS_ERROR2;
423 if (state != KCS_READ_STATE) {
424 start_error_recovery(kcs,
425 "Not in read state for error2");
428 if (! check_obf(kcs, status, time))
429 return SI_SM_CALL_WITH_DELAY;
431 clear_obf(kcs, status);
432 write_data(kcs, KCS_READ_BYTE);
433 kcs->state = KCS_ERROR3;
437 if (state != KCS_IDLE_STATE) {
438 start_error_recovery(kcs,
439 "Not in idle state for error3");
443 if (! check_obf(kcs, status, time))
444 return SI_SM_CALL_WITH_DELAY;
446 clear_obf(kcs, status);
447 if (kcs->orig_write_count) {
448 restart_kcs_transaction(kcs);
450 kcs->state = KCS_IDLE;
451 return SI_SM_TRANSACTION_COMPLETE;
459 if (kcs->state == KCS_HOSED) {
460 init_kcs_data(kcs, kcs->io);
464 return SI_SM_CALL_WITHOUT_DELAY;
467 static int kcs_size(void)
469 return sizeof(struct si_sm_data);
472 static int kcs_detect(struct si_sm_data *kcs)
474 /* It's impossible for the KCS status register to be all 1's,
475 (assuming a properly functioning, self-initialized BMC)
476 but that's what you get from reading a bogus address, so we
478 if (read_status(kcs) == 0xff)
484 static void kcs_cleanup(struct si_sm_data *kcs)
488 struct si_sm_handlers kcs_smi_handlers =
490 .init_data = init_kcs_data,
491 .start_transaction = start_kcs_transaction,
492 .get_result = get_kcs_result,
494 .detect = kcs_detect,
495 .cleanup = kcs_cleanup,