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