blob: 6503db8c6291836285488330b9b489b7249a5904 [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
53// Use a lookup table to find the interface name of a specific sensor
54// This will be used until an alternative is found. this is the first
55// step for mapping IPMI
56int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
57
58 char *str1;
59 sd_bus_error error = SD_BUS_ERROR_NULL;
60 sd_bus_message *reply = NULL, *m=NULL;
61
62
63 int r;
64
65 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
66 if (r < 0) {
67 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
68 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
69 interface->bus, interface->path, interface->interface);
70 goto final;
71 }
72
73 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
74 if (r < 0) {
75 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
76 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
77 interface->bus, interface->path, interface->interface);
78 goto final;
79 }
80
81 r = sd_bus_call(bus, m, 0, &error, &reply);
82 if (r < 0) {
83 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
84 goto final;
85 }
86
87 r = sd_bus_message_read(reply, "v", "s", &str1) ;
88 if (r < 0) {
89 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
90 goto final;
91 }
92
93 strcpy(property_value, str1);
94
95final:
96
97 sd_bus_error_free(&error);
98 m = sd_bus_message_unref(m);
99 reply = sd_bus_message_unref(reply);
100
101 return r;
102}
103
104
105// Use a lookup table to find the interface name of a specific sensor
106// This will be used until an alternative is found. this is the first
107// step for mapping IPMI
108int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
109 char *busname = NULL;
110 const char *iface = "org.openbmc.managers.System";
111 const char *objname = "/org/openbmc/managers/System";
112 char *str1 = NULL, *str2, *str3;
113 sd_bus_error error = SD_BUS_ERROR_NULL;
114 sd_bus_message *reply = NULL;
115
116
117 int r;
118 r = mapper_get_service(bus, objname, &busname);
119 if (r < 0) {
120 fprintf(stderr, "Failed to get system manager busname: %s\n", strerror(-r));
121 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) {
139 fprintf(stderr, "Failed to get item busname: %s\n", strerror(-r));
140 goto final;
141 }
142
143 strncpy(interface->bus, str1, MAX_DBUS_PATH);
144 strncpy(interface->path, str2, MAX_DBUS_PATH);
145 strncpy(interface->interface, str3, MAX_DBUS_PATH);
146
147 interface->sensornumber = num;
148
149final:
150
151 sd_bus_error_free(&error);
152 reply = sd_bus_message_unref(reply);
153 free(busname);
154 free(str1);
155
156 return r;
157}
158
159
160/////////////////////////////////////////////////////////////////////
161//
162// Routines used by ipmi commands wanting to interact on the dbus
163//
164/////////////////////////////////////////////////////////////////////
165int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
166
167
168 dbus_interface_t a;
169 int r;
170 sd_bus_error error = SD_BUS_ERROR_NULL;
171 sd_bus_message *m=NULL;
172
173 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
174 number, method, value);
175
176 r = find_openbmc_path("SENSOR", number, &a);
177
178 if (r < 0) {
179 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
180 return 0;
181 }
182
183 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
184 if (r < 0) {
185 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
186 goto final;
187 }
188
189 r = sd_bus_message_append(m, "v", "s", value);
190 if (r < 0) {
191 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
192 goto final;
193 }
194
195
196 r = sd_bus_call(bus, m, 0, &error, NULL);
197 if (r < 0) {
198 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
199 }
200
201final:
202 sd_bus_error_free(&error);
203 m = sd_bus_message_unref(m);
204
205 return 0;
206}
207int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
208
209
210 dbus_interface_t a;
211 int r;
212 sd_bus_error error = SD_BUS_ERROR_NULL;
213 sd_bus_message *m=NULL;
214
215 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
216 number, method, value);
217
218 r = find_openbmc_path("SENSOR", number, &a);
219
220 if (r < 0) {
221 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
222 return 0;
223 }
224
225 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
226 if (r < 0) {
227 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
228 goto final;
229 }
230
231 r = sd_bus_message_append(m, "v", "i", value);
232 if (r < 0) {
233 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
234 goto final;
235 }
236
237
238 r = sd_bus_call(bus, m, 0, &error, NULL);
239 if (r < 0) {
240 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
241 }
242
243final:
244 sd_bus_error_free(&error);
245 m = sd_bus_message_unref(m);
246
247 return 0;
248}
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;
290 char r;
291
292 r = find_openbmc_path("SENSOR", sensor_number, &a);
293
294 if (r < 0) { return 0; }
295
296 // This is where sensors that do not exist in dbus but do
297 // exist in the host code stop. This should indicate it
298 // is not a supported sensor
Chris Austend7cf0e42015-11-07 14:27:12 -0600299 if (a.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500300
301 if (strstr(a.interface, "InventoryItem")) {
302 // InventoryItems are real frus. So need to get the
303 // fru_type property
304 r = dbus_to_sensor_type_from_dbus(&a);
305 } else {
306 // Non InventoryItems
307 p = strrchr (a.path, '/');
308 r = dbus_to_sensor_type(p+1);
309 }
310
311 return r;
312 }
313
Chris Austen10ccc0f2015-12-10 18:27:04 -0600314
315
316
317
Chris Austen0012e9b2015-10-22 01:37:46 -0500318ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
319 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500320 ipmi_data_len_t data_len, ipmi_context_t context)
321{
322 sensor_data_t *reqptr = (sensor_data_t*)request;
323 ipmi_ret_t rc = IPMI_CC_OK;
324
325 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
326
327 // TODO Not sure what the System-event-sensor is suppose to return
328 // need to ask Hostboot team
329 unsigned char buf[] = {0x00,0x6F};
330
Chris Austen0012e9b2015-10-22 01:37:46 -0500331 buf[0] = find_sensor(reqptr->sennum);
332
333 // HACK UNTIL Dbus gets updated or we find a better way
334 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600335 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500336 }
337
Chris Austenac4604a2015-10-13 12:43:27 -0500338
339 *data_len = sizeof(buf);
340 memcpy(response, &buf, *data_len);
341
Chris Austenac4604a2015-10-13 12:43:27 -0500342 return rc;
343}
344
345
346
Chris Austen0012e9b2015-10-22 01:37:46 -0500347ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
348 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500349 ipmi_data_len_t data_len, ipmi_context_t context)
350{
Chris Austenac4604a2015-10-13 12:43:27 -0500351 sensor_data_t *reqptr = (sensor_data_t*)request;
352 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austenac4604a2015-10-13 12:43:27 -0500353
Chris Austen0012e9b2015-10-22 01:37:46 -0500354 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500355
Chris Austen8a45e7c2015-10-15 00:31:46 -0500356 updateSensorRecordFromSSRAESC(reqptr);
357
Chris Austenac4604a2015-10-13 12:43:27 -0500358 *data_len=0;
359
Chris Austenac4604a2015-10-13 12:43:27 -0500360 return rc;
361}
362
Chris Austen10ccc0f2015-12-10 18:27:04 -0600363
364ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
365 ipmi_request_t request, ipmi_response_t response,
366 ipmi_data_len_t data_len, ipmi_context_t context)
367{
368 sensor_data_t *reqptr = (sensor_data_t*)request;
369 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
370 uint8_t type;
371 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
372 int r;
373 dbus_interface_t a;
374 sd_bus *bus = ipmid_get_sd_bus_connection();
375 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600376 int reading = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600377
378
379 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
380
381 r = find_openbmc_path("SENSOR", reqptr->sennum, &a);
382
Nan Li36deb762016-05-12 10:23:41 +0800383 if (r < 0) {
384 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
385 return IPMI_CC_SENSOR_INVALID;
386 }
387
Chris Austen10ccc0f2015-12-10 18:27:04 -0600388 type = find_sensor(reqptr->sennum);
389
390 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
391
392 *data_len=0;
393
394 switch(type) {
395 case 0xC3:
396 case 0xC2:
Adriana Kobylak93125982016-03-01 12:48:10 -0600397 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600398 if (r < 0) {
399 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
400 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
401 a.bus, a.path, a.interface);
402 break;
403 }
404
Adriana Kobylak93125982016-03-01 12:48:10 -0600405 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600406 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600407 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600408 break;
409 }
410
411 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
412
413 rc = IPMI_CC_OK;
414 *data_len=sizeof(sensorreadingresp_t);
415
Adriana Kobylak93125982016-03-01 12:48:10 -0600416 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600417 resp->operation = 0;
418 resp->indication[0] = 0;
419 resp->indication[1] = 0;
420 break;
421
422 default:
423 *data_len=0;
424 rc = IPMI_CC_SENSOR_INVALID;
425 break;
426 }
427
428
vishwa1eaea4f2016-02-26 11:57:40 -0600429 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600430
431 return rc;
432}
433
Chris Austen0012e9b2015-10-22 01:37:46 -0500434ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
435 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500436 ipmi_data_len_t data_len, ipmi_context_t context)
437{
Nan Li70aa8d92016-08-29 00:11:10 +0800438 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500439
440 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
441 *data_len = 0;
442
443 return rc;
444}
445
446
447void register_netfn_sen_functions()
448{
449 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_WILDCARD);
450 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, NULL, ipmi_sen_wildcard);
451
452 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
453 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, NULL, ipmi_sen_get_sensor_type);
454
455 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
456 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, NULL, ipmi_sen_set_sensor);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500457
Chris Austen10ccc0f2015-12-10 18:27:04 -0600458 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
459 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, NULL, ipmi_sen_get_sensor_reading);
460
Chris Austenac4604a2015-10-13 12:43:27 -0500461 return;
462}