4 * The state-machine driver for an IPMI SMIC driver
6 * It started as a copy of Corey Minyard's driver for the KSC interface
7 * and the kernel patch "mmcdev-patch-245" by HP
9 * modified by: Hannes Schulz <schulz@schwaar.com>
13 * Corey Minyard's driver for the KSC interface has the following
15 * Copyright 2002 MontaVista Software Inc.
17 * the kernel patch "mmcdev-patch-245" by HP has the following
19 * (c) Copyright 2001 Grant Grundler (c) Copyright
20 * 2001 Hewlett-Packard Company
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the
25 * Free Software Foundation; either version 2 of the License, or (at your
26 * option) any later version.
29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
35 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
37 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 675 Mass Ave, Cambridge, MA 02139, USA. */
44 #include <linux/kernel.h> /* For printk. */
45 #include <linux/string.h>
46 #include <linux/ipmi_msgdefs.h> /* for completion codes */
47 #include "ipmi_si_sm.h"
49 #define IPMI_SMIC_VERSION "v33"
51 /* smic_debug is a bit-field
52 * SMIC_DEBUG_ENABLE - turned on for now
53 * SMIC_DEBUG_MSG - commands and their responses
54 * SMIC_DEBUG_STATES - state machine
56 #define SMIC_DEBUG_STATES 4
57 #define SMIC_DEBUG_MSG 2
58 #define SMIC_DEBUG_ENABLE 1
60 static int smic_debug = 1;
76 #define MAX_SMIC_READ_SIZE 80
77 #define MAX_SMIC_WRITE_SIZE 80
78 #define SMIC_MAX_ERROR_RETRIES 3
80 /* Timeouts in microseconds. */
81 #define SMIC_RETRY_TIMEOUT 100000
83 /* SMIC Flags Register Bits */
84 #define SMIC_RX_DATA_READY 0x80
85 #define SMIC_TX_DATA_READY 0x40
87 #define SMIC_EVM_DATA_AVAIL 0x08
88 #define SMIC_SMS_DATA_AVAIL 0x04
89 #define SMIC_FLAG_BSY 0x01
91 /* SMIC Error Codes */
92 #define EC_NO_ERROR 0x00
93 #define EC_ABORTED 0x01
94 #define EC_ILLEGAL_CONTROL 0x02
95 #define EC_NO_RESPONSE 0x03
96 #define EC_ILLEGAL_COMMAND 0x04
97 #define EC_BUFFER_FULL 0x05
101 enum smic_states state;
103 unsigned char write_data[MAX_SMIC_WRITE_SIZE];
106 int orig_write_count;
107 unsigned char read_data[MAX_SMIC_READ_SIZE];
110 unsigned int error_retries;
114 static unsigned int init_smic_data (struct si_sm_data *smic,
117 smic->state = SMIC_IDLE;
120 smic->write_count = 0;
121 smic->orig_write_count = 0;
123 smic->error_retries = 0;
125 smic->smic_timeout = SMIC_RETRY_TIMEOUT;
127 /* We use 3 bytes of I/O. */
131 static int start_smic_transaction(struct si_sm_data *smic,
132 unsigned char *data, unsigned int size)
136 if ((size < 2) || (size > MAX_SMIC_WRITE_SIZE)) {
139 if ((smic->state != SMIC_IDLE) && (smic->state != SMIC_HOSED)) {
142 if (smic_debug & SMIC_DEBUG_MSG) {
143 printk(KERN_INFO "start_smic_transaction -");
144 for (i = 0; i < size; i ++) {
145 printk (" %02x", (unsigned char) (data [i]));
149 smic->error_retries = 0;
150 memcpy(smic->write_data, data, size);
151 smic->write_count = size;
152 smic->orig_write_count = size;
155 smic->state = SMIC_START_OP;
156 smic->smic_timeout = SMIC_RETRY_TIMEOUT;
160 static int smic_get_result(struct si_sm_data *smic,
161 unsigned char *data, unsigned int length)
165 if (smic_debug & SMIC_DEBUG_MSG) {
166 printk (KERN_INFO "smic_get result -");
167 for (i = 0; i < smic->read_pos; i ++) {
168 printk (" %02x", (smic->read_data [i]));
172 if (length < smic->read_pos) {
173 smic->read_pos = length;
176 memcpy(data, smic->read_data, smic->read_pos);
178 if ((length >= 3) && (smic->read_pos < 3)) {
179 data[2] = IPMI_ERR_UNSPECIFIED;
182 if (smic->truncated) {
183 data[2] = IPMI_ERR_MSG_TRUNCATED;
186 return smic->read_pos;
189 static inline unsigned char read_smic_flags(struct si_sm_data *smic)
191 return smic->io->inputb(smic->io, 2);
194 static inline unsigned char read_smic_status(struct si_sm_data *smic)
196 return smic->io->inputb(smic->io, 1);
199 static inline unsigned char read_smic_data(struct si_sm_data *smic)
201 return smic->io->inputb(smic->io, 0);
204 static inline void write_smic_flags(struct si_sm_data *smic,
207 smic->io->outputb(smic->io, 2, flags);
210 static inline void write_smic_control(struct si_sm_data *smic,
211 unsigned char control)
213 smic->io->outputb(smic->io, 1, control);
216 static inline void write_si_sm_data (struct si_sm_data *smic,
219 smic->io->outputb(smic->io, 0, data);
222 static inline void start_error_recovery(struct si_sm_data *smic, char *reason)
224 (smic->error_retries)++;
225 if (smic->error_retries > SMIC_MAX_ERROR_RETRIES) {
226 if (smic_debug & SMIC_DEBUG_ENABLE) {
228 "ipmi_smic_drv: smic hosed: %s\n", reason);
230 smic->state = SMIC_HOSED;
232 smic->write_count = smic->orig_write_count;
235 smic->state = SMIC_START_OP;
236 smic->smic_timeout = SMIC_RETRY_TIMEOUT;
240 static inline void write_next_byte(struct si_sm_data *smic)
242 write_si_sm_data(smic, smic->write_data[smic->write_pos]);
244 (smic->write_count)--;
247 static inline void read_next_byte (struct si_sm_data *smic)
249 if (smic->read_pos >= MAX_SMIC_READ_SIZE) {
250 read_smic_data (smic);
253 smic->read_data[smic->read_pos] = read_smic_data(smic);
258 /* SMIC Control/Status Code Components */
259 #define SMIC_GET_STATUS 0x00 /* Control form's name */
260 #define SMIC_READY 0x00 /* Status form's name */
261 #define SMIC_WR_START 0x01 /* Unified Control/Status names... */
262 #define SMIC_WR_NEXT 0x02
263 #define SMIC_WR_END 0x03
264 #define SMIC_RD_START 0x04
265 #define SMIC_RD_NEXT 0x05
266 #define SMIC_RD_END 0x06
267 #define SMIC_CODE_MASK 0x0f
269 #define SMIC_CONTROL 0x00
270 #define SMIC_STATUS 0x80
271 #define SMIC_CS_MASK 0x80
273 #define SMIC_SMS 0x40
274 #define SMIC_SMM 0x60
275 #define SMIC_STREAM_MASK 0x60
277 /* SMIC Control Codes */
278 #define SMIC_CC_SMS_GET_STATUS (SMIC_CONTROL|SMIC_SMS|SMIC_GET_STATUS)
279 #define SMIC_CC_SMS_WR_START (SMIC_CONTROL|SMIC_SMS|SMIC_WR_START)
280 #define SMIC_CC_SMS_WR_NEXT (SMIC_CONTROL|SMIC_SMS|SMIC_WR_NEXT)
281 #define SMIC_CC_SMS_WR_END (SMIC_CONTROL|SMIC_SMS|SMIC_WR_END)
282 #define SMIC_CC_SMS_RD_START (SMIC_CONTROL|SMIC_SMS|SMIC_RD_START)
283 #define SMIC_CC_SMS_RD_NEXT (SMIC_CONTROL|SMIC_SMS|SMIC_RD_NEXT)
284 #define SMIC_CC_SMS_RD_END (SMIC_CONTROL|SMIC_SMS|SMIC_RD_END)
286 #define SMIC_CC_SMM_GET_STATUS (SMIC_CONTROL|SMIC_SMM|SMIC_GET_STATUS)
287 #define SMIC_CC_SMM_WR_START (SMIC_CONTROL|SMIC_SMM|SMIC_WR_START)
288 #define SMIC_CC_SMM_WR_NEXT (SMIC_CONTROL|SMIC_SMM|SMIC_WR_NEXT)
289 #define SMIC_CC_SMM_WR_END (SMIC_CONTROL|SMIC_SMM|SMIC_WR_END)
290 #define SMIC_CC_SMM_RD_START (SMIC_CONTROL|SMIC_SMM|SMIC_RD_START)
291 #define SMIC_CC_SMM_RD_NEXT (SMIC_CONTROL|SMIC_SMM|SMIC_RD_NEXT)
292 #define SMIC_CC_SMM_RD_END (SMIC_CONTROL|SMIC_SMM|SMIC_RD_END)
294 /* SMIC Status Codes */
295 #define SMIC_SC_SMS_READY (SMIC_STATUS|SMIC_SMS|SMIC_READY)
296 #define SMIC_SC_SMS_WR_START (SMIC_STATUS|SMIC_SMS|SMIC_WR_START)
297 #define SMIC_SC_SMS_WR_NEXT (SMIC_STATUS|SMIC_SMS|SMIC_WR_NEXT)
298 #define SMIC_SC_SMS_WR_END (SMIC_STATUS|SMIC_SMS|SMIC_WR_END)
299 #define SMIC_SC_SMS_RD_START (SMIC_STATUS|SMIC_SMS|SMIC_RD_START)
300 #define SMIC_SC_SMS_RD_NEXT (SMIC_STATUS|SMIC_SMS|SMIC_RD_NEXT)
301 #define SMIC_SC_SMS_RD_END (SMIC_STATUS|SMIC_SMS|SMIC_RD_END)
303 #define SMIC_SC_SMM_READY (SMIC_STATUS|SMIC_SMM|SMIC_READY)
304 #define SMIC_SC_SMM_WR_START (SMIC_STATUS|SMIC_SMM|SMIC_WR_START)
305 #define SMIC_SC_SMM_WR_NEXT (SMIC_STATUS|SMIC_SMM|SMIC_WR_NEXT)
306 #define SMIC_SC_SMM_WR_END (SMIC_STATUS|SMIC_SMM|SMIC_WR_END)
307 #define SMIC_SC_SMM_RD_START (SMIC_STATUS|SMIC_SMM|SMIC_RD_START)
308 #define SMIC_SC_SMM_RD_NEXT (SMIC_STATUS|SMIC_SMM|SMIC_RD_NEXT)
309 #define SMIC_SC_SMM_RD_END (SMIC_STATUS|SMIC_SMM|SMIC_RD_END)
311 /* these are the control/status codes we actually use
312 SMIC_CC_SMS_GET_STATUS 0x40
313 SMIC_CC_SMS_WR_START 0x41
314 SMIC_CC_SMS_WR_NEXT 0x42
315 SMIC_CC_SMS_WR_END 0x43
316 SMIC_CC_SMS_RD_START 0x44
317 SMIC_CC_SMS_RD_NEXT 0x45
318 SMIC_CC_SMS_RD_END 0x46
320 SMIC_SC_SMS_READY 0xC0
321 SMIC_SC_SMS_WR_START 0xC1
322 SMIC_SC_SMS_WR_NEXT 0xC2
323 SMIC_SC_SMS_WR_END 0xC3
324 SMIC_SC_SMS_RD_START 0xC4
325 SMIC_SC_SMS_RD_NEXT 0xC5
326 SMIC_SC_SMS_RD_END 0xC6
329 static enum si_sm_result smic_event (struct si_sm_data *smic, long time)
331 unsigned char status;
335 if (smic->state == SMIC_HOSED) {
336 init_smic_data(smic, smic->io);
339 if (smic->state != SMIC_IDLE) {
340 if (smic_debug & SMIC_DEBUG_STATES) {
342 "smic_event - smic->smic_timeout = %ld,"
344 smic->smic_timeout, time);
346 /* FIXME: smic_event is sometimes called with time > SMIC_RETRY_TIMEOUT */
347 if (time < SMIC_RETRY_TIMEOUT) {
348 smic->smic_timeout -= time;
349 if (smic->smic_timeout < 0) {
350 start_error_recovery(smic, "smic timed out.");
351 return SI_SM_CALL_WITH_DELAY;
355 flags = read_smic_flags(smic);
356 if (flags & SMIC_FLAG_BSY)
357 return SI_SM_CALL_WITH_DELAY;
359 status = read_smic_status (smic);
360 if (smic_debug & SMIC_DEBUG_STATES)
362 "smic_event - state = %d, flags = 0x%02x,"
363 " status = 0x%02x\n",
364 smic->state, flags, status);
366 switch (smic->state) {
368 /* in IDLE we check for available messages */
369 if (flags & (SMIC_SMI |
370 SMIC_EVM_DATA_AVAIL | SMIC_SMS_DATA_AVAIL))
377 /* sanity check whether smic is really idle */
378 write_smic_control(smic, SMIC_CC_SMS_GET_STATUS);
379 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
380 smic->state = SMIC_OP_OK;
384 if (status != SMIC_SC_SMS_READY) {
385 /* this should not happen */
386 start_error_recovery(smic,
387 "state = SMIC_OP_OK,"
388 " status != SMIC_SC_SMS_READY");
389 return SI_SM_CALL_WITH_DELAY;
391 /* OK so far; smic is idle let us start ... */
392 write_smic_control(smic, SMIC_CC_SMS_WR_START);
393 write_next_byte(smic);
394 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
395 smic->state = SMIC_WRITE_START;
398 case SMIC_WRITE_START:
399 if (status != SMIC_SC_SMS_WR_START) {
400 start_error_recovery(smic,
401 "state = SMIC_WRITE_START, "
402 "status != SMIC_SC_SMS_WR_START");
403 return SI_SM_CALL_WITH_DELAY;
405 /* we must not issue WR_(NEXT|END) unless
406 TX_DATA_READY is set */
407 if (flags & SMIC_TX_DATA_READY) {
408 if (smic->write_count == 1) {
410 write_smic_control(smic, SMIC_CC_SMS_WR_END);
411 smic->state = SMIC_WRITE_END;
413 write_smic_control(smic, SMIC_CC_SMS_WR_NEXT);
414 smic->state = SMIC_WRITE_NEXT;
416 write_next_byte(smic);
417 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
420 return SI_SM_CALL_WITH_DELAY;
424 case SMIC_WRITE_NEXT:
425 if (status != SMIC_SC_SMS_WR_NEXT) {
426 start_error_recovery(smic,
427 "state = SMIC_WRITE_NEXT, "
428 "status != SMIC_SC_SMS_WR_NEXT");
429 return SI_SM_CALL_WITH_DELAY;
431 /* this is the same code as in SMIC_WRITE_START */
432 if (flags & SMIC_TX_DATA_READY) {
433 if (smic->write_count == 1) {
434 write_smic_control(smic, SMIC_CC_SMS_WR_END);
435 smic->state = SMIC_WRITE_END;
438 write_smic_control(smic, SMIC_CC_SMS_WR_NEXT);
439 smic->state = SMIC_WRITE_NEXT;
441 write_next_byte(smic);
442 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
445 return SI_SM_CALL_WITH_DELAY;
450 if (status != SMIC_SC_SMS_WR_END) {
451 start_error_recovery (smic,
452 "state = SMIC_WRITE_END, "
453 "status != SMIC_SC_SMS_WR_END");
454 return SI_SM_CALL_WITH_DELAY;
456 /* data register holds an error code */
457 data = read_smic_data(smic);
459 if (smic_debug & SMIC_DEBUG_ENABLE) {
461 "SMIC_WRITE_END: data = %02x\n", data);
463 start_error_recovery(smic,
464 "state = SMIC_WRITE_END, "
466 return SI_SM_CALL_WITH_DELAY;
468 smic->state = SMIC_WRITE2READ;
472 case SMIC_WRITE2READ:
473 /* we must wait for RX_DATA_READY to be set before we
475 if (flags & SMIC_RX_DATA_READY) {
476 write_smic_control(smic, SMIC_CC_SMS_RD_START);
477 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
478 smic->state = SMIC_READ_START;
480 return SI_SM_CALL_WITH_DELAY;
484 case SMIC_READ_START:
485 if (status != SMIC_SC_SMS_RD_START) {
486 start_error_recovery(smic,
487 "state = SMIC_READ_START, "
488 "status != SMIC_SC_SMS_RD_START");
489 return SI_SM_CALL_WITH_DELAY;
491 if (flags & SMIC_RX_DATA_READY) {
492 read_next_byte(smic);
493 write_smic_control(smic, SMIC_CC_SMS_RD_NEXT);
494 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
495 smic->state = SMIC_READ_NEXT;
497 return SI_SM_CALL_WITH_DELAY;
503 /* smic tells us that this is the last byte to be read
505 case SMIC_SC_SMS_RD_END:
506 read_next_byte(smic);
507 write_smic_control(smic, SMIC_CC_SMS_RD_END);
508 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
509 smic->state = SMIC_READ_END;
511 case SMIC_SC_SMS_RD_NEXT:
512 if (flags & SMIC_RX_DATA_READY) {
513 read_next_byte(smic);
514 write_smic_control(smic, SMIC_CC_SMS_RD_NEXT);
515 write_smic_flags(smic, flags | SMIC_FLAG_BSY);
516 smic->state = SMIC_READ_NEXT;
518 return SI_SM_CALL_WITH_DELAY;
522 start_error_recovery(
524 "state = SMIC_READ_NEXT, "
525 "status != SMIC_SC_SMS_RD_(NEXT|END)");
526 return SI_SM_CALL_WITH_DELAY;
531 if (status != SMIC_SC_SMS_READY) {
532 start_error_recovery(smic,
533 "state = SMIC_READ_END, "
534 "status != SMIC_SC_SMS_READY");
535 return SI_SM_CALL_WITH_DELAY;
537 data = read_smic_data(smic);
538 /* data register holds an error code */
540 if (smic_debug & SMIC_DEBUG_ENABLE) {
542 "SMIC_READ_END: data = %02x\n", data);
544 start_error_recovery(smic,
545 "state = SMIC_READ_END, "
547 return SI_SM_CALL_WITH_DELAY;
549 smic->state = SMIC_IDLE;
550 return SI_SM_TRANSACTION_COMPLETE;
554 init_smic_data(smic, smic->io);
558 if (smic_debug & SMIC_DEBUG_ENABLE) {
559 printk(KERN_WARNING "smic->state = %d\n", smic->state);
560 start_error_recovery(smic, "state = UNKNOWN");
561 return SI_SM_CALL_WITH_DELAY;
564 smic->smic_timeout = SMIC_RETRY_TIMEOUT;
565 return SI_SM_CALL_WITHOUT_DELAY;
568 static int smic_detect(struct si_sm_data *smic)
570 /* It's impossible for the SMIC fnags register to be all 1's,
571 (assuming a properly functioning, self-initialized BMC)
572 but that's what you get from reading a bogus address, so we
574 if (read_smic_flags(smic) == 0xff)
580 static void smic_cleanup(struct si_sm_data *kcs)
584 static int smic_size(void)
586 return sizeof(struct si_sm_data);
589 struct si_sm_handlers smic_smi_handlers =
591 .version = IPMI_SMIC_VERSION,
592 .init_data = init_smic_data,
593 .start_transaction = start_smic_transaction,
594 .get_result = smic_get_result,
596 .detect = smic_detect,
597 .cleanup = smic_cleanup,