ipmi_kcs_sm.c 12 KB

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