2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/condvar.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
40 #include <sys/selinfo.h>
41 #include <machine/bus.h>
48 #include <dev/ipmi/ipmivars.h>
51 static void kcs_clear_obf(struct ipmi_softc *, int);
52 static void kcs_error(struct ipmi_softc *);
53 static int kcs_wait_for_ibf(struct ipmi_softc *, int);
54 static int kcs_wait_for_obf(struct ipmi_softc *, int);
57 kcs_wait_for_ibf(struct ipmi_softc *sc, int state)
59 int status, start = ticks;
61 status = INB(sc, KCS_CTL_STS);
63 /* WAIT FOR IBF = 0 */
64 while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_IBF) {
66 status = INB(sc, KCS_CTL_STS);
69 /* WAIT FOR IBF = 1 */
70 while (ticks - start < MAX_TIMEOUT &&
71 !(status & KCS_STATUS_IBF)) {
73 status = INB(sc, KCS_CTL_STS);
80 kcs_wait_for_obf(struct ipmi_softc *sc, int state)
82 int status, start = ticks;
84 status = INB(sc, KCS_CTL_STS);
86 /* WAIT FOR OBF = 0 */
87 while (ticks - start < MAX_TIMEOUT && status & KCS_STATUS_OBF) {
89 status = INB(sc, KCS_CTL_STS);
92 /* WAIT FOR OBF = 1 */
93 while (ticks - start < MAX_TIMEOUT &&
94 !(status & KCS_STATUS_OBF)) {
96 status = INB(sc, KCS_CTL_STS);
103 kcs_clear_obf(struct ipmi_softc *sc, int status)
108 if (status & KCS_STATUS_OBF) {
109 data = INB(sc, KCS_DATA);
114 kcs_error(struct ipmi_softc *sc)
119 for (retry = 0; retry < 2; retry++) {
121 /* Wait for IBF = 0 */
122 status = kcs_wait_for_ibf(sc, 0);
125 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
127 /* Wait for IBF = 0 */
128 status = kcs_wait_for_ibf(sc, 0);
131 kcs_clear_obf(sc, status);
133 if (status & KCS_STATUS_OBF) {
134 data = INB(sc, KCS_DATA);
136 device_printf(sc->ipmi_dev,
137 "KCS Error Data %02x\n", data);
140 /* 0x00 to DATA_IN */
141 OUTB(sc, KCS_DATA, 0x00);
143 /* Wait for IBF = 0 */
144 status = kcs_wait_for_ibf(sc, 0);
146 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
148 /* Wait for OBF = 1 */
149 status = kcs_wait_for_obf(sc, 1);
151 /* Read error status */
152 data = INB(sc, KCS_DATA);
154 device_printf(sc->ipmi_dev, "KCS error: %02x\n",
157 /* Write READ into Data_in */
158 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
160 /* Wait for IBF = 0 */
161 status = kcs_wait_for_ibf(sc, 0);
165 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
166 /* Wait for OBF = 1 */
167 status = kcs_wait_for_obf(sc, 1);
170 kcs_clear_obf(sc, status);
174 device_printf(sc->ipmi_dev, "KCS: Error retry exhausted\n");
178 * Start to write a request. Waits for IBF to clear and then sends the
182 kcs_start_write(struct ipmi_softc *sc)
186 for (retry = 0; retry < 10; retry++) {
187 /* Wait for IBF = 0 */
188 status = kcs_wait_for_ibf(sc, 0);
189 if (status & KCS_STATUS_IBF)
193 kcs_clear_obf(sc, status);
195 /* Write start to command */
196 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_START);
198 /* Wait for IBF = 0 */
199 status = kcs_wait_for_ibf(sc, 0);
200 if (status & KCS_STATUS_IBF)
203 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_WRITE)
208 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
213 kcs_clear_obf(sc, status);
219 * Write a byte of the request message, excluding the last byte of the
220 * message which requires special handling.
223 kcs_write_byte(struct ipmi_softc *sc, u_char data)
228 OUTB(sc, KCS_DATA, data);
230 /* Wait for IBF = 0 */
231 status = kcs_wait_for_ibf(sc, 0);
232 if (status & KCS_STATUS_IBF)
235 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
239 kcs_clear_obf(sc, status);
244 * Write the last byte of a request message.
247 kcs_write_last_byte(struct ipmi_softc *sc, u_char data)
251 /* Write end to command */
252 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_END);
254 /* Wait for IBF = 0 */
255 status = kcs_wait_for_ibf(sc, 0);
256 if (status & KCS_STATUS_IBF)
259 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
264 kcs_clear_obf(sc, status);
266 /* Send data byte to DATA. */
267 OUTB(sc, KCS_DATA, data);
272 * Read one byte of the reply message.
275 kcs_read_byte(struct ipmi_softc *sc, u_char *data)
280 /* Wait for IBF = 0 */
281 status = kcs_wait_for_ibf(sc, 0);
284 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
286 /* Wait for OBF = 1 */
287 status = kcs_wait_for_obf(sc, 1);
288 if ((status & KCS_STATUS_OBF) == 0)
292 *data = INB(sc, KCS_DATA);
294 /* Write READ into Data_in */
295 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
300 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
302 /* Wait for OBF = 1*/
303 status = kcs_wait_for_obf(sc, 1);
304 if ((status & KCS_STATUS_OBF) == 0)
308 dummy = INB(sc, KCS_DATA);
317 * Send a request message and collect the reply. Returns true if we
321 kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
328 /* Send the request. */
329 if (!kcs_start_write(sc)) {
330 device_printf(sc->ipmi_dev, "KCS: Failed to start write\n");
334 device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n");
337 if (!kcs_write_byte(sc, req->ir_addr)) {
338 device_printf(sc->ipmi_dev, "KCS: Failed to write address\n");
342 device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr);
345 if (req->ir_requestlen == 0) {
346 if (!kcs_write_last_byte(sc, req->ir_command)) {
347 device_printf(sc->ipmi_dev,
348 "KCS: Failed to write command\n");
352 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
356 if (!kcs_write_byte(sc, req->ir_command)) {
357 device_printf(sc->ipmi_dev,
358 "KCS: Failed to write command\n");
362 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
366 cp = req->ir_request;
367 for (i = 0; i < req->ir_requestlen - 1; i++) {
368 if (!kcs_write_byte(sc, *cp++)) {
369 device_printf(sc->ipmi_dev,
370 "KCS: Failed to write data byte %d\n",
375 device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n",
380 if (!kcs_write_last_byte(sc, *cp)) {
381 device_printf(sc->ipmi_dev,
382 "KCS: Failed to write last dta byte\n");
386 device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n",
391 /* Read the reply. First, read the NetFn/LUN. */
392 if (kcs_read_byte(sc, &data) != 1) {
393 device_printf(sc->ipmi_dev, "KCS: Failed to read address\n");
397 device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data);
399 if (data != IPMI_REPLY_ADDR(req->ir_addr)) {
400 device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n");
404 /* Next we read the command. */
405 if (kcs_read_byte(sc, &data) != 1) {
406 device_printf(sc->ipmi_dev, "KCS: Failed to read command\n");
410 device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data);
412 if (data != req->ir_command) {
413 device_printf(sc->ipmi_dev, "KCS: Command mismatch\n");
417 /* Next we read the completion code. */
418 if (kcs_read_byte(sc, &req->ir_compcode) != 1) {
419 device_printf(sc->ipmi_dev,
420 "KCS: Failed to read completion code\n");
424 device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n",
428 /* Finally, read the reply from the BMC. */
431 state = kcs_read_byte(sc, &data);
433 device_printf(sc->ipmi_dev,
434 "KCS: Read failed on byte %d\n", i + 1);
439 if (i < req->ir_replybuflen) {
440 req->ir_reply[i] = data;
442 device_printf(sc->ipmi_dev, "KCS: Read data %02x\n",
445 device_printf(sc->ipmi_dev,
446 "KCS: Read short %02x byte %d\n", data, i + 1);
452 req->ir_replylen = i;
454 device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i);
455 if (req->ir_replybuflen < i)
457 if (req->ir_replybuflen < i && req->ir_replybuflen != 0)
459 device_printf(sc->ipmi_dev,
460 "KCS: Read short: %zd buffer, %d actual\n",
461 req->ir_replybuflen, i);
472 struct ipmi_softc *sc = arg;
473 struct ipmi_request *req;
477 while ((req = ipmi_dequeue_request(sc)) != NULL) {
480 for (i = 0; i < 3 && !ok; i++)
481 ok = kcs_polled_request(sc, req);
487 ipmi_complete_request(sc, req);
494 kcs_startup(struct ipmi_softc *sc)
497 return (kproc_create(kcs_loop, sc, &sc->ipmi_kthread, 0, 0, "%s: kcs",
498 device_get_nameunit(sc->ipmi_dev)));
502 kcs_driver_request(struct ipmi_softc *sc, struct ipmi_request *req, int timo)
507 for (i = 0; i < 3 && !ok; i++)
508 ok = kcs_polled_request(sc, req);
513 return (req->ir_error);
517 ipmi_kcs_attach(struct ipmi_softc *sc)
521 /* Setup function pointers. */
522 sc->ipmi_startup = kcs_startup;
523 sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
524 sc->ipmi_driver_request = kcs_driver_request;
525 sc->ipmi_driver_requests_polled = 1;
527 /* See if we can talk to the controller. */
528 status = INB(sc, KCS_CTL_STS);
529 if (status == 0xff) {
530 device_printf(sc->ipmi_dev, "couldn't find it\n");
535 device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status);
537 if (status & KCS_STATUS_OBF ||
538 KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE)
545 * Determine the alignment automatically for a PCI attachment. In this case,
546 * any unused bytes will return 0x00 when read. We make use of the C/D bit
547 * in the CTL_STS register to try to start a GET_STATUS transaction. When
548 * we write the command, that bit should be set, so we should get a non-zero
549 * value back when we read CTL_STS if the offset we are testing is the CTL_STS
553 ipmi_kcs_probe_align(struct ipmi_softc *sc)
557 sc->ipmi_io_spacing = 1;
560 device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing);
563 /* Wait for IBF = 0 */
564 status = INB(sc, KCS_CTL_STS);
565 while (status & KCS_STATUS_IBF) {
567 status = INB(sc, KCS_CTL_STS);
570 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
572 /* Wait for IBF = 0 */
573 status = INB(sc, KCS_CTL_STS);
574 while (status & KCS_STATUS_IBF) {
576 status = INB(sc, KCS_CTL_STS);
579 /* If we got 0x00 back, then this must not be the CTL_STS register. */
584 sc->ipmi_io_spacing <<= 1;
585 if (sc->ipmi_io_spacing > 4)
593 /* Finish out the transaction. */
596 if (status & KCS_STATUS_OBF)
597 data = INB(sc, KCS_DATA);
599 /* 0x00 to DATA_IN */
600 OUTB(sc, KCS_DATA, 0);
602 /* Wait for IBF = 0 */
603 status = INB(sc, KCS_CTL_STS);
604 while (status & KCS_STATUS_IBF) {
606 status = INB(sc, KCS_CTL_STS);
609 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
610 /* Wait for IBF = 1 */
611 while (!(status & KCS_STATUS_OBF)) {
613 status = INB(sc, KCS_CTL_STS);
616 /* Read error status. */
617 data = INB(sc, KCS_DATA);
619 /* Write dummy READ to DATA_IN. */
620 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
622 /* Wait for IBF = 0 */
623 status = INB(sc, KCS_CTL_STS);
624 while (status & KCS_STATUS_IBF) {
626 status = INB(sc, KCS_CTL_STS);
630 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
631 /* Wait for IBF = 1 */
632 while (!(status & KCS_STATUS_OBF)) {
634 status = INB(sc, KCS_CTL_STS);
638 if (status & KCS_STATUS_OBF)
639 data = INB(sc, KCS_DATA);
641 device_printf(sc->ipmi_dev, "KCS probe: end state %x\n",
642 KCS_STATUS_STATE(status));