162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  ipmi_bt_sm.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part
662306a36Sopenharmony_ci *  of the driver architecture at http://sourceforge.net/projects/openipmi
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci *  Author:	Rocky Craig <first.last@hp.com>
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#define DEBUG /* So dev_dbg() is always available. */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/kernel.h> /* For printk. */
1462306a36Sopenharmony_ci#include <linux/string.h>
1562306a36Sopenharmony_ci#include <linux/module.h>
1662306a36Sopenharmony_ci#include <linux/moduleparam.h>
1762306a36Sopenharmony_ci#include <linux/ipmi_msgdefs.h>		/* for completion codes */
1862306a36Sopenharmony_ci#include "ipmi_si_sm.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define BT_DEBUG_OFF	0	/* Used in production */
2162306a36Sopenharmony_ci#define BT_DEBUG_ENABLE	1	/* Generic messages */
2262306a36Sopenharmony_ci#define BT_DEBUG_MSG	2	/* Prints all request/response buffers */
2362306a36Sopenharmony_ci#define BT_DEBUG_STATES	4	/* Verbose look at state changes */
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * BT_DEBUG_OFF must be zero to correspond to the default uninitialized
2662306a36Sopenharmony_ci * value
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic int bt_debug; /* 0 == BT_DEBUG_OFF */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cimodule_param(bt_debug, int, 0644);
3262306a36Sopenharmony_ciMODULE_PARM_DESC(bt_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/*
3562306a36Sopenharmony_ci * Typical "Get BT Capabilities" values are 2-3 retries, 5-10 seconds,
3662306a36Sopenharmony_ci * and 64 byte buffers.  However, one HP implementation wants 255 bytes of
3762306a36Sopenharmony_ci * buffer (with a documented message of 160 bytes) so go for the max.
3862306a36Sopenharmony_ci * Since the Open IPMI architecture is single-message oriented at this
3962306a36Sopenharmony_ci * stage, the queue depth of BT is of no concern.
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define BT_NORMAL_TIMEOUT	5	/* seconds */
4362306a36Sopenharmony_ci#define BT_NORMAL_RETRY_LIMIT	2
4462306a36Sopenharmony_ci#define BT_RESET_DELAY		6	/* seconds after warm reset */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/*
4762306a36Sopenharmony_ci * States are written in chronological order and usually cover
4862306a36Sopenharmony_ci * multiple rows of the state table discussion in the IPMI spec.
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cienum bt_states {
5262306a36Sopenharmony_ci	BT_STATE_IDLE = 0,	/* Order is critical in this list */
5362306a36Sopenharmony_ci	BT_STATE_XACTION_START,
5462306a36Sopenharmony_ci	BT_STATE_WRITE_BYTES,
5562306a36Sopenharmony_ci	BT_STATE_WRITE_CONSUME,
5662306a36Sopenharmony_ci	BT_STATE_READ_WAIT,
5762306a36Sopenharmony_ci	BT_STATE_CLEAR_B2H,
5862306a36Sopenharmony_ci	BT_STATE_READ_BYTES,
5962306a36Sopenharmony_ci	BT_STATE_RESET1,	/* These must come last */
6062306a36Sopenharmony_ci	BT_STATE_RESET2,
6162306a36Sopenharmony_ci	BT_STATE_RESET3,
6262306a36Sopenharmony_ci	BT_STATE_RESTART,
6362306a36Sopenharmony_ci	BT_STATE_PRINTME,
6462306a36Sopenharmony_ci	BT_STATE_LONG_BUSY	/* BT doesn't get hosed :-) */
6562306a36Sopenharmony_ci};
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/*
6862306a36Sopenharmony_ci * Macros seen at the end of state "case" blocks.  They help with legibility
6962306a36Sopenharmony_ci * and debugging.
7062306a36Sopenharmony_ci */
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define BT_STATE_CHANGE(X, Y) { bt->state = X; return Y; }
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define BT_SI_SM_RETURN(Y)   { last_printed = BT_STATE_PRINTME; return Y; }
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistruct si_sm_data {
7762306a36Sopenharmony_ci	enum bt_states	state;
7862306a36Sopenharmony_ci	unsigned char	seq;		/* BT sequence number */
7962306a36Sopenharmony_ci	struct si_sm_io	*io;
8062306a36Sopenharmony_ci	unsigned char	write_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
8162306a36Sopenharmony_ci	int		write_count;
8262306a36Sopenharmony_ci	unsigned char	read_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
8362306a36Sopenharmony_ci	int		read_count;
8462306a36Sopenharmony_ci	int		truncated;
8562306a36Sopenharmony_ci	long		timeout;	/* microseconds countdown */
8662306a36Sopenharmony_ci	int		error_retries;	/* end of "common" fields */
8762306a36Sopenharmony_ci	int		nonzero_status;	/* hung BMCs stay all 0 */
8862306a36Sopenharmony_ci	enum bt_states	complete;	/* to divert the state machine */
8962306a36Sopenharmony_ci	long		BT_CAP_req2rsp;
9062306a36Sopenharmony_ci	int		BT_CAP_retries;	/* Recommended retries */
9162306a36Sopenharmony_ci};
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define BT_CLR_WR_PTR	0x01	/* See IPMI 1.5 table 11.6.4 */
9462306a36Sopenharmony_ci#define BT_CLR_RD_PTR	0x02
9562306a36Sopenharmony_ci#define BT_H2B_ATN	0x04
9662306a36Sopenharmony_ci#define BT_B2H_ATN	0x08
9762306a36Sopenharmony_ci#define BT_SMS_ATN	0x10
9862306a36Sopenharmony_ci#define BT_OEM0		0x20
9962306a36Sopenharmony_ci#define BT_H_BUSY	0x40
10062306a36Sopenharmony_ci#define BT_B_BUSY	0x80
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci/*
10362306a36Sopenharmony_ci * Some bits are toggled on each write: write once to set it, once
10462306a36Sopenharmony_ci * more to clear it; writing a zero does nothing.  To absolutely
10562306a36Sopenharmony_ci * clear it, check its state and write if set.  This avoids the "get
10662306a36Sopenharmony_ci * current then use as mask" scheme to modify one bit.  Note that the
10762306a36Sopenharmony_ci * variable "bt" is hardcoded into these macros.
10862306a36Sopenharmony_ci */
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci#define BT_STATUS	bt->io->inputb(bt->io, 0)
11162306a36Sopenharmony_ci#define BT_CONTROL(x)	bt->io->outputb(bt->io, 0, x)
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci#define BMC2HOST	bt->io->inputb(bt->io, 1)
11462306a36Sopenharmony_ci#define HOST2BMC(x)	bt->io->outputb(bt->io, 1, x)
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci#define BT_INTMASK_R	bt->io->inputb(bt->io, 2)
11762306a36Sopenharmony_ci#define BT_INTMASK_W(x)	bt->io->outputb(bt->io, 2, x)
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci/*
12062306a36Sopenharmony_ci * Convenience routines for debugging.  These are not multi-open safe!
12162306a36Sopenharmony_ci * Note the macros have hardcoded variables in them.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic char *state2txt(unsigned char state)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	switch (state) {
12762306a36Sopenharmony_ci	case BT_STATE_IDLE:		return("IDLE");
12862306a36Sopenharmony_ci	case BT_STATE_XACTION_START:	return("XACTION");
12962306a36Sopenharmony_ci	case BT_STATE_WRITE_BYTES:	return("WR_BYTES");
13062306a36Sopenharmony_ci	case BT_STATE_WRITE_CONSUME:	return("WR_CONSUME");
13162306a36Sopenharmony_ci	case BT_STATE_READ_WAIT:	return("RD_WAIT");
13262306a36Sopenharmony_ci	case BT_STATE_CLEAR_B2H:	return("CLEAR_B2H");
13362306a36Sopenharmony_ci	case BT_STATE_READ_BYTES:	return("RD_BYTES");
13462306a36Sopenharmony_ci	case BT_STATE_RESET1:		return("RESET1");
13562306a36Sopenharmony_ci	case BT_STATE_RESET2:		return("RESET2");
13662306a36Sopenharmony_ci	case BT_STATE_RESET3:		return("RESET3");
13762306a36Sopenharmony_ci	case BT_STATE_RESTART:		return("RESTART");
13862306a36Sopenharmony_ci	case BT_STATE_LONG_BUSY:	return("LONG_BUSY");
13962306a36Sopenharmony_ci	}
14062306a36Sopenharmony_ci	return("BAD STATE");
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci#define STATE2TXT state2txt(bt->state)
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistatic char *status2txt(unsigned char status)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	/*
14762306a36Sopenharmony_ci	 * This cannot be called by two threads at the same time and
14862306a36Sopenharmony_ci	 * the buffer is always consumed immediately, so the static is
14962306a36Sopenharmony_ci	 * safe to use.
15062306a36Sopenharmony_ci	 */
15162306a36Sopenharmony_ci	static char buf[40];
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	strcpy(buf, "[ ");
15462306a36Sopenharmony_ci	if (status & BT_B_BUSY)
15562306a36Sopenharmony_ci		strcat(buf, "B_BUSY ");
15662306a36Sopenharmony_ci	if (status & BT_H_BUSY)
15762306a36Sopenharmony_ci		strcat(buf, "H_BUSY ");
15862306a36Sopenharmony_ci	if (status & BT_OEM0)
15962306a36Sopenharmony_ci		strcat(buf, "OEM0 ");
16062306a36Sopenharmony_ci	if (status & BT_SMS_ATN)
16162306a36Sopenharmony_ci		strcat(buf, "SMS ");
16262306a36Sopenharmony_ci	if (status & BT_B2H_ATN)
16362306a36Sopenharmony_ci		strcat(buf, "B2H ");
16462306a36Sopenharmony_ci	if (status & BT_H2B_ATN)
16562306a36Sopenharmony_ci		strcat(buf, "H2B ");
16662306a36Sopenharmony_ci	strcat(buf, "]");
16762306a36Sopenharmony_ci	return buf;
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci#define STATUS2TXT status2txt(status)
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/* called externally at insmod time, and internally on cleanup */
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic unsigned int bt_init_data(struct si_sm_data *bt, struct si_sm_io *io)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci	memset(bt, 0, sizeof(struct si_sm_data));
17662306a36Sopenharmony_ci	if (bt->io != io) {
17762306a36Sopenharmony_ci		/* external: one-time only things */
17862306a36Sopenharmony_ci		bt->io = io;
17962306a36Sopenharmony_ci		bt->seq = 0;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci	bt->state = BT_STATE_IDLE;	/* start here */
18262306a36Sopenharmony_ci	bt->complete = BT_STATE_IDLE;	/* end here */
18362306a36Sopenharmony_ci	bt->BT_CAP_req2rsp = BT_NORMAL_TIMEOUT * USEC_PER_SEC;
18462306a36Sopenharmony_ci	bt->BT_CAP_retries = BT_NORMAL_RETRY_LIMIT;
18562306a36Sopenharmony_ci	return 3; /* We claim 3 bytes of space; ought to check SPMI table */
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/* Jam a completion code (probably an error) into a response */
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cistatic void force_result(struct si_sm_data *bt, unsigned char completion_code)
19162306a36Sopenharmony_ci{
19262306a36Sopenharmony_ci	bt->read_data[0] = 4;				/* # following bytes */
19362306a36Sopenharmony_ci	bt->read_data[1] = bt->write_data[1] | 4;	/* Odd NetFn/LUN */
19462306a36Sopenharmony_ci	bt->read_data[2] = bt->write_data[2];		/* seq (ignored) */
19562306a36Sopenharmony_ci	bt->read_data[3] = bt->write_data[3];		/* Command */
19662306a36Sopenharmony_ci	bt->read_data[4] = completion_code;
19762306a36Sopenharmony_ci	bt->read_count = 5;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/* The upper state machine starts here */
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_cistatic int bt_start_transaction(struct si_sm_data *bt,
20362306a36Sopenharmony_ci				unsigned char *data,
20462306a36Sopenharmony_ci				unsigned int size)
20562306a36Sopenharmony_ci{
20662306a36Sopenharmony_ci	unsigned int i;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	if (size < 2)
20962306a36Sopenharmony_ci		return IPMI_REQ_LEN_INVALID_ERR;
21062306a36Sopenharmony_ci	if (size > IPMI_MAX_MSG_LENGTH)
21162306a36Sopenharmony_ci		return IPMI_REQ_LEN_EXCEEDED_ERR;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	if (bt->state == BT_STATE_LONG_BUSY)
21462306a36Sopenharmony_ci		return IPMI_NODE_BUSY_ERR;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	if (bt->state != BT_STATE_IDLE) {
21762306a36Sopenharmony_ci		dev_warn(bt->io->dev, "BT in invalid state %d\n", bt->state);
21862306a36Sopenharmony_ci		return IPMI_NOT_IN_MY_STATE_ERR;
21962306a36Sopenharmony_ci	}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
22262306a36Sopenharmony_ci		dev_dbg(bt->io->dev, "+++++++++++++++++ New command\n");
22362306a36Sopenharmony_ci		dev_dbg(bt->io->dev, "NetFn/LUN CMD [%d data]:", size - 2);
22462306a36Sopenharmony_ci		for (i = 0; i < size; i ++)
22562306a36Sopenharmony_ci			pr_cont(" %02x", data[i]);
22662306a36Sopenharmony_ci		pr_cont("\n");
22762306a36Sopenharmony_ci	}
22862306a36Sopenharmony_ci	bt->write_data[0] = size + 1;	/* all data plus seq byte */
22962306a36Sopenharmony_ci	bt->write_data[1] = *data;	/* NetFn/LUN */
23062306a36Sopenharmony_ci	bt->write_data[2] = bt->seq++;
23162306a36Sopenharmony_ci	memcpy(bt->write_data + 3, data + 1, size - 1);
23262306a36Sopenharmony_ci	bt->write_count = size + 2;
23362306a36Sopenharmony_ci	bt->error_retries = 0;
23462306a36Sopenharmony_ci	bt->nonzero_status = 0;
23562306a36Sopenharmony_ci	bt->truncated = 0;
23662306a36Sopenharmony_ci	bt->state = BT_STATE_XACTION_START;
23762306a36Sopenharmony_ci	bt->timeout = bt->BT_CAP_req2rsp;
23862306a36Sopenharmony_ci	force_result(bt, IPMI_ERR_UNSPECIFIED);
23962306a36Sopenharmony_ci	return 0;
24062306a36Sopenharmony_ci}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci/*
24362306a36Sopenharmony_ci * After the upper state machine has been told SI_SM_TRANSACTION_COMPLETE
24462306a36Sopenharmony_ci * it calls this.  Strip out the length and seq bytes.
24562306a36Sopenharmony_ci */
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_cistatic int bt_get_result(struct si_sm_data *bt,
24862306a36Sopenharmony_ci			 unsigned char *data,
24962306a36Sopenharmony_ci			 unsigned int length)
25062306a36Sopenharmony_ci{
25162306a36Sopenharmony_ci	int i, msg_len;
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	msg_len = bt->read_count - 2;		/* account for length & seq */
25462306a36Sopenharmony_ci	if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
25562306a36Sopenharmony_ci		force_result(bt, IPMI_ERR_UNSPECIFIED);
25662306a36Sopenharmony_ci		msg_len = 3;
25762306a36Sopenharmony_ci	}
25862306a36Sopenharmony_ci	data[0] = bt->read_data[1];
25962306a36Sopenharmony_ci	data[1] = bt->read_data[3];
26062306a36Sopenharmony_ci	if (length < msg_len || bt->truncated) {
26162306a36Sopenharmony_ci		data[2] = IPMI_ERR_MSG_TRUNCATED;
26262306a36Sopenharmony_ci		msg_len = 3;
26362306a36Sopenharmony_ci	} else
26462306a36Sopenharmony_ci		memcpy(data + 2, bt->read_data + 4, msg_len - 2);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
26762306a36Sopenharmony_ci		dev_dbg(bt->io->dev, "result %d bytes:", msg_len);
26862306a36Sopenharmony_ci		for (i = 0; i < msg_len; i++)
26962306a36Sopenharmony_ci			pr_cont(" %02x", data[i]);
27062306a36Sopenharmony_ci		pr_cont("\n");
27162306a36Sopenharmony_ci	}
27262306a36Sopenharmony_ci	return msg_len;
27362306a36Sopenharmony_ci}
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci/* This bit's functionality is optional */
27662306a36Sopenharmony_ci#define BT_BMC_HWRST	0x80
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_cistatic void reset_flags(struct si_sm_data *bt)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	if (bt_debug)
28162306a36Sopenharmony_ci		dev_dbg(bt->io->dev, "flag reset %s\n", status2txt(BT_STATUS));
28262306a36Sopenharmony_ci	if (BT_STATUS & BT_H_BUSY)
28362306a36Sopenharmony_ci		BT_CONTROL(BT_H_BUSY);	/* force clear */
28462306a36Sopenharmony_ci	BT_CONTROL(BT_CLR_WR_PTR);	/* always reset */
28562306a36Sopenharmony_ci	BT_CONTROL(BT_SMS_ATN);		/* always clear */
28662306a36Sopenharmony_ci	BT_INTMASK_W(BT_BMC_HWRST);
28762306a36Sopenharmony_ci}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci/*
29062306a36Sopenharmony_ci * Get rid of an unwanted/stale response.  This should only be needed for
29162306a36Sopenharmony_ci * BMCs that support multiple outstanding requests.
29262306a36Sopenharmony_ci */
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_cistatic void drain_BMC2HOST(struct si_sm_data *bt)
29562306a36Sopenharmony_ci{
29662306a36Sopenharmony_ci	int i, size;
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	if (!(BT_STATUS & BT_B2H_ATN)) 	/* Not signalling a response */
29962306a36Sopenharmony_ci		return;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	BT_CONTROL(BT_H_BUSY);		/* now set */
30262306a36Sopenharmony_ci	BT_CONTROL(BT_B2H_ATN);		/* always clear */
30362306a36Sopenharmony_ci	BT_STATUS;			/* pause */
30462306a36Sopenharmony_ci	BT_CONTROL(BT_B2H_ATN);		/* some BMCs are stubborn */
30562306a36Sopenharmony_ci	BT_CONTROL(BT_CLR_RD_PTR);	/* always reset */
30662306a36Sopenharmony_ci	if (bt_debug)
30762306a36Sopenharmony_ci		dev_dbg(bt->io->dev, "stale response %s; ",
30862306a36Sopenharmony_ci			status2txt(BT_STATUS));
30962306a36Sopenharmony_ci	size = BMC2HOST;
31062306a36Sopenharmony_ci	for (i = 0; i < size ; i++)
31162306a36Sopenharmony_ci		BMC2HOST;
31262306a36Sopenharmony_ci	BT_CONTROL(BT_H_BUSY);		/* now clear */
31362306a36Sopenharmony_ci	if (bt_debug)
31462306a36Sopenharmony_ci		pr_cont("drained %d bytes\n", size + 1);
31562306a36Sopenharmony_ci}
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_cistatic inline void write_all_bytes(struct si_sm_data *bt)
31862306a36Sopenharmony_ci{
31962306a36Sopenharmony_ci	int i;
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
32262306a36Sopenharmony_ci		dev_dbg(bt->io->dev, "write %d bytes seq=0x%02X",
32362306a36Sopenharmony_ci			bt->write_count, bt->seq);
32462306a36Sopenharmony_ci		for (i = 0; i < bt->write_count; i++)
32562306a36Sopenharmony_ci			pr_cont(" %02x", bt->write_data[i]);
32662306a36Sopenharmony_ci		pr_cont("\n");
32762306a36Sopenharmony_ci	}
32862306a36Sopenharmony_ci	for (i = 0; i < bt->write_count; i++)
32962306a36Sopenharmony_ci		HOST2BMC(bt->write_data[i]);
33062306a36Sopenharmony_ci}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_cistatic inline int read_all_bytes(struct si_sm_data *bt)
33362306a36Sopenharmony_ci{
33462306a36Sopenharmony_ci	unsigned int i;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	/*
33762306a36Sopenharmony_ci	 * length is "framing info", minimum = 4: NetFn, Seq, Cmd, cCode.
33862306a36Sopenharmony_ci	 * Keep layout of first four bytes aligned with write_data[]
33962306a36Sopenharmony_ci	 */
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	bt->read_data[0] = BMC2HOST;
34262306a36Sopenharmony_ci	bt->read_count = bt->read_data[0];
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	if (bt->read_count < 4 || bt->read_count >= IPMI_MAX_MSG_LENGTH) {
34562306a36Sopenharmony_ci		if (bt_debug & BT_DEBUG_MSG)
34662306a36Sopenharmony_ci			dev_dbg(bt->io->dev,
34762306a36Sopenharmony_ci				"bad raw rsp len=%d\n", bt->read_count);
34862306a36Sopenharmony_ci		bt->truncated = 1;
34962306a36Sopenharmony_ci		return 1;	/* let next XACTION START clean it up */
35062306a36Sopenharmony_ci	}
35162306a36Sopenharmony_ci	for (i = 1; i <= bt->read_count; i++)
35262306a36Sopenharmony_ci		bt->read_data[i] = BMC2HOST;
35362306a36Sopenharmony_ci	bt->read_count++;	/* Account internally for length byte */
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG) {
35662306a36Sopenharmony_ci		int max = bt->read_count;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci		dev_dbg(bt->io->dev,
35962306a36Sopenharmony_ci			"got %d bytes seq=0x%02X", max, bt->read_data[2]);
36062306a36Sopenharmony_ci		if (max > 16)
36162306a36Sopenharmony_ci			max = 16;
36262306a36Sopenharmony_ci		for (i = 0; i < max; i++)
36362306a36Sopenharmony_ci			pr_cont(" %02x", bt->read_data[i]);
36462306a36Sopenharmony_ci		pr_cont("%s\n", bt->read_count == max ? "" : " ...");
36562306a36Sopenharmony_ci	}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci	/* per the spec, the (NetFn[1], Seq[2], Cmd[3]) tuples must match */
36862306a36Sopenharmony_ci	if ((bt->read_data[3] == bt->write_data[3]) &&
36962306a36Sopenharmony_ci	    (bt->read_data[2] == bt->write_data[2]) &&
37062306a36Sopenharmony_ci	    ((bt->read_data[1] & 0xF8) == (bt->write_data[1] & 0xF8)))
37162306a36Sopenharmony_ci			return 1;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	if (bt_debug & BT_DEBUG_MSG)
37462306a36Sopenharmony_ci		dev_dbg(bt->io->dev,
37562306a36Sopenharmony_ci			"IPMI BT: bad packet: want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
37662306a36Sopenharmony_ci			bt->write_data[1] | 0x04, bt->write_data[2],
37762306a36Sopenharmony_ci			bt->write_data[3],
37862306a36Sopenharmony_ci			bt->read_data[1],  bt->read_data[2],  bt->read_data[3]);
37962306a36Sopenharmony_ci	return 0;
38062306a36Sopenharmony_ci}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci/* Restart if retries are left, or return an error completion code */
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_cistatic enum si_sm_result error_recovery(struct si_sm_data *bt,
38562306a36Sopenharmony_ci					unsigned char status,
38662306a36Sopenharmony_ci					unsigned char cCode)
38762306a36Sopenharmony_ci{
38862306a36Sopenharmony_ci	char *reason;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	bt->timeout = bt->BT_CAP_req2rsp;
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	switch (cCode) {
39362306a36Sopenharmony_ci	case IPMI_TIMEOUT_ERR:
39462306a36Sopenharmony_ci		reason = "timeout";
39562306a36Sopenharmony_ci		break;
39662306a36Sopenharmony_ci	default:
39762306a36Sopenharmony_ci		reason = "internal error";
39862306a36Sopenharmony_ci		break;
39962306a36Sopenharmony_ci	}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	dev_warn(bt->io->dev, "IPMI BT: %s in %s %s ", /* open-ended line */
40262306a36Sopenharmony_ci		 reason, STATE2TXT, STATUS2TXT);
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	/*
40562306a36Sopenharmony_ci	 * Per the IPMI spec, retries are based on the sequence number
40662306a36Sopenharmony_ci	 * known only to this module, so manage a restart here.
40762306a36Sopenharmony_ci	 */
40862306a36Sopenharmony_ci	(bt->error_retries)++;
40962306a36Sopenharmony_ci	if (bt->error_retries < bt->BT_CAP_retries) {
41062306a36Sopenharmony_ci		pr_cont("%d retries left\n",
41162306a36Sopenharmony_ci			bt->BT_CAP_retries - bt->error_retries);
41262306a36Sopenharmony_ci		bt->state = BT_STATE_RESTART;
41362306a36Sopenharmony_ci		return SI_SM_CALL_WITHOUT_DELAY;
41462306a36Sopenharmony_ci	}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	dev_warn(bt->io->dev, "failed %d retries, sending error response\n",
41762306a36Sopenharmony_ci		 bt->BT_CAP_retries);
41862306a36Sopenharmony_ci	if (!bt->nonzero_status)
41962306a36Sopenharmony_ci		dev_err(bt->io->dev, "stuck, try power cycle\n");
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	/* this is most likely during insmod */
42262306a36Sopenharmony_ci	else if (bt->seq <= (unsigned char)(bt->BT_CAP_retries & 0xFF)) {
42362306a36Sopenharmony_ci		dev_warn(bt->io->dev, "BT reset (takes 5 secs)\n");
42462306a36Sopenharmony_ci		bt->state = BT_STATE_RESET1;
42562306a36Sopenharmony_ci		return SI_SM_CALL_WITHOUT_DELAY;
42662306a36Sopenharmony_ci	}
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	/*
42962306a36Sopenharmony_ci	 * Concoct a useful error message, set up the next state, and
43062306a36Sopenharmony_ci	 * be done with this sequence.
43162306a36Sopenharmony_ci	 */
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	bt->state = BT_STATE_IDLE;
43462306a36Sopenharmony_ci	switch (cCode) {
43562306a36Sopenharmony_ci	case IPMI_TIMEOUT_ERR:
43662306a36Sopenharmony_ci		if (status & BT_B_BUSY) {
43762306a36Sopenharmony_ci			cCode = IPMI_NODE_BUSY_ERR;
43862306a36Sopenharmony_ci			bt->state = BT_STATE_LONG_BUSY;
43962306a36Sopenharmony_ci		}
44062306a36Sopenharmony_ci		break;
44162306a36Sopenharmony_ci	default:
44262306a36Sopenharmony_ci		break;
44362306a36Sopenharmony_ci	}
44462306a36Sopenharmony_ci	force_result(bt, cCode);
44562306a36Sopenharmony_ci	return SI_SM_TRANSACTION_COMPLETE;
44662306a36Sopenharmony_ci}
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci/* Check status and (usually) take action and change this state machine. */
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_cistatic enum si_sm_result bt_event(struct si_sm_data *bt, long time)
45162306a36Sopenharmony_ci{
45262306a36Sopenharmony_ci	unsigned char status;
45362306a36Sopenharmony_ci	static enum bt_states last_printed = BT_STATE_PRINTME;
45462306a36Sopenharmony_ci	int i;
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	status = BT_STATUS;
45762306a36Sopenharmony_ci	bt->nonzero_status |= status;
45862306a36Sopenharmony_ci	if ((bt_debug & BT_DEBUG_STATES) && (bt->state != last_printed)) {
45962306a36Sopenharmony_ci		dev_dbg(bt->io->dev, "BT: %s %s TO=%ld - %ld\n",
46062306a36Sopenharmony_ci			STATE2TXT,
46162306a36Sopenharmony_ci			STATUS2TXT,
46262306a36Sopenharmony_ci			bt->timeout,
46362306a36Sopenharmony_ci			time);
46462306a36Sopenharmony_ci		last_printed = bt->state;
46562306a36Sopenharmony_ci	}
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	/*
46862306a36Sopenharmony_ci	 * Commands that time out may still (eventually) provide a response.
46962306a36Sopenharmony_ci	 * This stale response will get in the way of a new response so remove
47062306a36Sopenharmony_ci	 * it if possible (hopefully during IDLE).  Even if it comes up later
47162306a36Sopenharmony_ci	 * it will be rejected by its (now-forgotten) seq number.
47262306a36Sopenharmony_ci	 */
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci	if ((bt->state < BT_STATE_WRITE_BYTES) && (status & BT_B2H_ATN)) {
47562306a36Sopenharmony_ci		drain_BMC2HOST(bt);
47662306a36Sopenharmony_ci		BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
47762306a36Sopenharmony_ci	}
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci	if ((bt->state != BT_STATE_IDLE) &&
48062306a36Sopenharmony_ci	    (bt->state <  BT_STATE_PRINTME)) {
48162306a36Sopenharmony_ci		/* check timeout */
48262306a36Sopenharmony_ci		bt->timeout -= time;
48362306a36Sopenharmony_ci		if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1))
48462306a36Sopenharmony_ci			return error_recovery(bt,
48562306a36Sopenharmony_ci					      status,
48662306a36Sopenharmony_ci					      IPMI_TIMEOUT_ERR);
48762306a36Sopenharmony_ci	}
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	switch (bt->state) {
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	/*
49262306a36Sopenharmony_ci	 * Idle state first checks for asynchronous messages from another
49362306a36Sopenharmony_ci	 * channel, then does some opportunistic housekeeping.
49462306a36Sopenharmony_ci	 */
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	case BT_STATE_IDLE:
49762306a36Sopenharmony_ci		if (status & BT_SMS_ATN) {
49862306a36Sopenharmony_ci			BT_CONTROL(BT_SMS_ATN);	/* clear it */
49962306a36Sopenharmony_ci			return SI_SM_ATTN;
50062306a36Sopenharmony_ci		}
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci		if (status & BT_H_BUSY)		/* clear a leftover H_BUSY */
50362306a36Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci		BT_SI_SM_RETURN(SI_SM_IDLE);
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	case BT_STATE_XACTION_START:
50862306a36Sopenharmony_ci		if (status & (BT_B_BUSY | BT_H2B_ATN))
50962306a36Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
51062306a36Sopenharmony_ci		if (BT_STATUS & BT_H_BUSY)
51162306a36Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);	/* force clear */
51262306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_WRITE_BYTES,
51362306a36Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci	case BT_STATE_WRITE_BYTES:
51662306a36Sopenharmony_ci		if (status & BT_H_BUSY)
51762306a36Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);	/* clear */
51862306a36Sopenharmony_ci		BT_CONTROL(BT_CLR_WR_PTR);
51962306a36Sopenharmony_ci		write_all_bytes(bt);
52062306a36Sopenharmony_ci		BT_CONTROL(BT_H2B_ATN);	/* can clear too fast to catch */
52162306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_WRITE_CONSUME,
52262306a36Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	case BT_STATE_WRITE_CONSUME:
52562306a36Sopenharmony_ci		if (status & (BT_B_BUSY | BT_H2B_ATN))
52662306a36Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
52762306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_READ_WAIT,
52862306a36Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	/* Spinning hard can suppress B2H_ATN and force a timeout */
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ci	case BT_STATE_READ_WAIT:
53362306a36Sopenharmony_ci		if (!(status & BT_B2H_ATN))
53462306a36Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
53562306a36Sopenharmony_ci		BT_CONTROL(BT_H_BUSY);		/* set */
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci		/*
53862306a36Sopenharmony_ci		 * Uncached, ordered writes should just proceed serially but
53962306a36Sopenharmony_ci		 * some BMCs don't clear B2H_ATN with one hit.  Fast-path a
54062306a36Sopenharmony_ci		 * workaround without too much penalty to the general case.
54162306a36Sopenharmony_ci		 */
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci		BT_CONTROL(BT_B2H_ATN);		/* clear it to ACK the BMC */
54462306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_CLEAR_B2H,
54562306a36Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	case BT_STATE_CLEAR_B2H:
54862306a36Sopenharmony_ci		if (status & BT_B2H_ATN) {
54962306a36Sopenharmony_ci			/* keep hitting it */
55062306a36Sopenharmony_ci			BT_CONTROL(BT_B2H_ATN);
55162306a36Sopenharmony_ci			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
55262306a36Sopenharmony_ci		}
55362306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_READ_BYTES,
55462306a36Sopenharmony_ci				SI_SM_CALL_WITHOUT_DELAY);
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	case BT_STATE_READ_BYTES:
55762306a36Sopenharmony_ci		if (!(status & BT_H_BUSY))
55862306a36Sopenharmony_ci			/* check in case of retry */
55962306a36Sopenharmony_ci			BT_CONTROL(BT_H_BUSY);
56062306a36Sopenharmony_ci		BT_CONTROL(BT_CLR_RD_PTR);	/* start of BMC2HOST buffer */
56162306a36Sopenharmony_ci		i = read_all_bytes(bt);		/* true == packet seq match */
56262306a36Sopenharmony_ci		BT_CONTROL(BT_H_BUSY);		/* NOW clear */
56362306a36Sopenharmony_ci		if (!i) 			/* Not my message */
56462306a36Sopenharmony_ci			BT_STATE_CHANGE(BT_STATE_READ_WAIT,
56562306a36Sopenharmony_ci					SI_SM_CALL_WITHOUT_DELAY);
56662306a36Sopenharmony_ci		bt->state = bt->complete;
56762306a36Sopenharmony_ci		return bt->state == BT_STATE_IDLE ?	/* where to next? */
56862306a36Sopenharmony_ci			SI_SM_TRANSACTION_COMPLETE :	/* normal */
56962306a36Sopenharmony_ci			SI_SM_CALL_WITHOUT_DELAY;	/* Startup magic */
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	case BT_STATE_LONG_BUSY:	/* For example: after FW update */
57262306a36Sopenharmony_ci		if (!(status & BT_B_BUSY)) {
57362306a36Sopenharmony_ci			reset_flags(bt);	/* next state is now IDLE */
57462306a36Sopenharmony_ci			bt_init_data(bt, bt->io);
57562306a36Sopenharmony_ci		}
57662306a36Sopenharmony_ci		return SI_SM_CALL_WITH_DELAY;	/* No repeat printing */
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci	case BT_STATE_RESET1:
57962306a36Sopenharmony_ci		reset_flags(bt);
58062306a36Sopenharmony_ci		drain_BMC2HOST(bt);
58162306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_RESET2,
58262306a36Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	case BT_STATE_RESET2:		/* Send a soft reset */
58562306a36Sopenharmony_ci		BT_CONTROL(BT_CLR_WR_PTR);
58662306a36Sopenharmony_ci		HOST2BMC(3);		/* number of bytes following */
58762306a36Sopenharmony_ci		HOST2BMC(0x18);		/* NetFn/LUN == Application, LUN 0 */
58862306a36Sopenharmony_ci		HOST2BMC(42);		/* Sequence number */
58962306a36Sopenharmony_ci		HOST2BMC(3);		/* Cmd == Soft reset */
59062306a36Sopenharmony_ci		BT_CONTROL(BT_H2B_ATN);
59162306a36Sopenharmony_ci		bt->timeout = BT_RESET_DELAY * USEC_PER_SEC;
59262306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_RESET3,
59362306a36Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci	case BT_STATE_RESET3:		/* Hold off everything for a bit */
59662306a36Sopenharmony_ci		if (bt->timeout > 0)
59762306a36Sopenharmony_ci			return SI_SM_CALL_WITH_DELAY;
59862306a36Sopenharmony_ci		drain_BMC2HOST(bt);
59962306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_RESTART,
60062306a36Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci	case BT_STATE_RESTART:		/* don't reset retries or seq! */
60362306a36Sopenharmony_ci		bt->read_count = 0;
60462306a36Sopenharmony_ci		bt->nonzero_status = 0;
60562306a36Sopenharmony_ci		bt->timeout = bt->BT_CAP_req2rsp;
60662306a36Sopenharmony_ci		BT_STATE_CHANGE(BT_STATE_XACTION_START,
60762306a36Sopenharmony_ci				SI_SM_CALL_WITH_DELAY);
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	default:	/* should never occur */
61062306a36Sopenharmony_ci		return error_recovery(bt,
61162306a36Sopenharmony_ci				      status,
61262306a36Sopenharmony_ci				      IPMI_ERR_UNSPECIFIED);
61362306a36Sopenharmony_ci	}
61462306a36Sopenharmony_ci	return SI_SM_CALL_WITH_DELAY;
61562306a36Sopenharmony_ci}
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_cistatic int bt_detect(struct si_sm_data *bt)
61862306a36Sopenharmony_ci{
61962306a36Sopenharmony_ci	unsigned char GetBT_CAP[] = { 0x18, 0x36 };
62062306a36Sopenharmony_ci	unsigned char BT_CAP[8];
62162306a36Sopenharmony_ci	enum si_sm_result smi_result;
62262306a36Sopenharmony_ci	int rv;
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ci	/*
62562306a36Sopenharmony_ci	 * It's impossible for the BT status and interrupt registers to be
62662306a36Sopenharmony_ci	 * all 1's, (assuming a properly functioning, self-initialized BMC)
62762306a36Sopenharmony_ci	 * but that's what you get from reading a bogus address, so we
62862306a36Sopenharmony_ci	 * test that first.  The calling routine uses negative logic.
62962306a36Sopenharmony_ci	 */
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
63262306a36Sopenharmony_ci		return 1;
63362306a36Sopenharmony_ci	reset_flags(bt);
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci	/*
63662306a36Sopenharmony_ci	 * Try getting the BT capabilities here.
63762306a36Sopenharmony_ci	 */
63862306a36Sopenharmony_ci	rv = bt_start_transaction(bt, GetBT_CAP, sizeof(GetBT_CAP));
63962306a36Sopenharmony_ci	if (rv) {
64062306a36Sopenharmony_ci		dev_warn(bt->io->dev,
64162306a36Sopenharmony_ci			 "Can't start capabilities transaction: %d\n", rv);
64262306a36Sopenharmony_ci		goto out_no_bt_cap;
64362306a36Sopenharmony_ci	}
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	smi_result = SI_SM_CALL_WITHOUT_DELAY;
64662306a36Sopenharmony_ci	for (;;) {
64762306a36Sopenharmony_ci		if (smi_result == SI_SM_CALL_WITH_DELAY ||
64862306a36Sopenharmony_ci		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
64962306a36Sopenharmony_ci			schedule_timeout_uninterruptible(1);
65062306a36Sopenharmony_ci			smi_result = bt_event(bt, jiffies_to_usecs(1));
65162306a36Sopenharmony_ci		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
65262306a36Sopenharmony_ci			smi_result = bt_event(bt, 0);
65362306a36Sopenharmony_ci		} else
65462306a36Sopenharmony_ci			break;
65562306a36Sopenharmony_ci	}
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	rv = bt_get_result(bt, BT_CAP, sizeof(BT_CAP));
65862306a36Sopenharmony_ci	bt_init_data(bt, bt->io);
65962306a36Sopenharmony_ci	if (rv < 8) {
66062306a36Sopenharmony_ci		dev_warn(bt->io->dev, "bt cap response too short: %d\n", rv);
66162306a36Sopenharmony_ci		goto out_no_bt_cap;
66262306a36Sopenharmony_ci	}
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ci	if (BT_CAP[2]) {
66562306a36Sopenharmony_ci		dev_warn(bt->io->dev, "Error fetching bt cap: %x\n", BT_CAP[2]);
66662306a36Sopenharmony_ciout_no_bt_cap:
66762306a36Sopenharmony_ci		dev_warn(bt->io->dev, "using default values\n");
66862306a36Sopenharmony_ci	} else {
66962306a36Sopenharmony_ci		bt->BT_CAP_req2rsp = BT_CAP[6] * USEC_PER_SEC;
67062306a36Sopenharmony_ci		bt->BT_CAP_retries = BT_CAP[7];
67162306a36Sopenharmony_ci	}
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	dev_info(bt->io->dev, "req2rsp=%ld secs retries=%d\n",
67462306a36Sopenharmony_ci		 bt->BT_CAP_req2rsp / USEC_PER_SEC, bt->BT_CAP_retries);
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	return 0;
67762306a36Sopenharmony_ci}
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_cistatic void bt_cleanup(struct si_sm_data *bt)
68062306a36Sopenharmony_ci{
68162306a36Sopenharmony_ci}
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_cistatic int bt_size(void)
68462306a36Sopenharmony_ci{
68562306a36Sopenharmony_ci	return sizeof(struct si_sm_data);
68662306a36Sopenharmony_ci}
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_ciconst struct si_sm_handlers bt_smi_handlers = {
68962306a36Sopenharmony_ci	.init_data		= bt_init_data,
69062306a36Sopenharmony_ci	.start_transaction	= bt_start_transaction,
69162306a36Sopenharmony_ci	.get_result		= bt_get_result,
69262306a36Sopenharmony_ci	.event			= bt_event,
69362306a36Sopenharmony_ci	.detect			= bt_detect,
69462306a36Sopenharmony_ci	.cleanup		= bt_cleanup,
69562306a36Sopenharmony_ci	.size			= bt_size,
69662306a36Sopenharmony_ci};
697