ipmi_bt_sm.c 19 KB

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