ipmi_bt_sm.c 20 KB

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