0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #define DEBUG
0021
0022 #include <linux/kernel.h> /* For printk. */
0023 #include <linux/module.h>
0024 #include <linux/moduleparam.h>
0025 #include <linux/string.h>
0026 #include <linux/jiffies.h>
0027 #include <linux/ipmi_msgdefs.h> /* for completion codes */
0028 #include "ipmi_si_sm.h"
0029
0030
0031
0032
0033
0034
0035 #define KCS_DEBUG_STATES 4
0036 #define KCS_DEBUG_MSG 2
0037 #define KCS_DEBUG_ENABLE 1
0038
0039 static int kcs_debug;
0040 module_param(kcs_debug, int, 0644);
0041 MODULE_PARM_DESC(kcs_debug, "debug bitmask, 1=enable, 2=messages, 4=states");
0042
0043
0044 enum kcs_states {
0045
0046 KCS_IDLE,
0047
0048
0049
0050
0051
0052
0053
0054 KCS_START_OP,
0055
0056
0057 KCS_WAIT_WRITE_START,
0058
0059
0060 KCS_WAIT_WRITE,
0061
0062
0063
0064
0065
0066 KCS_WAIT_WRITE_END,
0067
0068
0069 KCS_WAIT_READ,
0070
0071
0072
0073
0074
0075 KCS_ERROR0,
0076
0077
0078
0079
0080
0081 KCS_ERROR1,
0082
0083
0084
0085
0086
0087 KCS_ERROR2,
0088
0089
0090
0091
0092
0093 KCS_ERROR3,
0094
0095
0096 KCS_HOSED
0097 };
0098
0099 #define MAX_KCS_READ_SIZE IPMI_MAX_MSG_LENGTH
0100 #define MAX_KCS_WRITE_SIZE IPMI_MAX_MSG_LENGTH
0101
0102
0103 #define IBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
0104 #define OBF_RETRY_TIMEOUT (5*USEC_PER_SEC)
0105 #define MAX_ERROR_RETRIES 10
0106 #define ERROR0_OBF_WAIT_JIFFIES (2*HZ)
0107
0108 struct si_sm_data {
0109 enum kcs_states state;
0110 struct si_sm_io *io;
0111 unsigned char write_data[MAX_KCS_WRITE_SIZE];
0112 int write_pos;
0113 int write_count;
0114 int orig_write_count;
0115 unsigned char read_data[MAX_KCS_READ_SIZE];
0116 int read_pos;
0117 int truncated;
0118
0119 unsigned int error_retries;
0120 long ibf_timeout;
0121 long obf_timeout;
0122 unsigned long error0_timeout;
0123 };
0124
0125 static unsigned int init_kcs_data(struct si_sm_data *kcs,
0126 struct si_sm_io *io)
0127 {
0128 kcs->state = KCS_IDLE;
0129 kcs->io = io;
0130 kcs->write_pos = 0;
0131 kcs->write_count = 0;
0132 kcs->orig_write_count = 0;
0133 kcs->read_pos = 0;
0134 kcs->error_retries = 0;
0135 kcs->truncated = 0;
0136 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
0137 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
0138
0139
0140 return 2;
0141 }
0142
0143 static inline unsigned char read_status(struct si_sm_data *kcs)
0144 {
0145 return kcs->io->inputb(kcs->io, 1);
0146 }
0147
0148 static inline unsigned char read_data(struct si_sm_data *kcs)
0149 {
0150 return kcs->io->inputb(kcs->io, 0);
0151 }
0152
0153 static inline void write_cmd(struct si_sm_data *kcs, unsigned char data)
0154 {
0155 kcs->io->outputb(kcs->io, 1, data);
0156 }
0157
0158 static inline void write_data(struct si_sm_data *kcs, unsigned char data)
0159 {
0160 kcs->io->outputb(kcs->io, 0, data);
0161 }
0162
0163
0164 #define KCS_GET_STATUS_ABORT 0x60
0165 #define KCS_WRITE_START 0x61
0166 #define KCS_WRITE_END 0x62
0167 #define KCS_READ_BYTE 0x68
0168
0169
0170 #define GET_STATUS_STATE(status) (((status) >> 6) & 0x03)
0171 #define KCS_IDLE_STATE 0
0172 #define KCS_READ_STATE 1
0173 #define KCS_WRITE_STATE 2
0174 #define KCS_ERROR_STATE 3
0175 #define GET_STATUS_ATN(status) ((status) & 0x04)
0176 #define GET_STATUS_IBF(status) ((status) & 0x02)
0177 #define GET_STATUS_OBF(status) ((status) & 0x01)
0178
0179
0180 static inline void write_next_byte(struct si_sm_data *kcs)
0181 {
0182 write_data(kcs, kcs->write_data[kcs->write_pos]);
0183 (kcs->write_pos)++;
0184 (kcs->write_count)--;
0185 }
0186
0187 static inline void start_error_recovery(struct si_sm_data *kcs, char *reason)
0188 {
0189 (kcs->error_retries)++;
0190 if (kcs->error_retries > MAX_ERROR_RETRIES) {
0191 if (kcs_debug & KCS_DEBUG_ENABLE)
0192 dev_dbg(kcs->io->dev, "ipmi_kcs_sm: kcs hosed: %s\n",
0193 reason);
0194 kcs->state = KCS_HOSED;
0195 } else {
0196 kcs->error0_timeout = jiffies + ERROR0_OBF_WAIT_JIFFIES;
0197 kcs->state = KCS_ERROR0;
0198 }
0199 }
0200
0201 static inline void read_next_byte(struct si_sm_data *kcs)
0202 {
0203 if (kcs->read_pos >= MAX_KCS_READ_SIZE) {
0204
0205 read_data(kcs);
0206 kcs->truncated = 1;
0207 } else {
0208 kcs->read_data[kcs->read_pos] = read_data(kcs);
0209 (kcs->read_pos)++;
0210 }
0211 write_data(kcs, KCS_READ_BYTE);
0212 }
0213
0214 static inline int check_ibf(struct si_sm_data *kcs, unsigned char status,
0215 long time)
0216 {
0217 if (GET_STATUS_IBF(status)) {
0218 kcs->ibf_timeout -= time;
0219 if (kcs->ibf_timeout < 0) {
0220 start_error_recovery(kcs, "IBF not ready in time");
0221 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
0222 return 1;
0223 }
0224 return 0;
0225 }
0226 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
0227 return 1;
0228 }
0229
0230 static inline int check_obf(struct si_sm_data *kcs, unsigned char status,
0231 long time)
0232 {
0233 if (!GET_STATUS_OBF(status)) {
0234 kcs->obf_timeout -= time;
0235 if (kcs->obf_timeout < 0) {
0236 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
0237 start_error_recovery(kcs, "OBF not ready in time");
0238 return 1;
0239 }
0240 return 0;
0241 }
0242 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
0243 return 1;
0244 }
0245
0246 static void clear_obf(struct si_sm_data *kcs, unsigned char status)
0247 {
0248 if (GET_STATUS_OBF(status))
0249 read_data(kcs);
0250 }
0251
0252 static void restart_kcs_transaction(struct si_sm_data *kcs)
0253 {
0254 kcs->write_count = kcs->orig_write_count;
0255 kcs->write_pos = 0;
0256 kcs->read_pos = 0;
0257 kcs->state = KCS_WAIT_WRITE_START;
0258 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
0259 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
0260 write_cmd(kcs, KCS_WRITE_START);
0261 }
0262
0263 static int start_kcs_transaction(struct si_sm_data *kcs, unsigned char *data,
0264 unsigned int size)
0265 {
0266 unsigned int i;
0267
0268 if (size < 2)
0269 return IPMI_REQ_LEN_INVALID_ERR;
0270 if (size > MAX_KCS_WRITE_SIZE)
0271 return IPMI_REQ_LEN_EXCEEDED_ERR;
0272
0273 if ((kcs->state != KCS_IDLE) && (kcs->state != KCS_HOSED)) {
0274 dev_warn(kcs->io->dev, "KCS in invalid state %d\n", kcs->state);
0275 return IPMI_NOT_IN_MY_STATE_ERR;
0276 }
0277
0278 if (kcs_debug & KCS_DEBUG_MSG) {
0279 dev_dbg(kcs->io->dev, "%s -", __func__);
0280 for (i = 0; i < size; i++)
0281 pr_cont(" %02x", data[i]);
0282 pr_cont("\n");
0283 }
0284 kcs->error_retries = 0;
0285 memcpy(kcs->write_data, data, size);
0286 kcs->write_count = size;
0287 kcs->orig_write_count = size;
0288 kcs->write_pos = 0;
0289 kcs->read_pos = 0;
0290 kcs->state = KCS_START_OP;
0291 kcs->ibf_timeout = IBF_RETRY_TIMEOUT;
0292 kcs->obf_timeout = OBF_RETRY_TIMEOUT;
0293 return 0;
0294 }
0295
0296 static int get_kcs_result(struct si_sm_data *kcs, unsigned char *data,
0297 unsigned int length)
0298 {
0299 if (length < kcs->read_pos) {
0300 kcs->read_pos = length;
0301 kcs->truncated = 1;
0302 }
0303
0304 memcpy(data, kcs->read_data, kcs->read_pos);
0305
0306 if ((length >= 3) && (kcs->read_pos < 3)) {
0307
0308
0309 data[2] = IPMI_ERR_UNSPECIFIED;
0310 kcs->read_pos = 3;
0311 }
0312 if (kcs->truncated) {
0313
0314
0315
0316
0317
0318 data[2] = IPMI_ERR_MSG_TRUNCATED;
0319 kcs->truncated = 0;
0320 }
0321
0322 return kcs->read_pos;
0323 }
0324
0325
0326
0327
0328
0329
0330 static enum si_sm_result kcs_event(struct si_sm_data *kcs, long time)
0331 {
0332 unsigned char status;
0333 unsigned char state;
0334
0335 status = read_status(kcs);
0336
0337 if (kcs_debug & KCS_DEBUG_STATES)
0338 dev_dbg(kcs->io->dev,
0339 "KCS: State = %d, %x\n", kcs->state, status);
0340
0341
0342 if (!check_ibf(kcs, status, time))
0343 return SI_SM_CALL_WITH_DELAY;
0344
0345
0346 state = GET_STATUS_STATE(status);
0347
0348 switch (kcs->state) {
0349 case KCS_IDLE:
0350
0351 clear_obf(kcs, status);
0352
0353 if (GET_STATUS_ATN(status))
0354 return SI_SM_ATTN;
0355 else
0356 return SI_SM_IDLE;
0357
0358 case KCS_START_OP:
0359 if (state != KCS_IDLE_STATE) {
0360 start_error_recovery(kcs,
0361 "State machine not idle at start");
0362 break;
0363 }
0364
0365 clear_obf(kcs, status);
0366 write_cmd(kcs, KCS_WRITE_START);
0367 kcs->state = KCS_WAIT_WRITE_START;
0368 break;
0369
0370 case KCS_WAIT_WRITE_START:
0371 if (state != KCS_WRITE_STATE) {
0372 start_error_recovery(
0373 kcs,
0374 "Not in write state at write start");
0375 break;
0376 }
0377 read_data(kcs);
0378 if (kcs->write_count == 1) {
0379 write_cmd(kcs, KCS_WRITE_END);
0380 kcs->state = KCS_WAIT_WRITE_END;
0381 } else {
0382 write_next_byte(kcs);
0383 kcs->state = KCS_WAIT_WRITE;
0384 }
0385 break;
0386
0387 case KCS_WAIT_WRITE:
0388 if (state != KCS_WRITE_STATE) {
0389 start_error_recovery(kcs,
0390 "Not in write state for write");
0391 break;
0392 }
0393 clear_obf(kcs, status);
0394 if (kcs->write_count == 1) {
0395 write_cmd(kcs, KCS_WRITE_END);
0396 kcs->state = KCS_WAIT_WRITE_END;
0397 } else {
0398 write_next_byte(kcs);
0399 }
0400 break;
0401
0402 case KCS_WAIT_WRITE_END:
0403 if (state != KCS_WRITE_STATE) {
0404 start_error_recovery(kcs,
0405 "Not in write state"
0406 " for write end");
0407 break;
0408 }
0409 clear_obf(kcs, status);
0410 write_next_byte(kcs);
0411 kcs->state = KCS_WAIT_READ;
0412 break;
0413
0414 case KCS_WAIT_READ:
0415 if ((state != KCS_READ_STATE) && (state != KCS_IDLE_STATE)) {
0416 start_error_recovery(
0417 kcs,
0418 "Not in read or idle in read state");
0419 break;
0420 }
0421
0422 if (state == KCS_READ_STATE) {
0423 if (!check_obf(kcs, status, time))
0424 return SI_SM_CALL_WITH_DELAY;
0425 read_next_byte(kcs);
0426 } else {
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436 clear_obf(kcs, status);
0437 kcs->orig_write_count = 0;
0438 kcs->state = KCS_IDLE;
0439 return SI_SM_TRANSACTION_COMPLETE;
0440 }
0441 break;
0442
0443 case KCS_ERROR0:
0444 clear_obf(kcs, status);
0445 status = read_status(kcs);
0446 if (GET_STATUS_OBF(status))
0447
0448 if (time_before(jiffies, kcs->error0_timeout))
0449 return SI_SM_CALL_WITH_TICK_DELAY;
0450 write_cmd(kcs, KCS_GET_STATUS_ABORT);
0451 kcs->state = KCS_ERROR1;
0452 break;
0453
0454 case KCS_ERROR1:
0455 clear_obf(kcs, status);
0456 write_data(kcs, 0);
0457 kcs->state = KCS_ERROR2;
0458 break;
0459
0460 case KCS_ERROR2:
0461 if (state != KCS_READ_STATE) {
0462 start_error_recovery(kcs,
0463 "Not in read state for error2");
0464 break;
0465 }
0466 if (!check_obf(kcs, status, time))
0467 return SI_SM_CALL_WITH_DELAY;
0468
0469 clear_obf(kcs, status);
0470 write_data(kcs, KCS_READ_BYTE);
0471 kcs->state = KCS_ERROR3;
0472 break;
0473
0474 case KCS_ERROR3:
0475 if (state != KCS_IDLE_STATE) {
0476 start_error_recovery(kcs,
0477 "Not in idle state for error3");
0478 break;
0479 }
0480
0481 if (!check_obf(kcs, status, time))
0482 return SI_SM_CALL_WITH_DELAY;
0483
0484 clear_obf(kcs, status);
0485 if (kcs->orig_write_count) {
0486 restart_kcs_transaction(kcs);
0487 } else {
0488 kcs->state = KCS_IDLE;
0489 return SI_SM_TRANSACTION_COMPLETE;
0490 }
0491 break;
0492
0493 case KCS_HOSED:
0494 break;
0495 }
0496
0497 if (kcs->state == KCS_HOSED) {
0498 init_kcs_data(kcs, kcs->io);
0499 return SI_SM_HOSED;
0500 }
0501
0502 return SI_SM_CALL_WITHOUT_DELAY;
0503 }
0504
0505 static int kcs_size(void)
0506 {
0507 return sizeof(struct si_sm_data);
0508 }
0509
0510 static int kcs_detect(struct si_sm_data *kcs)
0511 {
0512
0513
0514
0515
0516
0517
0518 if (read_status(kcs) == 0xff)
0519 return 1;
0520
0521 return 0;
0522 }
0523
0524 static void kcs_cleanup(struct si_sm_data *kcs)
0525 {
0526 }
0527
0528 const struct si_sm_handlers kcs_smi_handlers = {
0529 .init_data = init_kcs_data,
0530 .start_transaction = start_kcs_transaction,
0531 .get_result = get_kcs_result,
0532 .event = kcs_event,
0533 .detect = kcs_detect,
0534 .cleanup = kcs_cleanup,
0535 .size = kcs_size,
0536 };