xref: /illumos-gate/usr/src/lib/libipmi/common/libipmi.c (revision 7800901e60d340b6af88e94a2149805dcfcaaf56)
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 	ipmi_sdr_clear(ihp);
74 	ipmi_user_clear(ihp);
75 	free(ihp);
76 }
77 
78 /*
79  * See section 5.2 for a description of the completion codes.
80  */
81 static struct ipmi_err_conv {
82 	int	bmc_err;
83 	int	ipmi_err;
84 } ipmi_errtable[] = {
85 	{ 0xC0,			EIPMI_BUSY },
86 	{ 0xC1,			EIPMI_INVALID_COMMAND },
87 	{ 0xC2,			EIPMI_INVALID_COMMAND },
88 	{ 0xC3,			EIPMI_COMMAND_TIMEOUT },
89 	{ 0xC4,			EIPMI_NOSPACE },
90 	{ 0xC5,			EIPMI_INVALID_RESERVATION },
91 	{ 0xC6,			EIPMI_INVALID_REQUEST },
92 	{ 0xC7,			EIPMI_INVALID_REQUEST },
93 	{ 0xC8,			EIPMI_INVALID_REQUEST },
94 	{ 0xC9,			EIPMI_INVALID_REQUEST },
95 	{ 0xCA,			EIPMI_DATA_LENGTH_EXCEEDED },
96 	{ 0xCB,			EIPMI_NOT_PRESENT },
97 	{ 0xCC,			EIPMI_INVALID_REQUEST },
98 	{ 0xCD,			EIPMI_INVALID_COMMAND },
99 	{ 0xCE,			EIPMI_UNAVAILABLE },
100 	{ 0xCF,			EIPMI_UNAVAILABLE },
101 	{ 0xD0,			EIPMI_BUSY },
102 	{ 0xD1,			EIPMI_BUSY },
103 	{ 0xD2,			EIPMI_BUSY },
104 	{ 0xD3,			EIPMI_NOT_PRESENT },
105 	{ 0xD4,			EIPMI_ACCESS },
106 	{ 0xD5,			EIPMI_UNAVAILABLE },
107 	{ 0xD6,			EIPMI_UNAVAILABLE },
108 	{ 0xFF,			EIPMI_UNSPECIFIED },
109 	{ BMC_IPMI_OEM_FAILURE_SENDBMC,	EIPMI_SEND_FAILED },
110 };
111 
112 #define	IPMI_ERROR_COUNT \
113 	(sizeof (ipmi_errtable) / sizeof (ipmi_errtable[0]))
114 
115 ipmi_cmd_t *
116 ipmi_send(ipmi_handle_t *ihp, ipmi_cmd_t *cmd)
117 {
118 	int completion;
119 	int i;
120 
121 	if (ihp->ih_transport->it_send(ihp->ih_tdata, cmd, &ihp->ih_response,
122 	    &completion) != 0)
123 		return (NULL);
124 
125 	if (completion != 0) {
126 		for (i = 0; i < IPMI_ERROR_COUNT; i++) {
127 			if (completion == ipmi_errtable[i].bmc_err) {
128 				(void) ipmi_set_error(ihp,
129 				    ipmi_errtable[i].ipmi_err,
130 				    "IPMI completion code 0x%x", completion);
131 				return (NULL);
132 			}
133 		}
134 
135 		(void) ipmi_set_error(ihp, EIPMI_UNKNOWN,
136 		    "IPMI completion code 0x%x", completion);
137 		return (NULL);
138 	}
139 
140 	return (&ihp->ih_response);
141 }
142