2 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: head/sys/dev/ipmi/ipmi_kcs.c 248705 2013-03-25 14:30:34Z melifaro $
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/condvar.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
44 #include <dev/misc/ipmi/ipmivars.h>
47 static void kcs_clear_obf(struct ipmi_softc *, int);
48 static void kcs_error(struct ipmi_softc *);
49 static int kcs_wait_for_ibf(struct ipmi_softc *, int);
50 static int kcs_wait_for_obf(struct ipmi_softc *, int);
53 kcs_wait_for_ibf(struct ipmi_softc *sc, int state)
55 int status, start = ticks;
57 status = INB(sc, KCS_CTL_STS);
59 /* WAIT FOR IBF = 0 */
60 while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_IBF) {
62 status = INB(sc, KCS_CTL_STS);
65 /* WAIT FOR IBF = 1 */
66 while (ticks - start < MAX_TIMEOUT &&
67 !(status & KCS_STATUS_IBF)) {
69 status = INB(sc, KCS_CTL_STS);
76 kcs_wait_for_obf(struct ipmi_softc *sc, int state)
78 int status, start = ticks;
80 status = INB(sc, KCS_CTL_STS);
82 /* WAIT FOR OBF = 0 */
83 while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_OBF) {
85 status = INB(sc, KCS_CTL_STS);
88 /* WAIT FOR OBF = 1 */
89 while (ticks - start < MAX_TIMEOUT &&
90 !(status & KCS_STATUS_OBF)) {
92 status = INB(sc, KCS_CTL_STS);
99 kcs_clear_obf(struct ipmi_softc *sc, int status)
104 if (status & KCS_STATUS_OBF) {
105 data = INB(sc, KCS_DATA);
110 kcs_error(struct ipmi_softc *sc)
115 for (retry = 0; retry < 2; retry++) {
117 /* Wait for IBF = 0 */
118 status = kcs_wait_for_ibf(sc, 0);
121 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
123 /* Wait for IBF = 0 */
124 status = kcs_wait_for_ibf(sc, 0);
127 kcs_clear_obf(sc, status);
129 if (status & KCS_STATUS_OBF) {
130 data = INB(sc, KCS_DATA);
132 device_printf(sc->ipmi_dev,
133 "KCS Error Data %02x\n", data);
136 /* 0x00 to DATA_IN */
137 OUTB(sc, KCS_DATA, 0x00);
139 /* Wait for IBF = 0 */
140 status = kcs_wait_for_ibf(sc, 0);
142 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
144 /* Wait for OBF = 1 */
145 status = kcs_wait_for_obf(sc, 1);
147 /* Read error status */
148 data = INB(sc, KCS_DATA);
150 device_printf(sc->ipmi_dev, "KCS error: %02x\n",
153 /* Write READ into Data_in */
154 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
156 /* Wait for IBF = 0 */
157 status = kcs_wait_for_ibf(sc, 0);
161 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
162 /* Wait for OBF = 1 */
163 status = kcs_wait_for_obf(sc, 1);
166 kcs_clear_obf(sc, status);
170 device_printf(sc->ipmi_dev, "KCS: Error retry exhausted\n");
174 * Start to write a request. Waits for IBF to clear and then sends the
178 kcs_start_write(struct ipmi_softc *sc)
182 for (retry = 0; retry < 10; retry++) {
183 /* Wait for IBF = 0 */
184 status = kcs_wait_for_ibf(sc, 0);
187 kcs_clear_obf(sc, status);
189 /* Write start to command */
190 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_START);
192 /* Wait for IBF = 0 */
193 status = kcs_wait_for_ibf(sc, 0);
194 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_WRITE)
199 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
204 kcs_clear_obf(sc, status);
210 * Write a byte of the request message, excluding the last byte of the
211 * message which requires special handling.
214 kcs_write_byte(struct ipmi_softc *sc, u_char data)
219 OUTB(sc, KCS_DATA, data);
221 /* Wait for IBF = 0 */
222 status = kcs_wait_for_ibf(sc, 0);
224 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
228 kcs_clear_obf(sc, status);
233 * Write the last byte of a request message.
236 kcs_write_last_byte(struct ipmi_softc *sc, u_char data)
240 /* Write end to command */
241 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_END);
243 /* Wait for IBF = 0 */
244 status = kcs_wait_for_ibf(sc, 0);
246 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
251 kcs_clear_obf(sc, status);
253 /* Send data byte to DATA. */
254 OUTB(sc, KCS_DATA, data);
259 * Read one byte of the reply message.
262 kcs_read_byte(struct ipmi_softc *sc, u_char *data)
267 /* Wait for IBF = 0 */
268 status = kcs_wait_for_ibf(sc, 0);
271 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
273 /* Wait for OBF = 1 */
274 status = kcs_wait_for_obf(sc, 1);
277 *data = INB(sc, KCS_DATA);
279 /* Write READ into Data_in */
280 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
285 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
287 /* Wait for OBF = 1*/
288 status = kcs_wait_for_obf(sc, 1);
291 dummy = INB(sc, KCS_DATA);
300 * Send a request message and collect the reply. Returns true if we
304 kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
309 /* Send the request. */
310 if (!kcs_start_write(sc)) {
311 device_printf(sc->ipmi_dev, "KCS: Failed to start write\n");
315 device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n");
318 if (!kcs_write_byte(sc, req->ir_addr)) {
319 device_printf(sc->ipmi_dev, "KCS: Failed to write address\n");
323 device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr);
326 if (req->ir_requestlen == 0) {
327 if (!kcs_write_last_byte(sc, req->ir_command)) {
328 device_printf(sc->ipmi_dev,
329 "KCS: Failed to write command\n");
333 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
337 if (!kcs_write_byte(sc, req->ir_command)) {
338 device_printf(sc->ipmi_dev,
339 "KCS: Failed to write command\n");
343 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
347 cp = req->ir_request;
348 for (i = 0; i < req->ir_requestlen - 1; i++) {
349 if (!kcs_write_byte(sc, *cp++)) {
350 device_printf(sc->ipmi_dev,
351 "KCS: Failed to write data byte %d\n",
356 device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n",
361 if (!kcs_write_last_byte(sc, *cp)) {
362 device_printf(sc->ipmi_dev,
363 "KCS: Failed to write last dta byte\n");
367 device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n",
372 /* Read the reply. First, read the NetFn/LUN. */
373 if (kcs_read_byte(sc, &data) != 1) {
374 device_printf(sc->ipmi_dev, "KCS: Failed to read address\n");
378 device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data);
380 if (data != IPMI_REPLY_ADDR(req->ir_addr)) {
381 device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n");
385 /* Next we read the command. */
386 if (kcs_read_byte(sc, &data) != 1) {
387 device_printf(sc->ipmi_dev, "KCS: Failed to read command\n");
391 device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data);
393 if (data != req->ir_command) {
394 device_printf(sc->ipmi_dev, "KCS: Command mismatch\n");
398 /* Next we read the completion code. */
399 if (kcs_read_byte(sc, &req->ir_compcode) != 1) {
400 device_printf(sc->ipmi_dev,
401 "KCS: Failed to read completion code\n");
405 device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n",
409 /* Finally, read the reply from the BMC. */
412 state = kcs_read_byte(sc, &data);
414 device_printf(sc->ipmi_dev,
415 "KCS: Read failed on byte %d\n", i + 1);
420 if (i < req->ir_replybuflen) {
421 req->ir_reply[i] = data;
423 device_printf(sc->ipmi_dev, "KCS: Read data %02x\n",
426 device_printf(sc->ipmi_dev,
427 "KCS: Read short %02x byte %d\n", data, i + 1);
432 req->ir_replylen = i;
434 device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i);
435 if (req->ir_replybuflen < i)
437 if (req->ir_replybuflen < i && req->ir_replybuflen != 0)
439 device_printf(sc->ipmi_dev,
440 "KCS: Read short: %zd buffer, %d actual\n",
441 req->ir_replybuflen, i);
451 struct ipmi_softc *sc = arg;
452 struct ipmi_request *req;
456 while ((req = ipmi_dequeue_request(sc)) != NULL) {
459 for (i = 0; i < 3 && !ok; i++)
460 ok = kcs_polled_request(sc, req);
466 ipmi_complete_request(sc, req);
473 kcs_startup(struct ipmi_softc *sc)
476 return (kthread_create(kcs_loop, sc, &sc->ipmi_kthread, "%s: kcs",
477 device_get_nameunit(sc->ipmi_dev)));
481 ipmi_kcs_attach(struct ipmi_softc *sc)
485 /* Setup function pointers. */
486 sc->ipmi_startup = kcs_startup;
487 sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
489 /* See if we can talk to the controller. */
490 status = INB(sc, KCS_CTL_STS);
491 if (status == 0xff) {
492 device_printf(sc->ipmi_dev, "couldn't find it\n");
497 device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status);
499 if (status & KCS_STATUS_OBF ||
500 KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE)
507 * Determine the alignment automatically for a PCI attachment. In this case,
508 * any unused bytes will return 0x00 when read. We make use of the C/D bit
509 * in the CTL_STS register to try to start a GET_STATUS transaction. When
510 * we write the command, that bit should be set, so we should get a non-zero
511 * value back when we read CTL_STS if the offset we are testing is the CTL_STS
515 ipmi_kcs_probe_align(struct ipmi_softc *sc)
519 sc->ipmi_io_spacing = 1;
522 device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing);
525 /* Wait for IBF = 0 */
526 status = INB(sc, KCS_CTL_STS);
527 while (status & KCS_STATUS_IBF) {
529 status = INB(sc, KCS_CTL_STS);
532 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
534 /* Wait for IBF = 0 */
535 status = INB(sc, KCS_CTL_STS);
536 while (status & KCS_STATUS_IBF) {
538 status = INB(sc, KCS_CTL_STS);
541 /* If we got 0x00 back, then this must not be the CTL_STS register. */
546 sc->ipmi_io_spacing <<= 1;
547 if (sc->ipmi_io_spacing > 4)
555 /* Finish out the transaction. */
558 if (status & KCS_STATUS_OBF)
559 data = INB(sc, KCS_DATA);
561 /* 0x00 to DATA_IN */
562 OUTB(sc, KCS_DATA, 0);
564 /* Wait for IBF = 0 */
565 status = INB(sc, KCS_CTL_STS);
566 while (status & KCS_STATUS_IBF) {
568 status = INB(sc, KCS_CTL_STS);
571 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
572 /* Wait for IBF = 1 */
573 while (!(status & KCS_STATUS_OBF)) {
575 status = INB(sc, KCS_CTL_STS);
578 /* Read error status. */
579 data = INB(sc, KCS_DATA);
581 /* Write dummy READ to DATA_IN. */
582 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
584 /* Wait for IBF = 0 */
585 status = INB(sc, KCS_CTL_STS);
586 while (status & KCS_STATUS_IBF) {
588 status = INB(sc, KCS_CTL_STS);
592 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
593 /* Wait for IBF = 1 */
594 while (!(status & KCS_STATUS_OBF)) {
596 status = INB(sc, KCS_CTL_STS);
600 if (status & KCS_STATUS_OBF)
601 data = INB(sc, KCS_DATA);
603 device_printf(sc->ipmi_dev, "KCS probe: end state %x\n",
604 KCS_STATUS_STATE(status));