ipmi_bt_sm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * ipmi_bt_sm.c
  3. *
  4. * The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part
  5. * of the driver architecture at http://sourceforge.net/project/openipmi
  6. *
  7. * Author: Rocky Craig <first.last@hp.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  19. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  20. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  23. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 675 Mass Ave, Cambridge, MA 02139, USA. */
  28. #include <linux/kernel.h> /* For printk. */
  29. #include <linux/string.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/ipmi_msgdefs.h> /* for completion codes */
  33. #include "ipmi_si_sm.h"
  34. static int bt_debug = 0x00; /* Production value 0, see following flags */
  35. #define BT_DEBUG_ENABLE 1
  36. #define BT_DEBUG_MSG 2
  37. #define BT_DEBUG_STATES 4
  38. module_param(bt_debug, int, 0644);
  39. MODULE_PARM_DESC(bt_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
  40. /* Typical "Get BT Capabilities" values are 2-3 retries, 5-10 seconds,
  41. and 64 byte buffers. However, one HP implementation wants 255 bytes of
  42. buffer (with a documented message of 160 bytes) so go for the max.
  43. Since the Open IPMI architecture is single-message oriented at this
  44. stage, the queue depth of BT is of no concern. */
  45. #define BT_NORMAL_TIMEOUT 5000000 /* seconds in microseconds */
  46. #define BT_RETRY_LIMIT 2
  47. #define BT_RESET_DELAY 6000000 /* 6 seconds after warm reset */
  48. enum bt_states {
  49. BT_STATE_IDLE,
  50. BT_STATE_XACTION_START,
  51. BT_STATE_WRITE_BYTES,
  52. BT_STATE_WRITE_END,
  53. BT_STATE_WRITE_CONSUME,
  54. BT_STATE_B2H_WAIT,
  55. BT_STATE_READ_END,
  56. BT_STATE_RESET1, /* These must come last */
  57. BT_STATE_RESET2,
  58. BT_STATE_RESET3,
  59. BT_STATE_RESTART,
  60. BT_STATE_HOSED
  61. };
  62. struct si_sm_data {
  63. enum bt_states state;
  64. enum bt_states last_state; /* assist printing and resets */
  65. unsigned char seq; /* BT sequence number */
  66. struct si_sm_io *io;
  67. unsigned char write_data[IPMI_MAX_MSG_LENGTH];
  68. int write_count;
  69. unsigned char read_data[IPMI_MAX_MSG_LENGTH];
  70. int read_count;
  71. int truncated;
  72. long timeout;
  73. unsigned int error_retries; /* end of "common" fields */
  74. int nonzero_status; /* hung BMCs stay all 0 */
  75. };
  76. #define BT_CLR_WR_PTR 0x01 /* See IPMI 1.5 table 11.6.4 */
  77. #define BT_CLR_RD_PTR 0x02
  78. #define BT_H2B_ATN 0x04
  79. #define BT_B2H_ATN 0x08
  80. #define BT_SMS_ATN 0x10
  81. #define BT_OEM0 0x20
  82. #define BT_H_BUSY 0x40
  83. #define BT_B_BUSY 0x80
  84. /* Some bits are toggled on each write: write once to set it, once
  85. more to clear it; writing a zero does nothing. To absolutely
  86. clear it, check its state and write if set. This avoids the "get
  87. current then use as mask" scheme to modify one bit. Note that the
  88. variable "bt" is hardcoded into these macros. */
  89. #define BT_STATUS bt->io->inputb(bt->io, 0)
  90. #define BT_CONTROL(x) bt->io->outputb(bt->io, 0, x)
  91. #define BMC2HOST bt->io->inputb(bt->io, 1)
  92. #define HOST2BMC(x) bt->io->outputb(bt->io, 1, x)
  93. #define BT_INTMASK_R bt->io->inputb(bt->io, 2)
  94. #define BT_INTMASK_W(x) bt->io->outputb(bt->io, 2, x)
  95. /* Convenience routines for debugging. These are not multi-open safe!
  96. Note the macros have hardcoded variables in them. */
  97. static char *state2txt(unsigned char state)
  98. {
  99. switch (state) {
  100. case BT_STATE_IDLE: return("IDLE");
  101. case BT_STATE_XACTION_START: return("XACTION");
  102. case BT_STATE_WRITE_BYTES: return("WR_BYTES");
  103. case BT_STATE_WRITE_END: return("WR_END");
  104. case BT_STATE_WRITE_CONSUME: return("WR_CONSUME");
  105. case BT_STATE_B2H_WAIT: return("B2H_WAIT");
  106. case BT_STATE_READ_END: return("RD_END");
  107. case BT_STATE_RESET1: return("RESET1");
  108. case BT_STATE_RESET2: return("RESET2");
  109. case BT_STATE_RESET3: return("RESET3");
  110. case BT_STATE_RESTART: return("RESTART");
  111. case BT_STATE_HOSED: return("HOSED");
  112. }
  113. return("BAD STATE");
  114. }
  115. #define STATE2TXT state2txt(bt->state)
  116. static char *status2txt(unsigned char status, char *buf)
  117. {
  118. strcpy(buf, "[ ");
  119. if (status & BT_B_BUSY) strcat(buf, "B_BUSY ");
  120. if (status & BT_H_BUSY) strcat(buf, "H_BUSY ");
  121. if (status & BT_OEM0) strcat(buf, "OEM0 ");
  122. if (status & BT_SMS_ATN) strcat(buf, "SMS ");
  123. if (status & BT_B2H_ATN) strcat(buf, "B2H ");
  124. if (status & BT_H2B_ATN) strcat(buf, "H2B ");
  125. strcat(buf, "]");
  126. return buf;
  127. }
  128. #define STATUS2TXT(buf) status2txt(status, buf)
  129. /* This will be called from within this module on a hosed condition */
  130. #define FIRST_SEQ 0
  131. static unsigned int bt_init_data(struct si_sm_data *bt, struct si_sm_io *io)
  132. {
  133. bt->state = BT_STATE_IDLE;
  134. bt->last_state = BT_STATE_IDLE;
  135. bt->seq = FIRST_SEQ;
  136. bt->io = io;
  137. bt->write_count = 0;
  138. bt->read_count = 0;
  139. bt->error_retries = 0;
  140. bt->nonzero_status = 0;
  141. bt->truncated = 0;
  142. bt->timeout = BT_NORMAL_TIMEOUT;
  143. return 3; /* We claim 3 bytes of space; ought to check SPMI table */
  144. }
  145. static int bt_start_transaction(struct si_sm_data *bt,
  146. unsigned char *data,
  147. unsigned int size)
  148. {
  149. unsigned int i;
  150. if ((size < 2) || (size > IPMI_MAX_MSG_LENGTH))
  151. return -1;
  152. if ((bt->state != BT_STATE_IDLE) && (bt->state != BT_STATE_HOSED))
  153. return -2;
  154. if (bt_debug & BT_DEBUG_MSG) {
  155. printk(KERN_WARNING "+++++++++++++++++++++++++++++++++++++\n");
  156. printk(KERN_WARNING "BT: write seq=0x%02X:", bt->seq);
  157. for (i = 0; i < size; i ++)
  158. printk (" %02x", data[i]);
  159. printk("\n");
  160. }
  161. bt->write_data[0] = size + 1; /* all data plus seq byte */
  162. bt->write_data[1] = *data; /* NetFn/LUN */
  163. bt->write_data[2] = bt->seq;
  164. memcpy(bt->write_data + 3, data + 1, size - 1);
  165. bt->write_count = size + 2;
  166. bt->error_retries = 0;
  167. bt->nonzero_status = 0;
  168. bt->read_count = 0;
  169. bt->truncated = 0;
  170. bt->state = BT_STATE_XACTION_START;
  171. bt->last_state = BT_STATE_IDLE;
  172. bt->timeout = BT_NORMAL_TIMEOUT;
  173. return 0;
  174. }
  175. /* After the upper state machine has been told SI_SM_TRANSACTION_COMPLETE
  176. it calls this. Strip out the length and seq bytes. */
  177. static int bt_get_result(struct si_sm_data *bt,
  178. unsigned char *data,
  179. unsigned int length)
  180. {
  181. int i, msg_len;
  182. msg_len = bt->read_count - 2; /* account for length & seq */
  183. /* Always NetFn, Cmd, cCode */
  184. if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
  185. printk(KERN_DEBUG "BT results: bad msg_len = %d\n", msg_len);
  186. data[0] = bt->write_data[1] | 0x4; /* Kludge a response */
  187. data[1] = bt->write_data[3];
  188. data[2] = IPMI_ERR_UNSPECIFIED;
  189. msg_len = 3;
  190. } else {
  191. data[0] = bt->read_data[1];
  192. data[1] = bt->read_data[3];
  193. if (length < msg_len)
  194. bt->truncated = 1;
  195. if (bt->truncated) { /* can be set in read_all_bytes() */
  196. data[2] = IPMI_ERR_MSG_TRUNCATED;
  197. msg_len = 3;
  198. } else
  199. memcpy(data + 2, bt->read_data + 4, msg_len - 2);
  200. if (bt_debug & BT_DEBUG_MSG) {
  201. printk (KERN_WARNING "BT: res (raw)");
  202. for (i = 0; i < msg_len; i++)
  203. printk(" %02x", data[i]);
  204. printk ("\n");
  205. }
  206. }
  207. bt->read_count = 0; /* paranoia */
  208. return msg_len;
  209. }
  210. /* This bit's functionality is optional */
  211. #define BT_BMC_HWRST 0x80
  212. static void reset_flags(struct si_sm_data *bt)
  213. {
  214. if (BT_STATUS & BT_H_BUSY)
  215. BT_CONTROL(BT_H_BUSY);
  216. if (BT_STATUS & BT_B_BUSY)
  217. BT_CONTROL(BT_B_BUSY);
  218. BT_CONTROL(BT_CLR_WR_PTR);
  219. BT_CONTROL(BT_SMS_ATN);
  220. if (BT_STATUS & BT_B2H_ATN) {
  221. int i;
  222. BT_CONTROL(BT_H_BUSY);
  223. BT_CONTROL(BT_B2H_ATN);
  224. BT_CONTROL(BT_CLR_RD_PTR);
  225. for (i = 0; i < IPMI_MAX_MSG_LENGTH + 2; i++)
  226. BMC2HOST;
  227. BT_CONTROL(BT_H_BUSY);
  228. }
  229. }
  230. static inline void write_all_bytes(struct si_sm_data *bt)
  231. {
  232. int i;
  233. if (bt_debug & BT_DEBUG_MSG) {
  234. printk(KERN_WARNING "BT: write %d bytes seq=0x%02X",
  235. bt->write_count, bt->seq);
  236. for (i = 0; i < bt->write_count; i++)
  237. printk (" %02x", bt->write_data[i]);
  238. printk ("\n");
  239. }
  240. for (i = 0; i < bt->write_count; i++)
  241. HOST2BMC(bt->write_data[i]);
  242. }
  243. static inline int read_all_bytes(struct si_sm_data *bt)
  244. {
  245. unsigned char i;
  246. bt->read_data[0] = BMC2HOST;
  247. bt->read_count = bt->read_data[0];
  248. if (bt_debug & BT_DEBUG_MSG)
  249. printk(KERN_WARNING "BT: read %d bytes:", bt->read_count);
  250. /* minimum: length, NetFn, Seq, Cmd, cCode == 5 total, or 4 more
  251. following the length byte. */
  252. if (bt->read_count < 4 || bt->read_count >= IPMI_MAX_MSG_LENGTH) {
  253. if (bt_debug & BT_DEBUG_MSG)
  254. printk("bad length %d\n", bt->read_count);
  255. bt->truncated = 1;
  256. return 1; /* let next XACTION START clean it up */
  257. }
  258. for (i = 1; i <= bt->read_count; i++)
  259. bt->read_data[i] = BMC2HOST;
  260. bt->read_count++; /* account for the length byte */
  261. if (bt_debug & BT_DEBUG_MSG) {
  262. for (i = 0; i < bt->read_count; i++)
  263. printk (" %02x", bt->read_data[i]);
  264. printk ("\n");
  265. }
  266. if (bt->seq != bt->write_data[2]) /* idiot check */
  267. printk(KERN_DEBUG "BT: internal error: sequence mismatch\n");
  268. /* per the spec, the (NetFn, Seq, Cmd) tuples should match */
  269. if ((bt->read_data[3] == bt->write_data[3]) && /* Cmd */
  270. (bt->read_data[2] == bt->write_data[2]) && /* Sequence */
  271. ((bt->read_data[1] & 0xF8) == (bt->write_data[1] & 0xF8)))
  272. return 1;
  273. if (bt_debug & BT_DEBUG_MSG)
  274. printk(KERN_WARNING "BT: bad packet: "
  275. "want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
  276. bt->write_data[1], bt->write_data[2], bt->write_data[3],
  277. bt->read_data[1], bt->read_data[2], bt->read_data[3]);
  278. return 0;
  279. }
  280. /* Modifies bt->state appropriately, need to get into the bt_event() switch */
  281. static void error_recovery(struct si_sm_data *bt, char *reason)
  282. {
  283. unsigned char status;
  284. char buf[40]; /* For getting status */
  285. bt->timeout = BT_NORMAL_TIMEOUT; /* various places want to retry */
  286. status = BT_STATUS;
  287. printk(KERN_DEBUG "BT: %s in %s %s\n", reason, STATE2TXT,
  288. STATUS2TXT(buf));
  289. (bt->error_retries)++;
  290. if (bt->error_retries > BT_RETRY_LIMIT) {
  291. printk(KERN_DEBUG "retry limit (%d) exceeded\n", BT_RETRY_LIMIT);
  292. bt->state = BT_STATE_HOSED;
  293. if (!bt->nonzero_status)
  294. printk(KERN_ERR "IPMI: BT stuck, try power cycle\n");
  295. else if (bt->error_retries <= BT_RETRY_LIMIT + 1) {
  296. printk(KERN_DEBUG "IPMI: BT reset (takes 5 secs)\n");
  297. bt->state = BT_STATE_RESET1;
  298. }
  299. return;
  300. }
  301. /* Sometimes the BMC queues get in an "off-by-one" state...*/
  302. if ((bt->state == BT_STATE_B2H_WAIT) && (status & BT_B2H_ATN)) {
  303. printk(KERN_DEBUG "retry B2H_WAIT\n");
  304. return;
  305. }
  306. printk(KERN_DEBUG "restart command\n");
  307. bt->state = BT_STATE_RESTART;
  308. }
  309. /* Check the status and (possibly) advance the BT state machine. The
  310. default return is SI_SM_CALL_WITH_DELAY. */
  311. static enum si_sm_result bt_event(struct si_sm_data *bt, long time)
  312. {
  313. unsigned char status;
  314. char buf[40]; /* For getting status */
  315. int i;
  316. status = BT_STATUS;
  317. bt->nonzero_status |= status;
  318. if ((bt_debug & BT_DEBUG_STATES) && (bt->state != bt->last_state))
  319. printk(KERN_WARNING "BT: %s %s TO=%ld - %ld \n",
  320. STATE2TXT,
  321. STATUS2TXT(buf),
  322. bt->timeout,
  323. time);
  324. bt->last_state = bt->state;
  325. if (bt->state == BT_STATE_HOSED)
  326. return SI_SM_HOSED;
  327. if (bt->state != BT_STATE_IDLE) { /* do timeout test */
  328. bt->timeout -= time;
  329. if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1)) {
  330. error_recovery(bt, "timed out");
  331. return SI_SM_CALL_WITHOUT_DELAY;
  332. }
  333. }
  334. switch (bt->state) {
  335. case BT_STATE_IDLE: /* check for asynchronous messages */
  336. if (status & BT_SMS_ATN) {
  337. BT_CONTROL(BT_SMS_ATN); /* clear it */
  338. return SI_SM_ATTN;
  339. }
  340. return SI_SM_IDLE;
  341. case BT_STATE_XACTION_START:
  342. if (status & BT_H_BUSY) {
  343. BT_CONTROL(BT_H_BUSY);
  344. break;
  345. }
  346. if (status & BT_B2H_ATN)
  347. break;
  348. bt->state = BT_STATE_WRITE_BYTES;
  349. return SI_SM_CALL_WITHOUT_DELAY; /* for logging */
  350. case BT_STATE_WRITE_BYTES:
  351. if (status & (BT_B_BUSY | BT_H2B_ATN))
  352. break;
  353. BT_CONTROL(BT_CLR_WR_PTR);
  354. write_all_bytes(bt);
  355. BT_CONTROL(BT_H2B_ATN); /* clears too fast to catch? */
  356. bt->state = BT_STATE_WRITE_CONSUME;
  357. return SI_SM_CALL_WITHOUT_DELAY; /* it MIGHT sail through */
  358. case BT_STATE_WRITE_CONSUME: /* BMCs usually blow right thru here */
  359. if (status & (BT_H2B_ATN | BT_B_BUSY))
  360. break;
  361. bt->state = BT_STATE_B2H_WAIT;
  362. /* fall through with status */
  363. /* Stay in BT_STATE_B2H_WAIT until a packet matches. However, spinning
  364. hard here, constantly reading status, seems to hold off the
  365. generation of B2H_ATN so ALWAYS return CALL_WITH_DELAY. */
  366. case BT_STATE_B2H_WAIT:
  367. if (!(status & BT_B2H_ATN))
  368. break;
  369. /* Assume ordered, uncached writes: no need to wait */
  370. if (!(status & BT_H_BUSY))
  371. BT_CONTROL(BT_H_BUSY); /* set */
  372. BT_CONTROL(BT_B2H_ATN); /* clear it, ACK to the BMC */
  373. BT_CONTROL(BT_CLR_RD_PTR); /* reset the queue */
  374. i = read_all_bytes(bt);
  375. BT_CONTROL(BT_H_BUSY); /* clear */
  376. if (!i) /* Try this state again */
  377. break;
  378. bt->state = BT_STATE_READ_END;
  379. return SI_SM_CALL_WITHOUT_DELAY; /* for logging */
  380. case BT_STATE_READ_END:
  381. /* I could wait on BT_H_BUSY to go clear for a truly clean
  382. exit. However, this is already done in XACTION_START
  383. and the (possible) extra loop/status/possible wait affects
  384. performance. So, as long as it works, just ignore H_BUSY */
  385. #ifdef MAKE_THIS_TRUE_IF_NECESSARY
  386. if (status & BT_H_BUSY)
  387. break;
  388. #endif
  389. bt->seq++;
  390. bt->state = BT_STATE_IDLE;
  391. return SI_SM_TRANSACTION_COMPLETE;
  392. case BT_STATE_RESET1:
  393. reset_flags(bt);
  394. bt->timeout = BT_RESET_DELAY;
  395. bt->state = BT_STATE_RESET2;
  396. break;
  397. case BT_STATE_RESET2: /* Send a soft reset */
  398. BT_CONTROL(BT_CLR_WR_PTR);
  399. HOST2BMC(3); /* number of bytes following */
  400. HOST2BMC(0x18); /* NetFn/LUN == Application, LUN 0 */
  401. HOST2BMC(42); /* Sequence number */
  402. HOST2BMC(3); /* Cmd == Soft reset */
  403. BT_CONTROL(BT_H2B_ATN);
  404. bt->state = BT_STATE_RESET3;
  405. break;
  406. case BT_STATE_RESET3:
  407. if (bt->timeout > 0)
  408. return SI_SM_CALL_WITH_DELAY;
  409. bt->state = BT_STATE_RESTART; /* printk in debug modes */
  410. break;
  411. case BT_STATE_RESTART: /* don't reset retries! */
  412. reset_flags(bt);
  413. bt->write_data[2] = ++bt->seq;
  414. bt->read_count = 0;
  415. bt->nonzero_status = 0;
  416. bt->timeout = BT_NORMAL_TIMEOUT;
  417. bt->state = BT_STATE_XACTION_START;
  418. break;
  419. default: /* HOSED is supposed to be caught much earlier */
  420. error_recovery(bt, "internal logic error");
  421. break;
  422. }
  423. return SI_SM_CALL_WITH_DELAY;
  424. }
  425. static int bt_detect(struct si_sm_data *bt)
  426. {
  427. /* It's impossible for the BT status and interrupt registers to be
  428. all 1's, (assuming a properly functioning, self-initialized BMC)
  429. but that's what you get from reading a bogus address, so we
  430. test that first. The calling routine uses negative logic. */
  431. if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
  432. return 1;
  433. reset_flags(bt);
  434. return 0;
  435. }
  436. static void bt_cleanup(struct si_sm_data *bt)
  437. {
  438. }
  439. static int bt_size(void)
  440. {
  441. return sizeof(struct si_sm_data);
  442. }
  443. struct si_sm_handlers bt_smi_handlers =
  444. {
  445. .init_data = bt_init_data,
  446. .start_transaction = bt_start_transaction,
  447. .get_result = bt_get_result,
  448. .event = bt_event,
  449. .detect = bt_detect,
  450. .cleanup = bt_cleanup,
  451. .size = bt_size,
  452. };