blob: a4cfe342075312c676e45ae2bc4edfd9f2d6b005 [file] [log] [blame]
Tomd700e762016-09-20 18:24:13 +05301#include <mapper.h>
Emily Shaffer1fabf222017-04-05 08:53:21 -07002#include <math.h>
Chris Austenac4604a2015-10-13 12:43:27 -05003#include <stdio.h>
4#include <string.h>
Tom Josephbe703f72017-03-09 12:34:35 +05305#include <bitset>
Emily Shafferbbef71c2017-05-08 16:36:17 -07006#include <xyz/openbmc_project/Sensor/Value/server.hpp>
Chris Austen10ccc0f2015-12-10 18:27:04 -06007#include <systemd/sd-bus.h>
Tom Josephbe703f72017-03-09 12:34:35 +05308#include "host-ipmid/ipmid-api.h"
9#include <phosphor-logging/log.hpp>
Tomd700e762016-09-20 18:24:13 +053010#include "ipmid.hpp"
Tom Josephbe703f72017-03-09 12:34:35 +053011#include "sensorhandler.h"
12#include "types.hpp"
13#include "utils.hpp"
Chris Austenac4604a2015-10-13 12:43:27 -050014
Chris Austen8a45e7c2015-10-15 00:31:46 -050015extern int updateSensorRecordFromSSRAESC(const void *);
Tomd700e762016-09-20 18:24:13 +053016extern sd_bus *bus;
Tom Josephbe703f72017-03-09 12:34:35 +053017extern const ipmi::sensor::IdInfoMap sensors;
18using namespace phosphor::logging;
Chris Austenac4604a2015-10-13 12:43:27 -050019
20void register_netfn_sen_functions() __attribute__((constructor));
21
Chris Austen0012e9b2015-10-22 01:37:46 -050022struct sensorTypemap_t {
23 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060024 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050025 char dbusname[32];
26} ;
27
Chris Austen0012e9b2015-10-22 01:37:46 -050028sensorTypemap_t g_SensorTypeMap[] = {
29
Chris Austend7cf0e42015-11-07 14:27:12 -060030 {0x01, 0x6F, "Temp"},
31 {0x0C, 0x6F, "DIMM"},
32 {0x0C, 0x6F, "MEMORY_BUFFER"},
33 {0x07, 0x6F, "PROC"},
34 {0x07, 0x6F, "CORE"},
35 {0x07, 0x6F, "CPU"},
36 {0x0F, 0x6F, "BootProgress"},
37 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09
38 {0xC3, 0x6F, "BootCount"},
39 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060040 {0x12, 0x6F, "SYSTEM_EVENT"},
41 {0xC7, 0x03, "SYSTEM"},
42 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060043 {0xC2, 0x6F, "PowerCap"},
Dhruvaraj S07248292017-03-21 02:14:52 -050044 {0xD8, 0x03, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050045 {0xDA, 0x03, "TurboAllowed"},
Jayanth Othayothd5b2ac02017-03-27 08:19:18 -050046 {0xB4, 0x6F, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060047 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050048};
49
50
Chris Austenac4604a2015-10-13 12:43:27 -050051struct sensor_data_t {
52 uint8_t sennum;
53} __attribute__ ((packed)) ;
54
Chris Austen10ccc0f2015-12-10 18:27:04 -060055struct sensorreadingresp_t {
56 uint8_t value;
57 uint8_t operation;
58 uint8_t indication[2];
59} __attribute__ ((packed)) ;
Chris Austenac4604a2015-10-13 12:43:27 -050060
Tomd700e762016-09-20 18:24:13 +053061// Use a lookup table to find the interface name of a specific sensor
62// This will be used until an alternative is found. this is the first
63// step for mapping IPMI
64int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
65
66 char *str1;
67 sd_bus_error error = SD_BUS_ERROR_NULL;
68 sd_bus_message *reply = NULL, *m=NULL;
69
70
71 int r;
72
73 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
74 if (r < 0) {
75 fprintf(stderr, "Failed to create a method call: %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_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
82 if (r < 0) {
83 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
84 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
85 interface->bus, interface->path, interface->interface);
86 goto final;
87 }
88
89 r = sd_bus_call(bus, m, 0, &error, &reply);
90 if (r < 0) {
91 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
92 goto final;
93 }
94
95 r = sd_bus_message_read(reply, "v", "s", &str1) ;
96 if (r < 0) {
97 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
98 goto final;
99 }
100
101 strcpy(property_value, str1);
102
103final:
104
105 sd_bus_error_free(&error);
106 m = sd_bus_message_unref(m);
107 reply = sd_bus_message_unref(reply);
108
109 return r;
110}
111
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700112int get_bus_for_path(const char *path, char **busname) {
113 return mapper_get_service(bus, path, busname);
114}
Tomd700e762016-09-20 18:24:13 +0530115
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700116int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
Tomd700e762016-09-20 18:24:13 +0530117 char *busname = NULL;
118 const char *iface = "org.openbmc.managers.System";
119 const char *objname = "/org/openbmc/managers/System";
120 char *str1 = NULL, *str2, *str3;
121 sd_bus_error error = SD_BUS_ERROR_NULL;
122 sd_bus_message *reply = NULL;
123
124
125 int r;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700126 r = get_bus_for_path(objname, &busname);
Tomd700e762016-09-20 18:24:13 +0530127 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400128 fprintf(stderr, "Failed to get %s busname: %s\n",
129 objname, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530130 goto final;
131 }
132
133 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
134 &error, &reply, "sy", type, num);
135 if (r < 0) {
136 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
137 goto final;
138 }
139
140 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
141 if (r < 0) {
142 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
143 goto final;
144 }
145
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700146 r = get_bus_for_path(str2, &str1);
Tomd700e762016-09-20 18:24:13 +0530147 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400148 fprintf(stderr, "Failed to get %s busname: %s\n",
149 str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530150 goto final;
151 }
152
153 strncpy(interface->bus, str1, MAX_DBUS_PATH);
154 strncpy(interface->path, str2, MAX_DBUS_PATH);
155 strncpy(interface->interface, str3, MAX_DBUS_PATH);
156
157 interface->sensornumber = num;
Emily Shaffer71174412017-04-05 15:10:40 -0700158 // Make sure we know that the type hasn't been set, as newer codebase will
159 // set it automatically from the YAML at this step.
160 interface->sensortype = 0;
Tomd700e762016-09-20 18:24:13 +0530161
162final:
163
164 sd_bus_error_free(&error);
165 reply = sd_bus_message_unref(reply);
166 free(busname);
167 free(str1);
168
169 return r;
170}
171
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700172// Use a lookup table to find the interface name of a specific sensor
173// This will be used until an alternative is found. this is the first
174// step for mapping IPMI
175int find_openbmc_path(uint8_t num, dbus_interface_t *interface) {
176 int rc;
177
178 // When the sensor map does not contain the sensor requested,
179 // fall back to the legacy DBus lookup (deprecated)
180 const auto& sensor_it = sensors.find(num);
181 if (sensor_it == sensors.end())
182 {
183 return legacy_dbus_openbmc_path("SENSOR", num, interface);
184 }
185
186 const auto& info = sensor_it->second;
187
Patrick Williams8451edf2017-06-13 09:01:06 -0500188 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700189 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
190 if (rc < 0) {
191 fprintf(stderr, "Failed to get %s busname: %s\n",
192 info.sensorPath.c_str(),
193 busname);
194 goto final;
195 }
196
197 interface->sensortype = info.sensorType;
198 strcpy(interface->bus, busname);
199 strcpy(interface->path, info.sensorPath.c_str());
200 // Take the interface name from the beginning of the DbusInterfaceMap. This
201 // works for the Value interface but may not suffice for more complex
202 // sensors.
203 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
204 strcpy(interface->interface, info.sensorInterfaces.begin()->first.c_str());
205 interface->sensornumber = num;
206
207final:
208 free(busname);
209 return rc;
210}
211
Tomd700e762016-09-20 18:24:13 +0530212
213/////////////////////////////////////////////////////////////////////
214//
215// Routines used by ipmi commands wanting to interact on the dbus
216//
217/////////////////////////////////////////////////////////////////////
218int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
219
220
221 dbus_interface_t a;
222 int r;
223 sd_bus_error error = SD_BUS_ERROR_NULL;
224 sd_bus_message *m=NULL;
225
226 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
227 number, method, value);
228
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700229 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530230
231 if (r < 0) {
232 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
233 return 0;
234 }
235
236 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
237 if (r < 0) {
238 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
239 goto final;
240 }
241
242 r = sd_bus_message_append(m, "v", "s", value);
243 if (r < 0) {
244 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
245 goto final;
246 }
247
248
249 r = sd_bus_call(bus, m, 0, &error, NULL);
250 if (r < 0) {
251 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
252 }
253
254final:
255 sd_bus_error_free(&error);
256 m = sd_bus_message_unref(m);
257
258 return 0;
259}
260int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
261
262
263 dbus_interface_t a;
264 int r;
265 sd_bus_error error = SD_BUS_ERROR_NULL;
266 sd_bus_message *m=NULL;
267
268 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
269 number, method, value);
270
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700271 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530272
273 if (r < 0) {
274 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
275 return 0;
276 }
277
278 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
279 if (r < 0) {
280 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
281 goto final;
282 }
283
284 r = sd_bus_message_append(m, "v", "i", value);
285 if (r < 0) {
286 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
287 goto final;
288 }
289
290
291 r = sd_bus_call(bus, m, 0, &error, NULL);
292 if (r < 0) {
293 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
294 }
295
296final:
297 sd_bus_error_free(&error);
298 m = sd_bus_message_unref(m);
299
300 return 0;
301}
302
Chris Austen0012e9b2015-10-22 01:37:46 -0500303uint8_t dbus_to_sensor_type(char *p) {
Chris Austenac4604a2015-10-13 12:43:27 -0500304
Chris Austen0012e9b2015-10-22 01:37:46 -0500305 sensorTypemap_t *s = g_SensorTypeMap;
306 char r=0;
Chris Austenac4604a2015-10-13 12:43:27 -0500307
Chris Austen0012e9b2015-10-22 01:37:46 -0500308 while (s->number != 0xFF) {
309 if (!strcmp(s->dbusname,p)) {
310 r = s->number;
311 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500312 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500313 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500314 }
315
Chris Austend7cf0e42015-11-07 14:27:12 -0600316
Chris Austen0012e9b2015-10-22 01:37:46 -0500317 if (s->number == 0xFF)
318 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500319
Chris Austen0012e9b2015-10-22 01:37:46 -0500320 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500321}
322
Chris Austen0012e9b2015-10-22 01:37:46 -0500323
324uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) {
325 char fru_type_name[64];
326 int r= 0;
327
328 r = find_interface_property_fru_type(a, "fru_type", fru_type_name);
329 if (r<0) {
330 fprintf(stderr, "Failed to get a fru type: %s", strerror(-r));
331 return -1;
332 } else {
333 return dbus_to_sensor_type(fru_type_name);
334 }
335}
336
Emily Shaffer391f3302017-04-03 10:27:08 -0700337uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500338
Chris Austen0012e9b2015-10-22 01:37:46 -0500339 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400340 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500341
Chris Austen0012e9b2015-10-22 01:37:46 -0500342 // This is where sensors that do not exist in dbus but do
343 // exist in the host code stop. This should indicate it
344 // is not a supported sensor
Emily Shaffer391f3302017-04-03 10:27:08 -0700345 if (dbus_if.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500346
Emily Shaffer71174412017-04-05 15:10:40 -0700347 // Fetch type from interface itself.
348 if (dbus_if.sensortype != 0)
349 {
350 type = dbus_if.sensortype;
351 }
352 // Legacy codebase does not populate type during initial handling:
353 else if (strstr(dbus_if.interface, "InventoryItem")) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500354 // InventoryItems are real frus. So need to get the
355 // fru_type property
Emily Shaffer391f3302017-04-03 10:27:08 -0700356 type = dbus_to_sensor_type_from_dbus(&dbus_if);
Chris Austen0012e9b2015-10-22 01:37:46 -0500357 } else {
358 // Non InventoryItems
Emily Shaffer391f3302017-04-03 10:27:08 -0700359 p = strrchr (dbus_if.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400360 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500361 }
362
Brad Bishop56003452016-10-05 21:49:19 -0400363 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500364 }
365
Emily Shaffer391f3302017-04-03 10:27:08 -0700366// Replaces find_sensor
367uint8_t find_type_for_sensor_number(uint8_t num) {
368 int r;
369 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700370 r = find_openbmc_path(num, &dbus_if);
Emily Shaffer391f3302017-04-03 10:27:08 -0700371 if (r < 0) {
372 fprintf(stderr, "Could not find sensor %d\n", num);
373 return r;
374 }
375 return get_type_from_interface(dbus_if);
376}
377
Chris Austen10ccc0f2015-12-10 18:27:04 -0600378
379
380
381
Chris Austen0012e9b2015-10-22 01:37:46 -0500382ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
383 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500384 ipmi_data_len_t data_len, ipmi_context_t context)
385{
386 sensor_data_t *reqptr = (sensor_data_t*)request;
387 ipmi_ret_t rc = IPMI_CC_OK;
388
389 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
390
391 // TODO Not sure what the System-event-sensor is suppose to return
392 // need to ask Hostboot team
393 unsigned char buf[] = {0x00,0x6F};
394
Emily Shaffer391f3302017-04-03 10:27:08 -0700395 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500396
397 // HACK UNTIL Dbus gets updated or we find a better way
398 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600399 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500400 }
401
Chris Austenac4604a2015-10-13 12:43:27 -0500402
403 *data_len = sizeof(buf);
404 memcpy(response, &buf, *data_len);
405
Chris Austenac4604a2015-10-13 12:43:27 -0500406 return rc;
407}
408
Tom Josephbe703f72017-03-09 12:34:35 +0530409ipmi_ret_t setSensorReading(void *request)
410{
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500411 SetSensorReadingReq cmdData =
412 *(static_cast<SetSensorReadingReq *>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530413
414 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500415 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530416 if (iter == sensors.end())
417 {
418 return IPMI_CC_SENSOR_INVALID;
419 }
420
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500421 return iter->second.updateFunc(cmdData, iter->second);
Tom Josephbe703f72017-03-09 12:34:35 +0530422}
Chris Austenac4604a2015-10-13 12:43:27 -0500423
Chris Austen0012e9b2015-10-22 01:37:46 -0500424ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
425 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500426 ipmi_data_len_t data_len, ipmi_context_t context)
427{
Chris Austenac4604a2015-10-13 12:43:27 -0500428 sensor_data_t *reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500429
Chris Austen0012e9b2015-10-22 01:37:46 -0500430 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500431
Tom Josephbe703f72017-03-09 12:34:35 +0530432 /*
433 * This would support the Set Sensor Reading command for the presence
434 * and functional state of Processor, Core & DIMM. For the remaining
435 * sensors the existing support is invoked.
436 */
437 auto ipmiRC = setSensorReading(request);
438
439 if(ipmiRC == IPMI_CC_SENSOR_INVALID)
440 {
441 updateSensorRecordFromSSRAESC(reqptr);
442 ipmiRC = IPMI_CC_OK;
443 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500444
Chris Austenac4604a2015-10-13 12:43:27 -0500445 *data_len=0;
Tom Josephbe703f72017-03-09 12:34:35 +0530446 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500447}
448
Chris Austen10ccc0f2015-12-10 18:27:04 -0600449
450ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
451 ipmi_request_t request, ipmi_response_t response,
452 ipmi_data_len_t data_len, ipmi_context_t context)
453{
454 sensor_data_t *reqptr = (sensor_data_t*)request;
455 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
456 uint8_t type;
457 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
458 int r;
459 dbus_interface_t a;
460 sd_bus *bus = ipmid_get_sd_bus_connection();
461 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600462 int reading = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600463
464
465 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
466
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700467 r = find_openbmc_path(reqptr->sennum, &a);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600468
Nan Li36deb762016-05-12 10:23:41 +0800469 if (r < 0) {
470 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
471 return IPMI_CC_SENSOR_INVALID;
472 }
473
Emily Shaffer391f3302017-04-03 10:27:08 -0700474 type = get_type_from_interface(a);
Brad Bishop56003452016-10-05 21:49:19 -0400475 if(type == 0) {
476 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
477 return IPMI_CC_SENSOR_INVALID;
478 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600479
480 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
481
482 *data_len=0;
483
Emily Shaffer10f49592017-05-10 12:01:10 -0700484 int64_t raw_value;
Emily Shaffer1fabf222017-04-05 08:53:21 -0700485 ipmi::sensor::Info sensor;
486
Chris Austen10ccc0f2015-12-10 18:27:04 -0600487 switch(type) {
488 case 0xC3:
489 case 0xC2:
Adriana Kobylak93125982016-03-01 12:48:10 -0600490 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600491 if (r < 0) {
492 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
493 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
494 a.bus, a.path, a.interface);
495 break;
496 }
497
Adriana Kobylak93125982016-03-01 12:48:10 -0600498 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600499 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600500 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600501 break;
502 }
503
504 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
505
506 rc = IPMI_CC_OK;
507 *data_len=sizeof(sensorreadingresp_t);
508
Adriana Kobylak93125982016-03-01 12:48:10 -0600509 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600510 resp->operation = 0;
511 resp->indication[0] = 0;
512 resp->indication[1] = 0;
513 break;
514
Emily Shaffer1fabf222017-04-05 08:53:21 -0700515 case IPMI_SENSOR_TEMP:
516 case IPMI_SENSOR_VOLTAGE:
517 case IPMI_SENSOR_CURRENT:
518 case IPMI_SENSOR_FAN:
519 // Get reading for /xyz/openbmc_project/Sensor/Value.interface
Emily Shaffer10f49592017-05-10 12:01:10 -0700520 if(sensors.find(reqptr->sennum) == sensors.end())
521 {
522 fprintf(stderr, "Failed to find config entry for Sensor 0x%02x\n",
523 reqptr->sennum);
524 return IPMI_CC_SENSOR_INVALID;
525 }
526
527 sensor = sensors.at(reqptr->sennum);
Emily Shaffer1fabf222017-04-05 08:53:21 -0700528
529 // Get value
530 r = sd_bus_get_property_trivial(bus,
531 a.bus,
532 a.path,
533 a.interface,
534 "Value",
535 NULL,
536 'x',
537 &raw_value);
538 if (r < 0) {
539 fprintf(stderr,
540 "Failed to call sd_bus_get_property:%d, %s, 'value'\n",
541 r,
542 strerror(-r));
543 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
544 a.bus, a.path, a.interface);
545 break;
546 }
547
Emily Shaffer10f49592017-05-10 12:01:10 -0700548 // Prevent div0
549 if (sensor.coefficientM == 0) {
550 sensor.coefficientM = 1;
551 };
Emily Shaffer1fabf222017-04-05 08:53:21 -0700552
Emily Shaffer10f49592017-05-10 12:01:10 -0700553 resp->value = static_cast<uint8_t>(
554 (raw_value - sensor.scaledOffset) / sensor.coefficientM);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700555 resp->operation = 1<<6; // scanning enabled
556 resp->indication[0] = 0; // not a threshold sensor. ignore
Emily Shaffer1fabf222017-04-05 08:53:21 -0700557 resp->indication[1] = 0;
558 rc = IPMI_CC_OK;
559 *data_len=sizeof(sensorreadingresp_t);
560 break;
561
Chris Austen10ccc0f2015-12-10 18:27:04 -0600562 default:
563 *data_len=0;
564 rc = IPMI_CC_SENSOR_INVALID;
565 break;
566 }
567
568
vishwa1eaea4f2016-02-26 11:57:40 -0600569 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600570
571 return rc;
572}
573
Chris Austen0012e9b2015-10-22 01:37:46 -0500574ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
575 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500576 ipmi_data_len_t data_len, ipmi_context_t context)
577{
Nan Li70aa8d92016-08-29 00:11:10 +0800578 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500579
580 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
581 *data_len = 0;
582
583 return rc;
584}
585
Emily Shafferd06e0e72017-04-05 09:08:57 -0700586ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
587 ipmi_request_t request,
588 ipmi_response_t response,
589 ipmi_data_len_t data_len,
590 ipmi_context_t context)
591{
592 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
593 if (request == nullptr ||
594 get_sdr_info::request::get_count(request) == false)
595 {
596 // Get Sensor Count
597 resp->count = sensors.size();
598 }
599 else
600 {
601 resp->count = 1;
602 }
603
604 // Multiple LUNs not supported.
605 namespace response = get_sdr_info::response;
606 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
607 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
608 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
609 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
610 response::set_static_population(&(resp->luns_and_dynamic_population));
611
612 *data_len = SDR_INFO_RESP_SIZE;
613
614 return IPMI_CC_OK;
615}
616
Emily Shaffera344afc2017-04-13 15:09:39 -0700617ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
618 ipmi_request_t request,
619 ipmi_response_t response,
620 ipmi_data_len_t data_len,
621 ipmi_context_t context)
622{
623 // A constant reservation ID is okay until we implement add/remove SDR.
624 const uint16_t reservation_id = 1;
625 *(uint16_t*)response = reservation_id;
626
627 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
628 return IPMI_CC_OK;
629}
Chris Austenac4604a2015-10-13 12:43:27 -0500630
Emily Shafferbbef71c2017-05-08 16:36:17 -0700631ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
632 const ipmi::sensor::Info *info,
633 ipmi_data_len_t data_len)
634{
635 /* Functional sensor case */
636 if (info->sensorInterfaces.begin()->first ==
637 "xyz.openbmc_project.Sensor.Value")
638 {
639 // Get bus
640 sd_bus *bus = ipmid_get_sd_bus_connection();
641 dbus_interface_t iface;
642
643 if (0 > find_openbmc_path(body->entity_id, &iface))
644 return IPMI_CC_SENSOR_INVALID;
645
646 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
647
648 /* Unit info */
649 char *raw_cstr;
650 if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
651 iface.interface, "Unit", NULL,
652 &raw_cstr)) {
653 fprintf(stderr, "Expected to find Unit interface in bus %s, path %s, but it was missing.\n",
654 iface.bus, iface.path);
655 return IPMI_CC_SENSOR_INVALID;
656 }
657
658 std::string raw_str(raw_cstr);
659 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
660 server::Value::Unit unit =
661 server::Value::convertUnitFromString(raw_str);
662
663 // Unit strings defined in
664 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
665 switch (unit)
666 {
667 case server::Value::Unit::DegreesC:
668 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
669 break;
670 case server::Value::Unit::RPMS:
671 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
672 get_sdr::body::set_rate_unit(0b100, body); // per minute
673 break;
674 case server::Value::Unit::Volts:
675 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
676 break;
677 case server::Value::Unit::Meters:
678 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
679 break;
680 case server::Value::Unit::Amperes:
681 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
682 break;
683 case server::Value::Unit::Joules:
684 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
685 break;
686 default:
687 fprintf(stderr, "Unknown value unit type: = %s\n", raw_cstr);
688 }
689
690 free(raw_cstr);
691
692 /* Modifiers to reading info */
Emily Shaffer10f49592017-05-10 12:01:10 -0700693 // Get scale
694 int64_t scale;
695 if (0 > sd_bus_get_property_trivial(bus,
696 iface.bus,
697 iface.path,
698 iface.interface,
699 "Scale",
700 NULL,
701 'x',
702 &scale)) {
703 fprintf(stderr, "Expected to find Scale interface in bus %s, path %s, but it was missing.\n",
704 iface.bus, iface.path);
705 return IPMI_CC_SENSOR_INVALID;
706 }
707
708 get_sdr::body::set_b(info->coefficientB, body);
709 get_sdr::body::set_m(info->coefficientM, body);
710 get_sdr::body::set_b_exp(info->exponentB, body);
711 get_sdr::body::set_r_exp(scale, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700712
713 /* ID string */
714 std::string id_string = info->sensorPath.substr(
715 info->sensorPath.find_last_of('/')+1, info->sensorPath.length());
716 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
717 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
718 {
719 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
720 }
721 else
722 {
723 get_sdr::body::set_id_strlen(id_string.length(), body);
724 }
725 strncpy(body->id_string, id_string.c_str(),
726 get_sdr::body::get_id_strlen(body));
727 }
728
729 return IPMI_CC_OK;
730};
731
732ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
733 ipmi_request_t request, ipmi_response_t response,
734 ipmi_data_len_t data_len, ipmi_context_t context)
735{
736 ipmi_ret_t ret = IPMI_CC_OK;
737 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
738 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
739 get_sdr::SensorDataFullRecord record = {0};
740 if (req != NULL)
741 {
742 // Note: we use an iterator so we can provide the next ID at the end of
743 // the call.
744 auto sensor = sensors.begin();
745
746 // At the beginning of a scan, the host side will send us id=0.
747 if (get_sdr::request::get_record_id(req) != 0)
748 {
749 sensor = sensors.find(get_sdr::request::get_record_id(req));
750 if(sensor == sensors.end()) {
751 return IPMI_CC_SENSOR_INVALID;
752 }
753 }
754
755 uint8_t sensor_id = sensor->first;
756
757 /* Header */
758 get_sdr::header::set_record_id(sensor_id, &(record.header));
759 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
760 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
761 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
762
763 /* Key */
764 record.key.sensor_number = sensor_id;
765
766 /* Body */
767 record.body.entity_id = sensor_id;
768 record.body.sensor_type = sensor->second.sensorType;
769 record.body.event_reading_type = sensor->second.sensorReadingType;
770
771 // Set the type-specific details given the DBus interface
772 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
773 data_len);
774
775 if (++sensor == sensors.end())
776 {
777 get_sdr::response::set_next_record_id(0xFFFF, resp); // last record
778 }
779 else
780 {
781 get_sdr::response::set_next_record_id(sensor->first, resp);
782 }
783
784 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
785 memcpy(resp->record_data, (char*)&record + req->offset,
786 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
787 }
788
789 return ret;
790}
791
792
Chris Austenac4604a2015-10-13 12:43:27 -0500793void register_netfn_sen_functions()
794{
Tom05732372016-09-06 17:21:23 +0530795 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -0700796 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
797 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
798 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
799 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +0530800 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500801
Tom05732372016-09-06 17:21:23 +0530802 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -0700803 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
804 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
805 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
806 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +0530807 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500808
Tom05732372016-09-06 17:21:23 +0530809 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -0700810 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
811 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
812 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
813 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +0530814 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500815
Tom05732372016-09-06 17:21:23 +0530816 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -0700817 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
818 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
819 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
820 nullptr, ipmi_sen_get_sensor_reading,
821 PRIVILEGE_USER);
822
823 // <Reserve SDR>
824 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
825 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
826 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
827 nullptr, ipmi_sen_reserve_sdr,
828 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600829
Emily Shafferd06e0e72017-04-05 09:08:57 -0700830 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -0700831 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
832 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
833 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
834 nullptr, ipmi_sen_get_sdr_info,
835 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700836
837 // <Get SDR>
838 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
839 NETFUN_SENSOR, IPMI_CMD_GET_SDR);
840 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR,
841 nullptr, ipmi_sen_get_sdr,
842 PRIVILEGE_USER);
843
Chris Austenac4604a2015-10-13 12:43:27 -0500844 return;
845}