1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/condvar.h> 33 #include <sys/conf.h> 34 #include <sys/eventhandler.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/mutex.h> 40 #include <sys/poll.h> 41 #include <sys/reboot.h> 42 #include <sys/rman.h> 43 #include <sys/selinfo.h> 44 #include <sys/sysctl.h> 45 #include <sys/watchdog.h> 46 47 #ifdef LOCAL_MODULE 48 #include <ipmi.h> 49 #include <ipmivars.h> 50 #else 51 #include <sys/ipmi.h> 52 #include <dev/ipmi/ipmivars.h> 53 #endif 54 55 #ifdef IPMICTL_SEND_COMMAND_32 56 #include <sys/abi_compat.h> 57 #endif 58 59 /* 60 * Driver request structures are allocated on the stack via alloca() to 61 * avoid calling malloc(), especially for the watchdog handler. 62 * To avoid too much stack growth, a previously allocated structure can 63 * be reused via IPMI_INIT_DRIVER_REQUEST(), but the caller should ensure 64 * that there is adequate reply/request space in the original allocation. 65 */ 66 #define IPMI_INIT_DRIVER_REQUEST(req, addr, cmd, reqlen, replylen) \ 67 bzero((req), sizeof(struct ipmi_request)); \ 68 ipmi_init_request((req), NULL, 0, (addr), (cmd), (reqlen), (replylen)) 69 70 #define IPMI_ALLOC_DRIVER_REQUEST(req, addr, cmd, reqlen, replylen) \ 71 (req) = __builtin_alloca(sizeof(struct ipmi_request) + \ 72 (reqlen) + (replylen)); \ 73 IPMI_INIT_DRIVER_REQUEST((req), (addr), (cmd), (reqlen), \ 74 (replylen)) 75 76 static d_ioctl_t ipmi_ioctl; 77 static d_poll_t ipmi_poll; 78 static d_open_t ipmi_open; 79 static void ipmi_dtor(void *arg); 80 81 int ipmi_attached = 0; 82 83 static int on = 1; 84 static bool wd_in_shutdown = false; 85 static int wd_timer_actions = IPMI_SET_WD_ACTION_POWER_CYCLE; 86 static int wd_shutdown_countdown = 0; /* sec */ 87 static int wd_startup_countdown = 0; /* sec */ 88 static int wd_pretimeout_countdown = 120; /* sec */ 89 static int cycle_wait = 10; /* sec */ 90 static int wd_init_enable = 1; 91 92 /* Log IPMICTL_SEND_COMMAND transactions */ 93 static int hexdump_send = 0; 94 95 static SYSCTL_NODE(_hw, OID_AUTO, ipmi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 96 "IPMI driver parameters"); 97 SYSCTL_INT(_hw_ipmi, OID_AUTO, on, CTLFLAG_RWTUN, 98 &on, 0, ""); 99 SYSCTL_INT(_hw_ipmi, OID_AUTO, wd_init_enable, CTLFLAG_RWTUN, 100 &wd_init_enable, 1, "Enable watchdog initialization"); 101 SYSCTL_INT(_hw_ipmi, OID_AUTO, wd_timer_actions, CTLFLAG_RWTUN, 102 &wd_timer_actions, 0, 103 "IPMI watchdog timer actions (including pre-timeout interrupt)"); 104 SYSCTL_INT(_hw_ipmi, OID_AUTO, wd_shutdown_countdown, CTLFLAG_RWTUN, 105 &wd_shutdown_countdown, 0, 106 "IPMI watchdog countdown for shutdown (seconds)"); 107 SYSCTL_INT(_hw_ipmi, OID_AUTO, wd_startup_countdown, CTLFLAG_RDTUN, 108 &wd_startup_countdown, 0, 109 "IPMI watchdog countdown initialized during startup (seconds)"); 110 SYSCTL_INT(_hw_ipmi, OID_AUTO, wd_pretimeout_countdown, CTLFLAG_RWTUN, 111 &wd_pretimeout_countdown, 0, 112 "IPMI watchdog pre-timeout countdown (seconds)"); 113 SYSCTL_INT(_hw_ipmi, OID_AUTO, cycle_wait, CTLFLAG_RWTUN, 114 &cycle_wait, 0, 115 "IPMI power cycle on reboot delay time (seconds)"); 116 SYSCTL_INT(_hw_ipmi, OID_AUTO, hexdump_send, CTLFLAG_RWTUN, 117 &hexdump_send, 0, 118 "IPMI driver hexdump IPMICTL_SEND_COMMAND"); 119 120 static struct cdevsw ipmi_cdevsw = { 121 .d_version = D_VERSION, 122 .d_open = ipmi_open, 123 .d_ioctl = ipmi_ioctl, 124 .d_poll = ipmi_poll, 125 .d_name = "ipmi", 126 }; 127 128 static MALLOC_DEFINE(M_IPMI, "ipmi", "ipmi"); 129 130 static int 131 ipmi_open(struct cdev *cdev, int flags, int fmt, struct thread *td) 132 { 133 struct ipmi_device *dev; 134 struct ipmi_softc *sc; 135 int error; 136 137 if (!on) 138 return (ENOENT); 139 140 /* Initialize the per file descriptor data. */ 141 dev = malloc(sizeof(struct ipmi_device), M_IPMI, M_WAITOK | M_ZERO); 142 error = devfs_set_cdevpriv(dev, ipmi_dtor); 143 if (error) { 144 free(dev, M_IPMI); 145 return (error); 146 } 147 148 sc = cdev->si_drv1; 149 TAILQ_INIT(&dev->ipmi_completed_requests); 150 dev->ipmi_address = IPMI_BMC_SLAVE_ADDR; 151 dev->ipmi_lun = IPMI_BMC_SMS_LUN; 152 dev->ipmi_softc = sc; 153 IPMI_LOCK(sc); 154 sc->ipmi_opened++; 155 IPMI_UNLOCK(sc); 156 157 return (0); 158 } 159 160 static int 161 ipmi_poll(struct cdev *cdev, int poll_events, struct thread *td) 162 { 163 struct ipmi_device *dev; 164 struct ipmi_softc *sc; 165 int revents = 0; 166 167 if (devfs_get_cdevpriv((void **)&dev)) 168 return (0); 169 170 sc = cdev->si_drv1; 171 IPMI_LOCK(sc); 172 if (poll_events & (POLLIN | POLLRDNORM)) { 173 if (!TAILQ_EMPTY(&dev->ipmi_completed_requests)) 174 revents |= poll_events & (POLLIN | POLLRDNORM); 175 if (dev->ipmi_requests == 0) 176 revents |= POLLERR; 177 } 178 179 if (revents == 0) { 180 if (poll_events & (POLLIN | POLLRDNORM)) 181 selrecord(td, &dev->ipmi_select); 182 } 183 IPMI_UNLOCK(sc); 184 185 return (revents); 186 } 187 188 static void 189 ipmi_purge_completed_requests(struct ipmi_device *dev) 190 { 191 struct ipmi_request *req; 192 193 while (!TAILQ_EMPTY(&dev->ipmi_completed_requests)) { 194 req = TAILQ_FIRST(&dev->ipmi_completed_requests); 195 TAILQ_REMOVE(&dev->ipmi_completed_requests, req, ir_link); 196 dev->ipmi_requests--; 197 ipmi_free_request(req); 198 } 199 } 200 201 static void 202 ipmi_dtor(void *arg) 203 { 204 struct ipmi_request *req, *nreq; 205 struct ipmi_device *dev; 206 struct ipmi_softc *sc; 207 208 dev = arg; 209 sc = dev->ipmi_softc; 210 211 IPMI_LOCK(sc); 212 if (dev->ipmi_requests) { 213 /* Throw away any pending requests for this device. */ 214 TAILQ_FOREACH_SAFE(req, &sc->ipmi_pending_requests_highpri, ir_link, 215 nreq) { 216 if (req->ir_owner == dev) { 217 TAILQ_REMOVE(&sc->ipmi_pending_requests_highpri, req, 218 ir_link); 219 dev->ipmi_requests--; 220 ipmi_free_request(req); 221 } 222 } 223 TAILQ_FOREACH_SAFE(req, &sc->ipmi_pending_requests, ir_link, 224 nreq) { 225 if (req->ir_owner == dev) { 226 TAILQ_REMOVE(&sc->ipmi_pending_requests, req, 227 ir_link); 228 dev->ipmi_requests--; 229 ipmi_free_request(req); 230 } 231 } 232 233 /* Throw away any pending completed requests for this device. */ 234 ipmi_purge_completed_requests(dev); 235 236 /* 237 * If we still have outstanding requests, they must be stuck 238 * in an interface driver, so wait for those to drain. 239 */ 240 dev->ipmi_closing = 1; 241 while (dev->ipmi_requests > 0) { 242 msleep(&dev->ipmi_requests, &sc->ipmi_requests_lock, 243 PWAIT, "ipmidrain", 0); 244 ipmi_purge_completed_requests(dev); 245 } 246 } 247 sc->ipmi_opened--; 248 IPMI_UNLOCK(sc); 249 250 /* Cleanup. */ 251 free(dev, M_IPMI); 252 } 253 254 static u_char 255 ipmi_ipmb_checksum(u_char *data, int len) 256 { 257 u_char sum = 0; 258 259 for (; len; len--) 260 sum += *data++; 261 return (-sum); 262 } 263 264 static int 265 ipmi_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, 266 int flags, struct thread *td) 267 { 268 struct ipmi_softc *sc; 269 struct ipmi_device *dev; 270 struct ipmi_request *kreq; 271 struct ipmi_req *req = (struct ipmi_req *)data; 272 struct ipmi_recv *recv = (struct ipmi_recv *)data; 273 struct ipmi_addr addr; 274 #ifdef IPMICTL_SEND_COMMAND_32 275 struct ipmi_req32 *req32 = (struct ipmi_req32 *)data; 276 struct ipmi_recv32 *recv32 = (struct ipmi_recv32 *)data; 277 union { 278 struct ipmi_req req; 279 struct ipmi_recv recv; 280 } thunk32; 281 #endif 282 int error, len; 283 284 error = devfs_get_cdevpriv((void **)&dev); 285 if (error) 286 return (error); 287 288 sc = cdev->si_drv1; 289 290 #ifdef IPMICTL_SEND_COMMAND_32 291 /* Convert 32-bit structures to native. */ 292 switch (cmd) { 293 case IPMICTL_SEND_COMMAND_32: 294 req = &thunk32.req; 295 req->addr = PTRIN(req32->addr); 296 req->addr_len = req32->addr_len; 297 req->msgid = req32->msgid; 298 req->msg.netfn = req32->msg.netfn; 299 req->msg.cmd = req32->msg.cmd; 300 req->msg.data_len = req32->msg.data_len; 301 req->msg.data = PTRIN(req32->msg.data); 302 break; 303 case IPMICTL_RECEIVE_MSG_TRUNC_32: 304 case IPMICTL_RECEIVE_MSG_32: 305 recv = &thunk32.recv; 306 recv->addr = PTRIN(recv32->addr); 307 recv->addr_len = recv32->addr_len; 308 recv->msg.data_len = recv32->msg.data_len; 309 recv->msg.data = PTRIN(recv32->msg.data); 310 break; 311 } 312 #endif 313 314 switch (cmd) { 315 #ifdef IPMICTL_SEND_COMMAND_32 316 case IPMICTL_SEND_COMMAND_32: 317 #endif 318 case IPMICTL_SEND_COMMAND: 319 error = copyin(req->addr, &addr, sizeof(addr)); 320 if (error) 321 return (error); 322 323 if (addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) { 324 struct ipmi_system_interface_addr *saddr = 325 (struct ipmi_system_interface_addr *)&addr; 326 327 kreq = ipmi_alloc_request(dev, req->msgid, 328 IPMI_ADDR(req->msg.netfn, saddr->lun & 0x3), 329 req->msg.cmd, req->msg.data_len, IPMI_MAX_RX); 330 error = copyin(req->msg.data, kreq->ir_request, 331 req->msg.data_len); 332 if (error) { 333 ipmi_free_request(kreq); 334 return (error); 335 } 336 if (hexdump_send) { 337 printf("IPMI_REQ_MSG: netfn=%02x cmd=%02x\n", 338 req->msg.netfn, req->msg.cmd); 339 hexdump(kreq->ir_request, req->msg.data_len, 340 "SEND_COMMAND ->\n", 0); 341 } 342 IPMI_LOCK(sc); 343 dev->ipmi_requests++; 344 error = sc->ipmi_enqueue_request(sc, kreq); 345 IPMI_UNLOCK(sc); 346 if (error) 347 return (error); 348 break; 349 } 350 351 /* Special processing for IPMB commands */ 352 struct ipmi_ipmb_addr *iaddr = (struct ipmi_ipmb_addr *)&addr; 353 354 IPMI_ALLOC_DRIVER_REQUEST(kreq, IPMI_ADDR(IPMI_APP_REQUEST, 0), 355 IPMI_SEND_MSG, req->msg.data_len + 8, IPMI_MAX_RX); 356 /* Construct the SEND MSG header */ 357 kreq->ir_request[0] = iaddr->channel; 358 kreq->ir_request[1] = iaddr->slave_addr; 359 kreq->ir_request[2] = IPMI_ADDR(req->msg.netfn, iaddr->lun); 360 kreq->ir_request[3] = 361 ipmi_ipmb_checksum(&kreq->ir_request[1], 2); 362 kreq->ir_request[4] = dev->ipmi_address; 363 kreq->ir_request[5] = IPMI_ADDR(0, dev->ipmi_lun); 364 kreq->ir_request[6] = req->msg.cmd; 365 /* Copy the message data */ 366 if (req->msg.data_len > 0) { 367 error = copyin(req->msg.data, &kreq->ir_request[7], 368 req->msg.data_len); 369 if (error != 0) 370 return (error); 371 } 372 kreq->ir_request[req->msg.data_len + 7] = 373 ipmi_ipmb_checksum(&kreq->ir_request[4], 374 req->msg.data_len + 3); 375 error = ipmi_submit_driver_request(sc, kreq); 376 if (error != 0) 377 return (error); 378 379 kreq = ipmi_alloc_request(dev, req->msgid, 380 IPMI_ADDR(IPMI_APP_REQUEST, 0), IPMI_GET_MSG, 381 0, IPMI_MAX_RX); 382 kreq->ir_ipmb = true; 383 kreq->ir_ipmb_addr = IPMI_ADDR(req->msg.netfn, 0); 384 kreq->ir_ipmb_command = req->msg.cmd; 385 IPMI_LOCK(sc); 386 dev->ipmi_requests++; 387 error = sc->ipmi_enqueue_request(sc, kreq); 388 IPMI_UNLOCK(sc); 389 if (error != 0) 390 return (error); 391 break; 392 #ifdef IPMICTL_SEND_COMMAND_32 393 case IPMICTL_RECEIVE_MSG_TRUNC_32: 394 case IPMICTL_RECEIVE_MSG_32: 395 #endif 396 case IPMICTL_RECEIVE_MSG_TRUNC: 397 case IPMICTL_RECEIVE_MSG: 398 error = copyin(recv->addr, &addr, sizeof(addr)); 399 if (error) 400 return (error); 401 402 IPMI_LOCK(sc); 403 kreq = TAILQ_FIRST(&dev->ipmi_completed_requests); 404 if (kreq == NULL) { 405 IPMI_UNLOCK(sc); 406 return (EAGAIN); 407 } 408 if (kreq->ir_error != 0) { 409 error = kreq->ir_error; 410 TAILQ_REMOVE(&dev->ipmi_completed_requests, kreq, 411 ir_link); 412 dev->ipmi_requests--; 413 IPMI_UNLOCK(sc); 414 ipmi_free_request(kreq); 415 return (error); 416 } 417 418 recv->recv_type = IPMI_RESPONSE_RECV_TYPE; 419 recv->msgid = kreq->ir_msgid; 420 if (kreq->ir_ipmb) { 421 addr.channel = IPMI_IPMB_CHANNEL; 422 recv->msg.netfn = 423 IPMI_REPLY_ADDR(kreq->ir_ipmb_addr) >> 2; 424 recv->msg.cmd = kreq->ir_ipmb_command; 425 /* Get the compcode of response */ 426 kreq->ir_compcode = kreq->ir_reply[6]; 427 /* Move the reply head past response header */ 428 kreq->ir_reply += 7; 429 len = kreq->ir_replylen - 7; 430 } else { 431 addr.channel = IPMI_BMC_CHANNEL; 432 recv->msg.netfn = IPMI_REPLY_ADDR(kreq->ir_addr) >> 2; 433 recv->msg.cmd = kreq->ir_command; 434 len = kreq->ir_replylen + 1; 435 } 436 437 if (recv->msg.data_len < len && 438 (cmd == IPMICTL_RECEIVE_MSG 439 #ifdef IPMICTL_RECEIVE_MSG_32 440 || cmd == IPMICTL_RECEIVE_MSG_32 441 #endif 442 )) { 443 IPMI_UNLOCK(sc); 444 return (EMSGSIZE); 445 } 446 TAILQ_REMOVE(&dev->ipmi_completed_requests, kreq, ir_link); 447 dev->ipmi_requests--; 448 IPMI_UNLOCK(sc); 449 len = min(recv->msg.data_len, len); 450 recv->msg.data_len = len; 451 error = copyout(&addr, recv->addr,sizeof(addr)); 452 if (error == 0) 453 error = copyout(&kreq->ir_compcode, recv->msg.data, 1); 454 if (error == 0) 455 error = copyout(kreq->ir_reply, recv->msg.data + 1, 456 len - 1); 457 if (hexdump_send) { 458 printf("IPMI_RECV_MSG: netfn=%02x cmd=%02x\n", 459 recv->msg.netfn, recv->msg.cmd); 460 hexdump(kreq->ir_reply, len - 1, 461 "RECEIVE_MSG <-\n", 0); 462 } 463 ipmi_free_request(kreq); 464 if (error) 465 return (error); 466 break; 467 case IPMICTL_SET_MY_ADDRESS_CMD: 468 IPMI_LOCK(sc); 469 dev->ipmi_address = *(int*)data; 470 IPMI_UNLOCK(sc); 471 break; 472 case IPMICTL_GET_MY_ADDRESS_CMD: 473 IPMI_LOCK(sc); 474 *(int*)data = dev->ipmi_address; 475 IPMI_UNLOCK(sc); 476 break; 477 case IPMICTL_SET_MY_LUN_CMD: 478 IPMI_LOCK(sc); 479 dev->ipmi_lun = *(int*)data & 0x3; 480 IPMI_UNLOCK(sc); 481 break; 482 case IPMICTL_GET_MY_LUN_CMD: 483 IPMI_LOCK(sc); 484 *(int*)data = dev->ipmi_lun; 485 IPMI_UNLOCK(sc); 486 break; 487 case IPMICTL_SET_GETS_EVENTS_CMD: 488 /* 489 device_printf(sc->ipmi_dev, 490 "IPMICTL_SET_GETS_EVENTS_CMD NA\n"); 491 */ 492 break; 493 case IPMICTL_REGISTER_FOR_CMD: 494 case IPMICTL_UNREGISTER_FOR_CMD: 495 return (EOPNOTSUPP); 496 default: 497 device_printf(sc->ipmi_dev, "Unknown IOCTL %lX\n", cmd); 498 return (ENOIOCTL); 499 } 500 501 #ifdef IPMICTL_SEND_COMMAND_32 502 /* Update changed fields in 32-bit structures. */ 503 switch (cmd) { 504 case IPMICTL_RECEIVE_MSG_TRUNC_32: 505 case IPMICTL_RECEIVE_MSG_32: 506 recv32->recv_type = recv->recv_type; 507 recv32->msgid = recv->msgid; 508 recv32->msg.netfn = recv->msg.netfn; 509 recv32->msg.cmd = recv->msg.cmd; 510 recv32->msg.data_len = recv->msg.data_len; 511 break; 512 } 513 #endif 514 return (0); 515 } 516 517 /* 518 * Request management. 519 */ 520 521 __inline void 522 ipmi_init_request(struct ipmi_request *req, struct ipmi_device *dev, long msgid, 523 uint8_t addr, uint8_t command, size_t requestlen, size_t replylen) 524 { 525 526 req->ir_owner = dev; 527 req->ir_msgid = msgid; 528 req->ir_addr = addr; 529 req->ir_command = command; 530 if (requestlen) { 531 req->ir_request = (char *)&req[1]; 532 req->ir_requestlen = requestlen; 533 } 534 if (replylen) { 535 req->ir_reply = (char *)&req[1] + requestlen; 536 req->ir_replybuflen = replylen; 537 } 538 } 539 540 /* Allocate a new request with request and reply buffers. */ 541 struct ipmi_request * 542 ipmi_alloc_request(struct ipmi_device *dev, long msgid, uint8_t addr, 543 uint8_t command, size_t requestlen, size_t replylen) 544 { 545 struct ipmi_request *req; 546 547 req = malloc(sizeof(struct ipmi_request) + requestlen + replylen, 548 M_IPMI, M_WAITOK | M_ZERO); 549 ipmi_init_request(req, dev, msgid, addr, command, requestlen, replylen); 550 return (req); 551 } 552 553 /* Free a request no longer in use. */ 554 void 555 ipmi_free_request(struct ipmi_request *req) 556 { 557 558 free(req, M_IPMI); 559 } 560 561 /* Store a processed request on the appropriate completion queue. */ 562 void 563 ipmi_complete_request(struct ipmi_softc *sc, struct ipmi_request *req) 564 { 565 struct ipmi_device *dev; 566 567 IPMI_LOCK_ASSERT(sc); 568 569 /* 570 * Anonymous requests (from inside the driver) always have a 571 * waiter that we awaken. 572 */ 573 if (req->ir_owner == NULL) 574 wakeup(req); 575 else { 576 dev = req->ir_owner; 577 TAILQ_INSERT_TAIL(&dev->ipmi_completed_requests, req, ir_link); 578 selwakeup(&dev->ipmi_select); 579 if (dev->ipmi_closing) 580 wakeup(&dev->ipmi_requests); 581 } 582 } 583 584 /* Perform an internal driver request. */ 585 int 586 ipmi_submit_driver_request(struct ipmi_softc *sc, struct ipmi_request *req) 587 { 588 589 return (sc->ipmi_driver_request(sc, req)); 590 } 591 592 /* 593 * Helper routine for polled system interfaces that use 594 * ipmi_polled_enqueue_request() to queue requests. This request 595 * waits until there is a pending request and then returns the first 596 * request. If the driver is shutting down, it returns NULL. 597 */ 598 struct ipmi_request * 599 ipmi_dequeue_request(struct ipmi_softc *sc) 600 { 601 struct ipmi_request *req; 602 603 IPMI_LOCK_ASSERT(sc); 604 605 while (!sc->ipmi_detaching && TAILQ_EMPTY(&sc->ipmi_pending_requests) && 606 TAILQ_EMPTY(&sc->ipmi_pending_requests_highpri)) 607 cv_wait(&sc->ipmi_request_added, &sc->ipmi_requests_lock); 608 if (sc->ipmi_detaching) 609 return (NULL); 610 611 req = TAILQ_FIRST(&sc->ipmi_pending_requests_highpri); 612 if (req != NULL) 613 TAILQ_REMOVE(&sc->ipmi_pending_requests_highpri, req, ir_link); 614 else { 615 req = TAILQ_FIRST(&sc->ipmi_pending_requests); 616 TAILQ_REMOVE(&sc->ipmi_pending_requests, req, ir_link); 617 } 618 return (req); 619 } 620 621 /* Default implementation of ipmi_enqueue_request() for polled interfaces. */ 622 int 623 ipmi_polled_enqueue_request(struct ipmi_softc *sc, struct ipmi_request *req) 624 { 625 626 IPMI_LOCK_ASSERT(sc); 627 628 TAILQ_INSERT_TAIL(&sc->ipmi_pending_requests, req, ir_link); 629 cv_signal(&sc->ipmi_request_added); 630 return (0); 631 } 632 633 int 634 ipmi_polled_enqueue_request_highpri(struct ipmi_softc *sc, struct ipmi_request *req) 635 { 636 637 IPMI_LOCK_ASSERT(sc); 638 639 TAILQ_INSERT_TAIL(&sc->ipmi_pending_requests_highpri, req, ir_link); 640 cv_signal(&sc->ipmi_request_added); 641 return (0); 642 } 643 644 /* 645 * Watchdog event handler. 646 */ 647 648 static int 649 ipmi_reset_watchdog(struct ipmi_softc *sc) 650 { 651 struct ipmi_request *req; 652 int error; 653 654 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0), 655 IPMI_RESET_WDOG, 0, 0); 656 error = ipmi_submit_driver_request(sc, req); 657 if (error) { 658 device_printf(sc->ipmi_dev, "Failed to reset watchdog\n"); 659 } else if (req->ir_compcode == 0x80) { 660 error = ENOENT; 661 } else if (req->ir_compcode != 0) { 662 device_printf(sc->ipmi_dev, "Watchdog reset returned 0x%x\n", 663 req->ir_compcode); 664 error = EINVAL; 665 } 666 return (error); 667 } 668 669 static int 670 ipmi_set_watchdog(struct ipmi_softc *sc, unsigned int sec) 671 { 672 struct ipmi_request *req; 673 int error; 674 675 if (sec > 0xffff / 10) 676 return (EINVAL); 677 678 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0), 679 IPMI_SET_WDOG, 6, 0); 680 if (sec) { 681 req->ir_request[0] = IPMI_SET_WD_TIMER_DONT_STOP 682 | IPMI_SET_WD_TIMER_SMS_OS; 683 req->ir_request[1] = (wd_timer_actions & 0xff); 684 req->ir_request[2] = min(0xff, 685 min(wd_pretimeout_countdown, (sec + 2) / 4)); 686 req->ir_request[3] = 0; /* Timer use */ 687 req->ir_request[4] = (sec * 10) & 0xff; 688 req->ir_request[5] = (sec * 10) >> 8; 689 } else { 690 req->ir_request[0] = IPMI_SET_WD_TIMER_SMS_OS; 691 req->ir_request[1] = 0; 692 req->ir_request[2] = 0; 693 req->ir_request[3] = 0; /* Timer use */ 694 req->ir_request[4] = 0; 695 req->ir_request[5] = 0; 696 } 697 error = ipmi_submit_driver_request(sc, req); 698 if (error) { 699 device_printf(sc->ipmi_dev, "Failed to set watchdog\n"); 700 } else if (req->ir_compcode != 0) { 701 device_printf(sc->ipmi_dev, "Watchdog set returned 0x%x\n", 702 req->ir_compcode); 703 error = EINVAL; 704 } 705 return (error); 706 } 707 708 static void 709 ipmi_wd_event(void *arg, unsigned int cmd, int *error) 710 { 711 struct ipmi_softc *sc = arg; 712 unsigned int timeout; 713 int e; 714 715 /* Ignore requests while disabled. */ 716 if (!on) 717 return; 718 719 /* 720 * To prevent infinite hangs, we don't let anyone pat or change 721 * the watchdog when we're shutting down. (See ipmi_shutdown_event().) 722 * However, we do want to keep patting the watchdog while we are doing 723 * a coredump. 724 */ 725 if (wd_in_shutdown) { 726 if (dumping && sc->ipmi_watchdog_active) 727 ipmi_reset_watchdog(sc); 728 return; 729 } 730 731 cmd &= WD_INTERVAL; 732 if (cmd > 0 && cmd <= 63) { 733 timeout = ((uint64_t)1 << cmd) / 1000000000; 734 if (timeout == 0) 735 timeout = 1; 736 if (timeout != sc->ipmi_watchdog_active || 737 wd_timer_actions != sc->ipmi_watchdog_actions || 738 wd_pretimeout_countdown != sc->ipmi_watchdog_pretimeout) { 739 e = ipmi_set_watchdog(sc, timeout); 740 if (e == 0) { 741 sc->ipmi_watchdog_active = timeout; 742 sc->ipmi_watchdog_actions = wd_timer_actions; 743 sc->ipmi_watchdog_pretimeout = wd_pretimeout_countdown; 744 } else { 745 (void)ipmi_set_watchdog(sc, 0); 746 sc->ipmi_watchdog_active = 0; 747 sc->ipmi_watchdog_actions = 0; 748 sc->ipmi_watchdog_pretimeout = 0; 749 } 750 } 751 if (sc->ipmi_watchdog_active != 0) { 752 e = ipmi_reset_watchdog(sc); 753 if (e == 0) { 754 *error = 0; 755 } else { 756 (void)ipmi_set_watchdog(sc, 0); 757 sc->ipmi_watchdog_active = 0; 758 sc->ipmi_watchdog_actions = 0; 759 sc->ipmi_watchdog_pretimeout = 0; 760 } 761 } 762 } else if (atomic_readandclear_int(&sc->ipmi_watchdog_active) != 0) { 763 sc->ipmi_watchdog_actions = 0; 764 sc->ipmi_watchdog_pretimeout = 0; 765 766 e = ipmi_set_watchdog(sc, 0); 767 if (e != 0 && cmd == 0) 768 *error = EOPNOTSUPP; 769 } 770 } 771 772 static void 773 ipmi_shutdown_event(void *arg, int howto) 774 { 775 struct ipmi_softc *sc = arg; 776 777 /* Ignore event if disabled. */ 778 if (!on) 779 return; 780 781 /* 782 * Positive wd_shutdown_countdown value will re-arm watchdog; 783 * Zero value in wd_shutdown_countdown will disable watchdog; 784 * Negative value in wd_shutdown_countdown will keep existing state; 785 * 786 * System halt is a special case of shutdown where wd_shutdown_countdown 787 * is ignored and watchdog is disabled to ensure that the system remains 788 * halted as requested. 789 * 790 * Revert to using a power cycle to ensure that the watchdog will 791 * do something useful here. Having the watchdog send an NMI 792 * instead is useless during shutdown, and might be ignored if an 793 * NMI already triggered. 794 */ 795 796 wd_in_shutdown = true; 797 if (wd_shutdown_countdown == 0 || (howto & RB_HALT) != 0) { 798 /* disable watchdog */ 799 ipmi_set_watchdog(sc, 0); 800 sc->ipmi_watchdog_active = 0; 801 } else if (wd_shutdown_countdown > 0) { 802 /* set desired action and time, and, reset watchdog */ 803 wd_timer_actions = IPMI_SET_WD_ACTION_POWER_CYCLE; 804 ipmi_set_watchdog(sc, wd_shutdown_countdown); 805 sc->ipmi_watchdog_active = wd_shutdown_countdown; 806 ipmi_reset_watchdog(sc); 807 } 808 } 809 810 static void 811 ipmi_power_cycle(void *arg, int howto) 812 { 813 struct ipmi_softc *sc = arg; 814 struct ipmi_request *req; 815 816 /* 817 * Ignore everything except power cycling requests 818 */ 819 if ((howto & RB_POWERCYCLE) == 0) 820 return; 821 822 device_printf(sc->ipmi_dev, "Power cycling using IPMI\n"); 823 824 /* 825 * Send a CHASSIS_CONTROL command to the CHASSIS device, subcommand 2 826 * as described in IPMI v2.0 spec section 28.3. 827 */ 828 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_CHASSIS_REQUEST, 0), 829 IPMI_CHASSIS_CONTROL, 1, 0); 830 req->ir_request[0] = IPMI_CC_POWER_CYCLE; 831 832 ipmi_submit_driver_request(sc, req); 833 834 if (req->ir_error != 0 || req->ir_compcode != 0) { 835 device_printf(sc->ipmi_dev, "Power cycling via IPMI failed code %#x %#x\n", 836 req->ir_error, req->ir_compcode); 837 return; 838 } 839 840 /* 841 * BMCs are notoriously slow, give it cycle_wait seconds for the power 842 * down leg of the power cycle. If that fails, fallback to the next 843 * hanlder in the shutdown_final chain and/or the platform failsafe. 844 */ 845 DELAY(cycle_wait * 1000 * 1000); 846 device_printf(sc->ipmi_dev, "Power cycling via IPMI timed out\n"); 847 } 848 849 static void 850 ipmi_startup(void *arg) 851 { 852 struct ipmi_softc *sc = arg; 853 struct ipmi_request *req; 854 device_t dev; 855 int error, i; 856 857 config_intrhook_disestablish(&sc->ipmi_ich); 858 dev = sc->ipmi_dev; 859 860 /* Initialize interface-independent state. */ 861 mtx_init(&sc->ipmi_requests_lock, "ipmi requests", NULL, MTX_DEF); 862 mtx_init(&sc->ipmi_io_lock, "ipmi io", NULL, MTX_DEF); 863 cv_init(&sc->ipmi_request_added, "ipmireq"); 864 TAILQ_INIT(&sc->ipmi_pending_requests_highpri); 865 TAILQ_INIT(&sc->ipmi_pending_requests); 866 867 /* Initialize interface-dependent state. */ 868 error = sc->ipmi_startup(sc); 869 if (error) { 870 device_printf(dev, "Failed to initialize interface: %d\n", 871 error); 872 return; 873 } 874 875 /* Send a GET_DEVICE_ID request. */ 876 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0), 877 IPMI_GET_DEVICE_ID, 0, 15); 878 879 error = ipmi_submit_driver_request(sc, req); 880 if (error == EWOULDBLOCK) { 881 device_printf(dev, "Timed out waiting for GET_DEVICE_ID\n"); 882 return; 883 } else if (error) { 884 device_printf(dev, "Failed GET_DEVICE_ID: %d\n", error); 885 return; 886 } else if (req->ir_compcode != 0) { 887 device_printf(dev, 888 "Bad completion code for GET_DEVICE_ID: %d\n", 889 req->ir_compcode); 890 return; 891 } else if (req->ir_replylen < 5) { 892 device_printf(dev, "Short reply for GET_DEVICE_ID: %d\n", 893 req->ir_replylen); 894 return; 895 } 896 897 device_printf(dev, "IPMI device rev. %d, firmware rev. %d.%d%d, " 898 "version %d.%d, device support mask %#x\n", 899 req->ir_reply[1] & 0x0f, 900 req->ir_reply[2] & 0x7f, req->ir_reply[3] >> 4, req->ir_reply[3] & 0x0f, 901 req->ir_reply[4] & 0x0f, req->ir_reply[4] >> 4, req->ir_reply[5]); 902 903 sc->ipmi_dev_support = req->ir_reply[5]; 904 905 IPMI_INIT_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0), 906 IPMI_CLEAR_FLAGS, 1, 0); 907 908 ipmi_submit_driver_request(sc, req); 909 910 /* XXX: Magic numbers */ 911 if (req->ir_compcode == 0xc0) { 912 device_printf(dev, "Clear flags is busy\n"); 913 } 914 if (req->ir_compcode == 0xc1) { 915 device_printf(dev, "Clear flags illegal\n"); 916 } 917 918 for (i = 0; i < 8; i++) { 919 IPMI_INIT_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0), 920 IPMI_GET_CHANNEL_INFO, 1, 0); 921 req->ir_request[0] = i; 922 923 error = ipmi_submit_driver_request(sc, req); 924 925 if (error != 0 || req->ir_compcode != 0) 926 break; 927 } 928 device_printf(dev, "Number of channels %d\n", i); 929 930 /* 931 * Probe for watchdog, but only for backends which support 932 * polled driver requests. 933 */ 934 if (wd_init_enable && sc->ipmi_driver_requests_polled) { 935 IPMI_INIT_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0), 936 IPMI_GET_WDOG, 0, 0); 937 938 error = ipmi_submit_driver_request(sc, req); 939 940 if (error == 0 && req->ir_compcode == 0x00) { 941 device_printf(dev, "Attached watchdog\n"); 942 /* register the watchdog event handler */ 943 sc->ipmi_watchdog_tag = EVENTHANDLER_REGISTER( 944 watchdog_list, ipmi_wd_event, sc, 0); 945 sc->ipmi_shutdown_tag = EVENTHANDLER_REGISTER( 946 shutdown_pre_sync, ipmi_shutdown_event, 947 sc, 0); 948 } 949 } 950 951 sc->ipmi_cdev = make_dev(&ipmi_cdevsw, device_get_unit(dev), 952 UID_ROOT, GID_OPERATOR, 0660, "ipmi%d", device_get_unit(dev)); 953 if (sc->ipmi_cdev == NULL) { 954 device_printf(dev, "Failed to create cdev\n"); 955 return; 956 } 957 sc->ipmi_cdev->si_drv1 = sc; 958 959 /* 960 * Set initial watchdog state. If desired, set an initial 961 * watchdog on startup. Or, if the watchdog device is 962 * disabled, clear any existing watchdog. 963 */ 964 if (on && wd_startup_countdown > 0) { 965 if (ipmi_set_watchdog(sc, wd_startup_countdown) == 0 && 966 ipmi_reset_watchdog(sc) == 0) { 967 sc->ipmi_watchdog_active = wd_startup_countdown; 968 sc->ipmi_watchdog_actions = wd_timer_actions; 969 sc->ipmi_watchdog_pretimeout = wd_pretimeout_countdown; 970 } else 971 (void)ipmi_set_watchdog(sc, 0); 972 ipmi_reset_watchdog(sc); 973 } else if (!on) 974 (void)ipmi_set_watchdog(sc, 0); 975 /* 976 * Power cycle the system off using IPMI. We use last - 2 since we don't 977 * handle all the other kinds of reboots. We'll let others handle them. 978 * We only try to do this if the BMC supports the Chassis device. 979 */ 980 if (sc->ipmi_dev_support & IPMI_ADS_CHASSIS) { 981 device_printf(dev, "Establishing power cycle handler\n"); 982 sc->ipmi_power_cycle_tag = EVENTHANDLER_REGISTER(shutdown_final, 983 ipmi_power_cycle, sc, SHUTDOWN_PRI_LAST - 2); 984 } 985 } 986 987 int 988 ipmi_attach(device_t dev) 989 { 990 struct ipmi_softc *sc = device_get_softc(dev); 991 int error; 992 993 if (sc->ipmi_irq_res != NULL && sc->ipmi_intr != NULL) { 994 error = bus_setup_intr(dev, sc->ipmi_irq_res, INTR_TYPE_MISC, 995 NULL, sc->ipmi_intr, sc, &sc->ipmi_irq); 996 if (error) { 997 device_printf(dev, "can't set up interrupt\n"); 998 return (error); 999 } 1000 } 1001 1002 bzero(&sc->ipmi_ich, sizeof(struct intr_config_hook)); 1003 sc->ipmi_ich.ich_func = ipmi_startup; 1004 sc->ipmi_ich.ich_arg = sc; 1005 if (config_intrhook_establish(&sc->ipmi_ich) != 0) { 1006 device_printf(dev, "can't establish configuration hook\n"); 1007 return (ENOMEM); 1008 } 1009 1010 ipmi_attached = 1; 1011 return (0); 1012 } 1013 1014 int 1015 ipmi_detach(device_t dev) 1016 { 1017 struct ipmi_softc *sc; 1018 1019 sc = device_get_softc(dev); 1020 1021 /* Fail if there are any open handles. */ 1022 IPMI_LOCK(sc); 1023 if (sc->ipmi_opened) { 1024 IPMI_UNLOCK(sc); 1025 return (EBUSY); 1026 } 1027 IPMI_UNLOCK(sc); 1028 if (sc->ipmi_cdev) 1029 destroy_dev(sc->ipmi_cdev); 1030 1031 /* Detach from watchdog handling and turn off watchdog. */ 1032 if (sc->ipmi_shutdown_tag) 1033 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, 1034 sc->ipmi_shutdown_tag); 1035 if (sc->ipmi_watchdog_tag) { 1036 EVENTHANDLER_DEREGISTER(watchdog_list, sc->ipmi_watchdog_tag); 1037 ipmi_set_watchdog(sc, 0); 1038 } 1039 1040 /* Detach from shutdown handling for power cycle reboot */ 1041 if (sc->ipmi_power_cycle_tag) 1042 EVENTHANDLER_DEREGISTER(shutdown_final, sc->ipmi_power_cycle_tag); 1043 1044 /* XXX: should use shutdown callout I think. */ 1045 /* If the backend uses a kthread, shut it down. */ 1046 IPMI_LOCK(sc); 1047 sc->ipmi_detaching = 1; 1048 if (sc->ipmi_kthread) { 1049 cv_broadcast(&sc->ipmi_request_added); 1050 msleep(sc->ipmi_kthread, &sc->ipmi_requests_lock, 0, 1051 "ipmi_wait", 0); 1052 } 1053 IPMI_UNLOCK(sc); 1054 if (sc->ipmi_irq) 1055 bus_teardown_intr(dev, sc->ipmi_irq_res, sc->ipmi_irq); 1056 1057 ipmi_release_resources(dev); 1058 mtx_destroy(&sc->ipmi_io_lock); 1059 mtx_destroy(&sc->ipmi_requests_lock); 1060 return (0); 1061 } 1062 1063 void 1064 ipmi_release_resources(device_t dev) 1065 { 1066 struct ipmi_softc *sc; 1067 int i; 1068 1069 sc = device_get_softc(dev); 1070 if (sc->ipmi_irq) 1071 bus_teardown_intr(dev, sc->ipmi_irq_res, sc->ipmi_irq); 1072 if (sc->ipmi_irq_res) 1073 bus_release_resource(dev, SYS_RES_IRQ, sc->ipmi_irq_rid, 1074 sc->ipmi_irq_res); 1075 for (i = 0; i < MAX_RES; i++) 1076 if (sc->ipmi_io_res[i]) 1077 bus_release_resource(dev, sc->ipmi_io_type, 1078 sc->ipmi_io_rid + i, sc->ipmi_io_res[i]); 1079 } 1080 1081 /* XXX: Why? */ 1082 static void 1083 ipmi_unload(void *arg) 1084 { 1085 device_t * devs; 1086 int count; 1087 int i; 1088 1089 if (devclass_get_devices(devclass_find("ipmi"), &devs, &count) != 0) 1090 return; 1091 for (i = 0; i < count; i++) 1092 device_delete_child(device_get_parent(devs[i]), devs[i]); 1093 free(devs, M_TEMP); 1094 } 1095 SYSUNINIT(ipmi_unload, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipmi_unload, NULL); 1096 1097 #ifdef IMPI_DEBUG 1098 static void 1099 dump_buf(u_char *data, int len) 1100 { 1101 char buf[20]; 1102 char line[1024]; 1103 char temp[30]; 1104 int count = 0; 1105 int i=0; 1106 1107 printf("Address %p len %d\n", data, len); 1108 if (len > 256) 1109 len = 256; 1110 line[0] = '\000'; 1111 for (; len > 0; len--, data++) { 1112 sprintf(temp, "%02x ", *data); 1113 strcat(line, temp); 1114 if (*data >= ' ' && *data <= '~') 1115 buf[count] = *data; 1116 else if (*data >= 'A' && *data <= 'Z') 1117 buf[count] = *data; 1118 else 1119 buf[count] = '.'; 1120 if (++count == 16) { 1121 buf[count] = '\000'; 1122 count = 0; 1123 printf(" %3x %s %s\n", i, line, buf); 1124 i+=16; 1125 line[0] = '\000'; 1126 } 1127 } 1128 buf[count] = '\000'; 1129 1130 for (; count != 16; count++) { 1131 strcat(line, " "); 1132 } 1133 printf(" %3x %s %s\n", i, line, buf); 1134 } 1135 #endif 1136