blob: 41d393b393fe672d450b5344ee0185c6f88918a7 [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "sensorhandler.h"
2
3#include "fruread.hpp"
4#include "ipmid.hpp"
5#include "types.hpp"
6#include "utils.hpp"
7
Tomd700e762016-09-20 18:24:13 +05308#include <mapper.h>
Emily Shaffer1fabf222017-04-05 08:53:21 -07009#include <math.h>
Chris Austenac4604a2015-10-13 12:43:27 -050010#include <stdio.h>
11#include <string.h>
Chris Austen10ccc0f2015-12-10 18:27:04 -060012#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070013
14#include <bitset>
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050015#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070016#include <phosphor-logging/log.hpp>
17#include <set>
18#include <xyz/openbmc_project/Common/error.hpp>
19#include <xyz/openbmc_project/Sensor/Value/server.hpp>
20
21#include "host-ipmid/ipmid-api.h"
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050022
Ratan Guptae0cc8552018-01-22 14:23:04 +053023static constexpr uint8_t fruInventoryDevice = 0x10;
24static constexpr uint8_t IPMIFruInventory = 0x02;
25static constexpr uint8_t BMCSlaveAddress = 0x20;
26
Patrick Venture0b02be92018-08-31 11:55:55 -070027extern int updateSensorRecordFromSSRAESC(const void*);
28extern sd_bus* bus;
Tom Josephbe703f72017-03-09 12:34:35 +053029extern const ipmi::sensor::IdInfoMap sensors;
Ratan Guptae0cc8552018-01-22 14:23:04 +053030extern const FruMap frus;
31
Tom Josephbe703f72017-03-09 12:34:35 +053032using namespace phosphor::logging;
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050033using InternalFailure =
34 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austenac4604a2015-10-13 12:43:27 -050035
Patrick Venture0b02be92018-08-31 11:55:55 -070036void register_netfn_sen_functions() __attribute__((constructor));
Chris Austenac4604a2015-10-13 12:43:27 -050037
Patrick Venture0b02be92018-08-31 11:55:55 -070038struct sensorTypemap_t
39{
Chris Austen0012e9b2015-10-22 01:37:46 -050040 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060041 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050042 char dbusname[32];
Patrick Venture0b02be92018-08-31 11:55:55 -070043};
Chris Austen0012e9b2015-10-22 01:37:46 -050044
Chris Austen0012e9b2015-10-22 01:37:46 -050045sensorTypemap_t g_SensorTypeMap[] = {
46
Chris Austend7cf0e42015-11-07 14:27:12 -060047 {0x01, 0x6F, "Temp"},
48 {0x0C, 0x6F, "DIMM"},
49 {0x0C, 0x6F, "MEMORY_BUFFER"},
50 {0x07, 0x6F, "PROC"},
51 {0x07, 0x6F, "CORE"},
52 {0x07, 0x6F, "CPU"},
53 {0x0F, 0x6F, "BootProgress"},
Patrick Venture0b02be92018-08-31 11:55:55 -070054 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor
55 // type code os 0x09
Chris Austend7cf0e42015-11-07 14:27:12 -060056 {0xC3, 0x6F, "BootCount"},
57 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060058 {0x12, 0x6F, "SYSTEM_EVENT"},
59 {0xC7, 0x03, "SYSTEM"},
60 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060061 {0xC2, 0x6F, "PowerCap"},
Tom Joseph558184e2017-09-01 13:45:05 +053062 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050063 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053064 {0xD8, 0xC8, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060065 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050066};
67
Patrick Venture0b02be92018-08-31 11:55:55 -070068struct sensor_data_t
69{
Chris Austenac4604a2015-10-13 12:43:27 -050070 uint8_t sennum;
Patrick Venture0b02be92018-08-31 11:55:55 -070071} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050072
Patrick Venture0b02be92018-08-31 11:55:55 -070073struct sensorreadingresp_t
74{
Chris Austen10ccc0f2015-12-10 18:27:04 -060075 uint8_t value;
76 uint8_t operation;
77 uint8_t indication[2];
Patrick Venture0b02be92018-08-31 11:55:55 -070078} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050079
Patrick Venture0b02be92018-08-31 11:55:55 -070080int get_bus_for_path(const char* path, char** busname)
81{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070082 return mapper_get_service(bus, path, busname);
83}
Tomd700e762016-09-20 18:24:13 +053084
Patrick Venture0b02be92018-08-31 11:55:55 -070085int legacy_dbus_openbmc_path(const char* type, const uint8_t num,
86 dbus_interface_t* interface)
87{
88 char* busname = NULL;
89 const char* iface = "org.openbmc.managers.System";
90 const char* objname = "/org/openbmc/managers/System";
91 char *str1 = NULL, *str2, *str3;
Tomd700e762016-09-20 18:24:13 +053092 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -070093 sd_bus_message* reply = NULL;
Tomd700e762016-09-20 18:24:13 +053094
95 int r;
Emily Shaffer2ae09b92017-04-05 15:09:41 -070096 r = get_bus_for_path(objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -070097 if (r < 0)
98 {
99 fprintf(stderr, "Failed to get %s busname: %s\n", objname,
100 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530101 goto final;
102 }
103
Patrick Venture0b02be92018-08-31 11:55:55 -0700104 r = sd_bus_call_method(bus, busname, objname, iface, "getObjectFromByteId",
Tomd700e762016-09-20 18:24:13 +0530105 &error, &reply, "sy", type, num);
Patrick Venture0b02be92018-08-31 11:55:55 -0700106 if (r < 0)
107 {
Tomd700e762016-09-20 18:24:13 +0530108 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
109 goto final;
110 }
111
112 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
Patrick Venture0b02be92018-08-31 11:55:55 -0700113 if (r < 0)
114 {
Tomd700e762016-09-20 18:24:13 +0530115 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
116 goto final;
117 }
118
Lei YU91875f72018-04-03 15:14:49 +0800119 if (strlen(str2) == 0)
120 {
121 // Path being empty occurs when the sensor id is not in SystemManager
122 r = -EINVAL;
123 goto final;
124 }
125
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700126 r = get_bus_for_path(str2, &str1);
Patrick Venture0b02be92018-08-31 11:55:55 -0700127 if (r < 0)
128 {
129 fprintf(stderr, "Failed to get %s busname: %s\n", str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530130 goto final;
131 }
132
133 strncpy(interface->bus, str1, MAX_DBUS_PATH);
134 strncpy(interface->path, str2, MAX_DBUS_PATH);
135 strncpy(interface->interface, str3, MAX_DBUS_PATH);
136
137 interface->sensornumber = num;
Emily Shaffer71174412017-04-05 15:10:40 -0700138 // Make sure we know that the type hasn't been set, as newer codebase will
139 // set it automatically from the YAML at this step.
140 interface->sensortype = 0;
Tomd700e762016-09-20 18:24:13 +0530141
142final:
143
144 sd_bus_error_free(&error);
145 reply = sd_bus_message_unref(reply);
146 free(busname);
147 free(str1);
148
149 return r;
150}
151
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700152// Use a lookup table to find the interface name of a specific sensor
153// This will be used until an alternative is found. this is the first
154// step for mapping IPMI
Patrick Venture0b02be92018-08-31 11:55:55 -0700155int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
156{
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700157 int rc;
158
159 // When the sensor map does not contain the sensor requested,
160 // fall back to the legacy DBus lookup (deprecated)
161 const auto& sensor_it = sensors.find(num);
162 if (sensor_it == sensors.end())
163 {
164 return legacy_dbus_openbmc_path("SENSOR", num, interface);
165 }
166
167 const auto& info = sensor_it->second;
168
Patrick Williams8451edf2017-06-13 09:01:06 -0500169 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700170 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700171 if (rc < 0)
172 {
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700173 fprintf(stderr, "Failed to get %s busname: %s\n",
Patrick Venture0b02be92018-08-31 11:55:55 -0700174 info.sensorPath.c_str(), busname);
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700175 goto final;
176 }
177
178 interface->sensortype = info.sensorType;
179 strcpy(interface->bus, busname);
180 strcpy(interface->path, info.sensorPath.c_str());
181 // Take the interface name from the beginning of the DbusInterfaceMap. This
182 // works for the Value interface but may not suffice for more complex
183 // sensors.
184 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Patrick Venture0b02be92018-08-31 11:55:55 -0700185 strcpy(interface->interface,
186 info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700187 interface->sensornumber = num;
188
189final:
190 free(busname);
191 return rc;
192}
193
Tomd700e762016-09-20 18:24:13 +0530194/////////////////////////////////////////////////////////////////////
195//
196// Routines used by ipmi commands wanting to interact on the dbus
197//
198/////////////////////////////////////////////////////////////////////
Patrick Venture0b02be92018-08-31 11:55:55 -0700199int set_sensor_dbus_state_s(uint8_t number, const char* method,
200 const char* value)
201{
Tomd700e762016-09-20 18:24:13 +0530202
203 dbus_interface_t a;
204 int r;
205 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700206 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530207
Patrick Venture0b02be92018-08-31 11:55:55 -0700208 fprintf(ipmidbus,
209 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
210 "value of %s\n",
211 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530212
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700213 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530214
Patrick Venture0b02be92018-08-31 11:55:55 -0700215 if (r < 0)
216 {
Tomd700e762016-09-20 18:24:13 +0530217 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
218 return 0;
219 }
220
Patrick Venture0b02be92018-08-31 11:55:55 -0700221 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
222 method);
223 if (r < 0)
224 {
Tomd700e762016-09-20 18:24:13 +0530225 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
226 goto final;
227 }
228
229 r = sd_bus_message_append(m, "v", "s", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700230 if (r < 0)
231 {
Tomd700e762016-09-20 18:24:13 +0530232 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
233 goto final;
234 }
235
Tomd700e762016-09-20 18:24:13 +0530236 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700237 if (r < 0)
238 {
Tomd700e762016-09-20 18:24:13 +0530239 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
240 }
241
242final:
243 sd_bus_error_free(&error);
244 m = sd_bus_message_unref(m);
245
246 return 0;
247}
Patrick Venture0b02be92018-08-31 11:55:55 -0700248int set_sensor_dbus_state_y(uint8_t number, const char* method,
249 const uint8_t value)
250{
Tomd700e762016-09-20 18:24:13 +0530251
252 dbus_interface_t a;
253 int r;
254 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700255 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530256
Patrick Venture0b02be92018-08-31 11:55:55 -0700257 fprintf(ipmidbus,
258 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
259 "value of 0x%02x\n",
260 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530261
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700262 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530263
Patrick Venture0b02be92018-08-31 11:55:55 -0700264 if (r < 0)
265 {
Tomd700e762016-09-20 18:24:13 +0530266 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
267 return 0;
268 }
269
Patrick Venture0b02be92018-08-31 11:55:55 -0700270 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
271 method);
272 if (r < 0)
273 {
Tomd700e762016-09-20 18:24:13 +0530274 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
275 goto final;
276 }
277
278 r = sd_bus_message_append(m, "v", "i", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700279 if (r < 0)
280 {
Tomd700e762016-09-20 18:24:13 +0530281 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
282 goto final;
283 }
284
Tomd700e762016-09-20 18:24:13 +0530285 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700286 if (r < 0)
287 {
Tomd700e762016-09-20 18:24:13 +0530288 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
289 }
290
291final:
292 sd_bus_error_free(&error);
293 m = sd_bus_message_unref(m);
294
295 return 0;
296}
297
Patrick Venture0b02be92018-08-31 11:55:55 -0700298uint8_t dbus_to_sensor_type(char* p)
299{
Chris Austenac4604a2015-10-13 12:43:27 -0500300
Patrick Venture0b02be92018-08-31 11:55:55 -0700301 sensorTypemap_t* s = g_SensorTypeMap;
302 char r = 0;
303 while (s->number != 0xFF)
304 {
305 if (!strcmp(s->dbusname, p))
306 {
Tom Joseph558184e2017-09-01 13:45:05 +0530307 r = s->typecode;
Patrick Venture0b02be92018-08-31 11:55:55 -0700308 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500309 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500310 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500311 }
312
Chris Austen0012e9b2015-10-22 01:37:46 -0500313 if (s->number == 0xFF)
314 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500315
Chris Austen0012e9b2015-10-22 01:37:46 -0500316 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500317}
318
Patrick Venture0b02be92018-08-31 11:55:55 -0700319uint8_t get_type_from_interface(dbus_interface_t dbus_if)
320{
Chris Austen0012e9b2015-10-22 01:37:46 -0500321
Patrick Venture0b02be92018-08-31 11:55:55 -0700322 char* p;
Brad Bishop56003452016-10-05 21:49:19 -0400323 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500324
Chris Austen0012e9b2015-10-22 01:37:46 -0500325 // This is where sensors that do not exist in dbus but do
326 // exist in the host code stop. This should indicate it
327 // is not a supported sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700328 if (dbus_if.interface[0] == 0)
329 {
330 return 0;
331 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500332
Emily Shaffer71174412017-04-05 15:10:40 -0700333 // Fetch type from interface itself.
334 if (dbus_if.sensortype != 0)
335 {
336 type = dbus_if.sensortype;
Patrick Venture0b02be92018-08-31 11:55:55 -0700337 }
338 else
339 {
Chris Austen0012e9b2015-10-22 01:37:46 -0500340 // Non InventoryItems
Patrick Venture0b02be92018-08-31 11:55:55 -0700341 p = strrchr(dbus_if.path, '/');
342 type = dbus_to_sensor_type(p + 1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500343 }
344
Brad Bishop56003452016-10-05 21:49:19 -0400345 return type;
Patrick Venture0b02be92018-08-31 11:55:55 -0700346}
Chris Austen0012e9b2015-10-22 01:37:46 -0500347
Emily Shaffer391f3302017-04-03 10:27:08 -0700348// Replaces find_sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700349uint8_t find_type_for_sensor_number(uint8_t num)
350{
Emily Shaffer391f3302017-04-03 10:27:08 -0700351 int r;
352 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700353 r = find_openbmc_path(num, &dbus_if);
Patrick Venture0b02be92018-08-31 11:55:55 -0700354 if (r < 0)
355 {
Emily Shaffer391f3302017-04-03 10:27:08 -0700356 fprintf(stderr, "Could not find sensor %d\n", num);
Lei YU91875f72018-04-03 15:14:49 +0800357 return 0;
Emily Shaffer391f3302017-04-03 10:27:08 -0700358 }
359 return get_type_from_interface(dbus_if);
360}
361
Chris Austen0012e9b2015-10-22 01:37:46 -0500362ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700363 ipmi_request_t request,
364 ipmi_response_t response,
365 ipmi_data_len_t data_len,
366 ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500367{
Patrick Venture0b02be92018-08-31 11:55:55 -0700368 sensor_data_t* reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500369 ipmi_ret_t rc = IPMI_CC_OK;
370
Patrick Venture0b02be92018-08-31 11:55:55 -0700371 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n", reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500372
373 // TODO Not sure what the System-event-sensor is suppose to return
374 // need to ask Hostboot team
Patrick Venture0b02be92018-08-31 11:55:55 -0700375 unsigned char buf[] = {0x00, 0x6F};
Chris Austenac4604a2015-10-13 12:43:27 -0500376
Emily Shaffer391f3302017-04-03 10:27:08 -0700377 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500378
379 // HACK UNTIL Dbus gets updated or we find a better way
Patrick Venture0b02be92018-08-31 11:55:55 -0700380 if (buf[0] == 0)
381 {
Chris Austen800ba712015-12-03 15:31:00 -0600382 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500383 }
384
Chris Austenac4604a2015-10-13 12:43:27 -0500385 *data_len = sizeof(buf);
386 memcpy(response, &buf, *data_len);
387
Chris Austenac4604a2015-10-13 12:43:27 -0500388 return rc;
389}
390
Patrick Venture0b02be92018-08-31 11:55:55 -0700391const std::set<std::string> analogSensorInterfaces = {
Emily Shaffercc941e12017-06-14 13:06:26 -0700392 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700393 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700394};
395
396bool isAnalogSensor(const std::string& interface)
397{
398 return (analogSensorInterfaces.count(interface));
399}
400
Patrick Venture0b02be92018-08-31 11:55:55 -0700401ipmi_ret_t setSensorReading(void* request)
Tom Josephbe703f72017-03-09 12:34:35 +0530402{
Tom Joseph816e92b2017-09-06 19:23:00 +0530403 ipmi::sensor::SetSensorReadingReq cmdData =
Patrick Venture0b02be92018-08-31 11:55:55 -0700404 *(static_cast<ipmi::sensor::SetSensorReadingReq*>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530405
406 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500407 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530408 if (iter == sensors.end())
409 {
410 return IPMI_CC_SENSOR_INVALID;
411 }
412
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500413 try
414 {
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500415 if (ipmi::sensor::Mutability::Write !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700416 (iter->second.mutability & ipmi::sensor::Mutability::Write))
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500417 {
418 log<level::ERR>("Sensor Set operation is not allowed",
419 entry("SENSOR_NUM=%d", cmdData.number));
420 return IPMI_CC_ILLEGAL_COMMAND;
421 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500422 return iter->second.updateFunc(cmdData, iter->second);
423 }
424 catch (InternalFailure& e)
425 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700426 log<level::ERR>("Set sensor failed",
427 entry("SENSOR_NUM=%d", cmdData.number));
428 commit<InternalFailure>();
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500429 }
Tom Joseph82024322017-09-28 20:07:29 +0530430 catch (const std::runtime_error& e)
431 {
432 log<level::ERR>(e.what());
433 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500434
435 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530436}
Chris Austenac4604a2015-10-13 12:43:27 -0500437
Chris Austen0012e9b2015-10-22 01:37:46 -0500438ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700439 ipmi_request_t request, ipmi_response_t response,
440 ipmi_data_len_t data_len, ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500441{
Patrick Venture0b02be92018-08-31 11:55:55 -0700442 sensor_data_t* reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500443
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530444 log<level::DEBUG>("IPMI SET_SENSOR",
445 entry("SENSOR_NUM=0x%02x", 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
Patrick Venture0b02be92018-08-31 11:55:55 -0700454 if (ipmiRC == IPMI_CC_SENSOR_INVALID)
Tom Josephbe703f72017-03-09 12:34:35 +0530455 {
456 updateSensorRecordFromSSRAESC(reqptr);
457 ipmiRC = IPMI_CC_OK;
458 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500459
Patrick Venture0b02be92018-08-31 11:55:55 -0700460 *data_len = 0;
Tom Josephbe703f72017-03-09 12:34:35 +0530461 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500462}
463
Patrick Venture0b02be92018-08-31 11:55:55 -0700464ipmi_ret_t legacyGetSensorReading(uint8_t sensorNum, ipmi_response_t response,
Tom Joseph3ee668f2018-03-02 19:49:17 +0530465 ipmi_data_len_t data_len)
Chris Austen10ccc0f2015-12-10 18:27:04 -0600466{
Chris Austen10ccc0f2015-12-10 18:27:04 -0600467 int r;
468 dbus_interface_t a;
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 sd_bus* bus = ipmid_get_sd_bus_connection();
Tom Joseph3ee668f2018-03-02 19:49:17 +0530470 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
471 uint8_t type = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700472 sd_bus_message* reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600473 int reading = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600474 char* assertion = NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700475 sensorreadingresp_t* resp = (sensorreadingresp_t*)response;
476 *data_len = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600477
Tom Joseph3ee668f2018-03-02 19:49:17 +0530478 r = find_openbmc_path(sensorNum, &a);
Tom Joseph40c35b12017-09-07 02:04:42 +0530479 if (r < 0)
480 {
Tom Joseph3ee668f2018-03-02 19:49:17 +0530481 sd_journal_print(LOG_ERR, "Failed to find Sensor 0x%02x\n", sensorNum);
482 return IPMI_CC_SENSOR_INVALID;
Nan Li36deb762016-05-12 10:23:41 +0800483 }
Tom Joseph3ee668f2018-03-02 19:49:17 +0530484
485 type = get_type_from_interface(a);
486 if (type == 0)
Tom Joseph40c35b12017-09-07 02:04:42 +0530487 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700488 sd_journal_print(LOG_ERR, "Failed to find Sensor 0x%02x\n", sensorNum);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530489 return IPMI_CC_SENSOR_INVALID;
Brad Bishop56003452016-10-05 21:49:19 -0400490 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600491
Patrick Venture0b02be92018-08-31 11:55:55 -0700492 switch (type)
493 {
Chris Austen10ccc0f2015-12-10 18:27:04 -0600494 case 0xC2:
Patrick Venture0b02be92018-08-31 11:55:55 -0700495 r = sd_bus_get_property(bus, a.bus, a.path, a.interface, "value",
496 NULL, &reply, "i");
Tom Joseph3ee668f2018-03-02 19:49:17 +0530497 if (r < 0)
498 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700499 sd_journal_print(LOG_ERR,
500 "Failed to call sd_bus_get_property:"
501 " %d, %s\n",
502 r, strerror(-r));
Tom Joseph3ee668f2018-03-02 19:49:17 +0530503 sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n",
504 a.bus, a.path, a.interface);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600505 break;
506 }
507
Adriana Kobylak93125982016-03-01 12:48:10 -0600508 r = sd_bus_message_read(reply, "i", &reading);
Patrick Venture0b02be92018-08-31 11:55:55 -0700509 if (r < 0)
510 {
Tom Joseph3ee668f2018-03-02 19:49:17 +0530511 sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n",
512 strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600513 break;
514 }
515
Chris Austen10ccc0f2015-12-10 18:27:04 -0600516 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700517 *data_len = sizeof(sensorreadingresp_t);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600518
Patrick Venture0b02be92018-08-31 11:55:55 -0700519 resp->value = (uint8_t)reading;
520 resp->operation = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600521 resp->indication[0] = 0;
522 resp->indication[1] = 0;
523 break;
524
Nagaraju Gorugantie98d1462018-05-08 06:42:57 -0500525 case 0xC8:
Patrick Venture0b02be92018-08-31 11:55:55 -0700526 r = sd_bus_get_property(bus, a.bus, a.path, a.interface, "value",
527 NULL, &reply, "i");
Nagaraju Gorugantie98d1462018-05-08 06:42:57 -0500528 if (r < 0)
529 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700530 sd_journal_print(LOG_ERR,
531 "Failed to call sd_bus_get_property:"
532 " %d, %s\n",
533 r, strerror(-r));
Nagaraju Gorugantie98d1462018-05-08 06:42:57 -0500534 sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n",
535 a.bus, a.path, a.interface);
536 break;
537 }
538
539 r = sd_bus_message_read(reply, "i", &reading);
Patrick Venture0b02be92018-08-31 11:55:55 -0700540 if (r < 0)
541 {
Nagaraju Gorugantie98d1462018-05-08 06:42:57 -0500542 sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n",
543 strerror(-r));
544 break;
545 }
546
547 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700548 *data_len = sizeof(sensorreadingresp_t);
Nagaraju Gorugantie98d1462018-05-08 06:42:57 -0500549
Patrick Venture0b02be92018-08-31 11:55:55 -0700550 resp->value = 0;
551 resp->operation = 0;
Nagaraju Gorugantie98d1462018-05-08 06:42:57 -0500552 resp->indication[0] = (uint8_t)reading;
553 resp->indication[1] = 0;
554 break;
555
Patrick Venture0b02be92018-08-31 11:55:55 -0700556 // TODO openbmc/openbmc#2154 Move this sensor to right place.
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600557 case 0xCA:
Patrick Venture0b02be92018-08-31 11:55:55 -0700558 r = sd_bus_get_property(bus, a.bus, a.path, a.interface, "value",
Tom Joseph3ee668f2018-03-02 19:49:17 +0530559 NULL, &reply, "s");
560 if (r < 0)
561 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700562 sd_journal_print(LOG_ERR,
563 "Failed to call sd_bus_get_property:"
564 " %d, %s\n",
565 r, strerror(-r));
Tom Joseph3ee668f2018-03-02 19:49:17 +0530566 sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n",
567 a.bus, a.path, a.interface);
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600568 break;
569 }
570
571 r = sd_bus_message_read(reply, "s", &assertion);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530572 if (r < 0)
573 {
574 sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n",
575 strerror(-r));
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600576 break;
577 }
578
579 rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700580 *data_len = sizeof(sensorreadingresp_t);
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600581
Patrick Venture0b02be92018-08-31 11:55:55 -0700582 resp->value = 0;
583 resp->operation = 0;
584 if (strcmp(assertion, "Enabled") == 0)
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600585 {
586 resp->indication[0] = 0x02;
587 }
588 else
589 {
590 resp->indication[0] = 0x1;
591 }
592 resp->indication[1] = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600593 break;
594
Chris Austen10ccc0f2015-12-10 18:27:04 -0600595 default:
Tom Joseph14c15462017-09-07 02:16:51 +0530596 {
Tom Joseph3ee668f2018-03-02 19:49:17 +0530597 return IPMI_CC_SENSOR_INVALID;
Tom Joseph14c15462017-09-07 02:16:51 +0530598 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600599 }
600
vishwa1eaea4f2016-02-26 11:57:40 -0600601 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600602
603 return rc;
604}
605
Tom Joseph3ee668f2018-03-02 19:49:17 +0530606ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700607 ipmi_request_t request,
608 ipmi_response_t response,
609 ipmi_data_len_t data_len,
610 ipmi_context_t context)
Tom Joseph3ee668f2018-03-02 19:49:17 +0530611{
Patrick Venture0b02be92018-08-31 11:55:55 -0700612 sensor_data_t* reqptr = (sensor_data_t*)request;
613 sensorreadingresp_t* resp = (sensorreadingresp_t*)response;
614 ipmi::sensor::GetSensorResponse getResponse{};
Tom Joseph3ee668f2018-03-02 19:49:17 +0530615 static constexpr auto scanningEnabledBit = 6;
616
617 const auto iter = sensors.find(reqptr->sennum);
618 if (iter == sensors.end())
619 {
620 return legacyGetSensorReading(reqptr->sennum, response, data_len);
621 }
622 if (ipmi::sensor::Mutability::Read !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700623 (iter->second.mutability & ipmi::sensor::Mutability::Read))
Tom Joseph3ee668f2018-03-02 19:49:17 +0530624 {
Jayanth Othayoth6ccf8812018-04-05 22:58:48 -0500625 return IPMI_CC_ILLEGAL_COMMAND;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530626 }
627
628 try
629 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700630 getResponse = iter->second.getFunc(iter->second);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530631 *data_len = getResponse.size();
632 memcpy(resp, getResponse.data(), *data_len);
633 resp->operation = 1 << scanningEnabledBit;
634 return IPMI_CC_OK;
635 }
636 catch (const std::exception& e)
637 {
638 *data_len = getResponse.size();
639 memcpy(resp, getResponse.data(), *data_len);
640 return IPMI_CC_OK;
641 }
642}
643
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530644void getSensorThresholds(uint8_t sensorNum,
645 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600646{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530647 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600648 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530649 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600650 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600651
652 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
653
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530654 const auto iter = sensors.find(sensorNum);
655 const auto info = iter->second;
656
657 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
658
Patrick Venture0b02be92018-08-31 11:55:55 -0700659 auto warnThresholds = ipmi::getAllDbusProperties(
660 bus, service, info.sensorPath, warningThreshIntf);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530661
James Feist1e121122018-07-31 11:44:09 -0700662 double warnLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(),
663 warnThresholds["WarningLow"]);
664 double warnHigh = mapbox::util::apply_visitor(
665 ipmi::VariantToDoubleVisitor(), warnThresholds["WarningHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530666
667 if (warnLow != 0)
668 {
669 warnLow *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700670 response->lowerNonCritical = static_cast<uint8_t>(
671 (warnLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530672 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700673 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530674 }
675
676 if (warnHigh != 0)
677 {
678 warnHigh *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700679 response->upperNonCritical = static_cast<uint8_t>(
680 (warnHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530681 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700682 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530683 }
684
Patrick Venture0b02be92018-08-31 11:55:55 -0700685 auto critThresholds = ipmi::getAllDbusProperties(
686 bus, service, info.sensorPath, criticalThreshIntf);
James Feist1e121122018-07-31 11:44:09 -0700687 double critLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(),
688 critThresholds["CriticalLow"]);
689 double critHigh = mapbox::util::apply_visitor(
690 ipmi::VariantToDoubleVisitor(), critThresholds["CriticalHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530691
692 if (critLow != 0)
693 {
694 critLow *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700695 response->lowerCritical = static_cast<uint8_t>(
696 (critLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530697 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700698 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530699 }
700
701 if (critHigh != 0)
702 {
703 critHigh *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700704 response->upperCritical = static_cast<uint8_t>(
705 (critHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530706 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700707 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530708 }
709}
710
711ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700712 ipmi_request_t request,
713 ipmi_response_t response,
714 ipmi_data_len_t data_len,
715 ipmi_context_t context)
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530716{
717 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
718
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600719 if (*data_len != sizeof(uint8_t))
720 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530721 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600722 return IPMI_CC_REQ_DATA_LEN_INVALID;
723 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600724
Patrick Venture0b02be92018-08-31 11:55:55 -0700725 auto sensorNum = *(reinterpret_cast<const uint8_t*>(request));
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530726 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600727
728 const auto iter = sensors.find(sensorNum);
729 if (iter == sensors.end())
730 {
731 return IPMI_CC_SENSOR_INVALID;
732 }
733
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530734 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600735
Patrick Venture0b02be92018-08-31 11:55:55 -0700736 // Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530737 if (info.propertyInterfaces.find(valueInterface) ==
738 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600739 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700740 // return with valid mask as 0
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600741 return IPMI_CC_OK;
742 }
743
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530744 auto responseData =
745 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600746
747 try
748 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530749 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600750 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530751 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600752 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700753 // Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600754 responseData->validMask = 0;
755 }
756
757 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
758 return IPMI_CC_OK;
759}
760
Chris Austen0012e9b2015-10-22 01:37:46 -0500761ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
762 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500763 ipmi_data_len_t data_len, ipmi_context_t context)
764{
Nan Li70aa8d92016-08-29 00:11:10 +0800765 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500766
Patrick Venture0b02be92018-08-31 11:55:55 -0700767 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n", netfn, cmd);
Chris Austenac4604a2015-10-13 12:43:27 -0500768 *data_len = 0;
769
770 return rc;
771}
772
Emily Shafferd06e0e72017-04-05 09:08:57 -0700773ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
774 ipmi_request_t request,
775 ipmi_response_t response,
776 ipmi_data_len_t data_len,
777 ipmi_context_t context)
778{
779 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
780 if (request == nullptr ||
781 get_sdr_info::request::get_count(request) == false)
782 {
783 // Get Sensor Count
Ratan Guptae0cc8552018-01-22 14:23:04 +0530784 resp->count = sensors.size() + frus.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700785 }
786 else
787 {
788 resp->count = 1;
789 }
790
791 // Multiple LUNs not supported.
792 namespace response = get_sdr_info::response;
793 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
794 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
795 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
796 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
797 response::set_static_population(&(resp->luns_and_dynamic_population));
798
799 *data_len = SDR_INFO_RESP_SIZE;
800
801 return IPMI_CC_OK;
802}
803
Emily Shaffera344afc2017-04-13 15:09:39 -0700804ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
805 ipmi_request_t request,
806 ipmi_response_t response,
807 ipmi_data_len_t data_len,
808 ipmi_context_t context)
809{
810 // A constant reservation ID is okay until we implement add/remove SDR.
811 const uint16_t reservation_id = 1;
812 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700813 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700814
815 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
816 return IPMI_CC_OK;
817}
Chris Austenac4604a2015-10-13 12:43:27 -0500818
Patrick Venture0b02be92018-08-31 11:55:55 -0700819void setUnitFieldsForObject(const ipmi::sensor::Info* info,
820 get_sdr::SensorDataFullRecordBody* body)
Emily Shaffercc941e12017-06-14 13:06:26 -0700821{
Tom Josephdc212b22018-02-16 09:59:57 +0530822 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
823 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700824 {
Tom Josephdc212b22018-02-16 09:59:57 +0530825 auto unit = server::Value::convertUnitFromString(info->unit);
826 // Unit strings defined in
827 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
828 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700829 {
Tom Josephdc212b22018-02-16 09:59:57 +0530830 case server::Value::Unit::DegreesC:
831 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
832 break;
833 case server::Value::Unit::RPMS:
Patrick Venture0b02be92018-08-31 11:55:55 -0700834 body->sensor_units_2_base =
835 get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
Tom Josephdc212b22018-02-16 09:59:57 +0530836 get_sdr::body::set_rate_unit(0b100, body); // per minute
837 break;
838 case server::Value::Unit::Volts:
839 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
840 break;
841 case server::Value::Unit::Meters:
842 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
843 break;
844 case server::Value::Unit::Amperes:
845 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
846 break;
847 case server::Value::Unit::Joules:
848 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
849 break;
850 case server::Value::Unit::Watts:
851 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
852 break;
853 default:
854 // Cannot be hit.
855 fprintf(stderr, "Unknown value unit type: = %s\n",
856 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700857 }
858 }
Tom Josephdc212b22018-02-16 09:59:57 +0530859 catch (sdbusplus::exception::InvalidEnumString e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700860 {
Tom Josephdc212b22018-02-16 09:59:57 +0530861 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700862 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700863}
864
Patrick Venture0b02be92018-08-31 11:55:55 -0700865ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body,
866 const ipmi::sensor::Info* info,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700867 ipmi_data_len_t data_len)
868{
869 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700870 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700871 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700872
873 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
874
875 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530876 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700877
878 get_sdr::body::set_b(info->coefficientB, body);
879 get_sdr::body::set_m(info->coefficientM, body);
880 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530881 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700882
Emily Shafferbbef71c2017-05-08 16:36:17 -0700883 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700884 }
885
Tom Joseph96423912018-01-25 00:14:34 +0530886 /* ID string */
887 auto id_string = info->sensorNameFunc(*info);
888
889 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
890 {
891 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
892 }
893 else
894 {
895 get_sdr::body::set_id_strlen(id_string.length(), body);
896 }
897 strncpy(body->id_string, id_string.c_str(),
898 get_sdr::body::get_id_strlen(body));
899
Emily Shafferbbef71c2017-05-08 16:36:17 -0700900 return IPMI_CC_OK;
901};
902
Ratan Guptae0cc8552018-01-22 14:23:04 +0530903ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
904 ipmi_data_len_t data_len)
905{
906 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
907 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700908 get_sdr::SensorDataFruRecord record{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530909 auto dataLength = 0;
910
911 auto fru = frus.begin();
Patrick Venture0b02be92018-08-31 11:55:55 -0700912 uint8_t fruID{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530913 auto recordID = get_sdr::request::get_record_id(req);
914
915 fruID = recordID - FRU_RECORD_ID_START;
916 fru = frus.find(fruID);
917 if (fru == frus.end())
918 {
919 return IPMI_CC_SENSOR_INVALID;
920 }
921
922 /* Header */
923 get_sdr::header::set_record_id(recordID, &(record.header));
924 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
925 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
926 record.header.record_length = sizeof(record.key) + sizeof(record.body);
927
928 /* Key */
929 record.key.fruID = fruID;
930 record.key.accessLun |= IPMI_LOGICAL_FRU;
931 record.key.deviceAddress = BMCSlaveAddress;
932
933 /* Body */
934 record.body.entityID = fru->second[0].entityID;
935 record.body.entityInstance = fru->second[0].entityInstance;
936 record.body.deviceType = fruInventoryDevice;
937 record.body.deviceTypeModifier = IPMIFruInventory;
938
939 /* Device ID string */
Patrick Venture0b02be92018-08-31 11:55:55 -0700940 auto deviceID =
941 fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1,
942 fru->second[0].path.length());
Ratan Guptae0cc8552018-01-22 14:23:04 +0530943
944 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
945 {
946 get_sdr::body::set_device_id_strlen(
Patrick Venture0b02be92018-08-31 11:55:55 -0700947 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530948 }
949 else
950 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700951 get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530952 }
953
954 strncpy(record.body.deviceID, deviceID.c_str(),
955 get_sdr::body::get_device_id_strlen(&(record.body)));
956
957 if (++fru == frus.end())
958 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700959 get_sdr::response::set_next_record_id(END_OF_RECORD,
960 resp); // last record
Ratan Guptae0cc8552018-01-22 14:23:04 +0530961 }
962 else
963 {
964 get_sdr::response::set_next_record_id(
Patrick Venture0b02be92018-08-31 11:55:55 -0700965 (FRU_RECORD_ID_START + fru->first), resp);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530966 }
967
968 if (req->bytes_to_read > (sizeof(*resp) - req->offset))
969 {
970 dataLength = (sizeof(*resp) - req->offset);
971 }
972 else
973 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700974 dataLength = req->bytes_to_read;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530975 }
976
977 if (dataLength <= 0)
978 {
979 return IPMI_CC_REQ_DATA_LEN_INVALID;
980 }
981
Patrick Venture0b02be92018-08-31 11:55:55 -0700982 memcpy(resp->record_data, reinterpret_cast<uint8_t*>(&record) + req->offset,
983 (dataLength));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530984
985 *data_len = dataLength;
986 *data_len += 2; // additional 2 bytes for next record ID
987
988 return IPMI_CC_OK;
989}
990
Emily Shafferbbef71c2017-05-08 16:36:17 -0700991ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
992 ipmi_request_t request, ipmi_response_t response,
993 ipmi_data_len_t data_len, ipmi_context_t context)
994{
995 ipmi_ret_t ret = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700996 get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request;
997 get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700998 get_sdr::SensorDataFullRecord record = {0};
999 if (req != NULL)
1000 {
1001 // Note: we use an iterator so we can provide the next ID at the end of
1002 // the call.
1003 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +05301004 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001005
1006 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +05301007 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -07001008 {
Ratan Guptae0cc8552018-01-22 14:23:04 +05301009 // recordID greater then 255,it means it is a FRU record.
1010 // Currently we are supporting two record types either FULL record
1011 // or FRU record.
1012 if (recordID >= FRU_RECORD_ID_START)
1013 {
1014 return ipmi_fru_get_sdr(request, response, data_len);
1015 }
1016 else
1017 {
1018 sensor = sensors.find(recordID);
1019 if (sensor == sensors.end())
1020 {
1021 return IPMI_CC_SENSOR_INVALID;
1022 }
Emily Shafferbbef71c2017-05-08 16:36:17 -07001023 }
1024 }
1025
1026 uint8_t sensor_id = sensor->first;
1027
1028 /* Header */
1029 get_sdr::header::set_record_id(sensor_id, &(record.header));
1030 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
1031 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1032 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
1033
1034 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +05301035 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -07001036 record.key.sensor_number = sensor_id;
1037
1038 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +05301039 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001040 record.body.sensor_type = sensor->second.sensorType;
1041 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +05301042 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001043
1044 // Set the type-specific details given the DBus interface
1045 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
1046 data_len);
1047
1048 if (++sensor == sensors.end())
1049 {
Ratan Guptae0cc8552018-01-22 14:23:04 +05301050 // we have reached till end of sensor, so assign the next record id
1051 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
Patrick Venture0b02be92018-08-31 11:55:55 -07001052 auto next_record_id =
1053 (frus.size()) ? frus.begin()->first + FRU_RECORD_ID_START
1054 : END_OF_RECORD;
Ratan Guptae0cc8552018-01-22 14:23:04 +05301055
1056 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001057 }
1058 else
1059 {
1060 get_sdr::response::set_next_record_id(sensor->first, resp);
1061 }
1062
1063 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
1064 memcpy(resp->record_data, (char*)&record + req->offset,
1065 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
1066 }
1067
1068 return ret;
1069}
1070
Chris Austenac4604a2015-10-13 12:43:27 -05001071void register_netfn_sen_functions()
1072{
Tom05732372016-09-06 17:21:23 +05301073 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001074 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr,
1075 ipmi_sen_wildcard, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001076
Tom05732372016-09-06 17:21:23 +05301077 // <Get Sensor Type>
Patrick Venture0b02be92018-08-31 11:55:55 -07001078 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, nullptr,
1079 ipmi_sen_get_sensor_type, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001080
Tom05732372016-09-06 17:21:23 +05301081 // <Set Sensor Reading and Event Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001082 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, nullptr,
1083 ipmi_sen_set_sensor, PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001084
Tom05732372016-09-06 17:21:23 +05301085 // <Get Sensor Reading>
Patrick Venture0b02be92018-08-31 11:55:55 -07001086 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, nullptr,
1087 ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
Emily Shaffera344afc2017-04-13 15:09:39 -07001088
Tom Joseph5ca50952018-02-22 00:33:38 +05301089 // <Reserve Device SDR Repository>
1090 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_DEVICE_SDR_REPO,
Patrick Venture0b02be92018-08-31 11:55:55 -07001091 nullptr, ipmi_sen_reserve_sdr, PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001092
Tom Joseph5ca50952018-02-22 00:33:38 +05301093 // <Get Device SDR Info>
Patrick Venture0b02be92018-08-31 11:55:55 -07001094 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR_INFO, nullptr,
1095 ipmi_sen_get_sdr_info, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001096
Tom Joseph5ca50952018-02-22 00:33:38 +05301097 // <Get Device SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -07001098 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr,
1099 ipmi_sen_get_sdr, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001100
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001101 // <Get Sensor Thresholds>
1102 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1103 nullptr, ipmi_sen_get_sensor_thresholds,
1104 PRIVILEGE_USER);
1105
Chris Austenac4604a2015-10-13 12:43:27 -05001106 return;
1107}