aboutsummaryrefslogtreecommitdiff
path: root/core/ipmi.c
blob: 30ea78aa4b5d8416c4aaccc9c8ee92285c4f947c (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
/* Copyright 2013-2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <bt.h>
#include <ipmi.h>
#include <opal.h>
#include <device.h>
#include <time.h>
#include <time-utils.h>

/* Sane default (2014/01/01) */
static time_t time = 1388494800;

static void ipmi_process_storage_resp(struct ipmi_msg *msg)
{
	uint32_t new_time;

	switch (msg->cmd) {
	case IPMI_GET_SEL_TIME_CMD:
		/*
		 * I couldn't find any mention of endianess in the IPMI spec,
		 * but ipmitool seemed to assume little endian?
		 */
		memcpy(&new_time, msg->resp_data, 4);
		time = le32_to_cpu(new_time);
		break;

	case IPMI_SET_SEL_TIME_CMD:
		/* Nothing to do in this case */
		break;

	default:
		printf("Unsupported/invalid IPMI storage command\n");
	}
}

static uint32_t time_result;
static int64_t ipmi_get_sel_time(void)
{
	struct ipmi_msg *msg = malloc(sizeof(struct ipmi_msg));

	if (!msg)
		return OPAL_HARDWARE;

	msg->cmd = IPMI_GET_SEL_TIME_CMD;
	msg->netfn = IPMI_NETFN_STORAGE_REQUEST;
	msg->req_data = NULL;
	msg->req_data_len = 0;
	msg->resp_data = (uint8_t *) &time_result;
	msg->resp_data_len = 4;
	if (bt_add_ipmi_msg_wait(msg))
		return -1;

	return time_result;
}

static int64_t ipmi_set_sel_time(uint32_t tv)
{
	struct ipmi_msg *msg = malloc(sizeof(struct ipmi_msg));

	if (!msg)
		return OPAL_HARDWARE;

	msg->cmd = IPMI_SET_SEL_TIME_CMD;
	msg->netfn = IPMI_NETFN_STORAGE_REQUEST;
	msg->req_data = (uint8_t *) &tv;
	msg->req_data_len = 4;
	msg->resp_data = NULL;
	msg->resp_data_len = 0;

	return bt_add_ipmi_msg_wait(msg);
}

static int64_t ipmi_opal_rtc_read(uint32_t *y_m_d,
				 uint64_t *h_m_s_m)
{
	struct tm tm;

	if (ipmi_get_sel_time() < 0)
		return OPAL_HARDWARE;

	gmtime_r(&time, &tm);
	tm_to_datetime(&tm, y_m_d, h_m_s_m);
	return OPAL_SUCCESS;
}

static int64_t ipmi_opal_rtc_write(uint32_t year_month_day,
				  uint64_t hour_minute_second_millisecond)
{
	time_t t;
	struct tm tm;

	datetime_to_tm(year_month_day, hour_minute_second_millisecond, &tm);
	t = mktime(&tm);
	t = cpu_to_le32(t);
	if (ipmi_set_sel_time(t))
		return OPAL_HARDWARE;

	return OPAL_SUCCESS;
}

static void ipmi_rtc_init(void)
{
	struct dt_node *np = dt_new(opal_node, "rtc");
	dt_add_property_strings(np, "compatible", "ibm,opal-rtc");

	opal_register(OPAL_RTC_READ, ipmi_opal_rtc_read, 2);
	opal_register(OPAL_RTC_WRITE, ipmi_opal_rtc_write, 2);
}

static void ipmi_cmd_done(struct ipmi_msg *msg)
{
	if (msg->cc != IPMI_CC_NO_ERROR) {
		prerror("IPMI: Got error response 0x%02x\n", msg->cc);
		goto out;
	}

	switch (msg->netfn) {
	case IPMI_NETFN_STORAGE_RESPONSE:
		ipmi_process_storage_resp(msg);
		break;

	case IPMI_NETFN_CHASSIS_RESPONSE:
		break;
	default:
		prerror("IPMI: Invalid IPMI function code in response\n");
	}

out:
	free(msg);
}

static uint8_t chassis_control;
int64_t ipmi_opal_chassis_control(uint64_t request)
{
	struct ipmi_msg *msg = zalloc(sizeof(struct ipmi_msg));

	if (!msg)
		return OPAL_HARDWARE;

	if (request > IPMI_CHASSIS_SOFT_SHUTDOWN)
		return OPAL_PARAMETER;

	chassis_control = request;

	msg->cmd = IPMI_CHASSIS_CONTROL_CMD;
	msg->netfn = IPMI_NETFN_CHASSIS_REQUEST;
	msg->req_data = (uint8_t *)&chassis_control;
	msg->req_data_len = sizeof(chassis_control);
	msg->resp_data = NULL;
	msg->resp_data_len = 0;

	prlog(PR_INFO, "IPMI: sending chassis control request %llu\n",
			request);

	return bt_add_ipmi_msg_wait(msg);
}

void ipmi_init(void)
{
	bt_init(ipmi_cmd_done);

	ipmi_rtc_init();
}