Deleted Added
sdiff udiff text old ( 257421 ) new ( 278321 )
full compact
1/*-
2 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ipmi/ipmi.c 278321 2015-02-06 16:45:10Z jhb $");
29
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/kernel.h>
36#include <sys/malloc.h>

--- 7 unchanged lines hidden (view full) ---

44#ifdef LOCAL_MODULE
45#include <ipmi.h>
46#include <ipmivars.h>
47#else
48#include <sys/ipmi.h>
49#include <dev/ipmi/ipmivars.h>
50#endif
51
52/*
53 * Driver request structures are allocated on the stack via alloca() to
54 * avoid calling malloc(), especially for the watchdog handler.
55 * To avoid too much stack growth, a previously allocated structure can
56 * be reused via IPMI_INIT_DRIVER_REQUEST(), but the caller should ensure
57 * that there is adequate reply/request space in the original allocation.
58 */
59#define IPMI_INIT_DRIVER_REQUEST(req, addr, cmd, reqlen, replylen) \
60 bzero((req), sizeof(struct ipmi_request)); \
61 ipmi_init_request((req), NULL, 0, (addr), (cmd), (reqlen), (replylen))
62
63#define IPMI_ALLOC_DRIVER_REQUEST(req, addr, cmd, reqlen, replylen) \
64 (req) = __builtin_alloca(sizeof(struct ipmi_request) + \
65 (reqlen) + (replylen)); \
66 IPMI_INIT_DRIVER_REQUEST((req), (addr), (cmd), (reqlen), \
67 (replylen))
68
69#ifdef IPMB
70static int ipmi_ipmb_checksum(u_char, int);
71static int ipmi_ipmb_send_message(device_t, u_char, u_char, u_char,
72 u_char, u_char, int)
73#endif
74
75static d_ioctl_t ipmi_ioctl;
76static d_poll_t ipmi_poll;

--- 116 unchanged lines hidden (view full) ---

193 ipmi_purge_completed_requests(dev);
194
195 /*
196 * If we still have outstanding requests, they must be stuck
197 * in an interface driver, so wait for those to drain.
198 */
199 dev->ipmi_closing = 1;
200 while (dev->ipmi_requests > 0) {
201 msleep(&dev->ipmi_requests, &sc->ipmi_requests_lock,
202 PWAIT, "ipmidrain", 0);
203 ipmi_purge_completed_requests(dev);
204 }
205 }
206 sc->ipmi_opened--;
207 IPMI_UNLOCK(sc);
208
209 /* Cleanup. */
210 free(dev, M_IPMI);

--- 16 unchanged lines hidden (view full) ---

227ipmi_ipmb_send_message(device_t dev, u_char channel, u_char netfn,
228 u_char command, u_char seq, u_char *data, int data_len)
229{
230 struct ipmi_softc *sc = device_get_softc(dev);
231 struct ipmi_request *req;
232 u_char slave_addr = 0x52;
233 int error;
234
235 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
236 IPMI_SEND_MSG, data_len + 8, 0);
237 req->ir_request[0] = channel;
238 req->ir_request[1] = slave_addr;
239 req->ir_request[2] = IPMI_ADDR(netfn, 0);
240 req->ir_request[3] = ipmi_ipmb_checksum(&req->ir_request[1], 2);
241 req->ir_request[4] = sc->ipmi_address;
242 req->ir_request[5] = IPMI_ADDR(seq, sc->ipmi_lun);
243 req->ir_request[6] = command;
244
245 bcopy(data, &req->ir_request[7], data_len);
246 temp[data_len + 7] = ipmi_ipmb_checksum(&req->ir_request[4],
247 data_len + 3);
248
249 ipmi_submit_driver_request(sc, req);
250 error = req->ir_error;
251
252 return (error);
253}
254
255static int
256ipmi_handle_attn(struct ipmi_softc *sc)
257{
258 struct ipmi_request *req;
259 int error;
260
261 device_printf(sc->ipmi_dev, "BMC has a message\n");
262 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
263 IPMI_GET_MSG_FLAGS, 0, 1);
264
265 ipmi_submit_driver_request(sc, req);
266
267 if (req->ir_error == 0 && req->ir_compcode == 0) {
268 if (req->ir_reply[0] & IPMI_MSG_BUFFER_FULL) {
269 device_printf(sc->ipmi_dev, "message buffer full");
270 }
271 if (req->ir_reply[0] & IPMI_WDT_PRE_TIMEOUT) {
272 device_printf(sc->ipmi_dev,
273 "watchdog about to go off");
274 }
275 if (req->ir_reply[0] & IPMI_MSG_AVAILABLE) {
276 IPMI_ALLOC_DRIVER_REQUEST(req,
277 IPMI_ADDR(IPMI_APP_REQUEST, 0), IPMI_GET_MSG, 0,
278 16);
279
280 device_printf(sc->ipmi_dev, "throw out message ");
281 dump_buf(temp, 16);
282 }
283 }
284 error = req->ir_error;
285
286 return (error);
287}
288#endif
289
290#ifdef IPMICTL_SEND_COMMAND_32
291#define PTRIN(p) ((void *)(uintptr_t)(p))
292#define PTROUT(p) ((uintptr_t)(p))

