xref: /linux/drivers/char/ipmi/ipmi_bt_sm.c (revision c2b1e76d8c91166ca5f7aa8df02d67b619e24dc3)
1243ac210SCorey Minyard // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  ipmi_bt_sm.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part
6631dd1a8SJustin P. Mattock  *  of the driver architecture at http://sourceforge.net/projects/openipmi
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  Author:	Rocky Craig <first.last@hp.com>
9243ac210SCorey Minyard  */
101da177e4SLinus Torvalds 
11e3dbc6d1SCorey Minyard #define DEBUG /* So dev_dbg() is always available. */
12e3dbc6d1SCorey Minyard 
131da177e4SLinus Torvalds #include <linux/kernel.h> /* For printk. */
141da177e4SLinus Torvalds #include <linux/string.h>
15c4edff1cSCorey Minyard #include <linux/module.h>
16c4edff1cSCorey Minyard #include <linux/moduleparam.h>
171da177e4SLinus Torvalds #include <linux/ipmi_msgdefs.h>		/* for completion codes */
181da177e4SLinus Torvalds #include "ipmi_si_sm.h"
191da177e4SLinus Torvalds 
204d7cbac7SCorey Minyard #define BT_DEBUG_OFF	0	/* Used in production */
214d7cbac7SCorey Minyard #define BT_DEBUG_ENABLE	1	/* Generic messages */
224d7cbac7SCorey Minyard #define BT_DEBUG_MSG	2	/* Prints all request/response buffers */
234d7cbac7SCorey Minyard #define BT_DEBUG_STATES	4	/* Verbose look at state changes */
24c305e3d3SCorey Minyard /*
25c305e3d3SCorey Minyard  * BT_DEBUG_OFF must be zero to correspond to the default uninitialized
26c305e3d3SCorey Minyard  * value
27c305e3d3SCorey Minyard  */
281da177e4SLinus Torvalds 
290c8204b3SRandy Dunlap static int bt_debug; /* 0 == BT_DEBUG_OFF */
304d7cbac7SCorey Minyard 
31c4edff1cSCorey Minyard module_param(bt_debug, int, 0644);
32c4edff1cSCorey Minyard MODULE_PARM_DESC(bt_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
331da177e4SLinus Torvalds 
34c305e3d3SCorey Minyard /*
35c305e3d3SCorey Minyard  * Typical "Get BT Capabilities" values are 2-3 retries, 5-10 seconds,
36c305e3d3SCorey Minyard  * and 64 byte buffers.  However, one HP implementation wants 255 bytes of
37c305e3d3SCorey Minyard  * buffer (with a documented message of 160 bytes) so go for the max.
38c305e3d3SCorey Minyard  * Since the Open IPMI architecture is single-message oriented at this
39c305e3d3SCorey Minyard  * stage, the queue depth of BT is of no concern.
40c305e3d3SCorey Minyard  */
411da177e4SLinus Torvalds 
424d7cbac7SCorey Minyard #define BT_NORMAL_TIMEOUT	5	/* seconds */
434d7cbac7SCorey Minyard #define BT_NORMAL_RETRY_LIMIT	2
444d7cbac7SCorey Minyard #define BT_RESET_DELAY		6	/* seconds after warm reset */
454d7cbac7SCorey Minyard 
46c305e3d3SCorey Minyard /*
47c305e3d3SCorey Minyard  * States are written in chronological order and usually cover
48c305e3d3SCorey Minyard  * multiple rows of the state table discussion in the IPMI spec.
49c305e3d3SCorey Minyard  */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds enum bt_states {
524d7cbac7SCorey Minyard 	BT_STATE_IDLE = 0,	/* Order is critical in this list */
531da177e4SLinus Torvalds 	BT_STATE_XACTION_START,
541da177e4SLinus Torvalds 	BT_STATE_WRITE_BYTES,
551da177e4SLinus Torvalds 	BT_STATE_WRITE_CONSUME,
564d7cbac7SCorey Minyard 	BT_STATE_READ_WAIT,
574d7cbac7SCorey Minyard 	BT_STATE_CLEAR_B2H,
584d7cbac7SCorey Minyard 	BT_STATE_READ_BYTES,
591da177e4SLinus Torvalds 	BT_STATE_RESET1,	/* These must come last */
601da177e4SLinus Torvalds 	BT_STATE_RESET2,
611da177e4SLinus Torvalds 	BT_STATE_RESET3,
621da177e4SLinus Torvalds 	BT_STATE_RESTART,
634d7cbac7SCorey Minyard 	BT_STATE_PRINTME,
644d7cbac7SCorey Minyard 	BT_STATE_LONG_BUSY	/* BT doesn't get hosed :-) */
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
67c305e3d3SCorey Minyard /*
68c305e3d3SCorey Minyard  * Macros seen at the end of state "case" blocks.  They help with legibility
69c305e3d3SCorey Minyard  * and debugging.
70c305e3d3SCorey Minyard  */
714d7cbac7SCorey Minyard 
724d7cbac7SCorey Minyard #define BT_STATE_CHANGE(X, Y) { bt->state = X; return Y; }
734d7cbac7SCorey Minyard 
744d7cbac7SCorey Minyard #define BT_SI_SM_RETURN(Y)   { last_printed = BT_STATE_PRINTME; return Y; }
754d7cbac7SCorey Minyard 
761da177e4SLinus Torvalds struct si_sm_data {
771da177e4SLinus Torvalds 	enum bt_states	state;
781da177e4SLinus Torvalds 	unsigned char	seq;		/* BT sequence number */
791da177e4SLinus Torvalds 	struct si_sm_io	*io;
80a5f2b3d6SChen Gang 	unsigned char	write_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
811da177e4SLinus Torvalds 	int		write_count;
82a5f2b3d6SChen Gang 	unsigned char	read_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
831da177e4SLinus Torvalds 	int		read_count;
841da177e4SLinus Torvalds 	int		truncated;
854d7cbac7SCorey Minyard 	long		timeout;	/* microseconds countdown */
864d7cbac7SCorey Minyard 	int		error_retries;	/* end of "common" fields */
871da177e4SLinus Torvalds 	int		nonzero_status;	/* hung BMCs stay all 0 */
884d7cbac7SCorey Minyard 	enum bt_states	complete;	/* to divert the state machine */
894d7cbac7SCorey Minyard 	long		BT_CAP_req2rsp;
904d7cbac7SCorey Minyard 	int		BT_CAP_retries;	/* Recommended retries */
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define BT_CLR_WR_PTR	0x01	/* See IPMI 1.5 table 11.6.4 */
941da177e4SLinus Torvalds #define BT_CLR_RD_PTR	0x02
951da177e4SLinus Torvalds #define BT_H2B_ATN	0x04
961da177e4SLinus Torvalds #define BT_B2H_ATN	0x08
971da177e4SLinus Torvalds #define BT_SMS_ATN	0x10
981da177e4SLinus Torvalds #define BT_OEM0		0x20
991da177e4SLinus Torvalds #define BT_H_BUSY	0x40
1001da177e4SLinus Torvalds #define BT_B_BUSY	0x80
1011da177e4SLinus Torvalds 
102c305e3d3SCorey Minyard /*
103c305e3d3SCorey Minyard  * Some bits are toggled on each write: write once to set it, once
104c305e3d3SCorey Minyard  * more to clear it; writing a zero does nothing.  To absolutely
105c305e3d3SCorey Minyard  * clear it, check its state and write if set.  This avoids the "get
106c305e3d3SCorey Minyard  * current then use as mask" scheme to modify one bit.  Note that the
107c305e3d3SCorey Minyard  * variable "bt" is hardcoded into these macros.
108c305e3d3SCorey Minyard  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds #define BT_STATUS	bt->io->inputb(bt->io, 0)
1111da177e4SLinus Torvalds #define BT_CONTROL(x)	bt->io->outputb(bt->io, 0, x)
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #define BMC2HOST	bt->io->inputb(bt->io, 1)
1141da177e4SLinus Torvalds #define HOST2BMC(x)	bt->io->outputb(bt->io, 1, x)
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define BT_INTMASK_R	bt->io->inputb(bt->io, 2)
1171da177e4SLinus Torvalds #define BT_INTMASK_W(x)	bt->io->outputb(bt->io, 2, x)
1181da177e4SLinus Torvalds 
119c305e3d3SCorey Minyard /*
120c305e3d3SCorey Minyard  * Convenience routines for debugging.  These are not multi-open safe!
121c305e3d3SCorey Minyard  * Note the macros have hardcoded variables in them.
122c305e3d3SCorey Minyard  */
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static char *state2txt(unsigned char state)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	switch (state) {
1271da177e4SLinus Torvalds 	case BT_STATE_IDLE:		return("IDLE");
1281da177e4SLinus Torvalds 	case BT_STATE_XACTION_START:	return("XACTION");
1291da177e4SLinus Torvalds 	case BT_STATE_WRITE_BYTES:	return("WR_BYTES");
1301da177e4SLinus Torvalds 	case BT_STATE_WRITE_CONSUME:	return("WR_CONSUME");
1314d7cbac7SCorey Minyard 	case BT_STATE_READ_WAIT:	return("RD_WAIT");
1324d7cbac7SCorey Minyard 	case BT_STATE_CLEAR_B2H:	return("CLEAR_B2H");
1334d7cbac7SCorey Minyard 	case BT_STATE_READ_BYTES:	return("RD_BYTES");
1341da177e4SLinus Torvalds 	case BT_STATE_RESET1:		return("RESET1");
1351da177e4SLinus Torvalds 	case BT_STATE_RESET2:		return("RESET2");
1361da177e4SLinus Torvalds 	case BT_STATE_RESET3:		return("RESET3");
1371da177e4SLinus Torvalds 	case BT_STATE_RESTART:		return("RESTART");
1384d7cbac7SCorey Minyard 	case BT_STATE_LONG_BUSY:	return("LONG_BUSY");
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds 	return("BAD STATE");
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds #define STATE2TXT state2txt(bt->state)
1431da177e4SLinus Torvalds 
1444d7cbac7SCorey Minyard static char *status2txt(unsigned char status)
1451da177e4SLinus Torvalds {
1464d7cbac7SCorey Minyard 	/*
1474d7cbac7SCorey Minyard 	 * This cannot be called by two threads at the same time and
1484d7cbac7SCorey Minyard 	 * the buffer is always consumed immediately, so the static is
1494d7cbac7SCorey Minyard 	 * safe to use.
1504d7cbac7SCorey Minyard 	 */
1514d7cbac7SCorey Minyard 	static char buf[40];
1524d7cbac7SCorey Minyard 
1531da177e4SLinus Torvalds 	strcpy(buf, "[ ");
1544d7cbac7SCorey Minyard 	if (status & BT_B_BUSY)
1554d7cbac7SCorey Minyard 		strcat(buf, "B_BUSY ");
1564d7cbac7SCorey Minyard 	if (status & BT_H_BUSY)
1574d7cbac7SCorey Minyard 		strcat(buf, "H_BUSY ");
1584d7cbac7SCorey Minyard 	if (status & BT_OEM0)
1594d7cbac7SCorey Minyard 		strcat(buf, "OEM0 ");
1604d7cbac7SCorey Minyard 	if (status & BT_SMS_ATN)
1614d7cbac7SCorey Minyard 		strcat(buf, "SMS ");
1624d7cbac7SCorey Minyard 	if (status & BT_B2H_ATN)
1634d7cbac7SCorey Minyard 		strcat(buf, "B2H ");
1644d7cbac7SCorey Minyard 	if (status & BT_H2B_ATN)
1654d7cbac7SCorey Minyard 		strcat(buf, "H2B ");
1661da177e4SLinus Torvalds 	strcat(buf, "]");
1671da177e4SLinus Torvalds 	return buf;
1681da177e4SLinus Torvalds }
1694d7cbac7SCorey Minyard #define STATUS2TXT status2txt(status)
1701da177e4SLinus Torvalds 
1714d7cbac7SCorey Minyard /* called externally at insmod time, and internally on cleanup */
1724d7cbac7SCorey Minyard 
1731da177e4SLinus Torvalds static unsigned int bt_init_data(struct si_sm_data *bt, struct si_sm_io *io)
1741da177e4SLinus Torvalds {
1754d7cbac7SCorey Minyard 	memset(bt, 0, sizeof(struct si_sm_data));
176c305e3d3SCorey Minyard 	if (bt->io != io) {
177c305e3d3SCorey Minyard 		/* external: one-time only things */
1781da177e4SLinus Torvalds 		bt->io = io;
1794d7cbac7SCorey Minyard 		bt->seq = 0;
1804d7cbac7SCorey Minyard 	}
1814d7cbac7SCorey Minyard 	bt->state = BT_STATE_IDLE;	/* start here */
1824d7cbac7SCorey Minyard 	bt->complete = BT_STATE_IDLE;	/* end here */
183ccb3368cSXie XiuQi 	bt->BT_CAP_req2rsp = BT_NORMAL_TIMEOUT * USEC_PER_SEC;
1844d7cbac7SCorey Minyard 	bt->BT_CAP_retries = BT_NORMAL_RETRY_LIMIT;
1851da177e4SLinus Torvalds 	return 3; /* We claim 3 bytes of space; ought to check SPMI table */
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1884d7cbac7SCorey Minyard /* Jam a completion code (probably an error) into a response */
1894d7cbac7SCorey Minyard 
1904d7cbac7SCorey Minyard static void force_result(struct si_sm_data *bt, unsigned char completion_code)
1914d7cbac7SCorey Minyard {
1924d7cbac7SCorey Minyard 	bt->read_data[0] = 4;				/* # following bytes */
1934d7cbac7SCorey Minyard 	bt->read_data[1] = bt->write_data[1] | 4;	/* Odd NetFn/LUN */
1944d7cbac7SCorey Minyard 	bt->read_data[2] = bt->write_data[2];		/* seq (ignored) */
1954d7cbac7SCorey Minyard 	bt->read_data[3] = bt->write_data[3];		/* Command */
1964d7cbac7SCorey Minyard 	bt->read_data[4] = completion_code;
1974d7cbac7SCorey Minyard 	bt->read_count = 5;
1984d7cbac7SCorey Minyard }
1994d7cbac7SCorey Minyard 
2004d7cbac7SCorey Minyard /* The upper state machine starts here */
2014d7cbac7SCorey Minyard 
2021da177e4SLinus Torvalds static int bt_start_transaction(struct si_sm_data *bt,
2031da177e4SLinus Torvalds 				unsigned char *data,
2041da177e4SLinus Torvalds 				unsigned int size)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	unsigned int i;
2071da177e4SLinus Torvalds 
2084d7cbac7SCorey Minyard 	if (size < 2)
2094d7cbac7SCorey Minyard 		return IPMI_REQ_LEN_INVALID_ERR;
2104d7cbac7SCorey Minyard 	if (size > IPMI_MAX_MSG_LENGTH)
2114d7cbac7SCorey Minyard 		return IPMI_REQ_LEN_EXCEEDED_ERR;
2121da177e4SLinus Torvalds 
2134d7cbac7SCorey Minyard 	if (bt->state == BT_STATE_LONG_BUSY)
2144d7cbac7SCorey Minyard 		return IPMI_NODE_BUSY_ERR;
2154d7cbac7SCorey Minyard 
216*c2b1e76dSXianting Tian 	if (bt->state != BT_STATE_IDLE) {
217*c2b1e76dSXianting Tian 		dev_warn(bt->io->dev, "BT is now in the state %d\n", bt->state);
2184d7cbac7SCorey Minyard 		return IPMI_NOT_IN_MY_STATE_ERR;
219*c2b1e76dSXianting Tian 	}
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG) {
222e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev, "+++++++++++++++++ New command\n");
223e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev, "NetFn/LUN CMD [%d data]:", size - 2);
224e8b33617SCorey Minyard 		for (i = 0; i < size; i ++)
225f993cdd9SJoe Perches 			pr_cont(" %02x", data[i]);
226f993cdd9SJoe Perches 		pr_cont("\n");
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 	bt->write_data[0] = size + 1;	/* all data plus seq byte */
2291da177e4SLinus Torvalds 	bt->write_data[1] = *data;	/* NetFn/LUN */
2304d7cbac7SCorey Minyard 	bt->write_data[2] = bt->seq++;
2311da177e4SLinus Torvalds 	memcpy(bt->write_data + 3, data + 1, size - 1);
2321da177e4SLinus Torvalds 	bt->write_count = size + 2;
2331da177e4SLinus Torvalds 	bt->error_retries = 0;
2341da177e4SLinus Torvalds 	bt->nonzero_status = 0;
2351da177e4SLinus Torvalds 	bt->truncated = 0;
2361da177e4SLinus Torvalds 	bt->state = BT_STATE_XACTION_START;
2374d7cbac7SCorey Minyard 	bt->timeout = bt->BT_CAP_req2rsp;
2384d7cbac7SCorey Minyard 	force_result(bt, IPMI_ERR_UNSPECIFIED);
2391da177e4SLinus Torvalds 	return 0;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
242c305e3d3SCorey Minyard /*
243c305e3d3SCorey Minyard  * After the upper state machine has been told SI_SM_TRANSACTION_COMPLETE
244c305e3d3SCorey Minyard  * it calls this.  Strip out the length and seq bytes.
245c305e3d3SCorey Minyard  */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static int bt_get_result(struct si_sm_data *bt,
2481da177e4SLinus Torvalds 			 unsigned char *data,
2491da177e4SLinus Torvalds 			 unsigned int length)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	int i, msg_len;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	msg_len = bt->read_count - 2;		/* account for length & seq */
2541da177e4SLinus Torvalds 	if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
2554d7cbac7SCorey Minyard 		force_result(bt, IPMI_ERR_UNSPECIFIED);
2561da177e4SLinus Torvalds 		msg_len = 3;
2574d7cbac7SCorey Minyard 	}
2581da177e4SLinus Torvalds 	data[0] = bt->read_data[1];
2591da177e4SLinus Torvalds 	data[1] = bt->read_data[3];
2604d7cbac7SCorey Minyard 	if (length < msg_len || bt->truncated) {
2611da177e4SLinus Torvalds 		data[2] = IPMI_ERR_MSG_TRUNCATED;
2621da177e4SLinus Torvalds 		msg_len = 3;
263e8b33617SCorey Minyard 	} else
264e8b33617SCorey Minyard 		memcpy(data + 2, bt->read_data + 4, msg_len - 2);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG) {
267e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev, "result %d bytes:", msg_len);
268e8b33617SCorey Minyard 		for (i = 0; i < msg_len; i++)
269f993cdd9SJoe Perches 			pr_cont(" %02x", data[i]);
270f993cdd9SJoe Perches 		pr_cont("\n");
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	return msg_len;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /* This bit's functionality is optional */
2761da177e4SLinus Torvalds #define BT_BMC_HWRST	0x80
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static void reset_flags(struct si_sm_data *bt)
2791da177e4SLinus Torvalds {
2804d7cbac7SCorey Minyard 	if (bt_debug)
281e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev, "flag reset %s\n", status2txt(BT_STATUS));
282e8b33617SCorey Minyard 	if (BT_STATUS & BT_H_BUSY)
2834d7cbac7SCorey Minyard 		BT_CONTROL(BT_H_BUSY);	/* force clear */
2844d7cbac7SCorey Minyard 	BT_CONTROL(BT_CLR_WR_PTR);	/* always reset */
2854d7cbac7SCorey Minyard 	BT_CONTROL(BT_SMS_ATN);		/* always clear */
2864d7cbac7SCorey Minyard 	BT_INTMASK_W(BT_BMC_HWRST);
2871da177e4SLinus Torvalds }
2884d7cbac7SCorey Minyard 
289c305e3d3SCorey Minyard /*
290c305e3d3SCorey Minyard  * Get rid of an unwanted/stale response.  This should only be needed for
291c305e3d3SCorey Minyard  * BMCs that support multiple outstanding requests.
292c305e3d3SCorey Minyard  */
2934d7cbac7SCorey Minyard 
2944d7cbac7SCorey Minyard static void drain_BMC2HOST(struct si_sm_data *bt)
2954d7cbac7SCorey Minyard {
2964d7cbac7SCorey Minyard 	int i, size;
2974d7cbac7SCorey Minyard 
2984d7cbac7SCorey Minyard 	if (!(BT_STATUS & BT_B2H_ATN)) 	/* Not signalling a response */
2994d7cbac7SCorey Minyard 		return;
3004d7cbac7SCorey Minyard 
3014d7cbac7SCorey Minyard 	BT_CONTROL(BT_H_BUSY);		/* now set */
3024d7cbac7SCorey Minyard 	BT_CONTROL(BT_B2H_ATN);		/* always clear */
3034d7cbac7SCorey Minyard 	BT_STATUS;			/* pause */
3044d7cbac7SCorey Minyard 	BT_CONTROL(BT_B2H_ATN);		/* some BMCs are stubborn */
3054d7cbac7SCorey Minyard 	BT_CONTROL(BT_CLR_RD_PTR);	/* always reset */
3064d7cbac7SCorey Minyard 	if (bt_debug)
307e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev, "stale response %s; ",
308e3dbc6d1SCorey Minyard 			status2txt(BT_STATUS));
3094d7cbac7SCorey Minyard 	size = BMC2HOST;
3104d7cbac7SCorey Minyard 	for (i = 0; i < size ; i++)
3114d7cbac7SCorey Minyard 		BMC2HOST;
3124d7cbac7SCorey Minyard 	BT_CONTROL(BT_H_BUSY);		/* now clear */
3134d7cbac7SCorey Minyard 	if (bt_debug)
314f993cdd9SJoe Perches 		pr_cont("drained %d bytes\n", size + 1);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds static inline void write_all_bytes(struct si_sm_data *bt)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	int i;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG) {
322e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev, "write %d bytes seq=0x%02X",
3231da177e4SLinus Torvalds 			bt->write_count, bt->seq);
3241da177e4SLinus Torvalds 		for (i = 0; i < bt->write_count; i++)
325f993cdd9SJoe Perches 			pr_cont(" %02x", bt->write_data[i]);
326f993cdd9SJoe Perches 		pr_cont("\n");
3271da177e4SLinus Torvalds 	}
328e8b33617SCorey Minyard 	for (i = 0; i < bt->write_count; i++)
329e8b33617SCorey Minyard 		HOST2BMC(bt->write_data[i]);
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds static inline int read_all_bytes(struct si_sm_data *bt)
3331da177e4SLinus Torvalds {
334a94cdd1fSJiri Slaby 	unsigned int i;
3351da177e4SLinus Torvalds 
336c305e3d3SCorey Minyard 	/*
337c305e3d3SCorey Minyard 	 * length is "framing info", minimum = 4: NetFn, Seq, Cmd, cCode.
338c305e3d3SCorey Minyard 	 * Keep layout of first four bytes aligned with write_data[]
339c305e3d3SCorey Minyard 	 */
3404d7cbac7SCorey Minyard 
3411da177e4SLinus Torvalds 	bt->read_data[0] = BMC2HOST;
3421da177e4SLinus Torvalds 	bt->read_count = bt->read_data[0];
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	if (bt->read_count < 4 || bt->read_count >= IPMI_MAX_MSG_LENGTH) {
3451da177e4SLinus Torvalds 		if (bt_debug & BT_DEBUG_MSG)
346e3dbc6d1SCorey Minyard 			dev_dbg(bt->io->dev,
347e3dbc6d1SCorey Minyard 				"bad raw rsp len=%d\n", bt->read_count);
3481da177e4SLinus Torvalds 		bt->truncated = 1;
3491da177e4SLinus Torvalds 		return 1;	/* let next XACTION START clean it up */
3501da177e4SLinus Torvalds 	}
351e8b33617SCorey Minyard 	for (i = 1; i <= bt->read_count; i++)
352e8b33617SCorey Minyard 		bt->read_data[i] = BMC2HOST;
3534d7cbac7SCorey Minyard 	bt->read_count++;	/* Account internally for length byte */
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG) {
3564d7cbac7SCorey Minyard 		int max = bt->read_count;
3571da177e4SLinus Torvalds 
358e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev,
359e3dbc6d1SCorey Minyard 			"got %d bytes seq=0x%02X", max, bt->read_data[2]);
3604d7cbac7SCorey Minyard 		if (max > 16)
3614d7cbac7SCorey Minyard 			max = 16;
3624d7cbac7SCorey Minyard 		for (i = 0; i < max; i++)
363f993cdd9SJoe Perches 			pr_cont(" %02x", bt->read_data[i]);
364f993cdd9SJoe Perches 		pr_cont("%s\n", bt->read_count == max ? "" : " ...");
3654d7cbac7SCorey Minyard 	}
3664d7cbac7SCorey Minyard 
3674d7cbac7SCorey Minyard 	/* per the spec, the (NetFn[1], Seq[2], Cmd[3]) tuples must match */
3684d7cbac7SCorey Minyard 	if ((bt->read_data[3] == bt->write_data[3]) &&
3694d7cbac7SCorey Minyard 	    (bt->read_data[2] == bt->write_data[2]) &&
3701da177e4SLinus Torvalds 	    ((bt->read_data[1] & 0xF8) == (bt->write_data[1] & 0xF8)))
3711da177e4SLinus Torvalds 			return 1;
3721da177e4SLinus Torvalds 
373e8b33617SCorey Minyard 	if (bt_debug & BT_DEBUG_MSG)
374e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev,
375e3dbc6d1SCorey Minyard 			"IPMI BT: bad packet: want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
376f993cdd9SJoe Perches 			bt->write_data[1] | 0x04, bt->write_data[2],
377f993cdd9SJoe Perches 			bt->write_data[3],
3781da177e4SLinus Torvalds 			bt->read_data[1],  bt->read_data[2],  bt->read_data[3]);
3791da177e4SLinus Torvalds 	return 0;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3824d7cbac7SCorey Minyard /* Restart if retries are left, or return an error completion code */
3831da177e4SLinus Torvalds 
3844d7cbac7SCorey Minyard static enum si_sm_result error_recovery(struct si_sm_data *bt,
3854d7cbac7SCorey Minyard 					unsigned char status,
3864d7cbac7SCorey Minyard 					unsigned char cCode)
3871da177e4SLinus Torvalds {
3884d7cbac7SCorey Minyard 	char *reason;
3891da177e4SLinus Torvalds 
3904d7cbac7SCorey Minyard 	bt->timeout = bt->BT_CAP_req2rsp;
3911da177e4SLinus Torvalds 
3924d7cbac7SCorey Minyard 	switch (cCode) {
3934d7cbac7SCorey Minyard 	case IPMI_TIMEOUT_ERR:
3944d7cbac7SCorey Minyard 		reason = "timeout";
3954d7cbac7SCorey Minyard 		break;
3964d7cbac7SCorey Minyard 	default:
3974d7cbac7SCorey Minyard 		reason = "internal error";
3984d7cbac7SCorey Minyard 		break;
3994d7cbac7SCorey Minyard 	}
4001da177e4SLinus Torvalds 
401e3dbc6d1SCorey Minyard 	dev_warn(bt->io->dev, "IPMI BT: %s in %s %s ", /* open-ended line */
4024d7cbac7SCorey Minyard 		 reason, STATE2TXT, STATUS2TXT);
4034d7cbac7SCorey Minyard 
404c305e3d3SCorey Minyard 	/*
405c305e3d3SCorey Minyard 	 * Per the IPMI spec, retries are based on the sequence number
406c305e3d3SCorey Minyard 	 * known only to this module, so manage a restart here.
407c305e3d3SCorey Minyard 	 */
4081da177e4SLinus Torvalds 	(bt->error_retries)++;
4094d7cbac7SCorey Minyard 	if (bt->error_retries < bt->BT_CAP_retries) {
410f993cdd9SJoe Perches 		pr_cont("%d retries left\n",
4114d7cbac7SCorey Minyard 			bt->BT_CAP_retries - bt->error_retries);
4121da177e4SLinus Torvalds 		bt->state = BT_STATE_RESTART;
4134d7cbac7SCorey Minyard 		return SI_SM_CALL_WITHOUT_DELAY;
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
416e3dbc6d1SCorey Minyard 	dev_warn(bt->io->dev, "failed %d retries, sending error response\n",
4174d7cbac7SCorey Minyard 		 bt->BT_CAP_retries);
4184d7cbac7SCorey Minyard 	if (!bt->nonzero_status)
419e3dbc6d1SCorey Minyard 		dev_err(bt->io->dev, "stuck, try power cycle\n");
4204d7cbac7SCorey Minyard 
4214d7cbac7SCorey Minyard 	/* this is most likely during insmod */
4224d7cbac7SCorey Minyard 	else if (bt->seq <= (unsigned char)(bt->BT_CAP_retries & 0xFF)) {
423e3dbc6d1SCorey Minyard 		dev_warn(bt->io->dev, "BT reset (takes 5 secs)\n");
4244d7cbac7SCorey Minyard 		bt->state = BT_STATE_RESET1;
4254d7cbac7SCorey Minyard 		return SI_SM_CALL_WITHOUT_DELAY;
4264d7cbac7SCorey Minyard 	}
4274d7cbac7SCorey Minyard 
428c305e3d3SCorey Minyard 	/*
429c305e3d3SCorey Minyard 	 * Concoct a useful error message, set up the next state, and
430c305e3d3SCorey Minyard 	 * be done with this sequence.
431c305e3d3SCorey Minyard 	 */
4324d7cbac7SCorey Minyard 
4334d7cbac7SCorey Minyard 	bt->state = BT_STATE_IDLE;
4344d7cbac7SCorey Minyard 	switch (cCode) {
4354d7cbac7SCorey Minyard 	case IPMI_TIMEOUT_ERR:
4364d7cbac7SCorey Minyard 		if (status & BT_B_BUSY) {
4374d7cbac7SCorey Minyard 			cCode = IPMI_NODE_BUSY_ERR;
4384d7cbac7SCorey Minyard 			bt->state = BT_STATE_LONG_BUSY;
4394d7cbac7SCorey Minyard 		}
4404d7cbac7SCorey Minyard 		break;
4414d7cbac7SCorey Minyard 	default:
4424d7cbac7SCorey Minyard 		break;
4434d7cbac7SCorey Minyard 	}
4444d7cbac7SCorey Minyard 	force_result(bt, cCode);
4454d7cbac7SCorey Minyard 	return SI_SM_TRANSACTION_COMPLETE;
4464d7cbac7SCorey Minyard }
4474d7cbac7SCorey Minyard 
4484d7cbac7SCorey Minyard /* Check status and (usually) take action and change this state machine. */
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static enum si_sm_result bt_event(struct si_sm_data *bt, long time)
4511da177e4SLinus Torvalds {
452c86ba91bSCorey Minyard 	unsigned char status;
4534d7cbac7SCorey Minyard 	static enum bt_states last_printed = BT_STATE_PRINTME;
4541da177e4SLinus Torvalds 	int i;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	status = BT_STATUS;
4571da177e4SLinus Torvalds 	bt->nonzero_status |= status;
4584d7cbac7SCorey Minyard 	if ((bt_debug & BT_DEBUG_STATES) && (bt->state != last_printed)) {
459e3dbc6d1SCorey Minyard 		dev_dbg(bt->io->dev, "BT: %s %s TO=%ld - %ld\n",
4601da177e4SLinus Torvalds 			STATE2TXT,
4614d7cbac7SCorey Minyard 			STATUS2TXT,
4621da177e4SLinus Torvalds 			bt->timeout,
4631da177e4SLinus Torvalds 			time);
4644d7cbac7SCorey Minyard 		last_printed = bt->state;
4651da177e4SLinus Torvalds 	}
4664d7cbac7SCorey Minyard 
467c305e3d3SCorey Minyard 	/*
468c305e3d3SCorey Minyard 	 * Commands that time out may still (eventually) provide a response.
469c305e3d3SCorey Minyard 	 * This stale response will get in the way of a new response so remove
470c305e3d3SCorey Minyard 	 * it if possible (hopefully during IDLE).  Even if it comes up later
471c305e3d3SCorey Minyard 	 * it will be rejected by its (now-forgotten) seq number.
472c305e3d3SCorey Minyard 	 */
4734d7cbac7SCorey Minyard 
4744d7cbac7SCorey Minyard 	if ((bt->state < BT_STATE_WRITE_BYTES) && (status & BT_B2H_ATN)) {
4754d7cbac7SCorey Minyard 		drain_BMC2HOST(bt);
4764d7cbac7SCorey Minyard 		BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
4774d7cbac7SCorey Minyard 	}
4784d7cbac7SCorey Minyard 
4794d7cbac7SCorey Minyard 	if ((bt->state != BT_STATE_IDLE) &&
480c305e3d3SCorey Minyard 	    (bt->state <  BT_STATE_PRINTME)) {
481c305e3d3SCorey Minyard 		/* check timeout */
4824d7cbac7SCorey Minyard 		bt->timeout -= time;
4834d7cbac7SCorey Minyard 		if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1))
4844d7cbac7SCorey Minyard 			return error_recovery(bt,
4854d7cbac7SCorey Minyard 					      status,
4864d7cbac7SCorey Minyard 					      IPMI_TIMEOUT_ERR);
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	switch (bt->state) {
4901da177e4SLinus Torvalds 
491c305e3d3SCorey Minyard 	/*
492c305e3d3SCorey Minyard 	 * Idle state first checks for asynchronous messages from another
493c305e3d3SCorey Minyard 	 * channel, then does some opportunistic housekeeping.
494c305e3d3SCorey Minyard 	 */
4954d7cbac7SCorey Minyard 
4964d7cbac7SCorey Minyard 	case BT_STATE_IDLE:
4971da177e4SLinus Torvalds 		if (status & BT_SMS_ATN) {
4981da177e4SLinus Torvalds 			BT_CONTROL(BT_SMS_ATN);	/* clear it */
4991da177e4SLinus Torvalds 			return SI_SM_ATTN;
5001da177e4SLinus Torvalds 		}
5014d7cbac7SCorey Minyard 
5024d7cbac7SCorey Minyard 		if (status & BT_H_BUSY)		/* clear a leftover H_BUSY */
5034d7cbac7SCorey Minyard 			BT_CONTROL(BT_H_BUSY);
5044d7cbac7SCorey Minyard 
5054d7cbac7SCorey Minyard 		BT_SI_SM_RETURN(SI_SM_IDLE);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	case BT_STATE_XACTION_START:
5084d7cbac7SCorey Minyard 		if (status & (BT_B_BUSY | BT_H2B_ATN))
5094d7cbac7SCorey Minyard 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
5104d7cbac7SCorey Minyard 		if (BT_STATUS & BT_H_BUSY)
5114d7cbac7SCorey Minyard 			BT_CONTROL(BT_H_BUSY);	/* force clear */
5124d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_WRITE_BYTES,
5134d7cbac7SCorey Minyard 				SI_SM_CALL_WITHOUT_DELAY);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	case BT_STATE_WRITE_BYTES:
5164d7cbac7SCorey Minyard 		if (status & BT_H_BUSY)
5174d7cbac7SCorey Minyard 			BT_CONTROL(BT_H_BUSY);	/* clear */
5181da177e4SLinus Torvalds 		BT_CONTROL(BT_CLR_WR_PTR);
5191da177e4SLinus Torvalds 		write_all_bytes(bt);
5204d7cbac7SCorey Minyard 		BT_CONTROL(BT_H2B_ATN);	/* can clear too fast to catch */
5214d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_WRITE_CONSUME,
5224d7cbac7SCorey Minyard 				SI_SM_CALL_WITHOUT_DELAY);
5231da177e4SLinus Torvalds 
5244d7cbac7SCorey Minyard 	case BT_STATE_WRITE_CONSUME:
5254d7cbac7SCorey Minyard 		if (status & (BT_B_BUSY | BT_H2B_ATN))
5264d7cbac7SCorey Minyard 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
5274d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_READ_WAIT,
5284d7cbac7SCorey Minyard 				SI_SM_CALL_WITHOUT_DELAY);
5291da177e4SLinus Torvalds 
5304d7cbac7SCorey Minyard 	/* Spinning hard can suppress B2H_ATN and force a timeout */
5311da177e4SLinus Torvalds 
5324d7cbac7SCorey Minyard 	case BT_STATE_READ_WAIT:
533e8b33617SCorey Minyard 		if (!(status & BT_B2H_ATN))
5344d7cbac7SCorey Minyard 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
535e8b33617SCorey Minyard 		BT_CONTROL(BT_H_BUSY);		/* set */
5361da177e4SLinus Torvalds 
537c305e3d3SCorey Minyard 		/*
53842b2aa86SJustin P. Mattock 		 * Uncached, ordered writes should just proceed serially but
539c305e3d3SCorey Minyard 		 * some BMCs don't clear B2H_ATN with one hit.  Fast-path a
540c305e3d3SCorey Minyard 		 * workaround without too much penalty to the general case.
541c305e3d3SCorey Minyard 		 */
5421da177e4SLinus Torvalds 
5434d7cbac7SCorey Minyard 		BT_CONTROL(BT_B2H_ATN);		/* clear it to ACK the BMC */
5444d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_CLEAR_B2H,
5454d7cbac7SCorey Minyard 				SI_SM_CALL_WITHOUT_DELAY);
5461da177e4SLinus Torvalds 
5474d7cbac7SCorey Minyard 	case BT_STATE_CLEAR_B2H:
548c305e3d3SCorey Minyard 		if (status & BT_B2H_ATN) {
549c305e3d3SCorey Minyard 			/* keep hitting it */
5504d7cbac7SCorey Minyard 			BT_CONTROL(BT_B2H_ATN);
5514d7cbac7SCorey Minyard 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
5524d7cbac7SCorey Minyard 		}
5534d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_READ_BYTES,
5544d7cbac7SCorey Minyard 				SI_SM_CALL_WITHOUT_DELAY);
5551da177e4SLinus Torvalds 
5564d7cbac7SCorey Minyard 	case BT_STATE_READ_BYTES:
557c305e3d3SCorey Minyard 		if (!(status & BT_H_BUSY))
558c305e3d3SCorey Minyard 			/* check in case of retry */
5594d7cbac7SCorey Minyard 			BT_CONTROL(BT_H_BUSY);
5604d7cbac7SCorey Minyard 		BT_CONTROL(BT_CLR_RD_PTR);	/* start of BMC2HOST buffer */
5614d7cbac7SCorey Minyard 		i = read_all_bytes(bt);		/* true == packet seq match */
5624d7cbac7SCorey Minyard 		BT_CONTROL(BT_H_BUSY);		/* NOW clear */
5634d7cbac7SCorey Minyard 		if (!i) 			/* Not my message */
5644d7cbac7SCorey Minyard 			BT_STATE_CHANGE(BT_STATE_READ_WAIT,
5654d7cbac7SCorey Minyard 					SI_SM_CALL_WITHOUT_DELAY);
5664d7cbac7SCorey Minyard 		bt->state = bt->complete;
5674d7cbac7SCorey Minyard 		return bt->state == BT_STATE_IDLE ?	/* where to next? */
5684d7cbac7SCorey Minyard 			SI_SM_TRANSACTION_COMPLETE :	/* normal */
5694d7cbac7SCorey Minyard 			SI_SM_CALL_WITHOUT_DELAY;	/* Startup magic */
5704d7cbac7SCorey Minyard 
5714d7cbac7SCorey Minyard 	case BT_STATE_LONG_BUSY:	/* For example: after FW update */
5724d7cbac7SCorey Minyard 		if (!(status & BT_B_BUSY)) {
5734d7cbac7SCorey Minyard 			reset_flags(bt);	/* next state is now IDLE */
5744d7cbac7SCorey Minyard 			bt_init_data(bt, bt->io);
5754d7cbac7SCorey Minyard 		}
5764d7cbac7SCorey Minyard 		return SI_SM_CALL_WITH_DELAY;	/* No repeat printing */
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	case BT_STATE_RESET1:
5791da177e4SLinus Torvalds 		reset_flags(bt);
5804d7cbac7SCorey Minyard 		drain_BMC2HOST(bt);
5814d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_RESET2,
5824d7cbac7SCorey Minyard 				SI_SM_CALL_WITH_DELAY);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	case BT_STATE_RESET2:		/* Send a soft reset */
5851da177e4SLinus Torvalds 		BT_CONTROL(BT_CLR_WR_PTR);
5861da177e4SLinus Torvalds 		HOST2BMC(3);		/* number of bytes following */
5871da177e4SLinus Torvalds 		HOST2BMC(0x18);		/* NetFn/LUN == Application, LUN 0 */
5881da177e4SLinus Torvalds 		HOST2BMC(42);		/* Sequence number */
5891da177e4SLinus Torvalds 		HOST2BMC(3);		/* Cmd == Soft reset */
5901da177e4SLinus Torvalds 		BT_CONTROL(BT_H2B_ATN);
591ccb3368cSXie XiuQi 		bt->timeout = BT_RESET_DELAY * USEC_PER_SEC;
5924d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_RESET3,
5934d7cbac7SCorey Minyard 				SI_SM_CALL_WITH_DELAY);
5941da177e4SLinus Torvalds 
5954d7cbac7SCorey Minyard 	case BT_STATE_RESET3:		/* Hold off everything for a bit */
596e8b33617SCorey Minyard 		if (bt->timeout > 0)
597e8b33617SCorey Minyard 			return SI_SM_CALL_WITH_DELAY;
5984d7cbac7SCorey Minyard 		drain_BMC2HOST(bt);
5994d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_RESTART,
6004d7cbac7SCorey Minyard 				SI_SM_CALL_WITH_DELAY);
6011da177e4SLinus Torvalds 
6024d7cbac7SCorey Minyard 	case BT_STATE_RESTART:		/* don't reset retries or seq! */
6031da177e4SLinus Torvalds 		bt->read_count = 0;
6041da177e4SLinus Torvalds 		bt->nonzero_status = 0;
6054d7cbac7SCorey Minyard 		bt->timeout = bt->BT_CAP_req2rsp;
6064d7cbac7SCorey Minyard 		BT_STATE_CHANGE(BT_STATE_XACTION_START,
6074d7cbac7SCorey Minyard 				SI_SM_CALL_WITH_DELAY);
6081da177e4SLinus Torvalds 
6094d7cbac7SCorey Minyard 	default:	/* should never occur */
6104d7cbac7SCorey Minyard 		return error_recovery(bt,
6114d7cbac7SCorey Minyard 				      status,
6124d7cbac7SCorey Minyard 				      IPMI_ERR_UNSPECIFIED);
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 	return SI_SM_CALL_WITH_DELAY;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static int bt_detect(struct si_sm_data *bt)
6181da177e4SLinus Torvalds {
619c86ba91bSCorey Minyard 	unsigned char GetBT_CAP[] = { 0x18, 0x36 };
620c86ba91bSCorey Minyard 	unsigned char BT_CAP[8];
621c86ba91bSCorey Minyard 	enum si_sm_result smi_result;
622c86ba91bSCorey Minyard 	int rv;
623c86ba91bSCorey Minyard 
624c305e3d3SCorey Minyard 	/*
625c305e3d3SCorey Minyard 	 * It's impossible for the BT status and interrupt registers to be
626c305e3d3SCorey Minyard 	 * all 1's, (assuming a properly functioning, self-initialized BMC)
627c305e3d3SCorey Minyard 	 * but that's what you get from reading a bogus address, so we
628c305e3d3SCorey Minyard 	 * test that first.  The calling routine uses negative logic.
629c305e3d3SCorey Minyard 	 */
6301da177e4SLinus Torvalds 
631e8b33617SCorey Minyard 	if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
632e8b33617SCorey Minyard 		return 1;
6331da177e4SLinus Torvalds 	reset_flags(bt);
634c86ba91bSCorey Minyard 
635c86ba91bSCorey Minyard 	/*
636c86ba91bSCorey Minyard 	 * Try getting the BT capabilities here.
637c86ba91bSCorey Minyard 	 */
638c86ba91bSCorey Minyard 	rv = bt_start_transaction(bt, GetBT_CAP, sizeof(GetBT_CAP));
639c86ba91bSCorey Minyard 	if (rv) {
640c86ba91bSCorey Minyard 		dev_warn(bt->io->dev,
641c86ba91bSCorey Minyard 			 "Can't start capabilities transaction: %d\n", rv);
642c86ba91bSCorey Minyard 		goto out_no_bt_cap;
643c86ba91bSCorey Minyard 	}
644c86ba91bSCorey Minyard 
645c86ba91bSCorey Minyard 	smi_result = SI_SM_CALL_WITHOUT_DELAY;
646c86ba91bSCorey Minyard 	for (;;) {
647c86ba91bSCorey Minyard 		if (smi_result == SI_SM_CALL_WITH_DELAY ||
648c86ba91bSCorey Minyard 		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
649c86ba91bSCorey Minyard 			schedule_timeout_uninterruptible(1);
650c86ba91bSCorey Minyard 			smi_result = bt_event(bt, jiffies_to_usecs(1));
651c86ba91bSCorey Minyard 		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
652c86ba91bSCorey Minyard 			smi_result = bt_event(bt, 0);
653c86ba91bSCorey Minyard 		} else
654c86ba91bSCorey Minyard 			break;
655c86ba91bSCorey Minyard 	}
656c86ba91bSCorey Minyard 
657c86ba91bSCorey Minyard 	rv = bt_get_result(bt, BT_CAP, sizeof(BT_CAP));
658c86ba91bSCorey Minyard 	bt_init_data(bt, bt->io);
659c86ba91bSCorey Minyard 	if (rv < 8) {
660c86ba91bSCorey Minyard 		dev_warn(bt->io->dev, "bt cap response too short: %d\n", rv);
661c86ba91bSCorey Minyard 		goto out_no_bt_cap;
662c86ba91bSCorey Minyard 	}
663c86ba91bSCorey Minyard 
664c86ba91bSCorey Minyard 	if (BT_CAP[2]) {
665c86ba91bSCorey Minyard 		dev_warn(bt->io->dev, "Error fetching bt cap: %x\n", BT_CAP[2]);
666c86ba91bSCorey Minyard out_no_bt_cap:
667c86ba91bSCorey Minyard 		dev_warn(bt->io->dev, "using default values\n");
668c86ba91bSCorey Minyard 	} else {
669c86ba91bSCorey Minyard 		bt->BT_CAP_req2rsp = BT_CAP[6] * USEC_PER_SEC;
670c86ba91bSCorey Minyard 		bt->BT_CAP_retries = BT_CAP[7];
671c86ba91bSCorey Minyard 	}
672c86ba91bSCorey Minyard 
673c86ba91bSCorey Minyard 	dev_info(bt->io->dev, "req2rsp=%ld secs retries=%d\n",
674c86ba91bSCorey Minyard 		 bt->BT_CAP_req2rsp / USEC_PER_SEC, bt->BT_CAP_retries);
675c86ba91bSCorey Minyard 
6761da177e4SLinus Torvalds 	return 0;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds static void bt_cleanup(struct si_sm_data *bt)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds static int bt_size(void)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	return sizeof(struct si_sm_data);
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds 
68881d02b7fSCorey Minyard const struct si_sm_handlers bt_smi_handlers = {
6891da177e4SLinus Torvalds 	.init_data		= bt_init_data,
6901da177e4SLinus Torvalds 	.start_transaction	= bt_start_transaction,
6911da177e4SLinus Torvalds 	.get_result		= bt_get_result,
6921da177e4SLinus Torvalds 	.event			= bt_event,
6931da177e4SLinus Torvalds 	.detect			= bt_detect,
6941da177e4SLinus Torvalds 	.cleanup		= bt_cleanup,
6951da177e4SLinus Torvalds 	.size			= bt_size,
6961da177e4SLinus Torvalds };
697