ipmi_kcs_sm.c 12 KB

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