blob: a40196c0e9182a238b0c82e7456affd7e91f3b38 [file] [log] [blame]
Chris Austenac4604a2015-10-13 12:43:27 -05001#include "sensorhandler.h"
Patrick Williams37af7332016-09-02 21:21:42 -05002#include "host-ipmid/ipmid-api.h"
Tomd700e762016-09-20 18:24:13 +05303#include <mapper.h>
Chris Austenac4604a2015-10-13 12:43:27 -05004#include <stdio.h>
5#include <string.h>
6#include <stdint.h>
Chris Austen10ccc0f2015-12-10 18:27:04 -06007#include <systemd/sd-bus.h>
Tomd700e762016-09-20 18:24:13 +05308#include "ipmid.hpp"
Chris Austenac4604a2015-10-13 12:43:27 -05009
Chris Austen8a45e7c2015-10-15 00:31:46 -050010extern int updateSensorRecordFromSSRAESC(const void *);
Tomd700e762016-09-20 18:24:13 +053011extern sd_bus *bus;
Chris Austenac4604a2015-10-13 12:43:27 -050012
13void register_netfn_sen_functions() __attribute__((constructor));
14
Chris Austen0012e9b2015-10-22 01:37:46 -050015struct sensorTypemap_t {
16 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060017 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050018 char dbusname[32];
19} ;
20
21
22sensorTypemap_t g_SensorTypeMap[] = {
23
Chris Austend7cf0e42015-11-07 14:27:12 -060024 {0x01, 0x6F, "Temp"},
25 {0x0C, 0x6F, "DIMM"},
26 {0x0C, 0x6F, "MEMORY_BUFFER"},
27 {0x07, 0x6F, "PROC"},
28 {0x07, 0x6F, "CORE"},
29 {0x07, 0x6F, "CPU"},
30 {0x0F, 0x6F, "BootProgress"},
31 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09
32 {0xC3, 0x6F, "BootCount"},
33 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060034 {0x12, 0x6F, "SYSTEM_EVENT"},
35 {0xC7, 0x03, "SYSTEM"},
36 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060037 {0xC2, 0x6F, "PowerCap"},
Chris Austend7cf0e42015-11-07 14:27:12 -060038 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050039};
40
41
Chris Austenac4604a2015-10-13 12:43:27 -050042struct sensor_data_t {
43 uint8_t sennum;
44} __attribute__ ((packed)) ;
45
Chris Austen10ccc0f2015-12-10 18:27:04 -060046struct sensorreadingresp_t {
47 uint8_t value;
48 uint8_t operation;
49 uint8_t indication[2];
50} __attribute__ ((packed)) ;
Chris Austenac4604a2015-10-13 12:43:27 -050051
Tomd700e762016-09-20 18:24:13 +053052// Use a lookup table to find the interface name of a specific sensor
53// This will be used until an alternative is found. this is the first
54// step for mapping IPMI
55int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
56
57 char *str1;
58 sd_bus_error error = SD_BUS_ERROR_NULL;
59 sd_bus_message *reply = NULL, *m=NULL;
60
61
62 int r;
63
64 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
65 if (r < 0) {
66 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
67 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
68 interface->bus, interface->path, interface->interface);
69 goto final;
70 }
71
72 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
73 if (r < 0) {
74 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
75 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
76 interface->bus, interface->path, interface->interface);
77 goto final;
78 }
79
80 r = sd_bus_call(bus, m, 0, &error, &reply);
81 if (r < 0) {
82 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
83 goto final;
84 }
85
86 r = sd_bus_message_read(reply, "v", "s", &str1) ;
87 if (r < 0) {
88 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
89 goto final;
90 }
91
92 strcpy(property_value, str1);
93
94final:
95
96 sd_bus_error_free(&error);
97 m = sd_bus_message_unref(m);
98 reply = sd_bus_message_unref(reply);
99
100 return r;
101}
102
103
104// Use a lookup table to find the interface name of a specific sensor
105// This will be used until an alternative is found. this is the first
106// step for mapping IPMI
107int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
108 char *busname = NULL;
109 const char *iface = "org.openbmc.managers.System";
110 const char *objname = "/org/openbmc/managers/System";
111 char *str1 = NULL, *str2, *str3;
112 sd_bus_error error = SD_BUS_ERROR_NULL;
113 sd_bus_message *reply = NULL;
114
115
116 int r;
117 r = mapper_get_service(bus, objname, &busname);
118 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400119 fprintf(stderr, "Failed to get %s busname: %s\n",
120 objname, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530121 goto final;
122 }
123
124 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
125 &error, &reply, "sy", type, num);
126 if (r < 0) {
127 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
128 goto final;
129 }
130
131 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
132 if (r < 0) {
133 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
134 goto final;
135 }
136
137 r = mapper_get_service(bus, str2, &str1);
138 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400139 fprintf(stderr, "Failed to get %s busname: %s\n",
140 str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530141 goto final;
142 }
143
144 strncpy(interface->bus, str1, MAX_DBUS_PATH);
145 strncpy(interface->path, str2, MAX_DBUS_PATH);
146 strncpy(interface->interface, str3, MAX_DBUS_PATH);
147
148 interface->sensornumber = num;
149
150final:
151
152 sd_bus_error_free(&error);
153 reply = sd_bus_message_unref(reply);
154 free(busname);
155 free(str1);
156
157 return r;
158}
159
160
161/////////////////////////////////////////////////////////////////////
162//
163// Routines used by ipmi commands wanting to interact on the dbus
164//
165/////////////////////////////////////////////////////////////////////
166int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
167
168
169 dbus_interface_t a;
170 int r;
171 sd_bus_error error = SD_BUS_ERROR_NULL;
172 sd_bus_message *m=NULL;
173
174 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
175 number, method, value);
176
177 r = find_openbmc_path("SENSOR", number, &a);
178
179 if (r < 0) {
180 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
181 return 0;
182 }
183
184 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
185 if (r < 0) {
186 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
187 goto final;
188 }
189
190 r = sd_bus_message_append(m, "v", "s", value);
191 if (r < 0) {
192 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
193 goto final;
194 }
195
196
197 r = sd_bus_call(bus, m, 0, &error, NULL);
198 if (r < 0) {
199 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
200 }
201
202final:
203 sd_bus_error_free(&error);
204 m = sd_bus_message_unref(m);
205
206 return 0;
207}
208int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
209
210
211 dbus_interface_t a;
212 int r;
213 sd_bus_error error = SD_BUS_ERROR_NULL;
214 sd_bus_message *m=NULL;
215
216 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
217 number, method, value);
218
219 r = find_openbmc_path("SENSOR", number, &a);
220
221 if (r < 0) {
222 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
223 return 0;
224 }
225
226 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
227 if (r < 0) {
228 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
229 goto final;
230 }
231
232 r = sd_bus_message_append(m, "v", "i", value);
233 if (r < 0) {
234 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
235 goto final;
236 }
237
238
239 r = sd_bus_call(bus, m, 0, &error, NULL);
240 if (r < 0) {
241 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
242 }
243
244final:
245 sd_bus_error_free(&error);
246 m = sd_bus_message_unref(m);
247
248 return 0;
249}
250
Chris Austen0012e9b2015-10-22 01:37:46 -0500251uint8_t dbus_to_sensor_type(char *p) {
Chris Austenac4604a2015-10-13 12:43:27 -0500252
Chris Austen0012e9b2015-10-22 01:37:46 -0500253 sensorTypemap_t *s = g_SensorTypeMap;
254 char r=0;
Chris Austenac4604a2015-10-13 12:43:27 -0500255
Chris Austen0012e9b2015-10-22 01:37:46 -0500256 while (s->number != 0xFF) {
257 if (!strcmp(s->dbusname,p)) {
258 r = s->number;
259 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500260 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500261 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500262 }
263
Chris Austend7cf0e42015-11-07 14:27:12 -0600264
Chris Austen0012e9b2015-10-22 01:37:46 -0500265 if (s->number == 0xFF)
266 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500267
Chris Austen0012e9b2015-10-22 01:37:46 -0500268 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500269}
270
Chris Austen0012e9b2015-10-22 01:37:46 -0500271
272uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) {
273 char fru_type_name[64];
274 int r= 0;
275
276 r = find_interface_property_fru_type(a, "fru_type", fru_type_name);
277 if (r<0) {
278 fprintf(stderr, "Failed to get a fru type: %s", strerror(-r));
279 return -1;
280 } else {
281 return dbus_to_sensor_type(fru_type_name);
282 }
283}
284
285
286uint8_t find_sensor(uint8_t sensor_number) {
287
288 dbus_interface_t a;
289 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400290 int r;
291 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500292
293 r = find_openbmc_path("SENSOR", sensor_number, &a);
294
295 if (r < 0) { return 0; }
296
297 // This is where sensors that do not exist in dbus but do
298 // exist in the host code stop. This should indicate it
299 // is not a supported sensor
Chris Austend7cf0e42015-11-07 14:27:12 -0600300 if (a.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500301
302 if (strstr(a.interface, "InventoryItem")) {
303 // InventoryItems are real frus. So need to get the
304 // fru_type property
Brad Bishop56003452016-10-05 21:49:19 -0400305 type = dbus_to_sensor_type_from_dbus(&a);
Chris Austen0012e9b2015-10-22 01:37:46 -0500306 } else {
307 // Non InventoryItems
308 p = strrchr (a.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400309 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500310 }
311
Brad Bishop56003452016-10-05 21:49:19 -0400312 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500313 }
314
Chris Austen10ccc0f2015-12-10 18:27:04 -0600315
316
317
318
Chris Austen0012e9b2015-10-22 01:37:46 -0500319ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
320 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500321 ipmi_data_len_t data_len, ipmi_context_t context)
322{
323 sensor_data_t *reqptr = (sensor_data_t*)request;
324 ipmi_ret_t rc = IPMI_CC_OK;
325
326 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
327
328 // TODO Not sure what the System-event-sensor is suppose to return
329 // need to ask Hostboot team
330 unsigned char buf[] = {0x00,0x6F};
331
Chris Austen0012e9b2015-10-22 01:37:46 -0500332 buf[0] = find_sensor(reqptr->sennum);
333
334 // HACK UNTIL Dbus gets updated or we find a better way
335 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600336 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500337 }
338
Chris Austenac4604a2015-10-13 12:43:27 -0500339
340 *data_len = sizeof(buf);
341 memcpy(response, &buf, *data_len);
342
Chris Austenac4604a2015-10-13 12:43:27 -0500343 return rc;
344}
345
346
347
Chris Austen0012e9b2015-10-22 01:37:46 -0500348ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
349 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500350 ipmi_data_len_t data_len, ipmi_context_t context)
351{
Chris Austenac4604a2015-10-13 12:43:27 -0500352 sensor_data_t *reqptr = (sensor_data_t*)request;
353 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austenac4604a2015-10-13 12:43:27 -0500354
Chris Austen0012e9b2015-10-22 01:37:46 -0500355 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500356
Chris Austen8a45e7c2015-10-15 00:31:46 -0500357 updateSensorRecordFromSSRAESC(reqptr);
358
Chris Austenac4604a2015-10-13 12:43:27 -0500359 *data_len=0;
360
Chris Austenac4604a2015-10-13 12:43:27 -0500361 return rc;
362}
363
Chris Austen10ccc0f2015-12-10 18:27:04 -0600364
365ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
366 ipmi_request_t request, ipmi_response_t response,
367 ipmi_data_len_t data_len, ipmi_context_t context)
368{
369 sensor_data_t *reqptr = (sensor_data_t*)request;
370 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
371 uint8_t type;
372 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
373 int r;
374 dbus_interface_t a;
375 sd_bus *bus = ipmid_get_sd_bus_connection();
376 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600377 int reading = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600378
379
380 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
381
382 r = find_openbmc_path("SENSOR", reqptr->sennum, &a);
383
Nan Li36deb762016-05-12 10:23:41 +0800384 if (r < 0) {
385 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
386 return IPMI_CC_SENSOR_INVALID;
387 }
388
Chris Austen10ccc0f2015-12-10 18:27:04 -0600389 type = find_sensor(reqptr->sennum);
Brad Bishop56003452016-10-05 21:49:19 -0400390 if(type == 0) {
391 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
392 return IPMI_CC_SENSOR_INVALID;
393 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600394
395 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
396
397 *data_len=0;
398
399 switch(type) {
400 case 0xC3:
401 case 0xC2:
Adriana Kobylak93125982016-03-01 12:48:10 -0600402 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600403 if (r < 0) {
404 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
405 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
406 a.bus, a.path, a.interface);
407 break;
408 }
409
Adriana Kobylak93125982016-03-01 12:48:10 -0600410 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600411 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600412 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600413 break;
414 }
415
416 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
417
418 rc = IPMI_CC_OK;
419 *data_len=sizeof(sensorreadingresp_t);
420
Adriana Kobylak93125982016-03-01 12:48:10 -0600421 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600422 resp->operation = 0;
423 resp->indication[0] = 0;
424 resp->indication[1] = 0;
425 break;
426
427 default:
428 *data_len=0;
429 rc = IPMI_CC_SENSOR_INVALID;
430 break;
431 }
432
433
vishwa1eaea4f2016-02-26 11:57:40 -0600434 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600435
436 return rc;
437}
438
Chris Austen0012e9b2015-10-22 01:37:46 -0500439ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
440 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500441 ipmi_data_len_t data_len, ipmi_context_t context)
442{
Nan Li70aa8d92016-08-29 00:11:10 +0800443 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500444
445 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
446 *data_len = 0;
447
448 return rc;
449}
450
451
452void register_netfn_sen_functions()
453{
Tom05732372016-09-06 17:21:23 +0530454 // <Wildcard Command>
Chris Austenac4604a2015-10-13 12:43:27 -0500455 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_WILDCARD);
Tom05732372016-09-06 17:21:23 +0530456 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, NULL, ipmi_sen_wildcard,
457 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500458
Tom05732372016-09-06 17:21:23 +0530459 // <Get Sensor Type>
Chris Austenac4604a2015-10-13 12:43:27 -0500460 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
Tom05732372016-09-06 17:21:23 +0530461 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, NULL, ipmi_sen_get_sensor_type,
462 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500463
Tom05732372016-09-06 17:21:23 +0530464 // <Set Sensor Reading and Event Status>
Chris Austenac4604a2015-10-13 12:43:27 -0500465 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
Tom05732372016-09-06 17:21:23 +0530466 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, NULL, ipmi_sen_set_sensor,
467 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500468
Tom05732372016-09-06 17:21:23 +0530469 // <Get Sensor Reading>
Chris Austen10ccc0f2015-12-10 18:27:04 -0600470 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
Tom05732372016-09-06 17:21:23 +0530471 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, NULL,
472 ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600473
Chris Austenac4604a2015-10-13 12:43:27 -0500474 return;
475}