1
2
3
4
5
6
7
8
9
10
11#define DEBUG
12
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/ipmi_msgdefs.h>
18#include "ipmi_si_sm.h"
19
20#define BT_DEBUG_OFF 0
21#define BT_DEBUG_ENABLE 1
22#define BT_DEBUG_MSG 2
23#define BT_DEBUG_STATES 4
24
25
26
27
28
29static int bt_debug;
30
31module_param(bt_debug, int, 0644);
32MODULE_PARM_DESC(bt_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
33
34
35
36
37
38
39
40
41
42#define BT_NORMAL_TIMEOUT 5
43#define BT_NORMAL_RETRY_LIMIT 2
44#define BT_RESET_DELAY 6
45
46
47
48
49
50
51enum bt_states {
52 BT_STATE_IDLE = 0,
53 BT_STATE_XACTION_START,
54 BT_STATE_WRITE_BYTES,
55 BT_STATE_WRITE_CONSUME,
56 BT_STATE_READ_WAIT,
57 BT_STATE_CLEAR_B2H,
58 BT_STATE_READ_BYTES,
59 BT_STATE_RESET1,
60 BT_STATE_RESET2,
61 BT_STATE_RESET3,
62 BT_STATE_RESTART,
63 BT_STATE_PRINTME,
64 BT_STATE_LONG_BUSY
65};
66
67
68
69
70
71
72#define BT_STATE_CHANGE(X, Y) { bt->state = X; return Y; }
73
74#define BT_SI_SM_RETURN(Y) { last_printed = BT_STATE_PRINTME; return Y; }
75
76struct si_sm_data {
77 enum bt_states state;
78 unsigned char seq;
79 struct si_sm_io *io;
80 unsigned char write_data[IPMI_MAX_MSG_LENGTH + 2];
81 int write_count;
82 unsigned char read_data[IPMI_MAX_MSG_LENGTH + 2];
83 int read_count;
84 int truncated;
85 long timeout;
86 int error_retries;
87 int nonzero_status;
88 enum bt_states complete;
89 long BT_CAP_req2rsp;
90 int BT_CAP_retries;
91};
92
93#define BT_CLR_WR_PTR 0x01
94#define BT_CLR_RD_PTR 0x02
95#define BT_H2B_ATN 0x04
96#define BT_B2H_ATN 0x08
97#define BT_SMS_ATN 0x10
98#define BT_OEM0 0x20
99#define BT_H_BUSY 0x40
100#define BT_B_BUSY 0x80
101
102
103
104
105
106
107
108
109
110#define BT_STATUS bt->io->inputb(bt->io, 0)
111#define BT_CONTROL(x) bt->io->outputb(bt->io, 0, x)
112
113#define BMC2HOST bt->io->inputb(bt->io, 1)
114#define HOST2BMC(x) bt->io->outputb(bt->io, 1, x)
115
116#define BT_INTMASK_R bt->io->inputb(bt->io, 2)
117#define BT_INTMASK_W(x) bt->io->outputb(bt->io, 2, x)
118
119
120
121
122
123
124static char *state2txt(unsigned char state)
125{
126 switch (state) {
127 case BT_STATE_IDLE: return("IDLE");
128 case BT_STATE_XACTION_START: return("XACTION");
129 case BT_STATE_WRITE_BYTES: return("WR_BYTES");
130 case BT_STATE_WRITE_CONSUME: return("WR_CONSUME");
131 case BT_STATE_READ_WAIT: return("RD_WAIT");
132 case BT_STATE_CLEAR_B2H: return("CLEAR_B2H");
133 case BT_STATE_READ_BYTES: return("RD_BYTES");
134 case BT_STATE_RESET1: return("RESET1");
135 case BT_STATE_RESET2: return("RESET2");
136 case BT_STATE_RESET3: return("RESET3");
137 case BT_STATE_RESTART: return("RESTART");
138 case BT_STATE_LONG_BUSY: return("LONG_BUSY");
139 }
140 return("BAD STATE");
141}
142#define STATE2TXT state2txt(bt->state)
143
144static char *status2txt(unsigned char status)
145{
146
147
148
149
150
151 static char buf[40];
152
153 strcpy(buf, "[ ");
154 if (status & BT_B_BUSY)
155 strcat(buf, "B_BUSY ");
156 if (status & BT_H_BUSY)
157 strcat(buf, "H_BUSY ");
158 if (status & BT_OEM0)
159 strcat(buf, "OEM0 ");
160 if (status & BT_SMS_ATN)
161 strcat(buf, "SMS ");
162 if (status & BT_B2H_ATN)
163 strcat(buf, "B2H ");
164 if (status & BT_H2B_ATN)
165 strcat(buf, "H2B ");
166 strcat(buf, "]");
167 return buf;
168}
169#define STATUS2TXT status2txt(status)
170
171
172
173static unsigned int bt_init_data(struct si_sm_data *bt, struct si_sm_io *io)
174{
175 memset(bt, 0, sizeof(struct si_sm_data));
176 if (bt->io != io) {
177
178 bt->io = io;
179 bt->seq = 0;
180 }
181 bt->state = BT_STATE_IDLE;
182 bt->complete = BT_STATE_IDLE;
183 bt->BT_CAP_req2rsp = BT_NORMAL_TIMEOUT * USEC_PER_SEC;
184 bt->BT_CAP_retries = BT_NORMAL_RETRY_LIMIT;
185 return 3;
186}
187
188
189
190static void force_result(struct si_sm_data *bt, unsigned char completion_code)
191{
192 bt->read_data[0] = 4;
193 bt->read_data[1] = bt->write_data[1] | 4;
194 bt->read_data[2] = bt->write_data[2];
195 bt->read_data[3] = bt->write_data[3];
196 bt->read_data[4] = completion_code;
197 bt->read_count = 5;
198}
199
200
201
202static int bt_start_transaction(struct si_sm_data *bt,
203 unsigned char *data,
204 unsigned int size)
205{
206 unsigned int i;
207
208 if (size < 2)
209 return IPMI_REQ_LEN_INVALID_ERR;
210 if (size > IPMI_MAX_MSG_LENGTH)
211 return IPMI_REQ_LEN_EXCEEDED_ERR;
212
213 if (bt->state == BT_STATE_LONG_BUSY)
214 return IPMI_NODE_BUSY_ERR;
215
216 if (bt->state != BT_STATE_IDLE) {
217 dev_warn(bt->io->dev, "BT in invalid state %d\n", bt->state);
218 return IPMI_NOT_IN_MY_STATE_ERR;
219 }
220
221 if (bt_debug & BT_DEBUG_MSG) {
222 dev_dbg(bt->io->dev, "+++++++++++++++++ New command\n");
223 dev_dbg(bt->io->dev, "NetFn/LUN CMD [%d data]:", size - 2);
224 for (i = 0; i < size; i ++)
225 pr_cont(" %02x", data[i]);
226 pr_cont("\n");
227 }
228 bt->write_data[0] = size + 1;
229 bt->write_data[1] = *data;
230 bt->write_data[2] = bt->seq++;
231 memcpy(bt->write_data + 3, data + 1, size - 1);
232 bt->write_count = size + 2;
233 bt->error_retries = 0;
234 bt->nonzero_status = 0;
235 bt->truncated = 0;
236 bt->state = BT_STATE_XACTION_START;
237 bt->timeout = bt->BT_CAP_req2rsp;
238 force_result(bt, IPMI_ERR_UNSPECIFIED);
239 return 0;
240}
241
242
243
244
245
246
247static int bt_get_result(struct si_sm_data *bt,
248 unsigned char *data,
249 unsigned int length)
250{
251 int i, msg_len;
252
253 msg_len = bt->read_count - 2;
254 if (msg_len < 3 || msg_len > IPMI_MAX_MSG_LENGTH) {
255 force_result(bt, IPMI_ERR_UNSPECIFIED);
256 msg_len = 3;
257 }
258 data[0] = bt->read_data[1];
259 data[1] = bt->read_data[3];
260 if (length < msg_len || bt->truncated) {
261 data[2] = IPMI_ERR_MSG_TRUNCATED;
262 msg_len = 3;
263 } else
264 memcpy(data + 2, bt->read_data + 4, msg_len - 2);
265
266 if (bt_debug & BT_DEBUG_MSG) {
267 dev_dbg(bt->io->dev, "result %d bytes:", msg_len);
268 for (i = 0; i < msg_len; i++)
269 pr_cont(" %02x", data[i]);
270 pr_cont("\n");
271 }
272 return msg_len;
273}
274
275
276#define BT_BMC_HWRST 0x80
277
278static void reset_flags(struct si_sm_data *bt)
279{
280 if (bt_debug)
281 dev_dbg(bt->io->dev, "flag reset %s\n", status2txt(BT_STATUS));
282 if (BT_STATUS & BT_H_BUSY)
283 BT_CONTROL(BT_H_BUSY);
284 BT_CONTROL(BT_CLR_WR_PTR);
285 BT_CONTROL(BT_SMS_ATN);
286 BT_INTMASK_W(BT_BMC_HWRST);
287}
288
289
290
291
292
293
294static void drain_BMC2HOST(struct si_sm_data *bt)
295{
296 int i, size;
297
298 if (!(BT_STATUS & BT_B2H_ATN))
299 return;
300
301 BT_CONTROL(BT_H_BUSY);
302 BT_CONTROL(BT_B2H_ATN);
303 BT_STATUS;
304 BT_CONTROL(BT_B2H_ATN);
305 BT_CONTROL(BT_CLR_RD_PTR);
306 if (bt_debug)
307 dev_dbg(bt->io->dev, "stale response %s; ",
308 status2txt(BT_STATUS));
309 size = BMC2HOST;
310 for (i = 0; i < size ; i++)
311 BMC2HOST;
312 BT_CONTROL(BT_H_BUSY);
313 if (bt_debug)
314 pr_cont("drained %d bytes\n", size + 1);
315}
316
317static inline void write_all_bytes(struct si_sm_data *bt)
318{
319 int i;
320
321 if (bt_debug & BT_DEBUG_MSG) {
322 dev_dbg(bt->io->dev, "write %d bytes seq=0x%02X",
323 bt->write_count, bt->seq);
324 for (i = 0; i < bt->write_count; i++)
325 pr_cont(" %02x", bt->write_data[i]);
326 pr_cont("\n");
327 }
328 for (i = 0; i < bt->write_count; i++)
329 HOST2BMC(bt->write_data[i]);
330}
331
332static inline int read_all_bytes(struct si_sm_data *bt)
333{
334 unsigned int i;
335
336
337
338
339
340
341 bt->read_data[0] = BMC2HOST;
342 bt->read_count = bt->read_data[0];
343
344 if (bt->read_count < 4 || bt->read_count >= IPMI_MAX_MSG_LENGTH) {
345 if (bt_debug & BT_DEBUG_MSG)
346 dev_dbg(bt->io->dev,
347 "bad raw rsp len=%d\n", bt->read_count);
348 bt->truncated = 1;
349 return 1;
350 }
351 for (i = 1; i <= bt->read_count; i++)
352 bt->read_data[i] = BMC2HOST;
353 bt->read_count++;
354
355 if (bt_debug & BT_DEBUG_MSG) {
356 int max = bt->read_count;
357
358 dev_dbg(bt->io->dev,
359 "got %d bytes seq=0x%02X", max, bt->read_data[2]);
360 if (max > 16)
361 max = 16;
362 for (i = 0; i < max; i++)
363 pr_cont(" %02x", bt->read_data[i]);
364 pr_cont("%s\n", bt->read_count == max ? "" : " ...");
365 }
366
367
368 if ((bt->read_data[3] == bt->write_data[3]) &&
369 (bt->read_data[2] == bt->write_data[2]) &&
370 ((bt->read_data[1] & 0xF8) == (bt->write_data[1] & 0xF8)))
371 return 1;
372
373 if (bt_debug & BT_DEBUG_MSG)
374 dev_dbg(bt->io->dev,
375 "IPMI BT: bad packet: want 0x(%02X, %02X, %02X) got (%02X, %02X, %02X)\n",
376 bt->write_data[1] | 0x04, bt->write_data[2],
377 bt->write_data[3],
378 bt->read_data[1], bt->read_data[2], bt->read_data[3]);
379 return 0;
380}
381
382
383
384static enum si_sm_result error_recovery(struct si_sm_data *bt,
385 unsigned char status,
386 unsigned char cCode)
387{
388 char *reason;
389
390 bt->timeout = bt->BT_CAP_req2rsp;
391
392 switch (cCode) {
393 case IPMI_TIMEOUT_ERR:
394 reason = "timeout";
395 break;
396 default:
397 reason = "internal error";
398 break;
399 }
400
401 dev_warn(bt->io->dev, "IPMI BT: %s in %s %s ",
402 reason, STATE2TXT, STATUS2TXT);
403
404
405
406
407
408 (bt->error_retries)++;
409 if (bt->error_retries < bt->BT_CAP_retries) {
410 pr_cont("%d retries left\n",
411 bt->BT_CAP_retries - bt->error_retries);
412 bt->state = BT_STATE_RESTART;
413 return SI_SM_CALL_WITHOUT_DELAY;
414 }
415
416 dev_warn(bt->io->dev, "failed %d retries, sending error response\n",
417 bt->BT_CAP_retries);
418 if (!bt->nonzero_status)
419 dev_err(bt->io->dev, "stuck, try power cycle\n");
420
421
422 else if (bt->seq <= (unsigned char)(bt->BT_CAP_retries & 0xFF)) {
423 dev_warn(bt->io->dev, "BT reset (takes 5 secs)\n");
424 bt->state = BT_STATE_RESET1;
425 return SI_SM_CALL_WITHOUT_DELAY;
426 }
427
428
429
430
431
432
433 bt->state = BT_STATE_IDLE;
434 switch (cCode) {
435 case IPMI_TIMEOUT_ERR:
436 if (status & BT_B_BUSY) {
437 cCode = IPMI_NODE_BUSY_ERR;
438 bt->state = BT_STATE_LONG_BUSY;
439 }
440 break;
441 default:
442 break;
443 }
444 force_result(bt, cCode);
445 return SI_SM_TRANSACTION_COMPLETE;
446}
447
448
449
450static enum si_sm_result bt_event(struct si_sm_data *bt, long time)
451{
452 unsigned char status;
453 static enum bt_states last_printed = BT_STATE_PRINTME;
454 int i;
455
456 status = BT_STATUS;
457 bt->nonzero_status |= status;
458 if ((bt_debug & BT_DEBUG_STATES) && (bt->state != last_printed)) {
459 dev_dbg(bt->io->dev, "BT: %s %s TO=%ld - %ld\n",
460 STATE2TXT,
461 STATUS2TXT,
462 bt->timeout,
463 time);
464 last_printed = bt->state;
465 }
466
467
468
469
470
471
472
473
474 if ((bt->state < BT_STATE_WRITE_BYTES) && (status & BT_B2H_ATN)) {
475 drain_BMC2HOST(bt);
476 BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
477 }
478
479 if ((bt->state != BT_STATE_IDLE) &&
480 (bt->state < BT_STATE_PRINTME)) {
481
482 bt->timeout -= time;
483 if ((bt->timeout < 0) && (bt->state < BT_STATE_RESET1))
484 return error_recovery(bt,
485 status,
486 IPMI_TIMEOUT_ERR);
487 }
488
489 switch (bt->state) {
490
491
492
493
494
495
496 case BT_STATE_IDLE:
497 if (status & BT_SMS_ATN) {
498 BT_CONTROL(BT_SMS_ATN);
499 return SI_SM_ATTN;
500 }
501
502 if (status & BT_H_BUSY)
503 BT_CONTROL(BT_H_BUSY);
504
505 BT_SI_SM_RETURN(SI_SM_IDLE);
506
507 case BT_STATE_XACTION_START:
508 if (status & (BT_B_BUSY | BT_H2B_ATN))
509 BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
510 if (BT_STATUS & BT_H_BUSY)
511 BT_CONTROL(BT_H_BUSY);
512 BT_STATE_CHANGE(BT_STATE_WRITE_BYTES,
513 SI_SM_CALL_WITHOUT_DELAY);
514
515 case BT_STATE_WRITE_BYTES:
516 if (status & BT_H_BUSY)
517 BT_CONTROL(BT_H_BUSY);
518 BT_CONTROL(BT_CLR_WR_PTR);
519 write_all_bytes(bt);
520 BT_CONTROL(BT_H2B_ATN);
521 BT_STATE_CHANGE(BT_STATE_WRITE_CONSUME,
522 SI_SM_CALL_WITHOUT_DELAY);
523
524 case BT_STATE_WRITE_CONSUME:
525 if (status & (BT_B_BUSY | BT_H2B_ATN))
526 BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
527 BT_STATE_CHANGE(BT_STATE_READ_WAIT,
528 SI_SM_CALL_WITHOUT_DELAY);
529
530
531
532 case BT_STATE_READ_WAIT:
533 if (!(status & BT_B2H_ATN))
534 BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
535 BT_CONTROL(BT_H_BUSY);
536
537
538
539
540
541
542
543 BT_CONTROL(BT_B2H_ATN);
544 BT_STATE_CHANGE(BT_STATE_CLEAR_B2H,
545 SI_SM_CALL_WITHOUT_DELAY);
546
547 case BT_STATE_CLEAR_B2H:
548 if (status & BT_B2H_ATN) {
549
550 BT_CONTROL(BT_B2H_ATN);
551 BT_SI_SM_RETURN(SI_SM_CALL_WITH_DELAY);
552 }
553 BT_STATE_CHANGE(BT_STATE_READ_BYTES,
554 SI_SM_CALL_WITHOUT_DELAY);
555
556 case BT_STATE_READ_BYTES:
557 if (!(status & BT_H_BUSY))
558
559 BT_CONTROL(BT_H_BUSY);
560 BT_CONTROL(BT_CLR_RD_PTR);
561 i = read_all_bytes(bt);
562 BT_CONTROL(BT_H_BUSY);
563 if (!i)
564 BT_STATE_CHANGE(BT_STATE_READ_WAIT,
565 SI_SM_CALL_WITHOUT_DELAY);
566 bt->state = bt->complete;
567 return bt->state == BT_STATE_IDLE ?
568 SI_SM_TRANSACTION_COMPLETE :
569 SI_SM_CALL_WITHOUT_DELAY;
570
571 case BT_STATE_LONG_BUSY:
572 if (!(status & BT_B_BUSY)) {
573 reset_flags(bt);
574 bt_init_data(bt, bt->io);
575 }
576 return SI_SM_CALL_WITH_DELAY;
577
578 case BT_STATE_RESET1:
579 reset_flags(bt);
580 drain_BMC2HOST(bt);
581 BT_STATE_CHANGE(BT_STATE_RESET2,
582 SI_SM_CALL_WITH_DELAY);
583
584 case BT_STATE_RESET2:
585 BT_CONTROL(BT_CLR_WR_PTR);
586 HOST2BMC(3);
587 HOST2BMC(0x18);
588 HOST2BMC(42);
589 HOST2BMC(3);
590 BT_CONTROL(BT_H2B_ATN);
591 bt->timeout = BT_RESET_DELAY * USEC_PER_SEC;
592 BT_STATE_CHANGE(BT_STATE_RESET3,
593 SI_SM_CALL_WITH_DELAY);
594
595 case BT_STATE_RESET3:
596 if (bt->timeout > 0)
597 return SI_SM_CALL_WITH_DELAY;
598 drain_BMC2HOST(bt);
599 BT_STATE_CHANGE(BT_STATE_RESTART,
600 SI_SM_CALL_WITH_DELAY);
601
602 case BT_STATE_RESTART:
603 bt->read_count = 0;
604 bt->nonzero_status = 0;
605 bt->timeout = bt->BT_CAP_req2rsp;
606 BT_STATE_CHANGE(BT_STATE_XACTION_START,
607 SI_SM_CALL_WITH_DELAY);
608
609 default:
610 return error_recovery(bt,
611 status,
612 IPMI_ERR_UNSPECIFIED);
613 }
614 return SI_SM_CALL_WITH_DELAY;
615}
616
617static int bt_detect(struct si_sm_data *bt)
618{
619 unsigned char GetBT_CAP[] = { 0x18, 0x36 };
620 unsigned char BT_CAP[8];
621 enum si_sm_result smi_result;
622 int rv;
623
624
625
626
627
628
629
630
631 if ((BT_STATUS == 0xFF) && (BT_INTMASK_R == 0xFF))
632 return 1;
633 reset_flags(bt);
634
635
636
637
638 rv = bt_start_transaction(bt, GetBT_CAP, sizeof(GetBT_CAP));
639 if (rv) {
640 dev_warn(bt->io->dev,
641 "Can't start capabilities transaction: %d\n", rv);
642 goto out_no_bt_cap;
643 }
644
645 smi_result = SI_SM_CALL_WITHOUT_DELAY;
646 for (;;) {
647 if (smi_result == SI_SM_CALL_WITH_DELAY ||
648 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
649 schedule_timeout_uninterruptible(1);
650 smi_result = bt_event(bt, jiffies_to_usecs(1));
651 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
652 smi_result = bt_event(bt, 0);
653 } else
654 break;
655 }
656
657 rv = bt_get_result(bt, BT_CAP, sizeof(BT_CAP));
658 bt_init_data(bt, bt->io);
659 if (rv < 8) {
660 dev_warn(bt->io->dev, "bt cap response too short: %d\n", rv);
661 goto out_no_bt_cap;
662 }
663
664 if (BT_CAP[2]) {
665 dev_warn(bt->io->dev, "Error fetching bt cap: %x\n", BT_CAP[2]);
666out_no_bt_cap:
667 dev_warn(bt->io->dev, "using default values\n");
668 } else {
669 bt->BT_CAP_req2rsp = BT_CAP[6] * USEC_PER_SEC;
670 bt->BT_CAP_retries = BT_CAP[7];
671 }
672
673 dev_info(bt->io->dev, "req2rsp=%ld secs retries=%d\n",
674 bt->BT_CAP_req2rsp / USEC_PER_SEC, bt->BT_CAP_retries);
675
676 return 0;
677}
678
679static void bt_cleanup(struct si_sm_data *bt)
680{
681}
682
683static int bt_size(void)
684{
685 return sizeof(struct si_sm_data);
686}
687
688const struct si_sm_handlers bt_smi_handlers = {
689 .init_data = bt_init_data,
690 .start_transaction = bt_start_transaction,
691 .get_result = bt_get_result,
692 .event = bt_event,
693 .detect = bt_detect,
694 .cleanup = bt_cleanup,
695 .size = bt_size,
696};
697