blob: 9d0364d659920da909fbf92734cb2805448d1a1e [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "sensorhandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07002
3#include "fruread.hpp"
4#include "ipmid.hpp"
5#include "types.hpp"
6#include "utils.hpp"
7
William A. Kennington III194375f2018-12-14 02:14:33 -08008#include <ipmid/api.h>
Tomd700e762016-09-20 18:24:13 +05309#include <mapper.h>
Chris Austen10ccc0f2015-12-10 18:27:04 -060010#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070011
12#include <bitset>
Patrick Venture586d35b2018-09-07 19:56:18 -070013#include <cmath>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070014#include <cstring>
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>
William A. Kennington III4c008022018-10-12 17:18:14 -070017#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070018#include <set>
19#include <xyz/openbmc_project/Common/error.hpp>
20#include <xyz/openbmc_project/Sensor/Value/server.hpp>
21
Ratan Guptae0cc8552018-01-22 14:23:04 +053022static constexpr uint8_t fruInventoryDevice = 0x10;
23static constexpr uint8_t IPMIFruInventory = 0x02;
24static constexpr uint8_t BMCSlaveAddress = 0x20;
25
Patrick Venture0b02be92018-08-31 11:55:55 -070026extern int updateSensorRecordFromSSRAESC(const void*);
27extern sd_bus* bus;
Tom Josephbe703f72017-03-09 12:34:35 +053028extern const ipmi::sensor::IdInfoMap sensors;
Ratan Guptae0cc8552018-01-22 14:23:04 +053029extern const FruMap frus;
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -080030extern const ipmi::sensor::EntityInfoMap entities;
Ratan Guptae0cc8552018-01-22 14:23:04 +053031
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
James Feist37544422018-10-12 14:57:06 -070036namespace variant_ns = sdbusplus::message::variant_ns;
37
Patrick Venture0b02be92018-08-31 11:55:55 -070038void register_netfn_sen_functions() __attribute__((constructor));
Chris Austenac4604a2015-10-13 12:43:27 -050039
Patrick Venture0b02be92018-08-31 11:55:55 -070040struct sensorTypemap_t
41{
Chris Austen0012e9b2015-10-22 01:37:46 -050042 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060043 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050044 char dbusname[32];
Patrick Venture0b02be92018-08-31 11:55:55 -070045};
Chris Austen0012e9b2015-10-22 01:37:46 -050046
Chris Austen0012e9b2015-10-22 01:37:46 -050047sensorTypemap_t g_SensorTypeMap[] = {
48
Chris Austend7cf0e42015-11-07 14:27:12 -060049 {0x01, 0x6F, "Temp"},
50 {0x0C, 0x6F, "DIMM"},
51 {0x0C, 0x6F, "MEMORY_BUFFER"},
52 {0x07, 0x6F, "PROC"},
53 {0x07, 0x6F, "CORE"},
54 {0x07, 0x6F, "CPU"},
55 {0x0F, 0x6F, "BootProgress"},
Patrick Venture0b02be92018-08-31 11:55:55 -070056 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor
57 // type code os 0x09
Chris Austend7cf0e42015-11-07 14:27:12 -060058 {0xC3, 0x6F, "BootCount"},
59 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060060 {0x12, 0x6F, "SYSTEM_EVENT"},
61 {0xC7, 0x03, "SYSTEM"},
62 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060063 {0xC2, 0x6F, "PowerCap"},
Tom Joseph558184e2017-09-01 13:45:05 +053064 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050065 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053066 {0xD8, 0xC8, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060067 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050068};
69
Patrick Venture0b02be92018-08-31 11:55:55 -070070struct sensor_data_t
71{
Chris Austenac4604a2015-10-13 12:43:27 -050072 uint8_t sennum;
Patrick Venture0b02be92018-08-31 11:55:55 -070073} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050074
Patrick Venture0b02be92018-08-31 11:55:55 -070075struct sensorreadingresp_t
76{
Chris Austen10ccc0f2015-12-10 18:27:04 -060077 uint8_t value;
78 uint8_t operation;
79 uint8_t indication[2];
Patrick Venture0b02be92018-08-31 11:55:55 -070080} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050081
Patrick Venture0b02be92018-08-31 11:55:55 -070082int get_bus_for_path(const char* path, char** busname)
83{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070084 return mapper_get_service(bus, path, busname);
85}
Tomd700e762016-09-20 18:24:13 +053086
Emily Shaffer2ae09b92017-04-05 15:09:41 -070087// Use a lookup table to find the interface name of a specific sensor
88// This will be used until an alternative is found. this is the first
89// step for mapping IPMI
Patrick Venture0b02be92018-08-31 11:55:55 -070090int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
91{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070092 int rc;
93
Emily Shaffer2ae09b92017-04-05 15:09:41 -070094 const auto& sensor_it = sensors.find(num);
95 if (sensor_it == sensors.end())
96 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -050097 // The sensor map does not contain the sensor requested
98 return -EINVAL;
Emily Shaffer2ae09b92017-04-05 15:09:41 -070099 }
100
101 const auto& info = sensor_it->second;
102
Patrick Williams8451edf2017-06-13 09:01:06 -0500103 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700104 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700105 if (rc < 0)
106 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700107 std::fprintf(stderr, "Failed to get %s busname: %s\n",
108 info.sensorPath.c_str(), busname);
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700109 goto final;
110 }
111
112 interface->sensortype = info.sensorType;
113 strcpy(interface->bus, busname);
114 strcpy(interface->path, info.sensorPath.c_str());
115 // Take the interface name from the beginning of the DbusInterfaceMap. This
116 // works for the Value interface but may not suffice for more complex
117 // sensors.
118 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Patrick Venture0b02be92018-08-31 11:55:55 -0700119 strcpy(interface->interface,
120 info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700121 interface->sensornumber = num;
122
123final:
124 free(busname);
125 return rc;
126}
127
Tomd700e762016-09-20 18:24:13 +0530128/////////////////////////////////////////////////////////////////////
129//
130// Routines used by ipmi commands wanting to interact on the dbus
131//
132/////////////////////////////////////////////////////////////////////
Patrick Venture0b02be92018-08-31 11:55:55 -0700133int set_sensor_dbus_state_s(uint8_t number, const char* method,
134 const char* value)
135{
Tomd700e762016-09-20 18:24:13 +0530136
137 dbus_interface_t a;
138 int r;
139 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700140 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530141
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700142 std::fprintf(ipmidbus,
143 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
144 "value of %s\n",
145 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530146
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700147 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530148
Patrick Venture0b02be92018-08-31 11:55:55 -0700149 if (r < 0)
150 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700151 std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
Tomd700e762016-09-20 18:24:13 +0530152 return 0;
153 }
154
Patrick Venture0b02be92018-08-31 11:55:55 -0700155 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
156 method);
157 if (r < 0)
158 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700159 std::fprintf(stderr, "Failed to create a method call: %s",
160 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530161 goto final;
162 }
163
164 r = sd_bus_message_append(m, "v", "s", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700165 if (r < 0)
166 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700167 std::fprintf(stderr, "Failed to create a input parameter: %s",
168 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530169 goto final;
170 }
171
Tomd700e762016-09-20 18:24:13 +0530172 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700173 if (r < 0)
174 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700175 std::fprintf(stderr, "Failed to call the method: %s", strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530176 }
177
178final:
179 sd_bus_error_free(&error);
180 m = sd_bus_message_unref(m);
181
182 return 0;
183}
Patrick Venture0b02be92018-08-31 11:55:55 -0700184int set_sensor_dbus_state_y(uint8_t number, const char* method,
185 const uint8_t value)
186{
Tomd700e762016-09-20 18:24:13 +0530187
188 dbus_interface_t a;
189 int r;
190 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700191 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530192
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700193 std::fprintf(ipmidbus,
194 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
195 "value of 0x%02x\n",
196 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530197
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700198 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530199
Patrick Venture0b02be92018-08-31 11:55:55 -0700200 if (r < 0)
201 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700202 std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
Tomd700e762016-09-20 18:24:13 +0530203 return 0;
204 }
205
Patrick Venture0b02be92018-08-31 11:55:55 -0700206 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
207 method);
208 if (r < 0)
209 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700210 std::fprintf(stderr, "Failed to create a method call: %s",
211 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530212 goto final;
213 }
214
215 r = sd_bus_message_append(m, "v", "i", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700216 if (r < 0)
217 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700218 std::fprintf(stderr, "Failed to create a input parameter: %s",
219 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530220 goto final;
221 }
222
Tomd700e762016-09-20 18:24:13 +0530223 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700224 if (r < 0)
225 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700226 std::fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530227 }
228
229final:
230 sd_bus_error_free(&error);
231 m = sd_bus_message_unref(m);
232
233 return 0;
234}
235
Patrick Venture0b02be92018-08-31 11:55:55 -0700236uint8_t dbus_to_sensor_type(char* p)
237{
Chris Austenac4604a2015-10-13 12:43:27 -0500238
Patrick Venture0b02be92018-08-31 11:55:55 -0700239 sensorTypemap_t* s = g_SensorTypeMap;
240 char r = 0;
241 while (s->number != 0xFF)
242 {
243 if (!strcmp(s->dbusname, p))
244 {
Tom Joseph558184e2017-09-01 13:45:05 +0530245 r = s->typecode;
Patrick Venture0b02be92018-08-31 11:55:55 -0700246 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500247 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500248 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500249 }
250
Chris Austen0012e9b2015-10-22 01:37:46 -0500251 if (s->number == 0xFF)
252 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500253
Chris Austen0012e9b2015-10-22 01:37:46 -0500254 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500255}
256
Patrick Venture0b02be92018-08-31 11:55:55 -0700257uint8_t get_type_from_interface(dbus_interface_t dbus_if)
258{
Chris Austen0012e9b2015-10-22 01:37:46 -0500259
Brad Bishop56003452016-10-05 21:49:19 -0400260 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500261
Chris Austen0012e9b2015-10-22 01:37:46 -0500262 // This is where sensors that do not exist in dbus but do
263 // exist in the host code stop. This should indicate it
264 // is not a supported sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700265 if (dbus_if.interface[0] == 0)
266 {
267 return 0;
268 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500269
Emily Shaffer71174412017-04-05 15:10:40 -0700270 // Fetch type from interface itself.
271 if (dbus_if.sensortype != 0)
272 {
273 type = dbus_if.sensortype;
Patrick Venture0b02be92018-08-31 11:55:55 -0700274 }
275 else
276 {
Chris Austen0012e9b2015-10-22 01:37:46 -0500277 // Non InventoryItems
Patrick Venture4491a462018-10-13 13:00:42 -0700278 char* p = strrchr(dbus_if.path, '/');
Patrick Venture0b02be92018-08-31 11:55:55 -0700279 type = dbus_to_sensor_type(p + 1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500280 }
281
Brad Bishop56003452016-10-05 21:49:19 -0400282 return type;
Patrick Venture0b02be92018-08-31 11:55:55 -0700283}
Chris Austen0012e9b2015-10-22 01:37:46 -0500284
Emily Shaffer391f3302017-04-03 10:27:08 -0700285// Replaces find_sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700286uint8_t find_type_for_sensor_number(uint8_t num)
287{
Emily Shaffer391f3302017-04-03 10:27:08 -0700288 int r;
289 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700290 r = find_openbmc_path(num, &dbus_if);
Patrick Venture0b02be92018-08-31 11:55:55 -0700291 if (r < 0)
292 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700293 std::fprintf(stderr, "Could not find sensor %d\n", num);
Lei YU91875f72018-04-03 15:14:49 +0800294 return 0;
Emily Shaffer391f3302017-04-03 10:27:08 -0700295 }
296 return get_type_from_interface(dbus_if);
297}
298
Chris Austen0012e9b2015-10-22 01:37:46 -0500299ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700300 ipmi_request_t request,
301 ipmi_response_t response,
302 ipmi_data_len_t data_len,
303 ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500304{
Patrick Ventured99148b2018-10-13 10:06:13 -0700305 auto reqptr = static_cast<sensor_data_t*>(request);
Chris Austenac4604a2015-10-13 12:43:27 -0500306 ipmi_ret_t rc = IPMI_CC_OK;
307
Patrick Venture0b02be92018-08-31 11:55:55 -0700308 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n", reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500309
310 // TODO Not sure what the System-event-sensor is suppose to return
311 // need to ask Hostboot team
Patrick Venture0b02be92018-08-31 11:55:55 -0700312 unsigned char buf[] = {0x00, 0x6F};
Chris Austenac4604a2015-10-13 12:43:27 -0500313
Emily Shaffer391f3302017-04-03 10:27:08 -0700314 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500315
316 // HACK UNTIL Dbus gets updated or we find a better way
Patrick Venture0b02be92018-08-31 11:55:55 -0700317 if (buf[0] == 0)
318 {
Chris Austen800ba712015-12-03 15:31:00 -0600319 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500320 }
321
Chris Austenac4604a2015-10-13 12:43:27 -0500322 *data_len = sizeof(buf);
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700323 std::memcpy(response, &buf, *data_len);
Chris Austenac4604a2015-10-13 12:43:27 -0500324
Chris Austenac4604a2015-10-13 12:43:27 -0500325 return rc;
326}
327
Patrick Venture0b02be92018-08-31 11:55:55 -0700328const std::set<std::string> analogSensorInterfaces = {
Emily Shaffercc941e12017-06-14 13:06:26 -0700329 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700330 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700331};
332
333bool isAnalogSensor(const std::string& interface)
334{
335 return (analogSensorInterfaces.count(interface));
336}
337
Patrick Venture0b02be92018-08-31 11:55:55 -0700338ipmi_ret_t setSensorReading(void* request)
Tom Josephbe703f72017-03-09 12:34:35 +0530339{
Tom Joseph816e92b2017-09-06 19:23:00 +0530340 ipmi::sensor::SetSensorReadingReq cmdData =
Patrick Venture0b02be92018-08-31 11:55:55 -0700341 *(static_cast<ipmi::sensor::SetSensorReadingReq*>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530342
343 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500344 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530345 if (iter == sensors.end())
346 {
347 return IPMI_CC_SENSOR_INVALID;
348 }
349
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500350 try
351 {
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500352 if (ipmi::sensor::Mutability::Write !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700353 (iter->second.mutability & ipmi::sensor::Mutability::Write))
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500354 {
355 log<level::ERR>("Sensor Set operation is not allowed",
356 entry("SENSOR_NUM=%d", cmdData.number));
357 return IPMI_CC_ILLEGAL_COMMAND;
358 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500359 return iter->second.updateFunc(cmdData, iter->second);
360 }
361 catch (InternalFailure& e)
362 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700363 log<level::ERR>("Set sensor failed",
364 entry("SENSOR_NUM=%d", cmdData.number));
365 commit<InternalFailure>();
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500366 }
Tom Joseph82024322017-09-28 20:07:29 +0530367 catch (const std::runtime_error& e)
368 {
369 log<level::ERR>(e.what());
370 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500371
372 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530373}
Chris Austenac4604a2015-10-13 12:43:27 -0500374
Chris Austen0012e9b2015-10-22 01:37:46 -0500375ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 ipmi_request_t request, ipmi_response_t response,
377 ipmi_data_len_t data_len, ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500378{
Patrick Ventured99148b2018-10-13 10:06:13 -0700379 auto reqptr = static_cast<sensor_data_t*>(request);
Chris Austenac4604a2015-10-13 12:43:27 -0500380
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530381 log<level::DEBUG>("IPMI SET_SENSOR",
382 entry("SENSOR_NUM=0x%02x", reqptr->sennum));
Chris Austenac4604a2015-10-13 12:43:27 -0500383
Tom Josephbe703f72017-03-09 12:34:35 +0530384 /*
385 * This would support the Set Sensor Reading command for the presence
386 * and functional state of Processor, Core & DIMM. For the remaining
387 * sensors the existing support is invoked.
388 */
389 auto ipmiRC = setSensorReading(request);
390
Patrick Venture0b02be92018-08-31 11:55:55 -0700391 if (ipmiRC == IPMI_CC_SENSOR_INVALID)
Tom Josephbe703f72017-03-09 12:34:35 +0530392 {
393 updateSensorRecordFromSSRAESC(reqptr);
394 ipmiRC = IPMI_CC_OK;
395 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500396
Patrick Venture0b02be92018-08-31 11:55:55 -0700397 *data_len = 0;
Tom Josephbe703f72017-03-09 12:34:35 +0530398 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500399}
400
Tom Joseph3ee668f2018-03-02 19:49:17 +0530401ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700402 ipmi_request_t request,
403 ipmi_response_t response,
404 ipmi_data_len_t data_len,
405 ipmi_context_t context)
Tom Joseph3ee668f2018-03-02 19:49:17 +0530406{
Patrick Ventured99148b2018-10-13 10:06:13 -0700407 auto reqptr = static_cast<sensor_data_t*>(request);
408 auto resp = static_cast<sensorreadingresp_t*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700409 ipmi::sensor::GetSensorResponse getResponse{};
Tom Joseph3ee668f2018-03-02 19:49:17 +0530410 static constexpr auto scanningEnabledBit = 6;
411
412 const auto iter = sensors.find(reqptr->sennum);
413 if (iter == sensors.end())
414 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -0500415 return IPMI_CC_SENSOR_INVALID;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530416 }
417 if (ipmi::sensor::Mutability::Read !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700418 (iter->second.mutability & ipmi::sensor::Mutability::Read))
Tom Joseph3ee668f2018-03-02 19:49:17 +0530419 {
Jayanth Othayoth6ccf8812018-04-05 22:58:48 -0500420 return IPMI_CC_ILLEGAL_COMMAND;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530421 }
422
423 try
424 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700425 getResponse = iter->second.getFunc(iter->second);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530426 *data_len = getResponse.size();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700427 std::memcpy(resp, getResponse.data(), *data_len);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530428 resp->operation = 1 << scanningEnabledBit;
429 return IPMI_CC_OK;
430 }
431 catch (const std::exception& e)
432 {
433 *data_len = getResponse.size();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700434 std::memcpy(resp, getResponse.data(), *data_len);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530435 return IPMI_CC_OK;
436 }
437}
438
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530439void getSensorThresholds(uint8_t sensorNum,
440 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600441{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530442 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600443 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530444 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600445 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600446
447 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
448
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530449 const auto iter = sensors.find(sensorNum);
450 const auto info = iter->second;
451
452 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
453
Patrick Venture0b02be92018-08-31 11:55:55 -0700454 auto warnThresholds = ipmi::getAllDbusProperties(
455 bus, service, info.sensorPath, warningThreshIntf);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530456
William A. Kennington III4c008022018-10-12 17:18:14 -0700457 double warnLow = variant_ns::visit(ipmi::VariantToDoubleVisitor(),
458 warnThresholds["WarningLow"]);
459 double warnHigh = variant_ns::visit(ipmi::VariantToDoubleVisitor(),
460 warnThresholds["WarningHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530461
462 if (warnLow != 0)
463 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700464 warnLow *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700465 response->lowerNonCritical = static_cast<uint8_t>(
466 (warnLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530467 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700468 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530469 }
470
471 if (warnHigh != 0)
472 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700473 warnHigh *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700474 response->upperNonCritical = static_cast<uint8_t>(
475 (warnHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530476 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700477 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530478 }
479
Patrick Venture0b02be92018-08-31 11:55:55 -0700480 auto critThresholds = ipmi::getAllDbusProperties(
481 bus, service, info.sensorPath, criticalThreshIntf);
William A. Kennington III4c008022018-10-12 17:18:14 -0700482 double critLow = variant_ns::visit(ipmi::VariantToDoubleVisitor(),
483 critThresholds["CriticalLow"]);
484 double critHigh = variant_ns::visit(ipmi::VariantToDoubleVisitor(),
485 critThresholds["CriticalHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530486
487 if (critLow != 0)
488 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700489 critLow *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700490 response->lowerCritical = static_cast<uint8_t>(
491 (critLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530492 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700493 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530494 }
495
496 if (critHigh != 0)
497 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700498 critHigh *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700499 response->upperCritical = static_cast<uint8_t>(
500 (critHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530501 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700502 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530503 }
504}
505
506ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700507 ipmi_request_t request,
508 ipmi_response_t response,
509 ipmi_data_len_t data_len,
510 ipmi_context_t context)
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530511{
512 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
513
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600514 if (*data_len != sizeof(uint8_t))
515 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530516 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600517 return IPMI_CC_REQ_DATA_LEN_INVALID;
518 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600519
Patrick Venture0b02be92018-08-31 11:55:55 -0700520 auto sensorNum = *(reinterpret_cast<const uint8_t*>(request));
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530521 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600522
523 const auto iter = sensors.find(sensorNum);
524 if (iter == sensors.end())
525 {
526 return IPMI_CC_SENSOR_INVALID;
527 }
528
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530529 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600530
Patrick Venture0b02be92018-08-31 11:55:55 -0700531 // Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530532 if (info.propertyInterfaces.find(valueInterface) ==
533 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600534 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700535 // return with valid mask as 0
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600536 return IPMI_CC_OK;
537 }
538
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530539 auto responseData =
540 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600541
542 try
543 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530544 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600545 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530546 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600547 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700548 // Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600549 responseData->validMask = 0;
550 }
551
552 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
553 return IPMI_CC_OK;
554}
555
Chris Austen0012e9b2015-10-22 01:37:46 -0500556ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
557 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500558 ipmi_data_len_t data_len, ipmi_context_t context)
559{
Nan Li70aa8d92016-08-29 00:11:10 +0800560 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500561
Patrick Venture0b02be92018-08-31 11:55:55 -0700562 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n", netfn, cmd);
Chris Austenac4604a2015-10-13 12:43:27 -0500563 *data_len = 0;
564
565 return rc;
566}
567
Emily Shafferd06e0e72017-04-05 09:08:57 -0700568ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
569 ipmi_request_t request,
570 ipmi_response_t response,
571 ipmi_data_len_t data_len,
572 ipmi_context_t context)
573{
574 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
575 if (request == nullptr ||
576 get_sdr_info::request::get_count(request) == false)
577 {
578 // Get Sensor Count
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800579 resp->count = sensors.size() + frus.size() + entities.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700580 }
581 else
582 {
583 resp->count = 1;
584 }
585
586 // Multiple LUNs not supported.
587 namespace response = get_sdr_info::response;
588 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
589 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
590 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
591 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
592 response::set_static_population(&(resp->luns_and_dynamic_population));
593
594 *data_len = SDR_INFO_RESP_SIZE;
595
596 return IPMI_CC_OK;
597}
598
Emily Shaffera344afc2017-04-13 15:09:39 -0700599ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
600 ipmi_request_t request,
601 ipmi_response_t response,
602 ipmi_data_len_t data_len,
603 ipmi_context_t context)
604{
605 // A constant reservation ID is okay until we implement add/remove SDR.
606 const uint16_t reservation_id = 1;
607 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700608 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700609
610 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
611 return IPMI_CC_OK;
612}
Chris Austenac4604a2015-10-13 12:43:27 -0500613
Patrick Venture0b02be92018-08-31 11:55:55 -0700614void setUnitFieldsForObject(const ipmi::sensor::Info* info,
615 get_sdr::SensorDataFullRecordBody* body)
Emily Shaffercc941e12017-06-14 13:06:26 -0700616{
Tom Josephdc212b22018-02-16 09:59:57 +0530617 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
618 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700619 {
Tom Josephdc212b22018-02-16 09:59:57 +0530620 auto unit = server::Value::convertUnitFromString(info->unit);
621 // Unit strings defined in
622 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
623 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700624 {
Tom Josephdc212b22018-02-16 09:59:57 +0530625 case server::Value::Unit::DegreesC:
626 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
627 break;
628 case server::Value::Unit::RPMS:
Kirill Pakhomov812e44c2018-10-22 16:25:35 +0300629 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_RPM;
Tom Josephdc212b22018-02-16 09:59:57 +0530630 break;
631 case server::Value::Unit::Volts:
632 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
633 break;
634 case server::Value::Unit::Meters:
635 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
636 break;
637 case server::Value::Unit::Amperes:
638 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
639 break;
640 case server::Value::Unit::Joules:
641 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
642 break;
643 case server::Value::Unit::Watts:
644 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
645 break;
646 default:
647 // Cannot be hit.
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700648 std::fprintf(stderr, "Unknown value unit type: = %s\n",
649 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700650 }
651 }
Patrick Venture64678b82018-10-13 13:11:32 -0700652 catch (const sdbusplus::exception::InvalidEnumString& e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700653 {
Tom Josephdc212b22018-02-16 09:59:57 +0530654 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700655 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700656}
657
Patrick Venture0b02be92018-08-31 11:55:55 -0700658ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body,
659 const ipmi::sensor::Info* info,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700660 ipmi_data_len_t data_len)
661{
662 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700663 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700664 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700665
666 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
667
668 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530669 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700670
671 get_sdr::body::set_b(info->coefficientB, body);
672 get_sdr::body::set_m(info->coefficientM, body);
673 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530674 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700675
Emily Shafferbbef71c2017-05-08 16:36:17 -0700676 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700677 }
678
Tom Joseph96423912018-01-25 00:14:34 +0530679 /* ID string */
680 auto id_string = info->sensorNameFunc(*info);
681
682 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
683 {
684 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
685 }
686 else
687 {
688 get_sdr::body::set_id_strlen(id_string.length(), body);
689 }
690 strncpy(body->id_string, id_string.c_str(),
691 get_sdr::body::get_id_strlen(body));
692
Emily Shafferbbef71c2017-05-08 16:36:17 -0700693 return IPMI_CC_OK;
694};
695
Ratan Guptae0cc8552018-01-22 14:23:04 +0530696ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
697 ipmi_data_len_t data_len)
698{
699 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
700 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700701 get_sdr::SensorDataFruRecord record{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530702 auto dataLength = 0;
703
704 auto fru = frus.begin();
Patrick Venture0b02be92018-08-31 11:55:55 -0700705 uint8_t fruID{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530706 auto recordID = get_sdr::request::get_record_id(req);
707
708 fruID = recordID - FRU_RECORD_ID_START;
709 fru = frus.find(fruID);
710 if (fru == frus.end())
711 {
712 return IPMI_CC_SENSOR_INVALID;
713 }
714
715 /* Header */
716 get_sdr::header::set_record_id(recordID, &(record.header));
717 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
718 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
719 record.header.record_length = sizeof(record.key) + sizeof(record.body);
720
721 /* Key */
722 record.key.fruID = fruID;
723 record.key.accessLun |= IPMI_LOGICAL_FRU;
724 record.key.deviceAddress = BMCSlaveAddress;
725
726 /* Body */
727 record.body.entityID = fru->second[0].entityID;
728 record.body.entityInstance = fru->second[0].entityInstance;
729 record.body.deviceType = fruInventoryDevice;
730 record.body.deviceTypeModifier = IPMIFruInventory;
731
732 /* Device ID string */
Patrick Venture0b02be92018-08-31 11:55:55 -0700733 auto deviceID =
734 fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1,
735 fru->second[0].path.length());
Ratan Guptae0cc8552018-01-22 14:23:04 +0530736
737 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
738 {
739 get_sdr::body::set_device_id_strlen(
Patrick Venture0b02be92018-08-31 11:55:55 -0700740 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530741 }
742 else
743 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700744 get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530745 }
746
747 strncpy(record.body.deviceID, deviceID.c_str(),
748 get_sdr::body::get_device_id_strlen(&(record.body)));
749
750 if (++fru == frus.end())
751 {
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800752 // we have reached till end of fru, so assign the next record id to
753 // 512(Max fru ID = 511) + Entity Record ID(may start with 0).
754 auto next_record_id =
755 (entities.size()) ? entities.begin()->first + ENTITY_RECORD_ID_START
756 : END_OF_RECORD;
757 get_sdr::response::set_next_record_id(next_record_id, resp);
758 }
759 else
760 {
761 get_sdr::response::set_next_record_id(
762 (FRU_RECORD_ID_START + fru->first), resp);
763 }
764
765 // Check for invalid offset size
766 if (req->offset > sizeof(record))
767 {
768 return IPMI_CC_PARM_OUT_OF_RANGE;
769 }
770
771 dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
772 sizeof(record) - req->offset);
773
774 std::memcpy(resp->record_data,
775 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
776
777 *data_len = dataLength;
778 *data_len += 2; // additional 2 bytes for next record ID
779
780 return IPMI_CC_OK;
781}
782
783ipmi_ret_t ipmi_entity_get_sdr(ipmi_request_t request, ipmi_response_t response,
784 ipmi_data_len_t data_len)
785{
786 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
787 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
788 get_sdr::SensorDataEntityRecord record{};
789 auto dataLength = 0;
790
791 auto entity = entities.begin();
792 uint8_t entityRecordID;
793 auto recordID = get_sdr::request::get_record_id(req);
794
795 entityRecordID = recordID - ENTITY_RECORD_ID_START;
796 entity = entities.find(entityRecordID);
797 if (entity == entities.end())
798 {
799 return IPMI_CC_SENSOR_INVALID;
800 }
801
802 /* Header */
803 get_sdr::header::set_record_id(recordID, &(record.header));
804 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
805 record.header.record_type = get_sdr::SENSOR_DATA_ENTITY_RECORD;
806 record.header.record_length = sizeof(record.key) + sizeof(record.body);
807
808 /* Key */
809 record.key.containerEntityId = entity->second.containerEntityId;
810 record.key.containerEntityInstance = entity->second.containerEntityInstance;
811 get_sdr::key::set_flags(entity->second.isList, entity->second.isLinked,
812 &(record.key));
813 record.key.entityId1 = entity->second.containedEntities[0].first;
814 record.key.entityInstance1 = entity->second.containedEntities[0].second;
815
816 /* Body */
817 record.body.entityId2 = entity->second.containedEntities[1].first;
818 record.body.entityInstance2 = entity->second.containedEntities[1].second;
819 record.body.entityId3 = entity->second.containedEntities[2].first;
820 record.body.entityInstance3 = entity->second.containedEntities[2].second;
821 record.body.entityId4 = entity->second.containedEntities[3].first;
822 record.body.entityInstance4 = entity->second.containedEntities[3].second;
823
824 if (++entity == entities.end())
825 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700826 get_sdr::response::set_next_record_id(END_OF_RECORD,
827 resp); // last record
Ratan Guptae0cc8552018-01-22 14:23:04 +0530828 }
829 else
830 {
831 get_sdr::response::set_next_record_id(
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800832 (ENTITY_RECORD_ID_START + entity->first), resp);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530833 }
834
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700835 // Check for invalid offset size
836 if (req->offset > sizeof(record))
Ratan Guptae0cc8552018-01-22 14:23:04 +0530837 {
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700838 return IPMI_CC_PARM_OUT_OF_RANGE;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530839 }
840
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700841 dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
842 sizeof(record) - req->offset);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530843
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700844 std::memcpy(resp->record_data,
Jason M. Bills1cd85962018-10-05 12:04:01 -0700845 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530846
847 *data_len = dataLength;
848 *data_len += 2; // additional 2 bytes for next record ID
849
850 return IPMI_CC_OK;
851}
852
Emily Shafferbbef71c2017-05-08 16:36:17 -0700853ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
854 ipmi_request_t request, ipmi_response_t response,
855 ipmi_data_len_t data_len, ipmi_context_t context)
856{
857 ipmi_ret_t ret = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700858 get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request;
859 get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700860 get_sdr::SensorDataFullRecord record = {0};
861 if (req != NULL)
862 {
863 // Note: we use an iterator so we can provide the next ID at the end of
864 // the call.
865 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +0530866 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700867
868 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +0530869 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700870 {
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800871 // recordID 0 to 255 means it is a FULL record.
872 // recordID 256 to 511 means it is a FRU record.
873 // recordID greater then 511 means it is a Entity Association
874 // record. Currently we are supporting three record types: FULL
875 // record, FRU record and Enttiy Association record.
876 if (recordID >= ENTITY_RECORD_ID_START)
877 {
878 return ipmi_entity_get_sdr(request, response, data_len);
879 }
880 else if (recordID >= FRU_RECORD_ID_START &&
881 recordID < ENTITY_RECORD_ID_START)
Ratan Guptae0cc8552018-01-22 14:23:04 +0530882 {
883 return ipmi_fru_get_sdr(request, response, data_len);
884 }
885 else
886 {
887 sensor = sensors.find(recordID);
888 if (sensor == sensors.end())
889 {
890 return IPMI_CC_SENSOR_INVALID;
891 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700892 }
893 }
894
895 uint8_t sensor_id = sensor->first;
896
897 /* Header */
898 get_sdr::header::set_record_id(sensor_id, &(record.header));
899 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
900 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
901 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
902
903 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +0530904 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -0700905 record.key.sensor_number = sensor_id;
906
907 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +0530908 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700909 record.body.sensor_type = sensor->second.sensorType;
910 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +0530911 record.body.entity_instance = sensor->second.instance;
Jaghathiswari Rankappagounder Natarajan0780df12019-02-06 15:29:24 -0800912 if (ipmi::sensor::Mutability::Write ==
913 (sensor->second.mutability & ipmi::sensor::Mutability::Write))
914 {
915 get_sdr::body::init_settable_state(true, &(record.body));
916 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700917
918 // Set the type-specific details given the DBus interface
919 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
920 data_len);
921
922 if (++sensor == sensors.end())
923 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530924 // we have reached till end of sensor, so assign the next record id
925 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
Patrick Venture0b02be92018-08-31 11:55:55 -0700926 auto next_record_id =
927 (frus.size()) ? frus.begin()->first + FRU_RECORD_ID_START
928 : END_OF_RECORD;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530929
930 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700931 }
932 else
933 {
934 get_sdr::response::set_next_record_id(sensor->first, resp);
935 }
936
Emily Shaffer6c9ee512018-09-27 11:04:36 -0700937 if (req->offset > sizeof(record))
938 {
939 return IPMI_CC_PARM_OUT_OF_RANGE;
940 }
941
942 // data_len will ultimately be the size of the record, plus
943 // the size of the next record ID:
944 *data_len = std::min(static_cast<size_t>(req->bytes_to_read),
945 sizeof(record) - req->offset);
946
947 std::memcpy(resp->record_data,
948 reinterpret_cast<uint8_t*>(&record) + req->offset,
949 *data_len);
950
951 // data_len should include the LSB and MSB:
Jason M. Bills1cd85962018-10-05 12:04:01 -0700952 *data_len +=
953 sizeof(resp->next_record_id_lsb) + sizeof(resp->next_record_id_msb);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700954 }
955
956 return ret;
957}
958
Jia, Chunhui3342a8e2018-12-29 13:32:26 +0800959static bool isFromSystemChannel()
960{
961 // TODO we could not figure out where the request is from based on IPMI
962 // command handler parameters. because of it, we can not differentiate
963 // request from SMS/SMM or IPMB channel
964 return true;
965}
966
967ipmi_ret_t ipmicmdPlatformEvent(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
968 ipmi_request_t request,
969 ipmi_response_t response,
970 ipmi_data_len_t dataLen, ipmi_context_t context)
971{
972 uint16_t generatorID;
973 size_t count;
974 bool assert = true;
975 std::string sensorPath;
976 size_t paraLen = *dataLen;
977 PlatformEventRequest* req;
978 *dataLen = 0;
979
980 if ((paraLen < selSystemEventSizeWith1Bytes) ||
981 (paraLen > selSystemEventSizeWith3Bytes))
982 {
983 return IPMI_CC_REQ_DATA_LEN_INVALID;
984 }
985
986 if (isFromSystemChannel())
987 { // first byte for SYSTEM Interface is Generator ID
988 // +1 to get common struct
989 req = reinterpret_cast<PlatformEventRequest*>((uint8_t*)request + 1);
990 // Capture the generator ID
991 generatorID = *reinterpret_cast<uint8_t*>(request);
992 // Platform Event usually comes from other firmware, like BIOS.
993 // Unlike BMC sensor, it does not have BMC DBUS sensor path.
994 sensorPath = "System";
995 }
996 else
997 {
998 req = reinterpret_cast<PlatformEventRequest*>(request);
999 // TODO GenratorID for IPMB is combination of RqSA and RqLUN
1000 generatorID = 0xff;
1001 sensorPath = "IPMB";
1002 }
1003 // Content of event data field depends on sensor class.
1004 // When data0 bit[5:4] is non-zero, valid data counts is 3.
1005 // When data0 bit[7:6] is non-zero, valid data counts is 2.
1006 if (((req->data[0] & byte3EnableMask) != 0 &&
1007 paraLen < selSystemEventSizeWith3Bytes) ||
1008 ((req->data[0] & byte2EnableMask) != 0 &&
1009 paraLen < selSystemEventSizeWith2Bytes))
1010 {
1011 return IPMI_CC_REQ_DATA_LEN_INVALID;
1012 }
1013
1014 // Count bytes of Event Data
1015 if ((req->data[0] & byte3EnableMask) != 0)
1016 {
1017 count = 3;
1018 }
1019 else if ((req->data[0] & byte2EnableMask) != 0)
1020 {
1021 count = 2;
1022 }
1023 else
1024 {
1025 count = 1;
1026 }
1027 assert = req->eventDirectionType & directionMask ? false : true;
1028 std::vector<uint8_t> eventData(req->data, req->data + count);
1029
1030 sdbusplus::bus::bus dbus(bus);
1031 std::string service =
1032 ipmi::getService(dbus, ipmiSELAddInterface, ipmiSELPath);
1033 sdbusplus::message::message writeSEL = dbus.new_method_call(
1034 service.c_str(), ipmiSELPath, ipmiSELAddInterface, "IpmiSelAdd");
1035 writeSEL.append(ipmiSELAddMessage, sensorPath, eventData, assert,
1036 generatorID);
1037 try
1038 {
1039 dbus.call(writeSEL);
1040 }
1041 catch (sdbusplus::exception_t& e)
1042 {
1043 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1044 return IPMI_CC_UNSPECIFIED_ERROR;
1045 }
1046 return IPMI_CC_OK;
1047}
1048
Chris Austenac4604a2015-10-13 12:43:27 -05001049void register_netfn_sen_functions()
1050{
Tom05732372016-09-06 17:21:23 +05301051 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001052 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr,
1053 ipmi_sen_wildcard, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001054
Jia, Chunhui3342a8e2018-12-29 13:32:26 +08001055 // <Platform Event Message>
1056 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_PLATFORM_EVENT, nullptr,
1057 ipmicmdPlatformEvent, PRIVILEGE_OPERATOR);
Tom05732372016-09-06 17:21:23 +05301058 // <Get Sensor Type>
Patrick Venture0b02be92018-08-31 11:55:55 -07001059 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, nullptr,
1060 ipmi_sen_get_sensor_type, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001061
Tom05732372016-09-06 17:21:23 +05301062 // <Set Sensor Reading and Event Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001063 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, nullptr,
1064 ipmi_sen_set_sensor, PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001065
Tom05732372016-09-06 17:21:23 +05301066 // <Get Sensor Reading>
Patrick Venture0b02be92018-08-31 11:55:55 -07001067 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, nullptr,
1068 ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
Emily Shaffera344afc2017-04-13 15:09:39 -07001069
Tom Joseph5ca50952018-02-22 00:33:38 +05301070 // <Reserve Device SDR Repository>
1071 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_DEVICE_SDR_REPO,
Patrick Venture0b02be92018-08-31 11:55:55 -07001072 nullptr, ipmi_sen_reserve_sdr, PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001073
Tom Joseph5ca50952018-02-22 00:33:38 +05301074 // <Get Device SDR Info>
Patrick Venture0b02be92018-08-31 11:55:55 -07001075 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR_INFO, nullptr,
1076 ipmi_sen_get_sdr_info, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001077
Tom Joseph5ca50952018-02-22 00:33:38 +05301078 // <Get Device SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -07001079 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr,
1080 ipmi_sen_get_sdr, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001081
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001082 // <Get Sensor Thresholds>
1083 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1084 nullptr, ipmi_sen_get_sensor_thresholds,
1085 PRIVILEGE_USER);
1086
Chris Austenac4604a2015-10-13 12:43:27 -05001087 return;
1088}