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 #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. */
54 /* The states the KCS driver may be in. */
56 KCS_IDLE
, /* The KCS interface is currently
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
66 KCS_WAIT_WRITE
, /* We are writing bytes to the
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
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
82 KCS_ERROR3
, /* We wrote some data to the
83 interface, wait for it to switch to
85 KCS_HOSED
/* The hardware failed to follow the
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
99 enum kcs_states state
;
101 unsigned char write_data
[MAX_KCS_WRITE_SIZE
];
104 int orig_write_count
;
105 unsigned char read_data
[MAX_KCS_READ_SIZE
];
109 unsigned int error_retries
;
114 static unsigned int init_kcs_data(struct si_sm_data
*kcs
,
117 kcs
->state
= KCS_IDLE
;
120 kcs
->write_count
= 0;
121 kcs
->orig_write_count
= 0;
123 kcs
->error_retries
= 0;
125 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
126 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
128 /* Reserve 2 I/O bytes. */
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
);
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
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
]);
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
);
183 kcs
->state
= KCS_HOSED
;
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. */
196 kcs
->read_data
[kcs
->read_pos
] = read_data(kcs
);
199 write_data(kcs
, KCS_READ_BYTE
);
202 static inline int check_ibf(struct si_sm_data
*kcs
, unsigned char status
,
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
;
214 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
218 static inline int check_obf(struct si_sm_data
*kcs
, unsigned char status
,
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");
229 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
233 static void clear_obf(struct si_sm_data
*kcs
, unsigned char status
)
235 if (GET_STATUS_OBF(status
))
239 static void restart_kcs_transaction(struct si_sm_data
*kcs
)
241 kcs
->write_count
= kcs
->orig_write_count
;
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
,
253 if ((size
< 2) || (size
> MAX_KCS_WRITE_SIZE
)) {
257 if ((kcs
->state
!= KCS_IDLE
) && (kcs
->state
!= KCS_HOSED
)) {
261 kcs
->error_retries
= 0;
262 memcpy(kcs
->write_data
, data
, size
);
263 kcs
->write_count
= size
;
264 kcs
->orig_write_count
= size
;
267 kcs
->state
= KCS_START_OP
;
268 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
269 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
273 static int get_kcs_result(struct si_sm_data
*kcs
, unsigned char *data
,
276 if (length
< kcs
->read_pos
) {
277 kcs
->read_pos
= length
;
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
;
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
;
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
;
308 status
= read_status(kcs
);
311 printk(" State = %d, %x\n", kcs
->state
, status
);
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
) {
322 /* If there's and interrupt source, turn it off. */
323 clear_obf(kcs
, status
);
325 if (GET_STATUS_ATN(status
))
331 if (state
!= KCS_IDLE
) {
332 start_error_recovery(kcs
,
333 "State machine not idle at start");
337 clear_obf(kcs
, status
);
338 write_cmd(kcs
, KCS_WRITE_START
);
339 kcs
->state
= KCS_WAIT_WRITE_START
;
342 case KCS_WAIT_WRITE_START
:
343 if (state
!= KCS_WRITE_STATE
) {
344 start_error_recovery(
346 "Not in write state at write start");
350 if (kcs
->write_count
== 1) {
351 write_cmd(kcs
, KCS_WRITE_END
);
352 kcs
->state
= KCS_WAIT_WRITE_END
;
354 write_next_byte(kcs
);
355 kcs
->state
= KCS_WAIT_WRITE
;
360 if (state
!= KCS_WRITE_STATE
) {
361 start_error_recovery(kcs
,
362 "Not in write state for write");
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
;
370 write_next_byte(kcs
);
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");
380 clear_obf(kcs
, status
);
381 write_next_byte(kcs
);
382 kcs
->state
= KCS_WAIT_READ
;
386 if ((state
!= KCS_READ_STATE
) && (state
!= KCS_IDLE_STATE
)) {
387 start_error_recovery(
389 "Not in read or idle in read state");
393 if (state
== KCS_READ_STATE
) {
394 if (! check_obf(kcs
, status
, time
))
395 return SI_SM_CALL_WITH_DELAY
;
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
;
413 clear_obf(kcs
, status
);
414 write_cmd(kcs
, KCS_GET_STATUS_ABORT
);
415 kcs
->state
= KCS_ERROR1
;
419 clear_obf(kcs
, status
);
421 kcs
->state
= KCS_ERROR2
;
425 if (state
!= KCS_READ_STATE
) {
426 start_error_recovery(kcs
,
427 "Not in read state for error2");
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
;
439 if (state
!= KCS_IDLE_STATE
) {
440 start_error_recovery(kcs
,
441 "Not in idle state for error3");
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
);
452 kcs
->state
= KCS_IDLE
;
453 return SI_SM_TRANSACTION_COMPLETE
;
461 if (kcs
->state
== KCS_HOSED
) {
462 init_kcs_data(kcs
, kcs
->io
);
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
480 if (read_status(kcs
) == 0xff)
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
,
497 .detect
= kcs_detect
,
498 .cleanup
= kcs_cleanup
,