ipmi_bt_sm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. #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. return IPMI_NOT_IN_MY_STATE_ERR;
  187. if (bt_debug & BT_DEBUG_MSG) {
  188. dev_dbg(bt->io->dev, "+++++++++++++++++ New command\n");
  189. dev_dbg(bt->io->dev, "NetFn/LUN CMD [%d data]:", size - 2);
  190. for (i = 0; i < size; i ++)
  191. pr_cont(" %02x", data[i]);
  192. pr_cont("\n");
  193. }
  194. bt->write_data[0] = size + 1; /* all data plus seq byte */
  195. bt->write_data[1] = *data; /* NetFn/LUN */
  196. bt->write_data[2] = bt->seq++;
  197. memcpy(bt->write_data + 3, data + 1, size - 1);
  198. bt->write_count = size + 2;
  199. bt->error_retries = 0;
  200. bt->nonzero_status = 0;
  201. bt->truncated = 0;
  202. bt->state = BT_STATE_XACTION_START;
  203. bt->timeout = bt->BT_CAP_req2rsp;
  204. force_result(bt, IPMI_ERR_UNSPECIFIED);
  205. return 0;
  206. }
  207. /*
  208. * After the upper state machine has been told SI_SM_TRANSACTION_COMPLETE
  209. * it calls this. Strip out the length and seq bytes.
  210. */
  211. static int bt_get_result(struct si_sm_data *bt,
  212. unsigned char *data,
  213. unsigned int length)
  214. {
  215. int i, msg_len;
  216. msg_len = bt->read_count - 2; /* account for length & seq */
  217. if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
  218. force_result(bt, IPMI_ERR_UNSPECIFIED);
  219. msg_len = 3;
  220. }
  221. data[0] = bt->read_data[1];
  222. data[1] = bt->read_data[3];
  223. if (length < msg_len || bt->truncated) {
  224. data[2] = IPMI_ERR_MSG_TRUNCATED;
  225. msg_len = 3;
  226. } else
  227. memcpy(data + 2, bt->read_data + 4, msg_len - 2);
  228. if (bt_debug & BT_DEBUG_MSG) {
  229. dev_dbg(bt->io->dev, "result %d bytes:", msg_len);
  230. for (i = 0; i < msg_len; i++)
  231. pr_cont(" %02x", data[i]);
  232. pr_cont("\n");
  233. }
  234. return msg_len;
  235. }
  236. /* This bit's functionality is optional */
  237. #define BT_BMC_HWRST 0x80
  238. static void reset_flags(struct si_sm_data *bt)
  239. {
  240. if (bt_debug)
  241. dev_dbg(bt->io->dev, "flag reset %s\n", 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. dev_dbg(bt->io->dev, "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. pr_cont("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. dev_dbg(bt->io->dev, "write %d bytes seq=0x%02X",
  277. bt->write_count, bt->seq);
  278. for (i = 0; i < bt->write_count; i++)
  279. pr_cont(" %02x", bt->write_data[i]);
  280. pr_cont("\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. dev_dbg(bt->io->dev,
  297. "bad raw rsp len=%d\n", 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. dev_dbg(bt->io->dev,
  307. "got %d bytes seq=0x%02X", max, bt->read_data[2]);
  308. if (max > 16)
  309. max = 16;
  310. for (i = 0; i < max; i++)
  311. pr_cont(" %02x", bt->read_data[i]);
  312. pr_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. dev_dbg(bt->io->dev,
  321. "IPMI BT: bad packet: want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
  322. bt->write_data[1] | 0x04, bt->write_data[2],
  323. bt->write_data[3],
  324. bt->read_data[1], bt->read_data[2], bt->read_data[3]);
  325. return 0;
  326. }
  327. /* Restart if retries are left, or return an error completion code */
  328. static enum si_sm_result error_recovery(struct si_sm_data *bt,
  329. unsigned char status,
  330. unsigned char cCode)
  331. {
  332. char *reason;
  333. bt->timeout = bt->BT_CAP_req2rsp;
  334. switch (cCode) {
  335. case IPMI_TIMEOUT_ERR:
  336. reason = "timeout";
  337. break;
  338. default:
  339. reason = "internal error";
  340. break;
  341. }
  342. dev_warn(bt->io->dev, "IPMI BT: %s in %s %s ", /* open-ended line */
  343. reason, STATE2TXT, STATUS2TXT);
  344. /*
  345. * Per the IPMI spec, retries are based on the sequence number
  346. * known only to this module, so manage a restart here.
  347. */
  348. (bt->error_retries)++;
  349. if (bt->error_retries < bt->BT_CAP_retries) {
  350. pr_cont("%d retries left\n",
  351. bt->BT_CAP_retries - bt->error_retries);
  352. bt->state = BT_STATE_RESTART;
  353. return SI_SM_CALL_WITHOUT_DELAY;
  354. }
  355. dev_warn(bt->io->dev, "failed %d retries, sending error response\n",
  356. bt->BT_CAP_retries);
  357. if (!bt->nonzero_status)
  358. dev_err(bt->io->dev, "stuck, try power cycle\n");
  359. /* this is most likely during insmod */
  360. else if (bt->seq <= (unsigned char)(bt->BT_CAP_retries & 0xFF)) {
  361. dev_warn(bt->io->dev, "BT reset (takes 5 secs)\n");
  362. bt->state = BT_STATE_RESET1;
  363. return SI_SM_CALL_WITHOUT_DELAY;
  364. }
  365. /*
  366. * Concoct a useful error message, set up the next state, and
  367. * be done with this sequence.
  368. */
  369. bt->state = BT_STATE_IDLE;
  370. switch (cCode) {
  371. case IPMI_TIMEOUT_ERR:
  372. if (status & BT_B_BUSY) {
  373. cCode = IPMI_NODE_BUSY_ERR;
  374. bt->state = BT_STATE_LONG_BUSY;
  375. }
  376. break;
  377. default:
  378. break;
  379. }
  380. force_result(bt, cCode);
  381. return SI_SM_TRANSACTION_COMPLETE;
  382. }
  383. /* Check status and (usually) take action and change this state machine. */
  384. static enum si_sm_result bt_event(struct si_sm_data *bt, long time)
  385. {
  386. unsigned char status;
  387. static enum bt_states last_printed = BT_STATE_PRINTME;
  388. int i;
  389. status = BT_STATUS;
  390. bt->nonzero_status |= status;
  391. if ((bt_debug & BT_DEBUG_STATES) && (bt->state != last_printed)) {
  392. dev_dbg(bt->io->dev, "BT: %s %s TO=%ld - %ld\n",
  393. STATE2TXT,
  394. STATUS2TXT,
  395. bt->timeout,
  396. time);
  397. last_printed = bt->state;
  398. }
  399. /*
  400. * Commands that time out may still (eventually) provide a response.
  401. * This stale response will get in the way of a new response so remove
  402. * it if possible (hopefully during IDLE). Even if it comes up later
  403. * it will be rejected by its (now-forgotten) seq number.
  404. */
  405. if ((bt->state < BT_STATE_WRITE_BYTES) && (status & BT_B2H_ATN)) {
  406. drain_BMC2HOST(bt);
  407. BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
  408. }
  409. if ((bt->state != BT_STATE_IDLE) &&
  410. (bt->state < BT_STATE_PRINTME)) {
  411. /* check timeout */
  412. bt->timeout -= time;
  413. if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1))
  414. return error_recovery(bt,
  415. status,
  416. IPMI_TIMEOUT_ERR);
  417. }
  418. switch (bt->state) {
  419. /*
  420. * Idle state first checks for asynchronous messages from another
  421. * channel, then does some opportunistic housekeeping.
  422. */
  423. case BT_STATE_IDLE:
  424. if (status & BT_SMS_ATN) {
  425. BT_CONTROL(BT_SMS_ATN); /* clear it */
  426. return SI_SM_ATTN;
  427. }
  428. if (status & BT_H_BUSY) /* clear a leftover H_BUSY */
  429. BT_CONTROL(BT_H_BUSY);
  430. BT_SI_SM_RETURN(SI_SM_IDLE);
  431. case BT_STATE_XACTION_START:
  432. if (status & (BT_B_BUSY | BT_H2B_ATN))
  433. BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
  434. if (BT_STATUS & BT_H_BUSY)
  435. BT_CONTROL(BT_H_BUSY); /* force clear */
  436. BT_STATE_CHANGE(BT_STATE_WRITE_BYTES,
  437. SI_SM_CALL_WITHOUT_DELAY);
  438. case BT_STATE_WRITE_BYTES:
  439. if (status & BT_H_BUSY)
  440. BT_CONTROL(BT_H_BUSY); /* clear */
  441. BT_CONTROL(BT_CLR_WR_PTR);
  442. write_all_bytes(bt);
  443. BT_CONTROL(BT_H2B_ATN); /* can clear too fast to catch */
  444. BT_STATE_CHANGE(BT_STATE_WRITE_CONSUME,
  445. SI_SM_CALL_WITHOUT_DELAY);
  446. case BT_STATE_WRITE_CONSUME:
  447. if (status & (BT_B_BUSY | BT_H2B_ATN))
  448. BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
  449. BT_STATE_CHANGE(BT_STATE_READ_WAIT,
  450. SI_SM_CALL_WITHOUT_DELAY);
  451. /* Spinning hard can suppress B2H_ATN and force a timeout */
  452. case BT_STATE_READ_WAIT:
  453. if (!(status & BT_B2H_ATN))
  454. BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
  455. BT_CONTROL(BT_H_BUSY); /* set */
  456. /*
  457. * Uncached, ordered writes should just proceed serially but
  458. * some BMCs don't clear B2H_ATN with one hit. Fast-path a
  459. * workaround without too much penalty to the general case.
  460. */
  461. BT_CONTROL(BT_B2H_ATN); /* clear it to ACK the BMC */
  462. BT_STATE_CHANGE(BT_STATE_CLEAR_B2H,
  463. SI_SM_CALL_WITHOUT_DELAY);
  464. case BT_STATE_CLEAR_B2H:
  465. if (status & BT_B2H_ATN) {
  466. /* keep hitting it */
  467. BT_CONTROL(BT_B2H_ATN);
  468. BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
  469. }
  470. BT_STATE_CHANGE(BT_STATE_READ_BYTES,
  471. SI_SM_CALL_WITHOUT_DELAY);
  472. case BT_STATE_READ_BYTES:
  473. if (!(status & BT_H_BUSY))
  474. /* check in case of retry */
  475. BT_CONTROL(BT_H_BUSY);
  476. BT_CONTROL(BT_CLR_RD_PTR); /* start of BMC2HOST buffer */
  477. i = read_all_bytes(bt); /* true == packet seq match */
  478. BT_CONTROL(BT_H_BUSY); /* NOW clear */
  479. if (!i) /* Not my message */
  480. BT_STATE_CHANGE(BT_STATE_READ_WAIT,
  481. SI_SM_CALL_WITHOUT_DELAY);
  482. bt->state = bt->complete;
  483. return bt->state == BT_STATE_IDLE ? /* where to next? */
  484. SI_SM_TRANSACTION_COMPLETE : /* normal */
  485. SI_SM_CALL_WITHOUT_DELAY; /* Startup magic */
  486. case BT_STATE_LONG_BUSY: /* For example: after FW update */
  487. if (!(status & BT_B_BUSY)) {
  488. reset_flags(bt); /* next state is now IDLE */
  489. bt_init_data(bt, bt->io);
  490. }
  491. return SI_SM_CALL_WITH_DELAY; /* No repeat printing */
  492. case BT_STATE_RESET1:
  493. reset_flags(bt);
  494. drain_BMC2HOST(bt);
  495. BT_STATE_CHANGE(BT_STATE_RESET2,
  496. SI_SM_CALL_WITH_DELAY);
  497. case BT_STATE_RESET2: /* Send a soft reset */
  498. BT_CONTROL(BT_CLR_WR_PTR);
  499. HOST2BMC(3); /* number of bytes following */
  500. HOST2BMC(0x18); /* NetFn/LUN == Application, LUN 0 */
  501. HOST2BMC(42); /* Sequence number */
  502. HOST2BMC(3); /* Cmd == Soft reset */
  503. BT_CONTROL(BT_H2B_ATN);
  504. bt->timeout = BT_RESET_DELAY * USEC_PER_SEC;
  505. BT_STATE_CHANGE(BT_STATE_RESET3,
  506. SI_SM_CALL_WITH_DELAY);
  507. case BT_STATE_RESET3: /* Hold off everything for a bit */
  508. if (bt->timeout > 0)
  509. return SI_SM_CALL_WITH_DELAY;
  510. drain_BMC2HOST(bt);
  511. BT_STATE_CHANGE(BT_STATE_RESTART,
  512. SI_SM_CALL_WITH_DELAY);
  513. case BT_STATE_RESTART: /* don't reset retries or seq! */
  514. bt->read_count = 0;
  515. bt->nonzero_status = 0;
  516. bt->timeout = bt->BT_CAP_req2rsp;
  517. BT_STATE_CHANGE(BT_STATE_XACTION_START,
  518. SI_SM_CALL_WITH_DELAY);
  519. default: /* should never occur */
  520. return error_recovery(bt,
  521. status,
  522. IPMI_ERR_UNSPECIFIED);
  523. }
  524. return SI_SM_CALL_WITH_DELAY;
  525. }
  526. static int bt_detect(struct si_sm_data *bt)
  527. {
  528. unsigned char GetBT_CAP[] = { 0x18, 0x36 };
  529. unsigned char BT_CAP[8];
  530. enum si_sm_result smi_result;
  531. int rv;
  532. /*
  533. * It's impossible for the BT status and interrupt registers to be
  534. * all 1's, (assuming a properly functioning, self-initialized BMC)
  535. * but that's what you get from reading a bogus address, so we
  536. * test that first. The calling routine uses negative logic.
  537. */
  538. if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
  539. return 1;
  540. reset_flags(bt);
  541. /*
  542. * Try getting the BT capabilities here.
  543. */
  544. rv = bt_start_transaction(bt, GetBT_CAP, sizeof(GetBT_CAP));
  545. if (rv) {
  546. dev_warn(bt->io->dev,
  547. "Can't start capabilities transaction: %d\n", rv);
  548. goto out_no_bt_cap;
  549. }
  550. smi_result = SI_SM_CALL_WITHOUT_DELAY;
  551. for (;;) {
  552. if (smi_result == SI_SM_CALL_WITH_DELAY ||
  553. smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
  554. schedule_timeout_uninterruptible(1);
  555. smi_result = bt_event(bt, jiffies_to_usecs(1));
  556. } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
  557. smi_result = bt_event(bt, 0);
  558. } else
  559. break;
  560. }
  561. rv = bt_get_result(bt, BT_CAP, sizeof(BT_CAP));
  562. bt_init_data(bt, bt->io);
  563. if (rv < 8) {
  564. dev_warn(bt->io->dev, "bt cap response too short: %d\n", rv);
  565. goto out_no_bt_cap;
  566. }
  567. if (BT_CAP[2]) {
  568. dev_warn(bt->io->dev, "Error fetching bt cap: %x\n", BT_CAP[2]);
  569. out_no_bt_cap:
  570. dev_warn(bt->io->dev, "using default values\n");
  571. } else {
  572. bt->BT_CAP_req2rsp = BT_CAP[6] * USEC_PER_SEC;
  573. bt->BT_CAP_retries = BT_CAP[7];
  574. }
  575. dev_info(bt->io->dev, "req2rsp=%ld secs retries=%d\n",
  576. bt->BT_CAP_req2rsp / USEC_PER_SEC, bt->BT_CAP_retries);
  577. return 0;
  578. }
  579. static void bt_cleanup(struct si_sm_data *bt)
  580. {
  581. }
  582. static int bt_size(void)
  583. {
  584. return sizeof(struct si_sm_data);
  585. }
  586. const struct si_sm_handlers bt_smi_handlers = {
  587. .init_data = bt_init_data,
  588. .start_transaction = bt_start_transaction,
  589. .get_result = bt_get_result,
  590. .event = bt_event,
  591. .detect = bt_detect,
  592. .cleanup = bt_cleanup,
  593. .size = bt_size,
  594. };