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