blob: ea0eac42a5ee27920118f4ae71e99e8a114a8c86 [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>
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050010#include <phosphor-logging/elog-errors.hpp>
Tomd700e762016-09-20 18:24:13 +053011#include "ipmid.hpp"
Tom Josephbe703f72017-03-09 12:34:35 +053012#include "sensorhandler.h"
13#include "types.hpp"
14#include "utils.hpp"
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050015#include "xyz/openbmc_project/Common/error.hpp"
16
Chris Austen8a45e7c2015-10-15 00:31:46 -050017extern int updateSensorRecordFromSSRAESC(const void *);
Tomd700e762016-09-20 18:24:13 +053018extern sd_bus *bus;
Tom Josephbe703f72017-03-09 12:34:35 +053019extern const ipmi::sensor::IdInfoMap sensors;
20using namespace phosphor::logging;
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050021using InternalFailure =
22 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austenac4604a2015-10-13 12:43:27 -050023
24void register_netfn_sen_functions() __attribute__((constructor));
25
Chris Austen0012e9b2015-10-22 01:37:46 -050026struct sensorTypemap_t {
27 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060028 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050029 char dbusname[32];
30} ;
31
Chris Austen0012e9b2015-10-22 01:37:46 -050032sensorTypemap_t g_SensorTypeMap[] = {
33
Chris Austend7cf0e42015-11-07 14:27:12 -060034 {0x01, 0x6F, "Temp"},
35 {0x0C, 0x6F, "DIMM"},
36 {0x0C, 0x6F, "MEMORY_BUFFER"},
37 {0x07, 0x6F, "PROC"},
38 {0x07, 0x6F, "CORE"},
39 {0x07, 0x6F, "CPU"},
40 {0x0F, 0x6F, "BootProgress"},
41 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09
42 {0xC3, 0x6F, "BootCount"},
43 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060044 {0x12, 0x6F, "SYSTEM_EVENT"},
45 {0xC7, 0x03, "SYSTEM"},
46 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060047 {0xC2, 0x6F, "PowerCap"},
Dhruvaraj S07248292017-03-21 02:14:52 -050048 {0xD8, 0x03, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050049 {0xDA, 0x03, "TurboAllowed"},
Jayanth Othayothd5b2ac02017-03-27 08:19:18 -050050 {0xB4, 0x6F, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060051 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050052};
53
54
Chris Austenac4604a2015-10-13 12:43:27 -050055struct sensor_data_t {
56 uint8_t sennum;
57} __attribute__ ((packed)) ;
58
Chris Austen10ccc0f2015-12-10 18:27:04 -060059struct sensorreadingresp_t {
60 uint8_t value;
61 uint8_t operation;
62 uint8_t indication[2];
63} __attribute__ ((packed)) ;
Chris Austenac4604a2015-10-13 12:43:27 -050064
Tomd700e762016-09-20 18:24:13 +053065// Use a lookup table to find the interface name of a specific sensor
66// This will be used until an alternative is found. this is the first
67// step for mapping IPMI
68int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
69
70 char *str1;
71 sd_bus_error error = SD_BUS_ERROR_NULL;
72 sd_bus_message *reply = NULL, *m=NULL;
73
74
75 int r;
76
77 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
78 if (r < 0) {
79 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
80 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
81 interface->bus, interface->path, interface->interface);
82 goto final;
83 }
84
85 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
86 if (r < 0) {
87 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
88 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
89 interface->bus, interface->path, interface->interface);
90 goto final;
91 }
92
93 r = sd_bus_call(bus, m, 0, &error, &reply);
94 if (r < 0) {
95 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
96 goto final;
97 }
98
99 r = sd_bus_message_read(reply, "v", "s", &str1) ;
100 if (r < 0) {
101 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
102 goto final;
103 }
104
105 strcpy(property_value, str1);
106
107final:
108
109 sd_bus_error_free(&error);
110 m = sd_bus_message_unref(m);
111 reply = sd_bus_message_unref(reply);
112
113 return r;
114}
115
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700116int get_bus_for_path(const char *path, char **busname) {
117 return mapper_get_service(bus, path, busname);
118}
Tomd700e762016-09-20 18:24:13 +0530119
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700120int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
Tomd700e762016-09-20 18:24:13 +0530121 char *busname = NULL;
122 const char *iface = "org.openbmc.managers.System";
123 const char *objname = "/org/openbmc/managers/System";
124 char *str1 = NULL, *str2, *str3;
125 sd_bus_error error = SD_BUS_ERROR_NULL;
126 sd_bus_message *reply = NULL;
127
128
129 int r;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700130 r = get_bus_for_path(objname, &busname);
Tomd700e762016-09-20 18:24:13 +0530131 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400132 fprintf(stderr, "Failed to get %s busname: %s\n",
133 objname, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530134 goto final;
135 }
136
137 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
138 &error, &reply, "sy", type, num);
139 if (r < 0) {
140 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
141 goto final;
142 }
143
144 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
145 if (r < 0) {
146 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
147 goto final;
148 }
149
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700150 r = get_bus_for_path(str2, &str1);
Tomd700e762016-09-20 18:24:13 +0530151 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400152 fprintf(stderr, "Failed to get %s busname: %s\n",
153 str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530154 goto final;
155 }
156
157 strncpy(interface->bus, str1, MAX_DBUS_PATH);
158 strncpy(interface->path, str2, MAX_DBUS_PATH);
159 strncpy(interface->interface, str3, MAX_DBUS_PATH);
160
161 interface->sensornumber = num;
Emily Shaffer71174412017-04-05 15:10:40 -0700162 // Make sure we know that the type hasn't been set, as newer codebase will
163 // set it automatically from the YAML at this step.
164 interface->sensortype = 0;
Tomd700e762016-09-20 18:24:13 +0530165
166final:
167
168 sd_bus_error_free(&error);
169 reply = sd_bus_message_unref(reply);
170 free(busname);
171 free(str1);
172
173 return r;
174}
175
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700176// Use a lookup table to find the interface name of a specific sensor
177// This will be used until an alternative is found. this is the first
178// step for mapping IPMI
179int find_openbmc_path(uint8_t num, dbus_interface_t *interface) {
180 int rc;
181
182 // When the sensor map does not contain the sensor requested,
183 // fall back to the legacy DBus lookup (deprecated)
184 const auto& sensor_it = sensors.find(num);
185 if (sensor_it == sensors.end())
186 {
187 return legacy_dbus_openbmc_path("SENSOR", num, interface);
188 }
189
190 const auto& info = sensor_it->second;
191
Patrick Williams8451edf2017-06-13 09:01:06 -0500192 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700193 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
194 if (rc < 0) {
195 fprintf(stderr, "Failed to get %s busname: %s\n",
196 info.sensorPath.c_str(),
197 busname);
198 goto final;
199 }
200
201 interface->sensortype = info.sensorType;
202 strcpy(interface->bus, busname);
203 strcpy(interface->path, info.sensorPath.c_str());
204 // Take the interface name from the beginning of the DbusInterfaceMap. This
205 // works for the Value interface but may not suffice for more complex
206 // sensors.
207 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500208 strcpy(interface->interface, info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700209 interface->sensornumber = num;
210
211final:
212 free(busname);
213 return rc;
214}
215
Tomd700e762016-09-20 18:24:13 +0530216
217/////////////////////////////////////////////////////////////////////
218//
219// Routines used by ipmi commands wanting to interact on the dbus
220//
221/////////////////////////////////////////////////////////////////////
222int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
223
224
225 dbus_interface_t a;
226 int r;
227 sd_bus_error error = SD_BUS_ERROR_NULL;
228 sd_bus_message *m=NULL;
229
230 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
231 number, method, value);
232
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700233 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530234
235 if (r < 0) {
236 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
237 return 0;
238 }
239
240 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
241 if (r < 0) {
242 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
243 goto final;
244 }
245
246 r = sd_bus_message_append(m, "v", "s", value);
247 if (r < 0) {
248 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
249 goto final;
250 }
251
252
253 r = sd_bus_call(bus, m, 0, &error, NULL);
254 if (r < 0) {
255 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
256 }
257
258final:
259 sd_bus_error_free(&error);
260 m = sd_bus_message_unref(m);
261
262 return 0;
263}
264int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
265
266
267 dbus_interface_t a;
268 int r;
269 sd_bus_error error = SD_BUS_ERROR_NULL;
270 sd_bus_message *m=NULL;
271
272 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
273 number, method, value);
274
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700275 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530276
277 if (r < 0) {
278 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
279 return 0;
280 }
281
282 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
283 if (r < 0) {
284 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
285 goto final;
286 }
287
288 r = sd_bus_message_append(m, "v", "i", value);
289 if (r < 0) {
290 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
291 goto final;
292 }
293
294
295 r = sd_bus_call(bus, m, 0, &error, NULL);
296 if (r < 0) {
297 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
298 }
299
300final:
301 sd_bus_error_free(&error);
302 m = sd_bus_message_unref(m);
303
304 return 0;
305}
306
Chris Austen0012e9b2015-10-22 01:37:46 -0500307uint8_t dbus_to_sensor_type(char *p) {
Chris Austenac4604a2015-10-13 12:43:27 -0500308
Chris Austen0012e9b2015-10-22 01:37:46 -0500309 sensorTypemap_t *s = g_SensorTypeMap;
310 char r=0;
Chris Austenac4604a2015-10-13 12:43:27 -0500311
Chris Austen0012e9b2015-10-22 01:37:46 -0500312 while (s->number != 0xFF) {
313 if (!strcmp(s->dbusname,p)) {
314 r = s->number;
315 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500316 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500317 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500318 }
319
Chris Austend7cf0e42015-11-07 14:27:12 -0600320
Chris Austen0012e9b2015-10-22 01:37:46 -0500321 if (s->number == 0xFF)
322 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500323
Chris Austen0012e9b2015-10-22 01:37:46 -0500324 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500325}
326
Chris Austen0012e9b2015-10-22 01:37:46 -0500327
328uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) {
329 char fru_type_name[64];
330 int r= 0;
331
332 r = find_interface_property_fru_type(a, "fru_type", fru_type_name);
333 if (r<0) {
334 fprintf(stderr, "Failed to get a fru type: %s", strerror(-r));
335 return -1;
336 } else {
337 return dbus_to_sensor_type(fru_type_name);
338 }
339}
340
Emily Shaffer391f3302017-04-03 10:27:08 -0700341uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500342
Chris Austen0012e9b2015-10-22 01:37:46 -0500343 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400344 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500345
Chris Austen0012e9b2015-10-22 01:37:46 -0500346 // This is where sensors that do not exist in dbus but do
347 // exist in the host code stop. This should indicate it
348 // is not a supported sensor
Emily Shaffer391f3302017-04-03 10:27:08 -0700349 if (dbus_if.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500350
Emily Shaffer71174412017-04-05 15:10:40 -0700351 // Fetch type from interface itself.
352 if (dbus_if.sensortype != 0)
353 {
354 type = dbus_if.sensortype;
355 }
356 // Legacy codebase does not populate type during initial handling:
357 else if (strstr(dbus_if.interface, "InventoryItem")) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500358 // InventoryItems are real frus. So need to get the
359 // fru_type property
Emily Shaffer391f3302017-04-03 10:27:08 -0700360 type = dbus_to_sensor_type_from_dbus(&dbus_if);
Chris Austen0012e9b2015-10-22 01:37:46 -0500361 } else {
362 // Non InventoryItems
Emily Shaffer391f3302017-04-03 10:27:08 -0700363 p = strrchr (dbus_if.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400364 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500365 }
366
Brad Bishop56003452016-10-05 21:49:19 -0400367 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500368 }
369
Emily Shaffer391f3302017-04-03 10:27:08 -0700370// Replaces find_sensor
371uint8_t find_type_for_sensor_number(uint8_t num) {
372 int r;
373 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700374 r = find_openbmc_path(num, &dbus_if);
Emily Shaffer391f3302017-04-03 10:27:08 -0700375 if (r < 0) {
376 fprintf(stderr, "Could not find sensor %d\n", num);
377 return r;
378 }
379 return get_type_from_interface(dbus_if);
380}
381
Chris Austen10ccc0f2015-12-10 18:27:04 -0600382
383
384
385
Chris Austen0012e9b2015-10-22 01:37:46 -0500386ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
387 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500388 ipmi_data_len_t data_len, ipmi_context_t context)
389{
390 sensor_data_t *reqptr = (sensor_data_t*)request;
391 ipmi_ret_t rc = IPMI_CC_OK;
392
393 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
394
395 // TODO Not sure what the System-event-sensor is suppose to return
396 // need to ask Hostboot team
397 unsigned char buf[] = {0x00,0x6F};
398
Emily Shaffer391f3302017-04-03 10:27:08 -0700399 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500400
401 // HACK UNTIL Dbus gets updated or we find a better way
402 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600403 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500404 }
405
Chris Austenac4604a2015-10-13 12:43:27 -0500406
407 *data_len = sizeof(buf);
408 memcpy(response, &buf, *data_len);
409
Chris Austenac4604a2015-10-13 12:43:27 -0500410 return rc;
411}
412
Tom Josephbe703f72017-03-09 12:34:35 +0530413ipmi_ret_t setSensorReading(void *request)
414{
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500415 SetSensorReadingReq cmdData =
416 *(static_cast<SetSensorReadingReq *>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530417
418 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500419 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530420 if (iter == sensors.end())
421 {
422 return IPMI_CC_SENSOR_INVALID;
423 }
424
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500425 try
426 {
427 return iter->second.updateFunc(cmdData, iter->second);
428 }
429 catch (InternalFailure& e)
430 {
431 log<level::ERR>("Set sensor failed",
432 entry("SENSOR_NUM=%d", cmdData.number));
433 commit<InternalFailure>();
434 }
435
436 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530437}
Chris Austenac4604a2015-10-13 12:43:27 -0500438
Chris Austen0012e9b2015-10-22 01:37:46 -0500439ipmi_ret_t ipmi_sen_set_sensor(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{
Chris Austenac4604a2015-10-13 12:43:27 -0500443 sensor_data_t *reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500444
Chris Austen0012e9b2015-10-22 01:37:46 -0500445 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500446
Tom Josephbe703f72017-03-09 12:34:35 +0530447 /*
448 * This would support the Set Sensor Reading command for the presence
449 * and functional state of Processor, Core & DIMM. For the remaining
450 * sensors the existing support is invoked.
451 */
452 auto ipmiRC = setSensorReading(request);
453
454 if(ipmiRC == IPMI_CC_SENSOR_INVALID)
455 {
456 updateSensorRecordFromSSRAESC(reqptr);
457 ipmiRC = IPMI_CC_OK;
458 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500459
Chris Austenac4604a2015-10-13 12:43:27 -0500460 *data_len=0;
Tom Josephbe703f72017-03-09 12:34:35 +0530461 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500462}
463
Chris Austen10ccc0f2015-12-10 18:27:04 -0600464
465ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
466 ipmi_request_t request, ipmi_response_t response,
467 ipmi_data_len_t data_len, ipmi_context_t context)
468{
469 sensor_data_t *reqptr = (sensor_data_t*)request;
470 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
471 uint8_t type;
472 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
473 int r;
474 dbus_interface_t a;
475 sd_bus *bus = ipmid_get_sd_bus_connection();
476 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600477 int reading = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600478
Chris Austen10ccc0f2015-12-10 18:27:04 -0600479 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
480
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700481 r = find_openbmc_path(reqptr->sennum, &a);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600482
Nan Li36deb762016-05-12 10:23:41 +0800483 if (r < 0) {
484 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
485 return IPMI_CC_SENSOR_INVALID;
486 }
487
Emily Shaffer391f3302017-04-03 10:27:08 -0700488 type = get_type_from_interface(a);
Brad Bishop56003452016-10-05 21:49:19 -0400489 if(type == 0) {
490 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
491 return IPMI_CC_SENSOR_INVALID;
492 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600493
494 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
495
496 *data_len=0;
497
Emily Shaffer10f49592017-05-10 12:01:10 -0700498 int64_t raw_value;
Emily Shaffer1fabf222017-04-05 08:53:21 -0700499 ipmi::sensor::Info sensor;
500
Chris Austen10ccc0f2015-12-10 18:27:04 -0600501 switch(type) {
502 case 0xC3:
503 case 0xC2:
Adriana Kobylak93125982016-03-01 12:48:10 -0600504 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600505 if (r < 0) {
506 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
507 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
508 a.bus, a.path, a.interface);
509 break;
510 }
511
Adriana Kobylak93125982016-03-01 12:48:10 -0600512 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600513 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600514 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600515 break;
516 }
517
518 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
519
520 rc = IPMI_CC_OK;
521 *data_len=sizeof(sensorreadingresp_t);
522
Adriana Kobylak93125982016-03-01 12:48:10 -0600523 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600524 resp->operation = 0;
525 resp->indication[0] = 0;
526 resp->indication[1] = 0;
527 break;
528
Emily Shaffer1fabf222017-04-05 08:53:21 -0700529 case IPMI_SENSOR_TEMP:
530 case IPMI_SENSOR_VOLTAGE:
531 case IPMI_SENSOR_CURRENT:
532 case IPMI_SENSOR_FAN:
533 // Get reading for /xyz/openbmc_project/Sensor/Value.interface
Emily Shaffer10f49592017-05-10 12:01:10 -0700534 if(sensors.find(reqptr->sennum) == sensors.end())
535 {
536 fprintf(stderr, "Failed to find config entry for Sensor 0x%02x\n",
537 reqptr->sennum);
538 return IPMI_CC_SENSOR_INVALID;
539 }
540
541 sensor = sensors.at(reqptr->sennum);
Emily Shaffer1fabf222017-04-05 08:53:21 -0700542
543 // Get value
544 r = sd_bus_get_property_trivial(bus,
545 a.bus,
546 a.path,
547 a.interface,
548 "Value",
549 NULL,
550 'x',
551 &raw_value);
552 if (r < 0) {
553 fprintf(stderr,
554 "Failed to call sd_bus_get_property:%d, %s, 'value'\n",
555 r,
556 strerror(-r));
557 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
558 a.bus, a.path, a.interface);
559 break;
560 }
561
Emily Shaffer10f49592017-05-10 12:01:10 -0700562 // Prevent div0
563 if (sensor.coefficientM == 0) {
564 sensor.coefficientM = 1;
565 };
Emily Shaffer1fabf222017-04-05 08:53:21 -0700566
Emily Shaffer10f49592017-05-10 12:01:10 -0700567 resp->value = static_cast<uint8_t>(
568 (raw_value - sensor.scaledOffset) / sensor.coefficientM);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700569 resp->operation = 1<<6; // scanning enabled
570 resp->indication[0] = 0; // not a threshold sensor. ignore
Emily Shaffer1fabf222017-04-05 08:53:21 -0700571 resp->indication[1] = 0;
572 rc = IPMI_CC_OK;
573 *data_len=sizeof(sensorreadingresp_t);
574 break;
Tom Joseph60cac722017-08-18 12:06:07 +0530575 case IPMI_SENSOR_TPM:
576 {
577 auto iter = sensors.find(reqptr->sennum);
578 if (iter == sensors.end())
579 {
580 return IPMI_CC_SENSOR_INVALID;
581 }
Emily Shaffer1fabf222017-04-05 08:53:21 -0700582
Tom Joseph60cac722017-08-18 12:06:07 +0530583 auto& interfaceList = iter->second.propertyInterfaces;
584 if (interfaceList.empty())
585 {
586 log<level::ERR>("Interface List empty for the sensor",
587 entry("SENSOR_NUMBER=%d", reqptr->sennum));
588 return IPMI_CC_UNSPECIFIED_ERROR;
589 }
590
591 /* For the TPM sensor there is no reading value and sensor scanning
592 * is disabled. This is a discrete sensor and only the
593 * corresponding state is asserted.
594 */
595 resp->value = 0;
596 resp->operation = 0;
597 resp->indication[0] = 0;
598 resp->indication[1] = 0;
599
600 try
601 {
602 for (const auto& interface : interfaceList)
603 {
604 for (const auto& property : interface.second)
605 {
606 sdbusplus::bus::bus dbus{ipmid_get_sd_bus_connection()};
607
608 auto service = ipmi::getService(
609 dbus,
610 interface.first,
611 iter->second.sensorPath);
612
613 auto propValue = ipmi::getDbusProperty(
614 dbus, service, iter->second.sensorPath,
615 interface.first, property.first);
616
617 auto tpmStatus = propValue.get<bool>();
618
619 for (const auto& value : property.second)
620 {
621 if (tpmStatus == (value.second.assert).get<bool>())
622 {
623 /*
624 * The discrete sensors support upto 14 states.
625 * The assertion states for discrete sensors are
626 * stored in 2 bytes, 0-7 in Byte 4 of the
627 * response and 8-14 in Byte 5 of the response.
628 */
629 resp->indication[0] |= 1 << (value.first);
630 break;
631 }
632 }
633 }
634 }
635
636 rc = IPMI_CC_OK;
637 *data_len = sizeof(sensorreadingresp_t);
638 }
639 catch (InternalFailure& e)
640 {
641 return IPMI_CC_UNSPECIFIED_ERROR;
642 }
643 break;
644 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600645 default:
646 *data_len=0;
647 rc = IPMI_CC_SENSOR_INVALID;
648 break;
649 }
650
vishwa1eaea4f2016-02-26 11:57:40 -0600651 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600652
653 return rc;
654}
655
Chris Austen0012e9b2015-10-22 01:37:46 -0500656ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
657 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500658 ipmi_data_len_t data_len, ipmi_context_t context)
659{
Nan Li70aa8d92016-08-29 00:11:10 +0800660 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500661
662 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
663 *data_len = 0;
664
665 return rc;
666}
667
Emily Shafferd06e0e72017-04-05 09:08:57 -0700668ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
669 ipmi_request_t request,
670 ipmi_response_t response,
671 ipmi_data_len_t data_len,
672 ipmi_context_t context)
673{
674 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
675 if (request == nullptr ||
676 get_sdr_info::request::get_count(request) == false)
677 {
678 // Get Sensor Count
679 resp->count = sensors.size();
680 }
681 else
682 {
683 resp->count = 1;
684 }
685
686 // Multiple LUNs not supported.
687 namespace response = get_sdr_info::response;
688 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
689 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
690 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
691 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
692 response::set_static_population(&(resp->luns_and_dynamic_population));
693
694 *data_len = SDR_INFO_RESP_SIZE;
695
696 return IPMI_CC_OK;
697}
698
Emily Shaffera344afc2017-04-13 15:09:39 -0700699ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
700 ipmi_request_t request,
701 ipmi_response_t response,
702 ipmi_data_len_t data_len,
703 ipmi_context_t context)
704{
705 // A constant reservation ID is okay until we implement add/remove SDR.
706 const uint16_t reservation_id = 1;
707 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700708 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700709
710 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
711 return IPMI_CC_OK;
712}
Chris Austenac4604a2015-10-13 12:43:27 -0500713
Emily Shafferbbef71c2017-05-08 16:36:17 -0700714ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
715 const ipmi::sensor::Info *info,
716 ipmi_data_len_t data_len)
717{
718 /* Functional sensor case */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500719 if (info->propertyInterfaces.begin()->first ==
Emily Shafferbbef71c2017-05-08 16:36:17 -0700720 "xyz.openbmc_project.Sensor.Value")
721 {
722 // Get bus
723 sd_bus *bus = ipmid_get_sd_bus_connection();
724 dbus_interface_t iface;
725
726 if (0 > find_openbmc_path(body->entity_id, &iface))
727 return IPMI_CC_SENSOR_INVALID;
728
729 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
730
731 /* Unit info */
732 char *raw_cstr;
733 if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
734 iface.interface, "Unit", NULL,
735 &raw_cstr)) {
736 fprintf(stderr, "Expected to find Unit interface in bus %s, path %s, but it was missing.\n",
737 iface.bus, iface.path);
738 return IPMI_CC_SENSOR_INVALID;
739 }
740
741 std::string raw_str(raw_cstr);
742 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
743 server::Value::Unit unit =
744 server::Value::convertUnitFromString(raw_str);
745
746 // Unit strings defined in
747 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
748 switch (unit)
749 {
750 case server::Value::Unit::DegreesC:
751 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
752 break;
753 case server::Value::Unit::RPMS:
754 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
755 get_sdr::body::set_rate_unit(0b100, body); // per minute
756 break;
757 case server::Value::Unit::Volts:
758 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
759 break;
760 case server::Value::Unit::Meters:
761 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
762 break;
763 case server::Value::Unit::Amperes:
764 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
765 break;
766 case server::Value::Unit::Joules:
767 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
768 break;
769 default:
770 fprintf(stderr, "Unknown value unit type: = %s\n", raw_cstr);
771 }
772
773 free(raw_cstr);
774
775 /* Modifiers to reading info */
Emily Shaffer10f49592017-05-10 12:01:10 -0700776 // Get scale
777 int64_t scale;
778 if (0 > sd_bus_get_property_trivial(bus,
779 iface.bus,
780 iface.path,
781 iface.interface,
782 "Scale",
783 NULL,
784 'x',
785 &scale)) {
786 fprintf(stderr, "Expected to find Scale interface in bus %s, path %s, but it was missing.\n",
787 iface.bus, iface.path);
788 return IPMI_CC_SENSOR_INVALID;
789 }
790
791 get_sdr::body::set_b(info->coefficientB, body);
792 get_sdr::body::set_m(info->coefficientM, body);
793 get_sdr::body::set_b_exp(info->exponentB, body);
794 get_sdr::body::set_r_exp(scale, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700795
796 /* ID string */
797 std::string id_string = info->sensorPath.substr(
798 info->sensorPath.find_last_of('/')+1, info->sensorPath.length());
799 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
800 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
801 {
802 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
803 }
804 else
805 {
806 get_sdr::body::set_id_strlen(id_string.length(), body);
807 }
808 strncpy(body->id_string, id_string.c_str(),
809 get_sdr::body::get_id_strlen(body));
810 }
811
812 return IPMI_CC_OK;
813};
814
815ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
816 ipmi_request_t request, ipmi_response_t response,
817 ipmi_data_len_t data_len, ipmi_context_t context)
818{
819 ipmi_ret_t ret = IPMI_CC_OK;
820 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
821 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
822 get_sdr::SensorDataFullRecord record = {0};
823 if (req != NULL)
824 {
825 // Note: we use an iterator so we can provide the next ID at the end of
826 // the call.
827 auto sensor = sensors.begin();
828
829 // At the beginning of a scan, the host side will send us id=0.
830 if (get_sdr::request::get_record_id(req) != 0)
831 {
832 sensor = sensors.find(get_sdr::request::get_record_id(req));
833 if(sensor == sensors.end()) {
834 return IPMI_CC_SENSOR_INVALID;
835 }
836 }
837
838 uint8_t sensor_id = sensor->first;
839
840 /* Header */
841 get_sdr::header::set_record_id(sensor_id, &(record.header));
842 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
843 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
844 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
845
846 /* Key */
847 record.key.sensor_number = sensor_id;
848
849 /* Body */
850 record.body.entity_id = sensor_id;
851 record.body.sensor_type = sensor->second.sensorType;
852 record.body.event_reading_type = sensor->second.sensorReadingType;
853
854 // Set the type-specific details given the DBus interface
855 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
856 data_len);
857
858 if (++sensor == sensors.end())
859 {
860 get_sdr::response::set_next_record_id(0xFFFF, resp); // last record
861 }
862 else
863 {
864 get_sdr::response::set_next_record_id(sensor->first, resp);
865 }
866
867 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
868 memcpy(resp->record_data, (char*)&record + req->offset,
869 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
870 }
871
872 return ret;
873}
874
875
Chris Austenac4604a2015-10-13 12:43:27 -0500876void register_netfn_sen_functions()
877{
Tom05732372016-09-06 17:21:23 +0530878 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -0700879 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
880 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
881 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
882 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +0530883 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500884
Tom05732372016-09-06 17:21:23 +0530885 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -0700886 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
887 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
888 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
889 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +0530890 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500891
Tom05732372016-09-06 17:21:23 +0530892 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -0700893 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
894 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
895 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
896 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +0530897 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500898
Tom05732372016-09-06 17:21:23 +0530899 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -0700900 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
901 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
902 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
903 nullptr, ipmi_sen_get_sensor_reading,
904 PRIVILEGE_USER);
905
906 // <Reserve SDR>
907 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
908 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
909 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
910 nullptr, ipmi_sen_reserve_sdr,
911 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600912
Emily Shafferd06e0e72017-04-05 09:08:57 -0700913 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -0700914 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
915 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
916 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
917 nullptr, ipmi_sen_get_sdr_info,
918 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700919
920 // <Get SDR>
921 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
922 NETFUN_SENSOR, IPMI_CMD_GET_SDR);
923 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR,
924 nullptr, ipmi_sen_get_sdr,
925 PRIVILEGE_USER);
926
Chris Austenac4604a2015-10-13 12:43:27 -0500927 return;
928}