summaryrefslogtreecommitdiff
path: root/usr/src/lib/libipmi/common/ipmi_bmc.c
blob: b6c4076a56182a3bb8c4c4f555b52bfa2be150ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2012 Joyent, Inc.  All rights reserved.
 */

#include <errno.h>
#include <fcntl.h>
#include <libipmi.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stropts.h>
#include <unistd.h>

#include <sys/ipmi.h>

#include "ipmi_impl.h"

/*
 * IPMI transport for the local BMC at /dev/ipmi0.
 */

typedef struct ipmi_bmc {
	ipmi_handle_t	*ib_ihp;	/* ipmi handle */
	int		ib_fd;		/* /dev/ipmi0 filedescriptor */
	uint32_t	ib_msgseq;	/* message sequence number */
	uint8_t		*ib_msg;	/* message buffer */
	size_t		ib_msglen;	/* size of message buffer */
} ipmi_bmc_t;

#define	BMC_DEV	"/dev/ipmi0"

static void
ipmi_bmc_close(void *data)
{
	ipmi_bmc_t *ibp = data;

	ipmi_free(ibp->ib_ihp, ibp->ib_msg);

	(void) close(ibp->ib_fd);

	ipmi_free(ibp->ib_ihp, ibp);
}

/*ARGSUSED*/
static void *
ipmi_bmc_open(ipmi_handle_t *ihp, nvlist_t *params)
{
	ipmi_bmc_t *ibp;

	if ((ibp = ipmi_zalloc(ihp, sizeof (ipmi_bmc_t))) == NULL)
		return (NULL);
	ibp->ib_ihp = ihp;

	/* open /dev/ipmi0 */
	if ((ibp->ib_fd = open(BMC_DEV, O_RDWR)) < 0) {
		ipmi_free(ihp, ibp);
		(void) ipmi_set_error(ihp, EIPMI_BMC_OPEN_FAILED, "%s",
		    strerror(errno));
		return (NULL);
	}

	if ((ibp->ib_msg = (uint8_t *)ipmi_zalloc(ihp, BUFSIZ)) == NULL) {
		ipmi_bmc_close(ibp);
		return (NULL);
	}
	ibp->ib_msglen = BUFSIZ;

	return (ibp);
}

static int
ipmi_bmc_send(void *data, ipmi_cmd_t *cmd, ipmi_cmd_t *response,
    int *completion)
{
	ipmi_bmc_t *ibp = data;
	struct ipmi_req req;
	struct ipmi_recv recv;
	struct ipmi_addr addr;
	fd_set rset;
	struct ipmi_system_interface_addr bmc_addr;

	bmc_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	bmc_addr.channel = IPMI_BMC_CHANNEL;
	bmc_addr.lun = cmd->ic_lun;

	(void) memset(&req, 0, sizeof (struct ipmi_req));

	req.addr = (unsigned char *) &bmc_addr;
	req.addr_len = sizeof (bmc_addr);

	req.msgid = ibp->ib_msgseq++;
	req.msg.netfn = cmd->ic_netfn;
	req.msg.cmd = cmd->ic_cmd;
	req.msg.data = cmd->ic_data;
	req.msg.data_len = cmd->ic_dlen;

	if (ioctl(ibp->ib_fd, IPMICTL_SEND_COMMAND, &req) < 0) {
		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_PUTMSG, "%s",
		    strerror(errno));
		return (-1);
	}

	/* get the response from the BMC */

	FD_ZERO(&rset);
	FD_SET(ibp->ib_fd, &rset);

	if (select(ibp->ib_fd + 1, &rset, NULL, NULL, NULL) < 0) {
		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_GETMSG, "%s",
		    strerror(errno));
		return (-1);
	}
	if (FD_ISSET(ibp->ib_fd, &rset) == 0) {
		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_GETMSG, "%s",
		    "No data available");
		return (-1);
	}

	recv.addr = (unsigned char *) &addr;
	recv.addr_len = sizeof (addr);
	recv.msg.data = (unsigned char *)ibp->ib_msg;
	recv.msg.data_len = ibp->ib_msglen;

	/* get data */
	if (ioctl(ibp->ib_fd, IPMICTL_RECEIVE_MSG_TRUNC, &recv) < 0) {
		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_GETMSG, "%s",
		    strerror(errno));
		return (-1);
	}

	if (recv.recv_type != IPMI_RESPONSE_RECV_TYPE) {
		(void) ipmi_set_error(ibp->ib_ihp, EIPMI_BMC_RESPONSE,
		    "unknown BMC message type %d", recv.recv_type);
		return (-1);
	}

	response->ic_netfn = recv.msg.netfn;
	/* The lun is not returned in addr, return the lun passed in */
	response->ic_lun = cmd->ic_lun;
	response->ic_cmd = recv.msg.cmd;
	if (recv.msg.data[0] != 0) {
		*completion = recv.msg.data[0];
		response->ic_dlen = 0;
		response->ic_data = NULL;
	} else {
		*completion = 0;
		response->ic_dlen = (recv.msg.data_len > 0) ?
		    recv.msg.data_len - 1 : 0;
		response->ic_data = &(recv.msg.data[1]);
	}

	return (0);
}

ipmi_transport_t ipmi_transport_bmc = {
	ipmi_bmc_open,
	ipmi_bmc_close,
	ipmi_bmc_send
};