xref: /titanic_52/usr/src/lib/libipmi/common/ipmi_bmc.c (revision 2a8d6eba033e4713ab12b61178f0513f1f075482)
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 <errno.h>
29 #include <fcntl.h>
30 #include <libipmi.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stropts.h>
36 #include <unistd.h>
37 
38 #include <sys/bmc_intf.h>
39 
40 #include "ipmi_impl.h"
41 
42 /*
43  * IPMI transport for /dev/bmc
44  */
45 
46 typedef struct ipmi_bmc {
47 	ipmi_handle_t	*ib_ihp;	/* ipmi handle */
48 	int		ib_fd;		/* /dev/bmc filedescriptor */
49 	uint32_t	ib_msgseq;	/* message sequence number */
50 	bmc_msg_t	*ib_msg;	/* message buffer */
51 	size_t		ib_msglen;	/* size of message buffer */
52 } ipmi_bmc_t;
53 
54 #define	BMC_DEV	"/dev/bmc"
55 
56 static void
57 ipmi_bmc_close(void *data)
58 {
59 	ipmi_bmc_t *ibp = data;
60 
61 	ipmi_free(ibp->ib_ihp, ibp->ib_msg);
62 
63 	(void) close(ibp->ib_fd);
64 
65 	ipmi_free(ibp->ib_ihp, ibp);
66 }
67 
68 static void *
69 ipmi_bmc_open(ipmi_handle_t *ihp)
70 {
71 	ipmi_bmc_t *ibp;
72 
73 	if ((ibp = ipmi_zalloc(ihp, sizeof (ipmi_bmc_t))) == NULL)
74 		return (NULL);
75 	ibp->ib_ihp = ihp;
76 
77 	/* open /dev/bmc */
78 	if ((ibp->ib_fd = open(BMC_DEV, O_RDWR)) < 0) {
79 		ipmi_free(ihp, ibp);
80 		(void) ipmi_set_error(ihp, EIPMI_BMC_OPEN_FAILED, "%s",
81 		    strerror(errno));
82 		return (NULL);
83 	}
84 
85 	if ((ibp->ib_msg = (bmc_msg_t *)ipmi_zalloc(ihp, BUFSIZ)) == NULL) {
86 		ipmi_bmc_close(ibp);
87 		return (NULL);
88 	}
89 	ibp->ib_msglen = BUFSIZ;
90 
91 	return (ibp);
92 }
93 
94 static int
95 ipmi_bmc_send(void *data, ipmi_cmd_t *cmd, ipmi_cmd_t *response,
96     int *completion)
97 {
98 	ipmi_bmc_t *ibp = data;
99 	struct strbuf sb;
100 	int flags = 0;
101 	size_t msgsz;
102 	bmc_msg_t *msg;
103 	bmc_req_t *bmcreq;
104 	bmc_rsp_t *bmcrsp;
105 
106 	/*
107 	 * The length of the message structure is equal to the size of the
108 	 * bmc_req_t structure, PLUS any additional data space in excess of
109 	 * the data space already reserved in the data member + <n> for
110 	 * the rest of the members in the bmc_msg_t structure.
111 	 */
112 	msgsz = offsetof(bmc_msg_t, msg) + sizeof (bmc_req_t) +
113 	    ((cmd->ic_dlen > SEND_MAX_PAYLOAD_SIZE) ?
114 	    (cmd->ic_dlen - SEND_MAX_PAYLOAD_SIZE) : 0);
115 
116 	/* construct and send the message */
117 	if ((msg = ipmi_zalloc(ibp->ib_ihp, msgsz)) == NULL)
118 		return (-1);
119 	bmcreq = (bmc_req_t *)&msg->msg[0];
120 
121 	msg->m_type = BMC_MSG_REQUEST;
122 	msg->m_id = ibp->ib_msgseq++;
123 	bmcreq->fn = cmd->ic_netfn;
124 	bmcreq->lun = cmd->ic_lun;
125 	bmcreq->cmd = cmd->ic_cmd;
126 	bmcreq->datalength = cmd->ic_dlen;
127 	(void) memcpy(bmcreq->data, cmd->ic_data, cmd->ic_dlen);
128 	sb.len = msgsz;
129 	sb.buf = (char *)msg;
130 
131 	if (putmsg(ibp->ib_fd, NULL, &sb, 0) < 0) {
132 		ipmi_free(ibp->ib_ihp, msg);
133 		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_PUTMSG, "%s",
134 		    strerror(errno));
135 		return (-1);
136 	}
137 
138 	ipmi_free(ibp->ib_ihp, msg);
139 
140 	/* get the response from the BMC */
141 	sb.buf = (char *)ibp->ib_msg;
142 	sb.maxlen = ibp->ib_msglen;
143 
144 	if (getmsg(ibp->ib_fd, NULL, &sb, &flags) < 0) {
145 		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_GETMSG, "%s",
146 		    strerror(errno));
147 		return (-1);
148 	}
149 
150 	switch (ibp->ib_msg->m_type) {
151 	case BMC_MSG_RESPONSE:
152 		bmcrsp = (bmc_rsp_t *)&ibp->ib_msg->msg[0];
153 
154 		response->ic_netfn = bmcrsp->fn;
155 		response->ic_lun = bmcrsp->lun;
156 		response->ic_cmd = bmcrsp->cmd;
157 		if (bmcrsp->ccode != 0) {
158 			*completion = bmcrsp->ccode;
159 			response->ic_dlen = 0;
160 			response->ic_data = NULL;
161 		} else {
162 			*completion = 0;
163 			response->ic_dlen = bmcrsp->datalength;
164 			response->ic_data = bmcrsp->data;
165 		}
166 		break;
167 
168 	case BMC_MSG_ERROR:
169 		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_RESPONSE, "%s",
170 		    strerror(ibp->ib_msg->msg[0]));
171 		return (-1);
172 
173 	default:
174 		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_RESPONSE,
175 		    "unknown BMC message type %d", ibp->ib_msg->m_type);
176 		return (-1);
177 	}
178 
179 	return (0);
180 }
181 
182 ipmi_transport_t ipmi_transport_bmc = {
183 	ipmi_bmc_open,
184 	ipmi_bmc_close,
185 	ipmi_bmc_send
186 };
187