18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ipmi_kcs_sm.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * State machine for handling IPMI KCS interfaces.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: MontaVista Software, Inc.
88c2ecf20Sopenharmony_ci *         Corey Minyard <minyard@mvista.com>
98c2ecf20Sopenharmony_ci *         source@mvista.com
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Copyright 2002 MontaVista Software Inc.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * This state machine is taken from the state machine in the IPMI spec,
168c2ecf20Sopenharmony_ci * pretty much verbatim.  If you have questions about the states, see
178c2ecf20Sopenharmony_ci * that document.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define DEBUG /* So dev_dbg() is always available. */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/kernel.h> /* For printk. */
238c2ecf20Sopenharmony_ci#include <linux/module.h>
248c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
258c2ecf20Sopenharmony_ci#include <linux/string.h>
268c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
278c2ecf20Sopenharmony_ci#include <linux/ipmi_msgdefs.h>		/* for completion codes */
288c2ecf20Sopenharmony_ci#include "ipmi_si_sm.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* kcs_debug is a bit-field
318c2ecf20Sopenharmony_ci *	KCS_DEBUG_ENABLE -	turned on for now
328c2ecf20Sopenharmony_ci *	KCS_DEBUG_MSG    -	commands and their responses
338c2ecf20Sopenharmony_ci *	KCS_DEBUG_STATES -	state machine
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#define KCS_DEBUG_STATES	4
368c2ecf20Sopenharmony_ci#define KCS_DEBUG_MSG		2
378c2ecf20Sopenharmony_ci#define	KCS_DEBUG_ENABLE	1
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int kcs_debug;
408c2ecf20Sopenharmony_cimodule_param(kcs_debug, int, 0644);
418c2ecf20Sopenharmony_ciMODULE_PARM_DESC(kcs_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* The states the KCS driver may be in. */
448c2ecf20Sopenharmony_cienum kcs_states {
458c2ecf20Sopenharmony_ci	/* The KCS interface is currently doing nothing. */
468c2ecf20Sopenharmony_ci	KCS_IDLE,
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/*
498c2ecf20Sopenharmony_ci	 * We are starting an operation.  The data is in the output
508c2ecf20Sopenharmony_ci	 * buffer, but nothing has been done to the interface yet.  This
518c2ecf20Sopenharmony_ci	 * was added to the state machine in the spec to wait for the
528c2ecf20Sopenharmony_ci	 * initial IBF.
538c2ecf20Sopenharmony_ci	 */
548c2ecf20Sopenharmony_ci	KCS_START_OP,
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	/* We have written a write cmd to the interface. */
578c2ecf20Sopenharmony_ci	KCS_WAIT_WRITE_START,
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/* We are writing bytes to the interface. */
608c2ecf20Sopenharmony_ci	KCS_WAIT_WRITE,
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/*
638c2ecf20Sopenharmony_ci	 * We have written the write end cmd to the interface, and
648c2ecf20Sopenharmony_ci	 * still need to write the last byte.
658c2ecf20Sopenharmony_ci	 */
668c2ecf20Sopenharmony_ci	KCS_WAIT_WRITE_END,
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	/* We are waiting to read data from the interface. */
698c2ecf20Sopenharmony_ci	KCS_WAIT_READ,
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * State to transition to the error handler, this was added to
738c2ecf20Sopenharmony_ci	 * the state machine in the spec to be sure IBF was there.
748c2ecf20Sopenharmony_ci	 */
758c2ecf20Sopenharmony_ci	KCS_ERROR0,
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/*
788c2ecf20Sopenharmony_ci	 * First stage error handler, wait for the interface to
798c2ecf20Sopenharmony_ci	 * respond.
808c2ecf20Sopenharmony_ci	 */
818c2ecf20Sopenharmony_ci	KCS_ERROR1,
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/*
848c2ecf20Sopenharmony_ci	 * The abort cmd has been written, wait for the interface to
858c2ecf20Sopenharmony_ci	 * respond.
868c2ecf20Sopenharmony_ci	 */
878c2ecf20Sopenharmony_ci	KCS_ERROR2,
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/*
908c2ecf20Sopenharmony_ci	 * We wrote some data to the interface, wait for it to switch
918c2ecf20Sopenharmony_ci	 * to read mode.
928c2ecf20Sopenharmony_ci	 */
938c2ecf20Sopenharmony_ci	KCS_ERROR3,
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* The hardware failed to follow the state machine. */
968c2ecf20Sopenharmony_ci	KCS_HOSED
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define MAX_KCS_READ_SIZE IPMI_MAX_MSG_LENGTH
1008c2ecf20Sopenharmony_ci#define MAX_KCS_WRITE_SIZE IPMI_MAX_MSG_LENGTH
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* Timeouts in microseconds. */
1038c2ecf20Sopenharmony_ci#define IBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
1048c2ecf20Sopenharmony_ci#define OBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
1058c2ecf20Sopenharmony_ci#define MAX_ERROR_RETRIES 10
1068c2ecf20Sopenharmony_ci#define ERROR0_OBF_WAIT_JIFFIES (2*HZ)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistruct si_sm_data {
1098c2ecf20Sopenharmony_ci	enum kcs_states  state;
1108c2ecf20Sopenharmony_ci	struct si_sm_io *io;
1118c2ecf20Sopenharmony_ci	unsigned char    write_data[MAX_KCS_WRITE_SIZE];
1128c2ecf20Sopenharmony_ci	int              write_pos;
1138c2ecf20Sopenharmony_ci	int              write_count;
1148c2ecf20Sopenharmony_ci	int              orig_write_count;
1158c2ecf20Sopenharmony_ci	unsigned char    read_data[MAX_KCS_READ_SIZE];
1168c2ecf20Sopenharmony_ci	int              read_pos;
1178c2ecf20Sopenharmony_ci	int	         truncated;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	unsigned int  error_retries;
1208c2ecf20Sopenharmony_ci	long          ibf_timeout;
1218c2ecf20Sopenharmony_ci	long          obf_timeout;
1228c2ecf20Sopenharmony_ci	unsigned long  error0_timeout;
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic unsigned int init_kcs_data(struct si_sm_data *kcs,
1268c2ecf20Sopenharmony_ci				  struct si_sm_io *io)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	kcs->state = KCS_IDLE;
1298c2ecf20Sopenharmony_ci	kcs->io = io;
1308c2ecf20Sopenharmony_ci	kcs->write_pos = 0;
1318c2ecf20Sopenharmony_ci	kcs->write_count = 0;
1328c2ecf20Sopenharmony_ci	kcs->orig_write_count = 0;
1338c2ecf20Sopenharmony_ci	kcs->read_pos = 0;
1348c2ecf20Sopenharmony_ci	kcs->error_retries = 0;
1358c2ecf20Sopenharmony_ci	kcs->truncated = 0;
1368c2ecf20Sopenharmony_ci	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
1378c2ecf20Sopenharmony_ci	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* Reserve 2 I/O bytes. */
1408c2ecf20Sopenharmony_ci	return 2;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic inline unsigned char read_status(struct si_sm_data *kcs)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	return kcs->io->inputb(kcs->io, 1);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic inline unsigned char read_data(struct si_sm_data *kcs)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	return kcs->io->inputb(kcs->io, 0);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	kcs->io->outputb(kcs->io, 1, data);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic inline void write_data(struct si_sm_data *kcs, unsigned char data)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	kcs->io->outputb(kcs->io, 0, data);
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* Control codes. */
1648c2ecf20Sopenharmony_ci#define KCS_GET_STATUS_ABORT	0x60
1658c2ecf20Sopenharmony_ci#define KCS_WRITE_START		0x61
1668c2ecf20Sopenharmony_ci#define KCS_WRITE_END		0x62
1678c2ecf20Sopenharmony_ci#define KCS_READ_BYTE		0x68
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/* Status bits. */
1708c2ecf20Sopenharmony_ci#define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
1718c2ecf20Sopenharmony_ci#define KCS_IDLE_STATE	0
1728c2ecf20Sopenharmony_ci#define KCS_READ_STATE	1
1738c2ecf20Sopenharmony_ci#define KCS_WRITE_STATE	2
1748c2ecf20Sopenharmony_ci#define KCS_ERROR_STATE	3
1758c2ecf20Sopenharmony_ci#define GET_STATUS_ATN(status) ((status) & 0x04)
1768c2ecf20Sopenharmony_ci#define GET_STATUS_IBF(status) ((status) & 0x02)
1778c2ecf20Sopenharmony_ci#define GET_STATUS_OBF(status) ((status) & 0x01)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic inline void write_next_byte(struct si_sm_data *kcs)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	write_data(kcs, kcs->write_data[kcs->write_pos]);
1838c2ecf20Sopenharmony_ci	(kcs->write_pos)++;
1848c2ecf20Sopenharmony_ci	(kcs->write_count)--;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	(kcs->error_retries)++;
1908c2ecf20Sopenharmony_ci	if (kcs->error_retries > MAX_ERROR_RETRIES) {
1918c2ecf20Sopenharmony_ci		if (kcs_debug & KCS_DEBUG_ENABLE)
1928c2ecf20Sopenharmony_ci			dev_dbg(kcs->io->dev, "ipmi_kcs_sm: kcs hosed: %s\n",
1938c2ecf20Sopenharmony_ci				reason);
1948c2ecf20Sopenharmony_ci		kcs->state = KCS_HOSED;
1958c2ecf20Sopenharmony_ci	} else {
1968c2ecf20Sopenharmony_ci		kcs->error0_timeout = jiffies + ERROR0_OBF_WAIT_JIFFIES;
1978c2ecf20Sopenharmony_ci		kcs->state = KCS_ERROR0;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic inline void read_next_byte(struct si_sm_data *kcs)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
2048c2ecf20Sopenharmony_ci		/* Throw the data away and mark it truncated. */
2058c2ecf20Sopenharmony_ci		read_data(kcs);
2068c2ecf20Sopenharmony_ci		kcs->truncated = 1;
2078c2ecf20Sopenharmony_ci	} else {
2088c2ecf20Sopenharmony_ci		kcs->read_data[kcs->read_pos] = read_data(kcs);
2098c2ecf20Sopenharmony_ci		(kcs->read_pos)++;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci	write_data(kcs, KCS_READ_BYTE);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
2158c2ecf20Sopenharmony_ci			    long time)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	if (GET_STATUS_IBF(status)) {
2188c2ecf20Sopenharmony_ci		kcs->ibf_timeout -= time;
2198c2ecf20Sopenharmony_ci		if (kcs->ibf_timeout < 0) {
2208c2ecf20Sopenharmony_ci			start_error_recovery(kcs, "IBF not ready in time");
2218c2ecf20Sopenharmony_ci			kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
2228c2ecf20Sopenharmony_ci			return 1;
2238c2ecf20Sopenharmony_ci		}
2248c2ecf20Sopenharmony_ci		return 0;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
2278c2ecf20Sopenharmony_ci	return 1;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic inline int check_obf(struct si_sm_data *kcs, unsigned char status,
2318c2ecf20Sopenharmony_ci			    long time)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	if (!GET_STATUS_OBF(status)) {
2348c2ecf20Sopenharmony_ci		kcs->obf_timeout -= time;
2358c2ecf20Sopenharmony_ci		if (kcs->obf_timeout < 0) {
2368c2ecf20Sopenharmony_ci			kcs->obf_timeout = OBF_RETRY_TIMEOUT;
2378c2ecf20Sopenharmony_ci			start_error_recovery(kcs, "OBF not ready in time");
2388c2ecf20Sopenharmony_ci			return 1;
2398c2ecf20Sopenharmony_ci		}
2408c2ecf20Sopenharmony_ci		return 0;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
2438c2ecf20Sopenharmony_ci	return 1;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic void clear_obf(struct si_sm_data *kcs, unsigned char status)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	if (GET_STATUS_OBF(status))
2498c2ecf20Sopenharmony_ci		read_data(kcs);
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic void restart_kcs_transaction(struct si_sm_data *kcs)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	kcs->write_count = kcs->orig_write_count;
2558c2ecf20Sopenharmony_ci	kcs->write_pos = 0;
2568c2ecf20Sopenharmony_ci	kcs->read_pos = 0;
2578c2ecf20Sopenharmony_ci	kcs->state = KCS_WAIT_WRITE_START;
2588c2ecf20Sopenharmony_ci	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
2598c2ecf20Sopenharmony_ci	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
2608c2ecf20Sopenharmony_ci	write_cmd(kcs, KCS_WRITE_START);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
2648c2ecf20Sopenharmony_ci				 unsigned int size)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	unsigned int i;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	if (size < 2)
2698c2ecf20Sopenharmony_ci		return IPMI_REQ_LEN_INVALID_ERR;
2708c2ecf20Sopenharmony_ci	if (size > MAX_KCS_WRITE_SIZE)
2718c2ecf20Sopenharmony_ci		return IPMI_REQ_LEN_EXCEEDED_ERR;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) {
2748c2ecf20Sopenharmony_ci		dev_warn(kcs->io->dev, "KCS in invalid state %d\n", kcs->state);
2758c2ecf20Sopenharmony_ci		return IPMI_NOT_IN_MY_STATE_ERR;
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (kcs_debug & KCS_DEBUG_MSG) {
2798c2ecf20Sopenharmony_ci		dev_dbg(kcs->io->dev, "%s -", __func__);
2808c2ecf20Sopenharmony_ci		for (i = 0; i < size; i++)
2818c2ecf20Sopenharmony_ci			pr_cont(" %02x", data[i]);
2828c2ecf20Sopenharmony_ci		pr_cont("\n");
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci	kcs->error_retries = 0;
2858c2ecf20Sopenharmony_ci	memcpy(kcs->write_data, data, size);
2868c2ecf20Sopenharmony_ci	kcs->write_count = size;
2878c2ecf20Sopenharmony_ci	kcs->orig_write_count = size;
2888c2ecf20Sopenharmony_ci	kcs->write_pos = 0;
2898c2ecf20Sopenharmony_ci	kcs->read_pos = 0;
2908c2ecf20Sopenharmony_ci	kcs->state = KCS_START_OP;
2918c2ecf20Sopenharmony_ci	kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
2928c2ecf20Sopenharmony_ci	kcs->obf_timeout = OBF_RETRY_TIMEOUT;
2938c2ecf20Sopenharmony_ci	return 0;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
2978c2ecf20Sopenharmony_ci			  unsigned int length)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	if (length < kcs->read_pos) {
3008c2ecf20Sopenharmony_ci		kcs->read_pos = length;
3018c2ecf20Sopenharmony_ci		kcs->truncated = 1;
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	memcpy(data, kcs->read_data, kcs->read_pos);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	if ((length >= 3) && (kcs->read_pos < 3)) {
3078c2ecf20Sopenharmony_ci		/* Guarantee that we return at least 3 bytes, with an
3088c2ecf20Sopenharmony_ci		   error in the third byte if it is too short. */
3098c2ecf20Sopenharmony_ci		data[2] = IPMI_ERR_UNSPECIFIED;
3108c2ecf20Sopenharmony_ci		kcs->read_pos = 3;
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci	if (kcs->truncated) {
3138c2ecf20Sopenharmony_ci		/*
3148c2ecf20Sopenharmony_ci		 * Report a truncated error.  We might overwrite
3158c2ecf20Sopenharmony_ci		 * another error, but that's too bad, the user needs
3168c2ecf20Sopenharmony_ci		 * to know it was truncated.
3178c2ecf20Sopenharmony_ci		 */
3188c2ecf20Sopenharmony_ci		data[2] = IPMI_ERR_MSG_TRUNCATED;
3198c2ecf20Sopenharmony_ci		kcs->truncated = 0;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	return kcs->read_pos;
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci/*
3268c2ecf20Sopenharmony_ci * This implements the state machine defined in the IPMI manual, see
3278c2ecf20Sopenharmony_ci * that for details on how this works.  Divide that flowchart into
3288c2ecf20Sopenharmony_ci * sections delimited by "Wait for IBF" and this will become clear.
3298c2ecf20Sopenharmony_ci */
3308c2ecf20Sopenharmony_cistatic enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	unsigned char status;
3338c2ecf20Sopenharmony_ci	unsigned char state;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	status = read_status(kcs);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (kcs_debug & KCS_DEBUG_STATES)
3388c2ecf20Sopenharmony_ci		dev_dbg(kcs->io->dev,
3398c2ecf20Sopenharmony_ci			"KCS: State = %d, %x\n", kcs->state, status);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* All states wait for ibf, so just do it here. */
3428c2ecf20Sopenharmony_ci	if (!check_ibf(kcs, status, time))
3438c2ecf20Sopenharmony_ci		return SI_SM_CALL_WITH_DELAY;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* Just about everything looks at the KCS state, so grab that, too. */
3468c2ecf20Sopenharmony_ci	state = GET_STATUS_STATE(status);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	switch (kcs->state) {
3498c2ecf20Sopenharmony_ci	case KCS_IDLE:
3508c2ecf20Sopenharmony_ci		/* If there's and interrupt source, turn it off. */
3518c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		if (GET_STATUS_ATN(status))
3548c2ecf20Sopenharmony_ci			return SI_SM_ATTN;
3558c2ecf20Sopenharmony_ci		else
3568c2ecf20Sopenharmony_ci			return SI_SM_IDLE;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	case KCS_START_OP:
3598c2ecf20Sopenharmony_ci		if (state != KCS_IDLE_STATE) {
3608c2ecf20Sopenharmony_ci			start_error_recovery(kcs,
3618c2ecf20Sopenharmony_ci					     "State machine not idle at start");
3628c2ecf20Sopenharmony_ci			break;
3638c2ecf20Sopenharmony_ci		}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
3668c2ecf20Sopenharmony_ci		write_cmd(kcs, KCS_WRITE_START);
3678c2ecf20Sopenharmony_ci		kcs->state = KCS_WAIT_WRITE_START;
3688c2ecf20Sopenharmony_ci		break;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	case KCS_WAIT_WRITE_START:
3718c2ecf20Sopenharmony_ci		if (state != KCS_WRITE_STATE) {
3728c2ecf20Sopenharmony_ci			start_error_recovery(
3738c2ecf20Sopenharmony_ci				kcs,
3748c2ecf20Sopenharmony_ci				"Not in write state at write start");
3758c2ecf20Sopenharmony_ci			break;
3768c2ecf20Sopenharmony_ci		}
3778c2ecf20Sopenharmony_ci		read_data(kcs);
3788c2ecf20Sopenharmony_ci		if (kcs->write_count == 1) {
3798c2ecf20Sopenharmony_ci			write_cmd(kcs, KCS_WRITE_END);
3808c2ecf20Sopenharmony_ci			kcs->state = KCS_WAIT_WRITE_END;
3818c2ecf20Sopenharmony_ci		} else {
3828c2ecf20Sopenharmony_ci			write_next_byte(kcs);
3838c2ecf20Sopenharmony_ci			kcs->state = KCS_WAIT_WRITE;
3848c2ecf20Sopenharmony_ci		}
3858c2ecf20Sopenharmony_ci		break;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	case KCS_WAIT_WRITE:
3888c2ecf20Sopenharmony_ci		if (state != KCS_WRITE_STATE) {
3898c2ecf20Sopenharmony_ci			start_error_recovery(kcs,
3908c2ecf20Sopenharmony_ci					     "Not in write state for write");
3918c2ecf20Sopenharmony_ci			break;
3928c2ecf20Sopenharmony_ci		}
3938c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
3948c2ecf20Sopenharmony_ci		if (kcs->write_count == 1) {
3958c2ecf20Sopenharmony_ci			write_cmd(kcs, KCS_WRITE_END);
3968c2ecf20Sopenharmony_ci			kcs->state = KCS_WAIT_WRITE_END;
3978c2ecf20Sopenharmony_ci		} else {
3988c2ecf20Sopenharmony_ci			write_next_byte(kcs);
3998c2ecf20Sopenharmony_ci		}
4008c2ecf20Sopenharmony_ci		break;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	case KCS_WAIT_WRITE_END:
4038c2ecf20Sopenharmony_ci		if (state != KCS_WRITE_STATE) {
4048c2ecf20Sopenharmony_ci			start_error_recovery(kcs,
4058c2ecf20Sopenharmony_ci					     "Not in write state"
4068c2ecf20Sopenharmony_ci					     " for write end");
4078c2ecf20Sopenharmony_ci			break;
4088c2ecf20Sopenharmony_ci		}
4098c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
4108c2ecf20Sopenharmony_ci		write_next_byte(kcs);
4118c2ecf20Sopenharmony_ci		kcs->state = KCS_WAIT_READ;
4128c2ecf20Sopenharmony_ci		break;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	case KCS_WAIT_READ:
4158c2ecf20Sopenharmony_ci		if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
4168c2ecf20Sopenharmony_ci			start_error_recovery(
4178c2ecf20Sopenharmony_ci				kcs,
4188c2ecf20Sopenharmony_ci				"Not in read or idle in read state");
4198c2ecf20Sopenharmony_ci			break;
4208c2ecf20Sopenharmony_ci		}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci		if (state == KCS_READ_STATE) {
4238c2ecf20Sopenharmony_ci			if (!check_obf(kcs, status, time))
4248c2ecf20Sopenharmony_ci				return SI_SM_CALL_WITH_DELAY;
4258c2ecf20Sopenharmony_ci			read_next_byte(kcs);
4268c2ecf20Sopenharmony_ci		} else {
4278c2ecf20Sopenharmony_ci			/*
4288c2ecf20Sopenharmony_ci			 * We don't implement this exactly like the state
4298c2ecf20Sopenharmony_ci			 * machine in the spec.  Some broken hardware
4308c2ecf20Sopenharmony_ci			 * does not write the final dummy byte to the
4318c2ecf20Sopenharmony_ci			 * read register.  Thus obf will never go high
4328c2ecf20Sopenharmony_ci			 * here.  We just go straight to idle, and we
4338c2ecf20Sopenharmony_ci			 * handle clearing out obf in idle state if it
4348c2ecf20Sopenharmony_ci			 * happens to come in.
4358c2ecf20Sopenharmony_ci			 */
4368c2ecf20Sopenharmony_ci			clear_obf(kcs, status);
4378c2ecf20Sopenharmony_ci			kcs->orig_write_count = 0;
4388c2ecf20Sopenharmony_ci			kcs->state = KCS_IDLE;
4398c2ecf20Sopenharmony_ci			return SI_SM_TRANSACTION_COMPLETE;
4408c2ecf20Sopenharmony_ci		}
4418c2ecf20Sopenharmony_ci		break;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	case KCS_ERROR0:
4448c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
4458c2ecf20Sopenharmony_ci		status = read_status(kcs);
4468c2ecf20Sopenharmony_ci		if (GET_STATUS_OBF(status))
4478c2ecf20Sopenharmony_ci			/* controller isn't responding */
4488c2ecf20Sopenharmony_ci			if (time_before(jiffies, kcs->error0_timeout))
4498c2ecf20Sopenharmony_ci				return SI_SM_CALL_WITH_TICK_DELAY;
4508c2ecf20Sopenharmony_ci		write_cmd(kcs, KCS_GET_STATUS_ABORT);
4518c2ecf20Sopenharmony_ci		kcs->state = KCS_ERROR1;
4528c2ecf20Sopenharmony_ci		break;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	case KCS_ERROR1:
4558c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
4568c2ecf20Sopenharmony_ci		write_data(kcs, 0);
4578c2ecf20Sopenharmony_ci		kcs->state = KCS_ERROR2;
4588c2ecf20Sopenharmony_ci		break;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	case KCS_ERROR2:
4618c2ecf20Sopenharmony_ci		if (state != KCS_READ_STATE) {
4628c2ecf20Sopenharmony_ci			start_error_recovery(kcs,
4638c2ecf20Sopenharmony_ci					     "Not in read state for error2");
4648c2ecf20Sopenharmony_ci			break;
4658c2ecf20Sopenharmony_ci		}
4668c2ecf20Sopenharmony_ci		if (!check_obf(kcs, status, time))
4678c2ecf20Sopenharmony_ci			return SI_SM_CALL_WITH_DELAY;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
4708c2ecf20Sopenharmony_ci		write_data(kcs, KCS_READ_BYTE);
4718c2ecf20Sopenharmony_ci		kcs->state = KCS_ERROR3;
4728c2ecf20Sopenharmony_ci		break;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	case KCS_ERROR3:
4758c2ecf20Sopenharmony_ci		if (state != KCS_IDLE_STATE) {
4768c2ecf20Sopenharmony_ci			start_error_recovery(kcs,
4778c2ecf20Sopenharmony_ci					     "Not in idle state for error3");
4788c2ecf20Sopenharmony_ci			break;
4798c2ecf20Sopenharmony_ci		}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci		if (!check_obf(kcs, status, time))
4828c2ecf20Sopenharmony_ci			return SI_SM_CALL_WITH_DELAY;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		clear_obf(kcs, status);
4858c2ecf20Sopenharmony_ci		if (kcs->orig_write_count) {
4868c2ecf20Sopenharmony_ci			restart_kcs_transaction(kcs);
4878c2ecf20Sopenharmony_ci		} else {
4888c2ecf20Sopenharmony_ci			kcs->state = KCS_IDLE;
4898c2ecf20Sopenharmony_ci			return SI_SM_TRANSACTION_COMPLETE;
4908c2ecf20Sopenharmony_ci		}
4918c2ecf20Sopenharmony_ci		break;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	case KCS_HOSED:
4948c2ecf20Sopenharmony_ci		break;
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	if (kcs->state == KCS_HOSED) {
4988c2ecf20Sopenharmony_ci		init_kcs_data(kcs, kcs->io);
4998c2ecf20Sopenharmony_ci		return SI_SM_HOSED;
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	return SI_SM_CALL_WITHOUT_DELAY;
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic int kcs_size(void)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	return sizeof(struct si_sm_data);
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic int kcs_detect(struct si_sm_data *kcs)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	/*
5138c2ecf20Sopenharmony_ci	 * It's impossible for the KCS status register to be all 1's,
5148c2ecf20Sopenharmony_ci	 * (assuming a properly functioning, self-initialized BMC)
5158c2ecf20Sopenharmony_ci	 * but that's what you get from reading a bogus address, so we
5168c2ecf20Sopenharmony_ci	 * test that first.
5178c2ecf20Sopenharmony_ci	 */
5188c2ecf20Sopenharmony_ci	if (read_status(kcs) == 0xff)
5198c2ecf20Sopenharmony_ci		return 1;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return 0;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic void kcs_cleanup(struct si_sm_data *kcs)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ciconst struct si_sm_handlers kcs_smi_handlers = {
5298c2ecf20Sopenharmony_ci	.init_data         = init_kcs_data,
5308c2ecf20Sopenharmony_ci	.start_transaction = start_kcs_transaction,
5318c2ecf20Sopenharmony_ci	.get_result        = get_kcs_result,
5328c2ecf20Sopenharmony_ci	.event             = kcs_event,
5338c2ecf20Sopenharmony_ci	.detect            = kcs_detect,
5348c2ecf20Sopenharmony_ci	.cleanup           = kcs_cleanup,
5358c2ecf20Sopenharmony_ci	.size              = kcs_size,
5368c2ecf20Sopenharmony_ci};
537