blob: 41d76b8986826eb88f68799c2e74c2d44b85f0bc [file] [log] [blame]
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +05301#include <cstdio>
2#include <string>
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -05003#include <arpa/inet.h>
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +05304#include <systemd/sd-bus.h>
5#include <mapper.h>
6#include <chrono>
Chris Austen41a4b312015-10-25 03:45:42 -05007#include "storagehandler.h"
8#include "storageaddsel.h"
Patrick Williams37af7332016-09-02 21:21:42 -05009#include "host-ipmid/ipmid-api.h"
Chris Austen41a4b312015-10-25 03:45:42 -050010
Chris Austenb4f5b922015-10-13 12:44:43 -050011void register_netfn_storage_functions() __attribute__((constructor));
12
13
14unsigned int g_sel_time = 0xFFFFFFFF;
Nan Li36c0cb62016-03-31 11:16:08 +080015extern unsigned short g_sel_reserve;
Chris Austenb4f5b922015-10-13 12:44:43 -050016
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053017constexpr auto time_manager_intf = "org.openbmc.TimeManager";
18constexpr auto time_manager_obj = "/org/openbmc/TimeManager";
19
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -050020ipmi_ret_t ipmi_storage_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
21 ipmi_request_t request, ipmi_response_t response,
Chris Austenb4f5b922015-10-13 12:44:43 -050022 ipmi_data_len_t data_len, ipmi_context_t context)
23{
24 printf("Handling STORAGE WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
25 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +080026 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenb4f5b922015-10-13 12:44:43 -050027 *data_len = 0;
28 return rc;
29}
30
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -050031ipmi_ret_t ipmi_storage_get_sel_time(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
32 ipmi_request_t request, ipmi_response_t response,
33 ipmi_data_len_t data_len, ipmi_context_t context)
34{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053035 using namespace std::chrono;
36
37 char *time_provider = nullptr;
38 const char* time_in_str = nullptr;
39 uint64_t host_time_usec = 0;
40 uint32_t resp = 0;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -050041 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austenb4f5b922015-10-13 12:44:43 -050042
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053043 sd_bus_message *reply = nullptr;
44 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -050045
46 printf("IPMI Handling GET-SEL-TIME\n");
47
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053048 auto bus = ipmid_get_sd_bus_connection();
49
50 auto rct = mapper_get_service(bus, time_manager_obj, &time_provider);
51 if (rct < 0) {
52 printf("Error [%s] getting bus name for time provider\n",
53 strerror(-rct));
54 rc = IPMI_CC_UNSPECIFIED_ERROR;
55 goto finish;
56 }
57
58 rct = sd_bus_call_method(bus,
59 time_provider,
60 time_manager_obj,
61 time_manager_intf,
62 "GetTime",
63 &bus_error,
64 &reply,
65 "s",
66 "host");
67 if (rct < 0) {
68 printf("Error [%s] getting time\n", strerror(-rct));
69 rc = IPMI_CC_UNSPECIFIED_ERROR;
70 goto finish;
71 }
72
73 rct = sd_bus_message_read(reply, "sx", &time_in_str, &host_time_usec);
74 if (rct < 0) {
75 fprintf(stderr, "Error [%s] parsing get-time response\n",
76 strerror(-rct));
77 rc = IPMI_CC_UNSPECIFIED_ERROR;
78 goto finish;
79 }
80
81 // Time is really long int but IPMI wants just uint32. This works okay until
82 // the number of seconds since 1970 overflows uint32 size.. Still a whole
83 // lot of time here to even think about that.
84 resp = duration_cast<seconds>(microseconds(host_time_usec)).count();
85 resp = htole32(resp);
86 printf("Host Time read:[%s] :: [%d]\n", time_in_str, resp);
87
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -050088 // From the IPMI Spec 2.0, response should be a 32-bit value
89 *data_len = sizeof(resp);
90
91 // Pack the actual response
92 memcpy(response, &resp, *data_len);
93
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053094finish:
95 sd_bus_error_free(&bus_error);
96 reply = sd_bus_message_unref(reply);
97 free(time_provider);
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -050098 return rc;
99}
100
101ipmi_ret_t ipmi_storage_set_sel_time(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
102 ipmi_request_t request, ipmi_response_t response,
Chris Austenb4f5b922015-10-13 12:44:43 -0500103 ipmi_data_len_t data_len, ipmi_context_t context)
104{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530105 char *time_provider = nullptr;
106 int time_rc = 0;
107 ipmi_ret_t rc = IPMI_CC_OK;
108
109 sd_bus_message *reply = nullptr;
110 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
111
Norman James82330442015-11-19 16:53:26 -0600112 uint32_t* secs = (uint32_t*)request;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530113 *data_len = 0;
Chris Austenb4f5b922015-10-13 12:44:43 -0500114
115 printf("Handling Set-SEL-Time:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
Norman James82330442015-11-19 16:53:26 -0600116 printf("Data: 0x%X]\n",*secs);
Chris Austenb4f5b922015-10-13 12:44:43 -0500117
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530118 auto bus = ipmid_get_sd_bus_connection();
Norman James82330442015-11-19 16:53:26 -0600119
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530120 auto rct = mapper_get_service(bus, time_manager_obj, &time_provider);
121 if (rct < 0) {
122 printf("Error [%s] getting bus name for time provider\n",
123 strerror(-rct));
124 rc = IPMI_CC_UNSPECIFIED_ERROR;
125 goto finish;
Norman James82330442015-11-19 16:53:26 -0600126 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530127
128 rct = sd_bus_call_method(bus,
129 time_provider,
130 time_manager_obj,
131 time_manager_intf,
132 "SetTime",
133 &bus_error,
134 &reply,
135 "ss",
136 "host",
137 std::to_string(le32toh(*secs)).c_str());
138
139 if (rct < 0) {
140 printf("Error [%s] setting time\n", strerror(-rct));
141 rc = IPMI_CC_UNSPECIFIED_ERROR;
142 goto finish;
143 }
144
145 rct = sd_bus_message_read(reply, "i", &time_rc);
146 if (rct < 0) {
147 fprintf(stderr, "Error [%s] parsing set-time response\n",
148 strerror(-rct));
149 rc = IPMI_CC_UNSPECIFIED_ERROR;
150 goto finish;
151 }
152
153 if (time_rc < 0) {
154 printf("Error setting time.");
Norman James82330442015-11-19 16:53:26 -0600155 rc = IPMI_CC_UNSPECIFIED_ERROR;
156 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530157
158finish:
159 sd_bus_error_free(&bus_error);
160 reply = sd_bus_message_unref(reply);
161 free(time_provider);
Chris Austenb4f5b922015-10-13 12:44:43 -0500162 return rc;
163}
164
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500165ipmi_ret_t ipmi_storage_get_sel_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
166 ipmi_request_t request, ipmi_response_t response,
Chris Austenb4f5b922015-10-13 12:44:43 -0500167 ipmi_data_len_t data_len, ipmi_context_t context)
168{
169
170 ipmi_ret_t rc = IPMI_CC_OK;
171 unsigned char buf[] = {0x51,0,0,0xff, 0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0x06};
172
173 printf("IPMI Handling GET-SEL-INFO\n");
174
175 *data_len = sizeof(buf);
176
177 // TODO There is plently of work here. The SEL DB needs to hold a bunch
178 // of things in a header. Items like Time Stamp, number of entries, etc
179 // This is one place where the dbus object with the SEL information could
180 // mimic what IPMI needs.
181
182 // Pack the actual response
183 memcpy(response, &buf, *data_len);
184
185 return rc;
186}
187
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500188ipmi_ret_t ipmi_storage_reserve_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
189 ipmi_request_t request, ipmi_response_t response,
Chris Austenb4f5b922015-10-13 12:44:43 -0500190 ipmi_data_len_t data_len, ipmi_context_t context)
191{
Chris Austenb4f5b922015-10-13 12:44:43 -0500192 ipmi_ret_t rc = IPMI_CC_OK;
193
Nan Li36c0cb62016-03-31 11:16:08 +0800194 // IPMI spec, Reservation ID, the value simply increases against each execution of reserve_sel command.
195 if( ++g_sel_reserve == 0)
196 g_sel_reserve = 1;
Chris Austenb4f5b922015-10-13 12:44:43 -0500197
Nan Li36c0cb62016-03-31 11:16:08 +0800198 printf("IPMI Handling RESERVE-SEL 0x%04x\n", g_sel_reserve);
Chris Austen41a4b312015-10-25 03:45:42 -0500199
Chris Austenb4f5b922015-10-13 12:44:43 -0500200 *data_len = sizeof(g_sel_reserve);
201
202 // Pack the actual response
203 memcpy(response, &g_sel_reserve, *data_len);
204
205 return rc;
206}
207
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500208ipmi_ret_t ipmi_storage_add_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
209 ipmi_request_t request, ipmi_response_t response,
Chris Austenb4f5b922015-10-13 12:44:43 -0500210 ipmi_data_len_t data_len, ipmi_context_t context)
211{
212
213 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austen41a4b312015-10-25 03:45:42 -0500214 ipmi_add_sel_request_t *p = (ipmi_add_sel_request_t*) request;
215 uint16_t recordid;
Chris Austenb4f5b922015-10-13 12:44:43 -0500216
Chris Austen313d95b2015-10-31 12:55:30 -0500217 recordid = ((uint16_t)p->eventdata[1] << 8) | p->eventdata[2];
Chris Austen41a4b312015-10-25 03:45:42 -0500218
219 printf("IPMI Handling ADD-SEL for record 0x%04x\n", recordid);
Chris Austenb4f5b922015-10-13 12:44:43 -0500220
221 *data_len = sizeof(g_sel_reserve);
222
223 // Pack the actual response
Chris Austen7cc33322015-11-11 00:20:22 -0600224 memcpy(response, &p->eventdata[1], 2);
Chris Austenb4f5b922015-10-13 12:44:43 -0500225
Chris Austen41a4b312015-10-25 03:45:42 -0500226 send_esel(recordid);
Chris Austenb4f5b922015-10-13 12:44:43 -0500227
228 return rc;
229}
230
231
232
233void register_netfn_storage_functions()
234{
235 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_WILDCARD);
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530236 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WILDCARD, nullptr, ipmi_storage_wildcard);
Chris Austenb4f5b922015-10-13 12:44:43 -0500237
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500238 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_GET_SEL_TIME);
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530239 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_TIME, nullptr, ipmi_storage_get_sel_time);
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500240
Chris Austenb4f5b922015-10-13 12:44:43 -0500241 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_SET_SEL_TIME);
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530242 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_SET_SEL_TIME, nullptr, ipmi_storage_set_sel_time);
Chris Austenb4f5b922015-10-13 12:44:43 -0500243
Chris Austenb4f5b922015-10-13 12:44:43 -0500244 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_GET_SEL_INFO);
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530245 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_INFO, nullptr, ipmi_storage_get_sel_info);
Chris Austenb4f5b922015-10-13 12:44:43 -0500246
247 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_RESERVE_SEL);
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530248 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_RESERVE_SEL, nullptr, ipmi_storage_reserve_sel);
Chris Austenb4f5b922015-10-13 12:44:43 -0500249
250 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_ADD_SEL);
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530251 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_ADD_SEL, nullptr, ipmi_storage_add_sel);
Chris Austenb4f5b922015-10-13 12:44:43 -0500252 return;
253}
254