ipmi_bt_sm.c 19 KB

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