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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/condvar.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
38 #include <sys/selinfo.h>
39 #include <machine/bus.h>
46 #include <dev/ipmi/ipmivars.h>
49 static void kcs_clear_obf(struct ipmi_softc *, int);
50 static void kcs_error(struct ipmi_softc *);
51 static int kcs_wait_for_ibf(struct ipmi_softc *, int);
52 static int kcs_wait_for_obf(struct ipmi_softc *, int);
55 kcs_wait_for_ibf(struct ipmi_softc *sc, int state)
57 int status, start = ticks;
59 status = INB(sc, KCS_CTL_STS);
61 /* WAIT FOR IBF = 0 */
62 while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_IBF) {
64 status = INB(sc, KCS_CTL_STS);
67 /* WAIT FOR IBF = 1 */
68 while (ticks - start < MAX_TIMEOUT &&
69 !(status & KCS_STATUS_IBF)) {
71 status = INB(sc, KCS_CTL_STS);
78 kcs_wait_for_obf(struct ipmi_softc *sc, int state)
80 int status, start = ticks;
82 status = INB(sc, KCS_CTL_STS);
84 /* WAIT FOR OBF = 0 */
85 while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_OBF) {
87 status = INB(sc, KCS_CTL_STS);
90 /* WAIT FOR OBF = 1 */
91 while (ticks - start < MAX_TIMEOUT &&
92 !(status & KCS_STATUS_OBF)) {
94 status = INB(sc, KCS_CTL_STS);
101 kcs_clear_obf(struct ipmi_softc *sc, int status)
106 if (status & KCS_STATUS_OBF) {
107 data = INB(sc, KCS_DATA);
112 kcs_error(struct ipmi_softc *sc)
117 for (retry = 0; retry < 2; retry++) {
119 /* Wait for IBF = 0 */
120 status = kcs_wait_for_ibf(sc, 0);
123 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
125 /* Wait for IBF = 0 */
126 status = kcs_wait_for_ibf(sc, 0);
129 kcs_clear_obf(sc, status);
131 if (status & KCS_STATUS_OBF) {
132 data = INB(sc, KCS_DATA);
134 device_printf(sc->ipmi_dev,
135 "KCS Error Data %02x\n", data);
138 /* 0x00 to DATA_IN */
139 OUTB(sc, KCS_DATA, 0x00);
141 /* Wait for IBF = 0 */
142 status = kcs_wait_for_ibf(sc, 0);
144 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
146 /* Wait for OBF = 1 */
147 status = kcs_wait_for_obf(sc, 1);
149 /* Read error status */
150 data = INB(sc, KCS_DATA);
152 device_printf(sc->ipmi_dev, "KCS error: %02x\n",
155 /* Write READ into Data_in */
156 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
158 /* Wait for IBF = 0 */
159 status = kcs_wait_for_ibf(sc, 0);
163 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
164 /* Wait for OBF = 1 */
165 status = kcs_wait_for_obf(sc, 1);
168 kcs_clear_obf(sc, status);
172 device_printf(sc->ipmi_dev, "KCS: Error retry exhausted\n");
176 * Start to write a request. Waits for IBF to clear and then sends the
180 kcs_start_write(struct ipmi_softc *sc)
184 for (retry = 0; retry < 10; retry++) {
185 /* Wait for IBF = 0 */
186 status = kcs_wait_for_ibf(sc, 0);
187 if (status & KCS_STATUS_IBF)
191 kcs_clear_obf(sc, status);
193 /* Write start to command */
194 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_START);
196 /* Wait for IBF = 0 */
197 status = kcs_wait_for_ibf(sc, 0);
198 if (status & KCS_STATUS_IBF)
201 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_WRITE)
206 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
211 kcs_clear_obf(sc, status);
217 * Write a byte of the request message, excluding the last byte of the
218 * message which requires special handling.
221 kcs_write_byte(struct ipmi_softc *sc, u_char data)
226 OUTB(sc, KCS_DATA, data);
228 /* Wait for IBF = 0 */
229 status = kcs_wait_for_ibf(sc, 0);
230 if (status & KCS_STATUS_IBF)
233 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
237 kcs_clear_obf(sc, status);
242 * Write the last byte of a request message.
245 kcs_write_last_byte(struct ipmi_softc *sc, u_char data)
249 /* Write end to command */
250 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_END);
252 /* Wait for IBF = 0 */
253 status = kcs_wait_for_ibf(sc, 0);
254 if (status & KCS_STATUS_IBF)
257 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
262 kcs_clear_obf(sc, status);
264 /* Send data byte to DATA. */
265 OUTB(sc, KCS_DATA, data);
270 * Read one byte of the reply message.
273 kcs_read_byte(struct ipmi_softc *sc, u_char *data)
278 /* Wait for IBF = 0 */
279 status = kcs_wait_for_ibf(sc, 0);
282 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
284 /* Wait for OBF = 1 */
285 status = kcs_wait_for_obf(sc, 1);
286 if ((status & KCS_STATUS_OBF) == 0)
290 *data = INB(sc, KCS_DATA);
292 /* Write READ into Data_in */
293 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
298 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
300 /* Wait for OBF = 1*/
301 status = kcs_wait_for_obf(sc, 1);
302 if ((status & KCS_STATUS_OBF) == 0)
306 dummy = INB(sc, KCS_DATA);
315 * Send a request message and collect the reply. Returns true if we
319 kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
324 /* Send the request. */
325 if (!kcs_start_write(sc)) {
326 device_printf(sc->ipmi_dev, "KCS: Failed to start write\n");
330 device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n");
333 if (!kcs_write_byte(sc, req->ir_addr)) {
334 device_printf(sc->ipmi_dev, "KCS: Failed to write address\n");
338 device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr);
341 if (req->ir_requestlen == 0) {
342 if (!kcs_write_last_byte(sc, req->ir_command)) {
343 device_printf(sc->ipmi_dev,
344 "KCS: Failed to write command\n");
348 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
352 if (!kcs_write_byte(sc, req->ir_command)) {
353 device_printf(sc->ipmi_dev,
354 "KCS: Failed to write command\n");
358 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
362 cp = req->ir_request;
363 for (i = 0; i < req->ir_requestlen - 1; i++) {
364 if (!kcs_write_byte(sc, *cp++)) {
365 device_printf(sc->ipmi_dev,
366 "KCS: Failed to write data byte %d\n",
371 device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n",
376 if (!kcs_write_last_byte(sc, *cp)) {
377 device_printf(sc->ipmi_dev,
378 "KCS: Failed to write last dta byte\n");
382 device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n",
387 /* Read the reply. First, read the NetFn/LUN. */
388 if (kcs_read_byte(sc, &data) != 1) {
389 device_printf(sc->ipmi_dev, "KCS: Failed to read address\n");
393 device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data);
395 if (data != IPMI_REPLY_ADDR(req->ir_addr)) {
396 device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n");
400 /* Next we read the command. */
401 if (kcs_read_byte(sc, &data) != 1) {
402 device_printf(sc->ipmi_dev, "KCS: Failed to read command\n");
406 device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data);
408 if (data != req->ir_command) {
409 device_printf(sc->ipmi_dev, "KCS: Command mismatch\n");
413 /* Next we read the completion code. */
414 if (kcs_read_byte(sc, &req->ir_compcode) != 1) {
415 device_printf(sc->ipmi_dev,
416 "KCS: Failed to read completion code\n");
420 device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n",
424 /* Finally, read the reply from the BMC. */
427 state = kcs_read_byte(sc, &data);
429 device_printf(sc->ipmi_dev,
430 "KCS: Read failed on byte %d\n", i + 1);
435 if (i < req->ir_replybuflen) {
436 req->ir_reply[i] = data;
438 device_printf(sc->ipmi_dev, "KCS: Read data %02x\n",
441 device_printf(sc->ipmi_dev,
442 "KCS: Read short %02x byte %d\n", data, i + 1);
447 req->ir_replylen = i;
449 device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i);
450 if (req->ir_replybuflen < i)
452 if (req->ir_replybuflen < i && req->ir_replybuflen != 0)
454 device_printf(sc->ipmi_dev,
455 "KCS: Read short: %zd buffer, %d actual\n",
456 req->ir_replybuflen, i);
466 struct ipmi_softc *sc = arg;
467 struct ipmi_request *req;
471 while ((req = ipmi_dequeue_request(sc)) != NULL) {
474 for (i = 0; i < 3 && !ok; i++)
475 ok = kcs_polled_request(sc, req);
481 ipmi_complete_request(sc, req);
488 kcs_startup(struct ipmi_softc *sc)
491 return (kproc_create(kcs_loop, sc, &sc->ipmi_kthread, 0, 0, "%s: kcs",
492 device_get_nameunit(sc->ipmi_dev)));
496 ipmi_kcs_attach(struct ipmi_softc *sc)
500 /* Setup function pointers. */
501 sc->ipmi_startup = kcs_startup;
502 sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
504 /* See if we can talk to the controller. */
505 status = INB(sc, KCS_CTL_STS);
506 if (status == 0xff) {
507 device_printf(sc->ipmi_dev, "couldn't find it\n");
512 device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status);
514 if (status & KCS_STATUS_OBF ||
515 KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE)
522 * Determine the alignment automatically for a PCI attachment. In this case,
523 * any unused bytes will return 0x00 when read. We make use of the C/D bit
524 * in the CTL_STS register to try to start a GET_STATUS transaction. When
525 * we write the command, that bit should be set, so we should get a non-zero
526 * value back when we read CTL_STS if the offset we are testing is the CTL_STS
530 ipmi_kcs_probe_align(struct ipmi_softc *sc)
534 sc->ipmi_io_spacing = 1;
537 device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing);
540 /* Wait for IBF = 0 */
541 status = INB(sc, KCS_CTL_STS);
542 while (status & KCS_STATUS_IBF) {
544 status = INB(sc, KCS_CTL_STS);
547 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
549 /* Wait for IBF = 0 */
550 status = INB(sc, KCS_CTL_STS);
551 while (status & KCS_STATUS_IBF) {
553 status = INB(sc, KCS_CTL_STS);
556 /* If we got 0x00 back, then this must not be the CTL_STS register. */
561 sc->ipmi_io_spacing <<= 1;
562 if (sc->ipmi_io_spacing > 4)
570 /* Finish out the transaction. */
573 if (status & KCS_STATUS_OBF)
574 data = INB(sc, KCS_DATA);
576 /* 0x00 to DATA_IN */
577 OUTB(sc, KCS_DATA, 0);
579 /* Wait for IBF = 0 */
580 status = INB(sc, KCS_CTL_STS);
581 while (status & KCS_STATUS_IBF) {
583 status = INB(sc, KCS_CTL_STS);
586 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
587 /* Wait for IBF = 1 */
588 while (!(status & KCS_STATUS_OBF)) {
590 status = INB(sc, KCS_CTL_STS);
593 /* Read error status. */
594 data = INB(sc, KCS_DATA);
596 /* Write dummy READ to DATA_IN. */
597 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
599 /* Wait for IBF = 0 */
600 status = INB(sc, KCS_CTL_STS);
601 while (status & KCS_STATUS_IBF) {
603 status = INB(sc, KCS_CTL_STS);
607 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
608 /* Wait for IBF = 1 */
609 while (!(status & KCS_STATUS_OBF)) {
611 status = INB(sc, KCS_CTL_STS);
615 if (status & KCS_STATUS_OBF)
616 data = INB(sc, KCS_DATA);
618 device_printf(sc->ipmi_dev, "KCS probe: end state %x\n",
619 KCS_STATUS_STATE(status));