ipmi_kcs_sm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * ipmi_kcs_sm.c
  3. *
  4. * State machine for handling IPMI KCS interfaces.
  5. *
  6. * Author: MontaVista Software, Inc.
  7. * Corey Minyard <minyard@mvista.com>
  8. * source@mvista.com
  9. *
  10. * Copyright 2002 MontaVista Software Inc.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  26. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  27. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. /*
  34. * This state machine is taken from the state machine in the IPMI spec,
  35. * pretty much verbatim. If you have questions about the states, see
  36. * that document.
  37. */
  38. #include <linux/kernel.h> /* For printk. */
  39. #include <linux/string.h>
  40. #include <linux/ipmi_msgdefs.h> /* for completion codes */
  41. #include "ipmi_si_sm.h"
  42. /* Set this if you want a printout of why the state machine was hosed
  43. when it gets hosed. */
  44. #define DEBUG_HOSED_REASON
  45. /* Print the state machine state on entry every time. */
  46. #undef DEBUG_STATE
  47. /* The states the KCS driver may be in. */
  48. enum kcs_states {
  49. KCS_IDLE, /* The KCS interface is currently
  50. doing nothing. */
  51. KCS_START_OP, /* We are starting an operation. The
  52. data is in the output buffer, but
  53. nothing has been done to the
  54. interface yet. This was added to
  55. the state machine in the spec to
  56. wait for the initial IBF. */
  57. KCS_WAIT_WRITE_START, /* We have written a write cmd to the
  58. interface. */
  59. KCS_WAIT_WRITE, /* We are writing bytes to the
  60. interface. */
  61. KCS_WAIT_WRITE_END, /* We have written the write end cmd
  62. to the interface, and still need to
  63. write the last byte. */
  64. KCS_WAIT_READ, /* We are waiting to read data from
  65. the interface. */
  66. KCS_ERROR0, /* State to transition to the error
  67. handler, this was added to the
  68. state machine in the spec to be
  69. sure IBF was there. */
  70. KCS_ERROR1, /* First stage error handler, wait for
  71. the interface to respond. */
  72. KCS_ERROR2, /* The abort cmd has been written,
  73. wait for the interface to
  74. respond. */
  75. KCS_ERROR3, /* We wrote some data to the
  76. interface, wait for it to switch to
  77. read mode. */
  78. KCS_HOSED /* The hardware failed to follow the
  79. state machine. */
  80. };
  81. #define MAX_KCS_READ_SIZE 80
  82. #define MAX_KCS_WRITE_SIZE 80
  83. /* Timeouts in microseconds. */
  84. #define IBF_RETRY_TIMEOUT 1000000
  85. #define OBF_RETRY_TIMEOUT 1000000
  86. #define MAX_ERROR_RETRIES 10
  87. struct si_sm_data
  88. {
  89. enum kcs_states state;
  90. struct si_sm_io *io;
  91. unsigned char write_data[MAX_KCS_WRITE_SIZE];
  92. int write_pos;
  93. int write_count;
  94. int orig_write_count;
  95. unsigned char read_data[MAX_KCS_READ_SIZE];
  96. int read_pos;
  97. int truncated;
  98. unsigned int error_retries;
  99. long ibf_timeout;
  100. long obf_timeout;
  101. };
  102. static unsigned int init_kcs_data(struct si_sm_data *kcs,
  103. struct si_sm_io *io)
  104. {
  105. kcs->state = KCS_IDLE;
  106. kcs->io = io;
  107. kcs->write_pos = 0;
  108. kcs->write_count = 0;
  109. kcs->orig_write_count = 0;
  110. kcs->read_pos = 0;
  111. kcs->error_retries = 0;
  112. kcs->truncated = 0;
  113. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  114. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  115. /* Reserve 2 I/O bytes. */
  116. return 2;
  117. }
  118. static inline unsigned char read_status(struct si_sm_data *kcs)
  119. {
  120. return kcs->io->inputb(kcs->io, 1);
  121. }
  122. static inline unsigned char read_data(struct si_sm_data *kcs)
  123. {
  124. return kcs->io->inputb(kcs->io, 0);
  125. }
  126. static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
  127. {
  128. kcs->io->outputb(kcs->io, 1, data);
  129. }
  130. static inline void write_data(struct si_sm_data *kcs, unsigned char data)
  131. {
  132. kcs->io->outputb(kcs->io, 0, data);
  133. }
  134. /* Control codes. */
  135. #define KCS_GET_STATUS_ABORT 0x60
  136. #define KCS_WRITE_START 0x61
  137. #define KCS_WRITE_END 0x62
  138. #define KCS_READ_BYTE 0x68
  139. /* Status bits. */
  140. #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
  141. #define KCS_IDLE_STATE 0
  142. #define KCS_READ_STATE 1
  143. #define KCS_WRITE_STATE 2
  144. #define KCS_ERROR_STATE 3
  145. #define GET_STATUS_ATN(status) ((status) & 0x04)
  146. #define GET_STATUS_IBF(status) ((status) & 0x02)
  147. #define GET_STATUS_OBF(status) ((status) & 0x01)
  148. static inline void write_next_byte(struct si_sm_data *kcs)
  149. {
  150. write_data(kcs, kcs->write_data[kcs->write_pos]);
  151. (kcs->write_pos)++;
  152. (kcs->write_count)--;
  153. }
  154. static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
  155. {
  156. (kcs->error_retries)++;
  157. if (kcs->error_retries > MAX_ERROR_RETRIES) {
  158. #ifdef DEBUG_HOSED_REASON
  159. printk("ipmi_kcs_sm: kcs hosed: %s\n", reason);
  160. #endif
  161. kcs->state = KCS_HOSED;
  162. } else {
  163. kcs->state = KCS_ERROR0;
  164. }
  165. }
  166. static inline void read_next_byte(struct si_sm_data *kcs)
  167. {
  168. if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
  169. /* Throw the data away and mark it truncated. */
  170. read_data(kcs);
  171. kcs->truncated = 1;
  172. } else {
  173. kcs->read_data[kcs->read_pos] = read_data(kcs);
  174. (kcs->read_pos)++;
  175. }
  176. write_data(kcs, KCS_READ_BYTE);
  177. }
  178. static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
  179. long time)
  180. {
  181. if (GET_STATUS_IBF(status)) {
  182. kcs->ibf_timeout -= time;
  183. if (kcs->ibf_timeout < 0) {
  184. start_error_recovery(kcs, "IBF not ready in time");
  185. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  191. return 1;
  192. }
  193. static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
  194. long time)
  195. {
  196. if (! GET_STATUS_OBF(status)) {
  197. kcs->obf_timeout -= time;
  198. if (kcs->obf_timeout < 0) {
  199. start_error_recovery(kcs, "OBF not ready in time");
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  205. return 1;
  206. }
  207. static void clear_obf(struct si_sm_data *kcs, unsigned char status)
  208. {
  209. if (GET_STATUS_OBF(status))
  210. read_data(kcs);
  211. }
  212. static void restart_kcs_transaction(struct si_sm_data *kcs)
  213. {
  214. kcs->write_count = kcs->orig_write_count;
  215. kcs->write_pos = 0;
  216. kcs->read_pos = 0;
  217. kcs->state = KCS_WAIT_WRITE_START;
  218. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  219. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  220. write_cmd(kcs, KCS_WRITE_START);
  221. }
  222. static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
  223. unsigned int size)
  224. {
  225. if ((size < 2) || (size > MAX_KCS_WRITE_SIZE)) {
  226. return -1;
  227. }
  228. if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) {
  229. return -2;
  230. }
  231. kcs->error_retries = 0;
  232. memcpy(kcs->write_data, data, size);
  233. kcs->write_count = size;
  234. kcs->orig_write_count = size;
  235. kcs->write_pos = 0;
  236. kcs->read_pos = 0;
  237. kcs->state = KCS_START_OP;
  238. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  239. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  240. return 0;
  241. }
  242. static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
  243. unsigned int length)
  244. {
  245. if (length < kcs->read_pos) {
  246. kcs->read_pos = length;
  247. kcs->truncated = 1;
  248. }
  249. memcpy(data, kcs->read_data, kcs->read_pos);
  250. if ((length >= 3) && (kcs->read_pos < 3)) {
  251. /* Guarantee that we return at least 3 bytes, with an
  252. error in the third byte if it is too short. */
  253. data[2] = IPMI_ERR_UNSPECIFIED;
  254. kcs->read_pos = 3;
  255. }
  256. if (kcs->truncated) {
  257. /* Report a truncated error. We might overwrite
  258. another error, but that's too bad, the user needs
  259. to know it was truncated. */
  260. data[2] = IPMI_ERR_MSG_TRUNCATED;
  261. kcs->truncated = 0;
  262. }
  263. return kcs->read_pos;
  264. }
  265. /* This implements the state machine defined in the IPMI manual, see
  266. that for details on how this works. Divide that flowchart into
  267. sections delimited by "Wait for IBF" and this will become clear. */
  268. static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
  269. {
  270. unsigned char status;
  271. unsigned char state;
  272. status = read_status(kcs);
  273. #ifdef DEBUG_STATE
  274. printk(" State = %d, %x\n", kcs->state, status);
  275. #endif
  276. /* All states wait for ibf, so just do it here. */
  277. if (!check_ibf(kcs, status, time))
  278. return SI_SM_CALL_WITH_DELAY;
  279. /* Just about everything looks at the KCS state, so grab that, too. */
  280. state = GET_STATUS_STATE(status);
  281. switch (kcs->state) {
  282. case KCS_IDLE:
  283. /* If there's and interrupt source, turn it off. */
  284. clear_obf(kcs, status);
  285. if (GET_STATUS_ATN(status))
  286. return SI_SM_ATTN;
  287. else
  288. return SI_SM_IDLE;
  289. case KCS_START_OP:
  290. if (state != KCS_IDLE) {
  291. start_error_recovery(kcs,
  292. "State machine not idle at start");
  293. break;
  294. }
  295. clear_obf(kcs, status);
  296. write_cmd(kcs, KCS_WRITE_START);
  297. kcs->state = KCS_WAIT_WRITE_START;
  298. break;
  299. case KCS_WAIT_WRITE_START:
  300. if (state != KCS_WRITE_STATE) {
  301. start_error_recovery(
  302. kcs,
  303. "Not in write state at write start");
  304. break;
  305. }
  306. read_data(kcs);
  307. if (kcs->write_count == 1) {
  308. write_cmd(kcs, KCS_WRITE_END);
  309. kcs->state = KCS_WAIT_WRITE_END;
  310. } else {
  311. write_next_byte(kcs);
  312. kcs->state = KCS_WAIT_WRITE;
  313. }
  314. break;
  315. case KCS_WAIT_WRITE:
  316. if (state != KCS_WRITE_STATE) {
  317. start_error_recovery(kcs,
  318. "Not in write state for write");
  319. break;
  320. }
  321. clear_obf(kcs, status);
  322. if (kcs->write_count == 1) {
  323. write_cmd(kcs, KCS_WRITE_END);
  324. kcs->state = KCS_WAIT_WRITE_END;
  325. } else {
  326. write_next_byte(kcs);
  327. }
  328. break;
  329. case KCS_WAIT_WRITE_END:
  330. if (state != KCS_WRITE_STATE) {
  331. start_error_recovery(kcs,
  332. "Not in write state for write end");
  333. break;
  334. }
  335. clear_obf(kcs, status);
  336. write_next_byte(kcs);
  337. kcs->state = KCS_WAIT_READ;
  338. break;
  339. case KCS_WAIT_READ:
  340. if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
  341. start_error_recovery(
  342. kcs,
  343. "Not in read or idle in read state");
  344. break;
  345. }
  346. if (state == KCS_READ_STATE) {
  347. if (! check_obf(kcs, status, time))
  348. return SI_SM_CALL_WITH_DELAY;
  349. read_next_byte(kcs);
  350. } else {
  351. /* We don't implement this exactly like the state
  352. machine in the spec. Some broken hardware
  353. does not write the final dummy byte to the
  354. read register. Thus obf will never go high
  355. here. We just go straight to idle, and we
  356. handle clearing out obf in idle state if it
  357. happens to come in. */
  358. clear_obf(kcs, status);
  359. kcs->orig_write_count = 0;
  360. kcs->state = KCS_IDLE;
  361. return SI_SM_TRANSACTION_COMPLETE;
  362. }
  363. break;
  364. case KCS_ERROR0:
  365. clear_obf(kcs, status);
  366. write_cmd(kcs, KCS_GET_STATUS_ABORT);
  367. kcs->state = KCS_ERROR1;
  368. break;
  369. case KCS_ERROR1:
  370. clear_obf(kcs, status);
  371. write_data(kcs, 0);
  372. kcs->state = KCS_ERROR2;
  373. break;
  374. case KCS_ERROR2:
  375. if (state != KCS_READ_STATE) {
  376. start_error_recovery(kcs,
  377. "Not in read state for error2");
  378. break;
  379. }
  380. if (! check_obf(kcs, status, time))
  381. return SI_SM_CALL_WITH_DELAY;
  382. clear_obf(kcs, status);
  383. write_data(kcs, KCS_READ_BYTE);
  384. kcs->state = KCS_ERROR3;
  385. break;
  386. case KCS_ERROR3:
  387. if (state != KCS_IDLE_STATE) {
  388. start_error_recovery(kcs,
  389. "Not in idle state for error3");
  390. break;
  391. }
  392. if (! check_obf(kcs, status, time))
  393. return SI_SM_CALL_WITH_DELAY;
  394. clear_obf(kcs, status);
  395. if (kcs->orig_write_count) {
  396. restart_kcs_transaction(kcs);
  397. } else {
  398. kcs->state = KCS_IDLE;
  399. return SI_SM_TRANSACTION_COMPLETE;
  400. }
  401. break;
  402. case KCS_HOSED:
  403. break;
  404. }
  405. if (kcs->state == KCS_HOSED) {
  406. init_kcs_data(kcs, kcs->io);
  407. return SI_SM_HOSED;
  408. }
  409. return SI_SM_CALL_WITHOUT_DELAY;
  410. }
  411. static int kcs_size(void)
  412. {
  413. return sizeof(struct si_sm_data);
  414. }
  415. static int kcs_detect(struct si_sm_data *kcs)
  416. {
  417. /* It's impossible for the KCS status register to be all 1's,
  418. (assuming a properly functioning, self-initialized BMC)
  419. but that's what you get from reading a bogus address, so we
  420. test that first. */
  421. if (read_status(kcs) == 0xff)
  422. return 1;
  423. return 0;
  424. }
  425. static void kcs_cleanup(struct si_sm_data *kcs)
  426. {
  427. }
  428. struct si_sm_handlers kcs_smi_handlers =
  429. {
  430. .init_data = init_kcs_data,
  431. .start_transaction = start_kcs_transaction,
  432. .get_result = get_kcs_result,
  433. .event = kcs_event,
  434. .detect = kcs_detect,
  435. .cleanup = kcs_cleanup,
  436. .size = kcs_size,
  437. };