xref: /linux/drivers/char/ipmi/ipmi_bt_sm.c (revision c4edff1c19ef23e15aae64ca03f32c6719822d54)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  ipmi_bt_sm.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part
51da177e4SLinus Torvalds  *  of the driver architecture at http://sourceforge.net/project/openipmi
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Author:	Rocky Craig <first.last@hp.com>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify it
101da177e4SLinus Torvalds  *  under the terms of the GNU General Public License as published by the
111da177e4SLinus Torvalds  *  Free Software Foundation; either version 2 of the License, or (at your
121da177e4SLinus Torvalds  *  option) any later version.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
151da177e4SLinus Torvalds  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
161da177e4SLinus Torvalds  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171da177e4SLinus Torvalds  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181da177e4SLinus Torvalds  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
191da177e4SLinus Torvalds  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
201da177e4SLinus Torvalds  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
211da177e4SLinus Torvalds  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
221da177e4SLinus Torvalds  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
231da177e4SLinus Torvalds  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
261da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
271da177e4SLinus Torvalds  *  675 Mass Ave, Cambridge, MA 02139, USA.  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <linux/kernel.h> /* For printk. */
301da177e4SLinus Torvalds #include <linux/string.h>
31*c4edff1cSCorey Minyard #include <linux/module.h>
32*c4edff1cSCorey Minyard #include <linux/moduleparam.h>
331da177e4SLinus Torvalds #include <linux/ipmi_msgdefs.h>		/* for completion codes */
341da177e4SLinus Torvalds #include "ipmi_si_sm.h"
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds static int bt_debug = 0x00;	/* Production value 0, see following flags */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define	BT_DEBUG_ENABLE	1
391da177e4SLinus Torvalds #define BT_DEBUG_MSG	2
401da177e4SLinus Torvalds #define BT_DEBUG_STATES	4
41*c4edff1cSCorey Minyard module_param(bt_debug, int, 0644);
42*c4edff1cSCorey Minyard MODULE_PARM_DESC(bt_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* Typical "Get BT Capabilities" values are 2-3 retries, 5-10 seconds,
451da177e4SLinus Torvalds    and 64 byte buffers.  However, one HP implementation wants 255 bytes of
461da177e4SLinus Torvalds    buffer (with a documented message of 160 bytes) so go for the max.
471da177e4SLinus Torvalds    Since the Open IPMI architecture is single-message oriented at this
481da177e4SLinus Torvalds    stage, the queue depth of BT is of no concern. */
491da177e4SLinus Torvalds 
50*c4edff1cSCorey Minyard #define BT_NORMAL_TIMEOUT	5000000	/* seconds in microseconds */
511da177e4SLinus Torvalds #define BT_RETRY_LIMIT		2
521da177e4SLinus Torvalds #define BT_RESET_DELAY		6000000	/* 6 seconds after warm reset */
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds enum bt_states {
551da177e4SLinus Torvalds 	BT_STATE_IDLE,
561da177e4SLinus Torvalds 	BT_STATE_XACTION_START,
571da177e4SLinus Torvalds 	BT_STATE_WRITE_BYTES,
581da177e4SLinus Torvalds 	BT_STATE_WRITE_END,
591da177e4SLinus Torvalds 	BT_STATE_WRITE_CONSUME,
601da177e4SLinus Torvalds 	BT_STATE_B2H_WAIT,
611da177e4SLinus Torvalds 	BT_STATE_READ_END,
621da177e4SLinus Torvalds 	BT_STATE_RESET1,		/* These must come last */
631da177e4SLinus Torvalds 	BT_STATE_RESET2,
641da177e4SLinus Torvalds 	BT_STATE_RESET3,
651da177e4SLinus Torvalds 	BT_STATE_RESTART,
661da177e4SLinus Torvalds 	BT_STATE_HOSED
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds struct si_sm_data {
701da177e4SLinus Torvalds 	enum bt_states	state;
711da177e4SLinus Torvalds 	enum bt_states	last_state;	/* assist printing and resets */
721da177e4SLinus Torvalds 	unsigned char	seq;		/* BT sequence number */
731da177e4SLinus Torvalds 	struct si_sm_io	*io;
741da177e4SLinus Torvalds         unsigned char	write_data[IPMI_MAX_MSG_LENGTH];
751da177e4SLinus Torvalds         int		write_count;
761da177e4SLinus Torvalds         unsigned char	read_data[IPMI_MAX_MSG_LENGTH];
771da177e4SLinus Torvalds         int		read_count;
781da177e4SLinus Torvalds         int		truncated;
791da177e4SLinus Torvalds         long		timeout;
801da177e4SLinus Torvalds         unsigned int	error_retries;	/* end of "common" fields */
811da177e4SLinus Torvalds 	int		nonzero_status;	/* hung BMCs stay all 0 */
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #define BT_CLR_WR_PTR	0x01	/* See IPMI 1.5 table 11.6.4 */
851da177e4SLinus Torvalds #define BT_CLR_RD_PTR	0x02
861da177e4SLinus Torvalds #define BT_H2B_ATN	0x04
871da177e4SLinus Torvalds #define BT_B2H_ATN	0x08
881da177e4SLinus Torvalds #define BT_SMS_ATN	0x10
891da177e4SLinus Torvalds #define BT_OEM0		0x20
901da177e4SLinus Torvalds #define BT_H_BUSY	0x40
911da177e4SLinus Torvalds #define BT_B_BUSY	0x80
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* Some bits are toggled on each write: write once to set it, once
941da177e4SLinus Torvalds    more to clear it; writing a zero does nothing.  To absolutely
951da177e4SLinus Torvalds    clear it, check its state and write if set.  This avoids the "get
961da177e4SLinus Torvalds    current then use as mask" scheme to modify one bit.  Note that the
971da177e4SLinus Torvalds    variable "bt" is hardcoded into these macros. */
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #define BT_STATUS	bt->io->inputb(bt->io, 0)
1001da177e4SLinus Torvalds #define BT_CONTROL(x)	bt->io->outputb(bt->io, 0, x)
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #define BMC2HOST	bt->io->inputb(bt->io, 1)
1031da177e4SLinus Torvalds #define HOST2BMC(x)	bt->io->outputb(bt->io, 1, x)
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #define BT_INTMASK_R	bt->io->inputb(bt->io, 2)
1061da177e4SLinus Torvalds #define BT_INTMASK_W(x)	bt->io->outputb(bt->io, 2, x)
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /* Convenience routines for debugging.  These are not multi-open safe!
1091da177e4SLinus Torvalds    Note the macros have hardcoded variables in them. */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static char *state2txt(unsigned char state)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	switch (state) {
1141da177e4SLinus Torvalds 		case BT_STATE_IDLE:		return("IDLE");
1151da177e4SLinus Torvalds 		case BT_STATE_XACTION_START:	return("XACTION");
1161da177e4SLinus Torvalds 		case BT_STATE_WRITE_BYTES:	return("WR_BYTES");
1171da177e4SLinus Torvalds 		case BT_STATE_WRITE_END:	return("WR_END");
1181da177e4SLinus Torvalds 		case BT_STATE_WRITE_CONSUME:	return("WR_CONSUME");
1191da177e4SLinus Torvalds 		case BT_STATE_B2H_WAIT:		return("B2H_WAIT");
1201da177e4SLinus Torvalds 		case BT_STATE_READ_END:		return("RD_END");
1211da177e4SLinus Torvalds 		case BT_STATE_RESET1:		return("RESET1");
1221da177e4SLinus Torvalds 		case BT_STATE_RESET2:		return("RESET2");
1231da177e4SLinus Torvalds 		case BT_STATE_RESET3:		return("RESET3");
1241da177e4SLinus Torvalds 		case BT_STATE_RESTART:		return("RESTART");
1251da177e4SLinus Torvalds 		case BT_STATE_HOSED:		return("HOSED");
1261da177e4SLinus Torvalds 	}
1271da177e4SLinus Torvalds 	return("BAD STATE");
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds #define STATE2TXT state2txt(bt->state)
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds static char *status2txt(unsigned char status, char *buf)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	strcpy(buf, "[ ");
1341da177e4SLinus Torvalds 	if (status & BT_B_BUSY) strcat(buf, "B_BUSY ");
1351da177e4SLinus Torvalds 	if (status & BT_H_BUSY) strcat(buf, "H_BUSY ");
1361da177e4SLinus Torvalds 	if (status & BT_OEM0) strcat(buf, "OEM0 ");
1371da177e4SLinus Torvalds 	if (status & BT_SMS_ATN) strcat(buf, "SMS ");
1381da177e4SLinus Torvalds 	if (status & BT_B2H_ATN) strcat(buf, "B2H ");
1391da177e4SLinus Torvalds 	if (status & BT_H2B_ATN) strcat(buf, "H2B ");
1401da177e4SLinus Torvalds 	strcat(buf, "]");
1411da177e4SLinus Torvalds 	return buf;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds #define STATUS2TXT(buf) status2txt(status, buf)
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /* This will be called from within this module on a hosed condition */
1461da177e4SLinus Torvalds #define FIRST_SEQ	0
1471da177e4SLinus Torvalds static unsigned int bt_init_data(struct si_sm_data *bt, struct si_sm_io *io)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	bt->state = BT_STATE_IDLE;
1501da177e4SLinus Torvalds 	bt->last_state = BT_STATE_IDLE;
1511da177e4SLinus Torvalds 	bt->seq = FIRST_SEQ;
1521da177e4SLinus Torvalds 	bt->io = io;
1531da177e4SLinus Torvalds 	bt->write_count = 0;
1541da177e4SLinus Torvalds 	bt->read_count = 0;
1551da177e4SLinus Torvalds 	bt->error_retries = 0;
1561da177e4SLinus Torvalds 	bt->nonzero_status = 0;
1571da177e4SLinus Torvalds 	bt->truncated = 0;
1581da177e4SLinus Torvalds 	bt->timeout = BT_NORMAL_TIMEOUT;
1591da177e4SLinus Torvalds 	return 3; /* We claim 3 bytes of space; ought to check SPMI table */
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static int bt_start_transaction(struct si_sm_data *bt,
1631da177e4SLinus Torvalds 				unsigned char *data,
1641da177e4SLinus Torvalds 				unsigned int size)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	unsigned int i;
1671da177e4SLinus Torvalds 
168e8b33617SCorey Minyard 	if ((size < 2) || (size > IPMI_MAX_MSG_LENGTH))
169e8b33617SCorey Minyard 	       return -1;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	if ((bt->state != BT_STATE_IDLE) && (bt->state != BT_STATE_HOSED))
1721da177e4SLinus Torvalds 		return -2;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG) {
1751da177e4SLinus Torvalds     		printk(KERN_WARNING "+++++++++++++++++++++++++++++++++++++\n");
1761da177e4SLinus Torvalds 		printk(KERN_WARNING "BT: write seq=0x%02X:", bt->seq);
177e8b33617SCorey Minyard 		for (i = 0; i < size; i ++)
178e8b33617SCorey Minyard 		       printk (" %02x", data[i]);
1791da177e4SLinus Torvalds 		printk("\n");
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 	bt->write_data[0] = size + 1;	/* all data plus seq byte */
1821da177e4SLinus Torvalds 	bt->write_data[1] = *data;	/* NetFn/LUN */
1831da177e4SLinus Torvalds 	bt->write_data[2] = bt->seq;
1841da177e4SLinus Torvalds 	memcpy(bt->write_data + 3, data + 1, size - 1);
1851da177e4SLinus Torvalds 	bt->write_count = size + 2;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	bt->error_retries = 0;
1881da177e4SLinus Torvalds 	bt->nonzero_status = 0;
1891da177e4SLinus Torvalds 	bt->read_count = 0;
1901da177e4SLinus Torvalds 	bt->truncated = 0;
1911da177e4SLinus Torvalds 	bt->state = BT_STATE_XACTION_START;
1921da177e4SLinus Torvalds 	bt->last_state = BT_STATE_IDLE;
1931da177e4SLinus Torvalds 	bt->timeout = BT_NORMAL_TIMEOUT;
1941da177e4SLinus Torvalds 	return 0;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /* After the upper state machine has been told SI_SM_TRANSACTION_COMPLETE
1981da177e4SLinus Torvalds    it calls this.  Strip out the length and seq bytes. */
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static int bt_get_result(struct si_sm_data *bt,
2011da177e4SLinus Torvalds 			   unsigned char *data,
2021da177e4SLinus Torvalds 			   unsigned int length)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	int i, msg_len;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	msg_len = bt->read_count - 2;		/* account for length & seq */
2071da177e4SLinus Torvalds 	/* Always NetFn, Cmd, cCode */
2081da177e4SLinus Torvalds 	if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
209*c4edff1cSCorey Minyard 		printk(KERN_DEBUG "BT results: bad msg_len = %d\n", msg_len);
2101da177e4SLinus Torvalds 		data[0] = bt->write_data[1] | 0x4;	/* Kludge a response */
2111da177e4SLinus Torvalds 		data[1] = bt->write_data[3];
2121da177e4SLinus Torvalds 		data[2] = IPMI_ERR_UNSPECIFIED;
2131da177e4SLinus Torvalds 		msg_len = 3;
2141da177e4SLinus Torvalds 	} else {
2151da177e4SLinus Torvalds 		data[0] = bt->read_data[1];
2161da177e4SLinus Torvalds 		data[1] = bt->read_data[3];
217e8b33617SCorey Minyard 		if (length < msg_len)
218e8b33617SCorey Minyard 		       bt->truncated = 1;
2191da177e4SLinus Torvalds 		if (bt->truncated) {	/* can be set in read_all_bytes() */
2201da177e4SLinus Torvalds 			data[2] = IPMI_ERR_MSG_TRUNCATED;
2211da177e4SLinus Torvalds 			msg_len = 3;
222e8b33617SCorey Minyard 		} else
223e8b33617SCorey Minyard 		       memcpy(data + 2, bt->read_data + 4, msg_len - 2);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 		if (bt_debug & BT_DEBUG_MSG) {
2261da177e4SLinus Torvalds 			printk (KERN_WARNING "BT: res (raw)");
227e8b33617SCorey Minyard 			for (i = 0; i < msg_len; i++)
228e8b33617SCorey Minyard 			       printk(" %02x", data[i]);
2291da177e4SLinus Torvalds 			printk ("\n");
2301da177e4SLinus Torvalds 		}
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 	bt->read_count = 0;	/* paranoia */
2331da177e4SLinus Torvalds 	return msg_len;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /* This bit's functionality is optional */
2371da177e4SLinus Torvalds #define BT_BMC_HWRST	0x80
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static void reset_flags(struct si_sm_data *bt)
2401da177e4SLinus Torvalds {
241e8b33617SCorey Minyard 	if (BT_STATUS & BT_H_BUSY)
242e8b33617SCorey Minyard 	       BT_CONTROL(BT_H_BUSY);
243e8b33617SCorey Minyard 	if (BT_STATUS & BT_B_BUSY)
244e8b33617SCorey Minyard 	       BT_CONTROL(BT_B_BUSY);
2451da177e4SLinus Torvalds 	BT_CONTROL(BT_CLR_WR_PTR);
2461da177e4SLinus Torvalds 	BT_CONTROL(BT_SMS_ATN);
247*c4edff1cSCorey Minyard 
2481da177e4SLinus Torvalds 	if (BT_STATUS & BT_B2H_ATN) {
2491da177e4SLinus Torvalds 		int i;
2501da177e4SLinus Torvalds 		BT_CONTROL(BT_H_BUSY);
2511da177e4SLinus Torvalds 		BT_CONTROL(BT_B2H_ATN);
2521da177e4SLinus Torvalds 		BT_CONTROL(BT_CLR_RD_PTR);
253e8b33617SCorey Minyard 		for (i = 0; i < IPMI_MAX_MSG_LENGTH + 2; i++)
254e8b33617SCorey Minyard 		       BMC2HOST;
2551da177e4SLinus Torvalds 		BT_CONTROL(BT_H_BUSY);
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds static inline void write_all_bytes(struct si_sm_data *bt)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	int i;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG) {
2641da177e4SLinus Torvalds     		printk(KERN_WARNING "BT: write %d bytes seq=0x%02X",
2651da177e4SLinus Torvalds 			bt->write_count, bt->seq);
2661da177e4SLinus Torvalds 		for (i = 0; i < bt->write_count; i++)
2671da177e4SLinus Torvalds 			printk (" %02x", bt->write_data[i]);
2681da177e4SLinus Torvalds 		printk ("\n");
2691da177e4SLinus Torvalds 	}
270e8b33617SCorey Minyard 	for (i = 0; i < bt->write_count; i++)
271e8b33617SCorey Minyard 	       HOST2BMC(bt->write_data[i]);
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds static inline int read_all_bytes(struct si_sm_data *bt)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	unsigned char i;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	bt->read_data[0] = BMC2HOST;
2791da177e4SLinus Torvalds 	bt->read_count = bt->read_data[0];
2801da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG)
2811da177e4SLinus Torvalds     		printk(KERN_WARNING "BT: read %d bytes:", bt->read_count);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* minimum: length, NetFn, Seq, Cmd, cCode == 5 total, or 4 more
2841da177e4SLinus Torvalds 	   following the length byte. */
2851da177e4SLinus Torvalds 	if (bt->read_count < 4 || bt->read_count >= IPMI_MAX_MSG_LENGTH) {
2861da177e4SLinus Torvalds 		if (bt_debug & BT_DEBUG_MSG)
2871da177e4SLinus Torvalds 			printk("bad length %d\n", bt->read_count);
2881da177e4SLinus Torvalds 		bt->truncated = 1;
2891da177e4SLinus Torvalds 		return 1;	/* let next XACTION START clean it up */
2901da177e4SLinus Torvalds 	}
291e8b33617SCorey Minyard 	for (i = 1; i <= bt->read_count; i++)
292e8b33617SCorey Minyard 	       bt->read_data[i] = BMC2HOST;
2931da177e4SLinus Torvalds 	bt->read_count++;	/* account for the length byte */
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (bt_debug & BT_DEBUG_MSG) {
2961da177e4SLinus Torvalds 	    	for (i = 0; i < bt->read_count; i++)
2971da177e4SLinus Torvalds 			printk (" %02x", bt->read_data[i]);
2981da177e4SLinus Torvalds 	    	printk ("\n");
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 	if (bt->seq != bt->write_data[2])	/* idiot check */
301*c4edff1cSCorey Minyard 		printk(KERN_DEBUG "BT: internal error: sequence mismatch\n");
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/* per the spec, the (NetFn, Seq, Cmd) tuples should match */
3041da177e4SLinus Torvalds 	if ((bt->read_data[3] == bt->write_data[3]) &&		/* Cmd */
3051da177e4SLinus Torvalds         	(bt->read_data[2] == bt->write_data[2]) &&	/* Sequence */
3061da177e4SLinus Torvalds         	((bt->read_data[1] & 0xF8) == (bt->write_data[1] & 0xF8)))
3071da177e4SLinus Torvalds 			return 1;
3081da177e4SLinus Torvalds 
309e8b33617SCorey Minyard 	if (bt_debug & BT_DEBUG_MSG)
310e8b33617SCorey Minyard 	       printk(KERN_WARNING "BT: bad packet: "
3111da177e4SLinus Torvalds 		"want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
3121da177e4SLinus Torvalds 		bt->write_data[1], bt->write_data[2], bt->write_data[3],
3131da177e4SLinus Torvalds 		bt->read_data[1],  bt->read_data[2],  bt->read_data[3]);
3141da177e4SLinus Torvalds 	return 0;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds /* Modifies bt->state appropriately, need to get into the bt_event() switch */
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static void error_recovery(struct si_sm_data *bt, char *reason)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	unsigned char status;
3221da177e4SLinus Torvalds 	char buf[40]; /* For getting status */
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	bt->timeout = BT_NORMAL_TIMEOUT; /* various places want to retry */
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	status = BT_STATUS;
327*c4edff1cSCorey Minyard 	printk(KERN_DEBUG "BT: %s in %s %s\n", reason, STATE2TXT,
3281da177e4SLinus Torvalds 	       STATUS2TXT(buf));
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	(bt->error_retries)++;
3311da177e4SLinus Torvalds 	if (bt->error_retries > BT_RETRY_LIMIT) {
332*c4edff1cSCorey Minyard 		printk(KERN_DEBUG "retry limit (%d) exceeded\n", BT_RETRY_LIMIT);
3331da177e4SLinus Torvalds 		bt->state = BT_STATE_HOSED;
3341da177e4SLinus Torvalds 		if (!bt->nonzero_status)
3351da177e4SLinus Torvalds 			printk(KERN_ERR "IPMI: BT stuck, try power cycle\n");
3361da177e4SLinus Torvalds 		else if (bt->seq == FIRST_SEQ + BT_RETRY_LIMIT) {
3371da177e4SLinus Torvalds 			/* most likely during insmod */
338*c4edff1cSCorey Minyard 			printk(KERN_DEBUG "IPMI: BT reset (takes 5 secs)\n");
3391da177e4SLinus Torvalds         		bt->state = BT_STATE_RESET1;
3401da177e4SLinus Torvalds 		}
3411da177e4SLinus Torvalds 	return;
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	/* Sometimes the BMC queues get in an "off-by-one" state...*/
3451da177e4SLinus Torvalds 	if ((bt->state == BT_STATE_B2H_WAIT) && (status & BT_B2H_ATN)) {
346*c4edff1cSCorey Minyard     		printk(KERN_DEBUG "retry B2H_WAIT\n");
3471da177e4SLinus Torvalds 		return;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
350*c4edff1cSCorey Minyard 	printk(KERN_DEBUG "restart command\n");
3511da177e4SLinus Torvalds 	bt->state = BT_STATE_RESTART;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /* Check the status and (possibly) advance the BT state machine.  The
3551da177e4SLinus Torvalds    default return is SI_SM_CALL_WITH_DELAY. */
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds static enum si_sm_result bt_event(struct si_sm_data *bt, long time)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	unsigned char status;
3601da177e4SLinus Torvalds 	char buf[40]; /* For getting status */
3611da177e4SLinus Torvalds 	int i;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	status = BT_STATUS;
3641da177e4SLinus Torvalds 	bt->nonzero_status |= status;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if ((bt_debug & BT_DEBUG_STATES) && (bt->state != bt->last_state))
3671da177e4SLinus Torvalds 		printk(KERN_WARNING "BT: %s %s TO=%ld - %ld \n",
3681da177e4SLinus Torvalds 			STATE2TXT,
3691da177e4SLinus Torvalds 			STATUS2TXT(buf),
3701da177e4SLinus Torvalds 			bt->timeout,
3711da177e4SLinus Torvalds 			time);
3721da177e4SLinus Torvalds 	bt->last_state = bt->state;
3731da177e4SLinus Torvalds 
374e8b33617SCorey Minyard 	if (bt->state == BT_STATE_HOSED)
375e8b33617SCorey Minyard 	       return SI_SM_HOSED;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	if (bt->state != BT_STATE_IDLE) {	/* do timeout test */
3781da177e4SLinus Torvalds 		bt->timeout -= time;
3791da177e4SLinus Torvalds 		if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1)) {
3801da177e4SLinus Torvalds 			error_recovery(bt, "timed out");
3811da177e4SLinus Torvalds 			return SI_SM_CALL_WITHOUT_DELAY;
3821da177e4SLinus Torvalds 		}
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	switch (bt->state) {
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds     	case BT_STATE_IDLE:	/* check for asynchronous messages */
3881da177e4SLinus Torvalds 		if (status & BT_SMS_ATN) {
3891da177e4SLinus Torvalds 			BT_CONTROL(BT_SMS_ATN);	/* clear it */
3901da177e4SLinus Torvalds 			return SI_SM_ATTN;
3911da177e4SLinus Torvalds 		}
3921da177e4SLinus Torvalds 		return SI_SM_IDLE;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	case BT_STATE_XACTION_START:
3951da177e4SLinus Torvalds 		if (status & BT_H_BUSY) {
3961da177e4SLinus Torvalds 			BT_CONTROL(BT_H_BUSY);
3971da177e4SLinus Torvalds 			break;
3981da177e4SLinus Torvalds 		}
399e8b33617SCorey Minyard     		if (status & BT_B2H_ATN)
400e8b33617SCorey Minyard 		       break;
4011da177e4SLinus Torvalds 		bt->state = BT_STATE_WRITE_BYTES;
4021da177e4SLinus Torvalds 		return SI_SM_CALL_WITHOUT_DELAY;	/* for logging */
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	case BT_STATE_WRITE_BYTES:
405e8b33617SCorey Minyard 		if (status & (BT_B_BUSY | BT_H2B_ATN))
406e8b33617SCorey Minyard 		       break;
4071da177e4SLinus Torvalds 		BT_CONTROL(BT_CLR_WR_PTR);
4081da177e4SLinus Torvalds 		write_all_bytes(bt);
4091da177e4SLinus Torvalds 		BT_CONTROL(BT_H2B_ATN);	/* clears too fast to catch? */
4101da177e4SLinus Torvalds 		bt->state = BT_STATE_WRITE_CONSUME;
4111da177e4SLinus Torvalds 		return SI_SM_CALL_WITHOUT_DELAY; /* it MIGHT sail through */
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	case BT_STATE_WRITE_CONSUME: /* BMCs usually blow right thru here */
414e8b33617SCorey Minyard         	if (status & (BT_H2B_ATN | BT_B_BUSY))
415e8b33617SCorey Minyard 		       break;
4161da177e4SLinus Torvalds 		bt->state = BT_STATE_B2H_WAIT;
4171da177e4SLinus Torvalds 		/* fall through with status */
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* Stay in BT_STATE_B2H_WAIT until a packet matches.  However, spinning
4201da177e4SLinus Torvalds 	   hard here, constantly reading status, seems to hold off the
4211da177e4SLinus Torvalds 	   generation of B2H_ATN so ALWAYS return CALL_WITH_DELAY. */
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	case BT_STATE_B2H_WAIT:
424e8b33617SCorey Minyard     		if (!(status & BT_B2H_ATN))
425e8b33617SCorey Minyard 		       break;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 		/* Assume ordered, uncached writes: no need to wait */
428e8b33617SCorey Minyard 		if (!(status & BT_H_BUSY))
429e8b33617SCorey Minyard 		       BT_CONTROL(BT_H_BUSY); /* set */
4301da177e4SLinus Torvalds 		BT_CONTROL(BT_B2H_ATN);		/* clear it, ACK to the BMC */
4311da177e4SLinus Torvalds 		BT_CONTROL(BT_CLR_RD_PTR);	/* reset the queue */
4321da177e4SLinus Torvalds 		i = read_all_bytes(bt);
4331da177e4SLinus Torvalds 		BT_CONTROL(BT_H_BUSY);		/* clear */
434e8b33617SCorey Minyard 		if (!i)				/* Try this state again */
435e8b33617SCorey Minyard 		       break;
4361da177e4SLinus Torvalds 		bt->state = BT_STATE_READ_END;
4371da177e4SLinus Torvalds 		return SI_SM_CALL_WITHOUT_DELAY;	/* for logging */
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds     	case BT_STATE_READ_END:
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		/* I could wait on BT_H_BUSY to go clear for a truly clean
4421da177e4SLinus Torvalds 		   exit.  However, this is already done in XACTION_START
4431da177e4SLinus Torvalds 		   and the (possible) extra loop/status/possible wait affects
4441da177e4SLinus Torvalds 		   performance.  So, as long as it works, just ignore H_BUSY */
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds #ifdef MAKE_THIS_TRUE_IF_NECESSARY
4471da177e4SLinus Torvalds 
448e8b33617SCorey Minyard 		if (status & BT_H_BUSY)
449e8b33617SCorey Minyard 		       break;
4501da177e4SLinus Torvalds #endif
4511da177e4SLinus Torvalds 		bt->seq++;
4521da177e4SLinus Torvalds 		bt->state = BT_STATE_IDLE;
4531da177e4SLinus Torvalds 		return SI_SM_TRANSACTION_COMPLETE;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	case BT_STATE_RESET1:
4561da177e4SLinus Torvalds     		reset_flags(bt);
4571da177e4SLinus Torvalds     		bt->timeout = BT_RESET_DELAY;
4581da177e4SLinus Torvalds 		bt->state = BT_STATE_RESET2;
4591da177e4SLinus Torvalds 		break;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	case BT_STATE_RESET2:		/* Send a soft reset */
4621da177e4SLinus Torvalds 		BT_CONTROL(BT_CLR_WR_PTR);
4631da177e4SLinus Torvalds 		HOST2BMC(3);		/* number of bytes following */
4641da177e4SLinus Torvalds 		HOST2BMC(0x18);		/* NetFn/LUN == Application, LUN 0 */
4651da177e4SLinus Torvalds 		HOST2BMC(42);		/* Sequence number */
4661da177e4SLinus Torvalds 		HOST2BMC(3);		/* Cmd == Soft reset */
4671da177e4SLinus Torvalds 		BT_CONTROL(BT_H2B_ATN);
4681da177e4SLinus Torvalds 		bt->state = BT_STATE_RESET3;
4691da177e4SLinus Torvalds 		break;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	case BT_STATE_RESET3:
472e8b33617SCorey Minyard 		if (bt->timeout > 0)
473e8b33617SCorey Minyard 		       return SI_SM_CALL_WITH_DELAY;
4741da177e4SLinus Torvalds 		bt->state = BT_STATE_RESTART;	/* printk in debug modes */
4751da177e4SLinus Torvalds 		break;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	case BT_STATE_RESTART:		/* don't reset retries! */
4781da177e4SLinus Torvalds 		bt->write_data[2] = ++bt->seq;
4791da177e4SLinus Torvalds 		bt->read_count = 0;
4801da177e4SLinus Torvalds 		bt->nonzero_status = 0;
4811da177e4SLinus Torvalds 		bt->timeout = BT_NORMAL_TIMEOUT;
4821da177e4SLinus Torvalds 		bt->state = BT_STATE_XACTION_START;
4831da177e4SLinus Torvalds 		break;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	default:	/* HOSED is supposed to be caught much earlier */
4861da177e4SLinus Torvalds 		error_recovery(bt, "internal logic error");
4871da177e4SLinus Torvalds 		break;
4881da177e4SLinus Torvalds   	}
4891da177e4SLinus Torvalds   	return SI_SM_CALL_WITH_DELAY;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds static int bt_detect(struct si_sm_data *bt)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	/* It's impossible for the BT status and interrupt registers to be
4951da177e4SLinus Torvalds 	   all 1's, (assuming a properly functioning, self-initialized BMC)
4961da177e4SLinus Torvalds 	   but that's what you get from reading a bogus address, so we
4971da177e4SLinus Torvalds 	   test that first.  The calling routine uses negative logic. */
4981da177e4SLinus Torvalds 
499e8b33617SCorey Minyard 	if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
500e8b33617SCorey Minyard 	       return 1;
5011da177e4SLinus Torvalds 	reset_flags(bt);
5021da177e4SLinus Torvalds 	return 0;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds static void bt_cleanup(struct si_sm_data *bt)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds static int bt_size(void)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	return sizeof(struct si_sm_data);
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds struct si_sm_handlers bt_smi_handlers =
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	.init_data         = bt_init_data,
5171da177e4SLinus Torvalds 	.start_transaction = bt_start_transaction,
5181da177e4SLinus Torvalds 	.get_result        = bt_get_result,
5191da177e4SLinus Torvalds 	.event             = bt_event,
5201da177e4SLinus Torvalds 	.detect            = bt_detect,
5211da177e4SLinus Torvalds 	.cleanup           = bt_cleanup,
5221da177e4SLinus Torvalds 	.size              = bt_size,
5231da177e4SLinus Torvalds };
524