xref: /titanic_41/usr/src/lib/libipmi/common/libipmi.c (revision 1c42de6d020629af774dd9e9fc81be3f3ed9398e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <libipmi.h>
29 #include <string.h>
30 
31 #include <sys/bmc_intf.h>
32 
33 #include "ipmi_impl.h"
34 
35 ipmi_handle_t *
36 ipmi_open(int *errp, char **msg)
37 {
38 	ipmi_handle_t *ihp;
39 
40 	if (msg)
41 		*msg = NULL;
42 
43 	if ((ihp = calloc(sizeof (ipmi_handle_t), 1)) == NULL) {
44 		*errp = EIPMI_NOMEM;
45 		if (msg)
46 			*msg = "memory allocation failure";
47 		return (NULL);
48 	}
49 
50 	/* /dev/bmc is the only currently available transport */
51 	ihp->ih_transport = &ipmi_transport_bmc;
52 
53 	ihp->ih_retries = 3;
54 
55 	if ((ihp->ih_tdata = ihp->ih_transport->it_open(ihp)) == NULL) {
56 		*errp = ihp->ih_errno;
57 		if (msg) {
58 			if ((*msg = strdup(ipmi_errmsg(ihp))) == NULL)
59 				*msg = "memory allocation failure";
60 		}
61 		ipmi_close(ihp);
62 		return (NULL);
63 	}
64 
65 	return (ihp);
66 }
67 
68 void
69 ipmi_close(ipmi_handle_t *ihp)
70 {
71 	if (ihp->ih_transport && ihp->ih_tdata)
72 		ihp->ih_transport->it_close(ihp->ih_tdata);
73 	free(ihp);
74 }
75 
76 /*
77  * See section 5.2 for a description of the completion codes.
78  */
79 static struct ipmi_err_conv {
80 	int	bmc_err;
81 	int	ipmi_err;
82 } ipmi_errtable[] = {
83 	{ 0xC0,			EIPMI_BUSY },
84 	{ 0xC1,			EIPMI_INVALID_COMMAND },
85 	{ 0xC2,			EIPMI_INVALID_COMMAND },
86 	{ 0xC3,			EIPMI_COMMAND_TIMEOUT },
87 	{ 0xC4,			EIPMI_NOSPACE },
88 	{ 0xC5,			EIPMI_INVALID_RESERVATION },
89 	{ 0xC6,			EIPMI_INVALID_REQUEST },
90 	{ 0xC7,			EIPMI_INVALID_REQUEST },
91 	{ 0xC8,			EIPMI_INVALID_REQUEST },
92 	{ 0xC9,			EIPMI_INVALID_REQUEST },
93 	{ 0xCA,			EIPMI_DATA_LENGTH_EXCEEDED },
94 	{ 0xCB,			EIPMI_NOT_PRESENT },
95 	{ 0xCC,			EIPMI_INVALID_REQUEST },
96 	{ 0xCD,			EIPMI_INVALID_COMMAND },
97 	{ 0xCE,			EIPMI_UNAVAILABLE },
98 	{ 0xCF,			EIPMI_UNAVAILABLE },
99 	{ 0xD0,			EIPMI_BUSY },
100 	{ 0xD1,			EIPMI_BUSY },
101 	{ 0xD2,			EIPMI_BUSY },
102 	{ 0xD3,			EIPMI_NOT_PRESENT },
103 	{ 0xD4,			EIPMI_ACCESS },
104 	{ 0xD5,			EIPMI_UNAVAILABLE },
105 	{ 0xD6,			EIPMI_UNAVAILABLE },
106 	{ 0xFF,			EIPMI_UNSPECIFIED },
107 	{ BMC_IPMI_OEM_FAILURE_SENDBMC,	EIPMI_SEND_FAILED },
108 };
109 
110 #define	IPMI_ERROR_COUNT \
111 	(sizeof (ipmi_errtable) / sizeof (ipmi_errtable[0]))
112 
113 ipmi_cmd_t *
114 ipmi_send(ipmi_handle_t *ihp, ipmi_cmd_t *cmd)
115 {
116 	int completion;
117 	int i;
118 
119 	if (ihp->ih_transport->it_send(ihp->ih_tdata, cmd, &ihp->ih_response,
120 	    &completion) != 0)
121 		return (NULL);
122 
123 	if (completion != 0) {
124 		for (i = 0; i < IPMI_ERROR_COUNT; i++) {
125 			if (completion == ipmi_errtable[i].bmc_err) {
126 				(void) ipmi_set_error(ihp,
127 				    ipmi_errtable[i].ipmi_err,
128 				    "IPMI completion code 0x%x", completion);
129 				return (NULL);
130 			}
131 		}
132 
133 		(void) ipmi_set_error(ihp, EIPMI_UNKNOWN,
134 		    "IPMI completion code 0x%x", completion);
135 		return (NULL);
136 	}
137 
138 	return (&ihp->ih_response);
139 }
140