--- 193 unchanged lines hidden (view full) ---

486#endif
487 return (0);
488}
489
490/*
491 * Request management.
492 */
493
494static __inline void
495ipmi_init_request(struct ipmi_request *req, struct ipmi_device *dev, long msgid,
496 uint8_t addr, uint8_t command, size_t requestlen, size_t replylen)
497{
498
499 req->ir_owner = dev;
500 req->ir_msgid = msgid;
501 req->ir_addr = addr;
502 req->ir_command = command;
503 if (requestlen) {
504 req->ir_request = (char *)&req[1];
505 req->ir_requestlen = requestlen;
506 }
507 if (replylen) {
508 req->ir_reply = (char *)&req[1] + requestlen;
509 req->ir_replybuflen = replylen;
510 }
511}
512
513/* Allocate a new request with request and reply buffers. */
514struct ipmi_request *
515ipmi_alloc_request(struct ipmi_device *dev, long msgid, uint8_t addr,
516 uint8_t command, size_t requestlen, size_t replylen)
517{
518 struct ipmi_request *req;
519
520 req = malloc(sizeof(struct ipmi_request) + requestlen + replylen,
521 M_IPMI, M_WAITOK | M_ZERO);
522 ipmi_init_request(req, dev, msgid, addr, command, requestlen, replylen);
523 return (req);
524}
525
526/* Free a request no longer in use. */
527void
528ipmi_free_request(struct ipmi_request *req)
529{
530

--- 18 unchanged lines hidden (view full) ---

549 dev = req->ir_owner;
550 TAILQ_INSERT_TAIL(&dev->ipmi_completed_requests, req, ir_link);
551 selwakeup(&dev->ipmi_select);
552 if (dev->ipmi_closing)
553 wakeup(&dev->ipmi_requests);
554 }
555}
556
557/* Perform an internal driver request. */
558int
559ipmi_submit_driver_request(struct ipmi_softc *sc, struct ipmi_request *req,
560 int timo)
561{
562
563 return (sc->ipmi_driver_request(sc, req, timo));
564}
565
566/*
567 * Helper routine for polled system interfaces that use
568 * ipmi_polled_enqueue_request() to queue requests. This request
569 * waits until there is a pending request and then returns the first
570 * request. If the driver is shutting down, it returns NULL.
571 */
572struct ipmi_request *
573ipmi_dequeue_request(struct ipmi_softc *sc)
574{
575 struct ipmi_request *req;
576
577 IPMI_LOCK_ASSERT(sc);
578
579 while (!sc->ipmi_detaching && TAILQ_EMPTY(&sc->ipmi_pending_requests))
580 cv_wait(&sc->ipmi_request_added, &sc->ipmi_requests_lock);
581 if (sc->ipmi_detaching)
582 return (NULL);
583
584 req = TAILQ_FIRST(&sc->ipmi_pending_requests);
585 TAILQ_REMOVE(&sc->ipmi_pending_requests, req, ir_link);
586 return (req);
587}
588

--- 17 unchanged lines hidden (view full) ---

606ipmi_set_watchdog(struct ipmi_softc *sc, unsigned int sec)
607{
608 struct ipmi_request *req;
609 int error;
610
611 if (sec > 0xffff / 10)
612 return (EINVAL);
613
614 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
615 IPMI_SET_WDOG, 6, 0);
616
617 if (sec) {
618 req->ir_request[0] = IPMI_SET_WD_TIMER_DONT_STOP
619 | IPMI_SET_WD_TIMER_SMS_OS;
620 req->ir_request[1] = IPMI_SET_WD_ACTION_RESET;
621 req->ir_request[2] = 0;
622 req->ir_request[3] = 0; /* Timer use */

--- 7 unchanged lines hidden (view full) ---

630 req->ir_request[4] = 0;
631 req->ir_request[5] = 0;
632 }
633
634 error = ipmi_submit_driver_request(sc, req, 0);
635 if (error)
636 device_printf(sc->ipmi_dev, "Failed to set watchdog\n");
637 else if (sec) {
638 IPMI_INIT_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
639 IPMI_RESET_WDOG, 0, 0);
640
641 error = ipmi_submit_driver_request(sc, req, 0);
642 if (error)
643 device_printf(sc->ipmi_dev,
644 "Failed to reset watchdog\n");
645 }
646
647 return (error);
648 /*
649 dump_watchdog(sc);
650 */
651}
652
653static void
654ipmi_wd_event(void *arg, unsigned int cmd, int *error)

--- 30 unchanged lines hidden (view full) ---

