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