ipmi_bt_sm.c 19 KB

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