xref: /titanic_51/usr/src/lib/libipmi/common/libipmi.c (revision 283bfb4d4abcf7382b46f32663005b883ee2e3e0)
19113a79cSeschrock /*
29113a79cSeschrock  * CDDL HEADER START
39113a79cSeschrock  *
49113a79cSeschrock  * The contents of this file are subject to the terms of the
59113a79cSeschrock  * Common Development and Distribution License (the "License").
69113a79cSeschrock  * You may not use this file except in compliance with the License.
79113a79cSeschrock  *
89113a79cSeschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99113a79cSeschrock  * or http://www.opensolaris.org/os/licensing.
109113a79cSeschrock  * See the License for the specific language governing permissions
119113a79cSeschrock  * and limitations under the License.
129113a79cSeschrock  *
139113a79cSeschrock  * When distributing Covered Code, include this CDDL HEADER in each
149113a79cSeschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159113a79cSeschrock  * If applicable, add the following below this CDDL HEADER, with the
169113a79cSeschrock  * fields enclosed by brackets "[]" replaced with your own identifying
179113a79cSeschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
189113a79cSeschrock  *
199113a79cSeschrock  * CDDL HEADER END
209113a79cSeschrock  */
219113a79cSeschrock /*
222eeaed14Srobj  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
239113a79cSeschrock  * Use is subject to license terms.
249113a79cSeschrock  */
259113a79cSeschrock 
269113a79cSeschrock #include <libipmi.h>
279113a79cSeschrock #include <string.h>
289113a79cSeschrock 
299113a79cSeschrock #include <sys/bmc_intf.h>
309113a79cSeschrock 
319113a79cSeschrock #include "ipmi_impl.h"
329113a79cSeschrock 
339113a79cSeschrock ipmi_handle_t *
349113a79cSeschrock ipmi_open(int *errp, char **msg)
359113a79cSeschrock {
369113a79cSeschrock 	ipmi_handle_t *ihp;
372eeaed14Srobj 	static char errmsg[48];
389113a79cSeschrock 
399113a79cSeschrock 	if (msg)
409113a79cSeschrock 		*msg = NULL;
419113a79cSeschrock 
429113a79cSeschrock 	if ((ihp = calloc(sizeof (ipmi_handle_t), 1)) == NULL) {
439113a79cSeschrock 		*errp = EIPMI_NOMEM;
449113a79cSeschrock 		if (msg)
459113a79cSeschrock 			*msg = "memory allocation failure";
469113a79cSeschrock 		return (NULL);
479113a79cSeschrock 	}
489113a79cSeschrock 
499113a79cSeschrock 	/* /dev/bmc is the only currently available transport */
509113a79cSeschrock 	ihp->ih_transport = &ipmi_transport_bmc;
519113a79cSeschrock 
529113a79cSeschrock 	ihp->ih_retries = 3;
539113a79cSeschrock 
542eeaed14Srobj 	if ((ihp->ih_tdata = ihp->ih_transport->it_open(ihp)) == NULL ||
552eeaed14Srobj 	    ipmi_sdr_init(ihp) != 0 || ipmi_entity_init(ihp) != 0) {
569113a79cSeschrock 		*errp = ihp->ih_errno;
579113a79cSeschrock 		if (msg) {
582eeaed14Srobj 			(void) strncpy(errmsg, ipmi_errmsg(ihp), 47);
592eeaed14Srobj 			errmsg[47] = '\0';
602eeaed14Srobj 			*msg = errmsg;
619113a79cSeschrock 		}
629113a79cSeschrock 		ipmi_close(ihp);
639113a79cSeschrock 		return (NULL);
649113a79cSeschrock 	}
659113a79cSeschrock 
669113a79cSeschrock 	return (ihp);
679113a79cSeschrock }
689113a79cSeschrock 
699113a79cSeschrock void
709113a79cSeschrock ipmi_close(ipmi_handle_t *ihp)
719113a79cSeschrock {
729113a79cSeschrock 	if (ihp->ih_transport && ihp->ih_tdata)
739113a79cSeschrock 		ihp->ih_transport->it_close(ihp->ih_tdata);
742eeaed14Srobj 	ipmi_free(ihp, ihp->ih_deviceid);
752eeaed14Srobj 	ipmi_free(ihp, ihp->ih_firmware_rev);
761af98250Seschrock 	ipmi_user_clear(ihp);
772eeaed14Srobj 	ipmi_sdr_fini(ihp);
782eeaed14Srobj 	ipmi_entity_fini(ihp);
799113a79cSeschrock 	free(ihp);
809113a79cSeschrock }
819113a79cSeschrock 
829113a79cSeschrock /*
839113a79cSeschrock  * See section 5.2 for a description of the completion codes.
849113a79cSeschrock  */
859113a79cSeschrock static struct ipmi_err_conv {
869113a79cSeschrock 	int	bmc_err;
879113a79cSeschrock 	int	ipmi_err;
889113a79cSeschrock } ipmi_errtable[] = {
899113a79cSeschrock 	{ 0xC0,			EIPMI_BUSY },
909113a79cSeschrock 	{ 0xC1,			EIPMI_INVALID_COMMAND },
919113a79cSeschrock 	{ 0xC2,			EIPMI_INVALID_COMMAND },
929113a79cSeschrock 	{ 0xC3,			EIPMI_COMMAND_TIMEOUT },
939113a79cSeschrock 	{ 0xC4,			EIPMI_NOSPACE },
949113a79cSeschrock 	{ 0xC5,			EIPMI_INVALID_RESERVATION },
959113a79cSeschrock 	{ 0xC6,			EIPMI_INVALID_REQUEST },
969113a79cSeschrock 	{ 0xC7,			EIPMI_INVALID_REQUEST },
979113a79cSeschrock 	{ 0xC8,			EIPMI_INVALID_REQUEST },
989113a79cSeschrock 	{ 0xC9,			EIPMI_INVALID_REQUEST },
999113a79cSeschrock 	{ 0xCA,			EIPMI_DATA_LENGTH_EXCEEDED },
1009113a79cSeschrock 	{ 0xCB,			EIPMI_NOT_PRESENT },
1019113a79cSeschrock 	{ 0xCC,			EIPMI_INVALID_REQUEST },
1029113a79cSeschrock 	{ 0xCD,			EIPMI_INVALID_COMMAND },
1039113a79cSeschrock 	{ 0xCE,			EIPMI_UNAVAILABLE },
1049113a79cSeschrock 	{ 0xCF,			EIPMI_UNAVAILABLE },
1059113a79cSeschrock 	{ 0xD0,			EIPMI_BUSY },
1069113a79cSeschrock 	{ 0xD1,			EIPMI_BUSY },
1079113a79cSeschrock 	{ 0xD2,			EIPMI_BUSY },
1089113a79cSeschrock 	{ 0xD3,			EIPMI_NOT_PRESENT },
1099113a79cSeschrock 	{ 0xD4,			EIPMI_ACCESS },
1109113a79cSeschrock 	{ 0xD5,			EIPMI_UNAVAILABLE },
1119113a79cSeschrock 	{ 0xD6,			EIPMI_UNAVAILABLE },
1129113a79cSeschrock 	{ 0xFF,			EIPMI_UNSPECIFIED },
1139113a79cSeschrock 	{ BMC_IPMI_OEM_FAILURE_SENDBMC,	EIPMI_SEND_FAILED },
1149113a79cSeschrock };
1159113a79cSeschrock 
1169113a79cSeschrock #define	IPMI_ERROR_COUNT \
1179113a79cSeschrock 	(sizeof (ipmi_errtable) / sizeof (ipmi_errtable[0]))
1189113a79cSeschrock 
1199113a79cSeschrock ipmi_cmd_t *
1209113a79cSeschrock ipmi_send(ipmi_handle_t *ihp, ipmi_cmd_t *cmd)
1219113a79cSeschrock {
1229113a79cSeschrock 	int i;
1239113a79cSeschrock 
1249113a79cSeschrock 	if (ihp->ih_transport->it_send(ihp->ih_tdata, cmd, &ihp->ih_response,
125*283bfb4dSEric Schrock 	    &ihp->ih_completion) != 0)
1269113a79cSeschrock 		return (NULL);
1279113a79cSeschrock 
128*283bfb4dSEric Schrock 	if (ihp->ih_completion != 0) {
1299113a79cSeschrock 		for (i = 0; i < IPMI_ERROR_COUNT; i++) {
130*283bfb4dSEric Schrock 			if (ihp->ih_completion == ipmi_errtable[i].bmc_err) {
1319113a79cSeschrock 				(void) ipmi_set_error(ihp,
1329113a79cSeschrock 				    ipmi_errtable[i].ipmi_err,
133*283bfb4dSEric Schrock 				    "IPMI completion code 0x%x",
134*283bfb4dSEric Schrock 				    ihp->ih_completion);
1359113a79cSeschrock 				return (NULL);
1369113a79cSeschrock 			}
1379113a79cSeschrock 		}
1389113a79cSeschrock 
1399113a79cSeschrock 		(void) ipmi_set_error(ihp, EIPMI_UNKNOWN,
140*283bfb4dSEric Schrock 		    "IPMI completion code 0x%x", ihp->ih_completion);
1419113a79cSeschrock 		return (NULL);
1429113a79cSeschrock 	}
1439113a79cSeschrock 
1449113a79cSeschrock 	return (&ihp->ih_response);
1459113a79cSeschrock }
146