xref: /OK3568_Linux_fs/kernel/drivers/char/ipmi/ipmi_bt_sm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  ipmi_bt_sm.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part
6*4882a593Smuzhiyun  *  of the driver architecture at http://sourceforge.net/projects/openipmi
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *  Author:	Rocky Craig <first.last@hp.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #define DEBUG /* So dev_dbg() is always available. */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h> /* For printk. */
14*4882a593Smuzhiyun #include <linux/string.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/moduleparam.h>
17*4882a593Smuzhiyun #include <linux/ipmi_msgdefs.h>		/* for completion codes */
18*4882a593Smuzhiyun #include "ipmi_si_sm.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define BT_DEBUG_OFF	0	/* Used in production */
21*4882a593Smuzhiyun #define BT_DEBUG_ENABLE	1	/* Generic messages */
22*4882a593Smuzhiyun #define BT_DEBUG_MSG	2	/* Prints all request/response buffers */
23*4882a593Smuzhiyun #define BT_DEBUG_STATES	4	/* Verbose look at state changes */
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * BT_DEBUG_OFF must be zero to correspond to the default uninitialized
26*4882a593Smuzhiyun  * value
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static int bt_debug; /* 0 == BT_DEBUG_OFF */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun module_param(bt_debug, int, 0644);
32*4882a593Smuzhiyun MODULE_PARM_DESC(bt_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * Typical "Get BT Capabilities" values are 2-3 retries, 5-10 seconds,
36*4882a593Smuzhiyun  * and 64 byte buffers.  However, one HP implementation wants 255 bytes of
37*4882a593Smuzhiyun  * buffer (with a documented message of 160 bytes) so go for the max.
38*4882a593Smuzhiyun  * Since the Open IPMI architecture is single-message oriented at this
39*4882a593Smuzhiyun  * stage, the queue depth of BT is of no concern.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define BT_NORMAL_TIMEOUT	5	/* seconds */
43*4882a593Smuzhiyun #define BT_NORMAL_RETRY_LIMIT	2
44*4882a593Smuzhiyun #define BT_RESET_DELAY		6	/* seconds after warm reset */
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun  * States are written in chronological order and usually cover
48*4882a593Smuzhiyun  * multiple rows of the state table discussion in the IPMI spec.
49*4882a593Smuzhiyun  */
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun enum bt_states {
52*4882a593Smuzhiyun 	BT_STATE_IDLE = 0,	/* Order is critical in this list */
53*4882a593Smuzhiyun 	BT_STATE_XACTION_START,
54*4882a593Smuzhiyun 	BT_STATE_WRITE_BYTES,
55*4882a593Smuzhiyun 	BT_STATE_WRITE_CONSUME,
56*4882a593Smuzhiyun 	BT_STATE_READ_WAIT,
57*4882a593Smuzhiyun 	BT_STATE_CLEAR_B2H,
58*4882a593Smuzhiyun 	BT_STATE_READ_BYTES,
59*4882a593Smuzhiyun 	BT_STATE_RESET1,	/* These must come last */
60*4882a593Smuzhiyun 	BT_STATE_RESET2,
61*4882a593Smuzhiyun 	BT_STATE_RESET3,
62*4882a593Smuzhiyun 	BT_STATE_RESTART,
63*4882a593Smuzhiyun 	BT_STATE_PRINTME,
64*4882a593Smuzhiyun 	BT_STATE_LONG_BUSY	/* BT doesn't get hosed :-) */
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun  * Macros seen at the end of state "case" blocks.  They help with legibility
69*4882a593Smuzhiyun  * and debugging.
70*4882a593Smuzhiyun  */
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #define BT_STATE_CHANGE(X, Y) { bt->state = X; return Y; }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #define BT_SI_SM_RETURN(Y)   { last_printed = BT_STATE_PRINTME; return Y; }
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun struct si_sm_data {
77*4882a593Smuzhiyun 	enum bt_states	state;
78*4882a593Smuzhiyun 	unsigned char	seq;		/* BT sequence number */
79*4882a593Smuzhiyun 	struct si_sm_io	*io;
80*4882a593Smuzhiyun 	unsigned char	write_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
81*4882a593Smuzhiyun 	int		write_count;
82*4882a593Smuzhiyun 	unsigned char	read_data[IPMI_MAX_MSG_LENGTH + 2]; /* +2 for memcpy */
83*4882a593Smuzhiyun 	int		read_count;
84*4882a593Smuzhiyun 	int		truncated;
85*4882a593Smuzhiyun 	long		timeout;	/* microseconds countdown */
86*4882a593Smuzhiyun 	int		error_retries;	/* end of "common" fields */
87*4882a593Smuzhiyun 	int		nonzero_status;	/* hung BMCs stay all 0 */
88*4882a593Smuzhiyun 	enum bt_states	complete;	/* to divert the state machine */
89*4882a593Smuzhiyun 	long		BT_CAP_req2rsp;
90*4882a593Smuzhiyun 	int		BT_CAP_retries;	/* Recommended retries */
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #define BT_CLR_WR_PTR	0x01	/* See IPMI 1.5 table 11.6.4 */
94*4882a593Smuzhiyun #define BT_CLR_RD_PTR	0x02
95*4882a593Smuzhiyun #define BT_H2B_ATN	0x04
96*4882a593Smuzhiyun #define BT_B2H_ATN	0x08
97*4882a593Smuzhiyun #define BT_SMS_ATN	0x10
98*4882a593Smuzhiyun #define BT_OEM0		0x20
99*4882a593Smuzhiyun #define BT_H_BUSY	0x40
100*4882a593Smuzhiyun #define BT_B_BUSY	0x80
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun  * Some bits are toggled on each write: write once to set it, once
104*4882a593Smuzhiyun  * more to clear it; writing a zero does nothing.  To absolutely
105*4882a593Smuzhiyun  * clear it, check its state and write if set.  This avoids the "get
106*4882a593Smuzhiyun  * current then use as mask" scheme to modify one bit.  Note that the
107*4882a593Smuzhiyun  * variable "bt" is hardcoded into these macros.
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #define BT_STATUS	bt->io->inputb(bt->io, 0)
111*4882a593Smuzhiyun #define BT_CONTROL(x)	bt->io->outputb(bt->io, 0, x)
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun #define BMC2HOST	bt->io->inputb(bt->io, 1)
114*4882a593Smuzhiyun #define HOST2BMC(x)	bt->io->outputb(bt->io, 1, x)
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun #define BT_INTMASK_R	bt->io->inputb(bt->io, 2)
117*4882a593Smuzhiyun #define BT_INTMASK_W(x)	bt->io->outputb(bt->io, 2, x)
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun  * Convenience routines for debugging.  These are not multi-open safe!
121*4882a593Smuzhiyun  * Note the macros have hardcoded variables in them.
122*4882a593Smuzhiyun  */
123*4882a593Smuzhiyun 
state2txt(unsigned char state)124*4882a593Smuzhiyun static char *state2txt(unsigned char state)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	switch (state) {
127*4882a593Smuzhiyun 	case BT_STATE_IDLE:		return("IDLE");
128*4882a593Smuzhiyun 	case BT_STATE_XACTION_START:	return("XACTION");
129*4882a593Smuzhiyun 	case BT_STATE_WRITE_BYTES:	return("WR_BYTES");
130*4882a593Smuzhiyun 	case BT_STATE_WRITE_CONSUME:	return("WR_CONSUME");
131*4882a593Smuzhiyun 	case BT_STATE_READ_WAIT:	return("RD_WAIT");
132*4882a593Smuzhiyun 	case BT_STATE_CLEAR_B2H:	return("CLEAR_B2H");
133*4882a593Smuzhiyun 	case BT_STATE_READ_BYTES:	return("RD_BYTES");
134*4882a593Smuzhiyun 	case BT_STATE_RESET1:		return("RESET1");
135*4882a593Smuzhiyun 	case BT_STATE_RESET2:		return("RESET2");
136*4882a593Smuzhiyun 	case BT_STATE_RESET3:		return("RESET3");
137*4882a593Smuzhiyun 	case BT_STATE_RESTART:		return("RESTART");
138*4882a593Smuzhiyun 	case BT_STATE_LONG_BUSY:	return("LONG_BUSY");
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 	return("BAD STATE");
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun #define STATE2TXT state2txt(bt->state)
143*4882a593Smuzhiyun 
status2txt(unsigned char status)144*4882a593Smuzhiyun static char *status2txt(unsigned char status)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	/*
147*4882a593Smuzhiyun 	 * This cannot be called by two threads at the same time and
148*4882a593Smuzhiyun 	 * the buffer is always consumed immediately, so the static is
149*4882a593Smuzhiyun 	 * safe to use.
150*4882a593Smuzhiyun 	 */
151*4882a593Smuzhiyun 	static char buf[40];
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	strcpy(buf, "[ ");
154*4882a593Smuzhiyun 	if (status & BT_B_BUSY)
155*4882a593Smuzhiyun 		strcat(buf, "B_BUSY ");
156*4882a593Smuzhiyun 	if (status & BT_H_BUSY)
157*4882a593Smuzhiyun 		strcat(buf, "H_BUSY ");
158*4882a593Smuzhiyun 	if (status & BT_OEM0)
159*4882a593Smuzhiyun 		strcat(buf, "OEM0 ");
160*4882a593Smuzhiyun 	if (status & BT_SMS_ATN)
161*4882a593Smuzhiyun 		strcat(buf, "SMS ");
162*4882a593Smuzhiyun 	if (status & BT_B2H_ATN)
163*4882a593Smuzhiyun 		strcat(buf, "B2H ");
164*4882a593Smuzhiyun 	if (status & BT_H2B_ATN)
165*4882a593Smuzhiyun 		strcat(buf, "H2B ");
166*4882a593Smuzhiyun 	strcat(buf, "]");
167*4882a593Smuzhiyun 	return buf;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun #define STATUS2TXT status2txt(status)
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /* called externally at insmod time, and internally on cleanup */
172*4882a593Smuzhiyun 
bt_init_data(struct si_sm_data * bt,struct si_sm_io * io)173*4882a593Smuzhiyun static unsigned int bt_init_data(struct si_sm_data *bt, struct si_sm_io *io)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	memset(bt, 0, sizeof(struct si_sm_data));
176*4882a593Smuzhiyun 	if (bt->io != io) {
177*4882a593Smuzhiyun 		/* external: one-time only things */
178*4882a593Smuzhiyun 		bt->io = io;
179*4882a593Smuzhiyun 		bt->seq = 0;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 	bt->state = BT_STATE_IDLE;	/* start here */
182*4882a593Smuzhiyun 	bt->complete = BT_STATE_IDLE;	/* end here */
183*4882a593Smuzhiyun 	bt->BT_CAP_req2rsp = BT_NORMAL_TIMEOUT * USEC_PER_SEC;
184*4882a593Smuzhiyun 	bt->BT_CAP_retries = BT_NORMAL_RETRY_LIMIT;
185*4882a593Smuzhiyun 	return 3; /* We claim 3 bytes of space; ought to check SPMI table */
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /* Jam a completion code (probably an error) into a response */
189*4882a593Smuzhiyun 
force_result(struct si_sm_data * bt,unsigned char completion_code)190*4882a593Smuzhiyun static void force_result(struct si_sm_data *bt, unsigned char completion_code)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	bt->read_data[0] = 4;				/* # following bytes */
193*4882a593Smuzhiyun 	bt->read_data[1] = bt->write_data[1] | 4;	/* Odd NetFn/LUN */
194*4882a593Smuzhiyun 	bt->read_data[2] = bt->write_data[2];		/* seq (ignored) */
195*4882a593Smuzhiyun 	bt->read_data[3] = bt->write_data[3];		/* Command */
196*4882a593Smuzhiyun 	bt->read_data[4] = completion_code;
197*4882a593Smuzhiyun 	bt->read_count = 5;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun /* The upper state machine starts here */
201*4882a593Smuzhiyun 
bt_start_transaction(struct si_sm_data * bt,unsigned char * data,unsigned int size)202*4882a593Smuzhiyun static int bt_start_transaction(struct si_sm_data *bt,
203*4882a593Smuzhiyun 				unsigned char *data,
204*4882a593Smuzhiyun 				unsigned int size)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	unsigned int i;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	if (size < 2)
209*4882a593Smuzhiyun 		return IPMI_REQ_LEN_INVALID_ERR;
210*4882a593Smuzhiyun 	if (size > IPMI_MAX_MSG_LENGTH)
211*4882a593Smuzhiyun 		return IPMI_REQ_LEN_EXCEEDED_ERR;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (bt->state == BT_STATE_LONG_BUSY)
214*4882a593Smuzhiyun 		return IPMI_NODE_BUSY_ERR;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (bt->state != BT_STATE_IDLE) {
217*4882a593Smuzhiyun 		dev_warn(bt->io->dev, "BT in invalid state %d\n", bt->state);
218*4882a593Smuzhiyun 		return IPMI_NOT_IN_MY_STATE_ERR;
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (bt_debug & BT_DEBUG_MSG) {
222*4882a593Smuzhiyun 		dev_dbg(bt->io->dev, "+++++++++++++++++ New command\n");
223*4882a593Smuzhiyun 		dev_dbg(bt->io->dev, "NetFn/LUN CMD [%d data]:", size - 2);
224*4882a593Smuzhiyun 		for (i = 0; i < size; i ++)
225*4882a593Smuzhiyun 			pr_cont(" %02x", data[i]);
226*4882a593Smuzhiyun 		pr_cont("\n");
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 	bt->write_data[0] = size + 1;	/* all data plus seq byte */
229*4882a593Smuzhiyun 	bt->write_data[1] = *data;	/* NetFn/LUN */
230*4882a593Smuzhiyun 	bt->write_data[2] = bt->seq++;
231*4882a593Smuzhiyun 	memcpy(bt->write_data + 3, data + 1, size - 1);
232*4882a593Smuzhiyun 	bt->write_count = size + 2;
233*4882a593Smuzhiyun 	bt->error_retries = 0;
234*4882a593Smuzhiyun 	bt->nonzero_status = 0;
235*4882a593Smuzhiyun 	bt->truncated = 0;
236*4882a593Smuzhiyun 	bt->state = BT_STATE_XACTION_START;
237*4882a593Smuzhiyun 	bt->timeout = bt->BT_CAP_req2rsp;
238*4882a593Smuzhiyun 	force_result(bt, IPMI_ERR_UNSPECIFIED);
239*4882a593Smuzhiyun 	return 0;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun /*
243*4882a593Smuzhiyun  * After the upper state machine has been told SI_SM_TRANSACTION_COMPLETE
244*4882a593Smuzhiyun  * it calls this.  Strip out the length and seq bytes.
245*4882a593Smuzhiyun  */
246*4882a593Smuzhiyun 
bt_get_result(struct si_sm_data * bt,unsigned char * data,unsigned int length)247*4882a593Smuzhiyun static int bt_get_result(struct si_sm_data *bt,
248*4882a593Smuzhiyun 			 unsigned char *data,
249*4882a593Smuzhiyun 			 unsigned int length)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	int i, msg_len;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	msg_len = bt->read_count - 2;		/* account for length & seq */
254*4882a593Smuzhiyun 	if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
255*4882a593Smuzhiyun 		force_result(bt, IPMI_ERR_UNSPECIFIED);
256*4882a593Smuzhiyun 		msg_len = 3;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 	data[0] = bt->read_data[1];
259*4882a593Smuzhiyun 	data[1] = bt->read_data[3];
260*4882a593Smuzhiyun 	if (length < msg_len || bt->truncated) {
261*4882a593Smuzhiyun 		data[2] = IPMI_ERR_MSG_TRUNCATED;
262*4882a593Smuzhiyun 		msg_len = 3;
263*4882a593Smuzhiyun 	} else
264*4882a593Smuzhiyun 		memcpy(data + 2, bt->read_data + 4, msg_len - 2);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (bt_debug & BT_DEBUG_MSG) {
267*4882a593Smuzhiyun 		dev_dbg(bt->io->dev, "result %d bytes:", msg_len);
268*4882a593Smuzhiyun 		for (i = 0; i < msg_len; i++)
269*4882a593Smuzhiyun 			pr_cont(" %02x", data[i]);
270*4882a593Smuzhiyun 		pr_cont("\n");
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 	return msg_len;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /* This bit's functionality is optional */
276*4882a593Smuzhiyun #define BT_BMC_HWRST	0x80
277*4882a593Smuzhiyun 
reset_flags(struct si_sm_data * bt)278*4882a593Smuzhiyun static void reset_flags(struct si_sm_data *bt)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	if (bt_debug)
281*4882a593Smuzhiyun 		dev_dbg(bt->io->dev, "flag reset %s\n", status2txt(BT_STATUS));
282*4882a593Smuzhiyun 	if (BT_STATUS & BT_H_BUSY)
283*4882a593Smuzhiyun 		BT_CONTROL(BT_H_BUSY);	/* force clear */
284*4882a593Smuzhiyun 	BT_CONTROL(BT_CLR_WR_PTR);	/* always reset */
285*4882a593Smuzhiyun 	BT_CONTROL(BT_SMS_ATN);		/* always clear */
286*4882a593Smuzhiyun 	BT_INTMASK_W(BT_BMC_HWRST);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun  * Get rid of an unwanted/stale response.  This should only be needed for
291*4882a593Smuzhiyun  * BMCs that support multiple outstanding requests.
292*4882a593Smuzhiyun  */
293*4882a593Smuzhiyun 
drain_BMC2HOST(struct si_sm_data * bt)294*4882a593Smuzhiyun static void drain_BMC2HOST(struct si_sm_data *bt)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	int i, size;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (!(BT_STATUS & BT_B2H_ATN)) 	/* Not signalling a response */
299*4882a593Smuzhiyun 		return;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	BT_CONTROL(BT_H_BUSY);		/* now set */
302*4882a593Smuzhiyun 	BT_CONTROL(BT_B2H_ATN);		/* always clear */
303*4882a593Smuzhiyun 	BT_STATUS;			/* pause */
304*4882a593Smuzhiyun 	BT_CONTROL(BT_B2H_ATN);		/* some BMCs are stubborn */
305*4882a593Smuzhiyun 	BT_CONTROL(BT_CLR_RD_PTR);	/* always reset */
306*4882a593Smuzhiyun 	if (bt_debug)
307*4882a593Smuzhiyun 		dev_dbg(bt->io->dev, "stale response %s; ",
308*4882a593Smuzhiyun 			status2txt(BT_STATUS));
309*4882a593Smuzhiyun 	size = BMC2HOST;
310*4882a593Smuzhiyun 	for (i = 0; i < size ; i++)
311*4882a593Smuzhiyun 		BMC2HOST;
312*4882a593Smuzhiyun 	BT_CONTROL(BT_H_BUSY);		/* now clear */
313*4882a593Smuzhiyun 	if (bt_debug)
314*4882a593Smuzhiyun 		pr_cont("drained %d bytes\n", size + 1);
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
write_all_bytes(struct si_sm_data * bt)317*4882a593Smuzhiyun static inline void write_all_bytes(struct si_sm_data *bt)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	int i;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (bt_debug & BT_DEBUG_MSG) {
322*4882a593Smuzhiyun 		dev_dbg(bt->io->dev, "write %d bytes seq=0x%02X",
323*4882a593Smuzhiyun 			bt->write_count, bt->seq);
324*4882a593Smuzhiyun 		for (i = 0; i < bt->write_count; i++)
325*4882a593Smuzhiyun 			pr_cont(" %02x", bt->write_data[i]);
326*4882a593Smuzhiyun 		pr_cont("\n");
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 	for (i = 0; i < bt->write_count; i++)
329*4882a593Smuzhiyun 		HOST2BMC(bt->write_data[i]);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
read_all_bytes(struct si_sm_data * bt)332*4882a593Smuzhiyun static inline int read_all_bytes(struct si_sm_data *bt)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	unsigned int i;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	/*
337*4882a593Smuzhiyun 	 * length is "framing info", minimum = 4: NetFn, Seq, Cmd, cCode.
338*4882a593Smuzhiyun 	 * Keep layout of first four bytes aligned with write_data[]
339*4882a593Smuzhiyun 	 */
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	bt->read_data[0] = BMC2HOST;
342*4882a593Smuzhiyun 	bt->read_count = bt->read_data[0];
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	if (bt->read_count < 4 || bt->read_count >= IPMI_MAX_MSG_LENGTH) {
345*4882a593Smuzhiyun 		if (bt_debug & BT_DEBUG_MSG)
346*4882a593Smuzhiyun 			dev_dbg(bt->io->dev,
347*4882a593Smuzhiyun 				"bad raw rsp len=%d\n", bt->read_count);
348*4882a593Smuzhiyun 		bt->truncated = 1;
349*4882a593Smuzhiyun 		return 1;	/* let next XACTION START clean it up */
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 	for (i = 1; i <= bt->read_count; i++)
352*4882a593Smuzhiyun 		bt->read_data[i] = BMC2HOST;
353*4882a593Smuzhiyun 	bt->read_count++;	/* Account internally for length byte */
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	if (bt_debug & BT_DEBUG_MSG) {
356*4882a593Smuzhiyun 		int max = bt->read_count;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		dev_dbg(bt->io->dev,
359*4882a593Smuzhiyun 			"got %d bytes seq=0x%02X", max, bt->read_data[2]);
360*4882a593Smuzhiyun 		if (max > 16)
361*4882a593Smuzhiyun 			max = 16;
362*4882a593Smuzhiyun 		for (i = 0; i < max; i++)
363*4882a593Smuzhiyun 			pr_cont(" %02x", bt->read_data[i]);
364*4882a593Smuzhiyun 		pr_cont("%s\n", bt->read_count == max ? "" : " ...");
365*4882a593Smuzhiyun 	}
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* per the spec, the (NetFn[1], Seq[2], Cmd[3]) tuples must match */
368*4882a593Smuzhiyun 	if ((bt->read_data[3] == bt->write_data[3]) &&
369*4882a593Smuzhiyun 	    (bt->read_data[2] == bt->write_data[2]) &&
370*4882a593Smuzhiyun 	    ((bt->read_data[1] & 0xF8) == (bt->write_data[1] & 0xF8)))
371*4882a593Smuzhiyun 			return 1;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	if (bt_debug & BT_DEBUG_MSG)
374*4882a593Smuzhiyun 		dev_dbg(bt->io->dev,
375*4882a593Smuzhiyun 			"IPMI BT: bad packet: want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
376*4882a593Smuzhiyun 			bt->write_data[1] | 0x04, bt->write_data[2],
377*4882a593Smuzhiyun 			bt->write_data[3],
378*4882a593Smuzhiyun 			bt->read_data[1],  bt->read_data[2],  bt->read_data[3]);
379*4882a593Smuzhiyun 	return 0;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /* Restart if retries are left, or return an error completion code */
383*4882a593Smuzhiyun 
error_recovery(struct si_sm_data * bt,unsigned char status,unsigned char cCode)384*4882a593Smuzhiyun static enum si_sm_result error_recovery(struct si_sm_data *bt,
385*4882a593Smuzhiyun 					unsigned char status,
386*4882a593Smuzhiyun 					unsigned char cCode)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun 	char *reason;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	bt->timeout = bt->BT_CAP_req2rsp;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	switch (cCode) {
393*4882a593Smuzhiyun 	case IPMI_TIMEOUT_ERR:
394*4882a593Smuzhiyun 		reason = "timeout";
395*4882a593Smuzhiyun 		break;
396*4882a593Smuzhiyun 	default:
397*4882a593Smuzhiyun 		reason = "internal error";
398*4882a593Smuzhiyun 		break;
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	dev_warn(bt->io->dev, "IPMI BT: %s in %s %s ", /* open-ended line */
402*4882a593Smuzhiyun 		 reason, STATE2TXT, STATUS2TXT);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	/*
405*4882a593Smuzhiyun 	 * Per the IPMI spec, retries are based on the sequence number
406*4882a593Smuzhiyun 	 * known only to this module, so manage a restart here.
407*4882a593Smuzhiyun 	 */
408*4882a593Smuzhiyun 	(bt->error_retries)++;
409*4882a593Smuzhiyun 	if (bt->error_retries < bt->BT_CAP_retries) {
410*4882a593Smuzhiyun 		pr_cont("%d retries left\n",
411*4882a593Smuzhiyun 			bt->BT_CAP_retries - bt->error_retries);
412*4882a593Smuzhiyun 		bt->state = BT_STATE_RESTART;
413*4882a593Smuzhiyun 		return SI_SM_CALL_WITHOUT_DELAY;
414*4882a593Smuzhiyun 	}
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	dev_warn(bt->io->dev, "failed %d retries, sending error response\n",
417*4882a593Smuzhiyun 		 bt->BT_CAP_retries);
418*4882a593Smuzhiyun 	if (!bt->nonzero_status)
419*4882a593Smuzhiyun 		dev_err(bt->io->dev, "stuck, try power cycle\n");
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	/* this is most likely during insmod */
422*4882a593Smuzhiyun 	else if (bt->seq <= (unsigned char)(bt->BT_CAP_retries & 0xFF)) {
423*4882a593Smuzhiyun 		dev_warn(bt->io->dev, "BT reset (takes 5 secs)\n");
424*4882a593Smuzhiyun 		bt->state = BT_STATE_RESET1;
425*4882a593Smuzhiyun 		return SI_SM_CALL_WITHOUT_DELAY;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	/*
429*4882a593Smuzhiyun 	 * Concoct a useful error message, set up the next state, and
430*4882a593Smuzhiyun 	 * be done with this sequence.
431*4882a593Smuzhiyun 	 */
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	bt->state = BT_STATE_IDLE;
434*4882a593Smuzhiyun 	switch (cCode) {
435*4882a593Smuzhiyun 	case IPMI_TIMEOUT_ERR:
436*4882a593Smuzhiyun 		if (status & BT_B_BUSY) {
437*4882a593Smuzhiyun 			cCode = IPMI_NODE_BUSY_ERR;
438*4882a593Smuzhiyun 			bt->state = BT_STATE_LONG_BUSY;
439*4882a593Smuzhiyun 		}
440*4882a593Smuzhiyun 		break;
441*4882a593Smuzhiyun 	default:
442*4882a593Smuzhiyun 		break;
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun 	force_result(bt, cCode);
445*4882a593Smuzhiyun 	return SI_SM_TRANSACTION_COMPLETE;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun /* Check status and (usually) take action and change this state machine. */
449*4882a593Smuzhiyun 
bt_event(struct si_sm_data * bt,long time)450*4882a593Smuzhiyun static enum si_sm_result bt_event(struct si_sm_data *bt, long time)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	unsigned char status;
453*4882a593Smuzhiyun 	static enum bt_states last_printed = BT_STATE_PRINTME;
454*4882a593Smuzhiyun 	int i;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	status = BT_STATUS;
457*4882a593Smuzhiyun 	bt->nonzero_status |= status;
458*4882a593Smuzhiyun 	if ((bt_debug & BT_DEBUG_STATES) && (bt->state != last_printed)) {
459*4882a593Smuzhiyun 		dev_dbg(bt->io->dev, "BT: %s %s TO=%ld - %ld\n",
460*4882a593Smuzhiyun 			STATE2TXT,
461*4882a593Smuzhiyun 			STATUS2TXT,
462*4882a593Smuzhiyun 			bt->timeout,
463*4882a593Smuzhiyun 			time);
464*4882a593Smuzhiyun 		last_printed = bt->state;
465*4882a593Smuzhiyun 	}
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	/*
468*4882a593Smuzhiyun 	 * Commands that time out may still (eventually) provide a response.
469*4882a593Smuzhiyun 	 * This stale response will get in the way of a new response so remove
470*4882a593Smuzhiyun 	 * it if possible (hopefully during IDLE).  Even if it comes up later
471*4882a593Smuzhiyun 	 * it will be rejected by its (now-forgotten) seq number.
472*4882a593Smuzhiyun 	 */
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	if ((bt->state < BT_STATE_WRITE_BYTES) && (status & BT_B2H_ATN)) {
475*4882a593Smuzhiyun 		drain_BMC2HOST(bt);
476*4882a593Smuzhiyun 		BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
477*4882a593Smuzhiyun 	}
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	if ((bt->state != BT_STATE_IDLE) &&
480*4882a593Smuzhiyun 	    (bt->state <  BT_STATE_PRINTME)) {
481*4882a593Smuzhiyun 		/* check timeout */
482*4882a593Smuzhiyun 		bt->timeout -= time;
483*4882a593Smuzhiyun 		if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1))
484*4882a593Smuzhiyun 			return error_recovery(bt,
485*4882a593Smuzhiyun 					      status,
486*4882a593Smuzhiyun 					      IPMI_TIMEOUT_ERR);
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	switch (bt->state) {
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/*
492*4882a593Smuzhiyun 	 * Idle state first checks for asynchronous messages from another
493*4882a593Smuzhiyun 	 * channel, then does some opportunistic housekeeping.
494*4882a593Smuzhiyun 	 */
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	case BT_STATE_IDLE:
497*4882a593Smuzhiyun 		if (status & BT_SMS_ATN) {
498*4882a593Smuzhiyun 			BT_CONTROL(BT_SMS_ATN);	/* clear it */
499*4882a593Smuzhiyun 			return SI_SM_ATTN;
500*4882a593Smuzhiyun 		}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 		if (status & BT_H_BUSY)		/* clear a leftover H_BUSY */
503*4882a593Smuzhiyun 			BT_CONTROL(BT_H_BUSY);
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 		BT_SI_SM_RETURN(SI_SM_IDLE);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	case BT_STATE_XACTION_START:
508*4882a593Smuzhiyun 		if (status & (BT_B_BUSY | BT_H2B_ATN))
509*4882a593Smuzhiyun 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
510*4882a593Smuzhiyun 		if (BT_STATUS & BT_H_BUSY)
511*4882a593Smuzhiyun 			BT_CONTROL(BT_H_BUSY);	/* force clear */
512*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_WRITE_BYTES,
513*4882a593Smuzhiyun 				SI_SM_CALL_WITHOUT_DELAY);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	case BT_STATE_WRITE_BYTES:
516*4882a593Smuzhiyun 		if (status & BT_H_BUSY)
517*4882a593Smuzhiyun 			BT_CONTROL(BT_H_BUSY);	/* clear */
518*4882a593Smuzhiyun 		BT_CONTROL(BT_CLR_WR_PTR);
519*4882a593Smuzhiyun 		write_all_bytes(bt);
520*4882a593Smuzhiyun 		BT_CONTROL(BT_H2B_ATN);	/* can clear too fast to catch */
521*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_WRITE_CONSUME,
522*4882a593Smuzhiyun 				SI_SM_CALL_WITHOUT_DELAY);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	case BT_STATE_WRITE_CONSUME:
525*4882a593Smuzhiyun 		if (status & (BT_B_BUSY | BT_H2B_ATN))
526*4882a593Smuzhiyun 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
527*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_READ_WAIT,
528*4882a593Smuzhiyun 				SI_SM_CALL_WITHOUT_DELAY);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/* Spinning hard can suppress B2H_ATN and force a timeout */
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	case BT_STATE_READ_WAIT:
533*4882a593Smuzhiyun 		if (!(status & BT_B2H_ATN))
534*4882a593Smuzhiyun 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
535*4882a593Smuzhiyun 		BT_CONTROL(BT_H_BUSY);		/* set */
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 		/*
538*4882a593Smuzhiyun 		 * Uncached, ordered writes should just proceed serially but
539*4882a593Smuzhiyun 		 * some BMCs don't clear B2H_ATN with one hit.  Fast-path a
540*4882a593Smuzhiyun 		 * workaround without too much penalty to the general case.
541*4882a593Smuzhiyun 		 */
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 		BT_CONTROL(BT_B2H_ATN);		/* clear it to ACK the BMC */
544*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_CLEAR_B2H,
545*4882a593Smuzhiyun 				SI_SM_CALL_WITHOUT_DELAY);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	case BT_STATE_CLEAR_B2H:
548*4882a593Smuzhiyun 		if (status & BT_B2H_ATN) {
549*4882a593Smuzhiyun 			/* keep hitting it */
550*4882a593Smuzhiyun 			BT_CONTROL(BT_B2H_ATN);
551*4882a593Smuzhiyun 			BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
552*4882a593Smuzhiyun 		}
553*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_READ_BYTES,
554*4882a593Smuzhiyun 				SI_SM_CALL_WITHOUT_DELAY);
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	case BT_STATE_READ_BYTES:
557*4882a593Smuzhiyun 		if (!(status & BT_H_BUSY))
558*4882a593Smuzhiyun 			/* check in case of retry */
559*4882a593Smuzhiyun 			BT_CONTROL(BT_H_BUSY);
560*4882a593Smuzhiyun 		BT_CONTROL(BT_CLR_RD_PTR);	/* start of BMC2HOST buffer */
561*4882a593Smuzhiyun 		i = read_all_bytes(bt);		/* true == packet seq match */
562*4882a593Smuzhiyun 		BT_CONTROL(BT_H_BUSY);		/* NOW clear */
563*4882a593Smuzhiyun 		if (!i) 			/* Not my message */
564*4882a593Smuzhiyun 			BT_STATE_CHANGE(BT_STATE_READ_WAIT,
565*4882a593Smuzhiyun 					SI_SM_CALL_WITHOUT_DELAY);
566*4882a593Smuzhiyun 		bt->state = bt->complete;
567*4882a593Smuzhiyun 		return bt->state == BT_STATE_IDLE ?	/* where to next? */
568*4882a593Smuzhiyun 			SI_SM_TRANSACTION_COMPLETE :	/* normal */
569*4882a593Smuzhiyun 			SI_SM_CALL_WITHOUT_DELAY;	/* Startup magic */
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	case BT_STATE_LONG_BUSY:	/* For example: after FW update */
572*4882a593Smuzhiyun 		if (!(status & BT_B_BUSY)) {
573*4882a593Smuzhiyun 			reset_flags(bt);	/* next state is now IDLE */
574*4882a593Smuzhiyun 			bt_init_data(bt, bt->io);
575*4882a593Smuzhiyun 		}
576*4882a593Smuzhiyun 		return SI_SM_CALL_WITH_DELAY;	/* No repeat printing */
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	case BT_STATE_RESET1:
579*4882a593Smuzhiyun 		reset_flags(bt);
580*4882a593Smuzhiyun 		drain_BMC2HOST(bt);
581*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_RESET2,
582*4882a593Smuzhiyun 				SI_SM_CALL_WITH_DELAY);
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	case BT_STATE_RESET2:		/* Send a soft reset */
585*4882a593Smuzhiyun 		BT_CONTROL(BT_CLR_WR_PTR);
586*4882a593Smuzhiyun 		HOST2BMC(3);		/* number of bytes following */
587*4882a593Smuzhiyun 		HOST2BMC(0x18);		/* NetFn/LUN == Application, LUN 0 */
588*4882a593Smuzhiyun 		HOST2BMC(42);		/* Sequence number */
589*4882a593Smuzhiyun 		HOST2BMC(3);		/* Cmd == Soft reset */
590*4882a593Smuzhiyun 		BT_CONTROL(BT_H2B_ATN);
591*4882a593Smuzhiyun 		bt->timeout = BT_RESET_DELAY * USEC_PER_SEC;
592*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_RESET3,
593*4882a593Smuzhiyun 				SI_SM_CALL_WITH_DELAY);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	case BT_STATE_RESET3:		/* Hold off everything for a bit */
596*4882a593Smuzhiyun 		if (bt->timeout > 0)
597*4882a593Smuzhiyun 			return SI_SM_CALL_WITH_DELAY;
598*4882a593Smuzhiyun 		drain_BMC2HOST(bt);
599*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_RESTART,
600*4882a593Smuzhiyun 				SI_SM_CALL_WITH_DELAY);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	case BT_STATE_RESTART:		/* don't reset retries or seq! */
603*4882a593Smuzhiyun 		bt->read_count = 0;
604*4882a593Smuzhiyun 		bt->nonzero_status = 0;
605*4882a593Smuzhiyun 		bt->timeout = bt->BT_CAP_req2rsp;
606*4882a593Smuzhiyun 		BT_STATE_CHANGE(BT_STATE_XACTION_START,
607*4882a593Smuzhiyun 				SI_SM_CALL_WITH_DELAY);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	default:	/* should never occur */
610*4882a593Smuzhiyun 		return error_recovery(bt,
611*4882a593Smuzhiyun 				      status,
612*4882a593Smuzhiyun 				      IPMI_ERR_UNSPECIFIED);
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 	return SI_SM_CALL_WITH_DELAY;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
bt_detect(struct si_sm_data * bt)617*4882a593Smuzhiyun static int bt_detect(struct si_sm_data *bt)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun 	unsigned char GetBT_CAP[] = { 0x18, 0x36 };
620*4882a593Smuzhiyun 	unsigned char BT_CAP[8];
621*4882a593Smuzhiyun 	enum si_sm_result smi_result;
622*4882a593Smuzhiyun 	int rv;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	/*
625*4882a593Smuzhiyun 	 * It's impossible for the BT status and interrupt registers to be
626*4882a593Smuzhiyun 	 * all 1's, (assuming a properly functioning, self-initialized BMC)
627*4882a593Smuzhiyun 	 * but that's what you get from reading a bogus address, so we
628*4882a593Smuzhiyun 	 * test that first.  The calling routine uses negative logic.
629*4882a593Smuzhiyun 	 */
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
632*4882a593Smuzhiyun 		return 1;
633*4882a593Smuzhiyun 	reset_flags(bt);
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/*
636*4882a593Smuzhiyun 	 * Try getting the BT capabilities here.
637*4882a593Smuzhiyun 	 */
638*4882a593Smuzhiyun 	rv = bt_start_transaction(bt, GetBT_CAP, sizeof(GetBT_CAP));
639*4882a593Smuzhiyun 	if (rv) {
640*4882a593Smuzhiyun 		dev_warn(bt->io->dev,
641*4882a593Smuzhiyun 			 "Can't start capabilities transaction: %d\n", rv);
642*4882a593Smuzhiyun 		goto out_no_bt_cap;
643*4882a593Smuzhiyun 	}
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	smi_result = SI_SM_CALL_WITHOUT_DELAY;
646*4882a593Smuzhiyun 	for (;;) {
647*4882a593Smuzhiyun 		if (smi_result == SI_SM_CALL_WITH_DELAY ||
648*4882a593Smuzhiyun 		    smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
649*4882a593Smuzhiyun 			schedule_timeout_uninterruptible(1);
650*4882a593Smuzhiyun 			smi_result = bt_event(bt, jiffies_to_usecs(1));
651*4882a593Smuzhiyun 		} else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
652*4882a593Smuzhiyun 			smi_result = bt_event(bt, 0);
653*4882a593Smuzhiyun 		} else
654*4882a593Smuzhiyun 			break;
655*4882a593Smuzhiyun 	}
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	rv = bt_get_result(bt, BT_CAP, sizeof(BT_CAP));
658*4882a593Smuzhiyun 	bt_init_data(bt, bt->io);
659*4882a593Smuzhiyun 	if (rv < 8) {
660*4882a593Smuzhiyun 		dev_warn(bt->io->dev, "bt cap response too short: %d\n", rv);
661*4882a593Smuzhiyun 		goto out_no_bt_cap;
662*4882a593Smuzhiyun 	}
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	if (BT_CAP[2]) {
665*4882a593Smuzhiyun 		dev_warn(bt->io->dev, "Error fetching bt cap: %x\n", BT_CAP[2]);
666*4882a593Smuzhiyun out_no_bt_cap:
667*4882a593Smuzhiyun 		dev_warn(bt->io->dev, "using default values\n");
668*4882a593Smuzhiyun 	} else {
669*4882a593Smuzhiyun 		bt->BT_CAP_req2rsp = BT_CAP[6] * USEC_PER_SEC;
670*4882a593Smuzhiyun 		bt->BT_CAP_retries = BT_CAP[7];
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	dev_info(bt->io->dev, "req2rsp=%ld secs retries=%d\n",
674*4882a593Smuzhiyun 		 bt->BT_CAP_req2rsp / USEC_PER_SEC, bt->BT_CAP_retries);
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	return 0;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
bt_cleanup(struct si_sm_data * bt)679*4882a593Smuzhiyun static void bt_cleanup(struct si_sm_data *bt)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
bt_size(void)683*4882a593Smuzhiyun static int bt_size(void)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	return sizeof(struct si_sm_data);
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun const struct si_sm_handlers bt_smi_handlers = {
689*4882a593Smuzhiyun 	.init_data		= bt_init_data,
690*4882a593Smuzhiyun 	.start_transaction	= bt_start_transaction,
691*4882a593Smuzhiyun 	.get_result		= bt_get_result,
692*4882a593Smuzhiyun 	.event			= bt_event,
693*4882a593Smuzhiyun 	.detect			= bt_detect,
694*4882a593Smuzhiyun 	.cleanup		= bt_cleanup,
695*4882a593Smuzhiyun 	.size			= bt_size,
696*4882a593Smuzhiyun };
697