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