blob: 1901464574c49d61c0df99ed7f12a92b644a7b9d [file] [log] [blame]
Brandon Kim9cf85622019-06-19 12:05:08 -07001#include "config.h"
2
Patrick Venture46470a32018-09-07 19:26:25 -07003#include "sensorhandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
5#include "fruread.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07006
Tomd700e762016-09-20 18:24:13 +05307#include <mapper.h>
Chris Austen10ccc0f2015-12-10 18:27:04 -06008#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07009
10#include <bitset>
Patrick Venture586d35b2018-09-07 19:56:18 -070011#include <cmath>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070012#include <cstring>
Vernon Mauerye08fbff2019-04-03 09:19:34 -070013#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070014#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070015#include <ipmid/utils.hpp>
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050016#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070017#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070018#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070019#include <set>
20#include <xyz/openbmc_project/Common/error.hpp>
21#include <xyz/openbmc_project/Sensor/Value/server.hpp>
22
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 Venture235b3c72019-08-23 09:16:07 -070080namespace ipmi
81{
82namespace sensor
83{
84extern const EntityInfoMap entities;
85
86const EntityInfoMap& getIpmiEntityRecords()
Patrick Venture83a0b842019-07-19 18:37:15 -070087{
88 return entities;
89}
90
Patrick Venture235b3c72019-08-23 09:16:07 -070091} // namespace sensor
92} // namespace ipmi
93
Patrick Venture0b02be92018-08-31 11:55:55 -070094int get_bus_for_path(const char* path, char** busname)
95{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070096 return mapper_get_service(bus, path, busname);
97}
Tomd700e762016-09-20 18:24:13 +053098
Emily Shaffer2ae09b92017-04-05 15:09:41 -070099// Use a lookup table to find the interface name of a specific sensor
100// This will be used until an alternative is found. this is the first
101// step for mapping IPMI
Patrick Venture0b02be92018-08-31 11:55:55 -0700102int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
103{
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700104 int rc;
105
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700106 const auto& sensor_it = sensors.find(num);
107 if (sensor_it == sensors.end())
108 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -0500109 // The sensor map does not contain the sensor requested
110 return -EINVAL;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700111 }
112
113 const auto& info = sensor_it->second;
114
Patrick Williams8451edf2017-06-13 09:01:06 -0500115 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700116 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700117 if (rc < 0)
118 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700119 std::fprintf(stderr, "Failed to get %s busname: %s\n",
120 info.sensorPath.c_str(), busname);
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700121 goto final;
122 }
123
124 interface->sensortype = info.sensorType;
125 strcpy(interface->bus, busname);
126 strcpy(interface->path, info.sensorPath.c_str());
127 // Take the interface name from the beginning of the DbusInterfaceMap. This
128 // works for the Value interface but may not suffice for more complex
129 // sensors.
130 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Patrick Venture0b02be92018-08-31 11:55:55 -0700131 strcpy(interface->interface,
132 info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700133 interface->sensornumber = num;
134
135final:
136 free(busname);
137 return rc;
138}
139
Tomd700e762016-09-20 18:24:13 +0530140/////////////////////////////////////////////////////////////////////
141//
142// Routines used by ipmi commands wanting to interact on the dbus
143//
144/////////////////////////////////////////////////////////////////////
Patrick Venture0b02be92018-08-31 11:55:55 -0700145int set_sensor_dbus_state_s(uint8_t number, const char* method,
146 const char* value)
147{
Tomd700e762016-09-20 18:24:13 +0530148
149 dbus_interface_t a;
150 int r;
151 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700152 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530153
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700154 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530155
Patrick Venture0b02be92018-08-31 11:55:55 -0700156 if (r < 0)
157 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700158 std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
Tomd700e762016-09-20 18:24:13 +0530159 return 0;
160 }
161
Patrick Venture0b02be92018-08-31 11:55:55 -0700162 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
163 method);
164 if (r < 0)
165 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700166 std::fprintf(stderr, "Failed to create a method call: %s",
167 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530168 goto final;
169 }
170
171 r = sd_bus_message_append(m, "v", "s", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700172 if (r < 0)
173 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700174 std::fprintf(stderr, "Failed to create a input parameter: %s",
175 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530176 goto final;
177 }
178
Tomd700e762016-09-20 18:24:13 +0530179 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700180 if (r < 0)
181 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700182 std::fprintf(stderr, "Failed to call the method: %s", strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530183 }
184
185final:
186 sd_bus_error_free(&error);
187 m = sd_bus_message_unref(m);
188
189 return 0;
190}
Patrick Venture0b02be92018-08-31 11:55:55 -0700191int set_sensor_dbus_state_y(uint8_t number, const char* method,
192 const uint8_t value)
193{
Tomd700e762016-09-20 18:24:13 +0530194
195 dbus_interface_t a;
196 int r;
197 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700198 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530199
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700200 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530201
Patrick Venture0b02be92018-08-31 11:55:55 -0700202 if (r < 0)
203 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700204 std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
Tomd700e762016-09-20 18:24:13 +0530205 return 0;
206 }
207
Patrick Venture0b02be92018-08-31 11:55:55 -0700208 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
209 method);
210 if (r < 0)
211 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700212 std::fprintf(stderr, "Failed to create a method call: %s",
213 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530214 goto final;
215 }
216
217 r = sd_bus_message_append(m, "v", "i", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700218 if (r < 0)
219 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700220 std::fprintf(stderr, "Failed to create a input parameter: %s",
221 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530222 goto final;
223 }
224
Tomd700e762016-09-20 18:24:13 +0530225 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700226 if (r < 0)
227 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700228 std::fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530229 }
230
231final:
232 sd_bus_error_free(&error);
233 m = sd_bus_message_unref(m);
234
235 return 0;
236}
237
Patrick Venture0b02be92018-08-31 11:55:55 -0700238uint8_t dbus_to_sensor_type(char* p)
239{
Chris Austenac4604a2015-10-13 12:43:27 -0500240
Patrick Venture0b02be92018-08-31 11:55:55 -0700241 sensorTypemap_t* s = g_SensorTypeMap;
242 char r = 0;
243 while (s->number != 0xFF)
244 {
245 if (!strcmp(s->dbusname, p))
246 {
Tom Joseph558184e2017-09-01 13:45:05 +0530247 r = s->typecode;
Patrick Venture0b02be92018-08-31 11:55:55 -0700248 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500249 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500250 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500251 }
252
Chris Austen0012e9b2015-10-22 01:37:46 -0500253 if (s->number == 0xFF)
254 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500255
Chris Austen0012e9b2015-10-22 01:37:46 -0500256 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500257}
258
Patrick Venture0b02be92018-08-31 11:55:55 -0700259uint8_t get_type_from_interface(dbus_interface_t dbus_if)
260{
Chris Austen0012e9b2015-10-22 01:37:46 -0500261
Brad Bishop56003452016-10-05 21:49:19 -0400262 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500263
Chris Austen0012e9b2015-10-22 01:37:46 -0500264 // This is where sensors that do not exist in dbus but do
265 // exist in the host code stop. This should indicate it
266 // is not a supported sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700267 if (dbus_if.interface[0] == 0)
268 {
269 return 0;
270 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500271
Emily Shaffer71174412017-04-05 15:10:40 -0700272 // Fetch type from interface itself.
273 if (dbus_if.sensortype != 0)
274 {
275 type = dbus_if.sensortype;
Patrick Venture0b02be92018-08-31 11:55:55 -0700276 }
277 else
278 {
Chris Austen0012e9b2015-10-22 01:37:46 -0500279 // Non InventoryItems
Patrick Venture4491a462018-10-13 13:00:42 -0700280 char* p = strrchr(dbus_if.path, '/');
Patrick Venture0b02be92018-08-31 11:55:55 -0700281 type = dbus_to_sensor_type(p + 1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500282 }
283
Brad Bishop56003452016-10-05 21:49:19 -0400284 return type;
Patrick Venture0b02be92018-08-31 11:55:55 -0700285}
Chris Austen0012e9b2015-10-22 01:37:46 -0500286
Emily Shaffer391f3302017-04-03 10:27:08 -0700287// Replaces find_sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700288uint8_t find_type_for_sensor_number(uint8_t num)
289{
Emily Shaffer391f3302017-04-03 10:27:08 -0700290 int r;
291 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700292 r = find_openbmc_path(num, &dbus_if);
Patrick Venture0b02be92018-08-31 11:55:55 -0700293 if (r < 0)
294 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700295 std::fprintf(stderr, "Could not find sensor %d\n", num);
Lei YU91875f72018-04-03 15:14:49 +0800296 return 0;
Emily Shaffer391f3302017-04-03 10:27:08 -0700297 }
298 return get_type_from_interface(dbus_if);
299}
300
Chris Austen0012e9b2015-10-22 01:37:46 -0500301ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700302 ipmi_request_t request,
303 ipmi_response_t response,
304 ipmi_data_len_t data_len,
305 ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500306{
Patrick Ventured99148b2018-10-13 10:06:13 -0700307 auto reqptr = static_cast<sensor_data_t*>(request);
Chris Austenac4604a2015-10-13 12:43:27 -0500308 ipmi_ret_t rc = IPMI_CC_OK;
309
Patrick Venture0b02be92018-08-31 11:55:55 -0700310 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n", reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500311
312 // TODO Not sure what the System-event-sensor is suppose to return
313 // need to ask Hostboot team
Patrick Venture0b02be92018-08-31 11:55:55 -0700314 unsigned char buf[] = {0x00, 0x6F};
Chris Austenac4604a2015-10-13 12:43:27 -0500315
Emily Shaffer391f3302017-04-03 10:27:08 -0700316 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500317
318 // HACK UNTIL Dbus gets updated or we find a better way
Patrick Venture0b02be92018-08-31 11:55:55 -0700319 if (buf[0] == 0)
320 {
Chris Austen800ba712015-12-03 15:31:00 -0600321 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500322 }
323
Chris Austenac4604a2015-10-13 12:43:27 -0500324 *data_len = sizeof(buf);
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700325 std::memcpy(response, &buf, *data_len);
Chris Austenac4604a2015-10-13 12:43:27 -0500326
Chris Austenac4604a2015-10-13 12:43:27 -0500327 return rc;
328}
329
Patrick Venture0b02be92018-08-31 11:55:55 -0700330const std::set<std::string> analogSensorInterfaces = {
Emily Shaffercc941e12017-06-14 13:06:26 -0700331 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700332 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700333};
334
335bool isAnalogSensor(const std::string& interface)
336{
337 return (analogSensorInterfaces.count(interface));
338}
339
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000340/**
341@brief This command is used to set sensorReading.
342
343@param
344 - sensorNumber
345 - operation
346 - reading
347 - assertOffset0_7
348 - assertOffset8_14
349 - deassertOffset0_7
350 - deassertOffset8_14
351 - eventData1
352 - eventData2
353 - eventData3
354
355@return completion code on success.
356**/
357
358ipmi::RspType<> ipmiSetSensorReading(uint8_t sensorNumber, uint8_t operation,
359 uint8_t reading, uint8_t assertOffset0_7,
360 uint8_t assertOffset8_14,
361 uint8_t deassertOffset0_7,
362 uint8_t deassertOffset8_14,
363 uint8_t eventData1, uint8_t eventData2,
364 uint8_t eventData3)
Tom Josephbe703f72017-03-09 12:34:35 +0530365{
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000366 log<level::DEBUG>("IPMI SET_SENSOR",
367 entry("SENSOR_NUM=0x%02x", sensorNumber));
368
369 ipmi::sensor::SetSensorReadingReq cmdData;
370
371 cmdData.number = sensorNumber;
372 cmdData.operation = operation;
373 cmdData.reading = reading;
374 cmdData.assertOffset0_7 = assertOffset0_7;
375 cmdData.assertOffset8_14 = assertOffset8_14;
376 cmdData.deassertOffset0_7 = deassertOffset0_7;
377 cmdData.deassertOffset8_14 = deassertOffset8_14;
378 cmdData.eventData1 = eventData1;
379 cmdData.eventData2 = eventData2;
380 cmdData.eventData3 = eventData3;
Tom Josephbe703f72017-03-09 12:34:35 +0530381
382 // Check if the Sensor Number is present
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000383 const auto iter = sensors.find(sensorNumber);
Tom Josephbe703f72017-03-09 12:34:35 +0530384 if (iter == sensors.end())
385 {
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000386 updateSensorRecordFromSSRAESC(&sensorNumber);
387 return ipmi::responseSuccess();
Tom Josephbe703f72017-03-09 12:34:35 +0530388 }
389
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500390 try
391 {
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500392 if (ipmi::sensor::Mutability::Write !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700393 (iter->second.mutability & ipmi::sensor::Mutability::Write))
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500394 {
395 log<level::ERR>("Sensor Set operation is not allowed",
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000396 entry("SENSOR_NUM=%d", sensorNumber));
397 return ipmi::responseIllegalCommand();
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500398 }
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000399 auto ipmiRC = iter->second.updateFunc(cmdData, iter->second);
400 return ipmi::response(ipmiRC);
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500401 }
402 catch (InternalFailure& e)
403 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700404 log<level::ERR>("Set sensor failed",
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000405 entry("SENSOR_NUM=%d", sensorNumber));
Patrick Venture0b02be92018-08-31 11:55:55 -0700406 commit<InternalFailure>();
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000407 return ipmi::responseUnspecifiedError();
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500408 }
Tom Joseph82024322017-09-28 20:07:29 +0530409 catch (const std::runtime_error& e)
410 {
411 log<level::ERR>(e.what());
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +0000412 return ipmi::responseUnspecifiedError();
Tom Joseph82024322017-09-28 20:07:29 +0530413 }
Chris Austenac4604a2015-10-13 12:43:27 -0500414}
415
Tom Joseph3ee668f2018-03-02 19:49:17 +0530416ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700417 ipmi_request_t request,
418 ipmi_response_t response,
419 ipmi_data_len_t data_len,
420 ipmi_context_t context)
Tom Joseph3ee668f2018-03-02 19:49:17 +0530421{
Patrick Ventured99148b2018-10-13 10:06:13 -0700422 auto reqptr = static_cast<sensor_data_t*>(request);
423 auto resp = static_cast<sensorreadingresp_t*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700424 ipmi::sensor::GetSensorResponse getResponse{};
Tom Joseph3ee668f2018-03-02 19:49:17 +0530425 static constexpr auto scanningEnabledBit = 6;
426
427 const auto iter = sensors.find(reqptr->sennum);
428 if (iter == sensors.end())
429 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -0500430 return IPMI_CC_SENSOR_INVALID;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530431 }
432 if (ipmi::sensor::Mutability::Read !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700433 (iter->second.mutability & ipmi::sensor::Mutability::Read))
Tom Joseph3ee668f2018-03-02 19:49:17 +0530434 {
Jayanth Othayoth6ccf8812018-04-05 22:58:48 -0500435 return IPMI_CC_ILLEGAL_COMMAND;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530436 }
437
438 try
439 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700440 getResponse = iter->second.getFunc(iter->second);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530441 *data_len = getResponse.size();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700442 std::memcpy(resp, getResponse.data(), *data_len);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530443 resp->operation = 1 << scanningEnabledBit;
444 return IPMI_CC_OK;
445 }
Brandon Kim9cf85622019-06-19 12:05:08 -0700446#ifdef UPDATE_FUNCTIONAL_ON_FAIL
447 catch (const SensorFunctionalError& e)
448 {
449 return IPMI_CC_RESPONSE_ERROR;
450 }
451#endif
Tom Joseph3ee668f2018-03-02 19:49:17 +0530452 catch (const std::exception& e)
453 {
454 *data_len = getResponse.size();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700455 std::memcpy(resp, getResponse.data(), *data_len);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530456 return IPMI_CC_OK;
457 }
458}
459
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530460void getSensorThresholds(uint8_t sensorNum,
461 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600462{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530463 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600464 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530465 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600466 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600467
468 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
469
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530470 const auto iter = sensors.find(sensorNum);
471 const auto info = iter->second;
472
473 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
474
Patrick Venture0b02be92018-08-31 11:55:55 -0700475 auto warnThresholds = ipmi::getAllDbusProperties(
476 bus, service, info.sensorPath, warningThreshIntf);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530477
Vernon Maueryf442e112019-04-09 11:44:36 -0700478 double warnLow = std::visit(ipmi::VariantToDoubleVisitor(),
479 warnThresholds["WarningLow"]);
480 double warnHigh = std::visit(ipmi::VariantToDoubleVisitor(),
481 warnThresholds["WarningHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530482
483 if (warnLow != 0)
484 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700485 warnLow *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700486 response->lowerNonCritical = static_cast<uint8_t>(
487 (warnLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530488 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700489 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530490 }
491
492 if (warnHigh != 0)
493 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700494 warnHigh *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700495 response->upperNonCritical = static_cast<uint8_t>(
496 (warnHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530497 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700498 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530499 }
500
Patrick Venture0b02be92018-08-31 11:55:55 -0700501 auto critThresholds = ipmi::getAllDbusProperties(
502 bus, service, info.sensorPath, criticalThreshIntf);
Vernon Maueryf442e112019-04-09 11:44:36 -0700503 double critLow = std::visit(ipmi::VariantToDoubleVisitor(),
504 critThresholds["CriticalLow"]);
505 double critHigh = std::visit(ipmi::VariantToDoubleVisitor(),
506 critThresholds["CriticalHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530507
508 if (critLow != 0)
509 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700510 critLow *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700511 response->lowerCritical = static_cast<uint8_t>(
512 (critLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530513 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700514 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530515 }
516
517 if (critHigh != 0)
518 {
Patrick Venture586d35b2018-09-07 19:56:18 -0700519 critHigh *= std::pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700520 response->upperCritical = static_cast<uint8_t>(
521 (critHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530522 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700523 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530524 }
525}
526
527ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700528 ipmi_request_t request,
529 ipmi_response_t response,
530 ipmi_data_len_t data_len,
531 ipmi_context_t context)
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530532{
533 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
534
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600535 if (*data_len != sizeof(uint8_t))
536 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530537 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600538 return IPMI_CC_REQ_DATA_LEN_INVALID;
539 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600540
Patrick Venture0b02be92018-08-31 11:55:55 -0700541 auto sensorNum = *(reinterpret_cast<const uint8_t*>(request));
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530542 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600543
544 const auto iter = sensors.find(sensorNum);
545 if (iter == sensors.end())
546 {
547 return IPMI_CC_SENSOR_INVALID;
548 }
549
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530550 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600551
Patrick Venture0b02be92018-08-31 11:55:55 -0700552 // Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530553 if (info.propertyInterfaces.find(valueInterface) ==
554 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600555 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700556 // return with valid mask as 0
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600557 return IPMI_CC_OK;
558 }
559
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530560 auto responseData =
561 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600562
563 try
564 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530565 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600566 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530567 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600568 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700569 // Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600570 responseData->validMask = 0;
571 }
572
573 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
574 return IPMI_CC_OK;
575}
576
Chris Austen0012e9b2015-10-22 01:37:46 -0500577ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
578 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500579 ipmi_data_len_t data_len, ipmi_context_t context)
580{
Nan Li70aa8d92016-08-29 00:11:10 +0800581 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500582
Patrick Venture0b02be92018-08-31 11:55:55 -0700583 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n", netfn, cmd);
Chris Austenac4604a2015-10-13 12:43:27 -0500584 *data_len = 0;
585
586 return rc;
587}
588
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000589/** @brief implements the get SDR Info command
590 * @param count - Operation
591 *
592 * @returns IPMI completion code plus response data
593 * - sdrCount - sensor/SDR count
594 * - lunsAndDynamicPopulation - static/Dynamic sensor population flag
595 */
596ipmi::RspType<uint8_t, // respcount
597 uint8_t // dynamic population flags
598 >
599 ipmiSensorGetDeviceSdrInfo(std::optional<uint8_t> count)
Emily Shafferd06e0e72017-04-05 09:08:57 -0700600{
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000601 uint8_t sdrCount;
602 // multiple LUNs not supported.
603 constexpr uint8_t lunsAndDynamicPopulation = 1;
604 constexpr uint8_t getSdrCount = 0x01;
605 constexpr uint8_t getSensorCount = 0x00;
606
607 if (count.value_or(0) == getSdrCount)
Emily Shafferd06e0e72017-04-05 09:08:57 -0700608 {
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000609 // Get SDR count. This returns the total number of SDRs in the device.
Patrick Venture235b3c72019-08-23 09:16:07 -0700610 const auto& entityRecords = ipmi::sensor::getIpmiEntityRecords();
Patrick Venture83a0b842019-07-19 18:37:15 -0700611 sdrCount = sensors.size() + frus.size() + entityRecords.size();
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000612 }
613 else if (count.value_or(0) == getSensorCount)
614 {
615 // Get Sensor count. This returns the number of sensors
616 sdrCount = sensors.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700617 }
618 else
619 {
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000620 return ipmi::responseInvalidCommandOnLun();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700621 }
622
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000623 return ipmi::responseSuccess(sdrCount, lunsAndDynamicPopulation);
Emily Shafferd06e0e72017-04-05 09:08:57 -0700624}
625
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000626/** @brief implements the reserve SDR command
627 * @returns IPMI completion code plus response data
628 * - reservationID - reservation ID
629 */
630ipmi::RspType<uint16_t> ipmiSensorReserveSdr()
Emily Shaffera344afc2017-04-13 15:09:39 -0700631{
632 // A constant reservation ID is okay until we implement add/remove SDR.
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000633 constexpr uint16_t reservationID = 1;
Emily Shaffera344afc2017-04-13 15:09:39 -0700634
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000635 return ipmi::responseSuccess(reservationID);
Emily Shaffera344afc2017-04-13 15:09:39 -0700636}
Chris Austenac4604a2015-10-13 12:43:27 -0500637
Patrick Venture0b02be92018-08-31 11:55:55 -0700638void setUnitFieldsForObject(const ipmi::sensor::Info* info,
639 get_sdr::SensorDataFullRecordBody* body)
Emily Shaffercc941e12017-06-14 13:06:26 -0700640{
Tom Josephdc212b22018-02-16 09:59:57 +0530641 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
642 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700643 {
Tom Josephdc212b22018-02-16 09:59:57 +0530644 auto unit = server::Value::convertUnitFromString(info->unit);
645 // Unit strings defined in
646 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
647 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700648 {
Tom Josephdc212b22018-02-16 09:59:57 +0530649 case server::Value::Unit::DegreesC:
650 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
651 break;
652 case server::Value::Unit::RPMS:
Kirill Pakhomov812e44c2018-10-22 16:25:35 +0300653 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_RPM;
Tom Josephdc212b22018-02-16 09:59:57 +0530654 break;
655 case server::Value::Unit::Volts:
656 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
657 break;
658 case server::Value::Unit::Meters:
659 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
660 break;
661 case server::Value::Unit::Amperes:
662 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
663 break;
664 case server::Value::Unit::Joules:
665 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
666 break;
667 case server::Value::Unit::Watts:
668 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
669 break;
670 default:
671 // Cannot be hit.
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700672 std::fprintf(stderr, "Unknown value unit type: = %s\n",
673 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700674 }
675 }
Patrick Venture64678b82018-10-13 13:11:32 -0700676 catch (const sdbusplus::exception::InvalidEnumString& e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700677 {
Tom Josephdc212b22018-02-16 09:59:57 +0530678 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700679 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700680}
681
Patrick Venture0b02be92018-08-31 11:55:55 -0700682ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body,
683 const ipmi::sensor::Info* info,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700684 ipmi_data_len_t data_len)
685{
686 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700687 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700688 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700689
690 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
691
692 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530693 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700694
695 get_sdr::body::set_b(info->coefficientB, body);
696 get_sdr::body::set_m(info->coefficientM, body);
697 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530698 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700699
Emily Shafferbbef71c2017-05-08 16:36:17 -0700700 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700701 }
702
Tom Joseph96423912018-01-25 00:14:34 +0530703 /* ID string */
704 auto id_string = info->sensorNameFunc(*info);
705
706 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
707 {
708 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
709 }
710 else
711 {
712 get_sdr::body::set_id_strlen(id_string.length(), body);
713 }
714 strncpy(body->id_string, id_string.c_str(),
715 get_sdr::body::get_id_strlen(body));
716
Emily Shafferbbef71c2017-05-08 16:36:17 -0700717 return IPMI_CC_OK;
718};
719
Ratan Guptae0cc8552018-01-22 14:23:04 +0530720ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
721 ipmi_data_len_t data_len)
722{
723 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
724 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700725 get_sdr::SensorDataFruRecord record{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530726 auto dataLength = 0;
727
728 auto fru = frus.begin();
Patrick Venture0b02be92018-08-31 11:55:55 -0700729 uint8_t fruID{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530730 auto recordID = get_sdr::request::get_record_id(req);
731
732 fruID = recordID - FRU_RECORD_ID_START;
733 fru = frus.find(fruID);
734 if (fru == frus.end())
735 {
736 return IPMI_CC_SENSOR_INVALID;
737 }
738
739 /* Header */
740 get_sdr::header::set_record_id(recordID, &(record.header));
741 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
742 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
743 record.header.record_length = sizeof(record.key) + sizeof(record.body);
744
745 /* Key */
746 record.key.fruID = fruID;
747 record.key.accessLun |= IPMI_LOGICAL_FRU;
748 record.key.deviceAddress = BMCSlaveAddress;
749
750 /* Body */
751 record.body.entityID = fru->second[0].entityID;
752 record.body.entityInstance = fru->second[0].entityInstance;
753 record.body.deviceType = fruInventoryDevice;
754 record.body.deviceTypeModifier = IPMIFruInventory;
755
756 /* Device ID string */
Patrick Venture0b02be92018-08-31 11:55:55 -0700757 auto deviceID =
758 fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1,
759 fru->second[0].path.length());
Ratan Guptae0cc8552018-01-22 14:23:04 +0530760
761 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
762 {
763 get_sdr::body::set_device_id_strlen(
Patrick Venture0b02be92018-08-31 11:55:55 -0700764 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530765 }
766 else
767 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700768 get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530769 }
770
771 strncpy(record.body.deviceID, deviceID.c_str(),
772 get_sdr::body::get_device_id_strlen(&(record.body)));
773
774 if (++fru == frus.end())
775 {
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800776 // we have reached till end of fru, so assign the next record id to
777 // 512(Max fru ID = 511) + Entity Record ID(may start with 0).
Patrick Venture235b3c72019-08-23 09:16:07 -0700778 const auto& entityRecords = ipmi::sensor::getIpmiEntityRecords();
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800779 auto next_record_id =
Patrick Venture83a0b842019-07-19 18:37:15 -0700780 (entityRecords.size())
781 ? entityRecords.begin()->first + ENTITY_RECORD_ID_START
782 : END_OF_RECORD;
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800783 get_sdr::response::set_next_record_id(next_record_id, resp);
784 }
785 else
786 {
787 get_sdr::response::set_next_record_id(
788 (FRU_RECORD_ID_START + fru->first), resp);
789 }
790
791 // Check for invalid offset size
792 if (req->offset > sizeof(record))
793 {
794 return IPMI_CC_PARM_OUT_OF_RANGE;
795 }
796
797 dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
798 sizeof(record) - req->offset);
799
800 std::memcpy(resp->record_data,
801 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
802
803 *data_len = dataLength;
804 *data_len += 2; // additional 2 bytes for next record ID
805
806 return IPMI_CC_OK;
807}
808
809ipmi_ret_t ipmi_entity_get_sdr(ipmi_request_t request, ipmi_response_t response,
810 ipmi_data_len_t data_len)
811{
812 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
813 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
814 get_sdr::SensorDataEntityRecord record{};
815 auto dataLength = 0;
816
Patrick Venture235b3c72019-08-23 09:16:07 -0700817 const auto& entityRecords = ipmi::sensor::getIpmiEntityRecords();
Patrick Venture83a0b842019-07-19 18:37:15 -0700818 auto entity = entityRecords.begin();
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800819 uint8_t entityRecordID;
820 auto recordID = get_sdr::request::get_record_id(req);
821
822 entityRecordID = recordID - ENTITY_RECORD_ID_START;
Patrick Venture83a0b842019-07-19 18:37:15 -0700823 entity = entityRecords.find(entityRecordID);
824 if (entity == entityRecords.end())
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800825 {
826 return IPMI_CC_SENSOR_INVALID;
827 }
828
829 /* Header */
830 get_sdr::header::set_record_id(recordID, &(record.header));
831 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
832 record.header.record_type = get_sdr::SENSOR_DATA_ENTITY_RECORD;
833 record.header.record_length = sizeof(record.key) + sizeof(record.body);
834
835 /* Key */
836 record.key.containerEntityId = entity->second.containerEntityId;
837 record.key.containerEntityInstance = entity->second.containerEntityInstance;
838 get_sdr::key::set_flags(entity->second.isList, entity->second.isLinked,
839 &(record.key));
840 record.key.entityId1 = entity->second.containedEntities[0].first;
841 record.key.entityInstance1 = entity->second.containedEntities[0].second;
842
843 /* Body */
844 record.body.entityId2 = entity->second.containedEntities[1].first;
845 record.body.entityInstance2 = entity->second.containedEntities[1].second;
846 record.body.entityId3 = entity->second.containedEntities[2].first;
847 record.body.entityInstance3 = entity->second.containedEntities[2].second;
848 record.body.entityId4 = entity->second.containedEntities[3].first;
849 record.body.entityInstance4 = entity->second.containedEntities[3].second;
850
Patrick Venture83a0b842019-07-19 18:37:15 -0700851 if (++entity == entityRecords.end())
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800852 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700853 get_sdr::response::set_next_record_id(END_OF_RECORD,
854 resp); // last record
Ratan Guptae0cc8552018-01-22 14:23:04 +0530855 }
856 else
857 {
858 get_sdr::response::set_next_record_id(
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800859 (ENTITY_RECORD_ID_START + entity->first), resp);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530860 }
861
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700862 // Check for invalid offset size
863 if (req->offset > sizeof(record))
Ratan Guptae0cc8552018-01-22 14:23:04 +0530864 {
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700865 return IPMI_CC_PARM_OUT_OF_RANGE;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530866 }
867
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700868 dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
869 sizeof(record) - req->offset);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530870
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700871 std::memcpy(resp->record_data,
Jason M. Bills1cd85962018-10-05 12:04:01 -0700872 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530873
874 *data_len = dataLength;
875 *data_len += 2; // additional 2 bytes for next record ID
876
877 return IPMI_CC_OK;
878}
879
Emily Shafferbbef71c2017-05-08 16:36:17 -0700880ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
881 ipmi_request_t request, ipmi_response_t response,
882 ipmi_data_len_t data_len, ipmi_context_t context)
883{
884 ipmi_ret_t ret = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700885 get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request;
886 get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700887 get_sdr::SensorDataFullRecord record = {0};
Patrick Venture38426dd2019-07-30 15:22:29 -0700888
889 // Note: we use an iterator so we can provide the next ID at the end of
890 // the call.
891 auto sensor = sensors.begin();
892 auto recordID = get_sdr::request::get_record_id(req);
893
894 // At the beginning of a scan, the host side will send us id=0.
895 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700896 {
Patrick Venture38426dd2019-07-30 15:22:29 -0700897 // recordID 0 to 255 means it is a FULL record.
898 // recordID 256 to 511 means it is a FRU record.
899 // recordID greater then 511 means it is a Entity Association
900 // record. Currently we are supporting three record types: FULL
901 // record, FRU record and Enttiy Association record.
902 if (recordID >= ENTITY_RECORD_ID_START)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700903 {
Patrick Venture38426dd2019-07-30 15:22:29 -0700904 return ipmi_entity_get_sdr(request, response, data_len);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700905 }
Patrick Venture38426dd2019-07-30 15:22:29 -0700906 else if (recordID >= FRU_RECORD_ID_START &&
907 recordID < ENTITY_RECORD_ID_START)
Jaghathiswari Rankappagounder Natarajan0780df12019-02-06 15:29:24 -0800908 {
Patrick Venture38426dd2019-07-30 15:22:29 -0700909 return ipmi_fru_get_sdr(request, response, data_len);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700910 }
911 else
912 {
Patrick Venture38426dd2019-07-30 15:22:29 -0700913 sensor = sensors.find(recordID);
914 if (sensor == sensors.end())
915 {
916 return IPMI_CC_SENSOR_INVALID;
917 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700918 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700919 }
920
Patrick Venture38426dd2019-07-30 15:22:29 -0700921 uint8_t sensor_id = sensor->first;
922
923 /* Header */
924 get_sdr::header::set_record_id(sensor_id, &(record.header));
925 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
926 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
927 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
928
929 /* Key */
930 get_sdr::key::set_owner_id_bmc(&(record.key));
931 record.key.sensor_number = sensor_id;
932
933 /* Body */
934 record.body.entity_id = sensor->second.entityType;
935 record.body.sensor_type = sensor->second.sensorType;
936 record.body.event_reading_type = sensor->second.sensorReadingType;
937 record.body.entity_instance = sensor->second.instance;
938 if (ipmi::sensor::Mutability::Write ==
939 (sensor->second.mutability & ipmi::sensor::Mutability::Write))
940 {
941 get_sdr::body::init_settable_state(true, &(record.body));
942 }
943
944 // Set the type-specific details given the DBus interface
945 ret =
946 populate_record_from_dbus(&(record.body), &(sensor->second), data_len);
947
948 if (++sensor == sensors.end())
949 {
950 // we have reached till end of sensor, so assign the next record id
951 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
952 auto next_record_id = (frus.size())
953 ? frus.begin()->first + FRU_RECORD_ID_START
954 : END_OF_RECORD;
955
956 get_sdr::response::set_next_record_id(next_record_id, resp);
957 }
958 else
959 {
960 get_sdr::response::set_next_record_id(sensor->first, resp);
961 }
962
963 if (req->offset > sizeof(record))
964 {
965 return IPMI_CC_PARM_OUT_OF_RANGE;
966 }
967
968 // data_len will ultimately be the size of the record, plus
969 // the size of the next record ID:
970 *data_len = std::min(static_cast<size_t>(req->bytes_to_read),
971 sizeof(record) - req->offset);
972
973 std::memcpy(resp->record_data,
974 reinterpret_cast<uint8_t*>(&record) + req->offset, *data_len);
975
976 // data_len should include the LSB and MSB:
977 *data_len +=
978 sizeof(resp->next_record_id_lsb) + sizeof(resp->next_record_id_msb);
979
Emily Shafferbbef71c2017-05-08 16:36:17 -0700980 return ret;
981}
982
Jia, Chunhui3342a8e2018-12-29 13:32:26 +0800983static bool isFromSystemChannel()
984{
985 // TODO we could not figure out where the request is from based on IPMI
986 // command handler parameters. because of it, we can not differentiate
987 // request from SMS/SMM or IPMB channel
988 return true;
989}
990
991ipmi_ret_t ipmicmdPlatformEvent(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
992 ipmi_request_t request,
993 ipmi_response_t response,
994 ipmi_data_len_t dataLen, ipmi_context_t context)
995{
996 uint16_t generatorID;
997 size_t count;
998 bool assert = true;
999 std::string sensorPath;
1000 size_t paraLen = *dataLen;
1001 PlatformEventRequest* req;
1002 *dataLen = 0;
1003
1004 if ((paraLen < selSystemEventSizeWith1Bytes) ||
1005 (paraLen > selSystemEventSizeWith3Bytes))
1006 {
1007 return IPMI_CC_REQ_DATA_LEN_INVALID;
1008 }
1009
1010 if (isFromSystemChannel())
1011 { // first byte for SYSTEM Interface is Generator ID
1012 // +1 to get common struct
1013 req = reinterpret_cast<PlatformEventRequest*>((uint8_t*)request + 1);
1014 // Capture the generator ID
1015 generatorID = *reinterpret_cast<uint8_t*>(request);
1016 // Platform Event usually comes from other firmware, like BIOS.
1017 // Unlike BMC sensor, it does not have BMC DBUS sensor path.
1018 sensorPath = "System";
1019 }
1020 else
1021 {
1022 req = reinterpret_cast<PlatformEventRequest*>(request);
1023 // TODO GenratorID for IPMB is combination of RqSA and RqLUN
1024 generatorID = 0xff;
1025 sensorPath = "IPMB";
1026 }
1027 // Content of event data field depends on sensor class.
1028 // When data0 bit[5:4] is non-zero, valid data counts is 3.
1029 // When data0 bit[7:6] is non-zero, valid data counts is 2.
1030 if (((req->data[0] & byte3EnableMask) != 0 &&
1031 paraLen < selSystemEventSizeWith3Bytes) ||
1032 ((req->data[0] & byte2EnableMask) != 0 &&
1033 paraLen < selSystemEventSizeWith2Bytes))
1034 {
1035 return IPMI_CC_REQ_DATA_LEN_INVALID;
1036 }
1037
1038 // Count bytes of Event Data
1039 if ((req->data[0] & byte3EnableMask) != 0)
1040 {
1041 count = 3;
1042 }
1043 else if ((req->data[0] & byte2EnableMask) != 0)
1044 {
1045 count = 2;
1046 }
1047 else
1048 {
1049 count = 1;
1050 }
1051 assert = req->eventDirectionType & directionMask ? false : true;
1052 std::vector<uint8_t> eventData(req->data, req->data + count);
1053
1054 sdbusplus::bus::bus dbus(bus);
1055 std::string service =
1056 ipmi::getService(dbus, ipmiSELAddInterface, ipmiSELPath);
1057 sdbusplus::message::message writeSEL = dbus.new_method_call(
1058 service.c_str(), ipmiSELPath, ipmiSELAddInterface, "IpmiSelAdd");
1059 writeSEL.append(ipmiSELAddMessage, sensorPath, eventData, assert,
1060 generatorID);
1061 try
1062 {
1063 dbus.call(writeSEL);
1064 }
1065 catch (sdbusplus::exception_t& e)
1066 {
1067 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1068 return IPMI_CC_UNSPECIFIED_ERROR;
1069 }
1070 return IPMI_CC_OK;
1071}
1072
Chris Austenac4604a2015-10-13 12:43:27 -05001073void register_netfn_sen_functions()
1074{
Tom05732372016-09-06 17:21:23 +05301075 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001076 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr,
1077 ipmi_sen_wildcard, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001078
Jia, Chunhui3342a8e2018-12-29 13:32:26 +08001079 // <Platform Event Message>
1080 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_PLATFORM_EVENT, nullptr,
1081 ipmicmdPlatformEvent, PRIVILEGE_OPERATOR);
Tom05732372016-09-06 17:21:23 +05301082 // <Get Sensor Type>
Patrick Venture0b02be92018-08-31 11:55:55 -07001083 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, nullptr,
1084 ipmi_sen_get_sensor_type, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001085
Tom05732372016-09-06 17:21:23 +05301086 // <Set Sensor Reading and Event Status>
Deepak Kumar Sahu9da3a752019-05-21 00:45:14 +00001087 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1088 ipmi::sensor_event::cmdSetSensorReadingAndEvtSts,
1089 ipmi::Privilege::Operator, ipmiSetSensorReading);
Tom05732372016-09-06 17:21:23 +05301090 // <Get Sensor Reading>
Patrick Venture0b02be92018-08-31 11:55:55 -07001091 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, nullptr,
1092 ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
Emily Shaffera344afc2017-04-13 15:09:39 -07001093
Tom Joseph5ca50952018-02-22 00:33:38 +05301094 // <Reserve Device SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +00001095 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1096 ipmi::sensor_event::cmdReserveDeviceSdrRepository,
1097 ipmi::Privilege::User, ipmiSensorReserveSdr);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001098
Tom Joseph5ca50952018-02-22 00:33:38 +05301099 // <Get Device SDR Info>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +00001100 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1101 ipmi::sensor_event::cmdGetDeviceSdrInfo,
1102 ipmi::Privilege::User, ipmiSensorGetDeviceSdrInfo);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001103
Tom Joseph5ca50952018-02-22 00:33:38 +05301104 // <Get Device SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -07001105 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr,
1106 ipmi_sen_get_sdr, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001107
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001108 // <Get Sensor Thresholds>
1109 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1110 nullptr, ipmi_sen_get_sensor_thresholds,
1111 PRIVILEGE_USER);
1112
Chris Austenac4604a2015-10-13 12:43:27 -05001113 return;
1114}