1 // SPDX-License-Identifier: GPL-2.0+
5 * State machine for handling IPMI KCS interfaces.
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
11 * Copyright 2002 MontaVista Software Inc.
15 * This state machine is taken from the state machine in the IPMI spec,
16 * pretty much verbatim. If you have questions about the states, see
20 #include <linux/kernel.h> /* For printk. */
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/string.h>
24 #include <linux/jiffies.h>
25 #include <linux/ipmi_msgdefs.h> /* for completion codes */
26 #include "ipmi_si_sm.h"
28 /* kcs_debug is a bit-field
29 * KCS_DEBUG_ENABLE - turned on for now
30 * KCS_DEBUG_MSG - commands and their responses
31 * KCS_DEBUG_STATES - state machine
33 #define KCS_DEBUG_STATES 4
34 #define KCS_DEBUG_MSG 2
35 #define KCS_DEBUG_ENABLE 1
38 module_param(kcs_debug
, int, 0644);
39 MODULE_PARM_DESC(kcs_debug
, "debug bitmask, 1=enable, 2=messages, 4=states");
41 /* The states the KCS driver may be in. */
43 /* The KCS interface is currently doing nothing. */
47 * We are starting an operation. The data is in the output
48 * buffer, but nothing has been done to the interface yet. This
49 * was added to the state machine in the spec to wait for the
54 /* We have written a write cmd to the interface. */
57 /* We are writing bytes to the interface. */
61 * We have written the write end cmd to the interface, and
62 * still need to write the last byte.
66 /* We are waiting to read data from the interface. */
70 * State to transition to the error handler, this was added to
71 * the state machine in the spec to be sure IBF was there.
76 * First stage error handler, wait for the interface to
82 * The abort cmd has been written, wait for the interface to
88 * We wrote some data to the interface, wait for it to switch
93 /* The hardware failed to follow the state machine. */
97 #define MAX_KCS_READ_SIZE IPMI_MAX_MSG_LENGTH
98 #define MAX_KCS_WRITE_SIZE IPMI_MAX_MSG_LENGTH
100 /* Timeouts in microseconds. */
101 #define IBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
102 #define OBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
103 #define MAX_ERROR_RETRIES 10
104 #define ERROR0_OBF_WAIT_JIFFIES (2*HZ)
107 enum kcs_states state
;
109 unsigned char write_data
[MAX_KCS_WRITE_SIZE
];
112 int orig_write_count
;
113 unsigned char read_data
[MAX_KCS_READ_SIZE
];
117 unsigned int error_retries
;
120 unsigned long error0_timeout
;
123 static unsigned int init_kcs_data(struct si_sm_data
*kcs
,
126 kcs
->state
= KCS_IDLE
;
129 kcs
->write_count
= 0;
130 kcs
->orig_write_count
= 0;
132 kcs
->error_retries
= 0;
134 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
135 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
137 /* Reserve 2 I/O bytes. */
141 static inline unsigned char read_status(struct si_sm_data
*kcs
)
143 return kcs
->io
->inputb(kcs
->io
, 1);
146 static inline unsigned char read_data(struct si_sm_data
*kcs
)
148 return kcs
->io
->inputb(kcs
->io
, 0);
151 static inline void write_cmd(struct si_sm_data
*kcs
, unsigned char data
)
153 kcs
->io
->outputb(kcs
->io
, 1, data
);
156 static inline void write_data(struct si_sm_data
*kcs
, unsigned char data
)
158 kcs
->io
->outputb(kcs
->io
, 0, data
);
162 #define KCS_GET_STATUS_ABORT 0x60
163 #define KCS_WRITE_START 0x61
164 #define KCS_WRITE_END 0x62
165 #define KCS_READ_BYTE 0x68
168 #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
169 #define KCS_IDLE_STATE 0
170 #define KCS_READ_STATE 1
171 #define KCS_WRITE_STATE 2
172 #define KCS_ERROR_STATE 3
173 #define GET_STATUS_ATN(status) ((status) & 0x04)
174 #define GET_STATUS_IBF(status) ((status) & 0x02)
175 #define GET_STATUS_OBF(status) ((status) & 0x01)
178 static inline void write_next_byte(struct si_sm_data
*kcs
)
180 write_data(kcs
, kcs
->write_data
[kcs
->write_pos
]);
182 (kcs
->write_count
)--;
185 static inline void start_error_recovery(struct si_sm_data
*kcs
, char *reason
)
187 (kcs
->error_retries
)++;
188 if (kcs
->error_retries
> MAX_ERROR_RETRIES
) {
189 if (kcs_debug
& KCS_DEBUG_ENABLE
)
190 printk(KERN_DEBUG
"ipmi_kcs_sm: kcs hosed: %s\n",
192 kcs
->state
= KCS_HOSED
;
194 kcs
->error0_timeout
= jiffies
+ ERROR0_OBF_WAIT_JIFFIES
;
195 kcs
->state
= KCS_ERROR0
;
199 static inline void read_next_byte(struct si_sm_data
*kcs
)
201 if (kcs
->read_pos
>= MAX_KCS_READ_SIZE
) {
202 /* Throw the data away and mark it truncated. */
206 kcs
->read_data
[kcs
->read_pos
] = read_data(kcs
);
209 write_data(kcs
, KCS_READ_BYTE
);
212 static inline int check_ibf(struct si_sm_data
*kcs
, unsigned char status
,
215 if (GET_STATUS_IBF(status
)) {
216 kcs
->ibf_timeout
-= time
;
217 if (kcs
->ibf_timeout
< 0) {
218 start_error_recovery(kcs
, "IBF not ready in time");
219 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
224 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
228 static inline int check_obf(struct si_sm_data
*kcs
, unsigned char status
,
231 if (!GET_STATUS_OBF(status
)) {
232 kcs
->obf_timeout
-= time
;
233 if (kcs
->obf_timeout
< 0) {
234 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
235 start_error_recovery(kcs
, "OBF not ready in time");
240 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
244 static void clear_obf(struct si_sm_data
*kcs
, unsigned char status
)
246 if (GET_STATUS_OBF(status
))
250 static void restart_kcs_transaction(struct si_sm_data
*kcs
)
252 kcs
->write_count
= kcs
->orig_write_count
;
255 kcs
->state
= KCS_WAIT_WRITE_START
;
256 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
257 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
258 write_cmd(kcs
, KCS_WRITE_START
);
261 static int start_kcs_transaction(struct si_sm_data
*kcs
, unsigned char *data
,
267 return IPMI_REQ_LEN_INVALID_ERR
;
268 if (size
> MAX_KCS_WRITE_SIZE
)
269 return IPMI_REQ_LEN_EXCEEDED_ERR
;
271 if ((kcs
->state
!= KCS_IDLE
) && (kcs
->state
!= KCS_HOSED
))
272 return IPMI_NOT_IN_MY_STATE_ERR
;
274 if (kcs_debug
& KCS_DEBUG_MSG
) {
275 printk(KERN_DEBUG
"start_kcs_transaction -");
276 for (i
= 0; i
< size
; i
++)
277 printk(" %02x", (unsigned char) (data
[i
]));
280 kcs
->error_retries
= 0;
281 memcpy(kcs
->write_data
, data
, size
);
282 kcs
->write_count
= size
;
283 kcs
->orig_write_count
= size
;
286 kcs
->state
= KCS_START_OP
;
287 kcs
->ibf_timeout
= IBF_RETRY_TIMEOUT
;
288 kcs
->obf_timeout
= OBF_RETRY_TIMEOUT
;
292 static int get_kcs_result(struct si_sm_data
*kcs
, unsigned char *data
,
295 if (length
< kcs
->read_pos
) {
296 kcs
->read_pos
= length
;
300 memcpy(data
, kcs
->read_data
, kcs
->read_pos
);
302 if ((length
>= 3) && (kcs
->read_pos
< 3)) {
303 /* Guarantee that we return at least 3 bytes, with an
304 error in the third byte if it is too short. */
305 data
[2] = IPMI_ERR_UNSPECIFIED
;
308 if (kcs
->truncated
) {
310 * Report a truncated error. We might overwrite
311 * another error, but that's too bad, the user needs
312 * to know it was truncated.
314 data
[2] = IPMI_ERR_MSG_TRUNCATED
;
318 return kcs
->read_pos
;
322 * This implements the state machine defined in the IPMI manual, see
323 * that for details on how this works. Divide that flowchart into
324 * sections delimited by "Wait for IBF" and this will become clear.
326 static enum si_sm_result
kcs_event(struct si_sm_data
*kcs
, long time
)
328 unsigned char status
;
331 status
= read_status(kcs
);
333 if (kcs_debug
& KCS_DEBUG_STATES
)
334 printk(KERN_DEBUG
"KCS: State = %d, %x\n", kcs
->state
, status
);
336 /* All states wait for ibf, so just do it here. */
337 if (!check_ibf(kcs
, status
, time
))
338 return SI_SM_CALL_WITH_DELAY
;
340 /* Just about everything looks at the KCS state, so grab that, too. */
341 state
= GET_STATUS_STATE(status
);
343 switch (kcs
->state
) {
345 /* If there's and interrupt source, turn it off. */
346 clear_obf(kcs
, status
);
348 if (GET_STATUS_ATN(status
))
354 if (state
!= KCS_IDLE_STATE
) {
355 start_error_recovery(kcs
,
356 "State machine not idle at start");
360 clear_obf(kcs
, status
);
361 write_cmd(kcs
, KCS_WRITE_START
);
362 kcs
->state
= KCS_WAIT_WRITE_START
;
365 case KCS_WAIT_WRITE_START
:
366 if (state
!= KCS_WRITE_STATE
) {
367 start_error_recovery(
369 "Not in write state at write start");
373 if (kcs
->write_count
== 1) {
374 write_cmd(kcs
, KCS_WRITE_END
);
375 kcs
->state
= KCS_WAIT_WRITE_END
;
377 write_next_byte(kcs
);
378 kcs
->state
= KCS_WAIT_WRITE
;
383 if (state
!= KCS_WRITE_STATE
) {
384 start_error_recovery(kcs
,
385 "Not in write state for write");
388 clear_obf(kcs
, status
);
389 if (kcs
->write_count
== 1) {
390 write_cmd(kcs
, KCS_WRITE_END
);
391 kcs
->state
= KCS_WAIT_WRITE_END
;
393 write_next_byte(kcs
);
397 case KCS_WAIT_WRITE_END
:
398 if (state
!= KCS_WRITE_STATE
) {
399 start_error_recovery(kcs
,
404 clear_obf(kcs
, status
);
405 write_next_byte(kcs
);
406 kcs
->state
= KCS_WAIT_READ
;
410 if ((state
!= KCS_READ_STATE
) && (state
!= KCS_IDLE_STATE
)) {
411 start_error_recovery(
413 "Not in read or idle in read state");
417 if (state
== KCS_READ_STATE
) {
418 if (!check_obf(kcs
, status
, time
))
419 return SI_SM_CALL_WITH_DELAY
;
423 * We don't implement this exactly like the state
424 * machine in the spec. Some broken hardware
425 * does not write the final dummy byte to the
426 * read register. Thus obf will never go high
427 * here. We just go straight to idle, and we
428 * handle clearing out obf in idle state if it
429 * happens to come in.
431 clear_obf(kcs
, status
);
432 kcs
->orig_write_count
= 0;
433 kcs
->state
= KCS_IDLE
;
434 return SI_SM_TRANSACTION_COMPLETE
;
439 clear_obf(kcs
, status
);
440 status
= read_status(kcs
);
441 if (GET_STATUS_OBF(status
))
442 /* controller isn't responding */
443 if (time_before(jiffies
, kcs
->error0_timeout
))
444 return SI_SM_CALL_WITH_TICK_DELAY
;
445 write_cmd(kcs
, KCS_GET_STATUS_ABORT
);
446 kcs
->state
= KCS_ERROR1
;
450 clear_obf(kcs
, status
);
452 kcs
->state
= KCS_ERROR2
;
456 if (state
!= KCS_READ_STATE
) {
457 start_error_recovery(kcs
,
458 "Not in read state for error2");
461 if (!check_obf(kcs
, status
, time
))
462 return SI_SM_CALL_WITH_DELAY
;
464 clear_obf(kcs
, status
);
465 write_data(kcs
, KCS_READ_BYTE
);
466 kcs
->state
= KCS_ERROR3
;
470 if (state
!= KCS_IDLE_STATE
) {
471 start_error_recovery(kcs
,
472 "Not in idle state for error3");
476 if (!check_obf(kcs
, status
, time
))
477 return SI_SM_CALL_WITH_DELAY
;
479 clear_obf(kcs
, status
);
480 if (kcs
->orig_write_count
) {
481 restart_kcs_transaction(kcs
);
483 kcs
->state
= KCS_IDLE
;
484 return SI_SM_TRANSACTION_COMPLETE
;
492 if (kcs
->state
== KCS_HOSED
) {
493 init_kcs_data(kcs
, kcs
->io
);
497 return SI_SM_CALL_WITHOUT_DELAY
;
500 static int kcs_size(void)
502 return sizeof(struct si_sm_data
);
505 static int kcs_detect(struct si_sm_data
*kcs
)
508 * It's impossible for the KCS status register to be all 1's,
509 * (assuming a properly functioning, self-initialized BMC)
510 * but that's what you get from reading a bogus address, so we
513 if (read_status(kcs
) == 0xff)
519 static void kcs_cleanup(struct si_sm_data
*kcs
)
523 const struct si_sm_handlers kcs_smi_handlers
= {
524 .init_data
= init_kcs_data
,
525 .start_transaction
= start_kcs_transaction
,
526 .get_result
= get_kcs_result
,
528 .detect
= kcs_detect
,
529 .cleanup
= kcs_cleanup
,