1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/moduleparam.h>
43#include <linux/string.h>
44#include <linux/jiffies.h>
45#include <linux/ipmi_msgdefs.h>
46#include "ipmi_si_sm.h"
47
48
49
50
51
52
53#define KCS_DEBUG_STATES 4
54#define KCS_DEBUG_MSG 2
55#define KCS_DEBUG_ENABLE 1
56
57static int kcs_debug;
58module_param(kcs_debug, int, 0644);
59MODULE_PARM_DESC(kcs_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
60
61
62enum kcs_states {
63
64 KCS_IDLE,
65
66
67
68
69
70
71
72 KCS_START_OP,
73
74
75 KCS_WAIT_WRITE_START,
76
77
78 KCS_WAIT_WRITE,
79
80
81
82
83
84 KCS_WAIT_WRITE_END,
85
86
87 KCS_WAIT_READ,
88
89
90
91
92
93 KCS_ERROR0,
94
95
96
97
98
99 KCS_ERROR1,
100
101
102
103
104
105 KCS_ERROR2,
106
107
108
109
110
111 KCS_ERROR3,
112
113
114 KCS_HOSED
115};
116
117#define MAX_KCS_READ_SIZE IPMI_MAX_MSG_LENGTH
118#define MAX_KCS_WRITE_SIZE IPMI_MAX_MSG_LENGTH
119
120
121#define IBF_RETRY_TIMEOUT 5000000
122#define OBF_RETRY_TIMEOUT 5000000
123#define MAX_ERROR_RETRIES 10
124#define ERROR0_OBF_WAIT_JIFFIES (2*HZ)
125
126struct si_sm_data {
127 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;
140 unsigned long error0_timeout;
141};
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
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
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
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) {
209 if (kcs_debug & KCS_DEBUG_ENABLE)
210 printk(KERN_DEBUG "ipmi_kcs_sm: kcs hosed: %s\n",
211 reason);
212 kcs->state = KCS_HOSED;
213 } else {
214 kcs->error0_timeout = jiffies + ERROR0_OBF_WAIT_JIFFIES;
215 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
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{
251 if (!GET_STATUS_OBF(status)) {
252 kcs->obf_timeout -= time;
253 if (kcs->obf_timeout < 0) {
254 start_error_recovery(kcs, "OBF not ready in time");
255 return 1;
256 }
257 return 0;
258 }
259 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
260 return 1;
261}
262
263static void clear_obf(struct si_sm_data *kcs, unsigned char status)
264{
265 if (GET_STATUS_OBF(status))
266 read_data(kcs);
267}
268
269static void restart_kcs_transaction(struct si_sm_data *kcs)
270{
271 kcs->write_count = kcs->orig_write_count;
272 kcs->write_pos = 0;
273 kcs->read_pos = 0;
274 kcs->state = KCS_WAIT_WRITE_START;
275 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
276 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
277 write_cmd(kcs, KCS_WRITE_START);
278}
279
280static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
281 unsigned int size)
282{
283 unsigned int i;
284
285 if (size < 2)
286 return IPMI_REQ_LEN_INVALID_ERR;
287 if (size > MAX_KCS_WRITE_SIZE)
288 return IPMI_REQ_LEN_EXCEEDED_ERR;
289
290 if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED))
291 return IPMI_NOT_IN_MY_STATE_ERR;
292
293 if (kcs_debug & KCS_DEBUG_MSG) {
294 printk(KERN_DEBUG "start_kcs_transaction -");
295 for (i = 0; i < size; i++)
296 printk(" %02x", (unsigned char) (data [i]));
297 printk("\n");
298 }
299 kcs->error_retries = 0;
300 memcpy(kcs->write_data, data, size);
301 kcs->write_count = size;
302 kcs->orig_write_count = size;
303 kcs->write_pos = 0;
304 kcs->read_pos = 0;
305 kcs->state = KCS_START_OP;
306 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
307 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
308 return 0;
309}
310
311static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
312 unsigned int length)
313{
314 if (length < kcs->read_pos) {
315 kcs->read_pos = length;
316 kcs->truncated = 1;
317 }
318
319 memcpy(data, kcs->read_data, kcs->read_pos);
320
321 if ((length >= 3) && (kcs->read_pos < 3)) {
322
323
324 data[2] = IPMI_ERR_UNSPECIFIED;
325 kcs->read_pos = 3;
326 }
327 if (kcs->truncated) {
328
329
330
331
332
333 data[2] = IPMI_ERR_MSG_TRUNCATED;
334 kcs->truncated = 0;
335 }
336
337 return kcs->read_pos;
338}
339
340
341
342
343
344
345static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
346{
347 unsigned char status;
348 unsigned char state;
349
350 status = read_status(kcs);
351
352 if (kcs_debug & KCS_DEBUG_STATES)
353 printk(KERN_DEBUG "KCS: State = %d, %x\n", kcs->state, status);
354
355
356 if (!check_ibf(kcs, status, time))
357 return SI_SM_CALL_WITH_DELAY;
358
359
360 state = GET_STATUS_STATE(status);
361
362 switch (kcs->state) {
363 case KCS_IDLE:
364
365 clear_obf(kcs, status);
366
367 if (GET_STATUS_ATN(status))
368 return SI_SM_ATTN;
369 else
370 return SI_SM_IDLE;
371
372 case KCS_START_OP:
373 if (state != KCS_IDLE_STATE) {
374 start_error_recovery(kcs,
375 "State machine not idle at start");
376 break;
377 }
378
379 clear_obf(kcs, status);
380 write_cmd(kcs, KCS_WRITE_START);
381 kcs->state = KCS_WAIT_WRITE_START;
382 break;
383
384 case KCS_WAIT_WRITE_START:
385 if (state != KCS_WRITE_STATE) {
386 start_error_recovery(
387 kcs,
388 "Not in write state at write start");
389 break;
390 }
391 read_data(kcs);
392 if (kcs->write_count == 1) {
393 write_cmd(kcs, KCS_WRITE_END);
394 kcs->state = KCS_WAIT_WRITE_END;
395 } else {
396 write_next_byte(kcs);
397 kcs->state = KCS_WAIT_WRITE;
398 }
399 break;
400
401 case KCS_WAIT_WRITE:
402 if (state != KCS_WRITE_STATE) {
403 start_error_recovery(kcs,
404 "Not in write state for write");
405 break;
406 }
407 clear_obf(kcs, status);
408 if (kcs->write_count == 1) {
409 write_cmd(kcs, KCS_WRITE_END);
410 kcs->state = KCS_WAIT_WRITE_END;
411 } else {
412 write_next_byte(kcs);
413 }
414 break;
415
416 case KCS_WAIT_WRITE_END:
417 if (state != KCS_WRITE_STATE) {
418 start_error_recovery(kcs,
419 "Not in write state"
420 " for write end");
421 break;
422 }
423 clear_obf(kcs, status);
424 write_next_byte(kcs);
425 kcs->state = KCS_WAIT_READ;
426 break;
427
428 case KCS_WAIT_READ:
429 if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
430 start_error_recovery(
431 kcs,
432 "Not in read or idle in read state");
433 break;
434 }
435
436 if (state == KCS_READ_STATE) {
437 if (!check_obf(kcs, status, time))
438 return SI_SM_CALL_WITH_DELAY;
439 read_next_byte(kcs);
440 } else {
441
442
443
444
445
446
447
448
449
450 clear_obf(kcs, status);
451 kcs->orig_write_count = 0;
452 kcs->state = KCS_IDLE;
453 return SI_SM_TRANSACTION_COMPLETE;
454 }
455 break;
456
457 case KCS_ERROR0:
458 clear_obf(kcs, status);
459 status = read_status(kcs);
460 if (GET_STATUS_OBF(status))
461
462 if (time_before(jiffies, kcs->error0_timeout))
463 return SI_SM_CALL_WITH_TICK_DELAY;
464 write_cmd(kcs, KCS_GET_STATUS_ABORT);
465 kcs->state = KCS_ERROR1;
466 break;
467
468 case KCS_ERROR1:
469 clear_obf(kcs, status);
470 write_data(kcs, 0);
471 kcs->state = KCS_ERROR2;
472 break;
473
474 case KCS_ERROR2:
475 if (state != KCS_READ_STATE) {
476 start_error_recovery(kcs,
477 "Not in read state for error2");
478 break;
479 }
480 if (!check_obf(kcs, status, time))
481 return SI_SM_CALL_WITH_DELAY;
482
483 clear_obf(kcs, status);
484 write_data(kcs, KCS_READ_BYTE);
485 kcs->state = KCS_ERROR3;
486 break;
487
488 case KCS_ERROR3:
489 if (state != KCS_IDLE_STATE) {
490 start_error_recovery(kcs,
491 "Not in idle state for error3");
492 break;
493 }
494
495 if (!check_obf(kcs, status, time))
496 return SI_SM_CALL_WITH_DELAY;
497
498 clear_obf(kcs, status);
499 if (kcs->orig_write_count) {
500 restart_kcs_transaction(kcs);
501 } else {
502 kcs->state = KCS_IDLE;
503 return SI_SM_TRANSACTION_COMPLETE;
504 }
505 break;
506
507 case KCS_HOSED:
508 break;
509 }
510
511 if (kcs->state == KCS_HOSED) {
512 init_kcs_data(kcs, kcs->io);
513 return SI_SM_HOSED;
514 }
515
516 return SI_SM_CALL_WITHOUT_DELAY;
517}
518
519static int kcs_size(void)
520{
521 return sizeof(struct si_sm_data);
522}
523
524static int kcs_detect(struct si_sm_data *kcs)
525{
526
527
528
529
530
531
532 if (read_status(kcs) == 0xff)
533 return 1;
534
535 return 0;
536}
537
538static void kcs_cleanup(struct si_sm_data *kcs)
539{
540}
541
542struct si_sm_handlers kcs_smi_handlers = {
543 .init_data = init_kcs_data,
544 .start_transaction = start_kcs_transaction,
545 .get_result = get_kcs_result,
546 .event = kcs_event,
547 .detect = kcs_detect,
548 .cleanup = kcs_cleanup,
549 .size = kcs_size,
550};
551