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