xref: /titanic_41/usr/src/lib/libipmi/common/libipmi.c (revision d29f5a711240f866521445b1656d114da090335e)
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 2008 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 	static char errmsg[48];
40 
41 	if (msg)
42 		*msg = NULL;
43 
44 	if ((ihp = calloc(sizeof (ipmi_handle_t), 1)) == NULL) {
45 		*errp = EIPMI_NOMEM;
46 		if (msg)
47 			*msg = "memory allocation failure";
48 		return (NULL);
49 	}
50 
51 	/* /dev/bmc is the only currently available transport */
52 	ihp->ih_transport = &ipmi_transport_bmc;
53 
54 	ihp->ih_retries = 3;
55 
56 	if ((ihp->ih_tdata = ihp->ih_transport->it_open(ihp)) == NULL ||
57 	    ipmi_sdr_init(ihp) != 0 || ipmi_entity_init(ihp) != 0) {
58 		*errp = ihp->ih_errno;
59 		if (msg) {
60 			(void) strncpy(errmsg, ipmi_errmsg(ihp), 47);
61 			errmsg[47] = '\0';
62 			*msg = errmsg;
63 		}
64 		ipmi_close(ihp);
65 		return (NULL);
66 	}
67 
68 	return (ihp);
69 }
70 
71 void
72 ipmi_close(ipmi_handle_t *ihp)
73 {
74 	if (ihp->ih_transport && ihp->ih_tdata)
75 		ihp->ih_transport->it_close(ihp->ih_tdata);
76 	ipmi_free(ihp, ihp->ih_deviceid);
77 	ipmi_free(ihp, ihp->ih_firmware_rev);
78 	ipmi_user_clear(ihp);
79 	ipmi_sdr_fini(ihp);
80 	ipmi_entity_fini(ihp);
81 	free(ihp);
82 }
83 
84 /*
85  * See section 5.2 for a description of the completion codes.
86  */
87 static struct ipmi_err_conv {
88 	int	bmc_err;
89 	int	ipmi_err;
90 } ipmi_errtable[] = {
91 	{ 0xC0,			EIPMI_BUSY },
92 	{ 0xC1,			EIPMI_INVALID_COMMAND },
93 	{ 0xC2,			EIPMI_INVALID_COMMAND },
94 	{ 0xC3,			EIPMI_COMMAND_TIMEOUT },
95 	{ 0xC4,			EIPMI_NOSPACE },
96 	{ 0xC5,			EIPMI_INVALID_RESERVATION },
97 	{ 0xC6,			EIPMI_INVALID_REQUEST },
98 	{ 0xC7,			EIPMI_INVALID_REQUEST },
99 	{ 0xC8,			EIPMI_INVALID_REQUEST },
100 	{ 0xC9,			EIPMI_INVALID_REQUEST },
101 	{ 0xCA,			EIPMI_DATA_LENGTH_EXCEEDED },
102 	{ 0xCB,			EIPMI_NOT_PRESENT },
103 	{ 0xCC,			EIPMI_INVALID_REQUEST },
104 	{ 0xCD,			EIPMI_INVALID_COMMAND },
105 	{ 0xCE,			EIPMI_UNAVAILABLE },
106 	{ 0xCF,			EIPMI_UNAVAILABLE },
107 	{ 0xD0,			EIPMI_BUSY },
108 	{ 0xD1,			EIPMI_BUSY },
109 	{ 0xD2,			EIPMI_BUSY },
110 	{ 0xD3,			EIPMI_NOT_PRESENT },
111 	{ 0xD4,			EIPMI_ACCESS },
112 	{ 0xD5,			EIPMI_UNAVAILABLE },
113 	{ 0xD6,			EIPMI_UNAVAILABLE },
114 	{ 0xFF,			EIPMI_UNSPECIFIED },
115 	{ BMC_IPMI_OEM_FAILURE_SENDBMC,	EIPMI_SEND_FAILED },
116 };
117 
118 #define	IPMI_ERROR_COUNT \
119 	(sizeof (ipmi_errtable) / sizeof (ipmi_errtable[0]))
120 
121 ipmi_cmd_t *
122 ipmi_send(ipmi_handle_t *ihp, ipmi_cmd_t *cmd)
123 {
124 	int completion;
125 	int i;
126 
127 	if (ihp->ih_transport->it_send(ihp->ih_tdata, cmd, &ihp->ih_response,
128 	    &completion) != 0)
129 		return (NULL);
130 
131 	if (completion != 0) {
132 		for (i = 0; i < IPMI_ERROR_COUNT; i++) {
133 			if (completion == ipmi_errtable[i].bmc_err) {
134 				(void) ipmi_set_error(ihp,
135 				    ipmi_errtable[i].ipmi_err,
136 				    "IPMI completion code 0x%x", completion);
137 				return (NULL);
138 			}
139 		}
140 
141 		(void) ipmi_set_error(ihp, EIPMI_UNKNOWN,
142 		    "IPMI completion code 0x%x", completion);
143 		return (NULL);
144 	}
145 
146 	return (&ihp->ih_response);
147 }
148