ipmi_kcs_sm.c 13 KB

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