blob: 4562476829b04de243bee531ab7a0e4db3ca929d [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"},
Tom Joseph558184e2017-09-01 13:45:05 +053048 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050049 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053050 {0xD8, 0xC8, "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 Austen0012e9b2015-10-22 01:37:46 -0500311 while (s->number != 0xFF) {
312 if (!strcmp(s->dbusname,p)) {
Tom Joseph558184e2017-09-01 13:45:05 +0530313 r = s->typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -0500314 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500315 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500316 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500317 }
318
Chris Austen0012e9b2015-10-22 01:37:46 -0500319 if (s->number == 0xFF)
320 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500321
Chris Austen0012e9b2015-10-22 01:37:46 -0500322 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500323}
324
Chris Austen0012e9b2015-10-22 01:37:46 -0500325
326uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) {
327 char fru_type_name[64];
328 int r= 0;
329
330 r = find_interface_property_fru_type(a, "fru_type", fru_type_name);
331 if (r<0) {
332 fprintf(stderr, "Failed to get a fru type: %s", strerror(-r));
333 return -1;
334 } else {
335 return dbus_to_sensor_type(fru_type_name);
336 }
337}
338
Emily Shaffer391f3302017-04-03 10:27:08 -0700339uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500340
Chris Austen0012e9b2015-10-22 01:37:46 -0500341 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400342 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500343
Chris Austen0012e9b2015-10-22 01:37:46 -0500344 // This is where sensors that do not exist in dbus but do
345 // exist in the host code stop. This should indicate it
346 // is not a supported sensor
Emily Shaffer391f3302017-04-03 10:27:08 -0700347 if (dbus_if.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500348
Emily Shaffer71174412017-04-05 15:10:40 -0700349 // Fetch type from interface itself.
350 if (dbus_if.sensortype != 0)
351 {
352 type = dbus_if.sensortype;
353 }
354 // Legacy codebase does not populate type during initial handling:
355 else if (strstr(dbus_if.interface, "InventoryItem")) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500356 // InventoryItems are real frus. So need to get the
357 // fru_type property
Emily Shaffer391f3302017-04-03 10:27:08 -0700358 type = dbus_to_sensor_type_from_dbus(&dbus_if);
Chris Austen0012e9b2015-10-22 01:37:46 -0500359 } else {
360 // Non InventoryItems
Emily Shaffer391f3302017-04-03 10:27:08 -0700361 p = strrchr (dbus_if.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400362 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500363 }
364
Brad Bishop56003452016-10-05 21:49:19 -0400365 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500366 }
367
Emily Shaffer391f3302017-04-03 10:27:08 -0700368// Replaces find_sensor
369uint8_t find_type_for_sensor_number(uint8_t num) {
370 int r;
371 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700372 r = find_openbmc_path(num, &dbus_if);
Emily Shaffer391f3302017-04-03 10:27:08 -0700373 if (r < 0) {
374 fprintf(stderr, "Could not find sensor %d\n", num);
375 return r;
376 }
377 return get_type_from_interface(dbus_if);
378}
379
Chris Austen10ccc0f2015-12-10 18:27:04 -0600380
381
382
383
Chris Austen0012e9b2015-10-22 01:37:46 -0500384ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
385 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500386 ipmi_data_len_t data_len, ipmi_context_t context)
387{
388 sensor_data_t *reqptr = (sensor_data_t*)request;
389 ipmi_ret_t rc = IPMI_CC_OK;
390
391 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
392
393 // TODO Not sure what the System-event-sensor is suppose to return
394 // need to ask Hostboot team
395 unsigned char buf[] = {0x00,0x6F};
396
Emily Shaffer391f3302017-04-03 10:27:08 -0700397 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500398
399 // HACK UNTIL Dbus gets updated or we find a better way
400 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600401 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500402 }
403
Chris Austenac4604a2015-10-13 12:43:27 -0500404
405 *data_len = sizeof(buf);
406 memcpy(response, &buf, *data_len);
407
Chris Austenac4604a2015-10-13 12:43:27 -0500408 return rc;
409}
410
Tom Josephbe703f72017-03-09 12:34:35 +0530411ipmi_ret_t setSensorReading(void *request)
412{
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500413 SetSensorReadingReq cmdData =
414 *(static_cast<SetSensorReadingReq *>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530415
416 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500417 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530418 if (iter == sensors.end())
419 {
420 return IPMI_CC_SENSOR_INVALID;
421 }
422
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500423 try
424 {
425 return iter->second.updateFunc(cmdData, iter->second);
426 }
427 catch (InternalFailure& e)
428 {
429 log<level::ERR>("Set sensor failed",
430 entry("SENSOR_NUM=%d", cmdData.number));
431 commit<InternalFailure>();
432 }
433
434 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530435}
Chris Austenac4604a2015-10-13 12:43:27 -0500436
Chris Austen0012e9b2015-10-22 01:37:46 -0500437ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
438 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500439 ipmi_data_len_t data_len, ipmi_context_t context)
440{
Chris Austenac4604a2015-10-13 12:43:27 -0500441 sensor_data_t *reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500442
Chris Austen0012e9b2015-10-22 01:37:46 -0500443 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500444
Tom Josephbe703f72017-03-09 12:34:35 +0530445 /*
446 * This would support the Set Sensor Reading command for the presence
447 * and functional state of Processor, Core & DIMM. For the remaining
448 * sensors the existing support is invoked.
449 */
450 auto ipmiRC = setSensorReading(request);
451
452 if(ipmiRC == IPMI_CC_SENSOR_INVALID)
453 {
454 updateSensorRecordFromSSRAESC(reqptr);
455 ipmiRC = IPMI_CC_OK;
456 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500457
Chris Austenac4604a2015-10-13 12:43:27 -0500458 *data_len=0;
Tom Josephbe703f72017-03-09 12:34:35 +0530459 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500460}
461
Chris Austen10ccc0f2015-12-10 18:27:04 -0600462
463ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
464 ipmi_request_t request, ipmi_response_t response,
465 ipmi_data_len_t data_len, ipmi_context_t context)
466{
467 sensor_data_t *reqptr = (sensor_data_t*)request;
468 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
469 uint8_t type;
470 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
471 int r;
472 dbus_interface_t a;
473 sd_bus *bus = ipmid_get_sd_bus_connection();
474 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600475 int reading = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600476
Chris Austen10ccc0f2015-12-10 18:27:04 -0600477 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
478
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700479 r = find_openbmc_path(reqptr->sennum, &a);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600480
Nan Li36deb762016-05-12 10:23:41 +0800481 if (r < 0) {
482 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
483 return IPMI_CC_SENSOR_INVALID;
484 }
485
Emily Shaffer391f3302017-04-03 10:27:08 -0700486 type = get_type_from_interface(a);
Brad Bishop56003452016-10-05 21:49:19 -0400487 if(type == 0) {
488 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
489 return IPMI_CC_SENSOR_INVALID;
490 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600491
492 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
493
494 *data_len=0;
495
Emily Shaffer10f49592017-05-10 12:01:10 -0700496 int64_t raw_value;
Emily Shaffer1fabf222017-04-05 08:53:21 -0700497 ipmi::sensor::Info sensor;
498
Chris Austen10ccc0f2015-12-10 18:27:04 -0600499 switch(type) {
500 case 0xC3:
501 case 0xC2:
Tom Joseph558184e2017-09-01 13:45:05 +0530502 case 0xC8:
Adriana Kobylak93125982016-03-01 12:48:10 -0600503 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600504 if (r < 0) {
505 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
506 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
507 a.bus, a.path, a.interface);
508 break;
509 }
510
Adriana Kobylak93125982016-03-01 12:48:10 -0600511 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600512 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600513 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600514 break;
515 }
516
517 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
518
519 rc = IPMI_CC_OK;
520 *data_len=sizeof(sensorreadingresp_t);
521
Adriana Kobylak93125982016-03-01 12:48:10 -0600522 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600523 resp->operation = 0;
524 resp->indication[0] = 0;
525 resp->indication[1] = 0;
526 break;
527
Emily Shaffer1fabf222017-04-05 08:53:21 -0700528 case IPMI_SENSOR_TEMP:
529 case IPMI_SENSOR_VOLTAGE:
530 case IPMI_SENSOR_CURRENT:
531 case IPMI_SENSOR_FAN:
532 // Get reading for /xyz/openbmc_project/Sensor/Value.interface
Emily Shaffer10f49592017-05-10 12:01:10 -0700533 if(sensors.find(reqptr->sennum) == sensors.end())
534 {
535 fprintf(stderr, "Failed to find config entry for Sensor 0x%02x\n",
536 reqptr->sennum);
537 return IPMI_CC_SENSOR_INVALID;
538 }
539
540 sensor = sensors.at(reqptr->sennum);
Emily Shaffer1fabf222017-04-05 08:53:21 -0700541
542 // Get value
543 r = sd_bus_get_property_trivial(bus,
544 a.bus,
545 a.path,
546 a.interface,
547 "Value",
548 NULL,
549 'x',
550 &raw_value);
551 if (r < 0) {
552 fprintf(stderr,
553 "Failed to call sd_bus_get_property:%d, %s, 'value'\n",
554 r,
555 strerror(-r));
556 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
557 a.bus, a.path, a.interface);
558 break;
559 }
560
Emily Shaffer10f49592017-05-10 12:01:10 -0700561 // Prevent div0
562 if (sensor.coefficientM == 0) {
563 sensor.coefficientM = 1;
564 };
Emily Shaffer1fabf222017-04-05 08:53:21 -0700565
Emily Shaffer10f49592017-05-10 12:01:10 -0700566 resp->value = static_cast<uint8_t>(
567 (raw_value - sensor.scaledOffset) / sensor.coefficientM);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700568 resp->operation = 1<<6; // scanning enabled
569 resp->indication[0] = 0; // not a threshold sensor. ignore
Emily Shaffer1fabf222017-04-05 08:53:21 -0700570 resp->indication[1] = 0;
571 rc = IPMI_CC_OK;
572 *data_len=sizeof(sensorreadingresp_t);
573 break;
Tom Joseph60cac722017-08-18 12:06:07 +0530574 case IPMI_SENSOR_TPM:
575 {
576 auto iter = sensors.find(reqptr->sennum);
577 if (iter == sensors.end())
578 {
579 return IPMI_CC_SENSOR_INVALID;
580 }
Emily Shaffer1fabf222017-04-05 08:53:21 -0700581
Tom Joseph60cac722017-08-18 12:06:07 +0530582 auto& interfaceList = iter->second.propertyInterfaces;
583 if (interfaceList.empty())
584 {
585 log<level::ERR>("Interface List empty for the sensor",
586 entry("SENSOR_NUMBER=%d", reqptr->sennum));
587 return IPMI_CC_UNSPECIFIED_ERROR;
588 }
589
590 /* For the TPM sensor there is no reading value and sensor scanning
591 * is disabled. This is a discrete sensor and only the
592 * corresponding state is asserted.
593 */
594 resp->value = 0;
595 resp->operation = 0;
596 resp->indication[0] = 0;
597 resp->indication[1] = 0;
598
599 try
600 {
601 for (const auto& interface : interfaceList)
602 {
603 for (const auto& property : interface.second)
604 {
605 sdbusplus::bus::bus dbus{ipmid_get_sd_bus_connection()};
606
607 auto service = ipmi::getService(
608 dbus,
609 interface.first,
610 iter->second.sensorPath);
611
612 auto propValue = ipmi::getDbusProperty(
613 dbus, service, iter->second.sensorPath,
614 interface.first, property.first);
615
616 auto tpmStatus = propValue.get<bool>();
617
618 for (const auto& value : property.second)
619 {
620 if (tpmStatus == (value.second.assert).get<bool>())
621 {
622 /*
623 * The discrete sensors support upto 14 states.
624 * The assertion states for discrete sensors are
625 * stored in 2 bytes, 0-7 in Byte 4 of the
626 * response and 8-14 in Byte 5 of the response.
627 */
628 resp->indication[0] |= 1 << (value.first);
629 break;
630 }
631 }
632 }
633 }
634
635 rc = IPMI_CC_OK;
636 *data_len = sizeof(sensorreadingresp_t);
637 }
638 catch (InternalFailure& e)
639 {
640 return IPMI_CC_UNSPECIFIED_ERROR;
641 }
642 break;
643 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600644 default:
645 *data_len=0;
646 rc = IPMI_CC_SENSOR_INVALID;
647 break;
648 }
649
vishwa1eaea4f2016-02-26 11:57:40 -0600650 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600651
652 return rc;
653}
654
Chris Austen0012e9b2015-10-22 01:37:46 -0500655ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
656 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500657 ipmi_data_len_t data_len, ipmi_context_t context)
658{
Nan Li70aa8d92016-08-29 00:11:10 +0800659 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500660
661 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
662 *data_len = 0;
663
664 return rc;
665}
666
Emily Shafferd06e0e72017-04-05 09:08:57 -0700667ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
668 ipmi_request_t request,
669 ipmi_response_t response,
670 ipmi_data_len_t data_len,
671 ipmi_context_t context)
672{
673 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
674 if (request == nullptr ||
675 get_sdr_info::request::get_count(request) == false)
676 {
677 // Get Sensor Count
678 resp->count = sensors.size();
679 }
680 else
681 {
682 resp->count = 1;
683 }
684
685 // Multiple LUNs not supported.
686 namespace response = get_sdr_info::response;
687 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
688 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
689 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
690 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
691 response::set_static_population(&(resp->luns_and_dynamic_population));
692
693 *data_len = SDR_INFO_RESP_SIZE;
694
695 return IPMI_CC_OK;
696}
697
Emily Shaffera344afc2017-04-13 15:09:39 -0700698ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
699 ipmi_request_t request,
700 ipmi_response_t response,
701 ipmi_data_len_t data_len,
702 ipmi_context_t context)
703{
704 // A constant reservation ID is okay until we implement add/remove SDR.
705 const uint16_t reservation_id = 1;
706 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700707 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700708
709 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
710 return IPMI_CC_OK;
711}
Chris Austenac4604a2015-10-13 12:43:27 -0500712
Emily Shafferbbef71c2017-05-08 16:36:17 -0700713ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
714 const ipmi::sensor::Info *info,
715 ipmi_data_len_t data_len)
716{
717 /* Functional sensor case */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500718 if (info->propertyInterfaces.begin()->first ==
Emily Shafferbbef71c2017-05-08 16:36:17 -0700719 "xyz.openbmc_project.Sensor.Value")
720 {
721 // Get bus
722 sd_bus *bus = ipmid_get_sd_bus_connection();
723 dbus_interface_t iface;
724
725 if (0 > find_openbmc_path(body->entity_id, &iface))
726 return IPMI_CC_SENSOR_INVALID;
727
728 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
729
730 /* Unit info */
731 char *raw_cstr;
732 if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
733 iface.interface, "Unit", NULL,
734 &raw_cstr)) {
735 fprintf(stderr, "Expected to find Unit interface in bus %s, path %s, but it was missing.\n",
736 iface.bus, iface.path);
737 return IPMI_CC_SENSOR_INVALID;
738 }
739
740 std::string raw_str(raw_cstr);
741 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
742 server::Value::Unit unit =
743 server::Value::convertUnitFromString(raw_str);
744
745 // Unit strings defined in
746 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
747 switch (unit)
748 {
749 case server::Value::Unit::DegreesC:
750 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
751 break;
752 case server::Value::Unit::RPMS:
753 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
754 get_sdr::body::set_rate_unit(0b100, body); // per minute
755 break;
756 case server::Value::Unit::Volts:
757 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
758 break;
759 case server::Value::Unit::Meters:
760 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
761 break;
762 case server::Value::Unit::Amperes:
763 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
764 break;
765 case server::Value::Unit::Joules:
766 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
767 break;
768 default:
769 fprintf(stderr, "Unknown value unit type: = %s\n", raw_cstr);
770 }
771
772 free(raw_cstr);
773
774 /* Modifiers to reading info */
Emily Shaffer10f49592017-05-10 12:01:10 -0700775 // Get scale
776 int64_t scale;
777 if (0 > sd_bus_get_property_trivial(bus,
778 iface.bus,
779 iface.path,
780 iface.interface,
781 "Scale",
782 NULL,
783 'x',
784 &scale)) {
785 fprintf(stderr, "Expected to find Scale interface in bus %s, path %s, but it was missing.\n",
786 iface.bus, iface.path);
787 return IPMI_CC_SENSOR_INVALID;
788 }
789
790 get_sdr::body::set_b(info->coefficientB, body);
791 get_sdr::body::set_m(info->coefficientM, body);
792 get_sdr::body::set_b_exp(info->exponentB, body);
793 get_sdr::body::set_r_exp(scale, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700794
795 /* ID string */
796 std::string id_string = info->sensorPath.substr(
797 info->sensorPath.find_last_of('/')+1, info->sensorPath.length());
798 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
799 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
800 {
801 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
802 }
803 else
804 {
805 get_sdr::body::set_id_strlen(id_string.length(), body);
806 }
807 strncpy(body->id_string, id_string.c_str(),
808 get_sdr::body::get_id_strlen(body));
809 }
810
811 return IPMI_CC_OK;
812};
813
814ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
815 ipmi_request_t request, ipmi_response_t response,
816 ipmi_data_len_t data_len, ipmi_context_t context)
817{
818 ipmi_ret_t ret = IPMI_CC_OK;
819 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
820 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
821 get_sdr::SensorDataFullRecord record = {0};
822 if (req != NULL)
823 {
824 // Note: we use an iterator so we can provide the next ID at the end of
825 // the call.
826 auto sensor = sensors.begin();
827
828 // At the beginning of a scan, the host side will send us id=0.
829 if (get_sdr::request::get_record_id(req) != 0)
830 {
831 sensor = sensors.find(get_sdr::request::get_record_id(req));
832 if(sensor == sensors.end()) {
833 return IPMI_CC_SENSOR_INVALID;
834 }
835 }
836
837 uint8_t sensor_id = sensor->first;
838
839 /* Header */
840 get_sdr::header::set_record_id(sensor_id, &(record.header));
841 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
842 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
843 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
844
845 /* Key */
846 record.key.sensor_number = sensor_id;
847
848 /* Body */
849 record.body.entity_id = sensor_id;
850 record.body.sensor_type = sensor->second.sensorType;
851 record.body.event_reading_type = sensor->second.sensorReadingType;
852
853 // Set the type-specific details given the DBus interface
854 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
855 data_len);
856
857 if (++sensor == sensors.end())
858 {
859 get_sdr::response::set_next_record_id(0xFFFF, resp); // last record
860 }
861 else
862 {
863 get_sdr::response::set_next_record_id(sensor->first, resp);
864 }
865
866 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
867 memcpy(resp->record_data, (char*)&record + req->offset,
868 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
869 }
870
871 return ret;
872}
873
874
Chris Austenac4604a2015-10-13 12:43:27 -0500875void register_netfn_sen_functions()
876{
Tom05732372016-09-06 17:21:23 +0530877 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -0700878 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
879 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
880 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
881 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +0530882 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500883
Tom05732372016-09-06 17:21:23 +0530884 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -0700885 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
886 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
887 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
888 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +0530889 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500890
Tom05732372016-09-06 17:21:23 +0530891 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -0700892 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
893 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
894 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
895 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +0530896 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500897
Tom05732372016-09-06 17:21:23 +0530898 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -0700899 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
900 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
901 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
902 nullptr, ipmi_sen_get_sensor_reading,
903 PRIVILEGE_USER);
904
905 // <Reserve SDR>
906 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
907 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
908 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
909 nullptr, ipmi_sen_reserve_sdr,
910 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600911
Emily Shafferd06e0e72017-04-05 09:08:57 -0700912 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -0700913 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
914 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
915 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
916 nullptr, ipmi_sen_get_sdr_info,
917 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700918
919 // <Get SDR>
920 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
921 NETFUN_SENSOR, IPMI_CMD_GET_SDR);
922 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR,
923 nullptr, ipmi_sen_get_sdr,
924 PRIVILEGE_USER);
925
Chris Austenac4604a2015-10-13 12:43:27 -0500926 return;
927}