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 #define DEBUG /* So dev_dbg() is always available. */
22 #include <linux/kernel.h> /* For printk. */
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/string.h>
26 #include <linux/jiffies.h>
27 #include <linux/ipmi_msgdefs.h> /* for completion codes */
28 #include "ipmi_si_sm.h"
30 /* kcs_debug is a bit-field
31 * KCS_DEBUG_ENABLE - turned on for now
32 * KCS_DEBUG_MSG - commands and their responses
33 * KCS_DEBUG_STATES - state machine
35 #define KCS_DEBUG_STATES 4
36 #define KCS_DEBUG_MSG 2
37 #define KCS_DEBUG_ENABLE 1
40 module_param(kcs_debug, int, 0644);
41 MODULE_PARM_DESC(kcs_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
43 /* The states the KCS driver may be in. */
45 /* The KCS interface is currently doing nothing. */
49 * We are starting an operation. The data is in the output
50 * buffer, but nothing has been done to the interface yet. This
51 * was added to the state machine in the spec to wait for the
56 /* We have written a write cmd to the interface. */
59 /* We are writing bytes to the interface. */
63 * We have written the write end cmd to the interface, and
64 * still need to write the last byte.
68 /* We are waiting to read data from the interface. */
72 * State to transition to the error handler, this was added to
73 * the state machine in the spec to be sure IBF was there.
78 * First stage error handler, wait for the interface to
84 * The abort cmd has been written, wait for the interface to
90 * We wrote some data to the interface, wait for it to switch
95 /* The hardware failed to follow the state machine. */
99 #define MAX_KCS_READ_SIZE IPMI_MAX_MSG_LENGTH
100 #define MAX_KCS_WRITE_SIZE IPMI_MAX_MSG_LENGTH
102 /* Timeouts in microseconds. */
103 #define IBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
104 #define OBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
105 #define MAX_ERROR_RETRIES 10
106 #define ERROR0_OBF_WAIT_JIFFIES (2*HZ)
109 enum kcs_states state;
111 unsigned char write_data[MAX_KCS_WRITE_SIZE];
114 int orig_write_count;
115 unsigned char read_data[MAX_KCS_READ_SIZE];
119 unsigned int error_retries;
122 unsigned long error0_timeout;
125 static unsigned int init_kcs_data_with_state(struct si_sm_data *kcs,
126 struct si_sm_io *io, enum kcs_states state)
131 kcs->write_count = 0;
132 kcs->orig_write_count = 0;
134 kcs->error_retries = 0;
136 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
137 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
139 /* Reserve 2 I/O bytes. */
143 static unsigned int init_kcs_data(struct si_sm_data *kcs,
146 return init_kcs_data_with_state(kcs, io, KCS_IDLE);
149 static inline unsigned char read_status(struct si_sm_data *kcs)
151 return kcs->io->inputb(kcs->io, 1);
154 static inline unsigned char read_data(struct si_sm_data *kcs)
156 return kcs->io->inputb(kcs->io, 0);
159 static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
161 kcs->io->outputb(kcs->io, 1, data);
164 static inline void write_data(struct si_sm_data *kcs, unsigned char data)
166 kcs->io->outputb(kcs->io, 0, data);
170 #define KCS_GET_STATUS_ABORT 0x60
171 #define KCS_WRITE_START 0x61
172 #define KCS_WRITE_END 0x62
173 #define KCS_READ_BYTE 0x68
176 #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
177 #define KCS_IDLE_STATE 0
178 #define KCS_READ_STATE 1
179 #define KCS_WRITE_STATE 2
180 #define KCS_ERROR_STATE 3
181 #define GET_STATUS_ATN(status) ((status) & 0x04)
182 #define GET_STATUS_IBF(status) ((status) & 0x02)
183 #define GET_STATUS_OBF(status) ((status) & 0x01)
186 static inline void write_next_byte(struct si_sm_data *kcs)
188 write_data(kcs, kcs->write_data[kcs->write_pos]);
190 (kcs->write_count)--;
193 static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
195 (kcs->error_retries)++;
196 if (kcs->error_retries > MAX_ERROR_RETRIES) {
197 if (kcs_debug & KCS_DEBUG_ENABLE)
198 dev_dbg(kcs->io->dev, "ipmi_kcs_sm: kcs hosed: %s\n",
200 kcs->state = KCS_HOSED;
202 kcs->error0_timeout = jiffies + ERROR0_OBF_WAIT_JIFFIES;
203 kcs->state = KCS_ERROR0;
207 static inline void read_next_byte(struct si_sm_data *kcs)
209 if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
210 /* Throw the data away and mark it truncated. */
214 kcs->read_data[kcs->read_pos] = read_data(kcs);
217 write_data(kcs, KCS_READ_BYTE);
220 static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
223 if (GET_STATUS_IBF(status)) {
224 kcs->ibf_timeout -= time;
225 if (kcs->ibf_timeout < 0) {
226 start_error_recovery(kcs, "IBF not ready in time");
227 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
232 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
236 static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
239 if (!GET_STATUS_OBF(status)) {
240 kcs->obf_timeout -= time;
241 if (kcs->obf_timeout < 0) {
242 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
243 start_error_recovery(kcs, "OBF not ready in time");
248 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
252 static void clear_obf(struct si_sm_data *kcs, unsigned char status)
254 if (GET_STATUS_OBF(status))
258 static void restart_kcs_transaction(struct si_sm_data *kcs)
260 kcs->write_count = kcs->orig_write_count;
263 kcs->state = KCS_WAIT_WRITE_START;
264 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
265 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
266 write_cmd(kcs, KCS_WRITE_START);
269 static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
275 return IPMI_REQ_LEN_INVALID_ERR;
276 if (size > MAX_KCS_WRITE_SIZE)
277 return IPMI_REQ_LEN_EXCEEDED_ERR;
279 if (kcs->state != KCS_IDLE) {
280 dev_warn(kcs->io->dev, "KCS in invalid state %d\n", kcs->state);
281 return IPMI_NOT_IN_MY_STATE_ERR;
284 if (kcs_debug & KCS_DEBUG_MSG) {
285 dev_dbg(kcs->io->dev, "%s -", __func__);
286 for (i = 0; i < size; i++)
287 pr_cont(" %02x", data[i]);
290 kcs->error_retries = 0;
291 memcpy(kcs->write_data, data, size);
292 kcs->write_count = size;
293 kcs->orig_write_count = size;
296 kcs->state = KCS_START_OP;
297 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
298 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
302 static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
305 if (length < kcs->read_pos) {
306 kcs->read_pos = length;
310 memcpy(data, kcs->read_data, kcs->read_pos);
312 if ((length >= 3) && (kcs->read_pos < 3)) {
313 /* Guarantee that we return at least 3 bytes, with an
314 error in the third byte if it is too short. */
315 data[2] = IPMI_ERR_UNSPECIFIED;
318 if (kcs->truncated) {
320 * Report a truncated error. We might overwrite
321 * another error, but that's too bad, the user needs
322 * to know it was truncated.
324 data[2] = IPMI_ERR_MSG_TRUNCATED;
328 return kcs->read_pos;
332 * This implements the state machine defined in the IPMI manual, see
333 * that for details on how this works. Divide that flowchart into
334 * sections delimited by "Wait for IBF" and this will become clear.
336 static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
338 unsigned char status;
341 status = read_status(kcs);
343 if (kcs_debug & KCS_DEBUG_STATES)
344 dev_dbg(kcs->io->dev,
345 "KCS: State = %d, %x\n", kcs->state, status);
347 /* All states wait for ibf, so just do it here. */
348 if (!check_ibf(kcs, status, time))
349 return SI_SM_CALL_WITH_DELAY;
351 /* Just about everything looks at the KCS state, so grab that, too. */
352 state = GET_STATUS_STATE(status);
354 switch (kcs->state) {
356 /* If there's and interrupt source, turn it off. */
357 clear_obf(kcs, status);
359 if (GET_STATUS_ATN(status))
365 if (state != KCS_IDLE_STATE) {
366 start_error_recovery(kcs,
367 "State machine not idle at start");
371 clear_obf(kcs, status);
372 write_cmd(kcs, KCS_WRITE_START);
373 kcs->state = KCS_WAIT_WRITE_START;
376 case KCS_WAIT_WRITE_START:
377 if (state != KCS_WRITE_STATE) {
378 start_error_recovery(
380 "Not in write state at write start");
384 if (kcs->write_count == 1) {
385 write_cmd(kcs, KCS_WRITE_END);
386 kcs->state = KCS_WAIT_WRITE_END;
388 write_next_byte(kcs);
389 kcs->state = KCS_WAIT_WRITE;
394 if (state != KCS_WRITE_STATE) {
395 start_error_recovery(kcs,
396 "Not in write state for write");
399 clear_obf(kcs, status);
400 if (kcs->write_count == 1) {
401 write_cmd(kcs, KCS_WRITE_END);
402 kcs->state = KCS_WAIT_WRITE_END;
404 write_next_byte(kcs);
408 case KCS_WAIT_WRITE_END:
409 if (state != KCS_WRITE_STATE) {
410 start_error_recovery(kcs,
415 clear_obf(kcs, status);
416 write_next_byte(kcs);
417 kcs->state = KCS_WAIT_READ;
421 if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
422 start_error_recovery(
424 "Not in read or idle in read state");
428 if (state == KCS_READ_STATE) {
429 if (!check_obf(kcs, status, time))
430 return SI_SM_CALL_WITH_DELAY;
434 * We don't implement this exactly like the state
435 * machine in the spec. Some broken hardware
436 * does not write the final dummy byte to the
437 * read register. Thus obf will never go high
438 * here. We just go straight to idle, and we
439 * handle clearing out obf in idle state if it
440 * happens to come in.
442 clear_obf(kcs, status);
443 kcs->orig_write_count = 0;
444 kcs->state = KCS_IDLE;
445 return SI_SM_TRANSACTION_COMPLETE;
450 clear_obf(kcs, status);
451 status = read_status(kcs);
452 if (GET_STATUS_OBF(status))
453 /* controller isn't responding */
454 if (time_before(jiffies, kcs->error0_timeout))
455 return SI_SM_CALL_WITH_TICK_DELAY;
456 write_cmd(kcs, KCS_GET_STATUS_ABORT);
457 kcs->state = KCS_ERROR1;
461 clear_obf(kcs, status);
463 kcs->state = KCS_ERROR2;
467 if (state != KCS_READ_STATE) {
468 start_error_recovery(kcs,
469 "Not in read state for error2");
472 if (!check_obf(kcs, status, time))
473 return SI_SM_CALL_WITH_DELAY;
475 clear_obf(kcs, status);
476 write_data(kcs, KCS_READ_BYTE);
477 kcs->state = KCS_ERROR3;
481 if (state != KCS_IDLE_STATE) {
482 start_error_recovery(kcs,
483 "Not in idle state for error3");
487 if (!check_obf(kcs, status, time))
488 return SI_SM_CALL_WITH_DELAY;
490 clear_obf(kcs, status);
491 if (kcs->orig_write_count) {
492 restart_kcs_transaction(kcs);
494 kcs->state = KCS_IDLE;
495 return SI_SM_TRANSACTION_COMPLETE;
503 if (kcs->state == KCS_HOSED) {
504 init_kcs_data_with_state(kcs, kcs->io, KCS_ERROR0);
508 return SI_SM_CALL_WITHOUT_DELAY;
511 static int kcs_size(void)
513 return sizeof(struct si_sm_data);
516 static int kcs_detect(struct si_sm_data *kcs)
519 * It's impossible for the KCS status register to be all 1's,
520 * (assuming a properly functioning, self-initialized BMC)
521 * but that's what you get from reading a bogus address, so we
524 if (read_status(kcs) == 0xff)
530 static void kcs_cleanup(struct si_sm_data *kcs)
534 const struct si_sm_handlers kcs_smi_handlers = {
535 .init_data = init_kcs_data,
536 .start_transaction = start_kcs_transaction,
537 .get_result = get_kcs_result,
539 .detect = kcs_detect,
540 .cleanup = kcs_cleanup,