ipmi_kcs_sm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/string.h>
  42. #include <linux/jiffies.h>
  43. #include <linux/ipmi_msgdefs.h> /* for completion codes */
  44. #include "ipmi_si_sm.h"
  45. /* kcs_debug is a bit-field
  46. * KCS_DEBUG_ENABLE - turned on for now
  47. * KCS_DEBUG_MSG - commands and their responses
  48. * KCS_DEBUG_STATES - state machine
  49. */
  50. #define KCS_DEBUG_STATES 4
  51. #define KCS_DEBUG_MSG 2
  52. #define KCS_DEBUG_ENABLE 1
  53. static int kcs_debug;
  54. module_param(kcs_debug, int, 0644);
  55. MODULE_PARM_DESC(kcs_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
  56. /* The states the KCS driver may be in. */
  57. enum kcs_states {
  58. KCS_IDLE, /* The KCS interface is currently
  59. doing nothing. */
  60. KCS_START_OP, /* We are starting an operation. The
  61. data is in the output buffer, but
  62. nothing has been done to the
  63. interface yet. This was added to
  64. the state machine in the spec to
  65. wait for the initial IBF. */
  66. KCS_WAIT_WRITE_START, /* We have written a write cmd to the
  67. interface. */
  68. KCS_WAIT_WRITE, /* We are writing bytes to the
  69. interface. */
  70. KCS_WAIT_WRITE_END, /* We have written the write end cmd
  71. to the interface, and still need to
  72. write the last byte. */
  73. KCS_WAIT_READ, /* We are waiting to read data from
  74. the interface. */
  75. KCS_ERROR0, /* State to transition to the error
  76. handler, this was added to the
  77. state machine in the spec to be
  78. sure IBF was there. */
  79. KCS_ERROR1, /* First stage error handler, wait for
  80. the interface to respond. */
  81. KCS_ERROR2, /* The abort cmd has been written,
  82. wait for the interface to
  83. respond. */
  84. KCS_ERROR3, /* We wrote some data to the
  85. interface, wait for it to switch to
  86. read mode. */
  87. KCS_HOSED /* The hardware failed to follow the
  88. state machine. */
  89. };
  90. #define MAX_KCS_READ_SIZE IPMI_MAX_MSG_LENGTH
  91. #define MAX_KCS_WRITE_SIZE IPMI_MAX_MSG_LENGTH
  92. /* Timeouts in microseconds. */
  93. #define IBF_RETRY_TIMEOUT 1000000
  94. #define OBF_RETRY_TIMEOUT 1000000
  95. #define MAX_ERROR_RETRIES 10
  96. #define ERROR0_OBF_WAIT_JIFFIES (2*HZ)
  97. struct si_sm_data
  98. {
  99. enum kcs_states state;
  100. struct si_sm_io *io;
  101. unsigned char write_data[MAX_KCS_WRITE_SIZE];
  102. int write_pos;
  103. int write_count;
  104. int orig_write_count;
  105. unsigned char read_data[MAX_KCS_READ_SIZE];
  106. int read_pos;
  107. int truncated;
  108. unsigned int error_retries;
  109. long ibf_timeout;
  110. long obf_timeout;
  111. unsigned long error0_timeout;
  112. };
  113. static unsigned int init_kcs_data(struct si_sm_data *kcs,
  114. struct si_sm_io *io)
  115. {
  116. kcs->state = KCS_IDLE;
  117. kcs->io = io;
  118. kcs->write_pos = 0;
  119. kcs->write_count = 0;
  120. kcs->orig_write_count = 0;
  121. kcs->read_pos = 0;
  122. kcs->error_retries = 0;
  123. kcs->truncated = 0;
  124. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  125. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  126. /* Reserve 2 I/O bytes. */
  127. return 2;
  128. }
  129. static inline unsigned char read_status(struct si_sm_data *kcs)
  130. {
  131. return kcs->io->inputb(kcs->io, 1);
  132. }
  133. static inline unsigned char read_data(struct si_sm_data *kcs)
  134. {
  135. return kcs->io->inputb(kcs->io, 0);
  136. }
  137. static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
  138. {
  139. kcs->io->outputb(kcs->io, 1, data);
  140. }
  141. static inline void write_data(struct si_sm_data *kcs, unsigned char data)
  142. {
  143. kcs->io->outputb(kcs->io, 0, data);
  144. }
  145. /* Control codes. */
  146. #define KCS_GET_STATUS_ABORT 0x60
  147. #define KCS_WRITE_START 0x61
  148. #define KCS_WRITE_END 0x62
  149. #define KCS_READ_BYTE 0x68
  150. /* Status bits. */
  151. #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
  152. #define KCS_IDLE_STATE 0
  153. #define KCS_READ_STATE 1
  154. #define KCS_WRITE_STATE 2
  155. #define KCS_ERROR_STATE 3
  156. #define GET_STATUS_ATN(status) ((status) & 0x04)
  157. #define GET_STATUS_IBF(status) ((status) & 0x02)
  158. #define GET_STATUS_OBF(status) ((status) & 0x01)
  159. static inline void write_next_byte(struct si_sm_data *kcs)
  160. {
  161. write_data(kcs, kcs->write_data[kcs->write_pos]);
  162. (kcs->write_pos)++;
  163. (kcs->write_count)--;
  164. }
  165. static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
  166. {
  167. (kcs->error_retries)++;
  168. if (kcs->error_retries > MAX_ERROR_RETRIES) {
  169. if (kcs_debug & KCS_DEBUG_ENABLE)
  170. printk(KERN_DEBUG "ipmi_kcs_sm: kcs hosed: %s\n", reason);
  171. kcs->state = KCS_HOSED;
  172. } else {
  173. kcs->error0_timeout = jiffies + ERROR0_OBF_WAIT_JIFFIES;
  174. kcs->state = KCS_ERROR0;
  175. }
  176. }
  177. static inline void read_next_byte(struct si_sm_data *kcs)
  178. {
  179. if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
  180. /* Throw the data away and mark it truncated. */
  181. read_data(kcs);
  182. kcs->truncated = 1;
  183. } else {
  184. kcs->read_data[kcs->read_pos] = read_data(kcs);
  185. (kcs->read_pos)++;
  186. }
  187. write_data(kcs, KCS_READ_BYTE);
  188. }
  189. static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
  190. long time)
  191. {
  192. if (GET_STATUS_IBF(status)) {
  193. kcs->ibf_timeout -= time;
  194. if (kcs->ibf_timeout < 0) {
  195. start_error_recovery(kcs, "IBF not ready in time");
  196. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  202. return 1;
  203. }
  204. static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
  205. long time)
  206. {
  207. if (!GET_STATUS_OBF(status)) {
  208. kcs->obf_timeout -= time;
  209. if (kcs->obf_timeout < 0) {
  210. start_error_recovery(kcs, "OBF not ready in time");
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  216. return 1;
  217. }
  218. static void clear_obf(struct si_sm_data *kcs, unsigned char status)
  219. {
  220. if (GET_STATUS_OBF(status))
  221. read_data(kcs);
  222. }
  223. static void restart_kcs_transaction(struct si_sm_data *kcs)
  224. {
  225. kcs->write_count = kcs->orig_write_count;
  226. kcs->write_pos = 0;
  227. kcs->read_pos = 0;
  228. kcs->state = KCS_WAIT_WRITE_START;
  229. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  230. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  231. write_cmd(kcs, KCS_WRITE_START);
  232. }
  233. static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
  234. unsigned int size)
  235. {
  236. unsigned int i;
  237. if (size < 2)
  238. return IPMI_REQ_LEN_INVALID_ERR;
  239. if (size > MAX_KCS_WRITE_SIZE)
  240. return IPMI_REQ_LEN_EXCEEDED_ERR;
  241. if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED))
  242. return IPMI_NOT_IN_MY_STATE_ERR;
  243. if (kcs_debug & KCS_DEBUG_MSG) {
  244. printk(KERN_DEBUG "start_kcs_transaction -");
  245. for (i = 0; i < size; i ++) {
  246. printk(" %02x", (unsigned char) (data [i]));
  247. }
  248. printk ("\n");
  249. }
  250. kcs->error_retries = 0;
  251. memcpy(kcs->write_data, data, size);
  252. kcs->write_count = size;
  253. kcs->orig_write_count = size;
  254. kcs->write_pos = 0;
  255. kcs->read_pos = 0;
  256. kcs->state = KCS_START_OP;
  257. kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
  258. kcs->obf_timeout = OBF_RETRY_TIMEOUT;
  259. return 0;
  260. }
  261. static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
  262. unsigned int length)
  263. {
  264. if (length < kcs->read_pos) {
  265. kcs->read_pos = length;
  266. kcs->truncated = 1;
  267. }
  268. memcpy(data, kcs->read_data, kcs->read_pos);
  269. if ((length >= 3) && (kcs->read_pos < 3)) {
  270. /* Guarantee that we return at least 3 bytes, with an
  271. error in the third byte if it is too short. */
  272. data[2] = IPMI_ERR_UNSPECIFIED;
  273. kcs->read_pos = 3;
  274. }
  275. if (kcs->truncated) {
  276. /* Report a truncated error. We might overwrite
  277. another error, but that's too bad, the user needs
  278. to know it was truncated. */
  279. data[2] = IPMI_ERR_MSG_TRUNCATED;
  280. kcs->truncated = 0;
  281. }
  282. return kcs->read_pos;
  283. }
  284. /* This implements the state machine defined in the IPMI manual, see
  285. that for details on how this works. Divide that flowchart into
  286. sections delimited by "Wait for IBF" and this will become clear. */
  287. static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
  288. {
  289. unsigned char status;
  290. unsigned char state;
  291. status = read_status(kcs);
  292. if (kcs_debug & KCS_DEBUG_STATES)
  293. printk(KERN_DEBUG "KCS: State = %d, %x\n", kcs->state, status);
  294. /* All states wait for ibf, so just do it here. */
  295. if (!check_ibf(kcs, status, time))
  296. return SI_SM_CALL_WITH_DELAY;
  297. /* Just about everything looks at the KCS state, so grab that, too. */
  298. state = GET_STATUS_STATE(status);
  299. switch (kcs->state) {
  300. case KCS_IDLE:
  301. /* If there's and interrupt source, turn it off. */
  302. clear_obf(kcs, status);
  303. if (GET_STATUS_ATN(status))
  304. return SI_SM_ATTN;
  305. else
  306. return SI_SM_IDLE;
  307. case KCS_START_OP:
  308. if (state != KCS_IDLE) {
  309. start_error_recovery(kcs,
  310. "State machine not idle at start");
  311. break;
  312. }
  313. clear_obf(kcs, status);
  314. write_cmd(kcs, KCS_WRITE_START);
  315. kcs->state = KCS_WAIT_WRITE_START;
  316. break;
  317. case KCS_WAIT_WRITE_START:
  318. if (state != KCS_WRITE_STATE) {
  319. start_error_recovery(
  320. kcs,
  321. "Not in write state at write start");
  322. break;
  323. }
  324. read_data(kcs);
  325. if (kcs->write_count == 1) {
  326. write_cmd(kcs, KCS_WRITE_END);
  327. kcs->state = KCS_WAIT_WRITE_END;
  328. } else {
  329. write_next_byte(kcs);
  330. kcs->state = KCS_WAIT_WRITE;
  331. }
  332. break;
  333. case KCS_WAIT_WRITE:
  334. if (state != KCS_WRITE_STATE) {
  335. start_error_recovery(kcs,
  336. "Not in write state for write");
  337. break;
  338. }
  339. clear_obf(kcs, status);
  340. if (kcs->write_count == 1) {
  341. write_cmd(kcs, KCS_WRITE_END);
  342. kcs->state = KCS_WAIT_WRITE_END;
  343. } else {
  344. write_next_byte(kcs);
  345. }
  346. break;
  347. case KCS_WAIT_WRITE_END:
  348. if (state != KCS_WRITE_STATE) {
  349. start_error_recovery(kcs,
  350. "Not in write state for write end");
  351. break;
  352. }
  353. clear_obf(kcs, status);
  354. write_next_byte(kcs);
  355. kcs->state = KCS_WAIT_READ;
  356. break;
  357. case KCS_WAIT_READ:
  358. if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
  359. start_error_recovery(
  360. kcs,
  361. "Not in read or idle in read state");
  362. break;
  363. }
  364. if (state == KCS_READ_STATE) {
  365. if (!check_obf(kcs, status, time))
  366. return SI_SM_CALL_WITH_DELAY;
  367. read_next_byte(kcs);
  368. } else {
  369. /* We don't implement this exactly like the state
  370. machine in the spec. Some broken hardware
  371. does not write the final dummy byte to the
  372. read register. Thus obf will never go high
  373. here. We just go straight to idle, and we
  374. handle clearing out obf in idle state if it
  375. happens to come in. */
  376. clear_obf(kcs, status);
  377. kcs->orig_write_count = 0;
  378. kcs->state = KCS_IDLE;
  379. return SI_SM_TRANSACTION_COMPLETE;
  380. }
  381. break;
  382. case KCS_ERROR0:
  383. clear_obf(kcs, status);
  384. status = read_status(kcs);
  385. if (GET_STATUS_OBF(status)) /* controller isn't responding */
  386. if (time_before(jiffies, kcs->error0_timeout))
  387. return SI_SM_CALL_WITH_TICK_DELAY;
  388. write_cmd(kcs, KCS_GET_STATUS_ABORT);
  389. kcs->state = KCS_ERROR1;
  390. break;
  391. case KCS_ERROR1:
  392. clear_obf(kcs, status);
  393. write_data(kcs, 0);
  394. kcs->state = KCS_ERROR2;
  395. break;
  396. case KCS_ERROR2:
  397. if (state != KCS_READ_STATE) {
  398. start_error_recovery(kcs,
  399. "Not in read state for error2");
  400. break;
  401. }
  402. if (!check_obf(kcs, status, time))
  403. return SI_SM_CALL_WITH_DELAY;
  404. clear_obf(kcs, status);
  405. write_data(kcs, KCS_READ_BYTE);
  406. kcs->state = KCS_ERROR3;
  407. break;
  408. case KCS_ERROR3:
  409. if (state != KCS_IDLE_STATE) {
  410. start_error_recovery(kcs,
  411. "Not in idle state for error3");
  412. break;
  413. }
  414. if (!check_obf(kcs, status, time))
  415. return SI_SM_CALL_WITH_DELAY;
  416. clear_obf(kcs, status);
  417. if (kcs->orig_write_count) {
  418. restart_kcs_transaction(kcs);
  419. } else {
  420. kcs->state = KCS_IDLE;
  421. return SI_SM_TRANSACTION_COMPLETE;
  422. }
  423. break;
  424. case KCS_HOSED:
  425. break;
  426. }
  427. if (kcs->state == KCS_HOSED) {
  428. init_kcs_data(kcs, kcs->io);
  429. return SI_SM_HOSED;
  430. }
  431. return SI_SM_CALL_WITHOUT_DELAY;
  432. }
  433. static int kcs_size(void)
  434. {
  435. return sizeof(struct si_sm_data);
  436. }
  437. static int kcs_detect(struct si_sm_data *kcs)
  438. {
  439. /* It's impossible for the KCS status register to be all 1's,
  440. (assuming a properly functioning, self-initialized BMC)
  441. but that's what you get from reading a bogus address, so we
  442. test that first. */
  443. if (read_status(kcs) == 0xff)
  444. return 1;
  445. return 0;
  446. }
  447. static void kcs_cleanup(struct si_sm_data *kcs)
  448. {
  449. }
  450. struct si_sm_handlers kcs_smi_handlers =
  451. {
  452. .init_data = init_kcs_data,
  453. .start_transaction = start_kcs_transaction,
  454. .get_result = get_kcs_result,
  455. .event = kcs_event,
  456. .detect = kcs_detect,
  457. .cleanup = kcs_cleanup,
  458. .size = kcs_size,
  459. };