685 struct ipmi_request *req;
686 device_t dev;
687 int error, i;
688
689 config_intrhook_disestablish(&sc->ipmi_ich);
690 dev = sc->ipmi_dev;
691
692 /* Initialize interface-independent state. */
693 mtx_init(&sc->ipmi_requests_lock, "ipmi requests", NULL, MTX_DEF);
694 mtx_init(&sc->ipmi_io_lock, "ipmi io", NULL, MTX_DEF);
695 cv_init(&sc->ipmi_request_added, "ipmireq");
696 TAILQ_INIT(&sc->ipmi_pending_requests);
697
698 /* Initialize interface-dependent state. */
699 error = sc->ipmi_startup(sc);
700 if (error) {
701 device_printf(dev, "Failed to initialize interface: %d\n",
702 error);
703 return;
704 }
705
706 /* Send a GET_DEVICE_ID request. */
707 IPMI_ALLOC_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
708 IPMI_GET_DEVICE_ID, 0, 15);
709
710 error = ipmi_submit_driver_request(sc, req, MAX_TIMEOUT);
711 if (error == EWOULDBLOCK) {
712 device_printf(dev, "Timed out waiting for GET_DEVICE_ID\n");
713 return;
714 } else if (error) {
715 device_printf(dev, "Failed GET_DEVICE_ID: %d\n", error);
716 return;
717 } else if (req->ir_compcode != 0) {
718 device_printf(dev,
719 "Bad completion code for GET_DEVICE_ID: %d\n",
720 req->ir_compcode);
721 return;
722 } else if (req->ir_replylen < 5) {
723 device_printf(dev, "Short reply for GET_DEVICE_ID: %d\n",
724 req->ir_replylen);
725 return;
726 }
727
728 device_printf(dev, "IPMI device rev. %d, firmware rev. %d.%d%d, "
729 "version %d.%d\n",
730 req->ir_reply[1] & 0x0f,
731 req->ir_reply[2] & 0x7f, req->ir_reply[3] >> 4, req->ir_reply[3] & 0x0f,
732 req->ir_reply[4] & 0x0f, req->ir_reply[4] >> 4);
733
734 IPMI_INIT_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
735 IPMI_CLEAR_FLAGS, 1, 0);
736
737 ipmi_submit_driver_request(sc, req, 0);
738
739 /* XXX: Magic numbers */
740 if (req->ir_compcode == 0xc0) {
741 device_printf(dev, "Clear flags is busy\n");
742 }
743 if (req->ir_compcode == 0xc1) {
744 device_printf(dev, "Clear flags illegal\n");
745 }
746
747 for (i = 0; i < 8; i++) {
748 IPMI_INIT_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
749 IPMI_GET_CHANNEL_INFO, 1, 0);
750 req->ir_request[0] = i;
751
752 ipmi_submit_driver_request(sc, req, 0);
753
754 if (req->ir_compcode != 0)
755 break;
756 }
757 device_printf(dev, "Number of channels %d\n", i);
758
759 /* probe for watchdog */
760 IPMI_INIT_DRIVER_REQUEST(req, IPMI_ADDR(IPMI_APP_REQUEST, 0),
761 IPMI_GET_WDOG, 0, 0);
762
763 ipmi_submit_driver_request(sc, req, 0);
764
765 if (req->ir_compcode == 0x00) {
766 device_printf(dev, "Attached watchdog\n");
767 /* register the watchdog event handler */
768 sc->ipmi_watchdog_tag = EVENTHANDLER_REGISTER(watchdog_list,
769 ipmi_wd_event, sc, 0);
770 }
771
772 sc->ipmi_cdev = make_dev(&ipmi_cdevsw, device_get_unit(dev),
773 UID_ROOT, GID_OPERATOR, 0660, "ipmi%d", device_get_unit(dev));
774 if (sc->ipmi_cdev == NULL) {
775 device_printf(dev, "Failed to create cdev\n");
776 return;
777 }
778 sc->ipmi_cdev->si_drv1 = sc;

--- 50 unchanged lines hidden (view full) ---

829 }
830
831 /* XXX: should use shutdown callout I think. */
832 /* If the backend uses a kthread, shut it down. */
833 IPMI_LOCK(sc);
834 sc->ipmi_detaching = 1;
835 if (sc->ipmi_kthread) {
836 cv_broadcast(&sc->ipmi_request_added);
837 msleep(sc->ipmi_kthread, &sc->ipmi_requests_lock, 0,
838 "ipmi_wait", 0);
839 }
840 IPMI_UNLOCK(sc);
841 if (sc->ipmi_irq)
842 bus_teardown_intr(dev, sc->ipmi_irq_res, sc->ipmi_irq);
843
844 ipmi_release_resources(dev);
845 mtx_destroy(&sc->ipmi_io_lock);
846 mtx_destroy(&sc->ipmi_requests_lock);
847 return (0);
848}
849
850void
851ipmi_release_resources(device_t dev)
852{
853 struct ipmi_softc *sc;
854 int i;

--- 70 unchanged lines hidden ---