blob: 8c25f596808a9a2b50d9f14b9a750b26fc079b74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/*
35 * This state machine is taken from the state machine in the IPMI spec,
36 * pretty much verbatim. If you have questions about the states, see
37 * that document.
38 */
39
40#include <linux/kernel.h> /* For printk. */
Corey Minyardc4edff12005-11-07 00:59:56 -080041#include <linux/module.h>
42#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/string.h>
Corey Minyardc3e7e792005-11-07 01:00:02 -080044#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/ipmi_msgdefs.h> /* for completion codes */
46#include "ipmi_si_sm.h"
47
Corey Minyardc4edff12005-11-07 00:59:56 -080048/* kcs_debug is a bit-field
49 * KCS_DEBUG_ENABLE - turned on for now
50 * KCS_DEBUG_MSG - commands and their responses
51 * KCS_DEBUG_STATES - state machine
52 */
53#define KCS_DEBUG_STATES 4
54#define KCS_DEBUG_MSG 2
55#define KCS_DEBUG_ENABLE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Corey Minyardc4edff12005-11-07 00:59:56 -080057static int kcs_debug;
58module_param(kcs_debug, int, 0644);
59MODULE_PARM_DESC(kcs_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* The states the KCS driver may be in. */
62enum kcs_states {
Corey Minyardc305e3d2008-04-29 01:01:10 -070063 /* The KCS interface is currently doing nothing. */
64 KCS_IDLE,
65
66 /*
67 * We are starting an operation. The data is in the output
68 * buffer, but nothing has been done to the interface yet. This
69 * was added to the state machine in the spec to wait for the
70 * initial IBF.
71 */
72 KCS_START_OP,
73
74 /* We have written a write cmd to the interface. */
75 KCS_WAIT_WRITE_START,
76
77 /* We are writing bytes to the interface. */
78 KCS_WAIT_WRITE,
79
80 /*
81 * We have written the write end cmd to the interface, and
82 * still need to write the last byte.
83 */
84 KCS_WAIT_WRITE_END,
85
86 /* We are waiting to read data from the interface. */
87 KCS_WAIT_READ,
88
89 /*
90 * State to transition to the error handler, this was added to
91 * the state machine in the spec to be sure IBF was there.
92 */
93 KCS_ERROR0,
94
95 /*
96 * First stage error handler, wait for the interface to
97 * respond.
98 */
99 KCS_ERROR1,
100
101 /*
102 * The abort cmd has been written, wait for the interface to
103 * respond.
104 */
105 KCS_ERROR2,
106
107 /*
108 * We wrote some data to the interface, wait for it to switch
109 * to read mode.
110 */
111 KCS_ERROR3,
112
113 /* The hardware failed to follow the state machine. */
114 KCS_HOSED
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
Corey Minyard5a1099b2006-12-06 20:41:16 -0800117#define MAX_KCS_READ_SIZE IPMI_MAX_MSG_LENGTH
118#define MAX_KCS_WRITE_SIZE IPMI_MAX_MSG_LENGTH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* Timeouts in microseconds. */
Xie XiuQiccb33682014-01-24 14:00:51 -0600121#define IBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
122#define OBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define MAX_ERROR_RETRIES 10
Corey Minyardc3e7e792005-11-07 01:00:02 -0800124#define ERROR0_OBF_WAIT_JIFFIES (2*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Corey Minyardc305e3d2008-04-29 01:01:10 -0700126struct si_sm_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 enum kcs_states state;
128 struct si_sm_io *io;
129 unsigned char write_data[MAX_KCS_WRITE_SIZE];
130 int write_pos;
131 int write_count;
132 int orig_write_count;
133 unsigned char read_data[MAX_KCS_READ_SIZE];
134 int read_pos;
135 int truncated;
136
137 unsigned int error_retries;
138 long ibf_timeout;
139 long obf_timeout;
Corey Minyardc3e7e792005-11-07 01:00:02 -0800140 unsigned long error0_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143static unsigned int init_kcs_data(struct si_sm_data *kcs,
144 struct si_sm_io *io)
145{
146 kcs->state = KCS_IDLE;
147 kcs->io = io;
148 kcs->write_pos = 0;
149 kcs->write_count = 0;
150 kcs->orig_write_count = 0;
151 kcs->read_pos = 0;
152 kcs->error_retries = 0;
153 kcs->truncated = 0;
154 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
155 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
156
157 /* Reserve 2 I/O bytes. */
158 return 2;
159}
160
161static inline unsigned char read_status(struct si_sm_data *kcs)
162{
163 return kcs->io->inputb(kcs->io, 1);
164}
165
166static inline unsigned char read_data(struct si_sm_data *kcs)
167{
168 return kcs->io->inputb(kcs->io, 0);
169}
170
171static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
172{
173 kcs->io->outputb(kcs->io, 1, data);
174}
175
176static inline void write_data(struct si_sm_data *kcs, unsigned char data)
177{
178 kcs->io->outputb(kcs->io, 0, data);
179}
180
181/* Control codes. */
182#define KCS_GET_STATUS_ABORT 0x60
183#define KCS_WRITE_START 0x61
184#define KCS_WRITE_END 0x62
185#define KCS_READ_BYTE 0x68
186
187/* Status bits. */
188#define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
189#define KCS_IDLE_STATE 0
190#define KCS_READ_STATE 1
191#define KCS_WRITE_STATE 2
192#define KCS_ERROR_STATE 3
193#define GET_STATUS_ATN(status) ((status) & 0x04)
194#define GET_STATUS_IBF(status) ((status) & 0x02)
195#define GET_STATUS_OBF(status) ((status) & 0x01)
196
197
198static inline void write_next_byte(struct si_sm_data *kcs)
199{
200 write_data(kcs, kcs->write_data[kcs->write_pos]);
201 (kcs->write_pos)++;
202 (kcs->write_count)--;
203}
204
205static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
206{
207 (kcs->error_retries)++;
208 if (kcs->error_retries > MAX_ERROR_RETRIES) {
Corey Minyardc4edff12005-11-07 00:59:56 -0800209 if (kcs_debug & KCS_DEBUG_ENABLE)
Corey Minyardc305e3d2008-04-29 01:01:10 -0700210 printk(KERN_DEBUG "ipmi_kcs_sm: kcs hosed: %s\n",
211 reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 kcs->state = KCS_HOSED;
213 } else {
Corey Minyardc3e7e792005-11-07 01:00:02 -0800214 kcs->error0_timeout = jiffies + ERROR0_OBF_WAIT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 kcs->state = KCS_ERROR0;
216 }
217}
218
219static inline void read_next_byte(struct si_sm_data *kcs)
220{
221 if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
222 /* Throw the data away and mark it truncated. */
223 read_data(kcs);
224 kcs->truncated = 1;
225 } else {
226 kcs->read_data[kcs->read_pos] = read_data(kcs);
227 (kcs->read_pos)++;
228 }
229 write_data(kcs, KCS_READ_BYTE);
230}
231
232static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
233 long time)
234{
235 if (GET_STATUS_IBF(status)) {
236 kcs->ibf_timeout -= time;
237 if (kcs->ibf_timeout < 0) {
238 start_error_recovery(kcs, "IBF not ready in time");
239 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
240 return 1;
241 }
242 return 0;
243 }
244 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
245 return 1;
246}
247
248static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
249 long time)
250{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800251 if (!GET_STATUS_OBF(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 kcs->obf_timeout -= time;
253 if (kcs->obf_timeout < 0) {
Corey Minyardeb6d78e2014-04-14 09:46:52 -0500254 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
255 start_error_recovery(kcs, "OBF not ready in time");
256 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 return 0;
259 }
260 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
261 return 1;
262}
263
264static void clear_obf(struct si_sm_data *kcs, unsigned char status)
265{
266 if (GET_STATUS_OBF(status))
267 read_data(kcs);
268}
269
270static void restart_kcs_transaction(struct si_sm_data *kcs)
271{
272 kcs->write_count = kcs->orig_write_count;
273 kcs->write_pos = 0;
274 kcs->read_pos = 0;
275 kcs->state = KCS_WAIT_WRITE_START;
276 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
277 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
278 write_cmd(kcs, KCS_WRITE_START);
279}
280
281static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
282 unsigned int size)
283{
Corey Minyardc4edff12005-11-07 00:59:56 -0800284 unsigned int i;
285
Corey Minyard4d7cbac2006-12-06 20:41:14 -0800286 if (size < 2)
287 return IPMI_REQ_LEN_INVALID_ERR;
288 if (size > MAX_KCS_WRITE_SIZE)
289 return IPMI_REQ_LEN_EXCEEDED_ERR;
290
291 if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED))
292 return IPMI_NOT_IN_MY_STATE_ERR;
293
Corey Minyardc4edff12005-11-07 00:59:56 -0800294 if (kcs_debug & KCS_DEBUG_MSG) {
295 printk(KERN_DEBUG "start_kcs_transaction -");
Corey Minyardc305e3d2008-04-29 01:01:10 -0700296 for (i = 0; i < size; i++)
Corey Minyardc4edff12005-11-07 00:59:56 -0800297 printk(" %02x", (unsigned char) (data [i]));
Corey Minyardc305e3d2008-04-29 01:01:10 -0700298 printk("\n");
Corey Minyardc4edff12005-11-07 00:59:56 -0800299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 kcs->error_retries = 0;
301 memcpy(kcs->write_data, data, size);
302 kcs->write_count = size;
303 kcs->orig_write_count = size;
304 kcs->write_pos = 0;
305 kcs->read_pos = 0;
306 kcs->state = KCS_START_OP;
307 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
308 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
309 return 0;
310}
311
312static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
313 unsigned int length)
314{
315 if (length < kcs->read_pos) {
316 kcs->read_pos = length;
317 kcs->truncated = 1;
318 }
319
320 memcpy(data, kcs->read_data, kcs->read_pos);
321
322 if ((length >= 3) && (kcs->read_pos < 3)) {
323 /* Guarantee that we return at least 3 bytes, with an
324 error in the third byte if it is too short. */
325 data[2] = IPMI_ERR_UNSPECIFIED;
326 kcs->read_pos = 3;
327 }
328 if (kcs->truncated) {
Corey Minyardc305e3d2008-04-29 01:01:10 -0700329 /*
330 * Report a truncated error. We might overwrite
331 * another error, but that's too bad, the user needs
332 * to know it was truncated.
333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 data[2] = IPMI_ERR_MSG_TRUNCATED;
335 kcs->truncated = 0;
336 }
337
338 return kcs->read_pos;
339}
340
Corey Minyardc305e3d2008-04-29 01:01:10 -0700341/*
342 * This implements the state machine defined in the IPMI manual, see
343 * that for details on how this works. Divide that flowchart into
344 * sections delimited by "Wait for IBF" and this will become clear.
345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
347{
348 unsigned char status;
349 unsigned char state;
350
351 status = read_status(kcs);
352
Corey Minyardc4edff12005-11-07 00:59:56 -0800353 if (kcs_debug & KCS_DEBUG_STATES)
354 printk(KERN_DEBUG "KCS: State = %d, %x\n", kcs->state, status);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* All states wait for ibf, so just do it here. */
357 if (!check_ibf(kcs, status, time))
358 return SI_SM_CALL_WITH_DELAY;
359
360 /* Just about everything looks at the KCS state, so grab that, too. */
361 state = GET_STATUS_STATE(status);
362
363 switch (kcs->state) {
364 case KCS_IDLE:
365 /* If there's and interrupt source, turn it off. */
366 clear_obf(kcs, status);
367
368 if (GET_STATUS_ATN(status))
369 return SI_SM_ATTN;
370 else
371 return SI_SM_IDLE;
372
373 case KCS_START_OP:
Julia Lawalld1da96a2009-12-15 16:47:37 -0800374 if (state != KCS_IDLE_STATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 start_error_recovery(kcs,
376 "State machine not idle at start");
377 break;
378 }
379
380 clear_obf(kcs, status);
381 write_cmd(kcs, KCS_WRITE_START);
382 kcs->state = KCS_WAIT_WRITE_START;
383 break;
384
385 case KCS_WAIT_WRITE_START:
386 if (state != KCS_WRITE_STATE) {
387 start_error_recovery(
388 kcs,
389 "Not in write state at write start");
390 break;
391 }
392 read_data(kcs);
393 if (kcs->write_count == 1) {
394 write_cmd(kcs, KCS_WRITE_END);
395 kcs->state = KCS_WAIT_WRITE_END;
396 } else {
397 write_next_byte(kcs);
398 kcs->state = KCS_WAIT_WRITE;
399 }
400 break;
401
402 case KCS_WAIT_WRITE:
403 if (state != KCS_WRITE_STATE) {
404 start_error_recovery(kcs,
405 "Not in write state for write");
406 break;
407 }
408 clear_obf(kcs, status);
409 if (kcs->write_count == 1) {
410 write_cmd(kcs, KCS_WRITE_END);
411 kcs->state = KCS_WAIT_WRITE_END;
412 } else {
413 write_next_byte(kcs);
414 }
415 break;
Corey Minyardc305e3d2008-04-29 01:01:10 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 case KCS_WAIT_WRITE_END:
418 if (state != KCS_WRITE_STATE) {
419 start_error_recovery(kcs,
Corey Minyardc305e3d2008-04-29 01:01:10 -0700420 "Not in write state"
421 " for write end");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 }
424 clear_obf(kcs, status);
425 write_next_byte(kcs);
426 kcs->state = KCS_WAIT_READ;
427 break;
428
429 case KCS_WAIT_READ:
430 if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
431 start_error_recovery(
432 kcs,
433 "Not in read or idle in read state");
434 break;
435 }
436
437 if (state == KCS_READ_STATE) {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800438 if (!check_obf(kcs, status, time))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return SI_SM_CALL_WITH_DELAY;
440 read_next_byte(kcs);
441 } else {
Corey Minyardc305e3d2008-04-29 01:01:10 -0700442 /*
443 * We don't implement this exactly like the state
444 * machine in the spec. Some broken hardware
445 * does not write the final dummy byte to the
446 * read register. Thus obf will never go high
447 * here. We just go straight to idle, and we
448 * handle clearing out obf in idle state if it
449 * happens to come in.
450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 clear_obf(kcs, status);
452 kcs->orig_write_count = 0;
453 kcs->state = KCS_IDLE;
454 return SI_SM_TRANSACTION_COMPLETE;
455 }
456 break;
457
458 case KCS_ERROR0:
459 clear_obf(kcs, status);
Corey Minyardc3e7e792005-11-07 01:00:02 -0800460 status = read_status(kcs);
Corey Minyardc305e3d2008-04-29 01:01:10 -0700461 if (GET_STATUS_OBF(status))
462 /* controller isn't responding */
Corey Minyardc3e7e792005-11-07 01:00:02 -0800463 if (time_before(jiffies, kcs->error0_timeout))
464 return SI_SM_CALL_WITH_TICK_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 write_cmd(kcs, KCS_GET_STATUS_ABORT);
466 kcs->state = KCS_ERROR1;
467 break;
468
469 case KCS_ERROR1:
470 clear_obf(kcs, status);
471 write_data(kcs, 0);
472 kcs->state = KCS_ERROR2;
473 break;
Corey Minyardc305e3d2008-04-29 01:01:10 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 case KCS_ERROR2:
476 if (state != KCS_READ_STATE) {
477 start_error_recovery(kcs,
478 "Not in read state for error2");
479 break;
480 }
Corey Minyard8a3628d2006-03-31 02:30:40 -0800481 if (!check_obf(kcs, status, time))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return SI_SM_CALL_WITH_DELAY;
483
484 clear_obf(kcs, status);
485 write_data(kcs, KCS_READ_BYTE);
486 kcs->state = KCS_ERROR3;
487 break;
Corey Minyardc305e3d2008-04-29 01:01:10 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 case KCS_ERROR3:
490 if (state != KCS_IDLE_STATE) {
491 start_error_recovery(kcs,
492 "Not in idle state for error3");
493 break;
494 }
495
Corey Minyard8a3628d2006-03-31 02:30:40 -0800496 if (!check_obf(kcs, status, time))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return SI_SM_CALL_WITH_DELAY;
498
499 clear_obf(kcs, status);
500 if (kcs->orig_write_count) {
501 restart_kcs_transaction(kcs);
502 } else {
503 kcs->state = KCS_IDLE;
504 return SI_SM_TRANSACTION_COMPLETE;
505 }
506 break;
Corey Minyardc305e3d2008-04-29 01:01:10 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 case KCS_HOSED:
509 break;
510 }
511
512 if (kcs->state == KCS_HOSED) {
513 init_kcs_data(kcs, kcs->io);
514 return SI_SM_HOSED;
515 }
516
517 return SI_SM_CALL_WITHOUT_DELAY;
518}
519
520static int kcs_size(void)
521{
522 return sizeof(struct si_sm_data);
523}
524
525static int kcs_detect(struct si_sm_data *kcs)
526{
Corey Minyardc305e3d2008-04-29 01:01:10 -0700527 /*
528 * It's impossible for the KCS status register to be all 1's,
529 * (assuming a properly functioning, self-initialized BMC)
530 * but that's what you get from reading a bogus address, so we
531 * test that first.
532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (read_status(kcs) == 0xff)
534 return 1;
535
536 return 0;
537}
538
539static void kcs_cleanup(struct si_sm_data *kcs)
540{
541}
542
Corey Minyardc305e3d2008-04-29 01:01:10 -0700543struct si_sm_handlers kcs_smi_handlers = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 .init_data = init_kcs_data,
545 .start_transaction = start_kcs_transaction,
546 .get_result = get_kcs_result,
547 .event = kcs_event,
548 .detect = kcs_detect,
549 .cleanup = kcs_cleanup,
550 .size = kcs_size,
551};