18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  ipmi_bt_sm.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part
68c2ecf20Sopenharmony_ci *  of the driver architecture at http://sourceforge.net/projects/openipmi
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  Author:	Rocky Craig <first.last@hp.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define DEBUG /* So dev_dbg() is always available. */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h> /* For printk. */
148c2ecf20Sopenharmony_ci#include <linux/string.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
178c2ecf20Sopenharmony_ci#include <linux/ipmi_msgdefs.h>		/* for completion codes */
188c2ecf20Sopenharmony_ci#include "ipmi_si_sm.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define BT_DEBUG_OFF	0	/* Used in production */
218c2ecf20Sopenharmony_ci#define BT_DEBUG_ENABLE	1	/* Generic messages */
228c2ecf20Sopenharmony_ci#define BT_DEBUG_MSG	2	/* Prints all request/response buffers */
238c2ecf20Sopenharmony_ci#define BT_DEBUG_STATES	4	/* Verbose look at state changes */
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * BT_DEBUG_OFF must be zero to correspond to the default uninitialized
268c2ecf20Sopenharmony_ci * value
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int bt_debug; /* 0 == BT_DEBUG_OFF */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cimodule_param(bt_debug, int, 0644);
328c2ecf20Sopenharmony_ciMODULE_PARM_DESC(bt_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Typical "Get BT Capabilities" values are 2-3 retries, 5-10 seconds,
368c2ecf20Sopenharmony_ci * and 64 byte buffers.  However, one HP implementation wants 255 bytes of
378c2ecf20Sopenharmony_ci * buffer (with a documented message of 160 bytes) so go for the max.
388c2ecf20Sopenharmony_ci * Since the Open IPMI architecture is single-message oriented at this
398c2ecf20Sopenharmony_ci * stage, the queue depth of BT is of no concern.
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define BT_NORMAL_TIMEOUT	5	/* seconds */
438c2ecf20Sopenharmony_ci#define BT_NORMAL_RETRY_LIMIT	2
448c2ecf20Sopenharmony_ci#define BT_RESET_DELAY		6	/* seconds after warm reset */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/*
478c2ecf20Sopenharmony_ci * States are written in chronological order and usually cover
488c2ecf20Sopenharmony_ci * multiple rows of the state table discussion in the IPMI spec.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cienum bt_states {
528c2ecf20Sopenharmony_ci	BT_STATE_IDLE = 0,	/* Order is critical in this list */
538c2ecf20Sopenharmony_ci	BT_STATE_XACTION_START,
548c2ecf20Sopenharmony_ci	BT_STATE_WRITE_BYTES,
558c2ecf20Sopenharmony_ci	BT_STATE_WRITE_CONSUME,
568c2ecf20Sopenharmony_ci	BT_STATE_READ_WAIT,
578c2ecf20Sopenharmony_ci	BT_STATE_CLEAR_B2H,
588c2ecf20Sopenharmony_ci	BT_STATE_READ_BYTES,
598c2ecf20Sopenharmony_ci	BT_STATE_RESET1,	/* These must come last */
608c2ecf20Sopenharmony_ci	BT_STATE_RESET2,
618c2ecf20Sopenharmony_ci	BT_STATE_RESET3,
628c2ecf20Sopenharmony_ci	BT_STATE_RESTART,
638c2ecf20Sopenharmony_ci	BT_STATE_PRINTME,
648c2ecf20Sopenharmony_ci	BT_STATE_LONG_BUSY	/* BT doesn't get hosed :-) */
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * Macros seen at the end of state "case" blocks.  They help with legibility
698c2ecf20Sopenharmony_ci * and debugging.
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define BT_STATE_CHANGE(X, Y) { bt->state = X; return Y; }
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define BT_SI_SM_RETURN(Y)   { last_printed = BT_STATE_PRINTME; return Y; }
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistruct si_sm_data {
778c2ecf20Sopenharmony_ci	enum bt_states	state;
788c2ecf20Sopenharmony_ci	unsigned char	seq;		/* BT sequence number */
798c2ecf20Sopenharmony_ci	struct si_sm_io	*io;
808c2ecf20Sopenharmony_ci	unsigned char	write_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
818c2ecf20Sopenharmony_ci	int		write_count;
828c2ecf20Sopenharmony_ci	unsigned char	read_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
838c2ecf20Sopenharmony_ci	int		read_count;
848c2ecf20Sopenharmony_ci	int		truncated;
858c2ecf20Sopenharmony_ci	long		timeout;	/* microseconds countdown */
868c2ecf20Sopenharmony_ci	int		error_retries;	/* end of "common" fields */
878c2ecf20Sopenharmony_ci	int		nonzero_status;	/* hung BMCs stay all 0 */
888c2ecf20Sopenharmony_ci	enum bt_states	complete;	/* to divert the state machine */
898c2ecf20Sopenharmony_ci	long		BT_CAP_req2rsp;
908c2ecf20Sopenharmony_ci	int		BT_CAP_retries;	/* Recommended retries */
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define BT_CLR_WR_PTR	0x01	/* See IPMI 1.5 table 11.6.4 */
948c2ecf20Sopenharmony_ci#define BT_CLR_RD_PTR	0x02
958c2ecf20Sopenharmony_ci#define BT_H2B_ATN	0x04
968c2ecf20Sopenharmony_ci#define BT_B2H_ATN	0x08
978c2ecf20Sopenharmony_ci#define BT_SMS_ATN	0x10
988c2ecf20Sopenharmony_ci#define BT_OEM0		0x20
998c2ecf20Sopenharmony_ci#define BT_H_BUSY	0x40
1008c2ecf20Sopenharmony_ci#define BT_B_BUSY	0x80
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/*
1038c2ecf20Sopenharmony_ci * Some bits are toggled on each write: write once to set it, once
1048c2ecf20Sopenharmony_ci * more to clear it; writing a zero does nothing.  To absolutely
1058c2ecf20Sopenharmony_ci * clear it, check its state and write if set.  This avoids the "get
1068c2ecf20Sopenharmony_ci * current then use as mask" scheme to modify one bit.  Note that the
1078c2ecf20Sopenharmony_ci * variable "bt" is hardcoded into these macros.
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define BT_STATUS	bt->io->inputb(bt->io, 0)
1118c2ecf20Sopenharmony_ci#define BT_CONTROL(x)	bt->io->outputb(bt->io, 0, x)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define BMC2HOST	bt->io->inputb(bt->io, 1)
1148c2ecf20Sopenharmony_ci#define HOST2BMC(x)	bt->io->outputb(bt->io, 1, x)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define BT_INTMASK_R	bt->io->inputb(bt->io, 2)
1178c2ecf20Sopenharmony_ci#define BT_INTMASK_W(x)	bt->io->outputb(bt->io, 2, x)
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/*
1208c2ecf20Sopenharmony_ci * Convenience routines for debugging.  These are not multi-open safe!
1218c2ecf20Sopenharmony_ci * Note the macros have hardcoded variables in them.
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic char *state2txt(unsigned char state)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	switch (state) {
1278c2ecf20Sopenharmony_ci	case BT_STATE_IDLE:		return("IDLE");
1288c2ecf20Sopenharmony_ci	case BT_STATE_XACTION_START:	return("XACTION");
1298c2ecf20Sopenharmony_ci	case BT_STATE_WRITE_BYTES:	return("WR_BYTES");
1308c2ecf20Sopenharmony_ci	case BT_STATE_WRITE_CONSUME:	return("WR_CONSUME");
1318c2ecf20Sopenharmony_ci	case BT_STATE_READ_WAIT:	return("RD_WAIT");
1328c2ecf20Sopenharmony_ci	case BT_STATE_CLEAR_B2H:	return("CLEAR_B2H");
1338c2ecf20Sopenharmony_ci	case BT_STATE_READ_BYTES:	return("RD_BYTES");
1348c2ecf20Sopenharmony_ci	case BT_STATE_RESET1:		return("RESET1");
1358c2ecf20Sopenharmony_ci	case BT_STATE_RESET2:		return("RESET2");
1368c2ecf20Sopenharmony_ci	case BT_STATE_RESET3:		return("RESET3");
1378c2ecf20Sopenharmony_ci	case BT_STATE_RESTART:		return("RESTART");
1388c2ecf20Sopenharmony_ci	case BT_STATE_LONG_BUSY:	return("LONG_BUSY");
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci	return("BAD STATE");
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci#define STATE2TXT state2txt(bt->state)
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic char *status2txt(unsigned char status)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	/*
1478c2ecf20Sopenharmony_ci	 * This cannot be called by two threads at the same time and
1488c2ecf20Sopenharmony_ci	 * the buffer is always consumed immediately, so the static is
1498c2ecf20Sopenharmony_ci	 * safe to use.
1508c2ecf20Sopenharmony_ci	 */
1518c2ecf20Sopenharmony_ci	static char buf[40];
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	strcpy(buf, "[ ");
1548c2ecf20Sopenharmony_ci	if (status & BT_B_BUSY)
1558c2ecf20Sopenharmony_ci		strcat(buf, "B_BUSY ");
1568c2ecf20Sopenharmony_ci	if (status & BT_H_BUSY)
1578c2ecf20Sopenharmony_ci		strcat(buf, "H_BUSY ");
1588c2ecf20Sopenharmony_ci	if (status & BT_OEM0)
1598c2ecf20Sopenharmony_ci		strcat(buf, "OEM0 ");
1608c2ecf20Sopenharmony_ci	if (status & BT_SMS_ATN)
1618c2ecf20Sopenharmony_ci		strcat(buf, "SMS ");
1628c2ecf20Sopenharmony_ci	if (status & BT_B2H_ATN)
1638c2ecf20Sopenharmony_ci		strcat(buf, "B2H ");
1648c2ecf20Sopenharmony_ci	if (status & BT_H2B_ATN)
1658c2ecf20Sopenharmony_ci		strcat(buf, "H2B ");
1668c2ecf20Sopenharmony_ci	strcat(buf, "]");
1678c2ecf20Sopenharmony_ci	return buf;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci#define STATUS2TXT status2txt(status)
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci/* called externally at insmod time, and internally on cleanup */
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic unsigned int bt_init_data(struct si_sm_data *bt, struct si_sm_io *io)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	memset(bt, 0, sizeof(struct si_sm_data));
1768c2ecf20Sopenharmony_ci	if (bt->io != io) {
1778c2ecf20Sopenharmony_ci		/* external: one-time only things */
1788c2ecf20Sopenharmony_ci		bt->io = io;
1798c2ecf20Sopenharmony_ci		bt->seq = 0;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci	bt->state = BT_STATE_IDLE;	/* start here */
1828c2ecf20Sopenharmony_ci	bt->complete = BT_STATE_IDLE;	/* end here */
1838c2ecf20Sopenharmony_ci	bt->BT_CAP_req2rsp = BT_NORMAL_TIMEOUT * USEC_PER_SEC;
1848c2ecf20Sopenharmony_ci	bt->BT_CAP_retries = BT_NORMAL_RETRY_LIMIT;
1858c2ecf20Sopenharmony_ci	return 3; /* We claim 3 bytes of space; ought to check SPMI table */
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* Jam a completion code (probably an error) into a response */
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void force_result(struct si_sm_data *bt, unsigned char completion_code)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	bt->read_data[0] = 4;				/* # following bytes */
1938c2ecf20Sopenharmony_ci	bt->read_data[1] = bt->write_data[1] | 4;	/* Odd NetFn/LUN */
1948c2ecf20Sopenharmony_ci	bt->read_data[2] = bt->write_data[2];		/* seq (ignored) */
1958c2ecf20Sopenharmony_ci	bt->read_data[3] = bt->write_data[3];		/* Command */
1968c2ecf20Sopenharmony_ci	bt->read_data[4] = completion_code;
1978c2ecf20Sopenharmony_ci	bt->read_count = 5;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* The upper state machine starts here */
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int bt_start_transaction(struct si_sm_data *bt,
2038c2ecf20Sopenharmony_ci				unsigned char *data,
2048c2ecf20Sopenharmony_ci				unsigned int size)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	unsigned int i;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (size < 2)
2098c2ecf20Sopenharmony_ci		return IPMI_REQ_LEN_INVALID_ERR;
2108c2ecf20Sopenharmony_ci	if (size > IPMI_MAX_MSG_LENGTH)
2118c2ecf20Sopenharmony_ci		return IPMI_REQ_LEN_EXCEEDED_ERR;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (bt->state == BT_STATE_LONG_BUSY)
2148c2ecf20Sopenharmony_ci		return IPMI_NODE_BUSY_ERR;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (bt->state != BT_STATE_IDLE) {
2178c2ecf20Sopenharmony_ci		dev_warn(bt->io->dev, "BT in invalid state %d\n", bt->state);
2188c2ecf20Sopenharmony_ci		return IPMI_NOT_IN_MY_STATE_ERR;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
2228c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev, "+++++++++++++++++ New command\n");
2238c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev, "NetFn/LUN CMD [%d data]:", size - 2);
2248c2ecf20Sopenharmony_ci		for (i = 0; i < size; i ++)
2258c2ecf20Sopenharmony_ci			pr_cont(" %02x", data[i]);
2268c2ecf20Sopenharmony_ci		pr_cont("\n");
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci	bt->write_data[0] = size + 1;	/* all data plus seq byte */
2298c2ecf20Sopenharmony_ci	bt->write_data[1] = *data;	/* NetFn/LUN */
2308c2ecf20Sopenharmony_ci	bt->write_data[2] = bt->seq++;
2318c2ecf20Sopenharmony_ci	memcpy(bt->write_data + 3, data + 1, size - 1);
2328c2ecf20Sopenharmony_ci	bt->write_count = size + 2;
2338c2ecf20Sopenharmony_ci	bt->error_retries = 0;
2348c2ecf20Sopenharmony_ci	bt->nonzero_status = 0;
2358c2ecf20Sopenharmony_ci	bt->truncated = 0;
2368c2ecf20Sopenharmony_ci	bt->state = BT_STATE_XACTION_START;
2378c2ecf20Sopenharmony_ci	bt->timeout = bt->BT_CAP_req2rsp;
2388c2ecf20Sopenharmony_ci	force_result(bt, IPMI_ERR_UNSPECIFIED);
2398c2ecf20Sopenharmony_ci	return 0;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci/*
2438c2ecf20Sopenharmony_ci * After the upper state machine has been told SI_SM_TRANSACTION_COMPLETE
2448c2ecf20Sopenharmony_ci * it calls this.  Strip out the length and seq bytes.
2458c2ecf20Sopenharmony_ci */
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic int bt_get_result(struct si_sm_data *bt,
2488c2ecf20Sopenharmony_ci			 unsigned char *data,
2498c2ecf20Sopenharmony_ci			 unsigned int length)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	int i, msg_len;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	msg_len = bt->read_count - 2;		/* account for length & seq */
2548c2ecf20Sopenharmony_ci	if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
2558c2ecf20Sopenharmony_ci		force_result(bt, IPMI_ERR_UNSPECIFIED);
2568c2ecf20Sopenharmony_ci		msg_len = 3;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci	data[0] = bt->read_data[1];
2598c2ecf20Sopenharmony_ci	data[1] = bt->read_data[3];
2608c2ecf20Sopenharmony_ci	if (length < msg_len || bt->truncated) {
2618c2ecf20Sopenharmony_ci		data[2] = IPMI_ERR_MSG_TRUNCATED;
2628c2ecf20Sopenharmony_ci		msg_len = 3;
2638c2ecf20Sopenharmony_ci	} else
2648c2ecf20Sopenharmony_ci		memcpy(data + 2, bt->read_data + 4, msg_len - 2);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
2678c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev, "result %d bytes:", msg_len);
2688c2ecf20Sopenharmony_ci		for (i = 0; i < msg_len; i++)
2698c2ecf20Sopenharmony_ci			pr_cont(" %02x", data[i]);
2708c2ecf20Sopenharmony_ci		pr_cont("\n");
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci	return msg_len;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci/* This bit's functionality is optional */
2768c2ecf20Sopenharmony_ci#define BT_BMC_HWRST	0x80
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic void reset_flags(struct si_sm_data *bt)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	if (bt_debug)
2818c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev, "flag reset %s\n", status2txt(BT_STATUS));
2828c2ecf20Sopenharmony_ci	if (BT_STATUS & BT_H_BUSY)
2838c2ecf20Sopenharmony_ci		BT_CONTROL(BT_H_BUSY);	/* force clear */
2848c2ecf20Sopenharmony_ci	BT_CONTROL(BT_CLR_WR_PTR);	/* always reset */
2858c2ecf20Sopenharmony_ci	BT_CONTROL(BT_SMS_ATN);		/* always clear */
2868c2ecf20Sopenharmony_ci	BT_INTMASK_W(BT_BMC_HWRST);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci/*
2908c2ecf20Sopenharmony_ci * Get rid of an unwanted/stale response.  This should only be needed for
2918c2ecf20Sopenharmony_ci * BMCs that support multiple outstanding requests.
2928c2ecf20Sopenharmony_ci */
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic void drain_BMC2HOST(struct si_sm_data *bt)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	int i, size;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	if (!(BT_STATUS & BT_B2H_ATN)) 	/* Not signalling a response */
2998c2ecf20Sopenharmony_ci		return;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	BT_CONTROL(BT_H_BUSY);		/* now set */
3028c2ecf20Sopenharmony_ci	BT_CONTROL(BT_B2H_ATN);		/* always clear */
3038c2ecf20Sopenharmony_ci	BT_STATUS;			/* pause */
3048c2ecf20Sopenharmony_ci	BT_CONTROL(BT_B2H_ATN);		/* some BMCs are stubborn */
3058c2ecf20Sopenharmony_ci	BT_CONTROL(BT_CLR_RD_PTR);	/* always reset */
3068c2ecf20Sopenharmony_ci	if (bt_debug)
3078c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev, "stale response %s; ",
3088c2ecf20Sopenharmony_ci			status2txt(BT_STATUS));
3098c2ecf20Sopenharmony_ci	size = BMC2HOST;
3108c2ecf20Sopenharmony_ci	for (i = 0; i < size ; i++)
3118c2ecf20Sopenharmony_ci		BMC2HOST;
3128c2ecf20Sopenharmony_ci	BT_CONTROL(BT_H_BUSY);		/* now clear */
3138c2ecf20Sopenharmony_ci	if (bt_debug)
3148c2ecf20Sopenharmony_ci		pr_cont("drained %d bytes\n", size + 1);
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic inline void write_all_bytes(struct si_sm_data *bt)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	int i;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
3228c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev, "write %d bytes seq=0x%02X",
3238c2ecf20Sopenharmony_ci			bt->write_count, bt->seq);
3248c2ecf20Sopenharmony_ci		for (i = 0; i < bt->write_count; i++)
3258c2ecf20Sopenharmony_ci			pr_cont(" %02x", bt->write_data[i]);
3268c2ecf20Sopenharmony_ci		pr_cont("\n");
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci	for (i = 0; i < bt->write_count; i++)
3298c2ecf20Sopenharmony_ci		HOST2BMC(bt->write_data[i]);
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic inline int read_all_bytes(struct si_sm_data *bt)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	unsigned int i;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/*
3378c2ecf20Sopenharmony_ci	 * length is "framing info", minimum = 4: NetFn, Seq, Cmd, cCode.
3388c2ecf20Sopenharmony_ci	 * Keep layout of first four bytes aligned with write_data[]
3398c2ecf20Sopenharmony_ci	 */
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	bt->read_data[0] = BMC2HOST;
3428c2ecf20Sopenharmony_ci	bt->read_count = bt->read_data[0];
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (bt->read_count < 4 || bt->read_count >= IPMI_MAX_MSG_LENGTH) {
3458c2ecf20Sopenharmony_ci		if (bt_debug & BT_DEBUG_MSG)
3468c2ecf20Sopenharmony_ci			dev_dbg(bt->io->dev,
3478c2ecf20Sopenharmony_ci				"bad raw rsp len=%d\n", bt->read_count);
3488c2ecf20Sopenharmony_ci		bt->truncated = 1;
3498c2ecf20Sopenharmony_ci		return 1;	/* let next XACTION START clean it up */
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci	for (i = 1; i <= bt->read_count; i++)
3528c2ecf20Sopenharmony_ci		bt->read_data[i] = BMC2HOST;
3538c2ecf20Sopenharmony_ci	bt->read_count++;	/* Account internally for length byte */
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
3568c2ecf20Sopenharmony_ci		int max = bt->read_count;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev,
3598c2ecf20Sopenharmony_ci			"got %d bytes seq=0x%02X", max, bt->read_data[2]);
3608c2ecf20Sopenharmony_ci		if (max > 16)
3618c2ecf20Sopenharmony_ci			max = 16;
3628c2ecf20Sopenharmony_ci		for (i = 0; i < max; i++)
3638c2ecf20Sopenharmony_ci			pr_cont(" %02x", bt->read_data[i]);
3648c2ecf20Sopenharmony_ci		pr_cont("%s\n", bt->read_count == max ? "" : " ...");
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/* per the spec, the (NetFn[1], Seq[2], Cmd[3]) tuples must match */
3688c2ecf20Sopenharmony_ci	if ((bt->read_data[3] == bt->write_data[3]) &&
3698c2ecf20Sopenharmony_ci	    (bt->read_data[2] == bt->write_data[2]) &&
3708c2ecf20Sopenharmony_ci	    ((bt->read_data[1] & 0xF8) == (bt->write_data[1] & 0xF8)))
3718c2ecf20Sopenharmony_ci			return 1;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG)
3748c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev,
3758c2ecf20Sopenharmony_ci			"IPMI BT: bad packet: want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
3768c2ecf20Sopenharmony_ci			bt->write_data[1] | 0x04, bt->write_data[2],
3778c2ecf20Sopenharmony_ci			bt->write_data[3],
3788c2ecf20Sopenharmony_ci			bt->read_data[1],  bt->read_data[2],  bt->read_data[3]);
3798c2ecf20Sopenharmony_ci	return 0;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/* Restart if retries are left, or return an error completion code */
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic enum si_sm_result error_recovery(struct si_sm_data *bt,
3858c2ecf20Sopenharmony_ci					unsigned char status,
3868c2ecf20Sopenharmony_ci					unsigned char cCode)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	char *reason;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	bt->timeout = bt->BT_CAP_req2rsp;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	switch (cCode) {
3938c2ecf20Sopenharmony_ci	case IPMI_TIMEOUT_ERR:
3948c2ecf20Sopenharmony_ci		reason = "timeout";
3958c2ecf20Sopenharmony_ci		break;
3968c2ecf20Sopenharmony_ci	default:
3978c2ecf20Sopenharmony_ci		reason = "internal error";
3988c2ecf20Sopenharmony_ci		break;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	dev_warn(bt->io->dev, "IPMI BT: %s in %s %s ", /* open-ended line */
4028c2ecf20Sopenharmony_ci		 reason, STATE2TXT, STATUS2TXT);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	/*
4058c2ecf20Sopenharmony_ci	 * Per the IPMI spec, retries are based on the sequence number
4068c2ecf20Sopenharmony_ci	 * known only to this module, so manage a restart here.
4078c2ecf20Sopenharmony_ci	 */
4088c2ecf20Sopenharmony_ci	(bt->error_retries)++;
4098c2ecf20Sopenharmony_ci	if (bt->error_retries < bt->BT_CAP_retries) {
4108c2ecf20Sopenharmony_ci		pr_cont("%d retries left\n",
4118c2ecf20Sopenharmony_ci			bt->BT_CAP_retries - bt->error_retries);
4128c2ecf20Sopenharmony_ci		bt->state = BT_STATE_RESTART;
4138c2ecf20Sopenharmony_ci		return SI_SM_CALL_WITHOUT_DELAY;
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	dev_warn(bt->io->dev, "failed %d retries, sending error response\n",
4178c2ecf20Sopenharmony_ci		 bt->BT_CAP_retries);
4188c2ecf20Sopenharmony_ci	if (!bt->nonzero_status)
4198c2ecf20Sopenharmony_ci		dev_err(bt->io->dev, "stuck, try power cycle\n");
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/* this is most likely during insmod */
4228c2ecf20Sopenharmony_ci	else if (bt->seq <= (unsigned char)(bt->BT_CAP_retries & 0xFF)) {
4238c2ecf20Sopenharmony_ci		dev_warn(bt->io->dev, "BT reset (takes 5 secs)\n");
4248c2ecf20Sopenharmony_ci		bt->state = BT_STATE_RESET1;
4258c2ecf20Sopenharmony_ci		return SI_SM_CALL_WITHOUT_DELAY;
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/*
4298c2ecf20Sopenharmony_ci	 * Concoct a useful error message, set up the next state, and
4308c2ecf20Sopenharmony_ci	 * be done with this sequence.
4318c2ecf20Sopenharmony_ci	 */
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	bt->state = BT_STATE_IDLE;
4348c2ecf20Sopenharmony_ci	switch (cCode) {
4358c2ecf20Sopenharmony_ci	case IPMI_TIMEOUT_ERR:
4368c2ecf20Sopenharmony_ci		if (status & BT_B_BUSY) {
4378c2ecf20Sopenharmony_ci			cCode = IPMI_NODE_BUSY_ERR;
4388c2ecf20Sopenharmony_ci			bt->state = BT_STATE_LONG_BUSY;
4398c2ecf20Sopenharmony_ci		}
4408c2ecf20Sopenharmony_ci		break;
4418c2ecf20Sopenharmony_ci	default:
4428c2ecf20Sopenharmony_ci		break;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci	force_result(bt, cCode);
4458c2ecf20Sopenharmony_ci	return SI_SM_TRANSACTION_COMPLETE;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci/* Check status and (usually) take action and change this state machine. */
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic enum si_sm_result bt_event(struct si_sm_data *bt, long time)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	unsigned char status;
4538c2ecf20Sopenharmony_ci	static enum bt_states last_printed = BT_STATE_PRINTME;
4548c2ecf20Sopenharmony_ci	int i;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	status = BT_STATUS;
4578c2ecf20Sopenharmony_ci	bt->nonzero_status |= status;
4588c2ecf20Sopenharmony_ci	if ((bt_debug & BT_DEBUG_STATES) && (bt->state != last_printed)) {
4598c2ecf20Sopenharmony_ci		dev_dbg(bt->io->dev, "BT: %s %s TO=%ld - %ld\n",
4608c2ecf20Sopenharmony_ci			STATE2TXT,
4618c2ecf20Sopenharmony_ci			STATUS2TXT,
4628c2ecf20Sopenharmony_ci			bt->timeout,
4638c2ecf20Sopenharmony_ci			time);
4648c2ecf20Sopenharmony_ci		last_printed = bt->state;
4658c2ecf20Sopenharmony_ci	}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	/*
4688c2ecf20Sopenharmony_ci	 * Commands that time out may still (eventually) provide a response.
4698c2ecf20Sopenharmony_ci	 * This stale response will get in the way of a new response so remove
4708c2ecf20Sopenharmony_ci	 * it if possible (hopefully during IDLE).  Even if it comes up later
4718c2ecf20Sopenharmony_ci	 * it will be rejected by its (now-forgotten) seq number.
4728c2ecf20Sopenharmony_ci	 */
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	if ((bt->state < BT_STATE_WRITE_BYTES) && (status & BT_B2H_ATN)) {
4758c2ecf20Sopenharmony_ci		drain_BMC2HOST(bt);
4768c2ecf20Sopenharmony_ci		BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
4778c2ecf20Sopenharmony_ci	}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	if ((bt->state != BT_STATE_IDLE) &&
4808c2ecf20Sopenharmony_ci	    (bt->state <  BT_STATE_PRINTME)) {
4818c2ecf20Sopenharmony_ci		/* check timeout */
4828c2ecf20Sopenharmony_ci		bt->timeout -= time;
4838c2ecf20Sopenharmony_ci		if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1))
4848c2ecf20Sopenharmony_ci			return error_recovery(bt,
4858c2ecf20Sopenharmony_ci					      status,
4868c2ecf20Sopenharmony_ci					      IPMI_TIMEOUT_ERR);
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	switch (bt->state) {
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	/*
4928c2ecf20Sopenharmony_ci	 * Idle state first checks for asynchronous messages from another
4938c2ecf20Sopenharmony_ci	 * channel, then does some opportunistic housekeeping.
4948c2ecf20Sopenharmony_ci	 */
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	case BT_STATE_IDLE:
4978c2ecf20Sopenharmony_ci		if (status & BT_SMS_ATN) {
4988c2ecf20Sopenharmony_ci			BT_CONTROL(BT_SMS_ATN);	/* clear it */
4998c2ecf20Sopenharmony_ci			return SI_SM_ATTN;
5008c2ecf20Sopenharmony_ci		}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci		if (status & BT_H_BUSY)		/* clear a leftover H_BUSY */
5038c2ecf20Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci		BT_SI_SM_RETURN(SI_SM_IDLE);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	case BT_STATE_XACTION_START:
5088c2ecf20Sopenharmony_ci		if (status & (BT_B_BUSY | BT_H2B_ATN))
5098c2ecf20Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
5108c2ecf20Sopenharmony_ci		if (BT_STATUS & BT_H_BUSY)
5118c2ecf20Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);	/* force clear */
5128c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_WRITE_BYTES,
5138c2ecf20Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	case BT_STATE_WRITE_BYTES:
5168c2ecf20Sopenharmony_ci		if (status & BT_H_BUSY)
5178c2ecf20Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);	/* clear */
5188c2ecf20Sopenharmony_ci		BT_CONTROL(BT_CLR_WR_PTR);
5198c2ecf20Sopenharmony_ci		write_all_bytes(bt);
5208c2ecf20Sopenharmony_ci		BT_CONTROL(BT_H2B_ATN);	/* can clear too fast to catch */
5218c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_WRITE_CONSUME,
5228c2ecf20Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	case BT_STATE_WRITE_CONSUME:
5258c2ecf20Sopenharmony_ci		if (status & (BT_B_BUSY | BT_H2B_ATN))
5268c2ecf20Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
5278c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_READ_WAIT,
5288c2ecf20Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/* Spinning hard can suppress B2H_ATN and force a timeout */
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	case BT_STATE_READ_WAIT:
5338c2ecf20Sopenharmony_ci		if (!(status & BT_B2H_ATN))
5348c2ecf20Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
5358c2ecf20Sopenharmony_ci		BT_CONTROL(BT_H_BUSY);		/* set */
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		/*
5388c2ecf20Sopenharmony_ci		 * Uncached, ordered writes should just proceed serially but
5398c2ecf20Sopenharmony_ci		 * some BMCs don't clear B2H_ATN with one hit.  Fast-path a
5408c2ecf20Sopenharmony_ci		 * workaround without too much penalty to the general case.
5418c2ecf20Sopenharmony_ci		 */
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		BT_CONTROL(BT_B2H_ATN);		/* clear it to ACK the BMC */
5448c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_CLEAR_B2H,
5458c2ecf20Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	case BT_STATE_CLEAR_B2H:
5488c2ecf20Sopenharmony_ci		if (status & BT_B2H_ATN) {
5498c2ecf20Sopenharmony_ci			/* keep hitting it */
5508c2ecf20Sopenharmony_ci			BT_CONTROL(BT_B2H_ATN);
5518c2ecf20Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
5528c2ecf20Sopenharmony_ci		}
5538c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_READ_BYTES,
5548c2ecf20Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	case BT_STATE_READ_BYTES:
5578c2ecf20Sopenharmony_ci		if (!(status & BT_H_BUSY))
5588c2ecf20Sopenharmony_ci			/* check in case of retry */
5598c2ecf20Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);
5608c2ecf20Sopenharmony_ci		BT_CONTROL(BT_CLR_RD_PTR);	/* start of BMC2HOST buffer */
5618c2ecf20Sopenharmony_ci		i = read_all_bytes(bt);		/* true == packet seq match */
5628c2ecf20Sopenharmony_ci		BT_CONTROL(BT_H_BUSY);		/* NOW clear */
5638c2ecf20Sopenharmony_ci		if (!i) 			/* Not my message */
5648c2ecf20Sopenharmony_ci			BT_STATE_CHANGE(BT_STATE_READ_WAIT,
5658c2ecf20Sopenharmony_ci					SI_SM_CALL_WITHOUT_DELAY);
5668c2ecf20Sopenharmony_ci		bt->state = bt->complete;
5678c2ecf20Sopenharmony_ci		return bt->state == BT_STATE_IDLE ?	/* where to next? */
5688c2ecf20Sopenharmony_ci			SI_SM_TRANSACTION_COMPLETE :	/* normal */
5698c2ecf20Sopenharmony_ci			SI_SM_CALL_WITHOUT_DELAY;	/* Startup magic */
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	case BT_STATE_LONG_BUSY:	/* For example: after FW update */
5728c2ecf20Sopenharmony_ci		if (!(status & BT_B_BUSY)) {
5738c2ecf20Sopenharmony_ci			reset_flags(bt);	/* next state is now IDLE */
5748c2ecf20Sopenharmony_ci			bt_init_data(bt, bt->io);
5758c2ecf20Sopenharmony_ci		}
5768c2ecf20Sopenharmony_ci		return SI_SM_CALL_WITH_DELAY;	/* No repeat printing */
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	case BT_STATE_RESET1:
5798c2ecf20Sopenharmony_ci		reset_flags(bt);
5808c2ecf20Sopenharmony_ci		drain_BMC2HOST(bt);
5818c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_RESET2,
5828c2ecf20Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	case BT_STATE_RESET2:		/* Send a soft reset */
5858c2ecf20Sopenharmony_ci		BT_CONTROL(BT_CLR_WR_PTR);
5868c2ecf20Sopenharmony_ci		HOST2BMC(3);		/* number of bytes following */
5878c2ecf20Sopenharmony_ci		HOST2BMC(0x18);		/* NetFn/LUN == Application, LUN 0 */
5888c2ecf20Sopenharmony_ci		HOST2BMC(42);		/* Sequence number */
5898c2ecf20Sopenharmony_ci		HOST2BMC(3);		/* Cmd == Soft reset */
5908c2ecf20Sopenharmony_ci		BT_CONTROL(BT_H2B_ATN);
5918c2ecf20Sopenharmony_ci		bt->timeout = BT_RESET_DELAY * USEC_PER_SEC;
5928c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_RESET3,
5938c2ecf20Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	case BT_STATE_RESET3:		/* Hold off everything for a bit */
5968c2ecf20Sopenharmony_ci		if (bt->timeout > 0)
5978c2ecf20Sopenharmony_ci			return SI_SM_CALL_WITH_DELAY;
5988c2ecf20Sopenharmony_ci		drain_BMC2HOST(bt);
5998c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_RESTART,
6008c2ecf20Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	case BT_STATE_RESTART:		/* don't reset retries or seq! */
6038c2ecf20Sopenharmony_ci		bt->read_count = 0;
6048c2ecf20Sopenharmony_ci		bt->nonzero_status = 0;
6058c2ecf20Sopenharmony_ci		bt->timeout = bt->BT_CAP_req2rsp;
6068c2ecf20Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_XACTION_START,
6078c2ecf20Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	default:	/* should never occur */
6108c2ecf20Sopenharmony_ci		return error_recovery(bt,
6118c2ecf20Sopenharmony_ci				      status,
6128c2ecf20Sopenharmony_ci				      IPMI_ERR_UNSPECIFIED);
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci	return SI_SM_CALL_WITH_DELAY;
6158c2ecf20Sopenharmony_ci}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_cistatic int bt_detect(struct si_sm_data *bt)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	unsigned char GetBT_CAP[] = { 0x18, 0x36 };
6208c2ecf20Sopenharmony_ci	unsigned char BT_CAP[8];
6218c2ecf20Sopenharmony_ci	enum si_sm_result smi_result;
6228c2ecf20Sopenharmony_ci	int rv;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	/*
6258c2ecf20Sopenharmony_ci	 * It's impossible for the BT status and interrupt registers to be
6268c2ecf20Sopenharmony_ci	 * all 1's, (assuming a properly functioning, self-initialized BMC)
6278c2ecf20Sopenharmony_ci	 * but that's what you get from reading a bogus address, so we
6288c2ecf20Sopenharmony_ci	 * test that first.  The calling routine uses negative logic.
6298c2ecf20Sopenharmony_ci	 */
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
6328c2ecf20Sopenharmony_ci		return 1;
6338c2ecf20Sopenharmony_ci	reset_flags(bt);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/*
6368c2ecf20Sopenharmony_ci	 * Try getting the BT capabilities here.
6378c2ecf20Sopenharmony_ci	 */
6388c2ecf20Sopenharmony_ci	rv = bt_start_transaction(bt, GetBT_CAP, sizeof(GetBT_CAP));
6398c2ecf20Sopenharmony_ci	if (rv) {
6408c2ecf20Sopenharmony_ci		dev_warn(bt->io->dev,
6418c2ecf20Sopenharmony_ci			 "Can't start capabilities transaction: %d\n", rv);
6428c2ecf20Sopenharmony_ci		goto out_no_bt_cap;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	smi_result = SI_SM_CALL_WITHOUT_DELAY;
6468c2ecf20Sopenharmony_ci	for (;;) {
6478c2ecf20Sopenharmony_ci		if (smi_result == SI_SM_CALL_WITH_DELAY ||
6488c2ecf20Sopenharmony_ci		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
6498c2ecf20Sopenharmony_ci			schedule_timeout_uninterruptible(1);
6508c2ecf20Sopenharmony_ci			smi_result = bt_event(bt, jiffies_to_usecs(1));
6518c2ecf20Sopenharmony_ci		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
6528c2ecf20Sopenharmony_ci			smi_result = bt_event(bt, 0);
6538c2ecf20Sopenharmony_ci		} else
6548c2ecf20Sopenharmony_ci			break;
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	rv = bt_get_result(bt, BT_CAP, sizeof(BT_CAP));
6588c2ecf20Sopenharmony_ci	bt_init_data(bt, bt->io);
6598c2ecf20Sopenharmony_ci	if (rv < 8) {
6608c2ecf20Sopenharmony_ci		dev_warn(bt->io->dev, "bt cap response too short: %d\n", rv);
6618c2ecf20Sopenharmony_ci		goto out_no_bt_cap;
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	if (BT_CAP[2]) {
6658c2ecf20Sopenharmony_ci		dev_warn(bt->io->dev, "Error fetching bt cap: %x\n", BT_CAP[2]);
6668c2ecf20Sopenharmony_ciout_no_bt_cap:
6678c2ecf20Sopenharmony_ci		dev_warn(bt->io->dev, "using default values\n");
6688c2ecf20Sopenharmony_ci	} else {
6698c2ecf20Sopenharmony_ci		bt->BT_CAP_req2rsp = BT_CAP[6] * USEC_PER_SEC;
6708c2ecf20Sopenharmony_ci		bt->BT_CAP_retries = BT_CAP[7];
6718c2ecf20Sopenharmony_ci	}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	dev_info(bt->io->dev, "req2rsp=%ld secs retries=%d\n",
6748c2ecf20Sopenharmony_ci		 bt->BT_CAP_req2rsp / USEC_PER_SEC, bt->BT_CAP_retries);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	return 0;
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic void bt_cleanup(struct si_sm_data *bt)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_cistatic int bt_size(void)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	return sizeof(struct si_sm_data);
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ciconst struct si_sm_handlers bt_smi_handlers = {
6898c2ecf20Sopenharmony_ci	.init_data		= bt_init_data,
6908c2ecf20Sopenharmony_ci	.start_transaction	= bt_start_transaction,
6918c2ecf20Sopenharmony_ci	.get_result		= bt_get_result,
6928c2ecf20Sopenharmony_ci	.event			= bt_event,
6938c2ecf20Sopenharmony_ci	.detect			= bt_detect,
6948c2ecf20Sopenharmony_ci	.cleanup		= bt_cleanup,
6958c2ecf20Sopenharmony_ci	.size			= bt_size,
6968c2ecf20Sopenharmony_ci};
697