1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/condvar.h> 36 #include <sys/eventhandler.h> 37 #include <sys/kernel.h> 38 #include <sys/kthread.h> 39 #include <sys/rman.h> 40 #include <sys/selinfo.h> 41 #include <machine/bus.h> 42 43 #ifdef LOCAL_MODULE 44 #include <ipmi.h> 45 #include <ipmivars.h> 46 #else 47 #include <sys/ipmi.h> 48 #include <dev/ipmi/ipmivars.h> 49 #endif 50 51 #define POLLING_DELAY_MIN 4 /* Waits are 2-3 usecs on typical systems */ 52 #define POLLING_DELAY_MAX 256 53 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); 58 59 static int 60 kcs_wait(struct ipmi_softc *sc, int value, int mask) 61 { 62 int status, start = ticks; 63 int delay_usec = POLLING_DELAY_MIN; 64 65 status = INB(sc, KCS_CTL_STS); 66 while (ticks - start < MAX_TIMEOUT && (status & mask) != value) { 67 /* 68 * The wait delay is increased exponentially to avoid putting 69 * significant load on I/O bus. 70 */ 71 DELAY(delay_usec); 72 status = INB(sc, KCS_CTL_STS); 73 if (delay_usec < POLLING_DELAY_MAX) 74 delay_usec *= 2; 75 } 76 return (status); 77 } 78 79 static int 80 kcs_wait_for_ibf(struct ipmi_softc *sc, bool level) 81 { 82 83 return (kcs_wait(sc, level ? KCS_STATUS_IBF : 0, KCS_STATUS_IBF)); 84 } 85 86 static int 87 kcs_wait_for_obf(struct ipmi_softc *sc, bool level) 88 { 89 90 return (kcs_wait(sc, level ? KCS_STATUS_OBF : 0, KCS_STATUS_OBF)); 91 } 92 93 static void 94 kcs_clear_obf(struct ipmi_softc *sc, int status) 95 { 96 97 /* Clear OBF */ 98 if (status & KCS_STATUS_OBF) { 99 INB(sc, KCS_DATA); 100 } 101 } 102 103 static void 104 kcs_error(struct ipmi_softc *sc) 105 { 106 int retry, status; 107 u_char data; 108 109 for (retry = 0; retry < 2; retry++) { 110 111 /* Wait for IBF = 0 */ 112 status = kcs_wait_for_ibf(sc, 0); 113 114 /* ABORT */ 115 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT); 116 117 /* Wait for IBF = 0 */ 118 status = kcs_wait_for_ibf(sc, 0); 119 120 /* Clear OBF */ 121 kcs_clear_obf(sc, status); 122 123 if (status & KCS_STATUS_OBF) { 124 data = INB(sc, KCS_DATA); 125 if (data != 0) 126 device_printf(sc->ipmi_dev, 127 "KCS Error Data %02x\n", data); 128 } 129 130 /* 0x00 to DATA_IN */ 131 OUTB(sc, KCS_DATA, 0x00); 132 133 /* Wait for IBF = 0 */ 134 status = kcs_wait_for_ibf(sc, 0); 135 136 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) { 137 138 /* Wait for OBF = 1 */ 139 status = kcs_wait_for_obf(sc, 1); 140 141 /* Read error status */ 142 data = INB(sc, KCS_DATA); 143 if (data != 0 && (data != 0xff || bootverbose)) 144 device_printf(sc->ipmi_dev, "KCS error: %02x\n", 145 data); 146 147 /* Write READ into Data_in */ 148 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ); 149 150 /* Wait for IBF = 0 */ 151 status = kcs_wait_for_ibf(sc, 0); 152 } 153 154 /* IDLE STATE */ 155 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) { 156 /* Wait for OBF = 1 */ 157 status = kcs_wait_for_obf(sc, 1); 158 159 /* Clear OBF */ 160 kcs_clear_obf(sc, status); 161 return; 162 } 163 } 164 device_printf(sc->ipmi_dev, "KCS: Error retry exhausted\n"); 165 } 166 167 /* 168 * Start to write a request. Waits for IBF to clear and then sends the 169 * WR_START command. 170 */ 171 static int 172 kcs_start_write(struct ipmi_softc *sc) 173 { 174 int retry, status; 175 176 for (retry = 0; retry < 10; retry++) { 177 /* Wait for IBF = 0 */ 178 status = kcs_wait_for_ibf(sc, 0); 179 if (status & KCS_STATUS_IBF) 180 return (0); 181 182 /* Clear OBF */ 183 kcs_clear_obf(sc, status); 184 185 /* Write start to command */ 186 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_START); 187 188 /* Wait for IBF = 0 */ 189 status = kcs_wait_for_ibf(sc, 0); 190 if (status & KCS_STATUS_IBF) 191 return (0); 192 193 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_WRITE) 194 break; 195 DELAY(1000000); 196 } 197 198 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE) 199 /* error state */ 200 return (0); 201 202 /* Clear OBF */ 203 kcs_clear_obf(sc, status); 204 205 return (1); 206 } 207 208 /* 209 * Write a byte of the request message, excluding the last byte of the 210 * message which requires special handling. 211 */ 212 static int 213 kcs_write_byte(struct ipmi_softc *sc, u_char data) 214 { 215 int status; 216 217 /* Data to Data */ 218 OUTB(sc, KCS_DATA, data); 219 220 /* Wait for IBF = 0 */ 221 status = kcs_wait_for_ibf(sc, 0); 222 if (status & KCS_STATUS_IBF) 223 return (0); 224 225 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE) 226 return (0); 227 228 /* Clear OBF */ 229 kcs_clear_obf(sc, status); 230 return (1); 231 } 232 233 /* 234 * Write the last byte of a request message. 235 */ 236 static int 237 kcs_write_last_byte(struct ipmi_softc *sc, u_char data) 238 { 239 int status; 240 241 /* Write end to command */ 242 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_WRITE_END); 243 244 /* Wait for IBF = 0 */ 245 status = kcs_wait_for_ibf(sc, 0); 246 if (status & KCS_STATUS_IBF) 247 return (0); 248 249 if (KCS_STATUS_STATE(status) != KCS_STATUS_STATE_WRITE) 250 /* error state */ 251 return (0); 252 253 /* Clear OBF */ 254 kcs_clear_obf(sc, status); 255 256 /* Send data byte to DATA. */ 257 OUTB(sc, KCS_DATA, data); 258 return (1); 259 } 260 261 /* 262 * Read one byte of the reply message. 263 */ 264 static int 265 kcs_read_byte(struct ipmi_softc *sc, u_char *data) 266 { 267 int status; 268 269 /* Wait for IBF = 0 */ 270 status = kcs_wait_for_ibf(sc, 0); 271 272 /* Read State */ 273 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) { 274 275 /* Wait for OBF = 1 */ 276 status = kcs_wait_for_obf(sc, 1); 277 if ((status & KCS_STATUS_OBF) == 0) 278 return (0); 279 280 /* Read Data_out */ 281 *data = INB(sc, KCS_DATA); 282 283 /* Write READ into Data_in */ 284 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ); 285 return (1); 286 } 287 288 /* Idle State */ 289 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) { 290 291 /* Wait for OBF = 1*/ 292 status = kcs_wait_for_obf(sc, 1); 293 if ((status & KCS_STATUS_OBF) == 0) 294 return (0); 295 296 /* Read Dummy */ 297 INB(sc, KCS_DATA); 298 return (2); 299 } 300 301 /* Error State */ 302 return (0); 303 } 304 305 /* 306 * Send a request message and collect the reply. Returns true if we 307 * succeed. 308 */ 309 static int 310 kcs_polled_request(struct ipmi_softc *sc, struct ipmi_request *req) 311 { 312 u_char *cp, data; 313 int i, state; 314 315 IPMI_IO_LOCK(sc); 316 317 /* Send the request. */ 318 if (!kcs_start_write(sc)) { 319 device_printf(sc->ipmi_dev, "KCS: Failed to start write\n"); 320 goto fail; 321 } 322 #ifdef KCS_DEBUG 323 device_printf(sc->ipmi_dev, "KCS: WRITE_START... ok\n"); 324 #endif 325 326 if (!kcs_write_byte(sc, req->ir_addr)) { 327 device_printf(sc->ipmi_dev, "KCS: Failed to write address\n"); 328 goto fail; 329 } 330 #ifdef KCS_DEBUG 331 device_printf(sc->ipmi_dev, "KCS: Wrote address: %02x\n", req->ir_addr); 332 #endif 333 334 if (req->ir_requestlen == 0) { 335 if (!kcs_write_last_byte(sc, req->ir_command)) { 336 device_printf(sc->ipmi_dev, 337 "KCS: Failed to write command\n"); 338 goto fail; 339 } 340 #ifdef KCS_DEBUG 341 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n", 342 req->ir_command); 343 #endif 344 } else { 345 if (!kcs_write_byte(sc, req->ir_command)) { 346 device_printf(sc->ipmi_dev, 347 "KCS: Failed to write command\n"); 348 goto fail; 349 } 350 #ifdef KCS_DEBUG 351 device_printf(sc->ipmi_dev, "KCS: Wrote command: %02x\n", 352 req->ir_command); 353 #endif 354 355 cp = req->ir_request; 356 for (i = 0; i < req->ir_requestlen - 1; i++) { 357 if (!kcs_write_byte(sc, *cp++)) { 358 device_printf(sc->ipmi_dev, 359 "KCS: Failed to write data byte %d\n", 360 i + 1); 361 goto fail; 362 } 363 #ifdef KCS_DEBUG 364 device_printf(sc->ipmi_dev, "KCS: Wrote data: %02x\n", 365 cp[-1]); 366 #endif 367 } 368 369 if (!kcs_write_last_byte(sc, *cp)) { 370 device_printf(sc->ipmi_dev, 371 "KCS: Failed to write last dta byte\n"); 372 goto fail; 373 } 374 #ifdef KCS_DEBUG 375 device_printf(sc->ipmi_dev, "KCS: Wrote last data: %02x\n", 376 *cp); 377 #endif 378 } 379 380 /* Read the reply. First, read the NetFn/LUN. */ 381 if (kcs_read_byte(sc, &data) != 1) { 382 device_printf(sc->ipmi_dev, "KCS: Failed to read address\n"); 383 goto fail; 384 } 385 #ifdef KCS_DEBUG 386 device_printf(sc->ipmi_dev, "KCS: Read address: %02x\n", data); 387 #endif 388 if (data != IPMI_REPLY_ADDR(req->ir_addr)) { 389 device_printf(sc->ipmi_dev, "KCS: Reply address mismatch\n"); 390 goto fail; 391 } 392 393 /* Next we read the command. */ 394 if (kcs_read_byte(sc, &data) != 1) { 395 device_printf(sc->ipmi_dev, "KCS: Failed to read command\n"); 396 goto fail; 397 } 398 #ifdef KCS_DEBUG 399 device_printf(sc->ipmi_dev, "KCS: Read command: %02x\n", data); 400 #endif 401 if (data != req->ir_command) { 402 device_printf(sc->ipmi_dev, "KCS: Command mismatch\n"); 403 goto fail; 404 } 405 406 /* Next we read the completion code. */ 407 if (kcs_read_byte(sc, &req->ir_compcode) != 1) { 408 if (bootverbose) { 409 device_printf(sc->ipmi_dev, 410 "KCS: Failed to read completion code\n"); 411 } 412 goto fail; 413 } 414 #ifdef KCS_DEBUG 415 device_printf(sc->ipmi_dev, "KCS: Read completion code: %02x\n", 416 req->ir_compcode); 417 #endif 418 419 /* Finally, read the reply from the BMC. */ 420 i = 0; 421 for (;;) { 422 state = kcs_read_byte(sc, &data); 423 if (state == 0) { 424 device_printf(sc->ipmi_dev, 425 "KCS: Read failed on byte %d\n", i + 1); 426 goto fail; 427 } 428 if (state == 2) 429 break; 430 if (i < req->ir_replybuflen) { 431 req->ir_reply[i] = data; 432 #ifdef KCS_DEBUG 433 device_printf(sc->ipmi_dev, "KCS: Read data %02x\n", 434 data); 435 } else { 436 device_printf(sc->ipmi_dev, 437 "KCS: Read short %02x byte %d\n", data, i + 1); 438 #endif 439 } 440 i++; 441 } 442 IPMI_IO_UNLOCK(sc); 443 req->ir_replylen = i; 444 #ifdef KCS_DEBUG 445 device_printf(sc->ipmi_dev, "KCS: READ finished (%d bytes)\n", i); 446 if (req->ir_replybuflen < i) 447 #else 448 if (req->ir_replybuflen < i && req->ir_replybuflen != 0) 449 #endif 450 device_printf(sc->ipmi_dev, 451 "KCS: Read short: %zd buffer, %d actual\n", 452 req->ir_replybuflen, i); 453 return (1); 454 fail: 455 kcs_error(sc); 456 IPMI_IO_UNLOCK(sc); 457 return (0); 458 } 459 460 static void 461 kcs_loop(void *arg) 462 { 463 struct ipmi_softc *sc = arg; 464 struct ipmi_request *req; 465 int i, ok; 466 467 IPMI_LOCK(sc); 468 while ((req = ipmi_dequeue_request(sc)) != NULL) { 469 IPMI_UNLOCK(sc); 470 ok = 0; 471 for (i = 0; i < 3 && !ok; i++) 472 ok = kcs_polled_request(sc, req); 473 if (ok) 474 req->ir_error = 0; 475 else 476 req->ir_error = EIO; 477 IPMI_LOCK(sc); 478 ipmi_complete_request(sc, req); 479 } 480 IPMI_UNLOCK(sc); 481 kproc_exit(0); 482 } 483 484 static int 485 kcs_startup(struct ipmi_softc *sc) 486 { 487 488 return (kproc_create(kcs_loop, sc, &sc->ipmi_kthread, 0, 0, "%s: kcs", 489 device_get_nameunit(sc->ipmi_dev))); 490 } 491 492 static int 493 kcs_driver_request(struct ipmi_softc *sc, struct ipmi_request *req, int timo) 494 { 495 int i, ok; 496 497 ok = 0; 498 for (i = 0; i < 3 && !ok; i++) 499 ok = kcs_polled_request(sc, req); 500 if (ok) 501 req->ir_error = 0; 502 else 503 req->ir_error = EIO; 504 return (req->ir_error); 505 } 506 507 int 508 ipmi_kcs_attach(struct ipmi_softc *sc) 509 { 510 int status; 511 512 /* Setup function pointers. */ 513 sc->ipmi_startup = kcs_startup; 514 sc->ipmi_enqueue_request = ipmi_polled_enqueue_request; 515 sc->ipmi_driver_request = kcs_driver_request; 516 sc->ipmi_driver_requests_polled = 1; 517 518 /* See if we can talk to the controller. */ 519 status = INB(sc, KCS_CTL_STS); 520 if (status == 0xff) { 521 device_printf(sc->ipmi_dev, "couldn't find it\n"); 522 return (ENXIO); 523 } 524 525 #ifdef KCS_DEBUG 526 device_printf(sc->ipmi_dev, "KCS: initial state: %02x\n", status); 527 #endif 528 if (status & KCS_STATUS_OBF || 529 KCS_STATUS_STATE(status) != KCS_STATUS_STATE_IDLE) 530 kcs_error(sc); 531 532 return (0); 533 } 534 535 /* 536 * Determine the alignment automatically for a PCI attachment. In this case, 537 * any unused bytes will return 0x00 when read. We make use of the C/D bit 538 * in the CTL_STS register to try to start a GET_STATUS transaction. When 539 * we write the command, that bit should be set, so we should get a non-zero 540 * value back when we read CTL_STS if the offset we are testing is the CTL_STS 541 * register. 542 */ 543 int 544 ipmi_kcs_probe_align(struct ipmi_softc *sc) 545 { 546 int status; 547 548 sc->ipmi_io_spacing = 1; 549 retry: 550 #ifdef KCS_DEBUG 551 device_printf(sc->ipmi_dev, "Trying KCS align %d... ", sc->ipmi_io_spacing); 552 #endif 553 554 /* Wait for IBF = 0 */ 555 status = INB(sc, KCS_CTL_STS); 556 while (status & KCS_STATUS_IBF) { 557 DELAY(100); 558 status = INB(sc, KCS_CTL_STS); 559 } 560 561 OUTB(sc, KCS_CTL_STS, KCS_CONTROL_GET_STATUS_ABORT); 562 563 /* Wait for IBF = 0 */ 564 status = INB(sc, KCS_CTL_STS); 565 while (status & KCS_STATUS_IBF) { 566 DELAY(100); 567 status = INB(sc, KCS_CTL_STS); 568 } 569 570 /* If we got 0x00 back, then this must not be the CTL_STS register. */ 571 if (status == 0) { 572 #ifdef KCS_DEBUG 573 printf("failed\n"); 574 #endif 575 sc->ipmi_io_spacing <<= 1; 576 if (sc->ipmi_io_spacing > 4) 577 return (0); 578 goto retry; 579 } 580 #ifdef KCS_DEBUG 581 printf("ok\n"); 582 #endif 583 584 /* Finish out the transaction. */ 585 586 /* Clear OBF */ 587 if (status & KCS_STATUS_OBF) 588 INB(sc, KCS_DATA); 589 590 /* 0x00 to DATA_IN */ 591 OUTB(sc, KCS_DATA, 0); 592 593 /* Wait for IBF = 0 */ 594 status = INB(sc, KCS_CTL_STS); 595 while (status & KCS_STATUS_IBF) { 596 DELAY(100); 597 status = INB(sc, KCS_CTL_STS); 598 } 599 600 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_READ) { 601 /* Wait for IBF = 1 */ 602 while (!(status & KCS_STATUS_OBF)) { 603 DELAY(100); 604 status = INB(sc, KCS_CTL_STS); 605 } 606 607 /* Read error status. */ 608 INB(sc, KCS_DATA); 609 610 /* Write dummy READ to DATA_IN. */ 611 OUTB(sc, KCS_DATA, KCS_DATA_IN_READ); 612 613 /* Wait for IBF = 0 */ 614 status = INB(sc, KCS_CTL_STS); 615 while (status & KCS_STATUS_IBF) { 616 DELAY(100); 617 status = INB(sc, KCS_CTL_STS); 618 } 619 } 620 621 if (KCS_STATUS_STATE(status) == KCS_STATUS_STATE_IDLE) { 622 /* Wait for IBF = 1 */ 623 while (!(status & KCS_STATUS_OBF)) { 624 DELAY(100); 625 status = INB(sc, KCS_CTL_STS); 626 } 627 628 /* Clear OBF */ 629 if (status & KCS_STATUS_OBF) 630 INB(sc, KCS_DATA); 631 } else 632 device_printf(sc->ipmi_dev, "KCS probe: end state %x\n", 633 KCS_STATUS_STATE(status)); 634 635 return (1); 636 } 637