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 #define POLLING_DELAY_MIN 4 /* Waits are 2-3 usecs on typical systems */
52 #define POLLING_DELAY_MAX 256
54 static void kcs_clear_obf(struct ipmi_softc *, int);
55 static void kcs_error(struct ipmi_softc *);
56 static int kcs_wait_for_ibf(struct ipmi_softc *, bool);
57 static int kcs_wait_for_obf(struct ipmi_softc *, bool);
60 kcs_wait(struct ipmi_softc *sc, int value, int mask)
62 int status, start = ticks;
63 int delay_usec = POLLING_DELAY_MIN;
65 status = INB(sc, KCS_CTL_STS);
66 while (ticks - start < MAX_TIMEOUT && (status & mask) != value) {
68 * The wait delay is increased exponentially to avoid putting
69 * significant load on I/O bus.
72 status = INB(sc, KCS_CTL_STS);
73 if (delay_usec < POLLING_DELAY_MAX)
80 kcs_wait_for_ibf(struct ipmi_softc *sc, bool level)
83 return (kcs_wait(sc, level ? KCS_STATUS_IBF : 0, KCS_STATUS_IBF));
87 kcs_wait_for_obf(struct ipmi_softc *sc, bool level)
90 return (kcs_wait(sc, level ? KCS_STATUS_OBF : 0, KCS_STATUS_OBF));
94 kcs_clear_obf(struct ipmi_softc *sc, int status)
99 if (status & KCS_STATUS_OBF) {
100 data = INB(sc, KCS_DATA);
105 kcs_error(struct ipmi_softc *sc)
110 for (retry = 0; retry < 2; retry++) {
112 /* Wait for IBF = 0 */
113 status = kcs_wait_for_ibf(sc, 0);
116 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
118 /* Wait for IBF = 0 */
119 status = kcs_wait_for_ibf(sc, 0);
122 kcs_clear_obf(sc, status);
124 if (status & KCS_STATUS_OBF) {
125 data = INB(sc, KCS_DATA);
127 device_printf(sc->ipmi_dev,
128 "KCS Error Data %02x\n", data);
131 /* 0x00 to DATA_IN */
132 OUTB(sc, KCS_DATA, 0x00);
134 /* Wait for IBF = 0 */
135 status = kcs_wait_for_ibf(sc, 0);
137 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
139 /* Wait for OBF = 1 */
140 status = kcs_wait_for_obf(sc, 1);
142 /* Read error status */
143 data = INB(sc, KCS_DATA);
144 if (data != 0 && (data != 0xff || bootverbose))
145 device_printf(sc->ipmi_dev, "KCS error: %02x\n",
148 /* Write READ into Data_in */
149 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
151 /* Wait for IBF = 0 */
152 status = kcs_wait_for_ibf(sc, 0);
156 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
157 /* Wait for OBF = 1 */
158 status = kcs_wait_for_obf(sc, 1);
161 kcs_clear_obf(sc, status);
165 device_printf(sc->ipmi_dev, "KCS: Error retry exhausted\n");
169 * Start to write a request. Waits for IBF to clear and then sends the
173 kcs_start_write(struct ipmi_softc *sc)
177 for (retry = 0; retry < 10; retry++) {
178 /* Wait for IBF = 0 */
179 status = kcs_wait_for_ibf(sc, 0);
180 if (status & KCS_STATUS_IBF)
184 kcs_clear_obf(sc, status);
186 /* Write start to command */
187 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_START);
189 /* Wait for IBF = 0 */
190 status = kcs_wait_for_ibf(sc, 0);
191 if (status & KCS_STATUS_IBF)
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);
223 if (status & KCS_STATUS_IBF)
226 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
230 kcs_clear_obf(sc, status);
235 * Write the last byte of a request message.
238 kcs_write_last_byte(struct ipmi_softc *sc, u_char data)
242 /* Write end to command */
243 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_END);
245 /* Wait for IBF = 0 */
246 status = kcs_wait_for_ibf(sc, 0);
247 if (status & KCS_STATUS_IBF)
250 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE)
255 kcs_clear_obf(sc, status);
257 /* Send data byte to DATA. */
258 OUTB(sc, KCS_DATA, data);
263 * Read one byte of the reply message.
266 kcs_read_byte(struct ipmi_softc *sc, u_char *data)
271 /* Wait for IBF = 0 */
272 status = kcs_wait_for_ibf(sc, 0);
275 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
277 /* Wait for OBF = 1 */
278 status = kcs_wait_for_obf(sc, 1);
279 if ((status & KCS_STATUS_OBF) == 0)
283 *data = INB(sc, KCS_DATA);
285 /* Write READ into Data_in */
286 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
291 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
293 /* Wait for OBF = 1*/
294 status = kcs_wait_for_obf(sc, 1);
295 if ((status & KCS_STATUS_OBF) == 0)
299 dummy = INB(sc, KCS_DATA);
308 * Send a request message and collect the reply. Returns true if we
312 kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
319 /* Send the request. */
320 if (!kcs_start_write(sc)) {
321 device_printf(sc->ipmi_dev, "KCS: Failed to start write\n");
325 device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n");
328 if (!kcs_write_byte(sc, req->ir_addr)) {
329 device_printf(sc->ipmi_dev, "KCS: Failed to write address\n");
333 device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr);
336 if (req->ir_requestlen == 0) {
337 if (!kcs_write_last_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 if (!kcs_write_byte(sc, req->ir_command)) {
348 device_printf(sc->ipmi_dev,
349 "KCS: Failed to write command\n");
353 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n",
357 cp = req->ir_request;
358 for (i = 0; i < req->ir_requestlen - 1; i++) {
359 if (!kcs_write_byte(sc, *cp++)) {
360 device_printf(sc->ipmi_dev,
361 "KCS: Failed to write data byte %d\n",
366 device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n",
371 if (!kcs_write_last_byte(sc, *cp)) {
372 device_printf(sc->ipmi_dev,
373 "KCS: Failed to write last dta byte\n");
377 device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n",
382 /* Read the reply. First, read the NetFn/LUN. */
383 if (kcs_read_byte(sc, &data) != 1) {
384 device_printf(sc->ipmi_dev, "KCS: Failed to read address\n");
388 device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data);
390 if (data != IPMI_REPLY_ADDR(req->ir_addr)) {
391 device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n");
395 /* Next we read the command. */
396 if (kcs_read_byte(sc, &data) != 1) {
397 device_printf(sc->ipmi_dev, "KCS: Failed to read command\n");
401 device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data);
403 if (data != req->ir_command) {
404 device_printf(sc->ipmi_dev, "KCS: Command mismatch\n");
408 /* Next we read the completion code. */
409 if (kcs_read_byte(sc, &req->ir_compcode) != 1) {
411 device_printf(sc->ipmi_dev,
412 "KCS: Failed to read completion code\n");
417 device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n",
421 /* Finally, read the reply from the BMC. */
424 state = kcs_read_byte(sc, &data);
426 device_printf(sc->ipmi_dev,
427 "KCS: Read failed on byte %d\n", i + 1);
432 if (i < req->ir_replybuflen) {
433 req->ir_reply[i] = data;
435 device_printf(sc->ipmi_dev, "KCS: Read data %02x\n",
438 device_printf(sc->ipmi_dev,
439 "KCS: Read short %02x byte %d\n", data, i + 1);
445 req->ir_replylen = i;
447 device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i);
448 if (req->ir_replybuflen < i)
450 if (req->ir_replybuflen < i && req->ir_replybuflen != 0)
452 device_printf(sc->ipmi_dev,
453 "KCS: Read short: %zd buffer, %d actual\n",
454 req->ir_replybuflen, i);
465 struct ipmi_softc *sc = arg;
466 struct ipmi_request *req;
470 while ((req = ipmi_dequeue_request(sc)) != NULL) {
473 for (i = 0; i < 3 && !ok; i++)
474 ok = kcs_polled_request(sc, req);
480 ipmi_complete_request(sc, req);
487 kcs_startup(struct ipmi_softc *sc)
490 return (kproc_create(kcs_loop, sc, &sc->ipmi_kthread, 0, 0, "%s: kcs",
491 device_get_nameunit(sc->ipmi_dev)));
495 kcs_driver_request(struct ipmi_softc *sc, struct ipmi_request *req, int timo)
500 for (i = 0; i < 3 && !ok; i++)
501 ok = kcs_polled_request(sc, req);
506 return (req->ir_error);
510 ipmi_kcs_attach(struct ipmi_softc *sc)
514 /* Setup function pointers. */
515 sc->ipmi_startup = kcs_startup;
516 sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;
517 sc->ipmi_driver_request = kcs_driver_request;
518 sc->ipmi_driver_requests_polled = 1;
520 /* See if we can talk to the controller. */
521 status = INB(sc, KCS_CTL_STS);
522 if (status == 0xff) {
523 device_printf(sc->ipmi_dev, "couldn't find it\n");
528 device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status);
530 if (status & KCS_STATUS_OBF ||
531 KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE)
538 * Determine the alignment automatically for a PCI attachment. In this case,
539 * any unused bytes will return 0x00 when read. We make use of the C/D bit
540 * in the CTL_STS register to try to start a GET_STATUS transaction. When
541 * we write the command, that bit should be set, so we should get a non-zero
542 * value back when we read CTL_STS if the offset we are testing is the CTL_STS
546 ipmi_kcs_probe_align(struct ipmi_softc *sc)
550 sc->ipmi_io_spacing = 1;
553 device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing);
556 /* Wait for IBF = 0 */
557 status = INB(sc, KCS_CTL_STS);
558 while (status & KCS_STATUS_IBF) {
560 status = INB(sc, KCS_CTL_STS);
563 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT);
565 /* Wait for IBF = 0 */
566 status = INB(sc, KCS_CTL_STS);
567 while (status & KCS_STATUS_IBF) {
569 status = INB(sc, KCS_CTL_STS);
572 /* If we got 0x00 back, then this must not be the CTL_STS register. */
577 sc->ipmi_io_spacing <<= 1;
578 if (sc->ipmi_io_spacing > 4)
586 /* Finish out the transaction. */
589 if (status & KCS_STATUS_OBF)
590 data = INB(sc, KCS_DATA);
592 /* 0x00 to DATA_IN */
593 OUTB(sc, KCS_DATA, 0);
595 /* Wait for IBF = 0 */
596 status = INB(sc, KCS_CTL_STS);
597 while (status & KCS_STATUS_IBF) {
599 status = INB(sc, KCS_CTL_STS);
602 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) {
603 /* Wait for IBF = 1 */
604 while (!(status & KCS_STATUS_OBF)) {
606 status = INB(sc, KCS_CTL_STS);
609 /* Read error status. */
610 data = INB(sc, KCS_DATA);
612 /* Write dummy READ to DATA_IN. */
613 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ);
615 /* Wait for IBF = 0 */
616 status = INB(sc, KCS_CTL_STS);
617 while (status & KCS_STATUS_IBF) {
619 status = INB(sc, KCS_CTL_STS);
623 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) {
624 /* Wait for IBF = 1 */
625 while (!(status & KCS_STATUS_OBF)) {
627 status = INB(sc, KCS_CTL_STS);
631 if (status & KCS_STATUS_OBF)
632 data = INB(sc, KCS_DATA);
634 device_printf(sc->ipmi_dev, "KCS probe: end state %x\n",
635 KCS_STATUS_STATE(status));